blob: 01acd1dbc659983255d0da473bd7108f96e84b5e [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
43#if defined(MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED)
44/** Whether mbedtls_ssl_set_hostname() has been called.
45 *
46 * \param[in] ssl SSL context
47 *
48 * \return \c 1 if mbedtls_ssl_set_hostname() has been called on \p ssl
49 * (including `mbedtls_ssl_set_hostname(ssl, NULL)`),
50 * otherwise \c 0.
51 */
52static int mbedtls_ssl_has_set_hostname_been_called(
53 const mbedtls_ssl_context *ssl)
54{
55 /* We can't tell the difference between the case where
56 * mbedtls_ssl_set_hostname() has not been called at all, and
57 * the case where it was last called with NULL. For the time
58 * being, we assume the latter, i.e. we behave as if there had
59 * been an implicit call to mbedtls_ssl_set_hostname(ssl, NULL). */
60 return ssl->hostname != NULL;
61}
62#endif
63
64/* Micro-optimization: don't export this function if it isn't needed outside
65 * of this source file. */
66#if !defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
67static
68#endif
69const char *mbedtls_ssl_get_hostname_pointer(const mbedtls_ssl_context *ssl)
70{
71 return ssl->hostname;
72}
73
74static void mbedtls_ssl_free_hostname(mbedtls_ssl_context *ssl)
75{
76 if (ssl->hostname != NULL) {
77 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
102 /* Passing NULL as hostname shall clear the old one */
103
104 if (hostname == NULL) {
105 ssl->hostname = NULL;
106 } else {
107 ssl->hostname = mbedtls_calloc(1, hostname_len + 1);
108 if (ssl->hostname == NULL) {
109 return MBEDTLS_ERR_SSL_ALLOC_FAILED;
110 }
111
112 memcpy(ssl->hostname, hostname, hostname_len);
113
114 ssl->hostname[hostname_len] = '\0';
115 }
116
117 return 0;
118}
119#endif /* MBEDTLS_X509_CRT_PARSE_C */
120
Manuel Pégourié-Gonnard286a1362015-05-13 16:22:05 +0200121#if defined(MBEDTLS_SSL_PROTO_DTLS)
Hanno Becker2b1e3542018-08-06 11:19:13 +0100122
Hanno Beckera0e20d02019-05-15 14:03:01 +0100123#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Beckerf8542cf2019-04-09 15:22:03 +0100124/* Top-level Connection ID API */
125
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100126int mbedtls_ssl_conf_cid(mbedtls_ssl_config *conf,
127 size_t len,
128 int ignore_other_cid)
Hanno Beckerad4a1372019-05-03 13:06:44 +0100129{
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100130 if (len > MBEDTLS_SSL_CID_IN_LEN_MAX) {
131 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
132 }
Hanno Beckerad4a1372019-05-03 13:06:44 +0100133
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100134 if (ignore_other_cid != MBEDTLS_SSL_UNEXPECTED_CID_FAIL &&
135 ignore_other_cid != MBEDTLS_SSL_UNEXPECTED_CID_IGNORE) {
136 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
Hanno Becker611ac772019-05-14 11:45:26 +0100137 }
138
139 conf->ignore_unexpected_cid = ignore_other_cid;
Hanno Beckerad4a1372019-05-03 13:06:44 +0100140 conf->cid_len = len;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100141 return 0;
Hanno Beckerad4a1372019-05-03 13:06:44 +0100142}
143
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100144int mbedtls_ssl_set_cid(mbedtls_ssl_context *ssl,
145 int enable,
146 unsigned char const *own_cid,
147 size_t own_cid_len)
Hanno Beckerf8542cf2019-04-09 15:22:03 +0100148{
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100149 if (ssl->conf->transport != MBEDTLS_SSL_TRANSPORT_DATAGRAM) {
150 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
151 }
Hanno Becker76a79ab2019-05-03 14:38:32 +0100152
Hanno Beckerca092242019-04-25 16:01:49 +0100153 ssl->negotiate_cid = enable;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100154 if (enable == MBEDTLS_SSL_CID_DISABLED) {
155 MBEDTLS_SSL_DEBUG_MSG(3, ("Disable use of CID extension."));
156 return 0;
Hanno Beckerca092242019-04-25 16:01:49 +0100157 }
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100158 MBEDTLS_SSL_DEBUG_MSG(3, ("Enable use of CID extension."));
159 MBEDTLS_SSL_DEBUG_BUF(3, "Own CID", own_cid, own_cid_len);
Hanno Beckerca092242019-04-25 16:01:49 +0100160
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100161 if (own_cid_len != ssl->conf->cid_len) {
162 MBEDTLS_SSL_DEBUG_MSG(3, ("CID length %u does not match CID length %u in config",
163 (unsigned) own_cid_len,
164 (unsigned) ssl->conf->cid_len));
165 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
Hanno Beckerca092242019-04-25 16:01:49 +0100166 }
167
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100168 memcpy(ssl->own_cid, own_cid, own_cid_len);
Hanno Beckerb7ee0cf2019-04-30 14:07:31 +0100169 /* Truncation is not an issue here because
170 * MBEDTLS_SSL_CID_IN_LEN_MAX at most 255. */
171 ssl->own_cid_len = (uint8_t) own_cid_len;
Hanno Beckerca092242019-04-25 16:01:49 +0100172
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100173 return 0;
Hanno Beckerf8542cf2019-04-09 15:22:03 +0100174}
175
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100176int mbedtls_ssl_get_peer_cid(mbedtls_ssl_context *ssl,
177 int *enabled,
178 unsigned char peer_cid[MBEDTLS_SSL_CID_OUT_LEN_MAX],
179 size_t *peer_cid_len)
Hanno Beckerf8542cf2019-04-09 15:22:03 +0100180{
Hanno Beckerf8542cf2019-04-09 15:22:03 +0100181 *enabled = MBEDTLS_SSL_CID_DISABLED;
Hanno Beckerb1f89cd2019-04-26 17:08:02 +0100182
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100183 if (ssl->conf->transport != MBEDTLS_SSL_TRANSPORT_DATAGRAM ||
184 ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER) {
185 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
Hanno Becker76a79ab2019-05-03 14:38:32 +0100186 }
Hanno Beckerb1f89cd2019-04-26 17:08:02 +0100187
Hanno Beckerc5f24222019-05-03 12:54:52 +0100188 /* We report MBEDTLS_SSL_CID_DISABLED in case the CID extensions
189 * were used, but client and server requested the empty CID.
190 * This is indistinguishable from not using the CID extension
191 * in the first place. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100192 if (ssl->transform_in->in_cid_len == 0 &&
193 ssl->transform_in->out_cid_len == 0) {
194 return 0;
Hanno Beckerb1f89cd2019-04-26 17:08:02 +0100195 }
196
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100197 if (peer_cid_len != NULL) {
Hanno Becker615ef172019-05-22 16:50:35 +0100198 *peer_cid_len = ssl->transform_in->out_cid_len;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100199 if (peer_cid != NULL) {
200 memcpy(peer_cid, ssl->transform_in->out_cid,
201 ssl->transform_in->out_cid_len);
Hanno Becker615ef172019-05-22 16:50:35 +0100202 }
203 }
Hanno Beckerb1f89cd2019-04-26 17:08:02 +0100204
205 *enabled = MBEDTLS_SSL_CID_ENABLED;
206
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100207 return 0;
Hanno Beckerf8542cf2019-04-09 15:22:03 +0100208}
Hanno Beckera0e20d02019-05-15 14:03:01 +0100209#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Beckerf8542cf2019-04-09 15:22:03 +0100210
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200211#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +0200212
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200213#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
Manuel Pégourié-Gonnard581e6b62013-07-18 12:32:27 +0200214/*
215 * Convert max_fragment_length codes to length.
216 * RFC 6066 says:
217 * enum{
218 * 2^9(1), 2^10(2), 2^11(3), 2^12(4), (255)
219 * } MaxFragmentLength;
220 * and we add 0 -> extension unused
221 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100222static unsigned int ssl_mfl_code_to_length(int mfl)
Manuel Pégourié-Gonnard581e6b62013-07-18 12:32:27 +0200223{
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100224 switch (mfl) {
225 case MBEDTLS_SSL_MAX_FRAG_LEN_NONE:
226 return MBEDTLS_TLS_EXT_ADV_CONTENT_LEN;
227 case MBEDTLS_SSL_MAX_FRAG_LEN_512:
228 return 512;
229 case MBEDTLS_SSL_MAX_FRAG_LEN_1024:
230 return 1024;
231 case MBEDTLS_SSL_MAX_FRAG_LEN_2048:
232 return 2048;
233 case MBEDTLS_SSL_MAX_FRAG_LEN_4096:
234 return 4096;
235 default:
236 return MBEDTLS_TLS_EXT_ADV_CONTENT_LEN;
Angus Grattond8213d02016-05-25 20:56:48 +1000237 }
238}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200239#endif /* MBEDTLS_SSL_MAX_FRAGMENT_LENGTH */
Manuel Pégourié-Gonnard581e6b62013-07-18 12:32:27 +0200240
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100241int mbedtls_ssl_session_copy(mbedtls_ssl_session *dst,
242 const mbedtls_ssl_session *src)
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200243{
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100244 mbedtls_ssl_session_free(dst);
245 memcpy(dst, src, sizeof(mbedtls_ssl_session));
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200246
吴敬辉0f6c6bc2021-11-29 10:46:35 +0800247#if defined(MBEDTLS_SSL_SESSION_TICKETS) && defined(MBEDTLS_SSL_CLI_C)
248 dst->ticket = NULL;
249#endif
250
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200251#if defined(MBEDTLS_X509_CRT_PARSE_C)
Hanno Becker6d1986e2019-02-07 12:27:42 +0000252
253#if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100254 if (src->peer_cert != NULL) {
Janos Follath865b3eb2019-12-16 11:46:15 +0000255 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker2292d1f2013-09-15 17:06:49 +0200256
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100257 dst->peer_cert = mbedtls_calloc(1, sizeof(mbedtls_x509_crt));
258 if (dst->peer_cert == NULL) {
259 return MBEDTLS_ERR_SSL_ALLOC_FAILED;
260 }
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200261
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100262 mbedtls_x509_crt_init(dst->peer_cert);
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200263
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100264 if ((ret = mbedtls_x509_crt_parse_der(dst->peer_cert, src->peer_cert->raw.p,
265 src->peer_cert->raw.len)) != 0) {
266 mbedtls_free(dst->peer_cert);
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200267 dst->peer_cert = NULL;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100268 return ret;
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200269 }
270 }
Hanno Becker6d1986e2019-02-07 12:27:42 +0000271#else /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100272 if (src->peer_cert_digest != NULL) {
Hanno Becker9198ad12019-02-05 17:00:50 +0000273 dst->peer_cert_digest =
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100274 mbedtls_calloc(1, src->peer_cert_digest_len);
275 if (dst->peer_cert_digest == NULL) {
276 return MBEDTLS_ERR_SSL_ALLOC_FAILED;
277 }
Hanno Becker9198ad12019-02-05 17:00:50 +0000278
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100279 memcpy(dst->peer_cert_digest, src->peer_cert_digest,
280 src->peer_cert_digest_len);
Hanno Becker9198ad12019-02-05 17:00:50 +0000281 dst->peer_cert_digest_type = src->peer_cert_digest_type;
Hanno Beckeraccc5992019-02-25 10:06:59 +0000282 dst->peer_cert_digest_len = src->peer_cert_digest_len;
Hanno Becker9198ad12019-02-05 17:00:50 +0000283 }
284#endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
285
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200286#endif /* MBEDTLS_X509_CRT_PARSE_C */
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200287
Manuel Pégourié-Gonnardb596abf2015-05-20 10:45:29 +0200288#if defined(MBEDTLS_SSL_SESSION_TICKETS) && defined(MBEDTLS_SSL_CLI_C)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100289 if (src->ticket != NULL) {
290 dst->ticket = mbedtls_calloc(1, src->ticket_len);
291 if (dst->ticket == NULL) {
292 return MBEDTLS_ERR_SSL_ALLOC_FAILED;
293 }
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200294
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100295 memcpy(dst->ticket, src->ticket, src->ticket_len);
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200296 }
Manuel Pégourié-Gonnardb596abf2015-05-20 10:45:29 +0200297#endif /* MBEDTLS_SSL_SESSION_TICKETS && MBEDTLS_SSL_CLI_C */
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200298
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100299 return 0;
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200300}
301
Andrzej Kurek0afa2a12020-03-03 10:39:58 -0500302#if defined(MBEDTLS_SSL_VARIABLE_BUFFER_LENGTH)
Manuel Pégourié-Gonnardd904d662022-06-17 10:24:00 +0200303MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100304static int resize_buffer(unsigned char **buffer, size_t len_new, size_t *len_old)
Andrzej Kurek0afa2a12020-03-03 10:39:58 -0500305{
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100306 unsigned char *resized_buffer = mbedtls_calloc(1, len_new);
307 if (resized_buffer == NULL) {
Andrzej Kurek0afa2a12020-03-03 10:39:58 -0500308 return -1;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100309 }
Andrzej Kurek0afa2a12020-03-03 10:39:58 -0500310
311 /* We want to copy len_new bytes when downsizing the buffer, and
312 * len_old bytes when upsizing, so we choose the smaller of two sizes,
313 * to fit one buffer into another. Size checks, ensuring that no data is
314 * lost, are done outside of this function. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100315 memcpy(resized_buffer, *buffer,
316 (len_new < *len_old) ? len_new : *len_old);
317 mbedtls_platform_zeroize(*buffer, *len_old);
318 mbedtls_free(*buffer);
Andrzej Kurek0afa2a12020-03-03 10:39:58 -0500319
320 *buffer = resized_buffer;
321 *len_old = len_new;
322
323 return 0;
324}
Andrzej Kurek4a063792020-10-21 15:08:44 +0200325
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100326static void handle_buffer_resizing(mbedtls_ssl_context *ssl, int downsizing,
327 size_t in_buf_new_len,
328 size_t out_buf_new_len)
Andrzej Kurek4a063792020-10-21 15:08:44 +0200329{
330 int modified = 0;
331 size_t written_in = 0, iv_offset_in = 0, len_offset_in = 0;
332 size_t written_out = 0, iv_offset_out = 0, len_offset_out = 0;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100333 if (ssl->in_buf != NULL) {
Andrzej Kurek4a063792020-10-21 15:08:44 +0200334 written_in = ssl->in_msg - ssl->in_buf;
335 iv_offset_in = ssl->in_iv - ssl->in_buf;
336 len_offset_in = ssl->in_len - ssl->in_buf;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100337 if (downsizing ?
Andrzej Kurek4a063792020-10-21 15:08:44 +0200338 ssl->in_buf_len > in_buf_new_len && ssl->in_left < in_buf_new_len :
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100339 ssl->in_buf_len < in_buf_new_len) {
340 if (resize_buffer(&ssl->in_buf, in_buf_new_len, &ssl->in_buf_len) != 0) {
341 MBEDTLS_SSL_DEBUG_MSG(1, ("input buffer resizing failed - out of memory"));
342 } else {
343 MBEDTLS_SSL_DEBUG_MSG(2, ("Reallocating in_buf to %" MBEDTLS_PRINTF_SIZET,
344 in_buf_new_len));
Andrzej Kurek4a063792020-10-21 15:08:44 +0200345 modified = 1;
346 }
347 }
348 }
349
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100350 if (ssl->out_buf != NULL) {
Andrzej Kurek4a063792020-10-21 15:08:44 +0200351 written_out = ssl->out_msg - ssl->out_buf;
352 iv_offset_out = ssl->out_iv - ssl->out_buf;
353 len_offset_out = ssl->out_len - ssl->out_buf;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100354 if (downsizing ?
Andrzej Kurek4a063792020-10-21 15:08:44 +0200355 ssl->out_buf_len > out_buf_new_len && ssl->out_left < out_buf_new_len :
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100356 ssl->out_buf_len < out_buf_new_len) {
357 if (resize_buffer(&ssl->out_buf, out_buf_new_len, &ssl->out_buf_len) != 0) {
358 MBEDTLS_SSL_DEBUG_MSG(1, ("output buffer resizing failed - out of memory"));
359 } else {
360 MBEDTLS_SSL_DEBUG_MSG(2, ("Reallocating out_buf to %" MBEDTLS_PRINTF_SIZET,
361 out_buf_new_len));
Andrzej Kurek4a063792020-10-21 15:08:44 +0200362 modified = 1;
363 }
364 }
365 }
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100366 if (modified) {
Andrzej Kurek4a063792020-10-21 15:08:44 +0200367 /* Update pointers here to avoid doing it twice. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100368 mbedtls_ssl_reset_in_out_pointers(ssl);
Andrzej Kurek4a063792020-10-21 15:08:44 +0200369 /* Fields below might not be properly updated with record
370 * splitting or with CID, so they are manually updated here. */
371 ssl->out_msg = ssl->out_buf + written_out;
372 ssl->out_len = ssl->out_buf + len_offset_out;
373 ssl->out_iv = ssl->out_buf + iv_offset_out;
374
375 ssl->in_msg = ssl->in_buf + written_in;
376 ssl->in_len = ssl->in_buf + len_offset_in;
377 ssl->in_iv = ssl->in_buf + iv_offset_in;
378 }
379}
Andrzej Kurek0afa2a12020-03-03 10:39:58 -0500380#endif /* MBEDTLS_SSL_VARIABLE_BUFFER_LENGTH */
381
Paul Bakker5121ce52009-01-03 21:22:43 +0000382/*
383 * Key material generation
384 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200385#if defined(MBEDTLS_SSL_PROTO_SSL3)
Manuel Pégourié-Gonnardd904d662022-06-17 10:24:00 +0200386MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100387static int ssl3_prf(const unsigned char *secret, size_t slen,
388 const char *label,
389 const unsigned char *random, size_t rlen,
390 unsigned char *dstbuf, size_t dlen)
Paul Bakker5f70b252012-09-13 14:23:06 +0000391{
Andres Amaya Garcia33952502017-07-20 16:29:16 +0100392 int ret = 0;
Paul Bakker5f70b252012-09-13 14:23:06 +0000393 size_t i;
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +0200394 mbedtls_md5_context md5;
395 mbedtls_sha1_context sha1;
Paul Bakker5f70b252012-09-13 14:23:06 +0000396 unsigned char padding[16];
397 unsigned char sha1sum[20];
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100398 ((void) label);
Paul Bakker5f70b252012-09-13 14:23:06 +0000399
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100400 mbedtls_md5_init(&md5);
401 mbedtls_sha1_init(&sha1);
Paul Bakker5b4af392014-06-26 12:09:34 +0200402
Paul Bakker5f70b252012-09-13 14:23:06 +0000403 /*
404 * SSLv3:
405 * block =
406 * MD5( secret + SHA1( 'A' + secret + random ) ) +
407 * MD5( secret + SHA1( 'BB' + secret + random ) ) +
408 * MD5( secret + SHA1( 'CCC' + secret + random ) ) +
409 * ...
410 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100411 for (i = 0; i < dlen / 16; i++) {
412 memset(padding, (unsigned char) ('A' + i), 1 + i);
Paul Bakker5f70b252012-09-13 14:23:06 +0000413
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100414 if ((ret = mbedtls_sha1_starts_ret(&sha1)) != 0) {
Andres Amaya Garcia1a607a12017-06-29 17:09:42 +0100415 goto exit;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100416 }
417 if ((ret = mbedtls_sha1_update_ret(&sha1, padding, 1 + i)) != 0) {
Andres Amaya Garcia1a607a12017-06-29 17:09:42 +0100418 goto exit;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100419 }
420 if ((ret = mbedtls_sha1_update_ret(&sha1, secret, slen)) != 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, random, rlen)) != 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_finish_ret(&sha1, sha1sum)) != 0) {
Andres Amaya Garcia1a607a12017-06-29 17:09:42 +0100427 goto exit;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100428 }
Paul Bakker5f70b252012-09-13 14:23:06 +0000429
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100430 if ((ret = mbedtls_md5_starts_ret(&md5)) != 0) {
Andres Amaya Garcia1a607a12017-06-29 17:09:42 +0100431 goto exit;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100432 }
433 if ((ret = mbedtls_md5_update_ret(&md5, secret, slen)) != 0) {
Andres Amaya Garcia1a607a12017-06-29 17:09:42 +0100434 goto exit;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100435 }
436 if ((ret = mbedtls_md5_update_ret(&md5, sha1sum, 20)) != 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_finish_ret(&md5, dstbuf + i * 16)) != 0) {
Andres Amaya Garcia1a607a12017-06-29 17:09:42 +0100440 goto exit;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100441 }
Paul Bakker5f70b252012-09-13 14:23:06 +0000442 }
443
Andres Amaya Garcia1a607a12017-06-29 17:09:42 +0100444exit:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100445 mbedtls_md5_free(&md5);
446 mbedtls_sha1_free(&sha1);
Paul Bakker5f70b252012-09-13 14:23:06 +0000447
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100448 mbedtls_platform_zeroize(padding, sizeof(padding));
449 mbedtls_platform_zeroize(sha1sum, sizeof(sha1sum));
Paul Bakker5f70b252012-09-13 14:23:06 +0000450
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100451 return ret;
Paul Bakker5f70b252012-09-13 14:23:06 +0000452}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200453#endif /* MBEDTLS_SSL_PROTO_SSL3 */
Paul Bakker5f70b252012-09-13 14:23:06 +0000454
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200455#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1)
Manuel Pégourié-Gonnardd904d662022-06-17 10:24:00 +0200456MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100457static int tls1_prf(const unsigned char *secret, size_t slen,
458 const char *label,
459 const unsigned char *random, size_t rlen,
460 unsigned char *dstbuf, size_t dlen)
Paul Bakker5121ce52009-01-03 21:22:43 +0000461{
Paul Bakker23986e52011-04-24 08:57:21 +0000462 size_t nb, hs;
463 size_t i, j, k;
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +0200464 const unsigned char *S1, *S2;
Ron Eldor3b350852019-05-07 18:31:49 +0300465 unsigned char *tmp;
466 size_t tmp_len = 0;
Paul Bakker5121ce52009-01-03 21:22:43 +0000467 unsigned char h_i[20];
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200468 const mbedtls_md_info_t *md_info;
469 mbedtls_md_context_t md_ctx;
Janos Follath865b3eb2019-12-16 11:46:15 +0000470 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnardb7fcca32015-03-26 11:41:28 +0100471
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100472 mbedtls_md_init(&md_ctx);
Paul Bakker5121ce52009-01-03 21:22:43 +0000473
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100474 tmp_len = 20 + strlen(label) + rlen;
475 tmp = mbedtls_calloc(1, tmp_len);
476 if (tmp == NULL) {
Ron Eldor3b350852019-05-07 18:31:49 +0300477 ret = MBEDTLS_ERR_SSL_ALLOC_FAILED;
478 goto exit;
479 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000480
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100481 hs = (slen + 1) / 2;
Paul Bakker5121ce52009-01-03 21:22:43 +0000482 S1 = secret;
483 S2 = secret + slen - hs;
484
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100485 nb = strlen(label);
486 memcpy(tmp + 20, label, nb);
487 memcpy(tmp + 20 + nb, random, rlen);
Paul Bakker5121ce52009-01-03 21:22:43 +0000488 nb += rlen;
489
490 /*
491 * First compute P_md5(secret,label+random)[0..dlen]
492 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100493 if ((md_info = mbedtls_md_info_from_type(MBEDTLS_MD_MD5)) == NULL) {
Ron Eldor3b350852019-05-07 18:31:49 +0300494 ret = MBEDTLS_ERR_SSL_INTERNAL_ERROR;
495 goto exit;
496 }
Manuel Pégourié-Gonnard7da726b2015-03-24 18:08:19 +0100497
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100498 if ((ret = mbedtls_md_setup(&md_ctx, md_info, 1)) != 0) {
Ron Eldor3b350852019-05-07 18:31:49 +0300499 goto exit;
500 }
Manuel Pégourié-Gonnardb7fcca32015-03-26 11:41:28 +0100501
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100502 ret = mbedtls_md_hmac_starts(&md_ctx, S1, hs);
503 if (ret != 0) {
Gilles Peskine2b3f21d2021-12-10 21:35:10 +0100504 goto exit;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100505 }
506 ret = mbedtls_md_hmac_update(&md_ctx, tmp + 20, nb);
507 if (ret != 0) {
Gilles Peskine2b3f21d2021-12-10 21:35:10 +0100508 goto exit;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100509 }
510 ret = mbedtls_md_hmac_finish(&md_ctx, 4 + tmp);
511 if (ret != 0) {
Gilles Peskine2b3f21d2021-12-10 21:35:10 +0100512 goto exit;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100513 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000514
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100515 for (i = 0; i < dlen; i += 16) {
516 ret = mbedtls_md_hmac_reset(&md_ctx);
517 if (ret != 0) {
Gilles Peskine2b3f21d2021-12-10 21:35:10 +0100518 goto exit;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100519 }
520 ret = mbedtls_md_hmac_update(&md_ctx, 4 + tmp, 16 + nb);
521 if (ret != 0) {
Gilles Peskine2b3f21d2021-12-10 21:35:10 +0100522 goto exit;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100523 }
524 ret = mbedtls_md_hmac_finish(&md_ctx, h_i);
525 if (ret != 0) {
Gilles Peskine2b3f21d2021-12-10 21:35:10 +0100526 goto exit;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100527 }
Manuel Pégourié-Gonnardb7fcca32015-03-26 11:41:28 +0100528
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100529 ret = mbedtls_md_hmac_reset(&md_ctx);
530 if (ret != 0) {
Gilles Peskine2b3f21d2021-12-10 21:35:10 +0100531 goto exit;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100532 }
533 ret = mbedtls_md_hmac_update(&md_ctx, 4 + tmp, 16);
534 if (ret != 0) {
Gilles Peskine2b3f21d2021-12-10 21:35:10 +0100535 goto exit;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100536 }
537 ret = mbedtls_md_hmac_finish(&md_ctx, 4 + tmp);
538 if (ret != 0) {
Gilles Peskine2b3f21d2021-12-10 21:35:10 +0100539 goto exit;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100540 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000541
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100542 k = (i + 16 > dlen) ? dlen % 16 : 16;
Paul Bakker5121ce52009-01-03 21:22:43 +0000543
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100544 for (j = 0; j < k; j++) {
Paul Bakker5121ce52009-01-03 21:22:43 +0000545 dstbuf[i + j] = h_i[j];
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100546 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000547 }
548
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100549 mbedtls_md_free(&md_ctx);
Manuel Pégourié-Gonnardb7fcca32015-03-26 11:41:28 +0100550
Paul Bakker5121ce52009-01-03 21:22:43 +0000551 /*
552 * XOR out with P_sha1(secret,label+random)[0..dlen]
553 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100554 if ((md_info = mbedtls_md_info_from_type(MBEDTLS_MD_SHA1)) == NULL) {
Ron Eldor3b350852019-05-07 18:31:49 +0300555 ret = MBEDTLS_ERR_SSL_INTERNAL_ERROR;
556 goto exit;
557 }
Manuel Pégourié-Gonnard7da726b2015-03-24 18:08:19 +0100558
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100559 if ((ret = mbedtls_md_setup(&md_ctx, md_info, 1)) != 0) {
Ron Eldor3b350852019-05-07 18:31:49 +0300560 goto exit;
561 }
Manuel Pégourié-Gonnardb7fcca32015-03-26 11:41:28 +0100562
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100563 ret = mbedtls_md_hmac_starts(&md_ctx, S2, hs);
564 if (ret != 0) {
Gilles Peskine2b3f21d2021-12-10 21:35:10 +0100565 goto exit;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100566 }
567 ret = mbedtls_md_hmac_update(&md_ctx, tmp + 20, nb);
568 if (ret != 0) {
Gilles Peskine2b3f21d2021-12-10 21:35:10 +0100569 goto exit;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100570 }
571 ret = mbedtls_md_hmac_finish(&md_ctx, tmp);
572 if (ret != 0) {
Gilles Peskine2b3f21d2021-12-10 21:35:10 +0100573 goto exit;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100574 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000575
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100576 for (i = 0; i < dlen; i += 20) {
577 ret = mbedtls_md_hmac_reset(&md_ctx);
578 if (ret != 0) {
Gilles Peskine2b3f21d2021-12-10 21:35:10 +0100579 goto exit;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100580 }
581 ret = mbedtls_md_hmac_update(&md_ctx, tmp, 20 + nb);
582 if (ret != 0) {
Gilles Peskine2b3f21d2021-12-10 21:35:10 +0100583 goto exit;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100584 }
585 ret = mbedtls_md_hmac_finish(&md_ctx, h_i);
586 if (ret != 0) {
Gilles Peskine2b3f21d2021-12-10 21:35:10 +0100587 goto exit;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100588 }
Manuel Pégourié-Gonnardb7fcca32015-03-26 11:41:28 +0100589
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100590 ret = mbedtls_md_hmac_reset(&md_ctx);
591 if (ret != 0) {
Gilles Peskine2b3f21d2021-12-10 21:35:10 +0100592 goto exit;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100593 }
594 ret = mbedtls_md_hmac_update(&md_ctx, tmp, 20);
595 if (ret != 0) {
Gilles Peskine2b3f21d2021-12-10 21:35:10 +0100596 goto exit;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100597 }
598 ret = mbedtls_md_hmac_finish(&md_ctx, tmp);
599 if (ret != 0) {
Gilles Peskine2b3f21d2021-12-10 21:35:10 +0100600 goto exit;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100601 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000602
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100603 k = (i + 20 > dlen) ? dlen % 20 : 20;
Paul Bakker5121ce52009-01-03 21:22:43 +0000604
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100605 for (j = 0; j < k; j++) {
606 dstbuf[i + j] = (unsigned char) (dstbuf[i + j] ^ h_i[j]);
607 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000608 }
609
Ron Eldor3b350852019-05-07 18:31:49 +0300610exit:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100611 mbedtls_md_free(&md_ctx);
Manuel Pégourié-Gonnardb7fcca32015-03-26 11:41:28 +0100612
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100613 mbedtls_platform_zeroize(tmp, tmp_len);
614 mbedtls_platform_zeroize(h_i, sizeof(h_i));
Paul Bakker5121ce52009-01-03 21:22:43 +0000615
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100616 mbedtls_free(tmp);
617 return ret;
Paul Bakker5121ce52009-01-03 21:22:43 +0000618}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200619#endif /* MBEDTLS_SSL_PROTO_TLS1) || MBEDTLS_SSL_PROTO_TLS1_1 */
Paul Bakker5121ce52009-01-03 21:22:43 +0000620
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200621#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
Andrzej Kurekc929a822019-01-14 03:51:11 -0500622#if defined(MBEDTLS_USE_PSA_CRYPTO)
k-stachowiak81053a52019-08-17 10:30:28 +0200623
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100624static psa_status_t setup_psa_key_derivation(psa_key_derivation_operation_t *derivation,
625 psa_key_id_t key,
626 psa_algorithm_t alg,
627 const unsigned char *seed, size_t seed_length,
628 const unsigned char *label, size_t label_length,
629 size_t capacity)
k-stachowiak81053a52019-08-17 10:30:28 +0200630{
631 psa_status_t status;
632
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100633 status = psa_key_derivation_setup(derivation, alg);
634 if (status != PSA_SUCCESS) {
635 return status;
636 }
k-stachowiak81053a52019-08-17 10:30:28 +0200637
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100638 if (PSA_ALG_IS_TLS12_PRF(alg) || PSA_ALG_IS_TLS12_PSK_TO_MS(alg)) {
639 status = psa_key_derivation_input_bytes(derivation,
640 PSA_KEY_DERIVATION_INPUT_SEED,
641 seed, seed_length);
642 if (status != PSA_SUCCESS) {
643 return status;
644 }
k-stachowiak81053a52019-08-17 10:30:28 +0200645
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100646 if (mbedtls_svc_key_id_is_null(key)) {
Gilles Peskine311f54d2019-09-23 18:19:22 +0200647 status = psa_key_derivation_input_bytes(
648 derivation, PSA_KEY_DERIVATION_INPUT_SECRET,
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100649 NULL, 0);
650 } else {
Gilles Peskine311f54d2019-09-23 18:19:22 +0200651 status = psa_key_derivation_input_key(
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100652 derivation, PSA_KEY_DERIVATION_INPUT_SECRET, key);
Gilles Peskine311f54d2019-09-23 18:19:22 +0200653 }
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100654 if (status != PSA_SUCCESS) {
655 return status;
656 }
k-stachowiak81053a52019-08-17 10:30:28 +0200657
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100658 status = psa_key_derivation_input_bytes(derivation,
659 PSA_KEY_DERIVATION_INPUT_LABEL,
660 label, label_length);
661 if (status != PSA_SUCCESS) {
662 return status;
663 }
664 } else {
665 return PSA_ERROR_NOT_SUPPORTED;
k-stachowiak81053a52019-08-17 10:30:28 +0200666 }
667
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100668 status = psa_key_derivation_set_capacity(derivation, capacity);
669 if (status != PSA_SUCCESS) {
670 return status;
671 }
k-stachowiak81053a52019-08-17 10:30:28 +0200672
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100673 return PSA_SUCCESS;
k-stachowiak81053a52019-08-17 10:30:28 +0200674}
675
Manuel Pégourié-Gonnardd904d662022-06-17 10:24:00 +0200676MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100677static int tls_prf_generic(mbedtls_md_type_t md_type,
678 const unsigned char *secret, size_t slen,
679 const char *label,
680 const unsigned char *random, size_t rlen,
681 unsigned char *dstbuf, size_t dlen)
Andrzej Kurekc929a822019-01-14 03:51:11 -0500682{
683 psa_status_t status;
684 psa_algorithm_t alg;
Ronald Croncf56a0a2020-08-04 09:51:30 +0200685 psa_key_id_t master_key = MBEDTLS_SVC_KEY_ID_INIT;
Janos Follathda6ac012019-08-16 13:47:29 +0100686 psa_key_derivation_operation_t derivation =
Janos Follath8dee8772019-07-30 12:53:32 +0100687 PSA_KEY_DERIVATION_OPERATION_INIT;
Andrzej Kurekc929a822019-01-14 03:51:11 -0500688
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100689 if (md_type == MBEDTLS_MD_SHA384) {
Andrzej Kurekc929a822019-01-14 03:51:11 -0500690 alg = PSA_ALG_TLS12_PRF(PSA_ALG_SHA_384);
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100691 } else {
Andrzej Kurekc929a822019-01-14 03:51:11 -0500692 alg = PSA_ALG_TLS12_PRF(PSA_ALG_SHA_256);
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100693 }
Andrzej Kurekc929a822019-01-14 03:51:11 -0500694
Gilles Peskine311f54d2019-09-23 18:19:22 +0200695 /* Normally a "secret" should be long enough to be impossible to
696 * find by brute force, and in particular should not be empty. But
697 * this PRF is also used to derive an IV, in particular in EAP-TLS,
698 * and for this use case it makes sense to have a 0-length "secret".
699 * Since the key API doesn't allow importing a key of length 0,
Ronald Croncf56a0a2020-08-04 09:51:30 +0200700 * keep master_key=0, which setup_psa_key_derivation() understands
Gilles Peskine311f54d2019-09-23 18:19:22 +0200701 * to mean a 0-length "secret" input. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100702 if (slen != 0) {
Gilles Peskine311f54d2019-09-23 18:19:22 +0200703 psa_key_attributes_t key_attributes = psa_key_attributes_init();
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100704 psa_set_key_usage_flags(&key_attributes, PSA_KEY_USAGE_DERIVE);
705 psa_set_key_algorithm(&key_attributes, alg);
706 psa_set_key_type(&key_attributes, PSA_KEY_TYPE_DERIVE);
Andrzej Kurekc929a822019-01-14 03:51:11 -0500707
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100708 status = psa_import_key(&key_attributes, secret, slen, &master_key);
709 if (status != PSA_SUCCESS) {
710 return MBEDTLS_ERR_SSL_HW_ACCEL_FAILED;
711 }
Gilles Peskine311f54d2019-09-23 18:19:22 +0200712 }
Andrzej Kurekc929a822019-01-14 03:51:11 -0500713
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100714 status = setup_psa_key_derivation(&derivation,
715 master_key, alg,
716 random, rlen,
717 (unsigned char const *) label,
718 (size_t) strlen(label),
719 dlen);
720 if (status != PSA_SUCCESS) {
721 psa_key_derivation_abort(&derivation);
722 psa_destroy_key(master_key);
723 return MBEDTLS_ERR_SSL_HW_ACCEL_FAILED;
Andrzej Kurekc929a822019-01-14 03:51:11 -0500724 }
725
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100726 status = psa_key_derivation_output_bytes(&derivation, dstbuf, dlen);
727 if (status != PSA_SUCCESS) {
728 psa_key_derivation_abort(&derivation);
729 psa_destroy_key(master_key);
730 return MBEDTLS_ERR_SSL_HW_ACCEL_FAILED;
Andrzej Kurekc929a822019-01-14 03:51:11 -0500731 }
732
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100733 status = psa_key_derivation_abort(&derivation);
734 if (status != PSA_SUCCESS) {
735 psa_destroy_key(master_key);
736 return MBEDTLS_ERR_SSL_HW_ACCEL_FAILED;
Andrzej Kurek70737ca2019-01-14 05:37:13 -0500737 }
Andrzej Kurekc929a822019-01-14 03:51:11 -0500738
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100739 if (!mbedtls_svc_key_id_is_null(master_key)) {
740 status = psa_destroy_key(master_key);
741 }
742 if (status != PSA_SUCCESS) {
743 return MBEDTLS_ERR_SSL_HW_ACCEL_FAILED;
744 }
Andrzej Kurekc929a822019-01-14 03:51:11 -0500745
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100746 return 0;
Andrzej Kurekc929a822019-01-14 03:51:11 -0500747}
748
749#else /* MBEDTLS_USE_PSA_CRYPTO */
750
Manuel Pégourié-Gonnardd904d662022-06-17 10:24:00 +0200751MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100752static int tls_prf_generic(mbedtls_md_type_t md_type,
753 const unsigned char *secret, size_t slen,
754 const char *label,
755 const unsigned char *random, size_t rlen,
756 unsigned char *dstbuf, size_t dlen)
Paul Bakker1ef83d62012-04-11 12:09:53 +0000757{
758 size_t nb;
Manuel Pégourié-Gonnard6890c6b2015-03-26 11:11:49 +0100759 size_t i, j, k, md_len;
Ron Eldor3b350852019-05-07 18:31:49 +0300760 unsigned char *tmp;
761 size_t tmp_len = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200762 unsigned char h_i[MBEDTLS_MD_MAX_SIZE];
763 const mbedtls_md_info_t *md_info;
764 mbedtls_md_context_t md_ctx;
Janos Follath865b3eb2019-12-16 11:46:15 +0000765 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnardb7fcca32015-03-26 11:41:28 +0100766
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100767 mbedtls_md_init(&md_ctx);
Paul Bakker1ef83d62012-04-11 12:09:53 +0000768
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100769 if ((md_info = mbedtls_md_info_from_type(md_type)) == NULL) {
770 return MBEDTLS_ERR_SSL_INTERNAL_ERROR;
771 }
Manuel Pégourié-Gonnard6890c6b2015-03-26 11:11:49 +0100772
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100773 md_len = mbedtls_md_get_size(md_info);
Manuel Pégourié-Gonnard6890c6b2015-03-26 11:11:49 +0100774
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100775 tmp_len = md_len + strlen(label) + rlen;
776 tmp = mbedtls_calloc(1, tmp_len);
777 if (tmp == NULL) {
Ron Eldor3b350852019-05-07 18:31:49 +0300778 ret = MBEDTLS_ERR_SSL_ALLOC_FAILED;
779 goto exit;
780 }
Paul Bakker1ef83d62012-04-11 12:09:53 +0000781
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100782 nb = strlen(label);
783 memcpy(tmp + md_len, label, nb);
784 memcpy(tmp + md_len + nb, random, rlen);
Paul Bakker1ef83d62012-04-11 12:09:53 +0000785 nb += rlen;
786
787 /*
788 * Compute P_<hash>(secret, label + random)[0..dlen]
789 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100790 if ((ret = mbedtls_md_setup(&md_ctx, md_info, 1)) != 0) {
Ron Eldor3b350852019-05-07 18:31:49 +0300791 goto exit;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100792 }
Manuel Pégourié-Gonnardb7fcca32015-03-26 11:41:28 +0100793
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100794 ret = mbedtls_md_hmac_starts(&md_ctx, secret, slen);
795 if (ret != 0) {
Gilles Peskine2b3f21d2021-12-10 21:35:10 +0100796 goto exit;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100797 }
798 ret = mbedtls_md_hmac_update(&md_ctx, tmp + md_len, nb);
799 if (ret != 0) {
Gilles Peskine2b3f21d2021-12-10 21:35:10 +0100800 goto exit;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100801 }
802 ret = mbedtls_md_hmac_finish(&md_ctx, tmp);
803 if (ret != 0) {
Gilles Peskine2b3f21d2021-12-10 21:35:10 +0100804 goto exit;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100805 }
Manuel Pégourié-Gonnard7da726b2015-03-24 18:08:19 +0100806
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100807 for (i = 0; i < dlen; i += md_len) {
808 ret = mbedtls_md_hmac_reset(&md_ctx);
809 if (ret != 0) {
Gilles Peskine2b3f21d2021-12-10 21:35:10 +0100810 goto exit;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100811 }
812 ret = mbedtls_md_hmac_update(&md_ctx, tmp, md_len + nb);
813 if (ret != 0) {
Gilles Peskine2b3f21d2021-12-10 21:35:10 +0100814 goto exit;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100815 }
816 ret = mbedtls_md_hmac_finish(&md_ctx, h_i);
817 if (ret != 0) {
Gilles Peskine2b3f21d2021-12-10 21:35:10 +0100818 goto exit;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100819 }
Manuel Pégourié-Gonnardb7fcca32015-03-26 11:41:28 +0100820
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100821 ret = mbedtls_md_hmac_reset(&md_ctx);
822 if (ret != 0) {
Gilles Peskine2b3f21d2021-12-10 21:35:10 +0100823 goto exit;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100824 }
825 ret = mbedtls_md_hmac_update(&md_ctx, tmp, md_len);
826 if (ret != 0) {
Gilles Peskine2b3f21d2021-12-10 21:35:10 +0100827 goto exit;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100828 }
829 ret = mbedtls_md_hmac_finish(&md_ctx, tmp);
830 if (ret != 0) {
Gilles Peskine2b3f21d2021-12-10 21:35:10 +0100831 goto exit;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100832 }
Paul Bakker1ef83d62012-04-11 12:09:53 +0000833
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100834 k = (i + md_len > dlen) ? dlen % md_len : md_len;
Paul Bakker1ef83d62012-04-11 12:09:53 +0000835
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100836 for (j = 0; j < k; j++) {
Paul Bakker1ef83d62012-04-11 12:09:53 +0000837 dstbuf[i + j] = h_i[j];
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100838 }
Paul Bakker1ef83d62012-04-11 12:09:53 +0000839 }
840
Ron Eldor3b350852019-05-07 18:31:49 +0300841exit:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100842 mbedtls_md_free(&md_ctx);
Manuel Pégourié-Gonnardb7fcca32015-03-26 11:41:28 +0100843
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100844 if (tmp != NULL) {
845 mbedtls_platform_zeroize(tmp, tmp_len);
846 }
Dave Rodgman369f4952022-11-01 16:08:14 +0000847
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100848 mbedtls_platform_zeroize(h_i, sizeof(h_i));
Paul Bakker1ef83d62012-04-11 12:09:53 +0000849
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100850 mbedtls_free(tmp);
Ron Eldor3b350852019-05-07 18:31:49 +0300851
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100852 return ret;
Paul Bakker1ef83d62012-04-11 12:09:53 +0000853}
Andrzej Kurekc929a822019-01-14 03:51:11 -0500854#endif /* MBEDTLS_USE_PSA_CRYPTO */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200855#if defined(MBEDTLS_SHA256_C)
Manuel Pégourié-Gonnardd904d662022-06-17 10:24:00 +0200856MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100857static int tls_prf_sha256(const unsigned char *secret, size_t slen,
858 const char *label,
859 const unsigned char *random, size_t rlen,
860 unsigned char *dstbuf, size_t dlen)
Manuel Pégourié-Gonnard6890c6b2015-03-26 11:11:49 +0100861{
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100862 return tls_prf_generic(MBEDTLS_MD_SHA256, secret, slen,
863 label, random, rlen, dstbuf, dlen);
Manuel Pégourié-Gonnard6890c6b2015-03-26 11:11:49 +0100864}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200865#endif /* MBEDTLS_SHA256_C */
Paul Bakker1ef83d62012-04-11 12:09:53 +0000866
Gilles Peskined2d59372021-05-12 22:43:27 +0200867#if defined(MBEDTLS_SHA512_C) && !defined(MBEDTLS_SHA512_NO_SHA384)
Manuel Pégourié-Gonnardd904d662022-06-17 10:24:00 +0200868MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100869static int tls_prf_sha384(const unsigned char *secret, size_t slen,
870 const char *label,
871 const unsigned char *random, size_t rlen,
872 unsigned char *dstbuf, size_t dlen)
Paul Bakkerca4ab492012-04-18 14:23:57 +0000873{
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100874 return tls_prf_generic(MBEDTLS_MD_SHA384, secret, slen,
875 label, random, rlen, dstbuf, dlen);
Paul Bakkerca4ab492012-04-18 14:23:57 +0000876}
Gilles Peskined2d59372021-05-12 22:43:27 +0200877#endif /* MBEDTLS_SHA512_C && !MBEDTLS_SHA512_NO_SHA384 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200878#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakkerca4ab492012-04-18 14:23:57 +0000879
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100880static void ssl_update_checksum_start(mbedtls_ssl_context *, const unsigned char *, size_t);
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200881
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200882#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1) || \
883 defined(MBEDTLS_SSL_PROTO_TLS1_1)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100884static void ssl_update_checksum_md5sha1(mbedtls_ssl_context *, const unsigned char *, size_t);
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200885#endif
Paul Bakker380da532012-04-18 16:10:25 +0000886
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200887#if defined(MBEDTLS_SSL_PROTO_SSL3)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100888static void ssl_calc_verify_ssl(const mbedtls_ssl_context *, unsigned char *, size_t *);
889static void ssl_calc_finished_ssl(mbedtls_ssl_context *, unsigned char *, int);
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200890#endif
891
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200892#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100893static void ssl_calc_verify_tls(const mbedtls_ssl_context *, unsigned char *, size_t *);
894static void ssl_calc_finished_tls(mbedtls_ssl_context *, unsigned char *, int);
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200895#endif
896
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200897#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
898#if defined(MBEDTLS_SHA256_C)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100899static void ssl_update_checksum_sha256(mbedtls_ssl_context *, const unsigned char *, size_t);
900static void ssl_calc_verify_tls_sha256(const mbedtls_ssl_context *, unsigned char *, size_t *);
901static void ssl_calc_finished_tls_sha256(mbedtls_ssl_context *, unsigned char *, int);
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200902#endif
Paul Bakker769075d2012-11-24 11:26:46 +0100903
Gilles Peskined2d59372021-05-12 22:43:27 +0200904#if defined(MBEDTLS_SHA512_C) && !defined(MBEDTLS_SHA512_NO_SHA384)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100905static void ssl_update_checksum_sha384(mbedtls_ssl_context *, const unsigned char *, size_t);
906static void ssl_calc_verify_tls_sha384(const mbedtls_ssl_context *, unsigned char *, size_t *);
907static void ssl_calc_finished_tls_sha384(mbedtls_ssl_context *, unsigned char *, int);
Paul Bakker769075d2012-11-24 11:26:46 +0100908#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200909#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakker1ef83d62012-04-11 12:09:53 +0000910
Manuel Pégourié-Gonnard45be3d82019-02-18 23:35:14 +0100911#if defined(MBEDTLS_KEY_EXCHANGE_PSK_ENABLED) && \
Hanno Becker7d0a5692018-10-23 15:26:22 +0100912 defined(MBEDTLS_USE_PSA_CRYPTO)
Manuel Pégourié-Gonnardd904d662022-06-17 10:24:00 +0200913MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100914static int ssl_use_opaque_psk(mbedtls_ssl_context const *ssl)
Hanno Becker7d0a5692018-10-23 15:26:22 +0100915{
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100916 if (ssl->conf->f_psk != NULL) {
Hanno Becker7d0a5692018-10-23 15:26:22 +0100917 /* If we've used a callback to select the PSK,
918 * the static configuration is irrelevant. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100919 if (!mbedtls_svc_key_id_is_null(ssl->handshake->psk_opaque)) {
920 return 1;
921 }
Hanno Becker7d0a5692018-10-23 15:26:22 +0100922
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100923 return 0;
Hanno Becker7d0a5692018-10-23 15:26:22 +0100924 }
925
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100926 if (!mbedtls_svc_key_id_is_null(ssl->conf->psk_opaque)) {
927 return 1;
928 }
Hanno Becker7d0a5692018-10-23 15:26:22 +0100929
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100930 return 0;
Hanno Becker7d0a5692018-10-23 15:26:22 +0100931}
932#endif /* MBEDTLS_USE_PSA_CRYPTO &&
Manuel Pégourié-Gonnard45be3d82019-02-18 23:35:14 +0100933 MBEDTLS_KEY_EXCHANGE_PSK_ENABLED */
Hanno Becker7d0a5692018-10-23 15:26:22 +0100934
Ron Eldorcf280092019-05-14 20:19:13 +0300935#if defined(MBEDTLS_SSL_EXPORT_KEYS)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100936static mbedtls_tls_prf_types tls_prf_get_type(mbedtls_ssl_tls_prf_cb *tls_prf)
Ron Eldorcf280092019-05-14 20:19:13 +0300937{
938#if defined(MBEDTLS_SSL_PROTO_SSL3)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100939 if (tls_prf == ssl3_prf) {
940 return MBEDTLS_SSL_TLS_PRF_SSL3;
941 } else
Ron Eldorcf280092019-05-14 20:19:13 +0300942#endif
943#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100944 if (tls_prf == tls1_prf) {
945 return MBEDTLS_SSL_TLS_PRF_TLS1;
946 } else
Ron Eldorcf280092019-05-14 20:19:13 +0300947#endif
948#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
Gilles Peskined2d59372021-05-12 22:43:27 +0200949#if defined(MBEDTLS_SHA512_C) && !defined(MBEDTLS_SHA512_NO_SHA384)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100950 if (tls_prf == tls_prf_sha384) {
951 return MBEDTLS_SSL_TLS_PRF_SHA384;
952 } else
Ron Eldorcf280092019-05-14 20:19:13 +0300953#endif
954#if defined(MBEDTLS_SHA256_C)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100955 if (tls_prf == tls_prf_sha256) {
956 return MBEDTLS_SSL_TLS_PRF_SHA256;
957 } else
Ron Eldorcf280092019-05-14 20:19:13 +0300958#endif
959#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100960 return MBEDTLS_SSL_TLS_PRF_NONE;
Ron Eldorcf280092019-05-14 20:19:13 +0300961}
962#endif /* MBEDTLS_SSL_EXPORT_KEYS */
963
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100964int mbedtls_ssl_tls_prf(const mbedtls_tls_prf_types prf,
965 const unsigned char *secret, size_t slen,
966 const char *label,
967 const unsigned char *random, size_t rlen,
968 unsigned char *dstbuf, size_t dlen)
Ron Eldor51d3ab52019-05-12 14:54:30 +0300969{
970 mbedtls_ssl_tls_prf_cb *tls_prf = NULL;
971
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100972 switch (prf) {
Ron Eldor51d3ab52019-05-12 14:54:30 +0300973#if defined(MBEDTLS_SSL_PROTO_SSL3)
974 case MBEDTLS_SSL_TLS_PRF_SSL3:
975 tls_prf = ssl3_prf;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100976 break;
Ron Eldord2f25f72019-05-15 14:54:22 +0300977#endif /* MBEDTLS_SSL_PROTO_SSL3 */
Ron Eldor51d3ab52019-05-12 14:54:30 +0300978#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1)
979 case MBEDTLS_SSL_TLS_PRF_TLS1:
980 tls_prf = tls1_prf;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100981 break;
Ron Eldord2f25f72019-05-15 14:54:22 +0300982#endif /* MBEDTLS_SSL_PROTO_TLS1 || MBEDTLS_SSL_PROTO_TLS1_1 */
983
984#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
Gilles Peskined2d59372021-05-12 22:43:27 +0200985#if defined(MBEDTLS_SHA512_C) && !defined(MBEDTLS_SHA512_NO_SHA384)
Ron Eldor51d3ab52019-05-12 14:54:30 +0300986 case MBEDTLS_SSL_TLS_PRF_SHA384:
987 tls_prf = tls_prf_sha384;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100988 break;
Gilles Peskined2d59372021-05-12 22:43:27 +0200989#endif /* MBEDTLS_SHA512_C && !MBEDTLS_SHA512_NO_SHA384 */
Ron Eldor51d3ab52019-05-12 14:54:30 +0300990#if defined(MBEDTLS_SHA256_C)
991 case MBEDTLS_SSL_TLS_PRF_SHA256:
992 tls_prf = tls_prf_sha256;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100993 break;
Ron Eldord2f25f72019-05-15 14:54:22 +0300994#endif /* MBEDTLS_SHA256_C */
995#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100996 default:
997 return MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
Ron Eldor51d3ab52019-05-12 14:54:30 +0300998 }
999
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001000 return tls_prf(secret, slen, label, random, rlen, dstbuf, dlen);
Ron Eldor51d3ab52019-05-12 14:54:30 +03001001}
1002
Manuel Pégourié-Gonnard9b108c22019-05-06 13:32:17 +02001003/* Type for the TLS PRF */
1004typedef int ssl_tls_prf_t(const unsigned char *, size_t, const char *,
1005 const unsigned char *, size_t,
1006 unsigned char *, size_t);
1007
Manuel Pégourié-Gonnarde59ae232019-04-30 11:41:40 +02001008/*
Manuel Pégourié-Gonnardcba40d92019-05-06 12:55:40 +02001009 * Populate a transform structure with session keys and all the other
1010 * necessary information.
Manuel Pégourié-Gonnarde59ae232019-04-30 11:41:40 +02001011 *
Manuel Pégourié-Gonnardcba40d92019-05-06 12:55:40 +02001012 * Parameters:
1013 * - [in/out]: transform: structure to populate
1014 * [in] must be just initialised with mbedtls_ssl_transform_init()
Manuel Pégourié-Gonnardc864f6a2019-05-06 13:48:22 +02001015 * [out] fully populated, ready for use by mbedtls_ssl_{en,de}crypt_buf()
Manuel Pégourié-Gonnard6fa57bf2019-05-10 10:50:04 +02001016 * - [in] ciphersuite
1017 * - [in] master
1018 * - [in] encrypt_then_mac
1019 * - [in] trunc_hmac
1020 * - [in] compression
Manuel Pégourié-Gonnard9b108c22019-05-06 13:32:17 +02001021 * - [in] tls_prf: pointer to PRF to use for key derivation
1022 * - [in] randbytes: buffer holding ServerHello.random + ClientHello.random
Manuel Pégourié-Gonnardc864f6a2019-05-06 13:48:22 +02001023 * - [in] minor_ver: SSL/TLS minor version
1024 * - [in] endpoint: client or server
1025 * - [in] ssl: optionally used for:
Manuel Pégourié-Gonnarde07bc202020-02-26 09:53:42 +01001026 * - MBEDTLS_SSL_HW_RECORD_ACCEL: whole context (non-const)
Manuel Pégourié-Gonnardc864f6a2019-05-06 13:48:22 +02001027 * - MBEDTLS_SSL_EXPORT_KEYS: ssl->conf->{f,p}_export_keys
1028 * - MBEDTLS_DEBUG_C: ssl->conf->{f,p}_dbg
Manuel Pégourié-Gonnarde59ae232019-04-30 11:41:40 +02001029 */
Manuel Pégourié-Gonnardd904d662022-06-17 10:24:00 +02001030MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001031static int ssl_populate_transform(mbedtls_ssl_transform *transform,
1032 int ciphersuite,
1033 const unsigned char master[48],
Jarno Lamsac84bd242019-08-16 12:06:56 +03001034#if defined(MBEDTLS_SSL_SOME_MODES_USE_MAC)
Manuel Pégourié-Gonnard6fa57bf2019-05-10 10:50:04 +02001035#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001036 int encrypt_then_mac,
Jarno Lamsac84bd242019-08-16 12:06:56 +03001037#endif /* MBEDTLS_SSL_ENCRYPT_THEN_MAC */
Manuel Pégourié-Gonnard6fa57bf2019-05-10 10:50:04 +02001038#if defined(MBEDTLS_SSL_TRUNCATED_HMAC)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001039 int trunc_hmac,
Jarno Lamsac84bd242019-08-16 12:06:56 +03001040#endif /* MBEDTLS_SSL_TRUNCATED_HMAC */
1041#endif /* MBEDTLS_SSL_SOME_MODES_USE_MAC */
Manuel Pégourié-Gonnard6fa57bf2019-05-10 10:50:04 +02001042#if defined(MBEDTLS_ZLIB_SUPPORT)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001043 int compression,
Manuel Pégourié-Gonnard6fa57bf2019-05-10 10:50:04 +02001044#endif
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001045 ssl_tls_prf_t tls_prf,
1046 const unsigned char randbytes[64],
1047 int minor_ver,
1048 unsigned endpoint,
Manuel Pégourié-Gonnard7ae6ed42020-03-13 11:28:19 +01001049#if !defined(MBEDTLS_SSL_HW_RECORD_ACCEL)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001050 const
Manuel Pégourié-Gonnard7ae6ed42020-03-13 11:28:19 +01001051#endif
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001052 mbedtls_ssl_context *ssl)
Paul Bakker5121ce52009-01-03 21:22:43 +00001053{
Paul Bakkerda02a7f2013-08-31 17:25:14 +02001054 int ret = 0;
Hanno Beckercb1cc802018-11-17 22:27:38 +00001055#if defined(MBEDTLS_USE_PSA_CRYPTO)
1056 int psa_fallthrough;
1057#endif /* MBEDTLS_USE_PSA_CRYPTO */
David Horstmannd4f22082022-10-26 18:25:14 +01001058 int do_mbedtls_cipher_setup;
Paul Bakker5121ce52009-01-03 21:22:43 +00001059 unsigned char keyblk[256];
1060 unsigned char *key1;
1061 unsigned char *key2;
Paul Bakker68884e32013-01-07 18:20:04 +01001062 unsigned char *mac_enc;
1063 unsigned char *mac_dec;
sander-visser3888b032020-05-06 21:49:46 +02001064 size_t mac_key_len = 0;
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02001065 size_t iv_copy_len;
Hanno Becker88aaf652017-12-27 08:17:40 +00001066 unsigned keylen;
Hanno Beckere694c3e2017-12-27 21:34:08 +00001067 const mbedtls_ssl_ciphersuite_t *ciphersuite_info;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001068 const mbedtls_cipher_info_t *cipher_info;
1069 const mbedtls_md_info_t *md_info;
Paul Bakker68884e32013-01-07 18:20:04 +01001070
Manuel Pégourié-Gonnardc864f6a2019-05-06 13:48:22 +02001071#if !defined(MBEDTLS_SSL_HW_RECORD_ACCEL) && \
1072 !defined(MBEDTLS_SSL_EXPORT_KEYS) && \
Gilles Peskinea6f99a12022-04-13 13:24:56 +02001073 !defined(MBEDTLS_SSL_DTLS_CONNECTION_ID) && \
Manuel Pégourié-Gonnardc864f6a2019-05-06 13:48:22 +02001074 !defined(MBEDTLS_DEBUG_C)
Paul Elliott9c2faca2023-10-18 14:34:46 +01001075 (void) ssl; /* ssl is unused except for those cases */
Manuel Pégourié-Gonnardc864f6a2019-05-06 13:48:22 +02001076#endif
1077
Manuel Pégourié-Gonnard96fb0ee2019-07-09 12:54:17 +02001078 /*
1079 * Some data just needs copying into the structure
1080 */
Jaeden Amero2de07f12019-06-05 13:32:08 +01001081#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC) && \
1082 defined(MBEDTLS_SSL_SOME_MODES_USE_MAC)
Manuel Pégourié-Gonnard6fa57bf2019-05-10 10:50:04 +02001083 transform->encrypt_then_mac = encrypt_then_mac;
Hanno Becker9eddaeb2017-12-27 21:37:21 +00001084#endif
Manuel Pégourié-Gonnardc864f6a2019-05-06 13:48:22 +02001085 transform->minor_ver = minor_ver;
Hanno Beckere694c3e2017-12-27 21:34:08 +00001086
Manuel Pégourié-Gonnard96fb0ee2019-07-09 12:54:17 +02001087#if defined(MBEDTLS_SSL_CONTEXT_SERIALIZATION)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001088 memcpy(transform->randbytes, randbytes, sizeof(transform->randbytes));
Manuel Pégourié-Gonnard96fb0ee2019-07-09 12:54:17 +02001089#endif
1090
Manuel Pégourié-Gonnard9b108c22019-05-06 13:32:17 +02001091 /*
1092 * Get various info structures
1093 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001094 ciphersuite_info = mbedtls_ssl_ciphersuite_from_id(ciphersuite);
1095 if (ciphersuite_info == NULL) {
1096 MBEDTLS_SSL_DEBUG_MSG(1, ("ciphersuite info for %d not found",
1097 ciphersuite));
1098 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
Manuel Pégourié-Gonnard9b108c22019-05-06 13:32:17 +02001099 }
1100
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001101 cipher_info = mbedtls_cipher_info_from_type(ciphersuite_info->cipher);
1102 if (cipher_info == NULL) {
1103 MBEDTLS_SSL_DEBUG_MSG(1, ("cipher info for %u not found",
1104 ciphersuite_info->cipher));
1105 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
Paul Bakker68884e32013-01-07 18:20:04 +01001106 }
1107
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001108 md_info = mbedtls_md_info_from_type(ciphersuite_info->mac);
1109 if (md_info == NULL) {
1110 MBEDTLS_SSL_DEBUG_MSG(1, ("mbedtls_md info for %u not found",
1111 (unsigned) ciphersuite_info->mac));
1112 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
Paul Bakker68884e32013-01-07 18:20:04 +01001113 }
1114
Hanno Beckera0e20d02019-05-15 14:03:01 +01001115#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Becker4bf74652019-04-26 16:22:27 +01001116 /* Copy own and peer's CID if the use of the CID
1117 * extension has been negotiated. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001118 if (ssl->handshake->cid_in_use == MBEDTLS_SSL_CID_ENABLED) {
1119 MBEDTLS_SSL_DEBUG_MSG(3, ("Copy CIDs into SSL transform"));
Hanno Becker8a7f9722019-04-30 13:52:29 +01001120
Hanno Becker05154c32019-05-03 15:23:51 +01001121 transform->in_cid_len = ssl->own_cid_len;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001122 memcpy(transform->in_cid, ssl->own_cid, ssl->own_cid_len);
1123 MBEDTLS_SSL_DEBUG_BUF(3, "Incoming CID", transform->in_cid,
1124 transform->in_cid_len);
Hanno Beckerd1f20352019-05-15 10:21:55 +01001125
1126 transform->out_cid_len = ssl->handshake->peer_cid_len;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001127 memcpy(transform->out_cid, ssl->handshake->peer_cid,
1128 ssl->handshake->peer_cid_len);
1129 MBEDTLS_SSL_DEBUG_BUF(3, "Outgoing CID", transform->out_cid,
1130 transform->out_cid_len);
Hanno Becker4bf74652019-04-26 16:22:27 +01001131 }
Hanno Beckera0e20d02019-05-15 14:03:01 +01001132#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Becker4bf74652019-04-26 16:22:27 +01001133
Paul Bakker5121ce52009-01-03 21:22:43 +00001134 /*
Manuel Pégourié-Gonnard9b108c22019-05-06 13:32:17 +02001135 * Compute key block using the PRF
Paul Bakker5121ce52009-01-03 21:22:43 +00001136 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001137 ret = tls_prf(master, 48, "key expansion", randbytes, 64, keyblk, 256);
1138 if (ret != 0) {
1139 MBEDTLS_SSL_DEBUG_RET(1, "prf", ret);
1140 return ret;
Manuel Pégourié-Gonnarde9608182015-03-26 11:47:47 +01001141 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001142
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001143 MBEDTLS_SSL_DEBUG_MSG(3, ("ciphersuite = %s",
1144 mbedtls_ssl_get_ciphersuite_name(ciphersuite)));
1145 MBEDTLS_SSL_DEBUG_BUF(3, "master secret", master, 48);
1146 MBEDTLS_SSL_DEBUG_BUF(4, "random bytes", randbytes, 64);
1147 MBEDTLS_SSL_DEBUG_BUF(4, "key block", keyblk, 256);
Paul Bakker5121ce52009-01-03 21:22:43 +00001148
Paul Bakker5121ce52009-01-03 21:22:43 +00001149 /*
1150 * Determine the appropriate key, IV and MAC length.
1151 */
Paul Bakker68884e32013-01-07 18:20:04 +01001152
Hanno Becker88aaf652017-12-27 08:17:40 +00001153 keylen = cipher_info->key_bitlen / 8;
Manuel Pégourié-Gonnarde800cd82014-06-18 15:34:40 +02001154
Hanno Becker8031d062018-01-03 15:32:31 +00001155#if defined(MBEDTLS_GCM_C) || \
1156 defined(MBEDTLS_CCM_C) || \
1157 defined(MBEDTLS_CHACHAPOLY_C)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001158 if (cipher_info->mode == MBEDTLS_MODE_GCM ||
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02001159 cipher_info->mode == MBEDTLS_MODE_CCM ||
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001160 cipher_info->mode == MBEDTLS_MODE_CHACHAPOLY) {
Hanno Beckerf704bef2018-11-16 15:21:18 +00001161 size_t explicit_ivlen;
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02001162
Manuel Pégourié-Gonnarde800cd82014-06-18 15:34:40 +02001163 transform->maclen = 0;
Hanno Becker81c7b182017-11-09 18:39:33 +00001164 mac_key_len = 0;
Hanno Beckere694c3e2017-12-27 21:34:08 +00001165 transform->taglen =
1166 ciphersuite_info->flags & MBEDTLS_CIPHERSUITE_SHORT_TAG ? 8 : 16;
Manuel Pégourié-Gonnarde800cd82014-06-18 15:34:40 +02001167
Hanno Becker447558d2020-05-28 07:36:33 +01001168 /* All modes haves 96-bit IVs, but the length of the static parts vary
1169 * with mode and version:
1170 * - For GCM and CCM in TLS 1.2, there's a static IV of 4 Bytes
1171 * (to be concatenated with a dynamically chosen IV of 8 Bytes)
Hanno Beckerf93c2d72020-05-28 07:39:43 +01001172 * - For ChaChaPoly in TLS 1.2, and all modes in TLS 1.3, there's
1173 * a static IV of 12 Bytes (to be XOR'ed with the 8 Byte record
1174 * sequence number).
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02001175 */
Paul Bakker68884e32013-01-07 18:20:04 +01001176 transform->ivlen = 12;
Hanno Beckerf93c2d72020-05-28 07:39:43 +01001177#if defined(MBEDTLS_SSL_PROTO_TLS1_3_EXPERIMENTAL)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001178 if (minor_ver == MBEDTLS_SSL_MINOR_VERSION_4) {
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02001179 transform->fixed_ivlen = 12;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001180 } else
Hanno Beckerf93c2d72020-05-28 07:39:43 +01001181#endif /* MBEDTLS_SSL_PROTO_TLS1_3_EXPERIMENTAL */
1182 {
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001183 if (cipher_info->mode == MBEDTLS_MODE_CHACHAPOLY) {
Hanno Beckerf93c2d72020-05-28 07:39:43 +01001184 transform->fixed_ivlen = 12;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001185 } else {
Hanno Beckerf93c2d72020-05-28 07:39:43 +01001186 transform->fixed_ivlen = 4;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001187 }
Hanno Beckerf93c2d72020-05-28 07:39:43 +01001188 }
Manuel Pégourié-Gonnarde800cd82014-06-18 15:34:40 +02001189
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02001190 /* Minimum length of encrypted record */
1191 explicit_ivlen = transform->ivlen - transform->fixed_ivlen;
Hanno Beckere694c3e2017-12-27 21:34:08 +00001192 transform->minlen = explicit_ivlen + transform->taglen;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001193 } else
Hanno Becker8031d062018-01-03 15:32:31 +00001194#endif /* MBEDTLS_GCM_C || MBEDTLS_CCM_C || MBEDTLS_CHACHAPOLY_C */
1195#if defined(MBEDTLS_SSL_SOME_MODES_USE_MAC)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001196 if (cipher_info->mode == MBEDTLS_MODE_STREAM ||
1197 cipher_info->mode == MBEDTLS_MODE_CBC) {
Manuel Pégourié-Gonnarde800cd82014-06-18 15:34:40 +02001198 /* Initialize HMAC contexts */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001199 if ((ret = mbedtls_md_setup(&transform->md_ctx_enc, md_info, 1)) != 0 ||
1200 (ret = mbedtls_md_setup(&transform->md_ctx_dec, md_info, 1)) != 0) {
1201 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_md_setup", ret);
Ron Eldore6992702019-05-07 18:27:13 +03001202 goto end;
Paul Bakker68884e32013-01-07 18:20:04 +01001203 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001204
Manuel Pégourié-Gonnarde800cd82014-06-18 15:34:40 +02001205 /* Get MAC length */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001206 mac_key_len = mbedtls_md_get_size(md_info);
Hanno Becker81c7b182017-11-09 18:39:33 +00001207 transform->maclen = mac_key_len;
Manuel Pégourié-Gonnarde800cd82014-06-18 15:34:40 +02001208
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001209#if defined(MBEDTLS_SSL_TRUNCATED_HMAC)
Manuel Pégourié-Gonnarde800cd82014-06-18 15:34:40 +02001210 /*
1211 * If HMAC is to be truncated, we shall keep the leftmost bytes,
1212 * (rfc 6066 page 13 or rfc 2104 section 4),
1213 * so we only need to adjust the length here.
1214 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001215 if (trunc_hmac == MBEDTLS_SSL_TRUNC_HMAC_ENABLED) {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001216 transform->maclen = MBEDTLS_SSL_TRUNCATED_HMAC_LEN;
Hanno Beckere89353a2017-11-20 16:36:41 +00001217
1218#if defined(MBEDTLS_SSL_TRUNCATED_HMAC_COMPAT)
1219 /* Fall back to old, non-compliant version of the truncated
Hanno Becker563423f2017-11-21 17:20:17 +00001220 * HMAC implementation which also truncates the key
1221 * (Mbed TLS versions from 1.3 to 2.6.0) */
Hanno Beckere89353a2017-11-20 16:36:41 +00001222 mac_key_len = transform->maclen;
1223#endif
1224 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001225#endif /* MBEDTLS_SSL_TRUNCATED_HMAC */
Manuel Pégourié-Gonnarde800cd82014-06-18 15:34:40 +02001226
1227 /* IV length */
Paul Bakker68884e32013-01-07 18:20:04 +01001228 transform->ivlen = cipher_info->iv_size;
Paul Bakker5121ce52009-01-03 21:22:43 +00001229
Manuel Pégourié-Gonnardeaa76f72014-06-18 16:06:02 +02001230 /* Minimum length */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001231 if (cipher_info->mode == MBEDTLS_MODE_STREAM) {
Manuel Pégourié-Gonnardeaa76f72014-06-18 16:06:02 +02001232 transform->minlen = transform->maclen;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001233 } else {
Manuel Pégourié-Gonnardeaa76f72014-06-18 16:06:02 +02001234 /*
1235 * GenericBlockCipher:
Manuel Pégourié-Gonnard169dd6a2014-11-04 16:15:39 +01001236 * 1. if EtM is in use: one block plus MAC
1237 * otherwise: * first multiple of blocklen greater than maclen
1238 * 2. IV except for SSL3 and TLS 1.0
Manuel Pégourié-Gonnardeaa76f72014-06-18 16:06:02 +02001239 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001240#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001241 if (encrypt_then_mac == MBEDTLS_SSL_ETM_ENABLED) {
Manuel Pégourié-Gonnard169dd6a2014-11-04 16:15:39 +01001242 transform->minlen = transform->maclen
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001243 + cipher_info->block_size;
1244 } else
Manuel Pégourié-Gonnard169dd6a2014-11-04 16:15:39 +01001245#endif
1246 {
1247 transform->minlen = transform->maclen
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001248 + cipher_info->block_size
1249 - transform->maclen % cipher_info->block_size;
Manuel Pégourié-Gonnard169dd6a2014-11-04 16:15:39 +01001250 }
Manuel Pégourié-Gonnardeaa76f72014-06-18 16:06:02 +02001251
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001252#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001253 if (minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 ||
1254 minor_ver == MBEDTLS_SSL_MINOR_VERSION_1) {
Manuel Pégourié-Gonnardeaa76f72014-06-18 16:06:02 +02001255 ; /* No need to adjust minlen */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001256 } else
Manuel Pégourié-Gonnardeaa76f72014-06-18 16:06:02 +02001257#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001258#if defined(MBEDTLS_SSL_PROTO_TLS1_1) || defined(MBEDTLS_SSL_PROTO_TLS1_2)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001259 if (minor_ver == MBEDTLS_SSL_MINOR_VERSION_2 ||
1260 minor_ver == MBEDTLS_SSL_MINOR_VERSION_3) {
Manuel Pégourié-Gonnardeaa76f72014-06-18 16:06:02 +02001261 transform->minlen += transform->ivlen;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001262 } else
Manuel Pégourié-Gonnardeaa76f72014-06-18 16:06:02 +02001263#endif
1264 {
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001265 MBEDTLS_SSL_DEBUG_MSG(1, ("should never happen"));
Ron Eldore6992702019-05-07 18:27:13 +03001266 ret = MBEDTLS_ERR_SSL_INTERNAL_ERROR;
1267 goto end;
Manuel Pégourié-Gonnardeaa76f72014-06-18 16:06:02 +02001268 }
Paul Bakker68884e32013-01-07 18:20:04 +01001269 }
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001270 } else
Hanno Becker8031d062018-01-03 15:32:31 +00001271#endif /* MBEDTLS_SSL_SOME_MODES_USE_MAC */
1272 {
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001273 MBEDTLS_SSL_DEBUG_MSG(1, ("should never happen"));
1274 return MBEDTLS_ERR_SSL_INTERNAL_ERROR;
Hanno Becker8031d062018-01-03 15:32:31 +00001275 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001276
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001277 MBEDTLS_SSL_DEBUG_MSG(3, ("keylen: %u, minlen: %u, ivlen: %u, maclen: %u",
1278 (unsigned) keylen,
1279 (unsigned) transform->minlen,
1280 (unsigned) transform->ivlen,
1281 (unsigned) transform->maclen));
Paul Bakker5121ce52009-01-03 21:22:43 +00001282
1283 /*
1284 * Finally setup the cipher contexts, IVs and MAC secrets.
1285 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001286#if defined(MBEDTLS_SSL_CLI_C)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001287 if (endpoint == MBEDTLS_SSL_IS_CLIENT) {
Hanno Becker81c7b182017-11-09 18:39:33 +00001288 key1 = keyblk + mac_key_len * 2;
Hanno Becker88aaf652017-12-27 08:17:40 +00001289 key2 = keyblk + mac_key_len * 2 + keylen;
Paul Bakker5121ce52009-01-03 21:22:43 +00001290
Paul Bakker68884e32013-01-07 18:20:04 +01001291 mac_enc = keyblk;
Hanno Becker81c7b182017-11-09 18:39:33 +00001292 mac_dec = keyblk + mac_key_len;
Paul Bakker5121ce52009-01-03 21:22:43 +00001293
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001294 /*
1295 * This is not used in TLS v1.1.
1296 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001297 iv_copy_len = (transform->fixed_ivlen) ?
1298 transform->fixed_ivlen : transform->ivlen;
1299 memcpy(transform->iv_enc, key2 + keylen, iv_copy_len);
1300 memcpy(transform->iv_dec, key2 + keylen + iv_copy_len,
1301 iv_copy_len);
1302 } else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001303#endif /* MBEDTLS_SSL_CLI_C */
1304#if defined(MBEDTLS_SSL_SRV_C)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001305 if (endpoint == MBEDTLS_SSL_IS_SERVER) {
Hanno Becker88aaf652017-12-27 08:17:40 +00001306 key1 = keyblk + mac_key_len * 2 + keylen;
Hanno Becker81c7b182017-11-09 18:39:33 +00001307 key2 = keyblk + mac_key_len * 2;
Paul Bakker5121ce52009-01-03 21:22:43 +00001308
Hanno Becker81c7b182017-11-09 18:39:33 +00001309 mac_enc = keyblk + mac_key_len;
Paul Bakker68884e32013-01-07 18:20:04 +01001310 mac_dec = keyblk;
Paul Bakker5121ce52009-01-03 21:22:43 +00001311
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001312 /*
1313 * This is not used in TLS v1.1.
1314 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001315 iv_copy_len = (transform->fixed_ivlen) ?
1316 transform->fixed_ivlen : transform->ivlen;
1317 memcpy(transform->iv_dec, key1 + keylen, iv_copy_len);
1318 memcpy(transform->iv_enc, key1 + keylen + iv_copy_len,
1319 iv_copy_len);
1320 } else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001321#endif /* MBEDTLS_SSL_SRV_C */
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01001322 {
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001323 MBEDTLS_SSL_DEBUG_MSG(1, ("should never happen"));
Ron Eldore6992702019-05-07 18:27:13 +03001324 ret = MBEDTLS_ERR_SSL_INTERNAL_ERROR;
1325 goto end;
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01001326 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001327
Hanno Beckerd56ed242018-01-03 15:32:51 +00001328#if defined(MBEDTLS_SSL_SOME_MODES_USE_MAC)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001329#if defined(MBEDTLS_SSL_PROTO_SSL3)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001330 if (minor_ver == MBEDTLS_SSL_MINOR_VERSION_0) {
1331 if (mac_key_len > sizeof(transform->mac_enc)) {
1332 MBEDTLS_SSL_DEBUG_MSG(1, ("should never happen"));
Ron Eldore6992702019-05-07 18:27:13 +03001333 ret = MBEDTLS_ERR_SSL_INTERNAL_ERROR;
1334 goto end;
Manuel Pégourié-Gonnard7cfdcb82014-01-18 18:22:55 +01001335 }
1336
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001337 memcpy(transform->mac_enc, mac_enc, mac_key_len);
1338 memcpy(transform->mac_dec, mac_dec, mac_key_len);
1339 } else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001340#endif /* MBEDTLS_SSL_PROTO_SSL3 */
1341#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1) || \
1342 defined(MBEDTLS_SSL_PROTO_TLS1_2)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001343 if (minor_ver >= MBEDTLS_SSL_MINOR_VERSION_1) {
Gilles Peskine039fd122018-03-19 19:06:08 +01001344 /* For HMAC-based ciphersuites, initialize the HMAC transforms.
1345 For AEAD-based ciphersuites, there is nothing to do here. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001346 if (mac_key_len != 0) {
1347 ret = mbedtls_md_hmac_starts(&transform->md_ctx_enc,
1348 mac_enc, mac_key_len);
1349 if (ret != 0) {
Gilles Peskine2b3f21d2021-12-10 21:35:10 +01001350 goto end;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001351 }
1352 ret = mbedtls_md_hmac_starts(&transform->md_ctx_dec,
1353 mac_dec, mac_key_len);
1354 if (ret != 0) {
Gilles Peskine2b3f21d2021-12-10 21:35:10 +01001355 goto end;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001356 }
Gilles Peskine039fd122018-03-19 19:06:08 +01001357 }
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001358 } else
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001359#endif
Paul Bakker577e0062013-08-28 11:57:20 +02001360 {
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001361 MBEDTLS_SSL_DEBUG_MSG(1, ("should never happen"));
Ron Eldore6992702019-05-07 18:27:13 +03001362 ret = MBEDTLS_ERR_SSL_INTERNAL_ERROR;
1363 goto end;
Paul Bakker577e0062013-08-28 11:57:20 +02001364 }
Hanno Beckerd56ed242018-01-03 15:32:51 +00001365#endif /* MBEDTLS_SSL_SOME_MODES_USE_MAC */
Paul Bakker68884e32013-01-07 18:20:04 +01001366
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001367#if defined(MBEDTLS_SSL_HW_RECORD_ACCEL)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001368 if (mbedtls_ssl_hw_record_init != NULL) {
sander-visser1abe8ee2020-05-06 21:27:14 +02001369 ret = 0;
Paul Bakker05ef8352012-05-08 09:17:57 +00001370
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001371 MBEDTLS_SSL_DEBUG_MSG(2, ("going for mbedtls_ssl_hw_record_init()"));
Paul Bakker05ef8352012-05-08 09:17:57 +00001372
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001373 if ((ret = mbedtls_ssl_hw_record_init(ssl, key1, key2, keylen,
1374 transform->iv_enc, transform->iv_dec,
1375 iv_copy_len,
1376 mac_enc, mac_dec,
1377 mac_key_len)) != 0) {
1378 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ssl_hw_record_init", ret);
Ron Eldore6992702019-05-07 18:27:13 +03001379 ret = MBEDTLS_ERR_SSL_HW_ACCEL_FAILED;
1380 goto end;
Paul Bakker05ef8352012-05-08 09:17:57 +00001381 }
1382 }
Hanno Beckerd56ed242018-01-03 15:32:51 +00001383#else
1384 ((void) mac_dec);
1385 ((void) mac_enc);
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001386#endif /* MBEDTLS_SSL_HW_RECORD_ACCEL */
Paul Bakker05ef8352012-05-08 09:17:57 +00001387
Manuel Pégourié-Gonnard024b6df2015-10-19 13:52:53 +02001388#if defined(MBEDTLS_SSL_EXPORT_KEYS)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001389 if (ssl->conf->f_export_keys != NULL) {
1390 ssl->conf->f_export_keys(ssl->conf->p_export_keys,
1391 master, keyblk,
1392 mac_key_len, keylen,
1393 iv_copy_len);
Robert Cragie4feb7ae2015-10-02 13:33:37 +01001394 }
Ron Eldorf5cc10d2019-05-07 18:33:40 +03001395
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001396 if (ssl->conf->f_export_keys_ext != NULL) {
1397 ssl->conf->f_export_keys_ext(ssl->conf->p_export_keys,
1398 master, keyblk,
1399 mac_key_len, keylen,
1400 iv_copy_len,
1401 randbytes + 32,
1402 randbytes,
1403 tls_prf_get_type(tls_prf));
Ron Eldorf5cc10d2019-05-07 18:33:40 +03001404 }
Robert Cragie4feb7ae2015-10-02 13:33:37 +01001405#endif
1406
David Horstmannd4f22082022-10-26 18:25:14 +01001407 do_mbedtls_cipher_setup = 1;
Hanno Beckerf704bef2018-11-16 15:21:18 +00001408#if defined(MBEDTLS_USE_PSA_CRYPTO)
Hanno Beckercb1cc802018-11-17 22:27:38 +00001409
1410 /* Only use PSA-based ciphers for TLS-1.2.
1411 * That's relevant at least for TLS-1.0, where
1412 * we assume that mbedtls_cipher_crypt() updates
1413 * the structure field for the IV, which the PSA-based
1414 * implementation currently doesn't. */
1415#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001416 if (minor_ver == MBEDTLS_SSL_MINOR_VERSION_3) {
1417 ret = mbedtls_cipher_setup_psa(&transform->cipher_ctx_enc,
1418 cipher_info, transform->taglen);
1419 if (ret != 0 && ret != MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE) {
1420 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_cipher_setup_psa", ret);
Ron Eldore6992702019-05-07 18:27:13 +03001421 goto end;
Hanno Beckercb1cc802018-11-17 22:27:38 +00001422 }
1423
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001424 if (ret == 0) {
1425 MBEDTLS_SSL_DEBUG_MSG(3, ("Successfully setup PSA-based encryption cipher context"));
Hanno Beckercb1cc802018-11-17 22:27:38 +00001426 psa_fallthrough = 0;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001427 } else {
1428 MBEDTLS_SSL_DEBUG_MSG(1,
1429 (
1430 "Failed to setup PSA-based cipher context for record encryption - fall through to default setup."));
Hanno Beckercb1cc802018-11-17 22:27:38 +00001431 psa_fallthrough = 1;
1432 }
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001433 } else {
Hanno Beckercb1cc802018-11-17 22:27:38 +00001434 psa_fallthrough = 1;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001435 }
Hanno Beckercb1cc802018-11-17 22:27:38 +00001436#else
1437 psa_fallthrough = 1;
1438#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Hanno Beckerf704bef2018-11-16 15:21:18 +00001439
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001440 if (psa_fallthrough == 0) {
David Horstmannd4f22082022-10-26 18:25:14 +01001441 do_mbedtls_cipher_setup = 0;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001442 }
Hanno Beckerf704bef2018-11-16 15:21:18 +00001443#endif /* MBEDTLS_USE_PSA_CRYPTO */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001444 if (do_mbedtls_cipher_setup &&
1445 (ret = mbedtls_cipher_setup(&transform->cipher_ctx_enc,
1446 cipher_info)) != 0) {
1447 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_cipher_setup", ret);
Ron Eldore6992702019-05-07 18:27:13 +03001448 goto end;
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001449 }
Paul Bakkerda02a7f2013-08-31 17:25:14 +02001450
David Horstmannd4f22082022-10-26 18:25:14 +01001451 do_mbedtls_cipher_setup = 1;
Hanno Beckerf704bef2018-11-16 15:21:18 +00001452#if defined(MBEDTLS_USE_PSA_CRYPTO)
Hanno Beckercb1cc802018-11-17 22:27:38 +00001453 /* Only use PSA-based ciphers for TLS-1.2.
1454 * That's relevant at least for TLS-1.0, where
1455 * we assume that mbedtls_cipher_crypt() updates
1456 * the structure field for the IV, which the PSA-based
1457 * implementation currently doesn't. */
1458#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001459 if (minor_ver == MBEDTLS_SSL_MINOR_VERSION_3) {
1460 ret = mbedtls_cipher_setup_psa(&transform->cipher_ctx_dec,
1461 cipher_info, transform->taglen);
1462 if (ret != 0 && ret != MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE) {
1463 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_cipher_setup_psa", ret);
Ron Eldore6992702019-05-07 18:27:13 +03001464 goto end;
Hanno Beckercb1cc802018-11-17 22:27:38 +00001465 }
1466
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001467 if (ret == 0) {
1468 MBEDTLS_SSL_DEBUG_MSG(3, ("Successfully setup PSA-based decryption cipher context"));
Hanno Beckercb1cc802018-11-17 22:27:38 +00001469 psa_fallthrough = 0;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001470 } else {
1471 MBEDTLS_SSL_DEBUG_MSG(1,
1472 (
1473 "Failed to setup PSA-based cipher context for record decryption - fall through to default setup."));
Hanno Beckercb1cc802018-11-17 22:27:38 +00001474 psa_fallthrough = 1;
1475 }
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001476 } else {
Hanno Beckercb1cc802018-11-17 22:27:38 +00001477 psa_fallthrough = 1;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001478 }
Hanno Beckercb1cc802018-11-17 22:27:38 +00001479#else
1480 psa_fallthrough = 1;
1481#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Hanno Beckerf704bef2018-11-16 15:21:18 +00001482
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001483 if (psa_fallthrough == 0) {
David Horstmannd4f22082022-10-26 18:25:14 +01001484 do_mbedtls_cipher_setup = 0;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001485 }
Hanno Beckerf704bef2018-11-16 15:21:18 +00001486#endif /* MBEDTLS_USE_PSA_CRYPTO */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001487 if (do_mbedtls_cipher_setup &&
1488 (ret = mbedtls_cipher_setup(&transform->cipher_ctx_dec,
1489 cipher_info)) != 0) {
1490 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_cipher_setup", ret);
Ron Eldore6992702019-05-07 18:27:13 +03001491 goto end;
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001492 }
Paul Bakkerda02a7f2013-08-31 17:25:14 +02001493
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001494 if ((ret = mbedtls_cipher_setkey(&transform->cipher_ctx_enc, key1,
1495 cipher_info->key_bitlen,
1496 MBEDTLS_ENCRYPT)) != 0) {
1497 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_cipher_setkey", ret);
Ron Eldore6992702019-05-07 18:27:13 +03001498 goto end;
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001499 }
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02001500
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001501 if ((ret = mbedtls_cipher_setkey(&transform->cipher_ctx_dec, key2,
1502 cipher_info->key_bitlen,
1503 MBEDTLS_DECRYPT)) != 0) {
1504 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_cipher_setkey", ret);
Ron Eldore6992702019-05-07 18:27:13 +03001505 goto end;
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001506 }
Paul Bakkerda02a7f2013-08-31 17:25:14 +02001507
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001508#if defined(MBEDTLS_CIPHER_MODE_CBC)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001509 if (cipher_info->mode == MBEDTLS_MODE_CBC) {
1510 if ((ret = mbedtls_cipher_set_padding_mode(&transform->cipher_ctx_enc,
1511 MBEDTLS_PADDING_NONE)) != 0) {
1512 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_cipher_set_padding_mode", ret);
Ron Eldore6992702019-05-07 18:27:13 +03001513 goto end;
Manuel Pégourié-Gonnard126a66f2013-10-25 18:33:32 +02001514 }
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001515
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001516 if ((ret = mbedtls_cipher_set_padding_mode(&transform->cipher_ctx_dec,
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é-Gonnard88665912013-10-25 18:42:44 +02001520 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001521 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001522#endif /* MBEDTLS_CIPHER_MODE_CBC */
Paul Bakker5121ce52009-01-03 21:22:43 +00001523
Paul Bakker5121ce52009-01-03 21:22:43 +00001524
Manuel Pégourié-Gonnardd73b47f2019-05-06 12:44:24 +02001525 /* Initialize Zlib contexts */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001526#if defined(MBEDTLS_ZLIB_SUPPORT)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001527 if (compression == MBEDTLS_SSL_COMPRESS_DEFLATE) {
1528 MBEDTLS_SSL_DEBUG_MSG(3, ("Initializing zlib states"));
Paul Bakker2770fbd2012-07-03 13:30:23 +00001529
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001530 memset(&transform->ctx_deflate, 0, sizeof(transform->ctx_deflate));
1531 memset(&transform->ctx_inflate, 0, sizeof(transform->ctx_inflate));
Paul Bakker2770fbd2012-07-03 13:30:23 +00001532
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001533 if (deflateInit(&transform->ctx_deflate,
1534 Z_DEFAULT_COMPRESSION) != Z_OK ||
1535 inflateInit(&transform->ctx_inflate) != Z_OK) {
1536 MBEDTLS_SSL_DEBUG_MSG(1, ("Failed to initialize compression"));
Ron Eldore6992702019-05-07 18:27:13 +03001537 ret = MBEDTLS_ERR_SSL_COMPRESSION_FAILED;
1538 goto end;
Paul Bakker2770fbd2012-07-03 13:30:23 +00001539 }
1540 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001541#endif /* MBEDTLS_ZLIB_SUPPORT */
Paul Bakker2770fbd2012-07-03 13:30:23 +00001542
Ron Eldore6992702019-05-07 18:27:13 +03001543end:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001544 mbedtls_platform_zeroize(keyblk, sizeof(keyblk));
1545 return ret;
Paul Bakker5121ce52009-01-03 21:22:43 +00001546}
1547
Manuel Pégourié-Gonnard8d2805c2019-04-30 12:08:59 +02001548/*
Manuel Pégourié-Gonnard47e33e12019-05-20 10:10:17 +02001549 * Set appropriate PRF function and other SSL / TLS 1.0/1.1 / TLS1.2 functions
Manuel Pégourié-Gonnard8d2805c2019-04-30 12:08:59 +02001550 *
1551 * Inputs:
1552 * - SSL/TLS minor version
1553 * - hash associated with the ciphersuite (only used by TLS 1.2)
1554 *
Manuel Pégourié-Gonnard31d3ef12019-05-10 10:25:00 +02001555 * Outputs:
Manuel Pégourié-Gonnard8d2805c2019-04-30 12:08:59 +02001556 * - the tls_prf, calc_verify and calc_finished members of handshake structure
1557 */
Manuel Pégourié-Gonnardd904d662022-06-17 10:24:00 +02001558MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001559static int ssl_set_handshake_prfs(mbedtls_ssl_handshake_params *handshake,
1560 int minor_ver,
1561 mbedtls_md_type_t hash)
Manuel Pégourié-Gonnard1b00c4f2019-04-30 11:54:22 +02001562{
Gilles Peskinefc9c07f2021-05-12 22:51:53 +02001563#if !defined(MBEDTLS_SSL_PROTO_TLS1_2) || \
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001564 !(defined(MBEDTLS_SHA512_C) && !defined(MBEDTLS_SHA512_NO_SHA384))
Manuel Pégourié-Gonnard8d2805c2019-04-30 12:08:59 +02001565 (void) hash;
1566#endif
Manuel Pégourié-Gonnard1b00c4f2019-04-30 11:54:22 +02001567
Manuel Pégourié-Gonnard1b00c4f2019-04-30 11:54:22 +02001568#if defined(MBEDTLS_SSL_PROTO_SSL3)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001569 if (minor_ver == MBEDTLS_SSL_MINOR_VERSION_0) {
Manuel Pégourié-Gonnard1b00c4f2019-04-30 11:54:22 +02001570 handshake->tls_prf = ssl3_prf;
1571 handshake->calc_verify = ssl_calc_verify_ssl;
1572 handshake->calc_finished = ssl_calc_finished_ssl;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001573 } else
Manuel Pégourié-Gonnard1b00c4f2019-04-30 11:54:22 +02001574#endif
1575#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001576 if (minor_ver < MBEDTLS_SSL_MINOR_VERSION_3) {
Manuel Pégourié-Gonnard1b00c4f2019-04-30 11:54:22 +02001577 handshake->tls_prf = tls1_prf;
1578 handshake->calc_verify = ssl_calc_verify_tls;
1579 handshake->calc_finished = ssl_calc_finished_tls;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001580 } else
Manuel Pégourié-Gonnard1b00c4f2019-04-30 11:54:22 +02001581#endif
1582#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
Gilles Peskined2d59372021-05-12 22:43:27 +02001583#if defined(MBEDTLS_SHA512_C) && !defined(MBEDTLS_SHA512_NO_SHA384)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001584 if (minor_ver == MBEDTLS_SSL_MINOR_VERSION_3 &&
1585 hash == MBEDTLS_MD_SHA384) {
Manuel Pégourié-Gonnard1b00c4f2019-04-30 11:54:22 +02001586 handshake->tls_prf = tls_prf_sha384;
1587 handshake->calc_verify = ssl_calc_verify_tls_sha384;
1588 handshake->calc_finished = ssl_calc_finished_tls_sha384;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001589 } else
Manuel Pégourié-Gonnard1b00c4f2019-04-30 11:54:22 +02001590#endif
1591#if defined(MBEDTLS_SHA256_C)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001592 if (minor_ver == MBEDTLS_SSL_MINOR_VERSION_3) {
Manuel Pégourié-Gonnard1b00c4f2019-04-30 11:54:22 +02001593 handshake->tls_prf = tls_prf_sha256;
1594 handshake->calc_verify = ssl_calc_verify_tls_sha256;
1595 handshake->calc_finished = ssl_calc_finished_tls_sha256;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001596 } else
Manuel Pégourié-Gonnard1b00c4f2019-04-30 11:54:22 +02001597#endif
1598#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
1599 {
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001600 return MBEDTLS_ERR_SSL_INTERNAL_ERROR;
Manuel Pégourié-Gonnard1b00c4f2019-04-30 11:54:22 +02001601 }
1602
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001603 return 0;
Manuel Pégourié-Gonnard1b00c4f2019-04-30 11:54:22 +02001604}
1605
Manuel Pégourié-Gonnard9951b712019-04-30 12:08:59 +02001606/*
Manuel Pégourié-Gonnard85680c42019-05-03 09:16:16 +02001607 * Compute master secret if needed
Manuel Pégourié-Gonnardde047ad2019-05-03 09:46:14 +02001608 *
1609 * Parameters:
1610 * [in/out] handshake
1611 * [in] resume, premaster, extended_ms, calc_verify, tls_prf
Manuel Pégourié-Gonnardde718b92019-05-03 11:43:28 +02001612 * (PSA-PSK) ciphersuite_info, psk_opaque
Manuel Pégourié-Gonnardde047ad2019-05-03 09:46:14 +02001613 * [out] premaster (cleared)
Manuel Pégourié-Gonnardde047ad2019-05-03 09:46:14 +02001614 * [out] master
1615 * [in] ssl: optionally used for debugging, EMS and PSA-PSK
1616 * debug: conf->f_dbg, conf->p_dbg
1617 * EMS: passed to calc_verify (debug + (SSL3) session_negotiate)
Manuel Pégourié-Gonnardde718b92019-05-03 11:43:28 +02001618 * PSA-PSA: minor_ver, conf
Manuel Pégourié-Gonnard9951b712019-04-30 12:08:59 +02001619 */
Manuel Pégourié-Gonnardd904d662022-06-17 10:24:00 +02001620MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001621static int ssl_compute_master(mbedtls_ssl_handshake_params *handshake,
1622 unsigned char *master,
1623 const mbedtls_ssl_context *ssl)
Manuel Pégourié-Gonnard9951b712019-04-30 12:08:59 +02001624{
Janos Follath865b3eb2019-12-16 11:46:15 +00001625 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnard9951b712019-04-30 12:08:59 +02001626
1627 /* cf. RFC 5246, Section 8.1:
1628 * "The master secret is always exactly 48 bytes in length." */
1629 size_t const master_secret_len = 48;
1630
1631#if defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET)
1632 unsigned char session_hash[48];
1633#endif /* MBEDTLS_SSL_EXTENDED_MASTER_SECRET */
1634
Manuel Pégourié-Gonnard85680c42019-05-03 09:16:16 +02001635 /* The label for the KDF used for key expansion.
1636 * This is either "master secret" or "extended master secret"
1637 * depending on whether the Extended Master Secret extension
1638 * is used. */
1639 char const *lbl = "master secret";
1640
1641 /* The salt for the KDF used for key expansion.
1642 * - If the Extended Master Secret extension is not used,
1643 * this is ClientHello.Random + ServerHello.Random
1644 * (see Sect. 8.1 in RFC 5246).
1645 * - If the Extended Master Secret extension is used,
1646 * this is the transcript of the handshake so far.
1647 * (see Sect. 4 in RFC 7627). */
1648 unsigned char const *salt = handshake->randbytes;
1649 size_t salt_len = 64;
1650
Manuel Pégourié-Gonnardde047ad2019-05-03 09:46:14 +02001651#if !defined(MBEDTLS_DEBUG_C) && \
1652 !defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET) && \
1653 !(defined(MBEDTLS_USE_PSA_CRYPTO) && \
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001654 defined(MBEDTLS_KEY_EXCHANGE_PSK_ENABLED))
Manuel Pégourié-Gonnarda7505d12019-05-07 10:17:56 +02001655 ssl = NULL; /* make sure we don't use it except for those cases */
Manuel Pégourié-Gonnardde047ad2019-05-03 09:46:14 +02001656 (void) ssl;
1657#endif
Manuel Pégourié-Gonnardde047ad2019-05-03 09:46:14 +02001658
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001659 if (handshake->resume != 0) {
1660 MBEDTLS_SSL_DEBUG_MSG(3, ("no premaster (session resumed)"));
1661 return 0;
Manuel Pégourié-Gonnard9951b712019-04-30 12:08:59 +02001662 }
Manuel Pégourié-Gonnard9951b712019-04-30 12:08:59 +02001663
1664#if defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001665 if (handshake->extended_ms == MBEDTLS_SSL_EXTENDED_MS_ENABLED) {
Manuel Pégourié-Gonnard85680c42019-05-03 09:16:16 +02001666 lbl = "extended master secret";
1667 salt = session_hash;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001668 handshake->calc_verify(ssl, session_hash, &salt_len);
Manuel Pégourié-Gonnard85680c42019-05-03 09:16:16 +02001669
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001670 MBEDTLS_SSL_DEBUG_BUF(3, "session hash for extended master secret",
1671 session_hash, salt_len);
Manuel Pégourié-Gonnard85680c42019-05-03 09:16:16 +02001672 }
Manuel Pégourié-Gonnard9951b712019-04-30 12:08:59 +02001673#endif /* MBEDTLS_SSL_EXTENDED_MS_ENABLED */
1674
1675#if defined(MBEDTLS_USE_PSA_CRYPTO) && \
Manuel Pégourié-Gonnardde047ad2019-05-03 09:46:14 +02001676 defined(MBEDTLS_KEY_EXCHANGE_PSK_ENABLED)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001677 if (handshake->ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_PSK &&
Manuel Pégourié-Gonnardde718b92019-05-03 11:43:28 +02001678 ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_3 &&
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001679 ssl_use_opaque_psk(ssl) == 1) {
Manuel Pégourié-Gonnard85680c42019-05-03 09:16:16 +02001680 /* Perform PSK-to-MS expansion in a single step. */
1681 psa_status_t status;
1682 psa_algorithm_t alg;
Ronald Croncf56a0a2020-08-04 09:51:30 +02001683 psa_key_id_t psk;
Manuel Pégourié-Gonnard85680c42019-05-03 09:16:16 +02001684 psa_key_derivation_operation_t derivation =
1685 PSA_KEY_DERIVATION_OPERATION_INIT;
Manuel Pégourié-Gonnardde718b92019-05-03 11:43:28 +02001686 mbedtls_md_type_t hash_alg = handshake->ciphersuite_info->mac;
Manuel Pégourié-Gonnard9951b712019-04-30 12:08:59 +02001687
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001688 MBEDTLS_SSL_DEBUG_MSG(2, ("perform PSA-based PSK-to-MS expansion"));
Manuel Pégourié-Gonnard9951b712019-04-30 12:08:59 +02001689
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001690 psk = mbedtls_ssl_get_opaque_psk(ssl);
Manuel Pégourié-Gonnard9951b712019-04-30 12:08:59 +02001691
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001692 if (hash_alg == MBEDTLS_MD_SHA384) {
Manuel Pégourié-Gonnard85680c42019-05-03 09:16:16 +02001693 alg = PSA_ALG_TLS12_PSK_TO_MS(PSA_ALG_SHA_384);
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001694 } else {
Manuel Pégourié-Gonnard85680c42019-05-03 09:16:16 +02001695 alg = PSA_ALG_TLS12_PSK_TO_MS(PSA_ALG_SHA_256);
Manuel Pégourié-Gonnard9951b712019-04-30 12:08:59 +02001696 }
Manuel Pégourié-Gonnard85680c42019-05-03 09:16:16 +02001697
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001698 status = setup_psa_key_derivation(&derivation, psk, alg,
1699 salt, salt_len,
1700 (unsigned char const *) lbl,
1701 (size_t) strlen(lbl),
1702 master_secret_len);
1703 if (status != PSA_SUCCESS) {
1704 psa_key_derivation_abort(&derivation);
1705 return MBEDTLS_ERR_SSL_HW_ACCEL_FAILED;
Manuel Pégourié-Gonnard85680c42019-05-03 09:16:16 +02001706 }
1707
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001708 status = psa_key_derivation_output_bytes(&derivation,
1709 master,
1710 master_secret_len);
1711 if (status != PSA_SUCCESS) {
1712 psa_key_derivation_abort(&derivation);
1713 return MBEDTLS_ERR_SSL_HW_ACCEL_FAILED;
1714 }
1715
1716 status = psa_key_derivation_abort(&derivation);
1717 if (status != PSA_SUCCESS) {
1718 return MBEDTLS_ERR_SSL_HW_ACCEL_FAILED;
1719 }
1720 } else
Manuel Pégourié-Gonnard85680c42019-05-03 09:16:16 +02001721#endif
1722 {
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001723 ret = handshake->tls_prf(handshake->premaster, handshake->pmslen,
1724 lbl, salt, salt_len,
1725 master,
1726 master_secret_len);
1727 if (ret != 0) {
1728 MBEDTLS_SSL_DEBUG_RET(1, "prf", ret);
1729 return ret;
Manuel Pégourié-Gonnard85680c42019-05-03 09:16:16 +02001730 }
1731
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001732 MBEDTLS_SSL_DEBUG_BUF(3, "premaster secret",
1733 handshake->premaster,
1734 handshake->pmslen);
Manuel Pégourié-Gonnard85680c42019-05-03 09:16:16 +02001735
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001736 mbedtls_platform_zeroize(handshake->premaster,
1737 sizeof(handshake->premaster));
Manuel Pégourié-Gonnard9951b712019-04-30 12:08:59 +02001738 }
1739
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001740 return 0;
Manuel Pégourié-Gonnard9951b712019-04-30 12:08:59 +02001741}
Manuel Pégourié-Gonnard1b00c4f2019-04-30 11:54:22 +02001742
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001743int mbedtls_ssl_derive_keys(mbedtls_ssl_context *ssl)
Manuel Pégourié-Gonnarde59ae232019-04-30 11:41:40 +02001744{
Janos Follath865b3eb2019-12-16 11:46:15 +00001745 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnard8d2805c2019-04-30 12:08:59 +02001746 const mbedtls_ssl_ciphersuite_t * const ciphersuite_info =
1747 ssl->handshake->ciphersuite_info;
Manuel Pégourié-Gonnard1b00c4f2019-04-30 11:54:22 +02001748
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001749 MBEDTLS_SSL_DEBUG_MSG(2, ("=> derive keys"));
Manuel Pégourié-Gonnard040a9512019-05-06 12:05:58 +02001750
1751 /* Set PRF, calc_verify and calc_finished function pointers */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001752 ret = ssl_set_handshake_prfs(ssl->handshake,
1753 ssl->minor_ver,
1754 ciphersuite_info->mac);
1755 if (ret != 0) {
1756 MBEDTLS_SSL_DEBUG_RET(1, "ssl_set_handshake_prfs", ret);
1757 return ret;
Manuel Pégourié-Gonnard8d2805c2019-04-30 12:08:59 +02001758 }
Manuel Pégourié-Gonnard1b00c4f2019-04-30 11:54:22 +02001759
Manuel Pégourié-Gonnard040a9512019-05-06 12:05:58 +02001760 /* Compute master secret if needed */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001761 ret = ssl_compute_master(ssl->handshake,
1762 ssl->session_negotiate->master,
1763 ssl);
1764 if (ret != 0) {
1765 MBEDTLS_SSL_DEBUG_RET(1, "ssl_compute_master", ret);
1766 return ret;
Manuel Pégourié-Gonnard9951b712019-04-30 12:08:59 +02001767 }
1768
Manuel Pégourié-Gonnard040a9512019-05-06 12:05:58 +02001769 /* Swap the client and server random values:
1770 * - MS derivation wanted client+server (RFC 5246 8.1)
1771 * - key derivation wants server+client (RFC 5246 6.3) */
1772 {
1773 unsigned char tmp[64];
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001774 memcpy(tmp, ssl->handshake->randbytes, 64);
1775 memcpy(ssl->handshake->randbytes, tmp + 32, 32);
1776 memcpy(ssl->handshake->randbytes + 32, tmp, 32);
1777 mbedtls_platform_zeroize(tmp, sizeof(tmp));
Manuel Pégourié-Gonnard040a9512019-05-06 12:05:58 +02001778 }
1779
1780 /* Populate transform structure */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001781 ret = ssl_populate_transform(ssl->transform_negotiate,
1782 ssl->session_negotiate->ciphersuite,
1783 ssl->session_negotiate->master,
Jarno Lamsac84bd242019-08-16 12:06:56 +03001784#if defined(MBEDTLS_SSL_SOME_MODES_USE_MAC)
Manuel Pégourié-Gonnard6fa57bf2019-05-10 10:50:04 +02001785#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001786 ssl->session_negotiate->encrypt_then_mac,
Jarno Lamsac84bd242019-08-16 12:06:56 +03001787#endif /* MBEDTLS_SSL_ENCRYPT_THEN_MAC */
Manuel Pégourié-Gonnard6fa57bf2019-05-10 10:50:04 +02001788#if defined(MBEDTLS_SSL_TRUNCATED_HMAC)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001789 ssl->session_negotiate->trunc_hmac,
Jarno Lamsac84bd242019-08-16 12:06:56 +03001790#endif /* MBEDTLS_SSL_TRUNCATED_HMAC */
1791#endif /* MBEDTLS_SSL_SOME_MODES_USE_MAC */
Manuel Pégourié-Gonnard6fa57bf2019-05-10 10:50:04 +02001792#if defined(MBEDTLS_ZLIB_SUPPORT)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001793 ssl->session_negotiate->compression,
Manuel Pégourié-Gonnard6fa57bf2019-05-10 10:50:04 +02001794#endif
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001795 ssl->handshake->tls_prf,
1796 ssl->handshake->randbytes,
1797 ssl->minor_ver,
1798 ssl->conf->endpoint,
1799 ssl);
1800 if (ret != 0) {
1801 MBEDTLS_SSL_DEBUG_RET(1, "ssl_populate_transform", ret);
1802 return ret;
Manuel Pégourié-Gonnard040a9512019-05-06 12:05:58 +02001803 }
1804
1805 /* We no longer need Server/ClientHello.random values */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001806 mbedtls_platform_zeroize(ssl->handshake->randbytes,
1807 sizeof(ssl->handshake->randbytes));
Manuel Pégourié-Gonnard040a9512019-05-06 12:05:58 +02001808
Manuel Pégourié-Gonnardd73b47f2019-05-06 12:44:24 +02001809 /* Allocate compression buffer */
1810#if defined(MBEDTLS_ZLIB_SUPPORT)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001811 if (ssl->session_negotiate->compression == MBEDTLS_SSL_COMPRESS_DEFLATE &&
1812 ssl->compress_buf == NULL) {
1813 MBEDTLS_SSL_DEBUG_MSG(3, ("Allocating compression buffer"));
1814 ssl->compress_buf = mbedtls_calloc(1, MBEDTLS_SSL_COMPRESS_BUFFER_LEN);
1815 if (ssl->compress_buf == NULL) {
1816 MBEDTLS_SSL_DEBUG_MSG(1, ("alloc(%d bytes) failed",
1817 MBEDTLS_SSL_COMPRESS_BUFFER_LEN));
1818 return MBEDTLS_ERR_SSL_ALLOC_FAILED;
Manuel Pégourié-Gonnardd73b47f2019-05-06 12:44:24 +02001819 }
1820 }
1821#endif
1822
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001823 MBEDTLS_SSL_DEBUG_MSG(2, ("<= derive keys"));
Manuel Pégourié-Gonnard040a9512019-05-06 12:05:58 +02001824
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001825 return 0;
Manuel Pégourié-Gonnarde59ae232019-04-30 11:41:40 +02001826}
1827
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001828#if defined(MBEDTLS_SSL_PROTO_SSL3)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001829void ssl_calc_verify_ssl(const mbedtls_ssl_context *ssl,
1830 unsigned char *hash,
1831 size_t *hlen)
Paul Bakker5121ce52009-01-03 21:22:43 +00001832{
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02001833 mbedtls_md5_context md5;
1834 mbedtls_sha1_context sha1;
Paul Bakker5121ce52009-01-03 21:22:43 +00001835 unsigned char pad_1[48];
1836 unsigned char pad_2[48];
1837
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001838 MBEDTLS_SSL_DEBUG_MSG(2, ("=> calc verify ssl"));
Paul Bakker5121ce52009-01-03 21:22:43 +00001839
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001840 mbedtls_md5_init(&md5);
1841 mbedtls_sha1_init(&sha1);
Manuel Pégourié-Gonnard001f2b62015-07-06 16:21:13 +02001842
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001843 mbedtls_md5_clone(&md5, &ssl->handshake->fin_md5);
1844 mbedtls_sha1_clone(&sha1, &ssl->handshake->fin_sha1);
Paul Bakker5121ce52009-01-03 21:22:43 +00001845
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001846 memset(pad_1, 0x36, 48);
1847 memset(pad_2, 0x5C, 48);
Paul Bakker5121ce52009-01-03 21:22:43 +00001848
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001849 mbedtls_md5_update_ret(&md5, ssl->session_negotiate->master, 48);
1850 mbedtls_md5_update_ret(&md5, pad_1, 48);
1851 mbedtls_md5_finish_ret(&md5, hash);
Paul Bakker5121ce52009-01-03 21:22:43 +00001852
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001853 mbedtls_md5_starts_ret(&md5);
1854 mbedtls_md5_update_ret(&md5, ssl->session_negotiate->master, 48);
1855 mbedtls_md5_update_ret(&md5, pad_2, 48);
1856 mbedtls_md5_update_ret(&md5, hash, 16);
1857 mbedtls_md5_finish_ret(&md5, hash);
Paul Bakker5121ce52009-01-03 21:22:43 +00001858
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001859 mbedtls_sha1_update_ret(&sha1, ssl->session_negotiate->master, 48);
1860 mbedtls_sha1_update_ret(&sha1, pad_1, 40);
1861 mbedtls_sha1_finish_ret(&sha1, hash + 16);
Paul Bakker380da532012-04-18 16:10:25 +00001862
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001863 mbedtls_sha1_starts_ret(&sha1);
1864 mbedtls_sha1_update_ret(&sha1, ssl->session_negotiate->master, 48);
1865 mbedtls_sha1_update_ret(&sha1, pad_2, 40);
1866 mbedtls_sha1_update_ret(&sha1, hash + 16, 20);
1867 mbedtls_sha1_finish_ret(&sha1, hash + 16);
Paul Bakker380da532012-04-18 16:10:25 +00001868
Manuel Pégourié-Gonnardde718b92019-05-03 11:43:28 +02001869 *hlen = 36;
1870
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001871 MBEDTLS_SSL_DEBUG_BUF(3, "calculated verify result", hash, *hlen);
1872 MBEDTLS_SSL_DEBUG_MSG(2, ("<= calc verify"));
Paul Bakker380da532012-04-18 16:10:25 +00001873
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001874 mbedtls_md5_free(&md5);
1875 mbedtls_sha1_free(&sha1);
Paul Bakker5b4af392014-06-26 12:09:34 +02001876
Paul Bakker380da532012-04-18 16:10:25 +00001877 return;
1878}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001879#endif /* MBEDTLS_SSL_PROTO_SSL3 */
Paul Bakker380da532012-04-18 16:10:25 +00001880
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001881#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001882void ssl_calc_verify_tls(const mbedtls_ssl_context *ssl,
1883 unsigned char *hash,
1884 size_t *hlen)
Paul Bakker380da532012-04-18 16:10:25 +00001885{
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02001886 mbedtls_md5_context md5;
1887 mbedtls_sha1_context sha1;
Paul Bakker380da532012-04-18 16:10:25 +00001888
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001889 MBEDTLS_SSL_DEBUG_MSG(2, ("=> calc verify tls"));
Paul Bakker380da532012-04-18 16:10:25 +00001890
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001891 mbedtls_md5_init(&md5);
1892 mbedtls_sha1_init(&sha1);
Manuel Pégourié-Gonnard001f2b62015-07-06 16:21:13 +02001893
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001894 mbedtls_md5_clone(&md5, &ssl->handshake->fin_md5);
1895 mbedtls_sha1_clone(&sha1, &ssl->handshake->fin_sha1);
Paul Bakker380da532012-04-18 16:10:25 +00001896
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001897 mbedtls_md5_finish_ret(&md5, hash);
1898 mbedtls_sha1_finish_ret(&sha1, hash + 16);
Paul Bakker380da532012-04-18 16:10:25 +00001899
Manuel Pégourié-Gonnardde718b92019-05-03 11:43:28 +02001900 *hlen = 36;
1901
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001902 MBEDTLS_SSL_DEBUG_BUF(3, "calculated verify result", hash, *hlen);
1903 MBEDTLS_SSL_DEBUG_MSG(2, ("<= calc verify"));
Paul Bakker380da532012-04-18 16:10:25 +00001904
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001905 mbedtls_md5_free(&md5);
1906 mbedtls_sha1_free(&sha1);
Paul Bakker5b4af392014-06-26 12:09:34 +02001907
Paul Bakker380da532012-04-18 16:10:25 +00001908 return;
1909}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001910#endif /* MBEDTLS_SSL_PROTO_TLS1 || MBEDTLS_SSL_PROTO_TLS1_1 */
Paul Bakker380da532012-04-18 16:10:25 +00001911
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001912#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
1913#if defined(MBEDTLS_SHA256_C)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001914void ssl_calc_verify_tls_sha256(const mbedtls_ssl_context *ssl,
1915 unsigned char *hash,
1916 size_t *hlen)
Paul Bakker380da532012-04-18 16:10:25 +00001917{
Andrzej Kurekeb342242019-01-29 09:14:33 -05001918#if defined(MBEDTLS_USE_PSA_CRYPTO)
1919 size_t hash_size;
1920 psa_status_t status;
1921 psa_hash_operation_t sha256_psa = psa_hash_operation_init();
1922
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001923 MBEDTLS_SSL_DEBUG_MSG(2, ("=> PSA calc verify sha256"));
1924 status = psa_hash_clone(&ssl->handshake->fin_sha256_psa, &sha256_psa);
1925 if (status != PSA_SUCCESS) {
1926 MBEDTLS_SSL_DEBUG_MSG(2, ("PSA hash clone failed"));
Andrzej Kurekeb342242019-01-29 09:14:33 -05001927 return;
1928 }
1929
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001930 status = psa_hash_finish(&sha256_psa, hash, 32, &hash_size);
1931 if (status != PSA_SUCCESS) {
1932 MBEDTLS_SSL_DEBUG_MSG(2, ("PSA hash finish failed"));
Andrzej Kurekeb342242019-01-29 09:14:33 -05001933 return;
1934 }
Manuel Pégourié-Gonnardde718b92019-05-03 11:43:28 +02001935
1936 *hlen = 32;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001937 MBEDTLS_SSL_DEBUG_BUF(3, "PSA calculated verify result", hash, *hlen);
1938 MBEDTLS_SSL_DEBUG_MSG(2, ("<= PSA calc verify"));
Andrzej Kurekeb342242019-01-29 09:14:33 -05001939#else
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02001940 mbedtls_sha256_context sha256;
Paul Bakker380da532012-04-18 16:10:25 +00001941
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001942 mbedtls_sha256_init(&sha256);
Manuel Pégourié-Gonnard001f2b62015-07-06 16:21:13 +02001943
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001944 MBEDTLS_SSL_DEBUG_MSG(2, ("=> calc verify sha256"));
Paul Bakker380da532012-04-18 16:10:25 +00001945
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001946 mbedtls_sha256_clone(&sha256, &ssl->handshake->fin_sha256);
1947 mbedtls_sha256_finish_ret(&sha256, hash);
Paul Bakker380da532012-04-18 16:10:25 +00001948
Manuel Pégourié-Gonnardde718b92019-05-03 11:43:28 +02001949 *hlen = 32;
1950
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001951 MBEDTLS_SSL_DEBUG_BUF(3, "calculated verify result", hash, *hlen);
1952 MBEDTLS_SSL_DEBUG_MSG(2, ("<= calc verify"));
Paul Bakker380da532012-04-18 16:10:25 +00001953
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001954 mbedtls_sha256_free(&sha256);
Andrzej Kurekeb342242019-01-29 09:14:33 -05001955#endif /* MBEDTLS_USE_PSA_CRYPTO */
Paul Bakker380da532012-04-18 16:10:25 +00001956 return;
1957}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001958#endif /* MBEDTLS_SHA256_C */
Paul Bakker380da532012-04-18 16:10:25 +00001959
Gilles Peskined2d59372021-05-12 22:43:27 +02001960#if defined(MBEDTLS_SHA512_C) && !defined(MBEDTLS_SHA512_NO_SHA384)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001961void ssl_calc_verify_tls_sha384(const mbedtls_ssl_context *ssl,
1962 unsigned char *hash,
1963 size_t *hlen)
Paul Bakker380da532012-04-18 16:10:25 +00001964{
Andrzej Kurekeb342242019-01-29 09:14:33 -05001965#if defined(MBEDTLS_USE_PSA_CRYPTO)
1966 size_t hash_size;
1967 psa_status_t status;
Andrzej Kurek972fba52019-01-30 03:29:12 -05001968 psa_hash_operation_t sha384_psa = psa_hash_operation_init();
Andrzej Kurekeb342242019-01-29 09:14:33 -05001969
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001970 MBEDTLS_SSL_DEBUG_MSG(2, ("=> PSA calc verify sha384"));
1971 status = psa_hash_clone(&ssl->handshake->fin_sha384_psa, &sha384_psa);
1972 if (status != PSA_SUCCESS) {
1973 MBEDTLS_SSL_DEBUG_MSG(2, ("PSA hash clone failed"));
Andrzej Kurekeb342242019-01-29 09:14:33 -05001974 return;
1975 }
1976
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001977 status = psa_hash_finish(&sha384_psa, hash, 48, &hash_size);
1978 if (status != PSA_SUCCESS) {
1979 MBEDTLS_SSL_DEBUG_MSG(2, ("PSA hash finish failed"));
Andrzej Kurekeb342242019-01-29 09:14:33 -05001980 return;
1981 }
Manuel Pégourié-Gonnardde718b92019-05-03 11:43:28 +02001982
1983 *hlen = 48;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001984 MBEDTLS_SSL_DEBUG_BUF(3, "PSA calculated verify result", hash, *hlen);
1985 MBEDTLS_SSL_DEBUG_MSG(2, ("<= PSA calc verify"));
Andrzej Kurekeb342242019-01-29 09:14:33 -05001986#else
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02001987 mbedtls_sha512_context sha512;
Paul Bakker380da532012-04-18 16:10:25 +00001988
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001989 mbedtls_sha512_init(&sha512);
Manuel Pégourié-Gonnard001f2b62015-07-06 16:21:13 +02001990
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001991 MBEDTLS_SSL_DEBUG_MSG(2, ("=> calc verify sha384"));
Paul Bakker380da532012-04-18 16:10:25 +00001992
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001993 mbedtls_sha512_clone(&sha512, &ssl->handshake->fin_sha512);
1994 mbedtls_sha512_finish_ret(&sha512, hash);
Paul Bakker5121ce52009-01-03 21:22:43 +00001995
Manuel Pégourié-Gonnardde718b92019-05-03 11:43:28 +02001996 *hlen = 48;
1997
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001998 MBEDTLS_SSL_DEBUG_BUF(3, "calculated verify result", hash, *hlen);
1999 MBEDTLS_SSL_DEBUG_MSG(2, ("<= calc verify"));
Paul Bakker5121ce52009-01-03 21:22:43 +00002000
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002001 mbedtls_sha512_free(&sha512);
Andrzej Kurekeb342242019-01-29 09:14:33 -05002002#endif /* MBEDTLS_USE_PSA_CRYPTO */
Paul Bakker5121ce52009-01-03 21:22:43 +00002003 return;
2004}
Gilles Peskined2d59372021-05-12 22:43:27 +02002005#endif /* MBEDTLS_SHA512_C && !MBEDTLS_SHA512_NO_SHA384 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002006#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakker5121ce52009-01-03 21:22:43 +00002007
Gilles Peskineeccd8882020-03-10 12:19:08 +01002008#if defined(MBEDTLS_KEY_EXCHANGE_SOME_PSK_ENABLED)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002009int 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 +02002010{
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02002011 unsigned char *p = ssl->handshake->premaster;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002012 unsigned char *end = p + sizeof(ssl->handshake->premaster);
Guilhem Bryantb5f04e42020-04-01 11:23:58 +01002013 const unsigned char *psk = NULL;
Guilhem Bryant61b0fe62020-03-27 11:12:12 +00002014 size_t psk_len = 0;
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01002015
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002016 if (mbedtls_ssl_get_psk(ssl, &psk, &psk_len)
2017 == MBEDTLS_ERR_SSL_PRIVATE_KEY_REQUIRED) {
Guilhem Bryantc5285d82020-03-25 17:08:15 +00002018 /*
2019 * This should never happen because the existence of a PSK is always
2020 * checked before calling this function
2021 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002022 MBEDTLS_SSL_DEBUG_MSG(1, ("should never happen"));
2023 return MBEDTLS_ERR_SSL_INTERNAL_ERROR;
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01002024 }
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02002025
2026 /*
2027 * PMS = struct {
2028 * opaque other_secret<0..2^16-1>;
2029 * opaque psk<0..2^16-1>;
2030 * };
2031 * with "other_secret" depending on the particular key exchange
2032 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002033#if defined(MBEDTLS_KEY_EXCHANGE_PSK_ENABLED)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002034 if (key_ex == MBEDTLS_KEY_EXCHANGE_PSK) {
2035 if (end - p < 2) {
2036 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
2037 }
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02002038
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002039 MBEDTLS_PUT_UINT16_BE(psk_len, p, 0);
Joe Subbiania651e6f2021-08-23 11:35:25 +01002040 p += 2;
Manuel Pégourié-Gonnardbc5e5082015-10-21 12:35:29 +02002041
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002042 if (end < p || (size_t) (end - p) < psk_len) {
2043 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
2044 }
Manuel Pégourié-Gonnardbc5e5082015-10-21 12:35:29 +02002045
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002046 memset(p, 0, psk_len);
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01002047 p += psk_len;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002048 } else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002049#endif /* MBEDTLS_KEY_EXCHANGE_PSK_ENABLED */
2050#if defined(MBEDTLS_KEY_EXCHANGE_RSA_PSK_ENABLED)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002051 if (key_ex == MBEDTLS_KEY_EXCHANGE_RSA_PSK) {
Manuel Pégourié-Gonnard0fae60b2013-10-14 17:39:48 +02002052 /*
2053 * other_secret already set by the ClientKeyExchange message,
2054 * and is 48 bytes long
2055 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002056 if (end - p < 2) {
2057 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
2058 }
Philippe Antoine747fd532018-05-30 09:13:21 +02002059
Manuel Pégourié-Gonnard0fae60b2013-10-14 17:39:48 +02002060 *p++ = 0;
2061 *p++ = 48;
2062 p += 48;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002063 } else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002064#endif /* MBEDTLS_KEY_EXCHANGE_RSA_PSK_ENABLED */
2065#if defined(MBEDTLS_KEY_EXCHANGE_DHE_PSK_ENABLED)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002066 if (key_ex == MBEDTLS_KEY_EXCHANGE_DHE_PSK) {
Janos Follath865b3eb2019-12-16 11:46:15 +00002067 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnard33352052015-06-02 16:17:08 +01002068 size_t len;
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02002069
Manuel Pégourié-Gonnard8df68632014-06-23 17:56:08 +02002070 /* Write length only when we know the actual value */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002071 if ((ret = mbedtls_dhm_calc_secret(&ssl->handshake->dhm_ctx,
2072 p + 2, end - (p + 2), &len,
2073 ssl->conf->f_rng, ssl->conf->p_rng)) != 0) {
2074 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_dhm_calc_secret", ret);
2075 return ret;
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02002076 }
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002077 MBEDTLS_PUT_UINT16_BE(len, p, 0);
Joe Subbiania651e6f2021-08-23 11:35:25 +01002078 p += 2 + len;
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02002079
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002080 MBEDTLS_SSL_DEBUG_MPI(3, "DHM: K ", &ssl->handshake->dhm_ctx.K);
2081 } else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002082#endif /* MBEDTLS_KEY_EXCHANGE_DHE_PSK_ENABLED */
2083#if defined(MBEDTLS_KEY_EXCHANGE_ECDHE_PSK_ENABLED)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002084 if (key_ex == MBEDTLS_KEY_EXCHANGE_ECDHE_PSK) {
Janos Follath865b3eb2019-12-16 11:46:15 +00002085 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02002086 size_t zlen;
2087
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002088 if ((ret = mbedtls_ecdh_calc_secret(&ssl->handshake->ecdh_ctx, &zlen,
2089 p + 2, end - (p + 2),
2090 ssl->conf->f_rng, ssl->conf->p_rng)) != 0) {
2091 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ecdh_calc_secret", ret);
2092 return ret;
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02002093 }
2094
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002095 MBEDTLS_PUT_UINT16_BE(zlen, p, 0);
Joe Subbiania651e6f2021-08-23 11:35:25 +01002096 p += 2 + zlen;
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02002097
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002098 MBEDTLS_SSL_DEBUG_ECDH(3, &ssl->handshake->ecdh_ctx,
2099 MBEDTLS_DEBUG_ECDH_Z);
2100 } else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002101#endif /* MBEDTLS_KEY_EXCHANGE_ECDHE_PSK_ENABLED */
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02002102 {
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002103 MBEDTLS_SSL_DEBUG_MSG(1, ("should never happen"));
2104 return MBEDTLS_ERR_SSL_INTERNAL_ERROR;
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02002105 }
2106
2107 /* opaque psk<0..2^16-1>; */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002108 if (end - p < 2) {
2109 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
2110 }
Manuel Pégourié-Gonnardb2bf5a12014-03-25 16:28:12 +01002111
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002112 MBEDTLS_PUT_UINT16_BE(psk_len, p, 0);
Joe Subbiania651e6f2021-08-23 11:35:25 +01002113 p += 2;
Manuel Pégourié-Gonnardbc5e5082015-10-21 12:35:29 +02002114
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002115 if (end < p || (size_t) (end - p) < psk_len) {
2116 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
2117 }
Manuel Pégourié-Gonnardbc5e5082015-10-21 12:35:29 +02002118
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002119 memcpy(p, psk, psk_len);
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01002120 p += psk_len;
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02002121
2122 ssl->handshake->pmslen = p - ssl->handshake->premaster;
2123
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002124 return 0;
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02002125}
Gilles Peskineeccd8882020-03-10 12:19:08 +01002126#endif /* MBEDTLS_KEY_EXCHANGE_SOME_PSK_ENABLED */
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02002127
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002128#if defined(MBEDTLS_SSL_SRV_C) && defined(MBEDTLS_SSL_RENEGOTIATION)
Manuel Pégourié-Gonnardd904d662022-06-17 10:24:00 +02002129MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002130static int ssl_write_hello_request(mbedtls_ssl_context *ssl);
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +02002131
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002132#if defined(MBEDTLS_SSL_PROTO_DTLS)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002133int mbedtls_ssl_resend_hello_request(mbedtls_ssl_context *ssl)
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +02002134{
2135 /* If renegotiation is not enforced, retransmit until we would reach max
2136 * timeout if we were using the usual handshake doubling scheme */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002137 if (ssl->conf->renego_max_records < 0) {
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02002138 uint32_t ratio = ssl->conf->hs_timeout_max / ssl->conf->hs_timeout_min + 1;
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +02002139 unsigned char doublings = 1;
2140
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002141 while (ratio != 0) {
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +02002142 ++doublings;
2143 ratio >>= 1;
2144 }
2145
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002146 if (++ssl->renego_records_seen > doublings) {
2147 MBEDTLS_SSL_DEBUG_MSG(2, ("no longer retransmitting hello request"));
2148 return 0;
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +02002149 }
2150 }
2151
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002152 return ssl_write_hello_request(ssl);
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +02002153}
2154#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002155#endif /* MBEDTLS_SSL_SRV_C && MBEDTLS_SSL_RENEGOTIATION */
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02002156
Hanno Beckerb9d44792019-02-08 07:19:04 +00002157#if defined(MBEDTLS_X509_CRT_PARSE_C)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002158static void ssl_clear_peer_cert(mbedtls_ssl_session *session)
Hanno Beckerb9d44792019-02-08 07:19:04 +00002159{
2160#if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002161 if (session->peer_cert != NULL) {
2162 mbedtls_x509_crt_free(session->peer_cert);
2163 mbedtls_free(session->peer_cert);
Hanno Beckerb9d44792019-02-08 07:19:04 +00002164 session->peer_cert = NULL;
2165 }
2166#else /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002167 if (session->peer_cert_digest != NULL) {
Hanno Beckerb9d44792019-02-08 07:19:04 +00002168 /* Zeroization is not necessary. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002169 mbedtls_free(session->peer_cert_digest);
Hanno Beckerb9d44792019-02-08 07:19:04 +00002170 session->peer_cert_digest = NULL;
2171 session->peer_cert_digest_type = MBEDTLS_MD_NONE;
2172 session->peer_cert_digest_len = 0;
2173 }
2174#endif /* !MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
2175}
2176#endif /* MBEDTLS_X509_CRT_PARSE_C */
2177
Paul Bakker5121ce52009-01-03 21:22:43 +00002178/*
2179 * Handshake functions
2180 */
Gilles Peskineeccd8882020-03-10 12:19:08 +01002181#if !defined(MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED)
Gilles Peskinef9828522017-05-03 12:28:43 +02002182/* No certificate support -> dummy functions */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002183int mbedtls_ssl_write_certificate(mbedtls_ssl_context *ssl)
Paul Bakker5121ce52009-01-03 21:22:43 +00002184{
Hanno Beckere694c3e2017-12-27 21:34:08 +00002185 const mbedtls_ssl_ciphersuite_t *ciphersuite_info =
2186 ssl->handshake->ciphersuite_info;
Paul Bakker5121ce52009-01-03 21:22:43 +00002187
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002188 MBEDTLS_SSL_DEBUG_MSG(2, ("=> write certificate"));
Paul Bakker5121ce52009-01-03 21:22:43 +00002189
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002190 if (!mbedtls_ssl_ciphersuite_uses_srv_cert(ciphersuite_info)) {
2191 MBEDTLS_SSL_DEBUG_MSG(2, ("<= skip write certificate"));
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02002192 ssl->state++;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002193 return 0;
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02002194 }
2195
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002196 MBEDTLS_SSL_DEBUG_MSG(1, ("should never happen"));
2197 return MBEDTLS_ERR_SSL_INTERNAL_ERROR;
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002198}
2199
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002200int mbedtls_ssl_parse_certificate(mbedtls_ssl_context *ssl)
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002201{
Hanno Beckere694c3e2017-12-27 21:34:08 +00002202 const mbedtls_ssl_ciphersuite_t *ciphersuite_info =
2203 ssl->handshake->ciphersuite_info;
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002204
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002205 MBEDTLS_SSL_DEBUG_MSG(2, ("=> parse certificate"));
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002206
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002207 if (!mbedtls_ssl_ciphersuite_uses_srv_cert(ciphersuite_info)) {
2208 MBEDTLS_SSL_DEBUG_MSG(2, ("<= skip parse certificate"));
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002209 ssl->state++;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002210 return 0;
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002211 }
2212
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002213 MBEDTLS_SSL_DEBUG_MSG(1, ("should never happen"));
2214 return MBEDTLS_ERR_SSL_INTERNAL_ERROR;
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002215}
Gilles Peskinef9828522017-05-03 12:28:43 +02002216
Gilles Peskineeccd8882020-03-10 12:19:08 +01002217#else /* MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED */
Gilles Peskinef9828522017-05-03 12:28:43 +02002218/* Some certificate support -> implement write and parse */
2219
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002220int mbedtls_ssl_write_certificate(mbedtls_ssl_context *ssl)
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002221{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002222 int ret = MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002223 size_t i, n;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002224 const mbedtls_x509_crt *crt;
Hanno Beckere694c3e2017-12-27 21:34:08 +00002225 const mbedtls_ssl_ciphersuite_t *ciphersuite_info =
2226 ssl->handshake->ciphersuite_info;
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002227
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002228 MBEDTLS_SSL_DEBUG_MSG(2, ("=> write certificate"));
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002229
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002230 if (!mbedtls_ssl_ciphersuite_uses_srv_cert(ciphersuite_info)) {
2231 MBEDTLS_SSL_DEBUG_MSG(2, ("<= skip write certificate"));
Johan Pascal4f099262020-09-22 10:59:26 +02002232 ssl->state++;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002233 return 0;
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002234 }
2235
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002236#if defined(MBEDTLS_SSL_CLI_C)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002237 if (ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT) {
2238 if (ssl->client_auth == 0) {
2239 MBEDTLS_SSL_DEBUG_MSG(2, ("<= skip write certificate"));
Paul Bakker5121ce52009-01-03 21:22:43 +00002240 ssl->state++;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002241 return 0;
Paul Bakker5121ce52009-01-03 21:22:43 +00002242 }
2243
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002244#if defined(MBEDTLS_SSL_PROTO_SSL3)
Paul Bakker5121ce52009-01-03 21:22:43 +00002245 /*
2246 * If using SSLv3 and got no cert, send an Alert message
2247 * (otherwise an empty Certificate message will be sent).
2248 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002249 if (mbedtls_ssl_own_cert(ssl) == NULL &&
2250 ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0) {
Paul Bakker5121ce52009-01-03 21:22:43 +00002251 ssl->out_msglen = 2;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002252 ssl->out_msgtype = MBEDTLS_SSL_MSG_ALERT;
2253 ssl->out_msg[0] = MBEDTLS_SSL_ALERT_LEVEL_WARNING;
2254 ssl->out_msg[1] = MBEDTLS_SSL_ALERT_MSG_NO_CERT;
Paul Bakker5121ce52009-01-03 21:22:43 +00002255
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002256 MBEDTLS_SSL_DEBUG_MSG(2, ("got no certificate to send"));
Paul Bakker5121ce52009-01-03 21:22:43 +00002257 goto write_msg;
2258 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002259#endif /* MBEDTLS_SSL_PROTO_SSL3 */
Paul Bakker5121ce52009-01-03 21:22:43 +00002260 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002261#endif /* MBEDTLS_SSL_CLI_C */
2262#if defined(MBEDTLS_SSL_SRV_C)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002263 if (ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER) {
2264 if (mbedtls_ssl_own_cert(ssl) == NULL) {
2265 MBEDTLS_SSL_DEBUG_MSG(1, ("got no certificate to send"));
2266 return MBEDTLS_ERR_SSL_CERTIFICATE_REQUIRED;
Paul Bakker5121ce52009-01-03 21:22:43 +00002267 }
2268 }
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01002269#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00002270
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002271 MBEDTLS_SSL_DEBUG_CRT(3, "own certificate", mbedtls_ssl_own_cert(ssl));
Paul Bakker5121ce52009-01-03 21:22:43 +00002272
2273 /*
2274 * 0 . 0 handshake type
2275 * 1 . 3 handshake length
2276 * 4 . 6 length of all certs
2277 * 7 . 9 length of cert. 1
2278 * 10 . n-1 peer certificate
2279 * n . n+2 length of cert. 2
2280 * n+3 . ... upper level cert, etc.
2281 */
2282 i = 7;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002283 crt = mbedtls_ssl_own_cert(ssl);
Paul Bakker5121ce52009-01-03 21:22:43 +00002284
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002285 while (crt != NULL) {
Paul Bakker5121ce52009-01-03 21:22:43 +00002286 n = crt->raw.len;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002287 if (n > MBEDTLS_SSL_OUT_CONTENT_LEN - 3 - i) {
2288 MBEDTLS_SSL_DEBUG_MSG(1, ("certificate too large, %" MBEDTLS_PRINTF_SIZET
2289 " > %" MBEDTLS_PRINTF_SIZET,
2290 i + 3 + n, (size_t) MBEDTLS_SSL_OUT_CONTENT_LEN));
2291 return MBEDTLS_ERR_SSL_CERTIFICATE_TOO_LARGE;
Paul Bakker5121ce52009-01-03 21:22:43 +00002292 }
2293
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002294 ssl->out_msg[i] = MBEDTLS_BYTE_2(n);
2295 ssl->out_msg[i + 1] = MBEDTLS_BYTE_1(n);
2296 ssl->out_msg[i + 2] = MBEDTLS_BYTE_0(n);
Paul Bakker5121ce52009-01-03 21:22:43 +00002297
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002298 i += 3; memcpy(ssl->out_msg + i, crt->raw.p, n);
Paul Bakker5121ce52009-01-03 21:22:43 +00002299 i += n; crt = crt->next;
2300 }
2301
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002302 ssl->out_msg[4] = MBEDTLS_BYTE_2(i - 7);
2303 ssl->out_msg[5] = MBEDTLS_BYTE_1(i - 7);
2304 ssl->out_msg[6] = MBEDTLS_BYTE_0(i - 7);
Paul Bakker5121ce52009-01-03 21:22:43 +00002305
2306 ssl->out_msglen = i;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002307 ssl->out_msgtype = MBEDTLS_SSL_MSG_HANDSHAKE;
2308 ssl->out_msg[0] = MBEDTLS_SSL_HS_CERTIFICATE;
Paul Bakker5121ce52009-01-03 21:22:43 +00002309
Manuel Pégourié-Gonnardcf141ca2015-05-20 10:35:51 +02002310#if defined(MBEDTLS_SSL_PROTO_SSL3) && defined(MBEDTLS_SSL_CLI_C)
Paul Bakker5121ce52009-01-03 21:22:43 +00002311write_msg:
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002312#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00002313
2314 ssl->state++;
2315
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002316 if ((ret = mbedtls_ssl_write_handshake_msg(ssl)) != 0) {
2317 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ssl_write_handshake_msg", ret);
2318 return ret;
Paul Bakker5121ce52009-01-03 21:22:43 +00002319 }
2320
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002321 MBEDTLS_SSL_DEBUG_MSG(2, ("<= write certificate"));
Paul Bakker5121ce52009-01-03 21:22:43 +00002322
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002323 return ret;
Paul Bakker5121ce52009-01-03 21:22:43 +00002324}
2325
Hanno Becker84879e32019-01-31 07:44:03 +00002326#if defined(MBEDTLS_SSL_RENEGOTIATION) && defined(MBEDTLS_SSL_CLI_C)
Hanno Becker177475a2019-02-05 17:02:46 +00002327
2328#if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
Manuel Pégourié-Gonnardd904d662022-06-17 10:24:00 +02002329MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002330static int ssl_check_peer_crt_unchanged(mbedtls_ssl_context *ssl,
2331 unsigned char *crt_buf,
2332 size_t crt_buf_len)
Hanno Beckerdef9bdc2019-01-30 14:46:46 +00002333{
2334 mbedtls_x509_crt const * const peer_crt = ssl->session->peer_cert;
2335
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002336 if (peer_crt == NULL) {
2337 return -1;
2338 }
Hanno Beckerdef9bdc2019-01-30 14:46:46 +00002339
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002340 if (peer_crt->raw.len != crt_buf_len) {
2341 return -1;
2342 }
Hanno Beckerdef9bdc2019-01-30 14:46:46 +00002343
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002344 return memcmp(peer_crt->raw.p, crt_buf, peer_crt->raw.len);
Hanno Beckerdef9bdc2019-01-30 14:46:46 +00002345}
Hanno Becker177475a2019-02-05 17:02:46 +00002346#else /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
Manuel Pégourié-Gonnardd904d662022-06-17 10:24:00 +02002347MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002348static int ssl_check_peer_crt_unchanged(mbedtls_ssl_context *ssl,
2349 unsigned char *crt_buf,
2350 size_t crt_buf_len)
Hanno Becker177475a2019-02-05 17:02:46 +00002351{
Janos Follath865b3eb2019-12-16 11:46:15 +00002352 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Hanno Becker177475a2019-02-05 17:02:46 +00002353 unsigned char const * const peer_cert_digest =
2354 ssl->session->peer_cert_digest;
2355 mbedtls_md_type_t const peer_cert_digest_type =
2356 ssl->session->peer_cert_digest_type;
2357 mbedtls_md_info_t const * const digest_info =
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002358 mbedtls_md_info_from_type(peer_cert_digest_type);
Hanno Becker177475a2019-02-05 17:02:46 +00002359 unsigned char tmp_digest[MBEDTLS_SSL_PEER_CERT_DIGEST_MAX_LEN];
2360 size_t digest_len;
2361
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002362 if (peer_cert_digest == NULL || digest_info == NULL) {
2363 return -1;
2364 }
Hanno Becker177475a2019-02-05 17:02:46 +00002365
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002366 digest_len = mbedtls_md_get_size(digest_info);
2367 if (digest_len > MBEDTLS_SSL_PEER_CERT_DIGEST_MAX_LEN) {
2368 return -1;
2369 }
Hanno Becker177475a2019-02-05 17:02:46 +00002370
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002371 ret = mbedtls_md(digest_info, crt_buf, crt_buf_len, tmp_digest);
2372 if (ret != 0) {
2373 return -1;
2374 }
Hanno Becker177475a2019-02-05 17:02:46 +00002375
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002376 return memcmp(tmp_digest, peer_cert_digest, digest_len);
Hanno Becker177475a2019-02-05 17:02:46 +00002377}
2378#endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
Hanno Becker84879e32019-01-31 07:44:03 +00002379#endif /* MBEDTLS_SSL_RENEGOTIATION && MBEDTLS_SSL_CLI_C */
Hanno Beckerdef9bdc2019-01-30 14:46:46 +00002380
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02002381/*
2382 * Once the certificate message is read, parse it into a cert chain and
2383 * perform basic checks, but leave actual verification to the caller
2384 */
Manuel Pégourié-Gonnardd904d662022-06-17 10:24:00 +02002385MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002386static int ssl_parse_certificate_chain(mbedtls_ssl_context *ssl,
2387 mbedtls_x509_crt *chain)
Paul Bakker5121ce52009-01-03 21:22:43 +00002388{
Janos Follath865b3eb2019-12-16 11:46:15 +00002389 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Hanno Beckerc7bd7802019-02-05 15:37:23 +00002390#if defined(MBEDTLS_SSL_RENEGOTIATION) && defined(MBEDTLS_SSL_CLI_C)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002391 int crt_cnt = 0;
Hanno Beckerc7bd7802019-02-05 15:37:23 +00002392#endif
Paul Bakker23986e52011-04-24 08:57:21 +00002393 size_t i, n;
Gilles Peskine064a85c2017-05-10 10:46:40 +02002394 uint8_t alert;
Paul Bakker5121ce52009-01-03 21:22:43 +00002395
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002396 if (ssl->in_msgtype != MBEDTLS_SSL_MSG_HANDSHAKE) {
2397 MBEDTLS_SSL_DEBUG_MSG(1, ("bad certificate message"));
2398 mbedtls_ssl_send_alert_message(ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
2399 MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE);
2400 return MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE;
Paul Bakker5121ce52009-01-03 21:22:43 +00002401 }
2402
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002403 if (ssl->in_msg[0] != MBEDTLS_SSL_HS_CERTIFICATE ||
2404 ssl->in_hslen < mbedtls_ssl_hs_hdr_len(ssl) + 3 + 3) {
2405 MBEDTLS_SSL_DEBUG_MSG(1, ("bad certificate message"));
2406 mbedtls_ssl_send_alert_message(ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
2407 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR);
2408 return MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE;
Paul Bakker5121ce52009-01-03 21:22:43 +00002409 }
2410
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002411 i = mbedtls_ssl_hs_hdr_len(ssl);
Manuel Pégourié-Gonnardf49a7da2014-09-10 13:30:43 +00002412
Paul Bakker5121ce52009-01-03 21:22:43 +00002413 /*
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002414 * Same message structure as in mbedtls_ssl_write_certificate()
Paul Bakker5121ce52009-01-03 21:22:43 +00002415 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002416 n = (ssl->in_msg[i+1] << 8) | ssl->in_msg[i+2];
Paul Bakker5121ce52009-01-03 21:22:43 +00002417
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002418 if (ssl->in_msg[i] != 0 ||
2419 ssl->in_hslen != n + 3 + mbedtls_ssl_hs_hdr_len(ssl)) {
2420 MBEDTLS_SSL_DEBUG_MSG(1, ("bad certificate message"));
2421 mbedtls_ssl_send_alert_message(ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
2422 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR);
2423 return MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE;
Paul Bakker5121ce52009-01-03 21:22:43 +00002424 }
2425
Hanno Beckerdef9bdc2019-01-30 14:46:46 +00002426 /* Make &ssl->in_msg[i] point to the beginning of the CRT chain. */
2427 i += 3;
2428
Hanno Beckerdef9bdc2019-01-30 14:46:46 +00002429 /* Iterate through and parse the CRTs in the provided chain. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002430 while (i < ssl->in_hslen) {
Hanno Beckerdef9bdc2019-01-30 14:46:46 +00002431 /* Check that there's room for the next CRT's length fields. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002432 if (i + 3 > ssl->in_hslen) {
2433 MBEDTLS_SSL_DEBUG_MSG(1, ("bad certificate message"));
2434 mbedtls_ssl_send_alert_message(ssl,
2435 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
2436 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR);
2437 return MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE;
Philippe Antoine747fd532018-05-30 09:13:21 +02002438 }
Hanno Beckerdef9bdc2019-01-30 14:46:46 +00002439 /* In theory, the CRT can be up to 2**24 Bytes, but we don't support
2440 * anything beyond 2**16 ~ 64K. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002441 if (ssl->in_msg[i] != 0) {
2442 MBEDTLS_SSL_DEBUG_MSG(1, ("bad certificate message"));
2443 mbedtls_ssl_send_alert_message(ssl,
2444 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
2445 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR);
2446 return MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE;
Paul Bakker5121ce52009-01-03 21:22:43 +00002447 }
2448
Hanno Beckerdef9bdc2019-01-30 14:46:46 +00002449 /* Read length of the next CRT in the chain. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002450 n = ((unsigned int) ssl->in_msg[i + 1] << 8)
Paul Bakker5121ce52009-01-03 21:22:43 +00002451 | (unsigned int) ssl->in_msg[i + 2];
2452 i += 3;
2453
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002454 if (n < 128 || i + n > ssl->in_hslen) {
2455 MBEDTLS_SSL_DEBUG_MSG(1, ("bad certificate message"));
2456 mbedtls_ssl_send_alert_message(ssl,
2457 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
2458 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR);
2459 return MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE;
Paul Bakker5121ce52009-01-03 21:22:43 +00002460 }
2461
Hanno Beckerdef9bdc2019-01-30 14:46:46 +00002462 /* Check if we're handling the first CRT in the chain. */
Hanno Beckerc7bd7802019-02-05 15:37:23 +00002463#if defined(MBEDTLS_SSL_RENEGOTIATION) && defined(MBEDTLS_SSL_CLI_C)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002464 if (crt_cnt++ == 0 &&
Hanno Beckerc7bd7802019-02-05 15:37:23 +00002465 ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT &&
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002466 ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS) {
Hanno Becker46f34d02019-02-08 14:00:04 +00002467 /* During client-side renegotiation, check that the server's
2468 * end-CRTs hasn't changed compared to the initial handshake,
2469 * mitigating the triple handshake attack. On success, reuse
2470 * the original end-CRT instead of parsing it again. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002471 MBEDTLS_SSL_DEBUG_MSG(3, ("Check that peer CRT hasn't changed during renegotiation"));
2472 if (ssl_check_peer_crt_unchanged(ssl,
2473 &ssl->in_msg[i],
2474 n) != 0) {
2475 MBEDTLS_SSL_DEBUG_MSG(1, ("new server cert during renegotiation"));
2476 mbedtls_ssl_send_alert_message(ssl,
2477 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
2478 MBEDTLS_SSL_ALERT_MSG_ACCESS_DENIED);
2479 return MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE;
Hanno Beckerdef9bdc2019-01-30 14:46:46 +00002480 }
Hanno Beckerc7bd7802019-02-05 15:37:23 +00002481
2482 /* Now we can safely free the original chain. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002483 ssl_clear_peer_cert(ssl->session);
Hanno Beckerc7bd7802019-02-05 15:37:23 +00002484 }
Hanno Beckerdef9bdc2019-01-30 14:46:46 +00002485#endif /* MBEDTLS_SSL_RENEGOTIATION && MBEDTLS_SSL_CLI_C */
2486
Hanno Beckerdef9bdc2019-01-30 14:46:46 +00002487 /* Parse the next certificate in the chain. */
Hanno Becker0056eab2019-02-08 14:39:16 +00002488#if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002489 ret = mbedtls_x509_crt_parse_der(chain, ssl->in_msg + i, n);
Hanno Becker0056eab2019-02-08 14:39:16 +00002490#else
Hanno Becker353a6f02019-02-26 11:51:34 +00002491 /* If we don't need to store the CRT chain permanently, parse
Hanno Becker0056eab2019-02-08 14:39:16 +00002492 * it in-place from the input buffer instead of making a copy. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002493 ret = mbedtls_x509_crt_parse_der_nocopy(chain, ssl->in_msg + i, n);
Hanno Becker0056eab2019-02-08 14:39:16 +00002494#endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002495 switch (ret) {
Hanno Beckerdef9bdc2019-01-30 14:46:46 +00002496 case 0: /*ok*/
2497 case MBEDTLS_ERR_X509_UNKNOWN_SIG_ALG + MBEDTLS_ERR_OID_NOT_FOUND:
2498 /* Ignore certificate with an unknown algorithm: maybe a
2499 prior certificate was already trusted. */
2500 break;
Gilles Peskine1cc8e342017-05-03 16:28:34 +02002501
Hanno Beckerdef9bdc2019-01-30 14:46:46 +00002502 case MBEDTLS_ERR_X509_ALLOC_FAILED:
2503 alert = MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR;
2504 goto crt_parse_der_failed;
Gilles Peskine1cc8e342017-05-03 16:28:34 +02002505
Hanno Beckerdef9bdc2019-01-30 14:46:46 +00002506 case MBEDTLS_ERR_X509_UNKNOWN_VERSION:
2507 alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT;
2508 goto crt_parse_der_failed;
Gilles Peskine1cc8e342017-05-03 16:28:34 +02002509
Hanno Beckerdef9bdc2019-01-30 14:46:46 +00002510 default:
2511 alert = MBEDTLS_SSL_ALERT_MSG_BAD_CERT;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002512crt_parse_der_failed:
2513 mbedtls_ssl_send_alert_message(ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL, alert);
2514 MBEDTLS_SSL_DEBUG_RET(1, " mbedtls_x509_crt_parse_der", ret);
2515 return ret;
Paul Bakker5121ce52009-01-03 21:22:43 +00002516 }
2517
2518 i += n;
2519 }
2520
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002521 MBEDTLS_SSL_DEBUG_CRT(3, "peer certificate", chain);
2522 return 0;
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02002523}
2524
Hanno Becker4a55f632019-02-05 12:49:06 +00002525#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnardd904d662022-06-17 10:24:00 +02002526MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002527static int ssl_srv_check_client_no_crt_notification(mbedtls_ssl_context *ssl)
Hanno Becker4a55f632019-02-05 12:49:06 +00002528{
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002529 if (ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT) {
2530 return -1;
2531 }
Hanno Becker4a55f632019-02-05 12:49:06 +00002532
2533#if defined(MBEDTLS_SSL_PROTO_SSL3)
2534 /*
2535 * Check if the client sent an empty certificate
2536 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002537 if (ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0) {
2538 if (ssl->in_msglen == 2 &&
Hanno Becker4a55f632019-02-05 12:49:06 +00002539 ssl->in_msgtype == MBEDTLS_SSL_MSG_ALERT &&
2540 ssl->in_msg[0] == MBEDTLS_SSL_ALERT_LEVEL_WARNING &&
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002541 ssl->in_msg[1] == MBEDTLS_SSL_ALERT_MSG_NO_CERT) {
2542 MBEDTLS_SSL_DEBUG_MSG(1, ("SSLv3 client has no certificate"));
2543 return 0;
Hanno Becker4a55f632019-02-05 12:49:06 +00002544 }
2545
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002546 return -1;
Hanno Becker4a55f632019-02-05 12:49:06 +00002547 }
2548#endif /* MBEDTLS_SSL_PROTO_SSL3 */
2549
2550#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1) || \
2551 defined(MBEDTLS_SSL_PROTO_TLS1_2)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002552 if (ssl->in_hslen == 3 + mbedtls_ssl_hs_hdr_len(ssl) &&
Hanno Becker4a55f632019-02-05 12:49:06 +00002553 ssl->in_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE &&
2554 ssl->in_msg[0] == MBEDTLS_SSL_HS_CERTIFICATE &&
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002555 memcmp(ssl->in_msg + mbedtls_ssl_hs_hdr_len(ssl), "\0\0\0", 3) == 0) {
2556 MBEDTLS_SSL_DEBUG_MSG(1, ("TLSv1 client has no certificate"));
2557 return 0;
Hanno Becker4a55f632019-02-05 12:49:06 +00002558 }
2559
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002560 return -1;
Hanno Becker4a55f632019-02-05 12:49:06 +00002561#endif /* MBEDTLS_SSL_PROTO_TLS1 || MBEDTLS_SSL_PROTO_TLS1_1 || \
2562 MBEDTLS_SSL_PROTO_TLS1_2 */
2563}
2564#endif /* MBEDTLS_SSL_SRV_C */
2565
Hanno Becker28f2fcd2019-02-07 10:11:07 +00002566/* Check if a certificate message is expected.
2567 * Return either
2568 * - SSL_CERTIFICATE_EXPECTED, or
2569 * - SSL_CERTIFICATE_SKIP
2570 * indicating whether a Certificate message is expected or not.
2571 */
2572#define SSL_CERTIFICATE_EXPECTED 0
2573#define SSL_CERTIFICATE_SKIP 1
Manuel Pégourié-Gonnardd904d662022-06-17 10:24:00 +02002574MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002575static int ssl_parse_certificate_coordinate(mbedtls_ssl_context *ssl,
2576 int authmode)
Hanno Becker28f2fcd2019-02-07 10:11:07 +00002577{
2578 const mbedtls_ssl_ciphersuite_t *ciphersuite_info =
Hanno Beckere694c3e2017-12-27 21:34:08 +00002579 ssl->handshake->ciphersuite_info;
Hanno Becker28f2fcd2019-02-07 10:11:07 +00002580
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002581 if (!mbedtls_ssl_ciphersuite_uses_srv_cert(ciphersuite_info)) {
2582 return SSL_CERTIFICATE_SKIP;
2583 }
Hanno Becker28f2fcd2019-02-07 10:11:07 +00002584
2585#if defined(MBEDTLS_SSL_SRV_C)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002586 if (ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER) {
2587 if (ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_RSA_PSK) {
2588 return SSL_CERTIFICATE_SKIP;
2589 }
Hanno Becker28f2fcd2019-02-07 10:11:07 +00002590
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002591 if (authmode == MBEDTLS_SSL_VERIFY_NONE) {
Hanno Becker28f2fcd2019-02-07 10:11:07 +00002592 ssl->session_negotiate->verify_result =
2593 MBEDTLS_X509_BADCERT_SKIP_VERIFY;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002594 return SSL_CERTIFICATE_SKIP;
Hanno Becker28f2fcd2019-02-07 10:11:07 +00002595 }
2596 }
Hanno Becker84d9d272019-03-01 08:10:46 +00002597#else
2598 ((void) authmode);
Hanno Becker28f2fcd2019-02-07 10:11:07 +00002599#endif /* MBEDTLS_SSL_SRV_C */
2600
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002601 return SSL_CERTIFICATE_EXPECTED;
Hanno Becker28f2fcd2019-02-07 10:11:07 +00002602}
2603
Gilles Peskine3a2f75d2025-02-12 23:28:48 +01002604static int get_hostname_for_verification(mbedtls_ssl_context *ssl,
2605 const char **hostname)
2606{
2607 if (!mbedtls_ssl_has_set_hostname_been_called(ssl)) {
2608 MBEDTLS_SSL_DEBUG_MSG(1, ("Certificate verification without having set hostname"));
2609 }
2610
2611 *hostname = mbedtls_ssl_get_hostname_pointer(ssl);
2612 if (*hostname == NULL) {
2613 MBEDTLS_SSL_DEBUG_MSG(2, ("Certificate verification without CN verification"));
2614 }
2615
2616 return 0;
2617}
2618
Manuel Pégourié-Gonnardd904d662022-06-17 10:24:00 +02002619MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002620static int ssl_parse_certificate_verify(mbedtls_ssl_context *ssl,
2621 int authmode,
2622 mbedtls_x509_crt *chain,
2623 void *rs_ctx)
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02002624{
Hanno Becker28f2fcd2019-02-07 10:11:07 +00002625 const mbedtls_ssl_ciphersuite_t *ciphersuite_info =
Hanno Beckere694c3e2017-12-27 21:34:08 +00002626 ssl->handshake->ciphersuite_info;
Hanno Beckerafd0b0a2019-03-27 16:55:01 +00002627 int have_ca_chain = 0;
Hanno Becker68636192019-02-05 14:36:34 +00002628
Hanno Becker8927c832019-04-03 12:52:50 +01002629 int (*f_vrfy)(void *, mbedtls_x509_crt *, int, uint32_t *);
2630 void *p_vrfy;
2631
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002632 if (authmode == MBEDTLS_SSL_VERIFY_NONE) {
2633 return 0;
2634 }
Hanno Becker68636192019-02-05 14:36:34 +00002635
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002636 if (ssl->f_vrfy != NULL) {
2637 MBEDTLS_SSL_DEBUG_MSG(3, ("Use context-specific verification callback"));
Hanno Becker8927c832019-04-03 12:52:50 +01002638 f_vrfy = ssl->f_vrfy;
2639 p_vrfy = ssl->p_vrfy;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002640 } else {
2641 MBEDTLS_SSL_DEBUG_MSG(3, ("Use configuration-specific verification callback"));
Hanno Becker8927c832019-04-03 12:52:50 +01002642 f_vrfy = ssl->conf->f_vrfy;
2643 p_vrfy = ssl->conf->p_vrfy;
2644 }
2645
Gilles Peskine3a2f75d2025-02-12 23:28:48 +01002646 const char *hostname = "";
2647 int ret = get_hostname_for_verification(ssl, &hostname);
2648 if (ret != 0) {
2649 MBEDTLS_SSL_DEBUG_RET(1, "get_hostname_for_verification", ret);
2650 return ret;
2651 }
2652
Hanno Becker68636192019-02-05 14:36:34 +00002653 /*
2654 * Main check: verify certificate
2655 */
Hanno Beckerafd0b0a2019-03-27 16:55:01 +00002656#if defined(MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002657 if (ssl->conf->f_ca_cb != NULL) {
Hanno Beckerafd0b0a2019-03-27 16:55:01 +00002658 ((void) rs_ctx);
2659 have_ca_chain = 1;
2660
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002661 MBEDTLS_SSL_DEBUG_MSG(3, ("use CA callback for X.509 CRT verification"));
Jarno Lamsa9822c0d2019-04-01 16:59:48 +03002662 ret = mbedtls_x509_crt_verify_with_ca_cb(
Hanno Beckerafd0b0a2019-03-27 16:55:01 +00002663 chain,
2664 ssl->conf->f_ca_cb,
2665 ssl->conf->p_ca_cb,
2666 ssl->conf->cert_profile,
Gilles Peskine3a2f75d2025-02-12 23:28:48 +01002667 hostname,
Hanno Beckerafd0b0a2019-03-27 16:55:01 +00002668 &ssl->session_negotiate->verify_result,
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002669 f_vrfy, p_vrfy);
2670 } else
Hanno Beckerafd0b0a2019-03-27 16:55:01 +00002671#endif /* MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK */
2672 {
2673 mbedtls_x509_crt *ca_chain;
2674 mbedtls_x509_crl *ca_crl;
2675
2676#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002677 if (ssl->handshake->sni_ca_chain != NULL) {
Hanno Beckerafd0b0a2019-03-27 16:55:01 +00002678 ca_chain = ssl->handshake->sni_ca_chain;
2679 ca_crl = ssl->handshake->sni_ca_crl;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002680 } else
Hanno Beckerafd0b0a2019-03-27 16:55:01 +00002681#endif
2682 {
2683 ca_chain = ssl->conf->ca_chain;
2684 ca_crl = ssl->conf->ca_crl;
2685 }
2686
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002687 if (ca_chain != NULL) {
Hanno Beckerafd0b0a2019-03-27 16:55:01 +00002688 have_ca_chain = 1;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002689 }
Hanno Beckerafd0b0a2019-03-27 16:55:01 +00002690
2691 ret = mbedtls_x509_crt_verify_restartable(
2692 chain,
2693 ca_chain, ca_crl,
2694 ssl->conf->cert_profile,
Gilles Peskine3a2f75d2025-02-12 23:28:48 +01002695 hostname,
Hanno Beckerafd0b0a2019-03-27 16:55:01 +00002696 &ssl->session_negotiate->verify_result,
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002697 f_vrfy, p_vrfy, rs_ctx);
Hanno Beckerafd0b0a2019-03-27 16:55:01 +00002698 }
Hanno Becker68636192019-02-05 14:36:34 +00002699
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002700 if (ret != 0) {
2701 MBEDTLS_SSL_DEBUG_RET(1, "x509_verify_cert", ret);
Hanno Becker68636192019-02-05 14:36:34 +00002702 }
2703
Gilles Peskineeccd8882020-03-10 12:19:08 +01002704#if defined(MBEDTLS_SSL_ECP_RESTARTABLE_ENABLED)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002705 if (ret == MBEDTLS_ERR_ECP_IN_PROGRESS) {
2706 return MBEDTLS_ERR_SSL_CRYPTO_IN_PROGRESS;
2707 }
Hanno Becker68636192019-02-05 14:36:34 +00002708#endif
2709
2710 /*
2711 * Secondary checks: always done, but change 'ret' only if it was 0
2712 */
2713
2714#if defined(MBEDTLS_ECP_C)
2715 {
2716 const mbedtls_pk_context *pk = &chain->pk;
2717
Manuel Pégourié-Gonnard06e1fcd2022-06-16 10:48:06 +02002718 /* If certificate uses an EC key, make sure the curve is OK.
2719 * This is a public key, so it can't be opaque, so can_do() is a good
2720 * enough check to ensure pk_ec() is safe to use here. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002721 if (mbedtls_pk_can_do(pk, MBEDTLS_PK_ECKEY) &&
2722 mbedtls_ssl_check_curve(ssl, mbedtls_pk_ec(*pk)->grp.id) != 0) {
Hanno Becker68636192019-02-05 14:36:34 +00002723 ssl->session_negotiate->verify_result |= MBEDTLS_X509_BADCERT_BAD_KEY;
2724
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002725 MBEDTLS_SSL_DEBUG_MSG(1, ("bad certificate (EC key curve)"));
2726 if (ret == 0) {
Hanno Becker68636192019-02-05 14:36:34 +00002727 ret = MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002728 }
Hanno Becker68636192019-02-05 14:36:34 +00002729 }
2730 }
2731#endif /* MBEDTLS_ECP_C */
2732
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002733 if (mbedtls_ssl_check_cert_usage(chain,
2734 ciphersuite_info,
2735 !ssl->conf->endpoint,
2736 &ssl->session_negotiate->verify_result) != 0) {
2737 MBEDTLS_SSL_DEBUG_MSG(1, ("bad certificate (usage extensions)"));
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 /* mbedtls_x509_crt_verify_with_profile is supposed to report a
2744 * verification failure through MBEDTLS_ERR_X509_CERT_VERIFY_FAILED,
2745 * with details encoded in the verification flags. All other kinds
2746 * of error codes, including those from the user provided f_vrfy
2747 * functions, are treated as fatal and lead to a failure of
2748 * ssl_parse_certificate even if verification was optional. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002749 if (authmode == MBEDTLS_SSL_VERIFY_OPTIONAL &&
2750 (ret == MBEDTLS_ERR_X509_CERT_VERIFY_FAILED ||
2751 ret == MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE)) {
Hanno Becker68636192019-02-05 14:36:34 +00002752 ret = 0;
2753 }
2754
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002755 if (have_ca_chain == 0 && authmode == MBEDTLS_SSL_VERIFY_REQUIRED) {
2756 MBEDTLS_SSL_DEBUG_MSG(1, ("got no CA chain"));
Hanno Becker68636192019-02-05 14:36:34 +00002757 ret = MBEDTLS_ERR_SSL_CA_CHAIN_REQUIRED;
2758 }
2759
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002760 if (ret != 0) {
Hanno Becker68636192019-02-05 14:36:34 +00002761 uint8_t alert;
2762
2763 /* The certificate may have been rejected for several reasons.
2764 Pick one and send the corresponding alert. Which alert to send
2765 may be a subject of debate in some cases. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002766 if (ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_OTHER) {
Hanno Becker68636192019-02-05 14:36:34 +00002767 alert = MBEDTLS_SSL_ALERT_MSG_ACCESS_DENIED;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002768 } else if (ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_CN_MISMATCH) {
Hanno Becker68636192019-02-05 14:36:34 +00002769 alert = MBEDTLS_SSL_ALERT_MSG_BAD_CERT;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002770 } else if (ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_KEY_USAGE) {
Hanno Becker68636192019-02-05 14:36:34 +00002771 alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002772 } else if (ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_EXT_KEY_USAGE) {
Hanno Becker68636192019-02-05 14:36:34 +00002773 alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002774 } else if (ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_NS_CERT_TYPE) {
Hanno Becker68636192019-02-05 14:36:34 +00002775 alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002776 } else if (ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_BAD_PK) {
Hanno Becker68636192019-02-05 14:36:34 +00002777 alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002778 } else if (ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_BAD_KEY) {
Hanno Becker68636192019-02-05 14:36:34 +00002779 alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002780 } else if (ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_EXPIRED) {
Hanno Becker68636192019-02-05 14:36:34 +00002781 alert = MBEDTLS_SSL_ALERT_MSG_CERT_EXPIRED;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002782 } else if (ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_REVOKED) {
Hanno Becker68636192019-02-05 14:36:34 +00002783 alert = MBEDTLS_SSL_ALERT_MSG_CERT_REVOKED;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002784 } else if (ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_NOT_TRUSTED) {
Hanno Becker68636192019-02-05 14:36:34 +00002785 alert = MBEDTLS_SSL_ALERT_MSG_UNKNOWN_CA;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002786 } else {
Hanno Becker68636192019-02-05 14:36:34 +00002787 alert = MBEDTLS_SSL_ALERT_MSG_CERT_UNKNOWN;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002788 }
2789 mbedtls_ssl_send_alert_message(ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
2790 alert);
Hanno Becker68636192019-02-05 14:36:34 +00002791 }
2792
2793#if defined(MBEDTLS_DEBUG_C)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002794 if (ssl->session_negotiate->verify_result != 0) {
2795 MBEDTLS_SSL_DEBUG_MSG(3, ("! Certificate verification flags %08x",
2796 (unsigned int) ssl->session_negotiate->verify_result));
2797 } else {
2798 MBEDTLS_SSL_DEBUG_MSG(3, ("Certificate verification flags clear"));
Hanno Becker68636192019-02-05 14:36:34 +00002799 }
2800#endif /* MBEDTLS_DEBUG_C */
2801
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002802 return ret;
Hanno Becker68636192019-02-05 14:36:34 +00002803}
2804
Hanno Becker6b8fbab2019-02-08 14:59:05 +00002805#if !defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
Manuel Pégourié-Gonnardd904d662022-06-17 10:24:00 +02002806MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002807static int ssl_remember_peer_crt_digest(mbedtls_ssl_context *ssl,
2808 unsigned char *start, size_t len)
Hanno Becker6b8fbab2019-02-08 14:59:05 +00002809{
Janos Follath865b3eb2019-12-16 11:46:15 +00002810 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Hanno Becker6b8fbab2019-02-08 14:59:05 +00002811 /* Remember digest of the peer's end-CRT. */
2812 ssl->session_negotiate->peer_cert_digest =
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002813 mbedtls_calloc(1, MBEDTLS_SSL_PEER_CERT_DIGEST_DFL_LEN);
2814 if (ssl->session_negotiate->peer_cert_digest == NULL) {
2815 MBEDTLS_SSL_DEBUG_MSG(1, ("alloc(%d bytes) failed",
2816 MBEDTLS_SSL_PEER_CERT_DIGEST_DFL_LEN));
2817 mbedtls_ssl_send_alert_message(ssl,
2818 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
2819 MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR);
Hanno Becker6b8fbab2019-02-08 14:59:05 +00002820
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002821 return MBEDTLS_ERR_SSL_ALLOC_FAILED;
Hanno Becker6b8fbab2019-02-08 14:59:05 +00002822 }
2823
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002824 ret = mbedtls_md(mbedtls_md_info_from_type(
2825 MBEDTLS_SSL_PEER_CERT_DIGEST_DFL_TYPE),
2826 start, len,
2827 ssl->session_negotiate->peer_cert_digest);
Hanno Becker6b8fbab2019-02-08 14:59:05 +00002828
2829 ssl->session_negotiate->peer_cert_digest_type =
2830 MBEDTLS_SSL_PEER_CERT_DIGEST_DFL_TYPE;
2831 ssl->session_negotiate->peer_cert_digest_len =
2832 MBEDTLS_SSL_PEER_CERT_DIGEST_DFL_LEN;
2833
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002834 return ret;
Hanno Becker6b8fbab2019-02-08 14:59:05 +00002835}
2836
Manuel Pégourié-Gonnardd904d662022-06-17 10:24:00 +02002837MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002838static int ssl_remember_peer_pubkey(mbedtls_ssl_context *ssl,
2839 unsigned char *start, size_t len)
Hanno Becker6b8fbab2019-02-08 14:59:05 +00002840{
2841 unsigned char *end = start + len;
Janos Follath865b3eb2019-12-16 11:46:15 +00002842 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Hanno Becker6b8fbab2019-02-08 14:59:05 +00002843
2844 /* Make a copy of the peer's raw public key. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002845 mbedtls_pk_init(&ssl->handshake->peer_pubkey);
2846 ret = mbedtls_pk_parse_subpubkey(&start, end,
2847 &ssl->handshake->peer_pubkey);
2848 if (ret != 0) {
Hanno Becker6b8fbab2019-02-08 14:59:05 +00002849 /* We should have parsed the public key before. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002850 return MBEDTLS_ERR_SSL_INTERNAL_ERROR;
Hanno Becker6b8fbab2019-02-08 14:59:05 +00002851 }
2852
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002853 return 0;
Hanno Becker6b8fbab2019-02-08 14:59:05 +00002854}
2855#endif /* !MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
2856
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002857int mbedtls_ssl_parse_certificate(mbedtls_ssl_context *ssl)
Hanno Becker68636192019-02-05 14:36:34 +00002858{
2859 int ret = 0;
Hanno Becker28f2fcd2019-02-07 10:11:07 +00002860 int crt_expected;
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02002861#if defined(MBEDTLS_SSL_SRV_C) && defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
2862 const int authmode = ssl->handshake->sni_authmode != MBEDTLS_SSL_VERIFY_UNSET
2863 ? ssl->handshake->sni_authmode
2864 : ssl->conf->authmode;
2865#else
Johan Pascal4f099262020-09-22 10:59:26 +02002866 const int authmode = ssl->conf->authmode;
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02002867#endif
Manuel Pégourié-Gonnard3bf49c42017-08-15 13:47:06 +02002868 void *rs_ctx = NULL;
Hanno Becker3dad3112019-02-05 17:19:52 +00002869 mbedtls_x509_crt *chain = NULL;
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02002870
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002871 MBEDTLS_SSL_DEBUG_MSG(2, ("=> parse certificate"));
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02002872
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002873 crt_expected = ssl_parse_certificate_coordinate(ssl, authmode);
2874 if (crt_expected == SSL_CERTIFICATE_SKIP) {
2875 MBEDTLS_SSL_DEBUG_MSG(2, ("<= skip parse certificate"));
Hanno Becker6bdfab22019-02-05 13:11:17 +00002876 goto exit;
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02002877 }
2878
Gilles Peskineeccd8882020-03-10 12:19:08 +01002879#if defined(MBEDTLS_SSL_ECP_RESTARTABLE_ENABLED)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002880 if (ssl->handshake->ecrs_enabled &&
2881 ssl->handshake->ecrs_state == ssl_ecrs_crt_verify) {
Hanno Becker3dad3112019-02-05 17:19:52 +00002882 chain = ssl->handshake->ecrs_peer_cert;
2883 ssl->handshake->ecrs_peer_cert = NULL;
Manuel Pégourié-Gonnard3bf49c42017-08-15 13:47:06 +02002884 goto crt_verify;
2885 }
2886#endif
2887
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002888 if ((ret = mbedtls_ssl_read_record(ssl, 1)) != 0) {
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02002889 /* mbedtls_ssl_read_record may have sent an alert already. We
2890 let it decide whether to alert. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002891 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ssl_read_record", ret);
Hanno Becker3dad3112019-02-05 17:19:52 +00002892 goto exit;
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02002893 }
2894
Hanno Becker4a55f632019-02-05 12:49:06 +00002895#if defined(MBEDTLS_SSL_SRV_C)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002896 if (ssl_srv_check_client_no_crt_notification(ssl) == 0) {
Hanno Becker4a55f632019-02-05 12:49:06 +00002897 ssl->session_negotiate->verify_result = MBEDTLS_X509_BADCERT_MISSING;
Hanno Becker4a55f632019-02-05 12:49:06 +00002898
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002899 if (authmode != MBEDTLS_SSL_VERIFY_OPTIONAL) {
Hanno Becker6bdfab22019-02-05 13:11:17 +00002900 ret = MBEDTLS_ERR_SSL_NO_CLIENT_CERTIFICATE;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002901 }
Hanno Becker4a55f632019-02-05 12:49:06 +00002902
Hanno Becker6bdfab22019-02-05 13:11:17 +00002903 goto exit;
Hanno Becker4a55f632019-02-05 12:49:06 +00002904 }
2905#endif /* MBEDTLS_SSL_SRV_C */
2906
Hanno Beckerc7bd7802019-02-05 15:37:23 +00002907 /* Clear existing peer CRT structure in case we tried to
2908 * reuse a session but it failed, and allocate a new one. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002909 ssl_clear_peer_cert(ssl->session_negotiate);
Hanno Becker3dad3112019-02-05 17:19:52 +00002910
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002911 chain = mbedtls_calloc(1, sizeof(mbedtls_x509_crt));
2912 if (chain == NULL) {
2913 MBEDTLS_SSL_DEBUG_MSG(1, ("alloc(%" MBEDTLS_PRINTF_SIZET " bytes) failed",
2914 sizeof(mbedtls_x509_crt)));
2915 mbedtls_ssl_send_alert_message(ssl,
2916 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
2917 MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR);
Hanno Becker7a955a02019-02-05 13:08:01 +00002918
Hanno Becker3dad3112019-02-05 17:19:52 +00002919 ret = MBEDTLS_ERR_SSL_ALLOC_FAILED;
2920 goto exit;
2921 }
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002922 mbedtls_x509_crt_init(chain);
Hanno Becker3dad3112019-02-05 17:19:52 +00002923
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002924 ret = ssl_parse_certificate_chain(ssl, chain);
2925 if (ret != 0) {
Hanno Becker3dad3112019-02-05 17:19:52 +00002926 goto exit;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002927 }
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02002928
Gilles Peskineeccd8882020-03-10 12:19:08 +01002929#if defined(MBEDTLS_SSL_ECP_RESTARTABLE_ENABLED)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002930 if (ssl->handshake->ecrs_enabled) {
Manuel Pégourié-Gonnard0b23f162017-08-24 12:08:33 +02002931 ssl->handshake->ecrs_state = ssl_ecrs_crt_verify;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002932 }
Manuel Pégourié-Gonnard3bf49c42017-08-15 13:47:06 +02002933
2934crt_verify:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002935 if (ssl->handshake->ecrs_enabled) {
Manuel Pégourié-Gonnard3bf49c42017-08-15 13:47:06 +02002936 rs_ctx = &ssl->handshake->ecrs_ctx;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002937 }
Manuel Pégourié-Gonnard3bf49c42017-08-15 13:47:06 +02002938#endif
2939
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002940 ret = ssl_parse_certificate_verify(ssl, authmode,
2941 chain, rs_ctx);
2942 if (ret != 0) {
Hanno Becker3dad3112019-02-05 17:19:52 +00002943 goto exit;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002944 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002945
Hanno Becker6bbd94c2019-02-05 17:02:28 +00002946#if !defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
Hanno Becker6bbd94c2019-02-05 17:02:28 +00002947 {
Hanno Becker6b8fbab2019-02-08 14:59:05 +00002948 unsigned char *crt_start, *pk_start;
2949 size_t crt_len, pk_len;
Hanno Becker3dad3112019-02-05 17:19:52 +00002950
Hanno Becker6b8fbab2019-02-08 14:59:05 +00002951 /* We parse the CRT chain without copying, so
2952 * these pointers point into the input buffer,
2953 * and are hence still valid after freeing the
2954 * CRT chain. */
Hanno Becker6bbd94c2019-02-05 17:02:28 +00002955
Hanno Becker6b8fbab2019-02-08 14:59:05 +00002956 crt_start = chain->raw.p;
2957 crt_len = chain->raw.len;
Hanno Becker6bbd94c2019-02-05 17:02:28 +00002958
Hanno Becker6b8fbab2019-02-08 14:59:05 +00002959 pk_start = chain->pk_raw.p;
2960 pk_len = chain->pk_raw.len;
2961
2962 /* Free the CRT structures before computing
2963 * digest and copying the peer's public key. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002964 mbedtls_x509_crt_free(chain);
2965 mbedtls_free(chain);
Hanno Becker6b8fbab2019-02-08 14:59:05 +00002966 chain = NULL;
2967
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002968 ret = ssl_remember_peer_crt_digest(ssl, crt_start, crt_len);
2969 if (ret != 0) {
Hanno Beckera2747532019-02-06 16:19:04 +00002970 goto exit;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002971 }
Hanno Becker6b8fbab2019-02-08 14:59:05 +00002972
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002973 ret = ssl_remember_peer_pubkey(ssl, pk_start, pk_len);
2974 if (ret != 0) {
Hanno Becker6b8fbab2019-02-08 14:59:05 +00002975 goto exit;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002976 }
Hanno Beckera2747532019-02-06 16:19:04 +00002977 }
Hanno Becker6b8fbab2019-02-08 14:59:05 +00002978#else /* !MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
2979 /* Pass ownership to session structure. */
Hanno Becker3dad3112019-02-05 17:19:52 +00002980 ssl->session_negotiate->peer_cert = chain;
2981 chain = NULL;
Hanno Becker6b8fbab2019-02-08 14:59:05 +00002982#endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
Hanno Becker3dad3112019-02-05 17:19:52 +00002983
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002984 MBEDTLS_SSL_DEBUG_MSG(2, ("<= parse certificate"));
Paul Bakker5121ce52009-01-03 21:22:43 +00002985
Hanno Becker6bdfab22019-02-05 13:11:17 +00002986exit:
2987
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002988 if (ret == 0) {
Hanno Becker3dad3112019-02-05 17:19:52 +00002989 ssl->state++;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002990 }
Hanno Becker3dad3112019-02-05 17:19:52 +00002991
Gilles Peskineeccd8882020-03-10 12:19:08 +01002992#if defined(MBEDTLS_SSL_ECP_RESTARTABLE_ENABLED)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002993 if (ret == MBEDTLS_ERR_SSL_CRYPTO_IN_PROGRESS) {
Hanno Becker3dad3112019-02-05 17:19:52 +00002994 ssl->handshake->ecrs_peer_cert = chain;
2995 chain = NULL;
2996 }
2997#endif
2998
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002999 if (chain != NULL) {
3000 mbedtls_x509_crt_free(chain);
3001 mbedtls_free(chain);
Hanno Becker3dad3112019-02-05 17:19:52 +00003002 }
3003
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003004 return ret;
Paul Bakker5121ce52009-01-03 21:22:43 +00003005}
Gilles Peskineeccd8882020-03-10 12:19:08 +01003006#endif /* MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED */
Paul Bakker5121ce52009-01-03 21:22:43 +00003007
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003008void mbedtls_ssl_optimize_checksum(mbedtls_ssl_context *ssl,
3009 const mbedtls_ssl_ciphersuite_t *ciphersuite_info)
Paul Bakker380da532012-04-18 16:10:25 +00003010{
Paul Bakkerfb08fd22013-08-27 15:06:26 +02003011 ((void) ciphersuite_info);
Paul Bakker769075d2012-11-24 11:26:46 +01003012
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003013#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1) || \
3014 defined(MBEDTLS_SSL_PROTO_TLS1_1)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003015 if (ssl->minor_ver < MBEDTLS_SSL_MINOR_VERSION_3) {
Paul Bakker48916f92012-09-16 19:57:18 +00003016 ssl->handshake->update_checksum = ssl_update_checksum_md5sha1;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003017 } else
Paul Bakkerd2f068e2013-08-27 21:19:20 +02003018#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003019#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
Gilles Peskined2d59372021-05-12 22:43:27 +02003020#if defined(MBEDTLS_SHA512_C) && !defined(MBEDTLS_SHA512_NO_SHA384)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003021 if (ciphersuite_info->mac == MBEDTLS_MD_SHA384) {
Paul Bakkerd2f068e2013-08-27 21:19:20 +02003022 ssl->handshake->update_checksum = ssl_update_checksum_sha384;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003023 } else
Paul Bakkerd2f068e2013-08-27 21:19:20 +02003024#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003025#if defined(MBEDTLS_SHA256_C)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003026 if (ciphersuite_info->mac != MBEDTLS_MD_SHA384) {
Paul Bakker48916f92012-09-16 19:57:18 +00003027 ssl->handshake->update_checksum = ssl_update_checksum_sha256;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003028 } else
Paul Bakkerd2f068e2013-08-27 21:19:20 +02003029#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003030#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Manuel Pégourié-Gonnard61edffe2014-04-11 17:07:31 +02003031 {
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003032 MBEDTLS_SSL_DEBUG_MSG(1, ("should never happen"));
Paul Bakkerd2f068e2013-08-27 21:19:20 +02003033 return;
Manuel Pégourié-Gonnard61edffe2014-04-11 17:07:31 +02003034 }
Paul Bakker380da532012-04-18 16:10:25 +00003035}
Paul Bakkerf7abd422013-04-16 13:15:56 +02003036
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003037void mbedtls_ssl_reset_checksum(mbedtls_ssl_context *ssl)
Manuel Pégourié-Gonnard67427c02014-07-11 13:45:34 +02003038{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003039#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1) || \
3040 defined(MBEDTLS_SSL_PROTO_TLS1_1)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003041 mbedtls_md5_starts_ret(&ssl->handshake->fin_md5);
3042 mbedtls_sha1_starts_ret(&ssl->handshake->fin_sha1);
Manuel Pégourié-Gonnard67427c02014-07-11 13:45:34 +02003043#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003044#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
3045#if defined(MBEDTLS_SHA256_C)
Andrzej Kurekeb342242019-01-29 09:14:33 -05003046#if defined(MBEDTLS_USE_PSA_CRYPTO)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003047 psa_hash_abort(&ssl->handshake->fin_sha256_psa);
3048 psa_hash_setup(&ssl->handshake->fin_sha256_psa, PSA_ALG_SHA_256);
Andrzej Kurekeb342242019-01-29 09:14:33 -05003049#else
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003050 mbedtls_sha256_starts_ret(&ssl->handshake->fin_sha256, 0);
Manuel Pégourié-Gonnard67427c02014-07-11 13:45:34 +02003051#endif
Andrzej Kurekeb342242019-01-29 09:14:33 -05003052#endif
Gilles Peskined2d59372021-05-12 22:43:27 +02003053#if defined(MBEDTLS_SHA512_C) && !defined(MBEDTLS_SHA512_NO_SHA384)
Andrzej Kurekeb342242019-01-29 09:14:33 -05003054#if defined(MBEDTLS_USE_PSA_CRYPTO)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003055 psa_hash_abort(&ssl->handshake->fin_sha384_psa);
3056 psa_hash_setup(&ssl->handshake->fin_sha384_psa, PSA_ALG_SHA_384);
Andrzej Kurekeb342242019-01-29 09:14:33 -05003057#else
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003058 mbedtls_sha512_starts_ret(&ssl->handshake->fin_sha512, 1);
Manuel Pégourié-Gonnard67427c02014-07-11 13:45:34 +02003059#endif
Andrzej Kurekeb342242019-01-29 09:14:33 -05003060#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003061#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Manuel Pégourié-Gonnard67427c02014-07-11 13:45:34 +02003062}
3063
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003064static void ssl_update_checksum_start(mbedtls_ssl_context *ssl,
3065 const unsigned char *buf, size_t len)
Paul Bakker380da532012-04-18 16:10:25 +00003066{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003067#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1) || \
3068 defined(MBEDTLS_SSL_PROTO_TLS1_1)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003069 mbedtls_md5_update_ret(&ssl->handshake->fin_md5, buf, len);
3070 mbedtls_sha1_update_ret(&ssl->handshake->fin_sha1, buf, len);
Paul Bakkerd2f068e2013-08-27 21:19:20 +02003071#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003072#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
3073#if defined(MBEDTLS_SHA256_C)
Andrzej Kurekeb342242019-01-29 09:14:33 -05003074#if defined(MBEDTLS_USE_PSA_CRYPTO)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003075 psa_hash_update(&ssl->handshake->fin_sha256_psa, buf, len);
Andrzej Kurekeb342242019-01-29 09:14:33 -05003076#else
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003077 mbedtls_sha256_update_ret(&ssl->handshake->fin_sha256, buf, len);
Paul Bakkerd2f068e2013-08-27 21:19:20 +02003078#endif
Andrzej Kurekeb342242019-01-29 09:14:33 -05003079#endif
Gilles Peskined2d59372021-05-12 22:43:27 +02003080#if defined(MBEDTLS_SHA512_C) && !defined(MBEDTLS_SHA512_NO_SHA384)
Andrzej Kurekeb342242019-01-29 09:14:33 -05003081#if defined(MBEDTLS_USE_PSA_CRYPTO)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003082 psa_hash_update(&ssl->handshake->fin_sha384_psa, buf, len);
Andrzej Kurekeb342242019-01-29 09:14:33 -05003083#else
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003084 mbedtls_sha512_update_ret(&ssl->handshake->fin_sha512, buf, len);
Paul Bakker769075d2012-11-24 11:26:46 +01003085#endif
Andrzej Kurekeb342242019-01-29 09:14:33 -05003086#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003087#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakker380da532012-04-18 16:10:25 +00003088}
3089
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003090#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1) || \
3091 defined(MBEDTLS_SSL_PROTO_TLS1_1)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003092static void ssl_update_checksum_md5sha1(mbedtls_ssl_context *ssl,
3093 const unsigned char *buf, size_t len)
Paul Bakker380da532012-04-18 16:10:25 +00003094{
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003095 mbedtls_md5_update_ret(&ssl->handshake->fin_md5, buf, len);
3096 mbedtls_sha1_update_ret(&ssl->handshake->fin_sha1, buf, len);
Paul Bakker380da532012-04-18 16:10:25 +00003097}
Paul Bakkerd2f068e2013-08-27 21:19:20 +02003098#endif
Paul Bakker380da532012-04-18 16:10:25 +00003099
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003100#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
3101#if defined(MBEDTLS_SHA256_C)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003102static void ssl_update_checksum_sha256(mbedtls_ssl_context *ssl,
3103 const unsigned char *buf, size_t len)
Paul Bakker380da532012-04-18 16:10:25 +00003104{
Andrzej Kurekeb342242019-01-29 09:14:33 -05003105#if defined(MBEDTLS_USE_PSA_CRYPTO)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003106 psa_hash_update(&ssl->handshake->fin_sha256_psa, buf, len);
Andrzej Kurekeb342242019-01-29 09:14:33 -05003107#else
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003108 mbedtls_sha256_update_ret(&ssl->handshake->fin_sha256, buf, len);
Andrzej Kurekeb342242019-01-29 09:14:33 -05003109#endif
Paul Bakker380da532012-04-18 16:10:25 +00003110}
Paul Bakkerd2f068e2013-08-27 21:19:20 +02003111#endif
Paul Bakker380da532012-04-18 16:10:25 +00003112
Gilles Peskined2d59372021-05-12 22:43:27 +02003113#if defined(MBEDTLS_SHA512_C) && !defined(MBEDTLS_SHA512_NO_SHA384)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003114static void ssl_update_checksum_sha384(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_sha384_psa, buf, len);
Andrzej Kurekeb342242019-01-29 09:14:33 -05003119#else
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003120 mbedtls_sha512_update_ret(&ssl->handshake->fin_sha512, buf, len);
Andrzej Kurekeb342242019-01-29 09:14:33 -05003121#endif
Paul Bakker380da532012-04-18 16:10:25 +00003122}
Paul Bakker769075d2012-11-24 11:26:46 +01003123#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003124#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakker380da532012-04-18 16:10:25 +00003125
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003126#if defined(MBEDTLS_SSL_PROTO_SSL3)
Paul Bakker1ef83d62012-04-11 12:09:53 +00003127static void ssl_calc_finished_ssl(
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003128 mbedtls_ssl_context *ssl, unsigned char *buf, int from)
Paul Bakker5121ce52009-01-03 21:22:43 +00003129{
Paul Bakker3c2122f2013-06-24 19:03:14 +02003130 const char *sender;
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02003131 mbedtls_md5_context md5;
3132 mbedtls_sha1_context sha1;
Paul Bakker1ef83d62012-04-11 12:09:53 +00003133
Paul Bakker5121ce52009-01-03 21:22:43 +00003134 unsigned char padbuf[48];
3135 unsigned char md5sum[16];
3136 unsigned char sha1sum[20];
3137
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003138 mbedtls_ssl_session *session = ssl->session_negotiate;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003139 if (!session) {
Paul Bakker48916f92012-09-16 19:57:18 +00003140 session = ssl->session;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003141 }
Paul Bakker48916f92012-09-16 19:57:18 +00003142
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003143 MBEDTLS_SSL_DEBUG_MSG(2, ("=> calc finished ssl"));
Paul Bakker1ef83d62012-04-11 12:09:53 +00003144
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003145 mbedtls_md5_init(&md5);
3146 mbedtls_sha1_init(&sha1);
Manuel Pégourié-Gonnard001f2b62015-07-06 16:21:13 +02003147
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003148 mbedtls_md5_clone(&md5, &ssl->handshake->fin_md5);
3149 mbedtls_sha1_clone(&sha1, &ssl->handshake->fin_sha1);
Paul Bakker5121ce52009-01-03 21:22:43 +00003150
3151 /*
3152 * SSLv3:
3153 * hash =
3154 * MD5( master + pad2 +
3155 * MD5( handshake + sender + master + pad1 ) )
3156 * + SHA1( master + pad2 +
3157 * SHA1( handshake + sender + master + pad1 ) )
Paul Bakker5121ce52009-01-03 21:22:43 +00003158 */
3159
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003160#if !defined(MBEDTLS_MD5_ALT)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003161 MBEDTLS_SSL_DEBUG_BUF(4, "finished md5 state", (unsigned char *)
3162 md5.state, sizeof(md5.state));
Paul Bakker90995b52013-06-24 19:20:35 +02003163#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00003164
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003165#if !defined(MBEDTLS_SHA1_ALT)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003166 MBEDTLS_SSL_DEBUG_BUF(4, "finished sha1 state", (unsigned char *)
3167 sha1.state, sizeof(sha1.state));
Paul Bakker90995b52013-06-24 19:20:35 +02003168#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00003169
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003170 sender = (from == MBEDTLS_SSL_IS_CLIENT) ? "CLNT"
Paul Bakker3c2122f2013-06-24 19:03:14 +02003171 : "SRVR";
Paul Bakker5121ce52009-01-03 21:22:43 +00003172
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003173 memset(padbuf, 0x36, 48);
Paul Bakker5121ce52009-01-03 21:22:43 +00003174
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003175 mbedtls_md5_update_ret(&md5, (const unsigned char *) sender, 4);
3176 mbedtls_md5_update_ret(&md5, session->master, 48);
3177 mbedtls_md5_update_ret(&md5, padbuf, 48);
3178 mbedtls_md5_finish_ret(&md5, md5sum);
Paul Bakker5121ce52009-01-03 21:22:43 +00003179
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003180 mbedtls_sha1_update_ret(&sha1, (const unsigned char *) sender, 4);
3181 mbedtls_sha1_update_ret(&sha1, session->master, 48);
3182 mbedtls_sha1_update_ret(&sha1, padbuf, 40);
3183 mbedtls_sha1_finish_ret(&sha1, sha1sum);
Paul Bakker5121ce52009-01-03 21:22:43 +00003184
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003185 memset(padbuf, 0x5C, 48);
Paul Bakker5121ce52009-01-03 21:22:43 +00003186
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003187 mbedtls_md5_starts_ret(&md5);
3188 mbedtls_md5_update_ret(&md5, session->master, 48);
3189 mbedtls_md5_update_ret(&md5, padbuf, 48);
3190 mbedtls_md5_update_ret(&md5, md5sum, 16);
3191 mbedtls_md5_finish_ret(&md5, buf);
Paul Bakker5121ce52009-01-03 21:22:43 +00003192
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003193 mbedtls_sha1_starts_ret(&sha1);
3194 mbedtls_sha1_update_ret(&sha1, session->master, 48);
3195 mbedtls_sha1_update_ret(&sha1, padbuf, 40);
3196 mbedtls_sha1_update_ret(&sha1, sha1sum, 20);
3197 mbedtls_sha1_finish_ret(&sha1, buf + 16);
Paul Bakker5121ce52009-01-03 21:22:43 +00003198
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003199 MBEDTLS_SSL_DEBUG_BUF(3, "calc finished result", buf, 36);
Paul Bakker5121ce52009-01-03 21:22:43 +00003200
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003201 mbedtls_md5_free(&md5);
3202 mbedtls_sha1_free(&sha1);
Paul Bakker5121ce52009-01-03 21:22:43 +00003203
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003204 mbedtls_platform_zeroize(padbuf, sizeof(padbuf));
3205 mbedtls_platform_zeroize(md5sum, sizeof(md5sum));
3206 mbedtls_platform_zeroize(sha1sum, sizeof(sha1sum));
Paul Bakker5121ce52009-01-03 21:22:43 +00003207
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003208 MBEDTLS_SSL_DEBUG_MSG(2, ("<= calc finished"));
Paul Bakker5121ce52009-01-03 21:22:43 +00003209}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003210#endif /* MBEDTLS_SSL_PROTO_SSL3 */
Paul Bakker5121ce52009-01-03 21:22:43 +00003211
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003212#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1)
Paul Bakker1ef83d62012-04-11 12:09:53 +00003213static void ssl_calc_finished_tls(
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003214 mbedtls_ssl_context *ssl, unsigned char *buf, int from)
Paul Bakker5121ce52009-01-03 21:22:43 +00003215{
Paul Bakker1ef83d62012-04-11 12:09:53 +00003216 int len = 12;
Paul Bakker3c2122f2013-06-24 19:03:14 +02003217 const char *sender;
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02003218 mbedtls_md5_context md5;
3219 mbedtls_sha1_context sha1;
Paul Bakker1ef83d62012-04-11 12:09:53 +00003220 unsigned char padbuf[36];
Paul Bakker5121ce52009-01-03 21:22:43 +00003221
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003222 mbedtls_ssl_session *session = ssl->session_negotiate;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003223 if (!session) {
Paul Bakker48916f92012-09-16 19:57:18 +00003224 session = ssl->session;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003225 }
Paul Bakker48916f92012-09-16 19:57:18 +00003226
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003227 MBEDTLS_SSL_DEBUG_MSG(2, ("=> calc finished tls"));
Paul Bakker5121ce52009-01-03 21:22:43 +00003228
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003229 mbedtls_md5_init(&md5);
3230 mbedtls_sha1_init(&sha1);
Manuel Pégourié-Gonnard001f2b62015-07-06 16:21:13 +02003231
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003232 mbedtls_md5_clone(&md5, &ssl->handshake->fin_md5);
3233 mbedtls_sha1_clone(&sha1, &ssl->handshake->fin_sha1);
Paul Bakker5121ce52009-01-03 21:22:43 +00003234
Paul Bakker1ef83d62012-04-11 12:09:53 +00003235 /*
3236 * TLSv1:
3237 * hash = PRF( master, finished_label,
3238 * MD5( handshake ) + SHA1( handshake ) )[0..11]
3239 */
Paul Bakker5121ce52009-01-03 21:22:43 +00003240
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003241#if !defined(MBEDTLS_MD5_ALT)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003242 MBEDTLS_SSL_DEBUG_BUF(4, "finished md5 state", (unsigned char *)
3243 md5.state, sizeof(md5.state));
Paul Bakker90995b52013-06-24 19:20:35 +02003244#endif
Paul Bakker1ef83d62012-04-11 12:09:53 +00003245
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003246#if !defined(MBEDTLS_SHA1_ALT)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003247 MBEDTLS_SSL_DEBUG_BUF(4, "finished sha1 state", (unsigned char *)
3248 sha1.state, sizeof(sha1.state));
Paul Bakker90995b52013-06-24 19:20:35 +02003249#endif
Paul Bakker1ef83d62012-04-11 12:09:53 +00003250
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003251 sender = (from == MBEDTLS_SSL_IS_CLIENT)
Paul Bakker3c2122f2013-06-24 19:03:14 +02003252 ? "client finished"
3253 : "server finished";
Paul Bakker1ef83d62012-04-11 12:09:53 +00003254
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003255 mbedtls_md5_finish_ret(&md5, padbuf);
3256 mbedtls_sha1_finish_ret(&sha1, padbuf + 16);
Paul Bakker1ef83d62012-04-11 12:09:53 +00003257
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003258 ssl->handshake->tls_prf(session->master, 48, sender,
3259 padbuf, 36, buf, len);
Paul Bakker1ef83d62012-04-11 12:09:53 +00003260
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003261 MBEDTLS_SSL_DEBUG_BUF(3, "calc finished result", buf, len);
Paul Bakker1ef83d62012-04-11 12:09:53 +00003262
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003263 mbedtls_md5_free(&md5);
3264 mbedtls_sha1_free(&sha1);
Paul Bakker1ef83d62012-04-11 12:09:53 +00003265
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003266 mbedtls_platform_zeroize(padbuf, sizeof(padbuf));
Paul Bakker1ef83d62012-04-11 12:09:53 +00003267
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003268 MBEDTLS_SSL_DEBUG_MSG(2, ("<= calc finished"));
Paul Bakker1ef83d62012-04-11 12:09:53 +00003269}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003270#endif /* MBEDTLS_SSL_PROTO_TLS1 || MBEDTLS_SSL_PROTO_TLS1_1 */
Paul Bakker1ef83d62012-04-11 12:09:53 +00003271
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003272#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
3273#if defined(MBEDTLS_SHA256_C)
Paul Bakkerca4ab492012-04-18 14:23:57 +00003274static void ssl_calc_finished_tls_sha256(
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003275 mbedtls_ssl_context *ssl, unsigned char *buf, int from)
Paul Bakker1ef83d62012-04-11 12:09:53 +00003276{
3277 int len = 12;
Paul Bakker3c2122f2013-06-24 19:03:14 +02003278 const char *sender;
Paul Bakker1ef83d62012-04-11 12:09:53 +00003279 unsigned char padbuf[32];
Andrzej Kurekeb342242019-01-29 09:14:33 -05003280#if defined(MBEDTLS_USE_PSA_CRYPTO)
3281 size_t hash_size;
Jaeden Amero34973232019-02-20 10:32:28 +00003282 psa_hash_operation_t sha256_psa = PSA_HASH_OPERATION_INIT;
Andrzej Kurekeb342242019-01-29 09:14:33 -05003283 psa_status_t status;
3284#else
3285 mbedtls_sha256_context sha256;
3286#endif
Paul Bakker1ef83d62012-04-11 12:09:53 +00003287
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003288 mbedtls_ssl_session *session = ssl->session_negotiate;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003289 if (!session) {
Paul Bakker48916f92012-09-16 19:57:18 +00003290 session = ssl->session;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003291 }
Paul Bakker48916f92012-09-16 19:57:18 +00003292
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003293 sender = (from == MBEDTLS_SSL_IS_CLIENT)
Andrzej Kurekeb342242019-01-29 09:14:33 -05003294 ? "client finished"
3295 : "server finished";
3296
3297#if defined(MBEDTLS_USE_PSA_CRYPTO)
3298 sha256_psa = psa_hash_operation_init();
3299
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003300 MBEDTLS_SSL_DEBUG_MSG(2, ("=> calc PSA finished tls sha256"));
Andrzej Kurekeb342242019-01-29 09:14:33 -05003301
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003302 status = psa_hash_clone(&ssl->handshake->fin_sha256_psa, &sha256_psa);
3303 if (status != PSA_SUCCESS) {
3304 MBEDTLS_SSL_DEBUG_MSG(2, ("PSA hash clone failed"));
Andrzej Kurekeb342242019-01-29 09:14:33 -05003305 return;
3306 }
3307
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003308 status = psa_hash_finish(&sha256_psa, padbuf, sizeof(padbuf), &hash_size);
3309 if (status != PSA_SUCCESS) {
3310 MBEDTLS_SSL_DEBUG_MSG(2, ("PSA hash finish failed"));
Andrzej Kurekeb342242019-01-29 09:14:33 -05003311 return;
3312 }
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003313 MBEDTLS_SSL_DEBUG_BUF(3, "PSA calculated padbuf", padbuf, 32);
Andrzej Kurekeb342242019-01-29 09:14:33 -05003314#else
3315
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003316 mbedtls_sha256_init(&sha256);
Manuel Pégourié-Gonnard001f2b62015-07-06 16:21:13 +02003317
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003318 MBEDTLS_SSL_DEBUG_MSG(2, ("=> calc finished tls sha256"));
Paul Bakker1ef83d62012-04-11 12:09:53 +00003319
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003320 mbedtls_sha256_clone(&sha256, &ssl->handshake->fin_sha256);
Paul Bakker1ef83d62012-04-11 12:09:53 +00003321
3322 /*
3323 * TLSv1.2:
3324 * hash = PRF( master, finished_label,
3325 * Hash( handshake ) )[0.11]
3326 */
3327
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003328#if !defined(MBEDTLS_SHA256_ALT)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003329 MBEDTLS_SSL_DEBUG_BUF(4, "finished sha2 state", (unsigned char *)
3330 sha256.state, sizeof(sha256.state));
Paul Bakker90995b52013-06-24 19:20:35 +02003331#endif
Paul Bakker1ef83d62012-04-11 12:09:53 +00003332
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003333 mbedtls_sha256_finish_ret(&sha256, padbuf);
3334 mbedtls_sha256_free(&sha256);
Andrzej Kurekeb342242019-01-29 09:14:33 -05003335#endif /* MBEDTLS_USE_PSA_CRYPTO */
Paul Bakker1ef83d62012-04-11 12:09:53 +00003336
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003337 ssl->handshake->tls_prf(session->master, 48, sender,
3338 padbuf, 32, buf, len);
Paul Bakker1ef83d62012-04-11 12:09:53 +00003339
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003340 MBEDTLS_SSL_DEBUG_BUF(3, "calc finished result", buf, len);
Paul Bakker1ef83d62012-04-11 12:09:53 +00003341
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003342 mbedtls_platform_zeroize(padbuf, sizeof(padbuf));
Paul Bakker1ef83d62012-04-11 12:09:53 +00003343
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003344 MBEDTLS_SSL_DEBUG_MSG(2, ("<= calc finished"));
Paul Bakker1ef83d62012-04-11 12:09:53 +00003345}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003346#endif /* MBEDTLS_SHA256_C */
Paul Bakker1ef83d62012-04-11 12:09:53 +00003347
Gilles Peskined2d59372021-05-12 22:43:27 +02003348#if defined(MBEDTLS_SHA512_C) && !defined(MBEDTLS_SHA512_NO_SHA384)
Rodrigo Dias Corread596ca82020-11-25 00:42:28 -03003349
Paul Bakkerca4ab492012-04-18 14:23:57 +00003350static void ssl_calc_finished_tls_sha384(
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003351 mbedtls_ssl_context *ssl, unsigned char *buf, int from)
Paul Bakkerca4ab492012-04-18 14:23:57 +00003352{
3353 int len = 12;
Paul Bakker3c2122f2013-06-24 19:03:14 +02003354 const char *sender;
Rodrigo Dias Corread596ca82020-11-25 00:42:28 -03003355 unsigned char padbuf[48];
Andrzej Kurekeb342242019-01-29 09:14:33 -05003356#if defined(MBEDTLS_USE_PSA_CRYPTO)
3357 size_t hash_size;
Jaeden Amero34973232019-02-20 10:32:28 +00003358 psa_hash_operation_t sha384_psa = PSA_HASH_OPERATION_INIT;
Andrzej Kurekeb342242019-01-29 09:14:33 -05003359 psa_status_t status;
3360#else
3361 mbedtls_sha512_context sha512;
3362#endif
Paul Bakkerca4ab492012-04-18 14:23:57 +00003363
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003364 mbedtls_ssl_session *session = ssl->session_negotiate;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003365 if (!session) {
Paul Bakker48916f92012-09-16 19:57:18 +00003366 session = ssl->session;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003367 }
Paul Bakker48916f92012-09-16 19:57:18 +00003368
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003369 sender = (from == MBEDTLS_SSL_IS_CLIENT)
Andrzej Kurekeb342242019-01-29 09:14:33 -05003370 ? "client finished"
3371 : "server finished";
3372
3373#if defined(MBEDTLS_USE_PSA_CRYPTO)
Andrzej Kurek972fba52019-01-30 03:29:12 -05003374 sha384_psa = psa_hash_operation_init();
Andrzej Kurekeb342242019-01-29 09:14:33 -05003375
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003376 MBEDTLS_SSL_DEBUG_MSG(2, ("=> calc PSA finished tls sha384"));
Andrzej Kurekeb342242019-01-29 09:14:33 -05003377
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003378 status = psa_hash_clone(&ssl->handshake->fin_sha384_psa, &sha384_psa);
3379 if (status != PSA_SUCCESS) {
3380 MBEDTLS_SSL_DEBUG_MSG(2, ("PSA hash clone failed"));
Andrzej Kurekeb342242019-01-29 09:14:33 -05003381 return;
3382 }
3383
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003384 status = psa_hash_finish(&sha384_psa, padbuf, sizeof(padbuf), &hash_size);
3385 if (status != PSA_SUCCESS) {
3386 MBEDTLS_SSL_DEBUG_MSG(2, ("PSA hash finish failed"));
Andrzej Kurekeb342242019-01-29 09:14:33 -05003387 return;
3388 }
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003389 MBEDTLS_SSL_DEBUG_BUF(3, "PSA calculated padbuf", padbuf, 48);
Andrzej Kurekeb342242019-01-29 09:14:33 -05003390#else
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003391 mbedtls_sha512_init(&sha512);
Manuel Pégourié-Gonnard001f2b62015-07-06 16:21:13 +02003392
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003393 MBEDTLS_SSL_DEBUG_MSG(2, ("=> calc finished tls sha384"));
Paul Bakkerca4ab492012-04-18 14:23:57 +00003394
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003395 mbedtls_sha512_clone(&sha512, &ssl->handshake->fin_sha512);
Paul Bakkerca4ab492012-04-18 14:23:57 +00003396
3397 /*
3398 * TLSv1.2:
3399 * hash = PRF( master, finished_label,
3400 * Hash( handshake ) )[0.11]
3401 */
3402
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003403#if !defined(MBEDTLS_SHA512_ALT)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003404 MBEDTLS_SSL_DEBUG_BUF(4, "finished sha512 state", (unsigned char *)
3405 sha512.state, sizeof(sha512.state));
Paul Bakker90995b52013-06-24 19:20:35 +02003406#endif
Gilles Peskinebb66dac2021-05-13 00:00:45 +02003407 /* mbedtls_sha512_finish_ret's output parameter is declared as a
Tom Cosgrove49f99bc2022-12-04 16:44:21 +00003408 * 64-byte buffer, but since we're using SHA-384, we know that the
Gilles Peskinebb66dac2021-05-13 00:00:45 +02003409 * output fits in 48 bytes. This is correct C, but GCC 11.1 warns
3410 * about it.
Rodrigo Dias Corread596ca82020-11-25 00:42:28 -03003411 */
Gilles Peskinebb66dac2021-05-13 00:00:45 +02003412#if defined(__GNUC__) && __GNUC__ >= 11
3413#pragma GCC diagnostic push
3414#pragma GCC diagnostic ignored "-Wstringop-overflow"
3415#endif
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003416 mbedtls_sha512_finish_ret(&sha512, padbuf);
Gilles Peskinebb66dac2021-05-13 00:00:45 +02003417#if defined(__GNUC__) && __GNUC__ >= 11
3418#pragma GCC diagnostic pop
3419#endif
Paul Bakkerca4ab492012-04-18 14:23:57 +00003420
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003421 mbedtls_sha512_free(&sha512);
Andrzej Kurekeb342242019-01-29 09:14:33 -05003422#endif
Paul Bakkerca4ab492012-04-18 14:23:57 +00003423
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003424 ssl->handshake->tls_prf(session->master, 48, sender,
3425 padbuf, 48, buf, len);
Paul Bakkerca4ab492012-04-18 14:23:57 +00003426
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003427 MBEDTLS_SSL_DEBUG_BUF(3, "calc finished result", buf, len);
Paul Bakkerca4ab492012-04-18 14:23:57 +00003428
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003429 mbedtls_platform_zeroize(padbuf, sizeof(padbuf));
Paul Bakkerca4ab492012-04-18 14:23:57 +00003430
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003431 MBEDTLS_SSL_DEBUG_MSG(2, ("<= calc finished"));
Paul Bakkerca4ab492012-04-18 14:23:57 +00003432}
Gilles Peskined2d59372021-05-12 22:43:27 +02003433#endif /* MBEDTLS_SHA512_C && !MBEDTLS_SHA512_NO_SHA384 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003434#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakkerca4ab492012-04-18 14:23:57 +00003435
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003436void mbedtls_ssl_handshake_wrapup_free_hs_transform(mbedtls_ssl_context *ssl)
Paul Bakker48916f92012-09-16 19:57:18 +00003437{
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003438 MBEDTLS_SSL_DEBUG_MSG(3, ("=> handshake wrapup: final free"));
Paul Bakker48916f92012-09-16 19:57:18 +00003439
3440 /*
3441 * Free our handshake params
3442 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003443 mbedtls_ssl_handshake_free(ssl);
3444 mbedtls_free(ssl->handshake);
Paul Bakker48916f92012-09-16 19:57:18 +00003445 ssl->handshake = NULL;
3446
3447 /*
Shaun Case0e7791f2021-12-20 21:14:10 -08003448 * Free the previous transform and switch in the current one
Paul Bakker48916f92012-09-16 19:57:18 +00003449 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003450 if (ssl->transform) {
3451 mbedtls_ssl_transform_free(ssl->transform);
3452 mbedtls_free(ssl->transform);
Paul Bakker48916f92012-09-16 19:57:18 +00003453 }
3454 ssl->transform = ssl->transform_negotiate;
3455 ssl->transform_negotiate = NULL;
3456
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003457 MBEDTLS_SSL_DEBUG_MSG(3, ("<= handshake wrapup: final free"));
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02003458}
3459
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003460void mbedtls_ssl_handshake_wrapup(mbedtls_ssl_context *ssl)
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02003461{
3462 int resume = ssl->handshake->resume;
3463
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003464 MBEDTLS_SSL_DEBUG_MSG(3, ("=> handshake wrapup"));
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02003465
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003466#if defined(MBEDTLS_SSL_RENEGOTIATION)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003467 if (ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS) {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003468 ssl->renego_status = MBEDTLS_SSL_RENEGOTIATION_DONE;
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02003469 ssl->renego_records_seen = 0;
3470 }
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01003471#endif
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02003472
3473 /*
3474 * Free the previous session and switch in the current one
3475 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003476 if (ssl->session) {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003477#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
Manuel Pégourié-Gonnard1a034732014-11-04 17:36:18 +01003478 /* RFC 7366 3.1: keep the EtM state */
3479 ssl->session_negotiate->encrypt_then_mac =
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003480 ssl->session->encrypt_then_mac;
Manuel Pégourié-Gonnard1a034732014-11-04 17:36:18 +01003481#endif
3482
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003483 mbedtls_ssl_session_free(ssl->session);
3484 mbedtls_free(ssl->session);
Paul Bakker0a597072012-09-25 21:55:46 +00003485 }
3486 ssl->session = ssl->session_negotiate;
Paul Bakker48916f92012-09-16 19:57:18 +00003487 ssl->session_negotiate = NULL;
3488
Paul Bakker0a597072012-09-25 21:55:46 +00003489 /*
3490 * Add cache entry
3491 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003492 if (ssl->conf->f_set_cache != NULL &&
Manuel Pégourié-Gonnard12ad7982015-06-18 15:50:37 +02003493 ssl->session->id_len != 0 &&
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003494 resume == 0) {
3495 if (ssl->conf->f_set_cache(ssl->conf->p_cache, ssl->session) != 0) {
3496 MBEDTLS_SSL_DEBUG_MSG(1, ("cache did not store session"));
3497 }
Manuel Pégourié-Gonnardc086cce2013-08-02 14:13:02 +02003498 }
Paul Bakker0a597072012-09-25 21:55:46 +00003499
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003500#if defined(MBEDTLS_SSL_PROTO_DTLS)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003501 if (ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
3502 ssl->handshake->flight != NULL) {
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02003503 /* Cancel handshake timer */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003504 mbedtls_ssl_set_timer(ssl, 0);
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02003505
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02003506 /* Keep last flight around in case we need to resend it:
3507 * we need the handshake and transform structures for that */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003508 MBEDTLS_SSL_DEBUG_MSG(3, ("skip freeing handshake and transform"));
3509 } else
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02003510#endif
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003511 mbedtls_ssl_handshake_wrapup_free_hs_transform(ssl);
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02003512
Paul Bakker48916f92012-09-16 19:57:18 +00003513 ssl->state++;
3514
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003515 MBEDTLS_SSL_DEBUG_MSG(3, ("<= handshake wrapup"));
Paul Bakker48916f92012-09-16 19:57:18 +00003516}
3517
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003518int mbedtls_ssl_write_finished(mbedtls_ssl_context *ssl)
Paul Bakker1ef83d62012-04-11 12:09:53 +00003519{
Manuel Pégourié-Gonnard879a4f92014-07-11 22:31:12 +02003520 int ret, hash_len;
Paul Bakker1ef83d62012-04-11 12:09:53 +00003521
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003522 MBEDTLS_SSL_DEBUG_MSG(2, ("=> write finished"));
Paul Bakker1ef83d62012-04-11 12:09:53 +00003523
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003524 mbedtls_ssl_update_out_pointers(ssl, ssl->transform_negotiate);
Paul Bakker92be97b2013-01-02 17:30:03 +01003525
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003526 ssl->handshake->calc_finished(ssl, ssl->out_msg + 4, ssl->conf->endpoint);
Paul Bakker1ef83d62012-04-11 12:09:53 +00003527
Manuel Pégourié-Gonnard214a8482016-02-22 11:27:26 +01003528 /*
3529 * RFC 5246 7.4.9 (Page 63) says 12 is the default length and ciphersuites
3530 * may define some other value. Currently (early 2016), no defined
3531 * ciphersuite does this (and this is unlikely to change as activity has
3532 * moved to TLS 1.3 now) so we can keep the hardcoded 12 here.
3533 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003534 hash_len = (ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0) ? 36 : 12;
Paul Bakker5121ce52009-01-03 21:22:43 +00003535
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003536#if defined(MBEDTLS_SSL_RENEGOTIATION)
Paul Bakker48916f92012-09-16 19:57:18 +00003537 ssl->verify_data_len = hash_len;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003538 memcpy(ssl->own_verify_data, ssl->out_msg + 4, hash_len);
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01003539#endif
Paul Bakker48916f92012-09-16 19:57:18 +00003540
Paul Bakker5121ce52009-01-03 21:22:43 +00003541 ssl->out_msglen = 4 + hash_len;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003542 ssl->out_msgtype = MBEDTLS_SSL_MSG_HANDSHAKE;
3543 ssl->out_msg[0] = MBEDTLS_SSL_HS_FINISHED;
Paul Bakker5121ce52009-01-03 21:22:43 +00003544
3545 /*
3546 * In case of session resuming, invert the client and server
3547 * ChangeCipherSpec messages order.
3548 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003549 if (ssl->handshake->resume != 0) {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003550#if defined(MBEDTLS_SSL_CLI_C)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003551 if (ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT) {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003552 ssl->state = MBEDTLS_SSL_HANDSHAKE_WRAPUP;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003553 }
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01003554#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003555#if defined(MBEDTLS_SSL_SRV_C)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003556 if (ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER) {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003557 ssl->state = MBEDTLS_SSL_CLIENT_CHANGE_CIPHER_SPEC;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003558 }
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01003559#endif
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003560 } else {
Paul Bakker5121ce52009-01-03 21:22:43 +00003561 ssl->state++;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003562 }
Paul Bakker5121ce52009-01-03 21:22:43 +00003563
Paul Bakker48916f92012-09-16 19:57:18 +00003564 /*
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02003565 * Switch to our negotiated transform and session parameters for outbound
3566 * data.
Paul Bakker48916f92012-09-16 19:57:18 +00003567 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003568 MBEDTLS_SSL_DEBUG_MSG(3, ("switching to new transform spec for outbound data"));
Manuel Pégourié-Gonnard5afb1672014-02-16 18:33:22 +01003569
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003570#if defined(MBEDTLS_SSL_PROTO_DTLS)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003571 if (ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM) {
Manuel Pégourié-Gonnard879a4f92014-07-11 22:31:12 +02003572 unsigned char i;
3573
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003574 /* Remember current epoch settings for resending */
3575 ssl->handshake->alt_transform_out = ssl->transform_out;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003576 memcpy(ssl->handshake->alt_out_ctr, ssl->cur_out_ctr, 8);
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003577
Manuel Pégourié-Gonnard879a4f92014-07-11 22:31:12 +02003578 /* Set sequence_number to zero */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003579 memset(ssl->cur_out_ctr + 2, 0, 6);
Manuel Pégourié-Gonnard879a4f92014-07-11 22:31:12 +02003580
3581 /* Increment epoch */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003582 for (i = 2; i > 0; i--) {
3583 if (++ssl->cur_out_ctr[i - 1] != 0) {
Manuel Pégourié-Gonnard879a4f92014-07-11 22:31:12 +02003584 break;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003585 }
3586 }
Manuel Pégourié-Gonnard879a4f92014-07-11 22:31:12 +02003587
3588 /* The loop goes to its end iff the counter is wrapping */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003589 if (i == 0) {
3590 MBEDTLS_SSL_DEBUG_MSG(1, ("DTLS epoch would wrap"));
3591 return MBEDTLS_ERR_SSL_COUNTER_WRAPPING;
Manuel Pégourié-Gonnard879a4f92014-07-11 22:31:12 +02003592 }
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003593 } else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003594#endif /* MBEDTLS_SSL_PROTO_DTLS */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003595 memset(ssl->cur_out_ctr, 0, 8);
Paul Bakker5121ce52009-01-03 21:22:43 +00003596
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003597 ssl->transform_out = ssl->transform_negotiate;
3598 ssl->session_out = ssl->session_negotiate;
3599
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003600#if defined(MBEDTLS_SSL_HW_RECORD_ACCEL)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003601 if (mbedtls_ssl_hw_record_activate != NULL) {
3602 if ((ret = mbedtls_ssl_hw_record_activate(ssl, MBEDTLS_SSL_CHANNEL_OUTBOUND)) != 0) {
3603 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ssl_hw_record_activate", ret);
3604 return MBEDTLS_ERR_SSL_HW_ACCEL_FAILED;
Paul Bakker07eb38b2012-12-19 14:42:06 +01003605 }
3606 }
3607#endif
3608
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003609#if defined(MBEDTLS_SSL_PROTO_DTLS)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003610 if (ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM) {
3611 mbedtls_ssl_send_flight_completed(ssl);
3612 }
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02003613#endif
3614
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003615 if ((ret = mbedtls_ssl_write_handshake_msg(ssl)) != 0) {
3616 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ssl_write_handshake_msg", ret);
3617 return ret;
Paul Bakker5121ce52009-01-03 21:22:43 +00003618 }
3619
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02003620#if defined(MBEDTLS_SSL_PROTO_DTLS)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003621 if (ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
3622 (ret = mbedtls_ssl_flight_transmit(ssl)) != 0) {
3623 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ssl_flight_transmit", ret);
3624 return ret;
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02003625 }
3626#endif
3627
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003628 MBEDTLS_SSL_DEBUG_MSG(2, ("<= write finished"));
Paul Bakker5121ce52009-01-03 21:22:43 +00003629
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003630 return 0;
Paul Bakker5121ce52009-01-03 21:22:43 +00003631}
3632
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003633#if defined(MBEDTLS_SSL_PROTO_SSL3)
Manuel Pégourié-Gonnardca6440b2014-09-10 12:39:54 +00003634#define SSL_MAX_HASH_LEN 36
3635#else
3636#define SSL_MAX_HASH_LEN 12
3637#endif
3638
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003639int mbedtls_ssl_parse_finished(mbedtls_ssl_context *ssl)
Paul Bakker5121ce52009-01-03 21:22:43 +00003640{
Janos Follath865b3eb2019-12-16 11:46:15 +00003641 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnard879a4f92014-07-11 22:31:12 +02003642 unsigned int hash_len;
Manuel Pégourié-Gonnardca6440b2014-09-10 12:39:54 +00003643 unsigned char buf[SSL_MAX_HASH_LEN];
Paul Bakker5121ce52009-01-03 21:22:43 +00003644
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003645 MBEDTLS_SSL_DEBUG_MSG(2, ("=> parse finished"));
Paul Bakker5121ce52009-01-03 21:22:43 +00003646
Gilles Peskine622d8042021-12-13 14:38:40 +01003647 /* There is currently no ciphersuite using another length with TLS 1.2 */
3648#if defined(MBEDTLS_SSL_PROTO_SSL3)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003649 if (ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0) {
Gilles Peskine622d8042021-12-13 14:38:40 +01003650 hash_len = 36;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003651 } else
Gilles Peskine622d8042021-12-13 14:38:40 +01003652#endif
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003653 hash_len = 12;
Gilles Peskine622d8042021-12-13 14:38:40 +01003654
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003655 ssl->handshake->calc_finished(ssl, buf, ssl->conf->endpoint ^ 1);
Paul Bakker5121ce52009-01-03 21:22:43 +00003656
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003657 if ((ret = mbedtls_ssl_read_record(ssl, 1)) != 0) {
3658 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ssl_read_record", ret);
Gilles Peskinef91b2e52021-12-13 12:36:15 +01003659 goto exit;
Paul Bakker5121ce52009-01-03 21:22:43 +00003660 }
3661
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003662 if (ssl->in_msgtype != MBEDTLS_SSL_MSG_HANDSHAKE) {
3663 MBEDTLS_SSL_DEBUG_MSG(1, ("bad finished message"));
3664 mbedtls_ssl_send_alert_message(ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
3665 MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE);
Gilles Peskinef91b2e52021-12-13 12:36:15 +01003666 ret = MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE;
3667 goto exit;
Paul Bakker5121ce52009-01-03 21:22:43 +00003668 }
3669
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003670 if (ssl->in_msg[0] != MBEDTLS_SSL_HS_FINISHED ||
3671 ssl->in_hslen != mbedtls_ssl_hs_hdr_len(ssl) + hash_len) {
3672 MBEDTLS_SSL_DEBUG_MSG(1, ("bad finished message"));
3673 mbedtls_ssl_send_alert_message(ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
3674 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR);
Gilles Peskinef91b2e52021-12-13 12:36:15 +01003675 ret = MBEDTLS_ERR_SSL_BAD_HS_FINISHED;
3676 goto exit;
Paul Bakker5121ce52009-01-03 21:22:43 +00003677 }
3678
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003679 if (mbedtls_ct_memcmp(ssl->in_msg + mbedtls_ssl_hs_hdr_len(ssl),
3680 buf, hash_len) != 0) {
3681 MBEDTLS_SSL_DEBUG_MSG(1, ("bad finished message"));
3682 mbedtls_ssl_send_alert_message(ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
3683 MBEDTLS_SSL_ALERT_MSG_DECRYPT_ERROR);
Gilles Peskinef91b2e52021-12-13 12:36:15 +01003684 ret = MBEDTLS_ERR_SSL_BAD_HS_FINISHED;
3685 goto exit;
Paul Bakker5121ce52009-01-03 21:22:43 +00003686 }
3687
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003688#if defined(MBEDTLS_SSL_RENEGOTIATION)
Paul Bakker48916f92012-09-16 19:57:18 +00003689 ssl->verify_data_len = hash_len;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003690 memcpy(ssl->peer_verify_data, buf, hash_len);
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01003691#endif
Paul Bakker48916f92012-09-16 19:57:18 +00003692
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003693 if (ssl->handshake->resume != 0) {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003694#if defined(MBEDTLS_SSL_CLI_C)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003695 if (ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT) {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003696 ssl->state = MBEDTLS_SSL_CLIENT_CHANGE_CIPHER_SPEC;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003697 }
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01003698#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003699#if defined(MBEDTLS_SSL_SRV_C)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003700 if (ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER) {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003701 ssl->state = MBEDTLS_SSL_HANDSHAKE_WRAPUP;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003702 }
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01003703#endif
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003704 } else {
Paul Bakker5121ce52009-01-03 21:22:43 +00003705 ssl->state++;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003706 }
Paul Bakker5121ce52009-01-03 21:22:43 +00003707
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003708#if defined(MBEDTLS_SSL_PROTO_DTLS)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003709 if (ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM) {
3710 mbedtls_ssl_recv_flight_completed(ssl);
3711 }
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003712#endif
3713
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003714 MBEDTLS_SSL_DEBUG_MSG(2, ("<= parse finished"));
Paul Bakker5121ce52009-01-03 21:22:43 +00003715
Gilles Peskinef91b2e52021-12-13 12:36:15 +01003716exit:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003717 mbedtls_platform_zeroize(buf, hash_len);
3718 return ret;
Paul Bakker5121ce52009-01-03 21:22:43 +00003719}
3720
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003721static void ssl_handshake_params_init(mbedtls_ssl_handshake_params *handshake)
Paul Bakkeraccaffe2014-06-26 13:37:14 +02003722{
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003723 memset(handshake, 0, sizeof(mbedtls_ssl_handshake_params));
Paul Bakkeraccaffe2014-06-26 13:37:14 +02003724
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003725#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1) || \
3726 defined(MBEDTLS_SSL_PROTO_TLS1_1)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003727 mbedtls_md5_init(&handshake->fin_md5);
3728 mbedtls_sha1_init(&handshake->fin_sha1);
3729 mbedtls_md5_starts_ret(&handshake->fin_md5);
3730 mbedtls_sha1_starts_ret(&handshake->fin_sha1);
Paul Bakkeraccaffe2014-06-26 13:37:14 +02003731#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003732#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
3733#if defined(MBEDTLS_SHA256_C)
Andrzej Kurekeb342242019-01-29 09:14:33 -05003734#if defined(MBEDTLS_USE_PSA_CRYPTO)
3735 handshake->fin_sha256_psa = psa_hash_operation_init();
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003736 psa_hash_setup(&handshake->fin_sha256_psa, PSA_ALG_SHA_256);
Andrzej Kurekeb342242019-01-29 09:14:33 -05003737#else
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003738 mbedtls_sha256_init(&handshake->fin_sha256);
3739 mbedtls_sha256_starts_ret(&handshake->fin_sha256, 0);
Paul Bakkeraccaffe2014-06-26 13:37:14 +02003740#endif
Andrzej Kurekeb342242019-01-29 09:14:33 -05003741#endif
Gilles Peskined2d59372021-05-12 22:43:27 +02003742#if defined(MBEDTLS_SHA512_C) && !defined(MBEDTLS_SHA512_NO_SHA384)
Andrzej Kurekeb342242019-01-29 09:14:33 -05003743#if defined(MBEDTLS_USE_PSA_CRYPTO)
Andrzej Kurek972fba52019-01-30 03:29:12 -05003744 handshake->fin_sha384_psa = psa_hash_operation_init();
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003745 psa_hash_setup(&handshake->fin_sha384_psa, PSA_ALG_SHA_384);
Andrzej Kurekeb342242019-01-29 09:14:33 -05003746#else
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003747 mbedtls_sha512_init(&handshake->fin_sha512);
3748 mbedtls_sha512_starts_ret(&handshake->fin_sha512, 1);
Paul Bakkeraccaffe2014-06-26 13:37:14 +02003749#endif
Andrzej Kurekeb342242019-01-29 09:14:33 -05003750#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003751#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakkeraccaffe2014-06-26 13:37:14 +02003752
3753 handshake->update_checksum = ssl_update_checksum_start;
Hanno Becker7e5437a2017-04-28 17:15:26 +01003754
3755#if defined(MBEDTLS_SSL_PROTO_TLS1_2) && \
Gilles Peskineeccd8882020-03-10 12:19:08 +01003756 defined(MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003757 mbedtls_ssl_sig_hash_set_init(&handshake->hash_algs);
Hanno Becker7e5437a2017-04-28 17:15:26 +01003758#endif
Paul Bakkeraccaffe2014-06-26 13:37:14 +02003759
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003760#if defined(MBEDTLS_DHM_C)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003761 mbedtls_dhm_init(&handshake->dhm_ctx);
Paul Bakkeraccaffe2014-06-26 13:37:14 +02003762#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003763#if defined(MBEDTLS_ECDH_C)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003764 mbedtls_ecdh_init(&handshake->ecdh_ctx);
Paul Bakkeraccaffe2014-06-26 13:37:14 +02003765#endif
Manuel Pégourié-Gonnardeef142d2015-09-16 10:05:04 +02003766#if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003767 mbedtls_ecjpake_init(&handshake->ecjpake_ctx);
Manuel Pégourié-Gonnard77c06462015-09-17 13:59:49 +02003768#if defined(MBEDTLS_SSL_CLI_C)
3769 handshake->ecjpake_cache = NULL;
3770 handshake->ecjpake_cache_len = 0;
3771#endif
Manuel Pégourié-Gonnard76cfd3f2015-09-15 12:10:54 +02003772#endif
Manuel Pégourié-Gonnardcdc26ae2015-06-19 12:16:31 +02003773
Gilles Peskineeccd8882020-03-10 12:19:08 +01003774#if defined(MBEDTLS_SSL_ECP_RESTARTABLE_ENABLED)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003775 mbedtls_x509_crt_restart_init(&handshake->ecrs_ctx);
Manuel Pégourié-Gonnard862cde52017-05-17 11:56:15 +02003776#endif
3777
Manuel Pégourié-Gonnardcdc26ae2015-06-19 12:16:31 +02003778#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
3779 handshake->sni_authmode = MBEDTLS_SSL_VERIFY_UNSET;
3780#endif
Hanno Becker75173122019-02-06 16:18:31 +00003781
3782#if defined(MBEDTLS_X509_CRT_PARSE_C) && \
3783 !defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003784 mbedtls_pk_init(&handshake->peer_pubkey);
Hanno Becker75173122019-02-06 16:18:31 +00003785#endif
Paul Bakkeraccaffe2014-06-26 13:37:14 +02003786}
3787
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003788void mbedtls_ssl_transform_init(mbedtls_ssl_transform *transform)
Paul Bakkeraccaffe2014-06-26 13:37:14 +02003789{
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003790 memset(transform, 0, sizeof(mbedtls_ssl_transform));
Paul Bakker84bbeb52014-07-01 14:53:22 +02003791
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003792 mbedtls_cipher_init(&transform->cipher_ctx_enc);
3793 mbedtls_cipher_init(&transform->cipher_ctx_dec);
Paul Bakker84bbeb52014-07-01 14:53:22 +02003794
Hanno Beckerd56ed242018-01-03 15:32:51 +00003795#if defined(MBEDTLS_SSL_SOME_MODES_USE_MAC)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003796 mbedtls_md_init(&transform->md_ctx_enc);
3797 mbedtls_md_init(&transform->md_ctx_dec);
Hanno Beckerd56ed242018-01-03 15:32:51 +00003798#endif
Paul Bakkeraccaffe2014-06-26 13:37:14 +02003799}
3800
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003801void mbedtls_ssl_session_init(mbedtls_ssl_session *session)
Paul Bakkeraccaffe2014-06-26 13:37:14 +02003802{
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003803 memset(session, 0, sizeof(mbedtls_ssl_session));
Paul Bakkeraccaffe2014-06-26 13:37:14 +02003804}
3805
Manuel Pégourié-Gonnardd904d662022-06-17 10:24:00 +02003806MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003807static int ssl_handshake_init(mbedtls_ssl_context *ssl)
Paul Bakker48916f92012-09-16 19:57:18 +00003808{
Paul Bakkeraccaffe2014-06-26 13:37:14 +02003809 /* Clear old handshake information if present */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003810 if (ssl->transform_negotiate) {
3811 mbedtls_ssl_transform_free(ssl->transform_negotiate);
3812 }
3813 if (ssl->session_negotiate) {
3814 mbedtls_ssl_session_free(ssl->session_negotiate);
3815 }
3816 if (ssl->handshake) {
3817 mbedtls_ssl_handshake_free(ssl);
3818 }
Paul Bakkeraccaffe2014-06-26 13:37:14 +02003819
3820 /*
3821 * Either the pointers are now NULL or cleared properly and can be freed.
3822 * Now allocate missing structures.
3823 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003824 if (ssl->transform_negotiate == NULL) {
3825 ssl->transform_negotiate = mbedtls_calloc(1, sizeof(mbedtls_ssl_transform));
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02003826 }
Paul Bakker48916f92012-09-16 19:57:18 +00003827
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003828 if (ssl->session_negotiate == NULL) {
3829 ssl->session_negotiate = mbedtls_calloc(1, sizeof(mbedtls_ssl_session));
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02003830 }
Paul Bakker48916f92012-09-16 19:57:18 +00003831
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003832 if (ssl->handshake == NULL) {
3833 ssl->handshake = mbedtls_calloc(1, sizeof(mbedtls_ssl_handshake_params));
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02003834 }
Andrzej Kurek0afa2a12020-03-03 10:39:58 -05003835#if defined(MBEDTLS_SSL_VARIABLE_BUFFER_LENGTH)
3836 /* If the buffers are too small - reallocate */
Andrzej Kurek8ea68722020-04-03 06:40:47 -04003837
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003838 handle_buffer_resizing(ssl, 0, MBEDTLS_SSL_IN_BUFFER_LEN,
3839 MBEDTLS_SSL_OUT_BUFFER_LEN);
Andrzej Kurek0afa2a12020-03-03 10:39:58 -05003840#endif
Paul Bakker48916f92012-09-16 19:57:18 +00003841
Paul Bakkeraccaffe2014-06-26 13:37:14 +02003842 /* All pointers should exist and can be directly freed without issue */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003843 if (ssl->handshake == NULL ||
Paul Bakker48916f92012-09-16 19:57:18 +00003844 ssl->transform_negotiate == NULL ||
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003845 ssl->session_negotiate == NULL) {
3846 MBEDTLS_SSL_DEBUG_MSG(1, ("alloc() of ssl sub-contexts failed"));
Paul Bakkeraccaffe2014-06-26 13:37:14 +02003847
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003848 mbedtls_free(ssl->handshake);
3849 mbedtls_free(ssl->transform_negotiate);
3850 mbedtls_free(ssl->session_negotiate);
Paul Bakkeraccaffe2014-06-26 13:37:14 +02003851
3852 ssl->handshake = NULL;
3853 ssl->transform_negotiate = NULL;
3854 ssl->session_negotiate = NULL;
3855
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003856 return MBEDTLS_ERR_SSL_ALLOC_FAILED;
Paul Bakker48916f92012-09-16 19:57:18 +00003857 }
3858
Paul Bakkeraccaffe2014-06-26 13:37:14 +02003859 /* Initialize structures */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003860 mbedtls_ssl_session_init(ssl->session_negotiate);
3861 mbedtls_ssl_transform_init(ssl->transform_negotiate);
3862 ssl_handshake_params_init(ssl->handshake);
Paul Bakker968afaa2014-07-09 11:09:24 +02003863
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003864#if defined(MBEDTLS_SSL_PROTO_DTLS)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003865 if (ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM) {
Manuel Pégourié-Gonnard06939ce2015-05-11 11:25:46 +02003866 ssl->handshake->alt_transform_out = ssl->transform_out;
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003867
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003868 if (ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT) {
Manuel Pégourié-Gonnard06939ce2015-05-11 11:25:46 +02003869 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_PREPARING;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003870 } else {
Manuel Pégourié-Gonnard06939ce2015-05-11 11:25:46 +02003871 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_WAITING;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003872 }
Manuel Pégourié-Gonnard286a1362015-05-13 16:22:05 +02003873
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003874 mbedtls_ssl_set_timer(ssl, 0);
Manuel Pégourié-Gonnard06939ce2015-05-11 11:25:46 +02003875 }
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003876#endif
3877
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003878 return 0;
Paul Bakker48916f92012-09-16 19:57:18 +00003879}
3880
Manuel Pégourié-Gonnarde057d3b2015-05-20 10:59:43 +02003881#if defined(MBEDTLS_SSL_DTLS_HELLO_VERIFY) && defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard7d38d212014-07-23 17:52:09 +02003882/* Dummy cookie callbacks for defaults */
Manuel Pégourié-Gonnardd904d662022-06-17 10:24:00 +02003883MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003884static int ssl_cookie_write_dummy(void *ctx,
3885 unsigned char **p, unsigned char *end,
3886 const unsigned char *cli_id, size_t cli_id_len)
Manuel Pégourié-Gonnard7d38d212014-07-23 17:52:09 +02003887{
3888 ((void) ctx);
3889 ((void) p);
3890 ((void) end);
3891 ((void) cli_id);
3892 ((void) cli_id_len);
3893
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003894 return MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
Manuel Pégourié-Gonnard7d38d212014-07-23 17:52:09 +02003895}
3896
Manuel Pégourié-Gonnardd904d662022-06-17 10:24:00 +02003897MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003898static int ssl_cookie_check_dummy(void *ctx,
3899 const unsigned char *cookie, size_t cookie_len,
3900 const unsigned char *cli_id, size_t cli_id_len)
Manuel Pégourié-Gonnard7d38d212014-07-23 17:52:09 +02003901{
3902 ((void) ctx);
3903 ((void) cookie);
3904 ((void) cookie_len);
3905 ((void) cli_id);
3906 ((void) cli_id_len);
3907
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003908 return MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
Manuel Pégourié-Gonnard7d38d212014-07-23 17:52:09 +02003909}
Manuel Pégourié-Gonnarde057d3b2015-05-20 10:59:43 +02003910#endif /* MBEDTLS_SSL_DTLS_HELLO_VERIFY && MBEDTLS_SSL_SRV_C */
Manuel Pégourié-Gonnard7d38d212014-07-23 17:52:09 +02003911
Paul Bakker5121ce52009-01-03 21:22:43 +00003912/*
3913 * Initialize an SSL context
3914 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003915void mbedtls_ssl_init(mbedtls_ssl_context *ssl)
Manuel Pégourié-Gonnard41d479e2015-04-29 00:48:22 +02003916{
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003917 memset(ssl, 0, sizeof(mbedtls_ssl_context));
Manuel Pégourié-Gonnard41d479e2015-04-29 00:48:22 +02003918}
3919
3920/*
3921 * Setup an SSL context
3922 */
Hanno Becker2a43f6f2018-08-10 11:12:52 +01003923
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003924int mbedtls_ssl_setup(mbedtls_ssl_context *ssl,
3925 const mbedtls_ssl_config *conf)
Paul Bakker5121ce52009-01-03 21:22:43 +00003926{
Janos Follath865b3eb2019-12-16 11:46:15 +00003927 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Darryl Greenb33cc762019-11-28 14:29:44 +00003928 size_t in_buf_len = MBEDTLS_SSL_IN_BUFFER_LEN;
3929 size_t out_buf_len = MBEDTLS_SSL_OUT_BUFFER_LEN;
Paul Bakker5121ce52009-01-03 21:22:43 +00003930
Manuel Pégourié-Gonnarddef0bbe2015-05-04 14:56:36 +02003931 ssl->conf = conf;
Paul Bakker62f2dee2012-09-28 07:31:51 +00003932
3933 /*
Manuel Pégourié-Gonnard06193482014-02-14 08:39:32 +01003934 * Prepare base structures
Paul Bakker62f2dee2012-09-28 07:31:51 +00003935 */
k-stachowiakc9a5f022018-07-24 13:53:31 +02003936
3937 /* Set to NULL in case of an error condition */
3938 ssl->out_buf = NULL;
k-stachowiaka47911c2018-07-04 17:41:58 +02003939
Darryl Greenb33cc762019-11-28 14:29:44 +00003940#if defined(MBEDTLS_SSL_VARIABLE_BUFFER_LENGTH)
3941 ssl->in_buf_len = in_buf_len;
3942#endif
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003943 ssl->in_buf = mbedtls_calloc(1, in_buf_len);
3944 if (ssl->in_buf == NULL) {
3945 MBEDTLS_SSL_DEBUG_MSG(1, ("alloc(%" MBEDTLS_PRINTF_SIZET " bytes) failed", in_buf_len));
k-stachowiak9f7798e2018-07-31 16:52:32 +02003946 ret = MBEDTLS_ERR_SSL_ALLOC_FAILED;
k-stachowiaka47911c2018-07-04 17:41:58 +02003947 goto error;
Angus Grattond8213d02016-05-25 20:56:48 +10003948 }
3949
Darryl Greenb33cc762019-11-28 14:29:44 +00003950#if defined(MBEDTLS_SSL_VARIABLE_BUFFER_LENGTH)
3951 ssl->out_buf_len = out_buf_len;
3952#endif
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003953 ssl->out_buf = mbedtls_calloc(1, out_buf_len);
3954 if (ssl->out_buf == NULL) {
3955 MBEDTLS_SSL_DEBUG_MSG(1, ("alloc(%" MBEDTLS_PRINTF_SIZET " bytes) failed", out_buf_len));
k-stachowiak9f7798e2018-07-31 16:52:32 +02003956 ret = MBEDTLS_ERR_SSL_ALLOC_FAILED;
k-stachowiaka47911c2018-07-04 17:41:58 +02003957 goto error;
Paul Bakker5121ce52009-01-03 21:22:43 +00003958 }
3959
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003960 mbedtls_ssl_reset_in_out_pointers(ssl);
Manuel Pégourié-Gonnard419d5ae2015-05-04 19:32:36 +02003961
Johan Pascalb62bb512015-12-03 21:56:45 +01003962#if defined(MBEDTLS_SSL_DTLS_SRTP)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003963 memset(&ssl->dtls_srtp_info, 0, sizeof(ssl->dtls_srtp_info));
Johan Pascalb62bb512015-12-03 21:56:45 +01003964#endif
3965
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003966 if ((ret = ssl_handshake_init(ssl)) != 0) {
k-stachowiaka47911c2018-07-04 17:41:58 +02003967 goto error;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003968 }
Paul Bakker5121ce52009-01-03 21:22:43 +00003969
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003970 return 0;
k-stachowiaka47911c2018-07-04 17:41:58 +02003971
3972error:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003973 mbedtls_free(ssl->in_buf);
3974 mbedtls_free(ssl->out_buf);
k-stachowiaka47911c2018-07-04 17:41:58 +02003975
3976 ssl->conf = NULL;
3977
Darryl Greenb33cc762019-11-28 14:29:44 +00003978#if defined(MBEDTLS_SSL_VARIABLE_BUFFER_LENGTH)
3979 ssl->in_buf_len = 0;
3980 ssl->out_buf_len = 0;
3981#endif
k-stachowiaka47911c2018-07-04 17:41:58 +02003982 ssl->in_buf = NULL;
3983 ssl->out_buf = NULL;
3984
3985 ssl->in_hdr = NULL;
3986 ssl->in_ctr = NULL;
3987 ssl->in_len = NULL;
3988 ssl->in_iv = NULL;
3989 ssl->in_msg = NULL;
3990
3991 ssl->out_hdr = NULL;
3992 ssl->out_ctr = NULL;
3993 ssl->out_len = NULL;
3994 ssl->out_iv = NULL;
3995 ssl->out_msg = NULL;
3996
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003997 return ret;
Paul Bakker5121ce52009-01-03 21:22:43 +00003998}
3999
4000/*
Paul Bakker7eb013f2011-10-06 12:37:39 +00004001 * Reset an initialized and used SSL context for re-use while retaining
4002 * all application-set variables, function pointers and data.
Manuel Pégourié-Gonnard3f09b6d2015-09-08 11:58:14 +02004003 *
4004 * If partial is non-zero, keep data in the input buffer and client ID.
4005 * (Use when a DTLS client reconnects from the same port.)
Paul Bakker7eb013f2011-10-06 12:37:39 +00004006 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004007int mbedtls_ssl_session_reset_int(mbedtls_ssl_context *ssl, int partial)
Paul Bakker7eb013f2011-10-06 12:37:39 +00004008{
Janos Follath865b3eb2019-12-16 11:46:15 +00004009 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Darryl Greenb33cc762019-11-28 14:29:44 +00004010#if defined(MBEDTLS_SSL_VARIABLE_BUFFER_LENGTH)
4011 size_t in_buf_len = ssl->in_buf_len;
4012 size_t out_buf_len = ssl->out_buf_len;
4013#else
4014 size_t in_buf_len = MBEDTLS_SSL_IN_BUFFER_LEN;
4015 size_t out_buf_len = MBEDTLS_SSL_OUT_BUFFER_LEN;
4016#endif
Paul Bakker48916f92012-09-16 19:57:18 +00004017
Hanno Becker7e772132018-08-10 12:38:21 +01004018#if !defined(MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE) || \
4019 !defined(MBEDTLS_SSL_SRV_C)
4020 ((void) partial);
4021#endif
4022
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004023 ssl->state = MBEDTLS_SSL_HELLO_REQUEST;
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01004024
Manuel Pégourié-Gonnard286a1362015-05-13 16:22:05 +02004025 /* Cancel any possibly running timer */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004026 mbedtls_ssl_set_timer(ssl, 0);
Manuel Pégourié-Gonnard286a1362015-05-13 16:22:05 +02004027
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004028#if defined(MBEDTLS_SSL_RENEGOTIATION)
4029 ssl->renego_status = MBEDTLS_SSL_INITIAL_HANDSHAKE;
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01004030 ssl->renego_records_seen = 0;
Paul Bakker48916f92012-09-16 19:57:18 +00004031
4032 ssl->verify_data_len = 0;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004033 memset(ssl->own_verify_data, 0, MBEDTLS_SSL_VERIFY_DATA_MAX_LEN);
4034 memset(ssl->peer_verify_data, 0, MBEDTLS_SSL_VERIFY_DATA_MAX_LEN);
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01004035#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004036 ssl->secure_renegotiation = MBEDTLS_SSL_LEGACY_RENEGOTIATION;
Paul Bakker48916f92012-09-16 19:57:18 +00004037
Paul Bakker7eb013f2011-10-06 12:37:39 +00004038 ssl->in_offt = NULL;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004039 mbedtls_ssl_reset_in_out_pointers(ssl);
Paul Bakker7eb013f2011-10-06 12:37:39 +00004040
4041 ssl->in_msgtype = 0;
4042 ssl->in_msglen = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004043#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02004044 ssl->next_record_offset = 0;
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02004045 ssl->in_epoch = 0;
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02004046#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004047#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004048 mbedtls_ssl_dtls_replay_reset(ssl);
Manuel Pégourié-Gonnardb47368a2014-09-24 13:29:58 +02004049#endif
Paul Bakker7eb013f2011-10-06 12:37:39 +00004050
4051 ssl->in_hslen = 0;
4052 ssl->nb_zero = 0;
Hanno Beckeraf0665d2017-05-24 09:16:26 +01004053
4054 ssl->keep_current_message = 0;
Paul Bakker7eb013f2011-10-06 12:37:39 +00004055
4056 ssl->out_msgtype = 0;
4057 ssl->out_msglen = 0;
4058 ssl->out_left = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004059#if defined(MBEDTLS_SSL_CBC_RECORD_SPLITTING)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004060 if (ssl->split_done != MBEDTLS_SSL_CBC_RECORD_SPLITTING_DISABLED) {
Manuel Pégourié-Gonnardcfa477e2015-01-07 14:50:54 +01004061 ssl->split_done = 0;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004062 }
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +01004063#endif
Paul Bakker7eb013f2011-10-06 12:37:39 +00004064
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004065 memset(ssl->cur_out_ctr, 0, sizeof(ssl->cur_out_ctr));
Hanno Becker19859472018-08-06 09:40:20 +01004066
Paul Bakker48916f92012-09-16 19:57:18 +00004067 ssl->transform_in = NULL;
4068 ssl->transform_out = NULL;
Paul Bakker7eb013f2011-10-06 12:37:39 +00004069
Hanno Becker78640902018-08-13 16:35:15 +01004070 ssl->session_in = NULL;
4071 ssl->session_out = NULL;
4072
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004073 memset(ssl->out_buf, 0, out_buf_len);
Hanno Becker4ccbf062018-08-10 11:20:38 +01004074
David Horstmannd4f22082022-10-26 18:25:14 +01004075 int clear_in_buf = 1;
Hanno Becker4ccbf062018-08-10 11:20:38 +01004076#if defined(MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE) && defined(MBEDTLS_SSL_SRV_C)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004077 if (partial != 0) {
David Horstmannd4f22082022-10-26 18:25:14 +01004078 clear_in_buf = 0;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004079 }
Hanno Becker4ccbf062018-08-10 11:20:38 +01004080#endif /* MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE && MBEDTLS_SSL_SRV_C */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004081 if (clear_in_buf) {
Hanno Becker4ccbf062018-08-10 11:20:38 +01004082 ssl->in_left = 0;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004083 memset(ssl->in_buf, 0, in_buf_len);
Hanno Becker4ccbf062018-08-10 11:20:38 +01004084 }
Paul Bakker05ef8352012-05-08 09:17:57 +00004085
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004086#if defined(MBEDTLS_SSL_HW_RECORD_ACCEL)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004087 if (mbedtls_ssl_hw_record_reset != NULL) {
4088 MBEDTLS_SSL_DEBUG_MSG(2, ("going for mbedtls_ssl_hw_record_reset()"));
4089 if ((ret = mbedtls_ssl_hw_record_reset(ssl)) != 0) {
4090 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ssl_hw_record_reset", ret);
4091 return MBEDTLS_ERR_SSL_HW_ACCEL_FAILED;
Paul Bakker2770fbd2012-07-03 13:30:23 +00004092 }
Paul Bakker05ef8352012-05-08 09:17:57 +00004093 }
4094#endif
Paul Bakker2770fbd2012-07-03 13:30:23 +00004095
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004096 if (ssl->transform) {
4097 mbedtls_ssl_transform_free(ssl->transform);
4098 mbedtls_free(ssl->transform);
Paul Bakker48916f92012-09-16 19:57:18 +00004099 ssl->transform = NULL;
Paul Bakker2770fbd2012-07-03 13:30:23 +00004100 }
Paul Bakker48916f92012-09-16 19:57:18 +00004101
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004102 if (ssl->session) {
4103 mbedtls_ssl_session_free(ssl->session);
4104 mbedtls_free(ssl->session);
Paul Bakkerc0463502013-02-14 11:19:38 +01004105 ssl->session = NULL;
4106 }
4107
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004108#if defined(MBEDTLS_SSL_ALPN)
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02004109 ssl->alpn_chosen = NULL;
4110#endif
4111
Manuel Pégourié-Gonnarde057d3b2015-05-20 10:59:43 +02004112#if defined(MBEDTLS_SSL_DTLS_HELLO_VERIFY) && defined(MBEDTLS_SSL_SRV_C)
David Horstmannb5b1ed22022-10-27 13:21:49 +01004113 int free_cli_id = 1;
Hanno Becker4ccbf062018-08-10 11:20:38 +01004114#if defined(MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004115 if (partial != 0) {
David Horstmannd4f22082022-10-26 18:25:14 +01004116 free_cli_id = 0;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004117 }
Hanno Becker4ccbf062018-08-10 11:20:38 +01004118#endif
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004119 if (free_cli_id) {
4120 mbedtls_free(ssl->cli_id);
Manuel Pégourié-Gonnard3f09b6d2015-09-08 11:58:14 +02004121 ssl->cli_id = NULL;
4122 ssl->cli_id_len = 0;
4123 }
Manuel Pégourié-Gonnard43c02182014-07-22 17:32:01 +02004124#endif
4125
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004126 if ((ret = ssl_handshake_init(ssl)) != 0) {
4127 return ret;
4128 }
Paul Bakker2770fbd2012-07-03 13:30:23 +00004129
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004130 return 0;
Paul Bakker7eb013f2011-10-06 12:37:39 +00004131}
4132
Manuel Pégourié-Gonnard779e4292013-08-03 13:50:48 +02004133/*
Manuel Pégourié-Gonnard3f09b6d2015-09-08 11:58:14 +02004134 * Reset an initialized and used SSL context for re-use while retaining
4135 * all application-set variables, function pointers and data.
4136 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004137int mbedtls_ssl_session_reset(mbedtls_ssl_context *ssl)
Manuel Pégourié-Gonnard3f09b6d2015-09-08 11:58:14 +02004138{
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004139 return mbedtls_ssl_session_reset_int(ssl, 0);
Manuel Pégourié-Gonnard3f09b6d2015-09-08 11:58:14 +02004140}
4141
4142/*
Paul Bakker5121ce52009-01-03 21:22:43 +00004143 * SSL set accessors
4144 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004145void mbedtls_ssl_conf_endpoint(mbedtls_ssl_config *conf, int endpoint)
Paul Bakker5121ce52009-01-03 21:22:43 +00004146{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02004147 conf->endpoint = endpoint;
Paul Bakker5121ce52009-01-03 21:22:43 +00004148}
4149
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004150void mbedtls_ssl_conf_transport(mbedtls_ssl_config *conf, int transport)
Manuel Pégourié-Gonnard0b1ff292014-02-06 13:04:16 +01004151{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02004152 conf->transport = transport;
Manuel Pégourié-Gonnard0b1ff292014-02-06 13:04:16 +01004153}
4154
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004155#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004156void mbedtls_ssl_conf_dtls_anti_replay(mbedtls_ssl_config *conf, char mode)
Manuel Pégourié-Gonnard27393132014-09-24 14:41:11 +02004157{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02004158 conf->anti_replay = mode;
Manuel Pégourié-Gonnard27393132014-09-24 14:41:11 +02004159}
4160#endif
4161
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004162#if defined(MBEDTLS_SSL_DTLS_BADMAC_LIMIT)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004163void mbedtls_ssl_conf_dtls_badmac_limit(mbedtls_ssl_config *conf, unsigned limit)
Manuel Pégourié-Gonnardb0643d12014-10-14 18:30:36 +02004164{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02004165 conf->badmac_limit = limit;
Manuel Pégourié-Gonnardb0643d12014-10-14 18:30:36 +02004166}
4167#endif
4168
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004169#if defined(MBEDTLS_SSL_PROTO_DTLS)
Hanno Becker04da1892018-08-14 13:22:10 +01004170
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004171void mbedtls_ssl_set_datagram_packing(mbedtls_ssl_context *ssl,
4172 unsigned allow_packing)
Hanno Becker04da1892018-08-14 13:22:10 +01004173{
4174 ssl->disable_datagram_packing = !allow_packing;
4175}
4176
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004177void mbedtls_ssl_conf_handshake_timeout(mbedtls_ssl_config *conf,
4178 uint32_t min, uint32_t max)
Manuel Pégourié-Gonnard905dd242014-10-01 12:03:55 +02004179{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02004180 conf->hs_timeout_min = min;
4181 conf->hs_timeout_max = max;
Manuel Pégourié-Gonnard905dd242014-10-01 12:03:55 +02004182}
4183#endif
4184
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004185void mbedtls_ssl_conf_authmode(mbedtls_ssl_config *conf, int authmode)
Paul Bakker5121ce52009-01-03 21:22:43 +00004186{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02004187 conf->authmode = authmode;
Paul Bakker5121ce52009-01-03 21:22:43 +00004188}
4189
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004190#if defined(MBEDTLS_X509_CRT_PARSE_C)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004191void mbedtls_ssl_conf_verify(mbedtls_ssl_config *conf,
4192 int (*f_vrfy)(void *, mbedtls_x509_crt *, int, uint32_t *),
4193 void *p_vrfy)
Paul Bakkerb63b0af2011-01-13 17:54:59 +00004194{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02004195 conf->f_vrfy = f_vrfy;
4196 conf->p_vrfy = p_vrfy;
Paul Bakkerb63b0af2011-01-13 17:54:59 +00004197}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004198#endif /* MBEDTLS_X509_CRT_PARSE_C */
Paul Bakkerb63b0af2011-01-13 17:54:59 +00004199
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004200void mbedtls_ssl_conf_rng(mbedtls_ssl_config *conf,
4201 int (*f_rng)(void *, unsigned char *, size_t),
4202 void *p_rng)
Paul Bakker5121ce52009-01-03 21:22:43 +00004203{
Manuel Pégourié-Gonnard750e4d72015-05-07 12:35:38 +01004204 conf->f_rng = f_rng;
4205 conf->p_rng = p_rng;
Paul Bakker5121ce52009-01-03 21:22:43 +00004206}
4207
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004208void mbedtls_ssl_conf_dbg(mbedtls_ssl_config *conf,
4209 void (*f_dbg)(void *, int, const char *, int, const char *),
4210 void *p_dbg)
Paul Bakker5121ce52009-01-03 21:22:43 +00004211{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02004212 conf->f_dbg = f_dbg;
4213 conf->p_dbg = p_dbg;
Paul Bakker5121ce52009-01-03 21:22:43 +00004214}
4215
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004216void mbedtls_ssl_set_bio(mbedtls_ssl_context *ssl,
4217 void *p_bio,
4218 mbedtls_ssl_send_t *f_send,
4219 mbedtls_ssl_recv_t *f_recv,
4220 mbedtls_ssl_recv_timeout_t *f_recv_timeout)
Manuel Pégourié-Gonnard8fa6dfd2014-09-17 10:47:43 +02004221{
4222 ssl->p_bio = p_bio;
4223 ssl->f_send = f_send;
4224 ssl->f_recv = f_recv;
4225 ssl->f_recv_timeout = f_recv_timeout;
Manuel Pégourié-Gonnard97fd52c2015-05-06 15:38:52 +01004226}
4227
Manuel Pégourié-Gonnard6e7aaca2018-08-20 10:37:23 +02004228#if defined(MBEDTLS_SSL_PROTO_DTLS)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004229void mbedtls_ssl_set_mtu(mbedtls_ssl_context *ssl, uint16_t mtu)
Manuel Pégourié-Gonnard6e7aaca2018-08-20 10:37:23 +02004230{
4231 ssl->mtu = mtu;
4232}
4233#endif
4234
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004235void mbedtls_ssl_conf_read_timeout(mbedtls_ssl_config *conf, uint32_t timeout)
Manuel Pégourié-Gonnard97fd52c2015-05-06 15:38:52 +01004236{
4237 conf->read_timeout = timeout;
Manuel Pégourié-Gonnard8fa6dfd2014-09-17 10:47:43 +02004238}
4239
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004240void mbedtls_ssl_set_timer_cb(mbedtls_ssl_context *ssl,
4241 void *p_timer,
4242 mbedtls_ssl_set_timer_t *f_set_timer,
4243 mbedtls_ssl_get_timer_t *f_get_timer)
Manuel Pégourié-Gonnard2e012912015-05-12 20:55:41 +02004244{
4245 ssl->p_timer = p_timer;
4246 ssl->f_set_timer = f_set_timer;
4247 ssl->f_get_timer = f_get_timer;
Manuel Pégourié-Gonnard286a1362015-05-13 16:22:05 +02004248
4249 /* Make sure we start with no timer running */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004250 mbedtls_ssl_set_timer(ssl, 0);
Manuel Pégourié-Gonnard2e012912015-05-12 20:55:41 +02004251}
4252
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004253#if defined(MBEDTLS_SSL_SRV_C)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004254void mbedtls_ssl_conf_session_cache(mbedtls_ssl_config *conf,
4255 void *p_cache,
4256 int (*f_get_cache)(void *, mbedtls_ssl_session *),
4257 int (*f_set_cache)(void *, const mbedtls_ssl_session *))
Paul Bakker5121ce52009-01-03 21:22:43 +00004258{
Manuel Pégourié-Gonnard5cb33082015-05-06 18:06:26 +01004259 conf->p_cache = p_cache;
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02004260 conf->f_get_cache = f_get_cache;
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02004261 conf->f_set_cache = f_set_cache;
Paul Bakker5121ce52009-01-03 21:22:43 +00004262}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004263#endif /* MBEDTLS_SSL_SRV_C */
Paul Bakker5121ce52009-01-03 21:22:43 +00004264
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004265#if defined(MBEDTLS_SSL_CLI_C)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004266int mbedtls_ssl_set_session(mbedtls_ssl_context *ssl, const mbedtls_ssl_session *session)
Paul Bakker5121ce52009-01-03 21:22:43 +00004267{
Janos Follath865b3eb2019-12-16 11:46:15 +00004268 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +02004269
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004270 if (ssl == NULL ||
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +02004271 session == NULL ||
4272 ssl->session_negotiate == NULL ||
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004273 ssl->conf->endpoint != MBEDTLS_SSL_IS_CLIENT) {
4274 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +02004275 }
4276
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004277 if ((ret = mbedtls_ssl_session_copy(ssl->session_negotiate,
4278 session)) != 0) {
4279 return ret;
4280 }
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +02004281
Paul Bakker0a597072012-09-25 21:55:46 +00004282 ssl->handshake->resume = 1;
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +02004283
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004284 return 0;
Paul Bakker5121ce52009-01-03 21:22:43 +00004285}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004286#endif /* MBEDTLS_SSL_CLI_C */
Paul Bakker5121ce52009-01-03 21:22:43 +00004287
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004288void mbedtls_ssl_conf_ciphersuites(mbedtls_ssl_config *conf,
4289 const int *ciphersuites)
Paul Bakker5121ce52009-01-03 21:22:43 +00004290{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02004291 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_0] = ciphersuites;
4292 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_1] = ciphersuites;
4293 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_2] = ciphersuites;
4294 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_3] = ciphersuites;
Paul Bakker8f4ddae2013-04-15 15:09:54 +02004295}
4296
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004297void mbedtls_ssl_conf_ciphersuites_for_version(mbedtls_ssl_config *conf,
4298 const int *ciphersuites,
4299 int major, int minor)
Paul Bakker8f4ddae2013-04-15 15:09:54 +02004300{
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004301 if (major != MBEDTLS_SSL_MAJOR_VERSION_3) {
Paul Bakker8f4ddae2013-04-15 15:09:54 +02004302 return;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004303 }
Paul Bakker8f4ddae2013-04-15 15:09:54 +02004304
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004305 if (minor < MBEDTLS_SSL_MINOR_VERSION_0 || minor > MBEDTLS_SSL_MINOR_VERSION_3) {
Paul Bakker8f4ddae2013-04-15 15:09:54 +02004306 return;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004307 }
Paul Bakker8f4ddae2013-04-15 15:09:54 +02004308
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02004309 conf->ciphersuite_list[minor] = ciphersuites;
Paul Bakker5121ce52009-01-03 21:22:43 +00004310}
4311
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004312#if defined(MBEDTLS_X509_CRT_PARSE_C)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004313void mbedtls_ssl_conf_cert_profile(mbedtls_ssl_config *conf,
4314 const mbedtls_x509_crt_profile *profile)
Manuel Pégourié-Gonnard6e3ee3a2015-06-17 10:58:20 +02004315{
4316 conf->cert_profile = profile;
4317}
4318
Manuel Pégourié-Gonnard8f618a82015-05-10 21:13:36 +02004319/* Append a new keycert entry to a (possibly empty) list */
Manuel Pégourié-Gonnardd904d662022-06-17 10:24:00 +02004320MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004321static int ssl_append_key_cert(mbedtls_ssl_key_cert **head,
4322 mbedtls_x509_crt *cert,
4323 mbedtls_pk_context *key)
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02004324{
niisato8ee24222018-06-25 19:05:48 +09004325 mbedtls_ssl_key_cert *new_cert;
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02004326
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004327 new_cert = mbedtls_calloc(1, sizeof(mbedtls_ssl_key_cert));
4328 if (new_cert == NULL) {
4329 return MBEDTLS_ERR_SSL_ALLOC_FAILED;
4330 }
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02004331
niisato8ee24222018-06-25 19:05:48 +09004332 new_cert->cert = cert;
4333 new_cert->key = key;
4334 new_cert->next = NULL;
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02004335
Manuel Pégourié-Gonnard8f618a82015-05-10 21:13:36 +02004336 /* Update head is the list was null, else add to the end */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004337 if (*head == NULL) {
niisato8ee24222018-06-25 19:05:48 +09004338 *head = new_cert;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004339 } else {
Manuel Pégourié-Gonnard8f618a82015-05-10 21:13:36 +02004340 mbedtls_ssl_key_cert *cur = *head;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004341 while (cur->next != NULL) {
Manuel Pégourié-Gonnard8f618a82015-05-10 21:13:36 +02004342 cur = cur->next;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004343 }
niisato8ee24222018-06-25 19:05:48 +09004344 cur->next = new_cert;
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02004345 }
4346
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004347 return 0;
Manuel Pégourié-Gonnard8f618a82015-05-10 21:13:36 +02004348}
4349
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004350int mbedtls_ssl_conf_own_cert(mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnard8f618a82015-05-10 21:13:36 +02004351 mbedtls_x509_crt *own_cert,
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004352 mbedtls_pk_context *pk_key)
Manuel Pégourié-Gonnard8f618a82015-05-10 21:13:36 +02004353{
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004354 return ssl_append_key_cert(&conf->key_cert, own_cert, pk_key);
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02004355}
4356
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004357void mbedtls_ssl_conf_ca_chain(mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnardbc2b7712015-05-06 11:14:19 +01004358 mbedtls_x509_crt *ca_chain,
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004359 mbedtls_x509_crl *ca_crl)
Paul Bakker5121ce52009-01-03 21:22:43 +00004360{
Manuel Pégourié-Gonnardbc2b7712015-05-06 11:14:19 +01004361 conf->ca_chain = ca_chain;
4362 conf->ca_crl = ca_crl;
Hanno Becker5adaad92019-03-27 16:54:37 +00004363
4364#if defined(MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK)
4365 /* mbedtls_ssl_conf_ca_chain() and mbedtls_ssl_conf_ca_cb()
4366 * cannot be used together. */
4367 conf->f_ca_cb = NULL;
4368 conf->p_ca_cb = NULL;
4369#endif /* MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK */
Paul Bakker5121ce52009-01-03 21:22:43 +00004370}
Hanno Becker5adaad92019-03-27 16:54:37 +00004371
4372#if defined(MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004373void mbedtls_ssl_conf_ca_cb(mbedtls_ssl_config *conf,
4374 mbedtls_x509_crt_ca_cb_t f_ca_cb,
4375 void *p_ca_cb)
Hanno Becker5adaad92019-03-27 16:54:37 +00004376{
4377 conf->f_ca_cb = f_ca_cb;
4378 conf->p_ca_cb = p_ca_cb;
4379
4380 /* mbedtls_ssl_conf_ca_chain() and mbedtls_ssl_conf_ca_cb()
4381 * cannot be used together. */
4382 conf->ca_chain = NULL;
4383 conf->ca_crl = NULL;
4384}
4385#endif /* MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004386#endif /* MBEDTLS_X509_CRT_PARSE_C */
Paul Bakkereb2c6582012-09-27 19:15:01 +00004387
Manuel Pégourié-Gonnard1af6c852015-05-10 23:10:37 +02004388#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004389int mbedtls_ssl_set_hs_own_cert(mbedtls_ssl_context *ssl,
4390 mbedtls_x509_crt *own_cert,
4391 mbedtls_pk_context *pk_key)
Manuel Pégourié-Gonnard1af6c852015-05-10 23:10:37 +02004392{
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004393 return ssl_append_key_cert(&ssl->handshake->sni_key_cert,
4394 own_cert, pk_key);
Manuel Pégourié-Gonnard1af6c852015-05-10 23:10:37 +02004395}
Manuel Pégourié-Gonnard22bfa4b2015-05-11 08:46:37 +02004396
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004397void mbedtls_ssl_set_hs_ca_chain(mbedtls_ssl_context *ssl,
4398 mbedtls_x509_crt *ca_chain,
4399 mbedtls_x509_crl *ca_crl)
Manuel Pégourié-Gonnard22bfa4b2015-05-11 08:46:37 +02004400{
4401 ssl->handshake->sni_ca_chain = ca_chain;
4402 ssl->handshake->sni_ca_crl = ca_crl;
4403}
Manuel Pégourié-Gonnardcdc26ae2015-06-19 12:16:31 +02004404
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004405void mbedtls_ssl_set_hs_authmode(mbedtls_ssl_context *ssl,
4406 int authmode)
Manuel Pégourié-Gonnardcdc26ae2015-06-19 12:16:31 +02004407{
4408 ssl->handshake->sni_authmode = authmode;
4409}
Manuel Pégourié-Gonnard1af6c852015-05-10 23:10:37 +02004410#endif /* MBEDTLS_SSL_SERVER_NAME_INDICATION */
4411
Hanno Becker8927c832019-04-03 12:52:50 +01004412#if defined(MBEDTLS_X509_CRT_PARSE_C)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004413void mbedtls_ssl_set_verify(mbedtls_ssl_context *ssl,
4414 int (*f_vrfy)(void *, mbedtls_x509_crt *, int, uint32_t *),
4415 void *p_vrfy)
Hanno Becker8927c832019-04-03 12:52:50 +01004416{
4417 ssl->f_vrfy = f_vrfy;
4418 ssl->p_vrfy = p_vrfy;
4419}
4420#endif
4421
Manuel Pégourié-Gonnardeef142d2015-09-16 10:05:04 +02004422#if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
Manuel Pégourié-Gonnard7002f4a2015-09-15 12:43:43 +02004423/*
4424 * Set EC J-PAKE password for current handshake
4425 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004426int mbedtls_ssl_set_hs_ecjpake_password(mbedtls_ssl_context *ssl,
4427 const unsigned char *pw,
4428 size_t pw_len)
Manuel Pégourié-Gonnard7002f4a2015-09-15 12:43:43 +02004429{
4430 mbedtls_ecjpake_role role;
4431
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004432 if (ssl->handshake == NULL || ssl->conf == NULL) {
4433 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
4434 }
Manuel Pégourié-Gonnard7002f4a2015-09-15 12:43:43 +02004435
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004436 if (ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER) {
Manuel Pégourié-Gonnard7002f4a2015-09-15 12:43:43 +02004437 role = MBEDTLS_ECJPAKE_SERVER;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004438 } else {
Manuel Pégourié-Gonnard7002f4a2015-09-15 12:43:43 +02004439 role = MBEDTLS_ECJPAKE_CLIENT;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004440 }
Manuel Pégourié-Gonnard7002f4a2015-09-15 12:43:43 +02004441
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004442 return mbedtls_ecjpake_setup(&ssl->handshake->ecjpake_ctx,
4443 role,
4444 MBEDTLS_MD_SHA256,
4445 MBEDTLS_ECP_DP_SECP256R1,
4446 pw, pw_len);
Manuel Pégourié-Gonnard7002f4a2015-09-15 12:43:43 +02004447}
Manuel Pégourié-Gonnardeef142d2015-09-16 10:05:04 +02004448#endif /* MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED */
Manuel Pégourié-Gonnard7002f4a2015-09-15 12:43:43 +02004449
Gilles Peskineeccd8882020-03-10 12:19:08 +01004450#if defined(MBEDTLS_KEY_EXCHANGE_SOME_PSK_ENABLED)
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01004451
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004452static void ssl_conf_remove_psk(mbedtls_ssl_config *conf)
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01004453{
4454 /* Remove reference to existing PSK, if any. */
4455#if defined(MBEDTLS_USE_PSA_CRYPTO)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004456 if (!mbedtls_svc_key_id_is_null(conf->psk_opaque)) {
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01004457 /* The maintenance of the PSK key slot is the
4458 * user's responsibility. */
Ronald Croncf56a0a2020-08-04 09:51:30 +02004459 conf->psk_opaque = MBEDTLS_SVC_KEY_ID_INIT;
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01004460 }
Hanno Beckera63ac3f2018-11-05 12:47:16 +00004461 /* This and the following branch should never
Tom Cosgrove49f99bc2022-12-04 16:44:21 +00004462 * be taken simultaneously as we maintain the
Hanno Beckera63ac3f2018-11-05 12:47:16 +00004463 * invariant that raw and opaque PSKs are never
4464 * configured simultaneously. As a safeguard,
4465 * though, `else` is omitted here. */
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01004466#endif /* MBEDTLS_USE_PSA_CRYPTO */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004467 if (conf->psk != NULL) {
4468 mbedtls_platform_zeroize(conf->psk, conf->psk_len);
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01004469
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004470 mbedtls_free(conf->psk);
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01004471 conf->psk = NULL;
4472 conf->psk_len = 0;
4473 }
4474
4475 /* Remove reference to PSK identity, if any. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004476 if (conf->psk_identity != NULL) {
4477 mbedtls_free(conf->psk_identity);
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01004478 conf->psk_identity = NULL;
4479 conf->psk_identity_len = 0;
4480 }
4481}
4482
Hanno Becker7390c712018-11-15 13:33:04 +00004483/* This function assumes that PSK identity in the SSL config is unset.
4484 * It checks that the provided identity is well-formed and attempts
4485 * to make a copy of it in the SSL config.
4486 * On failure, the PSK identity in the config remains unset. */
Manuel Pégourié-Gonnardd904d662022-06-17 10:24:00 +02004487MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004488static int ssl_conf_set_psk_identity(mbedtls_ssl_config *conf,
4489 unsigned char const *psk_identity,
4490 size_t psk_identity_len)
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02004491{
Manuel Pégourié-Gonnardc6b5d832015-08-27 16:37:35 +02004492 /* Identity len will be encoded on two bytes */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004493 if (psk_identity == NULL ||
4494 (psk_identity_len >> 16) != 0 ||
4495 psk_identity_len > MBEDTLS_SSL_OUT_CONTENT_LEN) {
4496 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
Manuel Pégourié-Gonnardc6b5d832015-08-27 16:37:35 +02004497 }
4498
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004499 conf->psk_identity = mbedtls_calloc(1, psk_identity_len);
4500 if (conf->psk_identity == NULL) {
4501 return MBEDTLS_ERR_SSL_ALLOC_FAILED;
4502 }
Paul Bakker6db455e2013-09-18 17:29:31 +02004503
Manuel Pégourié-Gonnard120fdbd2015-05-07 17:07:50 +01004504 conf->psk_identity_len = psk_identity_len;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004505 memcpy(conf->psk_identity, psk_identity, conf->psk_identity_len);
Paul Bakker5ad403f2013-09-18 21:21:30 +02004506
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004507 return 0;
Paul Bakker6db455e2013-09-18 17:29:31 +02004508}
4509
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004510int mbedtls_ssl_conf_psk(mbedtls_ssl_config *conf,
4511 const unsigned char *psk, size_t psk_len,
4512 const unsigned char *psk_identity, size_t psk_identity_len)
Hanno Becker7390c712018-11-15 13:33:04 +00004513{
Janos Follath865b3eb2019-12-16 11:46:15 +00004514 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Hanno Becker7390c712018-11-15 13:33:04 +00004515 /* Remove opaque/raw PSK + PSK Identity */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004516 ssl_conf_remove_psk(conf);
Hanno Becker7390c712018-11-15 13:33:04 +00004517
4518 /* Check and set raw PSK */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004519 if (psk == NULL) {
4520 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
4521 }
4522 if (psk_len == 0) {
4523 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
4524 }
4525 if (psk_len > MBEDTLS_PSK_MAX_LEN) {
4526 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
4527 }
Piotr Nowicki9926eaf2019-11-20 14:54:36 +01004528
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004529 if ((conf->psk = mbedtls_calloc(1, psk_len)) == NULL) {
4530 return MBEDTLS_ERR_SSL_ALLOC_FAILED;
4531 }
Hanno Becker7390c712018-11-15 13:33:04 +00004532 conf->psk_len = psk_len;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004533 memcpy(conf->psk, psk, conf->psk_len);
Hanno Becker7390c712018-11-15 13:33:04 +00004534
4535 /* Check and set PSK Identity */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004536 ret = ssl_conf_set_psk_identity(conf, psk_identity, psk_identity_len);
4537 if (ret != 0) {
4538 ssl_conf_remove_psk(conf);
4539 }
Hanno Becker7390c712018-11-15 13:33:04 +00004540
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004541 return ret;
Hanno Becker7390c712018-11-15 13:33:04 +00004542}
4543
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004544static void ssl_remove_psk(mbedtls_ssl_context *ssl)
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01004545{
4546#if defined(MBEDTLS_USE_PSA_CRYPTO)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004547 if (!mbedtls_svc_key_id_is_null(ssl->handshake->psk_opaque)) {
Ronald Croncf56a0a2020-08-04 09:51:30 +02004548 ssl->handshake->psk_opaque = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004549 } else
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01004550#endif /* MBEDTLS_USE_PSA_CRYPTO */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004551 if (ssl->handshake->psk != NULL) {
4552 mbedtls_platform_zeroize(ssl->handshake->psk,
4553 ssl->handshake->psk_len);
4554 mbedtls_free(ssl->handshake->psk);
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01004555 ssl->handshake->psk_len = 0;
lhuang040a2dd6d2024-06-11 12:31:17 -07004556 ssl->handshake->psk = NULL;
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01004557 }
4558}
4559
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004560int mbedtls_ssl_set_hs_psk(mbedtls_ssl_context *ssl,
4561 const unsigned char *psk, size_t psk_len)
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01004562{
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004563 if (psk == NULL || ssl->handshake == NULL) {
4564 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
4565 }
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01004566
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004567 if (psk_len > MBEDTLS_PSK_MAX_LEN) {
4568 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
4569 }
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01004570
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004571 ssl_remove_psk(ssl);
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01004572
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004573 if ((ssl->handshake->psk = mbedtls_calloc(1, psk_len)) == NULL) {
4574 return MBEDTLS_ERR_SSL_ALLOC_FAILED;
4575 }
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01004576
4577 ssl->handshake->psk_len = psk_len;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004578 memcpy(ssl->handshake->psk, psk, ssl->handshake->psk_len);
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01004579
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004580 return 0;
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01004581}
4582
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01004583#if defined(MBEDTLS_USE_PSA_CRYPTO)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004584int mbedtls_ssl_conf_psk_opaque(mbedtls_ssl_config *conf,
4585 psa_key_id_t psk,
4586 const unsigned char *psk_identity,
4587 size_t psk_identity_len)
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01004588{
Janos Follath865b3eb2019-12-16 11:46:15 +00004589 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Hanno Becker7390c712018-11-15 13:33:04 +00004590 /* Clear opaque/raw PSK + PSK Identity, if present. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004591 ssl_conf_remove_psk(conf);
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01004592
Hanno Becker7390c712018-11-15 13:33:04 +00004593 /* Check and set opaque PSK */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004594 if (mbedtls_svc_key_id_is_null(psk)) {
4595 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
4596 }
Ronald Croncf56a0a2020-08-04 09:51:30 +02004597 conf->psk_opaque = psk;
Hanno Becker7390c712018-11-15 13:33:04 +00004598
4599 /* Check and set PSK Identity */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004600 ret = ssl_conf_set_psk_identity(conf, psk_identity,
4601 psk_identity_len);
4602 if (ret != 0) {
4603 ssl_conf_remove_psk(conf);
4604 }
Hanno Becker7390c712018-11-15 13:33:04 +00004605
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004606 return ret;
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01004607}
4608
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004609int mbedtls_ssl_set_hs_psk_opaque(mbedtls_ssl_context *ssl,
4610 psa_key_id_t psk)
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01004611{
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004612 if ((mbedtls_svc_key_id_is_null(psk)) ||
4613 (ssl->handshake == NULL)) {
4614 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
4615 }
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01004616
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004617 ssl_remove_psk(ssl);
Ronald Croncf56a0a2020-08-04 09:51:30 +02004618 ssl->handshake->psk_opaque = psk;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004619 return 0;
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01004620}
4621#endif /* MBEDTLS_USE_PSA_CRYPTO */
4622
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004623void mbedtls_ssl_conf_psk_cb(mbedtls_ssl_config *conf,
4624 int (*f_psk)(void *, mbedtls_ssl_context *, const unsigned char *,
4625 size_t),
4626 void *p_psk)
Paul Bakker6db455e2013-09-18 17:29:31 +02004627{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02004628 conf->f_psk = f_psk;
4629 conf->p_psk = p_psk;
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02004630}
Gilles Peskineeccd8882020-03-10 12:19:08 +01004631#endif /* MBEDTLS_KEY_EXCHANGE_SOME_PSK_ENABLED */
Paul Bakker43b7e352011-01-18 15:27:19 +00004632
Manuel Pégourié-Gonnardcf141ca2015-05-20 10:35:51 +02004633#if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_SSL_SRV_C)
Hanno Becker470a8c42017-10-04 15:28:46 +01004634
4635#if !defined(MBEDTLS_DEPRECATED_REMOVED)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004636int mbedtls_ssl_conf_dh_param(mbedtls_ssl_config *conf, const char *dhm_P, const char *dhm_G)
Paul Bakker5121ce52009-01-03 21:22:43 +00004637{
Janos Follath865b3eb2019-12-16 11:46:15 +00004638 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker5121ce52009-01-03 21:22:43 +00004639
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004640 if ((ret = mbedtls_mpi_read_string(&conf->dhm_P, 16, dhm_P)) != 0 ||
4641 (ret = mbedtls_mpi_read_string(&conf->dhm_G, 16, dhm_G)) != 0) {
4642 mbedtls_mpi_free(&conf->dhm_P);
4643 mbedtls_mpi_free(&conf->dhm_G);
4644 return ret;
Manuel Pégourié-Gonnard1028b742015-05-06 17:33:07 +01004645 }
Paul Bakker5121ce52009-01-03 21:22:43 +00004646
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004647 return 0;
Paul Bakker5121ce52009-01-03 21:22:43 +00004648}
Hanno Becker470a8c42017-10-04 15:28:46 +01004649#endif /* MBEDTLS_DEPRECATED_REMOVED */
Paul Bakker5121ce52009-01-03 21:22:43 +00004650
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004651int mbedtls_ssl_conf_dh_param_bin(mbedtls_ssl_config *conf,
4652 const unsigned char *dhm_P, size_t P_len,
4653 const unsigned char *dhm_G, size_t G_len)
Hanno Beckera90658f2017-10-04 15:29:08 +01004654{
Janos Follath865b3eb2019-12-16 11:46:15 +00004655 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Hanno Beckera90658f2017-10-04 15:29:08 +01004656
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004657 mbedtls_mpi_free(&conf->dhm_P);
4658 mbedtls_mpi_free(&conf->dhm_G);
Glenn Straussde081ce2021-12-20 01:43:17 -05004659
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004660 if ((ret = mbedtls_mpi_read_binary(&conf->dhm_P, dhm_P, P_len)) != 0 ||
4661 (ret = mbedtls_mpi_read_binary(&conf->dhm_G, dhm_G, G_len)) != 0) {
4662 mbedtls_mpi_free(&conf->dhm_P);
4663 mbedtls_mpi_free(&conf->dhm_G);
4664 return ret;
Hanno Beckera90658f2017-10-04 15:29:08 +01004665 }
4666
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004667 return 0;
Hanno Beckera90658f2017-10-04 15:29:08 +01004668}
Paul Bakker5121ce52009-01-03 21:22:43 +00004669
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004670int mbedtls_ssl_conf_dh_param_ctx(mbedtls_ssl_config *conf, mbedtls_dhm_context *dhm_ctx)
Paul Bakker1b57b062011-01-06 15:48:19 +00004671{
Janos Follath865b3eb2019-12-16 11:46:15 +00004672 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker1b57b062011-01-06 15:48:19 +00004673
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004674 mbedtls_mpi_free(&conf->dhm_P);
4675 mbedtls_mpi_free(&conf->dhm_G);
Glenn Straussde081ce2021-12-20 01:43:17 -05004676
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004677 if ((ret = mbedtls_mpi_copy(&conf->dhm_P, &dhm_ctx->P)) != 0 ||
4678 (ret = mbedtls_mpi_copy(&conf->dhm_G, &dhm_ctx->G)) != 0) {
4679 mbedtls_mpi_free(&conf->dhm_P);
4680 mbedtls_mpi_free(&conf->dhm_G);
4681 return ret;
Manuel Pégourié-Gonnard1028b742015-05-06 17:33:07 +01004682 }
Paul Bakker1b57b062011-01-06 15:48:19 +00004683
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004684 return 0;
Paul Bakker1b57b062011-01-06 15:48:19 +00004685}
Manuel Pégourié-Gonnardcf141ca2015-05-20 10:35:51 +02004686#endif /* MBEDTLS_DHM_C && MBEDTLS_SSL_SRV_C */
Paul Bakker1b57b062011-01-06 15:48:19 +00004687
Manuel Pégourié-Gonnardbd990d62015-06-11 14:49:42 +02004688#if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_SSL_CLI_C)
4689/*
4690 * Set the minimum length for Diffie-Hellman parameters
4691 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004692void mbedtls_ssl_conf_dhm_min_bitlen(mbedtls_ssl_config *conf,
4693 unsigned int bitlen)
Manuel Pégourié-Gonnardbd990d62015-06-11 14:49:42 +02004694{
4695 conf->dhm_min_bitlen = bitlen;
4696}
4697#endif /* MBEDTLS_DHM_C && MBEDTLS_SSL_CLI_C */
4698
Gilles Peskineeccd8882020-03-10 12:19:08 +01004699#if defined(MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED)
Manuel Pégourié-Gonnard36a8b572015-06-17 12:43:26 +02004700/*
4701 * Set allowed/preferred hashes for handshake signatures
4702 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004703void mbedtls_ssl_conf_sig_hashes(mbedtls_ssl_config *conf,
4704 const int *hashes)
Manuel Pégourié-Gonnard36a8b572015-06-17 12:43:26 +02004705{
4706 conf->sig_hashes = hashes;
4707}
Gilles Peskineeccd8882020-03-10 12:19:08 +01004708#endif /* MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED */
Manuel Pégourié-Gonnard36a8b572015-06-17 12:43:26 +02004709
Manuel Pégourié-Gonnardb541da62015-06-17 11:43:30 +02004710#if defined(MBEDTLS_ECP_C)
Manuel Pégourié-Gonnard7f38ed02014-02-04 15:52:33 +01004711/*
4712 * Set the allowed elliptic curves
4713 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004714void mbedtls_ssl_conf_curves(mbedtls_ssl_config *conf,
4715 const mbedtls_ecp_group_id *curve_list)
Manuel Pégourié-Gonnard7f38ed02014-02-04 15:52:33 +01004716{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02004717 conf->curve_list = curve_list;
Manuel Pégourié-Gonnard7f38ed02014-02-04 15:52:33 +01004718}
Hanno Becker947194e2017-04-07 13:25:49 +01004719#endif /* MBEDTLS_ECP_C */
Manuel Pégourié-Gonnard7f38ed02014-02-04 15:52:33 +01004720
Manuel Pégourié-Gonnardbc2b7712015-05-06 11:14:19 +01004721#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004722void mbedtls_ssl_conf_sni(mbedtls_ssl_config *conf,
4723 int (*f_sni)(void *, mbedtls_ssl_context *,
4724 const unsigned char *, size_t),
4725 void *p_sni)
Paul Bakker5701cdc2012-09-27 21:49:42 +00004726{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02004727 conf->f_sni = f_sni;
4728 conf->p_sni = p_sni;
Paul Bakker5701cdc2012-09-27 21:49:42 +00004729}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004730#endif /* MBEDTLS_SSL_SERVER_NAME_INDICATION */
Paul Bakker5701cdc2012-09-27 21:49:42 +00004731
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004732#if defined(MBEDTLS_SSL_ALPN)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004733int mbedtls_ssl_conf_alpn_protocols(mbedtls_ssl_config *conf, const char **protos)
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02004734{
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +02004735 size_t cur_len, tot_len;
4736 const char **p;
4737
4738 /*
Brian J Murray1903fb32016-11-06 04:45:15 -08004739 * RFC 7301 3.1: "Empty strings MUST NOT be included and byte strings
4740 * MUST NOT be truncated."
4741 * We check lengths now rather than later.
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +02004742 */
4743 tot_len = 0;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004744 for (p = protos; *p != NULL; p++) {
4745 cur_len = strlen(*p);
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +02004746 tot_len += cur_len;
4747
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004748 if ((cur_len == 0) ||
4749 (cur_len > MBEDTLS_SSL_MAX_ALPN_NAME_LEN) ||
4750 (tot_len > MBEDTLS_SSL_MAX_ALPN_LIST_LEN)) {
4751 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
4752 }
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +02004753 }
4754
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02004755 conf->alpn_list = protos;
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +02004756
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004757 return 0;
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02004758}
4759
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004760const char *mbedtls_ssl_get_alpn_protocol(const mbedtls_ssl_context *ssl)
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02004761{
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004762 return ssl->alpn_chosen;
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02004763}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004764#endif /* MBEDTLS_SSL_ALPN */
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02004765
Johan Pascalb62bb512015-12-03 21:56:45 +01004766#if defined(MBEDTLS_SSL_DTLS_SRTP)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004767void mbedtls_ssl_conf_srtp_mki_value_supported(mbedtls_ssl_config *conf,
4768 int support_mki_value)
Ron Eldor591f1622018-01-22 12:30:04 +02004769{
4770 conf->dtls_srtp_mki_support = support_mki_value;
4771}
4772
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004773int mbedtls_ssl_dtls_srtp_set_mki_value(mbedtls_ssl_context *ssl,
4774 unsigned char *mki_value,
4775 uint16_t mki_len)
Ron Eldor591f1622018-01-22 12:30:04 +02004776{
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004777 if (mki_len > MBEDTLS_TLS_SRTP_MAX_MKI_LENGTH) {
4778 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
Ron Eldora9788042018-12-05 11:04:31 +02004779 }
Ron Eldor591f1622018-01-22 12:30:04 +02004780
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004781 if (ssl->conf->dtls_srtp_mki_support == MBEDTLS_SSL_DTLS_SRTP_MKI_UNSUPPORTED) {
4782 return MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
Ron Eldora9788042018-12-05 11:04:31 +02004783 }
Ron Eldor591f1622018-01-22 12:30:04 +02004784
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004785 memcpy(ssl->dtls_srtp_info.mki_value, mki_value, mki_len);
Ron Eldor591f1622018-01-22 12:30:04 +02004786 ssl->dtls_srtp_info.mki_len = mki_len;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004787 return 0;
Ron Eldor591f1622018-01-22 12:30:04 +02004788}
4789
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004790int mbedtls_ssl_conf_dtls_srtp_protection_profiles(mbedtls_ssl_config *conf,
4791 const mbedtls_ssl_srtp_profile *profiles)
Johan Pascalb62bb512015-12-03 21:56:45 +01004792{
Johan Pascal253d0262020-09-22 13:04:45 +02004793 const mbedtls_ssl_srtp_profile *p;
4794 size_t list_size = 0;
Johan Pascalb62bb512015-12-03 21:56:45 +01004795
Johan Pascal253d0262020-09-22 13:04:45 +02004796 /* check the profiles list: all entry must be valid,
4797 * its size cannot be more than the total number of supported profiles, currently 4 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004798 for (p = profiles; *p != MBEDTLS_TLS_SRTP_UNSET &&
4799 list_size <= MBEDTLS_TLS_SRTP_MAX_PROFILE_LIST_LENGTH;
4800 p++) {
4801 if (mbedtls_ssl_check_srtp_profile_value(*p) != MBEDTLS_TLS_SRTP_UNSET) {
Johan Pascal76fdf1d2020-10-22 23:31:00 +02004802 list_size++;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004803 } else {
Johan Pascal76fdf1d2020-10-22 23:31:00 +02004804 /* unsupported value, stop parsing and set the size to an error value */
4805 list_size = MBEDTLS_TLS_SRTP_MAX_PROFILE_LIST_LENGTH + 1;
Johan Pascalb62bb512015-12-03 21:56:45 +01004806 }
4807 }
4808
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004809 if (list_size > MBEDTLS_TLS_SRTP_MAX_PROFILE_LIST_LENGTH) {
4810 conf->dtls_srtp_profile_list = NULL;
4811 conf->dtls_srtp_profile_list_len = 0;
4812 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
Johan Pascal253d0262020-09-22 13:04:45 +02004813 }
4814
Johan Pascal9bc97ca2020-09-21 23:44:45 +02004815 conf->dtls_srtp_profile_list = profiles;
Johan Pascal253d0262020-09-22 13:04:45 +02004816 conf->dtls_srtp_profile_list_len = list_size;
Johan Pascalb62bb512015-12-03 21:56:45 +01004817
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004818 return 0;
Johan Pascalb62bb512015-12-03 21:56:45 +01004819}
4820
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004821void mbedtls_ssl_get_dtls_srtp_negotiation_result(const mbedtls_ssl_context *ssl,
4822 mbedtls_dtls_srtp_info *dtls_srtp_info)
Johan Pascalb62bb512015-12-03 21:56:45 +01004823{
Johan Pascal2258a4f2020-10-28 13:53:09 +01004824 dtls_srtp_info->chosen_dtls_srtp_profile = ssl->dtls_srtp_info.chosen_dtls_srtp_profile;
4825 /* do not copy the mki value if there is no chosen profile */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004826 if (dtls_srtp_info->chosen_dtls_srtp_profile == MBEDTLS_TLS_SRTP_UNSET) {
Johan Pascal2258a4f2020-10-28 13:53:09 +01004827 dtls_srtp_info->mki_len = 0;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004828 } else {
Johan Pascal2258a4f2020-10-28 13:53:09 +01004829 dtls_srtp_info->mki_len = ssl->dtls_srtp_info.mki_len;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004830 memcpy(dtls_srtp_info->mki_value, ssl->dtls_srtp_info.mki_value,
4831 ssl->dtls_srtp_info.mki_len);
Johan Pascal2258a4f2020-10-28 13:53:09 +01004832 }
Johan Pascalb62bb512015-12-03 21:56:45 +01004833}
Johan Pascalb62bb512015-12-03 21:56:45 +01004834#endif /* MBEDTLS_SSL_DTLS_SRTP */
4835
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004836void mbedtls_ssl_conf_max_version(mbedtls_ssl_config *conf, int major, int minor)
Paul Bakker490ecc82011-10-06 13:04:09 +00004837{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02004838 conf->max_major_ver = major;
4839 conf->max_minor_ver = minor;
Paul Bakker490ecc82011-10-06 13:04:09 +00004840}
4841
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004842void mbedtls_ssl_conf_min_version(mbedtls_ssl_config *conf, int major, int minor)
Paul Bakker1d29fb52012-09-28 13:28:45 +00004843{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02004844 conf->min_major_ver = major;
4845 conf->min_minor_ver = minor;
Paul Bakker1d29fb52012-09-28 13:28:45 +00004846}
4847
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004848#if defined(MBEDTLS_SSL_FALLBACK_SCSV) && defined(MBEDTLS_SSL_CLI_C)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004849void mbedtls_ssl_conf_fallback(mbedtls_ssl_config *conf, char fallback)
Manuel Pégourié-Gonnard1cbd39d2014-10-20 13:34:59 +02004850{
Manuel Pégourié-Gonnard684b0592015-05-06 09:27:31 +01004851 conf->fallback = fallback;
Manuel Pégourié-Gonnard1cbd39d2014-10-20 13:34:59 +02004852}
4853#endif
4854
Janos Follath088ce432017-04-10 12:42:31 +01004855#if defined(MBEDTLS_SSL_SRV_C)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004856void mbedtls_ssl_conf_cert_req_ca_list(mbedtls_ssl_config *conf,
4857 char cert_req_ca_list)
Janos Follath088ce432017-04-10 12:42:31 +01004858{
4859 conf->cert_req_ca_list = cert_req_ca_list;
4860}
4861#endif
4862
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004863#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004864void mbedtls_ssl_conf_encrypt_then_mac(mbedtls_ssl_config *conf, char etm)
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +01004865{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02004866 conf->encrypt_then_mac = etm;
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +01004867}
4868#endif
4869
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004870#if defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004871void mbedtls_ssl_conf_extended_master_secret(mbedtls_ssl_config *conf, char ems)
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +02004872{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02004873 conf->extended_ms = ems;
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +02004874}
4875#endif
4876
Manuel Pégourié-Gonnard66dc5552015-05-14 12:28:21 +02004877#if defined(MBEDTLS_ARC4_C)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004878void mbedtls_ssl_conf_arc4_support(mbedtls_ssl_config *conf, char arc4)
Manuel Pégourié-Gonnardbd47a582015-01-12 13:43:29 +01004879{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02004880 conf->arc4_disabled = arc4;
Manuel Pégourié-Gonnardbd47a582015-01-12 13:43:29 +01004881}
Manuel Pégourié-Gonnard66dc5552015-05-14 12:28:21 +02004882#endif
Manuel Pégourié-Gonnardbd47a582015-01-12 13:43:29 +01004883
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004884#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004885int mbedtls_ssl_conf_max_frag_len(mbedtls_ssl_config *conf, unsigned char mfl_code)
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02004886{
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004887 if (mfl_code >= MBEDTLS_SSL_MAX_FRAG_LEN_INVALID ||
4888 ssl_mfl_code_to_length(mfl_code) > MBEDTLS_TLS_EXT_ADV_CONTENT_LEN) {
4889 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02004890 }
4891
Manuel Pégourié-Gonnard6bf89d62015-05-05 17:01:57 +01004892 conf->mfl_code = mfl_code;
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02004893
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004894 return 0;
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02004895}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004896#endif /* MBEDTLS_SSL_MAX_FRAGMENT_LENGTH */
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02004897
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004898#if defined(MBEDTLS_SSL_TRUNCATED_HMAC)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004899void mbedtls_ssl_conf_truncated_hmac(mbedtls_ssl_config *conf, int truncate)
Manuel Pégourié-Gonnarde980a992013-07-19 11:08:52 +02004900{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02004901 conf->trunc_hmac = truncate;
Manuel Pégourié-Gonnarde980a992013-07-19 11:08:52 +02004902}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004903#endif /* MBEDTLS_SSL_TRUNCATED_HMAC */
Manuel Pégourié-Gonnarde980a992013-07-19 11:08:52 +02004904
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004905#if defined(MBEDTLS_SSL_CBC_RECORD_SPLITTING)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004906void mbedtls_ssl_conf_cbc_record_splitting(mbedtls_ssl_config *conf, char split)
Manuel Pégourié-Gonnardcfa477e2015-01-07 14:50:54 +01004907{
Manuel Pégourié-Gonnard17eab2b2015-05-05 16:34:53 +01004908 conf->cbc_record_splitting = split;
Manuel Pégourié-Gonnardcfa477e2015-01-07 14:50:54 +01004909}
4910#endif
4911
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004912void mbedtls_ssl_conf_legacy_renegotiation(mbedtls_ssl_config *conf, int allow_legacy)
Paul Bakker48916f92012-09-16 19:57:18 +00004913{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02004914 conf->allow_legacy_renegotiation = allow_legacy;
Paul Bakker48916f92012-09-16 19:57:18 +00004915}
4916
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004917#if defined(MBEDTLS_SSL_RENEGOTIATION)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004918void mbedtls_ssl_conf_renegotiation(mbedtls_ssl_config *conf, int renegotiation)
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01004919{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02004920 conf->disable_renegotiation = renegotiation;
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01004921}
4922
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004923void mbedtls_ssl_conf_renegotiation_enforced(mbedtls_ssl_config *conf, int max_records)
Manuel Pégourié-Gonnarda9964db2014-07-03 19:29:16 +02004924{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02004925 conf->renego_max_records = max_records;
Manuel Pégourié-Gonnarda9964db2014-07-03 19:29:16 +02004926}
4927
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004928void mbedtls_ssl_conf_renegotiation_period(mbedtls_ssl_config *conf,
4929 const unsigned char period[8])
Manuel Pégourié-Gonnard837f0fe2014-11-05 13:58:53 +01004930{
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004931 memcpy(conf->renego_period, period, 8);
Manuel Pégourié-Gonnard837f0fe2014-11-05 13:58:53 +01004932}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004933#endif /* MBEDTLS_SSL_RENEGOTIATION */
Paul Bakker5121ce52009-01-03 21:22:43 +00004934
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004935#if defined(MBEDTLS_SSL_SESSION_TICKETS)
Manuel Pégourié-Gonnardb596abf2015-05-20 10:45:29 +02004936#if defined(MBEDTLS_SSL_CLI_C)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004937void mbedtls_ssl_conf_session_tickets(mbedtls_ssl_config *conf, int use_tickets)
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +02004938{
Manuel Pégourié-Gonnard2b494452015-05-06 10:05:11 +01004939 conf->session_tickets = use_tickets;
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +02004940}
Manuel Pégourié-Gonnardb596abf2015-05-20 10:45:29 +02004941#endif
Paul Bakker606b4ba2013-08-14 16:52:14 +02004942
Manuel Pégourié-Gonnardb596abf2015-05-20 10:45:29 +02004943#if defined(MBEDTLS_SSL_SRV_C)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004944void mbedtls_ssl_conf_session_tickets_cb(mbedtls_ssl_config *conf,
4945 mbedtls_ssl_ticket_write_t *f_ticket_write,
4946 mbedtls_ssl_ticket_parse_t *f_ticket_parse,
4947 void *p_ticket)
Paul Bakker606b4ba2013-08-14 16:52:14 +02004948{
Manuel Pégourié-Gonnardd59675d2015-05-19 15:28:00 +02004949 conf->f_ticket_write = f_ticket_write;
4950 conf->f_ticket_parse = f_ticket_parse;
4951 conf->p_ticket = p_ticket;
Paul Bakker606b4ba2013-08-14 16:52:14 +02004952}
Manuel Pégourié-Gonnardb596abf2015-05-20 10:45:29 +02004953#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004954#endif /* MBEDTLS_SSL_SESSION_TICKETS */
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +02004955
Robert Cragie4feb7ae2015-10-02 13:33:37 +01004956#if defined(MBEDTLS_SSL_EXPORT_KEYS)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004957void mbedtls_ssl_conf_export_keys_cb(mbedtls_ssl_config *conf,
4958 mbedtls_ssl_export_keys_t *f_export_keys,
4959 void *p_export_keys)
Robert Cragie4feb7ae2015-10-02 13:33:37 +01004960{
4961 conf->f_export_keys = f_export_keys;
4962 conf->p_export_keys = p_export_keys;
4963}
Ron Eldorf5cc10d2019-05-07 18:33:40 +03004964
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004965void mbedtls_ssl_conf_export_keys_ext_cb(mbedtls_ssl_config *conf,
4966 mbedtls_ssl_export_keys_ext_t *f_export_keys_ext,
4967 void *p_export_keys)
Ron Eldorf5cc10d2019-05-07 18:33:40 +03004968{
4969 conf->f_export_keys_ext = f_export_keys_ext;
4970 conf->p_export_keys = p_export_keys;
4971}
Robert Cragie4feb7ae2015-10-02 13:33:37 +01004972#endif
4973
Gilles Peskineb74a1c72018-04-24 13:09:22 +02004974#if defined(MBEDTLS_SSL_ASYNC_PRIVATE)
Gilles Peskine8bf79f62018-01-05 21:11:53 +01004975void mbedtls_ssl_conf_async_private_cb(
4976 mbedtls_ssl_config *conf,
4977 mbedtls_ssl_async_sign_t *f_async_sign,
4978 mbedtls_ssl_async_decrypt_t *f_async_decrypt,
4979 mbedtls_ssl_async_resume_t *f_async_resume,
4980 mbedtls_ssl_async_cancel_t *f_async_cancel,
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004981 void *async_config_data)
Gilles Peskine8bf79f62018-01-05 21:11:53 +01004982{
4983 conf->f_async_sign_start = f_async_sign;
4984 conf->f_async_decrypt_start = f_async_decrypt;
4985 conf->f_async_resume = f_async_resume;
4986 conf->f_async_cancel = f_async_cancel;
Gilles Peskinedf13d5c2018-04-25 20:39:48 +02004987 conf->p_async_config_data = async_config_data;
4988}
4989
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004990void *mbedtls_ssl_conf_get_async_config_data(const mbedtls_ssl_config *conf)
Gilles Peskine8f97af72018-04-26 11:46:10 +02004991{
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004992 return conf->p_async_config_data;
Gilles Peskine8f97af72018-04-26 11:46:10 +02004993}
4994
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004995void *mbedtls_ssl_get_async_operation_data(const mbedtls_ssl_context *ssl)
Gilles Peskinedf13d5c2018-04-25 20:39:48 +02004996{
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004997 if (ssl->handshake == NULL) {
4998 return NULL;
4999 } else {
5000 return ssl->handshake->user_async_ctx;
5001 }
Gilles Peskinedf13d5c2018-04-25 20:39:48 +02005002}
5003
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005004void mbedtls_ssl_set_async_operation_data(mbedtls_ssl_context *ssl,
5005 void *ctx)
Gilles Peskinedf13d5c2018-04-25 20:39:48 +02005006{
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005007 if (ssl->handshake != NULL) {
Gilles Peskinedf13d5c2018-04-25 20:39:48 +02005008 ssl->handshake->user_async_ctx = ctx;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005009 }
Gilles Peskine8bf79f62018-01-05 21:11:53 +01005010}
Gilles Peskineb74a1c72018-04-24 13:09:22 +02005011#endif /* MBEDTLS_SSL_ASYNC_PRIVATE */
Gilles Peskine8bf79f62018-01-05 21:11:53 +01005012
Paul Bakker5121ce52009-01-03 21:22:43 +00005013/*
5014 * SSL get accessors
5015 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005016uint32_t mbedtls_ssl_get_verify_result(const mbedtls_ssl_context *ssl)
Paul Bakker5121ce52009-01-03 21:22:43 +00005017{
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005018 if (ssl->session != NULL) {
5019 return ssl->session->verify_result;
5020 }
Manuel Pégourié-Gonnarde89163c2015-01-23 14:30:57 +00005021
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005022 if (ssl->session_negotiate != NULL) {
5023 return ssl->session_negotiate->verify_result;
5024 }
Manuel Pégourié-Gonnarde89163c2015-01-23 14:30:57 +00005025
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005026 return 0xFFFFFFFF;
Paul Bakker5121ce52009-01-03 21:22:43 +00005027}
5028
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005029const char *mbedtls_ssl_get_ciphersuite(const mbedtls_ssl_context *ssl)
Paul Bakker72f62662011-01-16 21:27:44 +00005030{
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005031 if (ssl == NULL || ssl->session == NULL) {
5032 return NULL;
5033 }
Paul Bakker926c8e42013-03-06 10:23:34 +01005034
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005035 return mbedtls_ssl_get_ciphersuite_name(ssl->session->ciphersuite);
Paul Bakker72f62662011-01-16 21:27:44 +00005036}
5037
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005038const char *mbedtls_ssl_get_version(const mbedtls_ssl_context *ssl)
Paul Bakker43ca69c2011-01-15 17:35:19 +00005039{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005040#if defined(MBEDTLS_SSL_PROTO_DTLS)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005041 if (ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM) {
5042 switch (ssl->minor_ver) {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005043 case MBEDTLS_SSL_MINOR_VERSION_2:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005044 return "DTLSv1.0";
Manuel Pégourié-Gonnardb21ca2a2014-02-10 13:43:33 +01005045
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005046 case MBEDTLS_SSL_MINOR_VERSION_3:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005047 return "DTLSv1.2";
Manuel Pégourié-Gonnardb21ca2a2014-02-10 13:43:33 +01005048
5049 default:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005050 return "unknown (DTLS)";
Manuel Pégourié-Gonnardb21ca2a2014-02-10 13:43:33 +01005051 }
5052 }
5053#endif
5054
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005055 switch (ssl->minor_ver) {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005056 case MBEDTLS_SSL_MINOR_VERSION_0:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005057 return "SSLv3.0";
Paul Bakker43ca69c2011-01-15 17:35:19 +00005058
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005059 case MBEDTLS_SSL_MINOR_VERSION_1:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005060 return "TLSv1.0";
Paul Bakker43ca69c2011-01-15 17:35:19 +00005061
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005062 case MBEDTLS_SSL_MINOR_VERSION_2:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005063 return "TLSv1.1";
Paul Bakker43ca69c2011-01-15 17:35:19 +00005064
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005065 case MBEDTLS_SSL_MINOR_VERSION_3:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005066 return "TLSv1.2";
Paul Bakker1ef83d62012-04-11 12:09:53 +00005067
Paul Bakker43ca69c2011-01-15 17:35:19 +00005068 default:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005069 return "unknown";
Paul Bakker43ca69c2011-01-15 17:35:19 +00005070 }
Paul Bakker43ca69c2011-01-15 17:35:19 +00005071}
5072
Manuel Pégourié-Gonnarda2cda6b2015-08-31 18:30:52 +02005073#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005074size_t mbedtls_ssl_get_input_max_frag_len(const mbedtls_ssl_context *ssl)
Andrzej Kurek90c6e842020-04-03 05:25:29 -04005075{
5076 size_t max_len = MBEDTLS_SSL_MAX_CONTENT_LEN;
5077 size_t read_mfl;
5078
5079 /* Use the configured MFL for the client if we're past SERVER_HELLO_DONE */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005080 if (ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT &&
5081 ssl->state >= MBEDTLS_SSL_SERVER_HELLO_DONE) {
5082 return ssl_mfl_code_to_length(ssl->conf->mfl_code);
Andrzej Kurek90c6e842020-04-03 05:25:29 -04005083 }
5084
5085 /* Check if a smaller max length was negotiated */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005086 if (ssl->session_out != NULL) {
5087 read_mfl = ssl_mfl_code_to_length(ssl->session_out->mfl_code);
5088 if (read_mfl < max_len) {
Andrzej Kurek90c6e842020-04-03 05:25:29 -04005089 max_len = read_mfl;
5090 }
5091 }
5092
5093 // During a handshake, use the value being negotiated
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005094 if (ssl->session_negotiate != NULL) {
5095 read_mfl = ssl_mfl_code_to_length(ssl->session_negotiate->mfl_code);
5096 if (read_mfl < max_len) {
Andrzej Kurek90c6e842020-04-03 05:25:29 -04005097 max_len = read_mfl;
5098 }
5099 }
5100
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005101 return max_len;
Andrzej Kurek90c6e842020-04-03 05:25:29 -04005102}
5103
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005104size_t mbedtls_ssl_get_output_max_frag_len(const mbedtls_ssl_context *ssl)
Manuel Pégourié-Gonnarda2cda6b2015-08-31 18:30:52 +02005105{
5106 size_t max_len;
5107
5108 /*
5109 * Assume mfl_code is correct since it was checked when set
5110 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005111 max_len = ssl_mfl_code_to_length(ssl->conf->mfl_code);
Manuel Pégourié-Gonnarda2cda6b2015-08-31 18:30:52 +02005112
Manuel Pégourié-Gonnard2cb17e22017-09-19 13:00:47 +02005113 /* Check if a smaller max length was negotiated */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005114 if (ssl->session_out != NULL &&
5115 ssl_mfl_code_to_length(ssl->session_out->mfl_code) < max_len) {
5116 max_len = ssl_mfl_code_to_length(ssl->session_out->mfl_code);
Manuel Pégourié-Gonnarda2cda6b2015-08-31 18:30:52 +02005117 }
5118
Manuel Pégourié-Gonnard2cb17e22017-09-19 13:00:47 +02005119 /* During a handshake, use the value being negotiated */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005120 if (ssl->session_negotiate != NULL &&
5121 ssl_mfl_code_to_length(ssl->session_negotiate->mfl_code) < max_len) {
5122 max_len = ssl_mfl_code_to_length(ssl->session_negotiate->mfl_code);
Manuel Pégourié-Gonnard2cb17e22017-09-19 13:00:47 +02005123 }
5124
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005125 return max_len;
Manuel Pégourié-Gonnarda2cda6b2015-08-31 18:30:52 +02005126}
Andrzej Kurek90c6e842020-04-03 05:25:29 -04005127
5128#if !defined(MBEDTLS_DEPRECATED_REMOVED)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005129size_t mbedtls_ssl_get_max_frag_len(const mbedtls_ssl_context *ssl)
Andrzej Kurek90c6e842020-04-03 05:25:29 -04005130{
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005131 return mbedtls_ssl_get_output_max_frag_len(ssl);
Andrzej Kurek90c6e842020-04-03 05:25:29 -04005132}
5133#endif /* !MBEDTLS_DEPRECATED_REMOVED */
Manuel Pégourié-Gonnarda2cda6b2015-08-31 18:30:52 +02005134#endif /* MBEDTLS_SSL_MAX_FRAGMENT_LENGTH */
5135
Manuel Pégourié-Gonnardb8eec192018-08-20 09:34:02 +02005136#if defined(MBEDTLS_SSL_PROTO_DTLS)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005137size_t mbedtls_ssl_get_current_mtu(const mbedtls_ssl_context *ssl)
Manuel Pégourié-Gonnardb8eec192018-08-20 09:34:02 +02005138{
Andrzej Kurekef43ce62018-10-09 08:24:12 -04005139 /* Return unlimited mtu for client hello messages to avoid fragmentation. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005140 if (ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT &&
5141 (ssl->state == MBEDTLS_SSL_CLIENT_HELLO ||
5142 ssl->state == MBEDTLS_SSL_SERVER_HELLO)) {
5143 return 0;
5144 }
Andrzej Kurekef43ce62018-10-09 08:24:12 -04005145
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005146 if (ssl->handshake == NULL || ssl->handshake->mtu == 0) {
5147 return ssl->mtu;
5148 }
Manuel Pégourié-Gonnardb8eec192018-08-20 09:34:02 +02005149
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005150 if (ssl->mtu == 0) {
5151 return ssl->handshake->mtu;
5152 }
Manuel Pégourié-Gonnardb8eec192018-08-20 09:34:02 +02005153
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005154 return ssl->mtu < ssl->handshake->mtu ?
5155 ssl->mtu : ssl->handshake->mtu;
Manuel Pégourié-Gonnardb8eec192018-08-20 09:34:02 +02005156}
5157#endif /* MBEDTLS_SSL_PROTO_DTLS */
5158
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005159int mbedtls_ssl_get_max_out_record_payload(const mbedtls_ssl_context *ssl)
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02005160{
5161 size_t max_len = MBEDTLS_SSL_OUT_CONTENT_LEN;
5162
Manuel Pégourié-Gonnard000281e2018-08-21 11:20:58 +02005163#if !defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH) && \
5164 !defined(MBEDTLS_SSL_PROTO_DTLS)
5165 (void) ssl;
5166#endif
5167
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02005168#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005169 const size_t mfl = mbedtls_ssl_get_output_max_frag_len(ssl);
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02005170
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005171 if (max_len > mfl) {
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02005172 max_len = mfl;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005173 }
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02005174#endif
5175
5176#if defined(MBEDTLS_SSL_PROTO_DTLS)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005177 if (mbedtls_ssl_get_current_mtu(ssl) != 0) {
5178 const size_t mtu = mbedtls_ssl_get_current_mtu(ssl);
5179 const int ret = mbedtls_ssl_get_record_expansion(ssl);
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02005180 const size_t overhead = (size_t) ret;
5181
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005182 if (ret < 0) {
5183 return ret;
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02005184 }
5185
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005186 if (mtu <= overhead) {
5187 MBEDTLS_SSL_DEBUG_MSG(1, ("MTU too low for record expansion"));
5188 return MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
5189 }
5190
5191 if (max_len > mtu - overhead) {
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02005192 max_len = mtu - overhead;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005193 }
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02005194 }
Manuel Pégourié-Gonnardb8eec192018-08-20 09:34:02 +02005195#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02005196
Hanno Becker0defedb2018-08-10 12:35:02 +01005197#if !defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH) && \
5198 !defined(MBEDTLS_SSL_PROTO_DTLS)
5199 ((void) ssl);
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02005200#endif
5201
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005202 return (int) max_len;
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02005203}
5204
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005205#if defined(MBEDTLS_X509_CRT_PARSE_C)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005206const mbedtls_x509_crt *mbedtls_ssl_get_peer_cert(const mbedtls_ssl_context *ssl)
Paul Bakkerb0550d92012-10-30 07:51:03 +00005207{
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005208 if (ssl == NULL || ssl->session == NULL) {
5209 return NULL;
5210 }
Paul Bakkerb0550d92012-10-30 07:51:03 +00005211
Hanno Beckere6824572019-02-07 13:18:46 +00005212#if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005213 return ssl->session->peer_cert;
Hanno Beckere6824572019-02-07 13:18:46 +00005214#else
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005215 return NULL;
Hanno Beckere6824572019-02-07 13:18:46 +00005216#endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
Paul Bakkerb0550d92012-10-30 07:51:03 +00005217}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005218#endif /* MBEDTLS_X509_CRT_PARSE_C */
Paul Bakkerb0550d92012-10-30 07:51:03 +00005219
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005220#if defined(MBEDTLS_SSL_CLI_C)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005221int mbedtls_ssl_get_session(const mbedtls_ssl_context *ssl,
5222 mbedtls_ssl_session *dst)
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02005223{
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005224 if (ssl == NULL ||
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02005225 dst == NULL ||
5226 ssl->session == NULL ||
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005227 ssl->conf->endpoint != MBEDTLS_SSL_IS_CLIENT) {
5228 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02005229 }
5230
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005231 return mbedtls_ssl_session_copy(dst, ssl->session);
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02005232}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005233#endif /* MBEDTLS_SSL_CLI_C */
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02005234
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005235const mbedtls_ssl_session *mbedtls_ssl_get_session_pointer(const mbedtls_ssl_context *ssl)
Manuel Pégourié-Gonnardb5e4e0a2019-05-20 11:12:28 +02005236{
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005237 if (ssl == NULL) {
5238 return NULL;
5239 }
Manuel Pégourié-Gonnardb5e4e0a2019-05-20 11:12:28 +02005240
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005241 return ssl->session;
Manuel Pégourié-Gonnardb5e4e0a2019-05-20 11:12:28 +02005242}
5243
Paul Bakker5121ce52009-01-03 21:22:43 +00005244/*
Hanno Beckera835da52019-05-16 12:39:07 +01005245 * Define ticket header determining Mbed TLS version
5246 * and structure of the ticket.
5247 */
5248
Hanno Becker94ef3b32019-05-16 12:50:45 +01005249/*
Hanno Becker50b59662019-05-28 14:30:45 +01005250 * Define bitflag determining compile-time settings influencing
5251 * structure of serialized SSL sessions.
Hanno Becker94ef3b32019-05-16 12:50:45 +01005252 */
5253
Hanno Becker50b59662019-05-28 14:30:45 +01005254#if defined(MBEDTLS_HAVE_TIME)
Hanno Becker3e088662019-05-29 11:10:18 +01005255#define SSL_SERIALIZED_SESSION_CONFIG_TIME 1
Hanno Becker50b59662019-05-28 14:30:45 +01005256#else
Hanno Becker3e088662019-05-29 11:10:18 +01005257#define SSL_SERIALIZED_SESSION_CONFIG_TIME 0
Hanno Becker94ef3b32019-05-16 12:50:45 +01005258#endif /* MBEDTLS_HAVE_TIME */
5259
5260#if defined(MBEDTLS_X509_CRT_PARSE_C)
Hanno Becker3e088662019-05-29 11:10:18 +01005261#define SSL_SERIALIZED_SESSION_CONFIG_CRT 1
Hanno Becker94ef3b32019-05-16 12:50:45 +01005262#else
Hanno Becker3e088662019-05-29 11:10:18 +01005263#define SSL_SERIALIZED_SESSION_CONFIG_CRT 0
Hanno Becker94ef3b32019-05-16 12:50:45 +01005264#endif /* MBEDTLS_X509_CRT_PARSE_C */
5265
David Horstmanneb77b6f2024-02-13 17:53:35 +00005266#if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
David Horstmann11def972024-03-01 11:20:32 +00005267#define SSL_SERIALIZED_SESSION_CONFIG_KEEP_PEER_CRT 1
David Horstmanneb77b6f2024-02-13 17:53:35 +00005268#else
David Horstmann11def972024-03-01 11:20:32 +00005269#define SSL_SERIALIZED_SESSION_CONFIG_KEEP_PEER_CRT 0
David Horstmanneb77b6f2024-02-13 17:53:35 +00005270#endif /* MBEDTLS_SSL_SESSION_TICKETS */
5271
Hanno Becker94ef3b32019-05-16 12:50:45 +01005272#if defined(MBEDTLS_SSL_CLI_C) && defined(MBEDTLS_SSL_SESSION_TICKETS)
Hanno Becker3e088662019-05-29 11:10:18 +01005273#define SSL_SERIALIZED_SESSION_CONFIG_CLIENT_TICKET 1
Hanno Becker94ef3b32019-05-16 12:50:45 +01005274#else
Hanno Becker3e088662019-05-29 11:10:18 +01005275#define SSL_SERIALIZED_SESSION_CONFIG_CLIENT_TICKET 0
Hanno Becker94ef3b32019-05-16 12:50:45 +01005276#endif /* MBEDTLS_SSL_CLI_C && MBEDTLS_SSL_SESSION_TICKETS */
5277
5278#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
Hanno Becker3e088662019-05-29 11:10:18 +01005279#define SSL_SERIALIZED_SESSION_CONFIG_MFL 1
Hanno Becker94ef3b32019-05-16 12:50:45 +01005280#else
Hanno Becker3e088662019-05-29 11:10:18 +01005281#define SSL_SERIALIZED_SESSION_CONFIG_MFL 0
Hanno Becker94ef3b32019-05-16 12:50:45 +01005282#endif /* MBEDTLS_SSL_MAX_FRAGMENT_LENGTH */
5283
5284#if defined(MBEDTLS_SSL_TRUNCATED_HMAC)
Hanno Becker3e088662019-05-29 11:10:18 +01005285#define SSL_SERIALIZED_SESSION_CONFIG_TRUNC_HMAC 1
Hanno Becker94ef3b32019-05-16 12:50:45 +01005286#else
Hanno Becker3e088662019-05-29 11:10:18 +01005287#define SSL_SERIALIZED_SESSION_CONFIG_TRUNC_HMAC 0
Hanno Becker94ef3b32019-05-16 12:50:45 +01005288#endif /* MBEDTLS_SSL_TRUNCATED_HMAC */
5289
5290#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
Hanno Becker3e088662019-05-29 11:10:18 +01005291#define SSL_SERIALIZED_SESSION_CONFIG_ETM 1
Hanno Becker94ef3b32019-05-16 12:50:45 +01005292#else
Hanno Becker3e088662019-05-29 11:10:18 +01005293#define SSL_SERIALIZED_SESSION_CONFIG_ETM 0
Hanno Becker94ef3b32019-05-16 12:50:45 +01005294#endif /* MBEDTLS_SSL_ENCRYPT_THEN_MAC */
5295
Hanno Becker94ef3b32019-05-16 12:50:45 +01005296#if defined(MBEDTLS_SSL_SESSION_TICKETS)
5297#define SSL_SERIALIZED_SESSION_CONFIG_TICKET 1
5298#else
5299#define SSL_SERIALIZED_SESSION_CONFIG_TICKET 0
5300#endif /* MBEDTLS_SSL_SESSION_TICKETS */
5301
Hanno Becker3e088662019-05-29 11:10:18 +01005302#define SSL_SERIALIZED_SESSION_CONFIG_TIME_BIT 0
5303#define SSL_SERIALIZED_SESSION_CONFIG_CRT_BIT 1
5304#define SSL_SERIALIZED_SESSION_CONFIG_CLIENT_TICKET_BIT 2
5305#define SSL_SERIALIZED_SESSION_CONFIG_MFL_BIT 3
5306#define SSL_SERIALIZED_SESSION_CONFIG_TRUNC_HMAC_BIT 4
5307#define SSL_SERIALIZED_SESSION_CONFIG_ETM_BIT 5
5308#define SSL_SERIALIZED_SESSION_CONFIG_TICKET_BIT 6
David Horstmann11def972024-03-01 11:20:32 +00005309#define SSL_SERIALIZED_SESSION_CONFIG_KEEP_PEER_CRT_BIT 7
Hanno Becker3e088662019-05-29 11:10:18 +01005310
Hanno Becker50b59662019-05-28 14:30:45 +01005311#define SSL_SERIALIZED_SESSION_CONFIG_BITFLAG \
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005312 ((uint16_t) ( \
5313 (SSL_SERIALIZED_SESSION_CONFIG_TIME << SSL_SERIALIZED_SESSION_CONFIG_TIME_BIT) | \
5314 (SSL_SERIALIZED_SESSION_CONFIG_CRT << SSL_SERIALIZED_SESSION_CONFIG_CRT_BIT) | \
5315 (SSL_SERIALIZED_SESSION_CONFIG_CLIENT_TICKET << \
5316 SSL_SERIALIZED_SESSION_CONFIG_CLIENT_TICKET_BIT) | \
5317 (SSL_SERIALIZED_SESSION_CONFIG_MFL << SSL_SERIALIZED_SESSION_CONFIG_MFL_BIT) | \
5318 (SSL_SERIALIZED_SESSION_CONFIG_TRUNC_HMAC << \
5319 SSL_SERIALIZED_SESSION_CONFIG_TRUNC_HMAC_BIT) | \
5320 (SSL_SERIALIZED_SESSION_CONFIG_ETM << SSL_SERIALIZED_SESSION_CONFIG_ETM_BIT) | \
David Horstmanneb77b6f2024-02-13 17:53:35 +00005321 (SSL_SERIALIZED_SESSION_CONFIG_TICKET << SSL_SERIALIZED_SESSION_CONFIG_TICKET_BIT) | \
David Horstmannf5a6fa22024-03-01 12:31:35 +00005322 (SSL_SERIALIZED_SESSION_CONFIG_KEEP_PEER_CRT << \
5323 SSL_SERIALIZED_SESSION_CONFIG_KEEP_PEER_CRT_BIT)))
Hanno Becker94ef3b32019-05-16 12:50:45 +01005324
Chien Wongb6d57932024-02-07 21:48:12 +08005325static const unsigned char ssl_serialized_session_header[] = {
Hanno Becker94ef3b32019-05-16 12:50:45 +01005326 MBEDTLS_VERSION_MAJOR,
5327 MBEDTLS_VERSION_MINOR,
5328 MBEDTLS_VERSION_PATCH,
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005329 MBEDTLS_BYTE_1(SSL_SERIALIZED_SESSION_CONFIG_BITFLAG),
5330 MBEDTLS_BYTE_0(SSL_SERIALIZED_SESSION_CONFIG_BITFLAG),
Hanno Beckerf8787072019-05-16 12:41:07 +01005331};
Hanno Beckera835da52019-05-16 12:39:07 +01005332
5333/*
Manuel Pégourié-Gonnarda3e7c652019-05-16 10:08:35 +02005334 * Serialize a session in the following format:
Manuel Pégourié-Gonnard35eb8022019-05-16 11:11:08 +02005335 * (in the presentation language of TLS, RFC 8446 section 3)
Manuel Pégourié-Gonnarda3e7c652019-05-16 10:08:35 +02005336 *
Hanno Becker50b59662019-05-28 14:30:45 +01005337 * opaque mbedtls_version[3]; // major, minor, patch
5338 * opaque session_format[2]; // version-specific 16-bit field determining
5339 * // the format of the remaining
5340 * // serialized data.
Hanno Beckerdc28b6c2019-05-29 11:08:00 +01005341 *
5342 * Note: When updating the format, remember to keep
5343 * these version+format bytes.
5344 *
Hanno Beckerbe34e8e2019-06-04 09:43:16 +01005345 * // In this version, `session_format` determines
5346 * // the setting of those compile-time
5347 * // configuration options which influence
Hanno Becker50b59662019-05-28 14:30:45 +01005348 * // the structure of mbedtls_ssl_session.
David Horstmann363db772024-03-01 12:11:24 +00005349 * #if defined(MBEDTLS_HAVE_TIME)
5350 * uint64 start_time;
5351 * #endif
5352 * uint8 ciphersuite[2]; // defined by the standard
5353 * uint8 compression; // 0 or 1
5354 * uint8 session_id_len; // at most 32
5355 * opaque session_id[32];
5356 * opaque master[48]; // fixed length in the standard
5357 * uint32 verify_result;
David Horstmannfc8cacf2024-03-04 16:37:02 +00005358 * #if defined(MBEDTLS_X509_CRT_PARSE_C)
David Horstmann363db772024-03-01 12:11:24 +00005359 * #if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
5360 * opaque peer_cert<0..2^24-1>; // length 0 means no peer cert
5361 * #else
5362 * uint8 peer_cert_digest_type;
5363 * opaque peer_cert_digest<0..2^8-1>
5364 * #endif
David Horstmannfc8cacf2024-03-04 16:37:02 +00005365 * #endif
5366 * #if defined(MBEDTLS_SSL_SESSION_TICKETS) && defined(MBEDTLS_SSL_CLI_C)
David Horstmann363db772024-03-01 12:11:24 +00005367 * opaque ticket<0..2^24-1>; // length 0 means no ticket
5368 * uint32 ticket_lifetime;
5369 * #endif
5370 * #if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
5371 * uint8 mfl_code; // up to 255 according to standard
5372 * #endif
5373 * #if defined(MBEDTLS_SSL_TRUNCATED_HMAC)
5374 * uint8 trunc_hmac; // 0 or 1
5375 * #endif
5376 * #if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
5377 * uint8 encrypt_then_mac; // 0 or 1
5378 * #endif
Manuel Pégourié-Gonnarda3e7c652019-05-16 10:08:35 +02005379 *
Manuel Pégourié-Gonnardf743c032019-05-24 12:06:29 +02005380 * The order is the same as in the definition of the structure, except
5381 * verify_result is put before peer_cert so that all mandatory fields come
5382 * together in one block.
Manuel Pégourié-Gonnarda3e7c652019-05-16 10:08:35 +02005383 */
Manuel Pégourié-Gonnardd904d662022-06-17 10:24:00 +02005384MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005385static int ssl_session_save(const mbedtls_ssl_session *session,
5386 unsigned char omit_header,
5387 unsigned char *buf,
5388 size_t buf_len,
5389 size_t *olen)
Manuel Pégourié-Gonnarda3e7c652019-05-16 10:08:35 +02005390{
5391 unsigned char *p = buf;
Manuel Pégourié-Gonnard26f982f2019-05-21 11:01:32 +02005392 size_t used = 0;
Manuel Pégourié-Gonnardf743c032019-05-24 12:06:29 +02005393#if defined(MBEDTLS_HAVE_TIME)
5394 uint64_t start;
5395#endif
Manuel Pégourié-Gonnarda3e7c652019-05-16 10:08:35 +02005396#if defined(MBEDTLS_X509_CRT_PARSE_C)
5397#if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
5398 size_t cert_len;
Manuel Pégourié-Gonnarda3e7c652019-05-16 10:08:35 +02005399#endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
5400#endif /* MBEDTLS_X509_CRT_PARSE_C */
5401
Manuel Pégourié-Gonnarda3e7c652019-05-16 10:08:35 +02005402
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005403 if (!omit_header) {
Manuel Pégourié-Gonnard45ac1f02019-07-23 16:52:45 +02005404 /*
5405 * Add version identifier
5406 */
5407
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005408 used += sizeof(ssl_serialized_session_header);
Manuel Pégourié-Gonnard45ac1f02019-07-23 16:52:45 +02005409
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005410 if (used <= buf_len) {
5411 memcpy(p, ssl_serialized_session_header,
5412 sizeof(ssl_serialized_session_header));
5413 p += sizeof(ssl_serialized_session_header);
Manuel Pégourié-Gonnard45ac1f02019-07-23 16:52:45 +02005414 }
Hanno Beckera835da52019-05-16 12:39:07 +01005415 }
5416
5417 /*
Manuel Pégourié-Gonnardf743c032019-05-24 12:06:29 +02005418 * Time
5419 */
5420#if defined(MBEDTLS_HAVE_TIME)
5421 used += 8;
5422
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005423 if (used <= buf_len) {
Manuel Pégourié-Gonnardf743c032019-05-24 12:06:29 +02005424 start = (uint64_t) session->start;
5425
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005426 MBEDTLS_PUT_UINT64_BE(start, p, 0);
Joe Subbiani2f98d792021-08-20 11:44:44 +01005427 p += 8;
Manuel Pégourié-Gonnardf743c032019-05-24 12:06:29 +02005428 }
5429#endif /* MBEDTLS_HAVE_TIME */
5430
5431 /*
5432 * Basic mandatory fields
5433 */
5434 used += 2 /* ciphersuite */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005435 + 1 /* compression */
5436 + 1 /* id_len */
5437 + sizeof(session->id)
5438 + sizeof(session->master)
5439 + 4; /* verify_result */
Manuel Pégourié-Gonnardf743c032019-05-24 12:06:29 +02005440
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005441 if (used <= buf_len) {
5442 MBEDTLS_PUT_UINT16_BE(session->ciphersuite, p, 0);
Joe Subbiani2f98d792021-08-20 11:44:44 +01005443 p += 2;
Manuel Pégourié-Gonnardf743c032019-05-24 12:06:29 +02005444
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005445 *p++ = MBEDTLS_BYTE_0(session->compression);
Manuel Pégourié-Gonnardf743c032019-05-24 12:06:29 +02005446
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005447 *p++ = MBEDTLS_BYTE_0(session->id_len);
5448 memcpy(p, session->id, 32);
Manuel Pégourié-Gonnardf743c032019-05-24 12:06:29 +02005449 p += 32;
5450
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005451 memcpy(p, session->master, 48);
Manuel Pégourié-Gonnardf743c032019-05-24 12:06:29 +02005452 p += 48;
5453
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005454 MBEDTLS_PUT_UINT32_BE(session->verify_result, p, 0);
Joe Subbiani2f98d792021-08-20 11:44:44 +01005455 p += 4;
Manuel Pégourié-Gonnard26f982f2019-05-21 11:01:32 +02005456 }
Manuel Pégourié-Gonnarda3e7c652019-05-16 10:08:35 +02005457
Manuel Pégourié-Gonnard35eb8022019-05-16 11:11:08 +02005458 /*
Manuel Pégourié-Gonnardf743c032019-05-24 12:06:29 +02005459 * Peer's end-entity certificate
Manuel Pégourié-Gonnard35eb8022019-05-16 11:11:08 +02005460 */
Manuel Pégourié-Gonnarda3e7c652019-05-16 10:08:35 +02005461#if defined(MBEDTLS_X509_CRT_PARSE_C)
5462#if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005463 if (session->peer_cert == NULL) {
Manuel Pégourié-Gonnarda3e7c652019-05-16 10:08:35 +02005464 cert_len = 0;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005465 } else {
Manuel Pégourié-Gonnarda3e7c652019-05-16 10:08:35 +02005466 cert_len = session->peer_cert->raw.len;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005467 }
Manuel Pégourié-Gonnarda3e7c652019-05-16 10:08:35 +02005468
Manuel Pégourié-Gonnard26f982f2019-05-21 11:01:32 +02005469 used += 3 + cert_len;
Manuel Pégourié-Gonnarda3e7c652019-05-16 10:08:35 +02005470
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005471 if (used <= buf_len) {
5472 *p++ = MBEDTLS_BYTE_2(cert_len);
5473 *p++ = MBEDTLS_BYTE_1(cert_len);
5474 *p++ = MBEDTLS_BYTE_0(cert_len);
Manuel Pégourié-Gonnarda3e7c652019-05-16 10:08:35 +02005475
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005476 if (session->peer_cert != NULL) {
5477 memcpy(p, session->peer_cert->raw.p, cert_len);
Manuel Pégourié-Gonnard26f982f2019-05-21 11:01:32 +02005478 p += cert_len;
5479 }
5480 }
Manuel Pégourié-Gonnarda3e7c652019-05-16 10:08:35 +02005481#else /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005482 if (session->peer_cert_digest != NULL) {
Manuel Pégourié-Gonnardf743c032019-05-24 12:06:29 +02005483 used += 1 /* type */ + 1 /* length */ + session->peer_cert_digest_len;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005484 if (used <= buf_len) {
Manuel Pégourié-Gonnardf743c032019-05-24 12:06:29 +02005485 *p++ = (unsigned char) session->peer_cert_digest_type;
5486 *p++ = (unsigned char) session->peer_cert_digest_len;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005487 memcpy(p, session->peer_cert_digest,
5488 session->peer_cert_digest_len);
Manuel Pégourié-Gonnardf743c032019-05-24 12:06:29 +02005489 p += session->peer_cert_digest_len;
5490 }
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005491 } else {
Manuel Pégourié-Gonnardf743c032019-05-24 12:06:29 +02005492 used += 2;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005493 if (used <= buf_len) {
Manuel Pégourié-Gonnardf743c032019-05-24 12:06:29 +02005494 *p++ = (unsigned char) MBEDTLS_MD_NONE;
5495 *p++ = 0;
5496 }
Manuel Pégourié-Gonnard26f982f2019-05-21 11:01:32 +02005497 }
Manuel Pégourié-Gonnarda3e7c652019-05-16 10:08:35 +02005498#endif /* !MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
5499#endif /* MBEDTLS_X509_CRT_PARSE_C */
5500
Manuel Pégourié-Gonnard35eb8022019-05-16 11:11:08 +02005501 /*
Manuel Pégourié-Gonnardf743c032019-05-24 12:06:29 +02005502 * Session ticket if any, plus associated data
Manuel Pégourié-Gonnard35eb8022019-05-16 11:11:08 +02005503 */
5504#if defined(MBEDTLS_SSL_SESSION_TICKETS) && defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnardf743c032019-05-24 12:06:29 +02005505 used += 3 + session->ticket_len + 4; /* len + ticket + lifetime */
Manuel Pégourié-Gonnard35eb8022019-05-16 11:11:08 +02005506
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005507 if (used <= buf_len) {
5508 *p++ = MBEDTLS_BYTE_2(session->ticket_len);
5509 *p++ = MBEDTLS_BYTE_1(session->ticket_len);
5510 *p++ = MBEDTLS_BYTE_0(session->ticket_len);
Manuel Pégourié-Gonnard26f982f2019-05-21 11:01:32 +02005511
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005512 if (session->ticket != NULL) {
5513 memcpy(p, session->ticket, session->ticket_len);
Manuel Pégourié-Gonnard26f982f2019-05-21 11:01:32 +02005514 p += session->ticket_len;
5515 }
Manuel Pégourié-Gonnardf743c032019-05-24 12:06:29 +02005516
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005517 MBEDTLS_PUT_UINT32_BE(session->ticket_lifetime, p, 0);
Joe Subbiani2f98d792021-08-20 11:44:44 +01005518 p += 4;
Manuel Pégourié-Gonnard35eb8022019-05-16 11:11:08 +02005519 }
5520#endif /* MBEDTLS_SSL_SESSION_TICKETS && MBEDTLS_SSL_CLI_C */
5521
Manuel Pégourié-Gonnardf743c032019-05-24 12:06:29 +02005522 /*
5523 * Misc extension-related info
5524 */
5525#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
5526 used += 1;
5527
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005528 if (used <= buf_len) {
Manuel Pégourié-Gonnardf743c032019-05-24 12:06:29 +02005529 *p++ = session->mfl_code;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005530 }
Manuel Pégourié-Gonnardf743c032019-05-24 12:06:29 +02005531#endif
5532
5533#if defined(MBEDTLS_SSL_TRUNCATED_HMAC)
5534 used += 1;
5535
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005536 if (used <= buf_len) {
5537 *p++ = (unsigned char) ((session->trunc_hmac) & 0xFF);
5538 }
Manuel Pégourié-Gonnardf743c032019-05-24 12:06:29 +02005539#endif
5540
5541#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
5542 used += 1;
5543
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005544 if (used <= buf_len) {
5545 *p++ = MBEDTLS_BYTE_0(session->encrypt_then_mac);
5546 }
Manuel Pégourié-Gonnardf743c032019-05-24 12:06:29 +02005547#endif
5548
Manuel Pégourié-Gonnard35eb8022019-05-16 11:11:08 +02005549 /* Done */
Manuel Pégourié-Gonnard26f982f2019-05-21 11:01:32 +02005550 *olen = used;
5551
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005552 if (used > buf_len) {
5553 return MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL;
5554 }
Manuel Pégourié-Gonnarda3e7c652019-05-16 10:08:35 +02005555
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005556 return 0;
Manuel Pégourié-Gonnarda3e7c652019-05-16 10:08:35 +02005557}
5558
5559/*
Manuel Pégourié-Gonnard45ac1f02019-07-23 16:52:45 +02005560 * Public wrapper for ssl_session_save()
5561 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005562int mbedtls_ssl_session_save(const mbedtls_ssl_session *session,
5563 unsigned char *buf,
5564 size_t buf_len,
5565 size_t *olen)
Manuel Pégourié-Gonnard45ac1f02019-07-23 16:52:45 +02005566{
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005567 return ssl_session_save(session, 0, buf, buf_len, olen);
Manuel Pégourié-Gonnard45ac1f02019-07-23 16:52:45 +02005568}
5569
5570/*
Manuel Pégourié-Gonnardb9dfc9f2019-07-12 10:50:19 +02005571 * Deserialize session, see mbedtls_ssl_session_save() for format.
Manuel Pégourié-Gonnarda3d831b2019-05-23 12:28:45 +02005572 *
5573 * This internal version is wrapped by a public function that cleans up in
Manuel Pégourié-Gonnard45ac1f02019-07-23 16:52:45 +02005574 * case of error, and has an extra option omit_header.
Manuel Pégourié-Gonnarda3e7c652019-05-16 10:08:35 +02005575 */
Manuel Pégourié-Gonnardd904d662022-06-17 10:24:00 +02005576MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005577static int ssl_session_load(mbedtls_ssl_session *session,
5578 unsigned char omit_header,
5579 const unsigned char *buf,
5580 size_t len)
Manuel Pégourié-Gonnarda3e7c652019-05-16 10:08:35 +02005581{
5582 const unsigned char *p = buf;
5583 const unsigned char * const end = buf + len;
Manuel Pégourié-Gonnardf743c032019-05-24 12:06:29 +02005584#if defined(MBEDTLS_HAVE_TIME)
5585 uint64_t start;
5586#endif
Manuel Pégourié-Gonnarda3e7c652019-05-16 10:08:35 +02005587#if defined(MBEDTLS_X509_CRT_PARSE_C)
5588#if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
5589 size_t cert_len;
Manuel Pégourié-Gonnarda3e7c652019-05-16 10:08:35 +02005590#endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
5591#endif /* MBEDTLS_X509_CRT_PARSE_C */
5592
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005593 if (!omit_header) {
Manuel Pégourié-Gonnard45ac1f02019-07-23 16:52:45 +02005594 /*
5595 * Check version identifier
5596 */
5597
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005598 if ((size_t) (end - p) < sizeof(ssl_serialized_session_header)) {
5599 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
Manuel Pégourié-Gonnard45ac1f02019-07-23 16:52:45 +02005600 }
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005601
5602 if (memcmp(p, ssl_serialized_session_header,
5603 sizeof(ssl_serialized_session_header)) != 0) {
5604 return MBEDTLS_ERR_SSL_VERSION_MISMATCH;
5605 }
5606 p += sizeof(ssl_serialized_session_header);
Hanno Beckera835da52019-05-16 12:39:07 +01005607 }
Hanno Beckera835da52019-05-16 12:39:07 +01005608
5609 /*
Manuel Pégourié-Gonnardf743c032019-05-24 12:06:29 +02005610 * Time
Manuel Pégourié-Gonnard35eb8022019-05-16 11:11:08 +02005611 */
Manuel Pégourié-Gonnardf743c032019-05-24 12:06:29 +02005612#if defined(MBEDTLS_HAVE_TIME)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005613 if (8 > (size_t) (end - p)) {
5614 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
5615 }
Manuel Pégourié-Gonnarda3e7c652019-05-16 10:08:35 +02005616
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005617 start = ((uint64_t) p[0] << 56) |
5618 ((uint64_t) p[1] << 48) |
5619 ((uint64_t) p[2] << 40) |
5620 ((uint64_t) p[3] << 32) |
5621 ((uint64_t) p[4] << 24) |
5622 ((uint64_t) p[5] << 16) |
5623 ((uint64_t) p[6] << 8) |
5624 ((uint64_t) p[7]);
Manuel Pégourié-Gonnardf743c032019-05-24 12:06:29 +02005625 p += 8;
Manuel Pégourié-Gonnarda3e7c652019-05-16 10:08:35 +02005626
Manuel Pégourié-Gonnardf743c032019-05-24 12:06:29 +02005627 session->start = (time_t) start;
5628#endif /* MBEDTLS_HAVE_TIME */
5629
5630 /*
5631 * Basic mandatory fields
5632 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005633 if (2 + 1 + 1 + 32 + 48 + 4 > (size_t) (end - p)) {
5634 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
5635 }
Manuel Pégourié-Gonnardf743c032019-05-24 12:06:29 +02005636
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005637 session->ciphersuite = (p[0] << 8) | p[1];
Manuel Pégourié-Gonnardf743c032019-05-24 12:06:29 +02005638 p += 2;
5639
5640 session->compression = *p++;
5641
5642 session->id_len = *p++;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005643 memcpy(session->id, p, 32);
Manuel Pégourié-Gonnardf743c032019-05-24 12:06:29 +02005644 p += 32;
5645
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005646 memcpy(session->master, p, 48);
Manuel Pégourié-Gonnardf743c032019-05-24 12:06:29 +02005647 p += 48;
5648
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005649 session->verify_result = ((uint32_t) p[0] << 24) |
5650 ((uint32_t) p[1] << 16) |
5651 ((uint32_t) p[2] << 8) |
5652 ((uint32_t) p[3]);
Manuel Pégourié-Gonnardf743c032019-05-24 12:06:29 +02005653 p += 4;
5654
5655 /* Immediately clear invalid pointer values that have been read, in case
5656 * we exit early before we replaced them with valid ones. */
Manuel Pégourié-Gonnarda3e7c652019-05-16 10:08:35 +02005657#if defined(MBEDTLS_X509_CRT_PARSE_C)
5658#if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
5659 session->peer_cert = NULL;
5660#else
5661 session->peer_cert_digest = NULL;
5662#endif /* !MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
5663#endif /* MBEDTLS_X509_CRT_PARSE_C */
5664#if defined(MBEDTLS_SSL_SESSION_TICKETS) && defined(MBEDTLS_SSL_CLI_C)
5665 session->ticket = NULL;
5666#endif /* MBEDTLS_SSL_SESSION_TICKETS && MBEDTLS_SSL_CLI_C */
5667
Manuel Pégourié-Gonnard35eb8022019-05-16 11:11:08 +02005668 /*
5669 * Peer certificate
5670 */
Manuel Pégourié-Gonnarda3e7c652019-05-16 10:08:35 +02005671#if defined(MBEDTLS_X509_CRT_PARSE_C)
5672#if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
5673 /* Deserialize CRT from the end of the ticket. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005674 if (3 > (size_t) (end - p)) {
5675 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
5676 }
Manuel Pégourié-Gonnarda3e7c652019-05-16 10:08:35 +02005677
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005678 cert_len = (p[0] << 16) | (p[1] << 8) | p[2];
Manuel Pégourié-Gonnarda3e7c652019-05-16 10:08:35 +02005679 p += 3;
5680
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005681 if (cert_len != 0) {
Janos Follath865b3eb2019-12-16 11:46:15 +00005682 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnarda3e7c652019-05-16 10:08:35 +02005683
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005684 if (cert_len > (size_t) (end - p)) {
5685 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
5686 }
Manuel Pégourié-Gonnarda3e7c652019-05-16 10:08:35 +02005687
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005688 session->peer_cert = mbedtls_calloc(1, sizeof(mbedtls_x509_crt));
Manuel Pégourié-Gonnarda3e7c652019-05-16 10:08:35 +02005689
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005690 if (session->peer_cert == NULL) {
5691 return MBEDTLS_ERR_SSL_ALLOC_FAILED;
5692 }
Manuel Pégourié-Gonnarda3e7c652019-05-16 10:08:35 +02005693
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005694 mbedtls_x509_crt_init(session->peer_cert);
Manuel Pégourié-Gonnarda3e7c652019-05-16 10:08:35 +02005695
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005696 if ((ret = mbedtls_x509_crt_parse_der(session->peer_cert,
5697 p, cert_len)) != 0) {
5698 mbedtls_x509_crt_free(session->peer_cert);
5699 mbedtls_free(session->peer_cert);
Manuel Pégourié-Gonnarda3e7c652019-05-16 10:08:35 +02005700 session->peer_cert = NULL;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005701 return ret;
Manuel Pégourié-Gonnarda3e7c652019-05-16 10:08:35 +02005702 }
5703
5704 p += cert_len;
5705 }
5706#else /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
5707 /* Deserialize CRT digest from the end of the ticket. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005708 if (2 > (size_t) (end - p)) {
5709 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
5710 }
Manuel Pégourié-Gonnarda3e7c652019-05-16 10:08:35 +02005711
Manuel Pégourié-Gonnardf743c032019-05-24 12:06:29 +02005712 session->peer_cert_digest_type = (mbedtls_md_type_t) *p++;
5713 session->peer_cert_digest_len = (size_t) *p++;
Manuel Pégourié-Gonnarda3e7c652019-05-16 10:08:35 +02005714
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005715 if (session->peer_cert_digest_len != 0) {
Manuel Pégourié-Gonnardf743c032019-05-24 12:06:29 +02005716 const mbedtls_md_info_t *md_info =
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005717 mbedtls_md_info_from_type(session->peer_cert_digest_type);
5718 if (md_info == NULL) {
5719 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
5720 }
5721 if (session->peer_cert_digest_len != mbedtls_md_get_size(md_info)) {
5722 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
5723 }
Manuel Pégourié-Gonnarda3e7c652019-05-16 10:08:35 +02005724
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005725 if (session->peer_cert_digest_len > (size_t) (end - p)) {
5726 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
5727 }
Manuel Pégourié-Gonnardf743c032019-05-24 12:06:29 +02005728
5729 session->peer_cert_digest =
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005730 mbedtls_calloc(1, session->peer_cert_digest_len);
5731 if (session->peer_cert_digest == NULL) {
5732 return MBEDTLS_ERR_SSL_ALLOC_FAILED;
5733 }
Manuel Pégourié-Gonnarda3e7c652019-05-16 10:08:35 +02005734
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005735 memcpy(session->peer_cert_digest, p,
5736 session->peer_cert_digest_len);
Manuel Pégourié-Gonnardf743c032019-05-24 12:06:29 +02005737 p += session->peer_cert_digest_len;
Manuel Pégourié-Gonnarda3e7c652019-05-16 10:08:35 +02005738 }
5739#endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
5740#endif /* MBEDTLS_X509_CRT_PARSE_C */
5741
Manuel Pégourié-Gonnard35eb8022019-05-16 11:11:08 +02005742 /*
Manuel Pégourié-Gonnardf743c032019-05-24 12:06:29 +02005743 * Session ticket and associated data
Manuel Pégourié-Gonnard35eb8022019-05-16 11:11:08 +02005744 */
5745#if defined(MBEDTLS_SSL_SESSION_TICKETS) && defined(MBEDTLS_SSL_CLI_C)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005746 if (3 > (size_t) (end - p)) {
5747 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
5748 }
Manuel Pégourié-Gonnard35eb8022019-05-16 11:11:08 +02005749
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005750 session->ticket_len = (p[0] << 16) | (p[1] << 8) | p[2];
Manuel Pégourié-Gonnard35eb8022019-05-16 11:11:08 +02005751 p += 3;
5752
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005753 if (session->ticket_len != 0) {
5754 if (session->ticket_len > (size_t) (end - p)) {
5755 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
5756 }
Manuel Pégourié-Gonnard35eb8022019-05-16 11:11:08 +02005757
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005758 session->ticket = mbedtls_calloc(1, session->ticket_len);
5759 if (session->ticket == NULL) {
5760 return MBEDTLS_ERR_SSL_ALLOC_FAILED;
5761 }
Manuel Pégourié-Gonnard35eb8022019-05-16 11:11:08 +02005762
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005763 memcpy(session->ticket, p, session->ticket_len);
Manuel Pégourié-Gonnard35eb8022019-05-16 11:11:08 +02005764 p += session->ticket_len;
5765 }
Manuel Pégourié-Gonnardf743c032019-05-24 12:06:29 +02005766
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005767 if (4 > (size_t) (end - p)) {
5768 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
5769 }
Manuel Pégourié-Gonnardf743c032019-05-24 12:06:29 +02005770
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005771 session->ticket_lifetime = ((uint32_t) p[0] << 24) |
5772 ((uint32_t) p[1] << 16) |
5773 ((uint32_t) p[2] << 8) |
5774 ((uint32_t) p[3]);
Manuel Pégourié-Gonnardf743c032019-05-24 12:06:29 +02005775 p += 4;
Manuel Pégourié-Gonnard35eb8022019-05-16 11:11:08 +02005776#endif /* MBEDTLS_SSL_SESSION_TICKETS && MBEDTLS_SSL_CLI_C */
5777
Manuel Pégourié-Gonnardf743c032019-05-24 12:06:29 +02005778 /*
5779 * Misc extension-related info
5780 */
5781#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005782 if (1 > (size_t) (end - p)) {
5783 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
5784 }
Manuel Pégourié-Gonnardf743c032019-05-24 12:06:29 +02005785
5786 session->mfl_code = *p++;
5787#endif
5788
5789#if defined(MBEDTLS_SSL_TRUNCATED_HMAC)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005790 if (1 > (size_t) (end - p)) {
5791 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
5792 }
Manuel Pégourié-Gonnardf743c032019-05-24 12:06:29 +02005793
5794 session->trunc_hmac = *p++;
5795#endif
5796
5797#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005798 if (1 > (size_t) (end - p)) {
5799 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
5800 }
Manuel Pégourié-Gonnardf743c032019-05-24 12:06:29 +02005801
5802 session->encrypt_then_mac = *p++;
5803#endif
5804
Manuel Pégourié-Gonnard35eb8022019-05-16 11:11:08 +02005805 /* Done, should have consumed entire buffer */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005806 if (p != end) {
5807 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
5808 }
Manuel Pégourié-Gonnarda3e7c652019-05-16 10:08:35 +02005809
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005810 return 0;
Manuel Pégourié-Gonnarda3e7c652019-05-16 10:08:35 +02005811}
5812
5813/*
Manuel Pégourié-Gonnardb9dfc9f2019-07-12 10:50:19 +02005814 * Deserialize session: public wrapper for error cleaning
Manuel Pégourié-Gonnarda3d831b2019-05-23 12:28:45 +02005815 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005816int mbedtls_ssl_session_load(mbedtls_ssl_session *session,
5817 const unsigned char *buf,
5818 size_t len)
Manuel Pégourié-Gonnarda3d831b2019-05-23 12:28:45 +02005819{
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005820 int ret = ssl_session_load(session, 0, buf, len);
Manuel Pégourié-Gonnarda3d831b2019-05-23 12:28:45 +02005821
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005822 if (ret != 0) {
5823 mbedtls_ssl_session_free(session);
5824 }
Manuel Pégourié-Gonnarda3d831b2019-05-23 12:28:45 +02005825
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005826 return ret;
Manuel Pégourié-Gonnarda3d831b2019-05-23 12:28:45 +02005827}
5828
5829/*
Paul Bakker1961b702013-01-25 14:49:24 +01005830 * Perform a single step of the SSL handshake
Paul Bakker5121ce52009-01-03 21:22:43 +00005831 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005832int mbedtls_ssl_handshake_step(mbedtls_ssl_context *ssl)
Paul Bakker5121ce52009-01-03 21:22:43 +00005833{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005834 int ret = MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
Paul Bakker5121ce52009-01-03 21:22:43 +00005835
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005836 if (ssl == NULL || ssl->conf == NULL) {
5837 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
5838 }
Manuel Pégourié-Gonnardf81ee2e2015-09-01 17:43:40 +02005839
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005840#if defined(MBEDTLS_SSL_CLI_C)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005841 if (ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT) {
5842 ret = mbedtls_ssl_handshake_client_step(ssl);
5843 }
Paul Bakker5121ce52009-01-03 21:22:43 +00005844#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005845#if defined(MBEDTLS_SSL_SRV_C)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005846 if (ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER) {
5847 ret = mbedtls_ssl_handshake_server_step(ssl);
5848 }
Paul Bakker5121ce52009-01-03 21:22:43 +00005849#endif
5850
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005851 return ret;
Paul Bakker1961b702013-01-25 14:49:24 +01005852}
5853
5854/*
5855 * Perform the SSL handshake
5856 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005857int mbedtls_ssl_handshake(mbedtls_ssl_context *ssl)
Paul Bakker1961b702013-01-25 14:49:24 +01005858{
5859 int ret = 0;
5860
Hanno Beckera817ea42020-10-20 15:20:23 +01005861 /* Sanity checks */
5862
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005863 if (ssl == NULL || ssl->conf == NULL) {
5864 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
5865 }
Manuel Pégourié-Gonnardf81ee2e2015-09-01 17:43:40 +02005866
Hanno Beckera817ea42020-10-20 15:20:23 +01005867#if defined(MBEDTLS_SSL_PROTO_DTLS)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005868 if (ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
5869 (ssl->f_set_timer == NULL || ssl->f_get_timer == NULL)) {
5870 MBEDTLS_SSL_DEBUG_MSG(1, ("You must use "
5871 "mbedtls_ssl_set_timer_cb() for DTLS"));
5872 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
Hanno Beckera817ea42020-10-20 15:20:23 +01005873 }
5874#endif /* MBEDTLS_SSL_PROTO_DTLS */
5875
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005876 MBEDTLS_SSL_DEBUG_MSG(2, ("=> handshake"));
Paul Bakker1961b702013-01-25 14:49:24 +01005877
Hanno Beckera817ea42020-10-20 15:20:23 +01005878 /* Main handshake loop */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005879 while (ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER) {
5880 ret = mbedtls_ssl_handshake_step(ssl);
Paul Bakker1961b702013-01-25 14:49:24 +01005881
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005882 if (ret != 0) {
Paul Bakker1961b702013-01-25 14:49:24 +01005883 break;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005884 }
Paul Bakker1961b702013-01-25 14:49:24 +01005885 }
5886
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005887 MBEDTLS_SSL_DEBUG_MSG(2, ("<= handshake"));
Paul Bakker5121ce52009-01-03 21:22:43 +00005888
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005889 return ret;
Paul Bakker5121ce52009-01-03 21:22:43 +00005890}
5891
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005892#if defined(MBEDTLS_SSL_RENEGOTIATION)
5893#if defined(MBEDTLS_SSL_SRV_C)
Paul Bakker5121ce52009-01-03 21:22:43 +00005894/*
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01005895 * Write HelloRequest to request renegotiation on server
Paul Bakker48916f92012-09-16 19:57:18 +00005896 */
Manuel Pégourié-Gonnardd904d662022-06-17 10:24:00 +02005897MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005898static int ssl_write_hello_request(mbedtls_ssl_context *ssl)
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01005899{
Janos Follath865b3eb2019-12-16 11:46:15 +00005900 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01005901
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005902 MBEDTLS_SSL_DEBUG_MSG(2, ("=> write hello request"));
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01005903
5904 ssl->out_msglen = 4;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005905 ssl->out_msgtype = MBEDTLS_SSL_MSG_HANDSHAKE;
5906 ssl->out_msg[0] = MBEDTLS_SSL_HS_HELLO_REQUEST;
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01005907
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005908 if ((ret = mbedtls_ssl_write_handshake_msg(ssl)) != 0) {
5909 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ssl_write_handshake_msg", ret);
5910 return ret;
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01005911 }
5912
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005913 MBEDTLS_SSL_DEBUG_MSG(2, ("<= write hello request"));
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01005914
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005915 return 0;
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01005916}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005917#endif /* MBEDTLS_SSL_SRV_C */
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01005918
5919/*
5920 * Actually renegotiate current connection, triggered by either:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005921 * - any side: calling mbedtls_ssl_renegotiate(),
5922 * - client: receiving a HelloRequest during mbedtls_ssl_read(),
5923 * - server: receiving any handshake message on server during mbedtls_ssl_read() after
Manuel Pégourié-Gonnard55e4ff22014-08-19 11:16:35 +02005924 * the initial handshake is completed.
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01005925 * If the handshake doesn't complete due to waiting for I/O, it will continue
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005926 * during the next calls to mbedtls_ssl_renegotiate() or mbedtls_ssl_read() respectively.
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01005927 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005928int mbedtls_ssl_start_renegotiation(mbedtls_ssl_context *ssl)
Paul Bakker48916f92012-09-16 19:57:18 +00005929{
Janos Follath865b3eb2019-12-16 11:46:15 +00005930 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker48916f92012-09-16 19:57:18 +00005931
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005932 MBEDTLS_SSL_DEBUG_MSG(2, ("=> renegotiate"));
Paul Bakker48916f92012-09-16 19:57:18 +00005933
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005934 if ((ret = ssl_handshake_init(ssl)) != 0) {
5935 return ret;
5936 }
Paul Bakker48916f92012-09-16 19:57:18 +00005937
Manuel Pégourié-Gonnard0557bd52014-08-19 19:18:39 +02005938 /* RFC 6347 4.2.2: "[...] the HelloRequest will have message_seq = 0 and
5939 * the ServerHello will have message_seq = 1" */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005940#if defined(MBEDTLS_SSL_PROTO_DTLS)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005941 if (ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
5942 ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_PENDING) {
5943 if (ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER) {
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02005944 ssl->handshake->out_msg_seq = 1;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005945 } else {
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02005946 ssl->handshake->in_msg_seq = 1;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005947 }
Manuel Pégourié-Gonnard0557bd52014-08-19 19:18:39 +02005948 }
5949#endif
5950
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005951 ssl->state = MBEDTLS_SSL_HELLO_REQUEST;
5952 ssl->renego_status = MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS;
Paul Bakker48916f92012-09-16 19:57:18 +00005953
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005954 if ((ret = mbedtls_ssl_handshake(ssl)) != 0) {
5955 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ssl_handshake", ret);
5956 return ret;
Paul Bakker48916f92012-09-16 19:57:18 +00005957 }
5958
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005959 MBEDTLS_SSL_DEBUG_MSG(2, ("<= renegotiate"));
Paul Bakker48916f92012-09-16 19:57:18 +00005960
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005961 return 0;
Paul Bakker48916f92012-09-16 19:57:18 +00005962}
5963
5964/*
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01005965 * Renegotiate current connection on client,
5966 * or request renegotiation on server
5967 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005968int mbedtls_ssl_renegotiate(mbedtls_ssl_context *ssl)
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01005969{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005970 int ret = MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01005971
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005972 if (ssl == NULL || ssl->conf == NULL) {
5973 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
5974 }
Manuel Pégourié-Gonnardf81ee2e2015-09-01 17:43:40 +02005975
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005976#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01005977 /* On server, just send the request */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005978 if (ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER) {
5979 if (ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER) {
5980 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
5981 }
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01005982
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005983 ssl->renego_status = MBEDTLS_SSL_RENEGOTIATION_PENDING;
Manuel Pégourié-Gonnardf07f4212014-08-15 19:04:47 +02005984
5985 /* Did we already try/start sending HelloRequest? */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005986 if (ssl->out_left != 0) {
5987 return mbedtls_ssl_flush_output(ssl);
5988 }
Manuel Pégourié-Gonnardf07f4212014-08-15 19:04:47 +02005989
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005990 return ssl_write_hello_request(ssl);
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01005991 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005992#endif /* MBEDTLS_SSL_SRV_C */
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01005993
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005994#if defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01005995 /*
5996 * On client, either start the renegotiation process or,
5997 * if already in progress, continue the handshake
5998 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005999 if (ssl->renego_status != MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS) {
6000 if (ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER) {
6001 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01006002 }
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01006003
6004 if ((ret = mbedtls_ssl_start_renegotiation(ssl)) != 0) {
6005 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ssl_start_renegotiation", ret);
6006 return ret;
6007 }
6008 } else {
6009 if ((ret = mbedtls_ssl_handshake(ssl)) != 0) {
6010 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ssl_handshake", ret);
6011 return ret;
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01006012 }
6013 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006014#endif /* MBEDTLS_SSL_CLI_C */
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01006015
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01006016 return ret;
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01006017}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006018#endif /* MBEDTLS_SSL_RENEGOTIATION */
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01006019
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006020#if defined(MBEDTLS_X509_CRT_PARSE_C)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01006021static void ssl_key_cert_free(mbedtls_ssl_key_cert *key_cert)
Manuel Pégourié-Gonnard705fcca2013-09-23 20:04:20 +02006022{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006023 mbedtls_ssl_key_cert *cur = key_cert, *next;
Manuel Pégourié-Gonnard705fcca2013-09-23 20:04:20 +02006024
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01006025 while (cur != NULL) {
Manuel Pégourié-Gonnard705fcca2013-09-23 20:04:20 +02006026 next = cur->next;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01006027 mbedtls_free(cur);
Manuel Pégourié-Gonnard705fcca2013-09-23 20:04:20 +02006028 cur = next;
6029 }
6030}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006031#endif /* MBEDTLS_X509_CRT_PARSE_C */
Manuel Pégourié-Gonnard705fcca2013-09-23 20:04:20 +02006032
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01006033void mbedtls_ssl_handshake_free(mbedtls_ssl_context *ssl)
Paul Bakker48916f92012-09-16 19:57:18 +00006034{
Gilles Peskine9b562d52018-04-25 20:32:43 +02006035 mbedtls_ssl_handshake_params *handshake = ssl->handshake;
6036
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01006037 if (handshake == NULL) {
Paul Bakkeraccaffe2014-06-26 13:37:14 +02006038 return;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01006039 }
Paul Bakkeraccaffe2014-06-26 13:37:14 +02006040
Gilles Peskinedf13d5c2018-04-25 20:39:48 +02006041#if defined(MBEDTLS_SSL_ASYNC_PRIVATE)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01006042 if (ssl->conf->f_async_cancel != NULL && handshake->async_in_progress != 0) {
6043 ssl->conf->f_async_cancel(ssl);
Gilles Peskinedf13d5c2018-04-25 20:39:48 +02006044 handshake->async_in_progress = 0;
6045 }
6046#endif /* MBEDTLS_SSL_ASYNC_PRIVATE */
6047
Manuel Pégourié-Gonnardb9d64e52015-07-06 14:18:56 +02006048#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1) || \
6049 defined(MBEDTLS_SSL_PROTO_TLS1_1)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01006050 mbedtls_md5_free(&handshake->fin_md5);
6051 mbedtls_sha1_free(&handshake->fin_sha1);
Manuel Pégourié-Gonnardb9d64e52015-07-06 14:18:56 +02006052#endif
6053#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
6054#if defined(MBEDTLS_SHA256_C)
Andrzej Kurekeb342242019-01-29 09:14:33 -05006055#if defined(MBEDTLS_USE_PSA_CRYPTO)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01006056 psa_hash_abort(&handshake->fin_sha256_psa);
Andrzej Kurekeb342242019-01-29 09:14:33 -05006057#else
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01006058 mbedtls_sha256_free(&handshake->fin_sha256);
Manuel Pégourié-Gonnardb9d64e52015-07-06 14:18:56 +02006059#endif
Andrzej Kurekeb342242019-01-29 09:14:33 -05006060#endif
Gilles Peskined2d59372021-05-12 22:43:27 +02006061#if defined(MBEDTLS_SHA512_C) && !defined(MBEDTLS_SHA512_NO_SHA384)
Andrzej Kurekeb342242019-01-29 09:14:33 -05006062#if defined(MBEDTLS_USE_PSA_CRYPTO)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01006063 psa_hash_abort(&handshake->fin_sha384_psa);
Andrzej Kurekeb342242019-01-29 09:14:33 -05006064#else
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01006065 mbedtls_sha512_free(&handshake->fin_sha512);
Manuel Pégourié-Gonnardb9d64e52015-07-06 14:18:56 +02006066#endif
Andrzej Kurekeb342242019-01-29 09:14:33 -05006067#endif
Manuel Pégourié-Gonnardb9d64e52015-07-06 14:18:56 +02006068#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
6069
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006070#if defined(MBEDTLS_DHM_C)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01006071 mbedtls_dhm_free(&handshake->dhm_ctx);
Paul Bakker48916f92012-09-16 19:57:18 +00006072#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006073#if defined(MBEDTLS_ECDH_C)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01006074 mbedtls_ecdh_free(&handshake->ecdh_ctx);
Paul Bakker61d113b2013-07-04 11:51:43 +02006075#endif
Manuel Pégourié-Gonnardeef142d2015-09-16 10:05:04 +02006076#if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01006077 mbedtls_ecjpake_free(&handshake->ecjpake_ctx);
Manuel Pégourié-Gonnard77c06462015-09-17 13:59:49 +02006078#if defined(MBEDTLS_SSL_CLI_C)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01006079 mbedtls_free(handshake->ecjpake_cache);
Manuel Pégourié-Gonnard77c06462015-09-17 13:59:49 +02006080 handshake->ecjpake_cache = NULL;
6081 handshake->ecjpake_cache_len = 0;
6082#endif
Manuel Pégourié-Gonnard76cfd3f2015-09-15 12:10:54 +02006083#endif
Paul Bakker61d113b2013-07-04 11:51:43 +02006084
Janos Follath4ae5c292016-02-10 11:27:43 +00006085#if defined(MBEDTLS_ECDH_C) || defined(MBEDTLS_ECDSA_C) || \
6086 defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
Paul Bakker9af723c2014-05-01 13:03:14 +02006087 /* explicit void pointer cast for buggy MS compiler */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01006088 mbedtls_free((void *) handshake->curves);
Manuel Pégourié-Gonnardd09453c2013-09-23 19:11:32 +02006089#endif
6090
Gilles Peskineeccd8882020-03-10 12:19:08 +01006091#if defined(MBEDTLS_KEY_EXCHANGE_SOME_PSK_ENABLED)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01006092 if (handshake->psk != NULL) {
6093 mbedtls_platform_zeroize(handshake->psk, handshake->psk_len);
6094 mbedtls_free(handshake->psk);
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01006095 }
6096#endif
6097
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006098#if defined(MBEDTLS_X509_CRT_PARSE_C) && \
6099 defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
Manuel Pégourié-Gonnard83724542013-09-24 22:30:56 +02006100 /*
6101 * Free only the linked list wrapper, not the keys themselves
6102 * since the belong to the SNI callback
6103 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01006104 if (handshake->sni_key_cert != NULL) {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006105 mbedtls_ssl_key_cert *cur = handshake->sni_key_cert, *next;
Manuel Pégourié-Gonnard83724542013-09-24 22:30:56 +02006106
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01006107 while (cur != NULL) {
Manuel Pégourié-Gonnard83724542013-09-24 22:30:56 +02006108 next = cur->next;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01006109 mbedtls_free(cur);
Manuel Pégourié-Gonnard83724542013-09-24 22:30:56 +02006110 cur = next;
6111 }
6112 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006113#endif /* MBEDTLS_X509_CRT_PARSE_C && MBEDTLS_SSL_SERVER_NAME_INDICATION */
Manuel Pégourié-Gonnard705fcca2013-09-23 20:04:20 +02006114
Gilles Peskineeccd8882020-03-10 12:19:08 +01006115#if defined(MBEDTLS_SSL_ECP_RESTARTABLE_ENABLED)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01006116 mbedtls_x509_crt_restart_free(&handshake->ecrs_ctx);
6117 if (handshake->ecrs_peer_cert != NULL) {
6118 mbedtls_x509_crt_free(handshake->ecrs_peer_cert);
6119 mbedtls_free(handshake->ecrs_peer_cert);
Hanno Becker3dad3112019-02-05 17:19:52 +00006120 }
Manuel Pégourié-Gonnard862cde52017-05-17 11:56:15 +02006121#endif
6122
Hanno Becker75173122019-02-06 16:18:31 +00006123#if defined(MBEDTLS_X509_CRT_PARSE_C) && \
6124 !defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01006125 mbedtls_pk_free(&handshake->peer_pubkey);
Hanno Becker75173122019-02-06 16:18:31 +00006126#endif /* MBEDTLS_X509_CRT_PARSE_C && !MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
6127
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006128#if defined(MBEDTLS_SSL_PROTO_DTLS)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01006129 mbedtls_free(handshake->verify_cookie);
6130 mbedtls_ssl_flight_free(handshake->flight);
6131 mbedtls_ssl_buffering_free(ssl);
Manuel Pégourié-Gonnard74848812014-07-11 02:43:49 +02006132#endif
6133
Hanno Becker4a63ed42019-01-08 11:39:35 +00006134#if defined(MBEDTLS_ECDH_C) && \
6135 defined(MBEDTLS_USE_PSA_CRYPTO)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01006136 psa_destroy_key(handshake->ecdh_psa_privkey);
Hanno Becker4a63ed42019-01-08 11:39:35 +00006137#endif /* MBEDTLS_ECDH_C && MBEDTLS_USE_PSA_CRYPTO */
6138
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01006139 mbedtls_platform_zeroize(handshake,
6140 sizeof(mbedtls_ssl_handshake_params));
Andrzej Kurek0afa2a12020-03-03 10:39:58 -05006141
6142#if defined(MBEDTLS_SSL_VARIABLE_BUFFER_LENGTH)
6143 /* If the buffers are too big - reallocate. Because of the way Mbed TLS
6144 * processes datagrams and the fact that a datagram is allowed to have
6145 * several records in it, it is possible that the I/O buffers are not
6146 * empty at this stage */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01006147 handle_buffer_resizing(ssl, 1, mbedtls_ssl_get_input_buflen(ssl),
6148 mbedtls_ssl_get_output_buflen(ssl));
Andrzej Kurek0afa2a12020-03-03 10:39:58 -05006149#endif
Paul Bakker48916f92012-09-16 19:57:18 +00006150}
6151
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01006152void mbedtls_ssl_session_free(mbedtls_ssl_session *session)
Paul Bakker48916f92012-09-16 19:57:18 +00006153{
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01006154 if (session == NULL) {
Paul Bakkeraccaffe2014-06-26 13:37:14 +02006155 return;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01006156 }
Paul Bakkeraccaffe2014-06-26 13:37:14 +02006157
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006158#if defined(MBEDTLS_X509_CRT_PARSE_C)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01006159 ssl_clear_peer_cert(session);
Paul Bakkered27a042013-04-18 22:46:23 +02006160#endif
Paul Bakker0a597072012-09-25 21:55:46 +00006161
Manuel Pégourié-Gonnardb596abf2015-05-20 10:45:29 +02006162#if defined(MBEDTLS_SSL_SESSION_TICKETS) && defined(MBEDTLS_SSL_CLI_C)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01006163 mbedtls_free(session->ticket);
Paul Bakkera503a632013-08-14 13:48:06 +02006164#endif
Manuel Pégourié-Gonnard75d44012013-08-02 14:44:04 +02006165
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01006166 mbedtls_platform_zeroize(session, sizeof(mbedtls_ssl_session));
Paul Bakker48916f92012-09-16 19:57:18 +00006167}
6168
Manuel Pégourié-Gonnard5c0e3772019-07-23 16:13:17 +02006169#if defined(MBEDTLS_SSL_CONTEXT_SERIALIZATION)
Manuel Pégourié-Gonnard4e9370b2019-07-23 16:31:16 +02006170
6171#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
6172#define SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_CONNECTION_ID 1u
6173#else
6174#define SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_CONNECTION_ID 0u
6175#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
6176
6177#if defined(MBEDTLS_SSL_DTLS_BADMAC_LIMIT)
6178#define SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_BADMAC_LIMIT 1u
6179#else
6180#define SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_BADMAC_LIMIT 0u
6181#endif /* MBEDTLS_SSL_DTLS_BADMAC_LIMIT */
6182
6183#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
6184#define SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_ANTI_REPLAY 1u
6185#else
6186#define SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_ANTI_REPLAY 0u
6187#endif /* MBEDTLS_SSL_DTLS_ANTI_REPLAY */
6188
6189#if defined(MBEDTLS_SSL_ALPN)
6190#define SSL_SERIALIZED_CONTEXT_CONFIG_ALPN 1u
6191#else
6192#define SSL_SERIALIZED_CONTEXT_CONFIG_ALPN 0u
6193#endif /* MBEDTLS_SSL_ALPN */
6194
6195#define SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_CONNECTION_ID_BIT 0
6196#define SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_BADMAC_LIMIT_BIT 1
6197#define SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_ANTI_REPLAY_BIT 2
6198#define SSL_SERIALIZED_CONTEXT_CONFIG_ALPN_BIT 3
6199
6200#define SSL_SERIALIZED_CONTEXT_CONFIG_BITFLAG \
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01006201 ((uint32_t) ( \
6202 (SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_CONNECTION_ID << \
6203 SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_CONNECTION_ID_BIT) | \
6204 (SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_BADMAC_LIMIT << \
6205 SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_BADMAC_LIMIT_BIT) | \
6206 (SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_ANTI_REPLAY << \
6207 SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_ANTI_REPLAY_BIT) | \
6208 (SSL_SERIALIZED_CONTEXT_CONFIG_ALPN << SSL_SERIALIZED_CONTEXT_CONFIG_ALPN_BIT) | \
6209 0u))
Manuel Pégourié-Gonnard4e9370b2019-07-23 16:31:16 +02006210
Chien Wongb6d57932024-02-07 21:48:12 +08006211static const unsigned char ssl_serialized_context_header[] = {
Manuel Pégourié-Gonnard4c90e852019-07-11 10:58:10 +02006212 MBEDTLS_VERSION_MAJOR,
6213 MBEDTLS_VERSION_MINOR,
6214 MBEDTLS_VERSION_PATCH,
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01006215 MBEDTLS_BYTE_1(SSL_SERIALIZED_SESSION_CONFIG_BITFLAG),
6216 MBEDTLS_BYTE_0(SSL_SERIALIZED_SESSION_CONFIG_BITFLAG),
6217 MBEDTLS_BYTE_2(SSL_SERIALIZED_CONTEXT_CONFIG_BITFLAG),
6218 MBEDTLS_BYTE_1(SSL_SERIALIZED_CONTEXT_CONFIG_BITFLAG),
6219 MBEDTLS_BYTE_0(SSL_SERIALIZED_CONTEXT_CONFIG_BITFLAG),
Manuel Pégourié-Gonnard4c90e852019-07-11 10:58:10 +02006220};
6221
Paul Bakker5121ce52009-01-03 21:22:43 +00006222/*
Manuel Pégourié-Gonnardac87e282019-05-28 13:02:16 +02006223 * Serialize a full SSL context
Manuel Pégourié-Gonnard00400c22019-07-10 14:58:45 +02006224 *
6225 * The format of the serialized data is:
6226 * (in the presentation language of TLS, RFC 8446 section 3)
6227 *
6228 * // header
6229 * opaque mbedtls_version[3]; // major, minor, patch
Manuel Pégourié-Gonnard4c90e852019-07-11 10:58:10 +02006230 * opaque context_format[5]; // version-specific field determining
Manuel Pégourié-Gonnard00400c22019-07-10 14:58:45 +02006231 * // the format of the remaining
Manuel Pégourié-Gonnard4c90e852019-07-11 10:58:10 +02006232 * // serialized data.
Manuel Pégourié-Gonnard4e9370b2019-07-23 16:31:16 +02006233 * Note: When updating the format, remember to keep these
6234 * version+format bytes. (We may make their size part of the API.)
Manuel Pégourié-Gonnard00400c22019-07-10 14:58:45 +02006235 *
6236 * // session sub-structure
6237 * opaque session<1..2^32-1>; // see mbedtls_ssl_session_save()
6238 * // transform sub-structure
6239 * uint8 random[64]; // ServerHello.random+ClientHello.random
6240 * uint8 in_cid<0..2^8-1> // Connection ID: expected incoming value
6241 * uint8 out_cid<0..2^8-1> // Connection ID: outgoing value to use
6242 * // fields from ssl_context
6243 * uint32 badmac_seen; // DTLS: number of records with failing MAC
6244 * uint64 in_window_top; // DTLS: last validated record seq_num
6245 * uint64 in_window; // DTLS: bitmask for replay protection
6246 * uint8 disable_datagram_packing; // DTLS: only one record per datagram
6247 * uint64 cur_out_ctr; // Record layer: outgoing sequence number
6248 * uint16 mtu; // DTLS: path mtu (max outgoing fragment size)
6249 * uint8 alpn_chosen<0..2^8-1> // ALPN: negotiated application protocol
6250 *
6251 * Note that many fields of the ssl_context or sub-structures are not
6252 * serialized, as they fall in one of the following categories:
6253 *
6254 * 1. forced value (eg in_left must be 0)
6255 * 2. pointer to dynamically-allocated memory (eg session, transform)
6256 * 3. value can be re-derived from other data (eg session keys from MS)
6257 * 4. value was temporary (eg content of input buffer)
6258 * 5. value will be provided by the user again (eg I/O callbacks and context)
Manuel Pégourié-Gonnardac87e282019-05-28 13:02:16 +02006259 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01006260int mbedtls_ssl_context_save(mbedtls_ssl_context *ssl,
6261 unsigned char *buf,
6262 size_t buf_len,
6263 size_t *olen)
Manuel Pégourié-Gonnardac87e282019-05-28 13:02:16 +02006264{
Manuel Pégourié-Gonnard4c90e852019-07-11 10:58:10 +02006265 unsigned char *p = buf;
6266 size_t used = 0;
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02006267 size_t session_len;
6268 int ret = 0;
Manuel Pégourié-Gonnard4c90e852019-07-11 10:58:10 +02006269
Manuel Pégourié-Gonnard1aaf6692019-07-10 14:14:05 +02006270 /*
Manuel Pégourié-Gonnarde4588692019-07-29 12:28:52 +02006271 * Enforce usage restrictions, see "return BAD_INPUT_DATA" in
6272 * this function's documentation.
6273 *
6274 * These are due to assumptions/limitations in the implementation. Some of
6275 * them are likely to stay (no handshake in progress) some might go away
6276 * (only DTLS) but are currently used to simplify the implementation.
Manuel Pégourié-Gonnard1aaf6692019-07-10 14:14:05 +02006277 */
Manuel Pégourié-Gonnarde4588692019-07-29 12:28:52 +02006278 /* The initial handshake must be over */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01006279 if (ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER) {
6280 MBEDTLS_SSL_DEBUG_MSG(1, ("Initial handshake isn't over"));
6281 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
Jarno Lamsa8c51b7c2019-08-21 13:45:05 +03006282 }
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01006283 if (ssl->handshake != NULL) {
6284 MBEDTLS_SSL_DEBUG_MSG(1, ("Handshake isn't completed"));
6285 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
Jarno Lamsa8c51b7c2019-08-21 13:45:05 +03006286 }
Manuel Pégourié-Gonnarde4588692019-07-29 12:28:52 +02006287 /* Double-check that sub-structures are indeed ready */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01006288 if (ssl->transform == NULL || ssl->session == NULL) {
6289 MBEDTLS_SSL_DEBUG_MSG(1, ("Serialised structures aren't ready"));
6290 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
Jarno Lamsa8c51b7c2019-08-21 13:45:05 +03006291 }
Manuel Pégourié-Gonnarde4588692019-07-29 12:28:52 +02006292 /* There must be no pending incoming or outgoing data */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01006293 if (mbedtls_ssl_check_pending(ssl) != 0) {
6294 MBEDTLS_SSL_DEBUG_MSG(1, ("There is pending incoming data"));
6295 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
Jarno Lamsa8c51b7c2019-08-21 13:45:05 +03006296 }
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01006297 if (ssl->out_left != 0) {
6298 MBEDTLS_SSL_DEBUG_MSG(1, ("There is pending outgoing data"));
6299 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
Jarno Lamsa8c51b7c2019-08-21 13:45:05 +03006300 }
Dave Rodgmana03396a2022-12-06 16:30:38 +00006301 /* Protocol must be DTLS, not TLS */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01006302 if (ssl->conf->transport != MBEDTLS_SSL_TRANSPORT_DATAGRAM) {
6303 MBEDTLS_SSL_DEBUG_MSG(1, ("Only DTLS is supported"));
6304 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
Jarno Lamsa8c51b7c2019-08-21 13:45:05 +03006305 }
Manuel Pégourié-Gonnarde4588692019-07-29 12:28:52 +02006306 /* Version must be 1.2 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01006307 if (ssl->major_ver != MBEDTLS_SSL_MAJOR_VERSION_3) {
6308 MBEDTLS_SSL_DEBUG_MSG(1, ("Only version 1.2 supported"));
6309 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
Jarno Lamsa8c51b7c2019-08-21 13:45:05 +03006310 }
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01006311 if (ssl->minor_ver != MBEDTLS_SSL_MINOR_VERSION_3) {
6312 MBEDTLS_SSL_DEBUG_MSG(1, ("Only version 1.2 supported"));
6313 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
Jarno Lamsa8c51b7c2019-08-21 13:45:05 +03006314 }
Manuel Pégourié-Gonnarde4588692019-07-29 12:28:52 +02006315 /* We must be using an AEAD ciphersuite */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01006316 if (mbedtls_ssl_transform_uses_aead(ssl->transform) != 1) {
6317 MBEDTLS_SSL_DEBUG_MSG(1, ("Only AEAD ciphersuites supported"));
6318 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
Jarno Lamsa8c51b7c2019-08-21 13:45:05 +03006319 }
Manuel Pégourié-Gonnarde4588692019-07-29 12:28:52 +02006320 /* Renegotiation must not be enabled */
6321#if defined(MBEDTLS_SSL_RENEGOTIATION)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01006322 if (ssl->conf->disable_renegotiation != MBEDTLS_SSL_RENEGOTIATION_DISABLED) {
6323 MBEDTLS_SSL_DEBUG_MSG(1, ("Renegotiation must not be enabled"));
6324 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
Jarno Lamsa8c51b7c2019-08-21 13:45:05 +03006325 }
Manuel Pégourié-Gonnarde4588692019-07-29 12:28:52 +02006326#endif
Manuel Pégourié-Gonnardac87e282019-05-28 13:02:16 +02006327
Manuel Pégourié-Gonnard4c90e852019-07-11 10:58:10 +02006328 /*
6329 * Version and format identifier
6330 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01006331 used += sizeof(ssl_serialized_context_header);
Manuel Pégourié-Gonnardac87e282019-05-28 13:02:16 +02006332
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01006333 if (used <= buf_len) {
6334 memcpy(p, ssl_serialized_context_header,
6335 sizeof(ssl_serialized_context_header));
6336 p += sizeof(ssl_serialized_context_header);
Manuel Pégourié-Gonnard4c90e852019-07-11 10:58:10 +02006337 }
6338
6339 /*
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02006340 * Session (length + data)
6341 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01006342 ret = ssl_session_save(ssl->session, 1, NULL, 0, &session_len);
6343 if (ret != MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL) {
6344 return ret;
6345 }
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02006346
6347 used += 4 + session_len;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01006348 if (used <= buf_len) {
6349 MBEDTLS_PUT_UINT32_BE(session_len, p, 0);
Joe Subbiani2f98d792021-08-20 11:44:44 +01006350 p += 4;
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02006351
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01006352 ret = ssl_session_save(ssl->session, 1,
6353 p, session_len, &session_len);
6354 if (ret != 0) {
6355 return ret;
6356 }
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02006357
6358 p += session_len;
6359 }
6360
6361 /*
Manuel Pégourié-Gonnardc2a7b892019-07-15 09:04:11 +02006362 * Transform
6363 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01006364 used += sizeof(ssl->transform->randbytes);
6365 if (used <= buf_len) {
6366 memcpy(p, ssl->transform->randbytes,
6367 sizeof(ssl->transform->randbytes));
6368 p += sizeof(ssl->transform->randbytes);
Manuel Pégourié-Gonnardc2a7b892019-07-15 09:04:11 +02006369 }
6370
6371#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
6372 used += 2 + ssl->transform->in_cid_len + ssl->transform->out_cid_len;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01006373 if (used <= buf_len) {
Manuel Pégourié-Gonnardc2a7b892019-07-15 09:04:11 +02006374 *p++ = ssl->transform->in_cid_len;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01006375 memcpy(p, ssl->transform->in_cid, ssl->transform->in_cid_len);
Manuel Pégourié-Gonnardc2a7b892019-07-15 09:04:11 +02006376 p += ssl->transform->in_cid_len;
6377
6378 *p++ = ssl->transform->out_cid_len;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01006379 memcpy(p, ssl->transform->out_cid, ssl->transform->out_cid_len);
Manuel Pégourié-Gonnardc2a7b892019-07-15 09:04:11 +02006380 p += ssl->transform->out_cid_len;
6381 }
6382#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
6383
6384 /*
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02006385 * Saved fields from top-level ssl_context structure
6386 */
6387#if defined(MBEDTLS_SSL_DTLS_BADMAC_LIMIT)
6388 used += 4;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01006389 if (used <= buf_len) {
6390 MBEDTLS_PUT_UINT32_BE(ssl->badmac_seen, p, 0);
Joe Subbiani2f98d792021-08-20 11:44:44 +01006391 p += 4;
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02006392 }
6393#endif /* MBEDTLS_SSL_DTLS_BADMAC_LIMIT */
6394
6395#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
6396 used += 16;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01006397 if (used <= buf_len) {
6398 MBEDTLS_PUT_UINT64_BE(ssl->in_window_top, p, 0);
Joe Subbiani2f98d792021-08-20 11:44:44 +01006399 p += 8;
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02006400
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01006401 MBEDTLS_PUT_UINT64_BE(ssl->in_window, p, 0);
Joe Subbiani2f98d792021-08-20 11:44:44 +01006402 p += 8;
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02006403 }
6404#endif /* MBEDTLS_SSL_DTLS_ANTI_REPLAY */
6405
6406#if defined(MBEDTLS_SSL_PROTO_DTLS)
6407 used += 1;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01006408 if (used <= buf_len) {
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02006409 *p++ = ssl->disable_datagram_packing;
6410 }
6411#endif /* MBEDTLS_SSL_PROTO_DTLS */
6412
6413 used += 8;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01006414 if (used <= buf_len) {
6415 memcpy(p, ssl->cur_out_ctr, 8);
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02006416 p += 8;
6417 }
6418
6419#if defined(MBEDTLS_SSL_PROTO_DTLS)
6420 used += 2;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01006421 if (used <= buf_len) {
6422 MBEDTLS_PUT_UINT16_BE(ssl->mtu, p, 0);
Joe Subbiani2f98d792021-08-20 11:44:44 +01006423 p += 2;
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02006424 }
6425#endif /* MBEDTLS_SSL_PROTO_DTLS */
6426
6427#if defined(MBEDTLS_SSL_ALPN)
6428 {
6429 const uint8_t alpn_len = ssl->alpn_chosen
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01006430 ? (uint8_t) strlen(ssl->alpn_chosen)
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02006431 : 0;
6432
6433 used += 1 + alpn_len;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01006434 if (used <= buf_len) {
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02006435 *p++ = alpn_len;
6436
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01006437 if (ssl->alpn_chosen != NULL) {
6438 memcpy(p, ssl->alpn_chosen, alpn_len);
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02006439 p += alpn_len;
6440 }
6441 }
6442 }
6443#endif /* MBEDTLS_SSL_ALPN */
6444
6445 /*
Manuel Pégourié-Gonnard4c90e852019-07-11 10:58:10 +02006446 * Done
6447 */
6448 *olen = used;
6449
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01006450 if (used > buf_len) {
6451 return MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL;
6452 }
Manuel Pégourié-Gonnardac87e282019-05-28 13:02:16 +02006453
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01006454 MBEDTLS_SSL_DEBUG_BUF(4, "saved context", buf, used);
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02006455
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01006456 return mbedtls_ssl_session_reset_int(ssl, 0);
Manuel Pégourié-Gonnardac87e282019-05-28 13:02:16 +02006457}
6458
6459/*
Manuel Pégourié-Gonnardc2a7b892019-07-15 09:04:11 +02006460 * Helper to get TLS 1.2 PRF from ciphersuite
6461 * (Duplicates bits of logic from ssl_set_handshake_prfs().)
6462 */
Andrzej Kurekf9412f72022-10-18 07:30:19 -04006463#if defined(MBEDTLS_SHA256_C) || \
6464 (defined(MBEDTLS_SHA512_C) && !defined(MBEDTLS_SHA512_NO_SHA384))
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01006465typedef int (*tls_prf_fn)(const unsigned char *secret, size_t slen,
6466 const char *label,
6467 const unsigned char *random, size_t rlen,
6468 unsigned char *dstbuf, size_t dlen);
6469static tls_prf_fn ssl_tls12prf_from_cs(int ciphersuite_id)
Manuel Pégourié-Gonnardc2a7b892019-07-15 09:04:11 +02006470{
6471 const mbedtls_ssl_ciphersuite_t * const ciphersuite_info =
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01006472 mbedtls_ssl_ciphersuite_from_id(ciphersuite_id);
Manuel Pégourié-Gonnardc2a7b892019-07-15 09:04:11 +02006473
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01006474 if (ciphersuite_info == NULL) {
6475 return NULL;
6476 }
Andrzej Kurek84fc52c2022-10-25 04:18:30 -04006477
6478#if defined(MBEDTLS_SHA512_C) && !defined(MBEDTLS_SHA512_NO_SHA384)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01006479 if (ciphersuite_info->mac == MBEDTLS_MD_SHA384) {
6480 return tls_prf_sha384;
6481 } else
Manuel Pégourié-Gonnard9a96fd72019-07-23 17:11:24 +02006482#endif
Andrzej Kurekf9412f72022-10-18 07:30:19 -04006483#if defined(MBEDTLS_SHA256_C)
6484 {
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01006485 if (ciphersuite_info->mac == MBEDTLS_MD_SHA256) {
6486 return tls_prf_sha256;
6487 }
Andrzej Kurekf9412f72022-10-18 07:30:19 -04006488 }
6489#endif
6490#if !defined(MBEDTLS_SHA256_C) && \
6491 (!defined(MBEDTLS_SHA512_C) || defined(MBEDTLS_SHA512_NO_SHA384))
6492 (void) ciphersuite_info;
6493#endif
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01006494 return NULL;
Manuel Pégourié-Gonnardc2a7b892019-07-15 09:04:11 +02006495}
6496
Andrzej Kurekf9412f72022-10-18 07:30:19 -04006497#endif /* MBEDTLS_SHA256_C ||
6498 (MBEDTLS_SHA512_C && !MBEDTLS_SHA512_NO_SHA384) */
6499
Manuel Pégourié-Gonnardc2a7b892019-07-15 09:04:11 +02006500/*
Manuel Pégourié-Gonnardb9dfc9f2019-07-12 10:50:19 +02006501 * Deserialize context, see mbedtls_ssl_context_save() for format.
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02006502 *
6503 * This internal version is wrapped by a public function that cleans up in
6504 * case of error.
Manuel Pégourié-Gonnardac87e282019-05-28 13:02:16 +02006505 */
Manuel Pégourié-Gonnardd904d662022-06-17 10:24:00 +02006506MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01006507static int ssl_context_load(mbedtls_ssl_context *ssl,
6508 const unsigned char *buf,
6509 size_t len)
Manuel Pégourié-Gonnardac87e282019-05-28 13:02:16 +02006510{
Manuel Pégourié-Gonnard4c90e852019-07-11 10:58:10 +02006511 const unsigned char *p = buf;
6512 const unsigned char * const end = buf + len;
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02006513 size_t session_len;
Janos Follath865b3eb2019-12-16 11:46:15 +00006514 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Andrzej Kurekf9412f72022-10-18 07:30:19 -04006515 tls_prf_fn prf_func = NULL;
Manuel Pégourié-Gonnard4c90e852019-07-11 10:58:10 +02006516
Manuel Pégourié-Gonnard0ff76402019-07-11 09:56:30 +02006517 /*
6518 * The context should have been freshly setup or reset.
6519 * Give the user an error in case of obvious misuse.
Manuel Pégourié-Gonnard4ca930f2019-07-26 16:31:53 +02006520 * (Checking session is useful because it won't be NULL if we're
Manuel Pégourié-Gonnard0ff76402019-07-11 09:56:30 +02006521 * renegotiating, or if the user mistakenly loaded a session first.)
6522 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01006523 if (ssl->state != MBEDTLS_SSL_HELLO_REQUEST ||
6524 ssl->session != NULL) {
6525 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
Manuel Pégourié-Gonnard0ff76402019-07-11 09:56:30 +02006526 }
6527
6528 /*
6529 * We can't check that the config matches the initial one, but we can at
6530 * least check it matches the requirements for serializing.
6531 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01006532 if (ssl->conf->transport != MBEDTLS_SSL_TRANSPORT_DATAGRAM ||
Manuel Pégourié-Gonnard0ff76402019-07-11 09:56:30 +02006533 ssl->conf->max_major_ver < MBEDTLS_SSL_MAJOR_VERSION_3 ||
6534 ssl->conf->min_major_ver > MBEDTLS_SSL_MAJOR_VERSION_3 ||
6535 ssl->conf->max_minor_ver < MBEDTLS_SSL_MINOR_VERSION_3 ||
6536 ssl->conf->min_minor_ver > MBEDTLS_SSL_MINOR_VERSION_3 ||
6537#if defined(MBEDTLS_SSL_RENEGOTIATION)
Manuel Pégourié-Gonnard9a96fd72019-07-23 17:11:24 +02006538 ssl->conf->disable_renegotiation != MBEDTLS_SSL_RENEGOTIATION_DISABLED ||
Manuel Pégourié-Gonnard0ff76402019-07-11 09:56:30 +02006539#endif
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01006540 0) {
6541 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
Manuel Pégourié-Gonnard0ff76402019-07-11 09:56:30 +02006542 }
6543
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01006544 MBEDTLS_SSL_DEBUG_BUF(4, "context to load", buf, len);
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02006545
Manuel Pégourié-Gonnard4c90e852019-07-11 10:58:10 +02006546 /*
6547 * Check version identifier
6548 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01006549 if ((size_t) (end - p) < sizeof(ssl_serialized_context_header)) {
6550 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
Manuel Pégourié-Gonnard4c90e852019-07-11 10:58:10 +02006551 }
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01006552
6553 if (memcmp(p, ssl_serialized_context_header,
6554 sizeof(ssl_serialized_context_header)) != 0) {
6555 return MBEDTLS_ERR_SSL_VERSION_MISMATCH;
6556 }
6557 p += sizeof(ssl_serialized_context_header);
Manuel Pégourié-Gonnard4c90e852019-07-11 10:58:10 +02006558
6559 /*
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02006560 * Session
6561 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01006562 if ((size_t) (end - p) < 4) {
6563 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
6564 }
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02006565
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01006566 session_len = ((size_t) p[0] << 24) |
6567 ((size_t) p[1] << 16) |
6568 ((size_t) p[2] << 8) |
6569 ((size_t) p[3]);
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02006570 p += 4;
6571
Manuel Pégourié-Gonnard142ba732019-07-23 14:43:30 +02006572 /* This has been allocated by ssl_handshake_init(), called by
Hanno Becker43aefe22020-02-05 10:44:56 +00006573 * by either mbedtls_ssl_session_reset_int() or mbedtls_ssl_setup(). */
Manuel Pégourié-Gonnard142ba732019-07-23 14:43:30 +02006574 ssl->session = ssl->session_negotiate;
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02006575 ssl->session_in = ssl->session;
6576 ssl->session_out = ssl->session;
Manuel Pégourié-Gonnard142ba732019-07-23 14:43:30 +02006577 ssl->session_negotiate = NULL;
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02006578
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01006579 if ((size_t) (end - p) < session_len) {
6580 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
6581 }
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02006582
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01006583 ret = ssl_session_load(ssl->session, 1, p, session_len);
6584 if (ret != 0) {
6585 mbedtls_ssl_session_free(ssl->session);
6586 return ret;
Manuel Pégourié-Gonnard45ac1f02019-07-23 16:52:45 +02006587 }
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02006588
6589 p += session_len;
6590
6591 /*
Manuel Pégourié-Gonnardc2a7b892019-07-15 09:04:11 +02006592 * Transform
6593 */
6594
Manuel Pégourié-Gonnard142ba732019-07-23 14:43:30 +02006595 /* This has been allocated by ssl_handshake_init(), called by
Hanno Becker43aefe22020-02-05 10:44:56 +00006596 * by either mbedtls_ssl_session_reset_int() or mbedtls_ssl_setup(). */
Manuel Pégourié-Gonnard142ba732019-07-23 14:43:30 +02006597 ssl->transform = ssl->transform_negotiate;
Manuel Pégourié-Gonnardc2a7b892019-07-15 09:04:11 +02006598 ssl->transform_in = ssl->transform;
6599 ssl->transform_out = ssl->transform;
Manuel Pégourié-Gonnard142ba732019-07-23 14:43:30 +02006600 ssl->transform_negotiate = NULL;
Manuel Pégourié-Gonnardc2a7b892019-07-15 09:04:11 +02006601
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01006602 prf_func = ssl_tls12prf_from_cs(ssl->session->ciphersuite);
6603 if (prf_func == NULL) {
6604 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
6605 }
Andrzej Kurekf9412f72022-10-18 07:30:19 -04006606
Manuel Pégourié-Gonnardc2a7b892019-07-15 09:04:11 +02006607 /* Read random bytes and populate structure */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01006608 if ((size_t) (end - p) < sizeof(ssl->transform->randbytes)) {
6609 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
6610 }
Manuel Pégourié-Gonnardc2a7b892019-07-15 09:04:11 +02006611
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01006612 ret = ssl_populate_transform(ssl->transform,
6613 ssl->session->ciphersuite,
6614 ssl->session->master,
Manuel Pégourié-Gonnardc2a7b892019-07-15 09:04:11 +02006615#if defined(MBEDTLS_SSL_SOME_MODES_USE_MAC)
6616#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01006617 ssl->session->encrypt_then_mac,
Manuel Pégourié-Gonnardc2a7b892019-07-15 09:04:11 +02006618#endif
6619#if defined(MBEDTLS_SSL_TRUNCATED_HMAC)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01006620 ssl->session->trunc_hmac,
Manuel Pégourié-Gonnardc2a7b892019-07-15 09:04:11 +02006621#endif
6622#endif /* MBEDTLS_SSL_SOME_MODES_USE_MAC */
6623#if defined(MBEDTLS_ZLIB_SUPPORT)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01006624 ssl->session->compression,
Manuel Pégourié-Gonnardc2a7b892019-07-15 09:04:11 +02006625#endif
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01006626 prf_func,
6627 p, /* currently pointing to randbytes */
6628 MBEDTLS_SSL_MINOR_VERSION_3, /* (D)TLS 1.2 is forced */
6629 ssl->conf->endpoint,
6630 ssl);
6631 if (ret != 0) {
6632 return ret;
6633 }
Manuel Pégourié-Gonnardc2a7b892019-07-15 09:04:11 +02006634
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01006635 p += sizeof(ssl->transform->randbytes);
Manuel Pégourié-Gonnardc2a7b892019-07-15 09:04:11 +02006636
6637#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
6638 /* Read connection IDs and store them */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01006639 if ((size_t) (end - p) < 1) {
6640 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
6641 }
Manuel Pégourié-Gonnardc2a7b892019-07-15 09:04:11 +02006642
6643 ssl->transform->in_cid_len = *p++;
6644
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01006645 if ((size_t) (end - p) < ssl->transform->in_cid_len + 1u) {
6646 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
6647 }
Manuel Pégourié-Gonnardc2a7b892019-07-15 09:04:11 +02006648
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01006649 memcpy(ssl->transform->in_cid, p, ssl->transform->in_cid_len);
Manuel Pégourié-Gonnardc2a7b892019-07-15 09:04:11 +02006650 p += ssl->transform->in_cid_len;
6651
6652 ssl->transform->out_cid_len = *p++;
6653
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01006654 if ((size_t) (end - p) < ssl->transform->out_cid_len) {
6655 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
6656 }
Manuel Pégourié-Gonnardc2a7b892019-07-15 09:04:11 +02006657
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01006658 memcpy(ssl->transform->out_cid, p, ssl->transform->out_cid_len);
Manuel Pégourié-Gonnardc2a7b892019-07-15 09:04:11 +02006659 p += ssl->transform->out_cid_len;
6660#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
6661
6662 /*
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02006663 * Saved fields from top-level ssl_context structure
6664 */
6665#if defined(MBEDTLS_SSL_DTLS_BADMAC_LIMIT)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01006666 if ((size_t) (end - p) < 4) {
6667 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
6668 }
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02006669
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01006670 ssl->badmac_seen = ((uint32_t) p[0] << 24) |
6671 ((uint32_t) p[1] << 16) |
6672 ((uint32_t) p[2] << 8) |
6673 ((uint32_t) p[3]);
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02006674 p += 4;
6675#endif /* MBEDTLS_SSL_DTLS_BADMAC_LIMIT */
6676
6677#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01006678 if ((size_t) (end - p) < 16) {
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->in_window_top = ((uint64_t) p[0] << 56) |
6683 ((uint64_t) p[1] << 48) |
6684 ((uint64_t) p[2] << 40) |
6685 ((uint64_t) p[3] << 32) |
6686 ((uint64_t) p[4] << 24) |
6687 ((uint64_t) p[5] << 16) |
6688 ((uint64_t) p[6] << 8) |
6689 ((uint64_t) p[7]);
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02006690 p += 8;
6691
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01006692 ssl->in_window = ((uint64_t) p[0] << 56) |
6693 ((uint64_t) p[1] << 48) |
6694 ((uint64_t) p[2] << 40) |
6695 ((uint64_t) p[3] << 32) |
6696 ((uint64_t) p[4] << 24) |
6697 ((uint64_t) p[5] << 16) |
6698 ((uint64_t) p[6] << 8) |
6699 ((uint64_t) p[7]);
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02006700 p += 8;
6701#endif /* MBEDTLS_SSL_DTLS_ANTI_REPLAY */
6702
6703#if defined(MBEDTLS_SSL_PROTO_DTLS)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01006704 if ((size_t) (end - p) < 1) {
6705 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
6706 }
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02006707
6708 ssl->disable_datagram_packing = *p++;
6709#endif /* MBEDTLS_SSL_PROTO_DTLS */
6710
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01006711 if ((size_t) (end - p) < 8) {
6712 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
6713 }
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02006714
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01006715 memcpy(ssl->cur_out_ctr, p, 8);
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02006716 p += 8;
6717
6718#if defined(MBEDTLS_SSL_PROTO_DTLS)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01006719 if ((size_t) (end - p) < 2) {
6720 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
6721 }
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02006722
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01006723 ssl->mtu = (p[0] << 8) | p[1];
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02006724 p += 2;
6725#endif /* MBEDTLS_SSL_PROTO_DTLS */
6726
6727#if defined(MBEDTLS_SSL_ALPN)
6728 {
6729 uint8_t alpn_len;
6730 const char **cur;
6731
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01006732 if ((size_t) (end - p) < 1) {
6733 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
6734 }
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02006735
6736 alpn_len = *p++;
6737
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01006738 if (alpn_len != 0 && ssl->conf->alpn_list != NULL) {
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02006739 /* alpn_chosen should point to an item in the configured list */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01006740 for (cur = ssl->conf->alpn_list; *cur != NULL; cur++) {
6741 if (strlen(*cur) == alpn_len &&
Waleed Elmelegy98ebf482024-03-15 14:29:24 +00006742 memcmp(p, *cur, alpn_len) == 0) {
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02006743 ssl->alpn_chosen = *cur;
6744 break;
6745 }
6746 }
6747 }
6748
6749 /* can only happen on conf mismatch */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01006750 if (alpn_len != 0 && ssl->alpn_chosen == NULL) {
6751 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
6752 }
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02006753
6754 p += alpn_len;
6755 }
6756#endif /* MBEDTLS_SSL_ALPN */
6757
6758 /*
Manuel Pégourié-Gonnard0eb3eac2019-07-15 11:53:51 +02006759 * Forced fields from top-level ssl_context structure
6760 *
6761 * Most of them already set to the correct value by mbedtls_ssl_init() and
6762 * mbedtls_ssl_reset(), so we only need to set the remaining ones.
6763 */
6764 ssl->state = MBEDTLS_SSL_HANDSHAKE_OVER;
6765
6766 ssl->major_ver = MBEDTLS_SSL_MAJOR_VERSION_3;
6767 ssl->minor_ver = MBEDTLS_SSL_MINOR_VERSION_3;
6768
Hanno Becker361b10d2019-08-30 10:42:49 +01006769 /* Adjust pointers for header fields of outgoing records to
6770 * the given transform, accounting for explicit IV and CID. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01006771 mbedtls_ssl_update_out_pointers(ssl, ssl->transform);
Hanno Becker361b10d2019-08-30 10:42:49 +01006772
Manuel Pégourié-Gonnard0eb3eac2019-07-15 11:53:51 +02006773#if defined(MBEDTLS_SSL_PROTO_DTLS)
6774 ssl->in_epoch = 1;
6775#endif
6776
6777 /* mbedtls_ssl_reset() leaves the handshake sub-structure allocated,
6778 * which we don't want - otherwise we'd end up freeing the wrong transform
Hanno Beckerce5f5fd2020-02-05 10:47:44 +00006779 * by calling mbedtls_ssl_handshake_wrapup_free_hs_transform()
6780 * inappropriately. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01006781 if (ssl->handshake != NULL) {
6782 mbedtls_ssl_handshake_free(ssl);
6783 mbedtls_free(ssl->handshake);
Manuel Pégourié-Gonnard0eb3eac2019-07-15 11:53:51 +02006784 ssl->handshake = NULL;
6785 }
6786
6787 /*
Manuel Pégourié-Gonnard4c90e852019-07-11 10:58:10 +02006788 * Done - should have consumed entire buffer
6789 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01006790 if (p != end) {
6791 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
6792 }
Manuel Pégourié-Gonnardac87e282019-05-28 13:02:16 +02006793
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01006794 return 0;
Manuel Pégourié-Gonnardac87e282019-05-28 13:02:16 +02006795}
6796
6797/*
Manuel Pégourié-Gonnardb9dfc9f2019-07-12 10:50:19 +02006798 * Deserialize context: public wrapper for error cleaning
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02006799 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01006800int mbedtls_ssl_context_load(mbedtls_ssl_context *context,
6801 const unsigned char *buf,
6802 size_t len)
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02006803{
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01006804 int ret = ssl_context_load(context, buf, len);
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02006805
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01006806 if (ret != 0) {
6807 mbedtls_ssl_free(context);
6808 }
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02006809
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01006810 return ret;
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02006811}
Manuel Pégourié-Gonnard5c0e3772019-07-23 16:13:17 +02006812#endif /* MBEDTLS_SSL_CONTEXT_SERIALIZATION */
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02006813
6814/*
Paul Bakker5121ce52009-01-03 21:22:43 +00006815 * Free an SSL context
6816 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01006817void mbedtls_ssl_free(mbedtls_ssl_context *ssl)
Paul Bakker5121ce52009-01-03 21:22:43 +00006818{
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01006819 if (ssl == NULL) {
Paul Bakkeraccaffe2014-06-26 13:37:14 +02006820 return;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01006821 }
Paul Bakkeraccaffe2014-06-26 13:37:14 +02006822
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01006823 MBEDTLS_SSL_DEBUG_MSG(2, ("=> free"));
Paul Bakker5121ce52009-01-03 21:22:43 +00006824
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01006825 if (ssl->out_buf != NULL) {
sander-visserb8aa2072020-05-06 22:05:13 +02006826#if defined(MBEDTLS_SSL_VARIABLE_BUFFER_LENGTH)
6827 size_t out_buf_len = ssl->out_buf_len;
6828#else
6829 size_t out_buf_len = MBEDTLS_SSL_OUT_BUFFER_LEN;
6830#endif
6831
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01006832 mbedtls_platform_zeroize(ssl->out_buf, out_buf_len);
6833 mbedtls_free(ssl->out_buf);
Andrzej Kurek0afa2a12020-03-03 10:39:58 -05006834 ssl->out_buf = NULL;
Paul Bakker5121ce52009-01-03 21:22:43 +00006835 }
6836
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01006837 if (ssl->in_buf != NULL) {
sander-visserb8aa2072020-05-06 22:05:13 +02006838#if defined(MBEDTLS_SSL_VARIABLE_BUFFER_LENGTH)
6839 size_t in_buf_len = ssl->in_buf_len;
6840#else
6841 size_t in_buf_len = MBEDTLS_SSL_IN_BUFFER_LEN;
6842#endif
6843
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01006844 mbedtls_platform_zeroize(ssl->in_buf, in_buf_len);
6845 mbedtls_free(ssl->in_buf);
Andrzej Kurek0afa2a12020-03-03 10:39:58 -05006846 ssl->in_buf = NULL;
Paul Bakker5121ce52009-01-03 21:22:43 +00006847 }
6848
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006849#if defined(MBEDTLS_ZLIB_SUPPORT)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01006850 if (ssl->compress_buf != NULL) {
6851 mbedtls_platform_zeroize(ssl->compress_buf, MBEDTLS_SSL_COMPRESS_BUFFER_LEN);
6852 mbedtls_free(ssl->compress_buf);
Paul Bakker16770332013-10-11 09:59:44 +02006853 }
6854#endif
6855
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01006856 if (ssl->transform) {
6857 mbedtls_ssl_transform_free(ssl->transform);
6858 mbedtls_free(ssl->transform);
Paul Bakker48916f92012-09-16 19:57:18 +00006859 }
6860
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01006861 if (ssl->handshake) {
6862 mbedtls_ssl_handshake_free(ssl);
6863 mbedtls_ssl_transform_free(ssl->transform_negotiate);
6864 mbedtls_ssl_session_free(ssl->session_negotiate);
Paul Bakker48916f92012-09-16 19:57:18 +00006865
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01006866 mbedtls_free(ssl->handshake);
6867 mbedtls_free(ssl->transform_negotiate);
6868 mbedtls_free(ssl->session_negotiate);
Paul Bakker48916f92012-09-16 19:57:18 +00006869 }
6870
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01006871 if (ssl->session) {
6872 mbedtls_ssl_session_free(ssl->session);
6873 mbedtls_free(ssl->session);
Paul Bakkerc0463502013-02-14 11:19:38 +01006874 }
6875
Manuel Pégourié-Gonnard55fab2d2015-05-11 16:15:19 +02006876#if defined(MBEDTLS_X509_CRT_PARSE_C)
Gilles Peskine3a2f75d2025-02-12 23:28:48 +01006877 mbedtls_ssl_free_hostname(ssl);
Paul Bakker0be444a2013-08-27 21:55:01 +02006878#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00006879
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006880#if defined(MBEDTLS_SSL_HW_RECORD_ACCEL)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01006881 if (mbedtls_ssl_hw_record_finish != NULL) {
6882 MBEDTLS_SSL_DEBUG_MSG(2, ("going for mbedtls_ssl_hw_record_finish()"));
6883 mbedtls_ssl_hw_record_finish(ssl);
Paul Bakker05ef8352012-05-08 09:17:57 +00006884 }
6885#endif
6886
Manuel Pégourié-Gonnarde057d3b2015-05-20 10:59:43 +02006887#if defined(MBEDTLS_SSL_DTLS_HELLO_VERIFY) && defined(MBEDTLS_SSL_SRV_C)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01006888 mbedtls_free(ssl->cli_id);
Manuel Pégourié-Gonnard43c02182014-07-22 17:32:01 +02006889#endif
6890
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01006891 MBEDTLS_SSL_DEBUG_MSG(2, ("<= free"));
Paul Bakker2da561c2009-02-05 18:00:28 +00006892
Paul Bakker86f04f42013-02-14 11:20:09 +01006893 /* Actually clear after last debug message */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01006894 mbedtls_platform_zeroize(ssl, sizeof(mbedtls_ssl_context));
Paul Bakker5121ce52009-01-03 21:22:43 +00006895}
6896
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02006897/*
Tom Cosgrove49f99bc2022-12-04 16:44:21 +00006898 * Initialize mbedtls_ssl_config
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02006899 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01006900void mbedtls_ssl_config_init(mbedtls_ssl_config *conf)
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02006901{
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01006902 memset(conf, 0, sizeof(mbedtls_ssl_config));
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02006903}
6904
Gilles Peskineeccd8882020-03-10 12:19:08 +01006905#if defined(MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED)
Chien Wongb6d57932024-02-07 21:48:12 +08006906static const int ssl_preset_default_hashes[] = {
Gilles Peskinec54010c2021-05-12 22:52:09 +02006907#if defined(MBEDTLS_SHA512_C)
Manuel Pégourié-Gonnard47229c72015-12-04 15:02:56 +01006908 MBEDTLS_MD_SHA512,
Gilles Peskinec54010c2021-05-12 22:52:09 +02006909#endif
6910#if defined(MBEDTLS_SHA512_C) && !defined(MBEDTLS_SHA512_NO_SHA384)
Manuel Pégourié-Gonnard47229c72015-12-04 15:02:56 +01006911 MBEDTLS_MD_SHA384,
6912#endif
6913#if defined(MBEDTLS_SHA256_C)
6914 MBEDTLS_MD_SHA256,
6915 MBEDTLS_MD_SHA224,
6916#endif
Gilles Peskine5d2511c2017-05-12 13:16:40 +02006917#if defined(MBEDTLS_SHA1_C) && defined(MBEDTLS_TLS_DEFAULT_ALLOW_SHA1_IN_KEY_EXCHANGE)
Manuel Pégourié-Gonnard47229c72015-12-04 15:02:56 +01006918 MBEDTLS_MD_SHA1,
6919#endif
6920 MBEDTLS_MD_NONE
6921};
Simon Butcherc97b6972015-12-27 23:48:17 +00006922#endif
Manuel Pégourié-Gonnard47229c72015-12-04 15:02:56 +01006923
Chien Wongb6d57932024-02-07 21:48:12 +08006924static const int ssl_preset_suiteb_ciphersuites[] = {
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02006925 MBEDTLS_TLS_ECDHE_ECDSA_WITH_AES_128_GCM_SHA256,
6926 MBEDTLS_TLS_ECDHE_ECDSA_WITH_AES_256_GCM_SHA384,
6927 0
6928};
6929
Gilles Peskineeccd8882020-03-10 12:19:08 +01006930#if defined(MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED)
Chien Wongb6d57932024-02-07 21:48:12 +08006931static const int ssl_preset_suiteb_hashes[] = {
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02006932 MBEDTLS_MD_SHA256,
6933 MBEDTLS_MD_SHA384,
6934 MBEDTLS_MD_NONE
6935};
6936#endif
6937
6938#if defined(MBEDTLS_ECP_C)
Chien Wongb6d57932024-02-07 21:48:12 +08006939static const mbedtls_ecp_group_id ssl_preset_suiteb_curves[] = {
Jaeden Amerod4311042019-06-03 08:27:16 +01006940#if defined(MBEDTLS_ECP_DP_SECP256R1_ENABLED)
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02006941 MBEDTLS_ECP_DP_SECP256R1,
Jaeden Amerod4311042019-06-03 08:27:16 +01006942#endif
6943#if defined(MBEDTLS_ECP_DP_SECP384R1_ENABLED)
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02006944 MBEDTLS_ECP_DP_SECP384R1,
Jaeden Amerod4311042019-06-03 08:27:16 +01006945#endif
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02006946 MBEDTLS_ECP_DP_NONE
6947};
6948#endif
6949
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02006950/*
Tillmann Karras588ad502015-09-25 04:27:22 +02006951 * Load default in mbedtls_ssl_config
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02006952 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01006953int mbedtls_ssl_config_defaults(mbedtls_ssl_config *conf,
6954 int endpoint, int transport, int preset)
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02006955{
Manuel Pégourié-Gonnard8b431fb2015-05-11 12:54:52 +02006956#if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_SSL_SRV_C)
Janos Follath865b3eb2019-12-16 11:46:15 +00006957 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnard8b431fb2015-05-11 12:54:52 +02006958#endif
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02006959
Manuel Pégourié-Gonnard0de074f2015-05-14 12:58:01 +02006960 /* Use the functions here so that they are covered in tests,
6961 * but otherwise access member directly for efficiency */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01006962 mbedtls_ssl_conf_endpoint(conf, endpoint);
6963 mbedtls_ssl_conf_transport(conf, transport);
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02006964
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02006965 /*
6966 * Things that are common to all presets
6967 */
Manuel Pégourié-Gonnard419d5ae2015-05-04 19:32:36 +02006968#if defined(MBEDTLS_SSL_CLI_C)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01006969 if (endpoint == MBEDTLS_SSL_IS_CLIENT) {
Manuel Pégourié-Gonnard419d5ae2015-05-04 19:32:36 +02006970 conf->authmode = MBEDTLS_SSL_VERIFY_REQUIRED;
6971#if defined(MBEDTLS_SSL_SESSION_TICKETS)
6972 conf->session_tickets = MBEDTLS_SSL_SESSION_TICKETS_ENABLED;
6973#endif
6974 }
6975#endif
6976
Manuel Pégourié-Gonnard66dc5552015-05-14 12:28:21 +02006977#if defined(MBEDTLS_ARC4_C)
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02006978 conf->arc4_disabled = MBEDTLS_SSL_ARC4_DISABLED;
Manuel Pégourié-Gonnard66dc5552015-05-14 12:28:21 +02006979#endif
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02006980
6981#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
6982 conf->encrypt_then_mac = MBEDTLS_SSL_ETM_ENABLED;
6983#endif
6984
6985#if defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET)
6986 conf->extended_ms = MBEDTLS_SSL_EXTENDED_MS_ENABLED;
6987#endif
6988
Manuel Pégourié-Gonnard17eab2b2015-05-05 16:34:53 +01006989#if defined(MBEDTLS_SSL_CBC_RECORD_SPLITTING)
6990 conf->cbc_record_splitting = MBEDTLS_SSL_CBC_RECORD_SPLITTING_ENABLED;
6991#endif
6992
Manuel Pégourié-Gonnarde057d3b2015-05-20 10:59:43 +02006993#if defined(MBEDTLS_SSL_DTLS_HELLO_VERIFY) && defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02006994 conf->f_cookie_write = ssl_cookie_write_dummy;
6995 conf->f_cookie_check = ssl_cookie_check_dummy;
6996#endif
6997
6998#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
6999 conf->anti_replay = MBEDTLS_SSL_ANTI_REPLAY_ENABLED;
7000#endif
7001
Janos Follath088ce432017-04-10 12:42:31 +01007002#if defined(MBEDTLS_SSL_SRV_C)
7003 conf->cert_req_ca_list = MBEDTLS_SSL_CERT_REQ_CA_LIST_ENABLED;
7004#endif
7005
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02007006#if defined(MBEDTLS_SSL_PROTO_DTLS)
7007 conf->hs_timeout_min = MBEDTLS_SSL_DTLS_TIMEOUT_DFL_MIN;
7008 conf->hs_timeout_max = MBEDTLS_SSL_DTLS_TIMEOUT_DFL_MAX;
7009#endif
7010
7011#if defined(MBEDTLS_SSL_RENEGOTIATION)
7012 conf->renego_max_records = MBEDTLS_SSL_RENEGO_MAX_RECORDS_DEFAULT;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01007013 memset(conf->renego_period, 0x00, 2);
7014 memset(conf->renego_period + 2, 0xFF, 6);
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02007015#endif
7016
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02007017#if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_SSL_SRV_C)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01007018 if (endpoint == MBEDTLS_SSL_IS_SERVER) {
7019 const unsigned char dhm_p[] =
7020 MBEDTLS_DHM_RFC3526_MODP_2048_P_BIN;
7021 const unsigned char dhm_g[] =
7022 MBEDTLS_DHM_RFC3526_MODP_2048_G_BIN;
Hanno Becker00d0a682017-10-04 13:14:29 +01007023
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01007024 if ((ret = mbedtls_ssl_conf_dh_param_bin(conf,
7025 dhm_p, sizeof(dhm_p),
7026 dhm_g, sizeof(dhm_g))) != 0) {
7027 return ret;
7028 }
7029 }
Manuel Pégourié-Gonnardbd990d62015-06-11 14:49:42 +02007030#endif
7031
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02007032 /*
7033 * Preset-specific defaults
7034 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01007035 switch (preset) {
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02007036 /*
7037 * NSA Suite B
7038 */
7039 case MBEDTLS_SSL_PRESET_SUITEB:
7040 conf->min_major_ver = MBEDTLS_SSL_MAJOR_VERSION_3;
7041 conf->min_minor_ver = MBEDTLS_SSL_MINOR_VERSION_3; /* TLS 1.2 */
7042 conf->max_major_ver = MBEDTLS_SSL_MAX_MAJOR_VERSION;
7043 conf->max_minor_ver = MBEDTLS_SSL_MAX_MINOR_VERSION;
7044
7045 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_0] =
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01007046 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_1] =
7047 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_2] =
7048 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_3] =
7049 ssl_preset_suiteb_ciphersuites;
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02007050
7051#if defined(MBEDTLS_X509_CRT_PARSE_C)
7052 conf->cert_profile = &mbedtls_x509_crt_profile_suiteb;
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02007053#endif
7054
Gilles Peskineeccd8882020-03-10 12:19:08 +01007055#if defined(MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED)
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02007056 conf->sig_hashes = ssl_preset_suiteb_hashes;
7057#endif
7058
7059#if defined(MBEDTLS_ECP_C)
7060 conf->curve_list = ssl_preset_suiteb_curves;
7061#endif
Manuel Pégourié-Gonnardc98204e2015-08-11 04:21:01 +02007062 break;
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02007063
7064 /*
7065 * Default
7066 */
7067 default:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01007068 conf->min_major_ver = (MBEDTLS_SSL_MIN_MAJOR_VERSION >
7069 MBEDTLS_SSL_MIN_VALID_MAJOR_VERSION) ?
7070 MBEDTLS_SSL_MIN_MAJOR_VERSION :
7071 MBEDTLS_SSL_MIN_VALID_MAJOR_VERSION;
7072 conf->min_minor_ver = (MBEDTLS_SSL_MIN_MINOR_VERSION >
7073 MBEDTLS_SSL_MIN_VALID_MINOR_VERSION) ?
7074 MBEDTLS_SSL_MIN_MINOR_VERSION :
7075 MBEDTLS_SSL_MIN_VALID_MINOR_VERSION;
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02007076 conf->max_major_ver = MBEDTLS_SSL_MAX_MAJOR_VERSION;
7077 conf->max_minor_ver = MBEDTLS_SSL_MAX_MINOR_VERSION;
7078
7079#if defined(MBEDTLS_SSL_PROTO_DTLS)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01007080 if (transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM) {
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02007081 conf->min_minor_ver = MBEDTLS_SSL_MINOR_VERSION_2;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01007082 }
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02007083#endif
7084
7085 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_0] =
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01007086 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_1] =
7087 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_2] =
7088 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_3] =
7089 mbedtls_ssl_list_ciphersuites();
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02007090
7091#if defined(MBEDTLS_X509_CRT_PARSE_C)
7092 conf->cert_profile = &mbedtls_x509_crt_profile_default;
7093#endif
7094
Gilles Peskineeccd8882020-03-10 12:19:08 +01007095#if defined(MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED)
Manuel Pégourié-Gonnard47229c72015-12-04 15:02:56 +01007096 conf->sig_hashes = ssl_preset_default_hashes;
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02007097#endif
7098
7099#if defined(MBEDTLS_ECP_C)
7100 conf->curve_list = mbedtls_ecp_grp_id_list();
7101#endif
7102
7103#if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_SSL_CLI_C)
7104 conf->dhm_min_bitlen = 1024;
7105#endif
7106 }
7107
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01007108 return 0;
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02007109}
7110
7111/*
7112 * Free mbedtls_ssl_config
7113 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01007114void mbedtls_ssl_config_free(mbedtls_ssl_config *conf)
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02007115{
7116#if defined(MBEDTLS_DHM_C)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01007117 mbedtls_mpi_free(&conf->dhm_P);
7118 mbedtls_mpi_free(&conf->dhm_G);
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02007119#endif
7120
Gilles Peskineeccd8882020-03-10 12:19:08 +01007121#if defined(MBEDTLS_KEY_EXCHANGE_SOME_PSK_ENABLED)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01007122 if (conf->psk != NULL) {
7123 mbedtls_platform_zeroize(conf->psk, conf->psk_len);
7124 mbedtls_free(conf->psk);
Azim Khan27e8a122018-03-21 14:24:11 +00007125 conf->psk = NULL;
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02007126 conf->psk_len = 0;
junyeonLEE316b1622017-12-20 16:29:30 +09007127 }
7128
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01007129 if (conf->psk_identity != NULL) {
7130 mbedtls_platform_zeroize(conf->psk_identity, conf->psk_identity_len);
7131 mbedtls_free(conf->psk_identity);
Azim Khan27e8a122018-03-21 14:24:11 +00007132 conf->psk_identity = NULL;
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02007133 conf->psk_identity_len = 0;
7134 }
7135#endif
7136
7137#if defined(MBEDTLS_X509_CRT_PARSE_C)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01007138 ssl_key_cert_free(conf->key_cert);
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02007139#endif
7140
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01007141 mbedtls_platform_zeroize(conf, sizeof(mbedtls_ssl_config));
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02007142}
7143
Manuel Pégourié-Gonnard5674a972015-10-19 15:14:03 +02007144#if defined(MBEDTLS_PK_C) && \
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01007145 (defined(MBEDTLS_RSA_C) || defined(MBEDTLS_ECDSA_C))
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02007146/*
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007147 * Convert between MBEDTLS_PK_XXX and SSL_SIG_XXX
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02007148 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01007149unsigned char mbedtls_ssl_sig_from_pk(mbedtls_pk_context *pk)
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02007150{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007151#if defined(MBEDTLS_RSA_C)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01007152 if (mbedtls_pk_can_do(pk, MBEDTLS_PK_RSA)) {
7153 return MBEDTLS_SSL_SIG_RSA;
7154 }
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02007155#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007156#if defined(MBEDTLS_ECDSA_C)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01007157 if (mbedtls_pk_can_do(pk, MBEDTLS_PK_ECDSA)) {
7158 return MBEDTLS_SSL_SIG_ECDSA;
7159 }
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02007160#endif
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01007161 return MBEDTLS_SSL_SIG_ANON;
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02007162}
7163
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01007164unsigned char mbedtls_ssl_sig_from_pk_alg(mbedtls_pk_type_t type)
Hanno Becker7e5437a2017-04-28 17:15:26 +01007165{
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01007166 switch (type) {
Hanno Becker7e5437a2017-04-28 17:15:26 +01007167 case MBEDTLS_PK_RSA:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01007168 return MBEDTLS_SSL_SIG_RSA;
Hanno Becker7e5437a2017-04-28 17:15:26 +01007169 case MBEDTLS_PK_ECDSA:
7170 case MBEDTLS_PK_ECKEY:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01007171 return MBEDTLS_SSL_SIG_ECDSA;
Hanno Becker7e5437a2017-04-28 17:15:26 +01007172 default:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01007173 return MBEDTLS_SSL_SIG_ANON;
Hanno Becker7e5437a2017-04-28 17:15:26 +01007174 }
7175}
7176
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01007177mbedtls_pk_type_t mbedtls_ssl_pk_alg_from_sig(unsigned char sig)
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02007178{
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01007179 switch (sig) {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007180#if defined(MBEDTLS_RSA_C)
7181 case MBEDTLS_SSL_SIG_RSA:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01007182 return MBEDTLS_PK_RSA;
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02007183#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007184#if defined(MBEDTLS_ECDSA_C)
7185 case MBEDTLS_SSL_SIG_ECDSA:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01007186 return MBEDTLS_PK_ECDSA;
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02007187#endif
7188 default:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01007189 return MBEDTLS_PK_NONE;
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02007190 }
7191}
Manuel Pégourié-Gonnard5674a972015-10-19 15:14:03 +02007192#endif /* MBEDTLS_PK_C && ( MBEDTLS_RSA_C || MBEDTLS_ECDSA_C ) */
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02007193
Hanno Becker7e5437a2017-04-28 17:15:26 +01007194#if defined(MBEDTLS_SSL_PROTO_TLS1_2) && \
Gilles Peskineeccd8882020-03-10 12:19:08 +01007195 defined(MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED)
Hanno Becker7e5437a2017-04-28 17:15:26 +01007196
7197/* Find an entry in a signature-hash set matching a given hash algorithm. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01007198mbedtls_md_type_t mbedtls_ssl_sig_hash_set_find(mbedtls_ssl_sig_hash_set_t *set,
7199 mbedtls_pk_type_t sig_alg)
Hanno Becker7e5437a2017-04-28 17:15:26 +01007200{
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01007201 switch (sig_alg) {
Hanno Becker7e5437a2017-04-28 17:15:26 +01007202 case MBEDTLS_PK_RSA:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01007203 return set->rsa;
Hanno Becker7e5437a2017-04-28 17:15:26 +01007204 case MBEDTLS_PK_ECDSA:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01007205 return set->ecdsa;
Hanno Becker7e5437a2017-04-28 17:15:26 +01007206 default:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01007207 return MBEDTLS_MD_NONE;
Hanno Becker7e5437a2017-04-28 17:15:26 +01007208 }
7209}
7210
7211/* Add a signature-hash-pair to a signature-hash set */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01007212void mbedtls_ssl_sig_hash_set_add(mbedtls_ssl_sig_hash_set_t *set,
7213 mbedtls_pk_type_t sig_alg,
7214 mbedtls_md_type_t md_alg)
Hanno Becker7e5437a2017-04-28 17:15:26 +01007215{
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01007216 switch (sig_alg) {
Hanno Becker7e5437a2017-04-28 17:15:26 +01007217 case MBEDTLS_PK_RSA:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01007218 if (set->rsa == MBEDTLS_MD_NONE) {
Hanno Becker7e5437a2017-04-28 17:15:26 +01007219 set->rsa = md_alg;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01007220 }
Hanno Becker7e5437a2017-04-28 17:15:26 +01007221 break;
7222
7223 case MBEDTLS_PK_ECDSA:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01007224 if (set->ecdsa == MBEDTLS_MD_NONE) {
Hanno Becker7e5437a2017-04-28 17:15:26 +01007225 set->ecdsa = md_alg;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01007226 }
Hanno Becker7e5437a2017-04-28 17:15:26 +01007227 break;
7228
7229 default:
7230 break;
7231 }
7232}
7233
7234/* Allow exactly one hash algorithm for each signature. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01007235void mbedtls_ssl_sig_hash_set_const_hash(mbedtls_ssl_sig_hash_set_t *set,
7236 mbedtls_md_type_t md_alg)
Hanno Becker7e5437a2017-04-28 17:15:26 +01007237{
7238 set->rsa = md_alg;
7239 set->ecdsa = md_alg;
7240}
7241
7242#endif /* MBEDTLS_SSL_PROTO_TLS1_2) &&
Gilles Peskineeccd8882020-03-10 12:19:08 +01007243 MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED */
Hanno Becker7e5437a2017-04-28 17:15:26 +01007244
Manuel Pégourié-Gonnard1a483832013-09-20 12:29:15 +02007245/*
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +02007246 * Convert from MBEDTLS_SSL_HASH_XXX to MBEDTLS_MD_XXX
Manuel Pégourié-Gonnard1a483832013-09-20 12:29:15 +02007247 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01007248mbedtls_md_type_t mbedtls_ssl_md_alg_from_hash(unsigned char hash)
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02007249{
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01007250 switch (hash) {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007251#if defined(MBEDTLS_MD5_C)
7252 case MBEDTLS_SSL_HASH_MD5:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01007253 return MBEDTLS_MD_MD5;
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02007254#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007255#if defined(MBEDTLS_SHA1_C)
7256 case MBEDTLS_SSL_HASH_SHA1:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01007257 return MBEDTLS_MD_SHA1;
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02007258#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007259#if defined(MBEDTLS_SHA256_C)
7260 case MBEDTLS_SSL_HASH_SHA224:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01007261 return MBEDTLS_MD_SHA224;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007262 case MBEDTLS_SSL_HASH_SHA256:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01007263 return MBEDTLS_MD_SHA256;
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02007264#endif
Gilles Peskined2d59372021-05-12 22:43:27 +02007265#if defined(MBEDTLS_SHA512_C) && !defined(MBEDTLS_SHA512_NO_SHA384)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007266 case MBEDTLS_SSL_HASH_SHA384:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01007267 return MBEDTLS_MD_SHA384;
Gilles Peskinec54010c2021-05-12 22:52:09 +02007268#endif
7269#if defined(MBEDTLS_SHA512_C)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007270 case MBEDTLS_SSL_HASH_SHA512:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01007271 return MBEDTLS_MD_SHA512;
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02007272#endif
7273 default:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01007274 return MBEDTLS_MD_NONE;
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02007275 }
7276}
7277
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +02007278/*
7279 * Convert from MBEDTLS_MD_XXX to MBEDTLS_SSL_HASH_XXX
7280 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01007281unsigned char mbedtls_ssl_hash_from_md_alg(int md)
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +02007282{
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01007283 switch (md) {
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +02007284#if defined(MBEDTLS_MD5_C)
7285 case MBEDTLS_MD_MD5:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01007286 return MBEDTLS_SSL_HASH_MD5;
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +02007287#endif
7288#if defined(MBEDTLS_SHA1_C)
7289 case MBEDTLS_MD_SHA1:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01007290 return MBEDTLS_SSL_HASH_SHA1;
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +02007291#endif
7292#if defined(MBEDTLS_SHA256_C)
7293 case MBEDTLS_MD_SHA224:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01007294 return MBEDTLS_SSL_HASH_SHA224;
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +02007295 case MBEDTLS_MD_SHA256:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01007296 return MBEDTLS_SSL_HASH_SHA256;
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +02007297#endif
Gilles Peskined2d59372021-05-12 22:43:27 +02007298#if defined(MBEDTLS_SHA512_C) && !defined(MBEDTLS_SHA512_NO_SHA384)
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +02007299 case MBEDTLS_MD_SHA384:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01007300 return MBEDTLS_SSL_HASH_SHA384;
Gilles Peskinec54010c2021-05-12 22:52:09 +02007301#endif
7302#if defined(MBEDTLS_SHA512_C)
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +02007303 case MBEDTLS_MD_SHA512:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01007304 return MBEDTLS_SSL_HASH_SHA512;
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +02007305#endif
7306 default:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01007307 return MBEDTLS_SSL_HASH_NONE;
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +02007308 }
7309}
7310
Manuel Pégourié-Gonnardb541da62015-06-17 11:43:30 +02007311#if defined(MBEDTLS_ECP_C)
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01007312/*
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +02007313 * Check if a curve proposed by the peer is in our list.
Manuel Pégourié-Gonnard9d412d82015-06-17 12:10:46 +02007314 * Return 0 if we're willing to use it, -1 otherwise.
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01007315 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01007316int mbedtls_ssl_check_curve(const mbedtls_ssl_context *ssl, mbedtls_ecp_group_id grp_id)
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01007317{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007318 const mbedtls_ecp_group_id *gid;
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01007319
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01007320 if (ssl->conf->curve_list == NULL) {
7321 return -1;
7322 }
Manuel Pégourié-Gonnard9d412d82015-06-17 12:10:46 +02007323
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01007324 for (gid = ssl->conf->curve_list; *gid != MBEDTLS_ECP_DP_NONE; gid++) {
7325 if (*gid == grp_id) {
7326 return 0;
7327 }
7328 }
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01007329
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01007330 return -1;
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01007331}
Manuel Pégourié-Gonnard298d6cc2022-02-14 11:34:47 +01007332
7333/*
7334 * Same as mbedtls_ssl_check_curve() but takes a TLS ID for the curve.
7335 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01007336int mbedtls_ssl_check_curve_tls_id(const mbedtls_ssl_context *ssl, uint16_t tls_id)
Manuel Pégourié-Gonnard298d6cc2022-02-14 11:34:47 +01007337{
7338 const mbedtls_ecp_curve_info *curve_info =
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01007339 mbedtls_ecp_curve_info_from_tls_id(tls_id);
7340 if (curve_info == NULL) {
7341 return -1;
7342 }
7343 return mbedtls_ssl_check_curve(ssl, curve_info->grp_id);
Manuel Pégourié-Gonnard298d6cc2022-02-14 11:34:47 +01007344}
Manuel Pégourié-Gonnardb541da62015-06-17 11:43:30 +02007345#endif /* MBEDTLS_ECP_C */
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02007346
Gilles Peskineeccd8882020-03-10 12:19:08 +01007347#if defined(MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED)
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +02007348/*
7349 * Check if a hash proposed by the peer is in our list.
7350 * Return 0 if we're willing to use it, -1 otherwise.
7351 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01007352int mbedtls_ssl_check_sig_hash(const mbedtls_ssl_context *ssl,
7353 mbedtls_md_type_t md)
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +02007354{
7355 const int *cur;
7356
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01007357 if (ssl->conf->sig_hashes == NULL) {
7358 return -1;
7359 }
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +02007360
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01007361 for (cur = ssl->conf->sig_hashes; *cur != MBEDTLS_MD_NONE; cur++) {
7362 if (*cur == (int) md) {
7363 return 0;
7364 }
7365 }
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +02007366
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01007367 return -1;
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +02007368}
Gilles Peskineeccd8882020-03-10 12:19:08 +01007369#endif /* MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED */
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +02007370
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007371#if defined(MBEDTLS_X509_CRT_PARSE_C)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01007372int mbedtls_ssl_check_cert_usage(const mbedtls_x509_crt *cert,
7373 const mbedtls_ssl_ciphersuite_t *ciphersuite,
7374 int cert_endpoint,
7375 uint32_t *flags)
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02007376{
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +01007377 int ret = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007378#if defined(MBEDTLS_X509_CHECK_KEY_USAGE)
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02007379 int usage = 0;
7380#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007381#if defined(MBEDTLS_X509_CHECK_EXTENDED_KEY_USAGE)
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +02007382 const char *ext_oid;
7383 size_t ext_len;
7384#endif
7385
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007386#if !defined(MBEDTLS_X509_CHECK_KEY_USAGE) && \
7387 !defined(MBEDTLS_X509_CHECK_EXTENDED_KEY_USAGE)
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +02007388 ((void) cert);
7389 ((void) cert_endpoint);
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +01007390 ((void) flags);
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +02007391#endif
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02007392
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007393#if defined(MBEDTLS_X509_CHECK_KEY_USAGE)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01007394 if (cert_endpoint == MBEDTLS_SSL_IS_SERVER) {
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02007395 /* Server part of the key exchange */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01007396 switch (ciphersuite->key_exchange) {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007397 case MBEDTLS_KEY_EXCHANGE_RSA:
7398 case MBEDTLS_KEY_EXCHANGE_RSA_PSK:
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +01007399 usage = MBEDTLS_X509_KU_KEY_ENCIPHERMENT;
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02007400 break;
7401
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007402 case MBEDTLS_KEY_EXCHANGE_DHE_RSA:
7403 case MBEDTLS_KEY_EXCHANGE_ECDHE_RSA:
7404 case MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA:
7405 usage = MBEDTLS_X509_KU_DIGITAL_SIGNATURE;
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02007406 break;
7407
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007408 case MBEDTLS_KEY_EXCHANGE_ECDH_RSA:
7409 case MBEDTLS_KEY_EXCHANGE_ECDH_ECDSA:
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +01007410 usage = MBEDTLS_X509_KU_KEY_AGREEMENT;
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02007411 break;
7412
7413 /* Don't use default: we want warnings when adding new values */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007414 case MBEDTLS_KEY_EXCHANGE_NONE:
7415 case MBEDTLS_KEY_EXCHANGE_PSK:
7416 case MBEDTLS_KEY_EXCHANGE_DHE_PSK:
7417 case MBEDTLS_KEY_EXCHANGE_ECDHE_PSK:
Manuel Pégourié-Gonnard557535d2015-09-15 17:53:32 +02007418 case MBEDTLS_KEY_EXCHANGE_ECJPAKE:
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02007419 usage = 0;
7420 }
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01007421 } else {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007422 /* Client auth: we only implement rsa_sign and mbedtls_ecdsa_sign for now */
7423 usage = MBEDTLS_X509_KU_DIGITAL_SIGNATURE;
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02007424 }
7425
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01007426 if (mbedtls_x509_crt_check_key_usage(cert, usage) != 0) {
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +01007427 *flags |= MBEDTLS_X509_BADCERT_KEY_USAGE;
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +01007428 ret = -1;
7429 }
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +02007430#else
7431 ((void) ciphersuite);
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007432#endif /* MBEDTLS_X509_CHECK_KEY_USAGE */
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02007433
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007434#if defined(MBEDTLS_X509_CHECK_EXTENDED_KEY_USAGE)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01007435 if (cert_endpoint == MBEDTLS_SSL_IS_SERVER) {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007436 ext_oid = MBEDTLS_OID_SERVER_AUTH;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01007437 ext_len = MBEDTLS_OID_SIZE(MBEDTLS_OID_SERVER_AUTH);
7438 } else {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007439 ext_oid = MBEDTLS_OID_CLIENT_AUTH;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01007440 ext_len = MBEDTLS_OID_SIZE(MBEDTLS_OID_CLIENT_AUTH);
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +02007441 }
7442
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01007443 if (mbedtls_x509_crt_check_extended_key_usage(cert, ext_oid, ext_len) != 0) {
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +01007444 *flags |= MBEDTLS_X509_BADCERT_EXT_KEY_USAGE;
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +01007445 ret = -1;
7446 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007447#endif /* MBEDTLS_X509_CHECK_EXTENDED_KEY_USAGE */
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +02007448
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01007449 return ret;
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02007450}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007451#endif /* MBEDTLS_X509_CRT_PARSE_C */
Manuel Pégourié-Gonnard3a306b92014-04-29 15:11:17 +02007452
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01007453int mbedtls_ssl_set_calc_verify_md(mbedtls_ssl_context *ssl, int md)
Simon Butcher99000142016-10-13 17:21:01 +01007454{
7455#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01007456 if (ssl->minor_ver != MBEDTLS_SSL_MINOR_VERSION_3) {
Simon Butcher99000142016-10-13 17:21:01 +01007457 return MBEDTLS_ERR_SSL_INVALID_VERIFY_HASH;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01007458 }
Simon Butcher99000142016-10-13 17:21:01 +01007459
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01007460 switch (md) {
Simon Butcher99000142016-10-13 17:21:01 +01007461#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1)
7462#if defined(MBEDTLS_MD5_C)
7463 case MBEDTLS_SSL_HASH_MD5:
Janos Follath182013f2016-10-25 10:50:22 +01007464 return MBEDTLS_ERR_SSL_INVALID_VERIFY_HASH;
Simon Butcher99000142016-10-13 17:21:01 +01007465#endif
7466#if defined(MBEDTLS_SHA1_C)
7467 case MBEDTLS_SSL_HASH_SHA1:
7468 ssl->handshake->calc_verify = ssl_calc_verify_tls;
7469 break;
7470#endif
7471#endif /* MBEDTLS_SSL_PROTO_TLS1 || MBEDTLS_SSL_PROTO_TLS1_1 */
Gilles Peskined2d59372021-05-12 22:43:27 +02007472#if defined(MBEDTLS_SHA512_C) && !defined(MBEDTLS_SHA512_NO_SHA384)
Simon Butcher99000142016-10-13 17:21:01 +01007473 case MBEDTLS_SSL_HASH_SHA384:
7474 ssl->handshake->calc_verify = ssl_calc_verify_tls_sha384;
7475 break;
7476#endif
7477#if defined(MBEDTLS_SHA256_C)
7478 case MBEDTLS_SSL_HASH_SHA256:
7479 ssl->handshake->calc_verify = ssl_calc_verify_tls_sha256;
7480 break;
7481#endif
7482 default:
7483 return MBEDTLS_ERR_SSL_INVALID_VERIFY_HASH;
7484 }
7485
7486 return 0;
7487#else /* !MBEDTLS_SSL_PROTO_TLS1_2 */
7488 (void) ssl;
7489 (void) md;
7490
7491 return MBEDTLS_ERR_SSL_INVALID_VERIFY_HASH;
7492#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
7493}
7494
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +01007495#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1) || \
7496 defined(MBEDTLS_SSL_PROTO_TLS1_1)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01007497int mbedtls_ssl_get_key_exchange_md_ssl_tls(mbedtls_ssl_context *ssl,
7498 unsigned char *output,
7499 unsigned char *data, size_t data_len)
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +01007500{
7501 int ret = 0;
7502 mbedtls_md5_context mbedtls_md5;
7503 mbedtls_sha1_context mbedtls_sha1;
7504
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01007505 mbedtls_md5_init(&mbedtls_md5);
7506 mbedtls_sha1_init(&mbedtls_sha1);
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +01007507
7508 /*
7509 * digitally-signed struct {
7510 * opaque md5_hash[16];
7511 * opaque sha_hash[20];
7512 * };
7513 *
7514 * md5_hash
7515 * MD5(ClientHello.random + ServerHello.random
7516 * + ServerParams);
7517 * sha_hash
7518 * SHA(ClientHello.random + ServerHello.random
7519 * + ServerParams);
7520 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01007521 if ((ret = mbedtls_md5_starts_ret(&mbedtls_md5)) != 0) {
7522 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_md5_starts_ret", ret);
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +01007523 goto exit;
7524 }
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01007525 if ((ret = mbedtls_md5_update_ret(&mbedtls_md5,
7526 ssl->handshake->randbytes, 64)) != 0) {
7527 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_md5_update_ret", ret);
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +01007528 goto exit;
7529 }
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01007530 if ((ret = mbedtls_md5_update_ret(&mbedtls_md5, data, data_len)) != 0) {
7531 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_md5_update_ret", ret);
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +01007532 goto exit;
7533 }
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01007534 if ((ret = mbedtls_md5_finish_ret(&mbedtls_md5, output)) != 0) {
7535 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_md5_finish_ret", ret);
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +01007536 goto exit;
7537 }
7538
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01007539 if ((ret = mbedtls_sha1_starts_ret(&mbedtls_sha1)) != 0) {
7540 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_sha1_starts_ret", ret);
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +01007541 goto exit;
7542 }
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01007543 if ((ret = mbedtls_sha1_update_ret(&mbedtls_sha1,
7544 ssl->handshake->randbytes, 64)) != 0) {
7545 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_sha1_update_ret", ret);
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +01007546 goto exit;
7547 }
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01007548 if ((ret = mbedtls_sha1_update_ret(&mbedtls_sha1, data,
7549 data_len)) != 0) {
7550 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_sha1_update_ret", ret);
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +01007551 goto exit;
7552 }
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01007553 if ((ret = mbedtls_sha1_finish_ret(&mbedtls_sha1,
7554 output + 16)) != 0) {
7555 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_sha1_finish_ret", ret);
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +01007556 goto exit;
7557 }
7558
7559exit:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01007560 mbedtls_md5_free(&mbedtls_md5);
7561 mbedtls_sha1_free(&mbedtls_sha1);
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +01007562
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01007563 if (ret != 0) {
7564 mbedtls_ssl_send_alert_message(ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
7565 MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR);
7566 }
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +01007567
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01007568 return ret;
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +01007569
7570}
7571#endif /* MBEDTLS_SSL_PROTO_SSL3 || MBEDTLS_SSL_PROTO_TLS1 || \
7572 MBEDTLS_SSL_PROTO_TLS1_1 */
7573
7574#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1) || \
7575 defined(MBEDTLS_SSL_PROTO_TLS1_2)
Andrzej Kurekd6db9be2019-01-10 05:27:10 -05007576
7577#if defined(MBEDTLS_USE_PSA_CRYPTO)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01007578int mbedtls_ssl_get_key_exchange_md_tls1_2(mbedtls_ssl_context *ssl,
7579 unsigned char *hash, size_t *hashlen,
7580 unsigned char *data, size_t data_len,
7581 mbedtls_md_type_t md_alg)
Andrzej Kurekd6db9be2019-01-10 05:27:10 -05007582{
Andrzej Kurek814feff2019-01-14 04:35:19 -05007583 psa_status_t status;
Jaeden Amero34973232019-02-20 10:32:28 +00007584 psa_hash_operation_t hash_operation = PSA_HASH_OPERATION_INIT;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01007585 psa_algorithm_t hash_alg = mbedtls_psa_translate_md(md_alg);
Andrzej Kurekd6db9be2019-01-10 05:27:10 -05007586
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01007587 MBEDTLS_SSL_DEBUG_MSG(3, ("Perform PSA-based computation of digest of ServerKeyExchange"));
Andrzej Kurek814feff2019-01-14 04:35:19 -05007588
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01007589 if ((status = psa_hash_setup(&hash_operation,
7590 hash_alg)) != PSA_SUCCESS) {
7591 MBEDTLS_SSL_DEBUG_RET(1, "psa_hash_setup", status);
Andrzej Kurekd6db9be2019-01-10 05:27:10 -05007592 goto exit;
7593 }
7594
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01007595 if ((status = psa_hash_update(&hash_operation, ssl->handshake->randbytes,
7596 64)) != PSA_SUCCESS) {
7597 MBEDTLS_SSL_DEBUG_RET(1, "psa_hash_update", status);
Andrzej Kurekd6db9be2019-01-10 05:27:10 -05007598 goto exit;
7599 }
7600
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01007601 if ((status = psa_hash_update(&hash_operation,
7602 data, data_len)) != PSA_SUCCESS) {
7603 MBEDTLS_SSL_DEBUG_RET(1, "psa_hash_update", 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_finish(&hash_operation, hash, PSA_HASH_MAX_SIZE,
7608 hashlen)) != PSA_SUCCESS) {
7609 MBEDTLS_SSL_DEBUG_RET(1, "psa_hash_finish", status);
7610 goto exit;
Andrzej Kurekd6db9be2019-01-10 05:27:10 -05007611 }
7612
7613exit:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01007614 if (status != PSA_SUCCESS) {
7615 mbedtls_ssl_send_alert_message(ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
7616 MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR);
7617 switch (status) {
Andrzej Kurekd6db9be2019-01-10 05:27:10 -05007618 case PSA_ERROR_NOT_SUPPORTED:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01007619 return MBEDTLS_ERR_MD_FEATURE_UNAVAILABLE;
Andrzej Kurek814feff2019-01-14 04:35:19 -05007620 case PSA_ERROR_BAD_STATE: /* Intentional fallthrough */
Andrzej Kurekd6db9be2019-01-10 05:27:10 -05007621 case PSA_ERROR_BUFFER_TOO_SMALL:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01007622 return MBEDTLS_ERR_MD_BAD_INPUT_DATA;
Andrzej Kurekd6db9be2019-01-10 05:27:10 -05007623 case PSA_ERROR_INSUFFICIENT_MEMORY:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01007624 return MBEDTLS_ERR_MD_ALLOC_FAILED;
Andrzej Kurekd6db9be2019-01-10 05:27:10 -05007625 default:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01007626 return MBEDTLS_ERR_MD_HW_ACCEL_FAILED;
Andrzej Kurekd6db9be2019-01-10 05:27:10 -05007627 }
7628 }
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01007629 return 0;
Andrzej Kurekd6db9be2019-01-10 05:27:10 -05007630}
7631
7632#else
7633
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01007634int mbedtls_ssl_get_key_exchange_md_tls1_2(mbedtls_ssl_context *ssl,
7635 unsigned char *hash, size_t *hashlen,
7636 unsigned char *data, size_t data_len,
7637 mbedtls_md_type_t md_alg)
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +01007638{
7639 int ret = 0;
7640 mbedtls_md_context_t ctx;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01007641 const mbedtls_md_info_t *md_info = mbedtls_md_info_from_type(md_alg);
7642 *hashlen = mbedtls_md_get_size(md_info);
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +01007643
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01007644 MBEDTLS_SSL_DEBUG_MSG(3, ("Perform mbedtls-based computation of digest of ServerKeyExchange"));
Andrzej Kurek814feff2019-01-14 04:35:19 -05007645
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01007646 mbedtls_md_init(&ctx);
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +01007647
7648 /*
7649 * digitally-signed struct {
7650 * opaque client_random[32];
7651 * opaque server_random[32];
7652 * ServerDHParams params;
7653 * };
7654 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01007655 if ((ret = mbedtls_md_setup(&ctx, md_info, 0)) != 0) {
7656 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_md_setup", ret);
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +01007657 goto exit;
7658 }
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01007659 if ((ret = mbedtls_md_starts(&ctx)) != 0) {
7660 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_md_starts", ret);
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +01007661 goto exit;
7662 }
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01007663 if ((ret = mbedtls_md_update(&ctx, ssl->handshake->randbytes, 64)) != 0) {
7664 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_md_update", ret);
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +01007665 goto exit;
7666 }
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01007667 if ((ret = mbedtls_md_update(&ctx, data, data_len)) != 0) {
7668 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_md_update", 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_finish(&ctx, hash)) != 0) {
7672 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_md_finish", ret);
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +01007673 goto exit;
7674 }
7675
7676exit:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01007677 mbedtls_md_free(&ctx);
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +01007678
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01007679 if (ret != 0) {
7680 mbedtls_ssl_send_alert_message(ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
7681 MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR);
7682 }
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +01007683
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01007684 return ret;
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +01007685}
Andrzej Kurekd6db9be2019-01-10 05:27:10 -05007686#endif /* MBEDTLS_USE_PSA_CRYPTO */
7687
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +01007688#endif /* MBEDTLS_SSL_PROTO_TLS1 || MBEDTLS_SSL_PROTO_TLS1_1 || \
7689 MBEDTLS_SSL_PROTO_TLS1_2 */
7690
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007691#endif /* MBEDTLS_SSL_TLS_C */