blob: 77510b97cb1654be0785240f5a65a5a6cc62b895 [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
Manuel Pégourié-Gonnard37ff1402015-09-04 14:21:07 +02005 * SPDX-License-Identifier: Apache-2.0
6 *
7 * Licensed under the Apache License, Version 2.0 (the "License"); you may
8 * not use this file except in compliance with the License.
9 * You may obtain a copy of the License at
10 *
11 * http://www.apache.org/licenses/LICENSE-2.0
12 *
13 * Unless required by applicable law or agreed to in writing, software
14 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
15 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 * See the License for the specific language governing permissions and
17 * limitations under the License.
Paul Bakker5121ce52009-01-03 21:22:43 +000018 */
19/*
20 * The SSL 3.0 specification was drafted by Netscape in 1996,
21 * and became an IETF standard in 1999.
22 *
23 * http://wp.netscape.com/eng/ssl3/
24 * http://www.ietf.org/rfc/rfc2246.txt
25 * http://www.ietf.org/rfc/rfc4346.txt
26 */
27
Gilles Peskinedb09ef62020-06-03 01:43:33 +020028#include "common.h"
Paul Bakker5121ce52009-01-03 21:22:43 +000029
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020030#if defined(MBEDTLS_SSL_TLS_C)
Paul Bakker5121ce52009-01-03 21:22:43 +000031
SimonBd5800b72016-04-26 07:43:27 +010032#include "mbedtls/platform.h"
SimonBd5800b72016-04-26 07:43:27 +010033
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000034#include "mbedtls/ssl.h"
Manuel Pégourié-Gonnard5e94dde2015-05-26 11:57:05 +020035#include "mbedtls/ssl_internal.h"
Janos Follath73c616b2019-12-18 15:07:04 +000036#include "mbedtls/debug.h"
37#include "mbedtls/error.h"
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -050038#include "mbedtls/platform_util.h"
Hanno Beckera835da52019-05-16 12:39:07 +010039#include "mbedtls/version.h"
Gabor Mezeie24dea82021-10-19 12:22:25 +020040#include "mbedtls/constant_time.h"
Paul Bakker0be444a2013-08-27 21:55:01 +020041
Rich Evans00ab4702015-02-06 13:43:58 +000042#include <string.h>
43
Andrzej Kurekd6db9be2019-01-10 05:27:10 -050044#if defined(MBEDTLS_USE_PSA_CRYPTO)
45#include "mbedtls/psa_util.h"
46#include "psa/crypto.h"
47#endif
48
Janos Follath23bdca02016-10-07 14:47:14 +010049#if defined(MBEDTLS_X509_CRT_PARSE_C)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000050#include "mbedtls/oid.h"
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +020051#endif
52
Manuel Pégourié-Gonnard286a1362015-05-13 16:22:05 +020053#if defined(MBEDTLS_SSL_PROTO_DTLS)
Hanno Becker2b1e3542018-08-06 11:19:13 +010054
Hanno Beckera0e20d02019-05-15 14:03:01 +010055#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Beckerf8542cf2019-04-09 15:22:03 +010056/* Top-level Connection ID API */
57
Gilles Peskine1b6c09a2023-01-11 14:52:35 +010058int mbedtls_ssl_conf_cid(mbedtls_ssl_config *conf,
59 size_t len,
60 int ignore_other_cid)
Hanno Beckerad4a1372019-05-03 13:06:44 +010061{
Gilles Peskine1b6c09a2023-01-11 14:52:35 +010062 if (len > MBEDTLS_SSL_CID_IN_LEN_MAX) {
63 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
64 }
Hanno Beckerad4a1372019-05-03 13:06:44 +010065
Gilles Peskine1b6c09a2023-01-11 14:52:35 +010066 if (ignore_other_cid != MBEDTLS_SSL_UNEXPECTED_CID_FAIL &&
67 ignore_other_cid != MBEDTLS_SSL_UNEXPECTED_CID_IGNORE) {
68 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
Hanno Becker611ac772019-05-14 11:45:26 +010069 }
70
71 conf->ignore_unexpected_cid = ignore_other_cid;
Hanno Beckerad4a1372019-05-03 13:06:44 +010072 conf->cid_len = len;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +010073 return 0;
Hanno Beckerad4a1372019-05-03 13:06:44 +010074}
75
Gilles Peskine1b6c09a2023-01-11 14:52:35 +010076int mbedtls_ssl_set_cid(mbedtls_ssl_context *ssl,
77 int enable,
78 unsigned char const *own_cid,
79 size_t own_cid_len)
Hanno Beckerf8542cf2019-04-09 15:22:03 +010080{
Gilles Peskine1b6c09a2023-01-11 14:52:35 +010081 if (ssl->conf->transport != MBEDTLS_SSL_TRANSPORT_DATAGRAM) {
82 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
83 }
Hanno Becker76a79ab2019-05-03 14:38:32 +010084
Hanno Beckerca092242019-04-25 16:01:49 +010085 ssl->negotiate_cid = enable;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +010086 if (enable == MBEDTLS_SSL_CID_DISABLED) {
87 MBEDTLS_SSL_DEBUG_MSG(3, ("Disable use of CID extension."));
88 return 0;
Hanno Beckerca092242019-04-25 16:01:49 +010089 }
Gilles Peskine1b6c09a2023-01-11 14:52:35 +010090 MBEDTLS_SSL_DEBUG_MSG(3, ("Enable use of CID extension."));
91 MBEDTLS_SSL_DEBUG_BUF(3, "Own CID", own_cid, own_cid_len);
Hanno Beckerca092242019-04-25 16:01:49 +010092
Gilles Peskine1b6c09a2023-01-11 14:52:35 +010093 if (own_cid_len != ssl->conf->cid_len) {
94 MBEDTLS_SSL_DEBUG_MSG(3, ("CID length %u does not match CID length %u in config",
95 (unsigned) own_cid_len,
96 (unsigned) ssl->conf->cid_len));
97 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
Hanno Beckerca092242019-04-25 16:01:49 +010098 }
99
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100100 memcpy(ssl->own_cid, own_cid, own_cid_len);
Hanno Beckerb7ee0cf2019-04-30 14:07:31 +0100101 /* Truncation is not an issue here because
102 * MBEDTLS_SSL_CID_IN_LEN_MAX at most 255. */
103 ssl->own_cid_len = (uint8_t) own_cid_len;
Hanno Beckerca092242019-04-25 16:01:49 +0100104
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100105 return 0;
Hanno Beckerf8542cf2019-04-09 15:22:03 +0100106}
107
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100108int mbedtls_ssl_get_peer_cid(mbedtls_ssl_context *ssl,
109 int *enabled,
110 unsigned char peer_cid[MBEDTLS_SSL_CID_OUT_LEN_MAX],
111 size_t *peer_cid_len)
Hanno Beckerf8542cf2019-04-09 15:22:03 +0100112{
Hanno Beckerf8542cf2019-04-09 15:22:03 +0100113 *enabled = MBEDTLS_SSL_CID_DISABLED;
Hanno Beckerb1f89cd2019-04-26 17:08:02 +0100114
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100115 if (ssl->conf->transport != MBEDTLS_SSL_TRANSPORT_DATAGRAM ||
116 ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER) {
117 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
Hanno Becker76a79ab2019-05-03 14:38:32 +0100118 }
Hanno Beckerb1f89cd2019-04-26 17:08:02 +0100119
Hanno Beckerc5f24222019-05-03 12:54:52 +0100120 /* We report MBEDTLS_SSL_CID_DISABLED in case the CID extensions
121 * were used, but client and server requested the empty CID.
122 * This is indistinguishable from not using the CID extension
123 * in the first place. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100124 if (ssl->transform_in->in_cid_len == 0 &&
125 ssl->transform_in->out_cid_len == 0) {
126 return 0;
Hanno Beckerb1f89cd2019-04-26 17:08:02 +0100127 }
128
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100129 if (peer_cid_len != NULL) {
Hanno Becker615ef172019-05-22 16:50:35 +0100130 *peer_cid_len = ssl->transform_in->out_cid_len;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100131 if (peer_cid != NULL) {
132 memcpy(peer_cid, ssl->transform_in->out_cid,
133 ssl->transform_in->out_cid_len);
Hanno Becker615ef172019-05-22 16:50:35 +0100134 }
135 }
Hanno Beckerb1f89cd2019-04-26 17:08:02 +0100136
137 *enabled = MBEDTLS_SSL_CID_ENABLED;
138
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100139 return 0;
Hanno Beckerf8542cf2019-04-09 15:22:03 +0100140}
Hanno Beckera0e20d02019-05-15 14:03:01 +0100141#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Beckerf8542cf2019-04-09 15:22:03 +0100142
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200143#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +0200144
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200145#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
Manuel Pégourié-Gonnard581e6b62013-07-18 12:32:27 +0200146/*
147 * Convert max_fragment_length codes to length.
148 * RFC 6066 says:
149 * enum{
150 * 2^9(1), 2^10(2), 2^11(3), 2^12(4), (255)
151 * } MaxFragmentLength;
152 * and we add 0 -> extension unused
153 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100154static unsigned int ssl_mfl_code_to_length(int mfl)
Manuel Pégourié-Gonnard581e6b62013-07-18 12:32:27 +0200155{
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100156 switch (mfl) {
157 case MBEDTLS_SSL_MAX_FRAG_LEN_NONE:
158 return MBEDTLS_TLS_EXT_ADV_CONTENT_LEN;
159 case MBEDTLS_SSL_MAX_FRAG_LEN_512:
160 return 512;
161 case MBEDTLS_SSL_MAX_FRAG_LEN_1024:
162 return 1024;
163 case MBEDTLS_SSL_MAX_FRAG_LEN_2048:
164 return 2048;
165 case MBEDTLS_SSL_MAX_FRAG_LEN_4096:
166 return 4096;
167 default:
168 return MBEDTLS_TLS_EXT_ADV_CONTENT_LEN;
Angus Grattond8213d02016-05-25 20:56:48 +1000169 }
170}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200171#endif /* MBEDTLS_SSL_MAX_FRAGMENT_LENGTH */
Manuel Pégourié-Gonnard581e6b62013-07-18 12:32:27 +0200172
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100173int mbedtls_ssl_session_copy(mbedtls_ssl_session *dst,
174 const mbedtls_ssl_session *src)
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200175{
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100176 mbedtls_ssl_session_free(dst);
177 memcpy(dst, src, sizeof(mbedtls_ssl_session));
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200178
吴敬辉0f6c6bc2021-11-29 10:46:35 +0800179#if defined(MBEDTLS_SSL_SESSION_TICKETS) && defined(MBEDTLS_SSL_CLI_C)
180 dst->ticket = NULL;
181#endif
182
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200183#if defined(MBEDTLS_X509_CRT_PARSE_C)
Hanno Becker6d1986e2019-02-07 12:27:42 +0000184
185#if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100186 if (src->peer_cert != NULL) {
Janos Follath865b3eb2019-12-16 11:46:15 +0000187 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker2292d1f2013-09-15 17:06:49 +0200188
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100189 dst->peer_cert = mbedtls_calloc(1, sizeof(mbedtls_x509_crt));
190 if (dst->peer_cert == NULL) {
191 return MBEDTLS_ERR_SSL_ALLOC_FAILED;
192 }
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200193
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100194 mbedtls_x509_crt_init(dst->peer_cert);
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200195
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100196 if ((ret = mbedtls_x509_crt_parse_der(dst->peer_cert, src->peer_cert->raw.p,
197 src->peer_cert->raw.len)) != 0) {
198 mbedtls_free(dst->peer_cert);
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200199 dst->peer_cert = NULL;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100200 return ret;
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200201 }
202 }
Hanno Becker6d1986e2019-02-07 12:27:42 +0000203#else /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100204 if (src->peer_cert_digest != NULL) {
Hanno Becker9198ad12019-02-05 17:00:50 +0000205 dst->peer_cert_digest =
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100206 mbedtls_calloc(1, src->peer_cert_digest_len);
207 if (dst->peer_cert_digest == NULL) {
208 return MBEDTLS_ERR_SSL_ALLOC_FAILED;
209 }
Hanno Becker9198ad12019-02-05 17:00:50 +0000210
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100211 memcpy(dst->peer_cert_digest, src->peer_cert_digest,
212 src->peer_cert_digest_len);
Hanno Becker9198ad12019-02-05 17:00:50 +0000213 dst->peer_cert_digest_type = src->peer_cert_digest_type;
Hanno Beckeraccc5992019-02-25 10:06:59 +0000214 dst->peer_cert_digest_len = src->peer_cert_digest_len;
Hanno Becker9198ad12019-02-05 17:00:50 +0000215 }
216#endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
217
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200218#endif /* MBEDTLS_X509_CRT_PARSE_C */
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200219
Manuel Pégourié-Gonnardb596abf2015-05-20 10:45:29 +0200220#if defined(MBEDTLS_SSL_SESSION_TICKETS) && defined(MBEDTLS_SSL_CLI_C)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100221 if (src->ticket != NULL) {
222 dst->ticket = mbedtls_calloc(1, src->ticket_len);
223 if (dst->ticket == NULL) {
224 return MBEDTLS_ERR_SSL_ALLOC_FAILED;
225 }
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200226
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100227 memcpy(dst->ticket, src->ticket, src->ticket_len);
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200228 }
Manuel Pégourié-Gonnardb596abf2015-05-20 10:45:29 +0200229#endif /* MBEDTLS_SSL_SESSION_TICKETS && MBEDTLS_SSL_CLI_C */
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200230
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100231 return 0;
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200232}
233
Andrzej Kurek0afa2a12020-03-03 10:39:58 -0500234#if defined(MBEDTLS_SSL_VARIABLE_BUFFER_LENGTH)
Manuel Pégourié-Gonnardd904d662022-06-17 10:24:00 +0200235MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100236static int resize_buffer(unsigned char **buffer, size_t len_new, size_t *len_old)
Andrzej Kurek0afa2a12020-03-03 10:39:58 -0500237{
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100238 unsigned char *resized_buffer = mbedtls_calloc(1, len_new);
239 if (resized_buffer == NULL) {
Andrzej Kurek0afa2a12020-03-03 10:39:58 -0500240 return -1;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100241 }
Andrzej Kurek0afa2a12020-03-03 10:39:58 -0500242
243 /* We want to copy len_new bytes when downsizing the buffer, and
244 * len_old bytes when upsizing, so we choose the smaller of two sizes,
245 * to fit one buffer into another. Size checks, ensuring that no data is
246 * lost, are done outside of this function. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100247 memcpy(resized_buffer, *buffer,
248 (len_new < *len_old) ? len_new : *len_old);
249 mbedtls_platform_zeroize(*buffer, *len_old);
250 mbedtls_free(*buffer);
Andrzej Kurek0afa2a12020-03-03 10:39:58 -0500251
252 *buffer = resized_buffer;
253 *len_old = len_new;
254
255 return 0;
256}
Andrzej Kurek4a063792020-10-21 15:08:44 +0200257
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100258static void handle_buffer_resizing(mbedtls_ssl_context *ssl, int downsizing,
259 size_t in_buf_new_len,
260 size_t out_buf_new_len)
Andrzej Kurek4a063792020-10-21 15:08:44 +0200261{
262 int modified = 0;
263 size_t written_in = 0, iv_offset_in = 0, len_offset_in = 0;
264 size_t written_out = 0, iv_offset_out = 0, len_offset_out = 0;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100265 if (ssl->in_buf != NULL) {
Andrzej Kurek4a063792020-10-21 15:08:44 +0200266 written_in = ssl->in_msg - ssl->in_buf;
267 iv_offset_in = ssl->in_iv - ssl->in_buf;
268 len_offset_in = ssl->in_len - ssl->in_buf;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100269 if (downsizing ?
Andrzej Kurek4a063792020-10-21 15:08:44 +0200270 ssl->in_buf_len > in_buf_new_len && ssl->in_left < in_buf_new_len :
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100271 ssl->in_buf_len < in_buf_new_len) {
272 if (resize_buffer(&ssl->in_buf, in_buf_new_len, &ssl->in_buf_len) != 0) {
273 MBEDTLS_SSL_DEBUG_MSG(1, ("input buffer resizing failed - out of memory"));
274 } else {
275 MBEDTLS_SSL_DEBUG_MSG(2, ("Reallocating in_buf to %" MBEDTLS_PRINTF_SIZET,
276 in_buf_new_len));
Andrzej Kurek4a063792020-10-21 15:08:44 +0200277 modified = 1;
278 }
279 }
280 }
281
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100282 if (ssl->out_buf != NULL) {
Andrzej Kurek4a063792020-10-21 15:08:44 +0200283 written_out = ssl->out_msg - ssl->out_buf;
284 iv_offset_out = ssl->out_iv - ssl->out_buf;
285 len_offset_out = ssl->out_len - ssl->out_buf;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100286 if (downsizing ?
Andrzej Kurek4a063792020-10-21 15:08:44 +0200287 ssl->out_buf_len > out_buf_new_len && ssl->out_left < out_buf_new_len :
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100288 ssl->out_buf_len < out_buf_new_len) {
289 if (resize_buffer(&ssl->out_buf, out_buf_new_len, &ssl->out_buf_len) != 0) {
290 MBEDTLS_SSL_DEBUG_MSG(1, ("output buffer resizing failed - out of memory"));
291 } else {
292 MBEDTLS_SSL_DEBUG_MSG(2, ("Reallocating out_buf to %" MBEDTLS_PRINTF_SIZET,
293 out_buf_new_len));
Andrzej Kurek4a063792020-10-21 15:08:44 +0200294 modified = 1;
295 }
296 }
297 }
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100298 if (modified) {
Andrzej Kurek4a063792020-10-21 15:08:44 +0200299 /* Update pointers here to avoid doing it twice. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100300 mbedtls_ssl_reset_in_out_pointers(ssl);
Andrzej Kurek4a063792020-10-21 15:08:44 +0200301 /* Fields below might not be properly updated with record
302 * splitting or with CID, so they are manually updated here. */
303 ssl->out_msg = ssl->out_buf + written_out;
304 ssl->out_len = ssl->out_buf + len_offset_out;
305 ssl->out_iv = ssl->out_buf + iv_offset_out;
306
307 ssl->in_msg = ssl->in_buf + written_in;
308 ssl->in_len = ssl->in_buf + len_offset_in;
309 ssl->in_iv = ssl->in_buf + iv_offset_in;
310 }
311}
Andrzej Kurek0afa2a12020-03-03 10:39:58 -0500312#endif /* MBEDTLS_SSL_VARIABLE_BUFFER_LENGTH */
313
Paul Bakker5121ce52009-01-03 21:22:43 +0000314/*
315 * Key material generation
316 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200317#if defined(MBEDTLS_SSL_PROTO_SSL3)
Manuel Pégourié-Gonnardd904d662022-06-17 10:24:00 +0200318MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100319static int ssl3_prf(const unsigned char *secret, size_t slen,
320 const char *label,
321 const unsigned char *random, size_t rlen,
322 unsigned char *dstbuf, size_t dlen)
Paul Bakker5f70b252012-09-13 14:23:06 +0000323{
Andres Amaya Garcia33952502017-07-20 16:29:16 +0100324 int ret = 0;
Paul Bakker5f70b252012-09-13 14:23:06 +0000325 size_t i;
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +0200326 mbedtls_md5_context md5;
327 mbedtls_sha1_context sha1;
Paul Bakker5f70b252012-09-13 14:23:06 +0000328 unsigned char padding[16];
329 unsigned char sha1sum[20];
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100330 ((void) label);
Paul Bakker5f70b252012-09-13 14:23:06 +0000331
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100332 mbedtls_md5_init(&md5);
333 mbedtls_sha1_init(&sha1);
Paul Bakker5b4af392014-06-26 12:09:34 +0200334
Paul Bakker5f70b252012-09-13 14:23:06 +0000335 /*
336 * SSLv3:
337 * block =
338 * MD5( secret + SHA1( 'A' + secret + random ) ) +
339 * MD5( secret + SHA1( 'BB' + secret + random ) ) +
340 * MD5( secret + SHA1( 'CCC' + secret + random ) ) +
341 * ...
342 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100343 for (i = 0; i < dlen / 16; i++) {
344 memset(padding, (unsigned char) ('A' + i), 1 + i);
Paul Bakker5f70b252012-09-13 14:23:06 +0000345
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100346 if ((ret = mbedtls_sha1_starts_ret(&sha1)) != 0) {
Andres Amaya Garcia1a607a12017-06-29 17:09:42 +0100347 goto exit;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100348 }
349 if ((ret = mbedtls_sha1_update_ret(&sha1, padding, 1 + i)) != 0) {
Andres Amaya Garcia1a607a12017-06-29 17:09:42 +0100350 goto exit;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100351 }
352 if ((ret = mbedtls_sha1_update_ret(&sha1, secret, slen)) != 0) {
Andres Amaya Garcia1a607a12017-06-29 17:09:42 +0100353 goto exit;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100354 }
355 if ((ret = mbedtls_sha1_update_ret(&sha1, random, rlen)) != 0) {
Andres Amaya Garcia1a607a12017-06-29 17:09:42 +0100356 goto exit;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100357 }
358 if ((ret = mbedtls_sha1_finish_ret(&sha1, sha1sum)) != 0) {
Andres Amaya Garcia1a607a12017-06-29 17:09:42 +0100359 goto exit;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100360 }
Paul Bakker5f70b252012-09-13 14:23:06 +0000361
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100362 if ((ret = mbedtls_md5_starts_ret(&md5)) != 0) {
Andres Amaya Garcia1a607a12017-06-29 17:09:42 +0100363 goto exit;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100364 }
365 if ((ret = mbedtls_md5_update_ret(&md5, secret, slen)) != 0) {
Andres Amaya Garcia1a607a12017-06-29 17:09:42 +0100366 goto exit;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100367 }
368 if ((ret = mbedtls_md5_update_ret(&md5, sha1sum, 20)) != 0) {
Andres Amaya Garcia1a607a12017-06-29 17:09:42 +0100369 goto exit;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100370 }
371 if ((ret = mbedtls_md5_finish_ret(&md5, dstbuf + i * 16)) != 0) {
Andres Amaya Garcia1a607a12017-06-29 17:09:42 +0100372 goto exit;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100373 }
Paul Bakker5f70b252012-09-13 14:23:06 +0000374 }
375
Andres Amaya Garcia1a607a12017-06-29 17:09:42 +0100376exit:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100377 mbedtls_md5_free(&md5);
378 mbedtls_sha1_free(&sha1);
Paul Bakker5f70b252012-09-13 14:23:06 +0000379
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100380 mbedtls_platform_zeroize(padding, sizeof(padding));
381 mbedtls_platform_zeroize(sha1sum, sizeof(sha1sum));
Paul Bakker5f70b252012-09-13 14:23:06 +0000382
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100383 return ret;
Paul Bakker5f70b252012-09-13 14:23:06 +0000384}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200385#endif /* MBEDTLS_SSL_PROTO_SSL3 */
Paul Bakker5f70b252012-09-13 14:23:06 +0000386
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200387#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1)
Manuel Pégourié-Gonnardd904d662022-06-17 10:24:00 +0200388MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100389static int tls1_prf(const unsigned char *secret, size_t slen,
390 const char *label,
391 const unsigned char *random, size_t rlen,
392 unsigned char *dstbuf, size_t dlen)
Paul Bakker5121ce52009-01-03 21:22:43 +0000393{
Paul Bakker23986e52011-04-24 08:57:21 +0000394 size_t nb, hs;
395 size_t i, j, k;
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +0200396 const unsigned char *S1, *S2;
Ron Eldor3b350852019-05-07 18:31:49 +0300397 unsigned char *tmp;
398 size_t tmp_len = 0;
Paul Bakker5121ce52009-01-03 21:22:43 +0000399 unsigned char h_i[20];
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200400 const mbedtls_md_info_t *md_info;
401 mbedtls_md_context_t md_ctx;
Janos Follath865b3eb2019-12-16 11:46:15 +0000402 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnardb7fcca32015-03-26 11:41:28 +0100403
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100404 mbedtls_md_init(&md_ctx);
Paul Bakker5121ce52009-01-03 21:22:43 +0000405
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100406 tmp_len = 20 + strlen(label) + rlen;
407 tmp = mbedtls_calloc(1, tmp_len);
408 if (tmp == NULL) {
Ron Eldor3b350852019-05-07 18:31:49 +0300409 ret = MBEDTLS_ERR_SSL_ALLOC_FAILED;
410 goto exit;
411 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000412
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100413 hs = (slen + 1) / 2;
Paul Bakker5121ce52009-01-03 21:22:43 +0000414 S1 = secret;
415 S2 = secret + slen - hs;
416
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100417 nb = strlen(label);
418 memcpy(tmp + 20, label, nb);
419 memcpy(tmp + 20 + nb, random, rlen);
Paul Bakker5121ce52009-01-03 21:22:43 +0000420 nb += rlen;
421
422 /*
423 * First compute P_md5(secret,label+random)[0..dlen]
424 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100425 if ((md_info = mbedtls_md_info_from_type(MBEDTLS_MD_MD5)) == NULL) {
Ron Eldor3b350852019-05-07 18:31:49 +0300426 ret = MBEDTLS_ERR_SSL_INTERNAL_ERROR;
427 goto exit;
428 }
Manuel Pégourié-Gonnard7da726b2015-03-24 18:08:19 +0100429
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100430 if ((ret = mbedtls_md_setup(&md_ctx, md_info, 1)) != 0) {
Ron Eldor3b350852019-05-07 18:31:49 +0300431 goto exit;
432 }
Manuel Pégourié-Gonnardb7fcca32015-03-26 11:41:28 +0100433
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100434 ret = mbedtls_md_hmac_starts(&md_ctx, S1, hs);
435 if (ret != 0) {
Gilles Peskine2b3f21d2021-12-10 21:35:10 +0100436 goto exit;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100437 }
438 ret = mbedtls_md_hmac_update(&md_ctx, tmp + 20, nb);
439 if (ret != 0) {
Gilles Peskine2b3f21d2021-12-10 21:35:10 +0100440 goto exit;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100441 }
442 ret = mbedtls_md_hmac_finish(&md_ctx, 4 + tmp);
443 if (ret != 0) {
Gilles Peskine2b3f21d2021-12-10 21:35:10 +0100444 goto exit;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100445 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000446
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100447 for (i = 0; i < dlen; i += 16) {
448 ret = mbedtls_md_hmac_reset(&md_ctx);
449 if (ret != 0) {
Gilles Peskine2b3f21d2021-12-10 21:35:10 +0100450 goto exit;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100451 }
452 ret = mbedtls_md_hmac_update(&md_ctx, 4 + tmp, 16 + nb);
453 if (ret != 0) {
Gilles Peskine2b3f21d2021-12-10 21:35:10 +0100454 goto exit;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100455 }
456 ret = mbedtls_md_hmac_finish(&md_ctx, h_i);
457 if (ret != 0) {
Gilles Peskine2b3f21d2021-12-10 21:35:10 +0100458 goto exit;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100459 }
Manuel Pégourié-Gonnardb7fcca32015-03-26 11:41:28 +0100460
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100461 ret = mbedtls_md_hmac_reset(&md_ctx);
462 if (ret != 0) {
Gilles Peskine2b3f21d2021-12-10 21:35:10 +0100463 goto exit;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100464 }
465 ret = mbedtls_md_hmac_update(&md_ctx, 4 + tmp, 16);
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_finish(&md_ctx, 4 + tmp);
470 if (ret != 0) {
Gilles Peskine2b3f21d2021-12-10 21:35:10 +0100471 goto exit;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100472 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000473
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100474 k = (i + 16 > dlen) ? dlen % 16 : 16;
Paul Bakker5121ce52009-01-03 21:22:43 +0000475
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100476 for (j = 0; j < k; j++) {
Paul Bakker5121ce52009-01-03 21:22:43 +0000477 dstbuf[i + j] = h_i[j];
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100478 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000479 }
480
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100481 mbedtls_md_free(&md_ctx);
Manuel Pégourié-Gonnardb7fcca32015-03-26 11:41:28 +0100482
Paul Bakker5121ce52009-01-03 21:22:43 +0000483 /*
484 * XOR out with P_sha1(secret,label+random)[0..dlen]
485 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100486 if ((md_info = mbedtls_md_info_from_type(MBEDTLS_MD_SHA1)) == NULL) {
Ron Eldor3b350852019-05-07 18:31:49 +0300487 ret = MBEDTLS_ERR_SSL_INTERNAL_ERROR;
488 goto exit;
489 }
Manuel Pégourié-Gonnard7da726b2015-03-24 18:08:19 +0100490
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100491 if ((ret = mbedtls_md_setup(&md_ctx, md_info, 1)) != 0) {
Ron Eldor3b350852019-05-07 18:31:49 +0300492 goto exit;
493 }
Manuel Pégourié-Gonnardb7fcca32015-03-26 11:41:28 +0100494
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100495 ret = mbedtls_md_hmac_starts(&md_ctx, S2, hs);
496 if (ret != 0) {
Gilles Peskine2b3f21d2021-12-10 21:35:10 +0100497 goto exit;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100498 }
499 ret = mbedtls_md_hmac_update(&md_ctx, tmp + 20, nb);
500 if (ret != 0) {
Gilles Peskine2b3f21d2021-12-10 21:35:10 +0100501 goto exit;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100502 }
503 ret = mbedtls_md_hmac_finish(&md_ctx, tmp);
504 if (ret != 0) {
Gilles Peskine2b3f21d2021-12-10 21:35:10 +0100505 goto exit;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100506 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000507
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100508 for (i = 0; i < dlen; i += 20) {
509 ret = mbedtls_md_hmac_reset(&md_ctx);
510 if (ret != 0) {
Gilles Peskine2b3f21d2021-12-10 21:35:10 +0100511 goto exit;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100512 }
513 ret = mbedtls_md_hmac_update(&md_ctx, tmp, 20 + nb);
514 if (ret != 0) {
Gilles Peskine2b3f21d2021-12-10 21:35:10 +0100515 goto exit;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100516 }
517 ret = mbedtls_md_hmac_finish(&md_ctx, h_i);
518 if (ret != 0) {
Gilles Peskine2b3f21d2021-12-10 21:35:10 +0100519 goto exit;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100520 }
Manuel Pégourié-Gonnardb7fcca32015-03-26 11:41:28 +0100521
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100522 ret = mbedtls_md_hmac_reset(&md_ctx);
523 if (ret != 0) {
Gilles Peskine2b3f21d2021-12-10 21:35:10 +0100524 goto exit;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100525 }
526 ret = mbedtls_md_hmac_update(&md_ctx, tmp, 20);
527 if (ret != 0) {
Gilles Peskine2b3f21d2021-12-10 21:35:10 +0100528 goto exit;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100529 }
530 ret = mbedtls_md_hmac_finish(&md_ctx, tmp);
531 if (ret != 0) {
Gilles Peskine2b3f21d2021-12-10 21:35:10 +0100532 goto exit;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100533 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000534
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100535 k = (i + 20 > dlen) ? dlen % 20 : 20;
Paul Bakker5121ce52009-01-03 21:22:43 +0000536
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100537 for (j = 0; j < k; j++) {
538 dstbuf[i + j] = (unsigned char) (dstbuf[i + j] ^ h_i[j]);
539 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000540 }
541
Ron Eldor3b350852019-05-07 18:31:49 +0300542exit:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100543 mbedtls_md_free(&md_ctx);
Manuel Pégourié-Gonnardb7fcca32015-03-26 11:41:28 +0100544
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100545 mbedtls_platform_zeroize(tmp, tmp_len);
546 mbedtls_platform_zeroize(h_i, sizeof(h_i));
Paul Bakker5121ce52009-01-03 21:22:43 +0000547
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100548 mbedtls_free(tmp);
549 return ret;
Paul Bakker5121ce52009-01-03 21:22:43 +0000550}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200551#endif /* MBEDTLS_SSL_PROTO_TLS1) || MBEDTLS_SSL_PROTO_TLS1_1 */
Paul Bakker5121ce52009-01-03 21:22:43 +0000552
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200553#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
Andrzej Kurekc929a822019-01-14 03:51:11 -0500554#if defined(MBEDTLS_USE_PSA_CRYPTO)
k-stachowiak81053a52019-08-17 10:30:28 +0200555
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100556static psa_status_t setup_psa_key_derivation(psa_key_derivation_operation_t *derivation,
557 psa_key_id_t key,
558 psa_algorithm_t alg,
559 const unsigned char *seed, size_t seed_length,
560 const unsigned char *label, size_t label_length,
561 size_t capacity)
k-stachowiak81053a52019-08-17 10:30:28 +0200562{
563 psa_status_t status;
564
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100565 status = psa_key_derivation_setup(derivation, alg);
566 if (status != PSA_SUCCESS) {
567 return status;
568 }
k-stachowiak81053a52019-08-17 10:30:28 +0200569
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100570 if (PSA_ALG_IS_TLS12_PRF(alg) || PSA_ALG_IS_TLS12_PSK_TO_MS(alg)) {
571 status = psa_key_derivation_input_bytes(derivation,
572 PSA_KEY_DERIVATION_INPUT_SEED,
573 seed, seed_length);
574 if (status != PSA_SUCCESS) {
575 return status;
576 }
k-stachowiak81053a52019-08-17 10:30:28 +0200577
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100578 if (mbedtls_svc_key_id_is_null(key)) {
Gilles Peskine311f54d2019-09-23 18:19:22 +0200579 status = psa_key_derivation_input_bytes(
580 derivation, PSA_KEY_DERIVATION_INPUT_SECRET,
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100581 NULL, 0);
582 } else {
Gilles Peskine311f54d2019-09-23 18:19:22 +0200583 status = psa_key_derivation_input_key(
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100584 derivation, PSA_KEY_DERIVATION_INPUT_SECRET, key);
Gilles Peskine311f54d2019-09-23 18:19:22 +0200585 }
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100586 if (status != PSA_SUCCESS) {
587 return status;
588 }
k-stachowiak81053a52019-08-17 10:30:28 +0200589
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100590 status = psa_key_derivation_input_bytes(derivation,
591 PSA_KEY_DERIVATION_INPUT_LABEL,
592 label, label_length);
593 if (status != PSA_SUCCESS) {
594 return status;
595 }
596 } else {
597 return PSA_ERROR_NOT_SUPPORTED;
k-stachowiak81053a52019-08-17 10:30:28 +0200598 }
599
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100600 status = psa_key_derivation_set_capacity(derivation, capacity);
601 if (status != PSA_SUCCESS) {
602 return status;
603 }
k-stachowiak81053a52019-08-17 10:30:28 +0200604
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100605 return PSA_SUCCESS;
k-stachowiak81053a52019-08-17 10:30:28 +0200606}
607
Manuel Pégourié-Gonnardd904d662022-06-17 10:24:00 +0200608MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100609static int tls_prf_generic(mbedtls_md_type_t md_type,
610 const unsigned char *secret, size_t slen,
611 const char *label,
612 const unsigned char *random, size_t rlen,
613 unsigned char *dstbuf, size_t dlen)
Andrzej Kurekc929a822019-01-14 03:51:11 -0500614{
615 psa_status_t status;
616 psa_algorithm_t alg;
Ronald Croncf56a0a2020-08-04 09:51:30 +0200617 psa_key_id_t master_key = MBEDTLS_SVC_KEY_ID_INIT;
Janos Follathda6ac012019-08-16 13:47:29 +0100618 psa_key_derivation_operation_t derivation =
Janos Follath8dee8772019-07-30 12:53:32 +0100619 PSA_KEY_DERIVATION_OPERATION_INIT;
Andrzej Kurekc929a822019-01-14 03:51:11 -0500620
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100621 if (md_type == MBEDTLS_MD_SHA384) {
Andrzej Kurekc929a822019-01-14 03:51:11 -0500622 alg = PSA_ALG_TLS12_PRF(PSA_ALG_SHA_384);
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100623 } else {
Andrzej Kurekc929a822019-01-14 03:51:11 -0500624 alg = PSA_ALG_TLS12_PRF(PSA_ALG_SHA_256);
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100625 }
Andrzej Kurekc929a822019-01-14 03:51:11 -0500626
Gilles Peskine311f54d2019-09-23 18:19:22 +0200627 /* Normally a "secret" should be long enough to be impossible to
628 * find by brute force, and in particular should not be empty. But
629 * this PRF is also used to derive an IV, in particular in EAP-TLS,
630 * and for this use case it makes sense to have a 0-length "secret".
631 * Since the key API doesn't allow importing a key of length 0,
Ronald Croncf56a0a2020-08-04 09:51:30 +0200632 * keep master_key=0, which setup_psa_key_derivation() understands
Gilles Peskine311f54d2019-09-23 18:19:22 +0200633 * to mean a 0-length "secret" input. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100634 if (slen != 0) {
Gilles Peskine311f54d2019-09-23 18:19:22 +0200635 psa_key_attributes_t key_attributes = psa_key_attributes_init();
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100636 psa_set_key_usage_flags(&key_attributes, PSA_KEY_USAGE_DERIVE);
637 psa_set_key_algorithm(&key_attributes, alg);
638 psa_set_key_type(&key_attributes, PSA_KEY_TYPE_DERIVE);
Andrzej Kurekc929a822019-01-14 03:51:11 -0500639
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100640 status = psa_import_key(&key_attributes, secret, slen, &master_key);
641 if (status != PSA_SUCCESS) {
642 return MBEDTLS_ERR_SSL_HW_ACCEL_FAILED;
643 }
Gilles Peskine311f54d2019-09-23 18:19:22 +0200644 }
Andrzej Kurekc929a822019-01-14 03:51:11 -0500645
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100646 status = setup_psa_key_derivation(&derivation,
647 master_key, alg,
648 random, rlen,
649 (unsigned char const *) label,
650 (size_t) strlen(label),
651 dlen);
652 if (status != PSA_SUCCESS) {
653 psa_key_derivation_abort(&derivation);
654 psa_destroy_key(master_key);
655 return MBEDTLS_ERR_SSL_HW_ACCEL_FAILED;
Andrzej Kurekc929a822019-01-14 03:51:11 -0500656 }
657
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100658 status = psa_key_derivation_output_bytes(&derivation, dstbuf, dlen);
659 if (status != PSA_SUCCESS) {
660 psa_key_derivation_abort(&derivation);
661 psa_destroy_key(master_key);
662 return MBEDTLS_ERR_SSL_HW_ACCEL_FAILED;
Andrzej Kurekc929a822019-01-14 03:51:11 -0500663 }
664
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100665 status = psa_key_derivation_abort(&derivation);
666 if (status != PSA_SUCCESS) {
667 psa_destroy_key(master_key);
668 return MBEDTLS_ERR_SSL_HW_ACCEL_FAILED;
Andrzej Kurek70737ca2019-01-14 05:37:13 -0500669 }
Andrzej Kurekc929a822019-01-14 03:51:11 -0500670
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100671 if (!mbedtls_svc_key_id_is_null(master_key)) {
672 status = psa_destroy_key(master_key);
673 }
674 if (status != PSA_SUCCESS) {
675 return MBEDTLS_ERR_SSL_HW_ACCEL_FAILED;
676 }
Andrzej Kurekc929a822019-01-14 03:51:11 -0500677
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100678 return 0;
Andrzej Kurekc929a822019-01-14 03:51:11 -0500679}
680
681#else /* MBEDTLS_USE_PSA_CRYPTO */
682
Manuel Pégourié-Gonnardd904d662022-06-17 10:24:00 +0200683MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100684static int tls_prf_generic(mbedtls_md_type_t md_type,
685 const unsigned char *secret, size_t slen,
686 const char *label,
687 const unsigned char *random, size_t rlen,
688 unsigned char *dstbuf, size_t dlen)
Paul Bakker1ef83d62012-04-11 12:09:53 +0000689{
690 size_t nb;
Manuel Pégourié-Gonnard6890c6b2015-03-26 11:11:49 +0100691 size_t i, j, k, md_len;
Ron Eldor3b350852019-05-07 18:31:49 +0300692 unsigned char *tmp;
693 size_t tmp_len = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200694 unsigned char h_i[MBEDTLS_MD_MAX_SIZE];
695 const mbedtls_md_info_t *md_info;
696 mbedtls_md_context_t md_ctx;
Janos Follath865b3eb2019-12-16 11:46:15 +0000697 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnardb7fcca32015-03-26 11:41:28 +0100698
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100699 mbedtls_md_init(&md_ctx);
Paul Bakker1ef83d62012-04-11 12:09:53 +0000700
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100701 if ((md_info = mbedtls_md_info_from_type(md_type)) == NULL) {
702 return MBEDTLS_ERR_SSL_INTERNAL_ERROR;
703 }
Manuel Pégourié-Gonnard6890c6b2015-03-26 11:11:49 +0100704
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100705 md_len = mbedtls_md_get_size(md_info);
Manuel Pégourié-Gonnard6890c6b2015-03-26 11:11:49 +0100706
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100707 tmp_len = md_len + strlen(label) + rlen;
708 tmp = mbedtls_calloc(1, tmp_len);
709 if (tmp == NULL) {
Ron Eldor3b350852019-05-07 18:31:49 +0300710 ret = MBEDTLS_ERR_SSL_ALLOC_FAILED;
711 goto exit;
712 }
Paul Bakker1ef83d62012-04-11 12:09:53 +0000713
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100714 nb = strlen(label);
715 memcpy(tmp + md_len, label, nb);
716 memcpy(tmp + md_len + nb, random, rlen);
Paul Bakker1ef83d62012-04-11 12:09:53 +0000717 nb += rlen;
718
719 /*
720 * Compute P_<hash>(secret, label + random)[0..dlen]
721 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100722 if ((ret = mbedtls_md_setup(&md_ctx, md_info, 1)) != 0) {
Ron Eldor3b350852019-05-07 18:31:49 +0300723 goto exit;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100724 }
Manuel Pégourié-Gonnardb7fcca32015-03-26 11:41:28 +0100725
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100726 ret = mbedtls_md_hmac_starts(&md_ctx, secret, slen);
727 if (ret != 0) {
Gilles Peskine2b3f21d2021-12-10 21:35:10 +0100728 goto exit;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100729 }
730 ret = mbedtls_md_hmac_update(&md_ctx, tmp + md_len, nb);
731 if (ret != 0) {
Gilles Peskine2b3f21d2021-12-10 21:35:10 +0100732 goto exit;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100733 }
734 ret = mbedtls_md_hmac_finish(&md_ctx, tmp);
735 if (ret != 0) {
Gilles Peskine2b3f21d2021-12-10 21:35:10 +0100736 goto exit;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100737 }
Manuel Pégourié-Gonnard7da726b2015-03-24 18:08:19 +0100738
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100739 for (i = 0; i < dlen; i += md_len) {
740 ret = mbedtls_md_hmac_reset(&md_ctx);
741 if (ret != 0) {
Gilles Peskine2b3f21d2021-12-10 21:35:10 +0100742 goto exit;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100743 }
744 ret = mbedtls_md_hmac_update(&md_ctx, tmp, md_len + nb);
745 if (ret != 0) {
Gilles Peskine2b3f21d2021-12-10 21:35:10 +0100746 goto exit;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100747 }
748 ret = mbedtls_md_hmac_finish(&md_ctx, h_i);
749 if (ret != 0) {
Gilles Peskine2b3f21d2021-12-10 21:35:10 +0100750 goto exit;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100751 }
Manuel Pégourié-Gonnardb7fcca32015-03-26 11:41:28 +0100752
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100753 ret = mbedtls_md_hmac_reset(&md_ctx);
754 if (ret != 0) {
Gilles Peskine2b3f21d2021-12-10 21:35:10 +0100755 goto exit;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100756 }
757 ret = mbedtls_md_hmac_update(&md_ctx, tmp, md_len);
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_finish(&md_ctx, tmp);
762 if (ret != 0) {
Gilles Peskine2b3f21d2021-12-10 21:35:10 +0100763 goto exit;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100764 }
Paul Bakker1ef83d62012-04-11 12:09:53 +0000765
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100766 k = (i + md_len > dlen) ? dlen % md_len : md_len;
Paul Bakker1ef83d62012-04-11 12:09:53 +0000767
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100768 for (j = 0; j < k; j++) {
Paul Bakker1ef83d62012-04-11 12:09:53 +0000769 dstbuf[i + j] = h_i[j];
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100770 }
Paul Bakker1ef83d62012-04-11 12:09:53 +0000771 }
772
Ron Eldor3b350852019-05-07 18:31:49 +0300773exit:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100774 mbedtls_md_free(&md_ctx);
Manuel Pégourié-Gonnardb7fcca32015-03-26 11:41:28 +0100775
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100776 if (tmp != NULL) {
777 mbedtls_platform_zeroize(tmp, tmp_len);
778 }
Dave Rodgman369f4952022-11-01 16:08:14 +0000779
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100780 mbedtls_platform_zeroize(h_i, sizeof(h_i));
Paul Bakker1ef83d62012-04-11 12:09:53 +0000781
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100782 mbedtls_free(tmp);
Ron Eldor3b350852019-05-07 18:31:49 +0300783
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100784 return ret;
Paul Bakker1ef83d62012-04-11 12:09:53 +0000785}
Andrzej Kurekc929a822019-01-14 03:51:11 -0500786#endif /* MBEDTLS_USE_PSA_CRYPTO */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200787#if defined(MBEDTLS_SHA256_C)
Manuel Pégourié-Gonnardd904d662022-06-17 10:24:00 +0200788MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100789static int tls_prf_sha256(const unsigned char *secret, size_t slen,
790 const char *label,
791 const unsigned char *random, size_t rlen,
792 unsigned char *dstbuf, size_t dlen)
Manuel Pégourié-Gonnard6890c6b2015-03-26 11:11:49 +0100793{
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100794 return tls_prf_generic(MBEDTLS_MD_SHA256, secret, slen,
795 label, random, rlen, dstbuf, dlen);
Manuel Pégourié-Gonnard6890c6b2015-03-26 11:11:49 +0100796}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200797#endif /* MBEDTLS_SHA256_C */
Paul Bakker1ef83d62012-04-11 12:09:53 +0000798
Gilles Peskined2d59372021-05-12 22:43:27 +0200799#if defined(MBEDTLS_SHA512_C) && !defined(MBEDTLS_SHA512_NO_SHA384)
Manuel Pégourié-Gonnardd904d662022-06-17 10:24:00 +0200800MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100801static int tls_prf_sha384(const unsigned char *secret, size_t slen,
802 const char *label,
803 const unsigned char *random, size_t rlen,
804 unsigned char *dstbuf, size_t dlen)
Paul Bakkerca4ab492012-04-18 14:23:57 +0000805{
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100806 return tls_prf_generic(MBEDTLS_MD_SHA384, secret, slen,
807 label, random, rlen, dstbuf, dlen);
Paul Bakkerca4ab492012-04-18 14:23:57 +0000808}
Gilles Peskined2d59372021-05-12 22:43:27 +0200809#endif /* MBEDTLS_SHA512_C && !MBEDTLS_SHA512_NO_SHA384 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200810#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakkerca4ab492012-04-18 14:23:57 +0000811
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100812static void ssl_update_checksum_start(mbedtls_ssl_context *, const unsigned char *, size_t);
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200813
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200814#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1) || \
815 defined(MBEDTLS_SSL_PROTO_TLS1_1)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100816static void ssl_update_checksum_md5sha1(mbedtls_ssl_context *, const unsigned char *, size_t);
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200817#endif
Paul Bakker380da532012-04-18 16:10:25 +0000818
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200819#if defined(MBEDTLS_SSL_PROTO_SSL3)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100820static void ssl_calc_verify_ssl(const mbedtls_ssl_context *, unsigned char *, size_t *);
821static void ssl_calc_finished_ssl(mbedtls_ssl_context *, unsigned char *, int);
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200822#endif
823
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200824#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100825static void ssl_calc_verify_tls(const mbedtls_ssl_context *, unsigned char *, size_t *);
826static void ssl_calc_finished_tls(mbedtls_ssl_context *, unsigned char *, int);
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200827#endif
828
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200829#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
830#if defined(MBEDTLS_SHA256_C)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100831static void ssl_update_checksum_sha256(mbedtls_ssl_context *, const unsigned char *, size_t);
832static void ssl_calc_verify_tls_sha256(const mbedtls_ssl_context *, unsigned char *, size_t *);
833static void ssl_calc_finished_tls_sha256(mbedtls_ssl_context *, unsigned char *, int);
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200834#endif
Paul Bakker769075d2012-11-24 11:26:46 +0100835
Gilles Peskined2d59372021-05-12 22:43:27 +0200836#if defined(MBEDTLS_SHA512_C) && !defined(MBEDTLS_SHA512_NO_SHA384)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100837static void ssl_update_checksum_sha384(mbedtls_ssl_context *, const unsigned char *, size_t);
838static void ssl_calc_verify_tls_sha384(const mbedtls_ssl_context *, unsigned char *, size_t *);
839static void ssl_calc_finished_tls_sha384(mbedtls_ssl_context *, unsigned char *, int);
Paul Bakker769075d2012-11-24 11:26:46 +0100840#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200841#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakker1ef83d62012-04-11 12:09:53 +0000842
Manuel Pégourié-Gonnard45be3d82019-02-18 23:35:14 +0100843#if defined(MBEDTLS_KEY_EXCHANGE_PSK_ENABLED) && \
Hanno Becker7d0a5692018-10-23 15:26:22 +0100844 defined(MBEDTLS_USE_PSA_CRYPTO)
Manuel Pégourié-Gonnardd904d662022-06-17 10:24:00 +0200845MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100846static int ssl_use_opaque_psk(mbedtls_ssl_context const *ssl)
Hanno Becker7d0a5692018-10-23 15:26:22 +0100847{
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100848 if (ssl->conf->f_psk != NULL) {
Hanno Becker7d0a5692018-10-23 15:26:22 +0100849 /* If we've used a callback to select the PSK,
850 * the static configuration is irrelevant. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100851 if (!mbedtls_svc_key_id_is_null(ssl->handshake->psk_opaque)) {
852 return 1;
853 }
Hanno Becker7d0a5692018-10-23 15:26:22 +0100854
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100855 return 0;
Hanno Becker7d0a5692018-10-23 15:26:22 +0100856 }
857
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100858 if (!mbedtls_svc_key_id_is_null(ssl->conf->psk_opaque)) {
859 return 1;
860 }
Hanno Becker7d0a5692018-10-23 15:26:22 +0100861
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100862 return 0;
Hanno Becker7d0a5692018-10-23 15:26:22 +0100863}
864#endif /* MBEDTLS_USE_PSA_CRYPTO &&
Manuel Pégourié-Gonnard45be3d82019-02-18 23:35:14 +0100865 MBEDTLS_KEY_EXCHANGE_PSK_ENABLED */
Hanno Becker7d0a5692018-10-23 15:26:22 +0100866
Ron Eldorcf280092019-05-14 20:19:13 +0300867#if defined(MBEDTLS_SSL_EXPORT_KEYS)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100868static mbedtls_tls_prf_types tls_prf_get_type(mbedtls_ssl_tls_prf_cb *tls_prf)
Ron Eldorcf280092019-05-14 20:19:13 +0300869{
870#if defined(MBEDTLS_SSL_PROTO_SSL3)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100871 if (tls_prf == ssl3_prf) {
872 return MBEDTLS_SSL_TLS_PRF_SSL3;
873 } else
Ron Eldorcf280092019-05-14 20:19:13 +0300874#endif
875#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100876 if (tls_prf == tls1_prf) {
877 return MBEDTLS_SSL_TLS_PRF_TLS1;
878 } else
Ron Eldorcf280092019-05-14 20:19:13 +0300879#endif
880#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
Gilles Peskined2d59372021-05-12 22:43:27 +0200881#if defined(MBEDTLS_SHA512_C) && !defined(MBEDTLS_SHA512_NO_SHA384)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100882 if (tls_prf == tls_prf_sha384) {
883 return MBEDTLS_SSL_TLS_PRF_SHA384;
884 } else
Ron Eldorcf280092019-05-14 20:19:13 +0300885#endif
886#if defined(MBEDTLS_SHA256_C)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100887 if (tls_prf == tls_prf_sha256) {
888 return MBEDTLS_SSL_TLS_PRF_SHA256;
889 } else
Ron Eldorcf280092019-05-14 20:19:13 +0300890#endif
891#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100892 return MBEDTLS_SSL_TLS_PRF_NONE;
Ron Eldorcf280092019-05-14 20:19:13 +0300893}
894#endif /* MBEDTLS_SSL_EXPORT_KEYS */
895
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100896int mbedtls_ssl_tls_prf(const mbedtls_tls_prf_types prf,
897 const unsigned char *secret, size_t slen,
898 const char *label,
899 const unsigned char *random, size_t rlen,
900 unsigned char *dstbuf, size_t dlen)
Ron Eldor51d3ab52019-05-12 14:54:30 +0300901{
902 mbedtls_ssl_tls_prf_cb *tls_prf = NULL;
903
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100904 switch (prf) {
Ron Eldor51d3ab52019-05-12 14:54:30 +0300905#if defined(MBEDTLS_SSL_PROTO_SSL3)
906 case MBEDTLS_SSL_TLS_PRF_SSL3:
907 tls_prf = ssl3_prf;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100908 break;
Ron Eldord2f25f72019-05-15 14:54:22 +0300909#endif /* MBEDTLS_SSL_PROTO_SSL3 */
Ron Eldor51d3ab52019-05-12 14:54:30 +0300910#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1)
911 case MBEDTLS_SSL_TLS_PRF_TLS1:
912 tls_prf = tls1_prf;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100913 break;
Ron Eldord2f25f72019-05-15 14:54:22 +0300914#endif /* MBEDTLS_SSL_PROTO_TLS1 || MBEDTLS_SSL_PROTO_TLS1_1 */
915
916#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
Gilles Peskined2d59372021-05-12 22:43:27 +0200917#if defined(MBEDTLS_SHA512_C) && !defined(MBEDTLS_SHA512_NO_SHA384)
Ron Eldor51d3ab52019-05-12 14:54:30 +0300918 case MBEDTLS_SSL_TLS_PRF_SHA384:
919 tls_prf = tls_prf_sha384;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100920 break;
Gilles Peskined2d59372021-05-12 22:43:27 +0200921#endif /* MBEDTLS_SHA512_C && !MBEDTLS_SHA512_NO_SHA384 */
Ron Eldor51d3ab52019-05-12 14:54:30 +0300922#if defined(MBEDTLS_SHA256_C)
923 case MBEDTLS_SSL_TLS_PRF_SHA256:
924 tls_prf = tls_prf_sha256;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100925 break;
Ron Eldord2f25f72019-05-15 14:54:22 +0300926#endif /* MBEDTLS_SHA256_C */
927#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100928 default:
929 return MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
Ron Eldor51d3ab52019-05-12 14:54:30 +0300930 }
931
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100932 return tls_prf(secret, slen, label, random, rlen, dstbuf, dlen);
Ron Eldor51d3ab52019-05-12 14:54:30 +0300933}
934
Manuel Pégourié-Gonnard9b108c22019-05-06 13:32:17 +0200935/* Type for the TLS PRF */
936typedef int ssl_tls_prf_t(const unsigned char *, size_t, const char *,
937 const unsigned char *, size_t,
938 unsigned char *, size_t);
939
Manuel Pégourié-Gonnarde59ae232019-04-30 11:41:40 +0200940/*
Manuel Pégourié-Gonnardcba40d92019-05-06 12:55:40 +0200941 * Populate a transform structure with session keys and all the other
942 * necessary information.
Manuel Pégourié-Gonnarde59ae232019-04-30 11:41:40 +0200943 *
Manuel Pégourié-Gonnardcba40d92019-05-06 12:55:40 +0200944 * Parameters:
945 * - [in/out]: transform: structure to populate
946 * [in] must be just initialised with mbedtls_ssl_transform_init()
Manuel Pégourié-Gonnardc864f6a2019-05-06 13:48:22 +0200947 * [out] fully populated, ready for use by mbedtls_ssl_{en,de}crypt_buf()
Manuel Pégourié-Gonnard6fa57bf2019-05-10 10:50:04 +0200948 * - [in] ciphersuite
949 * - [in] master
950 * - [in] encrypt_then_mac
951 * - [in] trunc_hmac
952 * - [in] compression
Manuel Pégourié-Gonnard9b108c22019-05-06 13:32:17 +0200953 * - [in] tls_prf: pointer to PRF to use for key derivation
954 * - [in] randbytes: buffer holding ServerHello.random + ClientHello.random
Manuel Pégourié-Gonnardc864f6a2019-05-06 13:48:22 +0200955 * - [in] minor_ver: SSL/TLS minor version
956 * - [in] endpoint: client or server
957 * - [in] ssl: optionally used for:
Manuel Pégourié-Gonnarde07bc202020-02-26 09:53:42 +0100958 * - MBEDTLS_SSL_HW_RECORD_ACCEL: whole context (non-const)
Manuel Pégourié-Gonnardc864f6a2019-05-06 13:48:22 +0200959 * - MBEDTLS_SSL_EXPORT_KEYS: ssl->conf->{f,p}_export_keys
960 * - MBEDTLS_DEBUG_C: ssl->conf->{f,p}_dbg
Manuel Pégourié-Gonnarde59ae232019-04-30 11:41:40 +0200961 */
Manuel Pégourié-Gonnardd904d662022-06-17 10:24:00 +0200962MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100963static int ssl_populate_transform(mbedtls_ssl_transform *transform,
964 int ciphersuite,
965 const unsigned char master[48],
Jarno Lamsac84bd242019-08-16 12:06:56 +0300966#if defined(MBEDTLS_SSL_SOME_MODES_USE_MAC)
Manuel Pégourié-Gonnard6fa57bf2019-05-10 10:50:04 +0200967#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100968 int encrypt_then_mac,
Jarno Lamsac84bd242019-08-16 12:06:56 +0300969#endif /* MBEDTLS_SSL_ENCRYPT_THEN_MAC */
Manuel Pégourié-Gonnard6fa57bf2019-05-10 10:50:04 +0200970#if defined(MBEDTLS_SSL_TRUNCATED_HMAC)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100971 int trunc_hmac,
Jarno Lamsac84bd242019-08-16 12:06:56 +0300972#endif /* MBEDTLS_SSL_TRUNCATED_HMAC */
973#endif /* MBEDTLS_SSL_SOME_MODES_USE_MAC */
Manuel Pégourié-Gonnard6fa57bf2019-05-10 10:50:04 +0200974#if defined(MBEDTLS_ZLIB_SUPPORT)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100975 int compression,
Manuel Pégourié-Gonnard6fa57bf2019-05-10 10:50:04 +0200976#endif
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100977 ssl_tls_prf_t tls_prf,
978 const unsigned char randbytes[64],
979 int minor_ver,
980 unsigned endpoint,
Manuel Pégourié-Gonnard7ae6ed42020-03-13 11:28:19 +0100981#if !defined(MBEDTLS_SSL_HW_RECORD_ACCEL)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100982 const
Manuel Pégourié-Gonnard7ae6ed42020-03-13 11:28:19 +0100983#endif
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100984 mbedtls_ssl_context *ssl)
Paul Bakker5121ce52009-01-03 21:22:43 +0000985{
Paul Bakkerda02a7f2013-08-31 17:25:14 +0200986 int ret = 0;
Hanno Beckercb1cc802018-11-17 22:27:38 +0000987#if defined(MBEDTLS_USE_PSA_CRYPTO)
988 int psa_fallthrough;
989#endif /* MBEDTLS_USE_PSA_CRYPTO */
David Horstmannd4f22082022-10-26 18:25:14 +0100990 int do_mbedtls_cipher_setup;
Paul Bakker5121ce52009-01-03 21:22:43 +0000991 unsigned char keyblk[256];
992 unsigned char *key1;
993 unsigned char *key2;
Paul Bakker68884e32013-01-07 18:20:04 +0100994 unsigned char *mac_enc;
995 unsigned char *mac_dec;
sander-visser3888b032020-05-06 21:49:46 +0200996 size_t mac_key_len = 0;
Paul Bakkerb9cfaa02013-10-11 18:58:55 +0200997 size_t iv_copy_len;
Hanno Becker88aaf652017-12-27 08:17:40 +0000998 unsigned keylen;
Hanno Beckere694c3e2017-12-27 21:34:08 +0000999 const mbedtls_ssl_ciphersuite_t *ciphersuite_info;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001000 const mbedtls_cipher_info_t *cipher_info;
1001 const mbedtls_md_info_t *md_info;
Paul Bakker68884e32013-01-07 18:20:04 +01001002
Manuel Pégourié-Gonnardc864f6a2019-05-06 13:48:22 +02001003#if !defined(MBEDTLS_SSL_HW_RECORD_ACCEL) && \
1004 !defined(MBEDTLS_SSL_EXPORT_KEYS) && \
Gilles Peskinea6f99a12022-04-13 13:24:56 +02001005 !defined(MBEDTLS_SSL_DTLS_CONNECTION_ID) && \
Manuel Pégourié-Gonnardc864f6a2019-05-06 13:48:22 +02001006 !defined(MBEDTLS_DEBUG_C)
Paul Elliott9c2faca2023-10-18 14:34:46 +01001007 (void) ssl; /* ssl is unused except for those cases */
Manuel Pégourié-Gonnardc864f6a2019-05-06 13:48:22 +02001008#endif
1009
Manuel Pégourié-Gonnard96fb0ee2019-07-09 12:54:17 +02001010 /*
1011 * Some data just needs copying into the structure
1012 */
Jaeden Amero2de07f12019-06-05 13:32:08 +01001013#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC) && \
1014 defined(MBEDTLS_SSL_SOME_MODES_USE_MAC)
Manuel Pégourié-Gonnard6fa57bf2019-05-10 10:50:04 +02001015 transform->encrypt_then_mac = encrypt_then_mac;
Hanno Becker9eddaeb2017-12-27 21:37:21 +00001016#endif
Manuel Pégourié-Gonnardc864f6a2019-05-06 13:48:22 +02001017 transform->minor_ver = minor_ver;
Hanno Beckere694c3e2017-12-27 21:34:08 +00001018
Manuel Pégourié-Gonnard96fb0ee2019-07-09 12:54:17 +02001019#if defined(MBEDTLS_SSL_CONTEXT_SERIALIZATION)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001020 memcpy(transform->randbytes, randbytes, sizeof(transform->randbytes));
Manuel Pégourié-Gonnard96fb0ee2019-07-09 12:54:17 +02001021#endif
1022
Manuel Pégourié-Gonnard9b108c22019-05-06 13:32:17 +02001023 /*
1024 * Get various info structures
1025 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001026 ciphersuite_info = mbedtls_ssl_ciphersuite_from_id(ciphersuite);
1027 if (ciphersuite_info == NULL) {
1028 MBEDTLS_SSL_DEBUG_MSG(1, ("ciphersuite info for %d not found",
1029 ciphersuite));
1030 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
Manuel Pégourié-Gonnard9b108c22019-05-06 13:32:17 +02001031 }
1032
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001033 cipher_info = mbedtls_cipher_info_from_type(ciphersuite_info->cipher);
1034 if (cipher_info == NULL) {
1035 MBEDTLS_SSL_DEBUG_MSG(1, ("cipher info for %u not found",
1036 ciphersuite_info->cipher));
1037 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
Paul Bakker68884e32013-01-07 18:20:04 +01001038 }
1039
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001040 md_info = mbedtls_md_info_from_type(ciphersuite_info->mac);
1041 if (md_info == NULL) {
1042 MBEDTLS_SSL_DEBUG_MSG(1, ("mbedtls_md info for %u not found",
1043 (unsigned) ciphersuite_info->mac));
1044 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
Paul Bakker68884e32013-01-07 18:20:04 +01001045 }
1046
Hanno Beckera0e20d02019-05-15 14:03:01 +01001047#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Becker4bf74652019-04-26 16:22:27 +01001048 /* Copy own and peer's CID if the use of the CID
1049 * extension has been negotiated. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001050 if (ssl->handshake->cid_in_use == MBEDTLS_SSL_CID_ENABLED) {
1051 MBEDTLS_SSL_DEBUG_MSG(3, ("Copy CIDs into SSL transform"));
Hanno Becker8a7f9722019-04-30 13:52:29 +01001052
Hanno Becker05154c32019-05-03 15:23:51 +01001053 transform->in_cid_len = ssl->own_cid_len;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001054 memcpy(transform->in_cid, ssl->own_cid, ssl->own_cid_len);
1055 MBEDTLS_SSL_DEBUG_BUF(3, "Incoming CID", transform->in_cid,
1056 transform->in_cid_len);
Hanno Beckerd1f20352019-05-15 10:21:55 +01001057
1058 transform->out_cid_len = ssl->handshake->peer_cid_len;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001059 memcpy(transform->out_cid, ssl->handshake->peer_cid,
1060 ssl->handshake->peer_cid_len);
1061 MBEDTLS_SSL_DEBUG_BUF(3, "Outgoing CID", transform->out_cid,
1062 transform->out_cid_len);
Hanno Becker4bf74652019-04-26 16:22:27 +01001063 }
Hanno Beckera0e20d02019-05-15 14:03:01 +01001064#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Becker4bf74652019-04-26 16:22:27 +01001065
Paul Bakker5121ce52009-01-03 21:22:43 +00001066 /*
Manuel Pégourié-Gonnard9b108c22019-05-06 13:32:17 +02001067 * Compute key block using the PRF
Paul Bakker5121ce52009-01-03 21:22:43 +00001068 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001069 ret = tls_prf(master, 48, "key expansion", randbytes, 64, keyblk, 256);
1070 if (ret != 0) {
1071 MBEDTLS_SSL_DEBUG_RET(1, "prf", ret);
1072 return ret;
Manuel Pégourié-Gonnarde9608182015-03-26 11:47:47 +01001073 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001074
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001075 MBEDTLS_SSL_DEBUG_MSG(3, ("ciphersuite = %s",
1076 mbedtls_ssl_get_ciphersuite_name(ciphersuite)));
1077 MBEDTLS_SSL_DEBUG_BUF(3, "master secret", master, 48);
1078 MBEDTLS_SSL_DEBUG_BUF(4, "random bytes", randbytes, 64);
1079 MBEDTLS_SSL_DEBUG_BUF(4, "key block", keyblk, 256);
Paul Bakker5121ce52009-01-03 21:22:43 +00001080
Paul Bakker5121ce52009-01-03 21:22:43 +00001081 /*
1082 * Determine the appropriate key, IV and MAC length.
1083 */
Paul Bakker68884e32013-01-07 18:20:04 +01001084
Hanno Becker88aaf652017-12-27 08:17:40 +00001085 keylen = cipher_info->key_bitlen / 8;
Manuel Pégourié-Gonnarde800cd82014-06-18 15:34:40 +02001086
Hanno Becker8031d062018-01-03 15:32:31 +00001087#if defined(MBEDTLS_GCM_C) || \
1088 defined(MBEDTLS_CCM_C) || \
1089 defined(MBEDTLS_CHACHAPOLY_C)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001090 if (cipher_info->mode == MBEDTLS_MODE_GCM ||
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02001091 cipher_info->mode == MBEDTLS_MODE_CCM ||
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001092 cipher_info->mode == MBEDTLS_MODE_CHACHAPOLY) {
Hanno Beckerf704bef2018-11-16 15:21:18 +00001093 size_t explicit_ivlen;
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02001094
Manuel Pégourié-Gonnarde800cd82014-06-18 15:34:40 +02001095 transform->maclen = 0;
Hanno Becker81c7b182017-11-09 18:39:33 +00001096 mac_key_len = 0;
Hanno Beckere694c3e2017-12-27 21:34:08 +00001097 transform->taglen =
1098 ciphersuite_info->flags & MBEDTLS_CIPHERSUITE_SHORT_TAG ? 8 : 16;
Manuel Pégourié-Gonnarde800cd82014-06-18 15:34:40 +02001099
Hanno Becker447558d2020-05-28 07:36:33 +01001100 /* All modes haves 96-bit IVs, but the length of the static parts vary
1101 * with mode and version:
1102 * - For GCM and CCM in TLS 1.2, there's a static IV of 4 Bytes
1103 * (to be concatenated with a dynamically chosen IV of 8 Bytes)
Hanno Beckerf93c2d72020-05-28 07:39:43 +01001104 * - For ChaChaPoly in TLS 1.2, and all modes in TLS 1.3, there's
1105 * a static IV of 12 Bytes (to be XOR'ed with the 8 Byte record
1106 * sequence number).
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02001107 */
Paul Bakker68884e32013-01-07 18:20:04 +01001108 transform->ivlen = 12;
Hanno Beckerf93c2d72020-05-28 07:39:43 +01001109#if defined(MBEDTLS_SSL_PROTO_TLS1_3_EXPERIMENTAL)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001110 if (minor_ver == MBEDTLS_SSL_MINOR_VERSION_4) {
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02001111 transform->fixed_ivlen = 12;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001112 } else
Hanno Beckerf93c2d72020-05-28 07:39:43 +01001113#endif /* MBEDTLS_SSL_PROTO_TLS1_3_EXPERIMENTAL */
1114 {
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001115 if (cipher_info->mode == MBEDTLS_MODE_CHACHAPOLY) {
Hanno Beckerf93c2d72020-05-28 07:39:43 +01001116 transform->fixed_ivlen = 12;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001117 } else {
Hanno Beckerf93c2d72020-05-28 07:39:43 +01001118 transform->fixed_ivlen = 4;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001119 }
Hanno Beckerf93c2d72020-05-28 07:39:43 +01001120 }
Manuel Pégourié-Gonnarde800cd82014-06-18 15:34:40 +02001121
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02001122 /* Minimum length of encrypted record */
1123 explicit_ivlen = transform->ivlen - transform->fixed_ivlen;
Hanno Beckere694c3e2017-12-27 21:34:08 +00001124 transform->minlen = explicit_ivlen + transform->taglen;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001125 } else
Hanno Becker8031d062018-01-03 15:32:31 +00001126#endif /* MBEDTLS_GCM_C || MBEDTLS_CCM_C || MBEDTLS_CHACHAPOLY_C */
1127#if defined(MBEDTLS_SSL_SOME_MODES_USE_MAC)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001128 if (cipher_info->mode == MBEDTLS_MODE_STREAM ||
1129 cipher_info->mode == MBEDTLS_MODE_CBC) {
Manuel Pégourié-Gonnarde800cd82014-06-18 15:34:40 +02001130 /* Initialize HMAC contexts */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001131 if ((ret = mbedtls_md_setup(&transform->md_ctx_enc, md_info, 1)) != 0 ||
1132 (ret = mbedtls_md_setup(&transform->md_ctx_dec, md_info, 1)) != 0) {
1133 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_md_setup", ret);
Ron Eldore6992702019-05-07 18:27:13 +03001134 goto end;
Paul Bakker68884e32013-01-07 18:20:04 +01001135 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001136
Manuel Pégourié-Gonnarde800cd82014-06-18 15:34:40 +02001137 /* Get MAC length */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001138 mac_key_len = mbedtls_md_get_size(md_info);
Hanno Becker81c7b182017-11-09 18:39:33 +00001139 transform->maclen = mac_key_len;
Manuel Pégourié-Gonnarde800cd82014-06-18 15:34:40 +02001140
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001141#if defined(MBEDTLS_SSL_TRUNCATED_HMAC)
Manuel Pégourié-Gonnarde800cd82014-06-18 15:34:40 +02001142 /*
1143 * If HMAC is to be truncated, we shall keep the leftmost bytes,
1144 * (rfc 6066 page 13 or rfc 2104 section 4),
1145 * so we only need to adjust the length here.
1146 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001147 if (trunc_hmac == MBEDTLS_SSL_TRUNC_HMAC_ENABLED) {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001148 transform->maclen = MBEDTLS_SSL_TRUNCATED_HMAC_LEN;
Hanno Beckere89353a2017-11-20 16:36:41 +00001149
1150#if defined(MBEDTLS_SSL_TRUNCATED_HMAC_COMPAT)
1151 /* Fall back to old, non-compliant version of the truncated
Hanno Becker563423f2017-11-21 17:20:17 +00001152 * HMAC implementation which also truncates the key
1153 * (Mbed TLS versions from 1.3 to 2.6.0) */
Hanno Beckere89353a2017-11-20 16:36:41 +00001154 mac_key_len = transform->maclen;
1155#endif
1156 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001157#endif /* MBEDTLS_SSL_TRUNCATED_HMAC */
Manuel Pégourié-Gonnarde800cd82014-06-18 15:34:40 +02001158
1159 /* IV length */
Paul Bakker68884e32013-01-07 18:20:04 +01001160 transform->ivlen = cipher_info->iv_size;
Paul Bakker5121ce52009-01-03 21:22:43 +00001161
Manuel Pégourié-Gonnardeaa76f72014-06-18 16:06:02 +02001162 /* Minimum length */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001163 if (cipher_info->mode == MBEDTLS_MODE_STREAM) {
Manuel Pégourié-Gonnardeaa76f72014-06-18 16:06:02 +02001164 transform->minlen = transform->maclen;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001165 } else {
Manuel Pégourié-Gonnardeaa76f72014-06-18 16:06:02 +02001166 /*
1167 * GenericBlockCipher:
Manuel Pégourié-Gonnard169dd6a2014-11-04 16:15:39 +01001168 * 1. if EtM is in use: one block plus MAC
1169 * otherwise: * first multiple of blocklen greater than maclen
1170 * 2. IV except for SSL3 and TLS 1.0
Manuel Pégourié-Gonnardeaa76f72014-06-18 16:06:02 +02001171 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001172#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001173 if (encrypt_then_mac == MBEDTLS_SSL_ETM_ENABLED) {
Manuel Pégourié-Gonnard169dd6a2014-11-04 16:15:39 +01001174 transform->minlen = transform->maclen
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001175 + cipher_info->block_size;
1176 } else
Manuel Pégourié-Gonnard169dd6a2014-11-04 16:15:39 +01001177#endif
1178 {
1179 transform->minlen = transform->maclen
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001180 + cipher_info->block_size
1181 - transform->maclen % cipher_info->block_size;
Manuel Pégourié-Gonnard169dd6a2014-11-04 16:15:39 +01001182 }
Manuel Pégourié-Gonnardeaa76f72014-06-18 16:06:02 +02001183
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001184#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001185 if (minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 ||
1186 minor_ver == MBEDTLS_SSL_MINOR_VERSION_1) {
Manuel Pégourié-Gonnardeaa76f72014-06-18 16:06:02 +02001187 ; /* No need to adjust minlen */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001188 } else
Manuel Pégourié-Gonnardeaa76f72014-06-18 16:06:02 +02001189#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001190#if defined(MBEDTLS_SSL_PROTO_TLS1_1) || defined(MBEDTLS_SSL_PROTO_TLS1_2)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001191 if (minor_ver == MBEDTLS_SSL_MINOR_VERSION_2 ||
1192 minor_ver == MBEDTLS_SSL_MINOR_VERSION_3) {
Manuel Pégourié-Gonnardeaa76f72014-06-18 16:06:02 +02001193 transform->minlen += transform->ivlen;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001194 } else
Manuel Pégourié-Gonnardeaa76f72014-06-18 16:06:02 +02001195#endif
1196 {
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001197 MBEDTLS_SSL_DEBUG_MSG(1, ("should never happen"));
Ron Eldore6992702019-05-07 18:27:13 +03001198 ret = MBEDTLS_ERR_SSL_INTERNAL_ERROR;
1199 goto end;
Manuel Pégourié-Gonnardeaa76f72014-06-18 16:06:02 +02001200 }
Paul Bakker68884e32013-01-07 18:20:04 +01001201 }
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001202 } else
Hanno Becker8031d062018-01-03 15:32:31 +00001203#endif /* MBEDTLS_SSL_SOME_MODES_USE_MAC */
1204 {
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001205 MBEDTLS_SSL_DEBUG_MSG(1, ("should never happen"));
1206 return MBEDTLS_ERR_SSL_INTERNAL_ERROR;
Hanno Becker8031d062018-01-03 15:32:31 +00001207 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001208
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001209 MBEDTLS_SSL_DEBUG_MSG(3, ("keylen: %u, minlen: %u, ivlen: %u, maclen: %u",
1210 (unsigned) keylen,
1211 (unsigned) transform->minlen,
1212 (unsigned) transform->ivlen,
1213 (unsigned) transform->maclen));
Paul Bakker5121ce52009-01-03 21:22:43 +00001214
1215 /*
1216 * Finally setup the cipher contexts, IVs and MAC secrets.
1217 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001218#if defined(MBEDTLS_SSL_CLI_C)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001219 if (endpoint == MBEDTLS_SSL_IS_CLIENT) {
Hanno Becker81c7b182017-11-09 18:39:33 +00001220 key1 = keyblk + mac_key_len * 2;
Hanno Becker88aaf652017-12-27 08:17:40 +00001221 key2 = keyblk + mac_key_len * 2 + keylen;
Paul Bakker5121ce52009-01-03 21:22:43 +00001222
Paul Bakker68884e32013-01-07 18:20:04 +01001223 mac_enc = keyblk;
Hanno Becker81c7b182017-11-09 18:39:33 +00001224 mac_dec = keyblk + mac_key_len;
Paul Bakker5121ce52009-01-03 21:22:43 +00001225
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001226 /*
1227 * This is not used in TLS v1.1.
1228 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001229 iv_copy_len = (transform->fixed_ivlen) ?
1230 transform->fixed_ivlen : transform->ivlen;
1231 memcpy(transform->iv_enc, key2 + keylen, iv_copy_len);
1232 memcpy(transform->iv_dec, key2 + keylen + iv_copy_len,
1233 iv_copy_len);
1234 } else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001235#endif /* MBEDTLS_SSL_CLI_C */
1236#if defined(MBEDTLS_SSL_SRV_C)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001237 if (endpoint == MBEDTLS_SSL_IS_SERVER) {
Hanno Becker88aaf652017-12-27 08:17:40 +00001238 key1 = keyblk + mac_key_len * 2 + keylen;
Hanno Becker81c7b182017-11-09 18:39:33 +00001239 key2 = keyblk + mac_key_len * 2;
Paul Bakker5121ce52009-01-03 21:22:43 +00001240
Hanno Becker81c7b182017-11-09 18:39:33 +00001241 mac_enc = keyblk + mac_key_len;
Paul Bakker68884e32013-01-07 18:20:04 +01001242 mac_dec = keyblk;
Paul Bakker5121ce52009-01-03 21:22:43 +00001243
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001244 /*
1245 * This is not used in TLS v1.1.
1246 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001247 iv_copy_len = (transform->fixed_ivlen) ?
1248 transform->fixed_ivlen : transform->ivlen;
1249 memcpy(transform->iv_dec, key1 + keylen, iv_copy_len);
1250 memcpy(transform->iv_enc, key1 + keylen + iv_copy_len,
1251 iv_copy_len);
1252 } else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001253#endif /* MBEDTLS_SSL_SRV_C */
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01001254 {
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001255 MBEDTLS_SSL_DEBUG_MSG(1, ("should never happen"));
Ron Eldore6992702019-05-07 18:27:13 +03001256 ret = MBEDTLS_ERR_SSL_INTERNAL_ERROR;
1257 goto end;
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01001258 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001259
Hanno Beckerd56ed242018-01-03 15:32:51 +00001260#if defined(MBEDTLS_SSL_SOME_MODES_USE_MAC)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001261#if defined(MBEDTLS_SSL_PROTO_SSL3)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001262 if (minor_ver == MBEDTLS_SSL_MINOR_VERSION_0) {
1263 if (mac_key_len > sizeof(transform->mac_enc)) {
1264 MBEDTLS_SSL_DEBUG_MSG(1, ("should never happen"));
Ron Eldore6992702019-05-07 18:27:13 +03001265 ret = MBEDTLS_ERR_SSL_INTERNAL_ERROR;
1266 goto end;
Manuel Pégourié-Gonnard7cfdcb82014-01-18 18:22:55 +01001267 }
1268
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001269 memcpy(transform->mac_enc, mac_enc, mac_key_len);
1270 memcpy(transform->mac_dec, mac_dec, mac_key_len);
1271 } else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001272#endif /* MBEDTLS_SSL_PROTO_SSL3 */
1273#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1) || \
1274 defined(MBEDTLS_SSL_PROTO_TLS1_2)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001275 if (minor_ver >= MBEDTLS_SSL_MINOR_VERSION_1) {
Gilles Peskine039fd122018-03-19 19:06:08 +01001276 /* For HMAC-based ciphersuites, initialize the HMAC transforms.
1277 For AEAD-based ciphersuites, there is nothing to do here. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001278 if (mac_key_len != 0) {
1279 ret = mbedtls_md_hmac_starts(&transform->md_ctx_enc,
1280 mac_enc, mac_key_len);
1281 if (ret != 0) {
Gilles Peskine2b3f21d2021-12-10 21:35:10 +01001282 goto end;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001283 }
1284 ret = mbedtls_md_hmac_starts(&transform->md_ctx_dec,
1285 mac_dec, mac_key_len);
1286 if (ret != 0) {
Gilles Peskine2b3f21d2021-12-10 21:35:10 +01001287 goto end;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001288 }
Gilles Peskine039fd122018-03-19 19:06:08 +01001289 }
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001290 } else
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001291#endif
Paul Bakker577e0062013-08-28 11:57:20 +02001292 {
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001293 MBEDTLS_SSL_DEBUG_MSG(1, ("should never happen"));
Ron Eldore6992702019-05-07 18:27:13 +03001294 ret = MBEDTLS_ERR_SSL_INTERNAL_ERROR;
1295 goto end;
Paul Bakker577e0062013-08-28 11:57:20 +02001296 }
Hanno Beckerd56ed242018-01-03 15:32:51 +00001297#endif /* MBEDTLS_SSL_SOME_MODES_USE_MAC */
Paul Bakker68884e32013-01-07 18:20:04 +01001298
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001299#if defined(MBEDTLS_SSL_HW_RECORD_ACCEL)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001300 if (mbedtls_ssl_hw_record_init != NULL) {
sander-visser1abe8ee2020-05-06 21:27:14 +02001301 ret = 0;
Paul Bakker05ef8352012-05-08 09:17:57 +00001302
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001303 MBEDTLS_SSL_DEBUG_MSG(2, ("going for mbedtls_ssl_hw_record_init()"));
Paul Bakker05ef8352012-05-08 09:17:57 +00001304
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001305 if ((ret = mbedtls_ssl_hw_record_init(ssl, key1, key2, keylen,
1306 transform->iv_enc, transform->iv_dec,
1307 iv_copy_len,
1308 mac_enc, mac_dec,
1309 mac_key_len)) != 0) {
1310 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ssl_hw_record_init", ret);
Ron Eldore6992702019-05-07 18:27:13 +03001311 ret = MBEDTLS_ERR_SSL_HW_ACCEL_FAILED;
1312 goto end;
Paul Bakker05ef8352012-05-08 09:17:57 +00001313 }
1314 }
Hanno Beckerd56ed242018-01-03 15:32:51 +00001315#else
1316 ((void) mac_dec);
1317 ((void) mac_enc);
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001318#endif /* MBEDTLS_SSL_HW_RECORD_ACCEL */
Paul Bakker05ef8352012-05-08 09:17:57 +00001319
Manuel Pégourié-Gonnard024b6df2015-10-19 13:52:53 +02001320#if defined(MBEDTLS_SSL_EXPORT_KEYS)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001321 if (ssl->conf->f_export_keys != NULL) {
1322 ssl->conf->f_export_keys(ssl->conf->p_export_keys,
1323 master, keyblk,
1324 mac_key_len, keylen,
1325 iv_copy_len);
Robert Cragie4feb7ae2015-10-02 13:33:37 +01001326 }
Ron Eldorf5cc10d2019-05-07 18:33:40 +03001327
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001328 if (ssl->conf->f_export_keys_ext != NULL) {
1329 ssl->conf->f_export_keys_ext(ssl->conf->p_export_keys,
1330 master, keyblk,
1331 mac_key_len, keylen,
1332 iv_copy_len,
1333 randbytes + 32,
1334 randbytes,
1335 tls_prf_get_type(tls_prf));
Ron Eldorf5cc10d2019-05-07 18:33:40 +03001336 }
Robert Cragie4feb7ae2015-10-02 13:33:37 +01001337#endif
1338
David Horstmannd4f22082022-10-26 18:25:14 +01001339 do_mbedtls_cipher_setup = 1;
Hanno Beckerf704bef2018-11-16 15:21:18 +00001340#if defined(MBEDTLS_USE_PSA_CRYPTO)
Hanno Beckercb1cc802018-11-17 22:27:38 +00001341
1342 /* Only use PSA-based ciphers for TLS-1.2.
1343 * That's relevant at least for TLS-1.0, where
1344 * we assume that mbedtls_cipher_crypt() updates
1345 * the structure field for the IV, which the PSA-based
1346 * implementation currently doesn't. */
1347#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001348 if (minor_ver == MBEDTLS_SSL_MINOR_VERSION_3) {
1349 ret = mbedtls_cipher_setup_psa(&transform->cipher_ctx_enc,
1350 cipher_info, transform->taglen);
1351 if (ret != 0 && ret != MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE) {
1352 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_cipher_setup_psa", ret);
Ron Eldore6992702019-05-07 18:27:13 +03001353 goto end;
Hanno Beckercb1cc802018-11-17 22:27:38 +00001354 }
1355
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001356 if (ret == 0) {
1357 MBEDTLS_SSL_DEBUG_MSG(3, ("Successfully setup PSA-based encryption cipher context"));
Hanno Beckercb1cc802018-11-17 22:27:38 +00001358 psa_fallthrough = 0;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001359 } else {
1360 MBEDTLS_SSL_DEBUG_MSG(1,
1361 (
1362 "Failed to setup PSA-based cipher context for record encryption - fall through to default setup."));
Hanno Beckercb1cc802018-11-17 22:27:38 +00001363 psa_fallthrough = 1;
1364 }
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001365 } else {
Hanno Beckercb1cc802018-11-17 22:27:38 +00001366 psa_fallthrough = 1;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001367 }
Hanno Beckercb1cc802018-11-17 22:27:38 +00001368#else
1369 psa_fallthrough = 1;
1370#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Hanno Beckerf704bef2018-11-16 15:21:18 +00001371
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001372 if (psa_fallthrough == 0) {
David Horstmannd4f22082022-10-26 18:25:14 +01001373 do_mbedtls_cipher_setup = 0;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001374 }
Hanno Beckerf704bef2018-11-16 15:21:18 +00001375#endif /* MBEDTLS_USE_PSA_CRYPTO */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001376 if (do_mbedtls_cipher_setup &&
1377 (ret = mbedtls_cipher_setup(&transform->cipher_ctx_enc,
1378 cipher_info)) != 0) {
1379 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_cipher_setup", ret);
Ron Eldore6992702019-05-07 18:27:13 +03001380 goto end;
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001381 }
Paul Bakkerda02a7f2013-08-31 17:25:14 +02001382
David Horstmannd4f22082022-10-26 18:25:14 +01001383 do_mbedtls_cipher_setup = 1;
Hanno Beckerf704bef2018-11-16 15:21:18 +00001384#if defined(MBEDTLS_USE_PSA_CRYPTO)
Hanno Beckercb1cc802018-11-17 22:27:38 +00001385 /* Only use PSA-based ciphers for TLS-1.2.
1386 * That's relevant at least for TLS-1.0, where
1387 * we assume that mbedtls_cipher_crypt() updates
1388 * the structure field for the IV, which the PSA-based
1389 * implementation currently doesn't. */
1390#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001391 if (minor_ver == MBEDTLS_SSL_MINOR_VERSION_3) {
1392 ret = mbedtls_cipher_setup_psa(&transform->cipher_ctx_dec,
1393 cipher_info, transform->taglen);
1394 if (ret != 0 && ret != MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE) {
1395 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_cipher_setup_psa", ret);
Ron Eldore6992702019-05-07 18:27:13 +03001396 goto end;
Hanno Beckercb1cc802018-11-17 22:27:38 +00001397 }
1398
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001399 if (ret == 0) {
1400 MBEDTLS_SSL_DEBUG_MSG(3, ("Successfully setup PSA-based decryption cipher context"));
Hanno Beckercb1cc802018-11-17 22:27:38 +00001401 psa_fallthrough = 0;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001402 } else {
1403 MBEDTLS_SSL_DEBUG_MSG(1,
1404 (
1405 "Failed to setup PSA-based cipher context for record decryption - fall through to default setup."));
Hanno Beckercb1cc802018-11-17 22:27:38 +00001406 psa_fallthrough = 1;
1407 }
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001408 } else {
Hanno Beckercb1cc802018-11-17 22:27:38 +00001409 psa_fallthrough = 1;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001410 }
Hanno Beckercb1cc802018-11-17 22:27:38 +00001411#else
1412 psa_fallthrough = 1;
1413#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Hanno Beckerf704bef2018-11-16 15:21:18 +00001414
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001415 if (psa_fallthrough == 0) {
David Horstmannd4f22082022-10-26 18:25:14 +01001416 do_mbedtls_cipher_setup = 0;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001417 }
Hanno Beckerf704bef2018-11-16 15:21:18 +00001418#endif /* MBEDTLS_USE_PSA_CRYPTO */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001419 if (do_mbedtls_cipher_setup &&
1420 (ret = mbedtls_cipher_setup(&transform->cipher_ctx_dec,
1421 cipher_info)) != 0) {
1422 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_cipher_setup", ret);
Ron Eldore6992702019-05-07 18:27:13 +03001423 goto end;
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001424 }
Paul Bakkerda02a7f2013-08-31 17:25:14 +02001425
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001426 if ((ret = mbedtls_cipher_setkey(&transform->cipher_ctx_enc, key1,
1427 cipher_info->key_bitlen,
1428 MBEDTLS_ENCRYPT)) != 0) {
1429 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_cipher_setkey", ret);
Ron Eldore6992702019-05-07 18:27:13 +03001430 goto end;
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001431 }
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02001432
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001433 if ((ret = mbedtls_cipher_setkey(&transform->cipher_ctx_dec, key2,
1434 cipher_info->key_bitlen,
1435 MBEDTLS_DECRYPT)) != 0) {
1436 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_cipher_setkey", ret);
Ron Eldore6992702019-05-07 18:27:13 +03001437 goto end;
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001438 }
Paul Bakkerda02a7f2013-08-31 17:25:14 +02001439
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001440#if defined(MBEDTLS_CIPHER_MODE_CBC)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001441 if (cipher_info->mode == MBEDTLS_MODE_CBC) {
1442 if ((ret = mbedtls_cipher_set_padding_mode(&transform->cipher_ctx_enc,
1443 MBEDTLS_PADDING_NONE)) != 0) {
1444 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_cipher_set_padding_mode", ret);
Ron Eldore6992702019-05-07 18:27:13 +03001445 goto end;
Manuel Pégourié-Gonnard126a66f2013-10-25 18:33:32 +02001446 }
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001447
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001448 if ((ret = mbedtls_cipher_set_padding_mode(&transform->cipher_ctx_dec,
1449 MBEDTLS_PADDING_NONE)) != 0) {
1450 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_cipher_set_padding_mode", ret);
Ron Eldore6992702019-05-07 18:27:13 +03001451 goto end;
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001452 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001453 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001454#endif /* MBEDTLS_CIPHER_MODE_CBC */
Paul Bakker5121ce52009-01-03 21:22:43 +00001455
Paul Bakker5121ce52009-01-03 21:22:43 +00001456
Manuel Pégourié-Gonnardd73b47f2019-05-06 12:44:24 +02001457 /* Initialize Zlib contexts */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001458#if defined(MBEDTLS_ZLIB_SUPPORT)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001459 if (compression == MBEDTLS_SSL_COMPRESS_DEFLATE) {
1460 MBEDTLS_SSL_DEBUG_MSG(3, ("Initializing zlib states"));
Paul Bakker2770fbd2012-07-03 13:30:23 +00001461
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001462 memset(&transform->ctx_deflate, 0, sizeof(transform->ctx_deflate));
1463 memset(&transform->ctx_inflate, 0, sizeof(transform->ctx_inflate));
Paul Bakker2770fbd2012-07-03 13:30:23 +00001464
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001465 if (deflateInit(&transform->ctx_deflate,
1466 Z_DEFAULT_COMPRESSION) != Z_OK ||
1467 inflateInit(&transform->ctx_inflate) != Z_OK) {
1468 MBEDTLS_SSL_DEBUG_MSG(1, ("Failed to initialize compression"));
Ron Eldore6992702019-05-07 18:27:13 +03001469 ret = MBEDTLS_ERR_SSL_COMPRESSION_FAILED;
1470 goto end;
Paul Bakker2770fbd2012-07-03 13:30:23 +00001471 }
1472 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001473#endif /* MBEDTLS_ZLIB_SUPPORT */
Paul Bakker2770fbd2012-07-03 13:30:23 +00001474
Ron Eldore6992702019-05-07 18:27:13 +03001475end:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001476 mbedtls_platform_zeroize(keyblk, sizeof(keyblk));
1477 return ret;
Paul Bakker5121ce52009-01-03 21:22:43 +00001478}
1479
Manuel Pégourié-Gonnard8d2805c2019-04-30 12:08:59 +02001480/*
Manuel Pégourié-Gonnard47e33e12019-05-20 10:10:17 +02001481 * Set appropriate PRF function and other SSL / TLS 1.0/1.1 / TLS1.2 functions
Manuel Pégourié-Gonnard8d2805c2019-04-30 12:08:59 +02001482 *
1483 * Inputs:
1484 * - SSL/TLS minor version
1485 * - hash associated with the ciphersuite (only used by TLS 1.2)
1486 *
Manuel Pégourié-Gonnard31d3ef12019-05-10 10:25:00 +02001487 * Outputs:
Manuel Pégourié-Gonnard8d2805c2019-04-30 12:08:59 +02001488 * - the tls_prf, calc_verify and calc_finished members of handshake structure
1489 */
Manuel Pégourié-Gonnardd904d662022-06-17 10:24:00 +02001490MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001491static int ssl_set_handshake_prfs(mbedtls_ssl_handshake_params *handshake,
1492 int minor_ver,
1493 mbedtls_md_type_t hash)
Manuel Pégourié-Gonnard1b00c4f2019-04-30 11:54:22 +02001494{
Gilles Peskinefc9c07f2021-05-12 22:51:53 +02001495#if !defined(MBEDTLS_SSL_PROTO_TLS1_2) || \
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001496 !(defined(MBEDTLS_SHA512_C) && !defined(MBEDTLS_SHA512_NO_SHA384))
Manuel Pégourié-Gonnard8d2805c2019-04-30 12:08:59 +02001497 (void) hash;
1498#endif
Manuel Pégourié-Gonnard1b00c4f2019-04-30 11:54:22 +02001499
Manuel Pégourié-Gonnard1b00c4f2019-04-30 11:54:22 +02001500#if defined(MBEDTLS_SSL_PROTO_SSL3)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001501 if (minor_ver == MBEDTLS_SSL_MINOR_VERSION_0) {
Manuel Pégourié-Gonnard1b00c4f2019-04-30 11:54:22 +02001502 handshake->tls_prf = ssl3_prf;
1503 handshake->calc_verify = ssl_calc_verify_ssl;
1504 handshake->calc_finished = ssl_calc_finished_ssl;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001505 } else
Manuel Pégourié-Gonnard1b00c4f2019-04-30 11:54:22 +02001506#endif
1507#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001508 if (minor_ver < MBEDTLS_SSL_MINOR_VERSION_3) {
Manuel Pégourié-Gonnard1b00c4f2019-04-30 11:54:22 +02001509 handshake->tls_prf = tls1_prf;
1510 handshake->calc_verify = ssl_calc_verify_tls;
1511 handshake->calc_finished = ssl_calc_finished_tls;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001512 } else
Manuel Pégourié-Gonnard1b00c4f2019-04-30 11:54:22 +02001513#endif
1514#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
Gilles Peskined2d59372021-05-12 22:43:27 +02001515#if defined(MBEDTLS_SHA512_C) && !defined(MBEDTLS_SHA512_NO_SHA384)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001516 if (minor_ver == MBEDTLS_SSL_MINOR_VERSION_3 &&
1517 hash == MBEDTLS_MD_SHA384) {
Manuel Pégourié-Gonnard1b00c4f2019-04-30 11:54:22 +02001518 handshake->tls_prf = tls_prf_sha384;
1519 handshake->calc_verify = ssl_calc_verify_tls_sha384;
1520 handshake->calc_finished = ssl_calc_finished_tls_sha384;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001521 } else
Manuel Pégourié-Gonnard1b00c4f2019-04-30 11:54:22 +02001522#endif
1523#if defined(MBEDTLS_SHA256_C)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001524 if (minor_ver == MBEDTLS_SSL_MINOR_VERSION_3) {
Manuel Pégourié-Gonnard1b00c4f2019-04-30 11:54:22 +02001525 handshake->tls_prf = tls_prf_sha256;
1526 handshake->calc_verify = ssl_calc_verify_tls_sha256;
1527 handshake->calc_finished = ssl_calc_finished_tls_sha256;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001528 } else
Manuel Pégourié-Gonnard1b00c4f2019-04-30 11:54:22 +02001529#endif
1530#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
1531 {
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001532 return MBEDTLS_ERR_SSL_INTERNAL_ERROR;
Manuel Pégourié-Gonnard1b00c4f2019-04-30 11:54:22 +02001533 }
1534
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001535 return 0;
Manuel Pégourié-Gonnard1b00c4f2019-04-30 11:54:22 +02001536}
1537
Manuel Pégourié-Gonnard9951b712019-04-30 12:08:59 +02001538/*
Manuel Pégourié-Gonnard85680c42019-05-03 09:16:16 +02001539 * Compute master secret if needed
Manuel Pégourié-Gonnardde047ad2019-05-03 09:46:14 +02001540 *
1541 * Parameters:
1542 * [in/out] handshake
1543 * [in] resume, premaster, extended_ms, calc_verify, tls_prf
Manuel Pégourié-Gonnardde718b92019-05-03 11:43:28 +02001544 * (PSA-PSK) ciphersuite_info, psk_opaque
Manuel Pégourié-Gonnardde047ad2019-05-03 09:46:14 +02001545 * [out] premaster (cleared)
Manuel Pégourié-Gonnardde047ad2019-05-03 09:46:14 +02001546 * [out] master
1547 * [in] ssl: optionally used for debugging, EMS and PSA-PSK
1548 * debug: conf->f_dbg, conf->p_dbg
1549 * EMS: passed to calc_verify (debug + (SSL3) session_negotiate)
Manuel Pégourié-Gonnardde718b92019-05-03 11:43:28 +02001550 * PSA-PSA: minor_ver, conf
Manuel Pégourié-Gonnard9951b712019-04-30 12:08:59 +02001551 */
Manuel Pégourié-Gonnardd904d662022-06-17 10:24:00 +02001552MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001553static int ssl_compute_master(mbedtls_ssl_handshake_params *handshake,
1554 unsigned char *master,
1555 const mbedtls_ssl_context *ssl)
Manuel Pégourié-Gonnard9951b712019-04-30 12:08:59 +02001556{
Janos Follath865b3eb2019-12-16 11:46:15 +00001557 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnard9951b712019-04-30 12:08:59 +02001558
1559 /* cf. RFC 5246, Section 8.1:
1560 * "The master secret is always exactly 48 bytes in length." */
1561 size_t const master_secret_len = 48;
1562
1563#if defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET)
1564 unsigned char session_hash[48];
1565#endif /* MBEDTLS_SSL_EXTENDED_MASTER_SECRET */
1566
Manuel Pégourié-Gonnard85680c42019-05-03 09:16:16 +02001567 /* The label for the KDF used for key expansion.
1568 * This is either "master secret" or "extended master secret"
1569 * depending on whether the Extended Master Secret extension
1570 * is used. */
1571 char const *lbl = "master secret";
1572
1573 /* The salt for the KDF used for key expansion.
1574 * - If the Extended Master Secret extension is not used,
1575 * this is ClientHello.Random + ServerHello.Random
1576 * (see Sect. 8.1 in RFC 5246).
1577 * - If the Extended Master Secret extension is used,
1578 * this is the transcript of the handshake so far.
1579 * (see Sect. 4 in RFC 7627). */
1580 unsigned char const *salt = handshake->randbytes;
1581 size_t salt_len = 64;
1582
Manuel Pégourié-Gonnardde047ad2019-05-03 09:46:14 +02001583#if !defined(MBEDTLS_DEBUG_C) && \
1584 !defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET) && \
1585 !(defined(MBEDTLS_USE_PSA_CRYPTO) && \
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001586 defined(MBEDTLS_KEY_EXCHANGE_PSK_ENABLED))
Manuel Pégourié-Gonnarda7505d12019-05-07 10:17:56 +02001587 ssl = NULL; /* make sure we don't use it except for those cases */
Manuel Pégourié-Gonnardde047ad2019-05-03 09:46:14 +02001588 (void) ssl;
1589#endif
Manuel Pégourié-Gonnardde047ad2019-05-03 09:46:14 +02001590
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001591 if (handshake->resume != 0) {
1592 MBEDTLS_SSL_DEBUG_MSG(3, ("no premaster (session resumed)"));
1593 return 0;
Manuel Pégourié-Gonnard9951b712019-04-30 12:08:59 +02001594 }
Manuel Pégourié-Gonnard9951b712019-04-30 12:08:59 +02001595
1596#if defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001597 if (handshake->extended_ms == MBEDTLS_SSL_EXTENDED_MS_ENABLED) {
Manuel Pégourié-Gonnard85680c42019-05-03 09:16:16 +02001598 lbl = "extended master secret";
1599 salt = session_hash;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001600 handshake->calc_verify(ssl, session_hash, &salt_len);
Manuel Pégourié-Gonnard85680c42019-05-03 09:16:16 +02001601
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001602 MBEDTLS_SSL_DEBUG_BUF(3, "session hash for extended master secret",
1603 session_hash, salt_len);
Manuel Pégourié-Gonnard85680c42019-05-03 09:16:16 +02001604 }
Manuel Pégourié-Gonnard9951b712019-04-30 12:08:59 +02001605#endif /* MBEDTLS_SSL_EXTENDED_MS_ENABLED */
1606
1607#if defined(MBEDTLS_USE_PSA_CRYPTO) && \
Manuel Pégourié-Gonnardde047ad2019-05-03 09:46:14 +02001608 defined(MBEDTLS_KEY_EXCHANGE_PSK_ENABLED)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001609 if (handshake->ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_PSK &&
Manuel Pégourié-Gonnardde718b92019-05-03 11:43:28 +02001610 ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_3 &&
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001611 ssl_use_opaque_psk(ssl) == 1) {
Manuel Pégourié-Gonnard85680c42019-05-03 09:16:16 +02001612 /* Perform PSK-to-MS expansion in a single step. */
1613 psa_status_t status;
1614 psa_algorithm_t alg;
Ronald Croncf56a0a2020-08-04 09:51:30 +02001615 psa_key_id_t psk;
Manuel Pégourié-Gonnard85680c42019-05-03 09:16:16 +02001616 psa_key_derivation_operation_t derivation =
1617 PSA_KEY_DERIVATION_OPERATION_INIT;
Manuel Pégourié-Gonnardde718b92019-05-03 11:43:28 +02001618 mbedtls_md_type_t hash_alg = handshake->ciphersuite_info->mac;
Manuel Pégourié-Gonnard9951b712019-04-30 12:08:59 +02001619
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001620 MBEDTLS_SSL_DEBUG_MSG(2, ("perform PSA-based PSK-to-MS expansion"));
Manuel Pégourié-Gonnard9951b712019-04-30 12:08:59 +02001621
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001622 psk = mbedtls_ssl_get_opaque_psk(ssl);
Manuel Pégourié-Gonnard9951b712019-04-30 12:08:59 +02001623
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001624 if (hash_alg == MBEDTLS_MD_SHA384) {
Manuel Pégourié-Gonnard85680c42019-05-03 09:16:16 +02001625 alg = PSA_ALG_TLS12_PSK_TO_MS(PSA_ALG_SHA_384);
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001626 } else {
Manuel Pégourié-Gonnard85680c42019-05-03 09:16:16 +02001627 alg = PSA_ALG_TLS12_PSK_TO_MS(PSA_ALG_SHA_256);
Manuel Pégourié-Gonnard9951b712019-04-30 12:08:59 +02001628 }
Manuel Pégourié-Gonnard85680c42019-05-03 09:16:16 +02001629
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001630 status = setup_psa_key_derivation(&derivation, psk, alg,
1631 salt, salt_len,
1632 (unsigned char const *) lbl,
1633 (size_t) strlen(lbl),
1634 master_secret_len);
1635 if (status != PSA_SUCCESS) {
1636 psa_key_derivation_abort(&derivation);
1637 return MBEDTLS_ERR_SSL_HW_ACCEL_FAILED;
Manuel Pégourié-Gonnard85680c42019-05-03 09:16:16 +02001638 }
1639
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001640 status = psa_key_derivation_output_bytes(&derivation,
1641 master,
1642 master_secret_len);
1643 if (status != PSA_SUCCESS) {
1644 psa_key_derivation_abort(&derivation);
1645 return MBEDTLS_ERR_SSL_HW_ACCEL_FAILED;
1646 }
1647
1648 status = psa_key_derivation_abort(&derivation);
1649 if (status != PSA_SUCCESS) {
1650 return MBEDTLS_ERR_SSL_HW_ACCEL_FAILED;
1651 }
1652 } else
Manuel Pégourié-Gonnard85680c42019-05-03 09:16:16 +02001653#endif
1654 {
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001655 ret = handshake->tls_prf(handshake->premaster, handshake->pmslen,
1656 lbl, salt, salt_len,
1657 master,
1658 master_secret_len);
1659 if (ret != 0) {
1660 MBEDTLS_SSL_DEBUG_RET(1, "prf", ret);
1661 return ret;
Manuel Pégourié-Gonnard85680c42019-05-03 09:16:16 +02001662 }
1663
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001664 MBEDTLS_SSL_DEBUG_BUF(3, "premaster secret",
1665 handshake->premaster,
1666 handshake->pmslen);
Manuel Pégourié-Gonnard85680c42019-05-03 09:16:16 +02001667
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001668 mbedtls_platform_zeroize(handshake->premaster,
1669 sizeof(handshake->premaster));
Manuel Pégourié-Gonnard9951b712019-04-30 12:08:59 +02001670 }
1671
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001672 return 0;
Manuel Pégourié-Gonnard9951b712019-04-30 12:08:59 +02001673}
Manuel Pégourié-Gonnard1b00c4f2019-04-30 11:54:22 +02001674
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001675int mbedtls_ssl_derive_keys(mbedtls_ssl_context *ssl)
Manuel Pégourié-Gonnarde59ae232019-04-30 11:41:40 +02001676{
Janos Follath865b3eb2019-12-16 11:46:15 +00001677 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnard8d2805c2019-04-30 12:08:59 +02001678 const mbedtls_ssl_ciphersuite_t * const ciphersuite_info =
1679 ssl->handshake->ciphersuite_info;
Manuel Pégourié-Gonnard1b00c4f2019-04-30 11:54:22 +02001680
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001681 MBEDTLS_SSL_DEBUG_MSG(2, ("=> derive keys"));
Manuel Pégourié-Gonnard040a9512019-05-06 12:05:58 +02001682
1683 /* Set PRF, calc_verify and calc_finished function pointers */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001684 ret = ssl_set_handshake_prfs(ssl->handshake,
1685 ssl->minor_ver,
1686 ciphersuite_info->mac);
1687 if (ret != 0) {
1688 MBEDTLS_SSL_DEBUG_RET(1, "ssl_set_handshake_prfs", ret);
1689 return ret;
Manuel Pégourié-Gonnard8d2805c2019-04-30 12:08:59 +02001690 }
Manuel Pégourié-Gonnard1b00c4f2019-04-30 11:54:22 +02001691
Manuel Pégourié-Gonnard040a9512019-05-06 12:05:58 +02001692 /* Compute master secret if needed */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001693 ret = ssl_compute_master(ssl->handshake,
1694 ssl->session_negotiate->master,
1695 ssl);
1696 if (ret != 0) {
1697 MBEDTLS_SSL_DEBUG_RET(1, "ssl_compute_master", ret);
1698 return ret;
Manuel Pégourié-Gonnard9951b712019-04-30 12:08:59 +02001699 }
1700
Manuel Pégourié-Gonnard040a9512019-05-06 12:05:58 +02001701 /* Swap the client and server random values:
1702 * - MS derivation wanted client+server (RFC 5246 8.1)
1703 * - key derivation wants server+client (RFC 5246 6.3) */
1704 {
1705 unsigned char tmp[64];
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001706 memcpy(tmp, ssl->handshake->randbytes, 64);
1707 memcpy(ssl->handshake->randbytes, tmp + 32, 32);
1708 memcpy(ssl->handshake->randbytes + 32, tmp, 32);
1709 mbedtls_platform_zeroize(tmp, sizeof(tmp));
Manuel Pégourié-Gonnard040a9512019-05-06 12:05:58 +02001710 }
1711
1712 /* Populate transform structure */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001713 ret = ssl_populate_transform(ssl->transform_negotiate,
1714 ssl->session_negotiate->ciphersuite,
1715 ssl->session_negotiate->master,
Jarno Lamsac84bd242019-08-16 12:06:56 +03001716#if defined(MBEDTLS_SSL_SOME_MODES_USE_MAC)
Manuel Pégourié-Gonnard6fa57bf2019-05-10 10:50:04 +02001717#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001718 ssl->session_negotiate->encrypt_then_mac,
Jarno Lamsac84bd242019-08-16 12:06:56 +03001719#endif /* MBEDTLS_SSL_ENCRYPT_THEN_MAC */
Manuel Pégourié-Gonnard6fa57bf2019-05-10 10:50:04 +02001720#if defined(MBEDTLS_SSL_TRUNCATED_HMAC)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001721 ssl->session_negotiate->trunc_hmac,
Jarno Lamsac84bd242019-08-16 12:06:56 +03001722#endif /* MBEDTLS_SSL_TRUNCATED_HMAC */
1723#endif /* MBEDTLS_SSL_SOME_MODES_USE_MAC */
Manuel Pégourié-Gonnard6fa57bf2019-05-10 10:50:04 +02001724#if defined(MBEDTLS_ZLIB_SUPPORT)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001725 ssl->session_negotiate->compression,
Manuel Pégourié-Gonnard6fa57bf2019-05-10 10:50:04 +02001726#endif
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001727 ssl->handshake->tls_prf,
1728 ssl->handshake->randbytes,
1729 ssl->minor_ver,
1730 ssl->conf->endpoint,
1731 ssl);
1732 if (ret != 0) {
1733 MBEDTLS_SSL_DEBUG_RET(1, "ssl_populate_transform", ret);
1734 return ret;
Manuel Pégourié-Gonnard040a9512019-05-06 12:05:58 +02001735 }
1736
1737 /* We no longer need Server/ClientHello.random values */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001738 mbedtls_platform_zeroize(ssl->handshake->randbytes,
1739 sizeof(ssl->handshake->randbytes));
Manuel Pégourié-Gonnard040a9512019-05-06 12:05:58 +02001740
Manuel Pégourié-Gonnardd73b47f2019-05-06 12:44:24 +02001741 /* Allocate compression buffer */
1742#if defined(MBEDTLS_ZLIB_SUPPORT)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001743 if (ssl->session_negotiate->compression == MBEDTLS_SSL_COMPRESS_DEFLATE &&
1744 ssl->compress_buf == NULL) {
1745 MBEDTLS_SSL_DEBUG_MSG(3, ("Allocating compression buffer"));
1746 ssl->compress_buf = mbedtls_calloc(1, MBEDTLS_SSL_COMPRESS_BUFFER_LEN);
1747 if (ssl->compress_buf == NULL) {
1748 MBEDTLS_SSL_DEBUG_MSG(1, ("alloc(%d bytes) failed",
1749 MBEDTLS_SSL_COMPRESS_BUFFER_LEN));
1750 return MBEDTLS_ERR_SSL_ALLOC_FAILED;
Manuel Pégourié-Gonnardd73b47f2019-05-06 12:44:24 +02001751 }
1752 }
1753#endif
1754
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001755 MBEDTLS_SSL_DEBUG_MSG(2, ("<= derive keys"));
Manuel Pégourié-Gonnard040a9512019-05-06 12:05:58 +02001756
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001757 return 0;
Manuel Pégourié-Gonnarde59ae232019-04-30 11:41:40 +02001758}
1759
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001760#if defined(MBEDTLS_SSL_PROTO_SSL3)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001761void ssl_calc_verify_ssl(const mbedtls_ssl_context *ssl,
1762 unsigned char *hash,
1763 size_t *hlen)
Paul Bakker5121ce52009-01-03 21:22:43 +00001764{
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02001765 mbedtls_md5_context md5;
1766 mbedtls_sha1_context sha1;
Paul Bakker5121ce52009-01-03 21:22:43 +00001767 unsigned char pad_1[48];
1768 unsigned char pad_2[48];
1769
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001770 MBEDTLS_SSL_DEBUG_MSG(2, ("=> calc verify ssl"));
Paul Bakker5121ce52009-01-03 21:22:43 +00001771
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001772 mbedtls_md5_init(&md5);
1773 mbedtls_sha1_init(&sha1);
Manuel Pégourié-Gonnard001f2b62015-07-06 16:21:13 +02001774
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001775 mbedtls_md5_clone(&md5, &ssl->handshake->fin_md5);
1776 mbedtls_sha1_clone(&sha1, &ssl->handshake->fin_sha1);
Paul Bakker5121ce52009-01-03 21:22:43 +00001777
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001778 memset(pad_1, 0x36, 48);
1779 memset(pad_2, 0x5C, 48);
Paul Bakker5121ce52009-01-03 21:22:43 +00001780
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001781 mbedtls_md5_update_ret(&md5, ssl->session_negotiate->master, 48);
1782 mbedtls_md5_update_ret(&md5, pad_1, 48);
1783 mbedtls_md5_finish_ret(&md5, hash);
Paul Bakker5121ce52009-01-03 21:22:43 +00001784
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001785 mbedtls_md5_starts_ret(&md5);
1786 mbedtls_md5_update_ret(&md5, ssl->session_negotiate->master, 48);
1787 mbedtls_md5_update_ret(&md5, pad_2, 48);
1788 mbedtls_md5_update_ret(&md5, hash, 16);
1789 mbedtls_md5_finish_ret(&md5, hash);
Paul Bakker5121ce52009-01-03 21:22:43 +00001790
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001791 mbedtls_sha1_update_ret(&sha1, ssl->session_negotiate->master, 48);
1792 mbedtls_sha1_update_ret(&sha1, pad_1, 40);
1793 mbedtls_sha1_finish_ret(&sha1, hash + 16);
Paul Bakker380da532012-04-18 16:10:25 +00001794
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001795 mbedtls_sha1_starts_ret(&sha1);
1796 mbedtls_sha1_update_ret(&sha1, ssl->session_negotiate->master, 48);
1797 mbedtls_sha1_update_ret(&sha1, pad_2, 40);
1798 mbedtls_sha1_update_ret(&sha1, hash + 16, 20);
1799 mbedtls_sha1_finish_ret(&sha1, hash + 16);
Paul Bakker380da532012-04-18 16:10:25 +00001800
Manuel Pégourié-Gonnardde718b92019-05-03 11:43:28 +02001801 *hlen = 36;
1802
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001803 MBEDTLS_SSL_DEBUG_BUF(3, "calculated verify result", hash, *hlen);
1804 MBEDTLS_SSL_DEBUG_MSG(2, ("<= calc verify"));
Paul Bakker380da532012-04-18 16:10:25 +00001805
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001806 mbedtls_md5_free(&md5);
1807 mbedtls_sha1_free(&sha1);
Paul Bakker5b4af392014-06-26 12:09:34 +02001808
Paul Bakker380da532012-04-18 16:10:25 +00001809 return;
1810}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001811#endif /* MBEDTLS_SSL_PROTO_SSL3 */
Paul Bakker380da532012-04-18 16:10:25 +00001812
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001813#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001814void ssl_calc_verify_tls(const mbedtls_ssl_context *ssl,
1815 unsigned char *hash,
1816 size_t *hlen)
Paul Bakker380da532012-04-18 16:10:25 +00001817{
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02001818 mbedtls_md5_context md5;
1819 mbedtls_sha1_context sha1;
Paul Bakker380da532012-04-18 16:10:25 +00001820
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001821 MBEDTLS_SSL_DEBUG_MSG(2, ("=> calc verify tls"));
Paul Bakker380da532012-04-18 16:10:25 +00001822
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001823 mbedtls_md5_init(&md5);
1824 mbedtls_sha1_init(&sha1);
Manuel Pégourié-Gonnard001f2b62015-07-06 16:21:13 +02001825
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001826 mbedtls_md5_clone(&md5, &ssl->handshake->fin_md5);
1827 mbedtls_sha1_clone(&sha1, &ssl->handshake->fin_sha1);
Paul Bakker380da532012-04-18 16:10:25 +00001828
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001829 mbedtls_md5_finish_ret(&md5, hash);
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_TLS1 || MBEDTLS_SSL_PROTO_TLS1_1 */
Paul Bakker380da532012-04-18 16:10:25 +00001843
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001844#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
1845#if defined(MBEDTLS_SHA256_C)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001846void ssl_calc_verify_tls_sha256(const mbedtls_ssl_context *ssl,
1847 unsigned char *hash,
1848 size_t *hlen)
Paul Bakker380da532012-04-18 16:10:25 +00001849{
Andrzej Kurekeb342242019-01-29 09:14:33 -05001850#if defined(MBEDTLS_USE_PSA_CRYPTO)
1851 size_t hash_size;
1852 psa_status_t status;
1853 psa_hash_operation_t sha256_psa = psa_hash_operation_init();
1854
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001855 MBEDTLS_SSL_DEBUG_MSG(2, ("=> PSA calc verify sha256"));
1856 status = psa_hash_clone(&ssl->handshake->fin_sha256_psa, &sha256_psa);
1857 if (status != PSA_SUCCESS) {
1858 MBEDTLS_SSL_DEBUG_MSG(2, ("PSA hash clone failed"));
Andrzej Kurekeb342242019-01-29 09:14:33 -05001859 return;
1860 }
1861
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001862 status = psa_hash_finish(&sha256_psa, hash, 32, &hash_size);
1863 if (status != PSA_SUCCESS) {
1864 MBEDTLS_SSL_DEBUG_MSG(2, ("PSA hash finish failed"));
Andrzej Kurekeb342242019-01-29 09:14:33 -05001865 return;
1866 }
Manuel Pégourié-Gonnardde718b92019-05-03 11:43:28 +02001867
1868 *hlen = 32;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001869 MBEDTLS_SSL_DEBUG_BUF(3, "PSA calculated verify result", hash, *hlen);
1870 MBEDTLS_SSL_DEBUG_MSG(2, ("<= PSA calc verify"));
Andrzej Kurekeb342242019-01-29 09:14:33 -05001871#else
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02001872 mbedtls_sha256_context sha256;
Paul Bakker380da532012-04-18 16:10:25 +00001873
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001874 mbedtls_sha256_init(&sha256);
Manuel Pégourié-Gonnard001f2b62015-07-06 16:21:13 +02001875
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001876 MBEDTLS_SSL_DEBUG_MSG(2, ("=> calc verify sha256"));
Paul Bakker380da532012-04-18 16:10:25 +00001877
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001878 mbedtls_sha256_clone(&sha256, &ssl->handshake->fin_sha256);
1879 mbedtls_sha256_finish_ret(&sha256, hash);
Paul Bakker380da532012-04-18 16:10:25 +00001880
Manuel Pégourié-Gonnardde718b92019-05-03 11:43:28 +02001881 *hlen = 32;
1882
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001883 MBEDTLS_SSL_DEBUG_BUF(3, "calculated verify result", hash, *hlen);
1884 MBEDTLS_SSL_DEBUG_MSG(2, ("<= calc verify"));
Paul Bakker380da532012-04-18 16:10:25 +00001885
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001886 mbedtls_sha256_free(&sha256);
Andrzej Kurekeb342242019-01-29 09:14:33 -05001887#endif /* MBEDTLS_USE_PSA_CRYPTO */
Paul Bakker380da532012-04-18 16:10:25 +00001888 return;
1889}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001890#endif /* MBEDTLS_SHA256_C */
Paul Bakker380da532012-04-18 16:10:25 +00001891
Gilles Peskined2d59372021-05-12 22:43:27 +02001892#if defined(MBEDTLS_SHA512_C) && !defined(MBEDTLS_SHA512_NO_SHA384)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001893void ssl_calc_verify_tls_sha384(const mbedtls_ssl_context *ssl,
1894 unsigned char *hash,
1895 size_t *hlen)
Paul Bakker380da532012-04-18 16:10:25 +00001896{
Andrzej Kurekeb342242019-01-29 09:14:33 -05001897#if defined(MBEDTLS_USE_PSA_CRYPTO)
1898 size_t hash_size;
1899 psa_status_t status;
Andrzej Kurek972fba52019-01-30 03:29:12 -05001900 psa_hash_operation_t sha384_psa = psa_hash_operation_init();
Andrzej Kurekeb342242019-01-29 09:14:33 -05001901
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001902 MBEDTLS_SSL_DEBUG_MSG(2, ("=> PSA calc verify sha384"));
1903 status = psa_hash_clone(&ssl->handshake->fin_sha384_psa, &sha384_psa);
1904 if (status != PSA_SUCCESS) {
1905 MBEDTLS_SSL_DEBUG_MSG(2, ("PSA hash clone failed"));
Andrzej Kurekeb342242019-01-29 09:14:33 -05001906 return;
1907 }
1908
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001909 status = psa_hash_finish(&sha384_psa, hash, 48, &hash_size);
1910 if (status != PSA_SUCCESS) {
1911 MBEDTLS_SSL_DEBUG_MSG(2, ("PSA hash finish failed"));
Andrzej Kurekeb342242019-01-29 09:14:33 -05001912 return;
1913 }
Manuel Pégourié-Gonnardde718b92019-05-03 11:43:28 +02001914
1915 *hlen = 48;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001916 MBEDTLS_SSL_DEBUG_BUF(3, "PSA calculated verify result", hash, *hlen);
1917 MBEDTLS_SSL_DEBUG_MSG(2, ("<= PSA calc verify"));
Andrzej Kurekeb342242019-01-29 09:14:33 -05001918#else
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02001919 mbedtls_sha512_context sha512;
Paul Bakker380da532012-04-18 16:10:25 +00001920
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001921 mbedtls_sha512_init(&sha512);
Manuel Pégourié-Gonnard001f2b62015-07-06 16:21:13 +02001922
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001923 MBEDTLS_SSL_DEBUG_MSG(2, ("=> calc verify sha384"));
Paul Bakker380da532012-04-18 16:10:25 +00001924
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001925 mbedtls_sha512_clone(&sha512, &ssl->handshake->fin_sha512);
1926 mbedtls_sha512_finish_ret(&sha512, hash);
Paul Bakker5121ce52009-01-03 21:22:43 +00001927
Manuel Pégourié-Gonnardde718b92019-05-03 11:43:28 +02001928 *hlen = 48;
1929
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001930 MBEDTLS_SSL_DEBUG_BUF(3, "calculated verify result", hash, *hlen);
1931 MBEDTLS_SSL_DEBUG_MSG(2, ("<= calc verify"));
Paul Bakker5121ce52009-01-03 21:22:43 +00001932
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001933 mbedtls_sha512_free(&sha512);
Andrzej Kurekeb342242019-01-29 09:14:33 -05001934#endif /* MBEDTLS_USE_PSA_CRYPTO */
Paul Bakker5121ce52009-01-03 21:22:43 +00001935 return;
1936}
Gilles Peskined2d59372021-05-12 22:43:27 +02001937#endif /* MBEDTLS_SHA512_C && !MBEDTLS_SHA512_NO_SHA384 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001938#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakker5121ce52009-01-03 21:22:43 +00001939
Gilles Peskineeccd8882020-03-10 12:19:08 +01001940#if defined(MBEDTLS_KEY_EXCHANGE_SOME_PSK_ENABLED)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001941int 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 +02001942{
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001943 unsigned char *p = ssl->handshake->premaster;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001944 unsigned char *end = p + sizeof(ssl->handshake->premaster);
Guilhem Bryantb5f04e42020-04-01 11:23:58 +01001945 const unsigned char *psk = NULL;
Guilhem Bryant61b0fe62020-03-27 11:12:12 +00001946 size_t psk_len = 0;
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01001947
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001948 if (mbedtls_ssl_get_psk(ssl, &psk, &psk_len)
1949 == MBEDTLS_ERR_SSL_PRIVATE_KEY_REQUIRED) {
Guilhem Bryantc5285d82020-03-25 17:08:15 +00001950 /*
1951 * This should never happen because the existence of a PSK is always
1952 * checked before calling this function
1953 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001954 MBEDTLS_SSL_DEBUG_MSG(1, ("should never happen"));
1955 return MBEDTLS_ERR_SSL_INTERNAL_ERROR;
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01001956 }
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001957
1958 /*
1959 * PMS = struct {
1960 * opaque other_secret<0..2^16-1>;
1961 * opaque psk<0..2^16-1>;
1962 * };
1963 * with "other_secret" depending on the particular key exchange
1964 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001965#if defined(MBEDTLS_KEY_EXCHANGE_PSK_ENABLED)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001966 if (key_ex == MBEDTLS_KEY_EXCHANGE_PSK) {
1967 if (end - p < 2) {
1968 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
1969 }
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001970
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001971 MBEDTLS_PUT_UINT16_BE(psk_len, p, 0);
Joe Subbiania651e6f2021-08-23 11:35:25 +01001972 p += 2;
Manuel Pégourié-Gonnardbc5e5082015-10-21 12:35:29 +02001973
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001974 if (end < p || (size_t) (end - p) < psk_len) {
1975 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
1976 }
Manuel Pégourié-Gonnardbc5e5082015-10-21 12:35:29 +02001977
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001978 memset(p, 0, psk_len);
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01001979 p += psk_len;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001980 } else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001981#endif /* MBEDTLS_KEY_EXCHANGE_PSK_ENABLED */
1982#if defined(MBEDTLS_KEY_EXCHANGE_RSA_PSK_ENABLED)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001983 if (key_ex == MBEDTLS_KEY_EXCHANGE_RSA_PSK) {
Manuel Pégourié-Gonnard0fae60b2013-10-14 17:39:48 +02001984 /*
1985 * other_secret already set by the ClientKeyExchange message,
1986 * and is 48 bytes long
1987 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001988 if (end - p < 2) {
1989 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
1990 }
Philippe Antoine747fd532018-05-30 09:13:21 +02001991
Manuel Pégourié-Gonnard0fae60b2013-10-14 17:39:48 +02001992 *p++ = 0;
1993 *p++ = 48;
1994 p += 48;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001995 } else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001996#endif /* MBEDTLS_KEY_EXCHANGE_RSA_PSK_ENABLED */
1997#if defined(MBEDTLS_KEY_EXCHANGE_DHE_PSK_ENABLED)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001998 if (key_ex == MBEDTLS_KEY_EXCHANGE_DHE_PSK) {
Janos Follath865b3eb2019-12-16 11:46:15 +00001999 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnard33352052015-06-02 16:17:08 +01002000 size_t len;
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02002001
Manuel Pégourié-Gonnard8df68632014-06-23 17:56:08 +02002002 /* Write length only when we know the actual value */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002003 if ((ret = mbedtls_dhm_calc_secret(&ssl->handshake->dhm_ctx,
2004 p + 2, end - (p + 2), &len,
2005 ssl->conf->f_rng, ssl->conf->p_rng)) != 0) {
2006 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_dhm_calc_secret", ret);
2007 return ret;
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02002008 }
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002009 MBEDTLS_PUT_UINT16_BE(len, p, 0);
Joe Subbiania651e6f2021-08-23 11:35:25 +01002010 p += 2 + len;
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02002011
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002012 MBEDTLS_SSL_DEBUG_MPI(3, "DHM: K ", &ssl->handshake->dhm_ctx.K);
2013 } else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002014#endif /* MBEDTLS_KEY_EXCHANGE_DHE_PSK_ENABLED */
2015#if defined(MBEDTLS_KEY_EXCHANGE_ECDHE_PSK_ENABLED)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002016 if (key_ex == MBEDTLS_KEY_EXCHANGE_ECDHE_PSK) {
Janos Follath865b3eb2019-12-16 11:46:15 +00002017 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02002018 size_t zlen;
2019
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002020 if ((ret = mbedtls_ecdh_calc_secret(&ssl->handshake->ecdh_ctx, &zlen,
2021 p + 2, end - (p + 2),
2022 ssl->conf->f_rng, ssl->conf->p_rng)) != 0) {
2023 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ecdh_calc_secret", ret);
2024 return ret;
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02002025 }
2026
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002027 MBEDTLS_PUT_UINT16_BE(zlen, p, 0);
Joe Subbiania651e6f2021-08-23 11:35:25 +01002028 p += 2 + zlen;
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02002029
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002030 MBEDTLS_SSL_DEBUG_ECDH(3, &ssl->handshake->ecdh_ctx,
2031 MBEDTLS_DEBUG_ECDH_Z);
2032 } else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002033#endif /* MBEDTLS_KEY_EXCHANGE_ECDHE_PSK_ENABLED */
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02002034 {
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002035 MBEDTLS_SSL_DEBUG_MSG(1, ("should never happen"));
2036 return MBEDTLS_ERR_SSL_INTERNAL_ERROR;
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02002037 }
2038
2039 /* opaque psk<0..2^16-1>; */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002040 if (end - p < 2) {
2041 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
2042 }
Manuel Pégourié-Gonnardb2bf5a12014-03-25 16:28:12 +01002043
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002044 MBEDTLS_PUT_UINT16_BE(psk_len, p, 0);
Joe Subbiania651e6f2021-08-23 11:35:25 +01002045 p += 2;
Manuel Pégourié-Gonnardbc5e5082015-10-21 12:35:29 +02002046
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002047 if (end < p || (size_t) (end - p) < psk_len) {
2048 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
2049 }
Manuel Pégourié-Gonnardbc5e5082015-10-21 12:35:29 +02002050
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002051 memcpy(p, psk, psk_len);
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01002052 p += psk_len;
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02002053
2054 ssl->handshake->pmslen = p - ssl->handshake->premaster;
2055
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002056 return 0;
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02002057}
Gilles Peskineeccd8882020-03-10 12:19:08 +01002058#endif /* MBEDTLS_KEY_EXCHANGE_SOME_PSK_ENABLED */
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02002059
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002060#if defined(MBEDTLS_SSL_SRV_C) && defined(MBEDTLS_SSL_RENEGOTIATION)
Manuel Pégourié-Gonnardd904d662022-06-17 10:24:00 +02002061MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002062static int ssl_write_hello_request(mbedtls_ssl_context *ssl);
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +02002063
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002064#if defined(MBEDTLS_SSL_PROTO_DTLS)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002065int mbedtls_ssl_resend_hello_request(mbedtls_ssl_context *ssl)
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +02002066{
2067 /* If renegotiation is not enforced, retransmit until we would reach max
2068 * timeout if we were using the usual handshake doubling scheme */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002069 if (ssl->conf->renego_max_records < 0) {
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02002070 uint32_t ratio = ssl->conf->hs_timeout_max / ssl->conf->hs_timeout_min + 1;
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +02002071 unsigned char doublings = 1;
2072
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002073 while (ratio != 0) {
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +02002074 ++doublings;
2075 ratio >>= 1;
2076 }
2077
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002078 if (++ssl->renego_records_seen > doublings) {
2079 MBEDTLS_SSL_DEBUG_MSG(2, ("no longer retransmitting hello request"));
2080 return 0;
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +02002081 }
2082 }
2083
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002084 return ssl_write_hello_request(ssl);
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +02002085}
2086#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002087#endif /* MBEDTLS_SSL_SRV_C && MBEDTLS_SSL_RENEGOTIATION */
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02002088
Hanno Beckerb9d44792019-02-08 07:19:04 +00002089#if defined(MBEDTLS_X509_CRT_PARSE_C)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002090static void ssl_clear_peer_cert(mbedtls_ssl_session *session)
Hanno Beckerb9d44792019-02-08 07:19:04 +00002091{
2092#if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002093 if (session->peer_cert != NULL) {
2094 mbedtls_x509_crt_free(session->peer_cert);
2095 mbedtls_free(session->peer_cert);
Hanno Beckerb9d44792019-02-08 07:19:04 +00002096 session->peer_cert = NULL;
2097 }
2098#else /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002099 if (session->peer_cert_digest != NULL) {
Hanno Beckerb9d44792019-02-08 07:19:04 +00002100 /* Zeroization is not necessary. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002101 mbedtls_free(session->peer_cert_digest);
Hanno Beckerb9d44792019-02-08 07:19:04 +00002102 session->peer_cert_digest = NULL;
2103 session->peer_cert_digest_type = MBEDTLS_MD_NONE;
2104 session->peer_cert_digest_len = 0;
2105 }
2106#endif /* !MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
2107}
2108#endif /* MBEDTLS_X509_CRT_PARSE_C */
2109
Paul Bakker5121ce52009-01-03 21:22:43 +00002110/*
2111 * Handshake functions
2112 */
Gilles Peskineeccd8882020-03-10 12:19:08 +01002113#if !defined(MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED)
Gilles Peskinef9828522017-05-03 12:28:43 +02002114/* No certificate support -> dummy functions */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002115int mbedtls_ssl_write_certificate(mbedtls_ssl_context *ssl)
Paul Bakker5121ce52009-01-03 21:22:43 +00002116{
Hanno Beckere694c3e2017-12-27 21:34:08 +00002117 const mbedtls_ssl_ciphersuite_t *ciphersuite_info =
2118 ssl->handshake->ciphersuite_info;
Paul Bakker5121ce52009-01-03 21:22:43 +00002119
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002120 MBEDTLS_SSL_DEBUG_MSG(2, ("=> write certificate"));
Paul Bakker5121ce52009-01-03 21:22:43 +00002121
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002122 if (!mbedtls_ssl_ciphersuite_uses_srv_cert(ciphersuite_info)) {
2123 MBEDTLS_SSL_DEBUG_MSG(2, ("<= skip write certificate"));
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02002124 ssl->state++;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002125 return 0;
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02002126 }
2127
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002128 MBEDTLS_SSL_DEBUG_MSG(1, ("should never happen"));
2129 return MBEDTLS_ERR_SSL_INTERNAL_ERROR;
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002130}
2131
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002132int mbedtls_ssl_parse_certificate(mbedtls_ssl_context *ssl)
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002133{
Hanno Beckere694c3e2017-12-27 21:34:08 +00002134 const mbedtls_ssl_ciphersuite_t *ciphersuite_info =
2135 ssl->handshake->ciphersuite_info;
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002136
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002137 MBEDTLS_SSL_DEBUG_MSG(2, ("=> parse certificate"));
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002138
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002139 if (!mbedtls_ssl_ciphersuite_uses_srv_cert(ciphersuite_info)) {
2140 MBEDTLS_SSL_DEBUG_MSG(2, ("<= skip parse certificate"));
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002141 ssl->state++;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002142 return 0;
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002143 }
2144
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002145 MBEDTLS_SSL_DEBUG_MSG(1, ("should never happen"));
2146 return MBEDTLS_ERR_SSL_INTERNAL_ERROR;
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002147}
Gilles Peskinef9828522017-05-03 12:28:43 +02002148
Gilles Peskineeccd8882020-03-10 12:19:08 +01002149#else /* MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED */
Gilles Peskinef9828522017-05-03 12:28:43 +02002150/* Some certificate support -> implement write and parse */
2151
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002152int mbedtls_ssl_write_certificate(mbedtls_ssl_context *ssl)
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002153{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002154 int ret = MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002155 size_t i, n;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002156 const mbedtls_x509_crt *crt;
Hanno Beckere694c3e2017-12-27 21:34:08 +00002157 const mbedtls_ssl_ciphersuite_t *ciphersuite_info =
2158 ssl->handshake->ciphersuite_info;
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002159
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002160 MBEDTLS_SSL_DEBUG_MSG(2, ("=> write certificate"));
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002161
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002162 if (!mbedtls_ssl_ciphersuite_uses_srv_cert(ciphersuite_info)) {
2163 MBEDTLS_SSL_DEBUG_MSG(2, ("<= skip write certificate"));
Johan Pascal4f099262020-09-22 10:59:26 +02002164 ssl->state++;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002165 return 0;
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002166 }
2167
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002168#if defined(MBEDTLS_SSL_CLI_C)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002169 if (ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT) {
2170 if (ssl->client_auth == 0) {
2171 MBEDTLS_SSL_DEBUG_MSG(2, ("<= skip write certificate"));
Paul Bakker5121ce52009-01-03 21:22:43 +00002172 ssl->state++;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002173 return 0;
Paul Bakker5121ce52009-01-03 21:22:43 +00002174 }
2175
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002176#if defined(MBEDTLS_SSL_PROTO_SSL3)
Paul Bakker5121ce52009-01-03 21:22:43 +00002177 /*
2178 * If using SSLv3 and got no cert, send an Alert message
2179 * (otherwise an empty Certificate message will be sent).
2180 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002181 if (mbedtls_ssl_own_cert(ssl) == NULL &&
2182 ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0) {
Paul Bakker5121ce52009-01-03 21:22:43 +00002183 ssl->out_msglen = 2;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002184 ssl->out_msgtype = MBEDTLS_SSL_MSG_ALERT;
2185 ssl->out_msg[0] = MBEDTLS_SSL_ALERT_LEVEL_WARNING;
2186 ssl->out_msg[1] = MBEDTLS_SSL_ALERT_MSG_NO_CERT;
Paul Bakker5121ce52009-01-03 21:22:43 +00002187
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002188 MBEDTLS_SSL_DEBUG_MSG(2, ("got no certificate to send"));
Paul Bakker5121ce52009-01-03 21:22:43 +00002189 goto write_msg;
2190 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002191#endif /* MBEDTLS_SSL_PROTO_SSL3 */
Paul Bakker5121ce52009-01-03 21:22:43 +00002192 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002193#endif /* MBEDTLS_SSL_CLI_C */
2194#if defined(MBEDTLS_SSL_SRV_C)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002195 if (ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER) {
2196 if (mbedtls_ssl_own_cert(ssl) == NULL) {
2197 MBEDTLS_SSL_DEBUG_MSG(1, ("got no certificate to send"));
2198 return MBEDTLS_ERR_SSL_CERTIFICATE_REQUIRED;
Paul Bakker5121ce52009-01-03 21:22:43 +00002199 }
2200 }
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01002201#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00002202
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002203 MBEDTLS_SSL_DEBUG_CRT(3, "own certificate", mbedtls_ssl_own_cert(ssl));
Paul Bakker5121ce52009-01-03 21:22:43 +00002204
2205 /*
2206 * 0 . 0 handshake type
2207 * 1 . 3 handshake length
2208 * 4 . 6 length of all certs
2209 * 7 . 9 length of cert. 1
2210 * 10 . n-1 peer certificate
2211 * n . n+2 length of cert. 2
2212 * n+3 . ... upper level cert, etc.
2213 */
2214 i = 7;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002215 crt = mbedtls_ssl_own_cert(ssl);
Paul Bakker5121ce52009-01-03 21:22:43 +00002216
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002217 while (crt != NULL) {
Paul Bakker5121ce52009-01-03 21:22:43 +00002218 n = crt->raw.len;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002219 if (n > MBEDTLS_SSL_OUT_CONTENT_LEN - 3 - i) {
2220 MBEDTLS_SSL_DEBUG_MSG(1, ("certificate too large, %" MBEDTLS_PRINTF_SIZET
2221 " > %" MBEDTLS_PRINTF_SIZET,
2222 i + 3 + n, (size_t) MBEDTLS_SSL_OUT_CONTENT_LEN));
2223 return MBEDTLS_ERR_SSL_CERTIFICATE_TOO_LARGE;
Paul Bakker5121ce52009-01-03 21:22:43 +00002224 }
2225
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002226 ssl->out_msg[i] = MBEDTLS_BYTE_2(n);
2227 ssl->out_msg[i + 1] = MBEDTLS_BYTE_1(n);
2228 ssl->out_msg[i + 2] = MBEDTLS_BYTE_0(n);
Paul Bakker5121ce52009-01-03 21:22:43 +00002229
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002230 i += 3; memcpy(ssl->out_msg + i, crt->raw.p, n);
Paul Bakker5121ce52009-01-03 21:22:43 +00002231 i += n; crt = crt->next;
2232 }
2233
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002234 ssl->out_msg[4] = MBEDTLS_BYTE_2(i - 7);
2235 ssl->out_msg[5] = MBEDTLS_BYTE_1(i - 7);
2236 ssl->out_msg[6] = MBEDTLS_BYTE_0(i - 7);
Paul Bakker5121ce52009-01-03 21:22:43 +00002237
2238 ssl->out_msglen = i;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002239 ssl->out_msgtype = MBEDTLS_SSL_MSG_HANDSHAKE;
2240 ssl->out_msg[0] = MBEDTLS_SSL_HS_CERTIFICATE;
Paul Bakker5121ce52009-01-03 21:22:43 +00002241
Manuel Pégourié-Gonnardcf141ca2015-05-20 10:35:51 +02002242#if defined(MBEDTLS_SSL_PROTO_SSL3) && defined(MBEDTLS_SSL_CLI_C)
Paul Bakker5121ce52009-01-03 21:22:43 +00002243write_msg:
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002244#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00002245
2246 ssl->state++;
2247
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002248 if ((ret = mbedtls_ssl_write_handshake_msg(ssl)) != 0) {
2249 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ssl_write_handshake_msg", ret);
2250 return ret;
Paul Bakker5121ce52009-01-03 21:22:43 +00002251 }
2252
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002253 MBEDTLS_SSL_DEBUG_MSG(2, ("<= write certificate"));
Paul Bakker5121ce52009-01-03 21:22:43 +00002254
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002255 return ret;
Paul Bakker5121ce52009-01-03 21:22:43 +00002256}
2257
Hanno Becker84879e32019-01-31 07:44:03 +00002258#if defined(MBEDTLS_SSL_RENEGOTIATION) && defined(MBEDTLS_SSL_CLI_C)
Hanno Becker177475a2019-02-05 17:02:46 +00002259
2260#if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
Manuel Pégourié-Gonnardd904d662022-06-17 10:24:00 +02002261MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002262static int ssl_check_peer_crt_unchanged(mbedtls_ssl_context *ssl,
2263 unsigned char *crt_buf,
2264 size_t crt_buf_len)
Hanno Beckerdef9bdc2019-01-30 14:46:46 +00002265{
2266 mbedtls_x509_crt const * const peer_crt = ssl->session->peer_cert;
2267
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002268 if (peer_crt == NULL) {
2269 return -1;
2270 }
Hanno Beckerdef9bdc2019-01-30 14:46:46 +00002271
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002272 if (peer_crt->raw.len != crt_buf_len) {
2273 return -1;
2274 }
Hanno Beckerdef9bdc2019-01-30 14:46:46 +00002275
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002276 return memcmp(peer_crt->raw.p, crt_buf, peer_crt->raw.len);
Hanno Beckerdef9bdc2019-01-30 14:46:46 +00002277}
Hanno Becker177475a2019-02-05 17:02:46 +00002278#else /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
Manuel Pégourié-Gonnardd904d662022-06-17 10:24:00 +02002279MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002280static int ssl_check_peer_crt_unchanged(mbedtls_ssl_context *ssl,
2281 unsigned char *crt_buf,
2282 size_t crt_buf_len)
Hanno Becker177475a2019-02-05 17:02:46 +00002283{
Janos Follath865b3eb2019-12-16 11:46:15 +00002284 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Hanno Becker177475a2019-02-05 17:02:46 +00002285 unsigned char const * const peer_cert_digest =
2286 ssl->session->peer_cert_digest;
2287 mbedtls_md_type_t const peer_cert_digest_type =
2288 ssl->session->peer_cert_digest_type;
2289 mbedtls_md_info_t const * const digest_info =
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002290 mbedtls_md_info_from_type(peer_cert_digest_type);
Hanno Becker177475a2019-02-05 17:02:46 +00002291 unsigned char tmp_digest[MBEDTLS_SSL_PEER_CERT_DIGEST_MAX_LEN];
2292 size_t digest_len;
2293
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002294 if (peer_cert_digest == NULL || digest_info == NULL) {
2295 return -1;
2296 }
Hanno Becker177475a2019-02-05 17:02:46 +00002297
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002298 digest_len = mbedtls_md_get_size(digest_info);
2299 if (digest_len > MBEDTLS_SSL_PEER_CERT_DIGEST_MAX_LEN) {
2300 return -1;
2301 }
Hanno Becker177475a2019-02-05 17:02:46 +00002302
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002303 ret = mbedtls_md(digest_info, crt_buf, crt_buf_len, tmp_digest);
2304 if (ret != 0) {
2305 return -1;
2306 }
Hanno Becker177475a2019-02-05 17:02:46 +00002307
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002308 return memcmp(tmp_digest, peer_cert_digest, digest_len);
Hanno Becker177475a2019-02-05 17:02:46 +00002309}
2310#endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
Hanno Becker84879e32019-01-31 07:44:03 +00002311#endif /* MBEDTLS_SSL_RENEGOTIATION && MBEDTLS_SSL_CLI_C */
Hanno Beckerdef9bdc2019-01-30 14:46:46 +00002312
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02002313/*
2314 * Once the certificate message is read, parse it into a cert chain and
2315 * perform basic checks, but leave actual verification to the caller
2316 */
Manuel Pégourié-Gonnardd904d662022-06-17 10:24:00 +02002317MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002318static int ssl_parse_certificate_chain(mbedtls_ssl_context *ssl,
2319 mbedtls_x509_crt *chain)
Paul Bakker5121ce52009-01-03 21:22:43 +00002320{
Janos Follath865b3eb2019-12-16 11:46:15 +00002321 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Hanno Beckerc7bd7802019-02-05 15:37:23 +00002322#if defined(MBEDTLS_SSL_RENEGOTIATION) && defined(MBEDTLS_SSL_CLI_C)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002323 int crt_cnt = 0;
Hanno Beckerc7bd7802019-02-05 15:37:23 +00002324#endif
Paul Bakker23986e52011-04-24 08:57:21 +00002325 size_t i, n;
Gilles Peskine064a85c2017-05-10 10:46:40 +02002326 uint8_t alert;
Paul Bakker5121ce52009-01-03 21:22:43 +00002327
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002328 if (ssl->in_msgtype != MBEDTLS_SSL_MSG_HANDSHAKE) {
2329 MBEDTLS_SSL_DEBUG_MSG(1, ("bad certificate message"));
2330 mbedtls_ssl_send_alert_message(ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
2331 MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE);
2332 return MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE;
Paul Bakker5121ce52009-01-03 21:22:43 +00002333 }
2334
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002335 if (ssl->in_msg[0] != MBEDTLS_SSL_HS_CERTIFICATE ||
2336 ssl->in_hslen < mbedtls_ssl_hs_hdr_len(ssl) + 3 + 3) {
2337 MBEDTLS_SSL_DEBUG_MSG(1, ("bad certificate message"));
2338 mbedtls_ssl_send_alert_message(ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
2339 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR);
2340 return MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE;
Paul Bakker5121ce52009-01-03 21:22:43 +00002341 }
2342
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002343 i = mbedtls_ssl_hs_hdr_len(ssl);
Manuel Pégourié-Gonnardf49a7da2014-09-10 13:30:43 +00002344
Paul Bakker5121ce52009-01-03 21:22:43 +00002345 /*
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002346 * Same message structure as in mbedtls_ssl_write_certificate()
Paul Bakker5121ce52009-01-03 21:22:43 +00002347 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002348 n = (ssl->in_msg[i+1] << 8) | ssl->in_msg[i+2];
Paul Bakker5121ce52009-01-03 21:22:43 +00002349
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002350 if (ssl->in_msg[i] != 0 ||
2351 ssl->in_hslen != n + 3 + mbedtls_ssl_hs_hdr_len(ssl)) {
2352 MBEDTLS_SSL_DEBUG_MSG(1, ("bad certificate message"));
2353 mbedtls_ssl_send_alert_message(ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
2354 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR);
2355 return MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE;
Paul Bakker5121ce52009-01-03 21:22:43 +00002356 }
2357
Hanno Beckerdef9bdc2019-01-30 14:46:46 +00002358 /* Make &ssl->in_msg[i] point to the beginning of the CRT chain. */
2359 i += 3;
2360
Hanno Beckerdef9bdc2019-01-30 14:46:46 +00002361 /* Iterate through and parse the CRTs in the provided chain. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002362 while (i < ssl->in_hslen) {
Hanno Beckerdef9bdc2019-01-30 14:46:46 +00002363 /* Check that there's room for the next CRT's length fields. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002364 if (i + 3 > ssl->in_hslen) {
2365 MBEDTLS_SSL_DEBUG_MSG(1, ("bad certificate message"));
2366 mbedtls_ssl_send_alert_message(ssl,
2367 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
2368 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR);
2369 return MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE;
Philippe Antoine747fd532018-05-30 09:13:21 +02002370 }
Hanno Beckerdef9bdc2019-01-30 14:46:46 +00002371 /* In theory, the CRT can be up to 2**24 Bytes, but we don't support
2372 * anything beyond 2**16 ~ 64K. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002373 if (ssl->in_msg[i] != 0) {
2374 MBEDTLS_SSL_DEBUG_MSG(1, ("bad certificate message"));
2375 mbedtls_ssl_send_alert_message(ssl,
2376 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
2377 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR);
2378 return MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE;
Paul Bakker5121ce52009-01-03 21:22:43 +00002379 }
2380
Hanno Beckerdef9bdc2019-01-30 14:46:46 +00002381 /* Read length of the next CRT in the chain. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002382 n = ((unsigned int) ssl->in_msg[i + 1] << 8)
Paul Bakker5121ce52009-01-03 21:22:43 +00002383 | (unsigned int) ssl->in_msg[i + 2];
2384 i += 3;
2385
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002386 if (n < 128 || i + n > ssl->in_hslen) {
2387 MBEDTLS_SSL_DEBUG_MSG(1, ("bad certificate message"));
2388 mbedtls_ssl_send_alert_message(ssl,
2389 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
2390 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR);
2391 return MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE;
Paul Bakker5121ce52009-01-03 21:22:43 +00002392 }
2393
Hanno Beckerdef9bdc2019-01-30 14:46:46 +00002394 /* Check if we're handling the first CRT in the chain. */
Hanno Beckerc7bd7802019-02-05 15:37:23 +00002395#if defined(MBEDTLS_SSL_RENEGOTIATION) && defined(MBEDTLS_SSL_CLI_C)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002396 if (crt_cnt++ == 0 &&
Hanno Beckerc7bd7802019-02-05 15:37:23 +00002397 ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT &&
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002398 ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS) {
Hanno Becker46f34d02019-02-08 14:00:04 +00002399 /* During client-side renegotiation, check that the server's
2400 * end-CRTs hasn't changed compared to the initial handshake,
2401 * mitigating the triple handshake attack. On success, reuse
2402 * the original end-CRT instead of parsing it again. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002403 MBEDTLS_SSL_DEBUG_MSG(3, ("Check that peer CRT hasn't changed during renegotiation"));
2404 if (ssl_check_peer_crt_unchanged(ssl,
2405 &ssl->in_msg[i],
2406 n) != 0) {
2407 MBEDTLS_SSL_DEBUG_MSG(1, ("new server cert during renegotiation"));
2408 mbedtls_ssl_send_alert_message(ssl,
2409 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
2410 MBEDTLS_SSL_ALERT_MSG_ACCESS_DENIED);
2411 return MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE;
Hanno Beckerdef9bdc2019-01-30 14:46:46 +00002412 }
Hanno Beckerc7bd7802019-02-05 15:37:23 +00002413
2414 /* Now we can safely free the original chain. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002415 ssl_clear_peer_cert(ssl->session);
Hanno Beckerc7bd7802019-02-05 15:37:23 +00002416 }
Hanno Beckerdef9bdc2019-01-30 14:46:46 +00002417#endif /* MBEDTLS_SSL_RENEGOTIATION && MBEDTLS_SSL_CLI_C */
2418
Hanno Beckerdef9bdc2019-01-30 14:46:46 +00002419 /* Parse the next certificate in the chain. */
Hanno Becker0056eab2019-02-08 14:39:16 +00002420#if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002421 ret = mbedtls_x509_crt_parse_der(chain, ssl->in_msg + i, n);
Hanno Becker0056eab2019-02-08 14:39:16 +00002422#else
Hanno Becker353a6f02019-02-26 11:51:34 +00002423 /* If we don't need to store the CRT chain permanently, parse
Hanno Becker0056eab2019-02-08 14:39:16 +00002424 * it in-place from the input buffer instead of making a copy. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002425 ret = mbedtls_x509_crt_parse_der_nocopy(chain, ssl->in_msg + i, n);
Hanno Becker0056eab2019-02-08 14:39:16 +00002426#endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002427 switch (ret) {
Hanno Beckerdef9bdc2019-01-30 14:46:46 +00002428 case 0: /*ok*/
2429 case MBEDTLS_ERR_X509_UNKNOWN_SIG_ALG + MBEDTLS_ERR_OID_NOT_FOUND:
2430 /* Ignore certificate with an unknown algorithm: maybe a
2431 prior certificate was already trusted. */
2432 break;
Gilles Peskine1cc8e342017-05-03 16:28:34 +02002433
Hanno Beckerdef9bdc2019-01-30 14:46:46 +00002434 case MBEDTLS_ERR_X509_ALLOC_FAILED:
2435 alert = MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR;
2436 goto crt_parse_der_failed;
Gilles Peskine1cc8e342017-05-03 16:28:34 +02002437
Hanno Beckerdef9bdc2019-01-30 14:46:46 +00002438 case MBEDTLS_ERR_X509_UNKNOWN_VERSION:
2439 alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT;
2440 goto crt_parse_der_failed;
Gilles Peskine1cc8e342017-05-03 16:28:34 +02002441
Hanno Beckerdef9bdc2019-01-30 14:46:46 +00002442 default:
2443 alert = MBEDTLS_SSL_ALERT_MSG_BAD_CERT;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002444crt_parse_der_failed:
2445 mbedtls_ssl_send_alert_message(ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL, alert);
2446 MBEDTLS_SSL_DEBUG_RET(1, " mbedtls_x509_crt_parse_der", ret);
2447 return ret;
Paul Bakker5121ce52009-01-03 21:22:43 +00002448 }
2449
2450 i += n;
2451 }
2452
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002453 MBEDTLS_SSL_DEBUG_CRT(3, "peer certificate", chain);
2454 return 0;
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02002455}
2456
Hanno Becker4a55f632019-02-05 12:49:06 +00002457#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnardd904d662022-06-17 10:24:00 +02002458MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002459static int ssl_srv_check_client_no_crt_notification(mbedtls_ssl_context *ssl)
Hanno Becker4a55f632019-02-05 12:49:06 +00002460{
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002461 if (ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT) {
2462 return -1;
2463 }
Hanno Becker4a55f632019-02-05 12:49:06 +00002464
2465#if defined(MBEDTLS_SSL_PROTO_SSL3)
2466 /*
2467 * Check if the client sent an empty certificate
2468 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002469 if (ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0) {
2470 if (ssl->in_msglen == 2 &&
Hanno Becker4a55f632019-02-05 12:49:06 +00002471 ssl->in_msgtype == MBEDTLS_SSL_MSG_ALERT &&
2472 ssl->in_msg[0] == MBEDTLS_SSL_ALERT_LEVEL_WARNING &&
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002473 ssl->in_msg[1] == MBEDTLS_SSL_ALERT_MSG_NO_CERT) {
2474 MBEDTLS_SSL_DEBUG_MSG(1, ("SSLv3 client has no certificate"));
2475 return 0;
Hanno Becker4a55f632019-02-05 12:49:06 +00002476 }
2477
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002478 return -1;
Hanno Becker4a55f632019-02-05 12:49:06 +00002479 }
2480#endif /* MBEDTLS_SSL_PROTO_SSL3 */
2481
2482#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1) || \
2483 defined(MBEDTLS_SSL_PROTO_TLS1_2)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002484 if (ssl->in_hslen == 3 + mbedtls_ssl_hs_hdr_len(ssl) &&
Hanno Becker4a55f632019-02-05 12:49:06 +00002485 ssl->in_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE &&
2486 ssl->in_msg[0] == MBEDTLS_SSL_HS_CERTIFICATE &&
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002487 memcmp(ssl->in_msg + mbedtls_ssl_hs_hdr_len(ssl), "\0\0\0", 3) == 0) {
2488 MBEDTLS_SSL_DEBUG_MSG(1, ("TLSv1 client has no certificate"));
2489 return 0;
Hanno Becker4a55f632019-02-05 12:49:06 +00002490 }
2491
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002492 return -1;
Hanno Becker4a55f632019-02-05 12:49:06 +00002493#endif /* MBEDTLS_SSL_PROTO_TLS1 || MBEDTLS_SSL_PROTO_TLS1_1 || \
2494 MBEDTLS_SSL_PROTO_TLS1_2 */
2495}
2496#endif /* MBEDTLS_SSL_SRV_C */
2497
Hanno Becker28f2fcd2019-02-07 10:11:07 +00002498/* Check if a certificate message is expected.
2499 * Return either
2500 * - SSL_CERTIFICATE_EXPECTED, or
2501 * - SSL_CERTIFICATE_SKIP
2502 * indicating whether a Certificate message is expected or not.
2503 */
2504#define SSL_CERTIFICATE_EXPECTED 0
2505#define SSL_CERTIFICATE_SKIP 1
Manuel Pégourié-Gonnardd904d662022-06-17 10:24:00 +02002506MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002507static int ssl_parse_certificate_coordinate(mbedtls_ssl_context *ssl,
2508 int authmode)
Hanno Becker28f2fcd2019-02-07 10:11:07 +00002509{
2510 const mbedtls_ssl_ciphersuite_t *ciphersuite_info =
Hanno Beckere694c3e2017-12-27 21:34:08 +00002511 ssl->handshake->ciphersuite_info;
Hanno Becker28f2fcd2019-02-07 10:11:07 +00002512
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002513 if (!mbedtls_ssl_ciphersuite_uses_srv_cert(ciphersuite_info)) {
2514 return SSL_CERTIFICATE_SKIP;
2515 }
Hanno Becker28f2fcd2019-02-07 10:11:07 +00002516
2517#if defined(MBEDTLS_SSL_SRV_C)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002518 if (ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER) {
2519 if (ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_RSA_PSK) {
2520 return SSL_CERTIFICATE_SKIP;
2521 }
Hanno Becker28f2fcd2019-02-07 10:11:07 +00002522
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002523 if (authmode == MBEDTLS_SSL_VERIFY_NONE) {
Hanno Becker28f2fcd2019-02-07 10:11:07 +00002524 ssl->session_negotiate->verify_result =
2525 MBEDTLS_X509_BADCERT_SKIP_VERIFY;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002526 return SSL_CERTIFICATE_SKIP;
Hanno Becker28f2fcd2019-02-07 10:11:07 +00002527 }
2528 }
Hanno Becker84d9d272019-03-01 08:10:46 +00002529#else
2530 ((void) authmode);
Hanno Becker28f2fcd2019-02-07 10:11:07 +00002531#endif /* MBEDTLS_SSL_SRV_C */
2532
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002533 return SSL_CERTIFICATE_EXPECTED;
Hanno Becker28f2fcd2019-02-07 10:11:07 +00002534}
2535
Manuel Pégourié-Gonnardd904d662022-06-17 10:24:00 +02002536MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002537static int ssl_parse_certificate_verify(mbedtls_ssl_context *ssl,
2538 int authmode,
2539 mbedtls_x509_crt *chain,
2540 void *rs_ctx)
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02002541{
Hanno Becker6bdfab22019-02-05 13:11:17 +00002542 int ret = 0;
Hanno Becker28f2fcd2019-02-07 10:11:07 +00002543 const mbedtls_ssl_ciphersuite_t *ciphersuite_info =
Hanno Beckere694c3e2017-12-27 21:34:08 +00002544 ssl->handshake->ciphersuite_info;
Hanno Beckerafd0b0a2019-03-27 16:55:01 +00002545 int have_ca_chain = 0;
Hanno Becker68636192019-02-05 14:36:34 +00002546
Hanno Becker8927c832019-04-03 12:52:50 +01002547 int (*f_vrfy)(void *, mbedtls_x509_crt *, int, uint32_t *);
2548 void *p_vrfy;
2549
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002550 if (authmode == MBEDTLS_SSL_VERIFY_NONE) {
2551 return 0;
2552 }
Hanno Becker68636192019-02-05 14:36:34 +00002553
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002554 if (ssl->f_vrfy != NULL) {
2555 MBEDTLS_SSL_DEBUG_MSG(3, ("Use context-specific verification callback"));
Hanno Becker8927c832019-04-03 12:52:50 +01002556 f_vrfy = ssl->f_vrfy;
2557 p_vrfy = ssl->p_vrfy;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002558 } else {
2559 MBEDTLS_SSL_DEBUG_MSG(3, ("Use configuration-specific verification callback"));
Hanno Becker8927c832019-04-03 12:52:50 +01002560 f_vrfy = ssl->conf->f_vrfy;
2561 p_vrfy = ssl->conf->p_vrfy;
2562 }
2563
Hanno Becker68636192019-02-05 14:36:34 +00002564 /*
2565 * Main check: verify certificate
2566 */
Hanno Beckerafd0b0a2019-03-27 16:55:01 +00002567#if defined(MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002568 if (ssl->conf->f_ca_cb != NULL) {
Hanno Beckerafd0b0a2019-03-27 16:55:01 +00002569 ((void) rs_ctx);
2570 have_ca_chain = 1;
2571
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002572 MBEDTLS_SSL_DEBUG_MSG(3, ("use CA callback for X.509 CRT verification"));
Jarno Lamsa9822c0d2019-04-01 16:59:48 +03002573 ret = mbedtls_x509_crt_verify_with_ca_cb(
Hanno Beckerafd0b0a2019-03-27 16:55:01 +00002574 chain,
2575 ssl->conf->f_ca_cb,
2576 ssl->conf->p_ca_cb,
2577 ssl->conf->cert_profile,
2578 ssl->hostname,
2579 &ssl->session_negotiate->verify_result,
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002580 f_vrfy, p_vrfy);
2581 } else
Hanno Beckerafd0b0a2019-03-27 16:55:01 +00002582#endif /* MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK */
2583 {
2584 mbedtls_x509_crt *ca_chain;
2585 mbedtls_x509_crl *ca_crl;
2586
2587#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002588 if (ssl->handshake->sni_ca_chain != NULL) {
Hanno Beckerafd0b0a2019-03-27 16:55:01 +00002589 ca_chain = ssl->handshake->sni_ca_chain;
2590 ca_crl = ssl->handshake->sni_ca_crl;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002591 } else
Hanno Beckerafd0b0a2019-03-27 16:55:01 +00002592#endif
2593 {
2594 ca_chain = ssl->conf->ca_chain;
2595 ca_crl = ssl->conf->ca_crl;
2596 }
2597
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002598 if (ca_chain != NULL) {
Hanno Beckerafd0b0a2019-03-27 16:55:01 +00002599 have_ca_chain = 1;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002600 }
Hanno Beckerafd0b0a2019-03-27 16:55:01 +00002601
2602 ret = mbedtls_x509_crt_verify_restartable(
2603 chain,
2604 ca_chain, ca_crl,
2605 ssl->conf->cert_profile,
2606 ssl->hostname,
2607 &ssl->session_negotiate->verify_result,
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002608 f_vrfy, p_vrfy, rs_ctx);
Hanno Beckerafd0b0a2019-03-27 16:55:01 +00002609 }
Hanno Becker68636192019-02-05 14:36:34 +00002610
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002611 if (ret != 0) {
2612 MBEDTLS_SSL_DEBUG_RET(1, "x509_verify_cert", ret);
Hanno Becker68636192019-02-05 14:36:34 +00002613 }
2614
Gilles Peskineeccd8882020-03-10 12:19:08 +01002615#if defined(MBEDTLS_SSL_ECP_RESTARTABLE_ENABLED)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002616 if (ret == MBEDTLS_ERR_ECP_IN_PROGRESS) {
2617 return MBEDTLS_ERR_SSL_CRYPTO_IN_PROGRESS;
2618 }
Hanno Becker68636192019-02-05 14:36:34 +00002619#endif
2620
2621 /*
2622 * Secondary checks: always done, but change 'ret' only if it was 0
2623 */
2624
2625#if defined(MBEDTLS_ECP_C)
2626 {
2627 const mbedtls_pk_context *pk = &chain->pk;
2628
Manuel Pégourié-Gonnard06e1fcd2022-06-16 10:48:06 +02002629 /* If certificate uses an EC key, make sure the curve is OK.
2630 * This is a public key, so it can't be opaque, so can_do() is a good
2631 * enough check to ensure pk_ec() is safe to use here. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002632 if (mbedtls_pk_can_do(pk, MBEDTLS_PK_ECKEY) &&
2633 mbedtls_ssl_check_curve(ssl, mbedtls_pk_ec(*pk)->grp.id) != 0) {
Hanno Becker68636192019-02-05 14:36:34 +00002634 ssl->session_negotiate->verify_result |= MBEDTLS_X509_BADCERT_BAD_KEY;
2635
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002636 MBEDTLS_SSL_DEBUG_MSG(1, ("bad certificate (EC key curve)"));
2637 if (ret == 0) {
Hanno Becker68636192019-02-05 14:36:34 +00002638 ret = MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002639 }
Hanno Becker68636192019-02-05 14:36:34 +00002640 }
2641 }
2642#endif /* MBEDTLS_ECP_C */
2643
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002644 if (mbedtls_ssl_check_cert_usage(chain,
2645 ciphersuite_info,
2646 !ssl->conf->endpoint,
2647 &ssl->session_negotiate->verify_result) != 0) {
2648 MBEDTLS_SSL_DEBUG_MSG(1, ("bad certificate (usage extensions)"));
2649 if (ret == 0) {
Hanno Becker68636192019-02-05 14:36:34 +00002650 ret = MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002651 }
Hanno Becker68636192019-02-05 14:36:34 +00002652 }
2653
2654 /* mbedtls_x509_crt_verify_with_profile is supposed to report a
2655 * verification failure through MBEDTLS_ERR_X509_CERT_VERIFY_FAILED,
2656 * with details encoded in the verification flags. All other kinds
2657 * of error codes, including those from the user provided f_vrfy
2658 * functions, are treated as fatal and lead to a failure of
2659 * ssl_parse_certificate even if verification was optional. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002660 if (authmode == MBEDTLS_SSL_VERIFY_OPTIONAL &&
2661 (ret == MBEDTLS_ERR_X509_CERT_VERIFY_FAILED ||
2662 ret == MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE)) {
Hanno Becker68636192019-02-05 14:36:34 +00002663 ret = 0;
2664 }
2665
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002666 if (have_ca_chain == 0 && authmode == MBEDTLS_SSL_VERIFY_REQUIRED) {
2667 MBEDTLS_SSL_DEBUG_MSG(1, ("got no CA chain"));
Hanno Becker68636192019-02-05 14:36:34 +00002668 ret = MBEDTLS_ERR_SSL_CA_CHAIN_REQUIRED;
2669 }
2670
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002671 if (ret != 0) {
Hanno Becker68636192019-02-05 14:36:34 +00002672 uint8_t alert;
2673
2674 /* The certificate may have been rejected for several reasons.
2675 Pick one and send the corresponding alert. Which alert to send
2676 may be a subject of debate in some cases. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002677 if (ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_OTHER) {
Hanno Becker68636192019-02-05 14:36:34 +00002678 alert = MBEDTLS_SSL_ALERT_MSG_ACCESS_DENIED;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002679 } else if (ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_CN_MISMATCH) {
Hanno Becker68636192019-02-05 14:36:34 +00002680 alert = MBEDTLS_SSL_ALERT_MSG_BAD_CERT;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002681 } else if (ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_KEY_USAGE) {
Hanno Becker68636192019-02-05 14:36:34 +00002682 alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002683 } else if (ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_EXT_KEY_USAGE) {
Hanno Becker68636192019-02-05 14:36:34 +00002684 alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002685 } else if (ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_NS_CERT_TYPE) {
Hanno Becker68636192019-02-05 14:36:34 +00002686 alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002687 } else if (ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_BAD_PK) {
Hanno Becker68636192019-02-05 14:36:34 +00002688 alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002689 } else if (ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_BAD_KEY) {
Hanno Becker68636192019-02-05 14:36:34 +00002690 alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002691 } else if (ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_EXPIRED) {
Hanno Becker68636192019-02-05 14:36:34 +00002692 alert = MBEDTLS_SSL_ALERT_MSG_CERT_EXPIRED;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002693 } else if (ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_REVOKED) {
Hanno Becker68636192019-02-05 14:36:34 +00002694 alert = MBEDTLS_SSL_ALERT_MSG_CERT_REVOKED;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002695 } else if (ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_NOT_TRUSTED) {
Hanno Becker68636192019-02-05 14:36:34 +00002696 alert = MBEDTLS_SSL_ALERT_MSG_UNKNOWN_CA;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002697 } else {
Hanno Becker68636192019-02-05 14:36:34 +00002698 alert = MBEDTLS_SSL_ALERT_MSG_CERT_UNKNOWN;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002699 }
2700 mbedtls_ssl_send_alert_message(ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
2701 alert);
Hanno Becker68636192019-02-05 14:36:34 +00002702 }
2703
2704#if defined(MBEDTLS_DEBUG_C)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002705 if (ssl->session_negotiate->verify_result != 0) {
2706 MBEDTLS_SSL_DEBUG_MSG(3, ("! Certificate verification flags %08x",
2707 (unsigned int) ssl->session_negotiate->verify_result));
2708 } else {
2709 MBEDTLS_SSL_DEBUG_MSG(3, ("Certificate verification flags clear"));
Hanno Becker68636192019-02-05 14:36:34 +00002710 }
2711#endif /* MBEDTLS_DEBUG_C */
2712
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002713 return ret;
Hanno Becker68636192019-02-05 14:36:34 +00002714}
2715
Hanno Becker6b8fbab2019-02-08 14:59:05 +00002716#if !defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
Manuel Pégourié-Gonnardd904d662022-06-17 10:24:00 +02002717MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002718static int ssl_remember_peer_crt_digest(mbedtls_ssl_context *ssl,
2719 unsigned char *start, size_t len)
Hanno Becker6b8fbab2019-02-08 14:59:05 +00002720{
Janos Follath865b3eb2019-12-16 11:46:15 +00002721 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Hanno Becker6b8fbab2019-02-08 14:59:05 +00002722 /* Remember digest of the peer's end-CRT. */
2723 ssl->session_negotiate->peer_cert_digest =
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002724 mbedtls_calloc(1, MBEDTLS_SSL_PEER_CERT_DIGEST_DFL_LEN);
2725 if (ssl->session_negotiate->peer_cert_digest == NULL) {
2726 MBEDTLS_SSL_DEBUG_MSG(1, ("alloc(%d bytes) failed",
2727 MBEDTLS_SSL_PEER_CERT_DIGEST_DFL_LEN));
2728 mbedtls_ssl_send_alert_message(ssl,
2729 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
2730 MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR);
Hanno Becker6b8fbab2019-02-08 14:59:05 +00002731
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002732 return MBEDTLS_ERR_SSL_ALLOC_FAILED;
Hanno Becker6b8fbab2019-02-08 14:59:05 +00002733 }
2734
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002735 ret = mbedtls_md(mbedtls_md_info_from_type(
2736 MBEDTLS_SSL_PEER_CERT_DIGEST_DFL_TYPE),
2737 start, len,
2738 ssl->session_negotiate->peer_cert_digest);
Hanno Becker6b8fbab2019-02-08 14:59:05 +00002739
2740 ssl->session_negotiate->peer_cert_digest_type =
2741 MBEDTLS_SSL_PEER_CERT_DIGEST_DFL_TYPE;
2742 ssl->session_negotiate->peer_cert_digest_len =
2743 MBEDTLS_SSL_PEER_CERT_DIGEST_DFL_LEN;
2744
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002745 return ret;
Hanno Becker6b8fbab2019-02-08 14:59:05 +00002746}
2747
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_pubkey(mbedtls_ssl_context *ssl,
2750 unsigned char *start, size_t len)
Hanno Becker6b8fbab2019-02-08 14:59:05 +00002751{
2752 unsigned char *end = start + len;
Janos Follath865b3eb2019-12-16 11:46:15 +00002753 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Hanno Becker6b8fbab2019-02-08 14:59:05 +00002754
2755 /* Make a copy of the peer's raw public key. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002756 mbedtls_pk_init(&ssl->handshake->peer_pubkey);
2757 ret = mbedtls_pk_parse_subpubkey(&start, end,
2758 &ssl->handshake->peer_pubkey);
2759 if (ret != 0) {
Hanno Becker6b8fbab2019-02-08 14:59:05 +00002760 /* We should have parsed the public key before. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002761 return MBEDTLS_ERR_SSL_INTERNAL_ERROR;
Hanno Becker6b8fbab2019-02-08 14:59:05 +00002762 }
2763
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002764 return 0;
Hanno Becker6b8fbab2019-02-08 14:59:05 +00002765}
2766#endif /* !MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
2767
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002768int mbedtls_ssl_parse_certificate(mbedtls_ssl_context *ssl)
Hanno Becker68636192019-02-05 14:36:34 +00002769{
2770 int ret = 0;
Hanno Becker28f2fcd2019-02-07 10:11:07 +00002771 int crt_expected;
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02002772#if defined(MBEDTLS_SSL_SRV_C) && defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
2773 const int authmode = ssl->handshake->sni_authmode != MBEDTLS_SSL_VERIFY_UNSET
2774 ? ssl->handshake->sni_authmode
2775 : ssl->conf->authmode;
2776#else
Johan Pascal4f099262020-09-22 10:59:26 +02002777 const int authmode = ssl->conf->authmode;
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02002778#endif
Manuel Pégourié-Gonnard3bf49c42017-08-15 13:47:06 +02002779 void *rs_ctx = NULL;
Hanno Becker3dad3112019-02-05 17:19:52 +00002780 mbedtls_x509_crt *chain = NULL;
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02002781
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002782 MBEDTLS_SSL_DEBUG_MSG(2, ("=> parse certificate"));
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02002783
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002784 crt_expected = ssl_parse_certificate_coordinate(ssl, authmode);
2785 if (crt_expected == SSL_CERTIFICATE_SKIP) {
2786 MBEDTLS_SSL_DEBUG_MSG(2, ("<= skip parse certificate"));
Hanno Becker6bdfab22019-02-05 13:11:17 +00002787 goto exit;
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02002788 }
2789
Gilles Peskineeccd8882020-03-10 12:19:08 +01002790#if defined(MBEDTLS_SSL_ECP_RESTARTABLE_ENABLED)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002791 if (ssl->handshake->ecrs_enabled &&
2792 ssl->handshake->ecrs_state == ssl_ecrs_crt_verify) {
Hanno Becker3dad3112019-02-05 17:19:52 +00002793 chain = ssl->handshake->ecrs_peer_cert;
2794 ssl->handshake->ecrs_peer_cert = NULL;
Manuel Pégourié-Gonnard3bf49c42017-08-15 13:47:06 +02002795 goto crt_verify;
2796 }
2797#endif
2798
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002799 if ((ret = mbedtls_ssl_read_record(ssl, 1)) != 0) {
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02002800 /* mbedtls_ssl_read_record may have sent an alert already. We
2801 let it decide whether to alert. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002802 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ssl_read_record", ret);
Hanno Becker3dad3112019-02-05 17:19:52 +00002803 goto exit;
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02002804 }
2805
Hanno Becker4a55f632019-02-05 12:49:06 +00002806#if defined(MBEDTLS_SSL_SRV_C)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002807 if (ssl_srv_check_client_no_crt_notification(ssl) == 0) {
Hanno Becker4a55f632019-02-05 12:49:06 +00002808 ssl->session_negotiate->verify_result = MBEDTLS_X509_BADCERT_MISSING;
Hanno Becker4a55f632019-02-05 12:49:06 +00002809
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002810 if (authmode != MBEDTLS_SSL_VERIFY_OPTIONAL) {
Hanno Becker6bdfab22019-02-05 13:11:17 +00002811 ret = MBEDTLS_ERR_SSL_NO_CLIENT_CERTIFICATE;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002812 }
Hanno Becker4a55f632019-02-05 12:49:06 +00002813
Hanno Becker6bdfab22019-02-05 13:11:17 +00002814 goto exit;
Hanno Becker4a55f632019-02-05 12:49:06 +00002815 }
2816#endif /* MBEDTLS_SSL_SRV_C */
2817
Hanno Beckerc7bd7802019-02-05 15:37:23 +00002818 /* Clear existing peer CRT structure in case we tried to
2819 * reuse a session but it failed, and allocate a new one. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002820 ssl_clear_peer_cert(ssl->session_negotiate);
Hanno Becker3dad3112019-02-05 17:19:52 +00002821
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002822 chain = mbedtls_calloc(1, sizeof(mbedtls_x509_crt));
2823 if (chain == NULL) {
2824 MBEDTLS_SSL_DEBUG_MSG(1, ("alloc(%" MBEDTLS_PRINTF_SIZET " bytes) failed",
2825 sizeof(mbedtls_x509_crt)));
2826 mbedtls_ssl_send_alert_message(ssl,
2827 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
2828 MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR);
Hanno Becker7a955a02019-02-05 13:08:01 +00002829
Hanno Becker3dad3112019-02-05 17:19:52 +00002830 ret = MBEDTLS_ERR_SSL_ALLOC_FAILED;
2831 goto exit;
2832 }
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002833 mbedtls_x509_crt_init(chain);
Hanno Becker3dad3112019-02-05 17:19:52 +00002834
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002835 ret = ssl_parse_certificate_chain(ssl, chain);
2836 if (ret != 0) {
Hanno Becker3dad3112019-02-05 17:19:52 +00002837 goto exit;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002838 }
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02002839
Gilles Peskineeccd8882020-03-10 12:19:08 +01002840#if defined(MBEDTLS_SSL_ECP_RESTARTABLE_ENABLED)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002841 if (ssl->handshake->ecrs_enabled) {
Manuel Pégourié-Gonnard0b23f162017-08-24 12:08:33 +02002842 ssl->handshake->ecrs_state = ssl_ecrs_crt_verify;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002843 }
Manuel Pégourié-Gonnard3bf49c42017-08-15 13:47:06 +02002844
2845crt_verify:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002846 if (ssl->handshake->ecrs_enabled) {
Manuel Pégourié-Gonnard3bf49c42017-08-15 13:47:06 +02002847 rs_ctx = &ssl->handshake->ecrs_ctx;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002848 }
Manuel Pégourié-Gonnard3bf49c42017-08-15 13:47:06 +02002849#endif
2850
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002851 ret = ssl_parse_certificate_verify(ssl, authmode,
2852 chain, rs_ctx);
2853 if (ret != 0) {
Hanno Becker3dad3112019-02-05 17:19:52 +00002854 goto exit;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002855 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002856
Hanno Becker6bbd94c2019-02-05 17:02:28 +00002857#if !defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
Hanno Becker6bbd94c2019-02-05 17:02:28 +00002858 {
Hanno Becker6b8fbab2019-02-08 14:59:05 +00002859 unsigned char *crt_start, *pk_start;
2860 size_t crt_len, pk_len;
Hanno Becker3dad3112019-02-05 17:19:52 +00002861
Hanno Becker6b8fbab2019-02-08 14:59:05 +00002862 /* We parse the CRT chain without copying, so
2863 * these pointers point into the input buffer,
2864 * and are hence still valid after freeing the
2865 * CRT chain. */
Hanno Becker6bbd94c2019-02-05 17:02:28 +00002866
Hanno Becker6b8fbab2019-02-08 14:59:05 +00002867 crt_start = chain->raw.p;
2868 crt_len = chain->raw.len;
Hanno Becker6bbd94c2019-02-05 17:02:28 +00002869
Hanno Becker6b8fbab2019-02-08 14:59:05 +00002870 pk_start = chain->pk_raw.p;
2871 pk_len = chain->pk_raw.len;
2872
2873 /* Free the CRT structures before computing
2874 * digest and copying the peer's public key. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002875 mbedtls_x509_crt_free(chain);
2876 mbedtls_free(chain);
Hanno Becker6b8fbab2019-02-08 14:59:05 +00002877 chain = NULL;
2878
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002879 ret = ssl_remember_peer_crt_digest(ssl, crt_start, crt_len);
2880 if (ret != 0) {
Hanno Beckera2747532019-02-06 16:19:04 +00002881 goto exit;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002882 }
Hanno Becker6b8fbab2019-02-08 14:59:05 +00002883
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002884 ret = ssl_remember_peer_pubkey(ssl, pk_start, pk_len);
2885 if (ret != 0) {
Hanno Becker6b8fbab2019-02-08 14:59:05 +00002886 goto exit;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002887 }
Hanno Beckera2747532019-02-06 16:19:04 +00002888 }
Hanno Becker6b8fbab2019-02-08 14:59:05 +00002889#else /* !MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
2890 /* Pass ownership to session structure. */
Hanno Becker3dad3112019-02-05 17:19:52 +00002891 ssl->session_negotiate->peer_cert = chain;
2892 chain = NULL;
Hanno Becker6b8fbab2019-02-08 14:59:05 +00002893#endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
Hanno Becker3dad3112019-02-05 17:19:52 +00002894
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002895 MBEDTLS_SSL_DEBUG_MSG(2, ("<= parse certificate"));
Paul Bakker5121ce52009-01-03 21:22:43 +00002896
Hanno Becker6bdfab22019-02-05 13:11:17 +00002897exit:
2898
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002899 if (ret == 0) {
Hanno Becker3dad3112019-02-05 17:19:52 +00002900 ssl->state++;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002901 }
Hanno Becker3dad3112019-02-05 17:19:52 +00002902
Gilles Peskineeccd8882020-03-10 12:19:08 +01002903#if defined(MBEDTLS_SSL_ECP_RESTARTABLE_ENABLED)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002904 if (ret == MBEDTLS_ERR_SSL_CRYPTO_IN_PROGRESS) {
Hanno Becker3dad3112019-02-05 17:19:52 +00002905 ssl->handshake->ecrs_peer_cert = chain;
2906 chain = NULL;
2907 }
2908#endif
2909
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002910 if (chain != NULL) {
2911 mbedtls_x509_crt_free(chain);
2912 mbedtls_free(chain);
Hanno Becker3dad3112019-02-05 17:19:52 +00002913 }
2914
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002915 return ret;
Paul Bakker5121ce52009-01-03 21:22:43 +00002916}
Gilles Peskineeccd8882020-03-10 12:19:08 +01002917#endif /* MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED */
Paul Bakker5121ce52009-01-03 21:22:43 +00002918
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002919void mbedtls_ssl_optimize_checksum(mbedtls_ssl_context *ssl,
2920 const mbedtls_ssl_ciphersuite_t *ciphersuite_info)
Paul Bakker380da532012-04-18 16:10:25 +00002921{
Paul Bakkerfb08fd22013-08-27 15:06:26 +02002922 ((void) ciphersuite_info);
Paul Bakker769075d2012-11-24 11:26:46 +01002923
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002924#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1) || \
2925 defined(MBEDTLS_SSL_PROTO_TLS1_1)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002926 if (ssl->minor_ver < MBEDTLS_SSL_MINOR_VERSION_3) {
Paul Bakker48916f92012-09-16 19:57:18 +00002927 ssl->handshake->update_checksum = ssl_update_checksum_md5sha1;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002928 } else
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002929#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002930#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
Gilles Peskined2d59372021-05-12 22:43:27 +02002931#if defined(MBEDTLS_SHA512_C) && !defined(MBEDTLS_SHA512_NO_SHA384)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002932 if (ciphersuite_info->mac == MBEDTLS_MD_SHA384) {
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002933 ssl->handshake->update_checksum = ssl_update_checksum_sha384;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002934 } else
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002935#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002936#if defined(MBEDTLS_SHA256_C)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002937 if (ciphersuite_info->mac != MBEDTLS_MD_SHA384) {
Paul Bakker48916f92012-09-16 19:57:18 +00002938 ssl->handshake->update_checksum = ssl_update_checksum_sha256;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002939 } else
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002940#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002941#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Manuel Pégourié-Gonnard61edffe2014-04-11 17:07:31 +02002942 {
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002943 MBEDTLS_SSL_DEBUG_MSG(1, ("should never happen"));
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002944 return;
Manuel Pégourié-Gonnard61edffe2014-04-11 17:07:31 +02002945 }
Paul Bakker380da532012-04-18 16:10:25 +00002946}
Paul Bakkerf7abd422013-04-16 13:15:56 +02002947
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002948void mbedtls_ssl_reset_checksum(mbedtls_ssl_context *ssl)
Manuel Pégourié-Gonnard67427c02014-07-11 13:45:34 +02002949{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002950#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1) || \
2951 defined(MBEDTLS_SSL_PROTO_TLS1_1)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002952 mbedtls_md5_starts_ret(&ssl->handshake->fin_md5);
2953 mbedtls_sha1_starts_ret(&ssl->handshake->fin_sha1);
Manuel Pégourié-Gonnard67427c02014-07-11 13:45:34 +02002954#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002955#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
2956#if defined(MBEDTLS_SHA256_C)
Andrzej Kurekeb342242019-01-29 09:14:33 -05002957#if defined(MBEDTLS_USE_PSA_CRYPTO)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002958 psa_hash_abort(&ssl->handshake->fin_sha256_psa);
2959 psa_hash_setup(&ssl->handshake->fin_sha256_psa, PSA_ALG_SHA_256);
Andrzej Kurekeb342242019-01-29 09:14:33 -05002960#else
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002961 mbedtls_sha256_starts_ret(&ssl->handshake->fin_sha256, 0);
Manuel Pégourié-Gonnard67427c02014-07-11 13:45:34 +02002962#endif
Andrzej Kurekeb342242019-01-29 09:14:33 -05002963#endif
Gilles Peskined2d59372021-05-12 22:43:27 +02002964#if defined(MBEDTLS_SHA512_C) && !defined(MBEDTLS_SHA512_NO_SHA384)
Andrzej Kurekeb342242019-01-29 09:14:33 -05002965#if defined(MBEDTLS_USE_PSA_CRYPTO)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002966 psa_hash_abort(&ssl->handshake->fin_sha384_psa);
2967 psa_hash_setup(&ssl->handshake->fin_sha384_psa, PSA_ALG_SHA_384);
Andrzej Kurekeb342242019-01-29 09:14:33 -05002968#else
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002969 mbedtls_sha512_starts_ret(&ssl->handshake->fin_sha512, 1);
Manuel Pégourié-Gonnard67427c02014-07-11 13:45:34 +02002970#endif
Andrzej Kurekeb342242019-01-29 09:14:33 -05002971#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002972#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Manuel Pégourié-Gonnard67427c02014-07-11 13:45:34 +02002973}
2974
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002975static void ssl_update_checksum_start(mbedtls_ssl_context *ssl,
2976 const unsigned char *buf, size_t len)
Paul Bakker380da532012-04-18 16:10:25 +00002977{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002978#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1) || \
2979 defined(MBEDTLS_SSL_PROTO_TLS1_1)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002980 mbedtls_md5_update_ret(&ssl->handshake->fin_md5, buf, len);
2981 mbedtls_sha1_update_ret(&ssl->handshake->fin_sha1, buf, len);
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002982#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002983#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
2984#if defined(MBEDTLS_SHA256_C)
Andrzej Kurekeb342242019-01-29 09:14:33 -05002985#if defined(MBEDTLS_USE_PSA_CRYPTO)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002986 psa_hash_update(&ssl->handshake->fin_sha256_psa, buf, len);
Andrzej Kurekeb342242019-01-29 09:14:33 -05002987#else
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002988 mbedtls_sha256_update_ret(&ssl->handshake->fin_sha256, buf, len);
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002989#endif
Andrzej Kurekeb342242019-01-29 09:14:33 -05002990#endif
Gilles Peskined2d59372021-05-12 22:43:27 +02002991#if defined(MBEDTLS_SHA512_C) && !defined(MBEDTLS_SHA512_NO_SHA384)
Andrzej Kurekeb342242019-01-29 09:14:33 -05002992#if defined(MBEDTLS_USE_PSA_CRYPTO)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002993 psa_hash_update(&ssl->handshake->fin_sha384_psa, buf, len);
Andrzej Kurekeb342242019-01-29 09:14:33 -05002994#else
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002995 mbedtls_sha512_update_ret(&ssl->handshake->fin_sha512, buf, len);
Paul Bakker769075d2012-11-24 11:26:46 +01002996#endif
Andrzej Kurekeb342242019-01-29 09:14:33 -05002997#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002998#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakker380da532012-04-18 16:10:25 +00002999}
3000
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003001#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1) || \
3002 defined(MBEDTLS_SSL_PROTO_TLS1_1)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003003static void ssl_update_checksum_md5sha1(mbedtls_ssl_context *ssl,
3004 const unsigned char *buf, size_t len)
Paul Bakker380da532012-04-18 16:10:25 +00003005{
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003006 mbedtls_md5_update_ret(&ssl->handshake->fin_md5, buf, len);
3007 mbedtls_sha1_update_ret(&ssl->handshake->fin_sha1, buf, len);
Paul Bakker380da532012-04-18 16:10:25 +00003008}
Paul Bakkerd2f068e2013-08-27 21:19:20 +02003009#endif
Paul Bakker380da532012-04-18 16:10:25 +00003010
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003011#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
3012#if defined(MBEDTLS_SHA256_C)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003013static void ssl_update_checksum_sha256(mbedtls_ssl_context *ssl,
3014 const unsigned char *buf, size_t len)
Paul Bakker380da532012-04-18 16:10:25 +00003015{
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);
Andrzej Kurekeb342242019-01-29 09:14:33 -05003020#endif
Paul Bakker380da532012-04-18 16:10:25 +00003021}
Paul Bakkerd2f068e2013-08-27 21:19:20 +02003022#endif
Paul Bakker380da532012-04-18 16:10:25 +00003023
Gilles Peskined2d59372021-05-12 22:43:27 +02003024#if defined(MBEDTLS_SHA512_C) && !defined(MBEDTLS_SHA512_NO_SHA384)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003025static void ssl_update_checksum_sha384(mbedtls_ssl_context *ssl,
3026 const unsigned char *buf, size_t len)
Paul Bakker380da532012-04-18 16:10:25 +00003027{
Andrzej Kurekeb342242019-01-29 09:14:33 -05003028#if defined(MBEDTLS_USE_PSA_CRYPTO)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003029 psa_hash_update(&ssl->handshake->fin_sha384_psa, buf, len);
Andrzej Kurekeb342242019-01-29 09:14:33 -05003030#else
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003031 mbedtls_sha512_update_ret(&ssl->handshake->fin_sha512, buf, len);
Andrzej Kurekeb342242019-01-29 09:14:33 -05003032#endif
Paul Bakker380da532012-04-18 16:10:25 +00003033}
Paul Bakker769075d2012-11-24 11:26:46 +01003034#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003035#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakker380da532012-04-18 16:10:25 +00003036
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003037#if defined(MBEDTLS_SSL_PROTO_SSL3)
Paul Bakker1ef83d62012-04-11 12:09:53 +00003038static void ssl_calc_finished_ssl(
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003039 mbedtls_ssl_context *ssl, unsigned char *buf, int from)
Paul Bakker5121ce52009-01-03 21:22:43 +00003040{
Paul Bakker3c2122f2013-06-24 19:03:14 +02003041 const char *sender;
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02003042 mbedtls_md5_context md5;
3043 mbedtls_sha1_context sha1;
Paul Bakker1ef83d62012-04-11 12:09:53 +00003044
Paul Bakker5121ce52009-01-03 21:22:43 +00003045 unsigned char padbuf[48];
3046 unsigned char md5sum[16];
3047 unsigned char sha1sum[20];
3048
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003049 mbedtls_ssl_session *session = ssl->session_negotiate;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003050 if (!session) {
Paul Bakker48916f92012-09-16 19:57:18 +00003051 session = ssl->session;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003052 }
Paul Bakker48916f92012-09-16 19:57:18 +00003053
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003054 MBEDTLS_SSL_DEBUG_MSG(2, ("=> calc finished ssl"));
Paul Bakker1ef83d62012-04-11 12:09:53 +00003055
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003056 mbedtls_md5_init(&md5);
3057 mbedtls_sha1_init(&sha1);
Manuel Pégourié-Gonnard001f2b62015-07-06 16:21:13 +02003058
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003059 mbedtls_md5_clone(&md5, &ssl->handshake->fin_md5);
3060 mbedtls_sha1_clone(&sha1, &ssl->handshake->fin_sha1);
Paul Bakker5121ce52009-01-03 21:22:43 +00003061
3062 /*
3063 * SSLv3:
3064 * hash =
3065 * MD5( master + pad2 +
3066 * MD5( handshake + sender + master + pad1 ) )
3067 * + SHA1( master + pad2 +
3068 * SHA1( handshake + sender + master + pad1 ) )
Paul Bakker5121ce52009-01-03 21:22:43 +00003069 */
3070
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003071#if !defined(MBEDTLS_MD5_ALT)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003072 MBEDTLS_SSL_DEBUG_BUF(4, "finished md5 state", (unsigned char *)
3073 md5.state, sizeof(md5.state));
Paul Bakker90995b52013-06-24 19:20:35 +02003074#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00003075
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003076#if !defined(MBEDTLS_SHA1_ALT)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003077 MBEDTLS_SSL_DEBUG_BUF(4, "finished sha1 state", (unsigned char *)
3078 sha1.state, sizeof(sha1.state));
Paul Bakker90995b52013-06-24 19:20:35 +02003079#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00003080
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003081 sender = (from == MBEDTLS_SSL_IS_CLIENT) ? "CLNT"
Paul Bakker3c2122f2013-06-24 19:03:14 +02003082 : "SRVR";
Paul Bakker5121ce52009-01-03 21:22:43 +00003083
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003084 memset(padbuf, 0x36, 48);
Paul Bakker5121ce52009-01-03 21:22:43 +00003085
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003086 mbedtls_md5_update_ret(&md5, (const unsigned char *) sender, 4);
3087 mbedtls_md5_update_ret(&md5, session->master, 48);
3088 mbedtls_md5_update_ret(&md5, padbuf, 48);
3089 mbedtls_md5_finish_ret(&md5, md5sum);
Paul Bakker5121ce52009-01-03 21:22:43 +00003090
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003091 mbedtls_sha1_update_ret(&sha1, (const unsigned char *) sender, 4);
3092 mbedtls_sha1_update_ret(&sha1, session->master, 48);
3093 mbedtls_sha1_update_ret(&sha1, padbuf, 40);
3094 mbedtls_sha1_finish_ret(&sha1, sha1sum);
Paul Bakker5121ce52009-01-03 21:22:43 +00003095
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003096 memset(padbuf, 0x5C, 48);
Paul Bakker5121ce52009-01-03 21:22:43 +00003097
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003098 mbedtls_md5_starts_ret(&md5);
3099 mbedtls_md5_update_ret(&md5, session->master, 48);
3100 mbedtls_md5_update_ret(&md5, padbuf, 48);
3101 mbedtls_md5_update_ret(&md5, md5sum, 16);
3102 mbedtls_md5_finish_ret(&md5, buf);
Paul Bakker5121ce52009-01-03 21:22:43 +00003103
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003104 mbedtls_sha1_starts_ret(&sha1);
3105 mbedtls_sha1_update_ret(&sha1, session->master, 48);
3106 mbedtls_sha1_update_ret(&sha1, padbuf, 40);
3107 mbedtls_sha1_update_ret(&sha1, sha1sum, 20);
3108 mbedtls_sha1_finish_ret(&sha1, buf + 16);
Paul Bakker5121ce52009-01-03 21:22:43 +00003109
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003110 MBEDTLS_SSL_DEBUG_BUF(3, "calc finished result", buf, 36);
Paul Bakker5121ce52009-01-03 21:22:43 +00003111
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003112 mbedtls_md5_free(&md5);
3113 mbedtls_sha1_free(&sha1);
Paul Bakker5121ce52009-01-03 21:22:43 +00003114
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003115 mbedtls_platform_zeroize(padbuf, sizeof(padbuf));
3116 mbedtls_platform_zeroize(md5sum, sizeof(md5sum));
3117 mbedtls_platform_zeroize(sha1sum, sizeof(sha1sum));
Paul Bakker5121ce52009-01-03 21:22:43 +00003118
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003119 MBEDTLS_SSL_DEBUG_MSG(2, ("<= calc finished"));
Paul Bakker5121ce52009-01-03 21:22:43 +00003120}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003121#endif /* MBEDTLS_SSL_PROTO_SSL3 */
Paul Bakker5121ce52009-01-03 21:22:43 +00003122
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003123#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1)
Paul Bakker1ef83d62012-04-11 12:09:53 +00003124static void ssl_calc_finished_tls(
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003125 mbedtls_ssl_context *ssl, unsigned char *buf, int from)
Paul Bakker5121ce52009-01-03 21:22:43 +00003126{
Paul Bakker1ef83d62012-04-11 12:09:53 +00003127 int len = 12;
Paul Bakker3c2122f2013-06-24 19:03:14 +02003128 const char *sender;
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02003129 mbedtls_md5_context md5;
3130 mbedtls_sha1_context sha1;
Paul Bakker1ef83d62012-04-11 12:09:53 +00003131 unsigned char padbuf[36];
Paul Bakker5121ce52009-01-03 21:22:43 +00003132
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003133 mbedtls_ssl_session *session = ssl->session_negotiate;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003134 if (!session) {
Paul Bakker48916f92012-09-16 19:57:18 +00003135 session = ssl->session;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003136 }
Paul Bakker48916f92012-09-16 19:57:18 +00003137
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003138 MBEDTLS_SSL_DEBUG_MSG(2, ("=> calc finished tls"));
Paul Bakker5121ce52009-01-03 21:22:43 +00003139
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003140 mbedtls_md5_init(&md5);
3141 mbedtls_sha1_init(&sha1);
Manuel Pégourié-Gonnard001f2b62015-07-06 16:21:13 +02003142
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003143 mbedtls_md5_clone(&md5, &ssl->handshake->fin_md5);
3144 mbedtls_sha1_clone(&sha1, &ssl->handshake->fin_sha1);
Paul Bakker5121ce52009-01-03 21:22:43 +00003145
Paul Bakker1ef83d62012-04-11 12:09:53 +00003146 /*
3147 * TLSv1:
3148 * hash = PRF( master, finished_label,
3149 * MD5( handshake ) + SHA1( handshake ) )[0..11]
3150 */
Paul Bakker5121ce52009-01-03 21:22:43 +00003151
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003152#if !defined(MBEDTLS_MD5_ALT)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003153 MBEDTLS_SSL_DEBUG_BUF(4, "finished md5 state", (unsigned char *)
3154 md5.state, sizeof(md5.state));
Paul Bakker90995b52013-06-24 19:20:35 +02003155#endif
Paul Bakker1ef83d62012-04-11 12:09:53 +00003156
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003157#if !defined(MBEDTLS_SHA1_ALT)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003158 MBEDTLS_SSL_DEBUG_BUF(4, "finished sha1 state", (unsigned char *)
3159 sha1.state, sizeof(sha1.state));
Paul Bakker90995b52013-06-24 19:20:35 +02003160#endif
Paul Bakker1ef83d62012-04-11 12:09:53 +00003161
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003162 sender = (from == MBEDTLS_SSL_IS_CLIENT)
Paul Bakker3c2122f2013-06-24 19:03:14 +02003163 ? "client finished"
3164 : "server finished";
Paul Bakker1ef83d62012-04-11 12:09:53 +00003165
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003166 mbedtls_md5_finish_ret(&md5, padbuf);
3167 mbedtls_sha1_finish_ret(&sha1, padbuf + 16);
Paul Bakker1ef83d62012-04-11 12:09:53 +00003168
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003169 ssl->handshake->tls_prf(session->master, 48, sender,
3170 padbuf, 36, buf, len);
Paul Bakker1ef83d62012-04-11 12:09:53 +00003171
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003172 MBEDTLS_SSL_DEBUG_BUF(3, "calc finished result", buf, len);
Paul Bakker1ef83d62012-04-11 12:09:53 +00003173
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003174 mbedtls_md5_free(&md5);
3175 mbedtls_sha1_free(&sha1);
Paul Bakker1ef83d62012-04-11 12:09:53 +00003176
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003177 mbedtls_platform_zeroize(padbuf, sizeof(padbuf));
Paul Bakker1ef83d62012-04-11 12:09:53 +00003178
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003179 MBEDTLS_SSL_DEBUG_MSG(2, ("<= calc finished"));
Paul Bakker1ef83d62012-04-11 12:09:53 +00003180}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003181#endif /* MBEDTLS_SSL_PROTO_TLS1 || MBEDTLS_SSL_PROTO_TLS1_1 */
Paul Bakker1ef83d62012-04-11 12:09:53 +00003182
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003183#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
3184#if defined(MBEDTLS_SHA256_C)
Paul Bakkerca4ab492012-04-18 14:23:57 +00003185static void ssl_calc_finished_tls_sha256(
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003186 mbedtls_ssl_context *ssl, unsigned char *buf, int from)
Paul Bakker1ef83d62012-04-11 12:09:53 +00003187{
3188 int len = 12;
Paul Bakker3c2122f2013-06-24 19:03:14 +02003189 const char *sender;
Paul Bakker1ef83d62012-04-11 12:09:53 +00003190 unsigned char padbuf[32];
Andrzej Kurekeb342242019-01-29 09:14:33 -05003191#if defined(MBEDTLS_USE_PSA_CRYPTO)
3192 size_t hash_size;
Jaeden Amero34973232019-02-20 10:32:28 +00003193 psa_hash_operation_t sha256_psa = PSA_HASH_OPERATION_INIT;
Andrzej Kurekeb342242019-01-29 09:14:33 -05003194 psa_status_t status;
3195#else
3196 mbedtls_sha256_context sha256;
3197#endif
Paul Bakker1ef83d62012-04-11 12:09:53 +00003198
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003199 mbedtls_ssl_session *session = ssl->session_negotiate;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003200 if (!session) {
Paul Bakker48916f92012-09-16 19:57:18 +00003201 session = ssl->session;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003202 }
Paul Bakker48916f92012-09-16 19:57:18 +00003203
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003204 sender = (from == MBEDTLS_SSL_IS_CLIENT)
Andrzej Kurekeb342242019-01-29 09:14:33 -05003205 ? "client finished"
3206 : "server finished";
3207
3208#if defined(MBEDTLS_USE_PSA_CRYPTO)
3209 sha256_psa = psa_hash_operation_init();
3210
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003211 MBEDTLS_SSL_DEBUG_MSG(2, ("=> calc PSA finished tls sha256"));
Andrzej Kurekeb342242019-01-29 09:14:33 -05003212
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003213 status = psa_hash_clone(&ssl->handshake->fin_sha256_psa, &sha256_psa);
3214 if (status != PSA_SUCCESS) {
3215 MBEDTLS_SSL_DEBUG_MSG(2, ("PSA hash clone failed"));
Andrzej Kurekeb342242019-01-29 09:14:33 -05003216 return;
3217 }
3218
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003219 status = psa_hash_finish(&sha256_psa, padbuf, sizeof(padbuf), &hash_size);
3220 if (status != PSA_SUCCESS) {
3221 MBEDTLS_SSL_DEBUG_MSG(2, ("PSA hash finish failed"));
Andrzej Kurekeb342242019-01-29 09:14:33 -05003222 return;
3223 }
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003224 MBEDTLS_SSL_DEBUG_BUF(3, "PSA calculated padbuf", padbuf, 32);
Andrzej Kurekeb342242019-01-29 09:14:33 -05003225#else
3226
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003227 mbedtls_sha256_init(&sha256);
Manuel Pégourié-Gonnard001f2b62015-07-06 16:21:13 +02003228
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003229 MBEDTLS_SSL_DEBUG_MSG(2, ("=> calc finished tls sha256"));
Paul Bakker1ef83d62012-04-11 12:09:53 +00003230
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003231 mbedtls_sha256_clone(&sha256, &ssl->handshake->fin_sha256);
Paul Bakker1ef83d62012-04-11 12:09:53 +00003232
3233 /*
3234 * TLSv1.2:
3235 * hash = PRF( master, finished_label,
3236 * Hash( handshake ) )[0.11]
3237 */
3238
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003239#if !defined(MBEDTLS_SHA256_ALT)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003240 MBEDTLS_SSL_DEBUG_BUF(4, "finished sha2 state", (unsigned char *)
3241 sha256.state, sizeof(sha256.state));
Paul Bakker90995b52013-06-24 19:20:35 +02003242#endif
Paul Bakker1ef83d62012-04-11 12:09:53 +00003243
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003244 mbedtls_sha256_finish_ret(&sha256, padbuf);
3245 mbedtls_sha256_free(&sha256);
Andrzej Kurekeb342242019-01-29 09:14:33 -05003246#endif /* MBEDTLS_USE_PSA_CRYPTO */
Paul Bakker1ef83d62012-04-11 12:09:53 +00003247
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003248 ssl->handshake->tls_prf(session->master, 48, sender,
3249 padbuf, 32, buf, len);
Paul Bakker1ef83d62012-04-11 12:09:53 +00003250
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003251 MBEDTLS_SSL_DEBUG_BUF(3, "calc finished result", buf, len);
Paul Bakker1ef83d62012-04-11 12:09:53 +00003252
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003253 mbedtls_platform_zeroize(padbuf, sizeof(padbuf));
Paul Bakker1ef83d62012-04-11 12:09:53 +00003254
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003255 MBEDTLS_SSL_DEBUG_MSG(2, ("<= calc finished"));
Paul Bakker1ef83d62012-04-11 12:09:53 +00003256}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003257#endif /* MBEDTLS_SHA256_C */
Paul Bakker1ef83d62012-04-11 12:09:53 +00003258
Gilles Peskined2d59372021-05-12 22:43:27 +02003259#if defined(MBEDTLS_SHA512_C) && !defined(MBEDTLS_SHA512_NO_SHA384)
Rodrigo Dias Corread596ca82020-11-25 00:42:28 -03003260
Paul Bakkerca4ab492012-04-18 14:23:57 +00003261static void ssl_calc_finished_tls_sha384(
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003262 mbedtls_ssl_context *ssl, unsigned char *buf, int from)
Paul Bakkerca4ab492012-04-18 14:23:57 +00003263{
3264 int len = 12;
Paul Bakker3c2122f2013-06-24 19:03:14 +02003265 const char *sender;
Rodrigo Dias Corread596ca82020-11-25 00:42:28 -03003266 unsigned char padbuf[48];
Andrzej Kurekeb342242019-01-29 09:14:33 -05003267#if defined(MBEDTLS_USE_PSA_CRYPTO)
3268 size_t hash_size;
Jaeden Amero34973232019-02-20 10:32:28 +00003269 psa_hash_operation_t sha384_psa = PSA_HASH_OPERATION_INIT;
Andrzej Kurekeb342242019-01-29 09:14:33 -05003270 psa_status_t status;
3271#else
3272 mbedtls_sha512_context sha512;
3273#endif
Paul Bakkerca4ab492012-04-18 14:23:57 +00003274
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003275 mbedtls_ssl_session *session = ssl->session_negotiate;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003276 if (!session) {
Paul Bakker48916f92012-09-16 19:57:18 +00003277 session = ssl->session;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003278 }
Paul Bakker48916f92012-09-16 19:57:18 +00003279
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003280 sender = (from == MBEDTLS_SSL_IS_CLIENT)
Andrzej Kurekeb342242019-01-29 09:14:33 -05003281 ? "client finished"
3282 : "server finished";
3283
3284#if defined(MBEDTLS_USE_PSA_CRYPTO)
Andrzej Kurek972fba52019-01-30 03:29:12 -05003285 sha384_psa = psa_hash_operation_init();
Andrzej Kurekeb342242019-01-29 09:14:33 -05003286
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003287 MBEDTLS_SSL_DEBUG_MSG(2, ("=> calc PSA finished tls sha384"));
Andrzej Kurekeb342242019-01-29 09:14:33 -05003288
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003289 status = psa_hash_clone(&ssl->handshake->fin_sha384_psa, &sha384_psa);
3290 if (status != PSA_SUCCESS) {
3291 MBEDTLS_SSL_DEBUG_MSG(2, ("PSA hash clone failed"));
Andrzej Kurekeb342242019-01-29 09:14:33 -05003292 return;
3293 }
3294
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003295 status = psa_hash_finish(&sha384_psa, padbuf, sizeof(padbuf), &hash_size);
3296 if (status != PSA_SUCCESS) {
3297 MBEDTLS_SSL_DEBUG_MSG(2, ("PSA hash finish failed"));
Andrzej Kurekeb342242019-01-29 09:14:33 -05003298 return;
3299 }
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003300 MBEDTLS_SSL_DEBUG_BUF(3, "PSA calculated padbuf", padbuf, 48);
Andrzej Kurekeb342242019-01-29 09:14:33 -05003301#else
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003302 mbedtls_sha512_init(&sha512);
Manuel Pégourié-Gonnard001f2b62015-07-06 16:21:13 +02003303
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003304 MBEDTLS_SSL_DEBUG_MSG(2, ("=> calc finished tls sha384"));
Paul Bakkerca4ab492012-04-18 14:23:57 +00003305
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003306 mbedtls_sha512_clone(&sha512, &ssl->handshake->fin_sha512);
Paul Bakkerca4ab492012-04-18 14:23:57 +00003307
3308 /*
3309 * TLSv1.2:
3310 * hash = PRF( master, finished_label,
3311 * Hash( handshake ) )[0.11]
3312 */
3313
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003314#if !defined(MBEDTLS_SHA512_ALT)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003315 MBEDTLS_SSL_DEBUG_BUF(4, "finished sha512 state", (unsigned char *)
3316 sha512.state, sizeof(sha512.state));
Paul Bakker90995b52013-06-24 19:20:35 +02003317#endif
Gilles Peskinebb66dac2021-05-13 00:00:45 +02003318 /* mbedtls_sha512_finish_ret's output parameter is declared as a
Tom Cosgrove49f99bc2022-12-04 16:44:21 +00003319 * 64-byte buffer, but since we're using SHA-384, we know that the
Gilles Peskinebb66dac2021-05-13 00:00:45 +02003320 * output fits in 48 bytes. This is correct C, but GCC 11.1 warns
3321 * about it.
Rodrigo Dias Corread596ca82020-11-25 00:42:28 -03003322 */
Gilles Peskinebb66dac2021-05-13 00:00:45 +02003323#if defined(__GNUC__) && __GNUC__ >= 11
3324#pragma GCC diagnostic push
3325#pragma GCC diagnostic ignored "-Wstringop-overflow"
3326#endif
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003327 mbedtls_sha512_finish_ret(&sha512, padbuf);
Gilles Peskinebb66dac2021-05-13 00:00:45 +02003328#if defined(__GNUC__) && __GNUC__ >= 11
3329#pragma GCC diagnostic pop
3330#endif
Paul Bakkerca4ab492012-04-18 14:23:57 +00003331
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003332 mbedtls_sha512_free(&sha512);
Andrzej Kurekeb342242019-01-29 09:14:33 -05003333#endif
Paul Bakkerca4ab492012-04-18 14:23:57 +00003334
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003335 ssl->handshake->tls_prf(session->master, 48, sender,
3336 padbuf, 48, buf, len);
Paul Bakkerca4ab492012-04-18 14:23:57 +00003337
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003338 MBEDTLS_SSL_DEBUG_BUF(3, "calc finished result", buf, len);
Paul Bakkerca4ab492012-04-18 14:23:57 +00003339
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003340 mbedtls_platform_zeroize(padbuf, sizeof(padbuf));
Paul Bakkerca4ab492012-04-18 14:23:57 +00003341
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003342 MBEDTLS_SSL_DEBUG_MSG(2, ("<= calc finished"));
Paul Bakkerca4ab492012-04-18 14:23:57 +00003343}
Gilles Peskined2d59372021-05-12 22:43:27 +02003344#endif /* MBEDTLS_SHA512_C && !MBEDTLS_SHA512_NO_SHA384 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003345#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakkerca4ab492012-04-18 14:23:57 +00003346
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003347void mbedtls_ssl_handshake_wrapup_free_hs_transform(mbedtls_ssl_context *ssl)
Paul Bakker48916f92012-09-16 19:57:18 +00003348{
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003349 MBEDTLS_SSL_DEBUG_MSG(3, ("=> handshake wrapup: final free"));
Paul Bakker48916f92012-09-16 19:57:18 +00003350
3351 /*
3352 * Free our handshake params
3353 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003354 mbedtls_ssl_handshake_free(ssl);
3355 mbedtls_free(ssl->handshake);
Paul Bakker48916f92012-09-16 19:57:18 +00003356 ssl->handshake = NULL;
3357
3358 /*
Shaun Case0e7791f2021-12-20 21:14:10 -08003359 * Free the previous transform and switch in the current one
Paul Bakker48916f92012-09-16 19:57:18 +00003360 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003361 if (ssl->transform) {
3362 mbedtls_ssl_transform_free(ssl->transform);
3363 mbedtls_free(ssl->transform);
Paul Bakker48916f92012-09-16 19:57:18 +00003364 }
3365 ssl->transform = ssl->transform_negotiate;
3366 ssl->transform_negotiate = NULL;
3367
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003368 MBEDTLS_SSL_DEBUG_MSG(3, ("<= handshake wrapup: final free"));
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02003369}
3370
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003371void mbedtls_ssl_handshake_wrapup(mbedtls_ssl_context *ssl)
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02003372{
3373 int resume = ssl->handshake->resume;
3374
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003375 MBEDTLS_SSL_DEBUG_MSG(3, ("=> handshake wrapup"));
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02003376
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003377#if defined(MBEDTLS_SSL_RENEGOTIATION)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003378 if (ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS) {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003379 ssl->renego_status = MBEDTLS_SSL_RENEGOTIATION_DONE;
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02003380 ssl->renego_records_seen = 0;
3381 }
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01003382#endif
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02003383
3384 /*
3385 * Free the previous session and switch in the current one
3386 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003387 if (ssl->session) {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003388#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
Manuel Pégourié-Gonnard1a034732014-11-04 17:36:18 +01003389 /* RFC 7366 3.1: keep the EtM state */
3390 ssl->session_negotiate->encrypt_then_mac =
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003391 ssl->session->encrypt_then_mac;
Manuel Pégourié-Gonnard1a034732014-11-04 17:36:18 +01003392#endif
3393
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003394 mbedtls_ssl_session_free(ssl->session);
3395 mbedtls_free(ssl->session);
Paul Bakker0a597072012-09-25 21:55:46 +00003396 }
3397 ssl->session = ssl->session_negotiate;
Paul Bakker48916f92012-09-16 19:57:18 +00003398 ssl->session_negotiate = NULL;
3399
Paul Bakker0a597072012-09-25 21:55:46 +00003400 /*
3401 * Add cache entry
3402 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003403 if (ssl->conf->f_set_cache != NULL &&
Manuel Pégourié-Gonnard12ad7982015-06-18 15:50:37 +02003404 ssl->session->id_len != 0 &&
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003405 resume == 0) {
3406 if (ssl->conf->f_set_cache(ssl->conf->p_cache, ssl->session) != 0) {
3407 MBEDTLS_SSL_DEBUG_MSG(1, ("cache did not store session"));
3408 }
Manuel Pégourié-Gonnardc086cce2013-08-02 14:13:02 +02003409 }
Paul Bakker0a597072012-09-25 21:55:46 +00003410
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003411#if defined(MBEDTLS_SSL_PROTO_DTLS)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003412 if (ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
3413 ssl->handshake->flight != NULL) {
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02003414 /* Cancel handshake timer */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003415 mbedtls_ssl_set_timer(ssl, 0);
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02003416
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02003417 /* Keep last flight around in case we need to resend it:
3418 * we need the handshake and transform structures for that */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003419 MBEDTLS_SSL_DEBUG_MSG(3, ("skip freeing handshake and transform"));
3420 } else
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02003421#endif
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003422 mbedtls_ssl_handshake_wrapup_free_hs_transform(ssl);
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02003423
Paul Bakker48916f92012-09-16 19:57:18 +00003424 ssl->state++;
3425
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003426 MBEDTLS_SSL_DEBUG_MSG(3, ("<= handshake wrapup"));
Paul Bakker48916f92012-09-16 19:57:18 +00003427}
3428
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003429int mbedtls_ssl_write_finished(mbedtls_ssl_context *ssl)
Paul Bakker1ef83d62012-04-11 12:09:53 +00003430{
Manuel Pégourié-Gonnard879a4f92014-07-11 22:31:12 +02003431 int ret, hash_len;
Paul Bakker1ef83d62012-04-11 12:09:53 +00003432
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003433 MBEDTLS_SSL_DEBUG_MSG(2, ("=> write finished"));
Paul Bakker1ef83d62012-04-11 12:09:53 +00003434
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003435 mbedtls_ssl_update_out_pointers(ssl, ssl->transform_negotiate);
Paul Bakker92be97b2013-01-02 17:30:03 +01003436
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003437 ssl->handshake->calc_finished(ssl, ssl->out_msg + 4, ssl->conf->endpoint);
Paul Bakker1ef83d62012-04-11 12:09:53 +00003438
Manuel Pégourié-Gonnard214a8482016-02-22 11:27:26 +01003439 /*
3440 * RFC 5246 7.4.9 (Page 63) says 12 is the default length and ciphersuites
3441 * may define some other value. Currently (early 2016), no defined
3442 * ciphersuite does this (and this is unlikely to change as activity has
3443 * moved to TLS 1.3 now) so we can keep the hardcoded 12 here.
3444 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003445 hash_len = (ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0) ? 36 : 12;
Paul Bakker5121ce52009-01-03 21:22:43 +00003446
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003447#if defined(MBEDTLS_SSL_RENEGOTIATION)
Paul Bakker48916f92012-09-16 19:57:18 +00003448 ssl->verify_data_len = hash_len;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003449 memcpy(ssl->own_verify_data, ssl->out_msg + 4, hash_len);
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01003450#endif
Paul Bakker48916f92012-09-16 19:57:18 +00003451
Paul Bakker5121ce52009-01-03 21:22:43 +00003452 ssl->out_msglen = 4 + hash_len;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003453 ssl->out_msgtype = MBEDTLS_SSL_MSG_HANDSHAKE;
3454 ssl->out_msg[0] = MBEDTLS_SSL_HS_FINISHED;
Paul Bakker5121ce52009-01-03 21:22:43 +00003455
3456 /*
3457 * In case of session resuming, invert the client and server
3458 * ChangeCipherSpec messages order.
3459 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003460 if (ssl->handshake->resume != 0) {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003461#if defined(MBEDTLS_SSL_CLI_C)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003462 if (ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT) {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003463 ssl->state = MBEDTLS_SSL_HANDSHAKE_WRAPUP;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003464 }
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01003465#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003466#if defined(MBEDTLS_SSL_SRV_C)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003467 if (ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER) {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003468 ssl->state = MBEDTLS_SSL_CLIENT_CHANGE_CIPHER_SPEC;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003469 }
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01003470#endif
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003471 } else {
Paul Bakker5121ce52009-01-03 21:22:43 +00003472 ssl->state++;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003473 }
Paul Bakker5121ce52009-01-03 21:22:43 +00003474
Paul Bakker48916f92012-09-16 19:57:18 +00003475 /*
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02003476 * Switch to our negotiated transform and session parameters for outbound
3477 * data.
Paul Bakker48916f92012-09-16 19:57:18 +00003478 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003479 MBEDTLS_SSL_DEBUG_MSG(3, ("switching to new transform spec for outbound data"));
Manuel Pégourié-Gonnard5afb1672014-02-16 18:33:22 +01003480
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003481#if defined(MBEDTLS_SSL_PROTO_DTLS)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003482 if (ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM) {
Manuel Pégourié-Gonnard879a4f92014-07-11 22:31:12 +02003483 unsigned char i;
3484
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003485 /* Remember current epoch settings for resending */
3486 ssl->handshake->alt_transform_out = ssl->transform_out;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003487 memcpy(ssl->handshake->alt_out_ctr, ssl->cur_out_ctr, 8);
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003488
Manuel Pégourié-Gonnard879a4f92014-07-11 22:31:12 +02003489 /* Set sequence_number to zero */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003490 memset(ssl->cur_out_ctr + 2, 0, 6);
Manuel Pégourié-Gonnard879a4f92014-07-11 22:31:12 +02003491
3492 /* Increment epoch */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003493 for (i = 2; i > 0; i--) {
3494 if (++ssl->cur_out_ctr[i - 1] != 0) {
Manuel Pégourié-Gonnard879a4f92014-07-11 22:31:12 +02003495 break;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003496 }
3497 }
Manuel Pégourié-Gonnard879a4f92014-07-11 22:31:12 +02003498
3499 /* The loop goes to its end iff the counter is wrapping */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003500 if (i == 0) {
3501 MBEDTLS_SSL_DEBUG_MSG(1, ("DTLS epoch would wrap"));
3502 return MBEDTLS_ERR_SSL_COUNTER_WRAPPING;
Manuel Pégourié-Gonnard879a4f92014-07-11 22:31:12 +02003503 }
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003504 } else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003505#endif /* MBEDTLS_SSL_PROTO_DTLS */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003506 memset(ssl->cur_out_ctr, 0, 8);
Paul Bakker5121ce52009-01-03 21:22:43 +00003507
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003508 ssl->transform_out = ssl->transform_negotiate;
3509 ssl->session_out = ssl->session_negotiate;
3510
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003511#if defined(MBEDTLS_SSL_HW_RECORD_ACCEL)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003512 if (mbedtls_ssl_hw_record_activate != NULL) {
3513 if ((ret = mbedtls_ssl_hw_record_activate(ssl, MBEDTLS_SSL_CHANNEL_OUTBOUND)) != 0) {
3514 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ssl_hw_record_activate", ret);
3515 return MBEDTLS_ERR_SSL_HW_ACCEL_FAILED;
Paul Bakker07eb38b2012-12-19 14:42:06 +01003516 }
3517 }
3518#endif
3519
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003520#if defined(MBEDTLS_SSL_PROTO_DTLS)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003521 if (ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM) {
3522 mbedtls_ssl_send_flight_completed(ssl);
3523 }
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02003524#endif
3525
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003526 if ((ret = mbedtls_ssl_write_handshake_msg(ssl)) != 0) {
3527 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ssl_write_handshake_msg", ret);
3528 return ret;
Paul Bakker5121ce52009-01-03 21:22:43 +00003529 }
3530
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02003531#if defined(MBEDTLS_SSL_PROTO_DTLS)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003532 if (ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
3533 (ret = mbedtls_ssl_flight_transmit(ssl)) != 0) {
3534 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ssl_flight_transmit", ret);
3535 return ret;
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02003536 }
3537#endif
3538
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003539 MBEDTLS_SSL_DEBUG_MSG(2, ("<= write finished"));
Paul Bakker5121ce52009-01-03 21:22:43 +00003540
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003541 return 0;
Paul Bakker5121ce52009-01-03 21:22:43 +00003542}
3543
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003544#if defined(MBEDTLS_SSL_PROTO_SSL3)
Manuel Pégourié-Gonnardca6440b2014-09-10 12:39:54 +00003545#define SSL_MAX_HASH_LEN 36
3546#else
3547#define SSL_MAX_HASH_LEN 12
3548#endif
3549
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003550int mbedtls_ssl_parse_finished(mbedtls_ssl_context *ssl)
Paul Bakker5121ce52009-01-03 21:22:43 +00003551{
Janos Follath865b3eb2019-12-16 11:46:15 +00003552 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnard879a4f92014-07-11 22:31:12 +02003553 unsigned int hash_len;
Manuel Pégourié-Gonnardca6440b2014-09-10 12:39:54 +00003554 unsigned char buf[SSL_MAX_HASH_LEN];
Paul Bakker5121ce52009-01-03 21:22:43 +00003555
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003556 MBEDTLS_SSL_DEBUG_MSG(2, ("=> parse finished"));
Paul Bakker5121ce52009-01-03 21:22:43 +00003557
Gilles Peskine622d8042021-12-13 14:38:40 +01003558 /* There is currently no ciphersuite using another length with TLS 1.2 */
3559#if defined(MBEDTLS_SSL_PROTO_SSL3)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003560 if (ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0) {
Gilles Peskine622d8042021-12-13 14:38:40 +01003561 hash_len = 36;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003562 } else
Gilles Peskine622d8042021-12-13 14:38:40 +01003563#endif
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003564 hash_len = 12;
Gilles Peskine622d8042021-12-13 14:38:40 +01003565
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003566 ssl->handshake->calc_finished(ssl, buf, ssl->conf->endpoint ^ 1);
Paul Bakker5121ce52009-01-03 21:22:43 +00003567
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003568 if ((ret = mbedtls_ssl_read_record(ssl, 1)) != 0) {
3569 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ssl_read_record", ret);
Gilles Peskinef91b2e52021-12-13 12:36:15 +01003570 goto exit;
Paul Bakker5121ce52009-01-03 21:22:43 +00003571 }
3572
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003573 if (ssl->in_msgtype != MBEDTLS_SSL_MSG_HANDSHAKE) {
3574 MBEDTLS_SSL_DEBUG_MSG(1, ("bad finished message"));
3575 mbedtls_ssl_send_alert_message(ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
3576 MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE);
Gilles Peskinef91b2e52021-12-13 12:36:15 +01003577 ret = MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE;
3578 goto exit;
Paul Bakker5121ce52009-01-03 21:22:43 +00003579 }
3580
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003581 if (ssl->in_msg[0] != MBEDTLS_SSL_HS_FINISHED ||
3582 ssl->in_hslen != mbedtls_ssl_hs_hdr_len(ssl) + hash_len) {
3583 MBEDTLS_SSL_DEBUG_MSG(1, ("bad finished message"));
3584 mbedtls_ssl_send_alert_message(ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
3585 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR);
Gilles Peskinef91b2e52021-12-13 12:36:15 +01003586 ret = MBEDTLS_ERR_SSL_BAD_HS_FINISHED;
3587 goto exit;
Paul Bakker5121ce52009-01-03 21:22:43 +00003588 }
3589
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003590 if (mbedtls_ct_memcmp(ssl->in_msg + mbedtls_ssl_hs_hdr_len(ssl),
3591 buf, hash_len) != 0) {
3592 MBEDTLS_SSL_DEBUG_MSG(1, ("bad finished message"));
3593 mbedtls_ssl_send_alert_message(ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
3594 MBEDTLS_SSL_ALERT_MSG_DECRYPT_ERROR);
Gilles Peskinef91b2e52021-12-13 12:36:15 +01003595 ret = MBEDTLS_ERR_SSL_BAD_HS_FINISHED;
3596 goto exit;
Paul Bakker5121ce52009-01-03 21:22:43 +00003597 }
3598
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003599#if defined(MBEDTLS_SSL_RENEGOTIATION)
Paul Bakker48916f92012-09-16 19:57:18 +00003600 ssl->verify_data_len = hash_len;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003601 memcpy(ssl->peer_verify_data, buf, hash_len);
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01003602#endif
Paul Bakker48916f92012-09-16 19:57:18 +00003603
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003604 if (ssl->handshake->resume != 0) {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003605#if defined(MBEDTLS_SSL_CLI_C)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003606 if (ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT) {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003607 ssl->state = MBEDTLS_SSL_CLIENT_CHANGE_CIPHER_SPEC;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003608 }
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01003609#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003610#if defined(MBEDTLS_SSL_SRV_C)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003611 if (ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER) {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003612 ssl->state = MBEDTLS_SSL_HANDSHAKE_WRAPUP;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003613 }
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01003614#endif
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003615 } else {
Paul Bakker5121ce52009-01-03 21:22:43 +00003616 ssl->state++;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003617 }
Paul Bakker5121ce52009-01-03 21:22:43 +00003618
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003619#if defined(MBEDTLS_SSL_PROTO_DTLS)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003620 if (ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM) {
3621 mbedtls_ssl_recv_flight_completed(ssl);
3622 }
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003623#endif
3624
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003625 MBEDTLS_SSL_DEBUG_MSG(2, ("<= parse finished"));
Paul Bakker5121ce52009-01-03 21:22:43 +00003626
Gilles Peskinef91b2e52021-12-13 12:36:15 +01003627exit:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003628 mbedtls_platform_zeroize(buf, hash_len);
3629 return ret;
Paul Bakker5121ce52009-01-03 21:22:43 +00003630}
3631
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003632static void ssl_handshake_params_init(mbedtls_ssl_handshake_params *handshake)
Paul Bakkeraccaffe2014-06-26 13:37:14 +02003633{
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003634 memset(handshake, 0, sizeof(mbedtls_ssl_handshake_params));
Paul Bakkeraccaffe2014-06-26 13:37:14 +02003635
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003636#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1) || \
3637 defined(MBEDTLS_SSL_PROTO_TLS1_1)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003638 mbedtls_md5_init(&handshake->fin_md5);
3639 mbedtls_sha1_init(&handshake->fin_sha1);
3640 mbedtls_md5_starts_ret(&handshake->fin_md5);
3641 mbedtls_sha1_starts_ret(&handshake->fin_sha1);
Paul Bakkeraccaffe2014-06-26 13:37:14 +02003642#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003643#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
3644#if defined(MBEDTLS_SHA256_C)
Andrzej Kurekeb342242019-01-29 09:14:33 -05003645#if defined(MBEDTLS_USE_PSA_CRYPTO)
3646 handshake->fin_sha256_psa = psa_hash_operation_init();
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003647 psa_hash_setup(&handshake->fin_sha256_psa, PSA_ALG_SHA_256);
Andrzej Kurekeb342242019-01-29 09:14:33 -05003648#else
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003649 mbedtls_sha256_init(&handshake->fin_sha256);
3650 mbedtls_sha256_starts_ret(&handshake->fin_sha256, 0);
Paul Bakkeraccaffe2014-06-26 13:37:14 +02003651#endif
Andrzej Kurekeb342242019-01-29 09:14:33 -05003652#endif
Gilles Peskined2d59372021-05-12 22:43:27 +02003653#if defined(MBEDTLS_SHA512_C) && !defined(MBEDTLS_SHA512_NO_SHA384)
Andrzej Kurekeb342242019-01-29 09:14:33 -05003654#if defined(MBEDTLS_USE_PSA_CRYPTO)
Andrzej Kurek972fba52019-01-30 03:29:12 -05003655 handshake->fin_sha384_psa = psa_hash_operation_init();
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003656 psa_hash_setup(&handshake->fin_sha384_psa, PSA_ALG_SHA_384);
Andrzej Kurekeb342242019-01-29 09:14:33 -05003657#else
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003658 mbedtls_sha512_init(&handshake->fin_sha512);
3659 mbedtls_sha512_starts_ret(&handshake->fin_sha512, 1);
Paul Bakkeraccaffe2014-06-26 13:37:14 +02003660#endif
Andrzej Kurekeb342242019-01-29 09:14:33 -05003661#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003662#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakkeraccaffe2014-06-26 13:37:14 +02003663
3664 handshake->update_checksum = ssl_update_checksum_start;
Hanno Becker7e5437a2017-04-28 17:15:26 +01003665
3666#if defined(MBEDTLS_SSL_PROTO_TLS1_2) && \
Gilles Peskineeccd8882020-03-10 12:19:08 +01003667 defined(MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003668 mbedtls_ssl_sig_hash_set_init(&handshake->hash_algs);
Hanno Becker7e5437a2017-04-28 17:15:26 +01003669#endif
Paul Bakkeraccaffe2014-06-26 13:37:14 +02003670
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003671#if defined(MBEDTLS_DHM_C)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003672 mbedtls_dhm_init(&handshake->dhm_ctx);
Paul Bakkeraccaffe2014-06-26 13:37:14 +02003673#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003674#if defined(MBEDTLS_ECDH_C)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003675 mbedtls_ecdh_init(&handshake->ecdh_ctx);
Paul Bakkeraccaffe2014-06-26 13:37:14 +02003676#endif
Manuel Pégourié-Gonnardeef142d2015-09-16 10:05:04 +02003677#if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003678 mbedtls_ecjpake_init(&handshake->ecjpake_ctx);
Manuel Pégourié-Gonnard77c06462015-09-17 13:59:49 +02003679#if defined(MBEDTLS_SSL_CLI_C)
3680 handshake->ecjpake_cache = NULL;
3681 handshake->ecjpake_cache_len = 0;
3682#endif
Manuel Pégourié-Gonnard76cfd3f2015-09-15 12:10:54 +02003683#endif
Manuel Pégourié-Gonnardcdc26ae2015-06-19 12:16:31 +02003684
Gilles Peskineeccd8882020-03-10 12:19:08 +01003685#if defined(MBEDTLS_SSL_ECP_RESTARTABLE_ENABLED)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003686 mbedtls_x509_crt_restart_init(&handshake->ecrs_ctx);
Manuel Pégourié-Gonnard862cde52017-05-17 11:56:15 +02003687#endif
3688
Manuel Pégourié-Gonnardcdc26ae2015-06-19 12:16:31 +02003689#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
3690 handshake->sni_authmode = MBEDTLS_SSL_VERIFY_UNSET;
3691#endif
Hanno Becker75173122019-02-06 16:18:31 +00003692
3693#if defined(MBEDTLS_X509_CRT_PARSE_C) && \
3694 !defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003695 mbedtls_pk_init(&handshake->peer_pubkey);
Hanno Becker75173122019-02-06 16:18:31 +00003696#endif
Paul Bakkeraccaffe2014-06-26 13:37:14 +02003697}
3698
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003699void mbedtls_ssl_transform_init(mbedtls_ssl_transform *transform)
Paul Bakkeraccaffe2014-06-26 13:37:14 +02003700{
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003701 memset(transform, 0, sizeof(mbedtls_ssl_transform));
Paul Bakker84bbeb52014-07-01 14:53:22 +02003702
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003703 mbedtls_cipher_init(&transform->cipher_ctx_enc);
3704 mbedtls_cipher_init(&transform->cipher_ctx_dec);
Paul Bakker84bbeb52014-07-01 14:53:22 +02003705
Hanno Beckerd56ed242018-01-03 15:32:51 +00003706#if defined(MBEDTLS_SSL_SOME_MODES_USE_MAC)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003707 mbedtls_md_init(&transform->md_ctx_enc);
3708 mbedtls_md_init(&transform->md_ctx_dec);
Hanno Beckerd56ed242018-01-03 15:32:51 +00003709#endif
Paul Bakkeraccaffe2014-06-26 13:37:14 +02003710}
3711
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003712void mbedtls_ssl_session_init(mbedtls_ssl_session *session)
Paul Bakkeraccaffe2014-06-26 13:37:14 +02003713{
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003714 memset(session, 0, sizeof(mbedtls_ssl_session));
Paul Bakkeraccaffe2014-06-26 13:37:14 +02003715}
3716
Manuel Pégourié-Gonnardd904d662022-06-17 10:24:00 +02003717MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003718static int ssl_handshake_init(mbedtls_ssl_context *ssl)
Paul Bakker48916f92012-09-16 19:57:18 +00003719{
Paul Bakkeraccaffe2014-06-26 13:37:14 +02003720 /* Clear old handshake information if present */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003721 if (ssl->transform_negotiate) {
3722 mbedtls_ssl_transform_free(ssl->transform_negotiate);
3723 }
3724 if (ssl->session_negotiate) {
3725 mbedtls_ssl_session_free(ssl->session_negotiate);
3726 }
3727 if (ssl->handshake) {
3728 mbedtls_ssl_handshake_free(ssl);
3729 }
Paul Bakkeraccaffe2014-06-26 13:37:14 +02003730
3731 /*
3732 * Either the pointers are now NULL or cleared properly and can be freed.
3733 * Now allocate missing structures.
3734 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003735 if (ssl->transform_negotiate == NULL) {
3736 ssl->transform_negotiate = mbedtls_calloc(1, sizeof(mbedtls_ssl_transform));
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02003737 }
Paul Bakker48916f92012-09-16 19:57:18 +00003738
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003739 if (ssl->session_negotiate == NULL) {
3740 ssl->session_negotiate = mbedtls_calloc(1, sizeof(mbedtls_ssl_session));
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02003741 }
Paul Bakker48916f92012-09-16 19:57:18 +00003742
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003743 if (ssl->handshake == NULL) {
3744 ssl->handshake = mbedtls_calloc(1, sizeof(mbedtls_ssl_handshake_params));
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02003745 }
Andrzej Kurek0afa2a12020-03-03 10:39:58 -05003746#if defined(MBEDTLS_SSL_VARIABLE_BUFFER_LENGTH)
3747 /* If the buffers are too small - reallocate */
Andrzej Kurek8ea68722020-04-03 06:40:47 -04003748
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003749 handle_buffer_resizing(ssl, 0, MBEDTLS_SSL_IN_BUFFER_LEN,
3750 MBEDTLS_SSL_OUT_BUFFER_LEN);
Andrzej Kurek0afa2a12020-03-03 10:39:58 -05003751#endif
Paul Bakker48916f92012-09-16 19:57:18 +00003752
Paul Bakkeraccaffe2014-06-26 13:37:14 +02003753 /* All pointers should exist and can be directly freed without issue */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003754 if (ssl->handshake == NULL ||
Paul Bakker48916f92012-09-16 19:57:18 +00003755 ssl->transform_negotiate == NULL ||
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003756 ssl->session_negotiate == NULL) {
3757 MBEDTLS_SSL_DEBUG_MSG(1, ("alloc() of ssl sub-contexts failed"));
Paul Bakkeraccaffe2014-06-26 13:37:14 +02003758
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003759 mbedtls_free(ssl->handshake);
3760 mbedtls_free(ssl->transform_negotiate);
3761 mbedtls_free(ssl->session_negotiate);
Paul Bakkeraccaffe2014-06-26 13:37:14 +02003762
3763 ssl->handshake = NULL;
3764 ssl->transform_negotiate = NULL;
3765 ssl->session_negotiate = NULL;
3766
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003767 return MBEDTLS_ERR_SSL_ALLOC_FAILED;
Paul Bakker48916f92012-09-16 19:57:18 +00003768 }
3769
Paul Bakkeraccaffe2014-06-26 13:37:14 +02003770 /* Initialize structures */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003771 mbedtls_ssl_session_init(ssl->session_negotiate);
3772 mbedtls_ssl_transform_init(ssl->transform_negotiate);
3773 ssl_handshake_params_init(ssl->handshake);
Paul Bakker968afaa2014-07-09 11:09:24 +02003774
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003775#if defined(MBEDTLS_SSL_PROTO_DTLS)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003776 if (ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM) {
Manuel Pégourié-Gonnard06939ce2015-05-11 11:25:46 +02003777 ssl->handshake->alt_transform_out = ssl->transform_out;
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003778
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003779 if (ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT) {
Manuel Pégourié-Gonnard06939ce2015-05-11 11:25:46 +02003780 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_PREPARING;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003781 } else {
Manuel Pégourié-Gonnard06939ce2015-05-11 11:25:46 +02003782 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_WAITING;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003783 }
Manuel Pégourié-Gonnard286a1362015-05-13 16:22:05 +02003784
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003785 mbedtls_ssl_set_timer(ssl, 0);
Manuel Pégourié-Gonnard06939ce2015-05-11 11:25:46 +02003786 }
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003787#endif
3788
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003789 return 0;
Paul Bakker48916f92012-09-16 19:57:18 +00003790}
3791
Manuel Pégourié-Gonnarde057d3b2015-05-20 10:59:43 +02003792#if defined(MBEDTLS_SSL_DTLS_HELLO_VERIFY) && defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard7d38d212014-07-23 17:52:09 +02003793/* Dummy cookie callbacks for defaults */
Manuel Pégourié-Gonnardd904d662022-06-17 10:24:00 +02003794MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003795static int ssl_cookie_write_dummy(void *ctx,
3796 unsigned char **p, unsigned char *end,
3797 const unsigned char *cli_id, size_t cli_id_len)
Manuel Pégourié-Gonnard7d38d212014-07-23 17:52:09 +02003798{
3799 ((void) ctx);
3800 ((void) p);
3801 ((void) end);
3802 ((void) cli_id);
3803 ((void) cli_id_len);
3804
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003805 return MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
Manuel Pégourié-Gonnard7d38d212014-07-23 17:52:09 +02003806}
3807
Manuel Pégourié-Gonnardd904d662022-06-17 10:24:00 +02003808MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003809static int ssl_cookie_check_dummy(void *ctx,
3810 const unsigned char *cookie, size_t cookie_len,
3811 const unsigned char *cli_id, size_t cli_id_len)
Manuel Pégourié-Gonnard7d38d212014-07-23 17:52:09 +02003812{
3813 ((void) ctx);
3814 ((void) cookie);
3815 ((void) cookie_len);
3816 ((void) cli_id);
3817 ((void) cli_id_len);
3818
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003819 return MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
Manuel Pégourié-Gonnard7d38d212014-07-23 17:52:09 +02003820}
Manuel Pégourié-Gonnarde057d3b2015-05-20 10:59:43 +02003821#endif /* MBEDTLS_SSL_DTLS_HELLO_VERIFY && MBEDTLS_SSL_SRV_C */
Manuel Pégourié-Gonnard7d38d212014-07-23 17:52:09 +02003822
Paul Bakker5121ce52009-01-03 21:22:43 +00003823/*
3824 * Initialize an SSL context
3825 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003826void mbedtls_ssl_init(mbedtls_ssl_context *ssl)
Manuel Pégourié-Gonnard41d479e2015-04-29 00:48:22 +02003827{
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003828 memset(ssl, 0, sizeof(mbedtls_ssl_context));
Manuel Pégourié-Gonnard41d479e2015-04-29 00:48:22 +02003829}
3830
3831/*
3832 * Setup an SSL context
3833 */
Hanno Becker2a43f6f2018-08-10 11:12:52 +01003834
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003835int mbedtls_ssl_setup(mbedtls_ssl_context *ssl,
3836 const mbedtls_ssl_config *conf)
Paul Bakker5121ce52009-01-03 21:22:43 +00003837{
Janos Follath865b3eb2019-12-16 11:46:15 +00003838 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Darryl Greenb33cc762019-11-28 14:29:44 +00003839 size_t in_buf_len = MBEDTLS_SSL_IN_BUFFER_LEN;
3840 size_t out_buf_len = MBEDTLS_SSL_OUT_BUFFER_LEN;
Paul Bakker5121ce52009-01-03 21:22:43 +00003841
Manuel Pégourié-Gonnarddef0bbe2015-05-04 14:56:36 +02003842 ssl->conf = conf;
Paul Bakker62f2dee2012-09-28 07:31:51 +00003843
3844 /*
Manuel Pégourié-Gonnard06193482014-02-14 08:39:32 +01003845 * Prepare base structures
Paul Bakker62f2dee2012-09-28 07:31:51 +00003846 */
k-stachowiakc9a5f022018-07-24 13:53:31 +02003847
3848 /* Set to NULL in case of an error condition */
3849 ssl->out_buf = NULL;
k-stachowiaka47911c2018-07-04 17:41:58 +02003850
Darryl Greenb33cc762019-11-28 14:29:44 +00003851#if defined(MBEDTLS_SSL_VARIABLE_BUFFER_LENGTH)
3852 ssl->in_buf_len = in_buf_len;
3853#endif
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003854 ssl->in_buf = mbedtls_calloc(1, in_buf_len);
3855 if (ssl->in_buf == NULL) {
3856 MBEDTLS_SSL_DEBUG_MSG(1, ("alloc(%" MBEDTLS_PRINTF_SIZET " bytes) failed", in_buf_len));
k-stachowiak9f7798e2018-07-31 16:52:32 +02003857 ret = MBEDTLS_ERR_SSL_ALLOC_FAILED;
k-stachowiaka47911c2018-07-04 17:41:58 +02003858 goto error;
Angus Grattond8213d02016-05-25 20:56:48 +10003859 }
3860
Darryl Greenb33cc762019-11-28 14:29:44 +00003861#if defined(MBEDTLS_SSL_VARIABLE_BUFFER_LENGTH)
3862 ssl->out_buf_len = out_buf_len;
3863#endif
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003864 ssl->out_buf = mbedtls_calloc(1, out_buf_len);
3865 if (ssl->out_buf == NULL) {
3866 MBEDTLS_SSL_DEBUG_MSG(1, ("alloc(%" MBEDTLS_PRINTF_SIZET " bytes) failed", out_buf_len));
k-stachowiak9f7798e2018-07-31 16:52:32 +02003867 ret = MBEDTLS_ERR_SSL_ALLOC_FAILED;
k-stachowiaka47911c2018-07-04 17:41:58 +02003868 goto error;
Paul Bakker5121ce52009-01-03 21:22:43 +00003869 }
3870
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003871 mbedtls_ssl_reset_in_out_pointers(ssl);
Manuel Pégourié-Gonnard419d5ae2015-05-04 19:32:36 +02003872
Johan Pascalb62bb512015-12-03 21:56:45 +01003873#if defined(MBEDTLS_SSL_DTLS_SRTP)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003874 memset(&ssl->dtls_srtp_info, 0, sizeof(ssl->dtls_srtp_info));
Johan Pascalb62bb512015-12-03 21:56:45 +01003875#endif
3876
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003877 if ((ret = ssl_handshake_init(ssl)) != 0) {
k-stachowiaka47911c2018-07-04 17:41:58 +02003878 goto error;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003879 }
Paul Bakker5121ce52009-01-03 21:22:43 +00003880
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003881 return 0;
k-stachowiaka47911c2018-07-04 17:41:58 +02003882
3883error:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003884 mbedtls_free(ssl->in_buf);
3885 mbedtls_free(ssl->out_buf);
k-stachowiaka47911c2018-07-04 17:41:58 +02003886
3887 ssl->conf = NULL;
3888
Darryl Greenb33cc762019-11-28 14:29:44 +00003889#if defined(MBEDTLS_SSL_VARIABLE_BUFFER_LENGTH)
3890 ssl->in_buf_len = 0;
3891 ssl->out_buf_len = 0;
3892#endif
k-stachowiaka47911c2018-07-04 17:41:58 +02003893 ssl->in_buf = NULL;
3894 ssl->out_buf = NULL;
3895
3896 ssl->in_hdr = NULL;
3897 ssl->in_ctr = NULL;
3898 ssl->in_len = NULL;
3899 ssl->in_iv = NULL;
3900 ssl->in_msg = NULL;
3901
3902 ssl->out_hdr = NULL;
3903 ssl->out_ctr = NULL;
3904 ssl->out_len = NULL;
3905 ssl->out_iv = NULL;
3906 ssl->out_msg = NULL;
3907
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003908 return ret;
Paul Bakker5121ce52009-01-03 21:22:43 +00003909}
3910
3911/*
Paul Bakker7eb013f2011-10-06 12:37:39 +00003912 * Reset an initialized and used SSL context for re-use while retaining
3913 * all application-set variables, function pointers and data.
Manuel Pégourié-Gonnard3f09b6d2015-09-08 11:58:14 +02003914 *
3915 * If partial is non-zero, keep data in the input buffer and client ID.
3916 * (Use when a DTLS client reconnects from the same port.)
Paul Bakker7eb013f2011-10-06 12:37:39 +00003917 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003918int mbedtls_ssl_session_reset_int(mbedtls_ssl_context *ssl, int partial)
Paul Bakker7eb013f2011-10-06 12:37:39 +00003919{
Janos Follath865b3eb2019-12-16 11:46:15 +00003920 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Darryl Greenb33cc762019-11-28 14:29:44 +00003921#if defined(MBEDTLS_SSL_VARIABLE_BUFFER_LENGTH)
3922 size_t in_buf_len = ssl->in_buf_len;
3923 size_t out_buf_len = ssl->out_buf_len;
3924#else
3925 size_t in_buf_len = MBEDTLS_SSL_IN_BUFFER_LEN;
3926 size_t out_buf_len = MBEDTLS_SSL_OUT_BUFFER_LEN;
3927#endif
Paul Bakker48916f92012-09-16 19:57:18 +00003928
Hanno Becker7e772132018-08-10 12:38:21 +01003929#if !defined(MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE) || \
3930 !defined(MBEDTLS_SSL_SRV_C)
3931 ((void) partial);
3932#endif
3933
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003934 ssl->state = MBEDTLS_SSL_HELLO_REQUEST;
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01003935
Manuel Pégourié-Gonnard286a1362015-05-13 16:22:05 +02003936 /* Cancel any possibly running timer */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003937 mbedtls_ssl_set_timer(ssl, 0);
Manuel Pégourié-Gonnard286a1362015-05-13 16:22:05 +02003938
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003939#if defined(MBEDTLS_SSL_RENEGOTIATION)
3940 ssl->renego_status = MBEDTLS_SSL_INITIAL_HANDSHAKE;
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01003941 ssl->renego_records_seen = 0;
Paul Bakker48916f92012-09-16 19:57:18 +00003942
3943 ssl->verify_data_len = 0;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003944 memset(ssl->own_verify_data, 0, MBEDTLS_SSL_VERIFY_DATA_MAX_LEN);
3945 memset(ssl->peer_verify_data, 0, MBEDTLS_SSL_VERIFY_DATA_MAX_LEN);
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01003946#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003947 ssl->secure_renegotiation = MBEDTLS_SSL_LEGACY_RENEGOTIATION;
Paul Bakker48916f92012-09-16 19:57:18 +00003948
Paul Bakker7eb013f2011-10-06 12:37:39 +00003949 ssl->in_offt = NULL;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003950 mbedtls_ssl_reset_in_out_pointers(ssl);
Paul Bakker7eb013f2011-10-06 12:37:39 +00003951
3952 ssl->in_msgtype = 0;
3953 ssl->in_msglen = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003954#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02003955 ssl->next_record_offset = 0;
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02003956 ssl->in_epoch = 0;
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02003957#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003958#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003959 mbedtls_ssl_dtls_replay_reset(ssl);
Manuel Pégourié-Gonnardb47368a2014-09-24 13:29:58 +02003960#endif
Paul Bakker7eb013f2011-10-06 12:37:39 +00003961
3962 ssl->in_hslen = 0;
3963 ssl->nb_zero = 0;
Hanno Beckeraf0665d2017-05-24 09:16:26 +01003964
3965 ssl->keep_current_message = 0;
Paul Bakker7eb013f2011-10-06 12:37:39 +00003966
3967 ssl->out_msgtype = 0;
3968 ssl->out_msglen = 0;
3969 ssl->out_left = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003970#if defined(MBEDTLS_SSL_CBC_RECORD_SPLITTING)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003971 if (ssl->split_done != MBEDTLS_SSL_CBC_RECORD_SPLITTING_DISABLED) {
Manuel Pégourié-Gonnardcfa477e2015-01-07 14:50:54 +01003972 ssl->split_done = 0;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003973 }
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +01003974#endif
Paul Bakker7eb013f2011-10-06 12:37:39 +00003975
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003976 memset(ssl->cur_out_ctr, 0, sizeof(ssl->cur_out_ctr));
Hanno Becker19859472018-08-06 09:40:20 +01003977
Paul Bakker48916f92012-09-16 19:57:18 +00003978 ssl->transform_in = NULL;
3979 ssl->transform_out = NULL;
Paul Bakker7eb013f2011-10-06 12:37:39 +00003980
Hanno Becker78640902018-08-13 16:35:15 +01003981 ssl->session_in = NULL;
3982 ssl->session_out = NULL;
3983
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003984 memset(ssl->out_buf, 0, out_buf_len);
Hanno Becker4ccbf062018-08-10 11:20:38 +01003985
David Horstmannd4f22082022-10-26 18:25:14 +01003986 int clear_in_buf = 1;
Hanno Becker4ccbf062018-08-10 11:20:38 +01003987#if defined(MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE) && defined(MBEDTLS_SSL_SRV_C)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003988 if (partial != 0) {
David Horstmannd4f22082022-10-26 18:25:14 +01003989 clear_in_buf = 0;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003990 }
Hanno Becker4ccbf062018-08-10 11:20:38 +01003991#endif /* MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE && MBEDTLS_SSL_SRV_C */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003992 if (clear_in_buf) {
Hanno Becker4ccbf062018-08-10 11:20:38 +01003993 ssl->in_left = 0;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003994 memset(ssl->in_buf, 0, in_buf_len);
Hanno Becker4ccbf062018-08-10 11:20:38 +01003995 }
Paul Bakker05ef8352012-05-08 09:17:57 +00003996
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003997#if defined(MBEDTLS_SSL_HW_RECORD_ACCEL)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003998 if (mbedtls_ssl_hw_record_reset != NULL) {
3999 MBEDTLS_SSL_DEBUG_MSG(2, ("going for mbedtls_ssl_hw_record_reset()"));
4000 if ((ret = mbedtls_ssl_hw_record_reset(ssl)) != 0) {
4001 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ssl_hw_record_reset", ret);
4002 return MBEDTLS_ERR_SSL_HW_ACCEL_FAILED;
Paul Bakker2770fbd2012-07-03 13:30:23 +00004003 }
Paul Bakker05ef8352012-05-08 09:17:57 +00004004 }
4005#endif
Paul Bakker2770fbd2012-07-03 13:30:23 +00004006
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004007 if (ssl->transform) {
4008 mbedtls_ssl_transform_free(ssl->transform);
4009 mbedtls_free(ssl->transform);
Paul Bakker48916f92012-09-16 19:57:18 +00004010 ssl->transform = NULL;
Paul Bakker2770fbd2012-07-03 13:30:23 +00004011 }
Paul Bakker48916f92012-09-16 19:57:18 +00004012
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004013 if (ssl->session) {
4014 mbedtls_ssl_session_free(ssl->session);
4015 mbedtls_free(ssl->session);
Paul Bakkerc0463502013-02-14 11:19:38 +01004016 ssl->session = NULL;
4017 }
4018
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004019#if defined(MBEDTLS_SSL_ALPN)
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02004020 ssl->alpn_chosen = NULL;
4021#endif
4022
Manuel Pégourié-Gonnarde057d3b2015-05-20 10:59:43 +02004023#if defined(MBEDTLS_SSL_DTLS_HELLO_VERIFY) && defined(MBEDTLS_SSL_SRV_C)
David Horstmannb5b1ed22022-10-27 13:21:49 +01004024 int free_cli_id = 1;
Hanno Becker4ccbf062018-08-10 11:20:38 +01004025#if defined(MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004026 if (partial != 0) {
David Horstmannd4f22082022-10-26 18:25:14 +01004027 free_cli_id = 0;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004028 }
Hanno Becker4ccbf062018-08-10 11:20:38 +01004029#endif
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004030 if (free_cli_id) {
4031 mbedtls_free(ssl->cli_id);
Manuel Pégourié-Gonnard3f09b6d2015-09-08 11:58:14 +02004032 ssl->cli_id = NULL;
4033 ssl->cli_id_len = 0;
4034 }
Manuel Pégourié-Gonnard43c02182014-07-22 17:32:01 +02004035#endif
4036
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004037 if ((ret = ssl_handshake_init(ssl)) != 0) {
4038 return ret;
4039 }
Paul Bakker2770fbd2012-07-03 13:30:23 +00004040
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004041 return 0;
Paul Bakker7eb013f2011-10-06 12:37:39 +00004042}
4043
Manuel Pégourié-Gonnard779e4292013-08-03 13:50:48 +02004044/*
Manuel Pégourié-Gonnard3f09b6d2015-09-08 11:58:14 +02004045 * Reset an initialized and used SSL context for re-use while retaining
4046 * all application-set variables, function pointers and data.
4047 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004048int mbedtls_ssl_session_reset(mbedtls_ssl_context *ssl)
Manuel Pégourié-Gonnard3f09b6d2015-09-08 11:58:14 +02004049{
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004050 return mbedtls_ssl_session_reset_int(ssl, 0);
Manuel Pégourié-Gonnard3f09b6d2015-09-08 11:58:14 +02004051}
4052
4053/*
Paul Bakker5121ce52009-01-03 21:22:43 +00004054 * SSL set accessors
4055 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004056void mbedtls_ssl_conf_endpoint(mbedtls_ssl_config *conf, int endpoint)
Paul Bakker5121ce52009-01-03 21:22:43 +00004057{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02004058 conf->endpoint = endpoint;
Paul Bakker5121ce52009-01-03 21:22:43 +00004059}
4060
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004061void mbedtls_ssl_conf_transport(mbedtls_ssl_config *conf, int transport)
Manuel Pégourié-Gonnard0b1ff292014-02-06 13:04:16 +01004062{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02004063 conf->transport = transport;
Manuel Pégourié-Gonnard0b1ff292014-02-06 13:04:16 +01004064}
4065
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004066#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004067void mbedtls_ssl_conf_dtls_anti_replay(mbedtls_ssl_config *conf, char mode)
Manuel Pégourié-Gonnard27393132014-09-24 14:41:11 +02004068{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02004069 conf->anti_replay = mode;
Manuel Pégourié-Gonnard27393132014-09-24 14:41:11 +02004070}
4071#endif
4072
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004073#if defined(MBEDTLS_SSL_DTLS_BADMAC_LIMIT)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004074void mbedtls_ssl_conf_dtls_badmac_limit(mbedtls_ssl_config *conf, unsigned limit)
Manuel Pégourié-Gonnardb0643d12014-10-14 18:30:36 +02004075{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02004076 conf->badmac_limit = limit;
Manuel Pégourié-Gonnardb0643d12014-10-14 18:30:36 +02004077}
4078#endif
4079
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004080#if defined(MBEDTLS_SSL_PROTO_DTLS)
Hanno Becker04da1892018-08-14 13:22:10 +01004081
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004082void mbedtls_ssl_set_datagram_packing(mbedtls_ssl_context *ssl,
4083 unsigned allow_packing)
Hanno Becker04da1892018-08-14 13:22:10 +01004084{
4085 ssl->disable_datagram_packing = !allow_packing;
4086}
4087
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004088void mbedtls_ssl_conf_handshake_timeout(mbedtls_ssl_config *conf,
4089 uint32_t min, uint32_t max)
Manuel Pégourié-Gonnard905dd242014-10-01 12:03:55 +02004090{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02004091 conf->hs_timeout_min = min;
4092 conf->hs_timeout_max = max;
Manuel Pégourié-Gonnard905dd242014-10-01 12:03:55 +02004093}
4094#endif
4095
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004096void mbedtls_ssl_conf_authmode(mbedtls_ssl_config *conf, int authmode)
Paul Bakker5121ce52009-01-03 21:22:43 +00004097{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02004098 conf->authmode = authmode;
Paul Bakker5121ce52009-01-03 21:22:43 +00004099}
4100
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004101#if defined(MBEDTLS_X509_CRT_PARSE_C)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004102void mbedtls_ssl_conf_verify(mbedtls_ssl_config *conf,
4103 int (*f_vrfy)(void *, mbedtls_x509_crt *, int, uint32_t *),
4104 void *p_vrfy)
Paul Bakkerb63b0af2011-01-13 17:54:59 +00004105{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02004106 conf->f_vrfy = f_vrfy;
4107 conf->p_vrfy = p_vrfy;
Paul Bakkerb63b0af2011-01-13 17:54:59 +00004108}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004109#endif /* MBEDTLS_X509_CRT_PARSE_C */
Paul Bakkerb63b0af2011-01-13 17:54:59 +00004110
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004111void mbedtls_ssl_conf_rng(mbedtls_ssl_config *conf,
4112 int (*f_rng)(void *, unsigned char *, size_t),
4113 void *p_rng)
Paul Bakker5121ce52009-01-03 21:22:43 +00004114{
Manuel Pégourié-Gonnard750e4d72015-05-07 12:35:38 +01004115 conf->f_rng = f_rng;
4116 conf->p_rng = p_rng;
Paul Bakker5121ce52009-01-03 21:22:43 +00004117}
4118
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004119void mbedtls_ssl_conf_dbg(mbedtls_ssl_config *conf,
4120 void (*f_dbg)(void *, int, const char *, int, const char *),
4121 void *p_dbg)
Paul Bakker5121ce52009-01-03 21:22:43 +00004122{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02004123 conf->f_dbg = f_dbg;
4124 conf->p_dbg = p_dbg;
Paul Bakker5121ce52009-01-03 21:22:43 +00004125}
4126
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004127void mbedtls_ssl_set_bio(mbedtls_ssl_context *ssl,
4128 void *p_bio,
4129 mbedtls_ssl_send_t *f_send,
4130 mbedtls_ssl_recv_t *f_recv,
4131 mbedtls_ssl_recv_timeout_t *f_recv_timeout)
Manuel Pégourié-Gonnard8fa6dfd2014-09-17 10:47:43 +02004132{
4133 ssl->p_bio = p_bio;
4134 ssl->f_send = f_send;
4135 ssl->f_recv = f_recv;
4136 ssl->f_recv_timeout = f_recv_timeout;
Manuel Pégourié-Gonnard97fd52c2015-05-06 15:38:52 +01004137}
4138
Manuel Pégourié-Gonnard6e7aaca2018-08-20 10:37:23 +02004139#if defined(MBEDTLS_SSL_PROTO_DTLS)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004140void mbedtls_ssl_set_mtu(mbedtls_ssl_context *ssl, uint16_t mtu)
Manuel Pégourié-Gonnard6e7aaca2018-08-20 10:37:23 +02004141{
4142 ssl->mtu = mtu;
4143}
4144#endif
4145
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004146void mbedtls_ssl_conf_read_timeout(mbedtls_ssl_config *conf, uint32_t timeout)
Manuel Pégourié-Gonnard97fd52c2015-05-06 15:38:52 +01004147{
4148 conf->read_timeout = timeout;
Manuel Pégourié-Gonnard8fa6dfd2014-09-17 10:47:43 +02004149}
4150
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004151void mbedtls_ssl_set_timer_cb(mbedtls_ssl_context *ssl,
4152 void *p_timer,
4153 mbedtls_ssl_set_timer_t *f_set_timer,
4154 mbedtls_ssl_get_timer_t *f_get_timer)
Manuel Pégourié-Gonnard2e012912015-05-12 20:55:41 +02004155{
4156 ssl->p_timer = p_timer;
4157 ssl->f_set_timer = f_set_timer;
4158 ssl->f_get_timer = f_get_timer;
Manuel Pégourié-Gonnard286a1362015-05-13 16:22:05 +02004159
4160 /* Make sure we start with no timer running */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004161 mbedtls_ssl_set_timer(ssl, 0);
Manuel Pégourié-Gonnard2e012912015-05-12 20:55:41 +02004162}
4163
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004164#if defined(MBEDTLS_SSL_SRV_C)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004165void mbedtls_ssl_conf_session_cache(mbedtls_ssl_config *conf,
4166 void *p_cache,
4167 int (*f_get_cache)(void *, mbedtls_ssl_session *),
4168 int (*f_set_cache)(void *, const mbedtls_ssl_session *))
Paul Bakker5121ce52009-01-03 21:22:43 +00004169{
Manuel Pégourié-Gonnard5cb33082015-05-06 18:06:26 +01004170 conf->p_cache = p_cache;
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02004171 conf->f_get_cache = f_get_cache;
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02004172 conf->f_set_cache = f_set_cache;
Paul Bakker5121ce52009-01-03 21:22:43 +00004173}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004174#endif /* MBEDTLS_SSL_SRV_C */
Paul Bakker5121ce52009-01-03 21:22:43 +00004175
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004176#if defined(MBEDTLS_SSL_CLI_C)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004177int mbedtls_ssl_set_session(mbedtls_ssl_context *ssl, const mbedtls_ssl_session *session)
Paul Bakker5121ce52009-01-03 21:22:43 +00004178{
Janos Follath865b3eb2019-12-16 11:46:15 +00004179 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +02004180
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004181 if (ssl == NULL ||
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +02004182 session == NULL ||
4183 ssl->session_negotiate == NULL ||
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004184 ssl->conf->endpoint != MBEDTLS_SSL_IS_CLIENT) {
4185 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +02004186 }
4187
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004188 if ((ret = mbedtls_ssl_session_copy(ssl->session_negotiate,
4189 session)) != 0) {
4190 return ret;
4191 }
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +02004192
Paul Bakker0a597072012-09-25 21:55:46 +00004193 ssl->handshake->resume = 1;
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +02004194
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004195 return 0;
Paul Bakker5121ce52009-01-03 21:22:43 +00004196}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004197#endif /* MBEDTLS_SSL_CLI_C */
Paul Bakker5121ce52009-01-03 21:22:43 +00004198
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004199void mbedtls_ssl_conf_ciphersuites(mbedtls_ssl_config *conf,
4200 const int *ciphersuites)
Paul Bakker5121ce52009-01-03 21:22:43 +00004201{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02004202 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_0] = ciphersuites;
4203 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_1] = ciphersuites;
4204 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_2] = ciphersuites;
4205 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_3] = ciphersuites;
Paul Bakker8f4ddae2013-04-15 15:09:54 +02004206}
4207
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004208void mbedtls_ssl_conf_ciphersuites_for_version(mbedtls_ssl_config *conf,
4209 const int *ciphersuites,
4210 int major, int minor)
Paul Bakker8f4ddae2013-04-15 15:09:54 +02004211{
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004212 if (major != MBEDTLS_SSL_MAJOR_VERSION_3) {
Paul Bakker8f4ddae2013-04-15 15:09:54 +02004213 return;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004214 }
Paul Bakker8f4ddae2013-04-15 15:09:54 +02004215
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004216 if (minor < MBEDTLS_SSL_MINOR_VERSION_0 || minor > MBEDTLS_SSL_MINOR_VERSION_3) {
Paul Bakker8f4ddae2013-04-15 15:09:54 +02004217 return;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004218 }
Paul Bakker8f4ddae2013-04-15 15:09:54 +02004219
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02004220 conf->ciphersuite_list[minor] = ciphersuites;
Paul Bakker5121ce52009-01-03 21:22:43 +00004221}
4222
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004223#if defined(MBEDTLS_X509_CRT_PARSE_C)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004224void mbedtls_ssl_conf_cert_profile(mbedtls_ssl_config *conf,
4225 const mbedtls_x509_crt_profile *profile)
Manuel Pégourié-Gonnard6e3ee3a2015-06-17 10:58:20 +02004226{
4227 conf->cert_profile = profile;
4228}
4229
Manuel Pégourié-Gonnard8f618a82015-05-10 21:13:36 +02004230/* Append a new keycert entry to a (possibly empty) list */
Manuel Pégourié-Gonnardd904d662022-06-17 10:24:00 +02004231MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004232static int ssl_append_key_cert(mbedtls_ssl_key_cert **head,
4233 mbedtls_x509_crt *cert,
4234 mbedtls_pk_context *key)
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02004235{
niisato8ee24222018-06-25 19:05:48 +09004236 mbedtls_ssl_key_cert *new_cert;
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02004237
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004238 new_cert = mbedtls_calloc(1, sizeof(mbedtls_ssl_key_cert));
4239 if (new_cert == NULL) {
4240 return MBEDTLS_ERR_SSL_ALLOC_FAILED;
4241 }
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02004242
niisato8ee24222018-06-25 19:05:48 +09004243 new_cert->cert = cert;
4244 new_cert->key = key;
4245 new_cert->next = NULL;
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02004246
Manuel Pégourié-Gonnard8f618a82015-05-10 21:13:36 +02004247 /* Update head is the list was null, else add to the end */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004248 if (*head == NULL) {
niisato8ee24222018-06-25 19:05:48 +09004249 *head = new_cert;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004250 } else {
Manuel Pégourié-Gonnard8f618a82015-05-10 21:13:36 +02004251 mbedtls_ssl_key_cert *cur = *head;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004252 while (cur->next != NULL) {
Manuel Pégourié-Gonnard8f618a82015-05-10 21:13:36 +02004253 cur = cur->next;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004254 }
niisato8ee24222018-06-25 19:05:48 +09004255 cur->next = new_cert;
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02004256 }
4257
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004258 return 0;
Manuel Pégourié-Gonnard8f618a82015-05-10 21:13:36 +02004259}
4260
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004261int mbedtls_ssl_conf_own_cert(mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnard8f618a82015-05-10 21:13:36 +02004262 mbedtls_x509_crt *own_cert,
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004263 mbedtls_pk_context *pk_key)
Manuel Pégourié-Gonnard8f618a82015-05-10 21:13:36 +02004264{
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004265 return ssl_append_key_cert(&conf->key_cert, own_cert, pk_key);
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02004266}
4267
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004268void mbedtls_ssl_conf_ca_chain(mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnardbc2b7712015-05-06 11:14:19 +01004269 mbedtls_x509_crt *ca_chain,
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004270 mbedtls_x509_crl *ca_crl)
Paul Bakker5121ce52009-01-03 21:22:43 +00004271{
Manuel Pégourié-Gonnardbc2b7712015-05-06 11:14:19 +01004272 conf->ca_chain = ca_chain;
4273 conf->ca_crl = ca_crl;
Hanno Becker5adaad92019-03-27 16:54:37 +00004274
4275#if defined(MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK)
4276 /* mbedtls_ssl_conf_ca_chain() and mbedtls_ssl_conf_ca_cb()
4277 * cannot be used together. */
4278 conf->f_ca_cb = NULL;
4279 conf->p_ca_cb = NULL;
4280#endif /* MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK */
Paul Bakker5121ce52009-01-03 21:22:43 +00004281}
Hanno Becker5adaad92019-03-27 16:54:37 +00004282
4283#if defined(MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004284void mbedtls_ssl_conf_ca_cb(mbedtls_ssl_config *conf,
4285 mbedtls_x509_crt_ca_cb_t f_ca_cb,
4286 void *p_ca_cb)
Hanno Becker5adaad92019-03-27 16:54:37 +00004287{
4288 conf->f_ca_cb = f_ca_cb;
4289 conf->p_ca_cb = p_ca_cb;
4290
4291 /* mbedtls_ssl_conf_ca_chain() and mbedtls_ssl_conf_ca_cb()
4292 * cannot be used together. */
4293 conf->ca_chain = NULL;
4294 conf->ca_crl = NULL;
4295}
4296#endif /* MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004297#endif /* MBEDTLS_X509_CRT_PARSE_C */
Paul Bakkereb2c6582012-09-27 19:15:01 +00004298
Manuel Pégourié-Gonnard1af6c852015-05-10 23:10:37 +02004299#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004300int mbedtls_ssl_set_hs_own_cert(mbedtls_ssl_context *ssl,
4301 mbedtls_x509_crt *own_cert,
4302 mbedtls_pk_context *pk_key)
Manuel Pégourié-Gonnard1af6c852015-05-10 23:10:37 +02004303{
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004304 return ssl_append_key_cert(&ssl->handshake->sni_key_cert,
4305 own_cert, pk_key);
Manuel Pégourié-Gonnard1af6c852015-05-10 23:10:37 +02004306}
Manuel Pégourié-Gonnard22bfa4b2015-05-11 08:46:37 +02004307
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004308void mbedtls_ssl_set_hs_ca_chain(mbedtls_ssl_context *ssl,
4309 mbedtls_x509_crt *ca_chain,
4310 mbedtls_x509_crl *ca_crl)
Manuel Pégourié-Gonnard22bfa4b2015-05-11 08:46:37 +02004311{
4312 ssl->handshake->sni_ca_chain = ca_chain;
4313 ssl->handshake->sni_ca_crl = ca_crl;
4314}
Manuel Pégourié-Gonnardcdc26ae2015-06-19 12:16:31 +02004315
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004316void mbedtls_ssl_set_hs_authmode(mbedtls_ssl_context *ssl,
4317 int authmode)
Manuel Pégourié-Gonnardcdc26ae2015-06-19 12:16:31 +02004318{
4319 ssl->handshake->sni_authmode = authmode;
4320}
Manuel Pégourié-Gonnard1af6c852015-05-10 23:10:37 +02004321#endif /* MBEDTLS_SSL_SERVER_NAME_INDICATION */
4322
Hanno Becker8927c832019-04-03 12:52:50 +01004323#if defined(MBEDTLS_X509_CRT_PARSE_C)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004324void mbedtls_ssl_set_verify(mbedtls_ssl_context *ssl,
4325 int (*f_vrfy)(void *, mbedtls_x509_crt *, int, uint32_t *),
4326 void *p_vrfy)
Hanno Becker8927c832019-04-03 12:52:50 +01004327{
4328 ssl->f_vrfy = f_vrfy;
4329 ssl->p_vrfy = p_vrfy;
4330}
4331#endif
4332
Manuel Pégourié-Gonnardeef142d2015-09-16 10:05:04 +02004333#if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
Manuel Pégourié-Gonnard7002f4a2015-09-15 12:43:43 +02004334/*
4335 * Set EC J-PAKE password for current handshake
4336 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004337int mbedtls_ssl_set_hs_ecjpake_password(mbedtls_ssl_context *ssl,
4338 const unsigned char *pw,
4339 size_t pw_len)
Manuel Pégourié-Gonnard7002f4a2015-09-15 12:43:43 +02004340{
4341 mbedtls_ecjpake_role role;
4342
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004343 if (ssl->handshake == NULL || ssl->conf == NULL) {
4344 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
4345 }
Manuel Pégourié-Gonnard7002f4a2015-09-15 12:43:43 +02004346
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004347 if (ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER) {
Manuel Pégourié-Gonnard7002f4a2015-09-15 12:43:43 +02004348 role = MBEDTLS_ECJPAKE_SERVER;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004349 } else {
Manuel Pégourié-Gonnard7002f4a2015-09-15 12:43:43 +02004350 role = MBEDTLS_ECJPAKE_CLIENT;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004351 }
Manuel Pégourié-Gonnard7002f4a2015-09-15 12:43:43 +02004352
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004353 return mbedtls_ecjpake_setup(&ssl->handshake->ecjpake_ctx,
4354 role,
4355 MBEDTLS_MD_SHA256,
4356 MBEDTLS_ECP_DP_SECP256R1,
4357 pw, pw_len);
Manuel Pégourié-Gonnard7002f4a2015-09-15 12:43:43 +02004358}
Manuel Pégourié-Gonnardeef142d2015-09-16 10:05:04 +02004359#endif /* MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED */
Manuel Pégourié-Gonnard7002f4a2015-09-15 12:43:43 +02004360
Gilles Peskineeccd8882020-03-10 12:19:08 +01004361#if defined(MBEDTLS_KEY_EXCHANGE_SOME_PSK_ENABLED)
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01004362
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004363static void ssl_conf_remove_psk(mbedtls_ssl_config *conf)
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01004364{
4365 /* Remove reference to existing PSK, if any. */
4366#if defined(MBEDTLS_USE_PSA_CRYPTO)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004367 if (!mbedtls_svc_key_id_is_null(conf->psk_opaque)) {
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01004368 /* The maintenance of the PSK key slot is the
4369 * user's responsibility. */
Ronald Croncf56a0a2020-08-04 09:51:30 +02004370 conf->psk_opaque = MBEDTLS_SVC_KEY_ID_INIT;
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01004371 }
Hanno Beckera63ac3f2018-11-05 12:47:16 +00004372 /* This and the following branch should never
Tom Cosgrove49f99bc2022-12-04 16:44:21 +00004373 * be taken simultaneously as we maintain the
Hanno Beckera63ac3f2018-11-05 12:47:16 +00004374 * invariant that raw and opaque PSKs are never
4375 * configured simultaneously. As a safeguard,
4376 * though, `else` is omitted here. */
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01004377#endif /* MBEDTLS_USE_PSA_CRYPTO */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004378 if (conf->psk != NULL) {
4379 mbedtls_platform_zeroize(conf->psk, conf->psk_len);
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01004380
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004381 mbedtls_free(conf->psk);
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01004382 conf->psk = NULL;
4383 conf->psk_len = 0;
4384 }
4385
4386 /* Remove reference to PSK identity, if any. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004387 if (conf->psk_identity != NULL) {
4388 mbedtls_free(conf->psk_identity);
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01004389 conf->psk_identity = NULL;
4390 conf->psk_identity_len = 0;
4391 }
4392}
4393
Hanno Becker7390c712018-11-15 13:33:04 +00004394/* This function assumes that PSK identity in the SSL config is unset.
4395 * It checks that the provided identity is well-formed and attempts
4396 * to make a copy of it in the SSL config.
4397 * On failure, the PSK identity in the config remains unset. */
Manuel Pégourié-Gonnardd904d662022-06-17 10:24:00 +02004398MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004399static int ssl_conf_set_psk_identity(mbedtls_ssl_config *conf,
4400 unsigned char const *psk_identity,
4401 size_t psk_identity_len)
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02004402{
Manuel Pégourié-Gonnardc6b5d832015-08-27 16:37:35 +02004403 /* Identity len will be encoded on two bytes */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004404 if (psk_identity == NULL ||
4405 (psk_identity_len >> 16) != 0 ||
4406 psk_identity_len > MBEDTLS_SSL_OUT_CONTENT_LEN) {
4407 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
Manuel Pégourié-Gonnardc6b5d832015-08-27 16:37:35 +02004408 }
4409
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004410 conf->psk_identity = mbedtls_calloc(1, psk_identity_len);
4411 if (conf->psk_identity == NULL) {
4412 return MBEDTLS_ERR_SSL_ALLOC_FAILED;
4413 }
Paul Bakker6db455e2013-09-18 17:29:31 +02004414
Manuel Pégourié-Gonnard120fdbd2015-05-07 17:07:50 +01004415 conf->psk_identity_len = psk_identity_len;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004416 memcpy(conf->psk_identity, psk_identity, conf->psk_identity_len);
Paul Bakker5ad403f2013-09-18 21:21:30 +02004417
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004418 return 0;
Paul Bakker6db455e2013-09-18 17:29:31 +02004419}
4420
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004421int mbedtls_ssl_conf_psk(mbedtls_ssl_config *conf,
4422 const unsigned char *psk, size_t psk_len,
4423 const unsigned char *psk_identity, size_t psk_identity_len)
Hanno Becker7390c712018-11-15 13:33:04 +00004424{
Janos Follath865b3eb2019-12-16 11:46:15 +00004425 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Hanno Becker7390c712018-11-15 13:33:04 +00004426 /* Remove opaque/raw PSK + PSK Identity */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004427 ssl_conf_remove_psk(conf);
Hanno Becker7390c712018-11-15 13:33:04 +00004428
4429 /* Check and set raw PSK */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004430 if (psk == NULL) {
4431 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
4432 }
4433 if (psk_len == 0) {
4434 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
4435 }
4436 if (psk_len > MBEDTLS_PSK_MAX_LEN) {
4437 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
4438 }
Piotr Nowicki9926eaf2019-11-20 14:54:36 +01004439
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004440 if ((conf->psk = mbedtls_calloc(1, psk_len)) == NULL) {
4441 return MBEDTLS_ERR_SSL_ALLOC_FAILED;
4442 }
Hanno Becker7390c712018-11-15 13:33:04 +00004443 conf->psk_len = psk_len;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004444 memcpy(conf->psk, psk, conf->psk_len);
Hanno Becker7390c712018-11-15 13:33:04 +00004445
4446 /* Check and set PSK Identity */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004447 ret = ssl_conf_set_psk_identity(conf, psk_identity, psk_identity_len);
4448 if (ret != 0) {
4449 ssl_conf_remove_psk(conf);
4450 }
Hanno Becker7390c712018-11-15 13:33:04 +00004451
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004452 return ret;
Hanno Becker7390c712018-11-15 13:33:04 +00004453}
4454
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004455static void ssl_remove_psk(mbedtls_ssl_context *ssl)
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01004456{
4457#if defined(MBEDTLS_USE_PSA_CRYPTO)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004458 if (!mbedtls_svc_key_id_is_null(ssl->handshake->psk_opaque)) {
Ronald Croncf56a0a2020-08-04 09:51:30 +02004459 ssl->handshake->psk_opaque = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004460 } else
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01004461#endif /* MBEDTLS_USE_PSA_CRYPTO */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004462 if (ssl->handshake->psk != NULL) {
4463 mbedtls_platform_zeroize(ssl->handshake->psk,
4464 ssl->handshake->psk_len);
4465 mbedtls_free(ssl->handshake->psk);
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01004466 ssl->handshake->psk_len = 0;
4467 }
4468}
4469
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004470int mbedtls_ssl_set_hs_psk(mbedtls_ssl_context *ssl,
4471 const unsigned char *psk, size_t psk_len)
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01004472{
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004473 if (psk == NULL || ssl->handshake == NULL) {
4474 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
4475 }
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01004476
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004477 if (psk_len > MBEDTLS_PSK_MAX_LEN) {
4478 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
4479 }
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01004480
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004481 ssl_remove_psk(ssl);
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01004482
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004483 if ((ssl->handshake->psk = mbedtls_calloc(1, psk_len)) == NULL) {
4484 return MBEDTLS_ERR_SSL_ALLOC_FAILED;
4485 }
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01004486
4487 ssl->handshake->psk_len = psk_len;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004488 memcpy(ssl->handshake->psk, psk, ssl->handshake->psk_len);
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01004489
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004490 return 0;
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01004491}
4492
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01004493#if defined(MBEDTLS_USE_PSA_CRYPTO)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004494int mbedtls_ssl_conf_psk_opaque(mbedtls_ssl_config *conf,
4495 psa_key_id_t psk,
4496 const unsigned char *psk_identity,
4497 size_t psk_identity_len)
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01004498{
Janos Follath865b3eb2019-12-16 11:46:15 +00004499 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Hanno Becker7390c712018-11-15 13:33:04 +00004500 /* Clear opaque/raw PSK + PSK Identity, if present. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004501 ssl_conf_remove_psk(conf);
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01004502
Hanno Becker7390c712018-11-15 13:33:04 +00004503 /* Check and set opaque PSK */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004504 if (mbedtls_svc_key_id_is_null(psk)) {
4505 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
4506 }
Ronald Croncf56a0a2020-08-04 09:51:30 +02004507 conf->psk_opaque = psk;
Hanno Becker7390c712018-11-15 13:33:04 +00004508
4509 /* Check and set PSK Identity */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004510 ret = ssl_conf_set_psk_identity(conf, psk_identity,
4511 psk_identity_len);
4512 if (ret != 0) {
4513 ssl_conf_remove_psk(conf);
4514 }
Hanno Becker7390c712018-11-15 13:33:04 +00004515
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004516 return ret;
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01004517}
4518
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004519int mbedtls_ssl_set_hs_psk_opaque(mbedtls_ssl_context *ssl,
4520 psa_key_id_t psk)
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01004521{
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004522 if ((mbedtls_svc_key_id_is_null(psk)) ||
4523 (ssl->handshake == NULL)) {
4524 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
4525 }
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01004526
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004527 ssl_remove_psk(ssl);
Ronald Croncf56a0a2020-08-04 09:51:30 +02004528 ssl->handshake->psk_opaque = psk;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004529 return 0;
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01004530}
4531#endif /* MBEDTLS_USE_PSA_CRYPTO */
4532
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004533void mbedtls_ssl_conf_psk_cb(mbedtls_ssl_config *conf,
4534 int (*f_psk)(void *, mbedtls_ssl_context *, const unsigned char *,
4535 size_t),
4536 void *p_psk)
Paul Bakker6db455e2013-09-18 17:29:31 +02004537{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02004538 conf->f_psk = f_psk;
4539 conf->p_psk = p_psk;
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02004540}
Gilles Peskineeccd8882020-03-10 12:19:08 +01004541#endif /* MBEDTLS_KEY_EXCHANGE_SOME_PSK_ENABLED */
Paul Bakker43b7e352011-01-18 15:27:19 +00004542
Manuel Pégourié-Gonnardcf141ca2015-05-20 10:35:51 +02004543#if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_SSL_SRV_C)
Hanno Becker470a8c42017-10-04 15:28:46 +01004544
4545#if !defined(MBEDTLS_DEPRECATED_REMOVED)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004546int mbedtls_ssl_conf_dh_param(mbedtls_ssl_config *conf, const char *dhm_P, const char *dhm_G)
Paul Bakker5121ce52009-01-03 21:22:43 +00004547{
Janos Follath865b3eb2019-12-16 11:46:15 +00004548 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker5121ce52009-01-03 21:22:43 +00004549
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004550 if ((ret = mbedtls_mpi_read_string(&conf->dhm_P, 16, dhm_P)) != 0 ||
4551 (ret = mbedtls_mpi_read_string(&conf->dhm_G, 16, dhm_G)) != 0) {
4552 mbedtls_mpi_free(&conf->dhm_P);
4553 mbedtls_mpi_free(&conf->dhm_G);
4554 return ret;
Manuel Pégourié-Gonnard1028b742015-05-06 17:33:07 +01004555 }
Paul Bakker5121ce52009-01-03 21:22:43 +00004556
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004557 return 0;
Paul Bakker5121ce52009-01-03 21:22:43 +00004558}
Hanno Becker470a8c42017-10-04 15:28:46 +01004559#endif /* MBEDTLS_DEPRECATED_REMOVED */
Paul Bakker5121ce52009-01-03 21:22:43 +00004560
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004561int mbedtls_ssl_conf_dh_param_bin(mbedtls_ssl_config *conf,
4562 const unsigned char *dhm_P, size_t P_len,
4563 const unsigned char *dhm_G, size_t G_len)
Hanno Beckera90658f2017-10-04 15:29:08 +01004564{
Janos Follath865b3eb2019-12-16 11:46:15 +00004565 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Hanno Beckera90658f2017-10-04 15:29:08 +01004566
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004567 mbedtls_mpi_free(&conf->dhm_P);
4568 mbedtls_mpi_free(&conf->dhm_G);
Glenn Straussde081ce2021-12-20 01:43:17 -05004569
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004570 if ((ret = mbedtls_mpi_read_binary(&conf->dhm_P, dhm_P, P_len)) != 0 ||
4571 (ret = mbedtls_mpi_read_binary(&conf->dhm_G, dhm_G, G_len)) != 0) {
4572 mbedtls_mpi_free(&conf->dhm_P);
4573 mbedtls_mpi_free(&conf->dhm_G);
4574 return ret;
Hanno Beckera90658f2017-10-04 15:29:08 +01004575 }
4576
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004577 return 0;
Hanno Beckera90658f2017-10-04 15:29:08 +01004578}
Paul Bakker5121ce52009-01-03 21:22:43 +00004579
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004580int mbedtls_ssl_conf_dh_param_ctx(mbedtls_ssl_config *conf, mbedtls_dhm_context *dhm_ctx)
Paul Bakker1b57b062011-01-06 15:48:19 +00004581{
Janos Follath865b3eb2019-12-16 11:46:15 +00004582 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker1b57b062011-01-06 15:48:19 +00004583
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004584 mbedtls_mpi_free(&conf->dhm_P);
4585 mbedtls_mpi_free(&conf->dhm_G);
Glenn Straussde081ce2021-12-20 01:43:17 -05004586
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004587 if ((ret = mbedtls_mpi_copy(&conf->dhm_P, &dhm_ctx->P)) != 0 ||
4588 (ret = mbedtls_mpi_copy(&conf->dhm_G, &dhm_ctx->G)) != 0) {
4589 mbedtls_mpi_free(&conf->dhm_P);
4590 mbedtls_mpi_free(&conf->dhm_G);
4591 return ret;
Manuel Pégourié-Gonnard1028b742015-05-06 17:33:07 +01004592 }
Paul Bakker1b57b062011-01-06 15:48:19 +00004593
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004594 return 0;
Paul Bakker1b57b062011-01-06 15:48:19 +00004595}
Manuel Pégourié-Gonnardcf141ca2015-05-20 10:35:51 +02004596#endif /* MBEDTLS_DHM_C && MBEDTLS_SSL_SRV_C */
Paul Bakker1b57b062011-01-06 15:48:19 +00004597
Manuel Pégourié-Gonnardbd990d62015-06-11 14:49:42 +02004598#if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_SSL_CLI_C)
4599/*
4600 * Set the minimum length for Diffie-Hellman parameters
4601 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004602void mbedtls_ssl_conf_dhm_min_bitlen(mbedtls_ssl_config *conf,
4603 unsigned int bitlen)
Manuel Pégourié-Gonnardbd990d62015-06-11 14:49:42 +02004604{
4605 conf->dhm_min_bitlen = bitlen;
4606}
4607#endif /* MBEDTLS_DHM_C && MBEDTLS_SSL_CLI_C */
4608
Gilles Peskineeccd8882020-03-10 12:19:08 +01004609#if defined(MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED)
Manuel Pégourié-Gonnard36a8b572015-06-17 12:43:26 +02004610/*
4611 * Set allowed/preferred hashes for handshake signatures
4612 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004613void mbedtls_ssl_conf_sig_hashes(mbedtls_ssl_config *conf,
4614 const int *hashes)
Manuel Pégourié-Gonnard36a8b572015-06-17 12:43:26 +02004615{
4616 conf->sig_hashes = hashes;
4617}
Gilles Peskineeccd8882020-03-10 12:19:08 +01004618#endif /* MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED */
Manuel Pégourié-Gonnard36a8b572015-06-17 12:43:26 +02004619
Manuel Pégourié-Gonnardb541da62015-06-17 11:43:30 +02004620#if defined(MBEDTLS_ECP_C)
Manuel Pégourié-Gonnard7f38ed02014-02-04 15:52:33 +01004621/*
4622 * Set the allowed elliptic curves
4623 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004624void mbedtls_ssl_conf_curves(mbedtls_ssl_config *conf,
4625 const mbedtls_ecp_group_id *curve_list)
Manuel Pégourié-Gonnard7f38ed02014-02-04 15:52:33 +01004626{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02004627 conf->curve_list = curve_list;
Manuel Pégourié-Gonnard7f38ed02014-02-04 15:52:33 +01004628}
Hanno Becker947194e2017-04-07 13:25:49 +01004629#endif /* MBEDTLS_ECP_C */
Manuel Pégourié-Gonnard7f38ed02014-02-04 15:52:33 +01004630
Manuel Pégourié-Gonnardbc2b7712015-05-06 11:14:19 +01004631#if defined(MBEDTLS_X509_CRT_PARSE_C)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004632int mbedtls_ssl_set_hostname(mbedtls_ssl_context *ssl, const char *hostname)
Paul Bakker5121ce52009-01-03 21:22:43 +00004633{
Hanno Becker947194e2017-04-07 13:25:49 +01004634 /* Initialize to suppress unnecessary compiler warning */
4635 size_t hostname_len = 0;
4636
4637 /* Check if new hostname is valid before
4638 * making any change to current one */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004639 if (hostname != NULL) {
4640 hostname_len = strlen(hostname);
Hanno Becker947194e2017-04-07 13:25:49 +01004641
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004642 if (hostname_len > MBEDTLS_SSL_MAX_HOST_NAME_LEN) {
4643 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
4644 }
Hanno Becker947194e2017-04-07 13:25:49 +01004645 }
4646
4647 /* Now it's clear that we will overwrite the old hostname,
4648 * so we can free it safely */
4649
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004650 if (ssl->hostname != NULL) {
4651 mbedtls_platform_zeroize(ssl->hostname, strlen(ssl->hostname));
4652 mbedtls_free(ssl->hostname);
Hanno Becker947194e2017-04-07 13:25:49 +01004653 }
4654
4655 /* Passing NULL as hostname shall clear the old one */
Manuel Pégourié-Gonnardba26c242015-05-06 10:47:06 +01004656
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004657 if (hostname == NULL) {
Hanno Becker947194e2017-04-07 13:25:49 +01004658 ssl->hostname = NULL;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004659 } else {
4660 ssl->hostname = mbedtls_calloc(1, hostname_len + 1);
4661 if (ssl->hostname == NULL) {
4662 return MBEDTLS_ERR_SSL_ALLOC_FAILED;
4663 }
Paul Bakker75c1a6f2013-08-19 14:25:29 +02004664
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004665 memcpy(ssl->hostname, hostname, hostname_len);
Paul Bakker75c1a6f2013-08-19 14:25:29 +02004666
Hanno Becker947194e2017-04-07 13:25:49 +01004667 ssl->hostname[hostname_len] = '\0';
4668 }
Paul Bakker5121ce52009-01-03 21:22:43 +00004669
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004670 return 0;
Paul Bakker5121ce52009-01-03 21:22:43 +00004671}
Hanno Becker1a9a51c2017-04-07 13:02:16 +01004672#endif /* MBEDTLS_X509_CRT_PARSE_C */
Paul Bakker5121ce52009-01-03 21:22:43 +00004673
Manuel Pégourié-Gonnardbc2b7712015-05-06 11:14:19 +01004674#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004675void mbedtls_ssl_conf_sni(mbedtls_ssl_config *conf,
4676 int (*f_sni)(void *, mbedtls_ssl_context *,
4677 const unsigned char *, size_t),
4678 void *p_sni)
Paul Bakker5701cdc2012-09-27 21:49:42 +00004679{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02004680 conf->f_sni = f_sni;
4681 conf->p_sni = p_sni;
Paul Bakker5701cdc2012-09-27 21:49:42 +00004682}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004683#endif /* MBEDTLS_SSL_SERVER_NAME_INDICATION */
Paul Bakker5701cdc2012-09-27 21:49:42 +00004684
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004685#if defined(MBEDTLS_SSL_ALPN)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004686int mbedtls_ssl_conf_alpn_protocols(mbedtls_ssl_config *conf, const char **protos)
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02004687{
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +02004688 size_t cur_len, tot_len;
4689 const char **p;
4690
4691 /*
Brian J Murray1903fb32016-11-06 04:45:15 -08004692 * RFC 7301 3.1: "Empty strings MUST NOT be included and byte strings
4693 * MUST NOT be truncated."
4694 * We check lengths now rather than later.
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +02004695 */
4696 tot_len = 0;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004697 for (p = protos; *p != NULL; p++) {
4698 cur_len = strlen(*p);
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +02004699 tot_len += cur_len;
4700
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004701 if ((cur_len == 0) ||
4702 (cur_len > MBEDTLS_SSL_MAX_ALPN_NAME_LEN) ||
4703 (tot_len > MBEDTLS_SSL_MAX_ALPN_LIST_LEN)) {
4704 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
4705 }
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +02004706 }
4707
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02004708 conf->alpn_list = protos;
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +02004709
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004710 return 0;
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02004711}
4712
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004713const char *mbedtls_ssl_get_alpn_protocol(const mbedtls_ssl_context *ssl)
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02004714{
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004715 return ssl->alpn_chosen;
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02004716}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004717#endif /* MBEDTLS_SSL_ALPN */
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02004718
Johan Pascalb62bb512015-12-03 21:56:45 +01004719#if defined(MBEDTLS_SSL_DTLS_SRTP)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004720void mbedtls_ssl_conf_srtp_mki_value_supported(mbedtls_ssl_config *conf,
4721 int support_mki_value)
Ron Eldor591f1622018-01-22 12:30:04 +02004722{
4723 conf->dtls_srtp_mki_support = support_mki_value;
4724}
4725
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004726int mbedtls_ssl_dtls_srtp_set_mki_value(mbedtls_ssl_context *ssl,
4727 unsigned char *mki_value,
4728 uint16_t mki_len)
Ron Eldor591f1622018-01-22 12:30:04 +02004729{
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004730 if (mki_len > MBEDTLS_TLS_SRTP_MAX_MKI_LENGTH) {
4731 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
Ron Eldora9788042018-12-05 11:04:31 +02004732 }
Ron Eldor591f1622018-01-22 12:30:04 +02004733
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004734 if (ssl->conf->dtls_srtp_mki_support == MBEDTLS_SSL_DTLS_SRTP_MKI_UNSUPPORTED) {
4735 return MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
Ron Eldora9788042018-12-05 11:04:31 +02004736 }
Ron Eldor591f1622018-01-22 12:30:04 +02004737
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004738 memcpy(ssl->dtls_srtp_info.mki_value, mki_value, mki_len);
Ron Eldor591f1622018-01-22 12:30:04 +02004739 ssl->dtls_srtp_info.mki_len = mki_len;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004740 return 0;
Ron Eldor591f1622018-01-22 12:30:04 +02004741}
4742
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004743int mbedtls_ssl_conf_dtls_srtp_protection_profiles(mbedtls_ssl_config *conf,
4744 const mbedtls_ssl_srtp_profile *profiles)
Johan Pascalb62bb512015-12-03 21:56:45 +01004745{
Johan Pascal253d0262020-09-22 13:04:45 +02004746 const mbedtls_ssl_srtp_profile *p;
4747 size_t list_size = 0;
Johan Pascalb62bb512015-12-03 21:56:45 +01004748
Johan Pascal253d0262020-09-22 13:04:45 +02004749 /* check the profiles list: all entry must be valid,
4750 * its size cannot be more than the total number of supported profiles, currently 4 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004751 for (p = profiles; *p != MBEDTLS_TLS_SRTP_UNSET &&
4752 list_size <= MBEDTLS_TLS_SRTP_MAX_PROFILE_LIST_LENGTH;
4753 p++) {
4754 if (mbedtls_ssl_check_srtp_profile_value(*p) != MBEDTLS_TLS_SRTP_UNSET) {
Johan Pascal76fdf1d2020-10-22 23:31:00 +02004755 list_size++;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004756 } else {
Johan Pascal76fdf1d2020-10-22 23:31:00 +02004757 /* unsupported value, stop parsing and set the size to an error value */
4758 list_size = MBEDTLS_TLS_SRTP_MAX_PROFILE_LIST_LENGTH + 1;
Johan Pascalb62bb512015-12-03 21:56:45 +01004759 }
4760 }
4761
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004762 if (list_size > MBEDTLS_TLS_SRTP_MAX_PROFILE_LIST_LENGTH) {
4763 conf->dtls_srtp_profile_list = NULL;
4764 conf->dtls_srtp_profile_list_len = 0;
4765 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
Johan Pascal253d0262020-09-22 13:04:45 +02004766 }
4767
Johan Pascal9bc97ca2020-09-21 23:44:45 +02004768 conf->dtls_srtp_profile_list = profiles;
Johan Pascal253d0262020-09-22 13:04:45 +02004769 conf->dtls_srtp_profile_list_len = list_size;
Johan Pascalb62bb512015-12-03 21:56:45 +01004770
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004771 return 0;
Johan Pascalb62bb512015-12-03 21:56:45 +01004772}
4773
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004774void mbedtls_ssl_get_dtls_srtp_negotiation_result(const mbedtls_ssl_context *ssl,
4775 mbedtls_dtls_srtp_info *dtls_srtp_info)
Johan Pascalb62bb512015-12-03 21:56:45 +01004776{
Johan Pascal2258a4f2020-10-28 13:53:09 +01004777 dtls_srtp_info->chosen_dtls_srtp_profile = ssl->dtls_srtp_info.chosen_dtls_srtp_profile;
4778 /* do not copy the mki value if there is no chosen profile */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004779 if (dtls_srtp_info->chosen_dtls_srtp_profile == MBEDTLS_TLS_SRTP_UNSET) {
Johan Pascal2258a4f2020-10-28 13:53:09 +01004780 dtls_srtp_info->mki_len = 0;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004781 } else {
Johan Pascal2258a4f2020-10-28 13:53:09 +01004782 dtls_srtp_info->mki_len = ssl->dtls_srtp_info.mki_len;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004783 memcpy(dtls_srtp_info->mki_value, ssl->dtls_srtp_info.mki_value,
4784 ssl->dtls_srtp_info.mki_len);
Johan Pascal2258a4f2020-10-28 13:53:09 +01004785 }
Johan Pascalb62bb512015-12-03 21:56:45 +01004786}
Johan Pascalb62bb512015-12-03 21:56:45 +01004787#endif /* MBEDTLS_SSL_DTLS_SRTP */
4788
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004789void mbedtls_ssl_conf_max_version(mbedtls_ssl_config *conf, int major, int minor)
Paul Bakker490ecc82011-10-06 13:04:09 +00004790{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02004791 conf->max_major_ver = major;
4792 conf->max_minor_ver = minor;
Paul Bakker490ecc82011-10-06 13:04:09 +00004793}
4794
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004795void mbedtls_ssl_conf_min_version(mbedtls_ssl_config *conf, int major, int minor)
Paul Bakker1d29fb52012-09-28 13:28:45 +00004796{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02004797 conf->min_major_ver = major;
4798 conf->min_minor_ver = minor;
Paul Bakker1d29fb52012-09-28 13:28:45 +00004799}
4800
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004801#if defined(MBEDTLS_SSL_FALLBACK_SCSV) && defined(MBEDTLS_SSL_CLI_C)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004802void mbedtls_ssl_conf_fallback(mbedtls_ssl_config *conf, char fallback)
Manuel Pégourié-Gonnard1cbd39d2014-10-20 13:34:59 +02004803{
Manuel Pégourié-Gonnard684b0592015-05-06 09:27:31 +01004804 conf->fallback = fallback;
Manuel Pégourié-Gonnard1cbd39d2014-10-20 13:34:59 +02004805}
4806#endif
4807
Janos Follath088ce432017-04-10 12:42:31 +01004808#if defined(MBEDTLS_SSL_SRV_C)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004809void mbedtls_ssl_conf_cert_req_ca_list(mbedtls_ssl_config *conf,
4810 char cert_req_ca_list)
Janos Follath088ce432017-04-10 12:42:31 +01004811{
4812 conf->cert_req_ca_list = cert_req_ca_list;
4813}
4814#endif
4815
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004816#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004817void mbedtls_ssl_conf_encrypt_then_mac(mbedtls_ssl_config *conf, char etm)
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +01004818{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02004819 conf->encrypt_then_mac = etm;
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +01004820}
4821#endif
4822
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004823#if defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004824void mbedtls_ssl_conf_extended_master_secret(mbedtls_ssl_config *conf, char ems)
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +02004825{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02004826 conf->extended_ms = ems;
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +02004827}
4828#endif
4829
Manuel Pégourié-Gonnard66dc5552015-05-14 12:28:21 +02004830#if defined(MBEDTLS_ARC4_C)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004831void mbedtls_ssl_conf_arc4_support(mbedtls_ssl_config *conf, char arc4)
Manuel Pégourié-Gonnardbd47a582015-01-12 13:43:29 +01004832{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02004833 conf->arc4_disabled = arc4;
Manuel Pégourié-Gonnardbd47a582015-01-12 13:43:29 +01004834}
Manuel Pégourié-Gonnard66dc5552015-05-14 12:28:21 +02004835#endif
Manuel Pégourié-Gonnardbd47a582015-01-12 13:43:29 +01004836
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004837#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004838int mbedtls_ssl_conf_max_frag_len(mbedtls_ssl_config *conf, unsigned char mfl_code)
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02004839{
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004840 if (mfl_code >= MBEDTLS_SSL_MAX_FRAG_LEN_INVALID ||
4841 ssl_mfl_code_to_length(mfl_code) > MBEDTLS_TLS_EXT_ADV_CONTENT_LEN) {
4842 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02004843 }
4844
Manuel Pégourié-Gonnard6bf89d62015-05-05 17:01:57 +01004845 conf->mfl_code = mfl_code;
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02004846
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004847 return 0;
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02004848}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004849#endif /* MBEDTLS_SSL_MAX_FRAGMENT_LENGTH */
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02004850
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004851#if defined(MBEDTLS_SSL_TRUNCATED_HMAC)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004852void mbedtls_ssl_conf_truncated_hmac(mbedtls_ssl_config *conf, int truncate)
Manuel Pégourié-Gonnarde980a992013-07-19 11:08:52 +02004853{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02004854 conf->trunc_hmac = truncate;
Manuel Pégourié-Gonnarde980a992013-07-19 11:08:52 +02004855}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004856#endif /* MBEDTLS_SSL_TRUNCATED_HMAC */
Manuel Pégourié-Gonnarde980a992013-07-19 11:08:52 +02004857
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004858#if defined(MBEDTLS_SSL_CBC_RECORD_SPLITTING)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004859void mbedtls_ssl_conf_cbc_record_splitting(mbedtls_ssl_config *conf, char split)
Manuel Pégourié-Gonnardcfa477e2015-01-07 14:50:54 +01004860{
Manuel Pégourié-Gonnard17eab2b2015-05-05 16:34:53 +01004861 conf->cbc_record_splitting = split;
Manuel Pégourié-Gonnardcfa477e2015-01-07 14:50:54 +01004862}
4863#endif
4864
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004865void mbedtls_ssl_conf_legacy_renegotiation(mbedtls_ssl_config *conf, int allow_legacy)
Paul Bakker48916f92012-09-16 19:57:18 +00004866{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02004867 conf->allow_legacy_renegotiation = allow_legacy;
Paul Bakker48916f92012-09-16 19:57:18 +00004868}
4869
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004870#if defined(MBEDTLS_SSL_RENEGOTIATION)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004871void mbedtls_ssl_conf_renegotiation(mbedtls_ssl_config *conf, int renegotiation)
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01004872{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02004873 conf->disable_renegotiation = renegotiation;
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01004874}
4875
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004876void mbedtls_ssl_conf_renegotiation_enforced(mbedtls_ssl_config *conf, int max_records)
Manuel Pégourié-Gonnarda9964db2014-07-03 19:29:16 +02004877{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02004878 conf->renego_max_records = max_records;
Manuel Pégourié-Gonnarda9964db2014-07-03 19:29:16 +02004879}
4880
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004881void mbedtls_ssl_conf_renegotiation_period(mbedtls_ssl_config *conf,
4882 const unsigned char period[8])
Manuel Pégourié-Gonnard837f0fe2014-11-05 13:58:53 +01004883{
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004884 memcpy(conf->renego_period, period, 8);
Manuel Pégourié-Gonnard837f0fe2014-11-05 13:58:53 +01004885}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004886#endif /* MBEDTLS_SSL_RENEGOTIATION */
Paul Bakker5121ce52009-01-03 21:22:43 +00004887
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004888#if defined(MBEDTLS_SSL_SESSION_TICKETS)
Manuel Pégourié-Gonnardb596abf2015-05-20 10:45:29 +02004889#if defined(MBEDTLS_SSL_CLI_C)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004890void mbedtls_ssl_conf_session_tickets(mbedtls_ssl_config *conf, int use_tickets)
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +02004891{
Manuel Pégourié-Gonnard2b494452015-05-06 10:05:11 +01004892 conf->session_tickets = use_tickets;
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +02004893}
Manuel Pégourié-Gonnardb596abf2015-05-20 10:45:29 +02004894#endif
Paul Bakker606b4ba2013-08-14 16:52:14 +02004895
Manuel Pégourié-Gonnardb596abf2015-05-20 10:45:29 +02004896#if defined(MBEDTLS_SSL_SRV_C)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004897void mbedtls_ssl_conf_session_tickets_cb(mbedtls_ssl_config *conf,
4898 mbedtls_ssl_ticket_write_t *f_ticket_write,
4899 mbedtls_ssl_ticket_parse_t *f_ticket_parse,
4900 void *p_ticket)
Paul Bakker606b4ba2013-08-14 16:52:14 +02004901{
Manuel Pégourié-Gonnardd59675d2015-05-19 15:28:00 +02004902 conf->f_ticket_write = f_ticket_write;
4903 conf->f_ticket_parse = f_ticket_parse;
4904 conf->p_ticket = p_ticket;
Paul Bakker606b4ba2013-08-14 16:52:14 +02004905}
Manuel Pégourié-Gonnardb596abf2015-05-20 10:45:29 +02004906#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004907#endif /* MBEDTLS_SSL_SESSION_TICKETS */
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +02004908
Robert Cragie4feb7ae2015-10-02 13:33:37 +01004909#if defined(MBEDTLS_SSL_EXPORT_KEYS)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004910void mbedtls_ssl_conf_export_keys_cb(mbedtls_ssl_config *conf,
4911 mbedtls_ssl_export_keys_t *f_export_keys,
4912 void *p_export_keys)
Robert Cragie4feb7ae2015-10-02 13:33:37 +01004913{
4914 conf->f_export_keys = f_export_keys;
4915 conf->p_export_keys = p_export_keys;
4916}
Ron Eldorf5cc10d2019-05-07 18:33:40 +03004917
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004918void mbedtls_ssl_conf_export_keys_ext_cb(mbedtls_ssl_config *conf,
4919 mbedtls_ssl_export_keys_ext_t *f_export_keys_ext,
4920 void *p_export_keys)
Ron Eldorf5cc10d2019-05-07 18:33:40 +03004921{
4922 conf->f_export_keys_ext = f_export_keys_ext;
4923 conf->p_export_keys = p_export_keys;
4924}
Robert Cragie4feb7ae2015-10-02 13:33:37 +01004925#endif
4926
Gilles Peskineb74a1c72018-04-24 13:09:22 +02004927#if defined(MBEDTLS_SSL_ASYNC_PRIVATE)
Gilles Peskine8bf79f62018-01-05 21:11:53 +01004928void mbedtls_ssl_conf_async_private_cb(
4929 mbedtls_ssl_config *conf,
4930 mbedtls_ssl_async_sign_t *f_async_sign,
4931 mbedtls_ssl_async_decrypt_t *f_async_decrypt,
4932 mbedtls_ssl_async_resume_t *f_async_resume,
4933 mbedtls_ssl_async_cancel_t *f_async_cancel,
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004934 void *async_config_data)
Gilles Peskine8bf79f62018-01-05 21:11:53 +01004935{
4936 conf->f_async_sign_start = f_async_sign;
4937 conf->f_async_decrypt_start = f_async_decrypt;
4938 conf->f_async_resume = f_async_resume;
4939 conf->f_async_cancel = f_async_cancel;
Gilles Peskinedf13d5c2018-04-25 20:39:48 +02004940 conf->p_async_config_data = async_config_data;
4941}
4942
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004943void *mbedtls_ssl_conf_get_async_config_data(const mbedtls_ssl_config *conf)
Gilles Peskine8f97af72018-04-26 11:46:10 +02004944{
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004945 return conf->p_async_config_data;
Gilles Peskine8f97af72018-04-26 11:46:10 +02004946}
4947
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004948void *mbedtls_ssl_get_async_operation_data(const mbedtls_ssl_context *ssl)
Gilles Peskinedf13d5c2018-04-25 20:39:48 +02004949{
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004950 if (ssl->handshake == NULL) {
4951 return NULL;
4952 } else {
4953 return ssl->handshake->user_async_ctx;
4954 }
Gilles Peskinedf13d5c2018-04-25 20:39:48 +02004955}
4956
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004957void mbedtls_ssl_set_async_operation_data(mbedtls_ssl_context *ssl,
4958 void *ctx)
Gilles Peskinedf13d5c2018-04-25 20:39:48 +02004959{
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004960 if (ssl->handshake != NULL) {
Gilles Peskinedf13d5c2018-04-25 20:39:48 +02004961 ssl->handshake->user_async_ctx = ctx;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004962 }
Gilles Peskine8bf79f62018-01-05 21:11:53 +01004963}
Gilles Peskineb74a1c72018-04-24 13:09:22 +02004964#endif /* MBEDTLS_SSL_ASYNC_PRIVATE */
Gilles Peskine8bf79f62018-01-05 21:11:53 +01004965
Paul Bakker5121ce52009-01-03 21:22:43 +00004966/*
4967 * SSL get accessors
4968 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004969uint32_t mbedtls_ssl_get_verify_result(const mbedtls_ssl_context *ssl)
Paul Bakker5121ce52009-01-03 21:22:43 +00004970{
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004971 if (ssl->session != NULL) {
4972 return ssl->session->verify_result;
4973 }
Manuel Pégourié-Gonnarde89163c2015-01-23 14:30:57 +00004974
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004975 if (ssl->session_negotiate != NULL) {
4976 return ssl->session_negotiate->verify_result;
4977 }
Manuel Pégourié-Gonnarde89163c2015-01-23 14:30:57 +00004978
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004979 return 0xFFFFFFFF;
Paul Bakker5121ce52009-01-03 21:22:43 +00004980}
4981
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004982const char *mbedtls_ssl_get_ciphersuite(const mbedtls_ssl_context *ssl)
Paul Bakker72f62662011-01-16 21:27:44 +00004983{
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004984 if (ssl == NULL || ssl->session == NULL) {
4985 return NULL;
4986 }
Paul Bakker926c8e42013-03-06 10:23:34 +01004987
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004988 return mbedtls_ssl_get_ciphersuite_name(ssl->session->ciphersuite);
Paul Bakker72f62662011-01-16 21:27:44 +00004989}
4990
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004991const char *mbedtls_ssl_get_version(const mbedtls_ssl_context *ssl)
Paul Bakker43ca69c2011-01-15 17:35:19 +00004992{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004993#if defined(MBEDTLS_SSL_PROTO_DTLS)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004994 if (ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM) {
4995 switch (ssl->minor_ver) {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004996 case MBEDTLS_SSL_MINOR_VERSION_2:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004997 return "DTLSv1.0";
Manuel Pégourié-Gonnardb21ca2a2014-02-10 13:43:33 +01004998
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004999 case MBEDTLS_SSL_MINOR_VERSION_3:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005000 return "DTLSv1.2";
Manuel Pégourié-Gonnardb21ca2a2014-02-10 13:43:33 +01005001
5002 default:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005003 return "unknown (DTLS)";
Manuel Pégourié-Gonnardb21ca2a2014-02-10 13:43:33 +01005004 }
5005 }
5006#endif
5007
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005008 switch (ssl->minor_ver) {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005009 case MBEDTLS_SSL_MINOR_VERSION_0:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005010 return "SSLv3.0";
Paul Bakker43ca69c2011-01-15 17:35:19 +00005011
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005012 case MBEDTLS_SSL_MINOR_VERSION_1:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005013 return "TLSv1.0";
Paul Bakker43ca69c2011-01-15 17:35:19 +00005014
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005015 case MBEDTLS_SSL_MINOR_VERSION_2:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005016 return "TLSv1.1";
Paul Bakker43ca69c2011-01-15 17:35:19 +00005017
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005018 case MBEDTLS_SSL_MINOR_VERSION_3:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005019 return "TLSv1.2";
Paul Bakker1ef83d62012-04-11 12:09:53 +00005020
Paul Bakker43ca69c2011-01-15 17:35:19 +00005021 default:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005022 return "unknown";
Paul Bakker43ca69c2011-01-15 17:35:19 +00005023 }
Paul Bakker43ca69c2011-01-15 17:35:19 +00005024}
5025
Manuel Pégourié-Gonnarda2cda6b2015-08-31 18:30:52 +02005026#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005027size_t mbedtls_ssl_get_input_max_frag_len(const mbedtls_ssl_context *ssl)
Andrzej Kurek90c6e842020-04-03 05:25:29 -04005028{
5029 size_t max_len = MBEDTLS_SSL_MAX_CONTENT_LEN;
5030 size_t read_mfl;
5031
5032 /* Use the configured MFL for the client if we're past SERVER_HELLO_DONE */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005033 if (ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT &&
5034 ssl->state >= MBEDTLS_SSL_SERVER_HELLO_DONE) {
5035 return ssl_mfl_code_to_length(ssl->conf->mfl_code);
Andrzej Kurek90c6e842020-04-03 05:25:29 -04005036 }
5037
5038 /* Check if a smaller max length was negotiated */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005039 if (ssl->session_out != NULL) {
5040 read_mfl = ssl_mfl_code_to_length(ssl->session_out->mfl_code);
5041 if (read_mfl < max_len) {
Andrzej Kurek90c6e842020-04-03 05:25:29 -04005042 max_len = read_mfl;
5043 }
5044 }
5045
5046 // During a handshake, use the value being negotiated
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005047 if (ssl->session_negotiate != NULL) {
5048 read_mfl = ssl_mfl_code_to_length(ssl->session_negotiate->mfl_code);
5049 if (read_mfl < max_len) {
Andrzej Kurek90c6e842020-04-03 05:25:29 -04005050 max_len = read_mfl;
5051 }
5052 }
5053
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005054 return max_len;
Andrzej Kurek90c6e842020-04-03 05:25:29 -04005055}
5056
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005057size_t mbedtls_ssl_get_output_max_frag_len(const mbedtls_ssl_context *ssl)
Manuel Pégourié-Gonnarda2cda6b2015-08-31 18:30:52 +02005058{
5059 size_t max_len;
5060
5061 /*
5062 * Assume mfl_code is correct since it was checked when set
5063 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005064 max_len = ssl_mfl_code_to_length(ssl->conf->mfl_code);
Manuel Pégourié-Gonnarda2cda6b2015-08-31 18:30:52 +02005065
Manuel Pégourié-Gonnard2cb17e22017-09-19 13:00:47 +02005066 /* Check if a smaller max length was negotiated */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005067 if (ssl->session_out != NULL &&
5068 ssl_mfl_code_to_length(ssl->session_out->mfl_code) < max_len) {
5069 max_len = ssl_mfl_code_to_length(ssl->session_out->mfl_code);
Manuel Pégourié-Gonnarda2cda6b2015-08-31 18:30:52 +02005070 }
5071
Manuel Pégourié-Gonnard2cb17e22017-09-19 13:00:47 +02005072 /* During a handshake, use the value being negotiated */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005073 if (ssl->session_negotiate != NULL &&
5074 ssl_mfl_code_to_length(ssl->session_negotiate->mfl_code) < max_len) {
5075 max_len = ssl_mfl_code_to_length(ssl->session_negotiate->mfl_code);
Manuel Pégourié-Gonnard2cb17e22017-09-19 13:00:47 +02005076 }
5077
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005078 return max_len;
Manuel Pégourié-Gonnarda2cda6b2015-08-31 18:30:52 +02005079}
Andrzej Kurek90c6e842020-04-03 05:25:29 -04005080
5081#if !defined(MBEDTLS_DEPRECATED_REMOVED)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005082size_t mbedtls_ssl_get_max_frag_len(const mbedtls_ssl_context *ssl)
Andrzej Kurek90c6e842020-04-03 05:25:29 -04005083{
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005084 return mbedtls_ssl_get_output_max_frag_len(ssl);
Andrzej Kurek90c6e842020-04-03 05:25:29 -04005085}
5086#endif /* !MBEDTLS_DEPRECATED_REMOVED */
Manuel Pégourié-Gonnarda2cda6b2015-08-31 18:30:52 +02005087#endif /* MBEDTLS_SSL_MAX_FRAGMENT_LENGTH */
5088
Manuel Pégourié-Gonnardb8eec192018-08-20 09:34:02 +02005089#if defined(MBEDTLS_SSL_PROTO_DTLS)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005090size_t mbedtls_ssl_get_current_mtu(const mbedtls_ssl_context *ssl)
Manuel Pégourié-Gonnardb8eec192018-08-20 09:34:02 +02005091{
Andrzej Kurekef43ce62018-10-09 08:24:12 -04005092 /* Return unlimited mtu for client hello messages to avoid fragmentation. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005093 if (ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT &&
5094 (ssl->state == MBEDTLS_SSL_CLIENT_HELLO ||
5095 ssl->state == MBEDTLS_SSL_SERVER_HELLO)) {
5096 return 0;
5097 }
Andrzej Kurekef43ce62018-10-09 08:24:12 -04005098
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005099 if (ssl->handshake == NULL || ssl->handshake->mtu == 0) {
5100 return ssl->mtu;
5101 }
Manuel Pégourié-Gonnardb8eec192018-08-20 09:34:02 +02005102
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005103 if (ssl->mtu == 0) {
5104 return ssl->handshake->mtu;
5105 }
Manuel Pégourié-Gonnardb8eec192018-08-20 09:34:02 +02005106
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005107 return ssl->mtu < ssl->handshake->mtu ?
5108 ssl->mtu : ssl->handshake->mtu;
Manuel Pégourié-Gonnardb8eec192018-08-20 09:34:02 +02005109}
5110#endif /* MBEDTLS_SSL_PROTO_DTLS */
5111
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005112int mbedtls_ssl_get_max_out_record_payload(const mbedtls_ssl_context *ssl)
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02005113{
5114 size_t max_len = MBEDTLS_SSL_OUT_CONTENT_LEN;
5115
Manuel Pégourié-Gonnard000281e2018-08-21 11:20:58 +02005116#if !defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH) && \
5117 !defined(MBEDTLS_SSL_PROTO_DTLS)
5118 (void) ssl;
5119#endif
5120
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02005121#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005122 const size_t mfl = mbedtls_ssl_get_output_max_frag_len(ssl);
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02005123
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005124 if (max_len > mfl) {
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02005125 max_len = mfl;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005126 }
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02005127#endif
5128
5129#if defined(MBEDTLS_SSL_PROTO_DTLS)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005130 if (mbedtls_ssl_get_current_mtu(ssl) != 0) {
5131 const size_t mtu = mbedtls_ssl_get_current_mtu(ssl);
5132 const int ret = mbedtls_ssl_get_record_expansion(ssl);
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02005133 const size_t overhead = (size_t) ret;
5134
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005135 if (ret < 0) {
5136 return ret;
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02005137 }
5138
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005139 if (mtu <= overhead) {
5140 MBEDTLS_SSL_DEBUG_MSG(1, ("MTU too low for record expansion"));
5141 return MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
5142 }
5143
5144 if (max_len > mtu - overhead) {
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02005145 max_len = mtu - overhead;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005146 }
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02005147 }
Manuel Pégourié-Gonnardb8eec192018-08-20 09:34:02 +02005148#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02005149
Hanno Becker0defedb2018-08-10 12:35:02 +01005150#if !defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH) && \
5151 !defined(MBEDTLS_SSL_PROTO_DTLS)
5152 ((void) ssl);
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02005153#endif
5154
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005155 return (int) max_len;
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02005156}
5157
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005158#if defined(MBEDTLS_X509_CRT_PARSE_C)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005159const mbedtls_x509_crt *mbedtls_ssl_get_peer_cert(const mbedtls_ssl_context *ssl)
Paul Bakkerb0550d92012-10-30 07:51:03 +00005160{
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005161 if (ssl == NULL || ssl->session == NULL) {
5162 return NULL;
5163 }
Paul Bakkerb0550d92012-10-30 07:51:03 +00005164
Hanno Beckere6824572019-02-07 13:18:46 +00005165#if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005166 return ssl->session->peer_cert;
Hanno Beckere6824572019-02-07 13:18:46 +00005167#else
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005168 return NULL;
Hanno Beckere6824572019-02-07 13:18:46 +00005169#endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
Paul Bakkerb0550d92012-10-30 07:51:03 +00005170}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005171#endif /* MBEDTLS_X509_CRT_PARSE_C */
Paul Bakkerb0550d92012-10-30 07:51:03 +00005172
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005173#if defined(MBEDTLS_SSL_CLI_C)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005174int mbedtls_ssl_get_session(const mbedtls_ssl_context *ssl,
5175 mbedtls_ssl_session *dst)
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02005176{
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005177 if (ssl == NULL ||
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02005178 dst == NULL ||
5179 ssl->session == NULL ||
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005180 ssl->conf->endpoint != MBEDTLS_SSL_IS_CLIENT) {
5181 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02005182 }
5183
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005184 return mbedtls_ssl_session_copy(dst, ssl->session);
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02005185}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005186#endif /* MBEDTLS_SSL_CLI_C */
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02005187
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005188const mbedtls_ssl_session *mbedtls_ssl_get_session_pointer(const mbedtls_ssl_context *ssl)
Manuel Pégourié-Gonnardb5e4e0a2019-05-20 11:12:28 +02005189{
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005190 if (ssl == NULL) {
5191 return NULL;
5192 }
Manuel Pégourié-Gonnardb5e4e0a2019-05-20 11:12:28 +02005193
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005194 return ssl->session;
Manuel Pégourié-Gonnardb5e4e0a2019-05-20 11:12:28 +02005195}
5196
Paul Bakker5121ce52009-01-03 21:22:43 +00005197/*
Hanno Beckera835da52019-05-16 12:39:07 +01005198 * Define ticket header determining Mbed TLS version
5199 * and structure of the ticket.
5200 */
5201
Hanno Becker94ef3b32019-05-16 12:50:45 +01005202/*
Hanno Becker50b59662019-05-28 14:30:45 +01005203 * Define bitflag determining compile-time settings influencing
5204 * structure of serialized SSL sessions.
Hanno Becker94ef3b32019-05-16 12:50:45 +01005205 */
5206
Hanno Becker50b59662019-05-28 14:30:45 +01005207#if defined(MBEDTLS_HAVE_TIME)
Hanno Becker3e088662019-05-29 11:10:18 +01005208#define SSL_SERIALIZED_SESSION_CONFIG_TIME 1
Hanno Becker50b59662019-05-28 14:30:45 +01005209#else
Hanno Becker3e088662019-05-29 11:10:18 +01005210#define SSL_SERIALIZED_SESSION_CONFIG_TIME 0
Hanno Becker94ef3b32019-05-16 12:50:45 +01005211#endif /* MBEDTLS_HAVE_TIME */
5212
5213#if defined(MBEDTLS_X509_CRT_PARSE_C)
Hanno Becker3e088662019-05-29 11:10:18 +01005214#define SSL_SERIALIZED_SESSION_CONFIG_CRT 1
Hanno Becker94ef3b32019-05-16 12:50:45 +01005215#else
Hanno Becker3e088662019-05-29 11:10:18 +01005216#define SSL_SERIALIZED_SESSION_CONFIG_CRT 0
Hanno Becker94ef3b32019-05-16 12:50:45 +01005217#endif /* MBEDTLS_X509_CRT_PARSE_C */
5218
5219#if defined(MBEDTLS_SSL_CLI_C) && defined(MBEDTLS_SSL_SESSION_TICKETS)
Hanno Becker3e088662019-05-29 11:10:18 +01005220#define SSL_SERIALIZED_SESSION_CONFIG_CLIENT_TICKET 1
Hanno Becker94ef3b32019-05-16 12:50:45 +01005221#else
Hanno Becker3e088662019-05-29 11:10:18 +01005222#define SSL_SERIALIZED_SESSION_CONFIG_CLIENT_TICKET 0
Hanno Becker94ef3b32019-05-16 12:50:45 +01005223#endif /* MBEDTLS_SSL_CLI_C && MBEDTLS_SSL_SESSION_TICKETS */
5224
5225#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
Hanno Becker3e088662019-05-29 11:10:18 +01005226#define SSL_SERIALIZED_SESSION_CONFIG_MFL 1
Hanno Becker94ef3b32019-05-16 12:50:45 +01005227#else
Hanno Becker3e088662019-05-29 11:10:18 +01005228#define SSL_SERIALIZED_SESSION_CONFIG_MFL 0
Hanno Becker94ef3b32019-05-16 12:50:45 +01005229#endif /* MBEDTLS_SSL_MAX_FRAGMENT_LENGTH */
5230
5231#if defined(MBEDTLS_SSL_TRUNCATED_HMAC)
Hanno Becker3e088662019-05-29 11:10:18 +01005232#define SSL_SERIALIZED_SESSION_CONFIG_TRUNC_HMAC 1
Hanno Becker94ef3b32019-05-16 12:50:45 +01005233#else
Hanno Becker3e088662019-05-29 11:10:18 +01005234#define SSL_SERIALIZED_SESSION_CONFIG_TRUNC_HMAC 0
Hanno Becker94ef3b32019-05-16 12:50:45 +01005235#endif /* MBEDTLS_SSL_TRUNCATED_HMAC */
5236
5237#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
Hanno Becker3e088662019-05-29 11:10:18 +01005238#define SSL_SERIALIZED_SESSION_CONFIG_ETM 1
Hanno Becker94ef3b32019-05-16 12:50:45 +01005239#else
Hanno Becker3e088662019-05-29 11:10:18 +01005240#define SSL_SERIALIZED_SESSION_CONFIG_ETM 0
Hanno Becker94ef3b32019-05-16 12:50:45 +01005241#endif /* MBEDTLS_SSL_ENCRYPT_THEN_MAC */
5242
Hanno Becker94ef3b32019-05-16 12:50:45 +01005243#if defined(MBEDTLS_SSL_SESSION_TICKETS)
5244#define SSL_SERIALIZED_SESSION_CONFIG_TICKET 1
5245#else
5246#define SSL_SERIALIZED_SESSION_CONFIG_TICKET 0
5247#endif /* MBEDTLS_SSL_SESSION_TICKETS */
5248
Hanno Becker3e088662019-05-29 11:10:18 +01005249#define SSL_SERIALIZED_SESSION_CONFIG_TIME_BIT 0
5250#define SSL_SERIALIZED_SESSION_CONFIG_CRT_BIT 1
5251#define SSL_SERIALIZED_SESSION_CONFIG_CLIENT_TICKET_BIT 2
5252#define SSL_SERIALIZED_SESSION_CONFIG_MFL_BIT 3
5253#define SSL_SERIALIZED_SESSION_CONFIG_TRUNC_HMAC_BIT 4
5254#define SSL_SERIALIZED_SESSION_CONFIG_ETM_BIT 5
5255#define SSL_SERIALIZED_SESSION_CONFIG_TICKET_BIT 6
Hanno Becker3e088662019-05-29 11:10:18 +01005256
Hanno Becker50b59662019-05-28 14:30:45 +01005257#define SSL_SERIALIZED_SESSION_CONFIG_BITFLAG \
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005258 ((uint16_t) ( \
5259 (SSL_SERIALIZED_SESSION_CONFIG_TIME << SSL_SERIALIZED_SESSION_CONFIG_TIME_BIT) | \
5260 (SSL_SERIALIZED_SESSION_CONFIG_CRT << SSL_SERIALIZED_SESSION_CONFIG_CRT_BIT) | \
5261 (SSL_SERIALIZED_SESSION_CONFIG_CLIENT_TICKET << \
5262 SSL_SERIALIZED_SESSION_CONFIG_CLIENT_TICKET_BIT) | \
5263 (SSL_SERIALIZED_SESSION_CONFIG_MFL << SSL_SERIALIZED_SESSION_CONFIG_MFL_BIT) | \
5264 (SSL_SERIALIZED_SESSION_CONFIG_TRUNC_HMAC << \
5265 SSL_SERIALIZED_SESSION_CONFIG_TRUNC_HMAC_BIT) | \
5266 (SSL_SERIALIZED_SESSION_CONFIG_ETM << SSL_SERIALIZED_SESSION_CONFIG_ETM_BIT) | \
5267 (SSL_SERIALIZED_SESSION_CONFIG_TICKET << SSL_SERIALIZED_SESSION_CONFIG_TICKET_BIT)))
Hanno Becker94ef3b32019-05-16 12:50:45 +01005268
Hanno Beckerf8787072019-05-16 12:41:07 +01005269static unsigned char ssl_serialized_session_header[] = {
Hanno Becker94ef3b32019-05-16 12:50:45 +01005270 MBEDTLS_VERSION_MAJOR,
5271 MBEDTLS_VERSION_MINOR,
5272 MBEDTLS_VERSION_PATCH,
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005273 MBEDTLS_BYTE_1(SSL_SERIALIZED_SESSION_CONFIG_BITFLAG),
5274 MBEDTLS_BYTE_0(SSL_SERIALIZED_SESSION_CONFIG_BITFLAG),
Hanno Beckerf8787072019-05-16 12:41:07 +01005275};
Hanno Beckera835da52019-05-16 12:39:07 +01005276
5277/*
Manuel Pégourié-Gonnarda3e7c652019-05-16 10:08:35 +02005278 * Serialize a session in the following format:
Manuel Pégourié-Gonnard35eb8022019-05-16 11:11:08 +02005279 * (in the presentation language of TLS, RFC 8446 section 3)
Manuel Pégourié-Gonnarda3e7c652019-05-16 10:08:35 +02005280 *
Hanno Becker50b59662019-05-28 14:30:45 +01005281 * opaque mbedtls_version[3]; // major, minor, patch
5282 * opaque session_format[2]; // version-specific 16-bit field determining
5283 * // the format of the remaining
5284 * // serialized data.
Hanno Beckerdc28b6c2019-05-29 11:08:00 +01005285 *
5286 * Note: When updating the format, remember to keep
5287 * these version+format bytes.
5288 *
Hanno Beckerbe34e8e2019-06-04 09:43:16 +01005289 * // In this version, `session_format` determines
5290 * // the setting of those compile-time
5291 * // configuration options which influence
Hanno Becker50b59662019-05-28 14:30:45 +01005292 * // the structure of mbedtls_ssl_session.
Manuel Pégourié-Gonnardf743c032019-05-24 12:06:29 +02005293 * uint64 start_time;
Hanno Becker50b59662019-05-28 14:30:45 +01005294 * uint8 ciphersuite[2]; // defined by the standard
5295 * uint8 compression; // 0 or 1
5296 * uint8 session_id_len; // at most 32
Manuel Pégourié-Gonnardf743c032019-05-24 12:06:29 +02005297 * opaque session_id[32];
Hanno Becker50b59662019-05-28 14:30:45 +01005298 * opaque master[48]; // fixed length in the standard
Manuel Pégourié-Gonnardf743c032019-05-24 12:06:29 +02005299 * uint32 verify_result;
Hanno Becker50b59662019-05-28 14:30:45 +01005300 * opaque peer_cert<0..2^24-1>; // length 0 means no peer cert
5301 * opaque ticket<0..2^24-1>; // length 0 means no ticket
Manuel Pégourié-Gonnardf743c032019-05-24 12:06:29 +02005302 * uint32 ticket_lifetime;
Hanno Becker50b59662019-05-28 14:30:45 +01005303 * uint8 mfl_code; // up to 255 according to standard
5304 * uint8 trunc_hmac; // 0 or 1
5305 * uint8 encrypt_then_mac; // 0 or 1
Manuel Pégourié-Gonnarda3e7c652019-05-16 10:08:35 +02005306 *
Manuel Pégourié-Gonnardf743c032019-05-24 12:06:29 +02005307 * The order is the same as in the definition of the structure, except
5308 * verify_result is put before peer_cert so that all mandatory fields come
5309 * together in one block.
Manuel Pégourié-Gonnarda3e7c652019-05-16 10:08:35 +02005310 */
Manuel Pégourié-Gonnardd904d662022-06-17 10:24:00 +02005311MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005312static int ssl_session_save(const mbedtls_ssl_session *session,
5313 unsigned char omit_header,
5314 unsigned char *buf,
5315 size_t buf_len,
5316 size_t *olen)
Manuel Pégourié-Gonnarda3e7c652019-05-16 10:08:35 +02005317{
5318 unsigned char *p = buf;
Manuel Pégourié-Gonnard26f982f2019-05-21 11:01:32 +02005319 size_t used = 0;
Manuel Pégourié-Gonnardf743c032019-05-24 12:06:29 +02005320#if defined(MBEDTLS_HAVE_TIME)
5321 uint64_t start;
5322#endif
Manuel Pégourié-Gonnarda3e7c652019-05-16 10:08:35 +02005323#if defined(MBEDTLS_X509_CRT_PARSE_C)
5324#if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
5325 size_t cert_len;
Manuel Pégourié-Gonnarda3e7c652019-05-16 10:08:35 +02005326#endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
5327#endif /* MBEDTLS_X509_CRT_PARSE_C */
5328
Manuel Pégourié-Gonnarda3e7c652019-05-16 10:08:35 +02005329
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005330 if (!omit_header) {
Manuel Pégourié-Gonnard45ac1f02019-07-23 16:52:45 +02005331 /*
5332 * Add version identifier
5333 */
5334
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005335 used += sizeof(ssl_serialized_session_header);
Manuel Pégourié-Gonnard45ac1f02019-07-23 16:52:45 +02005336
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005337 if (used <= buf_len) {
5338 memcpy(p, ssl_serialized_session_header,
5339 sizeof(ssl_serialized_session_header));
5340 p += sizeof(ssl_serialized_session_header);
Manuel Pégourié-Gonnard45ac1f02019-07-23 16:52:45 +02005341 }
Hanno Beckera835da52019-05-16 12:39:07 +01005342 }
5343
5344 /*
Manuel Pégourié-Gonnardf743c032019-05-24 12:06:29 +02005345 * Time
5346 */
5347#if defined(MBEDTLS_HAVE_TIME)
5348 used += 8;
5349
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005350 if (used <= buf_len) {
Manuel Pégourié-Gonnardf743c032019-05-24 12:06:29 +02005351 start = (uint64_t) session->start;
5352
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005353 MBEDTLS_PUT_UINT64_BE(start, p, 0);
Joe Subbiani2f98d792021-08-20 11:44:44 +01005354 p += 8;
Manuel Pégourié-Gonnardf743c032019-05-24 12:06:29 +02005355 }
5356#endif /* MBEDTLS_HAVE_TIME */
5357
5358 /*
5359 * Basic mandatory fields
5360 */
5361 used += 2 /* ciphersuite */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005362 + 1 /* compression */
5363 + 1 /* id_len */
5364 + sizeof(session->id)
5365 + sizeof(session->master)
5366 + 4; /* verify_result */
Manuel Pégourié-Gonnardf743c032019-05-24 12:06:29 +02005367
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005368 if (used <= buf_len) {
5369 MBEDTLS_PUT_UINT16_BE(session->ciphersuite, p, 0);
Joe Subbiani2f98d792021-08-20 11:44:44 +01005370 p += 2;
Manuel Pégourié-Gonnardf743c032019-05-24 12:06:29 +02005371
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005372 *p++ = MBEDTLS_BYTE_0(session->compression);
Manuel Pégourié-Gonnardf743c032019-05-24 12:06:29 +02005373
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005374 *p++ = MBEDTLS_BYTE_0(session->id_len);
5375 memcpy(p, session->id, 32);
Manuel Pégourié-Gonnardf743c032019-05-24 12:06:29 +02005376 p += 32;
5377
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005378 memcpy(p, session->master, 48);
Manuel Pégourié-Gonnardf743c032019-05-24 12:06:29 +02005379 p += 48;
5380
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005381 MBEDTLS_PUT_UINT32_BE(session->verify_result, p, 0);
Joe Subbiani2f98d792021-08-20 11:44:44 +01005382 p += 4;
Manuel Pégourié-Gonnard26f982f2019-05-21 11:01:32 +02005383 }
Manuel Pégourié-Gonnarda3e7c652019-05-16 10:08:35 +02005384
Manuel Pégourié-Gonnard35eb8022019-05-16 11:11:08 +02005385 /*
Manuel Pégourié-Gonnardf743c032019-05-24 12:06:29 +02005386 * Peer's end-entity certificate
Manuel Pégourié-Gonnard35eb8022019-05-16 11:11:08 +02005387 */
Manuel Pégourié-Gonnarda3e7c652019-05-16 10:08:35 +02005388#if defined(MBEDTLS_X509_CRT_PARSE_C)
5389#if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005390 if (session->peer_cert == NULL) {
Manuel Pégourié-Gonnarda3e7c652019-05-16 10:08:35 +02005391 cert_len = 0;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005392 } else {
Manuel Pégourié-Gonnarda3e7c652019-05-16 10:08:35 +02005393 cert_len = session->peer_cert->raw.len;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005394 }
Manuel Pégourié-Gonnarda3e7c652019-05-16 10:08:35 +02005395
Manuel Pégourié-Gonnard26f982f2019-05-21 11:01:32 +02005396 used += 3 + cert_len;
Manuel Pégourié-Gonnarda3e7c652019-05-16 10:08:35 +02005397
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005398 if (used <= buf_len) {
5399 *p++ = MBEDTLS_BYTE_2(cert_len);
5400 *p++ = MBEDTLS_BYTE_1(cert_len);
5401 *p++ = MBEDTLS_BYTE_0(cert_len);
Manuel Pégourié-Gonnarda3e7c652019-05-16 10:08:35 +02005402
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005403 if (session->peer_cert != NULL) {
5404 memcpy(p, session->peer_cert->raw.p, cert_len);
Manuel Pégourié-Gonnard26f982f2019-05-21 11:01:32 +02005405 p += cert_len;
5406 }
5407 }
Manuel Pégourié-Gonnarda3e7c652019-05-16 10:08:35 +02005408#else /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005409 if (session->peer_cert_digest != NULL) {
Manuel Pégourié-Gonnardf743c032019-05-24 12:06:29 +02005410 used += 1 /* type */ + 1 /* length */ + session->peer_cert_digest_len;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005411 if (used <= buf_len) {
Manuel Pégourié-Gonnardf743c032019-05-24 12:06:29 +02005412 *p++ = (unsigned char) session->peer_cert_digest_type;
5413 *p++ = (unsigned char) session->peer_cert_digest_len;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005414 memcpy(p, session->peer_cert_digest,
5415 session->peer_cert_digest_len);
Manuel Pégourié-Gonnardf743c032019-05-24 12:06:29 +02005416 p += session->peer_cert_digest_len;
5417 }
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005418 } else {
Manuel Pégourié-Gonnardf743c032019-05-24 12:06:29 +02005419 used += 2;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005420 if (used <= buf_len) {
Manuel Pégourié-Gonnardf743c032019-05-24 12:06:29 +02005421 *p++ = (unsigned char) MBEDTLS_MD_NONE;
5422 *p++ = 0;
5423 }
Manuel Pégourié-Gonnard26f982f2019-05-21 11:01:32 +02005424 }
Manuel Pégourié-Gonnarda3e7c652019-05-16 10:08:35 +02005425#endif /* !MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
5426#endif /* MBEDTLS_X509_CRT_PARSE_C */
5427
Manuel Pégourié-Gonnard35eb8022019-05-16 11:11:08 +02005428 /*
Manuel Pégourié-Gonnardf743c032019-05-24 12:06:29 +02005429 * Session ticket if any, plus associated data
Manuel Pégourié-Gonnard35eb8022019-05-16 11:11:08 +02005430 */
5431#if defined(MBEDTLS_SSL_SESSION_TICKETS) && defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnardf743c032019-05-24 12:06:29 +02005432 used += 3 + session->ticket_len + 4; /* len + ticket + lifetime */
Manuel Pégourié-Gonnard35eb8022019-05-16 11:11:08 +02005433
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005434 if (used <= buf_len) {
5435 *p++ = MBEDTLS_BYTE_2(session->ticket_len);
5436 *p++ = MBEDTLS_BYTE_1(session->ticket_len);
5437 *p++ = MBEDTLS_BYTE_0(session->ticket_len);
Manuel Pégourié-Gonnard26f982f2019-05-21 11:01:32 +02005438
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005439 if (session->ticket != NULL) {
5440 memcpy(p, session->ticket, session->ticket_len);
Manuel Pégourié-Gonnard26f982f2019-05-21 11:01:32 +02005441 p += session->ticket_len;
5442 }
Manuel Pégourié-Gonnardf743c032019-05-24 12:06:29 +02005443
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005444 MBEDTLS_PUT_UINT32_BE(session->ticket_lifetime, p, 0);
Joe Subbiani2f98d792021-08-20 11:44:44 +01005445 p += 4;
Manuel Pégourié-Gonnard35eb8022019-05-16 11:11:08 +02005446 }
5447#endif /* MBEDTLS_SSL_SESSION_TICKETS && MBEDTLS_SSL_CLI_C */
5448
Manuel Pégourié-Gonnardf743c032019-05-24 12:06:29 +02005449 /*
5450 * Misc extension-related info
5451 */
5452#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
5453 used += 1;
5454
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005455 if (used <= buf_len) {
Manuel Pégourié-Gonnardf743c032019-05-24 12:06:29 +02005456 *p++ = session->mfl_code;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005457 }
Manuel Pégourié-Gonnardf743c032019-05-24 12:06:29 +02005458#endif
5459
5460#if defined(MBEDTLS_SSL_TRUNCATED_HMAC)
5461 used += 1;
5462
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005463 if (used <= buf_len) {
5464 *p++ = (unsigned char) ((session->trunc_hmac) & 0xFF);
5465 }
Manuel Pégourié-Gonnardf743c032019-05-24 12:06:29 +02005466#endif
5467
5468#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
5469 used += 1;
5470
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005471 if (used <= buf_len) {
5472 *p++ = MBEDTLS_BYTE_0(session->encrypt_then_mac);
5473 }
Manuel Pégourié-Gonnardf743c032019-05-24 12:06:29 +02005474#endif
5475
Manuel Pégourié-Gonnard35eb8022019-05-16 11:11:08 +02005476 /* Done */
Manuel Pégourié-Gonnard26f982f2019-05-21 11:01:32 +02005477 *olen = used;
5478
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005479 if (used > buf_len) {
5480 return MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL;
5481 }
Manuel Pégourié-Gonnarda3e7c652019-05-16 10:08:35 +02005482
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005483 return 0;
Manuel Pégourié-Gonnarda3e7c652019-05-16 10:08:35 +02005484}
5485
5486/*
Manuel Pégourié-Gonnard45ac1f02019-07-23 16:52:45 +02005487 * Public wrapper for ssl_session_save()
5488 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005489int mbedtls_ssl_session_save(const mbedtls_ssl_session *session,
5490 unsigned char *buf,
5491 size_t buf_len,
5492 size_t *olen)
Manuel Pégourié-Gonnard45ac1f02019-07-23 16:52:45 +02005493{
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005494 return ssl_session_save(session, 0, buf, buf_len, olen);
Manuel Pégourié-Gonnard45ac1f02019-07-23 16:52:45 +02005495}
5496
5497/*
Manuel Pégourié-Gonnardb9dfc9f2019-07-12 10:50:19 +02005498 * Deserialize session, see mbedtls_ssl_session_save() for format.
Manuel Pégourié-Gonnarda3d831b2019-05-23 12:28:45 +02005499 *
5500 * This internal version is wrapped by a public function that cleans up in
Manuel Pégourié-Gonnard45ac1f02019-07-23 16:52:45 +02005501 * case of error, and has an extra option omit_header.
Manuel Pégourié-Gonnarda3e7c652019-05-16 10:08:35 +02005502 */
Manuel Pégourié-Gonnardd904d662022-06-17 10:24:00 +02005503MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005504static int ssl_session_load(mbedtls_ssl_session *session,
5505 unsigned char omit_header,
5506 const unsigned char *buf,
5507 size_t len)
Manuel Pégourié-Gonnarda3e7c652019-05-16 10:08:35 +02005508{
5509 const unsigned char *p = buf;
5510 const unsigned char * const end = buf + len;
Manuel Pégourié-Gonnardf743c032019-05-24 12:06:29 +02005511#if defined(MBEDTLS_HAVE_TIME)
5512 uint64_t start;
5513#endif
Manuel Pégourié-Gonnarda3e7c652019-05-16 10:08:35 +02005514#if defined(MBEDTLS_X509_CRT_PARSE_C)
5515#if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
5516 size_t cert_len;
Manuel Pégourié-Gonnarda3e7c652019-05-16 10:08:35 +02005517#endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
5518#endif /* MBEDTLS_X509_CRT_PARSE_C */
5519
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005520 if (!omit_header) {
Manuel Pégourié-Gonnard45ac1f02019-07-23 16:52:45 +02005521 /*
5522 * Check version identifier
5523 */
5524
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005525 if ((size_t) (end - p) < sizeof(ssl_serialized_session_header)) {
5526 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
Manuel Pégourié-Gonnard45ac1f02019-07-23 16:52:45 +02005527 }
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005528
5529 if (memcmp(p, ssl_serialized_session_header,
5530 sizeof(ssl_serialized_session_header)) != 0) {
5531 return MBEDTLS_ERR_SSL_VERSION_MISMATCH;
5532 }
5533 p += sizeof(ssl_serialized_session_header);
Hanno Beckera835da52019-05-16 12:39:07 +01005534 }
Hanno Beckera835da52019-05-16 12:39:07 +01005535
5536 /*
Manuel Pégourié-Gonnardf743c032019-05-24 12:06:29 +02005537 * Time
Manuel Pégourié-Gonnard35eb8022019-05-16 11:11:08 +02005538 */
Manuel Pégourié-Gonnardf743c032019-05-24 12:06:29 +02005539#if defined(MBEDTLS_HAVE_TIME)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005540 if (8 > (size_t) (end - p)) {
5541 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
5542 }
Manuel Pégourié-Gonnarda3e7c652019-05-16 10:08:35 +02005543
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005544 start = ((uint64_t) p[0] << 56) |
5545 ((uint64_t) p[1] << 48) |
5546 ((uint64_t) p[2] << 40) |
5547 ((uint64_t) p[3] << 32) |
5548 ((uint64_t) p[4] << 24) |
5549 ((uint64_t) p[5] << 16) |
5550 ((uint64_t) p[6] << 8) |
5551 ((uint64_t) p[7]);
Manuel Pégourié-Gonnardf743c032019-05-24 12:06:29 +02005552 p += 8;
Manuel Pégourié-Gonnarda3e7c652019-05-16 10:08:35 +02005553
Manuel Pégourié-Gonnardf743c032019-05-24 12:06:29 +02005554 session->start = (time_t) start;
5555#endif /* MBEDTLS_HAVE_TIME */
5556
5557 /*
5558 * Basic mandatory fields
5559 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005560 if (2 + 1 + 1 + 32 + 48 + 4 > (size_t) (end - p)) {
5561 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
5562 }
Manuel Pégourié-Gonnardf743c032019-05-24 12:06:29 +02005563
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005564 session->ciphersuite = (p[0] << 8) | p[1];
Manuel Pégourié-Gonnardf743c032019-05-24 12:06:29 +02005565 p += 2;
5566
5567 session->compression = *p++;
5568
5569 session->id_len = *p++;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005570 memcpy(session->id, p, 32);
Manuel Pégourié-Gonnardf743c032019-05-24 12:06:29 +02005571 p += 32;
5572
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005573 memcpy(session->master, p, 48);
Manuel Pégourié-Gonnardf743c032019-05-24 12:06:29 +02005574 p += 48;
5575
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005576 session->verify_result = ((uint32_t) p[0] << 24) |
5577 ((uint32_t) p[1] << 16) |
5578 ((uint32_t) p[2] << 8) |
5579 ((uint32_t) p[3]);
Manuel Pégourié-Gonnardf743c032019-05-24 12:06:29 +02005580 p += 4;
5581
5582 /* Immediately clear invalid pointer values that have been read, in case
5583 * we exit early before we replaced them with valid ones. */
Manuel Pégourié-Gonnarda3e7c652019-05-16 10:08:35 +02005584#if defined(MBEDTLS_X509_CRT_PARSE_C)
5585#if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
5586 session->peer_cert = NULL;
5587#else
5588 session->peer_cert_digest = NULL;
5589#endif /* !MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
5590#endif /* MBEDTLS_X509_CRT_PARSE_C */
5591#if defined(MBEDTLS_SSL_SESSION_TICKETS) && defined(MBEDTLS_SSL_CLI_C)
5592 session->ticket = NULL;
5593#endif /* MBEDTLS_SSL_SESSION_TICKETS && MBEDTLS_SSL_CLI_C */
5594
Manuel Pégourié-Gonnard35eb8022019-05-16 11:11:08 +02005595 /*
5596 * Peer certificate
5597 */
Manuel Pégourié-Gonnarda3e7c652019-05-16 10:08:35 +02005598#if defined(MBEDTLS_X509_CRT_PARSE_C)
5599#if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
5600 /* Deserialize CRT from the end of the ticket. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005601 if (3 > (size_t) (end - p)) {
5602 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
5603 }
Manuel Pégourié-Gonnarda3e7c652019-05-16 10:08:35 +02005604
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005605 cert_len = (p[0] << 16) | (p[1] << 8) | p[2];
Manuel Pégourié-Gonnarda3e7c652019-05-16 10:08:35 +02005606 p += 3;
5607
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005608 if (cert_len != 0) {
Janos Follath865b3eb2019-12-16 11:46:15 +00005609 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnarda3e7c652019-05-16 10:08:35 +02005610
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005611 if (cert_len > (size_t) (end - p)) {
5612 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
5613 }
Manuel Pégourié-Gonnarda3e7c652019-05-16 10:08:35 +02005614
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005615 session->peer_cert = mbedtls_calloc(1, sizeof(mbedtls_x509_crt));
Manuel Pégourié-Gonnarda3e7c652019-05-16 10:08:35 +02005616
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005617 if (session->peer_cert == NULL) {
5618 return MBEDTLS_ERR_SSL_ALLOC_FAILED;
5619 }
Manuel Pégourié-Gonnarda3e7c652019-05-16 10:08:35 +02005620
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005621 mbedtls_x509_crt_init(session->peer_cert);
Manuel Pégourié-Gonnarda3e7c652019-05-16 10:08:35 +02005622
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005623 if ((ret = mbedtls_x509_crt_parse_der(session->peer_cert,
5624 p, cert_len)) != 0) {
5625 mbedtls_x509_crt_free(session->peer_cert);
5626 mbedtls_free(session->peer_cert);
Manuel Pégourié-Gonnarda3e7c652019-05-16 10:08:35 +02005627 session->peer_cert = NULL;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005628 return ret;
Manuel Pégourié-Gonnarda3e7c652019-05-16 10:08:35 +02005629 }
5630
5631 p += cert_len;
5632 }
5633#else /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
5634 /* Deserialize CRT digest from the end of the ticket. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005635 if (2 > (size_t) (end - p)) {
5636 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
5637 }
Manuel Pégourié-Gonnarda3e7c652019-05-16 10:08:35 +02005638
Manuel Pégourié-Gonnardf743c032019-05-24 12:06:29 +02005639 session->peer_cert_digest_type = (mbedtls_md_type_t) *p++;
5640 session->peer_cert_digest_len = (size_t) *p++;
Manuel Pégourié-Gonnarda3e7c652019-05-16 10:08:35 +02005641
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005642 if (session->peer_cert_digest_len != 0) {
Manuel Pégourié-Gonnardf743c032019-05-24 12:06:29 +02005643 const mbedtls_md_info_t *md_info =
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005644 mbedtls_md_info_from_type(session->peer_cert_digest_type);
5645 if (md_info == NULL) {
5646 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
5647 }
5648 if (session->peer_cert_digest_len != mbedtls_md_get_size(md_info)) {
5649 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
5650 }
Manuel Pégourié-Gonnarda3e7c652019-05-16 10:08:35 +02005651
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005652 if (session->peer_cert_digest_len > (size_t) (end - p)) {
5653 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
5654 }
Manuel Pégourié-Gonnardf743c032019-05-24 12:06:29 +02005655
5656 session->peer_cert_digest =
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005657 mbedtls_calloc(1, session->peer_cert_digest_len);
5658 if (session->peer_cert_digest == NULL) {
5659 return MBEDTLS_ERR_SSL_ALLOC_FAILED;
5660 }
Manuel Pégourié-Gonnarda3e7c652019-05-16 10:08:35 +02005661
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005662 memcpy(session->peer_cert_digest, p,
5663 session->peer_cert_digest_len);
Manuel Pégourié-Gonnardf743c032019-05-24 12:06:29 +02005664 p += session->peer_cert_digest_len;
Manuel Pégourié-Gonnarda3e7c652019-05-16 10:08:35 +02005665 }
5666#endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
5667#endif /* MBEDTLS_X509_CRT_PARSE_C */
5668
Manuel Pégourié-Gonnard35eb8022019-05-16 11:11:08 +02005669 /*
Manuel Pégourié-Gonnardf743c032019-05-24 12:06:29 +02005670 * Session ticket and associated data
Manuel Pégourié-Gonnard35eb8022019-05-16 11:11:08 +02005671 */
5672#if defined(MBEDTLS_SSL_SESSION_TICKETS) && defined(MBEDTLS_SSL_CLI_C)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005673 if (3 > (size_t) (end - p)) {
5674 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
5675 }
Manuel Pégourié-Gonnard35eb8022019-05-16 11:11:08 +02005676
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005677 session->ticket_len = (p[0] << 16) | (p[1] << 8) | p[2];
Manuel Pégourié-Gonnard35eb8022019-05-16 11:11:08 +02005678 p += 3;
5679
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005680 if (session->ticket_len != 0) {
5681 if (session->ticket_len > (size_t) (end - p)) {
5682 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
5683 }
Manuel Pégourié-Gonnard35eb8022019-05-16 11:11:08 +02005684
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005685 session->ticket = mbedtls_calloc(1, session->ticket_len);
5686 if (session->ticket == NULL) {
5687 return MBEDTLS_ERR_SSL_ALLOC_FAILED;
5688 }
Manuel Pégourié-Gonnard35eb8022019-05-16 11:11:08 +02005689
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005690 memcpy(session->ticket, p, session->ticket_len);
Manuel Pégourié-Gonnard35eb8022019-05-16 11:11:08 +02005691 p += session->ticket_len;
5692 }
Manuel Pégourié-Gonnardf743c032019-05-24 12:06:29 +02005693
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005694 if (4 > (size_t) (end - p)) {
5695 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
5696 }
Manuel Pégourié-Gonnardf743c032019-05-24 12:06:29 +02005697
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005698 session->ticket_lifetime = ((uint32_t) p[0] << 24) |
5699 ((uint32_t) p[1] << 16) |
5700 ((uint32_t) p[2] << 8) |
5701 ((uint32_t) p[3]);
Manuel Pégourié-Gonnardf743c032019-05-24 12:06:29 +02005702 p += 4;
Manuel Pégourié-Gonnard35eb8022019-05-16 11:11:08 +02005703#endif /* MBEDTLS_SSL_SESSION_TICKETS && MBEDTLS_SSL_CLI_C */
5704
Manuel Pégourié-Gonnardf743c032019-05-24 12:06:29 +02005705 /*
5706 * Misc extension-related info
5707 */
5708#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005709 if (1 > (size_t) (end - p)) {
5710 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
5711 }
Manuel Pégourié-Gonnardf743c032019-05-24 12:06:29 +02005712
5713 session->mfl_code = *p++;
5714#endif
5715
5716#if defined(MBEDTLS_SSL_TRUNCATED_HMAC)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005717 if (1 > (size_t) (end - p)) {
5718 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
5719 }
Manuel Pégourié-Gonnardf743c032019-05-24 12:06:29 +02005720
5721 session->trunc_hmac = *p++;
5722#endif
5723
5724#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005725 if (1 > (size_t) (end - p)) {
5726 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
5727 }
Manuel Pégourié-Gonnardf743c032019-05-24 12:06:29 +02005728
5729 session->encrypt_then_mac = *p++;
5730#endif
5731
Manuel Pégourié-Gonnard35eb8022019-05-16 11:11:08 +02005732 /* Done, should have consumed entire buffer */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005733 if (p != end) {
5734 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
5735 }
Manuel Pégourié-Gonnarda3e7c652019-05-16 10:08:35 +02005736
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005737 return 0;
Manuel Pégourié-Gonnarda3e7c652019-05-16 10:08:35 +02005738}
5739
5740/*
Manuel Pégourié-Gonnardb9dfc9f2019-07-12 10:50:19 +02005741 * Deserialize session: public wrapper for error cleaning
Manuel Pégourié-Gonnarda3d831b2019-05-23 12:28:45 +02005742 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005743int mbedtls_ssl_session_load(mbedtls_ssl_session *session,
5744 const unsigned char *buf,
5745 size_t len)
Manuel Pégourié-Gonnarda3d831b2019-05-23 12:28:45 +02005746{
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005747 int ret = ssl_session_load(session, 0, buf, len);
Manuel Pégourié-Gonnarda3d831b2019-05-23 12:28:45 +02005748
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005749 if (ret != 0) {
5750 mbedtls_ssl_session_free(session);
5751 }
Manuel Pégourié-Gonnarda3d831b2019-05-23 12:28:45 +02005752
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005753 return ret;
Manuel Pégourié-Gonnarda3d831b2019-05-23 12:28:45 +02005754}
5755
5756/*
Paul Bakker1961b702013-01-25 14:49:24 +01005757 * Perform a single step of the SSL handshake
Paul Bakker5121ce52009-01-03 21:22:43 +00005758 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005759int mbedtls_ssl_handshake_step(mbedtls_ssl_context *ssl)
Paul Bakker5121ce52009-01-03 21:22:43 +00005760{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005761 int ret = MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
Paul Bakker5121ce52009-01-03 21:22:43 +00005762
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005763 if (ssl == NULL || ssl->conf == NULL) {
5764 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
5765 }
Manuel Pégourié-Gonnardf81ee2e2015-09-01 17:43:40 +02005766
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005767#if defined(MBEDTLS_SSL_CLI_C)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005768 if (ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT) {
5769 ret = mbedtls_ssl_handshake_client_step(ssl);
5770 }
Paul Bakker5121ce52009-01-03 21:22:43 +00005771#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005772#if defined(MBEDTLS_SSL_SRV_C)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005773 if (ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER) {
5774 ret = mbedtls_ssl_handshake_server_step(ssl);
5775 }
Paul Bakker5121ce52009-01-03 21:22:43 +00005776#endif
5777
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005778 return ret;
Paul Bakker1961b702013-01-25 14:49:24 +01005779}
5780
5781/*
5782 * Perform the SSL handshake
5783 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005784int mbedtls_ssl_handshake(mbedtls_ssl_context *ssl)
Paul Bakker1961b702013-01-25 14:49:24 +01005785{
5786 int ret = 0;
5787
Hanno Beckera817ea42020-10-20 15:20:23 +01005788 /* Sanity checks */
5789
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005790 if (ssl == NULL || ssl->conf == NULL) {
5791 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
5792 }
Manuel Pégourié-Gonnardf81ee2e2015-09-01 17:43:40 +02005793
Hanno Beckera817ea42020-10-20 15:20:23 +01005794#if defined(MBEDTLS_SSL_PROTO_DTLS)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005795 if (ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
5796 (ssl->f_set_timer == NULL || ssl->f_get_timer == NULL)) {
5797 MBEDTLS_SSL_DEBUG_MSG(1, ("You must use "
5798 "mbedtls_ssl_set_timer_cb() for DTLS"));
5799 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
Hanno Beckera817ea42020-10-20 15:20:23 +01005800 }
5801#endif /* MBEDTLS_SSL_PROTO_DTLS */
5802
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005803 MBEDTLS_SSL_DEBUG_MSG(2, ("=> handshake"));
Paul Bakker1961b702013-01-25 14:49:24 +01005804
Hanno Beckera817ea42020-10-20 15:20:23 +01005805 /* Main handshake loop */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005806 while (ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER) {
5807 ret = mbedtls_ssl_handshake_step(ssl);
Paul Bakker1961b702013-01-25 14:49:24 +01005808
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005809 if (ret != 0) {
Paul Bakker1961b702013-01-25 14:49:24 +01005810 break;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005811 }
Paul Bakker1961b702013-01-25 14:49:24 +01005812 }
5813
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005814 MBEDTLS_SSL_DEBUG_MSG(2, ("<= handshake"));
Paul Bakker5121ce52009-01-03 21:22:43 +00005815
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005816 return ret;
Paul Bakker5121ce52009-01-03 21:22:43 +00005817}
5818
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005819#if defined(MBEDTLS_SSL_RENEGOTIATION)
5820#if defined(MBEDTLS_SSL_SRV_C)
Paul Bakker5121ce52009-01-03 21:22:43 +00005821/*
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01005822 * Write HelloRequest to request renegotiation on server
Paul Bakker48916f92012-09-16 19:57:18 +00005823 */
Manuel Pégourié-Gonnardd904d662022-06-17 10:24:00 +02005824MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005825static int ssl_write_hello_request(mbedtls_ssl_context *ssl)
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01005826{
Janos Follath865b3eb2019-12-16 11:46:15 +00005827 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01005828
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005829 MBEDTLS_SSL_DEBUG_MSG(2, ("=> write hello request"));
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01005830
5831 ssl->out_msglen = 4;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005832 ssl->out_msgtype = MBEDTLS_SSL_MSG_HANDSHAKE;
5833 ssl->out_msg[0] = MBEDTLS_SSL_HS_HELLO_REQUEST;
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01005834
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005835 if ((ret = mbedtls_ssl_write_handshake_msg(ssl)) != 0) {
5836 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ssl_write_handshake_msg", ret);
5837 return ret;
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01005838 }
5839
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005840 MBEDTLS_SSL_DEBUG_MSG(2, ("<= write hello request"));
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01005841
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005842 return 0;
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01005843}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005844#endif /* MBEDTLS_SSL_SRV_C */
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01005845
5846/*
5847 * Actually renegotiate current connection, triggered by either:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005848 * - any side: calling mbedtls_ssl_renegotiate(),
5849 * - client: receiving a HelloRequest during mbedtls_ssl_read(),
5850 * - server: receiving any handshake message on server during mbedtls_ssl_read() after
Manuel Pégourié-Gonnard55e4ff22014-08-19 11:16:35 +02005851 * the initial handshake is completed.
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01005852 * If the handshake doesn't complete due to waiting for I/O, it will continue
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005853 * during the next calls to mbedtls_ssl_renegotiate() or mbedtls_ssl_read() respectively.
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01005854 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005855int mbedtls_ssl_start_renegotiation(mbedtls_ssl_context *ssl)
Paul Bakker48916f92012-09-16 19:57:18 +00005856{
Janos Follath865b3eb2019-12-16 11:46:15 +00005857 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker48916f92012-09-16 19:57:18 +00005858
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005859 MBEDTLS_SSL_DEBUG_MSG(2, ("=> renegotiate"));
Paul Bakker48916f92012-09-16 19:57:18 +00005860
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005861 if ((ret = ssl_handshake_init(ssl)) != 0) {
5862 return ret;
5863 }
Paul Bakker48916f92012-09-16 19:57:18 +00005864
Manuel Pégourié-Gonnard0557bd52014-08-19 19:18:39 +02005865 /* RFC 6347 4.2.2: "[...] the HelloRequest will have message_seq = 0 and
5866 * the ServerHello will have message_seq = 1" */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005867#if defined(MBEDTLS_SSL_PROTO_DTLS)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005868 if (ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
5869 ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_PENDING) {
5870 if (ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER) {
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02005871 ssl->handshake->out_msg_seq = 1;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005872 } else {
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02005873 ssl->handshake->in_msg_seq = 1;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005874 }
Manuel Pégourié-Gonnard0557bd52014-08-19 19:18:39 +02005875 }
5876#endif
5877
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005878 ssl->state = MBEDTLS_SSL_HELLO_REQUEST;
5879 ssl->renego_status = MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS;
Paul Bakker48916f92012-09-16 19:57:18 +00005880
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005881 if ((ret = mbedtls_ssl_handshake(ssl)) != 0) {
5882 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ssl_handshake", ret);
5883 return ret;
Paul Bakker48916f92012-09-16 19:57:18 +00005884 }
5885
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005886 MBEDTLS_SSL_DEBUG_MSG(2, ("<= renegotiate"));
Paul Bakker48916f92012-09-16 19:57:18 +00005887
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005888 return 0;
Paul Bakker48916f92012-09-16 19:57:18 +00005889}
5890
5891/*
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01005892 * Renegotiate current connection on client,
5893 * or request renegotiation on server
5894 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005895int mbedtls_ssl_renegotiate(mbedtls_ssl_context *ssl)
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01005896{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005897 int ret = MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01005898
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005899 if (ssl == NULL || ssl->conf == NULL) {
5900 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
5901 }
Manuel Pégourié-Gonnardf81ee2e2015-09-01 17:43:40 +02005902
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005903#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01005904 /* On server, just send the request */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005905 if (ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER) {
5906 if (ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER) {
5907 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
5908 }
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01005909
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005910 ssl->renego_status = MBEDTLS_SSL_RENEGOTIATION_PENDING;
Manuel Pégourié-Gonnardf07f4212014-08-15 19:04:47 +02005911
5912 /* Did we already try/start sending HelloRequest? */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005913 if (ssl->out_left != 0) {
5914 return mbedtls_ssl_flush_output(ssl);
5915 }
Manuel Pégourié-Gonnardf07f4212014-08-15 19:04:47 +02005916
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005917 return ssl_write_hello_request(ssl);
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01005918 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005919#endif /* MBEDTLS_SSL_SRV_C */
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01005920
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005921#if defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01005922 /*
5923 * On client, either start the renegotiation process or,
5924 * if already in progress, continue the handshake
5925 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005926 if (ssl->renego_status != MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS) {
5927 if (ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER) {
5928 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01005929 }
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005930
5931 if ((ret = mbedtls_ssl_start_renegotiation(ssl)) != 0) {
5932 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ssl_start_renegotiation", ret);
5933 return ret;
5934 }
5935 } else {
5936 if ((ret = mbedtls_ssl_handshake(ssl)) != 0) {
5937 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ssl_handshake", ret);
5938 return ret;
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01005939 }
5940 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005941#endif /* MBEDTLS_SSL_CLI_C */
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01005942
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005943 return ret;
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01005944}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005945#endif /* MBEDTLS_SSL_RENEGOTIATION */
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01005946
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005947#if defined(MBEDTLS_X509_CRT_PARSE_C)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005948static void ssl_key_cert_free(mbedtls_ssl_key_cert *key_cert)
Manuel Pégourié-Gonnard705fcca2013-09-23 20:04:20 +02005949{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005950 mbedtls_ssl_key_cert *cur = key_cert, *next;
Manuel Pégourié-Gonnard705fcca2013-09-23 20:04:20 +02005951
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005952 while (cur != NULL) {
Manuel Pégourié-Gonnard705fcca2013-09-23 20:04:20 +02005953 next = cur->next;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005954 mbedtls_free(cur);
Manuel Pégourié-Gonnard705fcca2013-09-23 20:04:20 +02005955 cur = next;
5956 }
5957}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005958#endif /* MBEDTLS_X509_CRT_PARSE_C */
Manuel Pégourié-Gonnard705fcca2013-09-23 20:04:20 +02005959
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005960void mbedtls_ssl_handshake_free(mbedtls_ssl_context *ssl)
Paul Bakker48916f92012-09-16 19:57:18 +00005961{
Gilles Peskine9b562d52018-04-25 20:32:43 +02005962 mbedtls_ssl_handshake_params *handshake = ssl->handshake;
5963
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005964 if (handshake == NULL) {
Paul Bakkeraccaffe2014-06-26 13:37:14 +02005965 return;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005966 }
Paul Bakkeraccaffe2014-06-26 13:37:14 +02005967
Gilles Peskinedf13d5c2018-04-25 20:39:48 +02005968#if defined(MBEDTLS_SSL_ASYNC_PRIVATE)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005969 if (ssl->conf->f_async_cancel != NULL && handshake->async_in_progress != 0) {
5970 ssl->conf->f_async_cancel(ssl);
Gilles Peskinedf13d5c2018-04-25 20:39:48 +02005971 handshake->async_in_progress = 0;
5972 }
5973#endif /* MBEDTLS_SSL_ASYNC_PRIVATE */
5974
Manuel Pégourié-Gonnardb9d64e52015-07-06 14:18:56 +02005975#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1) || \
5976 defined(MBEDTLS_SSL_PROTO_TLS1_1)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005977 mbedtls_md5_free(&handshake->fin_md5);
5978 mbedtls_sha1_free(&handshake->fin_sha1);
Manuel Pégourié-Gonnardb9d64e52015-07-06 14:18:56 +02005979#endif
5980#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
5981#if defined(MBEDTLS_SHA256_C)
Andrzej Kurekeb342242019-01-29 09:14:33 -05005982#if defined(MBEDTLS_USE_PSA_CRYPTO)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005983 psa_hash_abort(&handshake->fin_sha256_psa);
Andrzej Kurekeb342242019-01-29 09:14:33 -05005984#else
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005985 mbedtls_sha256_free(&handshake->fin_sha256);
Manuel Pégourié-Gonnardb9d64e52015-07-06 14:18:56 +02005986#endif
Andrzej Kurekeb342242019-01-29 09:14:33 -05005987#endif
Gilles Peskined2d59372021-05-12 22:43:27 +02005988#if defined(MBEDTLS_SHA512_C) && !defined(MBEDTLS_SHA512_NO_SHA384)
Andrzej Kurekeb342242019-01-29 09:14:33 -05005989#if defined(MBEDTLS_USE_PSA_CRYPTO)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005990 psa_hash_abort(&handshake->fin_sha384_psa);
Andrzej Kurekeb342242019-01-29 09:14:33 -05005991#else
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005992 mbedtls_sha512_free(&handshake->fin_sha512);
Manuel Pégourié-Gonnardb9d64e52015-07-06 14:18:56 +02005993#endif
Andrzej Kurekeb342242019-01-29 09:14:33 -05005994#endif
Manuel Pégourié-Gonnardb9d64e52015-07-06 14:18:56 +02005995#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
5996
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005997#if defined(MBEDTLS_DHM_C)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005998 mbedtls_dhm_free(&handshake->dhm_ctx);
Paul Bakker48916f92012-09-16 19:57:18 +00005999#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006000#if defined(MBEDTLS_ECDH_C)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01006001 mbedtls_ecdh_free(&handshake->ecdh_ctx);
Paul Bakker61d113b2013-07-04 11:51:43 +02006002#endif
Manuel Pégourié-Gonnardeef142d2015-09-16 10:05:04 +02006003#if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01006004 mbedtls_ecjpake_free(&handshake->ecjpake_ctx);
Manuel Pégourié-Gonnard77c06462015-09-17 13:59:49 +02006005#if defined(MBEDTLS_SSL_CLI_C)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01006006 mbedtls_free(handshake->ecjpake_cache);
Manuel Pégourié-Gonnard77c06462015-09-17 13:59:49 +02006007 handshake->ecjpake_cache = NULL;
6008 handshake->ecjpake_cache_len = 0;
6009#endif
Manuel Pégourié-Gonnard76cfd3f2015-09-15 12:10:54 +02006010#endif
Paul Bakker61d113b2013-07-04 11:51:43 +02006011
Janos Follath4ae5c292016-02-10 11:27:43 +00006012#if defined(MBEDTLS_ECDH_C) || defined(MBEDTLS_ECDSA_C) || \
6013 defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
Paul Bakker9af723c2014-05-01 13:03:14 +02006014 /* explicit void pointer cast for buggy MS compiler */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01006015 mbedtls_free((void *) handshake->curves);
Manuel Pégourié-Gonnardd09453c2013-09-23 19:11:32 +02006016#endif
6017
Gilles Peskineeccd8882020-03-10 12:19:08 +01006018#if defined(MBEDTLS_KEY_EXCHANGE_SOME_PSK_ENABLED)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01006019 if (handshake->psk != NULL) {
6020 mbedtls_platform_zeroize(handshake->psk, handshake->psk_len);
6021 mbedtls_free(handshake->psk);
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01006022 }
6023#endif
6024
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006025#if defined(MBEDTLS_X509_CRT_PARSE_C) && \
6026 defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
Manuel Pégourié-Gonnard83724542013-09-24 22:30:56 +02006027 /*
6028 * Free only the linked list wrapper, not the keys themselves
6029 * since the belong to the SNI callback
6030 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01006031 if (handshake->sni_key_cert != NULL) {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006032 mbedtls_ssl_key_cert *cur = handshake->sni_key_cert, *next;
Manuel Pégourié-Gonnard83724542013-09-24 22:30:56 +02006033
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01006034 while (cur != NULL) {
Manuel Pégourié-Gonnard83724542013-09-24 22:30:56 +02006035 next = cur->next;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01006036 mbedtls_free(cur);
Manuel Pégourié-Gonnard83724542013-09-24 22:30:56 +02006037 cur = next;
6038 }
6039 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006040#endif /* MBEDTLS_X509_CRT_PARSE_C && MBEDTLS_SSL_SERVER_NAME_INDICATION */
Manuel Pégourié-Gonnard705fcca2013-09-23 20:04:20 +02006041
Gilles Peskineeccd8882020-03-10 12:19:08 +01006042#if defined(MBEDTLS_SSL_ECP_RESTARTABLE_ENABLED)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01006043 mbedtls_x509_crt_restart_free(&handshake->ecrs_ctx);
6044 if (handshake->ecrs_peer_cert != NULL) {
6045 mbedtls_x509_crt_free(handshake->ecrs_peer_cert);
6046 mbedtls_free(handshake->ecrs_peer_cert);
Hanno Becker3dad3112019-02-05 17:19:52 +00006047 }
Manuel Pégourié-Gonnard862cde52017-05-17 11:56:15 +02006048#endif
6049
Hanno Becker75173122019-02-06 16:18:31 +00006050#if defined(MBEDTLS_X509_CRT_PARSE_C) && \
6051 !defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01006052 mbedtls_pk_free(&handshake->peer_pubkey);
Hanno Becker75173122019-02-06 16:18:31 +00006053#endif /* MBEDTLS_X509_CRT_PARSE_C && !MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
6054
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006055#if defined(MBEDTLS_SSL_PROTO_DTLS)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01006056 mbedtls_free(handshake->verify_cookie);
6057 mbedtls_ssl_flight_free(handshake->flight);
6058 mbedtls_ssl_buffering_free(ssl);
Manuel Pégourié-Gonnard74848812014-07-11 02:43:49 +02006059#endif
6060
Hanno Becker4a63ed42019-01-08 11:39:35 +00006061#if defined(MBEDTLS_ECDH_C) && \
6062 defined(MBEDTLS_USE_PSA_CRYPTO)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01006063 psa_destroy_key(handshake->ecdh_psa_privkey);
Hanno Becker4a63ed42019-01-08 11:39:35 +00006064#endif /* MBEDTLS_ECDH_C && MBEDTLS_USE_PSA_CRYPTO */
6065
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01006066 mbedtls_platform_zeroize(handshake,
6067 sizeof(mbedtls_ssl_handshake_params));
Andrzej Kurek0afa2a12020-03-03 10:39:58 -05006068
6069#if defined(MBEDTLS_SSL_VARIABLE_BUFFER_LENGTH)
6070 /* If the buffers are too big - reallocate. Because of the way Mbed TLS
6071 * processes datagrams and the fact that a datagram is allowed to have
6072 * several records in it, it is possible that the I/O buffers are not
6073 * empty at this stage */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01006074 handle_buffer_resizing(ssl, 1, mbedtls_ssl_get_input_buflen(ssl),
6075 mbedtls_ssl_get_output_buflen(ssl));
Andrzej Kurek0afa2a12020-03-03 10:39:58 -05006076#endif
Paul Bakker48916f92012-09-16 19:57:18 +00006077}
6078
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01006079void mbedtls_ssl_session_free(mbedtls_ssl_session *session)
Paul Bakker48916f92012-09-16 19:57:18 +00006080{
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01006081 if (session == NULL) {
Paul Bakkeraccaffe2014-06-26 13:37:14 +02006082 return;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01006083 }
Paul Bakkeraccaffe2014-06-26 13:37:14 +02006084
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006085#if defined(MBEDTLS_X509_CRT_PARSE_C)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01006086 ssl_clear_peer_cert(session);
Paul Bakkered27a042013-04-18 22:46:23 +02006087#endif
Paul Bakker0a597072012-09-25 21:55:46 +00006088
Manuel Pégourié-Gonnardb596abf2015-05-20 10:45:29 +02006089#if defined(MBEDTLS_SSL_SESSION_TICKETS) && defined(MBEDTLS_SSL_CLI_C)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01006090 mbedtls_free(session->ticket);
Paul Bakkera503a632013-08-14 13:48:06 +02006091#endif
Manuel Pégourié-Gonnard75d44012013-08-02 14:44:04 +02006092
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01006093 mbedtls_platform_zeroize(session, sizeof(mbedtls_ssl_session));
Paul Bakker48916f92012-09-16 19:57:18 +00006094}
6095
Manuel Pégourié-Gonnard5c0e3772019-07-23 16:13:17 +02006096#if defined(MBEDTLS_SSL_CONTEXT_SERIALIZATION)
Manuel Pégourié-Gonnard4e9370b2019-07-23 16:31:16 +02006097
6098#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
6099#define SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_CONNECTION_ID 1u
6100#else
6101#define SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_CONNECTION_ID 0u
6102#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
6103
6104#if defined(MBEDTLS_SSL_DTLS_BADMAC_LIMIT)
6105#define SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_BADMAC_LIMIT 1u
6106#else
6107#define SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_BADMAC_LIMIT 0u
6108#endif /* MBEDTLS_SSL_DTLS_BADMAC_LIMIT */
6109
6110#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
6111#define SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_ANTI_REPLAY 1u
6112#else
6113#define SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_ANTI_REPLAY 0u
6114#endif /* MBEDTLS_SSL_DTLS_ANTI_REPLAY */
6115
6116#if defined(MBEDTLS_SSL_ALPN)
6117#define SSL_SERIALIZED_CONTEXT_CONFIG_ALPN 1u
6118#else
6119#define SSL_SERIALIZED_CONTEXT_CONFIG_ALPN 0u
6120#endif /* MBEDTLS_SSL_ALPN */
6121
6122#define SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_CONNECTION_ID_BIT 0
6123#define SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_BADMAC_LIMIT_BIT 1
6124#define SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_ANTI_REPLAY_BIT 2
6125#define SSL_SERIALIZED_CONTEXT_CONFIG_ALPN_BIT 3
6126
6127#define SSL_SERIALIZED_CONTEXT_CONFIG_BITFLAG \
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01006128 ((uint32_t) ( \
6129 (SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_CONNECTION_ID << \
6130 SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_CONNECTION_ID_BIT) | \
6131 (SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_BADMAC_LIMIT << \
6132 SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_BADMAC_LIMIT_BIT) | \
6133 (SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_ANTI_REPLAY << \
6134 SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_ANTI_REPLAY_BIT) | \
6135 (SSL_SERIALIZED_CONTEXT_CONFIG_ALPN << SSL_SERIALIZED_CONTEXT_CONFIG_ALPN_BIT) | \
6136 0u))
Manuel Pégourié-Gonnard4e9370b2019-07-23 16:31:16 +02006137
Manuel Pégourié-Gonnard4c90e852019-07-11 10:58:10 +02006138static unsigned char ssl_serialized_context_header[] = {
6139 MBEDTLS_VERSION_MAJOR,
6140 MBEDTLS_VERSION_MINOR,
6141 MBEDTLS_VERSION_PATCH,
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01006142 MBEDTLS_BYTE_1(SSL_SERIALIZED_SESSION_CONFIG_BITFLAG),
6143 MBEDTLS_BYTE_0(SSL_SERIALIZED_SESSION_CONFIG_BITFLAG),
6144 MBEDTLS_BYTE_2(SSL_SERIALIZED_CONTEXT_CONFIG_BITFLAG),
6145 MBEDTLS_BYTE_1(SSL_SERIALIZED_CONTEXT_CONFIG_BITFLAG),
6146 MBEDTLS_BYTE_0(SSL_SERIALIZED_CONTEXT_CONFIG_BITFLAG),
Manuel Pégourié-Gonnard4c90e852019-07-11 10:58:10 +02006147};
6148
Paul Bakker5121ce52009-01-03 21:22:43 +00006149/*
Manuel Pégourié-Gonnardac87e282019-05-28 13:02:16 +02006150 * Serialize a full SSL context
Manuel Pégourié-Gonnard00400c22019-07-10 14:58:45 +02006151 *
6152 * The format of the serialized data is:
6153 * (in the presentation language of TLS, RFC 8446 section 3)
6154 *
6155 * // header
6156 * opaque mbedtls_version[3]; // major, minor, patch
Manuel Pégourié-Gonnard4c90e852019-07-11 10:58:10 +02006157 * opaque context_format[5]; // version-specific field determining
Manuel Pégourié-Gonnard00400c22019-07-10 14:58:45 +02006158 * // the format of the remaining
Manuel Pégourié-Gonnard4c90e852019-07-11 10:58:10 +02006159 * // serialized data.
Manuel Pégourié-Gonnard4e9370b2019-07-23 16:31:16 +02006160 * Note: When updating the format, remember to keep these
6161 * version+format bytes. (We may make their size part of the API.)
Manuel Pégourié-Gonnard00400c22019-07-10 14:58:45 +02006162 *
6163 * // session sub-structure
6164 * opaque session<1..2^32-1>; // see mbedtls_ssl_session_save()
6165 * // transform sub-structure
6166 * uint8 random[64]; // ServerHello.random+ClientHello.random
6167 * uint8 in_cid<0..2^8-1> // Connection ID: expected incoming value
6168 * uint8 out_cid<0..2^8-1> // Connection ID: outgoing value to use
6169 * // fields from ssl_context
6170 * uint32 badmac_seen; // DTLS: number of records with failing MAC
6171 * uint64 in_window_top; // DTLS: last validated record seq_num
6172 * uint64 in_window; // DTLS: bitmask for replay protection
6173 * uint8 disable_datagram_packing; // DTLS: only one record per datagram
6174 * uint64 cur_out_ctr; // Record layer: outgoing sequence number
6175 * uint16 mtu; // DTLS: path mtu (max outgoing fragment size)
6176 * uint8 alpn_chosen<0..2^8-1> // ALPN: negotiated application protocol
6177 *
6178 * Note that many fields of the ssl_context or sub-structures are not
6179 * serialized, as they fall in one of the following categories:
6180 *
6181 * 1. forced value (eg in_left must be 0)
6182 * 2. pointer to dynamically-allocated memory (eg session, transform)
6183 * 3. value can be re-derived from other data (eg session keys from MS)
6184 * 4. value was temporary (eg content of input buffer)
6185 * 5. value will be provided by the user again (eg I/O callbacks and context)
Manuel Pégourié-Gonnardac87e282019-05-28 13:02:16 +02006186 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01006187int mbedtls_ssl_context_save(mbedtls_ssl_context *ssl,
6188 unsigned char *buf,
6189 size_t buf_len,
6190 size_t *olen)
Manuel Pégourié-Gonnardac87e282019-05-28 13:02:16 +02006191{
Manuel Pégourié-Gonnard4c90e852019-07-11 10:58:10 +02006192 unsigned char *p = buf;
6193 size_t used = 0;
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02006194 size_t session_len;
6195 int ret = 0;
Manuel Pégourié-Gonnard4c90e852019-07-11 10:58:10 +02006196
Manuel Pégourié-Gonnard1aaf6692019-07-10 14:14:05 +02006197 /*
Manuel Pégourié-Gonnarde4588692019-07-29 12:28:52 +02006198 * Enforce usage restrictions, see "return BAD_INPUT_DATA" in
6199 * this function's documentation.
6200 *
6201 * These are due to assumptions/limitations in the implementation. Some of
6202 * them are likely to stay (no handshake in progress) some might go away
6203 * (only DTLS) but are currently used to simplify the implementation.
Manuel Pégourié-Gonnard1aaf6692019-07-10 14:14:05 +02006204 */
Manuel Pégourié-Gonnarde4588692019-07-29 12:28:52 +02006205 /* The initial handshake must be over */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01006206 if (ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER) {
6207 MBEDTLS_SSL_DEBUG_MSG(1, ("Initial handshake isn't over"));
6208 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
Jarno Lamsa8c51b7c2019-08-21 13:45:05 +03006209 }
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01006210 if (ssl->handshake != NULL) {
6211 MBEDTLS_SSL_DEBUG_MSG(1, ("Handshake isn't completed"));
6212 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
Jarno Lamsa8c51b7c2019-08-21 13:45:05 +03006213 }
Manuel Pégourié-Gonnarde4588692019-07-29 12:28:52 +02006214 /* Double-check that sub-structures are indeed ready */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01006215 if (ssl->transform == NULL || ssl->session == NULL) {
6216 MBEDTLS_SSL_DEBUG_MSG(1, ("Serialised structures aren't ready"));
6217 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
Jarno Lamsa8c51b7c2019-08-21 13:45:05 +03006218 }
Manuel Pégourié-Gonnarde4588692019-07-29 12:28:52 +02006219 /* There must be no pending incoming or outgoing data */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01006220 if (mbedtls_ssl_check_pending(ssl) != 0) {
6221 MBEDTLS_SSL_DEBUG_MSG(1, ("There is pending incoming data"));
6222 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
Jarno Lamsa8c51b7c2019-08-21 13:45:05 +03006223 }
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01006224 if (ssl->out_left != 0) {
6225 MBEDTLS_SSL_DEBUG_MSG(1, ("There is pending outgoing data"));
6226 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
Jarno Lamsa8c51b7c2019-08-21 13:45:05 +03006227 }
Dave Rodgmana03396a2022-12-06 16:30:38 +00006228 /* Protocol must be DTLS, not TLS */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01006229 if (ssl->conf->transport != MBEDTLS_SSL_TRANSPORT_DATAGRAM) {
6230 MBEDTLS_SSL_DEBUG_MSG(1, ("Only DTLS is supported"));
6231 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
Jarno Lamsa8c51b7c2019-08-21 13:45:05 +03006232 }
Manuel Pégourié-Gonnarde4588692019-07-29 12:28:52 +02006233 /* Version must be 1.2 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01006234 if (ssl->major_ver != MBEDTLS_SSL_MAJOR_VERSION_3) {
6235 MBEDTLS_SSL_DEBUG_MSG(1, ("Only version 1.2 supported"));
6236 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
Jarno Lamsa8c51b7c2019-08-21 13:45:05 +03006237 }
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01006238 if (ssl->minor_ver != MBEDTLS_SSL_MINOR_VERSION_3) {
6239 MBEDTLS_SSL_DEBUG_MSG(1, ("Only version 1.2 supported"));
6240 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
Jarno Lamsa8c51b7c2019-08-21 13:45:05 +03006241 }
Manuel Pégourié-Gonnarde4588692019-07-29 12:28:52 +02006242 /* We must be using an AEAD ciphersuite */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01006243 if (mbedtls_ssl_transform_uses_aead(ssl->transform) != 1) {
6244 MBEDTLS_SSL_DEBUG_MSG(1, ("Only AEAD ciphersuites supported"));
6245 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
Jarno Lamsa8c51b7c2019-08-21 13:45:05 +03006246 }
Manuel Pégourié-Gonnarde4588692019-07-29 12:28:52 +02006247 /* Renegotiation must not be enabled */
6248#if defined(MBEDTLS_SSL_RENEGOTIATION)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01006249 if (ssl->conf->disable_renegotiation != MBEDTLS_SSL_RENEGOTIATION_DISABLED) {
6250 MBEDTLS_SSL_DEBUG_MSG(1, ("Renegotiation must not be enabled"));
6251 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
Jarno Lamsa8c51b7c2019-08-21 13:45:05 +03006252 }
Manuel Pégourié-Gonnarde4588692019-07-29 12:28:52 +02006253#endif
Manuel Pégourié-Gonnardac87e282019-05-28 13:02:16 +02006254
Manuel Pégourié-Gonnard4c90e852019-07-11 10:58:10 +02006255 /*
6256 * Version and format identifier
6257 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01006258 used += sizeof(ssl_serialized_context_header);
Manuel Pégourié-Gonnardac87e282019-05-28 13:02:16 +02006259
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01006260 if (used <= buf_len) {
6261 memcpy(p, ssl_serialized_context_header,
6262 sizeof(ssl_serialized_context_header));
6263 p += sizeof(ssl_serialized_context_header);
Manuel Pégourié-Gonnard4c90e852019-07-11 10:58:10 +02006264 }
6265
6266 /*
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02006267 * Session (length + data)
6268 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01006269 ret = ssl_session_save(ssl->session, 1, NULL, 0, &session_len);
6270 if (ret != MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL) {
6271 return ret;
6272 }
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02006273
6274 used += 4 + session_len;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01006275 if (used <= buf_len) {
6276 MBEDTLS_PUT_UINT32_BE(session_len, p, 0);
Joe Subbiani2f98d792021-08-20 11:44:44 +01006277 p += 4;
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02006278
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01006279 ret = ssl_session_save(ssl->session, 1,
6280 p, session_len, &session_len);
6281 if (ret != 0) {
6282 return ret;
6283 }
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02006284
6285 p += session_len;
6286 }
6287
6288 /*
Manuel Pégourié-Gonnardc2a7b892019-07-15 09:04:11 +02006289 * Transform
6290 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01006291 used += sizeof(ssl->transform->randbytes);
6292 if (used <= buf_len) {
6293 memcpy(p, ssl->transform->randbytes,
6294 sizeof(ssl->transform->randbytes));
6295 p += sizeof(ssl->transform->randbytes);
Manuel Pégourié-Gonnardc2a7b892019-07-15 09:04:11 +02006296 }
6297
6298#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
6299 used += 2 + ssl->transform->in_cid_len + ssl->transform->out_cid_len;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01006300 if (used <= buf_len) {
Manuel Pégourié-Gonnardc2a7b892019-07-15 09:04:11 +02006301 *p++ = ssl->transform->in_cid_len;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01006302 memcpy(p, ssl->transform->in_cid, ssl->transform->in_cid_len);
Manuel Pégourié-Gonnardc2a7b892019-07-15 09:04:11 +02006303 p += ssl->transform->in_cid_len;
6304
6305 *p++ = ssl->transform->out_cid_len;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01006306 memcpy(p, ssl->transform->out_cid, ssl->transform->out_cid_len);
Manuel Pégourié-Gonnardc2a7b892019-07-15 09:04:11 +02006307 p += ssl->transform->out_cid_len;
6308 }
6309#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
6310
6311 /*
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02006312 * Saved fields from top-level ssl_context structure
6313 */
6314#if defined(MBEDTLS_SSL_DTLS_BADMAC_LIMIT)
6315 used += 4;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01006316 if (used <= buf_len) {
6317 MBEDTLS_PUT_UINT32_BE(ssl->badmac_seen, p, 0);
Joe Subbiani2f98d792021-08-20 11:44:44 +01006318 p += 4;
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02006319 }
6320#endif /* MBEDTLS_SSL_DTLS_BADMAC_LIMIT */
6321
6322#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
6323 used += 16;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01006324 if (used <= buf_len) {
6325 MBEDTLS_PUT_UINT64_BE(ssl->in_window_top, p, 0);
Joe Subbiani2f98d792021-08-20 11:44:44 +01006326 p += 8;
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02006327
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01006328 MBEDTLS_PUT_UINT64_BE(ssl->in_window, p, 0);
Joe Subbiani2f98d792021-08-20 11:44:44 +01006329 p += 8;
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02006330 }
6331#endif /* MBEDTLS_SSL_DTLS_ANTI_REPLAY */
6332
6333#if defined(MBEDTLS_SSL_PROTO_DTLS)
6334 used += 1;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01006335 if (used <= buf_len) {
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02006336 *p++ = ssl->disable_datagram_packing;
6337 }
6338#endif /* MBEDTLS_SSL_PROTO_DTLS */
6339
6340 used += 8;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01006341 if (used <= buf_len) {
6342 memcpy(p, ssl->cur_out_ctr, 8);
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02006343 p += 8;
6344 }
6345
6346#if defined(MBEDTLS_SSL_PROTO_DTLS)
6347 used += 2;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01006348 if (used <= buf_len) {
6349 MBEDTLS_PUT_UINT16_BE(ssl->mtu, p, 0);
Joe Subbiani2f98d792021-08-20 11:44:44 +01006350 p += 2;
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02006351 }
6352#endif /* MBEDTLS_SSL_PROTO_DTLS */
6353
6354#if defined(MBEDTLS_SSL_ALPN)
6355 {
6356 const uint8_t alpn_len = ssl->alpn_chosen
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01006357 ? (uint8_t) strlen(ssl->alpn_chosen)
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02006358 : 0;
6359
6360 used += 1 + alpn_len;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01006361 if (used <= buf_len) {
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02006362 *p++ = alpn_len;
6363
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01006364 if (ssl->alpn_chosen != NULL) {
6365 memcpy(p, ssl->alpn_chosen, alpn_len);
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02006366 p += alpn_len;
6367 }
6368 }
6369 }
6370#endif /* MBEDTLS_SSL_ALPN */
6371
6372 /*
Manuel Pégourié-Gonnard4c90e852019-07-11 10:58:10 +02006373 * Done
6374 */
6375 *olen = used;
6376
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01006377 if (used > buf_len) {
6378 return MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL;
6379 }
Manuel Pégourié-Gonnardac87e282019-05-28 13:02:16 +02006380
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01006381 MBEDTLS_SSL_DEBUG_BUF(4, "saved context", buf, used);
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02006382
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01006383 return mbedtls_ssl_session_reset_int(ssl, 0);
Manuel Pégourié-Gonnardac87e282019-05-28 13:02:16 +02006384}
6385
6386/*
Manuel Pégourié-Gonnardc2a7b892019-07-15 09:04:11 +02006387 * Helper to get TLS 1.2 PRF from ciphersuite
6388 * (Duplicates bits of logic from ssl_set_handshake_prfs().)
6389 */
Andrzej Kurekf9412f72022-10-18 07:30:19 -04006390#if defined(MBEDTLS_SHA256_C) || \
6391 (defined(MBEDTLS_SHA512_C) && !defined(MBEDTLS_SHA512_NO_SHA384))
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01006392typedef int (*tls_prf_fn)(const unsigned char *secret, size_t slen,
6393 const char *label,
6394 const unsigned char *random, size_t rlen,
6395 unsigned char *dstbuf, size_t dlen);
6396static tls_prf_fn ssl_tls12prf_from_cs(int ciphersuite_id)
Manuel Pégourié-Gonnardc2a7b892019-07-15 09:04:11 +02006397{
6398 const mbedtls_ssl_ciphersuite_t * const ciphersuite_info =
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01006399 mbedtls_ssl_ciphersuite_from_id(ciphersuite_id);
Manuel Pégourié-Gonnardc2a7b892019-07-15 09:04:11 +02006400
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01006401 if (ciphersuite_info == NULL) {
6402 return NULL;
6403 }
Andrzej Kurek84fc52c2022-10-25 04:18:30 -04006404
6405#if defined(MBEDTLS_SHA512_C) && !defined(MBEDTLS_SHA512_NO_SHA384)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01006406 if (ciphersuite_info->mac == MBEDTLS_MD_SHA384) {
6407 return tls_prf_sha384;
6408 } else
Manuel Pégourié-Gonnard9a96fd72019-07-23 17:11:24 +02006409#endif
Andrzej Kurekf9412f72022-10-18 07:30:19 -04006410#if defined(MBEDTLS_SHA256_C)
6411 {
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01006412 if (ciphersuite_info->mac == MBEDTLS_MD_SHA256) {
6413 return tls_prf_sha256;
6414 }
Andrzej Kurekf9412f72022-10-18 07:30:19 -04006415 }
6416#endif
6417#if !defined(MBEDTLS_SHA256_C) && \
6418 (!defined(MBEDTLS_SHA512_C) || defined(MBEDTLS_SHA512_NO_SHA384))
6419 (void) ciphersuite_info;
6420#endif
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01006421 return NULL;
Manuel Pégourié-Gonnardc2a7b892019-07-15 09:04:11 +02006422}
6423
Andrzej Kurekf9412f72022-10-18 07:30:19 -04006424#endif /* MBEDTLS_SHA256_C ||
6425 (MBEDTLS_SHA512_C && !MBEDTLS_SHA512_NO_SHA384) */
6426
Manuel Pégourié-Gonnardc2a7b892019-07-15 09:04:11 +02006427/*
Manuel Pégourié-Gonnardb9dfc9f2019-07-12 10:50:19 +02006428 * Deserialize context, see mbedtls_ssl_context_save() for format.
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02006429 *
6430 * This internal version is wrapped by a public function that cleans up in
6431 * case of error.
Manuel Pégourié-Gonnardac87e282019-05-28 13:02:16 +02006432 */
Manuel Pégourié-Gonnardd904d662022-06-17 10:24:00 +02006433MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01006434static int ssl_context_load(mbedtls_ssl_context *ssl,
6435 const unsigned char *buf,
6436 size_t len)
Manuel Pégourié-Gonnardac87e282019-05-28 13:02:16 +02006437{
Manuel Pégourié-Gonnard4c90e852019-07-11 10:58:10 +02006438 const unsigned char *p = buf;
6439 const unsigned char * const end = buf + len;
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02006440 size_t session_len;
Janos Follath865b3eb2019-12-16 11:46:15 +00006441 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Andrzej Kurekf9412f72022-10-18 07:30:19 -04006442 tls_prf_fn prf_func = NULL;
Manuel Pégourié-Gonnard4c90e852019-07-11 10:58:10 +02006443
Manuel Pégourié-Gonnard0ff76402019-07-11 09:56:30 +02006444 /*
6445 * The context should have been freshly setup or reset.
6446 * Give the user an error in case of obvious misuse.
Manuel Pégourié-Gonnard4ca930f2019-07-26 16:31:53 +02006447 * (Checking session is useful because it won't be NULL if we're
Manuel Pégourié-Gonnard0ff76402019-07-11 09:56:30 +02006448 * renegotiating, or if the user mistakenly loaded a session first.)
6449 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01006450 if (ssl->state != MBEDTLS_SSL_HELLO_REQUEST ||
6451 ssl->session != NULL) {
6452 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
Manuel Pégourié-Gonnard0ff76402019-07-11 09:56:30 +02006453 }
6454
6455 /*
6456 * We can't check that the config matches the initial one, but we can at
6457 * least check it matches the requirements for serializing.
6458 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01006459 if (ssl->conf->transport != MBEDTLS_SSL_TRANSPORT_DATAGRAM ||
Manuel Pégourié-Gonnard0ff76402019-07-11 09:56:30 +02006460 ssl->conf->max_major_ver < MBEDTLS_SSL_MAJOR_VERSION_3 ||
6461 ssl->conf->min_major_ver > MBEDTLS_SSL_MAJOR_VERSION_3 ||
6462 ssl->conf->max_minor_ver < MBEDTLS_SSL_MINOR_VERSION_3 ||
6463 ssl->conf->min_minor_ver > MBEDTLS_SSL_MINOR_VERSION_3 ||
6464#if defined(MBEDTLS_SSL_RENEGOTIATION)
Manuel Pégourié-Gonnard9a96fd72019-07-23 17:11:24 +02006465 ssl->conf->disable_renegotiation != MBEDTLS_SSL_RENEGOTIATION_DISABLED ||
Manuel Pégourié-Gonnard0ff76402019-07-11 09:56:30 +02006466#endif
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01006467 0) {
6468 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
Manuel Pégourié-Gonnard0ff76402019-07-11 09:56:30 +02006469 }
6470
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01006471 MBEDTLS_SSL_DEBUG_BUF(4, "context to load", buf, len);
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02006472
Manuel Pégourié-Gonnard4c90e852019-07-11 10:58:10 +02006473 /*
6474 * Check version identifier
6475 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01006476 if ((size_t) (end - p) < sizeof(ssl_serialized_context_header)) {
6477 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
Manuel Pégourié-Gonnard4c90e852019-07-11 10:58:10 +02006478 }
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01006479
6480 if (memcmp(p, ssl_serialized_context_header,
6481 sizeof(ssl_serialized_context_header)) != 0) {
6482 return MBEDTLS_ERR_SSL_VERSION_MISMATCH;
6483 }
6484 p += sizeof(ssl_serialized_context_header);
Manuel Pégourié-Gonnard4c90e852019-07-11 10:58:10 +02006485
6486 /*
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02006487 * Session
6488 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01006489 if ((size_t) (end - p) < 4) {
6490 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
6491 }
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02006492
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01006493 session_len = ((size_t) p[0] << 24) |
6494 ((size_t) p[1] << 16) |
6495 ((size_t) p[2] << 8) |
6496 ((size_t) p[3]);
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02006497 p += 4;
6498
Manuel Pégourié-Gonnard142ba732019-07-23 14:43:30 +02006499 /* This has been allocated by ssl_handshake_init(), called by
Hanno Becker43aefe22020-02-05 10:44:56 +00006500 * by either mbedtls_ssl_session_reset_int() or mbedtls_ssl_setup(). */
Manuel Pégourié-Gonnard142ba732019-07-23 14:43:30 +02006501 ssl->session = ssl->session_negotiate;
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02006502 ssl->session_in = ssl->session;
6503 ssl->session_out = ssl->session;
Manuel Pégourié-Gonnard142ba732019-07-23 14:43:30 +02006504 ssl->session_negotiate = NULL;
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02006505
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01006506 if ((size_t) (end - p) < session_len) {
6507 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
6508 }
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02006509
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01006510 ret = ssl_session_load(ssl->session, 1, p, session_len);
6511 if (ret != 0) {
6512 mbedtls_ssl_session_free(ssl->session);
6513 return ret;
Manuel Pégourié-Gonnard45ac1f02019-07-23 16:52:45 +02006514 }
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02006515
6516 p += session_len;
6517
6518 /*
Manuel Pégourié-Gonnardc2a7b892019-07-15 09:04:11 +02006519 * Transform
6520 */
6521
Manuel Pégourié-Gonnard142ba732019-07-23 14:43:30 +02006522 /* This has been allocated by ssl_handshake_init(), called by
Hanno Becker43aefe22020-02-05 10:44:56 +00006523 * by either mbedtls_ssl_session_reset_int() or mbedtls_ssl_setup(). */
Manuel Pégourié-Gonnard142ba732019-07-23 14:43:30 +02006524 ssl->transform = ssl->transform_negotiate;
Manuel Pégourié-Gonnardc2a7b892019-07-15 09:04:11 +02006525 ssl->transform_in = ssl->transform;
6526 ssl->transform_out = ssl->transform;
Manuel Pégourié-Gonnard142ba732019-07-23 14:43:30 +02006527 ssl->transform_negotiate = NULL;
Manuel Pégourié-Gonnardc2a7b892019-07-15 09:04:11 +02006528
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01006529 prf_func = ssl_tls12prf_from_cs(ssl->session->ciphersuite);
6530 if (prf_func == NULL) {
6531 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
6532 }
Andrzej Kurekf9412f72022-10-18 07:30:19 -04006533
Manuel Pégourié-Gonnardc2a7b892019-07-15 09:04:11 +02006534 /* Read random bytes and populate structure */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01006535 if ((size_t) (end - p) < sizeof(ssl->transform->randbytes)) {
6536 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
6537 }
Manuel Pégourié-Gonnardc2a7b892019-07-15 09:04:11 +02006538
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01006539 ret = ssl_populate_transform(ssl->transform,
6540 ssl->session->ciphersuite,
6541 ssl->session->master,
Manuel Pégourié-Gonnardc2a7b892019-07-15 09:04:11 +02006542#if defined(MBEDTLS_SSL_SOME_MODES_USE_MAC)
6543#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01006544 ssl->session->encrypt_then_mac,
Manuel Pégourié-Gonnardc2a7b892019-07-15 09:04:11 +02006545#endif
6546#if defined(MBEDTLS_SSL_TRUNCATED_HMAC)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01006547 ssl->session->trunc_hmac,
Manuel Pégourié-Gonnardc2a7b892019-07-15 09:04:11 +02006548#endif
6549#endif /* MBEDTLS_SSL_SOME_MODES_USE_MAC */
6550#if defined(MBEDTLS_ZLIB_SUPPORT)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01006551 ssl->session->compression,
Manuel Pégourié-Gonnardc2a7b892019-07-15 09:04:11 +02006552#endif
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01006553 prf_func,
6554 p, /* currently pointing to randbytes */
6555 MBEDTLS_SSL_MINOR_VERSION_3, /* (D)TLS 1.2 is forced */
6556 ssl->conf->endpoint,
6557 ssl);
6558 if (ret != 0) {
6559 return ret;
6560 }
Manuel Pégourié-Gonnardc2a7b892019-07-15 09:04:11 +02006561
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01006562 p += sizeof(ssl->transform->randbytes);
Manuel Pégourié-Gonnardc2a7b892019-07-15 09:04:11 +02006563
6564#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
6565 /* Read connection IDs and store them */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01006566 if ((size_t) (end - p) < 1) {
6567 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
6568 }
Manuel Pégourié-Gonnardc2a7b892019-07-15 09:04:11 +02006569
6570 ssl->transform->in_cid_len = *p++;
6571
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01006572 if ((size_t) (end - p) < ssl->transform->in_cid_len + 1u) {
6573 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
6574 }
Manuel Pégourié-Gonnardc2a7b892019-07-15 09:04:11 +02006575
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01006576 memcpy(ssl->transform->in_cid, p, ssl->transform->in_cid_len);
Manuel Pégourié-Gonnardc2a7b892019-07-15 09:04:11 +02006577 p += ssl->transform->in_cid_len;
6578
6579 ssl->transform->out_cid_len = *p++;
6580
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01006581 if ((size_t) (end - p) < ssl->transform->out_cid_len) {
6582 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
6583 }
Manuel Pégourié-Gonnardc2a7b892019-07-15 09:04:11 +02006584
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01006585 memcpy(ssl->transform->out_cid, p, ssl->transform->out_cid_len);
Manuel Pégourié-Gonnardc2a7b892019-07-15 09:04:11 +02006586 p += ssl->transform->out_cid_len;
6587#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
6588
6589 /*
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02006590 * Saved fields from top-level ssl_context structure
6591 */
6592#if defined(MBEDTLS_SSL_DTLS_BADMAC_LIMIT)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01006593 if ((size_t) (end - p) < 4) {
6594 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
6595 }
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02006596
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01006597 ssl->badmac_seen = ((uint32_t) p[0] << 24) |
6598 ((uint32_t) p[1] << 16) |
6599 ((uint32_t) p[2] << 8) |
6600 ((uint32_t) p[3]);
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02006601 p += 4;
6602#endif /* MBEDTLS_SSL_DTLS_BADMAC_LIMIT */
6603
6604#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01006605 if ((size_t) (end - p) < 16) {
6606 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
6607 }
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02006608
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01006609 ssl->in_window_top = ((uint64_t) p[0] << 56) |
6610 ((uint64_t) p[1] << 48) |
6611 ((uint64_t) p[2] << 40) |
6612 ((uint64_t) p[3] << 32) |
6613 ((uint64_t) p[4] << 24) |
6614 ((uint64_t) p[5] << 16) |
6615 ((uint64_t) p[6] << 8) |
6616 ((uint64_t) p[7]);
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02006617 p += 8;
6618
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01006619 ssl->in_window = ((uint64_t) p[0] << 56) |
6620 ((uint64_t) p[1] << 48) |
6621 ((uint64_t) p[2] << 40) |
6622 ((uint64_t) p[3] << 32) |
6623 ((uint64_t) p[4] << 24) |
6624 ((uint64_t) p[5] << 16) |
6625 ((uint64_t) p[6] << 8) |
6626 ((uint64_t) p[7]);
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02006627 p += 8;
6628#endif /* MBEDTLS_SSL_DTLS_ANTI_REPLAY */
6629
6630#if defined(MBEDTLS_SSL_PROTO_DTLS)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01006631 if ((size_t) (end - p) < 1) {
6632 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
6633 }
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02006634
6635 ssl->disable_datagram_packing = *p++;
6636#endif /* MBEDTLS_SSL_PROTO_DTLS */
6637
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01006638 if ((size_t) (end - p) < 8) {
6639 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
6640 }
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02006641
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01006642 memcpy(ssl->cur_out_ctr, p, 8);
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02006643 p += 8;
6644
6645#if defined(MBEDTLS_SSL_PROTO_DTLS)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01006646 if ((size_t) (end - p) < 2) {
6647 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
6648 }
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02006649
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01006650 ssl->mtu = (p[0] << 8) | p[1];
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02006651 p += 2;
6652#endif /* MBEDTLS_SSL_PROTO_DTLS */
6653
6654#if defined(MBEDTLS_SSL_ALPN)
6655 {
6656 uint8_t alpn_len;
6657 const char **cur;
6658
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01006659 if ((size_t) (end - p) < 1) {
6660 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
6661 }
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02006662
6663 alpn_len = *p++;
6664
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01006665 if (alpn_len != 0 && ssl->conf->alpn_list != NULL) {
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02006666 /* alpn_chosen should point to an item in the configured list */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01006667 for (cur = ssl->conf->alpn_list; *cur != NULL; cur++) {
6668 if (strlen(*cur) == alpn_len &&
6669 memcmp(p, cur, alpn_len) == 0) {
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02006670 ssl->alpn_chosen = *cur;
6671 break;
6672 }
6673 }
6674 }
6675
6676 /* can only happen on conf mismatch */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01006677 if (alpn_len != 0 && ssl->alpn_chosen == NULL) {
6678 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
6679 }
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02006680
6681 p += alpn_len;
6682 }
6683#endif /* MBEDTLS_SSL_ALPN */
6684
6685 /*
Manuel Pégourié-Gonnard0eb3eac2019-07-15 11:53:51 +02006686 * Forced fields from top-level ssl_context structure
6687 *
6688 * Most of them already set to the correct value by mbedtls_ssl_init() and
6689 * mbedtls_ssl_reset(), so we only need to set the remaining ones.
6690 */
6691 ssl->state = MBEDTLS_SSL_HANDSHAKE_OVER;
6692
6693 ssl->major_ver = MBEDTLS_SSL_MAJOR_VERSION_3;
6694 ssl->minor_ver = MBEDTLS_SSL_MINOR_VERSION_3;
6695
Hanno Becker361b10d2019-08-30 10:42:49 +01006696 /* Adjust pointers for header fields of outgoing records to
6697 * the given transform, accounting for explicit IV and CID. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01006698 mbedtls_ssl_update_out_pointers(ssl, ssl->transform);
Hanno Becker361b10d2019-08-30 10:42:49 +01006699
Manuel Pégourié-Gonnard0eb3eac2019-07-15 11:53:51 +02006700#if defined(MBEDTLS_SSL_PROTO_DTLS)
6701 ssl->in_epoch = 1;
6702#endif
6703
6704 /* mbedtls_ssl_reset() leaves the handshake sub-structure allocated,
6705 * which we don't want - otherwise we'd end up freeing the wrong transform
Hanno Beckerce5f5fd2020-02-05 10:47:44 +00006706 * by calling mbedtls_ssl_handshake_wrapup_free_hs_transform()
6707 * inappropriately. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01006708 if (ssl->handshake != NULL) {
6709 mbedtls_ssl_handshake_free(ssl);
6710 mbedtls_free(ssl->handshake);
Manuel Pégourié-Gonnard0eb3eac2019-07-15 11:53:51 +02006711 ssl->handshake = NULL;
6712 }
6713
6714 /*
Manuel Pégourié-Gonnard4c90e852019-07-11 10:58:10 +02006715 * Done - should have consumed entire buffer
6716 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01006717 if (p != end) {
6718 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
6719 }
Manuel Pégourié-Gonnardac87e282019-05-28 13:02:16 +02006720
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01006721 return 0;
Manuel Pégourié-Gonnardac87e282019-05-28 13:02:16 +02006722}
6723
6724/*
Manuel Pégourié-Gonnardb9dfc9f2019-07-12 10:50:19 +02006725 * Deserialize context: public wrapper for error cleaning
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02006726 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01006727int mbedtls_ssl_context_load(mbedtls_ssl_context *context,
6728 const unsigned char *buf,
6729 size_t len)
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02006730{
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01006731 int ret = ssl_context_load(context, buf, len);
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02006732
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01006733 if (ret != 0) {
6734 mbedtls_ssl_free(context);
6735 }
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02006736
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01006737 return ret;
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02006738}
Manuel Pégourié-Gonnard5c0e3772019-07-23 16:13:17 +02006739#endif /* MBEDTLS_SSL_CONTEXT_SERIALIZATION */
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02006740
6741/*
Paul Bakker5121ce52009-01-03 21:22:43 +00006742 * Free an SSL context
6743 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01006744void mbedtls_ssl_free(mbedtls_ssl_context *ssl)
Paul Bakker5121ce52009-01-03 21:22:43 +00006745{
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01006746 if (ssl == NULL) {
Paul Bakkeraccaffe2014-06-26 13:37:14 +02006747 return;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01006748 }
Paul Bakkeraccaffe2014-06-26 13:37:14 +02006749
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01006750 MBEDTLS_SSL_DEBUG_MSG(2, ("=> free"));
Paul Bakker5121ce52009-01-03 21:22:43 +00006751
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01006752 if (ssl->out_buf != NULL) {
sander-visserb8aa2072020-05-06 22:05:13 +02006753#if defined(MBEDTLS_SSL_VARIABLE_BUFFER_LENGTH)
6754 size_t out_buf_len = ssl->out_buf_len;
6755#else
6756 size_t out_buf_len = MBEDTLS_SSL_OUT_BUFFER_LEN;
6757#endif
6758
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01006759 mbedtls_platform_zeroize(ssl->out_buf, out_buf_len);
6760 mbedtls_free(ssl->out_buf);
Andrzej Kurek0afa2a12020-03-03 10:39:58 -05006761 ssl->out_buf = NULL;
Paul Bakker5121ce52009-01-03 21:22:43 +00006762 }
6763
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01006764 if (ssl->in_buf != NULL) {
sander-visserb8aa2072020-05-06 22:05:13 +02006765#if defined(MBEDTLS_SSL_VARIABLE_BUFFER_LENGTH)
6766 size_t in_buf_len = ssl->in_buf_len;
6767#else
6768 size_t in_buf_len = MBEDTLS_SSL_IN_BUFFER_LEN;
6769#endif
6770
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01006771 mbedtls_platform_zeroize(ssl->in_buf, in_buf_len);
6772 mbedtls_free(ssl->in_buf);
Andrzej Kurek0afa2a12020-03-03 10:39:58 -05006773 ssl->in_buf = NULL;
Paul Bakker5121ce52009-01-03 21:22:43 +00006774 }
6775
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006776#if defined(MBEDTLS_ZLIB_SUPPORT)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01006777 if (ssl->compress_buf != NULL) {
6778 mbedtls_platform_zeroize(ssl->compress_buf, MBEDTLS_SSL_COMPRESS_BUFFER_LEN);
6779 mbedtls_free(ssl->compress_buf);
Paul Bakker16770332013-10-11 09:59:44 +02006780 }
6781#endif
6782
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01006783 if (ssl->transform) {
6784 mbedtls_ssl_transform_free(ssl->transform);
6785 mbedtls_free(ssl->transform);
Paul Bakker48916f92012-09-16 19:57:18 +00006786 }
6787
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01006788 if (ssl->handshake) {
6789 mbedtls_ssl_handshake_free(ssl);
6790 mbedtls_ssl_transform_free(ssl->transform_negotiate);
6791 mbedtls_ssl_session_free(ssl->session_negotiate);
Paul Bakker48916f92012-09-16 19:57:18 +00006792
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01006793 mbedtls_free(ssl->handshake);
6794 mbedtls_free(ssl->transform_negotiate);
6795 mbedtls_free(ssl->session_negotiate);
Paul Bakker48916f92012-09-16 19:57:18 +00006796 }
6797
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01006798 if (ssl->session) {
6799 mbedtls_ssl_session_free(ssl->session);
6800 mbedtls_free(ssl->session);
Paul Bakkerc0463502013-02-14 11:19:38 +01006801 }
6802
Manuel Pégourié-Gonnard55fab2d2015-05-11 16:15:19 +02006803#if defined(MBEDTLS_X509_CRT_PARSE_C)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01006804 if (ssl->hostname != NULL) {
6805 mbedtls_platform_zeroize(ssl->hostname, strlen(ssl->hostname));
6806 mbedtls_free(ssl->hostname);
Paul Bakker5121ce52009-01-03 21:22:43 +00006807 }
Paul Bakker0be444a2013-08-27 21:55:01 +02006808#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00006809
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006810#if defined(MBEDTLS_SSL_HW_RECORD_ACCEL)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01006811 if (mbedtls_ssl_hw_record_finish != NULL) {
6812 MBEDTLS_SSL_DEBUG_MSG(2, ("going for mbedtls_ssl_hw_record_finish()"));
6813 mbedtls_ssl_hw_record_finish(ssl);
Paul Bakker05ef8352012-05-08 09:17:57 +00006814 }
6815#endif
6816
Manuel Pégourié-Gonnarde057d3b2015-05-20 10:59:43 +02006817#if defined(MBEDTLS_SSL_DTLS_HELLO_VERIFY) && defined(MBEDTLS_SSL_SRV_C)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01006818 mbedtls_free(ssl->cli_id);
Manuel Pégourié-Gonnard43c02182014-07-22 17:32:01 +02006819#endif
6820
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01006821 MBEDTLS_SSL_DEBUG_MSG(2, ("<= free"));
Paul Bakker2da561c2009-02-05 18:00:28 +00006822
Paul Bakker86f04f42013-02-14 11:20:09 +01006823 /* Actually clear after last debug message */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01006824 mbedtls_platform_zeroize(ssl, sizeof(mbedtls_ssl_context));
Paul Bakker5121ce52009-01-03 21:22:43 +00006825}
6826
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02006827/*
Tom Cosgrove49f99bc2022-12-04 16:44:21 +00006828 * Initialize mbedtls_ssl_config
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02006829 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01006830void mbedtls_ssl_config_init(mbedtls_ssl_config *conf)
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02006831{
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01006832 memset(conf, 0, sizeof(mbedtls_ssl_config));
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02006833}
6834
Gilles Peskineeccd8882020-03-10 12:19:08 +01006835#if defined(MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED)
Manuel Pégourié-Gonnard47229c72015-12-04 15:02:56 +01006836static int ssl_preset_default_hashes[] = {
Gilles Peskinec54010c2021-05-12 22:52:09 +02006837#if defined(MBEDTLS_SHA512_C)
Manuel Pégourié-Gonnard47229c72015-12-04 15:02:56 +01006838 MBEDTLS_MD_SHA512,
Gilles Peskinec54010c2021-05-12 22:52:09 +02006839#endif
6840#if defined(MBEDTLS_SHA512_C) && !defined(MBEDTLS_SHA512_NO_SHA384)
Manuel Pégourié-Gonnard47229c72015-12-04 15:02:56 +01006841 MBEDTLS_MD_SHA384,
6842#endif
6843#if defined(MBEDTLS_SHA256_C)
6844 MBEDTLS_MD_SHA256,
6845 MBEDTLS_MD_SHA224,
6846#endif
Gilles Peskine5d2511c2017-05-12 13:16:40 +02006847#if defined(MBEDTLS_SHA1_C) && defined(MBEDTLS_TLS_DEFAULT_ALLOW_SHA1_IN_KEY_EXCHANGE)
Manuel Pégourié-Gonnard47229c72015-12-04 15:02:56 +01006848 MBEDTLS_MD_SHA1,
6849#endif
6850 MBEDTLS_MD_NONE
6851};
Simon Butcherc97b6972015-12-27 23:48:17 +00006852#endif
Manuel Pégourié-Gonnard47229c72015-12-04 15:02:56 +01006853
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02006854static int ssl_preset_suiteb_ciphersuites[] = {
6855 MBEDTLS_TLS_ECDHE_ECDSA_WITH_AES_128_GCM_SHA256,
6856 MBEDTLS_TLS_ECDHE_ECDSA_WITH_AES_256_GCM_SHA384,
6857 0
6858};
6859
Gilles Peskineeccd8882020-03-10 12:19:08 +01006860#if defined(MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED)
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02006861static int ssl_preset_suiteb_hashes[] = {
6862 MBEDTLS_MD_SHA256,
6863 MBEDTLS_MD_SHA384,
6864 MBEDTLS_MD_NONE
6865};
6866#endif
6867
6868#if defined(MBEDTLS_ECP_C)
6869static mbedtls_ecp_group_id ssl_preset_suiteb_curves[] = {
Jaeden Amerod4311042019-06-03 08:27:16 +01006870#if defined(MBEDTLS_ECP_DP_SECP256R1_ENABLED)
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02006871 MBEDTLS_ECP_DP_SECP256R1,
Jaeden Amerod4311042019-06-03 08:27:16 +01006872#endif
6873#if defined(MBEDTLS_ECP_DP_SECP384R1_ENABLED)
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02006874 MBEDTLS_ECP_DP_SECP384R1,
Jaeden Amerod4311042019-06-03 08:27:16 +01006875#endif
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02006876 MBEDTLS_ECP_DP_NONE
6877};
6878#endif
6879
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02006880/*
Tillmann Karras588ad502015-09-25 04:27:22 +02006881 * Load default in mbedtls_ssl_config
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02006882 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01006883int mbedtls_ssl_config_defaults(mbedtls_ssl_config *conf,
6884 int endpoint, int transport, int preset)
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02006885{
Manuel Pégourié-Gonnard8b431fb2015-05-11 12:54:52 +02006886#if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_SSL_SRV_C)
Janos Follath865b3eb2019-12-16 11:46:15 +00006887 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnard8b431fb2015-05-11 12:54:52 +02006888#endif
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02006889
Manuel Pégourié-Gonnard0de074f2015-05-14 12:58:01 +02006890 /* Use the functions here so that they are covered in tests,
6891 * but otherwise access member directly for efficiency */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01006892 mbedtls_ssl_conf_endpoint(conf, endpoint);
6893 mbedtls_ssl_conf_transport(conf, transport);
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02006894
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02006895 /*
6896 * Things that are common to all presets
6897 */
Manuel Pégourié-Gonnard419d5ae2015-05-04 19:32:36 +02006898#if defined(MBEDTLS_SSL_CLI_C)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01006899 if (endpoint == MBEDTLS_SSL_IS_CLIENT) {
Manuel Pégourié-Gonnard419d5ae2015-05-04 19:32:36 +02006900 conf->authmode = MBEDTLS_SSL_VERIFY_REQUIRED;
6901#if defined(MBEDTLS_SSL_SESSION_TICKETS)
6902 conf->session_tickets = MBEDTLS_SSL_SESSION_TICKETS_ENABLED;
6903#endif
6904 }
6905#endif
6906
Manuel Pégourié-Gonnard66dc5552015-05-14 12:28:21 +02006907#if defined(MBEDTLS_ARC4_C)
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02006908 conf->arc4_disabled = MBEDTLS_SSL_ARC4_DISABLED;
Manuel Pégourié-Gonnard66dc5552015-05-14 12:28:21 +02006909#endif
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02006910
6911#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
6912 conf->encrypt_then_mac = MBEDTLS_SSL_ETM_ENABLED;
6913#endif
6914
6915#if defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET)
6916 conf->extended_ms = MBEDTLS_SSL_EXTENDED_MS_ENABLED;
6917#endif
6918
Manuel Pégourié-Gonnard17eab2b2015-05-05 16:34:53 +01006919#if defined(MBEDTLS_SSL_CBC_RECORD_SPLITTING)
6920 conf->cbc_record_splitting = MBEDTLS_SSL_CBC_RECORD_SPLITTING_ENABLED;
6921#endif
6922
Manuel Pégourié-Gonnarde057d3b2015-05-20 10:59:43 +02006923#if defined(MBEDTLS_SSL_DTLS_HELLO_VERIFY) && defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02006924 conf->f_cookie_write = ssl_cookie_write_dummy;
6925 conf->f_cookie_check = ssl_cookie_check_dummy;
6926#endif
6927
6928#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
6929 conf->anti_replay = MBEDTLS_SSL_ANTI_REPLAY_ENABLED;
6930#endif
6931
Janos Follath088ce432017-04-10 12:42:31 +01006932#if defined(MBEDTLS_SSL_SRV_C)
6933 conf->cert_req_ca_list = MBEDTLS_SSL_CERT_REQ_CA_LIST_ENABLED;
6934#endif
6935
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02006936#if defined(MBEDTLS_SSL_PROTO_DTLS)
6937 conf->hs_timeout_min = MBEDTLS_SSL_DTLS_TIMEOUT_DFL_MIN;
6938 conf->hs_timeout_max = MBEDTLS_SSL_DTLS_TIMEOUT_DFL_MAX;
6939#endif
6940
6941#if defined(MBEDTLS_SSL_RENEGOTIATION)
6942 conf->renego_max_records = MBEDTLS_SSL_RENEGO_MAX_RECORDS_DEFAULT;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01006943 memset(conf->renego_period, 0x00, 2);
6944 memset(conf->renego_period + 2, 0xFF, 6);
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02006945#endif
6946
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02006947#if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_SSL_SRV_C)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01006948 if (endpoint == MBEDTLS_SSL_IS_SERVER) {
6949 const unsigned char dhm_p[] =
6950 MBEDTLS_DHM_RFC3526_MODP_2048_P_BIN;
6951 const unsigned char dhm_g[] =
6952 MBEDTLS_DHM_RFC3526_MODP_2048_G_BIN;
Hanno Becker00d0a682017-10-04 13:14:29 +01006953
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01006954 if ((ret = mbedtls_ssl_conf_dh_param_bin(conf,
6955 dhm_p, sizeof(dhm_p),
6956 dhm_g, sizeof(dhm_g))) != 0) {
6957 return ret;
6958 }
6959 }
Manuel Pégourié-Gonnardbd990d62015-06-11 14:49:42 +02006960#endif
6961
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02006962 /*
6963 * Preset-specific defaults
6964 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01006965 switch (preset) {
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02006966 /*
6967 * NSA Suite B
6968 */
6969 case MBEDTLS_SSL_PRESET_SUITEB:
6970 conf->min_major_ver = MBEDTLS_SSL_MAJOR_VERSION_3;
6971 conf->min_minor_ver = MBEDTLS_SSL_MINOR_VERSION_3; /* TLS 1.2 */
6972 conf->max_major_ver = MBEDTLS_SSL_MAX_MAJOR_VERSION;
6973 conf->max_minor_ver = MBEDTLS_SSL_MAX_MINOR_VERSION;
6974
6975 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_0] =
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01006976 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_1] =
6977 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_2] =
6978 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_3] =
6979 ssl_preset_suiteb_ciphersuites;
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02006980
6981#if defined(MBEDTLS_X509_CRT_PARSE_C)
6982 conf->cert_profile = &mbedtls_x509_crt_profile_suiteb;
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02006983#endif
6984
Gilles Peskineeccd8882020-03-10 12:19:08 +01006985#if defined(MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED)
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02006986 conf->sig_hashes = ssl_preset_suiteb_hashes;
6987#endif
6988
6989#if defined(MBEDTLS_ECP_C)
6990 conf->curve_list = ssl_preset_suiteb_curves;
6991#endif
Manuel Pégourié-Gonnardc98204e2015-08-11 04:21:01 +02006992 break;
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02006993
6994 /*
6995 * Default
6996 */
6997 default:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01006998 conf->min_major_ver = (MBEDTLS_SSL_MIN_MAJOR_VERSION >
6999 MBEDTLS_SSL_MIN_VALID_MAJOR_VERSION) ?
7000 MBEDTLS_SSL_MIN_MAJOR_VERSION :
7001 MBEDTLS_SSL_MIN_VALID_MAJOR_VERSION;
7002 conf->min_minor_ver = (MBEDTLS_SSL_MIN_MINOR_VERSION >
7003 MBEDTLS_SSL_MIN_VALID_MINOR_VERSION) ?
7004 MBEDTLS_SSL_MIN_MINOR_VERSION :
7005 MBEDTLS_SSL_MIN_VALID_MINOR_VERSION;
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02007006 conf->max_major_ver = MBEDTLS_SSL_MAX_MAJOR_VERSION;
7007 conf->max_minor_ver = MBEDTLS_SSL_MAX_MINOR_VERSION;
7008
7009#if defined(MBEDTLS_SSL_PROTO_DTLS)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01007010 if (transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM) {
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02007011 conf->min_minor_ver = MBEDTLS_SSL_MINOR_VERSION_2;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01007012 }
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02007013#endif
7014
7015 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_0] =
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01007016 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_1] =
7017 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_2] =
7018 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_3] =
7019 mbedtls_ssl_list_ciphersuites();
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02007020
7021#if defined(MBEDTLS_X509_CRT_PARSE_C)
7022 conf->cert_profile = &mbedtls_x509_crt_profile_default;
7023#endif
7024
Gilles Peskineeccd8882020-03-10 12:19:08 +01007025#if defined(MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED)
Manuel Pégourié-Gonnard47229c72015-12-04 15:02:56 +01007026 conf->sig_hashes = ssl_preset_default_hashes;
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02007027#endif
7028
7029#if defined(MBEDTLS_ECP_C)
7030 conf->curve_list = mbedtls_ecp_grp_id_list();
7031#endif
7032
7033#if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_SSL_CLI_C)
7034 conf->dhm_min_bitlen = 1024;
7035#endif
7036 }
7037
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01007038 return 0;
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02007039}
7040
7041/*
7042 * Free mbedtls_ssl_config
7043 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01007044void mbedtls_ssl_config_free(mbedtls_ssl_config *conf)
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02007045{
7046#if defined(MBEDTLS_DHM_C)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01007047 mbedtls_mpi_free(&conf->dhm_P);
7048 mbedtls_mpi_free(&conf->dhm_G);
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02007049#endif
7050
Gilles Peskineeccd8882020-03-10 12:19:08 +01007051#if defined(MBEDTLS_KEY_EXCHANGE_SOME_PSK_ENABLED)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01007052 if (conf->psk != NULL) {
7053 mbedtls_platform_zeroize(conf->psk, conf->psk_len);
7054 mbedtls_free(conf->psk);
Azim Khan27e8a122018-03-21 14:24:11 +00007055 conf->psk = NULL;
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02007056 conf->psk_len = 0;
junyeonLEE316b1622017-12-20 16:29:30 +09007057 }
7058
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01007059 if (conf->psk_identity != NULL) {
7060 mbedtls_platform_zeroize(conf->psk_identity, conf->psk_identity_len);
7061 mbedtls_free(conf->psk_identity);
Azim Khan27e8a122018-03-21 14:24:11 +00007062 conf->psk_identity = NULL;
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02007063 conf->psk_identity_len = 0;
7064 }
7065#endif
7066
7067#if defined(MBEDTLS_X509_CRT_PARSE_C)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01007068 ssl_key_cert_free(conf->key_cert);
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02007069#endif
7070
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01007071 mbedtls_platform_zeroize(conf, sizeof(mbedtls_ssl_config));
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02007072}
7073
Manuel Pégourié-Gonnard5674a972015-10-19 15:14:03 +02007074#if defined(MBEDTLS_PK_C) && \
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01007075 (defined(MBEDTLS_RSA_C) || defined(MBEDTLS_ECDSA_C))
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02007076/*
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007077 * Convert between MBEDTLS_PK_XXX and SSL_SIG_XXX
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02007078 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01007079unsigned char mbedtls_ssl_sig_from_pk(mbedtls_pk_context *pk)
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02007080{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007081#if defined(MBEDTLS_RSA_C)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01007082 if (mbedtls_pk_can_do(pk, MBEDTLS_PK_RSA)) {
7083 return MBEDTLS_SSL_SIG_RSA;
7084 }
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02007085#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007086#if defined(MBEDTLS_ECDSA_C)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01007087 if (mbedtls_pk_can_do(pk, MBEDTLS_PK_ECDSA)) {
7088 return MBEDTLS_SSL_SIG_ECDSA;
7089 }
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02007090#endif
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01007091 return MBEDTLS_SSL_SIG_ANON;
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02007092}
7093
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01007094unsigned char mbedtls_ssl_sig_from_pk_alg(mbedtls_pk_type_t type)
Hanno Becker7e5437a2017-04-28 17:15:26 +01007095{
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01007096 switch (type) {
Hanno Becker7e5437a2017-04-28 17:15:26 +01007097 case MBEDTLS_PK_RSA:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01007098 return MBEDTLS_SSL_SIG_RSA;
Hanno Becker7e5437a2017-04-28 17:15:26 +01007099 case MBEDTLS_PK_ECDSA:
7100 case MBEDTLS_PK_ECKEY:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01007101 return MBEDTLS_SSL_SIG_ECDSA;
Hanno Becker7e5437a2017-04-28 17:15:26 +01007102 default:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01007103 return MBEDTLS_SSL_SIG_ANON;
Hanno Becker7e5437a2017-04-28 17:15:26 +01007104 }
7105}
7106
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01007107mbedtls_pk_type_t mbedtls_ssl_pk_alg_from_sig(unsigned char sig)
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02007108{
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01007109 switch (sig) {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007110#if defined(MBEDTLS_RSA_C)
7111 case MBEDTLS_SSL_SIG_RSA:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01007112 return MBEDTLS_PK_RSA;
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02007113#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007114#if defined(MBEDTLS_ECDSA_C)
7115 case MBEDTLS_SSL_SIG_ECDSA:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01007116 return MBEDTLS_PK_ECDSA;
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02007117#endif
7118 default:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01007119 return MBEDTLS_PK_NONE;
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02007120 }
7121}
Manuel Pégourié-Gonnard5674a972015-10-19 15:14:03 +02007122#endif /* MBEDTLS_PK_C && ( MBEDTLS_RSA_C || MBEDTLS_ECDSA_C ) */
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02007123
Hanno Becker7e5437a2017-04-28 17:15:26 +01007124#if defined(MBEDTLS_SSL_PROTO_TLS1_2) && \
Gilles Peskineeccd8882020-03-10 12:19:08 +01007125 defined(MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED)
Hanno Becker7e5437a2017-04-28 17:15:26 +01007126
7127/* Find an entry in a signature-hash set matching a given hash algorithm. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01007128mbedtls_md_type_t mbedtls_ssl_sig_hash_set_find(mbedtls_ssl_sig_hash_set_t *set,
7129 mbedtls_pk_type_t sig_alg)
Hanno Becker7e5437a2017-04-28 17:15:26 +01007130{
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01007131 switch (sig_alg) {
Hanno Becker7e5437a2017-04-28 17:15:26 +01007132 case MBEDTLS_PK_RSA:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01007133 return set->rsa;
Hanno Becker7e5437a2017-04-28 17:15:26 +01007134 case MBEDTLS_PK_ECDSA:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01007135 return set->ecdsa;
Hanno Becker7e5437a2017-04-28 17:15:26 +01007136 default:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01007137 return MBEDTLS_MD_NONE;
Hanno Becker7e5437a2017-04-28 17:15:26 +01007138 }
7139}
7140
7141/* Add a signature-hash-pair to a signature-hash set */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01007142void mbedtls_ssl_sig_hash_set_add(mbedtls_ssl_sig_hash_set_t *set,
7143 mbedtls_pk_type_t sig_alg,
7144 mbedtls_md_type_t md_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 if (set->rsa == MBEDTLS_MD_NONE) {
Hanno Becker7e5437a2017-04-28 17:15:26 +01007149 set->rsa = md_alg;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01007150 }
Hanno Becker7e5437a2017-04-28 17:15:26 +01007151 break;
7152
7153 case MBEDTLS_PK_ECDSA:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01007154 if (set->ecdsa == MBEDTLS_MD_NONE) {
Hanno Becker7e5437a2017-04-28 17:15:26 +01007155 set->ecdsa = md_alg;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01007156 }
Hanno Becker7e5437a2017-04-28 17:15:26 +01007157 break;
7158
7159 default:
7160 break;
7161 }
7162}
7163
7164/* Allow exactly one hash algorithm for each signature. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01007165void mbedtls_ssl_sig_hash_set_const_hash(mbedtls_ssl_sig_hash_set_t *set,
7166 mbedtls_md_type_t md_alg)
Hanno Becker7e5437a2017-04-28 17:15:26 +01007167{
7168 set->rsa = md_alg;
7169 set->ecdsa = md_alg;
7170}
7171
7172#endif /* MBEDTLS_SSL_PROTO_TLS1_2) &&
Gilles Peskineeccd8882020-03-10 12:19:08 +01007173 MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED */
Hanno Becker7e5437a2017-04-28 17:15:26 +01007174
Manuel Pégourié-Gonnard1a483832013-09-20 12:29:15 +02007175/*
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +02007176 * Convert from MBEDTLS_SSL_HASH_XXX to MBEDTLS_MD_XXX
Manuel Pégourié-Gonnard1a483832013-09-20 12:29:15 +02007177 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01007178mbedtls_md_type_t mbedtls_ssl_md_alg_from_hash(unsigned char hash)
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02007179{
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01007180 switch (hash) {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007181#if defined(MBEDTLS_MD5_C)
7182 case MBEDTLS_SSL_HASH_MD5:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01007183 return MBEDTLS_MD_MD5;
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02007184#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007185#if defined(MBEDTLS_SHA1_C)
7186 case MBEDTLS_SSL_HASH_SHA1:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01007187 return MBEDTLS_MD_SHA1;
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02007188#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007189#if defined(MBEDTLS_SHA256_C)
7190 case MBEDTLS_SSL_HASH_SHA224:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01007191 return MBEDTLS_MD_SHA224;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007192 case MBEDTLS_SSL_HASH_SHA256:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01007193 return MBEDTLS_MD_SHA256;
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02007194#endif
Gilles Peskined2d59372021-05-12 22:43:27 +02007195#if defined(MBEDTLS_SHA512_C) && !defined(MBEDTLS_SHA512_NO_SHA384)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007196 case MBEDTLS_SSL_HASH_SHA384:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01007197 return MBEDTLS_MD_SHA384;
Gilles Peskinec54010c2021-05-12 22:52:09 +02007198#endif
7199#if defined(MBEDTLS_SHA512_C)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007200 case MBEDTLS_SSL_HASH_SHA512:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01007201 return MBEDTLS_MD_SHA512;
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02007202#endif
7203 default:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01007204 return MBEDTLS_MD_NONE;
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02007205 }
7206}
7207
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +02007208/*
7209 * Convert from MBEDTLS_MD_XXX to MBEDTLS_SSL_HASH_XXX
7210 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01007211unsigned char mbedtls_ssl_hash_from_md_alg(int md)
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +02007212{
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01007213 switch (md) {
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +02007214#if defined(MBEDTLS_MD5_C)
7215 case MBEDTLS_MD_MD5:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01007216 return MBEDTLS_SSL_HASH_MD5;
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +02007217#endif
7218#if defined(MBEDTLS_SHA1_C)
7219 case MBEDTLS_MD_SHA1:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01007220 return MBEDTLS_SSL_HASH_SHA1;
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +02007221#endif
7222#if defined(MBEDTLS_SHA256_C)
7223 case MBEDTLS_MD_SHA224:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01007224 return MBEDTLS_SSL_HASH_SHA224;
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +02007225 case MBEDTLS_MD_SHA256:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01007226 return MBEDTLS_SSL_HASH_SHA256;
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +02007227#endif
Gilles Peskined2d59372021-05-12 22:43:27 +02007228#if defined(MBEDTLS_SHA512_C) && !defined(MBEDTLS_SHA512_NO_SHA384)
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +02007229 case MBEDTLS_MD_SHA384:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01007230 return MBEDTLS_SSL_HASH_SHA384;
Gilles Peskinec54010c2021-05-12 22:52:09 +02007231#endif
7232#if defined(MBEDTLS_SHA512_C)
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +02007233 case MBEDTLS_MD_SHA512:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01007234 return MBEDTLS_SSL_HASH_SHA512;
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +02007235#endif
7236 default:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01007237 return MBEDTLS_SSL_HASH_NONE;
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +02007238 }
7239}
7240
Manuel Pégourié-Gonnardb541da62015-06-17 11:43:30 +02007241#if defined(MBEDTLS_ECP_C)
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01007242/*
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +02007243 * Check if a curve proposed by the peer is in our list.
Manuel Pégourié-Gonnard9d412d82015-06-17 12:10:46 +02007244 * Return 0 if we're willing to use it, -1 otherwise.
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01007245 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01007246int mbedtls_ssl_check_curve(const mbedtls_ssl_context *ssl, mbedtls_ecp_group_id grp_id)
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01007247{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007248 const mbedtls_ecp_group_id *gid;
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01007249
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01007250 if (ssl->conf->curve_list == NULL) {
7251 return -1;
7252 }
Manuel Pégourié-Gonnard9d412d82015-06-17 12:10:46 +02007253
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01007254 for (gid = ssl->conf->curve_list; *gid != MBEDTLS_ECP_DP_NONE; gid++) {
7255 if (*gid == grp_id) {
7256 return 0;
7257 }
7258 }
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01007259
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01007260 return -1;
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01007261}
Manuel Pégourié-Gonnard298d6cc2022-02-14 11:34:47 +01007262
7263/*
7264 * Same as mbedtls_ssl_check_curve() but takes a TLS ID for the curve.
7265 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01007266int mbedtls_ssl_check_curve_tls_id(const mbedtls_ssl_context *ssl, uint16_t tls_id)
Manuel Pégourié-Gonnard298d6cc2022-02-14 11:34:47 +01007267{
7268 const mbedtls_ecp_curve_info *curve_info =
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01007269 mbedtls_ecp_curve_info_from_tls_id(tls_id);
7270 if (curve_info == NULL) {
7271 return -1;
7272 }
7273 return mbedtls_ssl_check_curve(ssl, curve_info->grp_id);
Manuel Pégourié-Gonnard298d6cc2022-02-14 11:34:47 +01007274}
Manuel Pégourié-Gonnardb541da62015-06-17 11:43:30 +02007275#endif /* MBEDTLS_ECP_C */
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02007276
Gilles Peskineeccd8882020-03-10 12:19:08 +01007277#if defined(MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED)
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +02007278/*
7279 * Check if a hash proposed by the peer is in our list.
7280 * Return 0 if we're willing to use it, -1 otherwise.
7281 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01007282int mbedtls_ssl_check_sig_hash(const mbedtls_ssl_context *ssl,
7283 mbedtls_md_type_t md)
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +02007284{
7285 const int *cur;
7286
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01007287 if (ssl->conf->sig_hashes == NULL) {
7288 return -1;
7289 }
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +02007290
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01007291 for (cur = ssl->conf->sig_hashes; *cur != MBEDTLS_MD_NONE; cur++) {
7292 if (*cur == (int) md) {
7293 return 0;
7294 }
7295 }
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +02007296
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01007297 return -1;
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +02007298}
Gilles Peskineeccd8882020-03-10 12:19:08 +01007299#endif /* MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED */
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +02007300
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007301#if defined(MBEDTLS_X509_CRT_PARSE_C)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01007302int mbedtls_ssl_check_cert_usage(const mbedtls_x509_crt *cert,
7303 const mbedtls_ssl_ciphersuite_t *ciphersuite,
7304 int cert_endpoint,
7305 uint32_t *flags)
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02007306{
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +01007307 int ret = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007308#if defined(MBEDTLS_X509_CHECK_KEY_USAGE)
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02007309 int usage = 0;
7310#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007311#if defined(MBEDTLS_X509_CHECK_EXTENDED_KEY_USAGE)
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +02007312 const char *ext_oid;
7313 size_t ext_len;
7314#endif
7315
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007316#if !defined(MBEDTLS_X509_CHECK_KEY_USAGE) && \
7317 !defined(MBEDTLS_X509_CHECK_EXTENDED_KEY_USAGE)
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +02007318 ((void) cert);
7319 ((void) cert_endpoint);
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +01007320 ((void) flags);
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +02007321#endif
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02007322
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007323#if defined(MBEDTLS_X509_CHECK_KEY_USAGE)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01007324 if (cert_endpoint == MBEDTLS_SSL_IS_SERVER) {
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02007325 /* Server part of the key exchange */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01007326 switch (ciphersuite->key_exchange) {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007327 case MBEDTLS_KEY_EXCHANGE_RSA:
7328 case MBEDTLS_KEY_EXCHANGE_RSA_PSK:
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +01007329 usage = MBEDTLS_X509_KU_KEY_ENCIPHERMENT;
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02007330 break;
7331
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007332 case MBEDTLS_KEY_EXCHANGE_DHE_RSA:
7333 case MBEDTLS_KEY_EXCHANGE_ECDHE_RSA:
7334 case MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA:
7335 usage = MBEDTLS_X509_KU_DIGITAL_SIGNATURE;
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02007336 break;
7337
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007338 case MBEDTLS_KEY_EXCHANGE_ECDH_RSA:
7339 case MBEDTLS_KEY_EXCHANGE_ECDH_ECDSA:
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +01007340 usage = MBEDTLS_X509_KU_KEY_AGREEMENT;
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02007341 break;
7342
7343 /* Don't use default: we want warnings when adding new values */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007344 case MBEDTLS_KEY_EXCHANGE_NONE:
7345 case MBEDTLS_KEY_EXCHANGE_PSK:
7346 case MBEDTLS_KEY_EXCHANGE_DHE_PSK:
7347 case MBEDTLS_KEY_EXCHANGE_ECDHE_PSK:
Manuel Pégourié-Gonnard557535d2015-09-15 17:53:32 +02007348 case MBEDTLS_KEY_EXCHANGE_ECJPAKE:
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02007349 usage = 0;
7350 }
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01007351 } else {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007352 /* Client auth: we only implement rsa_sign and mbedtls_ecdsa_sign for now */
7353 usage = MBEDTLS_X509_KU_DIGITAL_SIGNATURE;
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02007354 }
7355
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01007356 if (mbedtls_x509_crt_check_key_usage(cert, usage) != 0) {
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +01007357 *flags |= MBEDTLS_X509_BADCERT_KEY_USAGE;
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +01007358 ret = -1;
7359 }
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +02007360#else
7361 ((void) ciphersuite);
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007362#endif /* MBEDTLS_X509_CHECK_KEY_USAGE */
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02007363
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007364#if defined(MBEDTLS_X509_CHECK_EXTENDED_KEY_USAGE)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01007365 if (cert_endpoint == MBEDTLS_SSL_IS_SERVER) {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007366 ext_oid = MBEDTLS_OID_SERVER_AUTH;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01007367 ext_len = MBEDTLS_OID_SIZE(MBEDTLS_OID_SERVER_AUTH);
7368 } else {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007369 ext_oid = MBEDTLS_OID_CLIENT_AUTH;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01007370 ext_len = MBEDTLS_OID_SIZE(MBEDTLS_OID_CLIENT_AUTH);
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +02007371 }
7372
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01007373 if (mbedtls_x509_crt_check_extended_key_usage(cert, ext_oid, ext_len) != 0) {
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +01007374 *flags |= MBEDTLS_X509_BADCERT_EXT_KEY_USAGE;
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +01007375 ret = -1;
7376 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007377#endif /* MBEDTLS_X509_CHECK_EXTENDED_KEY_USAGE */
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +02007378
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01007379 return ret;
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02007380}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007381#endif /* MBEDTLS_X509_CRT_PARSE_C */
Manuel Pégourié-Gonnard3a306b92014-04-29 15:11:17 +02007382
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01007383int mbedtls_ssl_set_calc_verify_md(mbedtls_ssl_context *ssl, int md)
Simon Butcher99000142016-10-13 17:21:01 +01007384{
7385#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01007386 if (ssl->minor_ver != MBEDTLS_SSL_MINOR_VERSION_3) {
Simon Butcher99000142016-10-13 17:21:01 +01007387 return MBEDTLS_ERR_SSL_INVALID_VERIFY_HASH;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01007388 }
Simon Butcher99000142016-10-13 17:21:01 +01007389
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01007390 switch (md) {
Simon Butcher99000142016-10-13 17:21:01 +01007391#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1)
7392#if defined(MBEDTLS_MD5_C)
7393 case MBEDTLS_SSL_HASH_MD5:
Janos Follath182013f2016-10-25 10:50:22 +01007394 return MBEDTLS_ERR_SSL_INVALID_VERIFY_HASH;
Simon Butcher99000142016-10-13 17:21:01 +01007395#endif
7396#if defined(MBEDTLS_SHA1_C)
7397 case MBEDTLS_SSL_HASH_SHA1:
7398 ssl->handshake->calc_verify = ssl_calc_verify_tls;
7399 break;
7400#endif
7401#endif /* MBEDTLS_SSL_PROTO_TLS1 || MBEDTLS_SSL_PROTO_TLS1_1 */
Gilles Peskined2d59372021-05-12 22:43:27 +02007402#if defined(MBEDTLS_SHA512_C) && !defined(MBEDTLS_SHA512_NO_SHA384)
Simon Butcher99000142016-10-13 17:21:01 +01007403 case MBEDTLS_SSL_HASH_SHA384:
7404 ssl->handshake->calc_verify = ssl_calc_verify_tls_sha384;
7405 break;
7406#endif
7407#if defined(MBEDTLS_SHA256_C)
7408 case MBEDTLS_SSL_HASH_SHA256:
7409 ssl->handshake->calc_verify = ssl_calc_verify_tls_sha256;
7410 break;
7411#endif
7412 default:
7413 return MBEDTLS_ERR_SSL_INVALID_VERIFY_HASH;
7414 }
7415
7416 return 0;
7417#else /* !MBEDTLS_SSL_PROTO_TLS1_2 */
7418 (void) ssl;
7419 (void) md;
7420
7421 return MBEDTLS_ERR_SSL_INVALID_VERIFY_HASH;
7422#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
7423}
7424
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +01007425#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1) || \
7426 defined(MBEDTLS_SSL_PROTO_TLS1_1)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01007427int mbedtls_ssl_get_key_exchange_md_ssl_tls(mbedtls_ssl_context *ssl,
7428 unsigned char *output,
7429 unsigned char *data, size_t data_len)
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +01007430{
7431 int ret = 0;
7432 mbedtls_md5_context mbedtls_md5;
7433 mbedtls_sha1_context mbedtls_sha1;
7434
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01007435 mbedtls_md5_init(&mbedtls_md5);
7436 mbedtls_sha1_init(&mbedtls_sha1);
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +01007437
7438 /*
7439 * digitally-signed struct {
7440 * opaque md5_hash[16];
7441 * opaque sha_hash[20];
7442 * };
7443 *
7444 * md5_hash
7445 * MD5(ClientHello.random + ServerHello.random
7446 * + ServerParams);
7447 * sha_hash
7448 * SHA(ClientHello.random + ServerHello.random
7449 * + ServerParams);
7450 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01007451 if ((ret = mbedtls_md5_starts_ret(&mbedtls_md5)) != 0) {
7452 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_md5_starts_ret", ret);
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +01007453 goto exit;
7454 }
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01007455 if ((ret = mbedtls_md5_update_ret(&mbedtls_md5,
7456 ssl->handshake->randbytes, 64)) != 0) {
7457 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_md5_update_ret", ret);
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +01007458 goto exit;
7459 }
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01007460 if ((ret = mbedtls_md5_update_ret(&mbedtls_md5, data, data_len)) != 0) {
7461 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_md5_update_ret", ret);
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +01007462 goto exit;
7463 }
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01007464 if ((ret = mbedtls_md5_finish_ret(&mbedtls_md5, output)) != 0) {
7465 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_md5_finish_ret", ret);
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +01007466 goto exit;
7467 }
7468
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01007469 if ((ret = mbedtls_sha1_starts_ret(&mbedtls_sha1)) != 0) {
7470 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_sha1_starts_ret", ret);
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +01007471 goto exit;
7472 }
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01007473 if ((ret = mbedtls_sha1_update_ret(&mbedtls_sha1,
7474 ssl->handshake->randbytes, 64)) != 0) {
7475 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_sha1_update_ret", ret);
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +01007476 goto exit;
7477 }
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01007478 if ((ret = mbedtls_sha1_update_ret(&mbedtls_sha1, data,
7479 data_len)) != 0) {
7480 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_sha1_update_ret", ret);
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +01007481 goto exit;
7482 }
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01007483 if ((ret = mbedtls_sha1_finish_ret(&mbedtls_sha1,
7484 output + 16)) != 0) {
7485 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_sha1_finish_ret", ret);
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +01007486 goto exit;
7487 }
7488
7489exit:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01007490 mbedtls_md5_free(&mbedtls_md5);
7491 mbedtls_sha1_free(&mbedtls_sha1);
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +01007492
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01007493 if (ret != 0) {
7494 mbedtls_ssl_send_alert_message(ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
7495 MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR);
7496 }
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +01007497
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01007498 return ret;
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +01007499
7500}
7501#endif /* MBEDTLS_SSL_PROTO_SSL3 || MBEDTLS_SSL_PROTO_TLS1 || \
7502 MBEDTLS_SSL_PROTO_TLS1_1 */
7503
7504#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1) || \
7505 defined(MBEDTLS_SSL_PROTO_TLS1_2)
Andrzej Kurekd6db9be2019-01-10 05:27:10 -05007506
7507#if defined(MBEDTLS_USE_PSA_CRYPTO)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01007508int mbedtls_ssl_get_key_exchange_md_tls1_2(mbedtls_ssl_context *ssl,
7509 unsigned char *hash, size_t *hashlen,
7510 unsigned char *data, size_t data_len,
7511 mbedtls_md_type_t md_alg)
Andrzej Kurekd6db9be2019-01-10 05:27:10 -05007512{
Andrzej Kurek814feff2019-01-14 04:35:19 -05007513 psa_status_t status;
Jaeden Amero34973232019-02-20 10:32:28 +00007514 psa_hash_operation_t hash_operation = PSA_HASH_OPERATION_INIT;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01007515 psa_algorithm_t hash_alg = mbedtls_psa_translate_md(md_alg);
Andrzej Kurekd6db9be2019-01-10 05:27:10 -05007516
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01007517 MBEDTLS_SSL_DEBUG_MSG(3, ("Perform PSA-based computation of digest of ServerKeyExchange"));
Andrzej Kurek814feff2019-01-14 04:35:19 -05007518
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01007519 if ((status = psa_hash_setup(&hash_operation,
7520 hash_alg)) != PSA_SUCCESS) {
7521 MBEDTLS_SSL_DEBUG_RET(1, "psa_hash_setup", status);
Andrzej Kurekd6db9be2019-01-10 05:27:10 -05007522 goto exit;
7523 }
7524
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01007525 if ((status = psa_hash_update(&hash_operation, ssl->handshake->randbytes,
7526 64)) != PSA_SUCCESS) {
7527 MBEDTLS_SSL_DEBUG_RET(1, "psa_hash_update", status);
Andrzej Kurekd6db9be2019-01-10 05:27:10 -05007528 goto exit;
7529 }
7530
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01007531 if ((status = psa_hash_update(&hash_operation,
7532 data, data_len)) != PSA_SUCCESS) {
7533 MBEDTLS_SSL_DEBUG_RET(1, "psa_hash_update", status);
Andrzej Kurekd6db9be2019-01-10 05:27:10 -05007534 goto exit;
7535 }
7536
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01007537 if ((status = psa_hash_finish(&hash_operation, hash, PSA_HASH_MAX_SIZE,
7538 hashlen)) != PSA_SUCCESS) {
7539 MBEDTLS_SSL_DEBUG_RET(1, "psa_hash_finish", status);
7540 goto exit;
Andrzej Kurekd6db9be2019-01-10 05:27:10 -05007541 }
7542
7543exit:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01007544 if (status != PSA_SUCCESS) {
7545 mbedtls_ssl_send_alert_message(ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
7546 MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR);
7547 switch (status) {
Andrzej Kurekd6db9be2019-01-10 05:27:10 -05007548 case PSA_ERROR_NOT_SUPPORTED:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01007549 return MBEDTLS_ERR_MD_FEATURE_UNAVAILABLE;
Andrzej Kurek814feff2019-01-14 04:35:19 -05007550 case PSA_ERROR_BAD_STATE: /* Intentional fallthrough */
Andrzej Kurekd6db9be2019-01-10 05:27:10 -05007551 case PSA_ERROR_BUFFER_TOO_SMALL:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01007552 return MBEDTLS_ERR_MD_BAD_INPUT_DATA;
Andrzej Kurekd6db9be2019-01-10 05:27:10 -05007553 case PSA_ERROR_INSUFFICIENT_MEMORY:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01007554 return MBEDTLS_ERR_MD_ALLOC_FAILED;
Andrzej Kurekd6db9be2019-01-10 05:27:10 -05007555 default:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01007556 return MBEDTLS_ERR_MD_HW_ACCEL_FAILED;
Andrzej Kurekd6db9be2019-01-10 05:27:10 -05007557 }
7558 }
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01007559 return 0;
Andrzej Kurekd6db9be2019-01-10 05:27:10 -05007560}
7561
7562#else
7563
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01007564int mbedtls_ssl_get_key_exchange_md_tls1_2(mbedtls_ssl_context *ssl,
7565 unsigned char *hash, size_t *hashlen,
7566 unsigned char *data, size_t data_len,
7567 mbedtls_md_type_t md_alg)
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +01007568{
7569 int ret = 0;
7570 mbedtls_md_context_t ctx;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01007571 const mbedtls_md_info_t *md_info = mbedtls_md_info_from_type(md_alg);
7572 *hashlen = mbedtls_md_get_size(md_info);
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +01007573
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01007574 MBEDTLS_SSL_DEBUG_MSG(3, ("Perform mbedtls-based computation of digest of ServerKeyExchange"));
Andrzej Kurek814feff2019-01-14 04:35:19 -05007575
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01007576 mbedtls_md_init(&ctx);
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +01007577
7578 /*
7579 * digitally-signed struct {
7580 * opaque client_random[32];
7581 * opaque server_random[32];
7582 * ServerDHParams params;
7583 * };
7584 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01007585 if ((ret = mbedtls_md_setup(&ctx, md_info, 0)) != 0) {
7586 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_md_setup", ret);
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +01007587 goto exit;
7588 }
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01007589 if ((ret = mbedtls_md_starts(&ctx)) != 0) {
7590 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_md_starts", ret);
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +01007591 goto exit;
7592 }
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01007593 if ((ret = mbedtls_md_update(&ctx, ssl->handshake->randbytes, 64)) != 0) {
7594 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_md_update", ret);
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +01007595 goto exit;
7596 }
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01007597 if ((ret = mbedtls_md_update(&ctx, data, data_len)) != 0) {
7598 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_md_update", ret);
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +01007599 goto exit;
7600 }
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01007601 if ((ret = mbedtls_md_finish(&ctx, hash)) != 0) {
7602 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_md_finish", ret);
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +01007603 goto exit;
7604 }
7605
7606exit:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01007607 mbedtls_md_free(&ctx);
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +01007608
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01007609 if (ret != 0) {
7610 mbedtls_ssl_send_alert_message(ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
7611 MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR);
7612 }
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +01007613
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01007614 return ret;
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +01007615}
Andrzej Kurekd6db9be2019-01-10 05:27:10 -05007616#endif /* MBEDTLS_USE_PSA_CRYPTO */
7617
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +01007618#endif /* MBEDTLS_SSL_PROTO_TLS1 || MBEDTLS_SSL_PROTO_TLS1_1 || \
7619 MBEDTLS_SSL_PROTO_TLS1_2 */
7620
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007621#endif /* MBEDTLS_SSL_TLS_C */