blob: 976c38f67e708043402e2a4dd799739303162757 [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
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100630static psa_status_t setup_psa_key_derivation(psa_key_derivation_operation_t *derivation,
631 psa_key_id_t key,
632 psa_algorithm_t alg,
633 const unsigned char *seed, size_t seed_length,
634 const unsigned char *label, size_t label_length,
635 size_t capacity)
k-stachowiak81053a52019-08-17 10:30:28 +0200636{
637 psa_status_t status;
638
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100639 status = psa_key_derivation_setup(derivation, alg);
640 if (status != PSA_SUCCESS) {
641 return status;
642 }
k-stachowiak81053a52019-08-17 10:30:28 +0200643
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100644 if (PSA_ALG_IS_TLS12_PRF(alg) || PSA_ALG_IS_TLS12_PSK_TO_MS(alg)) {
645 status = psa_key_derivation_input_bytes(derivation,
646 PSA_KEY_DERIVATION_INPUT_SEED,
647 seed, seed_length);
648 if (status != PSA_SUCCESS) {
649 return status;
650 }
k-stachowiak81053a52019-08-17 10:30:28 +0200651
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100652 if (mbedtls_svc_key_id_is_null(key)) {
Gilles Peskine311f54d2019-09-23 18:19:22 +0200653 status = psa_key_derivation_input_bytes(
654 derivation, PSA_KEY_DERIVATION_INPUT_SECRET,
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100655 NULL, 0);
656 } else {
Gilles Peskine311f54d2019-09-23 18:19:22 +0200657 status = psa_key_derivation_input_key(
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100658 derivation, PSA_KEY_DERIVATION_INPUT_SECRET, key);
Gilles Peskine311f54d2019-09-23 18:19:22 +0200659 }
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100660 if (status != PSA_SUCCESS) {
661 return status;
662 }
k-stachowiak81053a52019-08-17 10:30:28 +0200663
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100664 status = psa_key_derivation_input_bytes(derivation,
665 PSA_KEY_DERIVATION_INPUT_LABEL,
666 label, label_length);
667 if (status != PSA_SUCCESS) {
668 return status;
669 }
670 } else {
671 return PSA_ERROR_NOT_SUPPORTED;
k-stachowiak81053a52019-08-17 10:30:28 +0200672 }
673
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100674 status = psa_key_derivation_set_capacity(derivation, capacity);
675 if (status != PSA_SUCCESS) {
676 return status;
677 }
k-stachowiak81053a52019-08-17 10:30:28 +0200678
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100679 return PSA_SUCCESS;
k-stachowiak81053a52019-08-17 10:30:28 +0200680}
681
Manuel Pégourié-Gonnardd904d662022-06-17 10:24:00 +0200682MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100683static int tls_prf_generic(mbedtls_md_type_t md_type,
684 const unsigned char *secret, size_t slen,
685 const char *label,
686 const unsigned char *random, size_t rlen,
687 unsigned char *dstbuf, size_t dlen)
Andrzej Kurekc929a822019-01-14 03:51:11 -0500688{
689 psa_status_t status;
690 psa_algorithm_t alg;
Ronald Croncf56a0a2020-08-04 09:51:30 +0200691 psa_key_id_t master_key = MBEDTLS_SVC_KEY_ID_INIT;
Janos Follathda6ac012019-08-16 13:47:29 +0100692 psa_key_derivation_operation_t derivation =
Janos Follath8dee8772019-07-30 12:53:32 +0100693 PSA_KEY_DERIVATION_OPERATION_INIT;
Andrzej Kurekc929a822019-01-14 03:51:11 -0500694
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100695 if (md_type == MBEDTLS_MD_SHA384) {
Andrzej Kurekc929a822019-01-14 03:51:11 -0500696 alg = PSA_ALG_TLS12_PRF(PSA_ALG_SHA_384);
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100697 } else {
Andrzej Kurekc929a822019-01-14 03:51:11 -0500698 alg = PSA_ALG_TLS12_PRF(PSA_ALG_SHA_256);
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100699 }
Andrzej Kurekc929a822019-01-14 03:51:11 -0500700
Gilles Peskine311f54d2019-09-23 18:19:22 +0200701 /* Normally a "secret" should be long enough to be impossible to
702 * find by brute force, and in particular should not be empty. But
703 * this PRF is also used to derive an IV, in particular in EAP-TLS,
704 * and for this use case it makes sense to have a 0-length "secret".
705 * Since the key API doesn't allow importing a key of length 0,
Ronald Croncf56a0a2020-08-04 09:51:30 +0200706 * keep master_key=0, which setup_psa_key_derivation() understands
Gilles Peskine311f54d2019-09-23 18:19:22 +0200707 * to mean a 0-length "secret" input. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100708 if (slen != 0) {
Gilles Peskine311f54d2019-09-23 18:19:22 +0200709 psa_key_attributes_t key_attributes = psa_key_attributes_init();
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100710 psa_set_key_usage_flags(&key_attributes, PSA_KEY_USAGE_DERIVE);
711 psa_set_key_algorithm(&key_attributes, alg);
712 psa_set_key_type(&key_attributes, PSA_KEY_TYPE_DERIVE);
Andrzej Kurekc929a822019-01-14 03:51:11 -0500713
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100714 status = psa_import_key(&key_attributes, secret, slen, &master_key);
715 if (status != PSA_SUCCESS) {
716 return MBEDTLS_ERR_SSL_HW_ACCEL_FAILED;
717 }
Gilles Peskine311f54d2019-09-23 18:19:22 +0200718 }
Andrzej Kurekc929a822019-01-14 03:51:11 -0500719
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100720 status = setup_psa_key_derivation(&derivation,
721 master_key, alg,
722 random, rlen,
723 (unsigned char const *) label,
724 (size_t) strlen(label),
725 dlen);
726 if (status != PSA_SUCCESS) {
727 psa_key_derivation_abort(&derivation);
728 psa_destroy_key(master_key);
729 return MBEDTLS_ERR_SSL_HW_ACCEL_FAILED;
Andrzej Kurekc929a822019-01-14 03:51:11 -0500730 }
731
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100732 status = psa_key_derivation_output_bytes(&derivation, dstbuf, dlen);
733 if (status != PSA_SUCCESS) {
734 psa_key_derivation_abort(&derivation);
735 psa_destroy_key(master_key);
736 return MBEDTLS_ERR_SSL_HW_ACCEL_FAILED;
Andrzej Kurekc929a822019-01-14 03:51:11 -0500737 }
738
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100739 status = psa_key_derivation_abort(&derivation);
740 if (status != PSA_SUCCESS) {
741 psa_destroy_key(master_key);
742 return MBEDTLS_ERR_SSL_HW_ACCEL_FAILED;
Andrzej Kurek70737ca2019-01-14 05:37:13 -0500743 }
Andrzej Kurekc929a822019-01-14 03:51:11 -0500744
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100745 if (!mbedtls_svc_key_id_is_null(master_key)) {
746 status = psa_destroy_key(master_key);
747 }
748 if (status != PSA_SUCCESS) {
749 return MBEDTLS_ERR_SSL_HW_ACCEL_FAILED;
750 }
Andrzej Kurekc929a822019-01-14 03:51:11 -0500751
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100752 return 0;
Andrzej Kurekc929a822019-01-14 03:51:11 -0500753}
754
755#else /* MBEDTLS_USE_PSA_CRYPTO */
756
Manuel Pégourié-Gonnardd904d662022-06-17 10:24:00 +0200757MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100758static int tls_prf_generic(mbedtls_md_type_t md_type,
759 const unsigned char *secret, size_t slen,
760 const char *label,
761 const unsigned char *random, size_t rlen,
762 unsigned char *dstbuf, size_t dlen)
Paul Bakker1ef83d62012-04-11 12:09:53 +0000763{
764 size_t nb;
Manuel Pégourié-Gonnard6890c6b2015-03-26 11:11:49 +0100765 size_t i, j, k, md_len;
Ron Eldor3b350852019-05-07 18:31:49 +0300766 unsigned char *tmp;
767 size_t tmp_len = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200768 unsigned char h_i[MBEDTLS_MD_MAX_SIZE];
769 const mbedtls_md_info_t *md_info;
770 mbedtls_md_context_t md_ctx;
Janos Follath865b3eb2019-12-16 11:46:15 +0000771 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnardb7fcca32015-03-26 11:41:28 +0100772
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100773 mbedtls_md_init(&md_ctx);
Paul Bakker1ef83d62012-04-11 12:09:53 +0000774
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100775 if ((md_info = mbedtls_md_info_from_type(md_type)) == NULL) {
776 return MBEDTLS_ERR_SSL_INTERNAL_ERROR;
777 }
Manuel Pégourié-Gonnard6890c6b2015-03-26 11:11:49 +0100778
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100779 md_len = mbedtls_md_get_size(md_info);
Manuel Pégourié-Gonnard6890c6b2015-03-26 11:11:49 +0100780
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100781 tmp_len = md_len + strlen(label) + rlen;
782 tmp = mbedtls_calloc(1, tmp_len);
783 if (tmp == NULL) {
Ron Eldor3b350852019-05-07 18:31:49 +0300784 ret = MBEDTLS_ERR_SSL_ALLOC_FAILED;
785 goto exit;
786 }
Paul Bakker1ef83d62012-04-11 12:09:53 +0000787
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100788 nb = strlen(label);
789 memcpy(tmp + md_len, label, nb);
790 memcpy(tmp + md_len + nb, random, rlen);
Paul Bakker1ef83d62012-04-11 12:09:53 +0000791 nb += rlen;
792
793 /*
794 * Compute P_<hash>(secret, label + random)[0..dlen]
795 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100796 if ((ret = mbedtls_md_setup(&md_ctx, md_info, 1)) != 0) {
Ron Eldor3b350852019-05-07 18:31:49 +0300797 goto exit;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100798 }
Manuel Pégourié-Gonnardb7fcca32015-03-26 11:41:28 +0100799
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100800 ret = mbedtls_md_hmac_starts(&md_ctx, secret, slen);
801 if (ret != 0) {
Gilles Peskine2b3f21d2021-12-10 21:35:10 +0100802 goto exit;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100803 }
804 ret = mbedtls_md_hmac_update(&md_ctx, tmp + md_len, nb);
805 if (ret != 0) {
Gilles Peskine2b3f21d2021-12-10 21:35:10 +0100806 goto exit;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100807 }
808 ret = mbedtls_md_hmac_finish(&md_ctx, tmp);
809 if (ret != 0) {
Gilles Peskine2b3f21d2021-12-10 21:35:10 +0100810 goto exit;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100811 }
Manuel Pégourié-Gonnard7da726b2015-03-24 18:08:19 +0100812
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100813 for (i = 0; i < dlen; i += md_len) {
814 ret = mbedtls_md_hmac_reset(&md_ctx);
815 if (ret != 0) {
Gilles Peskine2b3f21d2021-12-10 21:35:10 +0100816 goto exit;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100817 }
818 ret = mbedtls_md_hmac_update(&md_ctx, tmp, md_len + nb);
819 if (ret != 0) {
Gilles Peskine2b3f21d2021-12-10 21:35:10 +0100820 goto exit;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100821 }
822 ret = mbedtls_md_hmac_finish(&md_ctx, h_i);
823 if (ret != 0) {
Gilles Peskine2b3f21d2021-12-10 21:35:10 +0100824 goto exit;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100825 }
Manuel Pégourié-Gonnardb7fcca32015-03-26 11:41:28 +0100826
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100827 ret = mbedtls_md_hmac_reset(&md_ctx);
828 if (ret != 0) {
Gilles Peskine2b3f21d2021-12-10 21:35:10 +0100829 goto exit;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100830 }
831 ret = mbedtls_md_hmac_update(&md_ctx, tmp, md_len);
832 if (ret != 0) {
Gilles Peskine2b3f21d2021-12-10 21:35:10 +0100833 goto exit;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100834 }
835 ret = mbedtls_md_hmac_finish(&md_ctx, tmp);
836 if (ret != 0) {
Gilles Peskine2b3f21d2021-12-10 21:35:10 +0100837 goto exit;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100838 }
Paul Bakker1ef83d62012-04-11 12:09:53 +0000839
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100840 k = (i + md_len > dlen) ? dlen % md_len : md_len;
Paul Bakker1ef83d62012-04-11 12:09:53 +0000841
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100842 for (j = 0; j < k; j++) {
Paul Bakker1ef83d62012-04-11 12:09:53 +0000843 dstbuf[i + j] = h_i[j];
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100844 }
Paul Bakker1ef83d62012-04-11 12:09:53 +0000845 }
846
Ron Eldor3b350852019-05-07 18:31:49 +0300847exit:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100848 mbedtls_md_free(&md_ctx);
Manuel Pégourié-Gonnardb7fcca32015-03-26 11:41:28 +0100849
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100850 if (tmp != NULL) {
851 mbedtls_platform_zeroize(tmp, tmp_len);
852 }
Dave Rodgman369f4952022-11-01 16:08:14 +0000853
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100854 mbedtls_platform_zeroize(h_i, sizeof(h_i));
Paul Bakker1ef83d62012-04-11 12:09:53 +0000855
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100856 mbedtls_free(tmp);
Ron Eldor3b350852019-05-07 18:31:49 +0300857
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100858 return ret;
Paul Bakker1ef83d62012-04-11 12:09:53 +0000859}
Andrzej Kurekc929a822019-01-14 03:51:11 -0500860#endif /* MBEDTLS_USE_PSA_CRYPTO */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200861#if defined(MBEDTLS_SHA256_C)
Manuel Pégourié-Gonnardd904d662022-06-17 10:24:00 +0200862MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100863static int tls_prf_sha256(const unsigned char *secret, size_t slen,
864 const char *label,
865 const unsigned char *random, size_t rlen,
866 unsigned char *dstbuf, size_t dlen)
Manuel Pégourié-Gonnard6890c6b2015-03-26 11:11:49 +0100867{
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100868 return tls_prf_generic(MBEDTLS_MD_SHA256, secret, slen,
869 label, random, rlen, dstbuf, dlen);
Manuel Pégourié-Gonnard6890c6b2015-03-26 11:11:49 +0100870}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200871#endif /* MBEDTLS_SHA256_C */
Paul Bakker1ef83d62012-04-11 12:09:53 +0000872
Gilles Peskined2d59372021-05-12 22:43:27 +0200873#if defined(MBEDTLS_SHA512_C) && !defined(MBEDTLS_SHA512_NO_SHA384)
Manuel Pégourié-Gonnardd904d662022-06-17 10:24:00 +0200874MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100875static int tls_prf_sha384(const unsigned char *secret, size_t slen,
876 const char *label,
877 const unsigned char *random, size_t rlen,
878 unsigned char *dstbuf, size_t dlen)
Paul Bakkerca4ab492012-04-18 14:23:57 +0000879{
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100880 return tls_prf_generic(MBEDTLS_MD_SHA384, secret, slen,
881 label, random, rlen, dstbuf, dlen);
Paul Bakkerca4ab492012-04-18 14:23:57 +0000882}
Gilles Peskined2d59372021-05-12 22:43:27 +0200883#endif /* MBEDTLS_SHA512_C && !MBEDTLS_SHA512_NO_SHA384 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200884#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakkerca4ab492012-04-18 14:23:57 +0000885
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100886static void ssl_update_checksum_start(mbedtls_ssl_context *, const unsigned char *, size_t);
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200887
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200888#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1) || \
889 defined(MBEDTLS_SSL_PROTO_TLS1_1)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100890static void ssl_update_checksum_md5sha1(mbedtls_ssl_context *, const unsigned char *, size_t);
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200891#endif
Paul Bakker380da532012-04-18 16:10:25 +0000892
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200893#if defined(MBEDTLS_SSL_PROTO_SSL3)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100894static void ssl_calc_verify_ssl(const mbedtls_ssl_context *, unsigned char *, size_t *);
David Horstmann68014b22025-03-10 14:10:11 +0000895static int ssl_calc_finished_ssl(mbedtls_ssl_context *, unsigned char *, int);
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200896#endif
897
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200898#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100899static void ssl_calc_verify_tls(const mbedtls_ssl_context *, unsigned char *, size_t *);
David Horstmann68014b22025-03-10 14:10:11 +0000900static int ssl_calc_finished_tls(mbedtls_ssl_context *, unsigned char *, int);
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200901#endif
902
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200903#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
904#if defined(MBEDTLS_SHA256_C)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100905static void ssl_update_checksum_sha256(mbedtls_ssl_context *, const unsigned char *, size_t);
906static void ssl_calc_verify_tls_sha256(const mbedtls_ssl_context *, unsigned char *, size_t *);
David Horstmann68014b22025-03-10 14:10:11 +0000907static int ssl_calc_finished_tls_sha256(mbedtls_ssl_context *, unsigned char *, int);
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200908#endif
Paul Bakker769075d2012-11-24 11:26:46 +0100909
Gilles Peskined2d59372021-05-12 22:43:27 +0200910#if defined(MBEDTLS_SHA512_C) && !defined(MBEDTLS_SHA512_NO_SHA384)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100911static void ssl_update_checksum_sha384(mbedtls_ssl_context *, const unsigned char *, size_t);
912static void ssl_calc_verify_tls_sha384(const mbedtls_ssl_context *, unsigned char *, size_t *);
David Horstmann68014b22025-03-10 14:10:11 +0000913static int ssl_calc_finished_tls_sha384(mbedtls_ssl_context *, unsigned char *, int);
Paul Bakker769075d2012-11-24 11:26:46 +0100914#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200915#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakker1ef83d62012-04-11 12:09:53 +0000916
Manuel Pégourié-Gonnard45be3d82019-02-18 23:35:14 +0100917#if defined(MBEDTLS_KEY_EXCHANGE_PSK_ENABLED) && \
Hanno Becker7d0a5692018-10-23 15:26:22 +0100918 defined(MBEDTLS_USE_PSA_CRYPTO)
Manuel Pégourié-Gonnardd904d662022-06-17 10:24:00 +0200919MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100920static int ssl_use_opaque_psk(mbedtls_ssl_context const *ssl)
Hanno Becker7d0a5692018-10-23 15:26:22 +0100921{
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100922 if (ssl->conf->f_psk != NULL) {
Hanno Becker7d0a5692018-10-23 15:26:22 +0100923 /* If we've used a callback to select the PSK,
924 * the static configuration is irrelevant. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100925 if (!mbedtls_svc_key_id_is_null(ssl->handshake->psk_opaque)) {
926 return 1;
927 }
Hanno Becker7d0a5692018-10-23 15:26:22 +0100928
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100929 return 0;
Hanno Becker7d0a5692018-10-23 15:26:22 +0100930 }
931
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100932 if (!mbedtls_svc_key_id_is_null(ssl->conf->psk_opaque)) {
933 return 1;
934 }
Hanno Becker7d0a5692018-10-23 15:26:22 +0100935
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100936 return 0;
Hanno Becker7d0a5692018-10-23 15:26:22 +0100937}
938#endif /* MBEDTLS_USE_PSA_CRYPTO &&
Manuel Pégourié-Gonnard45be3d82019-02-18 23:35:14 +0100939 MBEDTLS_KEY_EXCHANGE_PSK_ENABLED */
Hanno Becker7d0a5692018-10-23 15:26:22 +0100940
Ron Eldorcf280092019-05-14 20:19:13 +0300941#if defined(MBEDTLS_SSL_EXPORT_KEYS)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100942static mbedtls_tls_prf_types tls_prf_get_type(mbedtls_ssl_tls_prf_cb *tls_prf)
Ron Eldorcf280092019-05-14 20:19:13 +0300943{
944#if defined(MBEDTLS_SSL_PROTO_SSL3)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100945 if (tls_prf == ssl3_prf) {
946 return MBEDTLS_SSL_TLS_PRF_SSL3;
947 } else
Ron Eldorcf280092019-05-14 20:19:13 +0300948#endif
949#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100950 if (tls_prf == tls1_prf) {
951 return MBEDTLS_SSL_TLS_PRF_TLS1;
952 } else
Ron Eldorcf280092019-05-14 20:19:13 +0300953#endif
954#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
Gilles Peskined2d59372021-05-12 22:43:27 +0200955#if defined(MBEDTLS_SHA512_C) && !defined(MBEDTLS_SHA512_NO_SHA384)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100956 if (tls_prf == tls_prf_sha384) {
957 return MBEDTLS_SSL_TLS_PRF_SHA384;
958 } else
Ron Eldorcf280092019-05-14 20:19:13 +0300959#endif
960#if defined(MBEDTLS_SHA256_C)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100961 if (tls_prf == tls_prf_sha256) {
962 return MBEDTLS_SSL_TLS_PRF_SHA256;
963 } else
Ron Eldorcf280092019-05-14 20:19:13 +0300964#endif
965#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100966 return MBEDTLS_SSL_TLS_PRF_NONE;
Ron Eldorcf280092019-05-14 20:19:13 +0300967}
968#endif /* MBEDTLS_SSL_EXPORT_KEYS */
969
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100970int mbedtls_ssl_tls_prf(const mbedtls_tls_prf_types prf,
971 const unsigned char *secret, size_t slen,
972 const char *label,
973 const unsigned char *random, size_t rlen,
974 unsigned char *dstbuf, size_t dlen)
Ron Eldor51d3ab52019-05-12 14:54:30 +0300975{
976 mbedtls_ssl_tls_prf_cb *tls_prf = NULL;
977
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100978 switch (prf) {
Ron Eldor51d3ab52019-05-12 14:54:30 +0300979#if defined(MBEDTLS_SSL_PROTO_SSL3)
980 case MBEDTLS_SSL_TLS_PRF_SSL3:
981 tls_prf = ssl3_prf;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100982 break;
Ron Eldord2f25f72019-05-15 14:54:22 +0300983#endif /* MBEDTLS_SSL_PROTO_SSL3 */
Ron Eldor51d3ab52019-05-12 14:54:30 +0300984#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1)
985 case MBEDTLS_SSL_TLS_PRF_TLS1:
986 tls_prf = tls1_prf;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100987 break;
Ron Eldord2f25f72019-05-15 14:54:22 +0300988#endif /* MBEDTLS_SSL_PROTO_TLS1 || MBEDTLS_SSL_PROTO_TLS1_1 */
989
990#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
Gilles Peskined2d59372021-05-12 22:43:27 +0200991#if defined(MBEDTLS_SHA512_C) && !defined(MBEDTLS_SHA512_NO_SHA384)
Ron Eldor51d3ab52019-05-12 14:54:30 +0300992 case MBEDTLS_SSL_TLS_PRF_SHA384:
993 tls_prf = tls_prf_sha384;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100994 break;
Gilles Peskined2d59372021-05-12 22:43:27 +0200995#endif /* MBEDTLS_SHA512_C && !MBEDTLS_SHA512_NO_SHA384 */
Ron Eldor51d3ab52019-05-12 14:54:30 +0300996#if defined(MBEDTLS_SHA256_C)
997 case MBEDTLS_SSL_TLS_PRF_SHA256:
998 tls_prf = tls_prf_sha256;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100999 break;
Ron Eldord2f25f72019-05-15 14:54:22 +03001000#endif /* MBEDTLS_SHA256_C */
1001#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001002 default:
1003 return MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
Ron Eldor51d3ab52019-05-12 14:54:30 +03001004 }
1005
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001006 return tls_prf(secret, slen, label, random, rlen, dstbuf, dlen);
Ron Eldor51d3ab52019-05-12 14:54:30 +03001007}
1008
Manuel Pégourié-Gonnard9b108c22019-05-06 13:32:17 +02001009/* Type for the TLS PRF */
1010typedef int ssl_tls_prf_t(const unsigned char *, size_t, const char *,
1011 const unsigned char *, size_t,
1012 unsigned char *, size_t);
1013
Manuel Pégourié-Gonnarde59ae232019-04-30 11:41:40 +02001014/*
Manuel Pégourié-Gonnardcba40d92019-05-06 12:55:40 +02001015 * Populate a transform structure with session keys and all the other
1016 * necessary information.
Manuel Pégourié-Gonnarde59ae232019-04-30 11:41:40 +02001017 *
Manuel Pégourié-Gonnardcba40d92019-05-06 12:55:40 +02001018 * Parameters:
1019 * - [in/out]: transform: structure to populate
1020 * [in] must be just initialised with mbedtls_ssl_transform_init()
Manuel Pégourié-Gonnardc864f6a2019-05-06 13:48:22 +02001021 * [out] fully populated, ready for use by mbedtls_ssl_{en,de}crypt_buf()
Manuel Pégourié-Gonnard6fa57bf2019-05-10 10:50:04 +02001022 * - [in] ciphersuite
1023 * - [in] master
1024 * - [in] encrypt_then_mac
1025 * - [in] trunc_hmac
1026 * - [in] compression
Manuel Pégourié-Gonnard9b108c22019-05-06 13:32:17 +02001027 * - [in] tls_prf: pointer to PRF to use for key derivation
1028 * - [in] randbytes: buffer holding ServerHello.random + ClientHello.random
Manuel Pégourié-Gonnardc864f6a2019-05-06 13:48:22 +02001029 * - [in] minor_ver: SSL/TLS minor version
1030 * - [in] endpoint: client or server
1031 * - [in] ssl: optionally used for:
Manuel Pégourié-Gonnarde07bc202020-02-26 09:53:42 +01001032 * - MBEDTLS_SSL_HW_RECORD_ACCEL: whole context (non-const)
Manuel Pégourié-Gonnardc864f6a2019-05-06 13:48:22 +02001033 * - MBEDTLS_SSL_EXPORT_KEYS: ssl->conf->{f,p}_export_keys
1034 * - MBEDTLS_DEBUG_C: ssl->conf->{f,p}_dbg
Manuel Pégourié-Gonnarde59ae232019-04-30 11:41:40 +02001035 */
Manuel Pégourié-Gonnardd904d662022-06-17 10:24:00 +02001036MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001037static int ssl_populate_transform(mbedtls_ssl_transform *transform,
1038 int ciphersuite,
1039 const unsigned char master[48],
Jarno Lamsac84bd242019-08-16 12:06:56 +03001040#if defined(MBEDTLS_SSL_SOME_MODES_USE_MAC)
Manuel Pégourié-Gonnard6fa57bf2019-05-10 10:50:04 +02001041#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001042 int encrypt_then_mac,
Jarno Lamsac84bd242019-08-16 12:06:56 +03001043#endif /* MBEDTLS_SSL_ENCRYPT_THEN_MAC */
Manuel Pégourié-Gonnard6fa57bf2019-05-10 10:50:04 +02001044#if defined(MBEDTLS_SSL_TRUNCATED_HMAC)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001045 int trunc_hmac,
Jarno Lamsac84bd242019-08-16 12:06:56 +03001046#endif /* MBEDTLS_SSL_TRUNCATED_HMAC */
1047#endif /* MBEDTLS_SSL_SOME_MODES_USE_MAC */
Manuel Pégourié-Gonnard6fa57bf2019-05-10 10:50:04 +02001048#if defined(MBEDTLS_ZLIB_SUPPORT)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001049 int compression,
Manuel Pégourié-Gonnard6fa57bf2019-05-10 10:50:04 +02001050#endif
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001051 ssl_tls_prf_t tls_prf,
1052 const unsigned char randbytes[64],
1053 int minor_ver,
1054 unsigned endpoint,
Manuel Pégourié-Gonnard7ae6ed42020-03-13 11:28:19 +01001055#if !defined(MBEDTLS_SSL_HW_RECORD_ACCEL)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001056 const
Manuel Pégourié-Gonnard7ae6ed42020-03-13 11:28:19 +01001057#endif
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001058 mbedtls_ssl_context *ssl)
Paul Bakker5121ce52009-01-03 21:22:43 +00001059{
Paul Bakkerda02a7f2013-08-31 17:25:14 +02001060 int ret = 0;
Hanno Beckercb1cc802018-11-17 22:27:38 +00001061#if defined(MBEDTLS_USE_PSA_CRYPTO)
1062 int psa_fallthrough;
1063#endif /* MBEDTLS_USE_PSA_CRYPTO */
David Horstmannd4f22082022-10-26 18:25:14 +01001064 int do_mbedtls_cipher_setup;
Paul Bakker5121ce52009-01-03 21:22:43 +00001065 unsigned char keyblk[256];
1066 unsigned char *key1;
1067 unsigned char *key2;
Paul Bakker68884e32013-01-07 18:20:04 +01001068 unsigned char *mac_enc;
1069 unsigned char *mac_dec;
sander-visser3888b032020-05-06 21:49:46 +02001070 size_t mac_key_len = 0;
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02001071 size_t iv_copy_len;
Hanno Becker88aaf652017-12-27 08:17:40 +00001072 unsigned keylen;
Hanno Beckere694c3e2017-12-27 21:34:08 +00001073 const mbedtls_ssl_ciphersuite_t *ciphersuite_info;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001074 const mbedtls_cipher_info_t *cipher_info;
1075 const mbedtls_md_info_t *md_info;
Paul Bakker68884e32013-01-07 18:20:04 +01001076
Manuel Pégourié-Gonnardc864f6a2019-05-06 13:48:22 +02001077#if !defined(MBEDTLS_SSL_HW_RECORD_ACCEL) && \
1078 !defined(MBEDTLS_SSL_EXPORT_KEYS) && \
Gilles Peskinea6f99a12022-04-13 13:24:56 +02001079 !defined(MBEDTLS_SSL_DTLS_CONNECTION_ID) && \
Manuel Pégourié-Gonnardc864f6a2019-05-06 13:48:22 +02001080 !defined(MBEDTLS_DEBUG_C)
Paul Elliott9c2faca2023-10-18 14:34:46 +01001081 (void) ssl; /* ssl is unused except for those cases */
Manuel Pégourié-Gonnardc864f6a2019-05-06 13:48:22 +02001082#endif
1083
Manuel Pégourié-Gonnard96fb0ee2019-07-09 12:54:17 +02001084 /*
1085 * Some data just needs copying into the structure
1086 */
Jaeden Amero2de07f12019-06-05 13:32:08 +01001087#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC) && \
1088 defined(MBEDTLS_SSL_SOME_MODES_USE_MAC)
Manuel Pégourié-Gonnard6fa57bf2019-05-10 10:50:04 +02001089 transform->encrypt_then_mac = encrypt_then_mac;
Hanno Becker9eddaeb2017-12-27 21:37:21 +00001090#endif
Manuel Pégourié-Gonnardc864f6a2019-05-06 13:48:22 +02001091 transform->minor_ver = minor_ver;
Hanno Beckere694c3e2017-12-27 21:34:08 +00001092
Manuel Pégourié-Gonnard96fb0ee2019-07-09 12:54:17 +02001093#if defined(MBEDTLS_SSL_CONTEXT_SERIALIZATION)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001094 memcpy(transform->randbytes, randbytes, sizeof(transform->randbytes));
Manuel Pégourié-Gonnard96fb0ee2019-07-09 12:54:17 +02001095#endif
1096
Manuel Pégourié-Gonnard9b108c22019-05-06 13:32:17 +02001097 /*
1098 * Get various info structures
1099 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001100 ciphersuite_info = mbedtls_ssl_ciphersuite_from_id(ciphersuite);
1101 if (ciphersuite_info == NULL) {
1102 MBEDTLS_SSL_DEBUG_MSG(1, ("ciphersuite info for %d not found",
1103 ciphersuite));
1104 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
Manuel Pégourié-Gonnard9b108c22019-05-06 13:32:17 +02001105 }
1106
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001107 cipher_info = mbedtls_cipher_info_from_type(ciphersuite_info->cipher);
1108 if (cipher_info == NULL) {
1109 MBEDTLS_SSL_DEBUG_MSG(1, ("cipher info for %u not found",
1110 ciphersuite_info->cipher));
1111 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
Paul Bakker68884e32013-01-07 18:20:04 +01001112 }
1113
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001114 md_info = mbedtls_md_info_from_type(ciphersuite_info->mac);
1115 if (md_info == NULL) {
1116 MBEDTLS_SSL_DEBUG_MSG(1, ("mbedtls_md info for %u not found",
1117 (unsigned) ciphersuite_info->mac));
1118 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
Paul Bakker68884e32013-01-07 18:20:04 +01001119 }
1120
Hanno Beckera0e20d02019-05-15 14:03:01 +01001121#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Becker4bf74652019-04-26 16:22:27 +01001122 /* Copy own and peer's CID if the use of the CID
1123 * extension has been negotiated. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001124 if (ssl->handshake->cid_in_use == MBEDTLS_SSL_CID_ENABLED) {
1125 MBEDTLS_SSL_DEBUG_MSG(3, ("Copy CIDs into SSL transform"));
Hanno Becker8a7f9722019-04-30 13:52:29 +01001126
Hanno Becker05154c32019-05-03 15:23:51 +01001127 transform->in_cid_len = ssl->own_cid_len;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001128 memcpy(transform->in_cid, ssl->own_cid, ssl->own_cid_len);
1129 MBEDTLS_SSL_DEBUG_BUF(3, "Incoming CID", transform->in_cid,
1130 transform->in_cid_len);
Hanno Beckerd1f20352019-05-15 10:21:55 +01001131
1132 transform->out_cid_len = ssl->handshake->peer_cid_len;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001133 memcpy(transform->out_cid, ssl->handshake->peer_cid,
1134 ssl->handshake->peer_cid_len);
1135 MBEDTLS_SSL_DEBUG_BUF(3, "Outgoing CID", transform->out_cid,
1136 transform->out_cid_len);
Hanno Becker4bf74652019-04-26 16:22:27 +01001137 }
Hanno Beckera0e20d02019-05-15 14:03:01 +01001138#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Becker4bf74652019-04-26 16:22:27 +01001139
Paul Bakker5121ce52009-01-03 21:22:43 +00001140 /*
Manuel Pégourié-Gonnard9b108c22019-05-06 13:32:17 +02001141 * Compute key block using the PRF
Paul Bakker5121ce52009-01-03 21:22:43 +00001142 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001143 ret = tls_prf(master, 48, "key expansion", randbytes, 64, keyblk, 256);
1144 if (ret != 0) {
1145 MBEDTLS_SSL_DEBUG_RET(1, "prf", ret);
1146 return ret;
Manuel Pégourié-Gonnarde9608182015-03-26 11:47:47 +01001147 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001148
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001149 MBEDTLS_SSL_DEBUG_MSG(3, ("ciphersuite = %s",
1150 mbedtls_ssl_get_ciphersuite_name(ciphersuite)));
1151 MBEDTLS_SSL_DEBUG_BUF(3, "master secret", master, 48);
1152 MBEDTLS_SSL_DEBUG_BUF(4, "random bytes", randbytes, 64);
1153 MBEDTLS_SSL_DEBUG_BUF(4, "key block", keyblk, 256);
Paul Bakker5121ce52009-01-03 21:22:43 +00001154
Paul Bakker5121ce52009-01-03 21:22:43 +00001155 /*
1156 * Determine the appropriate key, IV and MAC length.
1157 */
Paul Bakker68884e32013-01-07 18:20:04 +01001158
Hanno Becker88aaf652017-12-27 08:17:40 +00001159 keylen = cipher_info->key_bitlen / 8;
Manuel Pégourié-Gonnarde800cd82014-06-18 15:34:40 +02001160
Hanno Becker8031d062018-01-03 15:32:31 +00001161#if defined(MBEDTLS_GCM_C) || \
1162 defined(MBEDTLS_CCM_C) || \
1163 defined(MBEDTLS_CHACHAPOLY_C)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001164 if (cipher_info->mode == MBEDTLS_MODE_GCM ||
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02001165 cipher_info->mode == MBEDTLS_MODE_CCM ||
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001166 cipher_info->mode == MBEDTLS_MODE_CHACHAPOLY) {
Hanno Beckerf704bef2018-11-16 15:21:18 +00001167 size_t explicit_ivlen;
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02001168
Manuel Pégourié-Gonnarde800cd82014-06-18 15:34:40 +02001169 transform->maclen = 0;
Hanno Becker81c7b182017-11-09 18:39:33 +00001170 mac_key_len = 0;
Hanno Beckere694c3e2017-12-27 21:34:08 +00001171 transform->taglen =
1172 ciphersuite_info->flags & MBEDTLS_CIPHERSUITE_SHORT_TAG ? 8 : 16;
Manuel Pégourié-Gonnarde800cd82014-06-18 15:34:40 +02001173
Hanno Becker447558d2020-05-28 07:36:33 +01001174 /* All modes haves 96-bit IVs, but the length of the static parts vary
1175 * with mode and version:
1176 * - For GCM and CCM in TLS 1.2, there's a static IV of 4 Bytes
1177 * (to be concatenated with a dynamically chosen IV of 8 Bytes)
Hanno Beckerf93c2d72020-05-28 07:39:43 +01001178 * - For ChaChaPoly in TLS 1.2, and all modes in TLS 1.3, there's
1179 * a static IV of 12 Bytes (to be XOR'ed with the 8 Byte record
1180 * sequence number).
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02001181 */
Paul Bakker68884e32013-01-07 18:20:04 +01001182 transform->ivlen = 12;
Hanno Beckerf93c2d72020-05-28 07:39:43 +01001183#if defined(MBEDTLS_SSL_PROTO_TLS1_3_EXPERIMENTAL)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001184 if (minor_ver == MBEDTLS_SSL_MINOR_VERSION_4) {
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02001185 transform->fixed_ivlen = 12;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001186 } else
Hanno Beckerf93c2d72020-05-28 07:39:43 +01001187#endif /* MBEDTLS_SSL_PROTO_TLS1_3_EXPERIMENTAL */
1188 {
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001189 if (cipher_info->mode == MBEDTLS_MODE_CHACHAPOLY) {
Hanno Beckerf93c2d72020-05-28 07:39:43 +01001190 transform->fixed_ivlen = 12;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001191 } else {
Hanno Beckerf93c2d72020-05-28 07:39:43 +01001192 transform->fixed_ivlen = 4;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001193 }
Hanno Beckerf93c2d72020-05-28 07:39:43 +01001194 }
Manuel Pégourié-Gonnarde800cd82014-06-18 15:34:40 +02001195
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02001196 /* Minimum length of encrypted record */
1197 explicit_ivlen = transform->ivlen - transform->fixed_ivlen;
Hanno Beckere694c3e2017-12-27 21:34:08 +00001198 transform->minlen = explicit_ivlen + transform->taglen;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001199 } else
Hanno Becker8031d062018-01-03 15:32:31 +00001200#endif /* MBEDTLS_GCM_C || MBEDTLS_CCM_C || MBEDTLS_CHACHAPOLY_C */
1201#if defined(MBEDTLS_SSL_SOME_MODES_USE_MAC)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001202 if (cipher_info->mode == MBEDTLS_MODE_STREAM ||
1203 cipher_info->mode == MBEDTLS_MODE_CBC) {
Manuel Pégourié-Gonnarde800cd82014-06-18 15:34:40 +02001204 /* Initialize HMAC contexts */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001205 if ((ret = mbedtls_md_setup(&transform->md_ctx_enc, md_info, 1)) != 0 ||
1206 (ret = mbedtls_md_setup(&transform->md_ctx_dec, md_info, 1)) != 0) {
1207 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_md_setup", ret);
Ron Eldore6992702019-05-07 18:27:13 +03001208 goto end;
Paul Bakker68884e32013-01-07 18:20:04 +01001209 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001210
Manuel Pégourié-Gonnarde800cd82014-06-18 15:34:40 +02001211 /* Get MAC length */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001212 mac_key_len = mbedtls_md_get_size(md_info);
Hanno Becker81c7b182017-11-09 18:39:33 +00001213 transform->maclen = mac_key_len;
Manuel Pégourié-Gonnarde800cd82014-06-18 15:34:40 +02001214
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001215#if defined(MBEDTLS_SSL_TRUNCATED_HMAC)
Manuel Pégourié-Gonnarde800cd82014-06-18 15:34:40 +02001216 /*
1217 * If HMAC is to be truncated, we shall keep the leftmost bytes,
1218 * (rfc 6066 page 13 or rfc 2104 section 4),
1219 * so we only need to adjust the length here.
1220 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001221 if (trunc_hmac == MBEDTLS_SSL_TRUNC_HMAC_ENABLED) {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001222 transform->maclen = MBEDTLS_SSL_TRUNCATED_HMAC_LEN;
Hanno Beckere89353a2017-11-20 16:36:41 +00001223
1224#if defined(MBEDTLS_SSL_TRUNCATED_HMAC_COMPAT)
1225 /* Fall back to old, non-compliant version of the truncated
Hanno Becker563423f2017-11-21 17:20:17 +00001226 * HMAC implementation which also truncates the key
1227 * (Mbed TLS versions from 1.3 to 2.6.0) */
Hanno Beckere89353a2017-11-20 16:36:41 +00001228 mac_key_len = transform->maclen;
1229#endif
1230 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001231#endif /* MBEDTLS_SSL_TRUNCATED_HMAC */
Manuel Pégourié-Gonnarde800cd82014-06-18 15:34:40 +02001232
1233 /* IV length */
Paul Bakker68884e32013-01-07 18:20:04 +01001234 transform->ivlen = cipher_info->iv_size;
Paul Bakker5121ce52009-01-03 21:22:43 +00001235
Manuel Pégourié-Gonnardeaa76f72014-06-18 16:06:02 +02001236 /* Minimum length */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001237 if (cipher_info->mode == MBEDTLS_MODE_STREAM) {
Manuel Pégourié-Gonnardeaa76f72014-06-18 16:06:02 +02001238 transform->minlen = transform->maclen;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001239 } else {
Manuel Pégourié-Gonnardeaa76f72014-06-18 16:06:02 +02001240 /*
1241 * GenericBlockCipher:
Manuel Pégourié-Gonnard169dd6a2014-11-04 16:15:39 +01001242 * 1. if EtM is in use: one block plus MAC
1243 * otherwise: * first multiple of blocklen greater than maclen
1244 * 2. IV except for SSL3 and TLS 1.0
Manuel Pégourié-Gonnardeaa76f72014-06-18 16:06:02 +02001245 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001246#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001247 if (encrypt_then_mac == MBEDTLS_SSL_ETM_ENABLED) {
Manuel Pégourié-Gonnard169dd6a2014-11-04 16:15:39 +01001248 transform->minlen = transform->maclen
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001249 + cipher_info->block_size;
1250 } else
Manuel Pégourié-Gonnard169dd6a2014-11-04 16:15:39 +01001251#endif
1252 {
1253 transform->minlen = transform->maclen
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001254 + cipher_info->block_size
1255 - transform->maclen % cipher_info->block_size;
Manuel Pégourié-Gonnard169dd6a2014-11-04 16:15:39 +01001256 }
Manuel Pégourié-Gonnardeaa76f72014-06-18 16:06:02 +02001257
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001258#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001259 if (minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 ||
1260 minor_ver == MBEDTLS_SSL_MINOR_VERSION_1) {
Manuel Pégourié-Gonnardeaa76f72014-06-18 16:06:02 +02001261 ; /* No need to adjust minlen */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001262 } else
Manuel Pégourié-Gonnardeaa76f72014-06-18 16:06:02 +02001263#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001264#if defined(MBEDTLS_SSL_PROTO_TLS1_1) || defined(MBEDTLS_SSL_PROTO_TLS1_2)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001265 if (minor_ver == MBEDTLS_SSL_MINOR_VERSION_2 ||
1266 minor_ver == MBEDTLS_SSL_MINOR_VERSION_3) {
Manuel Pégourié-Gonnardeaa76f72014-06-18 16:06:02 +02001267 transform->minlen += transform->ivlen;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001268 } else
Manuel Pégourié-Gonnardeaa76f72014-06-18 16:06:02 +02001269#endif
1270 {
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001271 MBEDTLS_SSL_DEBUG_MSG(1, ("should never happen"));
Ron Eldore6992702019-05-07 18:27:13 +03001272 ret = MBEDTLS_ERR_SSL_INTERNAL_ERROR;
1273 goto end;
Manuel Pégourié-Gonnardeaa76f72014-06-18 16:06:02 +02001274 }
Paul Bakker68884e32013-01-07 18:20:04 +01001275 }
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001276 } else
Hanno Becker8031d062018-01-03 15:32:31 +00001277#endif /* MBEDTLS_SSL_SOME_MODES_USE_MAC */
1278 {
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001279 MBEDTLS_SSL_DEBUG_MSG(1, ("should never happen"));
1280 return MBEDTLS_ERR_SSL_INTERNAL_ERROR;
Hanno Becker8031d062018-01-03 15:32:31 +00001281 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001282
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001283 MBEDTLS_SSL_DEBUG_MSG(3, ("keylen: %u, minlen: %u, ivlen: %u, maclen: %u",
1284 (unsigned) keylen,
1285 (unsigned) transform->minlen,
1286 (unsigned) transform->ivlen,
1287 (unsigned) transform->maclen));
Paul Bakker5121ce52009-01-03 21:22:43 +00001288
1289 /*
1290 * Finally setup the cipher contexts, IVs and MAC secrets.
1291 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001292#if defined(MBEDTLS_SSL_CLI_C)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001293 if (endpoint == MBEDTLS_SSL_IS_CLIENT) {
Hanno Becker81c7b182017-11-09 18:39:33 +00001294 key1 = keyblk + mac_key_len * 2;
Hanno Becker88aaf652017-12-27 08:17:40 +00001295 key2 = keyblk + mac_key_len * 2 + keylen;
Paul Bakker5121ce52009-01-03 21:22:43 +00001296
Paul Bakker68884e32013-01-07 18:20:04 +01001297 mac_enc = keyblk;
Hanno Becker81c7b182017-11-09 18:39:33 +00001298 mac_dec = keyblk + mac_key_len;
Paul Bakker5121ce52009-01-03 21:22:43 +00001299
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001300 /*
1301 * This is not used in TLS v1.1.
1302 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001303 iv_copy_len = (transform->fixed_ivlen) ?
1304 transform->fixed_ivlen : transform->ivlen;
1305 memcpy(transform->iv_enc, key2 + keylen, iv_copy_len);
1306 memcpy(transform->iv_dec, key2 + keylen + iv_copy_len,
1307 iv_copy_len);
1308 } else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001309#endif /* MBEDTLS_SSL_CLI_C */
1310#if defined(MBEDTLS_SSL_SRV_C)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001311 if (endpoint == MBEDTLS_SSL_IS_SERVER) {
Hanno Becker88aaf652017-12-27 08:17:40 +00001312 key1 = keyblk + mac_key_len * 2 + keylen;
Hanno Becker81c7b182017-11-09 18:39:33 +00001313 key2 = keyblk + mac_key_len * 2;
Paul Bakker5121ce52009-01-03 21:22:43 +00001314
Hanno Becker81c7b182017-11-09 18:39:33 +00001315 mac_enc = keyblk + mac_key_len;
Paul Bakker68884e32013-01-07 18:20:04 +01001316 mac_dec = keyblk;
Paul Bakker5121ce52009-01-03 21:22:43 +00001317
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001318 /*
1319 * This is not used in TLS v1.1.
1320 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001321 iv_copy_len = (transform->fixed_ivlen) ?
1322 transform->fixed_ivlen : transform->ivlen;
1323 memcpy(transform->iv_dec, key1 + keylen, iv_copy_len);
1324 memcpy(transform->iv_enc, key1 + keylen + iv_copy_len,
1325 iv_copy_len);
1326 } else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001327#endif /* MBEDTLS_SSL_SRV_C */
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01001328 {
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001329 MBEDTLS_SSL_DEBUG_MSG(1, ("should never happen"));
Ron Eldore6992702019-05-07 18:27:13 +03001330 ret = MBEDTLS_ERR_SSL_INTERNAL_ERROR;
1331 goto end;
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01001332 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001333
Hanno Beckerd56ed242018-01-03 15:32:51 +00001334#if defined(MBEDTLS_SSL_SOME_MODES_USE_MAC)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001335#if defined(MBEDTLS_SSL_PROTO_SSL3)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001336 if (minor_ver == MBEDTLS_SSL_MINOR_VERSION_0) {
1337 if (mac_key_len > sizeof(transform->mac_enc)) {
1338 MBEDTLS_SSL_DEBUG_MSG(1, ("should never happen"));
Ron Eldore6992702019-05-07 18:27:13 +03001339 ret = MBEDTLS_ERR_SSL_INTERNAL_ERROR;
1340 goto end;
Manuel Pégourié-Gonnard7cfdcb82014-01-18 18:22:55 +01001341 }
1342
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001343 memcpy(transform->mac_enc, mac_enc, mac_key_len);
1344 memcpy(transform->mac_dec, mac_dec, mac_key_len);
1345 } else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001346#endif /* MBEDTLS_SSL_PROTO_SSL3 */
1347#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1) || \
1348 defined(MBEDTLS_SSL_PROTO_TLS1_2)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001349 if (minor_ver >= MBEDTLS_SSL_MINOR_VERSION_1) {
Gilles Peskine039fd122018-03-19 19:06:08 +01001350 /* For HMAC-based ciphersuites, initialize the HMAC transforms.
1351 For AEAD-based ciphersuites, there is nothing to do here. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001352 if (mac_key_len != 0) {
1353 ret = mbedtls_md_hmac_starts(&transform->md_ctx_enc,
1354 mac_enc, mac_key_len);
1355 if (ret != 0) {
Gilles Peskine2b3f21d2021-12-10 21:35:10 +01001356 goto end;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001357 }
1358 ret = mbedtls_md_hmac_starts(&transform->md_ctx_dec,
1359 mac_dec, mac_key_len);
1360 if (ret != 0) {
Gilles Peskine2b3f21d2021-12-10 21:35:10 +01001361 goto end;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001362 }
Gilles Peskine039fd122018-03-19 19:06:08 +01001363 }
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001364 } else
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001365#endif
Paul Bakker577e0062013-08-28 11:57:20 +02001366 {
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001367 MBEDTLS_SSL_DEBUG_MSG(1, ("should never happen"));
Ron Eldore6992702019-05-07 18:27:13 +03001368 ret = MBEDTLS_ERR_SSL_INTERNAL_ERROR;
1369 goto end;
Paul Bakker577e0062013-08-28 11:57:20 +02001370 }
Hanno Beckerd56ed242018-01-03 15:32:51 +00001371#endif /* MBEDTLS_SSL_SOME_MODES_USE_MAC */
Paul Bakker68884e32013-01-07 18:20:04 +01001372
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001373#if defined(MBEDTLS_SSL_HW_RECORD_ACCEL)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001374 if (mbedtls_ssl_hw_record_init != NULL) {
sander-visser1abe8ee2020-05-06 21:27:14 +02001375 ret = 0;
Paul Bakker05ef8352012-05-08 09:17:57 +00001376
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001377 MBEDTLS_SSL_DEBUG_MSG(2, ("going for mbedtls_ssl_hw_record_init()"));
Paul Bakker05ef8352012-05-08 09:17:57 +00001378
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001379 if ((ret = mbedtls_ssl_hw_record_init(ssl, key1, key2, keylen,
1380 transform->iv_enc, transform->iv_dec,
1381 iv_copy_len,
1382 mac_enc, mac_dec,
1383 mac_key_len)) != 0) {
1384 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ssl_hw_record_init", ret);
Ron Eldore6992702019-05-07 18:27:13 +03001385 ret = MBEDTLS_ERR_SSL_HW_ACCEL_FAILED;
1386 goto end;
Paul Bakker05ef8352012-05-08 09:17:57 +00001387 }
1388 }
Hanno Beckerd56ed242018-01-03 15:32:51 +00001389#else
1390 ((void) mac_dec);
1391 ((void) mac_enc);
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001392#endif /* MBEDTLS_SSL_HW_RECORD_ACCEL */
Paul Bakker05ef8352012-05-08 09:17:57 +00001393
Manuel Pégourié-Gonnard024b6df2015-10-19 13:52:53 +02001394#if defined(MBEDTLS_SSL_EXPORT_KEYS)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001395 if (ssl->conf->f_export_keys != NULL) {
1396 ssl->conf->f_export_keys(ssl->conf->p_export_keys,
1397 master, keyblk,
1398 mac_key_len, keylen,
1399 iv_copy_len);
Robert Cragie4feb7ae2015-10-02 13:33:37 +01001400 }
Ron Eldorf5cc10d2019-05-07 18:33:40 +03001401
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001402 if (ssl->conf->f_export_keys_ext != NULL) {
1403 ssl->conf->f_export_keys_ext(ssl->conf->p_export_keys,
1404 master, keyblk,
1405 mac_key_len, keylen,
1406 iv_copy_len,
1407 randbytes + 32,
1408 randbytes,
1409 tls_prf_get_type(tls_prf));
Ron Eldorf5cc10d2019-05-07 18:33:40 +03001410 }
Robert Cragie4feb7ae2015-10-02 13:33:37 +01001411#endif
1412
David Horstmannd4f22082022-10-26 18:25:14 +01001413 do_mbedtls_cipher_setup = 1;
Hanno Beckerf704bef2018-11-16 15:21:18 +00001414#if defined(MBEDTLS_USE_PSA_CRYPTO)
Hanno Beckercb1cc802018-11-17 22:27:38 +00001415
1416 /* Only use PSA-based ciphers for TLS-1.2.
1417 * That's relevant at least for TLS-1.0, where
1418 * we assume that mbedtls_cipher_crypt() updates
1419 * the structure field for the IV, which the PSA-based
1420 * implementation currently doesn't. */
1421#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001422 if (minor_ver == MBEDTLS_SSL_MINOR_VERSION_3) {
1423 ret = mbedtls_cipher_setup_psa(&transform->cipher_ctx_enc,
1424 cipher_info, transform->taglen);
1425 if (ret != 0 && ret != MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE) {
1426 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_cipher_setup_psa", ret);
Ron Eldore6992702019-05-07 18:27:13 +03001427 goto end;
Hanno Beckercb1cc802018-11-17 22:27:38 +00001428 }
1429
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001430 if (ret == 0) {
1431 MBEDTLS_SSL_DEBUG_MSG(3, ("Successfully setup PSA-based encryption cipher context"));
Hanno Beckercb1cc802018-11-17 22:27:38 +00001432 psa_fallthrough = 0;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001433 } else {
1434 MBEDTLS_SSL_DEBUG_MSG(1,
1435 (
1436 "Failed to setup PSA-based cipher context for record encryption - fall through to default setup."));
Hanno Beckercb1cc802018-11-17 22:27:38 +00001437 psa_fallthrough = 1;
1438 }
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001439 } else {
Hanno Beckercb1cc802018-11-17 22:27:38 +00001440 psa_fallthrough = 1;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001441 }
Hanno Beckercb1cc802018-11-17 22:27:38 +00001442#else
1443 psa_fallthrough = 1;
1444#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Hanno Beckerf704bef2018-11-16 15:21:18 +00001445
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001446 if (psa_fallthrough == 0) {
David Horstmannd4f22082022-10-26 18:25:14 +01001447 do_mbedtls_cipher_setup = 0;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001448 }
Hanno Beckerf704bef2018-11-16 15:21:18 +00001449#endif /* MBEDTLS_USE_PSA_CRYPTO */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001450 if (do_mbedtls_cipher_setup &&
1451 (ret = mbedtls_cipher_setup(&transform->cipher_ctx_enc,
1452 cipher_info)) != 0) {
1453 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_cipher_setup", ret);
Ron Eldore6992702019-05-07 18:27:13 +03001454 goto end;
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001455 }
Paul Bakkerda02a7f2013-08-31 17:25:14 +02001456
David Horstmannd4f22082022-10-26 18:25:14 +01001457 do_mbedtls_cipher_setup = 1;
Hanno Beckerf704bef2018-11-16 15:21:18 +00001458#if defined(MBEDTLS_USE_PSA_CRYPTO)
Hanno Beckercb1cc802018-11-17 22:27:38 +00001459 /* Only use PSA-based ciphers for TLS-1.2.
1460 * That's relevant at least for TLS-1.0, where
1461 * we assume that mbedtls_cipher_crypt() updates
1462 * the structure field for the IV, which the PSA-based
1463 * implementation currently doesn't. */
1464#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001465 if (minor_ver == MBEDTLS_SSL_MINOR_VERSION_3) {
1466 ret = mbedtls_cipher_setup_psa(&transform->cipher_ctx_dec,
1467 cipher_info, transform->taglen);
1468 if (ret != 0 && ret != MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE) {
1469 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_cipher_setup_psa", ret);
Ron Eldore6992702019-05-07 18:27:13 +03001470 goto end;
Hanno Beckercb1cc802018-11-17 22:27:38 +00001471 }
1472
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001473 if (ret == 0) {
1474 MBEDTLS_SSL_DEBUG_MSG(3, ("Successfully setup PSA-based decryption cipher context"));
Hanno Beckercb1cc802018-11-17 22:27:38 +00001475 psa_fallthrough = 0;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001476 } else {
1477 MBEDTLS_SSL_DEBUG_MSG(1,
1478 (
1479 "Failed to setup PSA-based cipher context for record decryption - fall through to default setup."));
Hanno Beckercb1cc802018-11-17 22:27:38 +00001480 psa_fallthrough = 1;
1481 }
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001482 } else {
Hanno Beckercb1cc802018-11-17 22:27:38 +00001483 psa_fallthrough = 1;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001484 }
Hanno Beckercb1cc802018-11-17 22:27:38 +00001485#else
1486 psa_fallthrough = 1;
1487#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Hanno Beckerf704bef2018-11-16 15:21:18 +00001488
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001489 if (psa_fallthrough == 0) {
David Horstmannd4f22082022-10-26 18:25:14 +01001490 do_mbedtls_cipher_setup = 0;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001491 }
Hanno Beckerf704bef2018-11-16 15:21:18 +00001492#endif /* MBEDTLS_USE_PSA_CRYPTO */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001493 if (do_mbedtls_cipher_setup &&
1494 (ret = mbedtls_cipher_setup(&transform->cipher_ctx_dec,
1495 cipher_info)) != 0) {
1496 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_cipher_setup", ret);
Ron Eldore6992702019-05-07 18:27:13 +03001497 goto end;
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001498 }
Paul Bakkerda02a7f2013-08-31 17:25:14 +02001499
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001500 if ((ret = mbedtls_cipher_setkey(&transform->cipher_ctx_enc, key1,
1501 cipher_info->key_bitlen,
1502 MBEDTLS_ENCRYPT)) != 0) {
1503 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_cipher_setkey", ret);
Ron Eldore6992702019-05-07 18:27:13 +03001504 goto end;
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001505 }
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02001506
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001507 if ((ret = mbedtls_cipher_setkey(&transform->cipher_ctx_dec, key2,
1508 cipher_info->key_bitlen,
1509 MBEDTLS_DECRYPT)) != 0) {
1510 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_cipher_setkey", ret);
Ron Eldore6992702019-05-07 18:27:13 +03001511 goto end;
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001512 }
Paul Bakkerda02a7f2013-08-31 17:25:14 +02001513
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001514#if defined(MBEDTLS_CIPHER_MODE_CBC)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001515 if (cipher_info->mode == MBEDTLS_MODE_CBC) {
1516 if ((ret = mbedtls_cipher_set_padding_mode(&transform->cipher_ctx_enc,
1517 MBEDTLS_PADDING_NONE)) != 0) {
1518 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_cipher_set_padding_mode", ret);
Ron Eldore6992702019-05-07 18:27:13 +03001519 goto end;
Manuel Pégourié-Gonnard126a66f2013-10-25 18:33:32 +02001520 }
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001521
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001522 if ((ret = mbedtls_cipher_set_padding_mode(&transform->cipher_ctx_dec,
1523 MBEDTLS_PADDING_NONE)) != 0) {
1524 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_cipher_set_padding_mode", ret);
Ron Eldore6992702019-05-07 18:27:13 +03001525 goto end;
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001526 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001527 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001528#endif /* MBEDTLS_CIPHER_MODE_CBC */
Paul Bakker5121ce52009-01-03 21:22:43 +00001529
Paul Bakker5121ce52009-01-03 21:22:43 +00001530
Manuel Pégourié-Gonnardd73b47f2019-05-06 12:44:24 +02001531 /* Initialize Zlib contexts */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001532#if defined(MBEDTLS_ZLIB_SUPPORT)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001533 if (compression == MBEDTLS_SSL_COMPRESS_DEFLATE) {
1534 MBEDTLS_SSL_DEBUG_MSG(3, ("Initializing zlib states"));
Paul Bakker2770fbd2012-07-03 13:30:23 +00001535
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001536 memset(&transform->ctx_deflate, 0, sizeof(transform->ctx_deflate));
1537 memset(&transform->ctx_inflate, 0, sizeof(transform->ctx_inflate));
Paul Bakker2770fbd2012-07-03 13:30:23 +00001538
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001539 if (deflateInit(&transform->ctx_deflate,
1540 Z_DEFAULT_COMPRESSION) != Z_OK ||
1541 inflateInit(&transform->ctx_inflate) != Z_OK) {
1542 MBEDTLS_SSL_DEBUG_MSG(1, ("Failed to initialize compression"));
Ron Eldore6992702019-05-07 18:27:13 +03001543 ret = MBEDTLS_ERR_SSL_COMPRESSION_FAILED;
1544 goto end;
Paul Bakker2770fbd2012-07-03 13:30:23 +00001545 }
1546 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001547#endif /* MBEDTLS_ZLIB_SUPPORT */
Paul Bakker2770fbd2012-07-03 13:30:23 +00001548
Ron Eldore6992702019-05-07 18:27:13 +03001549end:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001550 mbedtls_platform_zeroize(keyblk, sizeof(keyblk));
1551 return ret;
Paul Bakker5121ce52009-01-03 21:22:43 +00001552}
1553
Manuel Pégourié-Gonnard8d2805c2019-04-30 12:08:59 +02001554/*
Manuel Pégourié-Gonnard47e33e12019-05-20 10:10:17 +02001555 * Set appropriate PRF function and other SSL / TLS 1.0/1.1 / TLS1.2 functions
Manuel Pégourié-Gonnard8d2805c2019-04-30 12:08:59 +02001556 *
1557 * Inputs:
1558 * - SSL/TLS minor version
1559 * - hash associated with the ciphersuite (only used by TLS 1.2)
1560 *
Manuel Pégourié-Gonnard31d3ef12019-05-10 10:25:00 +02001561 * Outputs:
Manuel Pégourié-Gonnard8d2805c2019-04-30 12:08:59 +02001562 * - the tls_prf, calc_verify and calc_finished members of handshake structure
1563 */
Manuel Pégourié-Gonnardd904d662022-06-17 10:24:00 +02001564MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001565static int ssl_set_handshake_prfs(mbedtls_ssl_handshake_params *handshake,
1566 int minor_ver,
1567 mbedtls_md_type_t hash)
Manuel Pégourié-Gonnard1b00c4f2019-04-30 11:54:22 +02001568{
Gilles Peskinefc9c07f2021-05-12 22:51:53 +02001569#if !defined(MBEDTLS_SSL_PROTO_TLS1_2) || \
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001570 !(defined(MBEDTLS_SHA512_C) && !defined(MBEDTLS_SHA512_NO_SHA384))
Manuel Pégourié-Gonnard8d2805c2019-04-30 12:08:59 +02001571 (void) hash;
1572#endif
Manuel Pégourié-Gonnard1b00c4f2019-04-30 11:54:22 +02001573
Manuel Pégourié-Gonnard1b00c4f2019-04-30 11:54:22 +02001574#if defined(MBEDTLS_SSL_PROTO_SSL3)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001575 if (minor_ver == MBEDTLS_SSL_MINOR_VERSION_0) {
Manuel Pégourié-Gonnard1b00c4f2019-04-30 11:54:22 +02001576 handshake->tls_prf = ssl3_prf;
1577 handshake->calc_verify = ssl_calc_verify_ssl;
1578 handshake->calc_finished = ssl_calc_finished_ssl;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001579 } else
Manuel Pégourié-Gonnard1b00c4f2019-04-30 11:54:22 +02001580#endif
1581#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001582 if (minor_ver < MBEDTLS_SSL_MINOR_VERSION_3) {
Manuel Pégourié-Gonnard1b00c4f2019-04-30 11:54:22 +02001583 handshake->tls_prf = tls1_prf;
1584 handshake->calc_verify = ssl_calc_verify_tls;
1585 handshake->calc_finished = ssl_calc_finished_tls;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001586 } else
Manuel Pégourié-Gonnard1b00c4f2019-04-30 11:54:22 +02001587#endif
1588#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
Gilles Peskined2d59372021-05-12 22:43:27 +02001589#if defined(MBEDTLS_SHA512_C) && !defined(MBEDTLS_SHA512_NO_SHA384)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001590 if (minor_ver == MBEDTLS_SSL_MINOR_VERSION_3 &&
1591 hash == MBEDTLS_MD_SHA384) {
Manuel Pégourié-Gonnard1b00c4f2019-04-30 11:54:22 +02001592 handshake->tls_prf = tls_prf_sha384;
1593 handshake->calc_verify = ssl_calc_verify_tls_sha384;
1594 handshake->calc_finished = ssl_calc_finished_tls_sha384;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001595 } else
Manuel Pégourié-Gonnard1b00c4f2019-04-30 11:54:22 +02001596#endif
1597#if defined(MBEDTLS_SHA256_C)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001598 if (minor_ver == MBEDTLS_SSL_MINOR_VERSION_3) {
Manuel Pégourié-Gonnard1b00c4f2019-04-30 11:54:22 +02001599 handshake->tls_prf = tls_prf_sha256;
1600 handshake->calc_verify = ssl_calc_verify_tls_sha256;
1601 handshake->calc_finished = ssl_calc_finished_tls_sha256;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001602 } else
Manuel Pégourié-Gonnard1b00c4f2019-04-30 11:54:22 +02001603#endif
1604#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
1605 {
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001606 return MBEDTLS_ERR_SSL_INTERNAL_ERROR;
Manuel Pégourié-Gonnard1b00c4f2019-04-30 11:54:22 +02001607 }
1608
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001609 return 0;
Manuel Pégourié-Gonnard1b00c4f2019-04-30 11:54:22 +02001610}
1611
Manuel Pégourié-Gonnard9951b712019-04-30 12:08:59 +02001612/*
Manuel Pégourié-Gonnard85680c42019-05-03 09:16:16 +02001613 * Compute master secret if needed
Manuel Pégourié-Gonnardde047ad2019-05-03 09:46:14 +02001614 *
1615 * Parameters:
1616 * [in/out] handshake
1617 * [in] resume, premaster, extended_ms, calc_verify, tls_prf
Manuel Pégourié-Gonnardde718b92019-05-03 11:43:28 +02001618 * (PSA-PSK) ciphersuite_info, psk_opaque
Manuel Pégourié-Gonnardde047ad2019-05-03 09:46:14 +02001619 * [out] premaster (cleared)
Manuel Pégourié-Gonnardde047ad2019-05-03 09:46:14 +02001620 * [out] master
1621 * [in] ssl: optionally used for debugging, EMS and PSA-PSK
1622 * debug: conf->f_dbg, conf->p_dbg
1623 * EMS: passed to calc_verify (debug + (SSL3) session_negotiate)
Manuel Pégourié-Gonnardde718b92019-05-03 11:43:28 +02001624 * PSA-PSA: minor_ver, conf
Manuel Pégourié-Gonnard9951b712019-04-30 12:08:59 +02001625 */
Manuel Pégourié-Gonnardd904d662022-06-17 10:24:00 +02001626MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001627static int ssl_compute_master(mbedtls_ssl_handshake_params *handshake,
1628 unsigned char *master,
1629 const mbedtls_ssl_context *ssl)
Manuel Pégourié-Gonnard9951b712019-04-30 12:08:59 +02001630{
Janos Follath865b3eb2019-12-16 11:46:15 +00001631 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnard9951b712019-04-30 12:08:59 +02001632
1633 /* cf. RFC 5246, Section 8.1:
1634 * "The master secret is always exactly 48 bytes in length." */
1635 size_t const master_secret_len = 48;
1636
1637#if defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET)
1638 unsigned char session_hash[48];
1639#endif /* MBEDTLS_SSL_EXTENDED_MASTER_SECRET */
1640
Manuel Pégourié-Gonnard85680c42019-05-03 09:16:16 +02001641 /* The label for the KDF used for key expansion.
1642 * This is either "master secret" or "extended master secret"
1643 * depending on whether the Extended Master Secret extension
1644 * is used. */
1645 char const *lbl = "master secret";
1646
1647 /* The salt for the KDF used for key expansion.
1648 * - If the Extended Master Secret extension is not used,
1649 * this is ClientHello.Random + ServerHello.Random
1650 * (see Sect. 8.1 in RFC 5246).
1651 * - If the Extended Master Secret extension is used,
1652 * this is the transcript of the handshake so far.
1653 * (see Sect. 4 in RFC 7627). */
1654 unsigned char const *salt = handshake->randbytes;
1655 size_t salt_len = 64;
1656
Manuel Pégourié-Gonnardde047ad2019-05-03 09:46:14 +02001657#if !defined(MBEDTLS_DEBUG_C) && \
1658 !defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET) && \
1659 !(defined(MBEDTLS_USE_PSA_CRYPTO) && \
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001660 defined(MBEDTLS_KEY_EXCHANGE_PSK_ENABLED))
Manuel Pégourié-Gonnarda7505d12019-05-07 10:17:56 +02001661 ssl = NULL; /* make sure we don't use it except for those cases */
Manuel Pégourié-Gonnardde047ad2019-05-03 09:46:14 +02001662 (void) ssl;
1663#endif
Manuel Pégourié-Gonnardde047ad2019-05-03 09:46:14 +02001664
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001665 if (handshake->resume != 0) {
1666 MBEDTLS_SSL_DEBUG_MSG(3, ("no premaster (session resumed)"));
1667 return 0;
Manuel Pégourié-Gonnard9951b712019-04-30 12:08:59 +02001668 }
Manuel Pégourié-Gonnard9951b712019-04-30 12:08:59 +02001669
1670#if defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001671 if (handshake->extended_ms == MBEDTLS_SSL_EXTENDED_MS_ENABLED) {
Manuel Pégourié-Gonnard85680c42019-05-03 09:16:16 +02001672 lbl = "extended master secret";
1673 salt = session_hash;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001674 handshake->calc_verify(ssl, session_hash, &salt_len);
Manuel Pégourié-Gonnard85680c42019-05-03 09:16:16 +02001675
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001676 MBEDTLS_SSL_DEBUG_BUF(3, "session hash for extended master secret",
1677 session_hash, salt_len);
Manuel Pégourié-Gonnard85680c42019-05-03 09:16:16 +02001678 }
Manuel Pégourié-Gonnard9951b712019-04-30 12:08:59 +02001679#endif /* MBEDTLS_SSL_EXTENDED_MS_ENABLED */
1680
1681#if defined(MBEDTLS_USE_PSA_CRYPTO) && \
Manuel Pégourié-Gonnardde047ad2019-05-03 09:46:14 +02001682 defined(MBEDTLS_KEY_EXCHANGE_PSK_ENABLED)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001683 if (handshake->ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_PSK &&
Manuel Pégourié-Gonnardde718b92019-05-03 11:43:28 +02001684 ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_3 &&
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001685 ssl_use_opaque_psk(ssl) == 1) {
Manuel Pégourié-Gonnard85680c42019-05-03 09:16:16 +02001686 /* Perform PSK-to-MS expansion in a single step. */
1687 psa_status_t status;
1688 psa_algorithm_t alg;
Ronald Croncf56a0a2020-08-04 09:51:30 +02001689 psa_key_id_t psk;
Manuel Pégourié-Gonnard85680c42019-05-03 09:16:16 +02001690 psa_key_derivation_operation_t derivation =
1691 PSA_KEY_DERIVATION_OPERATION_INIT;
Manuel Pégourié-Gonnardde718b92019-05-03 11:43:28 +02001692 mbedtls_md_type_t hash_alg = handshake->ciphersuite_info->mac;
Manuel Pégourié-Gonnard9951b712019-04-30 12:08:59 +02001693
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001694 MBEDTLS_SSL_DEBUG_MSG(2, ("perform PSA-based PSK-to-MS expansion"));
Manuel Pégourié-Gonnard9951b712019-04-30 12:08:59 +02001695
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001696 psk = mbedtls_ssl_get_opaque_psk(ssl);
Manuel Pégourié-Gonnard9951b712019-04-30 12:08:59 +02001697
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001698 if (hash_alg == MBEDTLS_MD_SHA384) {
Manuel Pégourié-Gonnard85680c42019-05-03 09:16:16 +02001699 alg = PSA_ALG_TLS12_PSK_TO_MS(PSA_ALG_SHA_384);
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001700 } else {
Manuel Pégourié-Gonnard85680c42019-05-03 09:16:16 +02001701 alg = PSA_ALG_TLS12_PSK_TO_MS(PSA_ALG_SHA_256);
Manuel Pégourié-Gonnard9951b712019-04-30 12:08:59 +02001702 }
Manuel Pégourié-Gonnard85680c42019-05-03 09:16:16 +02001703
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001704 status = setup_psa_key_derivation(&derivation, psk, alg,
1705 salt, salt_len,
1706 (unsigned char const *) lbl,
1707 (size_t) strlen(lbl),
1708 master_secret_len);
1709 if (status != PSA_SUCCESS) {
1710 psa_key_derivation_abort(&derivation);
1711 return MBEDTLS_ERR_SSL_HW_ACCEL_FAILED;
Manuel Pégourié-Gonnard85680c42019-05-03 09:16:16 +02001712 }
1713
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001714 status = psa_key_derivation_output_bytes(&derivation,
1715 master,
1716 master_secret_len);
1717 if (status != PSA_SUCCESS) {
1718 psa_key_derivation_abort(&derivation);
1719 return MBEDTLS_ERR_SSL_HW_ACCEL_FAILED;
1720 }
1721
1722 status = psa_key_derivation_abort(&derivation);
1723 if (status != PSA_SUCCESS) {
1724 return MBEDTLS_ERR_SSL_HW_ACCEL_FAILED;
1725 }
1726 } else
Manuel Pégourié-Gonnard85680c42019-05-03 09:16:16 +02001727#endif
1728 {
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001729 ret = handshake->tls_prf(handshake->premaster, handshake->pmslen,
1730 lbl, salt, salt_len,
1731 master,
1732 master_secret_len);
1733 if (ret != 0) {
1734 MBEDTLS_SSL_DEBUG_RET(1, "prf", ret);
1735 return ret;
Manuel Pégourié-Gonnard85680c42019-05-03 09:16:16 +02001736 }
1737
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001738 MBEDTLS_SSL_DEBUG_BUF(3, "premaster secret",
1739 handshake->premaster,
1740 handshake->pmslen);
Manuel Pégourié-Gonnard85680c42019-05-03 09:16:16 +02001741
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001742 mbedtls_platform_zeroize(handshake->premaster,
1743 sizeof(handshake->premaster));
Manuel Pégourié-Gonnard9951b712019-04-30 12:08:59 +02001744 }
1745
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001746 return 0;
Manuel Pégourié-Gonnard9951b712019-04-30 12:08:59 +02001747}
Manuel Pégourié-Gonnard1b00c4f2019-04-30 11:54:22 +02001748
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001749int mbedtls_ssl_derive_keys(mbedtls_ssl_context *ssl)
Manuel Pégourié-Gonnarde59ae232019-04-30 11:41:40 +02001750{
Janos Follath865b3eb2019-12-16 11:46:15 +00001751 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnard8d2805c2019-04-30 12:08:59 +02001752 const mbedtls_ssl_ciphersuite_t * const ciphersuite_info =
1753 ssl->handshake->ciphersuite_info;
Manuel Pégourié-Gonnard1b00c4f2019-04-30 11:54:22 +02001754
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001755 MBEDTLS_SSL_DEBUG_MSG(2, ("=> derive keys"));
Manuel Pégourié-Gonnard040a9512019-05-06 12:05:58 +02001756
1757 /* Set PRF, calc_verify and calc_finished function pointers */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001758 ret = ssl_set_handshake_prfs(ssl->handshake,
1759 ssl->minor_ver,
1760 ciphersuite_info->mac);
1761 if (ret != 0) {
1762 MBEDTLS_SSL_DEBUG_RET(1, "ssl_set_handshake_prfs", ret);
1763 return ret;
Manuel Pégourié-Gonnard8d2805c2019-04-30 12:08:59 +02001764 }
Manuel Pégourié-Gonnard1b00c4f2019-04-30 11:54:22 +02001765
Manuel Pégourié-Gonnard040a9512019-05-06 12:05:58 +02001766 /* Compute master secret if needed */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001767 ret = ssl_compute_master(ssl->handshake,
1768 ssl->session_negotiate->master,
1769 ssl);
1770 if (ret != 0) {
1771 MBEDTLS_SSL_DEBUG_RET(1, "ssl_compute_master", ret);
1772 return ret;
Manuel Pégourié-Gonnard9951b712019-04-30 12:08:59 +02001773 }
1774
Manuel Pégourié-Gonnard040a9512019-05-06 12:05:58 +02001775 /* Swap the client and server random values:
1776 * - MS derivation wanted client+server (RFC 5246 8.1)
1777 * - key derivation wants server+client (RFC 5246 6.3) */
1778 {
1779 unsigned char tmp[64];
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001780 memcpy(tmp, ssl->handshake->randbytes, 64);
1781 memcpy(ssl->handshake->randbytes, tmp + 32, 32);
1782 memcpy(ssl->handshake->randbytes + 32, tmp, 32);
1783 mbedtls_platform_zeroize(tmp, sizeof(tmp));
Manuel Pégourié-Gonnard040a9512019-05-06 12:05:58 +02001784 }
1785
1786 /* Populate transform structure */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001787 ret = ssl_populate_transform(ssl->transform_negotiate,
1788 ssl->session_negotiate->ciphersuite,
1789 ssl->session_negotiate->master,
Jarno Lamsac84bd242019-08-16 12:06:56 +03001790#if defined(MBEDTLS_SSL_SOME_MODES_USE_MAC)
Manuel Pégourié-Gonnard6fa57bf2019-05-10 10:50:04 +02001791#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001792 ssl->session_negotiate->encrypt_then_mac,
Jarno Lamsac84bd242019-08-16 12:06:56 +03001793#endif /* MBEDTLS_SSL_ENCRYPT_THEN_MAC */
Manuel Pégourié-Gonnard6fa57bf2019-05-10 10:50:04 +02001794#if defined(MBEDTLS_SSL_TRUNCATED_HMAC)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001795 ssl->session_negotiate->trunc_hmac,
Jarno Lamsac84bd242019-08-16 12:06:56 +03001796#endif /* MBEDTLS_SSL_TRUNCATED_HMAC */
1797#endif /* MBEDTLS_SSL_SOME_MODES_USE_MAC */
Manuel Pégourié-Gonnard6fa57bf2019-05-10 10:50:04 +02001798#if defined(MBEDTLS_ZLIB_SUPPORT)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001799 ssl->session_negotiate->compression,
Manuel Pégourié-Gonnard6fa57bf2019-05-10 10:50:04 +02001800#endif
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001801 ssl->handshake->tls_prf,
1802 ssl->handshake->randbytes,
1803 ssl->minor_ver,
1804 ssl->conf->endpoint,
1805 ssl);
1806 if (ret != 0) {
1807 MBEDTLS_SSL_DEBUG_RET(1, "ssl_populate_transform", ret);
1808 return ret;
Manuel Pégourié-Gonnard040a9512019-05-06 12:05:58 +02001809 }
1810
1811 /* We no longer need Server/ClientHello.random values */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001812 mbedtls_platform_zeroize(ssl->handshake->randbytes,
1813 sizeof(ssl->handshake->randbytes));
Manuel Pégourié-Gonnard040a9512019-05-06 12:05:58 +02001814
Manuel Pégourié-Gonnardd73b47f2019-05-06 12:44:24 +02001815 /* Allocate compression buffer */
1816#if defined(MBEDTLS_ZLIB_SUPPORT)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001817 if (ssl->session_negotiate->compression == MBEDTLS_SSL_COMPRESS_DEFLATE &&
1818 ssl->compress_buf == NULL) {
1819 MBEDTLS_SSL_DEBUG_MSG(3, ("Allocating compression buffer"));
1820 ssl->compress_buf = mbedtls_calloc(1, MBEDTLS_SSL_COMPRESS_BUFFER_LEN);
1821 if (ssl->compress_buf == NULL) {
1822 MBEDTLS_SSL_DEBUG_MSG(1, ("alloc(%d bytes) failed",
1823 MBEDTLS_SSL_COMPRESS_BUFFER_LEN));
1824 return MBEDTLS_ERR_SSL_ALLOC_FAILED;
Manuel Pégourié-Gonnardd73b47f2019-05-06 12:44:24 +02001825 }
1826 }
1827#endif
1828
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001829 MBEDTLS_SSL_DEBUG_MSG(2, ("<= derive keys"));
Manuel Pégourié-Gonnard040a9512019-05-06 12:05:58 +02001830
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001831 return 0;
Manuel Pégourié-Gonnarde59ae232019-04-30 11:41:40 +02001832}
1833
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001834#if defined(MBEDTLS_SSL_PROTO_SSL3)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001835void ssl_calc_verify_ssl(const mbedtls_ssl_context *ssl,
1836 unsigned char *hash,
1837 size_t *hlen)
Paul Bakker5121ce52009-01-03 21:22:43 +00001838{
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02001839 mbedtls_md5_context md5;
1840 mbedtls_sha1_context sha1;
Paul Bakker5121ce52009-01-03 21:22:43 +00001841 unsigned char pad_1[48];
1842 unsigned char pad_2[48];
1843
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001844 MBEDTLS_SSL_DEBUG_MSG(2, ("=> calc verify ssl"));
Paul Bakker5121ce52009-01-03 21:22:43 +00001845
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001846 mbedtls_md5_init(&md5);
1847 mbedtls_sha1_init(&sha1);
Manuel Pégourié-Gonnard001f2b62015-07-06 16:21:13 +02001848
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001849 mbedtls_md5_clone(&md5, &ssl->handshake->fin_md5);
1850 mbedtls_sha1_clone(&sha1, &ssl->handshake->fin_sha1);
Paul Bakker5121ce52009-01-03 21:22:43 +00001851
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001852 memset(pad_1, 0x36, 48);
1853 memset(pad_2, 0x5C, 48);
Paul Bakker5121ce52009-01-03 21:22:43 +00001854
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001855 mbedtls_md5_update_ret(&md5, ssl->session_negotiate->master, 48);
1856 mbedtls_md5_update_ret(&md5, pad_1, 48);
1857 mbedtls_md5_finish_ret(&md5, hash);
Paul Bakker5121ce52009-01-03 21:22:43 +00001858
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001859 mbedtls_md5_starts_ret(&md5);
1860 mbedtls_md5_update_ret(&md5, ssl->session_negotiate->master, 48);
1861 mbedtls_md5_update_ret(&md5, pad_2, 48);
1862 mbedtls_md5_update_ret(&md5, hash, 16);
1863 mbedtls_md5_finish_ret(&md5, hash);
Paul Bakker5121ce52009-01-03 21:22:43 +00001864
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001865 mbedtls_sha1_update_ret(&sha1, ssl->session_negotiate->master, 48);
1866 mbedtls_sha1_update_ret(&sha1, pad_1, 40);
1867 mbedtls_sha1_finish_ret(&sha1, hash + 16);
Paul Bakker380da532012-04-18 16:10:25 +00001868
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001869 mbedtls_sha1_starts_ret(&sha1);
1870 mbedtls_sha1_update_ret(&sha1, ssl->session_negotiate->master, 48);
1871 mbedtls_sha1_update_ret(&sha1, pad_2, 40);
1872 mbedtls_sha1_update_ret(&sha1, hash + 16, 20);
1873 mbedtls_sha1_finish_ret(&sha1, hash + 16);
Paul Bakker380da532012-04-18 16:10:25 +00001874
Manuel Pégourié-Gonnardde718b92019-05-03 11:43:28 +02001875 *hlen = 36;
1876
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001877 MBEDTLS_SSL_DEBUG_BUF(3, "calculated verify result", hash, *hlen);
1878 MBEDTLS_SSL_DEBUG_MSG(2, ("<= calc verify"));
Paul Bakker380da532012-04-18 16:10:25 +00001879
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001880 mbedtls_md5_free(&md5);
1881 mbedtls_sha1_free(&sha1);
Paul Bakker5b4af392014-06-26 12:09:34 +02001882
Paul Bakker380da532012-04-18 16:10:25 +00001883 return;
1884}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001885#endif /* MBEDTLS_SSL_PROTO_SSL3 */
Paul Bakker380da532012-04-18 16:10:25 +00001886
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001887#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001888void ssl_calc_verify_tls(const mbedtls_ssl_context *ssl,
1889 unsigned char *hash,
1890 size_t *hlen)
Paul Bakker380da532012-04-18 16:10:25 +00001891{
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02001892 mbedtls_md5_context md5;
1893 mbedtls_sha1_context sha1;
Paul Bakker380da532012-04-18 16:10:25 +00001894
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001895 MBEDTLS_SSL_DEBUG_MSG(2, ("=> calc verify tls"));
Paul Bakker380da532012-04-18 16:10:25 +00001896
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001897 mbedtls_md5_init(&md5);
1898 mbedtls_sha1_init(&sha1);
Manuel Pégourié-Gonnard001f2b62015-07-06 16:21:13 +02001899
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001900 mbedtls_md5_clone(&md5, &ssl->handshake->fin_md5);
1901 mbedtls_sha1_clone(&sha1, &ssl->handshake->fin_sha1);
Paul Bakker380da532012-04-18 16:10:25 +00001902
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001903 mbedtls_md5_finish_ret(&md5, hash);
1904 mbedtls_sha1_finish_ret(&sha1, hash + 16);
Paul Bakker380da532012-04-18 16:10:25 +00001905
Manuel Pégourié-Gonnardde718b92019-05-03 11:43:28 +02001906 *hlen = 36;
1907
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001908 MBEDTLS_SSL_DEBUG_BUF(3, "calculated verify result", hash, *hlen);
1909 MBEDTLS_SSL_DEBUG_MSG(2, ("<= calc verify"));
Paul Bakker380da532012-04-18 16:10:25 +00001910
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001911 mbedtls_md5_free(&md5);
1912 mbedtls_sha1_free(&sha1);
Paul Bakker5b4af392014-06-26 12:09:34 +02001913
Paul Bakker380da532012-04-18 16:10:25 +00001914 return;
1915}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001916#endif /* MBEDTLS_SSL_PROTO_TLS1 || MBEDTLS_SSL_PROTO_TLS1_1 */
Paul Bakker380da532012-04-18 16:10:25 +00001917
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001918#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
1919#if defined(MBEDTLS_SHA256_C)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001920void ssl_calc_verify_tls_sha256(const mbedtls_ssl_context *ssl,
1921 unsigned char *hash,
1922 size_t *hlen)
Paul Bakker380da532012-04-18 16:10:25 +00001923{
Andrzej Kurekeb342242019-01-29 09:14:33 -05001924#if defined(MBEDTLS_USE_PSA_CRYPTO)
1925 size_t hash_size;
1926 psa_status_t status;
1927 psa_hash_operation_t sha256_psa = psa_hash_operation_init();
1928
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001929 MBEDTLS_SSL_DEBUG_MSG(2, ("=> PSA calc verify sha256"));
1930 status = psa_hash_clone(&ssl->handshake->fin_sha256_psa, &sha256_psa);
1931 if (status != PSA_SUCCESS) {
1932 MBEDTLS_SSL_DEBUG_MSG(2, ("PSA hash clone failed"));
Andrzej Kurekeb342242019-01-29 09:14:33 -05001933 return;
1934 }
1935
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001936 status = psa_hash_finish(&sha256_psa, hash, 32, &hash_size);
1937 if (status != PSA_SUCCESS) {
1938 MBEDTLS_SSL_DEBUG_MSG(2, ("PSA hash finish failed"));
Andrzej Kurekeb342242019-01-29 09:14:33 -05001939 return;
1940 }
Manuel Pégourié-Gonnardde718b92019-05-03 11:43:28 +02001941
1942 *hlen = 32;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001943 MBEDTLS_SSL_DEBUG_BUF(3, "PSA calculated verify result", hash, *hlen);
1944 MBEDTLS_SSL_DEBUG_MSG(2, ("<= PSA calc verify"));
Andrzej Kurekeb342242019-01-29 09:14:33 -05001945#else
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02001946 mbedtls_sha256_context sha256;
Paul Bakker380da532012-04-18 16:10:25 +00001947
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001948 mbedtls_sha256_init(&sha256);
Manuel Pégourié-Gonnard001f2b62015-07-06 16:21:13 +02001949
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001950 MBEDTLS_SSL_DEBUG_MSG(2, ("=> calc verify sha256"));
Paul Bakker380da532012-04-18 16:10:25 +00001951
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001952 mbedtls_sha256_clone(&sha256, &ssl->handshake->fin_sha256);
1953 mbedtls_sha256_finish_ret(&sha256, hash);
Paul Bakker380da532012-04-18 16:10:25 +00001954
Manuel Pégourié-Gonnardde718b92019-05-03 11:43:28 +02001955 *hlen = 32;
1956
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001957 MBEDTLS_SSL_DEBUG_BUF(3, "calculated verify result", hash, *hlen);
1958 MBEDTLS_SSL_DEBUG_MSG(2, ("<= calc verify"));
Paul Bakker380da532012-04-18 16:10:25 +00001959
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001960 mbedtls_sha256_free(&sha256);
Andrzej Kurekeb342242019-01-29 09:14:33 -05001961#endif /* MBEDTLS_USE_PSA_CRYPTO */
Paul Bakker380da532012-04-18 16:10:25 +00001962 return;
1963}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001964#endif /* MBEDTLS_SHA256_C */
Paul Bakker380da532012-04-18 16:10:25 +00001965
Gilles Peskined2d59372021-05-12 22:43:27 +02001966#if defined(MBEDTLS_SHA512_C) && !defined(MBEDTLS_SHA512_NO_SHA384)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001967void ssl_calc_verify_tls_sha384(const mbedtls_ssl_context *ssl,
1968 unsigned char *hash,
1969 size_t *hlen)
Paul Bakker380da532012-04-18 16:10:25 +00001970{
Andrzej Kurekeb342242019-01-29 09:14:33 -05001971#if defined(MBEDTLS_USE_PSA_CRYPTO)
1972 size_t hash_size;
1973 psa_status_t status;
Andrzej Kurek972fba52019-01-30 03:29:12 -05001974 psa_hash_operation_t sha384_psa = psa_hash_operation_init();
Andrzej Kurekeb342242019-01-29 09:14:33 -05001975
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001976 MBEDTLS_SSL_DEBUG_MSG(2, ("=> PSA calc verify sha384"));
1977 status = psa_hash_clone(&ssl->handshake->fin_sha384_psa, &sha384_psa);
1978 if (status != PSA_SUCCESS) {
1979 MBEDTLS_SSL_DEBUG_MSG(2, ("PSA hash clone failed"));
Andrzej Kurekeb342242019-01-29 09:14:33 -05001980 return;
1981 }
1982
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001983 status = psa_hash_finish(&sha384_psa, hash, 48, &hash_size);
1984 if (status != PSA_SUCCESS) {
1985 MBEDTLS_SSL_DEBUG_MSG(2, ("PSA hash finish failed"));
Andrzej Kurekeb342242019-01-29 09:14:33 -05001986 return;
1987 }
Manuel Pégourié-Gonnardde718b92019-05-03 11:43:28 +02001988
1989 *hlen = 48;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001990 MBEDTLS_SSL_DEBUG_BUF(3, "PSA calculated verify result", hash, *hlen);
1991 MBEDTLS_SSL_DEBUG_MSG(2, ("<= PSA calc verify"));
Andrzej Kurekeb342242019-01-29 09:14:33 -05001992#else
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02001993 mbedtls_sha512_context sha512;
Paul Bakker380da532012-04-18 16:10:25 +00001994
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001995 mbedtls_sha512_init(&sha512);
Manuel Pégourié-Gonnard001f2b62015-07-06 16:21:13 +02001996
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001997 MBEDTLS_SSL_DEBUG_MSG(2, ("=> calc verify sha384"));
Paul Bakker380da532012-04-18 16:10:25 +00001998
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001999 mbedtls_sha512_clone(&sha512, &ssl->handshake->fin_sha512);
2000 mbedtls_sha512_finish_ret(&sha512, hash);
Paul Bakker5121ce52009-01-03 21:22:43 +00002001
Manuel Pégourié-Gonnardde718b92019-05-03 11:43:28 +02002002 *hlen = 48;
2003
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002004 MBEDTLS_SSL_DEBUG_BUF(3, "calculated verify result", hash, *hlen);
2005 MBEDTLS_SSL_DEBUG_MSG(2, ("<= calc verify"));
Paul Bakker5121ce52009-01-03 21:22:43 +00002006
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002007 mbedtls_sha512_free(&sha512);
Andrzej Kurekeb342242019-01-29 09:14:33 -05002008#endif /* MBEDTLS_USE_PSA_CRYPTO */
Paul Bakker5121ce52009-01-03 21:22:43 +00002009 return;
2010}
Gilles Peskined2d59372021-05-12 22:43:27 +02002011#endif /* MBEDTLS_SHA512_C && !MBEDTLS_SHA512_NO_SHA384 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002012#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakker5121ce52009-01-03 21:22:43 +00002013
Gilles Peskineeccd8882020-03-10 12:19:08 +01002014#if defined(MBEDTLS_KEY_EXCHANGE_SOME_PSK_ENABLED)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002015int 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 +02002016{
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02002017 unsigned char *p = ssl->handshake->premaster;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002018 unsigned char *end = p + sizeof(ssl->handshake->premaster);
Guilhem Bryantb5f04e42020-04-01 11:23:58 +01002019 const unsigned char *psk = NULL;
Guilhem Bryant61b0fe62020-03-27 11:12:12 +00002020 size_t psk_len = 0;
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01002021
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002022 if (mbedtls_ssl_get_psk(ssl, &psk, &psk_len)
2023 == MBEDTLS_ERR_SSL_PRIVATE_KEY_REQUIRED) {
Guilhem Bryantc5285d82020-03-25 17:08:15 +00002024 /*
2025 * This should never happen because the existence of a PSK is always
2026 * checked before calling this function
2027 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002028 MBEDTLS_SSL_DEBUG_MSG(1, ("should never happen"));
2029 return MBEDTLS_ERR_SSL_INTERNAL_ERROR;
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01002030 }
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02002031
2032 /*
2033 * PMS = struct {
2034 * opaque other_secret<0..2^16-1>;
2035 * opaque psk<0..2^16-1>;
2036 * };
2037 * with "other_secret" depending on the particular key exchange
2038 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002039#if defined(MBEDTLS_KEY_EXCHANGE_PSK_ENABLED)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002040 if (key_ex == MBEDTLS_KEY_EXCHANGE_PSK) {
2041 if (end - p < 2) {
2042 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
2043 }
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02002044
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002045 MBEDTLS_PUT_UINT16_BE(psk_len, p, 0);
Joe Subbiania651e6f2021-08-23 11:35:25 +01002046 p += 2;
Manuel Pégourié-Gonnardbc5e5082015-10-21 12:35:29 +02002047
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002048 if (end < p || (size_t) (end - p) < psk_len) {
2049 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
2050 }
Manuel Pégourié-Gonnardbc5e5082015-10-21 12:35:29 +02002051
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002052 memset(p, 0, psk_len);
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01002053 p += psk_len;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002054 } else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002055#endif /* MBEDTLS_KEY_EXCHANGE_PSK_ENABLED */
2056#if defined(MBEDTLS_KEY_EXCHANGE_RSA_PSK_ENABLED)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002057 if (key_ex == MBEDTLS_KEY_EXCHANGE_RSA_PSK) {
Manuel Pégourié-Gonnard0fae60b2013-10-14 17:39:48 +02002058 /*
2059 * other_secret already set by the ClientKeyExchange message,
2060 * and is 48 bytes long
2061 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002062 if (end - p < 2) {
2063 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
2064 }
Philippe Antoine747fd532018-05-30 09:13:21 +02002065
Manuel Pégourié-Gonnard0fae60b2013-10-14 17:39:48 +02002066 *p++ = 0;
2067 *p++ = 48;
2068 p += 48;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002069 } else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002070#endif /* MBEDTLS_KEY_EXCHANGE_RSA_PSK_ENABLED */
2071#if defined(MBEDTLS_KEY_EXCHANGE_DHE_PSK_ENABLED)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002072 if (key_ex == MBEDTLS_KEY_EXCHANGE_DHE_PSK) {
Janos Follath865b3eb2019-12-16 11:46:15 +00002073 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnard33352052015-06-02 16:17:08 +01002074 size_t len;
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02002075
Manuel Pégourié-Gonnard8df68632014-06-23 17:56:08 +02002076 /* Write length only when we know the actual value */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002077 if ((ret = mbedtls_dhm_calc_secret(&ssl->handshake->dhm_ctx,
2078 p + 2, end - (p + 2), &len,
2079 ssl->conf->f_rng, ssl->conf->p_rng)) != 0) {
2080 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_dhm_calc_secret", ret);
2081 return ret;
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02002082 }
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002083 MBEDTLS_PUT_UINT16_BE(len, p, 0);
Joe Subbiania651e6f2021-08-23 11:35:25 +01002084 p += 2 + len;
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02002085
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002086 MBEDTLS_SSL_DEBUG_MPI(3, "DHM: K ", &ssl->handshake->dhm_ctx.K);
2087 } else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002088#endif /* MBEDTLS_KEY_EXCHANGE_DHE_PSK_ENABLED */
2089#if defined(MBEDTLS_KEY_EXCHANGE_ECDHE_PSK_ENABLED)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002090 if (key_ex == MBEDTLS_KEY_EXCHANGE_ECDHE_PSK) {
Janos Follath865b3eb2019-12-16 11:46:15 +00002091 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02002092 size_t zlen;
2093
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002094 if ((ret = mbedtls_ecdh_calc_secret(&ssl->handshake->ecdh_ctx, &zlen,
2095 p + 2, end - (p + 2),
2096 ssl->conf->f_rng, ssl->conf->p_rng)) != 0) {
2097 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ecdh_calc_secret", ret);
2098 return ret;
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02002099 }
2100
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002101 MBEDTLS_PUT_UINT16_BE(zlen, p, 0);
Joe Subbiania651e6f2021-08-23 11:35:25 +01002102 p += 2 + zlen;
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02002103
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002104 MBEDTLS_SSL_DEBUG_ECDH(3, &ssl->handshake->ecdh_ctx,
2105 MBEDTLS_DEBUG_ECDH_Z);
2106 } else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002107#endif /* MBEDTLS_KEY_EXCHANGE_ECDHE_PSK_ENABLED */
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02002108 {
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002109 MBEDTLS_SSL_DEBUG_MSG(1, ("should never happen"));
2110 return MBEDTLS_ERR_SSL_INTERNAL_ERROR;
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02002111 }
2112
2113 /* opaque psk<0..2^16-1>; */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002114 if (end - p < 2) {
2115 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
2116 }
Manuel Pégourié-Gonnardb2bf5a12014-03-25 16:28:12 +01002117
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002118 MBEDTLS_PUT_UINT16_BE(psk_len, p, 0);
Joe Subbiania651e6f2021-08-23 11:35:25 +01002119 p += 2;
Manuel Pégourié-Gonnardbc5e5082015-10-21 12:35:29 +02002120
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002121 if (end < p || (size_t) (end - p) < psk_len) {
2122 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
2123 }
Manuel Pégourié-Gonnardbc5e5082015-10-21 12:35:29 +02002124
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002125 memcpy(p, psk, psk_len);
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01002126 p += psk_len;
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02002127
2128 ssl->handshake->pmslen = p - ssl->handshake->premaster;
2129
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002130 return 0;
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02002131}
Gilles Peskineeccd8882020-03-10 12:19:08 +01002132#endif /* MBEDTLS_KEY_EXCHANGE_SOME_PSK_ENABLED */
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02002133
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002134#if defined(MBEDTLS_SSL_SRV_C) && defined(MBEDTLS_SSL_RENEGOTIATION)
Manuel Pégourié-Gonnardd904d662022-06-17 10:24:00 +02002135MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002136static int ssl_write_hello_request(mbedtls_ssl_context *ssl);
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +02002137
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002138#if defined(MBEDTLS_SSL_PROTO_DTLS)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002139int mbedtls_ssl_resend_hello_request(mbedtls_ssl_context *ssl)
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +02002140{
2141 /* If renegotiation is not enforced, retransmit until we would reach max
2142 * timeout if we were using the usual handshake doubling scheme */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002143 if (ssl->conf->renego_max_records < 0) {
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02002144 uint32_t ratio = ssl->conf->hs_timeout_max / ssl->conf->hs_timeout_min + 1;
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +02002145 unsigned char doublings = 1;
2146
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002147 while (ratio != 0) {
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +02002148 ++doublings;
2149 ratio >>= 1;
2150 }
2151
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002152 if (++ssl->renego_records_seen > doublings) {
2153 MBEDTLS_SSL_DEBUG_MSG(2, ("no longer retransmitting hello request"));
2154 return 0;
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +02002155 }
2156 }
2157
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002158 return ssl_write_hello_request(ssl);
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +02002159}
2160#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002161#endif /* MBEDTLS_SSL_SRV_C && MBEDTLS_SSL_RENEGOTIATION */
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02002162
Hanno Beckerb9d44792019-02-08 07:19:04 +00002163#if defined(MBEDTLS_X509_CRT_PARSE_C)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002164static void ssl_clear_peer_cert(mbedtls_ssl_session *session)
Hanno Beckerb9d44792019-02-08 07:19:04 +00002165{
2166#if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002167 if (session->peer_cert != NULL) {
2168 mbedtls_x509_crt_free(session->peer_cert);
2169 mbedtls_free(session->peer_cert);
Hanno Beckerb9d44792019-02-08 07:19:04 +00002170 session->peer_cert = NULL;
2171 }
2172#else /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002173 if (session->peer_cert_digest != NULL) {
Hanno Beckerb9d44792019-02-08 07:19:04 +00002174 /* Zeroization is not necessary. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002175 mbedtls_free(session->peer_cert_digest);
Hanno Beckerb9d44792019-02-08 07:19:04 +00002176 session->peer_cert_digest = NULL;
2177 session->peer_cert_digest_type = MBEDTLS_MD_NONE;
2178 session->peer_cert_digest_len = 0;
2179 }
2180#endif /* !MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
2181}
2182#endif /* MBEDTLS_X509_CRT_PARSE_C */
2183
Paul Bakker5121ce52009-01-03 21:22:43 +00002184/*
2185 * Handshake functions
2186 */
Gilles Peskineeccd8882020-03-10 12:19:08 +01002187#if !defined(MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED)
Gilles Peskinef9828522017-05-03 12:28:43 +02002188/* No certificate support -> dummy functions */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002189int mbedtls_ssl_write_certificate(mbedtls_ssl_context *ssl)
Paul Bakker5121ce52009-01-03 21:22:43 +00002190{
Hanno Beckere694c3e2017-12-27 21:34:08 +00002191 const mbedtls_ssl_ciphersuite_t *ciphersuite_info =
2192 ssl->handshake->ciphersuite_info;
Paul Bakker5121ce52009-01-03 21:22:43 +00002193
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002194 MBEDTLS_SSL_DEBUG_MSG(2, ("=> write certificate"));
Paul Bakker5121ce52009-01-03 21:22:43 +00002195
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002196 if (!mbedtls_ssl_ciphersuite_uses_srv_cert(ciphersuite_info)) {
2197 MBEDTLS_SSL_DEBUG_MSG(2, ("<= skip write certificate"));
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02002198 ssl->state++;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002199 return 0;
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02002200 }
2201
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002202 MBEDTLS_SSL_DEBUG_MSG(1, ("should never happen"));
2203 return MBEDTLS_ERR_SSL_INTERNAL_ERROR;
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002204}
2205
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002206int mbedtls_ssl_parse_certificate(mbedtls_ssl_context *ssl)
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002207{
Hanno Beckere694c3e2017-12-27 21:34:08 +00002208 const mbedtls_ssl_ciphersuite_t *ciphersuite_info =
2209 ssl->handshake->ciphersuite_info;
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002210
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002211 MBEDTLS_SSL_DEBUG_MSG(2, ("=> parse certificate"));
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002212
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002213 if (!mbedtls_ssl_ciphersuite_uses_srv_cert(ciphersuite_info)) {
2214 MBEDTLS_SSL_DEBUG_MSG(2, ("<= skip parse certificate"));
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002215 ssl->state++;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002216 return 0;
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002217 }
2218
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002219 MBEDTLS_SSL_DEBUG_MSG(1, ("should never happen"));
2220 return MBEDTLS_ERR_SSL_INTERNAL_ERROR;
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002221}
Gilles Peskinef9828522017-05-03 12:28:43 +02002222
Gilles Peskineeccd8882020-03-10 12:19:08 +01002223#else /* MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED */
Gilles Peskinef9828522017-05-03 12:28:43 +02002224/* Some certificate support -> implement write and parse */
2225
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002226int mbedtls_ssl_write_certificate(mbedtls_ssl_context *ssl)
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002227{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002228 int ret = MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002229 size_t i, n;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002230 const mbedtls_x509_crt *crt;
Hanno Beckere694c3e2017-12-27 21:34:08 +00002231 const mbedtls_ssl_ciphersuite_t *ciphersuite_info =
2232 ssl->handshake->ciphersuite_info;
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002233
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002234 MBEDTLS_SSL_DEBUG_MSG(2, ("=> write certificate"));
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002235
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002236 if (!mbedtls_ssl_ciphersuite_uses_srv_cert(ciphersuite_info)) {
2237 MBEDTLS_SSL_DEBUG_MSG(2, ("<= skip write certificate"));
Johan Pascal4f099262020-09-22 10:59:26 +02002238 ssl->state++;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002239 return 0;
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002240 }
2241
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002242#if defined(MBEDTLS_SSL_CLI_C)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002243 if (ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT) {
2244 if (ssl->client_auth == 0) {
2245 MBEDTLS_SSL_DEBUG_MSG(2, ("<= skip write certificate"));
Paul Bakker5121ce52009-01-03 21:22:43 +00002246 ssl->state++;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002247 return 0;
Paul Bakker5121ce52009-01-03 21:22:43 +00002248 }
2249
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002250#if defined(MBEDTLS_SSL_PROTO_SSL3)
Paul Bakker5121ce52009-01-03 21:22:43 +00002251 /*
2252 * If using SSLv3 and got no cert, send an Alert message
2253 * (otherwise an empty Certificate message will be sent).
2254 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002255 if (mbedtls_ssl_own_cert(ssl) == NULL &&
2256 ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0) {
Paul Bakker5121ce52009-01-03 21:22:43 +00002257 ssl->out_msglen = 2;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002258 ssl->out_msgtype = MBEDTLS_SSL_MSG_ALERT;
2259 ssl->out_msg[0] = MBEDTLS_SSL_ALERT_LEVEL_WARNING;
2260 ssl->out_msg[1] = MBEDTLS_SSL_ALERT_MSG_NO_CERT;
Paul Bakker5121ce52009-01-03 21:22:43 +00002261
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002262 MBEDTLS_SSL_DEBUG_MSG(2, ("got no certificate to send"));
Paul Bakker5121ce52009-01-03 21:22:43 +00002263 goto write_msg;
2264 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002265#endif /* MBEDTLS_SSL_PROTO_SSL3 */
Paul Bakker5121ce52009-01-03 21:22:43 +00002266 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002267#endif /* MBEDTLS_SSL_CLI_C */
2268#if defined(MBEDTLS_SSL_SRV_C)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002269 if (ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER) {
2270 if (mbedtls_ssl_own_cert(ssl) == NULL) {
2271 MBEDTLS_SSL_DEBUG_MSG(1, ("got no certificate to send"));
2272 return MBEDTLS_ERR_SSL_CERTIFICATE_REQUIRED;
Paul Bakker5121ce52009-01-03 21:22:43 +00002273 }
2274 }
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01002275#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00002276
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002277 MBEDTLS_SSL_DEBUG_CRT(3, "own certificate", mbedtls_ssl_own_cert(ssl));
Paul Bakker5121ce52009-01-03 21:22:43 +00002278
2279 /*
2280 * 0 . 0 handshake type
2281 * 1 . 3 handshake length
2282 * 4 . 6 length of all certs
2283 * 7 . 9 length of cert. 1
2284 * 10 . n-1 peer certificate
2285 * n . n+2 length of cert. 2
2286 * n+3 . ... upper level cert, etc.
2287 */
2288 i = 7;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002289 crt = mbedtls_ssl_own_cert(ssl);
Paul Bakker5121ce52009-01-03 21:22:43 +00002290
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002291 while (crt != NULL) {
Paul Bakker5121ce52009-01-03 21:22:43 +00002292 n = crt->raw.len;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002293 if (n > MBEDTLS_SSL_OUT_CONTENT_LEN - 3 - i) {
2294 MBEDTLS_SSL_DEBUG_MSG(1, ("certificate too large, %" MBEDTLS_PRINTF_SIZET
2295 " > %" MBEDTLS_PRINTF_SIZET,
2296 i + 3 + n, (size_t) MBEDTLS_SSL_OUT_CONTENT_LEN));
2297 return MBEDTLS_ERR_SSL_CERTIFICATE_TOO_LARGE;
Paul Bakker5121ce52009-01-03 21:22:43 +00002298 }
2299
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002300 ssl->out_msg[i] = MBEDTLS_BYTE_2(n);
2301 ssl->out_msg[i + 1] = MBEDTLS_BYTE_1(n);
2302 ssl->out_msg[i + 2] = MBEDTLS_BYTE_0(n);
Paul Bakker5121ce52009-01-03 21:22:43 +00002303
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002304 i += 3; memcpy(ssl->out_msg + i, crt->raw.p, n);
Paul Bakker5121ce52009-01-03 21:22:43 +00002305 i += n; crt = crt->next;
2306 }
2307
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002308 ssl->out_msg[4] = MBEDTLS_BYTE_2(i - 7);
2309 ssl->out_msg[5] = MBEDTLS_BYTE_1(i - 7);
2310 ssl->out_msg[6] = MBEDTLS_BYTE_0(i - 7);
Paul Bakker5121ce52009-01-03 21:22:43 +00002311
2312 ssl->out_msglen = i;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002313 ssl->out_msgtype = MBEDTLS_SSL_MSG_HANDSHAKE;
2314 ssl->out_msg[0] = MBEDTLS_SSL_HS_CERTIFICATE;
Paul Bakker5121ce52009-01-03 21:22:43 +00002315
Manuel Pégourié-Gonnardcf141ca2015-05-20 10:35:51 +02002316#if defined(MBEDTLS_SSL_PROTO_SSL3) && defined(MBEDTLS_SSL_CLI_C)
Paul Bakker5121ce52009-01-03 21:22:43 +00002317write_msg:
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002318#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00002319
2320 ssl->state++;
2321
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002322 if ((ret = mbedtls_ssl_write_handshake_msg(ssl)) != 0) {
2323 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ssl_write_handshake_msg", ret);
2324 return ret;
Paul Bakker5121ce52009-01-03 21:22:43 +00002325 }
2326
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002327 MBEDTLS_SSL_DEBUG_MSG(2, ("<= write certificate"));
Paul Bakker5121ce52009-01-03 21:22:43 +00002328
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002329 return ret;
Paul Bakker5121ce52009-01-03 21:22:43 +00002330}
2331
Hanno Becker84879e32019-01-31 07:44:03 +00002332#if defined(MBEDTLS_SSL_RENEGOTIATION) && defined(MBEDTLS_SSL_CLI_C)
Hanno Becker177475a2019-02-05 17:02:46 +00002333
2334#if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
Manuel Pégourié-Gonnardd904d662022-06-17 10:24:00 +02002335MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002336static int ssl_check_peer_crt_unchanged(mbedtls_ssl_context *ssl,
2337 unsigned char *crt_buf,
2338 size_t crt_buf_len)
Hanno Beckerdef9bdc2019-01-30 14:46:46 +00002339{
2340 mbedtls_x509_crt const * const peer_crt = ssl->session->peer_cert;
2341
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002342 if (peer_crt == NULL) {
2343 return -1;
2344 }
Hanno Beckerdef9bdc2019-01-30 14:46:46 +00002345
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002346 if (peer_crt->raw.len != crt_buf_len) {
2347 return -1;
2348 }
Hanno Beckerdef9bdc2019-01-30 14:46:46 +00002349
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002350 return memcmp(peer_crt->raw.p, crt_buf, peer_crt->raw.len);
Hanno Beckerdef9bdc2019-01-30 14:46:46 +00002351}
Hanno Becker177475a2019-02-05 17:02:46 +00002352#else /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
Manuel Pégourié-Gonnardd904d662022-06-17 10:24:00 +02002353MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002354static int ssl_check_peer_crt_unchanged(mbedtls_ssl_context *ssl,
2355 unsigned char *crt_buf,
2356 size_t crt_buf_len)
Hanno Becker177475a2019-02-05 17:02:46 +00002357{
Janos Follath865b3eb2019-12-16 11:46:15 +00002358 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Hanno Becker177475a2019-02-05 17:02:46 +00002359 unsigned char const * const peer_cert_digest =
2360 ssl->session->peer_cert_digest;
2361 mbedtls_md_type_t const peer_cert_digest_type =
2362 ssl->session->peer_cert_digest_type;
2363 mbedtls_md_info_t const * const digest_info =
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002364 mbedtls_md_info_from_type(peer_cert_digest_type);
Hanno Becker177475a2019-02-05 17:02:46 +00002365 unsigned char tmp_digest[MBEDTLS_SSL_PEER_CERT_DIGEST_MAX_LEN];
2366 size_t digest_len;
2367
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002368 if (peer_cert_digest == NULL || digest_info == NULL) {
2369 return -1;
2370 }
Hanno Becker177475a2019-02-05 17:02:46 +00002371
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002372 digest_len = mbedtls_md_get_size(digest_info);
2373 if (digest_len > MBEDTLS_SSL_PEER_CERT_DIGEST_MAX_LEN) {
2374 return -1;
2375 }
Hanno Becker177475a2019-02-05 17:02:46 +00002376
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002377 ret = mbedtls_md(digest_info, crt_buf, crt_buf_len, tmp_digest);
2378 if (ret != 0) {
2379 return -1;
2380 }
Hanno Becker177475a2019-02-05 17:02:46 +00002381
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002382 return memcmp(tmp_digest, peer_cert_digest, digest_len);
Hanno Becker177475a2019-02-05 17:02:46 +00002383}
2384#endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
Hanno Becker84879e32019-01-31 07:44:03 +00002385#endif /* MBEDTLS_SSL_RENEGOTIATION && MBEDTLS_SSL_CLI_C */
Hanno Beckerdef9bdc2019-01-30 14:46:46 +00002386
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02002387/*
2388 * Once the certificate message is read, parse it into a cert chain and
2389 * perform basic checks, but leave actual verification to the caller
2390 */
Manuel Pégourié-Gonnardd904d662022-06-17 10:24:00 +02002391MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002392static int ssl_parse_certificate_chain(mbedtls_ssl_context *ssl,
2393 mbedtls_x509_crt *chain)
Paul Bakker5121ce52009-01-03 21:22:43 +00002394{
Janos Follath865b3eb2019-12-16 11:46:15 +00002395 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Hanno Beckerc7bd7802019-02-05 15:37:23 +00002396#if defined(MBEDTLS_SSL_RENEGOTIATION) && defined(MBEDTLS_SSL_CLI_C)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002397 int crt_cnt = 0;
Hanno Beckerc7bd7802019-02-05 15:37:23 +00002398#endif
Paul Bakker23986e52011-04-24 08:57:21 +00002399 size_t i, n;
Gilles Peskine064a85c2017-05-10 10:46:40 +02002400 uint8_t alert;
Paul Bakker5121ce52009-01-03 21:22:43 +00002401
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002402 if (ssl->in_msgtype != MBEDTLS_SSL_MSG_HANDSHAKE) {
2403 MBEDTLS_SSL_DEBUG_MSG(1, ("bad certificate message"));
2404 mbedtls_ssl_send_alert_message(ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
2405 MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE);
2406 return MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE;
Paul Bakker5121ce52009-01-03 21:22:43 +00002407 }
2408
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002409 if (ssl->in_msg[0] != MBEDTLS_SSL_HS_CERTIFICATE ||
2410 ssl->in_hslen < mbedtls_ssl_hs_hdr_len(ssl) + 3 + 3) {
2411 MBEDTLS_SSL_DEBUG_MSG(1, ("bad certificate message"));
2412 mbedtls_ssl_send_alert_message(ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
2413 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR);
2414 return MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE;
Paul Bakker5121ce52009-01-03 21:22:43 +00002415 }
2416
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002417 i = mbedtls_ssl_hs_hdr_len(ssl);
Manuel Pégourié-Gonnardf49a7da2014-09-10 13:30:43 +00002418
Paul Bakker5121ce52009-01-03 21:22:43 +00002419 /*
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002420 * Same message structure as in mbedtls_ssl_write_certificate()
Paul Bakker5121ce52009-01-03 21:22:43 +00002421 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002422 n = (ssl->in_msg[i+1] << 8) | ssl->in_msg[i+2];
Paul Bakker5121ce52009-01-03 21:22:43 +00002423
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002424 if (ssl->in_msg[i] != 0 ||
2425 ssl->in_hslen != n + 3 + mbedtls_ssl_hs_hdr_len(ssl)) {
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
Hanno Beckerdef9bdc2019-01-30 14:46:46 +00002432 /* Make &ssl->in_msg[i] point to the beginning of the CRT chain. */
2433 i += 3;
2434
Hanno Beckerdef9bdc2019-01-30 14:46:46 +00002435 /* Iterate through and parse the CRTs in the provided chain. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002436 while (i < ssl->in_hslen) {
Hanno Beckerdef9bdc2019-01-30 14:46:46 +00002437 /* Check that there's room for the next CRT's length fields. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002438 if (i + 3 > ssl->in_hslen) {
2439 MBEDTLS_SSL_DEBUG_MSG(1, ("bad certificate message"));
2440 mbedtls_ssl_send_alert_message(ssl,
2441 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
2442 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR);
2443 return MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE;
Philippe Antoine747fd532018-05-30 09:13:21 +02002444 }
Hanno Beckerdef9bdc2019-01-30 14:46:46 +00002445 /* In theory, the CRT can be up to 2**24 Bytes, but we don't support
2446 * anything beyond 2**16 ~ 64K. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002447 if (ssl->in_msg[i] != 0) {
2448 MBEDTLS_SSL_DEBUG_MSG(1, ("bad certificate message"));
2449 mbedtls_ssl_send_alert_message(ssl,
2450 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
2451 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR);
2452 return MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE;
Paul Bakker5121ce52009-01-03 21:22:43 +00002453 }
2454
Hanno Beckerdef9bdc2019-01-30 14:46:46 +00002455 /* Read length of the next CRT in the chain. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002456 n = ((unsigned int) ssl->in_msg[i + 1] << 8)
Paul Bakker5121ce52009-01-03 21:22:43 +00002457 | (unsigned int) ssl->in_msg[i + 2];
2458 i += 3;
2459
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002460 if (n < 128 || i + n > ssl->in_hslen) {
2461 MBEDTLS_SSL_DEBUG_MSG(1, ("bad certificate message"));
2462 mbedtls_ssl_send_alert_message(ssl,
2463 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
2464 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR);
2465 return MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE;
Paul Bakker5121ce52009-01-03 21:22:43 +00002466 }
2467
Hanno Beckerdef9bdc2019-01-30 14:46:46 +00002468 /* Check if we're handling the first CRT in the chain. */
Hanno Beckerc7bd7802019-02-05 15:37:23 +00002469#if defined(MBEDTLS_SSL_RENEGOTIATION) && defined(MBEDTLS_SSL_CLI_C)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002470 if (crt_cnt++ == 0 &&
Hanno Beckerc7bd7802019-02-05 15:37:23 +00002471 ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT &&
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002472 ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS) {
Hanno Becker46f34d02019-02-08 14:00:04 +00002473 /* During client-side renegotiation, check that the server's
2474 * end-CRTs hasn't changed compared to the initial handshake,
2475 * mitigating the triple handshake attack. On success, reuse
2476 * the original end-CRT instead of parsing it again. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002477 MBEDTLS_SSL_DEBUG_MSG(3, ("Check that peer CRT hasn't changed during renegotiation"));
2478 if (ssl_check_peer_crt_unchanged(ssl,
2479 &ssl->in_msg[i],
2480 n) != 0) {
2481 MBEDTLS_SSL_DEBUG_MSG(1, ("new server cert during renegotiation"));
2482 mbedtls_ssl_send_alert_message(ssl,
2483 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
2484 MBEDTLS_SSL_ALERT_MSG_ACCESS_DENIED);
2485 return MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE;
Hanno Beckerdef9bdc2019-01-30 14:46:46 +00002486 }
Hanno Beckerc7bd7802019-02-05 15:37:23 +00002487
2488 /* Now we can safely free the original chain. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002489 ssl_clear_peer_cert(ssl->session);
Hanno Beckerc7bd7802019-02-05 15:37:23 +00002490 }
Hanno Beckerdef9bdc2019-01-30 14:46:46 +00002491#endif /* MBEDTLS_SSL_RENEGOTIATION && MBEDTLS_SSL_CLI_C */
2492
Hanno Beckerdef9bdc2019-01-30 14:46:46 +00002493 /* Parse the next certificate in the chain. */
Hanno Becker0056eab2019-02-08 14:39:16 +00002494#if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002495 ret = mbedtls_x509_crt_parse_der(chain, ssl->in_msg + i, n);
Hanno Becker0056eab2019-02-08 14:39:16 +00002496#else
Hanno Becker353a6f02019-02-26 11:51:34 +00002497 /* If we don't need to store the CRT chain permanently, parse
Hanno Becker0056eab2019-02-08 14:39:16 +00002498 * it in-place from the input buffer instead of making a copy. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002499 ret = mbedtls_x509_crt_parse_der_nocopy(chain, ssl->in_msg + i, n);
Hanno Becker0056eab2019-02-08 14:39:16 +00002500#endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002501 switch (ret) {
Hanno Beckerdef9bdc2019-01-30 14:46:46 +00002502 case 0: /*ok*/
2503 case MBEDTLS_ERR_X509_UNKNOWN_SIG_ALG + MBEDTLS_ERR_OID_NOT_FOUND:
2504 /* Ignore certificate with an unknown algorithm: maybe a
2505 prior certificate was already trusted. */
2506 break;
Gilles Peskine1cc8e342017-05-03 16:28:34 +02002507
Hanno Beckerdef9bdc2019-01-30 14:46:46 +00002508 case MBEDTLS_ERR_X509_ALLOC_FAILED:
2509 alert = MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR;
2510 goto crt_parse_der_failed;
Gilles Peskine1cc8e342017-05-03 16:28:34 +02002511
Hanno Beckerdef9bdc2019-01-30 14:46:46 +00002512 case MBEDTLS_ERR_X509_UNKNOWN_VERSION:
2513 alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT;
2514 goto crt_parse_der_failed;
Gilles Peskine1cc8e342017-05-03 16:28:34 +02002515
Hanno Beckerdef9bdc2019-01-30 14:46:46 +00002516 default:
2517 alert = MBEDTLS_SSL_ALERT_MSG_BAD_CERT;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002518crt_parse_der_failed:
2519 mbedtls_ssl_send_alert_message(ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL, alert);
2520 MBEDTLS_SSL_DEBUG_RET(1, " mbedtls_x509_crt_parse_der", ret);
2521 return ret;
Paul Bakker5121ce52009-01-03 21:22:43 +00002522 }
2523
2524 i += n;
2525 }
2526
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002527 MBEDTLS_SSL_DEBUG_CRT(3, "peer certificate", chain);
2528 return 0;
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02002529}
2530
Hanno Becker4a55f632019-02-05 12:49:06 +00002531#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnardd904d662022-06-17 10:24:00 +02002532MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002533static int ssl_srv_check_client_no_crt_notification(mbedtls_ssl_context *ssl)
Hanno Becker4a55f632019-02-05 12:49:06 +00002534{
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002535 if (ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT) {
2536 return -1;
2537 }
Hanno Becker4a55f632019-02-05 12:49:06 +00002538
2539#if defined(MBEDTLS_SSL_PROTO_SSL3)
2540 /*
2541 * Check if the client sent an empty certificate
2542 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002543 if (ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0) {
2544 if (ssl->in_msglen == 2 &&
Hanno Becker4a55f632019-02-05 12:49:06 +00002545 ssl->in_msgtype == MBEDTLS_SSL_MSG_ALERT &&
2546 ssl->in_msg[0] == MBEDTLS_SSL_ALERT_LEVEL_WARNING &&
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002547 ssl->in_msg[1] == MBEDTLS_SSL_ALERT_MSG_NO_CERT) {
2548 MBEDTLS_SSL_DEBUG_MSG(1, ("SSLv3 client has no certificate"));
2549 return 0;
Hanno Becker4a55f632019-02-05 12:49:06 +00002550 }
2551
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002552 return -1;
Hanno Becker4a55f632019-02-05 12:49:06 +00002553 }
2554#endif /* MBEDTLS_SSL_PROTO_SSL3 */
2555
2556#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1) || \
2557 defined(MBEDTLS_SSL_PROTO_TLS1_2)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002558 if (ssl->in_hslen == 3 + mbedtls_ssl_hs_hdr_len(ssl) &&
Hanno Becker4a55f632019-02-05 12:49:06 +00002559 ssl->in_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE &&
2560 ssl->in_msg[0] == MBEDTLS_SSL_HS_CERTIFICATE &&
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002561 memcmp(ssl->in_msg + mbedtls_ssl_hs_hdr_len(ssl), "\0\0\0", 3) == 0) {
2562 MBEDTLS_SSL_DEBUG_MSG(1, ("TLSv1 client has no certificate"));
2563 return 0;
Hanno Becker4a55f632019-02-05 12:49:06 +00002564 }
2565
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002566 return -1;
Hanno Becker4a55f632019-02-05 12:49:06 +00002567#endif /* MBEDTLS_SSL_PROTO_TLS1 || MBEDTLS_SSL_PROTO_TLS1_1 || \
2568 MBEDTLS_SSL_PROTO_TLS1_2 */
2569}
2570#endif /* MBEDTLS_SSL_SRV_C */
2571
Hanno Becker28f2fcd2019-02-07 10:11:07 +00002572/* Check if a certificate message is expected.
2573 * Return either
2574 * - SSL_CERTIFICATE_EXPECTED, or
2575 * - SSL_CERTIFICATE_SKIP
2576 * indicating whether a Certificate message is expected or not.
2577 */
2578#define SSL_CERTIFICATE_EXPECTED 0
2579#define SSL_CERTIFICATE_SKIP 1
Manuel Pégourié-Gonnardd904d662022-06-17 10:24:00 +02002580MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002581static int ssl_parse_certificate_coordinate(mbedtls_ssl_context *ssl,
2582 int authmode)
Hanno Becker28f2fcd2019-02-07 10:11:07 +00002583{
2584 const mbedtls_ssl_ciphersuite_t *ciphersuite_info =
Hanno Beckere694c3e2017-12-27 21:34:08 +00002585 ssl->handshake->ciphersuite_info;
Hanno Becker28f2fcd2019-02-07 10:11:07 +00002586
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002587 if (!mbedtls_ssl_ciphersuite_uses_srv_cert(ciphersuite_info)) {
2588 return SSL_CERTIFICATE_SKIP;
2589 }
Hanno Becker28f2fcd2019-02-07 10:11:07 +00002590
2591#if defined(MBEDTLS_SSL_SRV_C)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002592 if (ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER) {
2593 if (ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_RSA_PSK) {
2594 return SSL_CERTIFICATE_SKIP;
2595 }
Hanno Becker28f2fcd2019-02-07 10:11:07 +00002596
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002597 if (authmode == MBEDTLS_SSL_VERIFY_NONE) {
Hanno Becker28f2fcd2019-02-07 10:11:07 +00002598 ssl->session_negotiate->verify_result =
2599 MBEDTLS_X509_BADCERT_SKIP_VERIFY;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002600 return SSL_CERTIFICATE_SKIP;
Hanno Becker28f2fcd2019-02-07 10:11:07 +00002601 }
2602 }
Hanno Becker84d9d272019-03-01 08:10:46 +00002603#else
2604 ((void) authmode);
Hanno Becker28f2fcd2019-02-07 10:11:07 +00002605#endif /* MBEDTLS_SSL_SRV_C */
2606
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002607 return SSL_CERTIFICATE_EXPECTED;
Hanno Becker28f2fcd2019-02-07 10:11:07 +00002608}
2609
Gilles Peskine3a2f75d2025-02-12 23:28:48 +01002610static int get_hostname_for_verification(mbedtls_ssl_context *ssl,
2611 const char **hostname)
2612{
2613 if (!mbedtls_ssl_has_set_hostname_been_called(ssl)) {
2614 MBEDTLS_SSL_DEBUG_MSG(1, ("Certificate verification without having set hostname"));
Gilles Peskine551756d2025-02-13 14:39:02 +01002615#if !defined(MBEDTLS_SSL_CLI_ALLOW_WEAK_CERTIFICATE_VERIFICATION_WITHOUT_HOSTNAME)
2616 if (ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT &&
2617 ssl->conf->authmode == MBEDTLS_SSL_VERIFY_REQUIRED) {
2618 return MBEDTLS_ERR_SSL_CERTIFICATE_VERIFICATION_WITHOUT_HOSTNAME;
2619 }
2620#endif
Gilles Peskine3a2f75d2025-02-12 23:28:48 +01002621 }
2622
2623 *hostname = mbedtls_ssl_get_hostname_pointer(ssl);
2624 if (*hostname == NULL) {
2625 MBEDTLS_SSL_DEBUG_MSG(2, ("Certificate verification without CN verification"));
2626 }
2627
2628 return 0;
2629}
2630
Manuel Pégourié-Gonnardd904d662022-06-17 10:24:00 +02002631MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002632static int ssl_parse_certificate_verify(mbedtls_ssl_context *ssl,
2633 int authmode,
2634 mbedtls_x509_crt *chain,
2635 void *rs_ctx)
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02002636{
Hanno Becker28f2fcd2019-02-07 10:11:07 +00002637 const mbedtls_ssl_ciphersuite_t *ciphersuite_info =
Hanno Beckere694c3e2017-12-27 21:34:08 +00002638 ssl->handshake->ciphersuite_info;
Hanno Beckerafd0b0a2019-03-27 16:55:01 +00002639 int have_ca_chain = 0;
Hanno Becker68636192019-02-05 14:36:34 +00002640
Hanno Becker8927c832019-04-03 12:52:50 +01002641 int (*f_vrfy)(void *, mbedtls_x509_crt *, int, uint32_t *);
2642 void *p_vrfy;
2643
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002644 if (authmode == MBEDTLS_SSL_VERIFY_NONE) {
2645 return 0;
2646 }
Hanno Becker68636192019-02-05 14:36:34 +00002647
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002648 if (ssl->f_vrfy != NULL) {
2649 MBEDTLS_SSL_DEBUG_MSG(3, ("Use context-specific verification callback"));
Hanno Becker8927c832019-04-03 12:52:50 +01002650 f_vrfy = ssl->f_vrfy;
2651 p_vrfy = ssl->p_vrfy;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002652 } else {
2653 MBEDTLS_SSL_DEBUG_MSG(3, ("Use configuration-specific verification callback"));
Hanno Becker8927c832019-04-03 12:52:50 +01002654 f_vrfy = ssl->conf->f_vrfy;
2655 p_vrfy = ssl->conf->p_vrfy;
2656 }
2657
Gilles Peskine3a2f75d2025-02-12 23:28:48 +01002658 const char *hostname = "";
2659 int ret = get_hostname_for_verification(ssl, &hostname);
2660 if (ret != 0) {
2661 MBEDTLS_SSL_DEBUG_RET(1, "get_hostname_for_verification", ret);
2662 return ret;
2663 }
2664
Hanno Becker68636192019-02-05 14:36:34 +00002665 /*
2666 * Main check: verify certificate
2667 */
Hanno Beckerafd0b0a2019-03-27 16:55:01 +00002668#if defined(MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002669 if (ssl->conf->f_ca_cb != NULL) {
Hanno Beckerafd0b0a2019-03-27 16:55:01 +00002670 ((void) rs_ctx);
2671 have_ca_chain = 1;
2672
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002673 MBEDTLS_SSL_DEBUG_MSG(3, ("use CA callback for X.509 CRT verification"));
Jarno Lamsa9822c0d2019-04-01 16:59:48 +03002674 ret = mbedtls_x509_crt_verify_with_ca_cb(
Hanno Beckerafd0b0a2019-03-27 16:55:01 +00002675 chain,
2676 ssl->conf->f_ca_cb,
2677 ssl->conf->p_ca_cb,
2678 ssl->conf->cert_profile,
Gilles Peskine3a2f75d2025-02-12 23:28:48 +01002679 hostname,
Hanno Beckerafd0b0a2019-03-27 16:55:01 +00002680 &ssl->session_negotiate->verify_result,
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002681 f_vrfy, p_vrfy);
2682 } else
Hanno Beckerafd0b0a2019-03-27 16:55:01 +00002683#endif /* MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK */
2684 {
2685 mbedtls_x509_crt *ca_chain;
2686 mbedtls_x509_crl *ca_crl;
2687
2688#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002689 if (ssl->handshake->sni_ca_chain != NULL) {
Hanno Beckerafd0b0a2019-03-27 16:55:01 +00002690 ca_chain = ssl->handshake->sni_ca_chain;
2691 ca_crl = ssl->handshake->sni_ca_crl;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002692 } else
Hanno Beckerafd0b0a2019-03-27 16:55:01 +00002693#endif
2694 {
2695 ca_chain = ssl->conf->ca_chain;
2696 ca_crl = ssl->conf->ca_crl;
2697 }
2698
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002699 if (ca_chain != NULL) {
Hanno Beckerafd0b0a2019-03-27 16:55:01 +00002700 have_ca_chain = 1;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002701 }
Hanno Beckerafd0b0a2019-03-27 16:55:01 +00002702
2703 ret = mbedtls_x509_crt_verify_restartable(
2704 chain,
2705 ca_chain, ca_crl,
2706 ssl->conf->cert_profile,
Gilles Peskine3a2f75d2025-02-12 23:28:48 +01002707 hostname,
Hanno Beckerafd0b0a2019-03-27 16:55:01 +00002708 &ssl->session_negotiate->verify_result,
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002709 f_vrfy, p_vrfy, rs_ctx);
Hanno Beckerafd0b0a2019-03-27 16:55:01 +00002710 }
Hanno Becker68636192019-02-05 14:36:34 +00002711
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002712 if (ret != 0) {
2713 MBEDTLS_SSL_DEBUG_RET(1, "x509_verify_cert", ret);
Hanno Becker68636192019-02-05 14:36:34 +00002714 }
2715
Gilles Peskineeccd8882020-03-10 12:19:08 +01002716#if defined(MBEDTLS_SSL_ECP_RESTARTABLE_ENABLED)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002717 if (ret == MBEDTLS_ERR_ECP_IN_PROGRESS) {
2718 return MBEDTLS_ERR_SSL_CRYPTO_IN_PROGRESS;
2719 }
Hanno Becker68636192019-02-05 14:36:34 +00002720#endif
2721
2722 /*
2723 * Secondary checks: always done, but change 'ret' only if it was 0
2724 */
2725
2726#if defined(MBEDTLS_ECP_C)
2727 {
2728 const mbedtls_pk_context *pk = &chain->pk;
2729
Manuel Pégourié-Gonnard06e1fcd2022-06-16 10:48:06 +02002730 /* If certificate uses an EC key, make sure the curve is OK.
2731 * This is a public key, so it can't be opaque, so can_do() is a good
2732 * enough check to ensure pk_ec() is safe to use here. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002733 if (mbedtls_pk_can_do(pk, MBEDTLS_PK_ECKEY) &&
2734 mbedtls_ssl_check_curve(ssl, mbedtls_pk_ec(*pk)->grp.id) != 0) {
Hanno Becker68636192019-02-05 14:36:34 +00002735 ssl->session_negotiate->verify_result |= MBEDTLS_X509_BADCERT_BAD_KEY;
2736
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002737 MBEDTLS_SSL_DEBUG_MSG(1, ("bad certificate (EC key curve)"));
2738 if (ret == 0) {
Hanno Becker68636192019-02-05 14:36:34 +00002739 ret = MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002740 }
Hanno Becker68636192019-02-05 14:36:34 +00002741 }
2742 }
2743#endif /* MBEDTLS_ECP_C */
2744
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002745 if (mbedtls_ssl_check_cert_usage(chain,
2746 ciphersuite_info,
2747 !ssl->conf->endpoint,
2748 &ssl->session_negotiate->verify_result) != 0) {
2749 MBEDTLS_SSL_DEBUG_MSG(1, ("bad certificate (usage extensions)"));
2750 if (ret == 0) {
Hanno Becker68636192019-02-05 14:36:34 +00002751 ret = MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002752 }
Hanno Becker68636192019-02-05 14:36:34 +00002753 }
2754
2755 /* mbedtls_x509_crt_verify_with_profile is supposed to report a
2756 * verification failure through MBEDTLS_ERR_X509_CERT_VERIFY_FAILED,
2757 * with details encoded in the verification flags. All other kinds
2758 * of error codes, including those from the user provided f_vrfy
2759 * functions, are treated as fatal and lead to a failure of
2760 * ssl_parse_certificate even if verification was optional. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002761 if (authmode == MBEDTLS_SSL_VERIFY_OPTIONAL &&
2762 (ret == MBEDTLS_ERR_X509_CERT_VERIFY_FAILED ||
2763 ret == MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE)) {
Hanno Becker68636192019-02-05 14:36:34 +00002764 ret = 0;
2765 }
2766
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002767 if (have_ca_chain == 0 && authmode == MBEDTLS_SSL_VERIFY_REQUIRED) {
2768 MBEDTLS_SSL_DEBUG_MSG(1, ("got no CA chain"));
Hanno Becker68636192019-02-05 14:36:34 +00002769 ret = MBEDTLS_ERR_SSL_CA_CHAIN_REQUIRED;
2770 }
2771
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002772 if (ret != 0) {
Hanno Becker68636192019-02-05 14:36:34 +00002773 uint8_t alert;
2774
2775 /* The certificate may have been rejected for several reasons.
2776 Pick one and send the corresponding alert. Which alert to send
2777 may be a subject of debate in some cases. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002778 if (ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_OTHER) {
Hanno Becker68636192019-02-05 14:36:34 +00002779 alert = MBEDTLS_SSL_ALERT_MSG_ACCESS_DENIED;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002780 } else if (ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_CN_MISMATCH) {
Hanno Becker68636192019-02-05 14:36:34 +00002781 alert = MBEDTLS_SSL_ALERT_MSG_BAD_CERT;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002782 } else if (ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_KEY_USAGE) {
Hanno Becker68636192019-02-05 14:36:34 +00002783 alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002784 } else if (ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_EXT_KEY_USAGE) {
Hanno Becker68636192019-02-05 14:36:34 +00002785 alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002786 } else if (ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_NS_CERT_TYPE) {
Hanno Becker68636192019-02-05 14:36:34 +00002787 alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002788 } else if (ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_BAD_PK) {
Hanno Becker68636192019-02-05 14:36:34 +00002789 alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002790 } else if (ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_BAD_KEY) {
Hanno Becker68636192019-02-05 14:36:34 +00002791 alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002792 } else if (ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_EXPIRED) {
Hanno Becker68636192019-02-05 14:36:34 +00002793 alert = MBEDTLS_SSL_ALERT_MSG_CERT_EXPIRED;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002794 } else if (ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_REVOKED) {
Hanno Becker68636192019-02-05 14:36:34 +00002795 alert = MBEDTLS_SSL_ALERT_MSG_CERT_REVOKED;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002796 } else if (ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_NOT_TRUSTED) {
Hanno Becker68636192019-02-05 14:36:34 +00002797 alert = MBEDTLS_SSL_ALERT_MSG_UNKNOWN_CA;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002798 } else {
Hanno Becker68636192019-02-05 14:36:34 +00002799 alert = MBEDTLS_SSL_ALERT_MSG_CERT_UNKNOWN;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002800 }
2801 mbedtls_ssl_send_alert_message(ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
2802 alert);
Hanno Becker68636192019-02-05 14:36:34 +00002803 }
2804
2805#if defined(MBEDTLS_DEBUG_C)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002806 if (ssl->session_negotiate->verify_result != 0) {
2807 MBEDTLS_SSL_DEBUG_MSG(3, ("! Certificate verification flags %08x",
2808 (unsigned int) ssl->session_negotiate->verify_result));
2809 } else {
2810 MBEDTLS_SSL_DEBUG_MSG(3, ("Certificate verification flags clear"));
Hanno Becker68636192019-02-05 14:36:34 +00002811 }
2812#endif /* MBEDTLS_DEBUG_C */
2813
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002814 return ret;
Hanno Becker68636192019-02-05 14:36:34 +00002815}
2816
Hanno Becker6b8fbab2019-02-08 14:59:05 +00002817#if !defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
Manuel Pégourié-Gonnardd904d662022-06-17 10:24:00 +02002818MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002819static int ssl_remember_peer_crt_digest(mbedtls_ssl_context *ssl,
2820 unsigned char *start, size_t len)
Hanno Becker6b8fbab2019-02-08 14:59:05 +00002821{
Janos Follath865b3eb2019-12-16 11:46:15 +00002822 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Hanno Becker6b8fbab2019-02-08 14:59:05 +00002823 /* Remember digest of the peer's end-CRT. */
2824 ssl->session_negotiate->peer_cert_digest =
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002825 mbedtls_calloc(1, MBEDTLS_SSL_PEER_CERT_DIGEST_DFL_LEN);
2826 if (ssl->session_negotiate->peer_cert_digest == NULL) {
2827 MBEDTLS_SSL_DEBUG_MSG(1, ("alloc(%d bytes) failed",
2828 MBEDTLS_SSL_PEER_CERT_DIGEST_DFL_LEN));
2829 mbedtls_ssl_send_alert_message(ssl,
2830 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
2831 MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR);
Hanno Becker6b8fbab2019-02-08 14:59:05 +00002832
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002833 return MBEDTLS_ERR_SSL_ALLOC_FAILED;
Hanno Becker6b8fbab2019-02-08 14:59:05 +00002834 }
2835
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002836 ret = mbedtls_md(mbedtls_md_info_from_type(
2837 MBEDTLS_SSL_PEER_CERT_DIGEST_DFL_TYPE),
2838 start, len,
2839 ssl->session_negotiate->peer_cert_digest);
Hanno Becker6b8fbab2019-02-08 14:59:05 +00002840
2841 ssl->session_negotiate->peer_cert_digest_type =
2842 MBEDTLS_SSL_PEER_CERT_DIGEST_DFL_TYPE;
2843 ssl->session_negotiate->peer_cert_digest_len =
2844 MBEDTLS_SSL_PEER_CERT_DIGEST_DFL_LEN;
2845
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002846 return ret;
Hanno Becker6b8fbab2019-02-08 14:59:05 +00002847}
2848
Manuel Pégourié-Gonnardd904d662022-06-17 10:24:00 +02002849MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002850static int ssl_remember_peer_pubkey(mbedtls_ssl_context *ssl,
2851 unsigned char *start, size_t len)
Hanno Becker6b8fbab2019-02-08 14:59:05 +00002852{
2853 unsigned char *end = start + len;
Janos Follath865b3eb2019-12-16 11:46:15 +00002854 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Hanno Becker6b8fbab2019-02-08 14:59:05 +00002855
2856 /* Make a copy of the peer's raw public key. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002857 mbedtls_pk_init(&ssl->handshake->peer_pubkey);
2858 ret = mbedtls_pk_parse_subpubkey(&start, end,
2859 &ssl->handshake->peer_pubkey);
2860 if (ret != 0) {
Hanno Becker6b8fbab2019-02-08 14:59:05 +00002861 /* We should have parsed the public key before. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002862 return MBEDTLS_ERR_SSL_INTERNAL_ERROR;
Hanno Becker6b8fbab2019-02-08 14:59:05 +00002863 }
2864
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002865 return 0;
Hanno Becker6b8fbab2019-02-08 14:59:05 +00002866}
2867#endif /* !MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
2868
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002869int mbedtls_ssl_parse_certificate(mbedtls_ssl_context *ssl)
Hanno Becker68636192019-02-05 14:36:34 +00002870{
2871 int ret = 0;
Hanno Becker28f2fcd2019-02-07 10:11:07 +00002872 int crt_expected;
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02002873#if defined(MBEDTLS_SSL_SRV_C) && defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
2874 const int authmode = ssl->handshake->sni_authmode != MBEDTLS_SSL_VERIFY_UNSET
2875 ? ssl->handshake->sni_authmode
2876 : ssl->conf->authmode;
2877#else
Johan Pascal4f099262020-09-22 10:59:26 +02002878 const int authmode = ssl->conf->authmode;
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02002879#endif
Manuel Pégourié-Gonnard3bf49c42017-08-15 13:47:06 +02002880 void *rs_ctx = NULL;
Hanno Becker3dad3112019-02-05 17:19:52 +00002881 mbedtls_x509_crt *chain = NULL;
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02002882
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002883 MBEDTLS_SSL_DEBUG_MSG(2, ("=> parse certificate"));
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02002884
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002885 crt_expected = ssl_parse_certificate_coordinate(ssl, authmode);
2886 if (crt_expected == SSL_CERTIFICATE_SKIP) {
2887 MBEDTLS_SSL_DEBUG_MSG(2, ("<= skip parse certificate"));
Hanno Becker6bdfab22019-02-05 13:11:17 +00002888 goto exit;
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02002889 }
2890
Gilles Peskineeccd8882020-03-10 12:19:08 +01002891#if defined(MBEDTLS_SSL_ECP_RESTARTABLE_ENABLED)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002892 if (ssl->handshake->ecrs_enabled &&
2893 ssl->handshake->ecrs_state == ssl_ecrs_crt_verify) {
Hanno Becker3dad3112019-02-05 17:19:52 +00002894 chain = ssl->handshake->ecrs_peer_cert;
2895 ssl->handshake->ecrs_peer_cert = NULL;
Manuel Pégourié-Gonnard3bf49c42017-08-15 13:47:06 +02002896 goto crt_verify;
2897 }
2898#endif
2899
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002900 if ((ret = mbedtls_ssl_read_record(ssl, 1)) != 0) {
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02002901 /* mbedtls_ssl_read_record may have sent an alert already. We
2902 let it decide whether to alert. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002903 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ssl_read_record", ret);
Hanno Becker3dad3112019-02-05 17:19:52 +00002904 goto exit;
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02002905 }
2906
Hanno Becker4a55f632019-02-05 12:49:06 +00002907#if defined(MBEDTLS_SSL_SRV_C)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002908 if (ssl_srv_check_client_no_crt_notification(ssl) == 0) {
Hanno Becker4a55f632019-02-05 12:49:06 +00002909 ssl->session_negotiate->verify_result = MBEDTLS_X509_BADCERT_MISSING;
Hanno Becker4a55f632019-02-05 12:49:06 +00002910
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002911 if (authmode != MBEDTLS_SSL_VERIFY_OPTIONAL) {
Hanno Becker6bdfab22019-02-05 13:11:17 +00002912 ret = MBEDTLS_ERR_SSL_NO_CLIENT_CERTIFICATE;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002913 }
Hanno Becker4a55f632019-02-05 12:49:06 +00002914
Hanno Becker6bdfab22019-02-05 13:11:17 +00002915 goto exit;
Hanno Becker4a55f632019-02-05 12:49:06 +00002916 }
2917#endif /* MBEDTLS_SSL_SRV_C */
2918
Hanno Beckerc7bd7802019-02-05 15:37:23 +00002919 /* Clear existing peer CRT structure in case we tried to
2920 * reuse a session but it failed, and allocate a new one. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002921 ssl_clear_peer_cert(ssl->session_negotiate);
Hanno Becker3dad3112019-02-05 17:19:52 +00002922
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002923 chain = mbedtls_calloc(1, sizeof(mbedtls_x509_crt));
2924 if (chain == NULL) {
2925 MBEDTLS_SSL_DEBUG_MSG(1, ("alloc(%" MBEDTLS_PRINTF_SIZET " bytes) failed",
2926 sizeof(mbedtls_x509_crt)));
2927 mbedtls_ssl_send_alert_message(ssl,
2928 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
2929 MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR);
Hanno Becker7a955a02019-02-05 13:08:01 +00002930
Hanno Becker3dad3112019-02-05 17:19:52 +00002931 ret = MBEDTLS_ERR_SSL_ALLOC_FAILED;
2932 goto exit;
2933 }
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002934 mbedtls_x509_crt_init(chain);
Hanno Becker3dad3112019-02-05 17:19:52 +00002935
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002936 ret = ssl_parse_certificate_chain(ssl, chain);
2937 if (ret != 0) {
Hanno Becker3dad3112019-02-05 17:19:52 +00002938 goto exit;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002939 }
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02002940
Gilles Peskineeccd8882020-03-10 12:19:08 +01002941#if defined(MBEDTLS_SSL_ECP_RESTARTABLE_ENABLED)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002942 if (ssl->handshake->ecrs_enabled) {
Manuel Pégourié-Gonnard0b23f162017-08-24 12:08:33 +02002943 ssl->handshake->ecrs_state = ssl_ecrs_crt_verify;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002944 }
Manuel Pégourié-Gonnard3bf49c42017-08-15 13:47:06 +02002945
2946crt_verify:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002947 if (ssl->handshake->ecrs_enabled) {
Manuel Pégourié-Gonnard3bf49c42017-08-15 13:47:06 +02002948 rs_ctx = &ssl->handshake->ecrs_ctx;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002949 }
Manuel Pégourié-Gonnard3bf49c42017-08-15 13:47:06 +02002950#endif
2951
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002952 ret = ssl_parse_certificate_verify(ssl, authmode,
2953 chain, rs_ctx);
2954 if (ret != 0) {
Hanno Becker3dad3112019-02-05 17:19:52 +00002955 goto exit;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002956 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002957
Hanno Becker6bbd94c2019-02-05 17:02:28 +00002958#if !defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
Hanno Becker6bbd94c2019-02-05 17:02:28 +00002959 {
Hanno Becker6b8fbab2019-02-08 14:59:05 +00002960 unsigned char *crt_start, *pk_start;
2961 size_t crt_len, pk_len;
Hanno Becker3dad3112019-02-05 17:19:52 +00002962
Hanno Becker6b8fbab2019-02-08 14:59:05 +00002963 /* We parse the CRT chain without copying, so
2964 * these pointers point into the input buffer,
2965 * and are hence still valid after freeing the
2966 * CRT chain. */
Hanno Becker6bbd94c2019-02-05 17:02:28 +00002967
Hanno Becker6b8fbab2019-02-08 14:59:05 +00002968 crt_start = chain->raw.p;
2969 crt_len = chain->raw.len;
Hanno Becker6bbd94c2019-02-05 17:02:28 +00002970
Hanno Becker6b8fbab2019-02-08 14:59:05 +00002971 pk_start = chain->pk_raw.p;
2972 pk_len = chain->pk_raw.len;
2973
2974 /* Free the CRT structures before computing
2975 * digest and copying the peer's public key. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002976 mbedtls_x509_crt_free(chain);
2977 mbedtls_free(chain);
Hanno Becker6b8fbab2019-02-08 14:59:05 +00002978 chain = NULL;
2979
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002980 ret = ssl_remember_peer_crt_digest(ssl, crt_start, crt_len);
2981 if (ret != 0) {
Hanno Beckera2747532019-02-06 16:19:04 +00002982 goto exit;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002983 }
Hanno Becker6b8fbab2019-02-08 14:59:05 +00002984
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002985 ret = ssl_remember_peer_pubkey(ssl, pk_start, pk_len);
2986 if (ret != 0) {
Hanno Becker6b8fbab2019-02-08 14:59:05 +00002987 goto exit;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002988 }
Hanno Beckera2747532019-02-06 16:19:04 +00002989 }
Hanno Becker6b8fbab2019-02-08 14:59:05 +00002990#else /* !MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
2991 /* Pass ownership to session structure. */
Hanno Becker3dad3112019-02-05 17:19:52 +00002992 ssl->session_negotiate->peer_cert = chain;
2993 chain = NULL;
Hanno Becker6b8fbab2019-02-08 14:59:05 +00002994#endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
Hanno Becker3dad3112019-02-05 17:19:52 +00002995
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002996 MBEDTLS_SSL_DEBUG_MSG(2, ("<= parse certificate"));
Paul Bakker5121ce52009-01-03 21:22:43 +00002997
Hanno Becker6bdfab22019-02-05 13:11:17 +00002998exit:
2999
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003000 if (ret == 0) {
Hanno Becker3dad3112019-02-05 17:19:52 +00003001 ssl->state++;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003002 }
Hanno Becker3dad3112019-02-05 17:19:52 +00003003
Gilles Peskineeccd8882020-03-10 12:19:08 +01003004#if defined(MBEDTLS_SSL_ECP_RESTARTABLE_ENABLED)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003005 if (ret == MBEDTLS_ERR_SSL_CRYPTO_IN_PROGRESS) {
Hanno Becker3dad3112019-02-05 17:19:52 +00003006 ssl->handshake->ecrs_peer_cert = chain;
3007 chain = NULL;
3008 }
3009#endif
3010
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003011 if (chain != NULL) {
3012 mbedtls_x509_crt_free(chain);
3013 mbedtls_free(chain);
Hanno Becker3dad3112019-02-05 17:19:52 +00003014 }
3015
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003016 return ret;
Paul Bakker5121ce52009-01-03 21:22:43 +00003017}
Gilles Peskineeccd8882020-03-10 12:19:08 +01003018#endif /* MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED */
Paul Bakker5121ce52009-01-03 21:22:43 +00003019
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003020void mbedtls_ssl_optimize_checksum(mbedtls_ssl_context *ssl,
3021 const mbedtls_ssl_ciphersuite_t *ciphersuite_info)
Paul Bakker380da532012-04-18 16:10:25 +00003022{
Paul Bakkerfb08fd22013-08-27 15:06:26 +02003023 ((void) ciphersuite_info);
Paul Bakker769075d2012-11-24 11:26:46 +01003024
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003025#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1) || \
3026 defined(MBEDTLS_SSL_PROTO_TLS1_1)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003027 if (ssl->minor_ver < MBEDTLS_SSL_MINOR_VERSION_3) {
Paul Bakker48916f92012-09-16 19:57:18 +00003028 ssl->handshake->update_checksum = ssl_update_checksum_md5sha1;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003029 } else
Paul Bakkerd2f068e2013-08-27 21:19:20 +02003030#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003031#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
Gilles Peskined2d59372021-05-12 22:43:27 +02003032#if defined(MBEDTLS_SHA512_C) && !defined(MBEDTLS_SHA512_NO_SHA384)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003033 if (ciphersuite_info->mac == MBEDTLS_MD_SHA384) {
Paul Bakkerd2f068e2013-08-27 21:19:20 +02003034 ssl->handshake->update_checksum = ssl_update_checksum_sha384;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003035 } else
Paul Bakkerd2f068e2013-08-27 21:19:20 +02003036#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003037#if defined(MBEDTLS_SHA256_C)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003038 if (ciphersuite_info->mac != MBEDTLS_MD_SHA384) {
Paul Bakker48916f92012-09-16 19:57:18 +00003039 ssl->handshake->update_checksum = ssl_update_checksum_sha256;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003040 } else
Paul Bakkerd2f068e2013-08-27 21:19:20 +02003041#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003042#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Manuel Pégourié-Gonnard61edffe2014-04-11 17:07:31 +02003043 {
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003044 MBEDTLS_SSL_DEBUG_MSG(1, ("should never happen"));
Paul Bakkerd2f068e2013-08-27 21:19:20 +02003045 return;
Manuel Pégourié-Gonnard61edffe2014-04-11 17:07:31 +02003046 }
Paul Bakker380da532012-04-18 16:10:25 +00003047}
Paul Bakkerf7abd422013-04-16 13:15:56 +02003048
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003049void mbedtls_ssl_reset_checksum(mbedtls_ssl_context *ssl)
Manuel Pégourié-Gonnard67427c02014-07-11 13:45:34 +02003050{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003051#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1) || \
3052 defined(MBEDTLS_SSL_PROTO_TLS1_1)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003053 mbedtls_md5_starts_ret(&ssl->handshake->fin_md5);
3054 mbedtls_sha1_starts_ret(&ssl->handshake->fin_sha1);
Manuel Pégourié-Gonnard67427c02014-07-11 13:45:34 +02003055#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003056#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
3057#if defined(MBEDTLS_SHA256_C)
Andrzej Kurekeb342242019-01-29 09:14:33 -05003058#if defined(MBEDTLS_USE_PSA_CRYPTO)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003059 psa_hash_abort(&ssl->handshake->fin_sha256_psa);
3060 psa_hash_setup(&ssl->handshake->fin_sha256_psa, PSA_ALG_SHA_256);
Andrzej Kurekeb342242019-01-29 09:14:33 -05003061#else
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003062 mbedtls_sha256_starts_ret(&ssl->handshake->fin_sha256, 0);
Manuel Pégourié-Gonnard67427c02014-07-11 13:45:34 +02003063#endif
Andrzej Kurekeb342242019-01-29 09:14:33 -05003064#endif
Gilles Peskined2d59372021-05-12 22:43:27 +02003065#if defined(MBEDTLS_SHA512_C) && !defined(MBEDTLS_SHA512_NO_SHA384)
Andrzej Kurekeb342242019-01-29 09:14:33 -05003066#if defined(MBEDTLS_USE_PSA_CRYPTO)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003067 psa_hash_abort(&ssl->handshake->fin_sha384_psa);
3068 psa_hash_setup(&ssl->handshake->fin_sha384_psa, PSA_ALG_SHA_384);
Andrzej Kurekeb342242019-01-29 09:14:33 -05003069#else
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003070 mbedtls_sha512_starts_ret(&ssl->handshake->fin_sha512, 1);
Manuel Pégourié-Gonnard67427c02014-07-11 13:45:34 +02003071#endif
Andrzej Kurekeb342242019-01-29 09:14:33 -05003072#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003073#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Manuel Pégourié-Gonnard67427c02014-07-11 13:45:34 +02003074}
3075
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003076static void ssl_update_checksum_start(mbedtls_ssl_context *ssl,
3077 const unsigned char *buf, size_t len)
Paul Bakker380da532012-04-18 16:10:25 +00003078{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003079#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1) || \
3080 defined(MBEDTLS_SSL_PROTO_TLS1_1)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003081 mbedtls_md5_update_ret(&ssl->handshake->fin_md5, buf, len);
3082 mbedtls_sha1_update_ret(&ssl->handshake->fin_sha1, buf, len);
Paul Bakkerd2f068e2013-08-27 21:19:20 +02003083#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003084#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
3085#if defined(MBEDTLS_SHA256_C)
Andrzej Kurekeb342242019-01-29 09:14:33 -05003086#if defined(MBEDTLS_USE_PSA_CRYPTO)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003087 psa_hash_update(&ssl->handshake->fin_sha256_psa, buf, len);
Andrzej Kurekeb342242019-01-29 09:14:33 -05003088#else
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003089 mbedtls_sha256_update_ret(&ssl->handshake->fin_sha256, buf, len);
Paul Bakkerd2f068e2013-08-27 21:19:20 +02003090#endif
Andrzej Kurekeb342242019-01-29 09:14:33 -05003091#endif
Gilles Peskined2d59372021-05-12 22:43:27 +02003092#if defined(MBEDTLS_SHA512_C) && !defined(MBEDTLS_SHA512_NO_SHA384)
Andrzej Kurekeb342242019-01-29 09:14:33 -05003093#if defined(MBEDTLS_USE_PSA_CRYPTO)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003094 psa_hash_update(&ssl->handshake->fin_sha384_psa, buf, len);
Andrzej Kurekeb342242019-01-29 09:14:33 -05003095#else
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003096 mbedtls_sha512_update_ret(&ssl->handshake->fin_sha512, buf, len);
Paul Bakker769075d2012-11-24 11:26:46 +01003097#endif
Andrzej Kurekeb342242019-01-29 09:14:33 -05003098#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003099#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakker380da532012-04-18 16:10:25 +00003100}
3101
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003102#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1) || \
3103 defined(MBEDTLS_SSL_PROTO_TLS1_1)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003104static void ssl_update_checksum_md5sha1(mbedtls_ssl_context *ssl,
3105 const unsigned char *buf, size_t len)
Paul Bakker380da532012-04-18 16:10:25 +00003106{
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003107 mbedtls_md5_update_ret(&ssl->handshake->fin_md5, buf, len);
3108 mbedtls_sha1_update_ret(&ssl->handshake->fin_sha1, buf, len);
Paul Bakker380da532012-04-18 16:10:25 +00003109}
Paul Bakkerd2f068e2013-08-27 21:19:20 +02003110#endif
Paul Bakker380da532012-04-18 16:10:25 +00003111
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003112#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
3113#if defined(MBEDTLS_SHA256_C)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003114static void ssl_update_checksum_sha256(mbedtls_ssl_context *ssl,
3115 const unsigned char *buf, size_t len)
Paul Bakker380da532012-04-18 16:10:25 +00003116{
Andrzej Kurekeb342242019-01-29 09:14:33 -05003117#if defined(MBEDTLS_USE_PSA_CRYPTO)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003118 psa_hash_update(&ssl->handshake->fin_sha256_psa, buf, len);
Andrzej Kurekeb342242019-01-29 09:14:33 -05003119#else
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003120 mbedtls_sha256_update_ret(&ssl->handshake->fin_sha256, buf, len);
Andrzej Kurekeb342242019-01-29 09:14:33 -05003121#endif
Paul Bakker380da532012-04-18 16:10:25 +00003122}
Paul Bakkerd2f068e2013-08-27 21:19:20 +02003123#endif
Paul Bakker380da532012-04-18 16:10:25 +00003124
Gilles Peskined2d59372021-05-12 22:43:27 +02003125#if defined(MBEDTLS_SHA512_C) && !defined(MBEDTLS_SHA512_NO_SHA384)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003126static void ssl_update_checksum_sha384(mbedtls_ssl_context *ssl,
3127 const unsigned char *buf, size_t len)
Paul Bakker380da532012-04-18 16:10:25 +00003128{
Andrzej Kurekeb342242019-01-29 09:14:33 -05003129#if defined(MBEDTLS_USE_PSA_CRYPTO)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003130 psa_hash_update(&ssl->handshake->fin_sha384_psa, buf, len);
Andrzej Kurekeb342242019-01-29 09:14:33 -05003131#else
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003132 mbedtls_sha512_update_ret(&ssl->handshake->fin_sha512, buf, len);
Andrzej Kurekeb342242019-01-29 09:14:33 -05003133#endif
Paul Bakker380da532012-04-18 16:10:25 +00003134}
Paul Bakker769075d2012-11-24 11:26:46 +01003135#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003136#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakker380da532012-04-18 16:10:25 +00003137
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003138#if defined(MBEDTLS_SSL_PROTO_SSL3)
David Horstmann68014b22025-03-10 14:10:11 +00003139static int ssl_calc_finished_ssl(
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003140 mbedtls_ssl_context *ssl, unsigned char *buf, int from)
Paul Bakker5121ce52009-01-03 21:22:43 +00003141{
Paul Bakker3c2122f2013-06-24 19:03:14 +02003142 const char *sender;
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02003143 mbedtls_md5_context md5;
3144 mbedtls_sha1_context sha1;
Paul Bakker1ef83d62012-04-11 12:09:53 +00003145
Paul Bakker5121ce52009-01-03 21:22:43 +00003146 unsigned char padbuf[48];
3147 unsigned char md5sum[16];
3148 unsigned char sha1sum[20];
3149
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003150 mbedtls_ssl_session *session = ssl->session_negotiate;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003151 if (!session) {
Paul Bakker48916f92012-09-16 19:57:18 +00003152 session = ssl->session;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003153 }
Paul Bakker48916f92012-09-16 19:57:18 +00003154
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003155 MBEDTLS_SSL_DEBUG_MSG(2, ("=> calc finished ssl"));
Paul Bakker1ef83d62012-04-11 12:09:53 +00003156
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003157 mbedtls_md5_init(&md5);
3158 mbedtls_sha1_init(&sha1);
Manuel Pégourié-Gonnard001f2b62015-07-06 16:21:13 +02003159
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003160 mbedtls_md5_clone(&md5, &ssl->handshake->fin_md5);
3161 mbedtls_sha1_clone(&sha1, &ssl->handshake->fin_sha1);
Paul Bakker5121ce52009-01-03 21:22:43 +00003162
3163 /*
3164 * SSLv3:
3165 * hash =
3166 * MD5( master + pad2 +
3167 * MD5( handshake + sender + master + pad1 ) )
3168 * + SHA1( master + pad2 +
3169 * SHA1( handshake + sender + master + pad1 ) )
Paul Bakker5121ce52009-01-03 21:22:43 +00003170 */
3171
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003172#if !defined(MBEDTLS_MD5_ALT)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003173 MBEDTLS_SSL_DEBUG_BUF(4, "finished md5 state", (unsigned char *)
3174 md5.state, sizeof(md5.state));
Paul Bakker90995b52013-06-24 19:20:35 +02003175#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00003176
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003177#if !defined(MBEDTLS_SHA1_ALT)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003178 MBEDTLS_SSL_DEBUG_BUF(4, "finished sha1 state", (unsigned char *)
3179 sha1.state, sizeof(sha1.state));
Paul Bakker90995b52013-06-24 19:20:35 +02003180#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00003181
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003182 sender = (from == MBEDTLS_SSL_IS_CLIENT) ? "CLNT"
Paul Bakker3c2122f2013-06-24 19:03:14 +02003183 : "SRVR";
Paul Bakker5121ce52009-01-03 21:22:43 +00003184
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003185 memset(padbuf, 0x36, 48);
Paul Bakker5121ce52009-01-03 21:22:43 +00003186
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003187 mbedtls_md5_update_ret(&md5, (const unsigned char *) sender, 4);
3188 mbedtls_md5_update_ret(&md5, session->master, 48);
3189 mbedtls_md5_update_ret(&md5, padbuf, 48);
3190 mbedtls_md5_finish_ret(&md5, md5sum);
Paul Bakker5121ce52009-01-03 21:22:43 +00003191
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003192 mbedtls_sha1_update_ret(&sha1, (const unsigned char *) sender, 4);
3193 mbedtls_sha1_update_ret(&sha1, session->master, 48);
3194 mbedtls_sha1_update_ret(&sha1, padbuf, 40);
3195 mbedtls_sha1_finish_ret(&sha1, sha1sum);
Paul Bakker5121ce52009-01-03 21:22:43 +00003196
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003197 memset(padbuf, 0x5C, 48);
Paul Bakker5121ce52009-01-03 21:22:43 +00003198
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003199 mbedtls_md5_starts_ret(&md5);
3200 mbedtls_md5_update_ret(&md5, session->master, 48);
3201 mbedtls_md5_update_ret(&md5, padbuf, 48);
3202 mbedtls_md5_update_ret(&md5, md5sum, 16);
3203 mbedtls_md5_finish_ret(&md5, buf);
Paul Bakker5121ce52009-01-03 21:22:43 +00003204
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003205 mbedtls_sha1_starts_ret(&sha1);
3206 mbedtls_sha1_update_ret(&sha1, session->master, 48);
3207 mbedtls_sha1_update_ret(&sha1, padbuf, 40);
3208 mbedtls_sha1_update_ret(&sha1, sha1sum, 20);
3209 mbedtls_sha1_finish_ret(&sha1, buf + 16);
Paul Bakker5121ce52009-01-03 21:22:43 +00003210
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003211 MBEDTLS_SSL_DEBUG_BUF(3, "calc finished result", buf, 36);
Paul Bakker5121ce52009-01-03 21:22:43 +00003212
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003213 mbedtls_md5_free(&md5);
3214 mbedtls_sha1_free(&sha1);
Paul Bakker5121ce52009-01-03 21:22:43 +00003215
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003216 mbedtls_platform_zeroize(padbuf, sizeof(padbuf));
3217 mbedtls_platform_zeroize(md5sum, sizeof(md5sum));
3218 mbedtls_platform_zeroize(sha1sum, sizeof(sha1sum));
Paul Bakker5121ce52009-01-03 21:22:43 +00003219
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003220 MBEDTLS_SSL_DEBUG_MSG(2, ("<= calc finished"));
David Horstmann68014b22025-03-10 14:10:11 +00003221
3222 return 0;
Paul Bakker5121ce52009-01-03 21:22:43 +00003223}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003224#endif /* MBEDTLS_SSL_PROTO_SSL3 */
Paul Bakker5121ce52009-01-03 21:22:43 +00003225
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003226#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1)
David Horstmann68014b22025-03-10 14:10:11 +00003227static int ssl_calc_finished_tls(
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003228 mbedtls_ssl_context *ssl, unsigned char *buf, int from)
Paul Bakker5121ce52009-01-03 21:22:43 +00003229{
Paul Bakker1ef83d62012-04-11 12:09:53 +00003230 int len = 12;
Paul Bakker3c2122f2013-06-24 19:03:14 +02003231 const char *sender;
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02003232 mbedtls_md5_context md5;
3233 mbedtls_sha1_context sha1;
Paul Bakker1ef83d62012-04-11 12:09:53 +00003234 unsigned char padbuf[36];
Paul Bakker5121ce52009-01-03 21:22:43 +00003235
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003236 mbedtls_ssl_session *session = ssl->session_negotiate;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003237 if (!session) {
Paul Bakker48916f92012-09-16 19:57:18 +00003238 session = ssl->session;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003239 }
Paul Bakker48916f92012-09-16 19:57:18 +00003240
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003241 MBEDTLS_SSL_DEBUG_MSG(2, ("=> calc finished tls"));
Paul Bakker5121ce52009-01-03 21:22:43 +00003242
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003243 mbedtls_md5_init(&md5);
3244 mbedtls_sha1_init(&sha1);
Manuel Pégourié-Gonnard001f2b62015-07-06 16:21:13 +02003245
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003246 mbedtls_md5_clone(&md5, &ssl->handshake->fin_md5);
3247 mbedtls_sha1_clone(&sha1, &ssl->handshake->fin_sha1);
Paul Bakker5121ce52009-01-03 21:22:43 +00003248
Paul Bakker1ef83d62012-04-11 12:09:53 +00003249 /*
3250 * TLSv1:
3251 * hash = PRF( master, finished_label,
3252 * MD5( handshake ) + SHA1( handshake ) )[0..11]
3253 */
Paul Bakker5121ce52009-01-03 21:22:43 +00003254
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003255#if !defined(MBEDTLS_MD5_ALT)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003256 MBEDTLS_SSL_DEBUG_BUF(4, "finished md5 state", (unsigned char *)
3257 md5.state, sizeof(md5.state));
Paul Bakker90995b52013-06-24 19:20:35 +02003258#endif
Paul Bakker1ef83d62012-04-11 12:09:53 +00003259
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003260#if !defined(MBEDTLS_SHA1_ALT)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003261 MBEDTLS_SSL_DEBUG_BUF(4, "finished sha1 state", (unsigned char *)
3262 sha1.state, sizeof(sha1.state));
Paul Bakker90995b52013-06-24 19:20:35 +02003263#endif
Paul Bakker1ef83d62012-04-11 12:09:53 +00003264
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003265 sender = (from == MBEDTLS_SSL_IS_CLIENT)
Paul Bakker3c2122f2013-06-24 19:03:14 +02003266 ? "client finished"
3267 : "server finished";
Paul Bakker1ef83d62012-04-11 12:09:53 +00003268
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003269 mbedtls_md5_finish_ret(&md5, padbuf);
3270 mbedtls_sha1_finish_ret(&sha1, padbuf + 16);
Paul Bakker1ef83d62012-04-11 12:09:53 +00003271
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003272 ssl->handshake->tls_prf(session->master, 48, sender,
3273 padbuf, 36, buf, len);
Paul Bakker1ef83d62012-04-11 12:09:53 +00003274
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003275 MBEDTLS_SSL_DEBUG_BUF(3, "calc finished result", buf, len);
Paul Bakker1ef83d62012-04-11 12:09:53 +00003276
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003277 mbedtls_md5_free(&md5);
3278 mbedtls_sha1_free(&sha1);
Paul Bakker1ef83d62012-04-11 12:09:53 +00003279
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003280 mbedtls_platform_zeroize(padbuf, sizeof(padbuf));
Paul Bakker1ef83d62012-04-11 12:09:53 +00003281
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003282 MBEDTLS_SSL_DEBUG_MSG(2, ("<= calc finished"));
David Horstmann68014b22025-03-10 14:10:11 +00003283
3284 return 0;
Paul Bakker1ef83d62012-04-11 12:09:53 +00003285}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003286#endif /* MBEDTLS_SSL_PROTO_TLS1 || MBEDTLS_SSL_PROTO_TLS1_1 */
Paul Bakker1ef83d62012-04-11 12:09:53 +00003287
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003288#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
3289#if defined(MBEDTLS_SHA256_C)
David Horstmann68014b22025-03-10 14:10:11 +00003290static int ssl_calc_finished_tls_sha256(
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003291 mbedtls_ssl_context *ssl, unsigned char *buf, int from)
Paul Bakker1ef83d62012-04-11 12:09:53 +00003292{
3293 int len = 12;
Paul Bakker3c2122f2013-06-24 19:03:14 +02003294 const char *sender;
Paul Bakker1ef83d62012-04-11 12:09:53 +00003295 unsigned char padbuf[32];
Andrzej Kurekeb342242019-01-29 09:14:33 -05003296#if defined(MBEDTLS_USE_PSA_CRYPTO)
3297 size_t hash_size;
Jaeden Amero34973232019-02-20 10:32:28 +00003298 psa_hash_operation_t sha256_psa = PSA_HASH_OPERATION_INIT;
Andrzej Kurekeb342242019-01-29 09:14:33 -05003299 psa_status_t status;
3300#else
3301 mbedtls_sha256_context sha256;
3302#endif
Paul Bakker1ef83d62012-04-11 12:09:53 +00003303
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003304 mbedtls_ssl_session *session = ssl->session_negotiate;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003305 if (!session) {
Paul Bakker48916f92012-09-16 19:57:18 +00003306 session = ssl->session;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003307 }
Paul Bakker48916f92012-09-16 19:57:18 +00003308
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003309 sender = (from == MBEDTLS_SSL_IS_CLIENT)
Andrzej Kurekeb342242019-01-29 09:14:33 -05003310 ? "client finished"
3311 : "server finished";
3312
3313#if defined(MBEDTLS_USE_PSA_CRYPTO)
3314 sha256_psa = psa_hash_operation_init();
3315
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003316 MBEDTLS_SSL_DEBUG_MSG(2, ("=> calc PSA finished tls sha256"));
Andrzej Kurekeb342242019-01-29 09:14:33 -05003317
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003318 status = psa_hash_clone(&ssl->handshake->fin_sha256_psa, &sha256_psa);
3319 if (status != PSA_SUCCESS) {
3320 MBEDTLS_SSL_DEBUG_MSG(2, ("PSA hash clone failed"));
David Horstmann68014b22025-03-10 14:10:11 +00003321 return status;
Andrzej Kurekeb342242019-01-29 09:14:33 -05003322 }
3323
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003324 status = psa_hash_finish(&sha256_psa, padbuf, sizeof(padbuf), &hash_size);
3325 if (status != PSA_SUCCESS) {
3326 MBEDTLS_SSL_DEBUG_MSG(2, ("PSA hash finish failed"));
David Horstmann68014b22025-03-10 14:10:11 +00003327 return status;
Andrzej Kurekeb342242019-01-29 09:14:33 -05003328 }
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003329 MBEDTLS_SSL_DEBUG_BUF(3, "PSA calculated padbuf", padbuf, 32);
Andrzej Kurekeb342242019-01-29 09:14:33 -05003330#else
3331
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003332 mbedtls_sha256_init(&sha256);
Manuel Pégourié-Gonnard001f2b62015-07-06 16:21:13 +02003333
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003334 MBEDTLS_SSL_DEBUG_MSG(2, ("=> calc finished tls sha256"));
Paul Bakker1ef83d62012-04-11 12:09:53 +00003335
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003336 mbedtls_sha256_clone(&sha256, &ssl->handshake->fin_sha256);
Paul Bakker1ef83d62012-04-11 12:09:53 +00003337
3338 /*
3339 * TLSv1.2:
3340 * hash = PRF( master, finished_label,
3341 * Hash( handshake ) )[0.11]
3342 */
3343
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003344#if !defined(MBEDTLS_SHA256_ALT)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003345 MBEDTLS_SSL_DEBUG_BUF(4, "finished sha2 state", (unsigned char *)
3346 sha256.state, sizeof(sha256.state));
Paul Bakker90995b52013-06-24 19:20:35 +02003347#endif
Paul Bakker1ef83d62012-04-11 12:09:53 +00003348
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003349 mbedtls_sha256_finish_ret(&sha256, padbuf);
3350 mbedtls_sha256_free(&sha256);
Andrzej Kurekeb342242019-01-29 09:14:33 -05003351#endif /* MBEDTLS_USE_PSA_CRYPTO */
Paul Bakker1ef83d62012-04-11 12:09:53 +00003352
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003353 ssl->handshake->tls_prf(session->master, 48, sender,
3354 padbuf, 32, buf, len);
Paul Bakker1ef83d62012-04-11 12:09:53 +00003355
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003356 MBEDTLS_SSL_DEBUG_BUF(3, "calc finished result", buf, len);
Paul Bakker1ef83d62012-04-11 12:09:53 +00003357
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003358 mbedtls_platform_zeroize(padbuf, sizeof(padbuf));
Paul Bakker1ef83d62012-04-11 12:09:53 +00003359
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003360 MBEDTLS_SSL_DEBUG_MSG(2, ("<= calc finished"));
David Horstmann68014b22025-03-10 14:10:11 +00003361
3362 return 0;
Paul Bakker1ef83d62012-04-11 12:09:53 +00003363}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003364#endif /* MBEDTLS_SHA256_C */
Paul Bakker1ef83d62012-04-11 12:09:53 +00003365
Gilles Peskined2d59372021-05-12 22:43:27 +02003366#if defined(MBEDTLS_SHA512_C) && !defined(MBEDTLS_SHA512_NO_SHA384)
Rodrigo Dias Corread596ca82020-11-25 00:42:28 -03003367
David Horstmann68014b22025-03-10 14:10:11 +00003368static int ssl_calc_finished_tls_sha384(
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003369 mbedtls_ssl_context *ssl, unsigned char *buf, int from)
Paul Bakkerca4ab492012-04-18 14:23:57 +00003370{
3371 int len = 12;
Paul Bakker3c2122f2013-06-24 19:03:14 +02003372 const char *sender;
Rodrigo Dias Corread596ca82020-11-25 00:42:28 -03003373 unsigned char padbuf[48];
Andrzej Kurekeb342242019-01-29 09:14:33 -05003374#if defined(MBEDTLS_USE_PSA_CRYPTO)
3375 size_t hash_size;
Jaeden Amero34973232019-02-20 10:32:28 +00003376 psa_hash_operation_t sha384_psa = PSA_HASH_OPERATION_INIT;
Andrzej Kurekeb342242019-01-29 09:14:33 -05003377 psa_status_t status;
3378#else
3379 mbedtls_sha512_context sha512;
3380#endif
Paul Bakkerca4ab492012-04-18 14:23:57 +00003381
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003382 mbedtls_ssl_session *session = ssl->session_negotiate;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003383 if (!session) {
Paul Bakker48916f92012-09-16 19:57:18 +00003384 session = ssl->session;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003385 }
Paul Bakker48916f92012-09-16 19:57:18 +00003386
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003387 sender = (from == MBEDTLS_SSL_IS_CLIENT)
Andrzej Kurekeb342242019-01-29 09:14:33 -05003388 ? "client finished"
3389 : "server finished";
3390
3391#if defined(MBEDTLS_USE_PSA_CRYPTO)
Andrzej Kurek972fba52019-01-30 03:29:12 -05003392 sha384_psa = psa_hash_operation_init();
Andrzej Kurekeb342242019-01-29 09:14:33 -05003393
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003394 MBEDTLS_SSL_DEBUG_MSG(2, ("=> calc PSA finished tls sha384"));
Andrzej Kurekeb342242019-01-29 09:14:33 -05003395
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003396 status = psa_hash_clone(&ssl->handshake->fin_sha384_psa, &sha384_psa);
3397 if (status != PSA_SUCCESS) {
3398 MBEDTLS_SSL_DEBUG_MSG(2, ("PSA hash clone failed"));
David Horstmann68014b22025-03-10 14:10:11 +00003399 return status;
Andrzej Kurekeb342242019-01-29 09:14:33 -05003400 }
3401
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003402 status = psa_hash_finish(&sha384_psa, padbuf, sizeof(padbuf), &hash_size);
3403 if (status != PSA_SUCCESS) {
3404 MBEDTLS_SSL_DEBUG_MSG(2, ("PSA hash finish failed"));
David Horstmann68014b22025-03-10 14:10:11 +00003405 return status;
Andrzej Kurekeb342242019-01-29 09:14:33 -05003406 }
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003407 MBEDTLS_SSL_DEBUG_BUF(3, "PSA calculated padbuf", padbuf, 48);
Andrzej Kurekeb342242019-01-29 09:14:33 -05003408#else
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003409 mbedtls_sha512_init(&sha512);
Manuel Pégourié-Gonnard001f2b62015-07-06 16:21:13 +02003410
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003411 MBEDTLS_SSL_DEBUG_MSG(2, ("=> calc finished tls sha384"));
Paul Bakkerca4ab492012-04-18 14:23:57 +00003412
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003413 mbedtls_sha512_clone(&sha512, &ssl->handshake->fin_sha512);
Paul Bakkerca4ab492012-04-18 14:23:57 +00003414
3415 /*
3416 * TLSv1.2:
3417 * hash = PRF( master, finished_label,
3418 * Hash( handshake ) )[0.11]
3419 */
3420
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003421#if !defined(MBEDTLS_SHA512_ALT)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003422 MBEDTLS_SSL_DEBUG_BUF(4, "finished sha512 state", (unsigned char *)
3423 sha512.state, sizeof(sha512.state));
Paul Bakker90995b52013-06-24 19:20:35 +02003424#endif
Gilles Peskinebb66dac2021-05-13 00:00:45 +02003425 /* mbedtls_sha512_finish_ret's output parameter is declared as a
Tom Cosgrove49f99bc2022-12-04 16:44:21 +00003426 * 64-byte buffer, but since we're using SHA-384, we know that the
Gilles Peskinebb66dac2021-05-13 00:00:45 +02003427 * output fits in 48 bytes. This is correct C, but GCC 11.1 warns
3428 * about it.
Rodrigo Dias Corread596ca82020-11-25 00:42:28 -03003429 */
Gilles Peskinebb66dac2021-05-13 00:00:45 +02003430#if defined(__GNUC__) && __GNUC__ >= 11
3431#pragma GCC diagnostic push
3432#pragma GCC diagnostic ignored "-Wstringop-overflow"
3433#endif
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003434 mbedtls_sha512_finish_ret(&sha512, padbuf);
Gilles Peskinebb66dac2021-05-13 00:00:45 +02003435#if defined(__GNUC__) && __GNUC__ >= 11
3436#pragma GCC diagnostic pop
3437#endif
Paul Bakkerca4ab492012-04-18 14:23:57 +00003438
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003439 mbedtls_sha512_free(&sha512);
Andrzej Kurekeb342242019-01-29 09:14:33 -05003440#endif
Paul Bakkerca4ab492012-04-18 14:23:57 +00003441
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003442 ssl->handshake->tls_prf(session->master, 48, sender,
3443 padbuf, 48, buf, len);
Paul Bakkerca4ab492012-04-18 14:23:57 +00003444
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003445 MBEDTLS_SSL_DEBUG_BUF(3, "calc finished result", buf, len);
Paul Bakkerca4ab492012-04-18 14:23:57 +00003446
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003447 mbedtls_platform_zeroize(padbuf, sizeof(padbuf));
Paul Bakkerca4ab492012-04-18 14:23:57 +00003448
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003449 MBEDTLS_SSL_DEBUG_MSG(2, ("<= calc finished"));
David Horstmann68014b22025-03-10 14:10:11 +00003450
3451 return 0;
Paul Bakkerca4ab492012-04-18 14:23:57 +00003452}
Gilles Peskined2d59372021-05-12 22:43:27 +02003453#endif /* MBEDTLS_SHA512_C && !MBEDTLS_SHA512_NO_SHA384 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003454#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakkerca4ab492012-04-18 14:23:57 +00003455
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003456void mbedtls_ssl_handshake_wrapup_free_hs_transform(mbedtls_ssl_context *ssl)
Paul Bakker48916f92012-09-16 19:57:18 +00003457{
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003458 MBEDTLS_SSL_DEBUG_MSG(3, ("=> handshake wrapup: final free"));
Paul Bakker48916f92012-09-16 19:57:18 +00003459
3460 /*
3461 * Free our handshake params
3462 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003463 mbedtls_ssl_handshake_free(ssl);
3464 mbedtls_free(ssl->handshake);
Paul Bakker48916f92012-09-16 19:57:18 +00003465 ssl->handshake = NULL;
3466
3467 /*
Shaun Case0e7791f2021-12-20 21:14:10 -08003468 * Free the previous transform and switch in the current one
Paul Bakker48916f92012-09-16 19:57:18 +00003469 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003470 if (ssl->transform) {
3471 mbedtls_ssl_transform_free(ssl->transform);
3472 mbedtls_free(ssl->transform);
Paul Bakker48916f92012-09-16 19:57:18 +00003473 }
3474 ssl->transform = ssl->transform_negotiate;
3475 ssl->transform_negotiate = NULL;
3476
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003477 MBEDTLS_SSL_DEBUG_MSG(3, ("<= handshake wrapup: final free"));
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02003478}
3479
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003480void mbedtls_ssl_handshake_wrapup(mbedtls_ssl_context *ssl)
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02003481{
3482 int resume = ssl->handshake->resume;
3483
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003484 MBEDTLS_SSL_DEBUG_MSG(3, ("=> handshake wrapup"));
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02003485
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003486#if defined(MBEDTLS_SSL_RENEGOTIATION)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003487 if (ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS) {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003488 ssl->renego_status = MBEDTLS_SSL_RENEGOTIATION_DONE;
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02003489 ssl->renego_records_seen = 0;
3490 }
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01003491#endif
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02003492
3493 /*
3494 * Free the previous session and switch in the current one
3495 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003496 if (ssl->session) {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003497#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
Manuel Pégourié-Gonnard1a034732014-11-04 17:36:18 +01003498 /* RFC 7366 3.1: keep the EtM state */
3499 ssl->session_negotiate->encrypt_then_mac =
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003500 ssl->session->encrypt_then_mac;
Manuel Pégourié-Gonnard1a034732014-11-04 17:36:18 +01003501#endif
3502
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003503 mbedtls_ssl_session_free(ssl->session);
3504 mbedtls_free(ssl->session);
Paul Bakker0a597072012-09-25 21:55:46 +00003505 }
3506 ssl->session = ssl->session_negotiate;
Paul Bakker48916f92012-09-16 19:57:18 +00003507 ssl->session_negotiate = NULL;
3508
Paul Bakker0a597072012-09-25 21:55:46 +00003509 /*
3510 * Add cache entry
3511 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003512 if (ssl->conf->f_set_cache != NULL &&
Manuel Pégourié-Gonnard12ad7982015-06-18 15:50:37 +02003513 ssl->session->id_len != 0 &&
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003514 resume == 0) {
3515 if (ssl->conf->f_set_cache(ssl->conf->p_cache, ssl->session) != 0) {
3516 MBEDTLS_SSL_DEBUG_MSG(1, ("cache did not store session"));
3517 }
Manuel Pégourié-Gonnardc086cce2013-08-02 14:13:02 +02003518 }
Paul Bakker0a597072012-09-25 21:55:46 +00003519
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003520#if defined(MBEDTLS_SSL_PROTO_DTLS)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003521 if (ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
3522 ssl->handshake->flight != NULL) {
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02003523 /* Cancel handshake timer */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003524 mbedtls_ssl_set_timer(ssl, 0);
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02003525
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02003526 /* Keep last flight around in case we need to resend it:
3527 * we need the handshake and transform structures for that */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003528 MBEDTLS_SSL_DEBUG_MSG(3, ("skip freeing handshake and transform"));
3529 } else
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02003530#endif
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003531 mbedtls_ssl_handshake_wrapup_free_hs_transform(ssl);
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02003532
Paul Bakker48916f92012-09-16 19:57:18 +00003533 ssl->state++;
3534
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003535 MBEDTLS_SSL_DEBUG_MSG(3, ("<= handshake wrapup"));
Paul Bakker48916f92012-09-16 19:57:18 +00003536}
3537
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003538int mbedtls_ssl_write_finished(mbedtls_ssl_context *ssl)
Paul Bakker1ef83d62012-04-11 12:09:53 +00003539{
Manuel Pégourié-Gonnard879a4f92014-07-11 22:31:12 +02003540 int ret, hash_len;
Paul Bakker1ef83d62012-04-11 12:09:53 +00003541
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003542 MBEDTLS_SSL_DEBUG_MSG(2, ("=> write finished"));
Paul Bakker1ef83d62012-04-11 12:09:53 +00003543
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003544 mbedtls_ssl_update_out_pointers(ssl, ssl->transform_negotiate);
Paul Bakker92be97b2013-01-02 17:30:03 +01003545
David Horstmann68014b22025-03-10 14:10:11 +00003546 ret = ssl->handshake->calc_finished(ssl, ssl->out_msg + 4,
3547 ssl->conf->endpoint);
3548 if (ret != 0) {
3549 MBEDTLS_SSL_DEBUG_RET(1, "calc_finished", ret);
3550 return ret;
3551 }
Paul Bakker1ef83d62012-04-11 12:09:53 +00003552
Manuel Pégourié-Gonnard214a8482016-02-22 11:27:26 +01003553 /*
3554 * RFC 5246 7.4.9 (Page 63) says 12 is the default length and ciphersuites
3555 * may define some other value. Currently (early 2016), no defined
3556 * ciphersuite does this (and this is unlikely to change as activity has
3557 * moved to TLS 1.3 now) so we can keep the hardcoded 12 here.
3558 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003559 hash_len = (ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0) ? 36 : 12;
Paul Bakker5121ce52009-01-03 21:22:43 +00003560
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003561#if defined(MBEDTLS_SSL_RENEGOTIATION)
Paul Bakker48916f92012-09-16 19:57:18 +00003562 ssl->verify_data_len = hash_len;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003563 memcpy(ssl->own_verify_data, ssl->out_msg + 4, hash_len);
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01003564#endif
Paul Bakker48916f92012-09-16 19:57:18 +00003565
Paul Bakker5121ce52009-01-03 21:22:43 +00003566 ssl->out_msglen = 4 + hash_len;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003567 ssl->out_msgtype = MBEDTLS_SSL_MSG_HANDSHAKE;
3568 ssl->out_msg[0] = MBEDTLS_SSL_HS_FINISHED;
Paul Bakker5121ce52009-01-03 21:22:43 +00003569
3570 /*
3571 * In case of session resuming, invert the client and server
3572 * ChangeCipherSpec messages order.
3573 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003574 if (ssl->handshake->resume != 0) {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003575#if defined(MBEDTLS_SSL_CLI_C)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003576 if (ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT) {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003577 ssl->state = MBEDTLS_SSL_HANDSHAKE_WRAPUP;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003578 }
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01003579#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003580#if defined(MBEDTLS_SSL_SRV_C)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003581 if (ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER) {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003582 ssl->state = MBEDTLS_SSL_CLIENT_CHANGE_CIPHER_SPEC;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003583 }
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01003584#endif
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003585 } else {
Paul Bakker5121ce52009-01-03 21:22:43 +00003586 ssl->state++;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003587 }
Paul Bakker5121ce52009-01-03 21:22:43 +00003588
Paul Bakker48916f92012-09-16 19:57:18 +00003589 /*
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02003590 * Switch to our negotiated transform and session parameters for outbound
3591 * data.
Paul Bakker48916f92012-09-16 19:57:18 +00003592 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003593 MBEDTLS_SSL_DEBUG_MSG(3, ("switching to new transform spec for outbound data"));
Manuel Pégourié-Gonnard5afb1672014-02-16 18:33:22 +01003594
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003595#if defined(MBEDTLS_SSL_PROTO_DTLS)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003596 if (ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM) {
Manuel Pégourié-Gonnard879a4f92014-07-11 22:31:12 +02003597 unsigned char i;
3598
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003599 /* Remember current epoch settings for resending */
3600 ssl->handshake->alt_transform_out = ssl->transform_out;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003601 memcpy(ssl->handshake->alt_out_ctr, ssl->cur_out_ctr, 8);
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003602
Manuel Pégourié-Gonnard879a4f92014-07-11 22:31:12 +02003603 /* Set sequence_number to zero */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003604 memset(ssl->cur_out_ctr + 2, 0, 6);
Manuel Pégourié-Gonnard879a4f92014-07-11 22:31:12 +02003605
3606 /* Increment epoch */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003607 for (i = 2; i > 0; i--) {
3608 if (++ssl->cur_out_ctr[i - 1] != 0) {
Manuel Pégourié-Gonnard879a4f92014-07-11 22:31:12 +02003609 break;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003610 }
3611 }
Manuel Pégourié-Gonnard879a4f92014-07-11 22:31:12 +02003612
3613 /* The loop goes to its end iff the counter is wrapping */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003614 if (i == 0) {
3615 MBEDTLS_SSL_DEBUG_MSG(1, ("DTLS epoch would wrap"));
3616 return MBEDTLS_ERR_SSL_COUNTER_WRAPPING;
Manuel Pégourié-Gonnard879a4f92014-07-11 22:31:12 +02003617 }
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003618 } else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003619#endif /* MBEDTLS_SSL_PROTO_DTLS */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003620 memset(ssl->cur_out_ctr, 0, 8);
Paul Bakker5121ce52009-01-03 21:22:43 +00003621
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003622 ssl->transform_out = ssl->transform_negotiate;
3623 ssl->session_out = ssl->session_negotiate;
3624
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003625#if defined(MBEDTLS_SSL_HW_RECORD_ACCEL)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003626 if (mbedtls_ssl_hw_record_activate != NULL) {
3627 if ((ret = mbedtls_ssl_hw_record_activate(ssl, MBEDTLS_SSL_CHANNEL_OUTBOUND)) != 0) {
3628 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ssl_hw_record_activate", ret);
3629 return MBEDTLS_ERR_SSL_HW_ACCEL_FAILED;
Paul Bakker07eb38b2012-12-19 14:42:06 +01003630 }
3631 }
3632#endif
3633
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003634#if defined(MBEDTLS_SSL_PROTO_DTLS)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003635 if (ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM) {
3636 mbedtls_ssl_send_flight_completed(ssl);
3637 }
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02003638#endif
3639
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003640 if ((ret = mbedtls_ssl_write_handshake_msg(ssl)) != 0) {
3641 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ssl_write_handshake_msg", ret);
3642 return ret;
Paul Bakker5121ce52009-01-03 21:22:43 +00003643 }
3644
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02003645#if defined(MBEDTLS_SSL_PROTO_DTLS)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003646 if (ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
3647 (ret = mbedtls_ssl_flight_transmit(ssl)) != 0) {
3648 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ssl_flight_transmit", ret);
3649 return ret;
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02003650 }
3651#endif
3652
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003653 MBEDTLS_SSL_DEBUG_MSG(2, ("<= write finished"));
Paul Bakker5121ce52009-01-03 21:22:43 +00003654
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003655 return 0;
Paul Bakker5121ce52009-01-03 21:22:43 +00003656}
3657
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003658#if defined(MBEDTLS_SSL_PROTO_SSL3)
Manuel Pégourié-Gonnardca6440b2014-09-10 12:39:54 +00003659#define SSL_MAX_HASH_LEN 36
3660#else
3661#define SSL_MAX_HASH_LEN 12
3662#endif
3663
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003664int mbedtls_ssl_parse_finished(mbedtls_ssl_context *ssl)
Paul Bakker5121ce52009-01-03 21:22:43 +00003665{
Janos Follath865b3eb2019-12-16 11:46:15 +00003666 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnard879a4f92014-07-11 22:31:12 +02003667 unsigned int hash_len;
Manuel Pégourié-Gonnardca6440b2014-09-10 12:39:54 +00003668 unsigned char buf[SSL_MAX_HASH_LEN];
Paul Bakker5121ce52009-01-03 21:22:43 +00003669
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003670 MBEDTLS_SSL_DEBUG_MSG(2, ("=> parse finished"));
Paul Bakker5121ce52009-01-03 21:22:43 +00003671
Gilles Peskine622d8042021-12-13 14:38:40 +01003672 /* There is currently no ciphersuite using another length with TLS 1.2 */
3673#if defined(MBEDTLS_SSL_PROTO_SSL3)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003674 if (ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0) {
Gilles Peskine622d8042021-12-13 14:38:40 +01003675 hash_len = 36;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003676 } else
Gilles Peskine622d8042021-12-13 14:38:40 +01003677#endif
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003678 hash_len = 12;
Gilles Peskine622d8042021-12-13 14:38:40 +01003679
David Horstmann2b857292025-03-11 18:13:02 +00003680 ret = ssl->handshake->calc_finished(ssl, buf, ssl->conf->endpoint ^ 1);
3681 if (ret != 0) {
3682 MBEDTLS_SSL_DEBUG_RET(1, "calc_finished", ret);
3683 goto exit;
3684 }
Paul Bakker5121ce52009-01-03 21:22:43 +00003685
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003686 if ((ret = mbedtls_ssl_read_record(ssl, 1)) != 0) {
3687 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ssl_read_record", ret);
Gilles Peskinef91b2e52021-12-13 12:36:15 +01003688 goto exit;
Paul Bakker5121ce52009-01-03 21:22:43 +00003689 }
3690
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003691 if (ssl->in_msgtype != MBEDTLS_SSL_MSG_HANDSHAKE) {
3692 MBEDTLS_SSL_DEBUG_MSG(1, ("bad finished message"));
3693 mbedtls_ssl_send_alert_message(ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
3694 MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE);
Gilles Peskinef91b2e52021-12-13 12:36:15 +01003695 ret = MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE;
3696 goto exit;
Paul Bakker5121ce52009-01-03 21:22:43 +00003697 }
3698
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003699 if (ssl->in_msg[0] != MBEDTLS_SSL_HS_FINISHED ||
3700 ssl->in_hslen != mbedtls_ssl_hs_hdr_len(ssl) + hash_len) {
3701 MBEDTLS_SSL_DEBUG_MSG(1, ("bad finished message"));
3702 mbedtls_ssl_send_alert_message(ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
3703 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR);
Gilles Peskinef91b2e52021-12-13 12:36:15 +01003704 ret = MBEDTLS_ERR_SSL_BAD_HS_FINISHED;
3705 goto exit;
Paul Bakker5121ce52009-01-03 21:22:43 +00003706 }
3707
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003708 if (mbedtls_ct_memcmp(ssl->in_msg + mbedtls_ssl_hs_hdr_len(ssl),
3709 buf, hash_len) != 0) {
3710 MBEDTLS_SSL_DEBUG_MSG(1, ("bad finished message"));
3711 mbedtls_ssl_send_alert_message(ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
3712 MBEDTLS_SSL_ALERT_MSG_DECRYPT_ERROR);
Gilles Peskinef91b2e52021-12-13 12:36:15 +01003713 ret = MBEDTLS_ERR_SSL_BAD_HS_FINISHED;
3714 goto exit;
Paul Bakker5121ce52009-01-03 21:22:43 +00003715 }
3716
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003717#if defined(MBEDTLS_SSL_RENEGOTIATION)
Paul Bakker48916f92012-09-16 19:57:18 +00003718 ssl->verify_data_len = hash_len;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003719 memcpy(ssl->peer_verify_data, buf, hash_len);
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01003720#endif
Paul Bakker48916f92012-09-16 19:57:18 +00003721
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003722 if (ssl->handshake->resume != 0) {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003723#if defined(MBEDTLS_SSL_CLI_C)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003724 if (ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT) {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003725 ssl->state = MBEDTLS_SSL_CLIENT_CHANGE_CIPHER_SPEC;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003726 }
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01003727#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003728#if defined(MBEDTLS_SSL_SRV_C)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003729 if (ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER) {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003730 ssl->state = MBEDTLS_SSL_HANDSHAKE_WRAPUP;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003731 }
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01003732#endif
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003733 } else {
Paul Bakker5121ce52009-01-03 21:22:43 +00003734 ssl->state++;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003735 }
Paul Bakker5121ce52009-01-03 21:22:43 +00003736
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003737#if defined(MBEDTLS_SSL_PROTO_DTLS)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003738 if (ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM) {
3739 mbedtls_ssl_recv_flight_completed(ssl);
3740 }
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003741#endif
3742
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003743 MBEDTLS_SSL_DEBUG_MSG(2, ("<= parse finished"));
Paul Bakker5121ce52009-01-03 21:22:43 +00003744
Gilles Peskinef91b2e52021-12-13 12:36:15 +01003745exit:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003746 mbedtls_platform_zeroize(buf, hash_len);
3747 return ret;
Paul Bakker5121ce52009-01-03 21:22:43 +00003748}
3749
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003750static void ssl_handshake_params_init(mbedtls_ssl_handshake_params *handshake)
Paul Bakkeraccaffe2014-06-26 13:37:14 +02003751{
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003752 memset(handshake, 0, sizeof(mbedtls_ssl_handshake_params));
Paul Bakkeraccaffe2014-06-26 13:37:14 +02003753
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003754#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1) || \
3755 defined(MBEDTLS_SSL_PROTO_TLS1_1)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003756 mbedtls_md5_init(&handshake->fin_md5);
3757 mbedtls_sha1_init(&handshake->fin_sha1);
3758 mbedtls_md5_starts_ret(&handshake->fin_md5);
3759 mbedtls_sha1_starts_ret(&handshake->fin_sha1);
Paul Bakkeraccaffe2014-06-26 13:37:14 +02003760#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003761#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
3762#if defined(MBEDTLS_SHA256_C)
Andrzej Kurekeb342242019-01-29 09:14:33 -05003763#if defined(MBEDTLS_USE_PSA_CRYPTO)
3764 handshake->fin_sha256_psa = psa_hash_operation_init();
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003765 psa_hash_setup(&handshake->fin_sha256_psa, PSA_ALG_SHA_256);
Andrzej Kurekeb342242019-01-29 09:14:33 -05003766#else
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003767 mbedtls_sha256_init(&handshake->fin_sha256);
3768 mbedtls_sha256_starts_ret(&handshake->fin_sha256, 0);
Paul Bakkeraccaffe2014-06-26 13:37:14 +02003769#endif
Andrzej Kurekeb342242019-01-29 09:14:33 -05003770#endif
Gilles Peskined2d59372021-05-12 22:43:27 +02003771#if defined(MBEDTLS_SHA512_C) && !defined(MBEDTLS_SHA512_NO_SHA384)
Andrzej Kurekeb342242019-01-29 09:14:33 -05003772#if defined(MBEDTLS_USE_PSA_CRYPTO)
Andrzej Kurek972fba52019-01-30 03:29:12 -05003773 handshake->fin_sha384_psa = psa_hash_operation_init();
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003774 psa_hash_setup(&handshake->fin_sha384_psa, PSA_ALG_SHA_384);
Andrzej Kurekeb342242019-01-29 09:14:33 -05003775#else
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003776 mbedtls_sha512_init(&handshake->fin_sha512);
3777 mbedtls_sha512_starts_ret(&handshake->fin_sha512, 1);
Paul Bakkeraccaffe2014-06-26 13:37:14 +02003778#endif
Andrzej Kurekeb342242019-01-29 09:14:33 -05003779#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003780#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakkeraccaffe2014-06-26 13:37:14 +02003781
3782 handshake->update_checksum = ssl_update_checksum_start;
Hanno Becker7e5437a2017-04-28 17:15:26 +01003783
3784#if defined(MBEDTLS_SSL_PROTO_TLS1_2) && \
Gilles Peskineeccd8882020-03-10 12:19:08 +01003785 defined(MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003786 mbedtls_ssl_sig_hash_set_init(&handshake->hash_algs);
Hanno Becker7e5437a2017-04-28 17:15:26 +01003787#endif
Paul Bakkeraccaffe2014-06-26 13:37:14 +02003788
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003789#if defined(MBEDTLS_DHM_C)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003790 mbedtls_dhm_init(&handshake->dhm_ctx);
Paul Bakkeraccaffe2014-06-26 13:37:14 +02003791#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003792#if defined(MBEDTLS_ECDH_C)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003793 mbedtls_ecdh_init(&handshake->ecdh_ctx);
Paul Bakkeraccaffe2014-06-26 13:37:14 +02003794#endif
Manuel Pégourié-Gonnardeef142d2015-09-16 10:05:04 +02003795#if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003796 mbedtls_ecjpake_init(&handshake->ecjpake_ctx);
Manuel Pégourié-Gonnard77c06462015-09-17 13:59:49 +02003797#if defined(MBEDTLS_SSL_CLI_C)
3798 handshake->ecjpake_cache = NULL;
3799 handshake->ecjpake_cache_len = 0;
3800#endif
Manuel Pégourié-Gonnard76cfd3f2015-09-15 12:10:54 +02003801#endif
Manuel Pégourié-Gonnardcdc26ae2015-06-19 12:16:31 +02003802
Gilles Peskineeccd8882020-03-10 12:19:08 +01003803#if defined(MBEDTLS_SSL_ECP_RESTARTABLE_ENABLED)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003804 mbedtls_x509_crt_restart_init(&handshake->ecrs_ctx);
Manuel Pégourié-Gonnard862cde52017-05-17 11:56:15 +02003805#endif
3806
Manuel Pégourié-Gonnardcdc26ae2015-06-19 12:16:31 +02003807#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
3808 handshake->sni_authmode = MBEDTLS_SSL_VERIFY_UNSET;
3809#endif
Hanno Becker75173122019-02-06 16:18:31 +00003810
3811#if defined(MBEDTLS_X509_CRT_PARSE_C) && \
3812 !defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003813 mbedtls_pk_init(&handshake->peer_pubkey);
Hanno Becker75173122019-02-06 16:18:31 +00003814#endif
Paul Bakkeraccaffe2014-06-26 13:37:14 +02003815}
3816
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003817void mbedtls_ssl_transform_init(mbedtls_ssl_transform *transform)
Paul Bakkeraccaffe2014-06-26 13:37:14 +02003818{
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003819 memset(transform, 0, sizeof(mbedtls_ssl_transform));
Paul Bakker84bbeb52014-07-01 14:53:22 +02003820
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003821 mbedtls_cipher_init(&transform->cipher_ctx_enc);
3822 mbedtls_cipher_init(&transform->cipher_ctx_dec);
Paul Bakker84bbeb52014-07-01 14:53:22 +02003823
Hanno Beckerd56ed242018-01-03 15:32:51 +00003824#if defined(MBEDTLS_SSL_SOME_MODES_USE_MAC)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003825 mbedtls_md_init(&transform->md_ctx_enc);
3826 mbedtls_md_init(&transform->md_ctx_dec);
Hanno Beckerd56ed242018-01-03 15:32:51 +00003827#endif
Paul Bakkeraccaffe2014-06-26 13:37:14 +02003828}
3829
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003830void mbedtls_ssl_session_init(mbedtls_ssl_session *session)
Paul Bakkeraccaffe2014-06-26 13:37:14 +02003831{
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003832 memset(session, 0, sizeof(mbedtls_ssl_session));
Paul Bakkeraccaffe2014-06-26 13:37:14 +02003833}
3834
Manuel Pégourié-Gonnardd904d662022-06-17 10:24:00 +02003835MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003836static int ssl_handshake_init(mbedtls_ssl_context *ssl)
Paul Bakker48916f92012-09-16 19:57:18 +00003837{
Paul Bakkeraccaffe2014-06-26 13:37:14 +02003838 /* Clear old handshake information if present */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003839 if (ssl->transform_negotiate) {
3840 mbedtls_ssl_transform_free(ssl->transform_negotiate);
3841 }
3842 if (ssl->session_negotiate) {
3843 mbedtls_ssl_session_free(ssl->session_negotiate);
3844 }
3845 if (ssl->handshake) {
3846 mbedtls_ssl_handshake_free(ssl);
3847 }
Paul Bakkeraccaffe2014-06-26 13:37:14 +02003848
3849 /*
3850 * Either the pointers are now NULL or cleared properly and can be freed.
3851 * Now allocate missing structures.
3852 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003853 if (ssl->transform_negotiate == NULL) {
3854 ssl->transform_negotiate = mbedtls_calloc(1, sizeof(mbedtls_ssl_transform));
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02003855 }
Paul Bakker48916f92012-09-16 19:57:18 +00003856
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003857 if (ssl->session_negotiate == NULL) {
3858 ssl->session_negotiate = mbedtls_calloc(1, sizeof(mbedtls_ssl_session));
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02003859 }
Paul Bakker48916f92012-09-16 19:57:18 +00003860
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003861 if (ssl->handshake == NULL) {
3862 ssl->handshake = mbedtls_calloc(1, sizeof(mbedtls_ssl_handshake_params));
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02003863 }
Andrzej Kurek0afa2a12020-03-03 10:39:58 -05003864#if defined(MBEDTLS_SSL_VARIABLE_BUFFER_LENGTH)
3865 /* If the buffers are too small - reallocate */
Andrzej Kurek8ea68722020-04-03 06:40:47 -04003866
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003867 handle_buffer_resizing(ssl, 0, MBEDTLS_SSL_IN_BUFFER_LEN,
3868 MBEDTLS_SSL_OUT_BUFFER_LEN);
Andrzej Kurek0afa2a12020-03-03 10:39:58 -05003869#endif
Paul Bakker48916f92012-09-16 19:57:18 +00003870
Paul Bakkeraccaffe2014-06-26 13:37:14 +02003871 /* All pointers should exist and can be directly freed without issue */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003872 if (ssl->handshake == NULL ||
Paul Bakker48916f92012-09-16 19:57:18 +00003873 ssl->transform_negotiate == NULL ||
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003874 ssl->session_negotiate == NULL) {
3875 MBEDTLS_SSL_DEBUG_MSG(1, ("alloc() of ssl sub-contexts failed"));
Paul Bakkeraccaffe2014-06-26 13:37:14 +02003876
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003877 mbedtls_free(ssl->handshake);
3878 mbedtls_free(ssl->transform_negotiate);
3879 mbedtls_free(ssl->session_negotiate);
Paul Bakkeraccaffe2014-06-26 13:37:14 +02003880
3881 ssl->handshake = NULL;
3882 ssl->transform_negotiate = NULL;
3883 ssl->session_negotiate = NULL;
3884
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003885 return MBEDTLS_ERR_SSL_ALLOC_FAILED;
Paul Bakker48916f92012-09-16 19:57:18 +00003886 }
3887
Paul Bakkeraccaffe2014-06-26 13:37:14 +02003888 /* Initialize structures */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003889 mbedtls_ssl_session_init(ssl->session_negotiate);
3890 mbedtls_ssl_transform_init(ssl->transform_negotiate);
3891 ssl_handshake_params_init(ssl->handshake);
Paul Bakker968afaa2014-07-09 11:09:24 +02003892
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003893#if defined(MBEDTLS_SSL_PROTO_DTLS)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003894 if (ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM) {
Manuel Pégourié-Gonnard06939ce2015-05-11 11:25:46 +02003895 ssl->handshake->alt_transform_out = ssl->transform_out;
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003896
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003897 if (ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT) {
Manuel Pégourié-Gonnard06939ce2015-05-11 11:25:46 +02003898 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_PREPARING;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003899 } else {
Manuel Pégourié-Gonnard06939ce2015-05-11 11:25:46 +02003900 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_WAITING;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003901 }
Manuel Pégourié-Gonnard286a1362015-05-13 16:22:05 +02003902
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003903 mbedtls_ssl_set_timer(ssl, 0);
Manuel Pégourié-Gonnard06939ce2015-05-11 11:25:46 +02003904 }
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003905#endif
3906
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003907 return 0;
Paul Bakker48916f92012-09-16 19:57:18 +00003908}
3909
Manuel Pégourié-Gonnarde057d3b2015-05-20 10:59:43 +02003910#if defined(MBEDTLS_SSL_DTLS_HELLO_VERIFY) && defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard7d38d212014-07-23 17:52:09 +02003911/* Dummy cookie callbacks for defaults */
Manuel Pégourié-Gonnardd904d662022-06-17 10:24:00 +02003912MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003913static int ssl_cookie_write_dummy(void *ctx,
3914 unsigned char **p, unsigned char *end,
3915 const unsigned char *cli_id, size_t cli_id_len)
Manuel Pégourié-Gonnard7d38d212014-07-23 17:52:09 +02003916{
3917 ((void) ctx);
3918 ((void) p);
3919 ((void) end);
3920 ((void) cli_id);
3921 ((void) cli_id_len);
3922
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003923 return MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
Manuel Pégourié-Gonnard7d38d212014-07-23 17:52:09 +02003924}
3925
Manuel Pégourié-Gonnardd904d662022-06-17 10:24:00 +02003926MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003927static int ssl_cookie_check_dummy(void *ctx,
3928 const unsigned char *cookie, size_t cookie_len,
3929 const unsigned char *cli_id, size_t cli_id_len)
Manuel Pégourié-Gonnard7d38d212014-07-23 17:52:09 +02003930{
3931 ((void) ctx);
3932 ((void) cookie);
3933 ((void) cookie_len);
3934 ((void) cli_id);
3935 ((void) cli_id_len);
3936
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003937 return MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
Manuel Pégourié-Gonnard7d38d212014-07-23 17:52:09 +02003938}
Manuel Pégourié-Gonnarde057d3b2015-05-20 10:59:43 +02003939#endif /* MBEDTLS_SSL_DTLS_HELLO_VERIFY && MBEDTLS_SSL_SRV_C */
Manuel Pégourié-Gonnard7d38d212014-07-23 17:52:09 +02003940
Paul Bakker5121ce52009-01-03 21:22:43 +00003941/*
3942 * Initialize an SSL context
3943 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003944void mbedtls_ssl_init(mbedtls_ssl_context *ssl)
Manuel Pégourié-Gonnard41d479e2015-04-29 00:48:22 +02003945{
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003946 memset(ssl, 0, sizeof(mbedtls_ssl_context));
Manuel Pégourié-Gonnard41d479e2015-04-29 00:48:22 +02003947}
3948
3949/*
3950 * Setup an SSL context
3951 */
Hanno Becker2a43f6f2018-08-10 11:12:52 +01003952
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003953int mbedtls_ssl_setup(mbedtls_ssl_context *ssl,
3954 const mbedtls_ssl_config *conf)
Paul Bakker5121ce52009-01-03 21:22:43 +00003955{
Janos Follath865b3eb2019-12-16 11:46:15 +00003956 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Darryl Greenb33cc762019-11-28 14:29:44 +00003957 size_t in_buf_len = MBEDTLS_SSL_IN_BUFFER_LEN;
3958 size_t out_buf_len = MBEDTLS_SSL_OUT_BUFFER_LEN;
Paul Bakker5121ce52009-01-03 21:22:43 +00003959
Manuel Pégourié-Gonnarddef0bbe2015-05-04 14:56:36 +02003960 ssl->conf = conf;
Paul Bakker62f2dee2012-09-28 07:31:51 +00003961
3962 /*
Manuel Pégourié-Gonnard06193482014-02-14 08:39:32 +01003963 * Prepare base structures
Paul Bakker62f2dee2012-09-28 07:31:51 +00003964 */
k-stachowiakc9a5f022018-07-24 13:53:31 +02003965
3966 /* Set to NULL in case of an error condition */
3967 ssl->out_buf = NULL;
k-stachowiaka47911c2018-07-04 17:41:58 +02003968
Darryl Greenb33cc762019-11-28 14:29:44 +00003969#if defined(MBEDTLS_SSL_VARIABLE_BUFFER_LENGTH)
3970 ssl->in_buf_len = in_buf_len;
3971#endif
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003972 ssl->in_buf = mbedtls_calloc(1, in_buf_len);
3973 if (ssl->in_buf == NULL) {
3974 MBEDTLS_SSL_DEBUG_MSG(1, ("alloc(%" MBEDTLS_PRINTF_SIZET " bytes) failed", in_buf_len));
k-stachowiak9f7798e2018-07-31 16:52:32 +02003975 ret = MBEDTLS_ERR_SSL_ALLOC_FAILED;
k-stachowiaka47911c2018-07-04 17:41:58 +02003976 goto error;
Angus Grattond8213d02016-05-25 20:56:48 +10003977 }
3978
Darryl Greenb33cc762019-11-28 14:29:44 +00003979#if defined(MBEDTLS_SSL_VARIABLE_BUFFER_LENGTH)
3980 ssl->out_buf_len = out_buf_len;
3981#endif
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003982 ssl->out_buf = mbedtls_calloc(1, out_buf_len);
3983 if (ssl->out_buf == NULL) {
3984 MBEDTLS_SSL_DEBUG_MSG(1, ("alloc(%" MBEDTLS_PRINTF_SIZET " bytes) failed", out_buf_len));
k-stachowiak9f7798e2018-07-31 16:52:32 +02003985 ret = MBEDTLS_ERR_SSL_ALLOC_FAILED;
k-stachowiaka47911c2018-07-04 17:41:58 +02003986 goto error;
Paul Bakker5121ce52009-01-03 21:22:43 +00003987 }
3988
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003989 mbedtls_ssl_reset_in_out_pointers(ssl);
Manuel Pégourié-Gonnard419d5ae2015-05-04 19:32:36 +02003990
Johan Pascalb62bb512015-12-03 21:56:45 +01003991#if defined(MBEDTLS_SSL_DTLS_SRTP)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003992 memset(&ssl->dtls_srtp_info, 0, sizeof(ssl->dtls_srtp_info));
Johan Pascalb62bb512015-12-03 21:56:45 +01003993#endif
3994
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003995 if ((ret = ssl_handshake_init(ssl)) != 0) {
k-stachowiaka47911c2018-07-04 17:41:58 +02003996 goto error;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003997 }
Paul Bakker5121ce52009-01-03 21:22:43 +00003998
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003999 return 0;
k-stachowiaka47911c2018-07-04 17:41:58 +02004000
4001error:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004002 mbedtls_free(ssl->in_buf);
4003 mbedtls_free(ssl->out_buf);
k-stachowiaka47911c2018-07-04 17:41:58 +02004004
4005 ssl->conf = NULL;
4006
Darryl Greenb33cc762019-11-28 14:29:44 +00004007#if defined(MBEDTLS_SSL_VARIABLE_BUFFER_LENGTH)
4008 ssl->in_buf_len = 0;
4009 ssl->out_buf_len = 0;
4010#endif
k-stachowiaka47911c2018-07-04 17:41:58 +02004011 ssl->in_buf = NULL;
4012 ssl->out_buf = NULL;
4013
4014 ssl->in_hdr = NULL;
4015 ssl->in_ctr = NULL;
4016 ssl->in_len = NULL;
4017 ssl->in_iv = NULL;
4018 ssl->in_msg = NULL;
4019
4020 ssl->out_hdr = NULL;
4021 ssl->out_ctr = NULL;
4022 ssl->out_len = NULL;
4023 ssl->out_iv = NULL;
4024 ssl->out_msg = NULL;
4025
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004026 return ret;
Paul Bakker5121ce52009-01-03 21:22:43 +00004027}
4028
4029/*
Paul Bakker7eb013f2011-10-06 12:37:39 +00004030 * Reset an initialized and used SSL context for re-use while retaining
4031 * all application-set variables, function pointers and data.
Manuel Pégourié-Gonnard3f09b6d2015-09-08 11:58:14 +02004032 *
4033 * If partial is non-zero, keep data in the input buffer and client ID.
4034 * (Use when a DTLS client reconnects from the same port.)
Paul Bakker7eb013f2011-10-06 12:37:39 +00004035 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004036int mbedtls_ssl_session_reset_int(mbedtls_ssl_context *ssl, int partial)
Paul Bakker7eb013f2011-10-06 12:37:39 +00004037{
Janos Follath865b3eb2019-12-16 11:46:15 +00004038 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Darryl Greenb33cc762019-11-28 14:29:44 +00004039#if defined(MBEDTLS_SSL_VARIABLE_BUFFER_LENGTH)
4040 size_t in_buf_len = ssl->in_buf_len;
4041 size_t out_buf_len = ssl->out_buf_len;
4042#else
4043 size_t in_buf_len = MBEDTLS_SSL_IN_BUFFER_LEN;
4044 size_t out_buf_len = MBEDTLS_SSL_OUT_BUFFER_LEN;
4045#endif
Paul Bakker48916f92012-09-16 19:57:18 +00004046
Hanno Becker7e772132018-08-10 12:38:21 +01004047#if !defined(MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE) || \
4048 !defined(MBEDTLS_SSL_SRV_C)
4049 ((void) partial);
4050#endif
4051
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004052 ssl->state = MBEDTLS_SSL_HELLO_REQUEST;
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01004053
Manuel Pégourié-Gonnard286a1362015-05-13 16:22:05 +02004054 /* Cancel any possibly running timer */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004055 mbedtls_ssl_set_timer(ssl, 0);
Manuel Pégourié-Gonnard286a1362015-05-13 16:22:05 +02004056
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004057#if defined(MBEDTLS_SSL_RENEGOTIATION)
4058 ssl->renego_status = MBEDTLS_SSL_INITIAL_HANDSHAKE;
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01004059 ssl->renego_records_seen = 0;
Paul Bakker48916f92012-09-16 19:57:18 +00004060
4061 ssl->verify_data_len = 0;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004062 memset(ssl->own_verify_data, 0, MBEDTLS_SSL_VERIFY_DATA_MAX_LEN);
4063 memset(ssl->peer_verify_data, 0, MBEDTLS_SSL_VERIFY_DATA_MAX_LEN);
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01004064#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004065 ssl->secure_renegotiation = MBEDTLS_SSL_LEGACY_RENEGOTIATION;
Paul Bakker48916f92012-09-16 19:57:18 +00004066
Paul Bakker7eb013f2011-10-06 12:37:39 +00004067 ssl->in_offt = NULL;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004068 mbedtls_ssl_reset_in_out_pointers(ssl);
Paul Bakker7eb013f2011-10-06 12:37:39 +00004069
4070 ssl->in_msgtype = 0;
4071 ssl->in_msglen = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004072#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02004073 ssl->next_record_offset = 0;
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02004074 ssl->in_epoch = 0;
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02004075#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004076#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004077 mbedtls_ssl_dtls_replay_reset(ssl);
Manuel Pégourié-Gonnardb47368a2014-09-24 13:29:58 +02004078#endif
Paul Bakker7eb013f2011-10-06 12:37:39 +00004079
4080 ssl->in_hslen = 0;
4081 ssl->nb_zero = 0;
Hanno Beckeraf0665d2017-05-24 09:16:26 +01004082
4083 ssl->keep_current_message = 0;
Paul Bakker7eb013f2011-10-06 12:37:39 +00004084
4085 ssl->out_msgtype = 0;
4086 ssl->out_msglen = 0;
4087 ssl->out_left = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004088#if defined(MBEDTLS_SSL_CBC_RECORD_SPLITTING)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004089 if (ssl->split_done != MBEDTLS_SSL_CBC_RECORD_SPLITTING_DISABLED) {
Manuel Pégourié-Gonnardcfa477e2015-01-07 14:50:54 +01004090 ssl->split_done = 0;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004091 }
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +01004092#endif
Paul Bakker7eb013f2011-10-06 12:37:39 +00004093
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004094 memset(ssl->cur_out_ctr, 0, sizeof(ssl->cur_out_ctr));
Hanno Becker19859472018-08-06 09:40:20 +01004095
Paul Bakker48916f92012-09-16 19:57:18 +00004096 ssl->transform_in = NULL;
4097 ssl->transform_out = NULL;
Paul Bakker7eb013f2011-10-06 12:37:39 +00004098
Hanno Becker78640902018-08-13 16:35:15 +01004099 ssl->session_in = NULL;
4100 ssl->session_out = NULL;
4101
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004102 memset(ssl->out_buf, 0, out_buf_len);
Hanno Becker4ccbf062018-08-10 11:20:38 +01004103
David Horstmannd4f22082022-10-26 18:25:14 +01004104 int clear_in_buf = 1;
Hanno Becker4ccbf062018-08-10 11:20:38 +01004105#if defined(MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE) && defined(MBEDTLS_SSL_SRV_C)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004106 if (partial != 0) {
David Horstmannd4f22082022-10-26 18:25:14 +01004107 clear_in_buf = 0;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004108 }
Hanno Becker4ccbf062018-08-10 11:20:38 +01004109#endif /* MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE && MBEDTLS_SSL_SRV_C */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004110 if (clear_in_buf) {
Hanno Becker4ccbf062018-08-10 11:20:38 +01004111 ssl->in_left = 0;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004112 memset(ssl->in_buf, 0, in_buf_len);
Hanno Becker4ccbf062018-08-10 11:20:38 +01004113 }
Paul Bakker05ef8352012-05-08 09:17:57 +00004114
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004115#if defined(MBEDTLS_SSL_HW_RECORD_ACCEL)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004116 if (mbedtls_ssl_hw_record_reset != NULL) {
4117 MBEDTLS_SSL_DEBUG_MSG(2, ("going for mbedtls_ssl_hw_record_reset()"));
4118 if ((ret = mbedtls_ssl_hw_record_reset(ssl)) != 0) {
4119 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ssl_hw_record_reset", ret);
4120 return MBEDTLS_ERR_SSL_HW_ACCEL_FAILED;
Paul Bakker2770fbd2012-07-03 13:30:23 +00004121 }
Paul Bakker05ef8352012-05-08 09:17:57 +00004122 }
4123#endif
Paul Bakker2770fbd2012-07-03 13:30:23 +00004124
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004125 if (ssl->transform) {
4126 mbedtls_ssl_transform_free(ssl->transform);
4127 mbedtls_free(ssl->transform);
Paul Bakker48916f92012-09-16 19:57:18 +00004128 ssl->transform = NULL;
Paul Bakker2770fbd2012-07-03 13:30:23 +00004129 }
Paul Bakker48916f92012-09-16 19:57:18 +00004130
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004131 if (ssl->session) {
4132 mbedtls_ssl_session_free(ssl->session);
4133 mbedtls_free(ssl->session);
Paul Bakkerc0463502013-02-14 11:19:38 +01004134 ssl->session = NULL;
4135 }
4136
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004137#if defined(MBEDTLS_SSL_ALPN)
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02004138 ssl->alpn_chosen = NULL;
4139#endif
4140
Manuel Pégourié-Gonnarde057d3b2015-05-20 10:59:43 +02004141#if defined(MBEDTLS_SSL_DTLS_HELLO_VERIFY) && defined(MBEDTLS_SSL_SRV_C)
David Horstmannb5b1ed22022-10-27 13:21:49 +01004142 int free_cli_id = 1;
Hanno Becker4ccbf062018-08-10 11:20:38 +01004143#if defined(MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004144 if (partial != 0) {
David Horstmannd4f22082022-10-26 18:25:14 +01004145 free_cli_id = 0;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004146 }
Hanno Becker4ccbf062018-08-10 11:20:38 +01004147#endif
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004148 if (free_cli_id) {
4149 mbedtls_free(ssl->cli_id);
Manuel Pégourié-Gonnard3f09b6d2015-09-08 11:58:14 +02004150 ssl->cli_id = NULL;
4151 ssl->cli_id_len = 0;
4152 }
Manuel Pégourié-Gonnard43c02182014-07-22 17:32:01 +02004153#endif
4154
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004155 if ((ret = ssl_handshake_init(ssl)) != 0) {
4156 return ret;
4157 }
Paul Bakker2770fbd2012-07-03 13:30:23 +00004158
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004159 return 0;
Paul Bakker7eb013f2011-10-06 12:37:39 +00004160}
4161
Manuel Pégourié-Gonnard779e4292013-08-03 13:50:48 +02004162/*
Manuel Pégourié-Gonnard3f09b6d2015-09-08 11:58:14 +02004163 * Reset an initialized and used SSL context for re-use while retaining
4164 * all application-set variables, function pointers and data.
4165 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004166int mbedtls_ssl_session_reset(mbedtls_ssl_context *ssl)
Manuel Pégourié-Gonnard3f09b6d2015-09-08 11:58:14 +02004167{
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004168 return mbedtls_ssl_session_reset_int(ssl, 0);
Manuel Pégourié-Gonnard3f09b6d2015-09-08 11:58:14 +02004169}
4170
4171/*
Paul Bakker5121ce52009-01-03 21:22:43 +00004172 * SSL set accessors
4173 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004174void mbedtls_ssl_conf_endpoint(mbedtls_ssl_config *conf, int endpoint)
Paul Bakker5121ce52009-01-03 21:22:43 +00004175{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02004176 conf->endpoint = endpoint;
Paul Bakker5121ce52009-01-03 21:22:43 +00004177}
4178
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004179void mbedtls_ssl_conf_transport(mbedtls_ssl_config *conf, int transport)
Manuel Pégourié-Gonnard0b1ff292014-02-06 13:04:16 +01004180{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02004181 conf->transport = transport;
Manuel Pégourié-Gonnard0b1ff292014-02-06 13:04:16 +01004182}
4183
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004184#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004185void mbedtls_ssl_conf_dtls_anti_replay(mbedtls_ssl_config *conf, char mode)
Manuel Pégourié-Gonnard27393132014-09-24 14:41:11 +02004186{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02004187 conf->anti_replay = mode;
Manuel Pégourié-Gonnard27393132014-09-24 14:41:11 +02004188}
4189#endif
4190
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004191#if defined(MBEDTLS_SSL_DTLS_BADMAC_LIMIT)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004192void mbedtls_ssl_conf_dtls_badmac_limit(mbedtls_ssl_config *conf, unsigned limit)
Manuel Pégourié-Gonnardb0643d12014-10-14 18:30:36 +02004193{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02004194 conf->badmac_limit = limit;
Manuel Pégourié-Gonnardb0643d12014-10-14 18:30:36 +02004195}
4196#endif
4197
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004198#if defined(MBEDTLS_SSL_PROTO_DTLS)
Hanno Becker04da1892018-08-14 13:22:10 +01004199
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004200void mbedtls_ssl_set_datagram_packing(mbedtls_ssl_context *ssl,
4201 unsigned allow_packing)
Hanno Becker04da1892018-08-14 13:22:10 +01004202{
4203 ssl->disable_datagram_packing = !allow_packing;
4204}
4205
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004206void mbedtls_ssl_conf_handshake_timeout(mbedtls_ssl_config *conf,
4207 uint32_t min, uint32_t max)
Manuel Pégourié-Gonnard905dd242014-10-01 12:03:55 +02004208{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02004209 conf->hs_timeout_min = min;
4210 conf->hs_timeout_max = max;
Manuel Pégourié-Gonnard905dd242014-10-01 12:03:55 +02004211}
4212#endif
4213
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004214void mbedtls_ssl_conf_authmode(mbedtls_ssl_config *conf, int authmode)
Paul Bakker5121ce52009-01-03 21:22:43 +00004215{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02004216 conf->authmode = authmode;
Paul Bakker5121ce52009-01-03 21:22:43 +00004217}
4218
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004219#if defined(MBEDTLS_X509_CRT_PARSE_C)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004220void mbedtls_ssl_conf_verify(mbedtls_ssl_config *conf,
4221 int (*f_vrfy)(void *, mbedtls_x509_crt *, int, uint32_t *),
4222 void *p_vrfy)
Paul Bakkerb63b0af2011-01-13 17:54:59 +00004223{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02004224 conf->f_vrfy = f_vrfy;
4225 conf->p_vrfy = p_vrfy;
Paul Bakkerb63b0af2011-01-13 17:54:59 +00004226}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004227#endif /* MBEDTLS_X509_CRT_PARSE_C */
Paul Bakkerb63b0af2011-01-13 17:54:59 +00004228
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004229void mbedtls_ssl_conf_rng(mbedtls_ssl_config *conf,
4230 int (*f_rng)(void *, unsigned char *, size_t),
4231 void *p_rng)
Paul Bakker5121ce52009-01-03 21:22:43 +00004232{
Manuel Pégourié-Gonnard750e4d72015-05-07 12:35:38 +01004233 conf->f_rng = f_rng;
4234 conf->p_rng = p_rng;
Paul Bakker5121ce52009-01-03 21:22:43 +00004235}
4236
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004237void mbedtls_ssl_conf_dbg(mbedtls_ssl_config *conf,
4238 void (*f_dbg)(void *, int, const char *, int, const char *),
4239 void *p_dbg)
Paul Bakker5121ce52009-01-03 21:22:43 +00004240{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02004241 conf->f_dbg = f_dbg;
4242 conf->p_dbg = p_dbg;
Paul Bakker5121ce52009-01-03 21:22:43 +00004243}
4244
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004245void mbedtls_ssl_set_bio(mbedtls_ssl_context *ssl,
4246 void *p_bio,
4247 mbedtls_ssl_send_t *f_send,
4248 mbedtls_ssl_recv_t *f_recv,
4249 mbedtls_ssl_recv_timeout_t *f_recv_timeout)
Manuel Pégourié-Gonnard8fa6dfd2014-09-17 10:47:43 +02004250{
4251 ssl->p_bio = p_bio;
4252 ssl->f_send = f_send;
4253 ssl->f_recv = f_recv;
4254 ssl->f_recv_timeout = f_recv_timeout;
Manuel Pégourié-Gonnard97fd52c2015-05-06 15:38:52 +01004255}
4256
Manuel Pégourié-Gonnard6e7aaca2018-08-20 10:37:23 +02004257#if defined(MBEDTLS_SSL_PROTO_DTLS)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004258void mbedtls_ssl_set_mtu(mbedtls_ssl_context *ssl, uint16_t mtu)
Manuel Pégourié-Gonnard6e7aaca2018-08-20 10:37:23 +02004259{
4260 ssl->mtu = mtu;
4261}
4262#endif
4263
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004264void mbedtls_ssl_conf_read_timeout(mbedtls_ssl_config *conf, uint32_t timeout)
Manuel Pégourié-Gonnard97fd52c2015-05-06 15:38:52 +01004265{
4266 conf->read_timeout = timeout;
Manuel Pégourié-Gonnard8fa6dfd2014-09-17 10:47:43 +02004267}
4268
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004269void mbedtls_ssl_set_timer_cb(mbedtls_ssl_context *ssl,
4270 void *p_timer,
4271 mbedtls_ssl_set_timer_t *f_set_timer,
4272 mbedtls_ssl_get_timer_t *f_get_timer)
Manuel Pégourié-Gonnard2e012912015-05-12 20:55:41 +02004273{
4274 ssl->p_timer = p_timer;
4275 ssl->f_set_timer = f_set_timer;
4276 ssl->f_get_timer = f_get_timer;
Manuel Pégourié-Gonnard286a1362015-05-13 16:22:05 +02004277
4278 /* Make sure we start with no timer running */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004279 mbedtls_ssl_set_timer(ssl, 0);
Manuel Pégourié-Gonnard2e012912015-05-12 20:55:41 +02004280}
4281
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004282#if defined(MBEDTLS_SSL_SRV_C)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004283void mbedtls_ssl_conf_session_cache(mbedtls_ssl_config *conf,
4284 void *p_cache,
4285 int (*f_get_cache)(void *, mbedtls_ssl_session *),
4286 int (*f_set_cache)(void *, const mbedtls_ssl_session *))
Paul Bakker5121ce52009-01-03 21:22:43 +00004287{
Manuel Pégourié-Gonnard5cb33082015-05-06 18:06:26 +01004288 conf->p_cache = p_cache;
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02004289 conf->f_get_cache = f_get_cache;
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02004290 conf->f_set_cache = f_set_cache;
Paul Bakker5121ce52009-01-03 21:22:43 +00004291}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004292#endif /* MBEDTLS_SSL_SRV_C */
Paul Bakker5121ce52009-01-03 21:22:43 +00004293
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004294#if defined(MBEDTLS_SSL_CLI_C)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004295int mbedtls_ssl_set_session(mbedtls_ssl_context *ssl, const mbedtls_ssl_session *session)
Paul Bakker5121ce52009-01-03 21:22:43 +00004296{
Janos Follath865b3eb2019-12-16 11:46:15 +00004297 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +02004298
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004299 if (ssl == NULL ||
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +02004300 session == NULL ||
4301 ssl->session_negotiate == NULL ||
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004302 ssl->conf->endpoint != MBEDTLS_SSL_IS_CLIENT) {
4303 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +02004304 }
4305
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004306 if ((ret = mbedtls_ssl_session_copy(ssl->session_negotiate,
4307 session)) != 0) {
4308 return ret;
4309 }
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +02004310
Paul Bakker0a597072012-09-25 21:55:46 +00004311 ssl->handshake->resume = 1;
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +02004312
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004313 return 0;
Paul Bakker5121ce52009-01-03 21:22:43 +00004314}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004315#endif /* MBEDTLS_SSL_CLI_C */
Paul Bakker5121ce52009-01-03 21:22:43 +00004316
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004317void mbedtls_ssl_conf_ciphersuites(mbedtls_ssl_config *conf,
4318 const int *ciphersuites)
Paul Bakker5121ce52009-01-03 21:22:43 +00004319{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02004320 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_0] = ciphersuites;
4321 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_1] = ciphersuites;
4322 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_2] = ciphersuites;
4323 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_3] = ciphersuites;
Paul Bakker8f4ddae2013-04-15 15:09:54 +02004324}
4325
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004326void mbedtls_ssl_conf_ciphersuites_for_version(mbedtls_ssl_config *conf,
4327 const int *ciphersuites,
4328 int major, int minor)
Paul Bakker8f4ddae2013-04-15 15:09:54 +02004329{
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004330 if (major != MBEDTLS_SSL_MAJOR_VERSION_3) {
Paul Bakker8f4ddae2013-04-15 15:09:54 +02004331 return;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004332 }
Paul Bakker8f4ddae2013-04-15 15:09:54 +02004333
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004334 if (minor < MBEDTLS_SSL_MINOR_VERSION_0 || minor > MBEDTLS_SSL_MINOR_VERSION_3) {
Paul Bakker8f4ddae2013-04-15 15:09:54 +02004335 return;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004336 }
Paul Bakker8f4ddae2013-04-15 15:09:54 +02004337
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02004338 conf->ciphersuite_list[minor] = ciphersuites;
Paul Bakker5121ce52009-01-03 21:22:43 +00004339}
4340
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004341#if defined(MBEDTLS_X509_CRT_PARSE_C)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004342void mbedtls_ssl_conf_cert_profile(mbedtls_ssl_config *conf,
4343 const mbedtls_x509_crt_profile *profile)
Manuel Pégourié-Gonnard6e3ee3a2015-06-17 10:58:20 +02004344{
4345 conf->cert_profile = profile;
4346}
4347
Manuel Pégourié-Gonnard8f618a82015-05-10 21:13:36 +02004348/* Append a new keycert entry to a (possibly empty) list */
Manuel Pégourié-Gonnardd904d662022-06-17 10:24:00 +02004349MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004350static int ssl_append_key_cert(mbedtls_ssl_key_cert **head,
4351 mbedtls_x509_crt *cert,
4352 mbedtls_pk_context *key)
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02004353{
niisato8ee24222018-06-25 19:05:48 +09004354 mbedtls_ssl_key_cert *new_cert;
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02004355
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004356 new_cert = mbedtls_calloc(1, sizeof(mbedtls_ssl_key_cert));
4357 if (new_cert == NULL) {
4358 return MBEDTLS_ERR_SSL_ALLOC_FAILED;
4359 }
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02004360
niisato8ee24222018-06-25 19:05:48 +09004361 new_cert->cert = cert;
4362 new_cert->key = key;
4363 new_cert->next = NULL;
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02004364
Manuel Pégourié-Gonnard8f618a82015-05-10 21:13:36 +02004365 /* Update head is the list was null, else add to the end */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004366 if (*head == NULL) {
niisato8ee24222018-06-25 19:05:48 +09004367 *head = new_cert;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004368 } else {
Manuel Pégourié-Gonnard8f618a82015-05-10 21:13:36 +02004369 mbedtls_ssl_key_cert *cur = *head;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004370 while (cur->next != NULL) {
Manuel Pégourié-Gonnard8f618a82015-05-10 21:13:36 +02004371 cur = cur->next;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004372 }
niisato8ee24222018-06-25 19:05:48 +09004373 cur->next = new_cert;
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02004374 }
4375
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004376 return 0;
Manuel Pégourié-Gonnard8f618a82015-05-10 21:13:36 +02004377}
4378
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004379int mbedtls_ssl_conf_own_cert(mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnard8f618a82015-05-10 21:13:36 +02004380 mbedtls_x509_crt *own_cert,
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004381 mbedtls_pk_context *pk_key)
Manuel Pégourié-Gonnard8f618a82015-05-10 21:13:36 +02004382{
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004383 return ssl_append_key_cert(&conf->key_cert, own_cert, pk_key);
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02004384}
4385
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004386void mbedtls_ssl_conf_ca_chain(mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnardbc2b7712015-05-06 11:14:19 +01004387 mbedtls_x509_crt *ca_chain,
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004388 mbedtls_x509_crl *ca_crl)
Paul Bakker5121ce52009-01-03 21:22:43 +00004389{
Manuel Pégourié-Gonnardbc2b7712015-05-06 11:14:19 +01004390 conf->ca_chain = ca_chain;
4391 conf->ca_crl = ca_crl;
Hanno Becker5adaad92019-03-27 16:54:37 +00004392
4393#if defined(MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK)
4394 /* mbedtls_ssl_conf_ca_chain() and mbedtls_ssl_conf_ca_cb()
4395 * cannot be used together. */
4396 conf->f_ca_cb = NULL;
4397 conf->p_ca_cb = NULL;
4398#endif /* MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK */
Paul Bakker5121ce52009-01-03 21:22:43 +00004399}
Hanno Becker5adaad92019-03-27 16:54:37 +00004400
4401#if defined(MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004402void mbedtls_ssl_conf_ca_cb(mbedtls_ssl_config *conf,
4403 mbedtls_x509_crt_ca_cb_t f_ca_cb,
4404 void *p_ca_cb)
Hanno Becker5adaad92019-03-27 16:54:37 +00004405{
4406 conf->f_ca_cb = f_ca_cb;
4407 conf->p_ca_cb = p_ca_cb;
4408
4409 /* mbedtls_ssl_conf_ca_chain() and mbedtls_ssl_conf_ca_cb()
4410 * cannot be used together. */
4411 conf->ca_chain = NULL;
4412 conf->ca_crl = NULL;
4413}
4414#endif /* MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004415#endif /* MBEDTLS_X509_CRT_PARSE_C */
Paul Bakkereb2c6582012-09-27 19:15:01 +00004416
Manuel Pégourié-Gonnard1af6c852015-05-10 23:10:37 +02004417#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004418int mbedtls_ssl_set_hs_own_cert(mbedtls_ssl_context *ssl,
4419 mbedtls_x509_crt *own_cert,
4420 mbedtls_pk_context *pk_key)
Manuel Pégourié-Gonnard1af6c852015-05-10 23:10:37 +02004421{
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004422 return ssl_append_key_cert(&ssl->handshake->sni_key_cert,
4423 own_cert, pk_key);
Manuel Pégourié-Gonnard1af6c852015-05-10 23:10:37 +02004424}
Manuel Pégourié-Gonnard22bfa4b2015-05-11 08:46:37 +02004425
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004426void mbedtls_ssl_set_hs_ca_chain(mbedtls_ssl_context *ssl,
4427 mbedtls_x509_crt *ca_chain,
4428 mbedtls_x509_crl *ca_crl)
Manuel Pégourié-Gonnard22bfa4b2015-05-11 08:46:37 +02004429{
4430 ssl->handshake->sni_ca_chain = ca_chain;
4431 ssl->handshake->sni_ca_crl = ca_crl;
4432}
Manuel Pégourié-Gonnardcdc26ae2015-06-19 12:16:31 +02004433
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004434void mbedtls_ssl_set_hs_authmode(mbedtls_ssl_context *ssl,
4435 int authmode)
Manuel Pégourié-Gonnardcdc26ae2015-06-19 12:16:31 +02004436{
4437 ssl->handshake->sni_authmode = authmode;
4438}
Manuel Pégourié-Gonnard1af6c852015-05-10 23:10:37 +02004439#endif /* MBEDTLS_SSL_SERVER_NAME_INDICATION */
4440
Hanno Becker8927c832019-04-03 12:52:50 +01004441#if defined(MBEDTLS_X509_CRT_PARSE_C)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004442void mbedtls_ssl_set_verify(mbedtls_ssl_context *ssl,
4443 int (*f_vrfy)(void *, mbedtls_x509_crt *, int, uint32_t *),
4444 void *p_vrfy)
Hanno Becker8927c832019-04-03 12:52:50 +01004445{
4446 ssl->f_vrfy = f_vrfy;
4447 ssl->p_vrfy = p_vrfy;
4448}
4449#endif
4450
Manuel Pégourié-Gonnardeef142d2015-09-16 10:05:04 +02004451#if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
Manuel Pégourié-Gonnard7002f4a2015-09-15 12:43:43 +02004452/*
4453 * Set EC J-PAKE password for current handshake
4454 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004455int mbedtls_ssl_set_hs_ecjpake_password(mbedtls_ssl_context *ssl,
4456 const unsigned char *pw,
4457 size_t pw_len)
Manuel Pégourié-Gonnard7002f4a2015-09-15 12:43:43 +02004458{
4459 mbedtls_ecjpake_role role;
4460
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004461 if (ssl->handshake == NULL || ssl->conf == NULL) {
4462 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
4463 }
Manuel Pégourié-Gonnard7002f4a2015-09-15 12:43:43 +02004464
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004465 if (ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER) {
Manuel Pégourié-Gonnard7002f4a2015-09-15 12:43:43 +02004466 role = MBEDTLS_ECJPAKE_SERVER;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004467 } else {
Manuel Pégourié-Gonnard7002f4a2015-09-15 12:43:43 +02004468 role = MBEDTLS_ECJPAKE_CLIENT;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004469 }
Manuel Pégourié-Gonnard7002f4a2015-09-15 12:43:43 +02004470
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004471 return mbedtls_ecjpake_setup(&ssl->handshake->ecjpake_ctx,
4472 role,
4473 MBEDTLS_MD_SHA256,
4474 MBEDTLS_ECP_DP_SECP256R1,
4475 pw, pw_len);
Manuel Pégourié-Gonnard7002f4a2015-09-15 12:43:43 +02004476}
Manuel Pégourié-Gonnardeef142d2015-09-16 10:05:04 +02004477#endif /* MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED */
Manuel Pégourié-Gonnard7002f4a2015-09-15 12:43:43 +02004478
Gilles Peskineeccd8882020-03-10 12:19:08 +01004479#if defined(MBEDTLS_KEY_EXCHANGE_SOME_PSK_ENABLED)
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01004480
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004481static void ssl_conf_remove_psk(mbedtls_ssl_config *conf)
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01004482{
4483 /* Remove reference to existing PSK, if any. */
4484#if defined(MBEDTLS_USE_PSA_CRYPTO)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004485 if (!mbedtls_svc_key_id_is_null(conf->psk_opaque)) {
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01004486 /* The maintenance of the PSK key slot is the
4487 * user's responsibility. */
Ronald Croncf56a0a2020-08-04 09:51:30 +02004488 conf->psk_opaque = MBEDTLS_SVC_KEY_ID_INIT;
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01004489 }
Hanno Beckera63ac3f2018-11-05 12:47:16 +00004490 /* This and the following branch should never
Tom Cosgrove49f99bc2022-12-04 16:44:21 +00004491 * be taken simultaneously as we maintain the
Hanno Beckera63ac3f2018-11-05 12:47:16 +00004492 * invariant that raw and opaque PSKs are never
4493 * configured simultaneously. As a safeguard,
4494 * though, `else` is omitted here. */
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01004495#endif /* MBEDTLS_USE_PSA_CRYPTO */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004496 if (conf->psk != NULL) {
4497 mbedtls_platform_zeroize(conf->psk, conf->psk_len);
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01004498
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004499 mbedtls_free(conf->psk);
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01004500 conf->psk = NULL;
4501 conf->psk_len = 0;
4502 }
4503
4504 /* Remove reference to PSK identity, if any. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004505 if (conf->psk_identity != NULL) {
4506 mbedtls_free(conf->psk_identity);
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01004507 conf->psk_identity = NULL;
4508 conf->psk_identity_len = 0;
4509 }
4510}
4511
Hanno Becker7390c712018-11-15 13:33:04 +00004512/* This function assumes that PSK identity in the SSL config is unset.
4513 * It checks that the provided identity is well-formed and attempts
4514 * to make a copy of it in the SSL config.
4515 * On failure, the PSK identity in the config remains unset. */
Manuel Pégourié-Gonnardd904d662022-06-17 10:24:00 +02004516MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004517static int ssl_conf_set_psk_identity(mbedtls_ssl_config *conf,
4518 unsigned char const *psk_identity,
4519 size_t psk_identity_len)
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02004520{
Manuel Pégourié-Gonnardc6b5d832015-08-27 16:37:35 +02004521 /* Identity len will be encoded on two bytes */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004522 if (psk_identity == NULL ||
4523 (psk_identity_len >> 16) != 0 ||
4524 psk_identity_len > MBEDTLS_SSL_OUT_CONTENT_LEN) {
4525 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
Manuel Pégourié-Gonnardc6b5d832015-08-27 16:37:35 +02004526 }
4527
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004528 conf->psk_identity = mbedtls_calloc(1, psk_identity_len);
4529 if (conf->psk_identity == NULL) {
4530 return MBEDTLS_ERR_SSL_ALLOC_FAILED;
4531 }
Paul Bakker6db455e2013-09-18 17:29:31 +02004532
Manuel Pégourié-Gonnard120fdbd2015-05-07 17:07:50 +01004533 conf->psk_identity_len = psk_identity_len;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004534 memcpy(conf->psk_identity, psk_identity, conf->psk_identity_len);
Paul Bakker5ad403f2013-09-18 21:21:30 +02004535
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004536 return 0;
Paul Bakker6db455e2013-09-18 17:29:31 +02004537}
4538
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004539int mbedtls_ssl_conf_psk(mbedtls_ssl_config *conf,
4540 const unsigned char *psk, size_t psk_len,
4541 const unsigned char *psk_identity, size_t psk_identity_len)
Hanno Becker7390c712018-11-15 13:33:04 +00004542{
Janos Follath865b3eb2019-12-16 11:46:15 +00004543 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Hanno Becker7390c712018-11-15 13:33:04 +00004544 /* Remove opaque/raw PSK + PSK Identity */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004545 ssl_conf_remove_psk(conf);
Hanno Becker7390c712018-11-15 13:33:04 +00004546
4547 /* Check and set raw PSK */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004548 if (psk == NULL) {
4549 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
4550 }
4551 if (psk_len == 0) {
4552 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
4553 }
4554 if (psk_len > MBEDTLS_PSK_MAX_LEN) {
4555 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
4556 }
Piotr Nowicki9926eaf2019-11-20 14:54:36 +01004557
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004558 if ((conf->psk = mbedtls_calloc(1, psk_len)) == NULL) {
4559 return MBEDTLS_ERR_SSL_ALLOC_FAILED;
4560 }
Hanno Becker7390c712018-11-15 13:33:04 +00004561 conf->psk_len = psk_len;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004562 memcpy(conf->psk, psk, conf->psk_len);
Hanno Becker7390c712018-11-15 13:33:04 +00004563
4564 /* Check and set PSK Identity */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004565 ret = ssl_conf_set_psk_identity(conf, psk_identity, psk_identity_len);
4566 if (ret != 0) {
4567 ssl_conf_remove_psk(conf);
4568 }
Hanno Becker7390c712018-11-15 13:33:04 +00004569
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004570 return ret;
Hanno Becker7390c712018-11-15 13:33:04 +00004571}
4572
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004573static void ssl_remove_psk(mbedtls_ssl_context *ssl)
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01004574{
4575#if defined(MBEDTLS_USE_PSA_CRYPTO)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004576 if (!mbedtls_svc_key_id_is_null(ssl->handshake->psk_opaque)) {
Ronald Croncf56a0a2020-08-04 09:51:30 +02004577 ssl->handshake->psk_opaque = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004578 } else
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01004579#endif /* MBEDTLS_USE_PSA_CRYPTO */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004580 if (ssl->handshake->psk != NULL) {
4581 mbedtls_platform_zeroize(ssl->handshake->psk,
4582 ssl->handshake->psk_len);
4583 mbedtls_free(ssl->handshake->psk);
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01004584 ssl->handshake->psk_len = 0;
lhuang040a2dd6d2024-06-11 12:31:17 -07004585 ssl->handshake->psk = NULL;
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01004586 }
4587}
4588
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004589int mbedtls_ssl_set_hs_psk(mbedtls_ssl_context *ssl,
4590 const unsigned char *psk, size_t psk_len)
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01004591{
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004592 if (psk == NULL || ssl->handshake == NULL) {
4593 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
4594 }
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01004595
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004596 if (psk_len > MBEDTLS_PSK_MAX_LEN) {
4597 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
4598 }
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01004599
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004600 ssl_remove_psk(ssl);
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01004601
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004602 if ((ssl->handshake->psk = mbedtls_calloc(1, psk_len)) == NULL) {
4603 return MBEDTLS_ERR_SSL_ALLOC_FAILED;
4604 }
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01004605
4606 ssl->handshake->psk_len = psk_len;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004607 memcpy(ssl->handshake->psk, psk, ssl->handshake->psk_len);
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01004608
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004609 return 0;
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01004610}
4611
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01004612#if defined(MBEDTLS_USE_PSA_CRYPTO)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004613int mbedtls_ssl_conf_psk_opaque(mbedtls_ssl_config *conf,
4614 psa_key_id_t psk,
4615 const unsigned char *psk_identity,
4616 size_t psk_identity_len)
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01004617{
Janos Follath865b3eb2019-12-16 11:46:15 +00004618 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Hanno Becker7390c712018-11-15 13:33:04 +00004619 /* Clear opaque/raw PSK + PSK Identity, if present. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004620 ssl_conf_remove_psk(conf);
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01004621
Hanno Becker7390c712018-11-15 13:33:04 +00004622 /* Check and set opaque PSK */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004623 if (mbedtls_svc_key_id_is_null(psk)) {
4624 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
4625 }
Ronald Croncf56a0a2020-08-04 09:51:30 +02004626 conf->psk_opaque = psk;
Hanno Becker7390c712018-11-15 13:33:04 +00004627
4628 /* Check and set PSK Identity */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004629 ret = ssl_conf_set_psk_identity(conf, psk_identity,
4630 psk_identity_len);
4631 if (ret != 0) {
4632 ssl_conf_remove_psk(conf);
4633 }
Hanno Becker7390c712018-11-15 13:33:04 +00004634
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004635 return ret;
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01004636}
4637
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004638int mbedtls_ssl_set_hs_psk_opaque(mbedtls_ssl_context *ssl,
4639 psa_key_id_t psk)
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01004640{
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004641 if ((mbedtls_svc_key_id_is_null(psk)) ||
4642 (ssl->handshake == NULL)) {
4643 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
4644 }
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01004645
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004646 ssl_remove_psk(ssl);
Ronald Croncf56a0a2020-08-04 09:51:30 +02004647 ssl->handshake->psk_opaque = psk;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004648 return 0;
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01004649}
4650#endif /* MBEDTLS_USE_PSA_CRYPTO */
4651
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004652void mbedtls_ssl_conf_psk_cb(mbedtls_ssl_config *conf,
4653 int (*f_psk)(void *, mbedtls_ssl_context *, const unsigned char *,
4654 size_t),
4655 void *p_psk)
Paul Bakker6db455e2013-09-18 17:29:31 +02004656{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02004657 conf->f_psk = f_psk;
4658 conf->p_psk = p_psk;
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02004659}
Gilles Peskineeccd8882020-03-10 12:19:08 +01004660#endif /* MBEDTLS_KEY_EXCHANGE_SOME_PSK_ENABLED */
Paul Bakker43b7e352011-01-18 15:27:19 +00004661
Manuel Pégourié-Gonnardcf141ca2015-05-20 10:35:51 +02004662#if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_SSL_SRV_C)
Hanno Becker470a8c42017-10-04 15:28:46 +01004663
4664#if !defined(MBEDTLS_DEPRECATED_REMOVED)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004665int mbedtls_ssl_conf_dh_param(mbedtls_ssl_config *conf, const char *dhm_P, const char *dhm_G)
Paul Bakker5121ce52009-01-03 21:22:43 +00004666{
Janos Follath865b3eb2019-12-16 11:46:15 +00004667 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker5121ce52009-01-03 21:22:43 +00004668
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004669 if ((ret = mbedtls_mpi_read_string(&conf->dhm_P, 16, dhm_P)) != 0 ||
4670 (ret = mbedtls_mpi_read_string(&conf->dhm_G, 16, dhm_G)) != 0) {
4671 mbedtls_mpi_free(&conf->dhm_P);
4672 mbedtls_mpi_free(&conf->dhm_G);
4673 return ret;
Manuel Pégourié-Gonnard1028b742015-05-06 17:33:07 +01004674 }
Paul Bakker5121ce52009-01-03 21:22:43 +00004675
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004676 return 0;
Paul Bakker5121ce52009-01-03 21:22:43 +00004677}
Hanno Becker470a8c42017-10-04 15:28:46 +01004678#endif /* MBEDTLS_DEPRECATED_REMOVED */
Paul Bakker5121ce52009-01-03 21:22:43 +00004679
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004680int mbedtls_ssl_conf_dh_param_bin(mbedtls_ssl_config *conf,
4681 const unsigned char *dhm_P, size_t P_len,
4682 const unsigned char *dhm_G, size_t G_len)
Hanno Beckera90658f2017-10-04 15:29:08 +01004683{
Janos Follath865b3eb2019-12-16 11:46:15 +00004684 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Hanno Beckera90658f2017-10-04 15:29:08 +01004685
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004686 mbedtls_mpi_free(&conf->dhm_P);
4687 mbedtls_mpi_free(&conf->dhm_G);
Glenn Straussde081ce2021-12-20 01:43:17 -05004688
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004689 if ((ret = mbedtls_mpi_read_binary(&conf->dhm_P, dhm_P, P_len)) != 0 ||
4690 (ret = mbedtls_mpi_read_binary(&conf->dhm_G, dhm_G, G_len)) != 0) {
4691 mbedtls_mpi_free(&conf->dhm_P);
4692 mbedtls_mpi_free(&conf->dhm_G);
4693 return ret;
Hanno Beckera90658f2017-10-04 15:29:08 +01004694 }
4695
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004696 return 0;
Hanno Beckera90658f2017-10-04 15:29:08 +01004697}
Paul Bakker5121ce52009-01-03 21:22:43 +00004698
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004699int mbedtls_ssl_conf_dh_param_ctx(mbedtls_ssl_config *conf, mbedtls_dhm_context *dhm_ctx)
Paul Bakker1b57b062011-01-06 15:48:19 +00004700{
Janos Follath865b3eb2019-12-16 11:46:15 +00004701 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker1b57b062011-01-06 15:48:19 +00004702
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004703 mbedtls_mpi_free(&conf->dhm_P);
4704 mbedtls_mpi_free(&conf->dhm_G);
Glenn Straussde081ce2021-12-20 01:43:17 -05004705
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004706 if ((ret = mbedtls_mpi_copy(&conf->dhm_P, &dhm_ctx->P)) != 0 ||
4707 (ret = mbedtls_mpi_copy(&conf->dhm_G, &dhm_ctx->G)) != 0) {
4708 mbedtls_mpi_free(&conf->dhm_P);
4709 mbedtls_mpi_free(&conf->dhm_G);
4710 return ret;
Manuel Pégourié-Gonnard1028b742015-05-06 17:33:07 +01004711 }
Paul Bakker1b57b062011-01-06 15:48:19 +00004712
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004713 return 0;
Paul Bakker1b57b062011-01-06 15:48:19 +00004714}
Manuel Pégourié-Gonnardcf141ca2015-05-20 10:35:51 +02004715#endif /* MBEDTLS_DHM_C && MBEDTLS_SSL_SRV_C */
Paul Bakker1b57b062011-01-06 15:48:19 +00004716
Manuel Pégourié-Gonnardbd990d62015-06-11 14:49:42 +02004717#if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_SSL_CLI_C)
4718/*
4719 * Set the minimum length for Diffie-Hellman parameters
4720 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004721void mbedtls_ssl_conf_dhm_min_bitlen(mbedtls_ssl_config *conf,
4722 unsigned int bitlen)
Manuel Pégourié-Gonnardbd990d62015-06-11 14:49:42 +02004723{
4724 conf->dhm_min_bitlen = bitlen;
4725}
4726#endif /* MBEDTLS_DHM_C && MBEDTLS_SSL_CLI_C */
4727
Gilles Peskineeccd8882020-03-10 12:19:08 +01004728#if defined(MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED)
Manuel Pégourié-Gonnard36a8b572015-06-17 12:43:26 +02004729/*
4730 * Set allowed/preferred hashes for handshake signatures
4731 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004732void mbedtls_ssl_conf_sig_hashes(mbedtls_ssl_config *conf,
4733 const int *hashes)
Manuel Pégourié-Gonnard36a8b572015-06-17 12:43:26 +02004734{
4735 conf->sig_hashes = hashes;
4736}
Gilles Peskineeccd8882020-03-10 12:19:08 +01004737#endif /* MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED */
Manuel Pégourié-Gonnard36a8b572015-06-17 12:43:26 +02004738
Manuel Pégourié-Gonnardb541da62015-06-17 11:43:30 +02004739#if defined(MBEDTLS_ECP_C)
Manuel Pégourié-Gonnard7f38ed02014-02-04 15:52:33 +01004740/*
4741 * Set the allowed elliptic curves
4742 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004743void mbedtls_ssl_conf_curves(mbedtls_ssl_config *conf,
4744 const mbedtls_ecp_group_id *curve_list)
Manuel Pégourié-Gonnard7f38ed02014-02-04 15:52:33 +01004745{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02004746 conf->curve_list = curve_list;
Manuel Pégourié-Gonnard7f38ed02014-02-04 15:52:33 +01004747}
Hanno Becker947194e2017-04-07 13:25:49 +01004748#endif /* MBEDTLS_ECP_C */
Manuel Pégourié-Gonnard7f38ed02014-02-04 15:52:33 +01004749
Manuel Pégourié-Gonnardbc2b7712015-05-06 11:14:19 +01004750#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004751void mbedtls_ssl_conf_sni(mbedtls_ssl_config *conf,
4752 int (*f_sni)(void *, mbedtls_ssl_context *,
4753 const unsigned char *, size_t),
4754 void *p_sni)
Paul Bakker5701cdc2012-09-27 21:49:42 +00004755{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02004756 conf->f_sni = f_sni;
4757 conf->p_sni = p_sni;
Paul Bakker5701cdc2012-09-27 21:49:42 +00004758}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004759#endif /* MBEDTLS_SSL_SERVER_NAME_INDICATION */
Paul Bakker5701cdc2012-09-27 21:49:42 +00004760
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004761#if defined(MBEDTLS_SSL_ALPN)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004762int mbedtls_ssl_conf_alpn_protocols(mbedtls_ssl_config *conf, const char **protos)
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02004763{
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +02004764 size_t cur_len, tot_len;
4765 const char **p;
4766
4767 /*
Brian J Murray1903fb32016-11-06 04:45:15 -08004768 * RFC 7301 3.1: "Empty strings MUST NOT be included and byte strings
4769 * MUST NOT be truncated."
4770 * We check lengths now rather than later.
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +02004771 */
4772 tot_len = 0;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004773 for (p = protos; *p != NULL; p++) {
4774 cur_len = strlen(*p);
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +02004775 tot_len += cur_len;
4776
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004777 if ((cur_len == 0) ||
4778 (cur_len > MBEDTLS_SSL_MAX_ALPN_NAME_LEN) ||
4779 (tot_len > MBEDTLS_SSL_MAX_ALPN_LIST_LEN)) {
4780 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
4781 }
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +02004782 }
4783
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02004784 conf->alpn_list = protos;
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +02004785
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004786 return 0;
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02004787}
4788
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004789const char *mbedtls_ssl_get_alpn_protocol(const mbedtls_ssl_context *ssl)
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02004790{
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004791 return ssl->alpn_chosen;
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02004792}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004793#endif /* MBEDTLS_SSL_ALPN */
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02004794
Johan Pascalb62bb512015-12-03 21:56:45 +01004795#if defined(MBEDTLS_SSL_DTLS_SRTP)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004796void mbedtls_ssl_conf_srtp_mki_value_supported(mbedtls_ssl_config *conf,
4797 int support_mki_value)
Ron Eldor591f1622018-01-22 12:30:04 +02004798{
4799 conf->dtls_srtp_mki_support = support_mki_value;
4800}
4801
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004802int mbedtls_ssl_dtls_srtp_set_mki_value(mbedtls_ssl_context *ssl,
4803 unsigned char *mki_value,
4804 uint16_t mki_len)
Ron Eldor591f1622018-01-22 12:30:04 +02004805{
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004806 if (mki_len > MBEDTLS_TLS_SRTP_MAX_MKI_LENGTH) {
4807 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
Ron Eldora9788042018-12-05 11:04:31 +02004808 }
Ron Eldor591f1622018-01-22 12:30:04 +02004809
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004810 if (ssl->conf->dtls_srtp_mki_support == MBEDTLS_SSL_DTLS_SRTP_MKI_UNSUPPORTED) {
4811 return MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
Ron Eldora9788042018-12-05 11:04:31 +02004812 }
Ron Eldor591f1622018-01-22 12:30:04 +02004813
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004814 memcpy(ssl->dtls_srtp_info.mki_value, mki_value, mki_len);
Ron Eldor591f1622018-01-22 12:30:04 +02004815 ssl->dtls_srtp_info.mki_len = mki_len;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004816 return 0;
Ron Eldor591f1622018-01-22 12:30:04 +02004817}
4818
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004819int mbedtls_ssl_conf_dtls_srtp_protection_profiles(mbedtls_ssl_config *conf,
4820 const mbedtls_ssl_srtp_profile *profiles)
Johan Pascalb62bb512015-12-03 21:56:45 +01004821{
Johan Pascal253d0262020-09-22 13:04:45 +02004822 const mbedtls_ssl_srtp_profile *p;
4823 size_t list_size = 0;
Johan Pascalb62bb512015-12-03 21:56:45 +01004824
Johan Pascal253d0262020-09-22 13:04:45 +02004825 /* check the profiles list: all entry must be valid,
4826 * its size cannot be more than the total number of supported profiles, currently 4 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004827 for (p = profiles; *p != MBEDTLS_TLS_SRTP_UNSET &&
4828 list_size <= MBEDTLS_TLS_SRTP_MAX_PROFILE_LIST_LENGTH;
4829 p++) {
4830 if (mbedtls_ssl_check_srtp_profile_value(*p) != MBEDTLS_TLS_SRTP_UNSET) {
Johan Pascal76fdf1d2020-10-22 23:31:00 +02004831 list_size++;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004832 } else {
Johan Pascal76fdf1d2020-10-22 23:31:00 +02004833 /* unsupported value, stop parsing and set the size to an error value */
4834 list_size = MBEDTLS_TLS_SRTP_MAX_PROFILE_LIST_LENGTH + 1;
Johan Pascalb62bb512015-12-03 21:56:45 +01004835 }
4836 }
4837
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004838 if (list_size > MBEDTLS_TLS_SRTP_MAX_PROFILE_LIST_LENGTH) {
4839 conf->dtls_srtp_profile_list = NULL;
4840 conf->dtls_srtp_profile_list_len = 0;
4841 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
Johan Pascal253d0262020-09-22 13:04:45 +02004842 }
4843
Johan Pascal9bc97ca2020-09-21 23:44:45 +02004844 conf->dtls_srtp_profile_list = profiles;
Johan Pascal253d0262020-09-22 13:04:45 +02004845 conf->dtls_srtp_profile_list_len = list_size;
Johan Pascalb62bb512015-12-03 21:56:45 +01004846
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004847 return 0;
Johan Pascalb62bb512015-12-03 21:56:45 +01004848}
4849
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004850void mbedtls_ssl_get_dtls_srtp_negotiation_result(const mbedtls_ssl_context *ssl,
4851 mbedtls_dtls_srtp_info *dtls_srtp_info)
Johan Pascalb62bb512015-12-03 21:56:45 +01004852{
Johan Pascal2258a4f2020-10-28 13:53:09 +01004853 dtls_srtp_info->chosen_dtls_srtp_profile = ssl->dtls_srtp_info.chosen_dtls_srtp_profile;
4854 /* do not copy the mki value if there is no chosen profile */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004855 if (dtls_srtp_info->chosen_dtls_srtp_profile == MBEDTLS_TLS_SRTP_UNSET) {
Johan Pascal2258a4f2020-10-28 13:53:09 +01004856 dtls_srtp_info->mki_len = 0;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004857 } else {
Johan Pascal2258a4f2020-10-28 13:53:09 +01004858 dtls_srtp_info->mki_len = ssl->dtls_srtp_info.mki_len;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004859 memcpy(dtls_srtp_info->mki_value, ssl->dtls_srtp_info.mki_value,
4860 ssl->dtls_srtp_info.mki_len);
Johan Pascal2258a4f2020-10-28 13:53:09 +01004861 }
Johan Pascalb62bb512015-12-03 21:56:45 +01004862}
Johan Pascalb62bb512015-12-03 21:56:45 +01004863#endif /* MBEDTLS_SSL_DTLS_SRTP */
4864
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004865void mbedtls_ssl_conf_max_version(mbedtls_ssl_config *conf, int major, int minor)
Paul Bakker490ecc82011-10-06 13:04:09 +00004866{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02004867 conf->max_major_ver = major;
4868 conf->max_minor_ver = minor;
Paul Bakker490ecc82011-10-06 13:04:09 +00004869}
4870
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004871void mbedtls_ssl_conf_min_version(mbedtls_ssl_config *conf, int major, int minor)
Paul Bakker1d29fb52012-09-28 13:28:45 +00004872{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02004873 conf->min_major_ver = major;
4874 conf->min_minor_ver = minor;
Paul Bakker1d29fb52012-09-28 13:28:45 +00004875}
4876
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004877#if defined(MBEDTLS_SSL_FALLBACK_SCSV) && defined(MBEDTLS_SSL_CLI_C)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004878void mbedtls_ssl_conf_fallback(mbedtls_ssl_config *conf, char fallback)
Manuel Pégourié-Gonnard1cbd39d2014-10-20 13:34:59 +02004879{
Manuel Pégourié-Gonnard684b0592015-05-06 09:27:31 +01004880 conf->fallback = fallback;
Manuel Pégourié-Gonnard1cbd39d2014-10-20 13:34:59 +02004881}
4882#endif
4883
Janos Follath088ce432017-04-10 12:42:31 +01004884#if defined(MBEDTLS_SSL_SRV_C)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004885void mbedtls_ssl_conf_cert_req_ca_list(mbedtls_ssl_config *conf,
4886 char cert_req_ca_list)
Janos Follath088ce432017-04-10 12:42:31 +01004887{
4888 conf->cert_req_ca_list = cert_req_ca_list;
4889}
4890#endif
4891
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004892#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004893void mbedtls_ssl_conf_encrypt_then_mac(mbedtls_ssl_config *conf, char etm)
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +01004894{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02004895 conf->encrypt_then_mac = etm;
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +01004896}
4897#endif
4898
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004899#if defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004900void mbedtls_ssl_conf_extended_master_secret(mbedtls_ssl_config *conf, char ems)
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +02004901{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02004902 conf->extended_ms = ems;
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +02004903}
4904#endif
4905
Manuel Pégourié-Gonnard66dc5552015-05-14 12:28:21 +02004906#if defined(MBEDTLS_ARC4_C)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004907void mbedtls_ssl_conf_arc4_support(mbedtls_ssl_config *conf, char arc4)
Manuel Pégourié-Gonnardbd47a582015-01-12 13:43:29 +01004908{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02004909 conf->arc4_disabled = arc4;
Manuel Pégourié-Gonnardbd47a582015-01-12 13:43:29 +01004910}
Manuel Pégourié-Gonnard66dc5552015-05-14 12:28:21 +02004911#endif
Manuel Pégourié-Gonnardbd47a582015-01-12 13:43:29 +01004912
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004913#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004914int mbedtls_ssl_conf_max_frag_len(mbedtls_ssl_config *conf, unsigned char mfl_code)
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02004915{
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004916 if (mfl_code >= MBEDTLS_SSL_MAX_FRAG_LEN_INVALID ||
4917 ssl_mfl_code_to_length(mfl_code) > MBEDTLS_TLS_EXT_ADV_CONTENT_LEN) {
4918 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02004919 }
4920
Manuel Pégourié-Gonnard6bf89d62015-05-05 17:01:57 +01004921 conf->mfl_code = mfl_code;
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02004922
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004923 return 0;
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02004924}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004925#endif /* MBEDTLS_SSL_MAX_FRAGMENT_LENGTH */
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02004926
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004927#if defined(MBEDTLS_SSL_TRUNCATED_HMAC)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004928void mbedtls_ssl_conf_truncated_hmac(mbedtls_ssl_config *conf, int truncate)
Manuel Pégourié-Gonnarde980a992013-07-19 11:08:52 +02004929{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02004930 conf->trunc_hmac = truncate;
Manuel Pégourié-Gonnarde980a992013-07-19 11:08:52 +02004931}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004932#endif /* MBEDTLS_SSL_TRUNCATED_HMAC */
Manuel Pégourié-Gonnarde980a992013-07-19 11:08:52 +02004933
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004934#if defined(MBEDTLS_SSL_CBC_RECORD_SPLITTING)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004935void mbedtls_ssl_conf_cbc_record_splitting(mbedtls_ssl_config *conf, char split)
Manuel Pégourié-Gonnardcfa477e2015-01-07 14:50:54 +01004936{
Manuel Pégourié-Gonnard17eab2b2015-05-05 16:34:53 +01004937 conf->cbc_record_splitting = split;
Manuel Pégourié-Gonnardcfa477e2015-01-07 14:50:54 +01004938}
4939#endif
4940
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004941void mbedtls_ssl_conf_legacy_renegotiation(mbedtls_ssl_config *conf, int allow_legacy)
Paul Bakker48916f92012-09-16 19:57:18 +00004942{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02004943 conf->allow_legacy_renegotiation = allow_legacy;
Paul Bakker48916f92012-09-16 19:57:18 +00004944}
4945
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004946#if defined(MBEDTLS_SSL_RENEGOTIATION)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004947void mbedtls_ssl_conf_renegotiation(mbedtls_ssl_config *conf, int renegotiation)
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01004948{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02004949 conf->disable_renegotiation = renegotiation;
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01004950}
4951
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004952void mbedtls_ssl_conf_renegotiation_enforced(mbedtls_ssl_config *conf, int max_records)
Manuel Pégourié-Gonnarda9964db2014-07-03 19:29:16 +02004953{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02004954 conf->renego_max_records = max_records;
Manuel Pégourié-Gonnarda9964db2014-07-03 19:29:16 +02004955}
4956
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004957void mbedtls_ssl_conf_renegotiation_period(mbedtls_ssl_config *conf,
4958 const unsigned char period[8])
Manuel Pégourié-Gonnard837f0fe2014-11-05 13:58:53 +01004959{
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004960 memcpy(conf->renego_period, period, 8);
Manuel Pégourié-Gonnard837f0fe2014-11-05 13:58:53 +01004961}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004962#endif /* MBEDTLS_SSL_RENEGOTIATION */
Paul Bakker5121ce52009-01-03 21:22:43 +00004963
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004964#if defined(MBEDTLS_SSL_SESSION_TICKETS)
Manuel Pégourié-Gonnardb596abf2015-05-20 10:45:29 +02004965#if defined(MBEDTLS_SSL_CLI_C)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004966void mbedtls_ssl_conf_session_tickets(mbedtls_ssl_config *conf, int use_tickets)
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +02004967{
Manuel Pégourié-Gonnard2b494452015-05-06 10:05:11 +01004968 conf->session_tickets = use_tickets;
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +02004969}
Manuel Pégourié-Gonnardb596abf2015-05-20 10:45:29 +02004970#endif
Paul Bakker606b4ba2013-08-14 16:52:14 +02004971
Manuel Pégourié-Gonnardb596abf2015-05-20 10:45:29 +02004972#if defined(MBEDTLS_SSL_SRV_C)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004973void mbedtls_ssl_conf_session_tickets_cb(mbedtls_ssl_config *conf,
4974 mbedtls_ssl_ticket_write_t *f_ticket_write,
4975 mbedtls_ssl_ticket_parse_t *f_ticket_parse,
4976 void *p_ticket)
Paul Bakker606b4ba2013-08-14 16:52:14 +02004977{
Manuel Pégourié-Gonnardd59675d2015-05-19 15:28:00 +02004978 conf->f_ticket_write = f_ticket_write;
4979 conf->f_ticket_parse = f_ticket_parse;
4980 conf->p_ticket = p_ticket;
Paul Bakker606b4ba2013-08-14 16:52:14 +02004981}
Manuel Pégourié-Gonnardb596abf2015-05-20 10:45:29 +02004982#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004983#endif /* MBEDTLS_SSL_SESSION_TICKETS */
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +02004984
Robert Cragie4feb7ae2015-10-02 13:33:37 +01004985#if defined(MBEDTLS_SSL_EXPORT_KEYS)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004986void mbedtls_ssl_conf_export_keys_cb(mbedtls_ssl_config *conf,
4987 mbedtls_ssl_export_keys_t *f_export_keys,
4988 void *p_export_keys)
Robert Cragie4feb7ae2015-10-02 13:33:37 +01004989{
4990 conf->f_export_keys = f_export_keys;
4991 conf->p_export_keys = p_export_keys;
4992}
Ron Eldorf5cc10d2019-05-07 18:33:40 +03004993
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004994void mbedtls_ssl_conf_export_keys_ext_cb(mbedtls_ssl_config *conf,
4995 mbedtls_ssl_export_keys_ext_t *f_export_keys_ext,
4996 void *p_export_keys)
Ron Eldorf5cc10d2019-05-07 18:33:40 +03004997{
4998 conf->f_export_keys_ext = f_export_keys_ext;
4999 conf->p_export_keys = p_export_keys;
5000}
Robert Cragie4feb7ae2015-10-02 13:33:37 +01005001#endif
5002
Gilles Peskineb74a1c72018-04-24 13:09:22 +02005003#if defined(MBEDTLS_SSL_ASYNC_PRIVATE)
Gilles Peskine8bf79f62018-01-05 21:11:53 +01005004void mbedtls_ssl_conf_async_private_cb(
5005 mbedtls_ssl_config *conf,
5006 mbedtls_ssl_async_sign_t *f_async_sign,
5007 mbedtls_ssl_async_decrypt_t *f_async_decrypt,
5008 mbedtls_ssl_async_resume_t *f_async_resume,
5009 mbedtls_ssl_async_cancel_t *f_async_cancel,
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005010 void *async_config_data)
Gilles Peskine8bf79f62018-01-05 21:11:53 +01005011{
5012 conf->f_async_sign_start = f_async_sign;
5013 conf->f_async_decrypt_start = f_async_decrypt;
5014 conf->f_async_resume = f_async_resume;
5015 conf->f_async_cancel = f_async_cancel;
Gilles Peskinedf13d5c2018-04-25 20:39:48 +02005016 conf->p_async_config_data = async_config_data;
5017}
5018
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005019void *mbedtls_ssl_conf_get_async_config_data(const mbedtls_ssl_config *conf)
Gilles Peskine8f97af72018-04-26 11:46:10 +02005020{
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005021 return conf->p_async_config_data;
Gilles Peskine8f97af72018-04-26 11:46:10 +02005022}
5023
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005024void *mbedtls_ssl_get_async_operation_data(const mbedtls_ssl_context *ssl)
Gilles Peskinedf13d5c2018-04-25 20:39:48 +02005025{
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005026 if (ssl->handshake == NULL) {
5027 return NULL;
5028 } else {
5029 return ssl->handshake->user_async_ctx;
5030 }
Gilles Peskinedf13d5c2018-04-25 20:39:48 +02005031}
5032
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005033void mbedtls_ssl_set_async_operation_data(mbedtls_ssl_context *ssl,
5034 void *ctx)
Gilles Peskinedf13d5c2018-04-25 20:39:48 +02005035{
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005036 if (ssl->handshake != NULL) {
Gilles Peskinedf13d5c2018-04-25 20:39:48 +02005037 ssl->handshake->user_async_ctx = ctx;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005038 }
Gilles Peskine8bf79f62018-01-05 21:11:53 +01005039}
Gilles Peskineb74a1c72018-04-24 13:09:22 +02005040#endif /* MBEDTLS_SSL_ASYNC_PRIVATE */
Gilles Peskine8bf79f62018-01-05 21:11:53 +01005041
Paul Bakker5121ce52009-01-03 21:22:43 +00005042/*
5043 * SSL get accessors
5044 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005045uint32_t mbedtls_ssl_get_verify_result(const mbedtls_ssl_context *ssl)
Paul Bakker5121ce52009-01-03 21:22:43 +00005046{
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005047 if (ssl->session != NULL) {
5048 return ssl->session->verify_result;
5049 }
Manuel Pégourié-Gonnarde89163c2015-01-23 14:30:57 +00005050
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005051 if (ssl->session_negotiate != NULL) {
5052 return ssl->session_negotiate->verify_result;
5053 }
Manuel Pégourié-Gonnarde89163c2015-01-23 14:30:57 +00005054
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005055 return 0xFFFFFFFF;
Paul Bakker5121ce52009-01-03 21:22:43 +00005056}
5057
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005058const char *mbedtls_ssl_get_ciphersuite(const mbedtls_ssl_context *ssl)
Paul Bakker72f62662011-01-16 21:27:44 +00005059{
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005060 if (ssl == NULL || ssl->session == NULL) {
5061 return NULL;
5062 }
Paul Bakker926c8e42013-03-06 10:23:34 +01005063
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005064 return mbedtls_ssl_get_ciphersuite_name(ssl->session->ciphersuite);
Paul Bakker72f62662011-01-16 21:27:44 +00005065}
5066
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005067const char *mbedtls_ssl_get_version(const mbedtls_ssl_context *ssl)
Paul Bakker43ca69c2011-01-15 17:35:19 +00005068{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005069#if defined(MBEDTLS_SSL_PROTO_DTLS)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005070 if (ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM) {
5071 switch (ssl->minor_ver) {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005072 case MBEDTLS_SSL_MINOR_VERSION_2:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005073 return "DTLSv1.0";
Manuel Pégourié-Gonnardb21ca2a2014-02-10 13:43:33 +01005074
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005075 case MBEDTLS_SSL_MINOR_VERSION_3:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005076 return "DTLSv1.2";
Manuel Pégourié-Gonnardb21ca2a2014-02-10 13:43:33 +01005077
5078 default:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005079 return "unknown (DTLS)";
Manuel Pégourié-Gonnardb21ca2a2014-02-10 13:43:33 +01005080 }
5081 }
5082#endif
5083
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005084 switch (ssl->minor_ver) {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005085 case MBEDTLS_SSL_MINOR_VERSION_0:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005086 return "SSLv3.0";
Paul Bakker43ca69c2011-01-15 17:35:19 +00005087
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005088 case MBEDTLS_SSL_MINOR_VERSION_1:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005089 return "TLSv1.0";
Paul Bakker43ca69c2011-01-15 17:35:19 +00005090
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005091 case MBEDTLS_SSL_MINOR_VERSION_2:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005092 return "TLSv1.1";
Paul Bakker43ca69c2011-01-15 17:35:19 +00005093
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005094 case MBEDTLS_SSL_MINOR_VERSION_3:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005095 return "TLSv1.2";
Paul Bakker1ef83d62012-04-11 12:09:53 +00005096
Paul Bakker43ca69c2011-01-15 17:35:19 +00005097 default:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005098 return "unknown";
Paul Bakker43ca69c2011-01-15 17:35:19 +00005099 }
Paul Bakker43ca69c2011-01-15 17:35:19 +00005100}
5101
Manuel Pégourié-Gonnarda2cda6b2015-08-31 18:30:52 +02005102#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005103size_t mbedtls_ssl_get_input_max_frag_len(const mbedtls_ssl_context *ssl)
Andrzej Kurek90c6e842020-04-03 05:25:29 -04005104{
5105 size_t max_len = MBEDTLS_SSL_MAX_CONTENT_LEN;
5106 size_t read_mfl;
5107
5108 /* Use the configured MFL for the client if we're past SERVER_HELLO_DONE */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005109 if (ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT &&
5110 ssl->state >= MBEDTLS_SSL_SERVER_HELLO_DONE) {
5111 return ssl_mfl_code_to_length(ssl->conf->mfl_code);
Andrzej Kurek90c6e842020-04-03 05:25:29 -04005112 }
5113
5114 /* Check if a smaller max length was negotiated */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005115 if (ssl->session_out != NULL) {
5116 read_mfl = ssl_mfl_code_to_length(ssl->session_out->mfl_code);
5117 if (read_mfl < max_len) {
Andrzej Kurek90c6e842020-04-03 05:25:29 -04005118 max_len = read_mfl;
5119 }
5120 }
5121
5122 // During a handshake, use the value being negotiated
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005123 if (ssl->session_negotiate != NULL) {
5124 read_mfl = ssl_mfl_code_to_length(ssl->session_negotiate->mfl_code);
5125 if (read_mfl < max_len) {
Andrzej Kurek90c6e842020-04-03 05:25:29 -04005126 max_len = read_mfl;
5127 }
5128 }
5129
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005130 return max_len;
Andrzej Kurek90c6e842020-04-03 05:25:29 -04005131}
5132
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005133size_t mbedtls_ssl_get_output_max_frag_len(const mbedtls_ssl_context *ssl)
Manuel Pégourié-Gonnarda2cda6b2015-08-31 18:30:52 +02005134{
5135 size_t max_len;
5136
5137 /*
5138 * Assume mfl_code is correct since it was checked when set
5139 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005140 max_len = ssl_mfl_code_to_length(ssl->conf->mfl_code);
Manuel Pégourié-Gonnarda2cda6b2015-08-31 18:30:52 +02005141
Manuel Pégourié-Gonnard2cb17e22017-09-19 13:00:47 +02005142 /* Check if a smaller max length was negotiated */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005143 if (ssl->session_out != NULL &&
5144 ssl_mfl_code_to_length(ssl->session_out->mfl_code) < max_len) {
5145 max_len = ssl_mfl_code_to_length(ssl->session_out->mfl_code);
Manuel Pégourié-Gonnarda2cda6b2015-08-31 18:30:52 +02005146 }
5147
Manuel Pégourié-Gonnard2cb17e22017-09-19 13:00:47 +02005148 /* During a handshake, use the value being negotiated */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005149 if (ssl->session_negotiate != NULL &&
5150 ssl_mfl_code_to_length(ssl->session_negotiate->mfl_code) < max_len) {
5151 max_len = ssl_mfl_code_to_length(ssl->session_negotiate->mfl_code);
Manuel Pégourié-Gonnard2cb17e22017-09-19 13:00:47 +02005152 }
5153
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005154 return max_len;
Manuel Pégourié-Gonnarda2cda6b2015-08-31 18:30:52 +02005155}
Andrzej Kurek90c6e842020-04-03 05:25:29 -04005156
5157#if !defined(MBEDTLS_DEPRECATED_REMOVED)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005158size_t mbedtls_ssl_get_max_frag_len(const mbedtls_ssl_context *ssl)
Andrzej Kurek90c6e842020-04-03 05:25:29 -04005159{
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005160 return mbedtls_ssl_get_output_max_frag_len(ssl);
Andrzej Kurek90c6e842020-04-03 05:25:29 -04005161}
5162#endif /* !MBEDTLS_DEPRECATED_REMOVED */
Manuel Pégourié-Gonnarda2cda6b2015-08-31 18:30:52 +02005163#endif /* MBEDTLS_SSL_MAX_FRAGMENT_LENGTH */
5164
Manuel Pégourié-Gonnardb8eec192018-08-20 09:34:02 +02005165#if defined(MBEDTLS_SSL_PROTO_DTLS)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005166size_t mbedtls_ssl_get_current_mtu(const mbedtls_ssl_context *ssl)
Manuel Pégourié-Gonnardb8eec192018-08-20 09:34:02 +02005167{
Andrzej Kurekef43ce62018-10-09 08:24:12 -04005168 /* Return unlimited mtu for client hello messages to avoid fragmentation. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005169 if (ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT &&
5170 (ssl->state == MBEDTLS_SSL_CLIENT_HELLO ||
5171 ssl->state == MBEDTLS_SSL_SERVER_HELLO)) {
5172 return 0;
5173 }
Andrzej Kurekef43ce62018-10-09 08:24:12 -04005174
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005175 if (ssl->handshake == NULL || ssl->handshake->mtu == 0) {
5176 return ssl->mtu;
5177 }
Manuel Pégourié-Gonnardb8eec192018-08-20 09:34:02 +02005178
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005179 if (ssl->mtu == 0) {
5180 return ssl->handshake->mtu;
5181 }
Manuel Pégourié-Gonnardb8eec192018-08-20 09:34:02 +02005182
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005183 return ssl->mtu < ssl->handshake->mtu ?
5184 ssl->mtu : ssl->handshake->mtu;
Manuel Pégourié-Gonnardb8eec192018-08-20 09:34:02 +02005185}
5186#endif /* MBEDTLS_SSL_PROTO_DTLS */
5187
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005188int mbedtls_ssl_get_max_out_record_payload(const mbedtls_ssl_context *ssl)
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02005189{
5190 size_t max_len = MBEDTLS_SSL_OUT_CONTENT_LEN;
5191
Manuel Pégourié-Gonnard000281e2018-08-21 11:20:58 +02005192#if !defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH) && \
5193 !defined(MBEDTLS_SSL_PROTO_DTLS)
5194 (void) ssl;
5195#endif
5196
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02005197#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005198 const size_t mfl = mbedtls_ssl_get_output_max_frag_len(ssl);
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02005199
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005200 if (max_len > mfl) {
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02005201 max_len = mfl;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005202 }
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02005203#endif
5204
5205#if defined(MBEDTLS_SSL_PROTO_DTLS)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005206 if (mbedtls_ssl_get_current_mtu(ssl) != 0) {
5207 const size_t mtu = mbedtls_ssl_get_current_mtu(ssl);
5208 const int ret = mbedtls_ssl_get_record_expansion(ssl);
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02005209 const size_t overhead = (size_t) ret;
5210
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005211 if (ret < 0) {
5212 return ret;
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02005213 }
5214
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005215 if (mtu <= overhead) {
5216 MBEDTLS_SSL_DEBUG_MSG(1, ("MTU too low for record expansion"));
5217 return MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
5218 }
5219
5220 if (max_len > mtu - overhead) {
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02005221 max_len = mtu - overhead;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005222 }
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02005223 }
Manuel Pégourié-Gonnardb8eec192018-08-20 09:34:02 +02005224#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02005225
Hanno Becker0defedb2018-08-10 12:35:02 +01005226#if !defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH) && \
5227 !defined(MBEDTLS_SSL_PROTO_DTLS)
5228 ((void) ssl);
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02005229#endif
5230
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005231 return (int) max_len;
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02005232}
5233
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005234#if defined(MBEDTLS_X509_CRT_PARSE_C)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005235const mbedtls_x509_crt *mbedtls_ssl_get_peer_cert(const mbedtls_ssl_context *ssl)
Paul Bakkerb0550d92012-10-30 07:51:03 +00005236{
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005237 if (ssl == NULL || ssl->session == NULL) {
5238 return NULL;
5239 }
Paul Bakkerb0550d92012-10-30 07:51:03 +00005240
Hanno Beckere6824572019-02-07 13:18:46 +00005241#if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005242 return ssl->session->peer_cert;
Hanno Beckere6824572019-02-07 13:18:46 +00005243#else
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005244 return NULL;
Hanno Beckere6824572019-02-07 13:18:46 +00005245#endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
Paul Bakkerb0550d92012-10-30 07:51:03 +00005246}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005247#endif /* MBEDTLS_X509_CRT_PARSE_C */
Paul Bakkerb0550d92012-10-30 07:51:03 +00005248
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005249#if defined(MBEDTLS_SSL_CLI_C)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005250int mbedtls_ssl_get_session(const mbedtls_ssl_context *ssl,
5251 mbedtls_ssl_session *dst)
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02005252{
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005253 if (ssl == NULL ||
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02005254 dst == NULL ||
5255 ssl->session == NULL ||
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005256 ssl->conf->endpoint != MBEDTLS_SSL_IS_CLIENT) {
5257 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02005258 }
5259
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005260 return mbedtls_ssl_session_copy(dst, ssl->session);
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02005261}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005262#endif /* MBEDTLS_SSL_CLI_C */
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02005263
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005264const mbedtls_ssl_session *mbedtls_ssl_get_session_pointer(const mbedtls_ssl_context *ssl)
Manuel Pégourié-Gonnardb5e4e0a2019-05-20 11:12:28 +02005265{
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005266 if (ssl == NULL) {
5267 return NULL;
5268 }
Manuel Pégourié-Gonnardb5e4e0a2019-05-20 11:12:28 +02005269
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005270 return ssl->session;
Manuel Pégourié-Gonnardb5e4e0a2019-05-20 11:12:28 +02005271}
5272
Paul Bakker5121ce52009-01-03 21:22:43 +00005273/*
Hanno Beckera835da52019-05-16 12:39:07 +01005274 * Define ticket header determining Mbed TLS version
5275 * and structure of the ticket.
5276 */
5277
Hanno Becker94ef3b32019-05-16 12:50:45 +01005278/*
Hanno Becker50b59662019-05-28 14:30:45 +01005279 * Define bitflag determining compile-time settings influencing
5280 * structure of serialized SSL sessions.
Hanno Becker94ef3b32019-05-16 12:50:45 +01005281 */
5282
Hanno Becker50b59662019-05-28 14:30:45 +01005283#if defined(MBEDTLS_HAVE_TIME)
Hanno Becker3e088662019-05-29 11:10:18 +01005284#define SSL_SERIALIZED_SESSION_CONFIG_TIME 1
Hanno Becker50b59662019-05-28 14:30:45 +01005285#else
Hanno Becker3e088662019-05-29 11:10:18 +01005286#define SSL_SERIALIZED_SESSION_CONFIG_TIME 0
Hanno Becker94ef3b32019-05-16 12:50:45 +01005287#endif /* MBEDTLS_HAVE_TIME */
5288
5289#if defined(MBEDTLS_X509_CRT_PARSE_C)
Hanno Becker3e088662019-05-29 11:10:18 +01005290#define SSL_SERIALIZED_SESSION_CONFIG_CRT 1
Hanno Becker94ef3b32019-05-16 12:50:45 +01005291#else
Hanno Becker3e088662019-05-29 11:10:18 +01005292#define SSL_SERIALIZED_SESSION_CONFIG_CRT 0
Hanno Becker94ef3b32019-05-16 12:50:45 +01005293#endif /* MBEDTLS_X509_CRT_PARSE_C */
5294
David Horstmanneb77b6f2024-02-13 17:53:35 +00005295#if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
David Horstmann11def972024-03-01 11:20:32 +00005296#define SSL_SERIALIZED_SESSION_CONFIG_KEEP_PEER_CRT 1
David Horstmanneb77b6f2024-02-13 17:53:35 +00005297#else
David Horstmann11def972024-03-01 11:20:32 +00005298#define SSL_SERIALIZED_SESSION_CONFIG_KEEP_PEER_CRT 0
David Horstmanneb77b6f2024-02-13 17:53:35 +00005299#endif /* MBEDTLS_SSL_SESSION_TICKETS */
5300
Hanno Becker94ef3b32019-05-16 12:50:45 +01005301#if defined(MBEDTLS_SSL_CLI_C) && defined(MBEDTLS_SSL_SESSION_TICKETS)
Hanno Becker3e088662019-05-29 11:10:18 +01005302#define SSL_SERIALIZED_SESSION_CONFIG_CLIENT_TICKET 1
Hanno Becker94ef3b32019-05-16 12:50:45 +01005303#else
Hanno Becker3e088662019-05-29 11:10:18 +01005304#define SSL_SERIALIZED_SESSION_CONFIG_CLIENT_TICKET 0
Hanno Becker94ef3b32019-05-16 12:50:45 +01005305#endif /* MBEDTLS_SSL_CLI_C && MBEDTLS_SSL_SESSION_TICKETS */
5306
5307#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
Hanno Becker3e088662019-05-29 11:10:18 +01005308#define SSL_SERIALIZED_SESSION_CONFIG_MFL 1
Hanno Becker94ef3b32019-05-16 12:50:45 +01005309#else
Hanno Becker3e088662019-05-29 11:10:18 +01005310#define SSL_SERIALIZED_SESSION_CONFIG_MFL 0
Hanno Becker94ef3b32019-05-16 12:50:45 +01005311#endif /* MBEDTLS_SSL_MAX_FRAGMENT_LENGTH */
5312
5313#if defined(MBEDTLS_SSL_TRUNCATED_HMAC)
Hanno Becker3e088662019-05-29 11:10:18 +01005314#define SSL_SERIALIZED_SESSION_CONFIG_TRUNC_HMAC 1
Hanno Becker94ef3b32019-05-16 12:50:45 +01005315#else
Hanno Becker3e088662019-05-29 11:10:18 +01005316#define SSL_SERIALIZED_SESSION_CONFIG_TRUNC_HMAC 0
Hanno Becker94ef3b32019-05-16 12:50:45 +01005317#endif /* MBEDTLS_SSL_TRUNCATED_HMAC */
5318
5319#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
Hanno Becker3e088662019-05-29 11:10:18 +01005320#define SSL_SERIALIZED_SESSION_CONFIG_ETM 1
Hanno Becker94ef3b32019-05-16 12:50:45 +01005321#else
Hanno Becker3e088662019-05-29 11:10:18 +01005322#define SSL_SERIALIZED_SESSION_CONFIG_ETM 0
Hanno Becker94ef3b32019-05-16 12:50:45 +01005323#endif /* MBEDTLS_SSL_ENCRYPT_THEN_MAC */
5324
Hanno Becker94ef3b32019-05-16 12:50:45 +01005325#if defined(MBEDTLS_SSL_SESSION_TICKETS)
5326#define SSL_SERIALIZED_SESSION_CONFIG_TICKET 1
5327#else
5328#define SSL_SERIALIZED_SESSION_CONFIG_TICKET 0
5329#endif /* MBEDTLS_SSL_SESSION_TICKETS */
5330
Hanno Becker3e088662019-05-29 11:10:18 +01005331#define SSL_SERIALIZED_SESSION_CONFIG_TIME_BIT 0
5332#define SSL_SERIALIZED_SESSION_CONFIG_CRT_BIT 1
5333#define SSL_SERIALIZED_SESSION_CONFIG_CLIENT_TICKET_BIT 2
5334#define SSL_SERIALIZED_SESSION_CONFIG_MFL_BIT 3
5335#define SSL_SERIALIZED_SESSION_CONFIG_TRUNC_HMAC_BIT 4
5336#define SSL_SERIALIZED_SESSION_CONFIG_ETM_BIT 5
5337#define SSL_SERIALIZED_SESSION_CONFIG_TICKET_BIT 6
David Horstmann11def972024-03-01 11:20:32 +00005338#define SSL_SERIALIZED_SESSION_CONFIG_KEEP_PEER_CRT_BIT 7
Hanno Becker3e088662019-05-29 11:10:18 +01005339
Hanno Becker50b59662019-05-28 14:30:45 +01005340#define SSL_SERIALIZED_SESSION_CONFIG_BITFLAG \
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005341 ((uint16_t) ( \
5342 (SSL_SERIALIZED_SESSION_CONFIG_TIME << SSL_SERIALIZED_SESSION_CONFIG_TIME_BIT) | \
5343 (SSL_SERIALIZED_SESSION_CONFIG_CRT << SSL_SERIALIZED_SESSION_CONFIG_CRT_BIT) | \
5344 (SSL_SERIALIZED_SESSION_CONFIG_CLIENT_TICKET << \
5345 SSL_SERIALIZED_SESSION_CONFIG_CLIENT_TICKET_BIT) | \
5346 (SSL_SERIALIZED_SESSION_CONFIG_MFL << SSL_SERIALIZED_SESSION_CONFIG_MFL_BIT) | \
5347 (SSL_SERIALIZED_SESSION_CONFIG_TRUNC_HMAC << \
5348 SSL_SERIALIZED_SESSION_CONFIG_TRUNC_HMAC_BIT) | \
5349 (SSL_SERIALIZED_SESSION_CONFIG_ETM << SSL_SERIALIZED_SESSION_CONFIG_ETM_BIT) | \
David Horstmanneb77b6f2024-02-13 17:53:35 +00005350 (SSL_SERIALIZED_SESSION_CONFIG_TICKET << SSL_SERIALIZED_SESSION_CONFIG_TICKET_BIT) | \
David Horstmannf5a6fa22024-03-01 12:31:35 +00005351 (SSL_SERIALIZED_SESSION_CONFIG_KEEP_PEER_CRT << \
5352 SSL_SERIALIZED_SESSION_CONFIG_KEEP_PEER_CRT_BIT)))
Hanno Becker94ef3b32019-05-16 12:50:45 +01005353
Chien Wongb6d57932024-02-07 21:48:12 +08005354static const unsigned char ssl_serialized_session_header[] = {
Hanno Becker94ef3b32019-05-16 12:50:45 +01005355 MBEDTLS_VERSION_MAJOR,
5356 MBEDTLS_VERSION_MINOR,
5357 MBEDTLS_VERSION_PATCH,
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005358 MBEDTLS_BYTE_1(SSL_SERIALIZED_SESSION_CONFIG_BITFLAG),
5359 MBEDTLS_BYTE_0(SSL_SERIALIZED_SESSION_CONFIG_BITFLAG),
Hanno Beckerf8787072019-05-16 12:41:07 +01005360};
Hanno Beckera835da52019-05-16 12:39:07 +01005361
5362/*
Manuel Pégourié-Gonnarda3e7c652019-05-16 10:08:35 +02005363 * Serialize a session in the following format:
Manuel Pégourié-Gonnard35eb8022019-05-16 11:11:08 +02005364 * (in the presentation language of TLS, RFC 8446 section 3)
Manuel Pégourié-Gonnarda3e7c652019-05-16 10:08:35 +02005365 *
Hanno Becker50b59662019-05-28 14:30:45 +01005366 * opaque mbedtls_version[3]; // major, minor, patch
5367 * opaque session_format[2]; // version-specific 16-bit field determining
5368 * // the format of the remaining
5369 * // serialized data.
Hanno Beckerdc28b6c2019-05-29 11:08:00 +01005370 *
5371 * Note: When updating the format, remember to keep
5372 * these version+format bytes.
5373 *
Hanno Beckerbe34e8e2019-06-04 09:43:16 +01005374 * // In this version, `session_format` determines
5375 * // the setting of those compile-time
5376 * // configuration options which influence
Hanno Becker50b59662019-05-28 14:30:45 +01005377 * // the structure of mbedtls_ssl_session.
David Horstmann363db772024-03-01 12:11:24 +00005378 * #if defined(MBEDTLS_HAVE_TIME)
5379 * uint64 start_time;
5380 * #endif
5381 * uint8 ciphersuite[2]; // defined by the standard
5382 * uint8 compression; // 0 or 1
5383 * uint8 session_id_len; // at most 32
5384 * opaque session_id[32];
5385 * opaque master[48]; // fixed length in the standard
5386 * uint32 verify_result;
David Horstmannfc8cacf2024-03-04 16:37:02 +00005387 * #if defined(MBEDTLS_X509_CRT_PARSE_C)
David Horstmann363db772024-03-01 12:11:24 +00005388 * #if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
5389 * opaque peer_cert<0..2^24-1>; // length 0 means no peer cert
5390 * #else
5391 * uint8 peer_cert_digest_type;
5392 * opaque peer_cert_digest<0..2^8-1>
5393 * #endif
David Horstmannfc8cacf2024-03-04 16:37:02 +00005394 * #endif
5395 * #if defined(MBEDTLS_SSL_SESSION_TICKETS) && defined(MBEDTLS_SSL_CLI_C)
David Horstmann363db772024-03-01 12:11:24 +00005396 * opaque ticket<0..2^24-1>; // length 0 means no ticket
5397 * uint32 ticket_lifetime;
5398 * #endif
5399 * #if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
5400 * uint8 mfl_code; // up to 255 according to standard
5401 * #endif
5402 * #if defined(MBEDTLS_SSL_TRUNCATED_HMAC)
5403 * uint8 trunc_hmac; // 0 or 1
5404 * #endif
5405 * #if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
5406 * uint8 encrypt_then_mac; // 0 or 1
5407 * #endif
Manuel Pégourié-Gonnarda3e7c652019-05-16 10:08:35 +02005408 *
Manuel Pégourié-Gonnardf743c032019-05-24 12:06:29 +02005409 * The order is the same as in the definition of the structure, except
5410 * verify_result is put before peer_cert so that all mandatory fields come
5411 * together in one block.
Manuel Pégourié-Gonnarda3e7c652019-05-16 10:08:35 +02005412 */
Manuel Pégourié-Gonnardd904d662022-06-17 10:24:00 +02005413MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005414static int ssl_session_save(const mbedtls_ssl_session *session,
5415 unsigned char omit_header,
5416 unsigned char *buf,
5417 size_t buf_len,
5418 size_t *olen)
Manuel Pégourié-Gonnarda3e7c652019-05-16 10:08:35 +02005419{
5420 unsigned char *p = buf;
Manuel Pégourié-Gonnard26f982f2019-05-21 11:01:32 +02005421 size_t used = 0;
Manuel Pégourié-Gonnardf743c032019-05-24 12:06:29 +02005422#if defined(MBEDTLS_HAVE_TIME)
5423 uint64_t start;
5424#endif
Manuel Pégourié-Gonnarda3e7c652019-05-16 10:08:35 +02005425#if defined(MBEDTLS_X509_CRT_PARSE_C)
5426#if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
5427 size_t cert_len;
Manuel Pégourié-Gonnarda3e7c652019-05-16 10:08:35 +02005428#endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
5429#endif /* MBEDTLS_X509_CRT_PARSE_C */
5430
Manuel Pégourié-Gonnarda3e7c652019-05-16 10:08:35 +02005431
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005432 if (!omit_header) {
Manuel Pégourié-Gonnard45ac1f02019-07-23 16:52:45 +02005433 /*
5434 * Add version identifier
5435 */
5436
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005437 used += sizeof(ssl_serialized_session_header);
Manuel Pégourié-Gonnard45ac1f02019-07-23 16:52:45 +02005438
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005439 if (used <= buf_len) {
5440 memcpy(p, ssl_serialized_session_header,
5441 sizeof(ssl_serialized_session_header));
5442 p += sizeof(ssl_serialized_session_header);
Manuel Pégourié-Gonnard45ac1f02019-07-23 16:52:45 +02005443 }
Hanno Beckera835da52019-05-16 12:39:07 +01005444 }
5445
5446 /*
Manuel Pégourié-Gonnardf743c032019-05-24 12:06:29 +02005447 * Time
5448 */
5449#if defined(MBEDTLS_HAVE_TIME)
5450 used += 8;
5451
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005452 if (used <= buf_len) {
Manuel Pégourié-Gonnardf743c032019-05-24 12:06:29 +02005453 start = (uint64_t) session->start;
5454
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005455 MBEDTLS_PUT_UINT64_BE(start, p, 0);
Joe Subbiani2f98d792021-08-20 11:44:44 +01005456 p += 8;
Manuel Pégourié-Gonnardf743c032019-05-24 12:06:29 +02005457 }
5458#endif /* MBEDTLS_HAVE_TIME */
5459
5460 /*
5461 * Basic mandatory fields
5462 */
5463 used += 2 /* ciphersuite */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005464 + 1 /* compression */
5465 + 1 /* id_len */
5466 + sizeof(session->id)
5467 + sizeof(session->master)
5468 + 4; /* verify_result */
Manuel Pégourié-Gonnardf743c032019-05-24 12:06:29 +02005469
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005470 if (used <= buf_len) {
5471 MBEDTLS_PUT_UINT16_BE(session->ciphersuite, p, 0);
Joe Subbiani2f98d792021-08-20 11:44:44 +01005472 p += 2;
Manuel Pégourié-Gonnardf743c032019-05-24 12:06:29 +02005473
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005474 *p++ = MBEDTLS_BYTE_0(session->compression);
Manuel Pégourié-Gonnardf743c032019-05-24 12:06:29 +02005475
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005476 *p++ = MBEDTLS_BYTE_0(session->id_len);
5477 memcpy(p, session->id, 32);
Manuel Pégourié-Gonnardf743c032019-05-24 12:06:29 +02005478 p += 32;
5479
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005480 memcpy(p, session->master, 48);
Manuel Pégourié-Gonnardf743c032019-05-24 12:06:29 +02005481 p += 48;
5482
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005483 MBEDTLS_PUT_UINT32_BE(session->verify_result, p, 0);
Joe Subbiani2f98d792021-08-20 11:44:44 +01005484 p += 4;
Manuel Pégourié-Gonnard26f982f2019-05-21 11:01:32 +02005485 }
Manuel Pégourié-Gonnarda3e7c652019-05-16 10:08:35 +02005486
Manuel Pégourié-Gonnard35eb8022019-05-16 11:11:08 +02005487 /*
Manuel Pégourié-Gonnardf743c032019-05-24 12:06:29 +02005488 * Peer's end-entity certificate
Manuel Pégourié-Gonnard35eb8022019-05-16 11:11:08 +02005489 */
Manuel Pégourié-Gonnarda3e7c652019-05-16 10:08:35 +02005490#if defined(MBEDTLS_X509_CRT_PARSE_C)
5491#if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005492 if (session->peer_cert == NULL) {
Manuel Pégourié-Gonnarda3e7c652019-05-16 10:08:35 +02005493 cert_len = 0;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005494 } else {
Manuel Pégourié-Gonnarda3e7c652019-05-16 10:08:35 +02005495 cert_len = session->peer_cert->raw.len;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005496 }
Manuel Pégourié-Gonnarda3e7c652019-05-16 10:08:35 +02005497
Manuel Pégourié-Gonnard26f982f2019-05-21 11:01:32 +02005498 used += 3 + cert_len;
Manuel Pégourié-Gonnarda3e7c652019-05-16 10:08:35 +02005499
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005500 if (used <= buf_len) {
5501 *p++ = MBEDTLS_BYTE_2(cert_len);
5502 *p++ = MBEDTLS_BYTE_1(cert_len);
5503 *p++ = MBEDTLS_BYTE_0(cert_len);
Manuel Pégourié-Gonnarda3e7c652019-05-16 10:08:35 +02005504
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005505 if (session->peer_cert != NULL) {
5506 memcpy(p, session->peer_cert->raw.p, cert_len);
Manuel Pégourié-Gonnard26f982f2019-05-21 11:01:32 +02005507 p += cert_len;
5508 }
5509 }
Manuel Pégourié-Gonnarda3e7c652019-05-16 10:08:35 +02005510#else /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005511 if (session->peer_cert_digest != NULL) {
Manuel Pégourié-Gonnardf743c032019-05-24 12:06:29 +02005512 used += 1 /* type */ + 1 /* length */ + session->peer_cert_digest_len;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005513 if (used <= buf_len) {
Manuel Pégourié-Gonnardf743c032019-05-24 12:06:29 +02005514 *p++ = (unsigned char) session->peer_cert_digest_type;
5515 *p++ = (unsigned char) session->peer_cert_digest_len;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005516 memcpy(p, session->peer_cert_digest,
5517 session->peer_cert_digest_len);
Manuel Pégourié-Gonnardf743c032019-05-24 12:06:29 +02005518 p += session->peer_cert_digest_len;
5519 }
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005520 } else {
Manuel Pégourié-Gonnardf743c032019-05-24 12:06:29 +02005521 used += 2;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005522 if (used <= buf_len) {
Manuel Pégourié-Gonnardf743c032019-05-24 12:06:29 +02005523 *p++ = (unsigned char) MBEDTLS_MD_NONE;
5524 *p++ = 0;
5525 }
Manuel Pégourié-Gonnard26f982f2019-05-21 11:01:32 +02005526 }
Manuel Pégourié-Gonnarda3e7c652019-05-16 10:08:35 +02005527#endif /* !MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
5528#endif /* MBEDTLS_X509_CRT_PARSE_C */
5529
Manuel Pégourié-Gonnard35eb8022019-05-16 11:11:08 +02005530 /*
Manuel Pégourié-Gonnardf743c032019-05-24 12:06:29 +02005531 * Session ticket if any, plus associated data
Manuel Pégourié-Gonnard35eb8022019-05-16 11:11:08 +02005532 */
5533#if defined(MBEDTLS_SSL_SESSION_TICKETS) && defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnardf743c032019-05-24 12:06:29 +02005534 used += 3 + session->ticket_len + 4; /* len + ticket + lifetime */
Manuel Pégourié-Gonnard35eb8022019-05-16 11:11:08 +02005535
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005536 if (used <= buf_len) {
5537 *p++ = MBEDTLS_BYTE_2(session->ticket_len);
5538 *p++ = MBEDTLS_BYTE_1(session->ticket_len);
5539 *p++ = MBEDTLS_BYTE_0(session->ticket_len);
Manuel Pégourié-Gonnard26f982f2019-05-21 11:01:32 +02005540
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005541 if (session->ticket != NULL) {
5542 memcpy(p, session->ticket, session->ticket_len);
Manuel Pégourié-Gonnard26f982f2019-05-21 11:01:32 +02005543 p += session->ticket_len;
5544 }
Manuel Pégourié-Gonnardf743c032019-05-24 12:06:29 +02005545
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005546 MBEDTLS_PUT_UINT32_BE(session->ticket_lifetime, p, 0);
Joe Subbiani2f98d792021-08-20 11:44:44 +01005547 p += 4;
Manuel Pégourié-Gonnard35eb8022019-05-16 11:11:08 +02005548 }
5549#endif /* MBEDTLS_SSL_SESSION_TICKETS && MBEDTLS_SSL_CLI_C */
5550
Manuel Pégourié-Gonnardf743c032019-05-24 12:06:29 +02005551 /*
5552 * Misc extension-related info
5553 */
5554#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
5555 used += 1;
5556
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005557 if (used <= buf_len) {
Manuel Pégourié-Gonnardf743c032019-05-24 12:06:29 +02005558 *p++ = session->mfl_code;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005559 }
Manuel Pégourié-Gonnardf743c032019-05-24 12:06:29 +02005560#endif
5561
5562#if defined(MBEDTLS_SSL_TRUNCATED_HMAC)
5563 used += 1;
5564
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005565 if (used <= buf_len) {
5566 *p++ = (unsigned char) ((session->trunc_hmac) & 0xFF);
5567 }
Manuel Pégourié-Gonnardf743c032019-05-24 12:06:29 +02005568#endif
5569
5570#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
5571 used += 1;
5572
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005573 if (used <= buf_len) {
5574 *p++ = MBEDTLS_BYTE_0(session->encrypt_then_mac);
5575 }
Manuel Pégourié-Gonnardf743c032019-05-24 12:06:29 +02005576#endif
5577
Manuel Pégourié-Gonnard35eb8022019-05-16 11:11:08 +02005578 /* Done */
Manuel Pégourié-Gonnard26f982f2019-05-21 11:01:32 +02005579 *olen = used;
5580
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005581 if (used > buf_len) {
5582 return MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL;
5583 }
Manuel Pégourié-Gonnarda3e7c652019-05-16 10:08:35 +02005584
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005585 return 0;
Manuel Pégourié-Gonnarda3e7c652019-05-16 10:08:35 +02005586}
5587
5588/*
Manuel Pégourié-Gonnard45ac1f02019-07-23 16:52:45 +02005589 * Public wrapper for ssl_session_save()
5590 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005591int mbedtls_ssl_session_save(const mbedtls_ssl_session *session,
5592 unsigned char *buf,
5593 size_t buf_len,
5594 size_t *olen)
Manuel Pégourié-Gonnard45ac1f02019-07-23 16:52:45 +02005595{
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005596 return ssl_session_save(session, 0, buf, buf_len, olen);
Manuel Pégourié-Gonnard45ac1f02019-07-23 16:52:45 +02005597}
5598
5599/*
Manuel Pégourié-Gonnardb9dfc9f2019-07-12 10:50:19 +02005600 * Deserialize session, see mbedtls_ssl_session_save() for format.
Manuel Pégourié-Gonnarda3d831b2019-05-23 12:28:45 +02005601 *
5602 * This internal version is wrapped by a public function that cleans up in
Manuel Pégourié-Gonnard45ac1f02019-07-23 16:52:45 +02005603 * case of error, and has an extra option omit_header.
Manuel Pégourié-Gonnarda3e7c652019-05-16 10:08:35 +02005604 */
Manuel Pégourié-Gonnardd904d662022-06-17 10:24:00 +02005605MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005606static int ssl_session_load(mbedtls_ssl_session *session,
5607 unsigned char omit_header,
5608 const unsigned char *buf,
5609 size_t len)
Manuel Pégourié-Gonnarda3e7c652019-05-16 10:08:35 +02005610{
5611 const unsigned char *p = buf;
5612 const unsigned char * const end = buf + len;
Manuel Pégourié-Gonnardf743c032019-05-24 12:06:29 +02005613#if defined(MBEDTLS_HAVE_TIME)
5614 uint64_t start;
5615#endif
Manuel Pégourié-Gonnarda3e7c652019-05-16 10:08:35 +02005616#if defined(MBEDTLS_X509_CRT_PARSE_C)
5617#if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
5618 size_t cert_len;
Manuel Pégourié-Gonnarda3e7c652019-05-16 10:08:35 +02005619#endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
5620#endif /* MBEDTLS_X509_CRT_PARSE_C */
5621
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005622 if (!omit_header) {
Manuel Pégourié-Gonnard45ac1f02019-07-23 16:52:45 +02005623 /*
5624 * Check version identifier
5625 */
5626
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005627 if ((size_t) (end - p) < sizeof(ssl_serialized_session_header)) {
5628 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
Manuel Pégourié-Gonnard45ac1f02019-07-23 16:52:45 +02005629 }
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005630
5631 if (memcmp(p, ssl_serialized_session_header,
5632 sizeof(ssl_serialized_session_header)) != 0) {
5633 return MBEDTLS_ERR_SSL_VERSION_MISMATCH;
5634 }
5635 p += sizeof(ssl_serialized_session_header);
Hanno Beckera835da52019-05-16 12:39:07 +01005636 }
Hanno Beckera835da52019-05-16 12:39:07 +01005637
5638 /*
Manuel Pégourié-Gonnardf743c032019-05-24 12:06:29 +02005639 * Time
Manuel Pégourié-Gonnard35eb8022019-05-16 11:11:08 +02005640 */
Manuel Pégourié-Gonnardf743c032019-05-24 12:06:29 +02005641#if defined(MBEDTLS_HAVE_TIME)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005642 if (8 > (size_t) (end - p)) {
5643 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
5644 }
Manuel Pégourié-Gonnarda3e7c652019-05-16 10:08:35 +02005645
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005646 start = ((uint64_t) p[0] << 56) |
5647 ((uint64_t) p[1] << 48) |
5648 ((uint64_t) p[2] << 40) |
5649 ((uint64_t) p[3] << 32) |
5650 ((uint64_t) p[4] << 24) |
5651 ((uint64_t) p[5] << 16) |
5652 ((uint64_t) p[6] << 8) |
5653 ((uint64_t) p[7]);
Manuel Pégourié-Gonnardf743c032019-05-24 12:06:29 +02005654 p += 8;
Manuel Pégourié-Gonnarda3e7c652019-05-16 10:08:35 +02005655
Manuel Pégourié-Gonnardf743c032019-05-24 12:06:29 +02005656 session->start = (time_t) start;
5657#endif /* MBEDTLS_HAVE_TIME */
5658
5659 /*
5660 * Basic mandatory fields
5661 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005662 if (2 + 1 + 1 + 32 + 48 + 4 > (size_t) (end - p)) {
5663 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
5664 }
Manuel Pégourié-Gonnardf743c032019-05-24 12:06:29 +02005665
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005666 session->ciphersuite = (p[0] << 8) | p[1];
Manuel Pégourié-Gonnardf743c032019-05-24 12:06:29 +02005667 p += 2;
5668
5669 session->compression = *p++;
5670
5671 session->id_len = *p++;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005672 memcpy(session->id, p, 32);
Manuel Pégourié-Gonnardf743c032019-05-24 12:06:29 +02005673 p += 32;
5674
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005675 memcpy(session->master, p, 48);
Manuel Pégourié-Gonnardf743c032019-05-24 12:06:29 +02005676 p += 48;
5677
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005678 session->verify_result = ((uint32_t) p[0] << 24) |
5679 ((uint32_t) p[1] << 16) |
5680 ((uint32_t) p[2] << 8) |
5681 ((uint32_t) p[3]);
Manuel Pégourié-Gonnardf743c032019-05-24 12:06:29 +02005682 p += 4;
5683
5684 /* Immediately clear invalid pointer values that have been read, in case
5685 * we exit early before we replaced them with valid ones. */
Manuel Pégourié-Gonnarda3e7c652019-05-16 10:08:35 +02005686#if defined(MBEDTLS_X509_CRT_PARSE_C)
5687#if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
5688 session->peer_cert = NULL;
5689#else
5690 session->peer_cert_digest = NULL;
5691#endif /* !MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
5692#endif /* MBEDTLS_X509_CRT_PARSE_C */
5693#if defined(MBEDTLS_SSL_SESSION_TICKETS) && defined(MBEDTLS_SSL_CLI_C)
5694 session->ticket = NULL;
5695#endif /* MBEDTLS_SSL_SESSION_TICKETS && MBEDTLS_SSL_CLI_C */
5696
Manuel Pégourié-Gonnard35eb8022019-05-16 11:11:08 +02005697 /*
5698 * Peer certificate
5699 */
Manuel Pégourié-Gonnarda3e7c652019-05-16 10:08:35 +02005700#if defined(MBEDTLS_X509_CRT_PARSE_C)
5701#if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
5702 /* Deserialize CRT from the end of the ticket. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005703 if (3 > (size_t) (end - p)) {
5704 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
5705 }
Manuel Pégourié-Gonnarda3e7c652019-05-16 10:08:35 +02005706
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005707 cert_len = (p[0] << 16) | (p[1] << 8) | p[2];
Manuel Pégourié-Gonnarda3e7c652019-05-16 10:08:35 +02005708 p += 3;
5709
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005710 if (cert_len != 0) {
Janos Follath865b3eb2019-12-16 11:46:15 +00005711 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnarda3e7c652019-05-16 10:08:35 +02005712
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005713 if (cert_len > (size_t) (end - p)) {
5714 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
5715 }
Manuel Pégourié-Gonnarda3e7c652019-05-16 10:08:35 +02005716
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005717 session->peer_cert = mbedtls_calloc(1, sizeof(mbedtls_x509_crt));
Manuel Pégourié-Gonnarda3e7c652019-05-16 10:08:35 +02005718
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005719 if (session->peer_cert == NULL) {
5720 return MBEDTLS_ERR_SSL_ALLOC_FAILED;
5721 }
Manuel Pégourié-Gonnarda3e7c652019-05-16 10:08:35 +02005722
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005723 mbedtls_x509_crt_init(session->peer_cert);
Manuel Pégourié-Gonnarda3e7c652019-05-16 10:08:35 +02005724
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005725 if ((ret = mbedtls_x509_crt_parse_der(session->peer_cert,
5726 p, cert_len)) != 0) {
5727 mbedtls_x509_crt_free(session->peer_cert);
5728 mbedtls_free(session->peer_cert);
Manuel Pégourié-Gonnarda3e7c652019-05-16 10:08:35 +02005729 session->peer_cert = NULL;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005730 return ret;
Manuel Pégourié-Gonnarda3e7c652019-05-16 10:08:35 +02005731 }
5732
5733 p += cert_len;
5734 }
5735#else /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
5736 /* Deserialize CRT digest from the end of the ticket. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005737 if (2 > (size_t) (end - p)) {
5738 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
5739 }
Manuel Pégourié-Gonnarda3e7c652019-05-16 10:08:35 +02005740
Manuel Pégourié-Gonnardf743c032019-05-24 12:06:29 +02005741 session->peer_cert_digest_type = (mbedtls_md_type_t) *p++;
5742 session->peer_cert_digest_len = (size_t) *p++;
Manuel Pégourié-Gonnarda3e7c652019-05-16 10:08:35 +02005743
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005744 if (session->peer_cert_digest_len != 0) {
Manuel Pégourié-Gonnardf743c032019-05-24 12:06:29 +02005745 const mbedtls_md_info_t *md_info =
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005746 mbedtls_md_info_from_type(session->peer_cert_digest_type);
5747 if (md_info == NULL) {
5748 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
5749 }
5750 if (session->peer_cert_digest_len != mbedtls_md_get_size(md_info)) {
5751 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
5752 }
Manuel Pégourié-Gonnarda3e7c652019-05-16 10:08:35 +02005753
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005754 if (session->peer_cert_digest_len > (size_t) (end - p)) {
5755 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
5756 }
Manuel Pégourié-Gonnardf743c032019-05-24 12:06:29 +02005757
5758 session->peer_cert_digest =
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005759 mbedtls_calloc(1, session->peer_cert_digest_len);
5760 if (session->peer_cert_digest == NULL) {
5761 return MBEDTLS_ERR_SSL_ALLOC_FAILED;
5762 }
Manuel Pégourié-Gonnarda3e7c652019-05-16 10:08:35 +02005763
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005764 memcpy(session->peer_cert_digest, p,
5765 session->peer_cert_digest_len);
Manuel Pégourié-Gonnardf743c032019-05-24 12:06:29 +02005766 p += session->peer_cert_digest_len;
Manuel Pégourié-Gonnarda3e7c652019-05-16 10:08:35 +02005767 }
5768#endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
5769#endif /* MBEDTLS_X509_CRT_PARSE_C */
5770
Manuel Pégourié-Gonnard35eb8022019-05-16 11:11:08 +02005771 /*
Manuel Pégourié-Gonnardf743c032019-05-24 12:06:29 +02005772 * Session ticket and associated data
Manuel Pégourié-Gonnard35eb8022019-05-16 11:11:08 +02005773 */
5774#if defined(MBEDTLS_SSL_SESSION_TICKETS) && defined(MBEDTLS_SSL_CLI_C)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005775 if (3 > (size_t) (end - p)) {
5776 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
5777 }
Manuel Pégourié-Gonnard35eb8022019-05-16 11:11:08 +02005778
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005779 session->ticket_len = (p[0] << 16) | (p[1] << 8) | p[2];
Manuel Pégourié-Gonnard35eb8022019-05-16 11:11:08 +02005780 p += 3;
5781
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005782 if (session->ticket_len != 0) {
5783 if (session->ticket_len > (size_t) (end - p)) {
5784 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
5785 }
Manuel Pégourié-Gonnard35eb8022019-05-16 11:11:08 +02005786
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005787 session->ticket = mbedtls_calloc(1, session->ticket_len);
5788 if (session->ticket == NULL) {
5789 return MBEDTLS_ERR_SSL_ALLOC_FAILED;
5790 }
Manuel Pégourié-Gonnard35eb8022019-05-16 11:11:08 +02005791
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005792 memcpy(session->ticket, p, session->ticket_len);
Manuel Pégourié-Gonnard35eb8022019-05-16 11:11:08 +02005793 p += session->ticket_len;
5794 }
Manuel Pégourié-Gonnardf743c032019-05-24 12:06:29 +02005795
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005796 if (4 > (size_t) (end - p)) {
5797 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
5798 }
Manuel Pégourié-Gonnardf743c032019-05-24 12:06:29 +02005799
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005800 session->ticket_lifetime = ((uint32_t) p[0] << 24) |
5801 ((uint32_t) p[1] << 16) |
5802 ((uint32_t) p[2] << 8) |
5803 ((uint32_t) p[3]);
Manuel Pégourié-Gonnardf743c032019-05-24 12:06:29 +02005804 p += 4;
Manuel Pégourié-Gonnard35eb8022019-05-16 11:11:08 +02005805#endif /* MBEDTLS_SSL_SESSION_TICKETS && MBEDTLS_SSL_CLI_C */
5806
Manuel Pégourié-Gonnardf743c032019-05-24 12:06:29 +02005807 /*
5808 * Misc extension-related info
5809 */
5810#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005811 if (1 > (size_t) (end - p)) {
5812 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
5813 }
Manuel Pégourié-Gonnardf743c032019-05-24 12:06:29 +02005814
5815 session->mfl_code = *p++;
5816#endif
5817
5818#if defined(MBEDTLS_SSL_TRUNCATED_HMAC)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005819 if (1 > (size_t) (end - p)) {
5820 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
5821 }
Manuel Pégourié-Gonnardf743c032019-05-24 12:06:29 +02005822
5823 session->trunc_hmac = *p++;
5824#endif
5825
5826#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005827 if (1 > (size_t) (end - p)) {
5828 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
5829 }
Manuel Pégourié-Gonnardf743c032019-05-24 12:06:29 +02005830
5831 session->encrypt_then_mac = *p++;
5832#endif
5833
Manuel Pégourié-Gonnard35eb8022019-05-16 11:11:08 +02005834 /* Done, should have consumed entire buffer */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005835 if (p != end) {
5836 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
5837 }
Manuel Pégourié-Gonnarda3e7c652019-05-16 10:08:35 +02005838
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005839 return 0;
Manuel Pégourié-Gonnarda3e7c652019-05-16 10:08:35 +02005840}
5841
5842/*
Manuel Pégourié-Gonnardb9dfc9f2019-07-12 10:50:19 +02005843 * Deserialize session: public wrapper for error cleaning
Manuel Pégourié-Gonnarda3d831b2019-05-23 12:28:45 +02005844 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005845int mbedtls_ssl_session_load(mbedtls_ssl_session *session,
5846 const unsigned char *buf,
5847 size_t len)
Manuel Pégourié-Gonnarda3d831b2019-05-23 12:28:45 +02005848{
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005849 int ret = ssl_session_load(session, 0, buf, len);
Manuel Pégourié-Gonnarda3d831b2019-05-23 12:28:45 +02005850
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005851 if (ret != 0) {
5852 mbedtls_ssl_session_free(session);
5853 }
Manuel Pégourié-Gonnarda3d831b2019-05-23 12:28:45 +02005854
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005855 return ret;
Manuel Pégourié-Gonnarda3d831b2019-05-23 12:28:45 +02005856}
5857
5858/*
Paul Bakker1961b702013-01-25 14:49:24 +01005859 * Perform a single step of the SSL handshake
Paul Bakker5121ce52009-01-03 21:22:43 +00005860 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005861int mbedtls_ssl_handshake_step(mbedtls_ssl_context *ssl)
Paul Bakker5121ce52009-01-03 21:22:43 +00005862{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005863 int ret = MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
Paul Bakker5121ce52009-01-03 21:22:43 +00005864
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005865 if (ssl == NULL || ssl->conf == NULL) {
5866 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
5867 }
Manuel Pégourié-Gonnardf81ee2e2015-09-01 17:43:40 +02005868
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005869#if defined(MBEDTLS_SSL_CLI_C)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005870 if (ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT) {
5871 ret = mbedtls_ssl_handshake_client_step(ssl);
5872 }
Paul Bakker5121ce52009-01-03 21:22:43 +00005873#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005874#if defined(MBEDTLS_SSL_SRV_C)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005875 if (ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER) {
5876 ret = mbedtls_ssl_handshake_server_step(ssl);
5877 }
Paul Bakker5121ce52009-01-03 21:22:43 +00005878#endif
5879
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005880 return ret;
Paul Bakker1961b702013-01-25 14:49:24 +01005881}
5882
5883/*
5884 * Perform the SSL handshake
5885 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005886int mbedtls_ssl_handshake(mbedtls_ssl_context *ssl)
Paul Bakker1961b702013-01-25 14:49:24 +01005887{
5888 int ret = 0;
5889
Hanno Beckera817ea42020-10-20 15:20:23 +01005890 /* Sanity checks */
5891
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005892 if (ssl == NULL || ssl->conf == NULL) {
5893 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
5894 }
Manuel Pégourié-Gonnardf81ee2e2015-09-01 17:43:40 +02005895
Hanno Beckera817ea42020-10-20 15:20:23 +01005896#if defined(MBEDTLS_SSL_PROTO_DTLS)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005897 if (ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
5898 (ssl->f_set_timer == NULL || ssl->f_get_timer == NULL)) {
5899 MBEDTLS_SSL_DEBUG_MSG(1, ("You must use "
5900 "mbedtls_ssl_set_timer_cb() for DTLS"));
5901 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
Hanno Beckera817ea42020-10-20 15:20:23 +01005902 }
5903#endif /* MBEDTLS_SSL_PROTO_DTLS */
5904
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005905 MBEDTLS_SSL_DEBUG_MSG(2, ("=> handshake"));
Paul Bakker1961b702013-01-25 14:49:24 +01005906
Hanno Beckera817ea42020-10-20 15:20:23 +01005907 /* Main handshake loop */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005908 while (ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER) {
5909 ret = mbedtls_ssl_handshake_step(ssl);
Paul Bakker1961b702013-01-25 14:49:24 +01005910
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005911 if (ret != 0) {
Paul Bakker1961b702013-01-25 14:49:24 +01005912 break;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005913 }
Paul Bakker1961b702013-01-25 14:49:24 +01005914 }
5915
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005916 MBEDTLS_SSL_DEBUG_MSG(2, ("<= handshake"));
Paul Bakker5121ce52009-01-03 21:22:43 +00005917
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005918 return ret;
Paul Bakker5121ce52009-01-03 21:22:43 +00005919}
5920
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005921#if defined(MBEDTLS_SSL_RENEGOTIATION)
5922#if defined(MBEDTLS_SSL_SRV_C)
Paul Bakker5121ce52009-01-03 21:22:43 +00005923/*
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01005924 * Write HelloRequest to request renegotiation on server
Paul Bakker48916f92012-09-16 19:57:18 +00005925 */
Manuel Pégourié-Gonnardd904d662022-06-17 10:24:00 +02005926MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005927static int ssl_write_hello_request(mbedtls_ssl_context *ssl)
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01005928{
Janos Follath865b3eb2019-12-16 11:46:15 +00005929 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01005930
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005931 MBEDTLS_SSL_DEBUG_MSG(2, ("=> write hello request"));
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01005932
5933 ssl->out_msglen = 4;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005934 ssl->out_msgtype = MBEDTLS_SSL_MSG_HANDSHAKE;
5935 ssl->out_msg[0] = MBEDTLS_SSL_HS_HELLO_REQUEST;
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01005936
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005937 if ((ret = mbedtls_ssl_write_handshake_msg(ssl)) != 0) {
5938 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ssl_write_handshake_msg", ret);
5939 return ret;
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01005940 }
5941
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005942 MBEDTLS_SSL_DEBUG_MSG(2, ("<= write hello request"));
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01005943
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005944 return 0;
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01005945}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005946#endif /* MBEDTLS_SSL_SRV_C */
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01005947
5948/*
5949 * Actually renegotiate current connection, triggered by either:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005950 * - any side: calling mbedtls_ssl_renegotiate(),
5951 * - client: receiving a HelloRequest during mbedtls_ssl_read(),
5952 * - server: receiving any handshake message on server during mbedtls_ssl_read() after
Manuel Pégourié-Gonnard55e4ff22014-08-19 11:16:35 +02005953 * the initial handshake is completed.
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01005954 * If the handshake doesn't complete due to waiting for I/O, it will continue
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005955 * during the next calls to mbedtls_ssl_renegotiate() or mbedtls_ssl_read() respectively.
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01005956 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005957int mbedtls_ssl_start_renegotiation(mbedtls_ssl_context *ssl)
Paul Bakker48916f92012-09-16 19:57:18 +00005958{
Janos Follath865b3eb2019-12-16 11:46:15 +00005959 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker48916f92012-09-16 19:57:18 +00005960
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005961 MBEDTLS_SSL_DEBUG_MSG(2, ("=> renegotiate"));
Paul Bakker48916f92012-09-16 19:57:18 +00005962
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005963 if ((ret = ssl_handshake_init(ssl)) != 0) {
5964 return ret;
5965 }
Paul Bakker48916f92012-09-16 19:57:18 +00005966
Manuel Pégourié-Gonnard0557bd52014-08-19 19:18:39 +02005967 /* RFC 6347 4.2.2: "[...] the HelloRequest will have message_seq = 0 and
5968 * the ServerHello will have message_seq = 1" */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005969#if defined(MBEDTLS_SSL_PROTO_DTLS)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005970 if (ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
5971 ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_PENDING) {
5972 if (ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER) {
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02005973 ssl->handshake->out_msg_seq = 1;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005974 } else {
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02005975 ssl->handshake->in_msg_seq = 1;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005976 }
Manuel Pégourié-Gonnard0557bd52014-08-19 19:18:39 +02005977 }
5978#endif
5979
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005980 ssl->state = MBEDTLS_SSL_HELLO_REQUEST;
5981 ssl->renego_status = MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS;
Paul Bakker48916f92012-09-16 19:57:18 +00005982
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005983 if ((ret = mbedtls_ssl_handshake(ssl)) != 0) {
5984 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ssl_handshake", ret);
5985 return ret;
Paul Bakker48916f92012-09-16 19:57:18 +00005986 }
5987
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005988 MBEDTLS_SSL_DEBUG_MSG(2, ("<= renegotiate"));
Paul Bakker48916f92012-09-16 19:57:18 +00005989
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005990 return 0;
Paul Bakker48916f92012-09-16 19:57:18 +00005991}
5992
5993/*
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01005994 * Renegotiate current connection on client,
5995 * or request renegotiation on server
5996 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005997int mbedtls_ssl_renegotiate(mbedtls_ssl_context *ssl)
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01005998{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005999 int ret = MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01006000
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01006001 if (ssl == NULL || ssl->conf == NULL) {
6002 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
6003 }
Manuel Pégourié-Gonnardf81ee2e2015-09-01 17:43:40 +02006004
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006005#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01006006 /* On server, just send the request */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01006007 if (ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER) {
6008 if (ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER) {
6009 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
6010 }
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01006011
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006012 ssl->renego_status = MBEDTLS_SSL_RENEGOTIATION_PENDING;
Manuel Pégourié-Gonnardf07f4212014-08-15 19:04:47 +02006013
6014 /* Did we already try/start sending HelloRequest? */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01006015 if (ssl->out_left != 0) {
6016 return mbedtls_ssl_flush_output(ssl);
6017 }
Manuel Pégourié-Gonnardf07f4212014-08-15 19:04:47 +02006018
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01006019 return ssl_write_hello_request(ssl);
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01006020 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006021#endif /* MBEDTLS_SSL_SRV_C */
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01006022
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006023#if defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01006024 /*
6025 * On client, either start the renegotiation process or,
6026 * if already in progress, continue the handshake
6027 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01006028 if (ssl->renego_status != MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS) {
6029 if (ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER) {
6030 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01006031 }
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01006032
6033 if ((ret = mbedtls_ssl_start_renegotiation(ssl)) != 0) {
6034 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ssl_start_renegotiation", ret);
6035 return ret;
6036 }
6037 } else {
6038 if ((ret = mbedtls_ssl_handshake(ssl)) != 0) {
6039 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ssl_handshake", ret);
6040 return ret;
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01006041 }
6042 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006043#endif /* MBEDTLS_SSL_CLI_C */
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01006044
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01006045 return ret;
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01006046}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006047#endif /* MBEDTLS_SSL_RENEGOTIATION */
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01006048
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006049#if defined(MBEDTLS_X509_CRT_PARSE_C)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01006050static void ssl_key_cert_free(mbedtls_ssl_key_cert *key_cert)
Manuel Pégourié-Gonnard705fcca2013-09-23 20:04:20 +02006051{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006052 mbedtls_ssl_key_cert *cur = key_cert, *next;
Manuel Pégourié-Gonnard705fcca2013-09-23 20:04:20 +02006053
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01006054 while (cur != NULL) {
Manuel Pégourié-Gonnard705fcca2013-09-23 20:04:20 +02006055 next = cur->next;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01006056 mbedtls_free(cur);
Manuel Pégourié-Gonnard705fcca2013-09-23 20:04:20 +02006057 cur = next;
6058 }
6059}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006060#endif /* MBEDTLS_X509_CRT_PARSE_C */
Manuel Pégourié-Gonnard705fcca2013-09-23 20:04:20 +02006061
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01006062void mbedtls_ssl_handshake_free(mbedtls_ssl_context *ssl)
Paul Bakker48916f92012-09-16 19:57:18 +00006063{
Gilles Peskine9b562d52018-04-25 20:32:43 +02006064 mbedtls_ssl_handshake_params *handshake = ssl->handshake;
6065
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01006066 if (handshake == NULL) {
Paul Bakkeraccaffe2014-06-26 13:37:14 +02006067 return;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01006068 }
Paul Bakkeraccaffe2014-06-26 13:37:14 +02006069
Gilles Peskinedf13d5c2018-04-25 20:39:48 +02006070#if defined(MBEDTLS_SSL_ASYNC_PRIVATE)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01006071 if (ssl->conf->f_async_cancel != NULL && handshake->async_in_progress != 0) {
6072 ssl->conf->f_async_cancel(ssl);
Gilles Peskinedf13d5c2018-04-25 20:39:48 +02006073 handshake->async_in_progress = 0;
6074 }
6075#endif /* MBEDTLS_SSL_ASYNC_PRIVATE */
6076
Manuel Pégourié-Gonnardb9d64e52015-07-06 14:18:56 +02006077#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1) || \
6078 defined(MBEDTLS_SSL_PROTO_TLS1_1)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01006079 mbedtls_md5_free(&handshake->fin_md5);
6080 mbedtls_sha1_free(&handshake->fin_sha1);
Manuel Pégourié-Gonnardb9d64e52015-07-06 14:18:56 +02006081#endif
6082#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
6083#if defined(MBEDTLS_SHA256_C)
Andrzej Kurekeb342242019-01-29 09:14:33 -05006084#if defined(MBEDTLS_USE_PSA_CRYPTO)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01006085 psa_hash_abort(&handshake->fin_sha256_psa);
Andrzej Kurekeb342242019-01-29 09:14:33 -05006086#else
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01006087 mbedtls_sha256_free(&handshake->fin_sha256);
Manuel Pégourié-Gonnardb9d64e52015-07-06 14:18:56 +02006088#endif
Andrzej Kurekeb342242019-01-29 09:14:33 -05006089#endif
Gilles Peskined2d59372021-05-12 22:43:27 +02006090#if defined(MBEDTLS_SHA512_C) && !defined(MBEDTLS_SHA512_NO_SHA384)
Andrzej Kurekeb342242019-01-29 09:14:33 -05006091#if defined(MBEDTLS_USE_PSA_CRYPTO)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01006092 psa_hash_abort(&handshake->fin_sha384_psa);
Andrzej Kurekeb342242019-01-29 09:14:33 -05006093#else
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01006094 mbedtls_sha512_free(&handshake->fin_sha512);
Manuel Pégourié-Gonnardb9d64e52015-07-06 14:18:56 +02006095#endif
Andrzej Kurekeb342242019-01-29 09:14:33 -05006096#endif
Manuel Pégourié-Gonnardb9d64e52015-07-06 14:18:56 +02006097#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
6098
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006099#if defined(MBEDTLS_DHM_C)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01006100 mbedtls_dhm_free(&handshake->dhm_ctx);
Paul Bakker48916f92012-09-16 19:57:18 +00006101#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006102#if defined(MBEDTLS_ECDH_C)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01006103 mbedtls_ecdh_free(&handshake->ecdh_ctx);
Paul Bakker61d113b2013-07-04 11:51:43 +02006104#endif
Manuel Pégourié-Gonnardeef142d2015-09-16 10:05:04 +02006105#if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01006106 mbedtls_ecjpake_free(&handshake->ecjpake_ctx);
Manuel Pégourié-Gonnard77c06462015-09-17 13:59:49 +02006107#if defined(MBEDTLS_SSL_CLI_C)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01006108 mbedtls_free(handshake->ecjpake_cache);
Manuel Pégourié-Gonnard77c06462015-09-17 13:59:49 +02006109 handshake->ecjpake_cache = NULL;
6110 handshake->ecjpake_cache_len = 0;
6111#endif
Manuel Pégourié-Gonnard76cfd3f2015-09-15 12:10:54 +02006112#endif
Paul Bakker61d113b2013-07-04 11:51:43 +02006113
Janos Follath4ae5c292016-02-10 11:27:43 +00006114#if defined(MBEDTLS_ECDH_C) || defined(MBEDTLS_ECDSA_C) || \
6115 defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
Paul Bakker9af723c2014-05-01 13:03:14 +02006116 /* explicit void pointer cast for buggy MS compiler */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01006117 mbedtls_free((void *) handshake->curves);
Manuel Pégourié-Gonnardd09453c2013-09-23 19:11:32 +02006118#endif
6119
Gilles Peskineeccd8882020-03-10 12:19:08 +01006120#if defined(MBEDTLS_KEY_EXCHANGE_SOME_PSK_ENABLED)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01006121 if (handshake->psk != NULL) {
6122 mbedtls_platform_zeroize(handshake->psk, handshake->psk_len);
6123 mbedtls_free(handshake->psk);
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01006124 }
6125#endif
6126
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006127#if defined(MBEDTLS_X509_CRT_PARSE_C) && \
6128 defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
Manuel Pégourié-Gonnard83724542013-09-24 22:30:56 +02006129 /*
6130 * Free only the linked list wrapper, not the keys themselves
6131 * since the belong to the SNI callback
6132 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01006133 if (handshake->sni_key_cert != NULL) {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006134 mbedtls_ssl_key_cert *cur = handshake->sni_key_cert, *next;
Manuel Pégourié-Gonnard83724542013-09-24 22:30:56 +02006135
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01006136 while (cur != NULL) {
Manuel Pégourié-Gonnard83724542013-09-24 22:30:56 +02006137 next = cur->next;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01006138 mbedtls_free(cur);
Manuel Pégourié-Gonnard83724542013-09-24 22:30:56 +02006139 cur = next;
6140 }
6141 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006142#endif /* MBEDTLS_X509_CRT_PARSE_C && MBEDTLS_SSL_SERVER_NAME_INDICATION */
Manuel Pégourié-Gonnard705fcca2013-09-23 20:04:20 +02006143
Gilles Peskineeccd8882020-03-10 12:19:08 +01006144#if defined(MBEDTLS_SSL_ECP_RESTARTABLE_ENABLED)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01006145 mbedtls_x509_crt_restart_free(&handshake->ecrs_ctx);
6146 if (handshake->ecrs_peer_cert != NULL) {
6147 mbedtls_x509_crt_free(handshake->ecrs_peer_cert);
6148 mbedtls_free(handshake->ecrs_peer_cert);
Hanno Becker3dad3112019-02-05 17:19:52 +00006149 }
Manuel Pégourié-Gonnard862cde52017-05-17 11:56:15 +02006150#endif
6151
Hanno Becker75173122019-02-06 16:18:31 +00006152#if defined(MBEDTLS_X509_CRT_PARSE_C) && \
6153 !defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01006154 mbedtls_pk_free(&handshake->peer_pubkey);
Hanno Becker75173122019-02-06 16:18:31 +00006155#endif /* MBEDTLS_X509_CRT_PARSE_C && !MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
6156
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006157#if defined(MBEDTLS_SSL_PROTO_DTLS)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01006158 mbedtls_free(handshake->verify_cookie);
6159 mbedtls_ssl_flight_free(handshake->flight);
6160 mbedtls_ssl_buffering_free(ssl);
Manuel Pégourié-Gonnard74848812014-07-11 02:43:49 +02006161#endif
6162
Hanno Becker4a63ed42019-01-08 11:39:35 +00006163#if defined(MBEDTLS_ECDH_C) && \
6164 defined(MBEDTLS_USE_PSA_CRYPTO)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01006165 psa_destroy_key(handshake->ecdh_psa_privkey);
Hanno Becker4a63ed42019-01-08 11:39:35 +00006166#endif /* MBEDTLS_ECDH_C && MBEDTLS_USE_PSA_CRYPTO */
6167
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01006168 mbedtls_platform_zeroize(handshake,
6169 sizeof(mbedtls_ssl_handshake_params));
Andrzej Kurek0afa2a12020-03-03 10:39:58 -05006170
6171#if defined(MBEDTLS_SSL_VARIABLE_BUFFER_LENGTH)
6172 /* If the buffers are too big - reallocate. Because of the way Mbed TLS
6173 * processes datagrams and the fact that a datagram is allowed to have
6174 * several records in it, it is possible that the I/O buffers are not
6175 * empty at this stage */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01006176 handle_buffer_resizing(ssl, 1, mbedtls_ssl_get_input_buflen(ssl),
6177 mbedtls_ssl_get_output_buflen(ssl));
Andrzej Kurek0afa2a12020-03-03 10:39:58 -05006178#endif
Paul Bakker48916f92012-09-16 19:57:18 +00006179}
6180
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01006181void mbedtls_ssl_session_free(mbedtls_ssl_session *session)
Paul Bakker48916f92012-09-16 19:57:18 +00006182{
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01006183 if (session == NULL) {
Paul Bakkeraccaffe2014-06-26 13:37:14 +02006184 return;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01006185 }
Paul Bakkeraccaffe2014-06-26 13:37:14 +02006186
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006187#if defined(MBEDTLS_X509_CRT_PARSE_C)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01006188 ssl_clear_peer_cert(session);
Paul Bakkered27a042013-04-18 22:46:23 +02006189#endif
Paul Bakker0a597072012-09-25 21:55:46 +00006190
Manuel Pégourié-Gonnardb596abf2015-05-20 10:45:29 +02006191#if defined(MBEDTLS_SSL_SESSION_TICKETS) && defined(MBEDTLS_SSL_CLI_C)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01006192 mbedtls_free(session->ticket);
Paul Bakkera503a632013-08-14 13:48:06 +02006193#endif
Manuel Pégourié-Gonnard75d44012013-08-02 14:44:04 +02006194
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01006195 mbedtls_platform_zeroize(session, sizeof(mbedtls_ssl_session));
Paul Bakker48916f92012-09-16 19:57:18 +00006196}
6197
Manuel Pégourié-Gonnard5c0e3772019-07-23 16:13:17 +02006198#if defined(MBEDTLS_SSL_CONTEXT_SERIALIZATION)
Manuel Pégourié-Gonnard4e9370b2019-07-23 16:31:16 +02006199
6200#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
6201#define SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_CONNECTION_ID 1u
6202#else
6203#define SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_CONNECTION_ID 0u
6204#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
6205
6206#if defined(MBEDTLS_SSL_DTLS_BADMAC_LIMIT)
6207#define SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_BADMAC_LIMIT 1u
6208#else
6209#define SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_BADMAC_LIMIT 0u
6210#endif /* MBEDTLS_SSL_DTLS_BADMAC_LIMIT */
6211
6212#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
6213#define SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_ANTI_REPLAY 1u
6214#else
6215#define SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_ANTI_REPLAY 0u
6216#endif /* MBEDTLS_SSL_DTLS_ANTI_REPLAY */
6217
6218#if defined(MBEDTLS_SSL_ALPN)
6219#define SSL_SERIALIZED_CONTEXT_CONFIG_ALPN 1u
6220#else
6221#define SSL_SERIALIZED_CONTEXT_CONFIG_ALPN 0u
6222#endif /* MBEDTLS_SSL_ALPN */
6223
6224#define SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_CONNECTION_ID_BIT 0
6225#define SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_BADMAC_LIMIT_BIT 1
6226#define SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_ANTI_REPLAY_BIT 2
6227#define SSL_SERIALIZED_CONTEXT_CONFIG_ALPN_BIT 3
6228
6229#define SSL_SERIALIZED_CONTEXT_CONFIG_BITFLAG \
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01006230 ((uint32_t) ( \
6231 (SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_CONNECTION_ID << \
6232 SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_CONNECTION_ID_BIT) | \
6233 (SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_BADMAC_LIMIT << \
6234 SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_BADMAC_LIMIT_BIT) | \
6235 (SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_ANTI_REPLAY << \
6236 SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_ANTI_REPLAY_BIT) | \
6237 (SSL_SERIALIZED_CONTEXT_CONFIG_ALPN << SSL_SERIALIZED_CONTEXT_CONFIG_ALPN_BIT) | \
6238 0u))
Manuel Pégourié-Gonnard4e9370b2019-07-23 16:31:16 +02006239
Chien Wongb6d57932024-02-07 21:48:12 +08006240static const unsigned char ssl_serialized_context_header[] = {
Manuel Pégourié-Gonnard4c90e852019-07-11 10:58:10 +02006241 MBEDTLS_VERSION_MAJOR,
6242 MBEDTLS_VERSION_MINOR,
6243 MBEDTLS_VERSION_PATCH,
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01006244 MBEDTLS_BYTE_1(SSL_SERIALIZED_SESSION_CONFIG_BITFLAG),
6245 MBEDTLS_BYTE_0(SSL_SERIALIZED_SESSION_CONFIG_BITFLAG),
6246 MBEDTLS_BYTE_2(SSL_SERIALIZED_CONTEXT_CONFIG_BITFLAG),
6247 MBEDTLS_BYTE_1(SSL_SERIALIZED_CONTEXT_CONFIG_BITFLAG),
6248 MBEDTLS_BYTE_0(SSL_SERIALIZED_CONTEXT_CONFIG_BITFLAG),
Manuel Pégourié-Gonnard4c90e852019-07-11 10:58:10 +02006249};
6250
Paul Bakker5121ce52009-01-03 21:22:43 +00006251/*
Manuel Pégourié-Gonnardac87e282019-05-28 13:02:16 +02006252 * Serialize a full SSL context
Manuel Pégourié-Gonnard00400c22019-07-10 14:58:45 +02006253 *
6254 * The format of the serialized data is:
6255 * (in the presentation language of TLS, RFC 8446 section 3)
6256 *
6257 * // header
6258 * opaque mbedtls_version[3]; // major, minor, patch
Manuel Pégourié-Gonnard4c90e852019-07-11 10:58:10 +02006259 * opaque context_format[5]; // version-specific field determining
Manuel Pégourié-Gonnard00400c22019-07-10 14:58:45 +02006260 * // the format of the remaining
Manuel Pégourié-Gonnard4c90e852019-07-11 10:58:10 +02006261 * // serialized data.
Manuel Pégourié-Gonnard4e9370b2019-07-23 16:31:16 +02006262 * Note: When updating the format, remember to keep these
6263 * version+format bytes. (We may make their size part of the API.)
Manuel Pégourié-Gonnard00400c22019-07-10 14:58:45 +02006264 *
6265 * // session sub-structure
6266 * opaque session<1..2^32-1>; // see mbedtls_ssl_session_save()
6267 * // transform sub-structure
6268 * uint8 random[64]; // ServerHello.random+ClientHello.random
6269 * uint8 in_cid<0..2^8-1> // Connection ID: expected incoming value
6270 * uint8 out_cid<0..2^8-1> // Connection ID: outgoing value to use
6271 * // fields from ssl_context
6272 * uint32 badmac_seen; // DTLS: number of records with failing MAC
6273 * uint64 in_window_top; // DTLS: last validated record seq_num
6274 * uint64 in_window; // DTLS: bitmask for replay protection
6275 * uint8 disable_datagram_packing; // DTLS: only one record per datagram
6276 * uint64 cur_out_ctr; // Record layer: outgoing sequence number
6277 * uint16 mtu; // DTLS: path mtu (max outgoing fragment size)
6278 * uint8 alpn_chosen<0..2^8-1> // ALPN: negotiated application protocol
6279 *
6280 * Note that many fields of the ssl_context or sub-structures are not
6281 * serialized, as they fall in one of the following categories:
6282 *
6283 * 1. forced value (eg in_left must be 0)
6284 * 2. pointer to dynamically-allocated memory (eg session, transform)
6285 * 3. value can be re-derived from other data (eg session keys from MS)
6286 * 4. value was temporary (eg content of input buffer)
6287 * 5. value will be provided by the user again (eg I/O callbacks and context)
Manuel Pégourié-Gonnardac87e282019-05-28 13:02:16 +02006288 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01006289int mbedtls_ssl_context_save(mbedtls_ssl_context *ssl,
6290 unsigned char *buf,
6291 size_t buf_len,
6292 size_t *olen)
Manuel Pégourié-Gonnardac87e282019-05-28 13:02:16 +02006293{
Manuel Pégourié-Gonnard4c90e852019-07-11 10:58:10 +02006294 unsigned char *p = buf;
6295 size_t used = 0;
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02006296 size_t session_len;
6297 int ret = 0;
Manuel Pégourié-Gonnard4c90e852019-07-11 10:58:10 +02006298
Manuel Pégourié-Gonnard1aaf6692019-07-10 14:14:05 +02006299 /*
Manuel Pégourié-Gonnarde4588692019-07-29 12:28:52 +02006300 * Enforce usage restrictions, see "return BAD_INPUT_DATA" in
6301 * this function's documentation.
6302 *
6303 * These are due to assumptions/limitations in the implementation. Some of
6304 * them are likely to stay (no handshake in progress) some might go away
6305 * (only DTLS) but are currently used to simplify the implementation.
Manuel Pégourié-Gonnard1aaf6692019-07-10 14:14:05 +02006306 */
Manuel Pégourié-Gonnarde4588692019-07-29 12:28:52 +02006307 /* The initial handshake must be over */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01006308 if (ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER) {
6309 MBEDTLS_SSL_DEBUG_MSG(1, ("Initial handshake isn't over"));
6310 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
Jarno Lamsa8c51b7c2019-08-21 13:45:05 +03006311 }
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01006312 if (ssl->handshake != NULL) {
6313 MBEDTLS_SSL_DEBUG_MSG(1, ("Handshake isn't completed"));
6314 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
Jarno Lamsa8c51b7c2019-08-21 13:45:05 +03006315 }
Manuel Pégourié-Gonnarde4588692019-07-29 12:28:52 +02006316 /* Double-check that sub-structures are indeed ready */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01006317 if (ssl->transform == NULL || ssl->session == NULL) {
6318 MBEDTLS_SSL_DEBUG_MSG(1, ("Serialised structures aren't ready"));
6319 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
Jarno Lamsa8c51b7c2019-08-21 13:45:05 +03006320 }
Manuel Pégourié-Gonnarde4588692019-07-29 12:28:52 +02006321 /* There must be no pending incoming or outgoing data */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01006322 if (mbedtls_ssl_check_pending(ssl) != 0) {
6323 MBEDTLS_SSL_DEBUG_MSG(1, ("There is pending incoming data"));
6324 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
Jarno Lamsa8c51b7c2019-08-21 13:45:05 +03006325 }
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01006326 if (ssl->out_left != 0) {
6327 MBEDTLS_SSL_DEBUG_MSG(1, ("There is pending outgoing data"));
6328 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
Jarno Lamsa8c51b7c2019-08-21 13:45:05 +03006329 }
Dave Rodgmana03396a2022-12-06 16:30:38 +00006330 /* Protocol must be DTLS, not TLS */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01006331 if (ssl->conf->transport != MBEDTLS_SSL_TRANSPORT_DATAGRAM) {
6332 MBEDTLS_SSL_DEBUG_MSG(1, ("Only DTLS is supported"));
6333 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
Jarno Lamsa8c51b7c2019-08-21 13:45:05 +03006334 }
Manuel Pégourié-Gonnarde4588692019-07-29 12:28:52 +02006335 /* Version must be 1.2 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01006336 if (ssl->major_ver != MBEDTLS_SSL_MAJOR_VERSION_3) {
6337 MBEDTLS_SSL_DEBUG_MSG(1, ("Only version 1.2 supported"));
6338 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
Jarno Lamsa8c51b7c2019-08-21 13:45:05 +03006339 }
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01006340 if (ssl->minor_ver != MBEDTLS_SSL_MINOR_VERSION_3) {
6341 MBEDTLS_SSL_DEBUG_MSG(1, ("Only version 1.2 supported"));
6342 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
Jarno Lamsa8c51b7c2019-08-21 13:45:05 +03006343 }
Manuel Pégourié-Gonnarde4588692019-07-29 12:28:52 +02006344 /* We must be using an AEAD ciphersuite */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01006345 if (mbedtls_ssl_transform_uses_aead(ssl->transform) != 1) {
6346 MBEDTLS_SSL_DEBUG_MSG(1, ("Only AEAD ciphersuites supported"));
6347 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
Jarno Lamsa8c51b7c2019-08-21 13:45:05 +03006348 }
Manuel Pégourié-Gonnarde4588692019-07-29 12:28:52 +02006349 /* Renegotiation must not be enabled */
6350#if defined(MBEDTLS_SSL_RENEGOTIATION)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01006351 if (ssl->conf->disable_renegotiation != MBEDTLS_SSL_RENEGOTIATION_DISABLED) {
6352 MBEDTLS_SSL_DEBUG_MSG(1, ("Renegotiation must not be enabled"));
6353 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
Jarno Lamsa8c51b7c2019-08-21 13:45:05 +03006354 }
Manuel Pégourié-Gonnarde4588692019-07-29 12:28:52 +02006355#endif
Manuel Pégourié-Gonnardac87e282019-05-28 13:02:16 +02006356
Manuel Pégourié-Gonnard4c90e852019-07-11 10:58:10 +02006357 /*
6358 * Version and format identifier
6359 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01006360 used += sizeof(ssl_serialized_context_header);
Manuel Pégourié-Gonnardac87e282019-05-28 13:02:16 +02006361
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01006362 if (used <= buf_len) {
6363 memcpy(p, ssl_serialized_context_header,
6364 sizeof(ssl_serialized_context_header));
6365 p += sizeof(ssl_serialized_context_header);
Manuel Pégourié-Gonnard4c90e852019-07-11 10:58:10 +02006366 }
6367
6368 /*
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02006369 * Session (length + data)
6370 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01006371 ret = ssl_session_save(ssl->session, 1, NULL, 0, &session_len);
6372 if (ret != MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL) {
6373 return ret;
6374 }
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02006375
6376 used += 4 + session_len;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01006377 if (used <= buf_len) {
6378 MBEDTLS_PUT_UINT32_BE(session_len, p, 0);
Joe Subbiani2f98d792021-08-20 11:44:44 +01006379 p += 4;
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02006380
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01006381 ret = ssl_session_save(ssl->session, 1,
6382 p, session_len, &session_len);
6383 if (ret != 0) {
6384 return ret;
6385 }
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02006386
6387 p += session_len;
6388 }
6389
6390 /*
Manuel Pégourié-Gonnardc2a7b892019-07-15 09:04:11 +02006391 * Transform
6392 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01006393 used += sizeof(ssl->transform->randbytes);
6394 if (used <= buf_len) {
6395 memcpy(p, ssl->transform->randbytes,
6396 sizeof(ssl->transform->randbytes));
6397 p += sizeof(ssl->transform->randbytes);
Manuel Pégourié-Gonnardc2a7b892019-07-15 09:04:11 +02006398 }
6399
6400#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
6401 used += 2 + ssl->transform->in_cid_len + ssl->transform->out_cid_len;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01006402 if (used <= buf_len) {
Manuel Pégourié-Gonnardc2a7b892019-07-15 09:04:11 +02006403 *p++ = ssl->transform->in_cid_len;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01006404 memcpy(p, ssl->transform->in_cid, ssl->transform->in_cid_len);
Manuel Pégourié-Gonnardc2a7b892019-07-15 09:04:11 +02006405 p += ssl->transform->in_cid_len;
6406
6407 *p++ = ssl->transform->out_cid_len;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01006408 memcpy(p, ssl->transform->out_cid, ssl->transform->out_cid_len);
Manuel Pégourié-Gonnardc2a7b892019-07-15 09:04:11 +02006409 p += ssl->transform->out_cid_len;
6410 }
6411#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
6412
6413 /*
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02006414 * Saved fields from top-level ssl_context structure
6415 */
6416#if defined(MBEDTLS_SSL_DTLS_BADMAC_LIMIT)
6417 used += 4;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01006418 if (used <= buf_len) {
6419 MBEDTLS_PUT_UINT32_BE(ssl->badmac_seen, p, 0);
Joe Subbiani2f98d792021-08-20 11:44:44 +01006420 p += 4;
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02006421 }
6422#endif /* MBEDTLS_SSL_DTLS_BADMAC_LIMIT */
6423
6424#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
6425 used += 16;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01006426 if (used <= buf_len) {
6427 MBEDTLS_PUT_UINT64_BE(ssl->in_window_top, p, 0);
Joe Subbiani2f98d792021-08-20 11:44:44 +01006428 p += 8;
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02006429
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01006430 MBEDTLS_PUT_UINT64_BE(ssl->in_window, p, 0);
Joe Subbiani2f98d792021-08-20 11:44:44 +01006431 p += 8;
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02006432 }
6433#endif /* MBEDTLS_SSL_DTLS_ANTI_REPLAY */
6434
6435#if defined(MBEDTLS_SSL_PROTO_DTLS)
6436 used += 1;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01006437 if (used <= buf_len) {
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02006438 *p++ = ssl->disable_datagram_packing;
6439 }
6440#endif /* MBEDTLS_SSL_PROTO_DTLS */
6441
6442 used += 8;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01006443 if (used <= buf_len) {
6444 memcpy(p, ssl->cur_out_ctr, 8);
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02006445 p += 8;
6446 }
6447
6448#if defined(MBEDTLS_SSL_PROTO_DTLS)
6449 used += 2;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01006450 if (used <= buf_len) {
6451 MBEDTLS_PUT_UINT16_BE(ssl->mtu, p, 0);
Joe Subbiani2f98d792021-08-20 11:44:44 +01006452 p += 2;
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02006453 }
6454#endif /* MBEDTLS_SSL_PROTO_DTLS */
6455
6456#if defined(MBEDTLS_SSL_ALPN)
6457 {
6458 const uint8_t alpn_len = ssl->alpn_chosen
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01006459 ? (uint8_t) strlen(ssl->alpn_chosen)
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02006460 : 0;
6461
6462 used += 1 + alpn_len;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01006463 if (used <= buf_len) {
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02006464 *p++ = alpn_len;
6465
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01006466 if (ssl->alpn_chosen != NULL) {
6467 memcpy(p, ssl->alpn_chosen, alpn_len);
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02006468 p += alpn_len;
6469 }
6470 }
6471 }
6472#endif /* MBEDTLS_SSL_ALPN */
6473
6474 /*
Manuel Pégourié-Gonnard4c90e852019-07-11 10:58:10 +02006475 * Done
6476 */
6477 *olen = used;
6478
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01006479 if (used > buf_len) {
6480 return MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL;
6481 }
Manuel Pégourié-Gonnardac87e282019-05-28 13:02:16 +02006482
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01006483 MBEDTLS_SSL_DEBUG_BUF(4, "saved context", buf, used);
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02006484
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01006485 return mbedtls_ssl_session_reset_int(ssl, 0);
Manuel Pégourié-Gonnardac87e282019-05-28 13:02:16 +02006486}
6487
6488/*
Manuel Pégourié-Gonnardc2a7b892019-07-15 09:04:11 +02006489 * Helper to get TLS 1.2 PRF from ciphersuite
6490 * (Duplicates bits of logic from ssl_set_handshake_prfs().)
6491 */
Andrzej Kurekf9412f72022-10-18 07:30:19 -04006492#if defined(MBEDTLS_SHA256_C) || \
6493 (defined(MBEDTLS_SHA512_C) && !defined(MBEDTLS_SHA512_NO_SHA384))
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01006494typedef int (*tls_prf_fn)(const unsigned char *secret, size_t slen,
6495 const char *label,
6496 const unsigned char *random, size_t rlen,
6497 unsigned char *dstbuf, size_t dlen);
6498static tls_prf_fn ssl_tls12prf_from_cs(int ciphersuite_id)
Manuel Pégourié-Gonnardc2a7b892019-07-15 09:04:11 +02006499{
6500 const mbedtls_ssl_ciphersuite_t * const ciphersuite_info =
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01006501 mbedtls_ssl_ciphersuite_from_id(ciphersuite_id);
Manuel Pégourié-Gonnardc2a7b892019-07-15 09:04:11 +02006502
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01006503 if (ciphersuite_info == NULL) {
6504 return NULL;
6505 }
Andrzej Kurek84fc52c2022-10-25 04:18:30 -04006506
6507#if defined(MBEDTLS_SHA512_C) && !defined(MBEDTLS_SHA512_NO_SHA384)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01006508 if (ciphersuite_info->mac == MBEDTLS_MD_SHA384) {
6509 return tls_prf_sha384;
6510 } else
Manuel Pégourié-Gonnard9a96fd72019-07-23 17:11:24 +02006511#endif
Andrzej Kurekf9412f72022-10-18 07:30:19 -04006512#if defined(MBEDTLS_SHA256_C)
6513 {
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01006514 if (ciphersuite_info->mac == MBEDTLS_MD_SHA256) {
6515 return tls_prf_sha256;
6516 }
Andrzej Kurekf9412f72022-10-18 07:30:19 -04006517 }
6518#endif
6519#if !defined(MBEDTLS_SHA256_C) && \
6520 (!defined(MBEDTLS_SHA512_C) || defined(MBEDTLS_SHA512_NO_SHA384))
6521 (void) ciphersuite_info;
6522#endif
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01006523 return NULL;
Manuel Pégourié-Gonnardc2a7b892019-07-15 09:04:11 +02006524}
6525
Andrzej Kurekf9412f72022-10-18 07:30:19 -04006526#endif /* MBEDTLS_SHA256_C ||
6527 (MBEDTLS_SHA512_C && !MBEDTLS_SHA512_NO_SHA384) */
6528
Manuel Pégourié-Gonnardc2a7b892019-07-15 09:04:11 +02006529/*
Manuel Pégourié-Gonnardb9dfc9f2019-07-12 10:50:19 +02006530 * Deserialize context, see mbedtls_ssl_context_save() for format.
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02006531 *
6532 * This internal version is wrapped by a public function that cleans up in
6533 * case of error.
Manuel Pégourié-Gonnardac87e282019-05-28 13:02:16 +02006534 */
Manuel Pégourié-Gonnardd904d662022-06-17 10:24:00 +02006535MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01006536static int ssl_context_load(mbedtls_ssl_context *ssl,
6537 const unsigned char *buf,
6538 size_t len)
Manuel Pégourié-Gonnardac87e282019-05-28 13:02:16 +02006539{
Manuel Pégourié-Gonnard4c90e852019-07-11 10:58:10 +02006540 const unsigned char *p = buf;
6541 const unsigned char * const end = buf + len;
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02006542 size_t session_len;
Janos Follath865b3eb2019-12-16 11:46:15 +00006543 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Andrzej Kurekf9412f72022-10-18 07:30:19 -04006544 tls_prf_fn prf_func = NULL;
Manuel Pégourié-Gonnard4c90e852019-07-11 10:58:10 +02006545
Manuel Pégourié-Gonnard0ff76402019-07-11 09:56:30 +02006546 /*
6547 * The context should have been freshly setup or reset.
6548 * Give the user an error in case of obvious misuse.
Manuel Pégourié-Gonnard4ca930f2019-07-26 16:31:53 +02006549 * (Checking session is useful because it won't be NULL if we're
Manuel Pégourié-Gonnard0ff76402019-07-11 09:56:30 +02006550 * renegotiating, or if the user mistakenly loaded a session first.)
6551 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01006552 if (ssl->state != MBEDTLS_SSL_HELLO_REQUEST ||
6553 ssl->session != NULL) {
6554 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
Manuel Pégourié-Gonnard0ff76402019-07-11 09:56:30 +02006555 }
6556
6557 /*
6558 * We can't check that the config matches the initial one, but we can at
6559 * least check it matches the requirements for serializing.
6560 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01006561 if (ssl->conf->transport != MBEDTLS_SSL_TRANSPORT_DATAGRAM ||
Manuel Pégourié-Gonnard0ff76402019-07-11 09:56:30 +02006562 ssl->conf->max_major_ver < MBEDTLS_SSL_MAJOR_VERSION_3 ||
6563 ssl->conf->min_major_ver > MBEDTLS_SSL_MAJOR_VERSION_3 ||
6564 ssl->conf->max_minor_ver < MBEDTLS_SSL_MINOR_VERSION_3 ||
6565 ssl->conf->min_minor_ver > MBEDTLS_SSL_MINOR_VERSION_3 ||
6566#if defined(MBEDTLS_SSL_RENEGOTIATION)
Manuel Pégourié-Gonnard9a96fd72019-07-23 17:11:24 +02006567 ssl->conf->disable_renegotiation != MBEDTLS_SSL_RENEGOTIATION_DISABLED ||
Manuel Pégourié-Gonnard0ff76402019-07-11 09:56:30 +02006568#endif
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01006569 0) {
6570 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
Manuel Pégourié-Gonnard0ff76402019-07-11 09:56:30 +02006571 }
6572
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01006573 MBEDTLS_SSL_DEBUG_BUF(4, "context to load", buf, len);
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02006574
Manuel Pégourié-Gonnard4c90e852019-07-11 10:58:10 +02006575 /*
6576 * Check version identifier
6577 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01006578 if ((size_t) (end - p) < sizeof(ssl_serialized_context_header)) {
6579 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
Manuel Pégourié-Gonnard4c90e852019-07-11 10:58:10 +02006580 }
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01006581
6582 if (memcmp(p, ssl_serialized_context_header,
6583 sizeof(ssl_serialized_context_header)) != 0) {
6584 return MBEDTLS_ERR_SSL_VERSION_MISMATCH;
6585 }
6586 p += sizeof(ssl_serialized_context_header);
Manuel Pégourié-Gonnard4c90e852019-07-11 10:58:10 +02006587
6588 /*
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02006589 * Session
6590 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01006591 if ((size_t) (end - p) < 4) {
6592 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
6593 }
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02006594
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01006595 session_len = ((size_t) p[0] << 24) |
6596 ((size_t) p[1] << 16) |
6597 ((size_t) p[2] << 8) |
6598 ((size_t) p[3]);
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02006599 p += 4;
6600
Manuel Pégourié-Gonnard142ba732019-07-23 14:43:30 +02006601 /* This has been allocated by ssl_handshake_init(), called by
Hanno Becker43aefe22020-02-05 10:44:56 +00006602 * by either mbedtls_ssl_session_reset_int() or mbedtls_ssl_setup(). */
Manuel Pégourié-Gonnard142ba732019-07-23 14:43:30 +02006603 ssl->session = ssl->session_negotiate;
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02006604 ssl->session_in = ssl->session;
6605 ssl->session_out = ssl->session;
Manuel Pégourié-Gonnard142ba732019-07-23 14:43:30 +02006606 ssl->session_negotiate = NULL;
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02006607
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01006608 if ((size_t) (end - p) < session_len) {
6609 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
6610 }
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02006611
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01006612 ret = ssl_session_load(ssl->session, 1, p, session_len);
6613 if (ret != 0) {
6614 mbedtls_ssl_session_free(ssl->session);
6615 return ret;
Manuel Pégourié-Gonnard45ac1f02019-07-23 16:52:45 +02006616 }
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02006617
6618 p += session_len;
6619
6620 /*
Manuel Pégourié-Gonnardc2a7b892019-07-15 09:04:11 +02006621 * Transform
6622 */
6623
Manuel Pégourié-Gonnard142ba732019-07-23 14:43:30 +02006624 /* This has been allocated by ssl_handshake_init(), called by
Hanno Becker43aefe22020-02-05 10:44:56 +00006625 * by either mbedtls_ssl_session_reset_int() or mbedtls_ssl_setup(). */
Manuel Pégourié-Gonnard142ba732019-07-23 14:43:30 +02006626 ssl->transform = ssl->transform_negotiate;
Manuel Pégourié-Gonnardc2a7b892019-07-15 09:04:11 +02006627 ssl->transform_in = ssl->transform;
6628 ssl->transform_out = ssl->transform;
Manuel Pégourié-Gonnard142ba732019-07-23 14:43:30 +02006629 ssl->transform_negotiate = NULL;
Manuel Pégourié-Gonnardc2a7b892019-07-15 09:04:11 +02006630
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01006631 prf_func = ssl_tls12prf_from_cs(ssl->session->ciphersuite);
6632 if (prf_func == NULL) {
6633 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
6634 }
Andrzej Kurekf9412f72022-10-18 07:30:19 -04006635
Manuel Pégourié-Gonnardc2a7b892019-07-15 09:04:11 +02006636 /* Read random bytes and populate structure */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01006637 if ((size_t) (end - p) < sizeof(ssl->transform->randbytes)) {
6638 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
6639 }
Manuel Pégourié-Gonnardc2a7b892019-07-15 09:04:11 +02006640
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01006641 ret = ssl_populate_transform(ssl->transform,
6642 ssl->session->ciphersuite,
6643 ssl->session->master,
Manuel Pégourié-Gonnardc2a7b892019-07-15 09:04:11 +02006644#if defined(MBEDTLS_SSL_SOME_MODES_USE_MAC)
6645#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01006646 ssl->session->encrypt_then_mac,
Manuel Pégourié-Gonnardc2a7b892019-07-15 09:04:11 +02006647#endif
6648#if defined(MBEDTLS_SSL_TRUNCATED_HMAC)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01006649 ssl->session->trunc_hmac,
Manuel Pégourié-Gonnardc2a7b892019-07-15 09:04:11 +02006650#endif
6651#endif /* MBEDTLS_SSL_SOME_MODES_USE_MAC */
6652#if defined(MBEDTLS_ZLIB_SUPPORT)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01006653 ssl->session->compression,
Manuel Pégourié-Gonnardc2a7b892019-07-15 09:04:11 +02006654#endif
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01006655 prf_func,
6656 p, /* currently pointing to randbytes */
6657 MBEDTLS_SSL_MINOR_VERSION_3, /* (D)TLS 1.2 is forced */
6658 ssl->conf->endpoint,
6659 ssl);
6660 if (ret != 0) {
6661 return ret;
6662 }
Manuel Pégourié-Gonnardc2a7b892019-07-15 09:04:11 +02006663
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01006664 p += sizeof(ssl->transform->randbytes);
Manuel Pégourié-Gonnardc2a7b892019-07-15 09:04:11 +02006665
6666#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
6667 /* Read connection IDs and store them */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01006668 if ((size_t) (end - p) < 1) {
6669 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
6670 }
Manuel Pégourié-Gonnardc2a7b892019-07-15 09:04:11 +02006671
6672 ssl->transform->in_cid_len = *p++;
6673
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01006674 if ((size_t) (end - p) < ssl->transform->in_cid_len + 1u) {
6675 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
6676 }
Manuel Pégourié-Gonnardc2a7b892019-07-15 09:04:11 +02006677
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01006678 memcpy(ssl->transform->in_cid, p, ssl->transform->in_cid_len);
Manuel Pégourié-Gonnardc2a7b892019-07-15 09:04:11 +02006679 p += ssl->transform->in_cid_len;
6680
6681 ssl->transform->out_cid_len = *p++;
6682
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01006683 if ((size_t) (end - p) < ssl->transform->out_cid_len) {
6684 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
6685 }
Manuel Pégourié-Gonnardc2a7b892019-07-15 09:04:11 +02006686
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01006687 memcpy(ssl->transform->out_cid, p, ssl->transform->out_cid_len);
Manuel Pégourié-Gonnardc2a7b892019-07-15 09:04:11 +02006688 p += ssl->transform->out_cid_len;
6689#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
6690
6691 /*
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02006692 * Saved fields from top-level ssl_context structure
6693 */
6694#if defined(MBEDTLS_SSL_DTLS_BADMAC_LIMIT)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01006695 if ((size_t) (end - p) < 4) {
6696 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
6697 }
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02006698
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01006699 ssl->badmac_seen = ((uint32_t) p[0] << 24) |
6700 ((uint32_t) p[1] << 16) |
6701 ((uint32_t) p[2] << 8) |
6702 ((uint32_t) p[3]);
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02006703 p += 4;
6704#endif /* MBEDTLS_SSL_DTLS_BADMAC_LIMIT */
6705
6706#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01006707 if ((size_t) (end - p) < 16) {
6708 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
6709 }
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02006710
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01006711 ssl->in_window_top = ((uint64_t) p[0] << 56) |
6712 ((uint64_t) p[1] << 48) |
6713 ((uint64_t) p[2] << 40) |
6714 ((uint64_t) p[3] << 32) |
6715 ((uint64_t) p[4] << 24) |
6716 ((uint64_t) p[5] << 16) |
6717 ((uint64_t) p[6] << 8) |
6718 ((uint64_t) p[7]);
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02006719 p += 8;
6720
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01006721 ssl->in_window = ((uint64_t) p[0] << 56) |
6722 ((uint64_t) p[1] << 48) |
6723 ((uint64_t) p[2] << 40) |
6724 ((uint64_t) p[3] << 32) |
6725 ((uint64_t) p[4] << 24) |
6726 ((uint64_t) p[5] << 16) |
6727 ((uint64_t) p[6] << 8) |
6728 ((uint64_t) p[7]);
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02006729 p += 8;
6730#endif /* MBEDTLS_SSL_DTLS_ANTI_REPLAY */
6731
6732#if defined(MBEDTLS_SSL_PROTO_DTLS)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01006733 if ((size_t) (end - p) < 1) {
6734 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
6735 }
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02006736
6737 ssl->disable_datagram_packing = *p++;
6738#endif /* MBEDTLS_SSL_PROTO_DTLS */
6739
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01006740 if ((size_t) (end - p) < 8) {
6741 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
6742 }
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02006743
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01006744 memcpy(ssl->cur_out_ctr, p, 8);
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02006745 p += 8;
6746
6747#if defined(MBEDTLS_SSL_PROTO_DTLS)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01006748 if ((size_t) (end - p) < 2) {
6749 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
6750 }
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02006751
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01006752 ssl->mtu = (p[0] << 8) | p[1];
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02006753 p += 2;
6754#endif /* MBEDTLS_SSL_PROTO_DTLS */
6755
6756#if defined(MBEDTLS_SSL_ALPN)
6757 {
6758 uint8_t alpn_len;
6759 const char **cur;
6760
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01006761 if ((size_t) (end - p) < 1) {
6762 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
6763 }
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02006764
6765 alpn_len = *p++;
6766
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01006767 if (alpn_len != 0 && ssl->conf->alpn_list != NULL) {
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02006768 /* alpn_chosen should point to an item in the configured list */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01006769 for (cur = ssl->conf->alpn_list; *cur != NULL; cur++) {
6770 if (strlen(*cur) == alpn_len &&
Waleed Elmelegy98ebf482024-03-15 14:29:24 +00006771 memcmp(p, *cur, alpn_len) == 0) {
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02006772 ssl->alpn_chosen = *cur;
6773 break;
6774 }
6775 }
6776 }
6777
6778 /* can only happen on conf mismatch */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01006779 if (alpn_len != 0 && ssl->alpn_chosen == NULL) {
6780 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
6781 }
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02006782
6783 p += alpn_len;
6784 }
6785#endif /* MBEDTLS_SSL_ALPN */
6786
6787 /*
Manuel Pégourié-Gonnard0eb3eac2019-07-15 11:53:51 +02006788 * Forced fields from top-level ssl_context structure
6789 *
6790 * Most of them already set to the correct value by mbedtls_ssl_init() and
6791 * mbedtls_ssl_reset(), so we only need to set the remaining ones.
6792 */
6793 ssl->state = MBEDTLS_SSL_HANDSHAKE_OVER;
6794
6795 ssl->major_ver = MBEDTLS_SSL_MAJOR_VERSION_3;
6796 ssl->minor_ver = MBEDTLS_SSL_MINOR_VERSION_3;
6797
Hanno Becker361b10d2019-08-30 10:42:49 +01006798 /* Adjust pointers for header fields of outgoing records to
6799 * the given transform, accounting for explicit IV and CID. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01006800 mbedtls_ssl_update_out_pointers(ssl, ssl->transform);
Hanno Becker361b10d2019-08-30 10:42:49 +01006801
Manuel Pégourié-Gonnard0eb3eac2019-07-15 11:53:51 +02006802#if defined(MBEDTLS_SSL_PROTO_DTLS)
6803 ssl->in_epoch = 1;
6804#endif
6805
6806 /* mbedtls_ssl_reset() leaves the handshake sub-structure allocated,
6807 * which we don't want - otherwise we'd end up freeing the wrong transform
Hanno Beckerce5f5fd2020-02-05 10:47:44 +00006808 * by calling mbedtls_ssl_handshake_wrapup_free_hs_transform()
6809 * inappropriately. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01006810 if (ssl->handshake != NULL) {
6811 mbedtls_ssl_handshake_free(ssl);
6812 mbedtls_free(ssl->handshake);
Manuel Pégourié-Gonnard0eb3eac2019-07-15 11:53:51 +02006813 ssl->handshake = NULL;
6814 }
6815
6816 /*
Manuel Pégourié-Gonnard4c90e852019-07-11 10:58:10 +02006817 * Done - should have consumed entire buffer
6818 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01006819 if (p != end) {
6820 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
6821 }
Manuel Pégourié-Gonnardac87e282019-05-28 13:02:16 +02006822
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01006823 return 0;
Manuel Pégourié-Gonnardac87e282019-05-28 13:02:16 +02006824}
6825
6826/*
Manuel Pégourié-Gonnardb9dfc9f2019-07-12 10:50:19 +02006827 * Deserialize context: public wrapper for error cleaning
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02006828 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01006829int mbedtls_ssl_context_load(mbedtls_ssl_context *context,
6830 const unsigned char *buf,
6831 size_t len)
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02006832{
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01006833 int ret = ssl_context_load(context, buf, len);
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02006834
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01006835 if (ret != 0) {
6836 mbedtls_ssl_free(context);
6837 }
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02006838
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01006839 return ret;
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02006840}
Manuel Pégourié-Gonnard5c0e3772019-07-23 16:13:17 +02006841#endif /* MBEDTLS_SSL_CONTEXT_SERIALIZATION */
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02006842
6843/*
Paul Bakker5121ce52009-01-03 21:22:43 +00006844 * Free an SSL context
6845 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01006846void mbedtls_ssl_free(mbedtls_ssl_context *ssl)
Paul Bakker5121ce52009-01-03 21:22:43 +00006847{
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01006848 if (ssl == NULL) {
Paul Bakkeraccaffe2014-06-26 13:37:14 +02006849 return;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01006850 }
Paul Bakkeraccaffe2014-06-26 13:37:14 +02006851
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01006852 MBEDTLS_SSL_DEBUG_MSG(2, ("=> free"));
Paul Bakker5121ce52009-01-03 21:22:43 +00006853
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01006854 if (ssl->out_buf != NULL) {
sander-visserb8aa2072020-05-06 22:05:13 +02006855#if defined(MBEDTLS_SSL_VARIABLE_BUFFER_LENGTH)
6856 size_t out_buf_len = ssl->out_buf_len;
6857#else
6858 size_t out_buf_len = MBEDTLS_SSL_OUT_BUFFER_LEN;
6859#endif
6860
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01006861 mbedtls_platform_zeroize(ssl->out_buf, out_buf_len);
6862 mbedtls_free(ssl->out_buf);
Andrzej Kurek0afa2a12020-03-03 10:39:58 -05006863 ssl->out_buf = NULL;
Paul Bakker5121ce52009-01-03 21:22:43 +00006864 }
6865
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01006866 if (ssl->in_buf != NULL) {
sander-visserb8aa2072020-05-06 22:05:13 +02006867#if defined(MBEDTLS_SSL_VARIABLE_BUFFER_LENGTH)
6868 size_t in_buf_len = ssl->in_buf_len;
6869#else
6870 size_t in_buf_len = MBEDTLS_SSL_IN_BUFFER_LEN;
6871#endif
6872
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01006873 mbedtls_platform_zeroize(ssl->in_buf, in_buf_len);
6874 mbedtls_free(ssl->in_buf);
Andrzej Kurek0afa2a12020-03-03 10:39:58 -05006875 ssl->in_buf = NULL;
Paul Bakker5121ce52009-01-03 21:22:43 +00006876 }
6877
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006878#if defined(MBEDTLS_ZLIB_SUPPORT)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01006879 if (ssl->compress_buf != NULL) {
6880 mbedtls_platform_zeroize(ssl->compress_buf, MBEDTLS_SSL_COMPRESS_BUFFER_LEN);
6881 mbedtls_free(ssl->compress_buf);
Paul Bakker16770332013-10-11 09:59:44 +02006882 }
6883#endif
6884
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01006885 if (ssl->transform) {
6886 mbedtls_ssl_transform_free(ssl->transform);
6887 mbedtls_free(ssl->transform);
Paul Bakker48916f92012-09-16 19:57:18 +00006888 }
6889
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01006890 if (ssl->handshake) {
6891 mbedtls_ssl_handshake_free(ssl);
6892 mbedtls_ssl_transform_free(ssl->transform_negotiate);
6893 mbedtls_ssl_session_free(ssl->session_negotiate);
Paul Bakker48916f92012-09-16 19:57:18 +00006894
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01006895 mbedtls_free(ssl->handshake);
6896 mbedtls_free(ssl->transform_negotiate);
6897 mbedtls_free(ssl->session_negotiate);
Paul Bakker48916f92012-09-16 19:57:18 +00006898 }
6899
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01006900 if (ssl->session) {
6901 mbedtls_ssl_session_free(ssl->session);
6902 mbedtls_free(ssl->session);
Paul Bakkerc0463502013-02-14 11:19:38 +01006903 }
6904
Manuel Pégourié-Gonnard55fab2d2015-05-11 16:15:19 +02006905#if defined(MBEDTLS_X509_CRT_PARSE_C)
Gilles Peskine3a2f75d2025-02-12 23:28:48 +01006906 mbedtls_ssl_free_hostname(ssl);
Paul Bakker0be444a2013-08-27 21:55:01 +02006907#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00006908
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006909#if defined(MBEDTLS_SSL_HW_RECORD_ACCEL)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01006910 if (mbedtls_ssl_hw_record_finish != NULL) {
6911 MBEDTLS_SSL_DEBUG_MSG(2, ("going for mbedtls_ssl_hw_record_finish()"));
6912 mbedtls_ssl_hw_record_finish(ssl);
Paul Bakker05ef8352012-05-08 09:17:57 +00006913 }
6914#endif
6915
Manuel Pégourié-Gonnarde057d3b2015-05-20 10:59:43 +02006916#if defined(MBEDTLS_SSL_DTLS_HELLO_VERIFY) && defined(MBEDTLS_SSL_SRV_C)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01006917 mbedtls_free(ssl->cli_id);
Manuel Pégourié-Gonnard43c02182014-07-22 17:32:01 +02006918#endif
6919
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01006920 MBEDTLS_SSL_DEBUG_MSG(2, ("<= free"));
Paul Bakker2da561c2009-02-05 18:00:28 +00006921
Paul Bakker86f04f42013-02-14 11:20:09 +01006922 /* Actually clear after last debug message */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01006923 mbedtls_platform_zeroize(ssl, sizeof(mbedtls_ssl_context));
Paul Bakker5121ce52009-01-03 21:22:43 +00006924}
6925
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02006926/*
Tom Cosgrove49f99bc2022-12-04 16:44:21 +00006927 * Initialize mbedtls_ssl_config
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02006928 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01006929void mbedtls_ssl_config_init(mbedtls_ssl_config *conf)
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02006930{
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01006931 memset(conf, 0, sizeof(mbedtls_ssl_config));
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02006932}
6933
Gilles Peskineeccd8882020-03-10 12:19:08 +01006934#if defined(MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED)
Chien Wongb6d57932024-02-07 21:48:12 +08006935static const int ssl_preset_default_hashes[] = {
Gilles Peskinec54010c2021-05-12 22:52:09 +02006936#if defined(MBEDTLS_SHA512_C)
Manuel Pégourié-Gonnard47229c72015-12-04 15:02:56 +01006937 MBEDTLS_MD_SHA512,
Gilles Peskinec54010c2021-05-12 22:52:09 +02006938#endif
6939#if defined(MBEDTLS_SHA512_C) && !defined(MBEDTLS_SHA512_NO_SHA384)
Manuel Pégourié-Gonnard47229c72015-12-04 15:02:56 +01006940 MBEDTLS_MD_SHA384,
6941#endif
6942#if defined(MBEDTLS_SHA256_C)
6943 MBEDTLS_MD_SHA256,
6944 MBEDTLS_MD_SHA224,
6945#endif
Gilles Peskine5d2511c2017-05-12 13:16:40 +02006946#if defined(MBEDTLS_SHA1_C) && defined(MBEDTLS_TLS_DEFAULT_ALLOW_SHA1_IN_KEY_EXCHANGE)
Manuel Pégourié-Gonnard47229c72015-12-04 15:02:56 +01006947 MBEDTLS_MD_SHA1,
6948#endif
6949 MBEDTLS_MD_NONE
6950};
Simon Butcherc97b6972015-12-27 23:48:17 +00006951#endif
Manuel Pégourié-Gonnard47229c72015-12-04 15:02:56 +01006952
Chien Wongb6d57932024-02-07 21:48:12 +08006953static const int ssl_preset_suiteb_ciphersuites[] = {
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02006954 MBEDTLS_TLS_ECDHE_ECDSA_WITH_AES_128_GCM_SHA256,
6955 MBEDTLS_TLS_ECDHE_ECDSA_WITH_AES_256_GCM_SHA384,
6956 0
6957};
6958
Gilles Peskineeccd8882020-03-10 12:19:08 +01006959#if defined(MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED)
Chien Wongb6d57932024-02-07 21:48:12 +08006960static const int ssl_preset_suiteb_hashes[] = {
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02006961 MBEDTLS_MD_SHA256,
6962 MBEDTLS_MD_SHA384,
6963 MBEDTLS_MD_NONE
6964};
6965#endif
6966
6967#if defined(MBEDTLS_ECP_C)
Chien Wongb6d57932024-02-07 21:48:12 +08006968static const mbedtls_ecp_group_id ssl_preset_suiteb_curves[] = {
Jaeden Amerod4311042019-06-03 08:27:16 +01006969#if defined(MBEDTLS_ECP_DP_SECP256R1_ENABLED)
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02006970 MBEDTLS_ECP_DP_SECP256R1,
Jaeden Amerod4311042019-06-03 08:27:16 +01006971#endif
6972#if defined(MBEDTLS_ECP_DP_SECP384R1_ENABLED)
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02006973 MBEDTLS_ECP_DP_SECP384R1,
Jaeden Amerod4311042019-06-03 08:27:16 +01006974#endif
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02006975 MBEDTLS_ECP_DP_NONE
6976};
6977#endif
6978
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02006979/*
Tillmann Karras588ad502015-09-25 04:27:22 +02006980 * Load default in mbedtls_ssl_config
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02006981 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01006982int mbedtls_ssl_config_defaults(mbedtls_ssl_config *conf,
6983 int endpoint, int transport, int preset)
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02006984{
Manuel Pégourié-Gonnard8b431fb2015-05-11 12:54:52 +02006985#if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_SSL_SRV_C)
Janos Follath865b3eb2019-12-16 11:46:15 +00006986 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnard8b431fb2015-05-11 12:54:52 +02006987#endif
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02006988
Manuel Pégourié-Gonnard0de074f2015-05-14 12:58:01 +02006989 /* Use the functions here so that they are covered in tests,
6990 * but otherwise access member directly for efficiency */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01006991 mbedtls_ssl_conf_endpoint(conf, endpoint);
6992 mbedtls_ssl_conf_transport(conf, transport);
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02006993
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02006994 /*
6995 * Things that are common to all presets
6996 */
Manuel Pégourié-Gonnard419d5ae2015-05-04 19:32:36 +02006997#if defined(MBEDTLS_SSL_CLI_C)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01006998 if (endpoint == MBEDTLS_SSL_IS_CLIENT) {
Manuel Pégourié-Gonnard419d5ae2015-05-04 19:32:36 +02006999 conf->authmode = MBEDTLS_SSL_VERIFY_REQUIRED;
7000#if defined(MBEDTLS_SSL_SESSION_TICKETS)
7001 conf->session_tickets = MBEDTLS_SSL_SESSION_TICKETS_ENABLED;
7002#endif
7003 }
7004#endif
7005
Manuel Pégourié-Gonnard66dc5552015-05-14 12:28:21 +02007006#if defined(MBEDTLS_ARC4_C)
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02007007 conf->arc4_disabled = MBEDTLS_SSL_ARC4_DISABLED;
Manuel Pégourié-Gonnard66dc5552015-05-14 12:28:21 +02007008#endif
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02007009
7010#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
7011 conf->encrypt_then_mac = MBEDTLS_SSL_ETM_ENABLED;
7012#endif
7013
7014#if defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET)
7015 conf->extended_ms = MBEDTLS_SSL_EXTENDED_MS_ENABLED;
7016#endif
7017
Manuel Pégourié-Gonnard17eab2b2015-05-05 16:34:53 +01007018#if defined(MBEDTLS_SSL_CBC_RECORD_SPLITTING)
7019 conf->cbc_record_splitting = MBEDTLS_SSL_CBC_RECORD_SPLITTING_ENABLED;
7020#endif
7021
Manuel Pégourié-Gonnarde057d3b2015-05-20 10:59:43 +02007022#if defined(MBEDTLS_SSL_DTLS_HELLO_VERIFY) && defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02007023 conf->f_cookie_write = ssl_cookie_write_dummy;
7024 conf->f_cookie_check = ssl_cookie_check_dummy;
7025#endif
7026
7027#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
7028 conf->anti_replay = MBEDTLS_SSL_ANTI_REPLAY_ENABLED;
7029#endif
7030
Janos Follath088ce432017-04-10 12:42:31 +01007031#if defined(MBEDTLS_SSL_SRV_C)
7032 conf->cert_req_ca_list = MBEDTLS_SSL_CERT_REQ_CA_LIST_ENABLED;
7033#endif
7034
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02007035#if defined(MBEDTLS_SSL_PROTO_DTLS)
7036 conf->hs_timeout_min = MBEDTLS_SSL_DTLS_TIMEOUT_DFL_MIN;
7037 conf->hs_timeout_max = MBEDTLS_SSL_DTLS_TIMEOUT_DFL_MAX;
7038#endif
7039
7040#if defined(MBEDTLS_SSL_RENEGOTIATION)
7041 conf->renego_max_records = MBEDTLS_SSL_RENEGO_MAX_RECORDS_DEFAULT;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01007042 memset(conf->renego_period, 0x00, 2);
7043 memset(conf->renego_period + 2, 0xFF, 6);
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02007044#endif
7045
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02007046#if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_SSL_SRV_C)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01007047 if (endpoint == MBEDTLS_SSL_IS_SERVER) {
7048 const unsigned char dhm_p[] =
7049 MBEDTLS_DHM_RFC3526_MODP_2048_P_BIN;
7050 const unsigned char dhm_g[] =
7051 MBEDTLS_DHM_RFC3526_MODP_2048_G_BIN;
Hanno Becker00d0a682017-10-04 13:14:29 +01007052
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01007053 if ((ret = mbedtls_ssl_conf_dh_param_bin(conf,
7054 dhm_p, sizeof(dhm_p),
7055 dhm_g, sizeof(dhm_g))) != 0) {
7056 return ret;
7057 }
7058 }
Manuel Pégourié-Gonnardbd990d62015-06-11 14:49:42 +02007059#endif
7060
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02007061 /*
7062 * Preset-specific defaults
7063 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01007064 switch (preset) {
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02007065 /*
7066 * NSA Suite B
7067 */
7068 case MBEDTLS_SSL_PRESET_SUITEB:
7069 conf->min_major_ver = MBEDTLS_SSL_MAJOR_VERSION_3;
7070 conf->min_minor_ver = MBEDTLS_SSL_MINOR_VERSION_3; /* TLS 1.2 */
7071 conf->max_major_ver = MBEDTLS_SSL_MAX_MAJOR_VERSION;
7072 conf->max_minor_ver = MBEDTLS_SSL_MAX_MINOR_VERSION;
7073
7074 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_0] =
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01007075 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_1] =
7076 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_2] =
7077 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_3] =
7078 ssl_preset_suiteb_ciphersuites;
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02007079
7080#if defined(MBEDTLS_X509_CRT_PARSE_C)
7081 conf->cert_profile = &mbedtls_x509_crt_profile_suiteb;
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02007082#endif
7083
Gilles Peskineeccd8882020-03-10 12:19:08 +01007084#if defined(MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED)
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02007085 conf->sig_hashes = ssl_preset_suiteb_hashes;
7086#endif
7087
7088#if defined(MBEDTLS_ECP_C)
7089 conf->curve_list = ssl_preset_suiteb_curves;
7090#endif
Manuel Pégourié-Gonnardc98204e2015-08-11 04:21:01 +02007091 break;
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02007092
7093 /*
7094 * Default
7095 */
7096 default:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01007097 conf->min_major_ver = (MBEDTLS_SSL_MIN_MAJOR_VERSION >
7098 MBEDTLS_SSL_MIN_VALID_MAJOR_VERSION) ?
7099 MBEDTLS_SSL_MIN_MAJOR_VERSION :
7100 MBEDTLS_SSL_MIN_VALID_MAJOR_VERSION;
7101 conf->min_minor_ver = (MBEDTLS_SSL_MIN_MINOR_VERSION >
7102 MBEDTLS_SSL_MIN_VALID_MINOR_VERSION) ?
7103 MBEDTLS_SSL_MIN_MINOR_VERSION :
7104 MBEDTLS_SSL_MIN_VALID_MINOR_VERSION;
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02007105 conf->max_major_ver = MBEDTLS_SSL_MAX_MAJOR_VERSION;
7106 conf->max_minor_ver = MBEDTLS_SSL_MAX_MINOR_VERSION;
7107
7108#if defined(MBEDTLS_SSL_PROTO_DTLS)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01007109 if (transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM) {
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02007110 conf->min_minor_ver = MBEDTLS_SSL_MINOR_VERSION_2;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01007111 }
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02007112#endif
7113
7114 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_0] =
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01007115 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_1] =
7116 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_2] =
7117 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_3] =
7118 mbedtls_ssl_list_ciphersuites();
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02007119
7120#if defined(MBEDTLS_X509_CRT_PARSE_C)
7121 conf->cert_profile = &mbedtls_x509_crt_profile_default;
7122#endif
7123
Gilles Peskineeccd8882020-03-10 12:19:08 +01007124#if defined(MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED)
Manuel Pégourié-Gonnard47229c72015-12-04 15:02:56 +01007125 conf->sig_hashes = ssl_preset_default_hashes;
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02007126#endif
7127
7128#if defined(MBEDTLS_ECP_C)
7129 conf->curve_list = mbedtls_ecp_grp_id_list();
7130#endif
7131
7132#if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_SSL_CLI_C)
7133 conf->dhm_min_bitlen = 1024;
7134#endif
7135 }
7136
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01007137 return 0;
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02007138}
7139
7140/*
7141 * Free mbedtls_ssl_config
7142 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01007143void mbedtls_ssl_config_free(mbedtls_ssl_config *conf)
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02007144{
7145#if defined(MBEDTLS_DHM_C)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01007146 mbedtls_mpi_free(&conf->dhm_P);
7147 mbedtls_mpi_free(&conf->dhm_G);
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02007148#endif
7149
Gilles Peskineeccd8882020-03-10 12:19:08 +01007150#if defined(MBEDTLS_KEY_EXCHANGE_SOME_PSK_ENABLED)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01007151 if (conf->psk != NULL) {
7152 mbedtls_platform_zeroize(conf->psk, conf->psk_len);
7153 mbedtls_free(conf->psk);
Azim Khan27e8a122018-03-21 14:24:11 +00007154 conf->psk = NULL;
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02007155 conf->psk_len = 0;
junyeonLEE316b1622017-12-20 16:29:30 +09007156 }
7157
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01007158 if (conf->psk_identity != NULL) {
7159 mbedtls_platform_zeroize(conf->psk_identity, conf->psk_identity_len);
7160 mbedtls_free(conf->psk_identity);
Azim Khan27e8a122018-03-21 14:24:11 +00007161 conf->psk_identity = NULL;
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02007162 conf->psk_identity_len = 0;
7163 }
7164#endif
7165
7166#if defined(MBEDTLS_X509_CRT_PARSE_C)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01007167 ssl_key_cert_free(conf->key_cert);
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02007168#endif
7169
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01007170 mbedtls_platform_zeroize(conf, sizeof(mbedtls_ssl_config));
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02007171}
7172
Manuel Pégourié-Gonnard5674a972015-10-19 15:14:03 +02007173#if defined(MBEDTLS_PK_C) && \
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01007174 (defined(MBEDTLS_RSA_C) || defined(MBEDTLS_ECDSA_C))
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02007175/*
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007176 * Convert between MBEDTLS_PK_XXX and SSL_SIG_XXX
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02007177 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01007178unsigned char mbedtls_ssl_sig_from_pk(mbedtls_pk_context *pk)
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02007179{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007180#if defined(MBEDTLS_RSA_C)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01007181 if (mbedtls_pk_can_do(pk, MBEDTLS_PK_RSA)) {
7182 return MBEDTLS_SSL_SIG_RSA;
7183 }
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02007184#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007185#if defined(MBEDTLS_ECDSA_C)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01007186 if (mbedtls_pk_can_do(pk, MBEDTLS_PK_ECDSA)) {
7187 return MBEDTLS_SSL_SIG_ECDSA;
7188 }
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02007189#endif
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01007190 return MBEDTLS_SSL_SIG_ANON;
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02007191}
7192
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01007193unsigned char mbedtls_ssl_sig_from_pk_alg(mbedtls_pk_type_t type)
Hanno Becker7e5437a2017-04-28 17:15:26 +01007194{
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01007195 switch (type) {
Hanno Becker7e5437a2017-04-28 17:15:26 +01007196 case MBEDTLS_PK_RSA:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01007197 return MBEDTLS_SSL_SIG_RSA;
Hanno Becker7e5437a2017-04-28 17:15:26 +01007198 case MBEDTLS_PK_ECDSA:
7199 case MBEDTLS_PK_ECKEY:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01007200 return MBEDTLS_SSL_SIG_ECDSA;
Hanno Becker7e5437a2017-04-28 17:15:26 +01007201 default:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01007202 return MBEDTLS_SSL_SIG_ANON;
Hanno Becker7e5437a2017-04-28 17:15:26 +01007203 }
7204}
7205
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01007206mbedtls_pk_type_t mbedtls_ssl_pk_alg_from_sig(unsigned char sig)
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02007207{
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01007208 switch (sig) {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007209#if defined(MBEDTLS_RSA_C)
7210 case MBEDTLS_SSL_SIG_RSA:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01007211 return MBEDTLS_PK_RSA;
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02007212#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007213#if defined(MBEDTLS_ECDSA_C)
7214 case MBEDTLS_SSL_SIG_ECDSA:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01007215 return MBEDTLS_PK_ECDSA;
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02007216#endif
7217 default:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01007218 return MBEDTLS_PK_NONE;
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02007219 }
7220}
Manuel Pégourié-Gonnard5674a972015-10-19 15:14:03 +02007221#endif /* MBEDTLS_PK_C && ( MBEDTLS_RSA_C || MBEDTLS_ECDSA_C ) */
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02007222
Hanno Becker7e5437a2017-04-28 17:15:26 +01007223#if defined(MBEDTLS_SSL_PROTO_TLS1_2) && \
Gilles Peskineeccd8882020-03-10 12:19:08 +01007224 defined(MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED)
Hanno Becker7e5437a2017-04-28 17:15:26 +01007225
7226/* Find an entry in a signature-hash set matching a given hash algorithm. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01007227mbedtls_md_type_t mbedtls_ssl_sig_hash_set_find(mbedtls_ssl_sig_hash_set_t *set,
7228 mbedtls_pk_type_t sig_alg)
Hanno Becker7e5437a2017-04-28 17:15:26 +01007229{
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01007230 switch (sig_alg) {
Hanno Becker7e5437a2017-04-28 17:15:26 +01007231 case MBEDTLS_PK_RSA:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01007232 return set->rsa;
Hanno Becker7e5437a2017-04-28 17:15:26 +01007233 case MBEDTLS_PK_ECDSA:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01007234 return set->ecdsa;
Hanno Becker7e5437a2017-04-28 17:15:26 +01007235 default:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01007236 return MBEDTLS_MD_NONE;
Hanno Becker7e5437a2017-04-28 17:15:26 +01007237 }
7238}
7239
7240/* Add a signature-hash-pair to a signature-hash set */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01007241void mbedtls_ssl_sig_hash_set_add(mbedtls_ssl_sig_hash_set_t *set,
7242 mbedtls_pk_type_t sig_alg,
7243 mbedtls_md_type_t md_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 if (set->rsa == MBEDTLS_MD_NONE) {
Hanno Becker7e5437a2017-04-28 17:15:26 +01007248 set->rsa = md_alg;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01007249 }
Hanno Becker7e5437a2017-04-28 17:15:26 +01007250 break;
7251
7252 case MBEDTLS_PK_ECDSA:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01007253 if (set->ecdsa == MBEDTLS_MD_NONE) {
Hanno Becker7e5437a2017-04-28 17:15:26 +01007254 set->ecdsa = md_alg;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01007255 }
Hanno Becker7e5437a2017-04-28 17:15:26 +01007256 break;
7257
7258 default:
7259 break;
7260 }
7261}
7262
7263/* Allow exactly one hash algorithm for each signature. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01007264void mbedtls_ssl_sig_hash_set_const_hash(mbedtls_ssl_sig_hash_set_t *set,
7265 mbedtls_md_type_t md_alg)
Hanno Becker7e5437a2017-04-28 17:15:26 +01007266{
7267 set->rsa = md_alg;
7268 set->ecdsa = md_alg;
7269}
7270
7271#endif /* MBEDTLS_SSL_PROTO_TLS1_2) &&
Gilles Peskineeccd8882020-03-10 12:19:08 +01007272 MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED */
Hanno Becker7e5437a2017-04-28 17:15:26 +01007273
Manuel Pégourié-Gonnard1a483832013-09-20 12:29:15 +02007274/*
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +02007275 * Convert from MBEDTLS_SSL_HASH_XXX to MBEDTLS_MD_XXX
Manuel Pégourié-Gonnard1a483832013-09-20 12:29:15 +02007276 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01007277mbedtls_md_type_t mbedtls_ssl_md_alg_from_hash(unsigned char hash)
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02007278{
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01007279 switch (hash) {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007280#if defined(MBEDTLS_MD5_C)
7281 case MBEDTLS_SSL_HASH_MD5:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01007282 return MBEDTLS_MD_MD5;
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02007283#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007284#if defined(MBEDTLS_SHA1_C)
7285 case MBEDTLS_SSL_HASH_SHA1:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01007286 return MBEDTLS_MD_SHA1;
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02007287#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007288#if defined(MBEDTLS_SHA256_C)
7289 case MBEDTLS_SSL_HASH_SHA224:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01007290 return MBEDTLS_MD_SHA224;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007291 case MBEDTLS_SSL_HASH_SHA256:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01007292 return MBEDTLS_MD_SHA256;
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02007293#endif
Gilles Peskined2d59372021-05-12 22:43:27 +02007294#if defined(MBEDTLS_SHA512_C) && !defined(MBEDTLS_SHA512_NO_SHA384)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007295 case MBEDTLS_SSL_HASH_SHA384:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01007296 return MBEDTLS_MD_SHA384;
Gilles Peskinec54010c2021-05-12 22:52:09 +02007297#endif
7298#if defined(MBEDTLS_SHA512_C)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007299 case MBEDTLS_SSL_HASH_SHA512:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01007300 return MBEDTLS_MD_SHA512;
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02007301#endif
7302 default:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01007303 return MBEDTLS_MD_NONE;
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02007304 }
7305}
7306
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +02007307/*
7308 * Convert from MBEDTLS_MD_XXX to MBEDTLS_SSL_HASH_XXX
7309 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01007310unsigned char mbedtls_ssl_hash_from_md_alg(int md)
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +02007311{
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01007312 switch (md) {
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +02007313#if defined(MBEDTLS_MD5_C)
7314 case MBEDTLS_MD_MD5:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01007315 return MBEDTLS_SSL_HASH_MD5;
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +02007316#endif
7317#if defined(MBEDTLS_SHA1_C)
7318 case MBEDTLS_MD_SHA1:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01007319 return MBEDTLS_SSL_HASH_SHA1;
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +02007320#endif
7321#if defined(MBEDTLS_SHA256_C)
7322 case MBEDTLS_MD_SHA224:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01007323 return MBEDTLS_SSL_HASH_SHA224;
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +02007324 case MBEDTLS_MD_SHA256:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01007325 return MBEDTLS_SSL_HASH_SHA256;
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +02007326#endif
Gilles Peskined2d59372021-05-12 22:43:27 +02007327#if defined(MBEDTLS_SHA512_C) && !defined(MBEDTLS_SHA512_NO_SHA384)
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +02007328 case MBEDTLS_MD_SHA384:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01007329 return MBEDTLS_SSL_HASH_SHA384;
Gilles Peskinec54010c2021-05-12 22:52:09 +02007330#endif
7331#if defined(MBEDTLS_SHA512_C)
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +02007332 case MBEDTLS_MD_SHA512:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01007333 return MBEDTLS_SSL_HASH_SHA512;
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +02007334#endif
7335 default:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01007336 return MBEDTLS_SSL_HASH_NONE;
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +02007337 }
7338}
7339
Manuel Pégourié-Gonnardb541da62015-06-17 11:43:30 +02007340#if defined(MBEDTLS_ECP_C)
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01007341/*
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +02007342 * Check if a curve proposed by the peer is in our list.
Manuel Pégourié-Gonnard9d412d82015-06-17 12:10:46 +02007343 * Return 0 if we're willing to use it, -1 otherwise.
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01007344 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01007345int mbedtls_ssl_check_curve(const mbedtls_ssl_context *ssl, mbedtls_ecp_group_id grp_id)
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01007346{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007347 const mbedtls_ecp_group_id *gid;
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01007348
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01007349 if (ssl->conf->curve_list == NULL) {
7350 return -1;
7351 }
Manuel Pégourié-Gonnard9d412d82015-06-17 12:10:46 +02007352
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01007353 for (gid = ssl->conf->curve_list; *gid != MBEDTLS_ECP_DP_NONE; gid++) {
7354 if (*gid == grp_id) {
7355 return 0;
7356 }
7357 }
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01007358
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01007359 return -1;
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01007360}
Manuel Pégourié-Gonnard298d6cc2022-02-14 11:34:47 +01007361
7362/*
7363 * Same as mbedtls_ssl_check_curve() but takes a TLS ID for the curve.
7364 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01007365int mbedtls_ssl_check_curve_tls_id(const mbedtls_ssl_context *ssl, uint16_t tls_id)
Manuel Pégourié-Gonnard298d6cc2022-02-14 11:34:47 +01007366{
7367 const mbedtls_ecp_curve_info *curve_info =
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01007368 mbedtls_ecp_curve_info_from_tls_id(tls_id);
7369 if (curve_info == NULL) {
7370 return -1;
7371 }
7372 return mbedtls_ssl_check_curve(ssl, curve_info->grp_id);
Manuel Pégourié-Gonnard298d6cc2022-02-14 11:34:47 +01007373}
Manuel Pégourié-Gonnardb541da62015-06-17 11:43:30 +02007374#endif /* MBEDTLS_ECP_C */
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02007375
Gilles Peskineeccd8882020-03-10 12:19:08 +01007376#if defined(MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED)
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +02007377/*
7378 * Check if a hash proposed by the peer is in our list.
7379 * Return 0 if we're willing to use it, -1 otherwise.
7380 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01007381int mbedtls_ssl_check_sig_hash(const mbedtls_ssl_context *ssl,
7382 mbedtls_md_type_t md)
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +02007383{
7384 const int *cur;
7385
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01007386 if (ssl->conf->sig_hashes == NULL) {
7387 return -1;
7388 }
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +02007389
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01007390 for (cur = ssl->conf->sig_hashes; *cur != MBEDTLS_MD_NONE; cur++) {
7391 if (*cur == (int) md) {
7392 return 0;
7393 }
7394 }
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +02007395
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01007396 return -1;
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +02007397}
Gilles Peskineeccd8882020-03-10 12:19:08 +01007398#endif /* MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED */
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +02007399
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007400#if defined(MBEDTLS_X509_CRT_PARSE_C)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01007401int mbedtls_ssl_check_cert_usage(const mbedtls_x509_crt *cert,
7402 const mbedtls_ssl_ciphersuite_t *ciphersuite,
7403 int cert_endpoint,
7404 uint32_t *flags)
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02007405{
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +01007406 int ret = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007407#if defined(MBEDTLS_X509_CHECK_KEY_USAGE)
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02007408 int usage = 0;
7409#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007410#if defined(MBEDTLS_X509_CHECK_EXTENDED_KEY_USAGE)
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +02007411 const char *ext_oid;
7412 size_t ext_len;
7413#endif
7414
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007415#if !defined(MBEDTLS_X509_CHECK_KEY_USAGE) && \
7416 !defined(MBEDTLS_X509_CHECK_EXTENDED_KEY_USAGE)
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +02007417 ((void) cert);
7418 ((void) cert_endpoint);
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +01007419 ((void) flags);
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +02007420#endif
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02007421
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007422#if defined(MBEDTLS_X509_CHECK_KEY_USAGE)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01007423 if (cert_endpoint == MBEDTLS_SSL_IS_SERVER) {
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02007424 /* Server part of the key exchange */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01007425 switch (ciphersuite->key_exchange) {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007426 case MBEDTLS_KEY_EXCHANGE_RSA:
7427 case MBEDTLS_KEY_EXCHANGE_RSA_PSK:
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +01007428 usage = MBEDTLS_X509_KU_KEY_ENCIPHERMENT;
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02007429 break;
7430
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007431 case MBEDTLS_KEY_EXCHANGE_DHE_RSA:
7432 case MBEDTLS_KEY_EXCHANGE_ECDHE_RSA:
7433 case MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA:
7434 usage = MBEDTLS_X509_KU_DIGITAL_SIGNATURE;
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02007435 break;
7436
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007437 case MBEDTLS_KEY_EXCHANGE_ECDH_RSA:
7438 case MBEDTLS_KEY_EXCHANGE_ECDH_ECDSA:
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +01007439 usage = MBEDTLS_X509_KU_KEY_AGREEMENT;
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02007440 break;
7441
7442 /* Don't use default: we want warnings when adding new values */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007443 case MBEDTLS_KEY_EXCHANGE_NONE:
7444 case MBEDTLS_KEY_EXCHANGE_PSK:
7445 case MBEDTLS_KEY_EXCHANGE_DHE_PSK:
7446 case MBEDTLS_KEY_EXCHANGE_ECDHE_PSK:
Manuel Pégourié-Gonnard557535d2015-09-15 17:53:32 +02007447 case MBEDTLS_KEY_EXCHANGE_ECJPAKE:
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02007448 usage = 0;
7449 }
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01007450 } else {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007451 /* Client auth: we only implement rsa_sign and mbedtls_ecdsa_sign for now */
7452 usage = MBEDTLS_X509_KU_DIGITAL_SIGNATURE;
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02007453 }
7454
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01007455 if (mbedtls_x509_crt_check_key_usage(cert, usage) != 0) {
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +01007456 *flags |= MBEDTLS_X509_BADCERT_KEY_USAGE;
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +01007457 ret = -1;
7458 }
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +02007459#else
7460 ((void) ciphersuite);
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007461#endif /* MBEDTLS_X509_CHECK_KEY_USAGE */
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02007462
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007463#if defined(MBEDTLS_X509_CHECK_EXTENDED_KEY_USAGE)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01007464 if (cert_endpoint == MBEDTLS_SSL_IS_SERVER) {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007465 ext_oid = MBEDTLS_OID_SERVER_AUTH;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01007466 ext_len = MBEDTLS_OID_SIZE(MBEDTLS_OID_SERVER_AUTH);
7467 } else {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007468 ext_oid = MBEDTLS_OID_CLIENT_AUTH;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01007469 ext_len = MBEDTLS_OID_SIZE(MBEDTLS_OID_CLIENT_AUTH);
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +02007470 }
7471
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01007472 if (mbedtls_x509_crt_check_extended_key_usage(cert, ext_oid, ext_len) != 0) {
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +01007473 *flags |= MBEDTLS_X509_BADCERT_EXT_KEY_USAGE;
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +01007474 ret = -1;
7475 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007476#endif /* MBEDTLS_X509_CHECK_EXTENDED_KEY_USAGE */
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +02007477
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01007478 return ret;
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02007479}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007480#endif /* MBEDTLS_X509_CRT_PARSE_C */
Manuel Pégourié-Gonnard3a306b92014-04-29 15:11:17 +02007481
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01007482int mbedtls_ssl_set_calc_verify_md(mbedtls_ssl_context *ssl, int md)
Simon Butcher99000142016-10-13 17:21:01 +01007483{
7484#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01007485 if (ssl->minor_ver != MBEDTLS_SSL_MINOR_VERSION_3) {
Simon Butcher99000142016-10-13 17:21:01 +01007486 return MBEDTLS_ERR_SSL_INVALID_VERIFY_HASH;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01007487 }
Simon Butcher99000142016-10-13 17:21:01 +01007488
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01007489 switch (md) {
Simon Butcher99000142016-10-13 17:21:01 +01007490#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1)
7491#if defined(MBEDTLS_MD5_C)
7492 case MBEDTLS_SSL_HASH_MD5:
Janos Follath182013f2016-10-25 10:50:22 +01007493 return MBEDTLS_ERR_SSL_INVALID_VERIFY_HASH;
Simon Butcher99000142016-10-13 17:21:01 +01007494#endif
7495#if defined(MBEDTLS_SHA1_C)
7496 case MBEDTLS_SSL_HASH_SHA1:
7497 ssl->handshake->calc_verify = ssl_calc_verify_tls;
7498 break;
7499#endif
7500#endif /* MBEDTLS_SSL_PROTO_TLS1 || MBEDTLS_SSL_PROTO_TLS1_1 */
Gilles Peskined2d59372021-05-12 22:43:27 +02007501#if defined(MBEDTLS_SHA512_C) && !defined(MBEDTLS_SHA512_NO_SHA384)
Simon Butcher99000142016-10-13 17:21:01 +01007502 case MBEDTLS_SSL_HASH_SHA384:
7503 ssl->handshake->calc_verify = ssl_calc_verify_tls_sha384;
7504 break;
7505#endif
7506#if defined(MBEDTLS_SHA256_C)
7507 case MBEDTLS_SSL_HASH_SHA256:
7508 ssl->handshake->calc_verify = ssl_calc_verify_tls_sha256;
7509 break;
7510#endif
7511 default:
7512 return MBEDTLS_ERR_SSL_INVALID_VERIFY_HASH;
7513 }
7514
7515 return 0;
7516#else /* !MBEDTLS_SSL_PROTO_TLS1_2 */
7517 (void) ssl;
7518 (void) md;
7519
7520 return MBEDTLS_ERR_SSL_INVALID_VERIFY_HASH;
7521#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
7522}
7523
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +01007524#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1) || \
7525 defined(MBEDTLS_SSL_PROTO_TLS1_1)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01007526int mbedtls_ssl_get_key_exchange_md_ssl_tls(mbedtls_ssl_context *ssl,
7527 unsigned char *output,
7528 unsigned char *data, size_t data_len)
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +01007529{
7530 int ret = 0;
7531 mbedtls_md5_context mbedtls_md5;
7532 mbedtls_sha1_context mbedtls_sha1;
7533
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01007534 mbedtls_md5_init(&mbedtls_md5);
7535 mbedtls_sha1_init(&mbedtls_sha1);
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +01007536
7537 /*
7538 * digitally-signed struct {
7539 * opaque md5_hash[16];
7540 * opaque sha_hash[20];
7541 * };
7542 *
7543 * md5_hash
7544 * MD5(ClientHello.random + ServerHello.random
7545 * + ServerParams);
7546 * sha_hash
7547 * SHA(ClientHello.random + ServerHello.random
7548 * + ServerParams);
7549 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01007550 if ((ret = mbedtls_md5_starts_ret(&mbedtls_md5)) != 0) {
7551 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_md5_starts_ret", ret);
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +01007552 goto exit;
7553 }
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01007554 if ((ret = mbedtls_md5_update_ret(&mbedtls_md5,
7555 ssl->handshake->randbytes, 64)) != 0) {
7556 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_md5_update_ret", ret);
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +01007557 goto exit;
7558 }
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01007559 if ((ret = mbedtls_md5_update_ret(&mbedtls_md5, data, data_len)) != 0) {
7560 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_md5_update_ret", ret);
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +01007561 goto exit;
7562 }
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01007563 if ((ret = mbedtls_md5_finish_ret(&mbedtls_md5, output)) != 0) {
7564 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_md5_finish_ret", ret);
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +01007565 goto exit;
7566 }
7567
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01007568 if ((ret = mbedtls_sha1_starts_ret(&mbedtls_sha1)) != 0) {
7569 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_sha1_starts_ret", ret);
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +01007570 goto exit;
7571 }
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01007572 if ((ret = mbedtls_sha1_update_ret(&mbedtls_sha1,
7573 ssl->handshake->randbytes, 64)) != 0) {
7574 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_sha1_update_ret", ret);
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +01007575 goto exit;
7576 }
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01007577 if ((ret = mbedtls_sha1_update_ret(&mbedtls_sha1, data,
7578 data_len)) != 0) {
7579 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_sha1_update_ret", ret);
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +01007580 goto exit;
7581 }
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01007582 if ((ret = mbedtls_sha1_finish_ret(&mbedtls_sha1,
7583 output + 16)) != 0) {
7584 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_sha1_finish_ret", ret);
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +01007585 goto exit;
7586 }
7587
7588exit:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01007589 mbedtls_md5_free(&mbedtls_md5);
7590 mbedtls_sha1_free(&mbedtls_sha1);
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +01007591
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01007592 if (ret != 0) {
7593 mbedtls_ssl_send_alert_message(ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
7594 MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR);
7595 }
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +01007596
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01007597 return ret;
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +01007598
7599}
7600#endif /* MBEDTLS_SSL_PROTO_SSL3 || MBEDTLS_SSL_PROTO_TLS1 || \
7601 MBEDTLS_SSL_PROTO_TLS1_1 */
7602
7603#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1) || \
7604 defined(MBEDTLS_SSL_PROTO_TLS1_2)
Andrzej Kurekd6db9be2019-01-10 05:27:10 -05007605
7606#if defined(MBEDTLS_USE_PSA_CRYPTO)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01007607int mbedtls_ssl_get_key_exchange_md_tls1_2(mbedtls_ssl_context *ssl,
7608 unsigned char *hash, size_t *hashlen,
7609 unsigned char *data, size_t data_len,
7610 mbedtls_md_type_t md_alg)
Andrzej Kurekd6db9be2019-01-10 05:27:10 -05007611{
Andrzej Kurek814feff2019-01-14 04:35:19 -05007612 psa_status_t status;
Jaeden Amero34973232019-02-20 10:32:28 +00007613 psa_hash_operation_t hash_operation = PSA_HASH_OPERATION_INIT;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01007614 psa_algorithm_t hash_alg = mbedtls_psa_translate_md(md_alg);
Andrzej Kurekd6db9be2019-01-10 05:27:10 -05007615
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01007616 MBEDTLS_SSL_DEBUG_MSG(3, ("Perform PSA-based computation of digest of ServerKeyExchange"));
Andrzej Kurek814feff2019-01-14 04:35:19 -05007617
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01007618 if ((status = psa_hash_setup(&hash_operation,
7619 hash_alg)) != PSA_SUCCESS) {
7620 MBEDTLS_SSL_DEBUG_RET(1, "psa_hash_setup", status);
Andrzej Kurekd6db9be2019-01-10 05:27:10 -05007621 goto exit;
7622 }
7623
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01007624 if ((status = psa_hash_update(&hash_operation, ssl->handshake->randbytes,
7625 64)) != PSA_SUCCESS) {
7626 MBEDTLS_SSL_DEBUG_RET(1, "psa_hash_update", status);
Andrzej Kurekd6db9be2019-01-10 05:27:10 -05007627 goto exit;
7628 }
7629
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01007630 if ((status = psa_hash_update(&hash_operation,
7631 data, data_len)) != PSA_SUCCESS) {
7632 MBEDTLS_SSL_DEBUG_RET(1, "psa_hash_update", status);
Andrzej Kurekd6db9be2019-01-10 05:27:10 -05007633 goto exit;
7634 }
7635
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01007636 if ((status = psa_hash_finish(&hash_operation, hash, PSA_HASH_MAX_SIZE,
7637 hashlen)) != PSA_SUCCESS) {
7638 MBEDTLS_SSL_DEBUG_RET(1, "psa_hash_finish", status);
7639 goto exit;
Andrzej Kurekd6db9be2019-01-10 05:27:10 -05007640 }
7641
7642exit:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01007643 if (status != PSA_SUCCESS) {
7644 mbedtls_ssl_send_alert_message(ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
7645 MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR);
7646 switch (status) {
Andrzej Kurekd6db9be2019-01-10 05:27:10 -05007647 case PSA_ERROR_NOT_SUPPORTED:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01007648 return MBEDTLS_ERR_MD_FEATURE_UNAVAILABLE;
Andrzej Kurek814feff2019-01-14 04:35:19 -05007649 case PSA_ERROR_BAD_STATE: /* Intentional fallthrough */
Andrzej Kurekd6db9be2019-01-10 05:27:10 -05007650 case PSA_ERROR_BUFFER_TOO_SMALL:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01007651 return MBEDTLS_ERR_MD_BAD_INPUT_DATA;
Andrzej Kurekd6db9be2019-01-10 05:27:10 -05007652 case PSA_ERROR_INSUFFICIENT_MEMORY:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01007653 return MBEDTLS_ERR_MD_ALLOC_FAILED;
Andrzej Kurekd6db9be2019-01-10 05:27:10 -05007654 default:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01007655 return MBEDTLS_ERR_MD_HW_ACCEL_FAILED;
Andrzej Kurekd6db9be2019-01-10 05:27:10 -05007656 }
7657 }
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01007658 return 0;
Andrzej Kurekd6db9be2019-01-10 05:27:10 -05007659}
7660
7661#else
7662
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01007663int mbedtls_ssl_get_key_exchange_md_tls1_2(mbedtls_ssl_context *ssl,
7664 unsigned char *hash, size_t *hashlen,
7665 unsigned char *data, size_t data_len,
7666 mbedtls_md_type_t md_alg)
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +01007667{
7668 int ret = 0;
7669 mbedtls_md_context_t ctx;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01007670 const mbedtls_md_info_t *md_info = mbedtls_md_info_from_type(md_alg);
7671 *hashlen = mbedtls_md_get_size(md_info);
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +01007672
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01007673 MBEDTLS_SSL_DEBUG_MSG(3, ("Perform mbedtls-based computation of digest of ServerKeyExchange"));
Andrzej Kurek814feff2019-01-14 04:35:19 -05007674
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01007675 mbedtls_md_init(&ctx);
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +01007676
7677 /*
7678 * digitally-signed struct {
7679 * opaque client_random[32];
7680 * opaque server_random[32];
7681 * ServerDHParams params;
7682 * };
7683 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01007684 if ((ret = mbedtls_md_setup(&ctx, md_info, 0)) != 0) {
7685 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_md_setup", ret);
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +01007686 goto exit;
7687 }
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01007688 if ((ret = mbedtls_md_starts(&ctx)) != 0) {
7689 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_md_starts", ret);
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +01007690 goto exit;
7691 }
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01007692 if ((ret = mbedtls_md_update(&ctx, ssl->handshake->randbytes, 64)) != 0) {
7693 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_md_update", ret);
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +01007694 goto exit;
7695 }
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01007696 if ((ret = mbedtls_md_update(&ctx, data, data_len)) != 0) {
7697 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_md_update", ret);
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +01007698 goto exit;
7699 }
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01007700 if ((ret = mbedtls_md_finish(&ctx, hash)) != 0) {
7701 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_md_finish", ret);
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +01007702 goto exit;
7703 }
7704
7705exit:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01007706 mbedtls_md_free(&ctx);
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +01007707
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01007708 if (ret != 0) {
7709 mbedtls_ssl_send_alert_message(ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
7710 MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR);
7711 }
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +01007712
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01007713 return ret;
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +01007714}
Andrzej Kurekd6db9be2019-01-10 05:27:10 -05007715#endif /* MBEDTLS_USE_PSA_CRYPTO */
7716
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +01007717#endif /* MBEDTLS_SSL_PROTO_TLS1 || MBEDTLS_SSL_PROTO_TLS1_1 || \
7718 MBEDTLS_SSL_PROTO_TLS1_2 */
7719
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007720#endif /* MBEDTLS_SSL_TLS_C */