blob: c3e005b188cfa01791bda66ffcac774035c9911f [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)
42int mbedtls_ssl_set_hostname(mbedtls_ssl_context *ssl, const char *hostname)
43{
44 /* Initialize to suppress unnecessary compiler warning */
45 size_t hostname_len = 0;
46
47 /* Check if new hostname is valid before
48 * making any change to current one */
49 if (hostname != NULL) {
50 hostname_len = strlen(hostname);
51
52 if (hostname_len > MBEDTLS_SSL_MAX_HOST_NAME_LEN) {
53 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
54 }
55 }
56
57 /* Now it's clear that we will overwrite the old hostname,
58 * so we can free it safely */
59
60 if (ssl->hostname != NULL) {
61 mbedtls_platform_zeroize(ssl->hostname, strlen(ssl->hostname));
62 mbedtls_free(ssl->hostname);
63 }
64
65 /* Passing NULL as hostname shall clear the old one */
66
67 if (hostname == NULL) {
68 ssl->hostname = NULL;
69 } else {
70 ssl->hostname = mbedtls_calloc(1, hostname_len + 1);
71 if (ssl->hostname == NULL) {
72 return MBEDTLS_ERR_SSL_ALLOC_FAILED;
73 }
74
75 memcpy(ssl->hostname, hostname, hostname_len);
76
77 ssl->hostname[hostname_len] = '\0';
78 }
79
80 return 0;
81}
82#endif /* MBEDTLS_X509_CRT_PARSE_C */
83
Manuel Pégourié-Gonnard286a1362015-05-13 16:22:05 +020084#if defined(MBEDTLS_SSL_PROTO_DTLS)
Hanno Becker2b1e3542018-08-06 11:19:13 +010085
Hanno Beckera0e20d02019-05-15 14:03:01 +010086#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Beckerf8542cf2019-04-09 15:22:03 +010087/* Top-level Connection ID API */
88
Gilles Peskine1b6c09a2023-01-11 14:52:35 +010089int mbedtls_ssl_conf_cid(mbedtls_ssl_config *conf,
90 size_t len,
91 int ignore_other_cid)
Hanno Beckerad4a1372019-05-03 13:06:44 +010092{
Gilles Peskine1b6c09a2023-01-11 14:52:35 +010093 if (len > MBEDTLS_SSL_CID_IN_LEN_MAX) {
94 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
95 }
Hanno Beckerad4a1372019-05-03 13:06:44 +010096
Gilles Peskine1b6c09a2023-01-11 14:52:35 +010097 if (ignore_other_cid != MBEDTLS_SSL_UNEXPECTED_CID_FAIL &&
98 ignore_other_cid != MBEDTLS_SSL_UNEXPECTED_CID_IGNORE) {
99 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
Hanno Becker611ac772019-05-14 11:45:26 +0100100 }
101
102 conf->ignore_unexpected_cid = ignore_other_cid;
Hanno Beckerad4a1372019-05-03 13:06:44 +0100103 conf->cid_len = len;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100104 return 0;
Hanno Beckerad4a1372019-05-03 13:06:44 +0100105}
106
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100107int mbedtls_ssl_set_cid(mbedtls_ssl_context *ssl,
108 int enable,
109 unsigned char const *own_cid,
110 size_t own_cid_len)
Hanno Beckerf8542cf2019-04-09 15:22:03 +0100111{
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100112 if (ssl->conf->transport != MBEDTLS_SSL_TRANSPORT_DATAGRAM) {
113 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
114 }
Hanno Becker76a79ab2019-05-03 14:38:32 +0100115
Hanno Beckerca092242019-04-25 16:01:49 +0100116 ssl->negotiate_cid = enable;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100117 if (enable == MBEDTLS_SSL_CID_DISABLED) {
118 MBEDTLS_SSL_DEBUG_MSG(3, ("Disable use of CID extension."));
119 return 0;
Hanno Beckerca092242019-04-25 16:01:49 +0100120 }
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100121 MBEDTLS_SSL_DEBUG_MSG(3, ("Enable use of CID extension."));
122 MBEDTLS_SSL_DEBUG_BUF(3, "Own CID", own_cid, own_cid_len);
Hanno Beckerca092242019-04-25 16:01:49 +0100123
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100124 if (own_cid_len != ssl->conf->cid_len) {
125 MBEDTLS_SSL_DEBUG_MSG(3, ("CID length %u does not match CID length %u in config",
126 (unsigned) own_cid_len,
127 (unsigned) ssl->conf->cid_len));
128 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
Hanno Beckerca092242019-04-25 16:01:49 +0100129 }
130
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100131 memcpy(ssl->own_cid, own_cid, own_cid_len);
Hanno Beckerb7ee0cf2019-04-30 14:07:31 +0100132 /* Truncation is not an issue here because
133 * MBEDTLS_SSL_CID_IN_LEN_MAX at most 255. */
134 ssl->own_cid_len = (uint8_t) own_cid_len;
Hanno Beckerca092242019-04-25 16:01:49 +0100135
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100136 return 0;
Hanno Beckerf8542cf2019-04-09 15:22:03 +0100137}
138
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100139int mbedtls_ssl_get_peer_cid(mbedtls_ssl_context *ssl,
140 int *enabled,
141 unsigned char peer_cid[MBEDTLS_SSL_CID_OUT_LEN_MAX],
142 size_t *peer_cid_len)
Hanno Beckerf8542cf2019-04-09 15:22:03 +0100143{
Hanno Beckerf8542cf2019-04-09 15:22:03 +0100144 *enabled = MBEDTLS_SSL_CID_DISABLED;
Hanno Beckerb1f89cd2019-04-26 17:08:02 +0100145
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100146 if (ssl->conf->transport != MBEDTLS_SSL_TRANSPORT_DATAGRAM ||
147 ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER) {
148 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
Hanno Becker76a79ab2019-05-03 14:38:32 +0100149 }
Hanno Beckerb1f89cd2019-04-26 17:08:02 +0100150
Hanno Beckerc5f24222019-05-03 12:54:52 +0100151 /* We report MBEDTLS_SSL_CID_DISABLED in case the CID extensions
152 * were used, but client and server requested the empty CID.
153 * This is indistinguishable from not using the CID extension
154 * in the first place. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100155 if (ssl->transform_in->in_cid_len == 0 &&
156 ssl->transform_in->out_cid_len == 0) {
157 return 0;
Hanno Beckerb1f89cd2019-04-26 17:08:02 +0100158 }
159
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100160 if (peer_cid_len != NULL) {
Hanno Becker615ef172019-05-22 16:50:35 +0100161 *peer_cid_len = ssl->transform_in->out_cid_len;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100162 if (peer_cid != NULL) {
163 memcpy(peer_cid, ssl->transform_in->out_cid,
164 ssl->transform_in->out_cid_len);
Hanno Becker615ef172019-05-22 16:50:35 +0100165 }
166 }
Hanno Beckerb1f89cd2019-04-26 17:08:02 +0100167
168 *enabled = MBEDTLS_SSL_CID_ENABLED;
169
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100170 return 0;
Hanno Beckerf8542cf2019-04-09 15:22:03 +0100171}
Hanno Beckera0e20d02019-05-15 14:03:01 +0100172#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Beckerf8542cf2019-04-09 15:22:03 +0100173
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200174#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +0200175
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200176#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
Manuel Pégourié-Gonnard581e6b62013-07-18 12:32:27 +0200177/*
178 * Convert max_fragment_length codes to length.
179 * RFC 6066 says:
180 * enum{
181 * 2^9(1), 2^10(2), 2^11(3), 2^12(4), (255)
182 * } MaxFragmentLength;
183 * and we add 0 -> extension unused
184 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100185static unsigned int ssl_mfl_code_to_length(int mfl)
Manuel Pégourié-Gonnard581e6b62013-07-18 12:32:27 +0200186{
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100187 switch (mfl) {
188 case MBEDTLS_SSL_MAX_FRAG_LEN_NONE:
189 return MBEDTLS_TLS_EXT_ADV_CONTENT_LEN;
190 case MBEDTLS_SSL_MAX_FRAG_LEN_512:
191 return 512;
192 case MBEDTLS_SSL_MAX_FRAG_LEN_1024:
193 return 1024;
194 case MBEDTLS_SSL_MAX_FRAG_LEN_2048:
195 return 2048;
196 case MBEDTLS_SSL_MAX_FRAG_LEN_4096:
197 return 4096;
198 default:
199 return MBEDTLS_TLS_EXT_ADV_CONTENT_LEN;
Angus Grattond8213d02016-05-25 20:56:48 +1000200 }
201}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200202#endif /* MBEDTLS_SSL_MAX_FRAGMENT_LENGTH */
Manuel Pégourié-Gonnard581e6b62013-07-18 12:32:27 +0200203
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100204int mbedtls_ssl_session_copy(mbedtls_ssl_session *dst,
205 const mbedtls_ssl_session *src)
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200206{
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100207 mbedtls_ssl_session_free(dst);
208 memcpy(dst, src, sizeof(mbedtls_ssl_session));
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200209
吴敬辉0f6c6bc2021-11-29 10:46:35 +0800210#if defined(MBEDTLS_SSL_SESSION_TICKETS) && defined(MBEDTLS_SSL_CLI_C)
211 dst->ticket = NULL;
212#endif
213
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200214#if defined(MBEDTLS_X509_CRT_PARSE_C)
Hanno Becker6d1986e2019-02-07 12:27:42 +0000215
216#if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100217 if (src->peer_cert != NULL) {
Janos Follath865b3eb2019-12-16 11:46:15 +0000218 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker2292d1f2013-09-15 17:06:49 +0200219
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100220 dst->peer_cert = mbedtls_calloc(1, sizeof(mbedtls_x509_crt));
221 if (dst->peer_cert == NULL) {
222 return MBEDTLS_ERR_SSL_ALLOC_FAILED;
223 }
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200224
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100225 mbedtls_x509_crt_init(dst->peer_cert);
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200226
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100227 if ((ret = mbedtls_x509_crt_parse_der(dst->peer_cert, src->peer_cert->raw.p,
228 src->peer_cert->raw.len)) != 0) {
229 mbedtls_free(dst->peer_cert);
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200230 dst->peer_cert = NULL;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100231 return ret;
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200232 }
233 }
Hanno Becker6d1986e2019-02-07 12:27:42 +0000234#else /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100235 if (src->peer_cert_digest != NULL) {
Hanno Becker9198ad12019-02-05 17:00:50 +0000236 dst->peer_cert_digest =
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100237 mbedtls_calloc(1, src->peer_cert_digest_len);
238 if (dst->peer_cert_digest == NULL) {
239 return MBEDTLS_ERR_SSL_ALLOC_FAILED;
240 }
Hanno Becker9198ad12019-02-05 17:00:50 +0000241
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100242 memcpy(dst->peer_cert_digest, src->peer_cert_digest,
243 src->peer_cert_digest_len);
Hanno Becker9198ad12019-02-05 17:00:50 +0000244 dst->peer_cert_digest_type = src->peer_cert_digest_type;
Hanno Beckeraccc5992019-02-25 10:06:59 +0000245 dst->peer_cert_digest_len = src->peer_cert_digest_len;
Hanno Becker9198ad12019-02-05 17:00:50 +0000246 }
247#endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
248
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200249#endif /* MBEDTLS_X509_CRT_PARSE_C */
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200250
Manuel Pégourié-Gonnardb596abf2015-05-20 10:45:29 +0200251#if defined(MBEDTLS_SSL_SESSION_TICKETS) && defined(MBEDTLS_SSL_CLI_C)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100252 if (src->ticket != NULL) {
253 dst->ticket = mbedtls_calloc(1, src->ticket_len);
254 if (dst->ticket == NULL) {
255 return MBEDTLS_ERR_SSL_ALLOC_FAILED;
256 }
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200257
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100258 memcpy(dst->ticket, src->ticket, src->ticket_len);
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200259 }
Manuel Pégourié-Gonnardb596abf2015-05-20 10:45:29 +0200260#endif /* MBEDTLS_SSL_SESSION_TICKETS && MBEDTLS_SSL_CLI_C */
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200261
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100262 return 0;
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200263}
264
Andrzej Kurek0afa2a12020-03-03 10:39:58 -0500265#if defined(MBEDTLS_SSL_VARIABLE_BUFFER_LENGTH)
Manuel Pégourié-Gonnardd904d662022-06-17 10:24:00 +0200266MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100267static int resize_buffer(unsigned char **buffer, size_t len_new, size_t *len_old)
Andrzej Kurek0afa2a12020-03-03 10:39:58 -0500268{
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100269 unsigned char *resized_buffer = mbedtls_calloc(1, len_new);
270 if (resized_buffer == NULL) {
Andrzej Kurek0afa2a12020-03-03 10:39:58 -0500271 return -1;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100272 }
Andrzej Kurek0afa2a12020-03-03 10:39:58 -0500273
274 /* We want to copy len_new bytes when downsizing the buffer, and
275 * len_old bytes when upsizing, so we choose the smaller of two sizes,
276 * to fit one buffer into another. Size checks, ensuring that no data is
277 * lost, are done outside of this function. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100278 memcpy(resized_buffer, *buffer,
279 (len_new < *len_old) ? len_new : *len_old);
280 mbedtls_platform_zeroize(*buffer, *len_old);
281 mbedtls_free(*buffer);
Andrzej Kurek0afa2a12020-03-03 10:39:58 -0500282
283 *buffer = resized_buffer;
284 *len_old = len_new;
285
286 return 0;
287}
Andrzej Kurek4a063792020-10-21 15:08:44 +0200288
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100289static void handle_buffer_resizing(mbedtls_ssl_context *ssl, int downsizing,
290 size_t in_buf_new_len,
291 size_t out_buf_new_len)
Andrzej Kurek4a063792020-10-21 15:08:44 +0200292{
293 int modified = 0;
294 size_t written_in = 0, iv_offset_in = 0, len_offset_in = 0;
295 size_t written_out = 0, iv_offset_out = 0, len_offset_out = 0;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100296 if (ssl->in_buf != NULL) {
Andrzej Kurek4a063792020-10-21 15:08:44 +0200297 written_in = ssl->in_msg - ssl->in_buf;
298 iv_offset_in = ssl->in_iv - ssl->in_buf;
299 len_offset_in = ssl->in_len - ssl->in_buf;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100300 if (downsizing ?
Andrzej Kurek4a063792020-10-21 15:08:44 +0200301 ssl->in_buf_len > in_buf_new_len && ssl->in_left < in_buf_new_len :
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100302 ssl->in_buf_len < in_buf_new_len) {
303 if (resize_buffer(&ssl->in_buf, in_buf_new_len, &ssl->in_buf_len) != 0) {
304 MBEDTLS_SSL_DEBUG_MSG(1, ("input buffer resizing failed - out of memory"));
305 } else {
306 MBEDTLS_SSL_DEBUG_MSG(2, ("Reallocating in_buf to %" MBEDTLS_PRINTF_SIZET,
307 in_buf_new_len));
Andrzej Kurek4a063792020-10-21 15:08:44 +0200308 modified = 1;
309 }
310 }
311 }
312
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100313 if (ssl->out_buf != NULL) {
Andrzej Kurek4a063792020-10-21 15:08:44 +0200314 written_out = ssl->out_msg - ssl->out_buf;
315 iv_offset_out = ssl->out_iv - ssl->out_buf;
316 len_offset_out = ssl->out_len - ssl->out_buf;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100317 if (downsizing ?
Andrzej Kurek4a063792020-10-21 15:08:44 +0200318 ssl->out_buf_len > out_buf_new_len && ssl->out_left < out_buf_new_len :
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100319 ssl->out_buf_len < out_buf_new_len) {
320 if (resize_buffer(&ssl->out_buf, out_buf_new_len, &ssl->out_buf_len) != 0) {
321 MBEDTLS_SSL_DEBUG_MSG(1, ("output buffer resizing failed - out of memory"));
322 } else {
323 MBEDTLS_SSL_DEBUG_MSG(2, ("Reallocating out_buf to %" MBEDTLS_PRINTF_SIZET,
324 out_buf_new_len));
Andrzej Kurek4a063792020-10-21 15:08:44 +0200325 modified = 1;
326 }
327 }
328 }
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100329 if (modified) {
Andrzej Kurek4a063792020-10-21 15:08:44 +0200330 /* Update pointers here to avoid doing it twice. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100331 mbedtls_ssl_reset_in_out_pointers(ssl);
Andrzej Kurek4a063792020-10-21 15:08:44 +0200332 /* Fields below might not be properly updated with record
333 * splitting or with CID, so they are manually updated here. */
334 ssl->out_msg = ssl->out_buf + written_out;
335 ssl->out_len = ssl->out_buf + len_offset_out;
336 ssl->out_iv = ssl->out_buf + iv_offset_out;
337
338 ssl->in_msg = ssl->in_buf + written_in;
339 ssl->in_len = ssl->in_buf + len_offset_in;
340 ssl->in_iv = ssl->in_buf + iv_offset_in;
341 }
342}
Andrzej Kurek0afa2a12020-03-03 10:39:58 -0500343#endif /* MBEDTLS_SSL_VARIABLE_BUFFER_LENGTH */
344
Paul Bakker5121ce52009-01-03 21:22:43 +0000345/*
346 * Key material generation
347 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200348#if defined(MBEDTLS_SSL_PROTO_SSL3)
Manuel Pégourié-Gonnardd904d662022-06-17 10:24:00 +0200349MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100350static int ssl3_prf(const unsigned char *secret, size_t slen,
351 const char *label,
352 const unsigned char *random, size_t rlen,
353 unsigned char *dstbuf, size_t dlen)
Paul Bakker5f70b252012-09-13 14:23:06 +0000354{
Andres Amaya Garcia33952502017-07-20 16:29:16 +0100355 int ret = 0;
Paul Bakker5f70b252012-09-13 14:23:06 +0000356 size_t i;
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +0200357 mbedtls_md5_context md5;
358 mbedtls_sha1_context sha1;
Paul Bakker5f70b252012-09-13 14:23:06 +0000359 unsigned char padding[16];
360 unsigned char sha1sum[20];
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100361 ((void) label);
Paul Bakker5f70b252012-09-13 14:23:06 +0000362
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100363 mbedtls_md5_init(&md5);
364 mbedtls_sha1_init(&sha1);
Paul Bakker5b4af392014-06-26 12:09:34 +0200365
Paul Bakker5f70b252012-09-13 14:23:06 +0000366 /*
367 * SSLv3:
368 * block =
369 * MD5( secret + SHA1( 'A' + secret + random ) ) +
370 * MD5( secret + SHA1( 'BB' + secret + random ) ) +
371 * MD5( secret + SHA1( 'CCC' + secret + random ) ) +
372 * ...
373 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100374 for (i = 0; i < dlen / 16; i++) {
375 memset(padding, (unsigned char) ('A' + i), 1 + i);
Paul Bakker5f70b252012-09-13 14:23:06 +0000376
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100377 if ((ret = mbedtls_sha1_starts_ret(&sha1)) != 0) {
Andres Amaya Garcia1a607a12017-06-29 17:09:42 +0100378 goto exit;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100379 }
380 if ((ret = mbedtls_sha1_update_ret(&sha1, padding, 1 + i)) != 0) {
Andres Amaya Garcia1a607a12017-06-29 17:09:42 +0100381 goto exit;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100382 }
383 if ((ret = mbedtls_sha1_update_ret(&sha1, secret, slen)) != 0) {
Andres Amaya Garcia1a607a12017-06-29 17:09:42 +0100384 goto exit;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100385 }
386 if ((ret = mbedtls_sha1_update_ret(&sha1, random, rlen)) != 0) {
Andres Amaya Garcia1a607a12017-06-29 17:09:42 +0100387 goto exit;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100388 }
389 if ((ret = mbedtls_sha1_finish_ret(&sha1, sha1sum)) != 0) {
Andres Amaya Garcia1a607a12017-06-29 17:09:42 +0100390 goto exit;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100391 }
Paul Bakker5f70b252012-09-13 14:23:06 +0000392
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100393 if ((ret = mbedtls_md5_starts_ret(&md5)) != 0) {
Andres Amaya Garcia1a607a12017-06-29 17:09:42 +0100394 goto exit;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100395 }
396 if ((ret = mbedtls_md5_update_ret(&md5, secret, slen)) != 0) {
Andres Amaya Garcia1a607a12017-06-29 17:09:42 +0100397 goto exit;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100398 }
399 if ((ret = mbedtls_md5_update_ret(&md5, sha1sum, 20)) != 0) {
Andres Amaya Garcia1a607a12017-06-29 17:09:42 +0100400 goto exit;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100401 }
402 if ((ret = mbedtls_md5_finish_ret(&md5, dstbuf + i * 16)) != 0) {
Andres Amaya Garcia1a607a12017-06-29 17:09:42 +0100403 goto exit;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100404 }
Paul Bakker5f70b252012-09-13 14:23:06 +0000405 }
406
Andres Amaya Garcia1a607a12017-06-29 17:09:42 +0100407exit:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100408 mbedtls_md5_free(&md5);
409 mbedtls_sha1_free(&sha1);
Paul Bakker5f70b252012-09-13 14:23:06 +0000410
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100411 mbedtls_platform_zeroize(padding, sizeof(padding));
412 mbedtls_platform_zeroize(sha1sum, sizeof(sha1sum));
Paul Bakker5f70b252012-09-13 14:23:06 +0000413
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100414 return ret;
Paul Bakker5f70b252012-09-13 14:23:06 +0000415}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200416#endif /* MBEDTLS_SSL_PROTO_SSL3 */
Paul Bakker5f70b252012-09-13 14:23:06 +0000417
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200418#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1)
Manuel Pégourié-Gonnardd904d662022-06-17 10:24:00 +0200419MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100420static int tls1_prf(const unsigned char *secret, size_t slen,
421 const char *label,
422 const unsigned char *random, size_t rlen,
423 unsigned char *dstbuf, size_t dlen)
Paul Bakker5121ce52009-01-03 21:22:43 +0000424{
Paul Bakker23986e52011-04-24 08:57:21 +0000425 size_t nb, hs;
426 size_t i, j, k;
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +0200427 const unsigned char *S1, *S2;
Ron Eldor3b350852019-05-07 18:31:49 +0300428 unsigned char *tmp;
429 size_t tmp_len = 0;
Paul Bakker5121ce52009-01-03 21:22:43 +0000430 unsigned char h_i[20];
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200431 const mbedtls_md_info_t *md_info;
432 mbedtls_md_context_t md_ctx;
Janos Follath865b3eb2019-12-16 11:46:15 +0000433 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnardb7fcca32015-03-26 11:41:28 +0100434
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100435 mbedtls_md_init(&md_ctx);
Paul Bakker5121ce52009-01-03 21:22:43 +0000436
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100437 tmp_len = 20 + strlen(label) + rlen;
438 tmp = mbedtls_calloc(1, tmp_len);
439 if (tmp == NULL) {
Ron Eldor3b350852019-05-07 18:31:49 +0300440 ret = MBEDTLS_ERR_SSL_ALLOC_FAILED;
441 goto exit;
442 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000443
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100444 hs = (slen + 1) / 2;
Paul Bakker5121ce52009-01-03 21:22:43 +0000445 S1 = secret;
446 S2 = secret + slen - hs;
447
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100448 nb = strlen(label);
449 memcpy(tmp + 20, label, nb);
450 memcpy(tmp + 20 + nb, random, rlen);
Paul Bakker5121ce52009-01-03 21:22:43 +0000451 nb += rlen;
452
453 /*
454 * First compute P_md5(secret,label+random)[0..dlen]
455 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100456 if ((md_info = mbedtls_md_info_from_type(MBEDTLS_MD_MD5)) == NULL) {
Ron Eldor3b350852019-05-07 18:31:49 +0300457 ret = MBEDTLS_ERR_SSL_INTERNAL_ERROR;
458 goto exit;
459 }
Manuel Pégourié-Gonnard7da726b2015-03-24 18:08:19 +0100460
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100461 if ((ret = mbedtls_md_setup(&md_ctx, md_info, 1)) != 0) {
Ron Eldor3b350852019-05-07 18:31:49 +0300462 goto exit;
463 }
Manuel Pégourié-Gonnardb7fcca32015-03-26 11:41:28 +0100464
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100465 ret = mbedtls_md_hmac_starts(&md_ctx, S1, hs);
466 if (ret != 0) {
Gilles Peskine2b3f21d2021-12-10 21:35:10 +0100467 goto exit;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100468 }
469 ret = mbedtls_md_hmac_update(&md_ctx, tmp + 20, nb);
470 if (ret != 0) {
Gilles Peskine2b3f21d2021-12-10 21:35:10 +0100471 goto exit;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100472 }
473 ret = mbedtls_md_hmac_finish(&md_ctx, 4 + tmp);
474 if (ret != 0) {
Gilles Peskine2b3f21d2021-12-10 21:35:10 +0100475 goto exit;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100476 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000477
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100478 for (i = 0; i < dlen; i += 16) {
479 ret = mbedtls_md_hmac_reset(&md_ctx);
480 if (ret != 0) {
Gilles Peskine2b3f21d2021-12-10 21:35:10 +0100481 goto exit;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100482 }
483 ret = mbedtls_md_hmac_update(&md_ctx, 4 + tmp, 16 + nb);
484 if (ret != 0) {
Gilles Peskine2b3f21d2021-12-10 21:35:10 +0100485 goto exit;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100486 }
487 ret = mbedtls_md_hmac_finish(&md_ctx, h_i);
488 if (ret != 0) {
Gilles Peskine2b3f21d2021-12-10 21:35:10 +0100489 goto exit;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100490 }
Manuel Pégourié-Gonnardb7fcca32015-03-26 11:41:28 +0100491
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100492 ret = mbedtls_md_hmac_reset(&md_ctx);
493 if (ret != 0) {
Gilles Peskine2b3f21d2021-12-10 21:35:10 +0100494 goto exit;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100495 }
496 ret = mbedtls_md_hmac_update(&md_ctx, 4 + tmp, 16);
497 if (ret != 0) {
Gilles Peskine2b3f21d2021-12-10 21:35:10 +0100498 goto exit;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100499 }
500 ret = mbedtls_md_hmac_finish(&md_ctx, 4 + tmp);
501 if (ret != 0) {
Gilles Peskine2b3f21d2021-12-10 21:35:10 +0100502 goto exit;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100503 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000504
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100505 k = (i + 16 > dlen) ? dlen % 16 : 16;
Paul Bakker5121ce52009-01-03 21:22:43 +0000506
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100507 for (j = 0; j < k; j++) {
Paul Bakker5121ce52009-01-03 21:22:43 +0000508 dstbuf[i + j] = h_i[j];
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100509 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000510 }
511
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100512 mbedtls_md_free(&md_ctx);
Manuel Pégourié-Gonnardb7fcca32015-03-26 11:41:28 +0100513
Paul Bakker5121ce52009-01-03 21:22:43 +0000514 /*
515 * XOR out with P_sha1(secret,label+random)[0..dlen]
516 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100517 if ((md_info = mbedtls_md_info_from_type(MBEDTLS_MD_SHA1)) == NULL) {
Ron Eldor3b350852019-05-07 18:31:49 +0300518 ret = MBEDTLS_ERR_SSL_INTERNAL_ERROR;
519 goto exit;
520 }
Manuel Pégourié-Gonnard7da726b2015-03-24 18:08:19 +0100521
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100522 if ((ret = mbedtls_md_setup(&md_ctx, md_info, 1)) != 0) {
Ron Eldor3b350852019-05-07 18:31:49 +0300523 goto exit;
524 }
Manuel Pégourié-Gonnardb7fcca32015-03-26 11:41:28 +0100525
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100526 ret = mbedtls_md_hmac_starts(&md_ctx, S2, hs);
527 if (ret != 0) {
Gilles Peskine2b3f21d2021-12-10 21:35:10 +0100528 goto exit;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100529 }
530 ret = mbedtls_md_hmac_update(&md_ctx, tmp + 20, nb);
531 if (ret != 0) {
Gilles Peskine2b3f21d2021-12-10 21:35:10 +0100532 goto exit;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100533 }
534 ret = mbedtls_md_hmac_finish(&md_ctx, tmp);
535 if (ret != 0) {
Gilles Peskine2b3f21d2021-12-10 21:35:10 +0100536 goto exit;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100537 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000538
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100539 for (i = 0; i < dlen; i += 20) {
540 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, tmp, 20 + nb);
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, h_i);
549 if (ret != 0) {
Gilles Peskine2b3f21d2021-12-10 21:35:10 +0100550 goto exit;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100551 }
Manuel Pégourié-Gonnardb7fcca32015-03-26 11:41:28 +0100552
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100553 ret = mbedtls_md_hmac_reset(&md_ctx);
554 if (ret != 0) {
Gilles Peskine2b3f21d2021-12-10 21:35:10 +0100555 goto exit;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100556 }
557 ret = mbedtls_md_hmac_update(&md_ctx, tmp, 20);
558 if (ret != 0) {
Gilles Peskine2b3f21d2021-12-10 21:35:10 +0100559 goto exit;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100560 }
561 ret = mbedtls_md_hmac_finish(&md_ctx, tmp);
562 if (ret != 0) {
Gilles Peskine2b3f21d2021-12-10 21:35:10 +0100563 goto exit;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100564 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000565
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100566 k = (i + 20 > dlen) ? dlen % 20 : 20;
Paul Bakker5121ce52009-01-03 21:22:43 +0000567
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100568 for (j = 0; j < k; j++) {
569 dstbuf[i + j] = (unsigned char) (dstbuf[i + j] ^ h_i[j]);
570 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000571 }
572
Ron Eldor3b350852019-05-07 18:31:49 +0300573exit:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100574 mbedtls_md_free(&md_ctx);
Manuel Pégourié-Gonnardb7fcca32015-03-26 11:41:28 +0100575
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100576 mbedtls_platform_zeroize(tmp, tmp_len);
577 mbedtls_platform_zeroize(h_i, sizeof(h_i));
Paul Bakker5121ce52009-01-03 21:22:43 +0000578
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100579 mbedtls_free(tmp);
580 return ret;
Paul Bakker5121ce52009-01-03 21:22:43 +0000581}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200582#endif /* MBEDTLS_SSL_PROTO_TLS1) || MBEDTLS_SSL_PROTO_TLS1_1 */
Paul Bakker5121ce52009-01-03 21:22:43 +0000583
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200584#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
Andrzej Kurekc929a822019-01-14 03:51:11 -0500585#if defined(MBEDTLS_USE_PSA_CRYPTO)
k-stachowiak81053a52019-08-17 10:30:28 +0200586
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100587static psa_status_t setup_psa_key_derivation(psa_key_derivation_operation_t *derivation,
588 psa_key_id_t key,
589 psa_algorithm_t alg,
590 const unsigned char *seed, size_t seed_length,
591 const unsigned char *label, size_t label_length,
592 size_t capacity)
k-stachowiak81053a52019-08-17 10:30:28 +0200593{
594 psa_status_t status;
595
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100596 status = psa_key_derivation_setup(derivation, alg);
597 if (status != PSA_SUCCESS) {
598 return status;
599 }
k-stachowiak81053a52019-08-17 10:30:28 +0200600
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100601 if (PSA_ALG_IS_TLS12_PRF(alg) || PSA_ALG_IS_TLS12_PSK_TO_MS(alg)) {
602 status = psa_key_derivation_input_bytes(derivation,
603 PSA_KEY_DERIVATION_INPUT_SEED,
604 seed, seed_length);
605 if (status != PSA_SUCCESS) {
606 return status;
607 }
k-stachowiak81053a52019-08-17 10:30:28 +0200608
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100609 if (mbedtls_svc_key_id_is_null(key)) {
Gilles Peskine311f54d2019-09-23 18:19:22 +0200610 status = psa_key_derivation_input_bytes(
611 derivation, PSA_KEY_DERIVATION_INPUT_SECRET,
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100612 NULL, 0);
613 } else {
Gilles Peskine311f54d2019-09-23 18:19:22 +0200614 status = psa_key_derivation_input_key(
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100615 derivation, PSA_KEY_DERIVATION_INPUT_SECRET, key);
Gilles Peskine311f54d2019-09-23 18:19:22 +0200616 }
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100617 if (status != PSA_SUCCESS) {
618 return status;
619 }
k-stachowiak81053a52019-08-17 10:30:28 +0200620
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100621 status = psa_key_derivation_input_bytes(derivation,
622 PSA_KEY_DERIVATION_INPUT_LABEL,
623 label, label_length);
624 if (status != PSA_SUCCESS) {
625 return status;
626 }
627 } else {
628 return PSA_ERROR_NOT_SUPPORTED;
k-stachowiak81053a52019-08-17 10:30:28 +0200629 }
630
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100631 status = psa_key_derivation_set_capacity(derivation, capacity);
632 if (status != PSA_SUCCESS) {
633 return status;
634 }
k-stachowiak81053a52019-08-17 10:30:28 +0200635
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100636 return PSA_SUCCESS;
k-stachowiak81053a52019-08-17 10:30:28 +0200637}
638
Manuel Pégourié-Gonnardd904d662022-06-17 10:24:00 +0200639MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100640static int tls_prf_generic(mbedtls_md_type_t md_type,
641 const unsigned char *secret, size_t slen,
642 const char *label,
643 const unsigned char *random, size_t rlen,
644 unsigned char *dstbuf, size_t dlen)
Andrzej Kurekc929a822019-01-14 03:51:11 -0500645{
646 psa_status_t status;
647 psa_algorithm_t alg;
Ronald Croncf56a0a2020-08-04 09:51:30 +0200648 psa_key_id_t master_key = MBEDTLS_SVC_KEY_ID_INIT;
Janos Follathda6ac012019-08-16 13:47:29 +0100649 psa_key_derivation_operation_t derivation =
Janos Follath8dee8772019-07-30 12:53:32 +0100650 PSA_KEY_DERIVATION_OPERATION_INIT;
Andrzej Kurekc929a822019-01-14 03:51:11 -0500651
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100652 if (md_type == MBEDTLS_MD_SHA384) {
Andrzej Kurekc929a822019-01-14 03:51:11 -0500653 alg = PSA_ALG_TLS12_PRF(PSA_ALG_SHA_384);
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100654 } else {
Andrzej Kurekc929a822019-01-14 03:51:11 -0500655 alg = PSA_ALG_TLS12_PRF(PSA_ALG_SHA_256);
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100656 }
Andrzej Kurekc929a822019-01-14 03:51:11 -0500657
Gilles Peskine311f54d2019-09-23 18:19:22 +0200658 /* Normally a "secret" should be long enough to be impossible to
659 * find by brute force, and in particular should not be empty. But
660 * this PRF is also used to derive an IV, in particular in EAP-TLS,
661 * and for this use case it makes sense to have a 0-length "secret".
662 * Since the key API doesn't allow importing a key of length 0,
Ronald Croncf56a0a2020-08-04 09:51:30 +0200663 * keep master_key=0, which setup_psa_key_derivation() understands
Gilles Peskine311f54d2019-09-23 18:19:22 +0200664 * to mean a 0-length "secret" input. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100665 if (slen != 0) {
Gilles Peskine311f54d2019-09-23 18:19:22 +0200666 psa_key_attributes_t key_attributes = psa_key_attributes_init();
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100667 psa_set_key_usage_flags(&key_attributes, PSA_KEY_USAGE_DERIVE);
668 psa_set_key_algorithm(&key_attributes, alg);
669 psa_set_key_type(&key_attributes, PSA_KEY_TYPE_DERIVE);
Andrzej Kurekc929a822019-01-14 03:51:11 -0500670
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100671 status = psa_import_key(&key_attributes, secret, slen, &master_key);
672 if (status != PSA_SUCCESS) {
673 return MBEDTLS_ERR_SSL_HW_ACCEL_FAILED;
674 }
Gilles Peskine311f54d2019-09-23 18:19:22 +0200675 }
Andrzej Kurekc929a822019-01-14 03:51:11 -0500676
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100677 status = setup_psa_key_derivation(&derivation,
678 master_key, alg,
679 random, rlen,
680 (unsigned char const *) label,
681 (size_t) strlen(label),
682 dlen);
683 if (status != PSA_SUCCESS) {
684 psa_key_derivation_abort(&derivation);
685 psa_destroy_key(master_key);
686 return MBEDTLS_ERR_SSL_HW_ACCEL_FAILED;
Andrzej Kurekc929a822019-01-14 03:51:11 -0500687 }
688
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100689 status = psa_key_derivation_output_bytes(&derivation, dstbuf, dlen);
690 if (status != PSA_SUCCESS) {
691 psa_key_derivation_abort(&derivation);
692 psa_destroy_key(master_key);
693 return MBEDTLS_ERR_SSL_HW_ACCEL_FAILED;
Andrzej Kurekc929a822019-01-14 03:51:11 -0500694 }
695
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100696 status = psa_key_derivation_abort(&derivation);
697 if (status != PSA_SUCCESS) {
698 psa_destroy_key(master_key);
699 return MBEDTLS_ERR_SSL_HW_ACCEL_FAILED;
Andrzej Kurek70737ca2019-01-14 05:37:13 -0500700 }
Andrzej Kurekc929a822019-01-14 03:51:11 -0500701
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100702 if (!mbedtls_svc_key_id_is_null(master_key)) {
703 status = psa_destroy_key(master_key);
704 }
705 if (status != PSA_SUCCESS) {
706 return MBEDTLS_ERR_SSL_HW_ACCEL_FAILED;
707 }
Andrzej Kurekc929a822019-01-14 03:51:11 -0500708
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100709 return 0;
Andrzej Kurekc929a822019-01-14 03:51:11 -0500710}
711
712#else /* MBEDTLS_USE_PSA_CRYPTO */
713
Manuel Pégourié-Gonnardd904d662022-06-17 10:24:00 +0200714MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100715static int tls_prf_generic(mbedtls_md_type_t md_type,
716 const unsigned char *secret, size_t slen,
717 const char *label,
718 const unsigned char *random, size_t rlen,
719 unsigned char *dstbuf, size_t dlen)
Paul Bakker1ef83d62012-04-11 12:09:53 +0000720{
721 size_t nb;
Manuel Pégourié-Gonnard6890c6b2015-03-26 11:11:49 +0100722 size_t i, j, k, md_len;
Ron Eldor3b350852019-05-07 18:31:49 +0300723 unsigned char *tmp;
724 size_t tmp_len = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200725 unsigned char h_i[MBEDTLS_MD_MAX_SIZE];
726 const mbedtls_md_info_t *md_info;
727 mbedtls_md_context_t md_ctx;
Janos Follath865b3eb2019-12-16 11:46:15 +0000728 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnardb7fcca32015-03-26 11:41:28 +0100729
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100730 mbedtls_md_init(&md_ctx);
Paul Bakker1ef83d62012-04-11 12:09:53 +0000731
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100732 if ((md_info = mbedtls_md_info_from_type(md_type)) == NULL) {
733 return MBEDTLS_ERR_SSL_INTERNAL_ERROR;
734 }
Manuel Pégourié-Gonnard6890c6b2015-03-26 11:11:49 +0100735
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100736 md_len = mbedtls_md_get_size(md_info);
Manuel Pégourié-Gonnard6890c6b2015-03-26 11:11:49 +0100737
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100738 tmp_len = md_len + strlen(label) + rlen;
739 tmp = mbedtls_calloc(1, tmp_len);
740 if (tmp == NULL) {
Ron Eldor3b350852019-05-07 18:31:49 +0300741 ret = MBEDTLS_ERR_SSL_ALLOC_FAILED;
742 goto exit;
743 }
Paul Bakker1ef83d62012-04-11 12:09:53 +0000744
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100745 nb = strlen(label);
746 memcpy(tmp + md_len, label, nb);
747 memcpy(tmp + md_len + nb, random, rlen);
Paul Bakker1ef83d62012-04-11 12:09:53 +0000748 nb += rlen;
749
750 /*
751 * Compute P_<hash>(secret, label + random)[0..dlen]
752 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100753 if ((ret = mbedtls_md_setup(&md_ctx, md_info, 1)) != 0) {
Ron Eldor3b350852019-05-07 18:31:49 +0300754 goto exit;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100755 }
Manuel Pégourié-Gonnardb7fcca32015-03-26 11:41:28 +0100756
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100757 ret = mbedtls_md_hmac_starts(&md_ctx, secret, slen);
758 if (ret != 0) {
Gilles Peskine2b3f21d2021-12-10 21:35:10 +0100759 goto exit;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100760 }
761 ret = mbedtls_md_hmac_update(&md_ctx, tmp + md_len, nb);
762 if (ret != 0) {
Gilles Peskine2b3f21d2021-12-10 21:35:10 +0100763 goto exit;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100764 }
765 ret = mbedtls_md_hmac_finish(&md_ctx, tmp);
766 if (ret != 0) {
Gilles Peskine2b3f21d2021-12-10 21:35:10 +0100767 goto exit;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100768 }
Manuel Pégourié-Gonnard7da726b2015-03-24 18:08:19 +0100769
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100770 for (i = 0; i < dlen; i += md_len) {
771 ret = mbedtls_md_hmac_reset(&md_ctx);
772 if (ret != 0) {
Gilles Peskine2b3f21d2021-12-10 21:35:10 +0100773 goto exit;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100774 }
775 ret = mbedtls_md_hmac_update(&md_ctx, tmp, md_len + nb);
776 if (ret != 0) {
Gilles Peskine2b3f21d2021-12-10 21:35:10 +0100777 goto exit;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100778 }
779 ret = mbedtls_md_hmac_finish(&md_ctx, h_i);
780 if (ret != 0) {
Gilles Peskine2b3f21d2021-12-10 21:35:10 +0100781 goto exit;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100782 }
Manuel Pégourié-Gonnardb7fcca32015-03-26 11:41:28 +0100783
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100784 ret = mbedtls_md_hmac_reset(&md_ctx);
785 if (ret != 0) {
Gilles Peskine2b3f21d2021-12-10 21:35:10 +0100786 goto exit;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100787 }
788 ret = mbedtls_md_hmac_update(&md_ctx, tmp, md_len);
789 if (ret != 0) {
Gilles Peskine2b3f21d2021-12-10 21:35:10 +0100790 goto exit;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100791 }
792 ret = mbedtls_md_hmac_finish(&md_ctx, tmp);
793 if (ret != 0) {
Gilles Peskine2b3f21d2021-12-10 21:35:10 +0100794 goto exit;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100795 }
Paul Bakker1ef83d62012-04-11 12:09:53 +0000796
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100797 k = (i + md_len > dlen) ? dlen % md_len : md_len;
Paul Bakker1ef83d62012-04-11 12:09:53 +0000798
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100799 for (j = 0; j < k; j++) {
Paul Bakker1ef83d62012-04-11 12:09:53 +0000800 dstbuf[i + j] = h_i[j];
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100801 }
Paul Bakker1ef83d62012-04-11 12:09:53 +0000802 }
803
Ron Eldor3b350852019-05-07 18:31:49 +0300804exit:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100805 mbedtls_md_free(&md_ctx);
Manuel Pégourié-Gonnardb7fcca32015-03-26 11:41:28 +0100806
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100807 if (tmp != NULL) {
808 mbedtls_platform_zeroize(tmp, tmp_len);
809 }
Dave Rodgman369f4952022-11-01 16:08:14 +0000810
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100811 mbedtls_platform_zeroize(h_i, sizeof(h_i));
Paul Bakker1ef83d62012-04-11 12:09:53 +0000812
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100813 mbedtls_free(tmp);
Ron Eldor3b350852019-05-07 18:31:49 +0300814
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100815 return ret;
Paul Bakker1ef83d62012-04-11 12:09:53 +0000816}
Andrzej Kurekc929a822019-01-14 03:51:11 -0500817#endif /* MBEDTLS_USE_PSA_CRYPTO */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200818#if defined(MBEDTLS_SHA256_C)
Manuel Pégourié-Gonnardd904d662022-06-17 10:24:00 +0200819MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100820static int tls_prf_sha256(const unsigned char *secret, size_t slen,
821 const char *label,
822 const unsigned char *random, size_t rlen,
823 unsigned char *dstbuf, size_t dlen)
Manuel Pégourié-Gonnard6890c6b2015-03-26 11:11:49 +0100824{
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100825 return tls_prf_generic(MBEDTLS_MD_SHA256, secret, slen,
826 label, random, rlen, dstbuf, dlen);
Manuel Pégourié-Gonnard6890c6b2015-03-26 11:11:49 +0100827}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200828#endif /* MBEDTLS_SHA256_C */
Paul Bakker1ef83d62012-04-11 12:09:53 +0000829
Gilles Peskined2d59372021-05-12 22:43:27 +0200830#if defined(MBEDTLS_SHA512_C) && !defined(MBEDTLS_SHA512_NO_SHA384)
Manuel Pégourié-Gonnardd904d662022-06-17 10:24:00 +0200831MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100832static int tls_prf_sha384(const unsigned char *secret, size_t slen,
833 const char *label,
834 const unsigned char *random, size_t rlen,
835 unsigned char *dstbuf, size_t dlen)
Paul Bakkerca4ab492012-04-18 14:23:57 +0000836{
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100837 return tls_prf_generic(MBEDTLS_MD_SHA384, secret, slen,
838 label, random, rlen, dstbuf, dlen);
Paul Bakkerca4ab492012-04-18 14:23:57 +0000839}
Gilles Peskined2d59372021-05-12 22:43:27 +0200840#endif /* MBEDTLS_SHA512_C && !MBEDTLS_SHA512_NO_SHA384 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200841#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakkerca4ab492012-04-18 14:23:57 +0000842
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100843static void ssl_update_checksum_start(mbedtls_ssl_context *, const unsigned char *, size_t);
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200844
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200845#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1) || \
846 defined(MBEDTLS_SSL_PROTO_TLS1_1)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100847static void ssl_update_checksum_md5sha1(mbedtls_ssl_context *, const unsigned char *, size_t);
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200848#endif
Paul Bakker380da532012-04-18 16:10:25 +0000849
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200850#if defined(MBEDTLS_SSL_PROTO_SSL3)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100851static void ssl_calc_verify_ssl(const mbedtls_ssl_context *, unsigned char *, size_t *);
852static void ssl_calc_finished_ssl(mbedtls_ssl_context *, unsigned char *, int);
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200853#endif
854
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200855#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100856static void ssl_calc_verify_tls(const mbedtls_ssl_context *, unsigned char *, size_t *);
857static void ssl_calc_finished_tls(mbedtls_ssl_context *, unsigned char *, int);
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200858#endif
859
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200860#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
861#if defined(MBEDTLS_SHA256_C)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100862static void ssl_update_checksum_sha256(mbedtls_ssl_context *, const unsigned char *, size_t);
863static void ssl_calc_verify_tls_sha256(const mbedtls_ssl_context *, unsigned char *, size_t *);
864static void ssl_calc_finished_tls_sha256(mbedtls_ssl_context *, unsigned char *, int);
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200865#endif
Paul Bakker769075d2012-11-24 11:26:46 +0100866
Gilles Peskined2d59372021-05-12 22:43:27 +0200867#if defined(MBEDTLS_SHA512_C) && !defined(MBEDTLS_SHA512_NO_SHA384)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100868static void ssl_update_checksum_sha384(mbedtls_ssl_context *, const unsigned char *, size_t);
869static void ssl_calc_verify_tls_sha384(const mbedtls_ssl_context *, unsigned char *, size_t *);
870static void ssl_calc_finished_tls_sha384(mbedtls_ssl_context *, unsigned char *, int);
Paul Bakker769075d2012-11-24 11:26:46 +0100871#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200872#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakker1ef83d62012-04-11 12:09:53 +0000873
Manuel Pégourié-Gonnard45be3d82019-02-18 23:35:14 +0100874#if defined(MBEDTLS_KEY_EXCHANGE_PSK_ENABLED) && \
Hanno Becker7d0a5692018-10-23 15:26:22 +0100875 defined(MBEDTLS_USE_PSA_CRYPTO)
Manuel Pégourié-Gonnardd904d662022-06-17 10:24:00 +0200876MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100877static int ssl_use_opaque_psk(mbedtls_ssl_context const *ssl)
Hanno Becker7d0a5692018-10-23 15:26:22 +0100878{
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100879 if (ssl->conf->f_psk != NULL) {
Hanno Becker7d0a5692018-10-23 15:26:22 +0100880 /* If we've used a callback to select the PSK,
881 * the static configuration is irrelevant. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100882 if (!mbedtls_svc_key_id_is_null(ssl->handshake->psk_opaque)) {
883 return 1;
884 }
Hanno Becker7d0a5692018-10-23 15:26:22 +0100885
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100886 return 0;
Hanno Becker7d0a5692018-10-23 15:26:22 +0100887 }
888
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100889 if (!mbedtls_svc_key_id_is_null(ssl->conf->psk_opaque)) {
890 return 1;
891 }
Hanno Becker7d0a5692018-10-23 15:26:22 +0100892
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100893 return 0;
Hanno Becker7d0a5692018-10-23 15:26:22 +0100894}
895#endif /* MBEDTLS_USE_PSA_CRYPTO &&
Manuel Pégourié-Gonnard45be3d82019-02-18 23:35:14 +0100896 MBEDTLS_KEY_EXCHANGE_PSK_ENABLED */
Hanno Becker7d0a5692018-10-23 15:26:22 +0100897
Ron Eldorcf280092019-05-14 20:19:13 +0300898#if defined(MBEDTLS_SSL_EXPORT_KEYS)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100899static mbedtls_tls_prf_types tls_prf_get_type(mbedtls_ssl_tls_prf_cb *tls_prf)
Ron Eldorcf280092019-05-14 20:19:13 +0300900{
901#if defined(MBEDTLS_SSL_PROTO_SSL3)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100902 if (tls_prf == ssl3_prf) {
903 return MBEDTLS_SSL_TLS_PRF_SSL3;
904 } else
Ron Eldorcf280092019-05-14 20:19:13 +0300905#endif
906#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100907 if (tls_prf == tls1_prf) {
908 return MBEDTLS_SSL_TLS_PRF_TLS1;
909 } else
Ron Eldorcf280092019-05-14 20:19:13 +0300910#endif
911#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
Gilles Peskined2d59372021-05-12 22:43:27 +0200912#if defined(MBEDTLS_SHA512_C) && !defined(MBEDTLS_SHA512_NO_SHA384)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100913 if (tls_prf == tls_prf_sha384) {
914 return MBEDTLS_SSL_TLS_PRF_SHA384;
915 } else
Ron Eldorcf280092019-05-14 20:19:13 +0300916#endif
917#if defined(MBEDTLS_SHA256_C)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100918 if (tls_prf == tls_prf_sha256) {
919 return MBEDTLS_SSL_TLS_PRF_SHA256;
920 } else
Ron Eldorcf280092019-05-14 20:19:13 +0300921#endif
922#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100923 return MBEDTLS_SSL_TLS_PRF_NONE;
Ron Eldorcf280092019-05-14 20:19:13 +0300924}
925#endif /* MBEDTLS_SSL_EXPORT_KEYS */
926
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100927int mbedtls_ssl_tls_prf(const mbedtls_tls_prf_types prf,
928 const unsigned char *secret, size_t slen,
929 const char *label,
930 const unsigned char *random, size_t rlen,
931 unsigned char *dstbuf, size_t dlen)
Ron Eldor51d3ab52019-05-12 14:54:30 +0300932{
933 mbedtls_ssl_tls_prf_cb *tls_prf = NULL;
934
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100935 switch (prf) {
Ron Eldor51d3ab52019-05-12 14:54:30 +0300936#if defined(MBEDTLS_SSL_PROTO_SSL3)
937 case MBEDTLS_SSL_TLS_PRF_SSL3:
938 tls_prf = ssl3_prf;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100939 break;
Ron Eldord2f25f72019-05-15 14:54:22 +0300940#endif /* MBEDTLS_SSL_PROTO_SSL3 */
Ron Eldor51d3ab52019-05-12 14:54:30 +0300941#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1)
942 case MBEDTLS_SSL_TLS_PRF_TLS1:
943 tls_prf = tls1_prf;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100944 break;
Ron Eldord2f25f72019-05-15 14:54:22 +0300945#endif /* MBEDTLS_SSL_PROTO_TLS1 || MBEDTLS_SSL_PROTO_TLS1_1 */
946
947#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
Gilles Peskined2d59372021-05-12 22:43:27 +0200948#if defined(MBEDTLS_SHA512_C) && !defined(MBEDTLS_SHA512_NO_SHA384)
Ron Eldor51d3ab52019-05-12 14:54:30 +0300949 case MBEDTLS_SSL_TLS_PRF_SHA384:
950 tls_prf = tls_prf_sha384;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100951 break;
Gilles Peskined2d59372021-05-12 22:43:27 +0200952#endif /* MBEDTLS_SHA512_C && !MBEDTLS_SHA512_NO_SHA384 */
Ron Eldor51d3ab52019-05-12 14:54:30 +0300953#if defined(MBEDTLS_SHA256_C)
954 case MBEDTLS_SSL_TLS_PRF_SHA256:
955 tls_prf = tls_prf_sha256;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100956 break;
Ron Eldord2f25f72019-05-15 14:54:22 +0300957#endif /* MBEDTLS_SHA256_C */
958#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100959 default:
960 return MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
Ron Eldor51d3ab52019-05-12 14:54:30 +0300961 }
962
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100963 return tls_prf(secret, slen, label, random, rlen, dstbuf, dlen);
Ron Eldor51d3ab52019-05-12 14:54:30 +0300964}
965
Manuel Pégourié-Gonnard9b108c22019-05-06 13:32:17 +0200966/* Type for the TLS PRF */
967typedef int ssl_tls_prf_t(const unsigned char *, size_t, const char *,
968 const unsigned char *, size_t,
969 unsigned char *, size_t);
970
Manuel Pégourié-Gonnarde59ae232019-04-30 11:41:40 +0200971/*
Manuel Pégourié-Gonnardcba40d92019-05-06 12:55:40 +0200972 * Populate a transform structure with session keys and all the other
973 * necessary information.
Manuel Pégourié-Gonnarde59ae232019-04-30 11:41:40 +0200974 *
Manuel Pégourié-Gonnardcba40d92019-05-06 12:55:40 +0200975 * Parameters:
976 * - [in/out]: transform: structure to populate
977 * [in] must be just initialised with mbedtls_ssl_transform_init()
Manuel Pégourié-Gonnardc864f6a2019-05-06 13:48:22 +0200978 * [out] fully populated, ready for use by mbedtls_ssl_{en,de}crypt_buf()
Manuel Pégourié-Gonnard6fa57bf2019-05-10 10:50:04 +0200979 * - [in] ciphersuite
980 * - [in] master
981 * - [in] encrypt_then_mac
982 * - [in] trunc_hmac
983 * - [in] compression
Manuel Pégourié-Gonnard9b108c22019-05-06 13:32:17 +0200984 * - [in] tls_prf: pointer to PRF to use for key derivation
985 * - [in] randbytes: buffer holding ServerHello.random + ClientHello.random
Manuel Pégourié-Gonnardc864f6a2019-05-06 13:48:22 +0200986 * - [in] minor_ver: SSL/TLS minor version
987 * - [in] endpoint: client or server
988 * - [in] ssl: optionally used for:
Manuel Pégourié-Gonnarde07bc202020-02-26 09:53:42 +0100989 * - MBEDTLS_SSL_HW_RECORD_ACCEL: whole context (non-const)
Manuel Pégourié-Gonnardc864f6a2019-05-06 13:48:22 +0200990 * - MBEDTLS_SSL_EXPORT_KEYS: ssl->conf->{f,p}_export_keys
991 * - MBEDTLS_DEBUG_C: ssl->conf->{f,p}_dbg
Manuel Pégourié-Gonnarde59ae232019-04-30 11:41:40 +0200992 */
Manuel Pégourié-Gonnardd904d662022-06-17 10:24:00 +0200993MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100994static int ssl_populate_transform(mbedtls_ssl_transform *transform,
995 int ciphersuite,
996 const unsigned char master[48],
Jarno Lamsac84bd242019-08-16 12:06:56 +0300997#if defined(MBEDTLS_SSL_SOME_MODES_USE_MAC)
Manuel Pégourié-Gonnard6fa57bf2019-05-10 10:50:04 +0200998#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100999 int encrypt_then_mac,
Jarno Lamsac84bd242019-08-16 12:06:56 +03001000#endif /* MBEDTLS_SSL_ENCRYPT_THEN_MAC */
Manuel Pégourié-Gonnard6fa57bf2019-05-10 10:50:04 +02001001#if defined(MBEDTLS_SSL_TRUNCATED_HMAC)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001002 int trunc_hmac,
Jarno Lamsac84bd242019-08-16 12:06:56 +03001003#endif /* MBEDTLS_SSL_TRUNCATED_HMAC */
1004#endif /* MBEDTLS_SSL_SOME_MODES_USE_MAC */
Manuel Pégourié-Gonnard6fa57bf2019-05-10 10:50:04 +02001005#if defined(MBEDTLS_ZLIB_SUPPORT)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001006 int compression,
Manuel Pégourié-Gonnard6fa57bf2019-05-10 10:50:04 +02001007#endif
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001008 ssl_tls_prf_t tls_prf,
1009 const unsigned char randbytes[64],
1010 int minor_ver,
1011 unsigned endpoint,
Manuel Pégourié-Gonnard7ae6ed42020-03-13 11:28:19 +01001012#if !defined(MBEDTLS_SSL_HW_RECORD_ACCEL)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001013 const
Manuel Pégourié-Gonnard7ae6ed42020-03-13 11:28:19 +01001014#endif
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001015 mbedtls_ssl_context *ssl)
Paul Bakker5121ce52009-01-03 21:22:43 +00001016{
Paul Bakkerda02a7f2013-08-31 17:25:14 +02001017 int ret = 0;
Hanno Beckercb1cc802018-11-17 22:27:38 +00001018#if defined(MBEDTLS_USE_PSA_CRYPTO)
1019 int psa_fallthrough;
1020#endif /* MBEDTLS_USE_PSA_CRYPTO */
David Horstmannd4f22082022-10-26 18:25:14 +01001021 int do_mbedtls_cipher_setup;
Paul Bakker5121ce52009-01-03 21:22:43 +00001022 unsigned char keyblk[256];
1023 unsigned char *key1;
1024 unsigned char *key2;
Paul Bakker68884e32013-01-07 18:20:04 +01001025 unsigned char *mac_enc;
1026 unsigned char *mac_dec;
sander-visser3888b032020-05-06 21:49:46 +02001027 size_t mac_key_len = 0;
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02001028 size_t iv_copy_len;
Hanno Becker88aaf652017-12-27 08:17:40 +00001029 unsigned keylen;
Hanno Beckere694c3e2017-12-27 21:34:08 +00001030 const mbedtls_ssl_ciphersuite_t *ciphersuite_info;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001031 const mbedtls_cipher_info_t *cipher_info;
1032 const mbedtls_md_info_t *md_info;
Paul Bakker68884e32013-01-07 18:20:04 +01001033
Manuel Pégourié-Gonnardc864f6a2019-05-06 13:48:22 +02001034#if !defined(MBEDTLS_SSL_HW_RECORD_ACCEL) && \
1035 !defined(MBEDTLS_SSL_EXPORT_KEYS) && \
Gilles Peskinea6f99a12022-04-13 13:24:56 +02001036 !defined(MBEDTLS_SSL_DTLS_CONNECTION_ID) && \
Manuel Pégourié-Gonnardc864f6a2019-05-06 13:48:22 +02001037 !defined(MBEDTLS_DEBUG_C)
Paul Elliott9c2faca2023-10-18 14:34:46 +01001038 (void) ssl; /* ssl is unused except for those cases */
Manuel Pégourié-Gonnardc864f6a2019-05-06 13:48:22 +02001039#endif
1040
Manuel Pégourié-Gonnard96fb0ee2019-07-09 12:54:17 +02001041 /*
1042 * Some data just needs copying into the structure
1043 */
Jaeden Amero2de07f12019-06-05 13:32:08 +01001044#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC) && \
1045 defined(MBEDTLS_SSL_SOME_MODES_USE_MAC)
Manuel Pégourié-Gonnard6fa57bf2019-05-10 10:50:04 +02001046 transform->encrypt_then_mac = encrypt_then_mac;
Hanno Becker9eddaeb2017-12-27 21:37:21 +00001047#endif
Manuel Pégourié-Gonnardc864f6a2019-05-06 13:48:22 +02001048 transform->minor_ver = minor_ver;
Hanno Beckere694c3e2017-12-27 21:34:08 +00001049
Manuel Pégourié-Gonnard96fb0ee2019-07-09 12:54:17 +02001050#if defined(MBEDTLS_SSL_CONTEXT_SERIALIZATION)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001051 memcpy(transform->randbytes, randbytes, sizeof(transform->randbytes));
Manuel Pégourié-Gonnard96fb0ee2019-07-09 12:54:17 +02001052#endif
1053
Manuel Pégourié-Gonnard9b108c22019-05-06 13:32:17 +02001054 /*
1055 * Get various info structures
1056 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001057 ciphersuite_info = mbedtls_ssl_ciphersuite_from_id(ciphersuite);
1058 if (ciphersuite_info == NULL) {
1059 MBEDTLS_SSL_DEBUG_MSG(1, ("ciphersuite info for %d not found",
1060 ciphersuite));
1061 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
Manuel Pégourié-Gonnard9b108c22019-05-06 13:32:17 +02001062 }
1063
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001064 cipher_info = mbedtls_cipher_info_from_type(ciphersuite_info->cipher);
1065 if (cipher_info == NULL) {
1066 MBEDTLS_SSL_DEBUG_MSG(1, ("cipher info for %u not found",
1067 ciphersuite_info->cipher));
1068 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
Paul Bakker68884e32013-01-07 18:20:04 +01001069 }
1070
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001071 md_info = mbedtls_md_info_from_type(ciphersuite_info->mac);
1072 if (md_info == NULL) {
1073 MBEDTLS_SSL_DEBUG_MSG(1, ("mbedtls_md info for %u not found",
1074 (unsigned) ciphersuite_info->mac));
1075 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
Paul Bakker68884e32013-01-07 18:20:04 +01001076 }
1077
Hanno Beckera0e20d02019-05-15 14:03:01 +01001078#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Becker4bf74652019-04-26 16:22:27 +01001079 /* Copy own and peer's CID if the use of the CID
1080 * extension has been negotiated. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001081 if (ssl->handshake->cid_in_use == MBEDTLS_SSL_CID_ENABLED) {
1082 MBEDTLS_SSL_DEBUG_MSG(3, ("Copy CIDs into SSL transform"));
Hanno Becker8a7f9722019-04-30 13:52:29 +01001083
Hanno Becker05154c32019-05-03 15:23:51 +01001084 transform->in_cid_len = ssl->own_cid_len;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001085 memcpy(transform->in_cid, ssl->own_cid, ssl->own_cid_len);
1086 MBEDTLS_SSL_DEBUG_BUF(3, "Incoming CID", transform->in_cid,
1087 transform->in_cid_len);
Hanno Beckerd1f20352019-05-15 10:21:55 +01001088
1089 transform->out_cid_len = ssl->handshake->peer_cid_len;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001090 memcpy(transform->out_cid, ssl->handshake->peer_cid,
1091 ssl->handshake->peer_cid_len);
1092 MBEDTLS_SSL_DEBUG_BUF(3, "Outgoing CID", transform->out_cid,
1093 transform->out_cid_len);
Hanno Becker4bf74652019-04-26 16:22:27 +01001094 }
Hanno Beckera0e20d02019-05-15 14:03:01 +01001095#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Becker4bf74652019-04-26 16:22:27 +01001096
Paul Bakker5121ce52009-01-03 21:22:43 +00001097 /*
Manuel Pégourié-Gonnard9b108c22019-05-06 13:32:17 +02001098 * Compute key block using the PRF
Paul Bakker5121ce52009-01-03 21:22:43 +00001099 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001100 ret = tls_prf(master, 48, "key expansion", randbytes, 64, keyblk, 256);
1101 if (ret != 0) {
1102 MBEDTLS_SSL_DEBUG_RET(1, "prf", ret);
1103 return ret;
Manuel Pégourié-Gonnarde9608182015-03-26 11:47:47 +01001104 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001105
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001106 MBEDTLS_SSL_DEBUG_MSG(3, ("ciphersuite = %s",
1107 mbedtls_ssl_get_ciphersuite_name(ciphersuite)));
1108 MBEDTLS_SSL_DEBUG_BUF(3, "master secret", master, 48);
1109 MBEDTLS_SSL_DEBUG_BUF(4, "random bytes", randbytes, 64);
1110 MBEDTLS_SSL_DEBUG_BUF(4, "key block", keyblk, 256);
Paul Bakker5121ce52009-01-03 21:22:43 +00001111
Paul Bakker5121ce52009-01-03 21:22:43 +00001112 /*
1113 * Determine the appropriate key, IV and MAC length.
1114 */
Paul Bakker68884e32013-01-07 18:20:04 +01001115
Hanno Becker88aaf652017-12-27 08:17:40 +00001116 keylen = cipher_info->key_bitlen / 8;
Manuel Pégourié-Gonnarde800cd82014-06-18 15:34:40 +02001117
Hanno Becker8031d062018-01-03 15:32:31 +00001118#if defined(MBEDTLS_GCM_C) || \
1119 defined(MBEDTLS_CCM_C) || \
1120 defined(MBEDTLS_CHACHAPOLY_C)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001121 if (cipher_info->mode == MBEDTLS_MODE_GCM ||
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02001122 cipher_info->mode == MBEDTLS_MODE_CCM ||
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001123 cipher_info->mode == MBEDTLS_MODE_CHACHAPOLY) {
Hanno Beckerf704bef2018-11-16 15:21:18 +00001124 size_t explicit_ivlen;
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02001125
Manuel Pégourié-Gonnarde800cd82014-06-18 15:34:40 +02001126 transform->maclen = 0;
Hanno Becker81c7b182017-11-09 18:39:33 +00001127 mac_key_len = 0;
Hanno Beckere694c3e2017-12-27 21:34:08 +00001128 transform->taglen =
1129 ciphersuite_info->flags & MBEDTLS_CIPHERSUITE_SHORT_TAG ? 8 : 16;
Manuel Pégourié-Gonnarde800cd82014-06-18 15:34:40 +02001130
Hanno Becker447558d2020-05-28 07:36:33 +01001131 /* All modes haves 96-bit IVs, but the length of the static parts vary
1132 * with mode and version:
1133 * - For GCM and CCM in TLS 1.2, there's a static IV of 4 Bytes
1134 * (to be concatenated with a dynamically chosen IV of 8 Bytes)
Hanno Beckerf93c2d72020-05-28 07:39:43 +01001135 * - For ChaChaPoly in TLS 1.2, and all modes in TLS 1.3, there's
1136 * a static IV of 12 Bytes (to be XOR'ed with the 8 Byte record
1137 * sequence number).
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02001138 */
Paul Bakker68884e32013-01-07 18:20:04 +01001139 transform->ivlen = 12;
Hanno Beckerf93c2d72020-05-28 07:39:43 +01001140#if defined(MBEDTLS_SSL_PROTO_TLS1_3_EXPERIMENTAL)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001141 if (minor_ver == MBEDTLS_SSL_MINOR_VERSION_4) {
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02001142 transform->fixed_ivlen = 12;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001143 } else
Hanno Beckerf93c2d72020-05-28 07:39:43 +01001144#endif /* MBEDTLS_SSL_PROTO_TLS1_3_EXPERIMENTAL */
1145 {
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001146 if (cipher_info->mode == MBEDTLS_MODE_CHACHAPOLY) {
Hanno Beckerf93c2d72020-05-28 07:39:43 +01001147 transform->fixed_ivlen = 12;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001148 } else {
Hanno Beckerf93c2d72020-05-28 07:39:43 +01001149 transform->fixed_ivlen = 4;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001150 }
Hanno Beckerf93c2d72020-05-28 07:39:43 +01001151 }
Manuel Pégourié-Gonnarde800cd82014-06-18 15:34:40 +02001152
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02001153 /* Minimum length of encrypted record */
1154 explicit_ivlen = transform->ivlen - transform->fixed_ivlen;
Hanno Beckere694c3e2017-12-27 21:34:08 +00001155 transform->minlen = explicit_ivlen + transform->taglen;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001156 } else
Hanno Becker8031d062018-01-03 15:32:31 +00001157#endif /* MBEDTLS_GCM_C || MBEDTLS_CCM_C || MBEDTLS_CHACHAPOLY_C */
1158#if defined(MBEDTLS_SSL_SOME_MODES_USE_MAC)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001159 if (cipher_info->mode == MBEDTLS_MODE_STREAM ||
1160 cipher_info->mode == MBEDTLS_MODE_CBC) {
Manuel Pégourié-Gonnarde800cd82014-06-18 15:34:40 +02001161 /* Initialize HMAC contexts */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001162 if ((ret = mbedtls_md_setup(&transform->md_ctx_enc, md_info, 1)) != 0 ||
1163 (ret = mbedtls_md_setup(&transform->md_ctx_dec, md_info, 1)) != 0) {
1164 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_md_setup", ret);
Ron Eldore6992702019-05-07 18:27:13 +03001165 goto end;
Paul Bakker68884e32013-01-07 18:20:04 +01001166 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001167
Manuel Pégourié-Gonnarde800cd82014-06-18 15:34:40 +02001168 /* Get MAC length */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001169 mac_key_len = mbedtls_md_get_size(md_info);
Hanno Becker81c7b182017-11-09 18:39:33 +00001170 transform->maclen = mac_key_len;
Manuel Pégourié-Gonnarde800cd82014-06-18 15:34:40 +02001171
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001172#if defined(MBEDTLS_SSL_TRUNCATED_HMAC)
Manuel Pégourié-Gonnarde800cd82014-06-18 15:34:40 +02001173 /*
1174 * If HMAC is to be truncated, we shall keep the leftmost bytes,
1175 * (rfc 6066 page 13 or rfc 2104 section 4),
1176 * so we only need to adjust the length here.
1177 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001178 if (trunc_hmac == MBEDTLS_SSL_TRUNC_HMAC_ENABLED) {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001179 transform->maclen = MBEDTLS_SSL_TRUNCATED_HMAC_LEN;
Hanno Beckere89353a2017-11-20 16:36:41 +00001180
1181#if defined(MBEDTLS_SSL_TRUNCATED_HMAC_COMPAT)
1182 /* Fall back to old, non-compliant version of the truncated
Hanno Becker563423f2017-11-21 17:20:17 +00001183 * HMAC implementation which also truncates the key
1184 * (Mbed TLS versions from 1.3 to 2.6.0) */
Hanno Beckere89353a2017-11-20 16:36:41 +00001185 mac_key_len = transform->maclen;
1186#endif
1187 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001188#endif /* MBEDTLS_SSL_TRUNCATED_HMAC */
Manuel Pégourié-Gonnarde800cd82014-06-18 15:34:40 +02001189
1190 /* IV length */
Paul Bakker68884e32013-01-07 18:20:04 +01001191 transform->ivlen = cipher_info->iv_size;
Paul Bakker5121ce52009-01-03 21:22:43 +00001192
Manuel Pégourié-Gonnardeaa76f72014-06-18 16:06:02 +02001193 /* Minimum length */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001194 if (cipher_info->mode == MBEDTLS_MODE_STREAM) {
Manuel Pégourié-Gonnardeaa76f72014-06-18 16:06:02 +02001195 transform->minlen = transform->maclen;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001196 } else {
Manuel Pégourié-Gonnardeaa76f72014-06-18 16:06:02 +02001197 /*
1198 * GenericBlockCipher:
Manuel Pégourié-Gonnard169dd6a2014-11-04 16:15:39 +01001199 * 1. if EtM is in use: one block plus MAC
1200 * otherwise: * first multiple of blocklen greater than maclen
1201 * 2. IV except for SSL3 and TLS 1.0
Manuel Pégourié-Gonnardeaa76f72014-06-18 16:06:02 +02001202 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001203#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001204 if (encrypt_then_mac == MBEDTLS_SSL_ETM_ENABLED) {
Manuel Pégourié-Gonnard169dd6a2014-11-04 16:15:39 +01001205 transform->minlen = transform->maclen
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001206 + cipher_info->block_size;
1207 } else
Manuel Pégourié-Gonnard169dd6a2014-11-04 16:15:39 +01001208#endif
1209 {
1210 transform->minlen = transform->maclen
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001211 + cipher_info->block_size
1212 - transform->maclen % cipher_info->block_size;
Manuel Pégourié-Gonnard169dd6a2014-11-04 16:15:39 +01001213 }
Manuel Pégourié-Gonnardeaa76f72014-06-18 16:06:02 +02001214
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001215#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001216 if (minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 ||
1217 minor_ver == MBEDTLS_SSL_MINOR_VERSION_1) {
Manuel Pégourié-Gonnardeaa76f72014-06-18 16:06:02 +02001218 ; /* No need to adjust minlen */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001219 } else
Manuel Pégourié-Gonnardeaa76f72014-06-18 16:06:02 +02001220#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001221#if defined(MBEDTLS_SSL_PROTO_TLS1_1) || defined(MBEDTLS_SSL_PROTO_TLS1_2)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001222 if (minor_ver == MBEDTLS_SSL_MINOR_VERSION_2 ||
1223 minor_ver == MBEDTLS_SSL_MINOR_VERSION_3) {
Manuel Pégourié-Gonnardeaa76f72014-06-18 16:06:02 +02001224 transform->minlen += transform->ivlen;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001225 } else
Manuel Pégourié-Gonnardeaa76f72014-06-18 16:06:02 +02001226#endif
1227 {
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001228 MBEDTLS_SSL_DEBUG_MSG(1, ("should never happen"));
Ron Eldore6992702019-05-07 18:27:13 +03001229 ret = MBEDTLS_ERR_SSL_INTERNAL_ERROR;
1230 goto end;
Manuel Pégourié-Gonnardeaa76f72014-06-18 16:06:02 +02001231 }
Paul Bakker68884e32013-01-07 18:20:04 +01001232 }
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001233 } else
Hanno Becker8031d062018-01-03 15:32:31 +00001234#endif /* MBEDTLS_SSL_SOME_MODES_USE_MAC */
1235 {
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001236 MBEDTLS_SSL_DEBUG_MSG(1, ("should never happen"));
1237 return MBEDTLS_ERR_SSL_INTERNAL_ERROR;
Hanno Becker8031d062018-01-03 15:32:31 +00001238 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001239
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001240 MBEDTLS_SSL_DEBUG_MSG(3, ("keylen: %u, minlen: %u, ivlen: %u, maclen: %u",
1241 (unsigned) keylen,
1242 (unsigned) transform->minlen,
1243 (unsigned) transform->ivlen,
1244 (unsigned) transform->maclen));
Paul Bakker5121ce52009-01-03 21:22:43 +00001245
1246 /*
1247 * Finally setup the cipher contexts, IVs and MAC secrets.
1248 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001249#if defined(MBEDTLS_SSL_CLI_C)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001250 if (endpoint == MBEDTLS_SSL_IS_CLIENT) {
Hanno Becker81c7b182017-11-09 18:39:33 +00001251 key1 = keyblk + mac_key_len * 2;
Hanno Becker88aaf652017-12-27 08:17:40 +00001252 key2 = keyblk + mac_key_len * 2 + keylen;
Paul Bakker5121ce52009-01-03 21:22:43 +00001253
Paul Bakker68884e32013-01-07 18:20:04 +01001254 mac_enc = keyblk;
Hanno Becker81c7b182017-11-09 18:39:33 +00001255 mac_dec = keyblk + mac_key_len;
Paul Bakker5121ce52009-01-03 21:22:43 +00001256
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001257 /*
1258 * This is not used in TLS v1.1.
1259 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001260 iv_copy_len = (transform->fixed_ivlen) ?
1261 transform->fixed_ivlen : transform->ivlen;
1262 memcpy(transform->iv_enc, key2 + keylen, iv_copy_len);
1263 memcpy(transform->iv_dec, key2 + keylen + iv_copy_len,
1264 iv_copy_len);
1265 } else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001266#endif /* MBEDTLS_SSL_CLI_C */
1267#if defined(MBEDTLS_SSL_SRV_C)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001268 if (endpoint == MBEDTLS_SSL_IS_SERVER) {
Hanno Becker88aaf652017-12-27 08:17:40 +00001269 key1 = keyblk + mac_key_len * 2 + keylen;
Hanno Becker81c7b182017-11-09 18:39:33 +00001270 key2 = keyblk + mac_key_len * 2;
Paul Bakker5121ce52009-01-03 21:22:43 +00001271
Hanno Becker81c7b182017-11-09 18:39:33 +00001272 mac_enc = keyblk + mac_key_len;
Paul Bakker68884e32013-01-07 18:20:04 +01001273 mac_dec = keyblk;
Paul Bakker5121ce52009-01-03 21:22:43 +00001274
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001275 /*
1276 * This is not used in TLS v1.1.
1277 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001278 iv_copy_len = (transform->fixed_ivlen) ?
1279 transform->fixed_ivlen : transform->ivlen;
1280 memcpy(transform->iv_dec, key1 + keylen, iv_copy_len);
1281 memcpy(transform->iv_enc, key1 + keylen + iv_copy_len,
1282 iv_copy_len);
1283 } else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001284#endif /* MBEDTLS_SSL_SRV_C */
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01001285 {
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001286 MBEDTLS_SSL_DEBUG_MSG(1, ("should never happen"));
Ron Eldore6992702019-05-07 18:27:13 +03001287 ret = MBEDTLS_ERR_SSL_INTERNAL_ERROR;
1288 goto end;
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01001289 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001290
Hanno Beckerd56ed242018-01-03 15:32:51 +00001291#if defined(MBEDTLS_SSL_SOME_MODES_USE_MAC)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001292#if defined(MBEDTLS_SSL_PROTO_SSL3)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001293 if (minor_ver == MBEDTLS_SSL_MINOR_VERSION_0) {
1294 if (mac_key_len > sizeof(transform->mac_enc)) {
1295 MBEDTLS_SSL_DEBUG_MSG(1, ("should never happen"));
Ron Eldore6992702019-05-07 18:27:13 +03001296 ret = MBEDTLS_ERR_SSL_INTERNAL_ERROR;
1297 goto end;
Manuel Pégourié-Gonnard7cfdcb82014-01-18 18:22:55 +01001298 }
1299
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001300 memcpy(transform->mac_enc, mac_enc, mac_key_len);
1301 memcpy(transform->mac_dec, mac_dec, mac_key_len);
1302 } else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001303#endif /* MBEDTLS_SSL_PROTO_SSL3 */
1304#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1) || \
1305 defined(MBEDTLS_SSL_PROTO_TLS1_2)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001306 if (minor_ver >= MBEDTLS_SSL_MINOR_VERSION_1) {
Gilles Peskine039fd122018-03-19 19:06:08 +01001307 /* For HMAC-based ciphersuites, initialize the HMAC transforms.
1308 For AEAD-based ciphersuites, there is nothing to do here. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001309 if (mac_key_len != 0) {
1310 ret = mbedtls_md_hmac_starts(&transform->md_ctx_enc,
1311 mac_enc, mac_key_len);
1312 if (ret != 0) {
Gilles Peskine2b3f21d2021-12-10 21:35:10 +01001313 goto end;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001314 }
1315 ret = mbedtls_md_hmac_starts(&transform->md_ctx_dec,
1316 mac_dec, mac_key_len);
1317 if (ret != 0) {
Gilles Peskine2b3f21d2021-12-10 21:35:10 +01001318 goto end;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001319 }
Gilles Peskine039fd122018-03-19 19:06:08 +01001320 }
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001321 } else
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001322#endif
Paul Bakker577e0062013-08-28 11:57:20 +02001323 {
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001324 MBEDTLS_SSL_DEBUG_MSG(1, ("should never happen"));
Ron Eldore6992702019-05-07 18:27:13 +03001325 ret = MBEDTLS_ERR_SSL_INTERNAL_ERROR;
1326 goto end;
Paul Bakker577e0062013-08-28 11:57:20 +02001327 }
Hanno Beckerd56ed242018-01-03 15:32:51 +00001328#endif /* MBEDTLS_SSL_SOME_MODES_USE_MAC */
Paul Bakker68884e32013-01-07 18:20:04 +01001329
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001330#if defined(MBEDTLS_SSL_HW_RECORD_ACCEL)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001331 if (mbedtls_ssl_hw_record_init != NULL) {
sander-visser1abe8ee2020-05-06 21:27:14 +02001332 ret = 0;
Paul Bakker05ef8352012-05-08 09:17:57 +00001333
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001334 MBEDTLS_SSL_DEBUG_MSG(2, ("going for mbedtls_ssl_hw_record_init()"));
Paul Bakker05ef8352012-05-08 09:17:57 +00001335
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001336 if ((ret = mbedtls_ssl_hw_record_init(ssl, key1, key2, keylen,
1337 transform->iv_enc, transform->iv_dec,
1338 iv_copy_len,
1339 mac_enc, mac_dec,
1340 mac_key_len)) != 0) {
1341 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ssl_hw_record_init", ret);
Ron Eldore6992702019-05-07 18:27:13 +03001342 ret = MBEDTLS_ERR_SSL_HW_ACCEL_FAILED;
1343 goto end;
Paul Bakker05ef8352012-05-08 09:17:57 +00001344 }
1345 }
Hanno Beckerd56ed242018-01-03 15:32:51 +00001346#else
1347 ((void) mac_dec);
1348 ((void) mac_enc);
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001349#endif /* MBEDTLS_SSL_HW_RECORD_ACCEL */
Paul Bakker05ef8352012-05-08 09:17:57 +00001350
Manuel Pégourié-Gonnard024b6df2015-10-19 13:52:53 +02001351#if defined(MBEDTLS_SSL_EXPORT_KEYS)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001352 if (ssl->conf->f_export_keys != NULL) {
1353 ssl->conf->f_export_keys(ssl->conf->p_export_keys,
1354 master, keyblk,
1355 mac_key_len, keylen,
1356 iv_copy_len);
Robert Cragie4feb7ae2015-10-02 13:33:37 +01001357 }
Ron Eldorf5cc10d2019-05-07 18:33:40 +03001358
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001359 if (ssl->conf->f_export_keys_ext != NULL) {
1360 ssl->conf->f_export_keys_ext(ssl->conf->p_export_keys,
1361 master, keyblk,
1362 mac_key_len, keylen,
1363 iv_copy_len,
1364 randbytes + 32,
1365 randbytes,
1366 tls_prf_get_type(tls_prf));
Ron Eldorf5cc10d2019-05-07 18:33:40 +03001367 }
Robert Cragie4feb7ae2015-10-02 13:33:37 +01001368#endif
1369
David Horstmannd4f22082022-10-26 18:25:14 +01001370 do_mbedtls_cipher_setup = 1;
Hanno Beckerf704bef2018-11-16 15:21:18 +00001371#if defined(MBEDTLS_USE_PSA_CRYPTO)
Hanno Beckercb1cc802018-11-17 22:27:38 +00001372
1373 /* Only use PSA-based ciphers for TLS-1.2.
1374 * That's relevant at least for TLS-1.0, where
1375 * we assume that mbedtls_cipher_crypt() updates
1376 * the structure field for the IV, which the PSA-based
1377 * implementation currently doesn't. */
1378#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001379 if (minor_ver == MBEDTLS_SSL_MINOR_VERSION_3) {
1380 ret = mbedtls_cipher_setup_psa(&transform->cipher_ctx_enc,
1381 cipher_info, transform->taglen);
1382 if (ret != 0 && ret != MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE) {
1383 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_cipher_setup_psa", ret);
Ron Eldore6992702019-05-07 18:27:13 +03001384 goto end;
Hanno Beckercb1cc802018-11-17 22:27:38 +00001385 }
1386
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001387 if (ret == 0) {
1388 MBEDTLS_SSL_DEBUG_MSG(3, ("Successfully setup PSA-based encryption cipher context"));
Hanno Beckercb1cc802018-11-17 22:27:38 +00001389 psa_fallthrough = 0;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001390 } else {
1391 MBEDTLS_SSL_DEBUG_MSG(1,
1392 (
1393 "Failed to setup PSA-based cipher context for record encryption - fall through to default setup."));
Hanno Beckercb1cc802018-11-17 22:27:38 +00001394 psa_fallthrough = 1;
1395 }
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001396 } else {
Hanno Beckercb1cc802018-11-17 22:27:38 +00001397 psa_fallthrough = 1;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001398 }
Hanno Beckercb1cc802018-11-17 22:27:38 +00001399#else
1400 psa_fallthrough = 1;
1401#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Hanno Beckerf704bef2018-11-16 15:21:18 +00001402
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001403 if (psa_fallthrough == 0) {
David Horstmannd4f22082022-10-26 18:25:14 +01001404 do_mbedtls_cipher_setup = 0;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001405 }
Hanno Beckerf704bef2018-11-16 15:21:18 +00001406#endif /* MBEDTLS_USE_PSA_CRYPTO */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001407 if (do_mbedtls_cipher_setup &&
1408 (ret = mbedtls_cipher_setup(&transform->cipher_ctx_enc,
1409 cipher_info)) != 0) {
1410 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_cipher_setup", ret);
Ron Eldore6992702019-05-07 18:27:13 +03001411 goto end;
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001412 }
Paul Bakkerda02a7f2013-08-31 17:25:14 +02001413
David Horstmannd4f22082022-10-26 18:25:14 +01001414 do_mbedtls_cipher_setup = 1;
Hanno Beckerf704bef2018-11-16 15:21:18 +00001415#if defined(MBEDTLS_USE_PSA_CRYPTO)
Hanno Beckercb1cc802018-11-17 22:27:38 +00001416 /* Only use PSA-based ciphers for TLS-1.2.
1417 * That's relevant at least for TLS-1.0, where
1418 * we assume that mbedtls_cipher_crypt() updates
1419 * the structure field for the IV, which the PSA-based
1420 * implementation currently doesn't. */
1421#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001422 if (minor_ver == MBEDTLS_SSL_MINOR_VERSION_3) {
1423 ret = mbedtls_cipher_setup_psa(&transform->cipher_ctx_dec,
1424 cipher_info, transform->taglen);
1425 if (ret != 0 && ret != MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE) {
1426 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_cipher_setup_psa", ret);
Ron Eldore6992702019-05-07 18:27:13 +03001427 goto end;
Hanno Beckercb1cc802018-11-17 22:27:38 +00001428 }
1429
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001430 if (ret == 0) {
1431 MBEDTLS_SSL_DEBUG_MSG(3, ("Successfully setup PSA-based decryption cipher context"));
Hanno Beckercb1cc802018-11-17 22:27:38 +00001432 psa_fallthrough = 0;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001433 } else {
1434 MBEDTLS_SSL_DEBUG_MSG(1,
1435 (
1436 "Failed to setup PSA-based cipher context for record decryption - fall through to default setup."));
Hanno Beckercb1cc802018-11-17 22:27:38 +00001437 psa_fallthrough = 1;
1438 }
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001439 } else {
Hanno Beckercb1cc802018-11-17 22:27:38 +00001440 psa_fallthrough = 1;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001441 }
Hanno Beckercb1cc802018-11-17 22:27:38 +00001442#else
1443 psa_fallthrough = 1;
1444#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Hanno Beckerf704bef2018-11-16 15:21:18 +00001445
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001446 if (psa_fallthrough == 0) {
David Horstmannd4f22082022-10-26 18:25:14 +01001447 do_mbedtls_cipher_setup = 0;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001448 }
Hanno Beckerf704bef2018-11-16 15:21:18 +00001449#endif /* MBEDTLS_USE_PSA_CRYPTO */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001450 if (do_mbedtls_cipher_setup &&
1451 (ret = mbedtls_cipher_setup(&transform->cipher_ctx_dec,
1452 cipher_info)) != 0) {
1453 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_cipher_setup", ret);
Ron Eldore6992702019-05-07 18:27:13 +03001454 goto end;
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001455 }
Paul Bakkerda02a7f2013-08-31 17:25:14 +02001456
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001457 if ((ret = mbedtls_cipher_setkey(&transform->cipher_ctx_enc, key1,
1458 cipher_info->key_bitlen,
1459 MBEDTLS_ENCRYPT)) != 0) {
1460 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_cipher_setkey", ret);
Ron Eldore6992702019-05-07 18:27:13 +03001461 goto end;
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001462 }
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02001463
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001464 if ((ret = mbedtls_cipher_setkey(&transform->cipher_ctx_dec, key2,
1465 cipher_info->key_bitlen,
1466 MBEDTLS_DECRYPT)) != 0) {
1467 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_cipher_setkey", ret);
Ron Eldore6992702019-05-07 18:27:13 +03001468 goto end;
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001469 }
Paul Bakkerda02a7f2013-08-31 17:25:14 +02001470
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001471#if defined(MBEDTLS_CIPHER_MODE_CBC)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001472 if (cipher_info->mode == MBEDTLS_MODE_CBC) {
1473 if ((ret = mbedtls_cipher_set_padding_mode(&transform->cipher_ctx_enc,
1474 MBEDTLS_PADDING_NONE)) != 0) {
1475 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_cipher_set_padding_mode", ret);
Ron Eldore6992702019-05-07 18:27:13 +03001476 goto end;
Manuel Pégourié-Gonnard126a66f2013-10-25 18:33:32 +02001477 }
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001478
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001479 if ((ret = mbedtls_cipher_set_padding_mode(&transform->cipher_ctx_dec,
1480 MBEDTLS_PADDING_NONE)) != 0) {
1481 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_cipher_set_padding_mode", ret);
Ron Eldore6992702019-05-07 18:27:13 +03001482 goto end;
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001483 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001484 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001485#endif /* MBEDTLS_CIPHER_MODE_CBC */
Paul Bakker5121ce52009-01-03 21:22:43 +00001486
Paul Bakker5121ce52009-01-03 21:22:43 +00001487
Manuel Pégourié-Gonnardd73b47f2019-05-06 12:44:24 +02001488 /* Initialize Zlib contexts */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001489#if defined(MBEDTLS_ZLIB_SUPPORT)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001490 if (compression == MBEDTLS_SSL_COMPRESS_DEFLATE) {
1491 MBEDTLS_SSL_DEBUG_MSG(3, ("Initializing zlib states"));
Paul Bakker2770fbd2012-07-03 13:30:23 +00001492
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001493 memset(&transform->ctx_deflate, 0, sizeof(transform->ctx_deflate));
1494 memset(&transform->ctx_inflate, 0, sizeof(transform->ctx_inflate));
Paul Bakker2770fbd2012-07-03 13:30:23 +00001495
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001496 if (deflateInit(&transform->ctx_deflate,
1497 Z_DEFAULT_COMPRESSION) != Z_OK ||
1498 inflateInit(&transform->ctx_inflate) != Z_OK) {
1499 MBEDTLS_SSL_DEBUG_MSG(1, ("Failed to initialize compression"));
Ron Eldore6992702019-05-07 18:27:13 +03001500 ret = MBEDTLS_ERR_SSL_COMPRESSION_FAILED;
1501 goto end;
Paul Bakker2770fbd2012-07-03 13:30:23 +00001502 }
1503 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001504#endif /* MBEDTLS_ZLIB_SUPPORT */
Paul Bakker2770fbd2012-07-03 13:30:23 +00001505
Ron Eldore6992702019-05-07 18:27:13 +03001506end:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001507 mbedtls_platform_zeroize(keyblk, sizeof(keyblk));
1508 return ret;
Paul Bakker5121ce52009-01-03 21:22:43 +00001509}
1510
Manuel Pégourié-Gonnard8d2805c2019-04-30 12:08:59 +02001511/*
Manuel Pégourié-Gonnard47e33e12019-05-20 10:10:17 +02001512 * Set appropriate PRF function and other SSL / TLS 1.0/1.1 / TLS1.2 functions
Manuel Pégourié-Gonnard8d2805c2019-04-30 12:08:59 +02001513 *
1514 * Inputs:
1515 * - SSL/TLS minor version
1516 * - hash associated with the ciphersuite (only used by TLS 1.2)
1517 *
Manuel Pégourié-Gonnard31d3ef12019-05-10 10:25:00 +02001518 * Outputs:
Manuel Pégourié-Gonnard8d2805c2019-04-30 12:08:59 +02001519 * - the tls_prf, calc_verify and calc_finished members of handshake structure
1520 */
Manuel Pégourié-Gonnardd904d662022-06-17 10:24:00 +02001521MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001522static int ssl_set_handshake_prfs(mbedtls_ssl_handshake_params *handshake,
1523 int minor_ver,
1524 mbedtls_md_type_t hash)
Manuel Pégourié-Gonnard1b00c4f2019-04-30 11:54:22 +02001525{
Gilles Peskinefc9c07f2021-05-12 22:51:53 +02001526#if !defined(MBEDTLS_SSL_PROTO_TLS1_2) || \
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001527 !(defined(MBEDTLS_SHA512_C) && !defined(MBEDTLS_SHA512_NO_SHA384))
Manuel Pégourié-Gonnard8d2805c2019-04-30 12:08:59 +02001528 (void) hash;
1529#endif
Manuel Pégourié-Gonnard1b00c4f2019-04-30 11:54:22 +02001530
Manuel Pégourié-Gonnard1b00c4f2019-04-30 11:54:22 +02001531#if defined(MBEDTLS_SSL_PROTO_SSL3)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001532 if (minor_ver == MBEDTLS_SSL_MINOR_VERSION_0) {
Manuel Pégourié-Gonnard1b00c4f2019-04-30 11:54:22 +02001533 handshake->tls_prf = ssl3_prf;
1534 handshake->calc_verify = ssl_calc_verify_ssl;
1535 handshake->calc_finished = ssl_calc_finished_ssl;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001536 } else
Manuel Pégourié-Gonnard1b00c4f2019-04-30 11:54:22 +02001537#endif
1538#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001539 if (minor_ver < MBEDTLS_SSL_MINOR_VERSION_3) {
Manuel Pégourié-Gonnard1b00c4f2019-04-30 11:54:22 +02001540 handshake->tls_prf = tls1_prf;
1541 handshake->calc_verify = ssl_calc_verify_tls;
1542 handshake->calc_finished = ssl_calc_finished_tls;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001543 } else
Manuel Pégourié-Gonnard1b00c4f2019-04-30 11:54:22 +02001544#endif
1545#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
Gilles Peskined2d59372021-05-12 22:43:27 +02001546#if defined(MBEDTLS_SHA512_C) && !defined(MBEDTLS_SHA512_NO_SHA384)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001547 if (minor_ver == MBEDTLS_SSL_MINOR_VERSION_3 &&
1548 hash == MBEDTLS_MD_SHA384) {
Manuel Pégourié-Gonnard1b00c4f2019-04-30 11:54:22 +02001549 handshake->tls_prf = tls_prf_sha384;
1550 handshake->calc_verify = ssl_calc_verify_tls_sha384;
1551 handshake->calc_finished = ssl_calc_finished_tls_sha384;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001552 } else
Manuel Pégourié-Gonnard1b00c4f2019-04-30 11:54:22 +02001553#endif
1554#if defined(MBEDTLS_SHA256_C)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001555 if (minor_ver == MBEDTLS_SSL_MINOR_VERSION_3) {
Manuel Pégourié-Gonnard1b00c4f2019-04-30 11:54:22 +02001556 handshake->tls_prf = tls_prf_sha256;
1557 handshake->calc_verify = ssl_calc_verify_tls_sha256;
1558 handshake->calc_finished = ssl_calc_finished_tls_sha256;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001559 } else
Manuel Pégourié-Gonnard1b00c4f2019-04-30 11:54:22 +02001560#endif
1561#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
1562 {
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001563 return MBEDTLS_ERR_SSL_INTERNAL_ERROR;
Manuel Pégourié-Gonnard1b00c4f2019-04-30 11:54:22 +02001564 }
1565
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001566 return 0;
Manuel Pégourié-Gonnard1b00c4f2019-04-30 11:54:22 +02001567}
1568
Manuel Pégourié-Gonnard9951b712019-04-30 12:08:59 +02001569/*
Manuel Pégourié-Gonnard85680c42019-05-03 09:16:16 +02001570 * Compute master secret if needed
Manuel Pégourié-Gonnardde047ad2019-05-03 09:46:14 +02001571 *
1572 * Parameters:
1573 * [in/out] handshake
1574 * [in] resume, premaster, extended_ms, calc_verify, tls_prf
Manuel Pégourié-Gonnardde718b92019-05-03 11:43:28 +02001575 * (PSA-PSK) ciphersuite_info, psk_opaque
Manuel Pégourié-Gonnardde047ad2019-05-03 09:46:14 +02001576 * [out] premaster (cleared)
Manuel Pégourié-Gonnardde047ad2019-05-03 09:46:14 +02001577 * [out] master
1578 * [in] ssl: optionally used for debugging, EMS and PSA-PSK
1579 * debug: conf->f_dbg, conf->p_dbg
1580 * EMS: passed to calc_verify (debug + (SSL3) session_negotiate)
Manuel Pégourié-Gonnardde718b92019-05-03 11:43:28 +02001581 * PSA-PSA: minor_ver, conf
Manuel Pégourié-Gonnard9951b712019-04-30 12:08:59 +02001582 */
Manuel Pégourié-Gonnardd904d662022-06-17 10:24:00 +02001583MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001584static int ssl_compute_master(mbedtls_ssl_handshake_params *handshake,
1585 unsigned char *master,
1586 const mbedtls_ssl_context *ssl)
Manuel Pégourié-Gonnard9951b712019-04-30 12:08:59 +02001587{
Janos Follath865b3eb2019-12-16 11:46:15 +00001588 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnard9951b712019-04-30 12:08:59 +02001589
1590 /* cf. RFC 5246, Section 8.1:
1591 * "The master secret is always exactly 48 bytes in length." */
1592 size_t const master_secret_len = 48;
1593
1594#if defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET)
1595 unsigned char session_hash[48];
1596#endif /* MBEDTLS_SSL_EXTENDED_MASTER_SECRET */
1597
Manuel Pégourié-Gonnard85680c42019-05-03 09:16:16 +02001598 /* The label for the KDF used for key expansion.
1599 * This is either "master secret" or "extended master secret"
1600 * depending on whether the Extended Master Secret extension
1601 * is used. */
1602 char const *lbl = "master secret";
1603
1604 /* The salt for the KDF used for key expansion.
1605 * - If the Extended Master Secret extension is not used,
1606 * this is ClientHello.Random + ServerHello.Random
1607 * (see Sect. 8.1 in RFC 5246).
1608 * - If the Extended Master Secret extension is used,
1609 * this is the transcript of the handshake so far.
1610 * (see Sect. 4 in RFC 7627). */
1611 unsigned char const *salt = handshake->randbytes;
1612 size_t salt_len = 64;
1613
Manuel Pégourié-Gonnardde047ad2019-05-03 09:46:14 +02001614#if !defined(MBEDTLS_DEBUG_C) && \
1615 !defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET) && \
1616 !(defined(MBEDTLS_USE_PSA_CRYPTO) && \
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001617 defined(MBEDTLS_KEY_EXCHANGE_PSK_ENABLED))
Manuel Pégourié-Gonnarda7505d12019-05-07 10:17:56 +02001618 ssl = NULL; /* make sure we don't use it except for those cases */
Manuel Pégourié-Gonnardde047ad2019-05-03 09:46:14 +02001619 (void) ssl;
1620#endif
Manuel Pégourié-Gonnardde047ad2019-05-03 09:46:14 +02001621
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001622 if (handshake->resume != 0) {
1623 MBEDTLS_SSL_DEBUG_MSG(3, ("no premaster (session resumed)"));
1624 return 0;
Manuel Pégourié-Gonnard9951b712019-04-30 12:08:59 +02001625 }
Manuel Pégourié-Gonnard9951b712019-04-30 12:08:59 +02001626
1627#if defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001628 if (handshake->extended_ms == MBEDTLS_SSL_EXTENDED_MS_ENABLED) {
Manuel Pégourié-Gonnard85680c42019-05-03 09:16:16 +02001629 lbl = "extended master secret";
1630 salt = session_hash;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001631 handshake->calc_verify(ssl, session_hash, &salt_len);
Manuel Pégourié-Gonnard85680c42019-05-03 09:16:16 +02001632
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001633 MBEDTLS_SSL_DEBUG_BUF(3, "session hash for extended master secret",
1634 session_hash, salt_len);
Manuel Pégourié-Gonnard85680c42019-05-03 09:16:16 +02001635 }
Manuel Pégourié-Gonnard9951b712019-04-30 12:08:59 +02001636#endif /* MBEDTLS_SSL_EXTENDED_MS_ENABLED */
1637
1638#if defined(MBEDTLS_USE_PSA_CRYPTO) && \
Manuel Pégourié-Gonnardde047ad2019-05-03 09:46:14 +02001639 defined(MBEDTLS_KEY_EXCHANGE_PSK_ENABLED)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001640 if (handshake->ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_PSK &&
Manuel Pégourié-Gonnardde718b92019-05-03 11:43:28 +02001641 ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_3 &&
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001642 ssl_use_opaque_psk(ssl) == 1) {
Manuel Pégourié-Gonnard85680c42019-05-03 09:16:16 +02001643 /* Perform PSK-to-MS expansion in a single step. */
1644 psa_status_t status;
1645 psa_algorithm_t alg;
Ronald Croncf56a0a2020-08-04 09:51:30 +02001646 psa_key_id_t psk;
Manuel Pégourié-Gonnard85680c42019-05-03 09:16:16 +02001647 psa_key_derivation_operation_t derivation =
1648 PSA_KEY_DERIVATION_OPERATION_INIT;
Manuel Pégourié-Gonnardde718b92019-05-03 11:43:28 +02001649 mbedtls_md_type_t hash_alg = handshake->ciphersuite_info->mac;
Manuel Pégourié-Gonnard9951b712019-04-30 12:08:59 +02001650
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001651 MBEDTLS_SSL_DEBUG_MSG(2, ("perform PSA-based PSK-to-MS expansion"));
Manuel Pégourié-Gonnard9951b712019-04-30 12:08:59 +02001652
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001653 psk = mbedtls_ssl_get_opaque_psk(ssl);
Manuel Pégourié-Gonnard9951b712019-04-30 12:08:59 +02001654
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001655 if (hash_alg == MBEDTLS_MD_SHA384) {
Manuel Pégourié-Gonnard85680c42019-05-03 09:16:16 +02001656 alg = PSA_ALG_TLS12_PSK_TO_MS(PSA_ALG_SHA_384);
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001657 } else {
Manuel Pégourié-Gonnard85680c42019-05-03 09:16:16 +02001658 alg = PSA_ALG_TLS12_PSK_TO_MS(PSA_ALG_SHA_256);
Manuel Pégourié-Gonnard9951b712019-04-30 12:08:59 +02001659 }
Manuel Pégourié-Gonnard85680c42019-05-03 09:16:16 +02001660
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001661 status = setup_psa_key_derivation(&derivation, psk, alg,
1662 salt, salt_len,
1663 (unsigned char const *) lbl,
1664 (size_t) strlen(lbl),
1665 master_secret_len);
1666 if (status != PSA_SUCCESS) {
1667 psa_key_derivation_abort(&derivation);
1668 return MBEDTLS_ERR_SSL_HW_ACCEL_FAILED;
Manuel Pégourié-Gonnard85680c42019-05-03 09:16:16 +02001669 }
1670
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001671 status = psa_key_derivation_output_bytes(&derivation,
1672 master,
1673 master_secret_len);
1674 if (status != PSA_SUCCESS) {
1675 psa_key_derivation_abort(&derivation);
1676 return MBEDTLS_ERR_SSL_HW_ACCEL_FAILED;
1677 }
1678
1679 status = psa_key_derivation_abort(&derivation);
1680 if (status != PSA_SUCCESS) {
1681 return MBEDTLS_ERR_SSL_HW_ACCEL_FAILED;
1682 }
1683 } else
Manuel Pégourié-Gonnard85680c42019-05-03 09:16:16 +02001684#endif
1685 {
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001686 ret = handshake->tls_prf(handshake->premaster, handshake->pmslen,
1687 lbl, salt, salt_len,
1688 master,
1689 master_secret_len);
1690 if (ret != 0) {
1691 MBEDTLS_SSL_DEBUG_RET(1, "prf", ret);
1692 return ret;
Manuel Pégourié-Gonnard85680c42019-05-03 09:16:16 +02001693 }
1694
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001695 MBEDTLS_SSL_DEBUG_BUF(3, "premaster secret",
1696 handshake->premaster,
1697 handshake->pmslen);
Manuel Pégourié-Gonnard85680c42019-05-03 09:16:16 +02001698
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001699 mbedtls_platform_zeroize(handshake->premaster,
1700 sizeof(handshake->premaster));
Manuel Pégourié-Gonnard9951b712019-04-30 12:08:59 +02001701 }
1702
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001703 return 0;
Manuel Pégourié-Gonnard9951b712019-04-30 12:08:59 +02001704}
Manuel Pégourié-Gonnard1b00c4f2019-04-30 11:54:22 +02001705
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001706int mbedtls_ssl_derive_keys(mbedtls_ssl_context *ssl)
Manuel Pégourié-Gonnarde59ae232019-04-30 11:41:40 +02001707{
Janos Follath865b3eb2019-12-16 11:46:15 +00001708 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnard8d2805c2019-04-30 12:08:59 +02001709 const mbedtls_ssl_ciphersuite_t * const ciphersuite_info =
1710 ssl->handshake->ciphersuite_info;
Manuel Pégourié-Gonnard1b00c4f2019-04-30 11:54:22 +02001711
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001712 MBEDTLS_SSL_DEBUG_MSG(2, ("=> derive keys"));
Manuel Pégourié-Gonnard040a9512019-05-06 12:05:58 +02001713
1714 /* Set PRF, calc_verify and calc_finished function pointers */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001715 ret = ssl_set_handshake_prfs(ssl->handshake,
1716 ssl->minor_ver,
1717 ciphersuite_info->mac);
1718 if (ret != 0) {
1719 MBEDTLS_SSL_DEBUG_RET(1, "ssl_set_handshake_prfs", ret);
1720 return ret;
Manuel Pégourié-Gonnard8d2805c2019-04-30 12:08:59 +02001721 }
Manuel Pégourié-Gonnard1b00c4f2019-04-30 11:54:22 +02001722
Manuel Pégourié-Gonnard040a9512019-05-06 12:05:58 +02001723 /* Compute master secret if needed */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001724 ret = ssl_compute_master(ssl->handshake,
1725 ssl->session_negotiate->master,
1726 ssl);
1727 if (ret != 0) {
1728 MBEDTLS_SSL_DEBUG_RET(1, "ssl_compute_master", ret);
1729 return ret;
Manuel Pégourié-Gonnard9951b712019-04-30 12:08:59 +02001730 }
1731
Manuel Pégourié-Gonnard040a9512019-05-06 12:05:58 +02001732 /* Swap the client and server random values:
1733 * - MS derivation wanted client+server (RFC 5246 8.1)
1734 * - key derivation wants server+client (RFC 5246 6.3) */
1735 {
1736 unsigned char tmp[64];
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001737 memcpy(tmp, ssl->handshake->randbytes, 64);
1738 memcpy(ssl->handshake->randbytes, tmp + 32, 32);
1739 memcpy(ssl->handshake->randbytes + 32, tmp, 32);
1740 mbedtls_platform_zeroize(tmp, sizeof(tmp));
Manuel Pégourié-Gonnard040a9512019-05-06 12:05:58 +02001741 }
1742
1743 /* Populate transform structure */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001744 ret = ssl_populate_transform(ssl->transform_negotiate,
1745 ssl->session_negotiate->ciphersuite,
1746 ssl->session_negotiate->master,
Jarno Lamsac84bd242019-08-16 12:06:56 +03001747#if defined(MBEDTLS_SSL_SOME_MODES_USE_MAC)
Manuel Pégourié-Gonnard6fa57bf2019-05-10 10:50:04 +02001748#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001749 ssl->session_negotiate->encrypt_then_mac,
Jarno Lamsac84bd242019-08-16 12:06:56 +03001750#endif /* MBEDTLS_SSL_ENCRYPT_THEN_MAC */
Manuel Pégourié-Gonnard6fa57bf2019-05-10 10:50:04 +02001751#if defined(MBEDTLS_SSL_TRUNCATED_HMAC)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001752 ssl->session_negotiate->trunc_hmac,
Jarno Lamsac84bd242019-08-16 12:06:56 +03001753#endif /* MBEDTLS_SSL_TRUNCATED_HMAC */
1754#endif /* MBEDTLS_SSL_SOME_MODES_USE_MAC */
Manuel Pégourié-Gonnard6fa57bf2019-05-10 10:50:04 +02001755#if defined(MBEDTLS_ZLIB_SUPPORT)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001756 ssl->session_negotiate->compression,
Manuel Pégourié-Gonnard6fa57bf2019-05-10 10:50:04 +02001757#endif
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001758 ssl->handshake->tls_prf,
1759 ssl->handshake->randbytes,
1760 ssl->minor_ver,
1761 ssl->conf->endpoint,
1762 ssl);
1763 if (ret != 0) {
1764 MBEDTLS_SSL_DEBUG_RET(1, "ssl_populate_transform", ret);
1765 return ret;
Manuel Pégourié-Gonnard040a9512019-05-06 12:05:58 +02001766 }
1767
1768 /* We no longer need Server/ClientHello.random values */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001769 mbedtls_platform_zeroize(ssl->handshake->randbytes,
1770 sizeof(ssl->handshake->randbytes));
Manuel Pégourié-Gonnard040a9512019-05-06 12:05:58 +02001771
Manuel Pégourié-Gonnardd73b47f2019-05-06 12:44:24 +02001772 /* Allocate compression buffer */
1773#if defined(MBEDTLS_ZLIB_SUPPORT)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001774 if (ssl->session_negotiate->compression == MBEDTLS_SSL_COMPRESS_DEFLATE &&
1775 ssl->compress_buf == NULL) {
1776 MBEDTLS_SSL_DEBUG_MSG(3, ("Allocating compression buffer"));
1777 ssl->compress_buf = mbedtls_calloc(1, MBEDTLS_SSL_COMPRESS_BUFFER_LEN);
1778 if (ssl->compress_buf == NULL) {
1779 MBEDTLS_SSL_DEBUG_MSG(1, ("alloc(%d bytes) failed",
1780 MBEDTLS_SSL_COMPRESS_BUFFER_LEN));
1781 return MBEDTLS_ERR_SSL_ALLOC_FAILED;
Manuel Pégourié-Gonnardd73b47f2019-05-06 12:44:24 +02001782 }
1783 }
1784#endif
1785
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001786 MBEDTLS_SSL_DEBUG_MSG(2, ("<= derive keys"));
Manuel Pégourié-Gonnard040a9512019-05-06 12:05:58 +02001787
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001788 return 0;
Manuel Pégourié-Gonnarde59ae232019-04-30 11:41:40 +02001789}
1790
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001791#if defined(MBEDTLS_SSL_PROTO_SSL3)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001792void ssl_calc_verify_ssl(const mbedtls_ssl_context *ssl,
1793 unsigned char *hash,
1794 size_t *hlen)
Paul Bakker5121ce52009-01-03 21:22:43 +00001795{
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02001796 mbedtls_md5_context md5;
1797 mbedtls_sha1_context sha1;
Paul Bakker5121ce52009-01-03 21:22:43 +00001798 unsigned char pad_1[48];
1799 unsigned char pad_2[48];
1800
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001801 MBEDTLS_SSL_DEBUG_MSG(2, ("=> calc verify ssl"));
Paul Bakker5121ce52009-01-03 21:22:43 +00001802
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001803 mbedtls_md5_init(&md5);
1804 mbedtls_sha1_init(&sha1);
Manuel Pégourié-Gonnard001f2b62015-07-06 16:21:13 +02001805
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001806 mbedtls_md5_clone(&md5, &ssl->handshake->fin_md5);
1807 mbedtls_sha1_clone(&sha1, &ssl->handshake->fin_sha1);
Paul Bakker5121ce52009-01-03 21:22:43 +00001808
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001809 memset(pad_1, 0x36, 48);
1810 memset(pad_2, 0x5C, 48);
Paul Bakker5121ce52009-01-03 21:22:43 +00001811
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001812 mbedtls_md5_update_ret(&md5, ssl->session_negotiate->master, 48);
1813 mbedtls_md5_update_ret(&md5, pad_1, 48);
1814 mbedtls_md5_finish_ret(&md5, hash);
Paul Bakker5121ce52009-01-03 21:22:43 +00001815
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001816 mbedtls_md5_starts_ret(&md5);
1817 mbedtls_md5_update_ret(&md5, ssl->session_negotiate->master, 48);
1818 mbedtls_md5_update_ret(&md5, pad_2, 48);
1819 mbedtls_md5_update_ret(&md5, hash, 16);
1820 mbedtls_md5_finish_ret(&md5, hash);
Paul Bakker5121ce52009-01-03 21:22:43 +00001821
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001822 mbedtls_sha1_update_ret(&sha1, ssl->session_negotiate->master, 48);
1823 mbedtls_sha1_update_ret(&sha1, pad_1, 40);
1824 mbedtls_sha1_finish_ret(&sha1, hash + 16);
Paul Bakker380da532012-04-18 16:10:25 +00001825
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001826 mbedtls_sha1_starts_ret(&sha1);
1827 mbedtls_sha1_update_ret(&sha1, ssl->session_negotiate->master, 48);
1828 mbedtls_sha1_update_ret(&sha1, pad_2, 40);
1829 mbedtls_sha1_update_ret(&sha1, hash + 16, 20);
1830 mbedtls_sha1_finish_ret(&sha1, hash + 16);
Paul Bakker380da532012-04-18 16:10:25 +00001831
Manuel Pégourié-Gonnardde718b92019-05-03 11:43:28 +02001832 *hlen = 36;
1833
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001834 MBEDTLS_SSL_DEBUG_BUF(3, "calculated verify result", hash, *hlen);
1835 MBEDTLS_SSL_DEBUG_MSG(2, ("<= calc verify"));
Paul Bakker380da532012-04-18 16:10:25 +00001836
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001837 mbedtls_md5_free(&md5);
1838 mbedtls_sha1_free(&sha1);
Paul Bakker5b4af392014-06-26 12:09:34 +02001839
Paul Bakker380da532012-04-18 16:10:25 +00001840 return;
1841}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001842#endif /* MBEDTLS_SSL_PROTO_SSL3 */
Paul Bakker380da532012-04-18 16:10:25 +00001843
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001844#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001845void ssl_calc_verify_tls(const mbedtls_ssl_context *ssl,
1846 unsigned char *hash,
1847 size_t *hlen)
Paul Bakker380da532012-04-18 16:10:25 +00001848{
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02001849 mbedtls_md5_context md5;
1850 mbedtls_sha1_context sha1;
Paul Bakker380da532012-04-18 16:10:25 +00001851
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001852 MBEDTLS_SSL_DEBUG_MSG(2, ("=> calc verify tls"));
Paul Bakker380da532012-04-18 16:10:25 +00001853
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001854 mbedtls_md5_init(&md5);
1855 mbedtls_sha1_init(&sha1);
Manuel Pégourié-Gonnard001f2b62015-07-06 16:21:13 +02001856
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001857 mbedtls_md5_clone(&md5, &ssl->handshake->fin_md5);
1858 mbedtls_sha1_clone(&sha1, &ssl->handshake->fin_sha1);
Paul Bakker380da532012-04-18 16:10:25 +00001859
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001860 mbedtls_md5_finish_ret(&md5, hash);
1861 mbedtls_sha1_finish_ret(&sha1, hash + 16);
Paul Bakker380da532012-04-18 16:10:25 +00001862
Manuel Pégourié-Gonnardde718b92019-05-03 11:43:28 +02001863 *hlen = 36;
1864
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001865 MBEDTLS_SSL_DEBUG_BUF(3, "calculated verify result", hash, *hlen);
1866 MBEDTLS_SSL_DEBUG_MSG(2, ("<= calc verify"));
Paul Bakker380da532012-04-18 16:10:25 +00001867
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001868 mbedtls_md5_free(&md5);
1869 mbedtls_sha1_free(&sha1);
Paul Bakker5b4af392014-06-26 12:09:34 +02001870
Paul Bakker380da532012-04-18 16:10:25 +00001871 return;
1872}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001873#endif /* MBEDTLS_SSL_PROTO_TLS1 || MBEDTLS_SSL_PROTO_TLS1_1 */
Paul Bakker380da532012-04-18 16:10:25 +00001874
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001875#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
1876#if defined(MBEDTLS_SHA256_C)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001877void ssl_calc_verify_tls_sha256(const mbedtls_ssl_context *ssl,
1878 unsigned char *hash,
1879 size_t *hlen)
Paul Bakker380da532012-04-18 16:10:25 +00001880{
Andrzej Kurekeb342242019-01-29 09:14:33 -05001881#if defined(MBEDTLS_USE_PSA_CRYPTO)
1882 size_t hash_size;
1883 psa_status_t status;
1884 psa_hash_operation_t sha256_psa = psa_hash_operation_init();
1885
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001886 MBEDTLS_SSL_DEBUG_MSG(2, ("=> PSA calc verify sha256"));
1887 status = psa_hash_clone(&ssl->handshake->fin_sha256_psa, &sha256_psa);
1888 if (status != PSA_SUCCESS) {
1889 MBEDTLS_SSL_DEBUG_MSG(2, ("PSA hash clone failed"));
Andrzej Kurekeb342242019-01-29 09:14:33 -05001890 return;
1891 }
1892
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001893 status = psa_hash_finish(&sha256_psa, hash, 32, &hash_size);
1894 if (status != PSA_SUCCESS) {
1895 MBEDTLS_SSL_DEBUG_MSG(2, ("PSA hash finish failed"));
Andrzej Kurekeb342242019-01-29 09:14:33 -05001896 return;
1897 }
Manuel Pégourié-Gonnardde718b92019-05-03 11:43:28 +02001898
1899 *hlen = 32;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001900 MBEDTLS_SSL_DEBUG_BUF(3, "PSA calculated verify result", hash, *hlen);
1901 MBEDTLS_SSL_DEBUG_MSG(2, ("<= PSA calc verify"));
Andrzej Kurekeb342242019-01-29 09:14:33 -05001902#else
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02001903 mbedtls_sha256_context sha256;
Paul Bakker380da532012-04-18 16:10:25 +00001904
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001905 mbedtls_sha256_init(&sha256);
Manuel Pégourié-Gonnard001f2b62015-07-06 16:21:13 +02001906
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001907 MBEDTLS_SSL_DEBUG_MSG(2, ("=> calc verify sha256"));
Paul Bakker380da532012-04-18 16:10:25 +00001908
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001909 mbedtls_sha256_clone(&sha256, &ssl->handshake->fin_sha256);
1910 mbedtls_sha256_finish_ret(&sha256, hash);
Paul Bakker380da532012-04-18 16:10:25 +00001911
Manuel Pégourié-Gonnardde718b92019-05-03 11:43:28 +02001912 *hlen = 32;
1913
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001914 MBEDTLS_SSL_DEBUG_BUF(3, "calculated verify result", hash, *hlen);
1915 MBEDTLS_SSL_DEBUG_MSG(2, ("<= calc verify"));
Paul Bakker380da532012-04-18 16:10:25 +00001916
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001917 mbedtls_sha256_free(&sha256);
Andrzej Kurekeb342242019-01-29 09:14:33 -05001918#endif /* MBEDTLS_USE_PSA_CRYPTO */
Paul Bakker380da532012-04-18 16:10:25 +00001919 return;
1920}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001921#endif /* MBEDTLS_SHA256_C */
Paul Bakker380da532012-04-18 16:10:25 +00001922
Gilles Peskined2d59372021-05-12 22:43:27 +02001923#if defined(MBEDTLS_SHA512_C) && !defined(MBEDTLS_SHA512_NO_SHA384)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001924void ssl_calc_verify_tls_sha384(const mbedtls_ssl_context *ssl,
1925 unsigned char *hash,
1926 size_t *hlen)
Paul Bakker380da532012-04-18 16:10:25 +00001927{
Andrzej Kurekeb342242019-01-29 09:14:33 -05001928#if defined(MBEDTLS_USE_PSA_CRYPTO)
1929 size_t hash_size;
1930 psa_status_t status;
Andrzej Kurek972fba52019-01-30 03:29:12 -05001931 psa_hash_operation_t sha384_psa = psa_hash_operation_init();
Andrzej Kurekeb342242019-01-29 09:14:33 -05001932
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001933 MBEDTLS_SSL_DEBUG_MSG(2, ("=> PSA calc verify sha384"));
1934 status = psa_hash_clone(&ssl->handshake->fin_sha384_psa, &sha384_psa);
1935 if (status != PSA_SUCCESS) {
1936 MBEDTLS_SSL_DEBUG_MSG(2, ("PSA hash clone failed"));
Andrzej Kurekeb342242019-01-29 09:14:33 -05001937 return;
1938 }
1939
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001940 status = psa_hash_finish(&sha384_psa, hash, 48, &hash_size);
1941 if (status != PSA_SUCCESS) {
1942 MBEDTLS_SSL_DEBUG_MSG(2, ("PSA hash finish failed"));
Andrzej Kurekeb342242019-01-29 09:14:33 -05001943 return;
1944 }
Manuel Pégourié-Gonnardde718b92019-05-03 11:43:28 +02001945
1946 *hlen = 48;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001947 MBEDTLS_SSL_DEBUG_BUF(3, "PSA calculated verify result", hash, *hlen);
1948 MBEDTLS_SSL_DEBUG_MSG(2, ("<= PSA calc verify"));
Andrzej Kurekeb342242019-01-29 09:14:33 -05001949#else
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02001950 mbedtls_sha512_context sha512;
Paul Bakker380da532012-04-18 16:10:25 +00001951
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001952 mbedtls_sha512_init(&sha512);
Manuel Pégourié-Gonnard001f2b62015-07-06 16:21:13 +02001953
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001954 MBEDTLS_SSL_DEBUG_MSG(2, ("=> calc verify sha384"));
Paul Bakker380da532012-04-18 16:10:25 +00001955
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001956 mbedtls_sha512_clone(&sha512, &ssl->handshake->fin_sha512);
1957 mbedtls_sha512_finish_ret(&sha512, hash);
Paul Bakker5121ce52009-01-03 21:22:43 +00001958
Manuel Pégourié-Gonnardde718b92019-05-03 11:43:28 +02001959 *hlen = 48;
1960
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001961 MBEDTLS_SSL_DEBUG_BUF(3, "calculated verify result", hash, *hlen);
1962 MBEDTLS_SSL_DEBUG_MSG(2, ("<= calc verify"));
Paul Bakker5121ce52009-01-03 21:22:43 +00001963
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001964 mbedtls_sha512_free(&sha512);
Andrzej Kurekeb342242019-01-29 09:14:33 -05001965#endif /* MBEDTLS_USE_PSA_CRYPTO */
Paul Bakker5121ce52009-01-03 21:22:43 +00001966 return;
1967}
Gilles Peskined2d59372021-05-12 22:43:27 +02001968#endif /* MBEDTLS_SHA512_C && !MBEDTLS_SHA512_NO_SHA384 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001969#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakker5121ce52009-01-03 21:22:43 +00001970
Gilles Peskineeccd8882020-03-10 12:19:08 +01001971#if defined(MBEDTLS_KEY_EXCHANGE_SOME_PSK_ENABLED)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001972int 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 +02001973{
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001974 unsigned char *p = ssl->handshake->premaster;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001975 unsigned char *end = p + sizeof(ssl->handshake->premaster);
Guilhem Bryantb5f04e42020-04-01 11:23:58 +01001976 const unsigned char *psk = NULL;
Guilhem Bryant61b0fe62020-03-27 11:12:12 +00001977 size_t psk_len = 0;
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01001978
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001979 if (mbedtls_ssl_get_psk(ssl, &psk, &psk_len)
1980 == MBEDTLS_ERR_SSL_PRIVATE_KEY_REQUIRED) {
Guilhem Bryantc5285d82020-03-25 17:08:15 +00001981 /*
1982 * This should never happen because the existence of a PSK is always
1983 * checked before calling this function
1984 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001985 MBEDTLS_SSL_DEBUG_MSG(1, ("should never happen"));
1986 return MBEDTLS_ERR_SSL_INTERNAL_ERROR;
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01001987 }
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001988
1989 /*
1990 * PMS = struct {
1991 * opaque other_secret<0..2^16-1>;
1992 * opaque psk<0..2^16-1>;
1993 * };
1994 * with "other_secret" depending on the particular key exchange
1995 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001996#if defined(MBEDTLS_KEY_EXCHANGE_PSK_ENABLED)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001997 if (key_ex == MBEDTLS_KEY_EXCHANGE_PSK) {
1998 if (end - p < 2) {
1999 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
2000 }
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02002001
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002002 MBEDTLS_PUT_UINT16_BE(psk_len, p, 0);
Joe Subbiania651e6f2021-08-23 11:35:25 +01002003 p += 2;
Manuel Pégourié-Gonnardbc5e5082015-10-21 12:35:29 +02002004
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002005 if (end < p || (size_t) (end - p) < psk_len) {
2006 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
2007 }
Manuel Pégourié-Gonnardbc5e5082015-10-21 12:35:29 +02002008
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002009 memset(p, 0, psk_len);
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01002010 p += psk_len;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002011 } else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002012#endif /* MBEDTLS_KEY_EXCHANGE_PSK_ENABLED */
2013#if defined(MBEDTLS_KEY_EXCHANGE_RSA_PSK_ENABLED)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002014 if (key_ex == MBEDTLS_KEY_EXCHANGE_RSA_PSK) {
Manuel Pégourié-Gonnard0fae60b2013-10-14 17:39:48 +02002015 /*
2016 * other_secret already set by the ClientKeyExchange message,
2017 * and is 48 bytes long
2018 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002019 if (end - p < 2) {
2020 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
2021 }
Philippe Antoine747fd532018-05-30 09:13:21 +02002022
Manuel Pégourié-Gonnard0fae60b2013-10-14 17:39:48 +02002023 *p++ = 0;
2024 *p++ = 48;
2025 p += 48;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002026 } else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002027#endif /* MBEDTLS_KEY_EXCHANGE_RSA_PSK_ENABLED */
2028#if defined(MBEDTLS_KEY_EXCHANGE_DHE_PSK_ENABLED)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002029 if (key_ex == MBEDTLS_KEY_EXCHANGE_DHE_PSK) {
Janos Follath865b3eb2019-12-16 11:46:15 +00002030 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnard33352052015-06-02 16:17:08 +01002031 size_t len;
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02002032
Manuel Pégourié-Gonnard8df68632014-06-23 17:56:08 +02002033 /* Write length only when we know the actual value */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002034 if ((ret = mbedtls_dhm_calc_secret(&ssl->handshake->dhm_ctx,
2035 p + 2, end - (p + 2), &len,
2036 ssl->conf->f_rng, ssl->conf->p_rng)) != 0) {
2037 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_dhm_calc_secret", ret);
2038 return ret;
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02002039 }
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002040 MBEDTLS_PUT_UINT16_BE(len, p, 0);
Joe Subbiania651e6f2021-08-23 11:35:25 +01002041 p += 2 + len;
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02002042
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002043 MBEDTLS_SSL_DEBUG_MPI(3, "DHM: K ", &ssl->handshake->dhm_ctx.K);
2044 } else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002045#endif /* MBEDTLS_KEY_EXCHANGE_DHE_PSK_ENABLED */
2046#if defined(MBEDTLS_KEY_EXCHANGE_ECDHE_PSK_ENABLED)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002047 if (key_ex == MBEDTLS_KEY_EXCHANGE_ECDHE_PSK) {
Janos Follath865b3eb2019-12-16 11:46:15 +00002048 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02002049 size_t zlen;
2050
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002051 if ((ret = mbedtls_ecdh_calc_secret(&ssl->handshake->ecdh_ctx, &zlen,
2052 p + 2, end - (p + 2),
2053 ssl->conf->f_rng, ssl->conf->p_rng)) != 0) {
2054 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ecdh_calc_secret", ret);
2055 return ret;
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02002056 }
2057
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002058 MBEDTLS_PUT_UINT16_BE(zlen, p, 0);
Joe Subbiania651e6f2021-08-23 11:35:25 +01002059 p += 2 + zlen;
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02002060
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002061 MBEDTLS_SSL_DEBUG_ECDH(3, &ssl->handshake->ecdh_ctx,
2062 MBEDTLS_DEBUG_ECDH_Z);
2063 } else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002064#endif /* MBEDTLS_KEY_EXCHANGE_ECDHE_PSK_ENABLED */
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02002065 {
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002066 MBEDTLS_SSL_DEBUG_MSG(1, ("should never happen"));
2067 return MBEDTLS_ERR_SSL_INTERNAL_ERROR;
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02002068 }
2069
2070 /* opaque psk<0..2^16-1>; */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002071 if (end - p < 2) {
2072 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
2073 }
Manuel Pégourié-Gonnardb2bf5a12014-03-25 16:28:12 +01002074
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002075 MBEDTLS_PUT_UINT16_BE(psk_len, p, 0);
Joe Subbiania651e6f2021-08-23 11:35:25 +01002076 p += 2;
Manuel Pégourié-Gonnardbc5e5082015-10-21 12:35:29 +02002077
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002078 if (end < p || (size_t) (end - p) < psk_len) {
2079 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
2080 }
Manuel Pégourié-Gonnardbc5e5082015-10-21 12:35:29 +02002081
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002082 memcpy(p, psk, psk_len);
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01002083 p += psk_len;
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02002084
2085 ssl->handshake->pmslen = p - ssl->handshake->premaster;
2086
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002087 return 0;
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02002088}
Gilles Peskineeccd8882020-03-10 12:19:08 +01002089#endif /* MBEDTLS_KEY_EXCHANGE_SOME_PSK_ENABLED */
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02002090
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002091#if defined(MBEDTLS_SSL_SRV_C) && defined(MBEDTLS_SSL_RENEGOTIATION)
Manuel Pégourié-Gonnardd904d662022-06-17 10:24:00 +02002092MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002093static int ssl_write_hello_request(mbedtls_ssl_context *ssl);
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +02002094
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002095#if defined(MBEDTLS_SSL_PROTO_DTLS)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002096int mbedtls_ssl_resend_hello_request(mbedtls_ssl_context *ssl)
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +02002097{
2098 /* If renegotiation is not enforced, retransmit until we would reach max
2099 * timeout if we were using the usual handshake doubling scheme */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002100 if (ssl->conf->renego_max_records < 0) {
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02002101 uint32_t ratio = ssl->conf->hs_timeout_max / ssl->conf->hs_timeout_min + 1;
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +02002102 unsigned char doublings = 1;
2103
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002104 while (ratio != 0) {
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +02002105 ++doublings;
2106 ratio >>= 1;
2107 }
2108
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002109 if (++ssl->renego_records_seen > doublings) {
2110 MBEDTLS_SSL_DEBUG_MSG(2, ("no longer retransmitting hello request"));
2111 return 0;
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +02002112 }
2113 }
2114
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002115 return ssl_write_hello_request(ssl);
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +02002116}
2117#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002118#endif /* MBEDTLS_SSL_SRV_C && MBEDTLS_SSL_RENEGOTIATION */
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02002119
Hanno Beckerb9d44792019-02-08 07:19:04 +00002120#if defined(MBEDTLS_X509_CRT_PARSE_C)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002121static void ssl_clear_peer_cert(mbedtls_ssl_session *session)
Hanno Beckerb9d44792019-02-08 07:19:04 +00002122{
2123#if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002124 if (session->peer_cert != NULL) {
2125 mbedtls_x509_crt_free(session->peer_cert);
2126 mbedtls_free(session->peer_cert);
Hanno Beckerb9d44792019-02-08 07:19:04 +00002127 session->peer_cert = NULL;
2128 }
2129#else /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002130 if (session->peer_cert_digest != NULL) {
Hanno Beckerb9d44792019-02-08 07:19:04 +00002131 /* Zeroization is not necessary. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002132 mbedtls_free(session->peer_cert_digest);
Hanno Beckerb9d44792019-02-08 07:19:04 +00002133 session->peer_cert_digest = NULL;
2134 session->peer_cert_digest_type = MBEDTLS_MD_NONE;
2135 session->peer_cert_digest_len = 0;
2136 }
2137#endif /* !MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
2138}
2139#endif /* MBEDTLS_X509_CRT_PARSE_C */
2140
Paul Bakker5121ce52009-01-03 21:22:43 +00002141/*
2142 * Handshake functions
2143 */
Gilles Peskineeccd8882020-03-10 12:19:08 +01002144#if !defined(MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED)
Gilles Peskinef9828522017-05-03 12:28:43 +02002145/* No certificate support -> dummy functions */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002146int mbedtls_ssl_write_certificate(mbedtls_ssl_context *ssl)
Paul Bakker5121ce52009-01-03 21:22:43 +00002147{
Hanno Beckere694c3e2017-12-27 21:34:08 +00002148 const mbedtls_ssl_ciphersuite_t *ciphersuite_info =
2149 ssl->handshake->ciphersuite_info;
Paul Bakker5121ce52009-01-03 21:22:43 +00002150
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002151 MBEDTLS_SSL_DEBUG_MSG(2, ("=> write certificate"));
Paul Bakker5121ce52009-01-03 21:22:43 +00002152
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002153 if (!mbedtls_ssl_ciphersuite_uses_srv_cert(ciphersuite_info)) {
2154 MBEDTLS_SSL_DEBUG_MSG(2, ("<= skip write certificate"));
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02002155 ssl->state++;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002156 return 0;
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02002157 }
2158
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002159 MBEDTLS_SSL_DEBUG_MSG(1, ("should never happen"));
2160 return MBEDTLS_ERR_SSL_INTERNAL_ERROR;
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002161}
2162
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002163int mbedtls_ssl_parse_certificate(mbedtls_ssl_context *ssl)
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002164{
Hanno Beckere694c3e2017-12-27 21:34:08 +00002165 const mbedtls_ssl_ciphersuite_t *ciphersuite_info =
2166 ssl->handshake->ciphersuite_info;
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002167
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002168 MBEDTLS_SSL_DEBUG_MSG(2, ("=> parse certificate"));
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002169
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002170 if (!mbedtls_ssl_ciphersuite_uses_srv_cert(ciphersuite_info)) {
2171 MBEDTLS_SSL_DEBUG_MSG(2, ("<= skip parse certificate"));
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002172 ssl->state++;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002173 return 0;
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002174 }
2175
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002176 MBEDTLS_SSL_DEBUG_MSG(1, ("should never happen"));
2177 return MBEDTLS_ERR_SSL_INTERNAL_ERROR;
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002178}
Gilles Peskinef9828522017-05-03 12:28:43 +02002179
Gilles Peskineeccd8882020-03-10 12:19:08 +01002180#else /* MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED */
Gilles Peskinef9828522017-05-03 12:28:43 +02002181/* Some certificate support -> implement write and parse */
2182
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002183int mbedtls_ssl_write_certificate(mbedtls_ssl_context *ssl)
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002184{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002185 int ret = MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002186 size_t i, n;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002187 const mbedtls_x509_crt *crt;
Hanno Beckere694c3e2017-12-27 21:34:08 +00002188 const mbedtls_ssl_ciphersuite_t *ciphersuite_info =
2189 ssl->handshake->ciphersuite_info;
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002190
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002191 MBEDTLS_SSL_DEBUG_MSG(2, ("=> write certificate"));
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002192
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002193 if (!mbedtls_ssl_ciphersuite_uses_srv_cert(ciphersuite_info)) {
2194 MBEDTLS_SSL_DEBUG_MSG(2, ("<= skip write certificate"));
Johan Pascal4f099262020-09-22 10:59:26 +02002195 ssl->state++;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002196 return 0;
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002197 }
2198
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002199#if defined(MBEDTLS_SSL_CLI_C)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002200 if (ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT) {
2201 if (ssl->client_auth == 0) {
2202 MBEDTLS_SSL_DEBUG_MSG(2, ("<= skip write certificate"));
Paul Bakker5121ce52009-01-03 21:22:43 +00002203 ssl->state++;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002204 return 0;
Paul Bakker5121ce52009-01-03 21:22:43 +00002205 }
2206
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002207#if defined(MBEDTLS_SSL_PROTO_SSL3)
Paul Bakker5121ce52009-01-03 21:22:43 +00002208 /*
2209 * If using SSLv3 and got no cert, send an Alert message
2210 * (otherwise an empty Certificate message will be sent).
2211 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002212 if (mbedtls_ssl_own_cert(ssl) == NULL &&
2213 ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0) {
Paul Bakker5121ce52009-01-03 21:22:43 +00002214 ssl->out_msglen = 2;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002215 ssl->out_msgtype = MBEDTLS_SSL_MSG_ALERT;
2216 ssl->out_msg[0] = MBEDTLS_SSL_ALERT_LEVEL_WARNING;
2217 ssl->out_msg[1] = MBEDTLS_SSL_ALERT_MSG_NO_CERT;
Paul Bakker5121ce52009-01-03 21:22:43 +00002218
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002219 MBEDTLS_SSL_DEBUG_MSG(2, ("got no certificate to send"));
Paul Bakker5121ce52009-01-03 21:22:43 +00002220 goto write_msg;
2221 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002222#endif /* MBEDTLS_SSL_PROTO_SSL3 */
Paul Bakker5121ce52009-01-03 21:22:43 +00002223 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002224#endif /* MBEDTLS_SSL_CLI_C */
2225#if defined(MBEDTLS_SSL_SRV_C)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002226 if (ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER) {
2227 if (mbedtls_ssl_own_cert(ssl) == NULL) {
2228 MBEDTLS_SSL_DEBUG_MSG(1, ("got no certificate to send"));
2229 return MBEDTLS_ERR_SSL_CERTIFICATE_REQUIRED;
Paul Bakker5121ce52009-01-03 21:22:43 +00002230 }
2231 }
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01002232#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00002233
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002234 MBEDTLS_SSL_DEBUG_CRT(3, "own certificate", mbedtls_ssl_own_cert(ssl));
Paul Bakker5121ce52009-01-03 21:22:43 +00002235
2236 /*
2237 * 0 . 0 handshake type
2238 * 1 . 3 handshake length
2239 * 4 . 6 length of all certs
2240 * 7 . 9 length of cert. 1
2241 * 10 . n-1 peer certificate
2242 * n . n+2 length of cert. 2
2243 * n+3 . ... upper level cert, etc.
2244 */
2245 i = 7;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002246 crt = mbedtls_ssl_own_cert(ssl);
Paul Bakker5121ce52009-01-03 21:22:43 +00002247
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002248 while (crt != NULL) {
Paul Bakker5121ce52009-01-03 21:22:43 +00002249 n = crt->raw.len;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002250 if (n > MBEDTLS_SSL_OUT_CONTENT_LEN - 3 - i) {
2251 MBEDTLS_SSL_DEBUG_MSG(1, ("certificate too large, %" MBEDTLS_PRINTF_SIZET
2252 " > %" MBEDTLS_PRINTF_SIZET,
2253 i + 3 + n, (size_t) MBEDTLS_SSL_OUT_CONTENT_LEN));
2254 return MBEDTLS_ERR_SSL_CERTIFICATE_TOO_LARGE;
Paul Bakker5121ce52009-01-03 21:22:43 +00002255 }
2256
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002257 ssl->out_msg[i] = MBEDTLS_BYTE_2(n);
2258 ssl->out_msg[i + 1] = MBEDTLS_BYTE_1(n);
2259 ssl->out_msg[i + 2] = MBEDTLS_BYTE_0(n);
Paul Bakker5121ce52009-01-03 21:22:43 +00002260
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002261 i += 3; memcpy(ssl->out_msg + i, crt->raw.p, n);
Paul Bakker5121ce52009-01-03 21:22:43 +00002262 i += n; crt = crt->next;
2263 }
2264
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002265 ssl->out_msg[4] = MBEDTLS_BYTE_2(i - 7);
2266 ssl->out_msg[5] = MBEDTLS_BYTE_1(i - 7);
2267 ssl->out_msg[6] = MBEDTLS_BYTE_0(i - 7);
Paul Bakker5121ce52009-01-03 21:22:43 +00002268
2269 ssl->out_msglen = i;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002270 ssl->out_msgtype = MBEDTLS_SSL_MSG_HANDSHAKE;
2271 ssl->out_msg[0] = MBEDTLS_SSL_HS_CERTIFICATE;
Paul Bakker5121ce52009-01-03 21:22:43 +00002272
Manuel Pégourié-Gonnardcf141ca2015-05-20 10:35:51 +02002273#if defined(MBEDTLS_SSL_PROTO_SSL3) && defined(MBEDTLS_SSL_CLI_C)
Paul Bakker5121ce52009-01-03 21:22:43 +00002274write_msg:
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002275#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00002276
2277 ssl->state++;
2278
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002279 if ((ret = mbedtls_ssl_write_handshake_msg(ssl)) != 0) {
2280 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ssl_write_handshake_msg", ret);
2281 return ret;
Paul Bakker5121ce52009-01-03 21:22:43 +00002282 }
2283
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002284 MBEDTLS_SSL_DEBUG_MSG(2, ("<= write certificate"));
Paul Bakker5121ce52009-01-03 21:22:43 +00002285
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002286 return ret;
Paul Bakker5121ce52009-01-03 21:22:43 +00002287}
2288
Hanno Becker84879e32019-01-31 07:44:03 +00002289#if defined(MBEDTLS_SSL_RENEGOTIATION) && defined(MBEDTLS_SSL_CLI_C)
Hanno Becker177475a2019-02-05 17:02:46 +00002290
2291#if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
Manuel Pégourié-Gonnardd904d662022-06-17 10:24:00 +02002292MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002293static int ssl_check_peer_crt_unchanged(mbedtls_ssl_context *ssl,
2294 unsigned char *crt_buf,
2295 size_t crt_buf_len)
Hanno Beckerdef9bdc2019-01-30 14:46:46 +00002296{
2297 mbedtls_x509_crt const * const peer_crt = ssl->session->peer_cert;
2298
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002299 if (peer_crt == NULL) {
2300 return -1;
2301 }
Hanno Beckerdef9bdc2019-01-30 14:46:46 +00002302
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002303 if (peer_crt->raw.len != crt_buf_len) {
2304 return -1;
2305 }
Hanno Beckerdef9bdc2019-01-30 14:46:46 +00002306
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002307 return memcmp(peer_crt->raw.p, crt_buf, peer_crt->raw.len);
Hanno Beckerdef9bdc2019-01-30 14:46:46 +00002308}
Hanno Becker177475a2019-02-05 17:02:46 +00002309#else /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
Manuel Pégourié-Gonnardd904d662022-06-17 10:24:00 +02002310MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002311static int ssl_check_peer_crt_unchanged(mbedtls_ssl_context *ssl,
2312 unsigned char *crt_buf,
2313 size_t crt_buf_len)
Hanno Becker177475a2019-02-05 17:02:46 +00002314{
Janos Follath865b3eb2019-12-16 11:46:15 +00002315 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Hanno Becker177475a2019-02-05 17:02:46 +00002316 unsigned char const * const peer_cert_digest =
2317 ssl->session->peer_cert_digest;
2318 mbedtls_md_type_t const peer_cert_digest_type =
2319 ssl->session->peer_cert_digest_type;
2320 mbedtls_md_info_t const * const digest_info =
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002321 mbedtls_md_info_from_type(peer_cert_digest_type);
Hanno Becker177475a2019-02-05 17:02:46 +00002322 unsigned char tmp_digest[MBEDTLS_SSL_PEER_CERT_DIGEST_MAX_LEN];
2323 size_t digest_len;
2324
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002325 if (peer_cert_digest == NULL || digest_info == NULL) {
2326 return -1;
2327 }
Hanno Becker177475a2019-02-05 17:02:46 +00002328
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002329 digest_len = mbedtls_md_get_size(digest_info);
2330 if (digest_len > MBEDTLS_SSL_PEER_CERT_DIGEST_MAX_LEN) {
2331 return -1;
2332 }
Hanno Becker177475a2019-02-05 17:02:46 +00002333
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002334 ret = mbedtls_md(digest_info, crt_buf, crt_buf_len, tmp_digest);
2335 if (ret != 0) {
2336 return -1;
2337 }
Hanno Becker177475a2019-02-05 17:02:46 +00002338
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002339 return memcmp(tmp_digest, peer_cert_digest, digest_len);
Hanno Becker177475a2019-02-05 17:02:46 +00002340}
2341#endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
Hanno Becker84879e32019-01-31 07:44:03 +00002342#endif /* MBEDTLS_SSL_RENEGOTIATION && MBEDTLS_SSL_CLI_C */
Hanno Beckerdef9bdc2019-01-30 14:46:46 +00002343
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02002344/*
2345 * Once the certificate message is read, parse it into a cert chain and
2346 * perform basic checks, but leave actual verification to the caller
2347 */
Manuel Pégourié-Gonnardd904d662022-06-17 10:24:00 +02002348MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002349static int ssl_parse_certificate_chain(mbedtls_ssl_context *ssl,
2350 mbedtls_x509_crt *chain)
Paul Bakker5121ce52009-01-03 21:22:43 +00002351{
Janos Follath865b3eb2019-12-16 11:46:15 +00002352 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Hanno Beckerc7bd7802019-02-05 15:37:23 +00002353#if defined(MBEDTLS_SSL_RENEGOTIATION) && defined(MBEDTLS_SSL_CLI_C)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002354 int crt_cnt = 0;
Hanno Beckerc7bd7802019-02-05 15:37:23 +00002355#endif
Paul Bakker23986e52011-04-24 08:57:21 +00002356 size_t i, n;
Gilles Peskine064a85c2017-05-10 10:46:40 +02002357 uint8_t alert;
Paul Bakker5121ce52009-01-03 21:22:43 +00002358
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002359 if (ssl->in_msgtype != MBEDTLS_SSL_MSG_HANDSHAKE) {
2360 MBEDTLS_SSL_DEBUG_MSG(1, ("bad certificate message"));
2361 mbedtls_ssl_send_alert_message(ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
2362 MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE);
2363 return MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE;
Paul Bakker5121ce52009-01-03 21:22:43 +00002364 }
2365
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002366 if (ssl->in_msg[0] != MBEDTLS_SSL_HS_CERTIFICATE ||
2367 ssl->in_hslen < mbedtls_ssl_hs_hdr_len(ssl) + 3 + 3) {
2368 MBEDTLS_SSL_DEBUG_MSG(1, ("bad certificate message"));
2369 mbedtls_ssl_send_alert_message(ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
2370 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR);
2371 return MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE;
Paul Bakker5121ce52009-01-03 21:22:43 +00002372 }
2373
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002374 i = mbedtls_ssl_hs_hdr_len(ssl);
Manuel Pégourié-Gonnardf49a7da2014-09-10 13:30:43 +00002375
Paul Bakker5121ce52009-01-03 21:22:43 +00002376 /*
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002377 * Same message structure as in mbedtls_ssl_write_certificate()
Paul Bakker5121ce52009-01-03 21:22:43 +00002378 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002379 n = (ssl->in_msg[i+1] << 8) | ssl->in_msg[i+2];
Paul Bakker5121ce52009-01-03 21:22:43 +00002380
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002381 if (ssl->in_msg[i] != 0 ||
2382 ssl->in_hslen != n + 3 + mbedtls_ssl_hs_hdr_len(ssl)) {
2383 MBEDTLS_SSL_DEBUG_MSG(1, ("bad certificate message"));
2384 mbedtls_ssl_send_alert_message(ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
2385 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR);
2386 return MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE;
Paul Bakker5121ce52009-01-03 21:22:43 +00002387 }
2388
Hanno Beckerdef9bdc2019-01-30 14:46:46 +00002389 /* Make &ssl->in_msg[i] point to the beginning of the CRT chain. */
2390 i += 3;
2391
Hanno Beckerdef9bdc2019-01-30 14:46:46 +00002392 /* Iterate through and parse the CRTs in the provided chain. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002393 while (i < ssl->in_hslen) {
Hanno Beckerdef9bdc2019-01-30 14:46:46 +00002394 /* Check that there's room for the next CRT's length fields. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002395 if (i + 3 > ssl->in_hslen) {
2396 MBEDTLS_SSL_DEBUG_MSG(1, ("bad certificate message"));
2397 mbedtls_ssl_send_alert_message(ssl,
2398 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
2399 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR);
2400 return MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE;
Philippe Antoine747fd532018-05-30 09:13:21 +02002401 }
Hanno Beckerdef9bdc2019-01-30 14:46:46 +00002402 /* In theory, the CRT can be up to 2**24 Bytes, but we don't support
2403 * anything beyond 2**16 ~ 64K. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002404 if (ssl->in_msg[i] != 0) {
2405 MBEDTLS_SSL_DEBUG_MSG(1, ("bad certificate message"));
2406 mbedtls_ssl_send_alert_message(ssl,
2407 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
2408 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR);
2409 return MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE;
Paul Bakker5121ce52009-01-03 21:22:43 +00002410 }
2411
Hanno Beckerdef9bdc2019-01-30 14:46:46 +00002412 /* Read length of the next CRT in the chain. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002413 n = ((unsigned int) ssl->in_msg[i + 1] << 8)
Paul Bakker5121ce52009-01-03 21:22:43 +00002414 | (unsigned int) ssl->in_msg[i + 2];
2415 i += 3;
2416
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002417 if (n < 128 || i + n > ssl->in_hslen) {
2418 MBEDTLS_SSL_DEBUG_MSG(1, ("bad certificate message"));
2419 mbedtls_ssl_send_alert_message(ssl,
2420 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
2421 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR);
2422 return MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE;
Paul Bakker5121ce52009-01-03 21:22:43 +00002423 }
2424
Hanno Beckerdef9bdc2019-01-30 14:46:46 +00002425 /* Check if we're handling the first CRT in the chain. */
Hanno Beckerc7bd7802019-02-05 15:37:23 +00002426#if defined(MBEDTLS_SSL_RENEGOTIATION) && defined(MBEDTLS_SSL_CLI_C)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002427 if (crt_cnt++ == 0 &&
Hanno Beckerc7bd7802019-02-05 15:37:23 +00002428 ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT &&
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002429 ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS) {
Hanno Becker46f34d02019-02-08 14:00:04 +00002430 /* During client-side renegotiation, check that the server's
2431 * end-CRTs hasn't changed compared to the initial handshake,
2432 * mitigating the triple handshake attack. On success, reuse
2433 * the original end-CRT instead of parsing it again. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002434 MBEDTLS_SSL_DEBUG_MSG(3, ("Check that peer CRT hasn't changed during renegotiation"));
2435 if (ssl_check_peer_crt_unchanged(ssl,
2436 &ssl->in_msg[i],
2437 n) != 0) {
2438 MBEDTLS_SSL_DEBUG_MSG(1, ("new server cert during renegotiation"));
2439 mbedtls_ssl_send_alert_message(ssl,
2440 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
2441 MBEDTLS_SSL_ALERT_MSG_ACCESS_DENIED);
2442 return MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE;
Hanno Beckerdef9bdc2019-01-30 14:46:46 +00002443 }
Hanno Beckerc7bd7802019-02-05 15:37:23 +00002444
2445 /* Now we can safely free the original chain. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002446 ssl_clear_peer_cert(ssl->session);
Hanno Beckerc7bd7802019-02-05 15:37:23 +00002447 }
Hanno Beckerdef9bdc2019-01-30 14:46:46 +00002448#endif /* MBEDTLS_SSL_RENEGOTIATION && MBEDTLS_SSL_CLI_C */
2449
Hanno Beckerdef9bdc2019-01-30 14:46:46 +00002450 /* Parse the next certificate in the chain. */
Hanno Becker0056eab2019-02-08 14:39:16 +00002451#if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002452 ret = mbedtls_x509_crt_parse_der(chain, ssl->in_msg + i, n);
Hanno Becker0056eab2019-02-08 14:39:16 +00002453#else
Hanno Becker353a6f02019-02-26 11:51:34 +00002454 /* If we don't need to store the CRT chain permanently, parse
Hanno Becker0056eab2019-02-08 14:39:16 +00002455 * it in-place from the input buffer instead of making a copy. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002456 ret = mbedtls_x509_crt_parse_der_nocopy(chain, ssl->in_msg + i, n);
Hanno Becker0056eab2019-02-08 14:39:16 +00002457#endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002458 switch (ret) {
Hanno Beckerdef9bdc2019-01-30 14:46:46 +00002459 case 0: /*ok*/
2460 case MBEDTLS_ERR_X509_UNKNOWN_SIG_ALG + MBEDTLS_ERR_OID_NOT_FOUND:
2461 /* Ignore certificate with an unknown algorithm: maybe a
2462 prior certificate was already trusted. */
2463 break;
Gilles Peskine1cc8e342017-05-03 16:28:34 +02002464
Hanno Beckerdef9bdc2019-01-30 14:46:46 +00002465 case MBEDTLS_ERR_X509_ALLOC_FAILED:
2466 alert = MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR;
2467 goto crt_parse_der_failed;
Gilles Peskine1cc8e342017-05-03 16:28:34 +02002468
Hanno Beckerdef9bdc2019-01-30 14:46:46 +00002469 case MBEDTLS_ERR_X509_UNKNOWN_VERSION:
2470 alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT;
2471 goto crt_parse_der_failed;
Gilles Peskine1cc8e342017-05-03 16:28:34 +02002472
Hanno Beckerdef9bdc2019-01-30 14:46:46 +00002473 default:
2474 alert = MBEDTLS_SSL_ALERT_MSG_BAD_CERT;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002475crt_parse_der_failed:
2476 mbedtls_ssl_send_alert_message(ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL, alert);
2477 MBEDTLS_SSL_DEBUG_RET(1, " mbedtls_x509_crt_parse_der", ret);
2478 return ret;
Paul Bakker5121ce52009-01-03 21:22:43 +00002479 }
2480
2481 i += n;
2482 }
2483
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002484 MBEDTLS_SSL_DEBUG_CRT(3, "peer certificate", chain);
2485 return 0;
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02002486}
2487
Hanno Becker4a55f632019-02-05 12:49:06 +00002488#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnardd904d662022-06-17 10:24:00 +02002489MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002490static int ssl_srv_check_client_no_crt_notification(mbedtls_ssl_context *ssl)
Hanno Becker4a55f632019-02-05 12:49:06 +00002491{
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002492 if (ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT) {
2493 return -1;
2494 }
Hanno Becker4a55f632019-02-05 12:49:06 +00002495
2496#if defined(MBEDTLS_SSL_PROTO_SSL3)
2497 /*
2498 * Check if the client sent an empty certificate
2499 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002500 if (ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0) {
2501 if (ssl->in_msglen == 2 &&
Hanno Becker4a55f632019-02-05 12:49:06 +00002502 ssl->in_msgtype == MBEDTLS_SSL_MSG_ALERT &&
2503 ssl->in_msg[0] == MBEDTLS_SSL_ALERT_LEVEL_WARNING &&
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002504 ssl->in_msg[1] == MBEDTLS_SSL_ALERT_MSG_NO_CERT) {
2505 MBEDTLS_SSL_DEBUG_MSG(1, ("SSLv3 client has no certificate"));
2506 return 0;
Hanno Becker4a55f632019-02-05 12:49:06 +00002507 }
2508
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002509 return -1;
Hanno Becker4a55f632019-02-05 12:49:06 +00002510 }
2511#endif /* MBEDTLS_SSL_PROTO_SSL3 */
2512
2513#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1) || \
2514 defined(MBEDTLS_SSL_PROTO_TLS1_2)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002515 if (ssl->in_hslen == 3 + mbedtls_ssl_hs_hdr_len(ssl) &&
Hanno Becker4a55f632019-02-05 12:49:06 +00002516 ssl->in_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE &&
2517 ssl->in_msg[0] == MBEDTLS_SSL_HS_CERTIFICATE &&
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002518 memcmp(ssl->in_msg + mbedtls_ssl_hs_hdr_len(ssl), "\0\0\0", 3) == 0) {
2519 MBEDTLS_SSL_DEBUG_MSG(1, ("TLSv1 client has no certificate"));
2520 return 0;
Hanno Becker4a55f632019-02-05 12:49:06 +00002521 }
2522
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002523 return -1;
Hanno Becker4a55f632019-02-05 12:49:06 +00002524#endif /* MBEDTLS_SSL_PROTO_TLS1 || MBEDTLS_SSL_PROTO_TLS1_1 || \
2525 MBEDTLS_SSL_PROTO_TLS1_2 */
2526}
2527#endif /* MBEDTLS_SSL_SRV_C */
2528
Hanno Becker28f2fcd2019-02-07 10:11:07 +00002529/* Check if a certificate message is expected.
2530 * Return either
2531 * - SSL_CERTIFICATE_EXPECTED, or
2532 * - SSL_CERTIFICATE_SKIP
2533 * indicating whether a Certificate message is expected or not.
2534 */
2535#define SSL_CERTIFICATE_EXPECTED 0
2536#define SSL_CERTIFICATE_SKIP 1
Manuel Pégourié-Gonnardd904d662022-06-17 10:24:00 +02002537MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002538static int ssl_parse_certificate_coordinate(mbedtls_ssl_context *ssl,
2539 int authmode)
Hanno Becker28f2fcd2019-02-07 10:11:07 +00002540{
2541 const mbedtls_ssl_ciphersuite_t *ciphersuite_info =
Hanno Beckere694c3e2017-12-27 21:34:08 +00002542 ssl->handshake->ciphersuite_info;
Hanno Becker28f2fcd2019-02-07 10:11:07 +00002543
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002544 if (!mbedtls_ssl_ciphersuite_uses_srv_cert(ciphersuite_info)) {
2545 return SSL_CERTIFICATE_SKIP;
2546 }
Hanno Becker28f2fcd2019-02-07 10:11:07 +00002547
2548#if defined(MBEDTLS_SSL_SRV_C)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002549 if (ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER) {
2550 if (ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_RSA_PSK) {
2551 return SSL_CERTIFICATE_SKIP;
2552 }
Hanno Becker28f2fcd2019-02-07 10:11:07 +00002553
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002554 if (authmode == MBEDTLS_SSL_VERIFY_NONE) {
Hanno Becker28f2fcd2019-02-07 10:11:07 +00002555 ssl->session_negotiate->verify_result =
2556 MBEDTLS_X509_BADCERT_SKIP_VERIFY;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002557 return SSL_CERTIFICATE_SKIP;
Hanno Becker28f2fcd2019-02-07 10:11:07 +00002558 }
2559 }
Hanno Becker84d9d272019-03-01 08:10:46 +00002560#else
2561 ((void) authmode);
Hanno Becker28f2fcd2019-02-07 10:11:07 +00002562#endif /* MBEDTLS_SSL_SRV_C */
2563
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002564 return SSL_CERTIFICATE_EXPECTED;
Hanno Becker28f2fcd2019-02-07 10:11:07 +00002565}
2566
Manuel Pégourié-Gonnardd904d662022-06-17 10:24:00 +02002567MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002568static int ssl_parse_certificate_verify(mbedtls_ssl_context *ssl,
2569 int authmode,
2570 mbedtls_x509_crt *chain,
2571 void *rs_ctx)
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02002572{
Hanno Becker6bdfab22019-02-05 13:11:17 +00002573 int ret = 0;
Hanno Becker28f2fcd2019-02-07 10:11:07 +00002574 const mbedtls_ssl_ciphersuite_t *ciphersuite_info =
Hanno Beckere694c3e2017-12-27 21:34:08 +00002575 ssl->handshake->ciphersuite_info;
Hanno Beckerafd0b0a2019-03-27 16:55:01 +00002576 int have_ca_chain = 0;
Hanno Becker68636192019-02-05 14:36:34 +00002577
Hanno Becker8927c832019-04-03 12:52:50 +01002578 int (*f_vrfy)(void *, mbedtls_x509_crt *, int, uint32_t *);
2579 void *p_vrfy;
2580
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002581 if (authmode == MBEDTLS_SSL_VERIFY_NONE) {
2582 return 0;
2583 }
Hanno Becker68636192019-02-05 14:36:34 +00002584
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002585 if (ssl->f_vrfy != NULL) {
2586 MBEDTLS_SSL_DEBUG_MSG(3, ("Use context-specific verification callback"));
Hanno Becker8927c832019-04-03 12:52:50 +01002587 f_vrfy = ssl->f_vrfy;
2588 p_vrfy = ssl->p_vrfy;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002589 } else {
2590 MBEDTLS_SSL_DEBUG_MSG(3, ("Use configuration-specific verification callback"));
Hanno Becker8927c832019-04-03 12:52:50 +01002591 f_vrfy = ssl->conf->f_vrfy;
2592 p_vrfy = ssl->conf->p_vrfy;
2593 }
2594
Hanno Becker68636192019-02-05 14:36:34 +00002595 /*
2596 * Main check: verify certificate
2597 */
Hanno Beckerafd0b0a2019-03-27 16:55:01 +00002598#if defined(MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002599 if (ssl->conf->f_ca_cb != NULL) {
Hanno Beckerafd0b0a2019-03-27 16:55:01 +00002600 ((void) rs_ctx);
2601 have_ca_chain = 1;
2602
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002603 MBEDTLS_SSL_DEBUG_MSG(3, ("use CA callback for X.509 CRT verification"));
Jarno Lamsa9822c0d2019-04-01 16:59:48 +03002604 ret = mbedtls_x509_crt_verify_with_ca_cb(
Hanno Beckerafd0b0a2019-03-27 16:55:01 +00002605 chain,
2606 ssl->conf->f_ca_cb,
2607 ssl->conf->p_ca_cb,
2608 ssl->conf->cert_profile,
2609 ssl->hostname,
2610 &ssl->session_negotiate->verify_result,
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002611 f_vrfy, p_vrfy);
2612 } else
Hanno Beckerafd0b0a2019-03-27 16:55:01 +00002613#endif /* MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK */
2614 {
2615 mbedtls_x509_crt *ca_chain;
2616 mbedtls_x509_crl *ca_crl;
2617
2618#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002619 if (ssl->handshake->sni_ca_chain != NULL) {
Hanno Beckerafd0b0a2019-03-27 16:55:01 +00002620 ca_chain = ssl->handshake->sni_ca_chain;
2621 ca_crl = ssl->handshake->sni_ca_crl;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002622 } else
Hanno Beckerafd0b0a2019-03-27 16:55:01 +00002623#endif
2624 {
2625 ca_chain = ssl->conf->ca_chain;
2626 ca_crl = ssl->conf->ca_crl;
2627 }
2628
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002629 if (ca_chain != NULL) {
Hanno Beckerafd0b0a2019-03-27 16:55:01 +00002630 have_ca_chain = 1;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002631 }
Hanno Beckerafd0b0a2019-03-27 16:55:01 +00002632
2633 ret = mbedtls_x509_crt_verify_restartable(
2634 chain,
2635 ca_chain, ca_crl,
2636 ssl->conf->cert_profile,
2637 ssl->hostname,
2638 &ssl->session_negotiate->verify_result,
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002639 f_vrfy, p_vrfy, rs_ctx);
Hanno Beckerafd0b0a2019-03-27 16:55:01 +00002640 }
Hanno Becker68636192019-02-05 14:36:34 +00002641
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002642 if (ret != 0) {
2643 MBEDTLS_SSL_DEBUG_RET(1, "x509_verify_cert", ret);
Hanno Becker68636192019-02-05 14:36:34 +00002644 }
2645
Gilles Peskineeccd8882020-03-10 12:19:08 +01002646#if defined(MBEDTLS_SSL_ECP_RESTARTABLE_ENABLED)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002647 if (ret == MBEDTLS_ERR_ECP_IN_PROGRESS) {
2648 return MBEDTLS_ERR_SSL_CRYPTO_IN_PROGRESS;
2649 }
Hanno Becker68636192019-02-05 14:36:34 +00002650#endif
2651
2652 /*
2653 * Secondary checks: always done, but change 'ret' only if it was 0
2654 */
2655
2656#if defined(MBEDTLS_ECP_C)
2657 {
2658 const mbedtls_pk_context *pk = &chain->pk;
2659
Manuel Pégourié-Gonnard06e1fcd2022-06-16 10:48:06 +02002660 /* If certificate uses an EC key, make sure the curve is OK.
2661 * This is a public key, so it can't be opaque, so can_do() is a good
2662 * enough check to ensure pk_ec() is safe to use here. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002663 if (mbedtls_pk_can_do(pk, MBEDTLS_PK_ECKEY) &&
2664 mbedtls_ssl_check_curve(ssl, mbedtls_pk_ec(*pk)->grp.id) != 0) {
Hanno Becker68636192019-02-05 14:36:34 +00002665 ssl->session_negotiate->verify_result |= MBEDTLS_X509_BADCERT_BAD_KEY;
2666
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002667 MBEDTLS_SSL_DEBUG_MSG(1, ("bad certificate (EC key curve)"));
2668 if (ret == 0) {
Hanno Becker68636192019-02-05 14:36:34 +00002669 ret = MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002670 }
Hanno Becker68636192019-02-05 14:36:34 +00002671 }
2672 }
2673#endif /* MBEDTLS_ECP_C */
2674
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002675 if (mbedtls_ssl_check_cert_usage(chain,
2676 ciphersuite_info,
2677 !ssl->conf->endpoint,
2678 &ssl->session_negotiate->verify_result) != 0) {
2679 MBEDTLS_SSL_DEBUG_MSG(1, ("bad certificate (usage extensions)"));
2680 if (ret == 0) {
Hanno Becker68636192019-02-05 14:36:34 +00002681 ret = MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002682 }
Hanno Becker68636192019-02-05 14:36:34 +00002683 }
2684
2685 /* mbedtls_x509_crt_verify_with_profile is supposed to report a
2686 * verification failure through MBEDTLS_ERR_X509_CERT_VERIFY_FAILED,
2687 * with details encoded in the verification flags. All other kinds
2688 * of error codes, including those from the user provided f_vrfy
2689 * functions, are treated as fatal and lead to a failure of
2690 * ssl_parse_certificate even if verification was optional. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002691 if (authmode == MBEDTLS_SSL_VERIFY_OPTIONAL &&
2692 (ret == MBEDTLS_ERR_X509_CERT_VERIFY_FAILED ||
2693 ret == MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE)) {
Hanno Becker68636192019-02-05 14:36:34 +00002694 ret = 0;
2695 }
2696
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002697 if (have_ca_chain == 0 && authmode == MBEDTLS_SSL_VERIFY_REQUIRED) {
2698 MBEDTLS_SSL_DEBUG_MSG(1, ("got no CA chain"));
Hanno Becker68636192019-02-05 14:36:34 +00002699 ret = MBEDTLS_ERR_SSL_CA_CHAIN_REQUIRED;
2700 }
2701
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002702 if (ret != 0) {
Hanno Becker68636192019-02-05 14:36:34 +00002703 uint8_t alert;
2704
2705 /* The certificate may have been rejected for several reasons.
2706 Pick one and send the corresponding alert. Which alert to send
2707 may be a subject of debate in some cases. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002708 if (ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_OTHER) {
Hanno Becker68636192019-02-05 14:36:34 +00002709 alert = MBEDTLS_SSL_ALERT_MSG_ACCESS_DENIED;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002710 } else if (ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_CN_MISMATCH) {
Hanno Becker68636192019-02-05 14:36:34 +00002711 alert = MBEDTLS_SSL_ALERT_MSG_BAD_CERT;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002712 } else if (ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_KEY_USAGE) {
Hanno Becker68636192019-02-05 14:36:34 +00002713 alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002714 } else if (ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_EXT_KEY_USAGE) {
Hanno Becker68636192019-02-05 14:36:34 +00002715 alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002716 } else if (ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_NS_CERT_TYPE) {
Hanno Becker68636192019-02-05 14:36:34 +00002717 alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002718 } else if (ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_BAD_PK) {
Hanno Becker68636192019-02-05 14:36:34 +00002719 alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002720 } else if (ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_BAD_KEY) {
Hanno Becker68636192019-02-05 14:36:34 +00002721 alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002722 } else if (ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_EXPIRED) {
Hanno Becker68636192019-02-05 14:36:34 +00002723 alert = MBEDTLS_SSL_ALERT_MSG_CERT_EXPIRED;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002724 } else if (ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_REVOKED) {
Hanno Becker68636192019-02-05 14:36:34 +00002725 alert = MBEDTLS_SSL_ALERT_MSG_CERT_REVOKED;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002726 } else if (ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_NOT_TRUSTED) {
Hanno Becker68636192019-02-05 14:36:34 +00002727 alert = MBEDTLS_SSL_ALERT_MSG_UNKNOWN_CA;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002728 } else {
Hanno Becker68636192019-02-05 14:36:34 +00002729 alert = MBEDTLS_SSL_ALERT_MSG_CERT_UNKNOWN;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002730 }
2731 mbedtls_ssl_send_alert_message(ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
2732 alert);
Hanno Becker68636192019-02-05 14:36:34 +00002733 }
2734
2735#if defined(MBEDTLS_DEBUG_C)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002736 if (ssl->session_negotiate->verify_result != 0) {
2737 MBEDTLS_SSL_DEBUG_MSG(3, ("! Certificate verification flags %08x",
2738 (unsigned int) ssl->session_negotiate->verify_result));
2739 } else {
2740 MBEDTLS_SSL_DEBUG_MSG(3, ("Certificate verification flags clear"));
Hanno Becker68636192019-02-05 14:36:34 +00002741 }
2742#endif /* MBEDTLS_DEBUG_C */
2743
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002744 return ret;
Hanno Becker68636192019-02-05 14:36:34 +00002745}
2746
Hanno Becker6b8fbab2019-02-08 14:59:05 +00002747#if !defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
Manuel Pégourié-Gonnardd904d662022-06-17 10:24:00 +02002748MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002749static int ssl_remember_peer_crt_digest(mbedtls_ssl_context *ssl,
2750 unsigned char *start, size_t len)
Hanno Becker6b8fbab2019-02-08 14:59:05 +00002751{
Janos Follath865b3eb2019-12-16 11:46:15 +00002752 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Hanno Becker6b8fbab2019-02-08 14:59:05 +00002753 /* Remember digest of the peer's end-CRT. */
2754 ssl->session_negotiate->peer_cert_digest =
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002755 mbedtls_calloc(1, MBEDTLS_SSL_PEER_CERT_DIGEST_DFL_LEN);
2756 if (ssl->session_negotiate->peer_cert_digest == NULL) {
2757 MBEDTLS_SSL_DEBUG_MSG(1, ("alloc(%d bytes) failed",
2758 MBEDTLS_SSL_PEER_CERT_DIGEST_DFL_LEN));
2759 mbedtls_ssl_send_alert_message(ssl,
2760 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
2761 MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR);
Hanno Becker6b8fbab2019-02-08 14:59:05 +00002762
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002763 return MBEDTLS_ERR_SSL_ALLOC_FAILED;
Hanno Becker6b8fbab2019-02-08 14:59:05 +00002764 }
2765
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002766 ret = mbedtls_md(mbedtls_md_info_from_type(
2767 MBEDTLS_SSL_PEER_CERT_DIGEST_DFL_TYPE),
2768 start, len,
2769 ssl->session_negotiate->peer_cert_digest);
Hanno Becker6b8fbab2019-02-08 14:59:05 +00002770
2771 ssl->session_negotiate->peer_cert_digest_type =
2772 MBEDTLS_SSL_PEER_CERT_DIGEST_DFL_TYPE;
2773 ssl->session_negotiate->peer_cert_digest_len =
2774 MBEDTLS_SSL_PEER_CERT_DIGEST_DFL_LEN;
2775
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002776 return ret;
Hanno Becker6b8fbab2019-02-08 14:59:05 +00002777}
2778
Manuel Pégourié-Gonnardd904d662022-06-17 10:24:00 +02002779MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002780static int ssl_remember_peer_pubkey(mbedtls_ssl_context *ssl,
2781 unsigned char *start, size_t len)
Hanno Becker6b8fbab2019-02-08 14:59:05 +00002782{
2783 unsigned char *end = start + len;
Janos Follath865b3eb2019-12-16 11:46:15 +00002784 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Hanno Becker6b8fbab2019-02-08 14:59:05 +00002785
2786 /* Make a copy of the peer's raw public key. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002787 mbedtls_pk_init(&ssl->handshake->peer_pubkey);
2788 ret = mbedtls_pk_parse_subpubkey(&start, end,
2789 &ssl->handshake->peer_pubkey);
2790 if (ret != 0) {
Hanno Becker6b8fbab2019-02-08 14:59:05 +00002791 /* We should have parsed the public key before. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002792 return MBEDTLS_ERR_SSL_INTERNAL_ERROR;
Hanno Becker6b8fbab2019-02-08 14:59:05 +00002793 }
2794
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002795 return 0;
Hanno Becker6b8fbab2019-02-08 14:59:05 +00002796}
2797#endif /* !MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
2798
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002799int mbedtls_ssl_parse_certificate(mbedtls_ssl_context *ssl)
Hanno Becker68636192019-02-05 14:36:34 +00002800{
2801 int ret = 0;
Hanno Becker28f2fcd2019-02-07 10:11:07 +00002802 int crt_expected;
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02002803#if defined(MBEDTLS_SSL_SRV_C) && defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
2804 const int authmode = ssl->handshake->sni_authmode != MBEDTLS_SSL_VERIFY_UNSET
2805 ? ssl->handshake->sni_authmode
2806 : ssl->conf->authmode;
2807#else
Johan Pascal4f099262020-09-22 10:59:26 +02002808 const int authmode = ssl->conf->authmode;
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02002809#endif
Manuel Pégourié-Gonnard3bf49c42017-08-15 13:47:06 +02002810 void *rs_ctx = NULL;
Hanno Becker3dad3112019-02-05 17:19:52 +00002811 mbedtls_x509_crt *chain = NULL;
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02002812
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002813 MBEDTLS_SSL_DEBUG_MSG(2, ("=> parse certificate"));
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02002814
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002815 crt_expected = ssl_parse_certificate_coordinate(ssl, authmode);
2816 if (crt_expected == SSL_CERTIFICATE_SKIP) {
2817 MBEDTLS_SSL_DEBUG_MSG(2, ("<= skip parse certificate"));
Hanno Becker6bdfab22019-02-05 13:11:17 +00002818 goto exit;
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02002819 }
2820
Gilles Peskineeccd8882020-03-10 12:19:08 +01002821#if defined(MBEDTLS_SSL_ECP_RESTARTABLE_ENABLED)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002822 if (ssl->handshake->ecrs_enabled &&
2823 ssl->handshake->ecrs_state == ssl_ecrs_crt_verify) {
Hanno Becker3dad3112019-02-05 17:19:52 +00002824 chain = ssl->handshake->ecrs_peer_cert;
2825 ssl->handshake->ecrs_peer_cert = NULL;
Manuel Pégourié-Gonnard3bf49c42017-08-15 13:47:06 +02002826 goto crt_verify;
2827 }
2828#endif
2829
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002830 if ((ret = mbedtls_ssl_read_record(ssl, 1)) != 0) {
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02002831 /* mbedtls_ssl_read_record may have sent an alert already. We
2832 let it decide whether to alert. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002833 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ssl_read_record", ret);
Hanno Becker3dad3112019-02-05 17:19:52 +00002834 goto exit;
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02002835 }
2836
Hanno Becker4a55f632019-02-05 12:49:06 +00002837#if defined(MBEDTLS_SSL_SRV_C)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002838 if (ssl_srv_check_client_no_crt_notification(ssl) == 0) {
Hanno Becker4a55f632019-02-05 12:49:06 +00002839 ssl->session_negotiate->verify_result = MBEDTLS_X509_BADCERT_MISSING;
Hanno Becker4a55f632019-02-05 12:49:06 +00002840
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002841 if (authmode != MBEDTLS_SSL_VERIFY_OPTIONAL) {
Hanno Becker6bdfab22019-02-05 13:11:17 +00002842 ret = MBEDTLS_ERR_SSL_NO_CLIENT_CERTIFICATE;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002843 }
Hanno Becker4a55f632019-02-05 12:49:06 +00002844
Hanno Becker6bdfab22019-02-05 13:11:17 +00002845 goto exit;
Hanno Becker4a55f632019-02-05 12:49:06 +00002846 }
2847#endif /* MBEDTLS_SSL_SRV_C */
2848
Hanno Beckerc7bd7802019-02-05 15:37:23 +00002849 /* Clear existing peer CRT structure in case we tried to
2850 * reuse a session but it failed, and allocate a new one. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002851 ssl_clear_peer_cert(ssl->session_negotiate);
Hanno Becker3dad3112019-02-05 17:19:52 +00002852
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002853 chain = mbedtls_calloc(1, sizeof(mbedtls_x509_crt));
2854 if (chain == NULL) {
2855 MBEDTLS_SSL_DEBUG_MSG(1, ("alloc(%" MBEDTLS_PRINTF_SIZET " bytes) failed",
2856 sizeof(mbedtls_x509_crt)));
2857 mbedtls_ssl_send_alert_message(ssl,
2858 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
2859 MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR);
Hanno Becker7a955a02019-02-05 13:08:01 +00002860
Hanno Becker3dad3112019-02-05 17:19:52 +00002861 ret = MBEDTLS_ERR_SSL_ALLOC_FAILED;
2862 goto exit;
2863 }
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002864 mbedtls_x509_crt_init(chain);
Hanno Becker3dad3112019-02-05 17:19:52 +00002865
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002866 ret = ssl_parse_certificate_chain(ssl, chain);
2867 if (ret != 0) {
Hanno Becker3dad3112019-02-05 17:19:52 +00002868 goto exit;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002869 }
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02002870
Gilles Peskineeccd8882020-03-10 12:19:08 +01002871#if defined(MBEDTLS_SSL_ECP_RESTARTABLE_ENABLED)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002872 if (ssl->handshake->ecrs_enabled) {
Manuel Pégourié-Gonnard0b23f162017-08-24 12:08:33 +02002873 ssl->handshake->ecrs_state = ssl_ecrs_crt_verify;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002874 }
Manuel Pégourié-Gonnard3bf49c42017-08-15 13:47:06 +02002875
2876crt_verify:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002877 if (ssl->handshake->ecrs_enabled) {
Manuel Pégourié-Gonnard3bf49c42017-08-15 13:47:06 +02002878 rs_ctx = &ssl->handshake->ecrs_ctx;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002879 }
Manuel Pégourié-Gonnard3bf49c42017-08-15 13:47:06 +02002880#endif
2881
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002882 ret = ssl_parse_certificate_verify(ssl, authmode,
2883 chain, rs_ctx);
2884 if (ret != 0) {
Hanno Becker3dad3112019-02-05 17:19:52 +00002885 goto exit;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002886 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002887
Hanno Becker6bbd94c2019-02-05 17:02:28 +00002888#if !defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
Hanno Becker6bbd94c2019-02-05 17:02:28 +00002889 {
Hanno Becker6b8fbab2019-02-08 14:59:05 +00002890 unsigned char *crt_start, *pk_start;
2891 size_t crt_len, pk_len;
Hanno Becker3dad3112019-02-05 17:19:52 +00002892
Hanno Becker6b8fbab2019-02-08 14:59:05 +00002893 /* We parse the CRT chain without copying, so
2894 * these pointers point into the input buffer,
2895 * and are hence still valid after freeing the
2896 * CRT chain. */
Hanno Becker6bbd94c2019-02-05 17:02:28 +00002897
Hanno Becker6b8fbab2019-02-08 14:59:05 +00002898 crt_start = chain->raw.p;
2899 crt_len = chain->raw.len;
Hanno Becker6bbd94c2019-02-05 17:02:28 +00002900
Hanno Becker6b8fbab2019-02-08 14:59:05 +00002901 pk_start = chain->pk_raw.p;
2902 pk_len = chain->pk_raw.len;
2903
2904 /* Free the CRT structures before computing
2905 * digest and copying the peer's public key. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002906 mbedtls_x509_crt_free(chain);
2907 mbedtls_free(chain);
Hanno Becker6b8fbab2019-02-08 14:59:05 +00002908 chain = NULL;
2909
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002910 ret = ssl_remember_peer_crt_digest(ssl, crt_start, crt_len);
2911 if (ret != 0) {
Hanno Beckera2747532019-02-06 16:19:04 +00002912 goto exit;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002913 }
Hanno Becker6b8fbab2019-02-08 14:59:05 +00002914
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002915 ret = ssl_remember_peer_pubkey(ssl, pk_start, pk_len);
2916 if (ret != 0) {
Hanno Becker6b8fbab2019-02-08 14:59:05 +00002917 goto exit;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002918 }
Hanno Beckera2747532019-02-06 16:19:04 +00002919 }
Hanno Becker6b8fbab2019-02-08 14:59:05 +00002920#else /* !MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
2921 /* Pass ownership to session structure. */
Hanno Becker3dad3112019-02-05 17:19:52 +00002922 ssl->session_negotiate->peer_cert = chain;
2923 chain = NULL;
Hanno Becker6b8fbab2019-02-08 14:59:05 +00002924#endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
Hanno Becker3dad3112019-02-05 17:19:52 +00002925
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002926 MBEDTLS_SSL_DEBUG_MSG(2, ("<= parse certificate"));
Paul Bakker5121ce52009-01-03 21:22:43 +00002927
Hanno Becker6bdfab22019-02-05 13:11:17 +00002928exit:
2929
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002930 if (ret == 0) {
Hanno Becker3dad3112019-02-05 17:19:52 +00002931 ssl->state++;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002932 }
Hanno Becker3dad3112019-02-05 17:19:52 +00002933
Gilles Peskineeccd8882020-03-10 12:19:08 +01002934#if defined(MBEDTLS_SSL_ECP_RESTARTABLE_ENABLED)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002935 if (ret == MBEDTLS_ERR_SSL_CRYPTO_IN_PROGRESS) {
Hanno Becker3dad3112019-02-05 17:19:52 +00002936 ssl->handshake->ecrs_peer_cert = chain;
2937 chain = NULL;
2938 }
2939#endif
2940
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002941 if (chain != NULL) {
2942 mbedtls_x509_crt_free(chain);
2943 mbedtls_free(chain);
Hanno Becker3dad3112019-02-05 17:19:52 +00002944 }
2945
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002946 return ret;
Paul Bakker5121ce52009-01-03 21:22:43 +00002947}
Gilles Peskineeccd8882020-03-10 12:19:08 +01002948#endif /* MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED */
Paul Bakker5121ce52009-01-03 21:22:43 +00002949
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002950void mbedtls_ssl_optimize_checksum(mbedtls_ssl_context *ssl,
2951 const mbedtls_ssl_ciphersuite_t *ciphersuite_info)
Paul Bakker380da532012-04-18 16:10:25 +00002952{
Paul Bakkerfb08fd22013-08-27 15:06:26 +02002953 ((void) ciphersuite_info);
Paul Bakker769075d2012-11-24 11:26:46 +01002954
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002955#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1) || \
2956 defined(MBEDTLS_SSL_PROTO_TLS1_1)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002957 if (ssl->minor_ver < MBEDTLS_SSL_MINOR_VERSION_3) {
Paul Bakker48916f92012-09-16 19:57:18 +00002958 ssl->handshake->update_checksum = ssl_update_checksum_md5sha1;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002959 } else
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002960#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002961#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
Gilles Peskined2d59372021-05-12 22:43:27 +02002962#if defined(MBEDTLS_SHA512_C) && !defined(MBEDTLS_SHA512_NO_SHA384)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002963 if (ciphersuite_info->mac == MBEDTLS_MD_SHA384) {
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002964 ssl->handshake->update_checksum = ssl_update_checksum_sha384;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002965 } else
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002966#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002967#if defined(MBEDTLS_SHA256_C)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002968 if (ciphersuite_info->mac != MBEDTLS_MD_SHA384) {
Paul Bakker48916f92012-09-16 19:57:18 +00002969 ssl->handshake->update_checksum = ssl_update_checksum_sha256;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002970 } else
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002971#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002972#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Manuel Pégourié-Gonnard61edffe2014-04-11 17:07:31 +02002973 {
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002974 MBEDTLS_SSL_DEBUG_MSG(1, ("should never happen"));
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002975 return;
Manuel Pégourié-Gonnard61edffe2014-04-11 17:07:31 +02002976 }
Paul Bakker380da532012-04-18 16:10:25 +00002977}
Paul Bakkerf7abd422013-04-16 13:15:56 +02002978
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002979void mbedtls_ssl_reset_checksum(mbedtls_ssl_context *ssl)
Manuel Pégourié-Gonnard67427c02014-07-11 13:45:34 +02002980{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002981#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1) || \
2982 defined(MBEDTLS_SSL_PROTO_TLS1_1)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002983 mbedtls_md5_starts_ret(&ssl->handshake->fin_md5);
2984 mbedtls_sha1_starts_ret(&ssl->handshake->fin_sha1);
Manuel Pégourié-Gonnard67427c02014-07-11 13:45:34 +02002985#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002986#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
2987#if defined(MBEDTLS_SHA256_C)
Andrzej Kurekeb342242019-01-29 09:14:33 -05002988#if defined(MBEDTLS_USE_PSA_CRYPTO)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002989 psa_hash_abort(&ssl->handshake->fin_sha256_psa);
2990 psa_hash_setup(&ssl->handshake->fin_sha256_psa, PSA_ALG_SHA_256);
Andrzej Kurekeb342242019-01-29 09:14:33 -05002991#else
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002992 mbedtls_sha256_starts_ret(&ssl->handshake->fin_sha256, 0);
Manuel Pégourié-Gonnard67427c02014-07-11 13:45:34 +02002993#endif
Andrzej Kurekeb342242019-01-29 09:14:33 -05002994#endif
Gilles Peskined2d59372021-05-12 22:43:27 +02002995#if defined(MBEDTLS_SHA512_C) && !defined(MBEDTLS_SHA512_NO_SHA384)
Andrzej Kurekeb342242019-01-29 09:14:33 -05002996#if defined(MBEDTLS_USE_PSA_CRYPTO)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002997 psa_hash_abort(&ssl->handshake->fin_sha384_psa);
2998 psa_hash_setup(&ssl->handshake->fin_sha384_psa, PSA_ALG_SHA_384);
Andrzej Kurekeb342242019-01-29 09:14:33 -05002999#else
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003000 mbedtls_sha512_starts_ret(&ssl->handshake->fin_sha512, 1);
Manuel Pégourié-Gonnard67427c02014-07-11 13:45:34 +02003001#endif
Andrzej Kurekeb342242019-01-29 09:14:33 -05003002#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003003#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Manuel Pégourié-Gonnard67427c02014-07-11 13:45:34 +02003004}
3005
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003006static void ssl_update_checksum_start(mbedtls_ssl_context *ssl,
3007 const unsigned char *buf, size_t len)
Paul Bakker380da532012-04-18 16:10:25 +00003008{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003009#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1) || \
3010 defined(MBEDTLS_SSL_PROTO_TLS1_1)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003011 mbedtls_md5_update_ret(&ssl->handshake->fin_md5, buf, len);
3012 mbedtls_sha1_update_ret(&ssl->handshake->fin_sha1, buf, len);
Paul Bakkerd2f068e2013-08-27 21:19:20 +02003013#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003014#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
3015#if defined(MBEDTLS_SHA256_C)
Andrzej Kurekeb342242019-01-29 09:14:33 -05003016#if defined(MBEDTLS_USE_PSA_CRYPTO)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003017 psa_hash_update(&ssl->handshake->fin_sha256_psa, buf, len);
Andrzej Kurekeb342242019-01-29 09:14:33 -05003018#else
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003019 mbedtls_sha256_update_ret(&ssl->handshake->fin_sha256, buf, len);
Paul Bakkerd2f068e2013-08-27 21:19:20 +02003020#endif
Andrzej Kurekeb342242019-01-29 09:14:33 -05003021#endif
Gilles Peskined2d59372021-05-12 22:43:27 +02003022#if defined(MBEDTLS_SHA512_C) && !defined(MBEDTLS_SHA512_NO_SHA384)
Andrzej Kurekeb342242019-01-29 09:14:33 -05003023#if defined(MBEDTLS_USE_PSA_CRYPTO)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003024 psa_hash_update(&ssl->handshake->fin_sha384_psa, buf, len);
Andrzej Kurekeb342242019-01-29 09:14:33 -05003025#else
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003026 mbedtls_sha512_update_ret(&ssl->handshake->fin_sha512, buf, len);
Paul Bakker769075d2012-11-24 11:26:46 +01003027#endif
Andrzej Kurekeb342242019-01-29 09:14:33 -05003028#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003029#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakker380da532012-04-18 16:10:25 +00003030}
3031
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003032#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1) || \
3033 defined(MBEDTLS_SSL_PROTO_TLS1_1)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003034static void ssl_update_checksum_md5sha1(mbedtls_ssl_context *ssl,
3035 const unsigned char *buf, size_t len)
Paul Bakker380da532012-04-18 16:10:25 +00003036{
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003037 mbedtls_md5_update_ret(&ssl->handshake->fin_md5, buf, len);
3038 mbedtls_sha1_update_ret(&ssl->handshake->fin_sha1, buf, len);
Paul Bakker380da532012-04-18 16:10:25 +00003039}
Paul Bakkerd2f068e2013-08-27 21:19:20 +02003040#endif
Paul Bakker380da532012-04-18 16:10:25 +00003041
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003042#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
3043#if defined(MBEDTLS_SHA256_C)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003044static void ssl_update_checksum_sha256(mbedtls_ssl_context *ssl,
3045 const unsigned char *buf, size_t len)
Paul Bakker380da532012-04-18 16:10:25 +00003046{
Andrzej Kurekeb342242019-01-29 09:14:33 -05003047#if defined(MBEDTLS_USE_PSA_CRYPTO)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003048 psa_hash_update(&ssl->handshake->fin_sha256_psa, buf, len);
Andrzej Kurekeb342242019-01-29 09:14:33 -05003049#else
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003050 mbedtls_sha256_update_ret(&ssl->handshake->fin_sha256, buf, len);
Andrzej Kurekeb342242019-01-29 09:14:33 -05003051#endif
Paul Bakker380da532012-04-18 16:10:25 +00003052}
Paul Bakkerd2f068e2013-08-27 21:19:20 +02003053#endif
Paul Bakker380da532012-04-18 16:10:25 +00003054
Gilles Peskined2d59372021-05-12 22:43:27 +02003055#if defined(MBEDTLS_SHA512_C) && !defined(MBEDTLS_SHA512_NO_SHA384)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003056static void ssl_update_checksum_sha384(mbedtls_ssl_context *ssl,
3057 const unsigned char *buf, size_t len)
Paul Bakker380da532012-04-18 16:10:25 +00003058{
Andrzej Kurekeb342242019-01-29 09:14:33 -05003059#if defined(MBEDTLS_USE_PSA_CRYPTO)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003060 psa_hash_update(&ssl->handshake->fin_sha384_psa, buf, len);
Andrzej Kurekeb342242019-01-29 09:14:33 -05003061#else
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003062 mbedtls_sha512_update_ret(&ssl->handshake->fin_sha512, buf, len);
Andrzej Kurekeb342242019-01-29 09:14:33 -05003063#endif
Paul Bakker380da532012-04-18 16:10:25 +00003064}
Paul Bakker769075d2012-11-24 11:26:46 +01003065#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003066#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakker380da532012-04-18 16:10:25 +00003067
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003068#if defined(MBEDTLS_SSL_PROTO_SSL3)
Paul Bakker1ef83d62012-04-11 12:09:53 +00003069static void ssl_calc_finished_ssl(
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003070 mbedtls_ssl_context *ssl, unsigned char *buf, int from)
Paul Bakker5121ce52009-01-03 21:22:43 +00003071{
Paul Bakker3c2122f2013-06-24 19:03:14 +02003072 const char *sender;
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02003073 mbedtls_md5_context md5;
3074 mbedtls_sha1_context sha1;
Paul Bakker1ef83d62012-04-11 12:09:53 +00003075
Paul Bakker5121ce52009-01-03 21:22:43 +00003076 unsigned char padbuf[48];
3077 unsigned char md5sum[16];
3078 unsigned char sha1sum[20];
3079
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003080 mbedtls_ssl_session *session = ssl->session_negotiate;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003081 if (!session) {
Paul Bakker48916f92012-09-16 19:57:18 +00003082 session = ssl->session;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003083 }
Paul Bakker48916f92012-09-16 19:57:18 +00003084
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003085 MBEDTLS_SSL_DEBUG_MSG(2, ("=> calc finished ssl"));
Paul Bakker1ef83d62012-04-11 12:09:53 +00003086
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003087 mbedtls_md5_init(&md5);
3088 mbedtls_sha1_init(&sha1);
Manuel Pégourié-Gonnard001f2b62015-07-06 16:21:13 +02003089
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003090 mbedtls_md5_clone(&md5, &ssl->handshake->fin_md5);
3091 mbedtls_sha1_clone(&sha1, &ssl->handshake->fin_sha1);
Paul Bakker5121ce52009-01-03 21:22:43 +00003092
3093 /*
3094 * SSLv3:
3095 * hash =
3096 * MD5( master + pad2 +
3097 * MD5( handshake + sender + master + pad1 ) )
3098 * + SHA1( master + pad2 +
3099 * SHA1( handshake + sender + master + pad1 ) )
Paul Bakker5121ce52009-01-03 21:22:43 +00003100 */
3101
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003102#if !defined(MBEDTLS_MD5_ALT)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003103 MBEDTLS_SSL_DEBUG_BUF(4, "finished md5 state", (unsigned char *)
3104 md5.state, sizeof(md5.state));
Paul Bakker90995b52013-06-24 19:20:35 +02003105#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00003106
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003107#if !defined(MBEDTLS_SHA1_ALT)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003108 MBEDTLS_SSL_DEBUG_BUF(4, "finished sha1 state", (unsigned char *)
3109 sha1.state, sizeof(sha1.state));
Paul Bakker90995b52013-06-24 19:20:35 +02003110#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00003111
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003112 sender = (from == MBEDTLS_SSL_IS_CLIENT) ? "CLNT"
Paul Bakker3c2122f2013-06-24 19:03:14 +02003113 : "SRVR";
Paul Bakker5121ce52009-01-03 21:22:43 +00003114
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003115 memset(padbuf, 0x36, 48);
Paul Bakker5121ce52009-01-03 21:22:43 +00003116
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003117 mbedtls_md5_update_ret(&md5, (const unsigned char *) sender, 4);
3118 mbedtls_md5_update_ret(&md5, session->master, 48);
3119 mbedtls_md5_update_ret(&md5, padbuf, 48);
3120 mbedtls_md5_finish_ret(&md5, md5sum);
Paul Bakker5121ce52009-01-03 21:22:43 +00003121
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003122 mbedtls_sha1_update_ret(&sha1, (const unsigned char *) sender, 4);
3123 mbedtls_sha1_update_ret(&sha1, session->master, 48);
3124 mbedtls_sha1_update_ret(&sha1, padbuf, 40);
3125 mbedtls_sha1_finish_ret(&sha1, sha1sum);
Paul Bakker5121ce52009-01-03 21:22:43 +00003126
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003127 memset(padbuf, 0x5C, 48);
Paul Bakker5121ce52009-01-03 21:22:43 +00003128
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003129 mbedtls_md5_starts_ret(&md5);
3130 mbedtls_md5_update_ret(&md5, session->master, 48);
3131 mbedtls_md5_update_ret(&md5, padbuf, 48);
3132 mbedtls_md5_update_ret(&md5, md5sum, 16);
3133 mbedtls_md5_finish_ret(&md5, buf);
Paul Bakker5121ce52009-01-03 21:22:43 +00003134
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003135 mbedtls_sha1_starts_ret(&sha1);
3136 mbedtls_sha1_update_ret(&sha1, session->master, 48);
3137 mbedtls_sha1_update_ret(&sha1, padbuf, 40);
3138 mbedtls_sha1_update_ret(&sha1, sha1sum, 20);
3139 mbedtls_sha1_finish_ret(&sha1, buf + 16);
Paul Bakker5121ce52009-01-03 21:22:43 +00003140
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003141 MBEDTLS_SSL_DEBUG_BUF(3, "calc finished result", buf, 36);
Paul Bakker5121ce52009-01-03 21:22:43 +00003142
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003143 mbedtls_md5_free(&md5);
3144 mbedtls_sha1_free(&sha1);
Paul Bakker5121ce52009-01-03 21:22:43 +00003145
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003146 mbedtls_platform_zeroize(padbuf, sizeof(padbuf));
3147 mbedtls_platform_zeroize(md5sum, sizeof(md5sum));
3148 mbedtls_platform_zeroize(sha1sum, sizeof(sha1sum));
Paul Bakker5121ce52009-01-03 21:22:43 +00003149
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003150 MBEDTLS_SSL_DEBUG_MSG(2, ("<= calc finished"));
Paul Bakker5121ce52009-01-03 21:22:43 +00003151}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003152#endif /* MBEDTLS_SSL_PROTO_SSL3 */
Paul Bakker5121ce52009-01-03 21:22:43 +00003153
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003154#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1)
Paul Bakker1ef83d62012-04-11 12:09:53 +00003155static void ssl_calc_finished_tls(
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003156 mbedtls_ssl_context *ssl, unsigned char *buf, int from)
Paul Bakker5121ce52009-01-03 21:22:43 +00003157{
Paul Bakker1ef83d62012-04-11 12:09:53 +00003158 int len = 12;
Paul Bakker3c2122f2013-06-24 19:03:14 +02003159 const char *sender;
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02003160 mbedtls_md5_context md5;
3161 mbedtls_sha1_context sha1;
Paul Bakker1ef83d62012-04-11 12:09:53 +00003162 unsigned char padbuf[36];
Paul Bakker5121ce52009-01-03 21:22:43 +00003163
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003164 mbedtls_ssl_session *session = ssl->session_negotiate;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003165 if (!session) {
Paul Bakker48916f92012-09-16 19:57:18 +00003166 session = ssl->session;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003167 }
Paul Bakker48916f92012-09-16 19:57:18 +00003168
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003169 MBEDTLS_SSL_DEBUG_MSG(2, ("=> calc finished tls"));
Paul Bakker5121ce52009-01-03 21:22:43 +00003170
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003171 mbedtls_md5_init(&md5);
3172 mbedtls_sha1_init(&sha1);
Manuel Pégourié-Gonnard001f2b62015-07-06 16:21:13 +02003173
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003174 mbedtls_md5_clone(&md5, &ssl->handshake->fin_md5);
3175 mbedtls_sha1_clone(&sha1, &ssl->handshake->fin_sha1);
Paul Bakker5121ce52009-01-03 21:22:43 +00003176
Paul Bakker1ef83d62012-04-11 12:09:53 +00003177 /*
3178 * TLSv1:
3179 * hash = PRF( master, finished_label,
3180 * MD5( handshake ) + SHA1( handshake ) )[0..11]
3181 */
Paul Bakker5121ce52009-01-03 21:22:43 +00003182
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003183#if !defined(MBEDTLS_MD5_ALT)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003184 MBEDTLS_SSL_DEBUG_BUF(4, "finished md5 state", (unsigned char *)
3185 md5.state, sizeof(md5.state));
Paul Bakker90995b52013-06-24 19:20:35 +02003186#endif
Paul Bakker1ef83d62012-04-11 12:09:53 +00003187
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003188#if !defined(MBEDTLS_SHA1_ALT)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003189 MBEDTLS_SSL_DEBUG_BUF(4, "finished sha1 state", (unsigned char *)
3190 sha1.state, sizeof(sha1.state));
Paul Bakker90995b52013-06-24 19:20:35 +02003191#endif
Paul Bakker1ef83d62012-04-11 12:09:53 +00003192
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003193 sender = (from == MBEDTLS_SSL_IS_CLIENT)
Paul Bakker3c2122f2013-06-24 19:03:14 +02003194 ? "client finished"
3195 : "server finished";
Paul Bakker1ef83d62012-04-11 12:09:53 +00003196
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003197 mbedtls_md5_finish_ret(&md5, padbuf);
3198 mbedtls_sha1_finish_ret(&sha1, padbuf + 16);
Paul Bakker1ef83d62012-04-11 12:09:53 +00003199
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003200 ssl->handshake->tls_prf(session->master, 48, sender,
3201 padbuf, 36, buf, len);
Paul Bakker1ef83d62012-04-11 12:09:53 +00003202
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003203 MBEDTLS_SSL_DEBUG_BUF(3, "calc finished result", buf, len);
Paul Bakker1ef83d62012-04-11 12:09:53 +00003204
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003205 mbedtls_md5_free(&md5);
3206 mbedtls_sha1_free(&sha1);
Paul Bakker1ef83d62012-04-11 12:09:53 +00003207
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003208 mbedtls_platform_zeroize(padbuf, sizeof(padbuf));
Paul Bakker1ef83d62012-04-11 12:09:53 +00003209
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003210 MBEDTLS_SSL_DEBUG_MSG(2, ("<= calc finished"));
Paul Bakker1ef83d62012-04-11 12:09:53 +00003211}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003212#endif /* MBEDTLS_SSL_PROTO_TLS1 || MBEDTLS_SSL_PROTO_TLS1_1 */
Paul Bakker1ef83d62012-04-11 12:09:53 +00003213
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003214#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
3215#if defined(MBEDTLS_SHA256_C)
Paul Bakkerca4ab492012-04-18 14:23:57 +00003216static void ssl_calc_finished_tls_sha256(
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003217 mbedtls_ssl_context *ssl, unsigned char *buf, int from)
Paul Bakker1ef83d62012-04-11 12:09:53 +00003218{
3219 int len = 12;
Paul Bakker3c2122f2013-06-24 19:03:14 +02003220 const char *sender;
Paul Bakker1ef83d62012-04-11 12:09:53 +00003221 unsigned char padbuf[32];
Andrzej Kurekeb342242019-01-29 09:14:33 -05003222#if defined(MBEDTLS_USE_PSA_CRYPTO)
3223 size_t hash_size;
Jaeden Amero34973232019-02-20 10:32:28 +00003224 psa_hash_operation_t sha256_psa = PSA_HASH_OPERATION_INIT;
Andrzej Kurekeb342242019-01-29 09:14:33 -05003225 psa_status_t status;
3226#else
3227 mbedtls_sha256_context sha256;
3228#endif
Paul Bakker1ef83d62012-04-11 12:09:53 +00003229
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003230 mbedtls_ssl_session *session = ssl->session_negotiate;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003231 if (!session) {
Paul Bakker48916f92012-09-16 19:57:18 +00003232 session = ssl->session;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003233 }
Paul Bakker48916f92012-09-16 19:57:18 +00003234
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003235 sender = (from == MBEDTLS_SSL_IS_CLIENT)
Andrzej Kurekeb342242019-01-29 09:14:33 -05003236 ? "client finished"
3237 : "server finished";
3238
3239#if defined(MBEDTLS_USE_PSA_CRYPTO)
3240 sha256_psa = psa_hash_operation_init();
3241
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003242 MBEDTLS_SSL_DEBUG_MSG(2, ("=> calc PSA finished tls sha256"));
Andrzej Kurekeb342242019-01-29 09:14:33 -05003243
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003244 status = psa_hash_clone(&ssl->handshake->fin_sha256_psa, &sha256_psa);
3245 if (status != PSA_SUCCESS) {
3246 MBEDTLS_SSL_DEBUG_MSG(2, ("PSA hash clone failed"));
Andrzej Kurekeb342242019-01-29 09:14:33 -05003247 return;
3248 }
3249
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003250 status = psa_hash_finish(&sha256_psa, padbuf, sizeof(padbuf), &hash_size);
3251 if (status != PSA_SUCCESS) {
3252 MBEDTLS_SSL_DEBUG_MSG(2, ("PSA hash finish failed"));
Andrzej Kurekeb342242019-01-29 09:14:33 -05003253 return;
3254 }
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003255 MBEDTLS_SSL_DEBUG_BUF(3, "PSA calculated padbuf", padbuf, 32);
Andrzej Kurekeb342242019-01-29 09:14:33 -05003256#else
3257
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003258 mbedtls_sha256_init(&sha256);
Manuel Pégourié-Gonnard001f2b62015-07-06 16:21:13 +02003259
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003260 MBEDTLS_SSL_DEBUG_MSG(2, ("=> calc finished tls sha256"));
Paul Bakker1ef83d62012-04-11 12:09:53 +00003261
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003262 mbedtls_sha256_clone(&sha256, &ssl->handshake->fin_sha256);
Paul Bakker1ef83d62012-04-11 12:09:53 +00003263
3264 /*
3265 * TLSv1.2:
3266 * hash = PRF( master, finished_label,
3267 * Hash( handshake ) )[0.11]
3268 */
3269
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003270#if !defined(MBEDTLS_SHA256_ALT)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003271 MBEDTLS_SSL_DEBUG_BUF(4, "finished sha2 state", (unsigned char *)
3272 sha256.state, sizeof(sha256.state));
Paul Bakker90995b52013-06-24 19:20:35 +02003273#endif
Paul Bakker1ef83d62012-04-11 12:09:53 +00003274
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003275 mbedtls_sha256_finish_ret(&sha256, padbuf);
3276 mbedtls_sha256_free(&sha256);
Andrzej Kurekeb342242019-01-29 09:14:33 -05003277#endif /* MBEDTLS_USE_PSA_CRYPTO */
Paul Bakker1ef83d62012-04-11 12:09:53 +00003278
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003279 ssl->handshake->tls_prf(session->master, 48, sender,
3280 padbuf, 32, buf, len);
Paul Bakker1ef83d62012-04-11 12:09:53 +00003281
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003282 MBEDTLS_SSL_DEBUG_BUF(3, "calc finished result", buf, len);
Paul Bakker1ef83d62012-04-11 12:09:53 +00003283
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003284 mbedtls_platform_zeroize(padbuf, sizeof(padbuf));
Paul Bakker1ef83d62012-04-11 12:09:53 +00003285
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003286 MBEDTLS_SSL_DEBUG_MSG(2, ("<= calc finished"));
Paul Bakker1ef83d62012-04-11 12:09:53 +00003287}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003288#endif /* MBEDTLS_SHA256_C */
Paul Bakker1ef83d62012-04-11 12:09:53 +00003289
Gilles Peskined2d59372021-05-12 22:43:27 +02003290#if defined(MBEDTLS_SHA512_C) && !defined(MBEDTLS_SHA512_NO_SHA384)
Rodrigo Dias Corread596ca82020-11-25 00:42:28 -03003291
Paul Bakkerca4ab492012-04-18 14:23:57 +00003292static void ssl_calc_finished_tls_sha384(
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003293 mbedtls_ssl_context *ssl, unsigned char *buf, int from)
Paul Bakkerca4ab492012-04-18 14:23:57 +00003294{
3295 int len = 12;
Paul Bakker3c2122f2013-06-24 19:03:14 +02003296 const char *sender;
Rodrigo Dias Corread596ca82020-11-25 00:42:28 -03003297 unsigned char padbuf[48];
Andrzej Kurekeb342242019-01-29 09:14:33 -05003298#if defined(MBEDTLS_USE_PSA_CRYPTO)
3299 size_t hash_size;
Jaeden Amero34973232019-02-20 10:32:28 +00003300 psa_hash_operation_t sha384_psa = PSA_HASH_OPERATION_INIT;
Andrzej Kurekeb342242019-01-29 09:14:33 -05003301 psa_status_t status;
3302#else
3303 mbedtls_sha512_context sha512;
3304#endif
Paul Bakkerca4ab492012-04-18 14:23:57 +00003305
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003306 mbedtls_ssl_session *session = ssl->session_negotiate;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003307 if (!session) {
Paul Bakker48916f92012-09-16 19:57:18 +00003308 session = ssl->session;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003309 }
Paul Bakker48916f92012-09-16 19:57:18 +00003310
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003311 sender = (from == MBEDTLS_SSL_IS_CLIENT)
Andrzej Kurekeb342242019-01-29 09:14:33 -05003312 ? "client finished"
3313 : "server finished";
3314
3315#if defined(MBEDTLS_USE_PSA_CRYPTO)
Andrzej Kurek972fba52019-01-30 03:29:12 -05003316 sha384_psa = psa_hash_operation_init();
Andrzej Kurekeb342242019-01-29 09:14:33 -05003317
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003318 MBEDTLS_SSL_DEBUG_MSG(2, ("=> calc PSA finished tls sha384"));
Andrzej Kurekeb342242019-01-29 09:14:33 -05003319
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003320 status = psa_hash_clone(&ssl->handshake->fin_sha384_psa, &sha384_psa);
3321 if (status != PSA_SUCCESS) {
3322 MBEDTLS_SSL_DEBUG_MSG(2, ("PSA hash clone failed"));
Andrzej Kurekeb342242019-01-29 09:14:33 -05003323 return;
3324 }
3325
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003326 status = psa_hash_finish(&sha384_psa, padbuf, sizeof(padbuf), &hash_size);
3327 if (status != PSA_SUCCESS) {
3328 MBEDTLS_SSL_DEBUG_MSG(2, ("PSA hash finish failed"));
Andrzej Kurekeb342242019-01-29 09:14:33 -05003329 return;
3330 }
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003331 MBEDTLS_SSL_DEBUG_BUF(3, "PSA calculated padbuf", padbuf, 48);
Andrzej Kurekeb342242019-01-29 09:14:33 -05003332#else
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003333 mbedtls_sha512_init(&sha512);
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 sha384"));
Paul Bakkerca4ab492012-04-18 14:23:57 +00003336
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003337 mbedtls_sha512_clone(&sha512, &ssl->handshake->fin_sha512);
Paul Bakkerca4ab492012-04-18 14:23:57 +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_SHA512_ALT)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003346 MBEDTLS_SSL_DEBUG_BUF(4, "finished sha512 state", (unsigned char *)
3347 sha512.state, sizeof(sha512.state));
Paul Bakker90995b52013-06-24 19:20:35 +02003348#endif
Gilles Peskinebb66dac2021-05-13 00:00:45 +02003349 /* mbedtls_sha512_finish_ret's output parameter is declared as a
Tom Cosgrove49f99bc2022-12-04 16:44:21 +00003350 * 64-byte buffer, but since we're using SHA-384, we know that the
Gilles Peskinebb66dac2021-05-13 00:00:45 +02003351 * output fits in 48 bytes. This is correct C, but GCC 11.1 warns
3352 * about it.
Rodrigo Dias Corread596ca82020-11-25 00:42:28 -03003353 */
Gilles Peskinebb66dac2021-05-13 00:00:45 +02003354#if defined(__GNUC__) && __GNUC__ >= 11
3355#pragma GCC diagnostic push
3356#pragma GCC diagnostic ignored "-Wstringop-overflow"
3357#endif
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003358 mbedtls_sha512_finish_ret(&sha512, padbuf);
Gilles Peskinebb66dac2021-05-13 00:00:45 +02003359#if defined(__GNUC__) && __GNUC__ >= 11
3360#pragma GCC diagnostic pop
3361#endif
Paul Bakkerca4ab492012-04-18 14:23:57 +00003362
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003363 mbedtls_sha512_free(&sha512);
Andrzej Kurekeb342242019-01-29 09:14:33 -05003364#endif
Paul Bakkerca4ab492012-04-18 14:23:57 +00003365
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003366 ssl->handshake->tls_prf(session->master, 48, sender,
3367 padbuf, 48, buf, len);
Paul Bakkerca4ab492012-04-18 14:23:57 +00003368
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003369 MBEDTLS_SSL_DEBUG_BUF(3, "calc finished result", buf, len);
Paul Bakkerca4ab492012-04-18 14:23:57 +00003370
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003371 mbedtls_platform_zeroize(padbuf, sizeof(padbuf));
Paul Bakkerca4ab492012-04-18 14:23:57 +00003372
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003373 MBEDTLS_SSL_DEBUG_MSG(2, ("<= calc finished"));
Paul Bakkerca4ab492012-04-18 14:23:57 +00003374}
Gilles Peskined2d59372021-05-12 22:43:27 +02003375#endif /* MBEDTLS_SHA512_C && !MBEDTLS_SHA512_NO_SHA384 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003376#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakkerca4ab492012-04-18 14:23:57 +00003377
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003378void mbedtls_ssl_handshake_wrapup_free_hs_transform(mbedtls_ssl_context *ssl)
Paul Bakker48916f92012-09-16 19:57:18 +00003379{
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003380 MBEDTLS_SSL_DEBUG_MSG(3, ("=> handshake wrapup: final free"));
Paul Bakker48916f92012-09-16 19:57:18 +00003381
3382 /*
3383 * Free our handshake params
3384 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003385 mbedtls_ssl_handshake_free(ssl);
3386 mbedtls_free(ssl->handshake);
Paul Bakker48916f92012-09-16 19:57:18 +00003387 ssl->handshake = NULL;
3388
3389 /*
Shaun Case0e7791f2021-12-20 21:14:10 -08003390 * Free the previous transform and switch in the current one
Paul Bakker48916f92012-09-16 19:57:18 +00003391 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003392 if (ssl->transform) {
3393 mbedtls_ssl_transform_free(ssl->transform);
3394 mbedtls_free(ssl->transform);
Paul Bakker48916f92012-09-16 19:57:18 +00003395 }
3396 ssl->transform = ssl->transform_negotiate;
3397 ssl->transform_negotiate = NULL;
3398
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003399 MBEDTLS_SSL_DEBUG_MSG(3, ("<= handshake wrapup: final free"));
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02003400}
3401
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003402void mbedtls_ssl_handshake_wrapup(mbedtls_ssl_context *ssl)
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02003403{
3404 int resume = ssl->handshake->resume;
3405
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003406 MBEDTLS_SSL_DEBUG_MSG(3, ("=> handshake wrapup"));
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02003407
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003408#if defined(MBEDTLS_SSL_RENEGOTIATION)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003409 if (ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS) {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003410 ssl->renego_status = MBEDTLS_SSL_RENEGOTIATION_DONE;
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02003411 ssl->renego_records_seen = 0;
3412 }
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01003413#endif
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02003414
3415 /*
3416 * Free the previous session and switch in the current one
3417 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003418 if (ssl->session) {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003419#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
Manuel Pégourié-Gonnard1a034732014-11-04 17:36:18 +01003420 /* RFC 7366 3.1: keep the EtM state */
3421 ssl->session_negotiate->encrypt_then_mac =
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003422 ssl->session->encrypt_then_mac;
Manuel Pégourié-Gonnard1a034732014-11-04 17:36:18 +01003423#endif
3424
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003425 mbedtls_ssl_session_free(ssl->session);
3426 mbedtls_free(ssl->session);
Paul Bakker0a597072012-09-25 21:55:46 +00003427 }
3428 ssl->session = ssl->session_negotiate;
Paul Bakker48916f92012-09-16 19:57:18 +00003429 ssl->session_negotiate = NULL;
3430
Paul Bakker0a597072012-09-25 21:55:46 +00003431 /*
3432 * Add cache entry
3433 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003434 if (ssl->conf->f_set_cache != NULL &&
Manuel Pégourié-Gonnard12ad7982015-06-18 15:50:37 +02003435 ssl->session->id_len != 0 &&
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003436 resume == 0) {
3437 if (ssl->conf->f_set_cache(ssl->conf->p_cache, ssl->session) != 0) {
3438 MBEDTLS_SSL_DEBUG_MSG(1, ("cache did not store session"));
3439 }
Manuel Pégourié-Gonnardc086cce2013-08-02 14:13:02 +02003440 }
Paul Bakker0a597072012-09-25 21:55:46 +00003441
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003442#if defined(MBEDTLS_SSL_PROTO_DTLS)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003443 if (ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
3444 ssl->handshake->flight != NULL) {
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02003445 /* Cancel handshake timer */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003446 mbedtls_ssl_set_timer(ssl, 0);
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02003447
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02003448 /* Keep last flight around in case we need to resend it:
3449 * we need the handshake and transform structures for that */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003450 MBEDTLS_SSL_DEBUG_MSG(3, ("skip freeing handshake and transform"));
3451 } else
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02003452#endif
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003453 mbedtls_ssl_handshake_wrapup_free_hs_transform(ssl);
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02003454
Paul Bakker48916f92012-09-16 19:57:18 +00003455 ssl->state++;
3456
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003457 MBEDTLS_SSL_DEBUG_MSG(3, ("<= handshake wrapup"));
Paul Bakker48916f92012-09-16 19:57:18 +00003458}
3459
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003460int mbedtls_ssl_write_finished(mbedtls_ssl_context *ssl)
Paul Bakker1ef83d62012-04-11 12:09:53 +00003461{
Manuel Pégourié-Gonnard879a4f92014-07-11 22:31:12 +02003462 int ret, hash_len;
Paul Bakker1ef83d62012-04-11 12:09:53 +00003463
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003464 MBEDTLS_SSL_DEBUG_MSG(2, ("=> write finished"));
Paul Bakker1ef83d62012-04-11 12:09:53 +00003465
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003466 mbedtls_ssl_update_out_pointers(ssl, ssl->transform_negotiate);
Paul Bakker92be97b2013-01-02 17:30:03 +01003467
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003468 ssl->handshake->calc_finished(ssl, ssl->out_msg + 4, ssl->conf->endpoint);
Paul Bakker1ef83d62012-04-11 12:09:53 +00003469
Manuel Pégourié-Gonnard214a8482016-02-22 11:27:26 +01003470 /*
3471 * RFC 5246 7.4.9 (Page 63) says 12 is the default length and ciphersuites
3472 * may define some other value. Currently (early 2016), no defined
3473 * ciphersuite does this (and this is unlikely to change as activity has
3474 * moved to TLS 1.3 now) so we can keep the hardcoded 12 here.
3475 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003476 hash_len = (ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0) ? 36 : 12;
Paul Bakker5121ce52009-01-03 21:22:43 +00003477
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003478#if defined(MBEDTLS_SSL_RENEGOTIATION)
Paul Bakker48916f92012-09-16 19:57:18 +00003479 ssl->verify_data_len = hash_len;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003480 memcpy(ssl->own_verify_data, ssl->out_msg + 4, hash_len);
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01003481#endif
Paul Bakker48916f92012-09-16 19:57:18 +00003482
Paul Bakker5121ce52009-01-03 21:22:43 +00003483 ssl->out_msglen = 4 + hash_len;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003484 ssl->out_msgtype = MBEDTLS_SSL_MSG_HANDSHAKE;
3485 ssl->out_msg[0] = MBEDTLS_SSL_HS_FINISHED;
Paul Bakker5121ce52009-01-03 21:22:43 +00003486
3487 /*
3488 * In case of session resuming, invert the client and server
3489 * ChangeCipherSpec messages order.
3490 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003491 if (ssl->handshake->resume != 0) {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003492#if defined(MBEDTLS_SSL_CLI_C)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003493 if (ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT) {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003494 ssl->state = MBEDTLS_SSL_HANDSHAKE_WRAPUP;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003495 }
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01003496#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003497#if defined(MBEDTLS_SSL_SRV_C)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003498 if (ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER) {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003499 ssl->state = MBEDTLS_SSL_CLIENT_CHANGE_CIPHER_SPEC;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003500 }
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01003501#endif
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003502 } else {
Paul Bakker5121ce52009-01-03 21:22:43 +00003503 ssl->state++;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003504 }
Paul Bakker5121ce52009-01-03 21:22:43 +00003505
Paul Bakker48916f92012-09-16 19:57:18 +00003506 /*
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02003507 * Switch to our negotiated transform and session parameters for outbound
3508 * data.
Paul Bakker48916f92012-09-16 19:57:18 +00003509 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003510 MBEDTLS_SSL_DEBUG_MSG(3, ("switching to new transform spec for outbound data"));
Manuel Pégourié-Gonnard5afb1672014-02-16 18:33:22 +01003511
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003512#if defined(MBEDTLS_SSL_PROTO_DTLS)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003513 if (ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM) {
Manuel Pégourié-Gonnard879a4f92014-07-11 22:31:12 +02003514 unsigned char i;
3515
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003516 /* Remember current epoch settings for resending */
3517 ssl->handshake->alt_transform_out = ssl->transform_out;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003518 memcpy(ssl->handshake->alt_out_ctr, ssl->cur_out_ctr, 8);
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003519
Manuel Pégourié-Gonnard879a4f92014-07-11 22:31:12 +02003520 /* Set sequence_number to zero */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003521 memset(ssl->cur_out_ctr + 2, 0, 6);
Manuel Pégourié-Gonnard879a4f92014-07-11 22:31:12 +02003522
3523 /* Increment epoch */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003524 for (i = 2; i > 0; i--) {
3525 if (++ssl->cur_out_ctr[i - 1] != 0) {
Manuel Pégourié-Gonnard879a4f92014-07-11 22:31:12 +02003526 break;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003527 }
3528 }
Manuel Pégourié-Gonnard879a4f92014-07-11 22:31:12 +02003529
3530 /* The loop goes to its end iff the counter is wrapping */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003531 if (i == 0) {
3532 MBEDTLS_SSL_DEBUG_MSG(1, ("DTLS epoch would wrap"));
3533 return MBEDTLS_ERR_SSL_COUNTER_WRAPPING;
Manuel Pégourié-Gonnard879a4f92014-07-11 22:31:12 +02003534 }
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003535 } else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003536#endif /* MBEDTLS_SSL_PROTO_DTLS */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003537 memset(ssl->cur_out_ctr, 0, 8);
Paul Bakker5121ce52009-01-03 21:22:43 +00003538
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003539 ssl->transform_out = ssl->transform_negotiate;
3540 ssl->session_out = ssl->session_negotiate;
3541
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003542#if defined(MBEDTLS_SSL_HW_RECORD_ACCEL)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003543 if (mbedtls_ssl_hw_record_activate != NULL) {
3544 if ((ret = mbedtls_ssl_hw_record_activate(ssl, MBEDTLS_SSL_CHANNEL_OUTBOUND)) != 0) {
3545 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ssl_hw_record_activate", ret);
3546 return MBEDTLS_ERR_SSL_HW_ACCEL_FAILED;
Paul Bakker07eb38b2012-12-19 14:42:06 +01003547 }
3548 }
3549#endif
3550
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003551#if defined(MBEDTLS_SSL_PROTO_DTLS)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003552 if (ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM) {
3553 mbedtls_ssl_send_flight_completed(ssl);
3554 }
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02003555#endif
3556
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003557 if ((ret = mbedtls_ssl_write_handshake_msg(ssl)) != 0) {
3558 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ssl_write_handshake_msg", ret);
3559 return ret;
Paul Bakker5121ce52009-01-03 21:22:43 +00003560 }
3561
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02003562#if defined(MBEDTLS_SSL_PROTO_DTLS)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003563 if (ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
3564 (ret = mbedtls_ssl_flight_transmit(ssl)) != 0) {
3565 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ssl_flight_transmit", ret);
3566 return ret;
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02003567 }
3568#endif
3569
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003570 MBEDTLS_SSL_DEBUG_MSG(2, ("<= write finished"));
Paul Bakker5121ce52009-01-03 21:22:43 +00003571
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003572 return 0;
Paul Bakker5121ce52009-01-03 21:22:43 +00003573}
3574
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003575#if defined(MBEDTLS_SSL_PROTO_SSL3)
Manuel Pégourié-Gonnardca6440b2014-09-10 12:39:54 +00003576#define SSL_MAX_HASH_LEN 36
3577#else
3578#define SSL_MAX_HASH_LEN 12
3579#endif
3580
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003581int mbedtls_ssl_parse_finished(mbedtls_ssl_context *ssl)
Paul Bakker5121ce52009-01-03 21:22:43 +00003582{
Janos Follath865b3eb2019-12-16 11:46:15 +00003583 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnard879a4f92014-07-11 22:31:12 +02003584 unsigned int hash_len;
Manuel Pégourié-Gonnardca6440b2014-09-10 12:39:54 +00003585 unsigned char buf[SSL_MAX_HASH_LEN];
Paul Bakker5121ce52009-01-03 21:22:43 +00003586
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003587 MBEDTLS_SSL_DEBUG_MSG(2, ("=> parse finished"));
Paul Bakker5121ce52009-01-03 21:22:43 +00003588
Gilles Peskine622d8042021-12-13 14:38:40 +01003589 /* There is currently no ciphersuite using another length with TLS 1.2 */
3590#if defined(MBEDTLS_SSL_PROTO_SSL3)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003591 if (ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0) {
Gilles Peskine622d8042021-12-13 14:38:40 +01003592 hash_len = 36;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003593 } else
Gilles Peskine622d8042021-12-13 14:38:40 +01003594#endif
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003595 hash_len = 12;
Gilles Peskine622d8042021-12-13 14:38:40 +01003596
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003597 ssl->handshake->calc_finished(ssl, buf, ssl->conf->endpoint ^ 1);
Paul Bakker5121ce52009-01-03 21:22:43 +00003598
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003599 if ((ret = mbedtls_ssl_read_record(ssl, 1)) != 0) {
3600 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ssl_read_record", ret);
Gilles Peskinef91b2e52021-12-13 12:36:15 +01003601 goto exit;
Paul Bakker5121ce52009-01-03 21:22:43 +00003602 }
3603
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003604 if (ssl->in_msgtype != MBEDTLS_SSL_MSG_HANDSHAKE) {
3605 MBEDTLS_SSL_DEBUG_MSG(1, ("bad finished message"));
3606 mbedtls_ssl_send_alert_message(ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
3607 MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE);
Gilles Peskinef91b2e52021-12-13 12:36:15 +01003608 ret = MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE;
3609 goto exit;
Paul Bakker5121ce52009-01-03 21:22:43 +00003610 }
3611
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003612 if (ssl->in_msg[0] != MBEDTLS_SSL_HS_FINISHED ||
3613 ssl->in_hslen != mbedtls_ssl_hs_hdr_len(ssl) + hash_len) {
3614 MBEDTLS_SSL_DEBUG_MSG(1, ("bad finished message"));
3615 mbedtls_ssl_send_alert_message(ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
3616 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR);
Gilles Peskinef91b2e52021-12-13 12:36:15 +01003617 ret = MBEDTLS_ERR_SSL_BAD_HS_FINISHED;
3618 goto exit;
Paul Bakker5121ce52009-01-03 21:22:43 +00003619 }
3620
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003621 if (mbedtls_ct_memcmp(ssl->in_msg + mbedtls_ssl_hs_hdr_len(ssl),
3622 buf, hash_len) != 0) {
3623 MBEDTLS_SSL_DEBUG_MSG(1, ("bad finished message"));
3624 mbedtls_ssl_send_alert_message(ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
3625 MBEDTLS_SSL_ALERT_MSG_DECRYPT_ERROR);
Gilles Peskinef91b2e52021-12-13 12:36:15 +01003626 ret = MBEDTLS_ERR_SSL_BAD_HS_FINISHED;
3627 goto exit;
Paul Bakker5121ce52009-01-03 21:22:43 +00003628 }
3629
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003630#if defined(MBEDTLS_SSL_RENEGOTIATION)
Paul Bakker48916f92012-09-16 19:57:18 +00003631 ssl->verify_data_len = hash_len;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003632 memcpy(ssl->peer_verify_data, buf, hash_len);
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01003633#endif
Paul Bakker48916f92012-09-16 19:57:18 +00003634
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003635 if (ssl->handshake->resume != 0) {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003636#if defined(MBEDTLS_SSL_CLI_C)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003637 if (ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT) {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003638 ssl->state = MBEDTLS_SSL_CLIENT_CHANGE_CIPHER_SPEC;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003639 }
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01003640#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003641#if defined(MBEDTLS_SSL_SRV_C)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003642 if (ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER) {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003643 ssl->state = MBEDTLS_SSL_HANDSHAKE_WRAPUP;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003644 }
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01003645#endif
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003646 } else {
Paul Bakker5121ce52009-01-03 21:22:43 +00003647 ssl->state++;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003648 }
Paul Bakker5121ce52009-01-03 21:22:43 +00003649
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003650#if defined(MBEDTLS_SSL_PROTO_DTLS)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003651 if (ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM) {
3652 mbedtls_ssl_recv_flight_completed(ssl);
3653 }
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003654#endif
3655
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003656 MBEDTLS_SSL_DEBUG_MSG(2, ("<= parse finished"));
Paul Bakker5121ce52009-01-03 21:22:43 +00003657
Gilles Peskinef91b2e52021-12-13 12:36:15 +01003658exit:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003659 mbedtls_platform_zeroize(buf, hash_len);
3660 return ret;
Paul Bakker5121ce52009-01-03 21:22:43 +00003661}
3662
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003663static void ssl_handshake_params_init(mbedtls_ssl_handshake_params *handshake)
Paul Bakkeraccaffe2014-06-26 13:37:14 +02003664{
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003665 memset(handshake, 0, sizeof(mbedtls_ssl_handshake_params));
Paul Bakkeraccaffe2014-06-26 13:37:14 +02003666
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003667#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1) || \
3668 defined(MBEDTLS_SSL_PROTO_TLS1_1)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003669 mbedtls_md5_init(&handshake->fin_md5);
3670 mbedtls_sha1_init(&handshake->fin_sha1);
3671 mbedtls_md5_starts_ret(&handshake->fin_md5);
3672 mbedtls_sha1_starts_ret(&handshake->fin_sha1);
Paul Bakkeraccaffe2014-06-26 13:37:14 +02003673#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003674#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
3675#if defined(MBEDTLS_SHA256_C)
Andrzej Kurekeb342242019-01-29 09:14:33 -05003676#if defined(MBEDTLS_USE_PSA_CRYPTO)
3677 handshake->fin_sha256_psa = psa_hash_operation_init();
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003678 psa_hash_setup(&handshake->fin_sha256_psa, PSA_ALG_SHA_256);
Andrzej Kurekeb342242019-01-29 09:14:33 -05003679#else
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003680 mbedtls_sha256_init(&handshake->fin_sha256);
3681 mbedtls_sha256_starts_ret(&handshake->fin_sha256, 0);
Paul Bakkeraccaffe2014-06-26 13:37:14 +02003682#endif
Andrzej Kurekeb342242019-01-29 09:14:33 -05003683#endif
Gilles Peskined2d59372021-05-12 22:43:27 +02003684#if defined(MBEDTLS_SHA512_C) && !defined(MBEDTLS_SHA512_NO_SHA384)
Andrzej Kurekeb342242019-01-29 09:14:33 -05003685#if defined(MBEDTLS_USE_PSA_CRYPTO)
Andrzej Kurek972fba52019-01-30 03:29:12 -05003686 handshake->fin_sha384_psa = psa_hash_operation_init();
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003687 psa_hash_setup(&handshake->fin_sha384_psa, PSA_ALG_SHA_384);
Andrzej Kurekeb342242019-01-29 09:14:33 -05003688#else
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003689 mbedtls_sha512_init(&handshake->fin_sha512);
3690 mbedtls_sha512_starts_ret(&handshake->fin_sha512, 1);
Paul Bakkeraccaffe2014-06-26 13:37:14 +02003691#endif
Andrzej Kurekeb342242019-01-29 09:14:33 -05003692#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003693#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakkeraccaffe2014-06-26 13:37:14 +02003694
3695 handshake->update_checksum = ssl_update_checksum_start;
Hanno Becker7e5437a2017-04-28 17:15:26 +01003696
3697#if defined(MBEDTLS_SSL_PROTO_TLS1_2) && \
Gilles Peskineeccd8882020-03-10 12:19:08 +01003698 defined(MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003699 mbedtls_ssl_sig_hash_set_init(&handshake->hash_algs);
Hanno Becker7e5437a2017-04-28 17:15:26 +01003700#endif
Paul Bakkeraccaffe2014-06-26 13:37:14 +02003701
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003702#if defined(MBEDTLS_DHM_C)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003703 mbedtls_dhm_init(&handshake->dhm_ctx);
Paul Bakkeraccaffe2014-06-26 13:37:14 +02003704#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003705#if defined(MBEDTLS_ECDH_C)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003706 mbedtls_ecdh_init(&handshake->ecdh_ctx);
Paul Bakkeraccaffe2014-06-26 13:37:14 +02003707#endif
Manuel Pégourié-Gonnardeef142d2015-09-16 10:05:04 +02003708#if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003709 mbedtls_ecjpake_init(&handshake->ecjpake_ctx);
Manuel Pégourié-Gonnard77c06462015-09-17 13:59:49 +02003710#if defined(MBEDTLS_SSL_CLI_C)
3711 handshake->ecjpake_cache = NULL;
3712 handshake->ecjpake_cache_len = 0;
3713#endif
Manuel Pégourié-Gonnard76cfd3f2015-09-15 12:10:54 +02003714#endif
Manuel Pégourié-Gonnardcdc26ae2015-06-19 12:16:31 +02003715
Gilles Peskineeccd8882020-03-10 12:19:08 +01003716#if defined(MBEDTLS_SSL_ECP_RESTARTABLE_ENABLED)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003717 mbedtls_x509_crt_restart_init(&handshake->ecrs_ctx);
Manuel Pégourié-Gonnard862cde52017-05-17 11:56:15 +02003718#endif
3719
Manuel Pégourié-Gonnardcdc26ae2015-06-19 12:16:31 +02003720#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
3721 handshake->sni_authmode = MBEDTLS_SSL_VERIFY_UNSET;
3722#endif
Hanno Becker75173122019-02-06 16:18:31 +00003723
3724#if defined(MBEDTLS_X509_CRT_PARSE_C) && \
3725 !defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003726 mbedtls_pk_init(&handshake->peer_pubkey);
Hanno Becker75173122019-02-06 16:18:31 +00003727#endif
Paul Bakkeraccaffe2014-06-26 13:37:14 +02003728}
3729
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003730void mbedtls_ssl_transform_init(mbedtls_ssl_transform *transform)
Paul Bakkeraccaffe2014-06-26 13:37:14 +02003731{
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003732 memset(transform, 0, sizeof(mbedtls_ssl_transform));
Paul Bakker84bbeb52014-07-01 14:53:22 +02003733
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003734 mbedtls_cipher_init(&transform->cipher_ctx_enc);
3735 mbedtls_cipher_init(&transform->cipher_ctx_dec);
Paul Bakker84bbeb52014-07-01 14:53:22 +02003736
Hanno Beckerd56ed242018-01-03 15:32:51 +00003737#if defined(MBEDTLS_SSL_SOME_MODES_USE_MAC)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003738 mbedtls_md_init(&transform->md_ctx_enc);
3739 mbedtls_md_init(&transform->md_ctx_dec);
Hanno Beckerd56ed242018-01-03 15:32:51 +00003740#endif
Paul Bakkeraccaffe2014-06-26 13:37:14 +02003741}
3742
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003743void mbedtls_ssl_session_init(mbedtls_ssl_session *session)
Paul Bakkeraccaffe2014-06-26 13:37:14 +02003744{
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003745 memset(session, 0, sizeof(mbedtls_ssl_session));
Paul Bakkeraccaffe2014-06-26 13:37:14 +02003746}
3747
Manuel Pégourié-Gonnardd904d662022-06-17 10:24:00 +02003748MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003749static int ssl_handshake_init(mbedtls_ssl_context *ssl)
Paul Bakker48916f92012-09-16 19:57:18 +00003750{
Paul Bakkeraccaffe2014-06-26 13:37:14 +02003751 /* Clear old handshake information if present */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003752 if (ssl->transform_negotiate) {
3753 mbedtls_ssl_transform_free(ssl->transform_negotiate);
3754 }
3755 if (ssl->session_negotiate) {
3756 mbedtls_ssl_session_free(ssl->session_negotiate);
3757 }
3758 if (ssl->handshake) {
3759 mbedtls_ssl_handshake_free(ssl);
3760 }
Paul Bakkeraccaffe2014-06-26 13:37:14 +02003761
3762 /*
3763 * Either the pointers are now NULL or cleared properly and can be freed.
3764 * Now allocate missing structures.
3765 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003766 if (ssl->transform_negotiate == NULL) {
3767 ssl->transform_negotiate = mbedtls_calloc(1, sizeof(mbedtls_ssl_transform));
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02003768 }
Paul Bakker48916f92012-09-16 19:57:18 +00003769
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003770 if (ssl->session_negotiate == NULL) {
3771 ssl->session_negotiate = mbedtls_calloc(1, sizeof(mbedtls_ssl_session));
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02003772 }
Paul Bakker48916f92012-09-16 19:57:18 +00003773
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003774 if (ssl->handshake == NULL) {
3775 ssl->handshake = mbedtls_calloc(1, sizeof(mbedtls_ssl_handshake_params));
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02003776 }
Andrzej Kurek0afa2a12020-03-03 10:39:58 -05003777#if defined(MBEDTLS_SSL_VARIABLE_BUFFER_LENGTH)
3778 /* If the buffers are too small - reallocate */
Andrzej Kurek8ea68722020-04-03 06:40:47 -04003779
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003780 handle_buffer_resizing(ssl, 0, MBEDTLS_SSL_IN_BUFFER_LEN,
3781 MBEDTLS_SSL_OUT_BUFFER_LEN);
Andrzej Kurek0afa2a12020-03-03 10:39:58 -05003782#endif
Paul Bakker48916f92012-09-16 19:57:18 +00003783
Paul Bakkeraccaffe2014-06-26 13:37:14 +02003784 /* All pointers should exist and can be directly freed without issue */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003785 if (ssl->handshake == NULL ||
Paul Bakker48916f92012-09-16 19:57:18 +00003786 ssl->transform_negotiate == NULL ||
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003787 ssl->session_negotiate == NULL) {
3788 MBEDTLS_SSL_DEBUG_MSG(1, ("alloc() of ssl sub-contexts failed"));
Paul Bakkeraccaffe2014-06-26 13:37:14 +02003789
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003790 mbedtls_free(ssl->handshake);
3791 mbedtls_free(ssl->transform_negotiate);
3792 mbedtls_free(ssl->session_negotiate);
Paul Bakkeraccaffe2014-06-26 13:37:14 +02003793
3794 ssl->handshake = NULL;
3795 ssl->transform_negotiate = NULL;
3796 ssl->session_negotiate = NULL;
3797
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003798 return MBEDTLS_ERR_SSL_ALLOC_FAILED;
Paul Bakker48916f92012-09-16 19:57:18 +00003799 }
3800
Paul Bakkeraccaffe2014-06-26 13:37:14 +02003801 /* Initialize structures */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003802 mbedtls_ssl_session_init(ssl->session_negotiate);
3803 mbedtls_ssl_transform_init(ssl->transform_negotiate);
3804 ssl_handshake_params_init(ssl->handshake);
Paul Bakker968afaa2014-07-09 11:09:24 +02003805
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003806#if defined(MBEDTLS_SSL_PROTO_DTLS)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003807 if (ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM) {
Manuel Pégourié-Gonnard06939ce2015-05-11 11:25:46 +02003808 ssl->handshake->alt_transform_out = ssl->transform_out;
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003809
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003810 if (ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT) {
Manuel Pégourié-Gonnard06939ce2015-05-11 11:25:46 +02003811 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_PREPARING;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003812 } else {
Manuel Pégourié-Gonnard06939ce2015-05-11 11:25:46 +02003813 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_WAITING;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003814 }
Manuel Pégourié-Gonnard286a1362015-05-13 16:22:05 +02003815
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003816 mbedtls_ssl_set_timer(ssl, 0);
Manuel Pégourié-Gonnard06939ce2015-05-11 11:25:46 +02003817 }
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003818#endif
3819
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003820 return 0;
Paul Bakker48916f92012-09-16 19:57:18 +00003821}
3822
Manuel Pégourié-Gonnarde057d3b2015-05-20 10:59:43 +02003823#if defined(MBEDTLS_SSL_DTLS_HELLO_VERIFY) && defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard7d38d212014-07-23 17:52:09 +02003824/* Dummy cookie callbacks for defaults */
Manuel Pégourié-Gonnardd904d662022-06-17 10:24:00 +02003825MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003826static int ssl_cookie_write_dummy(void *ctx,
3827 unsigned char **p, unsigned char *end,
3828 const unsigned char *cli_id, size_t cli_id_len)
Manuel Pégourié-Gonnard7d38d212014-07-23 17:52:09 +02003829{
3830 ((void) ctx);
3831 ((void) p);
3832 ((void) end);
3833 ((void) cli_id);
3834 ((void) cli_id_len);
3835
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003836 return MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
Manuel Pégourié-Gonnard7d38d212014-07-23 17:52:09 +02003837}
3838
Manuel Pégourié-Gonnardd904d662022-06-17 10:24:00 +02003839MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003840static int ssl_cookie_check_dummy(void *ctx,
3841 const unsigned char *cookie, size_t cookie_len,
3842 const unsigned char *cli_id, size_t cli_id_len)
Manuel Pégourié-Gonnard7d38d212014-07-23 17:52:09 +02003843{
3844 ((void) ctx);
3845 ((void) cookie);
3846 ((void) cookie_len);
3847 ((void) cli_id);
3848 ((void) cli_id_len);
3849
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003850 return MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
Manuel Pégourié-Gonnard7d38d212014-07-23 17:52:09 +02003851}
Manuel Pégourié-Gonnarde057d3b2015-05-20 10:59:43 +02003852#endif /* MBEDTLS_SSL_DTLS_HELLO_VERIFY && MBEDTLS_SSL_SRV_C */
Manuel Pégourié-Gonnard7d38d212014-07-23 17:52:09 +02003853
Paul Bakker5121ce52009-01-03 21:22:43 +00003854/*
3855 * Initialize an SSL context
3856 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003857void mbedtls_ssl_init(mbedtls_ssl_context *ssl)
Manuel Pégourié-Gonnard41d479e2015-04-29 00:48:22 +02003858{
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003859 memset(ssl, 0, sizeof(mbedtls_ssl_context));
Manuel Pégourié-Gonnard41d479e2015-04-29 00:48:22 +02003860}
3861
3862/*
3863 * Setup an SSL context
3864 */
Hanno Becker2a43f6f2018-08-10 11:12:52 +01003865
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003866int mbedtls_ssl_setup(mbedtls_ssl_context *ssl,
3867 const mbedtls_ssl_config *conf)
Paul Bakker5121ce52009-01-03 21:22:43 +00003868{
Janos Follath865b3eb2019-12-16 11:46:15 +00003869 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Darryl Greenb33cc762019-11-28 14:29:44 +00003870 size_t in_buf_len = MBEDTLS_SSL_IN_BUFFER_LEN;
3871 size_t out_buf_len = MBEDTLS_SSL_OUT_BUFFER_LEN;
Paul Bakker5121ce52009-01-03 21:22:43 +00003872
Manuel Pégourié-Gonnarddef0bbe2015-05-04 14:56:36 +02003873 ssl->conf = conf;
Paul Bakker62f2dee2012-09-28 07:31:51 +00003874
3875 /*
Manuel Pégourié-Gonnard06193482014-02-14 08:39:32 +01003876 * Prepare base structures
Paul Bakker62f2dee2012-09-28 07:31:51 +00003877 */
k-stachowiakc9a5f022018-07-24 13:53:31 +02003878
3879 /* Set to NULL in case of an error condition */
3880 ssl->out_buf = NULL;
k-stachowiaka47911c2018-07-04 17:41:58 +02003881
Darryl Greenb33cc762019-11-28 14:29:44 +00003882#if defined(MBEDTLS_SSL_VARIABLE_BUFFER_LENGTH)
3883 ssl->in_buf_len = in_buf_len;
3884#endif
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003885 ssl->in_buf = mbedtls_calloc(1, in_buf_len);
3886 if (ssl->in_buf == NULL) {
3887 MBEDTLS_SSL_DEBUG_MSG(1, ("alloc(%" MBEDTLS_PRINTF_SIZET " bytes) failed", in_buf_len));
k-stachowiak9f7798e2018-07-31 16:52:32 +02003888 ret = MBEDTLS_ERR_SSL_ALLOC_FAILED;
k-stachowiaka47911c2018-07-04 17:41:58 +02003889 goto error;
Angus Grattond8213d02016-05-25 20:56:48 +10003890 }
3891
Darryl Greenb33cc762019-11-28 14:29:44 +00003892#if defined(MBEDTLS_SSL_VARIABLE_BUFFER_LENGTH)
3893 ssl->out_buf_len = out_buf_len;
3894#endif
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003895 ssl->out_buf = mbedtls_calloc(1, out_buf_len);
3896 if (ssl->out_buf == NULL) {
3897 MBEDTLS_SSL_DEBUG_MSG(1, ("alloc(%" MBEDTLS_PRINTF_SIZET " bytes) failed", out_buf_len));
k-stachowiak9f7798e2018-07-31 16:52:32 +02003898 ret = MBEDTLS_ERR_SSL_ALLOC_FAILED;
k-stachowiaka47911c2018-07-04 17:41:58 +02003899 goto error;
Paul Bakker5121ce52009-01-03 21:22:43 +00003900 }
3901
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003902 mbedtls_ssl_reset_in_out_pointers(ssl);
Manuel Pégourié-Gonnard419d5ae2015-05-04 19:32:36 +02003903
Johan Pascalb62bb512015-12-03 21:56:45 +01003904#if defined(MBEDTLS_SSL_DTLS_SRTP)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003905 memset(&ssl->dtls_srtp_info, 0, sizeof(ssl->dtls_srtp_info));
Johan Pascalb62bb512015-12-03 21:56:45 +01003906#endif
3907
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003908 if ((ret = ssl_handshake_init(ssl)) != 0) {
k-stachowiaka47911c2018-07-04 17:41:58 +02003909 goto error;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003910 }
Paul Bakker5121ce52009-01-03 21:22:43 +00003911
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003912 return 0;
k-stachowiaka47911c2018-07-04 17:41:58 +02003913
3914error:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003915 mbedtls_free(ssl->in_buf);
3916 mbedtls_free(ssl->out_buf);
k-stachowiaka47911c2018-07-04 17:41:58 +02003917
3918 ssl->conf = NULL;
3919
Darryl Greenb33cc762019-11-28 14:29:44 +00003920#if defined(MBEDTLS_SSL_VARIABLE_BUFFER_LENGTH)
3921 ssl->in_buf_len = 0;
3922 ssl->out_buf_len = 0;
3923#endif
k-stachowiaka47911c2018-07-04 17:41:58 +02003924 ssl->in_buf = NULL;
3925 ssl->out_buf = NULL;
3926
3927 ssl->in_hdr = NULL;
3928 ssl->in_ctr = NULL;
3929 ssl->in_len = NULL;
3930 ssl->in_iv = NULL;
3931 ssl->in_msg = NULL;
3932
3933 ssl->out_hdr = NULL;
3934 ssl->out_ctr = NULL;
3935 ssl->out_len = NULL;
3936 ssl->out_iv = NULL;
3937 ssl->out_msg = NULL;
3938
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003939 return ret;
Paul Bakker5121ce52009-01-03 21:22:43 +00003940}
3941
3942/*
Paul Bakker7eb013f2011-10-06 12:37:39 +00003943 * Reset an initialized and used SSL context for re-use while retaining
3944 * all application-set variables, function pointers and data.
Manuel Pégourié-Gonnard3f09b6d2015-09-08 11:58:14 +02003945 *
3946 * If partial is non-zero, keep data in the input buffer and client ID.
3947 * (Use when a DTLS client reconnects from the same port.)
Paul Bakker7eb013f2011-10-06 12:37:39 +00003948 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003949int mbedtls_ssl_session_reset_int(mbedtls_ssl_context *ssl, int partial)
Paul Bakker7eb013f2011-10-06 12:37:39 +00003950{
Janos Follath865b3eb2019-12-16 11:46:15 +00003951 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Darryl Greenb33cc762019-11-28 14:29:44 +00003952#if defined(MBEDTLS_SSL_VARIABLE_BUFFER_LENGTH)
3953 size_t in_buf_len = ssl->in_buf_len;
3954 size_t out_buf_len = ssl->out_buf_len;
3955#else
3956 size_t in_buf_len = MBEDTLS_SSL_IN_BUFFER_LEN;
3957 size_t out_buf_len = MBEDTLS_SSL_OUT_BUFFER_LEN;
3958#endif
Paul Bakker48916f92012-09-16 19:57:18 +00003959
Hanno Becker7e772132018-08-10 12:38:21 +01003960#if !defined(MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE) || \
3961 !defined(MBEDTLS_SSL_SRV_C)
3962 ((void) partial);
3963#endif
3964
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003965 ssl->state = MBEDTLS_SSL_HELLO_REQUEST;
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01003966
Manuel Pégourié-Gonnard286a1362015-05-13 16:22:05 +02003967 /* Cancel any possibly running timer */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003968 mbedtls_ssl_set_timer(ssl, 0);
Manuel Pégourié-Gonnard286a1362015-05-13 16:22:05 +02003969
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003970#if defined(MBEDTLS_SSL_RENEGOTIATION)
3971 ssl->renego_status = MBEDTLS_SSL_INITIAL_HANDSHAKE;
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01003972 ssl->renego_records_seen = 0;
Paul Bakker48916f92012-09-16 19:57:18 +00003973
3974 ssl->verify_data_len = 0;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003975 memset(ssl->own_verify_data, 0, MBEDTLS_SSL_VERIFY_DATA_MAX_LEN);
3976 memset(ssl->peer_verify_data, 0, MBEDTLS_SSL_VERIFY_DATA_MAX_LEN);
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01003977#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003978 ssl->secure_renegotiation = MBEDTLS_SSL_LEGACY_RENEGOTIATION;
Paul Bakker48916f92012-09-16 19:57:18 +00003979
Paul Bakker7eb013f2011-10-06 12:37:39 +00003980 ssl->in_offt = NULL;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003981 mbedtls_ssl_reset_in_out_pointers(ssl);
Paul Bakker7eb013f2011-10-06 12:37:39 +00003982
3983 ssl->in_msgtype = 0;
3984 ssl->in_msglen = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003985#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02003986 ssl->next_record_offset = 0;
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02003987 ssl->in_epoch = 0;
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02003988#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003989#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003990 mbedtls_ssl_dtls_replay_reset(ssl);
Manuel Pégourié-Gonnardb47368a2014-09-24 13:29:58 +02003991#endif
Paul Bakker7eb013f2011-10-06 12:37:39 +00003992
3993 ssl->in_hslen = 0;
3994 ssl->nb_zero = 0;
Hanno Beckeraf0665d2017-05-24 09:16:26 +01003995
3996 ssl->keep_current_message = 0;
Paul Bakker7eb013f2011-10-06 12:37:39 +00003997
3998 ssl->out_msgtype = 0;
3999 ssl->out_msglen = 0;
4000 ssl->out_left = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004001#if defined(MBEDTLS_SSL_CBC_RECORD_SPLITTING)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004002 if (ssl->split_done != MBEDTLS_SSL_CBC_RECORD_SPLITTING_DISABLED) {
Manuel Pégourié-Gonnardcfa477e2015-01-07 14:50:54 +01004003 ssl->split_done = 0;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004004 }
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +01004005#endif
Paul Bakker7eb013f2011-10-06 12:37:39 +00004006
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004007 memset(ssl->cur_out_ctr, 0, sizeof(ssl->cur_out_ctr));
Hanno Becker19859472018-08-06 09:40:20 +01004008
Paul Bakker48916f92012-09-16 19:57:18 +00004009 ssl->transform_in = NULL;
4010 ssl->transform_out = NULL;
Paul Bakker7eb013f2011-10-06 12:37:39 +00004011
Hanno Becker78640902018-08-13 16:35:15 +01004012 ssl->session_in = NULL;
4013 ssl->session_out = NULL;
4014
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004015 memset(ssl->out_buf, 0, out_buf_len);
Hanno Becker4ccbf062018-08-10 11:20:38 +01004016
David Horstmannd4f22082022-10-26 18:25:14 +01004017 int clear_in_buf = 1;
Hanno Becker4ccbf062018-08-10 11:20:38 +01004018#if defined(MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE) && defined(MBEDTLS_SSL_SRV_C)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004019 if (partial != 0) {
David Horstmannd4f22082022-10-26 18:25:14 +01004020 clear_in_buf = 0;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004021 }
Hanno Becker4ccbf062018-08-10 11:20:38 +01004022#endif /* MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE && MBEDTLS_SSL_SRV_C */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004023 if (clear_in_buf) {
Hanno Becker4ccbf062018-08-10 11:20:38 +01004024 ssl->in_left = 0;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004025 memset(ssl->in_buf, 0, in_buf_len);
Hanno Becker4ccbf062018-08-10 11:20:38 +01004026 }
Paul Bakker05ef8352012-05-08 09:17:57 +00004027
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004028#if defined(MBEDTLS_SSL_HW_RECORD_ACCEL)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004029 if (mbedtls_ssl_hw_record_reset != NULL) {
4030 MBEDTLS_SSL_DEBUG_MSG(2, ("going for mbedtls_ssl_hw_record_reset()"));
4031 if ((ret = mbedtls_ssl_hw_record_reset(ssl)) != 0) {
4032 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ssl_hw_record_reset", ret);
4033 return MBEDTLS_ERR_SSL_HW_ACCEL_FAILED;
Paul Bakker2770fbd2012-07-03 13:30:23 +00004034 }
Paul Bakker05ef8352012-05-08 09:17:57 +00004035 }
4036#endif
Paul Bakker2770fbd2012-07-03 13:30:23 +00004037
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004038 if (ssl->transform) {
4039 mbedtls_ssl_transform_free(ssl->transform);
4040 mbedtls_free(ssl->transform);
Paul Bakker48916f92012-09-16 19:57:18 +00004041 ssl->transform = NULL;
Paul Bakker2770fbd2012-07-03 13:30:23 +00004042 }
Paul Bakker48916f92012-09-16 19:57:18 +00004043
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004044 if (ssl->session) {
4045 mbedtls_ssl_session_free(ssl->session);
4046 mbedtls_free(ssl->session);
Paul Bakkerc0463502013-02-14 11:19:38 +01004047 ssl->session = NULL;
4048 }
4049
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004050#if defined(MBEDTLS_SSL_ALPN)
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02004051 ssl->alpn_chosen = NULL;
4052#endif
4053
Manuel Pégourié-Gonnarde057d3b2015-05-20 10:59:43 +02004054#if defined(MBEDTLS_SSL_DTLS_HELLO_VERIFY) && defined(MBEDTLS_SSL_SRV_C)
David Horstmannb5b1ed22022-10-27 13:21:49 +01004055 int free_cli_id = 1;
Hanno Becker4ccbf062018-08-10 11:20:38 +01004056#if defined(MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004057 if (partial != 0) {
David Horstmannd4f22082022-10-26 18:25:14 +01004058 free_cli_id = 0;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004059 }
Hanno Becker4ccbf062018-08-10 11:20:38 +01004060#endif
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004061 if (free_cli_id) {
4062 mbedtls_free(ssl->cli_id);
Manuel Pégourié-Gonnard3f09b6d2015-09-08 11:58:14 +02004063 ssl->cli_id = NULL;
4064 ssl->cli_id_len = 0;
4065 }
Manuel Pégourié-Gonnard43c02182014-07-22 17:32:01 +02004066#endif
4067
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004068 if ((ret = ssl_handshake_init(ssl)) != 0) {
4069 return ret;
4070 }
Paul Bakker2770fbd2012-07-03 13:30:23 +00004071
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004072 return 0;
Paul Bakker7eb013f2011-10-06 12:37:39 +00004073}
4074
Manuel Pégourié-Gonnard779e4292013-08-03 13:50:48 +02004075/*
Manuel Pégourié-Gonnard3f09b6d2015-09-08 11:58:14 +02004076 * Reset an initialized and used SSL context for re-use while retaining
4077 * all application-set variables, function pointers and data.
4078 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004079int mbedtls_ssl_session_reset(mbedtls_ssl_context *ssl)
Manuel Pégourié-Gonnard3f09b6d2015-09-08 11:58:14 +02004080{
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004081 return mbedtls_ssl_session_reset_int(ssl, 0);
Manuel Pégourié-Gonnard3f09b6d2015-09-08 11:58:14 +02004082}
4083
4084/*
Paul Bakker5121ce52009-01-03 21:22:43 +00004085 * SSL set accessors
4086 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004087void mbedtls_ssl_conf_endpoint(mbedtls_ssl_config *conf, int endpoint)
Paul Bakker5121ce52009-01-03 21:22:43 +00004088{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02004089 conf->endpoint = endpoint;
Paul Bakker5121ce52009-01-03 21:22:43 +00004090}
4091
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004092void mbedtls_ssl_conf_transport(mbedtls_ssl_config *conf, int transport)
Manuel Pégourié-Gonnard0b1ff292014-02-06 13:04:16 +01004093{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02004094 conf->transport = transport;
Manuel Pégourié-Gonnard0b1ff292014-02-06 13:04:16 +01004095}
4096
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004097#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004098void mbedtls_ssl_conf_dtls_anti_replay(mbedtls_ssl_config *conf, char mode)
Manuel Pégourié-Gonnard27393132014-09-24 14:41:11 +02004099{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02004100 conf->anti_replay = mode;
Manuel Pégourié-Gonnard27393132014-09-24 14:41:11 +02004101}
4102#endif
4103
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004104#if defined(MBEDTLS_SSL_DTLS_BADMAC_LIMIT)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004105void mbedtls_ssl_conf_dtls_badmac_limit(mbedtls_ssl_config *conf, unsigned limit)
Manuel Pégourié-Gonnardb0643d12014-10-14 18:30:36 +02004106{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02004107 conf->badmac_limit = limit;
Manuel Pégourié-Gonnardb0643d12014-10-14 18:30:36 +02004108}
4109#endif
4110
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004111#if defined(MBEDTLS_SSL_PROTO_DTLS)
Hanno Becker04da1892018-08-14 13:22:10 +01004112
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004113void mbedtls_ssl_set_datagram_packing(mbedtls_ssl_context *ssl,
4114 unsigned allow_packing)
Hanno Becker04da1892018-08-14 13:22:10 +01004115{
4116 ssl->disable_datagram_packing = !allow_packing;
4117}
4118
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004119void mbedtls_ssl_conf_handshake_timeout(mbedtls_ssl_config *conf,
4120 uint32_t min, uint32_t max)
Manuel Pégourié-Gonnard905dd242014-10-01 12:03:55 +02004121{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02004122 conf->hs_timeout_min = min;
4123 conf->hs_timeout_max = max;
Manuel Pégourié-Gonnard905dd242014-10-01 12:03:55 +02004124}
4125#endif
4126
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004127void mbedtls_ssl_conf_authmode(mbedtls_ssl_config *conf, int authmode)
Paul Bakker5121ce52009-01-03 21:22:43 +00004128{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02004129 conf->authmode = authmode;
Paul Bakker5121ce52009-01-03 21:22:43 +00004130}
4131
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004132#if defined(MBEDTLS_X509_CRT_PARSE_C)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004133void mbedtls_ssl_conf_verify(mbedtls_ssl_config *conf,
4134 int (*f_vrfy)(void *, mbedtls_x509_crt *, int, uint32_t *),
4135 void *p_vrfy)
Paul Bakkerb63b0af2011-01-13 17:54:59 +00004136{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02004137 conf->f_vrfy = f_vrfy;
4138 conf->p_vrfy = p_vrfy;
Paul Bakkerb63b0af2011-01-13 17:54:59 +00004139}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004140#endif /* MBEDTLS_X509_CRT_PARSE_C */
Paul Bakkerb63b0af2011-01-13 17:54:59 +00004141
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004142void mbedtls_ssl_conf_rng(mbedtls_ssl_config *conf,
4143 int (*f_rng)(void *, unsigned char *, size_t),
4144 void *p_rng)
Paul Bakker5121ce52009-01-03 21:22:43 +00004145{
Manuel Pégourié-Gonnard750e4d72015-05-07 12:35:38 +01004146 conf->f_rng = f_rng;
4147 conf->p_rng = p_rng;
Paul Bakker5121ce52009-01-03 21:22:43 +00004148}
4149
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004150void mbedtls_ssl_conf_dbg(mbedtls_ssl_config *conf,
4151 void (*f_dbg)(void *, int, const char *, int, const char *),
4152 void *p_dbg)
Paul Bakker5121ce52009-01-03 21:22:43 +00004153{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02004154 conf->f_dbg = f_dbg;
4155 conf->p_dbg = p_dbg;
Paul Bakker5121ce52009-01-03 21:22:43 +00004156}
4157
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004158void mbedtls_ssl_set_bio(mbedtls_ssl_context *ssl,
4159 void *p_bio,
4160 mbedtls_ssl_send_t *f_send,
4161 mbedtls_ssl_recv_t *f_recv,
4162 mbedtls_ssl_recv_timeout_t *f_recv_timeout)
Manuel Pégourié-Gonnard8fa6dfd2014-09-17 10:47:43 +02004163{
4164 ssl->p_bio = p_bio;
4165 ssl->f_send = f_send;
4166 ssl->f_recv = f_recv;
4167 ssl->f_recv_timeout = f_recv_timeout;
Manuel Pégourié-Gonnard97fd52c2015-05-06 15:38:52 +01004168}
4169
Manuel Pégourié-Gonnard6e7aaca2018-08-20 10:37:23 +02004170#if defined(MBEDTLS_SSL_PROTO_DTLS)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004171void mbedtls_ssl_set_mtu(mbedtls_ssl_context *ssl, uint16_t mtu)
Manuel Pégourié-Gonnard6e7aaca2018-08-20 10:37:23 +02004172{
4173 ssl->mtu = mtu;
4174}
4175#endif
4176
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004177void mbedtls_ssl_conf_read_timeout(mbedtls_ssl_config *conf, uint32_t timeout)
Manuel Pégourié-Gonnard97fd52c2015-05-06 15:38:52 +01004178{
4179 conf->read_timeout = timeout;
Manuel Pégourié-Gonnard8fa6dfd2014-09-17 10:47:43 +02004180}
4181
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004182void mbedtls_ssl_set_timer_cb(mbedtls_ssl_context *ssl,
4183 void *p_timer,
4184 mbedtls_ssl_set_timer_t *f_set_timer,
4185 mbedtls_ssl_get_timer_t *f_get_timer)
Manuel Pégourié-Gonnard2e012912015-05-12 20:55:41 +02004186{
4187 ssl->p_timer = p_timer;
4188 ssl->f_set_timer = f_set_timer;
4189 ssl->f_get_timer = f_get_timer;
Manuel Pégourié-Gonnard286a1362015-05-13 16:22:05 +02004190
4191 /* Make sure we start with no timer running */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004192 mbedtls_ssl_set_timer(ssl, 0);
Manuel Pégourié-Gonnard2e012912015-05-12 20:55:41 +02004193}
4194
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004195#if defined(MBEDTLS_SSL_SRV_C)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004196void mbedtls_ssl_conf_session_cache(mbedtls_ssl_config *conf,
4197 void *p_cache,
4198 int (*f_get_cache)(void *, mbedtls_ssl_session *),
4199 int (*f_set_cache)(void *, const mbedtls_ssl_session *))
Paul Bakker5121ce52009-01-03 21:22:43 +00004200{
Manuel Pégourié-Gonnard5cb33082015-05-06 18:06:26 +01004201 conf->p_cache = p_cache;
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02004202 conf->f_get_cache = f_get_cache;
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02004203 conf->f_set_cache = f_set_cache;
Paul Bakker5121ce52009-01-03 21:22:43 +00004204}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004205#endif /* MBEDTLS_SSL_SRV_C */
Paul Bakker5121ce52009-01-03 21:22:43 +00004206
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004207#if defined(MBEDTLS_SSL_CLI_C)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004208int mbedtls_ssl_set_session(mbedtls_ssl_context *ssl, const mbedtls_ssl_session *session)
Paul Bakker5121ce52009-01-03 21:22:43 +00004209{
Janos Follath865b3eb2019-12-16 11:46:15 +00004210 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +02004211
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004212 if (ssl == NULL ||
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +02004213 session == NULL ||
4214 ssl->session_negotiate == NULL ||
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004215 ssl->conf->endpoint != MBEDTLS_SSL_IS_CLIENT) {
4216 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +02004217 }
4218
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004219 if ((ret = mbedtls_ssl_session_copy(ssl->session_negotiate,
4220 session)) != 0) {
4221 return ret;
4222 }
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +02004223
Paul Bakker0a597072012-09-25 21:55:46 +00004224 ssl->handshake->resume = 1;
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +02004225
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004226 return 0;
Paul Bakker5121ce52009-01-03 21:22:43 +00004227}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004228#endif /* MBEDTLS_SSL_CLI_C */
Paul Bakker5121ce52009-01-03 21:22:43 +00004229
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004230void mbedtls_ssl_conf_ciphersuites(mbedtls_ssl_config *conf,
4231 const int *ciphersuites)
Paul Bakker5121ce52009-01-03 21:22:43 +00004232{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02004233 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_0] = ciphersuites;
4234 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_1] = ciphersuites;
4235 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_2] = ciphersuites;
4236 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_3] = ciphersuites;
Paul Bakker8f4ddae2013-04-15 15:09:54 +02004237}
4238
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004239void mbedtls_ssl_conf_ciphersuites_for_version(mbedtls_ssl_config *conf,
4240 const int *ciphersuites,
4241 int major, int minor)
Paul Bakker8f4ddae2013-04-15 15:09:54 +02004242{
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004243 if (major != MBEDTLS_SSL_MAJOR_VERSION_3) {
Paul Bakker8f4ddae2013-04-15 15:09:54 +02004244 return;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004245 }
Paul Bakker8f4ddae2013-04-15 15:09:54 +02004246
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004247 if (minor < MBEDTLS_SSL_MINOR_VERSION_0 || minor > MBEDTLS_SSL_MINOR_VERSION_3) {
Paul Bakker8f4ddae2013-04-15 15:09:54 +02004248 return;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004249 }
Paul Bakker8f4ddae2013-04-15 15:09:54 +02004250
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02004251 conf->ciphersuite_list[minor] = ciphersuites;
Paul Bakker5121ce52009-01-03 21:22:43 +00004252}
4253
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004254#if defined(MBEDTLS_X509_CRT_PARSE_C)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004255void mbedtls_ssl_conf_cert_profile(mbedtls_ssl_config *conf,
4256 const mbedtls_x509_crt_profile *profile)
Manuel Pégourié-Gonnard6e3ee3a2015-06-17 10:58:20 +02004257{
4258 conf->cert_profile = profile;
4259}
4260
Manuel Pégourié-Gonnard8f618a82015-05-10 21:13:36 +02004261/* Append a new keycert entry to a (possibly empty) list */
Manuel Pégourié-Gonnardd904d662022-06-17 10:24:00 +02004262MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004263static int ssl_append_key_cert(mbedtls_ssl_key_cert **head,
4264 mbedtls_x509_crt *cert,
4265 mbedtls_pk_context *key)
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02004266{
niisato8ee24222018-06-25 19:05:48 +09004267 mbedtls_ssl_key_cert *new_cert;
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02004268
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004269 new_cert = mbedtls_calloc(1, sizeof(mbedtls_ssl_key_cert));
4270 if (new_cert == NULL) {
4271 return MBEDTLS_ERR_SSL_ALLOC_FAILED;
4272 }
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02004273
niisato8ee24222018-06-25 19:05:48 +09004274 new_cert->cert = cert;
4275 new_cert->key = key;
4276 new_cert->next = NULL;
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02004277
Manuel Pégourié-Gonnard8f618a82015-05-10 21:13:36 +02004278 /* Update head is the list was null, else add to the end */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004279 if (*head == NULL) {
niisato8ee24222018-06-25 19:05:48 +09004280 *head = new_cert;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004281 } else {
Manuel Pégourié-Gonnard8f618a82015-05-10 21:13:36 +02004282 mbedtls_ssl_key_cert *cur = *head;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004283 while (cur->next != NULL) {
Manuel Pégourié-Gonnard8f618a82015-05-10 21:13:36 +02004284 cur = cur->next;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004285 }
niisato8ee24222018-06-25 19:05:48 +09004286 cur->next = new_cert;
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02004287 }
4288
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004289 return 0;
Manuel Pégourié-Gonnard8f618a82015-05-10 21:13:36 +02004290}
4291
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004292int mbedtls_ssl_conf_own_cert(mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnard8f618a82015-05-10 21:13:36 +02004293 mbedtls_x509_crt *own_cert,
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004294 mbedtls_pk_context *pk_key)
Manuel Pégourié-Gonnard8f618a82015-05-10 21:13:36 +02004295{
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004296 return ssl_append_key_cert(&conf->key_cert, own_cert, pk_key);
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02004297}
4298
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004299void mbedtls_ssl_conf_ca_chain(mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnardbc2b7712015-05-06 11:14:19 +01004300 mbedtls_x509_crt *ca_chain,
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004301 mbedtls_x509_crl *ca_crl)
Paul Bakker5121ce52009-01-03 21:22:43 +00004302{
Manuel Pégourié-Gonnardbc2b7712015-05-06 11:14:19 +01004303 conf->ca_chain = ca_chain;
4304 conf->ca_crl = ca_crl;
Hanno Becker5adaad92019-03-27 16:54:37 +00004305
4306#if defined(MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK)
4307 /* mbedtls_ssl_conf_ca_chain() and mbedtls_ssl_conf_ca_cb()
4308 * cannot be used together. */
4309 conf->f_ca_cb = NULL;
4310 conf->p_ca_cb = NULL;
4311#endif /* MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK */
Paul Bakker5121ce52009-01-03 21:22:43 +00004312}
Hanno Becker5adaad92019-03-27 16:54:37 +00004313
4314#if defined(MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004315void mbedtls_ssl_conf_ca_cb(mbedtls_ssl_config *conf,
4316 mbedtls_x509_crt_ca_cb_t f_ca_cb,
4317 void *p_ca_cb)
Hanno Becker5adaad92019-03-27 16:54:37 +00004318{
4319 conf->f_ca_cb = f_ca_cb;
4320 conf->p_ca_cb = p_ca_cb;
4321
4322 /* mbedtls_ssl_conf_ca_chain() and mbedtls_ssl_conf_ca_cb()
4323 * cannot be used together. */
4324 conf->ca_chain = NULL;
4325 conf->ca_crl = NULL;
4326}
4327#endif /* MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004328#endif /* MBEDTLS_X509_CRT_PARSE_C */
Paul Bakkereb2c6582012-09-27 19:15:01 +00004329
Manuel Pégourié-Gonnard1af6c852015-05-10 23:10:37 +02004330#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004331int mbedtls_ssl_set_hs_own_cert(mbedtls_ssl_context *ssl,
4332 mbedtls_x509_crt *own_cert,
4333 mbedtls_pk_context *pk_key)
Manuel Pégourié-Gonnard1af6c852015-05-10 23:10:37 +02004334{
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004335 return ssl_append_key_cert(&ssl->handshake->sni_key_cert,
4336 own_cert, pk_key);
Manuel Pégourié-Gonnard1af6c852015-05-10 23:10:37 +02004337}
Manuel Pégourié-Gonnard22bfa4b2015-05-11 08:46:37 +02004338
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004339void mbedtls_ssl_set_hs_ca_chain(mbedtls_ssl_context *ssl,
4340 mbedtls_x509_crt *ca_chain,
4341 mbedtls_x509_crl *ca_crl)
Manuel Pégourié-Gonnard22bfa4b2015-05-11 08:46:37 +02004342{
4343 ssl->handshake->sni_ca_chain = ca_chain;
4344 ssl->handshake->sni_ca_crl = ca_crl;
4345}
Manuel Pégourié-Gonnardcdc26ae2015-06-19 12:16:31 +02004346
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004347void mbedtls_ssl_set_hs_authmode(mbedtls_ssl_context *ssl,
4348 int authmode)
Manuel Pégourié-Gonnardcdc26ae2015-06-19 12:16:31 +02004349{
4350 ssl->handshake->sni_authmode = authmode;
4351}
Manuel Pégourié-Gonnard1af6c852015-05-10 23:10:37 +02004352#endif /* MBEDTLS_SSL_SERVER_NAME_INDICATION */
4353
Hanno Becker8927c832019-04-03 12:52:50 +01004354#if defined(MBEDTLS_X509_CRT_PARSE_C)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004355void mbedtls_ssl_set_verify(mbedtls_ssl_context *ssl,
4356 int (*f_vrfy)(void *, mbedtls_x509_crt *, int, uint32_t *),
4357 void *p_vrfy)
Hanno Becker8927c832019-04-03 12:52:50 +01004358{
4359 ssl->f_vrfy = f_vrfy;
4360 ssl->p_vrfy = p_vrfy;
4361}
4362#endif
4363
Manuel Pégourié-Gonnardeef142d2015-09-16 10:05:04 +02004364#if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
Manuel Pégourié-Gonnard7002f4a2015-09-15 12:43:43 +02004365/*
4366 * Set EC J-PAKE password for current handshake
4367 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004368int mbedtls_ssl_set_hs_ecjpake_password(mbedtls_ssl_context *ssl,
4369 const unsigned char *pw,
4370 size_t pw_len)
Manuel Pégourié-Gonnard7002f4a2015-09-15 12:43:43 +02004371{
4372 mbedtls_ecjpake_role role;
4373
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004374 if (ssl->handshake == NULL || ssl->conf == NULL) {
4375 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
4376 }
Manuel Pégourié-Gonnard7002f4a2015-09-15 12:43:43 +02004377
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004378 if (ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER) {
Manuel Pégourié-Gonnard7002f4a2015-09-15 12:43:43 +02004379 role = MBEDTLS_ECJPAKE_SERVER;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004380 } else {
Manuel Pégourié-Gonnard7002f4a2015-09-15 12:43:43 +02004381 role = MBEDTLS_ECJPAKE_CLIENT;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004382 }
Manuel Pégourié-Gonnard7002f4a2015-09-15 12:43:43 +02004383
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004384 return mbedtls_ecjpake_setup(&ssl->handshake->ecjpake_ctx,
4385 role,
4386 MBEDTLS_MD_SHA256,
4387 MBEDTLS_ECP_DP_SECP256R1,
4388 pw, pw_len);
Manuel Pégourié-Gonnard7002f4a2015-09-15 12:43:43 +02004389}
Manuel Pégourié-Gonnardeef142d2015-09-16 10:05:04 +02004390#endif /* MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED */
Manuel Pégourié-Gonnard7002f4a2015-09-15 12:43:43 +02004391
Gilles Peskineeccd8882020-03-10 12:19:08 +01004392#if defined(MBEDTLS_KEY_EXCHANGE_SOME_PSK_ENABLED)
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01004393
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004394static void ssl_conf_remove_psk(mbedtls_ssl_config *conf)
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01004395{
4396 /* Remove reference to existing PSK, if any. */
4397#if defined(MBEDTLS_USE_PSA_CRYPTO)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004398 if (!mbedtls_svc_key_id_is_null(conf->psk_opaque)) {
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01004399 /* The maintenance of the PSK key slot is the
4400 * user's responsibility. */
Ronald Croncf56a0a2020-08-04 09:51:30 +02004401 conf->psk_opaque = MBEDTLS_SVC_KEY_ID_INIT;
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01004402 }
Hanno Beckera63ac3f2018-11-05 12:47:16 +00004403 /* This and the following branch should never
Tom Cosgrove49f99bc2022-12-04 16:44:21 +00004404 * be taken simultaneously as we maintain the
Hanno Beckera63ac3f2018-11-05 12:47:16 +00004405 * invariant that raw and opaque PSKs are never
4406 * configured simultaneously. As a safeguard,
4407 * though, `else` is omitted here. */
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01004408#endif /* MBEDTLS_USE_PSA_CRYPTO */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004409 if (conf->psk != NULL) {
4410 mbedtls_platform_zeroize(conf->psk, conf->psk_len);
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01004411
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004412 mbedtls_free(conf->psk);
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01004413 conf->psk = NULL;
4414 conf->psk_len = 0;
4415 }
4416
4417 /* Remove reference to PSK identity, if any. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004418 if (conf->psk_identity != NULL) {
4419 mbedtls_free(conf->psk_identity);
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01004420 conf->psk_identity = NULL;
4421 conf->psk_identity_len = 0;
4422 }
4423}
4424
Hanno Becker7390c712018-11-15 13:33:04 +00004425/* This function assumes that PSK identity in the SSL config is unset.
4426 * It checks that the provided identity is well-formed and attempts
4427 * to make a copy of it in the SSL config.
4428 * On failure, the PSK identity in the config remains unset. */
Manuel Pégourié-Gonnardd904d662022-06-17 10:24:00 +02004429MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004430static int ssl_conf_set_psk_identity(mbedtls_ssl_config *conf,
4431 unsigned char const *psk_identity,
4432 size_t psk_identity_len)
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02004433{
Manuel Pégourié-Gonnardc6b5d832015-08-27 16:37:35 +02004434 /* Identity len will be encoded on two bytes */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004435 if (psk_identity == NULL ||
4436 (psk_identity_len >> 16) != 0 ||
4437 psk_identity_len > MBEDTLS_SSL_OUT_CONTENT_LEN) {
4438 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
Manuel Pégourié-Gonnardc6b5d832015-08-27 16:37:35 +02004439 }
4440
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004441 conf->psk_identity = mbedtls_calloc(1, psk_identity_len);
4442 if (conf->psk_identity == NULL) {
4443 return MBEDTLS_ERR_SSL_ALLOC_FAILED;
4444 }
Paul Bakker6db455e2013-09-18 17:29:31 +02004445
Manuel Pégourié-Gonnard120fdbd2015-05-07 17:07:50 +01004446 conf->psk_identity_len = psk_identity_len;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004447 memcpy(conf->psk_identity, psk_identity, conf->psk_identity_len);
Paul Bakker5ad403f2013-09-18 21:21:30 +02004448
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004449 return 0;
Paul Bakker6db455e2013-09-18 17:29:31 +02004450}
4451
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004452int mbedtls_ssl_conf_psk(mbedtls_ssl_config *conf,
4453 const unsigned char *psk, size_t psk_len,
4454 const unsigned char *psk_identity, size_t psk_identity_len)
Hanno Becker7390c712018-11-15 13:33:04 +00004455{
Janos Follath865b3eb2019-12-16 11:46:15 +00004456 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Hanno Becker7390c712018-11-15 13:33:04 +00004457 /* Remove opaque/raw PSK + PSK Identity */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004458 ssl_conf_remove_psk(conf);
Hanno Becker7390c712018-11-15 13:33:04 +00004459
4460 /* Check and set raw PSK */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004461 if (psk == NULL) {
4462 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
4463 }
4464 if (psk_len == 0) {
4465 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
4466 }
4467 if (psk_len > MBEDTLS_PSK_MAX_LEN) {
4468 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
4469 }
Piotr Nowicki9926eaf2019-11-20 14:54:36 +01004470
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004471 if ((conf->psk = mbedtls_calloc(1, psk_len)) == NULL) {
4472 return MBEDTLS_ERR_SSL_ALLOC_FAILED;
4473 }
Hanno Becker7390c712018-11-15 13:33:04 +00004474 conf->psk_len = psk_len;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004475 memcpy(conf->psk, psk, conf->psk_len);
Hanno Becker7390c712018-11-15 13:33:04 +00004476
4477 /* Check and set PSK Identity */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004478 ret = ssl_conf_set_psk_identity(conf, psk_identity, psk_identity_len);
4479 if (ret != 0) {
4480 ssl_conf_remove_psk(conf);
4481 }
Hanno Becker7390c712018-11-15 13:33:04 +00004482
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004483 return ret;
Hanno Becker7390c712018-11-15 13:33:04 +00004484}
4485
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004486static void ssl_remove_psk(mbedtls_ssl_context *ssl)
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01004487{
4488#if defined(MBEDTLS_USE_PSA_CRYPTO)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004489 if (!mbedtls_svc_key_id_is_null(ssl->handshake->psk_opaque)) {
Ronald Croncf56a0a2020-08-04 09:51:30 +02004490 ssl->handshake->psk_opaque = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004491 } else
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01004492#endif /* MBEDTLS_USE_PSA_CRYPTO */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004493 if (ssl->handshake->psk != NULL) {
4494 mbedtls_platform_zeroize(ssl->handshake->psk,
4495 ssl->handshake->psk_len);
4496 mbedtls_free(ssl->handshake->psk);
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01004497 ssl->handshake->psk_len = 0;
lhuang040a2dd6d2024-06-11 12:31:17 -07004498 ssl->handshake->psk = NULL;
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01004499 }
4500}
4501
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004502int mbedtls_ssl_set_hs_psk(mbedtls_ssl_context *ssl,
4503 const unsigned char *psk, size_t psk_len)
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01004504{
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004505 if (psk == NULL || ssl->handshake == NULL) {
4506 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
4507 }
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01004508
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004509 if (psk_len > MBEDTLS_PSK_MAX_LEN) {
4510 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
4511 }
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01004512
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004513 ssl_remove_psk(ssl);
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01004514
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004515 if ((ssl->handshake->psk = mbedtls_calloc(1, psk_len)) == NULL) {
4516 return MBEDTLS_ERR_SSL_ALLOC_FAILED;
4517 }
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01004518
4519 ssl->handshake->psk_len = psk_len;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004520 memcpy(ssl->handshake->psk, psk, ssl->handshake->psk_len);
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01004521
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004522 return 0;
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01004523}
4524
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01004525#if defined(MBEDTLS_USE_PSA_CRYPTO)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004526int mbedtls_ssl_conf_psk_opaque(mbedtls_ssl_config *conf,
4527 psa_key_id_t psk,
4528 const unsigned char *psk_identity,
4529 size_t psk_identity_len)
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01004530{
Janos Follath865b3eb2019-12-16 11:46:15 +00004531 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Hanno Becker7390c712018-11-15 13:33:04 +00004532 /* Clear opaque/raw PSK + PSK Identity, if present. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004533 ssl_conf_remove_psk(conf);
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01004534
Hanno Becker7390c712018-11-15 13:33:04 +00004535 /* Check and set opaque PSK */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004536 if (mbedtls_svc_key_id_is_null(psk)) {
4537 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
4538 }
Ronald Croncf56a0a2020-08-04 09:51:30 +02004539 conf->psk_opaque = psk;
Hanno Becker7390c712018-11-15 13:33:04 +00004540
4541 /* Check and set PSK Identity */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004542 ret = ssl_conf_set_psk_identity(conf, psk_identity,
4543 psk_identity_len);
4544 if (ret != 0) {
4545 ssl_conf_remove_psk(conf);
4546 }
Hanno Becker7390c712018-11-15 13:33:04 +00004547
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004548 return ret;
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01004549}
4550
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004551int mbedtls_ssl_set_hs_psk_opaque(mbedtls_ssl_context *ssl,
4552 psa_key_id_t psk)
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01004553{
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004554 if ((mbedtls_svc_key_id_is_null(psk)) ||
4555 (ssl->handshake == NULL)) {
4556 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
4557 }
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01004558
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004559 ssl_remove_psk(ssl);
Ronald Croncf56a0a2020-08-04 09:51:30 +02004560 ssl->handshake->psk_opaque = psk;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004561 return 0;
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01004562}
4563#endif /* MBEDTLS_USE_PSA_CRYPTO */
4564
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004565void mbedtls_ssl_conf_psk_cb(mbedtls_ssl_config *conf,
4566 int (*f_psk)(void *, mbedtls_ssl_context *, const unsigned char *,
4567 size_t),
4568 void *p_psk)
Paul Bakker6db455e2013-09-18 17:29:31 +02004569{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02004570 conf->f_psk = f_psk;
4571 conf->p_psk = p_psk;
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02004572}
Gilles Peskineeccd8882020-03-10 12:19:08 +01004573#endif /* MBEDTLS_KEY_EXCHANGE_SOME_PSK_ENABLED */
Paul Bakker43b7e352011-01-18 15:27:19 +00004574
Manuel Pégourié-Gonnardcf141ca2015-05-20 10:35:51 +02004575#if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_SSL_SRV_C)
Hanno Becker470a8c42017-10-04 15:28:46 +01004576
4577#if !defined(MBEDTLS_DEPRECATED_REMOVED)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004578int mbedtls_ssl_conf_dh_param(mbedtls_ssl_config *conf, const char *dhm_P, const char *dhm_G)
Paul Bakker5121ce52009-01-03 21:22:43 +00004579{
Janos Follath865b3eb2019-12-16 11:46:15 +00004580 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker5121ce52009-01-03 21:22:43 +00004581
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004582 if ((ret = mbedtls_mpi_read_string(&conf->dhm_P, 16, dhm_P)) != 0 ||
4583 (ret = mbedtls_mpi_read_string(&conf->dhm_G, 16, dhm_G)) != 0) {
4584 mbedtls_mpi_free(&conf->dhm_P);
4585 mbedtls_mpi_free(&conf->dhm_G);
4586 return ret;
Manuel Pégourié-Gonnard1028b742015-05-06 17:33:07 +01004587 }
Paul Bakker5121ce52009-01-03 21:22:43 +00004588
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004589 return 0;
Paul Bakker5121ce52009-01-03 21:22:43 +00004590}
Hanno Becker470a8c42017-10-04 15:28:46 +01004591#endif /* MBEDTLS_DEPRECATED_REMOVED */
Paul Bakker5121ce52009-01-03 21:22:43 +00004592
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004593int mbedtls_ssl_conf_dh_param_bin(mbedtls_ssl_config *conf,
4594 const unsigned char *dhm_P, size_t P_len,
4595 const unsigned char *dhm_G, size_t G_len)
Hanno Beckera90658f2017-10-04 15:29:08 +01004596{
Janos Follath865b3eb2019-12-16 11:46:15 +00004597 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Hanno Beckera90658f2017-10-04 15:29:08 +01004598
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004599 mbedtls_mpi_free(&conf->dhm_P);
4600 mbedtls_mpi_free(&conf->dhm_G);
Glenn Straussde081ce2021-12-20 01:43:17 -05004601
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004602 if ((ret = mbedtls_mpi_read_binary(&conf->dhm_P, dhm_P, P_len)) != 0 ||
4603 (ret = mbedtls_mpi_read_binary(&conf->dhm_G, dhm_G, G_len)) != 0) {
4604 mbedtls_mpi_free(&conf->dhm_P);
4605 mbedtls_mpi_free(&conf->dhm_G);
4606 return ret;
Hanno Beckera90658f2017-10-04 15:29:08 +01004607 }
4608
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004609 return 0;
Hanno Beckera90658f2017-10-04 15:29:08 +01004610}
Paul Bakker5121ce52009-01-03 21:22:43 +00004611
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004612int mbedtls_ssl_conf_dh_param_ctx(mbedtls_ssl_config *conf, mbedtls_dhm_context *dhm_ctx)
Paul Bakker1b57b062011-01-06 15:48:19 +00004613{
Janos Follath865b3eb2019-12-16 11:46:15 +00004614 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker1b57b062011-01-06 15:48:19 +00004615
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004616 mbedtls_mpi_free(&conf->dhm_P);
4617 mbedtls_mpi_free(&conf->dhm_G);
Glenn Straussde081ce2021-12-20 01:43:17 -05004618
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004619 if ((ret = mbedtls_mpi_copy(&conf->dhm_P, &dhm_ctx->P)) != 0 ||
4620 (ret = mbedtls_mpi_copy(&conf->dhm_G, &dhm_ctx->G)) != 0) {
4621 mbedtls_mpi_free(&conf->dhm_P);
4622 mbedtls_mpi_free(&conf->dhm_G);
4623 return ret;
Manuel Pégourié-Gonnard1028b742015-05-06 17:33:07 +01004624 }
Paul Bakker1b57b062011-01-06 15:48:19 +00004625
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004626 return 0;
Paul Bakker1b57b062011-01-06 15:48:19 +00004627}
Manuel Pégourié-Gonnardcf141ca2015-05-20 10:35:51 +02004628#endif /* MBEDTLS_DHM_C && MBEDTLS_SSL_SRV_C */
Paul Bakker1b57b062011-01-06 15:48:19 +00004629
Manuel Pégourié-Gonnardbd990d62015-06-11 14:49:42 +02004630#if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_SSL_CLI_C)
4631/*
4632 * Set the minimum length for Diffie-Hellman parameters
4633 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004634void mbedtls_ssl_conf_dhm_min_bitlen(mbedtls_ssl_config *conf,
4635 unsigned int bitlen)
Manuel Pégourié-Gonnardbd990d62015-06-11 14:49:42 +02004636{
4637 conf->dhm_min_bitlen = bitlen;
4638}
4639#endif /* MBEDTLS_DHM_C && MBEDTLS_SSL_CLI_C */
4640
Gilles Peskineeccd8882020-03-10 12:19:08 +01004641#if defined(MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED)
Manuel Pégourié-Gonnard36a8b572015-06-17 12:43:26 +02004642/*
4643 * Set allowed/preferred hashes for handshake signatures
4644 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004645void mbedtls_ssl_conf_sig_hashes(mbedtls_ssl_config *conf,
4646 const int *hashes)
Manuel Pégourié-Gonnard36a8b572015-06-17 12:43:26 +02004647{
4648 conf->sig_hashes = hashes;
4649}
Gilles Peskineeccd8882020-03-10 12:19:08 +01004650#endif /* MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED */
Manuel Pégourié-Gonnard36a8b572015-06-17 12:43:26 +02004651
Manuel Pégourié-Gonnardb541da62015-06-17 11:43:30 +02004652#if defined(MBEDTLS_ECP_C)
Manuel Pégourié-Gonnard7f38ed02014-02-04 15:52:33 +01004653/*
4654 * Set the allowed elliptic curves
4655 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004656void mbedtls_ssl_conf_curves(mbedtls_ssl_config *conf,
4657 const mbedtls_ecp_group_id *curve_list)
Manuel Pégourié-Gonnard7f38ed02014-02-04 15:52:33 +01004658{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02004659 conf->curve_list = curve_list;
Manuel Pégourié-Gonnard7f38ed02014-02-04 15:52:33 +01004660}
Hanno Becker947194e2017-04-07 13:25:49 +01004661#endif /* MBEDTLS_ECP_C */
Manuel Pégourié-Gonnard7f38ed02014-02-04 15:52:33 +01004662
Manuel Pégourié-Gonnardbc2b7712015-05-06 11:14:19 +01004663#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004664void mbedtls_ssl_conf_sni(mbedtls_ssl_config *conf,
4665 int (*f_sni)(void *, mbedtls_ssl_context *,
4666 const unsigned char *, size_t),
4667 void *p_sni)
Paul Bakker5701cdc2012-09-27 21:49:42 +00004668{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02004669 conf->f_sni = f_sni;
4670 conf->p_sni = p_sni;
Paul Bakker5701cdc2012-09-27 21:49:42 +00004671}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004672#endif /* MBEDTLS_SSL_SERVER_NAME_INDICATION */
Paul Bakker5701cdc2012-09-27 21:49:42 +00004673
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004674#if defined(MBEDTLS_SSL_ALPN)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004675int mbedtls_ssl_conf_alpn_protocols(mbedtls_ssl_config *conf, const char **protos)
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02004676{
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +02004677 size_t cur_len, tot_len;
4678 const char **p;
4679
4680 /*
Brian J Murray1903fb32016-11-06 04:45:15 -08004681 * RFC 7301 3.1: "Empty strings MUST NOT be included and byte strings
4682 * MUST NOT be truncated."
4683 * We check lengths now rather than later.
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +02004684 */
4685 tot_len = 0;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004686 for (p = protos; *p != NULL; p++) {
4687 cur_len = strlen(*p);
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +02004688 tot_len += cur_len;
4689
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004690 if ((cur_len == 0) ||
4691 (cur_len > MBEDTLS_SSL_MAX_ALPN_NAME_LEN) ||
4692 (tot_len > MBEDTLS_SSL_MAX_ALPN_LIST_LEN)) {
4693 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
4694 }
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +02004695 }
4696
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02004697 conf->alpn_list = protos;
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +02004698
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004699 return 0;
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02004700}
4701
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004702const char *mbedtls_ssl_get_alpn_protocol(const mbedtls_ssl_context *ssl)
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02004703{
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004704 return ssl->alpn_chosen;
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02004705}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004706#endif /* MBEDTLS_SSL_ALPN */
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02004707
Johan Pascalb62bb512015-12-03 21:56:45 +01004708#if defined(MBEDTLS_SSL_DTLS_SRTP)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004709void mbedtls_ssl_conf_srtp_mki_value_supported(mbedtls_ssl_config *conf,
4710 int support_mki_value)
Ron Eldor591f1622018-01-22 12:30:04 +02004711{
4712 conf->dtls_srtp_mki_support = support_mki_value;
4713}
4714
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004715int mbedtls_ssl_dtls_srtp_set_mki_value(mbedtls_ssl_context *ssl,
4716 unsigned char *mki_value,
4717 uint16_t mki_len)
Ron Eldor591f1622018-01-22 12:30:04 +02004718{
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004719 if (mki_len > MBEDTLS_TLS_SRTP_MAX_MKI_LENGTH) {
4720 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
Ron Eldora9788042018-12-05 11:04:31 +02004721 }
Ron Eldor591f1622018-01-22 12:30:04 +02004722
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004723 if (ssl->conf->dtls_srtp_mki_support == MBEDTLS_SSL_DTLS_SRTP_MKI_UNSUPPORTED) {
4724 return MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
Ron Eldora9788042018-12-05 11:04:31 +02004725 }
Ron Eldor591f1622018-01-22 12:30:04 +02004726
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004727 memcpy(ssl->dtls_srtp_info.mki_value, mki_value, mki_len);
Ron Eldor591f1622018-01-22 12:30:04 +02004728 ssl->dtls_srtp_info.mki_len = mki_len;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004729 return 0;
Ron Eldor591f1622018-01-22 12:30:04 +02004730}
4731
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004732int mbedtls_ssl_conf_dtls_srtp_protection_profiles(mbedtls_ssl_config *conf,
4733 const mbedtls_ssl_srtp_profile *profiles)
Johan Pascalb62bb512015-12-03 21:56:45 +01004734{
Johan Pascal253d0262020-09-22 13:04:45 +02004735 const mbedtls_ssl_srtp_profile *p;
4736 size_t list_size = 0;
Johan Pascalb62bb512015-12-03 21:56:45 +01004737
Johan Pascal253d0262020-09-22 13:04:45 +02004738 /* check the profiles list: all entry must be valid,
4739 * its size cannot be more than the total number of supported profiles, currently 4 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004740 for (p = profiles; *p != MBEDTLS_TLS_SRTP_UNSET &&
4741 list_size <= MBEDTLS_TLS_SRTP_MAX_PROFILE_LIST_LENGTH;
4742 p++) {
4743 if (mbedtls_ssl_check_srtp_profile_value(*p) != MBEDTLS_TLS_SRTP_UNSET) {
Johan Pascal76fdf1d2020-10-22 23:31:00 +02004744 list_size++;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004745 } else {
Johan Pascal76fdf1d2020-10-22 23:31:00 +02004746 /* unsupported value, stop parsing and set the size to an error value */
4747 list_size = MBEDTLS_TLS_SRTP_MAX_PROFILE_LIST_LENGTH + 1;
Johan Pascalb62bb512015-12-03 21:56:45 +01004748 }
4749 }
4750
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004751 if (list_size > MBEDTLS_TLS_SRTP_MAX_PROFILE_LIST_LENGTH) {
4752 conf->dtls_srtp_profile_list = NULL;
4753 conf->dtls_srtp_profile_list_len = 0;
4754 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
Johan Pascal253d0262020-09-22 13:04:45 +02004755 }
4756
Johan Pascal9bc97ca2020-09-21 23:44:45 +02004757 conf->dtls_srtp_profile_list = profiles;
Johan Pascal253d0262020-09-22 13:04:45 +02004758 conf->dtls_srtp_profile_list_len = list_size;
Johan Pascalb62bb512015-12-03 21:56:45 +01004759
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004760 return 0;
Johan Pascalb62bb512015-12-03 21:56:45 +01004761}
4762
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004763void mbedtls_ssl_get_dtls_srtp_negotiation_result(const mbedtls_ssl_context *ssl,
4764 mbedtls_dtls_srtp_info *dtls_srtp_info)
Johan Pascalb62bb512015-12-03 21:56:45 +01004765{
Johan Pascal2258a4f2020-10-28 13:53:09 +01004766 dtls_srtp_info->chosen_dtls_srtp_profile = ssl->dtls_srtp_info.chosen_dtls_srtp_profile;
4767 /* do not copy the mki value if there is no chosen profile */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004768 if (dtls_srtp_info->chosen_dtls_srtp_profile == MBEDTLS_TLS_SRTP_UNSET) {
Johan Pascal2258a4f2020-10-28 13:53:09 +01004769 dtls_srtp_info->mki_len = 0;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004770 } else {
Johan Pascal2258a4f2020-10-28 13:53:09 +01004771 dtls_srtp_info->mki_len = ssl->dtls_srtp_info.mki_len;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004772 memcpy(dtls_srtp_info->mki_value, ssl->dtls_srtp_info.mki_value,
4773 ssl->dtls_srtp_info.mki_len);
Johan Pascal2258a4f2020-10-28 13:53:09 +01004774 }
Johan Pascalb62bb512015-12-03 21:56:45 +01004775}
Johan Pascalb62bb512015-12-03 21:56:45 +01004776#endif /* MBEDTLS_SSL_DTLS_SRTP */
4777
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004778void mbedtls_ssl_conf_max_version(mbedtls_ssl_config *conf, int major, int minor)
Paul Bakker490ecc82011-10-06 13:04:09 +00004779{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02004780 conf->max_major_ver = major;
4781 conf->max_minor_ver = minor;
Paul Bakker490ecc82011-10-06 13:04:09 +00004782}
4783
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004784void mbedtls_ssl_conf_min_version(mbedtls_ssl_config *conf, int major, int minor)
Paul Bakker1d29fb52012-09-28 13:28:45 +00004785{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02004786 conf->min_major_ver = major;
4787 conf->min_minor_ver = minor;
Paul Bakker1d29fb52012-09-28 13:28:45 +00004788}
4789
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004790#if defined(MBEDTLS_SSL_FALLBACK_SCSV) && defined(MBEDTLS_SSL_CLI_C)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004791void mbedtls_ssl_conf_fallback(mbedtls_ssl_config *conf, char fallback)
Manuel Pégourié-Gonnard1cbd39d2014-10-20 13:34:59 +02004792{
Manuel Pégourié-Gonnard684b0592015-05-06 09:27:31 +01004793 conf->fallback = fallback;
Manuel Pégourié-Gonnard1cbd39d2014-10-20 13:34:59 +02004794}
4795#endif
4796
Janos Follath088ce432017-04-10 12:42:31 +01004797#if defined(MBEDTLS_SSL_SRV_C)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004798void mbedtls_ssl_conf_cert_req_ca_list(mbedtls_ssl_config *conf,
4799 char cert_req_ca_list)
Janos Follath088ce432017-04-10 12:42:31 +01004800{
4801 conf->cert_req_ca_list = cert_req_ca_list;
4802}
4803#endif
4804
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004805#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004806void mbedtls_ssl_conf_encrypt_then_mac(mbedtls_ssl_config *conf, char etm)
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +01004807{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02004808 conf->encrypt_then_mac = etm;
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +01004809}
4810#endif
4811
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004812#if defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004813void mbedtls_ssl_conf_extended_master_secret(mbedtls_ssl_config *conf, char ems)
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +02004814{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02004815 conf->extended_ms = ems;
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +02004816}
4817#endif
4818
Manuel Pégourié-Gonnard66dc5552015-05-14 12:28:21 +02004819#if defined(MBEDTLS_ARC4_C)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004820void mbedtls_ssl_conf_arc4_support(mbedtls_ssl_config *conf, char arc4)
Manuel Pégourié-Gonnardbd47a582015-01-12 13:43:29 +01004821{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02004822 conf->arc4_disabled = arc4;
Manuel Pégourié-Gonnardbd47a582015-01-12 13:43:29 +01004823}
Manuel Pégourié-Gonnard66dc5552015-05-14 12:28:21 +02004824#endif
Manuel Pégourié-Gonnardbd47a582015-01-12 13:43:29 +01004825
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004826#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004827int mbedtls_ssl_conf_max_frag_len(mbedtls_ssl_config *conf, unsigned char mfl_code)
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02004828{
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004829 if (mfl_code >= MBEDTLS_SSL_MAX_FRAG_LEN_INVALID ||
4830 ssl_mfl_code_to_length(mfl_code) > MBEDTLS_TLS_EXT_ADV_CONTENT_LEN) {
4831 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02004832 }
4833
Manuel Pégourié-Gonnard6bf89d62015-05-05 17:01:57 +01004834 conf->mfl_code = mfl_code;
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02004835
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004836 return 0;
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02004837}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004838#endif /* MBEDTLS_SSL_MAX_FRAGMENT_LENGTH */
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02004839
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004840#if defined(MBEDTLS_SSL_TRUNCATED_HMAC)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004841void mbedtls_ssl_conf_truncated_hmac(mbedtls_ssl_config *conf, int truncate)
Manuel Pégourié-Gonnarde980a992013-07-19 11:08:52 +02004842{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02004843 conf->trunc_hmac = truncate;
Manuel Pégourié-Gonnarde980a992013-07-19 11:08:52 +02004844}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004845#endif /* MBEDTLS_SSL_TRUNCATED_HMAC */
Manuel Pégourié-Gonnarde980a992013-07-19 11:08:52 +02004846
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004847#if defined(MBEDTLS_SSL_CBC_RECORD_SPLITTING)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004848void mbedtls_ssl_conf_cbc_record_splitting(mbedtls_ssl_config *conf, char split)
Manuel Pégourié-Gonnardcfa477e2015-01-07 14:50:54 +01004849{
Manuel Pégourié-Gonnard17eab2b2015-05-05 16:34:53 +01004850 conf->cbc_record_splitting = split;
Manuel Pégourié-Gonnardcfa477e2015-01-07 14:50:54 +01004851}
4852#endif
4853
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004854void mbedtls_ssl_conf_legacy_renegotiation(mbedtls_ssl_config *conf, int allow_legacy)
Paul Bakker48916f92012-09-16 19:57:18 +00004855{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02004856 conf->allow_legacy_renegotiation = allow_legacy;
Paul Bakker48916f92012-09-16 19:57:18 +00004857}
4858
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004859#if defined(MBEDTLS_SSL_RENEGOTIATION)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004860void mbedtls_ssl_conf_renegotiation(mbedtls_ssl_config *conf, int renegotiation)
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01004861{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02004862 conf->disable_renegotiation = renegotiation;
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01004863}
4864
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004865void mbedtls_ssl_conf_renegotiation_enforced(mbedtls_ssl_config *conf, int max_records)
Manuel Pégourié-Gonnarda9964db2014-07-03 19:29:16 +02004866{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02004867 conf->renego_max_records = max_records;
Manuel Pégourié-Gonnarda9964db2014-07-03 19:29:16 +02004868}
4869
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004870void mbedtls_ssl_conf_renegotiation_period(mbedtls_ssl_config *conf,
4871 const unsigned char period[8])
Manuel Pégourié-Gonnard837f0fe2014-11-05 13:58:53 +01004872{
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004873 memcpy(conf->renego_period, period, 8);
Manuel Pégourié-Gonnard837f0fe2014-11-05 13:58:53 +01004874}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004875#endif /* MBEDTLS_SSL_RENEGOTIATION */
Paul Bakker5121ce52009-01-03 21:22:43 +00004876
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004877#if defined(MBEDTLS_SSL_SESSION_TICKETS)
Manuel Pégourié-Gonnardb596abf2015-05-20 10:45:29 +02004878#if defined(MBEDTLS_SSL_CLI_C)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004879void mbedtls_ssl_conf_session_tickets(mbedtls_ssl_config *conf, int use_tickets)
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +02004880{
Manuel Pégourié-Gonnard2b494452015-05-06 10:05:11 +01004881 conf->session_tickets = use_tickets;
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +02004882}
Manuel Pégourié-Gonnardb596abf2015-05-20 10:45:29 +02004883#endif
Paul Bakker606b4ba2013-08-14 16:52:14 +02004884
Manuel Pégourié-Gonnardb596abf2015-05-20 10:45:29 +02004885#if defined(MBEDTLS_SSL_SRV_C)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004886void mbedtls_ssl_conf_session_tickets_cb(mbedtls_ssl_config *conf,
4887 mbedtls_ssl_ticket_write_t *f_ticket_write,
4888 mbedtls_ssl_ticket_parse_t *f_ticket_parse,
4889 void *p_ticket)
Paul Bakker606b4ba2013-08-14 16:52:14 +02004890{
Manuel Pégourié-Gonnardd59675d2015-05-19 15:28:00 +02004891 conf->f_ticket_write = f_ticket_write;
4892 conf->f_ticket_parse = f_ticket_parse;
4893 conf->p_ticket = p_ticket;
Paul Bakker606b4ba2013-08-14 16:52:14 +02004894}
Manuel Pégourié-Gonnardb596abf2015-05-20 10:45:29 +02004895#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004896#endif /* MBEDTLS_SSL_SESSION_TICKETS */
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +02004897
Robert Cragie4feb7ae2015-10-02 13:33:37 +01004898#if defined(MBEDTLS_SSL_EXPORT_KEYS)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004899void mbedtls_ssl_conf_export_keys_cb(mbedtls_ssl_config *conf,
4900 mbedtls_ssl_export_keys_t *f_export_keys,
4901 void *p_export_keys)
Robert Cragie4feb7ae2015-10-02 13:33:37 +01004902{
4903 conf->f_export_keys = f_export_keys;
4904 conf->p_export_keys = p_export_keys;
4905}
Ron Eldorf5cc10d2019-05-07 18:33:40 +03004906
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004907void mbedtls_ssl_conf_export_keys_ext_cb(mbedtls_ssl_config *conf,
4908 mbedtls_ssl_export_keys_ext_t *f_export_keys_ext,
4909 void *p_export_keys)
Ron Eldorf5cc10d2019-05-07 18:33:40 +03004910{
4911 conf->f_export_keys_ext = f_export_keys_ext;
4912 conf->p_export_keys = p_export_keys;
4913}
Robert Cragie4feb7ae2015-10-02 13:33:37 +01004914#endif
4915
Gilles Peskineb74a1c72018-04-24 13:09:22 +02004916#if defined(MBEDTLS_SSL_ASYNC_PRIVATE)
Gilles Peskine8bf79f62018-01-05 21:11:53 +01004917void mbedtls_ssl_conf_async_private_cb(
4918 mbedtls_ssl_config *conf,
4919 mbedtls_ssl_async_sign_t *f_async_sign,
4920 mbedtls_ssl_async_decrypt_t *f_async_decrypt,
4921 mbedtls_ssl_async_resume_t *f_async_resume,
4922 mbedtls_ssl_async_cancel_t *f_async_cancel,
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004923 void *async_config_data)
Gilles Peskine8bf79f62018-01-05 21:11:53 +01004924{
4925 conf->f_async_sign_start = f_async_sign;
4926 conf->f_async_decrypt_start = f_async_decrypt;
4927 conf->f_async_resume = f_async_resume;
4928 conf->f_async_cancel = f_async_cancel;
Gilles Peskinedf13d5c2018-04-25 20:39:48 +02004929 conf->p_async_config_data = async_config_data;
4930}
4931
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004932void *mbedtls_ssl_conf_get_async_config_data(const mbedtls_ssl_config *conf)
Gilles Peskine8f97af72018-04-26 11:46:10 +02004933{
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004934 return conf->p_async_config_data;
Gilles Peskine8f97af72018-04-26 11:46:10 +02004935}
4936
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004937void *mbedtls_ssl_get_async_operation_data(const mbedtls_ssl_context *ssl)
Gilles Peskinedf13d5c2018-04-25 20:39:48 +02004938{
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004939 if (ssl->handshake == NULL) {
4940 return NULL;
4941 } else {
4942 return ssl->handshake->user_async_ctx;
4943 }
Gilles Peskinedf13d5c2018-04-25 20:39:48 +02004944}
4945
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004946void mbedtls_ssl_set_async_operation_data(mbedtls_ssl_context *ssl,
4947 void *ctx)
Gilles Peskinedf13d5c2018-04-25 20:39:48 +02004948{
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004949 if (ssl->handshake != NULL) {
Gilles Peskinedf13d5c2018-04-25 20:39:48 +02004950 ssl->handshake->user_async_ctx = ctx;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004951 }
Gilles Peskine8bf79f62018-01-05 21:11:53 +01004952}
Gilles Peskineb74a1c72018-04-24 13:09:22 +02004953#endif /* MBEDTLS_SSL_ASYNC_PRIVATE */
Gilles Peskine8bf79f62018-01-05 21:11:53 +01004954
Paul Bakker5121ce52009-01-03 21:22:43 +00004955/*
4956 * SSL get accessors
4957 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004958uint32_t mbedtls_ssl_get_verify_result(const mbedtls_ssl_context *ssl)
Paul Bakker5121ce52009-01-03 21:22:43 +00004959{
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004960 if (ssl->session != NULL) {
4961 return ssl->session->verify_result;
4962 }
Manuel Pégourié-Gonnarde89163c2015-01-23 14:30:57 +00004963
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004964 if (ssl->session_negotiate != NULL) {
4965 return ssl->session_negotiate->verify_result;
4966 }
Manuel Pégourié-Gonnarde89163c2015-01-23 14:30:57 +00004967
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004968 return 0xFFFFFFFF;
Paul Bakker5121ce52009-01-03 21:22:43 +00004969}
4970
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004971const char *mbedtls_ssl_get_ciphersuite(const mbedtls_ssl_context *ssl)
Paul Bakker72f62662011-01-16 21:27:44 +00004972{
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004973 if (ssl == NULL || ssl->session == NULL) {
4974 return NULL;
4975 }
Paul Bakker926c8e42013-03-06 10:23:34 +01004976
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004977 return mbedtls_ssl_get_ciphersuite_name(ssl->session->ciphersuite);
Paul Bakker72f62662011-01-16 21:27:44 +00004978}
4979
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004980const char *mbedtls_ssl_get_version(const mbedtls_ssl_context *ssl)
Paul Bakker43ca69c2011-01-15 17:35:19 +00004981{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004982#if defined(MBEDTLS_SSL_PROTO_DTLS)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004983 if (ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM) {
4984 switch (ssl->minor_ver) {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004985 case MBEDTLS_SSL_MINOR_VERSION_2:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004986 return "DTLSv1.0";
Manuel Pégourié-Gonnardb21ca2a2014-02-10 13:43:33 +01004987
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004988 case MBEDTLS_SSL_MINOR_VERSION_3:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004989 return "DTLSv1.2";
Manuel Pégourié-Gonnardb21ca2a2014-02-10 13:43:33 +01004990
4991 default:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004992 return "unknown (DTLS)";
Manuel Pégourié-Gonnardb21ca2a2014-02-10 13:43:33 +01004993 }
4994 }
4995#endif
4996
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004997 switch (ssl->minor_ver) {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004998 case MBEDTLS_SSL_MINOR_VERSION_0:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004999 return "SSLv3.0";
Paul Bakker43ca69c2011-01-15 17:35:19 +00005000
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005001 case MBEDTLS_SSL_MINOR_VERSION_1:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005002 return "TLSv1.0";
Paul Bakker43ca69c2011-01-15 17:35:19 +00005003
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005004 case MBEDTLS_SSL_MINOR_VERSION_2:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005005 return "TLSv1.1";
Paul Bakker43ca69c2011-01-15 17:35:19 +00005006
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005007 case MBEDTLS_SSL_MINOR_VERSION_3:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005008 return "TLSv1.2";
Paul Bakker1ef83d62012-04-11 12:09:53 +00005009
Paul Bakker43ca69c2011-01-15 17:35:19 +00005010 default:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005011 return "unknown";
Paul Bakker43ca69c2011-01-15 17:35:19 +00005012 }
Paul Bakker43ca69c2011-01-15 17:35:19 +00005013}
5014
Manuel Pégourié-Gonnarda2cda6b2015-08-31 18:30:52 +02005015#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005016size_t mbedtls_ssl_get_input_max_frag_len(const mbedtls_ssl_context *ssl)
Andrzej Kurek90c6e842020-04-03 05:25:29 -04005017{
5018 size_t max_len = MBEDTLS_SSL_MAX_CONTENT_LEN;
5019 size_t read_mfl;
5020
5021 /* Use the configured MFL for the client if we're past SERVER_HELLO_DONE */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005022 if (ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT &&
5023 ssl->state >= MBEDTLS_SSL_SERVER_HELLO_DONE) {
5024 return ssl_mfl_code_to_length(ssl->conf->mfl_code);
Andrzej Kurek90c6e842020-04-03 05:25:29 -04005025 }
5026
5027 /* Check if a smaller max length was negotiated */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005028 if (ssl->session_out != NULL) {
5029 read_mfl = ssl_mfl_code_to_length(ssl->session_out->mfl_code);
5030 if (read_mfl < max_len) {
Andrzej Kurek90c6e842020-04-03 05:25:29 -04005031 max_len = read_mfl;
5032 }
5033 }
5034
5035 // During a handshake, use the value being negotiated
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005036 if (ssl->session_negotiate != NULL) {
5037 read_mfl = ssl_mfl_code_to_length(ssl->session_negotiate->mfl_code);
5038 if (read_mfl < max_len) {
Andrzej Kurek90c6e842020-04-03 05:25:29 -04005039 max_len = read_mfl;
5040 }
5041 }
5042
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005043 return max_len;
Andrzej Kurek90c6e842020-04-03 05:25:29 -04005044}
5045
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005046size_t mbedtls_ssl_get_output_max_frag_len(const mbedtls_ssl_context *ssl)
Manuel Pégourié-Gonnarda2cda6b2015-08-31 18:30:52 +02005047{
5048 size_t max_len;
5049
5050 /*
5051 * Assume mfl_code is correct since it was checked when set
5052 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005053 max_len = ssl_mfl_code_to_length(ssl->conf->mfl_code);
Manuel Pégourié-Gonnarda2cda6b2015-08-31 18:30:52 +02005054
Manuel Pégourié-Gonnard2cb17e22017-09-19 13:00:47 +02005055 /* Check if a smaller max length was negotiated */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005056 if (ssl->session_out != NULL &&
5057 ssl_mfl_code_to_length(ssl->session_out->mfl_code) < max_len) {
5058 max_len = ssl_mfl_code_to_length(ssl->session_out->mfl_code);
Manuel Pégourié-Gonnarda2cda6b2015-08-31 18:30:52 +02005059 }
5060
Manuel Pégourié-Gonnard2cb17e22017-09-19 13:00:47 +02005061 /* During a handshake, use the value being negotiated */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005062 if (ssl->session_negotiate != NULL &&
5063 ssl_mfl_code_to_length(ssl->session_negotiate->mfl_code) < max_len) {
5064 max_len = ssl_mfl_code_to_length(ssl->session_negotiate->mfl_code);
Manuel Pégourié-Gonnard2cb17e22017-09-19 13:00:47 +02005065 }
5066
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005067 return max_len;
Manuel Pégourié-Gonnarda2cda6b2015-08-31 18:30:52 +02005068}
Andrzej Kurek90c6e842020-04-03 05:25:29 -04005069
5070#if !defined(MBEDTLS_DEPRECATED_REMOVED)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005071size_t mbedtls_ssl_get_max_frag_len(const mbedtls_ssl_context *ssl)
Andrzej Kurek90c6e842020-04-03 05:25:29 -04005072{
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005073 return mbedtls_ssl_get_output_max_frag_len(ssl);
Andrzej Kurek90c6e842020-04-03 05:25:29 -04005074}
5075#endif /* !MBEDTLS_DEPRECATED_REMOVED */
Manuel Pégourié-Gonnarda2cda6b2015-08-31 18:30:52 +02005076#endif /* MBEDTLS_SSL_MAX_FRAGMENT_LENGTH */
5077
Manuel Pégourié-Gonnardb8eec192018-08-20 09:34:02 +02005078#if defined(MBEDTLS_SSL_PROTO_DTLS)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005079size_t mbedtls_ssl_get_current_mtu(const mbedtls_ssl_context *ssl)
Manuel Pégourié-Gonnardb8eec192018-08-20 09:34:02 +02005080{
Andrzej Kurekef43ce62018-10-09 08:24:12 -04005081 /* Return unlimited mtu for client hello messages to avoid fragmentation. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005082 if (ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT &&
5083 (ssl->state == MBEDTLS_SSL_CLIENT_HELLO ||
5084 ssl->state == MBEDTLS_SSL_SERVER_HELLO)) {
5085 return 0;
5086 }
Andrzej Kurekef43ce62018-10-09 08:24:12 -04005087
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005088 if (ssl->handshake == NULL || ssl->handshake->mtu == 0) {
5089 return ssl->mtu;
5090 }
Manuel Pégourié-Gonnardb8eec192018-08-20 09:34:02 +02005091
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005092 if (ssl->mtu == 0) {
5093 return ssl->handshake->mtu;
5094 }
Manuel Pégourié-Gonnardb8eec192018-08-20 09:34:02 +02005095
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005096 return ssl->mtu < ssl->handshake->mtu ?
5097 ssl->mtu : ssl->handshake->mtu;
Manuel Pégourié-Gonnardb8eec192018-08-20 09:34:02 +02005098}
5099#endif /* MBEDTLS_SSL_PROTO_DTLS */
5100
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005101int mbedtls_ssl_get_max_out_record_payload(const mbedtls_ssl_context *ssl)
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02005102{
5103 size_t max_len = MBEDTLS_SSL_OUT_CONTENT_LEN;
5104
Manuel Pégourié-Gonnard000281e2018-08-21 11:20:58 +02005105#if !defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH) && \
5106 !defined(MBEDTLS_SSL_PROTO_DTLS)
5107 (void) ssl;
5108#endif
5109
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02005110#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005111 const size_t mfl = mbedtls_ssl_get_output_max_frag_len(ssl);
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02005112
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005113 if (max_len > mfl) {
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02005114 max_len = mfl;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005115 }
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02005116#endif
5117
5118#if defined(MBEDTLS_SSL_PROTO_DTLS)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005119 if (mbedtls_ssl_get_current_mtu(ssl) != 0) {
5120 const size_t mtu = mbedtls_ssl_get_current_mtu(ssl);
5121 const int ret = mbedtls_ssl_get_record_expansion(ssl);
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02005122 const size_t overhead = (size_t) ret;
5123
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005124 if (ret < 0) {
5125 return ret;
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02005126 }
5127
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005128 if (mtu <= overhead) {
5129 MBEDTLS_SSL_DEBUG_MSG(1, ("MTU too low for record expansion"));
5130 return MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
5131 }
5132
5133 if (max_len > mtu - overhead) {
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02005134 max_len = mtu - overhead;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005135 }
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02005136 }
Manuel Pégourié-Gonnardb8eec192018-08-20 09:34:02 +02005137#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02005138
Hanno Becker0defedb2018-08-10 12:35:02 +01005139#if !defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH) && \
5140 !defined(MBEDTLS_SSL_PROTO_DTLS)
5141 ((void) ssl);
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02005142#endif
5143
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005144 return (int) max_len;
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02005145}
5146
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005147#if defined(MBEDTLS_X509_CRT_PARSE_C)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005148const mbedtls_x509_crt *mbedtls_ssl_get_peer_cert(const mbedtls_ssl_context *ssl)
Paul Bakkerb0550d92012-10-30 07:51:03 +00005149{
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005150 if (ssl == NULL || ssl->session == NULL) {
5151 return NULL;
5152 }
Paul Bakkerb0550d92012-10-30 07:51:03 +00005153
Hanno Beckere6824572019-02-07 13:18:46 +00005154#if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005155 return ssl->session->peer_cert;
Hanno Beckere6824572019-02-07 13:18:46 +00005156#else
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005157 return NULL;
Hanno Beckere6824572019-02-07 13:18:46 +00005158#endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
Paul Bakkerb0550d92012-10-30 07:51:03 +00005159}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005160#endif /* MBEDTLS_X509_CRT_PARSE_C */
Paul Bakkerb0550d92012-10-30 07:51:03 +00005161
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005162#if defined(MBEDTLS_SSL_CLI_C)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005163int mbedtls_ssl_get_session(const mbedtls_ssl_context *ssl,
5164 mbedtls_ssl_session *dst)
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02005165{
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005166 if (ssl == NULL ||
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02005167 dst == NULL ||
5168 ssl->session == NULL ||
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005169 ssl->conf->endpoint != MBEDTLS_SSL_IS_CLIENT) {
5170 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02005171 }
5172
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005173 return mbedtls_ssl_session_copy(dst, ssl->session);
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02005174}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005175#endif /* MBEDTLS_SSL_CLI_C */
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02005176
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005177const mbedtls_ssl_session *mbedtls_ssl_get_session_pointer(const mbedtls_ssl_context *ssl)
Manuel Pégourié-Gonnardb5e4e0a2019-05-20 11:12:28 +02005178{
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005179 if (ssl == NULL) {
5180 return NULL;
5181 }
Manuel Pégourié-Gonnardb5e4e0a2019-05-20 11:12:28 +02005182
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005183 return ssl->session;
Manuel Pégourié-Gonnardb5e4e0a2019-05-20 11:12:28 +02005184}
5185
Paul Bakker5121ce52009-01-03 21:22:43 +00005186/*
Hanno Beckera835da52019-05-16 12:39:07 +01005187 * Define ticket header determining Mbed TLS version
5188 * and structure of the ticket.
5189 */
5190
Hanno Becker94ef3b32019-05-16 12:50:45 +01005191/*
Hanno Becker50b59662019-05-28 14:30:45 +01005192 * Define bitflag determining compile-time settings influencing
5193 * structure of serialized SSL sessions.
Hanno Becker94ef3b32019-05-16 12:50:45 +01005194 */
5195
Hanno Becker50b59662019-05-28 14:30:45 +01005196#if defined(MBEDTLS_HAVE_TIME)
Hanno Becker3e088662019-05-29 11:10:18 +01005197#define SSL_SERIALIZED_SESSION_CONFIG_TIME 1
Hanno Becker50b59662019-05-28 14:30:45 +01005198#else
Hanno Becker3e088662019-05-29 11:10:18 +01005199#define SSL_SERIALIZED_SESSION_CONFIG_TIME 0
Hanno Becker94ef3b32019-05-16 12:50:45 +01005200#endif /* MBEDTLS_HAVE_TIME */
5201
5202#if defined(MBEDTLS_X509_CRT_PARSE_C)
Hanno Becker3e088662019-05-29 11:10:18 +01005203#define SSL_SERIALIZED_SESSION_CONFIG_CRT 1
Hanno Becker94ef3b32019-05-16 12:50:45 +01005204#else
Hanno Becker3e088662019-05-29 11:10:18 +01005205#define SSL_SERIALIZED_SESSION_CONFIG_CRT 0
Hanno Becker94ef3b32019-05-16 12:50:45 +01005206#endif /* MBEDTLS_X509_CRT_PARSE_C */
5207
David Horstmanneb77b6f2024-02-13 17:53:35 +00005208#if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
David Horstmann11def972024-03-01 11:20:32 +00005209#define SSL_SERIALIZED_SESSION_CONFIG_KEEP_PEER_CRT 1
David Horstmanneb77b6f2024-02-13 17:53:35 +00005210#else
David Horstmann11def972024-03-01 11:20:32 +00005211#define SSL_SERIALIZED_SESSION_CONFIG_KEEP_PEER_CRT 0
David Horstmanneb77b6f2024-02-13 17:53:35 +00005212#endif /* MBEDTLS_SSL_SESSION_TICKETS */
5213
Hanno Becker94ef3b32019-05-16 12:50:45 +01005214#if defined(MBEDTLS_SSL_CLI_C) && defined(MBEDTLS_SSL_SESSION_TICKETS)
Hanno Becker3e088662019-05-29 11:10:18 +01005215#define SSL_SERIALIZED_SESSION_CONFIG_CLIENT_TICKET 1
Hanno Becker94ef3b32019-05-16 12:50:45 +01005216#else
Hanno Becker3e088662019-05-29 11:10:18 +01005217#define SSL_SERIALIZED_SESSION_CONFIG_CLIENT_TICKET 0
Hanno Becker94ef3b32019-05-16 12:50:45 +01005218#endif /* MBEDTLS_SSL_CLI_C && MBEDTLS_SSL_SESSION_TICKETS */
5219
5220#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
Hanno Becker3e088662019-05-29 11:10:18 +01005221#define SSL_SERIALIZED_SESSION_CONFIG_MFL 1
Hanno Becker94ef3b32019-05-16 12:50:45 +01005222#else
Hanno Becker3e088662019-05-29 11:10:18 +01005223#define SSL_SERIALIZED_SESSION_CONFIG_MFL 0
Hanno Becker94ef3b32019-05-16 12:50:45 +01005224#endif /* MBEDTLS_SSL_MAX_FRAGMENT_LENGTH */
5225
5226#if defined(MBEDTLS_SSL_TRUNCATED_HMAC)
Hanno Becker3e088662019-05-29 11:10:18 +01005227#define SSL_SERIALIZED_SESSION_CONFIG_TRUNC_HMAC 1
Hanno Becker94ef3b32019-05-16 12:50:45 +01005228#else
Hanno Becker3e088662019-05-29 11:10:18 +01005229#define SSL_SERIALIZED_SESSION_CONFIG_TRUNC_HMAC 0
Hanno Becker94ef3b32019-05-16 12:50:45 +01005230#endif /* MBEDTLS_SSL_TRUNCATED_HMAC */
5231
5232#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
Hanno Becker3e088662019-05-29 11:10:18 +01005233#define SSL_SERIALIZED_SESSION_CONFIG_ETM 1
Hanno Becker94ef3b32019-05-16 12:50:45 +01005234#else
Hanno Becker3e088662019-05-29 11:10:18 +01005235#define SSL_SERIALIZED_SESSION_CONFIG_ETM 0
Hanno Becker94ef3b32019-05-16 12:50:45 +01005236#endif /* MBEDTLS_SSL_ENCRYPT_THEN_MAC */
5237
Hanno Becker94ef3b32019-05-16 12:50:45 +01005238#if defined(MBEDTLS_SSL_SESSION_TICKETS)
5239#define SSL_SERIALIZED_SESSION_CONFIG_TICKET 1
5240#else
5241#define SSL_SERIALIZED_SESSION_CONFIG_TICKET 0
5242#endif /* MBEDTLS_SSL_SESSION_TICKETS */
5243
Hanno Becker3e088662019-05-29 11:10:18 +01005244#define SSL_SERIALIZED_SESSION_CONFIG_TIME_BIT 0
5245#define SSL_SERIALIZED_SESSION_CONFIG_CRT_BIT 1
5246#define SSL_SERIALIZED_SESSION_CONFIG_CLIENT_TICKET_BIT 2
5247#define SSL_SERIALIZED_SESSION_CONFIG_MFL_BIT 3
5248#define SSL_SERIALIZED_SESSION_CONFIG_TRUNC_HMAC_BIT 4
5249#define SSL_SERIALIZED_SESSION_CONFIG_ETM_BIT 5
5250#define SSL_SERIALIZED_SESSION_CONFIG_TICKET_BIT 6
David Horstmann11def972024-03-01 11:20:32 +00005251#define SSL_SERIALIZED_SESSION_CONFIG_KEEP_PEER_CRT_BIT 7
Hanno Becker3e088662019-05-29 11:10:18 +01005252
Hanno Becker50b59662019-05-28 14:30:45 +01005253#define SSL_SERIALIZED_SESSION_CONFIG_BITFLAG \
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005254 ((uint16_t) ( \
5255 (SSL_SERIALIZED_SESSION_CONFIG_TIME << SSL_SERIALIZED_SESSION_CONFIG_TIME_BIT) | \
5256 (SSL_SERIALIZED_SESSION_CONFIG_CRT << SSL_SERIALIZED_SESSION_CONFIG_CRT_BIT) | \
5257 (SSL_SERIALIZED_SESSION_CONFIG_CLIENT_TICKET << \
5258 SSL_SERIALIZED_SESSION_CONFIG_CLIENT_TICKET_BIT) | \
5259 (SSL_SERIALIZED_SESSION_CONFIG_MFL << SSL_SERIALIZED_SESSION_CONFIG_MFL_BIT) | \
5260 (SSL_SERIALIZED_SESSION_CONFIG_TRUNC_HMAC << \
5261 SSL_SERIALIZED_SESSION_CONFIG_TRUNC_HMAC_BIT) | \
5262 (SSL_SERIALIZED_SESSION_CONFIG_ETM << SSL_SERIALIZED_SESSION_CONFIG_ETM_BIT) | \
David Horstmanneb77b6f2024-02-13 17:53:35 +00005263 (SSL_SERIALIZED_SESSION_CONFIG_TICKET << SSL_SERIALIZED_SESSION_CONFIG_TICKET_BIT) | \
David Horstmannf5a6fa22024-03-01 12:31:35 +00005264 (SSL_SERIALIZED_SESSION_CONFIG_KEEP_PEER_CRT << \
5265 SSL_SERIALIZED_SESSION_CONFIG_KEEP_PEER_CRT_BIT)))
Hanno Becker94ef3b32019-05-16 12:50:45 +01005266
Chien Wongb6d57932024-02-07 21:48:12 +08005267static const unsigned char ssl_serialized_session_header[] = {
Hanno Becker94ef3b32019-05-16 12:50:45 +01005268 MBEDTLS_VERSION_MAJOR,
5269 MBEDTLS_VERSION_MINOR,
5270 MBEDTLS_VERSION_PATCH,
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005271 MBEDTLS_BYTE_1(SSL_SERIALIZED_SESSION_CONFIG_BITFLAG),
5272 MBEDTLS_BYTE_0(SSL_SERIALIZED_SESSION_CONFIG_BITFLAG),
Hanno Beckerf8787072019-05-16 12:41:07 +01005273};
Hanno Beckera835da52019-05-16 12:39:07 +01005274
5275/*
Manuel Pégourié-Gonnarda3e7c652019-05-16 10:08:35 +02005276 * Serialize a session in the following format:
Manuel Pégourié-Gonnard35eb8022019-05-16 11:11:08 +02005277 * (in the presentation language of TLS, RFC 8446 section 3)
Manuel Pégourié-Gonnarda3e7c652019-05-16 10:08:35 +02005278 *
Hanno Becker50b59662019-05-28 14:30:45 +01005279 * opaque mbedtls_version[3]; // major, minor, patch
5280 * opaque session_format[2]; // version-specific 16-bit field determining
5281 * // the format of the remaining
5282 * // serialized data.
Hanno Beckerdc28b6c2019-05-29 11:08:00 +01005283 *
5284 * Note: When updating the format, remember to keep
5285 * these version+format bytes.
5286 *
Hanno Beckerbe34e8e2019-06-04 09:43:16 +01005287 * // In this version, `session_format` determines
5288 * // the setting of those compile-time
5289 * // configuration options which influence
Hanno Becker50b59662019-05-28 14:30:45 +01005290 * // the structure of mbedtls_ssl_session.
David Horstmann363db772024-03-01 12:11:24 +00005291 * #if defined(MBEDTLS_HAVE_TIME)
5292 * uint64 start_time;
5293 * #endif
5294 * uint8 ciphersuite[2]; // defined by the standard
5295 * uint8 compression; // 0 or 1
5296 * uint8 session_id_len; // at most 32
5297 * opaque session_id[32];
5298 * opaque master[48]; // fixed length in the standard
5299 * uint32 verify_result;
David Horstmannfc8cacf2024-03-04 16:37:02 +00005300 * #if defined(MBEDTLS_X509_CRT_PARSE_C)
David Horstmann363db772024-03-01 12:11:24 +00005301 * #if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
5302 * opaque peer_cert<0..2^24-1>; // length 0 means no peer cert
5303 * #else
5304 * uint8 peer_cert_digest_type;
5305 * opaque peer_cert_digest<0..2^8-1>
5306 * #endif
David Horstmannfc8cacf2024-03-04 16:37:02 +00005307 * #endif
5308 * #if defined(MBEDTLS_SSL_SESSION_TICKETS) && defined(MBEDTLS_SSL_CLI_C)
David Horstmann363db772024-03-01 12:11:24 +00005309 * opaque ticket<0..2^24-1>; // length 0 means no ticket
5310 * uint32 ticket_lifetime;
5311 * #endif
5312 * #if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
5313 * uint8 mfl_code; // up to 255 according to standard
5314 * #endif
5315 * #if defined(MBEDTLS_SSL_TRUNCATED_HMAC)
5316 * uint8 trunc_hmac; // 0 or 1
5317 * #endif
5318 * #if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
5319 * uint8 encrypt_then_mac; // 0 or 1
5320 * #endif
Manuel Pégourié-Gonnarda3e7c652019-05-16 10:08:35 +02005321 *
Manuel Pégourié-Gonnardf743c032019-05-24 12:06:29 +02005322 * The order is the same as in the definition of the structure, except
5323 * verify_result is put before peer_cert so that all mandatory fields come
5324 * together in one block.
Manuel Pégourié-Gonnarda3e7c652019-05-16 10:08:35 +02005325 */
Manuel Pégourié-Gonnardd904d662022-06-17 10:24:00 +02005326MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005327static int ssl_session_save(const mbedtls_ssl_session *session,
5328 unsigned char omit_header,
5329 unsigned char *buf,
5330 size_t buf_len,
5331 size_t *olen)
Manuel Pégourié-Gonnarda3e7c652019-05-16 10:08:35 +02005332{
5333 unsigned char *p = buf;
Manuel Pégourié-Gonnard26f982f2019-05-21 11:01:32 +02005334 size_t used = 0;
Manuel Pégourié-Gonnardf743c032019-05-24 12:06:29 +02005335#if defined(MBEDTLS_HAVE_TIME)
5336 uint64_t start;
5337#endif
Manuel Pégourié-Gonnarda3e7c652019-05-16 10:08:35 +02005338#if defined(MBEDTLS_X509_CRT_PARSE_C)
5339#if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
5340 size_t cert_len;
Manuel Pégourié-Gonnarda3e7c652019-05-16 10:08:35 +02005341#endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
5342#endif /* MBEDTLS_X509_CRT_PARSE_C */
5343
Manuel Pégourié-Gonnarda3e7c652019-05-16 10:08:35 +02005344
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005345 if (!omit_header) {
Manuel Pégourié-Gonnard45ac1f02019-07-23 16:52:45 +02005346 /*
5347 * Add version identifier
5348 */
5349
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005350 used += sizeof(ssl_serialized_session_header);
Manuel Pégourié-Gonnard45ac1f02019-07-23 16:52:45 +02005351
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005352 if (used <= buf_len) {
5353 memcpy(p, ssl_serialized_session_header,
5354 sizeof(ssl_serialized_session_header));
5355 p += sizeof(ssl_serialized_session_header);
Manuel Pégourié-Gonnard45ac1f02019-07-23 16:52:45 +02005356 }
Hanno Beckera835da52019-05-16 12:39:07 +01005357 }
5358
5359 /*
Manuel Pégourié-Gonnardf743c032019-05-24 12:06:29 +02005360 * Time
5361 */
5362#if defined(MBEDTLS_HAVE_TIME)
5363 used += 8;
5364
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005365 if (used <= buf_len) {
Manuel Pégourié-Gonnardf743c032019-05-24 12:06:29 +02005366 start = (uint64_t) session->start;
5367
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005368 MBEDTLS_PUT_UINT64_BE(start, p, 0);
Joe Subbiani2f98d792021-08-20 11:44:44 +01005369 p += 8;
Manuel Pégourié-Gonnardf743c032019-05-24 12:06:29 +02005370 }
5371#endif /* MBEDTLS_HAVE_TIME */
5372
5373 /*
5374 * Basic mandatory fields
5375 */
5376 used += 2 /* ciphersuite */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005377 + 1 /* compression */
5378 + 1 /* id_len */
5379 + sizeof(session->id)
5380 + sizeof(session->master)
5381 + 4; /* verify_result */
Manuel Pégourié-Gonnardf743c032019-05-24 12:06:29 +02005382
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005383 if (used <= buf_len) {
5384 MBEDTLS_PUT_UINT16_BE(session->ciphersuite, p, 0);
Joe Subbiani2f98d792021-08-20 11:44:44 +01005385 p += 2;
Manuel Pégourié-Gonnardf743c032019-05-24 12:06:29 +02005386
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005387 *p++ = MBEDTLS_BYTE_0(session->compression);
Manuel Pégourié-Gonnardf743c032019-05-24 12:06:29 +02005388
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005389 *p++ = MBEDTLS_BYTE_0(session->id_len);
5390 memcpy(p, session->id, 32);
Manuel Pégourié-Gonnardf743c032019-05-24 12:06:29 +02005391 p += 32;
5392
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005393 memcpy(p, session->master, 48);
Manuel Pégourié-Gonnardf743c032019-05-24 12:06:29 +02005394 p += 48;
5395
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005396 MBEDTLS_PUT_UINT32_BE(session->verify_result, p, 0);
Joe Subbiani2f98d792021-08-20 11:44:44 +01005397 p += 4;
Manuel Pégourié-Gonnard26f982f2019-05-21 11:01:32 +02005398 }
Manuel Pégourié-Gonnarda3e7c652019-05-16 10:08:35 +02005399
Manuel Pégourié-Gonnard35eb8022019-05-16 11:11:08 +02005400 /*
Manuel Pégourié-Gonnardf743c032019-05-24 12:06:29 +02005401 * Peer's end-entity certificate
Manuel Pégourié-Gonnard35eb8022019-05-16 11:11:08 +02005402 */
Manuel Pégourié-Gonnarda3e7c652019-05-16 10:08:35 +02005403#if defined(MBEDTLS_X509_CRT_PARSE_C)
5404#if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005405 if (session->peer_cert == NULL) {
Manuel Pégourié-Gonnarda3e7c652019-05-16 10:08:35 +02005406 cert_len = 0;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005407 } else {
Manuel Pégourié-Gonnarda3e7c652019-05-16 10:08:35 +02005408 cert_len = session->peer_cert->raw.len;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005409 }
Manuel Pégourié-Gonnarda3e7c652019-05-16 10:08:35 +02005410
Manuel Pégourié-Gonnard26f982f2019-05-21 11:01:32 +02005411 used += 3 + cert_len;
Manuel Pégourié-Gonnarda3e7c652019-05-16 10:08:35 +02005412
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005413 if (used <= buf_len) {
5414 *p++ = MBEDTLS_BYTE_2(cert_len);
5415 *p++ = MBEDTLS_BYTE_1(cert_len);
5416 *p++ = MBEDTLS_BYTE_0(cert_len);
Manuel Pégourié-Gonnarda3e7c652019-05-16 10:08:35 +02005417
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005418 if (session->peer_cert != NULL) {
5419 memcpy(p, session->peer_cert->raw.p, cert_len);
Manuel Pégourié-Gonnard26f982f2019-05-21 11:01:32 +02005420 p += cert_len;
5421 }
5422 }
Manuel Pégourié-Gonnarda3e7c652019-05-16 10:08:35 +02005423#else /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005424 if (session->peer_cert_digest != NULL) {
Manuel Pégourié-Gonnardf743c032019-05-24 12:06:29 +02005425 used += 1 /* type */ + 1 /* length */ + session->peer_cert_digest_len;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005426 if (used <= buf_len) {
Manuel Pégourié-Gonnardf743c032019-05-24 12:06:29 +02005427 *p++ = (unsigned char) session->peer_cert_digest_type;
5428 *p++ = (unsigned char) session->peer_cert_digest_len;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005429 memcpy(p, session->peer_cert_digest,
5430 session->peer_cert_digest_len);
Manuel Pégourié-Gonnardf743c032019-05-24 12:06:29 +02005431 p += session->peer_cert_digest_len;
5432 }
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005433 } else {
Manuel Pégourié-Gonnardf743c032019-05-24 12:06:29 +02005434 used += 2;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005435 if (used <= buf_len) {
Manuel Pégourié-Gonnardf743c032019-05-24 12:06:29 +02005436 *p++ = (unsigned char) MBEDTLS_MD_NONE;
5437 *p++ = 0;
5438 }
Manuel Pégourié-Gonnard26f982f2019-05-21 11:01:32 +02005439 }
Manuel Pégourié-Gonnarda3e7c652019-05-16 10:08:35 +02005440#endif /* !MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
5441#endif /* MBEDTLS_X509_CRT_PARSE_C */
5442
Manuel Pégourié-Gonnard35eb8022019-05-16 11:11:08 +02005443 /*
Manuel Pégourié-Gonnardf743c032019-05-24 12:06:29 +02005444 * Session ticket if any, plus associated data
Manuel Pégourié-Gonnard35eb8022019-05-16 11:11:08 +02005445 */
5446#if defined(MBEDTLS_SSL_SESSION_TICKETS) && defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnardf743c032019-05-24 12:06:29 +02005447 used += 3 + session->ticket_len + 4; /* len + ticket + lifetime */
Manuel Pégourié-Gonnard35eb8022019-05-16 11:11:08 +02005448
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005449 if (used <= buf_len) {
5450 *p++ = MBEDTLS_BYTE_2(session->ticket_len);
5451 *p++ = MBEDTLS_BYTE_1(session->ticket_len);
5452 *p++ = MBEDTLS_BYTE_0(session->ticket_len);
Manuel Pégourié-Gonnard26f982f2019-05-21 11:01:32 +02005453
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005454 if (session->ticket != NULL) {
5455 memcpy(p, session->ticket, session->ticket_len);
Manuel Pégourié-Gonnard26f982f2019-05-21 11:01:32 +02005456 p += session->ticket_len;
5457 }
Manuel Pégourié-Gonnardf743c032019-05-24 12:06:29 +02005458
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005459 MBEDTLS_PUT_UINT32_BE(session->ticket_lifetime, p, 0);
Joe Subbiani2f98d792021-08-20 11:44:44 +01005460 p += 4;
Manuel Pégourié-Gonnard35eb8022019-05-16 11:11:08 +02005461 }
5462#endif /* MBEDTLS_SSL_SESSION_TICKETS && MBEDTLS_SSL_CLI_C */
5463
Manuel Pégourié-Gonnardf743c032019-05-24 12:06:29 +02005464 /*
5465 * Misc extension-related info
5466 */
5467#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
5468 used += 1;
5469
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005470 if (used <= buf_len) {
Manuel Pégourié-Gonnardf743c032019-05-24 12:06:29 +02005471 *p++ = session->mfl_code;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005472 }
Manuel Pégourié-Gonnardf743c032019-05-24 12:06:29 +02005473#endif
5474
5475#if defined(MBEDTLS_SSL_TRUNCATED_HMAC)
5476 used += 1;
5477
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005478 if (used <= buf_len) {
5479 *p++ = (unsigned char) ((session->trunc_hmac) & 0xFF);
5480 }
Manuel Pégourié-Gonnardf743c032019-05-24 12:06:29 +02005481#endif
5482
5483#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
5484 used += 1;
5485
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005486 if (used <= buf_len) {
5487 *p++ = MBEDTLS_BYTE_0(session->encrypt_then_mac);
5488 }
Manuel Pégourié-Gonnardf743c032019-05-24 12:06:29 +02005489#endif
5490
Manuel Pégourié-Gonnard35eb8022019-05-16 11:11:08 +02005491 /* Done */
Manuel Pégourié-Gonnard26f982f2019-05-21 11:01:32 +02005492 *olen = used;
5493
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005494 if (used > buf_len) {
5495 return MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL;
5496 }
Manuel Pégourié-Gonnarda3e7c652019-05-16 10:08:35 +02005497
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005498 return 0;
Manuel Pégourié-Gonnarda3e7c652019-05-16 10:08:35 +02005499}
5500
5501/*
Manuel Pégourié-Gonnard45ac1f02019-07-23 16:52:45 +02005502 * Public wrapper for ssl_session_save()
5503 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005504int mbedtls_ssl_session_save(const mbedtls_ssl_session *session,
5505 unsigned char *buf,
5506 size_t buf_len,
5507 size_t *olen)
Manuel Pégourié-Gonnard45ac1f02019-07-23 16:52:45 +02005508{
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005509 return ssl_session_save(session, 0, buf, buf_len, olen);
Manuel Pégourié-Gonnard45ac1f02019-07-23 16:52:45 +02005510}
5511
5512/*
Manuel Pégourié-Gonnardb9dfc9f2019-07-12 10:50:19 +02005513 * Deserialize session, see mbedtls_ssl_session_save() for format.
Manuel Pégourié-Gonnarda3d831b2019-05-23 12:28:45 +02005514 *
5515 * This internal version is wrapped by a public function that cleans up in
Manuel Pégourié-Gonnard45ac1f02019-07-23 16:52:45 +02005516 * case of error, and has an extra option omit_header.
Manuel Pégourié-Gonnarda3e7c652019-05-16 10:08:35 +02005517 */
Manuel Pégourié-Gonnardd904d662022-06-17 10:24:00 +02005518MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005519static int ssl_session_load(mbedtls_ssl_session *session,
5520 unsigned char omit_header,
5521 const unsigned char *buf,
5522 size_t len)
Manuel Pégourié-Gonnarda3e7c652019-05-16 10:08:35 +02005523{
5524 const unsigned char *p = buf;
5525 const unsigned char * const end = buf + len;
Manuel Pégourié-Gonnardf743c032019-05-24 12:06:29 +02005526#if defined(MBEDTLS_HAVE_TIME)
5527 uint64_t start;
5528#endif
Manuel Pégourié-Gonnarda3e7c652019-05-16 10:08:35 +02005529#if defined(MBEDTLS_X509_CRT_PARSE_C)
5530#if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
5531 size_t cert_len;
Manuel Pégourié-Gonnarda3e7c652019-05-16 10:08:35 +02005532#endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
5533#endif /* MBEDTLS_X509_CRT_PARSE_C */
5534
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005535 if (!omit_header) {
Manuel Pégourié-Gonnard45ac1f02019-07-23 16:52:45 +02005536 /*
5537 * Check version identifier
5538 */
5539
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005540 if ((size_t) (end - p) < sizeof(ssl_serialized_session_header)) {
5541 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
Manuel Pégourié-Gonnard45ac1f02019-07-23 16:52:45 +02005542 }
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005543
5544 if (memcmp(p, ssl_serialized_session_header,
5545 sizeof(ssl_serialized_session_header)) != 0) {
5546 return MBEDTLS_ERR_SSL_VERSION_MISMATCH;
5547 }
5548 p += sizeof(ssl_serialized_session_header);
Hanno Beckera835da52019-05-16 12:39:07 +01005549 }
Hanno Beckera835da52019-05-16 12:39:07 +01005550
5551 /*
Manuel Pégourié-Gonnardf743c032019-05-24 12:06:29 +02005552 * Time
Manuel Pégourié-Gonnard35eb8022019-05-16 11:11:08 +02005553 */
Manuel Pégourié-Gonnardf743c032019-05-24 12:06:29 +02005554#if defined(MBEDTLS_HAVE_TIME)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005555 if (8 > (size_t) (end - p)) {
5556 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
5557 }
Manuel Pégourié-Gonnarda3e7c652019-05-16 10:08:35 +02005558
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005559 start = ((uint64_t) p[0] << 56) |
5560 ((uint64_t) p[1] << 48) |
5561 ((uint64_t) p[2] << 40) |
5562 ((uint64_t) p[3] << 32) |
5563 ((uint64_t) p[4] << 24) |
5564 ((uint64_t) p[5] << 16) |
5565 ((uint64_t) p[6] << 8) |
5566 ((uint64_t) p[7]);
Manuel Pégourié-Gonnardf743c032019-05-24 12:06:29 +02005567 p += 8;
Manuel Pégourié-Gonnarda3e7c652019-05-16 10:08:35 +02005568
Manuel Pégourié-Gonnardf743c032019-05-24 12:06:29 +02005569 session->start = (time_t) start;
5570#endif /* MBEDTLS_HAVE_TIME */
5571
5572 /*
5573 * Basic mandatory fields
5574 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005575 if (2 + 1 + 1 + 32 + 48 + 4 > (size_t) (end - p)) {
5576 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
5577 }
Manuel Pégourié-Gonnardf743c032019-05-24 12:06:29 +02005578
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005579 session->ciphersuite = (p[0] << 8) | p[1];
Manuel Pégourié-Gonnardf743c032019-05-24 12:06:29 +02005580 p += 2;
5581
5582 session->compression = *p++;
5583
5584 session->id_len = *p++;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005585 memcpy(session->id, p, 32);
Manuel Pégourié-Gonnardf743c032019-05-24 12:06:29 +02005586 p += 32;
5587
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005588 memcpy(session->master, p, 48);
Manuel Pégourié-Gonnardf743c032019-05-24 12:06:29 +02005589 p += 48;
5590
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005591 session->verify_result = ((uint32_t) p[0] << 24) |
5592 ((uint32_t) p[1] << 16) |
5593 ((uint32_t) p[2] << 8) |
5594 ((uint32_t) p[3]);
Manuel Pégourié-Gonnardf743c032019-05-24 12:06:29 +02005595 p += 4;
5596
5597 /* Immediately clear invalid pointer values that have been read, in case
5598 * we exit early before we replaced them with valid ones. */
Manuel Pégourié-Gonnarda3e7c652019-05-16 10:08:35 +02005599#if defined(MBEDTLS_X509_CRT_PARSE_C)
5600#if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
5601 session->peer_cert = NULL;
5602#else
5603 session->peer_cert_digest = NULL;
5604#endif /* !MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
5605#endif /* MBEDTLS_X509_CRT_PARSE_C */
5606#if defined(MBEDTLS_SSL_SESSION_TICKETS) && defined(MBEDTLS_SSL_CLI_C)
5607 session->ticket = NULL;
5608#endif /* MBEDTLS_SSL_SESSION_TICKETS && MBEDTLS_SSL_CLI_C */
5609
Manuel Pégourié-Gonnard35eb8022019-05-16 11:11:08 +02005610 /*
5611 * Peer certificate
5612 */
Manuel Pégourié-Gonnarda3e7c652019-05-16 10:08:35 +02005613#if defined(MBEDTLS_X509_CRT_PARSE_C)
5614#if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
5615 /* Deserialize CRT from the end of the ticket. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005616 if (3 > (size_t) (end - p)) {
5617 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
5618 }
Manuel Pégourié-Gonnarda3e7c652019-05-16 10:08:35 +02005619
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005620 cert_len = (p[0] << 16) | (p[1] << 8) | p[2];
Manuel Pégourié-Gonnarda3e7c652019-05-16 10:08:35 +02005621 p += 3;
5622
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005623 if (cert_len != 0) {
Janos Follath865b3eb2019-12-16 11:46:15 +00005624 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnarda3e7c652019-05-16 10:08:35 +02005625
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005626 if (cert_len > (size_t) (end - p)) {
5627 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
5628 }
Manuel Pégourié-Gonnarda3e7c652019-05-16 10:08:35 +02005629
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005630 session->peer_cert = mbedtls_calloc(1, sizeof(mbedtls_x509_crt));
Manuel Pégourié-Gonnarda3e7c652019-05-16 10:08:35 +02005631
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005632 if (session->peer_cert == NULL) {
5633 return MBEDTLS_ERR_SSL_ALLOC_FAILED;
5634 }
Manuel Pégourié-Gonnarda3e7c652019-05-16 10:08:35 +02005635
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005636 mbedtls_x509_crt_init(session->peer_cert);
Manuel Pégourié-Gonnarda3e7c652019-05-16 10:08:35 +02005637
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005638 if ((ret = mbedtls_x509_crt_parse_der(session->peer_cert,
5639 p, cert_len)) != 0) {
5640 mbedtls_x509_crt_free(session->peer_cert);
5641 mbedtls_free(session->peer_cert);
Manuel Pégourié-Gonnarda3e7c652019-05-16 10:08:35 +02005642 session->peer_cert = NULL;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005643 return ret;
Manuel Pégourié-Gonnarda3e7c652019-05-16 10:08:35 +02005644 }
5645
5646 p += cert_len;
5647 }
5648#else /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
5649 /* Deserialize CRT digest from the end of the ticket. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005650 if (2 > (size_t) (end - p)) {
5651 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
5652 }
Manuel Pégourié-Gonnarda3e7c652019-05-16 10:08:35 +02005653
Manuel Pégourié-Gonnardf743c032019-05-24 12:06:29 +02005654 session->peer_cert_digest_type = (mbedtls_md_type_t) *p++;
5655 session->peer_cert_digest_len = (size_t) *p++;
Manuel Pégourié-Gonnarda3e7c652019-05-16 10:08:35 +02005656
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005657 if (session->peer_cert_digest_len != 0) {
Manuel Pégourié-Gonnardf743c032019-05-24 12:06:29 +02005658 const mbedtls_md_info_t *md_info =
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005659 mbedtls_md_info_from_type(session->peer_cert_digest_type);
5660 if (md_info == NULL) {
5661 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
5662 }
5663 if (session->peer_cert_digest_len != mbedtls_md_get_size(md_info)) {
5664 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
5665 }
Manuel Pégourié-Gonnarda3e7c652019-05-16 10:08:35 +02005666
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005667 if (session->peer_cert_digest_len > (size_t) (end - p)) {
5668 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
5669 }
Manuel Pégourié-Gonnardf743c032019-05-24 12:06:29 +02005670
5671 session->peer_cert_digest =
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005672 mbedtls_calloc(1, session->peer_cert_digest_len);
5673 if (session->peer_cert_digest == NULL) {
5674 return MBEDTLS_ERR_SSL_ALLOC_FAILED;
5675 }
Manuel Pégourié-Gonnarda3e7c652019-05-16 10:08:35 +02005676
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005677 memcpy(session->peer_cert_digest, p,
5678 session->peer_cert_digest_len);
Manuel Pégourié-Gonnardf743c032019-05-24 12:06:29 +02005679 p += session->peer_cert_digest_len;
Manuel Pégourié-Gonnarda3e7c652019-05-16 10:08:35 +02005680 }
5681#endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
5682#endif /* MBEDTLS_X509_CRT_PARSE_C */
5683
Manuel Pégourié-Gonnard35eb8022019-05-16 11:11:08 +02005684 /*
Manuel Pégourié-Gonnardf743c032019-05-24 12:06:29 +02005685 * Session ticket and associated data
Manuel Pégourié-Gonnard35eb8022019-05-16 11:11:08 +02005686 */
5687#if defined(MBEDTLS_SSL_SESSION_TICKETS) && defined(MBEDTLS_SSL_CLI_C)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005688 if (3 > (size_t) (end - p)) {
5689 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
5690 }
Manuel Pégourié-Gonnard35eb8022019-05-16 11:11:08 +02005691
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005692 session->ticket_len = (p[0] << 16) | (p[1] << 8) | p[2];
Manuel Pégourié-Gonnard35eb8022019-05-16 11:11:08 +02005693 p += 3;
5694
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005695 if (session->ticket_len != 0) {
5696 if (session->ticket_len > (size_t) (end - p)) {
5697 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
5698 }
Manuel Pégourié-Gonnard35eb8022019-05-16 11:11:08 +02005699
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005700 session->ticket = mbedtls_calloc(1, session->ticket_len);
5701 if (session->ticket == NULL) {
5702 return MBEDTLS_ERR_SSL_ALLOC_FAILED;
5703 }
Manuel Pégourié-Gonnard35eb8022019-05-16 11:11:08 +02005704
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005705 memcpy(session->ticket, p, session->ticket_len);
Manuel Pégourié-Gonnard35eb8022019-05-16 11:11:08 +02005706 p += session->ticket_len;
5707 }
Manuel Pégourié-Gonnardf743c032019-05-24 12:06:29 +02005708
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005709 if (4 > (size_t) (end - p)) {
5710 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
5711 }
Manuel Pégourié-Gonnardf743c032019-05-24 12:06:29 +02005712
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005713 session->ticket_lifetime = ((uint32_t) p[0] << 24) |
5714 ((uint32_t) p[1] << 16) |
5715 ((uint32_t) p[2] << 8) |
5716 ((uint32_t) p[3]);
Manuel Pégourié-Gonnardf743c032019-05-24 12:06:29 +02005717 p += 4;
Manuel Pégourié-Gonnard35eb8022019-05-16 11:11:08 +02005718#endif /* MBEDTLS_SSL_SESSION_TICKETS && MBEDTLS_SSL_CLI_C */
5719
Manuel Pégourié-Gonnardf743c032019-05-24 12:06:29 +02005720 /*
5721 * Misc extension-related info
5722 */
5723#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005724 if (1 > (size_t) (end - p)) {
5725 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
5726 }
Manuel Pégourié-Gonnardf743c032019-05-24 12:06:29 +02005727
5728 session->mfl_code = *p++;
5729#endif
5730
5731#if defined(MBEDTLS_SSL_TRUNCATED_HMAC)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005732 if (1 > (size_t) (end - p)) {
5733 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
5734 }
Manuel Pégourié-Gonnardf743c032019-05-24 12:06:29 +02005735
5736 session->trunc_hmac = *p++;
5737#endif
5738
5739#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005740 if (1 > (size_t) (end - p)) {
5741 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
5742 }
Manuel Pégourié-Gonnardf743c032019-05-24 12:06:29 +02005743
5744 session->encrypt_then_mac = *p++;
5745#endif
5746
Manuel Pégourié-Gonnard35eb8022019-05-16 11:11:08 +02005747 /* Done, should have consumed entire buffer */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005748 if (p != end) {
5749 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
5750 }
Manuel Pégourié-Gonnarda3e7c652019-05-16 10:08:35 +02005751
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005752 return 0;
Manuel Pégourié-Gonnarda3e7c652019-05-16 10:08:35 +02005753}
5754
5755/*
Manuel Pégourié-Gonnardb9dfc9f2019-07-12 10:50:19 +02005756 * Deserialize session: public wrapper for error cleaning
Manuel Pégourié-Gonnarda3d831b2019-05-23 12:28:45 +02005757 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005758int mbedtls_ssl_session_load(mbedtls_ssl_session *session,
5759 const unsigned char *buf,
5760 size_t len)
Manuel Pégourié-Gonnarda3d831b2019-05-23 12:28:45 +02005761{
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005762 int ret = ssl_session_load(session, 0, buf, len);
Manuel Pégourié-Gonnarda3d831b2019-05-23 12:28:45 +02005763
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005764 if (ret != 0) {
5765 mbedtls_ssl_session_free(session);
5766 }
Manuel Pégourié-Gonnarda3d831b2019-05-23 12:28:45 +02005767
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005768 return ret;
Manuel Pégourié-Gonnarda3d831b2019-05-23 12:28:45 +02005769}
5770
5771/*
Paul Bakker1961b702013-01-25 14:49:24 +01005772 * Perform a single step of the SSL handshake
Paul Bakker5121ce52009-01-03 21:22:43 +00005773 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005774int mbedtls_ssl_handshake_step(mbedtls_ssl_context *ssl)
Paul Bakker5121ce52009-01-03 21:22:43 +00005775{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005776 int ret = MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
Paul Bakker5121ce52009-01-03 21:22:43 +00005777
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005778 if (ssl == NULL || ssl->conf == NULL) {
5779 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
5780 }
Manuel Pégourié-Gonnardf81ee2e2015-09-01 17:43:40 +02005781
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005782#if defined(MBEDTLS_SSL_CLI_C)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005783 if (ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT) {
5784 ret = mbedtls_ssl_handshake_client_step(ssl);
5785 }
Paul Bakker5121ce52009-01-03 21:22:43 +00005786#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005787#if defined(MBEDTLS_SSL_SRV_C)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005788 if (ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER) {
5789 ret = mbedtls_ssl_handshake_server_step(ssl);
5790 }
Paul Bakker5121ce52009-01-03 21:22:43 +00005791#endif
5792
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005793 return ret;
Paul Bakker1961b702013-01-25 14:49:24 +01005794}
5795
5796/*
5797 * Perform the SSL handshake
5798 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005799int mbedtls_ssl_handshake(mbedtls_ssl_context *ssl)
Paul Bakker1961b702013-01-25 14:49:24 +01005800{
5801 int ret = 0;
5802
Hanno Beckera817ea42020-10-20 15:20:23 +01005803 /* Sanity checks */
5804
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005805 if (ssl == NULL || ssl->conf == NULL) {
5806 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
5807 }
Manuel Pégourié-Gonnardf81ee2e2015-09-01 17:43:40 +02005808
Hanno Beckera817ea42020-10-20 15:20:23 +01005809#if defined(MBEDTLS_SSL_PROTO_DTLS)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005810 if (ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
5811 (ssl->f_set_timer == NULL || ssl->f_get_timer == NULL)) {
5812 MBEDTLS_SSL_DEBUG_MSG(1, ("You must use "
5813 "mbedtls_ssl_set_timer_cb() for DTLS"));
5814 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
Hanno Beckera817ea42020-10-20 15:20:23 +01005815 }
5816#endif /* MBEDTLS_SSL_PROTO_DTLS */
5817
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005818 MBEDTLS_SSL_DEBUG_MSG(2, ("=> handshake"));
Paul Bakker1961b702013-01-25 14:49:24 +01005819
Hanno Beckera817ea42020-10-20 15:20:23 +01005820 /* Main handshake loop */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005821 while (ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER) {
5822 ret = mbedtls_ssl_handshake_step(ssl);
Paul Bakker1961b702013-01-25 14:49:24 +01005823
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005824 if (ret != 0) {
Paul Bakker1961b702013-01-25 14:49:24 +01005825 break;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005826 }
Paul Bakker1961b702013-01-25 14:49:24 +01005827 }
5828
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005829 MBEDTLS_SSL_DEBUG_MSG(2, ("<= handshake"));
Paul Bakker5121ce52009-01-03 21:22:43 +00005830
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005831 return ret;
Paul Bakker5121ce52009-01-03 21:22:43 +00005832}
5833
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005834#if defined(MBEDTLS_SSL_RENEGOTIATION)
5835#if defined(MBEDTLS_SSL_SRV_C)
Paul Bakker5121ce52009-01-03 21:22:43 +00005836/*
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01005837 * Write HelloRequest to request renegotiation on server
Paul Bakker48916f92012-09-16 19:57:18 +00005838 */
Manuel Pégourié-Gonnardd904d662022-06-17 10:24:00 +02005839MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005840static int ssl_write_hello_request(mbedtls_ssl_context *ssl)
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01005841{
Janos Follath865b3eb2019-12-16 11:46:15 +00005842 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01005843
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005844 MBEDTLS_SSL_DEBUG_MSG(2, ("=> write hello request"));
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01005845
5846 ssl->out_msglen = 4;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005847 ssl->out_msgtype = MBEDTLS_SSL_MSG_HANDSHAKE;
5848 ssl->out_msg[0] = MBEDTLS_SSL_HS_HELLO_REQUEST;
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01005849
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005850 if ((ret = mbedtls_ssl_write_handshake_msg(ssl)) != 0) {
5851 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ssl_write_handshake_msg", ret);
5852 return ret;
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01005853 }
5854
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005855 MBEDTLS_SSL_DEBUG_MSG(2, ("<= write hello request"));
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01005856
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005857 return 0;
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01005858}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005859#endif /* MBEDTLS_SSL_SRV_C */
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01005860
5861/*
5862 * Actually renegotiate current connection, triggered by either:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005863 * - any side: calling mbedtls_ssl_renegotiate(),
5864 * - client: receiving a HelloRequest during mbedtls_ssl_read(),
5865 * - server: receiving any handshake message on server during mbedtls_ssl_read() after
Manuel Pégourié-Gonnard55e4ff22014-08-19 11:16:35 +02005866 * the initial handshake is completed.
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01005867 * If the handshake doesn't complete due to waiting for I/O, it will continue
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005868 * during the next calls to mbedtls_ssl_renegotiate() or mbedtls_ssl_read() respectively.
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01005869 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005870int mbedtls_ssl_start_renegotiation(mbedtls_ssl_context *ssl)
Paul Bakker48916f92012-09-16 19:57:18 +00005871{
Janos Follath865b3eb2019-12-16 11:46:15 +00005872 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker48916f92012-09-16 19:57:18 +00005873
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005874 MBEDTLS_SSL_DEBUG_MSG(2, ("=> renegotiate"));
Paul Bakker48916f92012-09-16 19:57:18 +00005875
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005876 if ((ret = ssl_handshake_init(ssl)) != 0) {
5877 return ret;
5878 }
Paul Bakker48916f92012-09-16 19:57:18 +00005879
Manuel Pégourié-Gonnard0557bd52014-08-19 19:18:39 +02005880 /* RFC 6347 4.2.2: "[...] the HelloRequest will have message_seq = 0 and
5881 * the ServerHello will have message_seq = 1" */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005882#if defined(MBEDTLS_SSL_PROTO_DTLS)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005883 if (ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
5884 ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_PENDING) {
5885 if (ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER) {
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02005886 ssl->handshake->out_msg_seq = 1;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005887 } else {
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02005888 ssl->handshake->in_msg_seq = 1;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005889 }
Manuel Pégourié-Gonnard0557bd52014-08-19 19:18:39 +02005890 }
5891#endif
5892
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005893 ssl->state = MBEDTLS_SSL_HELLO_REQUEST;
5894 ssl->renego_status = MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS;
Paul Bakker48916f92012-09-16 19:57:18 +00005895
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005896 if ((ret = mbedtls_ssl_handshake(ssl)) != 0) {
5897 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ssl_handshake", ret);
5898 return ret;
Paul Bakker48916f92012-09-16 19:57:18 +00005899 }
5900
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005901 MBEDTLS_SSL_DEBUG_MSG(2, ("<= renegotiate"));
Paul Bakker48916f92012-09-16 19:57:18 +00005902
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005903 return 0;
Paul Bakker48916f92012-09-16 19:57:18 +00005904}
5905
5906/*
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01005907 * Renegotiate current connection on client,
5908 * or request renegotiation on server
5909 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005910int mbedtls_ssl_renegotiate(mbedtls_ssl_context *ssl)
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01005911{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005912 int ret = MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01005913
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005914 if (ssl == NULL || ssl->conf == NULL) {
5915 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
5916 }
Manuel Pégourié-Gonnardf81ee2e2015-09-01 17:43:40 +02005917
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005918#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01005919 /* On server, just send the request */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005920 if (ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER) {
5921 if (ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER) {
5922 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
5923 }
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01005924
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005925 ssl->renego_status = MBEDTLS_SSL_RENEGOTIATION_PENDING;
Manuel Pégourié-Gonnardf07f4212014-08-15 19:04:47 +02005926
5927 /* Did we already try/start sending HelloRequest? */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005928 if (ssl->out_left != 0) {
5929 return mbedtls_ssl_flush_output(ssl);
5930 }
Manuel Pégourié-Gonnardf07f4212014-08-15 19:04:47 +02005931
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005932 return ssl_write_hello_request(ssl);
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01005933 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005934#endif /* MBEDTLS_SSL_SRV_C */
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01005935
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005936#if defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01005937 /*
5938 * On client, either start the renegotiation process or,
5939 * if already in progress, continue the handshake
5940 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005941 if (ssl->renego_status != MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS) {
5942 if (ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER) {
5943 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01005944 }
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005945
5946 if ((ret = mbedtls_ssl_start_renegotiation(ssl)) != 0) {
5947 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ssl_start_renegotiation", ret);
5948 return ret;
5949 }
5950 } else {
5951 if ((ret = mbedtls_ssl_handshake(ssl)) != 0) {
5952 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ssl_handshake", ret);
5953 return ret;
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01005954 }
5955 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005956#endif /* MBEDTLS_SSL_CLI_C */
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01005957
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005958 return ret;
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01005959}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005960#endif /* MBEDTLS_SSL_RENEGOTIATION */
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01005961
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005962#if defined(MBEDTLS_X509_CRT_PARSE_C)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005963static void ssl_key_cert_free(mbedtls_ssl_key_cert *key_cert)
Manuel Pégourié-Gonnard705fcca2013-09-23 20:04:20 +02005964{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005965 mbedtls_ssl_key_cert *cur = key_cert, *next;
Manuel Pégourié-Gonnard705fcca2013-09-23 20:04:20 +02005966
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005967 while (cur != NULL) {
Manuel Pégourié-Gonnard705fcca2013-09-23 20:04:20 +02005968 next = cur->next;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005969 mbedtls_free(cur);
Manuel Pégourié-Gonnard705fcca2013-09-23 20:04:20 +02005970 cur = next;
5971 }
5972}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005973#endif /* MBEDTLS_X509_CRT_PARSE_C */
Manuel Pégourié-Gonnard705fcca2013-09-23 20:04:20 +02005974
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005975void mbedtls_ssl_handshake_free(mbedtls_ssl_context *ssl)
Paul Bakker48916f92012-09-16 19:57:18 +00005976{
Gilles Peskine9b562d52018-04-25 20:32:43 +02005977 mbedtls_ssl_handshake_params *handshake = ssl->handshake;
5978
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005979 if (handshake == NULL) {
Paul Bakkeraccaffe2014-06-26 13:37:14 +02005980 return;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005981 }
Paul Bakkeraccaffe2014-06-26 13:37:14 +02005982
Gilles Peskinedf13d5c2018-04-25 20:39:48 +02005983#if defined(MBEDTLS_SSL_ASYNC_PRIVATE)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005984 if (ssl->conf->f_async_cancel != NULL && handshake->async_in_progress != 0) {
5985 ssl->conf->f_async_cancel(ssl);
Gilles Peskinedf13d5c2018-04-25 20:39:48 +02005986 handshake->async_in_progress = 0;
5987 }
5988#endif /* MBEDTLS_SSL_ASYNC_PRIVATE */
5989
Manuel Pégourié-Gonnardb9d64e52015-07-06 14:18:56 +02005990#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1) || \
5991 defined(MBEDTLS_SSL_PROTO_TLS1_1)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005992 mbedtls_md5_free(&handshake->fin_md5);
5993 mbedtls_sha1_free(&handshake->fin_sha1);
Manuel Pégourié-Gonnardb9d64e52015-07-06 14:18:56 +02005994#endif
5995#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
5996#if defined(MBEDTLS_SHA256_C)
Andrzej Kurekeb342242019-01-29 09:14:33 -05005997#if defined(MBEDTLS_USE_PSA_CRYPTO)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005998 psa_hash_abort(&handshake->fin_sha256_psa);
Andrzej Kurekeb342242019-01-29 09:14:33 -05005999#else
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01006000 mbedtls_sha256_free(&handshake->fin_sha256);
Manuel Pégourié-Gonnardb9d64e52015-07-06 14:18:56 +02006001#endif
Andrzej Kurekeb342242019-01-29 09:14:33 -05006002#endif
Gilles Peskined2d59372021-05-12 22:43:27 +02006003#if defined(MBEDTLS_SHA512_C) && !defined(MBEDTLS_SHA512_NO_SHA384)
Andrzej Kurekeb342242019-01-29 09:14:33 -05006004#if defined(MBEDTLS_USE_PSA_CRYPTO)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01006005 psa_hash_abort(&handshake->fin_sha384_psa);
Andrzej Kurekeb342242019-01-29 09:14:33 -05006006#else
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01006007 mbedtls_sha512_free(&handshake->fin_sha512);
Manuel Pégourié-Gonnardb9d64e52015-07-06 14:18:56 +02006008#endif
Andrzej Kurekeb342242019-01-29 09:14:33 -05006009#endif
Manuel Pégourié-Gonnardb9d64e52015-07-06 14:18:56 +02006010#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
6011
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006012#if defined(MBEDTLS_DHM_C)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01006013 mbedtls_dhm_free(&handshake->dhm_ctx);
Paul Bakker48916f92012-09-16 19:57:18 +00006014#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006015#if defined(MBEDTLS_ECDH_C)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01006016 mbedtls_ecdh_free(&handshake->ecdh_ctx);
Paul Bakker61d113b2013-07-04 11:51:43 +02006017#endif
Manuel Pégourié-Gonnardeef142d2015-09-16 10:05:04 +02006018#if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01006019 mbedtls_ecjpake_free(&handshake->ecjpake_ctx);
Manuel Pégourié-Gonnard77c06462015-09-17 13:59:49 +02006020#if defined(MBEDTLS_SSL_CLI_C)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01006021 mbedtls_free(handshake->ecjpake_cache);
Manuel Pégourié-Gonnard77c06462015-09-17 13:59:49 +02006022 handshake->ecjpake_cache = NULL;
6023 handshake->ecjpake_cache_len = 0;
6024#endif
Manuel Pégourié-Gonnard76cfd3f2015-09-15 12:10:54 +02006025#endif
Paul Bakker61d113b2013-07-04 11:51:43 +02006026
Janos Follath4ae5c292016-02-10 11:27:43 +00006027#if defined(MBEDTLS_ECDH_C) || defined(MBEDTLS_ECDSA_C) || \
6028 defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
Paul Bakker9af723c2014-05-01 13:03:14 +02006029 /* explicit void pointer cast for buggy MS compiler */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01006030 mbedtls_free((void *) handshake->curves);
Manuel Pégourié-Gonnardd09453c2013-09-23 19:11:32 +02006031#endif
6032
Gilles Peskineeccd8882020-03-10 12:19:08 +01006033#if defined(MBEDTLS_KEY_EXCHANGE_SOME_PSK_ENABLED)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01006034 if (handshake->psk != NULL) {
6035 mbedtls_platform_zeroize(handshake->psk, handshake->psk_len);
6036 mbedtls_free(handshake->psk);
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01006037 }
6038#endif
6039
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006040#if defined(MBEDTLS_X509_CRT_PARSE_C) && \
6041 defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
Manuel Pégourié-Gonnard83724542013-09-24 22:30:56 +02006042 /*
6043 * Free only the linked list wrapper, not the keys themselves
6044 * since the belong to the SNI callback
6045 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01006046 if (handshake->sni_key_cert != NULL) {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006047 mbedtls_ssl_key_cert *cur = handshake->sni_key_cert, *next;
Manuel Pégourié-Gonnard83724542013-09-24 22:30:56 +02006048
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01006049 while (cur != NULL) {
Manuel Pégourié-Gonnard83724542013-09-24 22:30:56 +02006050 next = cur->next;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01006051 mbedtls_free(cur);
Manuel Pégourié-Gonnard83724542013-09-24 22:30:56 +02006052 cur = next;
6053 }
6054 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006055#endif /* MBEDTLS_X509_CRT_PARSE_C && MBEDTLS_SSL_SERVER_NAME_INDICATION */
Manuel Pégourié-Gonnard705fcca2013-09-23 20:04:20 +02006056
Gilles Peskineeccd8882020-03-10 12:19:08 +01006057#if defined(MBEDTLS_SSL_ECP_RESTARTABLE_ENABLED)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01006058 mbedtls_x509_crt_restart_free(&handshake->ecrs_ctx);
6059 if (handshake->ecrs_peer_cert != NULL) {
6060 mbedtls_x509_crt_free(handshake->ecrs_peer_cert);
6061 mbedtls_free(handshake->ecrs_peer_cert);
Hanno Becker3dad3112019-02-05 17:19:52 +00006062 }
Manuel Pégourié-Gonnard862cde52017-05-17 11:56:15 +02006063#endif
6064
Hanno Becker75173122019-02-06 16:18:31 +00006065#if defined(MBEDTLS_X509_CRT_PARSE_C) && \
6066 !defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01006067 mbedtls_pk_free(&handshake->peer_pubkey);
Hanno Becker75173122019-02-06 16:18:31 +00006068#endif /* MBEDTLS_X509_CRT_PARSE_C && !MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
6069
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006070#if defined(MBEDTLS_SSL_PROTO_DTLS)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01006071 mbedtls_free(handshake->verify_cookie);
6072 mbedtls_ssl_flight_free(handshake->flight);
6073 mbedtls_ssl_buffering_free(ssl);
Manuel Pégourié-Gonnard74848812014-07-11 02:43:49 +02006074#endif
6075
Hanno Becker4a63ed42019-01-08 11:39:35 +00006076#if defined(MBEDTLS_ECDH_C) && \
6077 defined(MBEDTLS_USE_PSA_CRYPTO)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01006078 psa_destroy_key(handshake->ecdh_psa_privkey);
Hanno Becker4a63ed42019-01-08 11:39:35 +00006079#endif /* MBEDTLS_ECDH_C && MBEDTLS_USE_PSA_CRYPTO */
6080
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01006081 mbedtls_platform_zeroize(handshake,
6082 sizeof(mbedtls_ssl_handshake_params));
Andrzej Kurek0afa2a12020-03-03 10:39:58 -05006083
6084#if defined(MBEDTLS_SSL_VARIABLE_BUFFER_LENGTH)
6085 /* If the buffers are too big - reallocate. Because of the way Mbed TLS
6086 * processes datagrams and the fact that a datagram is allowed to have
6087 * several records in it, it is possible that the I/O buffers are not
6088 * empty at this stage */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01006089 handle_buffer_resizing(ssl, 1, mbedtls_ssl_get_input_buflen(ssl),
6090 mbedtls_ssl_get_output_buflen(ssl));
Andrzej Kurek0afa2a12020-03-03 10:39:58 -05006091#endif
Paul Bakker48916f92012-09-16 19:57:18 +00006092}
6093
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01006094void mbedtls_ssl_session_free(mbedtls_ssl_session *session)
Paul Bakker48916f92012-09-16 19:57:18 +00006095{
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01006096 if (session == NULL) {
Paul Bakkeraccaffe2014-06-26 13:37:14 +02006097 return;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01006098 }
Paul Bakkeraccaffe2014-06-26 13:37:14 +02006099
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006100#if defined(MBEDTLS_X509_CRT_PARSE_C)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01006101 ssl_clear_peer_cert(session);
Paul Bakkered27a042013-04-18 22:46:23 +02006102#endif
Paul Bakker0a597072012-09-25 21:55:46 +00006103
Manuel Pégourié-Gonnardb596abf2015-05-20 10:45:29 +02006104#if defined(MBEDTLS_SSL_SESSION_TICKETS) && defined(MBEDTLS_SSL_CLI_C)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01006105 mbedtls_free(session->ticket);
Paul Bakkera503a632013-08-14 13:48:06 +02006106#endif
Manuel Pégourié-Gonnard75d44012013-08-02 14:44:04 +02006107
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01006108 mbedtls_platform_zeroize(session, sizeof(mbedtls_ssl_session));
Paul Bakker48916f92012-09-16 19:57:18 +00006109}
6110
Manuel Pégourié-Gonnard5c0e3772019-07-23 16:13:17 +02006111#if defined(MBEDTLS_SSL_CONTEXT_SERIALIZATION)
Manuel Pégourié-Gonnard4e9370b2019-07-23 16:31:16 +02006112
6113#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
6114#define SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_CONNECTION_ID 1u
6115#else
6116#define SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_CONNECTION_ID 0u
6117#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
6118
6119#if defined(MBEDTLS_SSL_DTLS_BADMAC_LIMIT)
6120#define SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_BADMAC_LIMIT 1u
6121#else
6122#define SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_BADMAC_LIMIT 0u
6123#endif /* MBEDTLS_SSL_DTLS_BADMAC_LIMIT */
6124
6125#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
6126#define SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_ANTI_REPLAY 1u
6127#else
6128#define SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_ANTI_REPLAY 0u
6129#endif /* MBEDTLS_SSL_DTLS_ANTI_REPLAY */
6130
6131#if defined(MBEDTLS_SSL_ALPN)
6132#define SSL_SERIALIZED_CONTEXT_CONFIG_ALPN 1u
6133#else
6134#define SSL_SERIALIZED_CONTEXT_CONFIG_ALPN 0u
6135#endif /* MBEDTLS_SSL_ALPN */
6136
6137#define SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_CONNECTION_ID_BIT 0
6138#define SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_BADMAC_LIMIT_BIT 1
6139#define SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_ANTI_REPLAY_BIT 2
6140#define SSL_SERIALIZED_CONTEXT_CONFIG_ALPN_BIT 3
6141
6142#define SSL_SERIALIZED_CONTEXT_CONFIG_BITFLAG \
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01006143 ((uint32_t) ( \
6144 (SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_CONNECTION_ID << \
6145 SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_CONNECTION_ID_BIT) | \
6146 (SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_BADMAC_LIMIT << \
6147 SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_BADMAC_LIMIT_BIT) | \
6148 (SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_ANTI_REPLAY << \
6149 SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_ANTI_REPLAY_BIT) | \
6150 (SSL_SERIALIZED_CONTEXT_CONFIG_ALPN << SSL_SERIALIZED_CONTEXT_CONFIG_ALPN_BIT) | \
6151 0u))
Manuel Pégourié-Gonnard4e9370b2019-07-23 16:31:16 +02006152
Chien Wongb6d57932024-02-07 21:48:12 +08006153static const unsigned char ssl_serialized_context_header[] = {
Manuel Pégourié-Gonnard4c90e852019-07-11 10:58:10 +02006154 MBEDTLS_VERSION_MAJOR,
6155 MBEDTLS_VERSION_MINOR,
6156 MBEDTLS_VERSION_PATCH,
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01006157 MBEDTLS_BYTE_1(SSL_SERIALIZED_SESSION_CONFIG_BITFLAG),
6158 MBEDTLS_BYTE_0(SSL_SERIALIZED_SESSION_CONFIG_BITFLAG),
6159 MBEDTLS_BYTE_2(SSL_SERIALIZED_CONTEXT_CONFIG_BITFLAG),
6160 MBEDTLS_BYTE_1(SSL_SERIALIZED_CONTEXT_CONFIG_BITFLAG),
6161 MBEDTLS_BYTE_0(SSL_SERIALIZED_CONTEXT_CONFIG_BITFLAG),
Manuel Pégourié-Gonnard4c90e852019-07-11 10:58:10 +02006162};
6163
Paul Bakker5121ce52009-01-03 21:22:43 +00006164/*
Manuel Pégourié-Gonnardac87e282019-05-28 13:02:16 +02006165 * Serialize a full SSL context
Manuel Pégourié-Gonnard00400c22019-07-10 14:58:45 +02006166 *
6167 * The format of the serialized data is:
6168 * (in the presentation language of TLS, RFC 8446 section 3)
6169 *
6170 * // header
6171 * opaque mbedtls_version[3]; // major, minor, patch
Manuel Pégourié-Gonnard4c90e852019-07-11 10:58:10 +02006172 * opaque context_format[5]; // version-specific field determining
Manuel Pégourié-Gonnard00400c22019-07-10 14:58:45 +02006173 * // the format of the remaining
Manuel Pégourié-Gonnard4c90e852019-07-11 10:58:10 +02006174 * // serialized data.
Manuel Pégourié-Gonnard4e9370b2019-07-23 16:31:16 +02006175 * Note: When updating the format, remember to keep these
6176 * version+format bytes. (We may make their size part of the API.)
Manuel Pégourié-Gonnard00400c22019-07-10 14:58:45 +02006177 *
6178 * // session sub-structure
6179 * opaque session<1..2^32-1>; // see mbedtls_ssl_session_save()
6180 * // transform sub-structure
6181 * uint8 random[64]; // ServerHello.random+ClientHello.random
6182 * uint8 in_cid<0..2^8-1> // Connection ID: expected incoming value
6183 * uint8 out_cid<0..2^8-1> // Connection ID: outgoing value to use
6184 * // fields from ssl_context
6185 * uint32 badmac_seen; // DTLS: number of records with failing MAC
6186 * uint64 in_window_top; // DTLS: last validated record seq_num
6187 * uint64 in_window; // DTLS: bitmask for replay protection
6188 * uint8 disable_datagram_packing; // DTLS: only one record per datagram
6189 * uint64 cur_out_ctr; // Record layer: outgoing sequence number
6190 * uint16 mtu; // DTLS: path mtu (max outgoing fragment size)
6191 * uint8 alpn_chosen<0..2^8-1> // ALPN: negotiated application protocol
6192 *
6193 * Note that many fields of the ssl_context or sub-structures are not
6194 * serialized, as they fall in one of the following categories:
6195 *
6196 * 1. forced value (eg in_left must be 0)
6197 * 2. pointer to dynamically-allocated memory (eg session, transform)
6198 * 3. value can be re-derived from other data (eg session keys from MS)
6199 * 4. value was temporary (eg content of input buffer)
6200 * 5. value will be provided by the user again (eg I/O callbacks and context)
Manuel Pégourié-Gonnardac87e282019-05-28 13:02:16 +02006201 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01006202int mbedtls_ssl_context_save(mbedtls_ssl_context *ssl,
6203 unsigned char *buf,
6204 size_t buf_len,
6205 size_t *olen)
Manuel Pégourié-Gonnardac87e282019-05-28 13:02:16 +02006206{
Manuel Pégourié-Gonnard4c90e852019-07-11 10:58:10 +02006207 unsigned char *p = buf;
6208 size_t used = 0;
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02006209 size_t session_len;
6210 int ret = 0;
Manuel Pégourié-Gonnard4c90e852019-07-11 10:58:10 +02006211
Manuel Pégourié-Gonnard1aaf6692019-07-10 14:14:05 +02006212 /*
Manuel Pégourié-Gonnarde4588692019-07-29 12:28:52 +02006213 * Enforce usage restrictions, see "return BAD_INPUT_DATA" in
6214 * this function's documentation.
6215 *
6216 * These are due to assumptions/limitations in the implementation. Some of
6217 * them are likely to stay (no handshake in progress) some might go away
6218 * (only DTLS) but are currently used to simplify the implementation.
Manuel Pégourié-Gonnard1aaf6692019-07-10 14:14:05 +02006219 */
Manuel Pégourié-Gonnarde4588692019-07-29 12:28:52 +02006220 /* The initial handshake must be over */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01006221 if (ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER) {
6222 MBEDTLS_SSL_DEBUG_MSG(1, ("Initial handshake isn't over"));
6223 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
Jarno Lamsa8c51b7c2019-08-21 13:45:05 +03006224 }
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01006225 if (ssl->handshake != NULL) {
6226 MBEDTLS_SSL_DEBUG_MSG(1, ("Handshake isn't completed"));
6227 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
Jarno Lamsa8c51b7c2019-08-21 13:45:05 +03006228 }
Manuel Pégourié-Gonnarde4588692019-07-29 12:28:52 +02006229 /* Double-check that sub-structures are indeed ready */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01006230 if (ssl->transform == NULL || ssl->session == NULL) {
6231 MBEDTLS_SSL_DEBUG_MSG(1, ("Serialised structures aren't ready"));
6232 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
Jarno Lamsa8c51b7c2019-08-21 13:45:05 +03006233 }
Manuel Pégourié-Gonnarde4588692019-07-29 12:28:52 +02006234 /* There must be no pending incoming or outgoing data */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01006235 if (mbedtls_ssl_check_pending(ssl) != 0) {
6236 MBEDTLS_SSL_DEBUG_MSG(1, ("There is pending incoming data"));
6237 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
Jarno Lamsa8c51b7c2019-08-21 13:45:05 +03006238 }
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01006239 if (ssl->out_left != 0) {
6240 MBEDTLS_SSL_DEBUG_MSG(1, ("There is pending outgoing data"));
6241 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
Jarno Lamsa8c51b7c2019-08-21 13:45:05 +03006242 }
Dave Rodgmana03396a2022-12-06 16:30:38 +00006243 /* Protocol must be DTLS, not TLS */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01006244 if (ssl->conf->transport != MBEDTLS_SSL_TRANSPORT_DATAGRAM) {
6245 MBEDTLS_SSL_DEBUG_MSG(1, ("Only DTLS is supported"));
6246 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
Jarno Lamsa8c51b7c2019-08-21 13:45:05 +03006247 }
Manuel Pégourié-Gonnarde4588692019-07-29 12:28:52 +02006248 /* Version must be 1.2 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01006249 if (ssl->major_ver != MBEDTLS_SSL_MAJOR_VERSION_3) {
6250 MBEDTLS_SSL_DEBUG_MSG(1, ("Only version 1.2 supported"));
6251 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
Jarno Lamsa8c51b7c2019-08-21 13:45:05 +03006252 }
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01006253 if (ssl->minor_ver != MBEDTLS_SSL_MINOR_VERSION_3) {
6254 MBEDTLS_SSL_DEBUG_MSG(1, ("Only version 1.2 supported"));
6255 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
Jarno Lamsa8c51b7c2019-08-21 13:45:05 +03006256 }
Manuel Pégourié-Gonnarde4588692019-07-29 12:28:52 +02006257 /* We must be using an AEAD ciphersuite */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01006258 if (mbedtls_ssl_transform_uses_aead(ssl->transform) != 1) {
6259 MBEDTLS_SSL_DEBUG_MSG(1, ("Only AEAD ciphersuites supported"));
6260 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
Jarno Lamsa8c51b7c2019-08-21 13:45:05 +03006261 }
Manuel Pégourié-Gonnarde4588692019-07-29 12:28:52 +02006262 /* Renegotiation must not be enabled */
6263#if defined(MBEDTLS_SSL_RENEGOTIATION)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01006264 if (ssl->conf->disable_renegotiation != MBEDTLS_SSL_RENEGOTIATION_DISABLED) {
6265 MBEDTLS_SSL_DEBUG_MSG(1, ("Renegotiation must not be enabled"));
6266 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
Jarno Lamsa8c51b7c2019-08-21 13:45:05 +03006267 }
Manuel Pégourié-Gonnarde4588692019-07-29 12:28:52 +02006268#endif
Manuel Pégourié-Gonnardac87e282019-05-28 13:02:16 +02006269
Manuel Pégourié-Gonnard4c90e852019-07-11 10:58:10 +02006270 /*
6271 * Version and format identifier
6272 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01006273 used += sizeof(ssl_serialized_context_header);
Manuel Pégourié-Gonnardac87e282019-05-28 13:02:16 +02006274
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01006275 if (used <= buf_len) {
6276 memcpy(p, ssl_serialized_context_header,
6277 sizeof(ssl_serialized_context_header));
6278 p += sizeof(ssl_serialized_context_header);
Manuel Pégourié-Gonnard4c90e852019-07-11 10:58:10 +02006279 }
6280
6281 /*
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02006282 * Session (length + data)
6283 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01006284 ret = ssl_session_save(ssl->session, 1, NULL, 0, &session_len);
6285 if (ret != MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL) {
6286 return ret;
6287 }
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02006288
6289 used += 4 + session_len;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01006290 if (used <= buf_len) {
6291 MBEDTLS_PUT_UINT32_BE(session_len, p, 0);
Joe Subbiani2f98d792021-08-20 11:44:44 +01006292 p += 4;
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02006293
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01006294 ret = ssl_session_save(ssl->session, 1,
6295 p, session_len, &session_len);
6296 if (ret != 0) {
6297 return ret;
6298 }
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02006299
6300 p += session_len;
6301 }
6302
6303 /*
Manuel Pégourié-Gonnardc2a7b892019-07-15 09:04:11 +02006304 * Transform
6305 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01006306 used += sizeof(ssl->transform->randbytes);
6307 if (used <= buf_len) {
6308 memcpy(p, ssl->transform->randbytes,
6309 sizeof(ssl->transform->randbytes));
6310 p += sizeof(ssl->transform->randbytes);
Manuel Pégourié-Gonnardc2a7b892019-07-15 09:04:11 +02006311 }
6312
6313#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
6314 used += 2 + ssl->transform->in_cid_len + ssl->transform->out_cid_len;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01006315 if (used <= buf_len) {
Manuel Pégourié-Gonnardc2a7b892019-07-15 09:04:11 +02006316 *p++ = ssl->transform->in_cid_len;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01006317 memcpy(p, ssl->transform->in_cid, ssl->transform->in_cid_len);
Manuel Pégourié-Gonnardc2a7b892019-07-15 09:04:11 +02006318 p += ssl->transform->in_cid_len;
6319
6320 *p++ = ssl->transform->out_cid_len;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01006321 memcpy(p, ssl->transform->out_cid, ssl->transform->out_cid_len);
Manuel Pégourié-Gonnardc2a7b892019-07-15 09:04:11 +02006322 p += ssl->transform->out_cid_len;
6323 }
6324#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
6325
6326 /*
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02006327 * Saved fields from top-level ssl_context structure
6328 */
6329#if defined(MBEDTLS_SSL_DTLS_BADMAC_LIMIT)
6330 used += 4;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01006331 if (used <= buf_len) {
6332 MBEDTLS_PUT_UINT32_BE(ssl->badmac_seen, p, 0);
Joe Subbiani2f98d792021-08-20 11:44:44 +01006333 p += 4;
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02006334 }
6335#endif /* MBEDTLS_SSL_DTLS_BADMAC_LIMIT */
6336
6337#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
6338 used += 16;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01006339 if (used <= buf_len) {
6340 MBEDTLS_PUT_UINT64_BE(ssl->in_window_top, p, 0);
Joe Subbiani2f98d792021-08-20 11:44:44 +01006341 p += 8;
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02006342
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01006343 MBEDTLS_PUT_UINT64_BE(ssl->in_window, p, 0);
Joe Subbiani2f98d792021-08-20 11:44:44 +01006344 p += 8;
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02006345 }
6346#endif /* MBEDTLS_SSL_DTLS_ANTI_REPLAY */
6347
6348#if defined(MBEDTLS_SSL_PROTO_DTLS)
6349 used += 1;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01006350 if (used <= buf_len) {
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02006351 *p++ = ssl->disable_datagram_packing;
6352 }
6353#endif /* MBEDTLS_SSL_PROTO_DTLS */
6354
6355 used += 8;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01006356 if (used <= buf_len) {
6357 memcpy(p, ssl->cur_out_ctr, 8);
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02006358 p += 8;
6359 }
6360
6361#if defined(MBEDTLS_SSL_PROTO_DTLS)
6362 used += 2;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01006363 if (used <= buf_len) {
6364 MBEDTLS_PUT_UINT16_BE(ssl->mtu, p, 0);
Joe Subbiani2f98d792021-08-20 11:44:44 +01006365 p += 2;
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02006366 }
6367#endif /* MBEDTLS_SSL_PROTO_DTLS */
6368
6369#if defined(MBEDTLS_SSL_ALPN)
6370 {
6371 const uint8_t alpn_len = ssl->alpn_chosen
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01006372 ? (uint8_t) strlen(ssl->alpn_chosen)
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02006373 : 0;
6374
6375 used += 1 + alpn_len;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01006376 if (used <= buf_len) {
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02006377 *p++ = alpn_len;
6378
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01006379 if (ssl->alpn_chosen != NULL) {
6380 memcpy(p, ssl->alpn_chosen, alpn_len);
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02006381 p += alpn_len;
6382 }
6383 }
6384 }
6385#endif /* MBEDTLS_SSL_ALPN */
6386
6387 /*
Manuel Pégourié-Gonnard4c90e852019-07-11 10:58:10 +02006388 * Done
6389 */
6390 *olen = used;
6391
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01006392 if (used > buf_len) {
6393 return MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL;
6394 }
Manuel Pégourié-Gonnardac87e282019-05-28 13:02:16 +02006395
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01006396 MBEDTLS_SSL_DEBUG_BUF(4, "saved context", buf, used);
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02006397
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01006398 return mbedtls_ssl_session_reset_int(ssl, 0);
Manuel Pégourié-Gonnardac87e282019-05-28 13:02:16 +02006399}
6400
6401/*
Manuel Pégourié-Gonnardc2a7b892019-07-15 09:04:11 +02006402 * Helper to get TLS 1.2 PRF from ciphersuite
6403 * (Duplicates bits of logic from ssl_set_handshake_prfs().)
6404 */
Andrzej Kurekf9412f72022-10-18 07:30:19 -04006405#if defined(MBEDTLS_SHA256_C) || \
6406 (defined(MBEDTLS_SHA512_C) && !defined(MBEDTLS_SHA512_NO_SHA384))
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01006407typedef int (*tls_prf_fn)(const unsigned char *secret, size_t slen,
6408 const char *label,
6409 const unsigned char *random, size_t rlen,
6410 unsigned char *dstbuf, size_t dlen);
6411static tls_prf_fn ssl_tls12prf_from_cs(int ciphersuite_id)
Manuel Pégourié-Gonnardc2a7b892019-07-15 09:04:11 +02006412{
6413 const mbedtls_ssl_ciphersuite_t * const ciphersuite_info =
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01006414 mbedtls_ssl_ciphersuite_from_id(ciphersuite_id);
Manuel Pégourié-Gonnardc2a7b892019-07-15 09:04:11 +02006415
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01006416 if (ciphersuite_info == NULL) {
6417 return NULL;
6418 }
Andrzej Kurek84fc52c2022-10-25 04:18:30 -04006419
6420#if defined(MBEDTLS_SHA512_C) && !defined(MBEDTLS_SHA512_NO_SHA384)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01006421 if (ciphersuite_info->mac == MBEDTLS_MD_SHA384) {
6422 return tls_prf_sha384;
6423 } else
Manuel Pégourié-Gonnard9a96fd72019-07-23 17:11:24 +02006424#endif
Andrzej Kurekf9412f72022-10-18 07:30:19 -04006425#if defined(MBEDTLS_SHA256_C)
6426 {
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01006427 if (ciphersuite_info->mac == MBEDTLS_MD_SHA256) {
6428 return tls_prf_sha256;
6429 }
Andrzej Kurekf9412f72022-10-18 07:30:19 -04006430 }
6431#endif
6432#if !defined(MBEDTLS_SHA256_C) && \
6433 (!defined(MBEDTLS_SHA512_C) || defined(MBEDTLS_SHA512_NO_SHA384))
6434 (void) ciphersuite_info;
6435#endif
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01006436 return NULL;
Manuel Pégourié-Gonnardc2a7b892019-07-15 09:04:11 +02006437}
6438
Andrzej Kurekf9412f72022-10-18 07:30:19 -04006439#endif /* MBEDTLS_SHA256_C ||
6440 (MBEDTLS_SHA512_C && !MBEDTLS_SHA512_NO_SHA384) */
6441
Manuel Pégourié-Gonnardc2a7b892019-07-15 09:04:11 +02006442/*
Manuel Pégourié-Gonnardb9dfc9f2019-07-12 10:50:19 +02006443 * Deserialize context, see mbedtls_ssl_context_save() for format.
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02006444 *
6445 * This internal version is wrapped by a public function that cleans up in
6446 * case of error.
Manuel Pégourié-Gonnardac87e282019-05-28 13:02:16 +02006447 */
Manuel Pégourié-Gonnardd904d662022-06-17 10:24:00 +02006448MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01006449static int ssl_context_load(mbedtls_ssl_context *ssl,
6450 const unsigned char *buf,
6451 size_t len)
Manuel Pégourié-Gonnardac87e282019-05-28 13:02:16 +02006452{
Manuel Pégourié-Gonnard4c90e852019-07-11 10:58:10 +02006453 const unsigned char *p = buf;
6454 const unsigned char * const end = buf + len;
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02006455 size_t session_len;
Janos Follath865b3eb2019-12-16 11:46:15 +00006456 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Andrzej Kurekf9412f72022-10-18 07:30:19 -04006457 tls_prf_fn prf_func = NULL;
Manuel Pégourié-Gonnard4c90e852019-07-11 10:58:10 +02006458
Manuel Pégourié-Gonnard0ff76402019-07-11 09:56:30 +02006459 /*
6460 * The context should have been freshly setup or reset.
6461 * Give the user an error in case of obvious misuse.
Manuel Pégourié-Gonnard4ca930f2019-07-26 16:31:53 +02006462 * (Checking session is useful because it won't be NULL if we're
Manuel Pégourié-Gonnard0ff76402019-07-11 09:56:30 +02006463 * renegotiating, or if the user mistakenly loaded a session first.)
6464 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01006465 if (ssl->state != MBEDTLS_SSL_HELLO_REQUEST ||
6466 ssl->session != NULL) {
6467 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
Manuel Pégourié-Gonnard0ff76402019-07-11 09:56:30 +02006468 }
6469
6470 /*
6471 * We can't check that the config matches the initial one, but we can at
6472 * least check it matches the requirements for serializing.
6473 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01006474 if (ssl->conf->transport != MBEDTLS_SSL_TRANSPORT_DATAGRAM ||
Manuel Pégourié-Gonnard0ff76402019-07-11 09:56:30 +02006475 ssl->conf->max_major_ver < MBEDTLS_SSL_MAJOR_VERSION_3 ||
6476 ssl->conf->min_major_ver > MBEDTLS_SSL_MAJOR_VERSION_3 ||
6477 ssl->conf->max_minor_ver < MBEDTLS_SSL_MINOR_VERSION_3 ||
6478 ssl->conf->min_minor_ver > MBEDTLS_SSL_MINOR_VERSION_3 ||
6479#if defined(MBEDTLS_SSL_RENEGOTIATION)
Manuel Pégourié-Gonnard9a96fd72019-07-23 17:11:24 +02006480 ssl->conf->disable_renegotiation != MBEDTLS_SSL_RENEGOTIATION_DISABLED ||
Manuel Pégourié-Gonnard0ff76402019-07-11 09:56:30 +02006481#endif
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01006482 0) {
6483 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
Manuel Pégourié-Gonnard0ff76402019-07-11 09:56:30 +02006484 }
6485
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01006486 MBEDTLS_SSL_DEBUG_BUF(4, "context to load", buf, len);
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02006487
Manuel Pégourié-Gonnard4c90e852019-07-11 10:58:10 +02006488 /*
6489 * Check version identifier
6490 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01006491 if ((size_t) (end - p) < sizeof(ssl_serialized_context_header)) {
6492 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
Manuel Pégourié-Gonnard4c90e852019-07-11 10:58:10 +02006493 }
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01006494
6495 if (memcmp(p, ssl_serialized_context_header,
6496 sizeof(ssl_serialized_context_header)) != 0) {
6497 return MBEDTLS_ERR_SSL_VERSION_MISMATCH;
6498 }
6499 p += sizeof(ssl_serialized_context_header);
Manuel Pégourié-Gonnard4c90e852019-07-11 10:58:10 +02006500
6501 /*
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02006502 * Session
6503 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01006504 if ((size_t) (end - p) < 4) {
6505 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
6506 }
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02006507
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01006508 session_len = ((size_t) p[0] << 24) |
6509 ((size_t) p[1] << 16) |
6510 ((size_t) p[2] << 8) |
6511 ((size_t) p[3]);
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02006512 p += 4;
6513
Manuel Pégourié-Gonnard142ba732019-07-23 14:43:30 +02006514 /* This has been allocated by ssl_handshake_init(), called by
Hanno Becker43aefe22020-02-05 10:44:56 +00006515 * by either mbedtls_ssl_session_reset_int() or mbedtls_ssl_setup(). */
Manuel Pégourié-Gonnard142ba732019-07-23 14:43:30 +02006516 ssl->session = ssl->session_negotiate;
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02006517 ssl->session_in = ssl->session;
6518 ssl->session_out = ssl->session;
Manuel Pégourié-Gonnard142ba732019-07-23 14:43:30 +02006519 ssl->session_negotiate = NULL;
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02006520
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01006521 if ((size_t) (end - p) < session_len) {
6522 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
6523 }
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02006524
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01006525 ret = ssl_session_load(ssl->session, 1, p, session_len);
6526 if (ret != 0) {
6527 mbedtls_ssl_session_free(ssl->session);
6528 return ret;
Manuel Pégourié-Gonnard45ac1f02019-07-23 16:52:45 +02006529 }
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02006530
6531 p += session_len;
6532
6533 /*
Manuel Pégourié-Gonnardc2a7b892019-07-15 09:04:11 +02006534 * Transform
6535 */
6536
Manuel Pégourié-Gonnard142ba732019-07-23 14:43:30 +02006537 /* This has been allocated by ssl_handshake_init(), called by
Hanno Becker43aefe22020-02-05 10:44:56 +00006538 * by either mbedtls_ssl_session_reset_int() or mbedtls_ssl_setup(). */
Manuel Pégourié-Gonnard142ba732019-07-23 14:43:30 +02006539 ssl->transform = ssl->transform_negotiate;
Manuel Pégourié-Gonnardc2a7b892019-07-15 09:04:11 +02006540 ssl->transform_in = ssl->transform;
6541 ssl->transform_out = ssl->transform;
Manuel Pégourié-Gonnard142ba732019-07-23 14:43:30 +02006542 ssl->transform_negotiate = NULL;
Manuel Pégourié-Gonnardc2a7b892019-07-15 09:04:11 +02006543
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01006544 prf_func = ssl_tls12prf_from_cs(ssl->session->ciphersuite);
6545 if (prf_func == NULL) {
6546 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
6547 }
Andrzej Kurekf9412f72022-10-18 07:30:19 -04006548
Manuel Pégourié-Gonnardc2a7b892019-07-15 09:04:11 +02006549 /* Read random bytes and populate structure */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01006550 if ((size_t) (end - p) < sizeof(ssl->transform->randbytes)) {
6551 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
6552 }
Manuel Pégourié-Gonnardc2a7b892019-07-15 09:04:11 +02006553
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01006554 ret = ssl_populate_transform(ssl->transform,
6555 ssl->session->ciphersuite,
6556 ssl->session->master,
Manuel Pégourié-Gonnardc2a7b892019-07-15 09:04:11 +02006557#if defined(MBEDTLS_SSL_SOME_MODES_USE_MAC)
6558#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01006559 ssl->session->encrypt_then_mac,
Manuel Pégourié-Gonnardc2a7b892019-07-15 09:04:11 +02006560#endif
6561#if defined(MBEDTLS_SSL_TRUNCATED_HMAC)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01006562 ssl->session->trunc_hmac,
Manuel Pégourié-Gonnardc2a7b892019-07-15 09:04:11 +02006563#endif
6564#endif /* MBEDTLS_SSL_SOME_MODES_USE_MAC */
6565#if defined(MBEDTLS_ZLIB_SUPPORT)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01006566 ssl->session->compression,
Manuel Pégourié-Gonnardc2a7b892019-07-15 09:04:11 +02006567#endif
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01006568 prf_func,
6569 p, /* currently pointing to randbytes */
6570 MBEDTLS_SSL_MINOR_VERSION_3, /* (D)TLS 1.2 is forced */
6571 ssl->conf->endpoint,
6572 ssl);
6573 if (ret != 0) {
6574 return ret;
6575 }
Manuel Pégourié-Gonnardc2a7b892019-07-15 09:04:11 +02006576
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01006577 p += sizeof(ssl->transform->randbytes);
Manuel Pégourié-Gonnardc2a7b892019-07-15 09:04:11 +02006578
6579#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
6580 /* Read connection IDs and store them */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01006581 if ((size_t) (end - p) < 1) {
6582 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
6583 }
Manuel Pégourié-Gonnardc2a7b892019-07-15 09:04:11 +02006584
6585 ssl->transform->in_cid_len = *p++;
6586
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01006587 if ((size_t) (end - p) < ssl->transform->in_cid_len + 1u) {
6588 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
6589 }
Manuel Pégourié-Gonnardc2a7b892019-07-15 09:04:11 +02006590
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01006591 memcpy(ssl->transform->in_cid, p, ssl->transform->in_cid_len);
Manuel Pégourié-Gonnardc2a7b892019-07-15 09:04:11 +02006592 p += ssl->transform->in_cid_len;
6593
6594 ssl->transform->out_cid_len = *p++;
6595
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01006596 if ((size_t) (end - p) < ssl->transform->out_cid_len) {
6597 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
6598 }
Manuel Pégourié-Gonnardc2a7b892019-07-15 09:04:11 +02006599
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01006600 memcpy(ssl->transform->out_cid, p, ssl->transform->out_cid_len);
Manuel Pégourié-Gonnardc2a7b892019-07-15 09:04:11 +02006601 p += ssl->transform->out_cid_len;
6602#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
6603
6604 /*
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02006605 * Saved fields from top-level ssl_context structure
6606 */
6607#if defined(MBEDTLS_SSL_DTLS_BADMAC_LIMIT)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01006608 if ((size_t) (end - p) < 4) {
6609 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
6610 }
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02006611
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01006612 ssl->badmac_seen = ((uint32_t) p[0] << 24) |
6613 ((uint32_t) p[1] << 16) |
6614 ((uint32_t) p[2] << 8) |
6615 ((uint32_t) p[3]);
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02006616 p += 4;
6617#endif /* MBEDTLS_SSL_DTLS_BADMAC_LIMIT */
6618
6619#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01006620 if ((size_t) (end - p) < 16) {
6621 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
6622 }
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02006623
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01006624 ssl->in_window_top = ((uint64_t) p[0] << 56) |
6625 ((uint64_t) p[1] << 48) |
6626 ((uint64_t) p[2] << 40) |
6627 ((uint64_t) p[3] << 32) |
6628 ((uint64_t) p[4] << 24) |
6629 ((uint64_t) p[5] << 16) |
6630 ((uint64_t) p[6] << 8) |
6631 ((uint64_t) p[7]);
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02006632 p += 8;
6633
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01006634 ssl->in_window = ((uint64_t) p[0] << 56) |
6635 ((uint64_t) p[1] << 48) |
6636 ((uint64_t) p[2] << 40) |
6637 ((uint64_t) p[3] << 32) |
6638 ((uint64_t) p[4] << 24) |
6639 ((uint64_t) p[5] << 16) |
6640 ((uint64_t) p[6] << 8) |
6641 ((uint64_t) p[7]);
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02006642 p += 8;
6643#endif /* MBEDTLS_SSL_DTLS_ANTI_REPLAY */
6644
6645#if defined(MBEDTLS_SSL_PROTO_DTLS)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01006646 if ((size_t) (end - p) < 1) {
6647 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
6648 }
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02006649
6650 ssl->disable_datagram_packing = *p++;
6651#endif /* MBEDTLS_SSL_PROTO_DTLS */
6652
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01006653 if ((size_t) (end - p) < 8) {
6654 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
6655 }
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02006656
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01006657 memcpy(ssl->cur_out_ctr, p, 8);
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02006658 p += 8;
6659
6660#if defined(MBEDTLS_SSL_PROTO_DTLS)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01006661 if ((size_t) (end - p) < 2) {
6662 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
6663 }
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02006664
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01006665 ssl->mtu = (p[0] << 8) | p[1];
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02006666 p += 2;
6667#endif /* MBEDTLS_SSL_PROTO_DTLS */
6668
6669#if defined(MBEDTLS_SSL_ALPN)
6670 {
6671 uint8_t alpn_len;
6672 const char **cur;
6673
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01006674 if ((size_t) (end - p) < 1) {
6675 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
6676 }
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02006677
6678 alpn_len = *p++;
6679
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01006680 if (alpn_len != 0 && ssl->conf->alpn_list != NULL) {
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02006681 /* alpn_chosen should point to an item in the configured list */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01006682 for (cur = ssl->conf->alpn_list; *cur != NULL; cur++) {
6683 if (strlen(*cur) == alpn_len &&
Waleed Elmelegy98ebf482024-03-15 14:29:24 +00006684 memcmp(p, *cur, alpn_len) == 0) {
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02006685 ssl->alpn_chosen = *cur;
6686 break;
6687 }
6688 }
6689 }
6690
6691 /* can only happen on conf mismatch */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01006692 if (alpn_len != 0 && ssl->alpn_chosen == NULL) {
6693 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
6694 }
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02006695
6696 p += alpn_len;
6697 }
6698#endif /* MBEDTLS_SSL_ALPN */
6699
6700 /*
Manuel Pégourié-Gonnard0eb3eac2019-07-15 11:53:51 +02006701 * Forced fields from top-level ssl_context structure
6702 *
6703 * Most of them already set to the correct value by mbedtls_ssl_init() and
6704 * mbedtls_ssl_reset(), so we only need to set the remaining ones.
6705 */
6706 ssl->state = MBEDTLS_SSL_HANDSHAKE_OVER;
6707
6708 ssl->major_ver = MBEDTLS_SSL_MAJOR_VERSION_3;
6709 ssl->minor_ver = MBEDTLS_SSL_MINOR_VERSION_3;
6710
Hanno Becker361b10d2019-08-30 10:42:49 +01006711 /* Adjust pointers for header fields of outgoing records to
6712 * the given transform, accounting for explicit IV and CID. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01006713 mbedtls_ssl_update_out_pointers(ssl, ssl->transform);
Hanno Becker361b10d2019-08-30 10:42:49 +01006714
Manuel Pégourié-Gonnard0eb3eac2019-07-15 11:53:51 +02006715#if defined(MBEDTLS_SSL_PROTO_DTLS)
6716 ssl->in_epoch = 1;
6717#endif
6718
6719 /* mbedtls_ssl_reset() leaves the handshake sub-structure allocated,
6720 * which we don't want - otherwise we'd end up freeing the wrong transform
Hanno Beckerce5f5fd2020-02-05 10:47:44 +00006721 * by calling mbedtls_ssl_handshake_wrapup_free_hs_transform()
6722 * inappropriately. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01006723 if (ssl->handshake != NULL) {
6724 mbedtls_ssl_handshake_free(ssl);
6725 mbedtls_free(ssl->handshake);
Manuel Pégourié-Gonnard0eb3eac2019-07-15 11:53:51 +02006726 ssl->handshake = NULL;
6727 }
6728
6729 /*
Manuel Pégourié-Gonnard4c90e852019-07-11 10:58:10 +02006730 * Done - should have consumed entire buffer
6731 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01006732 if (p != end) {
6733 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
6734 }
Manuel Pégourié-Gonnardac87e282019-05-28 13:02:16 +02006735
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01006736 return 0;
Manuel Pégourié-Gonnardac87e282019-05-28 13:02:16 +02006737}
6738
6739/*
Manuel Pégourié-Gonnardb9dfc9f2019-07-12 10:50:19 +02006740 * Deserialize context: public wrapper for error cleaning
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02006741 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01006742int mbedtls_ssl_context_load(mbedtls_ssl_context *context,
6743 const unsigned char *buf,
6744 size_t len)
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02006745{
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01006746 int ret = ssl_context_load(context, buf, len);
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02006747
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01006748 if (ret != 0) {
6749 mbedtls_ssl_free(context);
6750 }
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02006751
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01006752 return ret;
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02006753}
Manuel Pégourié-Gonnard5c0e3772019-07-23 16:13:17 +02006754#endif /* MBEDTLS_SSL_CONTEXT_SERIALIZATION */
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02006755
6756/*
Paul Bakker5121ce52009-01-03 21:22:43 +00006757 * Free an SSL context
6758 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01006759void mbedtls_ssl_free(mbedtls_ssl_context *ssl)
Paul Bakker5121ce52009-01-03 21:22:43 +00006760{
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01006761 if (ssl == NULL) {
Paul Bakkeraccaffe2014-06-26 13:37:14 +02006762 return;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01006763 }
Paul Bakkeraccaffe2014-06-26 13:37:14 +02006764
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01006765 MBEDTLS_SSL_DEBUG_MSG(2, ("=> free"));
Paul Bakker5121ce52009-01-03 21:22:43 +00006766
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01006767 if (ssl->out_buf != NULL) {
sander-visserb8aa2072020-05-06 22:05:13 +02006768#if defined(MBEDTLS_SSL_VARIABLE_BUFFER_LENGTH)
6769 size_t out_buf_len = ssl->out_buf_len;
6770#else
6771 size_t out_buf_len = MBEDTLS_SSL_OUT_BUFFER_LEN;
6772#endif
6773
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01006774 mbedtls_platform_zeroize(ssl->out_buf, out_buf_len);
6775 mbedtls_free(ssl->out_buf);
Andrzej Kurek0afa2a12020-03-03 10:39:58 -05006776 ssl->out_buf = NULL;
Paul Bakker5121ce52009-01-03 21:22:43 +00006777 }
6778
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01006779 if (ssl->in_buf != NULL) {
sander-visserb8aa2072020-05-06 22:05:13 +02006780#if defined(MBEDTLS_SSL_VARIABLE_BUFFER_LENGTH)
6781 size_t in_buf_len = ssl->in_buf_len;
6782#else
6783 size_t in_buf_len = MBEDTLS_SSL_IN_BUFFER_LEN;
6784#endif
6785
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01006786 mbedtls_platform_zeroize(ssl->in_buf, in_buf_len);
6787 mbedtls_free(ssl->in_buf);
Andrzej Kurek0afa2a12020-03-03 10:39:58 -05006788 ssl->in_buf = NULL;
Paul Bakker5121ce52009-01-03 21:22:43 +00006789 }
6790
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006791#if defined(MBEDTLS_ZLIB_SUPPORT)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01006792 if (ssl->compress_buf != NULL) {
6793 mbedtls_platform_zeroize(ssl->compress_buf, MBEDTLS_SSL_COMPRESS_BUFFER_LEN);
6794 mbedtls_free(ssl->compress_buf);
Paul Bakker16770332013-10-11 09:59:44 +02006795 }
6796#endif
6797
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01006798 if (ssl->transform) {
6799 mbedtls_ssl_transform_free(ssl->transform);
6800 mbedtls_free(ssl->transform);
Paul Bakker48916f92012-09-16 19:57:18 +00006801 }
6802
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01006803 if (ssl->handshake) {
6804 mbedtls_ssl_handshake_free(ssl);
6805 mbedtls_ssl_transform_free(ssl->transform_negotiate);
6806 mbedtls_ssl_session_free(ssl->session_negotiate);
Paul Bakker48916f92012-09-16 19:57:18 +00006807
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01006808 mbedtls_free(ssl->handshake);
6809 mbedtls_free(ssl->transform_negotiate);
6810 mbedtls_free(ssl->session_negotiate);
Paul Bakker48916f92012-09-16 19:57:18 +00006811 }
6812
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01006813 if (ssl->session) {
6814 mbedtls_ssl_session_free(ssl->session);
6815 mbedtls_free(ssl->session);
Paul Bakkerc0463502013-02-14 11:19:38 +01006816 }
6817
Manuel Pégourié-Gonnard55fab2d2015-05-11 16:15:19 +02006818#if defined(MBEDTLS_X509_CRT_PARSE_C)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01006819 if (ssl->hostname != NULL) {
6820 mbedtls_platform_zeroize(ssl->hostname, strlen(ssl->hostname));
6821 mbedtls_free(ssl->hostname);
Paul Bakker5121ce52009-01-03 21:22:43 +00006822 }
Paul Bakker0be444a2013-08-27 21:55:01 +02006823#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00006824
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006825#if defined(MBEDTLS_SSL_HW_RECORD_ACCEL)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01006826 if (mbedtls_ssl_hw_record_finish != NULL) {
6827 MBEDTLS_SSL_DEBUG_MSG(2, ("going for mbedtls_ssl_hw_record_finish()"));
6828 mbedtls_ssl_hw_record_finish(ssl);
Paul Bakker05ef8352012-05-08 09:17:57 +00006829 }
6830#endif
6831
Manuel Pégourié-Gonnarde057d3b2015-05-20 10:59:43 +02006832#if defined(MBEDTLS_SSL_DTLS_HELLO_VERIFY) && defined(MBEDTLS_SSL_SRV_C)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01006833 mbedtls_free(ssl->cli_id);
Manuel Pégourié-Gonnard43c02182014-07-22 17:32:01 +02006834#endif
6835
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01006836 MBEDTLS_SSL_DEBUG_MSG(2, ("<= free"));
Paul Bakker2da561c2009-02-05 18:00:28 +00006837
Paul Bakker86f04f42013-02-14 11:20:09 +01006838 /* Actually clear after last debug message */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01006839 mbedtls_platform_zeroize(ssl, sizeof(mbedtls_ssl_context));
Paul Bakker5121ce52009-01-03 21:22:43 +00006840}
6841
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02006842/*
Tom Cosgrove49f99bc2022-12-04 16:44:21 +00006843 * Initialize mbedtls_ssl_config
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02006844 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01006845void mbedtls_ssl_config_init(mbedtls_ssl_config *conf)
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02006846{
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01006847 memset(conf, 0, sizeof(mbedtls_ssl_config));
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02006848}
6849
Gilles Peskineeccd8882020-03-10 12:19:08 +01006850#if defined(MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED)
Chien Wongb6d57932024-02-07 21:48:12 +08006851static const int ssl_preset_default_hashes[] = {
Gilles Peskinec54010c2021-05-12 22:52:09 +02006852#if defined(MBEDTLS_SHA512_C)
Manuel Pégourié-Gonnard47229c72015-12-04 15:02:56 +01006853 MBEDTLS_MD_SHA512,
Gilles Peskinec54010c2021-05-12 22:52:09 +02006854#endif
6855#if defined(MBEDTLS_SHA512_C) && !defined(MBEDTLS_SHA512_NO_SHA384)
Manuel Pégourié-Gonnard47229c72015-12-04 15:02:56 +01006856 MBEDTLS_MD_SHA384,
6857#endif
6858#if defined(MBEDTLS_SHA256_C)
6859 MBEDTLS_MD_SHA256,
6860 MBEDTLS_MD_SHA224,
6861#endif
Gilles Peskine5d2511c2017-05-12 13:16:40 +02006862#if defined(MBEDTLS_SHA1_C) && defined(MBEDTLS_TLS_DEFAULT_ALLOW_SHA1_IN_KEY_EXCHANGE)
Manuel Pégourié-Gonnard47229c72015-12-04 15:02:56 +01006863 MBEDTLS_MD_SHA1,
6864#endif
6865 MBEDTLS_MD_NONE
6866};
Simon Butcherc97b6972015-12-27 23:48:17 +00006867#endif
Manuel Pégourié-Gonnard47229c72015-12-04 15:02:56 +01006868
Chien Wongb6d57932024-02-07 21:48:12 +08006869static const int ssl_preset_suiteb_ciphersuites[] = {
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02006870 MBEDTLS_TLS_ECDHE_ECDSA_WITH_AES_128_GCM_SHA256,
6871 MBEDTLS_TLS_ECDHE_ECDSA_WITH_AES_256_GCM_SHA384,
6872 0
6873};
6874
Gilles Peskineeccd8882020-03-10 12:19:08 +01006875#if defined(MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED)
Chien Wongb6d57932024-02-07 21:48:12 +08006876static const int ssl_preset_suiteb_hashes[] = {
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02006877 MBEDTLS_MD_SHA256,
6878 MBEDTLS_MD_SHA384,
6879 MBEDTLS_MD_NONE
6880};
6881#endif
6882
6883#if defined(MBEDTLS_ECP_C)
Chien Wongb6d57932024-02-07 21:48:12 +08006884static const mbedtls_ecp_group_id ssl_preset_suiteb_curves[] = {
Jaeden Amerod4311042019-06-03 08:27:16 +01006885#if defined(MBEDTLS_ECP_DP_SECP256R1_ENABLED)
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02006886 MBEDTLS_ECP_DP_SECP256R1,
Jaeden Amerod4311042019-06-03 08:27:16 +01006887#endif
6888#if defined(MBEDTLS_ECP_DP_SECP384R1_ENABLED)
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02006889 MBEDTLS_ECP_DP_SECP384R1,
Jaeden Amerod4311042019-06-03 08:27:16 +01006890#endif
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02006891 MBEDTLS_ECP_DP_NONE
6892};
6893#endif
6894
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02006895/*
Tillmann Karras588ad502015-09-25 04:27:22 +02006896 * Load default in mbedtls_ssl_config
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02006897 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01006898int mbedtls_ssl_config_defaults(mbedtls_ssl_config *conf,
6899 int endpoint, int transport, int preset)
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02006900{
Manuel Pégourié-Gonnard8b431fb2015-05-11 12:54:52 +02006901#if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_SSL_SRV_C)
Janos Follath865b3eb2019-12-16 11:46:15 +00006902 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnard8b431fb2015-05-11 12:54:52 +02006903#endif
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02006904
Manuel Pégourié-Gonnard0de074f2015-05-14 12:58:01 +02006905 /* Use the functions here so that they are covered in tests,
6906 * but otherwise access member directly for efficiency */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01006907 mbedtls_ssl_conf_endpoint(conf, endpoint);
6908 mbedtls_ssl_conf_transport(conf, transport);
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02006909
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02006910 /*
6911 * Things that are common to all presets
6912 */
Manuel Pégourié-Gonnard419d5ae2015-05-04 19:32:36 +02006913#if defined(MBEDTLS_SSL_CLI_C)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01006914 if (endpoint == MBEDTLS_SSL_IS_CLIENT) {
Manuel Pégourié-Gonnard419d5ae2015-05-04 19:32:36 +02006915 conf->authmode = MBEDTLS_SSL_VERIFY_REQUIRED;
6916#if defined(MBEDTLS_SSL_SESSION_TICKETS)
6917 conf->session_tickets = MBEDTLS_SSL_SESSION_TICKETS_ENABLED;
6918#endif
6919 }
6920#endif
6921
Manuel Pégourié-Gonnard66dc5552015-05-14 12:28:21 +02006922#if defined(MBEDTLS_ARC4_C)
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02006923 conf->arc4_disabled = MBEDTLS_SSL_ARC4_DISABLED;
Manuel Pégourié-Gonnard66dc5552015-05-14 12:28:21 +02006924#endif
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02006925
6926#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
6927 conf->encrypt_then_mac = MBEDTLS_SSL_ETM_ENABLED;
6928#endif
6929
6930#if defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET)
6931 conf->extended_ms = MBEDTLS_SSL_EXTENDED_MS_ENABLED;
6932#endif
6933
Manuel Pégourié-Gonnard17eab2b2015-05-05 16:34:53 +01006934#if defined(MBEDTLS_SSL_CBC_RECORD_SPLITTING)
6935 conf->cbc_record_splitting = MBEDTLS_SSL_CBC_RECORD_SPLITTING_ENABLED;
6936#endif
6937
Manuel Pégourié-Gonnarde057d3b2015-05-20 10:59:43 +02006938#if defined(MBEDTLS_SSL_DTLS_HELLO_VERIFY) && defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02006939 conf->f_cookie_write = ssl_cookie_write_dummy;
6940 conf->f_cookie_check = ssl_cookie_check_dummy;
6941#endif
6942
6943#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
6944 conf->anti_replay = MBEDTLS_SSL_ANTI_REPLAY_ENABLED;
6945#endif
6946
Janos Follath088ce432017-04-10 12:42:31 +01006947#if defined(MBEDTLS_SSL_SRV_C)
6948 conf->cert_req_ca_list = MBEDTLS_SSL_CERT_REQ_CA_LIST_ENABLED;
6949#endif
6950
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02006951#if defined(MBEDTLS_SSL_PROTO_DTLS)
6952 conf->hs_timeout_min = MBEDTLS_SSL_DTLS_TIMEOUT_DFL_MIN;
6953 conf->hs_timeout_max = MBEDTLS_SSL_DTLS_TIMEOUT_DFL_MAX;
6954#endif
6955
6956#if defined(MBEDTLS_SSL_RENEGOTIATION)
6957 conf->renego_max_records = MBEDTLS_SSL_RENEGO_MAX_RECORDS_DEFAULT;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01006958 memset(conf->renego_period, 0x00, 2);
6959 memset(conf->renego_period + 2, 0xFF, 6);
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02006960#endif
6961
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02006962#if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_SSL_SRV_C)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01006963 if (endpoint == MBEDTLS_SSL_IS_SERVER) {
6964 const unsigned char dhm_p[] =
6965 MBEDTLS_DHM_RFC3526_MODP_2048_P_BIN;
6966 const unsigned char dhm_g[] =
6967 MBEDTLS_DHM_RFC3526_MODP_2048_G_BIN;
Hanno Becker00d0a682017-10-04 13:14:29 +01006968
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01006969 if ((ret = mbedtls_ssl_conf_dh_param_bin(conf,
6970 dhm_p, sizeof(dhm_p),
6971 dhm_g, sizeof(dhm_g))) != 0) {
6972 return ret;
6973 }
6974 }
Manuel Pégourié-Gonnardbd990d62015-06-11 14:49:42 +02006975#endif
6976
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02006977 /*
6978 * Preset-specific defaults
6979 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01006980 switch (preset) {
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02006981 /*
6982 * NSA Suite B
6983 */
6984 case MBEDTLS_SSL_PRESET_SUITEB:
6985 conf->min_major_ver = MBEDTLS_SSL_MAJOR_VERSION_3;
6986 conf->min_minor_ver = MBEDTLS_SSL_MINOR_VERSION_3; /* TLS 1.2 */
6987 conf->max_major_ver = MBEDTLS_SSL_MAX_MAJOR_VERSION;
6988 conf->max_minor_ver = MBEDTLS_SSL_MAX_MINOR_VERSION;
6989
6990 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_0] =
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01006991 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_1] =
6992 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_2] =
6993 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_3] =
6994 ssl_preset_suiteb_ciphersuites;
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02006995
6996#if defined(MBEDTLS_X509_CRT_PARSE_C)
6997 conf->cert_profile = &mbedtls_x509_crt_profile_suiteb;
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02006998#endif
6999
Gilles Peskineeccd8882020-03-10 12:19:08 +01007000#if defined(MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED)
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02007001 conf->sig_hashes = ssl_preset_suiteb_hashes;
7002#endif
7003
7004#if defined(MBEDTLS_ECP_C)
7005 conf->curve_list = ssl_preset_suiteb_curves;
7006#endif
Manuel Pégourié-Gonnardc98204e2015-08-11 04:21:01 +02007007 break;
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02007008
7009 /*
7010 * Default
7011 */
7012 default:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01007013 conf->min_major_ver = (MBEDTLS_SSL_MIN_MAJOR_VERSION >
7014 MBEDTLS_SSL_MIN_VALID_MAJOR_VERSION) ?
7015 MBEDTLS_SSL_MIN_MAJOR_VERSION :
7016 MBEDTLS_SSL_MIN_VALID_MAJOR_VERSION;
7017 conf->min_minor_ver = (MBEDTLS_SSL_MIN_MINOR_VERSION >
7018 MBEDTLS_SSL_MIN_VALID_MINOR_VERSION) ?
7019 MBEDTLS_SSL_MIN_MINOR_VERSION :
7020 MBEDTLS_SSL_MIN_VALID_MINOR_VERSION;
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02007021 conf->max_major_ver = MBEDTLS_SSL_MAX_MAJOR_VERSION;
7022 conf->max_minor_ver = MBEDTLS_SSL_MAX_MINOR_VERSION;
7023
7024#if defined(MBEDTLS_SSL_PROTO_DTLS)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01007025 if (transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM) {
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02007026 conf->min_minor_ver = MBEDTLS_SSL_MINOR_VERSION_2;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01007027 }
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02007028#endif
7029
7030 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_0] =
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01007031 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_1] =
7032 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_2] =
7033 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_3] =
7034 mbedtls_ssl_list_ciphersuites();
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02007035
7036#if defined(MBEDTLS_X509_CRT_PARSE_C)
7037 conf->cert_profile = &mbedtls_x509_crt_profile_default;
7038#endif
7039
Gilles Peskineeccd8882020-03-10 12:19:08 +01007040#if defined(MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED)
Manuel Pégourié-Gonnard47229c72015-12-04 15:02:56 +01007041 conf->sig_hashes = ssl_preset_default_hashes;
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02007042#endif
7043
7044#if defined(MBEDTLS_ECP_C)
7045 conf->curve_list = mbedtls_ecp_grp_id_list();
7046#endif
7047
7048#if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_SSL_CLI_C)
7049 conf->dhm_min_bitlen = 1024;
7050#endif
7051 }
7052
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01007053 return 0;
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02007054}
7055
7056/*
7057 * Free mbedtls_ssl_config
7058 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01007059void mbedtls_ssl_config_free(mbedtls_ssl_config *conf)
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02007060{
7061#if defined(MBEDTLS_DHM_C)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01007062 mbedtls_mpi_free(&conf->dhm_P);
7063 mbedtls_mpi_free(&conf->dhm_G);
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02007064#endif
7065
Gilles Peskineeccd8882020-03-10 12:19:08 +01007066#if defined(MBEDTLS_KEY_EXCHANGE_SOME_PSK_ENABLED)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01007067 if (conf->psk != NULL) {
7068 mbedtls_platform_zeroize(conf->psk, conf->psk_len);
7069 mbedtls_free(conf->psk);
Azim Khan27e8a122018-03-21 14:24:11 +00007070 conf->psk = NULL;
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02007071 conf->psk_len = 0;
junyeonLEE316b1622017-12-20 16:29:30 +09007072 }
7073
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01007074 if (conf->psk_identity != NULL) {
7075 mbedtls_platform_zeroize(conf->psk_identity, conf->psk_identity_len);
7076 mbedtls_free(conf->psk_identity);
Azim Khan27e8a122018-03-21 14:24:11 +00007077 conf->psk_identity = NULL;
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02007078 conf->psk_identity_len = 0;
7079 }
7080#endif
7081
7082#if defined(MBEDTLS_X509_CRT_PARSE_C)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01007083 ssl_key_cert_free(conf->key_cert);
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02007084#endif
7085
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01007086 mbedtls_platform_zeroize(conf, sizeof(mbedtls_ssl_config));
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02007087}
7088
Manuel Pégourié-Gonnard5674a972015-10-19 15:14:03 +02007089#if defined(MBEDTLS_PK_C) && \
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01007090 (defined(MBEDTLS_RSA_C) || defined(MBEDTLS_ECDSA_C))
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02007091/*
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007092 * Convert between MBEDTLS_PK_XXX and SSL_SIG_XXX
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02007093 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01007094unsigned char mbedtls_ssl_sig_from_pk(mbedtls_pk_context *pk)
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02007095{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007096#if defined(MBEDTLS_RSA_C)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01007097 if (mbedtls_pk_can_do(pk, MBEDTLS_PK_RSA)) {
7098 return MBEDTLS_SSL_SIG_RSA;
7099 }
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02007100#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007101#if defined(MBEDTLS_ECDSA_C)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01007102 if (mbedtls_pk_can_do(pk, MBEDTLS_PK_ECDSA)) {
7103 return MBEDTLS_SSL_SIG_ECDSA;
7104 }
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02007105#endif
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01007106 return MBEDTLS_SSL_SIG_ANON;
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02007107}
7108
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01007109unsigned char mbedtls_ssl_sig_from_pk_alg(mbedtls_pk_type_t type)
Hanno Becker7e5437a2017-04-28 17:15:26 +01007110{
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01007111 switch (type) {
Hanno Becker7e5437a2017-04-28 17:15:26 +01007112 case MBEDTLS_PK_RSA:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01007113 return MBEDTLS_SSL_SIG_RSA;
Hanno Becker7e5437a2017-04-28 17:15:26 +01007114 case MBEDTLS_PK_ECDSA:
7115 case MBEDTLS_PK_ECKEY:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01007116 return MBEDTLS_SSL_SIG_ECDSA;
Hanno Becker7e5437a2017-04-28 17:15:26 +01007117 default:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01007118 return MBEDTLS_SSL_SIG_ANON;
Hanno Becker7e5437a2017-04-28 17:15:26 +01007119 }
7120}
7121
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01007122mbedtls_pk_type_t mbedtls_ssl_pk_alg_from_sig(unsigned char sig)
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02007123{
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01007124 switch (sig) {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007125#if defined(MBEDTLS_RSA_C)
7126 case MBEDTLS_SSL_SIG_RSA:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01007127 return MBEDTLS_PK_RSA;
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02007128#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007129#if defined(MBEDTLS_ECDSA_C)
7130 case MBEDTLS_SSL_SIG_ECDSA:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01007131 return MBEDTLS_PK_ECDSA;
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02007132#endif
7133 default:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01007134 return MBEDTLS_PK_NONE;
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02007135 }
7136}
Manuel Pégourié-Gonnard5674a972015-10-19 15:14:03 +02007137#endif /* MBEDTLS_PK_C && ( MBEDTLS_RSA_C || MBEDTLS_ECDSA_C ) */
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02007138
Hanno Becker7e5437a2017-04-28 17:15:26 +01007139#if defined(MBEDTLS_SSL_PROTO_TLS1_2) && \
Gilles Peskineeccd8882020-03-10 12:19:08 +01007140 defined(MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED)
Hanno Becker7e5437a2017-04-28 17:15:26 +01007141
7142/* Find an entry in a signature-hash set matching a given hash algorithm. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01007143mbedtls_md_type_t mbedtls_ssl_sig_hash_set_find(mbedtls_ssl_sig_hash_set_t *set,
7144 mbedtls_pk_type_t sig_alg)
Hanno Becker7e5437a2017-04-28 17:15:26 +01007145{
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01007146 switch (sig_alg) {
Hanno Becker7e5437a2017-04-28 17:15:26 +01007147 case MBEDTLS_PK_RSA:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01007148 return set->rsa;
Hanno Becker7e5437a2017-04-28 17:15:26 +01007149 case MBEDTLS_PK_ECDSA:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01007150 return set->ecdsa;
Hanno Becker7e5437a2017-04-28 17:15:26 +01007151 default:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01007152 return MBEDTLS_MD_NONE;
Hanno Becker7e5437a2017-04-28 17:15:26 +01007153 }
7154}
7155
7156/* Add a signature-hash-pair to a signature-hash set */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01007157void mbedtls_ssl_sig_hash_set_add(mbedtls_ssl_sig_hash_set_t *set,
7158 mbedtls_pk_type_t sig_alg,
7159 mbedtls_md_type_t md_alg)
Hanno Becker7e5437a2017-04-28 17:15:26 +01007160{
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01007161 switch (sig_alg) {
Hanno Becker7e5437a2017-04-28 17:15:26 +01007162 case MBEDTLS_PK_RSA:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01007163 if (set->rsa == MBEDTLS_MD_NONE) {
Hanno Becker7e5437a2017-04-28 17:15:26 +01007164 set->rsa = md_alg;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01007165 }
Hanno Becker7e5437a2017-04-28 17:15:26 +01007166 break;
7167
7168 case MBEDTLS_PK_ECDSA:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01007169 if (set->ecdsa == MBEDTLS_MD_NONE) {
Hanno Becker7e5437a2017-04-28 17:15:26 +01007170 set->ecdsa = md_alg;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01007171 }
Hanno Becker7e5437a2017-04-28 17:15:26 +01007172 break;
7173
7174 default:
7175 break;
7176 }
7177}
7178
7179/* Allow exactly one hash algorithm for each signature. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01007180void mbedtls_ssl_sig_hash_set_const_hash(mbedtls_ssl_sig_hash_set_t *set,
7181 mbedtls_md_type_t md_alg)
Hanno Becker7e5437a2017-04-28 17:15:26 +01007182{
7183 set->rsa = md_alg;
7184 set->ecdsa = md_alg;
7185}
7186
7187#endif /* MBEDTLS_SSL_PROTO_TLS1_2) &&
Gilles Peskineeccd8882020-03-10 12:19:08 +01007188 MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED */
Hanno Becker7e5437a2017-04-28 17:15:26 +01007189
Manuel Pégourié-Gonnard1a483832013-09-20 12:29:15 +02007190/*
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +02007191 * Convert from MBEDTLS_SSL_HASH_XXX to MBEDTLS_MD_XXX
Manuel Pégourié-Gonnard1a483832013-09-20 12:29:15 +02007192 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01007193mbedtls_md_type_t mbedtls_ssl_md_alg_from_hash(unsigned char hash)
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02007194{
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01007195 switch (hash) {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007196#if defined(MBEDTLS_MD5_C)
7197 case MBEDTLS_SSL_HASH_MD5:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01007198 return MBEDTLS_MD_MD5;
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02007199#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007200#if defined(MBEDTLS_SHA1_C)
7201 case MBEDTLS_SSL_HASH_SHA1:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01007202 return MBEDTLS_MD_SHA1;
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02007203#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007204#if defined(MBEDTLS_SHA256_C)
7205 case MBEDTLS_SSL_HASH_SHA224:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01007206 return MBEDTLS_MD_SHA224;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007207 case MBEDTLS_SSL_HASH_SHA256:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01007208 return MBEDTLS_MD_SHA256;
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02007209#endif
Gilles Peskined2d59372021-05-12 22:43:27 +02007210#if defined(MBEDTLS_SHA512_C) && !defined(MBEDTLS_SHA512_NO_SHA384)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007211 case MBEDTLS_SSL_HASH_SHA384:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01007212 return MBEDTLS_MD_SHA384;
Gilles Peskinec54010c2021-05-12 22:52:09 +02007213#endif
7214#if defined(MBEDTLS_SHA512_C)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007215 case MBEDTLS_SSL_HASH_SHA512:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01007216 return MBEDTLS_MD_SHA512;
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02007217#endif
7218 default:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01007219 return MBEDTLS_MD_NONE;
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02007220 }
7221}
7222
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +02007223/*
7224 * Convert from MBEDTLS_MD_XXX to MBEDTLS_SSL_HASH_XXX
7225 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01007226unsigned char mbedtls_ssl_hash_from_md_alg(int md)
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +02007227{
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01007228 switch (md) {
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +02007229#if defined(MBEDTLS_MD5_C)
7230 case MBEDTLS_MD_MD5:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01007231 return MBEDTLS_SSL_HASH_MD5;
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +02007232#endif
7233#if defined(MBEDTLS_SHA1_C)
7234 case MBEDTLS_MD_SHA1:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01007235 return MBEDTLS_SSL_HASH_SHA1;
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +02007236#endif
7237#if defined(MBEDTLS_SHA256_C)
7238 case MBEDTLS_MD_SHA224:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01007239 return MBEDTLS_SSL_HASH_SHA224;
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +02007240 case MBEDTLS_MD_SHA256:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01007241 return MBEDTLS_SSL_HASH_SHA256;
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +02007242#endif
Gilles Peskined2d59372021-05-12 22:43:27 +02007243#if defined(MBEDTLS_SHA512_C) && !defined(MBEDTLS_SHA512_NO_SHA384)
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +02007244 case MBEDTLS_MD_SHA384:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01007245 return MBEDTLS_SSL_HASH_SHA384;
Gilles Peskinec54010c2021-05-12 22:52:09 +02007246#endif
7247#if defined(MBEDTLS_SHA512_C)
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +02007248 case MBEDTLS_MD_SHA512:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01007249 return MBEDTLS_SSL_HASH_SHA512;
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +02007250#endif
7251 default:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01007252 return MBEDTLS_SSL_HASH_NONE;
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +02007253 }
7254}
7255
Manuel Pégourié-Gonnardb541da62015-06-17 11:43:30 +02007256#if defined(MBEDTLS_ECP_C)
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01007257/*
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +02007258 * Check if a curve proposed by the peer is in our list.
Manuel Pégourié-Gonnard9d412d82015-06-17 12:10:46 +02007259 * Return 0 if we're willing to use it, -1 otherwise.
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01007260 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01007261int mbedtls_ssl_check_curve(const mbedtls_ssl_context *ssl, mbedtls_ecp_group_id grp_id)
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01007262{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007263 const mbedtls_ecp_group_id *gid;
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01007264
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01007265 if (ssl->conf->curve_list == NULL) {
7266 return -1;
7267 }
Manuel Pégourié-Gonnard9d412d82015-06-17 12:10:46 +02007268
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01007269 for (gid = ssl->conf->curve_list; *gid != MBEDTLS_ECP_DP_NONE; gid++) {
7270 if (*gid == grp_id) {
7271 return 0;
7272 }
7273 }
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01007274
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01007275 return -1;
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01007276}
Manuel Pégourié-Gonnard298d6cc2022-02-14 11:34:47 +01007277
7278/*
7279 * Same as mbedtls_ssl_check_curve() but takes a TLS ID for the curve.
7280 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01007281int mbedtls_ssl_check_curve_tls_id(const mbedtls_ssl_context *ssl, uint16_t tls_id)
Manuel Pégourié-Gonnard298d6cc2022-02-14 11:34:47 +01007282{
7283 const mbedtls_ecp_curve_info *curve_info =
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01007284 mbedtls_ecp_curve_info_from_tls_id(tls_id);
7285 if (curve_info == NULL) {
7286 return -1;
7287 }
7288 return mbedtls_ssl_check_curve(ssl, curve_info->grp_id);
Manuel Pégourié-Gonnard298d6cc2022-02-14 11:34:47 +01007289}
Manuel Pégourié-Gonnardb541da62015-06-17 11:43:30 +02007290#endif /* MBEDTLS_ECP_C */
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02007291
Gilles Peskineeccd8882020-03-10 12:19:08 +01007292#if defined(MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED)
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +02007293/*
7294 * Check if a hash proposed by the peer is in our list.
7295 * Return 0 if we're willing to use it, -1 otherwise.
7296 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01007297int mbedtls_ssl_check_sig_hash(const mbedtls_ssl_context *ssl,
7298 mbedtls_md_type_t md)
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +02007299{
7300 const int *cur;
7301
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01007302 if (ssl->conf->sig_hashes == NULL) {
7303 return -1;
7304 }
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +02007305
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01007306 for (cur = ssl->conf->sig_hashes; *cur != MBEDTLS_MD_NONE; cur++) {
7307 if (*cur == (int) md) {
7308 return 0;
7309 }
7310 }
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +02007311
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01007312 return -1;
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +02007313}
Gilles Peskineeccd8882020-03-10 12:19:08 +01007314#endif /* MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED */
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +02007315
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007316#if defined(MBEDTLS_X509_CRT_PARSE_C)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01007317int mbedtls_ssl_check_cert_usage(const mbedtls_x509_crt *cert,
7318 const mbedtls_ssl_ciphersuite_t *ciphersuite,
7319 int cert_endpoint,
7320 uint32_t *flags)
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02007321{
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +01007322 int ret = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007323#if defined(MBEDTLS_X509_CHECK_KEY_USAGE)
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02007324 int usage = 0;
7325#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007326#if defined(MBEDTLS_X509_CHECK_EXTENDED_KEY_USAGE)
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +02007327 const char *ext_oid;
7328 size_t ext_len;
7329#endif
7330
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007331#if !defined(MBEDTLS_X509_CHECK_KEY_USAGE) && \
7332 !defined(MBEDTLS_X509_CHECK_EXTENDED_KEY_USAGE)
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +02007333 ((void) cert);
7334 ((void) cert_endpoint);
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +01007335 ((void) flags);
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +02007336#endif
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02007337
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007338#if defined(MBEDTLS_X509_CHECK_KEY_USAGE)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01007339 if (cert_endpoint == MBEDTLS_SSL_IS_SERVER) {
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02007340 /* Server part of the key exchange */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01007341 switch (ciphersuite->key_exchange) {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007342 case MBEDTLS_KEY_EXCHANGE_RSA:
7343 case MBEDTLS_KEY_EXCHANGE_RSA_PSK:
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +01007344 usage = MBEDTLS_X509_KU_KEY_ENCIPHERMENT;
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02007345 break;
7346
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007347 case MBEDTLS_KEY_EXCHANGE_DHE_RSA:
7348 case MBEDTLS_KEY_EXCHANGE_ECDHE_RSA:
7349 case MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA:
7350 usage = MBEDTLS_X509_KU_DIGITAL_SIGNATURE;
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02007351 break;
7352
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007353 case MBEDTLS_KEY_EXCHANGE_ECDH_RSA:
7354 case MBEDTLS_KEY_EXCHANGE_ECDH_ECDSA:
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +01007355 usage = MBEDTLS_X509_KU_KEY_AGREEMENT;
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02007356 break;
7357
7358 /* Don't use default: we want warnings when adding new values */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007359 case MBEDTLS_KEY_EXCHANGE_NONE:
7360 case MBEDTLS_KEY_EXCHANGE_PSK:
7361 case MBEDTLS_KEY_EXCHANGE_DHE_PSK:
7362 case MBEDTLS_KEY_EXCHANGE_ECDHE_PSK:
Manuel Pégourié-Gonnard557535d2015-09-15 17:53:32 +02007363 case MBEDTLS_KEY_EXCHANGE_ECJPAKE:
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02007364 usage = 0;
7365 }
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01007366 } else {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007367 /* Client auth: we only implement rsa_sign and mbedtls_ecdsa_sign for now */
7368 usage = MBEDTLS_X509_KU_DIGITAL_SIGNATURE;
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02007369 }
7370
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01007371 if (mbedtls_x509_crt_check_key_usage(cert, usage) != 0) {
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +01007372 *flags |= MBEDTLS_X509_BADCERT_KEY_USAGE;
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +01007373 ret = -1;
7374 }
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +02007375#else
7376 ((void) ciphersuite);
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007377#endif /* MBEDTLS_X509_CHECK_KEY_USAGE */
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02007378
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007379#if defined(MBEDTLS_X509_CHECK_EXTENDED_KEY_USAGE)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01007380 if (cert_endpoint == MBEDTLS_SSL_IS_SERVER) {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007381 ext_oid = MBEDTLS_OID_SERVER_AUTH;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01007382 ext_len = MBEDTLS_OID_SIZE(MBEDTLS_OID_SERVER_AUTH);
7383 } else {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007384 ext_oid = MBEDTLS_OID_CLIENT_AUTH;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01007385 ext_len = MBEDTLS_OID_SIZE(MBEDTLS_OID_CLIENT_AUTH);
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +02007386 }
7387
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01007388 if (mbedtls_x509_crt_check_extended_key_usage(cert, ext_oid, ext_len) != 0) {
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +01007389 *flags |= MBEDTLS_X509_BADCERT_EXT_KEY_USAGE;
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +01007390 ret = -1;
7391 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007392#endif /* MBEDTLS_X509_CHECK_EXTENDED_KEY_USAGE */
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +02007393
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01007394 return ret;
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02007395}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007396#endif /* MBEDTLS_X509_CRT_PARSE_C */
Manuel Pégourié-Gonnard3a306b92014-04-29 15:11:17 +02007397
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01007398int mbedtls_ssl_set_calc_verify_md(mbedtls_ssl_context *ssl, int md)
Simon Butcher99000142016-10-13 17:21:01 +01007399{
7400#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01007401 if (ssl->minor_ver != MBEDTLS_SSL_MINOR_VERSION_3) {
Simon Butcher99000142016-10-13 17:21:01 +01007402 return MBEDTLS_ERR_SSL_INVALID_VERIFY_HASH;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01007403 }
Simon Butcher99000142016-10-13 17:21:01 +01007404
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01007405 switch (md) {
Simon Butcher99000142016-10-13 17:21:01 +01007406#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1)
7407#if defined(MBEDTLS_MD5_C)
7408 case MBEDTLS_SSL_HASH_MD5:
Janos Follath182013f2016-10-25 10:50:22 +01007409 return MBEDTLS_ERR_SSL_INVALID_VERIFY_HASH;
Simon Butcher99000142016-10-13 17:21:01 +01007410#endif
7411#if defined(MBEDTLS_SHA1_C)
7412 case MBEDTLS_SSL_HASH_SHA1:
7413 ssl->handshake->calc_verify = ssl_calc_verify_tls;
7414 break;
7415#endif
7416#endif /* MBEDTLS_SSL_PROTO_TLS1 || MBEDTLS_SSL_PROTO_TLS1_1 */
Gilles Peskined2d59372021-05-12 22:43:27 +02007417#if defined(MBEDTLS_SHA512_C) && !defined(MBEDTLS_SHA512_NO_SHA384)
Simon Butcher99000142016-10-13 17:21:01 +01007418 case MBEDTLS_SSL_HASH_SHA384:
7419 ssl->handshake->calc_verify = ssl_calc_verify_tls_sha384;
7420 break;
7421#endif
7422#if defined(MBEDTLS_SHA256_C)
7423 case MBEDTLS_SSL_HASH_SHA256:
7424 ssl->handshake->calc_verify = ssl_calc_verify_tls_sha256;
7425 break;
7426#endif
7427 default:
7428 return MBEDTLS_ERR_SSL_INVALID_VERIFY_HASH;
7429 }
7430
7431 return 0;
7432#else /* !MBEDTLS_SSL_PROTO_TLS1_2 */
7433 (void) ssl;
7434 (void) md;
7435
7436 return MBEDTLS_ERR_SSL_INVALID_VERIFY_HASH;
7437#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
7438}
7439
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +01007440#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1) || \
7441 defined(MBEDTLS_SSL_PROTO_TLS1_1)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01007442int mbedtls_ssl_get_key_exchange_md_ssl_tls(mbedtls_ssl_context *ssl,
7443 unsigned char *output,
7444 unsigned char *data, size_t data_len)
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +01007445{
7446 int ret = 0;
7447 mbedtls_md5_context mbedtls_md5;
7448 mbedtls_sha1_context mbedtls_sha1;
7449
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01007450 mbedtls_md5_init(&mbedtls_md5);
7451 mbedtls_sha1_init(&mbedtls_sha1);
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +01007452
7453 /*
7454 * digitally-signed struct {
7455 * opaque md5_hash[16];
7456 * opaque sha_hash[20];
7457 * };
7458 *
7459 * md5_hash
7460 * MD5(ClientHello.random + ServerHello.random
7461 * + ServerParams);
7462 * sha_hash
7463 * SHA(ClientHello.random + ServerHello.random
7464 * + ServerParams);
7465 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01007466 if ((ret = mbedtls_md5_starts_ret(&mbedtls_md5)) != 0) {
7467 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_md5_starts_ret", ret);
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +01007468 goto exit;
7469 }
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01007470 if ((ret = mbedtls_md5_update_ret(&mbedtls_md5,
7471 ssl->handshake->randbytes, 64)) != 0) {
7472 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_md5_update_ret", ret);
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +01007473 goto exit;
7474 }
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01007475 if ((ret = mbedtls_md5_update_ret(&mbedtls_md5, data, data_len)) != 0) {
7476 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_md5_update_ret", ret);
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +01007477 goto exit;
7478 }
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01007479 if ((ret = mbedtls_md5_finish_ret(&mbedtls_md5, output)) != 0) {
7480 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_md5_finish_ret", ret);
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +01007481 goto exit;
7482 }
7483
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01007484 if ((ret = mbedtls_sha1_starts_ret(&mbedtls_sha1)) != 0) {
7485 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_sha1_starts_ret", ret);
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +01007486 goto exit;
7487 }
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01007488 if ((ret = mbedtls_sha1_update_ret(&mbedtls_sha1,
7489 ssl->handshake->randbytes, 64)) != 0) {
7490 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_sha1_update_ret", ret);
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +01007491 goto exit;
7492 }
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01007493 if ((ret = mbedtls_sha1_update_ret(&mbedtls_sha1, data,
7494 data_len)) != 0) {
7495 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_sha1_update_ret", ret);
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +01007496 goto exit;
7497 }
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01007498 if ((ret = mbedtls_sha1_finish_ret(&mbedtls_sha1,
7499 output + 16)) != 0) {
7500 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_sha1_finish_ret", ret);
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +01007501 goto exit;
7502 }
7503
7504exit:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01007505 mbedtls_md5_free(&mbedtls_md5);
7506 mbedtls_sha1_free(&mbedtls_sha1);
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +01007507
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01007508 if (ret != 0) {
7509 mbedtls_ssl_send_alert_message(ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
7510 MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR);
7511 }
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +01007512
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01007513 return ret;
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +01007514
7515}
7516#endif /* MBEDTLS_SSL_PROTO_SSL3 || MBEDTLS_SSL_PROTO_TLS1 || \
7517 MBEDTLS_SSL_PROTO_TLS1_1 */
7518
7519#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1) || \
7520 defined(MBEDTLS_SSL_PROTO_TLS1_2)
Andrzej Kurekd6db9be2019-01-10 05:27:10 -05007521
7522#if defined(MBEDTLS_USE_PSA_CRYPTO)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01007523int mbedtls_ssl_get_key_exchange_md_tls1_2(mbedtls_ssl_context *ssl,
7524 unsigned char *hash, size_t *hashlen,
7525 unsigned char *data, size_t data_len,
7526 mbedtls_md_type_t md_alg)
Andrzej Kurekd6db9be2019-01-10 05:27:10 -05007527{
Andrzej Kurek814feff2019-01-14 04:35:19 -05007528 psa_status_t status;
Jaeden Amero34973232019-02-20 10:32:28 +00007529 psa_hash_operation_t hash_operation = PSA_HASH_OPERATION_INIT;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01007530 psa_algorithm_t hash_alg = mbedtls_psa_translate_md(md_alg);
Andrzej Kurekd6db9be2019-01-10 05:27:10 -05007531
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01007532 MBEDTLS_SSL_DEBUG_MSG(3, ("Perform PSA-based computation of digest of ServerKeyExchange"));
Andrzej Kurek814feff2019-01-14 04:35:19 -05007533
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01007534 if ((status = psa_hash_setup(&hash_operation,
7535 hash_alg)) != PSA_SUCCESS) {
7536 MBEDTLS_SSL_DEBUG_RET(1, "psa_hash_setup", status);
Andrzej Kurekd6db9be2019-01-10 05:27:10 -05007537 goto exit;
7538 }
7539
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01007540 if ((status = psa_hash_update(&hash_operation, ssl->handshake->randbytes,
7541 64)) != PSA_SUCCESS) {
7542 MBEDTLS_SSL_DEBUG_RET(1, "psa_hash_update", status);
Andrzej Kurekd6db9be2019-01-10 05:27:10 -05007543 goto exit;
7544 }
7545
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01007546 if ((status = psa_hash_update(&hash_operation,
7547 data, data_len)) != PSA_SUCCESS) {
7548 MBEDTLS_SSL_DEBUG_RET(1, "psa_hash_update", status);
Andrzej Kurekd6db9be2019-01-10 05:27:10 -05007549 goto exit;
7550 }
7551
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01007552 if ((status = psa_hash_finish(&hash_operation, hash, PSA_HASH_MAX_SIZE,
7553 hashlen)) != PSA_SUCCESS) {
7554 MBEDTLS_SSL_DEBUG_RET(1, "psa_hash_finish", status);
7555 goto exit;
Andrzej Kurekd6db9be2019-01-10 05:27:10 -05007556 }
7557
7558exit:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01007559 if (status != PSA_SUCCESS) {
7560 mbedtls_ssl_send_alert_message(ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
7561 MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR);
7562 switch (status) {
Andrzej Kurekd6db9be2019-01-10 05:27:10 -05007563 case PSA_ERROR_NOT_SUPPORTED:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01007564 return MBEDTLS_ERR_MD_FEATURE_UNAVAILABLE;
Andrzej Kurek814feff2019-01-14 04:35:19 -05007565 case PSA_ERROR_BAD_STATE: /* Intentional fallthrough */
Andrzej Kurekd6db9be2019-01-10 05:27:10 -05007566 case PSA_ERROR_BUFFER_TOO_SMALL:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01007567 return MBEDTLS_ERR_MD_BAD_INPUT_DATA;
Andrzej Kurekd6db9be2019-01-10 05:27:10 -05007568 case PSA_ERROR_INSUFFICIENT_MEMORY:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01007569 return MBEDTLS_ERR_MD_ALLOC_FAILED;
Andrzej Kurekd6db9be2019-01-10 05:27:10 -05007570 default:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01007571 return MBEDTLS_ERR_MD_HW_ACCEL_FAILED;
Andrzej Kurekd6db9be2019-01-10 05:27:10 -05007572 }
7573 }
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01007574 return 0;
Andrzej Kurekd6db9be2019-01-10 05:27:10 -05007575}
7576
7577#else
7578
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01007579int mbedtls_ssl_get_key_exchange_md_tls1_2(mbedtls_ssl_context *ssl,
7580 unsigned char *hash, size_t *hashlen,
7581 unsigned char *data, size_t data_len,
7582 mbedtls_md_type_t md_alg)
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +01007583{
7584 int ret = 0;
7585 mbedtls_md_context_t ctx;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01007586 const mbedtls_md_info_t *md_info = mbedtls_md_info_from_type(md_alg);
7587 *hashlen = mbedtls_md_get_size(md_info);
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +01007588
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01007589 MBEDTLS_SSL_DEBUG_MSG(3, ("Perform mbedtls-based computation of digest of ServerKeyExchange"));
Andrzej Kurek814feff2019-01-14 04:35:19 -05007590
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01007591 mbedtls_md_init(&ctx);
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +01007592
7593 /*
7594 * digitally-signed struct {
7595 * opaque client_random[32];
7596 * opaque server_random[32];
7597 * ServerDHParams params;
7598 * };
7599 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01007600 if ((ret = mbedtls_md_setup(&ctx, md_info, 0)) != 0) {
7601 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_md_setup", ret);
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +01007602 goto exit;
7603 }
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01007604 if ((ret = mbedtls_md_starts(&ctx)) != 0) {
7605 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_md_starts", ret);
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +01007606 goto exit;
7607 }
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01007608 if ((ret = mbedtls_md_update(&ctx, ssl->handshake->randbytes, 64)) != 0) {
7609 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_md_update", ret);
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +01007610 goto exit;
7611 }
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01007612 if ((ret = mbedtls_md_update(&ctx, data, data_len)) != 0) {
7613 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_md_update", ret);
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +01007614 goto exit;
7615 }
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01007616 if ((ret = mbedtls_md_finish(&ctx, hash)) != 0) {
7617 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_md_finish", ret);
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +01007618 goto exit;
7619 }
7620
7621exit:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01007622 mbedtls_md_free(&ctx);
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +01007623
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01007624 if (ret != 0) {
7625 mbedtls_ssl_send_alert_message(ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
7626 MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR);
7627 }
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +01007628
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01007629 return ret;
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +01007630}
Andrzej Kurekd6db9be2019-01-10 05:27:10 -05007631#endif /* MBEDTLS_USE_PSA_CRYPTO */
7632
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +01007633#endif /* MBEDTLS_SSL_PROTO_TLS1 || MBEDTLS_SSL_PROTO_TLS1_1 || \
7634 MBEDTLS_SSL_PROTO_TLS1_2 */
7635
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007636#endif /* MBEDTLS_SSL_TLS_C */