blob: 7526a1e96a30088b35725ad11b8b2024d730aadb [file] [log] [blame]
Paul Bakker5121ce52009-01-03 21:22:43 +00001/*
2 * SSLv3/TLSv1 shared functions
3 *
Bence Szépkúti1e148272020-08-07 13:07:28 +02004 * Copyright The Mbed TLS Contributors
Dave Rodgman7ff79652023-11-03 12:04:52 +00005 * SPDX-License-Identifier: Apache-2.0 OR GPL-2.0-or-later
Paul Bakker5121ce52009-01-03 21:22:43 +00006 */
7/*
8 * The SSL 3.0 specification was drafted by Netscape in 1996,
9 * and became an IETF standard in 1999.
10 *
11 * http://wp.netscape.com/eng/ssl3/
12 * http://www.ietf.org/rfc/rfc2246.txt
13 * http://www.ietf.org/rfc/rfc4346.txt
14 */
15
Gilles Peskinedb09ef62020-06-03 01:43:33 +020016#include "common.h"
Paul Bakker5121ce52009-01-03 21:22:43 +000017
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020018#if defined(MBEDTLS_SSL_TLS_C)
Paul Bakker5121ce52009-01-03 21:22:43 +000019
SimonBd5800b72016-04-26 07:43:27 +010020#include "mbedtls/platform.h"
SimonBd5800b72016-04-26 07:43:27 +010021
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000022#include "mbedtls/ssl.h"
Manuel Pégourié-Gonnard5e94dde2015-05-26 11:57:05 +020023#include "mbedtls/ssl_internal.h"
Janos Follath73c616b2019-12-18 15:07:04 +000024#include "mbedtls/debug.h"
25#include "mbedtls/error.h"
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -050026#include "mbedtls/platform_util.h"
Hanno Beckera835da52019-05-16 12:39:07 +010027#include "mbedtls/version.h"
Gabor Mezeie24dea82021-10-19 12:22:25 +020028#include "mbedtls/constant_time.h"
Paul Bakker0be444a2013-08-27 21:55:01 +020029
Rich Evans00ab4702015-02-06 13:43:58 +000030#include <string.h>
31
Andrzej Kurekd6db9be2019-01-10 05:27:10 -050032#if defined(MBEDTLS_USE_PSA_CRYPTO)
33#include "mbedtls/psa_util.h"
34#include "psa/crypto.h"
35#endif
36
Janos Follath23bdca02016-10-07 14:47:14 +010037#if defined(MBEDTLS_X509_CRT_PARSE_C)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000038#include "mbedtls/oid.h"
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +020039#endif
40
Gilles Peskineff257152025-02-20 13:57:51 +010041#if defined(MBEDTLS_X509_CRT_PARSE_C)
Gilles Peskine3a2f75d2025-02-12 23:28:48 +010042
Gilles Peskinef33c45f2025-02-12 23:53:25 +010043/* A magic value for `ssl->hostname` indicating that
44 * mbedtls_ssl_set_hostname() has been called with `NULL`.
45 * If mbedtls_ssl_set_hostname() has never been called on `ssl`, then
46 * `ssl->hostname == NULL`. */
47static const char *const ssl_hostname_skip_cn_verification = "";
48
Gilles Peskine3a2f75d2025-02-12 23:28:48 +010049#if defined(MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED)
50/** Whether mbedtls_ssl_set_hostname() has been called.
51 *
52 * \param[in] ssl SSL context
53 *
54 * \return \c 1 if mbedtls_ssl_set_hostname() has been called on \p ssl
55 * (including `mbedtls_ssl_set_hostname(ssl, NULL)`),
56 * otherwise \c 0.
57 */
58static int mbedtls_ssl_has_set_hostname_been_called(
59 const mbedtls_ssl_context *ssl)
60{
Gilles Peskine3a2f75d2025-02-12 23:28:48 +010061 return ssl->hostname != NULL;
62}
63#endif
64
65/* Micro-optimization: don't export this function if it isn't needed outside
66 * of this source file. */
67#if !defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
68static
69#endif
70const char *mbedtls_ssl_get_hostname_pointer(const mbedtls_ssl_context *ssl)
71{
Gilles Peskinef33c45f2025-02-12 23:53:25 +010072 if (ssl->hostname == ssl_hostname_skip_cn_verification) {
73 return NULL;
74 }
Gilles Peskine3a2f75d2025-02-12 23:28:48 +010075 return ssl->hostname;
76}
77
78static void mbedtls_ssl_free_hostname(mbedtls_ssl_context *ssl)
79{
Gilles Peskinef33c45f2025-02-12 23:53:25 +010080 if (ssl->hostname != NULL &&
81 ssl->hostname != ssl_hostname_skip_cn_verification) {
Gilles Peskine3a2f75d2025-02-12 23:28:48 +010082 mbedtls_platform_zeroize(ssl->hostname, strlen(ssl->hostname));
83 mbedtls_free(ssl->hostname);
84 }
85 ssl->hostname = NULL;
86}
87
Gilles Peskineff257152025-02-20 13:57:51 +010088int mbedtls_ssl_set_hostname(mbedtls_ssl_context *ssl, const char *hostname)
89{
90 /* Initialize to suppress unnecessary compiler warning */
91 size_t hostname_len = 0;
92
93 /* Check if new hostname is valid before
94 * making any change to current one */
95 if (hostname != NULL) {
96 hostname_len = strlen(hostname);
97
98 if (hostname_len > MBEDTLS_SSL_MAX_HOST_NAME_LEN) {
99 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
100 }
101 }
102
103 /* Now it's clear that we will overwrite the old hostname,
104 * so we can free it safely */
Gilles Peskine3a2f75d2025-02-12 23:28:48 +0100105 mbedtls_ssl_free_hostname(ssl);
Gilles Peskineff257152025-02-20 13:57:51 +0100106
Gilles Peskineff257152025-02-20 13:57:51 +0100107 if (hostname == NULL) {
Gilles Peskinef33c45f2025-02-12 23:53:25 +0100108 /* Passing NULL as hostname clears the old one, but leaves a
109 * special marker to indicate that mbedtls_ssl_set_hostname()
110 * has been called. */
111 /* ssl->hostname should be const, but isn't. We won't actually
112 * write to the buffer, so it's ok to cast away the const. */
113 ssl->hostname = (char *) ssl_hostname_skip_cn_verification;
Gilles Peskineff257152025-02-20 13:57:51 +0100114 } else {
115 ssl->hostname = mbedtls_calloc(1, hostname_len + 1);
116 if (ssl->hostname == NULL) {
Gilles Peskinef33c45f2025-02-12 23:53:25 +0100117 /* mbedtls_ssl_set_hostname() has been called, but unsuccessfully.
118 * Leave ssl->hostname in the same state as if the function had
119 * not been called, i.e. a null pointer. */
Gilles Peskineff257152025-02-20 13:57:51 +0100120 return MBEDTLS_ERR_SSL_ALLOC_FAILED;
121 }
122
123 memcpy(ssl->hostname, hostname, hostname_len);
124
125 ssl->hostname[hostname_len] = '\0';
126 }
127
128 return 0;
129}
130#endif /* MBEDTLS_X509_CRT_PARSE_C */
131
Manuel Pégourié-Gonnard286a1362015-05-13 16:22:05 +0200132#if defined(MBEDTLS_SSL_PROTO_DTLS)
Hanno Becker2b1e3542018-08-06 11:19:13 +0100133
Hanno Beckera0e20d02019-05-15 14:03:01 +0100134#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Beckerf8542cf2019-04-09 15:22:03 +0100135/* Top-level Connection ID API */
136
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100137int mbedtls_ssl_conf_cid(mbedtls_ssl_config *conf,
138 size_t len,
139 int ignore_other_cid)
Hanno Beckerad4a1372019-05-03 13:06:44 +0100140{
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100141 if (len > MBEDTLS_SSL_CID_IN_LEN_MAX) {
142 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
143 }
Hanno Beckerad4a1372019-05-03 13:06:44 +0100144
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100145 if (ignore_other_cid != MBEDTLS_SSL_UNEXPECTED_CID_FAIL &&
146 ignore_other_cid != MBEDTLS_SSL_UNEXPECTED_CID_IGNORE) {
147 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
Hanno Becker611ac772019-05-14 11:45:26 +0100148 }
149
150 conf->ignore_unexpected_cid = ignore_other_cid;
Hanno Beckerad4a1372019-05-03 13:06:44 +0100151 conf->cid_len = len;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100152 return 0;
Hanno Beckerad4a1372019-05-03 13:06:44 +0100153}
154
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100155int mbedtls_ssl_set_cid(mbedtls_ssl_context *ssl,
156 int enable,
157 unsigned char const *own_cid,
158 size_t own_cid_len)
Hanno Beckerf8542cf2019-04-09 15:22:03 +0100159{
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100160 if (ssl->conf->transport != MBEDTLS_SSL_TRANSPORT_DATAGRAM) {
161 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
162 }
Hanno Becker76a79ab2019-05-03 14:38:32 +0100163
Hanno Beckerca092242019-04-25 16:01:49 +0100164 ssl->negotiate_cid = enable;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100165 if (enable == MBEDTLS_SSL_CID_DISABLED) {
166 MBEDTLS_SSL_DEBUG_MSG(3, ("Disable use of CID extension."));
167 return 0;
Hanno Beckerca092242019-04-25 16:01:49 +0100168 }
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100169 MBEDTLS_SSL_DEBUG_MSG(3, ("Enable use of CID extension."));
170 MBEDTLS_SSL_DEBUG_BUF(3, "Own CID", own_cid, own_cid_len);
Hanno Beckerca092242019-04-25 16:01:49 +0100171
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100172 if (own_cid_len != ssl->conf->cid_len) {
173 MBEDTLS_SSL_DEBUG_MSG(3, ("CID length %u does not match CID length %u in config",
174 (unsigned) own_cid_len,
175 (unsigned) ssl->conf->cid_len));
176 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
Hanno Beckerca092242019-04-25 16:01:49 +0100177 }
178
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100179 memcpy(ssl->own_cid, own_cid, own_cid_len);
Hanno Beckerb7ee0cf2019-04-30 14:07:31 +0100180 /* Truncation is not an issue here because
181 * MBEDTLS_SSL_CID_IN_LEN_MAX at most 255. */
182 ssl->own_cid_len = (uint8_t) own_cid_len;
Hanno Beckerca092242019-04-25 16:01:49 +0100183
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100184 return 0;
Hanno Beckerf8542cf2019-04-09 15:22:03 +0100185}
186
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100187int mbedtls_ssl_get_peer_cid(mbedtls_ssl_context *ssl,
188 int *enabled,
189 unsigned char peer_cid[MBEDTLS_SSL_CID_OUT_LEN_MAX],
190 size_t *peer_cid_len)
Hanno Beckerf8542cf2019-04-09 15:22:03 +0100191{
Hanno Beckerf8542cf2019-04-09 15:22:03 +0100192 *enabled = MBEDTLS_SSL_CID_DISABLED;
Hanno Beckerb1f89cd2019-04-26 17:08:02 +0100193
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100194 if (ssl->conf->transport != MBEDTLS_SSL_TRANSPORT_DATAGRAM ||
195 ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER) {
196 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
Hanno Becker76a79ab2019-05-03 14:38:32 +0100197 }
Hanno Beckerb1f89cd2019-04-26 17:08:02 +0100198
Hanno Beckerc5f24222019-05-03 12:54:52 +0100199 /* We report MBEDTLS_SSL_CID_DISABLED in case the CID extensions
200 * were used, but client and server requested the empty CID.
201 * This is indistinguishable from not using the CID extension
202 * in the first place. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100203 if (ssl->transform_in->in_cid_len == 0 &&
204 ssl->transform_in->out_cid_len == 0) {
205 return 0;
Hanno Beckerb1f89cd2019-04-26 17:08:02 +0100206 }
207
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100208 if (peer_cid_len != NULL) {
Hanno Becker615ef172019-05-22 16:50:35 +0100209 *peer_cid_len = ssl->transform_in->out_cid_len;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100210 if (peer_cid != NULL) {
211 memcpy(peer_cid, ssl->transform_in->out_cid,
212 ssl->transform_in->out_cid_len);
Hanno Becker615ef172019-05-22 16:50:35 +0100213 }
214 }
Hanno Beckerb1f89cd2019-04-26 17:08:02 +0100215
216 *enabled = MBEDTLS_SSL_CID_ENABLED;
217
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100218 return 0;
Hanno Beckerf8542cf2019-04-09 15:22:03 +0100219}
Hanno Beckera0e20d02019-05-15 14:03:01 +0100220#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Beckerf8542cf2019-04-09 15:22:03 +0100221
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200222#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +0200223
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200224#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
Manuel Pégourié-Gonnard581e6b62013-07-18 12:32:27 +0200225/*
226 * Convert max_fragment_length codes to length.
227 * RFC 6066 says:
228 * enum{
229 * 2^9(1), 2^10(2), 2^11(3), 2^12(4), (255)
230 * } MaxFragmentLength;
231 * and we add 0 -> extension unused
232 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100233static unsigned int ssl_mfl_code_to_length(int mfl)
Manuel Pégourié-Gonnard581e6b62013-07-18 12:32:27 +0200234{
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100235 switch (mfl) {
236 case MBEDTLS_SSL_MAX_FRAG_LEN_NONE:
237 return MBEDTLS_TLS_EXT_ADV_CONTENT_LEN;
238 case MBEDTLS_SSL_MAX_FRAG_LEN_512:
239 return 512;
240 case MBEDTLS_SSL_MAX_FRAG_LEN_1024:
241 return 1024;
242 case MBEDTLS_SSL_MAX_FRAG_LEN_2048:
243 return 2048;
244 case MBEDTLS_SSL_MAX_FRAG_LEN_4096:
245 return 4096;
246 default:
247 return MBEDTLS_TLS_EXT_ADV_CONTENT_LEN;
Angus Grattond8213d02016-05-25 20:56:48 +1000248 }
249}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200250#endif /* MBEDTLS_SSL_MAX_FRAGMENT_LENGTH */
Manuel Pégourié-Gonnard581e6b62013-07-18 12:32:27 +0200251
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100252int mbedtls_ssl_session_copy(mbedtls_ssl_session *dst,
253 const mbedtls_ssl_session *src)
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200254{
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100255 mbedtls_ssl_session_free(dst);
256 memcpy(dst, src, sizeof(mbedtls_ssl_session));
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200257
吴敬辉0f6c6bc2021-11-29 10:46:35 +0800258#if defined(MBEDTLS_SSL_SESSION_TICKETS) && defined(MBEDTLS_SSL_CLI_C)
259 dst->ticket = NULL;
260#endif
261
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200262#if defined(MBEDTLS_X509_CRT_PARSE_C)
Hanno Becker6d1986e2019-02-07 12:27:42 +0000263
264#if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100265 if (src->peer_cert != NULL) {
Janos Follath865b3eb2019-12-16 11:46:15 +0000266 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker2292d1f2013-09-15 17:06:49 +0200267
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100268 dst->peer_cert = mbedtls_calloc(1, sizeof(mbedtls_x509_crt));
269 if (dst->peer_cert == NULL) {
270 return MBEDTLS_ERR_SSL_ALLOC_FAILED;
271 }
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200272
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100273 mbedtls_x509_crt_init(dst->peer_cert);
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200274
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100275 if ((ret = mbedtls_x509_crt_parse_der(dst->peer_cert, src->peer_cert->raw.p,
276 src->peer_cert->raw.len)) != 0) {
277 mbedtls_free(dst->peer_cert);
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200278 dst->peer_cert = NULL;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100279 return ret;
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200280 }
281 }
Hanno Becker6d1986e2019-02-07 12:27:42 +0000282#else /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100283 if (src->peer_cert_digest != NULL) {
Hanno Becker9198ad12019-02-05 17:00:50 +0000284 dst->peer_cert_digest =
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100285 mbedtls_calloc(1, src->peer_cert_digest_len);
286 if (dst->peer_cert_digest == NULL) {
287 return MBEDTLS_ERR_SSL_ALLOC_FAILED;
288 }
Hanno Becker9198ad12019-02-05 17:00:50 +0000289
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100290 memcpy(dst->peer_cert_digest, src->peer_cert_digest,
291 src->peer_cert_digest_len);
Hanno Becker9198ad12019-02-05 17:00:50 +0000292 dst->peer_cert_digest_type = src->peer_cert_digest_type;
Hanno Beckeraccc5992019-02-25 10:06:59 +0000293 dst->peer_cert_digest_len = src->peer_cert_digest_len;
Hanno Becker9198ad12019-02-05 17:00:50 +0000294 }
295#endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
296
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200297#endif /* MBEDTLS_X509_CRT_PARSE_C */
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200298
Manuel Pégourié-Gonnardb596abf2015-05-20 10:45:29 +0200299#if defined(MBEDTLS_SSL_SESSION_TICKETS) && defined(MBEDTLS_SSL_CLI_C)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100300 if (src->ticket != NULL) {
301 dst->ticket = mbedtls_calloc(1, src->ticket_len);
302 if (dst->ticket == NULL) {
303 return MBEDTLS_ERR_SSL_ALLOC_FAILED;
304 }
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200305
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100306 memcpy(dst->ticket, src->ticket, src->ticket_len);
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200307 }
Manuel Pégourié-Gonnardb596abf2015-05-20 10:45:29 +0200308#endif /* MBEDTLS_SSL_SESSION_TICKETS && MBEDTLS_SSL_CLI_C */
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200309
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100310 return 0;
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200311}
312
Andrzej Kurek0afa2a12020-03-03 10:39:58 -0500313#if defined(MBEDTLS_SSL_VARIABLE_BUFFER_LENGTH)
Manuel Pégourié-Gonnardd904d662022-06-17 10:24:00 +0200314MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100315static int resize_buffer(unsigned char **buffer, size_t len_new, size_t *len_old)
Andrzej Kurek0afa2a12020-03-03 10:39:58 -0500316{
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100317 unsigned char *resized_buffer = mbedtls_calloc(1, len_new);
318 if (resized_buffer == NULL) {
Andrzej Kurek0afa2a12020-03-03 10:39:58 -0500319 return -1;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100320 }
Andrzej Kurek0afa2a12020-03-03 10:39:58 -0500321
322 /* We want to copy len_new bytes when downsizing the buffer, and
323 * len_old bytes when upsizing, so we choose the smaller of two sizes,
324 * to fit one buffer into another. Size checks, ensuring that no data is
325 * lost, are done outside of this function. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100326 memcpy(resized_buffer, *buffer,
327 (len_new < *len_old) ? len_new : *len_old);
328 mbedtls_platform_zeroize(*buffer, *len_old);
329 mbedtls_free(*buffer);
Andrzej Kurek0afa2a12020-03-03 10:39:58 -0500330
331 *buffer = resized_buffer;
332 *len_old = len_new;
333
334 return 0;
335}
Andrzej Kurek4a063792020-10-21 15:08:44 +0200336
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100337static void handle_buffer_resizing(mbedtls_ssl_context *ssl, int downsizing,
338 size_t in_buf_new_len,
339 size_t out_buf_new_len)
Andrzej Kurek4a063792020-10-21 15:08:44 +0200340{
341 int modified = 0;
342 size_t written_in = 0, iv_offset_in = 0, len_offset_in = 0;
343 size_t written_out = 0, iv_offset_out = 0, len_offset_out = 0;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100344 if (ssl->in_buf != NULL) {
Andrzej Kurek4a063792020-10-21 15:08:44 +0200345 written_in = ssl->in_msg - ssl->in_buf;
346 iv_offset_in = ssl->in_iv - ssl->in_buf;
347 len_offset_in = ssl->in_len - ssl->in_buf;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100348 if (downsizing ?
Andrzej Kurek4a063792020-10-21 15:08:44 +0200349 ssl->in_buf_len > in_buf_new_len && ssl->in_left < in_buf_new_len :
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100350 ssl->in_buf_len < in_buf_new_len) {
351 if (resize_buffer(&ssl->in_buf, in_buf_new_len, &ssl->in_buf_len) != 0) {
352 MBEDTLS_SSL_DEBUG_MSG(1, ("input buffer resizing failed - out of memory"));
353 } else {
354 MBEDTLS_SSL_DEBUG_MSG(2, ("Reallocating in_buf to %" MBEDTLS_PRINTF_SIZET,
355 in_buf_new_len));
Andrzej Kurek4a063792020-10-21 15:08:44 +0200356 modified = 1;
357 }
358 }
359 }
360
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100361 if (ssl->out_buf != NULL) {
Andrzej Kurek4a063792020-10-21 15:08:44 +0200362 written_out = ssl->out_msg - ssl->out_buf;
363 iv_offset_out = ssl->out_iv - ssl->out_buf;
364 len_offset_out = ssl->out_len - ssl->out_buf;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100365 if (downsizing ?
Andrzej Kurek4a063792020-10-21 15:08:44 +0200366 ssl->out_buf_len > out_buf_new_len && ssl->out_left < out_buf_new_len :
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100367 ssl->out_buf_len < out_buf_new_len) {
368 if (resize_buffer(&ssl->out_buf, out_buf_new_len, &ssl->out_buf_len) != 0) {
369 MBEDTLS_SSL_DEBUG_MSG(1, ("output buffer resizing failed - out of memory"));
370 } else {
371 MBEDTLS_SSL_DEBUG_MSG(2, ("Reallocating out_buf to %" MBEDTLS_PRINTF_SIZET,
372 out_buf_new_len));
Andrzej Kurek4a063792020-10-21 15:08:44 +0200373 modified = 1;
374 }
375 }
376 }
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100377 if (modified) {
Andrzej Kurek4a063792020-10-21 15:08:44 +0200378 /* Update pointers here to avoid doing it twice. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100379 mbedtls_ssl_reset_in_out_pointers(ssl);
Andrzej Kurek4a063792020-10-21 15:08:44 +0200380 /* Fields below might not be properly updated with record
381 * splitting or with CID, so they are manually updated here. */
382 ssl->out_msg = ssl->out_buf + written_out;
383 ssl->out_len = ssl->out_buf + len_offset_out;
384 ssl->out_iv = ssl->out_buf + iv_offset_out;
385
386 ssl->in_msg = ssl->in_buf + written_in;
387 ssl->in_len = ssl->in_buf + len_offset_in;
388 ssl->in_iv = ssl->in_buf + iv_offset_in;
389 }
390}
Andrzej Kurek0afa2a12020-03-03 10:39:58 -0500391#endif /* MBEDTLS_SSL_VARIABLE_BUFFER_LENGTH */
392
Paul Bakker5121ce52009-01-03 21:22:43 +0000393/*
394 * Key material generation
395 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200396#if defined(MBEDTLS_SSL_PROTO_SSL3)
Manuel Pégourié-Gonnardd904d662022-06-17 10:24:00 +0200397MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100398static int ssl3_prf(const unsigned char *secret, size_t slen,
399 const char *label,
400 const unsigned char *random, size_t rlen,
401 unsigned char *dstbuf, size_t dlen)
Paul Bakker5f70b252012-09-13 14:23:06 +0000402{
Andres Amaya Garcia33952502017-07-20 16:29:16 +0100403 int ret = 0;
Paul Bakker5f70b252012-09-13 14:23:06 +0000404 size_t i;
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +0200405 mbedtls_md5_context md5;
406 mbedtls_sha1_context sha1;
Paul Bakker5f70b252012-09-13 14:23:06 +0000407 unsigned char padding[16];
408 unsigned char sha1sum[20];
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100409 ((void) label);
Paul Bakker5f70b252012-09-13 14:23:06 +0000410
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100411 mbedtls_md5_init(&md5);
412 mbedtls_sha1_init(&sha1);
Paul Bakker5b4af392014-06-26 12:09:34 +0200413
Paul Bakker5f70b252012-09-13 14:23:06 +0000414 /*
415 * SSLv3:
416 * block =
417 * MD5( secret + SHA1( 'A' + secret + random ) ) +
418 * MD5( secret + SHA1( 'BB' + secret + random ) ) +
419 * MD5( secret + SHA1( 'CCC' + secret + random ) ) +
420 * ...
421 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100422 for (i = 0; i < dlen / 16; i++) {
423 memset(padding, (unsigned char) ('A' + i), 1 + i);
Paul Bakker5f70b252012-09-13 14:23:06 +0000424
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100425 if ((ret = mbedtls_sha1_starts_ret(&sha1)) != 0) {
Andres Amaya Garcia1a607a12017-06-29 17:09:42 +0100426 goto exit;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100427 }
428 if ((ret = mbedtls_sha1_update_ret(&sha1, padding, 1 + i)) != 0) {
Andres Amaya Garcia1a607a12017-06-29 17:09:42 +0100429 goto exit;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100430 }
431 if ((ret = mbedtls_sha1_update_ret(&sha1, secret, slen)) != 0) {
Andres Amaya Garcia1a607a12017-06-29 17:09:42 +0100432 goto exit;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100433 }
434 if ((ret = mbedtls_sha1_update_ret(&sha1, random, rlen)) != 0) {
Andres Amaya Garcia1a607a12017-06-29 17:09:42 +0100435 goto exit;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100436 }
437 if ((ret = mbedtls_sha1_finish_ret(&sha1, sha1sum)) != 0) {
Andres Amaya Garcia1a607a12017-06-29 17:09:42 +0100438 goto exit;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100439 }
Paul Bakker5f70b252012-09-13 14:23:06 +0000440
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100441 if ((ret = mbedtls_md5_starts_ret(&md5)) != 0) {
Andres Amaya Garcia1a607a12017-06-29 17:09:42 +0100442 goto exit;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100443 }
444 if ((ret = mbedtls_md5_update_ret(&md5, secret, slen)) != 0) {
Andres Amaya Garcia1a607a12017-06-29 17:09:42 +0100445 goto exit;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100446 }
447 if ((ret = mbedtls_md5_update_ret(&md5, sha1sum, 20)) != 0) {
Andres Amaya Garcia1a607a12017-06-29 17:09:42 +0100448 goto exit;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100449 }
450 if ((ret = mbedtls_md5_finish_ret(&md5, dstbuf + i * 16)) != 0) {
Andres Amaya Garcia1a607a12017-06-29 17:09:42 +0100451 goto exit;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100452 }
Paul Bakker5f70b252012-09-13 14:23:06 +0000453 }
454
Andres Amaya Garcia1a607a12017-06-29 17:09:42 +0100455exit:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100456 mbedtls_md5_free(&md5);
457 mbedtls_sha1_free(&sha1);
Paul Bakker5f70b252012-09-13 14:23:06 +0000458
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100459 mbedtls_platform_zeroize(padding, sizeof(padding));
460 mbedtls_platform_zeroize(sha1sum, sizeof(sha1sum));
Paul Bakker5f70b252012-09-13 14:23:06 +0000461
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100462 return ret;
Paul Bakker5f70b252012-09-13 14:23:06 +0000463}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200464#endif /* MBEDTLS_SSL_PROTO_SSL3 */
Paul Bakker5f70b252012-09-13 14:23:06 +0000465
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200466#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1)
Manuel Pégourié-Gonnardd904d662022-06-17 10:24:00 +0200467MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100468static int tls1_prf(const unsigned char *secret, size_t slen,
469 const char *label,
470 const unsigned char *random, size_t rlen,
471 unsigned char *dstbuf, size_t dlen)
Paul Bakker5121ce52009-01-03 21:22:43 +0000472{
Paul Bakker23986e52011-04-24 08:57:21 +0000473 size_t nb, hs;
474 size_t i, j, k;
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +0200475 const unsigned char *S1, *S2;
Ron Eldor3b350852019-05-07 18:31:49 +0300476 unsigned char *tmp;
477 size_t tmp_len = 0;
Paul Bakker5121ce52009-01-03 21:22:43 +0000478 unsigned char h_i[20];
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200479 const mbedtls_md_info_t *md_info;
480 mbedtls_md_context_t md_ctx;
Janos Follath865b3eb2019-12-16 11:46:15 +0000481 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnardb7fcca32015-03-26 11:41:28 +0100482
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100483 mbedtls_md_init(&md_ctx);
Paul Bakker5121ce52009-01-03 21:22:43 +0000484
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100485 tmp_len = 20 + strlen(label) + rlen;
486 tmp = mbedtls_calloc(1, tmp_len);
487 if (tmp == NULL) {
Ron Eldor3b350852019-05-07 18:31:49 +0300488 ret = MBEDTLS_ERR_SSL_ALLOC_FAILED;
489 goto exit;
490 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000491
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100492 hs = (slen + 1) / 2;
Paul Bakker5121ce52009-01-03 21:22:43 +0000493 S1 = secret;
494 S2 = secret + slen - hs;
495
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100496 nb = strlen(label);
497 memcpy(tmp + 20, label, nb);
498 memcpy(tmp + 20 + nb, random, rlen);
Paul Bakker5121ce52009-01-03 21:22:43 +0000499 nb += rlen;
500
501 /*
502 * First compute P_md5(secret,label+random)[0..dlen]
503 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100504 if ((md_info = mbedtls_md_info_from_type(MBEDTLS_MD_MD5)) == NULL) {
Ron Eldor3b350852019-05-07 18:31:49 +0300505 ret = MBEDTLS_ERR_SSL_INTERNAL_ERROR;
506 goto exit;
507 }
Manuel Pégourié-Gonnard7da726b2015-03-24 18:08:19 +0100508
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100509 if ((ret = mbedtls_md_setup(&md_ctx, md_info, 1)) != 0) {
Ron Eldor3b350852019-05-07 18:31:49 +0300510 goto exit;
511 }
Manuel Pégourié-Gonnardb7fcca32015-03-26 11:41:28 +0100512
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100513 ret = mbedtls_md_hmac_starts(&md_ctx, S1, hs);
514 if (ret != 0) {
Gilles Peskine2b3f21d2021-12-10 21:35:10 +0100515 goto exit;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100516 }
517 ret = mbedtls_md_hmac_update(&md_ctx, tmp + 20, nb);
518 if (ret != 0) {
Gilles Peskine2b3f21d2021-12-10 21:35:10 +0100519 goto exit;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100520 }
521 ret = mbedtls_md_hmac_finish(&md_ctx, 4 + tmp);
522 if (ret != 0) {
Gilles Peskine2b3f21d2021-12-10 21:35:10 +0100523 goto exit;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100524 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000525
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100526 for (i = 0; i < dlen; i += 16) {
527 ret = mbedtls_md_hmac_reset(&md_ctx);
528 if (ret != 0) {
Gilles Peskine2b3f21d2021-12-10 21:35:10 +0100529 goto exit;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100530 }
531 ret = mbedtls_md_hmac_update(&md_ctx, 4 + tmp, 16 + nb);
532 if (ret != 0) {
Gilles Peskine2b3f21d2021-12-10 21:35:10 +0100533 goto exit;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100534 }
535 ret = mbedtls_md_hmac_finish(&md_ctx, h_i);
536 if (ret != 0) {
Gilles Peskine2b3f21d2021-12-10 21:35:10 +0100537 goto exit;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100538 }
Manuel Pégourié-Gonnardb7fcca32015-03-26 11:41:28 +0100539
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100540 ret = mbedtls_md_hmac_reset(&md_ctx);
541 if (ret != 0) {
Gilles Peskine2b3f21d2021-12-10 21:35:10 +0100542 goto exit;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100543 }
544 ret = mbedtls_md_hmac_update(&md_ctx, 4 + tmp, 16);
545 if (ret != 0) {
Gilles Peskine2b3f21d2021-12-10 21:35:10 +0100546 goto exit;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100547 }
548 ret = mbedtls_md_hmac_finish(&md_ctx, 4 + tmp);
549 if (ret != 0) {
Gilles Peskine2b3f21d2021-12-10 21:35:10 +0100550 goto exit;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100551 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000552
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100553 k = (i + 16 > dlen) ? dlen % 16 : 16;
Paul Bakker5121ce52009-01-03 21:22:43 +0000554
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100555 for (j = 0; j < k; j++) {
Paul Bakker5121ce52009-01-03 21:22:43 +0000556 dstbuf[i + j] = h_i[j];
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100557 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000558 }
559
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100560 mbedtls_md_free(&md_ctx);
Manuel Pégourié-Gonnardb7fcca32015-03-26 11:41:28 +0100561
Paul Bakker5121ce52009-01-03 21:22:43 +0000562 /*
563 * XOR out with P_sha1(secret,label+random)[0..dlen]
564 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100565 if ((md_info = mbedtls_md_info_from_type(MBEDTLS_MD_SHA1)) == NULL) {
Ron Eldor3b350852019-05-07 18:31:49 +0300566 ret = MBEDTLS_ERR_SSL_INTERNAL_ERROR;
567 goto exit;
568 }
Manuel Pégourié-Gonnard7da726b2015-03-24 18:08:19 +0100569
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100570 if ((ret = mbedtls_md_setup(&md_ctx, md_info, 1)) != 0) {
Ron Eldor3b350852019-05-07 18:31:49 +0300571 goto exit;
572 }
Manuel Pégourié-Gonnardb7fcca32015-03-26 11:41:28 +0100573
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100574 ret = mbedtls_md_hmac_starts(&md_ctx, S2, hs);
575 if (ret != 0) {
Gilles Peskine2b3f21d2021-12-10 21:35:10 +0100576 goto exit;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100577 }
578 ret = mbedtls_md_hmac_update(&md_ctx, tmp + 20, nb);
579 if (ret != 0) {
Gilles Peskine2b3f21d2021-12-10 21:35:10 +0100580 goto exit;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100581 }
582 ret = mbedtls_md_hmac_finish(&md_ctx, tmp);
583 if (ret != 0) {
Gilles Peskine2b3f21d2021-12-10 21:35:10 +0100584 goto exit;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100585 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000586
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100587 for (i = 0; i < dlen; i += 20) {
588 ret = mbedtls_md_hmac_reset(&md_ctx);
589 if (ret != 0) {
Gilles Peskine2b3f21d2021-12-10 21:35:10 +0100590 goto exit;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100591 }
592 ret = mbedtls_md_hmac_update(&md_ctx, tmp, 20 + nb);
593 if (ret != 0) {
Gilles Peskine2b3f21d2021-12-10 21:35:10 +0100594 goto exit;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100595 }
596 ret = mbedtls_md_hmac_finish(&md_ctx, h_i);
597 if (ret != 0) {
Gilles Peskine2b3f21d2021-12-10 21:35:10 +0100598 goto exit;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100599 }
Manuel Pégourié-Gonnardb7fcca32015-03-26 11:41:28 +0100600
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100601 ret = mbedtls_md_hmac_reset(&md_ctx);
602 if (ret != 0) {
Gilles Peskine2b3f21d2021-12-10 21:35:10 +0100603 goto exit;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100604 }
605 ret = mbedtls_md_hmac_update(&md_ctx, tmp, 20);
606 if (ret != 0) {
Gilles Peskine2b3f21d2021-12-10 21:35:10 +0100607 goto exit;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100608 }
609 ret = mbedtls_md_hmac_finish(&md_ctx, tmp);
610 if (ret != 0) {
Gilles Peskine2b3f21d2021-12-10 21:35:10 +0100611 goto exit;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100612 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000613
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100614 k = (i + 20 > dlen) ? dlen % 20 : 20;
Paul Bakker5121ce52009-01-03 21:22:43 +0000615
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100616 for (j = 0; j < k; j++) {
617 dstbuf[i + j] = (unsigned char) (dstbuf[i + j] ^ h_i[j]);
618 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000619 }
620
Ron Eldor3b350852019-05-07 18:31:49 +0300621exit:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100622 mbedtls_md_free(&md_ctx);
Manuel Pégourié-Gonnardb7fcca32015-03-26 11:41:28 +0100623
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100624 mbedtls_platform_zeroize(tmp, tmp_len);
625 mbedtls_platform_zeroize(h_i, sizeof(h_i));
Paul Bakker5121ce52009-01-03 21:22:43 +0000626
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100627 mbedtls_free(tmp);
628 return ret;
Paul Bakker5121ce52009-01-03 21:22:43 +0000629}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200630#endif /* MBEDTLS_SSL_PROTO_TLS1) || MBEDTLS_SSL_PROTO_TLS1_1 */
Paul Bakker5121ce52009-01-03 21:22:43 +0000631
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200632#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
Andrzej Kurekc929a822019-01-14 03:51:11 -0500633#if defined(MBEDTLS_USE_PSA_CRYPTO)
k-stachowiak81053a52019-08-17 10:30:28 +0200634
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100635static psa_status_t setup_psa_key_derivation(psa_key_derivation_operation_t *derivation,
636 psa_key_id_t key,
637 psa_algorithm_t alg,
638 const unsigned char *seed, size_t seed_length,
639 const unsigned char *label, size_t label_length,
640 size_t capacity)
k-stachowiak81053a52019-08-17 10:30:28 +0200641{
642 psa_status_t status;
643
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100644 status = psa_key_derivation_setup(derivation, alg);
645 if (status != PSA_SUCCESS) {
646 return status;
647 }
k-stachowiak81053a52019-08-17 10:30:28 +0200648
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100649 if (PSA_ALG_IS_TLS12_PRF(alg) || PSA_ALG_IS_TLS12_PSK_TO_MS(alg)) {
650 status = psa_key_derivation_input_bytes(derivation,
651 PSA_KEY_DERIVATION_INPUT_SEED,
652 seed, seed_length);
653 if (status != PSA_SUCCESS) {
654 return status;
655 }
k-stachowiak81053a52019-08-17 10:30:28 +0200656
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100657 if (mbedtls_svc_key_id_is_null(key)) {
Gilles Peskine311f54d2019-09-23 18:19:22 +0200658 status = psa_key_derivation_input_bytes(
659 derivation, PSA_KEY_DERIVATION_INPUT_SECRET,
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100660 NULL, 0);
661 } else {
Gilles Peskine311f54d2019-09-23 18:19:22 +0200662 status = psa_key_derivation_input_key(
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100663 derivation, PSA_KEY_DERIVATION_INPUT_SECRET, key);
Gilles Peskine311f54d2019-09-23 18:19:22 +0200664 }
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100665 if (status != PSA_SUCCESS) {
666 return status;
667 }
k-stachowiak81053a52019-08-17 10:30:28 +0200668
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100669 status = psa_key_derivation_input_bytes(derivation,
670 PSA_KEY_DERIVATION_INPUT_LABEL,
671 label, label_length);
672 if (status != PSA_SUCCESS) {
673 return status;
674 }
675 } else {
676 return PSA_ERROR_NOT_SUPPORTED;
k-stachowiak81053a52019-08-17 10:30:28 +0200677 }
678
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100679 status = psa_key_derivation_set_capacity(derivation, capacity);
680 if (status != PSA_SUCCESS) {
681 return status;
682 }
k-stachowiak81053a52019-08-17 10:30:28 +0200683
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100684 return PSA_SUCCESS;
k-stachowiak81053a52019-08-17 10:30:28 +0200685}
686
Manuel Pégourié-Gonnardd904d662022-06-17 10:24:00 +0200687MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100688static int tls_prf_generic(mbedtls_md_type_t md_type,
689 const unsigned char *secret, size_t slen,
690 const char *label,
691 const unsigned char *random, size_t rlen,
692 unsigned char *dstbuf, size_t dlen)
Andrzej Kurekc929a822019-01-14 03:51:11 -0500693{
694 psa_status_t status;
695 psa_algorithm_t alg;
Ronald Croncf56a0a2020-08-04 09:51:30 +0200696 psa_key_id_t master_key = MBEDTLS_SVC_KEY_ID_INIT;
Janos Follathda6ac012019-08-16 13:47:29 +0100697 psa_key_derivation_operation_t derivation =
Janos Follath8dee8772019-07-30 12:53:32 +0100698 PSA_KEY_DERIVATION_OPERATION_INIT;
Andrzej Kurekc929a822019-01-14 03:51:11 -0500699
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100700 if (md_type == MBEDTLS_MD_SHA384) {
Andrzej Kurekc929a822019-01-14 03:51:11 -0500701 alg = PSA_ALG_TLS12_PRF(PSA_ALG_SHA_384);
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100702 } else {
Andrzej Kurekc929a822019-01-14 03:51:11 -0500703 alg = PSA_ALG_TLS12_PRF(PSA_ALG_SHA_256);
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100704 }
Andrzej Kurekc929a822019-01-14 03:51:11 -0500705
Gilles Peskine311f54d2019-09-23 18:19:22 +0200706 /* Normally a "secret" should be long enough to be impossible to
707 * find by brute force, and in particular should not be empty. But
708 * this PRF is also used to derive an IV, in particular in EAP-TLS,
709 * and for this use case it makes sense to have a 0-length "secret".
710 * Since the key API doesn't allow importing a key of length 0,
Ronald Croncf56a0a2020-08-04 09:51:30 +0200711 * keep master_key=0, which setup_psa_key_derivation() understands
Gilles Peskine311f54d2019-09-23 18:19:22 +0200712 * to mean a 0-length "secret" input. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100713 if (slen != 0) {
Gilles Peskine311f54d2019-09-23 18:19:22 +0200714 psa_key_attributes_t key_attributes = psa_key_attributes_init();
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100715 psa_set_key_usage_flags(&key_attributes, PSA_KEY_USAGE_DERIVE);
716 psa_set_key_algorithm(&key_attributes, alg);
717 psa_set_key_type(&key_attributes, PSA_KEY_TYPE_DERIVE);
Andrzej Kurekc929a822019-01-14 03:51:11 -0500718
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100719 status = psa_import_key(&key_attributes, secret, slen, &master_key);
720 if (status != PSA_SUCCESS) {
721 return MBEDTLS_ERR_SSL_HW_ACCEL_FAILED;
722 }
Gilles Peskine311f54d2019-09-23 18:19:22 +0200723 }
Andrzej Kurekc929a822019-01-14 03:51:11 -0500724
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100725 status = setup_psa_key_derivation(&derivation,
726 master_key, alg,
727 random, rlen,
728 (unsigned char const *) label,
729 (size_t) strlen(label),
730 dlen);
731 if (status != PSA_SUCCESS) {
732 psa_key_derivation_abort(&derivation);
733 psa_destroy_key(master_key);
734 return MBEDTLS_ERR_SSL_HW_ACCEL_FAILED;
Andrzej Kurekc929a822019-01-14 03:51:11 -0500735 }
736
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100737 status = psa_key_derivation_output_bytes(&derivation, dstbuf, dlen);
738 if (status != PSA_SUCCESS) {
739 psa_key_derivation_abort(&derivation);
740 psa_destroy_key(master_key);
741 return MBEDTLS_ERR_SSL_HW_ACCEL_FAILED;
Andrzej Kurekc929a822019-01-14 03:51:11 -0500742 }
743
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100744 status = psa_key_derivation_abort(&derivation);
745 if (status != PSA_SUCCESS) {
746 psa_destroy_key(master_key);
747 return MBEDTLS_ERR_SSL_HW_ACCEL_FAILED;
Andrzej Kurek70737ca2019-01-14 05:37:13 -0500748 }
Andrzej Kurekc929a822019-01-14 03:51:11 -0500749
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100750 if (!mbedtls_svc_key_id_is_null(master_key)) {
751 status = psa_destroy_key(master_key);
752 }
753 if (status != PSA_SUCCESS) {
754 return MBEDTLS_ERR_SSL_HW_ACCEL_FAILED;
755 }
Andrzej Kurekc929a822019-01-14 03:51:11 -0500756
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100757 return 0;
Andrzej Kurekc929a822019-01-14 03:51:11 -0500758}
759
760#else /* MBEDTLS_USE_PSA_CRYPTO */
761
Manuel Pégourié-Gonnardd904d662022-06-17 10:24:00 +0200762MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100763static int tls_prf_generic(mbedtls_md_type_t md_type,
764 const unsigned char *secret, size_t slen,
765 const char *label,
766 const unsigned char *random, size_t rlen,
767 unsigned char *dstbuf, size_t dlen)
Paul Bakker1ef83d62012-04-11 12:09:53 +0000768{
769 size_t nb;
Manuel Pégourié-Gonnard6890c6b2015-03-26 11:11:49 +0100770 size_t i, j, k, md_len;
Ron Eldor3b350852019-05-07 18:31:49 +0300771 unsigned char *tmp;
772 size_t tmp_len = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200773 unsigned char h_i[MBEDTLS_MD_MAX_SIZE];
774 const mbedtls_md_info_t *md_info;
775 mbedtls_md_context_t md_ctx;
Janos Follath865b3eb2019-12-16 11:46:15 +0000776 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnardb7fcca32015-03-26 11:41:28 +0100777
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100778 mbedtls_md_init(&md_ctx);
Paul Bakker1ef83d62012-04-11 12:09:53 +0000779
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100780 if ((md_info = mbedtls_md_info_from_type(md_type)) == NULL) {
781 return MBEDTLS_ERR_SSL_INTERNAL_ERROR;
782 }
Manuel Pégourié-Gonnard6890c6b2015-03-26 11:11:49 +0100783
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100784 md_len = mbedtls_md_get_size(md_info);
Manuel Pégourié-Gonnard6890c6b2015-03-26 11:11:49 +0100785
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100786 tmp_len = md_len + strlen(label) + rlen;
787 tmp = mbedtls_calloc(1, tmp_len);
788 if (tmp == NULL) {
Ron Eldor3b350852019-05-07 18:31:49 +0300789 ret = MBEDTLS_ERR_SSL_ALLOC_FAILED;
790 goto exit;
791 }
Paul Bakker1ef83d62012-04-11 12:09:53 +0000792
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100793 nb = strlen(label);
794 memcpy(tmp + md_len, label, nb);
795 memcpy(tmp + md_len + nb, random, rlen);
Paul Bakker1ef83d62012-04-11 12:09:53 +0000796 nb += rlen;
797
798 /*
799 * Compute P_<hash>(secret, label + random)[0..dlen]
800 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100801 if ((ret = mbedtls_md_setup(&md_ctx, md_info, 1)) != 0) {
Ron Eldor3b350852019-05-07 18:31:49 +0300802 goto exit;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100803 }
Manuel Pégourié-Gonnardb7fcca32015-03-26 11:41:28 +0100804
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100805 ret = mbedtls_md_hmac_starts(&md_ctx, secret, slen);
806 if (ret != 0) {
Gilles Peskine2b3f21d2021-12-10 21:35:10 +0100807 goto exit;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100808 }
809 ret = mbedtls_md_hmac_update(&md_ctx, tmp + md_len, nb);
810 if (ret != 0) {
Gilles Peskine2b3f21d2021-12-10 21:35:10 +0100811 goto exit;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100812 }
813 ret = mbedtls_md_hmac_finish(&md_ctx, tmp);
814 if (ret != 0) {
Gilles Peskine2b3f21d2021-12-10 21:35:10 +0100815 goto exit;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100816 }
Manuel Pégourié-Gonnard7da726b2015-03-24 18:08:19 +0100817
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100818 for (i = 0; i < dlen; i += md_len) {
819 ret = mbedtls_md_hmac_reset(&md_ctx);
820 if (ret != 0) {
Gilles Peskine2b3f21d2021-12-10 21:35:10 +0100821 goto exit;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100822 }
823 ret = mbedtls_md_hmac_update(&md_ctx, tmp, md_len + nb);
824 if (ret != 0) {
Gilles Peskine2b3f21d2021-12-10 21:35:10 +0100825 goto exit;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100826 }
827 ret = mbedtls_md_hmac_finish(&md_ctx, h_i);
828 if (ret != 0) {
Gilles Peskine2b3f21d2021-12-10 21:35:10 +0100829 goto exit;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100830 }
Manuel Pégourié-Gonnardb7fcca32015-03-26 11:41:28 +0100831
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100832 ret = mbedtls_md_hmac_reset(&md_ctx);
833 if (ret != 0) {
Gilles Peskine2b3f21d2021-12-10 21:35:10 +0100834 goto exit;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100835 }
836 ret = mbedtls_md_hmac_update(&md_ctx, tmp, md_len);
837 if (ret != 0) {
Gilles Peskine2b3f21d2021-12-10 21:35:10 +0100838 goto exit;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100839 }
840 ret = mbedtls_md_hmac_finish(&md_ctx, tmp);
841 if (ret != 0) {
Gilles Peskine2b3f21d2021-12-10 21:35:10 +0100842 goto exit;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100843 }
Paul Bakker1ef83d62012-04-11 12:09:53 +0000844
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100845 k = (i + md_len > dlen) ? dlen % md_len : md_len;
Paul Bakker1ef83d62012-04-11 12:09:53 +0000846
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100847 for (j = 0; j < k; j++) {
Paul Bakker1ef83d62012-04-11 12:09:53 +0000848 dstbuf[i + j] = h_i[j];
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100849 }
Paul Bakker1ef83d62012-04-11 12:09:53 +0000850 }
851
Ron Eldor3b350852019-05-07 18:31:49 +0300852exit:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100853 mbedtls_md_free(&md_ctx);
Manuel Pégourié-Gonnardb7fcca32015-03-26 11:41:28 +0100854
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100855 if (tmp != NULL) {
856 mbedtls_platform_zeroize(tmp, tmp_len);
857 }
Dave Rodgman369f4952022-11-01 16:08:14 +0000858
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100859 mbedtls_platform_zeroize(h_i, sizeof(h_i));
Paul Bakker1ef83d62012-04-11 12:09:53 +0000860
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100861 mbedtls_free(tmp);
Ron Eldor3b350852019-05-07 18:31:49 +0300862
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100863 return ret;
Paul Bakker1ef83d62012-04-11 12:09:53 +0000864}
Andrzej Kurekc929a822019-01-14 03:51:11 -0500865#endif /* MBEDTLS_USE_PSA_CRYPTO */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200866#if defined(MBEDTLS_SHA256_C)
Manuel Pégourié-Gonnardd904d662022-06-17 10:24:00 +0200867MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100868static int tls_prf_sha256(const unsigned char *secret, size_t slen,
869 const char *label,
870 const unsigned char *random, size_t rlen,
871 unsigned char *dstbuf, size_t dlen)
Manuel Pégourié-Gonnard6890c6b2015-03-26 11:11:49 +0100872{
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100873 return tls_prf_generic(MBEDTLS_MD_SHA256, secret, slen,
874 label, random, rlen, dstbuf, dlen);
Manuel Pégourié-Gonnard6890c6b2015-03-26 11:11:49 +0100875}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200876#endif /* MBEDTLS_SHA256_C */
Paul Bakker1ef83d62012-04-11 12:09:53 +0000877
Gilles Peskined2d59372021-05-12 22:43:27 +0200878#if defined(MBEDTLS_SHA512_C) && !defined(MBEDTLS_SHA512_NO_SHA384)
Manuel Pégourié-Gonnardd904d662022-06-17 10:24:00 +0200879MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100880static int tls_prf_sha384(const unsigned char *secret, size_t slen,
881 const char *label,
882 const unsigned char *random, size_t rlen,
883 unsigned char *dstbuf, size_t dlen)
Paul Bakkerca4ab492012-04-18 14:23:57 +0000884{
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100885 return tls_prf_generic(MBEDTLS_MD_SHA384, secret, slen,
886 label, random, rlen, dstbuf, dlen);
Paul Bakkerca4ab492012-04-18 14:23:57 +0000887}
Gilles Peskined2d59372021-05-12 22:43:27 +0200888#endif /* MBEDTLS_SHA512_C && !MBEDTLS_SHA512_NO_SHA384 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200889#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakkerca4ab492012-04-18 14:23:57 +0000890
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100891static void ssl_update_checksum_start(mbedtls_ssl_context *, const unsigned char *, size_t);
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200892
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200893#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1) || \
894 defined(MBEDTLS_SSL_PROTO_TLS1_1)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100895static void ssl_update_checksum_md5sha1(mbedtls_ssl_context *, const unsigned char *, size_t);
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200896#endif
Paul Bakker380da532012-04-18 16:10:25 +0000897
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200898#if defined(MBEDTLS_SSL_PROTO_SSL3)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100899static void ssl_calc_verify_ssl(const mbedtls_ssl_context *, unsigned char *, size_t *);
900static void ssl_calc_finished_ssl(mbedtls_ssl_context *, unsigned char *, int);
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200901#endif
902
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200903#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100904static void ssl_calc_verify_tls(const mbedtls_ssl_context *, unsigned char *, size_t *);
905static void ssl_calc_finished_tls(mbedtls_ssl_context *, unsigned char *, int);
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200906#endif
907
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200908#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
909#if defined(MBEDTLS_SHA256_C)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100910static void ssl_update_checksum_sha256(mbedtls_ssl_context *, const unsigned char *, size_t);
911static void ssl_calc_verify_tls_sha256(const mbedtls_ssl_context *, unsigned char *, size_t *);
912static void ssl_calc_finished_tls_sha256(mbedtls_ssl_context *, unsigned char *, int);
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200913#endif
Paul Bakker769075d2012-11-24 11:26:46 +0100914
Gilles Peskined2d59372021-05-12 22:43:27 +0200915#if defined(MBEDTLS_SHA512_C) && !defined(MBEDTLS_SHA512_NO_SHA384)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100916static void ssl_update_checksum_sha384(mbedtls_ssl_context *, const unsigned char *, size_t);
917static void ssl_calc_verify_tls_sha384(const mbedtls_ssl_context *, unsigned char *, size_t *);
918static void ssl_calc_finished_tls_sha384(mbedtls_ssl_context *, unsigned char *, int);
Paul Bakker769075d2012-11-24 11:26:46 +0100919#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200920#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakker1ef83d62012-04-11 12:09:53 +0000921
Manuel Pégourié-Gonnard45be3d82019-02-18 23:35:14 +0100922#if defined(MBEDTLS_KEY_EXCHANGE_PSK_ENABLED) && \
Hanno Becker7d0a5692018-10-23 15:26:22 +0100923 defined(MBEDTLS_USE_PSA_CRYPTO)
Manuel Pégourié-Gonnardd904d662022-06-17 10:24:00 +0200924MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100925static int ssl_use_opaque_psk(mbedtls_ssl_context const *ssl)
Hanno Becker7d0a5692018-10-23 15:26:22 +0100926{
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100927 if (ssl->conf->f_psk != NULL) {
Hanno Becker7d0a5692018-10-23 15:26:22 +0100928 /* If we've used a callback to select the PSK,
929 * the static configuration is irrelevant. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100930 if (!mbedtls_svc_key_id_is_null(ssl->handshake->psk_opaque)) {
931 return 1;
932 }
Hanno Becker7d0a5692018-10-23 15:26:22 +0100933
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100934 return 0;
Hanno Becker7d0a5692018-10-23 15:26:22 +0100935 }
936
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100937 if (!mbedtls_svc_key_id_is_null(ssl->conf->psk_opaque)) {
938 return 1;
939 }
Hanno Becker7d0a5692018-10-23 15:26:22 +0100940
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100941 return 0;
Hanno Becker7d0a5692018-10-23 15:26:22 +0100942}
943#endif /* MBEDTLS_USE_PSA_CRYPTO &&
Manuel Pégourié-Gonnard45be3d82019-02-18 23:35:14 +0100944 MBEDTLS_KEY_EXCHANGE_PSK_ENABLED */
Hanno Becker7d0a5692018-10-23 15:26:22 +0100945
Ron Eldorcf280092019-05-14 20:19:13 +0300946#if defined(MBEDTLS_SSL_EXPORT_KEYS)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100947static mbedtls_tls_prf_types tls_prf_get_type(mbedtls_ssl_tls_prf_cb *tls_prf)
Ron Eldorcf280092019-05-14 20:19:13 +0300948{
949#if defined(MBEDTLS_SSL_PROTO_SSL3)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100950 if (tls_prf == ssl3_prf) {
951 return MBEDTLS_SSL_TLS_PRF_SSL3;
952 } else
Ron Eldorcf280092019-05-14 20:19:13 +0300953#endif
954#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100955 if (tls_prf == tls1_prf) {
956 return MBEDTLS_SSL_TLS_PRF_TLS1;
957 } else
Ron Eldorcf280092019-05-14 20:19:13 +0300958#endif
959#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
Gilles Peskined2d59372021-05-12 22:43:27 +0200960#if defined(MBEDTLS_SHA512_C) && !defined(MBEDTLS_SHA512_NO_SHA384)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100961 if (tls_prf == tls_prf_sha384) {
962 return MBEDTLS_SSL_TLS_PRF_SHA384;
963 } else
Ron Eldorcf280092019-05-14 20:19:13 +0300964#endif
965#if defined(MBEDTLS_SHA256_C)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100966 if (tls_prf == tls_prf_sha256) {
967 return MBEDTLS_SSL_TLS_PRF_SHA256;
968 } else
Ron Eldorcf280092019-05-14 20:19:13 +0300969#endif
970#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100971 return MBEDTLS_SSL_TLS_PRF_NONE;
Ron Eldorcf280092019-05-14 20:19:13 +0300972}
973#endif /* MBEDTLS_SSL_EXPORT_KEYS */
974
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100975int mbedtls_ssl_tls_prf(const mbedtls_tls_prf_types prf,
976 const unsigned char *secret, size_t slen,
977 const char *label,
978 const unsigned char *random, size_t rlen,
979 unsigned char *dstbuf, size_t dlen)
Ron Eldor51d3ab52019-05-12 14:54:30 +0300980{
981 mbedtls_ssl_tls_prf_cb *tls_prf = NULL;
982
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100983 switch (prf) {
Ron Eldor51d3ab52019-05-12 14:54:30 +0300984#if defined(MBEDTLS_SSL_PROTO_SSL3)
985 case MBEDTLS_SSL_TLS_PRF_SSL3:
986 tls_prf = ssl3_prf;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100987 break;
Ron Eldord2f25f72019-05-15 14:54:22 +0300988#endif /* MBEDTLS_SSL_PROTO_SSL3 */
Ron Eldor51d3ab52019-05-12 14:54:30 +0300989#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1)
990 case MBEDTLS_SSL_TLS_PRF_TLS1:
991 tls_prf = tls1_prf;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100992 break;
Ron Eldord2f25f72019-05-15 14:54:22 +0300993#endif /* MBEDTLS_SSL_PROTO_TLS1 || MBEDTLS_SSL_PROTO_TLS1_1 */
994
995#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
Gilles Peskined2d59372021-05-12 22:43:27 +0200996#if defined(MBEDTLS_SHA512_C) && !defined(MBEDTLS_SHA512_NO_SHA384)
Ron Eldor51d3ab52019-05-12 14:54:30 +0300997 case MBEDTLS_SSL_TLS_PRF_SHA384:
998 tls_prf = tls_prf_sha384;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100999 break;
Gilles Peskined2d59372021-05-12 22:43:27 +02001000#endif /* MBEDTLS_SHA512_C && !MBEDTLS_SHA512_NO_SHA384 */
Ron Eldor51d3ab52019-05-12 14:54:30 +03001001#if defined(MBEDTLS_SHA256_C)
1002 case MBEDTLS_SSL_TLS_PRF_SHA256:
1003 tls_prf = tls_prf_sha256;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001004 break;
Ron Eldord2f25f72019-05-15 14:54:22 +03001005#endif /* MBEDTLS_SHA256_C */
1006#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001007 default:
1008 return MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
Ron Eldor51d3ab52019-05-12 14:54:30 +03001009 }
1010
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001011 return tls_prf(secret, slen, label, random, rlen, dstbuf, dlen);
Ron Eldor51d3ab52019-05-12 14:54:30 +03001012}
1013
Manuel Pégourié-Gonnard9b108c22019-05-06 13:32:17 +02001014/* Type for the TLS PRF */
1015typedef int ssl_tls_prf_t(const unsigned char *, size_t, const char *,
1016 const unsigned char *, size_t,
1017 unsigned char *, size_t);
1018
Manuel Pégourié-Gonnarde59ae232019-04-30 11:41:40 +02001019/*
Manuel Pégourié-Gonnardcba40d92019-05-06 12:55:40 +02001020 * Populate a transform structure with session keys and all the other
1021 * necessary information.
Manuel Pégourié-Gonnarde59ae232019-04-30 11:41:40 +02001022 *
Manuel Pégourié-Gonnardcba40d92019-05-06 12:55:40 +02001023 * Parameters:
1024 * - [in/out]: transform: structure to populate
1025 * [in] must be just initialised with mbedtls_ssl_transform_init()
Manuel Pégourié-Gonnardc864f6a2019-05-06 13:48:22 +02001026 * [out] fully populated, ready for use by mbedtls_ssl_{en,de}crypt_buf()
Manuel Pégourié-Gonnard6fa57bf2019-05-10 10:50:04 +02001027 * - [in] ciphersuite
1028 * - [in] master
1029 * - [in] encrypt_then_mac
1030 * - [in] trunc_hmac
1031 * - [in] compression
Manuel Pégourié-Gonnard9b108c22019-05-06 13:32:17 +02001032 * - [in] tls_prf: pointer to PRF to use for key derivation
1033 * - [in] randbytes: buffer holding ServerHello.random + ClientHello.random
Manuel Pégourié-Gonnardc864f6a2019-05-06 13:48:22 +02001034 * - [in] minor_ver: SSL/TLS minor version
1035 * - [in] endpoint: client or server
1036 * - [in] ssl: optionally used for:
Manuel Pégourié-Gonnarde07bc202020-02-26 09:53:42 +01001037 * - MBEDTLS_SSL_HW_RECORD_ACCEL: whole context (non-const)
Manuel Pégourié-Gonnardc864f6a2019-05-06 13:48:22 +02001038 * - MBEDTLS_SSL_EXPORT_KEYS: ssl->conf->{f,p}_export_keys
1039 * - MBEDTLS_DEBUG_C: ssl->conf->{f,p}_dbg
Manuel Pégourié-Gonnarde59ae232019-04-30 11:41:40 +02001040 */
Manuel Pégourié-Gonnardd904d662022-06-17 10:24:00 +02001041MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001042static int ssl_populate_transform(mbedtls_ssl_transform *transform,
1043 int ciphersuite,
1044 const unsigned char master[48],
Jarno Lamsac84bd242019-08-16 12:06:56 +03001045#if defined(MBEDTLS_SSL_SOME_MODES_USE_MAC)
Manuel Pégourié-Gonnard6fa57bf2019-05-10 10:50:04 +02001046#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001047 int encrypt_then_mac,
Jarno Lamsac84bd242019-08-16 12:06:56 +03001048#endif /* MBEDTLS_SSL_ENCRYPT_THEN_MAC */
Manuel Pégourié-Gonnard6fa57bf2019-05-10 10:50:04 +02001049#if defined(MBEDTLS_SSL_TRUNCATED_HMAC)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001050 int trunc_hmac,
Jarno Lamsac84bd242019-08-16 12:06:56 +03001051#endif /* MBEDTLS_SSL_TRUNCATED_HMAC */
1052#endif /* MBEDTLS_SSL_SOME_MODES_USE_MAC */
Manuel Pégourié-Gonnard6fa57bf2019-05-10 10:50:04 +02001053#if defined(MBEDTLS_ZLIB_SUPPORT)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001054 int compression,
Manuel Pégourié-Gonnard6fa57bf2019-05-10 10:50:04 +02001055#endif
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001056 ssl_tls_prf_t tls_prf,
1057 const unsigned char randbytes[64],
1058 int minor_ver,
1059 unsigned endpoint,
Manuel Pégourié-Gonnard7ae6ed42020-03-13 11:28:19 +01001060#if !defined(MBEDTLS_SSL_HW_RECORD_ACCEL)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001061 const
Manuel Pégourié-Gonnard7ae6ed42020-03-13 11:28:19 +01001062#endif
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001063 mbedtls_ssl_context *ssl)
Paul Bakker5121ce52009-01-03 21:22:43 +00001064{
Paul Bakkerda02a7f2013-08-31 17:25:14 +02001065 int ret = 0;
Hanno Beckercb1cc802018-11-17 22:27:38 +00001066#if defined(MBEDTLS_USE_PSA_CRYPTO)
1067 int psa_fallthrough;
1068#endif /* MBEDTLS_USE_PSA_CRYPTO */
David Horstmannd4f22082022-10-26 18:25:14 +01001069 int do_mbedtls_cipher_setup;
Paul Bakker5121ce52009-01-03 21:22:43 +00001070 unsigned char keyblk[256];
1071 unsigned char *key1;
1072 unsigned char *key2;
Paul Bakker68884e32013-01-07 18:20:04 +01001073 unsigned char *mac_enc;
1074 unsigned char *mac_dec;
sander-visser3888b032020-05-06 21:49:46 +02001075 size_t mac_key_len = 0;
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02001076 size_t iv_copy_len;
Hanno Becker88aaf652017-12-27 08:17:40 +00001077 unsigned keylen;
Hanno Beckere694c3e2017-12-27 21:34:08 +00001078 const mbedtls_ssl_ciphersuite_t *ciphersuite_info;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001079 const mbedtls_cipher_info_t *cipher_info;
1080 const mbedtls_md_info_t *md_info;
Paul Bakker68884e32013-01-07 18:20:04 +01001081
Manuel Pégourié-Gonnardc864f6a2019-05-06 13:48:22 +02001082#if !defined(MBEDTLS_SSL_HW_RECORD_ACCEL) && \
1083 !defined(MBEDTLS_SSL_EXPORT_KEYS) && \
Gilles Peskinea6f99a12022-04-13 13:24:56 +02001084 !defined(MBEDTLS_SSL_DTLS_CONNECTION_ID) && \
Manuel Pégourié-Gonnardc864f6a2019-05-06 13:48:22 +02001085 !defined(MBEDTLS_DEBUG_C)
Paul Elliott9c2faca2023-10-18 14:34:46 +01001086 (void) ssl; /* ssl is unused except for those cases */
Manuel Pégourié-Gonnardc864f6a2019-05-06 13:48:22 +02001087#endif
1088
Manuel Pégourié-Gonnard96fb0ee2019-07-09 12:54:17 +02001089 /*
1090 * Some data just needs copying into the structure
1091 */
Jaeden Amero2de07f12019-06-05 13:32:08 +01001092#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC) && \
1093 defined(MBEDTLS_SSL_SOME_MODES_USE_MAC)
Manuel Pégourié-Gonnard6fa57bf2019-05-10 10:50:04 +02001094 transform->encrypt_then_mac = encrypt_then_mac;
Hanno Becker9eddaeb2017-12-27 21:37:21 +00001095#endif
Manuel Pégourié-Gonnardc864f6a2019-05-06 13:48:22 +02001096 transform->minor_ver = minor_ver;
Hanno Beckere694c3e2017-12-27 21:34:08 +00001097
Manuel Pégourié-Gonnard96fb0ee2019-07-09 12:54:17 +02001098#if defined(MBEDTLS_SSL_CONTEXT_SERIALIZATION)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001099 memcpy(transform->randbytes, randbytes, sizeof(transform->randbytes));
Manuel Pégourié-Gonnard96fb0ee2019-07-09 12:54:17 +02001100#endif
1101
Manuel Pégourié-Gonnard9b108c22019-05-06 13:32:17 +02001102 /*
1103 * Get various info structures
1104 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001105 ciphersuite_info = mbedtls_ssl_ciphersuite_from_id(ciphersuite);
1106 if (ciphersuite_info == NULL) {
1107 MBEDTLS_SSL_DEBUG_MSG(1, ("ciphersuite info for %d not found",
1108 ciphersuite));
1109 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
Manuel Pégourié-Gonnard9b108c22019-05-06 13:32:17 +02001110 }
1111
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001112 cipher_info = mbedtls_cipher_info_from_type(ciphersuite_info->cipher);
1113 if (cipher_info == NULL) {
1114 MBEDTLS_SSL_DEBUG_MSG(1, ("cipher info for %u not found",
1115 ciphersuite_info->cipher));
1116 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
Paul Bakker68884e32013-01-07 18:20:04 +01001117 }
1118
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001119 md_info = mbedtls_md_info_from_type(ciphersuite_info->mac);
1120 if (md_info == NULL) {
1121 MBEDTLS_SSL_DEBUG_MSG(1, ("mbedtls_md info for %u not found",
1122 (unsigned) ciphersuite_info->mac));
1123 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
Paul Bakker68884e32013-01-07 18:20:04 +01001124 }
1125
Hanno Beckera0e20d02019-05-15 14:03:01 +01001126#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Becker4bf74652019-04-26 16:22:27 +01001127 /* Copy own and peer's CID if the use of the CID
1128 * extension has been negotiated. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001129 if (ssl->handshake->cid_in_use == MBEDTLS_SSL_CID_ENABLED) {
1130 MBEDTLS_SSL_DEBUG_MSG(3, ("Copy CIDs into SSL transform"));
Hanno Becker8a7f9722019-04-30 13:52:29 +01001131
Hanno Becker05154c32019-05-03 15:23:51 +01001132 transform->in_cid_len = ssl->own_cid_len;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001133 memcpy(transform->in_cid, ssl->own_cid, ssl->own_cid_len);
1134 MBEDTLS_SSL_DEBUG_BUF(3, "Incoming CID", transform->in_cid,
1135 transform->in_cid_len);
Hanno Beckerd1f20352019-05-15 10:21:55 +01001136
1137 transform->out_cid_len = ssl->handshake->peer_cid_len;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001138 memcpy(transform->out_cid, ssl->handshake->peer_cid,
1139 ssl->handshake->peer_cid_len);
1140 MBEDTLS_SSL_DEBUG_BUF(3, "Outgoing CID", transform->out_cid,
1141 transform->out_cid_len);
Hanno Becker4bf74652019-04-26 16:22:27 +01001142 }
Hanno Beckera0e20d02019-05-15 14:03:01 +01001143#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Becker4bf74652019-04-26 16:22:27 +01001144
Paul Bakker5121ce52009-01-03 21:22:43 +00001145 /*
Manuel Pégourié-Gonnard9b108c22019-05-06 13:32:17 +02001146 * Compute key block using the PRF
Paul Bakker5121ce52009-01-03 21:22:43 +00001147 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001148 ret = tls_prf(master, 48, "key expansion", randbytes, 64, keyblk, 256);
1149 if (ret != 0) {
1150 MBEDTLS_SSL_DEBUG_RET(1, "prf", ret);
1151 return ret;
Manuel Pégourié-Gonnarde9608182015-03-26 11:47:47 +01001152 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001153
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001154 MBEDTLS_SSL_DEBUG_MSG(3, ("ciphersuite = %s",
1155 mbedtls_ssl_get_ciphersuite_name(ciphersuite)));
1156 MBEDTLS_SSL_DEBUG_BUF(3, "master secret", master, 48);
1157 MBEDTLS_SSL_DEBUG_BUF(4, "random bytes", randbytes, 64);
1158 MBEDTLS_SSL_DEBUG_BUF(4, "key block", keyblk, 256);
Paul Bakker5121ce52009-01-03 21:22:43 +00001159
Paul Bakker5121ce52009-01-03 21:22:43 +00001160 /*
1161 * Determine the appropriate key, IV and MAC length.
1162 */
Paul Bakker68884e32013-01-07 18:20:04 +01001163
Hanno Becker88aaf652017-12-27 08:17:40 +00001164 keylen = cipher_info->key_bitlen / 8;
Manuel Pégourié-Gonnarde800cd82014-06-18 15:34:40 +02001165
Hanno Becker8031d062018-01-03 15:32:31 +00001166#if defined(MBEDTLS_GCM_C) || \
1167 defined(MBEDTLS_CCM_C) || \
1168 defined(MBEDTLS_CHACHAPOLY_C)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001169 if (cipher_info->mode == MBEDTLS_MODE_GCM ||
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02001170 cipher_info->mode == MBEDTLS_MODE_CCM ||
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001171 cipher_info->mode == MBEDTLS_MODE_CHACHAPOLY) {
Hanno Beckerf704bef2018-11-16 15:21:18 +00001172 size_t explicit_ivlen;
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02001173
Manuel Pégourié-Gonnarde800cd82014-06-18 15:34:40 +02001174 transform->maclen = 0;
Hanno Becker81c7b182017-11-09 18:39:33 +00001175 mac_key_len = 0;
Hanno Beckere694c3e2017-12-27 21:34:08 +00001176 transform->taglen =
1177 ciphersuite_info->flags & MBEDTLS_CIPHERSUITE_SHORT_TAG ? 8 : 16;
Manuel Pégourié-Gonnarde800cd82014-06-18 15:34:40 +02001178
Hanno Becker447558d2020-05-28 07:36:33 +01001179 /* All modes haves 96-bit IVs, but the length of the static parts vary
1180 * with mode and version:
1181 * - For GCM and CCM in TLS 1.2, there's a static IV of 4 Bytes
1182 * (to be concatenated with a dynamically chosen IV of 8 Bytes)
Hanno Beckerf93c2d72020-05-28 07:39:43 +01001183 * - For ChaChaPoly in TLS 1.2, and all modes in TLS 1.3, there's
1184 * a static IV of 12 Bytes (to be XOR'ed with the 8 Byte record
1185 * sequence number).
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02001186 */
Paul Bakker68884e32013-01-07 18:20:04 +01001187 transform->ivlen = 12;
Hanno Beckerf93c2d72020-05-28 07:39:43 +01001188#if defined(MBEDTLS_SSL_PROTO_TLS1_3_EXPERIMENTAL)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001189 if (minor_ver == MBEDTLS_SSL_MINOR_VERSION_4) {
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02001190 transform->fixed_ivlen = 12;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001191 } else
Hanno Beckerf93c2d72020-05-28 07:39:43 +01001192#endif /* MBEDTLS_SSL_PROTO_TLS1_3_EXPERIMENTAL */
1193 {
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001194 if (cipher_info->mode == MBEDTLS_MODE_CHACHAPOLY) {
Hanno Beckerf93c2d72020-05-28 07:39:43 +01001195 transform->fixed_ivlen = 12;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001196 } else {
Hanno Beckerf93c2d72020-05-28 07:39:43 +01001197 transform->fixed_ivlen = 4;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001198 }
Hanno Beckerf93c2d72020-05-28 07:39:43 +01001199 }
Manuel Pégourié-Gonnarde800cd82014-06-18 15:34:40 +02001200
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02001201 /* Minimum length of encrypted record */
1202 explicit_ivlen = transform->ivlen - transform->fixed_ivlen;
Hanno Beckere694c3e2017-12-27 21:34:08 +00001203 transform->minlen = explicit_ivlen + transform->taglen;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001204 } else
Hanno Becker8031d062018-01-03 15:32:31 +00001205#endif /* MBEDTLS_GCM_C || MBEDTLS_CCM_C || MBEDTLS_CHACHAPOLY_C */
1206#if defined(MBEDTLS_SSL_SOME_MODES_USE_MAC)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001207 if (cipher_info->mode == MBEDTLS_MODE_STREAM ||
1208 cipher_info->mode == MBEDTLS_MODE_CBC) {
Manuel Pégourié-Gonnarde800cd82014-06-18 15:34:40 +02001209 /* Initialize HMAC contexts */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001210 if ((ret = mbedtls_md_setup(&transform->md_ctx_enc, md_info, 1)) != 0 ||
1211 (ret = mbedtls_md_setup(&transform->md_ctx_dec, md_info, 1)) != 0) {
1212 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_md_setup", ret);
Ron Eldore6992702019-05-07 18:27:13 +03001213 goto end;
Paul Bakker68884e32013-01-07 18:20:04 +01001214 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001215
Manuel Pégourié-Gonnarde800cd82014-06-18 15:34:40 +02001216 /* Get MAC length */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001217 mac_key_len = mbedtls_md_get_size(md_info);
Hanno Becker81c7b182017-11-09 18:39:33 +00001218 transform->maclen = mac_key_len;
Manuel Pégourié-Gonnarde800cd82014-06-18 15:34:40 +02001219
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001220#if defined(MBEDTLS_SSL_TRUNCATED_HMAC)
Manuel Pégourié-Gonnarde800cd82014-06-18 15:34:40 +02001221 /*
1222 * If HMAC is to be truncated, we shall keep the leftmost bytes,
1223 * (rfc 6066 page 13 or rfc 2104 section 4),
1224 * so we only need to adjust the length here.
1225 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001226 if (trunc_hmac == MBEDTLS_SSL_TRUNC_HMAC_ENABLED) {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001227 transform->maclen = MBEDTLS_SSL_TRUNCATED_HMAC_LEN;
Hanno Beckere89353a2017-11-20 16:36:41 +00001228
1229#if defined(MBEDTLS_SSL_TRUNCATED_HMAC_COMPAT)
1230 /* Fall back to old, non-compliant version of the truncated
Hanno Becker563423f2017-11-21 17:20:17 +00001231 * HMAC implementation which also truncates the key
1232 * (Mbed TLS versions from 1.3 to 2.6.0) */
Hanno Beckere89353a2017-11-20 16:36:41 +00001233 mac_key_len = transform->maclen;
1234#endif
1235 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001236#endif /* MBEDTLS_SSL_TRUNCATED_HMAC */
Manuel Pégourié-Gonnarde800cd82014-06-18 15:34:40 +02001237
1238 /* IV length */
Paul Bakker68884e32013-01-07 18:20:04 +01001239 transform->ivlen = cipher_info->iv_size;
Paul Bakker5121ce52009-01-03 21:22:43 +00001240
Manuel Pégourié-Gonnardeaa76f72014-06-18 16:06:02 +02001241 /* Minimum length */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001242 if (cipher_info->mode == MBEDTLS_MODE_STREAM) {
Manuel Pégourié-Gonnardeaa76f72014-06-18 16:06:02 +02001243 transform->minlen = transform->maclen;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001244 } else {
Manuel Pégourié-Gonnardeaa76f72014-06-18 16:06:02 +02001245 /*
1246 * GenericBlockCipher:
Manuel Pégourié-Gonnard169dd6a2014-11-04 16:15:39 +01001247 * 1. if EtM is in use: one block plus MAC
1248 * otherwise: * first multiple of blocklen greater than maclen
1249 * 2. IV except for SSL3 and TLS 1.0
Manuel Pégourié-Gonnardeaa76f72014-06-18 16:06:02 +02001250 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001251#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001252 if (encrypt_then_mac == MBEDTLS_SSL_ETM_ENABLED) {
Manuel Pégourié-Gonnard169dd6a2014-11-04 16:15:39 +01001253 transform->minlen = transform->maclen
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001254 + cipher_info->block_size;
1255 } else
Manuel Pégourié-Gonnard169dd6a2014-11-04 16:15:39 +01001256#endif
1257 {
1258 transform->minlen = transform->maclen
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001259 + cipher_info->block_size
1260 - transform->maclen % cipher_info->block_size;
Manuel Pégourié-Gonnard169dd6a2014-11-04 16:15:39 +01001261 }
Manuel Pégourié-Gonnardeaa76f72014-06-18 16:06:02 +02001262
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001263#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001264 if (minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 ||
1265 minor_ver == MBEDTLS_SSL_MINOR_VERSION_1) {
Manuel Pégourié-Gonnardeaa76f72014-06-18 16:06:02 +02001266 ; /* No need to adjust minlen */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001267 } else
Manuel Pégourié-Gonnardeaa76f72014-06-18 16:06:02 +02001268#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001269#if defined(MBEDTLS_SSL_PROTO_TLS1_1) || defined(MBEDTLS_SSL_PROTO_TLS1_2)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001270 if (minor_ver == MBEDTLS_SSL_MINOR_VERSION_2 ||
1271 minor_ver == MBEDTLS_SSL_MINOR_VERSION_3) {
Manuel Pégourié-Gonnardeaa76f72014-06-18 16:06:02 +02001272 transform->minlen += transform->ivlen;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001273 } else
Manuel Pégourié-Gonnardeaa76f72014-06-18 16:06:02 +02001274#endif
1275 {
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001276 MBEDTLS_SSL_DEBUG_MSG(1, ("should never happen"));
Ron Eldore6992702019-05-07 18:27:13 +03001277 ret = MBEDTLS_ERR_SSL_INTERNAL_ERROR;
1278 goto end;
Manuel Pégourié-Gonnardeaa76f72014-06-18 16:06:02 +02001279 }
Paul Bakker68884e32013-01-07 18:20:04 +01001280 }
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001281 } else
Hanno Becker8031d062018-01-03 15:32:31 +00001282#endif /* MBEDTLS_SSL_SOME_MODES_USE_MAC */
1283 {
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001284 MBEDTLS_SSL_DEBUG_MSG(1, ("should never happen"));
1285 return MBEDTLS_ERR_SSL_INTERNAL_ERROR;
Hanno Becker8031d062018-01-03 15:32:31 +00001286 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001287
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001288 MBEDTLS_SSL_DEBUG_MSG(3, ("keylen: %u, minlen: %u, ivlen: %u, maclen: %u",
1289 (unsigned) keylen,
1290 (unsigned) transform->minlen,
1291 (unsigned) transform->ivlen,
1292 (unsigned) transform->maclen));
Paul Bakker5121ce52009-01-03 21:22:43 +00001293
1294 /*
1295 * Finally setup the cipher contexts, IVs and MAC secrets.
1296 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001297#if defined(MBEDTLS_SSL_CLI_C)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001298 if (endpoint == MBEDTLS_SSL_IS_CLIENT) {
Hanno Becker81c7b182017-11-09 18:39:33 +00001299 key1 = keyblk + mac_key_len * 2;
Hanno Becker88aaf652017-12-27 08:17:40 +00001300 key2 = keyblk + mac_key_len * 2 + keylen;
Paul Bakker5121ce52009-01-03 21:22:43 +00001301
Paul Bakker68884e32013-01-07 18:20:04 +01001302 mac_enc = keyblk;
Hanno Becker81c7b182017-11-09 18:39:33 +00001303 mac_dec = keyblk + mac_key_len;
Paul Bakker5121ce52009-01-03 21:22:43 +00001304
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001305 /*
1306 * This is not used in TLS v1.1.
1307 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001308 iv_copy_len = (transform->fixed_ivlen) ?
1309 transform->fixed_ivlen : transform->ivlen;
1310 memcpy(transform->iv_enc, key2 + keylen, iv_copy_len);
1311 memcpy(transform->iv_dec, key2 + keylen + iv_copy_len,
1312 iv_copy_len);
1313 } else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001314#endif /* MBEDTLS_SSL_CLI_C */
1315#if defined(MBEDTLS_SSL_SRV_C)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001316 if (endpoint == MBEDTLS_SSL_IS_SERVER) {
Hanno Becker88aaf652017-12-27 08:17:40 +00001317 key1 = keyblk + mac_key_len * 2 + keylen;
Hanno Becker81c7b182017-11-09 18:39:33 +00001318 key2 = keyblk + mac_key_len * 2;
Paul Bakker5121ce52009-01-03 21:22:43 +00001319
Hanno Becker81c7b182017-11-09 18:39:33 +00001320 mac_enc = keyblk + mac_key_len;
Paul Bakker68884e32013-01-07 18:20:04 +01001321 mac_dec = keyblk;
Paul Bakker5121ce52009-01-03 21:22:43 +00001322
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001323 /*
1324 * This is not used in TLS v1.1.
1325 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001326 iv_copy_len = (transform->fixed_ivlen) ?
1327 transform->fixed_ivlen : transform->ivlen;
1328 memcpy(transform->iv_dec, key1 + keylen, iv_copy_len);
1329 memcpy(transform->iv_enc, key1 + keylen + iv_copy_len,
1330 iv_copy_len);
1331 } else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001332#endif /* MBEDTLS_SSL_SRV_C */
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01001333 {
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001334 MBEDTLS_SSL_DEBUG_MSG(1, ("should never happen"));
Ron Eldore6992702019-05-07 18:27:13 +03001335 ret = MBEDTLS_ERR_SSL_INTERNAL_ERROR;
1336 goto end;
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01001337 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001338
Hanno Beckerd56ed242018-01-03 15:32:51 +00001339#if defined(MBEDTLS_SSL_SOME_MODES_USE_MAC)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001340#if defined(MBEDTLS_SSL_PROTO_SSL3)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001341 if (minor_ver == MBEDTLS_SSL_MINOR_VERSION_0) {
1342 if (mac_key_len > sizeof(transform->mac_enc)) {
1343 MBEDTLS_SSL_DEBUG_MSG(1, ("should never happen"));
Ron Eldore6992702019-05-07 18:27:13 +03001344 ret = MBEDTLS_ERR_SSL_INTERNAL_ERROR;
1345 goto end;
Manuel Pégourié-Gonnard7cfdcb82014-01-18 18:22:55 +01001346 }
1347
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001348 memcpy(transform->mac_enc, mac_enc, mac_key_len);
1349 memcpy(transform->mac_dec, mac_dec, mac_key_len);
1350 } else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001351#endif /* MBEDTLS_SSL_PROTO_SSL3 */
1352#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1) || \
1353 defined(MBEDTLS_SSL_PROTO_TLS1_2)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001354 if (minor_ver >= MBEDTLS_SSL_MINOR_VERSION_1) {
Gilles Peskine039fd122018-03-19 19:06:08 +01001355 /* For HMAC-based ciphersuites, initialize the HMAC transforms.
1356 For AEAD-based ciphersuites, there is nothing to do here. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001357 if (mac_key_len != 0) {
1358 ret = mbedtls_md_hmac_starts(&transform->md_ctx_enc,
1359 mac_enc, mac_key_len);
1360 if (ret != 0) {
Gilles Peskine2b3f21d2021-12-10 21:35:10 +01001361 goto end;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001362 }
1363 ret = mbedtls_md_hmac_starts(&transform->md_ctx_dec,
1364 mac_dec, mac_key_len);
1365 if (ret != 0) {
Gilles Peskine2b3f21d2021-12-10 21:35:10 +01001366 goto end;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001367 }
Gilles Peskine039fd122018-03-19 19:06:08 +01001368 }
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001369 } else
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001370#endif
Paul Bakker577e0062013-08-28 11:57:20 +02001371 {
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001372 MBEDTLS_SSL_DEBUG_MSG(1, ("should never happen"));
Ron Eldore6992702019-05-07 18:27:13 +03001373 ret = MBEDTLS_ERR_SSL_INTERNAL_ERROR;
1374 goto end;
Paul Bakker577e0062013-08-28 11:57:20 +02001375 }
Hanno Beckerd56ed242018-01-03 15:32:51 +00001376#endif /* MBEDTLS_SSL_SOME_MODES_USE_MAC */
Paul Bakker68884e32013-01-07 18:20:04 +01001377
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001378#if defined(MBEDTLS_SSL_HW_RECORD_ACCEL)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001379 if (mbedtls_ssl_hw_record_init != NULL) {
sander-visser1abe8ee2020-05-06 21:27:14 +02001380 ret = 0;
Paul Bakker05ef8352012-05-08 09:17:57 +00001381
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001382 MBEDTLS_SSL_DEBUG_MSG(2, ("going for mbedtls_ssl_hw_record_init()"));
Paul Bakker05ef8352012-05-08 09:17:57 +00001383
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001384 if ((ret = mbedtls_ssl_hw_record_init(ssl, key1, key2, keylen,
1385 transform->iv_enc, transform->iv_dec,
1386 iv_copy_len,
1387 mac_enc, mac_dec,
1388 mac_key_len)) != 0) {
1389 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ssl_hw_record_init", ret);
Ron Eldore6992702019-05-07 18:27:13 +03001390 ret = MBEDTLS_ERR_SSL_HW_ACCEL_FAILED;
1391 goto end;
Paul Bakker05ef8352012-05-08 09:17:57 +00001392 }
1393 }
Hanno Beckerd56ed242018-01-03 15:32:51 +00001394#else
1395 ((void) mac_dec);
1396 ((void) mac_enc);
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001397#endif /* MBEDTLS_SSL_HW_RECORD_ACCEL */
Paul Bakker05ef8352012-05-08 09:17:57 +00001398
Manuel Pégourié-Gonnard024b6df2015-10-19 13:52:53 +02001399#if defined(MBEDTLS_SSL_EXPORT_KEYS)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001400 if (ssl->conf->f_export_keys != NULL) {
1401 ssl->conf->f_export_keys(ssl->conf->p_export_keys,
1402 master, keyblk,
1403 mac_key_len, keylen,
1404 iv_copy_len);
Robert Cragie4feb7ae2015-10-02 13:33:37 +01001405 }
Ron Eldorf5cc10d2019-05-07 18:33:40 +03001406
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001407 if (ssl->conf->f_export_keys_ext != NULL) {
1408 ssl->conf->f_export_keys_ext(ssl->conf->p_export_keys,
1409 master, keyblk,
1410 mac_key_len, keylen,
1411 iv_copy_len,
1412 randbytes + 32,
1413 randbytes,
1414 tls_prf_get_type(tls_prf));
Ron Eldorf5cc10d2019-05-07 18:33:40 +03001415 }
Robert Cragie4feb7ae2015-10-02 13:33:37 +01001416#endif
1417
David Horstmannd4f22082022-10-26 18:25:14 +01001418 do_mbedtls_cipher_setup = 1;
Hanno Beckerf704bef2018-11-16 15:21:18 +00001419#if defined(MBEDTLS_USE_PSA_CRYPTO)
Hanno Beckercb1cc802018-11-17 22:27:38 +00001420
1421 /* Only use PSA-based ciphers for TLS-1.2.
1422 * That's relevant at least for TLS-1.0, where
1423 * we assume that mbedtls_cipher_crypt() updates
1424 * the structure field for the IV, which the PSA-based
1425 * implementation currently doesn't. */
1426#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001427 if (minor_ver == MBEDTLS_SSL_MINOR_VERSION_3) {
1428 ret = mbedtls_cipher_setup_psa(&transform->cipher_ctx_enc,
1429 cipher_info, transform->taglen);
1430 if (ret != 0 && ret != MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE) {
1431 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_cipher_setup_psa", ret);
Ron Eldore6992702019-05-07 18:27:13 +03001432 goto end;
Hanno Beckercb1cc802018-11-17 22:27:38 +00001433 }
1434
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001435 if (ret == 0) {
1436 MBEDTLS_SSL_DEBUG_MSG(3, ("Successfully setup PSA-based encryption cipher context"));
Hanno Beckercb1cc802018-11-17 22:27:38 +00001437 psa_fallthrough = 0;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001438 } else {
1439 MBEDTLS_SSL_DEBUG_MSG(1,
1440 (
1441 "Failed to setup PSA-based cipher context for record encryption - fall through to default setup."));
Hanno Beckercb1cc802018-11-17 22:27:38 +00001442 psa_fallthrough = 1;
1443 }
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001444 } else {
Hanno Beckercb1cc802018-11-17 22:27:38 +00001445 psa_fallthrough = 1;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001446 }
Hanno Beckercb1cc802018-11-17 22:27:38 +00001447#else
1448 psa_fallthrough = 1;
1449#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Hanno Beckerf704bef2018-11-16 15:21:18 +00001450
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001451 if (psa_fallthrough == 0) {
David Horstmannd4f22082022-10-26 18:25:14 +01001452 do_mbedtls_cipher_setup = 0;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001453 }
Hanno Beckerf704bef2018-11-16 15:21:18 +00001454#endif /* MBEDTLS_USE_PSA_CRYPTO */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001455 if (do_mbedtls_cipher_setup &&
1456 (ret = mbedtls_cipher_setup(&transform->cipher_ctx_enc,
1457 cipher_info)) != 0) {
1458 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_cipher_setup", ret);
Ron Eldore6992702019-05-07 18:27:13 +03001459 goto end;
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001460 }
Paul Bakkerda02a7f2013-08-31 17:25:14 +02001461
David Horstmannd4f22082022-10-26 18:25:14 +01001462 do_mbedtls_cipher_setup = 1;
Hanno Beckerf704bef2018-11-16 15:21:18 +00001463#if defined(MBEDTLS_USE_PSA_CRYPTO)
Hanno Beckercb1cc802018-11-17 22:27:38 +00001464 /* Only use PSA-based ciphers for TLS-1.2.
1465 * That's relevant at least for TLS-1.0, where
1466 * we assume that mbedtls_cipher_crypt() updates
1467 * the structure field for the IV, which the PSA-based
1468 * implementation currently doesn't. */
1469#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001470 if (minor_ver == MBEDTLS_SSL_MINOR_VERSION_3) {
1471 ret = mbedtls_cipher_setup_psa(&transform->cipher_ctx_dec,
1472 cipher_info, transform->taglen);
1473 if (ret != 0 && ret != MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE) {
1474 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_cipher_setup_psa", ret);
Ron Eldore6992702019-05-07 18:27:13 +03001475 goto end;
Hanno Beckercb1cc802018-11-17 22:27:38 +00001476 }
1477
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001478 if (ret == 0) {
1479 MBEDTLS_SSL_DEBUG_MSG(3, ("Successfully setup PSA-based decryption cipher context"));
Hanno Beckercb1cc802018-11-17 22:27:38 +00001480 psa_fallthrough = 0;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001481 } else {
1482 MBEDTLS_SSL_DEBUG_MSG(1,
1483 (
1484 "Failed to setup PSA-based cipher context for record decryption - fall through to default setup."));
Hanno Beckercb1cc802018-11-17 22:27:38 +00001485 psa_fallthrough = 1;
1486 }
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001487 } else {
Hanno Beckercb1cc802018-11-17 22:27:38 +00001488 psa_fallthrough = 1;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001489 }
Hanno Beckercb1cc802018-11-17 22:27:38 +00001490#else
1491 psa_fallthrough = 1;
1492#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Hanno Beckerf704bef2018-11-16 15:21:18 +00001493
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001494 if (psa_fallthrough == 0) {
David Horstmannd4f22082022-10-26 18:25:14 +01001495 do_mbedtls_cipher_setup = 0;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001496 }
Hanno Beckerf704bef2018-11-16 15:21:18 +00001497#endif /* MBEDTLS_USE_PSA_CRYPTO */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001498 if (do_mbedtls_cipher_setup &&
1499 (ret = mbedtls_cipher_setup(&transform->cipher_ctx_dec,
1500 cipher_info)) != 0) {
1501 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_cipher_setup", ret);
Ron Eldore6992702019-05-07 18:27:13 +03001502 goto end;
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001503 }
Paul Bakkerda02a7f2013-08-31 17:25:14 +02001504
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001505 if ((ret = mbedtls_cipher_setkey(&transform->cipher_ctx_enc, key1,
1506 cipher_info->key_bitlen,
1507 MBEDTLS_ENCRYPT)) != 0) {
1508 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_cipher_setkey", ret);
Ron Eldore6992702019-05-07 18:27:13 +03001509 goto end;
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001510 }
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02001511
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001512 if ((ret = mbedtls_cipher_setkey(&transform->cipher_ctx_dec, key2,
1513 cipher_info->key_bitlen,
1514 MBEDTLS_DECRYPT)) != 0) {
1515 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_cipher_setkey", ret);
Ron Eldore6992702019-05-07 18:27:13 +03001516 goto end;
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001517 }
Paul Bakkerda02a7f2013-08-31 17:25:14 +02001518
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001519#if defined(MBEDTLS_CIPHER_MODE_CBC)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001520 if (cipher_info->mode == MBEDTLS_MODE_CBC) {
1521 if ((ret = mbedtls_cipher_set_padding_mode(&transform->cipher_ctx_enc,
1522 MBEDTLS_PADDING_NONE)) != 0) {
1523 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_cipher_set_padding_mode", ret);
Ron Eldore6992702019-05-07 18:27:13 +03001524 goto end;
Manuel Pégourié-Gonnard126a66f2013-10-25 18:33:32 +02001525 }
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001526
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001527 if ((ret = mbedtls_cipher_set_padding_mode(&transform->cipher_ctx_dec,
1528 MBEDTLS_PADDING_NONE)) != 0) {
1529 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_cipher_set_padding_mode", ret);
Ron Eldore6992702019-05-07 18:27:13 +03001530 goto end;
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001531 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001532 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001533#endif /* MBEDTLS_CIPHER_MODE_CBC */
Paul Bakker5121ce52009-01-03 21:22:43 +00001534
Paul Bakker5121ce52009-01-03 21:22:43 +00001535
Manuel Pégourié-Gonnardd73b47f2019-05-06 12:44:24 +02001536 /* Initialize Zlib contexts */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001537#if defined(MBEDTLS_ZLIB_SUPPORT)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001538 if (compression == MBEDTLS_SSL_COMPRESS_DEFLATE) {
1539 MBEDTLS_SSL_DEBUG_MSG(3, ("Initializing zlib states"));
Paul Bakker2770fbd2012-07-03 13:30:23 +00001540
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001541 memset(&transform->ctx_deflate, 0, sizeof(transform->ctx_deflate));
1542 memset(&transform->ctx_inflate, 0, sizeof(transform->ctx_inflate));
Paul Bakker2770fbd2012-07-03 13:30:23 +00001543
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001544 if (deflateInit(&transform->ctx_deflate,
1545 Z_DEFAULT_COMPRESSION) != Z_OK ||
1546 inflateInit(&transform->ctx_inflate) != Z_OK) {
1547 MBEDTLS_SSL_DEBUG_MSG(1, ("Failed to initialize compression"));
Ron Eldore6992702019-05-07 18:27:13 +03001548 ret = MBEDTLS_ERR_SSL_COMPRESSION_FAILED;
1549 goto end;
Paul Bakker2770fbd2012-07-03 13:30:23 +00001550 }
1551 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001552#endif /* MBEDTLS_ZLIB_SUPPORT */
Paul Bakker2770fbd2012-07-03 13:30:23 +00001553
Ron Eldore6992702019-05-07 18:27:13 +03001554end:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001555 mbedtls_platform_zeroize(keyblk, sizeof(keyblk));
1556 return ret;
Paul Bakker5121ce52009-01-03 21:22:43 +00001557}
1558
Manuel Pégourié-Gonnard8d2805c2019-04-30 12:08:59 +02001559/*
Manuel Pégourié-Gonnard47e33e12019-05-20 10:10:17 +02001560 * Set appropriate PRF function and other SSL / TLS 1.0/1.1 / TLS1.2 functions
Manuel Pégourié-Gonnard8d2805c2019-04-30 12:08:59 +02001561 *
1562 * Inputs:
1563 * - SSL/TLS minor version
1564 * - hash associated with the ciphersuite (only used by TLS 1.2)
1565 *
Manuel Pégourié-Gonnard31d3ef12019-05-10 10:25:00 +02001566 * Outputs:
Manuel Pégourié-Gonnard8d2805c2019-04-30 12:08:59 +02001567 * - the tls_prf, calc_verify and calc_finished members of handshake structure
1568 */
Manuel Pégourié-Gonnardd904d662022-06-17 10:24:00 +02001569MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001570static int ssl_set_handshake_prfs(mbedtls_ssl_handshake_params *handshake,
1571 int minor_ver,
1572 mbedtls_md_type_t hash)
Manuel Pégourié-Gonnard1b00c4f2019-04-30 11:54:22 +02001573{
Gilles Peskinefc9c07f2021-05-12 22:51:53 +02001574#if !defined(MBEDTLS_SSL_PROTO_TLS1_2) || \
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001575 !(defined(MBEDTLS_SHA512_C) && !defined(MBEDTLS_SHA512_NO_SHA384))
Manuel Pégourié-Gonnard8d2805c2019-04-30 12:08:59 +02001576 (void) hash;
1577#endif
Manuel Pégourié-Gonnard1b00c4f2019-04-30 11:54:22 +02001578
Manuel Pégourié-Gonnard1b00c4f2019-04-30 11:54:22 +02001579#if defined(MBEDTLS_SSL_PROTO_SSL3)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001580 if (minor_ver == MBEDTLS_SSL_MINOR_VERSION_0) {
Manuel Pégourié-Gonnard1b00c4f2019-04-30 11:54:22 +02001581 handshake->tls_prf = ssl3_prf;
1582 handshake->calc_verify = ssl_calc_verify_ssl;
1583 handshake->calc_finished = ssl_calc_finished_ssl;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001584 } else
Manuel Pégourié-Gonnard1b00c4f2019-04-30 11:54:22 +02001585#endif
1586#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001587 if (minor_ver < MBEDTLS_SSL_MINOR_VERSION_3) {
Manuel Pégourié-Gonnard1b00c4f2019-04-30 11:54:22 +02001588 handshake->tls_prf = tls1_prf;
1589 handshake->calc_verify = ssl_calc_verify_tls;
1590 handshake->calc_finished = ssl_calc_finished_tls;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001591 } else
Manuel Pégourié-Gonnard1b00c4f2019-04-30 11:54:22 +02001592#endif
1593#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
Gilles Peskined2d59372021-05-12 22:43:27 +02001594#if defined(MBEDTLS_SHA512_C) && !defined(MBEDTLS_SHA512_NO_SHA384)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001595 if (minor_ver == MBEDTLS_SSL_MINOR_VERSION_3 &&
1596 hash == MBEDTLS_MD_SHA384) {
Manuel Pégourié-Gonnard1b00c4f2019-04-30 11:54:22 +02001597 handshake->tls_prf = tls_prf_sha384;
1598 handshake->calc_verify = ssl_calc_verify_tls_sha384;
1599 handshake->calc_finished = ssl_calc_finished_tls_sha384;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001600 } else
Manuel Pégourié-Gonnard1b00c4f2019-04-30 11:54:22 +02001601#endif
1602#if defined(MBEDTLS_SHA256_C)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001603 if (minor_ver == MBEDTLS_SSL_MINOR_VERSION_3) {
Manuel Pégourié-Gonnard1b00c4f2019-04-30 11:54:22 +02001604 handshake->tls_prf = tls_prf_sha256;
1605 handshake->calc_verify = ssl_calc_verify_tls_sha256;
1606 handshake->calc_finished = ssl_calc_finished_tls_sha256;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001607 } else
Manuel Pégourié-Gonnard1b00c4f2019-04-30 11:54:22 +02001608#endif
1609#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
1610 {
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001611 return MBEDTLS_ERR_SSL_INTERNAL_ERROR;
Manuel Pégourié-Gonnard1b00c4f2019-04-30 11:54:22 +02001612 }
1613
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001614 return 0;
Manuel Pégourié-Gonnard1b00c4f2019-04-30 11:54:22 +02001615}
1616
Manuel Pégourié-Gonnard9951b712019-04-30 12:08:59 +02001617/*
Manuel Pégourié-Gonnard85680c42019-05-03 09:16:16 +02001618 * Compute master secret if needed
Manuel Pégourié-Gonnardde047ad2019-05-03 09:46:14 +02001619 *
1620 * Parameters:
1621 * [in/out] handshake
1622 * [in] resume, premaster, extended_ms, calc_verify, tls_prf
Manuel Pégourié-Gonnardde718b92019-05-03 11:43:28 +02001623 * (PSA-PSK) ciphersuite_info, psk_opaque
Manuel Pégourié-Gonnardde047ad2019-05-03 09:46:14 +02001624 * [out] premaster (cleared)
Manuel Pégourié-Gonnardde047ad2019-05-03 09:46:14 +02001625 * [out] master
1626 * [in] ssl: optionally used for debugging, EMS and PSA-PSK
1627 * debug: conf->f_dbg, conf->p_dbg
1628 * EMS: passed to calc_verify (debug + (SSL3) session_negotiate)
Manuel Pégourié-Gonnardde718b92019-05-03 11:43:28 +02001629 * PSA-PSA: minor_ver, conf
Manuel Pégourié-Gonnard9951b712019-04-30 12:08:59 +02001630 */
Manuel Pégourié-Gonnardd904d662022-06-17 10:24:00 +02001631MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001632static int ssl_compute_master(mbedtls_ssl_handshake_params *handshake,
1633 unsigned char *master,
1634 const mbedtls_ssl_context *ssl)
Manuel Pégourié-Gonnard9951b712019-04-30 12:08:59 +02001635{
Janos Follath865b3eb2019-12-16 11:46:15 +00001636 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnard9951b712019-04-30 12:08:59 +02001637
1638 /* cf. RFC 5246, Section 8.1:
1639 * "The master secret is always exactly 48 bytes in length." */
1640 size_t const master_secret_len = 48;
1641
1642#if defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET)
1643 unsigned char session_hash[48];
1644#endif /* MBEDTLS_SSL_EXTENDED_MASTER_SECRET */
1645
Manuel Pégourié-Gonnard85680c42019-05-03 09:16:16 +02001646 /* The label for the KDF used for key expansion.
1647 * This is either "master secret" or "extended master secret"
1648 * depending on whether the Extended Master Secret extension
1649 * is used. */
1650 char const *lbl = "master secret";
1651
1652 /* The salt for the KDF used for key expansion.
1653 * - If the Extended Master Secret extension is not used,
1654 * this is ClientHello.Random + ServerHello.Random
1655 * (see Sect. 8.1 in RFC 5246).
1656 * - If the Extended Master Secret extension is used,
1657 * this is the transcript of the handshake so far.
1658 * (see Sect. 4 in RFC 7627). */
1659 unsigned char const *salt = handshake->randbytes;
1660 size_t salt_len = 64;
1661
Manuel Pégourié-Gonnardde047ad2019-05-03 09:46:14 +02001662#if !defined(MBEDTLS_DEBUG_C) && \
1663 !defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET) && \
1664 !(defined(MBEDTLS_USE_PSA_CRYPTO) && \
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001665 defined(MBEDTLS_KEY_EXCHANGE_PSK_ENABLED))
Manuel Pégourié-Gonnarda7505d12019-05-07 10:17:56 +02001666 ssl = NULL; /* make sure we don't use it except for those cases */
Manuel Pégourié-Gonnardde047ad2019-05-03 09:46:14 +02001667 (void) ssl;
1668#endif
Manuel Pégourié-Gonnardde047ad2019-05-03 09:46:14 +02001669
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001670 if (handshake->resume != 0) {
1671 MBEDTLS_SSL_DEBUG_MSG(3, ("no premaster (session resumed)"));
1672 return 0;
Manuel Pégourié-Gonnard9951b712019-04-30 12:08:59 +02001673 }
Manuel Pégourié-Gonnard9951b712019-04-30 12:08:59 +02001674
1675#if defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001676 if (handshake->extended_ms == MBEDTLS_SSL_EXTENDED_MS_ENABLED) {
Manuel Pégourié-Gonnard85680c42019-05-03 09:16:16 +02001677 lbl = "extended master secret";
1678 salt = session_hash;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001679 handshake->calc_verify(ssl, session_hash, &salt_len);
Manuel Pégourié-Gonnard85680c42019-05-03 09:16:16 +02001680
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001681 MBEDTLS_SSL_DEBUG_BUF(3, "session hash for extended master secret",
1682 session_hash, salt_len);
Manuel Pégourié-Gonnard85680c42019-05-03 09:16:16 +02001683 }
Manuel Pégourié-Gonnard9951b712019-04-30 12:08:59 +02001684#endif /* MBEDTLS_SSL_EXTENDED_MS_ENABLED */
1685
1686#if defined(MBEDTLS_USE_PSA_CRYPTO) && \
Manuel Pégourié-Gonnardde047ad2019-05-03 09:46:14 +02001687 defined(MBEDTLS_KEY_EXCHANGE_PSK_ENABLED)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001688 if (handshake->ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_PSK &&
Manuel Pégourié-Gonnardde718b92019-05-03 11:43:28 +02001689 ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_3 &&
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001690 ssl_use_opaque_psk(ssl) == 1) {
Manuel Pégourié-Gonnard85680c42019-05-03 09:16:16 +02001691 /* Perform PSK-to-MS expansion in a single step. */
1692 psa_status_t status;
1693 psa_algorithm_t alg;
Ronald Croncf56a0a2020-08-04 09:51:30 +02001694 psa_key_id_t psk;
Manuel Pégourié-Gonnard85680c42019-05-03 09:16:16 +02001695 psa_key_derivation_operation_t derivation =
1696 PSA_KEY_DERIVATION_OPERATION_INIT;
Manuel Pégourié-Gonnardde718b92019-05-03 11:43:28 +02001697 mbedtls_md_type_t hash_alg = handshake->ciphersuite_info->mac;
Manuel Pégourié-Gonnard9951b712019-04-30 12:08:59 +02001698
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001699 MBEDTLS_SSL_DEBUG_MSG(2, ("perform PSA-based PSK-to-MS expansion"));
Manuel Pégourié-Gonnard9951b712019-04-30 12:08:59 +02001700
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001701 psk = mbedtls_ssl_get_opaque_psk(ssl);
Manuel Pégourié-Gonnard9951b712019-04-30 12:08:59 +02001702
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001703 if (hash_alg == MBEDTLS_MD_SHA384) {
Manuel Pégourié-Gonnard85680c42019-05-03 09:16:16 +02001704 alg = PSA_ALG_TLS12_PSK_TO_MS(PSA_ALG_SHA_384);
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001705 } else {
Manuel Pégourié-Gonnard85680c42019-05-03 09:16:16 +02001706 alg = PSA_ALG_TLS12_PSK_TO_MS(PSA_ALG_SHA_256);
Manuel Pégourié-Gonnard9951b712019-04-30 12:08:59 +02001707 }
Manuel Pégourié-Gonnard85680c42019-05-03 09:16:16 +02001708
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001709 status = setup_psa_key_derivation(&derivation, psk, alg,
1710 salt, salt_len,
1711 (unsigned char const *) lbl,
1712 (size_t) strlen(lbl),
1713 master_secret_len);
1714 if (status != PSA_SUCCESS) {
1715 psa_key_derivation_abort(&derivation);
1716 return MBEDTLS_ERR_SSL_HW_ACCEL_FAILED;
Manuel Pégourié-Gonnard85680c42019-05-03 09:16:16 +02001717 }
1718
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001719 status = psa_key_derivation_output_bytes(&derivation,
1720 master,
1721 master_secret_len);
1722 if (status != PSA_SUCCESS) {
1723 psa_key_derivation_abort(&derivation);
1724 return MBEDTLS_ERR_SSL_HW_ACCEL_FAILED;
1725 }
1726
1727 status = psa_key_derivation_abort(&derivation);
1728 if (status != PSA_SUCCESS) {
1729 return MBEDTLS_ERR_SSL_HW_ACCEL_FAILED;
1730 }
1731 } else
Manuel Pégourié-Gonnard85680c42019-05-03 09:16:16 +02001732#endif
1733 {
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001734 ret = handshake->tls_prf(handshake->premaster, handshake->pmslen,
1735 lbl, salt, salt_len,
1736 master,
1737 master_secret_len);
1738 if (ret != 0) {
1739 MBEDTLS_SSL_DEBUG_RET(1, "prf", ret);
1740 return ret;
Manuel Pégourié-Gonnard85680c42019-05-03 09:16:16 +02001741 }
1742
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001743 MBEDTLS_SSL_DEBUG_BUF(3, "premaster secret",
1744 handshake->premaster,
1745 handshake->pmslen);
Manuel Pégourié-Gonnard85680c42019-05-03 09:16:16 +02001746
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001747 mbedtls_platform_zeroize(handshake->premaster,
1748 sizeof(handshake->premaster));
Manuel Pégourié-Gonnard9951b712019-04-30 12:08:59 +02001749 }
1750
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001751 return 0;
Manuel Pégourié-Gonnard9951b712019-04-30 12:08:59 +02001752}
Manuel Pégourié-Gonnard1b00c4f2019-04-30 11:54:22 +02001753
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001754int mbedtls_ssl_derive_keys(mbedtls_ssl_context *ssl)
Manuel Pégourié-Gonnarde59ae232019-04-30 11:41:40 +02001755{
Janos Follath865b3eb2019-12-16 11:46:15 +00001756 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnard8d2805c2019-04-30 12:08:59 +02001757 const mbedtls_ssl_ciphersuite_t * const ciphersuite_info =
1758 ssl->handshake->ciphersuite_info;
Manuel Pégourié-Gonnard1b00c4f2019-04-30 11:54:22 +02001759
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001760 MBEDTLS_SSL_DEBUG_MSG(2, ("=> derive keys"));
Manuel Pégourié-Gonnard040a9512019-05-06 12:05:58 +02001761
1762 /* Set PRF, calc_verify and calc_finished function pointers */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001763 ret = ssl_set_handshake_prfs(ssl->handshake,
1764 ssl->minor_ver,
1765 ciphersuite_info->mac);
1766 if (ret != 0) {
1767 MBEDTLS_SSL_DEBUG_RET(1, "ssl_set_handshake_prfs", ret);
1768 return ret;
Manuel Pégourié-Gonnard8d2805c2019-04-30 12:08:59 +02001769 }
Manuel Pégourié-Gonnard1b00c4f2019-04-30 11:54:22 +02001770
Manuel Pégourié-Gonnard040a9512019-05-06 12:05:58 +02001771 /* Compute master secret if needed */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001772 ret = ssl_compute_master(ssl->handshake,
1773 ssl->session_negotiate->master,
1774 ssl);
1775 if (ret != 0) {
1776 MBEDTLS_SSL_DEBUG_RET(1, "ssl_compute_master", ret);
1777 return ret;
Manuel Pégourié-Gonnard9951b712019-04-30 12:08:59 +02001778 }
1779
Manuel Pégourié-Gonnard040a9512019-05-06 12:05:58 +02001780 /* Swap the client and server random values:
1781 * - MS derivation wanted client+server (RFC 5246 8.1)
1782 * - key derivation wants server+client (RFC 5246 6.3) */
1783 {
1784 unsigned char tmp[64];
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001785 memcpy(tmp, ssl->handshake->randbytes, 64);
1786 memcpy(ssl->handshake->randbytes, tmp + 32, 32);
1787 memcpy(ssl->handshake->randbytes + 32, tmp, 32);
1788 mbedtls_platform_zeroize(tmp, sizeof(tmp));
Manuel Pégourié-Gonnard040a9512019-05-06 12:05:58 +02001789 }
1790
1791 /* Populate transform structure */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001792 ret = ssl_populate_transform(ssl->transform_negotiate,
1793 ssl->session_negotiate->ciphersuite,
1794 ssl->session_negotiate->master,
Jarno Lamsac84bd242019-08-16 12:06:56 +03001795#if defined(MBEDTLS_SSL_SOME_MODES_USE_MAC)
Manuel Pégourié-Gonnard6fa57bf2019-05-10 10:50:04 +02001796#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001797 ssl->session_negotiate->encrypt_then_mac,
Jarno Lamsac84bd242019-08-16 12:06:56 +03001798#endif /* MBEDTLS_SSL_ENCRYPT_THEN_MAC */
Manuel Pégourié-Gonnard6fa57bf2019-05-10 10:50:04 +02001799#if defined(MBEDTLS_SSL_TRUNCATED_HMAC)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001800 ssl->session_negotiate->trunc_hmac,
Jarno Lamsac84bd242019-08-16 12:06:56 +03001801#endif /* MBEDTLS_SSL_TRUNCATED_HMAC */
1802#endif /* MBEDTLS_SSL_SOME_MODES_USE_MAC */
Manuel Pégourié-Gonnard6fa57bf2019-05-10 10:50:04 +02001803#if defined(MBEDTLS_ZLIB_SUPPORT)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001804 ssl->session_negotiate->compression,
Manuel Pégourié-Gonnard6fa57bf2019-05-10 10:50:04 +02001805#endif
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001806 ssl->handshake->tls_prf,
1807 ssl->handshake->randbytes,
1808 ssl->minor_ver,
1809 ssl->conf->endpoint,
1810 ssl);
1811 if (ret != 0) {
1812 MBEDTLS_SSL_DEBUG_RET(1, "ssl_populate_transform", ret);
1813 return ret;
Manuel Pégourié-Gonnard040a9512019-05-06 12:05:58 +02001814 }
1815
1816 /* We no longer need Server/ClientHello.random values */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001817 mbedtls_platform_zeroize(ssl->handshake->randbytes,
1818 sizeof(ssl->handshake->randbytes));
Manuel Pégourié-Gonnard040a9512019-05-06 12:05:58 +02001819
Manuel Pégourié-Gonnardd73b47f2019-05-06 12:44:24 +02001820 /* Allocate compression buffer */
1821#if defined(MBEDTLS_ZLIB_SUPPORT)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001822 if (ssl->session_negotiate->compression == MBEDTLS_SSL_COMPRESS_DEFLATE &&
1823 ssl->compress_buf == NULL) {
1824 MBEDTLS_SSL_DEBUG_MSG(3, ("Allocating compression buffer"));
1825 ssl->compress_buf = mbedtls_calloc(1, MBEDTLS_SSL_COMPRESS_BUFFER_LEN);
1826 if (ssl->compress_buf == NULL) {
1827 MBEDTLS_SSL_DEBUG_MSG(1, ("alloc(%d bytes) failed",
1828 MBEDTLS_SSL_COMPRESS_BUFFER_LEN));
1829 return MBEDTLS_ERR_SSL_ALLOC_FAILED;
Manuel Pégourié-Gonnardd73b47f2019-05-06 12:44:24 +02001830 }
1831 }
1832#endif
1833
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001834 MBEDTLS_SSL_DEBUG_MSG(2, ("<= derive keys"));
Manuel Pégourié-Gonnard040a9512019-05-06 12:05:58 +02001835
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001836 return 0;
Manuel Pégourié-Gonnarde59ae232019-04-30 11:41:40 +02001837}
1838
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001839#if defined(MBEDTLS_SSL_PROTO_SSL3)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001840void ssl_calc_verify_ssl(const mbedtls_ssl_context *ssl,
1841 unsigned char *hash,
1842 size_t *hlen)
Paul Bakker5121ce52009-01-03 21:22:43 +00001843{
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02001844 mbedtls_md5_context md5;
1845 mbedtls_sha1_context sha1;
Paul Bakker5121ce52009-01-03 21:22:43 +00001846 unsigned char pad_1[48];
1847 unsigned char pad_2[48];
1848
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001849 MBEDTLS_SSL_DEBUG_MSG(2, ("=> calc verify ssl"));
Paul Bakker5121ce52009-01-03 21:22:43 +00001850
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001851 mbedtls_md5_init(&md5);
1852 mbedtls_sha1_init(&sha1);
Manuel Pégourié-Gonnard001f2b62015-07-06 16:21:13 +02001853
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001854 mbedtls_md5_clone(&md5, &ssl->handshake->fin_md5);
1855 mbedtls_sha1_clone(&sha1, &ssl->handshake->fin_sha1);
Paul Bakker5121ce52009-01-03 21:22:43 +00001856
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001857 memset(pad_1, 0x36, 48);
1858 memset(pad_2, 0x5C, 48);
Paul Bakker5121ce52009-01-03 21:22:43 +00001859
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001860 mbedtls_md5_update_ret(&md5, ssl->session_negotiate->master, 48);
1861 mbedtls_md5_update_ret(&md5, pad_1, 48);
1862 mbedtls_md5_finish_ret(&md5, hash);
Paul Bakker5121ce52009-01-03 21:22:43 +00001863
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001864 mbedtls_md5_starts_ret(&md5);
1865 mbedtls_md5_update_ret(&md5, ssl->session_negotiate->master, 48);
1866 mbedtls_md5_update_ret(&md5, pad_2, 48);
1867 mbedtls_md5_update_ret(&md5, hash, 16);
1868 mbedtls_md5_finish_ret(&md5, hash);
Paul Bakker5121ce52009-01-03 21:22:43 +00001869
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001870 mbedtls_sha1_update_ret(&sha1, ssl->session_negotiate->master, 48);
1871 mbedtls_sha1_update_ret(&sha1, pad_1, 40);
1872 mbedtls_sha1_finish_ret(&sha1, hash + 16);
Paul Bakker380da532012-04-18 16:10:25 +00001873
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001874 mbedtls_sha1_starts_ret(&sha1);
1875 mbedtls_sha1_update_ret(&sha1, ssl->session_negotiate->master, 48);
1876 mbedtls_sha1_update_ret(&sha1, pad_2, 40);
1877 mbedtls_sha1_update_ret(&sha1, hash + 16, 20);
1878 mbedtls_sha1_finish_ret(&sha1, hash + 16);
Paul Bakker380da532012-04-18 16:10:25 +00001879
Manuel Pégourié-Gonnardde718b92019-05-03 11:43:28 +02001880 *hlen = 36;
1881
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001882 MBEDTLS_SSL_DEBUG_BUF(3, "calculated verify result", hash, *hlen);
1883 MBEDTLS_SSL_DEBUG_MSG(2, ("<= calc verify"));
Paul Bakker380da532012-04-18 16:10:25 +00001884
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001885 mbedtls_md5_free(&md5);
1886 mbedtls_sha1_free(&sha1);
Paul Bakker5b4af392014-06-26 12:09:34 +02001887
Paul Bakker380da532012-04-18 16:10:25 +00001888 return;
1889}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001890#endif /* MBEDTLS_SSL_PROTO_SSL3 */
Paul Bakker380da532012-04-18 16:10:25 +00001891
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001892#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001893void ssl_calc_verify_tls(const mbedtls_ssl_context *ssl,
1894 unsigned char *hash,
1895 size_t *hlen)
Paul Bakker380da532012-04-18 16:10:25 +00001896{
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02001897 mbedtls_md5_context md5;
1898 mbedtls_sha1_context sha1;
Paul Bakker380da532012-04-18 16:10:25 +00001899
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001900 MBEDTLS_SSL_DEBUG_MSG(2, ("=> calc verify tls"));
Paul Bakker380da532012-04-18 16:10:25 +00001901
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001902 mbedtls_md5_init(&md5);
1903 mbedtls_sha1_init(&sha1);
Manuel Pégourié-Gonnard001f2b62015-07-06 16:21:13 +02001904
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001905 mbedtls_md5_clone(&md5, &ssl->handshake->fin_md5);
1906 mbedtls_sha1_clone(&sha1, &ssl->handshake->fin_sha1);
Paul Bakker380da532012-04-18 16:10:25 +00001907
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001908 mbedtls_md5_finish_ret(&md5, hash);
1909 mbedtls_sha1_finish_ret(&sha1, hash + 16);
Paul Bakker380da532012-04-18 16:10:25 +00001910
Manuel Pégourié-Gonnardde718b92019-05-03 11:43:28 +02001911 *hlen = 36;
1912
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001913 MBEDTLS_SSL_DEBUG_BUF(3, "calculated verify result", hash, *hlen);
1914 MBEDTLS_SSL_DEBUG_MSG(2, ("<= calc verify"));
Paul Bakker380da532012-04-18 16:10:25 +00001915
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001916 mbedtls_md5_free(&md5);
1917 mbedtls_sha1_free(&sha1);
Paul Bakker5b4af392014-06-26 12:09:34 +02001918
Paul Bakker380da532012-04-18 16:10:25 +00001919 return;
1920}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001921#endif /* MBEDTLS_SSL_PROTO_TLS1 || MBEDTLS_SSL_PROTO_TLS1_1 */
Paul Bakker380da532012-04-18 16:10:25 +00001922
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001923#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
1924#if defined(MBEDTLS_SHA256_C)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001925void ssl_calc_verify_tls_sha256(const mbedtls_ssl_context *ssl,
1926 unsigned char *hash,
1927 size_t *hlen)
Paul Bakker380da532012-04-18 16:10:25 +00001928{
Andrzej Kurekeb342242019-01-29 09:14:33 -05001929#if defined(MBEDTLS_USE_PSA_CRYPTO)
1930 size_t hash_size;
1931 psa_status_t status;
1932 psa_hash_operation_t sha256_psa = psa_hash_operation_init();
1933
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001934 MBEDTLS_SSL_DEBUG_MSG(2, ("=> PSA calc verify sha256"));
1935 status = psa_hash_clone(&ssl->handshake->fin_sha256_psa, &sha256_psa);
1936 if (status != PSA_SUCCESS) {
1937 MBEDTLS_SSL_DEBUG_MSG(2, ("PSA hash clone failed"));
Andrzej Kurekeb342242019-01-29 09:14:33 -05001938 return;
1939 }
1940
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001941 status = psa_hash_finish(&sha256_psa, hash, 32, &hash_size);
1942 if (status != PSA_SUCCESS) {
1943 MBEDTLS_SSL_DEBUG_MSG(2, ("PSA hash finish failed"));
Andrzej Kurekeb342242019-01-29 09:14:33 -05001944 return;
1945 }
Manuel Pégourié-Gonnardde718b92019-05-03 11:43:28 +02001946
1947 *hlen = 32;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001948 MBEDTLS_SSL_DEBUG_BUF(3, "PSA calculated verify result", hash, *hlen);
1949 MBEDTLS_SSL_DEBUG_MSG(2, ("<= PSA calc verify"));
Andrzej Kurekeb342242019-01-29 09:14:33 -05001950#else
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02001951 mbedtls_sha256_context sha256;
Paul Bakker380da532012-04-18 16:10:25 +00001952
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001953 mbedtls_sha256_init(&sha256);
Manuel Pégourié-Gonnard001f2b62015-07-06 16:21:13 +02001954
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001955 MBEDTLS_SSL_DEBUG_MSG(2, ("=> calc verify sha256"));
Paul Bakker380da532012-04-18 16:10:25 +00001956
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001957 mbedtls_sha256_clone(&sha256, &ssl->handshake->fin_sha256);
1958 mbedtls_sha256_finish_ret(&sha256, hash);
Paul Bakker380da532012-04-18 16:10:25 +00001959
Manuel Pégourié-Gonnardde718b92019-05-03 11:43:28 +02001960 *hlen = 32;
1961
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001962 MBEDTLS_SSL_DEBUG_BUF(3, "calculated verify result", hash, *hlen);
1963 MBEDTLS_SSL_DEBUG_MSG(2, ("<= calc verify"));
Paul Bakker380da532012-04-18 16:10:25 +00001964
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001965 mbedtls_sha256_free(&sha256);
Andrzej Kurekeb342242019-01-29 09:14:33 -05001966#endif /* MBEDTLS_USE_PSA_CRYPTO */
Paul Bakker380da532012-04-18 16:10:25 +00001967 return;
1968}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001969#endif /* MBEDTLS_SHA256_C */
Paul Bakker380da532012-04-18 16:10:25 +00001970
Gilles Peskined2d59372021-05-12 22:43:27 +02001971#if defined(MBEDTLS_SHA512_C) && !defined(MBEDTLS_SHA512_NO_SHA384)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001972void ssl_calc_verify_tls_sha384(const mbedtls_ssl_context *ssl,
1973 unsigned char *hash,
1974 size_t *hlen)
Paul Bakker380da532012-04-18 16:10:25 +00001975{
Andrzej Kurekeb342242019-01-29 09:14:33 -05001976#if defined(MBEDTLS_USE_PSA_CRYPTO)
1977 size_t hash_size;
1978 psa_status_t status;
Andrzej Kurek972fba52019-01-30 03:29:12 -05001979 psa_hash_operation_t sha384_psa = psa_hash_operation_init();
Andrzej Kurekeb342242019-01-29 09:14:33 -05001980
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001981 MBEDTLS_SSL_DEBUG_MSG(2, ("=> PSA calc verify sha384"));
1982 status = psa_hash_clone(&ssl->handshake->fin_sha384_psa, &sha384_psa);
1983 if (status != PSA_SUCCESS) {
1984 MBEDTLS_SSL_DEBUG_MSG(2, ("PSA hash clone failed"));
Andrzej Kurekeb342242019-01-29 09:14:33 -05001985 return;
1986 }
1987
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001988 status = psa_hash_finish(&sha384_psa, hash, 48, &hash_size);
1989 if (status != PSA_SUCCESS) {
1990 MBEDTLS_SSL_DEBUG_MSG(2, ("PSA hash finish failed"));
Andrzej Kurekeb342242019-01-29 09:14:33 -05001991 return;
1992 }
Manuel Pégourié-Gonnardde718b92019-05-03 11:43:28 +02001993
1994 *hlen = 48;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001995 MBEDTLS_SSL_DEBUG_BUF(3, "PSA calculated verify result", hash, *hlen);
1996 MBEDTLS_SSL_DEBUG_MSG(2, ("<= PSA calc verify"));
Andrzej Kurekeb342242019-01-29 09:14:33 -05001997#else
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02001998 mbedtls_sha512_context sha512;
Paul Bakker380da532012-04-18 16:10:25 +00001999
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002000 mbedtls_sha512_init(&sha512);
Manuel Pégourié-Gonnard001f2b62015-07-06 16:21:13 +02002001
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002002 MBEDTLS_SSL_DEBUG_MSG(2, ("=> calc verify sha384"));
Paul Bakker380da532012-04-18 16:10:25 +00002003
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002004 mbedtls_sha512_clone(&sha512, &ssl->handshake->fin_sha512);
2005 mbedtls_sha512_finish_ret(&sha512, hash);
Paul Bakker5121ce52009-01-03 21:22:43 +00002006
Manuel Pégourié-Gonnardde718b92019-05-03 11:43:28 +02002007 *hlen = 48;
2008
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002009 MBEDTLS_SSL_DEBUG_BUF(3, "calculated verify result", hash, *hlen);
2010 MBEDTLS_SSL_DEBUG_MSG(2, ("<= calc verify"));
Paul Bakker5121ce52009-01-03 21:22:43 +00002011
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002012 mbedtls_sha512_free(&sha512);
Andrzej Kurekeb342242019-01-29 09:14:33 -05002013#endif /* MBEDTLS_USE_PSA_CRYPTO */
Paul Bakker5121ce52009-01-03 21:22:43 +00002014 return;
2015}
Gilles Peskined2d59372021-05-12 22:43:27 +02002016#endif /* MBEDTLS_SHA512_C && !MBEDTLS_SHA512_NO_SHA384 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002017#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakker5121ce52009-01-03 21:22:43 +00002018
Gilles Peskineeccd8882020-03-10 12:19:08 +01002019#if defined(MBEDTLS_KEY_EXCHANGE_SOME_PSK_ENABLED)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002020int 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 +02002021{
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02002022 unsigned char *p = ssl->handshake->premaster;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002023 unsigned char *end = p + sizeof(ssl->handshake->premaster);
Guilhem Bryantb5f04e42020-04-01 11:23:58 +01002024 const unsigned char *psk = NULL;
Guilhem Bryant61b0fe62020-03-27 11:12:12 +00002025 size_t psk_len = 0;
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01002026
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002027 if (mbedtls_ssl_get_psk(ssl, &psk, &psk_len)
2028 == MBEDTLS_ERR_SSL_PRIVATE_KEY_REQUIRED) {
Guilhem Bryantc5285d82020-03-25 17:08:15 +00002029 /*
2030 * This should never happen because the existence of a PSK is always
2031 * checked before calling this function
2032 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002033 MBEDTLS_SSL_DEBUG_MSG(1, ("should never happen"));
2034 return MBEDTLS_ERR_SSL_INTERNAL_ERROR;
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01002035 }
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02002036
2037 /*
2038 * PMS = struct {
2039 * opaque other_secret<0..2^16-1>;
2040 * opaque psk<0..2^16-1>;
2041 * };
2042 * with "other_secret" depending on the particular key exchange
2043 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002044#if defined(MBEDTLS_KEY_EXCHANGE_PSK_ENABLED)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002045 if (key_ex == MBEDTLS_KEY_EXCHANGE_PSK) {
2046 if (end - p < 2) {
2047 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
2048 }
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02002049
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002050 MBEDTLS_PUT_UINT16_BE(psk_len, p, 0);
Joe Subbiania651e6f2021-08-23 11:35:25 +01002051 p += 2;
Manuel Pégourié-Gonnardbc5e5082015-10-21 12:35:29 +02002052
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002053 if (end < p || (size_t) (end - p) < psk_len) {
2054 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
2055 }
Manuel Pégourié-Gonnardbc5e5082015-10-21 12:35:29 +02002056
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002057 memset(p, 0, psk_len);
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01002058 p += psk_len;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002059 } else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002060#endif /* MBEDTLS_KEY_EXCHANGE_PSK_ENABLED */
2061#if defined(MBEDTLS_KEY_EXCHANGE_RSA_PSK_ENABLED)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002062 if (key_ex == MBEDTLS_KEY_EXCHANGE_RSA_PSK) {
Manuel Pégourié-Gonnard0fae60b2013-10-14 17:39:48 +02002063 /*
2064 * other_secret already set by the ClientKeyExchange message,
2065 * and is 48 bytes long
2066 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002067 if (end - p < 2) {
2068 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
2069 }
Philippe Antoine747fd532018-05-30 09:13:21 +02002070
Manuel Pégourié-Gonnard0fae60b2013-10-14 17:39:48 +02002071 *p++ = 0;
2072 *p++ = 48;
2073 p += 48;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002074 } else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002075#endif /* MBEDTLS_KEY_EXCHANGE_RSA_PSK_ENABLED */
2076#if defined(MBEDTLS_KEY_EXCHANGE_DHE_PSK_ENABLED)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002077 if (key_ex == MBEDTLS_KEY_EXCHANGE_DHE_PSK) {
Janos Follath865b3eb2019-12-16 11:46:15 +00002078 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnard33352052015-06-02 16:17:08 +01002079 size_t len;
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02002080
Manuel Pégourié-Gonnard8df68632014-06-23 17:56:08 +02002081 /* Write length only when we know the actual value */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002082 if ((ret = mbedtls_dhm_calc_secret(&ssl->handshake->dhm_ctx,
2083 p + 2, end - (p + 2), &len,
2084 ssl->conf->f_rng, ssl->conf->p_rng)) != 0) {
2085 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_dhm_calc_secret", ret);
2086 return ret;
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02002087 }
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002088 MBEDTLS_PUT_UINT16_BE(len, p, 0);
Joe Subbiania651e6f2021-08-23 11:35:25 +01002089 p += 2 + len;
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02002090
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002091 MBEDTLS_SSL_DEBUG_MPI(3, "DHM: K ", &ssl->handshake->dhm_ctx.K);
2092 } else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002093#endif /* MBEDTLS_KEY_EXCHANGE_DHE_PSK_ENABLED */
2094#if defined(MBEDTLS_KEY_EXCHANGE_ECDHE_PSK_ENABLED)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002095 if (key_ex == MBEDTLS_KEY_EXCHANGE_ECDHE_PSK) {
Janos Follath865b3eb2019-12-16 11:46:15 +00002096 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02002097 size_t zlen;
2098
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002099 if ((ret = mbedtls_ecdh_calc_secret(&ssl->handshake->ecdh_ctx, &zlen,
2100 p + 2, end - (p + 2),
2101 ssl->conf->f_rng, ssl->conf->p_rng)) != 0) {
2102 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ecdh_calc_secret", ret);
2103 return ret;
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02002104 }
2105
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002106 MBEDTLS_PUT_UINT16_BE(zlen, p, 0);
Joe Subbiania651e6f2021-08-23 11:35:25 +01002107 p += 2 + zlen;
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02002108
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002109 MBEDTLS_SSL_DEBUG_ECDH(3, &ssl->handshake->ecdh_ctx,
2110 MBEDTLS_DEBUG_ECDH_Z);
2111 } else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002112#endif /* MBEDTLS_KEY_EXCHANGE_ECDHE_PSK_ENABLED */
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02002113 {
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002114 MBEDTLS_SSL_DEBUG_MSG(1, ("should never happen"));
2115 return MBEDTLS_ERR_SSL_INTERNAL_ERROR;
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02002116 }
2117
2118 /* opaque psk<0..2^16-1>; */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002119 if (end - p < 2) {
2120 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
2121 }
Manuel Pégourié-Gonnardb2bf5a12014-03-25 16:28:12 +01002122
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002123 MBEDTLS_PUT_UINT16_BE(psk_len, p, 0);
Joe Subbiania651e6f2021-08-23 11:35:25 +01002124 p += 2;
Manuel Pégourié-Gonnardbc5e5082015-10-21 12:35:29 +02002125
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002126 if (end < p || (size_t) (end - p) < psk_len) {
2127 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
2128 }
Manuel Pégourié-Gonnardbc5e5082015-10-21 12:35:29 +02002129
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002130 memcpy(p, psk, psk_len);
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01002131 p += psk_len;
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02002132
2133 ssl->handshake->pmslen = p - ssl->handshake->premaster;
2134
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002135 return 0;
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02002136}
Gilles Peskineeccd8882020-03-10 12:19:08 +01002137#endif /* MBEDTLS_KEY_EXCHANGE_SOME_PSK_ENABLED */
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02002138
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002139#if defined(MBEDTLS_SSL_SRV_C) && defined(MBEDTLS_SSL_RENEGOTIATION)
Manuel Pégourié-Gonnardd904d662022-06-17 10:24:00 +02002140MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002141static int ssl_write_hello_request(mbedtls_ssl_context *ssl);
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +02002142
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002143#if defined(MBEDTLS_SSL_PROTO_DTLS)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002144int mbedtls_ssl_resend_hello_request(mbedtls_ssl_context *ssl)
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +02002145{
2146 /* If renegotiation is not enforced, retransmit until we would reach max
2147 * timeout if we were using the usual handshake doubling scheme */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002148 if (ssl->conf->renego_max_records < 0) {
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02002149 uint32_t ratio = ssl->conf->hs_timeout_max / ssl->conf->hs_timeout_min + 1;
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +02002150 unsigned char doublings = 1;
2151
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002152 while (ratio != 0) {
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +02002153 ++doublings;
2154 ratio >>= 1;
2155 }
2156
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002157 if (++ssl->renego_records_seen > doublings) {
2158 MBEDTLS_SSL_DEBUG_MSG(2, ("no longer retransmitting hello request"));
2159 return 0;
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +02002160 }
2161 }
2162
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002163 return ssl_write_hello_request(ssl);
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +02002164}
2165#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002166#endif /* MBEDTLS_SSL_SRV_C && MBEDTLS_SSL_RENEGOTIATION */
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02002167
Hanno Beckerb9d44792019-02-08 07:19:04 +00002168#if defined(MBEDTLS_X509_CRT_PARSE_C)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002169static void ssl_clear_peer_cert(mbedtls_ssl_session *session)
Hanno Beckerb9d44792019-02-08 07:19:04 +00002170{
2171#if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002172 if (session->peer_cert != NULL) {
2173 mbedtls_x509_crt_free(session->peer_cert);
2174 mbedtls_free(session->peer_cert);
Hanno Beckerb9d44792019-02-08 07:19:04 +00002175 session->peer_cert = NULL;
2176 }
2177#else /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002178 if (session->peer_cert_digest != NULL) {
Hanno Beckerb9d44792019-02-08 07:19:04 +00002179 /* Zeroization is not necessary. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002180 mbedtls_free(session->peer_cert_digest);
Hanno Beckerb9d44792019-02-08 07:19:04 +00002181 session->peer_cert_digest = NULL;
2182 session->peer_cert_digest_type = MBEDTLS_MD_NONE;
2183 session->peer_cert_digest_len = 0;
2184 }
2185#endif /* !MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
2186}
2187#endif /* MBEDTLS_X509_CRT_PARSE_C */
2188
Paul Bakker5121ce52009-01-03 21:22:43 +00002189/*
2190 * Handshake functions
2191 */
Gilles Peskineeccd8882020-03-10 12:19:08 +01002192#if !defined(MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED)
Gilles Peskinef9828522017-05-03 12:28:43 +02002193/* No certificate support -> dummy functions */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002194int mbedtls_ssl_write_certificate(mbedtls_ssl_context *ssl)
Paul Bakker5121ce52009-01-03 21:22:43 +00002195{
Hanno Beckere694c3e2017-12-27 21:34:08 +00002196 const mbedtls_ssl_ciphersuite_t *ciphersuite_info =
2197 ssl->handshake->ciphersuite_info;
Paul Bakker5121ce52009-01-03 21:22:43 +00002198
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002199 MBEDTLS_SSL_DEBUG_MSG(2, ("=> write certificate"));
Paul Bakker5121ce52009-01-03 21:22:43 +00002200
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002201 if (!mbedtls_ssl_ciphersuite_uses_srv_cert(ciphersuite_info)) {
2202 MBEDTLS_SSL_DEBUG_MSG(2, ("<= skip write certificate"));
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02002203 ssl->state++;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002204 return 0;
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02002205 }
2206
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002207 MBEDTLS_SSL_DEBUG_MSG(1, ("should never happen"));
2208 return MBEDTLS_ERR_SSL_INTERNAL_ERROR;
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002209}
2210
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002211int mbedtls_ssl_parse_certificate(mbedtls_ssl_context *ssl)
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002212{
Hanno Beckere694c3e2017-12-27 21:34:08 +00002213 const mbedtls_ssl_ciphersuite_t *ciphersuite_info =
2214 ssl->handshake->ciphersuite_info;
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002215
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002216 MBEDTLS_SSL_DEBUG_MSG(2, ("=> parse certificate"));
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002217
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002218 if (!mbedtls_ssl_ciphersuite_uses_srv_cert(ciphersuite_info)) {
2219 MBEDTLS_SSL_DEBUG_MSG(2, ("<= skip parse certificate"));
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002220 ssl->state++;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002221 return 0;
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002222 }
2223
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002224 MBEDTLS_SSL_DEBUG_MSG(1, ("should never happen"));
2225 return MBEDTLS_ERR_SSL_INTERNAL_ERROR;
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002226}
Gilles Peskinef9828522017-05-03 12:28:43 +02002227
Gilles Peskineeccd8882020-03-10 12:19:08 +01002228#else /* MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED */
Gilles Peskinef9828522017-05-03 12:28:43 +02002229/* Some certificate support -> implement write and parse */
2230
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002231int mbedtls_ssl_write_certificate(mbedtls_ssl_context *ssl)
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002232{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002233 int ret = MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002234 size_t i, n;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002235 const mbedtls_x509_crt *crt;
Hanno Beckere694c3e2017-12-27 21:34:08 +00002236 const mbedtls_ssl_ciphersuite_t *ciphersuite_info =
2237 ssl->handshake->ciphersuite_info;
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002238
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002239 MBEDTLS_SSL_DEBUG_MSG(2, ("=> write certificate"));
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002240
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002241 if (!mbedtls_ssl_ciphersuite_uses_srv_cert(ciphersuite_info)) {
2242 MBEDTLS_SSL_DEBUG_MSG(2, ("<= skip write certificate"));
Johan Pascal4f099262020-09-22 10:59:26 +02002243 ssl->state++;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002244 return 0;
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002245 }
2246
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002247#if defined(MBEDTLS_SSL_CLI_C)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002248 if (ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT) {
2249 if (ssl->client_auth == 0) {
2250 MBEDTLS_SSL_DEBUG_MSG(2, ("<= skip write certificate"));
Paul Bakker5121ce52009-01-03 21:22:43 +00002251 ssl->state++;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002252 return 0;
Paul Bakker5121ce52009-01-03 21:22:43 +00002253 }
2254
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002255#if defined(MBEDTLS_SSL_PROTO_SSL3)
Paul Bakker5121ce52009-01-03 21:22:43 +00002256 /*
2257 * If using SSLv3 and got no cert, send an Alert message
2258 * (otherwise an empty Certificate message will be sent).
2259 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002260 if (mbedtls_ssl_own_cert(ssl) == NULL &&
2261 ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0) {
Paul Bakker5121ce52009-01-03 21:22:43 +00002262 ssl->out_msglen = 2;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002263 ssl->out_msgtype = MBEDTLS_SSL_MSG_ALERT;
2264 ssl->out_msg[0] = MBEDTLS_SSL_ALERT_LEVEL_WARNING;
2265 ssl->out_msg[1] = MBEDTLS_SSL_ALERT_MSG_NO_CERT;
Paul Bakker5121ce52009-01-03 21:22:43 +00002266
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002267 MBEDTLS_SSL_DEBUG_MSG(2, ("got no certificate to send"));
Paul Bakker5121ce52009-01-03 21:22:43 +00002268 goto write_msg;
2269 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002270#endif /* MBEDTLS_SSL_PROTO_SSL3 */
Paul Bakker5121ce52009-01-03 21:22:43 +00002271 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002272#endif /* MBEDTLS_SSL_CLI_C */
2273#if defined(MBEDTLS_SSL_SRV_C)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002274 if (ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER) {
2275 if (mbedtls_ssl_own_cert(ssl) == NULL) {
2276 MBEDTLS_SSL_DEBUG_MSG(1, ("got no certificate to send"));
2277 return MBEDTLS_ERR_SSL_CERTIFICATE_REQUIRED;
Paul Bakker5121ce52009-01-03 21:22:43 +00002278 }
2279 }
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01002280#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00002281
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002282 MBEDTLS_SSL_DEBUG_CRT(3, "own certificate", mbedtls_ssl_own_cert(ssl));
Paul Bakker5121ce52009-01-03 21:22:43 +00002283
2284 /*
2285 * 0 . 0 handshake type
2286 * 1 . 3 handshake length
2287 * 4 . 6 length of all certs
2288 * 7 . 9 length of cert. 1
2289 * 10 . n-1 peer certificate
2290 * n . n+2 length of cert. 2
2291 * n+3 . ... upper level cert, etc.
2292 */
2293 i = 7;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002294 crt = mbedtls_ssl_own_cert(ssl);
Paul Bakker5121ce52009-01-03 21:22:43 +00002295
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002296 while (crt != NULL) {
Paul Bakker5121ce52009-01-03 21:22:43 +00002297 n = crt->raw.len;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002298 if (n > MBEDTLS_SSL_OUT_CONTENT_LEN - 3 - i) {
2299 MBEDTLS_SSL_DEBUG_MSG(1, ("certificate too large, %" MBEDTLS_PRINTF_SIZET
2300 " > %" MBEDTLS_PRINTF_SIZET,
2301 i + 3 + n, (size_t) MBEDTLS_SSL_OUT_CONTENT_LEN));
2302 return MBEDTLS_ERR_SSL_CERTIFICATE_TOO_LARGE;
Paul Bakker5121ce52009-01-03 21:22:43 +00002303 }
2304
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002305 ssl->out_msg[i] = MBEDTLS_BYTE_2(n);
2306 ssl->out_msg[i + 1] = MBEDTLS_BYTE_1(n);
2307 ssl->out_msg[i + 2] = MBEDTLS_BYTE_0(n);
Paul Bakker5121ce52009-01-03 21:22:43 +00002308
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002309 i += 3; memcpy(ssl->out_msg + i, crt->raw.p, n);
Paul Bakker5121ce52009-01-03 21:22:43 +00002310 i += n; crt = crt->next;
2311 }
2312
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002313 ssl->out_msg[4] = MBEDTLS_BYTE_2(i - 7);
2314 ssl->out_msg[5] = MBEDTLS_BYTE_1(i - 7);
2315 ssl->out_msg[6] = MBEDTLS_BYTE_0(i - 7);
Paul Bakker5121ce52009-01-03 21:22:43 +00002316
2317 ssl->out_msglen = i;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002318 ssl->out_msgtype = MBEDTLS_SSL_MSG_HANDSHAKE;
2319 ssl->out_msg[0] = MBEDTLS_SSL_HS_CERTIFICATE;
Paul Bakker5121ce52009-01-03 21:22:43 +00002320
Manuel Pégourié-Gonnardcf141ca2015-05-20 10:35:51 +02002321#if defined(MBEDTLS_SSL_PROTO_SSL3) && defined(MBEDTLS_SSL_CLI_C)
Paul Bakker5121ce52009-01-03 21:22:43 +00002322write_msg:
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002323#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00002324
2325 ssl->state++;
2326
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002327 if ((ret = mbedtls_ssl_write_handshake_msg(ssl)) != 0) {
2328 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ssl_write_handshake_msg", ret);
2329 return ret;
Paul Bakker5121ce52009-01-03 21:22:43 +00002330 }
2331
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002332 MBEDTLS_SSL_DEBUG_MSG(2, ("<= write certificate"));
Paul Bakker5121ce52009-01-03 21:22:43 +00002333
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002334 return ret;
Paul Bakker5121ce52009-01-03 21:22:43 +00002335}
2336
Hanno Becker84879e32019-01-31 07:44:03 +00002337#if defined(MBEDTLS_SSL_RENEGOTIATION) && defined(MBEDTLS_SSL_CLI_C)
Hanno Becker177475a2019-02-05 17:02:46 +00002338
2339#if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
Manuel Pégourié-Gonnardd904d662022-06-17 10:24:00 +02002340MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002341static int ssl_check_peer_crt_unchanged(mbedtls_ssl_context *ssl,
2342 unsigned char *crt_buf,
2343 size_t crt_buf_len)
Hanno Beckerdef9bdc2019-01-30 14:46:46 +00002344{
2345 mbedtls_x509_crt const * const peer_crt = ssl->session->peer_cert;
2346
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002347 if (peer_crt == NULL) {
2348 return -1;
2349 }
Hanno Beckerdef9bdc2019-01-30 14:46:46 +00002350
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002351 if (peer_crt->raw.len != crt_buf_len) {
2352 return -1;
2353 }
Hanno Beckerdef9bdc2019-01-30 14:46:46 +00002354
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002355 return memcmp(peer_crt->raw.p, crt_buf, peer_crt->raw.len);
Hanno Beckerdef9bdc2019-01-30 14:46:46 +00002356}
Hanno Becker177475a2019-02-05 17:02:46 +00002357#else /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
Manuel Pégourié-Gonnardd904d662022-06-17 10:24:00 +02002358MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002359static int ssl_check_peer_crt_unchanged(mbedtls_ssl_context *ssl,
2360 unsigned char *crt_buf,
2361 size_t crt_buf_len)
Hanno Becker177475a2019-02-05 17:02:46 +00002362{
Janos Follath865b3eb2019-12-16 11:46:15 +00002363 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Hanno Becker177475a2019-02-05 17:02:46 +00002364 unsigned char const * const peer_cert_digest =
2365 ssl->session->peer_cert_digest;
2366 mbedtls_md_type_t const peer_cert_digest_type =
2367 ssl->session->peer_cert_digest_type;
2368 mbedtls_md_info_t const * const digest_info =
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002369 mbedtls_md_info_from_type(peer_cert_digest_type);
Hanno Becker177475a2019-02-05 17:02:46 +00002370 unsigned char tmp_digest[MBEDTLS_SSL_PEER_CERT_DIGEST_MAX_LEN];
2371 size_t digest_len;
2372
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002373 if (peer_cert_digest == NULL || digest_info == NULL) {
2374 return -1;
2375 }
Hanno Becker177475a2019-02-05 17:02:46 +00002376
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002377 digest_len = mbedtls_md_get_size(digest_info);
2378 if (digest_len > MBEDTLS_SSL_PEER_CERT_DIGEST_MAX_LEN) {
2379 return -1;
2380 }
Hanno Becker177475a2019-02-05 17:02:46 +00002381
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002382 ret = mbedtls_md(digest_info, crt_buf, crt_buf_len, tmp_digest);
2383 if (ret != 0) {
2384 return -1;
2385 }
Hanno Becker177475a2019-02-05 17:02:46 +00002386
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002387 return memcmp(tmp_digest, peer_cert_digest, digest_len);
Hanno Becker177475a2019-02-05 17:02:46 +00002388}
2389#endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
Hanno Becker84879e32019-01-31 07:44:03 +00002390#endif /* MBEDTLS_SSL_RENEGOTIATION && MBEDTLS_SSL_CLI_C */
Hanno Beckerdef9bdc2019-01-30 14:46:46 +00002391
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02002392/*
2393 * Once the certificate message is read, parse it into a cert chain and
2394 * perform basic checks, but leave actual verification to the caller
2395 */
Manuel Pégourié-Gonnardd904d662022-06-17 10:24:00 +02002396MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002397static int ssl_parse_certificate_chain(mbedtls_ssl_context *ssl,
2398 mbedtls_x509_crt *chain)
Paul Bakker5121ce52009-01-03 21:22:43 +00002399{
Janos Follath865b3eb2019-12-16 11:46:15 +00002400 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Hanno Beckerc7bd7802019-02-05 15:37:23 +00002401#if defined(MBEDTLS_SSL_RENEGOTIATION) && defined(MBEDTLS_SSL_CLI_C)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002402 int crt_cnt = 0;
Hanno Beckerc7bd7802019-02-05 15:37:23 +00002403#endif
Paul Bakker23986e52011-04-24 08:57:21 +00002404 size_t i, n;
Gilles Peskine064a85c2017-05-10 10:46:40 +02002405 uint8_t alert;
Paul Bakker5121ce52009-01-03 21:22:43 +00002406
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002407 if (ssl->in_msgtype != MBEDTLS_SSL_MSG_HANDSHAKE) {
2408 MBEDTLS_SSL_DEBUG_MSG(1, ("bad certificate message"));
2409 mbedtls_ssl_send_alert_message(ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
2410 MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE);
2411 return MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE;
Paul Bakker5121ce52009-01-03 21:22:43 +00002412 }
2413
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002414 if (ssl->in_msg[0] != MBEDTLS_SSL_HS_CERTIFICATE ||
2415 ssl->in_hslen < mbedtls_ssl_hs_hdr_len(ssl) + 3 + 3) {
2416 MBEDTLS_SSL_DEBUG_MSG(1, ("bad certificate message"));
2417 mbedtls_ssl_send_alert_message(ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
2418 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR);
2419 return MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE;
Paul Bakker5121ce52009-01-03 21:22:43 +00002420 }
2421
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002422 i = mbedtls_ssl_hs_hdr_len(ssl);
Manuel Pégourié-Gonnardf49a7da2014-09-10 13:30:43 +00002423
Paul Bakker5121ce52009-01-03 21:22:43 +00002424 /*
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002425 * Same message structure as in mbedtls_ssl_write_certificate()
Paul Bakker5121ce52009-01-03 21:22:43 +00002426 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002427 n = (ssl->in_msg[i+1] << 8) | ssl->in_msg[i+2];
Paul Bakker5121ce52009-01-03 21:22:43 +00002428
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002429 if (ssl->in_msg[i] != 0 ||
2430 ssl->in_hslen != n + 3 + mbedtls_ssl_hs_hdr_len(ssl)) {
2431 MBEDTLS_SSL_DEBUG_MSG(1, ("bad certificate message"));
2432 mbedtls_ssl_send_alert_message(ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
2433 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR);
2434 return MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE;
Paul Bakker5121ce52009-01-03 21:22:43 +00002435 }
2436
Hanno Beckerdef9bdc2019-01-30 14:46:46 +00002437 /* Make &ssl->in_msg[i] point to the beginning of the CRT chain. */
2438 i += 3;
2439
Hanno Beckerdef9bdc2019-01-30 14:46:46 +00002440 /* Iterate through and parse the CRTs in the provided chain. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002441 while (i < ssl->in_hslen) {
Hanno Beckerdef9bdc2019-01-30 14:46:46 +00002442 /* Check that there's room for the next CRT's length fields. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002443 if (i + 3 > ssl->in_hslen) {
2444 MBEDTLS_SSL_DEBUG_MSG(1, ("bad certificate message"));
2445 mbedtls_ssl_send_alert_message(ssl,
2446 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
2447 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR);
2448 return MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE;
Philippe Antoine747fd532018-05-30 09:13:21 +02002449 }
Hanno Beckerdef9bdc2019-01-30 14:46:46 +00002450 /* In theory, the CRT can be up to 2**24 Bytes, but we don't support
2451 * anything beyond 2**16 ~ 64K. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002452 if (ssl->in_msg[i] != 0) {
2453 MBEDTLS_SSL_DEBUG_MSG(1, ("bad certificate message"));
2454 mbedtls_ssl_send_alert_message(ssl,
2455 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
2456 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR);
2457 return MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE;
Paul Bakker5121ce52009-01-03 21:22:43 +00002458 }
2459
Hanno Beckerdef9bdc2019-01-30 14:46:46 +00002460 /* Read length of the next CRT in the chain. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002461 n = ((unsigned int) ssl->in_msg[i + 1] << 8)
Paul Bakker5121ce52009-01-03 21:22:43 +00002462 | (unsigned int) ssl->in_msg[i + 2];
2463 i += 3;
2464
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002465 if (n < 128 || i + n > ssl->in_hslen) {
2466 MBEDTLS_SSL_DEBUG_MSG(1, ("bad certificate message"));
2467 mbedtls_ssl_send_alert_message(ssl,
2468 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
2469 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR);
2470 return MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE;
Paul Bakker5121ce52009-01-03 21:22:43 +00002471 }
2472
Hanno Beckerdef9bdc2019-01-30 14:46:46 +00002473 /* Check if we're handling the first CRT in the chain. */
Hanno Beckerc7bd7802019-02-05 15:37:23 +00002474#if defined(MBEDTLS_SSL_RENEGOTIATION) && defined(MBEDTLS_SSL_CLI_C)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002475 if (crt_cnt++ == 0 &&
Hanno Beckerc7bd7802019-02-05 15:37:23 +00002476 ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT &&
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002477 ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS) {
Hanno Becker46f34d02019-02-08 14:00:04 +00002478 /* During client-side renegotiation, check that the server's
2479 * end-CRTs hasn't changed compared to the initial handshake,
2480 * mitigating the triple handshake attack. On success, reuse
2481 * the original end-CRT instead of parsing it again. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002482 MBEDTLS_SSL_DEBUG_MSG(3, ("Check that peer CRT hasn't changed during renegotiation"));
2483 if (ssl_check_peer_crt_unchanged(ssl,
2484 &ssl->in_msg[i],
2485 n) != 0) {
2486 MBEDTLS_SSL_DEBUG_MSG(1, ("new server cert during renegotiation"));
2487 mbedtls_ssl_send_alert_message(ssl,
2488 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
2489 MBEDTLS_SSL_ALERT_MSG_ACCESS_DENIED);
2490 return MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE;
Hanno Beckerdef9bdc2019-01-30 14:46:46 +00002491 }
Hanno Beckerc7bd7802019-02-05 15:37:23 +00002492
2493 /* Now we can safely free the original chain. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002494 ssl_clear_peer_cert(ssl->session);
Hanno Beckerc7bd7802019-02-05 15:37:23 +00002495 }
Hanno Beckerdef9bdc2019-01-30 14:46:46 +00002496#endif /* MBEDTLS_SSL_RENEGOTIATION && MBEDTLS_SSL_CLI_C */
2497
Hanno Beckerdef9bdc2019-01-30 14:46:46 +00002498 /* Parse the next certificate in the chain. */
Hanno Becker0056eab2019-02-08 14:39:16 +00002499#if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002500 ret = mbedtls_x509_crt_parse_der(chain, ssl->in_msg + i, n);
Hanno Becker0056eab2019-02-08 14:39:16 +00002501#else
Hanno Becker353a6f02019-02-26 11:51:34 +00002502 /* If we don't need to store the CRT chain permanently, parse
Hanno Becker0056eab2019-02-08 14:39:16 +00002503 * it in-place from the input buffer instead of making a copy. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002504 ret = mbedtls_x509_crt_parse_der_nocopy(chain, ssl->in_msg + i, n);
Hanno Becker0056eab2019-02-08 14:39:16 +00002505#endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002506 switch (ret) {
Hanno Beckerdef9bdc2019-01-30 14:46:46 +00002507 case 0: /*ok*/
2508 case MBEDTLS_ERR_X509_UNKNOWN_SIG_ALG + MBEDTLS_ERR_OID_NOT_FOUND:
2509 /* Ignore certificate with an unknown algorithm: maybe a
2510 prior certificate was already trusted. */
2511 break;
Gilles Peskine1cc8e342017-05-03 16:28:34 +02002512
Hanno Beckerdef9bdc2019-01-30 14:46:46 +00002513 case MBEDTLS_ERR_X509_ALLOC_FAILED:
2514 alert = MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR;
2515 goto crt_parse_der_failed;
Gilles Peskine1cc8e342017-05-03 16:28:34 +02002516
Hanno Beckerdef9bdc2019-01-30 14:46:46 +00002517 case MBEDTLS_ERR_X509_UNKNOWN_VERSION:
2518 alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT;
2519 goto crt_parse_der_failed;
Gilles Peskine1cc8e342017-05-03 16:28:34 +02002520
Hanno Beckerdef9bdc2019-01-30 14:46:46 +00002521 default:
2522 alert = MBEDTLS_SSL_ALERT_MSG_BAD_CERT;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002523crt_parse_der_failed:
2524 mbedtls_ssl_send_alert_message(ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL, alert);
2525 MBEDTLS_SSL_DEBUG_RET(1, " mbedtls_x509_crt_parse_der", ret);
2526 return ret;
Paul Bakker5121ce52009-01-03 21:22:43 +00002527 }
2528
2529 i += n;
2530 }
2531
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002532 MBEDTLS_SSL_DEBUG_CRT(3, "peer certificate", chain);
2533 return 0;
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02002534}
2535
Hanno Becker4a55f632019-02-05 12:49:06 +00002536#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnardd904d662022-06-17 10:24:00 +02002537MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002538static int ssl_srv_check_client_no_crt_notification(mbedtls_ssl_context *ssl)
Hanno Becker4a55f632019-02-05 12:49:06 +00002539{
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002540 if (ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT) {
2541 return -1;
2542 }
Hanno Becker4a55f632019-02-05 12:49:06 +00002543
2544#if defined(MBEDTLS_SSL_PROTO_SSL3)
2545 /*
2546 * Check if the client sent an empty certificate
2547 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002548 if (ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0) {
2549 if (ssl->in_msglen == 2 &&
Hanno Becker4a55f632019-02-05 12:49:06 +00002550 ssl->in_msgtype == MBEDTLS_SSL_MSG_ALERT &&
2551 ssl->in_msg[0] == MBEDTLS_SSL_ALERT_LEVEL_WARNING &&
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002552 ssl->in_msg[1] == MBEDTLS_SSL_ALERT_MSG_NO_CERT) {
2553 MBEDTLS_SSL_DEBUG_MSG(1, ("SSLv3 client has no certificate"));
2554 return 0;
Hanno Becker4a55f632019-02-05 12:49:06 +00002555 }
2556
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002557 return -1;
Hanno Becker4a55f632019-02-05 12:49:06 +00002558 }
2559#endif /* MBEDTLS_SSL_PROTO_SSL3 */
2560
2561#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1) || \
2562 defined(MBEDTLS_SSL_PROTO_TLS1_2)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002563 if (ssl->in_hslen == 3 + mbedtls_ssl_hs_hdr_len(ssl) &&
Hanno Becker4a55f632019-02-05 12:49:06 +00002564 ssl->in_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE &&
2565 ssl->in_msg[0] == MBEDTLS_SSL_HS_CERTIFICATE &&
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002566 memcmp(ssl->in_msg + mbedtls_ssl_hs_hdr_len(ssl), "\0\0\0", 3) == 0) {
2567 MBEDTLS_SSL_DEBUG_MSG(1, ("TLSv1 client has no certificate"));
2568 return 0;
Hanno Becker4a55f632019-02-05 12:49:06 +00002569 }
2570
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002571 return -1;
Hanno Becker4a55f632019-02-05 12:49:06 +00002572#endif /* MBEDTLS_SSL_PROTO_TLS1 || MBEDTLS_SSL_PROTO_TLS1_1 || \
2573 MBEDTLS_SSL_PROTO_TLS1_2 */
2574}
2575#endif /* MBEDTLS_SSL_SRV_C */
2576
Hanno Becker28f2fcd2019-02-07 10:11:07 +00002577/* Check if a certificate message is expected.
2578 * Return either
2579 * - SSL_CERTIFICATE_EXPECTED, or
2580 * - SSL_CERTIFICATE_SKIP
2581 * indicating whether a Certificate message is expected or not.
2582 */
2583#define SSL_CERTIFICATE_EXPECTED 0
2584#define SSL_CERTIFICATE_SKIP 1
Manuel Pégourié-Gonnardd904d662022-06-17 10:24:00 +02002585MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002586static int ssl_parse_certificate_coordinate(mbedtls_ssl_context *ssl,
2587 int authmode)
Hanno Becker28f2fcd2019-02-07 10:11:07 +00002588{
2589 const mbedtls_ssl_ciphersuite_t *ciphersuite_info =
Hanno Beckere694c3e2017-12-27 21:34:08 +00002590 ssl->handshake->ciphersuite_info;
Hanno Becker28f2fcd2019-02-07 10:11:07 +00002591
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002592 if (!mbedtls_ssl_ciphersuite_uses_srv_cert(ciphersuite_info)) {
2593 return SSL_CERTIFICATE_SKIP;
2594 }
Hanno Becker28f2fcd2019-02-07 10:11:07 +00002595
2596#if defined(MBEDTLS_SSL_SRV_C)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002597 if (ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER) {
2598 if (ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_RSA_PSK) {
2599 return SSL_CERTIFICATE_SKIP;
2600 }
Hanno Becker28f2fcd2019-02-07 10:11:07 +00002601
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002602 if (authmode == MBEDTLS_SSL_VERIFY_NONE) {
Hanno Becker28f2fcd2019-02-07 10:11:07 +00002603 ssl->session_negotiate->verify_result =
2604 MBEDTLS_X509_BADCERT_SKIP_VERIFY;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002605 return SSL_CERTIFICATE_SKIP;
Hanno Becker28f2fcd2019-02-07 10:11:07 +00002606 }
2607 }
Hanno Becker84d9d272019-03-01 08:10:46 +00002608#else
2609 ((void) authmode);
Hanno Becker28f2fcd2019-02-07 10:11:07 +00002610#endif /* MBEDTLS_SSL_SRV_C */
2611
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002612 return SSL_CERTIFICATE_EXPECTED;
Hanno Becker28f2fcd2019-02-07 10:11:07 +00002613}
2614
Gilles Peskine3a2f75d2025-02-12 23:28:48 +01002615static int get_hostname_for_verification(mbedtls_ssl_context *ssl,
2616 const char **hostname)
2617{
2618 if (!mbedtls_ssl_has_set_hostname_been_called(ssl)) {
2619 MBEDTLS_SSL_DEBUG_MSG(1, ("Certificate verification without having set hostname"));
2620 }
2621
2622 *hostname = mbedtls_ssl_get_hostname_pointer(ssl);
2623 if (*hostname == NULL) {
2624 MBEDTLS_SSL_DEBUG_MSG(2, ("Certificate verification without CN verification"));
2625 }
2626
2627 return 0;
2628}
2629
Manuel Pégourié-Gonnardd904d662022-06-17 10:24:00 +02002630MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002631static int ssl_parse_certificate_verify(mbedtls_ssl_context *ssl,
2632 int authmode,
2633 mbedtls_x509_crt *chain,
2634 void *rs_ctx)
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02002635{
Hanno Becker28f2fcd2019-02-07 10:11:07 +00002636 const mbedtls_ssl_ciphersuite_t *ciphersuite_info =
Hanno Beckere694c3e2017-12-27 21:34:08 +00002637 ssl->handshake->ciphersuite_info;
Hanno Beckerafd0b0a2019-03-27 16:55:01 +00002638 int have_ca_chain = 0;
Hanno Becker68636192019-02-05 14:36:34 +00002639
Hanno Becker8927c832019-04-03 12:52:50 +01002640 int (*f_vrfy)(void *, mbedtls_x509_crt *, int, uint32_t *);
2641 void *p_vrfy;
2642
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002643 if (authmode == MBEDTLS_SSL_VERIFY_NONE) {
2644 return 0;
2645 }
Hanno Becker68636192019-02-05 14:36:34 +00002646
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002647 if (ssl->f_vrfy != NULL) {
2648 MBEDTLS_SSL_DEBUG_MSG(3, ("Use context-specific verification callback"));
Hanno Becker8927c832019-04-03 12:52:50 +01002649 f_vrfy = ssl->f_vrfy;
2650 p_vrfy = ssl->p_vrfy;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002651 } else {
2652 MBEDTLS_SSL_DEBUG_MSG(3, ("Use configuration-specific verification callback"));
Hanno Becker8927c832019-04-03 12:52:50 +01002653 f_vrfy = ssl->conf->f_vrfy;
2654 p_vrfy = ssl->conf->p_vrfy;
2655 }
2656
Gilles Peskine3a2f75d2025-02-12 23:28:48 +01002657 const char *hostname = "";
2658 int ret = get_hostname_for_verification(ssl, &hostname);
2659 if (ret != 0) {
2660 MBEDTLS_SSL_DEBUG_RET(1, "get_hostname_for_verification", ret);
2661 return ret;
2662 }
2663
Hanno Becker68636192019-02-05 14:36:34 +00002664 /*
2665 * Main check: verify certificate
2666 */
Hanno Beckerafd0b0a2019-03-27 16:55:01 +00002667#if defined(MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002668 if (ssl->conf->f_ca_cb != NULL) {
Hanno Beckerafd0b0a2019-03-27 16:55:01 +00002669 ((void) rs_ctx);
2670 have_ca_chain = 1;
2671
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002672 MBEDTLS_SSL_DEBUG_MSG(3, ("use CA callback for X.509 CRT verification"));
Jarno Lamsa9822c0d2019-04-01 16:59:48 +03002673 ret = mbedtls_x509_crt_verify_with_ca_cb(
Hanno Beckerafd0b0a2019-03-27 16:55:01 +00002674 chain,
2675 ssl->conf->f_ca_cb,
2676 ssl->conf->p_ca_cb,
2677 ssl->conf->cert_profile,
Gilles Peskine3a2f75d2025-02-12 23:28:48 +01002678 hostname,
Hanno Beckerafd0b0a2019-03-27 16:55:01 +00002679 &ssl->session_negotiate->verify_result,
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002680 f_vrfy, p_vrfy);
2681 } else
Hanno Beckerafd0b0a2019-03-27 16:55:01 +00002682#endif /* MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK */
2683 {
2684 mbedtls_x509_crt *ca_chain;
2685 mbedtls_x509_crl *ca_crl;
2686
2687#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002688 if (ssl->handshake->sni_ca_chain != NULL) {
Hanno Beckerafd0b0a2019-03-27 16:55:01 +00002689 ca_chain = ssl->handshake->sni_ca_chain;
2690 ca_crl = ssl->handshake->sni_ca_crl;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002691 } else
Hanno Beckerafd0b0a2019-03-27 16:55:01 +00002692#endif
2693 {
2694 ca_chain = ssl->conf->ca_chain;
2695 ca_crl = ssl->conf->ca_crl;
2696 }
2697
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002698 if (ca_chain != NULL) {
Hanno Beckerafd0b0a2019-03-27 16:55:01 +00002699 have_ca_chain = 1;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002700 }
Hanno Beckerafd0b0a2019-03-27 16:55:01 +00002701
2702 ret = mbedtls_x509_crt_verify_restartable(
2703 chain,
2704 ca_chain, ca_crl,
2705 ssl->conf->cert_profile,
Gilles Peskine3a2f75d2025-02-12 23:28:48 +01002706 hostname,
Hanno Beckerafd0b0a2019-03-27 16:55:01 +00002707 &ssl->session_negotiate->verify_result,
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002708 f_vrfy, p_vrfy, rs_ctx);
Hanno Beckerafd0b0a2019-03-27 16:55:01 +00002709 }
Hanno Becker68636192019-02-05 14:36:34 +00002710
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002711 if (ret != 0) {
2712 MBEDTLS_SSL_DEBUG_RET(1, "x509_verify_cert", ret);
Hanno Becker68636192019-02-05 14:36:34 +00002713 }
2714
Gilles Peskineeccd8882020-03-10 12:19:08 +01002715#if defined(MBEDTLS_SSL_ECP_RESTARTABLE_ENABLED)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002716 if (ret == MBEDTLS_ERR_ECP_IN_PROGRESS) {
2717 return MBEDTLS_ERR_SSL_CRYPTO_IN_PROGRESS;
2718 }
Hanno Becker68636192019-02-05 14:36:34 +00002719#endif
2720
2721 /*
2722 * Secondary checks: always done, but change 'ret' only if it was 0
2723 */
2724
2725#if defined(MBEDTLS_ECP_C)
2726 {
2727 const mbedtls_pk_context *pk = &chain->pk;
2728
Manuel Pégourié-Gonnard06e1fcd2022-06-16 10:48:06 +02002729 /* If certificate uses an EC key, make sure the curve is OK.
2730 * This is a public key, so it can't be opaque, so can_do() is a good
2731 * enough check to ensure pk_ec() is safe to use here. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002732 if (mbedtls_pk_can_do(pk, MBEDTLS_PK_ECKEY) &&
2733 mbedtls_ssl_check_curve(ssl, mbedtls_pk_ec(*pk)->grp.id) != 0) {
Hanno Becker68636192019-02-05 14:36:34 +00002734 ssl->session_negotiate->verify_result |= MBEDTLS_X509_BADCERT_BAD_KEY;
2735
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002736 MBEDTLS_SSL_DEBUG_MSG(1, ("bad certificate (EC key curve)"));
2737 if (ret == 0) {
Hanno Becker68636192019-02-05 14:36:34 +00002738 ret = MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002739 }
Hanno Becker68636192019-02-05 14:36:34 +00002740 }
2741 }
2742#endif /* MBEDTLS_ECP_C */
2743
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002744 if (mbedtls_ssl_check_cert_usage(chain,
2745 ciphersuite_info,
2746 !ssl->conf->endpoint,
2747 &ssl->session_negotiate->verify_result) != 0) {
2748 MBEDTLS_SSL_DEBUG_MSG(1, ("bad certificate (usage extensions)"));
2749 if (ret == 0) {
Hanno Becker68636192019-02-05 14:36:34 +00002750 ret = MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002751 }
Hanno Becker68636192019-02-05 14:36:34 +00002752 }
2753
2754 /* mbedtls_x509_crt_verify_with_profile is supposed to report a
2755 * verification failure through MBEDTLS_ERR_X509_CERT_VERIFY_FAILED,
2756 * with details encoded in the verification flags. All other kinds
2757 * of error codes, including those from the user provided f_vrfy
2758 * functions, are treated as fatal and lead to a failure of
2759 * ssl_parse_certificate even if verification was optional. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002760 if (authmode == MBEDTLS_SSL_VERIFY_OPTIONAL &&
2761 (ret == MBEDTLS_ERR_X509_CERT_VERIFY_FAILED ||
2762 ret == MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE)) {
Hanno Becker68636192019-02-05 14:36:34 +00002763 ret = 0;
2764 }
2765
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002766 if (have_ca_chain == 0 && authmode == MBEDTLS_SSL_VERIFY_REQUIRED) {
2767 MBEDTLS_SSL_DEBUG_MSG(1, ("got no CA chain"));
Hanno Becker68636192019-02-05 14:36:34 +00002768 ret = MBEDTLS_ERR_SSL_CA_CHAIN_REQUIRED;
2769 }
2770
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002771 if (ret != 0) {
Hanno Becker68636192019-02-05 14:36:34 +00002772 uint8_t alert;
2773
2774 /* The certificate may have been rejected for several reasons.
2775 Pick one and send the corresponding alert. Which alert to send
2776 may be a subject of debate in some cases. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002777 if (ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_OTHER) {
Hanno Becker68636192019-02-05 14:36:34 +00002778 alert = MBEDTLS_SSL_ALERT_MSG_ACCESS_DENIED;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002779 } else if (ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_CN_MISMATCH) {
Hanno Becker68636192019-02-05 14:36:34 +00002780 alert = MBEDTLS_SSL_ALERT_MSG_BAD_CERT;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002781 } else if (ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_KEY_USAGE) {
Hanno Becker68636192019-02-05 14:36:34 +00002782 alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002783 } else if (ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_EXT_KEY_USAGE) {
Hanno Becker68636192019-02-05 14:36:34 +00002784 alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002785 } else if (ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_NS_CERT_TYPE) {
Hanno Becker68636192019-02-05 14:36:34 +00002786 alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002787 } else if (ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_BAD_PK) {
Hanno Becker68636192019-02-05 14:36:34 +00002788 alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002789 } else if (ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_BAD_KEY) {
Hanno Becker68636192019-02-05 14:36:34 +00002790 alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002791 } else if (ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_EXPIRED) {
Hanno Becker68636192019-02-05 14:36:34 +00002792 alert = MBEDTLS_SSL_ALERT_MSG_CERT_EXPIRED;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002793 } else if (ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_REVOKED) {
Hanno Becker68636192019-02-05 14:36:34 +00002794 alert = MBEDTLS_SSL_ALERT_MSG_CERT_REVOKED;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002795 } else if (ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_NOT_TRUSTED) {
Hanno Becker68636192019-02-05 14:36:34 +00002796 alert = MBEDTLS_SSL_ALERT_MSG_UNKNOWN_CA;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002797 } else {
Hanno Becker68636192019-02-05 14:36:34 +00002798 alert = MBEDTLS_SSL_ALERT_MSG_CERT_UNKNOWN;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002799 }
2800 mbedtls_ssl_send_alert_message(ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
2801 alert);
Hanno Becker68636192019-02-05 14:36:34 +00002802 }
2803
2804#if defined(MBEDTLS_DEBUG_C)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002805 if (ssl->session_negotiate->verify_result != 0) {
2806 MBEDTLS_SSL_DEBUG_MSG(3, ("! Certificate verification flags %08x",
2807 (unsigned int) ssl->session_negotiate->verify_result));
2808 } else {
2809 MBEDTLS_SSL_DEBUG_MSG(3, ("Certificate verification flags clear"));
Hanno Becker68636192019-02-05 14:36:34 +00002810 }
2811#endif /* MBEDTLS_DEBUG_C */
2812
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002813 return ret;
Hanno Becker68636192019-02-05 14:36:34 +00002814}
2815
Hanno Becker6b8fbab2019-02-08 14:59:05 +00002816#if !defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
Manuel Pégourié-Gonnardd904d662022-06-17 10:24:00 +02002817MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002818static int ssl_remember_peer_crt_digest(mbedtls_ssl_context *ssl,
2819 unsigned char *start, size_t len)
Hanno Becker6b8fbab2019-02-08 14:59:05 +00002820{
Janos Follath865b3eb2019-12-16 11:46:15 +00002821 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Hanno Becker6b8fbab2019-02-08 14:59:05 +00002822 /* Remember digest of the peer's end-CRT. */
2823 ssl->session_negotiate->peer_cert_digest =
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002824 mbedtls_calloc(1, MBEDTLS_SSL_PEER_CERT_DIGEST_DFL_LEN);
2825 if (ssl->session_negotiate->peer_cert_digest == NULL) {
2826 MBEDTLS_SSL_DEBUG_MSG(1, ("alloc(%d bytes) failed",
2827 MBEDTLS_SSL_PEER_CERT_DIGEST_DFL_LEN));
2828 mbedtls_ssl_send_alert_message(ssl,
2829 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
2830 MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR);
Hanno Becker6b8fbab2019-02-08 14:59:05 +00002831
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002832 return MBEDTLS_ERR_SSL_ALLOC_FAILED;
Hanno Becker6b8fbab2019-02-08 14:59:05 +00002833 }
2834
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002835 ret = mbedtls_md(mbedtls_md_info_from_type(
2836 MBEDTLS_SSL_PEER_CERT_DIGEST_DFL_TYPE),
2837 start, len,
2838 ssl->session_negotiate->peer_cert_digest);
Hanno Becker6b8fbab2019-02-08 14:59:05 +00002839
2840 ssl->session_negotiate->peer_cert_digest_type =
2841 MBEDTLS_SSL_PEER_CERT_DIGEST_DFL_TYPE;
2842 ssl->session_negotiate->peer_cert_digest_len =
2843 MBEDTLS_SSL_PEER_CERT_DIGEST_DFL_LEN;
2844
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002845 return ret;
Hanno Becker6b8fbab2019-02-08 14:59:05 +00002846}
2847
Manuel Pégourié-Gonnardd904d662022-06-17 10:24:00 +02002848MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002849static int ssl_remember_peer_pubkey(mbedtls_ssl_context *ssl,
2850 unsigned char *start, size_t len)
Hanno Becker6b8fbab2019-02-08 14:59:05 +00002851{
2852 unsigned char *end = start + len;
Janos Follath865b3eb2019-12-16 11:46:15 +00002853 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Hanno Becker6b8fbab2019-02-08 14:59:05 +00002854
2855 /* Make a copy of the peer's raw public key. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002856 mbedtls_pk_init(&ssl->handshake->peer_pubkey);
2857 ret = mbedtls_pk_parse_subpubkey(&start, end,
2858 &ssl->handshake->peer_pubkey);
2859 if (ret != 0) {
Hanno Becker6b8fbab2019-02-08 14:59:05 +00002860 /* We should have parsed the public key before. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002861 return MBEDTLS_ERR_SSL_INTERNAL_ERROR;
Hanno Becker6b8fbab2019-02-08 14:59:05 +00002862 }
2863
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002864 return 0;
Hanno Becker6b8fbab2019-02-08 14:59:05 +00002865}
2866#endif /* !MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
2867
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002868int mbedtls_ssl_parse_certificate(mbedtls_ssl_context *ssl)
Hanno Becker68636192019-02-05 14:36:34 +00002869{
2870 int ret = 0;
Hanno Becker28f2fcd2019-02-07 10:11:07 +00002871 int crt_expected;
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02002872#if defined(MBEDTLS_SSL_SRV_C) && defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
2873 const int authmode = ssl->handshake->sni_authmode != MBEDTLS_SSL_VERIFY_UNSET
2874 ? ssl->handshake->sni_authmode
2875 : ssl->conf->authmode;
2876#else
Johan Pascal4f099262020-09-22 10:59:26 +02002877 const int authmode = ssl->conf->authmode;
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02002878#endif
Manuel Pégourié-Gonnard3bf49c42017-08-15 13:47:06 +02002879 void *rs_ctx = NULL;
Hanno Becker3dad3112019-02-05 17:19:52 +00002880 mbedtls_x509_crt *chain = NULL;
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02002881
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002882 MBEDTLS_SSL_DEBUG_MSG(2, ("=> parse certificate"));
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02002883
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002884 crt_expected = ssl_parse_certificate_coordinate(ssl, authmode);
2885 if (crt_expected == SSL_CERTIFICATE_SKIP) {
2886 MBEDTLS_SSL_DEBUG_MSG(2, ("<= skip parse certificate"));
Hanno Becker6bdfab22019-02-05 13:11:17 +00002887 goto exit;
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02002888 }
2889
Gilles Peskineeccd8882020-03-10 12:19:08 +01002890#if defined(MBEDTLS_SSL_ECP_RESTARTABLE_ENABLED)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002891 if (ssl->handshake->ecrs_enabled &&
2892 ssl->handshake->ecrs_state == ssl_ecrs_crt_verify) {
Hanno Becker3dad3112019-02-05 17:19:52 +00002893 chain = ssl->handshake->ecrs_peer_cert;
2894 ssl->handshake->ecrs_peer_cert = NULL;
Manuel Pégourié-Gonnard3bf49c42017-08-15 13:47:06 +02002895 goto crt_verify;
2896 }
2897#endif
2898
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002899 if ((ret = mbedtls_ssl_read_record(ssl, 1)) != 0) {
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02002900 /* mbedtls_ssl_read_record may have sent an alert already. We
2901 let it decide whether to alert. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002902 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ssl_read_record", ret);
Hanno Becker3dad3112019-02-05 17:19:52 +00002903 goto exit;
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02002904 }
2905
Hanno Becker4a55f632019-02-05 12:49:06 +00002906#if defined(MBEDTLS_SSL_SRV_C)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002907 if (ssl_srv_check_client_no_crt_notification(ssl) == 0) {
Hanno Becker4a55f632019-02-05 12:49:06 +00002908 ssl->session_negotiate->verify_result = MBEDTLS_X509_BADCERT_MISSING;
Hanno Becker4a55f632019-02-05 12:49:06 +00002909
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002910 if (authmode != MBEDTLS_SSL_VERIFY_OPTIONAL) {
Hanno Becker6bdfab22019-02-05 13:11:17 +00002911 ret = MBEDTLS_ERR_SSL_NO_CLIENT_CERTIFICATE;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002912 }
Hanno Becker4a55f632019-02-05 12:49:06 +00002913
Hanno Becker6bdfab22019-02-05 13:11:17 +00002914 goto exit;
Hanno Becker4a55f632019-02-05 12:49:06 +00002915 }
2916#endif /* MBEDTLS_SSL_SRV_C */
2917
Hanno Beckerc7bd7802019-02-05 15:37:23 +00002918 /* Clear existing peer CRT structure in case we tried to
2919 * reuse a session but it failed, and allocate a new one. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002920 ssl_clear_peer_cert(ssl->session_negotiate);
Hanno Becker3dad3112019-02-05 17:19:52 +00002921
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002922 chain = mbedtls_calloc(1, sizeof(mbedtls_x509_crt));
2923 if (chain == NULL) {
2924 MBEDTLS_SSL_DEBUG_MSG(1, ("alloc(%" MBEDTLS_PRINTF_SIZET " bytes) failed",
2925 sizeof(mbedtls_x509_crt)));
2926 mbedtls_ssl_send_alert_message(ssl,
2927 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
2928 MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR);
Hanno Becker7a955a02019-02-05 13:08:01 +00002929
Hanno Becker3dad3112019-02-05 17:19:52 +00002930 ret = MBEDTLS_ERR_SSL_ALLOC_FAILED;
2931 goto exit;
2932 }
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002933 mbedtls_x509_crt_init(chain);
Hanno Becker3dad3112019-02-05 17:19:52 +00002934
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002935 ret = ssl_parse_certificate_chain(ssl, chain);
2936 if (ret != 0) {
Hanno Becker3dad3112019-02-05 17:19:52 +00002937 goto exit;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002938 }
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02002939
Gilles Peskineeccd8882020-03-10 12:19:08 +01002940#if defined(MBEDTLS_SSL_ECP_RESTARTABLE_ENABLED)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002941 if (ssl->handshake->ecrs_enabled) {
Manuel Pégourié-Gonnard0b23f162017-08-24 12:08:33 +02002942 ssl->handshake->ecrs_state = ssl_ecrs_crt_verify;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002943 }
Manuel Pégourié-Gonnard3bf49c42017-08-15 13:47:06 +02002944
2945crt_verify:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002946 if (ssl->handshake->ecrs_enabled) {
Manuel Pégourié-Gonnard3bf49c42017-08-15 13:47:06 +02002947 rs_ctx = &ssl->handshake->ecrs_ctx;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002948 }
Manuel Pégourié-Gonnard3bf49c42017-08-15 13:47:06 +02002949#endif
2950
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002951 ret = ssl_parse_certificate_verify(ssl, authmode,
2952 chain, rs_ctx);
2953 if (ret != 0) {
Hanno Becker3dad3112019-02-05 17:19:52 +00002954 goto exit;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002955 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002956
Hanno Becker6bbd94c2019-02-05 17:02:28 +00002957#if !defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
Hanno Becker6bbd94c2019-02-05 17:02:28 +00002958 {
Hanno Becker6b8fbab2019-02-08 14:59:05 +00002959 unsigned char *crt_start, *pk_start;
2960 size_t crt_len, pk_len;
Hanno Becker3dad3112019-02-05 17:19:52 +00002961
Hanno Becker6b8fbab2019-02-08 14:59:05 +00002962 /* We parse the CRT chain without copying, so
2963 * these pointers point into the input buffer,
2964 * and are hence still valid after freeing the
2965 * CRT chain. */
Hanno Becker6bbd94c2019-02-05 17:02:28 +00002966
Hanno Becker6b8fbab2019-02-08 14:59:05 +00002967 crt_start = chain->raw.p;
2968 crt_len = chain->raw.len;
Hanno Becker6bbd94c2019-02-05 17:02:28 +00002969
Hanno Becker6b8fbab2019-02-08 14:59:05 +00002970 pk_start = chain->pk_raw.p;
2971 pk_len = chain->pk_raw.len;
2972
2973 /* Free the CRT structures before computing
2974 * digest and copying the peer's public key. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002975 mbedtls_x509_crt_free(chain);
2976 mbedtls_free(chain);
Hanno Becker6b8fbab2019-02-08 14:59:05 +00002977 chain = NULL;
2978
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002979 ret = ssl_remember_peer_crt_digest(ssl, crt_start, crt_len);
2980 if (ret != 0) {
Hanno Beckera2747532019-02-06 16:19:04 +00002981 goto exit;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002982 }
Hanno Becker6b8fbab2019-02-08 14:59:05 +00002983
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002984 ret = ssl_remember_peer_pubkey(ssl, pk_start, pk_len);
2985 if (ret != 0) {
Hanno Becker6b8fbab2019-02-08 14:59:05 +00002986 goto exit;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002987 }
Hanno Beckera2747532019-02-06 16:19:04 +00002988 }
Hanno Becker6b8fbab2019-02-08 14:59:05 +00002989#else /* !MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
2990 /* Pass ownership to session structure. */
Hanno Becker3dad3112019-02-05 17:19:52 +00002991 ssl->session_negotiate->peer_cert = chain;
2992 chain = NULL;
Hanno Becker6b8fbab2019-02-08 14:59:05 +00002993#endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
Hanno Becker3dad3112019-02-05 17:19:52 +00002994
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002995 MBEDTLS_SSL_DEBUG_MSG(2, ("<= parse certificate"));
Paul Bakker5121ce52009-01-03 21:22:43 +00002996
Hanno Becker6bdfab22019-02-05 13:11:17 +00002997exit:
2998
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002999 if (ret == 0) {
Hanno Becker3dad3112019-02-05 17:19:52 +00003000 ssl->state++;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003001 }
Hanno Becker3dad3112019-02-05 17:19:52 +00003002
Gilles Peskineeccd8882020-03-10 12:19:08 +01003003#if defined(MBEDTLS_SSL_ECP_RESTARTABLE_ENABLED)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003004 if (ret == MBEDTLS_ERR_SSL_CRYPTO_IN_PROGRESS) {
Hanno Becker3dad3112019-02-05 17:19:52 +00003005 ssl->handshake->ecrs_peer_cert = chain;
3006 chain = NULL;
3007 }
3008#endif
3009
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003010 if (chain != NULL) {
3011 mbedtls_x509_crt_free(chain);
3012 mbedtls_free(chain);
Hanno Becker3dad3112019-02-05 17:19:52 +00003013 }
3014
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003015 return ret;
Paul Bakker5121ce52009-01-03 21:22:43 +00003016}
Gilles Peskineeccd8882020-03-10 12:19:08 +01003017#endif /* MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED */
Paul Bakker5121ce52009-01-03 21:22:43 +00003018
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003019void mbedtls_ssl_optimize_checksum(mbedtls_ssl_context *ssl,
3020 const mbedtls_ssl_ciphersuite_t *ciphersuite_info)
Paul Bakker380da532012-04-18 16:10:25 +00003021{
Paul Bakkerfb08fd22013-08-27 15:06:26 +02003022 ((void) ciphersuite_info);
Paul Bakker769075d2012-11-24 11:26:46 +01003023
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003024#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1) || \
3025 defined(MBEDTLS_SSL_PROTO_TLS1_1)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003026 if (ssl->minor_ver < MBEDTLS_SSL_MINOR_VERSION_3) {
Paul Bakker48916f92012-09-16 19:57:18 +00003027 ssl->handshake->update_checksum = ssl_update_checksum_md5sha1;
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#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
Gilles Peskined2d59372021-05-12 22:43:27 +02003031#if defined(MBEDTLS_SHA512_C) && !defined(MBEDTLS_SHA512_NO_SHA384)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003032 if (ciphersuite_info->mac == MBEDTLS_MD_SHA384) {
Paul Bakkerd2f068e2013-08-27 21:19:20 +02003033 ssl->handshake->update_checksum = ssl_update_checksum_sha384;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003034 } else
Paul Bakkerd2f068e2013-08-27 21:19:20 +02003035#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003036#if defined(MBEDTLS_SHA256_C)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003037 if (ciphersuite_info->mac != MBEDTLS_MD_SHA384) {
Paul Bakker48916f92012-09-16 19:57:18 +00003038 ssl->handshake->update_checksum = ssl_update_checksum_sha256;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003039 } else
Paul Bakkerd2f068e2013-08-27 21:19:20 +02003040#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003041#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Manuel Pégourié-Gonnard61edffe2014-04-11 17:07:31 +02003042 {
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003043 MBEDTLS_SSL_DEBUG_MSG(1, ("should never happen"));
Paul Bakkerd2f068e2013-08-27 21:19:20 +02003044 return;
Manuel Pégourié-Gonnard61edffe2014-04-11 17:07:31 +02003045 }
Paul Bakker380da532012-04-18 16:10:25 +00003046}
Paul Bakkerf7abd422013-04-16 13:15:56 +02003047
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003048void mbedtls_ssl_reset_checksum(mbedtls_ssl_context *ssl)
Manuel Pégourié-Gonnard67427c02014-07-11 13:45:34 +02003049{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003050#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1) || \
3051 defined(MBEDTLS_SSL_PROTO_TLS1_1)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003052 mbedtls_md5_starts_ret(&ssl->handshake->fin_md5);
3053 mbedtls_sha1_starts_ret(&ssl->handshake->fin_sha1);
Manuel Pégourié-Gonnard67427c02014-07-11 13:45:34 +02003054#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003055#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
3056#if defined(MBEDTLS_SHA256_C)
Andrzej Kurekeb342242019-01-29 09:14:33 -05003057#if defined(MBEDTLS_USE_PSA_CRYPTO)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003058 psa_hash_abort(&ssl->handshake->fin_sha256_psa);
3059 psa_hash_setup(&ssl->handshake->fin_sha256_psa, PSA_ALG_SHA_256);
Andrzej Kurekeb342242019-01-29 09:14:33 -05003060#else
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003061 mbedtls_sha256_starts_ret(&ssl->handshake->fin_sha256, 0);
Manuel Pégourié-Gonnard67427c02014-07-11 13:45:34 +02003062#endif
Andrzej Kurekeb342242019-01-29 09:14:33 -05003063#endif
Gilles Peskined2d59372021-05-12 22:43:27 +02003064#if defined(MBEDTLS_SHA512_C) && !defined(MBEDTLS_SHA512_NO_SHA384)
Andrzej Kurekeb342242019-01-29 09:14:33 -05003065#if defined(MBEDTLS_USE_PSA_CRYPTO)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003066 psa_hash_abort(&ssl->handshake->fin_sha384_psa);
3067 psa_hash_setup(&ssl->handshake->fin_sha384_psa, PSA_ALG_SHA_384);
Andrzej Kurekeb342242019-01-29 09:14:33 -05003068#else
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003069 mbedtls_sha512_starts_ret(&ssl->handshake->fin_sha512, 1);
Manuel Pégourié-Gonnard67427c02014-07-11 13:45:34 +02003070#endif
Andrzej Kurekeb342242019-01-29 09:14:33 -05003071#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003072#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Manuel Pégourié-Gonnard67427c02014-07-11 13:45:34 +02003073}
3074
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003075static void ssl_update_checksum_start(mbedtls_ssl_context *ssl,
3076 const unsigned char *buf, size_t len)
Paul Bakker380da532012-04-18 16:10:25 +00003077{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003078#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1) || \
3079 defined(MBEDTLS_SSL_PROTO_TLS1_1)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003080 mbedtls_md5_update_ret(&ssl->handshake->fin_md5, buf, len);
3081 mbedtls_sha1_update_ret(&ssl->handshake->fin_sha1, buf, len);
Paul Bakkerd2f068e2013-08-27 21:19:20 +02003082#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003083#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
3084#if defined(MBEDTLS_SHA256_C)
Andrzej Kurekeb342242019-01-29 09:14:33 -05003085#if defined(MBEDTLS_USE_PSA_CRYPTO)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003086 psa_hash_update(&ssl->handshake->fin_sha256_psa, buf, len);
Andrzej Kurekeb342242019-01-29 09:14:33 -05003087#else
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003088 mbedtls_sha256_update_ret(&ssl->handshake->fin_sha256, buf, len);
Paul Bakkerd2f068e2013-08-27 21:19:20 +02003089#endif
Andrzej Kurekeb342242019-01-29 09:14:33 -05003090#endif
Gilles Peskined2d59372021-05-12 22:43:27 +02003091#if defined(MBEDTLS_SHA512_C) && !defined(MBEDTLS_SHA512_NO_SHA384)
Andrzej Kurekeb342242019-01-29 09:14:33 -05003092#if defined(MBEDTLS_USE_PSA_CRYPTO)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003093 psa_hash_update(&ssl->handshake->fin_sha384_psa, buf, len);
Andrzej Kurekeb342242019-01-29 09:14:33 -05003094#else
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003095 mbedtls_sha512_update_ret(&ssl->handshake->fin_sha512, buf, len);
Paul Bakker769075d2012-11-24 11:26:46 +01003096#endif
Andrzej Kurekeb342242019-01-29 09:14:33 -05003097#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003098#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakker380da532012-04-18 16:10:25 +00003099}
3100
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003101#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1) || \
3102 defined(MBEDTLS_SSL_PROTO_TLS1_1)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003103static void ssl_update_checksum_md5sha1(mbedtls_ssl_context *ssl,
3104 const unsigned char *buf, size_t len)
Paul Bakker380da532012-04-18 16:10:25 +00003105{
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003106 mbedtls_md5_update_ret(&ssl->handshake->fin_md5, buf, len);
3107 mbedtls_sha1_update_ret(&ssl->handshake->fin_sha1, buf, len);
Paul Bakker380da532012-04-18 16:10:25 +00003108}
Paul Bakkerd2f068e2013-08-27 21:19:20 +02003109#endif
Paul Bakker380da532012-04-18 16:10:25 +00003110
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003111#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
3112#if defined(MBEDTLS_SHA256_C)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003113static void ssl_update_checksum_sha256(mbedtls_ssl_context *ssl,
3114 const unsigned char *buf, size_t len)
Paul Bakker380da532012-04-18 16:10:25 +00003115{
Andrzej Kurekeb342242019-01-29 09:14:33 -05003116#if defined(MBEDTLS_USE_PSA_CRYPTO)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003117 psa_hash_update(&ssl->handshake->fin_sha256_psa, buf, len);
Andrzej Kurekeb342242019-01-29 09:14:33 -05003118#else
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003119 mbedtls_sha256_update_ret(&ssl->handshake->fin_sha256, buf, len);
Andrzej Kurekeb342242019-01-29 09:14:33 -05003120#endif
Paul Bakker380da532012-04-18 16:10:25 +00003121}
Paul Bakkerd2f068e2013-08-27 21:19:20 +02003122#endif
Paul Bakker380da532012-04-18 16:10:25 +00003123
Gilles Peskined2d59372021-05-12 22:43:27 +02003124#if defined(MBEDTLS_SHA512_C) && !defined(MBEDTLS_SHA512_NO_SHA384)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003125static void ssl_update_checksum_sha384(mbedtls_ssl_context *ssl,
3126 const unsigned char *buf, size_t len)
Paul Bakker380da532012-04-18 16:10:25 +00003127{
Andrzej Kurekeb342242019-01-29 09:14:33 -05003128#if defined(MBEDTLS_USE_PSA_CRYPTO)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003129 psa_hash_update(&ssl->handshake->fin_sha384_psa, buf, len);
Andrzej Kurekeb342242019-01-29 09:14:33 -05003130#else
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003131 mbedtls_sha512_update_ret(&ssl->handshake->fin_sha512, buf, len);
Andrzej Kurekeb342242019-01-29 09:14:33 -05003132#endif
Paul Bakker380da532012-04-18 16:10:25 +00003133}
Paul Bakker769075d2012-11-24 11:26:46 +01003134#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003135#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakker380da532012-04-18 16:10:25 +00003136
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003137#if defined(MBEDTLS_SSL_PROTO_SSL3)
Paul Bakker1ef83d62012-04-11 12:09:53 +00003138static void ssl_calc_finished_ssl(
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003139 mbedtls_ssl_context *ssl, unsigned char *buf, int from)
Paul Bakker5121ce52009-01-03 21:22:43 +00003140{
Paul Bakker3c2122f2013-06-24 19:03:14 +02003141 const char *sender;
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02003142 mbedtls_md5_context md5;
3143 mbedtls_sha1_context sha1;
Paul Bakker1ef83d62012-04-11 12:09:53 +00003144
Paul Bakker5121ce52009-01-03 21:22:43 +00003145 unsigned char padbuf[48];
3146 unsigned char md5sum[16];
3147 unsigned char sha1sum[20];
3148
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003149 mbedtls_ssl_session *session = ssl->session_negotiate;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003150 if (!session) {
Paul Bakker48916f92012-09-16 19:57:18 +00003151 session = ssl->session;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003152 }
Paul Bakker48916f92012-09-16 19:57:18 +00003153
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003154 MBEDTLS_SSL_DEBUG_MSG(2, ("=> calc finished ssl"));
Paul Bakker1ef83d62012-04-11 12:09:53 +00003155
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003156 mbedtls_md5_init(&md5);
3157 mbedtls_sha1_init(&sha1);
Manuel Pégourié-Gonnard001f2b62015-07-06 16:21:13 +02003158
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003159 mbedtls_md5_clone(&md5, &ssl->handshake->fin_md5);
3160 mbedtls_sha1_clone(&sha1, &ssl->handshake->fin_sha1);
Paul Bakker5121ce52009-01-03 21:22:43 +00003161
3162 /*
3163 * SSLv3:
3164 * hash =
3165 * MD5( master + pad2 +
3166 * MD5( handshake + sender + master + pad1 ) )
3167 * + SHA1( master + pad2 +
3168 * SHA1( handshake + sender + master + pad1 ) )
Paul Bakker5121ce52009-01-03 21:22:43 +00003169 */
3170
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003171#if !defined(MBEDTLS_MD5_ALT)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003172 MBEDTLS_SSL_DEBUG_BUF(4, "finished md5 state", (unsigned char *)
3173 md5.state, sizeof(md5.state));
Paul Bakker90995b52013-06-24 19:20:35 +02003174#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00003175
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003176#if !defined(MBEDTLS_SHA1_ALT)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003177 MBEDTLS_SSL_DEBUG_BUF(4, "finished sha1 state", (unsigned char *)
3178 sha1.state, sizeof(sha1.state));
Paul Bakker90995b52013-06-24 19:20:35 +02003179#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00003180
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003181 sender = (from == MBEDTLS_SSL_IS_CLIENT) ? "CLNT"
Paul Bakker3c2122f2013-06-24 19:03:14 +02003182 : "SRVR";
Paul Bakker5121ce52009-01-03 21:22:43 +00003183
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003184 memset(padbuf, 0x36, 48);
Paul Bakker5121ce52009-01-03 21:22:43 +00003185
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003186 mbedtls_md5_update_ret(&md5, (const unsigned char *) sender, 4);
3187 mbedtls_md5_update_ret(&md5, session->master, 48);
3188 mbedtls_md5_update_ret(&md5, padbuf, 48);
3189 mbedtls_md5_finish_ret(&md5, md5sum);
Paul Bakker5121ce52009-01-03 21:22:43 +00003190
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003191 mbedtls_sha1_update_ret(&sha1, (const unsigned char *) sender, 4);
3192 mbedtls_sha1_update_ret(&sha1, session->master, 48);
3193 mbedtls_sha1_update_ret(&sha1, padbuf, 40);
3194 mbedtls_sha1_finish_ret(&sha1, sha1sum);
Paul Bakker5121ce52009-01-03 21:22:43 +00003195
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003196 memset(padbuf, 0x5C, 48);
Paul Bakker5121ce52009-01-03 21:22:43 +00003197
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003198 mbedtls_md5_starts_ret(&md5);
3199 mbedtls_md5_update_ret(&md5, session->master, 48);
3200 mbedtls_md5_update_ret(&md5, padbuf, 48);
3201 mbedtls_md5_update_ret(&md5, md5sum, 16);
3202 mbedtls_md5_finish_ret(&md5, buf);
Paul Bakker5121ce52009-01-03 21:22:43 +00003203
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003204 mbedtls_sha1_starts_ret(&sha1);
3205 mbedtls_sha1_update_ret(&sha1, session->master, 48);
3206 mbedtls_sha1_update_ret(&sha1, padbuf, 40);
3207 mbedtls_sha1_update_ret(&sha1, sha1sum, 20);
3208 mbedtls_sha1_finish_ret(&sha1, buf + 16);
Paul Bakker5121ce52009-01-03 21:22:43 +00003209
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003210 MBEDTLS_SSL_DEBUG_BUF(3, "calc finished result", buf, 36);
Paul Bakker5121ce52009-01-03 21:22:43 +00003211
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003212 mbedtls_md5_free(&md5);
3213 mbedtls_sha1_free(&sha1);
Paul Bakker5121ce52009-01-03 21:22:43 +00003214
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003215 mbedtls_platform_zeroize(padbuf, sizeof(padbuf));
3216 mbedtls_platform_zeroize(md5sum, sizeof(md5sum));
3217 mbedtls_platform_zeroize(sha1sum, sizeof(sha1sum));
Paul Bakker5121ce52009-01-03 21:22:43 +00003218
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003219 MBEDTLS_SSL_DEBUG_MSG(2, ("<= calc finished"));
Paul Bakker5121ce52009-01-03 21:22:43 +00003220}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003221#endif /* MBEDTLS_SSL_PROTO_SSL3 */
Paul Bakker5121ce52009-01-03 21:22:43 +00003222
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003223#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1)
Paul Bakker1ef83d62012-04-11 12:09:53 +00003224static void ssl_calc_finished_tls(
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003225 mbedtls_ssl_context *ssl, unsigned char *buf, int from)
Paul Bakker5121ce52009-01-03 21:22:43 +00003226{
Paul Bakker1ef83d62012-04-11 12:09:53 +00003227 int len = 12;
Paul Bakker3c2122f2013-06-24 19:03:14 +02003228 const char *sender;
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02003229 mbedtls_md5_context md5;
3230 mbedtls_sha1_context sha1;
Paul Bakker1ef83d62012-04-11 12:09:53 +00003231 unsigned char padbuf[36];
Paul Bakker5121ce52009-01-03 21:22:43 +00003232
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003233 mbedtls_ssl_session *session = ssl->session_negotiate;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003234 if (!session) {
Paul Bakker48916f92012-09-16 19:57:18 +00003235 session = ssl->session;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003236 }
Paul Bakker48916f92012-09-16 19:57:18 +00003237
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003238 MBEDTLS_SSL_DEBUG_MSG(2, ("=> calc finished tls"));
Paul Bakker5121ce52009-01-03 21:22:43 +00003239
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003240 mbedtls_md5_init(&md5);
3241 mbedtls_sha1_init(&sha1);
Manuel Pégourié-Gonnard001f2b62015-07-06 16:21:13 +02003242
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003243 mbedtls_md5_clone(&md5, &ssl->handshake->fin_md5);
3244 mbedtls_sha1_clone(&sha1, &ssl->handshake->fin_sha1);
Paul Bakker5121ce52009-01-03 21:22:43 +00003245
Paul Bakker1ef83d62012-04-11 12:09:53 +00003246 /*
3247 * TLSv1:
3248 * hash = PRF( master, finished_label,
3249 * MD5( handshake ) + SHA1( handshake ) )[0..11]
3250 */
Paul Bakker5121ce52009-01-03 21:22:43 +00003251
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003252#if !defined(MBEDTLS_MD5_ALT)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003253 MBEDTLS_SSL_DEBUG_BUF(4, "finished md5 state", (unsigned char *)
3254 md5.state, sizeof(md5.state));
Paul Bakker90995b52013-06-24 19:20:35 +02003255#endif
Paul Bakker1ef83d62012-04-11 12:09:53 +00003256
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003257#if !defined(MBEDTLS_SHA1_ALT)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003258 MBEDTLS_SSL_DEBUG_BUF(4, "finished sha1 state", (unsigned char *)
3259 sha1.state, sizeof(sha1.state));
Paul Bakker90995b52013-06-24 19:20:35 +02003260#endif
Paul Bakker1ef83d62012-04-11 12:09:53 +00003261
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003262 sender = (from == MBEDTLS_SSL_IS_CLIENT)
Paul Bakker3c2122f2013-06-24 19:03:14 +02003263 ? "client finished"
3264 : "server finished";
Paul Bakker1ef83d62012-04-11 12:09:53 +00003265
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003266 mbedtls_md5_finish_ret(&md5, padbuf);
3267 mbedtls_sha1_finish_ret(&sha1, padbuf + 16);
Paul Bakker1ef83d62012-04-11 12:09:53 +00003268
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003269 ssl->handshake->tls_prf(session->master, 48, sender,
3270 padbuf, 36, buf, len);
Paul Bakker1ef83d62012-04-11 12:09:53 +00003271
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003272 MBEDTLS_SSL_DEBUG_BUF(3, "calc finished result", buf, len);
Paul Bakker1ef83d62012-04-11 12:09:53 +00003273
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003274 mbedtls_md5_free(&md5);
3275 mbedtls_sha1_free(&sha1);
Paul Bakker1ef83d62012-04-11 12:09:53 +00003276
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003277 mbedtls_platform_zeroize(padbuf, sizeof(padbuf));
Paul Bakker1ef83d62012-04-11 12:09:53 +00003278
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003279 MBEDTLS_SSL_DEBUG_MSG(2, ("<= calc finished"));
Paul Bakker1ef83d62012-04-11 12:09:53 +00003280}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003281#endif /* MBEDTLS_SSL_PROTO_TLS1 || MBEDTLS_SSL_PROTO_TLS1_1 */
Paul Bakker1ef83d62012-04-11 12:09:53 +00003282
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003283#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
3284#if defined(MBEDTLS_SHA256_C)
Paul Bakkerca4ab492012-04-18 14:23:57 +00003285static void ssl_calc_finished_tls_sha256(
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003286 mbedtls_ssl_context *ssl, unsigned char *buf, int from)
Paul Bakker1ef83d62012-04-11 12:09:53 +00003287{
3288 int len = 12;
Paul Bakker3c2122f2013-06-24 19:03:14 +02003289 const char *sender;
Paul Bakker1ef83d62012-04-11 12:09:53 +00003290 unsigned char padbuf[32];
Andrzej Kurekeb342242019-01-29 09:14:33 -05003291#if defined(MBEDTLS_USE_PSA_CRYPTO)
3292 size_t hash_size;
Jaeden Amero34973232019-02-20 10:32:28 +00003293 psa_hash_operation_t sha256_psa = PSA_HASH_OPERATION_INIT;
Andrzej Kurekeb342242019-01-29 09:14:33 -05003294 psa_status_t status;
3295#else
3296 mbedtls_sha256_context sha256;
3297#endif
Paul Bakker1ef83d62012-04-11 12:09:53 +00003298
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003299 mbedtls_ssl_session *session = ssl->session_negotiate;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003300 if (!session) {
Paul Bakker48916f92012-09-16 19:57:18 +00003301 session = ssl->session;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003302 }
Paul Bakker48916f92012-09-16 19:57:18 +00003303
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003304 sender = (from == MBEDTLS_SSL_IS_CLIENT)
Andrzej Kurekeb342242019-01-29 09:14:33 -05003305 ? "client finished"
3306 : "server finished";
3307
3308#if defined(MBEDTLS_USE_PSA_CRYPTO)
3309 sha256_psa = psa_hash_operation_init();
3310
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003311 MBEDTLS_SSL_DEBUG_MSG(2, ("=> calc PSA finished tls sha256"));
Andrzej Kurekeb342242019-01-29 09:14:33 -05003312
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003313 status = psa_hash_clone(&ssl->handshake->fin_sha256_psa, &sha256_psa);
3314 if (status != PSA_SUCCESS) {
3315 MBEDTLS_SSL_DEBUG_MSG(2, ("PSA hash clone failed"));
Andrzej Kurekeb342242019-01-29 09:14:33 -05003316 return;
3317 }
3318
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003319 status = psa_hash_finish(&sha256_psa, padbuf, sizeof(padbuf), &hash_size);
3320 if (status != PSA_SUCCESS) {
3321 MBEDTLS_SSL_DEBUG_MSG(2, ("PSA hash finish failed"));
Andrzej Kurekeb342242019-01-29 09:14:33 -05003322 return;
3323 }
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003324 MBEDTLS_SSL_DEBUG_BUF(3, "PSA calculated padbuf", padbuf, 32);
Andrzej Kurekeb342242019-01-29 09:14:33 -05003325#else
3326
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003327 mbedtls_sha256_init(&sha256);
Manuel Pégourié-Gonnard001f2b62015-07-06 16:21:13 +02003328
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003329 MBEDTLS_SSL_DEBUG_MSG(2, ("=> calc finished tls sha256"));
Paul Bakker1ef83d62012-04-11 12:09:53 +00003330
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003331 mbedtls_sha256_clone(&sha256, &ssl->handshake->fin_sha256);
Paul Bakker1ef83d62012-04-11 12:09:53 +00003332
3333 /*
3334 * TLSv1.2:
3335 * hash = PRF( master, finished_label,
3336 * Hash( handshake ) )[0.11]
3337 */
3338
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003339#if !defined(MBEDTLS_SHA256_ALT)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003340 MBEDTLS_SSL_DEBUG_BUF(4, "finished sha2 state", (unsigned char *)
3341 sha256.state, sizeof(sha256.state));
Paul Bakker90995b52013-06-24 19:20:35 +02003342#endif
Paul Bakker1ef83d62012-04-11 12:09:53 +00003343
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003344 mbedtls_sha256_finish_ret(&sha256, padbuf);
3345 mbedtls_sha256_free(&sha256);
Andrzej Kurekeb342242019-01-29 09:14:33 -05003346#endif /* MBEDTLS_USE_PSA_CRYPTO */
Paul Bakker1ef83d62012-04-11 12:09:53 +00003347
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003348 ssl->handshake->tls_prf(session->master, 48, sender,
3349 padbuf, 32, buf, len);
Paul Bakker1ef83d62012-04-11 12:09:53 +00003350
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003351 MBEDTLS_SSL_DEBUG_BUF(3, "calc finished result", buf, len);
Paul Bakker1ef83d62012-04-11 12:09:53 +00003352
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003353 mbedtls_platform_zeroize(padbuf, sizeof(padbuf));
Paul Bakker1ef83d62012-04-11 12:09:53 +00003354
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003355 MBEDTLS_SSL_DEBUG_MSG(2, ("<= calc finished"));
Paul Bakker1ef83d62012-04-11 12:09:53 +00003356}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003357#endif /* MBEDTLS_SHA256_C */
Paul Bakker1ef83d62012-04-11 12:09:53 +00003358
Gilles Peskined2d59372021-05-12 22:43:27 +02003359#if defined(MBEDTLS_SHA512_C) && !defined(MBEDTLS_SHA512_NO_SHA384)
Rodrigo Dias Corread596ca82020-11-25 00:42:28 -03003360
Paul Bakkerca4ab492012-04-18 14:23:57 +00003361static void ssl_calc_finished_tls_sha384(
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003362 mbedtls_ssl_context *ssl, unsigned char *buf, int from)
Paul Bakkerca4ab492012-04-18 14:23:57 +00003363{
3364 int len = 12;
Paul Bakker3c2122f2013-06-24 19:03:14 +02003365 const char *sender;
Rodrigo Dias Corread596ca82020-11-25 00:42:28 -03003366 unsigned char padbuf[48];
Andrzej Kurekeb342242019-01-29 09:14:33 -05003367#if defined(MBEDTLS_USE_PSA_CRYPTO)
3368 size_t hash_size;
Jaeden Amero34973232019-02-20 10:32:28 +00003369 psa_hash_operation_t sha384_psa = PSA_HASH_OPERATION_INIT;
Andrzej Kurekeb342242019-01-29 09:14:33 -05003370 psa_status_t status;
3371#else
3372 mbedtls_sha512_context sha512;
3373#endif
Paul Bakkerca4ab492012-04-18 14:23:57 +00003374
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003375 mbedtls_ssl_session *session = ssl->session_negotiate;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003376 if (!session) {
Paul Bakker48916f92012-09-16 19:57:18 +00003377 session = ssl->session;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003378 }
Paul Bakker48916f92012-09-16 19:57:18 +00003379
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003380 sender = (from == MBEDTLS_SSL_IS_CLIENT)
Andrzej Kurekeb342242019-01-29 09:14:33 -05003381 ? "client finished"
3382 : "server finished";
3383
3384#if defined(MBEDTLS_USE_PSA_CRYPTO)
Andrzej Kurek972fba52019-01-30 03:29:12 -05003385 sha384_psa = psa_hash_operation_init();
Andrzej Kurekeb342242019-01-29 09:14:33 -05003386
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003387 MBEDTLS_SSL_DEBUG_MSG(2, ("=> calc PSA finished tls sha384"));
Andrzej Kurekeb342242019-01-29 09:14:33 -05003388
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003389 status = psa_hash_clone(&ssl->handshake->fin_sha384_psa, &sha384_psa);
3390 if (status != PSA_SUCCESS) {
3391 MBEDTLS_SSL_DEBUG_MSG(2, ("PSA hash clone failed"));
Andrzej Kurekeb342242019-01-29 09:14:33 -05003392 return;
3393 }
3394
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003395 status = psa_hash_finish(&sha384_psa, padbuf, sizeof(padbuf), &hash_size);
3396 if (status != PSA_SUCCESS) {
3397 MBEDTLS_SSL_DEBUG_MSG(2, ("PSA hash finish failed"));
Andrzej Kurekeb342242019-01-29 09:14:33 -05003398 return;
3399 }
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003400 MBEDTLS_SSL_DEBUG_BUF(3, "PSA calculated padbuf", padbuf, 48);
Andrzej Kurekeb342242019-01-29 09:14:33 -05003401#else
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003402 mbedtls_sha512_init(&sha512);
Manuel Pégourié-Gonnard001f2b62015-07-06 16:21:13 +02003403
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003404 MBEDTLS_SSL_DEBUG_MSG(2, ("=> calc finished tls sha384"));
Paul Bakkerca4ab492012-04-18 14:23:57 +00003405
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003406 mbedtls_sha512_clone(&sha512, &ssl->handshake->fin_sha512);
Paul Bakkerca4ab492012-04-18 14:23:57 +00003407
3408 /*
3409 * TLSv1.2:
3410 * hash = PRF( master, finished_label,
3411 * Hash( handshake ) )[0.11]
3412 */
3413
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003414#if !defined(MBEDTLS_SHA512_ALT)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003415 MBEDTLS_SSL_DEBUG_BUF(4, "finished sha512 state", (unsigned char *)
3416 sha512.state, sizeof(sha512.state));
Paul Bakker90995b52013-06-24 19:20:35 +02003417#endif
Gilles Peskinebb66dac2021-05-13 00:00:45 +02003418 /* mbedtls_sha512_finish_ret's output parameter is declared as a
Tom Cosgrove49f99bc2022-12-04 16:44:21 +00003419 * 64-byte buffer, but since we're using SHA-384, we know that the
Gilles Peskinebb66dac2021-05-13 00:00:45 +02003420 * output fits in 48 bytes. This is correct C, but GCC 11.1 warns
3421 * about it.
Rodrigo Dias Corread596ca82020-11-25 00:42:28 -03003422 */
Gilles Peskinebb66dac2021-05-13 00:00:45 +02003423#if defined(__GNUC__) && __GNUC__ >= 11
3424#pragma GCC diagnostic push
3425#pragma GCC diagnostic ignored "-Wstringop-overflow"
3426#endif
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003427 mbedtls_sha512_finish_ret(&sha512, padbuf);
Gilles Peskinebb66dac2021-05-13 00:00:45 +02003428#if defined(__GNUC__) && __GNUC__ >= 11
3429#pragma GCC diagnostic pop
3430#endif
Paul Bakkerca4ab492012-04-18 14:23:57 +00003431
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003432 mbedtls_sha512_free(&sha512);
Andrzej Kurekeb342242019-01-29 09:14:33 -05003433#endif
Paul Bakkerca4ab492012-04-18 14:23:57 +00003434
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003435 ssl->handshake->tls_prf(session->master, 48, sender,
3436 padbuf, 48, buf, len);
Paul Bakkerca4ab492012-04-18 14:23:57 +00003437
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003438 MBEDTLS_SSL_DEBUG_BUF(3, "calc finished result", buf, len);
Paul Bakkerca4ab492012-04-18 14:23:57 +00003439
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003440 mbedtls_platform_zeroize(padbuf, sizeof(padbuf));
Paul Bakkerca4ab492012-04-18 14:23:57 +00003441
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003442 MBEDTLS_SSL_DEBUG_MSG(2, ("<= calc finished"));
Paul Bakkerca4ab492012-04-18 14:23:57 +00003443}
Gilles Peskined2d59372021-05-12 22:43:27 +02003444#endif /* MBEDTLS_SHA512_C && !MBEDTLS_SHA512_NO_SHA384 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003445#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakkerca4ab492012-04-18 14:23:57 +00003446
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003447void mbedtls_ssl_handshake_wrapup_free_hs_transform(mbedtls_ssl_context *ssl)
Paul Bakker48916f92012-09-16 19:57:18 +00003448{
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003449 MBEDTLS_SSL_DEBUG_MSG(3, ("=> handshake wrapup: final free"));
Paul Bakker48916f92012-09-16 19:57:18 +00003450
3451 /*
3452 * Free our handshake params
3453 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003454 mbedtls_ssl_handshake_free(ssl);
3455 mbedtls_free(ssl->handshake);
Paul Bakker48916f92012-09-16 19:57:18 +00003456 ssl->handshake = NULL;
3457
3458 /*
Shaun Case0e7791f2021-12-20 21:14:10 -08003459 * Free the previous transform and switch in the current one
Paul Bakker48916f92012-09-16 19:57:18 +00003460 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003461 if (ssl->transform) {
3462 mbedtls_ssl_transform_free(ssl->transform);
3463 mbedtls_free(ssl->transform);
Paul Bakker48916f92012-09-16 19:57:18 +00003464 }
3465 ssl->transform = ssl->transform_negotiate;
3466 ssl->transform_negotiate = NULL;
3467
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003468 MBEDTLS_SSL_DEBUG_MSG(3, ("<= handshake wrapup: final free"));
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02003469}
3470
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003471void mbedtls_ssl_handshake_wrapup(mbedtls_ssl_context *ssl)
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02003472{
3473 int resume = ssl->handshake->resume;
3474
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003475 MBEDTLS_SSL_DEBUG_MSG(3, ("=> handshake wrapup"));
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02003476
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003477#if defined(MBEDTLS_SSL_RENEGOTIATION)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003478 if (ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS) {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003479 ssl->renego_status = MBEDTLS_SSL_RENEGOTIATION_DONE;
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02003480 ssl->renego_records_seen = 0;
3481 }
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01003482#endif
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02003483
3484 /*
3485 * Free the previous session and switch in the current one
3486 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003487 if (ssl->session) {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003488#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
Manuel Pégourié-Gonnard1a034732014-11-04 17:36:18 +01003489 /* RFC 7366 3.1: keep the EtM state */
3490 ssl->session_negotiate->encrypt_then_mac =
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003491 ssl->session->encrypt_then_mac;
Manuel Pégourié-Gonnard1a034732014-11-04 17:36:18 +01003492#endif
3493
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003494 mbedtls_ssl_session_free(ssl->session);
3495 mbedtls_free(ssl->session);
Paul Bakker0a597072012-09-25 21:55:46 +00003496 }
3497 ssl->session = ssl->session_negotiate;
Paul Bakker48916f92012-09-16 19:57:18 +00003498 ssl->session_negotiate = NULL;
3499
Paul Bakker0a597072012-09-25 21:55:46 +00003500 /*
3501 * Add cache entry
3502 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003503 if (ssl->conf->f_set_cache != NULL &&
Manuel Pégourié-Gonnard12ad7982015-06-18 15:50:37 +02003504 ssl->session->id_len != 0 &&
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003505 resume == 0) {
3506 if (ssl->conf->f_set_cache(ssl->conf->p_cache, ssl->session) != 0) {
3507 MBEDTLS_SSL_DEBUG_MSG(1, ("cache did not store session"));
3508 }
Manuel Pégourié-Gonnardc086cce2013-08-02 14:13:02 +02003509 }
Paul Bakker0a597072012-09-25 21:55:46 +00003510
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003511#if defined(MBEDTLS_SSL_PROTO_DTLS)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003512 if (ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
3513 ssl->handshake->flight != NULL) {
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02003514 /* Cancel handshake timer */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003515 mbedtls_ssl_set_timer(ssl, 0);
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02003516
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02003517 /* Keep last flight around in case we need to resend it:
3518 * we need the handshake and transform structures for that */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003519 MBEDTLS_SSL_DEBUG_MSG(3, ("skip freeing handshake and transform"));
3520 } else
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02003521#endif
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003522 mbedtls_ssl_handshake_wrapup_free_hs_transform(ssl);
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02003523
Paul Bakker48916f92012-09-16 19:57:18 +00003524 ssl->state++;
3525
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003526 MBEDTLS_SSL_DEBUG_MSG(3, ("<= handshake wrapup"));
Paul Bakker48916f92012-09-16 19:57:18 +00003527}
3528
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003529int mbedtls_ssl_write_finished(mbedtls_ssl_context *ssl)
Paul Bakker1ef83d62012-04-11 12:09:53 +00003530{
Manuel Pégourié-Gonnard879a4f92014-07-11 22:31:12 +02003531 int ret, hash_len;
Paul Bakker1ef83d62012-04-11 12:09:53 +00003532
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003533 MBEDTLS_SSL_DEBUG_MSG(2, ("=> write finished"));
Paul Bakker1ef83d62012-04-11 12:09:53 +00003534
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003535 mbedtls_ssl_update_out_pointers(ssl, ssl->transform_negotiate);
Paul Bakker92be97b2013-01-02 17:30:03 +01003536
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003537 ssl->handshake->calc_finished(ssl, ssl->out_msg + 4, ssl->conf->endpoint);
Paul Bakker1ef83d62012-04-11 12:09:53 +00003538
Manuel Pégourié-Gonnard214a8482016-02-22 11:27:26 +01003539 /*
3540 * RFC 5246 7.4.9 (Page 63) says 12 is the default length and ciphersuites
3541 * may define some other value. Currently (early 2016), no defined
3542 * ciphersuite does this (and this is unlikely to change as activity has
3543 * moved to TLS 1.3 now) so we can keep the hardcoded 12 here.
3544 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003545 hash_len = (ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0) ? 36 : 12;
Paul Bakker5121ce52009-01-03 21:22:43 +00003546
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003547#if defined(MBEDTLS_SSL_RENEGOTIATION)
Paul Bakker48916f92012-09-16 19:57:18 +00003548 ssl->verify_data_len = hash_len;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003549 memcpy(ssl->own_verify_data, ssl->out_msg + 4, hash_len);
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01003550#endif
Paul Bakker48916f92012-09-16 19:57:18 +00003551
Paul Bakker5121ce52009-01-03 21:22:43 +00003552 ssl->out_msglen = 4 + hash_len;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003553 ssl->out_msgtype = MBEDTLS_SSL_MSG_HANDSHAKE;
3554 ssl->out_msg[0] = MBEDTLS_SSL_HS_FINISHED;
Paul Bakker5121ce52009-01-03 21:22:43 +00003555
3556 /*
3557 * In case of session resuming, invert the client and server
3558 * ChangeCipherSpec messages order.
3559 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003560 if (ssl->handshake->resume != 0) {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003561#if defined(MBEDTLS_SSL_CLI_C)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003562 if (ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT) {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003563 ssl->state = MBEDTLS_SSL_HANDSHAKE_WRAPUP;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003564 }
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01003565#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003566#if defined(MBEDTLS_SSL_SRV_C)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003567 if (ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER) {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003568 ssl->state = MBEDTLS_SSL_CLIENT_CHANGE_CIPHER_SPEC;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003569 }
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01003570#endif
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003571 } else {
Paul Bakker5121ce52009-01-03 21:22:43 +00003572 ssl->state++;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003573 }
Paul Bakker5121ce52009-01-03 21:22:43 +00003574
Paul Bakker48916f92012-09-16 19:57:18 +00003575 /*
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02003576 * Switch to our negotiated transform and session parameters for outbound
3577 * data.
Paul Bakker48916f92012-09-16 19:57:18 +00003578 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003579 MBEDTLS_SSL_DEBUG_MSG(3, ("switching to new transform spec for outbound data"));
Manuel Pégourié-Gonnard5afb1672014-02-16 18:33:22 +01003580
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003581#if defined(MBEDTLS_SSL_PROTO_DTLS)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003582 if (ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM) {
Manuel Pégourié-Gonnard879a4f92014-07-11 22:31:12 +02003583 unsigned char i;
3584
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003585 /* Remember current epoch settings for resending */
3586 ssl->handshake->alt_transform_out = ssl->transform_out;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003587 memcpy(ssl->handshake->alt_out_ctr, ssl->cur_out_ctr, 8);
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003588
Manuel Pégourié-Gonnard879a4f92014-07-11 22:31:12 +02003589 /* Set sequence_number to zero */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003590 memset(ssl->cur_out_ctr + 2, 0, 6);
Manuel Pégourié-Gonnard879a4f92014-07-11 22:31:12 +02003591
3592 /* Increment epoch */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003593 for (i = 2; i > 0; i--) {
3594 if (++ssl->cur_out_ctr[i - 1] != 0) {
Manuel Pégourié-Gonnard879a4f92014-07-11 22:31:12 +02003595 break;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003596 }
3597 }
Manuel Pégourié-Gonnard879a4f92014-07-11 22:31:12 +02003598
3599 /* The loop goes to its end iff the counter is wrapping */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003600 if (i == 0) {
3601 MBEDTLS_SSL_DEBUG_MSG(1, ("DTLS epoch would wrap"));
3602 return MBEDTLS_ERR_SSL_COUNTER_WRAPPING;
Manuel Pégourié-Gonnard879a4f92014-07-11 22:31:12 +02003603 }
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003604 } else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003605#endif /* MBEDTLS_SSL_PROTO_DTLS */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003606 memset(ssl->cur_out_ctr, 0, 8);
Paul Bakker5121ce52009-01-03 21:22:43 +00003607
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003608 ssl->transform_out = ssl->transform_negotiate;
3609 ssl->session_out = ssl->session_negotiate;
3610
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003611#if defined(MBEDTLS_SSL_HW_RECORD_ACCEL)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003612 if (mbedtls_ssl_hw_record_activate != NULL) {
3613 if ((ret = mbedtls_ssl_hw_record_activate(ssl, MBEDTLS_SSL_CHANNEL_OUTBOUND)) != 0) {
3614 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ssl_hw_record_activate", ret);
3615 return MBEDTLS_ERR_SSL_HW_ACCEL_FAILED;
Paul Bakker07eb38b2012-12-19 14:42:06 +01003616 }
3617 }
3618#endif
3619
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003620#if defined(MBEDTLS_SSL_PROTO_DTLS)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003621 if (ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM) {
3622 mbedtls_ssl_send_flight_completed(ssl);
3623 }
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02003624#endif
3625
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003626 if ((ret = mbedtls_ssl_write_handshake_msg(ssl)) != 0) {
3627 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ssl_write_handshake_msg", ret);
3628 return ret;
Paul Bakker5121ce52009-01-03 21:22:43 +00003629 }
3630
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02003631#if defined(MBEDTLS_SSL_PROTO_DTLS)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003632 if (ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
3633 (ret = mbedtls_ssl_flight_transmit(ssl)) != 0) {
3634 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ssl_flight_transmit", ret);
3635 return ret;
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02003636 }
3637#endif
3638
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003639 MBEDTLS_SSL_DEBUG_MSG(2, ("<= write finished"));
Paul Bakker5121ce52009-01-03 21:22:43 +00003640
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003641 return 0;
Paul Bakker5121ce52009-01-03 21:22:43 +00003642}
3643
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003644#if defined(MBEDTLS_SSL_PROTO_SSL3)
Manuel Pégourié-Gonnardca6440b2014-09-10 12:39:54 +00003645#define SSL_MAX_HASH_LEN 36
3646#else
3647#define SSL_MAX_HASH_LEN 12
3648#endif
3649
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003650int mbedtls_ssl_parse_finished(mbedtls_ssl_context *ssl)
Paul Bakker5121ce52009-01-03 21:22:43 +00003651{
Janos Follath865b3eb2019-12-16 11:46:15 +00003652 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnard879a4f92014-07-11 22:31:12 +02003653 unsigned int hash_len;
Manuel Pégourié-Gonnardca6440b2014-09-10 12:39:54 +00003654 unsigned char buf[SSL_MAX_HASH_LEN];
Paul Bakker5121ce52009-01-03 21:22:43 +00003655
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003656 MBEDTLS_SSL_DEBUG_MSG(2, ("=> parse finished"));
Paul Bakker5121ce52009-01-03 21:22:43 +00003657
Gilles Peskine622d8042021-12-13 14:38:40 +01003658 /* There is currently no ciphersuite using another length with TLS 1.2 */
3659#if defined(MBEDTLS_SSL_PROTO_SSL3)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003660 if (ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0) {
Gilles Peskine622d8042021-12-13 14:38:40 +01003661 hash_len = 36;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003662 } else
Gilles Peskine622d8042021-12-13 14:38:40 +01003663#endif
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003664 hash_len = 12;
Gilles Peskine622d8042021-12-13 14:38:40 +01003665
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003666 ssl->handshake->calc_finished(ssl, buf, ssl->conf->endpoint ^ 1);
Paul Bakker5121ce52009-01-03 21:22:43 +00003667
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003668 if ((ret = mbedtls_ssl_read_record(ssl, 1)) != 0) {
3669 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ssl_read_record", ret);
Gilles Peskinef91b2e52021-12-13 12:36:15 +01003670 goto exit;
Paul Bakker5121ce52009-01-03 21:22:43 +00003671 }
3672
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003673 if (ssl->in_msgtype != MBEDTLS_SSL_MSG_HANDSHAKE) {
3674 MBEDTLS_SSL_DEBUG_MSG(1, ("bad finished message"));
3675 mbedtls_ssl_send_alert_message(ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
3676 MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE);
Gilles Peskinef91b2e52021-12-13 12:36:15 +01003677 ret = MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE;
3678 goto exit;
Paul Bakker5121ce52009-01-03 21:22:43 +00003679 }
3680
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003681 if (ssl->in_msg[0] != MBEDTLS_SSL_HS_FINISHED ||
3682 ssl->in_hslen != mbedtls_ssl_hs_hdr_len(ssl) + hash_len) {
3683 MBEDTLS_SSL_DEBUG_MSG(1, ("bad finished message"));
3684 mbedtls_ssl_send_alert_message(ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
3685 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR);
Gilles Peskinef91b2e52021-12-13 12:36:15 +01003686 ret = MBEDTLS_ERR_SSL_BAD_HS_FINISHED;
3687 goto exit;
Paul Bakker5121ce52009-01-03 21:22:43 +00003688 }
3689
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003690 if (mbedtls_ct_memcmp(ssl->in_msg + mbedtls_ssl_hs_hdr_len(ssl),
3691 buf, hash_len) != 0) {
3692 MBEDTLS_SSL_DEBUG_MSG(1, ("bad finished message"));
3693 mbedtls_ssl_send_alert_message(ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
3694 MBEDTLS_SSL_ALERT_MSG_DECRYPT_ERROR);
Gilles Peskinef91b2e52021-12-13 12:36:15 +01003695 ret = MBEDTLS_ERR_SSL_BAD_HS_FINISHED;
3696 goto exit;
Paul Bakker5121ce52009-01-03 21:22:43 +00003697 }
3698
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003699#if defined(MBEDTLS_SSL_RENEGOTIATION)
Paul Bakker48916f92012-09-16 19:57:18 +00003700 ssl->verify_data_len = hash_len;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003701 memcpy(ssl->peer_verify_data, buf, hash_len);
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01003702#endif
Paul Bakker48916f92012-09-16 19:57:18 +00003703
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003704 if (ssl->handshake->resume != 0) {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003705#if defined(MBEDTLS_SSL_CLI_C)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003706 if (ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT) {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003707 ssl->state = MBEDTLS_SSL_CLIENT_CHANGE_CIPHER_SPEC;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003708 }
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01003709#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003710#if defined(MBEDTLS_SSL_SRV_C)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003711 if (ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER) {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003712 ssl->state = MBEDTLS_SSL_HANDSHAKE_WRAPUP;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003713 }
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01003714#endif
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003715 } else {
Paul Bakker5121ce52009-01-03 21:22:43 +00003716 ssl->state++;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003717 }
Paul Bakker5121ce52009-01-03 21:22:43 +00003718
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003719#if defined(MBEDTLS_SSL_PROTO_DTLS)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003720 if (ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM) {
3721 mbedtls_ssl_recv_flight_completed(ssl);
3722 }
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003723#endif
3724
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003725 MBEDTLS_SSL_DEBUG_MSG(2, ("<= parse finished"));
Paul Bakker5121ce52009-01-03 21:22:43 +00003726
Gilles Peskinef91b2e52021-12-13 12:36:15 +01003727exit:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003728 mbedtls_platform_zeroize(buf, hash_len);
3729 return ret;
Paul Bakker5121ce52009-01-03 21:22:43 +00003730}
3731
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003732static void ssl_handshake_params_init(mbedtls_ssl_handshake_params *handshake)
Paul Bakkeraccaffe2014-06-26 13:37:14 +02003733{
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003734 memset(handshake, 0, sizeof(mbedtls_ssl_handshake_params));
Paul Bakkeraccaffe2014-06-26 13:37:14 +02003735
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003736#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1) || \
3737 defined(MBEDTLS_SSL_PROTO_TLS1_1)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003738 mbedtls_md5_init(&handshake->fin_md5);
3739 mbedtls_sha1_init(&handshake->fin_sha1);
3740 mbedtls_md5_starts_ret(&handshake->fin_md5);
3741 mbedtls_sha1_starts_ret(&handshake->fin_sha1);
Paul Bakkeraccaffe2014-06-26 13:37:14 +02003742#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003743#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
3744#if defined(MBEDTLS_SHA256_C)
Andrzej Kurekeb342242019-01-29 09:14:33 -05003745#if defined(MBEDTLS_USE_PSA_CRYPTO)
3746 handshake->fin_sha256_psa = psa_hash_operation_init();
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003747 psa_hash_setup(&handshake->fin_sha256_psa, PSA_ALG_SHA_256);
Andrzej Kurekeb342242019-01-29 09:14:33 -05003748#else
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003749 mbedtls_sha256_init(&handshake->fin_sha256);
3750 mbedtls_sha256_starts_ret(&handshake->fin_sha256, 0);
Paul Bakkeraccaffe2014-06-26 13:37:14 +02003751#endif
Andrzej Kurekeb342242019-01-29 09:14:33 -05003752#endif
Gilles Peskined2d59372021-05-12 22:43:27 +02003753#if defined(MBEDTLS_SHA512_C) && !defined(MBEDTLS_SHA512_NO_SHA384)
Andrzej Kurekeb342242019-01-29 09:14:33 -05003754#if defined(MBEDTLS_USE_PSA_CRYPTO)
Andrzej Kurek972fba52019-01-30 03:29:12 -05003755 handshake->fin_sha384_psa = psa_hash_operation_init();
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003756 psa_hash_setup(&handshake->fin_sha384_psa, PSA_ALG_SHA_384);
Andrzej Kurekeb342242019-01-29 09:14:33 -05003757#else
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003758 mbedtls_sha512_init(&handshake->fin_sha512);
3759 mbedtls_sha512_starts_ret(&handshake->fin_sha512, 1);
Paul Bakkeraccaffe2014-06-26 13:37:14 +02003760#endif
Andrzej Kurekeb342242019-01-29 09:14:33 -05003761#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003762#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakkeraccaffe2014-06-26 13:37:14 +02003763
3764 handshake->update_checksum = ssl_update_checksum_start;
Hanno Becker7e5437a2017-04-28 17:15:26 +01003765
3766#if defined(MBEDTLS_SSL_PROTO_TLS1_2) && \
Gilles Peskineeccd8882020-03-10 12:19:08 +01003767 defined(MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003768 mbedtls_ssl_sig_hash_set_init(&handshake->hash_algs);
Hanno Becker7e5437a2017-04-28 17:15:26 +01003769#endif
Paul Bakkeraccaffe2014-06-26 13:37:14 +02003770
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003771#if defined(MBEDTLS_DHM_C)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003772 mbedtls_dhm_init(&handshake->dhm_ctx);
Paul Bakkeraccaffe2014-06-26 13:37:14 +02003773#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003774#if defined(MBEDTLS_ECDH_C)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003775 mbedtls_ecdh_init(&handshake->ecdh_ctx);
Paul Bakkeraccaffe2014-06-26 13:37:14 +02003776#endif
Manuel Pégourié-Gonnardeef142d2015-09-16 10:05:04 +02003777#if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003778 mbedtls_ecjpake_init(&handshake->ecjpake_ctx);
Manuel Pégourié-Gonnard77c06462015-09-17 13:59:49 +02003779#if defined(MBEDTLS_SSL_CLI_C)
3780 handshake->ecjpake_cache = NULL;
3781 handshake->ecjpake_cache_len = 0;
3782#endif
Manuel Pégourié-Gonnard76cfd3f2015-09-15 12:10:54 +02003783#endif
Manuel Pégourié-Gonnardcdc26ae2015-06-19 12:16:31 +02003784
Gilles Peskineeccd8882020-03-10 12:19:08 +01003785#if defined(MBEDTLS_SSL_ECP_RESTARTABLE_ENABLED)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003786 mbedtls_x509_crt_restart_init(&handshake->ecrs_ctx);
Manuel Pégourié-Gonnard862cde52017-05-17 11:56:15 +02003787#endif
3788
Manuel Pégourié-Gonnardcdc26ae2015-06-19 12:16:31 +02003789#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
3790 handshake->sni_authmode = MBEDTLS_SSL_VERIFY_UNSET;
3791#endif
Hanno Becker75173122019-02-06 16:18:31 +00003792
3793#if defined(MBEDTLS_X509_CRT_PARSE_C) && \
3794 !defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003795 mbedtls_pk_init(&handshake->peer_pubkey);
Hanno Becker75173122019-02-06 16:18:31 +00003796#endif
Paul Bakkeraccaffe2014-06-26 13:37:14 +02003797}
3798
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003799void mbedtls_ssl_transform_init(mbedtls_ssl_transform *transform)
Paul Bakkeraccaffe2014-06-26 13:37:14 +02003800{
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003801 memset(transform, 0, sizeof(mbedtls_ssl_transform));
Paul Bakker84bbeb52014-07-01 14:53:22 +02003802
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003803 mbedtls_cipher_init(&transform->cipher_ctx_enc);
3804 mbedtls_cipher_init(&transform->cipher_ctx_dec);
Paul Bakker84bbeb52014-07-01 14:53:22 +02003805
Hanno Beckerd56ed242018-01-03 15:32:51 +00003806#if defined(MBEDTLS_SSL_SOME_MODES_USE_MAC)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003807 mbedtls_md_init(&transform->md_ctx_enc);
3808 mbedtls_md_init(&transform->md_ctx_dec);
Hanno Beckerd56ed242018-01-03 15:32:51 +00003809#endif
Paul Bakkeraccaffe2014-06-26 13:37:14 +02003810}
3811
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003812void mbedtls_ssl_session_init(mbedtls_ssl_session *session)
Paul Bakkeraccaffe2014-06-26 13:37:14 +02003813{
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003814 memset(session, 0, sizeof(mbedtls_ssl_session));
Paul Bakkeraccaffe2014-06-26 13:37:14 +02003815}
3816
Manuel Pégourié-Gonnardd904d662022-06-17 10:24:00 +02003817MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003818static int ssl_handshake_init(mbedtls_ssl_context *ssl)
Paul Bakker48916f92012-09-16 19:57:18 +00003819{
Paul Bakkeraccaffe2014-06-26 13:37:14 +02003820 /* Clear old handshake information if present */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003821 if (ssl->transform_negotiate) {
3822 mbedtls_ssl_transform_free(ssl->transform_negotiate);
3823 }
3824 if (ssl->session_negotiate) {
3825 mbedtls_ssl_session_free(ssl->session_negotiate);
3826 }
3827 if (ssl->handshake) {
3828 mbedtls_ssl_handshake_free(ssl);
3829 }
Paul Bakkeraccaffe2014-06-26 13:37:14 +02003830
3831 /*
3832 * Either the pointers are now NULL or cleared properly and can be freed.
3833 * Now allocate missing structures.
3834 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003835 if (ssl->transform_negotiate == NULL) {
3836 ssl->transform_negotiate = mbedtls_calloc(1, sizeof(mbedtls_ssl_transform));
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02003837 }
Paul Bakker48916f92012-09-16 19:57:18 +00003838
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003839 if (ssl->session_negotiate == NULL) {
3840 ssl->session_negotiate = mbedtls_calloc(1, sizeof(mbedtls_ssl_session));
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02003841 }
Paul Bakker48916f92012-09-16 19:57:18 +00003842
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003843 if (ssl->handshake == NULL) {
3844 ssl->handshake = mbedtls_calloc(1, sizeof(mbedtls_ssl_handshake_params));
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02003845 }
Andrzej Kurek0afa2a12020-03-03 10:39:58 -05003846#if defined(MBEDTLS_SSL_VARIABLE_BUFFER_LENGTH)
3847 /* If the buffers are too small - reallocate */
Andrzej Kurek8ea68722020-04-03 06:40:47 -04003848
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003849 handle_buffer_resizing(ssl, 0, MBEDTLS_SSL_IN_BUFFER_LEN,
3850 MBEDTLS_SSL_OUT_BUFFER_LEN);
Andrzej Kurek0afa2a12020-03-03 10:39:58 -05003851#endif
Paul Bakker48916f92012-09-16 19:57:18 +00003852
Paul Bakkeraccaffe2014-06-26 13:37:14 +02003853 /* All pointers should exist and can be directly freed without issue */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003854 if (ssl->handshake == NULL ||
Paul Bakker48916f92012-09-16 19:57:18 +00003855 ssl->transform_negotiate == NULL ||
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003856 ssl->session_negotiate == NULL) {
3857 MBEDTLS_SSL_DEBUG_MSG(1, ("alloc() of ssl sub-contexts failed"));
Paul Bakkeraccaffe2014-06-26 13:37:14 +02003858
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003859 mbedtls_free(ssl->handshake);
3860 mbedtls_free(ssl->transform_negotiate);
3861 mbedtls_free(ssl->session_negotiate);
Paul Bakkeraccaffe2014-06-26 13:37:14 +02003862
3863 ssl->handshake = NULL;
3864 ssl->transform_negotiate = NULL;
3865 ssl->session_negotiate = NULL;
3866
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003867 return MBEDTLS_ERR_SSL_ALLOC_FAILED;
Paul Bakker48916f92012-09-16 19:57:18 +00003868 }
3869
Paul Bakkeraccaffe2014-06-26 13:37:14 +02003870 /* Initialize structures */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003871 mbedtls_ssl_session_init(ssl->session_negotiate);
3872 mbedtls_ssl_transform_init(ssl->transform_negotiate);
3873 ssl_handshake_params_init(ssl->handshake);
Paul Bakker968afaa2014-07-09 11:09:24 +02003874
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003875#if defined(MBEDTLS_SSL_PROTO_DTLS)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003876 if (ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM) {
Manuel Pégourié-Gonnard06939ce2015-05-11 11:25:46 +02003877 ssl->handshake->alt_transform_out = ssl->transform_out;
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003878
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003879 if (ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT) {
Manuel Pégourié-Gonnard06939ce2015-05-11 11:25:46 +02003880 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_PREPARING;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003881 } else {
Manuel Pégourié-Gonnard06939ce2015-05-11 11:25:46 +02003882 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_WAITING;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003883 }
Manuel Pégourié-Gonnard286a1362015-05-13 16:22:05 +02003884
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003885 mbedtls_ssl_set_timer(ssl, 0);
Manuel Pégourié-Gonnard06939ce2015-05-11 11:25:46 +02003886 }
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003887#endif
3888
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003889 return 0;
Paul Bakker48916f92012-09-16 19:57:18 +00003890}
3891
Manuel Pégourié-Gonnarde057d3b2015-05-20 10:59:43 +02003892#if defined(MBEDTLS_SSL_DTLS_HELLO_VERIFY) && defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard7d38d212014-07-23 17:52:09 +02003893/* Dummy cookie callbacks for defaults */
Manuel Pégourié-Gonnardd904d662022-06-17 10:24:00 +02003894MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003895static int ssl_cookie_write_dummy(void *ctx,
3896 unsigned char **p, unsigned char *end,
3897 const unsigned char *cli_id, size_t cli_id_len)
Manuel Pégourié-Gonnard7d38d212014-07-23 17:52:09 +02003898{
3899 ((void) ctx);
3900 ((void) p);
3901 ((void) end);
3902 ((void) cli_id);
3903 ((void) cli_id_len);
3904
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003905 return MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
Manuel Pégourié-Gonnard7d38d212014-07-23 17:52:09 +02003906}
3907
Manuel Pégourié-Gonnardd904d662022-06-17 10:24:00 +02003908MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003909static int ssl_cookie_check_dummy(void *ctx,
3910 const unsigned char *cookie, size_t cookie_len,
3911 const unsigned char *cli_id, size_t cli_id_len)
Manuel Pégourié-Gonnard7d38d212014-07-23 17:52:09 +02003912{
3913 ((void) ctx);
3914 ((void) cookie);
3915 ((void) cookie_len);
3916 ((void) cli_id);
3917 ((void) cli_id_len);
3918
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003919 return MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
Manuel Pégourié-Gonnard7d38d212014-07-23 17:52:09 +02003920}
Manuel Pégourié-Gonnarde057d3b2015-05-20 10:59:43 +02003921#endif /* MBEDTLS_SSL_DTLS_HELLO_VERIFY && MBEDTLS_SSL_SRV_C */
Manuel Pégourié-Gonnard7d38d212014-07-23 17:52:09 +02003922
Paul Bakker5121ce52009-01-03 21:22:43 +00003923/*
3924 * Initialize an SSL context
3925 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003926void mbedtls_ssl_init(mbedtls_ssl_context *ssl)
Manuel Pégourié-Gonnard41d479e2015-04-29 00:48:22 +02003927{
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003928 memset(ssl, 0, sizeof(mbedtls_ssl_context));
Manuel Pégourié-Gonnard41d479e2015-04-29 00:48:22 +02003929}
3930
3931/*
3932 * Setup an SSL context
3933 */
Hanno Becker2a43f6f2018-08-10 11:12:52 +01003934
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003935int mbedtls_ssl_setup(mbedtls_ssl_context *ssl,
3936 const mbedtls_ssl_config *conf)
Paul Bakker5121ce52009-01-03 21:22:43 +00003937{
Janos Follath865b3eb2019-12-16 11:46:15 +00003938 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Darryl Greenb33cc762019-11-28 14:29:44 +00003939 size_t in_buf_len = MBEDTLS_SSL_IN_BUFFER_LEN;
3940 size_t out_buf_len = MBEDTLS_SSL_OUT_BUFFER_LEN;
Paul Bakker5121ce52009-01-03 21:22:43 +00003941
Manuel Pégourié-Gonnarddef0bbe2015-05-04 14:56:36 +02003942 ssl->conf = conf;
Paul Bakker62f2dee2012-09-28 07:31:51 +00003943
3944 /*
Manuel Pégourié-Gonnard06193482014-02-14 08:39:32 +01003945 * Prepare base structures
Paul Bakker62f2dee2012-09-28 07:31:51 +00003946 */
k-stachowiakc9a5f022018-07-24 13:53:31 +02003947
3948 /* Set to NULL in case of an error condition */
3949 ssl->out_buf = NULL;
k-stachowiaka47911c2018-07-04 17:41:58 +02003950
Darryl Greenb33cc762019-11-28 14:29:44 +00003951#if defined(MBEDTLS_SSL_VARIABLE_BUFFER_LENGTH)
3952 ssl->in_buf_len = in_buf_len;
3953#endif
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003954 ssl->in_buf = mbedtls_calloc(1, in_buf_len);
3955 if (ssl->in_buf == NULL) {
3956 MBEDTLS_SSL_DEBUG_MSG(1, ("alloc(%" MBEDTLS_PRINTF_SIZET " bytes) failed", in_buf_len));
k-stachowiak9f7798e2018-07-31 16:52:32 +02003957 ret = MBEDTLS_ERR_SSL_ALLOC_FAILED;
k-stachowiaka47911c2018-07-04 17:41:58 +02003958 goto error;
Angus Grattond8213d02016-05-25 20:56:48 +10003959 }
3960
Darryl Greenb33cc762019-11-28 14:29:44 +00003961#if defined(MBEDTLS_SSL_VARIABLE_BUFFER_LENGTH)
3962 ssl->out_buf_len = out_buf_len;
3963#endif
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003964 ssl->out_buf = mbedtls_calloc(1, out_buf_len);
3965 if (ssl->out_buf == NULL) {
3966 MBEDTLS_SSL_DEBUG_MSG(1, ("alloc(%" MBEDTLS_PRINTF_SIZET " bytes) failed", out_buf_len));
k-stachowiak9f7798e2018-07-31 16:52:32 +02003967 ret = MBEDTLS_ERR_SSL_ALLOC_FAILED;
k-stachowiaka47911c2018-07-04 17:41:58 +02003968 goto error;
Paul Bakker5121ce52009-01-03 21:22:43 +00003969 }
3970
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003971 mbedtls_ssl_reset_in_out_pointers(ssl);
Manuel Pégourié-Gonnard419d5ae2015-05-04 19:32:36 +02003972
Johan Pascalb62bb512015-12-03 21:56:45 +01003973#if defined(MBEDTLS_SSL_DTLS_SRTP)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003974 memset(&ssl->dtls_srtp_info, 0, sizeof(ssl->dtls_srtp_info));
Johan Pascalb62bb512015-12-03 21:56:45 +01003975#endif
3976
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003977 if ((ret = ssl_handshake_init(ssl)) != 0) {
k-stachowiaka47911c2018-07-04 17:41:58 +02003978 goto error;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003979 }
Paul Bakker5121ce52009-01-03 21:22:43 +00003980
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003981 return 0;
k-stachowiaka47911c2018-07-04 17:41:58 +02003982
3983error:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003984 mbedtls_free(ssl->in_buf);
3985 mbedtls_free(ssl->out_buf);
k-stachowiaka47911c2018-07-04 17:41:58 +02003986
3987 ssl->conf = NULL;
3988
Darryl Greenb33cc762019-11-28 14:29:44 +00003989#if defined(MBEDTLS_SSL_VARIABLE_BUFFER_LENGTH)
3990 ssl->in_buf_len = 0;
3991 ssl->out_buf_len = 0;
3992#endif
k-stachowiaka47911c2018-07-04 17:41:58 +02003993 ssl->in_buf = NULL;
3994 ssl->out_buf = NULL;
3995
3996 ssl->in_hdr = NULL;
3997 ssl->in_ctr = NULL;
3998 ssl->in_len = NULL;
3999 ssl->in_iv = NULL;
4000 ssl->in_msg = NULL;
4001
4002 ssl->out_hdr = NULL;
4003 ssl->out_ctr = NULL;
4004 ssl->out_len = NULL;
4005 ssl->out_iv = NULL;
4006 ssl->out_msg = NULL;
4007
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004008 return ret;
Paul Bakker5121ce52009-01-03 21:22:43 +00004009}
4010
4011/*
Paul Bakker7eb013f2011-10-06 12:37:39 +00004012 * Reset an initialized and used SSL context for re-use while retaining
4013 * all application-set variables, function pointers and data.
Manuel Pégourié-Gonnard3f09b6d2015-09-08 11:58:14 +02004014 *
4015 * If partial is non-zero, keep data in the input buffer and client ID.
4016 * (Use when a DTLS client reconnects from the same port.)
Paul Bakker7eb013f2011-10-06 12:37:39 +00004017 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004018int mbedtls_ssl_session_reset_int(mbedtls_ssl_context *ssl, int partial)
Paul Bakker7eb013f2011-10-06 12:37:39 +00004019{
Janos Follath865b3eb2019-12-16 11:46:15 +00004020 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Darryl Greenb33cc762019-11-28 14:29:44 +00004021#if defined(MBEDTLS_SSL_VARIABLE_BUFFER_LENGTH)
4022 size_t in_buf_len = ssl->in_buf_len;
4023 size_t out_buf_len = ssl->out_buf_len;
4024#else
4025 size_t in_buf_len = MBEDTLS_SSL_IN_BUFFER_LEN;
4026 size_t out_buf_len = MBEDTLS_SSL_OUT_BUFFER_LEN;
4027#endif
Paul Bakker48916f92012-09-16 19:57:18 +00004028
Hanno Becker7e772132018-08-10 12:38:21 +01004029#if !defined(MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE) || \
4030 !defined(MBEDTLS_SSL_SRV_C)
4031 ((void) partial);
4032#endif
4033
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004034 ssl->state = MBEDTLS_SSL_HELLO_REQUEST;
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01004035
Manuel Pégourié-Gonnard286a1362015-05-13 16:22:05 +02004036 /* Cancel any possibly running timer */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004037 mbedtls_ssl_set_timer(ssl, 0);
Manuel Pégourié-Gonnard286a1362015-05-13 16:22:05 +02004038
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004039#if defined(MBEDTLS_SSL_RENEGOTIATION)
4040 ssl->renego_status = MBEDTLS_SSL_INITIAL_HANDSHAKE;
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01004041 ssl->renego_records_seen = 0;
Paul Bakker48916f92012-09-16 19:57:18 +00004042
4043 ssl->verify_data_len = 0;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004044 memset(ssl->own_verify_data, 0, MBEDTLS_SSL_VERIFY_DATA_MAX_LEN);
4045 memset(ssl->peer_verify_data, 0, MBEDTLS_SSL_VERIFY_DATA_MAX_LEN);
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01004046#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004047 ssl->secure_renegotiation = MBEDTLS_SSL_LEGACY_RENEGOTIATION;
Paul Bakker48916f92012-09-16 19:57:18 +00004048
Paul Bakker7eb013f2011-10-06 12:37:39 +00004049 ssl->in_offt = NULL;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004050 mbedtls_ssl_reset_in_out_pointers(ssl);
Paul Bakker7eb013f2011-10-06 12:37:39 +00004051
4052 ssl->in_msgtype = 0;
4053 ssl->in_msglen = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004054#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02004055 ssl->next_record_offset = 0;
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02004056 ssl->in_epoch = 0;
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02004057#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004058#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004059 mbedtls_ssl_dtls_replay_reset(ssl);
Manuel Pégourié-Gonnardb47368a2014-09-24 13:29:58 +02004060#endif
Paul Bakker7eb013f2011-10-06 12:37:39 +00004061
4062 ssl->in_hslen = 0;
4063 ssl->nb_zero = 0;
Hanno Beckeraf0665d2017-05-24 09:16:26 +01004064
4065 ssl->keep_current_message = 0;
Paul Bakker7eb013f2011-10-06 12:37:39 +00004066
4067 ssl->out_msgtype = 0;
4068 ssl->out_msglen = 0;
4069 ssl->out_left = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004070#if defined(MBEDTLS_SSL_CBC_RECORD_SPLITTING)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004071 if (ssl->split_done != MBEDTLS_SSL_CBC_RECORD_SPLITTING_DISABLED) {
Manuel Pégourié-Gonnardcfa477e2015-01-07 14:50:54 +01004072 ssl->split_done = 0;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004073 }
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +01004074#endif
Paul Bakker7eb013f2011-10-06 12:37:39 +00004075
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004076 memset(ssl->cur_out_ctr, 0, sizeof(ssl->cur_out_ctr));
Hanno Becker19859472018-08-06 09:40:20 +01004077
Paul Bakker48916f92012-09-16 19:57:18 +00004078 ssl->transform_in = NULL;
4079 ssl->transform_out = NULL;
Paul Bakker7eb013f2011-10-06 12:37:39 +00004080
Hanno Becker78640902018-08-13 16:35:15 +01004081 ssl->session_in = NULL;
4082 ssl->session_out = NULL;
4083
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004084 memset(ssl->out_buf, 0, out_buf_len);
Hanno Becker4ccbf062018-08-10 11:20:38 +01004085
David Horstmannd4f22082022-10-26 18:25:14 +01004086 int clear_in_buf = 1;
Hanno Becker4ccbf062018-08-10 11:20:38 +01004087#if defined(MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE) && defined(MBEDTLS_SSL_SRV_C)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004088 if (partial != 0) {
David Horstmannd4f22082022-10-26 18:25:14 +01004089 clear_in_buf = 0;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004090 }
Hanno Becker4ccbf062018-08-10 11:20:38 +01004091#endif /* MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE && MBEDTLS_SSL_SRV_C */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004092 if (clear_in_buf) {
Hanno Becker4ccbf062018-08-10 11:20:38 +01004093 ssl->in_left = 0;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004094 memset(ssl->in_buf, 0, in_buf_len);
Hanno Becker4ccbf062018-08-10 11:20:38 +01004095 }
Paul Bakker05ef8352012-05-08 09:17:57 +00004096
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004097#if defined(MBEDTLS_SSL_HW_RECORD_ACCEL)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004098 if (mbedtls_ssl_hw_record_reset != NULL) {
4099 MBEDTLS_SSL_DEBUG_MSG(2, ("going for mbedtls_ssl_hw_record_reset()"));
4100 if ((ret = mbedtls_ssl_hw_record_reset(ssl)) != 0) {
4101 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ssl_hw_record_reset", ret);
4102 return MBEDTLS_ERR_SSL_HW_ACCEL_FAILED;
Paul Bakker2770fbd2012-07-03 13:30:23 +00004103 }
Paul Bakker05ef8352012-05-08 09:17:57 +00004104 }
4105#endif
Paul Bakker2770fbd2012-07-03 13:30:23 +00004106
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004107 if (ssl->transform) {
4108 mbedtls_ssl_transform_free(ssl->transform);
4109 mbedtls_free(ssl->transform);
Paul Bakker48916f92012-09-16 19:57:18 +00004110 ssl->transform = NULL;
Paul Bakker2770fbd2012-07-03 13:30:23 +00004111 }
Paul Bakker48916f92012-09-16 19:57:18 +00004112
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004113 if (ssl->session) {
4114 mbedtls_ssl_session_free(ssl->session);
4115 mbedtls_free(ssl->session);
Paul Bakkerc0463502013-02-14 11:19:38 +01004116 ssl->session = NULL;
4117 }
4118
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004119#if defined(MBEDTLS_SSL_ALPN)
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02004120 ssl->alpn_chosen = NULL;
4121#endif
4122
Manuel Pégourié-Gonnarde057d3b2015-05-20 10:59:43 +02004123#if defined(MBEDTLS_SSL_DTLS_HELLO_VERIFY) && defined(MBEDTLS_SSL_SRV_C)
David Horstmannb5b1ed22022-10-27 13:21:49 +01004124 int free_cli_id = 1;
Hanno Becker4ccbf062018-08-10 11:20:38 +01004125#if defined(MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004126 if (partial != 0) {
David Horstmannd4f22082022-10-26 18:25:14 +01004127 free_cli_id = 0;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004128 }
Hanno Becker4ccbf062018-08-10 11:20:38 +01004129#endif
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004130 if (free_cli_id) {
4131 mbedtls_free(ssl->cli_id);
Manuel Pégourié-Gonnard3f09b6d2015-09-08 11:58:14 +02004132 ssl->cli_id = NULL;
4133 ssl->cli_id_len = 0;
4134 }
Manuel Pégourié-Gonnard43c02182014-07-22 17:32:01 +02004135#endif
4136
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004137 if ((ret = ssl_handshake_init(ssl)) != 0) {
4138 return ret;
4139 }
Paul Bakker2770fbd2012-07-03 13:30:23 +00004140
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004141 return 0;
Paul Bakker7eb013f2011-10-06 12:37:39 +00004142}
4143
Manuel Pégourié-Gonnard779e4292013-08-03 13:50:48 +02004144/*
Manuel Pégourié-Gonnard3f09b6d2015-09-08 11:58:14 +02004145 * Reset an initialized and used SSL context for re-use while retaining
4146 * all application-set variables, function pointers and data.
4147 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004148int mbedtls_ssl_session_reset(mbedtls_ssl_context *ssl)
Manuel Pégourié-Gonnard3f09b6d2015-09-08 11:58:14 +02004149{
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004150 return mbedtls_ssl_session_reset_int(ssl, 0);
Manuel Pégourié-Gonnard3f09b6d2015-09-08 11:58:14 +02004151}
4152
4153/*
Paul Bakker5121ce52009-01-03 21:22:43 +00004154 * SSL set accessors
4155 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004156void mbedtls_ssl_conf_endpoint(mbedtls_ssl_config *conf, int endpoint)
Paul Bakker5121ce52009-01-03 21:22:43 +00004157{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02004158 conf->endpoint = endpoint;
Paul Bakker5121ce52009-01-03 21:22:43 +00004159}
4160
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004161void mbedtls_ssl_conf_transport(mbedtls_ssl_config *conf, int transport)
Manuel Pégourié-Gonnard0b1ff292014-02-06 13:04:16 +01004162{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02004163 conf->transport = transport;
Manuel Pégourié-Gonnard0b1ff292014-02-06 13:04:16 +01004164}
4165
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004166#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004167void mbedtls_ssl_conf_dtls_anti_replay(mbedtls_ssl_config *conf, char mode)
Manuel Pégourié-Gonnard27393132014-09-24 14:41:11 +02004168{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02004169 conf->anti_replay = mode;
Manuel Pégourié-Gonnard27393132014-09-24 14:41:11 +02004170}
4171#endif
4172
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004173#if defined(MBEDTLS_SSL_DTLS_BADMAC_LIMIT)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004174void mbedtls_ssl_conf_dtls_badmac_limit(mbedtls_ssl_config *conf, unsigned limit)
Manuel Pégourié-Gonnardb0643d12014-10-14 18:30:36 +02004175{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02004176 conf->badmac_limit = limit;
Manuel Pégourié-Gonnardb0643d12014-10-14 18:30:36 +02004177}
4178#endif
4179
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004180#if defined(MBEDTLS_SSL_PROTO_DTLS)
Hanno Becker04da1892018-08-14 13:22:10 +01004181
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004182void mbedtls_ssl_set_datagram_packing(mbedtls_ssl_context *ssl,
4183 unsigned allow_packing)
Hanno Becker04da1892018-08-14 13:22:10 +01004184{
4185 ssl->disable_datagram_packing = !allow_packing;
4186}
4187
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004188void mbedtls_ssl_conf_handshake_timeout(mbedtls_ssl_config *conf,
4189 uint32_t min, uint32_t max)
Manuel Pégourié-Gonnard905dd242014-10-01 12:03:55 +02004190{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02004191 conf->hs_timeout_min = min;
4192 conf->hs_timeout_max = max;
Manuel Pégourié-Gonnard905dd242014-10-01 12:03:55 +02004193}
4194#endif
4195
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004196void mbedtls_ssl_conf_authmode(mbedtls_ssl_config *conf, int authmode)
Paul Bakker5121ce52009-01-03 21:22:43 +00004197{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02004198 conf->authmode = authmode;
Paul Bakker5121ce52009-01-03 21:22:43 +00004199}
4200
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004201#if defined(MBEDTLS_X509_CRT_PARSE_C)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004202void mbedtls_ssl_conf_verify(mbedtls_ssl_config *conf,
4203 int (*f_vrfy)(void *, mbedtls_x509_crt *, int, uint32_t *),
4204 void *p_vrfy)
Paul Bakkerb63b0af2011-01-13 17:54:59 +00004205{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02004206 conf->f_vrfy = f_vrfy;
4207 conf->p_vrfy = p_vrfy;
Paul Bakkerb63b0af2011-01-13 17:54:59 +00004208}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004209#endif /* MBEDTLS_X509_CRT_PARSE_C */
Paul Bakkerb63b0af2011-01-13 17:54:59 +00004210
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004211void mbedtls_ssl_conf_rng(mbedtls_ssl_config *conf,
4212 int (*f_rng)(void *, unsigned char *, size_t),
4213 void *p_rng)
Paul Bakker5121ce52009-01-03 21:22:43 +00004214{
Manuel Pégourié-Gonnard750e4d72015-05-07 12:35:38 +01004215 conf->f_rng = f_rng;
4216 conf->p_rng = p_rng;
Paul Bakker5121ce52009-01-03 21:22:43 +00004217}
4218
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004219void mbedtls_ssl_conf_dbg(mbedtls_ssl_config *conf,
4220 void (*f_dbg)(void *, int, const char *, int, const char *),
4221 void *p_dbg)
Paul Bakker5121ce52009-01-03 21:22:43 +00004222{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02004223 conf->f_dbg = f_dbg;
4224 conf->p_dbg = p_dbg;
Paul Bakker5121ce52009-01-03 21:22:43 +00004225}
4226
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004227void mbedtls_ssl_set_bio(mbedtls_ssl_context *ssl,
4228 void *p_bio,
4229 mbedtls_ssl_send_t *f_send,
4230 mbedtls_ssl_recv_t *f_recv,
4231 mbedtls_ssl_recv_timeout_t *f_recv_timeout)
Manuel Pégourié-Gonnard8fa6dfd2014-09-17 10:47:43 +02004232{
4233 ssl->p_bio = p_bio;
4234 ssl->f_send = f_send;
4235 ssl->f_recv = f_recv;
4236 ssl->f_recv_timeout = f_recv_timeout;
Manuel Pégourié-Gonnard97fd52c2015-05-06 15:38:52 +01004237}
4238
Manuel Pégourié-Gonnard6e7aaca2018-08-20 10:37:23 +02004239#if defined(MBEDTLS_SSL_PROTO_DTLS)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004240void mbedtls_ssl_set_mtu(mbedtls_ssl_context *ssl, uint16_t mtu)
Manuel Pégourié-Gonnard6e7aaca2018-08-20 10:37:23 +02004241{
4242 ssl->mtu = mtu;
4243}
4244#endif
4245
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004246void mbedtls_ssl_conf_read_timeout(mbedtls_ssl_config *conf, uint32_t timeout)
Manuel Pégourié-Gonnard97fd52c2015-05-06 15:38:52 +01004247{
4248 conf->read_timeout = timeout;
Manuel Pégourié-Gonnard8fa6dfd2014-09-17 10:47:43 +02004249}
4250
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004251void mbedtls_ssl_set_timer_cb(mbedtls_ssl_context *ssl,
4252 void *p_timer,
4253 mbedtls_ssl_set_timer_t *f_set_timer,
4254 mbedtls_ssl_get_timer_t *f_get_timer)
Manuel Pégourié-Gonnard2e012912015-05-12 20:55:41 +02004255{
4256 ssl->p_timer = p_timer;
4257 ssl->f_set_timer = f_set_timer;
4258 ssl->f_get_timer = f_get_timer;
Manuel Pégourié-Gonnard286a1362015-05-13 16:22:05 +02004259
4260 /* Make sure we start with no timer running */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004261 mbedtls_ssl_set_timer(ssl, 0);
Manuel Pégourié-Gonnard2e012912015-05-12 20:55:41 +02004262}
4263
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004264#if defined(MBEDTLS_SSL_SRV_C)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004265void mbedtls_ssl_conf_session_cache(mbedtls_ssl_config *conf,
4266 void *p_cache,
4267 int (*f_get_cache)(void *, mbedtls_ssl_session *),
4268 int (*f_set_cache)(void *, const mbedtls_ssl_session *))
Paul Bakker5121ce52009-01-03 21:22:43 +00004269{
Manuel Pégourié-Gonnard5cb33082015-05-06 18:06:26 +01004270 conf->p_cache = p_cache;
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02004271 conf->f_get_cache = f_get_cache;
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02004272 conf->f_set_cache = f_set_cache;
Paul Bakker5121ce52009-01-03 21:22:43 +00004273}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004274#endif /* MBEDTLS_SSL_SRV_C */
Paul Bakker5121ce52009-01-03 21:22:43 +00004275
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004276#if defined(MBEDTLS_SSL_CLI_C)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004277int mbedtls_ssl_set_session(mbedtls_ssl_context *ssl, const mbedtls_ssl_session *session)
Paul Bakker5121ce52009-01-03 21:22:43 +00004278{
Janos Follath865b3eb2019-12-16 11:46:15 +00004279 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +02004280
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004281 if (ssl == NULL ||
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +02004282 session == NULL ||
4283 ssl->session_negotiate == NULL ||
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004284 ssl->conf->endpoint != MBEDTLS_SSL_IS_CLIENT) {
4285 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +02004286 }
4287
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004288 if ((ret = mbedtls_ssl_session_copy(ssl->session_negotiate,
4289 session)) != 0) {
4290 return ret;
4291 }
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +02004292
Paul Bakker0a597072012-09-25 21:55:46 +00004293 ssl->handshake->resume = 1;
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +02004294
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004295 return 0;
Paul Bakker5121ce52009-01-03 21:22:43 +00004296}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004297#endif /* MBEDTLS_SSL_CLI_C */
Paul Bakker5121ce52009-01-03 21:22:43 +00004298
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004299void mbedtls_ssl_conf_ciphersuites(mbedtls_ssl_config *conf,
4300 const int *ciphersuites)
Paul Bakker5121ce52009-01-03 21:22:43 +00004301{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02004302 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_0] = ciphersuites;
4303 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_1] = ciphersuites;
4304 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_2] = ciphersuites;
4305 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_3] = ciphersuites;
Paul Bakker8f4ddae2013-04-15 15:09:54 +02004306}
4307
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004308void mbedtls_ssl_conf_ciphersuites_for_version(mbedtls_ssl_config *conf,
4309 const int *ciphersuites,
4310 int major, int minor)
Paul Bakker8f4ddae2013-04-15 15:09:54 +02004311{
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004312 if (major != MBEDTLS_SSL_MAJOR_VERSION_3) {
Paul Bakker8f4ddae2013-04-15 15:09:54 +02004313 return;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004314 }
Paul Bakker8f4ddae2013-04-15 15:09:54 +02004315
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004316 if (minor < MBEDTLS_SSL_MINOR_VERSION_0 || minor > MBEDTLS_SSL_MINOR_VERSION_3) {
Paul Bakker8f4ddae2013-04-15 15:09:54 +02004317 return;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004318 }
Paul Bakker8f4ddae2013-04-15 15:09:54 +02004319
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02004320 conf->ciphersuite_list[minor] = ciphersuites;
Paul Bakker5121ce52009-01-03 21:22:43 +00004321}
4322
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004323#if defined(MBEDTLS_X509_CRT_PARSE_C)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004324void mbedtls_ssl_conf_cert_profile(mbedtls_ssl_config *conf,
4325 const mbedtls_x509_crt_profile *profile)
Manuel Pégourié-Gonnard6e3ee3a2015-06-17 10:58:20 +02004326{
4327 conf->cert_profile = profile;
4328}
4329
Manuel Pégourié-Gonnard8f618a82015-05-10 21:13:36 +02004330/* Append a new keycert entry to a (possibly empty) list */
Manuel Pégourié-Gonnardd904d662022-06-17 10:24:00 +02004331MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004332static int ssl_append_key_cert(mbedtls_ssl_key_cert **head,
4333 mbedtls_x509_crt *cert,
4334 mbedtls_pk_context *key)
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02004335{
niisato8ee24222018-06-25 19:05:48 +09004336 mbedtls_ssl_key_cert *new_cert;
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02004337
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004338 new_cert = mbedtls_calloc(1, sizeof(mbedtls_ssl_key_cert));
4339 if (new_cert == NULL) {
4340 return MBEDTLS_ERR_SSL_ALLOC_FAILED;
4341 }
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02004342
niisato8ee24222018-06-25 19:05:48 +09004343 new_cert->cert = cert;
4344 new_cert->key = key;
4345 new_cert->next = NULL;
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02004346
Manuel Pégourié-Gonnard8f618a82015-05-10 21:13:36 +02004347 /* Update head is the list was null, else add to the end */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004348 if (*head == NULL) {
niisato8ee24222018-06-25 19:05:48 +09004349 *head = new_cert;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004350 } else {
Manuel Pégourié-Gonnard8f618a82015-05-10 21:13:36 +02004351 mbedtls_ssl_key_cert *cur = *head;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004352 while (cur->next != NULL) {
Manuel Pégourié-Gonnard8f618a82015-05-10 21:13:36 +02004353 cur = cur->next;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004354 }
niisato8ee24222018-06-25 19:05:48 +09004355 cur->next = new_cert;
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02004356 }
4357
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004358 return 0;
Manuel Pégourié-Gonnard8f618a82015-05-10 21:13:36 +02004359}
4360
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004361int mbedtls_ssl_conf_own_cert(mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnard8f618a82015-05-10 21:13:36 +02004362 mbedtls_x509_crt *own_cert,
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004363 mbedtls_pk_context *pk_key)
Manuel Pégourié-Gonnard8f618a82015-05-10 21:13:36 +02004364{
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004365 return ssl_append_key_cert(&conf->key_cert, own_cert, pk_key);
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02004366}
4367
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004368void mbedtls_ssl_conf_ca_chain(mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnardbc2b7712015-05-06 11:14:19 +01004369 mbedtls_x509_crt *ca_chain,
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004370 mbedtls_x509_crl *ca_crl)
Paul Bakker5121ce52009-01-03 21:22:43 +00004371{
Manuel Pégourié-Gonnardbc2b7712015-05-06 11:14:19 +01004372 conf->ca_chain = ca_chain;
4373 conf->ca_crl = ca_crl;
Hanno Becker5adaad92019-03-27 16:54:37 +00004374
4375#if defined(MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK)
4376 /* mbedtls_ssl_conf_ca_chain() and mbedtls_ssl_conf_ca_cb()
4377 * cannot be used together. */
4378 conf->f_ca_cb = NULL;
4379 conf->p_ca_cb = NULL;
4380#endif /* MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK */
Paul Bakker5121ce52009-01-03 21:22:43 +00004381}
Hanno Becker5adaad92019-03-27 16:54:37 +00004382
4383#if defined(MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004384void mbedtls_ssl_conf_ca_cb(mbedtls_ssl_config *conf,
4385 mbedtls_x509_crt_ca_cb_t f_ca_cb,
4386 void *p_ca_cb)
Hanno Becker5adaad92019-03-27 16:54:37 +00004387{
4388 conf->f_ca_cb = f_ca_cb;
4389 conf->p_ca_cb = p_ca_cb;
4390
4391 /* mbedtls_ssl_conf_ca_chain() and mbedtls_ssl_conf_ca_cb()
4392 * cannot be used together. */
4393 conf->ca_chain = NULL;
4394 conf->ca_crl = NULL;
4395}
4396#endif /* MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004397#endif /* MBEDTLS_X509_CRT_PARSE_C */
Paul Bakkereb2c6582012-09-27 19:15:01 +00004398
Manuel Pégourié-Gonnard1af6c852015-05-10 23:10:37 +02004399#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004400int mbedtls_ssl_set_hs_own_cert(mbedtls_ssl_context *ssl,
4401 mbedtls_x509_crt *own_cert,
4402 mbedtls_pk_context *pk_key)
Manuel Pégourié-Gonnard1af6c852015-05-10 23:10:37 +02004403{
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004404 return ssl_append_key_cert(&ssl->handshake->sni_key_cert,
4405 own_cert, pk_key);
Manuel Pégourié-Gonnard1af6c852015-05-10 23:10:37 +02004406}
Manuel Pégourié-Gonnard22bfa4b2015-05-11 08:46:37 +02004407
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004408void mbedtls_ssl_set_hs_ca_chain(mbedtls_ssl_context *ssl,
4409 mbedtls_x509_crt *ca_chain,
4410 mbedtls_x509_crl *ca_crl)
Manuel Pégourié-Gonnard22bfa4b2015-05-11 08:46:37 +02004411{
4412 ssl->handshake->sni_ca_chain = ca_chain;
4413 ssl->handshake->sni_ca_crl = ca_crl;
4414}
Manuel Pégourié-Gonnardcdc26ae2015-06-19 12:16:31 +02004415
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004416void mbedtls_ssl_set_hs_authmode(mbedtls_ssl_context *ssl,
4417 int authmode)
Manuel Pégourié-Gonnardcdc26ae2015-06-19 12:16:31 +02004418{
4419 ssl->handshake->sni_authmode = authmode;
4420}
Manuel Pégourié-Gonnard1af6c852015-05-10 23:10:37 +02004421#endif /* MBEDTLS_SSL_SERVER_NAME_INDICATION */
4422
Hanno Becker8927c832019-04-03 12:52:50 +01004423#if defined(MBEDTLS_X509_CRT_PARSE_C)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004424void mbedtls_ssl_set_verify(mbedtls_ssl_context *ssl,
4425 int (*f_vrfy)(void *, mbedtls_x509_crt *, int, uint32_t *),
4426 void *p_vrfy)
Hanno Becker8927c832019-04-03 12:52:50 +01004427{
4428 ssl->f_vrfy = f_vrfy;
4429 ssl->p_vrfy = p_vrfy;
4430}
4431#endif
4432
Manuel Pégourié-Gonnardeef142d2015-09-16 10:05:04 +02004433#if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
Manuel Pégourié-Gonnard7002f4a2015-09-15 12:43:43 +02004434/*
4435 * Set EC J-PAKE password for current handshake
4436 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004437int mbedtls_ssl_set_hs_ecjpake_password(mbedtls_ssl_context *ssl,
4438 const unsigned char *pw,
4439 size_t pw_len)
Manuel Pégourié-Gonnard7002f4a2015-09-15 12:43:43 +02004440{
4441 mbedtls_ecjpake_role role;
4442
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004443 if (ssl->handshake == NULL || ssl->conf == NULL) {
4444 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
4445 }
Manuel Pégourié-Gonnard7002f4a2015-09-15 12:43:43 +02004446
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004447 if (ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER) {
Manuel Pégourié-Gonnard7002f4a2015-09-15 12:43:43 +02004448 role = MBEDTLS_ECJPAKE_SERVER;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004449 } else {
Manuel Pégourié-Gonnard7002f4a2015-09-15 12:43:43 +02004450 role = MBEDTLS_ECJPAKE_CLIENT;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004451 }
Manuel Pégourié-Gonnard7002f4a2015-09-15 12:43:43 +02004452
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004453 return mbedtls_ecjpake_setup(&ssl->handshake->ecjpake_ctx,
4454 role,
4455 MBEDTLS_MD_SHA256,
4456 MBEDTLS_ECP_DP_SECP256R1,
4457 pw, pw_len);
Manuel Pégourié-Gonnard7002f4a2015-09-15 12:43:43 +02004458}
Manuel Pégourié-Gonnardeef142d2015-09-16 10:05:04 +02004459#endif /* MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED */
Manuel Pégourié-Gonnard7002f4a2015-09-15 12:43:43 +02004460
Gilles Peskineeccd8882020-03-10 12:19:08 +01004461#if defined(MBEDTLS_KEY_EXCHANGE_SOME_PSK_ENABLED)
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01004462
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004463static void ssl_conf_remove_psk(mbedtls_ssl_config *conf)
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01004464{
4465 /* Remove reference to existing PSK, if any. */
4466#if defined(MBEDTLS_USE_PSA_CRYPTO)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004467 if (!mbedtls_svc_key_id_is_null(conf->psk_opaque)) {
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01004468 /* The maintenance of the PSK key slot is the
4469 * user's responsibility. */
Ronald Croncf56a0a2020-08-04 09:51:30 +02004470 conf->psk_opaque = MBEDTLS_SVC_KEY_ID_INIT;
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01004471 }
Hanno Beckera63ac3f2018-11-05 12:47:16 +00004472 /* This and the following branch should never
Tom Cosgrove49f99bc2022-12-04 16:44:21 +00004473 * be taken simultaneously as we maintain the
Hanno Beckera63ac3f2018-11-05 12:47:16 +00004474 * invariant that raw and opaque PSKs are never
4475 * configured simultaneously. As a safeguard,
4476 * though, `else` is omitted here. */
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01004477#endif /* MBEDTLS_USE_PSA_CRYPTO */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004478 if (conf->psk != NULL) {
4479 mbedtls_platform_zeroize(conf->psk, conf->psk_len);
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01004480
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004481 mbedtls_free(conf->psk);
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01004482 conf->psk = NULL;
4483 conf->psk_len = 0;
4484 }
4485
4486 /* Remove reference to PSK identity, if any. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004487 if (conf->psk_identity != NULL) {
4488 mbedtls_free(conf->psk_identity);
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01004489 conf->psk_identity = NULL;
4490 conf->psk_identity_len = 0;
4491 }
4492}
4493
Hanno Becker7390c712018-11-15 13:33:04 +00004494/* This function assumes that PSK identity in the SSL config is unset.
4495 * It checks that the provided identity is well-formed and attempts
4496 * to make a copy of it in the SSL config.
4497 * On failure, the PSK identity in the config remains unset. */
Manuel Pégourié-Gonnardd904d662022-06-17 10:24:00 +02004498MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004499static int ssl_conf_set_psk_identity(mbedtls_ssl_config *conf,
4500 unsigned char const *psk_identity,
4501 size_t psk_identity_len)
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02004502{
Manuel Pégourié-Gonnardc6b5d832015-08-27 16:37:35 +02004503 /* Identity len will be encoded on two bytes */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004504 if (psk_identity == NULL ||
4505 (psk_identity_len >> 16) != 0 ||
4506 psk_identity_len > MBEDTLS_SSL_OUT_CONTENT_LEN) {
4507 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
Manuel Pégourié-Gonnardc6b5d832015-08-27 16:37:35 +02004508 }
4509
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004510 conf->psk_identity = mbedtls_calloc(1, psk_identity_len);
4511 if (conf->psk_identity == NULL) {
4512 return MBEDTLS_ERR_SSL_ALLOC_FAILED;
4513 }
Paul Bakker6db455e2013-09-18 17:29:31 +02004514
Manuel Pégourié-Gonnard120fdbd2015-05-07 17:07:50 +01004515 conf->psk_identity_len = psk_identity_len;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004516 memcpy(conf->psk_identity, psk_identity, conf->psk_identity_len);
Paul Bakker5ad403f2013-09-18 21:21:30 +02004517
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004518 return 0;
Paul Bakker6db455e2013-09-18 17:29:31 +02004519}
4520
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004521int mbedtls_ssl_conf_psk(mbedtls_ssl_config *conf,
4522 const unsigned char *psk, size_t psk_len,
4523 const unsigned char *psk_identity, size_t psk_identity_len)
Hanno Becker7390c712018-11-15 13:33:04 +00004524{
Janos Follath865b3eb2019-12-16 11:46:15 +00004525 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Hanno Becker7390c712018-11-15 13:33:04 +00004526 /* Remove opaque/raw PSK + PSK Identity */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004527 ssl_conf_remove_psk(conf);
Hanno Becker7390c712018-11-15 13:33:04 +00004528
4529 /* Check and set raw PSK */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004530 if (psk == NULL) {
4531 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
4532 }
4533 if (psk_len == 0) {
4534 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
4535 }
4536 if (psk_len > MBEDTLS_PSK_MAX_LEN) {
4537 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
4538 }
Piotr Nowicki9926eaf2019-11-20 14:54:36 +01004539
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004540 if ((conf->psk = mbedtls_calloc(1, psk_len)) == NULL) {
4541 return MBEDTLS_ERR_SSL_ALLOC_FAILED;
4542 }
Hanno Becker7390c712018-11-15 13:33:04 +00004543 conf->psk_len = psk_len;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004544 memcpy(conf->psk, psk, conf->psk_len);
Hanno Becker7390c712018-11-15 13:33:04 +00004545
4546 /* Check and set PSK Identity */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004547 ret = ssl_conf_set_psk_identity(conf, psk_identity, psk_identity_len);
4548 if (ret != 0) {
4549 ssl_conf_remove_psk(conf);
4550 }
Hanno Becker7390c712018-11-15 13:33:04 +00004551
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004552 return ret;
Hanno Becker7390c712018-11-15 13:33:04 +00004553}
4554
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004555static void ssl_remove_psk(mbedtls_ssl_context *ssl)
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01004556{
4557#if defined(MBEDTLS_USE_PSA_CRYPTO)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004558 if (!mbedtls_svc_key_id_is_null(ssl->handshake->psk_opaque)) {
Ronald Croncf56a0a2020-08-04 09:51:30 +02004559 ssl->handshake->psk_opaque = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004560 } else
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01004561#endif /* MBEDTLS_USE_PSA_CRYPTO */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004562 if (ssl->handshake->psk != NULL) {
4563 mbedtls_platform_zeroize(ssl->handshake->psk,
4564 ssl->handshake->psk_len);
4565 mbedtls_free(ssl->handshake->psk);
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01004566 ssl->handshake->psk_len = 0;
lhuang040a2dd6d2024-06-11 12:31:17 -07004567 ssl->handshake->psk = NULL;
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01004568 }
4569}
4570
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004571int mbedtls_ssl_set_hs_psk(mbedtls_ssl_context *ssl,
4572 const unsigned char *psk, size_t psk_len)
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01004573{
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004574 if (psk == NULL || ssl->handshake == NULL) {
4575 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
4576 }
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01004577
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004578 if (psk_len > MBEDTLS_PSK_MAX_LEN) {
4579 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
4580 }
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01004581
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004582 ssl_remove_psk(ssl);
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01004583
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004584 if ((ssl->handshake->psk = mbedtls_calloc(1, psk_len)) == NULL) {
4585 return MBEDTLS_ERR_SSL_ALLOC_FAILED;
4586 }
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01004587
4588 ssl->handshake->psk_len = psk_len;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004589 memcpy(ssl->handshake->psk, psk, ssl->handshake->psk_len);
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01004590
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004591 return 0;
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01004592}
4593
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01004594#if defined(MBEDTLS_USE_PSA_CRYPTO)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004595int mbedtls_ssl_conf_psk_opaque(mbedtls_ssl_config *conf,
4596 psa_key_id_t psk,
4597 const unsigned char *psk_identity,
4598 size_t psk_identity_len)
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01004599{
Janos Follath865b3eb2019-12-16 11:46:15 +00004600 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Hanno Becker7390c712018-11-15 13:33:04 +00004601 /* Clear opaque/raw PSK + PSK Identity, if present. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004602 ssl_conf_remove_psk(conf);
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01004603
Hanno Becker7390c712018-11-15 13:33:04 +00004604 /* Check and set opaque PSK */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004605 if (mbedtls_svc_key_id_is_null(psk)) {
4606 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
4607 }
Ronald Croncf56a0a2020-08-04 09:51:30 +02004608 conf->psk_opaque = psk;
Hanno Becker7390c712018-11-15 13:33:04 +00004609
4610 /* Check and set PSK Identity */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004611 ret = ssl_conf_set_psk_identity(conf, psk_identity,
4612 psk_identity_len);
4613 if (ret != 0) {
4614 ssl_conf_remove_psk(conf);
4615 }
Hanno Becker7390c712018-11-15 13:33:04 +00004616
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004617 return ret;
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01004618}
4619
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004620int mbedtls_ssl_set_hs_psk_opaque(mbedtls_ssl_context *ssl,
4621 psa_key_id_t psk)
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01004622{
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004623 if ((mbedtls_svc_key_id_is_null(psk)) ||
4624 (ssl->handshake == NULL)) {
4625 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
4626 }
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01004627
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004628 ssl_remove_psk(ssl);
Ronald Croncf56a0a2020-08-04 09:51:30 +02004629 ssl->handshake->psk_opaque = psk;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004630 return 0;
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01004631}
4632#endif /* MBEDTLS_USE_PSA_CRYPTO */
4633
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004634void mbedtls_ssl_conf_psk_cb(mbedtls_ssl_config *conf,
4635 int (*f_psk)(void *, mbedtls_ssl_context *, const unsigned char *,
4636 size_t),
4637 void *p_psk)
Paul Bakker6db455e2013-09-18 17:29:31 +02004638{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02004639 conf->f_psk = f_psk;
4640 conf->p_psk = p_psk;
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02004641}
Gilles Peskineeccd8882020-03-10 12:19:08 +01004642#endif /* MBEDTLS_KEY_EXCHANGE_SOME_PSK_ENABLED */
Paul Bakker43b7e352011-01-18 15:27:19 +00004643
Manuel Pégourié-Gonnardcf141ca2015-05-20 10:35:51 +02004644#if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_SSL_SRV_C)
Hanno Becker470a8c42017-10-04 15:28:46 +01004645
4646#if !defined(MBEDTLS_DEPRECATED_REMOVED)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004647int mbedtls_ssl_conf_dh_param(mbedtls_ssl_config *conf, const char *dhm_P, const char *dhm_G)
Paul Bakker5121ce52009-01-03 21:22:43 +00004648{
Janos Follath865b3eb2019-12-16 11:46:15 +00004649 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker5121ce52009-01-03 21:22:43 +00004650
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004651 if ((ret = mbedtls_mpi_read_string(&conf->dhm_P, 16, dhm_P)) != 0 ||
4652 (ret = mbedtls_mpi_read_string(&conf->dhm_G, 16, dhm_G)) != 0) {
4653 mbedtls_mpi_free(&conf->dhm_P);
4654 mbedtls_mpi_free(&conf->dhm_G);
4655 return ret;
Manuel Pégourié-Gonnard1028b742015-05-06 17:33:07 +01004656 }
Paul Bakker5121ce52009-01-03 21:22:43 +00004657
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004658 return 0;
Paul Bakker5121ce52009-01-03 21:22:43 +00004659}
Hanno Becker470a8c42017-10-04 15:28:46 +01004660#endif /* MBEDTLS_DEPRECATED_REMOVED */
Paul Bakker5121ce52009-01-03 21:22:43 +00004661
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004662int mbedtls_ssl_conf_dh_param_bin(mbedtls_ssl_config *conf,
4663 const unsigned char *dhm_P, size_t P_len,
4664 const unsigned char *dhm_G, size_t G_len)
Hanno Beckera90658f2017-10-04 15:29:08 +01004665{
Janos Follath865b3eb2019-12-16 11:46:15 +00004666 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Hanno Beckera90658f2017-10-04 15:29:08 +01004667
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004668 mbedtls_mpi_free(&conf->dhm_P);
4669 mbedtls_mpi_free(&conf->dhm_G);
Glenn Straussde081ce2021-12-20 01:43:17 -05004670
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004671 if ((ret = mbedtls_mpi_read_binary(&conf->dhm_P, dhm_P, P_len)) != 0 ||
4672 (ret = mbedtls_mpi_read_binary(&conf->dhm_G, dhm_G, G_len)) != 0) {
4673 mbedtls_mpi_free(&conf->dhm_P);
4674 mbedtls_mpi_free(&conf->dhm_G);
4675 return ret;
Hanno Beckera90658f2017-10-04 15:29:08 +01004676 }
4677
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004678 return 0;
Hanno Beckera90658f2017-10-04 15:29:08 +01004679}
Paul Bakker5121ce52009-01-03 21:22:43 +00004680
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004681int mbedtls_ssl_conf_dh_param_ctx(mbedtls_ssl_config *conf, mbedtls_dhm_context *dhm_ctx)
Paul Bakker1b57b062011-01-06 15:48:19 +00004682{
Janos Follath865b3eb2019-12-16 11:46:15 +00004683 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker1b57b062011-01-06 15:48:19 +00004684
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004685 mbedtls_mpi_free(&conf->dhm_P);
4686 mbedtls_mpi_free(&conf->dhm_G);
Glenn Straussde081ce2021-12-20 01:43:17 -05004687
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004688 if ((ret = mbedtls_mpi_copy(&conf->dhm_P, &dhm_ctx->P)) != 0 ||
4689 (ret = mbedtls_mpi_copy(&conf->dhm_G, &dhm_ctx->G)) != 0) {
4690 mbedtls_mpi_free(&conf->dhm_P);
4691 mbedtls_mpi_free(&conf->dhm_G);
4692 return ret;
Manuel Pégourié-Gonnard1028b742015-05-06 17:33:07 +01004693 }
Paul Bakker1b57b062011-01-06 15:48:19 +00004694
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004695 return 0;
Paul Bakker1b57b062011-01-06 15:48:19 +00004696}
Manuel Pégourié-Gonnardcf141ca2015-05-20 10:35:51 +02004697#endif /* MBEDTLS_DHM_C && MBEDTLS_SSL_SRV_C */
Paul Bakker1b57b062011-01-06 15:48:19 +00004698
Manuel Pégourié-Gonnardbd990d62015-06-11 14:49:42 +02004699#if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_SSL_CLI_C)
4700/*
4701 * Set the minimum length for Diffie-Hellman parameters
4702 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004703void mbedtls_ssl_conf_dhm_min_bitlen(mbedtls_ssl_config *conf,
4704 unsigned int bitlen)
Manuel Pégourié-Gonnardbd990d62015-06-11 14:49:42 +02004705{
4706 conf->dhm_min_bitlen = bitlen;
4707}
4708#endif /* MBEDTLS_DHM_C && MBEDTLS_SSL_CLI_C */
4709
Gilles Peskineeccd8882020-03-10 12:19:08 +01004710#if defined(MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED)
Manuel Pégourié-Gonnard36a8b572015-06-17 12:43:26 +02004711/*
4712 * Set allowed/preferred hashes for handshake signatures
4713 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004714void mbedtls_ssl_conf_sig_hashes(mbedtls_ssl_config *conf,
4715 const int *hashes)
Manuel Pégourié-Gonnard36a8b572015-06-17 12:43:26 +02004716{
4717 conf->sig_hashes = hashes;
4718}
Gilles Peskineeccd8882020-03-10 12:19:08 +01004719#endif /* MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED */
Manuel Pégourié-Gonnard36a8b572015-06-17 12:43:26 +02004720
Manuel Pégourié-Gonnardb541da62015-06-17 11:43:30 +02004721#if defined(MBEDTLS_ECP_C)
Manuel Pégourié-Gonnard7f38ed02014-02-04 15:52:33 +01004722/*
4723 * Set the allowed elliptic curves
4724 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004725void mbedtls_ssl_conf_curves(mbedtls_ssl_config *conf,
4726 const mbedtls_ecp_group_id *curve_list)
Manuel Pégourié-Gonnard7f38ed02014-02-04 15:52:33 +01004727{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02004728 conf->curve_list = curve_list;
Manuel Pégourié-Gonnard7f38ed02014-02-04 15:52:33 +01004729}
Hanno Becker947194e2017-04-07 13:25:49 +01004730#endif /* MBEDTLS_ECP_C */
Manuel Pégourié-Gonnard7f38ed02014-02-04 15:52:33 +01004731
Manuel Pégourié-Gonnardbc2b7712015-05-06 11:14:19 +01004732#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004733void mbedtls_ssl_conf_sni(mbedtls_ssl_config *conf,
4734 int (*f_sni)(void *, mbedtls_ssl_context *,
4735 const unsigned char *, size_t),
4736 void *p_sni)
Paul Bakker5701cdc2012-09-27 21:49:42 +00004737{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02004738 conf->f_sni = f_sni;
4739 conf->p_sni = p_sni;
Paul Bakker5701cdc2012-09-27 21:49:42 +00004740}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004741#endif /* MBEDTLS_SSL_SERVER_NAME_INDICATION */
Paul Bakker5701cdc2012-09-27 21:49:42 +00004742
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004743#if defined(MBEDTLS_SSL_ALPN)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004744int mbedtls_ssl_conf_alpn_protocols(mbedtls_ssl_config *conf, const char **protos)
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02004745{
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +02004746 size_t cur_len, tot_len;
4747 const char **p;
4748
4749 /*
Brian J Murray1903fb32016-11-06 04:45:15 -08004750 * RFC 7301 3.1: "Empty strings MUST NOT be included and byte strings
4751 * MUST NOT be truncated."
4752 * We check lengths now rather than later.
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +02004753 */
4754 tot_len = 0;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004755 for (p = protos; *p != NULL; p++) {
4756 cur_len = strlen(*p);
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +02004757 tot_len += cur_len;
4758
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004759 if ((cur_len == 0) ||
4760 (cur_len > MBEDTLS_SSL_MAX_ALPN_NAME_LEN) ||
4761 (tot_len > MBEDTLS_SSL_MAX_ALPN_LIST_LEN)) {
4762 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
4763 }
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +02004764 }
4765
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02004766 conf->alpn_list = protos;
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +02004767
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004768 return 0;
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02004769}
4770
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004771const char *mbedtls_ssl_get_alpn_protocol(const mbedtls_ssl_context *ssl)
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02004772{
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004773 return ssl->alpn_chosen;
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02004774}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004775#endif /* MBEDTLS_SSL_ALPN */
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02004776
Johan Pascalb62bb512015-12-03 21:56:45 +01004777#if defined(MBEDTLS_SSL_DTLS_SRTP)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004778void mbedtls_ssl_conf_srtp_mki_value_supported(mbedtls_ssl_config *conf,
4779 int support_mki_value)
Ron Eldor591f1622018-01-22 12:30:04 +02004780{
4781 conf->dtls_srtp_mki_support = support_mki_value;
4782}
4783
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004784int mbedtls_ssl_dtls_srtp_set_mki_value(mbedtls_ssl_context *ssl,
4785 unsigned char *mki_value,
4786 uint16_t mki_len)
Ron Eldor591f1622018-01-22 12:30:04 +02004787{
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004788 if (mki_len > MBEDTLS_TLS_SRTP_MAX_MKI_LENGTH) {
4789 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
Ron Eldora9788042018-12-05 11:04:31 +02004790 }
Ron Eldor591f1622018-01-22 12:30:04 +02004791
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004792 if (ssl->conf->dtls_srtp_mki_support == MBEDTLS_SSL_DTLS_SRTP_MKI_UNSUPPORTED) {
4793 return MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
Ron Eldora9788042018-12-05 11:04:31 +02004794 }
Ron Eldor591f1622018-01-22 12:30:04 +02004795
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004796 memcpy(ssl->dtls_srtp_info.mki_value, mki_value, mki_len);
Ron Eldor591f1622018-01-22 12:30:04 +02004797 ssl->dtls_srtp_info.mki_len = mki_len;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004798 return 0;
Ron Eldor591f1622018-01-22 12:30:04 +02004799}
4800
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004801int mbedtls_ssl_conf_dtls_srtp_protection_profiles(mbedtls_ssl_config *conf,
4802 const mbedtls_ssl_srtp_profile *profiles)
Johan Pascalb62bb512015-12-03 21:56:45 +01004803{
Johan Pascal253d0262020-09-22 13:04:45 +02004804 const mbedtls_ssl_srtp_profile *p;
4805 size_t list_size = 0;
Johan Pascalb62bb512015-12-03 21:56:45 +01004806
Johan Pascal253d0262020-09-22 13:04:45 +02004807 /* check the profiles list: all entry must be valid,
4808 * its size cannot be more than the total number of supported profiles, currently 4 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004809 for (p = profiles; *p != MBEDTLS_TLS_SRTP_UNSET &&
4810 list_size <= MBEDTLS_TLS_SRTP_MAX_PROFILE_LIST_LENGTH;
4811 p++) {
4812 if (mbedtls_ssl_check_srtp_profile_value(*p) != MBEDTLS_TLS_SRTP_UNSET) {
Johan Pascal76fdf1d2020-10-22 23:31:00 +02004813 list_size++;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004814 } else {
Johan Pascal76fdf1d2020-10-22 23:31:00 +02004815 /* unsupported value, stop parsing and set the size to an error value */
4816 list_size = MBEDTLS_TLS_SRTP_MAX_PROFILE_LIST_LENGTH + 1;
Johan Pascalb62bb512015-12-03 21:56:45 +01004817 }
4818 }
4819
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004820 if (list_size > MBEDTLS_TLS_SRTP_MAX_PROFILE_LIST_LENGTH) {
4821 conf->dtls_srtp_profile_list = NULL;
4822 conf->dtls_srtp_profile_list_len = 0;
4823 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
Johan Pascal253d0262020-09-22 13:04:45 +02004824 }
4825
Johan Pascal9bc97ca2020-09-21 23:44:45 +02004826 conf->dtls_srtp_profile_list = profiles;
Johan Pascal253d0262020-09-22 13:04:45 +02004827 conf->dtls_srtp_profile_list_len = list_size;
Johan Pascalb62bb512015-12-03 21:56:45 +01004828
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004829 return 0;
Johan Pascalb62bb512015-12-03 21:56:45 +01004830}
4831
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004832void mbedtls_ssl_get_dtls_srtp_negotiation_result(const mbedtls_ssl_context *ssl,
4833 mbedtls_dtls_srtp_info *dtls_srtp_info)
Johan Pascalb62bb512015-12-03 21:56:45 +01004834{
Johan Pascal2258a4f2020-10-28 13:53:09 +01004835 dtls_srtp_info->chosen_dtls_srtp_profile = ssl->dtls_srtp_info.chosen_dtls_srtp_profile;
4836 /* do not copy the mki value if there is no chosen profile */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004837 if (dtls_srtp_info->chosen_dtls_srtp_profile == MBEDTLS_TLS_SRTP_UNSET) {
Johan Pascal2258a4f2020-10-28 13:53:09 +01004838 dtls_srtp_info->mki_len = 0;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004839 } else {
Johan Pascal2258a4f2020-10-28 13:53:09 +01004840 dtls_srtp_info->mki_len = ssl->dtls_srtp_info.mki_len;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004841 memcpy(dtls_srtp_info->mki_value, ssl->dtls_srtp_info.mki_value,
4842 ssl->dtls_srtp_info.mki_len);
Johan Pascal2258a4f2020-10-28 13:53:09 +01004843 }
Johan Pascalb62bb512015-12-03 21:56:45 +01004844}
Johan Pascalb62bb512015-12-03 21:56:45 +01004845#endif /* MBEDTLS_SSL_DTLS_SRTP */
4846
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004847void mbedtls_ssl_conf_max_version(mbedtls_ssl_config *conf, int major, int minor)
Paul Bakker490ecc82011-10-06 13:04:09 +00004848{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02004849 conf->max_major_ver = major;
4850 conf->max_minor_ver = minor;
Paul Bakker490ecc82011-10-06 13:04:09 +00004851}
4852
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004853void mbedtls_ssl_conf_min_version(mbedtls_ssl_config *conf, int major, int minor)
Paul Bakker1d29fb52012-09-28 13:28:45 +00004854{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02004855 conf->min_major_ver = major;
4856 conf->min_minor_ver = minor;
Paul Bakker1d29fb52012-09-28 13:28:45 +00004857}
4858
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004859#if defined(MBEDTLS_SSL_FALLBACK_SCSV) && defined(MBEDTLS_SSL_CLI_C)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004860void mbedtls_ssl_conf_fallback(mbedtls_ssl_config *conf, char fallback)
Manuel Pégourié-Gonnard1cbd39d2014-10-20 13:34:59 +02004861{
Manuel Pégourié-Gonnard684b0592015-05-06 09:27:31 +01004862 conf->fallback = fallback;
Manuel Pégourié-Gonnard1cbd39d2014-10-20 13:34:59 +02004863}
4864#endif
4865
Janos Follath088ce432017-04-10 12:42:31 +01004866#if defined(MBEDTLS_SSL_SRV_C)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004867void mbedtls_ssl_conf_cert_req_ca_list(mbedtls_ssl_config *conf,
4868 char cert_req_ca_list)
Janos Follath088ce432017-04-10 12:42:31 +01004869{
4870 conf->cert_req_ca_list = cert_req_ca_list;
4871}
4872#endif
4873
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004874#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004875void mbedtls_ssl_conf_encrypt_then_mac(mbedtls_ssl_config *conf, char etm)
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +01004876{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02004877 conf->encrypt_then_mac = etm;
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +01004878}
4879#endif
4880
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004881#if defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004882void mbedtls_ssl_conf_extended_master_secret(mbedtls_ssl_config *conf, char ems)
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +02004883{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02004884 conf->extended_ms = ems;
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +02004885}
4886#endif
4887
Manuel Pégourié-Gonnard66dc5552015-05-14 12:28:21 +02004888#if defined(MBEDTLS_ARC4_C)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004889void mbedtls_ssl_conf_arc4_support(mbedtls_ssl_config *conf, char arc4)
Manuel Pégourié-Gonnardbd47a582015-01-12 13:43:29 +01004890{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02004891 conf->arc4_disabled = arc4;
Manuel Pégourié-Gonnardbd47a582015-01-12 13:43:29 +01004892}
Manuel Pégourié-Gonnard66dc5552015-05-14 12:28:21 +02004893#endif
Manuel Pégourié-Gonnardbd47a582015-01-12 13:43:29 +01004894
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004895#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004896int mbedtls_ssl_conf_max_frag_len(mbedtls_ssl_config *conf, unsigned char mfl_code)
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02004897{
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004898 if (mfl_code >= MBEDTLS_SSL_MAX_FRAG_LEN_INVALID ||
4899 ssl_mfl_code_to_length(mfl_code) > MBEDTLS_TLS_EXT_ADV_CONTENT_LEN) {
4900 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02004901 }
4902
Manuel Pégourié-Gonnard6bf89d62015-05-05 17:01:57 +01004903 conf->mfl_code = mfl_code;
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02004904
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004905 return 0;
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02004906}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004907#endif /* MBEDTLS_SSL_MAX_FRAGMENT_LENGTH */
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02004908
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004909#if defined(MBEDTLS_SSL_TRUNCATED_HMAC)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004910void mbedtls_ssl_conf_truncated_hmac(mbedtls_ssl_config *conf, int truncate)
Manuel Pégourié-Gonnarde980a992013-07-19 11:08:52 +02004911{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02004912 conf->trunc_hmac = truncate;
Manuel Pégourié-Gonnarde980a992013-07-19 11:08:52 +02004913}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004914#endif /* MBEDTLS_SSL_TRUNCATED_HMAC */
Manuel Pégourié-Gonnarde980a992013-07-19 11:08:52 +02004915
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004916#if defined(MBEDTLS_SSL_CBC_RECORD_SPLITTING)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004917void mbedtls_ssl_conf_cbc_record_splitting(mbedtls_ssl_config *conf, char split)
Manuel Pégourié-Gonnardcfa477e2015-01-07 14:50:54 +01004918{
Manuel Pégourié-Gonnard17eab2b2015-05-05 16:34:53 +01004919 conf->cbc_record_splitting = split;
Manuel Pégourié-Gonnardcfa477e2015-01-07 14:50:54 +01004920}
4921#endif
4922
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004923void mbedtls_ssl_conf_legacy_renegotiation(mbedtls_ssl_config *conf, int allow_legacy)
Paul Bakker48916f92012-09-16 19:57:18 +00004924{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02004925 conf->allow_legacy_renegotiation = allow_legacy;
Paul Bakker48916f92012-09-16 19:57:18 +00004926}
4927
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004928#if defined(MBEDTLS_SSL_RENEGOTIATION)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004929void mbedtls_ssl_conf_renegotiation(mbedtls_ssl_config *conf, int renegotiation)
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01004930{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02004931 conf->disable_renegotiation = renegotiation;
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01004932}
4933
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004934void mbedtls_ssl_conf_renegotiation_enforced(mbedtls_ssl_config *conf, int max_records)
Manuel Pégourié-Gonnarda9964db2014-07-03 19:29:16 +02004935{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02004936 conf->renego_max_records = max_records;
Manuel Pégourié-Gonnarda9964db2014-07-03 19:29:16 +02004937}
4938
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004939void mbedtls_ssl_conf_renegotiation_period(mbedtls_ssl_config *conf,
4940 const unsigned char period[8])
Manuel Pégourié-Gonnard837f0fe2014-11-05 13:58:53 +01004941{
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004942 memcpy(conf->renego_period, period, 8);
Manuel Pégourié-Gonnard837f0fe2014-11-05 13:58:53 +01004943}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004944#endif /* MBEDTLS_SSL_RENEGOTIATION */
Paul Bakker5121ce52009-01-03 21:22:43 +00004945
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004946#if defined(MBEDTLS_SSL_SESSION_TICKETS)
Manuel Pégourié-Gonnardb596abf2015-05-20 10:45:29 +02004947#if defined(MBEDTLS_SSL_CLI_C)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004948void mbedtls_ssl_conf_session_tickets(mbedtls_ssl_config *conf, int use_tickets)
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +02004949{
Manuel Pégourié-Gonnard2b494452015-05-06 10:05:11 +01004950 conf->session_tickets = use_tickets;
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +02004951}
Manuel Pégourié-Gonnardb596abf2015-05-20 10:45:29 +02004952#endif
Paul Bakker606b4ba2013-08-14 16:52:14 +02004953
Manuel Pégourié-Gonnardb596abf2015-05-20 10:45:29 +02004954#if defined(MBEDTLS_SSL_SRV_C)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004955void mbedtls_ssl_conf_session_tickets_cb(mbedtls_ssl_config *conf,
4956 mbedtls_ssl_ticket_write_t *f_ticket_write,
4957 mbedtls_ssl_ticket_parse_t *f_ticket_parse,
4958 void *p_ticket)
Paul Bakker606b4ba2013-08-14 16:52:14 +02004959{
Manuel Pégourié-Gonnardd59675d2015-05-19 15:28:00 +02004960 conf->f_ticket_write = f_ticket_write;
4961 conf->f_ticket_parse = f_ticket_parse;
4962 conf->p_ticket = p_ticket;
Paul Bakker606b4ba2013-08-14 16:52:14 +02004963}
Manuel Pégourié-Gonnardb596abf2015-05-20 10:45:29 +02004964#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004965#endif /* MBEDTLS_SSL_SESSION_TICKETS */
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +02004966
Robert Cragie4feb7ae2015-10-02 13:33:37 +01004967#if defined(MBEDTLS_SSL_EXPORT_KEYS)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004968void mbedtls_ssl_conf_export_keys_cb(mbedtls_ssl_config *conf,
4969 mbedtls_ssl_export_keys_t *f_export_keys,
4970 void *p_export_keys)
Robert Cragie4feb7ae2015-10-02 13:33:37 +01004971{
4972 conf->f_export_keys = f_export_keys;
4973 conf->p_export_keys = p_export_keys;
4974}
Ron Eldorf5cc10d2019-05-07 18:33:40 +03004975
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004976void mbedtls_ssl_conf_export_keys_ext_cb(mbedtls_ssl_config *conf,
4977 mbedtls_ssl_export_keys_ext_t *f_export_keys_ext,
4978 void *p_export_keys)
Ron Eldorf5cc10d2019-05-07 18:33:40 +03004979{
4980 conf->f_export_keys_ext = f_export_keys_ext;
4981 conf->p_export_keys = p_export_keys;
4982}
Robert Cragie4feb7ae2015-10-02 13:33:37 +01004983#endif
4984
Gilles Peskineb74a1c72018-04-24 13:09:22 +02004985#if defined(MBEDTLS_SSL_ASYNC_PRIVATE)
Gilles Peskine8bf79f62018-01-05 21:11:53 +01004986void mbedtls_ssl_conf_async_private_cb(
4987 mbedtls_ssl_config *conf,
4988 mbedtls_ssl_async_sign_t *f_async_sign,
4989 mbedtls_ssl_async_decrypt_t *f_async_decrypt,
4990 mbedtls_ssl_async_resume_t *f_async_resume,
4991 mbedtls_ssl_async_cancel_t *f_async_cancel,
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004992 void *async_config_data)
Gilles Peskine8bf79f62018-01-05 21:11:53 +01004993{
4994 conf->f_async_sign_start = f_async_sign;
4995 conf->f_async_decrypt_start = f_async_decrypt;
4996 conf->f_async_resume = f_async_resume;
4997 conf->f_async_cancel = f_async_cancel;
Gilles Peskinedf13d5c2018-04-25 20:39:48 +02004998 conf->p_async_config_data = async_config_data;
4999}
5000
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005001void *mbedtls_ssl_conf_get_async_config_data(const mbedtls_ssl_config *conf)
Gilles Peskine8f97af72018-04-26 11:46:10 +02005002{
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005003 return conf->p_async_config_data;
Gilles Peskine8f97af72018-04-26 11:46:10 +02005004}
5005
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005006void *mbedtls_ssl_get_async_operation_data(const mbedtls_ssl_context *ssl)
Gilles Peskinedf13d5c2018-04-25 20:39:48 +02005007{
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005008 if (ssl->handshake == NULL) {
5009 return NULL;
5010 } else {
5011 return ssl->handshake->user_async_ctx;
5012 }
Gilles Peskinedf13d5c2018-04-25 20:39:48 +02005013}
5014
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005015void mbedtls_ssl_set_async_operation_data(mbedtls_ssl_context *ssl,
5016 void *ctx)
Gilles Peskinedf13d5c2018-04-25 20:39:48 +02005017{
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005018 if (ssl->handshake != NULL) {
Gilles Peskinedf13d5c2018-04-25 20:39:48 +02005019 ssl->handshake->user_async_ctx = ctx;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005020 }
Gilles Peskine8bf79f62018-01-05 21:11:53 +01005021}
Gilles Peskineb74a1c72018-04-24 13:09:22 +02005022#endif /* MBEDTLS_SSL_ASYNC_PRIVATE */
Gilles Peskine8bf79f62018-01-05 21:11:53 +01005023
Paul Bakker5121ce52009-01-03 21:22:43 +00005024/*
5025 * SSL get accessors
5026 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005027uint32_t mbedtls_ssl_get_verify_result(const mbedtls_ssl_context *ssl)
Paul Bakker5121ce52009-01-03 21:22:43 +00005028{
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005029 if (ssl->session != NULL) {
5030 return ssl->session->verify_result;
5031 }
Manuel Pégourié-Gonnarde89163c2015-01-23 14:30:57 +00005032
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005033 if (ssl->session_negotiate != NULL) {
5034 return ssl->session_negotiate->verify_result;
5035 }
Manuel Pégourié-Gonnarde89163c2015-01-23 14:30:57 +00005036
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005037 return 0xFFFFFFFF;
Paul Bakker5121ce52009-01-03 21:22:43 +00005038}
5039
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005040const char *mbedtls_ssl_get_ciphersuite(const mbedtls_ssl_context *ssl)
Paul Bakker72f62662011-01-16 21:27:44 +00005041{
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005042 if (ssl == NULL || ssl->session == NULL) {
5043 return NULL;
5044 }
Paul Bakker926c8e42013-03-06 10:23:34 +01005045
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005046 return mbedtls_ssl_get_ciphersuite_name(ssl->session->ciphersuite);
Paul Bakker72f62662011-01-16 21:27:44 +00005047}
5048
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005049const char *mbedtls_ssl_get_version(const mbedtls_ssl_context *ssl)
Paul Bakker43ca69c2011-01-15 17:35:19 +00005050{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005051#if defined(MBEDTLS_SSL_PROTO_DTLS)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005052 if (ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM) {
5053 switch (ssl->minor_ver) {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005054 case MBEDTLS_SSL_MINOR_VERSION_2:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005055 return "DTLSv1.0";
Manuel Pégourié-Gonnardb21ca2a2014-02-10 13:43:33 +01005056
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005057 case MBEDTLS_SSL_MINOR_VERSION_3:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005058 return "DTLSv1.2";
Manuel Pégourié-Gonnardb21ca2a2014-02-10 13:43:33 +01005059
5060 default:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005061 return "unknown (DTLS)";
Manuel Pégourié-Gonnardb21ca2a2014-02-10 13:43:33 +01005062 }
5063 }
5064#endif
5065
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005066 switch (ssl->minor_ver) {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005067 case MBEDTLS_SSL_MINOR_VERSION_0:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005068 return "SSLv3.0";
Paul Bakker43ca69c2011-01-15 17:35:19 +00005069
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005070 case MBEDTLS_SSL_MINOR_VERSION_1:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005071 return "TLSv1.0";
Paul Bakker43ca69c2011-01-15 17:35:19 +00005072
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005073 case MBEDTLS_SSL_MINOR_VERSION_2:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005074 return "TLSv1.1";
Paul Bakker43ca69c2011-01-15 17:35:19 +00005075
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005076 case MBEDTLS_SSL_MINOR_VERSION_3:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005077 return "TLSv1.2";
Paul Bakker1ef83d62012-04-11 12:09:53 +00005078
Paul Bakker43ca69c2011-01-15 17:35:19 +00005079 default:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005080 return "unknown";
Paul Bakker43ca69c2011-01-15 17:35:19 +00005081 }
Paul Bakker43ca69c2011-01-15 17:35:19 +00005082}
5083
Manuel Pégourié-Gonnarda2cda6b2015-08-31 18:30:52 +02005084#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005085size_t mbedtls_ssl_get_input_max_frag_len(const mbedtls_ssl_context *ssl)
Andrzej Kurek90c6e842020-04-03 05:25:29 -04005086{
5087 size_t max_len = MBEDTLS_SSL_MAX_CONTENT_LEN;
5088 size_t read_mfl;
5089
5090 /* Use the configured MFL for the client if we're past SERVER_HELLO_DONE */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005091 if (ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT &&
5092 ssl->state >= MBEDTLS_SSL_SERVER_HELLO_DONE) {
5093 return ssl_mfl_code_to_length(ssl->conf->mfl_code);
Andrzej Kurek90c6e842020-04-03 05:25:29 -04005094 }
5095
5096 /* Check if a smaller max length was negotiated */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005097 if (ssl->session_out != NULL) {
5098 read_mfl = ssl_mfl_code_to_length(ssl->session_out->mfl_code);
5099 if (read_mfl < max_len) {
Andrzej Kurek90c6e842020-04-03 05:25:29 -04005100 max_len = read_mfl;
5101 }
5102 }
5103
5104 // During a handshake, use the value being negotiated
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005105 if (ssl->session_negotiate != NULL) {
5106 read_mfl = ssl_mfl_code_to_length(ssl->session_negotiate->mfl_code);
5107 if (read_mfl < max_len) {
Andrzej Kurek90c6e842020-04-03 05:25:29 -04005108 max_len = read_mfl;
5109 }
5110 }
5111
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005112 return max_len;
Andrzej Kurek90c6e842020-04-03 05:25:29 -04005113}
5114
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005115size_t mbedtls_ssl_get_output_max_frag_len(const mbedtls_ssl_context *ssl)
Manuel Pégourié-Gonnarda2cda6b2015-08-31 18:30:52 +02005116{
5117 size_t max_len;
5118
5119 /*
5120 * Assume mfl_code is correct since it was checked when set
5121 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005122 max_len = ssl_mfl_code_to_length(ssl->conf->mfl_code);
Manuel Pégourié-Gonnarda2cda6b2015-08-31 18:30:52 +02005123
Manuel Pégourié-Gonnard2cb17e22017-09-19 13:00:47 +02005124 /* Check if a smaller max length was negotiated */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005125 if (ssl->session_out != NULL &&
5126 ssl_mfl_code_to_length(ssl->session_out->mfl_code) < max_len) {
5127 max_len = ssl_mfl_code_to_length(ssl->session_out->mfl_code);
Manuel Pégourié-Gonnarda2cda6b2015-08-31 18:30:52 +02005128 }
5129
Manuel Pégourié-Gonnard2cb17e22017-09-19 13:00:47 +02005130 /* During a handshake, use the value being negotiated */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005131 if (ssl->session_negotiate != NULL &&
5132 ssl_mfl_code_to_length(ssl->session_negotiate->mfl_code) < max_len) {
5133 max_len = ssl_mfl_code_to_length(ssl->session_negotiate->mfl_code);
Manuel Pégourié-Gonnard2cb17e22017-09-19 13:00:47 +02005134 }
5135
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005136 return max_len;
Manuel Pégourié-Gonnarda2cda6b2015-08-31 18:30:52 +02005137}
Andrzej Kurek90c6e842020-04-03 05:25:29 -04005138
5139#if !defined(MBEDTLS_DEPRECATED_REMOVED)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005140size_t mbedtls_ssl_get_max_frag_len(const mbedtls_ssl_context *ssl)
Andrzej Kurek90c6e842020-04-03 05:25:29 -04005141{
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005142 return mbedtls_ssl_get_output_max_frag_len(ssl);
Andrzej Kurek90c6e842020-04-03 05:25:29 -04005143}
5144#endif /* !MBEDTLS_DEPRECATED_REMOVED */
Manuel Pégourié-Gonnarda2cda6b2015-08-31 18:30:52 +02005145#endif /* MBEDTLS_SSL_MAX_FRAGMENT_LENGTH */
5146
Manuel Pégourié-Gonnardb8eec192018-08-20 09:34:02 +02005147#if defined(MBEDTLS_SSL_PROTO_DTLS)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005148size_t mbedtls_ssl_get_current_mtu(const mbedtls_ssl_context *ssl)
Manuel Pégourié-Gonnardb8eec192018-08-20 09:34:02 +02005149{
Andrzej Kurekef43ce62018-10-09 08:24:12 -04005150 /* Return unlimited mtu for client hello messages to avoid fragmentation. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005151 if (ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT &&
5152 (ssl->state == MBEDTLS_SSL_CLIENT_HELLO ||
5153 ssl->state == MBEDTLS_SSL_SERVER_HELLO)) {
5154 return 0;
5155 }
Andrzej Kurekef43ce62018-10-09 08:24:12 -04005156
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005157 if (ssl->handshake == NULL || ssl->handshake->mtu == 0) {
5158 return ssl->mtu;
5159 }
Manuel Pégourié-Gonnardb8eec192018-08-20 09:34:02 +02005160
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005161 if (ssl->mtu == 0) {
5162 return ssl->handshake->mtu;
5163 }
Manuel Pégourié-Gonnardb8eec192018-08-20 09:34:02 +02005164
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005165 return ssl->mtu < ssl->handshake->mtu ?
5166 ssl->mtu : ssl->handshake->mtu;
Manuel Pégourié-Gonnardb8eec192018-08-20 09:34:02 +02005167}
5168#endif /* MBEDTLS_SSL_PROTO_DTLS */
5169
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005170int mbedtls_ssl_get_max_out_record_payload(const mbedtls_ssl_context *ssl)
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02005171{
5172 size_t max_len = MBEDTLS_SSL_OUT_CONTENT_LEN;
5173
Manuel Pégourié-Gonnard000281e2018-08-21 11:20:58 +02005174#if !defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH) && \
5175 !defined(MBEDTLS_SSL_PROTO_DTLS)
5176 (void) ssl;
5177#endif
5178
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02005179#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005180 const size_t mfl = mbedtls_ssl_get_output_max_frag_len(ssl);
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02005181
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005182 if (max_len > mfl) {
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02005183 max_len = mfl;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005184 }
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02005185#endif
5186
5187#if defined(MBEDTLS_SSL_PROTO_DTLS)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005188 if (mbedtls_ssl_get_current_mtu(ssl) != 0) {
5189 const size_t mtu = mbedtls_ssl_get_current_mtu(ssl);
5190 const int ret = mbedtls_ssl_get_record_expansion(ssl);
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02005191 const size_t overhead = (size_t) ret;
5192
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005193 if (ret < 0) {
5194 return ret;
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02005195 }
5196
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005197 if (mtu <= overhead) {
5198 MBEDTLS_SSL_DEBUG_MSG(1, ("MTU too low for record expansion"));
5199 return MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
5200 }
5201
5202 if (max_len > mtu - overhead) {
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02005203 max_len = mtu - overhead;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005204 }
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02005205 }
Manuel Pégourié-Gonnardb8eec192018-08-20 09:34:02 +02005206#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02005207
Hanno Becker0defedb2018-08-10 12:35:02 +01005208#if !defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH) && \
5209 !defined(MBEDTLS_SSL_PROTO_DTLS)
5210 ((void) ssl);
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02005211#endif
5212
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005213 return (int) max_len;
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02005214}
5215
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005216#if defined(MBEDTLS_X509_CRT_PARSE_C)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005217const mbedtls_x509_crt *mbedtls_ssl_get_peer_cert(const mbedtls_ssl_context *ssl)
Paul Bakkerb0550d92012-10-30 07:51:03 +00005218{
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005219 if (ssl == NULL || ssl->session == NULL) {
5220 return NULL;
5221 }
Paul Bakkerb0550d92012-10-30 07:51:03 +00005222
Hanno Beckere6824572019-02-07 13:18:46 +00005223#if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005224 return ssl->session->peer_cert;
Hanno Beckere6824572019-02-07 13:18:46 +00005225#else
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005226 return NULL;
Hanno Beckere6824572019-02-07 13:18:46 +00005227#endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
Paul Bakkerb0550d92012-10-30 07:51:03 +00005228}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005229#endif /* MBEDTLS_X509_CRT_PARSE_C */
Paul Bakkerb0550d92012-10-30 07:51:03 +00005230
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005231#if defined(MBEDTLS_SSL_CLI_C)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005232int mbedtls_ssl_get_session(const mbedtls_ssl_context *ssl,
5233 mbedtls_ssl_session *dst)
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02005234{
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005235 if (ssl == NULL ||
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02005236 dst == NULL ||
5237 ssl->session == NULL ||
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005238 ssl->conf->endpoint != MBEDTLS_SSL_IS_CLIENT) {
5239 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02005240 }
5241
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005242 return mbedtls_ssl_session_copy(dst, ssl->session);
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02005243}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005244#endif /* MBEDTLS_SSL_CLI_C */
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02005245
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005246const mbedtls_ssl_session *mbedtls_ssl_get_session_pointer(const mbedtls_ssl_context *ssl)
Manuel Pégourié-Gonnardb5e4e0a2019-05-20 11:12:28 +02005247{
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005248 if (ssl == NULL) {
5249 return NULL;
5250 }
Manuel Pégourié-Gonnardb5e4e0a2019-05-20 11:12:28 +02005251
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005252 return ssl->session;
Manuel Pégourié-Gonnardb5e4e0a2019-05-20 11:12:28 +02005253}
5254
Paul Bakker5121ce52009-01-03 21:22:43 +00005255/*
Hanno Beckera835da52019-05-16 12:39:07 +01005256 * Define ticket header determining Mbed TLS version
5257 * and structure of the ticket.
5258 */
5259
Hanno Becker94ef3b32019-05-16 12:50:45 +01005260/*
Hanno Becker50b59662019-05-28 14:30:45 +01005261 * Define bitflag determining compile-time settings influencing
5262 * structure of serialized SSL sessions.
Hanno Becker94ef3b32019-05-16 12:50:45 +01005263 */
5264
Hanno Becker50b59662019-05-28 14:30:45 +01005265#if defined(MBEDTLS_HAVE_TIME)
Hanno Becker3e088662019-05-29 11:10:18 +01005266#define SSL_SERIALIZED_SESSION_CONFIG_TIME 1
Hanno Becker50b59662019-05-28 14:30:45 +01005267#else
Hanno Becker3e088662019-05-29 11:10:18 +01005268#define SSL_SERIALIZED_SESSION_CONFIG_TIME 0
Hanno Becker94ef3b32019-05-16 12:50:45 +01005269#endif /* MBEDTLS_HAVE_TIME */
5270
5271#if defined(MBEDTLS_X509_CRT_PARSE_C)
Hanno Becker3e088662019-05-29 11:10:18 +01005272#define SSL_SERIALIZED_SESSION_CONFIG_CRT 1
Hanno Becker94ef3b32019-05-16 12:50:45 +01005273#else
Hanno Becker3e088662019-05-29 11:10:18 +01005274#define SSL_SERIALIZED_SESSION_CONFIG_CRT 0
Hanno Becker94ef3b32019-05-16 12:50:45 +01005275#endif /* MBEDTLS_X509_CRT_PARSE_C */
5276
David Horstmanneb77b6f2024-02-13 17:53:35 +00005277#if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
David Horstmann11def972024-03-01 11:20:32 +00005278#define SSL_SERIALIZED_SESSION_CONFIG_KEEP_PEER_CRT 1
David Horstmanneb77b6f2024-02-13 17:53:35 +00005279#else
David Horstmann11def972024-03-01 11:20:32 +00005280#define SSL_SERIALIZED_SESSION_CONFIG_KEEP_PEER_CRT 0
David Horstmanneb77b6f2024-02-13 17:53:35 +00005281#endif /* MBEDTLS_SSL_SESSION_TICKETS */
5282
Hanno Becker94ef3b32019-05-16 12:50:45 +01005283#if defined(MBEDTLS_SSL_CLI_C) && defined(MBEDTLS_SSL_SESSION_TICKETS)
Hanno Becker3e088662019-05-29 11:10:18 +01005284#define SSL_SERIALIZED_SESSION_CONFIG_CLIENT_TICKET 1
Hanno Becker94ef3b32019-05-16 12:50:45 +01005285#else
Hanno Becker3e088662019-05-29 11:10:18 +01005286#define SSL_SERIALIZED_SESSION_CONFIG_CLIENT_TICKET 0
Hanno Becker94ef3b32019-05-16 12:50:45 +01005287#endif /* MBEDTLS_SSL_CLI_C && MBEDTLS_SSL_SESSION_TICKETS */
5288
5289#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
Hanno Becker3e088662019-05-29 11:10:18 +01005290#define SSL_SERIALIZED_SESSION_CONFIG_MFL 1
Hanno Becker94ef3b32019-05-16 12:50:45 +01005291#else
Hanno Becker3e088662019-05-29 11:10:18 +01005292#define SSL_SERIALIZED_SESSION_CONFIG_MFL 0
Hanno Becker94ef3b32019-05-16 12:50:45 +01005293#endif /* MBEDTLS_SSL_MAX_FRAGMENT_LENGTH */
5294
5295#if defined(MBEDTLS_SSL_TRUNCATED_HMAC)
Hanno Becker3e088662019-05-29 11:10:18 +01005296#define SSL_SERIALIZED_SESSION_CONFIG_TRUNC_HMAC 1
Hanno Becker94ef3b32019-05-16 12:50:45 +01005297#else
Hanno Becker3e088662019-05-29 11:10:18 +01005298#define SSL_SERIALIZED_SESSION_CONFIG_TRUNC_HMAC 0
Hanno Becker94ef3b32019-05-16 12:50:45 +01005299#endif /* MBEDTLS_SSL_TRUNCATED_HMAC */
5300
5301#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
Hanno Becker3e088662019-05-29 11:10:18 +01005302#define SSL_SERIALIZED_SESSION_CONFIG_ETM 1
Hanno Becker94ef3b32019-05-16 12:50:45 +01005303#else
Hanno Becker3e088662019-05-29 11:10:18 +01005304#define SSL_SERIALIZED_SESSION_CONFIG_ETM 0
Hanno Becker94ef3b32019-05-16 12:50:45 +01005305#endif /* MBEDTLS_SSL_ENCRYPT_THEN_MAC */
5306
Hanno Becker94ef3b32019-05-16 12:50:45 +01005307#if defined(MBEDTLS_SSL_SESSION_TICKETS)
5308#define SSL_SERIALIZED_SESSION_CONFIG_TICKET 1
5309#else
5310#define SSL_SERIALIZED_SESSION_CONFIG_TICKET 0
5311#endif /* MBEDTLS_SSL_SESSION_TICKETS */
5312
Hanno Becker3e088662019-05-29 11:10:18 +01005313#define SSL_SERIALIZED_SESSION_CONFIG_TIME_BIT 0
5314#define SSL_SERIALIZED_SESSION_CONFIG_CRT_BIT 1
5315#define SSL_SERIALIZED_SESSION_CONFIG_CLIENT_TICKET_BIT 2
5316#define SSL_SERIALIZED_SESSION_CONFIG_MFL_BIT 3
5317#define SSL_SERIALIZED_SESSION_CONFIG_TRUNC_HMAC_BIT 4
5318#define SSL_SERIALIZED_SESSION_CONFIG_ETM_BIT 5
5319#define SSL_SERIALIZED_SESSION_CONFIG_TICKET_BIT 6
David Horstmann11def972024-03-01 11:20:32 +00005320#define SSL_SERIALIZED_SESSION_CONFIG_KEEP_PEER_CRT_BIT 7
Hanno Becker3e088662019-05-29 11:10:18 +01005321
Hanno Becker50b59662019-05-28 14:30:45 +01005322#define SSL_SERIALIZED_SESSION_CONFIG_BITFLAG \
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005323 ((uint16_t) ( \
5324 (SSL_SERIALIZED_SESSION_CONFIG_TIME << SSL_SERIALIZED_SESSION_CONFIG_TIME_BIT) | \
5325 (SSL_SERIALIZED_SESSION_CONFIG_CRT << SSL_SERIALIZED_SESSION_CONFIG_CRT_BIT) | \
5326 (SSL_SERIALIZED_SESSION_CONFIG_CLIENT_TICKET << \
5327 SSL_SERIALIZED_SESSION_CONFIG_CLIENT_TICKET_BIT) | \
5328 (SSL_SERIALIZED_SESSION_CONFIG_MFL << SSL_SERIALIZED_SESSION_CONFIG_MFL_BIT) | \
5329 (SSL_SERIALIZED_SESSION_CONFIG_TRUNC_HMAC << \
5330 SSL_SERIALIZED_SESSION_CONFIG_TRUNC_HMAC_BIT) | \
5331 (SSL_SERIALIZED_SESSION_CONFIG_ETM << SSL_SERIALIZED_SESSION_CONFIG_ETM_BIT) | \
David Horstmanneb77b6f2024-02-13 17:53:35 +00005332 (SSL_SERIALIZED_SESSION_CONFIG_TICKET << SSL_SERIALIZED_SESSION_CONFIG_TICKET_BIT) | \
David Horstmannf5a6fa22024-03-01 12:31:35 +00005333 (SSL_SERIALIZED_SESSION_CONFIG_KEEP_PEER_CRT << \
5334 SSL_SERIALIZED_SESSION_CONFIG_KEEP_PEER_CRT_BIT)))
Hanno Becker94ef3b32019-05-16 12:50:45 +01005335
Chien Wongb6d57932024-02-07 21:48:12 +08005336static const unsigned char ssl_serialized_session_header[] = {
Hanno Becker94ef3b32019-05-16 12:50:45 +01005337 MBEDTLS_VERSION_MAJOR,
5338 MBEDTLS_VERSION_MINOR,
5339 MBEDTLS_VERSION_PATCH,
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005340 MBEDTLS_BYTE_1(SSL_SERIALIZED_SESSION_CONFIG_BITFLAG),
5341 MBEDTLS_BYTE_0(SSL_SERIALIZED_SESSION_CONFIG_BITFLAG),
Hanno Beckerf8787072019-05-16 12:41:07 +01005342};
Hanno Beckera835da52019-05-16 12:39:07 +01005343
5344/*
Manuel Pégourié-Gonnarda3e7c652019-05-16 10:08:35 +02005345 * Serialize a session in the following format:
Manuel Pégourié-Gonnard35eb8022019-05-16 11:11:08 +02005346 * (in the presentation language of TLS, RFC 8446 section 3)
Manuel Pégourié-Gonnarda3e7c652019-05-16 10:08:35 +02005347 *
Hanno Becker50b59662019-05-28 14:30:45 +01005348 * opaque mbedtls_version[3]; // major, minor, patch
5349 * opaque session_format[2]; // version-specific 16-bit field determining
5350 * // the format of the remaining
5351 * // serialized data.
Hanno Beckerdc28b6c2019-05-29 11:08:00 +01005352 *
5353 * Note: When updating the format, remember to keep
5354 * these version+format bytes.
5355 *
Hanno Beckerbe34e8e2019-06-04 09:43:16 +01005356 * // In this version, `session_format` determines
5357 * // the setting of those compile-time
5358 * // configuration options which influence
Hanno Becker50b59662019-05-28 14:30:45 +01005359 * // the structure of mbedtls_ssl_session.
David Horstmann363db772024-03-01 12:11:24 +00005360 * #if defined(MBEDTLS_HAVE_TIME)
5361 * uint64 start_time;
5362 * #endif
5363 * uint8 ciphersuite[2]; // defined by the standard
5364 * uint8 compression; // 0 or 1
5365 * uint8 session_id_len; // at most 32
5366 * opaque session_id[32];
5367 * opaque master[48]; // fixed length in the standard
5368 * uint32 verify_result;
David Horstmannfc8cacf2024-03-04 16:37:02 +00005369 * #if defined(MBEDTLS_X509_CRT_PARSE_C)
David Horstmann363db772024-03-01 12:11:24 +00005370 * #if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
5371 * opaque peer_cert<0..2^24-1>; // length 0 means no peer cert
5372 * #else
5373 * uint8 peer_cert_digest_type;
5374 * opaque peer_cert_digest<0..2^8-1>
5375 * #endif
David Horstmannfc8cacf2024-03-04 16:37:02 +00005376 * #endif
5377 * #if defined(MBEDTLS_SSL_SESSION_TICKETS) && defined(MBEDTLS_SSL_CLI_C)
David Horstmann363db772024-03-01 12:11:24 +00005378 * opaque ticket<0..2^24-1>; // length 0 means no ticket
5379 * uint32 ticket_lifetime;
5380 * #endif
5381 * #if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
5382 * uint8 mfl_code; // up to 255 according to standard
5383 * #endif
5384 * #if defined(MBEDTLS_SSL_TRUNCATED_HMAC)
5385 * uint8 trunc_hmac; // 0 or 1
5386 * #endif
5387 * #if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
5388 * uint8 encrypt_then_mac; // 0 or 1
5389 * #endif
Manuel Pégourié-Gonnarda3e7c652019-05-16 10:08:35 +02005390 *
Manuel Pégourié-Gonnardf743c032019-05-24 12:06:29 +02005391 * The order is the same as in the definition of the structure, except
5392 * verify_result is put before peer_cert so that all mandatory fields come
5393 * together in one block.
Manuel Pégourié-Gonnarda3e7c652019-05-16 10:08:35 +02005394 */
Manuel Pégourié-Gonnardd904d662022-06-17 10:24:00 +02005395MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005396static int ssl_session_save(const mbedtls_ssl_session *session,
5397 unsigned char omit_header,
5398 unsigned char *buf,
5399 size_t buf_len,
5400 size_t *olen)
Manuel Pégourié-Gonnarda3e7c652019-05-16 10:08:35 +02005401{
5402 unsigned char *p = buf;
Manuel Pégourié-Gonnard26f982f2019-05-21 11:01:32 +02005403 size_t used = 0;
Manuel Pégourié-Gonnardf743c032019-05-24 12:06:29 +02005404#if defined(MBEDTLS_HAVE_TIME)
5405 uint64_t start;
5406#endif
Manuel Pégourié-Gonnarda3e7c652019-05-16 10:08:35 +02005407#if defined(MBEDTLS_X509_CRT_PARSE_C)
5408#if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
5409 size_t cert_len;
Manuel Pégourié-Gonnarda3e7c652019-05-16 10:08:35 +02005410#endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
5411#endif /* MBEDTLS_X509_CRT_PARSE_C */
5412
Manuel Pégourié-Gonnarda3e7c652019-05-16 10:08:35 +02005413
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005414 if (!omit_header) {
Manuel Pégourié-Gonnard45ac1f02019-07-23 16:52:45 +02005415 /*
5416 * Add version identifier
5417 */
5418
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005419 used += sizeof(ssl_serialized_session_header);
Manuel Pégourié-Gonnard45ac1f02019-07-23 16:52:45 +02005420
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005421 if (used <= buf_len) {
5422 memcpy(p, ssl_serialized_session_header,
5423 sizeof(ssl_serialized_session_header));
5424 p += sizeof(ssl_serialized_session_header);
Manuel Pégourié-Gonnard45ac1f02019-07-23 16:52:45 +02005425 }
Hanno Beckera835da52019-05-16 12:39:07 +01005426 }
5427
5428 /*
Manuel Pégourié-Gonnardf743c032019-05-24 12:06:29 +02005429 * Time
5430 */
5431#if defined(MBEDTLS_HAVE_TIME)
5432 used += 8;
5433
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005434 if (used <= buf_len) {
Manuel Pégourié-Gonnardf743c032019-05-24 12:06:29 +02005435 start = (uint64_t) session->start;
5436
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005437 MBEDTLS_PUT_UINT64_BE(start, p, 0);
Joe Subbiani2f98d792021-08-20 11:44:44 +01005438 p += 8;
Manuel Pégourié-Gonnardf743c032019-05-24 12:06:29 +02005439 }
5440#endif /* MBEDTLS_HAVE_TIME */
5441
5442 /*
5443 * Basic mandatory fields
5444 */
5445 used += 2 /* ciphersuite */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005446 + 1 /* compression */
5447 + 1 /* id_len */
5448 + sizeof(session->id)
5449 + sizeof(session->master)
5450 + 4; /* verify_result */
Manuel Pégourié-Gonnardf743c032019-05-24 12:06:29 +02005451
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005452 if (used <= buf_len) {
5453 MBEDTLS_PUT_UINT16_BE(session->ciphersuite, p, 0);
Joe Subbiani2f98d792021-08-20 11:44:44 +01005454 p += 2;
Manuel Pégourié-Gonnardf743c032019-05-24 12:06:29 +02005455
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005456 *p++ = MBEDTLS_BYTE_0(session->compression);
Manuel Pégourié-Gonnardf743c032019-05-24 12:06:29 +02005457
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005458 *p++ = MBEDTLS_BYTE_0(session->id_len);
5459 memcpy(p, session->id, 32);
Manuel Pégourié-Gonnardf743c032019-05-24 12:06:29 +02005460 p += 32;
5461
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005462 memcpy(p, session->master, 48);
Manuel Pégourié-Gonnardf743c032019-05-24 12:06:29 +02005463 p += 48;
5464
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005465 MBEDTLS_PUT_UINT32_BE(session->verify_result, p, 0);
Joe Subbiani2f98d792021-08-20 11:44:44 +01005466 p += 4;
Manuel Pégourié-Gonnard26f982f2019-05-21 11:01:32 +02005467 }
Manuel Pégourié-Gonnarda3e7c652019-05-16 10:08:35 +02005468
Manuel Pégourié-Gonnard35eb8022019-05-16 11:11:08 +02005469 /*
Manuel Pégourié-Gonnardf743c032019-05-24 12:06:29 +02005470 * Peer's end-entity certificate
Manuel Pégourié-Gonnard35eb8022019-05-16 11:11:08 +02005471 */
Manuel Pégourié-Gonnarda3e7c652019-05-16 10:08:35 +02005472#if defined(MBEDTLS_X509_CRT_PARSE_C)
5473#if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005474 if (session->peer_cert == NULL) {
Manuel Pégourié-Gonnarda3e7c652019-05-16 10:08:35 +02005475 cert_len = 0;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005476 } else {
Manuel Pégourié-Gonnarda3e7c652019-05-16 10:08:35 +02005477 cert_len = session->peer_cert->raw.len;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005478 }
Manuel Pégourié-Gonnarda3e7c652019-05-16 10:08:35 +02005479
Manuel Pégourié-Gonnard26f982f2019-05-21 11:01:32 +02005480 used += 3 + cert_len;
Manuel Pégourié-Gonnarda3e7c652019-05-16 10:08:35 +02005481
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005482 if (used <= buf_len) {
5483 *p++ = MBEDTLS_BYTE_2(cert_len);
5484 *p++ = MBEDTLS_BYTE_1(cert_len);
5485 *p++ = MBEDTLS_BYTE_0(cert_len);
Manuel Pégourié-Gonnarda3e7c652019-05-16 10:08:35 +02005486
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005487 if (session->peer_cert != NULL) {
5488 memcpy(p, session->peer_cert->raw.p, cert_len);
Manuel Pégourié-Gonnard26f982f2019-05-21 11:01:32 +02005489 p += cert_len;
5490 }
5491 }
Manuel Pégourié-Gonnarda3e7c652019-05-16 10:08:35 +02005492#else /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005493 if (session->peer_cert_digest != NULL) {
Manuel Pégourié-Gonnardf743c032019-05-24 12:06:29 +02005494 used += 1 /* type */ + 1 /* length */ + session->peer_cert_digest_len;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005495 if (used <= buf_len) {
Manuel Pégourié-Gonnardf743c032019-05-24 12:06:29 +02005496 *p++ = (unsigned char) session->peer_cert_digest_type;
5497 *p++ = (unsigned char) session->peer_cert_digest_len;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005498 memcpy(p, session->peer_cert_digest,
5499 session->peer_cert_digest_len);
Manuel Pégourié-Gonnardf743c032019-05-24 12:06:29 +02005500 p += session->peer_cert_digest_len;
5501 }
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005502 } else {
Manuel Pégourié-Gonnardf743c032019-05-24 12:06:29 +02005503 used += 2;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005504 if (used <= buf_len) {
Manuel Pégourié-Gonnardf743c032019-05-24 12:06:29 +02005505 *p++ = (unsigned char) MBEDTLS_MD_NONE;
5506 *p++ = 0;
5507 }
Manuel Pégourié-Gonnard26f982f2019-05-21 11:01:32 +02005508 }
Manuel Pégourié-Gonnarda3e7c652019-05-16 10:08:35 +02005509#endif /* !MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
5510#endif /* MBEDTLS_X509_CRT_PARSE_C */
5511
Manuel Pégourié-Gonnard35eb8022019-05-16 11:11:08 +02005512 /*
Manuel Pégourié-Gonnardf743c032019-05-24 12:06:29 +02005513 * Session ticket if any, plus associated data
Manuel Pégourié-Gonnard35eb8022019-05-16 11:11:08 +02005514 */
5515#if defined(MBEDTLS_SSL_SESSION_TICKETS) && defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnardf743c032019-05-24 12:06:29 +02005516 used += 3 + session->ticket_len + 4; /* len + ticket + lifetime */
Manuel Pégourié-Gonnard35eb8022019-05-16 11:11:08 +02005517
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005518 if (used <= buf_len) {
5519 *p++ = MBEDTLS_BYTE_2(session->ticket_len);
5520 *p++ = MBEDTLS_BYTE_1(session->ticket_len);
5521 *p++ = MBEDTLS_BYTE_0(session->ticket_len);
Manuel Pégourié-Gonnard26f982f2019-05-21 11:01:32 +02005522
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005523 if (session->ticket != NULL) {
5524 memcpy(p, session->ticket, session->ticket_len);
Manuel Pégourié-Gonnard26f982f2019-05-21 11:01:32 +02005525 p += session->ticket_len;
5526 }
Manuel Pégourié-Gonnardf743c032019-05-24 12:06:29 +02005527
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005528 MBEDTLS_PUT_UINT32_BE(session->ticket_lifetime, p, 0);
Joe Subbiani2f98d792021-08-20 11:44:44 +01005529 p += 4;
Manuel Pégourié-Gonnard35eb8022019-05-16 11:11:08 +02005530 }
5531#endif /* MBEDTLS_SSL_SESSION_TICKETS && MBEDTLS_SSL_CLI_C */
5532
Manuel Pégourié-Gonnardf743c032019-05-24 12:06:29 +02005533 /*
5534 * Misc extension-related info
5535 */
5536#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
5537 used += 1;
5538
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005539 if (used <= buf_len) {
Manuel Pégourié-Gonnardf743c032019-05-24 12:06:29 +02005540 *p++ = session->mfl_code;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005541 }
Manuel Pégourié-Gonnardf743c032019-05-24 12:06:29 +02005542#endif
5543
5544#if defined(MBEDTLS_SSL_TRUNCATED_HMAC)
5545 used += 1;
5546
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005547 if (used <= buf_len) {
5548 *p++ = (unsigned char) ((session->trunc_hmac) & 0xFF);
5549 }
Manuel Pégourié-Gonnardf743c032019-05-24 12:06:29 +02005550#endif
5551
5552#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
5553 used += 1;
5554
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005555 if (used <= buf_len) {
5556 *p++ = MBEDTLS_BYTE_0(session->encrypt_then_mac);
5557 }
Manuel Pégourié-Gonnardf743c032019-05-24 12:06:29 +02005558#endif
5559
Manuel Pégourié-Gonnard35eb8022019-05-16 11:11:08 +02005560 /* Done */
Manuel Pégourié-Gonnard26f982f2019-05-21 11:01:32 +02005561 *olen = used;
5562
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005563 if (used > buf_len) {
5564 return MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL;
5565 }
Manuel Pégourié-Gonnarda3e7c652019-05-16 10:08:35 +02005566
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005567 return 0;
Manuel Pégourié-Gonnarda3e7c652019-05-16 10:08:35 +02005568}
5569
5570/*
Manuel Pégourié-Gonnard45ac1f02019-07-23 16:52:45 +02005571 * Public wrapper for ssl_session_save()
5572 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005573int mbedtls_ssl_session_save(const mbedtls_ssl_session *session,
5574 unsigned char *buf,
5575 size_t buf_len,
5576 size_t *olen)
Manuel Pégourié-Gonnard45ac1f02019-07-23 16:52:45 +02005577{
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005578 return ssl_session_save(session, 0, buf, buf_len, olen);
Manuel Pégourié-Gonnard45ac1f02019-07-23 16:52:45 +02005579}
5580
5581/*
Manuel Pégourié-Gonnardb9dfc9f2019-07-12 10:50:19 +02005582 * Deserialize session, see mbedtls_ssl_session_save() for format.
Manuel Pégourié-Gonnarda3d831b2019-05-23 12:28:45 +02005583 *
5584 * This internal version is wrapped by a public function that cleans up in
Manuel Pégourié-Gonnard45ac1f02019-07-23 16:52:45 +02005585 * case of error, and has an extra option omit_header.
Manuel Pégourié-Gonnarda3e7c652019-05-16 10:08:35 +02005586 */
Manuel Pégourié-Gonnardd904d662022-06-17 10:24:00 +02005587MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005588static int ssl_session_load(mbedtls_ssl_session *session,
5589 unsigned char omit_header,
5590 const unsigned char *buf,
5591 size_t len)
Manuel Pégourié-Gonnarda3e7c652019-05-16 10:08:35 +02005592{
5593 const unsigned char *p = buf;
5594 const unsigned char * const end = buf + len;
Manuel Pégourié-Gonnardf743c032019-05-24 12:06:29 +02005595#if defined(MBEDTLS_HAVE_TIME)
5596 uint64_t start;
5597#endif
Manuel Pégourié-Gonnarda3e7c652019-05-16 10:08:35 +02005598#if defined(MBEDTLS_X509_CRT_PARSE_C)
5599#if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
5600 size_t cert_len;
Manuel Pégourié-Gonnarda3e7c652019-05-16 10:08:35 +02005601#endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
5602#endif /* MBEDTLS_X509_CRT_PARSE_C */
5603
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005604 if (!omit_header) {
Manuel Pégourié-Gonnard45ac1f02019-07-23 16:52:45 +02005605 /*
5606 * Check version identifier
5607 */
5608
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005609 if ((size_t) (end - p) < sizeof(ssl_serialized_session_header)) {
5610 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
Manuel Pégourié-Gonnard45ac1f02019-07-23 16:52:45 +02005611 }
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005612
5613 if (memcmp(p, ssl_serialized_session_header,
5614 sizeof(ssl_serialized_session_header)) != 0) {
5615 return MBEDTLS_ERR_SSL_VERSION_MISMATCH;
5616 }
5617 p += sizeof(ssl_serialized_session_header);
Hanno Beckera835da52019-05-16 12:39:07 +01005618 }
Hanno Beckera835da52019-05-16 12:39:07 +01005619
5620 /*
Manuel Pégourié-Gonnardf743c032019-05-24 12:06:29 +02005621 * Time
Manuel Pégourié-Gonnard35eb8022019-05-16 11:11:08 +02005622 */
Manuel Pégourié-Gonnardf743c032019-05-24 12:06:29 +02005623#if defined(MBEDTLS_HAVE_TIME)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005624 if (8 > (size_t) (end - p)) {
5625 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
5626 }
Manuel Pégourié-Gonnarda3e7c652019-05-16 10:08:35 +02005627
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005628 start = ((uint64_t) p[0] << 56) |
5629 ((uint64_t) p[1] << 48) |
5630 ((uint64_t) p[2] << 40) |
5631 ((uint64_t) p[3] << 32) |
5632 ((uint64_t) p[4] << 24) |
5633 ((uint64_t) p[5] << 16) |
5634 ((uint64_t) p[6] << 8) |
5635 ((uint64_t) p[7]);
Manuel Pégourié-Gonnardf743c032019-05-24 12:06:29 +02005636 p += 8;
Manuel Pégourié-Gonnarda3e7c652019-05-16 10:08:35 +02005637
Manuel Pégourié-Gonnardf743c032019-05-24 12:06:29 +02005638 session->start = (time_t) start;
5639#endif /* MBEDTLS_HAVE_TIME */
5640
5641 /*
5642 * Basic mandatory fields
5643 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005644 if (2 + 1 + 1 + 32 + 48 + 4 > (size_t) (end - p)) {
5645 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
5646 }
Manuel Pégourié-Gonnardf743c032019-05-24 12:06:29 +02005647
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005648 session->ciphersuite = (p[0] << 8) | p[1];
Manuel Pégourié-Gonnardf743c032019-05-24 12:06:29 +02005649 p += 2;
5650
5651 session->compression = *p++;
5652
5653 session->id_len = *p++;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005654 memcpy(session->id, p, 32);
Manuel Pégourié-Gonnardf743c032019-05-24 12:06:29 +02005655 p += 32;
5656
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005657 memcpy(session->master, p, 48);
Manuel Pégourié-Gonnardf743c032019-05-24 12:06:29 +02005658 p += 48;
5659
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005660 session->verify_result = ((uint32_t) p[0] << 24) |
5661 ((uint32_t) p[1] << 16) |
5662 ((uint32_t) p[2] << 8) |
5663 ((uint32_t) p[3]);
Manuel Pégourié-Gonnardf743c032019-05-24 12:06:29 +02005664 p += 4;
5665
5666 /* Immediately clear invalid pointer values that have been read, in case
5667 * we exit early before we replaced them with valid ones. */
Manuel Pégourié-Gonnarda3e7c652019-05-16 10:08:35 +02005668#if defined(MBEDTLS_X509_CRT_PARSE_C)
5669#if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
5670 session->peer_cert = NULL;
5671#else
5672 session->peer_cert_digest = NULL;
5673#endif /* !MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
5674#endif /* MBEDTLS_X509_CRT_PARSE_C */
5675#if defined(MBEDTLS_SSL_SESSION_TICKETS) && defined(MBEDTLS_SSL_CLI_C)
5676 session->ticket = NULL;
5677#endif /* MBEDTLS_SSL_SESSION_TICKETS && MBEDTLS_SSL_CLI_C */
5678
Manuel Pégourié-Gonnard35eb8022019-05-16 11:11:08 +02005679 /*
5680 * Peer certificate
5681 */
Manuel Pégourié-Gonnarda3e7c652019-05-16 10:08:35 +02005682#if defined(MBEDTLS_X509_CRT_PARSE_C)
5683#if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
5684 /* Deserialize CRT from the end of the ticket. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005685 if (3 > (size_t) (end - p)) {
5686 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
5687 }
Manuel Pégourié-Gonnarda3e7c652019-05-16 10:08:35 +02005688
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005689 cert_len = (p[0] << 16) | (p[1] << 8) | p[2];
Manuel Pégourié-Gonnarda3e7c652019-05-16 10:08:35 +02005690 p += 3;
5691
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005692 if (cert_len != 0) {
Janos Follath865b3eb2019-12-16 11:46:15 +00005693 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnarda3e7c652019-05-16 10:08:35 +02005694
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005695 if (cert_len > (size_t) (end - p)) {
5696 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
5697 }
Manuel Pégourié-Gonnarda3e7c652019-05-16 10:08:35 +02005698
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005699 session->peer_cert = mbedtls_calloc(1, sizeof(mbedtls_x509_crt));
Manuel Pégourié-Gonnarda3e7c652019-05-16 10:08:35 +02005700
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005701 if (session->peer_cert == NULL) {
5702 return MBEDTLS_ERR_SSL_ALLOC_FAILED;
5703 }
Manuel Pégourié-Gonnarda3e7c652019-05-16 10:08:35 +02005704
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005705 mbedtls_x509_crt_init(session->peer_cert);
Manuel Pégourié-Gonnarda3e7c652019-05-16 10:08:35 +02005706
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005707 if ((ret = mbedtls_x509_crt_parse_der(session->peer_cert,
5708 p, cert_len)) != 0) {
5709 mbedtls_x509_crt_free(session->peer_cert);
5710 mbedtls_free(session->peer_cert);
Manuel Pégourié-Gonnarda3e7c652019-05-16 10:08:35 +02005711 session->peer_cert = NULL;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005712 return ret;
Manuel Pégourié-Gonnarda3e7c652019-05-16 10:08:35 +02005713 }
5714
5715 p += cert_len;
5716 }
5717#else /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
5718 /* Deserialize CRT digest from the end of the ticket. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005719 if (2 > (size_t) (end - p)) {
5720 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
5721 }
Manuel Pégourié-Gonnarda3e7c652019-05-16 10:08:35 +02005722
Manuel Pégourié-Gonnardf743c032019-05-24 12:06:29 +02005723 session->peer_cert_digest_type = (mbedtls_md_type_t) *p++;
5724 session->peer_cert_digest_len = (size_t) *p++;
Manuel Pégourié-Gonnarda3e7c652019-05-16 10:08:35 +02005725
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005726 if (session->peer_cert_digest_len != 0) {
Manuel Pégourié-Gonnardf743c032019-05-24 12:06:29 +02005727 const mbedtls_md_info_t *md_info =
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005728 mbedtls_md_info_from_type(session->peer_cert_digest_type);
5729 if (md_info == NULL) {
5730 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
5731 }
5732 if (session->peer_cert_digest_len != mbedtls_md_get_size(md_info)) {
5733 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
5734 }
Manuel Pégourié-Gonnarda3e7c652019-05-16 10:08:35 +02005735
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005736 if (session->peer_cert_digest_len > (size_t) (end - p)) {
5737 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
5738 }
Manuel Pégourié-Gonnardf743c032019-05-24 12:06:29 +02005739
5740 session->peer_cert_digest =
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005741 mbedtls_calloc(1, session->peer_cert_digest_len);
5742 if (session->peer_cert_digest == NULL) {
5743 return MBEDTLS_ERR_SSL_ALLOC_FAILED;
5744 }
Manuel Pégourié-Gonnarda3e7c652019-05-16 10:08:35 +02005745
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005746 memcpy(session->peer_cert_digest, p,
5747 session->peer_cert_digest_len);
Manuel Pégourié-Gonnardf743c032019-05-24 12:06:29 +02005748 p += session->peer_cert_digest_len;
Manuel Pégourié-Gonnarda3e7c652019-05-16 10:08:35 +02005749 }
5750#endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
5751#endif /* MBEDTLS_X509_CRT_PARSE_C */
5752
Manuel Pégourié-Gonnard35eb8022019-05-16 11:11:08 +02005753 /*
Manuel Pégourié-Gonnardf743c032019-05-24 12:06:29 +02005754 * Session ticket and associated data
Manuel Pégourié-Gonnard35eb8022019-05-16 11:11:08 +02005755 */
5756#if defined(MBEDTLS_SSL_SESSION_TICKETS) && defined(MBEDTLS_SSL_CLI_C)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005757 if (3 > (size_t) (end - p)) {
5758 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
5759 }
Manuel Pégourié-Gonnard35eb8022019-05-16 11:11:08 +02005760
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005761 session->ticket_len = (p[0] << 16) | (p[1] << 8) | p[2];
Manuel Pégourié-Gonnard35eb8022019-05-16 11:11:08 +02005762 p += 3;
5763
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005764 if (session->ticket_len != 0) {
5765 if (session->ticket_len > (size_t) (end - p)) {
5766 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
5767 }
Manuel Pégourié-Gonnard35eb8022019-05-16 11:11:08 +02005768
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005769 session->ticket = mbedtls_calloc(1, session->ticket_len);
5770 if (session->ticket == NULL) {
5771 return MBEDTLS_ERR_SSL_ALLOC_FAILED;
5772 }
Manuel Pégourié-Gonnard35eb8022019-05-16 11:11:08 +02005773
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005774 memcpy(session->ticket, p, session->ticket_len);
Manuel Pégourié-Gonnard35eb8022019-05-16 11:11:08 +02005775 p += session->ticket_len;
5776 }
Manuel Pégourié-Gonnardf743c032019-05-24 12:06:29 +02005777
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005778 if (4 > (size_t) (end - p)) {
5779 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
5780 }
Manuel Pégourié-Gonnardf743c032019-05-24 12:06:29 +02005781
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005782 session->ticket_lifetime = ((uint32_t) p[0] << 24) |
5783 ((uint32_t) p[1] << 16) |
5784 ((uint32_t) p[2] << 8) |
5785 ((uint32_t) p[3]);
Manuel Pégourié-Gonnardf743c032019-05-24 12:06:29 +02005786 p += 4;
Manuel Pégourié-Gonnard35eb8022019-05-16 11:11:08 +02005787#endif /* MBEDTLS_SSL_SESSION_TICKETS && MBEDTLS_SSL_CLI_C */
5788
Manuel Pégourié-Gonnardf743c032019-05-24 12:06:29 +02005789 /*
5790 * Misc extension-related info
5791 */
5792#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005793 if (1 > (size_t) (end - p)) {
5794 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
5795 }
Manuel Pégourié-Gonnardf743c032019-05-24 12:06:29 +02005796
5797 session->mfl_code = *p++;
5798#endif
5799
5800#if defined(MBEDTLS_SSL_TRUNCATED_HMAC)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005801 if (1 > (size_t) (end - p)) {
5802 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
5803 }
Manuel Pégourié-Gonnardf743c032019-05-24 12:06:29 +02005804
5805 session->trunc_hmac = *p++;
5806#endif
5807
5808#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005809 if (1 > (size_t) (end - p)) {
5810 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
5811 }
Manuel Pégourié-Gonnardf743c032019-05-24 12:06:29 +02005812
5813 session->encrypt_then_mac = *p++;
5814#endif
5815
Manuel Pégourié-Gonnard35eb8022019-05-16 11:11:08 +02005816 /* Done, should have consumed entire buffer */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005817 if (p != end) {
5818 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
5819 }
Manuel Pégourié-Gonnarda3e7c652019-05-16 10:08:35 +02005820
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005821 return 0;
Manuel Pégourié-Gonnarda3e7c652019-05-16 10:08:35 +02005822}
5823
5824/*
Manuel Pégourié-Gonnardb9dfc9f2019-07-12 10:50:19 +02005825 * Deserialize session: public wrapper for error cleaning
Manuel Pégourié-Gonnarda3d831b2019-05-23 12:28:45 +02005826 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005827int mbedtls_ssl_session_load(mbedtls_ssl_session *session,
5828 const unsigned char *buf,
5829 size_t len)
Manuel Pégourié-Gonnarda3d831b2019-05-23 12:28:45 +02005830{
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005831 int ret = ssl_session_load(session, 0, buf, len);
Manuel Pégourié-Gonnarda3d831b2019-05-23 12:28:45 +02005832
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005833 if (ret != 0) {
5834 mbedtls_ssl_session_free(session);
5835 }
Manuel Pégourié-Gonnarda3d831b2019-05-23 12:28:45 +02005836
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005837 return ret;
Manuel Pégourié-Gonnarda3d831b2019-05-23 12:28:45 +02005838}
5839
5840/*
Paul Bakker1961b702013-01-25 14:49:24 +01005841 * Perform a single step of the SSL handshake
Paul Bakker5121ce52009-01-03 21:22:43 +00005842 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005843int mbedtls_ssl_handshake_step(mbedtls_ssl_context *ssl)
Paul Bakker5121ce52009-01-03 21:22:43 +00005844{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005845 int ret = MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
Paul Bakker5121ce52009-01-03 21:22:43 +00005846
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005847 if (ssl == NULL || ssl->conf == NULL) {
5848 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
5849 }
Manuel Pégourié-Gonnardf81ee2e2015-09-01 17:43:40 +02005850
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005851#if defined(MBEDTLS_SSL_CLI_C)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005852 if (ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT) {
5853 ret = mbedtls_ssl_handshake_client_step(ssl);
5854 }
Paul Bakker5121ce52009-01-03 21:22:43 +00005855#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005856#if defined(MBEDTLS_SSL_SRV_C)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005857 if (ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER) {
5858 ret = mbedtls_ssl_handshake_server_step(ssl);
5859 }
Paul Bakker5121ce52009-01-03 21:22:43 +00005860#endif
5861
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005862 return ret;
Paul Bakker1961b702013-01-25 14:49:24 +01005863}
5864
5865/*
5866 * Perform the SSL handshake
5867 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005868int mbedtls_ssl_handshake(mbedtls_ssl_context *ssl)
Paul Bakker1961b702013-01-25 14:49:24 +01005869{
5870 int ret = 0;
5871
Hanno Beckera817ea42020-10-20 15:20:23 +01005872 /* Sanity checks */
5873
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005874 if (ssl == NULL || ssl->conf == NULL) {
5875 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
5876 }
Manuel Pégourié-Gonnardf81ee2e2015-09-01 17:43:40 +02005877
Hanno Beckera817ea42020-10-20 15:20:23 +01005878#if defined(MBEDTLS_SSL_PROTO_DTLS)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005879 if (ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
5880 (ssl->f_set_timer == NULL || ssl->f_get_timer == NULL)) {
5881 MBEDTLS_SSL_DEBUG_MSG(1, ("You must use "
5882 "mbedtls_ssl_set_timer_cb() for DTLS"));
5883 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
Hanno Beckera817ea42020-10-20 15:20:23 +01005884 }
5885#endif /* MBEDTLS_SSL_PROTO_DTLS */
5886
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005887 MBEDTLS_SSL_DEBUG_MSG(2, ("=> handshake"));
Paul Bakker1961b702013-01-25 14:49:24 +01005888
Hanno Beckera817ea42020-10-20 15:20:23 +01005889 /* Main handshake loop */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005890 while (ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER) {
5891 ret = mbedtls_ssl_handshake_step(ssl);
Paul Bakker1961b702013-01-25 14:49:24 +01005892
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005893 if (ret != 0) {
Paul Bakker1961b702013-01-25 14:49:24 +01005894 break;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005895 }
Paul Bakker1961b702013-01-25 14:49:24 +01005896 }
5897
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005898 MBEDTLS_SSL_DEBUG_MSG(2, ("<= handshake"));
Paul Bakker5121ce52009-01-03 21:22:43 +00005899
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005900 return ret;
Paul Bakker5121ce52009-01-03 21:22:43 +00005901}
5902
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005903#if defined(MBEDTLS_SSL_RENEGOTIATION)
5904#if defined(MBEDTLS_SSL_SRV_C)
Paul Bakker5121ce52009-01-03 21:22:43 +00005905/*
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01005906 * Write HelloRequest to request renegotiation on server
Paul Bakker48916f92012-09-16 19:57:18 +00005907 */
Manuel Pégourié-Gonnardd904d662022-06-17 10:24:00 +02005908MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005909static int ssl_write_hello_request(mbedtls_ssl_context *ssl)
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01005910{
Janos Follath865b3eb2019-12-16 11:46:15 +00005911 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01005912
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
5915 ssl->out_msglen = 4;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005916 ssl->out_msgtype = MBEDTLS_SSL_MSG_HANDSHAKE;
5917 ssl->out_msg[0] = MBEDTLS_SSL_HS_HELLO_REQUEST;
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01005918
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005919 if ((ret = mbedtls_ssl_write_handshake_msg(ssl)) != 0) {
5920 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ssl_write_handshake_msg", ret);
5921 return ret;
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01005922 }
5923
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005924 MBEDTLS_SSL_DEBUG_MSG(2, ("<= write hello request"));
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01005925
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005926 return 0;
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01005927}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005928#endif /* MBEDTLS_SSL_SRV_C */
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01005929
5930/*
5931 * Actually renegotiate current connection, triggered by either:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005932 * - any side: calling mbedtls_ssl_renegotiate(),
5933 * - client: receiving a HelloRequest during mbedtls_ssl_read(),
5934 * - server: receiving any handshake message on server during mbedtls_ssl_read() after
Manuel Pégourié-Gonnard55e4ff22014-08-19 11:16:35 +02005935 * the initial handshake is completed.
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01005936 * If the handshake doesn't complete due to waiting for I/O, it will continue
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005937 * during the next calls to mbedtls_ssl_renegotiate() or mbedtls_ssl_read() respectively.
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01005938 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005939int mbedtls_ssl_start_renegotiation(mbedtls_ssl_context *ssl)
Paul Bakker48916f92012-09-16 19:57:18 +00005940{
Janos Follath865b3eb2019-12-16 11:46:15 +00005941 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker48916f92012-09-16 19:57:18 +00005942
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005943 MBEDTLS_SSL_DEBUG_MSG(2, ("=> renegotiate"));
Paul Bakker48916f92012-09-16 19:57:18 +00005944
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005945 if ((ret = ssl_handshake_init(ssl)) != 0) {
5946 return ret;
5947 }
Paul Bakker48916f92012-09-16 19:57:18 +00005948
Manuel Pégourié-Gonnard0557bd52014-08-19 19:18:39 +02005949 /* RFC 6347 4.2.2: "[...] the HelloRequest will have message_seq = 0 and
5950 * the ServerHello will have message_seq = 1" */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005951#if defined(MBEDTLS_SSL_PROTO_DTLS)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005952 if (ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
5953 ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_PENDING) {
5954 if (ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER) {
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02005955 ssl->handshake->out_msg_seq = 1;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005956 } else {
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02005957 ssl->handshake->in_msg_seq = 1;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005958 }
Manuel Pégourié-Gonnard0557bd52014-08-19 19:18:39 +02005959 }
5960#endif
5961
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005962 ssl->state = MBEDTLS_SSL_HELLO_REQUEST;
5963 ssl->renego_status = MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS;
Paul Bakker48916f92012-09-16 19:57:18 +00005964
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005965 if ((ret = mbedtls_ssl_handshake(ssl)) != 0) {
5966 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ssl_handshake", ret);
5967 return ret;
Paul Bakker48916f92012-09-16 19:57:18 +00005968 }
5969
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005970 MBEDTLS_SSL_DEBUG_MSG(2, ("<= renegotiate"));
Paul Bakker48916f92012-09-16 19:57:18 +00005971
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005972 return 0;
Paul Bakker48916f92012-09-16 19:57:18 +00005973}
5974
5975/*
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01005976 * Renegotiate current connection on client,
5977 * or request renegotiation on server
5978 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005979int mbedtls_ssl_renegotiate(mbedtls_ssl_context *ssl)
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01005980{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005981 int ret = MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01005982
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005983 if (ssl == NULL || ssl->conf == NULL) {
5984 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
5985 }
Manuel Pégourié-Gonnardf81ee2e2015-09-01 17:43:40 +02005986
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005987#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01005988 /* On server, just send the request */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005989 if (ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER) {
5990 if (ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER) {
5991 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
5992 }
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01005993
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005994 ssl->renego_status = MBEDTLS_SSL_RENEGOTIATION_PENDING;
Manuel Pégourié-Gonnardf07f4212014-08-15 19:04:47 +02005995
5996 /* Did we already try/start sending HelloRequest? */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005997 if (ssl->out_left != 0) {
5998 return mbedtls_ssl_flush_output(ssl);
5999 }
Manuel Pégourié-Gonnardf07f4212014-08-15 19:04:47 +02006000
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01006001 return ssl_write_hello_request(ssl);
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01006002 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006003#endif /* MBEDTLS_SSL_SRV_C */
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01006004
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006005#if defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01006006 /*
6007 * On client, either start the renegotiation process or,
6008 * if already in progress, continue the handshake
6009 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01006010 if (ssl->renego_status != MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS) {
6011 if (ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER) {
6012 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01006013 }
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01006014
6015 if ((ret = mbedtls_ssl_start_renegotiation(ssl)) != 0) {
6016 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ssl_start_renegotiation", ret);
6017 return ret;
6018 }
6019 } else {
6020 if ((ret = mbedtls_ssl_handshake(ssl)) != 0) {
6021 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ssl_handshake", ret);
6022 return ret;
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01006023 }
6024 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006025#endif /* MBEDTLS_SSL_CLI_C */
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01006026
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01006027 return ret;
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01006028}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006029#endif /* MBEDTLS_SSL_RENEGOTIATION */
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01006030
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006031#if defined(MBEDTLS_X509_CRT_PARSE_C)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01006032static void ssl_key_cert_free(mbedtls_ssl_key_cert *key_cert)
Manuel Pégourié-Gonnard705fcca2013-09-23 20:04:20 +02006033{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006034 mbedtls_ssl_key_cert *cur = key_cert, *next;
Manuel Pégourié-Gonnard705fcca2013-09-23 20:04:20 +02006035
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01006036 while (cur != NULL) {
Manuel Pégourié-Gonnard705fcca2013-09-23 20:04:20 +02006037 next = cur->next;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01006038 mbedtls_free(cur);
Manuel Pégourié-Gonnard705fcca2013-09-23 20:04:20 +02006039 cur = next;
6040 }
6041}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006042#endif /* MBEDTLS_X509_CRT_PARSE_C */
Manuel Pégourié-Gonnard705fcca2013-09-23 20:04:20 +02006043
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01006044void mbedtls_ssl_handshake_free(mbedtls_ssl_context *ssl)
Paul Bakker48916f92012-09-16 19:57:18 +00006045{
Gilles Peskine9b562d52018-04-25 20:32:43 +02006046 mbedtls_ssl_handshake_params *handshake = ssl->handshake;
6047
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01006048 if (handshake == NULL) {
Paul Bakkeraccaffe2014-06-26 13:37:14 +02006049 return;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01006050 }
Paul Bakkeraccaffe2014-06-26 13:37:14 +02006051
Gilles Peskinedf13d5c2018-04-25 20:39:48 +02006052#if defined(MBEDTLS_SSL_ASYNC_PRIVATE)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01006053 if (ssl->conf->f_async_cancel != NULL && handshake->async_in_progress != 0) {
6054 ssl->conf->f_async_cancel(ssl);
Gilles Peskinedf13d5c2018-04-25 20:39:48 +02006055 handshake->async_in_progress = 0;
6056 }
6057#endif /* MBEDTLS_SSL_ASYNC_PRIVATE */
6058
Manuel Pégourié-Gonnardb9d64e52015-07-06 14:18:56 +02006059#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1) || \
6060 defined(MBEDTLS_SSL_PROTO_TLS1_1)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01006061 mbedtls_md5_free(&handshake->fin_md5);
6062 mbedtls_sha1_free(&handshake->fin_sha1);
Manuel Pégourié-Gonnardb9d64e52015-07-06 14:18:56 +02006063#endif
6064#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
6065#if defined(MBEDTLS_SHA256_C)
Andrzej Kurekeb342242019-01-29 09:14:33 -05006066#if defined(MBEDTLS_USE_PSA_CRYPTO)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01006067 psa_hash_abort(&handshake->fin_sha256_psa);
Andrzej Kurekeb342242019-01-29 09:14:33 -05006068#else
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01006069 mbedtls_sha256_free(&handshake->fin_sha256);
Manuel Pégourié-Gonnardb9d64e52015-07-06 14:18:56 +02006070#endif
Andrzej Kurekeb342242019-01-29 09:14:33 -05006071#endif
Gilles Peskined2d59372021-05-12 22:43:27 +02006072#if defined(MBEDTLS_SHA512_C) && !defined(MBEDTLS_SHA512_NO_SHA384)
Andrzej Kurekeb342242019-01-29 09:14:33 -05006073#if defined(MBEDTLS_USE_PSA_CRYPTO)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01006074 psa_hash_abort(&handshake->fin_sha384_psa);
Andrzej Kurekeb342242019-01-29 09:14:33 -05006075#else
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01006076 mbedtls_sha512_free(&handshake->fin_sha512);
Manuel Pégourié-Gonnardb9d64e52015-07-06 14:18:56 +02006077#endif
Andrzej Kurekeb342242019-01-29 09:14:33 -05006078#endif
Manuel Pégourié-Gonnardb9d64e52015-07-06 14:18:56 +02006079#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
6080
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006081#if defined(MBEDTLS_DHM_C)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01006082 mbedtls_dhm_free(&handshake->dhm_ctx);
Paul Bakker48916f92012-09-16 19:57:18 +00006083#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006084#if defined(MBEDTLS_ECDH_C)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01006085 mbedtls_ecdh_free(&handshake->ecdh_ctx);
Paul Bakker61d113b2013-07-04 11:51:43 +02006086#endif
Manuel Pégourié-Gonnardeef142d2015-09-16 10:05:04 +02006087#if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01006088 mbedtls_ecjpake_free(&handshake->ecjpake_ctx);
Manuel Pégourié-Gonnard77c06462015-09-17 13:59:49 +02006089#if defined(MBEDTLS_SSL_CLI_C)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01006090 mbedtls_free(handshake->ecjpake_cache);
Manuel Pégourié-Gonnard77c06462015-09-17 13:59:49 +02006091 handshake->ecjpake_cache = NULL;
6092 handshake->ecjpake_cache_len = 0;
6093#endif
Manuel Pégourié-Gonnard76cfd3f2015-09-15 12:10:54 +02006094#endif
Paul Bakker61d113b2013-07-04 11:51:43 +02006095
Janos Follath4ae5c292016-02-10 11:27:43 +00006096#if defined(MBEDTLS_ECDH_C) || defined(MBEDTLS_ECDSA_C) || \
6097 defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
Paul Bakker9af723c2014-05-01 13:03:14 +02006098 /* explicit void pointer cast for buggy MS compiler */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01006099 mbedtls_free((void *) handshake->curves);
Manuel Pégourié-Gonnardd09453c2013-09-23 19:11:32 +02006100#endif
6101
Gilles Peskineeccd8882020-03-10 12:19:08 +01006102#if defined(MBEDTLS_KEY_EXCHANGE_SOME_PSK_ENABLED)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01006103 if (handshake->psk != NULL) {
6104 mbedtls_platform_zeroize(handshake->psk, handshake->psk_len);
6105 mbedtls_free(handshake->psk);
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01006106 }
6107#endif
6108
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006109#if defined(MBEDTLS_X509_CRT_PARSE_C) && \
6110 defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
Manuel Pégourié-Gonnard83724542013-09-24 22:30:56 +02006111 /*
6112 * Free only the linked list wrapper, not the keys themselves
6113 * since the belong to the SNI callback
6114 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01006115 if (handshake->sni_key_cert != NULL) {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006116 mbedtls_ssl_key_cert *cur = handshake->sni_key_cert, *next;
Manuel Pégourié-Gonnard83724542013-09-24 22:30:56 +02006117
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01006118 while (cur != NULL) {
Manuel Pégourié-Gonnard83724542013-09-24 22:30:56 +02006119 next = cur->next;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01006120 mbedtls_free(cur);
Manuel Pégourié-Gonnard83724542013-09-24 22:30:56 +02006121 cur = next;
6122 }
6123 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006124#endif /* MBEDTLS_X509_CRT_PARSE_C && MBEDTLS_SSL_SERVER_NAME_INDICATION */
Manuel Pégourié-Gonnard705fcca2013-09-23 20:04:20 +02006125
Gilles Peskineeccd8882020-03-10 12:19:08 +01006126#if defined(MBEDTLS_SSL_ECP_RESTARTABLE_ENABLED)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01006127 mbedtls_x509_crt_restart_free(&handshake->ecrs_ctx);
6128 if (handshake->ecrs_peer_cert != NULL) {
6129 mbedtls_x509_crt_free(handshake->ecrs_peer_cert);
6130 mbedtls_free(handshake->ecrs_peer_cert);
Hanno Becker3dad3112019-02-05 17:19:52 +00006131 }
Manuel Pégourié-Gonnard862cde52017-05-17 11:56:15 +02006132#endif
6133
Hanno Becker75173122019-02-06 16:18:31 +00006134#if defined(MBEDTLS_X509_CRT_PARSE_C) && \
6135 !defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01006136 mbedtls_pk_free(&handshake->peer_pubkey);
Hanno Becker75173122019-02-06 16:18:31 +00006137#endif /* MBEDTLS_X509_CRT_PARSE_C && !MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
6138
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006139#if defined(MBEDTLS_SSL_PROTO_DTLS)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01006140 mbedtls_free(handshake->verify_cookie);
6141 mbedtls_ssl_flight_free(handshake->flight);
6142 mbedtls_ssl_buffering_free(ssl);
Manuel Pégourié-Gonnard74848812014-07-11 02:43:49 +02006143#endif
6144
Hanno Becker4a63ed42019-01-08 11:39:35 +00006145#if defined(MBEDTLS_ECDH_C) && \
6146 defined(MBEDTLS_USE_PSA_CRYPTO)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01006147 psa_destroy_key(handshake->ecdh_psa_privkey);
Hanno Becker4a63ed42019-01-08 11:39:35 +00006148#endif /* MBEDTLS_ECDH_C && MBEDTLS_USE_PSA_CRYPTO */
6149
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01006150 mbedtls_platform_zeroize(handshake,
6151 sizeof(mbedtls_ssl_handshake_params));
Andrzej Kurek0afa2a12020-03-03 10:39:58 -05006152
6153#if defined(MBEDTLS_SSL_VARIABLE_BUFFER_LENGTH)
6154 /* If the buffers are too big - reallocate. Because of the way Mbed TLS
6155 * processes datagrams and the fact that a datagram is allowed to have
6156 * several records in it, it is possible that the I/O buffers are not
6157 * empty at this stage */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01006158 handle_buffer_resizing(ssl, 1, mbedtls_ssl_get_input_buflen(ssl),
6159 mbedtls_ssl_get_output_buflen(ssl));
Andrzej Kurek0afa2a12020-03-03 10:39:58 -05006160#endif
Paul Bakker48916f92012-09-16 19:57:18 +00006161}
6162
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01006163void mbedtls_ssl_session_free(mbedtls_ssl_session *session)
Paul Bakker48916f92012-09-16 19:57:18 +00006164{
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01006165 if (session == NULL) {
Paul Bakkeraccaffe2014-06-26 13:37:14 +02006166 return;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01006167 }
Paul Bakkeraccaffe2014-06-26 13:37:14 +02006168
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006169#if defined(MBEDTLS_X509_CRT_PARSE_C)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01006170 ssl_clear_peer_cert(session);
Paul Bakkered27a042013-04-18 22:46:23 +02006171#endif
Paul Bakker0a597072012-09-25 21:55:46 +00006172
Manuel Pégourié-Gonnardb596abf2015-05-20 10:45:29 +02006173#if defined(MBEDTLS_SSL_SESSION_TICKETS) && defined(MBEDTLS_SSL_CLI_C)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01006174 mbedtls_free(session->ticket);
Paul Bakkera503a632013-08-14 13:48:06 +02006175#endif
Manuel Pégourié-Gonnard75d44012013-08-02 14:44:04 +02006176
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01006177 mbedtls_platform_zeroize(session, sizeof(mbedtls_ssl_session));
Paul Bakker48916f92012-09-16 19:57:18 +00006178}
6179
Manuel Pégourié-Gonnard5c0e3772019-07-23 16:13:17 +02006180#if defined(MBEDTLS_SSL_CONTEXT_SERIALIZATION)
Manuel Pégourié-Gonnard4e9370b2019-07-23 16:31:16 +02006181
6182#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
6183#define SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_CONNECTION_ID 1u
6184#else
6185#define SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_CONNECTION_ID 0u
6186#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
6187
6188#if defined(MBEDTLS_SSL_DTLS_BADMAC_LIMIT)
6189#define SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_BADMAC_LIMIT 1u
6190#else
6191#define SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_BADMAC_LIMIT 0u
6192#endif /* MBEDTLS_SSL_DTLS_BADMAC_LIMIT */
6193
6194#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
6195#define SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_ANTI_REPLAY 1u
6196#else
6197#define SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_ANTI_REPLAY 0u
6198#endif /* MBEDTLS_SSL_DTLS_ANTI_REPLAY */
6199
6200#if defined(MBEDTLS_SSL_ALPN)
6201#define SSL_SERIALIZED_CONTEXT_CONFIG_ALPN 1u
6202#else
6203#define SSL_SERIALIZED_CONTEXT_CONFIG_ALPN 0u
6204#endif /* MBEDTLS_SSL_ALPN */
6205
6206#define SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_CONNECTION_ID_BIT 0
6207#define SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_BADMAC_LIMIT_BIT 1
6208#define SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_ANTI_REPLAY_BIT 2
6209#define SSL_SERIALIZED_CONTEXT_CONFIG_ALPN_BIT 3
6210
6211#define SSL_SERIALIZED_CONTEXT_CONFIG_BITFLAG \
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01006212 ((uint32_t) ( \
6213 (SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_CONNECTION_ID << \
6214 SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_CONNECTION_ID_BIT) | \
6215 (SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_BADMAC_LIMIT << \
6216 SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_BADMAC_LIMIT_BIT) | \
6217 (SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_ANTI_REPLAY << \
6218 SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_ANTI_REPLAY_BIT) | \
6219 (SSL_SERIALIZED_CONTEXT_CONFIG_ALPN << SSL_SERIALIZED_CONTEXT_CONFIG_ALPN_BIT) | \
6220 0u))
Manuel Pégourié-Gonnard4e9370b2019-07-23 16:31:16 +02006221
Chien Wongb6d57932024-02-07 21:48:12 +08006222static const unsigned char ssl_serialized_context_header[] = {
Manuel Pégourié-Gonnard4c90e852019-07-11 10:58:10 +02006223 MBEDTLS_VERSION_MAJOR,
6224 MBEDTLS_VERSION_MINOR,
6225 MBEDTLS_VERSION_PATCH,
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01006226 MBEDTLS_BYTE_1(SSL_SERIALIZED_SESSION_CONFIG_BITFLAG),
6227 MBEDTLS_BYTE_0(SSL_SERIALIZED_SESSION_CONFIG_BITFLAG),
6228 MBEDTLS_BYTE_2(SSL_SERIALIZED_CONTEXT_CONFIG_BITFLAG),
6229 MBEDTLS_BYTE_1(SSL_SERIALIZED_CONTEXT_CONFIG_BITFLAG),
6230 MBEDTLS_BYTE_0(SSL_SERIALIZED_CONTEXT_CONFIG_BITFLAG),
Manuel Pégourié-Gonnard4c90e852019-07-11 10:58:10 +02006231};
6232
Paul Bakker5121ce52009-01-03 21:22:43 +00006233/*
Manuel Pégourié-Gonnardac87e282019-05-28 13:02:16 +02006234 * Serialize a full SSL context
Manuel Pégourié-Gonnard00400c22019-07-10 14:58:45 +02006235 *
6236 * The format of the serialized data is:
6237 * (in the presentation language of TLS, RFC 8446 section 3)
6238 *
6239 * // header
6240 * opaque mbedtls_version[3]; // major, minor, patch
Manuel Pégourié-Gonnard4c90e852019-07-11 10:58:10 +02006241 * opaque context_format[5]; // version-specific field determining
Manuel Pégourié-Gonnard00400c22019-07-10 14:58:45 +02006242 * // the format of the remaining
Manuel Pégourié-Gonnard4c90e852019-07-11 10:58:10 +02006243 * // serialized data.
Manuel Pégourié-Gonnard4e9370b2019-07-23 16:31:16 +02006244 * Note: When updating the format, remember to keep these
6245 * version+format bytes. (We may make their size part of the API.)
Manuel Pégourié-Gonnard00400c22019-07-10 14:58:45 +02006246 *
6247 * // session sub-structure
6248 * opaque session<1..2^32-1>; // see mbedtls_ssl_session_save()
6249 * // transform sub-structure
6250 * uint8 random[64]; // ServerHello.random+ClientHello.random
6251 * uint8 in_cid<0..2^8-1> // Connection ID: expected incoming value
6252 * uint8 out_cid<0..2^8-1> // Connection ID: outgoing value to use
6253 * // fields from ssl_context
6254 * uint32 badmac_seen; // DTLS: number of records with failing MAC
6255 * uint64 in_window_top; // DTLS: last validated record seq_num
6256 * uint64 in_window; // DTLS: bitmask for replay protection
6257 * uint8 disable_datagram_packing; // DTLS: only one record per datagram
6258 * uint64 cur_out_ctr; // Record layer: outgoing sequence number
6259 * uint16 mtu; // DTLS: path mtu (max outgoing fragment size)
6260 * uint8 alpn_chosen<0..2^8-1> // ALPN: negotiated application protocol
6261 *
6262 * Note that many fields of the ssl_context or sub-structures are not
6263 * serialized, as they fall in one of the following categories:
6264 *
6265 * 1. forced value (eg in_left must be 0)
6266 * 2. pointer to dynamically-allocated memory (eg session, transform)
6267 * 3. value can be re-derived from other data (eg session keys from MS)
6268 * 4. value was temporary (eg content of input buffer)
6269 * 5. value will be provided by the user again (eg I/O callbacks and context)
Manuel Pégourié-Gonnardac87e282019-05-28 13:02:16 +02006270 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01006271int mbedtls_ssl_context_save(mbedtls_ssl_context *ssl,
6272 unsigned char *buf,
6273 size_t buf_len,
6274 size_t *olen)
Manuel Pégourié-Gonnardac87e282019-05-28 13:02:16 +02006275{
Manuel Pégourié-Gonnard4c90e852019-07-11 10:58:10 +02006276 unsigned char *p = buf;
6277 size_t used = 0;
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02006278 size_t session_len;
6279 int ret = 0;
Manuel Pégourié-Gonnard4c90e852019-07-11 10:58:10 +02006280
Manuel Pégourié-Gonnard1aaf6692019-07-10 14:14:05 +02006281 /*
Manuel Pégourié-Gonnarde4588692019-07-29 12:28:52 +02006282 * Enforce usage restrictions, see "return BAD_INPUT_DATA" in
6283 * this function's documentation.
6284 *
6285 * These are due to assumptions/limitations in the implementation. Some of
6286 * them are likely to stay (no handshake in progress) some might go away
6287 * (only DTLS) but are currently used to simplify the implementation.
Manuel Pégourié-Gonnard1aaf6692019-07-10 14:14:05 +02006288 */
Manuel Pégourié-Gonnarde4588692019-07-29 12:28:52 +02006289 /* The initial handshake must be over */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01006290 if (ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER) {
6291 MBEDTLS_SSL_DEBUG_MSG(1, ("Initial handshake isn't over"));
6292 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
Jarno Lamsa8c51b7c2019-08-21 13:45:05 +03006293 }
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01006294 if (ssl->handshake != NULL) {
6295 MBEDTLS_SSL_DEBUG_MSG(1, ("Handshake isn't completed"));
6296 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
Jarno Lamsa8c51b7c2019-08-21 13:45:05 +03006297 }
Manuel Pégourié-Gonnarde4588692019-07-29 12:28:52 +02006298 /* Double-check that sub-structures are indeed ready */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01006299 if (ssl->transform == NULL || ssl->session == NULL) {
6300 MBEDTLS_SSL_DEBUG_MSG(1, ("Serialised structures aren't ready"));
6301 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
Jarno Lamsa8c51b7c2019-08-21 13:45:05 +03006302 }
Manuel Pégourié-Gonnarde4588692019-07-29 12:28:52 +02006303 /* There must be no pending incoming or outgoing data */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01006304 if (mbedtls_ssl_check_pending(ssl) != 0) {
6305 MBEDTLS_SSL_DEBUG_MSG(1, ("There is pending incoming data"));
6306 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
Jarno Lamsa8c51b7c2019-08-21 13:45:05 +03006307 }
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01006308 if (ssl->out_left != 0) {
6309 MBEDTLS_SSL_DEBUG_MSG(1, ("There is pending outgoing data"));
6310 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
Jarno Lamsa8c51b7c2019-08-21 13:45:05 +03006311 }
Dave Rodgmana03396a2022-12-06 16:30:38 +00006312 /* Protocol must be DTLS, not TLS */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01006313 if (ssl->conf->transport != MBEDTLS_SSL_TRANSPORT_DATAGRAM) {
6314 MBEDTLS_SSL_DEBUG_MSG(1, ("Only DTLS is supported"));
6315 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
Jarno Lamsa8c51b7c2019-08-21 13:45:05 +03006316 }
Manuel Pégourié-Gonnarde4588692019-07-29 12:28:52 +02006317 /* Version must be 1.2 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01006318 if (ssl->major_ver != MBEDTLS_SSL_MAJOR_VERSION_3) {
6319 MBEDTLS_SSL_DEBUG_MSG(1, ("Only version 1.2 supported"));
6320 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
Jarno Lamsa8c51b7c2019-08-21 13:45:05 +03006321 }
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01006322 if (ssl->minor_ver != MBEDTLS_SSL_MINOR_VERSION_3) {
6323 MBEDTLS_SSL_DEBUG_MSG(1, ("Only version 1.2 supported"));
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 /* We must be using an AEAD ciphersuite */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01006327 if (mbedtls_ssl_transform_uses_aead(ssl->transform) != 1) {
6328 MBEDTLS_SSL_DEBUG_MSG(1, ("Only AEAD ciphersuites supported"));
6329 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
Jarno Lamsa8c51b7c2019-08-21 13:45:05 +03006330 }
Manuel Pégourié-Gonnarde4588692019-07-29 12:28:52 +02006331 /* Renegotiation must not be enabled */
6332#if defined(MBEDTLS_SSL_RENEGOTIATION)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01006333 if (ssl->conf->disable_renegotiation != MBEDTLS_SSL_RENEGOTIATION_DISABLED) {
6334 MBEDTLS_SSL_DEBUG_MSG(1, ("Renegotiation must not be enabled"));
6335 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
Jarno Lamsa8c51b7c2019-08-21 13:45:05 +03006336 }
Manuel Pégourié-Gonnarde4588692019-07-29 12:28:52 +02006337#endif
Manuel Pégourié-Gonnardac87e282019-05-28 13:02:16 +02006338
Manuel Pégourié-Gonnard4c90e852019-07-11 10:58:10 +02006339 /*
6340 * Version and format identifier
6341 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01006342 used += sizeof(ssl_serialized_context_header);
Manuel Pégourié-Gonnardac87e282019-05-28 13:02:16 +02006343
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01006344 if (used <= buf_len) {
6345 memcpy(p, ssl_serialized_context_header,
6346 sizeof(ssl_serialized_context_header));
6347 p += sizeof(ssl_serialized_context_header);
Manuel Pégourié-Gonnard4c90e852019-07-11 10:58:10 +02006348 }
6349
6350 /*
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02006351 * Session (length + data)
6352 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01006353 ret = ssl_session_save(ssl->session, 1, NULL, 0, &session_len);
6354 if (ret != MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL) {
6355 return ret;
6356 }
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02006357
6358 used += 4 + session_len;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01006359 if (used <= buf_len) {
6360 MBEDTLS_PUT_UINT32_BE(session_len, p, 0);
Joe Subbiani2f98d792021-08-20 11:44:44 +01006361 p += 4;
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02006362
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01006363 ret = ssl_session_save(ssl->session, 1,
6364 p, session_len, &session_len);
6365 if (ret != 0) {
6366 return ret;
6367 }
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02006368
6369 p += session_len;
6370 }
6371
6372 /*
Manuel Pégourié-Gonnardc2a7b892019-07-15 09:04:11 +02006373 * Transform
6374 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01006375 used += sizeof(ssl->transform->randbytes);
6376 if (used <= buf_len) {
6377 memcpy(p, ssl->transform->randbytes,
6378 sizeof(ssl->transform->randbytes));
6379 p += sizeof(ssl->transform->randbytes);
Manuel Pégourié-Gonnardc2a7b892019-07-15 09:04:11 +02006380 }
6381
6382#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
6383 used += 2 + ssl->transform->in_cid_len + ssl->transform->out_cid_len;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01006384 if (used <= buf_len) {
Manuel Pégourié-Gonnardc2a7b892019-07-15 09:04:11 +02006385 *p++ = ssl->transform->in_cid_len;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01006386 memcpy(p, ssl->transform->in_cid, ssl->transform->in_cid_len);
Manuel Pégourié-Gonnardc2a7b892019-07-15 09:04:11 +02006387 p += ssl->transform->in_cid_len;
6388
6389 *p++ = ssl->transform->out_cid_len;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01006390 memcpy(p, ssl->transform->out_cid, ssl->transform->out_cid_len);
Manuel Pégourié-Gonnardc2a7b892019-07-15 09:04:11 +02006391 p += ssl->transform->out_cid_len;
6392 }
6393#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
6394
6395 /*
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02006396 * Saved fields from top-level ssl_context structure
6397 */
6398#if defined(MBEDTLS_SSL_DTLS_BADMAC_LIMIT)
6399 used += 4;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01006400 if (used <= buf_len) {
6401 MBEDTLS_PUT_UINT32_BE(ssl->badmac_seen, p, 0);
Joe Subbiani2f98d792021-08-20 11:44:44 +01006402 p += 4;
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02006403 }
6404#endif /* MBEDTLS_SSL_DTLS_BADMAC_LIMIT */
6405
6406#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
6407 used += 16;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01006408 if (used <= buf_len) {
6409 MBEDTLS_PUT_UINT64_BE(ssl->in_window_top, p, 0);
Joe Subbiani2f98d792021-08-20 11:44:44 +01006410 p += 8;
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02006411
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01006412 MBEDTLS_PUT_UINT64_BE(ssl->in_window, p, 0);
Joe Subbiani2f98d792021-08-20 11:44:44 +01006413 p += 8;
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02006414 }
6415#endif /* MBEDTLS_SSL_DTLS_ANTI_REPLAY */
6416
6417#if defined(MBEDTLS_SSL_PROTO_DTLS)
6418 used += 1;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01006419 if (used <= buf_len) {
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02006420 *p++ = ssl->disable_datagram_packing;
6421 }
6422#endif /* MBEDTLS_SSL_PROTO_DTLS */
6423
6424 used += 8;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01006425 if (used <= buf_len) {
6426 memcpy(p, ssl->cur_out_ctr, 8);
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02006427 p += 8;
6428 }
6429
6430#if defined(MBEDTLS_SSL_PROTO_DTLS)
6431 used += 2;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01006432 if (used <= buf_len) {
6433 MBEDTLS_PUT_UINT16_BE(ssl->mtu, p, 0);
Joe Subbiani2f98d792021-08-20 11:44:44 +01006434 p += 2;
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02006435 }
6436#endif /* MBEDTLS_SSL_PROTO_DTLS */
6437
6438#if defined(MBEDTLS_SSL_ALPN)
6439 {
6440 const uint8_t alpn_len = ssl->alpn_chosen
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01006441 ? (uint8_t) strlen(ssl->alpn_chosen)
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02006442 : 0;
6443
6444 used += 1 + alpn_len;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01006445 if (used <= buf_len) {
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02006446 *p++ = alpn_len;
6447
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01006448 if (ssl->alpn_chosen != NULL) {
6449 memcpy(p, ssl->alpn_chosen, alpn_len);
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02006450 p += alpn_len;
6451 }
6452 }
6453 }
6454#endif /* MBEDTLS_SSL_ALPN */
6455
6456 /*
Manuel Pégourié-Gonnard4c90e852019-07-11 10:58:10 +02006457 * Done
6458 */
6459 *olen = used;
6460
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01006461 if (used > buf_len) {
6462 return MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL;
6463 }
Manuel Pégourié-Gonnardac87e282019-05-28 13:02:16 +02006464
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01006465 MBEDTLS_SSL_DEBUG_BUF(4, "saved context", buf, used);
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02006466
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01006467 return mbedtls_ssl_session_reset_int(ssl, 0);
Manuel Pégourié-Gonnardac87e282019-05-28 13:02:16 +02006468}
6469
6470/*
Manuel Pégourié-Gonnardc2a7b892019-07-15 09:04:11 +02006471 * Helper to get TLS 1.2 PRF from ciphersuite
6472 * (Duplicates bits of logic from ssl_set_handshake_prfs().)
6473 */
Andrzej Kurekf9412f72022-10-18 07:30:19 -04006474#if defined(MBEDTLS_SHA256_C) || \
6475 (defined(MBEDTLS_SHA512_C) && !defined(MBEDTLS_SHA512_NO_SHA384))
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01006476typedef int (*tls_prf_fn)(const unsigned char *secret, size_t slen,
6477 const char *label,
6478 const unsigned char *random, size_t rlen,
6479 unsigned char *dstbuf, size_t dlen);
6480static tls_prf_fn ssl_tls12prf_from_cs(int ciphersuite_id)
Manuel Pégourié-Gonnardc2a7b892019-07-15 09:04:11 +02006481{
6482 const mbedtls_ssl_ciphersuite_t * const ciphersuite_info =
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01006483 mbedtls_ssl_ciphersuite_from_id(ciphersuite_id);
Manuel Pégourié-Gonnardc2a7b892019-07-15 09:04:11 +02006484
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01006485 if (ciphersuite_info == NULL) {
6486 return NULL;
6487 }
Andrzej Kurek84fc52c2022-10-25 04:18:30 -04006488
6489#if defined(MBEDTLS_SHA512_C) && !defined(MBEDTLS_SHA512_NO_SHA384)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01006490 if (ciphersuite_info->mac == MBEDTLS_MD_SHA384) {
6491 return tls_prf_sha384;
6492 } else
Manuel Pégourié-Gonnard9a96fd72019-07-23 17:11:24 +02006493#endif
Andrzej Kurekf9412f72022-10-18 07:30:19 -04006494#if defined(MBEDTLS_SHA256_C)
6495 {
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01006496 if (ciphersuite_info->mac == MBEDTLS_MD_SHA256) {
6497 return tls_prf_sha256;
6498 }
Andrzej Kurekf9412f72022-10-18 07:30:19 -04006499 }
6500#endif
6501#if !defined(MBEDTLS_SHA256_C) && \
6502 (!defined(MBEDTLS_SHA512_C) || defined(MBEDTLS_SHA512_NO_SHA384))
6503 (void) ciphersuite_info;
6504#endif
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01006505 return NULL;
Manuel Pégourié-Gonnardc2a7b892019-07-15 09:04:11 +02006506}
6507
Andrzej Kurekf9412f72022-10-18 07:30:19 -04006508#endif /* MBEDTLS_SHA256_C ||
6509 (MBEDTLS_SHA512_C && !MBEDTLS_SHA512_NO_SHA384) */
6510
Manuel Pégourié-Gonnardc2a7b892019-07-15 09:04:11 +02006511/*
Manuel Pégourié-Gonnardb9dfc9f2019-07-12 10:50:19 +02006512 * Deserialize context, see mbedtls_ssl_context_save() for format.
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02006513 *
6514 * This internal version is wrapped by a public function that cleans up in
6515 * case of error.
Manuel Pégourié-Gonnardac87e282019-05-28 13:02:16 +02006516 */
Manuel Pégourié-Gonnardd904d662022-06-17 10:24:00 +02006517MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01006518static int ssl_context_load(mbedtls_ssl_context *ssl,
6519 const unsigned char *buf,
6520 size_t len)
Manuel Pégourié-Gonnardac87e282019-05-28 13:02:16 +02006521{
Manuel Pégourié-Gonnard4c90e852019-07-11 10:58:10 +02006522 const unsigned char *p = buf;
6523 const unsigned char * const end = buf + len;
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02006524 size_t session_len;
Janos Follath865b3eb2019-12-16 11:46:15 +00006525 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Andrzej Kurekf9412f72022-10-18 07:30:19 -04006526 tls_prf_fn prf_func = NULL;
Manuel Pégourié-Gonnard4c90e852019-07-11 10:58:10 +02006527
Manuel Pégourié-Gonnard0ff76402019-07-11 09:56:30 +02006528 /*
6529 * The context should have been freshly setup or reset.
6530 * Give the user an error in case of obvious misuse.
Manuel Pégourié-Gonnard4ca930f2019-07-26 16:31:53 +02006531 * (Checking session is useful because it won't be NULL if we're
Manuel Pégourié-Gonnard0ff76402019-07-11 09:56:30 +02006532 * renegotiating, or if the user mistakenly loaded a session first.)
6533 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01006534 if (ssl->state != MBEDTLS_SSL_HELLO_REQUEST ||
6535 ssl->session != NULL) {
6536 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
Manuel Pégourié-Gonnard0ff76402019-07-11 09:56:30 +02006537 }
6538
6539 /*
6540 * We can't check that the config matches the initial one, but we can at
6541 * least check it matches the requirements for serializing.
6542 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01006543 if (ssl->conf->transport != MBEDTLS_SSL_TRANSPORT_DATAGRAM ||
Manuel Pégourié-Gonnard0ff76402019-07-11 09:56:30 +02006544 ssl->conf->max_major_ver < MBEDTLS_SSL_MAJOR_VERSION_3 ||
6545 ssl->conf->min_major_ver > MBEDTLS_SSL_MAJOR_VERSION_3 ||
6546 ssl->conf->max_minor_ver < MBEDTLS_SSL_MINOR_VERSION_3 ||
6547 ssl->conf->min_minor_ver > MBEDTLS_SSL_MINOR_VERSION_3 ||
6548#if defined(MBEDTLS_SSL_RENEGOTIATION)
Manuel Pégourié-Gonnard9a96fd72019-07-23 17:11:24 +02006549 ssl->conf->disable_renegotiation != MBEDTLS_SSL_RENEGOTIATION_DISABLED ||
Manuel Pégourié-Gonnard0ff76402019-07-11 09:56:30 +02006550#endif
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01006551 0) {
6552 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
Manuel Pégourié-Gonnard0ff76402019-07-11 09:56:30 +02006553 }
6554
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01006555 MBEDTLS_SSL_DEBUG_BUF(4, "context to load", buf, len);
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02006556
Manuel Pégourié-Gonnard4c90e852019-07-11 10:58:10 +02006557 /*
6558 * Check version identifier
6559 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01006560 if ((size_t) (end - p) < sizeof(ssl_serialized_context_header)) {
6561 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
Manuel Pégourié-Gonnard4c90e852019-07-11 10:58:10 +02006562 }
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01006563
6564 if (memcmp(p, ssl_serialized_context_header,
6565 sizeof(ssl_serialized_context_header)) != 0) {
6566 return MBEDTLS_ERR_SSL_VERSION_MISMATCH;
6567 }
6568 p += sizeof(ssl_serialized_context_header);
Manuel Pégourié-Gonnard4c90e852019-07-11 10:58:10 +02006569
6570 /*
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02006571 * Session
6572 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01006573 if ((size_t) (end - p) < 4) {
6574 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
6575 }
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02006576
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01006577 session_len = ((size_t) p[0] << 24) |
6578 ((size_t) p[1] << 16) |
6579 ((size_t) p[2] << 8) |
6580 ((size_t) p[3]);
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02006581 p += 4;
6582
Manuel Pégourié-Gonnard142ba732019-07-23 14:43:30 +02006583 /* This has been allocated by ssl_handshake_init(), called by
Hanno Becker43aefe22020-02-05 10:44:56 +00006584 * by either mbedtls_ssl_session_reset_int() or mbedtls_ssl_setup(). */
Manuel Pégourié-Gonnard142ba732019-07-23 14:43:30 +02006585 ssl->session = ssl->session_negotiate;
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02006586 ssl->session_in = ssl->session;
6587 ssl->session_out = ssl->session;
Manuel Pégourié-Gonnard142ba732019-07-23 14:43:30 +02006588 ssl->session_negotiate = NULL;
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02006589
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01006590 if ((size_t) (end - p) < session_len) {
6591 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
6592 }
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02006593
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01006594 ret = ssl_session_load(ssl->session, 1, p, session_len);
6595 if (ret != 0) {
6596 mbedtls_ssl_session_free(ssl->session);
6597 return ret;
Manuel Pégourié-Gonnard45ac1f02019-07-23 16:52:45 +02006598 }
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02006599
6600 p += session_len;
6601
6602 /*
Manuel Pégourié-Gonnardc2a7b892019-07-15 09:04:11 +02006603 * Transform
6604 */
6605
Manuel Pégourié-Gonnard142ba732019-07-23 14:43:30 +02006606 /* This has been allocated by ssl_handshake_init(), called by
Hanno Becker43aefe22020-02-05 10:44:56 +00006607 * by either mbedtls_ssl_session_reset_int() or mbedtls_ssl_setup(). */
Manuel Pégourié-Gonnard142ba732019-07-23 14:43:30 +02006608 ssl->transform = ssl->transform_negotiate;
Manuel Pégourié-Gonnardc2a7b892019-07-15 09:04:11 +02006609 ssl->transform_in = ssl->transform;
6610 ssl->transform_out = ssl->transform;
Manuel Pégourié-Gonnard142ba732019-07-23 14:43:30 +02006611 ssl->transform_negotiate = NULL;
Manuel Pégourié-Gonnardc2a7b892019-07-15 09:04:11 +02006612
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01006613 prf_func = ssl_tls12prf_from_cs(ssl->session->ciphersuite);
6614 if (prf_func == NULL) {
6615 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
6616 }
Andrzej Kurekf9412f72022-10-18 07:30:19 -04006617
Manuel Pégourié-Gonnardc2a7b892019-07-15 09:04:11 +02006618 /* Read random bytes and populate structure */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01006619 if ((size_t) (end - p) < sizeof(ssl->transform->randbytes)) {
6620 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
6621 }
Manuel Pégourié-Gonnardc2a7b892019-07-15 09:04:11 +02006622
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01006623 ret = ssl_populate_transform(ssl->transform,
6624 ssl->session->ciphersuite,
6625 ssl->session->master,
Manuel Pégourié-Gonnardc2a7b892019-07-15 09:04:11 +02006626#if defined(MBEDTLS_SSL_SOME_MODES_USE_MAC)
6627#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01006628 ssl->session->encrypt_then_mac,
Manuel Pégourié-Gonnardc2a7b892019-07-15 09:04:11 +02006629#endif
6630#if defined(MBEDTLS_SSL_TRUNCATED_HMAC)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01006631 ssl->session->trunc_hmac,
Manuel Pégourié-Gonnardc2a7b892019-07-15 09:04:11 +02006632#endif
6633#endif /* MBEDTLS_SSL_SOME_MODES_USE_MAC */
6634#if defined(MBEDTLS_ZLIB_SUPPORT)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01006635 ssl->session->compression,
Manuel Pégourié-Gonnardc2a7b892019-07-15 09:04:11 +02006636#endif
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01006637 prf_func,
6638 p, /* currently pointing to randbytes */
6639 MBEDTLS_SSL_MINOR_VERSION_3, /* (D)TLS 1.2 is forced */
6640 ssl->conf->endpoint,
6641 ssl);
6642 if (ret != 0) {
6643 return ret;
6644 }
Manuel Pégourié-Gonnardc2a7b892019-07-15 09:04:11 +02006645
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01006646 p += sizeof(ssl->transform->randbytes);
Manuel Pégourié-Gonnardc2a7b892019-07-15 09:04:11 +02006647
6648#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
6649 /* Read connection IDs and store them */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01006650 if ((size_t) (end - p) < 1) {
6651 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
6652 }
Manuel Pégourié-Gonnardc2a7b892019-07-15 09:04:11 +02006653
6654 ssl->transform->in_cid_len = *p++;
6655
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01006656 if ((size_t) (end - p) < ssl->transform->in_cid_len + 1u) {
6657 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
6658 }
Manuel Pégourié-Gonnardc2a7b892019-07-15 09:04:11 +02006659
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01006660 memcpy(ssl->transform->in_cid, p, ssl->transform->in_cid_len);
Manuel Pégourié-Gonnardc2a7b892019-07-15 09:04:11 +02006661 p += ssl->transform->in_cid_len;
6662
6663 ssl->transform->out_cid_len = *p++;
6664
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01006665 if ((size_t) (end - p) < ssl->transform->out_cid_len) {
6666 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
6667 }
Manuel Pégourié-Gonnardc2a7b892019-07-15 09:04:11 +02006668
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01006669 memcpy(ssl->transform->out_cid, p, ssl->transform->out_cid_len);
Manuel Pégourié-Gonnardc2a7b892019-07-15 09:04:11 +02006670 p += ssl->transform->out_cid_len;
6671#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
6672
6673 /*
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02006674 * Saved fields from top-level ssl_context structure
6675 */
6676#if defined(MBEDTLS_SSL_DTLS_BADMAC_LIMIT)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01006677 if ((size_t) (end - p) < 4) {
6678 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
6679 }
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02006680
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01006681 ssl->badmac_seen = ((uint32_t) p[0] << 24) |
6682 ((uint32_t) p[1] << 16) |
6683 ((uint32_t) p[2] << 8) |
6684 ((uint32_t) p[3]);
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02006685 p += 4;
6686#endif /* MBEDTLS_SSL_DTLS_BADMAC_LIMIT */
6687
6688#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01006689 if ((size_t) (end - p) < 16) {
6690 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
6691 }
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02006692
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01006693 ssl->in_window_top = ((uint64_t) p[0] << 56) |
6694 ((uint64_t) p[1] << 48) |
6695 ((uint64_t) p[2] << 40) |
6696 ((uint64_t) p[3] << 32) |
6697 ((uint64_t) p[4] << 24) |
6698 ((uint64_t) p[5] << 16) |
6699 ((uint64_t) p[6] << 8) |
6700 ((uint64_t) p[7]);
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02006701 p += 8;
6702
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01006703 ssl->in_window = ((uint64_t) p[0] << 56) |
6704 ((uint64_t) p[1] << 48) |
6705 ((uint64_t) p[2] << 40) |
6706 ((uint64_t) p[3] << 32) |
6707 ((uint64_t) p[4] << 24) |
6708 ((uint64_t) p[5] << 16) |
6709 ((uint64_t) p[6] << 8) |
6710 ((uint64_t) p[7]);
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02006711 p += 8;
6712#endif /* MBEDTLS_SSL_DTLS_ANTI_REPLAY */
6713
6714#if defined(MBEDTLS_SSL_PROTO_DTLS)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01006715 if ((size_t) (end - p) < 1) {
6716 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
6717 }
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02006718
6719 ssl->disable_datagram_packing = *p++;
6720#endif /* MBEDTLS_SSL_PROTO_DTLS */
6721
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01006722 if ((size_t) (end - p) < 8) {
6723 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
6724 }
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02006725
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01006726 memcpy(ssl->cur_out_ctr, p, 8);
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02006727 p += 8;
6728
6729#if defined(MBEDTLS_SSL_PROTO_DTLS)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01006730 if ((size_t) (end - p) < 2) {
6731 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
6732 }
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02006733
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01006734 ssl->mtu = (p[0] << 8) | p[1];
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02006735 p += 2;
6736#endif /* MBEDTLS_SSL_PROTO_DTLS */
6737
6738#if defined(MBEDTLS_SSL_ALPN)
6739 {
6740 uint8_t alpn_len;
6741 const char **cur;
6742
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01006743 if ((size_t) (end - p) < 1) {
6744 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
6745 }
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02006746
6747 alpn_len = *p++;
6748
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01006749 if (alpn_len != 0 && ssl->conf->alpn_list != NULL) {
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02006750 /* alpn_chosen should point to an item in the configured list */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01006751 for (cur = ssl->conf->alpn_list; *cur != NULL; cur++) {
6752 if (strlen(*cur) == alpn_len &&
Waleed Elmelegy98ebf482024-03-15 14:29:24 +00006753 memcmp(p, *cur, alpn_len) == 0) {
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02006754 ssl->alpn_chosen = *cur;
6755 break;
6756 }
6757 }
6758 }
6759
6760 /* can only happen on conf mismatch */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01006761 if (alpn_len != 0 && ssl->alpn_chosen == NULL) {
6762 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
6763 }
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02006764
6765 p += alpn_len;
6766 }
6767#endif /* MBEDTLS_SSL_ALPN */
6768
6769 /*
Manuel Pégourié-Gonnard0eb3eac2019-07-15 11:53:51 +02006770 * Forced fields from top-level ssl_context structure
6771 *
6772 * Most of them already set to the correct value by mbedtls_ssl_init() and
6773 * mbedtls_ssl_reset(), so we only need to set the remaining ones.
6774 */
6775 ssl->state = MBEDTLS_SSL_HANDSHAKE_OVER;
6776
6777 ssl->major_ver = MBEDTLS_SSL_MAJOR_VERSION_3;
6778 ssl->minor_ver = MBEDTLS_SSL_MINOR_VERSION_3;
6779
Hanno Becker361b10d2019-08-30 10:42:49 +01006780 /* Adjust pointers for header fields of outgoing records to
6781 * the given transform, accounting for explicit IV and CID. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01006782 mbedtls_ssl_update_out_pointers(ssl, ssl->transform);
Hanno Becker361b10d2019-08-30 10:42:49 +01006783
Manuel Pégourié-Gonnard0eb3eac2019-07-15 11:53:51 +02006784#if defined(MBEDTLS_SSL_PROTO_DTLS)
6785 ssl->in_epoch = 1;
6786#endif
6787
6788 /* mbedtls_ssl_reset() leaves the handshake sub-structure allocated,
6789 * which we don't want - otherwise we'd end up freeing the wrong transform
Hanno Beckerce5f5fd2020-02-05 10:47:44 +00006790 * by calling mbedtls_ssl_handshake_wrapup_free_hs_transform()
6791 * inappropriately. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01006792 if (ssl->handshake != NULL) {
6793 mbedtls_ssl_handshake_free(ssl);
6794 mbedtls_free(ssl->handshake);
Manuel Pégourié-Gonnard0eb3eac2019-07-15 11:53:51 +02006795 ssl->handshake = NULL;
6796 }
6797
6798 /*
Manuel Pégourié-Gonnard4c90e852019-07-11 10:58:10 +02006799 * Done - should have consumed entire buffer
6800 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01006801 if (p != end) {
6802 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
6803 }
Manuel Pégourié-Gonnardac87e282019-05-28 13:02:16 +02006804
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01006805 return 0;
Manuel Pégourié-Gonnardac87e282019-05-28 13:02:16 +02006806}
6807
6808/*
Manuel Pégourié-Gonnardb9dfc9f2019-07-12 10:50:19 +02006809 * Deserialize context: public wrapper for error cleaning
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02006810 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01006811int mbedtls_ssl_context_load(mbedtls_ssl_context *context,
6812 const unsigned char *buf,
6813 size_t len)
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02006814{
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01006815 int ret = ssl_context_load(context, buf, len);
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02006816
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01006817 if (ret != 0) {
6818 mbedtls_ssl_free(context);
6819 }
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02006820
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01006821 return ret;
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02006822}
Manuel Pégourié-Gonnard5c0e3772019-07-23 16:13:17 +02006823#endif /* MBEDTLS_SSL_CONTEXT_SERIALIZATION */
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02006824
6825/*
Paul Bakker5121ce52009-01-03 21:22:43 +00006826 * Free an SSL context
6827 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01006828void mbedtls_ssl_free(mbedtls_ssl_context *ssl)
Paul Bakker5121ce52009-01-03 21:22:43 +00006829{
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01006830 if (ssl == NULL) {
Paul Bakkeraccaffe2014-06-26 13:37:14 +02006831 return;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01006832 }
Paul Bakkeraccaffe2014-06-26 13:37:14 +02006833
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01006834 MBEDTLS_SSL_DEBUG_MSG(2, ("=> free"));
Paul Bakker5121ce52009-01-03 21:22:43 +00006835
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01006836 if (ssl->out_buf != NULL) {
sander-visserb8aa2072020-05-06 22:05:13 +02006837#if defined(MBEDTLS_SSL_VARIABLE_BUFFER_LENGTH)
6838 size_t out_buf_len = ssl->out_buf_len;
6839#else
6840 size_t out_buf_len = MBEDTLS_SSL_OUT_BUFFER_LEN;
6841#endif
6842
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01006843 mbedtls_platform_zeroize(ssl->out_buf, out_buf_len);
6844 mbedtls_free(ssl->out_buf);
Andrzej Kurek0afa2a12020-03-03 10:39:58 -05006845 ssl->out_buf = NULL;
Paul Bakker5121ce52009-01-03 21:22:43 +00006846 }
6847
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01006848 if (ssl->in_buf != NULL) {
sander-visserb8aa2072020-05-06 22:05:13 +02006849#if defined(MBEDTLS_SSL_VARIABLE_BUFFER_LENGTH)
6850 size_t in_buf_len = ssl->in_buf_len;
6851#else
6852 size_t in_buf_len = MBEDTLS_SSL_IN_BUFFER_LEN;
6853#endif
6854
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01006855 mbedtls_platform_zeroize(ssl->in_buf, in_buf_len);
6856 mbedtls_free(ssl->in_buf);
Andrzej Kurek0afa2a12020-03-03 10:39:58 -05006857 ssl->in_buf = NULL;
Paul Bakker5121ce52009-01-03 21:22:43 +00006858 }
6859
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006860#if defined(MBEDTLS_ZLIB_SUPPORT)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01006861 if (ssl->compress_buf != NULL) {
6862 mbedtls_platform_zeroize(ssl->compress_buf, MBEDTLS_SSL_COMPRESS_BUFFER_LEN);
6863 mbedtls_free(ssl->compress_buf);
Paul Bakker16770332013-10-11 09:59:44 +02006864 }
6865#endif
6866
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01006867 if (ssl->transform) {
6868 mbedtls_ssl_transform_free(ssl->transform);
6869 mbedtls_free(ssl->transform);
Paul Bakker48916f92012-09-16 19:57:18 +00006870 }
6871
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01006872 if (ssl->handshake) {
6873 mbedtls_ssl_handshake_free(ssl);
6874 mbedtls_ssl_transform_free(ssl->transform_negotiate);
6875 mbedtls_ssl_session_free(ssl->session_negotiate);
Paul Bakker48916f92012-09-16 19:57:18 +00006876
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01006877 mbedtls_free(ssl->handshake);
6878 mbedtls_free(ssl->transform_negotiate);
6879 mbedtls_free(ssl->session_negotiate);
Paul Bakker48916f92012-09-16 19:57:18 +00006880 }
6881
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01006882 if (ssl->session) {
6883 mbedtls_ssl_session_free(ssl->session);
6884 mbedtls_free(ssl->session);
Paul Bakkerc0463502013-02-14 11:19:38 +01006885 }
6886
Manuel Pégourié-Gonnard55fab2d2015-05-11 16:15:19 +02006887#if defined(MBEDTLS_X509_CRT_PARSE_C)
Gilles Peskine3a2f75d2025-02-12 23:28:48 +01006888 mbedtls_ssl_free_hostname(ssl);
Paul Bakker0be444a2013-08-27 21:55:01 +02006889#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00006890
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006891#if defined(MBEDTLS_SSL_HW_RECORD_ACCEL)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01006892 if (mbedtls_ssl_hw_record_finish != NULL) {
6893 MBEDTLS_SSL_DEBUG_MSG(2, ("going for mbedtls_ssl_hw_record_finish()"));
6894 mbedtls_ssl_hw_record_finish(ssl);
Paul Bakker05ef8352012-05-08 09:17:57 +00006895 }
6896#endif
6897
Manuel Pégourié-Gonnarde057d3b2015-05-20 10:59:43 +02006898#if defined(MBEDTLS_SSL_DTLS_HELLO_VERIFY) && defined(MBEDTLS_SSL_SRV_C)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01006899 mbedtls_free(ssl->cli_id);
Manuel Pégourié-Gonnard43c02182014-07-22 17:32:01 +02006900#endif
6901
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01006902 MBEDTLS_SSL_DEBUG_MSG(2, ("<= free"));
Paul Bakker2da561c2009-02-05 18:00:28 +00006903
Paul Bakker86f04f42013-02-14 11:20:09 +01006904 /* Actually clear after last debug message */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01006905 mbedtls_platform_zeroize(ssl, sizeof(mbedtls_ssl_context));
Paul Bakker5121ce52009-01-03 21:22:43 +00006906}
6907
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02006908/*
Tom Cosgrove49f99bc2022-12-04 16:44:21 +00006909 * Initialize mbedtls_ssl_config
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02006910 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01006911void mbedtls_ssl_config_init(mbedtls_ssl_config *conf)
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02006912{
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01006913 memset(conf, 0, sizeof(mbedtls_ssl_config));
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02006914}
6915
Gilles Peskineeccd8882020-03-10 12:19:08 +01006916#if defined(MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED)
Chien Wongb6d57932024-02-07 21:48:12 +08006917static const int ssl_preset_default_hashes[] = {
Gilles Peskinec54010c2021-05-12 22:52:09 +02006918#if defined(MBEDTLS_SHA512_C)
Manuel Pégourié-Gonnard47229c72015-12-04 15:02:56 +01006919 MBEDTLS_MD_SHA512,
Gilles Peskinec54010c2021-05-12 22:52:09 +02006920#endif
6921#if defined(MBEDTLS_SHA512_C) && !defined(MBEDTLS_SHA512_NO_SHA384)
Manuel Pégourié-Gonnard47229c72015-12-04 15:02:56 +01006922 MBEDTLS_MD_SHA384,
6923#endif
6924#if defined(MBEDTLS_SHA256_C)
6925 MBEDTLS_MD_SHA256,
6926 MBEDTLS_MD_SHA224,
6927#endif
Gilles Peskine5d2511c2017-05-12 13:16:40 +02006928#if defined(MBEDTLS_SHA1_C) && defined(MBEDTLS_TLS_DEFAULT_ALLOW_SHA1_IN_KEY_EXCHANGE)
Manuel Pégourié-Gonnard47229c72015-12-04 15:02:56 +01006929 MBEDTLS_MD_SHA1,
6930#endif
6931 MBEDTLS_MD_NONE
6932};
Simon Butcherc97b6972015-12-27 23:48:17 +00006933#endif
Manuel Pégourié-Gonnard47229c72015-12-04 15:02:56 +01006934
Chien Wongb6d57932024-02-07 21:48:12 +08006935static const int ssl_preset_suiteb_ciphersuites[] = {
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02006936 MBEDTLS_TLS_ECDHE_ECDSA_WITH_AES_128_GCM_SHA256,
6937 MBEDTLS_TLS_ECDHE_ECDSA_WITH_AES_256_GCM_SHA384,
6938 0
6939};
6940
Gilles Peskineeccd8882020-03-10 12:19:08 +01006941#if defined(MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED)
Chien Wongb6d57932024-02-07 21:48:12 +08006942static const int ssl_preset_suiteb_hashes[] = {
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02006943 MBEDTLS_MD_SHA256,
6944 MBEDTLS_MD_SHA384,
6945 MBEDTLS_MD_NONE
6946};
6947#endif
6948
6949#if defined(MBEDTLS_ECP_C)
Chien Wongb6d57932024-02-07 21:48:12 +08006950static const mbedtls_ecp_group_id ssl_preset_suiteb_curves[] = {
Jaeden Amerod4311042019-06-03 08:27:16 +01006951#if defined(MBEDTLS_ECP_DP_SECP256R1_ENABLED)
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02006952 MBEDTLS_ECP_DP_SECP256R1,
Jaeden Amerod4311042019-06-03 08:27:16 +01006953#endif
6954#if defined(MBEDTLS_ECP_DP_SECP384R1_ENABLED)
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02006955 MBEDTLS_ECP_DP_SECP384R1,
Jaeden Amerod4311042019-06-03 08:27:16 +01006956#endif
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02006957 MBEDTLS_ECP_DP_NONE
6958};
6959#endif
6960
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02006961/*
Tillmann Karras588ad502015-09-25 04:27:22 +02006962 * Load default in mbedtls_ssl_config
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02006963 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01006964int mbedtls_ssl_config_defaults(mbedtls_ssl_config *conf,
6965 int endpoint, int transport, int preset)
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02006966{
Manuel Pégourié-Gonnard8b431fb2015-05-11 12:54:52 +02006967#if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_SSL_SRV_C)
Janos Follath865b3eb2019-12-16 11:46:15 +00006968 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnard8b431fb2015-05-11 12:54:52 +02006969#endif
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02006970
Manuel Pégourié-Gonnard0de074f2015-05-14 12:58:01 +02006971 /* Use the functions here so that they are covered in tests,
6972 * but otherwise access member directly for efficiency */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01006973 mbedtls_ssl_conf_endpoint(conf, endpoint);
6974 mbedtls_ssl_conf_transport(conf, transport);
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02006975
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02006976 /*
6977 * Things that are common to all presets
6978 */
Manuel Pégourié-Gonnard419d5ae2015-05-04 19:32:36 +02006979#if defined(MBEDTLS_SSL_CLI_C)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01006980 if (endpoint == MBEDTLS_SSL_IS_CLIENT) {
Manuel Pégourié-Gonnard419d5ae2015-05-04 19:32:36 +02006981 conf->authmode = MBEDTLS_SSL_VERIFY_REQUIRED;
6982#if defined(MBEDTLS_SSL_SESSION_TICKETS)
6983 conf->session_tickets = MBEDTLS_SSL_SESSION_TICKETS_ENABLED;
6984#endif
6985 }
6986#endif
6987
Manuel Pégourié-Gonnard66dc5552015-05-14 12:28:21 +02006988#if defined(MBEDTLS_ARC4_C)
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02006989 conf->arc4_disabled = MBEDTLS_SSL_ARC4_DISABLED;
Manuel Pégourié-Gonnard66dc5552015-05-14 12:28:21 +02006990#endif
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02006991
6992#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
6993 conf->encrypt_then_mac = MBEDTLS_SSL_ETM_ENABLED;
6994#endif
6995
6996#if defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET)
6997 conf->extended_ms = MBEDTLS_SSL_EXTENDED_MS_ENABLED;
6998#endif
6999
Manuel Pégourié-Gonnard17eab2b2015-05-05 16:34:53 +01007000#if defined(MBEDTLS_SSL_CBC_RECORD_SPLITTING)
7001 conf->cbc_record_splitting = MBEDTLS_SSL_CBC_RECORD_SPLITTING_ENABLED;
7002#endif
7003
Manuel Pégourié-Gonnarde057d3b2015-05-20 10:59:43 +02007004#if defined(MBEDTLS_SSL_DTLS_HELLO_VERIFY) && defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02007005 conf->f_cookie_write = ssl_cookie_write_dummy;
7006 conf->f_cookie_check = ssl_cookie_check_dummy;
7007#endif
7008
7009#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
7010 conf->anti_replay = MBEDTLS_SSL_ANTI_REPLAY_ENABLED;
7011#endif
7012
Janos Follath088ce432017-04-10 12:42:31 +01007013#if defined(MBEDTLS_SSL_SRV_C)
7014 conf->cert_req_ca_list = MBEDTLS_SSL_CERT_REQ_CA_LIST_ENABLED;
7015#endif
7016
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02007017#if defined(MBEDTLS_SSL_PROTO_DTLS)
7018 conf->hs_timeout_min = MBEDTLS_SSL_DTLS_TIMEOUT_DFL_MIN;
7019 conf->hs_timeout_max = MBEDTLS_SSL_DTLS_TIMEOUT_DFL_MAX;
7020#endif
7021
7022#if defined(MBEDTLS_SSL_RENEGOTIATION)
7023 conf->renego_max_records = MBEDTLS_SSL_RENEGO_MAX_RECORDS_DEFAULT;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01007024 memset(conf->renego_period, 0x00, 2);
7025 memset(conf->renego_period + 2, 0xFF, 6);
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02007026#endif
7027
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02007028#if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_SSL_SRV_C)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01007029 if (endpoint == MBEDTLS_SSL_IS_SERVER) {
7030 const unsigned char dhm_p[] =
7031 MBEDTLS_DHM_RFC3526_MODP_2048_P_BIN;
7032 const unsigned char dhm_g[] =
7033 MBEDTLS_DHM_RFC3526_MODP_2048_G_BIN;
Hanno Becker00d0a682017-10-04 13:14:29 +01007034
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01007035 if ((ret = mbedtls_ssl_conf_dh_param_bin(conf,
7036 dhm_p, sizeof(dhm_p),
7037 dhm_g, sizeof(dhm_g))) != 0) {
7038 return ret;
7039 }
7040 }
Manuel Pégourié-Gonnardbd990d62015-06-11 14:49:42 +02007041#endif
7042
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02007043 /*
7044 * Preset-specific defaults
7045 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01007046 switch (preset) {
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02007047 /*
7048 * NSA Suite B
7049 */
7050 case MBEDTLS_SSL_PRESET_SUITEB:
7051 conf->min_major_ver = MBEDTLS_SSL_MAJOR_VERSION_3;
7052 conf->min_minor_ver = MBEDTLS_SSL_MINOR_VERSION_3; /* TLS 1.2 */
7053 conf->max_major_ver = MBEDTLS_SSL_MAX_MAJOR_VERSION;
7054 conf->max_minor_ver = MBEDTLS_SSL_MAX_MINOR_VERSION;
7055
7056 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_0] =
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01007057 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_1] =
7058 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_2] =
7059 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_3] =
7060 ssl_preset_suiteb_ciphersuites;
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02007061
7062#if defined(MBEDTLS_X509_CRT_PARSE_C)
7063 conf->cert_profile = &mbedtls_x509_crt_profile_suiteb;
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02007064#endif
7065
Gilles Peskineeccd8882020-03-10 12:19:08 +01007066#if defined(MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED)
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02007067 conf->sig_hashes = ssl_preset_suiteb_hashes;
7068#endif
7069
7070#if defined(MBEDTLS_ECP_C)
7071 conf->curve_list = ssl_preset_suiteb_curves;
7072#endif
Manuel Pégourié-Gonnardc98204e2015-08-11 04:21:01 +02007073 break;
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02007074
7075 /*
7076 * Default
7077 */
7078 default:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01007079 conf->min_major_ver = (MBEDTLS_SSL_MIN_MAJOR_VERSION >
7080 MBEDTLS_SSL_MIN_VALID_MAJOR_VERSION) ?
7081 MBEDTLS_SSL_MIN_MAJOR_VERSION :
7082 MBEDTLS_SSL_MIN_VALID_MAJOR_VERSION;
7083 conf->min_minor_ver = (MBEDTLS_SSL_MIN_MINOR_VERSION >
7084 MBEDTLS_SSL_MIN_VALID_MINOR_VERSION) ?
7085 MBEDTLS_SSL_MIN_MINOR_VERSION :
7086 MBEDTLS_SSL_MIN_VALID_MINOR_VERSION;
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02007087 conf->max_major_ver = MBEDTLS_SSL_MAX_MAJOR_VERSION;
7088 conf->max_minor_ver = MBEDTLS_SSL_MAX_MINOR_VERSION;
7089
7090#if defined(MBEDTLS_SSL_PROTO_DTLS)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01007091 if (transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM) {
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02007092 conf->min_minor_ver = MBEDTLS_SSL_MINOR_VERSION_2;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01007093 }
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02007094#endif
7095
7096 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_0] =
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01007097 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_1] =
7098 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_2] =
7099 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_3] =
7100 mbedtls_ssl_list_ciphersuites();
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02007101
7102#if defined(MBEDTLS_X509_CRT_PARSE_C)
7103 conf->cert_profile = &mbedtls_x509_crt_profile_default;
7104#endif
7105
Gilles Peskineeccd8882020-03-10 12:19:08 +01007106#if defined(MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED)
Manuel Pégourié-Gonnard47229c72015-12-04 15:02:56 +01007107 conf->sig_hashes = ssl_preset_default_hashes;
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02007108#endif
7109
7110#if defined(MBEDTLS_ECP_C)
7111 conf->curve_list = mbedtls_ecp_grp_id_list();
7112#endif
7113
7114#if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_SSL_CLI_C)
7115 conf->dhm_min_bitlen = 1024;
7116#endif
7117 }
7118
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01007119 return 0;
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02007120}
7121
7122/*
7123 * Free mbedtls_ssl_config
7124 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01007125void mbedtls_ssl_config_free(mbedtls_ssl_config *conf)
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02007126{
7127#if defined(MBEDTLS_DHM_C)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01007128 mbedtls_mpi_free(&conf->dhm_P);
7129 mbedtls_mpi_free(&conf->dhm_G);
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02007130#endif
7131
Gilles Peskineeccd8882020-03-10 12:19:08 +01007132#if defined(MBEDTLS_KEY_EXCHANGE_SOME_PSK_ENABLED)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01007133 if (conf->psk != NULL) {
7134 mbedtls_platform_zeroize(conf->psk, conf->psk_len);
7135 mbedtls_free(conf->psk);
Azim Khan27e8a122018-03-21 14:24:11 +00007136 conf->psk = NULL;
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02007137 conf->psk_len = 0;
junyeonLEE316b1622017-12-20 16:29:30 +09007138 }
7139
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01007140 if (conf->psk_identity != NULL) {
7141 mbedtls_platform_zeroize(conf->psk_identity, conf->psk_identity_len);
7142 mbedtls_free(conf->psk_identity);
Azim Khan27e8a122018-03-21 14:24:11 +00007143 conf->psk_identity = NULL;
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02007144 conf->psk_identity_len = 0;
7145 }
7146#endif
7147
7148#if defined(MBEDTLS_X509_CRT_PARSE_C)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01007149 ssl_key_cert_free(conf->key_cert);
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02007150#endif
7151
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01007152 mbedtls_platform_zeroize(conf, sizeof(mbedtls_ssl_config));
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02007153}
7154
Manuel Pégourié-Gonnard5674a972015-10-19 15:14:03 +02007155#if defined(MBEDTLS_PK_C) && \
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01007156 (defined(MBEDTLS_RSA_C) || defined(MBEDTLS_ECDSA_C))
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02007157/*
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007158 * Convert between MBEDTLS_PK_XXX and SSL_SIG_XXX
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02007159 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01007160unsigned char mbedtls_ssl_sig_from_pk(mbedtls_pk_context *pk)
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02007161{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007162#if defined(MBEDTLS_RSA_C)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01007163 if (mbedtls_pk_can_do(pk, MBEDTLS_PK_RSA)) {
7164 return MBEDTLS_SSL_SIG_RSA;
7165 }
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02007166#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007167#if defined(MBEDTLS_ECDSA_C)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01007168 if (mbedtls_pk_can_do(pk, MBEDTLS_PK_ECDSA)) {
7169 return MBEDTLS_SSL_SIG_ECDSA;
7170 }
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02007171#endif
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01007172 return MBEDTLS_SSL_SIG_ANON;
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02007173}
7174
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01007175unsigned char mbedtls_ssl_sig_from_pk_alg(mbedtls_pk_type_t type)
Hanno Becker7e5437a2017-04-28 17:15:26 +01007176{
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01007177 switch (type) {
Hanno Becker7e5437a2017-04-28 17:15:26 +01007178 case MBEDTLS_PK_RSA:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01007179 return MBEDTLS_SSL_SIG_RSA;
Hanno Becker7e5437a2017-04-28 17:15:26 +01007180 case MBEDTLS_PK_ECDSA:
7181 case MBEDTLS_PK_ECKEY:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01007182 return MBEDTLS_SSL_SIG_ECDSA;
Hanno Becker7e5437a2017-04-28 17:15:26 +01007183 default:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01007184 return MBEDTLS_SSL_SIG_ANON;
Hanno Becker7e5437a2017-04-28 17:15:26 +01007185 }
7186}
7187
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01007188mbedtls_pk_type_t mbedtls_ssl_pk_alg_from_sig(unsigned char sig)
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02007189{
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01007190 switch (sig) {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007191#if defined(MBEDTLS_RSA_C)
7192 case MBEDTLS_SSL_SIG_RSA:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01007193 return MBEDTLS_PK_RSA;
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02007194#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007195#if defined(MBEDTLS_ECDSA_C)
7196 case MBEDTLS_SSL_SIG_ECDSA:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01007197 return MBEDTLS_PK_ECDSA;
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02007198#endif
7199 default:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01007200 return MBEDTLS_PK_NONE;
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02007201 }
7202}
Manuel Pégourié-Gonnard5674a972015-10-19 15:14:03 +02007203#endif /* MBEDTLS_PK_C && ( MBEDTLS_RSA_C || MBEDTLS_ECDSA_C ) */
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02007204
Hanno Becker7e5437a2017-04-28 17:15:26 +01007205#if defined(MBEDTLS_SSL_PROTO_TLS1_2) && \
Gilles Peskineeccd8882020-03-10 12:19:08 +01007206 defined(MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED)
Hanno Becker7e5437a2017-04-28 17:15:26 +01007207
7208/* Find an entry in a signature-hash set matching a given hash algorithm. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01007209mbedtls_md_type_t mbedtls_ssl_sig_hash_set_find(mbedtls_ssl_sig_hash_set_t *set,
7210 mbedtls_pk_type_t sig_alg)
Hanno Becker7e5437a2017-04-28 17:15:26 +01007211{
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01007212 switch (sig_alg) {
Hanno Becker7e5437a2017-04-28 17:15:26 +01007213 case MBEDTLS_PK_RSA:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01007214 return set->rsa;
Hanno Becker7e5437a2017-04-28 17:15:26 +01007215 case MBEDTLS_PK_ECDSA:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01007216 return set->ecdsa;
Hanno Becker7e5437a2017-04-28 17:15:26 +01007217 default:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01007218 return MBEDTLS_MD_NONE;
Hanno Becker7e5437a2017-04-28 17:15:26 +01007219 }
7220}
7221
7222/* Add a signature-hash-pair to a signature-hash set */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01007223void mbedtls_ssl_sig_hash_set_add(mbedtls_ssl_sig_hash_set_t *set,
7224 mbedtls_pk_type_t sig_alg,
7225 mbedtls_md_type_t md_alg)
Hanno Becker7e5437a2017-04-28 17:15:26 +01007226{
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01007227 switch (sig_alg) {
Hanno Becker7e5437a2017-04-28 17:15:26 +01007228 case MBEDTLS_PK_RSA:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01007229 if (set->rsa == MBEDTLS_MD_NONE) {
Hanno Becker7e5437a2017-04-28 17:15:26 +01007230 set->rsa = md_alg;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01007231 }
Hanno Becker7e5437a2017-04-28 17:15:26 +01007232 break;
7233
7234 case MBEDTLS_PK_ECDSA:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01007235 if (set->ecdsa == MBEDTLS_MD_NONE) {
Hanno Becker7e5437a2017-04-28 17:15:26 +01007236 set->ecdsa = md_alg;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01007237 }
Hanno Becker7e5437a2017-04-28 17:15:26 +01007238 break;
7239
7240 default:
7241 break;
7242 }
7243}
7244
7245/* Allow exactly one hash algorithm for each signature. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01007246void mbedtls_ssl_sig_hash_set_const_hash(mbedtls_ssl_sig_hash_set_t *set,
7247 mbedtls_md_type_t md_alg)
Hanno Becker7e5437a2017-04-28 17:15:26 +01007248{
7249 set->rsa = md_alg;
7250 set->ecdsa = md_alg;
7251}
7252
7253#endif /* MBEDTLS_SSL_PROTO_TLS1_2) &&
Gilles Peskineeccd8882020-03-10 12:19:08 +01007254 MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED */
Hanno Becker7e5437a2017-04-28 17:15:26 +01007255
Manuel Pégourié-Gonnard1a483832013-09-20 12:29:15 +02007256/*
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +02007257 * Convert from MBEDTLS_SSL_HASH_XXX to MBEDTLS_MD_XXX
Manuel Pégourié-Gonnard1a483832013-09-20 12:29:15 +02007258 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01007259mbedtls_md_type_t mbedtls_ssl_md_alg_from_hash(unsigned char hash)
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02007260{
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01007261 switch (hash) {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007262#if defined(MBEDTLS_MD5_C)
7263 case MBEDTLS_SSL_HASH_MD5:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01007264 return MBEDTLS_MD_MD5;
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02007265#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007266#if defined(MBEDTLS_SHA1_C)
7267 case MBEDTLS_SSL_HASH_SHA1:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01007268 return MBEDTLS_MD_SHA1;
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02007269#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007270#if defined(MBEDTLS_SHA256_C)
7271 case MBEDTLS_SSL_HASH_SHA224:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01007272 return MBEDTLS_MD_SHA224;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007273 case MBEDTLS_SSL_HASH_SHA256:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01007274 return MBEDTLS_MD_SHA256;
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02007275#endif
Gilles Peskined2d59372021-05-12 22:43:27 +02007276#if defined(MBEDTLS_SHA512_C) && !defined(MBEDTLS_SHA512_NO_SHA384)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007277 case MBEDTLS_SSL_HASH_SHA384:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01007278 return MBEDTLS_MD_SHA384;
Gilles Peskinec54010c2021-05-12 22:52:09 +02007279#endif
7280#if defined(MBEDTLS_SHA512_C)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007281 case MBEDTLS_SSL_HASH_SHA512:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01007282 return MBEDTLS_MD_SHA512;
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02007283#endif
7284 default:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01007285 return MBEDTLS_MD_NONE;
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02007286 }
7287}
7288
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +02007289/*
7290 * Convert from MBEDTLS_MD_XXX to MBEDTLS_SSL_HASH_XXX
7291 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01007292unsigned char mbedtls_ssl_hash_from_md_alg(int md)
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +02007293{
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01007294 switch (md) {
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +02007295#if defined(MBEDTLS_MD5_C)
7296 case MBEDTLS_MD_MD5:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01007297 return MBEDTLS_SSL_HASH_MD5;
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +02007298#endif
7299#if defined(MBEDTLS_SHA1_C)
7300 case MBEDTLS_MD_SHA1:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01007301 return MBEDTLS_SSL_HASH_SHA1;
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +02007302#endif
7303#if defined(MBEDTLS_SHA256_C)
7304 case MBEDTLS_MD_SHA224:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01007305 return MBEDTLS_SSL_HASH_SHA224;
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +02007306 case MBEDTLS_MD_SHA256:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01007307 return MBEDTLS_SSL_HASH_SHA256;
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +02007308#endif
Gilles Peskined2d59372021-05-12 22:43:27 +02007309#if defined(MBEDTLS_SHA512_C) && !defined(MBEDTLS_SHA512_NO_SHA384)
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +02007310 case MBEDTLS_MD_SHA384:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01007311 return MBEDTLS_SSL_HASH_SHA384;
Gilles Peskinec54010c2021-05-12 22:52:09 +02007312#endif
7313#if defined(MBEDTLS_SHA512_C)
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +02007314 case MBEDTLS_MD_SHA512:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01007315 return MBEDTLS_SSL_HASH_SHA512;
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +02007316#endif
7317 default:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01007318 return MBEDTLS_SSL_HASH_NONE;
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +02007319 }
7320}
7321
Manuel Pégourié-Gonnardb541da62015-06-17 11:43:30 +02007322#if defined(MBEDTLS_ECP_C)
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01007323/*
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +02007324 * Check if a curve proposed by the peer is in our list.
Manuel Pégourié-Gonnard9d412d82015-06-17 12:10:46 +02007325 * Return 0 if we're willing to use it, -1 otherwise.
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01007326 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01007327int mbedtls_ssl_check_curve(const mbedtls_ssl_context *ssl, mbedtls_ecp_group_id grp_id)
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01007328{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007329 const mbedtls_ecp_group_id *gid;
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01007330
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01007331 if (ssl->conf->curve_list == NULL) {
7332 return -1;
7333 }
Manuel Pégourié-Gonnard9d412d82015-06-17 12:10:46 +02007334
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01007335 for (gid = ssl->conf->curve_list; *gid != MBEDTLS_ECP_DP_NONE; gid++) {
7336 if (*gid == grp_id) {
7337 return 0;
7338 }
7339 }
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01007340
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01007341 return -1;
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01007342}
Manuel Pégourié-Gonnard298d6cc2022-02-14 11:34:47 +01007343
7344/*
7345 * Same as mbedtls_ssl_check_curve() but takes a TLS ID for the curve.
7346 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01007347int mbedtls_ssl_check_curve_tls_id(const mbedtls_ssl_context *ssl, uint16_t tls_id)
Manuel Pégourié-Gonnard298d6cc2022-02-14 11:34:47 +01007348{
7349 const mbedtls_ecp_curve_info *curve_info =
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01007350 mbedtls_ecp_curve_info_from_tls_id(tls_id);
7351 if (curve_info == NULL) {
7352 return -1;
7353 }
7354 return mbedtls_ssl_check_curve(ssl, curve_info->grp_id);
Manuel Pégourié-Gonnard298d6cc2022-02-14 11:34:47 +01007355}
Manuel Pégourié-Gonnardb541da62015-06-17 11:43:30 +02007356#endif /* MBEDTLS_ECP_C */
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02007357
Gilles Peskineeccd8882020-03-10 12:19:08 +01007358#if defined(MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED)
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +02007359/*
7360 * Check if a hash proposed by the peer is in our list.
7361 * Return 0 if we're willing to use it, -1 otherwise.
7362 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01007363int mbedtls_ssl_check_sig_hash(const mbedtls_ssl_context *ssl,
7364 mbedtls_md_type_t md)
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +02007365{
7366 const int *cur;
7367
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01007368 if (ssl->conf->sig_hashes == NULL) {
7369 return -1;
7370 }
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +02007371
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01007372 for (cur = ssl->conf->sig_hashes; *cur != MBEDTLS_MD_NONE; cur++) {
7373 if (*cur == (int) md) {
7374 return 0;
7375 }
7376 }
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +02007377
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01007378 return -1;
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +02007379}
Gilles Peskineeccd8882020-03-10 12:19:08 +01007380#endif /* MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED */
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +02007381
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007382#if defined(MBEDTLS_X509_CRT_PARSE_C)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01007383int mbedtls_ssl_check_cert_usage(const mbedtls_x509_crt *cert,
7384 const mbedtls_ssl_ciphersuite_t *ciphersuite,
7385 int cert_endpoint,
7386 uint32_t *flags)
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02007387{
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +01007388 int ret = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007389#if defined(MBEDTLS_X509_CHECK_KEY_USAGE)
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02007390 int usage = 0;
7391#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007392#if defined(MBEDTLS_X509_CHECK_EXTENDED_KEY_USAGE)
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +02007393 const char *ext_oid;
7394 size_t ext_len;
7395#endif
7396
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007397#if !defined(MBEDTLS_X509_CHECK_KEY_USAGE) && \
7398 !defined(MBEDTLS_X509_CHECK_EXTENDED_KEY_USAGE)
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +02007399 ((void) cert);
7400 ((void) cert_endpoint);
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +01007401 ((void) flags);
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +02007402#endif
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02007403
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007404#if defined(MBEDTLS_X509_CHECK_KEY_USAGE)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01007405 if (cert_endpoint == MBEDTLS_SSL_IS_SERVER) {
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02007406 /* Server part of the key exchange */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01007407 switch (ciphersuite->key_exchange) {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007408 case MBEDTLS_KEY_EXCHANGE_RSA:
7409 case MBEDTLS_KEY_EXCHANGE_RSA_PSK:
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +01007410 usage = MBEDTLS_X509_KU_KEY_ENCIPHERMENT;
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02007411 break;
7412
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007413 case MBEDTLS_KEY_EXCHANGE_DHE_RSA:
7414 case MBEDTLS_KEY_EXCHANGE_ECDHE_RSA:
7415 case MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA:
7416 usage = MBEDTLS_X509_KU_DIGITAL_SIGNATURE;
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02007417 break;
7418
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007419 case MBEDTLS_KEY_EXCHANGE_ECDH_RSA:
7420 case MBEDTLS_KEY_EXCHANGE_ECDH_ECDSA:
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +01007421 usage = MBEDTLS_X509_KU_KEY_AGREEMENT;
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02007422 break;
7423
7424 /* Don't use default: we want warnings when adding new values */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007425 case MBEDTLS_KEY_EXCHANGE_NONE:
7426 case MBEDTLS_KEY_EXCHANGE_PSK:
7427 case MBEDTLS_KEY_EXCHANGE_DHE_PSK:
7428 case MBEDTLS_KEY_EXCHANGE_ECDHE_PSK:
Manuel Pégourié-Gonnard557535d2015-09-15 17:53:32 +02007429 case MBEDTLS_KEY_EXCHANGE_ECJPAKE:
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02007430 usage = 0;
7431 }
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01007432 } else {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007433 /* Client auth: we only implement rsa_sign and mbedtls_ecdsa_sign for now */
7434 usage = MBEDTLS_X509_KU_DIGITAL_SIGNATURE;
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02007435 }
7436
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01007437 if (mbedtls_x509_crt_check_key_usage(cert, usage) != 0) {
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +01007438 *flags |= MBEDTLS_X509_BADCERT_KEY_USAGE;
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +01007439 ret = -1;
7440 }
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +02007441#else
7442 ((void) ciphersuite);
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007443#endif /* MBEDTLS_X509_CHECK_KEY_USAGE */
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02007444
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007445#if defined(MBEDTLS_X509_CHECK_EXTENDED_KEY_USAGE)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01007446 if (cert_endpoint == MBEDTLS_SSL_IS_SERVER) {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007447 ext_oid = MBEDTLS_OID_SERVER_AUTH;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01007448 ext_len = MBEDTLS_OID_SIZE(MBEDTLS_OID_SERVER_AUTH);
7449 } else {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007450 ext_oid = MBEDTLS_OID_CLIENT_AUTH;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01007451 ext_len = MBEDTLS_OID_SIZE(MBEDTLS_OID_CLIENT_AUTH);
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +02007452 }
7453
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01007454 if (mbedtls_x509_crt_check_extended_key_usage(cert, ext_oid, ext_len) != 0) {
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +01007455 *flags |= MBEDTLS_X509_BADCERT_EXT_KEY_USAGE;
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +01007456 ret = -1;
7457 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007458#endif /* MBEDTLS_X509_CHECK_EXTENDED_KEY_USAGE */
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +02007459
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01007460 return ret;
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02007461}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007462#endif /* MBEDTLS_X509_CRT_PARSE_C */
Manuel Pégourié-Gonnard3a306b92014-04-29 15:11:17 +02007463
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01007464int mbedtls_ssl_set_calc_verify_md(mbedtls_ssl_context *ssl, int md)
Simon Butcher99000142016-10-13 17:21:01 +01007465{
7466#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01007467 if (ssl->minor_ver != MBEDTLS_SSL_MINOR_VERSION_3) {
Simon Butcher99000142016-10-13 17:21:01 +01007468 return MBEDTLS_ERR_SSL_INVALID_VERIFY_HASH;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01007469 }
Simon Butcher99000142016-10-13 17:21:01 +01007470
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01007471 switch (md) {
Simon Butcher99000142016-10-13 17:21:01 +01007472#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1)
7473#if defined(MBEDTLS_MD5_C)
7474 case MBEDTLS_SSL_HASH_MD5:
Janos Follath182013f2016-10-25 10:50:22 +01007475 return MBEDTLS_ERR_SSL_INVALID_VERIFY_HASH;
Simon Butcher99000142016-10-13 17:21:01 +01007476#endif
7477#if defined(MBEDTLS_SHA1_C)
7478 case MBEDTLS_SSL_HASH_SHA1:
7479 ssl->handshake->calc_verify = ssl_calc_verify_tls;
7480 break;
7481#endif
7482#endif /* MBEDTLS_SSL_PROTO_TLS1 || MBEDTLS_SSL_PROTO_TLS1_1 */
Gilles Peskined2d59372021-05-12 22:43:27 +02007483#if defined(MBEDTLS_SHA512_C) && !defined(MBEDTLS_SHA512_NO_SHA384)
Simon Butcher99000142016-10-13 17:21:01 +01007484 case MBEDTLS_SSL_HASH_SHA384:
7485 ssl->handshake->calc_verify = ssl_calc_verify_tls_sha384;
7486 break;
7487#endif
7488#if defined(MBEDTLS_SHA256_C)
7489 case MBEDTLS_SSL_HASH_SHA256:
7490 ssl->handshake->calc_verify = ssl_calc_verify_tls_sha256;
7491 break;
7492#endif
7493 default:
7494 return MBEDTLS_ERR_SSL_INVALID_VERIFY_HASH;
7495 }
7496
7497 return 0;
7498#else /* !MBEDTLS_SSL_PROTO_TLS1_2 */
7499 (void) ssl;
7500 (void) md;
7501
7502 return MBEDTLS_ERR_SSL_INVALID_VERIFY_HASH;
7503#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
7504}
7505
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +01007506#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1) || \
7507 defined(MBEDTLS_SSL_PROTO_TLS1_1)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01007508int mbedtls_ssl_get_key_exchange_md_ssl_tls(mbedtls_ssl_context *ssl,
7509 unsigned char *output,
7510 unsigned char *data, size_t data_len)
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +01007511{
7512 int ret = 0;
7513 mbedtls_md5_context mbedtls_md5;
7514 mbedtls_sha1_context mbedtls_sha1;
7515
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01007516 mbedtls_md5_init(&mbedtls_md5);
7517 mbedtls_sha1_init(&mbedtls_sha1);
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +01007518
7519 /*
7520 * digitally-signed struct {
7521 * opaque md5_hash[16];
7522 * opaque sha_hash[20];
7523 * };
7524 *
7525 * md5_hash
7526 * MD5(ClientHello.random + ServerHello.random
7527 * + ServerParams);
7528 * sha_hash
7529 * SHA(ClientHello.random + ServerHello.random
7530 * + ServerParams);
7531 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01007532 if ((ret = mbedtls_md5_starts_ret(&mbedtls_md5)) != 0) {
7533 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_md5_starts_ret", ret);
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +01007534 goto exit;
7535 }
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01007536 if ((ret = mbedtls_md5_update_ret(&mbedtls_md5,
7537 ssl->handshake->randbytes, 64)) != 0) {
7538 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_md5_update_ret", ret);
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +01007539 goto exit;
7540 }
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01007541 if ((ret = mbedtls_md5_update_ret(&mbedtls_md5, data, data_len)) != 0) {
7542 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_md5_update_ret", ret);
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +01007543 goto exit;
7544 }
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01007545 if ((ret = mbedtls_md5_finish_ret(&mbedtls_md5, output)) != 0) {
7546 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_md5_finish_ret", ret);
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +01007547 goto exit;
7548 }
7549
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01007550 if ((ret = mbedtls_sha1_starts_ret(&mbedtls_sha1)) != 0) {
7551 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_sha1_starts_ret", ret);
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +01007552 goto exit;
7553 }
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01007554 if ((ret = mbedtls_sha1_update_ret(&mbedtls_sha1,
7555 ssl->handshake->randbytes, 64)) != 0) {
7556 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_sha1_update_ret", ret);
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +01007557 goto exit;
7558 }
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01007559 if ((ret = mbedtls_sha1_update_ret(&mbedtls_sha1, data,
7560 data_len)) != 0) {
7561 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_sha1_update_ret", ret);
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +01007562 goto exit;
7563 }
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01007564 if ((ret = mbedtls_sha1_finish_ret(&mbedtls_sha1,
7565 output + 16)) != 0) {
7566 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_sha1_finish_ret", ret);
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +01007567 goto exit;
7568 }
7569
7570exit:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01007571 mbedtls_md5_free(&mbedtls_md5);
7572 mbedtls_sha1_free(&mbedtls_sha1);
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +01007573
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01007574 if (ret != 0) {
7575 mbedtls_ssl_send_alert_message(ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
7576 MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR);
7577 }
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +01007578
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01007579 return ret;
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +01007580
7581}
7582#endif /* MBEDTLS_SSL_PROTO_SSL3 || MBEDTLS_SSL_PROTO_TLS1 || \
7583 MBEDTLS_SSL_PROTO_TLS1_1 */
7584
7585#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1) || \
7586 defined(MBEDTLS_SSL_PROTO_TLS1_2)
Andrzej Kurekd6db9be2019-01-10 05:27:10 -05007587
7588#if defined(MBEDTLS_USE_PSA_CRYPTO)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01007589int mbedtls_ssl_get_key_exchange_md_tls1_2(mbedtls_ssl_context *ssl,
7590 unsigned char *hash, size_t *hashlen,
7591 unsigned char *data, size_t data_len,
7592 mbedtls_md_type_t md_alg)
Andrzej Kurekd6db9be2019-01-10 05:27:10 -05007593{
Andrzej Kurek814feff2019-01-14 04:35:19 -05007594 psa_status_t status;
Jaeden Amero34973232019-02-20 10:32:28 +00007595 psa_hash_operation_t hash_operation = PSA_HASH_OPERATION_INIT;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01007596 psa_algorithm_t hash_alg = mbedtls_psa_translate_md(md_alg);
Andrzej Kurekd6db9be2019-01-10 05:27:10 -05007597
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01007598 MBEDTLS_SSL_DEBUG_MSG(3, ("Perform PSA-based computation of digest of ServerKeyExchange"));
Andrzej Kurek814feff2019-01-14 04:35:19 -05007599
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01007600 if ((status = psa_hash_setup(&hash_operation,
7601 hash_alg)) != PSA_SUCCESS) {
7602 MBEDTLS_SSL_DEBUG_RET(1, "psa_hash_setup", status);
Andrzej Kurekd6db9be2019-01-10 05:27:10 -05007603 goto exit;
7604 }
7605
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01007606 if ((status = psa_hash_update(&hash_operation, ssl->handshake->randbytes,
7607 64)) != PSA_SUCCESS) {
7608 MBEDTLS_SSL_DEBUG_RET(1, "psa_hash_update", status);
Andrzej Kurekd6db9be2019-01-10 05:27:10 -05007609 goto exit;
7610 }
7611
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01007612 if ((status = psa_hash_update(&hash_operation,
7613 data, data_len)) != PSA_SUCCESS) {
7614 MBEDTLS_SSL_DEBUG_RET(1, "psa_hash_update", status);
Andrzej Kurekd6db9be2019-01-10 05:27:10 -05007615 goto exit;
7616 }
7617
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01007618 if ((status = psa_hash_finish(&hash_operation, hash, PSA_HASH_MAX_SIZE,
7619 hashlen)) != PSA_SUCCESS) {
7620 MBEDTLS_SSL_DEBUG_RET(1, "psa_hash_finish", status);
7621 goto exit;
Andrzej Kurekd6db9be2019-01-10 05:27:10 -05007622 }
7623
7624exit:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01007625 if (status != PSA_SUCCESS) {
7626 mbedtls_ssl_send_alert_message(ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
7627 MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR);
7628 switch (status) {
Andrzej Kurekd6db9be2019-01-10 05:27:10 -05007629 case PSA_ERROR_NOT_SUPPORTED:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01007630 return MBEDTLS_ERR_MD_FEATURE_UNAVAILABLE;
Andrzej Kurek814feff2019-01-14 04:35:19 -05007631 case PSA_ERROR_BAD_STATE: /* Intentional fallthrough */
Andrzej Kurekd6db9be2019-01-10 05:27:10 -05007632 case PSA_ERROR_BUFFER_TOO_SMALL:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01007633 return MBEDTLS_ERR_MD_BAD_INPUT_DATA;
Andrzej Kurekd6db9be2019-01-10 05:27:10 -05007634 case PSA_ERROR_INSUFFICIENT_MEMORY:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01007635 return MBEDTLS_ERR_MD_ALLOC_FAILED;
Andrzej Kurekd6db9be2019-01-10 05:27:10 -05007636 default:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01007637 return MBEDTLS_ERR_MD_HW_ACCEL_FAILED;
Andrzej Kurekd6db9be2019-01-10 05:27:10 -05007638 }
7639 }
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01007640 return 0;
Andrzej Kurekd6db9be2019-01-10 05:27:10 -05007641}
7642
7643#else
7644
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01007645int mbedtls_ssl_get_key_exchange_md_tls1_2(mbedtls_ssl_context *ssl,
7646 unsigned char *hash, size_t *hashlen,
7647 unsigned char *data, size_t data_len,
7648 mbedtls_md_type_t md_alg)
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +01007649{
7650 int ret = 0;
7651 mbedtls_md_context_t ctx;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01007652 const mbedtls_md_info_t *md_info = mbedtls_md_info_from_type(md_alg);
7653 *hashlen = mbedtls_md_get_size(md_info);
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +01007654
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01007655 MBEDTLS_SSL_DEBUG_MSG(3, ("Perform mbedtls-based computation of digest of ServerKeyExchange"));
Andrzej Kurek814feff2019-01-14 04:35:19 -05007656
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01007657 mbedtls_md_init(&ctx);
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +01007658
7659 /*
7660 * digitally-signed struct {
7661 * opaque client_random[32];
7662 * opaque server_random[32];
7663 * ServerDHParams params;
7664 * };
7665 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01007666 if ((ret = mbedtls_md_setup(&ctx, md_info, 0)) != 0) {
7667 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_md_setup", ret);
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +01007668 goto exit;
7669 }
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01007670 if ((ret = mbedtls_md_starts(&ctx)) != 0) {
7671 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_md_starts", ret);
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +01007672 goto exit;
7673 }
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01007674 if ((ret = mbedtls_md_update(&ctx, ssl->handshake->randbytes, 64)) != 0) {
7675 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_md_update", ret);
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +01007676 goto exit;
7677 }
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01007678 if ((ret = mbedtls_md_update(&ctx, data, data_len)) != 0) {
7679 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_md_update", ret);
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +01007680 goto exit;
7681 }
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01007682 if ((ret = mbedtls_md_finish(&ctx, hash)) != 0) {
7683 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_md_finish", ret);
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +01007684 goto exit;
7685 }
7686
7687exit:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01007688 mbedtls_md_free(&ctx);
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +01007689
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01007690 if (ret != 0) {
7691 mbedtls_ssl_send_alert_message(ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
7692 MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR);
7693 }
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +01007694
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01007695 return ret;
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +01007696}
Andrzej Kurekd6db9be2019-01-10 05:27:10 -05007697#endif /* MBEDTLS_USE_PSA_CRYPTO */
7698
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +01007699#endif /* MBEDTLS_SSL_PROTO_TLS1 || MBEDTLS_SSL_PROTO_TLS1_1 || \
7700 MBEDTLS_SSL_PROTO_TLS1_2 */
7701
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007702#endif /* MBEDTLS_SSL_TLS_C */