blob: 494de1b93e16abfa808b97e9376bb98157c0abdd [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)
Manuel Pégourié-Gonnarda7505d12019-05-07 10:17:56 +02001007 ssl = NULL; /* make sure we don't use it except for those cases */
Manuel Pégourié-Gonnardc864f6a2019-05-06 13:48:22 +02001008 (void) ssl;
1009#endif
1010
Manuel Pégourié-Gonnard96fb0ee2019-07-09 12:54:17 +02001011 /*
1012 * Some data just needs copying into the structure
1013 */
Jaeden Amero2de07f12019-06-05 13:32:08 +01001014#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC) && \
1015 defined(MBEDTLS_SSL_SOME_MODES_USE_MAC)
Manuel Pégourié-Gonnard6fa57bf2019-05-10 10:50:04 +02001016 transform->encrypt_then_mac = encrypt_then_mac;
Hanno Becker9eddaeb2017-12-27 21:37:21 +00001017#endif
Manuel Pégourié-Gonnardc864f6a2019-05-06 13:48:22 +02001018 transform->minor_ver = minor_ver;
Hanno Beckere694c3e2017-12-27 21:34:08 +00001019
Manuel Pégourié-Gonnard96fb0ee2019-07-09 12:54:17 +02001020#if defined(MBEDTLS_SSL_CONTEXT_SERIALIZATION)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001021 memcpy(transform->randbytes, randbytes, sizeof(transform->randbytes));
Manuel Pégourié-Gonnard96fb0ee2019-07-09 12:54:17 +02001022#endif
1023
Manuel Pégourié-Gonnard9b108c22019-05-06 13:32:17 +02001024 /*
1025 * Get various info structures
1026 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001027 ciphersuite_info = mbedtls_ssl_ciphersuite_from_id(ciphersuite);
1028 if (ciphersuite_info == NULL) {
1029 MBEDTLS_SSL_DEBUG_MSG(1, ("ciphersuite info for %d not found",
1030 ciphersuite));
1031 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
Manuel Pégourié-Gonnard9b108c22019-05-06 13:32:17 +02001032 }
1033
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001034 cipher_info = mbedtls_cipher_info_from_type(ciphersuite_info->cipher);
1035 if (cipher_info == NULL) {
1036 MBEDTLS_SSL_DEBUG_MSG(1, ("cipher info for %u not found",
1037 ciphersuite_info->cipher));
1038 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
Paul Bakker68884e32013-01-07 18:20:04 +01001039 }
1040
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001041 md_info = mbedtls_md_info_from_type(ciphersuite_info->mac);
1042 if (md_info == NULL) {
1043 MBEDTLS_SSL_DEBUG_MSG(1, ("mbedtls_md info for %u not found",
1044 (unsigned) ciphersuite_info->mac));
1045 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
Paul Bakker68884e32013-01-07 18:20:04 +01001046 }
1047
Hanno Beckera0e20d02019-05-15 14:03:01 +01001048#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Becker4bf74652019-04-26 16:22:27 +01001049 /* Copy own and peer's CID if the use of the CID
1050 * extension has been negotiated. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001051 if (ssl->handshake->cid_in_use == MBEDTLS_SSL_CID_ENABLED) {
1052 MBEDTLS_SSL_DEBUG_MSG(3, ("Copy CIDs into SSL transform"));
Hanno Becker8a7f9722019-04-30 13:52:29 +01001053
Hanno Becker05154c32019-05-03 15:23:51 +01001054 transform->in_cid_len = ssl->own_cid_len;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001055 memcpy(transform->in_cid, ssl->own_cid, ssl->own_cid_len);
1056 MBEDTLS_SSL_DEBUG_BUF(3, "Incoming CID", transform->in_cid,
1057 transform->in_cid_len);
Hanno Beckerd1f20352019-05-15 10:21:55 +01001058
1059 transform->out_cid_len = ssl->handshake->peer_cid_len;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001060 memcpy(transform->out_cid, ssl->handshake->peer_cid,
1061 ssl->handshake->peer_cid_len);
1062 MBEDTLS_SSL_DEBUG_BUF(3, "Outgoing CID", transform->out_cid,
1063 transform->out_cid_len);
Hanno Becker4bf74652019-04-26 16:22:27 +01001064 }
Hanno Beckera0e20d02019-05-15 14:03:01 +01001065#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Becker4bf74652019-04-26 16:22:27 +01001066
Paul Bakker5121ce52009-01-03 21:22:43 +00001067 /*
Manuel Pégourié-Gonnard9b108c22019-05-06 13:32:17 +02001068 * Compute key block using the PRF
Paul Bakker5121ce52009-01-03 21:22:43 +00001069 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001070 ret = tls_prf(master, 48, "key expansion", randbytes, 64, keyblk, 256);
1071 if (ret != 0) {
1072 MBEDTLS_SSL_DEBUG_RET(1, "prf", ret);
1073 return ret;
Manuel Pégourié-Gonnarde9608182015-03-26 11:47:47 +01001074 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001075
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001076 MBEDTLS_SSL_DEBUG_MSG(3, ("ciphersuite = %s",
1077 mbedtls_ssl_get_ciphersuite_name(ciphersuite)));
1078 MBEDTLS_SSL_DEBUG_BUF(3, "master secret", master, 48);
1079 MBEDTLS_SSL_DEBUG_BUF(4, "random bytes", randbytes, 64);
1080 MBEDTLS_SSL_DEBUG_BUF(4, "key block", keyblk, 256);
Paul Bakker5121ce52009-01-03 21:22:43 +00001081
Paul Bakker5121ce52009-01-03 21:22:43 +00001082 /*
1083 * Determine the appropriate key, IV and MAC length.
1084 */
Paul Bakker68884e32013-01-07 18:20:04 +01001085
Hanno Becker88aaf652017-12-27 08:17:40 +00001086 keylen = cipher_info->key_bitlen / 8;
Manuel Pégourié-Gonnarde800cd82014-06-18 15:34:40 +02001087
Hanno Becker8031d062018-01-03 15:32:31 +00001088#if defined(MBEDTLS_GCM_C) || \
1089 defined(MBEDTLS_CCM_C) || \
1090 defined(MBEDTLS_CHACHAPOLY_C)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001091 if (cipher_info->mode == MBEDTLS_MODE_GCM ||
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02001092 cipher_info->mode == MBEDTLS_MODE_CCM ||
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001093 cipher_info->mode == MBEDTLS_MODE_CHACHAPOLY) {
Hanno Beckerf704bef2018-11-16 15:21:18 +00001094 size_t explicit_ivlen;
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02001095
Manuel Pégourié-Gonnarde800cd82014-06-18 15:34:40 +02001096 transform->maclen = 0;
Hanno Becker81c7b182017-11-09 18:39:33 +00001097 mac_key_len = 0;
Hanno Beckere694c3e2017-12-27 21:34:08 +00001098 transform->taglen =
1099 ciphersuite_info->flags & MBEDTLS_CIPHERSUITE_SHORT_TAG ? 8 : 16;
Manuel Pégourié-Gonnarde800cd82014-06-18 15:34:40 +02001100
Hanno Becker447558d2020-05-28 07:36:33 +01001101 /* All modes haves 96-bit IVs, but the length of the static parts vary
1102 * with mode and version:
1103 * - For GCM and CCM in TLS 1.2, there's a static IV of 4 Bytes
1104 * (to be concatenated with a dynamically chosen IV of 8 Bytes)
Hanno Beckerf93c2d72020-05-28 07:39:43 +01001105 * - For ChaChaPoly in TLS 1.2, and all modes in TLS 1.3, there's
1106 * a static IV of 12 Bytes (to be XOR'ed with the 8 Byte record
1107 * sequence number).
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02001108 */
Paul Bakker68884e32013-01-07 18:20:04 +01001109 transform->ivlen = 12;
Hanno Beckerf93c2d72020-05-28 07:39:43 +01001110#if defined(MBEDTLS_SSL_PROTO_TLS1_3_EXPERIMENTAL)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001111 if (minor_ver == MBEDTLS_SSL_MINOR_VERSION_4) {
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02001112 transform->fixed_ivlen = 12;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001113 } else
Hanno Beckerf93c2d72020-05-28 07:39:43 +01001114#endif /* MBEDTLS_SSL_PROTO_TLS1_3_EXPERIMENTAL */
1115 {
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001116 if (cipher_info->mode == MBEDTLS_MODE_CHACHAPOLY) {
Hanno Beckerf93c2d72020-05-28 07:39:43 +01001117 transform->fixed_ivlen = 12;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001118 } else {
Hanno Beckerf93c2d72020-05-28 07:39:43 +01001119 transform->fixed_ivlen = 4;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001120 }
Hanno Beckerf93c2d72020-05-28 07:39:43 +01001121 }
Manuel Pégourié-Gonnarde800cd82014-06-18 15:34:40 +02001122
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02001123 /* Minimum length of encrypted record */
1124 explicit_ivlen = transform->ivlen - transform->fixed_ivlen;
Hanno Beckere694c3e2017-12-27 21:34:08 +00001125 transform->minlen = explicit_ivlen + transform->taglen;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001126 } else
Hanno Becker8031d062018-01-03 15:32:31 +00001127#endif /* MBEDTLS_GCM_C || MBEDTLS_CCM_C || MBEDTLS_CHACHAPOLY_C */
1128#if defined(MBEDTLS_SSL_SOME_MODES_USE_MAC)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001129 if (cipher_info->mode == MBEDTLS_MODE_STREAM ||
1130 cipher_info->mode == MBEDTLS_MODE_CBC) {
Manuel Pégourié-Gonnarde800cd82014-06-18 15:34:40 +02001131 /* Initialize HMAC contexts */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001132 if ((ret = mbedtls_md_setup(&transform->md_ctx_enc, md_info, 1)) != 0 ||
1133 (ret = mbedtls_md_setup(&transform->md_ctx_dec, md_info, 1)) != 0) {
1134 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_md_setup", ret);
Ron Eldore6992702019-05-07 18:27:13 +03001135 goto end;
Paul Bakker68884e32013-01-07 18:20:04 +01001136 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001137
Manuel Pégourié-Gonnarde800cd82014-06-18 15:34:40 +02001138 /* Get MAC length */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001139 mac_key_len = mbedtls_md_get_size(md_info);
Hanno Becker81c7b182017-11-09 18:39:33 +00001140 transform->maclen = mac_key_len;
Manuel Pégourié-Gonnarde800cd82014-06-18 15:34:40 +02001141
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001142#if defined(MBEDTLS_SSL_TRUNCATED_HMAC)
Manuel Pégourié-Gonnarde800cd82014-06-18 15:34:40 +02001143 /*
1144 * If HMAC is to be truncated, we shall keep the leftmost bytes,
1145 * (rfc 6066 page 13 or rfc 2104 section 4),
1146 * so we only need to adjust the length here.
1147 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001148 if (trunc_hmac == MBEDTLS_SSL_TRUNC_HMAC_ENABLED) {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001149 transform->maclen = MBEDTLS_SSL_TRUNCATED_HMAC_LEN;
Hanno Beckere89353a2017-11-20 16:36:41 +00001150
1151#if defined(MBEDTLS_SSL_TRUNCATED_HMAC_COMPAT)
1152 /* Fall back to old, non-compliant version of the truncated
Hanno Becker563423f2017-11-21 17:20:17 +00001153 * HMAC implementation which also truncates the key
1154 * (Mbed TLS versions from 1.3 to 2.6.0) */
Hanno Beckere89353a2017-11-20 16:36:41 +00001155 mac_key_len = transform->maclen;
1156#endif
1157 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001158#endif /* MBEDTLS_SSL_TRUNCATED_HMAC */
Manuel Pégourié-Gonnarde800cd82014-06-18 15:34:40 +02001159
1160 /* IV length */
Paul Bakker68884e32013-01-07 18:20:04 +01001161 transform->ivlen = cipher_info->iv_size;
Paul Bakker5121ce52009-01-03 21:22:43 +00001162
Manuel Pégourié-Gonnardeaa76f72014-06-18 16:06:02 +02001163 /* Minimum length */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001164 if (cipher_info->mode == MBEDTLS_MODE_STREAM) {
Manuel Pégourié-Gonnardeaa76f72014-06-18 16:06:02 +02001165 transform->minlen = transform->maclen;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001166 } else {
Manuel Pégourié-Gonnardeaa76f72014-06-18 16:06:02 +02001167 /*
1168 * GenericBlockCipher:
Manuel Pégourié-Gonnard169dd6a2014-11-04 16:15:39 +01001169 * 1. if EtM is in use: one block plus MAC
1170 * otherwise: * first multiple of blocklen greater than maclen
1171 * 2. IV except for SSL3 and TLS 1.0
Manuel Pégourié-Gonnardeaa76f72014-06-18 16:06:02 +02001172 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001173#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001174 if (encrypt_then_mac == MBEDTLS_SSL_ETM_ENABLED) {
Manuel Pégourié-Gonnard169dd6a2014-11-04 16:15:39 +01001175 transform->minlen = transform->maclen
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001176 + cipher_info->block_size;
1177 } else
Manuel Pégourié-Gonnard169dd6a2014-11-04 16:15:39 +01001178#endif
1179 {
1180 transform->minlen = transform->maclen
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001181 + cipher_info->block_size
1182 - transform->maclen % cipher_info->block_size;
Manuel Pégourié-Gonnard169dd6a2014-11-04 16:15:39 +01001183 }
Manuel Pégourié-Gonnardeaa76f72014-06-18 16:06:02 +02001184
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001185#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001186 if (minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 ||
1187 minor_ver == MBEDTLS_SSL_MINOR_VERSION_1) {
Manuel Pégourié-Gonnardeaa76f72014-06-18 16:06:02 +02001188 ; /* No need to adjust minlen */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001189 } else
Manuel Pégourié-Gonnardeaa76f72014-06-18 16:06:02 +02001190#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001191#if defined(MBEDTLS_SSL_PROTO_TLS1_1) || defined(MBEDTLS_SSL_PROTO_TLS1_2)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001192 if (minor_ver == MBEDTLS_SSL_MINOR_VERSION_2 ||
1193 minor_ver == MBEDTLS_SSL_MINOR_VERSION_3) {
Manuel Pégourié-Gonnardeaa76f72014-06-18 16:06:02 +02001194 transform->minlen += transform->ivlen;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001195 } else
Manuel Pégourié-Gonnardeaa76f72014-06-18 16:06:02 +02001196#endif
1197 {
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001198 MBEDTLS_SSL_DEBUG_MSG(1, ("should never happen"));
Ron Eldore6992702019-05-07 18:27:13 +03001199 ret = MBEDTLS_ERR_SSL_INTERNAL_ERROR;
1200 goto end;
Manuel Pégourié-Gonnardeaa76f72014-06-18 16:06:02 +02001201 }
Paul Bakker68884e32013-01-07 18:20:04 +01001202 }
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001203 } else
Hanno Becker8031d062018-01-03 15:32:31 +00001204#endif /* MBEDTLS_SSL_SOME_MODES_USE_MAC */
1205 {
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001206 MBEDTLS_SSL_DEBUG_MSG(1, ("should never happen"));
1207 return MBEDTLS_ERR_SSL_INTERNAL_ERROR;
Hanno Becker8031d062018-01-03 15:32:31 +00001208 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001209
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001210 MBEDTLS_SSL_DEBUG_MSG(3, ("keylen: %u, minlen: %u, ivlen: %u, maclen: %u",
1211 (unsigned) keylen,
1212 (unsigned) transform->minlen,
1213 (unsigned) transform->ivlen,
1214 (unsigned) transform->maclen));
Paul Bakker5121ce52009-01-03 21:22:43 +00001215
1216 /*
1217 * Finally setup the cipher contexts, IVs and MAC secrets.
1218 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001219#if defined(MBEDTLS_SSL_CLI_C)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001220 if (endpoint == MBEDTLS_SSL_IS_CLIENT) {
Hanno Becker81c7b182017-11-09 18:39:33 +00001221 key1 = keyblk + mac_key_len * 2;
Hanno Becker88aaf652017-12-27 08:17:40 +00001222 key2 = keyblk + mac_key_len * 2 + keylen;
Paul Bakker5121ce52009-01-03 21:22:43 +00001223
Paul Bakker68884e32013-01-07 18:20:04 +01001224 mac_enc = keyblk;
Hanno Becker81c7b182017-11-09 18:39:33 +00001225 mac_dec = keyblk + mac_key_len;
Paul Bakker5121ce52009-01-03 21:22:43 +00001226
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001227 /*
1228 * This is not used in TLS v1.1.
1229 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001230 iv_copy_len = (transform->fixed_ivlen) ?
1231 transform->fixed_ivlen : transform->ivlen;
1232 memcpy(transform->iv_enc, key2 + keylen, iv_copy_len);
1233 memcpy(transform->iv_dec, key2 + keylen + iv_copy_len,
1234 iv_copy_len);
1235 } else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001236#endif /* MBEDTLS_SSL_CLI_C */
1237#if defined(MBEDTLS_SSL_SRV_C)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001238 if (endpoint == MBEDTLS_SSL_IS_SERVER) {
Hanno Becker88aaf652017-12-27 08:17:40 +00001239 key1 = keyblk + mac_key_len * 2 + keylen;
Hanno Becker81c7b182017-11-09 18:39:33 +00001240 key2 = keyblk + mac_key_len * 2;
Paul Bakker5121ce52009-01-03 21:22:43 +00001241
Hanno Becker81c7b182017-11-09 18:39:33 +00001242 mac_enc = keyblk + mac_key_len;
Paul Bakker68884e32013-01-07 18:20:04 +01001243 mac_dec = keyblk;
Paul Bakker5121ce52009-01-03 21:22:43 +00001244
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001245 /*
1246 * This is not used in TLS v1.1.
1247 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001248 iv_copy_len = (transform->fixed_ivlen) ?
1249 transform->fixed_ivlen : transform->ivlen;
1250 memcpy(transform->iv_dec, key1 + keylen, iv_copy_len);
1251 memcpy(transform->iv_enc, key1 + keylen + iv_copy_len,
1252 iv_copy_len);
1253 } else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001254#endif /* MBEDTLS_SSL_SRV_C */
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01001255 {
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001256 MBEDTLS_SSL_DEBUG_MSG(1, ("should never happen"));
Ron Eldore6992702019-05-07 18:27:13 +03001257 ret = MBEDTLS_ERR_SSL_INTERNAL_ERROR;
1258 goto end;
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01001259 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001260
Hanno Beckerd56ed242018-01-03 15:32:51 +00001261#if defined(MBEDTLS_SSL_SOME_MODES_USE_MAC)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001262#if defined(MBEDTLS_SSL_PROTO_SSL3)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001263 if (minor_ver == MBEDTLS_SSL_MINOR_VERSION_0) {
1264 if (mac_key_len > sizeof(transform->mac_enc)) {
1265 MBEDTLS_SSL_DEBUG_MSG(1, ("should never happen"));
Ron Eldore6992702019-05-07 18:27:13 +03001266 ret = MBEDTLS_ERR_SSL_INTERNAL_ERROR;
1267 goto end;
Manuel Pégourié-Gonnard7cfdcb82014-01-18 18:22:55 +01001268 }
1269
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001270 memcpy(transform->mac_enc, mac_enc, mac_key_len);
1271 memcpy(transform->mac_dec, mac_dec, mac_key_len);
1272 } else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001273#endif /* MBEDTLS_SSL_PROTO_SSL3 */
1274#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1) || \
1275 defined(MBEDTLS_SSL_PROTO_TLS1_2)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001276 if (minor_ver >= MBEDTLS_SSL_MINOR_VERSION_1) {
Gilles Peskine039fd122018-03-19 19:06:08 +01001277 /* For HMAC-based ciphersuites, initialize the HMAC transforms.
1278 For AEAD-based ciphersuites, there is nothing to do here. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001279 if (mac_key_len != 0) {
1280 ret = mbedtls_md_hmac_starts(&transform->md_ctx_enc,
1281 mac_enc, mac_key_len);
1282 if (ret != 0) {
Gilles Peskine2b3f21d2021-12-10 21:35:10 +01001283 goto end;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001284 }
1285 ret = mbedtls_md_hmac_starts(&transform->md_ctx_dec,
1286 mac_dec, mac_key_len);
1287 if (ret != 0) {
Gilles Peskine2b3f21d2021-12-10 21:35:10 +01001288 goto end;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001289 }
Gilles Peskine039fd122018-03-19 19:06:08 +01001290 }
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001291 } else
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001292#endif
Paul Bakker577e0062013-08-28 11:57:20 +02001293 {
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001294 MBEDTLS_SSL_DEBUG_MSG(1, ("should never happen"));
Ron Eldore6992702019-05-07 18:27:13 +03001295 ret = MBEDTLS_ERR_SSL_INTERNAL_ERROR;
1296 goto end;
Paul Bakker577e0062013-08-28 11:57:20 +02001297 }
Hanno Beckerd56ed242018-01-03 15:32:51 +00001298#endif /* MBEDTLS_SSL_SOME_MODES_USE_MAC */
Paul Bakker68884e32013-01-07 18:20:04 +01001299
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001300#if defined(MBEDTLS_SSL_HW_RECORD_ACCEL)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001301 if (mbedtls_ssl_hw_record_init != NULL) {
sander-visser1abe8ee2020-05-06 21:27:14 +02001302 ret = 0;
Paul Bakker05ef8352012-05-08 09:17:57 +00001303
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001304 MBEDTLS_SSL_DEBUG_MSG(2, ("going for mbedtls_ssl_hw_record_init()"));
Paul Bakker05ef8352012-05-08 09:17:57 +00001305
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001306 if ((ret = mbedtls_ssl_hw_record_init(ssl, key1, key2, keylen,
1307 transform->iv_enc, transform->iv_dec,
1308 iv_copy_len,
1309 mac_enc, mac_dec,
1310 mac_key_len)) != 0) {
1311 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ssl_hw_record_init", ret);
Ron Eldore6992702019-05-07 18:27:13 +03001312 ret = MBEDTLS_ERR_SSL_HW_ACCEL_FAILED;
1313 goto end;
Paul Bakker05ef8352012-05-08 09:17:57 +00001314 }
1315 }
Hanno Beckerd56ed242018-01-03 15:32:51 +00001316#else
1317 ((void) mac_dec);
1318 ((void) mac_enc);
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001319#endif /* MBEDTLS_SSL_HW_RECORD_ACCEL */
Paul Bakker05ef8352012-05-08 09:17:57 +00001320
Manuel Pégourié-Gonnard024b6df2015-10-19 13:52:53 +02001321#if defined(MBEDTLS_SSL_EXPORT_KEYS)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001322 if (ssl->conf->f_export_keys != NULL) {
1323 ssl->conf->f_export_keys(ssl->conf->p_export_keys,
1324 master, keyblk,
1325 mac_key_len, keylen,
1326 iv_copy_len);
Robert Cragie4feb7ae2015-10-02 13:33:37 +01001327 }
Ron Eldorf5cc10d2019-05-07 18:33:40 +03001328
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001329 if (ssl->conf->f_export_keys_ext != NULL) {
1330 ssl->conf->f_export_keys_ext(ssl->conf->p_export_keys,
1331 master, keyblk,
1332 mac_key_len, keylen,
1333 iv_copy_len,
1334 randbytes + 32,
1335 randbytes,
1336 tls_prf_get_type(tls_prf));
Ron Eldorf5cc10d2019-05-07 18:33:40 +03001337 }
Robert Cragie4feb7ae2015-10-02 13:33:37 +01001338#endif
1339
David Horstmannd4f22082022-10-26 18:25:14 +01001340 do_mbedtls_cipher_setup = 1;
Hanno Beckerf704bef2018-11-16 15:21:18 +00001341#if defined(MBEDTLS_USE_PSA_CRYPTO)
Hanno Beckercb1cc802018-11-17 22:27:38 +00001342
1343 /* Only use PSA-based ciphers for TLS-1.2.
1344 * That's relevant at least for TLS-1.0, where
1345 * we assume that mbedtls_cipher_crypt() updates
1346 * the structure field for the IV, which the PSA-based
1347 * implementation currently doesn't. */
1348#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001349 if (minor_ver == MBEDTLS_SSL_MINOR_VERSION_3) {
1350 ret = mbedtls_cipher_setup_psa(&transform->cipher_ctx_enc,
1351 cipher_info, transform->taglen);
1352 if (ret != 0 && ret != MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE) {
1353 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_cipher_setup_psa", ret);
Ron Eldore6992702019-05-07 18:27:13 +03001354 goto end;
Hanno Beckercb1cc802018-11-17 22:27:38 +00001355 }
1356
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001357 if (ret == 0) {
1358 MBEDTLS_SSL_DEBUG_MSG(3, ("Successfully setup PSA-based encryption cipher context"));
Hanno Beckercb1cc802018-11-17 22:27:38 +00001359 psa_fallthrough = 0;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001360 } else {
1361 MBEDTLS_SSL_DEBUG_MSG(1,
1362 (
1363 "Failed to setup PSA-based cipher context for record encryption - fall through to default setup."));
Hanno Beckercb1cc802018-11-17 22:27:38 +00001364 psa_fallthrough = 1;
1365 }
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001366 } else {
Hanno Beckercb1cc802018-11-17 22:27:38 +00001367 psa_fallthrough = 1;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001368 }
Hanno Beckercb1cc802018-11-17 22:27:38 +00001369#else
1370 psa_fallthrough = 1;
1371#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Hanno Beckerf704bef2018-11-16 15:21:18 +00001372
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001373 if (psa_fallthrough == 0) {
David Horstmannd4f22082022-10-26 18:25:14 +01001374 do_mbedtls_cipher_setup = 0;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001375 }
Hanno Beckerf704bef2018-11-16 15:21:18 +00001376#endif /* MBEDTLS_USE_PSA_CRYPTO */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001377 if (do_mbedtls_cipher_setup &&
1378 (ret = mbedtls_cipher_setup(&transform->cipher_ctx_enc,
1379 cipher_info)) != 0) {
1380 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_cipher_setup", ret);
Ron Eldore6992702019-05-07 18:27:13 +03001381 goto end;
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001382 }
Paul Bakkerda02a7f2013-08-31 17:25:14 +02001383
David Horstmannd4f22082022-10-26 18:25:14 +01001384 do_mbedtls_cipher_setup = 1;
Hanno Beckerf704bef2018-11-16 15:21:18 +00001385#if defined(MBEDTLS_USE_PSA_CRYPTO)
Hanno Beckercb1cc802018-11-17 22:27:38 +00001386 /* Only use PSA-based ciphers for TLS-1.2.
1387 * That's relevant at least for TLS-1.0, where
1388 * we assume that mbedtls_cipher_crypt() updates
1389 * the structure field for the IV, which the PSA-based
1390 * implementation currently doesn't. */
1391#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001392 if (minor_ver == MBEDTLS_SSL_MINOR_VERSION_3) {
1393 ret = mbedtls_cipher_setup_psa(&transform->cipher_ctx_dec,
1394 cipher_info, transform->taglen);
1395 if (ret != 0 && ret != MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE) {
1396 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_cipher_setup_psa", ret);
Ron Eldore6992702019-05-07 18:27:13 +03001397 goto end;
Hanno Beckercb1cc802018-11-17 22:27:38 +00001398 }
1399
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001400 if (ret == 0) {
1401 MBEDTLS_SSL_DEBUG_MSG(3, ("Successfully setup PSA-based decryption cipher context"));
Hanno Beckercb1cc802018-11-17 22:27:38 +00001402 psa_fallthrough = 0;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001403 } else {
1404 MBEDTLS_SSL_DEBUG_MSG(1,
1405 (
1406 "Failed to setup PSA-based cipher context for record decryption - fall through to default setup."));
Hanno Beckercb1cc802018-11-17 22:27:38 +00001407 psa_fallthrough = 1;
1408 }
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001409 } else {
Hanno Beckercb1cc802018-11-17 22:27:38 +00001410 psa_fallthrough = 1;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001411 }
Hanno Beckercb1cc802018-11-17 22:27:38 +00001412#else
1413 psa_fallthrough = 1;
1414#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Hanno Beckerf704bef2018-11-16 15:21:18 +00001415
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001416 if (psa_fallthrough == 0) {
David Horstmannd4f22082022-10-26 18:25:14 +01001417 do_mbedtls_cipher_setup = 0;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001418 }
Hanno Beckerf704bef2018-11-16 15:21:18 +00001419#endif /* MBEDTLS_USE_PSA_CRYPTO */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001420 if (do_mbedtls_cipher_setup &&
1421 (ret = mbedtls_cipher_setup(&transform->cipher_ctx_dec,
1422 cipher_info)) != 0) {
1423 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_cipher_setup", ret);
Ron Eldore6992702019-05-07 18:27:13 +03001424 goto end;
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001425 }
Paul Bakkerda02a7f2013-08-31 17:25:14 +02001426
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001427 if ((ret = mbedtls_cipher_setkey(&transform->cipher_ctx_enc, key1,
1428 cipher_info->key_bitlen,
1429 MBEDTLS_ENCRYPT)) != 0) {
1430 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_cipher_setkey", ret);
Ron Eldore6992702019-05-07 18:27:13 +03001431 goto end;
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001432 }
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02001433
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001434 if ((ret = mbedtls_cipher_setkey(&transform->cipher_ctx_dec, key2,
1435 cipher_info->key_bitlen,
1436 MBEDTLS_DECRYPT)) != 0) {
1437 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_cipher_setkey", ret);
Ron Eldore6992702019-05-07 18:27:13 +03001438 goto end;
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001439 }
Paul Bakkerda02a7f2013-08-31 17:25:14 +02001440
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001441#if defined(MBEDTLS_CIPHER_MODE_CBC)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001442 if (cipher_info->mode == MBEDTLS_MODE_CBC) {
1443 if ((ret = mbedtls_cipher_set_padding_mode(&transform->cipher_ctx_enc,
1444 MBEDTLS_PADDING_NONE)) != 0) {
1445 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_cipher_set_padding_mode", ret);
Ron Eldore6992702019-05-07 18:27:13 +03001446 goto end;
Manuel Pégourié-Gonnard126a66f2013-10-25 18:33:32 +02001447 }
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001448
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001449 if ((ret = mbedtls_cipher_set_padding_mode(&transform->cipher_ctx_dec,
1450 MBEDTLS_PADDING_NONE)) != 0) {
1451 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_cipher_set_padding_mode", ret);
Ron Eldore6992702019-05-07 18:27:13 +03001452 goto end;
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001453 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001454 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001455#endif /* MBEDTLS_CIPHER_MODE_CBC */
Paul Bakker5121ce52009-01-03 21:22:43 +00001456
Paul Bakker5121ce52009-01-03 21:22:43 +00001457
Manuel Pégourié-Gonnardd73b47f2019-05-06 12:44:24 +02001458 /* Initialize Zlib contexts */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001459#if defined(MBEDTLS_ZLIB_SUPPORT)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001460 if (compression == MBEDTLS_SSL_COMPRESS_DEFLATE) {
1461 MBEDTLS_SSL_DEBUG_MSG(3, ("Initializing zlib states"));
Paul Bakker2770fbd2012-07-03 13:30:23 +00001462
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001463 memset(&transform->ctx_deflate, 0, sizeof(transform->ctx_deflate));
1464 memset(&transform->ctx_inflate, 0, sizeof(transform->ctx_inflate));
Paul Bakker2770fbd2012-07-03 13:30:23 +00001465
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001466 if (deflateInit(&transform->ctx_deflate,
1467 Z_DEFAULT_COMPRESSION) != Z_OK ||
1468 inflateInit(&transform->ctx_inflate) != Z_OK) {
1469 MBEDTLS_SSL_DEBUG_MSG(1, ("Failed to initialize compression"));
Ron Eldore6992702019-05-07 18:27:13 +03001470 ret = MBEDTLS_ERR_SSL_COMPRESSION_FAILED;
1471 goto end;
Paul Bakker2770fbd2012-07-03 13:30:23 +00001472 }
1473 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001474#endif /* MBEDTLS_ZLIB_SUPPORT */
Paul Bakker2770fbd2012-07-03 13:30:23 +00001475
Ron Eldore6992702019-05-07 18:27:13 +03001476end:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001477 mbedtls_platform_zeroize(keyblk, sizeof(keyblk));
1478 return ret;
Paul Bakker5121ce52009-01-03 21:22:43 +00001479}
1480
Manuel Pégourié-Gonnard8d2805c2019-04-30 12:08:59 +02001481/*
Manuel Pégourié-Gonnard47e33e12019-05-20 10:10:17 +02001482 * Set appropriate PRF function and other SSL / TLS 1.0/1.1 / TLS1.2 functions
Manuel Pégourié-Gonnard8d2805c2019-04-30 12:08:59 +02001483 *
1484 * Inputs:
1485 * - SSL/TLS minor version
1486 * - hash associated with the ciphersuite (only used by TLS 1.2)
1487 *
Manuel Pégourié-Gonnard31d3ef12019-05-10 10:25:00 +02001488 * Outputs:
Manuel Pégourié-Gonnard8d2805c2019-04-30 12:08:59 +02001489 * - the tls_prf, calc_verify and calc_finished members of handshake structure
1490 */
Manuel Pégourié-Gonnardd904d662022-06-17 10:24:00 +02001491MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001492static int ssl_set_handshake_prfs(mbedtls_ssl_handshake_params *handshake,
1493 int minor_ver,
1494 mbedtls_md_type_t hash)
Manuel Pégourié-Gonnard1b00c4f2019-04-30 11:54:22 +02001495{
Gilles Peskinefc9c07f2021-05-12 22:51:53 +02001496#if !defined(MBEDTLS_SSL_PROTO_TLS1_2) || \
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001497 !(defined(MBEDTLS_SHA512_C) && !defined(MBEDTLS_SHA512_NO_SHA384))
Manuel Pégourié-Gonnard8d2805c2019-04-30 12:08:59 +02001498 (void) hash;
1499#endif
Manuel Pégourié-Gonnard1b00c4f2019-04-30 11:54:22 +02001500
Manuel Pégourié-Gonnard1b00c4f2019-04-30 11:54:22 +02001501#if defined(MBEDTLS_SSL_PROTO_SSL3)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001502 if (minor_ver == MBEDTLS_SSL_MINOR_VERSION_0) {
Manuel Pégourié-Gonnard1b00c4f2019-04-30 11:54:22 +02001503 handshake->tls_prf = ssl3_prf;
1504 handshake->calc_verify = ssl_calc_verify_ssl;
1505 handshake->calc_finished = ssl_calc_finished_ssl;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001506 } else
Manuel Pégourié-Gonnard1b00c4f2019-04-30 11:54:22 +02001507#endif
1508#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001509 if (minor_ver < MBEDTLS_SSL_MINOR_VERSION_3) {
Manuel Pégourié-Gonnard1b00c4f2019-04-30 11:54:22 +02001510 handshake->tls_prf = tls1_prf;
1511 handshake->calc_verify = ssl_calc_verify_tls;
1512 handshake->calc_finished = ssl_calc_finished_tls;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001513 } else
Manuel Pégourié-Gonnard1b00c4f2019-04-30 11:54:22 +02001514#endif
1515#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
Gilles Peskined2d59372021-05-12 22:43:27 +02001516#if defined(MBEDTLS_SHA512_C) && !defined(MBEDTLS_SHA512_NO_SHA384)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001517 if (minor_ver == MBEDTLS_SSL_MINOR_VERSION_3 &&
1518 hash == MBEDTLS_MD_SHA384) {
Manuel Pégourié-Gonnard1b00c4f2019-04-30 11:54:22 +02001519 handshake->tls_prf = tls_prf_sha384;
1520 handshake->calc_verify = ssl_calc_verify_tls_sha384;
1521 handshake->calc_finished = ssl_calc_finished_tls_sha384;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001522 } else
Manuel Pégourié-Gonnard1b00c4f2019-04-30 11:54:22 +02001523#endif
1524#if defined(MBEDTLS_SHA256_C)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001525 if (minor_ver == MBEDTLS_SSL_MINOR_VERSION_3) {
Manuel Pégourié-Gonnard1b00c4f2019-04-30 11:54:22 +02001526 handshake->tls_prf = tls_prf_sha256;
1527 handshake->calc_verify = ssl_calc_verify_tls_sha256;
1528 handshake->calc_finished = ssl_calc_finished_tls_sha256;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001529 } else
Manuel Pégourié-Gonnard1b00c4f2019-04-30 11:54:22 +02001530#endif
1531#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
1532 {
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001533 return MBEDTLS_ERR_SSL_INTERNAL_ERROR;
Manuel Pégourié-Gonnard1b00c4f2019-04-30 11:54:22 +02001534 }
1535
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001536 return 0;
Manuel Pégourié-Gonnard1b00c4f2019-04-30 11:54:22 +02001537}
1538
Manuel Pégourié-Gonnard9951b712019-04-30 12:08:59 +02001539/*
Manuel Pégourié-Gonnard85680c42019-05-03 09:16:16 +02001540 * Compute master secret if needed
Manuel Pégourié-Gonnardde047ad2019-05-03 09:46:14 +02001541 *
1542 * Parameters:
1543 * [in/out] handshake
1544 * [in] resume, premaster, extended_ms, calc_verify, tls_prf
Manuel Pégourié-Gonnardde718b92019-05-03 11:43:28 +02001545 * (PSA-PSK) ciphersuite_info, psk_opaque
Manuel Pégourié-Gonnardde047ad2019-05-03 09:46:14 +02001546 * [out] premaster (cleared)
Manuel Pégourié-Gonnardde047ad2019-05-03 09:46:14 +02001547 * [out] master
1548 * [in] ssl: optionally used for debugging, EMS and PSA-PSK
1549 * debug: conf->f_dbg, conf->p_dbg
1550 * EMS: passed to calc_verify (debug + (SSL3) session_negotiate)
Manuel Pégourié-Gonnardde718b92019-05-03 11:43:28 +02001551 * PSA-PSA: minor_ver, conf
Manuel Pégourié-Gonnard9951b712019-04-30 12:08:59 +02001552 */
Manuel Pégourié-Gonnardd904d662022-06-17 10:24:00 +02001553MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001554static int ssl_compute_master(mbedtls_ssl_handshake_params *handshake,
1555 unsigned char *master,
1556 const mbedtls_ssl_context *ssl)
Manuel Pégourié-Gonnard9951b712019-04-30 12:08:59 +02001557{
Janos Follath865b3eb2019-12-16 11:46:15 +00001558 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnard9951b712019-04-30 12:08:59 +02001559
1560 /* cf. RFC 5246, Section 8.1:
1561 * "The master secret is always exactly 48 bytes in length." */
1562 size_t const master_secret_len = 48;
1563
1564#if defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET)
1565 unsigned char session_hash[48];
1566#endif /* MBEDTLS_SSL_EXTENDED_MASTER_SECRET */
1567
Manuel Pégourié-Gonnard85680c42019-05-03 09:16:16 +02001568 /* The label for the KDF used for key expansion.
1569 * This is either "master secret" or "extended master secret"
1570 * depending on whether the Extended Master Secret extension
1571 * is used. */
1572 char const *lbl = "master secret";
1573
1574 /* The salt for the KDF used for key expansion.
1575 * - If the Extended Master Secret extension is not used,
1576 * this is ClientHello.Random + ServerHello.Random
1577 * (see Sect. 8.1 in RFC 5246).
1578 * - If the Extended Master Secret extension is used,
1579 * this is the transcript of the handshake so far.
1580 * (see Sect. 4 in RFC 7627). */
1581 unsigned char const *salt = handshake->randbytes;
1582 size_t salt_len = 64;
1583
Manuel Pégourié-Gonnardde047ad2019-05-03 09:46:14 +02001584#if !defined(MBEDTLS_DEBUG_C) && \
1585 !defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET) && \
1586 !(defined(MBEDTLS_USE_PSA_CRYPTO) && \
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001587 defined(MBEDTLS_KEY_EXCHANGE_PSK_ENABLED))
Manuel Pégourié-Gonnarda7505d12019-05-07 10:17:56 +02001588 ssl = NULL; /* make sure we don't use it except for those cases */
Manuel Pégourié-Gonnardde047ad2019-05-03 09:46:14 +02001589 (void) ssl;
1590#endif
Manuel Pégourié-Gonnardde047ad2019-05-03 09:46:14 +02001591
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001592 if (handshake->resume != 0) {
1593 MBEDTLS_SSL_DEBUG_MSG(3, ("no premaster (session resumed)"));
1594 return 0;
Manuel Pégourié-Gonnard9951b712019-04-30 12:08:59 +02001595 }
Manuel Pégourié-Gonnard9951b712019-04-30 12:08:59 +02001596
1597#if defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001598 if (handshake->extended_ms == MBEDTLS_SSL_EXTENDED_MS_ENABLED) {
Manuel Pégourié-Gonnard85680c42019-05-03 09:16:16 +02001599 lbl = "extended master secret";
1600 salt = session_hash;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001601 handshake->calc_verify(ssl, session_hash, &salt_len);
Manuel Pégourié-Gonnard85680c42019-05-03 09:16:16 +02001602
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001603 MBEDTLS_SSL_DEBUG_BUF(3, "session hash for extended master secret",
1604 session_hash, salt_len);
Manuel Pégourié-Gonnard85680c42019-05-03 09:16:16 +02001605 }
Manuel Pégourié-Gonnard9951b712019-04-30 12:08:59 +02001606#endif /* MBEDTLS_SSL_EXTENDED_MS_ENABLED */
1607
1608#if defined(MBEDTLS_USE_PSA_CRYPTO) && \
Manuel Pégourié-Gonnardde047ad2019-05-03 09:46:14 +02001609 defined(MBEDTLS_KEY_EXCHANGE_PSK_ENABLED)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001610 if (handshake->ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_PSK &&
Manuel Pégourié-Gonnardde718b92019-05-03 11:43:28 +02001611 ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_3 &&
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001612 ssl_use_opaque_psk(ssl) == 1) {
Manuel Pégourié-Gonnard85680c42019-05-03 09:16:16 +02001613 /* Perform PSK-to-MS expansion in a single step. */
1614 psa_status_t status;
1615 psa_algorithm_t alg;
Ronald Croncf56a0a2020-08-04 09:51:30 +02001616 psa_key_id_t psk;
Manuel Pégourié-Gonnard85680c42019-05-03 09:16:16 +02001617 psa_key_derivation_operation_t derivation =
1618 PSA_KEY_DERIVATION_OPERATION_INIT;
Manuel Pégourié-Gonnardde718b92019-05-03 11:43:28 +02001619 mbedtls_md_type_t hash_alg = handshake->ciphersuite_info->mac;
Manuel Pégourié-Gonnard9951b712019-04-30 12:08:59 +02001620
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001621 MBEDTLS_SSL_DEBUG_MSG(2, ("perform PSA-based PSK-to-MS expansion"));
Manuel Pégourié-Gonnard9951b712019-04-30 12:08:59 +02001622
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001623 psk = mbedtls_ssl_get_opaque_psk(ssl);
Manuel Pégourié-Gonnard9951b712019-04-30 12:08:59 +02001624
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001625 if (hash_alg == MBEDTLS_MD_SHA384) {
Manuel Pégourié-Gonnard85680c42019-05-03 09:16:16 +02001626 alg = PSA_ALG_TLS12_PSK_TO_MS(PSA_ALG_SHA_384);
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001627 } else {
Manuel Pégourié-Gonnard85680c42019-05-03 09:16:16 +02001628 alg = PSA_ALG_TLS12_PSK_TO_MS(PSA_ALG_SHA_256);
Manuel Pégourié-Gonnard9951b712019-04-30 12:08:59 +02001629 }
Manuel Pégourié-Gonnard85680c42019-05-03 09:16:16 +02001630
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001631 status = setup_psa_key_derivation(&derivation, psk, alg,
1632 salt, salt_len,
1633 (unsigned char const *) lbl,
1634 (size_t) strlen(lbl),
1635 master_secret_len);
1636 if (status != PSA_SUCCESS) {
1637 psa_key_derivation_abort(&derivation);
1638 return MBEDTLS_ERR_SSL_HW_ACCEL_FAILED;
Manuel Pégourié-Gonnard85680c42019-05-03 09:16:16 +02001639 }
1640
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001641 status = psa_key_derivation_output_bytes(&derivation,
1642 master,
1643 master_secret_len);
1644 if (status != PSA_SUCCESS) {
1645 psa_key_derivation_abort(&derivation);
1646 return MBEDTLS_ERR_SSL_HW_ACCEL_FAILED;
1647 }
1648
1649 status = psa_key_derivation_abort(&derivation);
1650 if (status != PSA_SUCCESS) {
1651 return MBEDTLS_ERR_SSL_HW_ACCEL_FAILED;
1652 }
1653 } else
Manuel Pégourié-Gonnard85680c42019-05-03 09:16:16 +02001654#endif
1655 {
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001656 ret = handshake->tls_prf(handshake->premaster, handshake->pmslen,
1657 lbl, salt, salt_len,
1658 master,
1659 master_secret_len);
1660 if (ret != 0) {
1661 MBEDTLS_SSL_DEBUG_RET(1, "prf", ret);
1662 return ret;
Manuel Pégourié-Gonnard85680c42019-05-03 09:16:16 +02001663 }
1664
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001665 MBEDTLS_SSL_DEBUG_BUF(3, "premaster secret",
1666 handshake->premaster,
1667 handshake->pmslen);
Manuel Pégourié-Gonnard85680c42019-05-03 09:16:16 +02001668
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001669 mbedtls_platform_zeroize(handshake->premaster,
1670 sizeof(handshake->premaster));
Manuel Pégourié-Gonnard9951b712019-04-30 12:08:59 +02001671 }
1672
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001673 return 0;
Manuel Pégourié-Gonnard9951b712019-04-30 12:08:59 +02001674}
Manuel Pégourié-Gonnard1b00c4f2019-04-30 11:54:22 +02001675
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001676int mbedtls_ssl_derive_keys(mbedtls_ssl_context *ssl)
Manuel Pégourié-Gonnarde59ae232019-04-30 11:41:40 +02001677{
Janos Follath865b3eb2019-12-16 11:46:15 +00001678 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnard8d2805c2019-04-30 12:08:59 +02001679 const mbedtls_ssl_ciphersuite_t * const ciphersuite_info =
1680 ssl->handshake->ciphersuite_info;
Manuel Pégourié-Gonnard1b00c4f2019-04-30 11:54:22 +02001681
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001682 MBEDTLS_SSL_DEBUG_MSG(2, ("=> derive keys"));
Manuel Pégourié-Gonnard040a9512019-05-06 12:05:58 +02001683
1684 /* Set PRF, calc_verify and calc_finished function pointers */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001685 ret = ssl_set_handshake_prfs(ssl->handshake,
1686 ssl->minor_ver,
1687 ciphersuite_info->mac);
1688 if (ret != 0) {
1689 MBEDTLS_SSL_DEBUG_RET(1, "ssl_set_handshake_prfs", ret);
1690 return ret;
Manuel Pégourié-Gonnard8d2805c2019-04-30 12:08:59 +02001691 }
Manuel Pégourié-Gonnard1b00c4f2019-04-30 11:54:22 +02001692
Manuel Pégourié-Gonnard040a9512019-05-06 12:05:58 +02001693 /* Compute master secret if needed */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001694 ret = ssl_compute_master(ssl->handshake,
1695 ssl->session_negotiate->master,
1696 ssl);
1697 if (ret != 0) {
1698 MBEDTLS_SSL_DEBUG_RET(1, "ssl_compute_master", ret);
1699 return ret;
Manuel Pégourié-Gonnard9951b712019-04-30 12:08:59 +02001700 }
1701
Manuel Pégourié-Gonnard040a9512019-05-06 12:05:58 +02001702 /* Swap the client and server random values:
1703 * - MS derivation wanted client+server (RFC 5246 8.1)
1704 * - key derivation wants server+client (RFC 5246 6.3) */
1705 {
1706 unsigned char tmp[64];
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001707 memcpy(tmp, ssl->handshake->randbytes, 64);
1708 memcpy(ssl->handshake->randbytes, tmp + 32, 32);
1709 memcpy(ssl->handshake->randbytes + 32, tmp, 32);
1710 mbedtls_platform_zeroize(tmp, sizeof(tmp));
Manuel Pégourié-Gonnard040a9512019-05-06 12:05:58 +02001711 }
1712
1713 /* Populate transform structure */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001714 ret = ssl_populate_transform(ssl->transform_negotiate,
1715 ssl->session_negotiate->ciphersuite,
1716 ssl->session_negotiate->master,
Jarno Lamsac84bd242019-08-16 12:06:56 +03001717#if defined(MBEDTLS_SSL_SOME_MODES_USE_MAC)
Manuel Pégourié-Gonnard6fa57bf2019-05-10 10:50:04 +02001718#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001719 ssl->session_negotiate->encrypt_then_mac,
Jarno Lamsac84bd242019-08-16 12:06:56 +03001720#endif /* MBEDTLS_SSL_ENCRYPT_THEN_MAC */
Manuel Pégourié-Gonnard6fa57bf2019-05-10 10:50:04 +02001721#if defined(MBEDTLS_SSL_TRUNCATED_HMAC)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001722 ssl->session_negotiate->trunc_hmac,
Jarno Lamsac84bd242019-08-16 12:06:56 +03001723#endif /* MBEDTLS_SSL_TRUNCATED_HMAC */
1724#endif /* MBEDTLS_SSL_SOME_MODES_USE_MAC */
Manuel Pégourié-Gonnard6fa57bf2019-05-10 10:50:04 +02001725#if defined(MBEDTLS_ZLIB_SUPPORT)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001726 ssl->session_negotiate->compression,
Manuel Pégourié-Gonnard6fa57bf2019-05-10 10:50:04 +02001727#endif
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001728 ssl->handshake->tls_prf,
1729 ssl->handshake->randbytes,
1730 ssl->minor_ver,
1731 ssl->conf->endpoint,
1732 ssl);
1733 if (ret != 0) {
1734 MBEDTLS_SSL_DEBUG_RET(1, "ssl_populate_transform", ret);
1735 return ret;
Manuel Pégourié-Gonnard040a9512019-05-06 12:05:58 +02001736 }
1737
1738 /* We no longer need Server/ClientHello.random values */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001739 mbedtls_platform_zeroize(ssl->handshake->randbytes,
1740 sizeof(ssl->handshake->randbytes));
Manuel Pégourié-Gonnard040a9512019-05-06 12:05:58 +02001741
Manuel Pégourié-Gonnardd73b47f2019-05-06 12:44:24 +02001742 /* Allocate compression buffer */
1743#if defined(MBEDTLS_ZLIB_SUPPORT)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001744 if (ssl->session_negotiate->compression == MBEDTLS_SSL_COMPRESS_DEFLATE &&
1745 ssl->compress_buf == NULL) {
1746 MBEDTLS_SSL_DEBUG_MSG(3, ("Allocating compression buffer"));
1747 ssl->compress_buf = mbedtls_calloc(1, MBEDTLS_SSL_COMPRESS_BUFFER_LEN);
1748 if (ssl->compress_buf == NULL) {
1749 MBEDTLS_SSL_DEBUG_MSG(1, ("alloc(%d bytes) failed",
1750 MBEDTLS_SSL_COMPRESS_BUFFER_LEN));
1751 return MBEDTLS_ERR_SSL_ALLOC_FAILED;
Manuel Pégourié-Gonnardd73b47f2019-05-06 12:44:24 +02001752 }
1753 }
1754#endif
1755
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001756 MBEDTLS_SSL_DEBUG_MSG(2, ("<= derive keys"));
Manuel Pégourié-Gonnard040a9512019-05-06 12:05:58 +02001757
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001758 return 0;
Manuel Pégourié-Gonnarde59ae232019-04-30 11:41:40 +02001759}
1760
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001761#if defined(MBEDTLS_SSL_PROTO_SSL3)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001762void ssl_calc_verify_ssl(const mbedtls_ssl_context *ssl,
1763 unsigned char *hash,
1764 size_t *hlen)
Paul Bakker5121ce52009-01-03 21:22:43 +00001765{
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02001766 mbedtls_md5_context md5;
1767 mbedtls_sha1_context sha1;
Paul Bakker5121ce52009-01-03 21:22:43 +00001768 unsigned char pad_1[48];
1769 unsigned char pad_2[48];
1770
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001771 MBEDTLS_SSL_DEBUG_MSG(2, ("=> calc verify ssl"));
Paul Bakker5121ce52009-01-03 21:22:43 +00001772
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001773 mbedtls_md5_init(&md5);
1774 mbedtls_sha1_init(&sha1);
Manuel Pégourié-Gonnard001f2b62015-07-06 16:21:13 +02001775
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001776 mbedtls_md5_clone(&md5, &ssl->handshake->fin_md5);
1777 mbedtls_sha1_clone(&sha1, &ssl->handshake->fin_sha1);
Paul Bakker5121ce52009-01-03 21:22:43 +00001778
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001779 memset(pad_1, 0x36, 48);
1780 memset(pad_2, 0x5C, 48);
Paul Bakker5121ce52009-01-03 21:22:43 +00001781
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001782 mbedtls_md5_update_ret(&md5, ssl->session_negotiate->master, 48);
1783 mbedtls_md5_update_ret(&md5, pad_1, 48);
1784 mbedtls_md5_finish_ret(&md5, hash);
Paul Bakker5121ce52009-01-03 21:22:43 +00001785
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001786 mbedtls_md5_starts_ret(&md5);
1787 mbedtls_md5_update_ret(&md5, ssl->session_negotiate->master, 48);
1788 mbedtls_md5_update_ret(&md5, pad_2, 48);
1789 mbedtls_md5_update_ret(&md5, hash, 16);
1790 mbedtls_md5_finish_ret(&md5, hash);
Paul Bakker5121ce52009-01-03 21:22:43 +00001791
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001792 mbedtls_sha1_update_ret(&sha1, ssl->session_negotiate->master, 48);
1793 mbedtls_sha1_update_ret(&sha1, pad_1, 40);
1794 mbedtls_sha1_finish_ret(&sha1, hash + 16);
Paul Bakker380da532012-04-18 16:10:25 +00001795
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001796 mbedtls_sha1_starts_ret(&sha1);
1797 mbedtls_sha1_update_ret(&sha1, ssl->session_negotiate->master, 48);
1798 mbedtls_sha1_update_ret(&sha1, pad_2, 40);
1799 mbedtls_sha1_update_ret(&sha1, hash + 16, 20);
1800 mbedtls_sha1_finish_ret(&sha1, hash + 16);
Paul Bakker380da532012-04-18 16:10:25 +00001801
Manuel Pégourié-Gonnardde718b92019-05-03 11:43:28 +02001802 *hlen = 36;
1803
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001804 MBEDTLS_SSL_DEBUG_BUF(3, "calculated verify result", hash, *hlen);
1805 MBEDTLS_SSL_DEBUG_MSG(2, ("<= calc verify"));
Paul Bakker380da532012-04-18 16:10:25 +00001806
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001807 mbedtls_md5_free(&md5);
1808 mbedtls_sha1_free(&sha1);
Paul Bakker5b4af392014-06-26 12:09:34 +02001809
Paul Bakker380da532012-04-18 16:10:25 +00001810 return;
1811}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001812#endif /* MBEDTLS_SSL_PROTO_SSL3 */
Paul Bakker380da532012-04-18 16:10:25 +00001813
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001814#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001815void ssl_calc_verify_tls(const mbedtls_ssl_context *ssl,
1816 unsigned char *hash,
1817 size_t *hlen)
Paul Bakker380da532012-04-18 16:10:25 +00001818{
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02001819 mbedtls_md5_context md5;
1820 mbedtls_sha1_context sha1;
Paul Bakker380da532012-04-18 16:10:25 +00001821
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001822 MBEDTLS_SSL_DEBUG_MSG(2, ("=> calc verify tls"));
Paul Bakker380da532012-04-18 16:10:25 +00001823
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001824 mbedtls_md5_init(&md5);
1825 mbedtls_sha1_init(&sha1);
Manuel Pégourié-Gonnard001f2b62015-07-06 16:21:13 +02001826
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001827 mbedtls_md5_clone(&md5, &ssl->handshake->fin_md5);
1828 mbedtls_sha1_clone(&sha1, &ssl->handshake->fin_sha1);
Paul Bakker380da532012-04-18 16:10:25 +00001829
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001830 mbedtls_md5_finish_ret(&md5, hash);
1831 mbedtls_sha1_finish_ret(&sha1, hash + 16);
Paul Bakker380da532012-04-18 16:10:25 +00001832
Manuel Pégourié-Gonnardde718b92019-05-03 11:43:28 +02001833 *hlen = 36;
1834
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001835 MBEDTLS_SSL_DEBUG_BUF(3, "calculated verify result", hash, *hlen);
1836 MBEDTLS_SSL_DEBUG_MSG(2, ("<= calc verify"));
Paul Bakker380da532012-04-18 16:10:25 +00001837
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001838 mbedtls_md5_free(&md5);
1839 mbedtls_sha1_free(&sha1);
Paul Bakker5b4af392014-06-26 12:09:34 +02001840
Paul Bakker380da532012-04-18 16:10:25 +00001841 return;
1842}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001843#endif /* MBEDTLS_SSL_PROTO_TLS1 || MBEDTLS_SSL_PROTO_TLS1_1 */
Paul Bakker380da532012-04-18 16:10:25 +00001844
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001845#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
1846#if defined(MBEDTLS_SHA256_C)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001847void ssl_calc_verify_tls_sha256(const mbedtls_ssl_context *ssl,
1848 unsigned char *hash,
1849 size_t *hlen)
Paul Bakker380da532012-04-18 16:10:25 +00001850{
Andrzej Kurekeb342242019-01-29 09:14:33 -05001851#if defined(MBEDTLS_USE_PSA_CRYPTO)
1852 size_t hash_size;
1853 psa_status_t status;
1854 psa_hash_operation_t sha256_psa = psa_hash_operation_init();
1855
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001856 MBEDTLS_SSL_DEBUG_MSG(2, ("=> PSA calc verify sha256"));
1857 status = psa_hash_clone(&ssl->handshake->fin_sha256_psa, &sha256_psa);
1858 if (status != PSA_SUCCESS) {
1859 MBEDTLS_SSL_DEBUG_MSG(2, ("PSA hash clone failed"));
Andrzej Kurekeb342242019-01-29 09:14:33 -05001860 return;
1861 }
1862
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001863 status = psa_hash_finish(&sha256_psa, hash, 32, &hash_size);
1864 if (status != PSA_SUCCESS) {
1865 MBEDTLS_SSL_DEBUG_MSG(2, ("PSA hash finish failed"));
Andrzej Kurekeb342242019-01-29 09:14:33 -05001866 return;
1867 }
Manuel Pégourié-Gonnardde718b92019-05-03 11:43:28 +02001868
1869 *hlen = 32;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001870 MBEDTLS_SSL_DEBUG_BUF(3, "PSA calculated verify result", hash, *hlen);
1871 MBEDTLS_SSL_DEBUG_MSG(2, ("<= PSA calc verify"));
Andrzej Kurekeb342242019-01-29 09:14:33 -05001872#else
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02001873 mbedtls_sha256_context sha256;
Paul Bakker380da532012-04-18 16:10:25 +00001874
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001875 mbedtls_sha256_init(&sha256);
Manuel Pégourié-Gonnard001f2b62015-07-06 16:21:13 +02001876
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001877 MBEDTLS_SSL_DEBUG_MSG(2, ("=> calc verify sha256"));
Paul Bakker380da532012-04-18 16:10:25 +00001878
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001879 mbedtls_sha256_clone(&sha256, &ssl->handshake->fin_sha256);
1880 mbedtls_sha256_finish_ret(&sha256, hash);
Paul Bakker380da532012-04-18 16:10:25 +00001881
Manuel Pégourié-Gonnardde718b92019-05-03 11:43:28 +02001882 *hlen = 32;
1883
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001884 MBEDTLS_SSL_DEBUG_BUF(3, "calculated verify result", hash, *hlen);
1885 MBEDTLS_SSL_DEBUG_MSG(2, ("<= calc verify"));
Paul Bakker380da532012-04-18 16:10:25 +00001886
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001887 mbedtls_sha256_free(&sha256);
Andrzej Kurekeb342242019-01-29 09:14:33 -05001888#endif /* MBEDTLS_USE_PSA_CRYPTO */
Paul Bakker380da532012-04-18 16:10:25 +00001889 return;
1890}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001891#endif /* MBEDTLS_SHA256_C */
Paul Bakker380da532012-04-18 16:10:25 +00001892
Gilles Peskined2d59372021-05-12 22:43:27 +02001893#if defined(MBEDTLS_SHA512_C) && !defined(MBEDTLS_SHA512_NO_SHA384)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001894void ssl_calc_verify_tls_sha384(const mbedtls_ssl_context *ssl,
1895 unsigned char *hash,
1896 size_t *hlen)
Paul Bakker380da532012-04-18 16:10:25 +00001897{
Andrzej Kurekeb342242019-01-29 09:14:33 -05001898#if defined(MBEDTLS_USE_PSA_CRYPTO)
1899 size_t hash_size;
1900 psa_status_t status;
Andrzej Kurek972fba52019-01-30 03:29:12 -05001901 psa_hash_operation_t sha384_psa = psa_hash_operation_init();
Andrzej Kurekeb342242019-01-29 09:14:33 -05001902
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001903 MBEDTLS_SSL_DEBUG_MSG(2, ("=> PSA calc verify sha384"));
1904 status = psa_hash_clone(&ssl->handshake->fin_sha384_psa, &sha384_psa);
1905 if (status != PSA_SUCCESS) {
1906 MBEDTLS_SSL_DEBUG_MSG(2, ("PSA hash clone failed"));
Andrzej Kurekeb342242019-01-29 09:14:33 -05001907 return;
1908 }
1909
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001910 status = psa_hash_finish(&sha384_psa, hash, 48, &hash_size);
1911 if (status != PSA_SUCCESS) {
1912 MBEDTLS_SSL_DEBUG_MSG(2, ("PSA hash finish failed"));
Andrzej Kurekeb342242019-01-29 09:14:33 -05001913 return;
1914 }
Manuel Pégourié-Gonnardde718b92019-05-03 11:43:28 +02001915
1916 *hlen = 48;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001917 MBEDTLS_SSL_DEBUG_BUF(3, "PSA calculated verify result", hash, *hlen);
1918 MBEDTLS_SSL_DEBUG_MSG(2, ("<= PSA calc verify"));
Andrzej Kurekeb342242019-01-29 09:14:33 -05001919#else
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02001920 mbedtls_sha512_context sha512;
Paul Bakker380da532012-04-18 16:10:25 +00001921
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001922 mbedtls_sha512_init(&sha512);
Manuel Pégourié-Gonnard001f2b62015-07-06 16:21:13 +02001923
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001924 MBEDTLS_SSL_DEBUG_MSG(2, ("=> calc verify sha384"));
Paul Bakker380da532012-04-18 16:10:25 +00001925
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001926 mbedtls_sha512_clone(&sha512, &ssl->handshake->fin_sha512);
1927 mbedtls_sha512_finish_ret(&sha512, hash);
Paul Bakker5121ce52009-01-03 21:22:43 +00001928
Manuel Pégourié-Gonnardde718b92019-05-03 11:43:28 +02001929 *hlen = 48;
1930
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001931 MBEDTLS_SSL_DEBUG_BUF(3, "calculated verify result", hash, *hlen);
1932 MBEDTLS_SSL_DEBUG_MSG(2, ("<= calc verify"));
Paul Bakker5121ce52009-01-03 21:22:43 +00001933
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001934 mbedtls_sha512_free(&sha512);
Andrzej Kurekeb342242019-01-29 09:14:33 -05001935#endif /* MBEDTLS_USE_PSA_CRYPTO */
Paul Bakker5121ce52009-01-03 21:22:43 +00001936 return;
1937}
Gilles Peskined2d59372021-05-12 22:43:27 +02001938#endif /* MBEDTLS_SHA512_C && !MBEDTLS_SHA512_NO_SHA384 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001939#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakker5121ce52009-01-03 21:22:43 +00001940
Gilles Peskineeccd8882020-03-10 12:19:08 +01001941#if defined(MBEDTLS_KEY_EXCHANGE_SOME_PSK_ENABLED)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001942int 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 +02001943{
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001944 unsigned char *p = ssl->handshake->premaster;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001945 unsigned char *end = p + sizeof(ssl->handshake->premaster);
Guilhem Bryantb5f04e42020-04-01 11:23:58 +01001946 const unsigned char *psk = NULL;
Guilhem Bryant61b0fe62020-03-27 11:12:12 +00001947 size_t psk_len = 0;
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01001948
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001949 if (mbedtls_ssl_get_psk(ssl, &psk, &psk_len)
1950 == MBEDTLS_ERR_SSL_PRIVATE_KEY_REQUIRED) {
Guilhem Bryantc5285d82020-03-25 17:08:15 +00001951 /*
1952 * This should never happen because the existence of a PSK is always
1953 * checked before calling this function
1954 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001955 MBEDTLS_SSL_DEBUG_MSG(1, ("should never happen"));
1956 return MBEDTLS_ERR_SSL_INTERNAL_ERROR;
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01001957 }
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001958
1959 /*
1960 * PMS = struct {
1961 * opaque other_secret<0..2^16-1>;
1962 * opaque psk<0..2^16-1>;
1963 * };
1964 * with "other_secret" depending on the particular key exchange
1965 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001966#if defined(MBEDTLS_KEY_EXCHANGE_PSK_ENABLED)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001967 if (key_ex == MBEDTLS_KEY_EXCHANGE_PSK) {
1968 if (end - p < 2) {
1969 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
1970 }
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001971
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001972 MBEDTLS_PUT_UINT16_BE(psk_len, p, 0);
Joe Subbiania651e6f2021-08-23 11:35:25 +01001973 p += 2;
Manuel Pégourié-Gonnardbc5e5082015-10-21 12:35:29 +02001974
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001975 if (end < p || (size_t) (end - p) < psk_len) {
1976 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
1977 }
Manuel Pégourié-Gonnardbc5e5082015-10-21 12:35:29 +02001978
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001979 memset(p, 0, psk_len);
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01001980 p += psk_len;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001981 } else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001982#endif /* MBEDTLS_KEY_EXCHANGE_PSK_ENABLED */
1983#if defined(MBEDTLS_KEY_EXCHANGE_RSA_PSK_ENABLED)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001984 if (key_ex == MBEDTLS_KEY_EXCHANGE_RSA_PSK) {
Manuel Pégourié-Gonnard0fae60b2013-10-14 17:39:48 +02001985 /*
1986 * other_secret already set by the ClientKeyExchange message,
1987 * and is 48 bytes long
1988 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001989 if (end - p < 2) {
1990 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
1991 }
Philippe Antoine747fd532018-05-30 09:13:21 +02001992
Manuel Pégourié-Gonnard0fae60b2013-10-14 17:39:48 +02001993 *p++ = 0;
1994 *p++ = 48;
1995 p += 48;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001996 } else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001997#endif /* MBEDTLS_KEY_EXCHANGE_RSA_PSK_ENABLED */
1998#if defined(MBEDTLS_KEY_EXCHANGE_DHE_PSK_ENABLED)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001999 if (key_ex == MBEDTLS_KEY_EXCHANGE_DHE_PSK) {
Janos Follath865b3eb2019-12-16 11:46:15 +00002000 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnard33352052015-06-02 16:17:08 +01002001 size_t len;
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02002002
Manuel Pégourié-Gonnard8df68632014-06-23 17:56:08 +02002003 /* Write length only when we know the actual value */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002004 if ((ret = mbedtls_dhm_calc_secret(&ssl->handshake->dhm_ctx,
2005 p + 2, end - (p + 2), &len,
2006 ssl->conf->f_rng, ssl->conf->p_rng)) != 0) {
2007 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_dhm_calc_secret", ret);
2008 return ret;
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02002009 }
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002010 MBEDTLS_PUT_UINT16_BE(len, p, 0);
Joe Subbiania651e6f2021-08-23 11:35:25 +01002011 p += 2 + len;
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02002012
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002013 MBEDTLS_SSL_DEBUG_MPI(3, "DHM: K ", &ssl->handshake->dhm_ctx.K);
2014 } else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002015#endif /* MBEDTLS_KEY_EXCHANGE_DHE_PSK_ENABLED */
2016#if defined(MBEDTLS_KEY_EXCHANGE_ECDHE_PSK_ENABLED)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002017 if (key_ex == MBEDTLS_KEY_EXCHANGE_ECDHE_PSK) {
Janos Follath865b3eb2019-12-16 11:46:15 +00002018 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02002019 size_t zlen;
2020
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002021 if ((ret = mbedtls_ecdh_calc_secret(&ssl->handshake->ecdh_ctx, &zlen,
2022 p + 2, end - (p + 2),
2023 ssl->conf->f_rng, ssl->conf->p_rng)) != 0) {
2024 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ecdh_calc_secret", ret);
2025 return ret;
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02002026 }
2027
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002028 MBEDTLS_PUT_UINT16_BE(zlen, p, 0);
Joe Subbiania651e6f2021-08-23 11:35:25 +01002029 p += 2 + zlen;
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02002030
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002031 MBEDTLS_SSL_DEBUG_ECDH(3, &ssl->handshake->ecdh_ctx,
2032 MBEDTLS_DEBUG_ECDH_Z);
2033 } else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002034#endif /* MBEDTLS_KEY_EXCHANGE_ECDHE_PSK_ENABLED */
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02002035 {
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002036 MBEDTLS_SSL_DEBUG_MSG(1, ("should never happen"));
2037 return MBEDTLS_ERR_SSL_INTERNAL_ERROR;
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02002038 }
2039
2040 /* opaque psk<0..2^16-1>; */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002041 if (end - p < 2) {
2042 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
2043 }
Manuel Pégourié-Gonnardb2bf5a12014-03-25 16:28:12 +01002044
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002045 MBEDTLS_PUT_UINT16_BE(psk_len, p, 0);
Joe Subbiania651e6f2021-08-23 11:35:25 +01002046 p += 2;
Manuel Pégourié-Gonnardbc5e5082015-10-21 12:35:29 +02002047
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002048 if (end < p || (size_t) (end - p) < psk_len) {
2049 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
2050 }
Manuel Pégourié-Gonnardbc5e5082015-10-21 12:35:29 +02002051
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002052 memcpy(p, psk, psk_len);
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01002053 p += psk_len;
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02002054
2055 ssl->handshake->pmslen = p - ssl->handshake->premaster;
2056
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002057 return 0;
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02002058}
Gilles Peskineeccd8882020-03-10 12:19:08 +01002059#endif /* MBEDTLS_KEY_EXCHANGE_SOME_PSK_ENABLED */
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02002060
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002061#if defined(MBEDTLS_SSL_SRV_C) && defined(MBEDTLS_SSL_RENEGOTIATION)
Manuel Pégourié-Gonnardd904d662022-06-17 10:24:00 +02002062MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002063static int ssl_write_hello_request(mbedtls_ssl_context *ssl);
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +02002064
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002065#if defined(MBEDTLS_SSL_PROTO_DTLS)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002066int mbedtls_ssl_resend_hello_request(mbedtls_ssl_context *ssl)
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +02002067{
2068 /* If renegotiation is not enforced, retransmit until we would reach max
2069 * timeout if we were using the usual handshake doubling scheme */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002070 if (ssl->conf->renego_max_records < 0) {
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02002071 uint32_t ratio = ssl->conf->hs_timeout_max / ssl->conf->hs_timeout_min + 1;
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +02002072 unsigned char doublings = 1;
2073
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002074 while (ratio != 0) {
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +02002075 ++doublings;
2076 ratio >>= 1;
2077 }
2078
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002079 if (++ssl->renego_records_seen > doublings) {
2080 MBEDTLS_SSL_DEBUG_MSG(2, ("no longer retransmitting hello request"));
2081 return 0;
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +02002082 }
2083 }
2084
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002085 return ssl_write_hello_request(ssl);
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +02002086}
2087#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002088#endif /* MBEDTLS_SSL_SRV_C && MBEDTLS_SSL_RENEGOTIATION */
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02002089
Hanno Beckerb9d44792019-02-08 07:19:04 +00002090#if defined(MBEDTLS_X509_CRT_PARSE_C)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002091static void ssl_clear_peer_cert(mbedtls_ssl_session *session)
Hanno Beckerb9d44792019-02-08 07:19:04 +00002092{
2093#if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002094 if (session->peer_cert != NULL) {
2095 mbedtls_x509_crt_free(session->peer_cert);
2096 mbedtls_free(session->peer_cert);
Hanno Beckerb9d44792019-02-08 07:19:04 +00002097 session->peer_cert = NULL;
2098 }
2099#else /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002100 if (session->peer_cert_digest != NULL) {
Hanno Beckerb9d44792019-02-08 07:19:04 +00002101 /* Zeroization is not necessary. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002102 mbedtls_free(session->peer_cert_digest);
Hanno Beckerb9d44792019-02-08 07:19:04 +00002103 session->peer_cert_digest = NULL;
2104 session->peer_cert_digest_type = MBEDTLS_MD_NONE;
2105 session->peer_cert_digest_len = 0;
2106 }
2107#endif /* !MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
2108}
2109#endif /* MBEDTLS_X509_CRT_PARSE_C */
2110
Paul Bakker5121ce52009-01-03 21:22:43 +00002111/*
2112 * Handshake functions
2113 */
Gilles Peskineeccd8882020-03-10 12:19:08 +01002114#if !defined(MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED)
Gilles Peskinef9828522017-05-03 12:28:43 +02002115/* No certificate support -> dummy functions */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002116int mbedtls_ssl_write_certificate(mbedtls_ssl_context *ssl)
Paul Bakker5121ce52009-01-03 21:22:43 +00002117{
Hanno Beckere694c3e2017-12-27 21:34:08 +00002118 const mbedtls_ssl_ciphersuite_t *ciphersuite_info =
2119 ssl->handshake->ciphersuite_info;
Paul Bakker5121ce52009-01-03 21:22:43 +00002120
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002121 MBEDTLS_SSL_DEBUG_MSG(2, ("=> write certificate"));
Paul Bakker5121ce52009-01-03 21:22:43 +00002122
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002123 if (!mbedtls_ssl_ciphersuite_uses_srv_cert(ciphersuite_info)) {
2124 MBEDTLS_SSL_DEBUG_MSG(2, ("<= skip write certificate"));
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02002125 ssl->state++;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002126 return 0;
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02002127 }
2128
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002129 MBEDTLS_SSL_DEBUG_MSG(1, ("should never happen"));
2130 return MBEDTLS_ERR_SSL_INTERNAL_ERROR;
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002131}
2132
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002133int mbedtls_ssl_parse_certificate(mbedtls_ssl_context *ssl)
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002134{
Hanno Beckere694c3e2017-12-27 21:34:08 +00002135 const mbedtls_ssl_ciphersuite_t *ciphersuite_info =
2136 ssl->handshake->ciphersuite_info;
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002137
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002138 MBEDTLS_SSL_DEBUG_MSG(2, ("=> parse certificate"));
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002139
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002140 if (!mbedtls_ssl_ciphersuite_uses_srv_cert(ciphersuite_info)) {
2141 MBEDTLS_SSL_DEBUG_MSG(2, ("<= skip parse certificate"));
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002142 ssl->state++;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002143 return 0;
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002144 }
2145
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002146 MBEDTLS_SSL_DEBUG_MSG(1, ("should never happen"));
2147 return MBEDTLS_ERR_SSL_INTERNAL_ERROR;
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002148}
Gilles Peskinef9828522017-05-03 12:28:43 +02002149
Gilles Peskineeccd8882020-03-10 12:19:08 +01002150#else /* MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED */
Gilles Peskinef9828522017-05-03 12:28:43 +02002151/* Some certificate support -> implement write and parse */
2152
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002153int mbedtls_ssl_write_certificate(mbedtls_ssl_context *ssl)
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002154{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002155 int ret = MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002156 size_t i, n;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002157 const mbedtls_x509_crt *crt;
Hanno Beckere694c3e2017-12-27 21:34:08 +00002158 const mbedtls_ssl_ciphersuite_t *ciphersuite_info =
2159 ssl->handshake->ciphersuite_info;
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002160
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002161 MBEDTLS_SSL_DEBUG_MSG(2, ("=> write certificate"));
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002162
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002163 if (!mbedtls_ssl_ciphersuite_uses_srv_cert(ciphersuite_info)) {
2164 MBEDTLS_SSL_DEBUG_MSG(2, ("<= skip write certificate"));
Johan Pascal4f099262020-09-22 10:59:26 +02002165 ssl->state++;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002166 return 0;
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002167 }
2168
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002169#if defined(MBEDTLS_SSL_CLI_C)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002170 if (ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT) {
2171 if (ssl->client_auth == 0) {
2172 MBEDTLS_SSL_DEBUG_MSG(2, ("<= skip write certificate"));
Paul Bakker5121ce52009-01-03 21:22:43 +00002173 ssl->state++;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002174 return 0;
Paul Bakker5121ce52009-01-03 21:22:43 +00002175 }
2176
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002177#if defined(MBEDTLS_SSL_PROTO_SSL3)
Paul Bakker5121ce52009-01-03 21:22:43 +00002178 /*
2179 * If using SSLv3 and got no cert, send an Alert message
2180 * (otherwise an empty Certificate message will be sent).
2181 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002182 if (mbedtls_ssl_own_cert(ssl) == NULL &&
2183 ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0) {
Paul Bakker5121ce52009-01-03 21:22:43 +00002184 ssl->out_msglen = 2;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002185 ssl->out_msgtype = MBEDTLS_SSL_MSG_ALERT;
2186 ssl->out_msg[0] = MBEDTLS_SSL_ALERT_LEVEL_WARNING;
2187 ssl->out_msg[1] = MBEDTLS_SSL_ALERT_MSG_NO_CERT;
Paul Bakker5121ce52009-01-03 21:22:43 +00002188
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002189 MBEDTLS_SSL_DEBUG_MSG(2, ("got no certificate to send"));
Paul Bakker5121ce52009-01-03 21:22:43 +00002190 goto write_msg;
2191 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002192#endif /* MBEDTLS_SSL_PROTO_SSL3 */
Paul Bakker5121ce52009-01-03 21:22:43 +00002193 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002194#endif /* MBEDTLS_SSL_CLI_C */
2195#if defined(MBEDTLS_SSL_SRV_C)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002196 if (ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER) {
2197 if (mbedtls_ssl_own_cert(ssl) == NULL) {
2198 MBEDTLS_SSL_DEBUG_MSG(1, ("got no certificate to send"));
2199 return MBEDTLS_ERR_SSL_CERTIFICATE_REQUIRED;
Paul Bakker5121ce52009-01-03 21:22:43 +00002200 }
2201 }
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01002202#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00002203
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002204 MBEDTLS_SSL_DEBUG_CRT(3, "own certificate", mbedtls_ssl_own_cert(ssl));
Paul Bakker5121ce52009-01-03 21:22:43 +00002205
2206 /*
2207 * 0 . 0 handshake type
2208 * 1 . 3 handshake length
2209 * 4 . 6 length of all certs
2210 * 7 . 9 length of cert. 1
2211 * 10 . n-1 peer certificate
2212 * n . n+2 length of cert. 2
2213 * n+3 . ... upper level cert, etc.
2214 */
2215 i = 7;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002216 crt = mbedtls_ssl_own_cert(ssl);
Paul Bakker5121ce52009-01-03 21:22:43 +00002217
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002218 while (crt != NULL) {
Paul Bakker5121ce52009-01-03 21:22:43 +00002219 n = crt->raw.len;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002220 if (n > MBEDTLS_SSL_OUT_CONTENT_LEN - 3 - i) {
2221 MBEDTLS_SSL_DEBUG_MSG(1, ("certificate too large, %" MBEDTLS_PRINTF_SIZET
2222 " > %" MBEDTLS_PRINTF_SIZET,
2223 i + 3 + n, (size_t) MBEDTLS_SSL_OUT_CONTENT_LEN));
2224 return MBEDTLS_ERR_SSL_CERTIFICATE_TOO_LARGE;
Paul Bakker5121ce52009-01-03 21:22:43 +00002225 }
2226
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002227 ssl->out_msg[i] = MBEDTLS_BYTE_2(n);
2228 ssl->out_msg[i + 1] = MBEDTLS_BYTE_1(n);
2229 ssl->out_msg[i + 2] = MBEDTLS_BYTE_0(n);
Paul Bakker5121ce52009-01-03 21:22:43 +00002230
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002231 i += 3; memcpy(ssl->out_msg + i, crt->raw.p, n);
Paul Bakker5121ce52009-01-03 21:22:43 +00002232 i += n; crt = crt->next;
2233 }
2234
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002235 ssl->out_msg[4] = MBEDTLS_BYTE_2(i - 7);
2236 ssl->out_msg[5] = MBEDTLS_BYTE_1(i - 7);
2237 ssl->out_msg[6] = MBEDTLS_BYTE_0(i - 7);
Paul Bakker5121ce52009-01-03 21:22:43 +00002238
2239 ssl->out_msglen = i;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002240 ssl->out_msgtype = MBEDTLS_SSL_MSG_HANDSHAKE;
2241 ssl->out_msg[0] = MBEDTLS_SSL_HS_CERTIFICATE;
Paul Bakker5121ce52009-01-03 21:22:43 +00002242
Manuel Pégourié-Gonnardcf141ca2015-05-20 10:35:51 +02002243#if defined(MBEDTLS_SSL_PROTO_SSL3) && defined(MBEDTLS_SSL_CLI_C)
Paul Bakker5121ce52009-01-03 21:22:43 +00002244write_msg:
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002245#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00002246
2247 ssl->state++;
2248
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002249 if ((ret = mbedtls_ssl_write_handshake_msg(ssl)) != 0) {
2250 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ssl_write_handshake_msg", ret);
2251 return ret;
Paul Bakker5121ce52009-01-03 21:22:43 +00002252 }
2253
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002254 MBEDTLS_SSL_DEBUG_MSG(2, ("<= write certificate"));
Paul Bakker5121ce52009-01-03 21:22:43 +00002255
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002256 return ret;
Paul Bakker5121ce52009-01-03 21:22:43 +00002257}
2258
Hanno Becker84879e32019-01-31 07:44:03 +00002259#if defined(MBEDTLS_SSL_RENEGOTIATION) && defined(MBEDTLS_SSL_CLI_C)
Hanno Becker177475a2019-02-05 17:02:46 +00002260
2261#if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
Manuel Pégourié-Gonnardd904d662022-06-17 10:24:00 +02002262MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002263static int ssl_check_peer_crt_unchanged(mbedtls_ssl_context *ssl,
2264 unsigned char *crt_buf,
2265 size_t crt_buf_len)
Hanno Beckerdef9bdc2019-01-30 14:46:46 +00002266{
2267 mbedtls_x509_crt const * const peer_crt = ssl->session->peer_cert;
2268
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002269 if (peer_crt == NULL) {
2270 return -1;
2271 }
Hanno Beckerdef9bdc2019-01-30 14:46:46 +00002272
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002273 if (peer_crt->raw.len != crt_buf_len) {
2274 return -1;
2275 }
Hanno Beckerdef9bdc2019-01-30 14:46:46 +00002276
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002277 return memcmp(peer_crt->raw.p, crt_buf, peer_crt->raw.len);
Hanno Beckerdef9bdc2019-01-30 14:46:46 +00002278}
Hanno Becker177475a2019-02-05 17:02:46 +00002279#else /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
Manuel Pégourié-Gonnardd904d662022-06-17 10:24:00 +02002280MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002281static int ssl_check_peer_crt_unchanged(mbedtls_ssl_context *ssl,
2282 unsigned char *crt_buf,
2283 size_t crt_buf_len)
Hanno Becker177475a2019-02-05 17:02:46 +00002284{
Janos Follath865b3eb2019-12-16 11:46:15 +00002285 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Hanno Becker177475a2019-02-05 17:02:46 +00002286 unsigned char const * const peer_cert_digest =
2287 ssl->session->peer_cert_digest;
2288 mbedtls_md_type_t const peer_cert_digest_type =
2289 ssl->session->peer_cert_digest_type;
2290 mbedtls_md_info_t const * const digest_info =
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002291 mbedtls_md_info_from_type(peer_cert_digest_type);
Hanno Becker177475a2019-02-05 17:02:46 +00002292 unsigned char tmp_digest[MBEDTLS_SSL_PEER_CERT_DIGEST_MAX_LEN];
2293 size_t digest_len;
2294
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002295 if (peer_cert_digest == NULL || digest_info == NULL) {
2296 return -1;
2297 }
Hanno Becker177475a2019-02-05 17:02:46 +00002298
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002299 digest_len = mbedtls_md_get_size(digest_info);
2300 if (digest_len > MBEDTLS_SSL_PEER_CERT_DIGEST_MAX_LEN) {
2301 return -1;
2302 }
Hanno Becker177475a2019-02-05 17:02:46 +00002303
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002304 ret = mbedtls_md(digest_info, crt_buf, crt_buf_len, tmp_digest);
2305 if (ret != 0) {
2306 return -1;
2307 }
Hanno Becker177475a2019-02-05 17:02:46 +00002308
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002309 return memcmp(tmp_digest, peer_cert_digest, digest_len);
Hanno Becker177475a2019-02-05 17:02:46 +00002310}
2311#endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
Hanno Becker84879e32019-01-31 07:44:03 +00002312#endif /* MBEDTLS_SSL_RENEGOTIATION && MBEDTLS_SSL_CLI_C */
Hanno Beckerdef9bdc2019-01-30 14:46:46 +00002313
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02002314/*
2315 * Once the certificate message is read, parse it into a cert chain and
2316 * perform basic checks, but leave actual verification to the caller
2317 */
Manuel Pégourié-Gonnardd904d662022-06-17 10:24:00 +02002318MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002319static int ssl_parse_certificate_chain(mbedtls_ssl_context *ssl,
2320 mbedtls_x509_crt *chain)
Paul Bakker5121ce52009-01-03 21:22:43 +00002321{
Janos Follath865b3eb2019-12-16 11:46:15 +00002322 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Hanno Beckerc7bd7802019-02-05 15:37:23 +00002323#if defined(MBEDTLS_SSL_RENEGOTIATION) && defined(MBEDTLS_SSL_CLI_C)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002324 int crt_cnt = 0;
Hanno Beckerc7bd7802019-02-05 15:37:23 +00002325#endif
Paul Bakker23986e52011-04-24 08:57:21 +00002326 size_t i, n;
Gilles Peskine064a85c2017-05-10 10:46:40 +02002327 uint8_t alert;
Paul Bakker5121ce52009-01-03 21:22:43 +00002328
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002329 if (ssl->in_msgtype != MBEDTLS_SSL_MSG_HANDSHAKE) {
2330 MBEDTLS_SSL_DEBUG_MSG(1, ("bad certificate message"));
2331 mbedtls_ssl_send_alert_message(ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
2332 MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE);
2333 return MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE;
Paul Bakker5121ce52009-01-03 21:22:43 +00002334 }
2335
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002336 if (ssl->in_msg[0] != MBEDTLS_SSL_HS_CERTIFICATE ||
2337 ssl->in_hslen < mbedtls_ssl_hs_hdr_len(ssl) + 3 + 3) {
2338 MBEDTLS_SSL_DEBUG_MSG(1, ("bad certificate message"));
2339 mbedtls_ssl_send_alert_message(ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
2340 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR);
2341 return MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE;
Paul Bakker5121ce52009-01-03 21:22:43 +00002342 }
2343
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002344 i = mbedtls_ssl_hs_hdr_len(ssl);
Manuel Pégourié-Gonnardf49a7da2014-09-10 13:30:43 +00002345
Paul Bakker5121ce52009-01-03 21:22:43 +00002346 /*
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002347 * Same message structure as in mbedtls_ssl_write_certificate()
Paul Bakker5121ce52009-01-03 21:22:43 +00002348 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002349 n = (ssl->in_msg[i+1] << 8) | ssl->in_msg[i+2];
Paul Bakker5121ce52009-01-03 21:22:43 +00002350
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002351 if (ssl->in_msg[i] != 0 ||
2352 ssl->in_hslen != n + 3 + mbedtls_ssl_hs_hdr_len(ssl)) {
2353 MBEDTLS_SSL_DEBUG_MSG(1, ("bad certificate message"));
2354 mbedtls_ssl_send_alert_message(ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
2355 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR);
2356 return MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE;
Paul Bakker5121ce52009-01-03 21:22:43 +00002357 }
2358
Hanno Beckerdef9bdc2019-01-30 14:46:46 +00002359 /* Make &ssl->in_msg[i] point to the beginning of the CRT chain. */
2360 i += 3;
2361
Hanno Beckerdef9bdc2019-01-30 14:46:46 +00002362 /* Iterate through and parse the CRTs in the provided chain. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002363 while (i < ssl->in_hslen) {
Hanno Beckerdef9bdc2019-01-30 14:46:46 +00002364 /* Check that there's room for the next CRT's length fields. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002365 if (i + 3 > ssl->in_hslen) {
2366 MBEDTLS_SSL_DEBUG_MSG(1, ("bad certificate message"));
2367 mbedtls_ssl_send_alert_message(ssl,
2368 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
2369 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR);
2370 return MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE;
Philippe Antoine747fd532018-05-30 09:13:21 +02002371 }
Hanno Beckerdef9bdc2019-01-30 14:46:46 +00002372 /* In theory, the CRT can be up to 2**24 Bytes, but we don't support
2373 * anything beyond 2**16 ~ 64K. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002374 if (ssl->in_msg[i] != 0) {
2375 MBEDTLS_SSL_DEBUG_MSG(1, ("bad certificate message"));
2376 mbedtls_ssl_send_alert_message(ssl,
2377 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
2378 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR);
2379 return MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE;
Paul Bakker5121ce52009-01-03 21:22:43 +00002380 }
2381
Hanno Beckerdef9bdc2019-01-30 14:46:46 +00002382 /* Read length of the next CRT in the chain. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002383 n = ((unsigned int) ssl->in_msg[i + 1] << 8)
Paul Bakker5121ce52009-01-03 21:22:43 +00002384 | (unsigned int) ssl->in_msg[i + 2];
2385 i += 3;
2386
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002387 if (n < 128 || i + n > ssl->in_hslen) {
2388 MBEDTLS_SSL_DEBUG_MSG(1, ("bad certificate message"));
2389 mbedtls_ssl_send_alert_message(ssl,
2390 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
2391 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR);
2392 return MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE;
Paul Bakker5121ce52009-01-03 21:22:43 +00002393 }
2394
Hanno Beckerdef9bdc2019-01-30 14:46:46 +00002395 /* Check if we're handling the first CRT in the chain. */
Hanno Beckerc7bd7802019-02-05 15:37:23 +00002396#if defined(MBEDTLS_SSL_RENEGOTIATION) && defined(MBEDTLS_SSL_CLI_C)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002397 if (crt_cnt++ == 0 &&
Hanno Beckerc7bd7802019-02-05 15:37:23 +00002398 ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT &&
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002399 ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS) {
Hanno Becker46f34d02019-02-08 14:00:04 +00002400 /* During client-side renegotiation, check that the server's
2401 * end-CRTs hasn't changed compared to the initial handshake,
2402 * mitigating the triple handshake attack. On success, reuse
2403 * the original end-CRT instead of parsing it again. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002404 MBEDTLS_SSL_DEBUG_MSG(3, ("Check that peer CRT hasn't changed during renegotiation"));
2405 if (ssl_check_peer_crt_unchanged(ssl,
2406 &ssl->in_msg[i],
2407 n) != 0) {
2408 MBEDTLS_SSL_DEBUG_MSG(1, ("new server cert during renegotiation"));
2409 mbedtls_ssl_send_alert_message(ssl,
2410 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
2411 MBEDTLS_SSL_ALERT_MSG_ACCESS_DENIED);
2412 return MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE;
Hanno Beckerdef9bdc2019-01-30 14:46:46 +00002413 }
Hanno Beckerc7bd7802019-02-05 15:37:23 +00002414
2415 /* Now we can safely free the original chain. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002416 ssl_clear_peer_cert(ssl->session);
Hanno Beckerc7bd7802019-02-05 15:37:23 +00002417 }
Hanno Beckerdef9bdc2019-01-30 14:46:46 +00002418#endif /* MBEDTLS_SSL_RENEGOTIATION && MBEDTLS_SSL_CLI_C */
2419
Hanno Beckerdef9bdc2019-01-30 14:46:46 +00002420 /* Parse the next certificate in the chain. */
Hanno Becker0056eab2019-02-08 14:39:16 +00002421#if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002422 ret = mbedtls_x509_crt_parse_der(chain, ssl->in_msg + i, n);
Hanno Becker0056eab2019-02-08 14:39:16 +00002423#else
Hanno Becker353a6f02019-02-26 11:51:34 +00002424 /* If we don't need to store the CRT chain permanently, parse
Hanno Becker0056eab2019-02-08 14:39:16 +00002425 * it in-place from the input buffer instead of making a copy. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002426 ret = mbedtls_x509_crt_parse_der_nocopy(chain, ssl->in_msg + i, n);
Hanno Becker0056eab2019-02-08 14:39:16 +00002427#endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002428 switch (ret) {
Hanno Beckerdef9bdc2019-01-30 14:46:46 +00002429 case 0: /*ok*/
2430 case MBEDTLS_ERR_X509_UNKNOWN_SIG_ALG + MBEDTLS_ERR_OID_NOT_FOUND:
2431 /* Ignore certificate with an unknown algorithm: maybe a
2432 prior certificate was already trusted. */
2433 break;
Gilles Peskine1cc8e342017-05-03 16:28:34 +02002434
Hanno Beckerdef9bdc2019-01-30 14:46:46 +00002435 case MBEDTLS_ERR_X509_ALLOC_FAILED:
2436 alert = MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR;
2437 goto crt_parse_der_failed;
Gilles Peskine1cc8e342017-05-03 16:28:34 +02002438
Hanno Beckerdef9bdc2019-01-30 14:46:46 +00002439 case MBEDTLS_ERR_X509_UNKNOWN_VERSION:
2440 alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT;
2441 goto crt_parse_der_failed;
Gilles Peskine1cc8e342017-05-03 16:28:34 +02002442
Hanno Beckerdef9bdc2019-01-30 14:46:46 +00002443 default:
2444 alert = MBEDTLS_SSL_ALERT_MSG_BAD_CERT;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002445crt_parse_der_failed:
2446 mbedtls_ssl_send_alert_message(ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL, alert);
2447 MBEDTLS_SSL_DEBUG_RET(1, " mbedtls_x509_crt_parse_der", ret);
2448 return ret;
Paul Bakker5121ce52009-01-03 21:22:43 +00002449 }
2450
2451 i += n;
2452 }
2453
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002454 MBEDTLS_SSL_DEBUG_CRT(3, "peer certificate", chain);
2455 return 0;
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02002456}
2457
Hanno Becker4a55f632019-02-05 12:49:06 +00002458#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnardd904d662022-06-17 10:24:00 +02002459MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002460static int ssl_srv_check_client_no_crt_notification(mbedtls_ssl_context *ssl)
Hanno Becker4a55f632019-02-05 12:49:06 +00002461{
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002462 if (ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT) {
2463 return -1;
2464 }
Hanno Becker4a55f632019-02-05 12:49:06 +00002465
2466#if defined(MBEDTLS_SSL_PROTO_SSL3)
2467 /*
2468 * Check if the client sent an empty certificate
2469 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002470 if (ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0) {
2471 if (ssl->in_msglen == 2 &&
Hanno Becker4a55f632019-02-05 12:49:06 +00002472 ssl->in_msgtype == MBEDTLS_SSL_MSG_ALERT &&
2473 ssl->in_msg[0] == MBEDTLS_SSL_ALERT_LEVEL_WARNING &&
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002474 ssl->in_msg[1] == MBEDTLS_SSL_ALERT_MSG_NO_CERT) {
2475 MBEDTLS_SSL_DEBUG_MSG(1, ("SSLv3 client has no certificate"));
2476 return 0;
Hanno Becker4a55f632019-02-05 12:49:06 +00002477 }
2478
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002479 return -1;
Hanno Becker4a55f632019-02-05 12:49:06 +00002480 }
2481#endif /* MBEDTLS_SSL_PROTO_SSL3 */
2482
2483#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1) || \
2484 defined(MBEDTLS_SSL_PROTO_TLS1_2)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002485 if (ssl->in_hslen == 3 + mbedtls_ssl_hs_hdr_len(ssl) &&
Hanno Becker4a55f632019-02-05 12:49:06 +00002486 ssl->in_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE &&
2487 ssl->in_msg[0] == MBEDTLS_SSL_HS_CERTIFICATE &&
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002488 memcmp(ssl->in_msg + mbedtls_ssl_hs_hdr_len(ssl), "\0\0\0", 3) == 0) {
2489 MBEDTLS_SSL_DEBUG_MSG(1, ("TLSv1 client has no certificate"));
2490 return 0;
Hanno Becker4a55f632019-02-05 12:49:06 +00002491 }
2492
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002493 return -1;
Hanno Becker4a55f632019-02-05 12:49:06 +00002494#endif /* MBEDTLS_SSL_PROTO_TLS1 || MBEDTLS_SSL_PROTO_TLS1_1 || \
2495 MBEDTLS_SSL_PROTO_TLS1_2 */
2496}
2497#endif /* MBEDTLS_SSL_SRV_C */
2498
Hanno Becker28f2fcd2019-02-07 10:11:07 +00002499/* Check if a certificate message is expected.
2500 * Return either
2501 * - SSL_CERTIFICATE_EXPECTED, or
2502 * - SSL_CERTIFICATE_SKIP
2503 * indicating whether a Certificate message is expected or not.
2504 */
2505#define SSL_CERTIFICATE_EXPECTED 0
2506#define SSL_CERTIFICATE_SKIP 1
Manuel Pégourié-Gonnardd904d662022-06-17 10:24:00 +02002507MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002508static int ssl_parse_certificate_coordinate(mbedtls_ssl_context *ssl,
2509 int authmode)
Hanno Becker28f2fcd2019-02-07 10:11:07 +00002510{
2511 const mbedtls_ssl_ciphersuite_t *ciphersuite_info =
Hanno Beckere694c3e2017-12-27 21:34:08 +00002512 ssl->handshake->ciphersuite_info;
Hanno Becker28f2fcd2019-02-07 10:11:07 +00002513
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002514 if (!mbedtls_ssl_ciphersuite_uses_srv_cert(ciphersuite_info)) {
2515 return SSL_CERTIFICATE_SKIP;
2516 }
Hanno Becker28f2fcd2019-02-07 10:11:07 +00002517
2518#if defined(MBEDTLS_SSL_SRV_C)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002519 if (ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER) {
2520 if (ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_RSA_PSK) {
2521 return SSL_CERTIFICATE_SKIP;
2522 }
Hanno Becker28f2fcd2019-02-07 10:11:07 +00002523
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002524 if (authmode == MBEDTLS_SSL_VERIFY_NONE) {
Hanno Becker28f2fcd2019-02-07 10:11:07 +00002525 ssl->session_negotiate->verify_result =
2526 MBEDTLS_X509_BADCERT_SKIP_VERIFY;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002527 return SSL_CERTIFICATE_SKIP;
Hanno Becker28f2fcd2019-02-07 10:11:07 +00002528 }
2529 }
Hanno Becker84d9d272019-03-01 08:10:46 +00002530#else
2531 ((void) authmode);
Hanno Becker28f2fcd2019-02-07 10:11:07 +00002532#endif /* MBEDTLS_SSL_SRV_C */
2533
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002534 return SSL_CERTIFICATE_EXPECTED;
Hanno Becker28f2fcd2019-02-07 10:11:07 +00002535}
2536
Manuel Pégourié-Gonnardd904d662022-06-17 10:24:00 +02002537MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002538static int ssl_parse_certificate_verify(mbedtls_ssl_context *ssl,
2539 int authmode,
2540 mbedtls_x509_crt *chain,
2541 void *rs_ctx)
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02002542{
Hanno Becker6bdfab22019-02-05 13:11:17 +00002543 int ret = 0;
Hanno Becker28f2fcd2019-02-07 10:11:07 +00002544 const mbedtls_ssl_ciphersuite_t *ciphersuite_info =
Hanno Beckere694c3e2017-12-27 21:34:08 +00002545 ssl->handshake->ciphersuite_info;
Hanno Beckerafd0b0a2019-03-27 16:55:01 +00002546 int have_ca_chain = 0;
Hanno Becker68636192019-02-05 14:36:34 +00002547
Hanno Becker8927c832019-04-03 12:52:50 +01002548 int (*f_vrfy)(void *, mbedtls_x509_crt *, int, uint32_t *);
2549 void *p_vrfy;
2550
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002551 if (authmode == MBEDTLS_SSL_VERIFY_NONE) {
2552 return 0;
2553 }
Hanno Becker68636192019-02-05 14:36:34 +00002554
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002555 if (ssl->f_vrfy != NULL) {
2556 MBEDTLS_SSL_DEBUG_MSG(3, ("Use context-specific verification callback"));
Hanno Becker8927c832019-04-03 12:52:50 +01002557 f_vrfy = ssl->f_vrfy;
2558 p_vrfy = ssl->p_vrfy;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002559 } else {
2560 MBEDTLS_SSL_DEBUG_MSG(3, ("Use configuration-specific verification callback"));
Hanno Becker8927c832019-04-03 12:52:50 +01002561 f_vrfy = ssl->conf->f_vrfy;
2562 p_vrfy = ssl->conf->p_vrfy;
2563 }
2564
Hanno Becker68636192019-02-05 14:36:34 +00002565 /*
2566 * Main check: verify certificate
2567 */
Hanno Beckerafd0b0a2019-03-27 16:55:01 +00002568#if defined(MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002569 if (ssl->conf->f_ca_cb != NULL) {
Hanno Beckerafd0b0a2019-03-27 16:55:01 +00002570 ((void) rs_ctx);
2571 have_ca_chain = 1;
2572
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002573 MBEDTLS_SSL_DEBUG_MSG(3, ("use CA callback for X.509 CRT verification"));
Jarno Lamsa9822c0d2019-04-01 16:59:48 +03002574 ret = mbedtls_x509_crt_verify_with_ca_cb(
Hanno Beckerafd0b0a2019-03-27 16:55:01 +00002575 chain,
2576 ssl->conf->f_ca_cb,
2577 ssl->conf->p_ca_cb,
2578 ssl->conf->cert_profile,
2579 ssl->hostname,
2580 &ssl->session_negotiate->verify_result,
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002581 f_vrfy, p_vrfy);
2582 } else
Hanno Beckerafd0b0a2019-03-27 16:55:01 +00002583#endif /* MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK */
2584 {
2585 mbedtls_x509_crt *ca_chain;
2586 mbedtls_x509_crl *ca_crl;
2587
2588#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002589 if (ssl->handshake->sni_ca_chain != NULL) {
Hanno Beckerafd0b0a2019-03-27 16:55:01 +00002590 ca_chain = ssl->handshake->sni_ca_chain;
2591 ca_crl = ssl->handshake->sni_ca_crl;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002592 } else
Hanno Beckerafd0b0a2019-03-27 16:55:01 +00002593#endif
2594 {
2595 ca_chain = ssl->conf->ca_chain;
2596 ca_crl = ssl->conf->ca_crl;
2597 }
2598
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002599 if (ca_chain != NULL) {
Hanno Beckerafd0b0a2019-03-27 16:55:01 +00002600 have_ca_chain = 1;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002601 }
Hanno Beckerafd0b0a2019-03-27 16:55:01 +00002602
2603 ret = mbedtls_x509_crt_verify_restartable(
2604 chain,
2605 ca_chain, ca_crl,
2606 ssl->conf->cert_profile,
2607 ssl->hostname,
2608 &ssl->session_negotiate->verify_result,
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002609 f_vrfy, p_vrfy, rs_ctx);
Hanno Beckerafd0b0a2019-03-27 16:55:01 +00002610 }
Hanno Becker68636192019-02-05 14:36:34 +00002611
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002612 if (ret != 0) {
2613 MBEDTLS_SSL_DEBUG_RET(1, "x509_verify_cert", ret);
Hanno Becker68636192019-02-05 14:36:34 +00002614 }
2615
Gilles Peskineeccd8882020-03-10 12:19:08 +01002616#if defined(MBEDTLS_SSL_ECP_RESTARTABLE_ENABLED)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002617 if (ret == MBEDTLS_ERR_ECP_IN_PROGRESS) {
2618 return MBEDTLS_ERR_SSL_CRYPTO_IN_PROGRESS;
2619 }
Hanno Becker68636192019-02-05 14:36:34 +00002620#endif
2621
2622 /*
2623 * Secondary checks: always done, but change 'ret' only if it was 0
2624 */
2625
2626#if defined(MBEDTLS_ECP_C)
2627 {
2628 const mbedtls_pk_context *pk = &chain->pk;
2629
Manuel Pégourié-Gonnard06e1fcd2022-06-16 10:48:06 +02002630 /* If certificate uses an EC key, make sure the curve is OK.
2631 * This is a public key, so it can't be opaque, so can_do() is a good
2632 * enough check to ensure pk_ec() is safe to use here. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002633 if (mbedtls_pk_can_do(pk, MBEDTLS_PK_ECKEY) &&
2634 mbedtls_ssl_check_curve(ssl, mbedtls_pk_ec(*pk)->grp.id) != 0) {
Hanno Becker68636192019-02-05 14:36:34 +00002635 ssl->session_negotiate->verify_result |= MBEDTLS_X509_BADCERT_BAD_KEY;
2636
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002637 MBEDTLS_SSL_DEBUG_MSG(1, ("bad certificate (EC key curve)"));
2638 if (ret == 0) {
Hanno Becker68636192019-02-05 14:36:34 +00002639 ret = MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002640 }
Hanno Becker68636192019-02-05 14:36:34 +00002641 }
2642 }
2643#endif /* MBEDTLS_ECP_C */
2644
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002645 if (mbedtls_ssl_check_cert_usage(chain,
2646 ciphersuite_info,
2647 !ssl->conf->endpoint,
2648 &ssl->session_negotiate->verify_result) != 0) {
2649 MBEDTLS_SSL_DEBUG_MSG(1, ("bad certificate (usage extensions)"));
2650 if (ret == 0) {
Hanno Becker68636192019-02-05 14:36:34 +00002651 ret = MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002652 }
Hanno Becker68636192019-02-05 14:36:34 +00002653 }
2654
2655 /* mbedtls_x509_crt_verify_with_profile is supposed to report a
2656 * verification failure through MBEDTLS_ERR_X509_CERT_VERIFY_FAILED,
2657 * with details encoded in the verification flags. All other kinds
2658 * of error codes, including those from the user provided f_vrfy
2659 * functions, are treated as fatal and lead to a failure of
2660 * ssl_parse_certificate even if verification was optional. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002661 if (authmode == MBEDTLS_SSL_VERIFY_OPTIONAL &&
2662 (ret == MBEDTLS_ERR_X509_CERT_VERIFY_FAILED ||
2663 ret == MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE)) {
Hanno Becker68636192019-02-05 14:36:34 +00002664 ret = 0;
2665 }
2666
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002667 if (have_ca_chain == 0 && authmode == MBEDTLS_SSL_VERIFY_REQUIRED) {
2668 MBEDTLS_SSL_DEBUG_MSG(1, ("got no CA chain"));
Hanno Becker68636192019-02-05 14:36:34 +00002669 ret = MBEDTLS_ERR_SSL_CA_CHAIN_REQUIRED;
2670 }
2671
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002672 if (ret != 0) {
Hanno Becker68636192019-02-05 14:36:34 +00002673 uint8_t alert;
2674
2675 /* The certificate may have been rejected for several reasons.
2676 Pick one and send the corresponding alert. Which alert to send
2677 may be a subject of debate in some cases. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002678 if (ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_OTHER) {
Hanno Becker68636192019-02-05 14:36:34 +00002679 alert = MBEDTLS_SSL_ALERT_MSG_ACCESS_DENIED;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002680 } else if (ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_CN_MISMATCH) {
Hanno Becker68636192019-02-05 14:36:34 +00002681 alert = MBEDTLS_SSL_ALERT_MSG_BAD_CERT;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002682 } else if (ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_KEY_USAGE) {
Hanno Becker68636192019-02-05 14:36:34 +00002683 alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002684 } else if (ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_EXT_KEY_USAGE) {
Hanno Becker68636192019-02-05 14:36:34 +00002685 alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002686 } else if (ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_NS_CERT_TYPE) {
Hanno Becker68636192019-02-05 14:36:34 +00002687 alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002688 } else if (ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_BAD_PK) {
Hanno Becker68636192019-02-05 14:36:34 +00002689 alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002690 } else if (ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_BAD_KEY) {
Hanno Becker68636192019-02-05 14:36:34 +00002691 alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002692 } else if (ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_EXPIRED) {
Hanno Becker68636192019-02-05 14:36:34 +00002693 alert = MBEDTLS_SSL_ALERT_MSG_CERT_EXPIRED;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002694 } else if (ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_REVOKED) {
Hanno Becker68636192019-02-05 14:36:34 +00002695 alert = MBEDTLS_SSL_ALERT_MSG_CERT_REVOKED;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002696 } else if (ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_NOT_TRUSTED) {
Hanno Becker68636192019-02-05 14:36:34 +00002697 alert = MBEDTLS_SSL_ALERT_MSG_UNKNOWN_CA;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002698 } else {
Hanno Becker68636192019-02-05 14:36:34 +00002699 alert = MBEDTLS_SSL_ALERT_MSG_CERT_UNKNOWN;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002700 }
2701 mbedtls_ssl_send_alert_message(ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
2702 alert);
Hanno Becker68636192019-02-05 14:36:34 +00002703 }
2704
2705#if defined(MBEDTLS_DEBUG_C)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002706 if (ssl->session_negotiate->verify_result != 0) {
2707 MBEDTLS_SSL_DEBUG_MSG(3, ("! Certificate verification flags %08x",
2708 (unsigned int) ssl->session_negotiate->verify_result));
2709 } else {
2710 MBEDTLS_SSL_DEBUG_MSG(3, ("Certificate verification flags clear"));
Hanno Becker68636192019-02-05 14:36:34 +00002711 }
2712#endif /* MBEDTLS_DEBUG_C */
2713
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002714 return ret;
Hanno Becker68636192019-02-05 14:36:34 +00002715}
2716
Hanno Becker6b8fbab2019-02-08 14:59:05 +00002717#if !defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
Manuel Pégourié-Gonnardd904d662022-06-17 10:24:00 +02002718MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002719static int ssl_remember_peer_crt_digest(mbedtls_ssl_context *ssl,
2720 unsigned char *start, size_t len)
Hanno Becker6b8fbab2019-02-08 14:59:05 +00002721{
Janos Follath865b3eb2019-12-16 11:46:15 +00002722 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Hanno Becker6b8fbab2019-02-08 14:59:05 +00002723 /* Remember digest of the peer's end-CRT. */
2724 ssl->session_negotiate->peer_cert_digest =
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002725 mbedtls_calloc(1, MBEDTLS_SSL_PEER_CERT_DIGEST_DFL_LEN);
2726 if (ssl->session_negotiate->peer_cert_digest == NULL) {
2727 MBEDTLS_SSL_DEBUG_MSG(1, ("alloc(%d bytes) failed",
2728 MBEDTLS_SSL_PEER_CERT_DIGEST_DFL_LEN));
2729 mbedtls_ssl_send_alert_message(ssl,
2730 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
2731 MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR);
Hanno Becker6b8fbab2019-02-08 14:59:05 +00002732
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002733 return MBEDTLS_ERR_SSL_ALLOC_FAILED;
Hanno Becker6b8fbab2019-02-08 14:59:05 +00002734 }
2735
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002736 ret = mbedtls_md(mbedtls_md_info_from_type(
2737 MBEDTLS_SSL_PEER_CERT_DIGEST_DFL_TYPE),
2738 start, len,
2739 ssl->session_negotiate->peer_cert_digest);
Hanno Becker6b8fbab2019-02-08 14:59:05 +00002740
2741 ssl->session_negotiate->peer_cert_digest_type =
2742 MBEDTLS_SSL_PEER_CERT_DIGEST_DFL_TYPE;
2743 ssl->session_negotiate->peer_cert_digest_len =
2744 MBEDTLS_SSL_PEER_CERT_DIGEST_DFL_LEN;
2745
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002746 return ret;
Hanno Becker6b8fbab2019-02-08 14:59:05 +00002747}
2748
Manuel Pégourié-Gonnardd904d662022-06-17 10:24:00 +02002749MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002750static int ssl_remember_peer_pubkey(mbedtls_ssl_context *ssl,
2751 unsigned char *start, size_t len)
Hanno Becker6b8fbab2019-02-08 14:59:05 +00002752{
2753 unsigned char *end = start + len;
Janos Follath865b3eb2019-12-16 11:46:15 +00002754 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Hanno Becker6b8fbab2019-02-08 14:59:05 +00002755
2756 /* Make a copy of the peer's raw public key. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002757 mbedtls_pk_init(&ssl->handshake->peer_pubkey);
2758 ret = mbedtls_pk_parse_subpubkey(&start, end,
2759 &ssl->handshake->peer_pubkey);
2760 if (ret != 0) {
Hanno Becker6b8fbab2019-02-08 14:59:05 +00002761 /* We should have parsed the public key before. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002762 return MBEDTLS_ERR_SSL_INTERNAL_ERROR;
Hanno Becker6b8fbab2019-02-08 14:59:05 +00002763 }
2764
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002765 return 0;
Hanno Becker6b8fbab2019-02-08 14:59:05 +00002766}
2767#endif /* !MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
2768
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002769int mbedtls_ssl_parse_certificate(mbedtls_ssl_context *ssl)
Hanno Becker68636192019-02-05 14:36:34 +00002770{
2771 int ret = 0;
Hanno Becker28f2fcd2019-02-07 10:11:07 +00002772 int crt_expected;
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02002773#if defined(MBEDTLS_SSL_SRV_C) && defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
2774 const int authmode = ssl->handshake->sni_authmode != MBEDTLS_SSL_VERIFY_UNSET
2775 ? ssl->handshake->sni_authmode
2776 : ssl->conf->authmode;
2777#else
Johan Pascal4f099262020-09-22 10:59:26 +02002778 const int authmode = ssl->conf->authmode;
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02002779#endif
Manuel Pégourié-Gonnard3bf49c42017-08-15 13:47:06 +02002780 void *rs_ctx = NULL;
Hanno Becker3dad3112019-02-05 17:19:52 +00002781 mbedtls_x509_crt *chain = NULL;
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02002782
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002783 MBEDTLS_SSL_DEBUG_MSG(2, ("=> parse certificate"));
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02002784
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002785 crt_expected = ssl_parse_certificate_coordinate(ssl, authmode);
2786 if (crt_expected == SSL_CERTIFICATE_SKIP) {
2787 MBEDTLS_SSL_DEBUG_MSG(2, ("<= skip parse certificate"));
Hanno Becker6bdfab22019-02-05 13:11:17 +00002788 goto exit;
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02002789 }
2790
Gilles Peskineeccd8882020-03-10 12:19:08 +01002791#if defined(MBEDTLS_SSL_ECP_RESTARTABLE_ENABLED)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002792 if (ssl->handshake->ecrs_enabled &&
2793 ssl->handshake->ecrs_state == ssl_ecrs_crt_verify) {
Hanno Becker3dad3112019-02-05 17:19:52 +00002794 chain = ssl->handshake->ecrs_peer_cert;
2795 ssl->handshake->ecrs_peer_cert = NULL;
Manuel Pégourié-Gonnard3bf49c42017-08-15 13:47:06 +02002796 goto crt_verify;
2797 }
2798#endif
2799
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002800 if ((ret = mbedtls_ssl_read_record(ssl, 1)) != 0) {
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02002801 /* mbedtls_ssl_read_record may have sent an alert already. We
2802 let it decide whether to alert. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002803 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ssl_read_record", ret);
Hanno Becker3dad3112019-02-05 17:19:52 +00002804 goto exit;
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02002805 }
2806
Hanno Becker4a55f632019-02-05 12:49:06 +00002807#if defined(MBEDTLS_SSL_SRV_C)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002808 if (ssl_srv_check_client_no_crt_notification(ssl) == 0) {
Hanno Becker4a55f632019-02-05 12:49:06 +00002809 ssl->session_negotiate->verify_result = MBEDTLS_X509_BADCERT_MISSING;
Hanno Becker4a55f632019-02-05 12:49:06 +00002810
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002811 if (authmode != MBEDTLS_SSL_VERIFY_OPTIONAL) {
Hanno Becker6bdfab22019-02-05 13:11:17 +00002812 ret = MBEDTLS_ERR_SSL_NO_CLIENT_CERTIFICATE;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002813 }
Hanno Becker4a55f632019-02-05 12:49:06 +00002814
Hanno Becker6bdfab22019-02-05 13:11:17 +00002815 goto exit;
Hanno Becker4a55f632019-02-05 12:49:06 +00002816 }
2817#endif /* MBEDTLS_SSL_SRV_C */
2818
Hanno Beckerc7bd7802019-02-05 15:37:23 +00002819 /* Clear existing peer CRT structure in case we tried to
2820 * reuse a session but it failed, and allocate a new one. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002821 ssl_clear_peer_cert(ssl->session_negotiate);
Hanno Becker3dad3112019-02-05 17:19:52 +00002822
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002823 chain = mbedtls_calloc(1, sizeof(mbedtls_x509_crt));
2824 if (chain == NULL) {
2825 MBEDTLS_SSL_DEBUG_MSG(1, ("alloc(%" MBEDTLS_PRINTF_SIZET " bytes) failed",
2826 sizeof(mbedtls_x509_crt)));
2827 mbedtls_ssl_send_alert_message(ssl,
2828 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
2829 MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR);
Hanno Becker7a955a02019-02-05 13:08:01 +00002830
Hanno Becker3dad3112019-02-05 17:19:52 +00002831 ret = MBEDTLS_ERR_SSL_ALLOC_FAILED;
2832 goto exit;
2833 }
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002834 mbedtls_x509_crt_init(chain);
Hanno Becker3dad3112019-02-05 17:19:52 +00002835
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002836 ret = ssl_parse_certificate_chain(ssl, chain);
2837 if (ret != 0) {
Hanno Becker3dad3112019-02-05 17:19:52 +00002838 goto exit;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002839 }
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02002840
Gilles Peskineeccd8882020-03-10 12:19:08 +01002841#if defined(MBEDTLS_SSL_ECP_RESTARTABLE_ENABLED)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002842 if (ssl->handshake->ecrs_enabled) {
Manuel Pégourié-Gonnard0b23f162017-08-24 12:08:33 +02002843 ssl->handshake->ecrs_state = ssl_ecrs_crt_verify;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002844 }
Manuel Pégourié-Gonnard3bf49c42017-08-15 13:47:06 +02002845
2846crt_verify:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002847 if (ssl->handshake->ecrs_enabled) {
Manuel Pégourié-Gonnard3bf49c42017-08-15 13:47:06 +02002848 rs_ctx = &ssl->handshake->ecrs_ctx;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002849 }
Manuel Pégourié-Gonnard3bf49c42017-08-15 13:47:06 +02002850#endif
2851
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002852 ret = ssl_parse_certificate_verify(ssl, authmode,
2853 chain, rs_ctx);
2854 if (ret != 0) {
Hanno Becker3dad3112019-02-05 17:19:52 +00002855 goto exit;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002856 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002857
Hanno Becker6bbd94c2019-02-05 17:02:28 +00002858#if !defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
Hanno Becker6bbd94c2019-02-05 17:02:28 +00002859 {
Hanno Becker6b8fbab2019-02-08 14:59:05 +00002860 unsigned char *crt_start, *pk_start;
2861 size_t crt_len, pk_len;
Hanno Becker3dad3112019-02-05 17:19:52 +00002862
Hanno Becker6b8fbab2019-02-08 14:59:05 +00002863 /* We parse the CRT chain without copying, so
2864 * these pointers point into the input buffer,
2865 * and are hence still valid after freeing the
2866 * CRT chain. */
Hanno Becker6bbd94c2019-02-05 17:02:28 +00002867
Hanno Becker6b8fbab2019-02-08 14:59:05 +00002868 crt_start = chain->raw.p;
2869 crt_len = chain->raw.len;
Hanno Becker6bbd94c2019-02-05 17:02:28 +00002870
Hanno Becker6b8fbab2019-02-08 14:59:05 +00002871 pk_start = chain->pk_raw.p;
2872 pk_len = chain->pk_raw.len;
2873
2874 /* Free the CRT structures before computing
2875 * digest and copying the peer's public key. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002876 mbedtls_x509_crt_free(chain);
2877 mbedtls_free(chain);
Hanno Becker6b8fbab2019-02-08 14:59:05 +00002878 chain = NULL;
2879
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002880 ret = ssl_remember_peer_crt_digest(ssl, crt_start, crt_len);
2881 if (ret != 0) {
Hanno Beckera2747532019-02-06 16:19:04 +00002882 goto exit;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002883 }
Hanno Becker6b8fbab2019-02-08 14:59:05 +00002884
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002885 ret = ssl_remember_peer_pubkey(ssl, pk_start, pk_len);
2886 if (ret != 0) {
Hanno Becker6b8fbab2019-02-08 14:59:05 +00002887 goto exit;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002888 }
Hanno Beckera2747532019-02-06 16:19:04 +00002889 }
Hanno Becker6b8fbab2019-02-08 14:59:05 +00002890#else /* !MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
2891 /* Pass ownership to session structure. */
Hanno Becker3dad3112019-02-05 17:19:52 +00002892 ssl->session_negotiate->peer_cert = chain;
2893 chain = NULL;
Hanno Becker6b8fbab2019-02-08 14:59:05 +00002894#endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
Hanno Becker3dad3112019-02-05 17:19:52 +00002895
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002896 MBEDTLS_SSL_DEBUG_MSG(2, ("<= parse certificate"));
Paul Bakker5121ce52009-01-03 21:22:43 +00002897
Hanno Becker6bdfab22019-02-05 13:11:17 +00002898exit:
2899
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002900 if (ret == 0) {
Hanno Becker3dad3112019-02-05 17:19:52 +00002901 ssl->state++;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002902 }
Hanno Becker3dad3112019-02-05 17:19:52 +00002903
Gilles Peskineeccd8882020-03-10 12:19:08 +01002904#if defined(MBEDTLS_SSL_ECP_RESTARTABLE_ENABLED)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002905 if (ret == MBEDTLS_ERR_SSL_CRYPTO_IN_PROGRESS) {
Hanno Becker3dad3112019-02-05 17:19:52 +00002906 ssl->handshake->ecrs_peer_cert = chain;
2907 chain = NULL;
2908 }
2909#endif
2910
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002911 if (chain != NULL) {
2912 mbedtls_x509_crt_free(chain);
2913 mbedtls_free(chain);
Hanno Becker3dad3112019-02-05 17:19:52 +00002914 }
2915
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002916 return ret;
Paul Bakker5121ce52009-01-03 21:22:43 +00002917}
Gilles Peskineeccd8882020-03-10 12:19:08 +01002918#endif /* MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED */
Paul Bakker5121ce52009-01-03 21:22:43 +00002919
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002920void mbedtls_ssl_optimize_checksum(mbedtls_ssl_context *ssl,
2921 const mbedtls_ssl_ciphersuite_t *ciphersuite_info)
Paul Bakker380da532012-04-18 16:10:25 +00002922{
Paul Bakkerfb08fd22013-08-27 15:06:26 +02002923 ((void) ciphersuite_info);
Paul Bakker769075d2012-11-24 11:26:46 +01002924
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002925#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1) || \
2926 defined(MBEDTLS_SSL_PROTO_TLS1_1)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002927 if (ssl->minor_ver < MBEDTLS_SSL_MINOR_VERSION_3) {
Paul Bakker48916f92012-09-16 19:57:18 +00002928 ssl->handshake->update_checksum = ssl_update_checksum_md5sha1;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002929 } else
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002930#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002931#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
Gilles Peskined2d59372021-05-12 22:43:27 +02002932#if defined(MBEDTLS_SHA512_C) && !defined(MBEDTLS_SHA512_NO_SHA384)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002933 if (ciphersuite_info->mac == MBEDTLS_MD_SHA384) {
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002934 ssl->handshake->update_checksum = ssl_update_checksum_sha384;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002935 } else
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002936#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002937#if defined(MBEDTLS_SHA256_C)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002938 if (ciphersuite_info->mac != MBEDTLS_MD_SHA384) {
Paul Bakker48916f92012-09-16 19:57:18 +00002939 ssl->handshake->update_checksum = ssl_update_checksum_sha256;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002940 } else
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002941#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002942#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Manuel Pégourié-Gonnard61edffe2014-04-11 17:07:31 +02002943 {
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002944 MBEDTLS_SSL_DEBUG_MSG(1, ("should never happen"));
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002945 return;
Manuel Pégourié-Gonnard61edffe2014-04-11 17:07:31 +02002946 }
Paul Bakker380da532012-04-18 16:10:25 +00002947}
Paul Bakkerf7abd422013-04-16 13:15:56 +02002948
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002949void mbedtls_ssl_reset_checksum(mbedtls_ssl_context *ssl)
Manuel Pégourié-Gonnard67427c02014-07-11 13:45:34 +02002950{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002951#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1) || \
2952 defined(MBEDTLS_SSL_PROTO_TLS1_1)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002953 mbedtls_md5_starts_ret(&ssl->handshake->fin_md5);
2954 mbedtls_sha1_starts_ret(&ssl->handshake->fin_sha1);
Manuel Pégourié-Gonnard67427c02014-07-11 13:45:34 +02002955#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002956#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
2957#if defined(MBEDTLS_SHA256_C)
Andrzej Kurekeb342242019-01-29 09:14:33 -05002958#if defined(MBEDTLS_USE_PSA_CRYPTO)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002959 psa_hash_abort(&ssl->handshake->fin_sha256_psa);
2960 psa_hash_setup(&ssl->handshake->fin_sha256_psa, PSA_ALG_SHA_256);
Andrzej Kurekeb342242019-01-29 09:14:33 -05002961#else
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002962 mbedtls_sha256_starts_ret(&ssl->handshake->fin_sha256, 0);
Manuel Pégourié-Gonnard67427c02014-07-11 13:45:34 +02002963#endif
Andrzej Kurekeb342242019-01-29 09:14:33 -05002964#endif
Gilles Peskined2d59372021-05-12 22:43:27 +02002965#if defined(MBEDTLS_SHA512_C) && !defined(MBEDTLS_SHA512_NO_SHA384)
Andrzej Kurekeb342242019-01-29 09:14:33 -05002966#if defined(MBEDTLS_USE_PSA_CRYPTO)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002967 psa_hash_abort(&ssl->handshake->fin_sha384_psa);
2968 psa_hash_setup(&ssl->handshake->fin_sha384_psa, PSA_ALG_SHA_384);
Andrzej Kurekeb342242019-01-29 09:14:33 -05002969#else
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002970 mbedtls_sha512_starts_ret(&ssl->handshake->fin_sha512, 1);
Manuel Pégourié-Gonnard67427c02014-07-11 13:45:34 +02002971#endif
Andrzej Kurekeb342242019-01-29 09:14:33 -05002972#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002973#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Manuel Pégourié-Gonnard67427c02014-07-11 13:45:34 +02002974}
2975
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002976static void ssl_update_checksum_start(mbedtls_ssl_context *ssl,
2977 const unsigned char *buf, size_t len)
Paul Bakker380da532012-04-18 16:10:25 +00002978{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002979#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1) || \
2980 defined(MBEDTLS_SSL_PROTO_TLS1_1)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002981 mbedtls_md5_update_ret(&ssl->handshake->fin_md5, buf, len);
2982 mbedtls_sha1_update_ret(&ssl->handshake->fin_sha1, buf, len);
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002983#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002984#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
2985#if defined(MBEDTLS_SHA256_C)
Andrzej Kurekeb342242019-01-29 09:14:33 -05002986#if defined(MBEDTLS_USE_PSA_CRYPTO)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002987 psa_hash_update(&ssl->handshake->fin_sha256_psa, buf, len);
Andrzej Kurekeb342242019-01-29 09:14:33 -05002988#else
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002989 mbedtls_sha256_update_ret(&ssl->handshake->fin_sha256, buf, len);
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002990#endif
Andrzej Kurekeb342242019-01-29 09:14:33 -05002991#endif
Gilles Peskined2d59372021-05-12 22:43:27 +02002992#if defined(MBEDTLS_SHA512_C) && !defined(MBEDTLS_SHA512_NO_SHA384)
Andrzej Kurekeb342242019-01-29 09:14:33 -05002993#if defined(MBEDTLS_USE_PSA_CRYPTO)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002994 psa_hash_update(&ssl->handshake->fin_sha384_psa, buf, len);
Andrzej Kurekeb342242019-01-29 09:14:33 -05002995#else
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002996 mbedtls_sha512_update_ret(&ssl->handshake->fin_sha512, buf, len);
Paul Bakker769075d2012-11-24 11:26:46 +01002997#endif
Andrzej Kurekeb342242019-01-29 09:14:33 -05002998#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002999#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakker380da532012-04-18 16:10:25 +00003000}
3001
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003002#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1) || \
3003 defined(MBEDTLS_SSL_PROTO_TLS1_1)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003004static void ssl_update_checksum_md5sha1(mbedtls_ssl_context *ssl,
3005 const unsigned char *buf, size_t len)
Paul Bakker380da532012-04-18 16:10:25 +00003006{
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003007 mbedtls_md5_update_ret(&ssl->handshake->fin_md5, buf, len);
3008 mbedtls_sha1_update_ret(&ssl->handshake->fin_sha1, buf, len);
Paul Bakker380da532012-04-18 16:10:25 +00003009}
Paul Bakkerd2f068e2013-08-27 21:19:20 +02003010#endif
Paul Bakker380da532012-04-18 16:10:25 +00003011
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003012#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
3013#if defined(MBEDTLS_SHA256_C)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003014static void ssl_update_checksum_sha256(mbedtls_ssl_context *ssl,
3015 const unsigned char *buf, size_t len)
Paul Bakker380da532012-04-18 16:10:25 +00003016{
Andrzej Kurekeb342242019-01-29 09:14:33 -05003017#if defined(MBEDTLS_USE_PSA_CRYPTO)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003018 psa_hash_update(&ssl->handshake->fin_sha256_psa, buf, len);
Andrzej Kurekeb342242019-01-29 09:14:33 -05003019#else
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003020 mbedtls_sha256_update_ret(&ssl->handshake->fin_sha256, buf, len);
Andrzej Kurekeb342242019-01-29 09:14:33 -05003021#endif
Paul Bakker380da532012-04-18 16:10:25 +00003022}
Paul Bakkerd2f068e2013-08-27 21:19:20 +02003023#endif
Paul Bakker380da532012-04-18 16:10:25 +00003024
Gilles Peskined2d59372021-05-12 22:43:27 +02003025#if defined(MBEDTLS_SHA512_C) && !defined(MBEDTLS_SHA512_NO_SHA384)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003026static void ssl_update_checksum_sha384(mbedtls_ssl_context *ssl,
3027 const unsigned char *buf, size_t len)
Paul Bakker380da532012-04-18 16:10:25 +00003028{
Andrzej Kurekeb342242019-01-29 09:14:33 -05003029#if defined(MBEDTLS_USE_PSA_CRYPTO)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003030 psa_hash_update(&ssl->handshake->fin_sha384_psa, buf, len);
Andrzej Kurekeb342242019-01-29 09:14:33 -05003031#else
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003032 mbedtls_sha512_update_ret(&ssl->handshake->fin_sha512, buf, len);
Andrzej Kurekeb342242019-01-29 09:14:33 -05003033#endif
Paul Bakker380da532012-04-18 16:10:25 +00003034}
Paul Bakker769075d2012-11-24 11:26:46 +01003035#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003036#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakker380da532012-04-18 16:10:25 +00003037
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003038#if defined(MBEDTLS_SSL_PROTO_SSL3)
Paul Bakker1ef83d62012-04-11 12:09:53 +00003039static void ssl_calc_finished_ssl(
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003040 mbedtls_ssl_context *ssl, unsigned char *buf, int from)
Paul Bakker5121ce52009-01-03 21:22:43 +00003041{
Paul Bakker3c2122f2013-06-24 19:03:14 +02003042 const char *sender;
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02003043 mbedtls_md5_context md5;
3044 mbedtls_sha1_context sha1;
Paul Bakker1ef83d62012-04-11 12:09:53 +00003045
Paul Bakker5121ce52009-01-03 21:22:43 +00003046 unsigned char padbuf[48];
3047 unsigned char md5sum[16];
3048 unsigned char sha1sum[20];
3049
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003050 mbedtls_ssl_session *session = ssl->session_negotiate;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003051 if (!session) {
Paul Bakker48916f92012-09-16 19:57:18 +00003052 session = ssl->session;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003053 }
Paul Bakker48916f92012-09-16 19:57:18 +00003054
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003055 MBEDTLS_SSL_DEBUG_MSG(2, ("=> calc finished ssl"));
Paul Bakker1ef83d62012-04-11 12:09:53 +00003056
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003057 mbedtls_md5_init(&md5);
3058 mbedtls_sha1_init(&sha1);
Manuel Pégourié-Gonnard001f2b62015-07-06 16:21:13 +02003059
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003060 mbedtls_md5_clone(&md5, &ssl->handshake->fin_md5);
3061 mbedtls_sha1_clone(&sha1, &ssl->handshake->fin_sha1);
Paul Bakker5121ce52009-01-03 21:22:43 +00003062
3063 /*
3064 * SSLv3:
3065 * hash =
3066 * MD5( master + pad2 +
3067 * MD5( handshake + sender + master + pad1 ) )
3068 * + SHA1( master + pad2 +
3069 * SHA1( handshake + sender + master + pad1 ) )
Paul Bakker5121ce52009-01-03 21:22:43 +00003070 */
3071
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003072#if !defined(MBEDTLS_MD5_ALT)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003073 MBEDTLS_SSL_DEBUG_BUF(4, "finished md5 state", (unsigned char *)
3074 md5.state, sizeof(md5.state));
Paul Bakker90995b52013-06-24 19:20:35 +02003075#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00003076
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003077#if !defined(MBEDTLS_SHA1_ALT)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003078 MBEDTLS_SSL_DEBUG_BUF(4, "finished sha1 state", (unsigned char *)
3079 sha1.state, sizeof(sha1.state));
Paul Bakker90995b52013-06-24 19:20:35 +02003080#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00003081
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003082 sender = (from == MBEDTLS_SSL_IS_CLIENT) ? "CLNT"
Paul Bakker3c2122f2013-06-24 19:03:14 +02003083 : "SRVR";
Paul Bakker5121ce52009-01-03 21:22:43 +00003084
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003085 memset(padbuf, 0x36, 48);
Paul Bakker5121ce52009-01-03 21:22:43 +00003086
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003087 mbedtls_md5_update_ret(&md5, (const unsigned char *) sender, 4);
3088 mbedtls_md5_update_ret(&md5, session->master, 48);
3089 mbedtls_md5_update_ret(&md5, padbuf, 48);
3090 mbedtls_md5_finish_ret(&md5, md5sum);
Paul Bakker5121ce52009-01-03 21:22:43 +00003091
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003092 mbedtls_sha1_update_ret(&sha1, (const unsigned char *) sender, 4);
3093 mbedtls_sha1_update_ret(&sha1, session->master, 48);
3094 mbedtls_sha1_update_ret(&sha1, padbuf, 40);
3095 mbedtls_sha1_finish_ret(&sha1, sha1sum);
Paul Bakker5121ce52009-01-03 21:22:43 +00003096
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003097 memset(padbuf, 0x5C, 48);
Paul Bakker5121ce52009-01-03 21:22:43 +00003098
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003099 mbedtls_md5_starts_ret(&md5);
3100 mbedtls_md5_update_ret(&md5, session->master, 48);
3101 mbedtls_md5_update_ret(&md5, padbuf, 48);
3102 mbedtls_md5_update_ret(&md5, md5sum, 16);
3103 mbedtls_md5_finish_ret(&md5, buf);
Paul Bakker5121ce52009-01-03 21:22:43 +00003104
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003105 mbedtls_sha1_starts_ret(&sha1);
3106 mbedtls_sha1_update_ret(&sha1, session->master, 48);
3107 mbedtls_sha1_update_ret(&sha1, padbuf, 40);
3108 mbedtls_sha1_update_ret(&sha1, sha1sum, 20);
3109 mbedtls_sha1_finish_ret(&sha1, buf + 16);
Paul Bakker5121ce52009-01-03 21:22:43 +00003110
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003111 MBEDTLS_SSL_DEBUG_BUF(3, "calc finished result", buf, 36);
Paul Bakker5121ce52009-01-03 21:22:43 +00003112
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003113 mbedtls_md5_free(&md5);
3114 mbedtls_sha1_free(&sha1);
Paul Bakker5121ce52009-01-03 21:22:43 +00003115
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003116 mbedtls_platform_zeroize(padbuf, sizeof(padbuf));
3117 mbedtls_platform_zeroize(md5sum, sizeof(md5sum));
3118 mbedtls_platform_zeroize(sha1sum, sizeof(sha1sum));
Paul Bakker5121ce52009-01-03 21:22:43 +00003119
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003120 MBEDTLS_SSL_DEBUG_MSG(2, ("<= calc finished"));
Paul Bakker5121ce52009-01-03 21:22:43 +00003121}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003122#endif /* MBEDTLS_SSL_PROTO_SSL3 */
Paul Bakker5121ce52009-01-03 21:22:43 +00003123
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003124#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1)
Paul Bakker1ef83d62012-04-11 12:09:53 +00003125static void ssl_calc_finished_tls(
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003126 mbedtls_ssl_context *ssl, unsigned char *buf, int from)
Paul Bakker5121ce52009-01-03 21:22:43 +00003127{
Paul Bakker1ef83d62012-04-11 12:09:53 +00003128 int len = 12;
Paul Bakker3c2122f2013-06-24 19:03:14 +02003129 const char *sender;
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02003130 mbedtls_md5_context md5;
3131 mbedtls_sha1_context sha1;
Paul Bakker1ef83d62012-04-11 12:09:53 +00003132 unsigned char padbuf[36];
Paul Bakker5121ce52009-01-03 21:22:43 +00003133
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003134 mbedtls_ssl_session *session = ssl->session_negotiate;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003135 if (!session) {
Paul Bakker48916f92012-09-16 19:57:18 +00003136 session = ssl->session;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003137 }
Paul Bakker48916f92012-09-16 19:57:18 +00003138
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003139 MBEDTLS_SSL_DEBUG_MSG(2, ("=> calc finished tls"));
Paul Bakker5121ce52009-01-03 21:22:43 +00003140
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003141 mbedtls_md5_init(&md5);
3142 mbedtls_sha1_init(&sha1);
Manuel Pégourié-Gonnard001f2b62015-07-06 16:21:13 +02003143
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003144 mbedtls_md5_clone(&md5, &ssl->handshake->fin_md5);
3145 mbedtls_sha1_clone(&sha1, &ssl->handshake->fin_sha1);
Paul Bakker5121ce52009-01-03 21:22:43 +00003146
Paul Bakker1ef83d62012-04-11 12:09:53 +00003147 /*
3148 * TLSv1:
3149 * hash = PRF( master, finished_label,
3150 * MD5( handshake ) + SHA1( handshake ) )[0..11]
3151 */
Paul Bakker5121ce52009-01-03 21:22:43 +00003152
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003153#if !defined(MBEDTLS_MD5_ALT)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003154 MBEDTLS_SSL_DEBUG_BUF(4, "finished md5 state", (unsigned char *)
3155 md5.state, sizeof(md5.state));
Paul Bakker90995b52013-06-24 19:20:35 +02003156#endif
Paul Bakker1ef83d62012-04-11 12:09:53 +00003157
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003158#if !defined(MBEDTLS_SHA1_ALT)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003159 MBEDTLS_SSL_DEBUG_BUF(4, "finished sha1 state", (unsigned char *)
3160 sha1.state, sizeof(sha1.state));
Paul Bakker90995b52013-06-24 19:20:35 +02003161#endif
Paul Bakker1ef83d62012-04-11 12:09:53 +00003162
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003163 sender = (from == MBEDTLS_SSL_IS_CLIENT)
Paul Bakker3c2122f2013-06-24 19:03:14 +02003164 ? "client finished"
3165 : "server finished";
Paul Bakker1ef83d62012-04-11 12:09:53 +00003166
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003167 mbedtls_md5_finish_ret(&md5, padbuf);
3168 mbedtls_sha1_finish_ret(&sha1, padbuf + 16);
Paul Bakker1ef83d62012-04-11 12:09:53 +00003169
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003170 ssl->handshake->tls_prf(session->master, 48, sender,
3171 padbuf, 36, buf, len);
Paul Bakker1ef83d62012-04-11 12:09:53 +00003172
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003173 MBEDTLS_SSL_DEBUG_BUF(3, "calc finished result", buf, len);
Paul Bakker1ef83d62012-04-11 12:09:53 +00003174
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003175 mbedtls_md5_free(&md5);
3176 mbedtls_sha1_free(&sha1);
Paul Bakker1ef83d62012-04-11 12:09:53 +00003177
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003178 mbedtls_platform_zeroize(padbuf, sizeof(padbuf));
Paul Bakker1ef83d62012-04-11 12:09:53 +00003179
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003180 MBEDTLS_SSL_DEBUG_MSG(2, ("<= calc finished"));
Paul Bakker1ef83d62012-04-11 12:09:53 +00003181}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003182#endif /* MBEDTLS_SSL_PROTO_TLS1 || MBEDTLS_SSL_PROTO_TLS1_1 */
Paul Bakker1ef83d62012-04-11 12:09:53 +00003183
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003184#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
3185#if defined(MBEDTLS_SHA256_C)
Paul Bakkerca4ab492012-04-18 14:23:57 +00003186static void ssl_calc_finished_tls_sha256(
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003187 mbedtls_ssl_context *ssl, unsigned char *buf, int from)
Paul Bakker1ef83d62012-04-11 12:09:53 +00003188{
3189 int len = 12;
Paul Bakker3c2122f2013-06-24 19:03:14 +02003190 const char *sender;
Paul Bakker1ef83d62012-04-11 12:09:53 +00003191 unsigned char padbuf[32];
Andrzej Kurekeb342242019-01-29 09:14:33 -05003192#if defined(MBEDTLS_USE_PSA_CRYPTO)
3193 size_t hash_size;
Jaeden Amero34973232019-02-20 10:32:28 +00003194 psa_hash_operation_t sha256_psa = PSA_HASH_OPERATION_INIT;
Andrzej Kurekeb342242019-01-29 09:14:33 -05003195 psa_status_t status;
3196#else
3197 mbedtls_sha256_context sha256;
3198#endif
Paul Bakker1ef83d62012-04-11 12:09:53 +00003199
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003200 mbedtls_ssl_session *session = ssl->session_negotiate;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003201 if (!session) {
Paul Bakker48916f92012-09-16 19:57:18 +00003202 session = ssl->session;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003203 }
Paul Bakker48916f92012-09-16 19:57:18 +00003204
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003205 sender = (from == MBEDTLS_SSL_IS_CLIENT)
Andrzej Kurekeb342242019-01-29 09:14:33 -05003206 ? "client finished"
3207 : "server finished";
3208
3209#if defined(MBEDTLS_USE_PSA_CRYPTO)
3210 sha256_psa = psa_hash_operation_init();
3211
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003212 MBEDTLS_SSL_DEBUG_MSG(2, ("=> calc PSA finished tls sha256"));
Andrzej Kurekeb342242019-01-29 09:14:33 -05003213
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003214 status = psa_hash_clone(&ssl->handshake->fin_sha256_psa, &sha256_psa);
3215 if (status != PSA_SUCCESS) {
3216 MBEDTLS_SSL_DEBUG_MSG(2, ("PSA hash clone failed"));
Andrzej Kurekeb342242019-01-29 09:14:33 -05003217 return;
3218 }
3219
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003220 status = psa_hash_finish(&sha256_psa, padbuf, sizeof(padbuf), &hash_size);
3221 if (status != PSA_SUCCESS) {
3222 MBEDTLS_SSL_DEBUG_MSG(2, ("PSA hash finish failed"));
Andrzej Kurekeb342242019-01-29 09:14:33 -05003223 return;
3224 }
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003225 MBEDTLS_SSL_DEBUG_BUF(3, "PSA calculated padbuf", padbuf, 32);
Andrzej Kurekeb342242019-01-29 09:14:33 -05003226#else
3227
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003228 mbedtls_sha256_init(&sha256);
Manuel Pégourié-Gonnard001f2b62015-07-06 16:21:13 +02003229
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003230 MBEDTLS_SSL_DEBUG_MSG(2, ("=> calc finished tls sha256"));
Paul Bakker1ef83d62012-04-11 12:09:53 +00003231
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003232 mbedtls_sha256_clone(&sha256, &ssl->handshake->fin_sha256);
Paul Bakker1ef83d62012-04-11 12:09:53 +00003233
3234 /*
3235 * TLSv1.2:
3236 * hash = PRF( master, finished_label,
3237 * Hash( handshake ) )[0.11]
3238 */
3239
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003240#if !defined(MBEDTLS_SHA256_ALT)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003241 MBEDTLS_SSL_DEBUG_BUF(4, "finished sha2 state", (unsigned char *)
3242 sha256.state, sizeof(sha256.state));
Paul Bakker90995b52013-06-24 19:20:35 +02003243#endif
Paul Bakker1ef83d62012-04-11 12:09:53 +00003244
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003245 mbedtls_sha256_finish_ret(&sha256, padbuf);
3246 mbedtls_sha256_free(&sha256);
Andrzej Kurekeb342242019-01-29 09:14:33 -05003247#endif /* MBEDTLS_USE_PSA_CRYPTO */
Paul Bakker1ef83d62012-04-11 12:09:53 +00003248
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003249 ssl->handshake->tls_prf(session->master, 48, sender,
3250 padbuf, 32, buf, len);
Paul Bakker1ef83d62012-04-11 12:09:53 +00003251
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003252 MBEDTLS_SSL_DEBUG_BUF(3, "calc finished result", buf, len);
Paul Bakker1ef83d62012-04-11 12:09:53 +00003253
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003254 mbedtls_platform_zeroize(padbuf, sizeof(padbuf));
Paul Bakker1ef83d62012-04-11 12:09:53 +00003255
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003256 MBEDTLS_SSL_DEBUG_MSG(2, ("<= calc finished"));
Paul Bakker1ef83d62012-04-11 12:09:53 +00003257}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003258#endif /* MBEDTLS_SHA256_C */
Paul Bakker1ef83d62012-04-11 12:09:53 +00003259
Gilles Peskined2d59372021-05-12 22:43:27 +02003260#if defined(MBEDTLS_SHA512_C) && !defined(MBEDTLS_SHA512_NO_SHA384)
Rodrigo Dias Corread596ca82020-11-25 00:42:28 -03003261
Paul Bakkerca4ab492012-04-18 14:23:57 +00003262static void ssl_calc_finished_tls_sha384(
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003263 mbedtls_ssl_context *ssl, unsigned char *buf, int from)
Paul Bakkerca4ab492012-04-18 14:23:57 +00003264{
3265 int len = 12;
Paul Bakker3c2122f2013-06-24 19:03:14 +02003266 const char *sender;
Rodrigo Dias Corread596ca82020-11-25 00:42:28 -03003267 unsigned char padbuf[48];
Andrzej Kurekeb342242019-01-29 09:14:33 -05003268#if defined(MBEDTLS_USE_PSA_CRYPTO)
3269 size_t hash_size;
Jaeden Amero34973232019-02-20 10:32:28 +00003270 psa_hash_operation_t sha384_psa = PSA_HASH_OPERATION_INIT;
Andrzej Kurekeb342242019-01-29 09:14:33 -05003271 psa_status_t status;
3272#else
3273 mbedtls_sha512_context sha512;
3274#endif
Paul Bakkerca4ab492012-04-18 14:23:57 +00003275
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003276 mbedtls_ssl_session *session = ssl->session_negotiate;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003277 if (!session) {
Paul Bakker48916f92012-09-16 19:57:18 +00003278 session = ssl->session;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003279 }
Paul Bakker48916f92012-09-16 19:57:18 +00003280
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003281 sender = (from == MBEDTLS_SSL_IS_CLIENT)
Andrzej Kurekeb342242019-01-29 09:14:33 -05003282 ? "client finished"
3283 : "server finished";
3284
3285#if defined(MBEDTLS_USE_PSA_CRYPTO)
Andrzej Kurek972fba52019-01-30 03:29:12 -05003286 sha384_psa = psa_hash_operation_init();
Andrzej Kurekeb342242019-01-29 09:14:33 -05003287
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003288 MBEDTLS_SSL_DEBUG_MSG(2, ("=> calc PSA finished tls sha384"));
Andrzej Kurekeb342242019-01-29 09:14:33 -05003289
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003290 status = psa_hash_clone(&ssl->handshake->fin_sha384_psa, &sha384_psa);
3291 if (status != PSA_SUCCESS) {
3292 MBEDTLS_SSL_DEBUG_MSG(2, ("PSA hash clone failed"));
Andrzej Kurekeb342242019-01-29 09:14:33 -05003293 return;
3294 }
3295
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003296 status = psa_hash_finish(&sha384_psa, padbuf, sizeof(padbuf), &hash_size);
3297 if (status != PSA_SUCCESS) {
3298 MBEDTLS_SSL_DEBUG_MSG(2, ("PSA hash finish failed"));
Andrzej Kurekeb342242019-01-29 09:14:33 -05003299 return;
3300 }
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003301 MBEDTLS_SSL_DEBUG_BUF(3, "PSA calculated padbuf", padbuf, 48);
Andrzej Kurekeb342242019-01-29 09:14:33 -05003302#else
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003303 mbedtls_sha512_init(&sha512);
Manuel Pégourié-Gonnard001f2b62015-07-06 16:21:13 +02003304
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003305 MBEDTLS_SSL_DEBUG_MSG(2, ("=> calc finished tls sha384"));
Paul Bakkerca4ab492012-04-18 14:23:57 +00003306
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003307 mbedtls_sha512_clone(&sha512, &ssl->handshake->fin_sha512);
Paul Bakkerca4ab492012-04-18 14:23:57 +00003308
3309 /*
3310 * TLSv1.2:
3311 * hash = PRF( master, finished_label,
3312 * Hash( handshake ) )[0.11]
3313 */
3314
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003315#if !defined(MBEDTLS_SHA512_ALT)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003316 MBEDTLS_SSL_DEBUG_BUF(4, "finished sha512 state", (unsigned char *)
3317 sha512.state, sizeof(sha512.state));
Paul Bakker90995b52013-06-24 19:20:35 +02003318#endif
Gilles Peskinebb66dac2021-05-13 00:00:45 +02003319 /* mbedtls_sha512_finish_ret's output parameter is declared as a
Tom Cosgrove49f99bc2022-12-04 16:44:21 +00003320 * 64-byte buffer, but since we're using SHA-384, we know that the
Gilles Peskinebb66dac2021-05-13 00:00:45 +02003321 * output fits in 48 bytes. This is correct C, but GCC 11.1 warns
3322 * about it.
Rodrigo Dias Corread596ca82020-11-25 00:42:28 -03003323 */
Gilles Peskinebb66dac2021-05-13 00:00:45 +02003324#if defined(__GNUC__) && __GNUC__ >= 11
3325#pragma GCC diagnostic push
3326#pragma GCC diagnostic ignored "-Wstringop-overflow"
3327#endif
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003328 mbedtls_sha512_finish_ret(&sha512, padbuf);
Gilles Peskinebb66dac2021-05-13 00:00:45 +02003329#if defined(__GNUC__) && __GNUC__ >= 11
3330#pragma GCC diagnostic pop
3331#endif
Paul Bakkerca4ab492012-04-18 14:23:57 +00003332
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003333 mbedtls_sha512_free(&sha512);
Andrzej Kurekeb342242019-01-29 09:14:33 -05003334#endif
Paul Bakkerca4ab492012-04-18 14:23:57 +00003335
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003336 ssl->handshake->tls_prf(session->master, 48, sender,
3337 padbuf, 48, buf, len);
Paul Bakkerca4ab492012-04-18 14:23:57 +00003338
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003339 MBEDTLS_SSL_DEBUG_BUF(3, "calc finished result", buf, len);
Paul Bakkerca4ab492012-04-18 14:23:57 +00003340
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003341 mbedtls_platform_zeroize(padbuf, sizeof(padbuf));
Paul Bakkerca4ab492012-04-18 14:23:57 +00003342
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003343 MBEDTLS_SSL_DEBUG_MSG(2, ("<= calc finished"));
Paul Bakkerca4ab492012-04-18 14:23:57 +00003344}
Gilles Peskined2d59372021-05-12 22:43:27 +02003345#endif /* MBEDTLS_SHA512_C && !MBEDTLS_SHA512_NO_SHA384 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003346#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakkerca4ab492012-04-18 14:23:57 +00003347
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003348void mbedtls_ssl_handshake_wrapup_free_hs_transform(mbedtls_ssl_context *ssl)
Paul Bakker48916f92012-09-16 19:57:18 +00003349{
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003350 MBEDTLS_SSL_DEBUG_MSG(3, ("=> handshake wrapup: final free"));
Paul Bakker48916f92012-09-16 19:57:18 +00003351
3352 /*
3353 * Free our handshake params
3354 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003355 mbedtls_ssl_handshake_free(ssl);
3356 mbedtls_free(ssl->handshake);
Paul Bakker48916f92012-09-16 19:57:18 +00003357 ssl->handshake = NULL;
3358
3359 /*
Shaun Case0e7791f2021-12-20 21:14:10 -08003360 * Free the previous transform and switch in the current one
Paul Bakker48916f92012-09-16 19:57:18 +00003361 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003362 if (ssl->transform) {
3363 mbedtls_ssl_transform_free(ssl->transform);
3364 mbedtls_free(ssl->transform);
Paul Bakker48916f92012-09-16 19:57:18 +00003365 }
3366 ssl->transform = ssl->transform_negotiate;
3367 ssl->transform_negotiate = NULL;
3368
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003369 MBEDTLS_SSL_DEBUG_MSG(3, ("<= handshake wrapup: final free"));
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02003370}
3371
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003372void mbedtls_ssl_handshake_wrapup(mbedtls_ssl_context *ssl)
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02003373{
3374 int resume = ssl->handshake->resume;
3375
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003376 MBEDTLS_SSL_DEBUG_MSG(3, ("=> handshake wrapup"));
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02003377
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003378#if defined(MBEDTLS_SSL_RENEGOTIATION)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003379 if (ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS) {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003380 ssl->renego_status = MBEDTLS_SSL_RENEGOTIATION_DONE;
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02003381 ssl->renego_records_seen = 0;
3382 }
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01003383#endif
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02003384
3385 /*
3386 * Free the previous session and switch in the current one
3387 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003388 if (ssl->session) {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003389#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
Manuel Pégourié-Gonnard1a034732014-11-04 17:36:18 +01003390 /* RFC 7366 3.1: keep the EtM state */
3391 ssl->session_negotiate->encrypt_then_mac =
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003392 ssl->session->encrypt_then_mac;
Manuel Pégourié-Gonnard1a034732014-11-04 17:36:18 +01003393#endif
3394
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003395 mbedtls_ssl_session_free(ssl->session);
3396 mbedtls_free(ssl->session);
Paul Bakker0a597072012-09-25 21:55:46 +00003397 }
3398 ssl->session = ssl->session_negotiate;
Paul Bakker48916f92012-09-16 19:57:18 +00003399 ssl->session_negotiate = NULL;
3400
Paul Bakker0a597072012-09-25 21:55:46 +00003401 /*
3402 * Add cache entry
3403 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003404 if (ssl->conf->f_set_cache != NULL &&
Manuel Pégourié-Gonnard12ad7982015-06-18 15:50:37 +02003405 ssl->session->id_len != 0 &&
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003406 resume == 0) {
3407 if (ssl->conf->f_set_cache(ssl->conf->p_cache, ssl->session) != 0) {
3408 MBEDTLS_SSL_DEBUG_MSG(1, ("cache did not store session"));
3409 }
Manuel Pégourié-Gonnardc086cce2013-08-02 14:13:02 +02003410 }
Paul Bakker0a597072012-09-25 21:55:46 +00003411
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003412#if defined(MBEDTLS_SSL_PROTO_DTLS)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003413 if (ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
3414 ssl->handshake->flight != NULL) {
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02003415 /* Cancel handshake timer */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003416 mbedtls_ssl_set_timer(ssl, 0);
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02003417
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02003418 /* Keep last flight around in case we need to resend it:
3419 * we need the handshake and transform structures for that */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003420 MBEDTLS_SSL_DEBUG_MSG(3, ("skip freeing handshake and transform"));
3421 } else
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02003422#endif
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003423 mbedtls_ssl_handshake_wrapup_free_hs_transform(ssl);
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02003424
Paul Bakker48916f92012-09-16 19:57:18 +00003425 ssl->state++;
3426
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003427 MBEDTLS_SSL_DEBUG_MSG(3, ("<= handshake wrapup"));
Paul Bakker48916f92012-09-16 19:57:18 +00003428}
3429
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003430int mbedtls_ssl_write_finished(mbedtls_ssl_context *ssl)
Paul Bakker1ef83d62012-04-11 12:09:53 +00003431{
Manuel Pégourié-Gonnard879a4f92014-07-11 22:31:12 +02003432 int ret, hash_len;
Paul Bakker1ef83d62012-04-11 12:09:53 +00003433
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003434 MBEDTLS_SSL_DEBUG_MSG(2, ("=> write finished"));
Paul Bakker1ef83d62012-04-11 12:09:53 +00003435
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003436 mbedtls_ssl_update_out_pointers(ssl, ssl->transform_negotiate);
Paul Bakker92be97b2013-01-02 17:30:03 +01003437
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003438 ssl->handshake->calc_finished(ssl, ssl->out_msg + 4, ssl->conf->endpoint);
Paul Bakker1ef83d62012-04-11 12:09:53 +00003439
Manuel Pégourié-Gonnard214a8482016-02-22 11:27:26 +01003440 /*
3441 * RFC 5246 7.4.9 (Page 63) says 12 is the default length and ciphersuites
3442 * may define some other value. Currently (early 2016), no defined
3443 * ciphersuite does this (and this is unlikely to change as activity has
3444 * moved to TLS 1.3 now) so we can keep the hardcoded 12 here.
3445 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003446 hash_len = (ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0) ? 36 : 12;
Paul Bakker5121ce52009-01-03 21:22:43 +00003447
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003448#if defined(MBEDTLS_SSL_RENEGOTIATION)
Paul Bakker48916f92012-09-16 19:57:18 +00003449 ssl->verify_data_len = hash_len;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003450 memcpy(ssl->own_verify_data, ssl->out_msg + 4, hash_len);
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01003451#endif
Paul Bakker48916f92012-09-16 19:57:18 +00003452
Paul Bakker5121ce52009-01-03 21:22:43 +00003453 ssl->out_msglen = 4 + hash_len;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003454 ssl->out_msgtype = MBEDTLS_SSL_MSG_HANDSHAKE;
3455 ssl->out_msg[0] = MBEDTLS_SSL_HS_FINISHED;
Paul Bakker5121ce52009-01-03 21:22:43 +00003456
3457 /*
3458 * In case of session resuming, invert the client and server
3459 * ChangeCipherSpec messages order.
3460 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003461 if (ssl->handshake->resume != 0) {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003462#if defined(MBEDTLS_SSL_CLI_C)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003463 if (ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT) {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003464 ssl->state = MBEDTLS_SSL_HANDSHAKE_WRAPUP;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003465 }
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01003466#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003467#if defined(MBEDTLS_SSL_SRV_C)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003468 if (ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER) {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003469 ssl->state = MBEDTLS_SSL_CLIENT_CHANGE_CIPHER_SPEC;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003470 }
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01003471#endif
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003472 } else {
Paul Bakker5121ce52009-01-03 21:22:43 +00003473 ssl->state++;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003474 }
Paul Bakker5121ce52009-01-03 21:22:43 +00003475
Paul Bakker48916f92012-09-16 19:57:18 +00003476 /*
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02003477 * Switch to our negotiated transform and session parameters for outbound
3478 * data.
Paul Bakker48916f92012-09-16 19:57:18 +00003479 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003480 MBEDTLS_SSL_DEBUG_MSG(3, ("switching to new transform spec for outbound data"));
Manuel Pégourié-Gonnard5afb1672014-02-16 18:33:22 +01003481
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003482#if defined(MBEDTLS_SSL_PROTO_DTLS)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003483 if (ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM) {
Manuel Pégourié-Gonnard879a4f92014-07-11 22:31:12 +02003484 unsigned char i;
3485
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003486 /* Remember current epoch settings for resending */
3487 ssl->handshake->alt_transform_out = ssl->transform_out;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003488 memcpy(ssl->handshake->alt_out_ctr, ssl->cur_out_ctr, 8);
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003489
Manuel Pégourié-Gonnard879a4f92014-07-11 22:31:12 +02003490 /* Set sequence_number to zero */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003491 memset(ssl->cur_out_ctr + 2, 0, 6);
Manuel Pégourié-Gonnard879a4f92014-07-11 22:31:12 +02003492
3493 /* Increment epoch */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003494 for (i = 2; i > 0; i--) {
3495 if (++ssl->cur_out_ctr[i - 1] != 0) {
Manuel Pégourié-Gonnard879a4f92014-07-11 22:31:12 +02003496 break;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003497 }
3498 }
Manuel Pégourié-Gonnard879a4f92014-07-11 22:31:12 +02003499
3500 /* The loop goes to its end iff the counter is wrapping */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003501 if (i == 0) {
3502 MBEDTLS_SSL_DEBUG_MSG(1, ("DTLS epoch would wrap"));
3503 return MBEDTLS_ERR_SSL_COUNTER_WRAPPING;
Manuel Pégourié-Gonnard879a4f92014-07-11 22:31:12 +02003504 }
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003505 } else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003506#endif /* MBEDTLS_SSL_PROTO_DTLS */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003507 memset(ssl->cur_out_ctr, 0, 8);
Paul Bakker5121ce52009-01-03 21:22:43 +00003508
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003509 ssl->transform_out = ssl->transform_negotiate;
3510 ssl->session_out = ssl->session_negotiate;
3511
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003512#if defined(MBEDTLS_SSL_HW_RECORD_ACCEL)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003513 if (mbedtls_ssl_hw_record_activate != NULL) {
3514 if ((ret = mbedtls_ssl_hw_record_activate(ssl, MBEDTLS_SSL_CHANNEL_OUTBOUND)) != 0) {
3515 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ssl_hw_record_activate", ret);
3516 return MBEDTLS_ERR_SSL_HW_ACCEL_FAILED;
Paul Bakker07eb38b2012-12-19 14:42:06 +01003517 }
3518 }
3519#endif
3520
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003521#if defined(MBEDTLS_SSL_PROTO_DTLS)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003522 if (ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM) {
3523 mbedtls_ssl_send_flight_completed(ssl);
3524 }
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02003525#endif
3526
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003527 if ((ret = mbedtls_ssl_write_handshake_msg(ssl)) != 0) {
3528 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ssl_write_handshake_msg", ret);
3529 return ret;
Paul Bakker5121ce52009-01-03 21:22:43 +00003530 }
3531
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02003532#if defined(MBEDTLS_SSL_PROTO_DTLS)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003533 if (ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
3534 (ret = mbedtls_ssl_flight_transmit(ssl)) != 0) {
3535 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ssl_flight_transmit", ret);
3536 return ret;
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02003537 }
3538#endif
3539
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003540 MBEDTLS_SSL_DEBUG_MSG(2, ("<= write finished"));
Paul Bakker5121ce52009-01-03 21:22:43 +00003541
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003542 return 0;
Paul Bakker5121ce52009-01-03 21:22:43 +00003543}
3544
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003545#if defined(MBEDTLS_SSL_PROTO_SSL3)
Manuel Pégourié-Gonnardca6440b2014-09-10 12:39:54 +00003546#define SSL_MAX_HASH_LEN 36
3547#else
3548#define SSL_MAX_HASH_LEN 12
3549#endif
3550
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003551int mbedtls_ssl_parse_finished(mbedtls_ssl_context *ssl)
Paul Bakker5121ce52009-01-03 21:22:43 +00003552{
Janos Follath865b3eb2019-12-16 11:46:15 +00003553 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnard879a4f92014-07-11 22:31:12 +02003554 unsigned int hash_len;
Manuel Pégourié-Gonnardca6440b2014-09-10 12:39:54 +00003555 unsigned char buf[SSL_MAX_HASH_LEN];
Paul Bakker5121ce52009-01-03 21:22:43 +00003556
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003557 MBEDTLS_SSL_DEBUG_MSG(2, ("=> parse finished"));
Paul Bakker5121ce52009-01-03 21:22:43 +00003558
Gilles Peskine622d8042021-12-13 14:38:40 +01003559 /* There is currently no ciphersuite using another length with TLS 1.2 */
3560#if defined(MBEDTLS_SSL_PROTO_SSL3)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003561 if (ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0) {
Gilles Peskine622d8042021-12-13 14:38:40 +01003562 hash_len = 36;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003563 } else
Gilles Peskine622d8042021-12-13 14:38:40 +01003564#endif
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003565 hash_len = 12;
Gilles Peskine622d8042021-12-13 14:38:40 +01003566
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003567 ssl->handshake->calc_finished(ssl, buf, ssl->conf->endpoint ^ 1);
Paul Bakker5121ce52009-01-03 21:22:43 +00003568
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003569 if ((ret = mbedtls_ssl_read_record(ssl, 1)) != 0) {
3570 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ssl_read_record", ret);
Gilles Peskinef91b2e52021-12-13 12:36:15 +01003571 goto exit;
Paul Bakker5121ce52009-01-03 21:22:43 +00003572 }
3573
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003574 if (ssl->in_msgtype != MBEDTLS_SSL_MSG_HANDSHAKE) {
3575 MBEDTLS_SSL_DEBUG_MSG(1, ("bad finished message"));
3576 mbedtls_ssl_send_alert_message(ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
3577 MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE);
Gilles Peskinef91b2e52021-12-13 12:36:15 +01003578 ret = MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE;
3579 goto exit;
Paul Bakker5121ce52009-01-03 21:22:43 +00003580 }
3581
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003582 if (ssl->in_msg[0] != MBEDTLS_SSL_HS_FINISHED ||
3583 ssl->in_hslen != mbedtls_ssl_hs_hdr_len(ssl) + hash_len) {
3584 MBEDTLS_SSL_DEBUG_MSG(1, ("bad finished message"));
3585 mbedtls_ssl_send_alert_message(ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
3586 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR);
Gilles Peskinef91b2e52021-12-13 12:36:15 +01003587 ret = MBEDTLS_ERR_SSL_BAD_HS_FINISHED;
3588 goto exit;
Paul Bakker5121ce52009-01-03 21:22:43 +00003589 }
3590
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003591 if (mbedtls_ct_memcmp(ssl->in_msg + mbedtls_ssl_hs_hdr_len(ssl),
3592 buf, hash_len) != 0) {
3593 MBEDTLS_SSL_DEBUG_MSG(1, ("bad finished message"));
3594 mbedtls_ssl_send_alert_message(ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
3595 MBEDTLS_SSL_ALERT_MSG_DECRYPT_ERROR);
Gilles Peskinef91b2e52021-12-13 12:36:15 +01003596 ret = MBEDTLS_ERR_SSL_BAD_HS_FINISHED;
3597 goto exit;
Paul Bakker5121ce52009-01-03 21:22:43 +00003598 }
3599
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003600#if defined(MBEDTLS_SSL_RENEGOTIATION)
Paul Bakker48916f92012-09-16 19:57:18 +00003601 ssl->verify_data_len = hash_len;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003602 memcpy(ssl->peer_verify_data, buf, hash_len);
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01003603#endif
Paul Bakker48916f92012-09-16 19:57:18 +00003604
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003605 if (ssl->handshake->resume != 0) {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003606#if defined(MBEDTLS_SSL_CLI_C)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003607 if (ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT) {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003608 ssl->state = MBEDTLS_SSL_CLIENT_CHANGE_CIPHER_SPEC;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003609 }
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01003610#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003611#if defined(MBEDTLS_SSL_SRV_C)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003612 if (ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER) {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003613 ssl->state = MBEDTLS_SSL_HANDSHAKE_WRAPUP;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003614 }
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01003615#endif
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003616 } else {
Paul Bakker5121ce52009-01-03 21:22:43 +00003617 ssl->state++;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003618 }
Paul Bakker5121ce52009-01-03 21:22:43 +00003619
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003620#if defined(MBEDTLS_SSL_PROTO_DTLS)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003621 if (ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM) {
3622 mbedtls_ssl_recv_flight_completed(ssl);
3623 }
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003624#endif
3625
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003626 MBEDTLS_SSL_DEBUG_MSG(2, ("<= parse finished"));
Paul Bakker5121ce52009-01-03 21:22:43 +00003627
Gilles Peskinef91b2e52021-12-13 12:36:15 +01003628exit:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003629 mbedtls_platform_zeroize(buf, hash_len);
3630 return ret;
Paul Bakker5121ce52009-01-03 21:22:43 +00003631}
3632
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003633static void ssl_handshake_params_init(mbedtls_ssl_handshake_params *handshake)
Paul Bakkeraccaffe2014-06-26 13:37:14 +02003634{
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003635 memset(handshake, 0, sizeof(mbedtls_ssl_handshake_params));
Paul Bakkeraccaffe2014-06-26 13:37:14 +02003636
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003637#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1) || \
3638 defined(MBEDTLS_SSL_PROTO_TLS1_1)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003639 mbedtls_md5_init(&handshake->fin_md5);
3640 mbedtls_sha1_init(&handshake->fin_sha1);
3641 mbedtls_md5_starts_ret(&handshake->fin_md5);
3642 mbedtls_sha1_starts_ret(&handshake->fin_sha1);
Paul Bakkeraccaffe2014-06-26 13:37:14 +02003643#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003644#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
3645#if defined(MBEDTLS_SHA256_C)
Andrzej Kurekeb342242019-01-29 09:14:33 -05003646#if defined(MBEDTLS_USE_PSA_CRYPTO)
3647 handshake->fin_sha256_psa = psa_hash_operation_init();
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003648 psa_hash_setup(&handshake->fin_sha256_psa, PSA_ALG_SHA_256);
Andrzej Kurekeb342242019-01-29 09:14:33 -05003649#else
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003650 mbedtls_sha256_init(&handshake->fin_sha256);
3651 mbedtls_sha256_starts_ret(&handshake->fin_sha256, 0);
Paul Bakkeraccaffe2014-06-26 13:37:14 +02003652#endif
Andrzej Kurekeb342242019-01-29 09:14:33 -05003653#endif
Gilles Peskined2d59372021-05-12 22:43:27 +02003654#if defined(MBEDTLS_SHA512_C) && !defined(MBEDTLS_SHA512_NO_SHA384)
Andrzej Kurekeb342242019-01-29 09:14:33 -05003655#if defined(MBEDTLS_USE_PSA_CRYPTO)
Andrzej Kurek972fba52019-01-30 03:29:12 -05003656 handshake->fin_sha384_psa = psa_hash_operation_init();
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003657 psa_hash_setup(&handshake->fin_sha384_psa, PSA_ALG_SHA_384);
Andrzej Kurekeb342242019-01-29 09:14:33 -05003658#else
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003659 mbedtls_sha512_init(&handshake->fin_sha512);
3660 mbedtls_sha512_starts_ret(&handshake->fin_sha512, 1);
Paul Bakkeraccaffe2014-06-26 13:37:14 +02003661#endif
Andrzej Kurekeb342242019-01-29 09:14:33 -05003662#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003663#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakkeraccaffe2014-06-26 13:37:14 +02003664
3665 handshake->update_checksum = ssl_update_checksum_start;
Hanno Becker7e5437a2017-04-28 17:15:26 +01003666
3667#if defined(MBEDTLS_SSL_PROTO_TLS1_2) && \
Gilles Peskineeccd8882020-03-10 12:19:08 +01003668 defined(MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003669 mbedtls_ssl_sig_hash_set_init(&handshake->hash_algs);
Hanno Becker7e5437a2017-04-28 17:15:26 +01003670#endif
Paul Bakkeraccaffe2014-06-26 13:37:14 +02003671
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003672#if defined(MBEDTLS_DHM_C)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003673 mbedtls_dhm_init(&handshake->dhm_ctx);
Paul Bakkeraccaffe2014-06-26 13:37:14 +02003674#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003675#if defined(MBEDTLS_ECDH_C)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003676 mbedtls_ecdh_init(&handshake->ecdh_ctx);
Paul Bakkeraccaffe2014-06-26 13:37:14 +02003677#endif
Manuel Pégourié-Gonnardeef142d2015-09-16 10:05:04 +02003678#if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003679 mbedtls_ecjpake_init(&handshake->ecjpake_ctx);
Manuel Pégourié-Gonnard77c06462015-09-17 13:59:49 +02003680#if defined(MBEDTLS_SSL_CLI_C)
3681 handshake->ecjpake_cache = NULL;
3682 handshake->ecjpake_cache_len = 0;
3683#endif
Manuel Pégourié-Gonnard76cfd3f2015-09-15 12:10:54 +02003684#endif
Manuel Pégourié-Gonnardcdc26ae2015-06-19 12:16:31 +02003685
Gilles Peskineeccd8882020-03-10 12:19:08 +01003686#if defined(MBEDTLS_SSL_ECP_RESTARTABLE_ENABLED)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003687 mbedtls_x509_crt_restart_init(&handshake->ecrs_ctx);
Manuel Pégourié-Gonnard862cde52017-05-17 11:56:15 +02003688#endif
3689
Manuel Pégourié-Gonnardcdc26ae2015-06-19 12:16:31 +02003690#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
3691 handshake->sni_authmode = MBEDTLS_SSL_VERIFY_UNSET;
3692#endif
Hanno Becker75173122019-02-06 16:18:31 +00003693
3694#if defined(MBEDTLS_X509_CRT_PARSE_C) && \
3695 !defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003696 mbedtls_pk_init(&handshake->peer_pubkey);
Hanno Becker75173122019-02-06 16:18:31 +00003697#endif
Paul Bakkeraccaffe2014-06-26 13:37:14 +02003698}
3699
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003700void mbedtls_ssl_transform_init(mbedtls_ssl_transform *transform)
Paul Bakkeraccaffe2014-06-26 13:37:14 +02003701{
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003702 memset(transform, 0, sizeof(mbedtls_ssl_transform));
Paul Bakker84bbeb52014-07-01 14:53:22 +02003703
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003704 mbedtls_cipher_init(&transform->cipher_ctx_enc);
3705 mbedtls_cipher_init(&transform->cipher_ctx_dec);
Paul Bakker84bbeb52014-07-01 14:53:22 +02003706
Hanno Beckerd56ed242018-01-03 15:32:51 +00003707#if defined(MBEDTLS_SSL_SOME_MODES_USE_MAC)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003708 mbedtls_md_init(&transform->md_ctx_enc);
3709 mbedtls_md_init(&transform->md_ctx_dec);
Hanno Beckerd56ed242018-01-03 15:32:51 +00003710#endif
Paul Bakkeraccaffe2014-06-26 13:37:14 +02003711}
3712
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003713void mbedtls_ssl_session_init(mbedtls_ssl_session *session)
Paul Bakkeraccaffe2014-06-26 13:37:14 +02003714{
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003715 memset(session, 0, sizeof(mbedtls_ssl_session));
Paul Bakkeraccaffe2014-06-26 13:37:14 +02003716}
3717
Manuel Pégourié-Gonnardd904d662022-06-17 10:24:00 +02003718MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003719static int ssl_handshake_init(mbedtls_ssl_context *ssl)
Paul Bakker48916f92012-09-16 19:57:18 +00003720{
Paul Bakkeraccaffe2014-06-26 13:37:14 +02003721 /* Clear old handshake information if present */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003722 if (ssl->transform_negotiate) {
3723 mbedtls_ssl_transform_free(ssl->transform_negotiate);
3724 }
3725 if (ssl->session_negotiate) {
3726 mbedtls_ssl_session_free(ssl->session_negotiate);
3727 }
3728 if (ssl->handshake) {
3729 mbedtls_ssl_handshake_free(ssl);
3730 }
Paul Bakkeraccaffe2014-06-26 13:37:14 +02003731
3732 /*
3733 * Either the pointers are now NULL or cleared properly and can be freed.
3734 * Now allocate missing structures.
3735 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003736 if (ssl->transform_negotiate == NULL) {
3737 ssl->transform_negotiate = mbedtls_calloc(1, sizeof(mbedtls_ssl_transform));
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02003738 }
Paul Bakker48916f92012-09-16 19:57:18 +00003739
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003740 if (ssl->session_negotiate == NULL) {
3741 ssl->session_negotiate = mbedtls_calloc(1, sizeof(mbedtls_ssl_session));
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02003742 }
Paul Bakker48916f92012-09-16 19:57:18 +00003743
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003744 if (ssl->handshake == NULL) {
3745 ssl->handshake = mbedtls_calloc(1, sizeof(mbedtls_ssl_handshake_params));
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02003746 }
Andrzej Kurek0afa2a12020-03-03 10:39:58 -05003747#if defined(MBEDTLS_SSL_VARIABLE_BUFFER_LENGTH)
3748 /* If the buffers are too small - reallocate */
Andrzej Kurek8ea68722020-04-03 06:40:47 -04003749
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003750 handle_buffer_resizing(ssl, 0, MBEDTLS_SSL_IN_BUFFER_LEN,
3751 MBEDTLS_SSL_OUT_BUFFER_LEN);
Andrzej Kurek0afa2a12020-03-03 10:39:58 -05003752#endif
Paul Bakker48916f92012-09-16 19:57:18 +00003753
Paul Bakkeraccaffe2014-06-26 13:37:14 +02003754 /* All pointers should exist and can be directly freed without issue */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003755 if (ssl->handshake == NULL ||
Paul Bakker48916f92012-09-16 19:57:18 +00003756 ssl->transform_negotiate == NULL ||
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003757 ssl->session_negotiate == NULL) {
3758 MBEDTLS_SSL_DEBUG_MSG(1, ("alloc() of ssl sub-contexts failed"));
Paul Bakkeraccaffe2014-06-26 13:37:14 +02003759
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003760 mbedtls_free(ssl->handshake);
3761 mbedtls_free(ssl->transform_negotiate);
3762 mbedtls_free(ssl->session_negotiate);
Paul Bakkeraccaffe2014-06-26 13:37:14 +02003763
3764 ssl->handshake = NULL;
3765 ssl->transform_negotiate = NULL;
3766 ssl->session_negotiate = NULL;
3767
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003768 return MBEDTLS_ERR_SSL_ALLOC_FAILED;
Paul Bakker48916f92012-09-16 19:57:18 +00003769 }
3770
Paul Bakkeraccaffe2014-06-26 13:37:14 +02003771 /* Initialize structures */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003772 mbedtls_ssl_session_init(ssl->session_negotiate);
3773 mbedtls_ssl_transform_init(ssl->transform_negotiate);
3774 ssl_handshake_params_init(ssl->handshake);
Paul Bakker968afaa2014-07-09 11:09:24 +02003775
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003776#if defined(MBEDTLS_SSL_PROTO_DTLS)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003777 if (ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM) {
Manuel Pégourié-Gonnard06939ce2015-05-11 11:25:46 +02003778 ssl->handshake->alt_transform_out = ssl->transform_out;
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003779
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003780 if (ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT) {
Manuel Pégourié-Gonnard06939ce2015-05-11 11:25:46 +02003781 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_PREPARING;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003782 } else {
Manuel Pégourié-Gonnard06939ce2015-05-11 11:25:46 +02003783 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_WAITING;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003784 }
Manuel Pégourié-Gonnard286a1362015-05-13 16:22:05 +02003785
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003786 mbedtls_ssl_set_timer(ssl, 0);
Manuel Pégourié-Gonnard06939ce2015-05-11 11:25:46 +02003787 }
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003788#endif
3789
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003790 return 0;
Paul Bakker48916f92012-09-16 19:57:18 +00003791}
3792
Manuel Pégourié-Gonnarde057d3b2015-05-20 10:59:43 +02003793#if defined(MBEDTLS_SSL_DTLS_HELLO_VERIFY) && defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard7d38d212014-07-23 17:52:09 +02003794/* Dummy cookie callbacks for defaults */
Manuel Pégourié-Gonnardd904d662022-06-17 10:24:00 +02003795MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003796static int ssl_cookie_write_dummy(void *ctx,
3797 unsigned char **p, unsigned char *end,
3798 const unsigned char *cli_id, size_t cli_id_len)
Manuel Pégourié-Gonnard7d38d212014-07-23 17:52:09 +02003799{
3800 ((void) ctx);
3801 ((void) p);
3802 ((void) end);
3803 ((void) cli_id);
3804 ((void) cli_id_len);
3805
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003806 return MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
Manuel Pégourié-Gonnard7d38d212014-07-23 17:52:09 +02003807}
3808
Manuel Pégourié-Gonnardd904d662022-06-17 10:24:00 +02003809MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003810static int ssl_cookie_check_dummy(void *ctx,
3811 const unsigned char *cookie, size_t cookie_len,
3812 const unsigned char *cli_id, size_t cli_id_len)
Manuel Pégourié-Gonnard7d38d212014-07-23 17:52:09 +02003813{
3814 ((void) ctx);
3815 ((void) cookie);
3816 ((void) cookie_len);
3817 ((void) cli_id);
3818 ((void) cli_id_len);
3819
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003820 return MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
Manuel Pégourié-Gonnard7d38d212014-07-23 17:52:09 +02003821}
Manuel Pégourié-Gonnarde057d3b2015-05-20 10:59:43 +02003822#endif /* MBEDTLS_SSL_DTLS_HELLO_VERIFY && MBEDTLS_SSL_SRV_C */
Manuel Pégourié-Gonnard7d38d212014-07-23 17:52:09 +02003823
Paul Bakker5121ce52009-01-03 21:22:43 +00003824/*
3825 * Initialize an SSL context
3826 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003827void mbedtls_ssl_init(mbedtls_ssl_context *ssl)
Manuel Pégourié-Gonnard41d479e2015-04-29 00:48:22 +02003828{
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003829 memset(ssl, 0, sizeof(mbedtls_ssl_context));
Manuel Pégourié-Gonnard41d479e2015-04-29 00:48:22 +02003830}
3831
3832/*
3833 * Setup an SSL context
3834 */
Hanno Becker2a43f6f2018-08-10 11:12:52 +01003835
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003836int mbedtls_ssl_setup(mbedtls_ssl_context *ssl,
3837 const mbedtls_ssl_config *conf)
Paul Bakker5121ce52009-01-03 21:22:43 +00003838{
Janos Follath865b3eb2019-12-16 11:46:15 +00003839 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Darryl Greenb33cc762019-11-28 14:29:44 +00003840 size_t in_buf_len = MBEDTLS_SSL_IN_BUFFER_LEN;
3841 size_t out_buf_len = MBEDTLS_SSL_OUT_BUFFER_LEN;
Paul Bakker5121ce52009-01-03 21:22:43 +00003842
Manuel Pégourié-Gonnarddef0bbe2015-05-04 14:56:36 +02003843 ssl->conf = conf;
Paul Bakker62f2dee2012-09-28 07:31:51 +00003844
3845 /*
Manuel Pégourié-Gonnard06193482014-02-14 08:39:32 +01003846 * Prepare base structures
Paul Bakker62f2dee2012-09-28 07:31:51 +00003847 */
k-stachowiakc9a5f022018-07-24 13:53:31 +02003848
3849 /* Set to NULL in case of an error condition */
3850 ssl->out_buf = NULL;
k-stachowiaka47911c2018-07-04 17:41:58 +02003851
Darryl Greenb33cc762019-11-28 14:29:44 +00003852#if defined(MBEDTLS_SSL_VARIABLE_BUFFER_LENGTH)
3853 ssl->in_buf_len = in_buf_len;
3854#endif
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003855 ssl->in_buf = mbedtls_calloc(1, in_buf_len);
3856 if (ssl->in_buf == NULL) {
3857 MBEDTLS_SSL_DEBUG_MSG(1, ("alloc(%" MBEDTLS_PRINTF_SIZET " bytes) failed", in_buf_len));
k-stachowiak9f7798e2018-07-31 16:52:32 +02003858 ret = MBEDTLS_ERR_SSL_ALLOC_FAILED;
k-stachowiaka47911c2018-07-04 17:41:58 +02003859 goto error;
Angus Grattond8213d02016-05-25 20:56:48 +10003860 }
3861
Darryl Greenb33cc762019-11-28 14:29:44 +00003862#if defined(MBEDTLS_SSL_VARIABLE_BUFFER_LENGTH)
3863 ssl->out_buf_len = out_buf_len;
3864#endif
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003865 ssl->out_buf = mbedtls_calloc(1, out_buf_len);
3866 if (ssl->out_buf == NULL) {
3867 MBEDTLS_SSL_DEBUG_MSG(1, ("alloc(%" MBEDTLS_PRINTF_SIZET " bytes) failed", out_buf_len));
k-stachowiak9f7798e2018-07-31 16:52:32 +02003868 ret = MBEDTLS_ERR_SSL_ALLOC_FAILED;
k-stachowiaka47911c2018-07-04 17:41:58 +02003869 goto error;
Paul Bakker5121ce52009-01-03 21:22:43 +00003870 }
3871
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003872 mbedtls_ssl_reset_in_out_pointers(ssl);
Manuel Pégourié-Gonnard419d5ae2015-05-04 19:32:36 +02003873
Johan Pascalb62bb512015-12-03 21:56:45 +01003874#if defined(MBEDTLS_SSL_DTLS_SRTP)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003875 memset(&ssl->dtls_srtp_info, 0, sizeof(ssl->dtls_srtp_info));
Johan Pascalb62bb512015-12-03 21:56:45 +01003876#endif
3877
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003878 if ((ret = ssl_handshake_init(ssl)) != 0) {
k-stachowiaka47911c2018-07-04 17:41:58 +02003879 goto error;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003880 }
Paul Bakker5121ce52009-01-03 21:22:43 +00003881
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003882 return 0;
k-stachowiaka47911c2018-07-04 17:41:58 +02003883
3884error:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003885 mbedtls_free(ssl->in_buf);
3886 mbedtls_free(ssl->out_buf);
k-stachowiaka47911c2018-07-04 17:41:58 +02003887
3888 ssl->conf = NULL;
3889
Darryl Greenb33cc762019-11-28 14:29:44 +00003890#if defined(MBEDTLS_SSL_VARIABLE_BUFFER_LENGTH)
3891 ssl->in_buf_len = 0;
3892 ssl->out_buf_len = 0;
3893#endif
k-stachowiaka47911c2018-07-04 17:41:58 +02003894 ssl->in_buf = NULL;
3895 ssl->out_buf = NULL;
3896
3897 ssl->in_hdr = NULL;
3898 ssl->in_ctr = NULL;
3899 ssl->in_len = NULL;
3900 ssl->in_iv = NULL;
3901 ssl->in_msg = NULL;
3902
3903 ssl->out_hdr = NULL;
3904 ssl->out_ctr = NULL;
3905 ssl->out_len = NULL;
3906 ssl->out_iv = NULL;
3907 ssl->out_msg = NULL;
3908
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003909 return ret;
Paul Bakker5121ce52009-01-03 21:22:43 +00003910}
3911
3912/*
Paul Bakker7eb013f2011-10-06 12:37:39 +00003913 * Reset an initialized and used SSL context for re-use while retaining
3914 * all application-set variables, function pointers and data.
Manuel Pégourié-Gonnard3f09b6d2015-09-08 11:58:14 +02003915 *
3916 * If partial is non-zero, keep data in the input buffer and client ID.
3917 * (Use when a DTLS client reconnects from the same port.)
Paul Bakker7eb013f2011-10-06 12:37:39 +00003918 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003919int mbedtls_ssl_session_reset_int(mbedtls_ssl_context *ssl, int partial)
Paul Bakker7eb013f2011-10-06 12:37:39 +00003920{
Janos Follath865b3eb2019-12-16 11:46:15 +00003921 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Darryl Greenb33cc762019-11-28 14:29:44 +00003922#if defined(MBEDTLS_SSL_VARIABLE_BUFFER_LENGTH)
3923 size_t in_buf_len = ssl->in_buf_len;
3924 size_t out_buf_len = ssl->out_buf_len;
3925#else
3926 size_t in_buf_len = MBEDTLS_SSL_IN_BUFFER_LEN;
3927 size_t out_buf_len = MBEDTLS_SSL_OUT_BUFFER_LEN;
3928#endif
Paul Bakker48916f92012-09-16 19:57:18 +00003929
Hanno Becker7e772132018-08-10 12:38:21 +01003930#if !defined(MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE) || \
3931 !defined(MBEDTLS_SSL_SRV_C)
3932 ((void) partial);
3933#endif
3934
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003935 ssl->state = MBEDTLS_SSL_HELLO_REQUEST;
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01003936
Manuel Pégourié-Gonnard286a1362015-05-13 16:22:05 +02003937 /* Cancel any possibly running timer */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003938 mbedtls_ssl_set_timer(ssl, 0);
Manuel Pégourié-Gonnard286a1362015-05-13 16:22:05 +02003939
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003940#if defined(MBEDTLS_SSL_RENEGOTIATION)
3941 ssl->renego_status = MBEDTLS_SSL_INITIAL_HANDSHAKE;
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01003942 ssl->renego_records_seen = 0;
Paul Bakker48916f92012-09-16 19:57:18 +00003943
3944 ssl->verify_data_len = 0;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003945 memset(ssl->own_verify_data, 0, MBEDTLS_SSL_VERIFY_DATA_MAX_LEN);
3946 memset(ssl->peer_verify_data, 0, MBEDTLS_SSL_VERIFY_DATA_MAX_LEN);
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01003947#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003948 ssl->secure_renegotiation = MBEDTLS_SSL_LEGACY_RENEGOTIATION;
Paul Bakker48916f92012-09-16 19:57:18 +00003949
Paul Bakker7eb013f2011-10-06 12:37:39 +00003950 ssl->in_offt = NULL;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003951 mbedtls_ssl_reset_in_out_pointers(ssl);
Paul Bakker7eb013f2011-10-06 12:37:39 +00003952
3953 ssl->in_msgtype = 0;
3954 ssl->in_msglen = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003955#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02003956 ssl->next_record_offset = 0;
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02003957 ssl->in_epoch = 0;
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02003958#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003959#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003960 mbedtls_ssl_dtls_replay_reset(ssl);
Manuel Pégourié-Gonnardb47368a2014-09-24 13:29:58 +02003961#endif
Paul Bakker7eb013f2011-10-06 12:37:39 +00003962
3963 ssl->in_hslen = 0;
3964 ssl->nb_zero = 0;
Hanno Beckeraf0665d2017-05-24 09:16:26 +01003965
3966 ssl->keep_current_message = 0;
Paul Bakker7eb013f2011-10-06 12:37:39 +00003967
3968 ssl->out_msgtype = 0;
3969 ssl->out_msglen = 0;
3970 ssl->out_left = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003971#if defined(MBEDTLS_SSL_CBC_RECORD_SPLITTING)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003972 if (ssl->split_done != MBEDTLS_SSL_CBC_RECORD_SPLITTING_DISABLED) {
Manuel Pégourié-Gonnardcfa477e2015-01-07 14:50:54 +01003973 ssl->split_done = 0;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003974 }
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +01003975#endif
Paul Bakker7eb013f2011-10-06 12:37:39 +00003976
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003977 memset(ssl->cur_out_ctr, 0, sizeof(ssl->cur_out_ctr));
Hanno Becker19859472018-08-06 09:40:20 +01003978
Paul Bakker48916f92012-09-16 19:57:18 +00003979 ssl->transform_in = NULL;
3980 ssl->transform_out = NULL;
Paul Bakker7eb013f2011-10-06 12:37:39 +00003981
Hanno Becker78640902018-08-13 16:35:15 +01003982 ssl->session_in = NULL;
3983 ssl->session_out = NULL;
3984
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003985 memset(ssl->out_buf, 0, out_buf_len);
Hanno Becker4ccbf062018-08-10 11:20:38 +01003986
David Horstmannd4f22082022-10-26 18:25:14 +01003987 int clear_in_buf = 1;
Hanno Becker4ccbf062018-08-10 11:20:38 +01003988#if defined(MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE) && defined(MBEDTLS_SSL_SRV_C)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003989 if (partial != 0) {
David Horstmannd4f22082022-10-26 18:25:14 +01003990 clear_in_buf = 0;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003991 }
Hanno Becker4ccbf062018-08-10 11:20:38 +01003992#endif /* MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE && MBEDTLS_SSL_SRV_C */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003993 if (clear_in_buf) {
Hanno Becker4ccbf062018-08-10 11:20:38 +01003994 ssl->in_left = 0;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003995 memset(ssl->in_buf, 0, in_buf_len);
Hanno Becker4ccbf062018-08-10 11:20:38 +01003996 }
Paul Bakker05ef8352012-05-08 09:17:57 +00003997
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003998#if defined(MBEDTLS_SSL_HW_RECORD_ACCEL)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003999 if (mbedtls_ssl_hw_record_reset != NULL) {
4000 MBEDTLS_SSL_DEBUG_MSG(2, ("going for mbedtls_ssl_hw_record_reset()"));
4001 if ((ret = mbedtls_ssl_hw_record_reset(ssl)) != 0) {
4002 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ssl_hw_record_reset", ret);
4003 return MBEDTLS_ERR_SSL_HW_ACCEL_FAILED;
Paul Bakker2770fbd2012-07-03 13:30:23 +00004004 }
Paul Bakker05ef8352012-05-08 09:17:57 +00004005 }
4006#endif
Paul Bakker2770fbd2012-07-03 13:30:23 +00004007
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004008 if (ssl->transform) {
4009 mbedtls_ssl_transform_free(ssl->transform);
4010 mbedtls_free(ssl->transform);
Paul Bakker48916f92012-09-16 19:57:18 +00004011 ssl->transform = NULL;
Paul Bakker2770fbd2012-07-03 13:30:23 +00004012 }
Paul Bakker48916f92012-09-16 19:57:18 +00004013
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004014 if (ssl->session) {
4015 mbedtls_ssl_session_free(ssl->session);
4016 mbedtls_free(ssl->session);
Paul Bakkerc0463502013-02-14 11:19:38 +01004017 ssl->session = NULL;
4018 }
4019
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004020#if defined(MBEDTLS_SSL_ALPN)
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02004021 ssl->alpn_chosen = NULL;
4022#endif
4023
Manuel Pégourié-Gonnarde057d3b2015-05-20 10:59:43 +02004024#if defined(MBEDTLS_SSL_DTLS_HELLO_VERIFY) && defined(MBEDTLS_SSL_SRV_C)
David Horstmannb5b1ed22022-10-27 13:21:49 +01004025 int free_cli_id = 1;
Hanno Becker4ccbf062018-08-10 11:20:38 +01004026#if defined(MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004027 if (partial != 0) {
David Horstmannd4f22082022-10-26 18:25:14 +01004028 free_cli_id = 0;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004029 }
Hanno Becker4ccbf062018-08-10 11:20:38 +01004030#endif
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004031 if (free_cli_id) {
4032 mbedtls_free(ssl->cli_id);
Manuel Pégourié-Gonnard3f09b6d2015-09-08 11:58:14 +02004033 ssl->cli_id = NULL;
4034 ssl->cli_id_len = 0;
4035 }
Manuel Pégourié-Gonnard43c02182014-07-22 17:32:01 +02004036#endif
4037
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004038 if ((ret = ssl_handshake_init(ssl)) != 0) {
4039 return ret;
4040 }
Paul Bakker2770fbd2012-07-03 13:30:23 +00004041
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004042 return 0;
Paul Bakker7eb013f2011-10-06 12:37:39 +00004043}
4044
Manuel Pégourié-Gonnard779e4292013-08-03 13:50:48 +02004045/*
Manuel Pégourié-Gonnard3f09b6d2015-09-08 11:58:14 +02004046 * Reset an initialized and used SSL context for re-use while retaining
4047 * all application-set variables, function pointers and data.
4048 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004049int mbedtls_ssl_session_reset(mbedtls_ssl_context *ssl)
Manuel Pégourié-Gonnard3f09b6d2015-09-08 11:58:14 +02004050{
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004051 return mbedtls_ssl_session_reset_int(ssl, 0);
Manuel Pégourié-Gonnard3f09b6d2015-09-08 11:58:14 +02004052}
4053
4054/*
Paul Bakker5121ce52009-01-03 21:22:43 +00004055 * SSL set accessors
4056 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004057void mbedtls_ssl_conf_endpoint(mbedtls_ssl_config *conf, int endpoint)
Paul Bakker5121ce52009-01-03 21:22:43 +00004058{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02004059 conf->endpoint = endpoint;
Paul Bakker5121ce52009-01-03 21:22:43 +00004060}
4061
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004062void mbedtls_ssl_conf_transport(mbedtls_ssl_config *conf, int transport)
Manuel Pégourié-Gonnard0b1ff292014-02-06 13:04:16 +01004063{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02004064 conf->transport = transport;
Manuel Pégourié-Gonnard0b1ff292014-02-06 13:04:16 +01004065}
4066
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004067#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004068void mbedtls_ssl_conf_dtls_anti_replay(mbedtls_ssl_config *conf, char mode)
Manuel Pégourié-Gonnard27393132014-09-24 14:41:11 +02004069{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02004070 conf->anti_replay = mode;
Manuel Pégourié-Gonnard27393132014-09-24 14:41:11 +02004071}
4072#endif
4073
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004074#if defined(MBEDTLS_SSL_DTLS_BADMAC_LIMIT)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004075void mbedtls_ssl_conf_dtls_badmac_limit(mbedtls_ssl_config *conf, unsigned limit)
Manuel Pégourié-Gonnardb0643d12014-10-14 18:30:36 +02004076{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02004077 conf->badmac_limit = limit;
Manuel Pégourié-Gonnardb0643d12014-10-14 18:30:36 +02004078}
4079#endif
4080
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004081#if defined(MBEDTLS_SSL_PROTO_DTLS)
Hanno Becker04da1892018-08-14 13:22:10 +01004082
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004083void mbedtls_ssl_set_datagram_packing(mbedtls_ssl_context *ssl,
4084 unsigned allow_packing)
Hanno Becker04da1892018-08-14 13:22:10 +01004085{
4086 ssl->disable_datagram_packing = !allow_packing;
4087}
4088
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004089void mbedtls_ssl_conf_handshake_timeout(mbedtls_ssl_config *conf,
4090 uint32_t min, uint32_t max)
Manuel Pégourié-Gonnard905dd242014-10-01 12:03:55 +02004091{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02004092 conf->hs_timeout_min = min;
4093 conf->hs_timeout_max = max;
Manuel Pégourié-Gonnard905dd242014-10-01 12:03:55 +02004094}
4095#endif
4096
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004097void mbedtls_ssl_conf_authmode(mbedtls_ssl_config *conf, int authmode)
Paul Bakker5121ce52009-01-03 21:22:43 +00004098{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02004099 conf->authmode = authmode;
Paul Bakker5121ce52009-01-03 21:22:43 +00004100}
4101
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004102#if defined(MBEDTLS_X509_CRT_PARSE_C)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004103void mbedtls_ssl_conf_verify(mbedtls_ssl_config *conf,
4104 int (*f_vrfy)(void *, mbedtls_x509_crt *, int, uint32_t *),
4105 void *p_vrfy)
Paul Bakkerb63b0af2011-01-13 17:54:59 +00004106{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02004107 conf->f_vrfy = f_vrfy;
4108 conf->p_vrfy = p_vrfy;
Paul Bakkerb63b0af2011-01-13 17:54:59 +00004109}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004110#endif /* MBEDTLS_X509_CRT_PARSE_C */
Paul Bakkerb63b0af2011-01-13 17:54:59 +00004111
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004112void mbedtls_ssl_conf_rng(mbedtls_ssl_config *conf,
4113 int (*f_rng)(void *, unsigned char *, size_t),
4114 void *p_rng)
Paul Bakker5121ce52009-01-03 21:22:43 +00004115{
Manuel Pégourié-Gonnard750e4d72015-05-07 12:35:38 +01004116 conf->f_rng = f_rng;
4117 conf->p_rng = p_rng;
Paul Bakker5121ce52009-01-03 21:22:43 +00004118}
4119
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004120void mbedtls_ssl_conf_dbg(mbedtls_ssl_config *conf,
4121 void (*f_dbg)(void *, int, const char *, int, const char *),
4122 void *p_dbg)
Paul Bakker5121ce52009-01-03 21:22:43 +00004123{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02004124 conf->f_dbg = f_dbg;
4125 conf->p_dbg = p_dbg;
Paul Bakker5121ce52009-01-03 21:22:43 +00004126}
4127
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004128void mbedtls_ssl_set_bio(mbedtls_ssl_context *ssl,
4129 void *p_bio,
4130 mbedtls_ssl_send_t *f_send,
4131 mbedtls_ssl_recv_t *f_recv,
4132 mbedtls_ssl_recv_timeout_t *f_recv_timeout)
Manuel Pégourié-Gonnard8fa6dfd2014-09-17 10:47:43 +02004133{
4134 ssl->p_bio = p_bio;
4135 ssl->f_send = f_send;
4136 ssl->f_recv = f_recv;
4137 ssl->f_recv_timeout = f_recv_timeout;
Manuel Pégourié-Gonnard97fd52c2015-05-06 15:38:52 +01004138}
4139
Manuel Pégourié-Gonnard6e7aaca2018-08-20 10:37:23 +02004140#if defined(MBEDTLS_SSL_PROTO_DTLS)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004141void mbedtls_ssl_set_mtu(mbedtls_ssl_context *ssl, uint16_t mtu)
Manuel Pégourié-Gonnard6e7aaca2018-08-20 10:37:23 +02004142{
4143 ssl->mtu = mtu;
4144}
4145#endif
4146
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004147void mbedtls_ssl_conf_read_timeout(mbedtls_ssl_config *conf, uint32_t timeout)
Manuel Pégourié-Gonnard97fd52c2015-05-06 15:38:52 +01004148{
4149 conf->read_timeout = timeout;
Manuel Pégourié-Gonnard8fa6dfd2014-09-17 10:47:43 +02004150}
4151
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004152void mbedtls_ssl_set_timer_cb(mbedtls_ssl_context *ssl,
4153 void *p_timer,
4154 mbedtls_ssl_set_timer_t *f_set_timer,
4155 mbedtls_ssl_get_timer_t *f_get_timer)
Manuel Pégourié-Gonnard2e012912015-05-12 20:55:41 +02004156{
4157 ssl->p_timer = p_timer;
4158 ssl->f_set_timer = f_set_timer;
4159 ssl->f_get_timer = f_get_timer;
Manuel Pégourié-Gonnard286a1362015-05-13 16:22:05 +02004160
4161 /* Make sure we start with no timer running */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004162 mbedtls_ssl_set_timer(ssl, 0);
Manuel Pégourié-Gonnard2e012912015-05-12 20:55:41 +02004163}
4164
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004165#if defined(MBEDTLS_SSL_SRV_C)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004166void mbedtls_ssl_conf_session_cache(mbedtls_ssl_config *conf,
4167 void *p_cache,
4168 int (*f_get_cache)(void *, mbedtls_ssl_session *),
4169 int (*f_set_cache)(void *, const mbedtls_ssl_session *))
Paul Bakker5121ce52009-01-03 21:22:43 +00004170{
Manuel Pégourié-Gonnard5cb33082015-05-06 18:06:26 +01004171 conf->p_cache = p_cache;
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02004172 conf->f_get_cache = f_get_cache;
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02004173 conf->f_set_cache = f_set_cache;
Paul Bakker5121ce52009-01-03 21:22:43 +00004174}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004175#endif /* MBEDTLS_SSL_SRV_C */
Paul Bakker5121ce52009-01-03 21:22:43 +00004176
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004177#if defined(MBEDTLS_SSL_CLI_C)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004178int mbedtls_ssl_set_session(mbedtls_ssl_context *ssl, const mbedtls_ssl_session *session)
Paul Bakker5121ce52009-01-03 21:22:43 +00004179{
Janos Follath865b3eb2019-12-16 11:46:15 +00004180 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +02004181
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004182 if (ssl == NULL ||
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +02004183 session == NULL ||
4184 ssl->session_negotiate == NULL ||
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004185 ssl->conf->endpoint != MBEDTLS_SSL_IS_CLIENT) {
4186 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +02004187 }
4188
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004189 if ((ret = mbedtls_ssl_session_copy(ssl->session_negotiate,
4190 session)) != 0) {
4191 return ret;
4192 }
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +02004193
Paul Bakker0a597072012-09-25 21:55:46 +00004194 ssl->handshake->resume = 1;
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +02004195
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004196 return 0;
Paul Bakker5121ce52009-01-03 21:22:43 +00004197}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004198#endif /* MBEDTLS_SSL_CLI_C */
Paul Bakker5121ce52009-01-03 21:22:43 +00004199
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004200void mbedtls_ssl_conf_ciphersuites(mbedtls_ssl_config *conf,
4201 const int *ciphersuites)
Paul Bakker5121ce52009-01-03 21:22:43 +00004202{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02004203 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_0] = ciphersuites;
4204 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_1] = ciphersuites;
4205 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_2] = ciphersuites;
4206 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_3] = ciphersuites;
Paul Bakker8f4ddae2013-04-15 15:09:54 +02004207}
4208
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004209void mbedtls_ssl_conf_ciphersuites_for_version(mbedtls_ssl_config *conf,
4210 const int *ciphersuites,
4211 int major, int minor)
Paul Bakker8f4ddae2013-04-15 15:09:54 +02004212{
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004213 if (major != MBEDTLS_SSL_MAJOR_VERSION_3) {
Paul Bakker8f4ddae2013-04-15 15:09:54 +02004214 return;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004215 }
Paul Bakker8f4ddae2013-04-15 15:09:54 +02004216
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004217 if (minor < MBEDTLS_SSL_MINOR_VERSION_0 || minor > MBEDTLS_SSL_MINOR_VERSION_3) {
Paul Bakker8f4ddae2013-04-15 15:09:54 +02004218 return;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004219 }
Paul Bakker8f4ddae2013-04-15 15:09:54 +02004220
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02004221 conf->ciphersuite_list[minor] = ciphersuites;
Paul Bakker5121ce52009-01-03 21:22:43 +00004222}
4223
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004224#if defined(MBEDTLS_X509_CRT_PARSE_C)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004225void mbedtls_ssl_conf_cert_profile(mbedtls_ssl_config *conf,
4226 const mbedtls_x509_crt_profile *profile)
Manuel Pégourié-Gonnard6e3ee3a2015-06-17 10:58:20 +02004227{
4228 conf->cert_profile = profile;
4229}
4230
Manuel Pégourié-Gonnard8f618a82015-05-10 21:13:36 +02004231/* Append a new keycert entry to a (possibly empty) list */
Manuel Pégourié-Gonnardd904d662022-06-17 10:24:00 +02004232MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004233static int ssl_append_key_cert(mbedtls_ssl_key_cert **head,
4234 mbedtls_x509_crt *cert,
4235 mbedtls_pk_context *key)
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02004236{
niisato8ee24222018-06-25 19:05:48 +09004237 mbedtls_ssl_key_cert *new_cert;
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02004238
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004239 new_cert = mbedtls_calloc(1, sizeof(mbedtls_ssl_key_cert));
4240 if (new_cert == NULL) {
4241 return MBEDTLS_ERR_SSL_ALLOC_FAILED;
4242 }
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02004243
niisato8ee24222018-06-25 19:05:48 +09004244 new_cert->cert = cert;
4245 new_cert->key = key;
4246 new_cert->next = NULL;
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02004247
Manuel Pégourié-Gonnard8f618a82015-05-10 21:13:36 +02004248 /* Update head is the list was null, else add to the end */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004249 if (*head == NULL) {
niisato8ee24222018-06-25 19:05:48 +09004250 *head = new_cert;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004251 } else {
Manuel Pégourié-Gonnard8f618a82015-05-10 21:13:36 +02004252 mbedtls_ssl_key_cert *cur = *head;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004253 while (cur->next != NULL) {
Manuel Pégourié-Gonnard8f618a82015-05-10 21:13:36 +02004254 cur = cur->next;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004255 }
niisato8ee24222018-06-25 19:05:48 +09004256 cur->next = new_cert;
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02004257 }
4258
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004259 return 0;
Manuel Pégourié-Gonnard8f618a82015-05-10 21:13:36 +02004260}
4261
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004262int mbedtls_ssl_conf_own_cert(mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnard8f618a82015-05-10 21:13:36 +02004263 mbedtls_x509_crt *own_cert,
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004264 mbedtls_pk_context *pk_key)
Manuel Pégourié-Gonnard8f618a82015-05-10 21:13:36 +02004265{
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004266 return ssl_append_key_cert(&conf->key_cert, own_cert, pk_key);
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02004267}
4268
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004269void mbedtls_ssl_conf_ca_chain(mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnardbc2b7712015-05-06 11:14:19 +01004270 mbedtls_x509_crt *ca_chain,
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004271 mbedtls_x509_crl *ca_crl)
Paul Bakker5121ce52009-01-03 21:22:43 +00004272{
Manuel Pégourié-Gonnardbc2b7712015-05-06 11:14:19 +01004273 conf->ca_chain = ca_chain;
4274 conf->ca_crl = ca_crl;
Hanno Becker5adaad92019-03-27 16:54:37 +00004275
4276#if defined(MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK)
4277 /* mbedtls_ssl_conf_ca_chain() and mbedtls_ssl_conf_ca_cb()
4278 * cannot be used together. */
4279 conf->f_ca_cb = NULL;
4280 conf->p_ca_cb = NULL;
4281#endif /* MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK */
Paul Bakker5121ce52009-01-03 21:22:43 +00004282}
Hanno Becker5adaad92019-03-27 16:54:37 +00004283
4284#if defined(MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004285void mbedtls_ssl_conf_ca_cb(mbedtls_ssl_config *conf,
4286 mbedtls_x509_crt_ca_cb_t f_ca_cb,
4287 void *p_ca_cb)
Hanno Becker5adaad92019-03-27 16:54:37 +00004288{
4289 conf->f_ca_cb = f_ca_cb;
4290 conf->p_ca_cb = p_ca_cb;
4291
4292 /* mbedtls_ssl_conf_ca_chain() and mbedtls_ssl_conf_ca_cb()
4293 * cannot be used together. */
4294 conf->ca_chain = NULL;
4295 conf->ca_crl = NULL;
4296}
4297#endif /* MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004298#endif /* MBEDTLS_X509_CRT_PARSE_C */
Paul Bakkereb2c6582012-09-27 19:15:01 +00004299
Manuel Pégourié-Gonnard1af6c852015-05-10 23:10:37 +02004300#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004301int mbedtls_ssl_set_hs_own_cert(mbedtls_ssl_context *ssl,
4302 mbedtls_x509_crt *own_cert,
4303 mbedtls_pk_context *pk_key)
Manuel Pégourié-Gonnard1af6c852015-05-10 23:10:37 +02004304{
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004305 return ssl_append_key_cert(&ssl->handshake->sni_key_cert,
4306 own_cert, pk_key);
Manuel Pégourié-Gonnard1af6c852015-05-10 23:10:37 +02004307}
Manuel Pégourié-Gonnard22bfa4b2015-05-11 08:46:37 +02004308
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004309void mbedtls_ssl_set_hs_ca_chain(mbedtls_ssl_context *ssl,
4310 mbedtls_x509_crt *ca_chain,
4311 mbedtls_x509_crl *ca_crl)
Manuel Pégourié-Gonnard22bfa4b2015-05-11 08:46:37 +02004312{
4313 ssl->handshake->sni_ca_chain = ca_chain;
4314 ssl->handshake->sni_ca_crl = ca_crl;
4315}
Manuel Pégourié-Gonnardcdc26ae2015-06-19 12:16:31 +02004316
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004317void mbedtls_ssl_set_hs_authmode(mbedtls_ssl_context *ssl,
4318 int authmode)
Manuel Pégourié-Gonnardcdc26ae2015-06-19 12:16:31 +02004319{
4320 ssl->handshake->sni_authmode = authmode;
4321}
Manuel Pégourié-Gonnard1af6c852015-05-10 23:10:37 +02004322#endif /* MBEDTLS_SSL_SERVER_NAME_INDICATION */
4323
Hanno Becker8927c832019-04-03 12:52:50 +01004324#if defined(MBEDTLS_X509_CRT_PARSE_C)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004325void mbedtls_ssl_set_verify(mbedtls_ssl_context *ssl,
4326 int (*f_vrfy)(void *, mbedtls_x509_crt *, int, uint32_t *),
4327 void *p_vrfy)
Hanno Becker8927c832019-04-03 12:52:50 +01004328{
4329 ssl->f_vrfy = f_vrfy;
4330 ssl->p_vrfy = p_vrfy;
4331}
4332#endif
4333
Manuel Pégourié-Gonnardeef142d2015-09-16 10:05:04 +02004334#if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
Manuel Pégourié-Gonnard7002f4a2015-09-15 12:43:43 +02004335/*
4336 * Set EC J-PAKE password for current handshake
4337 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004338int mbedtls_ssl_set_hs_ecjpake_password(mbedtls_ssl_context *ssl,
4339 const unsigned char *pw,
4340 size_t pw_len)
Manuel Pégourié-Gonnard7002f4a2015-09-15 12:43:43 +02004341{
4342 mbedtls_ecjpake_role role;
4343
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004344 if (ssl->handshake == NULL || ssl->conf == NULL) {
4345 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
4346 }
Manuel Pégourié-Gonnard7002f4a2015-09-15 12:43:43 +02004347
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004348 if (ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER) {
Manuel Pégourié-Gonnard7002f4a2015-09-15 12:43:43 +02004349 role = MBEDTLS_ECJPAKE_SERVER;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004350 } else {
Manuel Pégourié-Gonnard7002f4a2015-09-15 12:43:43 +02004351 role = MBEDTLS_ECJPAKE_CLIENT;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004352 }
Manuel Pégourié-Gonnard7002f4a2015-09-15 12:43:43 +02004353
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004354 return mbedtls_ecjpake_setup(&ssl->handshake->ecjpake_ctx,
4355 role,
4356 MBEDTLS_MD_SHA256,
4357 MBEDTLS_ECP_DP_SECP256R1,
4358 pw, pw_len);
Manuel Pégourié-Gonnard7002f4a2015-09-15 12:43:43 +02004359}
Manuel Pégourié-Gonnardeef142d2015-09-16 10:05:04 +02004360#endif /* MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED */
Manuel Pégourié-Gonnard7002f4a2015-09-15 12:43:43 +02004361
Gilles Peskineeccd8882020-03-10 12:19:08 +01004362#if defined(MBEDTLS_KEY_EXCHANGE_SOME_PSK_ENABLED)
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01004363
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004364static void ssl_conf_remove_psk(mbedtls_ssl_config *conf)
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01004365{
4366 /* Remove reference to existing PSK, if any. */
4367#if defined(MBEDTLS_USE_PSA_CRYPTO)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004368 if (!mbedtls_svc_key_id_is_null(conf->psk_opaque)) {
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01004369 /* The maintenance of the PSK key slot is the
4370 * user's responsibility. */
Ronald Croncf56a0a2020-08-04 09:51:30 +02004371 conf->psk_opaque = MBEDTLS_SVC_KEY_ID_INIT;
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01004372 }
Hanno Beckera63ac3f2018-11-05 12:47:16 +00004373 /* This and the following branch should never
Tom Cosgrove49f99bc2022-12-04 16:44:21 +00004374 * be taken simultaneously as we maintain the
Hanno Beckera63ac3f2018-11-05 12:47:16 +00004375 * invariant that raw and opaque PSKs are never
4376 * configured simultaneously. As a safeguard,
4377 * though, `else` is omitted here. */
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01004378#endif /* MBEDTLS_USE_PSA_CRYPTO */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004379 if (conf->psk != NULL) {
4380 mbedtls_platform_zeroize(conf->psk, conf->psk_len);
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01004381
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004382 mbedtls_free(conf->psk);
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01004383 conf->psk = NULL;
4384 conf->psk_len = 0;
4385 }
4386
4387 /* Remove reference to PSK identity, if any. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004388 if (conf->psk_identity != NULL) {
4389 mbedtls_free(conf->psk_identity);
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01004390 conf->psk_identity = NULL;
4391 conf->psk_identity_len = 0;
4392 }
4393}
4394
Hanno Becker7390c712018-11-15 13:33:04 +00004395/* This function assumes that PSK identity in the SSL config is unset.
4396 * It checks that the provided identity is well-formed and attempts
4397 * to make a copy of it in the SSL config.
4398 * On failure, the PSK identity in the config remains unset. */
Manuel Pégourié-Gonnardd904d662022-06-17 10:24:00 +02004399MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004400static int ssl_conf_set_psk_identity(mbedtls_ssl_config *conf,
4401 unsigned char const *psk_identity,
4402 size_t psk_identity_len)
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02004403{
Manuel Pégourié-Gonnardc6b5d832015-08-27 16:37:35 +02004404 /* Identity len will be encoded on two bytes */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004405 if (psk_identity == NULL ||
4406 (psk_identity_len >> 16) != 0 ||
4407 psk_identity_len > MBEDTLS_SSL_OUT_CONTENT_LEN) {
4408 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
Manuel Pégourié-Gonnardc6b5d832015-08-27 16:37:35 +02004409 }
4410
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004411 conf->psk_identity = mbedtls_calloc(1, psk_identity_len);
4412 if (conf->psk_identity == NULL) {
4413 return MBEDTLS_ERR_SSL_ALLOC_FAILED;
4414 }
Paul Bakker6db455e2013-09-18 17:29:31 +02004415
Manuel Pégourié-Gonnard120fdbd2015-05-07 17:07:50 +01004416 conf->psk_identity_len = psk_identity_len;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004417 memcpy(conf->psk_identity, psk_identity, conf->psk_identity_len);
Paul Bakker5ad403f2013-09-18 21:21:30 +02004418
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004419 return 0;
Paul Bakker6db455e2013-09-18 17:29:31 +02004420}
4421
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004422int mbedtls_ssl_conf_psk(mbedtls_ssl_config *conf,
4423 const unsigned char *psk, size_t psk_len,
4424 const unsigned char *psk_identity, size_t psk_identity_len)
Hanno Becker7390c712018-11-15 13:33:04 +00004425{
Janos Follath865b3eb2019-12-16 11:46:15 +00004426 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Hanno Becker7390c712018-11-15 13:33:04 +00004427 /* Remove opaque/raw PSK + PSK Identity */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004428 ssl_conf_remove_psk(conf);
Hanno Becker7390c712018-11-15 13:33:04 +00004429
4430 /* Check and set raw PSK */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004431 if (psk == NULL) {
4432 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
4433 }
4434 if (psk_len == 0) {
4435 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
4436 }
4437 if (psk_len > MBEDTLS_PSK_MAX_LEN) {
4438 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
4439 }
Piotr Nowicki9926eaf2019-11-20 14:54:36 +01004440
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004441 if ((conf->psk = mbedtls_calloc(1, psk_len)) == NULL) {
4442 return MBEDTLS_ERR_SSL_ALLOC_FAILED;
4443 }
Hanno Becker7390c712018-11-15 13:33:04 +00004444 conf->psk_len = psk_len;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004445 memcpy(conf->psk, psk, conf->psk_len);
Hanno Becker7390c712018-11-15 13:33:04 +00004446
4447 /* Check and set PSK Identity */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004448 ret = ssl_conf_set_psk_identity(conf, psk_identity, psk_identity_len);
4449 if (ret != 0) {
4450 ssl_conf_remove_psk(conf);
4451 }
Hanno Becker7390c712018-11-15 13:33:04 +00004452
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004453 return ret;
Hanno Becker7390c712018-11-15 13:33:04 +00004454}
4455
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004456static void ssl_remove_psk(mbedtls_ssl_context *ssl)
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01004457{
4458#if defined(MBEDTLS_USE_PSA_CRYPTO)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004459 if (!mbedtls_svc_key_id_is_null(ssl->handshake->psk_opaque)) {
Ronald Croncf56a0a2020-08-04 09:51:30 +02004460 ssl->handshake->psk_opaque = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004461 } else
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01004462#endif /* MBEDTLS_USE_PSA_CRYPTO */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004463 if (ssl->handshake->psk != NULL) {
4464 mbedtls_platform_zeroize(ssl->handshake->psk,
4465 ssl->handshake->psk_len);
4466 mbedtls_free(ssl->handshake->psk);
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01004467 ssl->handshake->psk_len = 0;
4468 }
4469}
4470
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004471int mbedtls_ssl_set_hs_psk(mbedtls_ssl_context *ssl,
4472 const unsigned char *psk, size_t psk_len)
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01004473{
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004474 if (psk == NULL || ssl->handshake == NULL) {
4475 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
4476 }
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01004477
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004478 if (psk_len > MBEDTLS_PSK_MAX_LEN) {
4479 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
4480 }
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01004481
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004482 ssl_remove_psk(ssl);
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01004483
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004484 if ((ssl->handshake->psk = mbedtls_calloc(1, psk_len)) == NULL) {
4485 return MBEDTLS_ERR_SSL_ALLOC_FAILED;
4486 }
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01004487
4488 ssl->handshake->psk_len = psk_len;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004489 memcpy(ssl->handshake->psk, psk, ssl->handshake->psk_len);
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01004490
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004491 return 0;
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01004492}
4493
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01004494#if defined(MBEDTLS_USE_PSA_CRYPTO)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004495int mbedtls_ssl_conf_psk_opaque(mbedtls_ssl_config *conf,
4496 psa_key_id_t psk,
4497 const unsigned char *psk_identity,
4498 size_t psk_identity_len)
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01004499{
Janos Follath865b3eb2019-12-16 11:46:15 +00004500 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Hanno Becker7390c712018-11-15 13:33:04 +00004501 /* Clear opaque/raw PSK + PSK Identity, if present. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004502 ssl_conf_remove_psk(conf);
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01004503
Hanno Becker7390c712018-11-15 13:33:04 +00004504 /* Check and set opaque PSK */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004505 if (mbedtls_svc_key_id_is_null(psk)) {
4506 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
4507 }
Ronald Croncf56a0a2020-08-04 09:51:30 +02004508 conf->psk_opaque = psk;
Hanno Becker7390c712018-11-15 13:33:04 +00004509
4510 /* Check and set PSK Identity */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004511 ret = ssl_conf_set_psk_identity(conf, psk_identity,
4512 psk_identity_len);
4513 if (ret != 0) {
4514 ssl_conf_remove_psk(conf);
4515 }
Hanno Becker7390c712018-11-15 13:33:04 +00004516
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004517 return ret;
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01004518}
4519
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004520int mbedtls_ssl_set_hs_psk_opaque(mbedtls_ssl_context *ssl,
4521 psa_key_id_t psk)
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01004522{
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004523 if ((mbedtls_svc_key_id_is_null(psk)) ||
4524 (ssl->handshake == NULL)) {
4525 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
4526 }
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01004527
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004528 ssl_remove_psk(ssl);
Ronald Croncf56a0a2020-08-04 09:51:30 +02004529 ssl->handshake->psk_opaque = psk;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004530 return 0;
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01004531}
4532#endif /* MBEDTLS_USE_PSA_CRYPTO */
4533
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004534void mbedtls_ssl_conf_psk_cb(mbedtls_ssl_config *conf,
4535 int (*f_psk)(void *, mbedtls_ssl_context *, const unsigned char *,
4536 size_t),
4537 void *p_psk)
Paul Bakker6db455e2013-09-18 17:29:31 +02004538{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02004539 conf->f_psk = f_psk;
4540 conf->p_psk = p_psk;
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02004541}
Gilles Peskineeccd8882020-03-10 12:19:08 +01004542#endif /* MBEDTLS_KEY_EXCHANGE_SOME_PSK_ENABLED */
Paul Bakker43b7e352011-01-18 15:27:19 +00004543
Manuel Pégourié-Gonnardcf141ca2015-05-20 10:35:51 +02004544#if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_SSL_SRV_C)
Hanno Becker470a8c42017-10-04 15:28:46 +01004545
4546#if !defined(MBEDTLS_DEPRECATED_REMOVED)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004547int mbedtls_ssl_conf_dh_param(mbedtls_ssl_config *conf, const char *dhm_P, const char *dhm_G)
Paul Bakker5121ce52009-01-03 21:22:43 +00004548{
Janos Follath865b3eb2019-12-16 11:46:15 +00004549 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker5121ce52009-01-03 21:22:43 +00004550
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004551 if ((ret = mbedtls_mpi_read_string(&conf->dhm_P, 16, dhm_P)) != 0 ||
4552 (ret = mbedtls_mpi_read_string(&conf->dhm_G, 16, dhm_G)) != 0) {
4553 mbedtls_mpi_free(&conf->dhm_P);
4554 mbedtls_mpi_free(&conf->dhm_G);
4555 return ret;
Manuel Pégourié-Gonnard1028b742015-05-06 17:33:07 +01004556 }
Paul Bakker5121ce52009-01-03 21:22:43 +00004557
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004558 return 0;
Paul Bakker5121ce52009-01-03 21:22:43 +00004559}
Hanno Becker470a8c42017-10-04 15:28:46 +01004560#endif /* MBEDTLS_DEPRECATED_REMOVED */
Paul Bakker5121ce52009-01-03 21:22:43 +00004561
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004562int mbedtls_ssl_conf_dh_param_bin(mbedtls_ssl_config *conf,
4563 const unsigned char *dhm_P, size_t P_len,
4564 const unsigned char *dhm_G, size_t G_len)
Hanno Beckera90658f2017-10-04 15:29:08 +01004565{
Janos Follath865b3eb2019-12-16 11:46:15 +00004566 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Hanno Beckera90658f2017-10-04 15:29:08 +01004567
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004568 mbedtls_mpi_free(&conf->dhm_P);
4569 mbedtls_mpi_free(&conf->dhm_G);
Glenn Straussde081ce2021-12-20 01:43:17 -05004570
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004571 if ((ret = mbedtls_mpi_read_binary(&conf->dhm_P, dhm_P, P_len)) != 0 ||
4572 (ret = mbedtls_mpi_read_binary(&conf->dhm_G, dhm_G, G_len)) != 0) {
4573 mbedtls_mpi_free(&conf->dhm_P);
4574 mbedtls_mpi_free(&conf->dhm_G);
4575 return ret;
Hanno Beckera90658f2017-10-04 15:29:08 +01004576 }
4577
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004578 return 0;
Hanno Beckera90658f2017-10-04 15:29:08 +01004579}
Paul Bakker5121ce52009-01-03 21:22:43 +00004580
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004581int mbedtls_ssl_conf_dh_param_ctx(mbedtls_ssl_config *conf, mbedtls_dhm_context *dhm_ctx)
Paul Bakker1b57b062011-01-06 15:48:19 +00004582{
Janos Follath865b3eb2019-12-16 11:46:15 +00004583 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker1b57b062011-01-06 15:48:19 +00004584
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004585 mbedtls_mpi_free(&conf->dhm_P);
4586 mbedtls_mpi_free(&conf->dhm_G);
Glenn Straussde081ce2021-12-20 01:43:17 -05004587
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004588 if ((ret = mbedtls_mpi_copy(&conf->dhm_P, &dhm_ctx->P)) != 0 ||
4589 (ret = mbedtls_mpi_copy(&conf->dhm_G, &dhm_ctx->G)) != 0) {
4590 mbedtls_mpi_free(&conf->dhm_P);
4591 mbedtls_mpi_free(&conf->dhm_G);
4592 return ret;
Manuel Pégourié-Gonnard1028b742015-05-06 17:33:07 +01004593 }
Paul Bakker1b57b062011-01-06 15:48:19 +00004594
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004595 return 0;
Paul Bakker1b57b062011-01-06 15:48:19 +00004596}
Manuel Pégourié-Gonnardcf141ca2015-05-20 10:35:51 +02004597#endif /* MBEDTLS_DHM_C && MBEDTLS_SSL_SRV_C */
Paul Bakker1b57b062011-01-06 15:48:19 +00004598
Manuel Pégourié-Gonnardbd990d62015-06-11 14:49:42 +02004599#if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_SSL_CLI_C)
4600/*
4601 * Set the minimum length for Diffie-Hellman parameters
4602 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004603void mbedtls_ssl_conf_dhm_min_bitlen(mbedtls_ssl_config *conf,
4604 unsigned int bitlen)
Manuel Pégourié-Gonnardbd990d62015-06-11 14:49:42 +02004605{
4606 conf->dhm_min_bitlen = bitlen;
4607}
4608#endif /* MBEDTLS_DHM_C && MBEDTLS_SSL_CLI_C */
4609
Gilles Peskineeccd8882020-03-10 12:19:08 +01004610#if defined(MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED)
Manuel Pégourié-Gonnard36a8b572015-06-17 12:43:26 +02004611/*
4612 * Set allowed/preferred hashes for handshake signatures
4613 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004614void mbedtls_ssl_conf_sig_hashes(mbedtls_ssl_config *conf,
4615 const int *hashes)
Manuel Pégourié-Gonnard36a8b572015-06-17 12:43:26 +02004616{
4617 conf->sig_hashes = hashes;
4618}
Gilles Peskineeccd8882020-03-10 12:19:08 +01004619#endif /* MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED */
Manuel Pégourié-Gonnard36a8b572015-06-17 12:43:26 +02004620
Manuel Pégourié-Gonnardb541da62015-06-17 11:43:30 +02004621#if defined(MBEDTLS_ECP_C)
Manuel Pégourié-Gonnard7f38ed02014-02-04 15:52:33 +01004622/*
4623 * Set the allowed elliptic curves
4624 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004625void mbedtls_ssl_conf_curves(mbedtls_ssl_config *conf,
4626 const mbedtls_ecp_group_id *curve_list)
Manuel Pégourié-Gonnard7f38ed02014-02-04 15:52:33 +01004627{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02004628 conf->curve_list = curve_list;
Manuel Pégourié-Gonnard7f38ed02014-02-04 15:52:33 +01004629}
Hanno Becker947194e2017-04-07 13:25:49 +01004630#endif /* MBEDTLS_ECP_C */
Manuel Pégourié-Gonnard7f38ed02014-02-04 15:52:33 +01004631
Manuel Pégourié-Gonnardbc2b7712015-05-06 11:14:19 +01004632#if defined(MBEDTLS_X509_CRT_PARSE_C)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004633int mbedtls_ssl_set_hostname(mbedtls_ssl_context *ssl, const char *hostname)
Paul Bakker5121ce52009-01-03 21:22:43 +00004634{
Hanno Becker947194e2017-04-07 13:25:49 +01004635 /* Initialize to suppress unnecessary compiler warning */
4636 size_t hostname_len = 0;
4637
4638 /* Check if new hostname is valid before
4639 * making any change to current one */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004640 if (hostname != NULL) {
4641 hostname_len = strlen(hostname);
Hanno Becker947194e2017-04-07 13:25:49 +01004642
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004643 if (hostname_len > MBEDTLS_SSL_MAX_HOST_NAME_LEN) {
4644 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
4645 }
Hanno Becker947194e2017-04-07 13:25:49 +01004646 }
4647
4648 /* Now it's clear that we will overwrite the old hostname,
4649 * so we can free it safely */
4650
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004651 if (ssl->hostname != NULL) {
4652 mbedtls_platform_zeroize(ssl->hostname, strlen(ssl->hostname));
4653 mbedtls_free(ssl->hostname);
Hanno Becker947194e2017-04-07 13:25:49 +01004654 }
4655
4656 /* Passing NULL as hostname shall clear the old one */
Manuel Pégourié-Gonnardba26c242015-05-06 10:47:06 +01004657
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004658 if (hostname == NULL) {
Hanno Becker947194e2017-04-07 13:25:49 +01004659 ssl->hostname = NULL;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004660 } else {
4661 ssl->hostname = mbedtls_calloc(1, hostname_len + 1);
4662 if (ssl->hostname == NULL) {
4663 return MBEDTLS_ERR_SSL_ALLOC_FAILED;
4664 }
Paul Bakker75c1a6f2013-08-19 14:25:29 +02004665
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004666 memcpy(ssl->hostname, hostname, hostname_len);
Paul Bakker75c1a6f2013-08-19 14:25:29 +02004667
Hanno Becker947194e2017-04-07 13:25:49 +01004668 ssl->hostname[hostname_len] = '\0';
4669 }
Paul Bakker5121ce52009-01-03 21:22:43 +00004670
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004671 return 0;
Paul Bakker5121ce52009-01-03 21:22:43 +00004672}
Hanno Becker1a9a51c2017-04-07 13:02:16 +01004673#endif /* MBEDTLS_X509_CRT_PARSE_C */
Paul Bakker5121ce52009-01-03 21:22:43 +00004674
Manuel Pégourié-Gonnardbc2b7712015-05-06 11:14:19 +01004675#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004676void mbedtls_ssl_conf_sni(mbedtls_ssl_config *conf,
4677 int (*f_sni)(void *, mbedtls_ssl_context *,
4678 const unsigned char *, size_t),
4679 void *p_sni)
Paul Bakker5701cdc2012-09-27 21:49:42 +00004680{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02004681 conf->f_sni = f_sni;
4682 conf->p_sni = p_sni;
Paul Bakker5701cdc2012-09-27 21:49:42 +00004683}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004684#endif /* MBEDTLS_SSL_SERVER_NAME_INDICATION */
Paul Bakker5701cdc2012-09-27 21:49:42 +00004685
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004686#if defined(MBEDTLS_SSL_ALPN)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004687int mbedtls_ssl_conf_alpn_protocols(mbedtls_ssl_config *conf, const char **protos)
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02004688{
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +02004689 size_t cur_len, tot_len;
4690 const char **p;
4691
4692 /*
Brian J Murray1903fb32016-11-06 04:45:15 -08004693 * RFC 7301 3.1: "Empty strings MUST NOT be included and byte strings
4694 * MUST NOT be truncated."
4695 * We check lengths now rather than later.
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +02004696 */
4697 tot_len = 0;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004698 for (p = protos; *p != NULL; p++) {
4699 cur_len = strlen(*p);
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +02004700 tot_len += cur_len;
4701
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004702 if ((cur_len == 0) ||
4703 (cur_len > MBEDTLS_SSL_MAX_ALPN_NAME_LEN) ||
4704 (tot_len > MBEDTLS_SSL_MAX_ALPN_LIST_LEN)) {
4705 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
4706 }
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +02004707 }
4708
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02004709 conf->alpn_list = protos;
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +02004710
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004711 return 0;
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02004712}
4713
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004714const char *mbedtls_ssl_get_alpn_protocol(const mbedtls_ssl_context *ssl)
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02004715{
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004716 return ssl->alpn_chosen;
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02004717}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004718#endif /* MBEDTLS_SSL_ALPN */
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02004719
Johan Pascalb62bb512015-12-03 21:56:45 +01004720#if defined(MBEDTLS_SSL_DTLS_SRTP)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004721void mbedtls_ssl_conf_srtp_mki_value_supported(mbedtls_ssl_config *conf,
4722 int support_mki_value)
Ron Eldor591f1622018-01-22 12:30:04 +02004723{
4724 conf->dtls_srtp_mki_support = support_mki_value;
4725}
4726
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004727int mbedtls_ssl_dtls_srtp_set_mki_value(mbedtls_ssl_context *ssl,
4728 unsigned char *mki_value,
4729 uint16_t mki_len)
Ron Eldor591f1622018-01-22 12:30:04 +02004730{
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004731 if (mki_len > MBEDTLS_TLS_SRTP_MAX_MKI_LENGTH) {
4732 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
Ron Eldora9788042018-12-05 11:04:31 +02004733 }
Ron Eldor591f1622018-01-22 12:30:04 +02004734
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004735 if (ssl->conf->dtls_srtp_mki_support == MBEDTLS_SSL_DTLS_SRTP_MKI_UNSUPPORTED) {
4736 return MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
Ron Eldora9788042018-12-05 11:04:31 +02004737 }
Ron Eldor591f1622018-01-22 12:30:04 +02004738
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004739 memcpy(ssl->dtls_srtp_info.mki_value, mki_value, mki_len);
Ron Eldor591f1622018-01-22 12:30:04 +02004740 ssl->dtls_srtp_info.mki_len = mki_len;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004741 return 0;
Ron Eldor591f1622018-01-22 12:30:04 +02004742}
4743
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004744int mbedtls_ssl_conf_dtls_srtp_protection_profiles(mbedtls_ssl_config *conf,
4745 const mbedtls_ssl_srtp_profile *profiles)
Johan Pascalb62bb512015-12-03 21:56:45 +01004746{
Johan Pascal253d0262020-09-22 13:04:45 +02004747 const mbedtls_ssl_srtp_profile *p;
4748 size_t list_size = 0;
Johan Pascalb62bb512015-12-03 21:56:45 +01004749
Johan Pascal253d0262020-09-22 13:04:45 +02004750 /* check the profiles list: all entry must be valid,
4751 * its size cannot be more than the total number of supported profiles, currently 4 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004752 for (p = profiles; *p != MBEDTLS_TLS_SRTP_UNSET &&
4753 list_size <= MBEDTLS_TLS_SRTP_MAX_PROFILE_LIST_LENGTH;
4754 p++) {
4755 if (mbedtls_ssl_check_srtp_profile_value(*p) != MBEDTLS_TLS_SRTP_UNSET) {
Johan Pascal76fdf1d2020-10-22 23:31:00 +02004756 list_size++;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004757 } else {
Johan Pascal76fdf1d2020-10-22 23:31:00 +02004758 /* unsupported value, stop parsing and set the size to an error value */
4759 list_size = MBEDTLS_TLS_SRTP_MAX_PROFILE_LIST_LENGTH + 1;
Johan Pascalb62bb512015-12-03 21:56:45 +01004760 }
4761 }
4762
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004763 if (list_size > MBEDTLS_TLS_SRTP_MAX_PROFILE_LIST_LENGTH) {
4764 conf->dtls_srtp_profile_list = NULL;
4765 conf->dtls_srtp_profile_list_len = 0;
4766 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
Johan Pascal253d0262020-09-22 13:04:45 +02004767 }
4768
Johan Pascal9bc97ca2020-09-21 23:44:45 +02004769 conf->dtls_srtp_profile_list = profiles;
Johan Pascal253d0262020-09-22 13:04:45 +02004770 conf->dtls_srtp_profile_list_len = list_size;
Johan Pascalb62bb512015-12-03 21:56:45 +01004771
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004772 return 0;
Johan Pascalb62bb512015-12-03 21:56:45 +01004773}
4774
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004775void mbedtls_ssl_get_dtls_srtp_negotiation_result(const mbedtls_ssl_context *ssl,
4776 mbedtls_dtls_srtp_info *dtls_srtp_info)
Johan Pascalb62bb512015-12-03 21:56:45 +01004777{
Johan Pascal2258a4f2020-10-28 13:53:09 +01004778 dtls_srtp_info->chosen_dtls_srtp_profile = ssl->dtls_srtp_info.chosen_dtls_srtp_profile;
4779 /* do not copy the mki value if there is no chosen profile */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004780 if (dtls_srtp_info->chosen_dtls_srtp_profile == MBEDTLS_TLS_SRTP_UNSET) {
Johan Pascal2258a4f2020-10-28 13:53:09 +01004781 dtls_srtp_info->mki_len = 0;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004782 } else {
Johan Pascal2258a4f2020-10-28 13:53:09 +01004783 dtls_srtp_info->mki_len = ssl->dtls_srtp_info.mki_len;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004784 memcpy(dtls_srtp_info->mki_value, ssl->dtls_srtp_info.mki_value,
4785 ssl->dtls_srtp_info.mki_len);
Johan Pascal2258a4f2020-10-28 13:53:09 +01004786 }
Johan Pascalb62bb512015-12-03 21:56:45 +01004787}
Johan Pascalb62bb512015-12-03 21:56:45 +01004788#endif /* MBEDTLS_SSL_DTLS_SRTP */
4789
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004790void mbedtls_ssl_conf_max_version(mbedtls_ssl_config *conf, int major, int minor)
Paul Bakker490ecc82011-10-06 13:04:09 +00004791{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02004792 conf->max_major_ver = major;
4793 conf->max_minor_ver = minor;
Paul Bakker490ecc82011-10-06 13:04:09 +00004794}
4795
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004796void mbedtls_ssl_conf_min_version(mbedtls_ssl_config *conf, int major, int minor)
Paul Bakker1d29fb52012-09-28 13:28:45 +00004797{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02004798 conf->min_major_ver = major;
4799 conf->min_minor_ver = minor;
Paul Bakker1d29fb52012-09-28 13:28:45 +00004800}
4801
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004802#if defined(MBEDTLS_SSL_FALLBACK_SCSV) && defined(MBEDTLS_SSL_CLI_C)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004803void mbedtls_ssl_conf_fallback(mbedtls_ssl_config *conf, char fallback)
Manuel Pégourié-Gonnard1cbd39d2014-10-20 13:34:59 +02004804{
Manuel Pégourié-Gonnard684b0592015-05-06 09:27:31 +01004805 conf->fallback = fallback;
Manuel Pégourié-Gonnard1cbd39d2014-10-20 13:34:59 +02004806}
4807#endif
4808
Janos Follath088ce432017-04-10 12:42:31 +01004809#if defined(MBEDTLS_SSL_SRV_C)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004810void mbedtls_ssl_conf_cert_req_ca_list(mbedtls_ssl_config *conf,
4811 char cert_req_ca_list)
Janos Follath088ce432017-04-10 12:42:31 +01004812{
4813 conf->cert_req_ca_list = cert_req_ca_list;
4814}
4815#endif
4816
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004817#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004818void mbedtls_ssl_conf_encrypt_then_mac(mbedtls_ssl_config *conf, char etm)
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +01004819{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02004820 conf->encrypt_then_mac = etm;
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +01004821}
4822#endif
4823
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004824#if defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004825void mbedtls_ssl_conf_extended_master_secret(mbedtls_ssl_config *conf, char ems)
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +02004826{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02004827 conf->extended_ms = ems;
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +02004828}
4829#endif
4830
Manuel Pégourié-Gonnard66dc5552015-05-14 12:28:21 +02004831#if defined(MBEDTLS_ARC4_C)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004832void mbedtls_ssl_conf_arc4_support(mbedtls_ssl_config *conf, char arc4)
Manuel Pégourié-Gonnardbd47a582015-01-12 13:43:29 +01004833{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02004834 conf->arc4_disabled = arc4;
Manuel Pégourié-Gonnardbd47a582015-01-12 13:43:29 +01004835}
Manuel Pégourié-Gonnard66dc5552015-05-14 12:28:21 +02004836#endif
Manuel Pégourié-Gonnardbd47a582015-01-12 13:43:29 +01004837
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004838#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004839int mbedtls_ssl_conf_max_frag_len(mbedtls_ssl_config *conf, unsigned char mfl_code)
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02004840{
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004841 if (mfl_code >= MBEDTLS_SSL_MAX_FRAG_LEN_INVALID ||
4842 ssl_mfl_code_to_length(mfl_code) > MBEDTLS_TLS_EXT_ADV_CONTENT_LEN) {
4843 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02004844 }
4845
Manuel Pégourié-Gonnard6bf89d62015-05-05 17:01:57 +01004846 conf->mfl_code = mfl_code;
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02004847
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004848 return 0;
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02004849}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004850#endif /* MBEDTLS_SSL_MAX_FRAGMENT_LENGTH */
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02004851
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004852#if defined(MBEDTLS_SSL_TRUNCATED_HMAC)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004853void mbedtls_ssl_conf_truncated_hmac(mbedtls_ssl_config *conf, int truncate)
Manuel Pégourié-Gonnarde980a992013-07-19 11:08:52 +02004854{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02004855 conf->trunc_hmac = truncate;
Manuel Pégourié-Gonnarde980a992013-07-19 11:08:52 +02004856}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004857#endif /* MBEDTLS_SSL_TRUNCATED_HMAC */
Manuel Pégourié-Gonnarde980a992013-07-19 11:08:52 +02004858
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004859#if defined(MBEDTLS_SSL_CBC_RECORD_SPLITTING)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004860void mbedtls_ssl_conf_cbc_record_splitting(mbedtls_ssl_config *conf, char split)
Manuel Pégourié-Gonnardcfa477e2015-01-07 14:50:54 +01004861{
Manuel Pégourié-Gonnard17eab2b2015-05-05 16:34:53 +01004862 conf->cbc_record_splitting = split;
Manuel Pégourié-Gonnardcfa477e2015-01-07 14:50:54 +01004863}
4864#endif
4865
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004866void mbedtls_ssl_conf_legacy_renegotiation(mbedtls_ssl_config *conf, int allow_legacy)
Paul Bakker48916f92012-09-16 19:57:18 +00004867{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02004868 conf->allow_legacy_renegotiation = allow_legacy;
Paul Bakker48916f92012-09-16 19:57:18 +00004869}
4870
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004871#if defined(MBEDTLS_SSL_RENEGOTIATION)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004872void mbedtls_ssl_conf_renegotiation(mbedtls_ssl_config *conf, int renegotiation)
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01004873{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02004874 conf->disable_renegotiation = renegotiation;
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01004875}
4876
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004877void mbedtls_ssl_conf_renegotiation_enforced(mbedtls_ssl_config *conf, int max_records)
Manuel Pégourié-Gonnarda9964db2014-07-03 19:29:16 +02004878{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02004879 conf->renego_max_records = max_records;
Manuel Pégourié-Gonnarda9964db2014-07-03 19:29:16 +02004880}
4881
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004882void mbedtls_ssl_conf_renegotiation_period(mbedtls_ssl_config *conf,
4883 const unsigned char period[8])
Manuel Pégourié-Gonnard837f0fe2014-11-05 13:58:53 +01004884{
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004885 memcpy(conf->renego_period, period, 8);
Manuel Pégourié-Gonnard837f0fe2014-11-05 13:58:53 +01004886}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004887#endif /* MBEDTLS_SSL_RENEGOTIATION */
Paul Bakker5121ce52009-01-03 21:22:43 +00004888
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004889#if defined(MBEDTLS_SSL_SESSION_TICKETS)
Manuel Pégourié-Gonnardb596abf2015-05-20 10:45:29 +02004890#if defined(MBEDTLS_SSL_CLI_C)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004891void mbedtls_ssl_conf_session_tickets(mbedtls_ssl_config *conf, int use_tickets)
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +02004892{
Manuel Pégourié-Gonnard2b494452015-05-06 10:05:11 +01004893 conf->session_tickets = use_tickets;
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +02004894}
Manuel Pégourié-Gonnardb596abf2015-05-20 10:45:29 +02004895#endif
Paul Bakker606b4ba2013-08-14 16:52:14 +02004896
Manuel Pégourié-Gonnardb596abf2015-05-20 10:45:29 +02004897#if defined(MBEDTLS_SSL_SRV_C)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004898void mbedtls_ssl_conf_session_tickets_cb(mbedtls_ssl_config *conf,
4899 mbedtls_ssl_ticket_write_t *f_ticket_write,
4900 mbedtls_ssl_ticket_parse_t *f_ticket_parse,
4901 void *p_ticket)
Paul Bakker606b4ba2013-08-14 16:52:14 +02004902{
Manuel Pégourié-Gonnardd59675d2015-05-19 15:28:00 +02004903 conf->f_ticket_write = f_ticket_write;
4904 conf->f_ticket_parse = f_ticket_parse;
4905 conf->p_ticket = p_ticket;
Paul Bakker606b4ba2013-08-14 16:52:14 +02004906}
Manuel Pégourié-Gonnardb596abf2015-05-20 10:45:29 +02004907#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004908#endif /* MBEDTLS_SSL_SESSION_TICKETS */
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +02004909
Robert Cragie4feb7ae2015-10-02 13:33:37 +01004910#if defined(MBEDTLS_SSL_EXPORT_KEYS)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004911void mbedtls_ssl_conf_export_keys_cb(mbedtls_ssl_config *conf,
4912 mbedtls_ssl_export_keys_t *f_export_keys,
4913 void *p_export_keys)
Robert Cragie4feb7ae2015-10-02 13:33:37 +01004914{
4915 conf->f_export_keys = f_export_keys;
4916 conf->p_export_keys = p_export_keys;
4917}
Ron Eldorf5cc10d2019-05-07 18:33:40 +03004918
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004919void mbedtls_ssl_conf_export_keys_ext_cb(mbedtls_ssl_config *conf,
4920 mbedtls_ssl_export_keys_ext_t *f_export_keys_ext,
4921 void *p_export_keys)
Ron Eldorf5cc10d2019-05-07 18:33:40 +03004922{
4923 conf->f_export_keys_ext = f_export_keys_ext;
4924 conf->p_export_keys = p_export_keys;
4925}
Robert Cragie4feb7ae2015-10-02 13:33:37 +01004926#endif
4927
Gilles Peskineb74a1c72018-04-24 13:09:22 +02004928#if defined(MBEDTLS_SSL_ASYNC_PRIVATE)
Gilles Peskine8bf79f62018-01-05 21:11:53 +01004929void mbedtls_ssl_conf_async_private_cb(
4930 mbedtls_ssl_config *conf,
4931 mbedtls_ssl_async_sign_t *f_async_sign,
4932 mbedtls_ssl_async_decrypt_t *f_async_decrypt,
4933 mbedtls_ssl_async_resume_t *f_async_resume,
4934 mbedtls_ssl_async_cancel_t *f_async_cancel,
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004935 void *async_config_data)
Gilles Peskine8bf79f62018-01-05 21:11:53 +01004936{
4937 conf->f_async_sign_start = f_async_sign;
4938 conf->f_async_decrypt_start = f_async_decrypt;
4939 conf->f_async_resume = f_async_resume;
4940 conf->f_async_cancel = f_async_cancel;
Gilles Peskinedf13d5c2018-04-25 20:39:48 +02004941 conf->p_async_config_data = async_config_data;
4942}
4943
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004944void *mbedtls_ssl_conf_get_async_config_data(const mbedtls_ssl_config *conf)
Gilles Peskine8f97af72018-04-26 11:46:10 +02004945{
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004946 return conf->p_async_config_data;
Gilles Peskine8f97af72018-04-26 11:46:10 +02004947}
4948
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004949void *mbedtls_ssl_get_async_operation_data(const mbedtls_ssl_context *ssl)
Gilles Peskinedf13d5c2018-04-25 20:39:48 +02004950{
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004951 if (ssl->handshake == NULL) {
4952 return NULL;
4953 } else {
4954 return ssl->handshake->user_async_ctx;
4955 }
Gilles Peskinedf13d5c2018-04-25 20:39:48 +02004956}
4957
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004958void mbedtls_ssl_set_async_operation_data(mbedtls_ssl_context *ssl,
4959 void *ctx)
Gilles Peskinedf13d5c2018-04-25 20:39:48 +02004960{
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004961 if (ssl->handshake != NULL) {
Gilles Peskinedf13d5c2018-04-25 20:39:48 +02004962 ssl->handshake->user_async_ctx = ctx;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004963 }
Gilles Peskine8bf79f62018-01-05 21:11:53 +01004964}
Gilles Peskineb74a1c72018-04-24 13:09:22 +02004965#endif /* MBEDTLS_SSL_ASYNC_PRIVATE */
Gilles Peskine8bf79f62018-01-05 21:11:53 +01004966
Paul Bakker5121ce52009-01-03 21:22:43 +00004967/*
4968 * SSL get accessors
4969 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004970uint32_t mbedtls_ssl_get_verify_result(const mbedtls_ssl_context *ssl)
Paul Bakker5121ce52009-01-03 21:22:43 +00004971{
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004972 if (ssl->session != NULL) {
4973 return ssl->session->verify_result;
4974 }
Manuel Pégourié-Gonnarde89163c2015-01-23 14:30:57 +00004975
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004976 if (ssl->session_negotiate != NULL) {
4977 return ssl->session_negotiate->verify_result;
4978 }
Manuel Pégourié-Gonnarde89163c2015-01-23 14:30:57 +00004979
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004980 return 0xFFFFFFFF;
Paul Bakker5121ce52009-01-03 21:22:43 +00004981}
4982
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004983const char *mbedtls_ssl_get_ciphersuite(const mbedtls_ssl_context *ssl)
Paul Bakker72f62662011-01-16 21:27:44 +00004984{
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004985 if (ssl == NULL || ssl->session == NULL) {
4986 return NULL;
4987 }
Paul Bakker926c8e42013-03-06 10:23:34 +01004988
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004989 return mbedtls_ssl_get_ciphersuite_name(ssl->session->ciphersuite);
Paul Bakker72f62662011-01-16 21:27:44 +00004990}
4991
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004992const char *mbedtls_ssl_get_version(const mbedtls_ssl_context *ssl)
Paul Bakker43ca69c2011-01-15 17:35:19 +00004993{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004994#if defined(MBEDTLS_SSL_PROTO_DTLS)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004995 if (ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM) {
4996 switch (ssl->minor_ver) {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004997 case MBEDTLS_SSL_MINOR_VERSION_2:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004998 return "DTLSv1.0";
Manuel Pégourié-Gonnardb21ca2a2014-02-10 13:43:33 +01004999
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005000 case MBEDTLS_SSL_MINOR_VERSION_3:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005001 return "DTLSv1.2";
Manuel Pégourié-Gonnardb21ca2a2014-02-10 13:43:33 +01005002
5003 default:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005004 return "unknown (DTLS)";
Manuel Pégourié-Gonnardb21ca2a2014-02-10 13:43:33 +01005005 }
5006 }
5007#endif
5008
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005009 switch (ssl->minor_ver) {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005010 case MBEDTLS_SSL_MINOR_VERSION_0:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005011 return "SSLv3.0";
Paul Bakker43ca69c2011-01-15 17:35:19 +00005012
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005013 case MBEDTLS_SSL_MINOR_VERSION_1:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005014 return "TLSv1.0";
Paul Bakker43ca69c2011-01-15 17:35:19 +00005015
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005016 case MBEDTLS_SSL_MINOR_VERSION_2:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005017 return "TLSv1.1";
Paul Bakker43ca69c2011-01-15 17:35:19 +00005018
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005019 case MBEDTLS_SSL_MINOR_VERSION_3:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005020 return "TLSv1.2";
Paul Bakker1ef83d62012-04-11 12:09:53 +00005021
Paul Bakker43ca69c2011-01-15 17:35:19 +00005022 default:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005023 return "unknown";
Paul Bakker43ca69c2011-01-15 17:35:19 +00005024 }
Paul Bakker43ca69c2011-01-15 17:35:19 +00005025}
5026
Manuel Pégourié-Gonnarda2cda6b2015-08-31 18:30:52 +02005027#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005028size_t mbedtls_ssl_get_input_max_frag_len(const mbedtls_ssl_context *ssl)
Andrzej Kurek90c6e842020-04-03 05:25:29 -04005029{
5030 size_t max_len = MBEDTLS_SSL_MAX_CONTENT_LEN;
5031 size_t read_mfl;
5032
5033 /* Use the configured MFL for the client if we're past SERVER_HELLO_DONE */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005034 if (ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT &&
5035 ssl->state >= MBEDTLS_SSL_SERVER_HELLO_DONE) {
5036 return ssl_mfl_code_to_length(ssl->conf->mfl_code);
Andrzej Kurek90c6e842020-04-03 05:25:29 -04005037 }
5038
5039 /* Check if a smaller max length was negotiated */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005040 if (ssl->session_out != NULL) {
5041 read_mfl = ssl_mfl_code_to_length(ssl->session_out->mfl_code);
5042 if (read_mfl < max_len) {
Andrzej Kurek90c6e842020-04-03 05:25:29 -04005043 max_len = read_mfl;
5044 }
5045 }
5046
5047 // During a handshake, use the value being negotiated
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005048 if (ssl->session_negotiate != NULL) {
5049 read_mfl = ssl_mfl_code_to_length(ssl->session_negotiate->mfl_code);
5050 if (read_mfl < max_len) {
Andrzej Kurek90c6e842020-04-03 05:25:29 -04005051 max_len = read_mfl;
5052 }
5053 }
5054
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005055 return max_len;
Andrzej Kurek90c6e842020-04-03 05:25:29 -04005056}
5057
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005058size_t mbedtls_ssl_get_output_max_frag_len(const mbedtls_ssl_context *ssl)
Manuel Pégourié-Gonnarda2cda6b2015-08-31 18:30:52 +02005059{
5060 size_t max_len;
5061
5062 /*
5063 * Assume mfl_code is correct since it was checked when set
5064 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005065 max_len = ssl_mfl_code_to_length(ssl->conf->mfl_code);
Manuel Pégourié-Gonnarda2cda6b2015-08-31 18:30:52 +02005066
Manuel Pégourié-Gonnard2cb17e22017-09-19 13:00:47 +02005067 /* Check if a smaller max length was negotiated */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005068 if (ssl->session_out != NULL &&
5069 ssl_mfl_code_to_length(ssl->session_out->mfl_code) < max_len) {
5070 max_len = ssl_mfl_code_to_length(ssl->session_out->mfl_code);
Manuel Pégourié-Gonnarda2cda6b2015-08-31 18:30:52 +02005071 }
5072
Manuel Pégourié-Gonnard2cb17e22017-09-19 13:00:47 +02005073 /* During a handshake, use the value being negotiated */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005074 if (ssl->session_negotiate != NULL &&
5075 ssl_mfl_code_to_length(ssl->session_negotiate->mfl_code) < max_len) {
5076 max_len = ssl_mfl_code_to_length(ssl->session_negotiate->mfl_code);
Manuel Pégourié-Gonnard2cb17e22017-09-19 13:00:47 +02005077 }
5078
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005079 return max_len;
Manuel Pégourié-Gonnarda2cda6b2015-08-31 18:30:52 +02005080}
Andrzej Kurek90c6e842020-04-03 05:25:29 -04005081
5082#if !defined(MBEDTLS_DEPRECATED_REMOVED)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005083size_t mbedtls_ssl_get_max_frag_len(const mbedtls_ssl_context *ssl)
Andrzej Kurek90c6e842020-04-03 05:25:29 -04005084{
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005085 return mbedtls_ssl_get_output_max_frag_len(ssl);
Andrzej Kurek90c6e842020-04-03 05:25:29 -04005086}
5087#endif /* !MBEDTLS_DEPRECATED_REMOVED */
Manuel Pégourié-Gonnarda2cda6b2015-08-31 18:30:52 +02005088#endif /* MBEDTLS_SSL_MAX_FRAGMENT_LENGTH */
5089
Manuel Pégourié-Gonnardb8eec192018-08-20 09:34:02 +02005090#if defined(MBEDTLS_SSL_PROTO_DTLS)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005091size_t mbedtls_ssl_get_current_mtu(const mbedtls_ssl_context *ssl)
Manuel Pégourié-Gonnardb8eec192018-08-20 09:34:02 +02005092{
Andrzej Kurekef43ce62018-10-09 08:24:12 -04005093 /* Return unlimited mtu for client hello messages to avoid fragmentation. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005094 if (ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT &&
5095 (ssl->state == MBEDTLS_SSL_CLIENT_HELLO ||
5096 ssl->state == MBEDTLS_SSL_SERVER_HELLO)) {
5097 return 0;
5098 }
Andrzej Kurekef43ce62018-10-09 08:24:12 -04005099
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005100 if (ssl->handshake == NULL || ssl->handshake->mtu == 0) {
5101 return ssl->mtu;
5102 }
Manuel Pégourié-Gonnardb8eec192018-08-20 09:34:02 +02005103
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005104 if (ssl->mtu == 0) {
5105 return ssl->handshake->mtu;
5106 }
Manuel Pégourié-Gonnardb8eec192018-08-20 09:34:02 +02005107
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005108 return ssl->mtu < ssl->handshake->mtu ?
5109 ssl->mtu : ssl->handshake->mtu;
Manuel Pégourié-Gonnardb8eec192018-08-20 09:34:02 +02005110}
5111#endif /* MBEDTLS_SSL_PROTO_DTLS */
5112
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005113int mbedtls_ssl_get_max_out_record_payload(const mbedtls_ssl_context *ssl)
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02005114{
5115 size_t max_len = MBEDTLS_SSL_OUT_CONTENT_LEN;
5116
Manuel Pégourié-Gonnard000281e2018-08-21 11:20:58 +02005117#if !defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH) && \
5118 !defined(MBEDTLS_SSL_PROTO_DTLS)
5119 (void) ssl;
5120#endif
5121
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02005122#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005123 const size_t mfl = mbedtls_ssl_get_output_max_frag_len(ssl);
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02005124
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005125 if (max_len > mfl) {
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02005126 max_len = mfl;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005127 }
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02005128#endif
5129
5130#if defined(MBEDTLS_SSL_PROTO_DTLS)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005131 if (mbedtls_ssl_get_current_mtu(ssl) != 0) {
5132 const size_t mtu = mbedtls_ssl_get_current_mtu(ssl);
5133 const int ret = mbedtls_ssl_get_record_expansion(ssl);
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02005134 const size_t overhead = (size_t) ret;
5135
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005136 if (ret < 0) {
5137 return ret;
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02005138 }
5139
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005140 if (mtu <= overhead) {
5141 MBEDTLS_SSL_DEBUG_MSG(1, ("MTU too low for record expansion"));
5142 return MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
5143 }
5144
5145 if (max_len > mtu - overhead) {
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02005146 max_len = mtu - overhead;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005147 }
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02005148 }
Manuel Pégourié-Gonnardb8eec192018-08-20 09:34:02 +02005149#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02005150
Hanno Becker0defedb2018-08-10 12:35:02 +01005151#if !defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH) && \
5152 !defined(MBEDTLS_SSL_PROTO_DTLS)
5153 ((void) ssl);
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02005154#endif
5155
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005156 return (int) max_len;
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02005157}
5158
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005159#if defined(MBEDTLS_X509_CRT_PARSE_C)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005160const mbedtls_x509_crt *mbedtls_ssl_get_peer_cert(const mbedtls_ssl_context *ssl)
Paul Bakkerb0550d92012-10-30 07:51:03 +00005161{
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005162 if (ssl == NULL || ssl->session == NULL) {
5163 return NULL;
5164 }
Paul Bakkerb0550d92012-10-30 07:51:03 +00005165
Hanno Beckere6824572019-02-07 13:18:46 +00005166#if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005167 return ssl->session->peer_cert;
Hanno Beckere6824572019-02-07 13:18:46 +00005168#else
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005169 return NULL;
Hanno Beckere6824572019-02-07 13:18:46 +00005170#endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
Paul Bakkerb0550d92012-10-30 07:51:03 +00005171}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005172#endif /* MBEDTLS_X509_CRT_PARSE_C */
Paul Bakkerb0550d92012-10-30 07:51:03 +00005173
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005174#if defined(MBEDTLS_SSL_CLI_C)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005175int mbedtls_ssl_get_session(const mbedtls_ssl_context *ssl,
5176 mbedtls_ssl_session *dst)
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02005177{
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005178 if (ssl == NULL ||
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02005179 dst == NULL ||
5180 ssl->session == NULL ||
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005181 ssl->conf->endpoint != MBEDTLS_SSL_IS_CLIENT) {
5182 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02005183 }
5184
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005185 return mbedtls_ssl_session_copy(dst, ssl->session);
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02005186}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005187#endif /* MBEDTLS_SSL_CLI_C */
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02005188
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005189const mbedtls_ssl_session *mbedtls_ssl_get_session_pointer(const mbedtls_ssl_context *ssl)
Manuel Pégourié-Gonnardb5e4e0a2019-05-20 11:12:28 +02005190{
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005191 if (ssl == NULL) {
5192 return NULL;
5193 }
Manuel Pégourié-Gonnardb5e4e0a2019-05-20 11:12:28 +02005194
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005195 return ssl->session;
Manuel Pégourié-Gonnardb5e4e0a2019-05-20 11:12:28 +02005196}
5197
Paul Bakker5121ce52009-01-03 21:22:43 +00005198/*
Hanno Beckera835da52019-05-16 12:39:07 +01005199 * Define ticket header determining Mbed TLS version
5200 * and structure of the ticket.
5201 */
5202
Hanno Becker94ef3b32019-05-16 12:50:45 +01005203/*
Hanno Becker50b59662019-05-28 14:30:45 +01005204 * Define bitflag determining compile-time settings influencing
5205 * structure of serialized SSL sessions.
Hanno Becker94ef3b32019-05-16 12:50:45 +01005206 */
5207
Hanno Becker50b59662019-05-28 14:30:45 +01005208#if defined(MBEDTLS_HAVE_TIME)
Hanno Becker3e088662019-05-29 11:10:18 +01005209#define SSL_SERIALIZED_SESSION_CONFIG_TIME 1
Hanno Becker50b59662019-05-28 14:30:45 +01005210#else
Hanno Becker3e088662019-05-29 11:10:18 +01005211#define SSL_SERIALIZED_SESSION_CONFIG_TIME 0
Hanno Becker94ef3b32019-05-16 12:50:45 +01005212#endif /* MBEDTLS_HAVE_TIME */
5213
5214#if defined(MBEDTLS_X509_CRT_PARSE_C)
Hanno Becker3e088662019-05-29 11:10:18 +01005215#define SSL_SERIALIZED_SESSION_CONFIG_CRT 1
Hanno Becker94ef3b32019-05-16 12:50:45 +01005216#else
Hanno Becker3e088662019-05-29 11:10:18 +01005217#define SSL_SERIALIZED_SESSION_CONFIG_CRT 0
Hanno Becker94ef3b32019-05-16 12:50:45 +01005218#endif /* MBEDTLS_X509_CRT_PARSE_C */
5219
5220#if defined(MBEDTLS_SSL_CLI_C) && defined(MBEDTLS_SSL_SESSION_TICKETS)
Hanno Becker3e088662019-05-29 11:10:18 +01005221#define SSL_SERIALIZED_SESSION_CONFIG_CLIENT_TICKET 1
Hanno Becker94ef3b32019-05-16 12:50:45 +01005222#else
Hanno Becker3e088662019-05-29 11:10:18 +01005223#define SSL_SERIALIZED_SESSION_CONFIG_CLIENT_TICKET 0
Hanno Becker94ef3b32019-05-16 12:50:45 +01005224#endif /* MBEDTLS_SSL_CLI_C && MBEDTLS_SSL_SESSION_TICKETS */
5225
5226#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
Hanno Becker3e088662019-05-29 11:10:18 +01005227#define SSL_SERIALIZED_SESSION_CONFIG_MFL 1
Hanno Becker94ef3b32019-05-16 12:50:45 +01005228#else
Hanno Becker3e088662019-05-29 11:10:18 +01005229#define SSL_SERIALIZED_SESSION_CONFIG_MFL 0
Hanno Becker94ef3b32019-05-16 12:50:45 +01005230#endif /* MBEDTLS_SSL_MAX_FRAGMENT_LENGTH */
5231
5232#if defined(MBEDTLS_SSL_TRUNCATED_HMAC)
Hanno Becker3e088662019-05-29 11:10:18 +01005233#define SSL_SERIALIZED_SESSION_CONFIG_TRUNC_HMAC 1
Hanno Becker94ef3b32019-05-16 12:50:45 +01005234#else
Hanno Becker3e088662019-05-29 11:10:18 +01005235#define SSL_SERIALIZED_SESSION_CONFIG_TRUNC_HMAC 0
Hanno Becker94ef3b32019-05-16 12:50:45 +01005236#endif /* MBEDTLS_SSL_TRUNCATED_HMAC */
5237
5238#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
Hanno Becker3e088662019-05-29 11:10:18 +01005239#define SSL_SERIALIZED_SESSION_CONFIG_ETM 1
Hanno Becker94ef3b32019-05-16 12:50:45 +01005240#else
Hanno Becker3e088662019-05-29 11:10:18 +01005241#define SSL_SERIALIZED_SESSION_CONFIG_ETM 0
Hanno Becker94ef3b32019-05-16 12:50:45 +01005242#endif /* MBEDTLS_SSL_ENCRYPT_THEN_MAC */
5243
Hanno Becker94ef3b32019-05-16 12:50:45 +01005244#if defined(MBEDTLS_SSL_SESSION_TICKETS)
5245#define SSL_SERIALIZED_SESSION_CONFIG_TICKET 1
5246#else
5247#define SSL_SERIALIZED_SESSION_CONFIG_TICKET 0
5248#endif /* MBEDTLS_SSL_SESSION_TICKETS */
5249
Hanno Becker3e088662019-05-29 11:10:18 +01005250#define SSL_SERIALIZED_SESSION_CONFIG_TIME_BIT 0
5251#define SSL_SERIALIZED_SESSION_CONFIG_CRT_BIT 1
5252#define SSL_SERIALIZED_SESSION_CONFIG_CLIENT_TICKET_BIT 2
5253#define SSL_SERIALIZED_SESSION_CONFIG_MFL_BIT 3
5254#define SSL_SERIALIZED_SESSION_CONFIG_TRUNC_HMAC_BIT 4
5255#define SSL_SERIALIZED_SESSION_CONFIG_ETM_BIT 5
5256#define SSL_SERIALIZED_SESSION_CONFIG_TICKET_BIT 6
Hanno Becker3e088662019-05-29 11:10:18 +01005257
Hanno Becker50b59662019-05-28 14:30:45 +01005258#define SSL_SERIALIZED_SESSION_CONFIG_BITFLAG \
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005259 ((uint16_t) ( \
5260 (SSL_SERIALIZED_SESSION_CONFIG_TIME << SSL_SERIALIZED_SESSION_CONFIG_TIME_BIT) | \
5261 (SSL_SERIALIZED_SESSION_CONFIG_CRT << SSL_SERIALIZED_SESSION_CONFIG_CRT_BIT) | \
5262 (SSL_SERIALIZED_SESSION_CONFIG_CLIENT_TICKET << \
5263 SSL_SERIALIZED_SESSION_CONFIG_CLIENT_TICKET_BIT) | \
5264 (SSL_SERIALIZED_SESSION_CONFIG_MFL << SSL_SERIALIZED_SESSION_CONFIG_MFL_BIT) | \
5265 (SSL_SERIALIZED_SESSION_CONFIG_TRUNC_HMAC << \
5266 SSL_SERIALIZED_SESSION_CONFIG_TRUNC_HMAC_BIT) | \
5267 (SSL_SERIALIZED_SESSION_CONFIG_ETM << SSL_SERIALIZED_SESSION_CONFIG_ETM_BIT) | \
5268 (SSL_SERIALIZED_SESSION_CONFIG_TICKET << SSL_SERIALIZED_SESSION_CONFIG_TICKET_BIT)))
Hanno Becker94ef3b32019-05-16 12:50:45 +01005269
Hanno Beckerf8787072019-05-16 12:41:07 +01005270static unsigned char ssl_serialized_session_header[] = {
Hanno Becker94ef3b32019-05-16 12:50:45 +01005271 MBEDTLS_VERSION_MAJOR,
5272 MBEDTLS_VERSION_MINOR,
5273 MBEDTLS_VERSION_PATCH,
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005274 MBEDTLS_BYTE_1(SSL_SERIALIZED_SESSION_CONFIG_BITFLAG),
5275 MBEDTLS_BYTE_0(SSL_SERIALIZED_SESSION_CONFIG_BITFLAG),
Hanno Beckerf8787072019-05-16 12:41:07 +01005276};
Hanno Beckera835da52019-05-16 12:39:07 +01005277
5278/*
Manuel Pégourié-Gonnarda3e7c652019-05-16 10:08:35 +02005279 * Serialize a session in the following format:
Manuel Pégourié-Gonnard35eb8022019-05-16 11:11:08 +02005280 * (in the presentation language of TLS, RFC 8446 section 3)
Manuel Pégourié-Gonnarda3e7c652019-05-16 10:08:35 +02005281 *
Hanno Becker50b59662019-05-28 14:30:45 +01005282 * opaque mbedtls_version[3]; // major, minor, patch
5283 * opaque session_format[2]; // version-specific 16-bit field determining
5284 * // the format of the remaining
5285 * // serialized data.
Hanno Beckerdc28b6c2019-05-29 11:08:00 +01005286 *
5287 * Note: When updating the format, remember to keep
5288 * these version+format bytes.
5289 *
Hanno Beckerbe34e8e2019-06-04 09:43:16 +01005290 * // In this version, `session_format` determines
5291 * // the setting of those compile-time
5292 * // configuration options which influence
Hanno Becker50b59662019-05-28 14:30:45 +01005293 * // the structure of mbedtls_ssl_session.
Manuel Pégourié-Gonnardf743c032019-05-24 12:06:29 +02005294 * uint64 start_time;
Hanno Becker50b59662019-05-28 14:30:45 +01005295 * uint8 ciphersuite[2]; // defined by the standard
5296 * uint8 compression; // 0 or 1
5297 * uint8 session_id_len; // at most 32
Manuel Pégourié-Gonnardf743c032019-05-24 12:06:29 +02005298 * opaque session_id[32];
Hanno Becker50b59662019-05-28 14:30:45 +01005299 * opaque master[48]; // fixed length in the standard
Manuel Pégourié-Gonnardf743c032019-05-24 12:06:29 +02005300 * uint32 verify_result;
Hanno Becker50b59662019-05-28 14:30:45 +01005301 * opaque peer_cert<0..2^24-1>; // length 0 means no peer cert
5302 * opaque ticket<0..2^24-1>; // length 0 means no ticket
Manuel Pégourié-Gonnardf743c032019-05-24 12:06:29 +02005303 * uint32 ticket_lifetime;
Hanno Becker50b59662019-05-28 14:30:45 +01005304 * uint8 mfl_code; // up to 255 according to standard
5305 * uint8 trunc_hmac; // 0 or 1
5306 * uint8 encrypt_then_mac; // 0 or 1
Manuel Pégourié-Gonnarda3e7c652019-05-16 10:08:35 +02005307 *
Manuel Pégourié-Gonnardf743c032019-05-24 12:06:29 +02005308 * The order is the same as in the definition of the structure, except
5309 * verify_result is put before peer_cert so that all mandatory fields come
5310 * together in one block.
Manuel Pégourié-Gonnarda3e7c652019-05-16 10:08:35 +02005311 */
Manuel Pégourié-Gonnardd904d662022-06-17 10:24:00 +02005312MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005313static int ssl_session_save(const mbedtls_ssl_session *session,
5314 unsigned char omit_header,
5315 unsigned char *buf,
5316 size_t buf_len,
5317 size_t *olen)
Manuel Pégourié-Gonnarda3e7c652019-05-16 10:08:35 +02005318{
5319 unsigned char *p = buf;
Manuel Pégourié-Gonnard26f982f2019-05-21 11:01:32 +02005320 size_t used = 0;
Manuel Pégourié-Gonnardf743c032019-05-24 12:06:29 +02005321#if defined(MBEDTLS_HAVE_TIME)
5322 uint64_t start;
5323#endif
Manuel Pégourié-Gonnarda3e7c652019-05-16 10:08:35 +02005324#if defined(MBEDTLS_X509_CRT_PARSE_C)
5325#if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
5326 size_t cert_len;
Manuel Pégourié-Gonnarda3e7c652019-05-16 10:08:35 +02005327#endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
5328#endif /* MBEDTLS_X509_CRT_PARSE_C */
5329
Manuel Pégourié-Gonnarda3e7c652019-05-16 10:08:35 +02005330
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005331 if (!omit_header) {
Manuel Pégourié-Gonnard45ac1f02019-07-23 16:52:45 +02005332 /*
5333 * Add version identifier
5334 */
5335
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005336 used += sizeof(ssl_serialized_session_header);
Manuel Pégourié-Gonnard45ac1f02019-07-23 16:52:45 +02005337
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005338 if (used <= buf_len) {
5339 memcpy(p, ssl_serialized_session_header,
5340 sizeof(ssl_serialized_session_header));
5341 p += sizeof(ssl_serialized_session_header);
Manuel Pégourié-Gonnard45ac1f02019-07-23 16:52:45 +02005342 }
Hanno Beckera835da52019-05-16 12:39:07 +01005343 }
5344
5345 /*
Manuel Pégourié-Gonnardf743c032019-05-24 12:06:29 +02005346 * Time
5347 */
5348#if defined(MBEDTLS_HAVE_TIME)
5349 used += 8;
5350
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005351 if (used <= buf_len) {
Manuel Pégourié-Gonnardf743c032019-05-24 12:06:29 +02005352 start = (uint64_t) session->start;
5353
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005354 MBEDTLS_PUT_UINT64_BE(start, p, 0);
Joe Subbiani2f98d792021-08-20 11:44:44 +01005355 p += 8;
Manuel Pégourié-Gonnardf743c032019-05-24 12:06:29 +02005356 }
5357#endif /* MBEDTLS_HAVE_TIME */
5358
5359 /*
5360 * Basic mandatory fields
5361 */
5362 used += 2 /* ciphersuite */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005363 + 1 /* compression */
5364 + 1 /* id_len */
5365 + sizeof(session->id)
5366 + sizeof(session->master)
5367 + 4; /* verify_result */
Manuel Pégourié-Gonnardf743c032019-05-24 12:06:29 +02005368
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005369 if (used <= buf_len) {
5370 MBEDTLS_PUT_UINT16_BE(session->ciphersuite, p, 0);
Joe Subbiani2f98d792021-08-20 11:44:44 +01005371 p += 2;
Manuel Pégourié-Gonnardf743c032019-05-24 12:06:29 +02005372
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005373 *p++ = MBEDTLS_BYTE_0(session->compression);
Manuel Pégourié-Gonnardf743c032019-05-24 12:06:29 +02005374
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005375 *p++ = MBEDTLS_BYTE_0(session->id_len);
5376 memcpy(p, session->id, 32);
Manuel Pégourié-Gonnardf743c032019-05-24 12:06:29 +02005377 p += 32;
5378
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005379 memcpy(p, session->master, 48);
Manuel Pégourié-Gonnardf743c032019-05-24 12:06:29 +02005380 p += 48;
5381
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005382 MBEDTLS_PUT_UINT32_BE(session->verify_result, p, 0);
Joe Subbiani2f98d792021-08-20 11:44:44 +01005383 p += 4;
Manuel Pégourié-Gonnard26f982f2019-05-21 11:01:32 +02005384 }
Manuel Pégourié-Gonnarda3e7c652019-05-16 10:08:35 +02005385
Manuel Pégourié-Gonnard35eb8022019-05-16 11:11:08 +02005386 /*
Manuel Pégourié-Gonnardf743c032019-05-24 12:06:29 +02005387 * Peer's end-entity certificate
Manuel Pégourié-Gonnard35eb8022019-05-16 11:11:08 +02005388 */
Manuel Pégourié-Gonnarda3e7c652019-05-16 10:08:35 +02005389#if defined(MBEDTLS_X509_CRT_PARSE_C)
5390#if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005391 if (session->peer_cert == NULL) {
Manuel Pégourié-Gonnarda3e7c652019-05-16 10:08:35 +02005392 cert_len = 0;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005393 } else {
Manuel Pégourié-Gonnarda3e7c652019-05-16 10:08:35 +02005394 cert_len = session->peer_cert->raw.len;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005395 }
Manuel Pégourié-Gonnarda3e7c652019-05-16 10:08:35 +02005396
Manuel Pégourié-Gonnard26f982f2019-05-21 11:01:32 +02005397 used += 3 + cert_len;
Manuel Pégourié-Gonnarda3e7c652019-05-16 10:08:35 +02005398
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005399 if (used <= buf_len) {
5400 *p++ = MBEDTLS_BYTE_2(cert_len);
5401 *p++ = MBEDTLS_BYTE_1(cert_len);
5402 *p++ = MBEDTLS_BYTE_0(cert_len);
Manuel Pégourié-Gonnarda3e7c652019-05-16 10:08:35 +02005403
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005404 if (session->peer_cert != NULL) {
5405 memcpy(p, session->peer_cert->raw.p, cert_len);
Manuel Pégourié-Gonnard26f982f2019-05-21 11:01:32 +02005406 p += cert_len;
5407 }
5408 }
Manuel Pégourié-Gonnarda3e7c652019-05-16 10:08:35 +02005409#else /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005410 if (session->peer_cert_digest != NULL) {
Manuel Pégourié-Gonnardf743c032019-05-24 12:06:29 +02005411 used += 1 /* type */ + 1 /* length */ + session->peer_cert_digest_len;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005412 if (used <= buf_len) {
Manuel Pégourié-Gonnardf743c032019-05-24 12:06:29 +02005413 *p++ = (unsigned char) session->peer_cert_digest_type;
5414 *p++ = (unsigned char) session->peer_cert_digest_len;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005415 memcpy(p, session->peer_cert_digest,
5416 session->peer_cert_digest_len);
Manuel Pégourié-Gonnardf743c032019-05-24 12:06:29 +02005417 p += session->peer_cert_digest_len;
5418 }
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005419 } else {
Manuel Pégourié-Gonnardf743c032019-05-24 12:06:29 +02005420 used += 2;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005421 if (used <= buf_len) {
Manuel Pégourié-Gonnardf743c032019-05-24 12:06:29 +02005422 *p++ = (unsigned char) MBEDTLS_MD_NONE;
5423 *p++ = 0;
5424 }
Manuel Pégourié-Gonnard26f982f2019-05-21 11:01:32 +02005425 }
Manuel Pégourié-Gonnarda3e7c652019-05-16 10:08:35 +02005426#endif /* !MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
5427#endif /* MBEDTLS_X509_CRT_PARSE_C */
5428
Manuel Pégourié-Gonnard35eb8022019-05-16 11:11:08 +02005429 /*
Manuel Pégourié-Gonnardf743c032019-05-24 12:06:29 +02005430 * Session ticket if any, plus associated data
Manuel Pégourié-Gonnard35eb8022019-05-16 11:11:08 +02005431 */
5432#if defined(MBEDTLS_SSL_SESSION_TICKETS) && defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnardf743c032019-05-24 12:06:29 +02005433 used += 3 + session->ticket_len + 4; /* len + ticket + lifetime */
Manuel Pégourié-Gonnard35eb8022019-05-16 11:11:08 +02005434
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005435 if (used <= buf_len) {
5436 *p++ = MBEDTLS_BYTE_2(session->ticket_len);
5437 *p++ = MBEDTLS_BYTE_1(session->ticket_len);
5438 *p++ = MBEDTLS_BYTE_0(session->ticket_len);
Manuel Pégourié-Gonnard26f982f2019-05-21 11:01:32 +02005439
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005440 if (session->ticket != NULL) {
5441 memcpy(p, session->ticket, session->ticket_len);
Manuel Pégourié-Gonnard26f982f2019-05-21 11:01:32 +02005442 p += session->ticket_len;
5443 }
Manuel Pégourié-Gonnardf743c032019-05-24 12:06:29 +02005444
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005445 MBEDTLS_PUT_UINT32_BE(session->ticket_lifetime, p, 0);
Joe Subbiani2f98d792021-08-20 11:44:44 +01005446 p += 4;
Manuel Pégourié-Gonnard35eb8022019-05-16 11:11:08 +02005447 }
5448#endif /* MBEDTLS_SSL_SESSION_TICKETS && MBEDTLS_SSL_CLI_C */
5449
Manuel Pégourié-Gonnardf743c032019-05-24 12:06:29 +02005450 /*
5451 * Misc extension-related info
5452 */
5453#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
5454 used += 1;
5455
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005456 if (used <= buf_len) {
Manuel Pégourié-Gonnardf743c032019-05-24 12:06:29 +02005457 *p++ = session->mfl_code;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005458 }
Manuel Pégourié-Gonnardf743c032019-05-24 12:06:29 +02005459#endif
5460
5461#if defined(MBEDTLS_SSL_TRUNCATED_HMAC)
5462 used += 1;
5463
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005464 if (used <= buf_len) {
5465 *p++ = (unsigned char) ((session->trunc_hmac) & 0xFF);
5466 }
Manuel Pégourié-Gonnardf743c032019-05-24 12:06:29 +02005467#endif
5468
5469#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
5470 used += 1;
5471
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005472 if (used <= buf_len) {
5473 *p++ = MBEDTLS_BYTE_0(session->encrypt_then_mac);
5474 }
Manuel Pégourié-Gonnardf743c032019-05-24 12:06:29 +02005475#endif
5476
Manuel Pégourié-Gonnard35eb8022019-05-16 11:11:08 +02005477 /* Done */
Manuel Pégourié-Gonnard26f982f2019-05-21 11:01:32 +02005478 *olen = used;
5479
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005480 if (used > buf_len) {
5481 return MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL;
5482 }
Manuel Pégourié-Gonnarda3e7c652019-05-16 10:08:35 +02005483
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005484 return 0;
Manuel Pégourié-Gonnarda3e7c652019-05-16 10:08:35 +02005485}
5486
5487/*
Manuel Pégourié-Gonnard45ac1f02019-07-23 16:52:45 +02005488 * Public wrapper for ssl_session_save()
5489 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005490int mbedtls_ssl_session_save(const mbedtls_ssl_session *session,
5491 unsigned char *buf,
5492 size_t buf_len,
5493 size_t *olen)
Manuel Pégourié-Gonnard45ac1f02019-07-23 16:52:45 +02005494{
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005495 return ssl_session_save(session, 0, buf, buf_len, olen);
Manuel Pégourié-Gonnard45ac1f02019-07-23 16:52:45 +02005496}
5497
5498/*
Manuel Pégourié-Gonnardb9dfc9f2019-07-12 10:50:19 +02005499 * Deserialize session, see mbedtls_ssl_session_save() for format.
Manuel Pégourié-Gonnarda3d831b2019-05-23 12:28:45 +02005500 *
5501 * This internal version is wrapped by a public function that cleans up in
Manuel Pégourié-Gonnard45ac1f02019-07-23 16:52:45 +02005502 * case of error, and has an extra option omit_header.
Manuel Pégourié-Gonnarda3e7c652019-05-16 10:08:35 +02005503 */
Manuel Pégourié-Gonnardd904d662022-06-17 10:24:00 +02005504MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005505static int ssl_session_load(mbedtls_ssl_session *session,
5506 unsigned char omit_header,
5507 const unsigned char *buf,
5508 size_t len)
Manuel Pégourié-Gonnarda3e7c652019-05-16 10:08:35 +02005509{
5510 const unsigned char *p = buf;
5511 const unsigned char * const end = buf + len;
Manuel Pégourié-Gonnardf743c032019-05-24 12:06:29 +02005512#if defined(MBEDTLS_HAVE_TIME)
5513 uint64_t start;
5514#endif
Manuel Pégourié-Gonnarda3e7c652019-05-16 10:08:35 +02005515#if defined(MBEDTLS_X509_CRT_PARSE_C)
5516#if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
5517 size_t cert_len;
Manuel Pégourié-Gonnarda3e7c652019-05-16 10:08:35 +02005518#endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
5519#endif /* MBEDTLS_X509_CRT_PARSE_C */
5520
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005521 if (!omit_header) {
Manuel Pégourié-Gonnard45ac1f02019-07-23 16:52:45 +02005522 /*
5523 * Check version identifier
5524 */
5525
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005526 if ((size_t) (end - p) < sizeof(ssl_serialized_session_header)) {
5527 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
Manuel Pégourié-Gonnard45ac1f02019-07-23 16:52:45 +02005528 }
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005529
5530 if (memcmp(p, ssl_serialized_session_header,
5531 sizeof(ssl_serialized_session_header)) != 0) {
5532 return MBEDTLS_ERR_SSL_VERSION_MISMATCH;
5533 }
5534 p += sizeof(ssl_serialized_session_header);
Hanno Beckera835da52019-05-16 12:39:07 +01005535 }
Hanno Beckera835da52019-05-16 12:39:07 +01005536
5537 /*
Manuel Pégourié-Gonnardf743c032019-05-24 12:06:29 +02005538 * Time
Manuel Pégourié-Gonnard35eb8022019-05-16 11:11:08 +02005539 */
Manuel Pégourié-Gonnardf743c032019-05-24 12:06:29 +02005540#if defined(MBEDTLS_HAVE_TIME)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005541 if (8 > (size_t) (end - p)) {
5542 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
5543 }
Manuel Pégourié-Gonnarda3e7c652019-05-16 10:08:35 +02005544
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005545 start = ((uint64_t) p[0] << 56) |
5546 ((uint64_t) p[1] << 48) |
5547 ((uint64_t) p[2] << 40) |
5548 ((uint64_t) p[3] << 32) |
5549 ((uint64_t) p[4] << 24) |
5550 ((uint64_t) p[5] << 16) |
5551 ((uint64_t) p[6] << 8) |
5552 ((uint64_t) p[7]);
Manuel Pégourié-Gonnardf743c032019-05-24 12:06:29 +02005553 p += 8;
Manuel Pégourié-Gonnarda3e7c652019-05-16 10:08:35 +02005554
Manuel Pégourié-Gonnardf743c032019-05-24 12:06:29 +02005555 session->start = (time_t) start;
5556#endif /* MBEDTLS_HAVE_TIME */
5557
5558 /*
5559 * Basic mandatory fields
5560 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005561 if (2 + 1 + 1 + 32 + 48 + 4 > (size_t) (end - p)) {
5562 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
5563 }
Manuel Pégourié-Gonnardf743c032019-05-24 12:06:29 +02005564
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005565 session->ciphersuite = (p[0] << 8) | p[1];
Manuel Pégourié-Gonnardf743c032019-05-24 12:06:29 +02005566 p += 2;
5567
5568 session->compression = *p++;
5569
5570 session->id_len = *p++;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005571 memcpy(session->id, p, 32);
Manuel Pégourié-Gonnardf743c032019-05-24 12:06:29 +02005572 p += 32;
5573
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005574 memcpy(session->master, p, 48);
Manuel Pégourié-Gonnardf743c032019-05-24 12:06:29 +02005575 p += 48;
5576
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005577 session->verify_result = ((uint32_t) p[0] << 24) |
5578 ((uint32_t) p[1] << 16) |
5579 ((uint32_t) p[2] << 8) |
5580 ((uint32_t) p[3]);
Manuel Pégourié-Gonnardf743c032019-05-24 12:06:29 +02005581 p += 4;
5582
5583 /* Immediately clear invalid pointer values that have been read, in case
5584 * we exit early before we replaced them with valid ones. */
Manuel Pégourié-Gonnarda3e7c652019-05-16 10:08:35 +02005585#if defined(MBEDTLS_X509_CRT_PARSE_C)
5586#if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
5587 session->peer_cert = NULL;
5588#else
5589 session->peer_cert_digest = NULL;
5590#endif /* !MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
5591#endif /* MBEDTLS_X509_CRT_PARSE_C */
5592#if defined(MBEDTLS_SSL_SESSION_TICKETS) && defined(MBEDTLS_SSL_CLI_C)
5593 session->ticket = NULL;
5594#endif /* MBEDTLS_SSL_SESSION_TICKETS && MBEDTLS_SSL_CLI_C */
5595
Manuel Pégourié-Gonnard35eb8022019-05-16 11:11:08 +02005596 /*
5597 * Peer certificate
5598 */
Manuel Pégourié-Gonnarda3e7c652019-05-16 10:08:35 +02005599#if defined(MBEDTLS_X509_CRT_PARSE_C)
5600#if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
5601 /* Deserialize CRT from the end of the ticket. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005602 if (3 > (size_t) (end - p)) {
5603 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
5604 }
Manuel Pégourié-Gonnarda3e7c652019-05-16 10:08:35 +02005605
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005606 cert_len = (p[0] << 16) | (p[1] << 8) | p[2];
Manuel Pégourié-Gonnarda3e7c652019-05-16 10:08:35 +02005607 p += 3;
5608
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005609 if (cert_len != 0) {
Janos Follath865b3eb2019-12-16 11:46:15 +00005610 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnarda3e7c652019-05-16 10:08:35 +02005611
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005612 if (cert_len > (size_t) (end - p)) {
5613 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
5614 }
Manuel Pégourié-Gonnarda3e7c652019-05-16 10:08:35 +02005615
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005616 session->peer_cert = mbedtls_calloc(1, sizeof(mbedtls_x509_crt));
Manuel Pégourié-Gonnarda3e7c652019-05-16 10:08:35 +02005617
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005618 if (session->peer_cert == NULL) {
5619 return MBEDTLS_ERR_SSL_ALLOC_FAILED;
5620 }
Manuel Pégourié-Gonnarda3e7c652019-05-16 10:08:35 +02005621
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005622 mbedtls_x509_crt_init(session->peer_cert);
Manuel Pégourié-Gonnarda3e7c652019-05-16 10:08:35 +02005623
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005624 if ((ret = mbedtls_x509_crt_parse_der(session->peer_cert,
5625 p, cert_len)) != 0) {
5626 mbedtls_x509_crt_free(session->peer_cert);
5627 mbedtls_free(session->peer_cert);
Manuel Pégourié-Gonnarda3e7c652019-05-16 10:08:35 +02005628 session->peer_cert = NULL;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005629 return ret;
Manuel Pégourié-Gonnarda3e7c652019-05-16 10:08:35 +02005630 }
5631
5632 p += cert_len;
5633 }
5634#else /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
5635 /* Deserialize CRT digest from the end of the ticket. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005636 if (2 > (size_t) (end - p)) {
5637 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
5638 }
Manuel Pégourié-Gonnarda3e7c652019-05-16 10:08:35 +02005639
Manuel Pégourié-Gonnardf743c032019-05-24 12:06:29 +02005640 session->peer_cert_digest_type = (mbedtls_md_type_t) *p++;
5641 session->peer_cert_digest_len = (size_t) *p++;
Manuel Pégourié-Gonnarda3e7c652019-05-16 10:08:35 +02005642
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005643 if (session->peer_cert_digest_len != 0) {
Manuel Pégourié-Gonnardf743c032019-05-24 12:06:29 +02005644 const mbedtls_md_info_t *md_info =
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005645 mbedtls_md_info_from_type(session->peer_cert_digest_type);
5646 if (md_info == NULL) {
5647 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
5648 }
5649 if (session->peer_cert_digest_len != mbedtls_md_get_size(md_info)) {
5650 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
5651 }
Manuel Pégourié-Gonnarda3e7c652019-05-16 10:08:35 +02005652
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005653 if (session->peer_cert_digest_len > (size_t) (end - p)) {
5654 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
5655 }
Manuel Pégourié-Gonnardf743c032019-05-24 12:06:29 +02005656
5657 session->peer_cert_digest =
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005658 mbedtls_calloc(1, session->peer_cert_digest_len);
5659 if (session->peer_cert_digest == NULL) {
5660 return MBEDTLS_ERR_SSL_ALLOC_FAILED;
5661 }
Manuel Pégourié-Gonnarda3e7c652019-05-16 10:08:35 +02005662
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005663 memcpy(session->peer_cert_digest, p,
5664 session->peer_cert_digest_len);
Manuel Pégourié-Gonnardf743c032019-05-24 12:06:29 +02005665 p += session->peer_cert_digest_len;
Manuel Pégourié-Gonnarda3e7c652019-05-16 10:08:35 +02005666 }
5667#endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
5668#endif /* MBEDTLS_X509_CRT_PARSE_C */
5669
Manuel Pégourié-Gonnard35eb8022019-05-16 11:11:08 +02005670 /*
Manuel Pégourié-Gonnardf743c032019-05-24 12:06:29 +02005671 * Session ticket and associated data
Manuel Pégourié-Gonnard35eb8022019-05-16 11:11:08 +02005672 */
5673#if defined(MBEDTLS_SSL_SESSION_TICKETS) && defined(MBEDTLS_SSL_CLI_C)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005674 if (3 > (size_t) (end - p)) {
5675 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
5676 }
Manuel Pégourié-Gonnard35eb8022019-05-16 11:11:08 +02005677
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005678 session->ticket_len = (p[0] << 16) | (p[1] << 8) | p[2];
Manuel Pégourié-Gonnard35eb8022019-05-16 11:11:08 +02005679 p += 3;
5680
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005681 if (session->ticket_len != 0) {
5682 if (session->ticket_len > (size_t) (end - p)) {
5683 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
5684 }
Manuel Pégourié-Gonnard35eb8022019-05-16 11:11:08 +02005685
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005686 session->ticket = mbedtls_calloc(1, session->ticket_len);
5687 if (session->ticket == NULL) {
5688 return MBEDTLS_ERR_SSL_ALLOC_FAILED;
5689 }
Manuel Pégourié-Gonnard35eb8022019-05-16 11:11:08 +02005690
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005691 memcpy(session->ticket, p, session->ticket_len);
Manuel Pégourié-Gonnard35eb8022019-05-16 11:11:08 +02005692 p += session->ticket_len;
5693 }
Manuel Pégourié-Gonnardf743c032019-05-24 12:06:29 +02005694
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005695 if (4 > (size_t) (end - p)) {
5696 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
5697 }
Manuel Pégourié-Gonnardf743c032019-05-24 12:06:29 +02005698
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005699 session->ticket_lifetime = ((uint32_t) p[0] << 24) |
5700 ((uint32_t) p[1] << 16) |
5701 ((uint32_t) p[2] << 8) |
5702 ((uint32_t) p[3]);
Manuel Pégourié-Gonnardf743c032019-05-24 12:06:29 +02005703 p += 4;
Manuel Pégourié-Gonnard35eb8022019-05-16 11:11:08 +02005704#endif /* MBEDTLS_SSL_SESSION_TICKETS && MBEDTLS_SSL_CLI_C */
5705
Manuel Pégourié-Gonnardf743c032019-05-24 12:06:29 +02005706 /*
5707 * Misc extension-related info
5708 */
5709#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005710 if (1 > (size_t) (end - p)) {
5711 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
5712 }
Manuel Pégourié-Gonnardf743c032019-05-24 12:06:29 +02005713
5714 session->mfl_code = *p++;
5715#endif
5716
5717#if defined(MBEDTLS_SSL_TRUNCATED_HMAC)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005718 if (1 > (size_t) (end - p)) {
5719 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
5720 }
Manuel Pégourié-Gonnardf743c032019-05-24 12:06:29 +02005721
5722 session->trunc_hmac = *p++;
5723#endif
5724
5725#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005726 if (1 > (size_t) (end - p)) {
5727 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
5728 }
Manuel Pégourié-Gonnardf743c032019-05-24 12:06:29 +02005729
5730 session->encrypt_then_mac = *p++;
5731#endif
5732
Manuel Pégourié-Gonnard35eb8022019-05-16 11:11:08 +02005733 /* Done, should have consumed entire buffer */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005734 if (p != end) {
5735 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
5736 }
Manuel Pégourié-Gonnarda3e7c652019-05-16 10:08:35 +02005737
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005738 return 0;
Manuel Pégourié-Gonnarda3e7c652019-05-16 10:08:35 +02005739}
5740
5741/*
Manuel Pégourié-Gonnardb9dfc9f2019-07-12 10:50:19 +02005742 * Deserialize session: public wrapper for error cleaning
Manuel Pégourié-Gonnarda3d831b2019-05-23 12:28:45 +02005743 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005744int mbedtls_ssl_session_load(mbedtls_ssl_session *session,
5745 const unsigned char *buf,
5746 size_t len)
Manuel Pégourié-Gonnarda3d831b2019-05-23 12:28:45 +02005747{
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005748 int ret = ssl_session_load(session, 0, buf, len);
Manuel Pégourié-Gonnarda3d831b2019-05-23 12:28:45 +02005749
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005750 if (ret != 0) {
5751 mbedtls_ssl_session_free(session);
5752 }
Manuel Pégourié-Gonnarda3d831b2019-05-23 12:28:45 +02005753
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005754 return ret;
Manuel Pégourié-Gonnarda3d831b2019-05-23 12:28:45 +02005755}
5756
5757/*
Paul Bakker1961b702013-01-25 14:49:24 +01005758 * Perform a single step of the SSL handshake
Paul Bakker5121ce52009-01-03 21:22:43 +00005759 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005760int mbedtls_ssl_handshake_step(mbedtls_ssl_context *ssl)
Paul Bakker5121ce52009-01-03 21:22:43 +00005761{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005762 int ret = MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
Paul Bakker5121ce52009-01-03 21:22:43 +00005763
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005764 if (ssl == NULL || ssl->conf == NULL) {
5765 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
5766 }
Manuel Pégourié-Gonnardf81ee2e2015-09-01 17:43:40 +02005767
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005768#if defined(MBEDTLS_SSL_CLI_C)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005769 if (ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT) {
5770 ret = mbedtls_ssl_handshake_client_step(ssl);
5771 }
Paul Bakker5121ce52009-01-03 21:22:43 +00005772#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005773#if defined(MBEDTLS_SSL_SRV_C)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005774 if (ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER) {
5775 ret = mbedtls_ssl_handshake_server_step(ssl);
5776 }
Paul Bakker5121ce52009-01-03 21:22:43 +00005777#endif
5778
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005779 return ret;
Paul Bakker1961b702013-01-25 14:49:24 +01005780}
5781
5782/*
5783 * Perform the SSL handshake
5784 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005785int mbedtls_ssl_handshake(mbedtls_ssl_context *ssl)
Paul Bakker1961b702013-01-25 14:49:24 +01005786{
5787 int ret = 0;
5788
Hanno Beckera817ea42020-10-20 15:20:23 +01005789 /* Sanity checks */
5790
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005791 if (ssl == NULL || ssl->conf == NULL) {
5792 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
5793 }
Manuel Pégourié-Gonnardf81ee2e2015-09-01 17:43:40 +02005794
Hanno Beckera817ea42020-10-20 15:20:23 +01005795#if defined(MBEDTLS_SSL_PROTO_DTLS)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005796 if (ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
5797 (ssl->f_set_timer == NULL || ssl->f_get_timer == NULL)) {
5798 MBEDTLS_SSL_DEBUG_MSG(1, ("You must use "
5799 "mbedtls_ssl_set_timer_cb() for DTLS"));
5800 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
Hanno Beckera817ea42020-10-20 15:20:23 +01005801 }
5802#endif /* MBEDTLS_SSL_PROTO_DTLS */
5803
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005804 MBEDTLS_SSL_DEBUG_MSG(2, ("=> handshake"));
Paul Bakker1961b702013-01-25 14:49:24 +01005805
Hanno Beckera817ea42020-10-20 15:20:23 +01005806 /* Main handshake loop */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005807 while (ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER) {
5808 ret = mbedtls_ssl_handshake_step(ssl);
Paul Bakker1961b702013-01-25 14:49:24 +01005809
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005810 if (ret != 0) {
Paul Bakker1961b702013-01-25 14:49:24 +01005811 break;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005812 }
Paul Bakker1961b702013-01-25 14:49:24 +01005813 }
5814
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005815 MBEDTLS_SSL_DEBUG_MSG(2, ("<= handshake"));
Paul Bakker5121ce52009-01-03 21:22:43 +00005816
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005817 return ret;
Paul Bakker5121ce52009-01-03 21:22:43 +00005818}
5819
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005820#if defined(MBEDTLS_SSL_RENEGOTIATION)
5821#if defined(MBEDTLS_SSL_SRV_C)
Paul Bakker5121ce52009-01-03 21:22:43 +00005822/*
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01005823 * Write HelloRequest to request renegotiation on server
Paul Bakker48916f92012-09-16 19:57:18 +00005824 */
Manuel Pégourié-Gonnardd904d662022-06-17 10:24:00 +02005825MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005826static int ssl_write_hello_request(mbedtls_ssl_context *ssl)
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01005827{
Janos Follath865b3eb2019-12-16 11:46:15 +00005828 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01005829
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005830 MBEDTLS_SSL_DEBUG_MSG(2, ("=> write hello request"));
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01005831
5832 ssl->out_msglen = 4;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005833 ssl->out_msgtype = MBEDTLS_SSL_MSG_HANDSHAKE;
5834 ssl->out_msg[0] = MBEDTLS_SSL_HS_HELLO_REQUEST;
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01005835
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005836 if ((ret = mbedtls_ssl_write_handshake_msg(ssl)) != 0) {
5837 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ssl_write_handshake_msg", ret);
5838 return ret;
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01005839 }
5840
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005841 MBEDTLS_SSL_DEBUG_MSG(2, ("<= write hello request"));
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01005842
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005843 return 0;
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01005844}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005845#endif /* MBEDTLS_SSL_SRV_C */
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01005846
5847/*
5848 * Actually renegotiate current connection, triggered by either:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005849 * - any side: calling mbedtls_ssl_renegotiate(),
5850 * - client: receiving a HelloRequest during mbedtls_ssl_read(),
5851 * - server: receiving any handshake message on server during mbedtls_ssl_read() after
Manuel Pégourié-Gonnard55e4ff22014-08-19 11:16:35 +02005852 * the initial handshake is completed.
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01005853 * If the handshake doesn't complete due to waiting for I/O, it will continue
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005854 * during the next calls to mbedtls_ssl_renegotiate() or mbedtls_ssl_read() respectively.
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01005855 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005856int mbedtls_ssl_start_renegotiation(mbedtls_ssl_context *ssl)
Paul Bakker48916f92012-09-16 19:57:18 +00005857{
Janos Follath865b3eb2019-12-16 11:46:15 +00005858 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker48916f92012-09-16 19:57:18 +00005859
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005860 MBEDTLS_SSL_DEBUG_MSG(2, ("=> renegotiate"));
Paul Bakker48916f92012-09-16 19:57:18 +00005861
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005862 if ((ret = ssl_handshake_init(ssl)) != 0) {
5863 return ret;
5864 }
Paul Bakker48916f92012-09-16 19:57:18 +00005865
Manuel Pégourié-Gonnard0557bd52014-08-19 19:18:39 +02005866 /* RFC 6347 4.2.2: "[...] the HelloRequest will have message_seq = 0 and
5867 * the ServerHello will have message_seq = 1" */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005868#if defined(MBEDTLS_SSL_PROTO_DTLS)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005869 if (ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
5870 ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_PENDING) {
5871 if (ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER) {
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02005872 ssl->handshake->out_msg_seq = 1;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005873 } else {
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02005874 ssl->handshake->in_msg_seq = 1;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005875 }
Manuel Pégourié-Gonnard0557bd52014-08-19 19:18:39 +02005876 }
5877#endif
5878
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005879 ssl->state = MBEDTLS_SSL_HELLO_REQUEST;
5880 ssl->renego_status = MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS;
Paul Bakker48916f92012-09-16 19:57:18 +00005881
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005882 if ((ret = mbedtls_ssl_handshake(ssl)) != 0) {
5883 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ssl_handshake", ret);
5884 return ret;
Paul Bakker48916f92012-09-16 19:57:18 +00005885 }
5886
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005887 MBEDTLS_SSL_DEBUG_MSG(2, ("<= renegotiate"));
Paul Bakker48916f92012-09-16 19:57:18 +00005888
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005889 return 0;
Paul Bakker48916f92012-09-16 19:57:18 +00005890}
5891
5892/*
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01005893 * Renegotiate current connection on client,
5894 * or request renegotiation on server
5895 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005896int mbedtls_ssl_renegotiate(mbedtls_ssl_context *ssl)
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01005897{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005898 int ret = MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01005899
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005900 if (ssl == NULL || ssl->conf == NULL) {
5901 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
5902 }
Manuel Pégourié-Gonnardf81ee2e2015-09-01 17:43:40 +02005903
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005904#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01005905 /* On server, just send the request */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005906 if (ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER) {
5907 if (ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER) {
5908 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
5909 }
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01005910
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005911 ssl->renego_status = MBEDTLS_SSL_RENEGOTIATION_PENDING;
Manuel Pégourié-Gonnardf07f4212014-08-15 19:04:47 +02005912
5913 /* Did we already try/start sending HelloRequest? */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005914 if (ssl->out_left != 0) {
5915 return mbedtls_ssl_flush_output(ssl);
5916 }
Manuel Pégourié-Gonnardf07f4212014-08-15 19:04:47 +02005917
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005918 return ssl_write_hello_request(ssl);
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01005919 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005920#endif /* MBEDTLS_SSL_SRV_C */
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01005921
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005922#if defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01005923 /*
5924 * On client, either start the renegotiation process or,
5925 * if already in progress, continue the handshake
5926 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005927 if (ssl->renego_status != MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS) {
5928 if (ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER) {
5929 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01005930 }
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005931
5932 if ((ret = mbedtls_ssl_start_renegotiation(ssl)) != 0) {
5933 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ssl_start_renegotiation", ret);
5934 return ret;
5935 }
5936 } else {
5937 if ((ret = mbedtls_ssl_handshake(ssl)) != 0) {
5938 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ssl_handshake", ret);
5939 return ret;
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01005940 }
5941 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005942#endif /* MBEDTLS_SSL_CLI_C */
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01005943
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005944 return ret;
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01005945}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005946#endif /* MBEDTLS_SSL_RENEGOTIATION */
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01005947
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005948#if defined(MBEDTLS_X509_CRT_PARSE_C)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005949static void ssl_key_cert_free(mbedtls_ssl_key_cert *key_cert)
Manuel Pégourié-Gonnard705fcca2013-09-23 20:04:20 +02005950{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005951 mbedtls_ssl_key_cert *cur = key_cert, *next;
Manuel Pégourié-Gonnard705fcca2013-09-23 20:04:20 +02005952
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005953 while (cur != NULL) {
Manuel Pégourié-Gonnard705fcca2013-09-23 20:04:20 +02005954 next = cur->next;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005955 mbedtls_free(cur);
Manuel Pégourié-Gonnard705fcca2013-09-23 20:04:20 +02005956 cur = next;
5957 }
5958}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005959#endif /* MBEDTLS_X509_CRT_PARSE_C */
Manuel Pégourié-Gonnard705fcca2013-09-23 20:04:20 +02005960
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005961void mbedtls_ssl_handshake_free(mbedtls_ssl_context *ssl)
Paul Bakker48916f92012-09-16 19:57:18 +00005962{
Gilles Peskine9b562d52018-04-25 20:32:43 +02005963 mbedtls_ssl_handshake_params *handshake = ssl->handshake;
5964
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005965 if (handshake == NULL) {
Paul Bakkeraccaffe2014-06-26 13:37:14 +02005966 return;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005967 }
Paul Bakkeraccaffe2014-06-26 13:37:14 +02005968
Gilles Peskinedf13d5c2018-04-25 20:39:48 +02005969#if defined(MBEDTLS_SSL_ASYNC_PRIVATE)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005970 if (ssl->conf->f_async_cancel != NULL && handshake->async_in_progress != 0) {
5971 ssl->conf->f_async_cancel(ssl);
Gilles Peskinedf13d5c2018-04-25 20:39:48 +02005972 handshake->async_in_progress = 0;
5973 }
5974#endif /* MBEDTLS_SSL_ASYNC_PRIVATE */
5975
Manuel Pégourié-Gonnardb9d64e52015-07-06 14:18:56 +02005976#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1) || \
5977 defined(MBEDTLS_SSL_PROTO_TLS1_1)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005978 mbedtls_md5_free(&handshake->fin_md5);
5979 mbedtls_sha1_free(&handshake->fin_sha1);
Manuel Pégourié-Gonnardb9d64e52015-07-06 14:18:56 +02005980#endif
5981#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
5982#if defined(MBEDTLS_SHA256_C)
Andrzej Kurekeb342242019-01-29 09:14:33 -05005983#if defined(MBEDTLS_USE_PSA_CRYPTO)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005984 psa_hash_abort(&handshake->fin_sha256_psa);
Andrzej Kurekeb342242019-01-29 09:14:33 -05005985#else
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005986 mbedtls_sha256_free(&handshake->fin_sha256);
Manuel Pégourié-Gonnardb9d64e52015-07-06 14:18:56 +02005987#endif
Andrzej Kurekeb342242019-01-29 09:14:33 -05005988#endif
Gilles Peskined2d59372021-05-12 22:43:27 +02005989#if defined(MBEDTLS_SHA512_C) && !defined(MBEDTLS_SHA512_NO_SHA384)
Andrzej Kurekeb342242019-01-29 09:14:33 -05005990#if defined(MBEDTLS_USE_PSA_CRYPTO)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005991 psa_hash_abort(&handshake->fin_sha384_psa);
Andrzej Kurekeb342242019-01-29 09:14:33 -05005992#else
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005993 mbedtls_sha512_free(&handshake->fin_sha512);
Manuel Pégourié-Gonnardb9d64e52015-07-06 14:18:56 +02005994#endif
Andrzej Kurekeb342242019-01-29 09:14:33 -05005995#endif
Manuel Pégourié-Gonnardb9d64e52015-07-06 14:18:56 +02005996#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
5997
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005998#if defined(MBEDTLS_DHM_C)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005999 mbedtls_dhm_free(&handshake->dhm_ctx);
Paul Bakker48916f92012-09-16 19:57:18 +00006000#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006001#if defined(MBEDTLS_ECDH_C)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01006002 mbedtls_ecdh_free(&handshake->ecdh_ctx);
Paul Bakker61d113b2013-07-04 11:51:43 +02006003#endif
Manuel Pégourié-Gonnardeef142d2015-09-16 10:05:04 +02006004#if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01006005 mbedtls_ecjpake_free(&handshake->ecjpake_ctx);
Manuel Pégourié-Gonnard77c06462015-09-17 13:59:49 +02006006#if defined(MBEDTLS_SSL_CLI_C)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01006007 mbedtls_free(handshake->ecjpake_cache);
Manuel Pégourié-Gonnard77c06462015-09-17 13:59:49 +02006008 handshake->ecjpake_cache = NULL;
6009 handshake->ecjpake_cache_len = 0;
6010#endif
Manuel Pégourié-Gonnard76cfd3f2015-09-15 12:10:54 +02006011#endif
Paul Bakker61d113b2013-07-04 11:51:43 +02006012
Janos Follath4ae5c292016-02-10 11:27:43 +00006013#if defined(MBEDTLS_ECDH_C) || defined(MBEDTLS_ECDSA_C) || \
6014 defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
Paul Bakker9af723c2014-05-01 13:03:14 +02006015 /* explicit void pointer cast for buggy MS compiler */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01006016 mbedtls_free((void *) handshake->curves);
Manuel Pégourié-Gonnardd09453c2013-09-23 19:11:32 +02006017#endif
6018
Gilles Peskineeccd8882020-03-10 12:19:08 +01006019#if defined(MBEDTLS_KEY_EXCHANGE_SOME_PSK_ENABLED)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01006020 if (handshake->psk != NULL) {
6021 mbedtls_platform_zeroize(handshake->psk, handshake->psk_len);
6022 mbedtls_free(handshake->psk);
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01006023 }
6024#endif
6025
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006026#if defined(MBEDTLS_X509_CRT_PARSE_C) && \
6027 defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
Manuel Pégourié-Gonnard83724542013-09-24 22:30:56 +02006028 /*
6029 * Free only the linked list wrapper, not the keys themselves
6030 * since the belong to the SNI callback
6031 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01006032 if (handshake->sni_key_cert != NULL) {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006033 mbedtls_ssl_key_cert *cur = handshake->sni_key_cert, *next;
Manuel Pégourié-Gonnard83724542013-09-24 22:30:56 +02006034
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01006035 while (cur != NULL) {
Manuel Pégourié-Gonnard83724542013-09-24 22:30:56 +02006036 next = cur->next;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01006037 mbedtls_free(cur);
Manuel Pégourié-Gonnard83724542013-09-24 22:30:56 +02006038 cur = next;
6039 }
6040 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006041#endif /* MBEDTLS_X509_CRT_PARSE_C && MBEDTLS_SSL_SERVER_NAME_INDICATION */
Manuel Pégourié-Gonnard705fcca2013-09-23 20:04:20 +02006042
Gilles Peskineeccd8882020-03-10 12:19:08 +01006043#if defined(MBEDTLS_SSL_ECP_RESTARTABLE_ENABLED)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01006044 mbedtls_x509_crt_restart_free(&handshake->ecrs_ctx);
6045 if (handshake->ecrs_peer_cert != NULL) {
6046 mbedtls_x509_crt_free(handshake->ecrs_peer_cert);
6047 mbedtls_free(handshake->ecrs_peer_cert);
Hanno Becker3dad3112019-02-05 17:19:52 +00006048 }
Manuel Pégourié-Gonnard862cde52017-05-17 11:56:15 +02006049#endif
6050
Hanno Becker75173122019-02-06 16:18:31 +00006051#if defined(MBEDTLS_X509_CRT_PARSE_C) && \
6052 !defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01006053 mbedtls_pk_free(&handshake->peer_pubkey);
Hanno Becker75173122019-02-06 16:18:31 +00006054#endif /* MBEDTLS_X509_CRT_PARSE_C && !MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
6055
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006056#if defined(MBEDTLS_SSL_PROTO_DTLS)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01006057 mbedtls_free(handshake->verify_cookie);
6058 mbedtls_ssl_flight_free(handshake->flight);
6059 mbedtls_ssl_buffering_free(ssl);
Manuel Pégourié-Gonnard74848812014-07-11 02:43:49 +02006060#endif
6061
Hanno Becker4a63ed42019-01-08 11:39:35 +00006062#if defined(MBEDTLS_ECDH_C) && \
6063 defined(MBEDTLS_USE_PSA_CRYPTO)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01006064 psa_destroy_key(handshake->ecdh_psa_privkey);
Hanno Becker4a63ed42019-01-08 11:39:35 +00006065#endif /* MBEDTLS_ECDH_C && MBEDTLS_USE_PSA_CRYPTO */
6066
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01006067 mbedtls_platform_zeroize(handshake,
6068 sizeof(mbedtls_ssl_handshake_params));
Andrzej Kurek0afa2a12020-03-03 10:39:58 -05006069
6070#if defined(MBEDTLS_SSL_VARIABLE_BUFFER_LENGTH)
6071 /* If the buffers are too big - reallocate. Because of the way Mbed TLS
6072 * processes datagrams and the fact that a datagram is allowed to have
6073 * several records in it, it is possible that the I/O buffers are not
6074 * empty at this stage */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01006075 handle_buffer_resizing(ssl, 1, mbedtls_ssl_get_input_buflen(ssl),
6076 mbedtls_ssl_get_output_buflen(ssl));
Andrzej Kurek0afa2a12020-03-03 10:39:58 -05006077#endif
Paul Bakker48916f92012-09-16 19:57:18 +00006078}
6079
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01006080void mbedtls_ssl_session_free(mbedtls_ssl_session *session)
Paul Bakker48916f92012-09-16 19:57:18 +00006081{
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01006082 if (session == NULL) {
Paul Bakkeraccaffe2014-06-26 13:37:14 +02006083 return;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01006084 }
Paul Bakkeraccaffe2014-06-26 13:37:14 +02006085
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006086#if defined(MBEDTLS_X509_CRT_PARSE_C)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01006087 ssl_clear_peer_cert(session);
Paul Bakkered27a042013-04-18 22:46:23 +02006088#endif
Paul Bakker0a597072012-09-25 21:55:46 +00006089
Manuel Pégourié-Gonnardb596abf2015-05-20 10:45:29 +02006090#if defined(MBEDTLS_SSL_SESSION_TICKETS) && defined(MBEDTLS_SSL_CLI_C)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01006091 mbedtls_free(session->ticket);
Paul Bakkera503a632013-08-14 13:48:06 +02006092#endif
Manuel Pégourié-Gonnard75d44012013-08-02 14:44:04 +02006093
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01006094 mbedtls_platform_zeroize(session, sizeof(mbedtls_ssl_session));
Paul Bakker48916f92012-09-16 19:57:18 +00006095}
6096
Manuel Pégourié-Gonnard5c0e3772019-07-23 16:13:17 +02006097#if defined(MBEDTLS_SSL_CONTEXT_SERIALIZATION)
Manuel Pégourié-Gonnard4e9370b2019-07-23 16:31:16 +02006098
6099#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
6100#define SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_CONNECTION_ID 1u
6101#else
6102#define SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_CONNECTION_ID 0u
6103#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
6104
6105#if defined(MBEDTLS_SSL_DTLS_BADMAC_LIMIT)
6106#define SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_BADMAC_LIMIT 1u
6107#else
6108#define SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_BADMAC_LIMIT 0u
6109#endif /* MBEDTLS_SSL_DTLS_BADMAC_LIMIT */
6110
6111#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
6112#define SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_ANTI_REPLAY 1u
6113#else
6114#define SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_ANTI_REPLAY 0u
6115#endif /* MBEDTLS_SSL_DTLS_ANTI_REPLAY */
6116
6117#if defined(MBEDTLS_SSL_ALPN)
6118#define SSL_SERIALIZED_CONTEXT_CONFIG_ALPN 1u
6119#else
6120#define SSL_SERIALIZED_CONTEXT_CONFIG_ALPN 0u
6121#endif /* MBEDTLS_SSL_ALPN */
6122
6123#define SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_CONNECTION_ID_BIT 0
6124#define SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_BADMAC_LIMIT_BIT 1
6125#define SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_ANTI_REPLAY_BIT 2
6126#define SSL_SERIALIZED_CONTEXT_CONFIG_ALPN_BIT 3
6127
6128#define SSL_SERIALIZED_CONTEXT_CONFIG_BITFLAG \
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01006129 ((uint32_t) ( \
6130 (SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_CONNECTION_ID << \
6131 SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_CONNECTION_ID_BIT) | \
6132 (SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_BADMAC_LIMIT << \
6133 SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_BADMAC_LIMIT_BIT) | \
6134 (SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_ANTI_REPLAY << \
6135 SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_ANTI_REPLAY_BIT) | \
6136 (SSL_SERIALIZED_CONTEXT_CONFIG_ALPN << SSL_SERIALIZED_CONTEXT_CONFIG_ALPN_BIT) | \
6137 0u))
Manuel Pégourié-Gonnard4e9370b2019-07-23 16:31:16 +02006138
Manuel Pégourié-Gonnard4c90e852019-07-11 10:58:10 +02006139static unsigned char ssl_serialized_context_header[] = {
6140 MBEDTLS_VERSION_MAJOR,
6141 MBEDTLS_VERSION_MINOR,
6142 MBEDTLS_VERSION_PATCH,
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01006143 MBEDTLS_BYTE_1(SSL_SERIALIZED_SESSION_CONFIG_BITFLAG),
6144 MBEDTLS_BYTE_0(SSL_SERIALIZED_SESSION_CONFIG_BITFLAG),
6145 MBEDTLS_BYTE_2(SSL_SERIALIZED_CONTEXT_CONFIG_BITFLAG),
6146 MBEDTLS_BYTE_1(SSL_SERIALIZED_CONTEXT_CONFIG_BITFLAG),
6147 MBEDTLS_BYTE_0(SSL_SERIALIZED_CONTEXT_CONFIG_BITFLAG),
Manuel Pégourié-Gonnard4c90e852019-07-11 10:58:10 +02006148};
6149
Paul Bakker5121ce52009-01-03 21:22:43 +00006150/*
Manuel Pégourié-Gonnardac87e282019-05-28 13:02:16 +02006151 * Serialize a full SSL context
Manuel Pégourié-Gonnard00400c22019-07-10 14:58:45 +02006152 *
6153 * The format of the serialized data is:
6154 * (in the presentation language of TLS, RFC 8446 section 3)
6155 *
6156 * // header
6157 * opaque mbedtls_version[3]; // major, minor, patch
Manuel Pégourié-Gonnard4c90e852019-07-11 10:58:10 +02006158 * opaque context_format[5]; // version-specific field determining
Manuel Pégourié-Gonnard00400c22019-07-10 14:58:45 +02006159 * // the format of the remaining
Manuel Pégourié-Gonnard4c90e852019-07-11 10:58:10 +02006160 * // serialized data.
Manuel Pégourié-Gonnard4e9370b2019-07-23 16:31:16 +02006161 * Note: When updating the format, remember to keep these
6162 * version+format bytes. (We may make their size part of the API.)
Manuel Pégourié-Gonnard00400c22019-07-10 14:58:45 +02006163 *
6164 * // session sub-structure
6165 * opaque session<1..2^32-1>; // see mbedtls_ssl_session_save()
6166 * // transform sub-structure
6167 * uint8 random[64]; // ServerHello.random+ClientHello.random
6168 * uint8 in_cid<0..2^8-1> // Connection ID: expected incoming value
6169 * uint8 out_cid<0..2^8-1> // Connection ID: outgoing value to use
6170 * // fields from ssl_context
6171 * uint32 badmac_seen; // DTLS: number of records with failing MAC
6172 * uint64 in_window_top; // DTLS: last validated record seq_num
6173 * uint64 in_window; // DTLS: bitmask for replay protection
6174 * uint8 disable_datagram_packing; // DTLS: only one record per datagram
6175 * uint64 cur_out_ctr; // Record layer: outgoing sequence number
6176 * uint16 mtu; // DTLS: path mtu (max outgoing fragment size)
6177 * uint8 alpn_chosen<0..2^8-1> // ALPN: negotiated application protocol
6178 *
6179 * Note that many fields of the ssl_context or sub-structures are not
6180 * serialized, as they fall in one of the following categories:
6181 *
6182 * 1. forced value (eg in_left must be 0)
6183 * 2. pointer to dynamically-allocated memory (eg session, transform)
6184 * 3. value can be re-derived from other data (eg session keys from MS)
6185 * 4. value was temporary (eg content of input buffer)
6186 * 5. value will be provided by the user again (eg I/O callbacks and context)
Manuel Pégourié-Gonnardac87e282019-05-28 13:02:16 +02006187 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01006188int mbedtls_ssl_context_save(mbedtls_ssl_context *ssl,
6189 unsigned char *buf,
6190 size_t buf_len,
6191 size_t *olen)
Manuel Pégourié-Gonnardac87e282019-05-28 13:02:16 +02006192{
Manuel Pégourié-Gonnard4c90e852019-07-11 10:58:10 +02006193 unsigned char *p = buf;
6194 size_t used = 0;
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02006195 size_t session_len;
6196 int ret = 0;
Manuel Pégourié-Gonnard4c90e852019-07-11 10:58:10 +02006197
Manuel Pégourié-Gonnard1aaf6692019-07-10 14:14:05 +02006198 /*
Manuel Pégourié-Gonnarde4588692019-07-29 12:28:52 +02006199 * Enforce usage restrictions, see "return BAD_INPUT_DATA" in
6200 * this function's documentation.
6201 *
6202 * These are due to assumptions/limitations in the implementation. Some of
6203 * them are likely to stay (no handshake in progress) some might go away
6204 * (only DTLS) but are currently used to simplify the implementation.
Manuel Pégourié-Gonnard1aaf6692019-07-10 14:14:05 +02006205 */
Manuel Pégourié-Gonnarde4588692019-07-29 12:28:52 +02006206 /* The initial handshake must be over */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01006207 if (ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER) {
6208 MBEDTLS_SSL_DEBUG_MSG(1, ("Initial handshake isn't over"));
6209 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
Jarno Lamsa8c51b7c2019-08-21 13:45:05 +03006210 }
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01006211 if (ssl->handshake != NULL) {
6212 MBEDTLS_SSL_DEBUG_MSG(1, ("Handshake isn't completed"));
6213 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
Jarno Lamsa8c51b7c2019-08-21 13:45:05 +03006214 }
Manuel Pégourié-Gonnarde4588692019-07-29 12:28:52 +02006215 /* Double-check that sub-structures are indeed ready */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01006216 if (ssl->transform == NULL || ssl->session == NULL) {
6217 MBEDTLS_SSL_DEBUG_MSG(1, ("Serialised structures aren't ready"));
6218 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
Jarno Lamsa8c51b7c2019-08-21 13:45:05 +03006219 }
Manuel Pégourié-Gonnarde4588692019-07-29 12:28:52 +02006220 /* There must be no pending incoming or outgoing data */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01006221 if (mbedtls_ssl_check_pending(ssl) != 0) {
6222 MBEDTLS_SSL_DEBUG_MSG(1, ("There is pending incoming data"));
6223 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
Jarno Lamsa8c51b7c2019-08-21 13:45:05 +03006224 }
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01006225 if (ssl->out_left != 0) {
6226 MBEDTLS_SSL_DEBUG_MSG(1, ("There is pending outgoing data"));
6227 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
Jarno Lamsa8c51b7c2019-08-21 13:45:05 +03006228 }
Dave Rodgmana03396a2022-12-06 16:30:38 +00006229 /* Protocol must be DTLS, not TLS */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01006230 if (ssl->conf->transport != MBEDTLS_SSL_TRANSPORT_DATAGRAM) {
6231 MBEDTLS_SSL_DEBUG_MSG(1, ("Only DTLS is supported"));
6232 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
Jarno Lamsa8c51b7c2019-08-21 13:45:05 +03006233 }
Manuel Pégourié-Gonnarde4588692019-07-29 12:28:52 +02006234 /* Version must be 1.2 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01006235 if (ssl->major_ver != MBEDTLS_SSL_MAJOR_VERSION_3) {
6236 MBEDTLS_SSL_DEBUG_MSG(1, ("Only version 1.2 supported"));
6237 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
Jarno Lamsa8c51b7c2019-08-21 13:45:05 +03006238 }
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01006239 if (ssl->minor_ver != MBEDTLS_SSL_MINOR_VERSION_3) {
6240 MBEDTLS_SSL_DEBUG_MSG(1, ("Only version 1.2 supported"));
6241 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
Jarno Lamsa8c51b7c2019-08-21 13:45:05 +03006242 }
Manuel Pégourié-Gonnarde4588692019-07-29 12:28:52 +02006243 /* We must be using an AEAD ciphersuite */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01006244 if (mbedtls_ssl_transform_uses_aead(ssl->transform) != 1) {
6245 MBEDTLS_SSL_DEBUG_MSG(1, ("Only AEAD ciphersuites supported"));
6246 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
Jarno Lamsa8c51b7c2019-08-21 13:45:05 +03006247 }
Manuel Pégourié-Gonnarde4588692019-07-29 12:28:52 +02006248 /* Renegotiation must not be enabled */
6249#if defined(MBEDTLS_SSL_RENEGOTIATION)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01006250 if (ssl->conf->disable_renegotiation != MBEDTLS_SSL_RENEGOTIATION_DISABLED) {
6251 MBEDTLS_SSL_DEBUG_MSG(1, ("Renegotiation must not be enabled"));
6252 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
Jarno Lamsa8c51b7c2019-08-21 13:45:05 +03006253 }
Manuel Pégourié-Gonnarde4588692019-07-29 12:28:52 +02006254#endif
Manuel Pégourié-Gonnardac87e282019-05-28 13:02:16 +02006255
Manuel Pégourié-Gonnard4c90e852019-07-11 10:58:10 +02006256 /*
6257 * Version and format identifier
6258 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01006259 used += sizeof(ssl_serialized_context_header);
Manuel Pégourié-Gonnardac87e282019-05-28 13:02:16 +02006260
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01006261 if (used <= buf_len) {
6262 memcpy(p, ssl_serialized_context_header,
6263 sizeof(ssl_serialized_context_header));
6264 p += sizeof(ssl_serialized_context_header);
Manuel Pégourié-Gonnard4c90e852019-07-11 10:58:10 +02006265 }
6266
6267 /*
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02006268 * Session (length + data)
6269 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01006270 ret = ssl_session_save(ssl->session, 1, NULL, 0, &session_len);
6271 if (ret != MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL) {
6272 return ret;
6273 }
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02006274
6275 used += 4 + session_len;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01006276 if (used <= buf_len) {
6277 MBEDTLS_PUT_UINT32_BE(session_len, p, 0);
Joe Subbiani2f98d792021-08-20 11:44:44 +01006278 p += 4;
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02006279
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01006280 ret = ssl_session_save(ssl->session, 1,
6281 p, session_len, &session_len);
6282 if (ret != 0) {
6283 return ret;
6284 }
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02006285
6286 p += session_len;
6287 }
6288
6289 /*
Manuel Pégourié-Gonnardc2a7b892019-07-15 09:04:11 +02006290 * Transform
6291 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01006292 used += sizeof(ssl->transform->randbytes);
6293 if (used <= buf_len) {
6294 memcpy(p, ssl->transform->randbytes,
6295 sizeof(ssl->transform->randbytes));
6296 p += sizeof(ssl->transform->randbytes);
Manuel Pégourié-Gonnardc2a7b892019-07-15 09:04:11 +02006297 }
6298
6299#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
6300 used += 2 + ssl->transform->in_cid_len + ssl->transform->out_cid_len;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01006301 if (used <= buf_len) {
Manuel Pégourié-Gonnardc2a7b892019-07-15 09:04:11 +02006302 *p++ = ssl->transform->in_cid_len;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01006303 memcpy(p, ssl->transform->in_cid, ssl->transform->in_cid_len);
Manuel Pégourié-Gonnardc2a7b892019-07-15 09:04:11 +02006304 p += ssl->transform->in_cid_len;
6305
6306 *p++ = ssl->transform->out_cid_len;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01006307 memcpy(p, ssl->transform->out_cid, ssl->transform->out_cid_len);
Manuel Pégourié-Gonnardc2a7b892019-07-15 09:04:11 +02006308 p += ssl->transform->out_cid_len;
6309 }
6310#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
6311
6312 /*
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02006313 * Saved fields from top-level ssl_context structure
6314 */
6315#if defined(MBEDTLS_SSL_DTLS_BADMAC_LIMIT)
6316 used += 4;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01006317 if (used <= buf_len) {
6318 MBEDTLS_PUT_UINT32_BE(ssl->badmac_seen, p, 0);
Joe Subbiani2f98d792021-08-20 11:44:44 +01006319 p += 4;
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02006320 }
6321#endif /* MBEDTLS_SSL_DTLS_BADMAC_LIMIT */
6322
6323#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
6324 used += 16;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01006325 if (used <= buf_len) {
6326 MBEDTLS_PUT_UINT64_BE(ssl->in_window_top, p, 0);
Joe Subbiani2f98d792021-08-20 11:44:44 +01006327 p += 8;
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02006328
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01006329 MBEDTLS_PUT_UINT64_BE(ssl->in_window, p, 0);
Joe Subbiani2f98d792021-08-20 11:44:44 +01006330 p += 8;
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02006331 }
6332#endif /* MBEDTLS_SSL_DTLS_ANTI_REPLAY */
6333
6334#if defined(MBEDTLS_SSL_PROTO_DTLS)
6335 used += 1;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01006336 if (used <= buf_len) {
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02006337 *p++ = ssl->disable_datagram_packing;
6338 }
6339#endif /* MBEDTLS_SSL_PROTO_DTLS */
6340
6341 used += 8;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01006342 if (used <= buf_len) {
6343 memcpy(p, ssl->cur_out_ctr, 8);
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02006344 p += 8;
6345 }
6346
6347#if defined(MBEDTLS_SSL_PROTO_DTLS)
6348 used += 2;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01006349 if (used <= buf_len) {
6350 MBEDTLS_PUT_UINT16_BE(ssl->mtu, p, 0);
Joe Subbiani2f98d792021-08-20 11:44:44 +01006351 p += 2;
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02006352 }
6353#endif /* MBEDTLS_SSL_PROTO_DTLS */
6354
6355#if defined(MBEDTLS_SSL_ALPN)
6356 {
6357 const uint8_t alpn_len = ssl->alpn_chosen
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01006358 ? (uint8_t) strlen(ssl->alpn_chosen)
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02006359 : 0;
6360
6361 used += 1 + alpn_len;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01006362 if (used <= buf_len) {
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02006363 *p++ = alpn_len;
6364
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01006365 if (ssl->alpn_chosen != NULL) {
6366 memcpy(p, ssl->alpn_chosen, alpn_len);
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02006367 p += alpn_len;
6368 }
6369 }
6370 }
6371#endif /* MBEDTLS_SSL_ALPN */
6372
6373 /*
Manuel Pégourié-Gonnard4c90e852019-07-11 10:58:10 +02006374 * Done
6375 */
6376 *olen = used;
6377
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01006378 if (used > buf_len) {
6379 return MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL;
6380 }
Manuel Pégourié-Gonnardac87e282019-05-28 13:02:16 +02006381
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01006382 MBEDTLS_SSL_DEBUG_BUF(4, "saved context", buf, used);
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02006383
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01006384 return mbedtls_ssl_session_reset_int(ssl, 0);
Manuel Pégourié-Gonnardac87e282019-05-28 13:02:16 +02006385}
6386
6387/*
Manuel Pégourié-Gonnardc2a7b892019-07-15 09:04:11 +02006388 * Helper to get TLS 1.2 PRF from ciphersuite
6389 * (Duplicates bits of logic from ssl_set_handshake_prfs().)
6390 */
Andrzej Kurekf9412f72022-10-18 07:30:19 -04006391#if defined(MBEDTLS_SHA256_C) || \
6392 (defined(MBEDTLS_SHA512_C) && !defined(MBEDTLS_SHA512_NO_SHA384))
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01006393typedef int (*tls_prf_fn)(const unsigned char *secret, size_t slen,
6394 const char *label,
6395 const unsigned char *random, size_t rlen,
6396 unsigned char *dstbuf, size_t dlen);
6397static tls_prf_fn ssl_tls12prf_from_cs(int ciphersuite_id)
Manuel Pégourié-Gonnardc2a7b892019-07-15 09:04:11 +02006398{
6399 const mbedtls_ssl_ciphersuite_t * const ciphersuite_info =
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01006400 mbedtls_ssl_ciphersuite_from_id(ciphersuite_id);
Manuel Pégourié-Gonnardc2a7b892019-07-15 09:04:11 +02006401
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01006402 if (ciphersuite_info == NULL) {
6403 return NULL;
6404 }
Andrzej Kurek84fc52c2022-10-25 04:18:30 -04006405
6406#if defined(MBEDTLS_SHA512_C) && !defined(MBEDTLS_SHA512_NO_SHA384)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01006407 if (ciphersuite_info->mac == MBEDTLS_MD_SHA384) {
6408 return tls_prf_sha384;
6409 } else
Manuel Pégourié-Gonnard9a96fd72019-07-23 17:11:24 +02006410#endif
Andrzej Kurekf9412f72022-10-18 07:30:19 -04006411#if defined(MBEDTLS_SHA256_C)
6412 {
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01006413 if (ciphersuite_info->mac == MBEDTLS_MD_SHA256) {
6414 return tls_prf_sha256;
6415 }
Andrzej Kurekf9412f72022-10-18 07:30:19 -04006416 }
6417#endif
6418#if !defined(MBEDTLS_SHA256_C) && \
6419 (!defined(MBEDTLS_SHA512_C) || defined(MBEDTLS_SHA512_NO_SHA384))
6420 (void) ciphersuite_info;
6421#endif
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01006422 return NULL;
Manuel Pégourié-Gonnardc2a7b892019-07-15 09:04:11 +02006423}
6424
Andrzej Kurekf9412f72022-10-18 07:30:19 -04006425#endif /* MBEDTLS_SHA256_C ||
6426 (MBEDTLS_SHA512_C && !MBEDTLS_SHA512_NO_SHA384) */
6427
Manuel Pégourié-Gonnardc2a7b892019-07-15 09:04:11 +02006428/*
Manuel Pégourié-Gonnardb9dfc9f2019-07-12 10:50:19 +02006429 * Deserialize context, see mbedtls_ssl_context_save() for format.
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02006430 *
6431 * This internal version is wrapped by a public function that cleans up in
6432 * case of error.
Manuel Pégourié-Gonnardac87e282019-05-28 13:02:16 +02006433 */
Manuel Pégourié-Gonnardd904d662022-06-17 10:24:00 +02006434MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01006435static int ssl_context_load(mbedtls_ssl_context *ssl,
6436 const unsigned char *buf,
6437 size_t len)
Manuel Pégourié-Gonnardac87e282019-05-28 13:02:16 +02006438{
Manuel Pégourié-Gonnard4c90e852019-07-11 10:58:10 +02006439 const unsigned char *p = buf;
6440 const unsigned char * const end = buf + len;
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02006441 size_t session_len;
Janos Follath865b3eb2019-12-16 11:46:15 +00006442 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Andrzej Kurekf9412f72022-10-18 07:30:19 -04006443 tls_prf_fn prf_func = NULL;
Manuel Pégourié-Gonnard4c90e852019-07-11 10:58:10 +02006444
Manuel Pégourié-Gonnard0ff76402019-07-11 09:56:30 +02006445 /*
6446 * The context should have been freshly setup or reset.
6447 * Give the user an error in case of obvious misuse.
Manuel Pégourié-Gonnard4ca930f2019-07-26 16:31:53 +02006448 * (Checking session is useful because it won't be NULL if we're
Manuel Pégourié-Gonnard0ff76402019-07-11 09:56:30 +02006449 * renegotiating, or if the user mistakenly loaded a session first.)
6450 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01006451 if (ssl->state != MBEDTLS_SSL_HELLO_REQUEST ||
6452 ssl->session != NULL) {
6453 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
Manuel Pégourié-Gonnard0ff76402019-07-11 09:56:30 +02006454 }
6455
6456 /*
6457 * We can't check that the config matches the initial one, but we can at
6458 * least check it matches the requirements for serializing.
6459 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01006460 if (ssl->conf->transport != MBEDTLS_SSL_TRANSPORT_DATAGRAM ||
Manuel Pégourié-Gonnard0ff76402019-07-11 09:56:30 +02006461 ssl->conf->max_major_ver < MBEDTLS_SSL_MAJOR_VERSION_3 ||
6462 ssl->conf->min_major_ver > MBEDTLS_SSL_MAJOR_VERSION_3 ||
6463 ssl->conf->max_minor_ver < MBEDTLS_SSL_MINOR_VERSION_3 ||
6464 ssl->conf->min_minor_ver > MBEDTLS_SSL_MINOR_VERSION_3 ||
6465#if defined(MBEDTLS_SSL_RENEGOTIATION)
Manuel Pégourié-Gonnard9a96fd72019-07-23 17:11:24 +02006466 ssl->conf->disable_renegotiation != MBEDTLS_SSL_RENEGOTIATION_DISABLED ||
Manuel Pégourié-Gonnard0ff76402019-07-11 09:56:30 +02006467#endif
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01006468 0) {
6469 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
Manuel Pégourié-Gonnard0ff76402019-07-11 09:56:30 +02006470 }
6471
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01006472 MBEDTLS_SSL_DEBUG_BUF(4, "context to load", buf, len);
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02006473
Manuel Pégourié-Gonnard4c90e852019-07-11 10:58:10 +02006474 /*
6475 * Check version identifier
6476 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01006477 if ((size_t) (end - p) < sizeof(ssl_serialized_context_header)) {
6478 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
Manuel Pégourié-Gonnard4c90e852019-07-11 10:58:10 +02006479 }
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01006480
6481 if (memcmp(p, ssl_serialized_context_header,
6482 sizeof(ssl_serialized_context_header)) != 0) {
6483 return MBEDTLS_ERR_SSL_VERSION_MISMATCH;
6484 }
6485 p += sizeof(ssl_serialized_context_header);
Manuel Pégourié-Gonnard4c90e852019-07-11 10:58:10 +02006486
6487 /*
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02006488 * Session
6489 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01006490 if ((size_t) (end - p) < 4) {
6491 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
6492 }
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02006493
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01006494 session_len = ((size_t) p[0] << 24) |
6495 ((size_t) p[1] << 16) |
6496 ((size_t) p[2] << 8) |
6497 ((size_t) p[3]);
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02006498 p += 4;
6499
Manuel Pégourié-Gonnard142ba732019-07-23 14:43:30 +02006500 /* This has been allocated by ssl_handshake_init(), called by
Hanno Becker43aefe22020-02-05 10:44:56 +00006501 * by either mbedtls_ssl_session_reset_int() or mbedtls_ssl_setup(). */
Manuel Pégourié-Gonnard142ba732019-07-23 14:43:30 +02006502 ssl->session = ssl->session_negotiate;
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02006503 ssl->session_in = ssl->session;
6504 ssl->session_out = ssl->session;
Manuel Pégourié-Gonnard142ba732019-07-23 14:43:30 +02006505 ssl->session_negotiate = NULL;
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02006506
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01006507 if ((size_t) (end - p) < session_len) {
6508 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
6509 }
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02006510
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01006511 ret = ssl_session_load(ssl->session, 1, p, session_len);
6512 if (ret != 0) {
6513 mbedtls_ssl_session_free(ssl->session);
6514 return ret;
Manuel Pégourié-Gonnard45ac1f02019-07-23 16:52:45 +02006515 }
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02006516
6517 p += session_len;
6518
6519 /*
Manuel Pégourié-Gonnardc2a7b892019-07-15 09:04:11 +02006520 * Transform
6521 */
6522
Manuel Pégourié-Gonnard142ba732019-07-23 14:43:30 +02006523 /* This has been allocated by ssl_handshake_init(), called by
Hanno Becker43aefe22020-02-05 10:44:56 +00006524 * by either mbedtls_ssl_session_reset_int() or mbedtls_ssl_setup(). */
Manuel Pégourié-Gonnard142ba732019-07-23 14:43:30 +02006525 ssl->transform = ssl->transform_negotiate;
Manuel Pégourié-Gonnardc2a7b892019-07-15 09:04:11 +02006526 ssl->transform_in = ssl->transform;
6527 ssl->transform_out = ssl->transform;
Manuel Pégourié-Gonnard142ba732019-07-23 14:43:30 +02006528 ssl->transform_negotiate = NULL;
Manuel Pégourié-Gonnardc2a7b892019-07-15 09:04:11 +02006529
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01006530 prf_func = ssl_tls12prf_from_cs(ssl->session->ciphersuite);
6531 if (prf_func == NULL) {
6532 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
6533 }
Andrzej Kurekf9412f72022-10-18 07:30:19 -04006534
Manuel Pégourié-Gonnardc2a7b892019-07-15 09:04:11 +02006535 /* Read random bytes and populate structure */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01006536 if ((size_t) (end - p) < sizeof(ssl->transform->randbytes)) {
6537 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
6538 }
Manuel Pégourié-Gonnardc2a7b892019-07-15 09:04:11 +02006539
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01006540 ret = ssl_populate_transform(ssl->transform,
6541 ssl->session->ciphersuite,
6542 ssl->session->master,
Manuel Pégourié-Gonnardc2a7b892019-07-15 09:04:11 +02006543#if defined(MBEDTLS_SSL_SOME_MODES_USE_MAC)
6544#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01006545 ssl->session->encrypt_then_mac,
Manuel Pégourié-Gonnardc2a7b892019-07-15 09:04:11 +02006546#endif
6547#if defined(MBEDTLS_SSL_TRUNCATED_HMAC)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01006548 ssl->session->trunc_hmac,
Manuel Pégourié-Gonnardc2a7b892019-07-15 09:04:11 +02006549#endif
6550#endif /* MBEDTLS_SSL_SOME_MODES_USE_MAC */
6551#if defined(MBEDTLS_ZLIB_SUPPORT)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01006552 ssl->session->compression,
Manuel Pégourié-Gonnardc2a7b892019-07-15 09:04:11 +02006553#endif
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01006554 prf_func,
6555 p, /* currently pointing to randbytes */
6556 MBEDTLS_SSL_MINOR_VERSION_3, /* (D)TLS 1.2 is forced */
6557 ssl->conf->endpoint,
6558 ssl);
6559 if (ret != 0) {
6560 return ret;
6561 }
Manuel Pégourié-Gonnardc2a7b892019-07-15 09:04:11 +02006562
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01006563 p += sizeof(ssl->transform->randbytes);
Manuel Pégourié-Gonnardc2a7b892019-07-15 09:04:11 +02006564
6565#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
6566 /* Read connection IDs and store them */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01006567 if ((size_t) (end - p) < 1) {
6568 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
6569 }
Manuel Pégourié-Gonnardc2a7b892019-07-15 09:04:11 +02006570
6571 ssl->transform->in_cid_len = *p++;
6572
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01006573 if ((size_t) (end - p) < ssl->transform->in_cid_len + 1u) {
6574 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
6575 }
Manuel Pégourié-Gonnardc2a7b892019-07-15 09:04:11 +02006576
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01006577 memcpy(ssl->transform->in_cid, p, ssl->transform->in_cid_len);
Manuel Pégourié-Gonnardc2a7b892019-07-15 09:04:11 +02006578 p += ssl->transform->in_cid_len;
6579
6580 ssl->transform->out_cid_len = *p++;
6581
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01006582 if ((size_t) (end - p) < ssl->transform->out_cid_len) {
6583 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
6584 }
Manuel Pégourié-Gonnardc2a7b892019-07-15 09:04:11 +02006585
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01006586 memcpy(ssl->transform->out_cid, p, ssl->transform->out_cid_len);
Manuel Pégourié-Gonnardc2a7b892019-07-15 09:04:11 +02006587 p += ssl->transform->out_cid_len;
6588#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
6589
6590 /*
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02006591 * Saved fields from top-level ssl_context structure
6592 */
6593#if defined(MBEDTLS_SSL_DTLS_BADMAC_LIMIT)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01006594 if ((size_t) (end - p) < 4) {
6595 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
6596 }
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02006597
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01006598 ssl->badmac_seen = ((uint32_t) p[0] << 24) |
6599 ((uint32_t) p[1] << 16) |
6600 ((uint32_t) p[2] << 8) |
6601 ((uint32_t) p[3]);
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02006602 p += 4;
6603#endif /* MBEDTLS_SSL_DTLS_BADMAC_LIMIT */
6604
6605#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01006606 if ((size_t) (end - p) < 16) {
6607 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
6608 }
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02006609
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01006610 ssl->in_window_top = ((uint64_t) p[0] << 56) |
6611 ((uint64_t) p[1] << 48) |
6612 ((uint64_t) p[2] << 40) |
6613 ((uint64_t) p[3] << 32) |
6614 ((uint64_t) p[4] << 24) |
6615 ((uint64_t) p[5] << 16) |
6616 ((uint64_t) p[6] << 8) |
6617 ((uint64_t) p[7]);
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02006618 p += 8;
6619
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01006620 ssl->in_window = ((uint64_t) p[0] << 56) |
6621 ((uint64_t) p[1] << 48) |
6622 ((uint64_t) p[2] << 40) |
6623 ((uint64_t) p[3] << 32) |
6624 ((uint64_t) p[4] << 24) |
6625 ((uint64_t) p[5] << 16) |
6626 ((uint64_t) p[6] << 8) |
6627 ((uint64_t) p[7]);
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02006628 p += 8;
6629#endif /* MBEDTLS_SSL_DTLS_ANTI_REPLAY */
6630
6631#if defined(MBEDTLS_SSL_PROTO_DTLS)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01006632 if ((size_t) (end - p) < 1) {
6633 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
6634 }
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02006635
6636 ssl->disable_datagram_packing = *p++;
6637#endif /* MBEDTLS_SSL_PROTO_DTLS */
6638
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01006639 if ((size_t) (end - p) < 8) {
6640 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
6641 }
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02006642
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01006643 memcpy(ssl->cur_out_ctr, p, 8);
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02006644 p += 8;
6645
6646#if defined(MBEDTLS_SSL_PROTO_DTLS)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01006647 if ((size_t) (end - p) < 2) {
6648 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
6649 }
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02006650
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01006651 ssl->mtu = (p[0] << 8) | p[1];
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02006652 p += 2;
6653#endif /* MBEDTLS_SSL_PROTO_DTLS */
6654
6655#if defined(MBEDTLS_SSL_ALPN)
6656 {
6657 uint8_t alpn_len;
6658 const char **cur;
6659
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01006660 if ((size_t) (end - p) < 1) {
6661 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
6662 }
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02006663
6664 alpn_len = *p++;
6665
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01006666 if (alpn_len != 0 && ssl->conf->alpn_list != NULL) {
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02006667 /* alpn_chosen should point to an item in the configured list */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01006668 for (cur = ssl->conf->alpn_list; *cur != NULL; cur++) {
6669 if (strlen(*cur) == alpn_len &&
6670 memcmp(p, cur, alpn_len) == 0) {
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02006671 ssl->alpn_chosen = *cur;
6672 break;
6673 }
6674 }
6675 }
6676
6677 /* can only happen on conf mismatch */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01006678 if (alpn_len != 0 && ssl->alpn_chosen == NULL) {
6679 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
6680 }
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02006681
6682 p += alpn_len;
6683 }
6684#endif /* MBEDTLS_SSL_ALPN */
6685
6686 /*
Manuel Pégourié-Gonnard0eb3eac2019-07-15 11:53:51 +02006687 * Forced fields from top-level ssl_context structure
6688 *
6689 * Most of them already set to the correct value by mbedtls_ssl_init() and
6690 * mbedtls_ssl_reset(), so we only need to set the remaining ones.
6691 */
6692 ssl->state = MBEDTLS_SSL_HANDSHAKE_OVER;
6693
6694 ssl->major_ver = MBEDTLS_SSL_MAJOR_VERSION_3;
6695 ssl->minor_ver = MBEDTLS_SSL_MINOR_VERSION_3;
6696
Hanno Becker361b10d2019-08-30 10:42:49 +01006697 /* Adjust pointers for header fields of outgoing records to
6698 * the given transform, accounting for explicit IV and CID. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01006699 mbedtls_ssl_update_out_pointers(ssl, ssl->transform);
Hanno Becker361b10d2019-08-30 10:42:49 +01006700
Manuel Pégourié-Gonnard0eb3eac2019-07-15 11:53:51 +02006701#if defined(MBEDTLS_SSL_PROTO_DTLS)
6702 ssl->in_epoch = 1;
6703#endif
6704
6705 /* mbedtls_ssl_reset() leaves the handshake sub-structure allocated,
6706 * which we don't want - otherwise we'd end up freeing the wrong transform
Hanno Beckerce5f5fd2020-02-05 10:47:44 +00006707 * by calling mbedtls_ssl_handshake_wrapup_free_hs_transform()
6708 * inappropriately. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01006709 if (ssl->handshake != NULL) {
6710 mbedtls_ssl_handshake_free(ssl);
6711 mbedtls_free(ssl->handshake);
Manuel Pégourié-Gonnard0eb3eac2019-07-15 11:53:51 +02006712 ssl->handshake = NULL;
6713 }
6714
6715 /*
Manuel Pégourié-Gonnard4c90e852019-07-11 10:58:10 +02006716 * Done - should have consumed entire buffer
6717 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01006718 if (p != end) {
6719 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
6720 }
Manuel Pégourié-Gonnardac87e282019-05-28 13:02:16 +02006721
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01006722 return 0;
Manuel Pégourié-Gonnardac87e282019-05-28 13:02:16 +02006723}
6724
6725/*
Manuel Pégourié-Gonnardb9dfc9f2019-07-12 10:50:19 +02006726 * Deserialize context: public wrapper for error cleaning
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02006727 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01006728int mbedtls_ssl_context_load(mbedtls_ssl_context *context,
6729 const unsigned char *buf,
6730 size_t len)
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02006731{
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01006732 int ret = ssl_context_load(context, buf, len);
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02006733
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01006734 if (ret != 0) {
6735 mbedtls_ssl_free(context);
6736 }
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02006737
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01006738 return ret;
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02006739}
Manuel Pégourié-Gonnard5c0e3772019-07-23 16:13:17 +02006740#endif /* MBEDTLS_SSL_CONTEXT_SERIALIZATION */
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02006741
6742/*
Paul Bakker5121ce52009-01-03 21:22:43 +00006743 * Free an SSL context
6744 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01006745void mbedtls_ssl_free(mbedtls_ssl_context *ssl)
Paul Bakker5121ce52009-01-03 21:22:43 +00006746{
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01006747 if (ssl == NULL) {
Paul Bakkeraccaffe2014-06-26 13:37:14 +02006748 return;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01006749 }
Paul Bakkeraccaffe2014-06-26 13:37:14 +02006750
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01006751 MBEDTLS_SSL_DEBUG_MSG(2, ("=> free"));
Paul Bakker5121ce52009-01-03 21:22:43 +00006752
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01006753 if (ssl->out_buf != NULL) {
sander-visserb8aa2072020-05-06 22:05:13 +02006754#if defined(MBEDTLS_SSL_VARIABLE_BUFFER_LENGTH)
6755 size_t out_buf_len = ssl->out_buf_len;
6756#else
6757 size_t out_buf_len = MBEDTLS_SSL_OUT_BUFFER_LEN;
6758#endif
6759
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01006760 mbedtls_platform_zeroize(ssl->out_buf, out_buf_len);
6761 mbedtls_free(ssl->out_buf);
Andrzej Kurek0afa2a12020-03-03 10:39:58 -05006762 ssl->out_buf = NULL;
Paul Bakker5121ce52009-01-03 21:22:43 +00006763 }
6764
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01006765 if (ssl->in_buf != NULL) {
sander-visserb8aa2072020-05-06 22:05:13 +02006766#if defined(MBEDTLS_SSL_VARIABLE_BUFFER_LENGTH)
6767 size_t in_buf_len = ssl->in_buf_len;
6768#else
6769 size_t in_buf_len = MBEDTLS_SSL_IN_BUFFER_LEN;
6770#endif
6771
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01006772 mbedtls_platform_zeroize(ssl->in_buf, in_buf_len);
6773 mbedtls_free(ssl->in_buf);
Andrzej Kurek0afa2a12020-03-03 10:39:58 -05006774 ssl->in_buf = NULL;
Paul Bakker5121ce52009-01-03 21:22:43 +00006775 }
6776
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006777#if defined(MBEDTLS_ZLIB_SUPPORT)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01006778 if (ssl->compress_buf != NULL) {
6779 mbedtls_platform_zeroize(ssl->compress_buf, MBEDTLS_SSL_COMPRESS_BUFFER_LEN);
6780 mbedtls_free(ssl->compress_buf);
Paul Bakker16770332013-10-11 09:59:44 +02006781 }
6782#endif
6783
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01006784 if (ssl->transform) {
6785 mbedtls_ssl_transform_free(ssl->transform);
6786 mbedtls_free(ssl->transform);
Paul Bakker48916f92012-09-16 19:57:18 +00006787 }
6788
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01006789 if (ssl->handshake) {
6790 mbedtls_ssl_handshake_free(ssl);
6791 mbedtls_ssl_transform_free(ssl->transform_negotiate);
6792 mbedtls_ssl_session_free(ssl->session_negotiate);
Paul Bakker48916f92012-09-16 19:57:18 +00006793
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01006794 mbedtls_free(ssl->handshake);
6795 mbedtls_free(ssl->transform_negotiate);
6796 mbedtls_free(ssl->session_negotiate);
Paul Bakker48916f92012-09-16 19:57:18 +00006797 }
6798
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01006799 if (ssl->session) {
6800 mbedtls_ssl_session_free(ssl->session);
6801 mbedtls_free(ssl->session);
Paul Bakkerc0463502013-02-14 11:19:38 +01006802 }
6803
Manuel Pégourié-Gonnard55fab2d2015-05-11 16:15:19 +02006804#if defined(MBEDTLS_X509_CRT_PARSE_C)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01006805 if (ssl->hostname != NULL) {
6806 mbedtls_platform_zeroize(ssl->hostname, strlen(ssl->hostname));
6807 mbedtls_free(ssl->hostname);
Paul Bakker5121ce52009-01-03 21:22:43 +00006808 }
Paul Bakker0be444a2013-08-27 21:55:01 +02006809#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00006810
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006811#if defined(MBEDTLS_SSL_HW_RECORD_ACCEL)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01006812 if (mbedtls_ssl_hw_record_finish != NULL) {
6813 MBEDTLS_SSL_DEBUG_MSG(2, ("going for mbedtls_ssl_hw_record_finish()"));
6814 mbedtls_ssl_hw_record_finish(ssl);
Paul Bakker05ef8352012-05-08 09:17:57 +00006815 }
6816#endif
6817
Manuel Pégourié-Gonnarde057d3b2015-05-20 10:59:43 +02006818#if defined(MBEDTLS_SSL_DTLS_HELLO_VERIFY) && defined(MBEDTLS_SSL_SRV_C)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01006819 mbedtls_free(ssl->cli_id);
Manuel Pégourié-Gonnard43c02182014-07-22 17:32:01 +02006820#endif
6821
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01006822 MBEDTLS_SSL_DEBUG_MSG(2, ("<= free"));
Paul Bakker2da561c2009-02-05 18:00:28 +00006823
Paul Bakker86f04f42013-02-14 11:20:09 +01006824 /* Actually clear after last debug message */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01006825 mbedtls_platform_zeroize(ssl, sizeof(mbedtls_ssl_context));
Paul Bakker5121ce52009-01-03 21:22:43 +00006826}
6827
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02006828/*
Tom Cosgrove49f99bc2022-12-04 16:44:21 +00006829 * Initialize mbedtls_ssl_config
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02006830 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01006831void mbedtls_ssl_config_init(mbedtls_ssl_config *conf)
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02006832{
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01006833 memset(conf, 0, sizeof(mbedtls_ssl_config));
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02006834}
6835
Gilles Peskineeccd8882020-03-10 12:19:08 +01006836#if defined(MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED)
Manuel Pégourié-Gonnard47229c72015-12-04 15:02:56 +01006837static int ssl_preset_default_hashes[] = {
Gilles Peskinec54010c2021-05-12 22:52:09 +02006838#if defined(MBEDTLS_SHA512_C)
Manuel Pégourié-Gonnard47229c72015-12-04 15:02:56 +01006839 MBEDTLS_MD_SHA512,
Gilles Peskinec54010c2021-05-12 22:52:09 +02006840#endif
6841#if defined(MBEDTLS_SHA512_C) && !defined(MBEDTLS_SHA512_NO_SHA384)
Manuel Pégourié-Gonnard47229c72015-12-04 15:02:56 +01006842 MBEDTLS_MD_SHA384,
6843#endif
6844#if defined(MBEDTLS_SHA256_C)
6845 MBEDTLS_MD_SHA256,
6846 MBEDTLS_MD_SHA224,
6847#endif
Gilles Peskine5d2511c2017-05-12 13:16:40 +02006848#if defined(MBEDTLS_SHA1_C) && defined(MBEDTLS_TLS_DEFAULT_ALLOW_SHA1_IN_KEY_EXCHANGE)
Manuel Pégourié-Gonnard47229c72015-12-04 15:02:56 +01006849 MBEDTLS_MD_SHA1,
6850#endif
6851 MBEDTLS_MD_NONE
6852};
Simon Butcherc97b6972015-12-27 23:48:17 +00006853#endif
Manuel Pégourié-Gonnard47229c72015-12-04 15:02:56 +01006854
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02006855static int ssl_preset_suiteb_ciphersuites[] = {
6856 MBEDTLS_TLS_ECDHE_ECDSA_WITH_AES_128_GCM_SHA256,
6857 MBEDTLS_TLS_ECDHE_ECDSA_WITH_AES_256_GCM_SHA384,
6858 0
6859};
6860
Gilles Peskineeccd8882020-03-10 12:19:08 +01006861#if defined(MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED)
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02006862static int ssl_preset_suiteb_hashes[] = {
6863 MBEDTLS_MD_SHA256,
6864 MBEDTLS_MD_SHA384,
6865 MBEDTLS_MD_NONE
6866};
6867#endif
6868
6869#if defined(MBEDTLS_ECP_C)
6870static mbedtls_ecp_group_id ssl_preset_suiteb_curves[] = {
Jaeden Amerod4311042019-06-03 08:27:16 +01006871#if defined(MBEDTLS_ECP_DP_SECP256R1_ENABLED)
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02006872 MBEDTLS_ECP_DP_SECP256R1,
Jaeden Amerod4311042019-06-03 08:27:16 +01006873#endif
6874#if defined(MBEDTLS_ECP_DP_SECP384R1_ENABLED)
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02006875 MBEDTLS_ECP_DP_SECP384R1,
Jaeden Amerod4311042019-06-03 08:27:16 +01006876#endif
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02006877 MBEDTLS_ECP_DP_NONE
6878};
6879#endif
6880
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02006881/*
Tillmann Karras588ad502015-09-25 04:27:22 +02006882 * Load default in mbedtls_ssl_config
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02006883 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01006884int mbedtls_ssl_config_defaults(mbedtls_ssl_config *conf,
6885 int endpoint, int transport, int preset)
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02006886{
Manuel Pégourié-Gonnard8b431fb2015-05-11 12:54:52 +02006887#if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_SSL_SRV_C)
Janos Follath865b3eb2019-12-16 11:46:15 +00006888 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnard8b431fb2015-05-11 12:54:52 +02006889#endif
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02006890
Manuel Pégourié-Gonnard0de074f2015-05-14 12:58:01 +02006891 /* Use the functions here so that they are covered in tests,
6892 * but otherwise access member directly for efficiency */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01006893 mbedtls_ssl_conf_endpoint(conf, endpoint);
6894 mbedtls_ssl_conf_transport(conf, transport);
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02006895
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02006896 /*
6897 * Things that are common to all presets
6898 */
Manuel Pégourié-Gonnard419d5ae2015-05-04 19:32:36 +02006899#if defined(MBEDTLS_SSL_CLI_C)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01006900 if (endpoint == MBEDTLS_SSL_IS_CLIENT) {
Manuel Pégourié-Gonnard419d5ae2015-05-04 19:32:36 +02006901 conf->authmode = MBEDTLS_SSL_VERIFY_REQUIRED;
6902#if defined(MBEDTLS_SSL_SESSION_TICKETS)
6903 conf->session_tickets = MBEDTLS_SSL_SESSION_TICKETS_ENABLED;
6904#endif
6905 }
6906#endif
6907
Manuel Pégourié-Gonnard66dc5552015-05-14 12:28:21 +02006908#if defined(MBEDTLS_ARC4_C)
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02006909 conf->arc4_disabled = MBEDTLS_SSL_ARC4_DISABLED;
Manuel Pégourié-Gonnard66dc5552015-05-14 12:28:21 +02006910#endif
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02006911
6912#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
6913 conf->encrypt_then_mac = MBEDTLS_SSL_ETM_ENABLED;
6914#endif
6915
6916#if defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET)
6917 conf->extended_ms = MBEDTLS_SSL_EXTENDED_MS_ENABLED;
6918#endif
6919
Manuel Pégourié-Gonnard17eab2b2015-05-05 16:34:53 +01006920#if defined(MBEDTLS_SSL_CBC_RECORD_SPLITTING)
6921 conf->cbc_record_splitting = MBEDTLS_SSL_CBC_RECORD_SPLITTING_ENABLED;
6922#endif
6923
Manuel Pégourié-Gonnarde057d3b2015-05-20 10:59:43 +02006924#if defined(MBEDTLS_SSL_DTLS_HELLO_VERIFY) && defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02006925 conf->f_cookie_write = ssl_cookie_write_dummy;
6926 conf->f_cookie_check = ssl_cookie_check_dummy;
6927#endif
6928
6929#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
6930 conf->anti_replay = MBEDTLS_SSL_ANTI_REPLAY_ENABLED;
6931#endif
6932
Janos Follath088ce432017-04-10 12:42:31 +01006933#if defined(MBEDTLS_SSL_SRV_C)
6934 conf->cert_req_ca_list = MBEDTLS_SSL_CERT_REQ_CA_LIST_ENABLED;
6935#endif
6936
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02006937#if defined(MBEDTLS_SSL_PROTO_DTLS)
6938 conf->hs_timeout_min = MBEDTLS_SSL_DTLS_TIMEOUT_DFL_MIN;
6939 conf->hs_timeout_max = MBEDTLS_SSL_DTLS_TIMEOUT_DFL_MAX;
6940#endif
6941
6942#if defined(MBEDTLS_SSL_RENEGOTIATION)
6943 conf->renego_max_records = MBEDTLS_SSL_RENEGO_MAX_RECORDS_DEFAULT;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01006944 memset(conf->renego_period, 0x00, 2);
6945 memset(conf->renego_period + 2, 0xFF, 6);
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02006946#endif
6947
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02006948#if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_SSL_SRV_C)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01006949 if (endpoint == MBEDTLS_SSL_IS_SERVER) {
6950 const unsigned char dhm_p[] =
6951 MBEDTLS_DHM_RFC3526_MODP_2048_P_BIN;
6952 const unsigned char dhm_g[] =
6953 MBEDTLS_DHM_RFC3526_MODP_2048_G_BIN;
Hanno Becker00d0a682017-10-04 13:14:29 +01006954
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01006955 if ((ret = mbedtls_ssl_conf_dh_param_bin(conf,
6956 dhm_p, sizeof(dhm_p),
6957 dhm_g, sizeof(dhm_g))) != 0) {
6958 return ret;
6959 }
6960 }
Manuel Pégourié-Gonnardbd990d62015-06-11 14:49:42 +02006961#endif
6962
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02006963 /*
6964 * Preset-specific defaults
6965 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01006966 switch (preset) {
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02006967 /*
6968 * NSA Suite B
6969 */
6970 case MBEDTLS_SSL_PRESET_SUITEB:
6971 conf->min_major_ver = MBEDTLS_SSL_MAJOR_VERSION_3;
6972 conf->min_minor_ver = MBEDTLS_SSL_MINOR_VERSION_3; /* TLS 1.2 */
6973 conf->max_major_ver = MBEDTLS_SSL_MAX_MAJOR_VERSION;
6974 conf->max_minor_ver = MBEDTLS_SSL_MAX_MINOR_VERSION;
6975
6976 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_0] =
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01006977 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_1] =
6978 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_2] =
6979 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_3] =
6980 ssl_preset_suiteb_ciphersuites;
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02006981
6982#if defined(MBEDTLS_X509_CRT_PARSE_C)
6983 conf->cert_profile = &mbedtls_x509_crt_profile_suiteb;
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02006984#endif
6985
Gilles Peskineeccd8882020-03-10 12:19:08 +01006986#if defined(MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED)
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02006987 conf->sig_hashes = ssl_preset_suiteb_hashes;
6988#endif
6989
6990#if defined(MBEDTLS_ECP_C)
6991 conf->curve_list = ssl_preset_suiteb_curves;
6992#endif
Manuel Pégourié-Gonnardc98204e2015-08-11 04:21:01 +02006993 break;
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02006994
6995 /*
6996 * Default
6997 */
6998 default:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01006999 conf->min_major_ver = (MBEDTLS_SSL_MIN_MAJOR_VERSION >
7000 MBEDTLS_SSL_MIN_VALID_MAJOR_VERSION) ?
7001 MBEDTLS_SSL_MIN_MAJOR_VERSION :
7002 MBEDTLS_SSL_MIN_VALID_MAJOR_VERSION;
7003 conf->min_minor_ver = (MBEDTLS_SSL_MIN_MINOR_VERSION >
7004 MBEDTLS_SSL_MIN_VALID_MINOR_VERSION) ?
7005 MBEDTLS_SSL_MIN_MINOR_VERSION :
7006 MBEDTLS_SSL_MIN_VALID_MINOR_VERSION;
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02007007 conf->max_major_ver = MBEDTLS_SSL_MAX_MAJOR_VERSION;
7008 conf->max_minor_ver = MBEDTLS_SSL_MAX_MINOR_VERSION;
7009
7010#if defined(MBEDTLS_SSL_PROTO_DTLS)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01007011 if (transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM) {
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02007012 conf->min_minor_ver = MBEDTLS_SSL_MINOR_VERSION_2;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01007013 }
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02007014#endif
7015
7016 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_0] =
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01007017 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_1] =
7018 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_2] =
7019 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_3] =
7020 mbedtls_ssl_list_ciphersuites();
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02007021
7022#if defined(MBEDTLS_X509_CRT_PARSE_C)
7023 conf->cert_profile = &mbedtls_x509_crt_profile_default;
7024#endif
7025
Gilles Peskineeccd8882020-03-10 12:19:08 +01007026#if defined(MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED)
Manuel Pégourié-Gonnard47229c72015-12-04 15:02:56 +01007027 conf->sig_hashes = ssl_preset_default_hashes;
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02007028#endif
7029
7030#if defined(MBEDTLS_ECP_C)
7031 conf->curve_list = mbedtls_ecp_grp_id_list();
7032#endif
7033
7034#if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_SSL_CLI_C)
7035 conf->dhm_min_bitlen = 1024;
7036#endif
7037 }
7038
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01007039 return 0;
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02007040}
7041
7042/*
7043 * Free mbedtls_ssl_config
7044 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01007045void mbedtls_ssl_config_free(mbedtls_ssl_config *conf)
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02007046{
7047#if defined(MBEDTLS_DHM_C)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01007048 mbedtls_mpi_free(&conf->dhm_P);
7049 mbedtls_mpi_free(&conf->dhm_G);
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02007050#endif
7051
Gilles Peskineeccd8882020-03-10 12:19:08 +01007052#if defined(MBEDTLS_KEY_EXCHANGE_SOME_PSK_ENABLED)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01007053 if (conf->psk != NULL) {
7054 mbedtls_platform_zeroize(conf->psk, conf->psk_len);
7055 mbedtls_free(conf->psk);
Azim Khan27e8a122018-03-21 14:24:11 +00007056 conf->psk = NULL;
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02007057 conf->psk_len = 0;
junyeonLEE316b1622017-12-20 16:29:30 +09007058 }
7059
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01007060 if (conf->psk_identity != NULL) {
7061 mbedtls_platform_zeroize(conf->psk_identity, conf->psk_identity_len);
7062 mbedtls_free(conf->psk_identity);
Azim Khan27e8a122018-03-21 14:24:11 +00007063 conf->psk_identity = NULL;
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02007064 conf->psk_identity_len = 0;
7065 }
7066#endif
7067
7068#if defined(MBEDTLS_X509_CRT_PARSE_C)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01007069 ssl_key_cert_free(conf->key_cert);
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02007070#endif
7071
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01007072 mbedtls_platform_zeroize(conf, sizeof(mbedtls_ssl_config));
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02007073}
7074
Manuel Pégourié-Gonnard5674a972015-10-19 15:14:03 +02007075#if defined(MBEDTLS_PK_C) && \
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01007076 (defined(MBEDTLS_RSA_C) || defined(MBEDTLS_ECDSA_C))
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02007077/*
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007078 * Convert between MBEDTLS_PK_XXX and SSL_SIG_XXX
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02007079 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01007080unsigned char mbedtls_ssl_sig_from_pk(mbedtls_pk_context *pk)
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02007081{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007082#if defined(MBEDTLS_RSA_C)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01007083 if (mbedtls_pk_can_do(pk, MBEDTLS_PK_RSA)) {
7084 return MBEDTLS_SSL_SIG_RSA;
7085 }
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02007086#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007087#if defined(MBEDTLS_ECDSA_C)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01007088 if (mbedtls_pk_can_do(pk, MBEDTLS_PK_ECDSA)) {
7089 return MBEDTLS_SSL_SIG_ECDSA;
7090 }
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02007091#endif
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01007092 return MBEDTLS_SSL_SIG_ANON;
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02007093}
7094
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01007095unsigned char mbedtls_ssl_sig_from_pk_alg(mbedtls_pk_type_t type)
Hanno Becker7e5437a2017-04-28 17:15:26 +01007096{
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01007097 switch (type) {
Hanno Becker7e5437a2017-04-28 17:15:26 +01007098 case MBEDTLS_PK_RSA:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01007099 return MBEDTLS_SSL_SIG_RSA;
Hanno Becker7e5437a2017-04-28 17:15:26 +01007100 case MBEDTLS_PK_ECDSA:
7101 case MBEDTLS_PK_ECKEY:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01007102 return MBEDTLS_SSL_SIG_ECDSA;
Hanno Becker7e5437a2017-04-28 17:15:26 +01007103 default:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01007104 return MBEDTLS_SSL_SIG_ANON;
Hanno Becker7e5437a2017-04-28 17:15:26 +01007105 }
7106}
7107
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01007108mbedtls_pk_type_t mbedtls_ssl_pk_alg_from_sig(unsigned char sig)
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02007109{
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01007110 switch (sig) {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007111#if defined(MBEDTLS_RSA_C)
7112 case MBEDTLS_SSL_SIG_RSA:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01007113 return MBEDTLS_PK_RSA;
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02007114#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007115#if defined(MBEDTLS_ECDSA_C)
7116 case MBEDTLS_SSL_SIG_ECDSA:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01007117 return MBEDTLS_PK_ECDSA;
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02007118#endif
7119 default:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01007120 return MBEDTLS_PK_NONE;
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02007121 }
7122}
Manuel Pégourié-Gonnard5674a972015-10-19 15:14:03 +02007123#endif /* MBEDTLS_PK_C && ( MBEDTLS_RSA_C || MBEDTLS_ECDSA_C ) */
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02007124
Hanno Becker7e5437a2017-04-28 17:15:26 +01007125#if defined(MBEDTLS_SSL_PROTO_TLS1_2) && \
Gilles Peskineeccd8882020-03-10 12:19:08 +01007126 defined(MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED)
Hanno Becker7e5437a2017-04-28 17:15:26 +01007127
7128/* Find an entry in a signature-hash set matching a given hash algorithm. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01007129mbedtls_md_type_t mbedtls_ssl_sig_hash_set_find(mbedtls_ssl_sig_hash_set_t *set,
7130 mbedtls_pk_type_t sig_alg)
Hanno Becker7e5437a2017-04-28 17:15:26 +01007131{
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01007132 switch (sig_alg) {
Hanno Becker7e5437a2017-04-28 17:15:26 +01007133 case MBEDTLS_PK_RSA:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01007134 return set->rsa;
Hanno Becker7e5437a2017-04-28 17:15:26 +01007135 case MBEDTLS_PK_ECDSA:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01007136 return set->ecdsa;
Hanno Becker7e5437a2017-04-28 17:15:26 +01007137 default:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01007138 return MBEDTLS_MD_NONE;
Hanno Becker7e5437a2017-04-28 17:15:26 +01007139 }
7140}
7141
7142/* Add a signature-hash-pair to a signature-hash set */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01007143void mbedtls_ssl_sig_hash_set_add(mbedtls_ssl_sig_hash_set_t *set,
7144 mbedtls_pk_type_t sig_alg,
7145 mbedtls_md_type_t md_alg)
Hanno Becker7e5437a2017-04-28 17:15:26 +01007146{
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01007147 switch (sig_alg) {
Hanno Becker7e5437a2017-04-28 17:15:26 +01007148 case MBEDTLS_PK_RSA:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01007149 if (set->rsa == MBEDTLS_MD_NONE) {
Hanno Becker7e5437a2017-04-28 17:15:26 +01007150 set->rsa = md_alg;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01007151 }
Hanno Becker7e5437a2017-04-28 17:15:26 +01007152 break;
7153
7154 case MBEDTLS_PK_ECDSA:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01007155 if (set->ecdsa == MBEDTLS_MD_NONE) {
Hanno Becker7e5437a2017-04-28 17:15:26 +01007156 set->ecdsa = md_alg;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01007157 }
Hanno Becker7e5437a2017-04-28 17:15:26 +01007158 break;
7159
7160 default:
7161 break;
7162 }
7163}
7164
7165/* Allow exactly one hash algorithm for each signature. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01007166void mbedtls_ssl_sig_hash_set_const_hash(mbedtls_ssl_sig_hash_set_t *set,
7167 mbedtls_md_type_t md_alg)
Hanno Becker7e5437a2017-04-28 17:15:26 +01007168{
7169 set->rsa = md_alg;
7170 set->ecdsa = md_alg;
7171}
7172
7173#endif /* MBEDTLS_SSL_PROTO_TLS1_2) &&
Gilles Peskineeccd8882020-03-10 12:19:08 +01007174 MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED */
Hanno Becker7e5437a2017-04-28 17:15:26 +01007175
Manuel Pégourié-Gonnard1a483832013-09-20 12:29:15 +02007176/*
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +02007177 * Convert from MBEDTLS_SSL_HASH_XXX to MBEDTLS_MD_XXX
Manuel Pégourié-Gonnard1a483832013-09-20 12:29:15 +02007178 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01007179mbedtls_md_type_t mbedtls_ssl_md_alg_from_hash(unsigned char hash)
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02007180{
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01007181 switch (hash) {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007182#if defined(MBEDTLS_MD5_C)
7183 case MBEDTLS_SSL_HASH_MD5:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01007184 return MBEDTLS_MD_MD5;
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02007185#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007186#if defined(MBEDTLS_SHA1_C)
7187 case MBEDTLS_SSL_HASH_SHA1:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01007188 return MBEDTLS_MD_SHA1;
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02007189#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007190#if defined(MBEDTLS_SHA256_C)
7191 case MBEDTLS_SSL_HASH_SHA224:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01007192 return MBEDTLS_MD_SHA224;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007193 case MBEDTLS_SSL_HASH_SHA256:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01007194 return MBEDTLS_MD_SHA256;
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02007195#endif
Gilles Peskined2d59372021-05-12 22:43:27 +02007196#if defined(MBEDTLS_SHA512_C) && !defined(MBEDTLS_SHA512_NO_SHA384)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007197 case MBEDTLS_SSL_HASH_SHA384:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01007198 return MBEDTLS_MD_SHA384;
Gilles Peskinec54010c2021-05-12 22:52:09 +02007199#endif
7200#if defined(MBEDTLS_SHA512_C)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007201 case MBEDTLS_SSL_HASH_SHA512:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01007202 return MBEDTLS_MD_SHA512;
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02007203#endif
7204 default:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01007205 return MBEDTLS_MD_NONE;
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02007206 }
7207}
7208
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +02007209/*
7210 * Convert from MBEDTLS_MD_XXX to MBEDTLS_SSL_HASH_XXX
7211 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01007212unsigned char mbedtls_ssl_hash_from_md_alg(int md)
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +02007213{
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01007214 switch (md) {
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +02007215#if defined(MBEDTLS_MD5_C)
7216 case MBEDTLS_MD_MD5:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01007217 return MBEDTLS_SSL_HASH_MD5;
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +02007218#endif
7219#if defined(MBEDTLS_SHA1_C)
7220 case MBEDTLS_MD_SHA1:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01007221 return MBEDTLS_SSL_HASH_SHA1;
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +02007222#endif
7223#if defined(MBEDTLS_SHA256_C)
7224 case MBEDTLS_MD_SHA224:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01007225 return MBEDTLS_SSL_HASH_SHA224;
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +02007226 case MBEDTLS_MD_SHA256:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01007227 return MBEDTLS_SSL_HASH_SHA256;
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +02007228#endif
Gilles Peskined2d59372021-05-12 22:43:27 +02007229#if defined(MBEDTLS_SHA512_C) && !defined(MBEDTLS_SHA512_NO_SHA384)
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +02007230 case MBEDTLS_MD_SHA384:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01007231 return MBEDTLS_SSL_HASH_SHA384;
Gilles Peskinec54010c2021-05-12 22:52:09 +02007232#endif
7233#if defined(MBEDTLS_SHA512_C)
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +02007234 case MBEDTLS_MD_SHA512:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01007235 return MBEDTLS_SSL_HASH_SHA512;
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +02007236#endif
7237 default:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01007238 return MBEDTLS_SSL_HASH_NONE;
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +02007239 }
7240}
7241
Manuel Pégourié-Gonnardb541da62015-06-17 11:43:30 +02007242#if defined(MBEDTLS_ECP_C)
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01007243/*
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +02007244 * Check if a curve proposed by the peer is in our list.
Manuel Pégourié-Gonnard9d412d82015-06-17 12:10:46 +02007245 * Return 0 if we're willing to use it, -1 otherwise.
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01007246 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01007247int mbedtls_ssl_check_curve(const mbedtls_ssl_context *ssl, mbedtls_ecp_group_id grp_id)
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01007248{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007249 const mbedtls_ecp_group_id *gid;
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01007250
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01007251 if (ssl->conf->curve_list == NULL) {
7252 return -1;
7253 }
Manuel Pégourié-Gonnard9d412d82015-06-17 12:10:46 +02007254
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01007255 for (gid = ssl->conf->curve_list; *gid != MBEDTLS_ECP_DP_NONE; gid++) {
7256 if (*gid == grp_id) {
7257 return 0;
7258 }
7259 }
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01007260
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01007261 return -1;
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01007262}
Manuel Pégourié-Gonnard298d6cc2022-02-14 11:34:47 +01007263
7264/*
7265 * Same as mbedtls_ssl_check_curve() but takes a TLS ID for the curve.
7266 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01007267int mbedtls_ssl_check_curve_tls_id(const mbedtls_ssl_context *ssl, uint16_t tls_id)
Manuel Pégourié-Gonnard298d6cc2022-02-14 11:34:47 +01007268{
7269 const mbedtls_ecp_curve_info *curve_info =
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01007270 mbedtls_ecp_curve_info_from_tls_id(tls_id);
7271 if (curve_info == NULL) {
7272 return -1;
7273 }
7274 return mbedtls_ssl_check_curve(ssl, curve_info->grp_id);
Manuel Pégourié-Gonnard298d6cc2022-02-14 11:34:47 +01007275}
Manuel Pégourié-Gonnardb541da62015-06-17 11:43:30 +02007276#endif /* MBEDTLS_ECP_C */
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02007277
Gilles Peskineeccd8882020-03-10 12:19:08 +01007278#if defined(MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED)
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +02007279/*
7280 * Check if a hash proposed by the peer is in our list.
7281 * Return 0 if we're willing to use it, -1 otherwise.
7282 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01007283int mbedtls_ssl_check_sig_hash(const mbedtls_ssl_context *ssl,
7284 mbedtls_md_type_t md)
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +02007285{
7286 const int *cur;
7287
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01007288 if (ssl->conf->sig_hashes == NULL) {
7289 return -1;
7290 }
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +02007291
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01007292 for (cur = ssl->conf->sig_hashes; *cur != MBEDTLS_MD_NONE; cur++) {
7293 if (*cur == (int) md) {
7294 return 0;
7295 }
7296 }
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +02007297
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01007298 return -1;
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +02007299}
Gilles Peskineeccd8882020-03-10 12:19:08 +01007300#endif /* MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED */
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +02007301
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007302#if defined(MBEDTLS_X509_CRT_PARSE_C)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01007303int mbedtls_ssl_check_cert_usage(const mbedtls_x509_crt *cert,
7304 const mbedtls_ssl_ciphersuite_t *ciphersuite,
7305 int cert_endpoint,
7306 uint32_t *flags)
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02007307{
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +01007308 int ret = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007309#if defined(MBEDTLS_X509_CHECK_KEY_USAGE)
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02007310 int usage = 0;
7311#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007312#if defined(MBEDTLS_X509_CHECK_EXTENDED_KEY_USAGE)
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +02007313 const char *ext_oid;
7314 size_t ext_len;
7315#endif
7316
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007317#if !defined(MBEDTLS_X509_CHECK_KEY_USAGE) && \
7318 !defined(MBEDTLS_X509_CHECK_EXTENDED_KEY_USAGE)
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +02007319 ((void) cert);
7320 ((void) cert_endpoint);
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +01007321 ((void) flags);
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +02007322#endif
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02007323
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007324#if defined(MBEDTLS_X509_CHECK_KEY_USAGE)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01007325 if (cert_endpoint == MBEDTLS_SSL_IS_SERVER) {
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02007326 /* Server part of the key exchange */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01007327 switch (ciphersuite->key_exchange) {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007328 case MBEDTLS_KEY_EXCHANGE_RSA:
7329 case MBEDTLS_KEY_EXCHANGE_RSA_PSK:
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +01007330 usage = MBEDTLS_X509_KU_KEY_ENCIPHERMENT;
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02007331 break;
7332
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007333 case MBEDTLS_KEY_EXCHANGE_DHE_RSA:
7334 case MBEDTLS_KEY_EXCHANGE_ECDHE_RSA:
7335 case MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA:
7336 usage = MBEDTLS_X509_KU_DIGITAL_SIGNATURE;
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02007337 break;
7338
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007339 case MBEDTLS_KEY_EXCHANGE_ECDH_RSA:
7340 case MBEDTLS_KEY_EXCHANGE_ECDH_ECDSA:
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +01007341 usage = MBEDTLS_X509_KU_KEY_AGREEMENT;
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02007342 break;
7343
7344 /* Don't use default: we want warnings when adding new values */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007345 case MBEDTLS_KEY_EXCHANGE_NONE:
7346 case MBEDTLS_KEY_EXCHANGE_PSK:
7347 case MBEDTLS_KEY_EXCHANGE_DHE_PSK:
7348 case MBEDTLS_KEY_EXCHANGE_ECDHE_PSK:
Manuel Pégourié-Gonnard557535d2015-09-15 17:53:32 +02007349 case MBEDTLS_KEY_EXCHANGE_ECJPAKE:
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02007350 usage = 0;
7351 }
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01007352 } else {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007353 /* Client auth: we only implement rsa_sign and mbedtls_ecdsa_sign for now */
7354 usage = MBEDTLS_X509_KU_DIGITAL_SIGNATURE;
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02007355 }
7356
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01007357 if (mbedtls_x509_crt_check_key_usage(cert, usage) != 0) {
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +01007358 *flags |= MBEDTLS_X509_BADCERT_KEY_USAGE;
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +01007359 ret = -1;
7360 }
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +02007361#else
7362 ((void) ciphersuite);
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007363#endif /* MBEDTLS_X509_CHECK_KEY_USAGE */
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02007364
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007365#if defined(MBEDTLS_X509_CHECK_EXTENDED_KEY_USAGE)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01007366 if (cert_endpoint == MBEDTLS_SSL_IS_SERVER) {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007367 ext_oid = MBEDTLS_OID_SERVER_AUTH;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01007368 ext_len = MBEDTLS_OID_SIZE(MBEDTLS_OID_SERVER_AUTH);
7369 } else {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007370 ext_oid = MBEDTLS_OID_CLIENT_AUTH;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01007371 ext_len = MBEDTLS_OID_SIZE(MBEDTLS_OID_CLIENT_AUTH);
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +02007372 }
7373
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01007374 if (mbedtls_x509_crt_check_extended_key_usage(cert, ext_oid, ext_len) != 0) {
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +01007375 *flags |= MBEDTLS_X509_BADCERT_EXT_KEY_USAGE;
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +01007376 ret = -1;
7377 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007378#endif /* MBEDTLS_X509_CHECK_EXTENDED_KEY_USAGE */
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +02007379
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01007380 return ret;
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02007381}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007382#endif /* MBEDTLS_X509_CRT_PARSE_C */
Manuel Pégourié-Gonnard3a306b92014-04-29 15:11:17 +02007383
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01007384int mbedtls_ssl_set_calc_verify_md(mbedtls_ssl_context *ssl, int md)
Simon Butcher99000142016-10-13 17:21:01 +01007385{
7386#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01007387 if (ssl->minor_ver != MBEDTLS_SSL_MINOR_VERSION_3) {
Simon Butcher99000142016-10-13 17:21:01 +01007388 return MBEDTLS_ERR_SSL_INVALID_VERIFY_HASH;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01007389 }
Simon Butcher99000142016-10-13 17:21:01 +01007390
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01007391 switch (md) {
Simon Butcher99000142016-10-13 17:21:01 +01007392#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1)
7393#if defined(MBEDTLS_MD5_C)
7394 case MBEDTLS_SSL_HASH_MD5:
Janos Follath182013f2016-10-25 10:50:22 +01007395 return MBEDTLS_ERR_SSL_INVALID_VERIFY_HASH;
Simon Butcher99000142016-10-13 17:21:01 +01007396#endif
7397#if defined(MBEDTLS_SHA1_C)
7398 case MBEDTLS_SSL_HASH_SHA1:
7399 ssl->handshake->calc_verify = ssl_calc_verify_tls;
7400 break;
7401#endif
7402#endif /* MBEDTLS_SSL_PROTO_TLS1 || MBEDTLS_SSL_PROTO_TLS1_1 */
Gilles Peskined2d59372021-05-12 22:43:27 +02007403#if defined(MBEDTLS_SHA512_C) && !defined(MBEDTLS_SHA512_NO_SHA384)
Simon Butcher99000142016-10-13 17:21:01 +01007404 case MBEDTLS_SSL_HASH_SHA384:
7405 ssl->handshake->calc_verify = ssl_calc_verify_tls_sha384;
7406 break;
7407#endif
7408#if defined(MBEDTLS_SHA256_C)
7409 case MBEDTLS_SSL_HASH_SHA256:
7410 ssl->handshake->calc_verify = ssl_calc_verify_tls_sha256;
7411 break;
7412#endif
7413 default:
7414 return MBEDTLS_ERR_SSL_INVALID_VERIFY_HASH;
7415 }
7416
7417 return 0;
7418#else /* !MBEDTLS_SSL_PROTO_TLS1_2 */
7419 (void) ssl;
7420 (void) md;
7421
7422 return MBEDTLS_ERR_SSL_INVALID_VERIFY_HASH;
7423#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
7424}
7425
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +01007426#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1) || \
7427 defined(MBEDTLS_SSL_PROTO_TLS1_1)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01007428int mbedtls_ssl_get_key_exchange_md_ssl_tls(mbedtls_ssl_context *ssl,
7429 unsigned char *output,
7430 unsigned char *data, size_t data_len)
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +01007431{
7432 int ret = 0;
7433 mbedtls_md5_context mbedtls_md5;
7434 mbedtls_sha1_context mbedtls_sha1;
7435
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01007436 mbedtls_md5_init(&mbedtls_md5);
7437 mbedtls_sha1_init(&mbedtls_sha1);
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +01007438
7439 /*
7440 * digitally-signed struct {
7441 * opaque md5_hash[16];
7442 * opaque sha_hash[20];
7443 * };
7444 *
7445 * md5_hash
7446 * MD5(ClientHello.random + ServerHello.random
7447 * + ServerParams);
7448 * sha_hash
7449 * SHA(ClientHello.random + ServerHello.random
7450 * + ServerParams);
7451 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01007452 if ((ret = mbedtls_md5_starts_ret(&mbedtls_md5)) != 0) {
7453 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_md5_starts_ret", ret);
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +01007454 goto exit;
7455 }
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01007456 if ((ret = mbedtls_md5_update_ret(&mbedtls_md5,
7457 ssl->handshake->randbytes, 64)) != 0) {
7458 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_md5_update_ret", ret);
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +01007459 goto exit;
7460 }
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01007461 if ((ret = mbedtls_md5_update_ret(&mbedtls_md5, data, data_len)) != 0) {
7462 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_md5_update_ret", ret);
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +01007463 goto exit;
7464 }
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01007465 if ((ret = mbedtls_md5_finish_ret(&mbedtls_md5, output)) != 0) {
7466 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_md5_finish_ret", ret);
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +01007467 goto exit;
7468 }
7469
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01007470 if ((ret = mbedtls_sha1_starts_ret(&mbedtls_sha1)) != 0) {
7471 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_sha1_starts_ret", ret);
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +01007472 goto exit;
7473 }
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01007474 if ((ret = mbedtls_sha1_update_ret(&mbedtls_sha1,
7475 ssl->handshake->randbytes, 64)) != 0) {
7476 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_sha1_update_ret", ret);
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +01007477 goto exit;
7478 }
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01007479 if ((ret = mbedtls_sha1_update_ret(&mbedtls_sha1, data,
7480 data_len)) != 0) {
7481 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_sha1_update_ret", ret);
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +01007482 goto exit;
7483 }
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01007484 if ((ret = mbedtls_sha1_finish_ret(&mbedtls_sha1,
7485 output + 16)) != 0) {
7486 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_sha1_finish_ret", ret);
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +01007487 goto exit;
7488 }
7489
7490exit:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01007491 mbedtls_md5_free(&mbedtls_md5);
7492 mbedtls_sha1_free(&mbedtls_sha1);
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +01007493
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01007494 if (ret != 0) {
7495 mbedtls_ssl_send_alert_message(ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
7496 MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR);
7497 }
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +01007498
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01007499 return ret;
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +01007500
7501}
7502#endif /* MBEDTLS_SSL_PROTO_SSL3 || MBEDTLS_SSL_PROTO_TLS1 || \
7503 MBEDTLS_SSL_PROTO_TLS1_1 */
7504
7505#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1) || \
7506 defined(MBEDTLS_SSL_PROTO_TLS1_2)
Andrzej Kurekd6db9be2019-01-10 05:27:10 -05007507
7508#if defined(MBEDTLS_USE_PSA_CRYPTO)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01007509int mbedtls_ssl_get_key_exchange_md_tls1_2(mbedtls_ssl_context *ssl,
7510 unsigned char *hash, size_t *hashlen,
7511 unsigned char *data, size_t data_len,
7512 mbedtls_md_type_t md_alg)
Andrzej Kurekd6db9be2019-01-10 05:27:10 -05007513{
Andrzej Kurek814feff2019-01-14 04:35:19 -05007514 psa_status_t status;
Jaeden Amero34973232019-02-20 10:32:28 +00007515 psa_hash_operation_t hash_operation = PSA_HASH_OPERATION_INIT;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01007516 psa_algorithm_t hash_alg = mbedtls_psa_translate_md(md_alg);
Andrzej Kurekd6db9be2019-01-10 05:27:10 -05007517
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01007518 MBEDTLS_SSL_DEBUG_MSG(3, ("Perform PSA-based computation of digest of ServerKeyExchange"));
Andrzej Kurek814feff2019-01-14 04:35:19 -05007519
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01007520 if ((status = psa_hash_setup(&hash_operation,
7521 hash_alg)) != PSA_SUCCESS) {
7522 MBEDTLS_SSL_DEBUG_RET(1, "psa_hash_setup", status);
Andrzej Kurekd6db9be2019-01-10 05:27:10 -05007523 goto exit;
7524 }
7525
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01007526 if ((status = psa_hash_update(&hash_operation, ssl->handshake->randbytes,
7527 64)) != PSA_SUCCESS) {
7528 MBEDTLS_SSL_DEBUG_RET(1, "psa_hash_update", status);
Andrzej Kurekd6db9be2019-01-10 05:27:10 -05007529 goto exit;
7530 }
7531
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01007532 if ((status = psa_hash_update(&hash_operation,
7533 data, data_len)) != PSA_SUCCESS) {
7534 MBEDTLS_SSL_DEBUG_RET(1, "psa_hash_update", status);
Andrzej Kurekd6db9be2019-01-10 05:27:10 -05007535 goto exit;
7536 }
7537
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01007538 if ((status = psa_hash_finish(&hash_operation, hash, PSA_HASH_MAX_SIZE,
7539 hashlen)) != PSA_SUCCESS) {
7540 MBEDTLS_SSL_DEBUG_RET(1, "psa_hash_finish", status);
7541 goto exit;
Andrzej Kurekd6db9be2019-01-10 05:27:10 -05007542 }
7543
7544exit:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01007545 if (status != PSA_SUCCESS) {
7546 mbedtls_ssl_send_alert_message(ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
7547 MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR);
7548 switch (status) {
Andrzej Kurekd6db9be2019-01-10 05:27:10 -05007549 case PSA_ERROR_NOT_SUPPORTED:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01007550 return MBEDTLS_ERR_MD_FEATURE_UNAVAILABLE;
Andrzej Kurek814feff2019-01-14 04:35:19 -05007551 case PSA_ERROR_BAD_STATE: /* Intentional fallthrough */
Andrzej Kurekd6db9be2019-01-10 05:27:10 -05007552 case PSA_ERROR_BUFFER_TOO_SMALL:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01007553 return MBEDTLS_ERR_MD_BAD_INPUT_DATA;
Andrzej Kurekd6db9be2019-01-10 05:27:10 -05007554 case PSA_ERROR_INSUFFICIENT_MEMORY:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01007555 return MBEDTLS_ERR_MD_ALLOC_FAILED;
Andrzej Kurekd6db9be2019-01-10 05:27:10 -05007556 default:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01007557 return MBEDTLS_ERR_MD_HW_ACCEL_FAILED;
Andrzej Kurekd6db9be2019-01-10 05:27:10 -05007558 }
7559 }
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01007560 return 0;
Andrzej Kurekd6db9be2019-01-10 05:27:10 -05007561}
7562
7563#else
7564
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01007565int mbedtls_ssl_get_key_exchange_md_tls1_2(mbedtls_ssl_context *ssl,
7566 unsigned char *hash, size_t *hashlen,
7567 unsigned char *data, size_t data_len,
7568 mbedtls_md_type_t md_alg)
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +01007569{
7570 int ret = 0;
7571 mbedtls_md_context_t ctx;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01007572 const mbedtls_md_info_t *md_info = mbedtls_md_info_from_type(md_alg);
7573 *hashlen = mbedtls_md_get_size(md_info);
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +01007574
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01007575 MBEDTLS_SSL_DEBUG_MSG(3, ("Perform mbedtls-based computation of digest of ServerKeyExchange"));
Andrzej Kurek814feff2019-01-14 04:35:19 -05007576
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01007577 mbedtls_md_init(&ctx);
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +01007578
7579 /*
7580 * digitally-signed struct {
7581 * opaque client_random[32];
7582 * opaque server_random[32];
7583 * ServerDHParams params;
7584 * };
7585 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01007586 if ((ret = mbedtls_md_setup(&ctx, md_info, 0)) != 0) {
7587 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_md_setup", ret);
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +01007588 goto exit;
7589 }
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01007590 if ((ret = mbedtls_md_starts(&ctx)) != 0) {
7591 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_md_starts", ret);
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +01007592 goto exit;
7593 }
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01007594 if ((ret = mbedtls_md_update(&ctx, ssl->handshake->randbytes, 64)) != 0) {
7595 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_md_update", ret);
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +01007596 goto exit;
7597 }
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01007598 if ((ret = mbedtls_md_update(&ctx, data, data_len)) != 0) {
7599 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_md_update", ret);
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +01007600 goto exit;
7601 }
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01007602 if ((ret = mbedtls_md_finish(&ctx, hash)) != 0) {
7603 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_md_finish", ret);
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +01007604 goto exit;
7605 }
7606
7607exit:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01007608 mbedtls_md_free(&ctx);
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +01007609
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01007610 if (ret != 0) {
7611 mbedtls_ssl_send_alert_message(ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
7612 MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR);
7613 }
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +01007614
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01007615 return ret;
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +01007616}
Andrzej Kurekd6db9be2019-01-10 05:27:10 -05007617#endif /* MBEDTLS_USE_PSA_CRYPTO */
7618
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +01007619#endif /* MBEDTLS_SSL_PROTO_TLS1 || MBEDTLS_SSL_PROTO_TLS1_1 || \
7620 MBEDTLS_SSL_PROTO_TLS1_2 */
7621
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007622#endif /* MBEDTLS_SSL_TLS_C */