blob: 91b4ae947321e3b645046a6efb05263aeeea633f [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"));
Gilles Peskine551756d2025-02-13 14:39:02 +01002620#if !defined(MBEDTLS_SSL_CLI_ALLOW_WEAK_CERTIFICATE_VERIFICATION_WITHOUT_HOSTNAME)
2621 if (ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT &&
2622 ssl->conf->authmode == MBEDTLS_SSL_VERIFY_REQUIRED) {
2623 return MBEDTLS_ERR_SSL_CERTIFICATE_VERIFICATION_WITHOUT_HOSTNAME;
2624 }
2625#endif
Gilles Peskine3a2f75d2025-02-12 23:28:48 +01002626 }
2627
2628 *hostname = mbedtls_ssl_get_hostname_pointer(ssl);
2629 if (*hostname == NULL) {
2630 MBEDTLS_SSL_DEBUG_MSG(2, ("Certificate verification without CN verification"));
2631 }
2632
2633 return 0;
2634}
2635
Manuel Pégourié-Gonnardd904d662022-06-17 10:24:00 +02002636MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002637static int ssl_parse_certificate_verify(mbedtls_ssl_context *ssl,
2638 int authmode,
2639 mbedtls_x509_crt *chain,
2640 void *rs_ctx)
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02002641{
Hanno Becker28f2fcd2019-02-07 10:11:07 +00002642 const mbedtls_ssl_ciphersuite_t *ciphersuite_info =
Hanno Beckere694c3e2017-12-27 21:34:08 +00002643 ssl->handshake->ciphersuite_info;
Hanno Beckerafd0b0a2019-03-27 16:55:01 +00002644 int have_ca_chain = 0;
Hanno Becker68636192019-02-05 14:36:34 +00002645
Hanno Becker8927c832019-04-03 12:52:50 +01002646 int (*f_vrfy)(void *, mbedtls_x509_crt *, int, uint32_t *);
2647 void *p_vrfy;
2648
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002649 if (authmode == MBEDTLS_SSL_VERIFY_NONE) {
2650 return 0;
2651 }
Hanno Becker68636192019-02-05 14:36:34 +00002652
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002653 if (ssl->f_vrfy != NULL) {
2654 MBEDTLS_SSL_DEBUG_MSG(3, ("Use context-specific verification callback"));
Hanno Becker8927c832019-04-03 12:52:50 +01002655 f_vrfy = ssl->f_vrfy;
2656 p_vrfy = ssl->p_vrfy;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002657 } else {
2658 MBEDTLS_SSL_DEBUG_MSG(3, ("Use configuration-specific verification callback"));
Hanno Becker8927c832019-04-03 12:52:50 +01002659 f_vrfy = ssl->conf->f_vrfy;
2660 p_vrfy = ssl->conf->p_vrfy;
2661 }
2662
Gilles Peskine3a2f75d2025-02-12 23:28:48 +01002663 const char *hostname = "";
2664 int ret = get_hostname_for_verification(ssl, &hostname);
2665 if (ret != 0) {
2666 MBEDTLS_SSL_DEBUG_RET(1, "get_hostname_for_verification", ret);
2667 return ret;
2668 }
2669
Hanno Becker68636192019-02-05 14:36:34 +00002670 /*
2671 * Main check: verify certificate
2672 */
Hanno Beckerafd0b0a2019-03-27 16:55:01 +00002673#if defined(MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002674 if (ssl->conf->f_ca_cb != NULL) {
Hanno Beckerafd0b0a2019-03-27 16:55:01 +00002675 ((void) rs_ctx);
2676 have_ca_chain = 1;
2677
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002678 MBEDTLS_SSL_DEBUG_MSG(3, ("use CA callback for X.509 CRT verification"));
Jarno Lamsa9822c0d2019-04-01 16:59:48 +03002679 ret = mbedtls_x509_crt_verify_with_ca_cb(
Hanno Beckerafd0b0a2019-03-27 16:55:01 +00002680 chain,
2681 ssl->conf->f_ca_cb,
2682 ssl->conf->p_ca_cb,
2683 ssl->conf->cert_profile,
Gilles Peskine3a2f75d2025-02-12 23:28:48 +01002684 hostname,
Hanno Beckerafd0b0a2019-03-27 16:55:01 +00002685 &ssl->session_negotiate->verify_result,
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002686 f_vrfy, p_vrfy);
2687 } else
Hanno Beckerafd0b0a2019-03-27 16:55:01 +00002688#endif /* MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK */
2689 {
2690 mbedtls_x509_crt *ca_chain;
2691 mbedtls_x509_crl *ca_crl;
2692
2693#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002694 if (ssl->handshake->sni_ca_chain != NULL) {
Hanno Beckerafd0b0a2019-03-27 16:55:01 +00002695 ca_chain = ssl->handshake->sni_ca_chain;
2696 ca_crl = ssl->handshake->sni_ca_crl;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002697 } else
Hanno Beckerafd0b0a2019-03-27 16:55:01 +00002698#endif
2699 {
2700 ca_chain = ssl->conf->ca_chain;
2701 ca_crl = ssl->conf->ca_crl;
2702 }
2703
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002704 if (ca_chain != NULL) {
Hanno Beckerafd0b0a2019-03-27 16:55:01 +00002705 have_ca_chain = 1;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002706 }
Hanno Beckerafd0b0a2019-03-27 16:55:01 +00002707
2708 ret = mbedtls_x509_crt_verify_restartable(
2709 chain,
2710 ca_chain, ca_crl,
2711 ssl->conf->cert_profile,
Gilles Peskine3a2f75d2025-02-12 23:28:48 +01002712 hostname,
Hanno Beckerafd0b0a2019-03-27 16:55:01 +00002713 &ssl->session_negotiate->verify_result,
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002714 f_vrfy, p_vrfy, rs_ctx);
Hanno Beckerafd0b0a2019-03-27 16:55:01 +00002715 }
Hanno Becker68636192019-02-05 14:36:34 +00002716
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002717 if (ret != 0) {
2718 MBEDTLS_SSL_DEBUG_RET(1, "x509_verify_cert", ret);
Hanno Becker68636192019-02-05 14:36:34 +00002719 }
2720
Gilles Peskineeccd8882020-03-10 12:19:08 +01002721#if defined(MBEDTLS_SSL_ECP_RESTARTABLE_ENABLED)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002722 if (ret == MBEDTLS_ERR_ECP_IN_PROGRESS) {
2723 return MBEDTLS_ERR_SSL_CRYPTO_IN_PROGRESS;
2724 }
Hanno Becker68636192019-02-05 14:36:34 +00002725#endif
2726
2727 /*
2728 * Secondary checks: always done, but change 'ret' only if it was 0
2729 */
2730
2731#if defined(MBEDTLS_ECP_C)
2732 {
2733 const mbedtls_pk_context *pk = &chain->pk;
2734
Manuel Pégourié-Gonnard06e1fcd2022-06-16 10:48:06 +02002735 /* If certificate uses an EC key, make sure the curve is OK.
2736 * This is a public key, so it can't be opaque, so can_do() is a good
2737 * enough check to ensure pk_ec() is safe to use here. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002738 if (mbedtls_pk_can_do(pk, MBEDTLS_PK_ECKEY) &&
2739 mbedtls_ssl_check_curve(ssl, mbedtls_pk_ec(*pk)->grp.id) != 0) {
Hanno Becker68636192019-02-05 14:36:34 +00002740 ssl->session_negotiate->verify_result |= MBEDTLS_X509_BADCERT_BAD_KEY;
2741
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002742 MBEDTLS_SSL_DEBUG_MSG(1, ("bad certificate (EC key curve)"));
2743 if (ret == 0) {
Hanno Becker68636192019-02-05 14:36:34 +00002744 ret = MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002745 }
Hanno Becker68636192019-02-05 14:36:34 +00002746 }
2747 }
2748#endif /* MBEDTLS_ECP_C */
2749
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002750 if (mbedtls_ssl_check_cert_usage(chain,
2751 ciphersuite_info,
2752 !ssl->conf->endpoint,
2753 &ssl->session_negotiate->verify_result) != 0) {
2754 MBEDTLS_SSL_DEBUG_MSG(1, ("bad certificate (usage extensions)"));
2755 if (ret == 0) {
Hanno Becker68636192019-02-05 14:36:34 +00002756 ret = MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002757 }
Hanno Becker68636192019-02-05 14:36:34 +00002758 }
2759
2760 /* mbedtls_x509_crt_verify_with_profile is supposed to report a
2761 * verification failure through MBEDTLS_ERR_X509_CERT_VERIFY_FAILED,
2762 * with details encoded in the verification flags. All other kinds
2763 * of error codes, including those from the user provided f_vrfy
2764 * functions, are treated as fatal and lead to a failure of
2765 * ssl_parse_certificate even if verification was optional. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002766 if (authmode == MBEDTLS_SSL_VERIFY_OPTIONAL &&
2767 (ret == MBEDTLS_ERR_X509_CERT_VERIFY_FAILED ||
2768 ret == MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE)) {
Hanno Becker68636192019-02-05 14:36:34 +00002769 ret = 0;
2770 }
2771
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002772 if (have_ca_chain == 0 && authmode == MBEDTLS_SSL_VERIFY_REQUIRED) {
2773 MBEDTLS_SSL_DEBUG_MSG(1, ("got no CA chain"));
Hanno Becker68636192019-02-05 14:36:34 +00002774 ret = MBEDTLS_ERR_SSL_CA_CHAIN_REQUIRED;
2775 }
2776
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002777 if (ret != 0) {
Hanno Becker68636192019-02-05 14:36:34 +00002778 uint8_t alert;
2779
2780 /* The certificate may have been rejected for several reasons.
2781 Pick one and send the corresponding alert. Which alert to send
2782 may be a subject of debate in some cases. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002783 if (ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_OTHER) {
Hanno Becker68636192019-02-05 14:36:34 +00002784 alert = MBEDTLS_SSL_ALERT_MSG_ACCESS_DENIED;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002785 } else if (ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_CN_MISMATCH) {
Hanno Becker68636192019-02-05 14:36:34 +00002786 alert = MBEDTLS_SSL_ALERT_MSG_BAD_CERT;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002787 } else if (ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_KEY_USAGE) {
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_EXT_KEY_USAGE) {
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_NS_CERT_TYPE) {
Hanno Becker68636192019-02-05 14:36:34 +00002792 alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002793 } else if (ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_BAD_PK) {
Hanno Becker68636192019-02-05 14:36:34 +00002794 alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002795 } else if (ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_BAD_KEY) {
Hanno Becker68636192019-02-05 14:36:34 +00002796 alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002797 } else if (ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_EXPIRED) {
Hanno Becker68636192019-02-05 14:36:34 +00002798 alert = MBEDTLS_SSL_ALERT_MSG_CERT_EXPIRED;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002799 } else if (ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_REVOKED) {
Hanno Becker68636192019-02-05 14:36:34 +00002800 alert = MBEDTLS_SSL_ALERT_MSG_CERT_REVOKED;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002801 } else if (ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_NOT_TRUSTED) {
Hanno Becker68636192019-02-05 14:36:34 +00002802 alert = MBEDTLS_SSL_ALERT_MSG_UNKNOWN_CA;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002803 } else {
Hanno Becker68636192019-02-05 14:36:34 +00002804 alert = MBEDTLS_SSL_ALERT_MSG_CERT_UNKNOWN;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002805 }
2806 mbedtls_ssl_send_alert_message(ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
2807 alert);
Hanno Becker68636192019-02-05 14:36:34 +00002808 }
2809
2810#if defined(MBEDTLS_DEBUG_C)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002811 if (ssl->session_negotiate->verify_result != 0) {
2812 MBEDTLS_SSL_DEBUG_MSG(3, ("! Certificate verification flags %08x",
2813 (unsigned int) ssl->session_negotiate->verify_result));
2814 } else {
2815 MBEDTLS_SSL_DEBUG_MSG(3, ("Certificate verification flags clear"));
Hanno Becker68636192019-02-05 14:36:34 +00002816 }
2817#endif /* MBEDTLS_DEBUG_C */
2818
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002819 return ret;
Hanno Becker68636192019-02-05 14:36:34 +00002820}
2821
Hanno Becker6b8fbab2019-02-08 14:59:05 +00002822#if !defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
Manuel Pégourié-Gonnardd904d662022-06-17 10:24:00 +02002823MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002824static int ssl_remember_peer_crt_digest(mbedtls_ssl_context *ssl,
2825 unsigned char *start, size_t len)
Hanno Becker6b8fbab2019-02-08 14:59:05 +00002826{
Janos Follath865b3eb2019-12-16 11:46:15 +00002827 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Hanno Becker6b8fbab2019-02-08 14:59:05 +00002828 /* Remember digest of the peer's end-CRT. */
2829 ssl->session_negotiate->peer_cert_digest =
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002830 mbedtls_calloc(1, MBEDTLS_SSL_PEER_CERT_DIGEST_DFL_LEN);
2831 if (ssl->session_negotiate->peer_cert_digest == NULL) {
2832 MBEDTLS_SSL_DEBUG_MSG(1, ("alloc(%d bytes) failed",
2833 MBEDTLS_SSL_PEER_CERT_DIGEST_DFL_LEN));
2834 mbedtls_ssl_send_alert_message(ssl,
2835 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
2836 MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR);
Hanno Becker6b8fbab2019-02-08 14:59:05 +00002837
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002838 return MBEDTLS_ERR_SSL_ALLOC_FAILED;
Hanno Becker6b8fbab2019-02-08 14:59:05 +00002839 }
2840
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002841 ret = mbedtls_md(mbedtls_md_info_from_type(
2842 MBEDTLS_SSL_PEER_CERT_DIGEST_DFL_TYPE),
2843 start, len,
2844 ssl->session_negotiate->peer_cert_digest);
Hanno Becker6b8fbab2019-02-08 14:59:05 +00002845
2846 ssl->session_negotiate->peer_cert_digest_type =
2847 MBEDTLS_SSL_PEER_CERT_DIGEST_DFL_TYPE;
2848 ssl->session_negotiate->peer_cert_digest_len =
2849 MBEDTLS_SSL_PEER_CERT_DIGEST_DFL_LEN;
2850
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002851 return ret;
Hanno Becker6b8fbab2019-02-08 14:59:05 +00002852}
2853
Manuel Pégourié-Gonnardd904d662022-06-17 10:24:00 +02002854MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002855static int ssl_remember_peer_pubkey(mbedtls_ssl_context *ssl,
2856 unsigned char *start, size_t len)
Hanno Becker6b8fbab2019-02-08 14:59:05 +00002857{
2858 unsigned char *end = start + len;
Janos Follath865b3eb2019-12-16 11:46:15 +00002859 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Hanno Becker6b8fbab2019-02-08 14:59:05 +00002860
2861 /* Make a copy of the peer's raw public key. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002862 mbedtls_pk_init(&ssl->handshake->peer_pubkey);
2863 ret = mbedtls_pk_parse_subpubkey(&start, end,
2864 &ssl->handshake->peer_pubkey);
2865 if (ret != 0) {
Hanno Becker6b8fbab2019-02-08 14:59:05 +00002866 /* We should have parsed the public key before. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002867 return MBEDTLS_ERR_SSL_INTERNAL_ERROR;
Hanno Becker6b8fbab2019-02-08 14:59:05 +00002868 }
2869
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002870 return 0;
Hanno Becker6b8fbab2019-02-08 14:59:05 +00002871}
2872#endif /* !MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
2873
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002874int mbedtls_ssl_parse_certificate(mbedtls_ssl_context *ssl)
Hanno Becker68636192019-02-05 14:36:34 +00002875{
2876 int ret = 0;
Hanno Becker28f2fcd2019-02-07 10:11:07 +00002877 int crt_expected;
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02002878#if defined(MBEDTLS_SSL_SRV_C) && defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
2879 const int authmode = ssl->handshake->sni_authmode != MBEDTLS_SSL_VERIFY_UNSET
2880 ? ssl->handshake->sni_authmode
2881 : ssl->conf->authmode;
2882#else
Johan Pascal4f099262020-09-22 10:59:26 +02002883 const int authmode = ssl->conf->authmode;
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02002884#endif
Manuel Pégourié-Gonnard3bf49c42017-08-15 13:47:06 +02002885 void *rs_ctx = NULL;
Hanno Becker3dad3112019-02-05 17:19:52 +00002886 mbedtls_x509_crt *chain = NULL;
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02002887
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002888 MBEDTLS_SSL_DEBUG_MSG(2, ("=> parse certificate"));
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02002889
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002890 crt_expected = ssl_parse_certificate_coordinate(ssl, authmode);
2891 if (crt_expected == SSL_CERTIFICATE_SKIP) {
2892 MBEDTLS_SSL_DEBUG_MSG(2, ("<= skip parse certificate"));
Hanno Becker6bdfab22019-02-05 13:11:17 +00002893 goto exit;
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02002894 }
2895
Gilles Peskineeccd8882020-03-10 12:19:08 +01002896#if defined(MBEDTLS_SSL_ECP_RESTARTABLE_ENABLED)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002897 if (ssl->handshake->ecrs_enabled &&
2898 ssl->handshake->ecrs_state == ssl_ecrs_crt_verify) {
Hanno Becker3dad3112019-02-05 17:19:52 +00002899 chain = ssl->handshake->ecrs_peer_cert;
2900 ssl->handshake->ecrs_peer_cert = NULL;
Manuel Pégourié-Gonnard3bf49c42017-08-15 13:47:06 +02002901 goto crt_verify;
2902 }
2903#endif
2904
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002905 if ((ret = mbedtls_ssl_read_record(ssl, 1)) != 0) {
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02002906 /* mbedtls_ssl_read_record may have sent an alert already. We
2907 let it decide whether to alert. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002908 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ssl_read_record", ret);
Hanno Becker3dad3112019-02-05 17:19:52 +00002909 goto exit;
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02002910 }
2911
Hanno Becker4a55f632019-02-05 12:49:06 +00002912#if defined(MBEDTLS_SSL_SRV_C)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002913 if (ssl_srv_check_client_no_crt_notification(ssl) == 0) {
Hanno Becker4a55f632019-02-05 12:49:06 +00002914 ssl->session_negotiate->verify_result = MBEDTLS_X509_BADCERT_MISSING;
Hanno Becker4a55f632019-02-05 12:49:06 +00002915
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002916 if (authmode != MBEDTLS_SSL_VERIFY_OPTIONAL) {
Hanno Becker6bdfab22019-02-05 13:11:17 +00002917 ret = MBEDTLS_ERR_SSL_NO_CLIENT_CERTIFICATE;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002918 }
Hanno Becker4a55f632019-02-05 12:49:06 +00002919
Hanno Becker6bdfab22019-02-05 13:11:17 +00002920 goto exit;
Hanno Becker4a55f632019-02-05 12:49:06 +00002921 }
2922#endif /* MBEDTLS_SSL_SRV_C */
2923
Hanno Beckerc7bd7802019-02-05 15:37:23 +00002924 /* Clear existing peer CRT structure in case we tried to
2925 * reuse a session but it failed, and allocate a new one. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002926 ssl_clear_peer_cert(ssl->session_negotiate);
Hanno Becker3dad3112019-02-05 17:19:52 +00002927
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002928 chain = mbedtls_calloc(1, sizeof(mbedtls_x509_crt));
2929 if (chain == NULL) {
2930 MBEDTLS_SSL_DEBUG_MSG(1, ("alloc(%" MBEDTLS_PRINTF_SIZET " bytes) failed",
2931 sizeof(mbedtls_x509_crt)));
2932 mbedtls_ssl_send_alert_message(ssl,
2933 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
2934 MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR);
Hanno Becker7a955a02019-02-05 13:08:01 +00002935
Hanno Becker3dad3112019-02-05 17:19:52 +00002936 ret = MBEDTLS_ERR_SSL_ALLOC_FAILED;
2937 goto exit;
2938 }
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002939 mbedtls_x509_crt_init(chain);
Hanno Becker3dad3112019-02-05 17:19:52 +00002940
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002941 ret = ssl_parse_certificate_chain(ssl, chain);
2942 if (ret != 0) {
Hanno Becker3dad3112019-02-05 17:19:52 +00002943 goto exit;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002944 }
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02002945
Gilles Peskineeccd8882020-03-10 12:19:08 +01002946#if defined(MBEDTLS_SSL_ECP_RESTARTABLE_ENABLED)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002947 if (ssl->handshake->ecrs_enabled) {
Manuel Pégourié-Gonnard0b23f162017-08-24 12:08:33 +02002948 ssl->handshake->ecrs_state = ssl_ecrs_crt_verify;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002949 }
Manuel Pégourié-Gonnard3bf49c42017-08-15 13:47:06 +02002950
2951crt_verify:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002952 if (ssl->handshake->ecrs_enabled) {
Manuel Pégourié-Gonnard3bf49c42017-08-15 13:47:06 +02002953 rs_ctx = &ssl->handshake->ecrs_ctx;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002954 }
Manuel Pégourié-Gonnard3bf49c42017-08-15 13:47:06 +02002955#endif
2956
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002957 ret = ssl_parse_certificate_verify(ssl, authmode,
2958 chain, rs_ctx);
2959 if (ret != 0) {
Hanno Becker3dad3112019-02-05 17:19:52 +00002960 goto exit;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002961 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002962
Hanno Becker6bbd94c2019-02-05 17:02:28 +00002963#if !defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
Hanno Becker6bbd94c2019-02-05 17:02:28 +00002964 {
Hanno Becker6b8fbab2019-02-08 14:59:05 +00002965 unsigned char *crt_start, *pk_start;
2966 size_t crt_len, pk_len;
Hanno Becker3dad3112019-02-05 17:19:52 +00002967
Hanno Becker6b8fbab2019-02-08 14:59:05 +00002968 /* We parse the CRT chain without copying, so
2969 * these pointers point into the input buffer,
2970 * and are hence still valid after freeing the
2971 * CRT chain. */
Hanno Becker6bbd94c2019-02-05 17:02:28 +00002972
Hanno Becker6b8fbab2019-02-08 14:59:05 +00002973 crt_start = chain->raw.p;
2974 crt_len = chain->raw.len;
Hanno Becker6bbd94c2019-02-05 17:02:28 +00002975
Hanno Becker6b8fbab2019-02-08 14:59:05 +00002976 pk_start = chain->pk_raw.p;
2977 pk_len = chain->pk_raw.len;
2978
2979 /* Free the CRT structures before computing
2980 * digest and copying the peer's public key. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002981 mbedtls_x509_crt_free(chain);
2982 mbedtls_free(chain);
Hanno Becker6b8fbab2019-02-08 14:59:05 +00002983 chain = NULL;
2984
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002985 ret = ssl_remember_peer_crt_digest(ssl, crt_start, crt_len);
2986 if (ret != 0) {
Hanno Beckera2747532019-02-06 16:19:04 +00002987 goto exit;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002988 }
Hanno Becker6b8fbab2019-02-08 14:59:05 +00002989
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002990 ret = ssl_remember_peer_pubkey(ssl, pk_start, pk_len);
2991 if (ret != 0) {
Hanno Becker6b8fbab2019-02-08 14:59:05 +00002992 goto exit;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002993 }
Hanno Beckera2747532019-02-06 16:19:04 +00002994 }
Hanno Becker6b8fbab2019-02-08 14:59:05 +00002995#else /* !MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
2996 /* Pass ownership to session structure. */
Hanno Becker3dad3112019-02-05 17:19:52 +00002997 ssl->session_negotiate->peer_cert = chain;
2998 chain = NULL;
Hanno Becker6b8fbab2019-02-08 14:59:05 +00002999#endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
Hanno Becker3dad3112019-02-05 17:19:52 +00003000
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003001 MBEDTLS_SSL_DEBUG_MSG(2, ("<= parse certificate"));
Paul Bakker5121ce52009-01-03 21:22:43 +00003002
Hanno Becker6bdfab22019-02-05 13:11:17 +00003003exit:
3004
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003005 if (ret == 0) {
Hanno Becker3dad3112019-02-05 17:19:52 +00003006 ssl->state++;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003007 }
Hanno Becker3dad3112019-02-05 17:19:52 +00003008
Gilles Peskineeccd8882020-03-10 12:19:08 +01003009#if defined(MBEDTLS_SSL_ECP_RESTARTABLE_ENABLED)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003010 if (ret == MBEDTLS_ERR_SSL_CRYPTO_IN_PROGRESS) {
Hanno Becker3dad3112019-02-05 17:19:52 +00003011 ssl->handshake->ecrs_peer_cert = chain;
3012 chain = NULL;
3013 }
3014#endif
3015
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003016 if (chain != NULL) {
3017 mbedtls_x509_crt_free(chain);
3018 mbedtls_free(chain);
Hanno Becker3dad3112019-02-05 17:19:52 +00003019 }
3020
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003021 return ret;
Paul Bakker5121ce52009-01-03 21:22:43 +00003022}
Gilles Peskineeccd8882020-03-10 12:19:08 +01003023#endif /* MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED */
Paul Bakker5121ce52009-01-03 21:22:43 +00003024
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003025void mbedtls_ssl_optimize_checksum(mbedtls_ssl_context *ssl,
3026 const mbedtls_ssl_ciphersuite_t *ciphersuite_info)
Paul Bakker380da532012-04-18 16:10:25 +00003027{
Paul Bakkerfb08fd22013-08-27 15:06:26 +02003028 ((void) ciphersuite_info);
Paul Bakker769075d2012-11-24 11:26:46 +01003029
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003030#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1) || \
3031 defined(MBEDTLS_SSL_PROTO_TLS1_1)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003032 if (ssl->minor_ver < MBEDTLS_SSL_MINOR_VERSION_3) {
Paul Bakker48916f92012-09-16 19:57:18 +00003033 ssl->handshake->update_checksum = ssl_update_checksum_md5sha1;
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_SSL_PROTO_TLS1_2)
Gilles Peskined2d59372021-05-12 22:43:27 +02003037#if defined(MBEDTLS_SHA512_C) && !defined(MBEDTLS_SHA512_NO_SHA384)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003038 if (ciphersuite_info->mac == MBEDTLS_MD_SHA384) {
Paul Bakkerd2f068e2013-08-27 21:19:20 +02003039 ssl->handshake->update_checksum = ssl_update_checksum_sha384;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003040 } else
Paul Bakkerd2f068e2013-08-27 21:19:20 +02003041#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003042#if defined(MBEDTLS_SHA256_C)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003043 if (ciphersuite_info->mac != MBEDTLS_MD_SHA384) {
Paul Bakker48916f92012-09-16 19:57:18 +00003044 ssl->handshake->update_checksum = ssl_update_checksum_sha256;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003045 } else
Paul Bakkerd2f068e2013-08-27 21:19:20 +02003046#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003047#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Manuel Pégourié-Gonnard61edffe2014-04-11 17:07:31 +02003048 {
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003049 MBEDTLS_SSL_DEBUG_MSG(1, ("should never happen"));
Paul Bakkerd2f068e2013-08-27 21:19:20 +02003050 return;
Manuel Pégourié-Gonnard61edffe2014-04-11 17:07:31 +02003051 }
Paul Bakker380da532012-04-18 16:10:25 +00003052}
Paul Bakkerf7abd422013-04-16 13:15:56 +02003053
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003054void mbedtls_ssl_reset_checksum(mbedtls_ssl_context *ssl)
Manuel Pégourié-Gonnard67427c02014-07-11 13:45:34 +02003055{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003056#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1) || \
3057 defined(MBEDTLS_SSL_PROTO_TLS1_1)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003058 mbedtls_md5_starts_ret(&ssl->handshake->fin_md5);
3059 mbedtls_sha1_starts_ret(&ssl->handshake->fin_sha1);
Manuel Pégourié-Gonnard67427c02014-07-11 13:45:34 +02003060#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003061#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
3062#if defined(MBEDTLS_SHA256_C)
Andrzej Kurekeb342242019-01-29 09:14:33 -05003063#if defined(MBEDTLS_USE_PSA_CRYPTO)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003064 psa_hash_abort(&ssl->handshake->fin_sha256_psa);
3065 psa_hash_setup(&ssl->handshake->fin_sha256_psa, PSA_ALG_SHA_256);
Andrzej Kurekeb342242019-01-29 09:14:33 -05003066#else
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003067 mbedtls_sha256_starts_ret(&ssl->handshake->fin_sha256, 0);
Manuel Pégourié-Gonnard67427c02014-07-11 13:45:34 +02003068#endif
Andrzej Kurekeb342242019-01-29 09:14:33 -05003069#endif
Gilles Peskined2d59372021-05-12 22:43:27 +02003070#if defined(MBEDTLS_SHA512_C) && !defined(MBEDTLS_SHA512_NO_SHA384)
Andrzej Kurekeb342242019-01-29 09:14:33 -05003071#if defined(MBEDTLS_USE_PSA_CRYPTO)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003072 psa_hash_abort(&ssl->handshake->fin_sha384_psa);
3073 psa_hash_setup(&ssl->handshake->fin_sha384_psa, PSA_ALG_SHA_384);
Andrzej Kurekeb342242019-01-29 09:14:33 -05003074#else
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003075 mbedtls_sha512_starts_ret(&ssl->handshake->fin_sha512, 1);
Manuel Pégourié-Gonnard67427c02014-07-11 13:45:34 +02003076#endif
Andrzej Kurekeb342242019-01-29 09:14:33 -05003077#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003078#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Manuel Pégourié-Gonnard67427c02014-07-11 13:45:34 +02003079}
3080
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003081static void ssl_update_checksum_start(mbedtls_ssl_context *ssl,
3082 const unsigned char *buf, size_t len)
Paul Bakker380da532012-04-18 16:10:25 +00003083{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003084#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1) || \
3085 defined(MBEDTLS_SSL_PROTO_TLS1_1)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003086 mbedtls_md5_update_ret(&ssl->handshake->fin_md5, buf, len);
3087 mbedtls_sha1_update_ret(&ssl->handshake->fin_sha1, buf, len);
Paul Bakkerd2f068e2013-08-27 21:19:20 +02003088#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003089#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
3090#if defined(MBEDTLS_SHA256_C)
Andrzej Kurekeb342242019-01-29 09:14:33 -05003091#if defined(MBEDTLS_USE_PSA_CRYPTO)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003092 psa_hash_update(&ssl->handshake->fin_sha256_psa, buf, len);
Andrzej Kurekeb342242019-01-29 09:14:33 -05003093#else
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003094 mbedtls_sha256_update_ret(&ssl->handshake->fin_sha256, buf, len);
Paul Bakkerd2f068e2013-08-27 21:19:20 +02003095#endif
Andrzej Kurekeb342242019-01-29 09:14:33 -05003096#endif
Gilles Peskined2d59372021-05-12 22:43:27 +02003097#if defined(MBEDTLS_SHA512_C) && !defined(MBEDTLS_SHA512_NO_SHA384)
Andrzej Kurekeb342242019-01-29 09:14:33 -05003098#if defined(MBEDTLS_USE_PSA_CRYPTO)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003099 psa_hash_update(&ssl->handshake->fin_sha384_psa, buf, len);
Andrzej Kurekeb342242019-01-29 09:14:33 -05003100#else
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003101 mbedtls_sha512_update_ret(&ssl->handshake->fin_sha512, buf, len);
Paul Bakker769075d2012-11-24 11:26:46 +01003102#endif
Andrzej Kurekeb342242019-01-29 09:14:33 -05003103#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003104#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakker380da532012-04-18 16:10:25 +00003105}
3106
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003107#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1) || \
3108 defined(MBEDTLS_SSL_PROTO_TLS1_1)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003109static void ssl_update_checksum_md5sha1(mbedtls_ssl_context *ssl,
3110 const unsigned char *buf, size_t len)
Paul Bakker380da532012-04-18 16:10:25 +00003111{
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003112 mbedtls_md5_update_ret(&ssl->handshake->fin_md5, buf, len);
3113 mbedtls_sha1_update_ret(&ssl->handshake->fin_sha1, buf, len);
Paul Bakker380da532012-04-18 16:10:25 +00003114}
Paul Bakkerd2f068e2013-08-27 21:19:20 +02003115#endif
Paul Bakker380da532012-04-18 16:10:25 +00003116
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003117#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
3118#if defined(MBEDTLS_SHA256_C)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003119static void ssl_update_checksum_sha256(mbedtls_ssl_context *ssl,
3120 const unsigned char *buf, size_t len)
Paul Bakker380da532012-04-18 16:10:25 +00003121{
Andrzej Kurekeb342242019-01-29 09:14:33 -05003122#if defined(MBEDTLS_USE_PSA_CRYPTO)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003123 psa_hash_update(&ssl->handshake->fin_sha256_psa, buf, len);
Andrzej Kurekeb342242019-01-29 09:14:33 -05003124#else
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003125 mbedtls_sha256_update_ret(&ssl->handshake->fin_sha256, buf, len);
Andrzej Kurekeb342242019-01-29 09:14:33 -05003126#endif
Paul Bakker380da532012-04-18 16:10:25 +00003127}
Paul Bakkerd2f068e2013-08-27 21:19:20 +02003128#endif
Paul Bakker380da532012-04-18 16:10:25 +00003129
Gilles Peskined2d59372021-05-12 22:43:27 +02003130#if defined(MBEDTLS_SHA512_C) && !defined(MBEDTLS_SHA512_NO_SHA384)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003131static void ssl_update_checksum_sha384(mbedtls_ssl_context *ssl,
3132 const unsigned char *buf, size_t len)
Paul Bakker380da532012-04-18 16:10:25 +00003133{
Andrzej Kurekeb342242019-01-29 09:14:33 -05003134#if defined(MBEDTLS_USE_PSA_CRYPTO)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003135 psa_hash_update(&ssl->handshake->fin_sha384_psa, buf, len);
Andrzej Kurekeb342242019-01-29 09:14:33 -05003136#else
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003137 mbedtls_sha512_update_ret(&ssl->handshake->fin_sha512, buf, len);
Andrzej Kurekeb342242019-01-29 09:14:33 -05003138#endif
Paul Bakker380da532012-04-18 16:10:25 +00003139}
Paul Bakker769075d2012-11-24 11:26:46 +01003140#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003141#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakker380da532012-04-18 16:10:25 +00003142
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003143#if defined(MBEDTLS_SSL_PROTO_SSL3)
Paul Bakker1ef83d62012-04-11 12:09:53 +00003144static void ssl_calc_finished_ssl(
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003145 mbedtls_ssl_context *ssl, unsigned char *buf, int from)
Paul Bakker5121ce52009-01-03 21:22:43 +00003146{
Paul Bakker3c2122f2013-06-24 19:03:14 +02003147 const char *sender;
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02003148 mbedtls_md5_context md5;
3149 mbedtls_sha1_context sha1;
Paul Bakker1ef83d62012-04-11 12:09:53 +00003150
Paul Bakker5121ce52009-01-03 21:22:43 +00003151 unsigned char padbuf[48];
3152 unsigned char md5sum[16];
3153 unsigned char sha1sum[20];
3154
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003155 mbedtls_ssl_session *session = ssl->session_negotiate;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003156 if (!session) {
Paul Bakker48916f92012-09-16 19:57:18 +00003157 session = ssl->session;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003158 }
Paul Bakker48916f92012-09-16 19:57:18 +00003159
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003160 MBEDTLS_SSL_DEBUG_MSG(2, ("=> calc finished ssl"));
Paul Bakker1ef83d62012-04-11 12:09:53 +00003161
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003162 mbedtls_md5_init(&md5);
3163 mbedtls_sha1_init(&sha1);
Manuel Pégourié-Gonnard001f2b62015-07-06 16:21:13 +02003164
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003165 mbedtls_md5_clone(&md5, &ssl->handshake->fin_md5);
3166 mbedtls_sha1_clone(&sha1, &ssl->handshake->fin_sha1);
Paul Bakker5121ce52009-01-03 21:22:43 +00003167
3168 /*
3169 * SSLv3:
3170 * hash =
3171 * MD5( master + pad2 +
3172 * MD5( handshake + sender + master + pad1 ) )
3173 * + SHA1( master + pad2 +
3174 * SHA1( handshake + sender + master + pad1 ) )
Paul Bakker5121ce52009-01-03 21:22:43 +00003175 */
3176
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003177#if !defined(MBEDTLS_MD5_ALT)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003178 MBEDTLS_SSL_DEBUG_BUF(4, "finished md5 state", (unsigned char *)
3179 md5.state, sizeof(md5.state));
Paul Bakker90995b52013-06-24 19:20:35 +02003180#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00003181
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003182#if !defined(MBEDTLS_SHA1_ALT)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003183 MBEDTLS_SSL_DEBUG_BUF(4, "finished sha1 state", (unsigned char *)
3184 sha1.state, sizeof(sha1.state));
Paul Bakker90995b52013-06-24 19:20:35 +02003185#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00003186
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003187 sender = (from == MBEDTLS_SSL_IS_CLIENT) ? "CLNT"
Paul Bakker3c2122f2013-06-24 19:03:14 +02003188 : "SRVR";
Paul Bakker5121ce52009-01-03 21:22:43 +00003189
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003190 memset(padbuf, 0x36, 48);
Paul Bakker5121ce52009-01-03 21:22:43 +00003191
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003192 mbedtls_md5_update_ret(&md5, (const unsigned char *) sender, 4);
3193 mbedtls_md5_update_ret(&md5, session->master, 48);
3194 mbedtls_md5_update_ret(&md5, padbuf, 48);
3195 mbedtls_md5_finish_ret(&md5, md5sum);
Paul Bakker5121ce52009-01-03 21:22:43 +00003196
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003197 mbedtls_sha1_update_ret(&sha1, (const unsigned char *) sender, 4);
3198 mbedtls_sha1_update_ret(&sha1, session->master, 48);
3199 mbedtls_sha1_update_ret(&sha1, padbuf, 40);
3200 mbedtls_sha1_finish_ret(&sha1, sha1sum);
Paul Bakker5121ce52009-01-03 21:22:43 +00003201
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003202 memset(padbuf, 0x5C, 48);
Paul Bakker5121ce52009-01-03 21:22:43 +00003203
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003204 mbedtls_md5_starts_ret(&md5);
3205 mbedtls_md5_update_ret(&md5, session->master, 48);
3206 mbedtls_md5_update_ret(&md5, padbuf, 48);
3207 mbedtls_md5_update_ret(&md5, md5sum, 16);
3208 mbedtls_md5_finish_ret(&md5, buf);
Paul Bakker5121ce52009-01-03 21:22:43 +00003209
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003210 mbedtls_sha1_starts_ret(&sha1);
3211 mbedtls_sha1_update_ret(&sha1, session->master, 48);
3212 mbedtls_sha1_update_ret(&sha1, padbuf, 40);
3213 mbedtls_sha1_update_ret(&sha1, sha1sum, 20);
3214 mbedtls_sha1_finish_ret(&sha1, buf + 16);
Paul Bakker5121ce52009-01-03 21:22:43 +00003215
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003216 MBEDTLS_SSL_DEBUG_BUF(3, "calc finished result", buf, 36);
Paul Bakker5121ce52009-01-03 21:22:43 +00003217
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003218 mbedtls_md5_free(&md5);
3219 mbedtls_sha1_free(&sha1);
Paul Bakker5121ce52009-01-03 21:22:43 +00003220
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003221 mbedtls_platform_zeroize(padbuf, sizeof(padbuf));
3222 mbedtls_platform_zeroize(md5sum, sizeof(md5sum));
3223 mbedtls_platform_zeroize(sha1sum, sizeof(sha1sum));
Paul Bakker5121ce52009-01-03 21:22:43 +00003224
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003225 MBEDTLS_SSL_DEBUG_MSG(2, ("<= calc finished"));
Paul Bakker5121ce52009-01-03 21:22:43 +00003226}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003227#endif /* MBEDTLS_SSL_PROTO_SSL3 */
Paul Bakker5121ce52009-01-03 21:22:43 +00003228
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003229#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1)
Paul Bakker1ef83d62012-04-11 12:09:53 +00003230static void ssl_calc_finished_tls(
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003231 mbedtls_ssl_context *ssl, unsigned char *buf, int from)
Paul Bakker5121ce52009-01-03 21:22:43 +00003232{
Paul Bakker1ef83d62012-04-11 12:09:53 +00003233 int len = 12;
Paul Bakker3c2122f2013-06-24 19:03:14 +02003234 const char *sender;
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02003235 mbedtls_md5_context md5;
3236 mbedtls_sha1_context sha1;
Paul Bakker1ef83d62012-04-11 12:09:53 +00003237 unsigned char padbuf[36];
Paul Bakker5121ce52009-01-03 21:22:43 +00003238
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003239 mbedtls_ssl_session *session = ssl->session_negotiate;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003240 if (!session) {
Paul Bakker48916f92012-09-16 19:57:18 +00003241 session = ssl->session;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003242 }
Paul Bakker48916f92012-09-16 19:57:18 +00003243
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003244 MBEDTLS_SSL_DEBUG_MSG(2, ("=> calc finished tls"));
Paul Bakker5121ce52009-01-03 21:22:43 +00003245
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003246 mbedtls_md5_init(&md5);
3247 mbedtls_sha1_init(&sha1);
Manuel Pégourié-Gonnard001f2b62015-07-06 16:21:13 +02003248
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003249 mbedtls_md5_clone(&md5, &ssl->handshake->fin_md5);
3250 mbedtls_sha1_clone(&sha1, &ssl->handshake->fin_sha1);
Paul Bakker5121ce52009-01-03 21:22:43 +00003251
Paul Bakker1ef83d62012-04-11 12:09:53 +00003252 /*
3253 * TLSv1:
3254 * hash = PRF( master, finished_label,
3255 * MD5( handshake ) + SHA1( handshake ) )[0..11]
3256 */
Paul Bakker5121ce52009-01-03 21:22:43 +00003257
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003258#if !defined(MBEDTLS_MD5_ALT)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003259 MBEDTLS_SSL_DEBUG_BUF(4, "finished md5 state", (unsigned char *)
3260 md5.state, sizeof(md5.state));
Paul Bakker90995b52013-06-24 19:20:35 +02003261#endif
Paul Bakker1ef83d62012-04-11 12:09:53 +00003262
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003263#if !defined(MBEDTLS_SHA1_ALT)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003264 MBEDTLS_SSL_DEBUG_BUF(4, "finished sha1 state", (unsigned char *)
3265 sha1.state, sizeof(sha1.state));
Paul Bakker90995b52013-06-24 19:20:35 +02003266#endif
Paul Bakker1ef83d62012-04-11 12:09:53 +00003267
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003268 sender = (from == MBEDTLS_SSL_IS_CLIENT)
Paul Bakker3c2122f2013-06-24 19:03:14 +02003269 ? "client finished"
3270 : "server finished";
Paul Bakker1ef83d62012-04-11 12:09:53 +00003271
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003272 mbedtls_md5_finish_ret(&md5, padbuf);
3273 mbedtls_sha1_finish_ret(&sha1, padbuf + 16);
Paul Bakker1ef83d62012-04-11 12:09:53 +00003274
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003275 ssl->handshake->tls_prf(session->master, 48, sender,
3276 padbuf, 36, buf, len);
Paul Bakker1ef83d62012-04-11 12:09:53 +00003277
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003278 MBEDTLS_SSL_DEBUG_BUF(3, "calc finished result", buf, len);
Paul Bakker1ef83d62012-04-11 12:09:53 +00003279
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003280 mbedtls_md5_free(&md5);
3281 mbedtls_sha1_free(&sha1);
Paul Bakker1ef83d62012-04-11 12:09:53 +00003282
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003283 mbedtls_platform_zeroize(padbuf, sizeof(padbuf));
Paul Bakker1ef83d62012-04-11 12:09:53 +00003284
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003285 MBEDTLS_SSL_DEBUG_MSG(2, ("<= calc finished"));
Paul Bakker1ef83d62012-04-11 12:09:53 +00003286}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003287#endif /* MBEDTLS_SSL_PROTO_TLS1 || MBEDTLS_SSL_PROTO_TLS1_1 */
Paul Bakker1ef83d62012-04-11 12:09:53 +00003288
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003289#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
3290#if defined(MBEDTLS_SHA256_C)
Paul Bakkerca4ab492012-04-18 14:23:57 +00003291static void ssl_calc_finished_tls_sha256(
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003292 mbedtls_ssl_context *ssl, unsigned char *buf, int from)
Paul Bakker1ef83d62012-04-11 12:09:53 +00003293{
3294 int len = 12;
Paul Bakker3c2122f2013-06-24 19:03:14 +02003295 const char *sender;
Paul Bakker1ef83d62012-04-11 12:09:53 +00003296 unsigned char padbuf[32];
Andrzej Kurekeb342242019-01-29 09:14:33 -05003297#if defined(MBEDTLS_USE_PSA_CRYPTO)
3298 size_t hash_size;
Jaeden Amero34973232019-02-20 10:32:28 +00003299 psa_hash_operation_t sha256_psa = PSA_HASH_OPERATION_INIT;
Andrzej Kurekeb342242019-01-29 09:14:33 -05003300 psa_status_t status;
3301#else
3302 mbedtls_sha256_context sha256;
3303#endif
Paul Bakker1ef83d62012-04-11 12:09:53 +00003304
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003305 mbedtls_ssl_session *session = ssl->session_negotiate;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003306 if (!session) {
Paul Bakker48916f92012-09-16 19:57:18 +00003307 session = ssl->session;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003308 }
Paul Bakker48916f92012-09-16 19:57:18 +00003309
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003310 sender = (from == MBEDTLS_SSL_IS_CLIENT)
Andrzej Kurekeb342242019-01-29 09:14:33 -05003311 ? "client finished"
3312 : "server finished";
3313
3314#if defined(MBEDTLS_USE_PSA_CRYPTO)
3315 sha256_psa = psa_hash_operation_init();
3316
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003317 MBEDTLS_SSL_DEBUG_MSG(2, ("=> calc PSA finished tls sha256"));
Andrzej Kurekeb342242019-01-29 09:14:33 -05003318
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003319 status = psa_hash_clone(&ssl->handshake->fin_sha256_psa, &sha256_psa);
3320 if (status != PSA_SUCCESS) {
3321 MBEDTLS_SSL_DEBUG_MSG(2, ("PSA hash clone failed"));
Andrzej Kurekeb342242019-01-29 09:14:33 -05003322 return;
3323 }
3324
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003325 status = psa_hash_finish(&sha256_psa, padbuf, sizeof(padbuf), &hash_size);
3326 if (status != PSA_SUCCESS) {
3327 MBEDTLS_SSL_DEBUG_MSG(2, ("PSA hash finish failed"));
Andrzej Kurekeb342242019-01-29 09:14:33 -05003328 return;
3329 }
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003330 MBEDTLS_SSL_DEBUG_BUF(3, "PSA calculated padbuf", padbuf, 32);
Andrzej Kurekeb342242019-01-29 09:14:33 -05003331#else
3332
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003333 mbedtls_sha256_init(&sha256);
Manuel Pégourié-Gonnard001f2b62015-07-06 16:21:13 +02003334
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003335 MBEDTLS_SSL_DEBUG_MSG(2, ("=> calc finished tls sha256"));
Paul Bakker1ef83d62012-04-11 12:09:53 +00003336
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003337 mbedtls_sha256_clone(&sha256, &ssl->handshake->fin_sha256);
Paul Bakker1ef83d62012-04-11 12:09:53 +00003338
3339 /*
3340 * TLSv1.2:
3341 * hash = PRF( master, finished_label,
3342 * Hash( handshake ) )[0.11]
3343 */
3344
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003345#if !defined(MBEDTLS_SHA256_ALT)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003346 MBEDTLS_SSL_DEBUG_BUF(4, "finished sha2 state", (unsigned char *)
3347 sha256.state, sizeof(sha256.state));
Paul Bakker90995b52013-06-24 19:20:35 +02003348#endif
Paul Bakker1ef83d62012-04-11 12:09:53 +00003349
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003350 mbedtls_sha256_finish_ret(&sha256, padbuf);
3351 mbedtls_sha256_free(&sha256);
Andrzej Kurekeb342242019-01-29 09:14:33 -05003352#endif /* MBEDTLS_USE_PSA_CRYPTO */
Paul Bakker1ef83d62012-04-11 12:09:53 +00003353
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003354 ssl->handshake->tls_prf(session->master, 48, sender,
3355 padbuf, 32, buf, len);
Paul Bakker1ef83d62012-04-11 12:09:53 +00003356
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003357 MBEDTLS_SSL_DEBUG_BUF(3, "calc finished result", buf, len);
Paul Bakker1ef83d62012-04-11 12:09:53 +00003358
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003359 mbedtls_platform_zeroize(padbuf, sizeof(padbuf));
Paul Bakker1ef83d62012-04-11 12:09:53 +00003360
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003361 MBEDTLS_SSL_DEBUG_MSG(2, ("<= calc finished"));
Paul Bakker1ef83d62012-04-11 12:09:53 +00003362}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003363#endif /* MBEDTLS_SHA256_C */
Paul Bakker1ef83d62012-04-11 12:09:53 +00003364
Gilles Peskined2d59372021-05-12 22:43:27 +02003365#if defined(MBEDTLS_SHA512_C) && !defined(MBEDTLS_SHA512_NO_SHA384)
Rodrigo Dias Corread596ca82020-11-25 00:42:28 -03003366
Paul Bakkerca4ab492012-04-18 14:23:57 +00003367static void ssl_calc_finished_tls_sha384(
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003368 mbedtls_ssl_context *ssl, unsigned char *buf, int from)
Paul Bakkerca4ab492012-04-18 14:23:57 +00003369{
3370 int len = 12;
Paul Bakker3c2122f2013-06-24 19:03:14 +02003371 const char *sender;
Rodrigo Dias Corread596ca82020-11-25 00:42:28 -03003372 unsigned char padbuf[48];
Andrzej Kurekeb342242019-01-29 09:14:33 -05003373#if defined(MBEDTLS_USE_PSA_CRYPTO)
3374 size_t hash_size;
Jaeden Amero34973232019-02-20 10:32:28 +00003375 psa_hash_operation_t sha384_psa = PSA_HASH_OPERATION_INIT;
Andrzej Kurekeb342242019-01-29 09:14:33 -05003376 psa_status_t status;
3377#else
3378 mbedtls_sha512_context sha512;
3379#endif
Paul Bakkerca4ab492012-04-18 14:23:57 +00003380
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003381 mbedtls_ssl_session *session = ssl->session_negotiate;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003382 if (!session) {
Paul Bakker48916f92012-09-16 19:57:18 +00003383 session = ssl->session;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003384 }
Paul Bakker48916f92012-09-16 19:57:18 +00003385
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003386 sender = (from == MBEDTLS_SSL_IS_CLIENT)
Andrzej Kurekeb342242019-01-29 09:14:33 -05003387 ? "client finished"
3388 : "server finished";
3389
3390#if defined(MBEDTLS_USE_PSA_CRYPTO)
Andrzej Kurek972fba52019-01-30 03:29:12 -05003391 sha384_psa = psa_hash_operation_init();
Andrzej Kurekeb342242019-01-29 09:14:33 -05003392
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003393 MBEDTLS_SSL_DEBUG_MSG(2, ("=> calc PSA finished tls sha384"));
Andrzej Kurekeb342242019-01-29 09:14:33 -05003394
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003395 status = psa_hash_clone(&ssl->handshake->fin_sha384_psa, &sha384_psa);
3396 if (status != PSA_SUCCESS) {
3397 MBEDTLS_SSL_DEBUG_MSG(2, ("PSA hash clone failed"));
Andrzej Kurekeb342242019-01-29 09:14:33 -05003398 return;
3399 }
3400
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003401 status = psa_hash_finish(&sha384_psa, padbuf, sizeof(padbuf), &hash_size);
3402 if (status != PSA_SUCCESS) {
3403 MBEDTLS_SSL_DEBUG_MSG(2, ("PSA hash finish failed"));
Andrzej Kurekeb342242019-01-29 09:14:33 -05003404 return;
3405 }
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003406 MBEDTLS_SSL_DEBUG_BUF(3, "PSA calculated padbuf", padbuf, 48);
Andrzej Kurekeb342242019-01-29 09:14:33 -05003407#else
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003408 mbedtls_sha512_init(&sha512);
Manuel Pégourié-Gonnard001f2b62015-07-06 16:21:13 +02003409
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003410 MBEDTLS_SSL_DEBUG_MSG(2, ("=> calc finished tls sha384"));
Paul Bakkerca4ab492012-04-18 14:23:57 +00003411
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003412 mbedtls_sha512_clone(&sha512, &ssl->handshake->fin_sha512);
Paul Bakkerca4ab492012-04-18 14:23:57 +00003413
3414 /*
3415 * TLSv1.2:
3416 * hash = PRF( master, finished_label,
3417 * Hash( handshake ) )[0.11]
3418 */
3419
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003420#if !defined(MBEDTLS_SHA512_ALT)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003421 MBEDTLS_SSL_DEBUG_BUF(4, "finished sha512 state", (unsigned char *)
3422 sha512.state, sizeof(sha512.state));
Paul Bakker90995b52013-06-24 19:20:35 +02003423#endif
Gilles Peskinebb66dac2021-05-13 00:00:45 +02003424 /* mbedtls_sha512_finish_ret's output parameter is declared as a
Tom Cosgrove49f99bc2022-12-04 16:44:21 +00003425 * 64-byte buffer, but since we're using SHA-384, we know that the
Gilles Peskinebb66dac2021-05-13 00:00:45 +02003426 * output fits in 48 bytes. This is correct C, but GCC 11.1 warns
3427 * about it.
Rodrigo Dias Corread596ca82020-11-25 00:42:28 -03003428 */
Gilles Peskinebb66dac2021-05-13 00:00:45 +02003429#if defined(__GNUC__) && __GNUC__ >= 11
3430#pragma GCC diagnostic push
3431#pragma GCC diagnostic ignored "-Wstringop-overflow"
3432#endif
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003433 mbedtls_sha512_finish_ret(&sha512, padbuf);
Gilles Peskinebb66dac2021-05-13 00:00:45 +02003434#if defined(__GNUC__) && __GNUC__ >= 11
3435#pragma GCC diagnostic pop
3436#endif
Paul Bakkerca4ab492012-04-18 14:23:57 +00003437
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003438 mbedtls_sha512_free(&sha512);
Andrzej Kurekeb342242019-01-29 09:14:33 -05003439#endif
Paul Bakkerca4ab492012-04-18 14:23:57 +00003440
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003441 ssl->handshake->tls_prf(session->master, 48, sender,
3442 padbuf, 48, buf, len);
Paul Bakkerca4ab492012-04-18 14:23:57 +00003443
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003444 MBEDTLS_SSL_DEBUG_BUF(3, "calc finished result", buf, len);
Paul Bakkerca4ab492012-04-18 14:23:57 +00003445
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003446 mbedtls_platform_zeroize(padbuf, sizeof(padbuf));
Paul Bakkerca4ab492012-04-18 14:23:57 +00003447
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003448 MBEDTLS_SSL_DEBUG_MSG(2, ("<= calc finished"));
Paul Bakkerca4ab492012-04-18 14:23:57 +00003449}
Gilles Peskined2d59372021-05-12 22:43:27 +02003450#endif /* MBEDTLS_SHA512_C && !MBEDTLS_SHA512_NO_SHA384 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003451#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakkerca4ab492012-04-18 14:23:57 +00003452
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003453void mbedtls_ssl_handshake_wrapup_free_hs_transform(mbedtls_ssl_context *ssl)
Paul Bakker48916f92012-09-16 19:57:18 +00003454{
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003455 MBEDTLS_SSL_DEBUG_MSG(3, ("=> handshake wrapup: final free"));
Paul Bakker48916f92012-09-16 19:57:18 +00003456
3457 /*
3458 * Free our handshake params
3459 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003460 mbedtls_ssl_handshake_free(ssl);
3461 mbedtls_free(ssl->handshake);
Paul Bakker48916f92012-09-16 19:57:18 +00003462 ssl->handshake = NULL;
3463
3464 /*
Shaun Case0e7791f2021-12-20 21:14:10 -08003465 * Free the previous transform and switch in the current one
Paul Bakker48916f92012-09-16 19:57:18 +00003466 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003467 if (ssl->transform) {
3468 mbedtls_ssl_transform_free(ssl->transform);
3469 mbedtls_free(ssl->transform);
Paul Bakker48916f92012-09-16 19:57:18 +00003470 }
3471 ssl->transform = ssl->transform_negotiate;
3472 ssl->transform_negotiate = NULL;
3473
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003474 MBEDTLS_SSL_DEBUG_MSG(3, ("<= handshake wrapup: final free"));
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02003475}
3476
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003477void mbedtls_ssl_handshake_wrapup(mbedtls_ssl_context *ssl)
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02003478{
3479 int resume = ssl->handshake->resume;
3480
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003481 MBEDTLS_SSL_DEBUG_MSG(3, ("=> handshake wrapup"));
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02003482
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003483#if defined(MBEDTLS_SSL_RENEGOTIATION)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003484 if (ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS) {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003485 ssl->renego_status = MBEDTLS_SSL_RENEGOTIATION_DONE;
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02003486 ssl->renego_records_seen = 0;
3487 }
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01003488#endif
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02003489
3490 /*
3491 * Free the previous session and switch in the current one
3492 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003493 if (ssl->session) {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003494#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
Manuel Pégourié-Gonnard1a034732014-11-04 17:36:18 +01003495 /* RFC 7366 3.1: keep the EtM state */
3496 ssl->session_negotiate->encrypt_then_mac =
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003497 ssl->session->encrypt_then_mac;
Manuel Pégourié-Gonnard1a034732014-11-04 17:36:18 +01003498#endif
3499
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003500 mbedtls_ssl_session_free(ssl->session);
3501 mbedtls_free(ssl->session);
Paul Bakker0a597072012-09-25 21:55:46 +00003502 }
3503 ssl->session = ssl->session_negotiate;
Paul Bakker48916f92012-09-16 19:57:18 +00003504 ssl->session_negotiate = NULL;
3505
Paul Bakker0a597072012-09-25 21:55:46 +00003506 /*
3507 * Add cache entry
3508 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003509 if (ssl->conf->f_set_cache != NULL &&
Manuel Pégourié-Gonnard12ad7982015-06-18 15:50:37 +02003510 ssl->session->id_len != 0 &&
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003511 resume == 0) {
3512 if (ssl->conf->f_set_cache(ssl->conf->p_cache, ssl->session) != 0) {
3513 MBEDTLS_SSL_DEBUG_MSG(1, ("cache did not store session"));
3514 }
Manuel Pégourié-Gonnardc086cce2013-08-02 14:13:02 +02003515 }
Paul Bakker0a597072012-09-25 21:55:46 +00003516
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003517#if defined(MBEDTLS_SSL_PROTO_DTLS)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003518 if (ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
3519 ssl->handshake->flight != NULL) {
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02003520 /* Cancel handshake timer */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003521 mbedtls_ssl_set_timer(ssl, 0);
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02003522
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02003523 /* Keep last flight around in case we need to resend it:
3524 * we need the handshake and transform structures for that */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003525 MBEDTLS_SSL_DEBUG_MSG(3, ("skip freeing handshake and transform"));
3526 } else
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02003527#endif
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003528 mbedtls_ssl_handshake_wrapup_free_hs_transform(ssl);
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02003529
Paul Bakker48916f92012-09-16 19:57:18 +00003530 ssl->state++;
3531
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003532 MBEDTLS_SSL_DEBUG_MSG(3, ("<= handshake wrapup"));
Paul Bakker48916f92012-09-16 19:57:18 +00003533}
3534
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003535int mbedtls_ssl_write_finished(mbedtls_ssl_context *ssl)
Paul Bakker1ef83d62012-04-11 12:09:53 +00003536{
Manuel Pégourié-Gonnard879a4f92014-07-11 22:31:12 +02003537 int ret, hash_len;
Paul Bakker1ef83d62012-04-11 12:09:53 +00003538
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003539 MBEDTLS_SSL_DEBUG_MSG(2, ("=> write finished"));
Paul Bakker1ef83d62012-04-11 12:09:53 +00003540
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003541 mbedtls_ssl_update_out_pointers(ssl, ssl->transform_negotiate);
Paul Bakker92be97b2013-01-02 17:30:03 +01003542
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003543 ssl->handshake->calc_finished(ssl, ssl->out_msg + 4, ssl->conf->endpoint);
Paul Bakker1ef83d62012-04-11 12:09:53 +00003544
Manuel Pégourié-Gonnard214a8482016-02-22 11:27:26 +01003545 /*
3546 * RFC 5246 7.4.9 (Page 63) says 12 is the default length and ciphersuites
3547 * may define some other value. Currently (early 2016), no defined
3548 * ciphersuite does this (and this is unlikely to change as activity has
3549 * moved to TLS 1.3 now) so we can keep the hardcoded 12 here.
3550 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003551 hash_len = (ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0) ? 36 : 12;
Paul Bakker5121ce52009-01-03 21:22:43 +00003552
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003553#if defined(MBEDTLS_SSL_RENEGOTIATION)
Paul Bakker48916f92012-09-16 19:57:18 +00003554 ssl->verify_data_len = hash_len;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003555 memcpy(ssl->own_verify_data, ssl->out_msg + 4, hash_len);
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01003556#endif
Paul Bakker48916f92012-09-16 19:57:18 +00003557
Paul Bakker5121ce52009-01-03 21:22:43 +00003558 ssl->out_msglen = 4 + hash_len;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003559 ssl->out_msgtype = MBEDTLS_SSL_MSG_HANDSHAKE;
3560 ssl->out_msg[0] = MBEDTLS_SSL_HS_FINISHED;
Paul Bakker5121ce52009-01-03 21:22:43 +00003561
3562 /*
3563 * In case of session resuming, invert the client and server
3564 * ChangeCipherSpec messages order.
3565 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003566 if (ssl->handshake->resume != 0) {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003567#if defined(MBEDTLS_SSL_CLI_C)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003568 if (ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT) {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003569 ssl->state = MBEDTLS_SSL_HANDSHAKE_WRAPUP;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003570 }
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01003571#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003572#if defined(MBEDTLS_SSL_SRV_C)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003573 if (ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER) {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003574 ssl->state = MBEDTLS_SSL_CLIENT_CHANGE_CIPHER_SPEC;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003575 }
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01003576#endif
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003577 } else {
Paul Bakker5121ce52009-01-03 21:22:43 +00003578 ssl->state++;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003579 }
Paul Bakker5121ce52009-01-03 21:22:43 +00003580
Paul Bakker48916f92012-09-16 19:57:18 +00003581 /*
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02003582 * Switch to our negotiated transform and session parameters for outbound
3583 * data.
Paul Bakker48916f92012-09-16 19:57:18 +00003584 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003585 MBEDTLS_SSL_DEBUG_MSG(3, ("switching to new transform spec for outbound data"));
Manuel Pégourié-Gonnard5afb1672014-02-16 18:33:22 +01003586
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003587#if defined(MBEDTLS_SSL_PROTO_DTLS)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003588 if (ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM) {
Manuel Pégourié-Gonnard879a4f92014-07-11 22:31:12 +02003589 unsigned char i;
3590
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003591 /* Remember current epoch settings for resending */
3592 ssl->handshake->alt_transform_out = ssl->transform_out;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003593 memcpy(ssl->handshake->alt_out_ctr, ssl->cur_out_ctr, 8);
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003594
Manuel Pégourié-Gonnard879a4f92014-07-11 22:31:12 +02003595 /* Set sequence_number to zero */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003596 memset(ssl->cur_out_ctr + 2, 0, 6);
Manuel Pégourié-Gonnard879a4f92014-07-11 22:31:12 +02003597
3598 /* Increment epoch */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003599 for (i = 2; i > 0; i--) {
3600 if (++ssl->cur_out_ctr[i - 1] != 0) {
Manuel Pégourié-Gonnard879a4f92014-07-11 22:31:12 +02003601 break;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003602 }
3603 }
Manuel Pégourié-Gonnard879a4f92014-07-11 22:31:12 +02003604
3605 /* The loop goes to its end iff the counter is wrapping */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003606 if (i == 0) {
3607 MBEDTLS_SSL_DEBUG_MSG(1, ("DTLS epoch would wrap"));
3608 return MBEDTLS_ERR_SSL_COUNTER_WRAPPING;
Manuel Pégourié-Gonnard879a4f92014-07-11 22:31:12 +02003609 }
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003610 } else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003611#endif /* MBEDTLS_SSL_PROTO_DTLS */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003612 memset(ssl->cur_out_ctr, 0, 8);
Paul Bakker5121ce52009-01-03 21:22:43 +00003613
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003614 ssl->transform_out = ssl->transform_negotiate;
3615 ssl->session_out = ssl->session_negotiate;
3616
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003617#if defined(MBEDTLS_SSL_HW_RECORD_ACCEL)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003618 if (mbedtls_ssl_hw_record_activate != NULL) {
3619 if ((ret = mbedtls_ssl_hw_record_activate(ssl, MBEDTLS_SSL_CHANNEL_OUTBOUND)) != 0) {
3620 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ssl_hw_record_activate", ret);
3621 return MBEDTLS_ERR_SSL_HW_ACCEL_FAILED;
Paul Bakker07eb38b2012-12-19 14:42:06 +01003622 }
3623 }
3624#endif
3625
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003626#if defined(MBEDTLS_SSL_PROTO_DTLS)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003627 if (ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM) {
3628 mbedtls_ssl_send_flight_completed(ssl);
3629 }
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02003630#endif
3631
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003632 if ((ret = mbedtls_ssl_write_handshake_msg(ssl)) != 0) {
3633 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ssl_write_handshake_msg", ret);
3634 return ret;
Paul Bakker5121ce52009-01-03 21:22:43 +00003635 }
3636
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02003637#if defined(MBEDTLS_SSL_PROTO_DTLS)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003638 if (ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
3639 (ret = mbedtls_ssl_flight_transmit(ssl)) != 0) {
3640 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ssl_flight_transmit", ret);
3641 return ret;
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02003642 }
3643#endif
3644
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003645 MBEDTLS_SSL_DEBUG_MSG(2, ("<= write finished"));
Paul Bakker5121ce52009-01-03 21:22:43 +00003646
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003647 return 0;
Paul Bakker5121ce52009-01-03 21:22:43 +00003648}
3649
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003650#if defined(MBEDTLS_SSL_PROTO_SSL3)
Manuel Pégourié-Gonnardca6440b2014-09-10 12:39:54 +00003651#define SSL_MAX_HASH_LEN 36
3652#else
3653#define SSL_MAX_HASH_LEN 12
3654#endif
3655
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003656int mbedtls_ssl_parse_finished(mbedtls_ssl_context *ssl)
Paul Bakker5121ce52009-01-03 21:22:43 +00003657{
Janos Follath865b3eb2019-12-16 11:46:15 +00003658 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnard879a4f92014-07-11 22:31:12 +02003659 unsigned int hash_len;
Manuel Pégourié-Gonnardca6440b2014-09-10 12:39:54 +00003660 unsigned char buf[SSL_MAX_HASH_LEN];
Paul Bakker5121ce52009-01-03 21:22:43 +00003661
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003662 MBEDTLS_SSL_DEBUG_MSG(2, ("=> parse finished"));
Paul Bakker5121ce52009-01-03 21:22:43 +00003663
Gilles Peskine622d8042021-12-13 14:38:40 +01003664 /* There is currently no ciphersuite using another length with TLS 1.2 */
3665#if defined(MBEDTLS_SSL_PROTO_SSL3)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003666 if (ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0) {
Gilles Peskine622d8042021-12-13 14:38:40 +01003667 hash_len = 36;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003668 } else
Gilles Peskine622d8042021-12-13 14:38:40 +01003669#endif
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003670 hash_len = 12;
Gilles Peskine622d8042021-12-13 14:38:40 +01003671
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003672 ssl->handshake->calc_finished(ssl, buf, ssl->conf->endpoint ^ 1);
Paul Bakker5121ce52009-01-03 21:22:43 +00003673
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003674 if ((ret = mbedtls_ssl_read_record(ssl, 1)) != 0) {
3675 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ssl_read_record", ret);
Gilles Peskinef91b2e52021-12-13 12:36:15 +01003676 goto exit;
Paul Bakker5121ce52009-01-03 21:22:43 +00003677 }
3678
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003679 if (ssl->in_msgtype != MBEDTLS_SSL_MSG_HANDSHAKE) {
3680 MBEDTLS_SSL_DEBUG_MSG(1, ("bad finished message"));
3681 mbedtls_ssl_send_alert_message(ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
3682 MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE);
Gilles Peskinef91b2e52021-12-13 12:36:15 +01003683 ret = MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE;
3684 goto exit;
Paul Bakker5121ce52009-01-03 21:22:43 +00003685 }
3686
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003687 if (ssl->in_msg[0] != MBEDTLS_SSL_HS_FINISHED ||
3688 ssl->in_hslen != mbedtls_ssl_hs_hdr_len(ssl) + hash_len) {
3689 MBEDTLS_SSL_DEBUG_MSG(1, ("bad finished message"));
3690 mbedtls_ssl_send_alert_message(ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
3691 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR);
Gilles Peskinef91b2e52021-12-13 12:36:15 +01003692 ret = MBEDTLS_ERR_SSL_BAD_HS_FINISHED;
3693 goto exit;
Paul Bakker5121ce52009-01-03 21:22:43 +00003694 }
3695
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003696 if (mbedtls_ct_memcmp(ssl->in_msg + mbedtls_ssl_hs_hdr_len(ssl),
3697 buf, hash_len) != 0) {
3698 MBEDTLS_SSL_DEBUG_MSG(1, ("bad finished message"));
3699 mbedtls_ssl_send_alert_message(ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
3700 MBEDTLS_SSL_ALERT_MSG_DECRYPT_ERROR);
Gilles Peskinef91b2e52021-12-13 12:36:15 +01003701 ret = MBEDTLS_ERR_SSL_BAD_HS_FINISHED;
3702 goto exit;
Paul Bakker5121ce52009-01-03 21:22:43 +00003703 }
3704
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003705#if defined(MBEDTLS_SSL_RENEGOTIATION)
Paul Bakker48916f92012-09-16 19:57:18 +00003706 ssl->verify_data_len = hash_len;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003707 memcpy(ssl->peer_verify_data, buf, hash_len);
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01003708#endif
Paul Bakker48916f92012-09-16 19:57:18 +00003709
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003710 if (ssl->handshake->resume != 0) {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003711#if defined(MBEDTLS_SSL_CLI_C)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003712 if (ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT) {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003713 ssl->state = MBEDTLS_SSL_CLIENT_CHANGE_CIPHER_SPEC;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003714 }
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01003715#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003716#if defined(MBEDTLS_SSL_SRV_C)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003717 if (ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER) {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003718 ssl->state = MBEDTLS_SSL_HANDSHAKE_WRAPUP;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003719 }
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01003720#endif
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003721 } else {
Paul Bakker5121ce52009-01-03 21:22:43 +00003722 ssl->state++;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003723 }
Paul Bakker5121ce52009-01-03 21:22:43 +00003724
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003725#if defined(MBEDTLS_SSL_PROTO_DTLS)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003726 if (ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM) {
3727 mbedtls_ssl_recv_flight_completed(ssl);
3728 }
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003729#endif
3730
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003731 MBEDTLS_SSL_DEBUG_MSG(2, ("<= parse finished"));
Paul Bakker5121ce52009-01-03 21:22:43 +00003732
Gilles Peskinef91b2e52021-12-13 12:36:15 +01003733exit:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003734 mbedtls_platform_zeroize(buf, hash_len);
3735 return ret;
Paul Bakker5121ce52009-01-03 21:22:43 +00003736}
3737
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003738static void ssl_handshake_params_init(mbedtls_ssl_handshake_params *handshake)
Paul Bakkeraccaffe2014-06-26 13:37:14 +02003739{
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003740 memset(handshake, 0, sizeof(mbedtls_ssl_handshake_params));
Paul Bakkeraccaffe2014-06-26 13:37:14 +02003741
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003742#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1) || \
3743 defined(MBEDTLS_SSL_PROTO_TLS1_1)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003744 mbedtls_md5_init(&handshake->fin_md5);
3745 mbedtls_sha1_init(&handshake->fin_sha1);
3746 mbedtls_md5_starts_ret(&handshake->fin_md5);
3747 mbedtls_sha1_starts_ret(&handshake->fin_sha1);
Paul Bakkeraccaffe2014-06-26 13:37:14 +02003748#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003749#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
3750#if defined(MBEDTLS_SHA256_C)
Andrzej Kurekeb342242019-01-29 09:14:33 -05003751#if defined(MBEDTLS_USE_PSA_CRYPTO)
3752 handshake->fin_sha256_psa = psa_hash_operation_init();
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003753 psa_hash_setup(&handshake->fin_sha256_psa, PSA_ALG_SHA_256);
Andrzej Kurekeb342242019-01-29 09:14:33 -05003754#else
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003755 mbedtls_sha256_init(&handshake->fin_sha256);
3756 mbedtls_sha256_starts_ret(&handshake->fin_sha256, 0);
Paul Bakkeraccaffe2014-06-26 13:37:14 +02003757#endif
Andrzej Kurekeb342242019-01-29 09:14:33 -05003758#endif
Gilles Peskined2d59372021-05-12 22:43:27 +02003759#if defined(MBEDTLS_SHA512_C) && !defined(MBEDTLS_SHA512_NO_SHA384)
Andrzej Kurekeb342242019-01-29 09:14:33 -05003760#if defined(MBEDTLS_USE_PSA_CRYPTO)
Andrzej Kurek972fba52019-01-30 03:29:12 -05003761 handshake->fin_sha384_psa = psa_hash_operation_init();
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003762 psa_hash_setup(&handshake->fin_sha384_psa, PSA_ALG_SHA_384);
Andrzej Kurekeb342242019-01-29 09:14:33 -05003763#else
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003764 mbedtls_sha512_init(&handshake->fin_sha512);
3765 mbedtls_sha512_starts_ret(&handshake->fin_sha512, 1);
Paul Bakkeraccaffe2014-06-26 13:37:14 +02003766#endif
Andrzej Kurekeb342242019-01-29 09:14:33 -05003767#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003768#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakkeraccaffe2014-06-26 13:37:14 +02003769
3770 handshake->update_checksum = ssl_update_checksum_start;
Hanno Becker7e5437a2017-04-28 17:15:26 +01003771
3772#if defined(MBEDTLS_SSL_PROTO_TLS1_2) && \
Gilles Peskineeccd8882020-03-10 12:19:08 +01003773 defined(MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003774 mbedtls_ssl_sig_hash_set_init(&handshake->hash_algs);
Hanno Becker7e5437a2017-04-28 17:15:26 +01003775#endif
Paul Bakkeraccaffe2014-06-26 13:37:14 +02003776
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003777#if defined(MBEDTLS_DHM_C)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003778 mbedtls_dhm_init(&handshake->dhm_ctx);
Paul Bakkeraccaffe2014-06-26 13:37:14 +02003779#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003780#if defined(MBEDTLS_ECDH_C)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003781 mbedtls_ecdh_init(&handshake->ecdh_ctx);
Paul Bakkeraccaffe2014-06-26 13:37:14 +02003782#endif
Manuel Pégourié-Gonnardeef142d2015-09-16 10:05:04 +02003783#if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003784 mbedtls_ecjpake_init(&handshake->ecjpake_ctx);
Manuel Pégourié-Gonnard77c06462015-09-17 13:59:49 +02003785#if defined(MBEDTLS_SSL_CLI_C)
3786 handshake->ecjpake_cache = NULL;
3787 handshake->ecjpake_cache_len = 0;
3788#endif
Manuel Pégourié-Gonnard76cfd3f2015-09-15 12:10:54 +02003789#endif
Manuel Pégourié-Gonnardcdc26ae2015-06-19 12:16:31 +02003790
Gilles Peskineeccd8882020-03-10 12:19:08 +01003791#if defined(MBEDTLS_SSL_ECP_RESTARTABLE_ENABLED)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003792 mbedtls_x509_crt_restart_init(&handshake->ecrs_ctx);
Manuel Pégourié-Gonnard862cde52017-05-17 11:56:15 +02003793#endif
3794
Manuel Pégourié-Gonnardcdc26ae2015-06-19 12:16:31 +02003795#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
3796 handshake->sni_authmode = MBEDTLS_SSL_VERIFY_UNSET;
3797#endif
Hanno Becker75173122019-02-06 16:18:31 +00003798
3799#if defined(MBEDTLS_X509_CRT_PARSE_C) && \
3800 !defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003801 mbedtls_pk_init(&handshake->peer_pubkey);
Hanno Becker75173122019-02-06 16:18:31 +00003802#endif
Paul Bakkeraccaffe2014-06-26 13:37:14 +02003803}
3804
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003805void mbedtls_ssl_transform_init(mbedtls_ssl_transform *transform)
Paul Bakkeraccaffe2014-06-26 13:37:14 +02003806{
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003807 memset(transform, 0, sizeof(mbedtls_ssl_transform));
Paul Bakker84bbeb52014-07-01 14:53:22 +02003808
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003809 mbedtls_cipher_init(&transform->cipher_ctx_enc);
3810 mbedtls_cipher_init(&transform->cipher_ctx_dec);
Paul Bakker84bbeb52014-07-01 14:53:22 +02003811
Hanno Beckerd56ed242018-01-03 15:32:51 +00003812#if defined(MBEDTLS_SSL_SOME_MODES_USE_MAC)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003813 mbedtls_md_init(&transform->md_ctx_enc);
3814 mbedtls_md_init(&transform->md_ctx_dec);
Hanno Beckerd56ed242018-01-03 15:32:51 +00003815#endif
Paul Bakkeraccaffe2014-06-26 13:37:14 +02003816}
3817
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003818void mbedtls_ssl_session_init(mbedtls_ssl_session *session)
Paul Bakkeraccaffe2014-06-26 13:37:14 +02003819{
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003820 memset(session, 0, sizeof(mbedtls_ssl_session));
Paul Bakkeraccaffe2014-06-26 13:37:14 +02003821}
3822
Manuel Pégourié-Gonnardd904d662022-06-17 10:24:00 +02003823MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003824static int ssl_handshake_init(mbedtls_ssl_context *ssl)
Paul Bakker48916f92012-09-16 19:57:18 +00003825{
Paul Bakkeraccaffe2014-06-26 13:37:14 +02003826 /* Clear old handshake information if present */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003827 if (ssl->transform_negotiate) {
3828 mbedtls_ssl_transform_free(ssl->transform_negotiate);
3829 }
3830 if (ssl->session_negotiate) {
3831 mbedtls_ssl_session_free(ssl->session_negotiate);
3832 }
3833 if (ssl->handshake) {
3834 mbedtls_ssl_handshake_free(ssl);
3835 }
Paul Bakkeraccaffe2014-06-26 13:37:14 +02003836
3837 /*
3838 * Either the pointers are now NULL or cleared properly and can be freed.
3839 * Now allocate missing structures.
3840 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003841 if (ssl->transform_negotiate == NULL) {
3842 ssl->transform_negotiate = mbedtls_calloc(1, sizeof(mbedtls_ssl_transform));
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02003843 }
Paul Bakker48916f92012-09-16 19:57:18 +00003844
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003845 if (ssl->session_negotiate == NULL) {
3846 ssl->session_negotiate = mbedtls_calloc(1, sizeof(mbedtls_ssl_session));
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02003847 }
Paul Bakker48916f92012-09-16 19:57:18 +00003848
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003849 if (ssl->handshake == NULL) {
3850 ssl->handshake = mbedtls_calloc(1, sizeof(mbedtls_ssl_handshake_params));
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02003851 }
Andrzej Kurek0afa2a12020-03-03 10:39:58 -05003852#if defined(MBEDTLS_SSL_VARIABLE_BUFFER_LENGTH)
3853 /* If the buffers are too small - reallocate */
Andrzej Kurek8ea68722020-04-03 06:40:47 -04003854
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003855 handle_buffer_resizing(ssl, 0, MBEDTLS_SSL_IN_BUFFER_LEN,
3856 MBEDTLS_SSL_OUT_BUFFER_LEN);
Andrzej Kurek0afa2a12020-03-03 10:39:58 -05003857#endif
Paul Bakker48916f92012-09-16 19:57:18 +00003858
Paul Bakkeraccaffe2014-06-26 13:37:14 +02003859 /* All pointers should exist and can be directly freed without issue */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003860 if (ssl->handshake == NULL ||
Paul Bakker48916f92012-09-16 19:57:18 +00003861 ssl->transform_negotiate == NULL ||
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003862 ssl->session_negotiate == NULL) {
3863 MBEDTLS_SSL_DEBUG_MSG(1, ("alloc() of ssl sub-contexts failed"));
Paul Bakkeraccaffe2014-06-26 13:37:14 +02003864
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003865 mbedtls_free(ssl->handshake);
3866 mbedtls_free(ssl->transform_negotiate);
3867 mbedtls_free(ssl->session_negotiate);
Paul Bakkeraccaffe2014-06-26 13:37:14 +02003868
3869 ssl->handshake = NULL;
3870 ssl->transform_negotiate = NULL;
3871 ssl->session_negotiate = NULL;
3872
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003873 return MBEDTLS_ERR_SSL_ALLOC_FAILED;
Paul Bakker48916f92012-09-16 19:57:18 +00003874 }
3875
Paul Bakkeraccaffe2014-06-26 13:37:14 +02003876 /* Initialize structures */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003877 mbedtls_ssl_session_init(ssl->session_negotiate);
3878 mbedtls_ssl_transform_init(ssl->transform_negotiate);
3879 ssl_handshake_params_init(ssl->handshake);
Paul Bakker968afaa2014-07-09 11:09:24 +02003880
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003881#if defined(MBEDTLS_SSL_PROTO_DTLS)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003882 if (ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM) {
Manuel Pégourié-Gonnard06939ce2015-05-11 11:25:46 +02003883 ssl->handshake->alt_transform_out = ssl->transform_out;
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003884
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003885 if (ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT) {
Manuel Pégourié-Gonnard06939ce2015-05-11 11:25:46 +02003886 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_PREPARING;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003887 } else {
Manuel Pégourié-Gonnard06939ce2015-05-11 11:25:46 +02003888 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_WAITING;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003889 }
Manuel Pégourié-Gonnard286a1362015-05-13 16:22:05 +02003890
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003891 mbedtls_ssl_set_timer(ssl, 0);
Manuel Pégourié-Gonnard06939ce2015-05-11 11:25:46 +02003892 }
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003893#endif
3894
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003895 return 0;
Paul Bakker48916f92012-09-16 19:57:18 +00003896}
3897
Manuel Pégourié-Gonnarde057d3b2015-05-20 10:59:43 +02003898#if defined(MBEDTLS_SSL_DTLS_HELLO_VERIFY) && defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard7d38d212014-07-23 17:52:09 +02003899/* Dummy cookie callbacks for defaults */
Manuel Pégourié-Gonnardd904d662022-06-17 10:24:00 +02003900MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003901static int ssl_cookie_write_dummy(void *ctx,
3902 unsigned char **p, unsigned char *end,
3903 const unsigned char *cli_id, size_t cli_id_len)
Manuel Pégourié-Gonnard7d38d212014-07-23 17:52:09 +02003904{
3905 ((void) ctx);
3906 ((void) p);
3907 ((void) end);
3908 ((void) cli_id);
3909 ((void) cli_id_len);
3910
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003911 return MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
Manuel Pégourié-Gonnard7d38d212014-07-23 17:52:09 +02003912}
3913
Manuel Pégourié-Gonnardd904d662022-06-17 10:24:00 +02003914MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003915static int ssl_cookie_check_dummy(void *ctx,
3916 const unsigned char *cookie, size_t cookie_len,
3917 const unsigned char *cli_id, size_t cli_id_len)
Manuel Pégourié-Gonnard7d38d212014-07-23 17:52:09 +02003918{
3919 ((void) ctx);
3920 ((void) cookie);
3921 ((void) cookie_len);
3922 ((void) cli_id);
3923 ((void) cli_id_len);
3924
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003925 return MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
Manuel Pégourié-Gonnard7d38d212014-07-23 17:52:09 +02003926}
Manuel Pégourié-Gonnarde057d3b2015-05-20 10:59:43 +02003927#endif /* MBEDTLS_SSL_DTLS_HELLO_VERIFY && MBEDTLS_SSL_SRV_C */
Manuel Pégourié-Gonnard7d38d212014-07-23 17:52:09 +02003928
Paul Bakker5121ce52009-01-03 21:22:43 +00003929/*
3930 * Initialize an SSL context
3931 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003932void mbedtls_ssl_init(mbedtls_ssl_context *ssl)
Manuel Pégourié-Gonnard41d479e2015-04-29 00:48:22 +02003933{
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003934 memset(ssl, 0, sizeof(mbedtls_ssl_context));
Manuel Pégourié-Gonnard41d479e2015-04-29 00:48:22 +02003935}
3936
3937/*
3938 * Setup an SSL context
3939 */
Hanno Becker2a43f6f2018-08-10 11:12:52 +01003940
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003941int mbedtls_ssl_setup(mbedtls_ssl_context *ssl,
3942 const mbedtls_ssl_config *conf)
Paul Bakker5121ce52009-01-03 21:22:43 +00003943{
Janos Follath865b3eb2019-12-16 11:46:15 +00003944 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Darryl Greenb33cc762019-11-28 14:29:44 +00003945 size_t in_buf_len = MBEDTLS_SSL_IN_BUFFER_LEN;
3946 size_t out_buf_len = MBEDTLS_SSL_OUT_BUFFER_LEN;
Paul Bakker5121ce52009-01-03 21:22:43 +00003947
Manuel Pégourié-Gonnarddef0bbe2015-05-04 14:56:36 +02003948 ssl->conf = conf;
Paul Bakker62f2dee2012-09-28 07:31:51 +00003949
3950 /*
Manuel Pégourié-Gonnard06193482014-02-14 08:39:32 +01003951 * Prepare base structures
Paul Bakker62f2dee2012-09-28 07:31:51 +00003952 */
k-stachowiakc9a5f022018-07-24 13:53:31 +02003953
3954 /* Set to NULL in case of an error condition */
3955 ssl->out_buf = NULL;
k-stachowiaka47911c2018-07-04 17:41:58 +02003956
Darryl Greenb33cc762019-11-28 14:29:44 +00003957#if defined(MBEDTLS_SSL_VARIABLE_BUFFER_LENGTH)
3958 ssl->in_buf_len = in_buf_len;
3959#endif
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003960 ssl->in_buf = mbedtls_calloc(1, in_buf_len);
3961 if (ssl->in_buf == NULL) {
3962 MBEDTLS_SSL_DEBUG_MSG(1, ("alloc(%" MBEDTLS_PRINTF_SIZET " bytes) failed", in_buf_len));
k-stachowiak9f7798e2018-07-31 16:52:32 +02003963 ret = MBEDTLS_ERR_SSL_ALLOC_FAILED;
k-stachowiaka47911c2018-07-04 17:41:58 +02003964 goto error;
Angus Grattond8213d02016-05-25 20:56:48 +10003965 }
3966
Darryl Greenb33cc762019-11-28 14:29:44 +00003967#if defined(MBEDTLS_SSL_VARIABLE_BUFFER_LENGTH)
3968 ssl->out_buf_len = out_buf_len;
3969#endif
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003970 ssl->out_buf = mbedtls_calloc(1, out_buf_len);
3971 if (ssl->out_buf == NULL) {
3972 MBEDTLS_SSL_DEBUG_MSG(1, ("alloc(%" MBEDTLS_PRINTF_SIZET " bytes) failed", out_buf_len));
k-stachowiak9f7798e2018-07-31 16:52:32 +02003973 ret = MBEDTLS_ERR_SSL_ALLOC_FAILED;
k-stachowiaka47911c2018-07-04 17:41:58 +02003974 goto error;
Paul Bakker5121ce52009-01-03 21:22:43 +00003975 }
3976
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003977 mbedtls_ssl_reset_in_out_pointers(ssl);
Manuel Pégourié-Gonnard419d5ae2015-05-04 19:32:36 +02003978
Johan Pascalb62bb512015-12-03 21:56:45 +01003979#if defined(MBEDTLS_SSL_DTLS_SRTP)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003980 memset(&ssl->dtls_srtp_info, 0, sizeof(ssl->dtls_srtp_info));
Johan Pascalb62bb512015-12-03 21:56:45 +01003981#endif
3982
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003983 if ((ret = ssl_handshake_init(ssl)) != 0) {
k-stachowiaka47911c2018-07-04 17:41:58 +02003984 goto error;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003985 }
Paul Bakker5121ce52009-01-03 21:22:43 +00003986
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003987 return 0;
k-stachowiaka47911c2018-07-04 17:41:58 +02003988
3989error:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003990 mbedtls_free(ssl->in_buf);
3991 mbedtls_free(ssl->out_buf);
k-stachowiaka47911c2018-07-04 17:41:58 +02003992
3993 ssl->conf = NULL;
3994
Darryl Greenb33cc762019-11-28 14:29:44 +00003995#if defined(MBEDTLS_SSL_VARIABLE_BUFFER_LENGTH)
3996 ssl->in_buf_len = 0;
3997 ssl->out_buf_len = 0;
3998#endif
k-stachowiaka47911c2018-07-04 17:41:58 +02003999 ssl->in_buf = NULL;
4000 ssl->out_buf = NULL;
4001
4002 ssl->in_hdr = NULL;
4003 ssl->in_ctr = NULL;
4004 ssl->in_len = NULL;
4005 ssl->in_iv = NULL;
4006 ssl->in_msg = NULL;
4007
4008 ssl->out_hdr = NULL;
4009 ssl->out_ctr = NULL;
4010 ssl->out_len = NULL;
4011 ssl->out_iv = NULL;
4012 ssl->out_msg = NULL;
4013
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004014 return ret;
Paul Bakker5121ce52009-01-03 21:22:43 +00004015}
4016
4017/*
Paul Bakker7eb013f2011-10-06 12:37:39 +00004018 * Reset an initialized and used SSL context for re-use while retaining
4019 * all application-set variables, function pointers and data.
Manuel Pégourié-Gonnard3f09b6d2015-09-08 11:58:14 +02004020 *
4021 * If partial is non-zero, keep data in the input buffer and client ID.
4022 * (Use when a DTLS client reconnects from the same port.)
Paul Bakker7eb013f2011-10-06 12:37:39 +00004023 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004024int mbedtls_ssl_session_reset_int(mbedtls_ssl_context *ssl, int partial)
Paul Bakker7eb013f2011-10-06 12:37:39 +00004025{
Janos Follath865b3eb2019-12-16 11:46:15 +00004026 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Darryl Greenb33cc762019-11-28 14:29:44 +00004027#if defined(MBEDTLS_SSL_VARIABLE_BUFFER_LENGTH)
4028 size_t in_buf_len = ssl->in_buf_len;
4029 size_t out_buf_len = ssl->out_buf_len;
4030#else
4031 size_t in_buf_len = MBEDTLS_SSL_IN_BUFFER_LEN;
4032 size_t out_buf_len = MBEDTLS_SSL_OUT_BUFFER_LEN;
4033#endif
Paul Bakker48916f92012-09-16 19:57:18 +00004034
Hanno Becker7e772132018-08-10 12:38:21 +01004035#if !defined(MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE) || \
4036 !defined(MBEDTLS_SSL_SRV_C)
4037 ((void) partial);
4038#endif
4039
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004040 ssl->state = MBEDTLS_SSL_HELLO_REQUEST;
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01004041
Manuel Pégourié-Gonnard286a1362015-05-13 16:22:05 +02004042 /* Cancel any possibly running timer */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004043 mbedtls_ssl_set_timer(ssl, 0);
Manuel Pégourié-Gonnard286a1362015-05-13 16:22:05 +02004044
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004045#if defined(MBEDTLS_SSL_RENEGOTIATION)
4046 ssl->renego_status = MBEDTLS_SSL_INITIAL_HANDSHAKE;
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01004047 ssl->renego_records_seen = 0;
Paul Bakker48916f92012-09-16 19:57:18 +00004048
4049 ssl->verify_data_len = 0;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004050 memset(ssl->own_verify_data, 0, MBEDTLS_SSL_VERIFY_DATA_MAX_LEN);
4051 memset(ssl->peer_verify_data, 0, MBEDTLS_SSL_VERIFY_DATA_MAX_LEN);
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01004052#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004053 ssl->secure_renegotiation = MBEDTLS_SSL_LEGACY_RENEGOTIATION;
Paul Bakker48916f92012-09-16 19:57:18 +00004054
Paul Bakker7eb013f2011-10-06 12:37:39 +00004055 ssl->in_offt = NULL;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004056 mbedtls_ssl_reset_in_out_pointers(ssl);
Paul Bakker7eb013f2011-10-06 12:37:39 +00004057
4058 ssl->in_msgtype = 0;
4059 ssl->in_msglen = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004060#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02004061 ssl->next_record_offset = 0;
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02004062 ssl->in_epoch = 0;
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02004063#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004064#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004065 mbedtls_ssl_dtls_replay_reset(ssl);
Manuel Pégourié-Gonnardb47368a2014-09-24 13:29:58 +02004066#endif
Paul Bakker7eb013f2011-10-06 12:37:39 +00004067
4068 ssl->in_hslen = 0;
4069 ssl->nb_zero = 0;
Hanno Beckeraf0665d2017-05-24 09:16:26 +01004070
4071 ssl->keep_current_message = 0;
Paul Bakker7eb013f2011-10-06 12:37:39 +00004072
4073 ssl->out_msgtype = 0;
4074 ssl->out_msglen = 0;
4075 ssl->out_left = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004076#if defined(MBEDTLS_SSL_CBC_RECORD_SPLITTING)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004077 if (ssl->split_done != MBEDTLS_SSL_CBC_RECORD_SPLITTING_DISABLED) {
Manuel Pégourié-Gonnardcfa477e2015-01-07 14:50:54 +01004078 ssl->split_done = 0;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004079 }
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +01004080#endif
Paul Bakker7eb013f2011-10-06 12:37:39 +00004081
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004082 memset(ssl->cur_out_ctr, 0, sizeof(ssl->cur_out_ctr));
Hanno Becker19859472018-08-06 09:40:20 +01004083
Paul Bakker48916f92012-09-16 19:57:18 +00004084 ssl->transform_in = NULL;
4085 ssl->transform_out = NULL;
Paul Bakker7eb013f2011-10-06 12:37:39 +00004086
Hanno Becker78640902018-08-13 16:35:15 +01004087 ssl->session_in = NULL;
4088 ssl->session_out = NULL;
4089
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004090 memset(ssl->out_buf, 0, out_buf_len);
Hanno Becker4ccbf062018-08-10 11:20:38 +01004091
David Horstmannd4f22082022-10-26 18:25:14 +01004092 int clear_in_buf = 1;
Hanno Becker4ccbf062018-08-10 11:20:38 +01004093#if defined(MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE) && defined(MBEDTLS_SSL_SRV_C)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004094 if (partial != 0) {
David Horstmannd4f22082022-10-26 18:25:14 +01004095 clear_in_buf = 0;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004096 }
Hanno Becker4ccbf062018-08-10 11:20:38 +01004097#endif /* MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE && MBEDTLS_SSL_SRV_C */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004098 if (clear_in_buf) {
Hanno Becker4ccbf062018-08-10 11:20:38 +01004099 ssl->in_left = 0;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004100 memset(ssl->in_buf, 0, in_buf_len);
Hanno Becker4ccbf062018-08-10 11:20:38 +01004101 }
Paul Bakker05ef8352012-05-08 09:17:57 +00004102
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004103#if defined(MBEDTLS_SSL_HW_RECORD_ACCEL)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004104 if (mbedtls_ssl_hw_record_reset != NULL) {
4105 MBEDTLS_SSL_DEBUG_MSG(2, ("going for mbedtls_ssl_hw_record_reset()"));
4106 if ((ret = mbedtls_ssl_hw_record_reset(ssl)) != 0) {
4107 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ssl_hw_record_reset", ret);
4108 return MBEDTLS_ERR_SSL_HW_ACCEL_FAILED;
Paul Bakker2770fbd2012-07-03 13:30:23 +00004109 }
Paul Bakker05ef8352012-05-08 09:17:57 +00004110 }
4111#endif
Paul Bakker2770fbd2012-07-03 13:30:23 +00004112
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004113 if (ssl->transform) {
4114 mbedtls_ssl_transform_free(ssl->transform);
4115 mbedtls_free(ssl->transform);
Paul Bakker48916f92012-09-16 19:57:18 +00004116 ssl->transform = NULL;
Paul Bakker2770fbd2012-07-03 13:30:23 +00004117 }
Paul Bakker48916f92012-09-16 19:57:18 +00004118
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004119 if (ssl->session) {
4120 mbedtls_ssl_session_free(ssl->session);
4121 mbedtls_free(ssl->session);
Paul Bakkerc0463502013-02-14 11:19:38 +01004122 ssl->session = NULL;
4123 }
4124
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004125#if defined(MBEDTLS_SSL_ALPN)
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02004126 ssl->alpn_chosen = NULL;
4127#endif
4128
Manuel Pégourié-Gonnarde057d3b2015-05-20 10:59:43 +02004129#if defined(MBEDTLS_SSL_DTLS_HELLO_VERIFY) && defined(MBEDTLS_SSL_SRV_C)
David Horstmannb5b1ed22022-10-27 13:21:49 +01004130 int free_cli_id = 1;
Hanno Becker4ccbf062018-08-10 11:20:38 +01004131#if defined(MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004132 if (partial != 0) {
David Horstmannd4f22082022-10-26 18:25:14 +01004133 free_cli_id = 0;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004134 }
Hanno Becker4ccbf062018-08-10 11:20:38 +01004135#endif
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004136 if (free_cli_id) {
4137 mbedtls_free(ssl->cli_id);
Manuel Pégourié-Gonnard3f09b6d2015-09-08 11:58:14 +02004138 ssl->cli_id = NULL;
4139 ssl->cli_id_len = 0;
4140 }
Manuel Pégourié-Gonnard43c02182014-07-22 17:32:01 +02004141#endif
4142
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004143 if ((ret = ssl_handshake_init(ssl)) != 0) {
4144 return ret;
4145 }
Paul Bakker2770fbd2012-07-03 13:30:23 +00004146
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004147 return 0;
Paul Bakker7eb013f2011-10-06 12:37:39 +00004148}
4149
Manuel Pégourié-Gonnard779e4292013-08-03 13:50:48 +02004150/*
Manuel Pégourié-Gonnard3f09b6d2015-09-08 11:58:14 +02004151 * Reset an initialized and used SSL context for re-use while retaining
4152 * all application-set variables, function pointers and data.
4153 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004154int mbedtls_ssl_session_reset(mbedtls_ssl_context *ssl)
Manuel Pégourié-Gonnard3f09b6d2015-09-08 11:58:14 +02004155{
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004156 return mbedtls_ssl_session_reset_int(ssl, 0);
Manuel Pégourié-Gonnard3f09b6d2015-09-08 11:58:14 +02004157}
4158
4159/*
Paul Bakker5121ce52009-01-03 21:22:43 +00004160 * SSL set accessors
4161 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004162void mbedtls_ssl_conf_endpoint(mbedtls_ssl_config *conf, int endpoint)
Paul Bakker5121ce52009-01-03 21:22:43 +00004163{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02004164 conf->endpoint = endpoint;
Paul Bakker5121ce52009-01-03 21:22:43 +00004165}
4166
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004167void mbedtls_ssl_conf_transport(mbedtls_ssl_config *conf, int transport)
Manuel Pégourié-Gonnard0b1ff292014-02-06 13:04:16 +01004168{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02004169 conf->transport = transport;
Manuel Pégourié-Gonnard0b1ff292014-02-06 13:04:16 +01004170}
4171
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004172#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004173void mbedtls_ssl_conf_dtls_anti_replay(mbedtls_ssl_config *conf, char mode)
Manuel Pégourié-Gonnard27393132014-09-24 14:41:11 +02004174{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02004175 conf->anti_replay = mode;
Manuel Pégourié-Gonnard27393132014-09-24 14:41:11 +02004176}
4177#endif
4178
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004179#if defined(MBEDTLS_SSL_DTLS_BADMAC_LIMIT)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004180void mbedtls_ssl_conf_dtls_badmac_limit(mbedtls_ssl_config *conf, unsigned limit)
Manuel Pégourié-Gonnardb0643d12014-10-14 18:30:36 +02004181{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02004182 conf->badmac_limit = limit;
Manuel Pégourié-Gonnardb0643d12014-10-14 18:30:36 +02004183}
4184#endif
4185
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004186#if defined(MBEDTLS_SSL_PROTO_DTLS)
Hanno Becker04da1892018-08-14 13:22:10 +01004187
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004188void mbedtls_ssl_set_datagram_packing(mbedtls_ssl_context *ssl,
4189 unsigned allow_packing)
Hanno Becker04da1892018-08-14 13:22:10 +01004190{
4191 ssl->disable_datagram_packing = !allow_packing;
4192}
4193
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004194void mbedtls_ssl_conf_handshake_timeout(mbedtls_ssl_config *conf,
4195 uint32_t min, uint32_t max)
Manuel Pégourié-Gonnard905dd242014-10-01 12:03:55 +02004196{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02004197 conf->hs_timeout_min = min;
4198 conf->hs_timeout_max = max;
Manuel Pégourié-Gonnard905dd242014-10-01 12:03:55 +02004199}
4200#endif
4201
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004202void mbedtls_ssl_conf_authmode(mbedtls_ssl_config *conf, int authmode)
Paul Bakker5121ce52009-01-03 21:22:43 +00004203{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02004204 conf->authmode = authmode;
Paul Bakker5121ce52009-01-03 21:22:43 +00004205}
4206
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004207#if defined(MBEDTLS_X509_CRT_PARSE_C)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004208void mbedtls_ssl_conf_verify(mbedtls_ssl_config *conf,
4209 int (*f_vrfy)(void *, mbedtls_x509_crt *, int, uint32_t *),
4210 void *p_vrfy)
Paul Bakkerb63b0af2011-01-13 17:54:59 +00004211{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02004212 conf->f_vrfy = f_vrfy;
4213 conf->p_vrfy = p_vrfy;
Paul Bakkerb63b0af2011-01-13 17:54:59 +00004214}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004215#endif /* MBEDTLS_X509_CRT_PARSE_C */
Paul Bakkerb63b0af2011-01-13 17:54:59 +00004216
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004217void mbedtls_ssl_conf_rng(mbedtls_ssl_config *conf,
4218 int (*f_rng)(void *, unsigned char *, size_t),
4219 void *p_rng)
Paul Bakker5121ce52009-01-03 21:22:43 +00004220{
Manuel Pégourié-Gonnard750e4d72015-05-07 12:35:38 +01004221 conf->f_rng = f_rng;
4222 conf->p_rng = p_rng;
Paul Bakker5121ce52009-01-03 21:22:43 +00004223}
4224
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004225void mbedtls_ssl_conf_dbg(mbedtls_ssl_config *conf,
4226 void (*f_dbg)(void *, int, const char *, int, const char *),
4227 void *p_dbg)
Paul Bakker5121ce52009-01-03 21:22:43 +00004228{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02004229 conf->f_dbg = f_dbg;
4230 conf->p_dbg = p_dbg;
Paul Bakker5121ce52009-01-03 21:22:43 +00004231}
4232
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004233void mbedtls_ssl_set_bio(mbedtls_ssl_context *ssl,
4234 void *p_bio,
4235 mbedtls_ssl_send_t *f_send,
4236 mbedtls_ssl_recv_t *f_recv,
4237 mbedtls_ssl_recv_timeout_t *f_recv_timeout)
Manuel Pégourié-Gonnard8fa6dfd2014-09-17 10:47:43 +02004238{
4239 ssl->p_bio = p_bio;
4240 ssl->f_send = f_send;
4241 ssl->f_recv = f_recv;
4242 ssl->f_recv_timeout = f_recv_timeout;
Manuel Pégourié-Gonnard97fd52c2015-05-06 15:38:52 +01004243}
4244
Manuel Pégourié-Gonnard6e7aaca2018-08-20 10:37:23 +02004245#if defined(MBEDTLS_SSL_PROTO_DTLS)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004246void mbedtls_ssl_set_mtu(mbedtls_ssl_context *ssl, uint16_t mtu)
Manuel Pégourié-Gonnard6e7aaca2018-08-20 10:37:23 +02004247{
4248 ssl->mtu = mtu;
4249}
4250#endif
4251
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004252void mbedtls_ssl_conf_read_timeout(mbedtls_ssl_config *conf, uint32_t timeout)
Manuel Pégourié-Gonnard97fd52c2015-05-06 15:38:52 +01004253{
4254 conf->read_timeout = timeout;
Manuel Pégourié-Gonnard8fa6dfd2014-09-17 10:47:43 +02004255}
4256
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004257void mbedtls_ssl_set_timer_cb(mbedtls_ssl_context *ssl,
4258 void *p_timer,
4259 mbedtls_ssl_set_timer_t *f_set_timer,
4260 mbedtls_ssl_get_timer_t *f_get_timer)
Manuel Pégourié-Gonnard2e012912015-05-12 20:55:41 +02004261{
4262 ssl->p_timer = p_timer;
4263 ssl->f_set_timer = f_set_timer;
4264 ssl->f_get_timer = f_get_timer;
Manuel Pégourié-Gonnard286a1362015-05-13 16:22:05 +02004265
4266 /* Make sure we start with no timer running */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004267 mbedtls_ssl_set_timer(ssl, 0);
Manuel Pégourié-Gonnard2e012912015-05-12 20:55:41 +02004268}
4269
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004270#if defined(MBEDTLS_SSL_SRV_C)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004271void mbedtls_ssl_conf_session_cache(mbedtls_ssl_config *conf,
4272 void *p_cache,
4273 int (*f_get_cache)(void *, mbedtls_ssl_session *),
4274 int (*f_set_cache)(void *, const mbedtls_ssl_session *))
Paul Bakker5121ce52009-01-03 21:22:43 +00004275{
Manuel Pégourié-Gonnard5cb33082015-05-06 18:06:26 +01004276 conf->p_cache = p_cache;
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02004277 conf->f_get_cache = f_get_cache;
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02004278 conf->f_set_cache = f_set_cache;
Paul Bakker5121ce52009-01-03 21:22:43 +00004279}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004280#endif /* MBEDTLS_SSL_SRV_C */
Paul Bakker5121ce52009-01-03 21:22:43 +00004281
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004282#if defined(MBEDTLS_SSL_CLI_C)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004283int mbedtls_ssl_set_session(mbedtls_ssl_context *ssl, const mbedtls_ssl_session *session)
Paul Bakker5121ce52009-01-03 21:22:43 +00004284{
Janos Follath865b3eb2019-12-16 11:46:15 +00004285 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +02004286
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004287 if (ssl == NULL ||
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +02004288 session == NULL ||
4289 ssl->session_negotiate == NULL ||
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004290 ssl->conf->endpoint != MBEDTLS_SSL_IS_CLIENT) {
4291 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +02004292 }
4293
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004294 if ((ret = mbedtls_ssl_session_copy(ssl->session_negotiate,
4295 session)) != 0) {
4296 return ret;
4297 }
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +02004298
Paul Bakker0a597072012-09-25 21:55:46 +00004299 ssl->handshake->resume = 1;
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +02004300
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004301 return 0;
Paul Bakker5121ce52009-01-03 21:22:43 +00004302}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004303#endif /* MBEDTLS_SSL_CLI_C */
Paul Bakker5121ce52009-01-03 21:22:43 +00004304
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004305void mbedtls_ssl_conf_ciphersuites(mbedtls_ssl_config *conf,
4306 const int *ciphersuites)
Paul Bakker5121ce52009-01-03 21:22:43 +00004307{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02004308 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_0] = ciphersuites;
4309 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_1] = ciphersuites;
4310 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_2] = ciphersuites;
4311 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_3] = ciphersuites;
Paul Bakker8f4ddae2013-04-15 15:09:54 +02004312}
4313
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004314void mbedtls_ssl_conf_ciphersuites_for_version(mbedtls_ssl_config *conf,
4315 const int *ciphersuites,
4316 int major, int minor)
Paul Bakker8f4ddae2013-04-15 15:09:54 +02004317{
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004318 if (major != MBEDTLS_SSL_MAJOR_VERSION_3) {
Paul Bakker8f4ddae2013-04-15 15:09:54 +02004319 return;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004320 }
Paul Bakker8f4ddae2013-04-15 15:09:54 +02004321
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004322 if (minor < MBEDTLS_SSL_MINOR_VERSION_0 || minor > MBEDTLS_SSL_MINOR_VERSION_3) {
Paul Bakker8f4ddae2013-04-15 15:09:54 +02004323 return;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004324 }
Paul Bakker8f4ddae2013-04-15 15:09:54 +02004325
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02004326 conf->ciphersuite_list[minor] = ciphersuites;
Paul Bakker5121ce52009-01-03 21:22:43 +00004327}
4328
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004329#if defined(MBEDTLS_X509_CRT_PARSE_C)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004330void mbedtls_ssl_conf_cert_profile(mbedtls_ssl_config *conf,
4331 const mbedtls_x509_crt_profile *profile)
Manuel Pégourié-Gonnard6e3ee3a2015-06-17 10:58:20 +02004332{
4333 conf->cert_profile = profile;
4334}
4335
Manuel Pégourié-Gonnard8f618a82015-05-10 21:13:36 +02004336/* Append a new keycert entry to a (possibly empty) list */
Manuel Pégourié-Gonnardd904d662022-06-17 10:24:00 +02004337MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004338static int ssl_append_key_cert(mbedtls_ssl_key_cert **head,
4339 mbedtls_x509_crt *cert,
4340 mbedtls_pk_context *key)
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02004341{
niisato8ee24222018-06-25 19:05:48 +09004342 mbedtls_ssl_key_cert *new_cert;
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02004343
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004344 new_cert = mbedtls_calloc(1, sizeof(mbedtls_ssl_key_cert));
4345 if (new_cert == NULL) {
4346 return MBEDTLS_ERR_SSL_ALLOC_FAILED;
4347 }
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02004348
niisato8ee24222018-06-25 19:05:48 +09004349 new_cert->cert = cert;
4350 new_cert->key = key;
4351 new_cert->next = NULL;
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02004352
Manuel Pégourié-Gonnard8f618a82015-05-10 21:13:36 +02004353 /* Update head is the list was null, else add to the end */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004354 if (*head == NULL) {
niisato8ee24222018-06-25 19:05:48 +09004355 *head = new_cert;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004356 } else {
Manuel Pégourié-Gonnard8f618a82015-05-10 21:13:36 +02004357 mbedtls_ssl_key_cert *cur = *head;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004358 while (cur->next != NULL) {
Manuel Pégourié-Gonnard8f618a82015-05-10 21:13:36 +02004359 cur = cur->next;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004360 }
niisato8ee24222018-06-25 19:05:48 +09004361 cur->next = new_cert;
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02004362 }
4363
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004364 return 0;
Manuel Pégourié-Gonnard8f618a82015-05-10 21:13:36 +02004365}
4366
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004367int mbedtls_ssl_conf_own_cert(mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnard8f618a82015-05-10 21:13:36 +02004368 mbedtls_x509_crt *own_cert,
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004369 mbedtls_pk_context *pk_key)
Manuel Pégourié-Gonnard8f618a82015-05-10 21:13:36 +02004370{
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004371 return ssl_append_key_cert(&conf->key_cert, own_cert, pk_key);
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02004372}
4373
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004374void mbedtls_ssl_conf_ca_chain(mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnardbc2b7712015-05-06 11:14:19 +01004375 mbedtls_x509_crt *ca_chain,
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004376 mbedtls_x509_crl *ca_crl)
Paul Bakker5121ce52009-01-03 21:22:43 +00004377{
Manuel Pégourié-Gonnardbc2b7712015-05-06 11:14:19 +01004378 conf->ca_chain = ca_chain;
4379 conf->ca_crl = ca_crl;
Hanno Becker5adaad92019-03-27 16:54:37 +00004380
4381#if defined(MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK)
4382 /* mbedtls_ssl_conf_ca_chain() and mbedtls_ssl_conf_ca_cb()
4383 * cannot be used together. */
4384 conf->f_ca_cb = NULL;
4385 conf->p_ca_cb = NULL;
4386#endif /* MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK */
Paul Bakker5121ce52009-01-03 21:22:43 +00004387}
Hanno Becker5adaad92019-03-27 16:54:37 +00004388
4389#if defined(MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004390void mbedtls_ssl_conf_ca_cb(mbedtls_ssl_config *conf,
4391 mbedtls_x509_crt_ca_cb_t f_ca_cb,
4392 void *p_ca_cb)
Hanno Becker5adaad92019-03-27 16:54:37 +00004393{
4394 conf->f_ca_cb = f_ca_cb;
4395 conf->p_ca_cb = p_ca_cb;
4396
4397 /* mbedtls_ssl_conf_ca_chain() and mbedtls_ssl_conf_ca_cb()
4398 * cannot be used together. */
4399 conf->ca_chain = NULL;
4400 conf->ca_crl = NULL;
4401}
4402#endif /* MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004403#endif /* MBEDTLS_X509_CRT_PARSE_C */
Paul Bakkereb2c6582012-09-27 19:15:01 +00004404
Manuel Pégourié-Gonnard1af6c852015-05-10 23:10:37 +02004405#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004406int mbedtls_ssl_set_hs_own_cert(mbedtls_ssl_context *ssl,
4407 mbedtls_x509_crt *own_cert,
4408 mbedtls_pk_context *pk_key)
Manuel Pégourié-Gonnard1af6c852015-05-10 23:10:37 +02004409{
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004410 return ssl_append_key_cert(&ssl->handshake->sni_key_cert,
4411 own_cert, pk_key);
Manuel Pégourié-Gonnard1af6c852015-05-10 23:10:37 +02004412}
Manuel Pégourié-Gonnard22bfa4b2015-05-11 08:46:37 +02004413
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004414void mbedtls_ssl_set_hs_ca_chain(mbedtls_ssl_context *ssl,
4415 mbedtls_x509_crt *ca_chain,
4416 mbedtls_x509_crl *ca_crl)
Manuel Pégourié-Gonnard22bfa4b2015-05-11 08:46:37 +02004417{
4418 ssl->handshake->sni_ca_chain = ca_chain;
4419 ssl->handshake->sni_ca_crl = ca_crl;
4420}
Manuel Pégourié-Gonnardcdc26ae2015-06-19 12:16:31 +02004421
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004422void mbedtls_ssl_set_hs_authmode(mbedtls_ssl_context *ssl,
4423 int authmode)
Manuel Pégourié-Gonnardcdc26ae2015-06-19 12:16:31 +02004424{
4425 ssl->handshake->sni_authmode = authmode;
4426}
Manuel Pégourié-Gonnard1af6c852015-05-10 23:10:37 +02004427#endif /* MBEDTLS_SSL_SERVER_NAME_INDICATION */
4428
Hanno Becker8927c832019-04-03 12:52:50 +01004429#if defined(MBEDTLS_X509_CRT_PARSE_C)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004430void mbedtls_ssl_set_verify(mbedtls_ssl_context *ssl,
4431 int (*f_vrfy)(void *, mbedtls_x509_crt *, int, uint32_t *),
4432 void *p_vrfy)
Hanno Becker8927c832019-04-03 12:52:50 +01004433{
4434 ssl->f_vrfy = f_vrfy;
4435 ssl->p_vrfy = p_vrfy;
4436}
4437#endif
4438
Manuel Pégourié-Gonnardeef142d2015-09-16 10:05:04 +02004439#if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
Manuel Pégourié-Gonnard7002f4a2015-09-15 12:43:43 +02004440/*
4441 * Set EC J-PAKE password for current handshake
4442 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004443int mbedtls_ssl_set_hs_ecjpake_password(mbedtls_ssl_context *ssl,
4444 const unsigned char *pw,
4445 size_t pw_len)
Manuel Pégourié-Gonnard7002f4a2015-09-15 12:43:43 +02004446{
4447 mbedtls_ecjpake_role role;
4448
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004449 if (ssl->handshake == NULL || ssl->conf == NULL) {
4450 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
4451 }
Manuel Pégourié-Gonnard7002f4a2015-09-15 12:43:43 +02004452
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004453 if (ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER) {
Manuel Pégourié-Gonnard7002f4a2015-09-15 12:43:43 +02004454 role = MBEDTLS_ECJPAKE_SERVER;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004455 } else {
Manuel Pégourié-Gonnard7002f4a2015-09-15 12:43:43 +02004456 role = MBEDTLS_ECJPAKE_CLIENT;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004457 }
Manuel Pégourié-Gonnard7002f4a2015-09-15 12:43:43 +02004458
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004459 return mbedtls_ecjpake_setup(&ssl->handshake->ecjpake_ctx,
4460 role,
4461 MBEDTLS_MD_SHA256,
4462 MBEDTLS_ECP_DP_SECP256R1,
4463 pw, pw_len);
Manuel Pégourié-Gonnard7002f4a2015-09-15 12:43:43 +02004464}
Manuel Pégourié-Gonnardeef142d2015-09-16 10:05:04 +02004465#endif /* MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED */
Manuel Pégourié-Gonnard7002f4a2015-09-15 12:43:43 +02004466
Gilles Peskineeccd8882020-03-10 12:19:08 +01004467#if defined(MBEDTLS_KEY_EXCHANGE_SOME_PSK_ENABLED)
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01004468
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004469static void ssl_conf_remove_psk(mbedtls_ssl_config *conf)
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01004470{
4471 /* Remove reference to existing PSK, if any. */
4472#if defined(MBEDTLS_USE_PSA_CRYPTO)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004473 if (!mbedtls_svc_key_id_is_null(conf->psk_opaque)) {
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01004474 /* The maintenance of the PSK key slot is the
4475 * user's responsibility. */
Ronald Croncf56a0a2020-08-04 09:51:30 +02004476 conf->psk_opaque = MBEDTLS_SVC_KEY_ID_INIT;
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01004477 }
Hanno Beckera63ac3f2018-11-05 12:47:16 +00004478 /* This and the following branch should never
Tom Cosgrove49f99bc2022-12-04 16:44:21 +00004479 * be taken simultaneously as we maintain the
Hanno Beckera63ac3f2018-11-05 12:47:16 +00004480 * invariant that raw and opaque PSKs are never
4481 * configured simultaneously. As a safeguard,
4482 * though, `else` is omitted here. */
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01004483#endif /* MBEDTLS_USE_PSA_CRYPTO */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004484 if (conf->psk != NULL) {
4485 mbedtls_platform_zeroize(conf->psk, conf->psk_len);
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01004486
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004487 mbedtls_free(conf->psk);
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01004488 conf->psk = NULL;
4489 conf->psk_len = 0;
4490 }
4491
4492 /* Remove reference to PSK identity, if any. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004493 if (conf->psk_identity != NULL) {
4494 mbedtls_free(conf->psk_identity);
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01004495 conf->psk_identity = NULL;
4496 conf->psk_identity_len = 0;
4497 }
4498}
4499
Hanno Becker7390c712018-11-15 13:33:04 +00004500/* This function assumes that PSK identity in the SSL config is unset.
4501 * It checks that the provided identity is well-formed and attempts
4502 * to make a copy of it in the SSL config.
4503 * On failure, the PSK identity in the config remains unset. */
Manuel Pégourié-Gonnardd904d662022-06-17 10:24:00 +02004504MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004505static int ssl_conf_set_psk_identity(mbedtls_ssl_config *conf,
4506 unsigned char const *psk_identity,
4507 size_t psk_identity_len)
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02004508{
Manuel Pégourié-Gonnardc6b5d832015-08-27 16:37:35 +02004509 /* Identity len will be encoded on two bytes */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004510 if (psk_identity == NULL ||
4511 (psk_identity_len >> 16) != 0 ||
4512 psk_identity_len > MBEDTLS_SSL_OUT_CONTENT_LEN) {
4513 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
Manuel Pégourié-Gonnardc6b5d832015-08-27 16:37:35 +02004514 }
4515
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004516 conf->psk_identity = mbedtls_calloc(1, psk_identity_len);
4517 if (conf->psk_identity == NULL) {
4518 return MBEDTLS_ERR_SSL_ALLOC_FAILED;
4519 }
Paul Bakker6db455e2013-09-18 17:29:31 +02004520
Manuel Pégourié-Gonnard120fdbd2015-05-07 17:07:50 +01004521 conf->psk_identity_len = psk_identity_len;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004522 memcpy(conf->psk_identity, psk_identity, conf->psk_identity_len);
Paul Bakker5ad403f2013-09-18 21:21:30 +02004523
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004524 return 0;
Paul Bakker6db455e2013-09-18 17:29:31 +02004525}
4526
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004527int mbedtls_ssl_conf_psk(mbedtls_ssl_config *conf,
4528 const unsigned char *psk, size_t psk_len,
4529 const unsigned char *psk_identity, size_t psk_identity_len)
Hanno Becker7390c712018-11-15 13:33:04 +00004530{
Janos Follath865b3eb2019-12-16 11:46:15 +00004531 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Hanno Becker7390c712018-11-15 13:33:04 +00004532 /* Remove opaque/raw PSK + PSK Identity */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004533 ssl_conf_remove_psk(conf);
Hanno Becker7390c712018-11-15 13:33:04 +00004534
4535 /* Check and set raw PSK */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004536 if (psk == NULL) {
4537 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
4538 }
4539 if (psk_len == 0) {
4540 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
4541 }
4542 if (psk_len > MBEDTLS_PSK_MAX_LEN) {
4543 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
4544 }
Piotr Nowicki9926eaf2019-11-20 14:54:36 +01004545
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004546 if ((conf->psk = mbedtls_calloc(1, psk_len)) == NULL) {
4547 return MBEDTLS_ERR_SSL_ALLOC_FAILED;
4548 }
Hanno Becker7390c712018-11-15 13:33:04 +00004549 conf->psk_len = psk_len;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004550 memcpy(conf->psk, psk, conf->psk_len);
Hanno Becker7390c712018-11-15 13:33:04 +00004551
4552 /* Check and set PSK Identity */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004553 ret = ssl_conf_set_psk_identity(conf, psk_identity, psk_identity_len);
4554 if (ret != 0) {
4555 ssl_conf_remove_psk(conf);
4556 }
Hanno Becker7390c712018-11-15 13:33:04 +00004557
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004558 return ret;
Hanno Becker7390c712018-11-15 13:33:04 +00004559}
4560
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004561static void ssl_remove_psk(mbedtls_ssl_context *ssl)
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01004562{
4563#if defined(MBEDTLS_USE_PSA_CRYPTO)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004564 if (!mbedtls_svc_key_id_is_null(ssl->handshake->psk_opaque)) {
Ronald Croncf56a0a2020-08-04 09:51:30 +02004565 ssl->handshake->psk_opaque = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004566 } else
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01004567#endif /* MBEDTLS_USE_PSA_CRYPTO */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004568 if (ssl->handshake->psk != NULL) {
4569 mbedtls_platform_zeroize(ssl->handshake->psk,
4570 ssl->handshake->psk_len);
4571 mbedtls_free(ssl->handshake->psk);
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01004572 ssl->handshake->psk_len = 0;
lhuang040a2dd6d2024-06-11 12:31:17 -07004573 ssl->handshake->psk = NULL;
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01004574 }
4575}
4576
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004577int mbedtls_ssl_set_hs_psk(mbedtls_ssl_context *ssl,
4578 const unsigned char *psk, size_t psk_len)
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01004579{
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004580 if (psk == NULL || ssl->handshake == NULL) {
4581 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
4582 }
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01004583
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004584 if (psk_len > MBEDTLS_PSK_MAX_LEN) {
4585 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
4586 }
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01004587
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004588 ssl_remove_psk(ssl);
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01004589
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004590 if ((ssl->handshake->psk = mbedtls_calloc(1, psk_len)) == NULL) {
4591 return MBEDTLS_ERR_SSL_ALLOC_FAILED;
4592 }
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01004593
4594 ssl->handshake->psk_len = psk_len;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004595 memcpy(ssl->handshake->psk, psk, ssl->handshake->psk_len);
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01004596
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004597 return 0;
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01004598}
4599
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01004600#if defined(MBEDTLS_USE_PSA_CRYPTO)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004601int mbedtls_ssl_conf_psk_opaque(mbedtls_ssl_config *conf,
4602 psa_key_id_t psk,
4603 const unsigned char *psk_identity,
4604 size_t psk_identity_len)
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01004605{
Janos Follath865b3eb2019-12-16 11:46:15 +00004606 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Hanno Becker7390c712018-11-15 13:33:04 +00004607 /* Clear opaque/raw PSK + PSK Identity, if present. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004608 ssl_conf_remove_psk(conf);
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01004609
Hanno Becker7390c712018-11-15 13:33:04 +00004610 /* Check and set opaque PSK */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004611 if (mbedtls_svc_key_id_is_null(psk)) {
4612 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
4613 }
Ronald Croncf56a0a2020-08-04 09:51:30 +02004614 conf->psk_opaque = psk;
Hanno Becker7390c712018-11-15 13:33:04 +00004615
4616 /* Check and set PSK Identity */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004617 ret = ssl_conf_set_psk_identity(conf, psk_identity,
4618 psk_identity_len);
4619 if (ret != 0) {
4620 ssl_conf_remove_psk(conf);
4621 }
Hanno Becker7390c712018-11-15 13:33:04 +00004622
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004623 return ret;
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01004624}
4625
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004626int mbedtls_ssl_set_hs_psk_opaque(mbedtls_ssl_context *ssl,
4627 psa_key_id_t psk)
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01004628{
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004629 if ((mbedtls_svc_key_id_is_null(psk)) ||
4630 (ssl->handshake == NULL)) {
4631 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
4632 }
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01004633
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004634 ssl_remove_psk(ssl);
Ronald Croncf56a0a2020-08-04 09:51:30 +02004635 ssl->handshake->psk_opaque = psk;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004636 return 0;
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01004637}
4638#endif /* MBEDTLS_USE_PSA_CRYPTO */
4639
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004640void mbedtls_ssl_conf_psk_cb(mbedtls_ssl_config *conf,
4641 int (*f_psk)(void *, mbedtls_ssl_context *, const unsigned char *,
4642 size_t),
4643 void *p_psk)
Paul Bakker6db455e2013-09-18 17:29:31 +02004644{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02004645 conf->f_psk = f_psk;
4646 conf->p_psk = p_psk;
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02004647}
Gilles Peskineeccd8882020-03-10 12:19:08 +01004648#endif /* MBEDTLS_KEY_EXCHANGE_SOME_PSK_ENABLED */
Paul Bakker43b7e352011-01-18 15:27:19 +00004649
Manuel Pégourié-Gonnardcf141ca2015-05-20 10:35:51 +02004650#if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_SSL_SRV_C)
Hanno Becker470a8c42017-10-04 15:28:46 +01004651
4652#if !defined(MBEDTLS_DEPRECATED_REMOVED)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004653int mbedtls_ssl_conf_dh_param(mbedtls_ssl_config *conf, const char *dhm_P, const char *dhm_G)
Paul Bakker5121ce52009-01-03 21:22:43 +00004654{
Janos Follath865b3eb2019-12-16 11:46:15 +00004655 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker5121ce52009-01-03 21:22:43 +00004656
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004657 if ((ret = mbedtls_mpi_read_string(&conf->dhm_P, 16, dhm_P)) != 0 ||
4658 (ret = mbedtls_mpi_read_string(&conf->dhm_G, 16, dhm_G)) != 0) {
4659 mbedtls_mpi_free(&conf->dhm_P);
4660 mbedtls_mpi_free(&conf->dhm_G);
4661 return ret;
Manuel Pégourié-Gonnard1028b742015-05-06 17:33:07 +01004662 }
Paul Bakker5121ce52009-01-03 21:22:43 +00004663
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004664 return 0;
Paul Bakker5121ce52009-01-03 21:22:43 +00004665}
Hanno Becker470a8c42017-10-04 15:28:46 +01004666#endif /* MBEDTLS_DEPRECATED_REMOVED */
Paul Bakker5121ce52009-01-03 21:22:43 +00004667
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004668int mbedtls_ssl_conf_dh_param_bin(mbedtls_ssl_config *conf,
4669 const unsigned char *dhm_P, size_t P_len,
4670 const unsigned char *dhm_G, size_t G_len)
Hanno Beckera90658f2017-10-04 15:29:08 +01004671{
Janos Follath865b3eb2019-12-16 11:46:15 +00004672 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Hanno Beckera90658f2017-10-04 15:29:08 +01004673
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004674 mbedtls_mpi_free(&conf->dhm_P);
4675 mbedtls_mpi_free(&conf->dhm_G);
Glenn Straussde081ce2021-12-20 01:43:17 -05004676
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004677 if ((ret = mbedtls_mpi_read_binary(&conf->dhm_P, dhm_P, P_len)) != 0 ||
4678 (ret = mbedtls_mpi_read_binary(&conf->dhm_G, dhm_G, G_len)) != 0) {
4679 mbedtls_mpi_free(&conf->dhm_P);
4680 mbedtls_mpi_free(&conf->dhm_G);
4681 return ret;
Hanno Beckera90658f2017-10-04 15:29:08 +01004682 }
4683
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004684 return 0;
Hanno Beckera90658f2017-10-04 15:29:08 +01004685}
Paul Bakker5121ce52009-01-03 21:22:43 +00004686
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004687int mbedtls_ssl_conf_dh_param_ctx(mbedtls_ssl_config *conf, mbedtls_dhm_context *dhm_ctx)
Paul Bakker1b57b062011-01-06 15:48:19 +00004688{
Janos Follath865b3eb2019-12-16 11:46:15 +00004689 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker1b57b062011-01-06 15:48:19 +00004690
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004691 mbedtls_mpi_free(&conf->dhm_P);
4692 mbedtls_mpi_free(&conf->dhm_G);
Glenn Straussde081ce2021-12-20 01:43:17 -05004693
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004694 if ((ret = mbedtls_mpi_copy(&conf->dhm_P, &dhm_ctx->P)) != 0 ||
4695 (ret = mbedtls_mpi_copy(&conf->dhm_G, &dhm_ctx->G)) != 0) {
4696 mbedtls_mpi_free(&conf->dhm_P);
4697 mbedtls_mpi_free(&conf->dhm_G);
4698 return ret;
Manuel Pégourié-Gonnard1028b742015-05-06 17:33:07 +01004699 }
Paul Bakker1b57b062011-01-06 15:48:19 +00004700
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004701 return 0;
Paul Bakker1b57b062011-01-06 15:48:19 +00004702}
Manuel Pégourié-Gonnardcf141ca2015-05-20 10:35:51 +02004703#endif /* MBEDTLS_DHM_C && MBEDTLS_SSL_SRV_C */
Paul Bakker1b57b062011-01-06 15:48:19 +00004704
Manuel Pégourié-Gonnardbd990d62015-06-11 14:49:42 +02004705#if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_SSL_CLI_C)
4706/*
4707 * Set the minimum length for Diffie-Hellman parameters
4708 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004709void mbedtls_ssl_conf_dhm_min_bitlen(mbedtls_ssl_config *conf,
4710 unsigned int bitlen)
Manuel Pégourié-Gonnardbd990d62015-06-11 14:49:42 +02004711{
4712 conf->dhm_min_bitlen = bitlen;
4713}
4714#endif /* MBEDTLS_DHM_C && MBEDTLS_SSL_CLI_C */
4715
Gilles Peskineeccd8882020-03-10 12:19:08 +01004716#if defined(MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED)
Manuel Pégourié-Gonnard36a8b572015-06-17 12:43:26 +02004717/*
4718 * Set allowed/preferred hashes for handshake signatures
4719 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004720void mbedtls_ssl_conf_sig_hashes(mbedtls_ssl_config *conf,
4721 const int *hashes)
Manuel Pégourié-Gonnard36a8b572015-06-17 12:43:26 +02004722{
4723 conf->sig_hashes = hashes;
4724}
Gilles Peskineeccd8882020-03-10 12:19:08 +01004725#endif /* MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED */
Manuel Pégourié-Gonnard36a8b572015-06-17 12:43:26 +02004726
Manuel Pégourié-Gonnardb541da62015-06-17 11:43:30 +02004727#if defined(MBEDTLS_ECP_C)
Manuel Pégourié-Gonnard7f38ed02014-02-04 15:52:33 +01004728/*
4729 * Set the allowed elliptic curves
4730 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004731void mbedtls_ssl_conf_curves(mbedtls_ssl_config *conf,
4732 const mbedtls_ecp_group_id *curve_list)
Manuel Pégourié-Gonnard7f38ed02014-02-04 15:52:33 +01004733{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02004734 conf->curve_list = curve_list;
Manuel Pégourié-Gonnard7f38ed02014-02-04 15:52:33 +01004735}
Hanno Becker947194e2017-04-07 13:25:49 +01004736#endif /* MBEDTLS_ECP_C */
Manuel Pégourié-Gonnard7f38ed02014-02-04 15:52:33 +01004737
Manuel Pégourié-Gonnardbc2b7712015-05-06 11:14:19 +01004738#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004739void mbedtls_ssl_conf_sni(mbedtls_ssl_config *conf,
4740 int (*f_sni)(void *, mbedtls_ssl_context *,
4741 const unsigned char *, size_t),
4742 void *p_sni)
Paul Bakker5701cdc2012-09-27 21:49:42 +00004743{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02004744 conf->f_sni = f_sni;
4745 conf->p_sni = p_sni;
Paul Bakker5701cdc2012-09-27 21:49:42 +00004746}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004747#endif /* MBEDTLS_SSL_SERVER_NAME_INDICATION */
Paul Bakker5701cdc2012-09-27 21:49:42 +00004748
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004749#if defined(MBEDTLS_SSL_ALPN)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004750int mbedtls_ssl_conf_alpn_protocols(mbedtls_ssl_config *conf, const char **protos)
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02004751{
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +02004752 size_t cur_len, tot_len;
4753 const char **p;
4754
4755 /*
Brian J Murray1903fb32016-11-06 04:45:15 -08004756 * RFC 7301 3.1: "Empty strings MUST NOT be included and byte strings
4757 * MUST NOT be truncated."
4758 * We check lengths now rather than later.
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +02004759 */
4760 tot_len = 0;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004761 for (p = protos; *p != NULL; p++) {
4762 cur_len = strlen(*p);
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +02004763 tot_len += cur_len;
4764
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004765 if ((cur_len == 0) ||
4766 (cur_len > MBEDTLS_SSL_MAX_ALPN_NAME_LEN) ||
4767 (tot_len > MBEDTLS_SSL_MAX_ALPN_LIST_LEN)) {
4768 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
4769 }
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +02004770 }
4771
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02004772 conf->alpn_list = protos;
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +02004773
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004774 return 0;
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02004775}
4776
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004777const char *mbedtls_ssl_get_alpn_protocol(const mbedtls_ssl_context *ssl)
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02004778{
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004779 return ssl->alpn_chosen;
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02004780}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004781#endif /* MBEDTLS_SSL_ALPN */
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02004782
Johan Pascalb62bb512015-12-03 21:56:45 +01004783#if defined(MBEDTLS_SSL_DTLS_SRTP)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004784void mbedtls_ssl_conf_srtp_mki_value_supported(mbedtls_ssl_config *conf,
4785 int support_mki_value)
Ron Eldor591f1622018-01-22 12:30:04 +02004786{
4787 conf->dtls_srtp_mki_support = support_mki_value;
4788}
4789
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004790int mbedtls_ssl_dtls_srtp_set_mki_value(mbedtls_ssl_context *ssl,
4791 unsigned char *mki_value,
4792 uint16_t mki_len)
Ron Eldor591f1622018-01-22 12:30:04 +02004793{
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004794 if (mki_len > MBEDTLS_TLS_SRTP_MAX_MKI_LENGTH) {
4795 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
Ron Eldora9788042018-12-05 11:04:31 +02004796 }
Ron Eldor591f1622018-01-22 12:30:04 +02004797
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004798 if (ssl->conf->dtls_srtp_mki_support == MBEDTLS_SSL_DTLS_SRTP_MKI_UNSUPPORTED) {
4799 return MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
Ron Eldora9788042018-12-05 11:04:31 +02004800 }
Ron Eldor591f1622018-01-22 12:30:04 +02004801
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004802 memcpy(ssl->dtls_srtp_info.mki_value, mki_value, mki_len);
Ron Eldor591f1622018-01-22 12:30:04 +02004803 ssl->dtls_srtp_info.mki_len = mki_len;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004804 return 0;
Ron Eldor591f1622018-01-22 12:30:04 +02004805}
4806
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004807int mbedtls_ssl_conf_dtls_srtp_protection_profiles(mbedtls_ssl_config *conf,
4808 const mbedtls_ssl_srtp_profile *profiles)
Johan Pascalb62bb512015-12-03 21:56:45 +01004809{
Johan Pascal253d0262020-09-22 13:04:45 +02004810 const mbedtls_ssl_srtp_profile *p;
4811 size_t list_size = 0;
Johan Pascalb62bb512015-12-03 21:56:45 +01004812
Johan Pascal253d0262020-09-22 13:04:45 +02004813 /* check the profiles list: all entry must be valid,
4814 * its size cannot be more than the total number of supported profiles, currently 4 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004815 for (p = profiles; *p != MBEDTLS_TLS_SRTP_UNSET &&
4816 list_size <= MBEDTLS_TLS_SRTP_MAX_PROFILE_LIST_LENGTH;
4817 p++) {
4818 if (mbedtls_ssl_check_srtp_profile_value(*p) != MBEDTLS_TLS_SRTP_UNSET) {
Johan Pascal76fdf1d2020-10-22 23:31:00 +02004819 list_size++;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004820 } else {
Johan Pascal76fdf1d2020-10-22 23:31:00 +02004821 /* unsupported value, stop parsing and set the size to an error value */
4822 list_size = MBEDTLS_TLS_SRTP_MAX_PROFILE_LIST_LENGTH + 1;
Johan Pascalb62bb512015-12-03 21:56:45 +01004823 }
4824 }
4825
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004826 if (list_size > MBEDTLS_TLS_SRTP_MAX_PROFILE_LIST_LENGTH) {
4827 conf->dtls_srtp_profile_list = NULL;
4828 conf->dtls_srtp_profile_list_len = 0;
4829 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
Johan Pascal253d0262020-09-22 13:04:45 +02004830 }
4831
Johan Pascal9bc97ca2020-09-21 23:44:45 +02004832 conf->dtls_srtp_profile_list = profiles;
Johan Pascal253d0262020-09-22 13:04:45 +02004833 conf->dtls_srtp_profile_list_len = list_size;
Johan Pascalb62bb512015-12-03 21:56:45 +01004834
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004835 return 0;
Johan Pascalb62bb512015-12-03 21:56:45 +01004836}
4837
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004838void mbedtls_ssl_get_dtls_srtp_negotiation_result(const mbedtls_ssl_context *ssl,
4839 mbedtls_dtls_srtp_info *dtls_srtp_info)
Johan Pascalb62bb512015-12-03 21:56:45 +01004840{
Johan Pascal2258a4f2020-10-28 13:53:09 +01004841 dtls_srtp_info->chosen_dtls_srtp_profile = ssl->dtls_srtp_info.chosen_dtls_srtp_profile;
4842 /* do not copy the mki value if there is no chosen profile */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004843 if (dtls_srtp_info->chosen_dtls_srtp_profile == MBEDTLS_TLS_SRTP_UNSET) {
Johan Pascal2258a4f2020-10-28 13:53:09 +01004844 dtls_srtp_info->mki_len = 0;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004845 } else {
Johan Pascal2258a4f2020-10-28 13:53:09 +01004846 dtls_srtp_info->mki_len = ssl->dtls_srtp_info.mki_len;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004847 memcpy(dtls_srtp_info->mki_value, ssl->dtls_srtp_info.mki_value,
4848 ssl->dtls_srtp_info.mki_len);
Johan Pascal2258a4f2020-10-28 13:53:09 +01004849 }
Johan Pascalb62bb512015-12-03 21:56:45 +01004850}
Johan Pascalb62bb512015-12-03 21:56:45 +01004851#endif /* MBEDTLS_SSL_DTLS_SRTP */
4852
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004853void mbedtls_ssl_conf_max_version(mbedtls_ssl_config *conf, int major, int minor)
Paul Bakker490ecc82011-10-06 13:04:09 +00004854{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02004855 conf->max_major_ver = major;
4856 conf->max_minor_ver = minor;
Paul Bakker490ecc82011-10-06 13:04:09 +00004857}
4858
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004859void mbedtls_ssl_conf_min_version(mbedtls_ssl_config *conf, int major, int minor)
Paul Bakker1d29fb52012-09-28 13:28:45 +00004860{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02004861 conf->min_major_ver = major;
4862 conf->min_minor_ver = minor;
Paul Bakker1d29fb52012-09-28 13:28:45 +00004863}
4864
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004865#if defined(MBEDTLS_SSL_FALLBACK_SCSV) && defined(MBEDTLS_SSL_CLI_C)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004866void mbedtls_ssl_conf_fallback(mbedtls_ssl_config *conf, char fallback)
Manuel Pégourié-Gonnard1cbd39d2014-10-20 13:34:59 +02004867{
Manuel Pégourié-Gonnard684b0592015-05-06 09:27:31 +01004868 conf->fallback = fallback;
Manuel Pégourié-Gonnard1cbd39d2014-10-20 13:34:59 +02004869}
4870#endif
4871
Janos Follath088ce432017-04-10 12:42:31 +01004872#if defined(MBEDTLS_SSL_SRV_C)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004873void mbedtls_ssl_conf_cert_req_ca_list(mbedtls_ssl_config *conf,
4874 char cert_req_ca_list)
Janos Follath088ce432017-04-10 12:42:31 +01004875{
4876 conf->cert_req_ca_list = cert_req_ca_list;
4877}
4878#endif
4879
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004880#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004881void mbedtls_ssl_conf_encrypt_then_mac(mbedtls_ssl_config *conf, char etm)
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +01004882{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02004883 conf->encrypt_then_mac = etm;
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +01004884}
4885#endif
4886
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004887#if defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004888void mbedtls_ssl_conf_extended_master_secret(mbedtls_ssl_config *conf, char ems)
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +02004889{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02004890 conf->extended_ms = ems;
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +02004891}
4892#endif
4893
Manuel Pégourié-Gonnard66dc5552015-05-14 12:28:21 +02004894#if defined(MBEDTLS_ARC4_C)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004895void mbedtls_ssl_conf_arc4_support(mbedtls_ssl_config *conf, char arc4)
Manuel Pégourié-Gonnardbd47a582015-01-12 13:43:29 +01004896{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02004897 conf->arc4_disabled = arc4;
Manuel Pégourié-Gonnardbd47a582015-01-12 13:43:29 +01004898}
Manuel Pégourié-Gonnard66dc5552015-05-14 12:28:21 +02004899#endif
Manuel Pégourié-Gonnardbd47a582015-01-12 13:43:29 +01004900
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004901#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004902int mbedtls_ssl_conf_max_frag_len(mbedtls_ssl_config *conf, unsigned char mfl_code)
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02004903{
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004904 if (mfl_code >= MBEDTLS_SSL_MAX_FRAG_LEN_INVALID ||
4905 ssl_mfl_code_to_length(mfl_code) > MBEDTLS_TLS_EXT_ADV_CONTENT_LEN) {
4906 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02004907 }
4908
Manuel Pégourié-Gonnard6bf89d62015-05-05 17:01:57 +01004909 conf->mfl_code = mfl_code;
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02004910
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004911 return 0;
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02004912}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004913#endif /* MBEDTLS_SSL_MAX_FRAGMENT_LENGTH */
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02004914
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004915#if defined(MBEDTLS_SSL_TRUNCATED_HMAC)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004916void mbedtls_ssl_conf_truncated_hmac(mbedtls_ssl_config *conf, int truncate)
Manuel Pégourié-Gonnarde980a992013-07-19 11:08:52 +02004917{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02004918 conf->trunc_hmac = truncate;
Manuel Pégourié-Gonnarde980a992013-07-19 11:08:52 +02004919}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004920#endif /* MBEDTLS_SSL_TRUNCATED_HMAC */
Manuel Pégourié-Gonnarde980a992013-07-19 11:08:52 +02004921
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004922#if defined(MBEDTLS_SSL_CBC_RECORD_SPLITTING)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004923void mbedtls_ssl_conf_cbc_record_splitting(mbedtls_ssl_config *conf, char split)
Manuel Pégourié-Gonnardcfa477e2015-01-07 14:50:54 +01004924{
Manuel Pégourié-Gonnard17eab2b2015-05-05 16:34:53 +01004925 conf->cbc_record_splitting = split;
Manuel Pégourié-Gonnardcfa477e2015-01-07 14:50:54 +01004926}
4927#endif
4928
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004929void mbedtls_ssl_conf_legacy_renegotiation(mbedtls_ssl_config *conf, int allow_legacy)
Paul Bakker48916f92012-09-16 19:57:18 +00004930{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02004931 conf->allow_legacy_renegotiation = allow_legacy;
Paul Bakker48916f92012-09-16 19:57:18 +00004932}
4933
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004934#if defined(MBEDTLS_SSL_RENEGOTIATION)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004935void mbedtls_ssl_conf_renegotiation(mbedtls_ssl_config *conf, int renegotiation)
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01004936{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02004937 conf->disable_renegotiation = renegotiation;
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01004938}
4939
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004940void mbedtls_ssl_conf_renegotiation_enforced(mbedtls_ssl_config *conf, int max_records)
Manuel Pégourié-Gonnarda9964db2014-07-03 19:29:16 +02004941{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02004942 conf->renego_max_records = max_records;
Manuel Pégourié-Gonnarda9964db2014-07-03 19:29:16 +02004943}
4944
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004945void mbedtls_ssl_conf_renegotiation_period(mbedtls_ssl_config *conf,
4946 const unsigned char period[8])
Manuel Pégourié-Gonnard837f0fe2014-11-05 13:58:53 +01004947{
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004948 memcpy(conf->renego_period, period, 8);
Manuel Pégourié-Gonnard837f0fe2014-11-05 13:58:53 +01004949}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004950#endif /* MBEDTLS_SSL_RENEGOTIATION */
Paul Bakker5121ce52009-01-03 21:22:43 +00004951
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004952#if defined(MBEDTLS_SSL_SESSION_TICKETS)
Manuel Pégourié-Gonnardb596abf2015-05-20 10:45:29 +02004953#if defined(MBEDTLS_SSL_CLI_C)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004954void mbedtls_ssl_conf_session_tickets(mbedtls_ssl_config *conf, int use_tickets)
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +02004955{
Manuel Pégourié-Gonnard2b494452015-05-06 10:05:11 +01004956 conf->session_tickets = use_tickets;
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +02004957}
Manuel Pégourié-Gonnardb596abf2015-05-20 10:45:29 +02004958#endif
Paul Bakker606b4ba2013-08-14 16:52:14 +02004959
Manuel Pégourié-Gonnardb596abf2015-05-20 10:45:29 +02004960#if defined(MBEDTLS_SSL_SRV_C)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004961void mbedtls_ssl_conf_session_tickets_cb(mbedtls_ssl_config *conf,
4962 mbedtls_ssl_ticket_write_t *f_ticket_write,
4963 mbedtls_ssl_ticket_parse_t *f_ticket_parse,
4964 void *p_ticket)
Paul Bakker606b4ba2013-08-14 16:52:14 +02004965{
Manuel Pégourié-Gonnardd59675d2015-05-19 15:28:00 +02004966 conf->f_ticket_write = f_ticket_write;
4967 conf->f_ticket_parse = f_ticket_parse;
4968 conf->p_ticket = p_ticket;
Paul Bakker606b4ba2013-08-14 16:52:14 +02004969}
Manuel Pégourié-Gonnardb596abf2015-05-20 10:45:29 +02004970#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004971#endif /* MBEDTLS_SSL_SESSION_TICKETS */
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +02004972
Robert Cragie4feb7ae2015-10-02 13:33:37 +01004973#if defined(MBEDTLS_SSL_EXPORT_KEYS)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004974void mbedtls_ssl_conf_export_keys_cb(mbedtls_ssl_config *conf,
4975 mbedtls_ssl_export_keys_t *f_export_keys,
4976 void *p_export_keys)
Robert Cragie4feb7ae2015-10-02 13:33:37 +01004977{
4978 conf->f_export_keys = f_export_keys;
4979 conf->p_export_keys = p_export_keys;
4980}
Ron Eldorf5cc10d2019-05-07 18:33:40 +03004981
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004982void mbedtls_ssl_conf_export_keys_ext_cb(mbedtls_ssl_config *conf,
4983 mbedtls_ssl_export_keys_ext_t *f_export_keys_ext,
4984 void *p_export_keys)
Ron Eldorf5cc10d2019-05-07 18:33:40 +03004985{
4986 conf->f_export_keys_ext = f_export_keys_ext;
4987 conf->p_export_keys = p_export_keys;
4988}
Robert Cragie4feb7ae2015-10-02 13:33:37 +01004989#endif
4990
Gilles Peskineb74a1c72018-04-24 13:09:22 +02004991#if defined(MBEDTLS_SSL_ASYNC_PRIVATE)
Gilles Peskine8bf79f62018-01-05 21:11:53 +01004992void mbedtls_ssl_conf_async_private_cb(
4993 mbedtls_ssl_config *conf,
4994 mbedtls_ssl_async_sign_t *f_async_sign,
4995 mbedtls_ssl_async_decrypt_t *f_async_decrypt,
4996 mbedtls_ssl_async_resume_t *f_async_resume,
4997 mbedtls_ssl_async_cancel_t *f_async_cancel,
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004998 void *async_config_data)
Gilles Peskine8bf79f62018-01-05 21:11:53 +01004999{
5000 conf->f_async_sign_start = f_async_sign;
5001 conf->f_async_decrypt_start = f_async_decrypt;
5002 conf->f_async_resume = f_async_resume;
5003 conf->f_async_cancel = f_async_cancel;
Gilles Peskinedf13d5c2018-04-25 20:39:48 +02005004 conf->p_async_config_data = async_config_data;
5005}
5006
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005007void *mbedtls_ssl_conf_get_async_config_data(const mbedtls_ssl_config *conf)
Gilles Peskine8f97af72018-04-26 11:46:10 +02005008{
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005009 return conf->p_async_config_data;
Gilles Peskine8f97af72018-04-26 11:46:10 +02005010}
5011
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005012void *mbedtls_ssl_get_async_operation_data(const mbedtls_ssl_context *ssl)
Gilles Peskinedf13d5c2018-04-25 20:39:48 +02005013{
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005014 if (ssl->handshake == NULL) {
5015 return NULL;
5016 } else {
5017 return ssl->handshake->user_async_ctx;
5018 }
Gilles Peskinedf13d5c2018-04-25 20:39:48 +02005019}
5020
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005021void mbedtls_ssl_set_async_operation_data(mbedtls_ssl_context *ssl,
5022 void *ctx)
Gilles Peskinedf13d5c2018-04-25 20:39:48 +02005023{
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005024 if (ssl->handshake != NULL) {
Gilles Peskinedf13d5c2018-04-25 20:39:48 +02005025 ssl->handshake->user_async_ctx = ctx;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005026 }
Gilles Peskine8bf79f62018-01-05 21:11:53 +01005027}
Gilles Peskineb74a1c72018-04-24 13:09:22 +02005028#endif /* MBEDTLS_SSL_ASYNC_PRIVATE */
Gilles Peskine8bf79f62018-01-05 21:11:53 +01005029
Paul Bakker5121ce52009-01-03 21:22:43 +00005030/*
5031 * SSL get accessors
5032 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005033uint32_t mbedtls_ssl_get_verify_result(const mbedtls_ssl_context *ssl)
Paul Bakker5121ce52009-01-03 21:22:43 +00005034{
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005035 if (ssl->session != NULL) {
5036 return ssl->session->verify_result;
5037 }
Manuel Pégourié-Gonnarde89163c2015-01-23 14:30:57 +00005038
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005039 if (ssl->session_negotiate != NULL) {
5040 return ssl->session_negotiate->verify_result;
5041 }
Manuel Pégourié-Gonnarde89163c2015-01-23 14:30:57 +00005042
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005043 return 0xFFFFFFFF;
Paul Bakker5121ce52009-01-03 21:22:43 +00005044}
5045
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005046const char *mbedtls_ssl_get_ciphersuite(const mbedtls_ssl_context *ssl)
Paul Bakker72f62662011-01-16 21:27:44 +00005047{
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005048 if (ssl == NULL || ssl->session == NULL) {
5049 return NULL;
5050 }
Paul Bakker926c8e42013-03-06 10:23:34 +01005051
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005052 return mbedtls_ssl_get_ciphersuite_name(ssl->session->ciphersuite);
Paul Bakker72f62662011-01-16 21:27:44 +00005053}
5054
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005055const char *mbedtls_ssl_get_version(const mbedtls_ssl_context *ssl)
Paul Bakker43ca69c2011-01-15 17:35:19 +00005056{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005057#if defined(MBEDTLS_SSL_PROTO_DTLS)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005058 if (ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM) {
5059 switch (ssl->minor_ver) {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005060 case MBEDTLS_SSL_MINOR_VERSION_2:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005061 return "DTLSv1.0";
Manuel Pégourié-Gonnardb21ca2a2014-02-10 13:43:33 +01005062
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005063 case MBEDTLS_SSL_MINOR_VERSION_3:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005064 return "DTLSv1.2";
Manuel Pégourié-Gonnardb21ca2a2014-02-10 13:43:33 +01005065
5066 default:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005067 return "unknown (DTLS)";
Manuel Pégourié-Gonnardb21ca2a2014-02-10 13:43:33 +01005068 }
5069 }
5070#endif
5071
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005072 switch (ssl->minor_ver) {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005073 case MBEDTLS_SSL_MINOR_VERSION_0:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005074 return "SSLv3.0";
Paul Bakker43ca69c2011-01-15 17:35:19 +00005075
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005076 case MBEDTLS_SSL_MINOR_VERSION_1:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005077 return "TLSv1.0";
Paul Bakker43ca69c2011-01-15 17:35:19 +00005078
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005079 case MBEDTLS_SSL_MINOR_VERSION_2:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005080 return "TLSv1.1";
Paul Bakker43ca69c2011-01-15 17:35:19 +00005081
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005082 case MBEDTLS_SSL_MINOR_VERSION_3:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005083 return "TLSv1.2";
Paul Bakker1ef83d62012-04-11 12:09:53 +00005084
Paul Bakker43ca69c2011-01-15 17:35:19 +00005085 default:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005086 return "unknown";
Paul Bakker43ca69c2011-01-15 17:35:19 +00005087 }
Paul Bakker43ca69c2011-01-15 17:35:19 +00005088}
5089
Manuel Pégourié-Gonnarda2cda6b2015-08-31 18:30:52 +02005090#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005091size_t mbedtls_ssl_get_input_max_frag_len(const mbedtls_ssl_context *ssl)
Andrzej Kurek90c6e842020-04-03 05:25:29 -04005092{
5093 size_t max_len = MBEDTLS_SSL_MAX_CONTENT_LEN;
5094 size_t read_mfl;
5095
5096 /* Use the configured MFL for the client if we're past SERVER_HELLO_DONE */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005097 if (ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT &&
5098 ssl->state >= MBEDTLS_SSL_SERVER_HELLO_DONE) {
5099 return ssl_mfl_code_to_length(ssl->conf->mfl_code);
Andrzej Kurek90c6e842020-04-03 05:25:29 -04005100 }
5101
5102 /* Check if a smaller max length was negotiated */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005103 if (ssl->session_out != NULL) {
5104 read_mfl = ssl_mfl_code_to_length(ssl->session_out->mfl_code);
5105 if (read_mfl < max_len) {
Andrzej Kurek90c6e842020-04-03 05:25:29 -04005106 max_len = read_mfl;
5107 }
5108 }
5109
5110 // During a handshake, use the value being negotiated
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005111 if (ssl->session_negotiate != NULL) {
5112 read_mfl = ssl_mfl_code_to_length(ssl->session_negotiate->mfl_code);
5113 if (read_mfl < max_len) {
Andrzej Kurek90c6e842020-04-03 05:25:29 -04005114 max_len = read_mfl;
5115 }
5116 }
5117
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005118 return max_len;
Andrzej Kurek90c6e842020-04-03 05:25:29 -04005119}
5120
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005121size_t mbedtls_ssl_get_output_max_frag_len(const mbedtls_ssl_context *ssl)
Manuel Pégourié-Gonnarda2cda6b2015-08-31 18:30:52 +02005122{
5123 size_t max_len;
5124
5125 /*
5126 * Assume mfl_code is correct since it was checked when set
5127 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005128 max_len = ssl_mfl_code_to_length(ssl->conf->mfl_code);
Manuel Pégourié-Gonnarda2cda6b2015-08-31 18:30:52 +02005129
Manuel Pégourié-Gonnard2cb17e22017-09-19 13:00:47 +02005130 /* Check if a smaller max length was negotiated */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005131 if (ssl->session_out != NULL &&
5132 ssl_mfl_code_to_length(ssl->session_out->mfl_code) < max_len) {
5133 max_len = ssl_mfl_code_to_length(ssl->session_out->mfl_code);
Manuel Pégourié-Gonnarda2cda6b2015-08-31 18:30:52 +02005134 }
5135
Manuel Pégourié-Gonnard2cb17e22017-09-19 13:00:47 +02005136 /* During a handshake, use the value being negotiated */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005137 if (ssl->session_negotiate != NULL &&
5138 ssl_mfl_code_to_length(ssl->session_negotiate->mfl_code) < max_len) {
5139 max_len = ssl_mfl_code_to_length(ssl->session_negotiate->mfl_code);
Manuel Pégourié-Gonnard2cb17e22017-09-19 13:00:47 +02005140 }
5141
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005142 return max_len;
Manuel Pégourié-Gonnarda2cda6b2015-08-31 18:30:52 +02005143}
Andrzej Kurek90c6e842020-04-03 05:25:29 -04005144
5145#if !defined(MBEDTLS_DEPRECATED_REMOVED)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005146size_t mbedtls_ssl_get_max_frag_len(const mbedtls_ssl_context *ssl)
Andrzej Kurek90c6e842020-04-03 05:25:29 -04005147{
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005148 return mbedtls_ssl_get_output_max_frag_len(ssl);
Andrzej Kurek90c6e842020-04-03 05:25:29 -04005149}
5150#endif /* !MBEDTLS_DEPRECATED_REMOVED */
Manuel Pégourié-Gonnarda2cda6b2015-08-31 18:30:52 +02005151#endif /* MBEDTLS_SSL_MAX_FRAGMENT_LENGTH */
5152
Manuel Pégourié-Gonnardb8eec192018-08-20 09:34:02 +02005153#if defined(MBEDTLS_SSL_PROTO_DTLS)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005154size_t mbedtls_ssl_get_current_mtu(const mbedtls_ssl_context *ssl)
Manuel Pégourié-Gonnardb8eec192018-08-20 09:34:02 +02005155{
Andrzej Kurekef43ce62018-10-09 08:24:12 -04005156 /* Return unlimited mtu for client hello messages to avoid fragmentation. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005157 if (ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT &&
5158 (ssl->state == MBEDTLS_SSL_CLIENT_HELLO ||
5159 ssl->state == MBEDTLS_SSL_SERVER_HELLO)) {
5160 return 0;
5161 }
Andrzej Kurekef43ce62018-10-09 08:24:12 -04005162
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005163 if (ssl->handshake == NULL || ssl->handshake->mtu == 0) {
5164 return ssl->mtu;
5165 }
Manuel Pégourié-Gonnardb8eec192018-08-20 09:34:02 +02005166
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005167 if (ssl->mtu == 0) {
5168 return ssl->handshake->mtu;
5169 }
Manuel Pégourié-Gonnardb8eec192018-08-20 09:34:02 +02005170
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005171 return ssl->mtu < ssl->handshake->mtu ?
5172 ssl->mtu : ssl->handshake->mtu;
Manuel Pégourié-Gonnardb8eec192018-08-20 09:34:02 +02005173}
5174#endif /* MBEDTLS_SSL_PROTO_DTLS */
5175
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005176int mbedtls_ssl_get_max_out_record_payload(const mbedtls_ssl_context *ssl)
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02005177{
5178 size_t max_len = MBEDTLS_SSL_OUT_CONTENT_LEN;
5179
Manuel Pégourié-Gonnard000281e2018-08-21 11:20:58 +02005180#if !defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH) && \
5181 !defined(MBEDTLS_SSL_PROTO_DTLS)
5182 (void) ssl;
5183#endif
5184
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02005185#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005186 const size_t mfl = mbedtls_ssl_get_output_max_frag_len(ssl);
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02005187
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005188 if (max_len > mfl) {
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02005189 max_len = mfl;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005190 }
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02005191#endif
5192
5193#if defined(MBEDTLS_SSL_PROTO_DTLS)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005194 if (mbedtls_ssl_get_current_mtu(ssl) != 0) {
5195 const size_t mtu = mbedtls_ssl_get_current_mtu(ssl);
5196 const int ret = mbedtls_ssl_get_record_expansion(ssl);
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02005197 const size_t overhead = (size_t) ret;
5198
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005199 if (ret < 0) {
5200 return ret;
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02005201 }
5202
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005203 if (mtu <= overhead) {
5204 MBEDTLS_SSL_DEBUG_MSG(1, ("MTU too low for record expansion"));
5205 return MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
5206 }
5207
5208 if (max_len > mtu - overhead) {
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02005209 max_len = mtu - overhead;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005210 }
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02005211 }
Manuel Pégourié-Gonnardb8eec192018-08-20 09:34:02 +02005212#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02005213
Hanno Becker0defedb2018-08-10 12:35:02 +01005214#if !defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH) && \
5215 !defined(MBEDTLS_SSL_PROTO_DTLS)
5216 ((void) ssl);
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02005217#endif
5218
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005219 return (int) max_len;
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02005220}
5221
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005222#if defined(MBEDTLS_X509_CRT_PARSE_C)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005223const mbedtls_x509_crt *mbedtls_ssl_get_peer_cert(const mbedtls_ssl_context *ssl)
Paul Bakkerb0550d92012-10-30 07:51:03 +00005224{
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005225 if (ssl == NULL || ssl->session == NULL) {
5226 return NULL;
5227 }
Paul Bakkerb0550d92012-10-30 07:51:03 +00005228
Hanno Beckere6824572019-02-07 13:18:46 +00005229#if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005230 return ssl->session->peer_cert;
Hanno Beckere6824572019-02-07 13:18:46 +00005231#else
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005232 return NULL;
Hanno Beckere6824572019-02-07 13:18:46 +00005233#endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
Paul Bakkerb0550d92012-10-30 07:51:03 +00005234}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005235#endif /* MBEDTLS_X509_CRT_PARSE_C */
Paul Bakkerb0550d92012-10-30 07:51:03 +00005236
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005237#if defined(MBEDTLS_SSL_CLI_C)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005238int mbedtls_ssl_get_session(const mbedtls_ssl_context *ssl,
5239 mbedtls_ssl_session *dst)
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02005240{
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005241 if (ssl == NULL ||
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02005242 dst == NULL ||
5243 ssl->session == NULL ||
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005244 ssl->conf->endpoint != MBEDTLS_SSL_IS_CLIENT) {
5245 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02005246 }
5247
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005248 return mbedtls_ssl_session_copy(dst, ssl->session);
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02005249}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005250#endif /* MBEDTLS_SSL_CLI_C */
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02005251
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005252const mbedtls_ssl_session *mbedtls_ssl_get_session_pointer(const mbedtls_ssl_context *ssl)
Manuel Pégourié-Gonnardb5e4e0a2019-05-20 11:12:28 +02005253{
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005254 if (ssl == NULL) {
5255 return NULL;
5256 }
Manuel Pégourié-Gonnardb5e4e0a2019-05-20 11:12:28 +02005257
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005258 return ssl->session;
Manuel Pégourié-Gonnardb5e4e0a2019-05-20 11:12:28 +02005259}
5260
Paul Bakker5121ce52009-01-03 21:22:43 +00005261/*
Hanno Beckera835da52019-05-16 12:39:07 +01005262 * Define ticket header determining Mbed TLS version
5263 * and structure of the ticket.
5264 */
5265
Hanno Becker94ef3b32019-05-16 12:50:45 +01005266/*
Hanno Becker50b59662019-05-28 14:30:45 +01005267 * Define bitflag determining compile-time settings influencing
5268 * structure of serialized SSL sessions.
Hanno Becker94ef3b32019-05-16 12:50:45 +01005269 */
5270
Hanno Becker50b59662019-05-28 14:30:45 +01005271#if defined(MBEDTLS_HAVE_TIME)
Hanno Becker3e088662019-05-29 11:10:18 +01005272#define SSL_SERIALIZED_SESSION_CONFIG_TIME 1
Hanno Becker50b59662019-05-28 14:30:45 +01005273#else
Hanno Becker3e088662019-05-29 11:10:18 +01005274#define SSL_SERIALIZED_SESSION_CONFIG_TIME 0
Hanno Becker94ef3b32019-05-16 12:50:45 +01005275#endif /* MBEDTLS_HAVE_TIME */
5276
5277#if defined(MBEDTLS_X509_CRT_PARSE_C)
Hanno Becker3e088662019-05-29 11:10:18 +01005278#define SSL_SERIALIZED_SESSION_CONFIG_CRT 1
Hanno Becker94ef3b32019-05-16 12:50:45 +01005279#else
Hanno Becker3e088662019-05-29 11:10:18 +01005280#define SSL_SERIALIZED_SESSION_CONFIG_CRT 0
Hanno Becker94ef3b32019-05-16 12:50:45 +01005281#endif /* MBEDTLS_X509_CRT_PARSE_C */
5282
David Horstmanneb77b6f2024-02-13 17:53:35 +00005283#if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
David Horstmann11def972024-03-01 11:20:32 +00005284#define SSL_SERIALIZED_SESSION_CONFIG_KEEP_PEER_CRT 1
David Horstmanneb77b6f2024-02-13 17:53:35 +00005285#else
David Horstmann11def972024-03-01 11:20:32 +00005286#define SSL_SERIALIZED_SESSION_CONFIG_KEEP_PEER_CRT 0
David Horstmanneb77b6f2024-02-13 17:53:35 +00005287#endif /* MBEDTLS_SSL_SESSION_TICKETS */
5288
Hanno Becker94ef3b32019-05-16 12:50:45 +01005289#if defined(MBEDTLS_SSL_CLI_C) && defined(MBEDTLS_SSL_SESSION_TICKETS)
Hanno Becker3e088662019-05-29 11:10:18 +01005290#define SSL_SERIALIZED_SESSION_CONFIG_CLIENT_TICKET 1
Hanno Becker94ef3b32019-05-16 12:50:45 +01005291#else
Hanno Becker3e088662019-05-29 11:10:18 +01005292#define SSL_SERIALIZED_SESSION_CONFIG_CLIENT_TICKET 0
Hanno Becker94ef3b32019-05-16 12:50:45 +01005293#endif /* MBEDTLS_SSL_CLI_C && MBEDTLS_SSL_SESSION_TICKETS */
5294
5295#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
Hanno Becker3e088662019-05-29 11:10:18 +01005296#define SSL_SERIALIZED_SESSION_CONFIG_MFL 1
Hanno Becker94ef3b32019-05-16 12:50:45 +01005297#else
Hanno Becker3e088662019-05-29 11:10:18 +01005298#define SSL_SERIALIZED_SESSION_CONFIG_MFL 0
Hanno Becker94ef3b32019-05-16 12:50:45 +01005299#endif /* MBEDTLS_SSL_MAX_FRAGMENT_LENGTH */
5300
5301#if defined(MBEDTLS_SSL_TRUNCATED_HMAC)
Hanno Becker3e088662019-05-29 11:10:18 +01005302#define SSL_SERIALIZED_SESSION_CONFIG_TRUNC_HMAC 1
Hanno Becker94ef3b32019-05-16 12:50:45 +01005303#else
Hanno Becker3e088662019-05-29 11:10:18 +01005304#define SSL_SERIALIZED_SESSION_CONFIG_TRUNC_HMAC 0
Hanno Becker94ef3b32019-05-16 12:50:45 +01005305#endif /* MBEDTLS_SSL_TRUNCATED_HMAC */
5306
5307#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
Hanno Becker3e088662019-05-29 11:10:18 +01005308#define SSL_SERIALIZED_SESSION_CONFIG_ETM 1
Hanno Becker94ef3b32019-05-16 12:50:45 +01005309#else
Hanno Becker3e088662019-05-29 11:10:18 +01005310#define SSL_SERIALIZED_SESSION_CONFIG_ETM 0
Hanno Becker94ef3b32019-05-16 12:50:45 +01005311#endif /* MBEDTLS_SSL_ENCRYPT_THEN_MAC */
5312
Hanno Becker94ef3b32019-05-16 12:50:45 +01005313#if defined(MBEDTLS_SSL_SESSION_TICKETS)
5314#define SSL_SERIALIZED_SESSION_CONFIG_TICKET 1
5315#else
5316#define SSL_SERIALIZED_SESSION_CONFIG_TICKET 0
5317#endif /* MBEDTLS_SSL_SESSION_TICKETS */
5318
Hanno Becker3e088662019-05-29 11:10:18 +01005319#define SSL_SERIALIZED_SESSION_CONFIG_TIME_BIT 0
5320#define SSL_SERIALIZED_SESSION_CONFIG_CRT_BIT 1
5321#define SSL_SERIALIZED_SESSION_CONFIG_CLIENT_TICKET_BIT 2
5322#define SSL_SERIALIZED_SESSION_CONFIG_MFL_BIT 3
5323#define SSL_SERIALIZED_SESSION_CONFIG_TRUNC_HMAC_BIT 4
5324#define SSL_SERIALIZED_SESSION_CONFIG_ETM_BIT 5
5325#define SSL_SERIALIZED_SESSION_CONFIG_TICKET_BIT 6
David Horstmann11def972024-03-01 11:20:32 +00005326#define SSL_SERIALIZED_SESSION_CONFIG_KEEP_PEER_CRT_BIT 7
Hanno Becker3e088662019-05-29 11:10:18 +01005327
Hanno Becker50b59662019-05-28 14:30:45 +01005328#define SSL_SERIALIZED_SESSION_CONFIG_BITFLAG \
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005329 ((uint16_t) ( \
5330 (SSL_SERIALIZED_SESSION_CONFIG_TIME << SSL_SERIALIZED_SESSION_CONFIG_TIME_BIT) | \
5331 (SSL_SERIALIZED_SESSION_CONFIG_CRT << SSL_SERIALIZED_SESSION_CONFIG_CRT_BIT) | \
5332 (SSL_SERIALIZED_SESSION_CONFIG_CLIENT_TICKET << \
5333 SSL_SERIALIZED_SESSION_CONFIG_CLIENT_TICKET_BIT) | \
5334 (SSL_SERIALIZED_SESSION_CONFIG_MFL << SSL_SERIALIZED_SESSION_CONFIG_MFL_BIT) | \
5335 (SSL_SERIALIZED_SESSION_CONFIG_TRUNC_HMAC << \
5336 SSL_SERIALIZED_SESSION_CONFIG_TRUNC_HMAC_BIT) | \
5337 (SSL_SERIALIZED_SESSION_CONFIG_ETM << SSL_SERIALIZED_SESSION_CONFIG_ETM_BIT) | \
David Horstmanneb77b6f2024-02-13 17:53:35 +00005338 (SSL_SERIALIZED_SESSION_CONFIG_TICKET << SSL_SERIALIZED_SESSION_CONFIG_TICKET_BIT) | \
David Horstmannf5a6fa22024-03-01 12:31:35 +00005339 (SSL_SERIALIZED_SESSION_CONFIG_KEEP_PEER_CRT << \
5340 SSL_SERIALIZED_SESSION_CONFIG_KEEP_PEER_CRT_BIT)))
Hanno Becker94ef3b32019-05-16 12:50:45 +01005341
Chien Wongb6d57932024-02-07 21:48:12 +08005342static const unsigned char ssl_serialized_session_header[] = {
Hanno Becker94ef3b32019-05-16 12:50:45 +01005343 MBEDTLS_VERSION_MAJOR,
5344 MBEDTLS_VERSION_MINOR,
5345 MBEDTLS_VERSION_PATCH,
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005346 MBEDTLS_BYTE_1(SSL_SERIALIZED_SESSION_CONFIG_BITFLAG),
5347 MBEDTLS_BYTE_0(SSL_SERIALIZED_SESSION_CONFIG_BITFLAG),
Hanno Beckerf8787072019-05-16 12:41:07 +01005348};
Hanno Beckera835da52019-05-16 12:39:07 +01005349
5350/*
Manuel Pégourié-Gonnarda3e7c652019-05-16 10:08:35 +02005351 * Serialize a session in the following format:
Manuel Pégourié-Gonnard35eb8022019-05-16 11:11:08 +02005352 * (in the presentation language of TLS, RFC 8446 section 3)
Manuel Pégourié-Gonnarda3e7c652019-05-16 10:08:35 +02005353 *
Hanno Becker50b59662019-05-28 14:30:45 +01005354 * opaque mbedtls_version[3]; // major, minor, patch
5355 * opaque session_format[2]; // version-specific 16-bit field determining
5356 * // the format of the remaining
5357 * // serialized data.
Hanno Beckerdc28b6c2019-05-29 11:08:00 +01005358 *
5359 * Note: When updating the format, remember to keep
5360 * these version+format bytes.
5361 *
Hanno Beckerbe34e8e2019-06-04 09:43:16 +01005362 * // In this version, `session_format` determines
5363 * // the setting of those compile-time
5364 * // configuration options which influence
Hanno Becker50b59662019-05-28 14:30:45 +01005365 * // the structure of mbedtls_ssl_session.
David Horstmann363db772024-03-01 12:11:24 +00005366 * #if defined(MBEDTLS_HAVE_TIME)
5367 * uint64 start_time;
5368 * #endif
5369 * uint8 ciphersuite[2]; // defined by the standard
5370 * uint8 compression; // 0 or 1
5371 * uint8 session_id_len; // at most 32
5372 * opaque session_id[32];
5373 * opaque master[48]; // fixed length in the standard
5374 * uint32 verify_result;
David Horstmannfc8cacf2024-03-04 16:37:02 +00005375 * #if defined(MBEDTLS_X509_CRT_PARSE_C)
David Horstmann363db772024-03-01 12:11:24 +00005376 * #if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
5377 * opaque peer_cert<0..2^24-1>; // length 0 means no peer cert
5378 * #else
5379 * uint8 peer_cert_digest_type;
5380 * opaque peer_cert_digest<0..2^8-1>
5381 * #endif
David Horstmannfc8cacf2024-03-04 16:37:02 +00005382 * #endif
5383 * #if defined(MBEDTLS_SSL_SESSION_TICKETS) && defined(MBEDTLS_SSL_CLI_C)
David Horstmann363db772024-03-01 12:11:24 +00005384 * opaque ticket<0..2^24-1>; // length 0 means no ticket
5385 * uint32 ticket_lifetime;
5386 * #endif
5387 * #if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
5388 * uint8 mfl_code; // up to 255 according to standard
5389 * #endif
5390 * #if defined(MBEDTLS_SSL_TRUNCATED_HMAC)
5391 * uint8 trunc_hmac; // 0 or 1
5392 * #endif
5393 * #if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
5394 * uint8 encrypt_then_mac; // 0 or 1
5395 * #endif
Manuel Pégourié-Gonnarda3e7c652019-05-16 10:08:35 +02005396 *
Manuel Pégourié-Gonnardf743c032019-05-24 12:06:29 +02005397 * The order is the same as in the definition of the structure, except
5398 * verify_result is put before peer_cert so that all mandatory fields come
5399 * together in one block.
Manuel Pégourié-Gonnarda3e7c652019-05-16 10:08:35 +02005400 */
Manuel Pégourié-Gonnardd904d662022-06-17 10:24:00 +02005401MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005402static int ssl_session_save(const mbedtls_ssl_session *session,
5403 unsigned char omit_header,
5404 unsigned char *buf,
5405 size_t buf_len,
5406 size_t *olen)
Manuel Pégourié-Gonnarda3e7c652019-05-16 10:08:35 +02005407{
5408 unsigned char *p = buf;
Manuel Pégourié-Gonnard26f982f2019-05-21 11:01:32 +02005409 size_t used = 0;
Manuel Pégourié-Gonnardf743c032019-05-24 12:06:29 +02005410#if defined(MBEDTLS_HAVE_TIME)
5411 uint64_t start;
5412#endif
Manuel Pégourié-Gonnarda3e7c652019-05-16 10:08:35 +02005413#if defined(MBEDTLS_X509_CRT_PARSE_C)
5414#if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
5415 size_t cert_len;
Manuel Pégourié-Gonnarda3e7c652019-05-16 10:08:35 +02005416#endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
5417#endif /* MBEDTLS_X509_CRT_PARSE_C */
5418
Manuel Pégourié-Gonnarda3e7c652019-05-16 10:08:35 +02005419
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005420 if (!omit_header) {
Manuel Pégourié-Gonnard45ac1f02019-07-23 16:52:45 +02005421 /*
5422 * Add version identifier
5423 */
5424
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005425 used += sizeof(ssl_serialized_session_header);
Manuel Pégourié-Gonnard45ac1f02019-07-23 16:52:45 +02005426
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005427 if (used <= buf_len) {
5428 memcpy(p, ssl_serialized_session_header,
5429 sizeof(ssl_serialized_session_header));
5430 p += sizeof(ssl_serialized_session_header);
Manuel Pégourié-Gonnard45ac1f02019-07-23 16:52:45 +02005431 }
Hanno Beckera835da52019-05-16 12:39:07 +01005432 }
5433
5434 /*
Manuel Pégourié-Gonnardf743c032019-05-24 12:06:29 +02005435 * Time
5436 */
5437#if defined(MBEDTLS_HAVE_TIME)
5438 used += 8;
5439
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005440 if (used <= buf_len) {
Manuel Pégourié-Gonnardf743c032019-05-24 12:06:29 +02005441 start = (uint64_t) session->start;
5442
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005443 MBEDTLS_PUT_UINT64_BE(start, p, 0);
Joe Subbiani2f98d792021-08-20 11:44:44 +01005444 p += 8;
Manuel Pégourié-Gonnardf743c032019-05-24 12:06:29 +02005445 }
5446#endif /* MBEDTLS_HAVE_TIME */
5447
5448 /*
5449 * Basic mandatory fields
5450 */
5451 used += 2 /* ciphersuite */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005452 + 1 /* compression */
5453 + 1 /* id_len */
5454 + sizeof(session->id)
5455 + sizeof(session->master)
5456 + 4; /* verify_result */
Manuel Pégourié-Gonnardf743c032019-05-24 12:06:29 +02005457
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005458 if (used <= buf_len) {
5459 MBEDTLS_PUT_UINT16_BE(session->ciphersuite, p, 0);
Joe Subbiani2f98d792021-08-20 11:44:44 +01005460 p += 2;
Manuel Pégourié-Gonnardf743c032019-05-24 12:06:29 +02005461
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005462 *p++ = MBEDTLS_BYTE_0(session->compression);
Manuel Pégourié-Gonnardf743c032019-05-24 12:06:29 +02005463
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005464 *p++ = MBEDTLS_BYTE_0(session->id_len);
5465 memcpy(p, session->id, 32);
Manuel Pégourié-Gonnardf743c032019-05-24 12:06:29 +02005466 p += 32;
5467
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005468 memcpy(p, session->master, 48);
Manuel Pégourié-Gonnardf743c032019-05-24 12:06:29 +02005469 p += 48;
5470
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005471 MBEDTLS_PUT_UINT32_BE(session->verify_result, p, 0);
Joe Subbiani2f98d792021-08-20 11:44:44 +01005472 p += 4;
Manuel Pégourié-Gonnard26f982f2019-05-21 11:01:32 +02005473 }
Manuel Pégourié-Gonnarda3e7c652019-05-16 10:08:35 +02005474
Manuel Pégourié-Gonnard35eb8022019-05-16 11:11:08 +02005475 /*
Manuel Pégourié-Gonnardf743c032019-05-24 12:06:29 +02005476 * Peer's end-entity certificate
Manuel Pégourié-Gonnard35eb8022019-05-16 11:11:08 +02005477 */
Manuel Pégourié-Gonnarda3e7c652019-05-16 10:08:35 +02005478#if defined(MBEDTLS_X509_CRT_PARSE_C)
5479#if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005480 if (session->peer_cert == NULL) {
Manuel Pégourié-Gonnarda3e7c652019-05-16 10:08:35 +02005481 cert_len = 0;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005482 } else {
Manuel Pégourié-Gonnarda3e7c652019-05-16 10:08:35 +02005483 cert_len = session->peer_cert->raw.len;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005484 }
Manuel Pégourié-Gonnarda3e7c652019-05-16 10:08:35 +02005485
Manuel Pégourié-Gonnard26f982f2019-05-21 11:01:32 +02005486 used += 3 + cert_len;
Manuel Pégourié-Gonnarda3e7c652019-05-16 10:08:35 +02005487
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005488 if (used <= buf_len) {
5489 *p++ = MBEDTLS_BYTE_2(cert_len);
5490 *p++ = MBEDTLS_BYTE_1(cert_len);
5491 *p++ = MBEDTLS_BYTE_0(cert_len);
Manuel Pégourié-Gonnarda3e7c652019-05-16 10:08:35 +02005492
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005493 if (session->peer_cert != NULL) {
5494 memcpy(p, session->peer_cert->raw.p, cert_len);
Manuel Pégourié-Gonnard26f982f2019-05-21 11:01:32 +02005495 p += cert_len;
5496 }
5497 }
Manuel Pégourié-Gonnarda3e7c652019-05-16 10:08:35 +02005498#else /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005499 if (session->peer_cert_digest != NULL) {
Manuel Pégourié-Gonnardf743c032019-05-24 12:06:29 +02005500 used += 1 /* type */ + 1 /* length */ + session->peer_cert_digest_len;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005501 if (used <= buf_len) {
Manuel Pégourié-Gonnardf743c032019-05-24 12:06:29 +02005502 *p++ = (unsigned char) session->peer_cert_digest_type;
5503 *p++ = (unsigned char) session->peer_cert_digest_len;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005504 memcpy(p, session->peer_cert_digest,
5505 session->peer_cert_digest_len);
Manuel Pégourié-Gonnardf743c032019-05-24 12:06:29 +02005506 p += session->peer_cert_digest_len;
5507 }
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005508 } else {
Manuel Pégourié-Gonnardf743c032019-05-24 12:06:29 +02005509 used += 2;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005510 if (used <= buf_len) {
Manuel Pégourié-Gonnardf743c032019-05-24 12:06:29 +02005511 *p++ = (unsigned char) MBEDTLS_MD_NONE;
5512 *p++ = 0;
5513 }
Manuel Pégourié-Gonnard26f982f2019-05-21 11:01:32 +02005514 }
Manuel Pégourié-Gonnarda3e7c652019-05-16 10:08:35 +02005515#endif /* !MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
5516#endif /* MBEDTLS_X509_CRT_PARSE_C */
5517
Manuel Pégourié-Gonnard35eb8022019-05-16 11:11:08 +02005518 /*
Manuel Pégourié-Gonnardf743c032019-05-24 12:06:29 +02005519 * Session ticket if any, plus associated data
Manuel Pégourié-Gonnard35eb8022019-05-16 11:11:08 +02005520 */
5521#if defined(MBEDTLS_SSL_SESSION_TICKETS) && defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnardf743c032019-05-24 12:06:29 +02005522 used += 3 + session->ticket_len + 4; /* len + ticket + lifetime */
Manuel Pégourié-Gonnard35eb8022019-05-16 11:11:08 +02005523
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005524 if (used <= buf_len) {
5525 *p++ = MBEDTLS_BYTE_2(session->ticket_len);
5526 *p++ = MBEDTLS_BYTE_1(session->ticket_len);
5527 *p++ = MBEDTLS_BYTE_0(session->ticket_len);
Manuel Pégourié-Gonnard26f982f2019-05-21 11:01:32 +02005528
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005529 if (session->ticket != NULL) {
5530 memcpy(p, session->ticket, session->ticket_len);
Manuel Pégourié-Gonnard26f982f2019-05-21 11:01:32 +02005531 p += session->ticket_len;
5532 }
Manuel Pégourié-Gonnardf743c032019-05-24 12:06:29 +02005533
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005534 MBEDTLS_PUT_UINT32_BE(session->ticket_lifetime, p, 0);
Joe Subbiani2f98d792021-08-20 11:44:44 +01005535 p += 4;
Manuel Pégourié-Gonnard35eb8022019-05-16 11:11:08 +02005536 }
5537#endif /* MBEDTLS_SSL_SESSION_TICKETS && MBEDTLS_SSL_CLI_C */
5538
Manuel Pégourié-Gonnardf743c032019-05-24 12:06:29 +02005539 /*
5540 * Misc extension-related info
5541 */
5542#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
5543 used += 1;
5544
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005545 if (used <= buf_len) {
Manuel Pégourié-Gonnardf743c032019-05-24 12:06:29 +02005546 *p++ = session->mfl_code;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005547 }
Manuel Pégourié-Gonnardf743c032019-05-24 12:06:29 +02005548#endif
5549
5550#if defined(MBEDTLS_SSL_TRUNCATED_HMAC)
5551 used += 1;
5552
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005553 if (used <= buf_len) {
5554 *p++ = (unsigned char) ((session->trunc_hmac) & 0xFF);
5555 }
Manuel Pégourié-Gonnardf743c032019-05-24 12:06:29 +02005556#endif
5557
5558#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
5559 used += 1;
5560
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005561 if (used <= buf_len) {
5562 *p++ = MBEDTLS_BYTE_0(session->encrypt_then_mac);
5563 }
Manuel Pégourié-Gonnardf743c032019-05-24 12:06:29 +02005564#endif
5565
Manuel Pégourié-Gonnard35eb8022019-05-16 11:11:08 +02005566 /* Done */
Manuel Pégourié-Gonnard26f982f2019-05-21 11:01:32 +02005567 *olen = used;
5568
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005569 if (used > buf_len) {
5570 return MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL;
5571 }
Manuel Pégourié-Gonnarda3e7c652019-05-16 10:08:35 +02005572
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005573 return 0;
Manuel Pégourié-Gonnarda3e7c652019-05-16 10:08:35 +02005574}
5575
5576/*
Manuel Pégourié-Gonnard45ac1f02019-07-23 16:52:45 +02005577 * Public wrapper for ssl_session_save()
5578 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005579int mbedtls_ssl_session_save(const mbedtls_ssl_session *session,
5580 unsigned char *buf,
5581 size_t buf_len,
5582 size_t *olen)
Manuel Pégourié-Gonnard45ac1f02019-07-23 16:52:45 +02005583{
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005584 return ssl_session_save(session, 0, buf, buf_len, olen);
Manuel Pégourié-Gonnard45ac1f02019-07-23 16:52:45 +02005585}
5586
5587/*
Manuel Pégourié-Gonnardb9dfc9f2019-07-12 10:50:19 +02005588 * Deserialize session, see mbedtls_ssl_session_save() for format.
Manuel Pégourié-Gonnarda3d831b2019-05-23 12:28:45 +02005589 *
5590 * This internal version is wrapped by a public function that cleans up in
Manuel Pégourié-Gonnard45ac1f02019-07-23 16:52:45 +02005591 * case of error, and has an extra option omit_header.
Manuel Pégourié-Gonnarda3e7c652019-05-16 10:08:35 +02005592 */
Manuel Pégourié-Gonnardd904d662022-06-17 10:24:00 +02005593MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005594static int ssl_session_load(mbedtls_ssl_session *session,
5595 unsigned char omit_header,
5596 const unsigned char *buf,
5597 size_t len)
Manuel Pégourié-Gonnarda3e7c652019-05-16 10:08:35 +02005598{
5599 const unsigned char *p = buf;
5600 const unsigned char * const end = buf + len;
Manuel Pégourié-Gonnardf743c032019-05-24 12:06:29 +02005601#if defined(MBEDTLS_HAVE_TIME)
5602 uint64_t start;
5603#endif
Manuel Pégourié-Gonnarda3e7c652019-05-16 10:08:35 +02005604#if defined(MBEDTLS_X509_CRT_PARSE_C)
5605#if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
5606 size_t cert_len;
Manuel Pégourié-Gonnarda3e7c652019-05-16 10:08:35 +02005607#endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
5608#endif /* MBEDTLS_X509_CRT_PARSE_C */
5609
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005610 if (!omit_header) {
Manuel Pégourié-Gonnard45ac1f02019-07-23 16:52:45 +02005611 /*
5612 * Check version identifier
5613 */
5614
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005615 if ((size_t) (end - p) < sizeof(ssl_serialized_session_header)) {
5616 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
Manuel Pégourié-Gonnard45ac1f02019-07-23 16:52:45 +02005617 }
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005618
5619 if (memcmp(p, ssl_serialized_session_header,
5620 sizeof(ssl_serialized_session_header)) != 0) {
5621 return MBEDTLS_ERR_SSL_VERSION_MISMATCH;
5622 }
5623 p += sizeof(ssl_serialized_session_header);
Hanno Beckera835da52019-05-16 12:39:07 +01005624 }
Hanno Beckera835da52019-05-16 12:39:07 +01005625
5626 /*
Manuel Pégourié-Gonnardf743c032019-05-24 12:06:29 +02005627 * Time
Manuel Pégourié-Gonnard35eb8022019-05-16 11:11:08 +02005628 */
Manuel Pégourié-Gonnardf743c032019-05-24 12:06:29 +02005629#if defined(MBEDTLS_HAVE_TIME)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005630 if (8 > (size_t) (end - p)) {
5631 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
5632 }
Manuel Pégourié-Gonnarda3e7c652019-05-16 10:08:35 +02005633
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005634 start = ((uint64_t) p[0] << 56) |
5635 ((uint64_t) p[1] << 48) |
5636 ((uint64_t) p[2] << 40) |
5637 ((uint64_t) p[3] << 32) |
5638 ((uint64_t) p[4] << 24) |
5639 ((uint64_t) p[5] << 16) |
5640 ((uint64_t) p[6] << 8) |
5641 ((uint64_t) p[7]);
Manuel Pégourié-Gonnardf743c032019-05-24 12:06:29 +02005642 p += 8;
Manuel Pégourié-Gonnarda3e7c652019-05-16 10:08:35 +02005643
Manuel Pégourié-Gonnardf743c032019-05-24 12:06:29 +02005644 session->start = (time_t) start;
5645#endif /* MBEDTLS_HAVE_TIME */
5646
5647 /*
5648 * Basic mandatory fields
5649 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005650 if (2 + 1 + 1 + 32 + 48 + 4 > (size_t) (end - p)) {
5651 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
5652 }
Manuel Pégourié-Gonnardf743c032019-05-24 12:06:29 +02005653
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005654 session->ciphersuite = (p[0] << 8) | p[1];
Manuel Pégourié-Gonnardf743c032019-05-24 12:06:29 +02005655 p += 2;
5656
5657 session->compression = *p++;
5658
5659 session->id_len = *p++;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005660 memcpy(session->id, p, 32);
Manuel Pégourié-Gonnardf743c032019-05-24 12:06:29 +02005661 p += 32;
5662
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005663 memcpy(session->master, p, 48);
Manuel Pégourié-Gonnardf743c032019-05-24 12:06:29 +02005664 p += 48;
5665
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005666 session->verify_result = ((uint32_t) p[0] << 24) |
5667 ((uint32_t) p[1] << 16) |
5668 ((uint32_t) p[2] << 8) |
5669 ((uint32_t) p[3]);
Manuel Pégourié-Gonnardf743c032019-05-24 12:06:29 +02005670 p += 4;
5671
5672 /* Immediately clear invalid pointer values that have been read, in case
5673 * we exit early before we replaced them with valid ones. */
Manuel Pégourié-Gonnarda3e7c652019-05-16 10:08:35 +02005674#if defined(MBEDTLS_X509_CRT_PARSE_C)
5675#if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
5676 session->peer_cert = NULL;
5677#else
5678 session->peer_cert_digest = NULL;
5679#endif /* !MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
5680#endif /* MBEDTLS_X509_CRT_PARSE_C */
5681#if defined(MBEDTLS_SSL_SESSION_TICKETS) && defined(MBEDTLS_SSL_CLI_C)
5682 session->ticket = NULL;
5683#endif /* MBEDTLS_SSL_SESSION_TICKETS && MBEDTLS_SSL_CLI_C */
5684
Manuel Pégourié-Gonnard35eb8022019-05-16 11:11:08 +02005685 /*
5686 * Peer certificate
5687 */
Manuel Pégourié-Gonnarda3e7c652019-05-16 10:08:35 +02005688#if defined(MBEDTLS_X509_CRT_PARSE_C)
5689#if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
5690 /* Deserialize CRT from the end of the ticket. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005691 if (3 > (size_t) (end - p)) {
5692 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
5693 }
Manuel Pégourié-Gonnarda3e7c652019-05-16 10:08:35 +02005694
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005695 cert_len = (p[0] << 16) | (p[1] << 8) | p[2];
Manuel Pégourié-Gonnarda3e7c652019-05-16 10:08:35 +02005696 p += 3;
5697
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005698 if (cert_len != 0) {
Janos Follath865b3eb2019-12-16 11:46:15 +00005699 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnarda3e7c652019-05-16 10:08:35 +02005700
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005701 if (cert_len > (size_t) (end - p)) {
5702 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
5703 }
Manuel Pégourié-Gonnarda3e7c652019-05-16 10:08:35 +02005704
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005705 session->peer_cert = mbedtls_calloc(1, sizeof(mbedtls_x509_crt));
Manuel Pégourié-Gonnarda3e7c652019-05-16 10:08:35 +02005706
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005707 if (session->peer_cert == NULL) {
5708 return MBEDTLS_ERR_SSL_ALLOC_FAILED;
5709 }
Manuel Pégourié-Gonnarda3e7c652019-05-16 10:08:35 +02005710
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005711 mbedtls_x509_crt_init(session->peer_cert);
Manuel Pégourié-Gonnarda3e7c652019-05-16 10:08:35 +02005712
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005713 if ((ret = mbedtls_x509_crt_parse_der(session->peer_cert,
5714 p, cert_len)) != 0) {
5715 mbedtls_x509_crt_free(session->peer_cert);
5716 mbedtls_free(session->peer_cert);
Manuel Pégourié-Gonnarda3e7c652019-05-16 10:08:35 +02005717 session->peer_cert = NULL;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005718 return ret;
Manuel Pégourié-Gonnarda3e7c652019-05-16 10:08:35 +02005719 }
5720
5721 p += cert_len;
5722 }
5723#else /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
5724 /* Deserialize CRT digest from the end of the ticket. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005725 if (2 > (size_t) (end - p)) {
5726 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
5727 }
Manuel Pégourié-Gonnarda3e7c652019-05-16 10:08:35 +02005728
Manuel Pégourié-Gonnardf743c032019-05-24 12:06:29 +02005729 session->peer_cert_digest_type = (mbedtls_md_type_t) *p++;
5730 session->peer_cert_digest_len = (size_t) *p++;
Manuel Pégourié-Gonnarda3e7c652019-05-16 10:08:35 +02005731
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005732 if (session->peer_cert_digest_len != 0) {
Manuel Pégourié-Gonnardf743c032019-05-24 12:06:29 +02005733 const mbedtls_md_info_t *md_info =
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005734 mbedtls_md_info_from_type(session->peer_cert_digest_type);
5735 if (md_info == NULL) {
5736 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
5737 }
5738 if (session->peer_cert_digest_len != mbedtls_md_get_size(md_info)) {
5739 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
5740 }
Manuel Pégourié-Gonnarda3e7c652019-05-16 10:08:35 +02005741
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005742 if (session->peer_cert_digest_len > (size_t) (end - p)) {
5743 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
5744 }
Manuel Pégourié-Gonnardf743c032019-05-24 12:06:29 +02005745
5746 session->peer_cert_digest =
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005747 mbedtls_calloc(1, session->peer_cert_digest_len);
5748 if (session->peer_cert_digest == NULL) {
5749 return MBEDTLS_ERR_SSL_ALLOC_FAILED;
5750 }
Manuel Pégourié-Gonnarda3e7c652019-05-16 10:08:35 +02005751
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005752 memcpy(session->peer_cert_digest, p,
5753 session->peer_cert_digest_len);
Manuel Pégourié-Gonnardf743c032019-05-24 12:06:29 +02005754 p += session->peer_cert_digest_len;
Manuel Pégourié-Gonnarda3e7c652019-05-16 10:08:35 +02005755 }
5756#endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
5757#endif /* MBEDTLS_X509_CRT_PARSE_C */
5758
Manuel Pégourié-Gonnard35eb8022019-05-16 11:11:08 +02005759 /*
Manuel Pégourié-Gonnardf743c032019-05-24 12:06:29 +02005760 * Session ticket and associated data
Manuel Pégourié-Gonnard35eb8022019-05-16 11:11:08 +02005761 */
5762#if defined(MBEDTLS_SSL_SESSION_TICKETS) && defined(MBEDTLS_SSL_CLI_C)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005763 if (3 > (size_t) (end - p)) {
5764 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
5765 }
Manuel Pégourié-Gonnard35eb8022019-05-16 11:11:08 +02005766
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005767 session->ticket_len = (p[0] << 16) | (p[1] << 8) | p[2];
Manuel Pégourié-Gonnard35eb8022019-05-16 11:11:08 +02005768 p += 3;
5769
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005770 if (session->ticket_len != 0) {
5771 if (session->ticket_len > (size_t) (end - p)) {
5772 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
5773 }
Manuel Pégourié-Gonnard35eb8022019-05-16 11:11:08 +02005774
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005775 session->ticket = mbedtls_calloc(1, session->ticket_len);
5776 if (session->ticket == NULL) {
5777 return MBEDTLS_ERR_SSL_ALLOC_FAILED;
5778 }
Manuel Pégourié-Gonnard35eb8022019-05-16 11:11:08 +02005779
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005780 memcpy(session->ticket, p, session->ticket_len);
Manuel Pégourié-Gonnard35eb8022019-05-16 11:11:08 +02005781 p += session->ticket_len;
5782 }
Manuel Pégourié-Gonnardf743c032019-05-24 12:06:29 +02005783
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005784 if (4 > (size_t) (end - p)) {
5785 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
5786 }
Manuel Pégourié-Gonnardf743c032019-05-24 12:06:29 +02005787
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005788 session->ticket_lifetime = ((uint32_t) p[0] << 24) |
5789 ((uint32_t) p[1] << 16) |
5790 ((uint32_t) p[2] << 8) |
5791 ((uint32_t) p[3]);
Manuel Pégourié-Gonnardf743c032019-05-24 12:06:29 +02005792 p += 4;
Manuel Pégourié-Gonnard35eb8022019-05-16 11:11:08 +02005793#endif /* MBEDTLS_SSL_SESSION_TICKETS && MBEDTLS_SSL_CLI_C */
5794
Manuel Pégourié-Gonnardf743c032019-05-24 12:06:29 +02005795 /*
5796 * Misc extension-related info
5797 */
5798#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005799 if (1 > (size_t) (end - p)) {
5800 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
5801 }
Manuel Pégourié-Gonnardf743c032019-05-24 12:06:29 +02005802
5803 session->mfl_code = *p++;
5804#endif
5805
5806#if defined(MBEDTLS_SSL_TRUNCATED_HMAC)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005807 if (1 > (size_t) (end - p)) {
5808 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
5809 }
Manuel Pégourié-Gonnardf743c032019-05-24 12:06:29 +02005810
5811 session->trunc_hmac = *p++;
5812#endif
5813
5814#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005815 if (1 > (size_t) (end - p)) {
5816 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
5817 }
Manuel Pégourié-Gonnardf743c032019-05-24 12:06:29 +02005818
5819 session->encrypt_then_mac = *p++;
5820#endif
5821
Manuel Pégourié-Gonnard35eb8022019-05-16 11:11:08 +02005822 /* Done, should have consumed entire buffer */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005823 if (p != end) {
5824 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
5825 }
Manuel Pégourié-Gonnarda3e7c652019-05-16 10:08:35 +02005826
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005827 return 0;
Manuel Pégourié-Gonnarda3e7c652019-05-16 10:08:35 +02005828}
5829
5830/*
Manuel Pégourié-Gonnardb9dfc9f2019-07-12 10:50:19 +02005831 * Deserialize session: public wrapper for error cleaning
Manuel Pégourié-Gonnarda3d831b2019-05-23 12:28:45 +02005832 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005833int mbedtls_ssl_session_load(mbedtls_ssl_session *session,
5834 const unsigned char *buf,
5835 size_t len)
Manuel Pégourié-Gonnarda3d831b2019-05-23 12:28:45 +02005836{
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005837 int ret = ssl_session_load(session, 0, buf, len);
Manuel Pégourié-Gonnarda3d831b2019-05-23 12:28:45 +02005838
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005839 if (ret != 0) {
5840 mbedtls_ssl_session_free(session);
5841 }
Manuel Pégourié-Gonnarda3d831b2019-05-23 12:28:45 +02005842
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005843 return ret;
Manuel Pégourié-Gonnarda3d831b2019-05-23 12:28:45 +02005844}
5845
5846/*
Paul Bakker1961b702013-01-25 14:49:24 +01005847 * Perform a single step of the SSL handshake
Paul Bakker5121ce52009-01-03 21:22:43 +00005848 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005849int mbedtls_ssl_handshake_step(mbedtls_ssl_context *ssl)
Paul Bakker5121ce52009-01-03 21:22:43 +00005850{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005851 int ret = MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
Paul Bakker5121ce52009-01-03 21:22:43 +00005852
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005853 if (ssl == NULL || ssl->conf == NULL) {
5854 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
5855 }
Manuel Pégourié-Gonnardf81ee2e2015-09-01 17:43:40 +02005856
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005857#if defined(MBEDTLS_SSL_CLI_C)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005858 if (ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT) {
5859 ret = mbedtls_ssl_handshake_client_step(ssl);
5860 }
Paul Bakker5121ce52009-01-03 21:22:43 +00005861#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005862#if defined(MBEDTLS_SSL_SRV_C)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005863 if (ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER) {
5864 ret = mbedtls_ssl_handshake_server_step(ssl);
5865 }
Paul Bakker5121ce52009-01-03 21:22:43 +00005866#endif
5867
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005868 return ret;
Paul Bakker1961b702013-01-25 14:49:24 +01005869}
5870
5871/*
5872 * Perform the SSL handshake
5873 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005874int mbedtls_ssl_handshake(mbedtls_ssl_context *ssl)
Paul Bakker1961b702013-01-25 14:49:24 +01005875{
5876 int ret = 0;
5877
Hanno Beckera817ea42020-10-20 15:20:23 +01005878 /* Sanity checks */
5879
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005880 if (ssl == NULL || ssl->conf == NULL) {
5881 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
5882 }
Manuel Pégourié-Gonnardf81ee2e2015-09-01 17:43:40 +02005883
Hanno Beckera817ea42020-10-20 15:20:23 +01005884#if defined(MBEDTLS_SSL_PROTO_DTLS)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005885 if (ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
5886 (ssl->f_set_timer == NULL || ssl->f_get_timer == NULL)) {
5887 MBEDTLS_SSL_DEBUG_MSG(1, ("You must use "
5888 "mbedtls_ssl_set_timer_cb() for DTLS"));
5889 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
Hanno Beckera817ea42020-10-20 15:20:23 +01005890 }
5891#endif /* MBEDTLS_SSL_PROTO_DTLS */
5892
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005893 MBEDTLS_SSL_DEBUG_MSG(2, ("=> handshake"));
Paul Bakker1961b702013-01-25 14:49:24 +01005894
Hanno Beckera817ea42020-10-20 15:20:23 +01005895 /* Main handshake loop */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005896 while (ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER) {
5897 ret = mbedtls_ssl_handshake_step(ssl);
Paul Bakker1961b702013-01-25 14:49:24 +01005898
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005899 if (ret != 0) {
Paul Bakker1961b702013-01-25 14:49:24 +01005900 break;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005901 }
Paul Bakker1961b702013-01-25 14:49:24 +01005902 }
5903
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005904 MBEDTLS_SSL_DEBUG_MSG(2, ("<= handshake"));
Paul Bakker5121ce52009-01-03 21:22:43 +00005905
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005906 return ret;
Paul Bakker5121ce52009-01-03 21:22:43 +00005907}
5908
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005909#if defined(MBEDTLS_SSL_RENEGOTIATION)
5910#if defined(MBEDTLS_SSL_SRV_C)
Paul Bakker5121ce52009-01-03 21:22:43 +00005911/*
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01005912 * Write HelloRequest to request renegotiation on server
Paul Bakker48916f92012-09-16 19:57:18 +00005913 */
Manuel Pégourié-Gonnardd904d662022-06-17 10:24:00 +02005914MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005915static int ssl_write_hello_request(mbedtls_ssl_context *ssl)
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01005916{
Janos Follath865b3eb2019-12-16 11:46:15 +00005917 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01005918
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005919 MBEDTLS_SSL_DEBUG_MSG(2, ("=> write hello request"));
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01005920
5921 ssl->out_msglen = 4;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005922 ssl->out_msgtype = MBEDTLS_SSL_MSG_HANDSHAKE;
5923 ssl->out_msg[0] = MBEDTLS_SSL_HS_HELLO_REQUEST;
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01005924
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005925 if ((ret = mbedtls_ssl_write_handshake_msg(ssl)) != 0) {
5926 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ssl_write_handshake_msg", ret);
5927 return ret;
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01005928 }
5929
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005930 MBEDTLS_SSL_DEBUG_MSG(2, ("<= write hello request"));
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01005931
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005932 return 0;
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01005933}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005934#endif /* MBEDTLS_SSL_SRV_C */
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01005935
5936/*
5937 * Actually renegotiate current connection, triggered by either:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005938 * - any side: calling mbedtls_ssl_renegotiate(),
5939 * - client: receiving a HelloRequest during mbedtls_ssl_read(),
5940 * - server: receiving any handshake message on server during mbedtls_ssl_read() after
Manuel Pégourié-Gonnard55e4ff22014-08-19 11:16:35 +02005941 * the initial handshake is completed.
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01005942 * If the handshake doesn't complete due to waiting for I/O, it will continue
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005943 * during the next calls to mbedtls_ssl_renegotiate() or mbedtls_ssl_read() respectively.
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01005944 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005945int mbedtls_ssl_start_renegotiation(mbedtls_ssl_context *ssl)
Paul Bakker48916f92012-09-16 19:57:18 +00005946{
Janos Follath865b3eb2019-12-16 11:46:15 +00005947 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker48916f92012-09-16 19:57:18 +00005948
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005949 MBEDTLS_SSL_DEBUG_MSG(2, ("=> renegotiate"));
Paul Bakker48916f92012-09-16 19:57:18 +00005950
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005951 if ((ret = ssl_handshake_init(ssl)) != 0) {
5952 return ret;
5953 }
Paul Bakker48916f92012-09-16 19:57:18 +00005954
Manuel Pégourié-Gonnard0557bd52014-08-19 19:18:39 +02005955 /* RFC 6347 4.2.2: "[...] the HelloRequest will have message_seq = 0 and
5956 * the ServerHello will have message_seq = 1" */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005957#if defined(MBEDTLS_SSL_PROTO_DTLS)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005958 if (ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
5959 ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_PENDING) {
5960 if (ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER) {
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02005961 ssl->handshake->out_msg_seq = 1;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005962 } else {
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02005963 ssl->handshake->in_msg_seq = 1;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005964 }
Manuel Pégourié-Gonnard0557bd52014-08-19 19:18:39 +02005965 }
5966#endif
5967
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005968 ssl->state = MBEDTLS_SSL_HELLO_REQUEST;
5969 ssl->renego_status = MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS;
Paul Bakker48916f92012-09-16 19:57:18 +00005970
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005971 if ((ret = mbedtls_ssl_handshake(ssl)) != 0) {
5972 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ssl_handshake", ret);
5973 return ret;
Paul Bakker48916f92012-09-16 19:57:18 +00005974 }
5975
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005976 MBEDTLS_SSL_DEBUG_MSG(2, ("<= renegotiate"));
Paul Bakker48916f92012-09-16 19:57:18 +00005977
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005978 return 0;
Paul Bakker48916f92012-09-16 19:57:18 +00005979}
5980
5981/*
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01005982 * Renegotiate current connection on client,
5983 * or request renegotiation on server
5984 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005985int mbedtls_ssl_renegotiate(mbedtls_ssl_context *ssl)
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01005986{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005987 int ret = MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01005988
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005989 if (ssl == NULL || ssl->conf == NULL) {
5990 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
5991 }
Manuel Pégourié-Gonnardf81ee2e2015-09-01 17:43:40 +02005992
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005993#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01005994 /* On server, just send the request */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005995 if (ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER) {
5996 if (ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER) {
5997 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
5998 }
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01005999
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006000 ssl->renego_status = MBEDTLS_SSL_RENEGOTIATION_PENDING;
Manuel Pégourié-Gonnardf07f4212014-08-15 19:04:47 +02006001
6002 /* Did we already try/start sending HelloRequest? */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01006003 if (ssl->out_left != 0) {
6004 return mbedtls_ssl_flush_output(ssl);
6005 }
Manuel Pégourié-Gonnardf07f4212014-08-15 19:04:47 +02006006
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01006007 return ssl_write_hello_request(ssl);
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01006008 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006009#endif /* MBEDTLS_SSL_SRV_C */
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01006010
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006011#if defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01006012 /*
6013 * On client, either start the renegotiation process or,
6014 * if already in progress, continue the handshake
6015 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01006016 if (ssl->renego_status != MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS) {
6017 if (ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER) {
6018 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01006019 }
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01006020
6021 if ((ret = mbedtls_ssl_start_renegotiation(ssl)) != 0) {
6022 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ssl_start_renegotiation", ret);
6023 return ret;
6024 }
6025 } else {
6026 if ((ret = mbedtls_ssl_handshake(ssl)) != 0) {
6027 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ssl_handshake", ret);
6028 return ret;
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01006029 }
6030 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006031#endif /* MBEDTLS_SSL_CLI_C */
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01006032
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01006033 return ret;
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01006034}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006035#endif /* MBEDTLS_SSL_RENEGOTIATION */
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01006036
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006037#if defined(MBEDTLS_X509_CRT_PARSE_C)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01006038static void ssl_key_cert_free(mbedtls_ssl_key_cert *key_cert)
Manuel Pégourié-Gonnard705fcca2013-09-23 20:04:20 +02006039{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006040 mbedtls_ssl_key_cert *cur = key_cert, *next;
Manuel Pégourié-Gonnard705fcca2013-09-23 20:04:20 +02006041
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01006042 while (cur != NULL) {
Manuel Pégourié-Gonnard705fcca2013-09-23 20:04:20 +02006043 next = cur->next;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01006044 mbedtls_free(cur);
Manuel Pégourié-Gonnard705fcca2013-09-23 20:04:20 +02006045 cur = next;
6046 }
6047}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006048#endif /* MBEDTLS_X509_CRT_PARSE_C */
Manuel Pégourié-Gonnard705fcca2013-09-23 20:04:20 +02006049
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01006050void mbedtls_ssl_handshake_free(mbedtls_ssl_context *ssl)
Paul Bakker48916f92012-09-16 19:57:18 +00006051{
Gilles Peskine9b562d52018-04-25 20:32:43 +02006052 mbedtls_ssl_handshake_params *handshake = ssl->handshake;
6053
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01006054 if (handshake == NULL) {
Paul Bakkeraccaffe2014-06-26 13:37:14 +02006055 return;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01006056 }
Paul Bakkeraccaffe2014-06-26 13:37:14 +02006057
Gilles Peskinedf13d5c2018-04-25 20:39:48 +02006058#if defined(MBEDTLS_SSL_ASYNC_PRIVATE)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01006059 if (ssl->conf->f_async_cancel != NULL && handshake->async_in_progress != 0) {
6060 ssl->conf->f_async_cancel(ssl);
Gilles Peskinedf13d5c2018-04-25 20:39:48 +02006061 handshake->async_in_progress = 0;
6062 }
6063#endif /* MBEDTLS_SSL_ASYNC_PRIVATE */
6064
Manuel Pégourié-Gonnardb9d64e52015-07-06 14:18:56 +02006065#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1) || \
6066 defined(MBEDTLS_SSL_PROTO_TLS1_1)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01006067 mbedtls_md5_free(&handshake->fin_md5);
6068 mbedtls_sha1_free(&handshake->fin_sha1);
Manuel Pégourié-Gonnardb9d64e52015-07-06 14:18:56 +02006069#endif
6070#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
6071#if defined(MBEDTLS_SHA256_C)
Andrzej Kurekeb342242019-01-29 09:14:33 -05006072#if defined(MBEDTLS_USE_PSA_CRYPTO)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01006073 psa_hash_abort(&handshake->fin_sha256_psa);
Andrzej Kurekeb342242019-01-29 09:14:33 -05006074#else
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01006075 mbedtls_sha256_free(&handshake->fin_sha256);
Manuel Pégourié-Gonnardb9d64e52015-07-06 14:18:56 +02006076#endif
Andrzej Kurekeb342242019-01-29 09:14:33 -05006077#endif
Gilles Peskined2d59372021-05-12 22:43:27 +02006078#if defined(MBEDTLS_SHA512_C) && !defined(MBEDTLS_SHA512_NO_SHA384)
Andrzej Kurekeb342242019-01-29 09:14:33 -05006079#if defined(MBEDTLS_USE_PSA_CRYPTO)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01006080 psa_hash_abort(&handshake->fin_sha384_psa);
Andrzej Kurekeb342242019-01-29 09:14:33 -05006081#else
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01006082 mbedtls_sha512_free(&handshake->fin_sha512);
Manuel Pégourié-Gonnardb9d64e52015-07-06 14:18:56 +02006083#endif
Andrzej Kurekeb342242019-01-29 09:14:33 -05006084#endif
Manuel Pégourié-Gonnardb9d64e52015-07-06 14:18:56 +02006085#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
6086
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006087#if defined(MBEDTLS_DHM_C)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01006088 mbedtls_dhm_free(&handshake->dhm_ctx);
Paul Bakker48916f92012-09-16 19:57:18 +00006089#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006090#if defined(MBEDTLS_ECDH_C)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01006091 mbedtls_ecdh_free(&handshake->ecdh_ctx);
Paul Bakker61d113b2013-07-04 11:51:43 +02006092#endif
Manuel Pégourié-Gonnardeef142d2015-09-16 10:05:04 +02006093#if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01006094 mbedtls_ecjpake_free(&handshake->ecjpake_ctx);
Manuel Pégourié-Gonnard77c06462015-09-17 13:59:49 +02006095#if defined(MBEDTLS_SSL_CLI_C)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01006096 mbedtls_free(handshake->ecjpake_cache);
Manuel Pégourié-Gonnard77c06462015-09-17 13:59:49 +02006097 handshake->ecjpake_cache = NULL;
6098 handshake->ecjpake_cache_len = 0;
6099#endif
Manuel Pégourié-Gonnard76cfd3f2015-09-15 12:10:54 +02006100#endif
Paul Bakker61d113b2013-07-04 11:51:43 +02006101
Janos Follath4ae5c292016-02-10 11:27:43 +00006102#if defined(MBEDTLS_ECDH_C) || defined(MBEDTLS_ECDSA_C) || \
6103 defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
Paul Bakker9af723c2014-05-01 13:03:14 +02006104 /* explicit void pointer cast for buggy MS compiler */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01006105 mbedtls_free((void *) handshake->curves);
Manuel Pégourié-Gonnardd09453c2013-09-23 19:11:32 +02006106#endif
6107
Gilles Peskineeccd8882020-03-10 12:19:08 +01006108#if defined(MBEDTLS_KEY_EXCHANGE_SOME_PSK_ENABLED)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01006109 if (handshake->psk != NULL) {
6110 mbedtls_platform_zeroize(handshake->psk, handshake->psk_len);
6111 mbedtls_free(handshake->psk);
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01006112 }
6113#endif
6114
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006115#if defined(MBEDTLS_X509_CRT_PARSE_C) && \
6116 defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
Manuel Pégourié-Gonnard83724542013-09-24 22:30:56 +02006117 /*
6118 * Free only the linked list wrapper, not the keys themselves
6119 * since the belong to the SNI callback
6120 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01006121 if (handshake->sni_key_cert != NULL) {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006122 mbedtls_ssl_key_cert *cur = handshake->sni_key_cert, *next;
Manuel Pégourié-Gonnard83724542013-09-24 22:30:56 +02006123
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01006124 while (cur != NULL) {
Manuel Pégourié-Gonnard83724542013-09-24 22:30:56 +02006125 next = cur->next;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01006126 mbedtls_free(cur);
Manuel Pégourié-Gonnard83724542013-09-24 22:30:56 +02006127 cur = next;
6128 }
6129 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006130#endif /* MBEDTLS_X509_CRT_PARSE_C && MBEDTLS_SSL_SERVER_NAME_INDICATION */
Manuel Pégourié-Gonnard705fcca2013-09-23 20:04:20 +02006131
Gilles Peskineeccd8882020-03-10 12:19:08 +01006132#if defined(MBEDTLS_SSL_ECP_RESTARTABLE_ENABLED)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01006133 mbedtls_x509_crt_restart_free(&handshake->ecrs_ctx);
6134 if (handshake->ecrs_peer_cert != NULL) {
6135 mbedtls_x509_crt_free(handshake->ecrs_peer_cert);
6136 mbedtls_free(handshake->ecrs_peer_cert);
Hanno Becker3dad3112019-02-05 17:19:52 +00006137 }
Manuel Pégourié-Gonnard862cde52017-05-17 11:56:15 +02006138#endif
6139
Hanno Becker75173122019-02-06 16:18:31 +00006140#if defined(MBEDTLS_X509_CRT_PARSE_C) && \
6141 !defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01006142 mbedtls_pk_free(&handshake->peer_pubkey);
Hanno Becker75173122019-02-06 16:18:31 +00006143#endif /* MBEDTLS_X509_CRT_PARSE_C && !MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
6144
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006145#if defined(MBEDTLS_SSL_PROTO_DTLS)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01006146 mbedtls_free(handshake->verify_cookie);
6147 mbedtls_ssl_flight_free(handshake->flight);
6148 mbedtls_ssl_buffering_free(ssl);
Manuel Pégourié-Gonnard74848812014-07-11 02:43:49 +02006149#endif
6150
Hanno Becker4a63ed42019-01-08 11:39:35 +00006151#if defined(MBEDTLS_ECDH_C) && \
6152 defined(MBEDTLS_USE_PSA_CRYPTO)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01006153 psa_destroy_key(handshake->ecdh_psa_privkey);
Hanno Becker4a63ed42019-01-08 11:39:35 +00006154#endif /* MBEDTLS_ECDH_C && MBEDTLS_USE_PSA_CRYPTO */
6155
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01006156 mbedtls_platform_zeroize(handshake,
6157 sizeof(mbedtls_ssl_handshake_params));
Andrzej Kurek0afa2a12020-03-03 10:39:58 -05006158
6159#if defined(MBEDTLS_SSL_VARIABLE_BUFFER_LENGTH)
6160 /* If the buffers are too big - reallocate. Because of the way Mbed TLS
6161 * processes datagrams and the fact that a datagram is allowed to have
6162 * several records in it, it is possible that the I/O buffers are not
6163 * empty at this stage */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01006164 handle_buffer_resizing(ssl, 1, mbedtls_ssl_get_input_buflen(ssl),
6165 mbedtls_ssl_get_output_buflen(ssl));
Andrzej Kurek0afa2a12020-03-03 10:39:58 -05006166#endif
Paul Bakker48916f92012-09-16 19:57:18 +00006167}
6168
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01006169void mbedtls_ssl_session_free(mbedtls_ssl_session *session)
Paul Bakker48916f92012-09-16 19:57:18 +00006170{
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01006171 if (session == NULL) {
Paul Bakkeraccaffe2014-06-26 13:37:14 +02006172 return;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01006173 }
Paul Bakkeraccaffe2014-06-26 13:37:14 +02006174
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006175#if defined(MBEDTLS_X509_CRT_PARSE_C)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01006176 ssl_clear_peer_cert(session);
Paul Bakkered27a042013-04-18 22:46:23 +02006177#endif
Paul Bakker0a597072012-09-25 21:55:46 +00006178
Manuel Pégourié-Gonnardb596abf2015-05-20 10:45:29 +02006179#if defined(MBEDTLS_SSL_SESSION_TICKETS) && defined(MBEDTLS_SSL_CLI_C)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01006180 mbedtls_free(session->ticket);
Paul Bakkera503a632013-08-14 13:48:06 +02006181#endif
Manuel Pégourié-Gonnard75d44012013-08-02 14:44:04 +02006182
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01006183 mbedtls_platform_zeroize(session, sizeof(mbedtls_ssl_session));
Paul Bakker48916f92012-09-16 19:57:18 +00006184}
6185
Manuel Pégourié-Gonnard5c0e3772019-07-23 16:13:17 +02006186#if defined(MBEDTLS_SSL_CONTEXT_SERIALIZATION)
Manuel Pégourié-Gonnard4e9370b2019-07-23 16:31:16 +02006187
6188#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
6189#define SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_CONNECTION_ID 1u
6190#else
6191#define SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_CONNECTION_ID 0u
6192#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
6193
6194#if defined(MBEDTLS_SSL_DTLS_BADMAC_LIMIT)
6195#define SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_BADMAC_LIMIT 1u
6196#else
6197#define SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_BADMAC_LIMIT 0u
6198#endif /* MBEDTLS_SSL_DTLS_BADMAC_LIMIT */
6199
6200#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
6201#define SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_ANTI_REPLAY 1u
6202#else
6203#define SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_ANTI_REPLAY 0u
6204#endif /* MBEDTLS_SSL_DTLS_ANTI_REPLAY */
6205
6206#if defined(MBEDTLS_SSL_ALPN)
6207#define SSL_SERIALIZED_CONTEXT_CONFIG_ALPN 1u
6208#else
6209#define SSL_SERIALIZED_CONTEXT_CONFIG_ALPN 0u
6210#endif /* MBEDTLS_SSL_ALPN */
6211
6212#define SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_CONNECTION_ID_BIT 0
6213#define SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_BADMAC_LIMIT_BIT 1
6214#define SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_ANTI_REPLAY_BIT 2
6215#define SSL_SERIALIZED_CONTEXT_CONFIG_ALPN_BIT 3
6216
6217#define SSL_SERIALIZED_CONTEXT_CONFIG_BITFLAG \
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01006218 ((uint32_t) ( \
6219 (SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_CONNECTION_ID << \
6220 SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_CONNECTION_ID_BIT) | \
6221 (SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_BADMAC_LIMIT << \
6222 SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_BADMAC_LIMIT_BIT) | \
6223 (SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_ANTI_REPLAY << \
6224 SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_ANTI_REPLAY_BIT) | \
6225 (SSL_SERIALIZED_CONTEXT_CONFIG_ALPN << SSL_SERIALIZED_CONTEXT_CONFIG_ALPN_BIT) | \
6226 0u))
Manuel Pégourié-Gonnard4e9370b2019-07-23 16:31:16 +02006227
Chien Wongb6d57932024-02-07 21:48:12 +08006228static const unsigned char ssl_serialized_context_header[] = {
Manuel Pégourié-Gonnard4c90e852019-07-11 10:58:10 +02006229 MBEDTLS_VERSION_MAJOR,
6230 MBEDTLS_VERSION_MINOR,
6231 MBEDTLS_VERSION_PATCH,
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01006232 MBEDTLS_BYTE_1(SSL_SERIALIZED_SESSION_CONFIG_BITFLAG),
6233 MBEDTLS_BYTE_0(SSL_SERIALIZED_SESSION_CONFIG_BITFLAG),
6234 MBEDTLS_BYTE_2(SSL_SERIALIZED_CONTEXT_CONFIG_BITFLAG),
6235 MBEDTLS_BYTE_1(SSL_SERIALIZED_CONTEXT_CONFIG_BITFLAG),
6236 MBEDTLS_BYTE_0(SSL_SERIALIZED_CONTEXT_CONFIG_BITFLAG),
Manuel Pégourié-Gonnard4c90e852019-07-11 10:58:10 +02006237};
6238
Paul Bakker5121ce52009-01-03 21:22:43 +00006239/*
Manuel Pégourié-Gonnardac87e282019-05-28 13:02:16 +02006240 * Serialize a full SSL context
Manuel Pégourié-Gonnard00400c22019-07-10 14:58:45 +02006241 *
6242 * The format of the serialized data is:
6243 * (in the presentation language of TLS, RFC 8446 section 3)
6244 *
6245 * // header
6246 * opaque mbedtls_version[3]; // major, minor, patch
Manuel Pégourié-Gonnard4c90e852019-07-11 10:58:10 +02006247 * opaque context_format[5]; // version-specific field determining
Manuel Pégourié-Gonnard00400c22019-07-10 14:58:45 +02006248 * // the format of the remaining
Manuel Pégourié-Gonnard4c90e852019-07-11 10:58:10 +02006249 * // serialized data.
Manuel Pégourié-Gonnard4e9370b2019-07-23 16:31:16 +02006250 * Note: When updating the format, remember to keep these
6251 * version+format bytes. (We may make their size part of the API.)
Manuel Pégourié-Gonnard00400c22019-07-10 14:58:45 +02006252 *
6253 * // session sub-structure
6254 * opaque session<1..2^32-1>; // see mbedtls_ssl_session_save()
6255 * // transform sub-structure
6256 * uint8 random[64]; // ServerHello.random+ClientHello.random
6257 * uint8 in_cid<0..2^8-1> // Connection ID: expected incoming value
6258 * uint8 out_cid<0..2^8-1> // Connection ID: outgoing value to use
6259 * // fields from ssl_context
6260 * uint32 badmac_seen; // DTLS: number of records with failing MAC
6261 * uint64 in_window_top; // DTLS: last validated record seq_num
6262 * uint64 in_window; // DTLS: bitmask for replay protection
6263 * uint8 disable_datagram_packing; // DTLS: only one record per datagram
6264 * uint64 cur_out_ctr; // Record layer: outgoing sequence number
6265 * uint16 mtu; // DTLS: path mtu (max outgoing fragment size)
6266 * uint8 alpn_chosen<0..2^8-1> // ALPN: negotiated application protocol
6267 *
6268 * Note that many fields of the ssl_context or sub-structures are not
6269 * serialized, as they fall in one of the following categories:
6270 *
6271 * 1. forced value (eg in_left must be 0)
6272 * 2. pointer to dynamically-allocated memory (eg session, transform)
6273 * 3. value can be re-derived from other data (eg session keys from MS)
6274 * 4. value was temporary (eg content of input buffer)
6275 * 5. value will be provided by the user again (eg I/O callbacks and context)
Manuel Pégourié-Gonnardac87e282019-05-28 13:02:16 +02006276 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01006277int mbedtls_ssl_context_save(mbedtls_ssl_context *ssl,
6278 unsigned char *buf,
6279 size_t buf_len,
6280 size_t *olen)
Manuel Pégourié-Gonnardac87e282019-05-28 13:02:16 +02006281{
Manuel Pégourié-Gonnard4c90e852019-07-11 10:58:10 +02006282 unsigned char *p = buf;
6283 size_t used = 0;
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02006284 size_t session_len;
6285 int ret = 0;
Manuel Pégourié-Gonnard4c90e852019-07-11 10:58:10 +02006286
Manuel Pégourié-Gonnard1aaf6692019-07-10 14:14:05 +02006287 /*
Manuel Pégourié-Gonnarde4588692019-07-29 12:28:52 +02006288 * Enforce usage restrictions, see "return BAD_INPUT_DATA" in
6289 * this function's documentation.
6290 *
6291 * These are due to assumptions/limitations in the implementation. Some of
6292 * them are likely to stay (no handshake in progress) some might go away
6293 * (only DTLS) but are currently used to simplify the implementation.
Manuel Pégourié-Gonnard1aaf6692019-07-10 14:14:05 +02006294 */
Manuel Pégourié-Gonnarde4588692019-07-29 12:28:52 +02006295 /* The initial handshake must be over */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01006296 if (ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER) {
6297 MBEDTLS_SSL_DEBUG_MSG(1, ("Initial handshake isn't over"));
6298 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
Jarno Lamsa8c51b7c2019-08-21 13:45:05 +03006299 }
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01006300 if (ssl->handshake != NULL) {
6301 MBEDTLS_SSL_DEBUG_MSG(1, ("Handshake isn't completed"));
6302 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
Jarno Lamsa8c51b7c2019-08-21 13:45:05 +03006303 }
Manuel Pégourié-Gonnarde4588692019-07-29 12:28:52 +02006304 /* Double-check that sub-structures are indeed ready */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01006305 if (ssl->transform == NULL || ssl->session == NULL) {
6306 MBEDTLS_SSL_DEBUG_MSG(1, ("Serialised structures aren't ready"));
6307 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
Jarno Lamsa8c51b7c2019-08-21 13:45:05 +03006308 }
Manuel Pégourié-Gonnarde4588692019-07-29 12:28:52 +02006309 /* There must be no pending incoming or outgoing data */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01006310 if (mbedtls_ssl_check_pending(ssl) != 0) {
6311 MBEDTLS_SSL_DEBUG_MSG(1, ("There is pending incoming data"));
6312 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
Jarno Lamsa8c51b7c2019-08-21 13:45:05 +03006313 }
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01006314 if (ssl->out_left != 0) {
6315 MBEDTLS_SSL_DEBUG_MSG(1, ("There is pending outgoing data"));
6316 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
Jarno Lamsa8c51b7c2019-08-21 13:45:05 +03006317 }
Dave Rodgmana03396a2022-12-06 16:30:38 +00006318 /* Protocol must be DTLS, not TLS */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01006319 if (ssl->conf->transport != MBEDTLS_SSL_TRANSPORT_DATAGRAM) {
6320 MBEDTLS_SSL_DEBUG_MSG(1, ("Only DTLS is supported"));
6321 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
Jarno Lamsa8c51b7c2019-08-21 13:45:05 +03006322 }
Manuel Pégourié-Gonnarde4588692019-07-29 12:28:52 +02006323 /* Version must be 1.2 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01006324 if (ssl->major_ver != MBEDTLS_SSL_MAJOR_VERSION_3) {
6325 MBEDTLS_SSL_DEBUG_MSG(1, ("Only version 1.2 supported"));
6326 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
Jarno Lamsa8c51b7c2019-08-21 13:45:05 +03006327 }
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01006328 if (ssl->minor_ver != MBEDTLS_SSL_MINOR_VERSION_3) {
6329 MBEDTLS_SSL_DEBUG_MSG(1, ("Only version 1.2 supported"));
6330 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
Jarno Lamsa8c51b7c2019-08-21 13:45:05 +03006331 }
Manuel Pégourié-Gonnarde4588692019-07-29 12:28:52 +02006332 /* We must be using an AEAD ciphersuite */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01006333 if (mbedtls_ssl_transform_uses_aead(ssl->transform) != 1) {
6334 MBEDTLS_SSL_DEBUG_MSG(1, ("Only AEAD ciphersuites supported"));
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 /* Renegotiation must not be enabled */
6338#if defined(MBEDTLS_SSL_RENEGOTIATION)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01006339 if (ssl->conf->disable_renegotiation != MBEDTLS_SSL_RENEGOTIATION_DISABLED) {
6340 MBEDTLS_SSL_DEBUG_MSG(1, ("Renegotiation must not be enabled"));
6341 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
Jarno Lamsa8c51b7c2019-08-21 13:45:05 +03006342 }
Manuel Pégourié-Gonnarde4588692019-07-29 12:28:52 +02006343#endif
Manuel Pégourié-Gonnardac87e282019-05-28 13:02:16 +02006344
Manuel Pégourié-Gonnard4c90e852019-07-11 10:58:10 +02006345 /*
6346 * Version and format identifier
6347 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01006348 used += sizeof(ssl_serialized_context_header);
Manuel Pégourié-Gonnardac87e282019-05-28 13:02:16 +02006349
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01006350 if (used <= buf_len) {
6351 memcpy(p, ssl_serialized_context_header,
6352 sizeof(ssl_serialized_context_header));
6353 p += sizeof(ssl_serialized_context_header);
Manuel Pégourié-Gonnard4c90e852019-07-11 10:58:10 +02006354 }
6355
6356 /*
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02006357 * Session (length + data)
6358 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01006359 ret = ssl_session_save(ssl->session, 1, NULL, 0, &session_len);
6360 if (ret != MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL) {
6361 return ret;
6362 }
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02006363
6364 used += 4 + session_len;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01006365 if (used <= buf_len) {
6366 MBEDTLS_PUT_UINT32_BE(session_len, p, 0);
Joe Subbiani2f98d792021-08-20 11:44:44 +01006367 p += 4;
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02006368
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01006369 ret = ssl_session_save(ssl->session, 1,
6370 p, session_len, &session_len);
6371 if (ret != 0) {
6372 return ret;
6373 }
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02006374
6375 p += session_len;
6376 }
6377
6378 /*
Manuel Pégourié-Gonnardc2a7b892019-07-15 09:04:11 +02006379 * Transform
6380 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01006381 used += sizeof(ssl->transform->randbytes);
6382 if (used <= buf_len) {
6383 memcpy(p, ssl->transform->randbytes,
6384 sizeof(ssl->transform->randbytes));
6385 p += sizeof(ssl->transform->randbytes);
Manuel Pégourié-Gonnardc2a7b892019-07-15 09:04:11 +02006386 }
6387
6388#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
6389 used += 2 + ssl->transform->in_cid_len + ssl->transform->out_cid_len;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01006390 if (used <= buf_len) {
Manuel Pégourié-Gonnardc2a7b892019-07-15 09:04:11 +02006391 *p++ = ssl->transform->in_cid_len;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01006392 memcpy(p, ssl->transform->in_cid, ssl->transform->in_cid_len);
Manuel Pégourié-Gonnardc2a7b892019-07-15 09:04:11 +02006393 p += ssl->transform->in_cid_len;
6394
6395 *p++ = ssl->transform->out_cid_len;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01006396 memcpy(p, ssl->transform->out_cid, ssl->transform->out_cid_len);
Manuel Pégourié-Gonnardc2a7b892019-07-15 09:04:11 +02006397 p += ssl->transform->out_cid_len;
6398 }
6399#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
6400
6401 /*
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02006402 * Saved fields from top-level ssl_context structure
6403 */
6404#if defined(MBEDTLS_SSL_DTLS_BADMAC_LIMIT)
6405 used += 4;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01006406 if (used <= buf_len) {
6407 MBEDTLS_PUT_UINT32_BE(ssl->badmac_seen, p, 0);
Joe Subbiani2f98d792021-08-20 11:44:44 +01006408 p += 4;
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02006409 }
6410#endif /* MBEDTLS_SSL_DTLS_BADMAC_LIMIT */
6411
6412#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
6413 used += 16;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01006414 if (used <= buf_len) {
6415 MBEDTLS_PUT_UINT64_BE(ssl->in_window_top, p, 0);
Joe Subbiani2f98d792021-08-20 11:44:44 +01006416 p += 8;
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02006417
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01006418 MBEDTLS_PUT_UINT64_BE(ssl->in_window, p, 0);
Joe Subbiani2f98d792021-08-20 11:44:44 +01006419 p += 8;
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02006420 }
6421#endif /* MBEDTLS_SSL_DTLS_ANTI_REPLAY */
6422
6423#if defined(MBEDTLS_SSL_PROTO_DTLS)
6424 used += 1;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01006425 if (used <= buf_len) {
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02006426 *p++ = ssl->disable_datagram_packing;
6427 }
6428#endif /* MBEDTLS_SSL_PROTO_DTLS */
6429
6430 used += 8;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01006431 if (used <= buf_len) {
6432 memcpy(p, ssl->cur_out_ctr, 8);
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02006433 p += 8;
6434 }
6435
6436#if defined(MBEDTLS_SSL_PROTO_DTLS)
6437 used += 2;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01006438 if (used <= buf_len) {
6439 MBEDTLS_PUT_UINT16_BE(ssl->mtu, p, 0);
Joe Subbiani2f98d792021-08-20 11:44:44 +01006440 p += 2;
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02006441 }
6442#endif /* MBEDTLS_SSL_PROTO_DTLS */
6443
6444#if defined(MBEDTLS_SSL_ALPN)
6445 {
6446 const uint8_t alpn_len = ssl->alpn_chosen
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01006447 ? (uint8_t) strlen(ssl->alpn_chosen)
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02006448 : 0;
6449
6450 used += 1 + alpn_len;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01006451 if (used <= buf_len) {
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02006452 *p++ = alpn_len;
6453
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01006454 if (ssl->alpn_chosen != NULL) {
6455 memcpy(p, ssl->alpn_chosen, alpn_len);
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02006456 p += alpn_len;
6457 }
6458 }
6459 }
6460#endif /* MBEDTLS_SSL_ALPN */
6461
6462 /*
Manuel Pégourié-Gonnard4c90e852019-07-11 10:58:10 +02006463 * Done
6464 */
6465 *olen = used;
6466
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01006467 if (used > buf_len) {
6468 return MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL;
6469 }
Manuel Pégourié-Gonnardac87e282019-05-28 13:02:16 +02006470
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01006471 MBEDTLS_SSL_DEBUG_BUF(4, "saved context", buf, used);
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02006472
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01006473 return mbedtls_ssl_session_reset_int(ssl, 0);
Manuel Pégourié-Gonnardac87e282019-05-28 13:02:16 +02006474}
6475
6476/*
Manuel Pégourié-Gonnardc2a7b892019-07-15 09:04:11 +02006477 * Helper to get TLS 1.2 PRF from ciphersuite
6478 * (Duplicates bits of logic from ssl_set_handshake_prfs().)
6479 */
Andrzej Kurekf9412f72022-10-18 07:30:19 -04006480#if defined(MBEDTLS_SHA256_C) || \
6481 (defined(MBEDTLS_SHA512_C) && !defined(MBEDTLS_SHA512_NO_SHA384))
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01006482typedef int (*tls_prf_fn)(const unsigned char *secret, size_t slen,
6483 const char *label,
6484 const unsigned char *random, size_t rlen,
6485 unsigned char *dstbuf, size_t dlen);
6486static tls_prf_fn ssl_tls12prf_from_cs(int ciphersuite_id)
Manuel Pégourié-Gonnardc2a7b892019-07-15 09:04:11 +02006487{
6488 const mbedtls_ssl_ciphersuite_t * const ciphersuite_info =
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01006489 mbedtls_ssl_ciphersuite_from_id(ciphersuite_id);
Manuel Pégourié-Gonnardc2a7b892019-07-15 09:04:11 +02006490
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01006491 if (ciphersuite_info == NULL) {
6492 return NULL;
6493 }
Andrzej Kurek84fc52c2022-10-25 04:18:30 -04006494
6495#if defined(MBEDTLS_SHA512_C) && !defined(MBEDTLS_SHA512_NO_SHA384)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01006496 if (ciphersuite_info->mac == MBEDTLS_MD_SHA384) {
6497 return tls_prf_sha384;
6498 } else
Manuel Pégourié-Gonnard9a96fd72019-07-23 17:11:24 +02006499#endif
Andrzej Kurekf9412f72022-10-18 07:30:19 -04006500#if defined(MBEDTLS_SHA256_C)
6501 {
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01006502 if (ciphersuite_info->mac == MBEDTLS_MD_SHA256) {
6503 return tls_prf_sha256;
6504 }
Andrzej Kurekf9412f72022-10-18 07:30:19 -04006505 }
6506#endif
6507#if !defined(MBEDTLS_SHA256_C) && \
6508 (!defined(MBEDTLS_SHA512_C) || defined(MBEDTLS_SHA512_NO_SHA384))
6509 (void) ciphersuite_info;
6510#endif
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01006511 return NULL;
Manuel Pégourié-Gonnardc2a7b892019-07-15 09:04:11 +02006512}
6513
Andrzej Kurekf9412f72022-10-18 07:30:19 -04006514#endif /* MBEDTLS_SHA256_C ||
6515 (MBEDTLS_SHA512_C && !MBEDTLS_SHA512_NO_SHA384) */
6516
Manuel Pégourié-Gonnardc2a7b892019-07-15 09:04:11 +02006517/*
Manuel Pégourié-Gonnardb9dfc9f2019-07-12 10:50:19 +02006518 * Deserialize context, see mbedtls_ssl_context_save() for format.
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02006519 *
6520 * This internal version is wrapped by a public function that cleans up in
6521 * case of error.
Manuel Pégourié-Gonnardac87e282019-05-28 13:02:16 +02006522 */
Manuel Pégourié-Gonnardd904d662022-06-17 10:24:00 +02006523MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01006524static int ssl_context_load(mbedtls_ssl_context *ssl,
6525 const unsigned char *buf,
6526 size_t len)
Manuel Pégourié-Gonnardac87e282019-05-28 13:02:16 +02006527{
Manuel Pégourié-Gonnard4c90e852019-07-11 10:58:10 +02006528 const unsigned char *p = buf;
6529 const unsigned char * const end = buf + len;
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02006530 size_t session_len;
Janos Follath865b3eb2019-12-16 11:46:15 +00006531 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Andrzej Kurekf9412f72022-10-18 07:30:19 -04006532 tls_prf_fn prf_func = NULL;
Manuel Pégourié-Gonnard4c90e852019-07-11 10:58:10 +02006533
Manuel Pégourié-Gonnard0ff76402019-07-11 09:56:30 +02006534 /*
6535 * The context should have been freshly setup or reset.
6536 * Give the user an error in case of obvious misuse.
Manuel Pégourié-Gonnard4ca930f2019-07-26 16:31:53 +02006537 * (Checking session is useful because it won't be NULL if we're
Manuel Pégourié-Gonnard0ff76402019-07-11 09:56:30 +02006538 * renegotiating, or if the user mistakenly loaded a session first.)
6539 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01006540 if (ssl->state != MBEDTLS_SSL_HELLO_REQUEST ||
6541 ssl->session != NULL) {
6542 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
Manuel Pégourié-Gonnard0ff76402019-07-11 09:56:30 +02006543 }
6544
6545 /*
6546 * We can't check that the config matches the initial one, but we can at
6547 * least check it matches the requirements for serializing.
6548 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01006549 if (ssl->conf->transport != MBEDTLS_SSL_TRANSPORT_DATAGRAM ||
Manuel Pégourié-Gonnard0ff76402019-07-11 09:56:30 +02006550 ssl->conf->max_major_ver < MBEDTLS_SSL_MAJOR_VERSION_3 ||
6551 ssl->conf->min_major_ver > MBEDTLS_SSL_MAJOR_VERSION_3 ||
6552 ssl->conf->max_minor_ver < MBEDTLS_SSL_MINOR_VERSION_3 ||
6553 ssl->conf->min_minor_ver > MBEDTLS_SSL_MINOR_VERSION_3 ||
6554#if defined(MBEDTLS_SSL_RENEGOTIATION)
Manuel Pégourié-Gonnard9a96fd72019-07-23 17:11:24 +02006555 ssl->conf->disable_renegotiation != MBEDTLS_SSL_RENEGOTIATION_DISABLED ||
Manuel Pégourié-Gonnard0ff76402019-07-11 09:56:30 +02006556#endif
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01006557 0) {
6558 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
Manuel Pégourié-Gonnard0ff76402019-07-11 09:56:30 +02006559 }
6560
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01006561 MBEDTLS_SSL_DEBUG_BUF(4, "context to load", buf, len);
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02006562
Manuel Pégourié-Gonnard4c90e852019-07-11 10:58:10 +02006563 /*
6564 * Check version identifier
6565 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01006566 if ((size_t) (end - p) < sizeof(ssl_serialized_context_header)) {
6567 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
Manuel Pégourié-Gonnard4c90e852019-07-11 10:58:10 +02006568 }
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01006569
6570 if (memcmp(p, ssl_serialized_context_header,
6571 sizeof(ssl_serialized_context_header)) != 0) {
6572 return MBEDTLS_ERR_SSL_VERSION_MISMATCH;
6573 }
6574 p += sizeof(ssl_serialized_context_header);
Manuel Pégourié-Gonnard4c90e852019-07-11 10:58:10 +02006575
6576 /*
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02006577 * Session
6578 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01006579 if ((size_t) (end - p) < 4) {
6580 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
6581 }
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02006582
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01006583 session_len = ((size_t) p[0] << 24) |
6584 ((size_t) p[1] << 16) |
6585 ((size_t) p[2] << 8) |
6586 ((size_t) p[3]);
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02006587 p += 4;
6588
Manuel Pégourié-Gonnard142ba732019-07-23 14:43:30 +02006589 /* This has been allocated by ssl_handshake_init(), called by
Hanno Becker43aefe22020-02-05 10:44:56 +00006590 * by either mbedtls_ssl_session_reset_int() or mbedtls_ssl_setup(). */
Manuel Pégourié-Gonnard142ba732019-07-23 14:43:30 +02006591 ssl->session = ssl->session_negotiate;
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02006592 ssl->session_in = ssl->session;
6593 ssl->session_out = ssl->session;
Manuel Pégourié-Gonnard142ba732019-07-23 14:43:30 +02006594 ssl->session_negotiate = NULL;
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02006595
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01006596 if ((size_t) (end - p) < session_len) {
6597 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
6598 }
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02006599
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01006600 ret = ssl_session_load(ssl->session, 1, p, session_len);
6601 if (ret != 0) {
6602 mbedtls_ssl_session_free(ssl->session);
6603 return ret;
Manuel Pégourié-Gonnard45ac1f02019-07-23 16:52:45 +02006604 }
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02006605
6606 p += session_len;
6607
6608 /*
Manuel Pégourié-Gonnardc2a7b892019-07-15 09:04:11 +02006609 * Transform
6610 */
6611
Manuel Pégourié-Gonnard142ba732019-07-23 14:43:30 +02006612 /* This has been allocated by ssl_handshake_init(), called by
Hanno Becker43aefe22020-02-05 10:44:56 +00006613 * by either mbedtls_ssl_session_reset_int() or mbedtls_ssl_setup(). */
Manuel Pégourié-Gonnard142ba732019-07-23 14:43:30 +02006614 ssl->transform = ssl->transform_negotiate;
Manuel Pégourié-Gonnardc2a7b892019-07-15 09:04:11 +02006615 ssl->transform_in = ssl->transform;
6616 ssl->transform_out = ssl->transform;
Manuel Pégourié-Gonnard142ba732019-07-23 14:43:30 +02006617 ssl->transform_negotiate = NULL;
Manuel Pégourié-Gonnardc2a7b892019-07-15 09:04:11 +02006618
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01006619 prf_func = ssl_tls12prf_from_cs(ssl->session->ciphersuite);
6620 if (prf_func == NULL) {
6621 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
6622 }
Andrzej Kurekf9412f72022-10-18 07:30:19 -04006623
Manuel Pégourié-Gonnardc2a7b892019-07-15 09:04:11 +02006624 /* Read random bytes and populate structure */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01006625 if ((size_t) (end - p) < sizeof(ssl->transform->randbytes)) {
6626 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
6627 }
Manuel Pégourié-Gonnardc2a7b892019-07-15 09:04:11 +02006628
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01006629 ret = ssl_populate_transform(ssl->transform,
6630 ssl->session->ciphersuite,
6631 ssl->session->master,
Manuel Pégourié-Gonnardc2a7b892019-07-15 09:04:11 +02006632#if defined(MBEDTLS_SSL_SOME_MODES_USE_MAC)
6633#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01006634 ssl->session->encrypt_then_mac,
Manuel Pégourié-Gonnardc2a7b892019-07-15 09:04:11 +02006635#endif
6636#if defined(MBEDTLS_SSL_TRUNCATED_HMAC)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01006637 ssl->session->trunc_hmac,
Manuel Pégourié-Gonnardc2a7b892019-07-15 09:04:11 +02006638#endif
6639#endif /* MBEDTLS_SSL_SOME_MODES_USE_MAC */
6640#if defined(MBEDTLS_ZLIB_SUPPORT)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01006641 ssl->session->compression,
Manuel Pégourié-Gonnardc2a7b892019-07-15 09:04:11 +02006642#endif
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01006643 prf_func,
6644 p, /* currently pointing to randbytes */
6645 MBEDTLS_SSL_MINOR_VERSION_3, /* (D)TLS 1.2 is forced */
6646 ssl->conf->endpoint,
6647 ssl);
6648 if (ret != 0) {
6649 return ret;
6650 }
Manuel Pégourié-Gonnardc2a7b892019-07-15 09:04:11 +02006651
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01006652 p += sizeof(ssl->transform->randbytes);
Manuel Pégourié-Gonnardc2a7b892019-07-15 09:04:11 +02006653
6654#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
6655 /* Read connection IDs and store them */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01006656 if ((size_t) (end - p) < 1) {
6657 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
6658 }
Manuel Pégourié-Gonnardc2a7b892019-07-15 09:04:11 +02006659
6660 ssl->transform->in_cid_len = *p++;
6661
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01006662 if ((size_t) (end - p) < ssl->transform->in_cid_len + 1u) {
6663 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
6664 }
Manuel Pégourié-Gonnardc2a7b892019-07-15 09:04:11 +02006665
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01006666 memcpy(ssl->transform->in_cid, p, ssl->transform->in_cid_len);
Manuel Pégourié-Gonnardc2a7b892019-07-15 09:04:11 +02006667 p += ssl->transform->in_cid_len;
6668
6669 ssl->transform->out_cid_len = *p++;
6670
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01006671 if ((size_t) (end - p) < ssl->transform->out_cid_len) {
6672 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
6673 }
Manuel Pégourié-Gonnardc2a7b892019-07-15 09:04:11 +02006674
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01006675 memcpy(ssl->transform->out_cid, p, ssl->transform->out_cid_len);
Manuel Pégourié-Gonnardc2a7b892019-07-15 09:04:11 +02006676 p += ssl->transform->out_cid_len;
6677#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
6678
6679 /*
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02006680 * Saved fields from top-level ssl_context structure
6681 */
6682#if defined(MBEDTLS_SSL_DTLS_BADMAC_LIMIT)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01006683 if ((size_t) (end - p) < 4) {
6684 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
6685 }
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02006686
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01006687 ssl->badmac_seen = ((uint32_t) p[0] << 24) |
6688 ((uint32_t) p[1] << 16) |
6689 ((uint32_t) p[2] << 8) |
6690 ((uint32_t) p[3]);
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02006691 p += 4;
6692#endif /* MBEDTLS_SSL_DTLS_BADMAC_LIMIT */
6693
6694#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01006695 if ((size_t) (end - p) < 16) {
6696 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
6697 }
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02006698
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01006699 ssl->in_window_top = ((uint64_t) p[0] << 56) |
6700 ((uint64_t) p[1] << 48) |
6701 ((uint64_t) p[2] << 40) |
6702 ((uint64_t) p[3] << 32) |
6703 ((uint64_t) p[4] << 24) |
6704 ((uint64_t) p[5] << 16) |
6705 ((uint64_t) p[6] << 8) |
6706 ((uint64_t) p[7]);
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02006707 p += 8;
6708
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01006709 ssl->in_window = ((uint64_t) p[0] << 56) |
6710 ((uint64_t) p[1] << 48) |
6711 ((uint64_t) p[2] << 40) |
6712 ((uint64_t) p[3] << 32) |
6713 ((uint64_t) p[4] << 24) |
6714 ((uint64_t) p[5] << 16) |
6715 ((uint64_t) p[6] << 8) |
6716 ((uint64_t) p[7]);
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02006717 p += 8;
6718#endif /* MBEDTLS_SSL_DTLS_ANTI_REPLAY */
6719
6720#if defined(MBEDTLS_SSL_PROTO_DTLS)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01006721 if ((size_t) (end - p) < 1) {
6722 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
6723 }
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02006724
6725 ssl->disable_datagram_packing = *p++;
6726#endif /* MBEDTLS_SSL_PROTO_DTLS */
6727
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01006728 if ((size_t) (end - p) < 8) {
6729 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
6730 }
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02006731
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01006732 memcpy(ssl->cur_out_ctr, p, 8);
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02006733 p += 8;
6734
6735#if defined(MBEDTLS_SSL_PROTO_DTLS)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01006736 if ((size_t) (end - p) < 2) {
6737 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
6738 }
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02006739
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01006740 ssl->mtu = (p[0] << 8) | p[1];
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02006741 p += 2;
6742#endif /* MBEDTLS_SSL_PROTO_DTLS */
6743
6744#if defined(MBEDTLS_SSL_ALPN)
6745 {
6746 uint8_t alpn_len;
6747 const char **cur;
6748
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01006749 if ((size_t) (end - p) < 1) {
6750 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
6751 }
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02006752
6753 alpn_len = *p++;
6754
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01006755 if (alpn_len != 0 && ssl->conf->alpn_list != NULL) {
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02006756 /* alpn_chosen should point to an item in the configured list */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01006757 for (cur = ssl->conf->alpn_list; *cur != NULL; cur++) {
6758 if (strlen(*cur) == alpn_len &&
Waleed Elmelegy98ebf482024-03-15 14:29:24 +00006759 memcmp(p, *cur, alpn_len) == 0) {
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02006760 ssl->alpn_chosen = *cur;
6761 break;
6762 }
6763 }
6764 }
6765
6766 /* can only happen on conf mismatch */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01006767 if (alpn_len != 0 && ssl->alpn_chosen == NULL) {
6768 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
6769 }
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02006770
6771 p += alpn_len;
6772 }
6773#endif /* MBEDTLS_SSL_ALPN */
6774
6775 /*
Manuel Pégourié-Gonnard0eb3eac2019-07-15 11:53:51 +02006776 * Forced fields from top-level ssl_context structure
6777 *
6778 * Most of them already set to the correct value by mbedtls_ssl_init() and
6779 * mbedtls_ssl_reset(), so we only need to set the remaining ones.
6780 */
6781 ssl->state = MBEDTLS_SSL_HANDSHAKE_OVER;
6782
6783 ssl->major_ver = MBEDTLS_SSL_MAJOR_VERSION_3;
6784 ssl->minor_ver = MBEDTLS_SSL_MINOR_VERSION_3;
6785
Hanno Becker361b10d2019-08-30 10:42:49 +01006786 /* Adjust pointers for header fields of outgoing records to
6787 * the given transform, accounting for explicit IV and CID. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01006788 mbedtls_ssl_update_out_pointers(ssl, ssl->transform);
Hanno Becker361b10d2019-08-30 10:42:49 +01006789
Manuel Pégourié-Gonnard0eb3eac2019-07-15 11:53:51 +02006790#if defined(MBEDTLS_SSL_PROTO_DTLS)
6791 ssl->in_epoch = 1;
6792#endif
6793
6794 /* mbedtls_ssl_reset() leaves the handshake sub-structure allocated,
6795 * which we don't want - otherwise we'd end up freeing the wrong transform
Hanno Beckerce5f5fd2020-02-05 10:47:44 +00006796 * by calling mbedtls_ssl_handshake_wrapup_free_hs_transform()
6797 * inappropriately. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01006798 if (ssl->handshake != NULL) {
6799 mbedtls_ssl_handshake_free(ssl);
6800 mbedtls_free(ssl->handshake);
Manuel Pégourié-Gonnard0eb3eac2019-07-15 11:53:51 +02006801 ssl->handshake = NULL;
6802 }
6803
6804 /*
Manuel Pégourié-Gonnard4c90e852019-07-11 10:58:10 +02006805 * Done - should have consumed entire buffer
6806 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01006807 if (p != end) {
6808 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
6809 }
Manuel Pégourié-Gonnardac87e282019-05-28 13:02:16 +02006810
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01006811 return 0;
Manuel Pégourié-Gonnardac87e282019-05-28 13:02:16 +02006812}
6813
6814/*
Manuel Pégourié-Gonnardb9dfc9f2019-07-12 10:50:19 +02006815 * Deserialize context: public wrapper for error cleaning
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02006816 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01006817int mbedtls_ssl_context_load(mbedtls_ssl_context *context,
6818 const unsigned char *buf,
6819 size_t len)
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02006820{
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01006821 int ret = ssl_context_load(context, buf, len);
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02006822
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01006823 if (ret != 0) {
6824 mbedtls_ssl_free(context);
6825 }
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02006826
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01006827 return ret;
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02006828}
Manuel Pégourié-Gonnard5c0e3772019-07-23 16:13:17 +02006829#endif /* MBEDTLS_SSL_CONTEXT_SERIALIZATION */
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02006830
6831/*
Paul Bakker5121ce52009-01-03 21:22:43 +00006832 * Free an SSL context
6833 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01006834void mbedtls_ssl_free(mbedtls_ssl_context *ssl)
Paul Bakker5121ce52009-01-03 21:22:43 +00006835{
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01006836 if (ssl == NULL) {
Paul Bakkeraccaffe2014-06-26 13:37:14 +02006837 return;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01006838 }
Paul Bakkeraccaffe2014-06-26 13:37:14 +02006839
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01006840 MBEDTLS_SSL_DEBUG_MSG(2, ("=> free"));
Paul Bakker5121ce52009-01-03 21:22:43 +00006841
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01006842 if (ssl->out_buf != NULL) {
sander-visserb8aa2072020-05-06 22:05:13 +02006843#if defined(MBEDTLS_SSL_VARIABLE_BUFFER_LENGTH)
6844 size_t out_buf_len = ssl->out_buf_len;
6845#else
6846 size_t out_buf_len = MBEDTLS_SSL_OUT_BUFFER_LEN;
6847#endif
6848
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01006849 mbedtls_platform_zeroize(ssl->out_buf, out_buf_len);
6850 mbedtls_free(ssl->out_buf);
Andrzej Kurek0afa2a12020-03-03 10:39:58 -05006851 ssl->out_buf = NULL;
Paul Bakker5121ce52009-01-03 21:22:43 +00006852 }
6853
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01006854 if (ssl->in_buf != NULL) {
sander-visserb8aa2072020-05-06 22:05:13 +02006855#if defined(MBEDTLS_SSL_VARIABLE_BUFFER_LENGTH)
6856 size_t in_buf_len = ssl->in_buf_len;
6857#else
6858 size_t in_buf_len = MBEDTLS_SSL_IN_BUFFER_LEN;
6859#endif
6860
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01006861 mbedtls_platform_zeroize(ssl->in_buf, in_buf_len);
6862 mbedtls_free(ssl->in_buf);
Andrzej Kurek0afa2a12020-03-03 10:39:58 -05006863 ssl->in_buf = NULL;
Paul Bakker5121ce52009-01-03 21:22:43 +00006864 }
6865
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006866#if defined(MBEDTLS_ZLIB_SUPPORT)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01006867 if (ssl->compress_buf != NULL) {
6868 mbedtls_platform_zeroize(ssl->compress_buf, MBEDTLS_SSL_COMPRESS_BUFFER_LEN);
6869 mbedtls_free(ssl->compress_buf);
Paul Bakker16770332013-10-11 09:59:44 +02006870 }
6871#endif
6872
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01006873 if (ssl->transform) {
6874 mbedtls_ssl_transform_free(ssl->transform);
6875 mbedtls_free(ssl->transform);
Paul Bakker48916f92012-09-16 19:57:18 +00006876 }
6877
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01006878 if (ssl->handshake) {
6879 mbedtls_ssl_handshake_free(ssl);
6880 mbedtls_ssl_transform_free(ssl->transform_negotiate);
6881 mbedtls_ssl_session_free(ssl->session_negotiate);
Paul Bakker48916f92012-09-16 19:57:18 +00006882
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01006883 mbedtls_free(ssl->handshake);
6884 mbedtls_free(ssl->transform_negotiate);
6885 mbedtls_free(ssl->session_negotiate);
Paul Bakker48916f92012-09-16 19:57:18 +00006886 }
6887
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01006888 if (ssl->session) {
6889 mbedtls_ssl_session_free(ssl->session);
6890 mbedtls_free(ssl->session);
Paul Bakkerc0463502013-02-14 11:19:38 +01006891 }
6892
Manuel Pégourié-Gonnard55fab2d2015-05-11 16:15:19 +02006893#if defined(MBEDTLS_X509_CRT_PARSE_C)
Gilles Peskine3a2f75d2025-02-12 23:28:48 +01006894 mbedtls_ssl_free_hostname(ssl);
Paul Bakker0be444a2013-08-27 21:55:01 +02006895#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00006896
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006897#if defined(MBEDTLS_SSL_HW_RECORD_ACCEL)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01006898 if (mbedtls_ssl_hw_record_finish != NULL) {
6899 MBEDTLS_SSL_DEBUG_MSG(2, ("going for mbedtls_ssl_hw_record_finish()"));
6900 mbedtls_ssl_hw_record_finish(ssl);
Paul Bakker05ef8352012-05-08 09:17:57 +00006901 }
6902#endif
6903
Manuel Pégourié-Gonnarde057d3b2015-05-20 10:59:43 +02006904#if defined(MBEDTLS_SSL_DTLS_HELLO_VERIFY) && defined(MBEDTLS_SSL_SRV_C)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01006905 mbedtls_free(ssl->cli_id);
Manuel Pégourié-Gonnard43c02182014-07-22 17:32:01 +02006906#endif
6907
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01006908 MBEDTLS_SSL_DEBUG_MSG(2, ("<= free"));
Paul Bakker2da561c2009-02-05 18:00:28 +00006909
Paul Bakker86f04f42013-02-14 11:20:09 +01006910 /* Actually clear after last debug message */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01006911 mbedtls_platform_zeroize(ssl, sizeof(mbedtls_ssl_context));
Paul Bakker5121ce52009-01-03 21:22:43 +00006912}
6913
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02006914/*
Tom Cosgrove49f99bc2022-12-04 16:44:21 +00006915 * Initialize mbedtls_ssl_config
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02006916 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01006917void mbedtls_ssl_config_init(mbedtls_ssl_config *conf)
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02006918{
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01006919 memset(conf, 0, sizeof(mbedtls_ssl_config));
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02006920}
6921
Gilles Peskineeccd8882020-03-10 12:19:08 +01006922#if defined(MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED)
Chien Wongb6d57932024-02-07 21:48:12 +08006923static const int ssl_preset_default_hashes[] = {
Gilles Peskinec54010c2021-05-12 22:52:09 +02006924#if defined(MBEDTLS_SHA512_C)
Manuel Pégourié-Gonnard47229c72015-12-04 15:02:56 +01006925 MBEDTLS_MD_SHA512,
Gilles Peskinec54010c2021-05-12 22:52:09 +02006926#endif
6927#if defined(MBEDTLS_SHA512_C) && !defined(MBEDTLS_SHA512_NO_SHA384)
Manuel Pégourié-Gonnard47229c72015-12-04 15:02:56 +01006928 MBEDTLS_MD_SHA384,
6929#endif
6930#if defined(MBEDTLS_SHA256_C)
6931 MBEDTLS_MD_SHA256,
6932 MBEDTLS_MD_SHA224,
6933#endif
Gilles Peskine5d2511c2017-05-12 13:16:40 +02006934#if defined(MBEDTLS_SHA1_C) && defined(MBEDTLS_TLS_DEFAULT_ALLOW_SHA1_IN_KEY_EXCHANGE)
Manuel Pégourié-Gonnard47229c72015-12-04 15:02:56 +01006935 MBEDTLS_MD_SHA1,
6936#endif
6937 MBEDTLS_MD_NONE
6938};
Simon Butcherc97b6972015-12-27 23:48:17 +00006939#endif
Manuel Pégourié-Gonnard47229c72015-12-04 15:02:56 +01006940
Chien Wongb6d57932024-02-07 21:48:12 +08006941static const int ssl_preset_suiteb_ciphersuites[] = {
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02006942 MBEDTLS_TLS_ECDHE_ECDSA_WITH_AES_128_GCM_SHA256,
6943 MBEDTLS_TLS_ECDHE_ECDSA_WITH_AES_256_GCM_SHA384,
6944 0
6945};
6946
Gilles Peskineeccd8882020-03-10 12:19:08 +01006947#if defined(MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED)
Chien Wongb6d57932024-02-07 21:48:12 +08006948static const int ssl_preset_suiteb_hashes[] = {
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02006949 MBEDTLS_MD_SHA256,
6950 MBEDTLS_MD_SHA384,
6951 MBEDTLS_MD_NONE
6952};
6953#endif
6954
6955#if defined(MBEDTLS_ECP_C)
Chien Wongb6d57932024-02-07 21:48:12 +08006956static const mbedtls_ecp_group_id ssl_preset_suiteb_curves[] = {
Jaeden Amerod4311042019-06-03 08:27:16 +01006957#if defined(MBEDTLS_ECP_DP_SECP256R1_ENABLED)
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02006958 MBEDTLS_ECP_DP_SECP256R1,
Jaeden Amerod4311042019-06-03 08:27:16 +01006959#endif
6960#if defined(MBEDTLS_ECP_DP_SECP384R1_ENABLED)
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02006961 MBEDTLS_ECP_DP_SECP384R1,
Jaeden Amerod4311042019-06-03 08:27:16 +01006962#endif
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02006963 MBEDTLS_ECP_DP_NONE
6964};
6965#endif
6966
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02006967/*
Tillmann Karras588ad502015-09-25 04:27:22 +02006968 * Load default in mbedtls_ssl_config
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02006969 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01006970int mbedtls_ssl_config_defaults(mbedtls_ssl_config *conf,
6971 int endpoint, int transport, int preset)
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02006972{
Manuel Pégourié-Gonnard8b431fb2015-05-11 12:54:52 +02006973#if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_SSL_SRV_C)
Janos Follath865b3eb2019-12-16 11:46:15 +00006974 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnard8b431fb2015-05-11 12:54:52 +02006975#endif
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02006976
Manuel Pégourié-Gonnard0de074f2015-05-14 12:58:01 +02006977 /* Use the functions here so that they are covered in tests,
6978 * but otherwise access member directly for efficiency */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01006979 mbedtls_ssl_conf_endpoint(conf, endpoint);
6980 mbedtls_ssl_conf_transport(conf, transport);
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02006981
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02006982 /*
6983 * Things that are common to all presets
6984 */
Manuel Pégourié-Gonnard419d5ae2015-05-04 19:32:36 +02006985#if defined(MBEDTLS_SSL_CLI_C)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01006986 if (endpoint == MBEDTLS_SSL_IS_CLIENT) {
Manuel Pégourié-Gonnard419d5ae2015-05-04 19:32:36 +02006987 conf->authmode = MBEDTLS_SSL_VERIFY_REQUIRED;
6988#if defined(MBEDTLS_SSL_SESSION_TICKETS)
6989 conf->session_tickets = MBEDTLS_SSL_SESSION_TICKETS_ENABLED;
6990#endif
6991 }
6992#endif
6993
Manuel Pégourié-Gonnard66dc5552015-05-14 12:28:21 +02006994#if defined(MBEDTLS_ARC4_C)
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02006995 conf->arc4_disabled = MBEDTLS_SSL_ARC4_DISABLED;
Manuel Pégourié-Gonnard66dc5552015-05-14 12:28:21 +02006996#endif
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02006997
6998#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
6999 conf->encrypt_then_mac = MBEDTLS_SSL_ETM_ENABLED;
7000#endif
7001
7002#if defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET)
7003 conf->extended_ms = MBEDTLS_SSL_EXTENDED_MS_ENABLED;
7004#endif
7005
Manuel Pégourié-Gonnard17eab2b2015-05-05 16:34:53 +01007006#if defined(MBEDTLS_SSL_CBC_RECORD_SPLITTING)
7007 conf->cbc_record_splitting = MBEDTLS_SSL_CBC_RECORD_SPLITTING_ENABLED;
7008#endif
7009
Manuel Pégourié-Gonnarde057d3b2015-05-20 10:59:43 +02007010#if defined(MBEDTLS_SSL_DTLS_HELLO_VERIFY) && defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02007011 conf->f_cookie_write = ssl_cookie_write_dummy;
7012 conf->f_cookie_check = ssl_cookie_check_dummy;
7013#endif
7014
7015#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
7016 conf->anti_replay = MBEDTLS_SSL_ANTI_REPLAY_ENABLED;
7017#endif
7018
Janos Follath088ce432017-04-10 12:42:31 +01007019#if defined(MBEDTLS_SSL_SRV_C)
7020 conf->cert_req_ca_list = MBEDTLS_SSL_CERT_REQ_CA_LIST_ENABLED;
7021#endif
7022
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02007023#if defined(MBEDTLS_SSL_PROTO_DTLS)
7024 conf->hs_timeout_min = MBEDTLS_SSL_DTLS_TIMEOUT_DFL_MIN;
7025 conf->hs_timeout_max = MBEDTLS_SSL_DTLS_TIMEOUT_DFL_MAX;
7026#endif
7027
7028#if defined(MBEDTLS_SSL_RENEGOTIATION)
7029 conf->renego_max_records = MBEDTLS_SSL_RENEGO_MAX_RECORDS_DEFAULT;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01007030 memset(conf->renego_period, 0x00, 2);
7031 memset(conf->renego_period + 2, 0xFF, 6);
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02007032#endif
7033
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02007034#if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_SSL_SRV_C)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01007035 if (endpoint == MBEDTLS_SSL_IS_SERVER) {
7036 const unsigned char dhm_p[] =
7037 MBEDTLS_DHM_RFC3526_MODP_2048_P_BIN;
7038 const unsigned char dhm_g[] =
7039 MBEDTLS_DHM_RFC3526_MODP_2048_G_BIN;
Hanno Becker00d0a682017-10-04 13:14:29 +01007040
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01007041 if ((ret = mbedtls_ssl_conf_dh_param_bin(conf,
7042 dhm_p, sizeof(dhm_p),
7043 dhm_g, sizeof(dhm_g))) != 0) {
7044 return ret;
7045 }
7046 }
Manuel Pégourié-Gonnardbd990d62015-06-11 14:49:42 +02007047#endif
7048
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02007049 /*
7050 * Preset-specific defaults
7051 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01007052 switch (preset) {
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02007053 /*
7054 * NSA Suite B
7055 */
7056 case MBEDTLS_SSL_PRESET_SUITEB:
7057 conf->min_major_ver = MBEDTLS_SSL_MAJOR_VERSION_3;
7058 conf->min_minor_ver = MBEDTLS_SSL_MINOR_VERSION_3; /* TLS 1.2 */
7059 conf->max_major_ver = MBEDTLS_SSL_MAX_MAJOR_VERSION;
7060 conf->max_minor_ver = MBEDTLS_SSL_MAX_MINOR_VERSION;
7061
7062 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_0] =
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01007063 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_1] =
7064 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_2] =
7065 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_3] =
7066 ssl_preset_suiteb_ciphersuites;
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02007067
7068#if defined(MBEDTLS_X509_CRT_PARSE_C)
7069 conf->cert_profile = &mbedtls_x509_crt_profile_suiteb;
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02007070#endif
7071
Gilles Peskineeccd8882020-03-10 12:19:08 +01007072#if defined(MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED)
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02007073 conf->sig_hashes = ssl_preset_suiteb_hashes;
7074#endif
7075
7076#if defined(MBEDTLS_ECP_C)
7077 conf->curve_list = ssl_preset_suiteb_curves;
7078#endif
Manuel Pégourié-Gonnardc98204e2015-08-11 04:21:01 +02007079 break;
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02007080
7081 /*
7082 * Default
7083 */
7084 default:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01007085 conf->min_major_ver = (MBEDTLS_SSL_MIN_MAJOR_VERSION >
7086 MBEDTLS_SSL_MIN_VALID_MAJOR_VERSION) ?
7087 MBEDTLS_SSL_MIN_MAJOR_VERSION :
7088 MBEDTLS_SSL_MIN_VALID_MAJOR_VERSION;
7089 conf->min_minor_ver = (MBEDTLS_SSL_MIN_MINOR_VERSION >
7090 MBEDTLS_SSL_MIN_VALID_MINOR_VERSION) ?
7091 MBEDTLS_SSL_MIN_MINOR_VERSION :
7092 MBEDTLS_SSL_MIN_VALID_MINOR_VERSION;
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02007093 conf->max_major_ver = MBEDTLS_SSL_MAX_MAJOR_VERSION;
7094 conf->max_minor_ver = MBEDTLS_SSL_MAX_MINOR_VERSION;
7095
7096#if defined(MBEDTLS_SSL_PROTO_DTLS)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01007097 if (transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM) {
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02007098 conf->min_minor_ver = MBEDTLS_SSL_MINOR_VERSION_2;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01007099 }
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02007100#endif
7101
7102 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_0] =
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01007103 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_1] =
7104 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_2] =
7105 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_3] =
7106 mbedtls_ssl_list_ciphersuites();
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02007107
7108#if defined(MBEDTLS_X509_CRT_PARSE_C)
7109 conf->cert_profile = &mbedtls_x509_crt_profile_default;
7110#endif
7111
Gilles Peskineeccd8882020-03-10 12:19:08 +01007112#if defined(MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED)
Manuel Pégourié-Gonnard47229c72015-12-04 15:02:56 +01007113 conf->sig_hashes = ssl_preset_default_hashes;
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02007114#endif
7115
7116#if defined(MBEDTLS_ECP_C)
7117 conf->curve_list = mbedtls_ecp_grp_id_list();
7118#endif
7119
7120#if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_SSL_CLI_C)
7121 conf->dhm_min_bitlen = 1024;
7122#endif
7123 }
7124
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01007125 return 0;
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02007126}
7127
7128/*
7129 * Free mbedtls_ssl_config
7130 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01007131void mbedtls_ssl_config_free(mbedtls_ssl_config *conf)
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02007132{
7133#if defined(MBEDTLS_DHM_C)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01007134 mbedtls_mpi_free(&conf->dhm_P);
7135 mbedtls_mpi_free(&conf->dhm_G);
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02007136#endif
7137
Gilles Peskineeccd8882020-03-10 12:19:08 +01007138#if defined(MBEDTLS_KEY_EXCHANGE_SOME_PSK_ENABLED)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01007139 if (conf->psk != NULL) {
7140 mbedtls_platform_zeroize(conf->psk, conf->psk_len);
7141 mbedtls_free(conf->psk);
Azim Khan27e8a122018-03-21 14:24:11 +00007142 conf->psk = NULL;
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02007143 conf->psk_len = 0;
junyeonLEE316b1622017-12-20 16:29:30 +09007144 }
7145
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01007146 if (conf->psk_identity != NULL) {
7147 mbedtls_platform_zeroize(conf->psk_identity, conf->psk_identity_len);
7148 mbedtls_free(conf->psk_identity);
Azim Khan27e8a122018-03-21 14:24:11 +00007149 conf->psk_identity = NULL;
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02007150 conf->psk_identity_len = 0;
7151 }
7152#endif
7153
7154#if defined(MBEDTLS_X509_CRT_PARSE_C)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01007155 ssl_key_cert_free(conf->key_cert);
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02007156#endif
7157
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01007158 mbedtls_platform_zeroize(conf, sizeof(mbedtls_ssl_config));
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02007159}
7160
Manuel Pégourié-Gonnard5674a972015-10-19 15:14:03 +02007161#if defined(MBEDTLS_PK_C) && \
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01007162 (defined(MBEDTLS_RSA_C) || defined(MBEDTLS_ECDSA_C))
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02007163/*
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007164 * Convert between MBEDTLS_PK_XXX and SSL_SIG_XXX
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02007165 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01007166unsigned char mbedtls_ssl_sig_from_pk(mbedtls_pk_context *pk)
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02007167{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007168#if defined(MBEDTLS_RSA_C)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01007169 if (mbedtls_pk_can_do(pk, MBEDTLS_PK_RSA)) {
7170 return MBEDTLS_SSL_SIG_RSA;
7171 }
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02007172#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007173#if defined(MBEDTLS_ECDSA_C)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01007174 if (mbedtls_pk_can_do(pk, MBEDTLS_PK_ECDSA)) {
7175 return MBEDTLS_SSL_SIG_ECDSA;
7176 }
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02007177#endif
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01007178 return MBEDTLS_SSL_SIG_ANON;
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02007179}
7180
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01007181unsigned char mbedtls_ssl_sig_from_pk_alg(mbedtls_pk_type_t type)
Hanno Becker7e5437a2017-04-28 17:15:26 +01007182{
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01007183 switch (type) {
Hanno Becker7e5437a2017-04-28 17:15:26 +01007184 case MBEDTLS_PK_RSA:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01007185 return MBEDTLS_SSL_SIG_RSA;
Hanno Becker7e5437a2017-04-28 17:15:26 +01007186 case MBEDTLS_PK_ECDSA:
7187 case MBEDTLS_PK_ECKEY:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01007188 return MBEDTLS_SSL_SIG_ECDSA;
Hanno Becker7e5437a2017-04-28 17:15:26 +01007189 default:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01007190 return MBEDTLS_SSL_SIG_ANON;
Hanno Becker7e5437a2017-04-28 17:15:26 +01007191 }
7192}
7193
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01007194mbedtls_pk_type_t mbedtls_ssl_pk_alg_from_sig(unsigned char sig)
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02007195{
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01007196 switch (sig) {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007197#if defined(MBEDTLS_RSA_C)
7198 case MBEDTLS_SSL_SIG_RSA:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01007199 return MBEDTLS_PK_RSA;
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02007200#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007201#if defined(MBEDTLS_ECDSA_C)
7202 case MBEDTLS_SSL_SIG_ECDSA:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01007203 return MBEDTLS_PK_ECDSA;
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02007204#endif
7205 default:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01007206 return MBEDTLS_PK_NONE;
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02007207 }
7208}
Manuel Pégourié-Gonnard5674a972015-10-19 15:14:03 +02007209#endif /* MBEDTLS_PK_C && ( MBEDTLS_RSA_C || MBEDTLS_ECDSA_C ) */
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02007210
Hanno Becker7e5437a2017-04-28 17:15:26 +01007211#if defined(MBEDTLS_SSL_PROTO_TLS1_2) && \
Gilles Peskineeccd8882020-03-10 12:19:08 +01007212 defined(MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED)
Hanno Becker7e5437a2017-04-28 17:15:26 +01007213
7214/* Find an entry in a signature-hash set matching a given hash algorithm. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01007215mbedtls_md_type_t mbedtls_ssl_sig_hash_set_find(mbedtls_ssl_sig_hash_set_t *set,
7216 mbedtls_pk_type_t sig_alg)
Hanno Becker7e5437a2017-04-28 17:15:26 +01007217{
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01007218 switch (sig_alg) {
Hanno Becker7e5437a2017-04-28 17:15:26 +01007219 case MBEDTLS_PK_RSA:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01007220 return set->rsa;
Hanno Becker7e5437a2017-04-28 17:15:26 +01007221 case MBEDTLS_PK_ECDSA:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01007222 return set->ecdsa;
Hanno Becker7e5437a2017-04-28 17:15:26 +01007223 default:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01007224 return MBEDTLS_MD_NONE;
Hanno Becker7e5437a2017-04-28 17:15:26 +01007225 }
7226}
7227
7228/* Add a signature-hash-pair to a signature-hash set */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01007229void mbedtls_ssl_sig_hash_set_add(mbedtls_ssl_sig_hash_set_t *set,
7230 mbedtls_pk_type_t sig_alg,
7231 mbedtls_md_type_t md_alg)
Hanno Becker7e5437a2017-04-28 17:15:26 +01007232{
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01007233 switch (sig_alg) {
Hanno Becker7e5437a2017-04-28 17:15:26 +01007234 case MBEDTLS_PK_RSA:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01007235 if (set->rsa == MBEDTLS_MD_NONE) {
Hanno Becker7e5437a2017-04-28 17:15:26 +01007236 set->rsa = md_alg;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01007237 }
Hanno Becker7e5437a2017-04-28 17:15:26 +01007238 break;
7239
7240 case MBEDTLS_PK_ECDSA:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01007241 if (set->ecdsa == MBEDTLS_MD_NONE) {
Hanno Becker7e5437a2017-04-28 17:15:26 +01007242 set->ecdsa = md_alg;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01007243 }
Hanno Becker7e5437a2017-04-28 17:15:26 +01007244 break;
7245
7246 default:
7247 break;
7248 }
7249}
7250
7251/* Allow exactly one hash algorithm for each signature. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01007252void mbedtls_ssl_sig_hash_set_const_hash(mbedtls_ssl_sig_hash_set_t *set,
7253 mbedtls_md_type_t md_alg)
Hanno Becker7e5437a2017-04-28 17:15:26 +01007254{
7255 set->rsa = md_alg;
7256 set->ecdsa = md_alg;
7257}
7258
7259#endif /* MBEDTLS_SSL_PROTO_TLS1_2) &&
Gilles Peskineeccd8882020-03-10 12:19:08 +01007260 MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED */
Hanno Becker7e5437a2017-04-28 17:15:26 +01007261
Manuel Pégourié-Gonnard1a483832013-09-20 12:29:15 +02007262/*
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +02007263 * Convert from MBEDTLS_SSL_HASH_XXX to MBEDTLS_MD_XXX
Manuel Pégourié-Gonnard1a483832013-09-20 12:29:15 +02007264 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01007265mbedtls_md_type_t mbedtls_ssl_md_alg_from_hash(unsigned char hash)
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02007266{
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01007267 switch (hash) {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007268#if defined(MBEDTLS_MD5_C)
7269 case MBEDTLS_SSL_HASH_MD5:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01007270 return MBEDTLS_MD_MD5;
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02007271#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007272#if defined(MBEDTLS_SHA1_C)
7273 case MBEDTLS_SSL_HASH_SHA1:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01007274 return MBEDTLS_MD_SHA1;
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02007275#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007276#if defined(MBEDTLS_SHA256_C)
7277 case MBEDTLS_SSL_HASH_SHA224:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01007278 return MBEDTLS_MD_SHA224;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007279 case MBEDTLS_SSL_HASH_SHA256:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01007280 return MBEDTLS_MD_SHA256;
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02007281#endif
Gilles Peskined2d59372021-05-12 22:43:27 +02007282#if defined(MBEDTLS_SHA512_C) && !defined(MBEDTLS_SHA512_NO_SHA384)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007283 case MBEDTLS_SSL_HASH_SHA384:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01007284 return MBEDTLS_MD_SHA384;
Gilles Peskinec54010c2021-05-12 22:52:09 +02007285#endif
7286#if defined(MBEDTLS_SHA512_C)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007287 case MBEDTLS_SSL_HASH_SHA512:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01007288 return MBEDTLS_MD_SHA512;
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02007289#endif
7290 default:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01007291 return MBEDTLS_MD_NONE;
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02007292 }
7293}
7294
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +02007295/*
7296 * Convert from MBEDTLS_MD_XXX to MBEDTLS_SSL_HASH_XXX
7297 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01007298unsigned char mbedtls_ssl_hash_from_md_alg(int md)
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +02007299{
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01007300 switch (md) {
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +02007301#if defined(MBEDTLS_MD5_C)
7302 case MBEDTLS_MD_MD5:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01007303 return MBEDTLS_SSL_HASH_MD5;
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +02007304#endif
7305#if defined(MBEDTLS_SHA1_C)
7306 case MBEDTLS_MD_SHA1:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01007307 return MBEDTLS_SSL_HASH_SHA1;
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +02007308#endif
7309#if defined(MBEDTLS_SHA256_C)
7310 case MBEDTLS_MD_SHA224:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01007311 return MBEDTLS_SSL_HASH_SHA224;
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +02007312 case MBEDTLS_MD_SHA256:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01007313 return MBEDTLS_SSL_HASH_SHA256;
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +02007314#endif
Gilles Peskined2d59372021-05-12 22:43:27 +02007315#if defined(MBEDTLS_SHA512_C) && !defined(MBEDTLS_SHA512_NO_SHA384)
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +02007316 case MBEDTLS_MD_SHA384:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01007317 return MBEDTLS_SSL_HASH_SHA384;
Gilles Peskinec54010c2021-05-12 22:52:09 +02007318#endif
7319#if defined(MBEDTLS_SHA512_C)
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +02007320 case MBEDTLS_MD_SHA512:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01007321 return MBEDTLS_SSL_HASH_SHA512;
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +02007322#endif
7323 default:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01007324 return MBEDTLS_SSL_HASH_NONE;
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +02007325 }
7326}
7327
Manuel Pégourié-Gonnardb541da62015-06-17 11:43:30 +02007328#if defined(MBEDTLS_ECP_C)
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01007329/*
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +02007330 * Check if a curve proposed by the peer is in our list.
Manuel Pégourié-Gonnard9d412d82015-06-17 12:10:46 +02007331 * Return 0 if we're willing to use it, -1 otherwise.
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01007332 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01007333int mbedtls_ssl_check_curve(const mbedtls_ssl_context *ssl, mbedtls_ecp_group_id grp_id)
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01007334{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007335 const mbedtls_ecp_group_id *gid;
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01007336
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01007337 if (ssl->conf->curve_list == NULL) {
7338 return -1;
7339 }
Manuel Pégourié-Gonnard9d412d82015-06-17 12:10:46 +02007340
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01007341 for (gid = ssl->conf->curve_list; *gid != MBEDTLS_ECP_DP_NONE; gid++) {
7342 if (*gid == grp_id) {
7343 return 0;
7344 }
7345 }
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01007346
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01007347 return -1;
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01007348}
Manuel Pégourié-Gonnard298d6cc2022-02-14 11:34:47 +01007349
7350/*
7351 * Same as mbedtls_ssl_check_curve() but takes a TLS ID for the curve.
7352 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01007353int mbedtls_ssl_check_curve_tls_id(const mbedtls_ssl_context *ssl, uint16_t tls_id)
Manuel Pégourié-Gonnard298d6cc2022-02-14 11:34:47 +01007354{
7355 const mbedtls_ecp_curve_info *curve_info =
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01007356 mbedtls_ecp_curve_info_from_tls_id(tls_id);
7357 if (curve_info == NULL) {
7358 return -1;
7359 }
7360 return mbedtls_ssl_check_curve(ssl, curve_info->grp_id);
Manuel Pégourié-Gonnard298d6cc2022-02-14 11:34:47 +01007361}
Manuel Pégourié-Gonnardb541da62015-06-17 11:43:30 +02007362#endif /* MBEDTLS_ECP_C */
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02007363
Gilles Peskineeccd8882020-03-10 12:19:08 +01007364#if defined(MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED)
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +02007365/*
7366 * Check if a hash proposed by the peer is in our list.
7367 * Return 0 if we're willing to use it, -1 otherwise.
7368 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01007369int mbedtls_ssl_check_sig_hash(const mbedtls_ssl_context *ssl,
7370 mbedtls_md_type_t md)
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +02007371{
7372 const int *cur;
7373
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01007374 if (ssl->conf->sig_hashes == NULL) {
7375 return -1;
7376 }
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +02007377
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01007378 for (cur = ssl->conf->sig_hashes; *cur != MBEDTLS_MD_NONE; cur++) {
7379 if (*cur == (int) md) {
7380 return 0;
7381 }
7382 }
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +02007383
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01007384 return -1;
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +02007385}
Gilles Peskineeccd8882020-03-10 12:19:08 +01007386#endif /* MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED */
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +02007387
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007388#if defined(MBEDTLS_X509_CRT_PARSE_C)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01007389int mbedtls_ssl_check_cert_usage(const mbedtls_x509_crt *cert,
7390 const mbedtls_ssl_ciphersuite_t *ciphersuite,
7391 int cert_endpoint,
7392 uint32_t *flags)
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02007393{
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +01007394 int ret = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007395#if defined(MBEDTLS_X509_CHECK_KEY_USAGE)
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02007396 int usage = 0;
7397#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007398#if defined(MBEDTLS_X509_CHECK_EXTENDED_KEY_USAGE)
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +02007399 const char *ext_oid;
7400 size_t ext_len;
7401#endif
7402
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007403#if !defined(MBEDTLS_X509_CHECK_KEY_USAGE) && \
7404 !defined(MBEDTLS_X509_CHECK_EXTENDED_KEY_USAGE)
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +02007405 ((void) cert);
7406 ((void) cert_endpoint);
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +01007407 ((void) flags);
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +02007408#endif
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02007409
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007410#if defined(MBEDTLS_X509_CHECK_KEY_USAGE)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01007411 if (cert_endpoint == MBEDTLS_SSL_IS_SERVER) {
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02007412 /* Server part of the key exchange */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01007413 switch (ciphersuite->key_exchange) {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007414 case MBEDTLS_KEY_EXCHANGE_RSA:
7415 case MBEDTLS_KEY_EXCHANGE_RSA_PSK:
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +01007416 usage = MBEDTLS_X509_KU_KEY_ENCIPHERMENT;
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_DHE_RSA:
7420 case MBEDTLS_KEY_EXCHANGE_ECDHE_RSA:
7421 case MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA:
7422 usage = MBEDTLS_X509_KU_DIGITAL_SIGNATURE;
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02007423 break;
7424
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007425 case MBEDTLS_KEY_EXCHANGE_ECDH_RSA:
7426 case MBEDTLS_KEY_EXCHANGE_ECDH_ECDSA:
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +01007427 usage = MBEDTLS_X509_KU_KEY_AGREEMENT;
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02007428 break;
7429
7430 /* Don't use default: we want warnings when adding new values */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007431 case MBEDTLS_KEY_EXCHANGE_NONE:
7432 case MBEDTLS_KEY_EXCHANGE_PSK:
7433 case MBEDTLS_KEY_EXCHANGE_DHE_PSK:
7434 case MBEDTLS_KEY_EXCHANGE_ECDHE_PSK:
Manuel Pégourié-Gonnard557535d2015-09-15 17:53:32 +02007435 case MBEDTLS_KEY_EXCHANGE_ECJPAKE:
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02007436 usage = 0;
7437 }
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01007438 } else {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007439 /* Client auth: we only implement rsa_sign and mbedtls_ecdsa_sign for now */
7440 usage = MBEDTLS_X509_KU_DIGITAL_SIGNATURE;
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02007441 }
7442
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01007443 if (mbedtls_x509_crt_check_key_usage(cert, usage) != 0) {
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +01007444 *flags |= MBEDTLS_X509_BADCERT_KEY_USAGE;
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +01007445 ret = -1;
7446 }
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +02007447#else
7448 ((void) ciphersuite);
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007449#endif /* MBEDTLS_X509_CHECK_KEY_USAGE */
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02007450
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007451#if defined(MBEDTLS_X509_CHECK_EXTENDED_KEY_USAGE)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01007452 if (cert_endpoint == MBEDTLS_SSL_IS_SERVER) {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007453 ext_oid = MBEDTLS_OID_SERVER_AUTH;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01007454 ext_len = MBEDTLS_OID_SIZE(MBEDTLS_OID_SERVER_AUTH);
7455 } else {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007456 ext_oid = MBEDTLS_OID_CLIENT_AUTH;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01007457 ext_len = MBEDTLS_OID_SIZE(MBEDTLS_OID_CLIENT_AUTH);
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +02007458 }
7459
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01007460 if (mbedtls_x509_crt_check_extended_key_usage(cert, ext_oid, ext_len) != 0) {
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +01007461 *flags |= MBEDTLS_X509_BADCERT_EXT_KEY_USAGE;
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +01007462 ret = -1;
7463 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007464#endif /* MBEDTLS_X509_CHECK_EXTENDED_KEY_USAGE */
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +02007465
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01007466 return ret;
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02007467}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007468#endif /* MBEDTLS_X509_CRT_PARSE_C */
Manuel Pégourié-Gonnard3a306b92014-04-29 15:11:17 +02007469
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01007470int mbedtls_ssl_set_calc_verify_md(mbedtls_ssl_context *ssl, int md)
Simon Butcher99000142016-10-13 17:21:01 +01007471{
7472#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01007473 if (ssl->minor_ver != MBEDTLS_SSL_MINOR_VERSION_3) {
Simon Butcher99000142016-10-13 17:21:01 +01007474 return MBEDTLS_ERR_SSL_INVALID_VERIFY_HASH;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01007475 }
Simon Butcher99000142016-10-13 17:21:01 +01007476
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01007477 switch (md) {
Simon Butcher99000142016-10-13 17:21:01 +01007478#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1)
7479#if defined(MBEDTLS_MD5_C)
7480 case MBEDTLS_SSL_HASH_MD5:
Janos Follath182013f2016-10-25 10:50:22 +01007481 return MBEDTLS_ERR_SSL_INVALID_VERIFY_HASH;
Simon Butcher99000142016-10-13 17:21:01 +01007482#endif
7483#if defined(MBEDTLS_SHA1_C)
7484 case MBEDTLS_SSL_HASH_SHA1:
7485 ssl->handshake->calc_verify = ssl_calc_verify_tls;
7486 break;
7487#endif
7488#endif /* MBEDTLS_SSL_PROTO_TLS1 || MBEDTLS_SSL_PROTO_TLS1_1 */
Gilles Peskined2d59372021-05-12 22:43:27 +02007489#if defined(MBEDTLS_SHA512_C) && !defined(MBEDTLS_SHA512_NO_SHA384)
Simon Butcher99000142016-10-13 17:21:01 +01007490 case MBEDTLS_SSL_HASH_SHA384:
7491 ssl->handshake->calc_verify = ssl_calc_verify_tls_sha384;
7492 break;
7493#endif
7494#if defined(MBEDTLS_SHA256_C)
7495 case MBEDTLS_SSL_HASH_SHA256:
7496 ssl->handshake->calc_verify = ssl_calc_verify_tls_sha256;
7497 break;
7498#endif
7499 default:
7500 return MBEDTLS_ERR_SSL_INVALID_VERIFY_HASH;
7501 }
7502
7503 return 0;
7504#else /* !MBEDTLS_SSL_PROTO_TLS1_2 */
7505 (void) ssl;
7506 (void) md;
7507
7508 return MBEDTLS_ERR_SSL_INVALID_VERIFY_HASH;
7509#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
7510}
7511
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +01007512#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1) || \
7513 defined(MBEDTLS_SSL_PROTO_TLS1_1)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01007514int mbedtls_ssl_get_key_exchange_md_ssl_tls(mbedtls_ssl_context *ssl,
7515 unsigned char *output,
7516 unsigned char *data, size_t data_len)
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +01007517{
7518 int ret = 0;
7519 mbedtls_md5_context mbedtls_md5;
7520 mbedtls_sha1_context mbedtls_sha1;
7521
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01007522 mbedtls_md5_init(&mbedtls_md5);
7523 mbedtls_sha1_init(&mbedtls_sha1);
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +01007524
7525 /*
7526 * digitally-signed struct {
7527 * opaque md5_hash[16];
7528 * opaque sha_hash[20];
7529 * };
7530 *
7531 * md5_hash
7532 * MD5(ClientHello.random + ServerHello.random
7533 * + ServerParams);
7534 * sha_hash
7535 * SHA(ClientHello.random + ServerHello.random
7536 * + ServerParams);
7537 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01007538 if ((ret = mbedtls_md5_starts_ret(&mbedtls_md5)) != 0) {
7539 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_md5_starts_ret", ret);
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +01007540 goto exit;
7541 }
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01007542 if ((ret = mbedtls_md5_update_ret(&mbedtls_md5,
7543 ssl->handshake->randbytes, 64)) != 0) {
7544 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_md5_update_ret", ret);
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +01007545 goto exit;
7546 }
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01007547 if ((ret = mbedtls_md5_update_ret(&mbedtls_md5, data, data_len)) != 0) {
7548 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_md5_update_ret", ret);
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +01007549 goto exit;
7550 }
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01007551 if ((ret = mbedtls_md5_finish_ret(&mbedtls_md5, output)) != 0) {
7552 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_md5_finish_ret", ret);
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +01007553 goto exit;
7554 }
7555
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01007556 if ((ret = mbedtls_sha1_starts_ret(&mbedtls_sha1)) != 0) {
7557 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_sha1_starts_ret", ret);
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +01007558 goto exit;
7559 }
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01007560 if ((ret = mbedtls_sha1_update_ret(&mbedtls_sha1,
7561 ssl->handshake->randbytes, 64)) != 0) {
7562 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_sha1_update_ret", ret);
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +01007563 goto exit;
7564 }
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01007565 if ((ret = mbedtls_sha1_update_ret(&mbedtls_sha1, data,
7566 data_len)) != 0) {
7567 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_sha1_update_ret", ret);
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +01007568 goto exit;
7569 }
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01007570 if ((ret = mbedtls_sha1_finish_ret(&mbedtls_sha1,
7571 output + 16)) != 0) {
7572 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_sha1_finish_ret", ret);
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +01007573 goto exit;
7574 }
7575
7576exit:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01007577 mbedtls_md5_free(&mbedtls_md5);
7578 mbedtls_sha1_free(&mbedtls_sha1);
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +01007579
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01007580 if (ret != 0) {
7581 mbedtls_ssl_send_alert_message(ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
7582 MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR);
7583 }
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +01007584
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01007585 return ret;
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +01007586
7587}
7588#endif /* MBEDTLS_SSL_PROTO_SSL3 || MBEDTLS_SSL_PROTO_TLS1 || \
7589 MBEDTLS_SSL_PROTO_TLS1_1 */
7590
7591#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1) || \
7592 defined(MBEDTLS_SSL_PROTO_TLS1_2)
Andrzej Kurekd6db9be2019-01-10 05:27:10 -05007593
7594#if defined(MBEDTLS_USE_PSA_CRYPTO)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01007595int mbedtls_ssl_get_key_exchange_md_tls1_2(mbedtls_ssl_context *ssl,
7596 unsigned char *hash, size_t *hashlen,
7597 unsigned char *data, size_t data_len,
7598 mbedtls_md_type_t md_alg)
Andrzej Kurekd6db9be2019-01-10 05:27:10 -05007599{
Andrzej Kurek814feff2019-01-14 04:35:19 -05007600 psa_status_t status;
Jaeden Amero34973232019-02-20 10:32:28 +00007601 psa_hash_operation_t hash_operation = PSA_HASH_OPERATION_INIT;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01007602 psa_algorithm_t hash_alg = mbedtls_psa_translate_md(md_alg);
Andrzej Kurekd6db9be2019-01-10 05:27:10 -05007603
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01007604 MBEDTLS_SSL_DEBUG_MSG(3, ("Perform PSA-based computation of digest of ServerKeyExchange"));
Andrzej Kurek814feff2019-01-14 04:35:19 -05007605
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01007606 if ((status = psa_hash_setup(&hash_operation,
7607 hash_alg)) != PSA_SUCCESS) {
7608 MBEDTLS_SSL_DEBUG_RET(1, "psa_hash_setup", 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, ssl->handshake->randbytes,
7613 64)) != 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_update(&hash_operation,
7619 data, data_len)) != PSA_SUCCESS) {
7620 MBEDTLS_SSL_DEBUG_RET(1, "psa_hash_update", status);
Andrzej Kurekd6db9be2019-01-10 05:27:10 -05007621 goto exit;
7622 }
7623
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01007624 if ((status = psa_hash_finish(&hash_operation, hash, PSA_HASH_MAX_SIZE,
7625 hashlen)) != PSA_SUCCESS) {
7626 MBEDTLS_SSL_DEBUG_RET(1, "psa_hash_finish", status);
7627 goto exit;
Andrzej Kurekd6db9be2019-01-10 05:27:10 -05007628 }
7629
7630exit:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01007631 if (status != PSA_SUCCESS) {
7632 mbedtls_ssl_send_alert_message(ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
7633 MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR);
7634 switch (status) {
Andrzej Kurekd6db9be2019-01-10 05:27:10 -05007635 case PSA_ERROR_NOT_SUPPORTED:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01007636 return MBEDTLS_ERR_MD_FEATURE_UNAVAILABLE;
Andrzej Kurek814feff2019-01-14 04:35:19 -05007637 case PSA_ERROR_BAD_STATE: /* Intentional fallthrough */
Andrzej Kurekd6db9be2019-01-10 05:27:10 -05007638 case PSA_ERROR_BUFFER_TOO_SMALL:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01007639 return MBEDTLS_ERR_MD_BAD_INPUT_DATA;
Andrzej Kurekd6db9be2019-01-10 05:27:10 -05007640 case PSA_ERROR_INSUFFICIENT_MEMORY:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01007641 return MBEDTLS_ERR_MD_ALLOC_FAILED;
Andrzej Kurekd6db9be2019-01-10 05:27:10 -05007642 default:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01007643 return MBEDTLS_ERR_MD_HW_ACCEL_FAILED;
Andrzej Kurekd6db9be2019-01-10 05:27:10 -05007644 }
7645 }
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01007646 return 0;
Andrzej Kurekd6db9be2019-01-10 05:27:10 -05007647}
7648
7649#else
7650
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01007651int mbedtls_ssl_get_key_exchange_md_tls1_2(mbedtls_ssl_context *ssl,
7652 unsigned char *hash, size_t *hashlen,
7653 unsigned char *data, size_t data_len,
7654 mbedtls_md_type_t md_alg)
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +01007655{
7656 int ret = 0;
7657 mbedtls_md_context_t ctx;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01007658 const mbedtls_md_info_t *md_info = mbedtls_md_info_from_type(md_alg);
7659 *hashlen = mbedtls_md_get_size(md_info);
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +01007660
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01007661 MBEDTLS_SSL_DEBUG_MSG(3, ("Perform mbedtls-based computation of digest of ServerKeyExchange"));
Andrzej Kurek814feff2019-01-14 04:35:19 -05007662
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01007663 mbedtls_md_init(&ctx);
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +01007664
7665 /*
7666 * digitally-signed struct {
7667 * opaque client_random[32];
7668 * opaque server_random[32];
7669 * ServerDHParams params;
7670 * };
7671 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01007672 if ((ret = mbedtls_md_setup(&ctx, md_info, 0)) != 0) {
7673 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_md_setup", ret);
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +01007674 goto exit;
7675 }
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01007676 if ((ret = mbedtls_md_starts(&ctx)) != 0) {
7677 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_md_starts", ret);
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +01007678 goto exit;
7679 }
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01007680 if ((ret = mbedtls_md_update(&ctx, ssl->handshake->randbytes, 64)) != 0) {
7681 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_md_update", ret);
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +01007682 goto exit;
7683 }
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01007684 if ((ret = mbedtls_md_update(&ctx, data, data_len)) != 0) {
7685 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_md_update", ret);
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +01007686 goto exit;
7687 }
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01007688 if ((ret = mbedtls_md_finish(&ctx, hash)) != 0) {
7689 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_md_finish", ret);
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +01007690 goto exit;
7691 }
7692
7693exit:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01007694 mbedtls_md_free(&ctx);
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +01007695
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01007696 if (ret != 0) {
7697 mbedtls_ssl_send_alert_message(ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
7698 MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR);
7699 }
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +01007700
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01007701 return ret;
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +01007702}
Andrzej Kurekd6db9be2019-01-10 05:27:10 -05007703#endif /* MBEDTLS_USE_PSA_CRYPTO */
7704
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +01007705#endif /* MBEDTLS_SSL_PROTO_TLS1 || MBEDTLS_SSL_PROTO_TLS1_1 || \
7706 MBEDTLS_SSL_PROTO_TLS1_2 */
7707
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007708#endif /* MBEDTLS_SSL_TLS_C */