blob: 65d5b964d372f8c47bf7fd8e52698e64f592b96c [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 *);
895static void 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 *);
900static void 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 *);
907static void 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 *);
913static void 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)
Paul Bakker1ef83d62012-04-11 12:09:53 +00003139static void 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"));
Paul Bakker5121ce52009-01-03 21:22:43 +00003221}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003222#endif /* MBEDTLS_SSL_PROTO_SSL3 */
Paul Bakker5121ce52009-01-03 21:22:43 +00003223
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003224#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1)
Paul Bakker1ef83d62012-04-11 12:09:53 +00003225static void ssl_calc_finished_tls(
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003226 mbedtls_ssl_context *ssl, unsigned char *buf, int from)
Paul Bakker5121ce52009-01-03 21:22:43 +00003227{
Paul Bakker1ef83d62012-04-11 12:09:53 +00003228 int len = 12;
Paul Bakker3c2122f2013-06-24 19:03:14 +02003229 const char *sender;
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02003230 mbedtls_md5_context md5;
3231 mbedtls_sha1_context sha1;
Paul Bakker1ef83d62012-04-11 12:09:53 +00003232 unsigned char padbuf[36];
Paul Bakker5121ce52009-01-03 21:22:43 +00003233
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003234 mbedtls_ssl_session *session = ssl->session_negotiate;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003235 if (!session) {
Paul Bakker48916f92012-09-16 19:57:18 +00003236 session = ssl->session;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003237 }
Paul Bakker48916f92012-09-16 19:57:18 +00003238
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003239 MBEDTLS_SSL_DEBUG_MSG(2, ("=> calc finished tls"));
Paul Bakker5121ce52009-01-03 21:22:43 +00003240
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003241 mbedtls_md5_init(&md5);
3242 mbedtls_sha1_init(&sha1);
Manuel Pégourié-Gonnard001f2b62015-07-06 16:21:13 +02003243
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003244 mbedtls_md5_clone(&md5, &ssl->handshake->fin_md5);
3245 mbedtls_sha1_clone(&sha1, &ssl->handshake->fin_sha1);
Paul Bakker5121ce52009-01-03 21:22:43 +00003246
Paul Bakker1ef83d62012-04-11 12:09:53 +00003247 /*
3248 * TLSv1:
3249 * hash = PRF( master, finished_label,
3250 * MD5( handshake ) + SHA1( handshake ) )[0..11]
3251 */
Paul Bakker5121ce52009-01-03 21:22:43 +00003252
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003253#if !defined(MBEDTLS_MD5_ALT)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003254 MBEDTLS_SSL_DEBUG_BUF(4, "finished md5 state", (unsigned char *)
3255 md5.state, sizeof(md5.state));
Paul Bakker90995b52013-06-24 19:20:35 +02003256#endif
Paul Bakker1ef83d62012-04-11 12:09:53 +00003257
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003258#if !defined(MBEDTLS_SHA1_ALT)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003259 MBEDTLS_SSL_DEBUG_BUF(4, "finished sha1 state", (unsigned char *)
3260 sha1.state, sizeof(sha1.state));
Paul Bakker90995b52013-06-24 19:20:35 +02003261#endif
Paul Bakker1ef83d62012-04-11 12:09:53 +00003262
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003263 sender = (from == MBEDTLS_SSL_IS_CLIENT)
Paul Bakker3c2122f2013-06-24 19:03:14 +02003264 ? "client finished"
3265 : "server finished";
Paul Bakker1ef83d62012-04-11 12:09:53 +00003266
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003267 mbedtls_md5_finish_ret(&md5, padbuf);
3268 mbedtls_sha1_finish_ret(&sha1, padbuf + 16);
Paul Bakker1ef83d62012-04-11 12:09:53 +00003269
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003270 ssl->handshake->tls_prf(session->master, 48, sender,
3271 padbuf, 36, buf, len);
Paul Bakker1ef83d62012-04-11 12:09:53 +00003272
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003273 MBEDTLS_SSL_DEBUG_BUF(3, "calc finished result", buf, len);
Paul Bakker1ef83d62012-04-11 12:09:53 +00003274
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003275 mbedtls_md5_free(&md5);
3276 mbedtls_sha1_free(&sha1);
Paul Bakker1ef83d62012-04-11 12:09:53 +00003277
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003278 mbedtls_platform_zeroize(padbuf, sizeof(padbuf));
Paul Bakker1ef83d62012-04-11 12:09:53 +00003279
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003280 MBEDTLS_SSL_DEBUG_MSG(2, ("<= calc finished"));
Paul Bakker1ef83d62012-04-11 12:09:53 +00003281}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003282#endif /* MBEDTLS_SSL_PROTO_TLS1 || MBEDTLS_SSL_PROTO_TLS1_1 */
Paul Bakker1ef83d62012-04-11 12:09:53 +00003283
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003284#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
3285#if defined(MBEDTLS_SHA256_C)
Paul Bakkerca4ab492012-04-18 14:23:57 +00003286static void ssl_calc_finished_tls_sha256(
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003287 mbedtls_ssl_context *ssl, unsigned char *buf, int from)
Paul Bakker1ef83d62012-04-11 12:09:53 +00003288{
3289 int len = 12;
Paul Bakker3c2122f2013-06-24 19:03:14 +02003290 const char *sender;
Paul Bakker1ef83d62012-04-11 12:09:53 +00003291 unsigned char padbuf[32];
Andrzej Kurekeb342242019-01-29 09:14:33 -05003292#if defined(MBEDTLS_USE_PSA_CRYPTO)
3293 size_t hash_size;
Jaeden Amero34973232019-02-20 10:32:28 +00003294 psa_hash_operation_t sha256_psa = PSA_HASH_OPERATION_INIT;
Andrzej Kurekeb342242019-01-29 09:14:33 -05003295 psa_status_t status;
3296#else
3297 mbedtls_sha256_context sha256;
3298#endif
Paul Bakker1ef83d62012-04-11 12:09:53 +00003299
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003300 mbedtls_ssl_session *session = ssl->session_negotiate;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003301 if (!session) {
Paul Bakker48916f92012-09-16 19:57:18 +00003302 session = ssl->session;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003303 }
Paul Bakker48916f92012-09-16 19:57:18 +00003304
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003305 sender = (from == MBEDTLS_SSL_IS_CLIENT)
Andrzej Kurekeb342242019-01-29 09:14:33 -05003306 ? "client finished"
3307 : "server finished";
3308
3309#if defined(MBEDTLS_USE_PSA_CRYPTO)
3310 sha256_psa = psa_hash_operation_init();
3311
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003312 MBEDTLS_SSL_DEBUG_MSG(2, ("=> calc PSA finished tls sha256"));
Andrzej Kurekeb342242019-01-29 09:14:33 -05003313
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003314 status = psa_hash_clone(&ssl->handshake->fin_sha256_psa, &sha256_psa);
3315 if (status != PSA_SUCCESS) {
3316 MBEDTLS_SSL_DEBUG_MSG(2, ("PSA hash clone failed"));
Andrzej Kurekeb342242019-01-29 09:14:33 -05003317 return;
3318 }
3319
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003320 status = psa_hash_finish(&sha256_psa, padbuf, sizeof(padbuf), &hash_size);
3321 if (status != PSA_SUCCESS) {
3322 MBEDTLS_SSL_DEBUG_MSG(2, ("PSA hash finish failed"));
Andrzej Kurekeb342242019-01-29 09:14:33 -05003323 return;
3324 }
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003325 MBEDTLS_SSL_DEBUG_BUF(3, "PSA calculated padbuf", padbuf, 32);
Andrzej Kurekeb342242019-01-29 09:14:33 -05003326#else
3327
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003328 mbedtls_sha256_init(&sha256);
Manuel Pégourié-Gonnard001f2b62015-07-06 16:21:13 +02003329
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003330 MBEDTLS_SSL_DEBUG_MSG(2, ("=> calc finished tls sha256"));
Paul Bakker1ef83d62012-04-11 12:09:53 +00003331
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003332 mbedtls_sha256_clone(&sha256, &ssl->handshake->fin_sha256);
Paul Bakker1ef83d62012-04-11 12:09:53 +00003333
3334 /*
3335 * TLSv1.2:
3336 * hash = PRF( master, finished_label,
3337 * Hash( handshake ) )[0.11]
3338 */
3339
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003340#if !defined(MBEDTLS_SHA256_ALT)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003341 MBEDTLS_SSL_DEBUG_BUF(4, "finished sha2 state", (unsigned char *)
3342 sha256.state, sizeof(sha256.state));
Paul Bakker90995b52013-06-24 19:20:35 +02003343#endif
Paul Bakker1ef83d62012-04-11 12:09:53 +00003344
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003345 mbedtls_sha256_finish_ret(&sha256, padbuf);
3346 mbedtls_sha256_free(&sha256);
Andrzej Kurekeb342242019-01-29 09:14:33 -05003347#endif /* MBEDTLS_USE_PSA_CRYPTO */
Paul Bakker1ef83d62012-04-11 12:09:53 +00003348
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003349 ssl->handshake->tls_prf(session->master, 48, sender,
3350 padbuf, 32, buf, len);
Paul Bakker1ef83d62012-04-11 12:09:53 +00003351
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003352 MBEDTLS_SSL_DEBUG_BUF(3, "calc finished result", buf, len);
Paul Bakker1ef83d62012-04-11 12:09:53 +00003353
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003354 mbedtls_platform_zeroize(padbuf, sizeof(padbuf));
Paul Bakker1ef83d62012-04-11 12:09:53 +00003355
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003356 MBEDTLS_SSL_DEBUG_MSG(2, ("<= calc finished"));
Paul Bakker1ef83d62012-04-11 12:09:53 +00003357}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003358#endif /* MBEDTLS_SHA256_C */
Paul Bakker1ef83d62012-04-11 12:09:53 +00003359
Gilles Peskined2d59372021-05-12 22:43:27 +02003360#if defined(MBEDTLS_SHA512_C) && !defined(MBEDTLS_SHA512_NO_SHA384)
Rodrigo Dias Corread596ca82020-11-25 00:42:28 -03003361
Paul Bakkerca4ab492012-04-18 14:23:57 +00003362static void ssl_calc_finished_tls_sha384(
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003363 mbedtls_ssl_context *ssl, unsigned char *buf, int from)
Paul Bakkerca4ab492012-04-18 14:23:57 +00003364{
3365 int len = 12;
Paul Bakker3c2122f2013-06-24 19:03:14 +02003366 const char *sender;
Rodrigo Dias Corread596ca82020-11-25 00:42:28 -03003367 unsigned char padbuf[48];
Andrzej Kurekeb342242019-01-29 09:14:33 -05003368#if defined(MBEDTLS_USE_PSA_CRYPTO)
3369 size_t hash_size;
Jaeden Amero34973232019-02-20 10:32:28 +00003370 psa_hash_operation_t sha384_psa = PSA_HASH_OPERATION_INIT;
Andrzej Kurekeb342242019-01-29 09:14:33 -05003371 psa_status_t status;
3372#else
3373 mbedtls_sha512_context sha512;
3374#endif
Paul Bakkerca4ab492012-04-18 14:23:57 +00003375
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003376 mbedtls_ssl_session *session = ssl->session_negotiate;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003377 if (!session) {
Paul Bakker48916f92012-09-16 19:57:18 +00003378 session = ssl->session;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003379 }
Paul Bakker48916f92012-09-16 19:57:18 +00003380
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003381 sender = (from == MBEDTLS_SSL_IS_CLIENT)
Andrzej Kurekeb342242019-01-29 09:14:33 -05003382 ? "client finished"
3383 : "server finished";
3384
3385#if defined(MBEDTLS_USE_PSA_CRYPTO)
Andrzej Kurek972fba52019-01-30 03:29:12 -05003386 sha384_psa = psa_hash_operation_init();
Andrzej Kurekeb342242019-01-29 09:14:33 -05003387
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003388 MBEDTLS_SSL_DEBUG_MSG(2, ("=> calc PSA finished tls sha384"));
Andrzej Kurekeb342242019-01-29 09:14:33 -05003389
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003390 status = psa_hash_clone(&ssl->handshake->fin_sha384_psa, &sha384_psa);
3391 if (status != PSA_SUCCESS) {
3392 MBEDTLS_SSL_DEBUG_MSG(2, ("PSA hash clone failed"));
Andrzej Kurekeb342242019-01-29 09:14:33 -05003393 return;
3394 }
3395
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003396 status = psa_hash_finish(&sha384_psa, padbuf, sizeof(padbuf), &hash_size);
3397 if (status != PSA_SUCCESS) {
3398 MBEDTLS_SSL_DEBUG_MSG(2, ("PSA hash finish failed"));
Andrzej Kurekeb342242019-01-29 09:14:33 -05003399 return;
3400 }
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003401 MBEDTLS_SSL_DEBUG_BUF(3, "PSA calculated padbuf", padbuf, 48);
Andrzej Kurekeb342242019-01-29 09:14:33 -05003402#else
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003403 mbedtls_sha512_init(&sha512);
Manuel Pégourié-Gonnard001f2b62015-07-06 16:21:13 +02003404
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003405 MBEDTLS_SSL_DEBUG_MSG(2, ("=> calc finished tls sha384"));
Paul Bakkerca4ab492012-04-18 14:23:57 +00003406
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003407 mbedtls_sha512_clone(&sha512, &ssl->handshake->fin_sha512);
Paul Bakkerca4ab492012-04-18 14:23:57 +00003408
3409 /*
3410 * TLSv1.2:
3411 * hash = PRF( master, finished_label,
3412 * Hash( handshake ) )[0.11]
3413 */
3414
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003415#if !defined(MBEDTLS_SHA512_ALT)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003416 MBEDTLS_SSL_DEBUG_BUF(4, "finished sha512 state", (unsigned char *)
3417 sha512.state, sizeof(sha512.state));
Paul Bakker90995b52013-06-24 19:20:35 +02003418#endif
Gilles Peskinebb66dac2021-05-13 00:00:45 +02003419 /* mbedtls_sha512_finish_ret's output parameter is declared as a
Tom Cosgrove49f99bc2022-12-04 16:44:21 +00003420 * 64-byte buffer, but since we're using SHA-384, we know that the
Gilles Peskinebb66dac2021-05-13 00:00:45 +02003421 * output fits in 48 bytes. This is correct C, but GCC 11.1 warns
3422 * about it.
Rodrigo Dias Corread596ca82020-11-25 00:42:28 -03003423 */
Gilles Peskinebb66dac2021-05-13 00:00:45 +02003424#if defined(__GNUC__) && __GNUC__ >= 11
3425#pragma GCC diagnostic push
3426#pragma GCC diagnostic ignored "-Wstringop-overflow"
3427#endif
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003428 mbedtls_sha512_finish_ret(&sha512, padbuf);
Gilles Peskinebb66dac2021-05-13 00:00:45 +02003429#if defined(__GNUC__) && __GNUC__ >= 11
3430#pragma GCC diagnostic pop
3431#endif
Paul Bakkerca4ab492012-04-18 14:23:57 +00003432
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003433 mbedtls_sha512_free(&sha512);
Andrzej Kurekeb342242019-01-29 09:14:33 -05003434#endif
Paul Bakkerca4ab492012-04-18 14:23:57 +00003435
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003436 ssl->handshake->tls_prf(session->master, 48, sender,
3437 padbuf, 48, buf, len);
Paul Bakkerca4ab492012-04-18 14:23:57 +00003438
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003439 MBEDTLS_SSL_DEBUG_BUF(3, "calc finished result", buf, len);
Paul Bakkerca4ab492012-04-18 14:23:57 +00003440
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003441 mbedtls_platform_zeroize(padbuf, sizeof(padbuf));
Paul Bakkerca4ab492012-04-18 14:23:57 +00003442
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003443 MBEDTLS_SSL_DEBUG_MSG(2, ("<= calc finished"));
Paul Bakkerca4ab492012-04-18 14:23:57 +00003444}
Gilles Peskined2d59372021-05-12 22:43:27 +02003445#endif /* MBEDTLS_SHA512_C && !MBEDTLS_SHA512_NO_SHA384 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003446#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakkerca4ab492012-04-18 14:23:57 +00003447
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003448void mbedtls_ssl_handshake_wrapup_free_hs_transform(mbedtls_ssl_context *ssl)
Paul Bakker48916f92012-09-16 19:57:18 +00003449{
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003450 MBEDTLS_SSL_DEBUG_MSG(3, ("=> handshake wrapup: final free"));
Paul Bakker48916f92012-09-16 19:57:18 +00003451
3452 /*
3453 * Free our handshake params
3454 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003455 mbedtls_ssl_handshake_free(ssl);
3456 mbedtls_free(ssl->handshake);
Paul Bakker48916f92012-09-16 19:57:18 +00003457 ssl->handshake = NULL;
3458
3459 /*
Shaun Case0e7791f2021-12-20 21:14:10 -08003460 * Free the previous transform and switch in the current one
Paul Bakker48916f92012-09-16 19:57:18 +00003461 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003462 if (ssl->transform) {
3463 mbedtls_ssl_transform_free(ssl->transform);
3464 mbedtls_free(ssl->transform);
Paul Bakker48916f92012-09-16 19:57:18 +00003465 }
3466 ssl->transform = ssl->transform_negotiate;
3467 ssl->transform_negotiate = NULL;
3468
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003469 MBEDTLS_SSL_DEBUG_MSG(3, ("<= handshake wrapup: final free"));
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02003470}
3471
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003472void mbedtls_ssl_handshake_wrapup(mbedtls_ssl_context *ssl)
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02003473{
3474 int resume = ssl->handshake->resume;
3475
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003476 MBEDTLS_SSL_DEBUG_MSG(3, ("=> handshake wrapup"));
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02003477
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003478#if defined(MBEDTLS_SSL_RENEGOTIATION)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003479 if (ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS) {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003480 ssl->renego_status = MBEDTLS_SSL_RENEGOTIATION_DONE;
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02003481 ssl->renego_records_seen = 0;
3482 }
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01003483#endif
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02003484
3485 /*
3486 * Free the previous session and switch in the current one
3487 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003488 if (ssl->session) {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003489#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
Manuel Pégourié-Gonnard1a034732014-11-04 17:36:18 +01003490 /* RFC 7366 3.1: keep the EtM state */
3491 ssl->session_negotiate->encrypt_then_mac =
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003492 ssl->session->encrypt_then_mac;
Manuel Pégourié-Gonnard1a034732014-11-04 17:36:18 +01003493#endif
3494
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003495 mbedtls_ssl_session_free(ssl->session);
3496 mbedtls_free(ssl->session);
Paul Bakker0a597072012-09-25 21:55:46 +00003497 }
3498 ssl->session = ssl->session_negotiate;
Paul Bakker48916f92012-09-16 19:57:18 +00003499 ssl->session_negotiate = NULL;
3500
Paul Bakker0a597072012-09-25 21:55:46 +00003501 /*
3502 * Add cache entry
3503 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003504 if (ssl->conf->f_set_cache != NULL &&
Manuel Pégourié-Gonnard12ad7982015-06-18 15:50:37 +02003505 ssl->session->id_len != 0 &&
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003506 resume == 0) {
3507 if (ssl->conf->f_set_cache(ssl->conf->p_cache, ssl->session) != 0) {
3508 MBEDTLS_SSL_DEBUG_MSG(1, ("cache did not store session"));
3509 }
Manuel Pégourié-Gonnardc086cce2013-08-02 14:13:02 +02003510 }
Paul Bakker0a597072012-09-25 21:55:46 +00003511
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003512#if defined(MBEDTLS_SSL_PROTO_DTLS)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003513 if (ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
3514 ssl->handshake->flight != NULL) {
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02003515 /* Cancel handshake timer */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003516 mbedtls_ssl_set_timer(ssl, 0);
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02003517
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02003518 /* Keep last flight around in case we need to resend it:
3519 * we need the handshake and transform structures for that */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003520 MBEDTLS_SSL_DEBUG_MSG(3, ("skip freeing handshake and transform"));
3521 } else
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02003522#endif
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003523 mbedtls_ssl_handshake_wrapup_free_hs_transform(ssl);
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02003524
Paul Bakker48916f92012-09-16 19:57:18 +00003525 ssl->state++;
3526
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003527 MBEDTLS_SSL_DEBUG_MSG(3, ("<= handshake wrapup"));
Paul Bakker48916f92012-09-16 19:57:18 +00003528}
3529
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003530int mbedtls_ssl_write_finished(mbedtls_ssl_context *ssl)
Paul Bakker1ef83d62012-04-11 12:09:53 +00003531{
Manuel Pégourié-Gonnard879a4f92014-07-11 22:31:12 +02003532 int ret, hash_len;
Paul Bakker1ef83d62012-04-11 12:09:53 +00003533
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003534 MBEDTLS_SSL_DEBUG_MSG(2, ("=> write finished"));
Paul Bakker1ef83d62012-04-11 12:09:53 +00003535
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003536 mbedtls_ssl_update_out_pointers(ssl, ssl->transform_negotiate);
Paul Bakker92be97b2013-01-02 17:30:03 +01003537
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003538 ssl->handshake->calc_finished(ssl, ssl->out_msg + 4, ssl->conf->endpoint);
Paul Bakker1ef83d62012-04-11 12:09:53 +00003539
Manuel Pégourié-Gonnard214a8482016-02-22 11:27:26 +01003540 /*
3541 * RFC 5246 7.4.9 (Page 63) says 12 is the default length and ciphersuites
3542 * may define some other value. Currently (early 2016), no defined
3543 * ciphersuite does this (and this is unlikely to change as activity has
3544 * moved to TLS 1.3 now) so we can keep the hardcoded 12 here.
3545 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003546 hash_len = (ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0) ? 36 : 12;
Paul Bakker5121ce52009-01-03 21:22:43 +00003547
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003548#if defined(MBEDTLS_SSL_RENEGOTIATION)
Paul Bakker48916f92012-09-16 19:57:18 +00003549 ssl->verify_data_len = hash_len;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003550 memcpy(ssl->own_verify_data, ssl->out_msg + 4, hash_len);
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01003551#endif
Paul Bakker48916f92012-09-16 19:57:18 +00003552
Paul Bakker5121ce52009-01-03 21:22:43 +00003553 ssl->out_msglen = 4 + hash_len;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003554 ssl->out_msgtype = MBEDTLS_SSL_MSG_HANDSHAKE;
3555 ssl->out_msg[0] = MBEDTLS_SSL_HS_FINISHED;
Paul Bakker5121ce52009-01-03 21:22:43 +00003556
3557 /*
3558 * In case of session resuming, invert the client and server
3559 * ChangeCipherSpec messages order.
3560 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003561 if (ssl->handshake->resume != 0) {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003562#if defined(MBEDTLS_SSL_CLI_C)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003563 if (ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT) {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003564 ssl->state = MBEDTLS_SSL_HANDSHAKE_WRAPUP;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003565 }
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01003566#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003567#if defined(MBEDTLS_SSL_SRV_C)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003568 if (ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER) {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003569 ssl->state = MBEDTLS_SSL_CLIENT_CHANGE_CIPHER_SPEC;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003570 }
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01003571#endif
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003572 } else {
Paul Bakker5121ce52009-01-03 21:22:43 +00003573 ssl->state++;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003574 }
Paul Bakker5121ce52009-01-03 21:22:43 +00003575
Paul Bakker48916f92012-09-16 19:57:18 +00003576 /*
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02003577 * Switch to our negotiated transform and session parameters for outbound
3578 * data.
Paul Bakker48916f92012-09-16 19:57:18 +00003579 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003580 MBEDTLS_SSL_DEBUG_MSG(3, ("switching to new transform spec for outbound data"));
Manuel Pégourié-Gonnard5afb1672014-02-16 18:33:22 +01003581
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003582#if defined(MBEDTLS_SSL_PROTO_DTLS)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003583 if (ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM) {
Manuel Pégourié-Gonnard879a4f92014-07-11 22:31:12 +02003584 unsigned char i;
3585
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003586 /* Remember current epoch settings for resending */
3587 ssl->handshake->alt_transform_out = ssl->transform_out;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003588 memcpy(ssl->handshake->alt_out_ctr, ssl->cur_out_ctr, 8);
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003589
Manuel Pégourié-Gonnard879a4f92014-07-11 22:31:12 +02003590 /* Set sequence_number to zero */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003591 memset(ssl->cur_out_ctr + 2, 0, 6);
Manuel Pégourié-Gonnard879a4f92014-07-11 22:31:12 +02003592
3593 /* Increment epoch */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003594 for (i = 2; i > 0; i--) {
3595 if (++ssl->cur_out_ctr[i - 1] != 0) {
Manuel Pégourié-Gonnard879a4f92014-07-11 22:31:12 +02003596 break;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003597 }
3598 }
Manuel Pégourié-Gonnard879a4f92014-07-11 22:31:12 +02003599
3600 /* The loop goes to its end iff the counter is wrapping */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003601 if (i == 0) {
3602 MBEDTLS_SSL_DEBUG_MSG(1, ("DTLS epoch would wrap"));
3603 return MBEDTLS_ERR_SSL_COUNTER_WRAPPING;
Manuel Pégourié-Gonnard879a4f92014-07-11 22:31:12 +02003604 }
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003605 } else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003606#endif /* MBEDTLS_SSL_PROTO_DTLS */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003607 memset(ssl->cur_out_ctr, 0, 8);
Paul Bakker5121ce52009-01-03 21:22:43 +00003608
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003609 ssl->transform_out = ssl->transform_negotiate;
3610 ssl->session_out = ssl->session_negotiate;
3611
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003612#if defined(MBEDTLS_SSL_HW_RECORD_ACCEL)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003613 if (mbedtls_ssl_hw_record_activate != NULL) {
3614 if ((ret = mbedtls_ssl_hw_record_activate(ssl, MBEDTLS_SSL_CHANNEL_OUTBOUND)) != 0) {
3615 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ssl_hw_record_activate", ret);
3616 return MBEDTLS_ERR_SSL_HW_ACCEL_FAILED;
Paul Bakker07eb38b2012-12-19 14:42:06 +01003617 }
3618 }
3619#endif
3620
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003621#if defined(MBEDTLS_SSL_PROTO_DTLS)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003622 if (ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM) {
3623 mbedtls_ssl_send_flight_completed(ssl);
3624 }
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02003625#endif
3626
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003627 if ((ret = mbedtls_ssl_write_handshake_msg(ssl)) != 0) {
3628 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ssl_write_handshake_msg", ret);
3629 return ret;
Paul Bakker5121ce52009-01-03 21:22:43 +00003630 }
3631
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02003632#if defined(MBEDTLS_SSL_PROTO_DTLS)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003633 if (ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
3634 (ret = mbedtls_ssl_flight_transmit(ssl)) != 0) {
3635 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ssl_flight_transmit", ret);
3636 return ret;
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02003637 }
3638#endif
3639
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003640 MBEDTLS_SSL_DEBUG_MSG(2, ("<= write finished"));
Paul Bakker5121ce52009-01-03 21:22:43 +00003641
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003642 return 0;
Paul Bakker5121ce52009-01-03 21:22:43 +00003643}
3644
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003645#if defined(MBEDTLS_SSL_PROTO_SSL3)
Manuel Pégourié-Gonnardca6440b2014-09-10 12:39:54 +00003646#define SSL_MAX_HASH_LEN 36
3647#else
3648#define SSL_MAX_HASH_LEN 12
3649#endif
3650
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003651int mbedtls_ssl_parse_finished(mbedtls_ssl_context *ssl)
Paul Bakker5121ce52009-01-03 21:22:43 +00003652{
Janos Follath865b3eb2019-12-16 11:46:15 +00003653 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnard879a4f92014-07-11 22:31:12 +02003654 unsigned int hash_len;
Manuel Pégourié-Gonnardca6440b2014-09-10 12:39:54 +00003655 unsigned char buf[SSL_MAX_HASH_LEN];
Paul Bakker5121ce52009-01-03 21:22:43 +00003656
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003657 MBEDTLS_SSL_DEBUG_MSG(2, ("=> parse finished"));
Paul Bakker5121ce52009-01-03 21:22:43 +00003658
Gilles Peskine622d8042021-12-13 14:38:40 +01003659 /* There is currently no ciphersuite using another length with TLS 1.2 */
3660#if defined(MBEDTLS_SSL_PROTO_SSL3)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003661 if (ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0) {
Gilles Peskine622d8042021-12-13 14:38:40 +01003662 hash_len = 36;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003663 } else
Gilles Peskine622d8042021-12-13 14:38:40 +01003664#endif
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003665 hash_len = 12;
Gilles Peskine622d8042021-12-13 14:38:40 +01003666
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003667 ssl->handshake->calc_finished(ssl, buf, ssl->conf->endpoint ^ 1);
Paul Bakker5121ce52009-01-03 21:22:43 +00003668
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003669 if ((ret = mbedtls_ssl_read_record(ssl, 1)) != 0) {
3670 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ssl_read_record", ret);
Gilles Peskinef91b2e52021-12-13 12:36:15 +01003671 goto exit;
Paul Bakker5121ce52009-01-03 21:22:43 +00003672 }
3673
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003674 if (ssl->in_msgtype != MBEDTLS_SSL_MSG_HANDSHAKE) {
3675 MBEDTLS_SSL_DEBUG_MSG(1, ("bad finished message"));
3676 mbedtls_ssl_send_alert_message(ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
3677 MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE);
Gilles Peskinef91b2e52021-12-13 12:36:15 +01003678 ret = MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE;
3679 goto exit;
Paul Bakker5121ce52009-01-03 21:22:43 +00003680 }
3681
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003682 if (ssl->in_msg[0] != MBEDTLS_SSL_HS_FINISHED ||
3683 ssl->in_hslen != mbedtls_ssl_hs_hdr_len(ssl) + hash_len) {
3684 MBEDTLS_SSL_DEBUG_MSG(1, ("bad finished message"));
3685 mbedtls_ssl_send_alert_message(ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
3686 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR);
Gilles Peskinef91b2e52021-12-13 12:36:15 +01003687 ret = MBEDTLS_ERR_SSL_BAD_HS_FINISHED;
3688 goto exit;
Paul Bakker5121ce52009-01-03 21:22:43 +00003689 }
3690
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003691 if (mbedtls_ct_memcmp(ssl->in_msg + mbedtls_ssl_hs_hdr_len(ssl),
3692 buf, hash_len) != 0) {
3693 MBEDTLS_SSL_DEBUG_MSG(1, ("bad finished message"));
3694 mbedtls_ssl_send_alert_message(ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
3695 MBEDTLS_SSL_ALERT_MSG_DECRYPT_ERROR);
Gilles Peskinef91b2e52021-12-13 12:36:15 +01003696 ret = MBEDTLS_ERR_SSL_BAD_HS_FINISHED;
3697 goto exit;
Paul Bakker5121ce52009-01-03 21:22:43 +00003698 }
3699
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003700#if defined(MBEDTLS_SSL_RENEGOTIATION)
Paul Bakker48916f92012-09-16 19:57:18 +00003701 ssl->verify_data_len = hash_len;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003702 memcpy(ssl->peer_verify_data, buf, hash_len);
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01003703#endif
Paul Bakker48916f92012-09-16 19:57:18 +00003704
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003705 if (ssl->handshake->resume != 0) {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003706#if defined(MBEDTLS_SSL_CLI_C)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003707 if (ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT) {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003708 ssl->state = MBEDTLS_SSL_CLIENT_CHANGE_CIPHER_SPEC;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003709 }
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01003710#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003711#if defined(MBEDTLS_SSL_SRV_C)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003712 if (ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER) {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003713 ssl->state = MBEDTLS_SSL_HANDSHAKE_WRAPUP;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003714 }
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01003715#endif
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003716 } else {
Paul Bakker5121ce52009-01-03 21:22:43 +00003717 ssl->state++;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003718 }
Paul Bakker5121ce52009-01-03 21:22:43 +00003719
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003720#if defined(MBEDTLS_SSL_PROTO_DTLS)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003721 if (ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM) {
3722 mbedtls_ssl_recv_flight_completed(ssl);
3723 }
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003724#endif
3725
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003726 MBEDTLS_SSL_DEBUG_MSG(2, ("<= parse finished"));
Paul Bakker5121ce52009-01-03 21:22:43 +00003727
Gilles Peskinef91b2e52021-12-13 12:36:15 +01003728exit:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003729 mbedtls_platform_zeroize(buf, hash_len);
3730 return ret;
Paul Bakker5121ce52009-01-03 21:22:43 +00003731}
3732
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003733static void ssl_handshake_params_init(mbedtls_ssl_handshake_params *handshake)
Paul Bakkeraccaffe2014-06-26 13:37:14 +02003734{
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003735 memset(handshake, 0, sizeof(mbedtls_ssl_handshake_params));
Paul Bakkeraccaffe2014-06-26 13:37:14 +02003736
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003737#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1) || \
3738 defined(MBEDTLS_SSL_PROTO_TLS1_1)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003739 mbedtls_md5_init(&handshake->fin_md5);
3740 mbedtls_sha1_init(&handshake->fin_sha1);
3741 mbedtls_md5_starts_ret(&handshake->fin_md5);
3742 mbedtls_sha1_starts_ret(&handshake->fin_sha1);
Paul Bakkeraccaffe2014-06-26 13:37:14 +02003743#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003744#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
3745#if defined(MBEDTLS_SHA256_C)
Andrzej Kurekeb342242019-01-29 09:14:33 -05003746#if defined(MBEDTLS_USE_PSA_CRYPTO)
3747 handshake->fin_sha256_psa = psa_hash_operation_init();
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003748 psa_hash_setup(&handshake->fin_sha256_psa, PSA_ALG_SHA_256);
Andrzej Kurekeb342242019-01-29 09:14:33 -05003749#else
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003750 mbedtls_sha256_init(&handshake->fin_sha256);
3751 mbedtls_sha256_starts_ret(&handshake->fin_sha256, 0);
Paul Bakkeraccaffe2014-06-26 13:37:14 +02003752#endif
Andrzej Kurekeb342242019-01-29 09:14:33 -05003753#endif
Gilles Peskined2d59372021-05-12 22:43:27 +02003754#if defined(MBEDTLS_SHA512_C) && !defined(MBEDTLS_SHA512_NO_SHA384)
Andrzej Kurekeb342242019-01-29 09:14:33 -05003755#if defined(MBEDTLS_USE_PSA_CRYPTO)
Andrzej Kurek972fba52019-01-30 03:29:12 -05003756 handshake->fin_sha384_psa = psa_hash_operation_init();
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003757 psa_hash_setup(&handshake->fin_sha384_psa, PSA_ALG_SHA_384);
Andrzej Kurekeb342242019-01-29 09:14:33 -05003758#else
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003759 mbedtls_sha512_init(&handshake->fin_sha512);
3760 mbedtls_sha512_starts_ret(&handshake->fin_sha512, 1);
Paul Bakkeraccaffe2014-06-26 13:37:14 +02003761#endif
Andrzej Kurekeb342242019-01-29 09:14:33 -05003762#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003763#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakkeraccaffe2014-06-26 13:37:14 +02003764
3765 handshake->update_checksum = ssl_update_checksum_start;
Hanno Becker7e5437a2017-04-28 17:15:26 +01003766
3767#if defined(MBEDTLS_SSL_PROTO_TLS1_2) && \
Gilles Peskineeccd8882020-03-10 12:19:08 +01003768 defined(MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003769 mbedtls_ssl_sig_hash_set_init(&handshake->hash_algs);
Hanno Becker7e5437a2017-04-28 17:15:26 +01003770#endif
Paul Bakkeraccaffe2014-06-26 13:37:14 +02003771
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003772#if defined(MBEDTLS_DHM_C)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003773 mbedtls_dhm_init(&handshake->dhm_ctx);
Paul Bakkeraccaffe2014-06-26 13:37:14 +02003774#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003775#if defined(MBEDTLS_ECDH_C)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003776 mbedtls_ecdh_init(&handshake->ecdh_ctx);
Paul Bakkeraccaffe2014-06-26 13:37:14 +02003777#endif
Manuel Pégourié-Gonnardeef142d2015-09-16 10:05:04 +02003778#if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003779 mbedtls_ecjpake_init(&handshake->ecjpake_ctx);
Manuel Pégourié-Gonnard77c06462015-09-17 13:59:49 +02003780#if defined(MBEDTLS_SSL_CLI_C)
3781 handshake->ecjpake_cache = NULL;
3782 handshake->ecjpake_cache_len = 0;
3783#endif
Manuel Pégourié-Gonnard76cfd3f2015-09-15 12:10:54 +02003784#endif
Manuel Pégourié-Gonnardcdc26ae2015-06-19 12:16:31 +02003785
Gilles Peskineeccd8882020-03-10 12:19:08 +01003786#if defined(MBEDTLS_SSL_ECP_RESTARTABLE_ENABLED)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003787 mbedtls_x509_crt_restart_init(&handshake->ecrs_ctx);
Manuel Pégourié-Gonnard862cde52017-05-17 11:56:15 +02003788#endif
3789
Manuel Pégourié-Gonnardcdc26ae2015-06-19 12:16:31 +02003790#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
3791 handshake->sni_authmode = MBEDTLS_SSL_VERIFY_UNSET;
3792#endif
Hanno Becker75173122019-02-06 16:18:31 +00003793
3794#if defined(MBEDTLS_X509_CRT_PARSE_C) && \
3795 !defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003796 mbedtls_pk_init(&handshake->peer_pubkey);
Hanno Becker75173122019-02-06 16:18:31 +00003797#endif
Paul Bakkeraccaffe2014-06-26 13:37:14 +02003798}
3799
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003800void mbedtls_ssl_transform_init(mbedtls_ssl_transform *transform)
Paul Bakkeraccaffe2014-06-26 13:37:14 +02003801{
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003802 memset(transform, 0, sizeof(mbedtls_ssl_transform));
Paul Bakker84bbeb52014-07-01 14:53:22 +02003803
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003804 mbedtls_cipher_init(&transform->cipher_ctx_enc);
3805 mbedtls_cipher_init(&transform->cipher_ctx_dec);
Paul Bakker84bbeb52014-07-01 14:53:22 +02003806
Hanno Beckerd56ed242018-01-03 15:32:51 +00003807#if defined(MBEDTLS_SSL_SOME_MODES_USE_MAC)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003808 mbedtls_md_init(&transform->md_ctx_enc);
3809 mbedtls_md_init(&transform->md_ctx_dec);
Hanno Beckerd56ed242018-01-03 15:32:51 +00003810#endif
Paul Bakkeraccaffe2014-06-26 13:37:14 +02003811}
3812
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003813void mbedtls_ssl_session_init(mbedtls_ssl_session *session)
Paul Bakkeraccaffe2014-06-26 13:37:14 +02003814{
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003815 memset(session, 0, sizeof(mbedtls_ssl_session));
Paul Bakkeraccaffe2014-06-26 13:37:14 +02003816}
3817
Manuel Pégourié-Gonnardd904d662022-06-17 10:24:00 +02003818MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003819static int ssl_handshake_init(mbedtls_ssl_context *ssl)
Paul Bakker48916f92012-09-16 19:57:18 +00003820{
Paul Bakkeraccaffe2014-06-26 13:37:14 +02003821 /* Clear old handshake information if present */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003822 if (ssl->transform_negotiate) {
3823 mbedtls_ssl_transform_free(ssl->transform_negotiate);
3824 }
3825 if (ssl->session_negotiate) {
3826 mbedtls_ssl_session_free(ssl->session_negotiate);
3827 }
3828 if (ssl->handshake) {
3829 mbedtls_ssl_handshake_free(ssl);
3830 }
Paul Bakkeraccaffe2014-06-26 13:37:14 +02003831
3832 /*
3833 * Either the pointers are now NULL or cleared properly and can be freed.
3834 * Now allocate missing structures.
3835 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003836 if (ssl->transform_negotiate == NULL) {
3837 ssl->transform_negotiate = mbedtls_calloc(1, sizeof(mbedtls_ssl_transform));
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02003838 }
Paul Bakker48916f92012-09-16 19:57:18 +00003839
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003840 if (ssl->session_negotiate == NULL) {
3841 ssl->session_negotiate = mbedtls_calloc(1, sizeof(mbedtls_ssl_session));
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02003842 }
Paul Bakker48916f92012-09-16 19:57:18 +00003843
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003844 if (ssl->handshake == NULL) {
3845 ssl->handshake = mbedtls_calloc(1, sizeof(mbedtls_ssl_handshake_params));
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02003846 }
Andrzej Kurek0afa2a12020-03-03 10:39:58 -05003847#if defined(MBEDTLS_SSL_VARIABLE_BUFFER_LENGTH)
3848 /* If the buffers are too small - reallocate */
Andrzej Kurek8ea68722020-04-03 06:40:47 -04003849
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003850 handle_buffer_resizing(ssl, 0, MBEDTLS_SSL_IN_BUFFER_LEN,
3851 MBEDTLS_SSL_OUT_BUFFER_LEN);
Andrzej Kurek0afa2a12020-03-03 10:39:58 -05003852#endif
Paul Bakker48916f92012-09-16 19:57:18 +00003853
Paul Bakkeraccaffe2014-06-26 13:37:14 +02003854 /* All pointers should exist and can be directly freed without issue */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003855 if (ssl->handshake == NULL ||
Paul Bakker48916f92012-09-16 19:57:18 +00003856 ssl->transform_negotiate == NULL ||
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003857 ssl->session_negotiate == NULL) {
3858 MBEDTLS_SSL_DEBUG_MSG(1, ("alloc() of ssl sub-contexts failed"));
Paul Bakkeraccaffe2014-06-26 13:37:14 +02003859
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003860 mbedtls_free(ssl->handshake);
3861 mbedtls_free(ssl->transform_negotiate);
3862 mbedtls_free(ssl->session_negotiate);
Paul Bakkeraccaffe2014-06-26 13:37:14 +02003863
3864 ssl->handshake = NULL;
3865 ssl->transform_negotiate = NULL;
3866 ssl->session_negotiate = NULL;
3867
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003868 return MBEDTLS_ERR_SSL_ALLOC_FAILED;
Paul Bakker48916f92012-09-16 19:57:18 +00003869 }
3870
Paul Bakkeraccaffe2014-06-26 13:37:14 +02003871 /* Initialize structures */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003872 mbedtls_ssl_session_init(ssl->session_negotiate);
3873 mbedtls_ssl_transform_init(ssl->transform_negotiate);
3874 ssl_handshake_params_init(ssl->handshake);
Paul Bakker968afaa2014-07-09 11:09:24 +02003875
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003876#if defined(MBEDTLS_SSL_PROTO_DTLS)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003877 if (ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM) {
Manuel Pégourié-Gonnard06939ce2015-05-11 11:25:46 +02003878 ssl->handshake->alt_transform_out = ssl->transform_out;
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003879
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003880 if (ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT) {
Manuel Pégourié-Gonnard06939ce2015-05-11 11:25:46 +02003881 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_PREPARING;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003882 } else {
Manuel Pégourié-Gonnard06939ce2015-05-11 11:25:46 +02003883 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_WAITING;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003884 }
Manuel Pégourié-Gonnard286a1362015-05-13 16:22:05 +02003885
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003886 mbedtls_ssl_set_timer(ssl, 0);
Manuel Pégourié-Gonnard06939ce2015-05-11 11:25:46 +02003887 }
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003888#endif
3889
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003890 return 0;
Paul Bakker48916f92012-09-16 19:57:18 +00003891}
3892
Manuel Pégourié-Gonnarde057d3b2015-05-20 10:59:43 +02003893#if defined(MBEDTLS_SSL_DTLS_HELLO_VERIFY) && defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard7d38d212014-07-23 17:52:09 +02003894/* Dummy cookie callbacks for defaults */
Manuel Pégourié-Gonnardd904d662022-06-17 10:24:00 +02003895MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003896static int ssl_cookie_write_dummy(void *ctx,
3897 unsigned char **p, unsigned char *end,
3898 const unsigned char *cli_id, size_t cli_id_len)
Manuel Pégourié-Gonnard7d38d212014-07-23 17:52:09 +02003899{
3900 ((void) ctx);
3901 ((void) p);
3902 ((void) end);
3903 ((void) cli_id);
3904 ((void) cli_id_len);
3905
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003906 return MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
Manuel Pégourié-Gonnard7d38d212014-07-23 17:52:09 +02003907}
3908
Manuel Pégourié-Gonnardd904d662022-06-17 10:24:00 +02003909MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003910static int ssl_cookie_check_dummy(void *ctx,
3911 const unsigned char *cookie, size_t cookie_len,
3912 const unsigned char *cli_id, size_t cli_id_len)
Manuel Pégourié-Gonnard7d38d212014-07-23 17:52:09 +02003913{
3914 ((void) ctx);
3915 ((void) cookie);
3916 ((void) cookie_len);
3917 ((void) cli_id);
3918 ((void) cli_id_len);
3919
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003920 return MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
Manuel Pégourié-Gonnard7d38d212014-07-23 17:52:09 +02003921}
Manuel Pégourié-Gonnarde057d3b2015-05-20 10:59:43 +02003922#endif /* MBEDTLS_SSL_DTLS_HELLO_VERIFY && MBEDTLS_SSL_SRV_C */
Manuel Pégourié-Gonnard7d38d212014-07-23 17:52:09 +02003923
Paul Bakker5121ce52009-01-03 21:22:43 +00003924/*
3925 * Initialize an SSL context
3926 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003927void mbedtls_ssl_init(mbedtls_ssl_context *ssl)
Manuel Pégourié-Gonnard41d479e2015-04-29 00:48:22 +02003928{
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003929 memset(ssl, 0, sizeof(mbedtls_ssl_context));
Manuel Pégourié-Gonnard41d479e2015-04-29 00:48:22 +02003930}
3931
3932/*
3933 * Setup an SSL context
3934 */
Hanno Becker2a43f6f2018-08-10 11:12:52 +01003935
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003936int mbedtls_ssl_setup(mbedtls_ssl_context *ssl,
3937 const mbedtls_ssl_config *conf)
Paul Bakker5121ce52009-01-03 21:22:43 +00003938{
Janos Follath865b3eb2019-12-16 11:46:15 +00003939 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Darryl Greenb33cc762019-11-28 14:29:44 +00003940 size_t in_buf_len = MBEDTLS_SSL_IN_BUFFER_LEN;
3941 size_t out_buf_len = MBEDTLS_SSL_OUT_BUFFER_LEN;
Paul Bakker5121ce52009-01-03 21:22:43 +00003942
Manuel Pégourié-Gonnarddef0bbe2015-05-04 14:56:36 +02003943 ssl->conf = conf;
Paul Bakker62f2dee2012-09-28 07:31:51 +00003944
3945 /*
Manuel Pégourié-Gonnard06193482014-02-14 08:39:32 +01003946 * Prepare base structures
Paul Bakker62f2dee2012-09-28 07:31:51 +00003947 */
k-stachowiakc9a5f022018-07-24 13:53:31 +02003948
3949 /* Set to NULL in case of an error condition */
3950 ssl->out_buf = NULL;
k-stachowiaka47911c2018-07-04 17:41:58 +02003951
Darryl Greenb33cc762019-11-28 14:29:44 +00003952#if defined(MBEDTLS_SSL_VARIABLE_BUFFER_LENGTH)
3953 ssl->in_buf_len = in_buf_len;
3954#endif
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003955 ssl->in_buf = mbedtls_calloc(1, in_buf_len);
3956 if (ssl->in_buf == NULL) {
3957 MBEDTLS_SSL_DEBUG_MSG(1, ("alloc(%" MBEDTLS_PRINTF_SIZET " bytes) failed", in_buf_len));
k-stachowiak9f7798e2018-07-31 16:52:32 +02003958 ret = MBEDTLS_ERR_SSL_ALLOC_FAILED;
k-stachowiaka47911c2018-07-04 17:41:58 +02003959 goto error;
Angus Grattond8213d02016-05-25 20:56:48 +10003960 }
3961
Darryl Greenb33cc762019-11-28 14:29:44 +00003962#if defined(MBEDTLS_SSL_VARIABLE_BUFFER_LENGTH)
3963 ssl->out_buf_len = out_buf_len;
3964#endif
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003965 ssl->out_buf = mbedtls_calloc(1, out_buf_len);
3966 if (ssl->out_buf == NULL) {
3967 MBEDTLS_SSL_DEBUG_MSG(1, ("alloc(%" MBEDTLS_PRINTF_SIZET " bytes) failed", out_buf_len));
k-stachowiak9f7798e2018-07-31 16:52:32 +02003968 ret = MBEDTLS_ERR_SSL_ALLOC_FAILED;
k-stachowiaka47911c2018-07-04 17:41:58 +02003969 goto error;
Paul Bakker5121ce52009-01-03 21:22:43 +00003970 }
3971
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003972 mbedtls_ssl_reset_in_out_pointers(ssl);
Manuel Pégourié-Gonnard419d5ae2015-05-04 19:32:36 +02003973
Johan Pascalb62bb512015-12-03 21:56:45 +01003974#if defined(MBEDTLS_SSL_DTLS_SRTP)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003975 memset(&ssl->dtls_srtp_info, 0, sizeof(ssl->dtls_srtp_info));
Johan Pascalb62bb512015-12-03 21:56:45 +01003976#endif
3977
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003978 if ((ret = ssl_handshake_init(ssl)) != 0) {
k-stachowiaka47911c2018-07-04 17:41:58 +02003979 goto error;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003980 }
Paul Bakker5121ce52009-01-03 21:22:43 +00003981
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003982 return 0;
k-stachowiaka47911c2018-07-04 17:41:58 +02003983
3984error:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003985 mbedtls_free(ssl->in_buf);
3986 mbedtls_free(ssl->out_buf);
k-stachowiaka47911c2018-07-04 17:41:58 +02003987
3988 ssl->conf = NULL;
3989
Darryl Greenb33cc762019-11-28 14:29:44 +00003990#if defined(MBEDTLS_SSL_VARIABLE_BUFFER_LENGTH)
3991 ssl->in_buf_len = 0;
3992 ssl->out_buf_len = 0;
3993#endif
k-stachowiaka47911c2018-07-04 17:41:58 +02003994 ssl->in_buf = NULL;
3995 ssl->out_buf = NULL;
3996
3997 ssl->in_hdr = NULL;
3998 ssl->in_ctr = NULL;
3999 ssl->in_len = NULL;
4000 ssl->in_iv = NULL;
4001 ssl->in_msg = NULL;
4002
4003 ssl->out_hdr = NULL;
4004 ssl->out_ctr = NULL;
4005 ssl->out_len = NULL;
4006 ssl->out_iv = NULL;
4007 ssl->out_msg = NULL;
4008
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004009 return ret;
Paul Bakker5121ce52009-01-03 21:22:43 +00004010}
4011
4012/*
Paul Bakker7eb013f2011-10-06 12:37:39 +00004013 * Reset an initialized and used SSL context for re-use while retaining
4014 * all application-set variables, function pointers and data.
Manuel Pégourié-Gonnard3f09b6d2015-09-08 11:58:14 +02004015 *
4016 * If partial is non-zero, keep data in the input buffer and client ID.
4017 * (Use when a DTLS client reconnects from the same port.)
Paul Bakker7eb013f2011-10-06 12:37:39 +00004018 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004019int mbedtls_ssl_session_reset_int(mbedtls_ssl_context *ssl, int partial)
Paul Bakker7eb013f2011-10-06 12:37:39 +00004020{
Janos Follath865b3eb2019-12-16 11:46:15 +00004021 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Darryl Greenb33cc762019-11-28 14:29:44 +00004022#if defined(MBEDTLS_SSL_VARIABLE_BUFFER_LENGTH)
4023 size_t in_buf_len = ssl->in_buf_len;
4024 size_t out_buf_len = ssl->out_buf_len;
4025#else
4026 size_t in_buf_len = MBEDTLS_SSL_IN_BUFFER_LEN;
4027 size_t out_buf_len = MBEDTLS_SSL_OUT_BUFFER_LEN;
4028#endif
Paul Bakker48916f92012-09-16 19:57:18 +00004029
Hanno Becker7e772132018-08-10 12:38:21 +01004030#if !defined(MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE) || \
4031 !defined(MBEDTLS_SSL_SRV_C)
4032 ((void) partial);
4033#endif
4034
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004035 ssl->state = MBEDTLS_SSL_HELLO_REQUEST;
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01004036
Manuel Pégourié-Gonnard286a1362015-05-13 16:22:05 +02004037 /* Cancel any possibly running timer */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004038 mbedtls_ssl_set_timer(ssl, 0);
Manuel Pégourié-Gonnard286a1362015-05-13 16:22:05 +02004039
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004040#if defined(MBEDTLS_SSL_RENEGOTIATION)
4041 ssl->renego_status = MBEDTLS_SSL_INITIAL_HANDSHAKE;
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01004042 ssl->renego_records_seen = 0;
Paul Bakker48916f92012-09-16 19:57:18 +00004043
4044 ssl->verify_data_len = 0;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004045 memset(ssl->own_verify_data, 0, MBEDTLS_SSL_VERIFY_DATA_MAX_LEN);
4046 memset(ssl->peer_verify_data, 0, MBEDTLS_SSL_VERIFY_DATA_MAX_LEN);
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01004047#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004048 ssl->secure_renegotiation = MBEDTLS_SSL_LEGACY_RENEGOTIATION;
Paul Bakker48916f92012-09-16 19:57:18 +00004049
Paul Bakker7eb013f2011-10-06 12:37:39 +00004050 ssl->in_offt = NULL;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004051 mbedtls_ssl_reset_in_out_pointers(ssl);
Paul Bakker7eb013f2011-10-06 12:37:39 +00004052
4053 ssl->in_msgtype = 0;
4054 ssl->in_msglen = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004055#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02004056 ssl->next_record_offset = 0;
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02004057 ssl->in_epoch = 0;
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02004058#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004059#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004060 mbedtls_ssl_dtls_replay_reset(ssl);
Manuel Pégourié-Gonnardb47368a2014-09-24 13:29:58 +02004061#endif
Paul Bakker7eb013f2011-10-06 12:37:39 +00004062
4063 ssl->in_hslen = 0;
4064 ssl->nb_zero = 0;
Hanno Beckeraf0665d2017-05-24 09:16:26 +01004065
4066 ssl->keep_current_message = 0;
Paul Bakker7eb013f2011-10-06 12:37:39 +00004067
4068 ssl->out_msgtype = 0;
4069 ssl->out_msglen = 0;
4070 ssl->out_left = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004071#if defined(MBEDTLS_SSL_CBC_RECORD_SPLITTING)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004072 if (ssl->split_done != MBEDTLS_SSL_CBC_RECORD_SPLITTING_DISABLED) {
Manuel Pégourié-Gonnardcfa477e2015-01-07 14:50:54 +01004073 ssl->split_done = 0;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004074 }
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +01004075#endif
Paul Bakker7eb013f2011-10-06 12:37:39 +00004076
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004077 memset(ssl->cur_out_ctr, 0, sizeof(ssl->cur_out_ctr));
Hanno Becker19859472018-08-06 09:40:20 +01004078
Paul Bakker48916f92012-09-16 19:57:18 +00004079 ssl->transform_in = NULL;
4080 ssl->transform_out = NULL;
Paul Bakker7eb013f2011-10-06 12:37:39 +00004081
Hanno Becker78640902018-08-13 16:35:15 +01004082 ssl->session_in = NULL;
4083 ssl->session_out = NULL;
4084
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004085 memset(ssl->out_buf, 0, out_buf_len);
Hanno Becker4ccbf062018-08-10 11:20:38 +01004086
David Horstmannd4f22082022-10-26 18:25:14 +01004087 int clear_in_buf = 1;
Hanno Becker4ccbf062018-08-10 11:20:38 +01004088#if defined(MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE) && defined(MBEDTLS_SSL_SRV_C)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004089 if (partial != 0) {
David Horstmannd4f22082022-10-26 18:25:14 +01004090 clear_in_buf = 0;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004091 }
Hanno Becker4ccbf062018-08-10 11:20:38 +01004092#endif /* MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE && MBEDTLS_SSL_SRV_C */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004093 if (clear_in_buf) {
Hanno Becker4ccbf062018-08-10 11:20:38 +01004094 ssl->in_left = 0;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004095 memset(ssl->in_buf, 0, in_buf_len);
Hanno Becker4ccbf062018-08-10 11:20:38 +01004096 }
Paul Bakker05ef8352012-05-08 09:17:57 +00004097
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004098#if defined(MBEDTLS_SSL_HW_RECORD_ACCEL)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004099 if (mbedtls_ssl_hw_record_reset != NULL) {
4100 MBEDTLS_SSL_DEBUG_MSG(2, ("going for mbedtls_ssl_hw_record_reset()"));
4101 if ((ret = mbedtls_ssl_hw_record_reset(ssl)) != 0) {
4102 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ssl_hw_record_reset", ret);
4103 return MBEDTLS_ERR_SSL_HW_ACCEL_FAILED;
Paul Bakker2770fbd2012-07-03 13:30:23 +00004104 }
Paul Bakker05ef8352012-05-08 09:17:57 +00004105 }
4106#endif
Paul Bakker2770fbd2012-07-03 13:30:23 +00004107
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004108 if (ssl->transform) {
4109 mbedtls_ssl_transform_free(ssl->transform);
4110 mbedtls_free(ssl->transform);
Paul Bakker48916f92012-09-16 19:57:18 +00004111 ssl->transform = NULL;
Paul Bakker2770fbd2012-07-03 13:30:23 +00004112 }
Paul Bakker48916f92012-09-16 19:57:18 +00004113
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004114 if (ssl->session) {
4115 mbedtls_ssl_session_free(ssl->session);
4116 mbedtls_free(ssl->session);
Paul Bakkerc0463502013-02-14 11:19:38 +01004117 ssl->session = NULL;
4118 }
4119
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004120#if defined(MBEDTLS_SSL_ALPN)
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02004121 ssl->alpn_chosen = NULL;
4122#endif
4123
Manuel Pégourié-Gonnarde057d3b2015-05-20 10:59:43 +02004124#if defined(MBEDTLS_SSL_DTLS_HELLO_VERIFY) && defined(MBEDTLS_SSL_SRV_C)
David Horstmannb5b1ed22022-10-27 13:21:49 +01004125 int free_cli_id = 1;
Hanno Becker4ccbf062018-08-10 11:20:38 +01004126#if defined(MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004127 if (partial != 0) {
David Horstmannd4f22082022-10-26 18:25:14 +01004128 free_cli_id = 0;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004129 }
Hanno Becker4ccbf062018-08-10 11:20:38 +01004130#endif
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004131 if (free_cli_id) {
4132 mbedtls_free(ssl->cli_id);
Manuel Pégourié-Gonnard3f09b6d2015-09-08 11:58:14 +02004133 ssl->cli_id = NULL;
4134 ssl->cli_id_len = 0;
4135 }
Manuel Pégourié-Gonnard43c02182014-07-22 17:32:01 +02004136#endif
4137
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004138 if ((ret = ssl_handshake_init(ssl)) != 0) {
4139 return ret;
4140 }
Paul Bakker2770fbd2012-07-03 13:30:23 +00004141
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004142 return 0;
Paul Bakker7eb013f2011-10-06 12:37:39 +00004143}
4144
Manuel Pégourié-Gonnard779e4292013-08-03 13:50:48 +02004145/*
Manuel Pégourié-Gonnard3f09b6d2015-09-08 11:58:14 +02004146 * Reset an initialized and used SSL context for re-use while retaining
4147 * all application-set variables, function pointers and data.
4148 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004149int mbedtls_ssl_session_reset(mbedtls_ssl_context *ssl)
Manuel Pégourié-Gonnard3f09b6d2015-09-08 11:58:14 +02004150{
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004151 return mbedtls_ssl_session_reset_int(ssl, 0);
Manuel Pégourié-Gonnard3f09b6d2015-09-08 11:58:14 +02004152}
4153
4154/*
Paul Bakker5121ce52009-01-03 21:22:43 +00004155 * SSL set accessors
4156 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004157void mbedtls_ssl_conf_endpoint(mbedtls_ssl_config *conf, int endpoint)
Paul Bakker5121ce52009-01-03 21:22:43 +00004158{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02004159 conf->endpoint = endpoint;
Paul Bakker5121ce52009-01-03 21:22:43 +00004160}
4161
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004162void mbedtls_ssl_conf_transport(mbedtls_ssl_config *conf, int transport)
Manuel Pégourié-Gonnard0b1ff292014-02-06 13:04:16 +01004163{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02004164 conf->transport = transport;
Manuel Pégourié-Gonnard0b1ff292014-02-06 13:04:16 +01004165}
4166
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004167#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004168void mbedtls_ssl_conf_dtls_anti_replay(mbedtls_ssl_config *conf, char mode)
Manuel Pégourié-Gonnard27393132014-09-24 14:41:11 +02004169{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02004170 conf->anti_replay = mode;
Manuel Pégourié-Gonnard27393132014-09-24 14:41:11 +02004171}
4172#endif
4173
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004174#if defined(MBEDTLS_SSL_DTLS_BADMAC_LIMIT)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004175void mbedtls_ssl_conf_dtls_badmac_limit(mbedtls_ssl_config *conf, unsigned limit)
Manuel Pégourié-Gonnardb0643d12014-10-14 18:30:36 +02004176{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02004177 conf->badmac_limit = limit;
Manuel Pégourié-Gonnardb0643d12014-10-14 18:30:36 +02004178}
4179#endif
4180
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004181#if defined(MBEDTLS_SSL_PROTO_DTLS)
Hanno Becker04da1892018-08-14 13:22:10 +01004182
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004183void mbedtls_ssl_set_datagram_packing(mbedtls_ssl_context *ssl,
4184 unsigned allow_packing)
Hanno Becker04da1892018-08-14 13:22:10 +01004185{
4186 ssl->disable_datagram_packing = !allow_packing;
4187}
4188
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004189void mbedtls_ssl_conf_handshake_timeout(mbedtls_ssl_config *conf,
4190 uint32_t min, uint32_t max)
Manuel Pégourié-Gonnard905dd242014-10-01 12:03:55 +02004191{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02004192 conf->hs_timeout_min = min;
4193 conf->hs_timeout_max = max;
Manuel Pégourié-Gonnard905dd242014-10-01 12:03:55 +02004194}
4195#endif
4196
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004197void mbedtls_ssl_conf_authmode(mbedtls_ssl_config *conf, int authmode)
Paul Bakker5121ce52009-01-03 21:22:43 +00004198{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02004199 conf->authmode = authmode;
Paul Bakker5121ce52009-01-03 21:22:43 +00004200}
4201
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004202#if defined(MBEDTLS_X509_CRT_PARSE_C)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004203void mbedtls_ssl_conf_verify(mbedtls_ssl_config *conf,
4204 int (*f_vrfy)(void *, mbedtls_x509_crt *, int, uint32_t *),
4205 void *p_vrfy)
Paul Bakkerb63b0af2011-01-13 17:54:59 +00004206{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02004207 conf->f_vrfy = f_vrfy;
4208 conf->p_vrfy = p_vrfy;
Paul Bakkerb63b0af2011-01-13 17:54:59 +00004209}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004210#endif /* MBEDTLS_X509_CRT_PARSE_C */
Paul Bakkerb63b0af2011-01-13 17:54:59 +00004211
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004212void mbedtls_ssl_conf_rng(mbedtls_ssl_config *conf,
4213 int (*f_rng)(void *, unsigned char *, size_t),
4214 void *p_rng)
Paul Bakker5121ce52009-01-03 21:22:43 +00004215{
Manuel Pégourié-Gonnard750e4d72015-05-07 12:35:38 +01004216 conf->f_rng = f_rng;
4217 conf->p_rng = p_rng;
Paul Bakker5121ce52009-01-03 21:22:43 +00004218}
4219
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004220void mbedtls_ssl_conf_dbg(mbedtls_ssl_config *conf,
4221 void (*f_dbg)(void *, int, const char *, int, const char *),
4222 void *p_dbg)
Paul Bakker5121ce52009-01-03 21:22:43 +00004223{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02004224 conf->f_dbg = f_dbg;
4225 conf->p_dbg = p_dbg;
Paul Bakker5121ce52009-01-03 21:22:43 +00004226}
4227
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004228void mbedtls_ssl_set_bio(mbedtls_ssl_context *ssl,
4229 void *p_bio,
4230 mbedtls_ssl_send_t *f_send,
4231 mbedtls_ssl_recv_t *f_recv,
4232 mbedtls_ssl_recv_timeout_t *f_recv_timeout)
Manuel Pégourié-Gonnard8fa6dfd2014-09-17 10:47:43 +02004233{
4234 ssl->p_bio = p_bio;
4235 ssl->f_send = f_send;
4236 ssl->f_recv = f_recv;
4237 ssl->f_recv_timeout = f_recv_timeout;
Manuel Pégourié-Gonnard97fd52c2015-05-06 15:38:52 +01004238}
4239
Manuel Pégourié-Gonnard6e7aaca2018-08-20 10:37:23 +02004240#if defined(MBEDTLS_SSL_PROTO_DTLS)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004241void mbedtls_ssl_set_mtu(mbedtls_ssl_context *ssl, uint16_t mtu)
Manuel Pégourié-Gonnard6e7aaca2018-08-20 10:37:23 +02004242{
4243 ssl->mtu = mtu;
4244}
4245#endif
4246
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004247void mbedtls_ssl_conf_read_timeout(mbedtls_ssl_config *conf, uint32_t timeout)
Manuel Pégourié-Gonnard97fd52c2015-05-06 15:38:52 +01004248{
4249 conf->read_timeout = timeout;
Manuel Pégourié-Gonnard8fa6dfd2014-09-17 10:47:43 +02004250}
4251
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004252void mbedtls_ssl_set_timer_cb(mbedtls_ssl_context *ssl,
4253 void *p_timer,
4254 mbedtls_ssl_set_timer_t *f_set_timer,
4255 mbedtls_ssl_get_timer_t *f_get_timer)
Manuel Pégourié-Gonnard2e012912015-05-12 20:55:41 +02004256{
4257 ssl->p_timer = p_timer;
4258 ssl->f_set_timer = f_set_timer;
4259 ssl->f_get_timer = f_get_timer;
Manuel Pégourié-Gonnard286a1362015-05-13 16:22:05 +02004260
4261 /* Make sure we start with no timer running */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004262 mbedtls_ssl_set_timer(ssl, 0);
Manuel Pégourié-Gonnard2e012912015-05-12 20:55:41 +02004263}
4264
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004265#if defined(MBEDTLS_SSL_SRV_C)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004266void mbedtls_ssl_conf_session_cache(mbedtls_ssl_config *conf,
4267 void *p_cache,
4268 int (*f_get_cache)(void *, mbedtls_ssl_session *),
4269 int (*f_set_cache)(void *, const mbedtls_ssl_session *))
Paul Bakker5121ce52009-01-03 21:22:43 +00004270{
Manuel Pégourié-Gonnard5cb33082015-05-06 18:06:26 +01004271 conf->p_cache = p_cache;
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02004272 conf->f_get_cache = f_get_cache;
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02004273 conf->f_set_cache = f_set_cache;
Paul Bakker5121ce52009-01-03 21:22:43 +00004274}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004275#endif /* MBEDTLS_SSL_SRV_C */
Paul Bakker5121ce52009-01-03 21:22:43 +00004276
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004277#if defined(MBEDTLS_SSL_CLI_C)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004278int mbedtls_ssl_set_session(mbedtls_ssl_context *ssl, const mbedtls_ssl_session *session)
Paul Bakker5121ce52009-01-03 21:22:43 +00004279{
Janos Follath865b3eb2019-12-16 11:46:15 +00004280 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +02004281
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004282 if (ssl == NULL ||
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +02004283 session == NULL ||
4284 ssl->session_negotiate == NULL ||
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004285 ssl->conf->endpoint != MBEDTLS_SSL_IS_CLIENT) {
4286 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +02004287 }
4288
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004289 if ((ret = mbedtls_ssl_session_copy(ssl->session_negotiate,
4290 session)) != 0) {
4291 return ret;
4292 }
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +02004293
Paul Bakker0a597072012-09-25 21:55:46 +00004294 ssl->handshake->resume = 1;
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +02004295
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004296 return 0;
Paul Bakker5121ce52009-01-03 21:22:43 +00004297}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004298#endif /* MBEDTLS_SSL_CLI_C */
Paul Bakker5121ce52009-01-03 21:22:43 +00004299
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004300void mbedtls_ssl_conf_ciphersuites(mbedtls_ssl_config *conf,
4301 const int *ciphersuites)
Paul Bakker5121ce52009-01-03 21:22:43 +00004302{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02004303 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_0] = ciphersuites;
4304 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_1] = ciphersuites;
4305 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_2] = ciphersuites;
4306 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_3] = ciphersuites;
Paul Bakker8f4ddae2013-04-15 15:09:54 +02004307}
4308
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004309void mbedtls_ssl_conf_ciphersuites_for_version(mbedtls_ssl_config *conf,
4310 const int *ciphersuites,
4311 int major, int minor)
Paul Bakker8f4ddae2013-04-15 15:09:54 +02004312{
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004313 if (major != MBEDTLS_SSL_MAJOR_VERSION_3) {
Paul Bakker8f4ddae2013-04-15 15:09:54 +02004314 return;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004315 }
Paul Bakker8f4ddae2013-04-15 15:09:54 +02004316
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004317 if (minor < MBEDTLS_SSL_MINOR_VERSION_0 || minor > MBEDTLS_SSL_MINOR_VERSION_3) {
Paul Bakker8f4ddae2013-04-15 15:09:54 +02004318 return;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004319 }
Paul Bakker8f4ddae2013-04-15 15:09:54 +02004320
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02004321 conf->ciphersuite_list[minor] = ciphersuites;
Paul Bakker5121ce52009-01-03 21:22:43 +00004322}
4323
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004324#if defined(MBEDTLS_X509_CRT_PARSE_C)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004325void mbedtls_ssl_conf_cert_profile(mbedtls_ssl_config *conf,
4326 const mbedtls_x509_crt_profile *profile)
Manuel Pégourié-Gonnard6e3ee3a2015-06-17 10:58:20 +02004327{
4328 conf->cert_profile = profile;
4329}
4330
Manuel Pégourié-Gonnard8f618a82015-05-10 21:13:36 +02004331/* Append a new keycert entry to a (possibly empty) list */
Manuel Pégourié-Gonnardd904d662022-06-17 10:24:00 +02004332MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004333static int ssl_append_key_cert(mbedtls_ssl_key_cert **head,
4334 mbedtls_x509_crt *cert,
4335 mbedtls_pk_context *key)
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02004336{
niisato8ee24222018-06-25 19:05:48 +09004337 mbedtls_ssl_key_cert *new_cert;
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02004338
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004339 new_cert = mbedtls_calloc(1, sizeof(mbedtls_ssl_key_cert));
4340 if (new_cert == NULL) {
4341 return MBEDTLS_ERR_SSL_ALLOC_FAILED;
4342 }
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02004343
niisato8ee24222018-06-25 19:05:48 +09004344 new_cert->cert = cert;
4345 new_cert->key = key;
4346 new_cert->next = NULL;
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02004347
Manuel Pégourié-Gonnard8f618a82015-05-10 21:13:36 +02004348 /* Update head is the list was null, else add to the end */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004349 if (*head == NULL) {
niisato8ee24222018-06-25 19:05:48 +09004350 *head = new_cert;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004351 } else {
Manuel Pégourié-Gonnard8f618a82015-05-10 21:13:36 +02004352 mbedtls_ssl_key_cert *cur = *head;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004353 while (cur->next != NULL) {
Manuel Pégourié-Gonnard8f618a82015-05-10 21:13:36 +02004354 cur = cur->next;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004355 }
niisato8ee24222018-06-25 19:05:48 +09004356 cur->next = new_cert;
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02004357 }
4358
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004359 return 0;
Manuel Pégourié-Gonnard8f618a82015-05-10 21:13:36 +02004360}
4361
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004362int mbedtls_ssl_conf_own_cert(mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnard8f618a82015-05-10 21:13:36 +02004363 mbedtls_x509_crt *own_cert,
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004364 mbedtls_pk_context *pk_key)
Manuel Pégourié-Gonnard8f618a82015-05-10 21:13:36 +02004365{
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004366 return ssl_append_key_cert(&conf->key_cert, own_cert, pk_key);
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02004367}
4368
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004369void mbedtls_ssl_conf_ca_chain(mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnardbc2b7712015-05-06 11:14:19 +01004370 mbedtls_x509_crt *ca_chain,
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004371 mbedtls_x509_crl *ca_crl)
Paul Bakker5121ce52009-01-03 21:22:43 +00004372{
Manuel Pégourié-Gonnardbc2b7712015-05-06 11:14:19 +01004373 conf->ca_chain = ca_chain;
4374 conf->ca_crl = ca_crl;
Hanno Becker5adaad92019-03-27 16:54:37 +00004375
4376#if defined(MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK)
4377 /* mbedtls_ssl_conf_ca_chain() and mbedtls_ssl_conf_ca_cb()
4378 * cannot be used together. */
4379 conf->f_ca_cb = NULL;
4380 conf->p_ca_cb = NULL;
4381#endif /* MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK */
Paul Bakker5121ce52009-01-03 21:22:43 +00004382}
Hanno Becker5adaad92019-03-27 16:54:37 +00004383
4384#if defined(MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004385void mbedtls_ssl_conf_ca_cb(mbedtls_ssl_config *conf,
4386 mbedtls_x509_crt_ca_cb_t f_ca_cb,
4387 void *p_ca_cb)
Hanno Becker5adaad92019-03-27 16:54:37 +00004388{
4389 conf->f_ca_cb = f_ca_cb;
4390 conf->p_ca_cb = p_ca_cb;
4391
4392 /* mbedtls_ssl_conf_ca_chain() and mbedtls_ssl_conf_ca_cb()
4393 * cannot be used together. */
4394 conf->ca_chain = NULL;
4395 conf->ca_crl = NULL;
4396}
4397#endif /* MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004398#endif /* MBEDTLS_X509_CRT_PARSE_C */
Paul Bakkereb2c6582012-09-27 19:15:01 +00004399
Manuel Pégourié-Gonnard1af6c852015-05-10 23:10:37 +02004400#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004401int mbedtls_ssl_set_hs_own_cert(mbedtls_ssl_context *ssl,
4402 mbedtls_x509_crt *own_cert,
4403 mbedtls_pk_context *pk_key)
Manuel Pégourié-Gonnard1af6c852015-05-10 23:10:37 +02004404{
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004405 return ssl_append_key_cert(&ssl->handshake->sni_key_cert,
4406 own_cert, pk_key);
Manuel Pégourié-Gonnard1af6c852015-05-10 23:10:37 +02004407}
Manuel Pégourié-Gonnard22bfa4b2015-05-11 08:46:37 +02004408
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004409void mbedtls_ssl_set_hs_ca_chain(mbedtls_ssl_context *ssl,
4410 mbedtls_x509_crt *ca_chain,
4411 mbedtls_x509_crl *ca_crl)
Manuel Pégourié-Gonnard22bfa4b2015-05-11 08:46:37 +02004412{
4413 ssl->handshake->sni_ca_chain = ca_chain;
4414 ssl->handshake->sni_ca_crl = ca_crl;
4415}
Manuel Pégourié-Gonnardcdc26ae2015-06-19 12:16:31 +02004416
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004417void mbedtls_ssl_set_hs_authmode(mbedtls_ssl_context *ssl,
4418 int authmode)
Manuel Pégourié-Gonnardcdc26ae2015-06-19 12:16:31 +02004419{
4420 ssl->handshake->sni_authmode = authmode;
4421}
Manuel Pégourié-Gonnard1af6c852015-05-10 23:10:37 +02004422#endif /* MBEDTLS_SSL_SERVER_NAME_INDICATION */
4423
Hanno Becker8927c832019-04-03 12:52:50 +01004424#if defined(MBEDTLS_X509_CRT_PARSE_C)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004425void mbedtls_ssl_set_verify(mbedtls_ssl_context *ssl,
4426 int (*f_vrfy)(void *, mbedtls_x509_crt *, int, uint32_t *),
4427 void *p_vrfy)
Hanno Becker8927c832019-04-03 12:52:50 +01004428{
4429 ssl->f_vrfy = f_vrfy;
4430 ssl->p_vrfy = p_vrfy;
4431}
4432#endif
4433
Manuel Pégourié-Gonnardeef142d2015-09-16 10:05:04 +02004434#if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
Manuel Pégourié-Gonnard7002f4a2015-09-15 12:43:43 +02004435/*
4436 * Set EC J-PAKE password for current handshake
4437 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004438int mbedtls_ssl_set_hs_ecjpake_password(mbedtls_ssl_context *ssl,
4439 const unsigned char *pw,
4440 size_t pw_len)
Manuel Pégourié-Gonnard7002f4a2015-09-15 12:43:43 +02004441{
4442 mbedtls_ecjpake_role role;
4443
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004444 if (ssl->handshake == NULL || ssl->conf == NULL) {
4445 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
4446 }
Manuel Pégourié-Gonnard7002f4a2015-09-15 12:43:43 +02004447
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004448 if (ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER) {
Manuel Pégourié-Gonnard7002f4a2015-09-15 12:43:43 +02004449 role = MBEDTLS_ECJPAKE_SERVER;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004450 } else {
Manuel Pégourié-Gonnard7002f4a2015-09-15 12:43:43 +02004451 role = MBEDTLS_ECJPAKE_CLIENT;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004452 }
Manuel Pégourié-Gonnard7002f4a2015-09-15 12:43:43 +02004453
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004454 return mbedtls_ecjpake_setup(&ssl->handshake->ecjpake_ctx,
4455 role,
4456 MBEDTLS_MD_SHA256,
4457 MBEDTLS_ECP_DP_SECP256R1,
4458 pw, pw_len);
Manuel Pégourié-Gonnard7002f4a2015-09-15 12:43:43 +02004459}
Manuel Pégourié-Gonnardeef142d2015-09-16 10:05:04 +02004460#endif /* MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED */
Manuel Pégourié-Gonnard7002f4a2015-09-15 12:43:43 +02004461
Gilles Peskineeccd8882020-03-10 12:19:08 +01004462#if defined(MBEDTLS_KEY_EXCHANGE_SOME_PSK_ENABLED)
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01004463
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004464static void ssl_conf_remove_psk(mbedtls_ssl_config *conf)
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01004465{
4466 /* Remove reference to existing PSK, if any. */
4467#if defined(MBEDTLS_USE_PSA_CRYPTO)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004468 if (!mbedtls_svc_key_id_is_null(conf->psk_opaque)) {
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01004469 /* The maintenance of the PSK key slot is the
4470 * user's responsibility. */
Ronald Croncf56a0a2020-08-04 09:51:30 +02004471 conf->psk_opaque = MBEDTLS_SVC_KEY_ID_INIT;
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01004472 }
Hanno Beckera63ac3f2018-11-05 12:47:16 +00004473 /* This and the following branch should never
Tom Cosgrove49f99bc2022-12-04 16:44:21 +00004474 * be taken simultaneously as we maintain the
Hanno Beckera63ac3f2018-11-05 12:47:16 +00004475 * invariant that raw and opaque PSKs are never
4476 * configured simultaneously. As a safeguard,
4477 * though, `else` is omitted here. */
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01004478#endif /* MBEDTLS_USE_PSA_CRYPTO */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004479 if (conf->psk != NULL) {
4480 mbedtls_platform_zeroize(conf->psk, conf->psk_len);
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01004481
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004482 mbedtls_free(conf->psk);
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01004483 conf->psk = NULL;
4484 conf->psk_len = 0;
4485 }
4486
4487 /* Remove reference to PSK identity, if any. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004488 if (conf->psk_identity != NULL) {
4489 mbedtls_free(conf->psk_identity);
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01004490 conf->psk_identity = NULL;
4491 conf->psk_identity_len = 0;
4492 }
4493}
4494
Hanno Becker7390c712018-11-15 13:33:04 +00004495/* This function assumes that PSK identity in the SSL config is unset.
4496 * It checks that the provided identity is well-formed and attempts
4497 * to make a copy of it in the SSL config.
4498 * On failure, the PSK identity in the config remains unset. */
Manuel Pégourié-Gonnardd904d662022-06-17 10:24:00 +02004499MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004500static int ssl_conf_set_psk_identity(mbedtls_ssl_config *conf,
4501 unsigned char const *psk_identity,
4502 size_t psk_identity_len)
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02004503{
Manuel Pégourié-Gonnardc6b5d832015-08-27 16:37:35 +02004504 /* Identity len will be encoded on two bytes */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004505 if (psk_identity == NULL ||
4506 (psk_identity_len >> 16) != 0 ||
4507 psk_identity_len > MBEDTLS_SSL_OUT_CONTENT_LEN) {
4508 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
Manuel Pégourié-Gonnardc6b5d832015-08-27 16:37:35 +02004509 }
4510
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004511 conf->psk_identity = mbedtls_calloc(1, psk_identity_len);
4512 if (conf->psk_identity == NULL) {
4513 return MBEDTLS_ERR_SSL_ALLOC_FAILED;
4514 }
Paul Bakker6db455e2013-09-18 17:29:31 +02004515
Manuel Pégourié-Gonnard120fdbd2015-05-07 17:07:50 +01004516 conf->psk_identity_len = psk_identity_len;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004517 memcpy(conf->psk_identity, psk_identity, conf->psk_identity_len);
Paul Bakker5ad403f2013-09-18 21:21:30 +02004518
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004519 return 0;
Paul Bakker6db455e2013-09-18 17:29:31 +02004520}
4521
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004522int mbedtls_ssl_conf_psk(mbedtls_ssl_config *conf,
4523 const unsigned char *psk, size_t psk_len,
4524 const unsigned char *psk_identity, size_t psk_identity_len)
Hanno Becker7390c712018-11-15 13:33:04 +00004525{
Janos Follath865b3eb2019-12-16 11:46:15 +00004526 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Hanno Becker7390c712018-11-15 13:33:04 +00004527 /* Remove opaque/raw PSK + PSK Identity */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004528 ssl_conf_remove_psk(conf);
Hanno Becker7390c712018-11-15 13:33:04 +00004529
4530 /* Check and set raw PSK */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004531 if (psk == NULL) {
4532 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
4533 }
4534 if (psk_len == 0) {
4535 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
4536 }
4537 if (psk_len > MBEDTLS_PSK_MAX_LEN) {
4538 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
4539 }
Piotr Nowicki9926eaf2019-11-20 14:54:36 +01004540
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004541 if ((conf->psk = mbedtls_calloc(1, psk_len)) == NULL) {
4542 return MBEDTLS_ERR_SSL_ALLOC_FAILED;
4543 }
Hanno Becker7390c712018-11-15 13:33:04 +00004544 conf->psk_len = psk_len;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004545 memcpy(conf->psk, psk, conf->psk_len);
Hanno Becker7390c712018-11-15 13:33:04 +00004546
4547 /* Check and set PSK Identity */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004548 ret = ssl_conf_set_psk_identity(conf, psk_identity, psk_identity_len);
4549 if (ret != 0) {
4550 ssl_conf_remove_psk(conf);
4551 }
Hanno Becker7390c712018-11-15 13:33:04 +00004552
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004553 return ret;
Hanno Becker7390c712018-11-15 13:33:04 +00004554}
4555
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004556static void ssl_remove_psk(mbedtls_ssl_context *ssl)
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01004557{
4558#if defined(MBEDTLS_USE_PSA_CRYPTO)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004559 if (!mbedtls_svc_key_id_is_null(ssl->handshake->psk_opaque)) {
Ronald Croncf56a0a2020-08-04 09:51:30 +02004560 ssl->handshake->psk_opaque = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004561 } else
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01004562#endif /* MBEDTLS_USE_PSA_CRYPTO */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004563 if (ssl->handshake->psk != NULL) {
4564 mbedtls_platform_zeroize(ssl->handshake->psk,
4565 ssl->handshake->psk_len);
4566 mbedtls_free(ssl->handshake->psk);
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01004567 ssl->handshake->psk_len = 0;
lhuang040a2dd6d2024-06-11 12:31:17 -07004568 ssl->handshake->psk = NULL;
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01004569 }
4570}
4571
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004572int mbedtls_ssl_set_hs_psk(mbedtls_ssl_context *ssl,
4573 const unsigned char *psk, size_t psk_len)
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01004574{
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004575 if (psk == NULL || ssl->handshake == NULL) {
4576 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
4577 }
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01004578
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004579 if (psk_len > MBEDTLS_PSK_MAX_LEN) {
4580 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
4581 }
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01004582
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004583 ssl_remove_psk(ssl);
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01004584
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004585 if ((ssl->handshake->psk = mbedtls_calloc(1, psk_len)) == NULL) {
4586 return MBEDTLS_ERR_SSL_ALLOC_FAILED;
4587 }
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01004588
4589 ssl->handshake->psk_len = psk_len;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004590 memcpy(ssl->handshake->psk, psk, ssl->handshake->psk_len);
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01004591
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004592 return 0;
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01004593}
4594
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01004595#if defined(MBEDTLS_USE_PSA_CRYPTO)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004596int mbedtls_ssl_conf_psk_opaque(mbedtls_ssl_config *conf,
4597 psa_key_id_t psk,
4598 const unsigned char *psk_identity,
4599 size_t psk_identity_len)
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01004600{
Janos Follath865b3eb2019-12-16 11:46:15 +00004601 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Hanno Becker7390c712018-11-15 13:33:04 +00004602 /* Clear opaque/raw PSK + PSK Identity, if present. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004603 ssl_conf_remove_psk(conf);
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01004604
Hanno Becker7390c712018-11-15 13:33:04 +00004605 /* Check and set opaque PSK */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004606 if (mbedtls_svc_key_id_is_null(psk)) {
4607 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
4608 }
Ronald Croncf56a0a2020-08-04 09:51:30 +02004609 conf->psk_opaque = psk;
Hanno Becker7390c712018-11-15 13:33:04 +00004610
4611 /* Check and set PSK Identity */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004612 ret = ssl_conf_set_psk_identity(conf, psk_identity,
4613 psk_identity_len);
4614 if (ret != 0) {
4615 ssl_conf_remove_psk(conf);
4616 }
Hanno Becker7390c712018-11-15 13:33:04 +00004617
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004618 return ret;
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01004619}
4620
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004621int mbedtls_ssl_set_hs_psk_opaque(mbedtls_ssl_context *ssl,
4622 psa_key_id_t psk)
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01004623{
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004624 if ((mbedtls_svc_key_id_is_null(psk)) ||
4625 (ssl->handshake == NULL)) {
4626 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
4627 }
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01004628
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004629 ssl_remove_psk(ssl);
Ronald Croncf56a0a2020-08-04 09:51:30 +02004630 ssl->handshake->psk_opaque = psk;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004631 return 0;
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01004632}
4633#endif /* MBEDTLS_USE_PSA_CRYPTO */
4634
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004635void mbedtls_ssl_conf_psk_cb(mbedtls_ssl_config *conf,
4636 int (*f_psk)(void *, mbedtls_ssl_context *, const unsigned char *,
4637 size_t),
4638 void *p_psk)
Paul Bakker6db455e2013-09-18 17:29:31 +02004639{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02004640 conf->f_psk = f_psk;
4641 conf->p_psk = p_psk;
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02004642}
Gilles Peskineeccd8882020-03-10 12:19:08 +01004643#endif /* MBEDTLS_KEY_EXCHANGE_SOME_PSK_ENABLED */
Paul Bakker43b7e352011-01-18 15:27:19 +00004644
Manuel Pégourié-Gonnardcf141ca2015-05-20 10:35:51 +02004645#if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_SSL_SRV_C)
Hanno Becker470a8c42017-10-04 15:28:46 +01004646
4647#if !defined(MBEDTLS_DEPRECATED_REMOVED)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004648int mbedtls_ssl_conf_dh_param(mbedtls_ssl_config *conf, const char *dhm_P, const char *dhm_G)
Paul Bakker5121ce52009-01-03 21:22:43 +00004649{
Janos Follath865b3eb2019-12-16 11:46:15 +00004650 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker5121ce52009-01-03 21:22:43 +00004651
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004652 if ((ret = mbedtls_mpi_read_string(&conf->dhm_P, 16, dhm_P)) != 0 ||
4653 (ret = mbedtls_mpi_read_string(&conf->dhm_G, 16, dhm_G)) != 0) {
4654 mbedtls_mpi_free(&conf->dhm_P);
4655 mbedtls_mpi_free(&conf->dhm_G);
4656 return ret;
Manuel Pégourié-Gonnard1028b742015-05-06 17:33:07 +01004657 }
Paul Bakker5121ce52009-01-03 21:22:43 +00004658
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004659 return 0;
Paul Bakker5121ce52009-01-03 21:22:43 +00004660}
Hanno Becker470a8c42017-10-04 15:28:46 +01004661#endif /* MBEDTLS_DEPRECATED_REMOVED */
Paul Bakker5121ce52009-01-03 21:22:43 +00004662
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004663int mbedtls_ssl_conf_dh_param_bin(mbedtls_ssl_config *conf,
4664 const unsigned char *dhm_P, size_t P_len,
4665 const unsigned char *dhm_G, size_t G_len)
Hanno Beckera90658f2017-10-04 15:29:08 +01004666{
Janos Follath865b3eb2019-12-16 11:46:15 +00004667 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Hanno Beckera90658f2017-10-04 15:29:08 +01004668
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004669 mbedtls_mpi_free(&conf->dhm_P);
4670 mbedtls_mpi_free(&conf->dhm_G);
Glenn Straussde081ce2021-12-20 01:43:17 -05004671
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004672 if ((ret = mbedtls_mpi_read_binary(&conf->dhm_P, dhm_P, P_len)) != 0 ||
4673 (ret = mbedtls_mpi_read_binary(&conf->dhm_G, dhm_G, G_len)) != 0) {
4674 mbedtls_mpi_free(&conf->dhm_P);
4675 mbedtls_mpi_free(&conf->dhm_G);
4676 return ret;
Hanno Beckera90658f2017-10-04 15:29:08 +01004677 }
4678
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004679 return 0;
Hanno Beckera90658f2017-10-04 15:29:08 +01004680}
Paul Bakker5121ce52009-01-03 21:22:43 +00004681
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004682int mbedtls_ssl_conf_dh_param_ctx(mbedtls_ssl_config *conf, mbedtls_dhm_context *dhm_ctx)
Paul Bakker1b57b062011-01-06 15:48:19 +00004683{
Janos Follath865b3eb2019-12-16 11:46:15 +00004684 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker1b57b062011-01-06 15:48:19 +00004685
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_copy(&conf->dhm_P, &dhm_ctx->P)) != 0 ||
4690 (ret = mbedtls_mpi_copy(&conf->dhm_G, &dhm_ctx->G)) != 0) {
4691 mbedtls_mpi_free(&conf->dhm_P);
4692 mbedtls_mpi_free(&conf->dhm_G);
4693 return ret;
Manuel Pégourié-Gonnard1028b742015-05-06 17:33:07 +01004694 }
Paul Bakker1b57b062011-01-06 15:48:19 +00004695
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004696 return 0;
Paul Bakker1b57b062011-01-06 15:48:19 +00004697}
Manuel Pégourié-Gonnardcf141ca2015-05-20 10:35:51 +02004698#endif /* MBEDTLS_DHM_C && MBEDTLS_SSL_SRV_C */
Paul Bakker1b57b062011-01-06 15:48:19 +00004699
Manuel Pégourié-Gonnardbd990d62015-06-11 14:49:42 +02004700#if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_SSL_CLI_C)
4701/*
4702 * Set the minimum length for Diffie-Hellman parameters
4703 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004704void mbedtls_ssl_conf_dhm_min_bitlen(mbedtls_ssl_config *conf,
4705 unsigned int bitlen)
Manuel Pégourié-Gonnardbd990d62015-06-11 14:49:42 +02004706{
4707 conf->dhm_min_bitlen = bitlen;
4708}
4709#endif /* MBEDTLS_DHM_C && MBEDTLS_SSL_CLI_C */
4710
Gilles Peskineeccd8882020-03-10 12:19:08 +01004711#if defined(MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED)
Manuel Pégourié-Gonnard36a8b572015-06-17 12:43:26 +02004712/*
4713 * Set allowed/preferred hashes for handshake signatures
4714 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004715void mbedtls_ssl_conf_sig_hashes(mbedtls_ssl_config *conf,
4716 const int *hashes)
Manuel Pégourié-Gonnard36a8b572015-06-17 12:43:26 +02004717{
4718 conf->sig_hashes = hashes;
4719}
Gilles Peskineeccd8882020-03-10 12:19:08 +01004720#endif /* MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED */
Manuel Pégourié-Gonnard36a8b572015-06-17 12:43:26 +02004721
Manuel Pégourié-Gonnardb541da62015-06-17 11:43:30 +02004722#if defined(MBEDTLS_ECP_C)
Manuel Pégourié-Gonnard7f38ed02014-02-04 15:52:33 +01004723/*
4724 * Set the allowed elliptic curves
4725 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004726void mbedtls_ssl_conf_curves(mbedtls_ssl_config *conf,
4727 const mbedtls_ecp_group_id *curve_list)
Manuel Pégourié-Gonnard7f38ed02014-02-04 15:52:33 +01004728{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02004729 conf->curve_list = curve_list;
Manuel Pégourié-Gonnard7f38ed02014-02-04 15:52:33 +01004730}
Hanno Becker947194e2017-04-07 13:25:49 +01004731#endif /* MBEDTLS_ECP_C */
Manuel Pégourié-Gonnard7f38ed02014-02-04 15:52:33 +01004732
Manuel Pégourié-Gonnardbc2b7712015-05-06 11:14:19 +01004733#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004734void mbedtls_ssl_conf_sni(mbedtls_ssl_config *conf,
4735 int (*f_sni)(void *, mbedtls_ssl_context *,
4736 const unsigned char *, size_t),
4737 void *p_sni)
Paul Bakker5701cdc2012-09-27 21:49:42 +00004738{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02004739 conf->f_sni = f_sni;
4740 conf->p_sni = p_sni;
Paul Bakker5701cdc2012-09-27 21:49:42 +00004741}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004742#endif /* MBEDTLS_SSL_SERVER_NAME_INDICATION */
Paul Bakker5701cdc2012-09-27 21:49:42 +00004743
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004744#if defined(MBEDTLS_SSL_ALPN)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004745int mbedtls_ssl_conf_alpn_protocols(mbedtls_ssl_config *conf, const char **protos)
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02004746{
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +02004747 size_t cur_len, tot_len;
4748 const char **p;
4749
4750 /*
Brian J Murray1903fb32016-11-06 04:45:15 -08004751 * RFC 7301 3.1: "Empty strings MUST NOT be included and byte strings
4752 * MUST NOT be truncated."
4753 * We check lengths now rather than later.
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +02004754 */
4755 tot_len = 0;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004756 for (p = protos; *p != NULL; p++) {
4757 cur_len = strlen(*p);
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +02004758 tot_len += cur_len;
4759
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004760 if ((cur_len == 0) ||
4761 (cur_len > MBEDTLS_SSL_MAX_ALPN_NAME_LEN) ||
4762 (tot_len > MBEDTLS_SSL_MAX_ALPN_LIST_LEN)) {
4763 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
4764 }
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +02004765 }
4766
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02004767 conf->alpn_list = protos;
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +02004768
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004769 return 0;
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02004770}
4771
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004772const char *mbedtls_ssl_get_alpn_protocol(const mbedtls_ssl_context *ssl)
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02004773{
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004774 return ssl->alpn_chosen;
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02004775}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004776#endif /* MBEDTLS_SSL_ALPN */
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02004777
Johan Pascalb62bb512015-12-03 21:56:45 +01004778#if defined(MBEDTLS_SSL_DTLS_SRTP)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004779void mbedtls_ssl_conf_srtp_mki_value_supported(mbedtls_ssl_config *conf,
4780 int support_mki_value)
Ron Eldor591f1622018-01-22 12:30:04 +02004781{
4782 conf->dtls_srtp_mki_support = support_mki_value;
4783}
4784
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004785int mbedtls_ssl_dtls_srtp_set_mki_value(mbedtls_ssl_context *ssl,
4786 unsigned char *mki_value,
4787 uint16_t mki_len)
Ron Eldor591f1622018-01-22 12:30:04 +02004788{
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004789 if (mki_len > MBEDTLS_TLS_SRTP_MAX_MKI_LENGTH) {
4790 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
Ron Eldora9788042018-12-05 11:04:31 +02004791 }
Ron Eldor591f1622018-01-22 12:30:04 +02004792
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004793 if (ssl->conf->dtls_srtp_mki_support == MBEDTLS_SSL_DTLS_SRTP_MKI_UNSUPPORTED) {
4794 return MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
Ron Eldora9788042018-12-05 11:04:31 +02004795 }
Ron Eldor591f1622018-01-22 12:30:04 +02004796
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004797 memcpy(ssl->dtls_srtp_info.mki_value, mki_value, mki_len);
Ron Eldor591f1622018-01-22 12:30:04 +02004798 ssl->dtls_srtp_info.mki_len = mki_len;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004799 return 0;
Ron Eldor591f1622018-01-22 12:30:04 +02004800}
4801
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004802int mbedtls_ssl_conf_dtls_srtp_protection_profiles(mbedtls_ssl_config *conf,
4803 const mbedtls_ssl_srtp_profile *profiles)
Johan Pascalb62bb512015-12-03 21:56:45 +01004804{
Johan Pascal253d0262020-09-22 13:04:45 +02004805 const mbedtls_ssl_srtp_profile *p;
4806 size_t list_size = 0;
Johan Pascalb62bb512015-12-03 21:56:45 +01004807
Johan Pascal253d0262020-09-22 13:04:45 +02004808 /* check the profiles list: all entry must be valid,
4809 * its size cannot be more than the total number of supported profiles, currently 4 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004810 for (p = profiles; *p != MBEDTLS_TLS_SRTP_UNSET &&
4811 list_size <= MBEDTLS_TLS_SRTP_MAX_PROFILE_LIST_LENGTH;
4812 p++) {
4813 if (mbedtls_ssl_check_srtp_profile_value(*p) != MBEDTLS_TLS_SRTP_UNSET) {
Johan Pascal76fdf1d2020-10-22 23:31:00 +02004814 list_size++;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004815 } else {
Johan Pascal76fdf1d2020-10-22 23:31:00 +02004816 /* unsupported value, stop parsing and set the size to an error value */
4817 list_size = MBEDTLS_TLS_SRTP_MAX_PROFILE_LIST_LENGTH + 1;
Johan Pascalb62bb512015-12-03 21:56:45 +01004818 }
4819 }
4820
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004821 if (list_size > MBEDTLS_TLS_SRTP_MAX_PROFILE_LIST_LENGTH) {
4822 conf->dtls_srtp_profile_list = NULL;
4823 conf->dtls_srtp_profile_list_len = 0;
4824 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
Johan Pascal253d0262020-09-22 13:04:45 +02004825 }
4826
Johan Pascal9bc97ca2020-09-21 23:44:45 +02004827 conf->dtls_srtp_profile_list = profiles;
Johan Pascal253d0262020-09-22 13:04:45 +02004828 conf->dtls_srtp_profile_list_len = list_size;
Johan Pascalb62bb512015-12-03 21:56:45 +01004829
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004830 return 0;
Johan Pascalb62bb512015-12-03 21:56:45 +01004831}
4832
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004833void mbedtls_ssl_get_dtls_srtp_negotiation_result(const mbedtls_ssl_context *ssl,
4834 mbedtls_dtls_srtp_info *dtls_srtp_info)
Johan Pascalb62bb512015-12-03 21:56:45 +01004835{
Johan Pascal2258a4f2020-10-28 13:53:09 +01004836 dtls_srtp_info->chosen_dtls_srtp_profile = ssl->dtls_srtp_info.chosen_dtls_srtp_profile;
4837 /* do not copy the mki value if there is no chosen profile */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004838 if (dtls_srtp_info->chosen_dtls_srtp_profile == MBEDTLS_TLS_SRTP_UNSET) {
Johan Pascal2258a4f2020-10-28 13:53:09 +01004839 dtls_srtp_info->mki_len = 0;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004840 } else {
Johan Pascal2258a4f2020-10-28 13:53:09 +01004841 dtls_srtp_info->mki_len = ssl->dtls_srtp_info.mki_len;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004842 memcpy(dtls_srtp_info->mki_value, ssl->dtls_srtp_info.mki_value,
4843 ssl->dtls_srtp_info.mki_len);
Johan Pascal2258a4f2020-10-28 13:53:09 +01004844 }
Johan Pascalb62bb512015-12-03 21:56:45 +01004845}
Johan Pascalb62bb512015-12-03 21:56:45 +01004846#endif /* MBEDTLS_SSL_DTLS_SRTP */
4847
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004848void mbedtls_ssl_conf_max_version(mbedtls_ssl_config *conf, int major, int minor)
Paul Bakker490ecc82011-10-06 13:04:09 +00004849{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02004850 conf->max_major_ver = major;
4851 conf->max_minor_ver = minor;
Paul Bakker490ecc82011-10-06 13:04:09 +00004852}
4853
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004854void mbedtls_ssl_conf_min_version(mbedtls_ssl_config *conf, int major, int minor)
Paul Bakker1d29fb52012-09-28 13:28:45 +00004855{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02004856 conf->min_major_ver = major;
4857 conf->min_minor_ver = minor;
Paul Bakker1d29fb52012-09-28 13:28:45 +00004858}
4859
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004860#if defined(MBEDTLS_SSL_FALLBACK_SCSV) && defined(MBEDTLS_SSL_CLI_C)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004861void mbedtls_ssl_conf_fallback(mbedtls_ssl_config *conf, char fallback)
Manuel Pégourié-Gonnard1cbd39d2014-10-20 13:34:59 +02004862{
Manuel Pégourié-Gonnard684b0592015-05-06 09:27:31 +01004863 conf->fallback = fallback;
Manuel Pégourié-Gonnard1cbd39d2014-10-20 13:34:59 +02004864}
4865#endif
4866
Janos Follath088ce432017-04-10 12:42:31 +01004867#if defined(MBEDTLS_SSL_SRV_C)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004868void mbedtls_ssl_conf_cert_req_ca_list(mbedtls_ssl_config *conf,
4869 char cert_req_ca_list)
Janos Follath088ce432017-04-10 12:42:31 +01004870{
4871 conf->cert_req_ca_list = cert_req_ca_list;
4872}
4873#endif
4874
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004875#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004876void mbedtls_ssl_conf_encrypt_then_mac(mbedtls_ssl_config *conf, char etm)
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +01004877{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02004878 conf->encrypt_then_mac = etm;
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +01004879}
4880#endif
4881
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004882#if defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004883void mbedtls_ssl_conf_extended_master_secret(mbedtls_ssl_config *conf, char ems)
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +02004884{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02004885 conf->extended_ms = ems;
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +02004886}
4887#endif
4888
Manuel Pégourié-Gonnard66dc5552015-05-14 12:28:21 +02004889#if defined(MBEDTLS_ARC4_C)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004890void mbedtls_ssl_conf_arc4_support(mbedtls_ssl_config *conf, char arc4)
Manuel Pégourié-Gonnardbd47a582015-01-12 13:43:29 +01004891{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02004892 conf->arc4_disabled = arc4;
Manuel Pégourié-Gonnardbd47a582015-01-12 13:43:29 +01004893}
Manuel Pégourié-Gonnard66dc5552015-05-14 12:28:21 +02004894#endif
Manuel Pégourié-Gonnardbd47a582015-01-12 13:43:29 +01004895
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004896#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004897int mbedtls_ssl_conf_max_frag_len(mbedtls_ssl_config *conf, unsigned char mfl_code)
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02004898{
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004899 if (mfl_code >= MBEDTLS_SSL_MAX_FRAG_LEN_INVALID ||
4900 ssl_mfl_code_to_length(mfl_code) > MBEDTLS_TLS_EXT_ADV_CONTENT_LEN) {
4901 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02004902 }
4903
Manuel Pégourié-Gonnard6bf89d62015-05-05 17:01:57 +01004904 conf->mfl_code = mfl_code;
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02004905
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004906 return 0;
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02004907}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004908#endif /* MBEDTLS_SSL_MAX_FRAGMENT_LENGTH */
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02004909
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004910#if defined(MBEDTLS_SSL_TRUNCATED_HMAC)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004911void mbedtls_ssl_conf_truncated_hmac(mbedtls_ssl_config *conf, int truncate)
Manuel Pégourié-Gonnarde980a992013-07-19 11:08:52 +02004912{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02004913 conf->trunc_hmac = truncate;
Manuel Pégourié-Gonnarde980a992013-07-19 11:08:52 +02004914}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004915#endif /* MBEDTLS_SSL_TRUNCATED_HMAC */
Manuel Pégourié-Gonnarde980a992013-07-19 11:08:52 +02004916
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004917#if defined(MBEDTLS_SSL_CBC_RECORD_SPLITTING)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004918void mbedtls_ssl_conf_cbc_record_splitting(mbedtls_ssl_config *conf, char split)
Manuel Pégourié-Gonnardcfa477e2015-01-07 14:50:54 +01004919{
Manuel Pégourié-Gonnard17eab2b2015-05-05 16:34:53 +01004920 conf->cbc_record_splitting = split;
Manuel Pégourié-Gonnardcfa477e2015-01-07 14:50:54 +01004921}
4922#endif
4923
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004924void mbedtls_ssl_conf_legacy_renegotiation(mbedtls_ssl_config *conf, int allow_legacy)
Paul Bakker48916f92012-09-16 19:57:18 +00004925{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02004926 conf->allow_legacy_renegotiation = allow_legacy;
Paul Bakker48916f92012-09-16 19:57:18 +00004927}
4928
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004929#if defined(MBEDTLS_SSL_RENEGOTIATION)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004930void mbedtls_ssl_conf_renegotiation(mbedtls_ssl_config *conf, int renegotiation)
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01004931{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02004932 conf->disable_renegotiation = renegotiation;
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01004933}
4934
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004935void mbedtls_ssl_conf_renegotiation_enforced(mbedtls_ssl_config *conf, int max_records)
Manuel Pégourié-Gonnarda9964db2014-07-03 19:29:16 +02004936{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02004937 conf->renego_max_records = max_records;
Manuel Pégourié-Gonnarda9964db2014-07-03 19:29:16 +02004938}
4939
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004940void mbedtls_ssl_conf_renegotiation_period(mbedtls_ssl_config *conf,
4941 const unsigned char period[8])
Manuel Pégourié-Gonnard837f0fe2014-11-05 13:58:53 +01004942{
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004943 memcpy(conf->renego_period, period, 8);
Manuel Pégourié-Gonnard837f0fe2014-11-05 13:58:53 +01004944}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004945#endif /* MBEDTLS_SSL_RENEGOTIATION */
Paul Bakker5121ce52009-01-03 21:22:43 +00004946
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004947#if defined(MBEDTLS_SSL_SESSION_TICKETS)
Manuel Pégourié-Gonnardb596abf2015-05-20 10:45:29 +02004948#if defined(MBEDTLS_SSL_CLI_C)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004949void mbedtls_ssl_conf_session_tickets(mbedtls_ssl_config *conf, int use_tickets)
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +02004950{
Manuel Pégourié-Gonnard2b494452015-05-06 10:05:11 +01004951 conf->session_tickets = use_tickets;
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +02004952}
Manuel Pégourié-Gonnardb596abf2015-05-20 10:45:29 +02004953#endif
Paul Bakker606b4ba2013-08-14 16:52:14 +02004954
Manuel Pégourié-Gonnardb596abf2015-05-20 10:45:29 +02004955#if defined(MBEDTLS_SSL_SRV_C)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004956void mbedtls_ssl_conf_session_tickets_cb(mbedtls_ssl_config *conf,
4957 mbedtls_ssl_ticket_write_t *f_ticket_write,
4958 mbedtls_ssl_ticket_parse_t *f_ticket_parse,
4959 void *p_ticket)
Paul Bakker606b4ba2013-08-14 16:52:14 +02004960{
Manuel Pégourié-Gonnardd59675d2015-05-19 15:28:00 +02004961 conf->f_ticket_write = f_ticket_write;
4962 conf->f_ticket_parse = f_ticket_parse;
4963 conf->p_ticket = p_ticket;
Paul Bakker606b4ba2013-08-14 16:52:14 +02004964}
Manuel Pégourié-Gonnardb596abf2015-05-20 10:45:29 +02004965#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004966#endif /* MBEDTLS_SSL_SESSION_TICKETS */
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +02004967
Robert Cragie4feb7ae2015-10-02 13:33:37 +01004968#if defined(MBEDTLS_SSL_EXPORT_KEYS)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004969void mbedtls_ssl_conf_export_keys_cb(mbedtls_ssl_config *conf,
4970 mbedtls_ssl_export_keys_t *f_export_keys,
4971 void *p_export_keys)
Robert Cragie4feb7ae2015-10-02 13:33:37 +01004972{
4973 conf->f_export_keys = f_export_keys;
4974 conf->p_export_keys = p_export_keys;
4975}
Ron Eldorf5cc10d2019-05-07 18:33:40 +03004976
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004977void mbedtls_ssl_conf_export_keys_ext_cb(mbedtls_ssl_config *conf,
4978 mbedtls_ssl_export_keys_ext_t *f_export_keys_ext,
4979 void *p_export_keys)
Ron Eldorf5cc10d2019-05-07 18:33:40 +03004980{
4981 conf->f_export_keys_ext = f_export_keys_ext;
4982 conf->p_export_keys = p_export_keys;
4983}
Robert Cragie4feb7ae2015-10-02 13:33:37 +01004984#endif
4985
Gilles Peskineb74a1c72018-04-24 13:09:22 +02004986#if defined(MBEDTLS_SSL_ASYNC_PRIVATE)
Gilles Peskine8bf79f62018-01-05 21:11:53 +01004987void mbedtls_ssl_conf_async_private_cb(
4988 mbedtls_ssl_config *conf,
4989 mbedtls_ssl_async_sign_t *f_async_sign,
4990 mbedtls_ssl_async_decrypt_t *f_async_decrypt,
4991 mbedtls_ssl_async_resume_t *f_async_resume,
4992 mbedtls_ssl_async_cancel_t *f_async_cancel,
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004993 void *async_config_data)
Gilles Peskine8bf79f62018-01-05 21:11:53 +01004994{
4995 conf->f_async_sign_start = f_async_sign;
4996 conf->f_async_decrypt_start = f_async_decrypt;
4997 conf->f_async_resume = f_async_resume;
4998 conf->f_async_cancel = f_async_cancel;
Gilles Peskinedf13d5c2018-04-25 20:39:48 +02004999 conf->p_async_config_data = async_config_data;
5000}
5001
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005002void *mbedtls_ssl_conf_get_async_config_data(const mbedtls_ssl_config *conf)
Gilles Peskine8f97af72018-04-26 11:46:10 +02005003{
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005004 return conf->p_async_config_data;
Gilles Peskine8f97af72018-04-26 11:46:10 +02005005}
5006
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005007void *mbedtls_ssl_get_async_operation_data(const mbedtls_ssl_context *ssl)
Gilles Peskinedf13d5c2018-04-25 20:39:48 +02005008{
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005009 if (ssl->handshake == NULL) {
5010 return NULL;
5011 } else {
5012 return ssl->handshake->user_async_ctx;
5013 }
Gilles Peskinedf13d5c2018-04-25 20:39:48 +02005014}
5015
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005016void mbedtls_ssl_set_async_operation_data(mbedtls_ssl_context *ssl,
5017 void *ctx)
Gilles Peskinedf13d5c2018-04-25 20:39:48 +02005018{
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005019 if (ssl->handshake != NULL) {
Gilles Peskinedf13d5c2018-04-25 20:39:48 +02005020 ssl->handshake->user_async_ctx = ctx;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005021 }
Gilles Peskine8bf79f62018-01-05 21:11:53 +01005022}
Gilles Peskineb74a1c72018-04-24 13:09:22 +02005023#endif /* MBEDTLS_SSL_ASYNC_PRIVATE */
Gilles Peskine8bf79f62018-01-05 21:11:53 +01005024
Paul Bakker5121ce52009-01-03 21:22:43 +00005025/*
5026 * SSL get accessors
5027 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005028uint32_t mbedtls_ssl_get_verify_result(const mbedtls_ssl_context *ssl)
Paul Bakker5121ce52009-01-03 21:22:43 +00005029{
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005030 if (ssl->session != NULL) {
5031 return ssl->session->verify_result;
5032 }
Manuel Pégourié-Gonnarde89163c2015-01-23 14:30:57 +00005033
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005034 if (ssl->session_negotiate != NULL) {
5035 return ssl->session_negotiate->verify_result;
5036 }
Manuel Pégourié-Gonnarde89163c2015-01-23 14:30:57 +00005037
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005038 return 0xFFFFFFFF;
Paul Bakker5121ce52009-01-03 21:22:43 +00005039}
5040
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005041const char *mbedtls_ssl_get_ciphersuite(const mbedtls_ssl_context *ssl)
Paul Bakker72f62662011-01-16 21:27:44 +00005042{
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005043 if (ssl == NULL || ssl->session == NULL) {
5044 return NULL;
5045 }
Paul Bakker926c8e42013-03-06 10:23:34 +01005046
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005047 return mbedtls_ssl_get_ciphersuite_name(ssl->session->ciphersuite);
Paul Bakker72f62662011-01-16 21:27:44 +00005048}
5049
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005050const char *mbedtls_ssl_get_version(const mbedtls_ssl_context *ssl)
Paul Bakker43ca69c2011-01-15 17:35:19 +00005051{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005052#if defined(MBEDTLS_SSL_PROTO_DTLS)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005053 if (ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM) {
5054 switch (ssl->minor_ver) {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005055 case MBEDTLS_SSL_MINOR_VERSION_2:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005056 return "DTLSv1.0";
Manuel Pégourié-Gonnardb21ca2a2014-02-10 13:43:33 +01005057
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005058 case MBEDTLS_SSL_MINOR_VERSION_3:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005059 return "DTLSv1.2";
Manuel Pégourié-Gonnardb21ca2a2014-02-10 13:43:33 +01005060
5061 default:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005062 return "unknown (DTLS)";
Manuel Pégourié-Gonnardb21ca2a2014-02-10 13:43:33 +01005063 }
5064 }
5065#endif
5066
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005067 switch (ssl->minor_ver) {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005068 case MBEDTLS_SSL_MINOR_VERSION_0:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005069 return "SSLv3.0";
Paul Bakker43ca69c2011-01-15 17:35:19 +00005070
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005071 case MBEDTLS_SSL_MINOR_VERSION_1:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005072 return "TLSv1.0";
Paul Bakker43ca69c2011-01-15 17:35:19 +00005073
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005074 case MBEDTLS_SSL_MINOR_VERSION_2:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005075 return "TLSv1.1";
Paul Bakker43ca69c2011-01-15 17:35:19 +00005076
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005077 case MBEDTLS_SSL_MINOR_VERSION_3:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005078 return "TLSv1.2";
Paul Bakker1ef83d62012-04-11 12:09:53 +00005079
Paul Bakker43ca69c2011-01-15 17:35:19 +00005080 default:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005081 return "unknown";
Paul Bakker43ca69c2011-01-15 17:35:19 +00005082 }
Paul Bakker43ca69c2011-01-15 17:35:19 +00005083}
5084
Manuel Pégourié-Gonnarda2cda6b2015-08-31 18:30:52 +02005085#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005086size_t mbedtls_ssl_get_input_max_frag_len(const mbedtls_ssl_context *ssl)
Andrzej Kurek90c6e842020-04-03 05:25:29 -04005087{
5088 size_t max_len = MBEDTLS_SSL_MAX_CONTENT_LEN;
5089 size_t read_mfl;
5090
5091 /* Use the configured MFL for the client if we're past SERVER_HELLO_DONE */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005092 if (ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT &&
5093 ssl->state >= MBEDTLS_SSL_SERVER_HELLO_DONE) {
5094 return ssl_mfl_code_to_length(ssl->conf->mfl_code);
Andrzej Kurek90c6e842020-04-03 05:25:29 -04005095 }
5096
5097 /* Check if a smaller max length was negotiated */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005098 if (ssl->session_out != NULL) {
5099 read_mfl = ssl_mfl_code_to_length(ssl->session_out->mfl_code);
5100 if (read_mfl < max_len) {
Andrzej Kurek90c6e842020-04-03 05:25:29 -04005101 max_len = read_mfl;
5102 }
5103 }
5104
5105 // During a handshake, use the value being negotiated
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005106 if (ssl->session_negotiate != NULL) {
5107 read_mfl = ssl_mfl_code_to_length(ssl->session_negotiate->mfl_code);
5108 if (read_mfl < max_len) {
Andrzej Kurek90c6e842020-04-03 05:25:29 -04005109 max_len = read_mfl;
5110 }
5111 }
5112
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005113 return max_len;
Andrzej Kurek90c6e842020-04-03 05:25:29 -04005114}
5115
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005116size_t mbedtls_ssl_get_output_max_frag_len(const mbedtls_ssl_context *ssl)
Manuel Pégourié-Gonnarda2cda6b2015-08-31 18:30:52 +02005117{
5118 size_t max_len;
5119
5120 /*
5121 * Assume mfl_code is correct since it was checked when set
5122 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005123 max_len = ssl_mfl_code_to_length(ssl->conf->mfl_code);
Manuel Pégourié-Gonnarda2cda6b2015-08-31 18:30:52 +02005124
Manuel Pégourié-Gonnard2cb17e22017-09-19 13:00:47 +02005125 /* Check if a smaller max length was negotiated */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005126 if (ssl->session_out != NULL &&
5127 ssl_mfl_code_to_length(ssl->session_out->mfl_code) < max_len) {
5128 max_len = ssl_mfl_code_to_length(ssl->session_out->mfl_code);
Manuel Pégourié-Gonnarda2cda6b2015-08-31 18:30:52 +02005129 }
5130
Manuel Pégourié-Gonnard2cb17e22017-09-19 13:00:47 +02005131 /* During a handshake, use the value being negotiated */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005132 if (ssl->session_negotiate != NULL &&
5133 ssl_mfl_code_to_length(ssl->session_negotiate->mfl_code) < max_len) {
5134 max_len = ssl_mfl_code_to_length(ssl->session_negotiate->mfl_code);
Manuel Pégourié-Gonnard2cb17e22017-09-19 13:00:47 +02005135 }
5136
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005137 return max_len;
Manuel Pégourié-Gonnarda2cda6b2015-08-31 18:30:52 +02005138}
Andrzej Kurek90c6e842020-04-03 05:25:29 -04005139
5140#if !defined(MBEDTLS_DEPRECATED_REMOVED)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005141size_t mbedtls_ssl_get_max_frag_len(const mbedtls_ssl_context *ssl)
Andrzej Kurek90c6e842020-04-03 05:25:29 -04005142{
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005143 return mbedtls_ssl_get_output_max_frag_len(ssl);
Andrzej Kurek90c6e842020-04-03 05:25:29 -04005144}
5145#endif /* !MBEDTLS_DEPRECATED_REMOVED */
Manuel Pégourié-Gonnarda2cda6b2015-08-31 18:30:52 +02005146#endif /* MBEDTLS_SSL_MAX_FRAGMENT_LENGTH */
5147
Manuel Pégourié-Gonnardb8eec192018-08-20 09:34:02 +02005148#if defined(MBEDTLS_SSL_PROTO_DTLS)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005149size_t mbedtls_ssl_get_current_mtu(const mbedtls_ssl_context *ssl)
Manuel Pégourié-Gonnardb8eec192018-08-20 09:34:02 +02005150{
Andrzej Kurekef43ce62018-10-09 08:24:12 -04005151 /* Return unlimited mtu for client hello messages to avoid fragmentation. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005152 if (ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT &&
5153 (ssl->state == MBEDTLS_SSL_CLIENT_HELLO ||
5154 ssl->state == MBEDTLS_SSL_SERVER_HELLO)) {
5155 return 0;
5156 }
Andrzej Kurekef43ce62018-10-09 08:24:12 -04005157
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005158 if (ssl->handshake == NULL || ssl->handshake->mtu == 0) {
5159 return ssl->mtu;
5160 }
Manuel Pégourié-Gonnardb8eec192018-08-20 09:34:02 +02005161
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005162 if (ssl->mtu == 0) {
5163 return ssl->handshake->mtu;
5164 }
Manuel Pégourié-Gonnardb8eec192018-08-20 09:34:02 +02005165
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005166 return ssl->mtu < ssl->handshake->mtu ?
5167 ssl->mtu : ssl->handshake->mtu;
Manuel Pégourié-Gonnardb8eec192018-08-20 09:34:02 +02005168}
5169#endif /* MBEDTLS_SSL_PROTO_DTLS */
5170
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005171int mbedtls_ssl_get_max_out_record_payload(const mbedtls_ssl_context *ssl)
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02005172{
5173 size_t max_len = MBEDTLS_SSL_OUT_CONTENT_LEN;
5174
Manuel Pégourié-Gonnard000281e2018-08-21 11:20:58 +02005175#if !defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH) && \
5176 !defined(MBEDTLS_SSL_PROTO_DTLS)
5177 (void) ssl;
5178#endif
5179
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02005180#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005181 const size_t mfl = mbedtls_ssl_get_output_max_frag_len(ssl);
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02005182
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005183 if (max_len > mfl) {
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02005184 max_len = mfl;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005185 }
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02005186#endif
5187
5188#if defined(MBEDTLS_SSL_PROTO_DTLS)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005189 if (mbedtls_ssl_get_current_mtu(ssl) != 0) {
5190 const size_t mtu = mbedtls_ssl_get_current_mtu(ssl);
5191 const int ret = mbedtls_ssl_get_record_expansion(ssl);
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02005192 const size_t overhead = (size_t) ret;
5193
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005194 if (ret < 0) {
5195 return ret;
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02005196 }
5197
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005198 if (mtu <= overhead) {
5199 MBEDTLS_SSL_DEBUG_MSG(1, ("MTU too low for record expansion"));
5200 return MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
5201 }
5202
5203 if (max_len > mtu - overhead) {
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02005204 max_len = mtu - overhead;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005205 }
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02005206 }
Manuel Pégourié-Gonnardb8eec192018-08-20 09:34:02 +02005207#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02005208
Hanno Becker0defedb2018-08-10 12:35:02 +01005209#if !defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH) && \
5210 !defined(MBEDTLS_SSL_PROTO_DTLS)
5211 ((void) ssl);
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02005212#endif
5213
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005214 return (int) max_len;
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02005215}
5216
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005217#if defined(MBEDTLS_X509_CRT_PARSE_C)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005218const mbedtls_x509_crt *mbedtls_ssl_get_peer_cert(const mbedtls_ssl_context *ssl)
Paul Bakkerb0550d92012-10-30 07:51:03 +00005219{
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005220 if (ssl == NULL || ssl->session == NULL) {
5221 return NULL;
5222 }
Paul Bakkerb0550d92012-10-30 07:51:03 +00005223
Hanno Beckere6824572019-02-07 13:18:46 +00005224#if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005225 return ssl->session->peer_cert;
Hanno Beckere6824572019-02-07 13:18:46 +00005226#else
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005227 return NULL;
Hanno Beckere6824572019-02-07 13:18:46 +00005228#endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
Paul Bakkerb0550d92012-10-30 07:51:03 +00005229}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005230#endif /* MBEDTLS_X509_CRT_PARSE_C */
Paul Bakkerb0550d92012-10-30 07:51:03 +00005231
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005232#if defined(MBEDTLS_SSL_CLI_C)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005233int mbedtls_ssl_get_session(const mbedtls_ssl_context *ssl,
5234 mbedtls_ssl_session *dst)
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02005235{
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005236 if (ssl == NULL ||
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02005237 dst == NULL ||
5238 ssl->session == NULL ||
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005239 ssl->conf->endpoint != MBEDTLS_SSL_IS_CLIENT) {
5240 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02005241 }
5242
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005243 return mbedtls_ssl_session_copy(dst, ssl->session);
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02005244}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005245#endif /* MBEDTLS_SSL_CLI_C */
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02005246
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005247const mbedtls_ssl_session *mbedtls_ssl_get_session_pointer(const mbedtls_ssl_context *ssl)
Manuel Pégourié-Gonnardb5e4e0a2019-05-20 11:12:28 +02005248{
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005249 if (ssl == NULL) {
5250 return NULL;
5251 }
Manuel Pégourié-Gonnardb5e4e0a2019-05-20 11:12:28 +02005252
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005253 return ssl->session;
Manuel Pégourié-Gonnardb5e4e0a2019-05-20 11:12:28 +02005254}
5255
Paul Bakker5121ce52009-01-03 21:22:43 +00005256/*
Hanno Beckera835da52019-05-16 12:39:07 +01005257 * Define ticket header determining Mbed TLS version
5258 * and structure of the ticket.
5259 */
5260
Hanno Becker94ef3b32019-05-16 12:50:45 +01005261/*
Hanno Becker50b59662019-05-28 14:30:45 +01005262 * Define bitflag determining compile-time settings influencing
5263 * structure of serialized SSL sessions.
Hanno Becker94ef3b32019-05-16 12:50:45 +01005264 */
5265
Hanno Becker50b59662019-05-28 14:30:45 +01005266#if defined(MBEDTLS_HAVE_TIME)
Hanno Becker3e088662019-05-29 11:10:18 +01005267#define SSL_SERIALIZED_SESSION_CONFIG_TIME 1
Hanno Becker50b59662019-05-28 14:30:45 +01005268#else
Hanno Becker3e088662019-05-29 11:10:18 +01005269#define SSL_SERIALIZED_SESSION_CONFIG_TIME 0
Hanno Becker94ef3b32019-05-16 12:50:45 +01005270#endif /* MBEDTLS_HAVE_TIME */
5271
5272#if defined(MBEDTLS_X509_CRT_PARSE_C)
Hanno Becker3e088662019-05-29 11:10:18 +01005273#define SSL_SERIALIZED_SESSION_CONFIG_CRT 1
Hanno Becker94ef3b32019-05-16 12:50:45 +01005274#else
Hanno Becker3e088662019-05-29 11:10:18 +01005275#define SSL_SERIALIZED_SESSION_CONFIG_CRT 0
Hanno Becker94ef3b32019-05-16 12:50:45 +01005276#endif /* MBEDTLS_X509_CRT_PARSE_C */
5277
David Horstmanneb77b6f2024-02-13 17:53:35 +00005278#if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
David Horstmann11def972024-03-01 11:20:32 +00005279#define SSL_SERIALIZED_SESSION_CONFIG_KEEP_PEER_CRT 1
David Horstmanneb77b6f2024-02-13 17:53:35 +00005280#else
David Horstmann11def972024-03-01 11:20:32 +00005281#define SSL_SERIALIZED_SESSION_CONFIG_KEEP_PEER_CRT 0
David Horstmanneb77b6f2024-02-13 17:53:35 +00005282#endif /* MBEDTLS_SSL_SESSION_TICKETS */
5283
Hanno Becker94ef3b32019-05-16 12:50:45 +01005284#if defined(MBEDTLS_SSL_CLI_C) && defined(MBEDTLS_SSL_SESSION_TICKETS)
Hanno Becker3e088662019-05-29 11:10:18 +01005285#define SSL_SERIALIZED_SESSION_CONFIG_CLIENT_TICKET 1
Hanno Becker94ef3b32019-05-16 12:50:45 +01005286#else
Hanno Becker3e088662019-05-29 11:10:18 +01005287#define SSL_SERIALIZED_SESSION_CONFIG_CLIENT_TICKET 0
Hanno Becker94ef3b32019-05-16 12:50:45 +01005288#endif /* MBEDTLS_SSL_CLI_C && MBEDTLS_SSL_SESSION_TICKETS */
5289
5290#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
Hanno Becker3e088662019-05-29 11:10:18 +01005291#define SSL_SERIALIZED_SESSION_CONFIG_MFL 1
Hanno Becker94ef3b32019-05-16 12:50:45 +01005292#else
Hanno Becker3e088662019-05-29 11:10:18 +01005293#define SSL_SERIALIZED_SESSION_CONFIG_MFL 0
Hanno Becker94ef3b32019-05-16 12:50:45 +01005294#endif /* MBEDTLS_SSL_MAX_FRAGMENT_LENGTH */
5295
5296#if defined(MBEDTLS_SSL_TRUNCATED_HMAC)
Hanno Becker3e088662019-05-29 11:10:18 +01005297#define SSL_SERIALIZED_SESSION_CONFIG_TRUNC_HMAC 1
Hanno Becker94ef3b32019-05-16 12:50:45 +01005298#else
Hanno Becker3e088662019-05-29 11:10:18 +01005299#define SSL_SERIALIZED_SESSION_CONFIG_TRUNC_HMAC 0
Hanno Becker94ef3b32019-05-16 12:50:45 +01005300#endif /* MBEDTLS_SSL_TRUNCATED_HMAC */
5301
5302#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
Hanno Becker3e088662019-05-29 11:10:18 +01005303#define SSL_SERIALIZED_SESSION_CONFIG_ETM 1
Hanno Becker94ef3b32019-05-16 12:50:45 +01005304#else
Hanno Becker3e088662019-05-29 11:10:18 +01005305#define SSL_SERIALIZED_SESSION_CONFIG_ETM 0
Hanno Becker94ef3b32019-05-16 12:50:45 +01005306#endif /* MBEDTLS_SSL_ENCRYPT_THEN_MAC */
5307
Hanno Becker94ef3b32019-05-16 12:50:45 +01005308#if defined(MBEDTLS_SSL_SESSION_TICKETS)
5309#define SSL_SERIALIZED_SESSION_CONFIG_TICKET 1
5310#else
5311#define SSL_SERIALIZED_SESSION_CONFIG_TICKET 0
5312#endif /* MBEDTLS_SSL_SESSION_TICKETS */
5313
Hanno Becker3e088662019-05-29 11:10:18 +01005314#define SSL_SERIALIZED_SESSION_CONFIG_TIME_BIT 0
5315#define SSL_SERIALIZED_SESSION_CONFIG_CRT_BIT 1
5316#define SSL_SERIALIZED_SESSION_CONFIG_CLIENT_TICKET_BIT 2
5317#define SSL_SERIALIZED_SESSION_CONFIG_MFL_BIT 3
5318#define SSL_SERIALIZED_SESSION_CONFIG_TRUNC_HMAC_BIT 4
5319#define SSL_SERIALIZED_SESSION_CONFIG_ETM_BIT 5
5320#define SSL_SERIALIZED_SESSION_CONFIG_TICKET_BIT 6
David Horstmann11def972024-03-01 11:20:32 +00005321#define SSL_SERIALIZED_SESSION_CONFIG_KEEP_PEER_CRT_BIT 7
Hanno Becker3e088662019-05-29 11:10:18 +01005322
Hanno Becker50b59662019-05-28 14:30:45 +01005323#define SSL_SERIALIZED_SESSION_CONFIG_BITFLAG \
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005324 ((uint16_t) ( \
5325 (SSL_SERIALIZED_SESSION_CONFIG_TIME << SSL_SERIALIZED_SESSION_CONFIG_TIME_BIT) | \
5326 (SSL_SERIALIZED_SESSION_CONFIG_CRT << SSL_SERIALIZED_SESSION_CONFIG_CRT_BIT) | \
5327 (SSL_SERIALIZED_SESSION_CONFIG_CLIENT_TICKET << \
5328 SSL_SERIALIZED_SESSION_CONFIG_CLIENT_TICKET_BIT) | \
5329 (SSL_SERIALIZED_SESSION_CONFIG_MFL << SSL_SERIALIZED_SESSION_CONFIG_MFL_BIT) | \
5330 (SSL_SERIALIZED_SESSION_CONFIG_TRUNC_HMAC << \
5331 SSL_SERIALIZED_SESSION_CONFIG_TRUNC_HMAC_BIT) | \
5332 (SSL_SERIALIZED_SESSION_CONFIG_ETM << SSL_SERIALIZED_SESSION_CONFIG_ETM_BIT) | \
David Horstmanneb77b6f2024-02-13 17:53:35 +00005333 (SSL_SERIALIZED_SESSION_CONFIG_TICKET << SSL_SERIALIZED_SESSION_CONFIG_TICKET_BIT) | \
David Horstmannf5a6fa22024-03-01 12:31:35 +00005334 (SSL_SERIALIZED_SESSION_CONFIG_KEEP_PEER_CRT << \
5335 SSL_SERIALIZED_SESSION_CONFIG_KEEP_PEER_CRT_BIT)))
Hanno Becker94ef3b32019-05-16 12:50:45 +01005336
Chien Wongb6d57932024-02-07 21:48:12 +08005337static const unsigned char ssl_serialized_session_header[] = {
Hanno Becker94ef3b32019-05-16 12:50:45 +01005338 MBEDTLS_VERSION_MAJOR,
5339 MBEDTLS_VERSION_MINOR,
5340 MBEDTLS_VERSION_PATCH,
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005341 MBEDTLS_BYTE_1(SSL_SERIALIZED_SESSION_CONFIG_BITFLAG),
5342 MBEDTLS_BYTE_0(SSL_SERIALIZED_SESSION_CONFIG_BITFLAG),
Hanno Beckerf8787072019-05-16 12:41:07 +01005343};
Hanno Beckera835da52019-05-16 12:39:07 +01005344
5345/*
Manuel Pégourié-Gonnarda3e7c652019-05-16 10:08:35 +02005346 * Serialize a session in the following format:
Manuel Pégourié-Gonnard35eb8022019-05-16 11:11:08 +02005347 * (in the presentation language of TLS, RFC 8446 section 3)
Manuel Pégourié-Gonnarda3e7c652019-05-16 10:08:35 +02005348 *
Hanno Becker50b59662019-05-28 14:30:45 +01005349 * opaque mbedtls_version[3]; // major, minor, patch
5350 * opaque session_format[2]; // version-specific 16-bit field determining
5351 * // the format of the remaining
5352 * // serialized data.
Hanno Beckerdc28b6c2019-05-29 11:08:00 +01005353 *
5354 * Note: When updating the format, remember to keep
5355 * these version+format bytes.
5356 *
Hanno Beckerbe34e8e2019-06-04 09:43:16 +01005357 * // In this version, `session_format` determines
5358 * // the setting of those compile-time
5359 * // configuration options which influence
Hanno Becker50b59662019-05-28 14:30:45 +01005360 * // the structure of mbedtls_ssl_session.
David Horstmann363db772024-03-01 12:11:24 +00005361 * #if defined(MBEDTLS_HAVE_TIME)
5362 * uint64 start_time;
5363 * #endif
5364 * uint8 ciphersuite[2]; // defined by the standard
5365 * uint8 compression; // 0 or 1
5366 * uint8 session_id_len; // at most 32
5367 * opaque session_id[32];
5368 * opaque master[48]; // fixed length in the standard
5369 * uint32 verify_result;
David Horstmannfc8cacf2024-03-04 16:37:02 +00005370 * #if defined(MBEDTLS_X509_CRT_PARSE_C)
David Horstmann363db772024-03-01 12:11:24 +00005371 * #if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
5372 * opaque peer_cert<0..2^24-1>; // length 0 means no peer cert
5373 * #else
5374 * uint8 peer_cert_digest_type;
5375 * opaque peer_cert_digest<0..2^8-1>
5376 * #endif
David Horstmannfc8cacf2024-03-04 16:37:02 +00005377 * #endif
5378 * #if defined(MBEDTLS_SSL_SESSION_TICKETS) && defined(MBEDTLS_SSL_CLI_C)
David Horstmann363db772024-03-01 12:11:24 +00005379 * opaque ticket<0..2^24-1>; // length 0 means no ticket
5380 * uint32 ticket_lifetime;
5381 * #endif
5382 * #if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
5383 * uint8 mfl_code; // up to 255 according to standard
5384 * #endif
5385 * #if defined(MBEDTLS_SSL_TRUNCATED_HMAC)
5386 * uint8 trunc_hmac; // 0 or 1
5387 * #endif
5388 * #if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
5389 * uint8 encrypt_then_mac; // 0 or 1
5390 * #endif
Manuel Pégourié-Gonnarda3e7c652019-05-16 10:08:35 +02005391 *
Manuel Pégourié-Gonnardf743c032019-05-24 12:06:29 +02005392 * The order is the same as in the definition of the structure, except
5393 * verify_result is put before peer_cert so that all mandatory fields come
5394 * together in one block.
Manuel Pégourié-Gonnarda3e7c652019-05-16 10:08:35 +02005395 */
Manuel Pégourié-Gonnardd904d662022-06-17 10:24:00 +02005396MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005397static int ssl_session_save(const mbedtls_ssl_session *session,
5398 unsigned char omit_header,
5399 unsigned char *buf,
5400 size_t buf_len,
5401 size_t *olen)
Manuel Pégourié-Gonnarda3e7c652019-05-16 10:08:35 +02005402{
5403 unsigned char *p = buf;
Manuel Pégourié-Gonnard26f982f2019-05-21 11:01:32 +02005404 size_t used = 0;
Manuel Pégourié-Gonnardf743c032019-05-24 12:06:29 +02005405#if defined(MBEDTLS_HAVE_TIME)
5406 uint64_t start;
5407#endif
Manuel Pégourié-Gonnarda3e7c652019-05-16 10:08:35 +02005408#if defined(MBEDTLS_X509_CRT_PARSE_C)
5409#if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
5410 size_t cert_len;
Manuel Pégourié-Gonnarda3e7c652019-05-16 10:08:35 +02005411#endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
5412#endif /* MBEDTLS_X509_CRT_PARSE_C */
5413
Manuel Pégourié-Gonnarda3e7c652019-05-16 10:08:35 +02005414
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005415 if (!omit_header) {
Manuel Pégourié-Gonnard45ac1f02019-07-23 16:52:45 +02005416 /*
5417 * Add version identifier
5418 */
5419
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005420 used += sizeof(ssl_serialized_session_header);
Manuel Pégourié-Gonnard45ac1f02019-07-23 16:52:45 +02005421
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005422 if (used <= buf_len) {
5423 memcpy(p, ssl_serialized_session_header,
5424 sizeof(ssl_serialized_session_header));
5425 p += sizeof(ssl_serialized_session_header);
Manuel Pégourié-Gonnard45ac1f02019-07-23 16:52:45 +02005426 }
Hanno Beckera835da52019-05-16 12:39:07 +01005427 }
5428
5429 /*
Manuel Pégourié-Gonnardf743c032019-05-24 12:06:29 +02005430 * Time
5431 */
5432#if defined(MBEDTLS_HAVE_TIME)
5433 used += 8;
5434
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005435 if (used <= buf_len) {
Manuel Pégourié-Gonnardf743c032019-05-24 12:06:29 +02005436 start = (uint64_t) session->start;
5437
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005438 MBEDTLS_PUT_UINT64_BE(start, p, 0);
Joe Subbiani2f98d792021-08-20 11:44:44 +01005439 p += 8;
Manuel Pégourié-Gonnardf743c032019-05-24 12:06:29 +02005440 }
5441#endif /* MBEDTLS_HAVE_TIME */
5442
5443 /*
5444 * Basic mandatory fields
5445 */
5446 used += 2 /* ciphersuite */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005447 + 1 /* compression */
5448 + 1 /* id_len */
5449 + sizeof(session->id)
5450 + sizeof(session->master)
5451 + 4; /* verify_result */
Manuel Pégourié-Gonnardf743c032019-05-24 12:06:29 +02005452
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005453 if (used <= buf_len) {
5454 MBEDTLS_PUT_UINT16_BE(session->ciphersuite, p, 0);
Joe Subbiani2f98d792021-08-20 11:44:44 +01005455 p += 2;
Manuel Pégourié-Gonnardf743c032019-05-24 12:06:29 +02005456
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005457 *p++ = MBEDTLS_BYTE_0(session->compression);
Manuel Pégourié-Gonnardf743c032019-05-24 12:06:29 +02005458
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005459 *p++ = MBEDTLS_BYTE_0(session->id_len);
5460 memcpy(p, session->id, 32);
Manuel Pégourié-Gonnardf743c032019-05-24 12:06:29 +02005461 p += 32;
5462
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005463 memcpy(p, session->master, 48);
Manuel Pégourié-Gonnardf743c032019-05-24 12:06:29 +02005464 p += 48;
5465
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005466 MBEDTLS_PUT_UINT32_BE(session->verify_result, p, 0);
Joe Subbiani2f98d792021-08-20 11:44:44 +01005467 p += 4;
Manuel Pégourié-Gonnard26f982f2019-05-21 11:01:32 +02005468 }
Manuel Pégourié-Gonnarda3e7c652019-05-16 10:08:35 +02005469
Manuel Pégourié-Gonnard35eb8022019-05-16 11:11:08 +02005470 /*
Manuel Pégourié-Gonnardf743c032019-05-24 12:06:29 +02005471 * Peer's end-entity certificate
Manuel Pégourié-Gonnard35eb8022019-05-16 11:11:08 +02005472 */
Manuel Pégourié-Gonnarda3e7c652019-05-16 10:08:35 +02005473#if defined(MBEDTLS_X509_CRT_PARSE_C)
5474#if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005475 if (session->peer_cert == NULL) {
Manuel Pégourié-Gonnarda3e7c652019-05-16 10:08:35 +02005476 cert_len = 0;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005477 } else {
Manuel Pégourié-Gonnarda3e7c652019-05-16 10:08:35 +02005478 cert_len = session->peer_cert->raw.len;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005479 }
Manuel Pégourié-Gonnarda3e7c652019-05-16 10:08:35 +02005480
Manuel Pégourié-Gonnard26f982f2019-05-21 11:01:32 +02005481 used += 3 + cert_len;
Manuel Pégourié-Gonnarda3e7c652019-05-16 10:08:35 +02005482
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005483 if (used <= buf_len) {
5484 *p++ = MBEDTLS_BYTE_2(cert_len);
5485 *p++ = MBEDTLS_BYTE_1(cert_len);
5486 *p++ = MBEDTLS_BYTE_0(cert_len);
Manuel Pégourié-Gonnarda3e7c652019-05-16 10:08:35 +02005487
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005488 if (session->peer_cert != NULL) {
5489 memcpy(p, session->peer_cert->raw.p, cert_len);
Manuel Pégourié-Gonnard26f982f2019-05-21 11:01:32 +02005490 p += cert_len;
5491 }
5492 }
Manuel Pégourié-Gonnarda3e7c652019-05-16 10:08:35 +02005493#else /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005494 if (session->peer_cert_digest != NULL) {
Manuel Pégourié-Gonnardf743c032019-05-24 12:06:29 +02005495 used += 1 /* type */ + 1 /* length */ + session->peer_cert_digest_len;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005496 if (used <= buf_len) {
Manuel Pégourié-Gonnardf743c032019-05-24 12:06:29 +02005497 *p++ = (unsigned char) session->peer_cert_digest_type;
5498 *p++ = (unsigned char) session->peer_cert_digest_len;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005499 memcpy(p, session->peer_cert_digest,
5500 session->peer_cert_digest_len);
Manuel Pégourié-Gonnardf743c032019-05-24 12:06:29 +02005501 p += session->peer_cert_digest_len;
5502 }
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005503 } else {
Manuel Pégourié-Gonnardf743c032019-05-24 12:06:29 +02005504 used += 2;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005505 if (used <= buf_len) {
Manuel Pégourié-Gonnardf743c032019-05-24 12:06:29 +02005506 *p++ = (unsigned char) MBEDTLS_MD_NONE;
5507 *p++ = 0;
5508 }
Manuel Pégourié-Gonnard26f982f2019-05-21 11:01:32 +02005509 }
Manuel Pégourié-Gonnarda3e7c652019-05-16 10:08:35 +02005510#endif /* !MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
5511#endif /* MBEDTLS_X509_CRT_PARSE_C */
5512
Manuel Pégourié-Gonnard35eb8022019-05-16 11:11:08 +02005513 /*
Manuel Pégourié-Gonnardf743c032019-05-24 12:06:29 +02005514 * Session ticket if any, plus associated data
Manuel Pégourié-Gonnard35eb8022019-05-16 11:11:08 +02005515 */
5516#if defined(MBEDTLS_SSL_SESSION_TICKETS) && defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnardf743c032019-05-24 12:06:29 +02005517 used += 3 + session->ticket_len + 4; /* len + ticket + lifetime */
Manuel Pégourié-Gonnard35eb8022019-05-16 11:11:08 +02005518
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005519 if (used <= buf_len) {
5520 *p++ = MBEDTLS_BYTE_2(session->ticket_len);
5521 *p++ = MBEDTLS_BYTE_1(session->ticket_len);
5522 *p++ = MBEDTLS_BYTE_0(session->ticket_len);
Manuel Pégourié-Gonnard26f982f2019-05-21 11:01:32 +02005523
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005524 if (session->ticket != NULL) {
5525 memcpy(p, session->ticket, session->ticket_len);
Manuel Pégourié-Gonnard26f982f2019-05-21 11:01:32 +02005526 p += session->ticket_len;
5527 }
Manuel Pégourié-Gonnardf743c032019-05-24 12:06:29 +02005528
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005529 MBEDTLS_PUT_UINT32_BE(session->ticket_lifetime, p, 0);
Joe Subbiani2f98d792021-08-20 11:44:44 +01005530 p += 4;
Manuel Pégourié-Gonnard35eb8022019-05-16 11:11:08 +02005531 }
5532#endif /* MBEDTLS_SSL_SESSION_TICKETS && MBEDTLS_SSL_CLI_C */
5533
Manuel Pégourié-Gonnardf743c032019-05-24 12:06:29 +02005534 /*
5535 * Misc extension-related info
5536 */
5537#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
5538 used += 1;
5539
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005540 if (used <= buf_len) {
Manuel Pégourié-Gonnardf743c032019-05-24 12:06:29 +02005541 *p++ = session->mfl_code;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005542 }
Manuel Pégourié-Gonnardf743c032019-05-24 12:06:29 +02005543#endif
5544
5545#if defined(MBEDTLS_SSL_TRUNCATED_HMAC)
5546 used += 1;
5547
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005548 if (used <= buf_len) {
5549 *p++ = (unsigned char) ((session->trunc_hmac) & 0xFF);
5550 }
Manuel Pégourié-Gonnardf743c032019-05-24 12:06:29 +02005551#endif
5552
5553#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
5554 used += 1;
5555
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005556 if (used <= buf_len) {
5557 *p++ = MBEDTLS_BYTE_0(session->encrypt_then_mac);
5558 }
Manuel Pégourié-Gonnardf743c032019-05-24 12:06:29 +02005559#endif
5560
Manuel Pégourié-Gonnard35eb8022019-05-16 11:11:08 +02005561 /* Done */
Manuel Pégourié-Gonnard26f982f2019-05-21 11:01:32 +02005562 *olen = used;
5563
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005564 if (used > buf_len) {
5565 return MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL;
5566 }
Manuel Pégourié-Gonnarda3e7c652019-05-16 10:08:35 +02005567
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005568 return 0;
Manuel Pégourié-Gonnarda3e7c652019-05-16 10:08:35 +02005569}
5570
5571/*
Manuel Pégourié-Gonnard45ac1f02019-07-23 16:52:45 +02005572 * Public wrapper for ssl_session_save()
5573 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005574int mbedtls_ssl_session_save(const mbedtls_ssl_session *session,
5575 unsigned char *buf,
5576 size_t buf_len,
5577 size_t *olen)
Manuel Pégourié-Gonnard45ac1f02019-07-23 16:52:45 +02005578{
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005579 return ssl_session_save(session, 0, buf, buf_len, olen);
Manuel Pégourié-Gonnard45ac1f02019-07-23 16:52:45 +02005580}
5581
5582/*
Manuel Pégourié-Gonnardb9dfc9f2019-07-12 10:50:19 +02005583 * Deserialize session, see mbedtls_ssl_session_save() for format.
Manuel Pégourié-Gonnarda3d831b2019-05-23 12:28:45 +02005584 *
5585 * This internal version is wrapped by a public function that cleans up in
Manuel Pégourié-Gonnard45ac1f02019-07-23 16:52:45 +02005586 * case of error, and has an extra option omit_header.
Manuel Pégourié-Gonnarda3e7c652019-05-16 10:08:35 +02005587 */
Manuel Pégourié-Gonnardd904d662022-06-17 10:24:00 +02005588MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005589static int ssl_session_load(mbedtls_ssl_session *session,
5590 unsigned char omit_header,
5591 const unsigned char *buf,
5592 size_t len)
Manuel Pégourié-Gonnarda3e7c652019-05-16 10:08:35 +02005593{
5594 const unsigned char *p = buf;
5595 const unsigned char * const end = buf + len;
Manuel Pégourié-Gonnardf743c032019-05-24 12:06:29 +02005596#if defined(MBEDTLS_HAVE_TIME)
5597 uint64_t start;
5598#endif
Manuel Pégourié-Gonnarda3e7c652019-05-16 10:08:35 +02005599#if defined(MBEDTLS_X509_CRT_PARSE_C)
5600#if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
5601 size_t cert_len;
Manuel Pégourié-Gonnarda3e7c652019-05-16 10:08:35 +02005602#endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
5603#endif /* MBEDTLS_X509_CRT_PARSE_C */
5604
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005605 if (!omit_header) {
Manuel Pégourié-Gonnard45ac1f02019-07-23 16:52:45 +02005606 /*
5607 * Check version identifier
5608 */
5609
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005610 if ((size_t) (end - p) < sizeof(ssl_serialized_session_header)) {
5611 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
Manuel Pégourié-Gonnard45ac1f02019-07-23 16:52:45 +02005612 }
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005613
5614 if (memcmp(p, ssl_serialized_session_header,
5615 sizeof(ssl_serialized_session_header)) != 0) {
5616 return MBEDTLS_ERR_SSL_VERSION_MISMATCH;
5617 }
5618 p += sizeof(ssl_serialized_session_header);
Hanno Beckera835da52019-05-16 12:39:07 +01005619 }
Hanno Beckera835da52019-05-16 12:39:07 +01005620
5621 /*
Manuel Pégourié-Gonnardf743c032019-05-24 12:06:29 +02005622 * Time
Manuel Pégourié-Gonnard35eb8022019-05-16 11:11:08 +02005623 */
Manuel Pégourié-Gonnardf743c032019-05-24 12:06:29 +02005624#if defined(MBEDTLS_HAVE_TIME)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005625 if (8 > (size_t) (end - p)) {
5626 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
5627 }
Manuel Pégourié-Gonnarda3e7c652019-05-16 10:08:35 +02005628
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005629 start = ((uint64_t) p[0] << 56) |
5630 ((uint64_t) p[1] << 48) |
5631 ((uint64_t) p[2] << 40) |
5632 ((uint64_t) p[3] << 32) |
5633 ((uint64_t) p[4] << 24) |
5634 ((uint64_t) p[5] << 16) |
5635 ((uint64_t) p[6] << 8) |
5636 ((uint64_t) p[7]);
Manuel Pégourié-Gonnardf743c032019-05-24 12:06:29 +02005637 p += 8;
Manuel Pégourié-Gonnarda3e7c652019-05-16 10:08:35 +02005638
Manuel Pégourié-Gonnardf743c032019-05-24 12:06:29 +02005639 session->start = (time_t) start;
5640#endif /* MBEDTLS_HAVE_TIME */
5641
5642 /*
5643 * Basic mandatory fields
5644 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005645 if (2 + 1 + 1 + 32 + 48 + 4 > (size_t) (end - p)) {
5646 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
5647 }
Manuel Pégourié-Gonnardf743c032019-05-24 12:06:29 +02005648
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005649 session->ciphersuite = (p[0] << 8) | p[1];
Manuel Pégourié-Gonnardf743c032019-05-24 12:06:29 +02005650 p += 2;
5651
5652 session->compression = *p++;
5653
5654 session->id_len = *p++;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005655 memcpy(session->id, p, 32);
Manuel Pégourié-Gonnardf743c032019-05-24 12:06:29 +02005656 p += 32;
5657
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005658 memcpy(session->master, p, 48);
Manuel Pégourié-Gonnardf743c032019-05-24 12:06:29 +02005659 p += 48;
5660
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005661 session->verify_result = ((uint32_t) p[0] << 24) |
5662 ((uint32_t) p[1] << 16) |
5663 ((uint32_t) p[2] << 8) |
5664 ((uint32_t) p[3]);
Manuel Pégourié-Gonnardf743c032019-05-24 12:06:29 +02005665 p += 4;
5666
5667 /* Immediately clear invalid pointer values that have been read, in case
5668 * we exit early before we replaced them with valid ones. */
Manuel Pégourié-Gonnarda3e7c652019-05-16 10:08:35 +02005669#if defined(MBEDTLS_X509_CRT_PARSE_C)
5670#if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
5671 session->peer_cert = NULL;
5672#else
5673 session->peer_cert_digest = NULL;
5674#endif /* !MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
5675#endif /* MBEDTLS_X509_CRT_PARSE_C */
5676#if defined(MBEDTLS_SSL_SESSION_TICKETS) && defined(MBEDTLS_SSL_CLI_C)
5677 session->ticket = NULL;
5678#endif /* MBEDTLS_SSL_SESSION_TICKETS && MBEDTLS_SSL_CLI_C */
5679
Manuel Pégourié-Gonnard35eb8022019-05-16 11:11:08 +02005680 /*
5681 * Peer certificate
5682 */
Manuel Pégourié-Gonnarda3e7c652019-05-16 10:08:35 +02005683#if defined(MBEDTLS_X509_CRT_PARSE_C)
5684#if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
5685 /* Deserialize CRT from the end of the ticket. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005686 if (3 > (size_t) (end - p)) {
5687 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
5688 }
Manuel Pégourié-Gonnarda3e7c652019-05-16 10:08:35 +02005689
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005690 cert_len = (p[0] << 16) | (p[1] << 8) | p[2];
Manuel Pégourié-Gonnarda3e7c652019-05-16 10:08:35 +02005691 p += 3;
5692
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005693 if (cert_len != 0) {
Janos Follath865b3eb2019-12-16 11:46:15 +00005694 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnarda3e7c652019-05-16 10:08:35 +02005695
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005696 if (cert_len > (size_t) (end - p)) {
5697 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
5698 }
Manuel Pégourié-Gonnarda3e7c652019-05-16 10:08:35 +02005699
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005700 session->peer_cert = mbedtls_calloc(1, sizeof(mbedtls_x509_crt));
Manuel Pégourié-Gonnarda3e7c652019-05-16 10:08:35 +02005701
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005702 if (session->peer_cert == NULL) {
5703 return MBEDTLS_ERR_SSL_ALLOC_FAILED;
5704 }
Manuel Pégourié-Gonnarda3e7c652019-05-16 10:08:35 +02005705
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005706 mbedtls_x509_crt_init(session->peer_cert);
Manuel Pégourié-Gonnarda3e7c652019-05-16 10:08:35 +02005707
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005708 if ((ret = mbedtls_x509_crt_parse_der(session->peer_cert,
5709 p, cert_len)) != 0) {
5710 mbedtls_x509_crt_free(session->peer_cert);
5711 mbedtls_free(session->peer_cert);
Manuel Pégourié-Gonnarda3e7c652019-05-16 10:08:35 +02005712 session->peer_cert = NULL;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005713 return ret;
Manuel Pégourié-Gonnarda3e7c652019-05-16 10:08:35 +02005714 }
5715
5716 p += cert_len;
5717 }
5718#else /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
5719 /* Deserialize CRT digest from the end of the ticket. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005720 if (2 > (size_t) (end - p)) {
5721 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
5722 }
Manuel Pégourié-Gonnarda3e7c652019-05-16 10:08:35 +02005723
Manuel Pégourié-Gonnardf743c032019-05-24 12:06:29 +02005724 session->peer_cert_digest_type = (mbedtls_md_type_t) *p++;
5725 session->peer_cert_digest_len = (size_t) *p++;
Manuel Pégourié-Gonnarda3e7c652019-05-16 10:08:35 +02005726
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005727 if (session->peer_cert_digest_len != 0) {
Manuel Pégourié-Gonnardf743c032019-05-24 12:06:29 +02005728 const mbedtls_md_info_t *md_info =
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005729 mbedtls_md_info_from_type(session->peer_cert_digest_type);
5730 if (md_info == NULL) {
5731 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
5732 }
5733 if (session->peer_cert_digest_len != mbedtls_md_get_size(md_info)) {
5734 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
5735 }
Manuel Pégourié-Gonnarda3e7c652019-05-16 10:08:35 +02005736
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005737 if (session->peer_cert_digest_len > (size_t) (end - p)) {
5738 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
5739 }
Manuel Pégourié-Gonnardf743c032019-05-24 12:06:29 +02005740
5741 session->peer_cert_digest =
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005742 mbedtls_calloc(1, session->peer_cert_digest_len);
5743 if (session->peer_cert_digest == NULL) {
5744 return MBEDTLS_ERR_SSL_ALLOC_FAILED;
5745 }
Manuel Pégourié-Gonnarda3e7c652019-05-16 10:08:35 +02005746
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005747 memcpy(session->peer_cert_digest, p,
5748 session->peer_cert_digest_len);
Manuel Pégourié-Gonnardf743c032019-05-24 12:06:29 +02005749 p += session->peer_cert_digest_len;
Manuel Pégourié-Gonnarda3e7c652019-05-16 10:08:35 +02005750 }
5751#endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
5752#endif /* MBEDTLS_X509_CRT_PARSE_C */
5753
Manuel Pégourié-Gonnard35eb8022019-05-16 11:11:08 +02005754 /*
Manuel Pégourié-Gonnardf743c032019-05-24 12:06:29 +02005755 * Session ticket and associated data
Manuel Pégourié-Gonnard35eb8022019-05-16 11:11:08 +02005756 */
5757#if defined(MBEDTLS_SSL_SESSION_TICKETS) && defined(MBEDTLS_SSL_CLI_C)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005758 if (3 > (size_t) (end - p)) {
5759 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
5760 }
Manuel Pégourié-Gonnard35eb8022019-05-16 11:11:08 +02005761
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005762 session->ticket_len = (p[0] << 16) | (p[1] << 8) | p[2];
Manuel Pégourié-Gonnard35eb8022019-05-16 11:11:08 +02005763 p += 3;
5764
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005765 if (session->ticket_len != 0) {
5766 if (session->ticket_len > (size_t) (end - p)) {
5767 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
5768 }
Manuel Pégourié-Gonnard35eb8022019-05-16 11:11:08 +02005769
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005770 session->ticket = mbedtls_calloc(1, session->ticket_len);
5771 if (session->ticket == NULL) {
5772 return MBEDTLS_ERR_SSL_ALLOC_FAILED;
5773 }
Manuel Pégourié-Gonnard35eb8022019-05-16 11:11:08 +02005774
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005775 memcpy(session->ticket, p, session->ticket_len);
Manuel Pégourié-Gonnard35eb8022019-05-16 11:11:08 +02005776 p += session->ticket_len;
5777 }
Manuel Pégourié-Gonnardf743c032019-05-24 12:06:29 +02005778
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005779 if (4 > (size_t) (end - p)) {
5780 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
5781 }
Manuel Pégourié-Gonnardf743c032019-05-24 12:06:29 +02005782
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005783 session->ticket_lifetime = ((uint32_t) p[0] << 24) |
5784 ((uint32_t) p[1] << 16) |
5785 ((uint32_t) p[2] << 8) |
5786 ((uint32_t) p[3]);
Manuel Pégourié-Gonnardf743c032019-05-24 12:06:29 +02005787 p += 4;
Manuel Pégourié-Gonnard35eb8022019-05-16 11:11:08 +02005788#endif /* MBEDTLS_SSL_SESSION_TICKETS && MBEDTLS_SSL_CLI_C */
5789
Manuel Pégourié-Gonnardf743c032019-05-24 12:06:29 +02005790 /*
5791 * Misc extension-related info
5792 */
5793#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005794 if (1 > (size_t) (end - p)) {
5795 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
5796 }
Manuel Pégourié-Gonnardf743c032019-05-24 12:06:29 +02005797
5798 session->mfl_code = *p++;
5799#endif
5800
5801#if defined(MBEDTLS_SSL_TRUNCATED_HMAC)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005802 if (1 > (size_t) (end - p)) {
5803 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
5804 }
Manuel Pégourié-Gonnardf743c032019-05-24 12:06:29 +02005805
5806 session->trunc_hmac = *p++;
5807#endif
5808
5809#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005810 if (1 > (size_t) (end - p)) {
5811 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
5812 }
Manuel Pégourié-Gonnardf743c032019-05-24 12:06:29 +02005813
5814 session->encrypt_then_mac = *p++;
5815#endif
5816
Manuel Pégourié-Gonnard35eb8022019-05-16 11:11:08 +02005817 /* Done, should have consumed entire buffer */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005818 if (p != end) {
5819 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
5820 }
Manuel Pégourié-Gonnarda3e7c652019-05-16 10:08:35 +02005821
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005822 return 0;
Manuel Pégourié-Gonnarda3e7c652019-05-16 10:08:35 +02005823}
5824
5825/*
Manuel Pégourié-Gonnardb9dfc9f2019-07-12 10:50:19 +02005826 * Deserialize session: public wrapper for error cleaning
Manuel Pégourié-Gonnarda3d831b2019-05-23 12:28:45 +02005827 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005828int mbedtls_ssl_session_load(mbedtls_ssl_session *session,
5829 const unsigned char *buf,
5830 size_t len)
Manuel Pégourié-Gonnarda3d831b2019-05-23 12:28:45 +02005831{
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005832 int ret = ssl_session_load(session, 0, buf, len);
Manuel Pégourié-Gonnarda3d831b2019-05-23 12:28:45 +02005833
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005834 if (ret != 0) {
5835 mbedtls_ssl_session_free(session);
5836 }
Manuel Pégourié-Gonnarda3d831b2019-05-23 12:28:45 +02005837
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005838 return ret;
Manuel Pégourié-Gonnarda3d831b2019-05-23 12:28:45 +02005839}
5840
5841/*
Paul Bakker1961b702013-01-25 14:49:24 +01005842 * Perform a single step of the SSL handshake
Paul Bakker5121ce52009-01-03 21:22:43 +00005843 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005844int mbedtls_ssl_handshake_step(mbedtls_ssl_context *ssl)
Paul Bakker5121ce52009-01-03 21:22:43 +00005845{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005846 int ret = MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
Paul Bakker5121ce52009-01-03 21:22:43 +00005847
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005848 if (ssl == NULL || ssl->conf == NULL) {
5849 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
5850 }
Manuel Pégourié-Gonnardf81ee2e2015-09-01 17:43:40 +02005851
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005852#if defined(MBEDTLS_SSL_CLI_C)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005853 if (ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT) {
5854 ret = mbedtls_ssl_handshake_client_step(ssl);
5855 }
Paul Bakker5121ce52009-01-03 21:22:43 +00005856#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005857#if defined(MBEDTLS_SSL_SRV_C)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005858 if (ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER) {
5859 ret = mbedtls_ssl_handshake_server_step(ssl);
5860 }
Paul Bakker5121ce52009-01-03 21:22:43 +00005861#endif
5862
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005863 return ret;
Paul Bakker1961b702013-01-25 14:49:24 +01005864}
5865
5866/*
5867 * Perform the SSL handshake
5868 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005869int mbedtls_ssl_handshake(mbedtls_ssl_context *ssl)
Paul Bakker1961b702013-01-25 14:49:24 +01005870{
5871 int ret = 0;
5872
Hanno Beckera817ea42020-10-20 15:20:23 +01005873 /* Sanity checks */
5874
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005875 if (ssl == NULL || ssl->conf == NULL) {
5876 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
5877 }
Manuel Pégourié-Gonnardf81ee2e2015-09-01 17:43:40 +02005878
Hanno Beckera817ea42020-10-20 15:20:23 +01005879#if defined(MBEDTLS_SSL_PROTO_DTLS)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005880 if (ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
5881 (ssl->f_set_timer == NULL || ssl->f_get_timer == NULL)) {
5882 MBEDTLS_SSL_DEBUG_MSG(1, ("You must use "
5883 "mbedtls_ssl_set_timer_cb() for DTLS"));
5884 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
Hanno Beckera817ea42020-10-20 15:20:23 +01005885 }
5886#endif /* MBEDTLS_SSL_PROTO_DTLS */
5887
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005888 MBEDTLS_SSL_DEBUG_MSG(2, ("=> handshake"));
Paul Bakker1961b702013-01-25 14:49:24 +01005889
Hanno Beckera817ea42020-10-20 15:20:23 +01005890 /* Main handshake loop */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005891 while (ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER) {
5892 ret = mbedtls_ssl_handshake_step(ssl);
Paul Bakker1961b702013-01-25 14:49:24 +01005893
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005894 if (ret != 0) {
Paul Bakker1961b702013-01-25 14:49:24 +01005895 break;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005896 }
Paul Bakker1961b702013-01-25 14:49:24 +01005897 }
5898
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005899 MBEDTLS_SSL_DEBUG_MSG(2, ("<= handshake"));
Paul Bakker5121ce52009-01-03 21:22:43 +00005900
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005901 return ret;
Paul Bakker5121ce52009-01-03 21:22:43 +00005902}
5903
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005904#if defined(MBEDTLS_SSL_RENEGOTIATION)
5905#if defined(MBEDTLS_SSL_SRV_C)
Paul Bakker5121ce52009-01-03 21:22:43 +00005906/*
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01005907 * Write HelloRequest to request renegotiation on server
Paul Bakker48916f92012-09-16 19:57:18 +00005908 */
Manuel Pégourié-Gonnardd904d662022-06-17 10:24:00 +02005909MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005910static int ssl_write_hello_request(mbedtls_ssl_context *ssl)
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01005911{
Janos Follath865b3eb2019-12-16 11:46:15 +00005912 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01005913
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005914 MBEDTLS_SSL_DEBUG_MSG(2, ("=> write hello request"));
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01005915
5916 ssl->out_msglen = 4;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005917 ssl->out_msgtype = MBEDTLS_SSL_MSG_HANDSHAKE;
5918 ssl->out_msg[0] = MBEDTLS_SSL_HS_HELLO_REQUEST;
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01005919
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005920 if ((ret = mbedtls_ssl_write_handshake_msg(ssl)) != 0) {
5921 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ssl_write_handshake_msg", ret);
5922 return ret;
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01005923 }
5924
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005925 MBEDTLS_SSL_DEBUG_MSG(2, ("<= write hello request"));
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01005926
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005927 return 0;
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01005928}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005929#endif /* MBEDTLS_SSL_SRV_C */
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01005930
5931/*
5932 * Actually renegotiate current connection, triggered by either:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005933 * - any side: calling mbedtls_ssl_renegotiate(),
5934 * - client: receiving a HelloRequest during mbedtls_ssl_read(),
5935 * - server: receiving any handshake message on server during mbedtls_ssl_read() after
Manuel Pégourié-Gonnard55e4ff22014-08-19 11:16:35 +02005936 * the initial handshake is completed.
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01005937 * If the handshake doesn't complete due to waiting for I/O, it will continue
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005938 * during the next calls to mbedtls_ssl_renegotiate() or mbedtls_ssl_read() respectively.
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01005939 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005940int mbedtls_ssl_start_renegotiation(mbedtls_ssl_context *ssl)
Paul Bakker48916f92012-09-16 19:57:18 +00005941{
Janos Follath865b3eb2019-12-16 11:46:15 +00005942 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker48916f92012-09-16 19:57:18 +00005943
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005944 MBEDTLS_SSL_DEBUG_MSG(2, ("=> renegotiate"));
Paul Bakker48916f92012-09-16 19:57:18 +00005945
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005946 if ((ret = ssl_handshake_init(ssl)) != 0) {
5947 return ret;
5948 }
Paul Bakker48916f92012-09-16 19:57:18 +00005949
Manuel Pégourié-Gonnard0557bd52014-08-19 19:18:39 +02005950 /* RFC 6347 4.2.2: "[...] the HelloRequest will have message_seq = 0 and
5951 * the ServerHello will have message_seq = 1" */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005952#if defined(MBEDTLS_SSL_PROTO_DTLS)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005953 if (ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
5954 ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_PENDING) {
5955 if (ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER) {
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02005956 ssl->handshake->out_msg_seq = 1;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005957 } else {
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02005958 ssl->handshake->in_msg_seq = 1;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005959 }
Manuel Pégourié-Gonnard0557bd52014-08-19 19:18:39 +02005960 }
5961#endif
5962
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005963 ssl->state = MBEDTLS_SSL_HELLO_REQUEST;
5964 ssl->renego_status = MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS;
Paul Bakker48916f92012-09-16 19:57:18 +00005965
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005966 if ((ret = mbedtls_ssl_handshake(ssl)) != 0) {
5967 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ssl_handshake", ret);
5968 return ret;
Paul Bakker48916f92012-09-16 19:57:18 +00005969 }
5970
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005971 MBEDTLS_SSL_DEBUG_MSG(2, ("<= renegotiate"));
Paul Bakker48916f92012-09-16 19:57:18 +00005972
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005973 return 0;
Paul Bakker48916f92012-09-16 19:57:18 +00005974}
5975
5976/*
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01005977 * Renegotiate current connection on client,
5978 * or request renegotiation on server
5979 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005980int mbedtls_ssl_renegotiate(mbedtls_ssl_context *ssl)
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01005981{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005982 int ret = MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01005983
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005984 if (ssl == NULL || ssl->conf == NULL) {
5985 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
5986 }
Manuel Pégourié-Gonnardf81ee2e2015-09-01 17:43:40 +02005987
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005988#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01005989 /* On server, just send the request */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005990 if (ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER) {
5991 if (ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER) {
5992 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
5993 }
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01005994
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005995 ssl->renego_status = MBEDTLS_SSL_RENEGOTIATION_PENDING;
Manuel Pégourié-Gonnardf07f4212014-08-15 19:04:47 +02005996
5997 /* Did we already try/start sending HelloRequest? */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005998 if (ssl->out_left != 0) {
5999 return mbedtls_ssl_flush_output(ssl);
6000 }
Manuel Pégourié-Gonnardf07f4212014-08-15 19:04:47 +02006001
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01006002 return ssl_write_hello_request(ssl);
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01006003 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006004#endif /* MBEDTLS_SSL_SRV_C */
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01006005
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006006#if defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01006007 /*
6008 * On client, either start the renegotiation process or,
6009 * if already in progress, continue the handshake
6010 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01006011 if (ssl->renego_status != MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS) {
6012 if (ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER) {
6013 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01006014 }
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01006015
6016 if ((ret = mbedtls_ssl_start_renegotiation(ssl)) != 0) {
6017 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ssl_start_renegotiation", ret);
6018 return ret;
6019 }
6020 } else {
6021 if ((ret = mbedtls_ssl_handshake(ssl)) != 0) {
6022 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ssl_handshake", ret);
6023 return ret;
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01006024 }
6025 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006026#endif /* MBEDTLS_SSL_CLI_C */
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01006027
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01006028 return ret;
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01006029}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006030#endif /* MBEDTLS_SSL_RENEGOTIATION */
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01006031
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006032#if defined(MBEDTLS_X509_CRT_PARSE_C)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01006033static void ssl_key_cert_free(mbedtls_ssl_key_cert *key_cert)
Manuel Pégourié-Gonnard705fcca2013-09-23 20:04:20 +02006034{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006035 mbedtls_ssl_key_cert *cur = key_cert, *next;
Manuel Pégourié-Gonnard705fcca2013-09-23 20:04:20 +02006036
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01006037 while (cur != NULL) {
Manuel Pégourié-Gonnard705fcca2013-09-23 20:04:20 +02006038 next = cur->next;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01006039 mbedtls_free(cur);
Manuel Pégourié-Gonnard705fcca2013-09-23 20:04:20 +02006040 cur = next;
6041 }
6042}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006043#endif /* MBEDTLS_X509_CRT_PARSE_C */
Manuel Pégourié-Gonnard705fcca2013-09-23 20:04:20 +02006044
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01006045void mbedtls_ssl_handshake_free(mbedtls_ssl_context *ssl)
Paul Bakker48916f92012-09-16 19:57:18 +00006046{
Gilles Peskine9b562d52018-04-25 20:32:43 +02006047 mbedtls_ssl_handshake_params *handshake = ssl->handshake;
6048
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01006049 if (handshake == NULL) {
Paul Bakkeraccaffe2014-06-26 13:37:14 +02006050 return;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01006051 }
Paul Bakkeraccaffe2014-06-26 13:37:14 +02006052
Gilles Peskinedf13d5c2018-04-25 20:39:48 +02006053#if defined(MBEDTLS_SSL_ASYNC_PRIVATE)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01006054 if (ssl->conf->f_async_cancel != NULL && handshake->async_in_progress != 0) {
6055 ssl->conf->f_async_cancel(ssl);
Gilles Peskinedf13d5c2018-04-25 20:39:48 +02006056 handshake->async_in_progress = 0;
6057 }
6058#endif /* MBEDTLS_SSL_ASYNC_PRIVATE */
6059
Manuel Pégourié-Gonnardb9d64e52015-07-06 14:18:56 +02006060#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1) || \
6061 defined(MBEDTLS_SSL_PROTO_TLS1_1)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01006062 mbedtls_md5_free(&handshake->fin_md5);
6063 mbedtls_sha1_free(&handshake->fin_sha1);
Manuel Pégourié-Gonnardb9d64e52015-07-06 14:18:56 +02006064#endif
6065#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
6066#if defined(MBEDTLS_SHA256_C)
Andrzej Kurekeb342242019-01-29 09:14:33 -05006067#if defined(MBEDTLS_USE_PSA_CRYPTO)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01006068 psa_hash_abort(&handshake->fin_sha256_psa);
Andrzej Kurekeb342242019-01-29 09:14:33 -05006069#else
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01006070 mbedtls_sha256_free(&handshake->fin_sha256);
Manuel Pégourié-Gonnardb9d64e52015-07-06 14:18:56 +02006071#endif
Andrzej Kurekeb342242019-01-29 09:14:33 -05006072#endif
Gilles Peskined2d59372021-05-12 22:43:27 +02006073#if defined(MBEDTLS_SHA512_C) && !defined(MBEDTLS_SHA512_NO_SHA384)
Andrzej Kurekeb342242019-01-29 09:14:33 -05006074#if defined(MBEDTLS_USE_PSA_CRYPTO)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01006075 psa_hash_abort(&handshake->fin_sha384_psa);
Andrzej Kurekeb342242019-01-29 09:14:33 -05006076#else
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01006077 mbedtls_sha512_free(&handshake->fin_sha512);
Manuel Pégourié-Gonnardb9d64e52015-07-06 14:18:56 +02006078#endif
Andrzej Kurekeb342242019-01-29 09:14:33 -05006079#endif
Manuel Pégourié-Gonnardb9d64e52015-07-06 14:18:56 +02006080#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
6081
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006082#if defined(MBEDTLS_DHM_C)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01006083 mbedtls_dhm_free(&handshake->dhm_ctx);
Paul Bakker48916f92012-09-16 19:57:18 +00006084#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006085#if defined(MBEDTLS_ECDH_C)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01006086 mbedtls_ecdh_free(&handshake->ecdh_ctx);
Paul Bakker61d113b2013-07-04 11:51:43 +02006087#endif
Manuel Pégourié-Gonnardeef142d2015-09-16 10:05:04 +02006088#if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01006089 mbedtls_ecjpake_free(&handshake->ecjpake_ctx);
Manuel Pégourié-Gonnard77c06462015-09-17 13:59:49 +02006090#if defined(MBEDTLS_SSL_CLI_C)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01006091 mbedtls_free(handshake->ecjpake_cache);
Manuel Pégourié-Gonnard77c06462015-09-17 13:59:49 +02006092 handshake->ecjpake_cache = NULL;
6093 handshake->ecjpake_cache_len = 0;
6094#endif
Manuel Pégourié-Gonnard76cfd3f2015-09-15 12:10:54 +02006095#endif
Paul Bakker61d113b2013-07-04 11:51:43 +02006096
Janos Follath4ae5c292016-02-10 11:27:43 +00006097#if defined(MBEDTLS_ECDH_C) || defined(MBEDTLS_ECDSA_C) || \
6098 defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
Paul Bakker9af723c2014-05-01 13:03:14 +02006099 /* explicit void pointer cast for buggy MS compiler */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01006100 mbedtls_free((void *) handshake->curves);
Manuel Pégourié-Gonnardd09453c2013-09-23 19:11:32 +02006101#endif
6102
Gilles Peskineeccd8882020-03-10 12:19:08 +01006103#if defined(MBEDTLS_KEY_EXCHANGE_SOME_PSK_ENABLED)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01006104 if (handshake->psk != NULL) {
6105 mbedtls_platform_zeroize(handshake->psk, handshake->psk_len);
6106 mbedtls_free(handshake->psk);
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01006107 }
6108#endif
6109
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006110#if defined(MBEDTLS_X509_CRT_PARSE_C) && \
6111 defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
Manuel Pégourié-Gonnard83724542013-09-24 22:30:56 +02006112 /*
6113 * Free only the linked list wrapper, not the keys themselves
6114 * since the belong to the SNI callback
6115 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01006116 if (handshake->sni_key_cert != NULL) {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006117 mbedtls_ssl_key_cert *cur = handshake->sni_key_cert, *next;
Manuel Pégourié-Gonnard83724542013-09-24 22:30:56 +02006118
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01006119 while (cur != NULL) {
Manuel Pégourié-Gonnard83724542013-09-24 22:30:56 +02006120 next = cur->next;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01006121 mbedtls_free(cur);
Manuel Pégourié-Gonnard83724542013-09-24 22:30:56 +02006122 cur = next;
6123 }
6124 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006125#endif /* MBEDTLS_X509_CRT_PARSE_C && MBEDTLS_SSL_SERVER_NAME_INDICATION */
Manuel Pégourié-Gonnard705fcca2013-09-23 20:04:20 +02006126
Gilles Peskineeccd8882020-03-10 12:19:08 +01006127#if defined(MBEDTLS_SSL_ECP_RESTARTABLE_ENABLED)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01006128 mbedtls_x509_crt_restart_free(&handshake->ecrs_ctx);
6129 if (handshake->ecrs_peer_cert != NULL) {
6130 mbedtls_x509_crt_free(handshake->ecrs_peer_cert);
6131 mbedtls_free(handshake->ecrs_peer_cert);
Hanno Becker3dad3112019-02-05 17:19:52 +00006132 }
Manuel Pégourié-Gonnard862cde52017-05-17 11:56:15 +02006133#endif
6134
Hanno Becker75173122019-02-06 16:18:31 +00006135#if defined(MBEDTLS_X509_CRT_PARSE_C) && \
6136 !defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01006137 mbedtls_pk_free(&handshake->peer_pubkey);
Hanno Becker75173122019-02-06 16:18:31 +00006138#endif /* MBEDTLS_X509_CRT_PARSE_C && !MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
6139
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006140#if defined(MBEDTLS_SSL_PROTO_DTLS)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01006141 mbedtls_free(handshake->verify_cookie);
6142 mbedtls_ssl_flight_free(handshake->flight);
6143 mbedtls_ssl_buffering_free(ssl);
Manuel Pégourié-Gonnard74848812014-07-11 02:43:49 +02006144#endif
6145
Hanno Becker4a63ed42019-01-08 11:39:35 +00006146#if defined(MBEDTLS_ECDH_C) && \
6147 defined(MBEDTLS_USE_PSA_CRYPTO)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01006148 psa_destroy_key(handshake->ecdh_psa_privkey);
Hanno Becker4a63ed42019-01-08 11:39:35 +00006149#endif /* MBEDTLS_ECDH_C && MBEDTLS_USE_PSA_CRYPTO */
6150
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01006151 mbedtls_platform_zeroize(handshake,
6152 sizeof(mbedtls_ssl_handshake_params));
Andrzej Kurek0afa2a12020-03-03 10:39:58 -05006153
6154#if defined(MBEDTLS_SSL_VARIABLE_BUFFER_LENGTH)
6155 /* If the buffers are too big - reallocate. Because of the way Mbed TLS
6156 * processes datagrams and the fact that a datagram is allowed to have
6157 * several records in it, it is possible that the I/O buffers are not
6158 * empty at this stage */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01006159 handle_buffer_resizing(ssl, 1, mbedtls_ssl_get_input_buflen(ssl),
6160 mbedtls_ssl_get_output_buflen(ssl));
Andrzej Kurek0afa2a12020-03-03 10:39:58 -05006161#endif
Paul Bakker48916f92012-09-16 19:57:18 +00006162}
6163
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01006164void mbedtls_ssl_session_free(mbedtls_ssl_session *session)
Paul Bakker48916f92012-09-16 19:57:18 +00006165{
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01006166 if (session == NULL) {
Paul Bakkeraccaffe2014-06-26 13:37:14 +02006167 return;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01006168 }
Paul Bakkeraccaffe2014-06-26 13:37:14 +02006169
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006170#if defined(MBEDTLS_X509_CRT_PARSE_C)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01006171 ssl_clear_peer_cert(session);
Paul Bakkered27a042013-04-18 22:46:23 +02006172#endif
Paul Bakker0a597072012-09-25 21:55:46 +00006173
Manuel Pégourié-Gonnardb596abf2015-05-20 10:45:29 +02006174#if defined(MBEDTLS_SSL_SESSION_TICKETS) && defined(MBEDTLS_SSL_CLI_C)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01006175 mbedtls_free(session->ticket);
Paul Bakkera503a632013-08-14 13:48:06 +02006176#endif
Manuel Pégourié-Gonnard75d44012013-08-02 14:44:04 +02006177
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01006178 mbedtls_platform_zeroize(session, sizeof(mbedtls_ssl_session));
Paul Bakker48916f92012-09-16 19:57:18 +00006179}
6180
Manuel Pégourié-Gonnard5c0e3772019-07-23 16:13:17 +02006181#if defined(MBEDTLS_SSL_CONTEXT_SERIALIZATION)
Manuel Pégourié-Gonnard4e9370b2019-07-23 16:31:16 +02006182
6183#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
6184#define SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_CONNECTION_ID 1u
6185#else
6186#define SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_CONNECTION_ID 0u
6187#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
6188
6189#if defined(MBEDTLS_SSL_DTLS_BADMAC_LIMIT)
6190#define SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_BADMAC_LIMIT 1u
6191#else
6192#define SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_BADMAC_LIMIT 0u
6193#endif /* MBEDTLS_SSL_DTLS_BADMAC_LIMIT */
6194
6195#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
6196#define SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_ANTI_REPLAY 1u
6197#else
6198#define SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_ANTI_REPLAY 0u
6199#endif /* MBEDTLS_SSL_DTLS_ANTI_REPLAY */
6200
6201#if defined(MBEDTLS_SSL_ALPN)
6202#define SSL_SERIALIZED_CONTEXT_CONFIG_ALPN 1u
6203#else
6204#define SSL_SERIALIZED_CONTEXT_CONFIG_ALPN 0u
6205#endif /* MBEDTLS_SSL_ALPN */
6206
6207#define SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_CONNECTION_ID_BIT 0
6208#define SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_BADMAC_LIMIT_BIT 1
6209#define SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_ANTI_REPLAY_BIT 2
6210#define SSL_SERIALIZED_CONTEXT_CONFIG_ALPN_BIT 3
6211
6212#define SSL_SERIALIZED_CONTEXT_CONFIG_BITFLAG \
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01006213 ((uint32_t) ( \
6214 (SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_CONNECTION_ID << \
6215 SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_CONNECTION_ID_BIT) | \
6216 (SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_BADMAC_LIMIT << \
6217 SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_BADMAC_LIMIT_BIT) | \
6218 (SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_ANTI_REPLAY << \
6219 SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_ANTI_REPLAY_BIT) | \
6220 (SSL_SERIALIZED_CONTEXT_CONFIG_ALPN << SSL_SERIALIZED_CONTEXT_CONFIG_ALPN_BIT) | \
6221 0u))
Manuel Pégourié-Gonnard4e9370b2019-07-23 16:31:16 +02006222
Chien Wongb6d57932024-02-07 21:48:12 +08006223static const unsigned char ssl_serialized_context_header[] = {
Manuel Pégourié-Gonnard4c90e852019-07-11 10:58:10 +02006224 MBEDTLS_VERSION_MAJOR,
6225 MBEDTLS_VERSION_MINOR,
6226 MBEDTLS_VERSION_PATCH,
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01006227 MBEDTLS_BYTE_1(SSL_SERIALIZED_SESSION_CONFIG_BITFLAG),
6228 MBEDTLS_BYTE_0(SSL_SERIALIZED_SESSION_CONFIG_BITFLAG),
6229 MBEDTLS_BYTE_2(SSL_SERIALIZED_CONTEXT_CONFIG_BITFLAG),
6230 MBEDTLS_BYTE_1(SSL_SERIALIZED_CONTEXT_CONFIG_BITFLAG),
6231 MBEDTLS_BYTE_0(SSL_SERIALIZED_CONTEXT_CONFIG_BITFLAG),
Manuel Pégourié-Gonnard4c90e852019-07-11 10:58:10 +02006232};
6233
Paul Bakker5121ce52009-01-03 21:22:43 +00006234/*
Manuel Pégourié-Gonnardac87e282019-05-28 13:02:16 +02006235 * Serialize a full SSL context
Manuel Pégourié-Gonnard00400c22019-07-10 14:58:45 +02006236 *
6237 * The format of the serialized data is:
6238 * (in the presentation language of TLS, RFC 8446 section 3)
6239 *
6240 * // header
6241 * opaque mbedtls_version[3]; // major, minor, patch
Manuel Pégourié-Gonnard4c90e852019-07-11 10:58:10 +02006242 * opaque context_format[5]; // version-specific field determining
Manuel Pégourié-Gonnard00400c22019-07-10 14:58:45 +02006243 * // the format of the remaining
Manuel Pégourié-Gonnard4c90e852019-07-11 10:58:10 +02006244 * // serialized data.
Manuel Pégourié-Gonnard4e9370b2019-07-23 16:31:16 +02006245 * Note: When updating the format, remember to keep these
6246 * version+format bytes. (We may make their size part of the API.)
Manuel Pégourié-Gonnard00400c22019-07-10 14:58:45 +02006247 *
6248 * // session sub-structure
6249 * opaque session<1..2^32-1>; // see mbedtls_ssl_session_save()
6250 * // transform sub-structure
6251 * uint8 random[64]; // ServerHello.random+ClientHello.random
6252 * uint8 in_cid<0..2^8-1> // Connection ID: expected incoming value
6253 * uint8 out_cid<0..2^8-1> // Connection ID: outgoing value to use
6254 * // fields from ssl_context
6255 * uint32 badmac_seen; // DTLS: number of records with failing MAC
6256 * uint64 in_window_top; // DTLS: last validated record seq_num
6257 * uint64 in_window; // DTLS: bitmask for replay protection
6258 * uint8 disable_datagram_packing; // DTLS: only one record per datagram
6259 * uint64 cur_out_ctr; // Record layer: outgoing sequence number
6260 * uint16 mtu; // DTLS: path mtu (max outgoing fragment size)
6261 * uint8 alpn_chosen<0..2^8-1> // ALPN: negotiated application protocol
6262 *
6263 * Note that many fields of the ssl_context or sub-structures are not
6264 * serialized, as they fall in one of the following categories:
6265 *
6266 * 1. forced value (eg in_left must be 0)
6267 * 2. pointer to dynamically-allocated memory (eg session, transform)
6268 * 3. value can be re-derived from other data (eg session keys from MS)
6269 * 4. value was temporary (eg content of input buffer)
6270 * 5. value will be provided by the user again (eg I/O callbacks and context)
Manuel Pégourié-Gonnardac87e282019-05-28 13:02:16 +02006271 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01006272int mbedtls_ssl_context_save(mbedtls_ssl_context *ssl,
6273 unsigned char *buf,
6274 size_t buf_len,
6275 size_t *olen)
Manuel Pégourié-Gonnardac87e282019-05-28 13:02:16 +02006276{
Manuel Pégourié-Gonnard4c90e852019-07-11 10:58:10 +02006277 unsigned char *p = buf;
6278 size_t used = 0;
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02006279 size_t session_len;
6280 int ret = 0;
Manuel Pégourié-Gonnard4c90e852019-07-11 10:58:10 +02006281
Manuel Pégourié-Gonnard1aaf6692019-07-10 14:14:05 +02006282 /*
Manuel Pégourié-Gonnarde4588692019-07-29 12:28:52 +02006283 * Enforce usage restrictions, see "return BAD_INPUT_DATA" in
6284 * this function's documentation.
6285 *
6286 * These are due to assumptions/limitations in the implementation. Some of
6287 * them are likely to stay (no handshake in progress) some might go away
6288 * (only DTLS) but are currently used to simplify the implementation.
Manuel Pégourié-Gonnard1aaf6692019-07-10 14:14:05 +02006289 */
Manuel Pégourié-Gonnarde4588692019-07-29 12:28:52 +02006290 /* The initial handshake must be over */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01006291 if (ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER) {
6292 MBEDTLS_SSL_DEBUG_MSG(1, ("Initial handshake isn't over"));
6293 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
Jarno Lamsa8c51b7c2019-08-21 13:45:05 +03006294 }
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01006295 if (ssl->handshake != NULL) {
6296 MBEDTLS_SSL_DEBUG_MSG(1, ("Handshake isn't completed"));
6297 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
Jarno Lamsa8c51b7c2019-08-21 13:45:05 +03006298 }
Manuel Pégourié-Gonnarde4588692019-07-29 12:28:52 +02006299 /* Double-check that sub-structures are indeed ready */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01006300 if (ssl->transform == NULL || ssl->session == NULL) {
6301 MBEDTLS_SSL_DEBUG_MSG(1, ("Serialised structures aren't ready"));
6302 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
Jarno Lamsa8c51b7c2019-08-21 13:45:05 +03006303 }
Manuel Pégourié-Gonnarde4588692019-07-29 12:28:52 +02006304 /* There must be no pending incoming or outgoing data */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01006305 if (mbedtls_ssl_check_pending(ssl) != 0) {
6306 MBEDTLS_SSL_DEBUG_MSG(1, ("There is pending incoming data"));
6307 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
Jarno Lamsa8c51b7c2019-08-21 13:45:05 +03006308 }
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01006309 if (ssl->out_left != 0) {
6310 MBEDTLS_SSL_DEBUG_MSG(1, ("There is pending outgoing data"));
6311 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
Jarno Lamsa8c51b7c2019-08-21 13:45:05 +03006312 }
Dave Rodgmana03396a2022-12-06 16:30:38 +00006313 /* Protocol must be DTLS, not TLS */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01006314 if (ssl->conf->transport != MBEDTLS_SSL_TRANSPORT_DATAGRAM) {
6315 MBEDTLS_SSL_DEBUG_MSG(1, ("Only DTLS is supported"));
6316 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
Jarno Lamsa8c51b7c2019-08-21 13:45:05 +03006317 }
Manuel Pégourié-Gonnarde4588692019-07-29 12:28:52 +02006318 /* Version must be 1.2 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01006319 if (ssl->major_ver != MBEDTLS_SSL_MAJOR_VERSION_3) {
6320 MBEDTLS_SSL_DEBUG_MSG(1, ("Only version 1.2 supported"));
6321 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
Jarno Lamsa8c51b7c2019-08-21 13:45:05 +03006322 }
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01006323 if (ssl->minor_ver != MBEDTLS_SSL_MINOR_VERSION_3) {
6324 MBEDTLS_SSL_DEBUG_MSG(1, ("Only version 1.2 supported"));
6325 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
Jarno Lamsa8c51b7c2019-08-21 13:45:05 +03006326 }
Manuel Pégourié-Gonnarde4588692019-07-29 12:28:52 +02006327 /* We must be using an AEAD ciphersuite */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01006328 if (mbedtls_ssl_transform_uses_aead(ssl->transform) != 1) {
6329 MBEDTLS_SSL_DEBUG_MSG(1, ("Only AEAD ciphersuites supported"));
6330 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
Jarno Lamsa8c51b7c2019-08-21 13:45:05 +03006331 }
Manuel Pégourié-Gonnarde4588692019-07-29 12:28:52 +02006332 /* Renegotiation must not be enabled */
6333#if defined(MBEDTLS_SSL_RENEGOTIATION)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01006334 if (ssl->conf->disable_renegotiation != MBEDTLS_SSL_RENEGOTIATION_DISABLED) {
6335 MBEDTLS_SSL_DEBUG_MSG(1, ("Renegotiation must not be enabled"));
6336 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
Jarno Lamsa8c51b7c2019-08-21 13:45:05 +03006337 }
Manuel Pégourié-Gonnarde4588692019-07-29 12:28:52 +02006338#endif
Manuel Pégourié-Gonnardac87e282019-05-28 13:02:16 +02006339
Manuel Pégourié-Gonnard4c90e852019-07-11 10:58:10 +02006340 /*
6341 * Version and format identifier
6342 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01006343 used += sizeof(ssl_serialized_context_header);
Manuel Pégourié-Gonnardac87e282019-05-28 13:02:16 +02006344
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01006345 if (used <= buf_len) {
6346 memcpy(p, ssl_serialized_context_header,
6347 sizeof(ssl_serialized_context_header));
6348 p += sizeof(ssl_serialized_context_header);
Manuel Pégourié-Gonnard4c90e852019-07-11 10:58:10 +02006349 }
6350
6351 /*
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02006352 * Session (length + data)
6353 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01006354 ret = ssl_session_save(ssl->session, 1, NULL, 0, &session_len);
6355 if (ret != MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL) {
6356 return ret;
6357 }
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02006358
6359 used += 4 + session_len;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01006360 if (used <= buf_len) {
6361 MBEDTLS_PUT_UINT32_BE(session_len, p, 0);
Joe Subbiani2f98d792021-08-20 11:44:44 +01006362 p += 4;
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02006363
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01006364 ret = ssl_session_save(ssl->session, 1,
6365 p, session_len, &session_len);
6366 if (ret != 0) {
6367 return ret;
6368 }
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02006369
6370 p += session_len;
6371 }
6372
6373 /*
Manuel Pégourié-Gonnardc2a7b892019-07-15 09:04:11 +02006374 * Transform
6375 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01006376 used += sizeof(ssl->transform->randbytes);
6377 if (used <= buf_len) {
6378 memcpy(p, ssl->transform->randbytes,
6379 sizeof(ssl->transform->randbytes));
6380 p += sizeof(ssl->transform->randbytes);
Manuel Pégourié-Gonnardc2a7b892019-07-15 09:04:11 +02006381 }
6382
6383#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
6384 used += 2 + ssl->transform->in_cid_len + ssl->transform->out_cid_len;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01006385 if (used <= buf_len) {
Manuel Pégourié-Gonnardc2a7b892019-07-15 09:04:11 +02006386 *p++ = ssl->transform->in_cid_len;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01006387 memcpy(p, ssl->transform->in_cid, ssl->transform->in_cid_len);
Manuel Pégourié-Gonnardc2a7b892019-07-15 09:04:11 +02006388 p += ssl->transform->in_cid_len;
6389
6390 *p++ = ssl->transform->out_cid_len;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01006391 memcpy(p, ssl->transform->out_cid, ssl->transform->out_cid_len);
Manuel Pégourié-Gonnardc2a7b892019-07-15 09:04:11 +02006392 p += ssl->transform->out_cid_len;
6393 }
6394#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
6395
6396 /*
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02006397 * Saved fields from top-level ssl_context structure
6398 */
6399#if defined(MBEDTLS_SSL_DTLS_BADMAC_LIMIT)
6400 used += 4;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01006401 if (used <= buf_len) {
6402 MBEDTLS_PUT_UINT32_BE(ssl->badmac_seen, p, 0);
Joe Subbiani2f98d792021-08-20 11:44:44 +01006403 p += 4;
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02006404 }
6405#endif /* MBEDTLS_SSL_DTLS_BADMAC_LIMIT */
6406
6407#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
6408 used += 16;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01006409 if (used <= buf_len) {
6410 MBEDTLS_PUT_UINT64_BE(ssl->in_window_top, p, 0);
Joe Subbiani2f98d792021-08-20 11:44:44 +01006411 p += 8;
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02006412
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01006413 MBEDTLS_PUT_UINT64_BE(ssl->in_window, p, 0);
Joe Subbiani2f98d792021-08-20 11:44:44 +01006414 p += 8;
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02006415 }
6416#endif /* MBEDTLS_SSL_DTLS_ANTI_REPLAY */
6417
6418#if defined(MBEDTLS_SSL_PROTO_DTLS)
6419 used += 1;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01006420 if (used <= buf_len) {
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02006421 *p++ = ssl->disable_datagram_packing;
6422 }
6423#endif /* MBEDTLS_SSL_PROTO_DTLS */
6424
6425 used += 8;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01006426 if (used <= buf_len) {
6427 memcpy(p, ssl->cur_out_ctr, 8);
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02006428 p += 8;
6429 }
6430
6431#if defined(MBEDTLS_SSL_PROTO_DTLS)
6432 used += 2;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01006433 if (used <= buf_len) {
6434 MBEDTLS_PUT_UINT16_BE(ssl->mtu, p, 0);
Joe Subbiani2f98d792021-08-20 11:44:44 +01006435 p += 2;
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02006436 }
6437#endif /* MBEDTLS_SSL_PROTO_DTLS */
6438
6439#if defined(MBEDTLS_SSL_ALPN)
6440 {
6441 const uint8_t alpn_len = ssl->alpn_chosen
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01006442 ? (uint8_t) strlen(ssl->alpn_chosen)
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02006443 : 0;
6444
6445 used += 1 + alpn_len;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01006446 if (used <= buf_len) {
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02006447 *p++ = alpn_len;
6448
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01006449 if (ssl->alpn_chosen != NULL) {
6450 memcpy(p, ssl->alpn_chosen, alpn_len);
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02006451 p += alpn_len;
6452 }
6453 }
6454 }
6455#endif /* MBEDTLS_SSL_ALPN */
6456
6457 /*
Manuel Pégourié-Gonnard4c90e852019-07-11 10:58:10 +02006458 * Done
6459 */
6460 *olen = used;
6461
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01006462 if (used > buf_len) {
6463 return MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL;
6464 }
Manuel Pégourié-Gonnardac87e282019-05-28 13:02:16 +02006465
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01006466 MBEDTLS_SSL_DEBUG_BUF(4, "saved context", buf, used);
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02006467
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01006468 return mbedtls_ssl_session_reset_int(ssl, 0);
Manuel Pégourié-Gonnardac87e282019-05-28 13:02:16 +02006469}
6470
6471/*
Manuel Pégourié-Gonnardc2a7b892019-07-15 09:04:11 +02006472 * Helper to get TLS 1.2 PRF from ciphersuite
6473 * (Duplicates bits of logic from ssl_set_handshake_prfs().)
6474 */
Andrzej Kurekf9412f72022-10-18 07:30:19 -04006475#if defined(MBEDTLS_SHA256_C) || \
6476 (defined(MBEDTLS_SHA512_C) && !defined(MBEDTLS_SHA512_NO_SHA384))
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01006477typedef int (*tls_prf_fn)(const unsigned char *secret, size_t slen,
6478 const char *label,
6479 const unsigned char *random, size_t rlen,
6480 unsigned char *dstbuf, size_t dlen);
6481static tls_prf_fn ssl_tls12prf_from_cs(int ciphersuite_id)
Manuel Pégourié-Gonnardc2a7b892019-07-15 09:04:11 +02006482{
6483 const mbedtls_ssl_ciphersuite_t * const ciphersuite_info =
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01006484 mbedtls_ssl_ciphersuite_from_id(ciphersuite_id);
Manuel Pégourié-Gonnardc2a7b892019-07-15 09:04:11 +02006485
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01006486 if (ciphersuite_info == NULL) {
6487 return NULL;
6488 }
Andrzej Kurek84fc52c2022-10-25 04:18:30 -04006489
6490#if defined(MBEDTLS_SHA512_C) && !defined(MBEDTLS_SHA512_NO_SHA384)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01006491 if (ciphersuite_info->mac == MBEDTLS_MD_SHA384) {
6492 return tls_prf_sha384;
6493 } else
Manuel Pégourié-Gonnard9a96fd72019-07-23 17:11:24 +02006494#endif
Andrzej Kurekf9412f72022-10-18 07:30:19 -04006495#if defined(MBEDTLS_SHA256_C)
6496 {
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01006497 if (ciphersuite_info->mac == MBEDTLS_MD_SHA256) {
6498 return tls_prf_sha256;
6499 }
Andrzej Kurekf9412f72022-10-18 07:30:19 -04006500 }
6501#endif
6502#if !defined(MBEDTLS_SHA256_C) && \
6503 (!defined(MBEDTLS_SHA512_C) || defined(MBEDTLS_SHA512_NO_SHA384))
6504 (void) ciphersuite_info;
6505#endif
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01006506 return NULL;
Manuel Pégourié-Gonnardc2a7b892019-07-15 09:04:11 +02006507}
6508
Andrzej Kurekf9412f72022-10-18 07:30:19 -04006509#endif /* MBEDTLS_SHA256_C ||
6510 (MBEDTLS_SHA512_C && !MBEDTLS_SHA512_NO_SHA384) */
6511
Manuel Pégourié-Gonnardc2a7b892019-07-15 09:04:11 +02006512/*
Manuel Pégourié-Gonnardb9dfc9f2019-07-12 10:50:19 +02006513 * Deserialize context, see mbedtls_ssl_context_save() for format.
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02006514 *
6515 * This internal version is wrapped by a public function that cleans up in
6516 * case of error.
Manuel Pégourié-Gonnardac87e282019-05-28 13:02:16 +02006517 */
Manuel Pégourié-Gonnardd904d662022-06-17 10:24:00 +02006518MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01006519static int ssl_context_load(mbedtls_ssl_context *ssl,
6520 const unsigned char *buf,
6521 size_t len)
Manuel Pégourié-Gonnardac87e282019-05-28 13:02:16 +02006522{
Manuel Pégourié-Gonnard4c90e852019-07-11 10:58:10 +02006523 const unsigned char *p = buf;
6524 const unsigned char * const end = buf + len;
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02006525 size_t session_len;
Janos Follath865b3eb2019-12-16 11:46:15 +00006526 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Andrzej Kurekf9412f72022-10-18 07:30:19 -04006527 tls_prf_fn prf_func = NULL;
Manuel Pégourié-Gonnard4c90e852019-07-11 10:58:10 +02006528
Manuel Pégourié-Gonnard0ff76402019-07-11 09:56:30 +02006529 /*
6530 * The context should have been freshly setup or reset.
6531 * Give the user an error in case of obvious misuse.
Manuel Pégourié-Gonnard4ca930f2019-07-26 16:31:53 +02006532 * (Checking session is useful because it won't be NULL if we're
Manuel Pégourié-Gonnard0ff76402019-07-11 09:56:30 +02006533 * renegotiating, or if the user mistakenly loaded a session first.)
6534 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01006535 if (ssl->state != MBEDTLS_SSL_HELLO_REQUEST ||
6536 ssl->session != NULL) {
6537 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
Manuel Pégourié-Gonnard0ff76402019-07-11 09:56:30 +02006538 }
6539
6540 /*
6541 * We can't check that the config matches the initial one, but we can at
6542 * least check it matches the requirements for serializing.
6543 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01006544 if (ssl->conf->transport != MBEDTLS_SSL_TRANSPORT_DATAGRAM ||
Manuel Pégourié-Gonnard0ff76402019-07-11 09:56:30 +02006545 ssl->conf->max_major_ver < MBEDTLS_SSL_MAJOR_VERSION_3 ||
6546 ssl->conf->min_major_ver > MBEDTLS_SSL_MAJOR_VERSION_3 ||
6547 ssl->conf->max_minor_ver < MBEDTLS_SSL_MINOR_VERSION_3 ||
6548 ssl->conf->min_minor_ver > MBEDTLS_SSL_MINOR_VERSION_3 ||
6549#if defined(MBEDTLS_SSL_RENEGOTIATION)
Manuel Pégourié-Gonnard9a96fd72019-07-23 17:11:24 +02006550 ssl->conf->disable_renegotiation != MBEDTLS_SSL_RENEGOTIATION_DISABLED ||
Manuel Pégourié-Gonnard0ff76402019-07-11 09:56:30 +02006551#endif
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01006552 0) {
6553 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
Manuel Pégourié-Gonnard0ff76402019-07-11 09:56:30 +02006554 }
6555
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01006556 MBEDTLS_SSL_DEBUG_BUF(4, "context to load", buf, len);
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02006557
Manuel Pégourié-Gonnard4c90e852019-07-11 10:58:10 +02006558 /*
6559 * Check version identifier
6560 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01006561 if ((size_t) (end - p) < sizeof(ssl_serialized_context_header)) {
6562 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
Manuel Pégourié-Gonnard4c90e852019-07-11 10:58:10 +02006563 }
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01006564
6565 if (memcmp(p, ssl_serialized_context_header,
6566 sizeof(ssl_serialized_context_header)) != 0) {
6567 return MBEDTLS_ERR_SSL_VERSION_MISMATCH;
6568 }
6569 p += sizeof(ssl_serialized_context_header);
Manuel Pégourié-Gonnard4c90e852019-07-11 10:58:10 +02006570
6571 /*
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02006572 * Session
6573 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01006574 if ((size_t) (end - p) < 4) {
6575 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
6576 }
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02006577
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01006578 session_len = ((size_t) p[0] << 24) |
6579 ((size_t) p[1] << 16) |
6580 ((size_t) p[2] << 8) |
6581 ((size_t) p[3]);
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02006582 p += 4;
6583
Manuel Pégourié-Gonnard142ba732019-07-23 14:43:30 +02006584 /* This has been allocated by ssl_handshake_init(), called by
Hanno Becker43aefe22020-02-05 10:44:56 +00006585 * by either mbedtls_ssl_session_reset_int() or mbedtls_ssl_setup(). */
Manuel Pégourié-Gonnard142ba732019-07-23 14:43:30 +02006586 ssl->session = ssl->session_negotiate;
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02006587 ssl->session_in = ssl->session;
6588 ssl->session_out = ssl->session;
Manuel Pégourié-Gonnard142ba732019-07-23 14:43:30 +02006589 ssl->session_negotiate = NULL;
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02006590
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01006591 if ((size_t) (end - p) < session_len) {
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 ret = ssl_session_load(ssl->session, 1, p, session_len);
6596 if (ret != 0) {
6597 mbedtls_ssl_session_free(ssl->session);
6598 return ret;
Manuel Pégourié-Gonnard45ac1f02019-07-23 16:52:45 +02006599 }
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02006600
6601 p += session_len;
6602
6603 /*
Manuel Pégourié-Gonnardc2a7b892019-07-15 09:04:11 +02006604 * Transform
6605 */
6606
Manuel Pégourié-Gonnard142ba732019-07-23 14:43:30 +02006607 /* This has been allocated by ssl_handshake_init(), called by
Hanno Becker43aefe22020-02-05 10:44:56 +00006608 * by either mbedtls_ssl_session_reset_int() or mbedtls_ssl_setup(). */
Manuel Pégourié-Gonnard142ba732019-07-23 14:43:30 +02006609 ssl->transform = ssl->transform_negotiate;
Manuel Pégourié-Gonnardc2a7b892019-07-15 09:04:11 +02006610 ssl->transform_in = ssl->transform;
6611 ssl->transform_out = ssl->transform;
Manuel Pégourié-Gonnard142ba732019-07-23 14:43:30 +02006612 ssl->transform_negotiate = NULL;
Manuel Pégourié-Gonnardc2a7b892019-07-15 09:04:11 +02006613
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01006614 prf_func = ssl_tls12prf_from_cs(ssl->session->ciphersuite);
6615 if (prf_func == NULL) {
6616 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
6617 }
Andrzej Kurekf9412f72022-10-18 07:30:19 -04006618
Manuel Pégourié-Gonnardc2a7b892019-07-15 09:04:11 +02006619 /* Read random bytes and populate structure */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01006620 if ((size_t) (end - p) < sizeof(ssl->transform->randbytes)) {
6621 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
6622 }
Manuel Pégourié-Gonnardc2a7b892019-07-15 09:04:11 +02006623
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01006624 ret = ssl_populate_transform(ssl->transform,
6625 ssl->session->ciphersuite,
6626 ssl->session->master,
Manuel Pégourié-Gonnardc2a7b892019-07-15 09:04:11 +02006627#if defined(MBEDTLS_SSL_SOME_MODES_USE_MAC)
6628#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01006629 ssl->session->encrypt_then_mac,
Manuel Pégourié-Gonnardc2a7b892019-07-15 09:04:11 +02006630#endif
6631#if defined(MBEDTLS_SSL_TRUNCATED_HMAC)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01006632 ssl->session->trunc_hmac,
Manuel Pégourié-Gonnardc2a7b892019-07-15 09:04:11 +02006633#endif
6634#endif /* MBEDTLS_SSL_SOME_MODES_USE_MAC */
6635#if defined(MBEDTLS_ZLIB_SUPPORT)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01006636 ssl->session->compression,
Manuel Pégourié-Gonnardc2a7b892019-07-15 09:04:11 +02006637#endif
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01006638 prf_func,
6639 p, /* currently pointing to randbytes */
6640 MBEDTLS_SSL_MINOR_VERSION_3, /* (D)TLS 1.2 is forced */
6641 ssl->conf->endpoint,
6642 ssl);
6643 if (ret != 0) {
6644 return ret;
6645 }
Manuel Pégourié-Gonnardc2a7b892019-07-15 09:04:11 +02006646
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01006647 p += sizeof(ssl->transform->randbytes);
Manuel Pégourié-Gonnardc2a7b892019-07-15 09:04:11 +02006648
6649#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
6650 /* Read connection IDs and store them */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01006651 if ((size_t) (end - p) < 1) {
6652 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
6653 }
Manuel Pégourié-Gonnardc2a7b892019-07-15 09:04:11 +02006654
6655 ssl->transform->in_cid_len = *p++;
6656
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01006657 if ((size_t) (end - p) < ssl->transform->in_cid_len + 1u) {
6658 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
6659 }
Manuel Pégourié-Gonnardc2a7b892019-07-15 09:04:11 +02006660
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01006661 memcpy(ssl->transform->in_cid, p, ssl->transform->in_cid_len);
Manuel Pégourié-Gonnardc2a7b892019-07-15 09:04:11 +02006662 p += ssl->transform->in_cid_len;
6663
6664 ssl->transform->out_cid_len = *p++;
6665
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01006666 if ((size_t) (end - p) < ssl->transform->out_cid_len) {
6667 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
6668 }
Manuel Pégourié-Gonnardc2a7b892019-07-15 09:04:11 +02006669
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01006670 memcpy(ssl->transform->out_cid, p, ssl->transform->out_cid_len);
Manuel Pégourié-Gonnardc2a7b892019-07-15 09:04:11 +02006671 p += ssl->transform->out_cid_len;
6672#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
6673
6674 /*
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02006675 * Saved fields from top-level ssl_context structure
6676 */
6677#if defined(MBEDTLS_SSL_DTLS_BADMAC_LIMIT)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01006678 if ((size_t) (end - p) < 4) {
6679 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
6680 }
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02006681
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01006682 ssl->badmac_seen = ((uint32_t) p[0] << 24) |
6683 ((uint32_t) p[1] << 16) |
6684 ((uint32_t) p[2] << 8) |
6685 ((uint32_t) p[3]);
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02006686 p += 4;
6687#endif /* MBEDTLS_SSL_DTLS_BADMAC_LIMIT */
6688
6689#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01006690 if ((size_t) (end - p) < 16) {
6691 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
6692 }
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02006693
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01006694 ssl->in_window_top = ((uint64_t) p[0] << 56) |
6695 ((uint64_t) p[1] << 48) |
6696 ((uint64_t) p[2] << 40) |
6697 ((uint64_t) p[3] << 32) |
6698 ((uint64_t) p[4] << 24) |
6699 ((uint64_t) p[5] << 16) |
6700 ((uint64_t) p[6] << 8) |
6701 ((uint64_t) p[7]);
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02006702 p += 8;
6703
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01006704 ssl->in_window = ((uint64_t) p[0] << 56) |
6705 ((uint64_t) p[1] << 48) |
6706 ((uint64_t) p[2] << 40) |
6707 ((uint64_t) p[3] << 32) |
6708 ((uint64_t) p[4] << 24) |
6709 ((uint64_t) p[5] << 16) |
6710 ((uint64_t) p[6] << 8) |
6711 ((uint64_t) p[7]);
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02006712 p += 8;
6713#endif /* MBEDTLS_SSL_DTLS_ANTI_REPLAY */
6714
6715#if defined(MBEDTLS_SSL_PROTO_DTLS)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01006716 if ((size_t) (end - p) < 1) {
6717 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
6718 }
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02006719
6720 ssl->disable_datagram_packing = *p++;
6721#endif /* MBEDTLS_SSL_PROTO_DTLS */
6722
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01006723 if ((size_t) (end - p) < 8) {
6724 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
6725 }
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02006726
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01006727 memcpy(ssl->cur_out_ctr, p, 8);
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02006728 p += 8;
6729
6730#if defined(MBEDTLS_SSL_PROTO_DTLS)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01006731 if ((size_t) (end - p) < 2) {
6732 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
6733 }
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02006734
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01006735 ssl->mtu = (p[0] << 8) | p[1];
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02006736 p += 2;
6737#endif /* MBEDTLS_SSL_PROTO_DTLS */
6738
6739#if defined(MBEDTLS_SSL_ALPN)
6740 {
6741 uint8_t alpn_len;
6742 const char **cur;
6743
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01006744 if ((size_t) (end - p) < 1) {
6745 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
6746 }
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02006747
6748 alpn_len = *p++;
6749
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01006750 if (alpn_len != 0 && ssl->conf->alpn_list != NULL) {
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02006751 /* alpn_chosen should point to an item in the configured list */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01006752 for (cur = ssl->conf->alpn_list; *cur != NULL; cur++) {
6753 if (strlen(*cur) == alpn_len &&
Waleed Elmelegy98ebf482024-03-15 14:29:24 +00006754 memcmp(p, *cur, alpn_len) == 0) {
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02006755 ssl->alpn_chosen = *cur;
6756 break;
6757 }
6758 }
6759 }
6760
6761 /* can only happen on conf mismatch */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01006762 if (alpn_len != 0 && ssl->alpn_chosen == NULL) {
6763 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
6764 }
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02006765
6766 p += alpn_len;
6767 }
6768#endif /* MBEDTLS_SSL_ALPN */
6769
6770 /*
Manuel Pégourié-Gonnard0eb3eac2019-07-15 11:53:51 +02006771 * Forced fields from top-level ssl_context structure
6772 *
6773 * Most of them already set to the correct value by mbedtls_ssl_init() and
6774 * mbedtls_ssl_reset(), so we only need to set the remaining ones.
6775 */
6776 ssl->state = MBEDTLS_SSL_HANDSHAKE_OVER;
6777
6778 ssl->major_ver = MBEDTLS_SSL_MAJOR_VERSION_3;
6779 ssl->minor_ver = MBEDTLS_SSL_MINOR_VERSION_3;
6780
Hanno Becker361b10d2019-08-30 10:42:49 +01006781 /* Adjust pointers for header fields of outgoing records to
6782 * the given transform, accounting for explicit IV and CID. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01006783 mbedtls_ssl_update_out_pointers(ssl, ssl->transform);
Hanno Becker361b10d2019-08-30 10:42:49 +01006784
Manuel Pégourié-Gonnard0eb3eac2019-07-15 11:53:51 +02006785#if defined(MBEDTLS_SSL_PROTO_DTLS)
6786 ssl->in_epoch = 1;
6787#endif
6788
6789 /* mbedtls_ssl_reset() leaves the handshake sub-structure allocated,
6790 * which we don't want - otherwise we'd end up freeing the wrong transform
Hanno Beckerce5f5fd2020-02-05 10:47:44 +00006791 * by calling mbedtls_ssl_handshake_wrapup_free_hs_transform()
6792 * inappropriately. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01006793 if (ssl->handshake != NULL) {
6794 mbedtls_ssl_handshake_free(ssl);
6795 mbedtls_free(ssl->handshake);
Manuel Pégourié-Gonnard0eb3eac2019-07-15 11:53:51 +02006796 ssl->handshake = NULL;
6797 }
6798
6799 /*
Manuel Pégourié-Gonnard4c90e852019-07-11 10:58:10 +02006800 * Done - should have consumed entire buffer
6801 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01006802 if (p != end) {
6803 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
6804 }
Manuel Pégourié-Gonnardac87e282019-05-28 13:02:16 +02006805
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01006806 return 0;
Manuel Pégourié-Gonnardac87e282019-05-28 13:02:16 +02006807}
6808
6809/*
Manuel Pégourié-Gonnardb9dfc9f2019-07-12 10:50:19 +02006810 * Deserialize context: public wrapper for error cleaning
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02006811 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01006812int mbedtls_ssl_context_load(mbedtls_ssl_context *context,
6813 const unsigned char *buf,
6814 size_t len)
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02006815{
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01006816 int ret = ssl_context_load(context, buf, len);
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02006817
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01006818 if (ret != 0) {
6819 mbedtls_ssl_free(context);
6820 }
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02006821
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01006822 return ret;
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02006823}
Manuel Pégourié-Gonnard5c0e3772019-07-23 16:13:17 +02006824#endif /* MBEDTLS_SSL_CONTEXT_SERIALIZATION */
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02006825
6826/*
Paul Bakker5121ce52009-01-03 21:22:43 +00006827 * Free an SSL context
6828 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01006829void mbedtls_ssl_free(mbedtls_ssl_context *ssl)
Paul Bakker5121ce52009-01-03 21:22:43 +00006830{
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01006831 if (ssl == NULL) {
Paul Bakkeraccaffe2014-06-26 13:37:14 +02006832 return;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01006833 }
Paul Bakkeraccaffe2014-06-26 13:37:14 +02006834
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01006835 MBEDTLS_SSL_DEBUG_MSG(2, ("=> free"));
Paul Bakker5121ce52009-01-03 21:22:43 +00006836
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01006837 if (ssl->out_buf != NULL) {
sander-visserb8aa2072020-05-06 22:05:13 +02006838#if defined(MBEDTLS_SSL_VARIABLE_BUFFER_LENGTH)
6839 size_t out_buf_len = ssl->out_buf_len;
6840#else
6841 size_t out_buf_len = MBEDTLS_SSL_OUT_BUFFER_LEN;
6842#endif
6843
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01006844 mbedtls_platform_zeroize(ssl->out_buf, out_buf_len);
6845 mbedtls_free(ssl->out_buf);
Andrzej Kurek0afa2a12020-03-03 10:39:58 -05006846 ssl->out_buf = NULL;
Paul Bakker5121ce52009-01-03 21:22:43 +00006847 }
6848
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01006849 if (ssl->in_buf != NULL) {
sander-visserb8aa2072020-05-06 22:05:13 +02006850#if defined(MBEDTLS_SSL_VARIABLE_BUFFER_LENGTH)
6851 size_t in_buf_len = ssl->in_buf_len;
6852#else
6853 size_t in_buf_len = MBEDTLS_SSL_IN_BUFFER_LEN;
6854#endif
6855
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01006856 mbedtls_platform_zeroize(ssl->in_buf, in_buf_len);
6857 mbedtls_free(ssl->in_buf);
Andrzej Kurek0afa2a12020-03-03 10:39:58 -05006858 ssl->in_buf = NULL;
Paul Bakker5121ce52009-01-03 21:22:43 +00006859 }
6860
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006861#if defined(MBEDTLS_ZLIB_SUPPORT)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01006862 if (ssl->compress_buf != NULL) {
6863 mbedtls_platform_zeroize(ssl->compress_buf, MBEDTLS_SSL_COMPRESS_BUFFER_LEN);
6864 mbedtls_free(ssl->compress_buf);
Paul Bakker16770332013-10-11 09:59:44 +02006865 }
6866#endif
6867
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01006868 if (ssl->transform) {
6869 mbedtls_ssl_transform_free(ssl->transform);
6870 mbedtls_free(ssl->transform);
Paul Bakker48916f92012-09-16 19:57:18 +00006871 }
6872
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01006873 if (ssl->handshake) {
6874 mbedtls_ssl_handshake_free(ssl);
6875 mbedtls_ssl_transform_free(ssl->transform_negotiate);
6876 mbedtls_ssl_session_free(ssl->session_negotiate);
Paul Bakker48916f92012-09-16 19:57:18 +00006877
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01006878 mbedtls_free(ssl->handshake);
6879 mbedtls_free(ssl->transform_negotiate);
6880 mbedtls_free(ssl->session_negotiate);
Paul Bakker48916f92012-09-16 19:57:18 +00006881 }
6882
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01006883 if (ssl->session) {
6884 mbedtls_ssl_session_free(ssl->session);
6885 mbedtls_free(ssl->session);
Paul Bakkerc0463502013-02-14 11:19:38 +01006886 }
6887
Manuel Pégourié-Gonnard55fab2d2015-05-11 16:15:19 +02006888#if defined(MBEDTLS_X509_CRT_PARSE_C)
Gilles Peskine3a2f75d2025-02-12 23:28:48 +01006889 mbedtls_ssl_free_hostname(ssl);
Paul Bakker0be444a2013-08-27 21:55:01 +02006890#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00006891
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006892#if defined(MBEDTLS_SSL_HW_RECORD_ACCEL)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01006893 if (mbedtls_ssl_hw_record_finish != NULL) {
6894 MBEDTLS_SSL_DEBUG_MSG(2, ("going for mbedtls_ssl_hw_record_finish()"));
6895 mbedtls_ssl_hw_record_finish(ssl);
Paul Bakker05ef8352012-05-08 09:17:57 +00006896 }
6897#endif
6898
Manuel Pégourié-Gonnarde057d3b2015-05-20 10:59:43 +02006899#if defined(MBEDTLS_SSL_DTLS_HELLO_VERIFY) && defined(MBEDTLS_SSL_SRV_C)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01006900 mbedtls_free(ssl->cli_id);
Manuel Pégourié-Gonnard43c02182014-07-22 17:32:01 +02006901#endif
6902
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01006903 MBEDTLS_SSL_DEBUG_MSG(2, ("<= free"));
Paul Bakker2da561c2009-02-05 18:00:28 +00006904
Paul Bakker86f04f42013-02-14 11:20:09 +01006905 /* Actually clear after last debug message */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01006906 mbedtls_platform_zeroize(ssl, sizeof(mbedtls_ssl_context));
Paul Bakker5121ce52009-01-03 21:22:43 +00006907}
6908
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02006909/*
Tom Cosgrove49f99bc2022-12-04 16:44:21 +00006910 * Initialize mbedtls_ssl_config
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02006911 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01006912void mbedtls_ssl_config_init(mbedtls_ssl_config *conf)
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02006913{
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01006914 memset(conf, 0, sizeof(mbedtls_ssl_config));
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02006915}
6916
Gilles Peskineeccd8882020-03-10 12:19:08 +01006917#if defined(MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED)
Chien Wongb6d57932024-02-07 21:48:12 +08006918static const int ssl_preset_default_hashes[] = {
Gilles Peskinec54010c2021-05-12 22:52:09 +02006919#if defined(MBEDTLS_SHA512_C)
Manuel Pégourié-Gonnard47229c72015-12-04 15:02:56 +01006920 MBEDTLS_MD_SHA512,
Gilles Peskinec54010c2021-05-12 22:52:09 +02006921#endif
6922#if defined(MBEDTLS_SHA512_C) && !defined(MBEDTLS_SHA512_NO_SHA384)
Manuel Pégourié-Gonnard47229c72015-12-04 15:02:56 +01006923 MBEDTLS_MD_SHA384,
6924#endif
6925#if defined(MBEDTLS_SHA256_C)
6926 MBEDTLS_MD_SHA256,
6927 MBEDTLS_MD_SHA224,
6928#endif
Gilles Peskine5d2511c2017-05-12 13:16:40 +02006929#if defined(MBEDTLS_SHA1_C) && defined(MBEDTLS_TLS_DEFAULT_ALLOW_SHA1_IN_KEY_EXCHANGE)
Manuel Pégourié-Gonnard47229c72015-12-04 15:02:56 +01006930 MBEDTLS_MD_SHA1,
6931#endif
6932 MBEDTLS_MD_NONE
6933};
Simon Butcherc97b6972015-12-27 23:48:17 +00006934#endif
Manuel Pégourié-Gonnard47229c72015-12-04 15:02:56 +01006935
Chien Wongb6d57932024-02-07 21:48:12 +08006936static const int ssl_preset_suiteb_ciphersuites[] = {
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02006937 MBEDTLS_TLS_ECDHE_ECDSA_WITH_AES_128_GCM_SHA256,
6938 MBEDTLS_TLS_ECDHE_ECDSA_WITH_AES_256_GCM_SHA384,
6939 0
6940};
6941
Gilles Peskineeccd8882020-03-10 12:19:08 +01006942#if defined(MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED)
Chien Wongb6d57932024-02-07 21:48:12 +08006943static const int ssl_preset_suiteb_hashes[] = {
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02006944 MBEDTLS_MD_SHA256,
6945 MBEDTLS_MD_SHA384,
6946 MBEDTLS_MD_NONE
6947};
6948#endif
6949
6950#if defined(MBEDTLS_ECP_C)
Chien Wongb6d57932024-02-07 21:48:12 +08006951static const mbedtls_ecp_group_id ssl_preset_suiteb_curves[] = {
Jaeden Amerod4311042019-06-03 08:27:16 +01006952#if defined(MBEDTLS_ECP_DP_SECP256R1_ENABLED)
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02006953 MBEDTLS_ECP_DP_SECP256R1,
Jaeden Amerod4311042019-06-03 08:27:16 +01006954#endif
6955#if defined(MBEDTLS_ECP_DP_SECP384R1_ENABLED)
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02006956 MBEDTLS_ECP_DP_SECP384R1,
Jaeden Amerod4311042019-06-03 08:27:16 +01006957#endif
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02006958 MBEDTLS_ECP_DP_NONE
6959};
6960#endif
6961
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02006962/*
Tillmann Karras588ad502015-09-25 04:27:22 +02006963 * Load default in mbedtls_ssl_config
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02006964 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01006965int mbedtls_ssl_config_defaults(mbedtls_ssl_config *conf,
6966 int endpoint, int transport, int preset)
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02006967{
Manuel Pégourié-Gonnard8b431fb2015-05-11 12:54:52 +02006968#if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_SSL_SRV_C)
Janos Follath865b3eb2019-12-16 11:46:15 +00006969 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnard8b431fb2015-05-11 12:54:52 +02006970#endif
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02006971
Manuel Pégourié-Gonnard0de074f2015-05-14 12:58:01 +02006972 /* Use the functions here so that they are covered in tests,
6973 * but otherwise access member directly for efficiency */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01006974 mbedtls_ssl_conf_endpoint(conf, endpoint);
6975 mbedtls_ssl_conf_transport(conf, transport);
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02006976
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02006977 /*
6978 * Things that are common to all presets
6979 */
Manuel Pégourié-Gonnard419d5ae2015-05-04 19:32:36 +02006980#if defined(MBEDTLS_SSL_CLI_C)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01006981 if (endpoint == MBEDTLS_SSL_IS_CLIENT) {
Manuel Pégourié-Gonnard419d5ae2015-05-04 19:32:36 +02006982 conf->authmode = MBEDTLS_SSL_VERIFY_REQUIRED;
6983#if defined(MBEDTLS_SSL_SESSION_TICKETS)
6984 conf->session_tickets = MBEDTLS_SSL_SESSION_TICKETS_ENABLED;
6985#endif
6986 }
6987#endif
6988
Manuel Pégourié-Gonnard66dc5552015-05-14 12:28:21 +02006989#if defined(MBEDTLS_ARC4_C)
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02006990 conf->arc4_disabled = MBEDTLS_SSL_ARC4_DISABLED;
Manuel Pégourié-Gonnard66dc5552015-05-14 12:28:21 +02006991#endif
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02006992
6993#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
6994 conf->encrypt_then_mac = MBEDTLS_SSL_ETM_ENABLED;
6995#endif
6996
6997#if defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET)
6998 conf->extended_ms = MBEDTLS_SSL_EXTENDED_MS_ENABLED;
6999#endif
7000
Manuel Pégourié-Gonnard17eab2b2015-05-05 16:34:53 +01007001#if defined(MBEDTLS_SSL_CBC_RECORD_SPLITTING)
7002 conf->cbc_record_splitting = MBEDTLS_SSL_CBC_RECORD_SPLITTING_ENABLED;
7003#endif
7004
Manuel Pégourié-Gonnarde057d3b2015-05-20 10:59:43 +02007005#if defined(MBEDTLS_SSL_DTLS_HELLO_VERIFY) && defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02007006 conf->f_cookie_write = ssl_cookie_write_dummy;
7007 conf->f_cookie_check = ssl_cookie_check_dummy;
7008#endif
7009
7010#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
7011 conf->anti_replay = MBEDTLS_SSL_ANTI_REPLAY_ENABLED;
7012#endif
7013
Janos Follath088ce432017-04-10 12:42:31 +01007014#if defined(MBEDTLS_SSL_SRV_C)
7015 conf->cert_req_ca_list = MBEDTLS_SSL_CERT_REQ_CA_LIST_ENABLED;
7016#endif
7017
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02007018#if defined(MBEDTLS_SSL_PROTO_DTLS)
7019 conf->hs_timeout_min = MBEDTLS_SSL_DTLS_TIMEOUT_DFL_MIN;
7020 conf->hs_timeout_max = MBEDTLS_SSL_DTLS_TIMEOUT_DFL_MAX;
7021#endif
7022
7023#if defined(MBEDTLS_SSL_RENEGOTIATION)
7024 conf->renego_max_records = MBEDTLS_SSL_RENEGO_MAX_RECORDS_DEFAULT;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01007025 memset(conf->renego_period, 0x00, 2);
7026 memset(conf->renego_period + 2, 0xFF, 6);
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02007027#endif
7028
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02007029#if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_SSL_SRV_C)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01007030 if (endpoint == MBEDTLS_SSL_IS_SERVER) {
7031 const unsigned char dhm_p[] =
7032 MBEDTLS_DHM_RFC3526_MODP_2048_P_BIN;
7033 const unsigned char dhm_g[] =
7034 MBEDTLS_DHM_RFC3526_MODP_2048_G_BIN;
Hanno Becker00d0a682017-10-04 13:14:29 +01007035
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01007036 if ((ret = mbedtls_ssl_conf_dh_param_bin(conf,
7037 dhm_p, sizeof(dhm_p),
7038 dhm_g, sizeof(dhm_g))) != 0) {
7039 return ret;
7040 }
7041 }
Manuel Pégourié-Gonnardbd990d62015-06-11 14:49:42 +02007042#endif
7043
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02007044 /*
7045 * Preset-specific defaults
7046 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01007047 switch (preset) {
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02007048 /*
7049 * NSA Suite B
7050 */
7051 case MBEDTLS_SSL_PRESET_SUITEB:
7052 conf->min_major_ver = MBEDTLS_SSL_MAJOR_VERSION_3;
7053 conf->min_minor_ver = MBEDTLS_SSL_MINOR_VERSION_3; /* TLS 1.2 */
7054 conf->max_major_ver = MBEDTLS_SSL_MAX_MAJOR_VERSION;
7055 conf->max_minor_ver = MBEDTLS_SSL_MAX_MINOR_VERSION;
7056
7057 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_0] =
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01007058 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_1] =
7059 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_2] =
7060 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_3] =
7061 ssl_preset_suiteb_ciphersuites;
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02007062
7063#if defined(MBEDTLS_X509_CRT_PARSE_C)
7064 conf->cert_profile = &mbedtls_x509_crt_profile_suiteb;
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02007065#endif
7066
Gilles Peskineeccd8882020-03-10 12:19:08 +01007067#if defined(MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED)
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02007068 conf->sig_hashes = ssl_preset_suiteb_hashes;
7069#endif
7070
7071#if defined(MBEDTLS_ECP_C)
7072 conf->curve_list = ssl_preset_suiteb_curves;
7073#endif
Manuel Pégourié-Gonnardc98204e2015-08-11 04:21:01 +02007074 break;
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02007075
7076 /*
7077 * Default
7078 */
7079 default:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01007080 conf->min_major_ver = (MBEDTLS_SSL_MIN_MAJOR_VERSION >
7081 MBEDTLS_SSL_MIN_VALID_MAJOR_VERSION) ?
7082 MBEDTLS_SSL_MIN_MAJOR_VERSION :
7083 MBEDTLS_SSL_MIN_VALID_MAJOR_VERSION;
7084 conf->min_minor_ver = (MBEDTLS_SSL_MIN_MINOR_VERSION >
7085 MBEDTLS_SSL_MIN_VALID_MINOR_VERSION) ?
7086 MBEDTLS_SSL_MIN_MINOR_VERSION :
7087 MBEDTLS_SSL_MIN_VALID_MINOR_VERSION;
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02007088 conf->max_major_ver = MBEDTLS_SSL_MAX_MAJOR_VERSION;
7089 conf->max_minor_ver = MBEDTLS_SSL_MAX_MINOR_VERSION;
7090
7091#if defined(MBEDTLS_SSL_PROTO_DTLS)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01007092 if (transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM) {
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02007093 conf->min_minor_ver = MBEDTLS_SSL_MINOR_VERSION_2;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01007094 }
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02007095#endif
7096
7097 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_0] =
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01007098 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_1] =
7099 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_2] =
7100 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_3] =
7101 mbedtls_ssl_list_ciphersuites();
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02007102
7103#if defined(MBEDTLS_X509_CRT_PARSE_C)
7104 conf->cert_profile = &mbedtls_x509_crt_profile_default;
7105#endif
7106
Gilles Peskineeccd8882020-03-10 12:19:08 +01007107#if defined(MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED)
Manuel Pégourié-Gonnard47229c72015-12-04 15:02:56 +01007108 conf->sig_hashes = ssl_preset_default_hashes;
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02007109#endif
7110
7111#if defined(MBEDTLS_ECP_C)
7112 conf->curve_list = mbedtls_ecp_grp_id_list();
7113#endif
7114
7115#if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_SSL_CLI_C)
7116 conf->dhm_min_bitlen = 1024;
7117#endif
7118 }
7119
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01007120 return 0;
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02007121}
7122
7123/*
7124 * Free mbedtls_ssl_config
7125 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01007126void mbedtls_ssl_config_free(mbedtls_ssl_config *conf)
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02007127{
7128#if defined(MBEDTLS_DHM_C)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01007129 mbedtls_mpi_free(&conf->dhm_P);
7130 mbedtls_mpi_free(&conf->dhm_G);
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02007131#endif
7132
Gilles Peskineeccd8882020-03-10 12:19:08 +01007133#if defined(MBEDTLS_KEY_EXCHANGE_SOME_PSK_ENABLED)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01007134 if (conf->psk != NULL) {
7135 mbedtls_platform_zeroize(conf->psk, conf->psk_len);
7136 mbedtls_free(conf->psk);
Azim Khan27e8a122018-03-21 14:24:11 +00007137 conf->psk = NULL;
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02007138 conf->psk_len = 0;
junyeonLEE316b1622017-12-20 16:29:30 +09007139 }
7140
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01007141 if (conf->psk_identity != NULL) {
7142 mbedtls_platform_zeroize(conf->psk_identity, conf->psk_identity_len);
7143 mbedtls_free(conf->psk_identity);
Azim Khan27e8a122018-03-21 14:24:11 +00007144 conf->psk_identity = NULL;
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02007145 conf->psk_identity_len = 0;
7146 }
7147#endif
7148
7149#if defined(MBEDTLS_X509_CRT_PARSE_C)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01007150 ssl_key_cert_free(conf->key_cert);
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02007151#endif
7152
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01007153 mbedtls_platform_zeroize(conf, sizeof(mbedtls_ssl_config));
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02007154}
7155
Manuel Pégourié-Gonnard5674a972015-10-19 15:14:03 +02007156#if defined(MBEDTLS_PK_C) && \
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01007157 (defined(MBEDTLS_RSA_C) || defined(MBEDTLS_ECDSA_C))
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02007158/*
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007159 * Convert between MBEDTLS_PK_XXX and SSL_SIG_XXX
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02007160 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01007161unsigned char mbedtls_ssl_sig_from_pk(mbedtls_pk_context *pk)
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02007162{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007163#if defined(MBEDTLS_RSA_C)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01007164 if (mbedtls_pk_can_do(pk, MBEDTLS_PK_RSA)) {
7165 return MBEDTLS_SSL_SIG_RSA;
7166 }
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02007167#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007168#if defined(MBEDTLS_ECDSA_C)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01007169 if (mbedtls_pk_can_do(pk, MBEDTLS_PK_ECDSA)) {
7170 return MBEDTLS_SSL_SIG_ECDSA;
7171 }
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02007172#endif
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01007173 return MBEDTLS_SSL_SIG_ANON;
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02007174}
7175
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01007176unsigned char mbedtls_ssl_sig_from_pk_alg(mbedtls_pk_type_t type)
Hanno Becker7e5437a2017-04-28 17:15:26 +01007177{
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01007178 switch (type) {
Hanno Becker7e5437a2017-04-28 17:15:26 +01007179 case MBEDTLS_PK_RSA:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01007180 return MBEDTLS_SSL_SIG_RSA;
Hanno Becker7e5437a2017-04-28 17:15:26 +01007181 case MBEDTLS_PK_ECDSA:
7182 case MBEDTLS_PK_ECKEY:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01007183 return MBEDTLS_SSL_SIG_ECDSA;
Hanno Becker7e5437a2017-04-28 17:15:26 +01007184 default:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01007185 return MBEDTLS_SSL_SIG_ANON;
Hanno Becker7e5437a2017-04-28 17:15:26 +01007186 }
7187}
7188
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01007189mbedtls_pk_type_t mbedtls_ssl_pk_alg_from_sig(unsigned char sig)
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02007190{
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01007191 switch (sig) {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007192#if defined(MBEDTLS_RSA_C)
7193 case MBEDTLS_SSL_SIG_RSA:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01007194 return MBEDTLS_PK_RSA;
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02007195#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007196#if defined(MBEDTLS_ECDSA_C)
7197 case MBEDTLS_SSL_SIG_ECDSA:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01007198 return MBEDTLS_PK_ECDSA;
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02007199#endif
7200 default:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01007201 return MBEDTLS_PK_NONE;
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02007202 }
7203}
Manuel Pégourié-Gonnard5674a972015-10-19 15:14:03 +02007204#endif /* MBEDTLS_PK_C && ( MBEDTLS_RSA_C || MBEDTLS_ECDSA_C ) */
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02007205
Hanno Becker7e5437a2017-04-28 17:15:26 +01007206#if defined(MBEDTLS_SSL_PROTO_TLS1_2) && \
Gilles Peskineeccd8882020-03-10 12:19:08 +01007207 defined(MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED)
Hanno Becker7e5437a2017-04-28 17:15:26 +01007208
7209/* Find an entry in a signature-hash set matching a given hash algorithm. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01007210mbedtls_md_type_t mbedtls_ssl_sig_hash_set_find(mbedtls_ssl_sig_hash_set_t *set,
7211 mbedtls_pk_type_t sig_alg)
Hanno Becker7e5437a2017-04-28 17:15:26 +01007212{
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01007213 switch (sig_alg) {
Hanno Becker7e5437a2017-04-28 17:15:26 +01007214 case MBEDTLS_PK_RSA:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01007215 return set->rsa;
Hanno Becker7e5437a2017-04-28 17:15:26 +01007216 case MBEDTLS_PK_ECDSA:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01007217 return set->ecdsa;
Hanno Becker7e5437a2017-04-28 17:15:26 +01007218 default:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01007219 return MBEDTLS_MD_NONE;
Hanno Becker7e5437a2017-04-28 17:15:26 +01007220 }
7221}
7222
7223/* Add a signature-hash-pair to a signature-hash set */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01007224void mbedtls_ssl_sig_hash_set_add(mbedtls_ssl_sig_hash_set_t *set,
7225 mbedtls_pk_type_t sig_alg,
7226 mbedtls_md_type_t md_alg)
Hanno Becker7e5437a2017-04-28 17:15:26 +01007227{
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01007228 switch (sig_alg) {
Hanno Becker7e5437a2017-04-28 17:15:26 +01007229 case MBEDTLS_PK_RSA:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01007230 if (set->rsa == MBEDTLS_MD_NONE) {
Hanno Becker7e5437a2017-04-28 17:15:26 +01007231 set->rsa = md_alg;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01007232 }
Hanno Becker7e5437a2017-04-28 17:15:26 +01007233 break;
7234
7235 case MBEDTLS_PK_ECDSA:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01007236 if (set->ecdsa == MBEDTLS_MD_NONE) {
Hanno Becker7e5437a2017-04-28 17:15:26 +01007237 set->ecdsa = md_alg;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01007238 }
Hanno Becker7e5437a2017-04-28 17:15:26 +01007239 break;
7240
7241 default:
7242 break;
7243 }
7244}
7245
7246/* Allow exactly one hash algorithm for each signature. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01007247void mbedtls_ssl_sig_hash_set_const_hash(mbedtls_ssl_sig_hash_set_t *set,
7248 mbedtls_md_type_t md_alg)
Hanno Becker7e5437a2017-04-28 17:15:26 +01007249{
7250 set->rsa = md_alg;
7251 set->ecdsa = md_alg;
7252}
7253
7254#endif /* MBEDTLS_SSL_PROTO_TLS1_2) &&
Gilles Peskineeccd8882020-03-10 12:19:08 +01007255 MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED */
Hanno Becker7e5437a2017-04-28 17:15:26 +01007256
Manuel Pégourié-Gonnard1a483832013-09-20 12:29:15 +02007257/*
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +02007258 * Convert from MBEDTLS_SSL_HASH_XXX to MBEDTLS_MD_XXX
Manuel Pégourié-Gonnard1a483832013-09-20 12:29:15 +02007259 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01007260mbedtls_md_type_t mbedtls_ssl_md_alg_from_hash(unsigned char hash)
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02007261{
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01007262 switch (hash) {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007263#if defined(MBEDTLS_MD5_C)
7264 case MBEDTLS_SSL_HASH_MD5:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01007265 return MBEDTLS_MD_MD5;
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02007266#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007267#if defined(MBEDTLS_SHA1_C)
7268 case MBEDTLS_SSL_HASH_SHA1:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01007269 return MBEDTLS_MD_SHA1;
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02007270#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007271#if defined(MBEDTLS_SHA256_C)
7272 case MBEDTLS_SSL_HASH_SHA224:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01007273 return MBEDTLS_MD_SHA224;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007274 case MBEDTLS_SSL_HASH_SHA256:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01007275 return MBEDTLS_MD_SHA256;
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02007276#endif
Gilles Peskined2d59372021-05-12 22:43:27 +02007277#if defined(MBEDTLS_SHA512_C) && !defined(MBEDTLS_SHA512_NO_SHA384)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007278 case MBEDTLS_SSL_HASH_SHA384:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01007279 return MBEDTLS_MD_SHA384;
Gilles Peskinec54010c2021-05-12 22:52:09 +02007280#endif
7281#if defined(MBEDTLS_SHA512_C)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007282 case MBEDTLS_SSL_HASH_SHA512:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01007283 return MBEDTLS_MD_SHA512;
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02007284#endif
7285 default:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01007286 return MBEDTLS_MD_NONE;
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02007287 }
7288}
7289
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +02007290/*
7291 * Convert from MBEDTLS_MD_XXX to MBEDTLS_SSL_HASH_XXX
7292 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01007293unsigned char mbedtls_ssl_hash_from_md_alg(int md)
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +02007294{
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01007295 switch (md) {
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +02007296#if defined(MBEDTLS_MD5_C)
7297 case MBEDTLS_MD_MD5:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01007298 return MBEDTLS_SSL_HASH_MD5;
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +02007299#endif
7300#if defined(MBEDTLS_SHA1_C)
7301 case MBEDTLS_MD_SHA1:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01007302 return MBEDTLS_SSL_HASH_SHA1;
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +02007303#endif
7304#if defined(MBEDTLS_SHA256_C)
7305 case MBEDTLS_MD_SHA224:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01007306 return MBEDTLS_SSL_HASH_SHA224;
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +02007307 case MBEDTLS_MD_SHA256:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01007308 return MBEDTLS_SSL_HASH_SHA256;
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +02007309#endif
Gilles Peskined2d59372021-05-12 22:43:27 +02007310#if defined(MBEDTLS_SHA512_C) && !defined(MBEDTLS_SHA512_NO_SHA384)
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +02007311 case MBEDTLS_MD_SHA384:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01007312 return MBEDTLS_SSL_HASH_SHA384;
Gilles Peskinec54010c2021-05-12 22:52:09 +02007313#endif
7314#if defined(MBEDTLS_SHA512_C)
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +02007315 case MBEDTLS_MD_SHA512:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01007316 return MBEDTLS_SSL_HASH_SHA512;
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +02007317#endif
7318 default:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01007319 return MBEDTLS_SSL_HASH_NONE;
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +02007320 }
7321}
7322
Manuel Pégourié-Gonnardb541da62015-06-17 11:43:30 +02007323#if defined(MBEDTLS_ECP_C)
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01007324/*
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +02007325 * Check if a curve proposed by the peer is in our list.
Manuel Pégourié-Gonnard9d412d82015-06-17 12:10:46 +02007326 * Return 0 if we're willing to use it, -1 otherwise.
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01007327 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01007328int mbedtls_ssl_check_curve(const mbedtls_ssl_context *ssl, mbedtls_ecp_group_id grp_id)
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01007329{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007330 const mbedtls_ecp_group_id *gid;
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01007331
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01007332 if (ssl->conf->curve_list == NULL) {
7333 return -1;
7334 }
Manuel Pégourié-Gonnard9d412d82015-06-17 12:10:46 +02007335
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01007336 for (gid = ssl->conf->curve_list; *gid != MBEDTLS_ECP_DP_NONE; gid++) {
7337 if (*gid == grp_id) {
7338 return 0;
7339 }
7340 }
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01007341
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01007342 return -1;
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01007343}
Manuel Pégourié-Gonnard298d6cc2022-02-14 11:34:47 +01007344
7345/*
7346 * Same as mbedtls_ssl_check_curve() but takes a TLS ID for the curve.
7347 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01007348int mbedtls_ssl_check_curve_tls_id(const mbedtls_ssl_context *ssl, uint16_t tls_id)
Manuel Pégourié-Gonnard298d6cc2022-02-14 11:34:47 +01007349{
7350 const mbedtls_ecp_curve_info *curve_info =
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01007351 mbedtls_ecp_curve_info_from_tls_id(tls_id);
7352 if (curve_info == NULL) {
7353 return -1;
7354 }
7355 return mbedtls_ssl_check_curve(ssl, curve_info->grp_id);
Manuel Pégourié-Gonnard298d6cc2022-02-14 11:34:47 +01007356}
Manuel Pégourié-Gonnardb541da62015-06-17 11:43:30 +02007357#endif /* MBEDTLS_ECP_C */
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02007358
Gilles Peskineeccd8882020-03-10 12:19:08 +01007359#if defined(MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED)
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +02007360/*
7361 * Check if a hash proposed by the peer is in our list.
7362 * Return 0 if we're willing to use it, -1 otherwise.
7363 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01007364int mbedtls_ssl_check_sig_hash(const mbedtls_ssl_context *ssl,
7365 mbedtls_md_type_t md)
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +02007366{
7367 const int *cur;
7368
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01007369 if (ssl->conf->sig_hashes == NULL) {
7370 return -1;
7371 }
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +02007372
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01007373 for (cur = ssl->conf->sig_hashes; *cur != MBEDTLS_MD_NONE; cur++) {
7374 if (*cur == (int) md) {
7375 return 0;
7376 }
7377 }
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +02007378
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01007379 return -1;
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +02007380}
Gilles Peskineeccd8882020-03-10 12:19:08 +01007381#endif /* MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED */
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +02007382
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007383#if defined(MBEDTLS_X509_CRT_PARSE_C)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01007384int mbedtls_ssl_check_cert_usage(const mbedtls_x509_crt *cert,
7385 const mbedtls_ssl_ciphersuite_t *ciphersuite,
7386 int cert_endpoint,
7387 uint32_t *flags)
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02007388{
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +01007389 int ret = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007390#if defined(MBEDTLS_X509_CHECK_KEY_USAGE)
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02007391 int usage = 0;
7392#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007393#if defined(MBEDTLS_X509_CHECK_EXTENDED_KEY_USAGE)
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +02007394 const char *ext_oid;
7395 size_t ext_len;
7396#endif
7397
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007398#if !defined(MBEDTLS_X509_CHECK_KEY_USAGE) && \
7399 !defined(MBEDTLS_X509_CHECK_EXTENDED_KEY_USAGE)
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +02007400 ((void) cert);
7401 ((void) cert_endpoint);
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +01007402 ((void) flags);
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +02007403#endif
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02007404
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007405#if defined(MBEDTLS_X509_CHECK_KEY_USAGE)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01007406 if (cert_endpoint == MBEDTLS_SSL_IS_SERVER) {
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02007407 /* Server part of the key exchange */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01007408 switch (ciphersuite->key_exchange) {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007409 case MBEDTLS_KEY_EXCHANGE_RSA:
7410 case MBEDTLS_KEY_EXCHANGE_RSA_PSK:
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +01007411 usage = MBEDTLS_X509_KU_KEY_ENCIPHERMENT;
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02007412 break;
7413
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007414 case MBEDTLS_KEY_EXCHANGE_DHE_RSA:
7415 case MBEDTLS_KEY_EXCHANGE_ECDHE_RSA:
7416 case MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA:
7417 usage = MBEDTLS_X509_KU_DIGITAL_SIGNATURE;
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02007418 break;
7419
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007420 case MBEDTLS_KEY_EXCHANGE_ECDH_RSA:
7421 case MBEDTLS_KEY_EXCHANGE_ECDH_ECDSA:
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +01007422 usage = MBEDTLS_X509_KU_KEY_AGREEMENT;
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02007423 break;
7424
7425 /* Don't use default: we want warnings when adding new values */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007426 case MBEDTLS_KEY_EXCHANGE_NONE:
7427 case MBEDTLS_KEY_EXCHANGE_PSK:
7428 case MBEDTLS_KEY_EXCHANGE_DHE_PSK:
7429 case MBEDTLS_KEY_EXCHANGE_ECDHE_PSK:
Manuel Pégourié-Gonnard557535d2015-09-15 17:53:32 +02007430 case MBEDTLS_KEY_EXCHANGE_ECJPAKE:
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02007431 usage = 0;
7432 }
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01007433 } else {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007434 /* Client auth: we only implement rsa_sign and mbedtls_ecdsa_sign for now */
7435 usage = MBEDTLS_X509_KU_DIGITAL_SIGNATURE;
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02007436 }
7437
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01007438 if (mbedtls_x509_crt_check_key_usage(cert, usage) != 0) {
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +01007439 *flags |= MBEDTLS_X509_BADCERT_KEY_USAGE;
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +01007440 ret = -1;
7441 }
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +02007442#else
7443 ((void) ciphersuite);
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007444#endif /* MBEDTLS_X509_CHECK_KEY_USAGE */
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02007445
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007446#if defined(MBEDTLS_X509_CHECK_EXTENDED_KEY_USAGE)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01007447 if (cert_endpoint == MBEDTLS_SSL_IS_SERVER) {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007448 ext_oid = MBEDTLS_OID_SERVER_AUTH;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01007449 ext_len = MBEDTLS_OID_SIZE(MBEDTLS_OID_SERVER_AUTH);
7450 } else {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007451 ext_oid = MBEDTLS_OID_CLIENT_AUTH;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01007452 ext_len = MBEDTLS_OID_SIZE(MBEDTLS_OID_CLIENT_AUTH);
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +02007453 }
7454
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01007455 if (mbedtls_x509_crt_check_extended_key_usage(cert, ext_oid, ext_len) != 0) {
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +01007456 *flags |= MBEDTLS_X509_BADCERT_EXT_KEY_USAGE;
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +01007457 ret = -1;
7458 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007459#endif /* MBEDTLS_X509_CHECK_EXTENDED_KEY_USAGE */
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +02007460
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01007461 return ret;
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02007462}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007463#endif /* MBEDTLS_X509_CRT_PARSE_C */
Manuel Pégourié-Gonnard3a306b92014-04-29 15:11:17 +02007464
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01007465int mbedtls_ssl_set_calc_verify_md(mbedtls_ssl_context *ssl, int md)
Simon Butcher99000142016-10-13 17:21:01 +01007466{
7467#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01007468 if (ssl->minor_ver != MBEDTLS_SSL_MINOR_VERSION_3) {
Simon Butcher99000142016-10-13 17:21:01 +01007469 return MBEDTLS_ERR_SSL_INVALID_VERIFY_HASH;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01007470 }
Simon Butcher99000142016-10-13 17:21:01 +01007471
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01007472 switch (md) {
Simon Butcher99000142016-10-13 17:21:01 +01007473#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1)
7474#if defined(MBEDTLS_MD5_C)
7475 case MBEDTLS_SSL_HASH_MD5:
Janos Follath182013f2016-10-25 10:50:22 +01007476 return MBEDTLS_ERR_SSL_INVALID_VERIFY_HASH;
Simon Butcher99000142016-10-13 17:21:01 +01007477#endif
7478#if defined(MBEDTLS_SHA1_C)
7479 case MBEDTLS_SSL_HASH_SHA1:
7480 ssl->handshake->calc_verify = ssl_calc_verify_tls;
7481 break;
7482#endif
7483#endif /* MBEDTLS_SSL_PROTO_TLS1 || MBEDTLS_SSL_PROTO_TLS1_1 */
Gilles Peskined2d59372021-05-12 22:43:27 +02007484#if defined(MBEDTLS_SHA512_C) && !defined(MBEDTLS_SHA512_NO_SHA384)
Simon Butcher99000142016-10-13 17:21:01 +01007485 case MBEDTLS_SSL_HASH_SHA384:
7486 ssl->handshake->calc_verify = ssl_calc_verify_tls_sha384;
7487 break;
7488#endif
7489#if defined(MBEDTLS_SHA256_C)
7490 case MBEDTLS_SSL_HASH_SHA256:
7491 ssl->handshake->calc_verify = ssl_calc_verify_tls_sha256;
7492 break;
7493#endif
7494 default:
7495 return MBEDTLS_ERR_SSL_INVALID_VERIFY_HASH;
7496 }
7497
7498 return 0;
7499#else /* !MBEDTLS_SSL_PROTO_TLS1_2 */
7500 (void) ssl;
7501 (void) md;
7502
7503 return MBEDTLS_ERR_SSL_INVALID_VERIFY_HASH;
7504#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
7505}
7506
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +01007507#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1) || \
7508 defined(MBEDTLS_SSL_PROTO_TLS1_1)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01007509int mbedtls_ssl_get_key_exchange_md_ssl_tls(mbedtls_ssl_context *ssl,
7510 unsigned char *output,
7511 unsigned char *data, size_t data_len)
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +01007512{
7513 int ret = 0;
7514 mbedtls_md5_context mbedtls_md5;
7515 mbedtls_sha1_context mbedtls_sha1;
7516
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01007517 mbedtls_md5_init(&mbedtls_md5);
7518 mbedtls_sha1_init(&mbedtls_sha1);
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +01007519
7520 /*
7521 * digitally-signed struct {
7522 * opaque md5_hash[16];
7523 * opaque sha_hash[20];
7524 * };
7525 *
7526 * md5_hash
7527 * MD5(ClientHello.random + ServerHello.random
7528 * + ServerParams);
7529 * sha_hash
7530 * SHA(ClientHello.random + ServerHello.random
7531 * + ServerParams);
7532 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01007533 if ((ret = mbedtls_md5_starts_ret(&mbedtls_md5)) != 0) {
7534 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_md5_starts_ret", ret);
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +01007535 goto exit;
7536 }
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01007537 if ((ret = mbedtls_md5_update_ret(&mbedtls_md5,
7538 ssl->handshake->randbytes, 64)) != 0) {
7539 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_md5_update_ret", ret);
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +01007540 goto exit;
7541 }
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01007542 if ((ret = mbedtls_md5_update_ret(&mbedtls_md5, data, data_len)) != 0) {
7543 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_md5_update_ret", ret);
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +01007544 goto exit;
7545 }
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01007546 if ((ret = mbedtls_md5_finish_ret(&mbedtls_md5, output)) != 0) {
7547 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_md5_finish_ret", ret);
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +01007548 goto exit;
7549 }
7550
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01007551 if ((ret = mbedtls_sha1_starts_ret(&mbedtls_sha1)) != 0) {
7552 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_sha1_starts_ret", ret);
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +01007553 goto exit;
7554 }
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01007555 if ((ret = mbedtls_sha1_update_ret(&mbedtls_sha1,
7556 ssl->handshake->randbytes, 64)) != 0) {
7557 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_sha1_update_ret", ret);
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +01007558 goto exit;
7559 }
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01007560 if ((ret = mbedtls_sha1_update_ret(&mbedtls_sha1, data,
7561 data_len)) != 0) {
7562 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_sha1_update_ret", ret);
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +01007563 goto exit;
7564 }
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01007565 if ((ret = mbedtls_sha1_finish_ret(&mbedtls_sha1,
7566 output + 16)) != 0) {
7567 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_sha1_finish_ret", ret);
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +01007568 goto exit;
7569 }
7570
7571exit:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01007572 mbedtls_md5_free(&mbedtls_md5);
7573 mbedtls_sha1_free(&mbedtls_sha1);
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +01007574
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01007575 if (ret != 0) {
7576 mbedtls_ssl_send_alert_message(ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
7577 MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR);
7578 }
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +01007579
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01007580 return ret;
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +01007581
7582}
7583#endif /* MBEDTLS_SSL_PROTO_SSL3 || MBEDTLS_SSL_PROTO_TLS1 || \
7584 MBEDTLS_SSL_PROTO_TLS1_1 */
7585
7586#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1) || \
7587 defined(MBEDTLS_SSL_PROTO_TLS1_2)
Andrzej Kurekd6db9be2019-01-10 05:27:10 -05007588
7589#if defined(MBEDTLS_USE_PSA_CRYPTO)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01007590int mbedtls_ssl_get_key_exchange_md_tls1_2(mbedtls_ssl_context *ssl,
7591 unsigned char *hash, size_t *hashlen,
7592 unsigned char *data, size_t data_len,
7593 mbedtls_md_type_t md_alg)
Andrzej Kurekd6db9be2019-01-10 05:27:10 -05007594{
Andrzej Kurek814feff2019-01-14 04:35:19 -05007595 psa_status_t status;
Jaeden Amero34973232019-02-20 10:32:28 +00007596 psa_hash_operation_t hash_operation = PSA_HASH_OPERATION_INIT;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01007597 psa_algorithm_t hash_alg = mbedtls_psa_translate_md(md_alg);
Andrzej Kurekd6db9be2019-01-10 05:27:10 -05007598
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01007599 MBEDTLS_SSL_DEBUG_MSG(3, ("Perform PSA-based computation of digest of ServerKeyExchange"));
Andrzej Kurek814feff2019-01-14 04:35:19 -05007600
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01007601 if ((status = psa_hash_setup(&hash_operation,
7602 hash_alg)) != PSA_SUCCESS) {
7603 MBEDTLS_SSL_DEBUG_RET(1, "psa_hash_setup", status);
Andrzej Kurekd6db9be2019-01-10 05:27:10 -05007604 goto exit;
7605 }
7606
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01007607 if ((status = psa_hash_update(&hash_operation, ssl->handshake->randbytes,
7608 64)) != PSA_SUCCESS) {
7609 MBEDTLS_SSL_DEBUG_RET(1, "psa_hash_update", status);
Andrzej Kurekd6db9be2019-01-10 05:27:10 -05007610 goto exit;
7611 }
7612
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01007613 if ((status = psa_hash_update(&hash_operation,
7614 data, data_len)) != PSA_SUCCESS) {
7615 MBEDTLS_SSL_DEBUG_RET(1, "psa_hash_update", status);
Andrzej Kurekd6db9be2019-01-10 05:27:10 -05007616 goto exit;
7617 }
7618
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01007619 if ((status = psa_hash_finish(&hash_operation, hash, PSA_HASH_MAX_SIZE,
7620 hashlen)) != PSA_SUCCESS) {
7621 MBEDTLS_SSL_DEBUG_RET(1, "psa_hash_finish", status);
7622 goto exit;
Andrzej Kurekd6db9be2019-01-10 05:27:10 -05007623 }
7624
7625exit:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01007626 if (status != PSA_SUCCESS) {
7627 mbedtls_ssl_send_alert_message(ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
7628 MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR);
7629 switch (status) {
Andrzej Kurekd6db9be2019-01-10 05:27:10 -05007630 case PSA_ERROR_NOT_SUPPORTED:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01007631 return MBEDTLS_ERR_MD_FEATURE_UNAVAILABLE;
Andrzej Kurek814feff2019-01-14 04:35:19 -05007632 case PSA_ERROR_BAD_STATE: /* Intentional fallthrough */
Andrzej Kurekd6db9be2019-01-10 05:27:10 -05007633 case PSA_ERROR_BUFFER_TOO_SMALL:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01007634 return MBEDTLS_ERR_MD_BAD_INPUT_DATA;
Andrzej Kurekd6db9be2019-01-10 05:27:10 -05007635 case PSA_ERROR_INSUFFICIENT_MEMORY:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01007636 return MBEDTLS_ERR_MD_ALLOC_FAILED;
Andrzej Kurekd6db9be2019-01-10 05:27:10 -05007637 default:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01007638 return MBEDTLS_ERR_MD_HW_ACCEL_FAILED;
Andrzej Kurekd6db9be2019-01-10 05:27:10 -05007639 }
7640 }
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01007641 return 0;
Andrzej Kurekd6db9be2019-01-10 05:27:10 -05007642}
7643
7644#else
7645
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01007646int mbedtls_ssl_get_key_exchange_md_tls1_2(mbedtls_ssl_context *ssl,
7647 unsigned char *hash, size_t *hashlen,
7648 unsigned char *data, size_t data_len,
7649 mbedtls_md_type_t md_alg)
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +01007650{
7651 int ret = 0;
7652 mbedtls_md_context_t ctx;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01007653 const mbedtls_md_info_t *md_info = mbedtls_md_info_from_type(md_alg);
7654 *hashlen = mbedtls_md_get_size(md_info);
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +01007655
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01007656 MBEDTLS_SSL_DEBUG_MSG(3, ("Perform mbedtls-based computation of digest of ServerKeyExchange"));
Andrzej Kurek814feff2019-01-14 04:35:19 -05007657
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01007658 mbedtls_md_init(&ctx);
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +01007659
7660 /*
7661 * digitally-signed struct {
7662 * opaque client_random[32];
7663 * opaque server_random[32];
7664 * ServerDHParams params;
7665 * };
7666 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01007667 if ((ret = mbedtls_md_setup(&ctx, md_info, 0)) != 0) {
7668 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_md_setup", ret);
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +01007669 goto exit;
7670 }
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01007671 if ((ret = mbedtls_md_starts(&ctx)) != 0) {
7672 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_md_starts", ret);
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +01007673 goto exit;
7674 }
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01007675 if ((ret = mbedtls_md_update(&ctx, ssl->handshake->randbytes, 64)) != 0) {
7676 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_md_update", ret);
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +01007677 goto exit;
7678 }
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01007679 if ((ret = mbedtls_md_update(&ctx, data, data_len)) != 0) {
7680 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_md_update", ret);
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +01007681 goto exit;
7682 }
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01007683 if ((ret = mbedtls_md_finish(&ctx, hash)) != 0) {
7684 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_md_finish", ret);
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +01007685 goto exit;
7686 }
7687
7688exit:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01007689 mbedtls_md_free(&ctx);
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +01007690
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01007691 if (ret != 0) {
7692 mbedtls_ssl_send_alert_message(ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
7693 MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR);
7694 }
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +01007695
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01007696 return ret;
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +01007697}
Andrzej Kurekd6db9be2019-01-10 05:27:10 -05007698#endif /* MBEDTLS_USE_PSA_CRYPTO */
7699
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +01007700#endif /* MBEDTLS_SSL_PROTO_TLS1 || MBEDTLS_SSL_PROTO_TLS1_1 || \
7701 MBEDTLS_SSL_PROTO_TLS1_2 */
7702
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007703#endif /* MBEDTLS_SSL_TLS_C */