blob: 778b006383d5492039acb38ea84074011e62f2f7 [file] [log] [blame]
Paul Bakker5121ce52009-01-03 21:22:43 +00001/*
Mateusz Starzyk06b07fb2021-02-18 13:55:21 +01002 * TLS shared functions
Paul Bakker5121ce52009-01-03 21:22:43 +00003 *
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/*
Paul Bakker5121ce52009-01-03 21:22:43 +000020 * http://www.ietf.org/rfc/rfc2246.txt
21 * http://www.ietf.org/rfc/rfc4346.txt
22 */
23
Gilles Peskinedb09ef62020-06-03 01:43:33 +020024#include "common.h"
Paul Bakker5121ce52009-01-03 21:22:43 +000025
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020026#if defined(MBEDTLS_SSL_TLS_C)
Paul Bakker5121ce52009-01-03 21:22:43 +000027
Jerry Yub476a442022-01-21 18:14:45 +080028#include <assert.h>
29
SimonBd5800b72016-04-26 07:43:27 +010030#include "mbedtls/platform.h"
SimonBd5800b72016-04-26 07:43:27 +010031
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000032#include "mbedtls/ssl.h"
Ronald Cron9f0fba32022-02-10 16:45:15 +010033#include "ssl_client.h"
Ronald Cron27c85e72022-03-08 11:37:55 +010034#include "ssl_debug_helpers.h"
Chris Jones84a773f2021-03-05 18:38:47 +000035#include "ssl_misc.h"
Andrzej Kurek25f27152022-08-17 16:09:31 -040036
Janos Follath73c616b2019-12-18 15:07:04 +000037#include "mbedtls/debug.h"
38#include "mbedtls/error.h"
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -050039#include "mbedtls/platform_util.h"
Hanno Beckera835da52019-05-16 12:39:07 +010040#include "mbedtls/version.h"
Gabor Mezei765862c2021-10-19 12:22:25 +020041#include "mbedtls/constant_time.h"
Paul Bakker0be444a2013-08-27 21:55:01 +020042
Rich Evans00ab4702015-02-06 13:43:58 +000043#include <string.h>
44
Andrzej Kurekd6db9be2019-01-10 05:27:10 -050045#if defined(MBEDTLS_USE_PSA_CRYPTO)
46#include "mbedtls/psa_util.h"
47#include "psa/crypto.h"
48#endif
Manuel Pégourié-Gonnard07018f92022-09-15 11:29:35 +020049#include "mbedtls/legacy_or_psa.h"
Andrzej Kurekd6db9be2019-01-10 05:27:10 -050050
Janos Follath23bdca02016-10-07 14:47:14 +010051#if defined(MBEDTLS_X509_CRT_PARSE_C)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000052#include "mbedtls/oid.h"
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +020053#endif
54
Ronald Cronad8c17b2022-06-10 17:18:09 +020055#if defined(MBEDTLS_TEST_HOOKS)
56static mbedtls_ssl_chk_buf_ptr_args chk_buf_ptr_fail_args;
57
58void mbedtls_ssl_set_chk_buf_ptr_fail_args(
Gilles Peskine449bd832023-01-11 14:50:10 +010059 const uint8_t *cur, const uint8_t *end, size_t need)
Ronald Cronad8c17b2022-06-10 17:18:09 +020060{
61 chk_buf_ptr_fail_args.cur = cur;
62 chk_buf_ptr_fail_args.end = end;
63 chk_buf_ptr_fail_args.need = need;
64}
65
Gilles Peskine449bd832023-01-11 14:50:10 +010066void mbedtls_ssl_reset_chk_buf_ptr_fail_args(void)
Ronald Cronad8c17b2022-06-10 17:18:09 +020067{
Gilles Peskine449bd832023-01-11 14:50:10 +010068 memset(&chk_buf_ptr_fail_args, 0, sizeof(chk_buf_ptr_fail_args));
Ronald Cronad8c17b2022-06-10 17:18:09 +020069}
70
Gilles Peskine449bd832023-01-11 14:50:10 +010071int mbedtls_ssl_cmp_chk_buf_ptr_fail_args(mbedtls_ssl_chk_buf_ptr_args *args)
Ronald Cronad8c17b2022-06-10 17:18:09 +020072{
Gilles Peskine449bd832023-01-11 14:50:10 +010073 return (chk_buf_ptr_fail_args.cur != args->cur) ||
74 (chk_buf_ptr_fail_args.end != args->end) ||
75 (chk_buf_ptr_fail_args.need != args->need);
Ronald Cronad8c17b2022-06-10 17:18:09 +020076}
77#endif /* MBEDTLS_TEST_HOOKS */
78
Manuel Pégourié-Gonnard286a1362015-05-13 16:22:05 +020079#if defined(MBEDTLS_SSL_PROTO_DTLS)
Hanno Becker2b1e3542018-08-06 11:19:13 +010080
Hanno Beckera0e20d02019-05-15 14:03:01 +010081#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Beckerf8542cf2019-04-09 15:22:03 +010082/* Top-level Connection ID API */
83
Gilles Peskine449bd832023-01-11 14:50:10 +010084int mbedtls_ssl_conf_cid(mbedtls_ssl_config *conf,
85 size_t len,
86 int ignore_other_cid)
Hanno Beckerad4a1372019-05-03 13:06:44 +010087{
Gilles Peskine449bd832023-01-11 14:50:10 +010088 if (len > MBEDTLS_SSL_CID_IN_LEN_MAX) {
89 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
90 }
Hanno Beckerad4a1372019-05-03 13:06:44 +010091
Gilles Peskine449bd832023-01-11 14:50:10 +010092 if (ignore_other_cid != MBEDTLS_SSL_UNEXPECTED_CID_FAIL &&
93 ignore_other_cid != MBEDTLS_SSL_UNEXPECTED_CID_IGNORE) {
94 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
Hanno Becker611ac772019-05-14 11:45:26 +010095 }
96
97 conf->ignore_unexpected_cid = ignore_other_cid;
Hanno Beckerad4a1372019-05-03 13:06:44 +010098 conf->cid_len = len;
Gilles Peskine449bd832023-01-11 14:50:10 +010099 return 0;
Hanno Beckerad4a1372019-05-03 13:06:44 +0100100}
101
Gilles Peskine449bd832023-01-11 14:50:10 +0100102int mbedtls_ssl_set_cid(mbedtls_ssl_context *ssl,
103 int enable,
104 unsigned char const *own_cid,
105 size_t own_cid_len)
Hanno Beckerf8542cf2019-04-09 15:22:03 +0100106{
Gilles Peskine449bd832023-01-11 14:50:10 +0100107 if (ssl->conf->transport != MBEDTLS_SSL_TRANSPORT_DATAGRAM) {
108 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
109 }
Hanno Becker76a79ab2019-05-03 14:38:32 +0100110
Hanno Beckerca092242019-04-25 16:01:49 +0100111 ssl->negotiate_cid = enable;
Gilles Peskine449bd832023-01-11 14:50:10 +0100112 if (enable == MBEDTLS_SSL_CID_DISABLED) {
113 MBEDTLS_SSL_DEBUG_MSG(3, ("Disable use of CID extension."));
114 return 0;
Hanno Beckerca092242019-04-25 16:01:49 +0100115 }
Gilles Peskine449bd832023-01-11 14:50:10 +0100116 MBEDTLS_SSL_DEBUG_MSG(3, ("Enable use of CID extension."));
117 MBEDTLS_SSL_DEBUG_BUF(3, "Own CID", own_cid, own_cid_len);
Hanno Beckerca092242019-04-25 16:01:49 +0100118
Gilles Peskine449bd832023-01-11 14:50:10 +0100119 if (own_cid_len != ssl->conf->cid_len) {
120 MBEDTLS_SSL_DEBUG_MSG(3, ("CID length %u does not match CID length %u in config",
121 (unsigned) own_cid_len,
122 (unsigned) ssl->conf->cid_len));
123 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
Hanno Beckerca092242019-04-25 16:01:49 +0100124 }
125
Gilles Peskine449bd832023-01-11 14:50:10 +0100126 memcpy(ssl->own_cid, own_cid, own_cid_len);
Hanno Beckerb7ee0cf2019-04-30 14:07:31 +0100127 /* Truncation is not an issue here because
128 * MBEDTLS_SSL_CID_IN_LEN_MAX at most 255. */
129 ssl->own_cid_len = (uint8_t) own_cid_len;
Hanno Beckerca092242019-04-25 16:01:49 +0100130
Gilles Peskine449bd832023-01-11 14:50:10 +0100131 return 0;
Hanno Beckerf8542cf2019-04-09 15:22:03 +0100132}
133
Gilles Peskine449bd832023-01-11 14:50:10 +0100134int mbedtls_ssl_get_own_cid(mbedtls_ssl_context *ssl,
135 int *enabled,
136 unsigned char own_cid[MBEDTLS_SSL_CID_OUT_LEN_MAX],
137 size_t *own_cid_len)
Paul Elliott0113cf12022-03-11 20:26:47 +0000138{
139 *enabled = MBEDTLS_SSL_CID_DISABLED;
140
Gilles Peskine449bd832023-01-11 14:50:10 +0100141 if (ssl->conf->transport != MBEDTLS_SSL_TRANSPORT_DATAGRAM) {
142 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
143 }
Paul Elliott0113cf12022-03-11 20:26:47 +0000144
145 /* We report MBEDTLS_SSL_CID_DISABLED in case the CID length is
146 * zero as this is indistinguishable from not requesting to use
147 * the CID extension. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100148 if (ssl->own_cid_len == 0 || ssl->negotiate_cid == MBEDTLS_SSL_CID_DISABLED) {
149 return 0;
150 }
Paul Elliott0113cf12022-03-11 20:26:47 +0000151
Gilles Peskine449bd832023-01-11 14:50:10 +0100152 if (own_cid_len != NULL) {
Paul Elliott0113cf12022-03-11 20:26:47 +0000153 *own_cid_len = ssl->own_cid_len;
Gilles Peskine449bd832023-01-11 14:50:10 +0100154 if (own_cid != NULL) {
155 memcpy(own_cid, ssl->own_cid, ssl->own_cid_len);
156 }
Paul Elliott0113cf12022-03-11 20:26:47 +0000157 }
158
159 *enabled = MBEDTLS_SSL_CID_ENABLED;
160
Gilles Peskine449bd832023-01-11 14:50:10 +0100161 return 0;
Paul Elliott0113cf12022-03-11 20:26:47 +0000162}
163
Gilles Peskine449bd832023-01-11 14:50:10 +0100164int mbedtls_ssl_get_peer_cid(mbedtls_ssl_context *ssl,
165 int *enabled,
166 unsigned char peer_cid[MBEDTLS_SSL_CID_OUT_LEN_MAX],
167 size_t *peer_cid_len)
Hanno Beckerf8542cf2019-04-09 15:22:03 +0100168{
Hanno Beckerf8542cf2019-04-09 15:22:03 +0100169 *enabled = MBEDTLS_SSL_CID_DISABLED;
Hanno Beckerb1f89cd2019-04-26 17:08:02 +0100170
Gilles Peskine449bd832023-01-11 14:50:10 +0100171 if (ssl->conf->transport != MBEDTLS_SSL_TRANSPORT_DATAGRAM ||
172 mbedtls_ssl_is_handshake_over(ssl) == 0) {
173 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
Hanno Becker76a79ab2019-05-03 14:38:32 +0100174 }
Hanno Beckerb1f89cd2019-04-26 17:08:02 +0100175
Hanno Beckerc5f24222019-05-03 12:54:52 +0100176 /* We report MBEDTLS_SSL_CID_DISABLED in case the CID extensions
177 * were used, but client and server requested the empty CID.
178 * This is indistinguishable from not using the CID extension
179 * in the first place. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100180 if (ssl->transform_in->in_cid_len == 0 &&
181 ssl->transform_in->out_cid_len == 0) {
182 return 0;
Hanno Beckerb1f89cd2019-04-26 17:08:02 +0100183 }
184
Gilles Peskine449bd832023-01-11 14:50:10 +0100185 if (peer_cid_len != NULL) {
Hanno Becker615ef172019-05-22 16:50:35 +0100186 *peer_cid_len = ssl->transform_in->out_cid_len;
Gilles Peskine449bd832023-01-11 14:50:10 +0100187 if (peer_cid != NULL) {
188 memcpy(peer_cid, ssl->transform_in->out_cid,
189 ssl->transform_in->out_cid_len);
Hanno Becker615ef172019-05-22 16:50:35 +0100190 }
191 }
Hanno Beckerb1f89cd2019-04-26 17:08:02 +0100192
193 *enabled = MBEDTLS_SSL_CID_ENABLED;
194
Gilles Peskine449bd832023-01-11 14:50:10 +0100195 return 0;
Hanno Beckerf8542cf2019-04-09 15:22:03 +0100196}
Hanno Beckera0e20d02019-05-15 14:03:01 +0100197#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Beckerf8542cf2019-04-09 15:22:03 +0100198
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200199#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +0200200
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200201#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
Manuel Pégourié-Gonnard581e6b62013-07-18 12:32:27 +0200202/*
203 * Convert max_fragment_length codes to length.
204 * RFC 6066 says:
205 * enum{
206 * 2^9(1), 2^10(2), 2^11(3), 2^12(4), (255)
207 * } MaxFragmentLength;
208 * and we add 0 -> extension unused
209 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100210static unsigned int ssl_mfl_code_to_length(int mfl)
Manuel Pégourié-Gonnard581e6b62013-07-18 12:32:27 +0200211{
Gilles Peskine449bd832023-01-11 14:50:10 +0100212 switch (mfl) {
213 case MBEDTLS_SSL_MAX_FRAG_LEN_NONE:
214 return MBEDTLS_TLS_EXT_ADV_CONTENT_LEN;
215 case MBEDTLS_SSL_MAX_FRAG_LEN_512:
216 return 512;
217 case MBEDTLS_SSL_MAX_FRAG_LEN_1024:
218 return 1024;
219 case MBEDTLS_SSL_MAX_FRAG_LEN_2048:
220 return 2048;
221 case MBEDTLS_SSL_MAX_FRAG_LEN_4096:
222 return 4096;
223 default:
224 return MBEDTLS_TLS_EXT_ADV_CONTENT_LEN;
Angus Grattond8213d02016-05-25 20:56:48 +1000225 }
226}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200227#endif /* MBEDTLS_SSL_MAX_FRAGMENT_LENGTH */
Manuel Pégourié-Gonnard581e6b62013-07-18 12:32:27 +0200228
Gilles Peskine449bd832023-01-11 14:50:10 +0100229int mbedtls_ssl_session_copy(mbedtls_ssl_session *dst,
230 const mbedtls_ssl_session *src)
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200231{
Gilles Peskine449bd832023-01-11 14:50:10 +0100232 mbedtls_ssl_session_free(dst);
233 memcpy(dst, src, sizeof(mbedtls_ssl_session));
吴敬辉0b716112021-11-29 10:46:35 +0800234#if defined(MBEDTLS_SSL_SESSION_TICKETS) && defined(MBEDTLS_SSL_CLI_C)
235 dst->ticket = NULL;
Xiaokang Qian87306442022-10-12 09:47:38 +0000236#if defined(MBEDTLS_SSL_PROTO_TLS1_3) && \
237 defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
Xiaokang Qian126bf8e2022-10-13 02:22:40 +0000238 dst->hostname = NULL;
吴敬辉0b716112021-11-29 10:46:35 +0800239#endif
Xiaokang Qian87306442022-10-12 09:47:38 +0000240#endif /* MBEDTLS_SSL_SESSION_TICKETS && MBEDTLS_SSL_CLI_C */
吴敬辉0b716112021-11-29 10:46:35 +0800241
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200242#if defined(MBEDTLS_X509_CRT_PARSE_C)
Hanno Becker6d1986e2019-02-07 12:27:42 +0000243
244#if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
Gilles Peskine449bd832023-01-11 14:50:10 +0100245 if (src->peer_cert != NULL) {
Janos Follath865b3eb2019-12-16 11:46:15 +0000246 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker2292d1f2013-09-15 17:06:49 +0200247
Gilles Peskine449bd832023-01-11 14:50:10 +0100248 dst->peer_cert = mbedtls_calloc(1, sizeof(mbedtls_x509_crt));
249 if (dst->peer_cert == NULL) {
250 return MBEDTLS_ERR_SSL_ALLOC_FAILED;
251 }
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200252
Gilles Peskine449bd832023-01-11 14:50:10 +0100253 mbedtls_x509_crt_init(dst->peer_cert);
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200254
Gilles Peskine449bd832023-01-11 14:50:10 +0100255 if ((ret = mbedtls_x509_crt_parse_der(dst->peer_cert, src->peer_cert->raw.p,
256 src->peer_cert->raw.len)) != 0) {
257 mbedtls_free(dst->peer_cert);
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200258 dst->peer_cert = NULL;
Gilles Peskine449bd832023-01-11 14:50:10 +0100259 return ret;
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200260 }
261 }
Hanno Becker6d1986e2019-02-07 12:27:42 +0000262#else /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
Gilles Peskine449bd832023-01-11 14:50:10 +0100263 if (src->peer_cert_digest != NULL) {
Hanno Becker9198ad12019-02-05 17:00:50 +0000264 dst->peer_cert_digest =
Gilles Peskine449bd832023-01-11 14:50:10 +0100265 mbedtls_calloc(1, src->peer_cert_digest_len);
266 if (dst->peer_cert_digest == NULL) {
267 return MBEDTLS_ERR_SSL_ALLOC_FAILED;
268 }
Hanno Becker9198ad12019-02-05 17:00:50 +0000269
Gilles Peskine449bd832023-01-11 14:50:10 +0100270 memcpy(dst->peer_cert_digest, src->peer_cert_digest,
271 src->peer_cert_digest_len);
Hanno Becker9198ad12019-02-05 17:00:50 +0000272 dst->peer_cert_digest_type = src->peer_cert_digest_type;
Hanno Beckeraccc5992019-02-25 10:06:59 +0000273 dst->peer_cert_digest_len = src->peer_cert_digest_len;
Hanno Becker9198ad12019-02-05 17:00:50 +0000274 }
275#endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
276
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200277#endif /* MBEDTLS_X509_CRT_PARSE_C */
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200278
Manuel Pégourié-Gonnardb596abf2015-05-20 10:45:29 +0200279#if defined(MBEDTLS_SSL_SESSION_TICKETS) && defined(MBEDTLS_SSL_CLI_C)
Gilles Peskine449bd832023-01-11 14:50:10 +0100280 if (src->ticket != NULL) {
281 dst->ticket = mbedtls_calloc(1, src->ticket_len);
282 if (dst->ticket == NULL) {
283 return MBEDTLS_ERR_SSL_ALLOC_FAILED;
284 }
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200285
Gilles Peskine449bd832023-01-11 14:50:10 +0100286 memcpy(dst->ticket, src->ticket, src->ticket_len);
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200287 }
Xiaokang Qian126bf8e2022-10-13 02:22:40 +0000288
289#if defined(MBEDTLS_SSL_PROTO_TLS1_3) && \
290 defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
Gilles Peskine449bd832023-01-11 14:50:10 +0100291 if (src->endpoint == MBEDTLS_SSL_IS_CLIENT) {
Xiaokang Qian126bf8e2022-10-13 02:22:40 +0000292 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Gilles Peskine449bd832023-01-11 14:50:10 +0100293 ret = mbedtls_ssl_session_set_hostname(dst, src->hostname);
294 if (ret != 0) {
295 return ret;
296 }
Xiaokang Qian126bf8e2022-10-13 02:22:40 +0000297 }
298#endif /* MBEDTLS_SSL_PROTO_TLS1_3 &&
299 MBEDTLS_SSL_SERVER_NAME_INDICATION */
Manuel Pégourié-Gonnardb596abf2015-05-20 10:45:29 +0200300#endif /* MBEDTLS_SSL_SESSION_TICKETS && MBEDTLS_SSL_CLI_C */
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200301
Gilles Peskine449bd832023-01-11 14:50:10 +0100302 return 0;
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200303}
304
Andrzej Kurek0afa2a12020-03-03 10:39:58 -0500305#if defined(MBEDTLS_SSL_VARIABLE_BUFFER_LENGTH)
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +0200306MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +0100307static int resize_buffer(unsigned char **buffer, size_t len_new, size_t *len_old)
Andrzej Kurek0afa2a12020-03-03 10:39:58 -0500308{
Gilles Peskine449bd832023-01-11 14:50:10 +0100309 unsigned char *resized_buffer = mbedtls_calloc(1, len_new);
310 if (resized_buffer == NULL) {
Andrzej Kurek0afa2a12020-03-03 10:39:58 -0500311 return -1;
Gilles Peskine449bd832023-01-11 14:50:10 +0100312 }
Andrzej Kurek0afa2a12020-03-03 10:39:58 -0500313
314 /* We want to copy len_new bytes when downsizing the buffer, and
315 * len_old bytes when upsizing, so we choose the smaller of two sizes,
316 * to fit one buffer into another. Size checks, ensuring that no data is
317 * lost, are done outside of this function. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100318 memcpy(resized_buffer, *buffer,
319 (len_new < *len_old) ? len_new : *len_old);
320 mbedtls_platform_zeroize(*buffer, *len_old);
321 mbedtls_free(*buffer);
Andrzej Kurek0afa2a12020-03-03 10:39:58 -0500322
323 *buffer = resized_buffer;
324 *len_old = len_new;
325
326 return 0;
327}
Andrzej Kurek4a063792020-10-21 15:08:44 +0200328
Gilles Peskine449bd832023-01-11 14:50:10 +0100329static void handle_buffer_resizing(mbedtls_ssl_context *ssl, int downsizing,
330 size_t in_buf_new_len,
331 size_t out_buf_new_len)
Andrzej Kurek4a063792020-10-21 15:08:44 +0200332{
333 int modified = 0;
334 size_t written_in = 0, iv_offset_in = 0, len_offset_in = 0;
335 size_t written_out = 0, iv_offset_out = 0, len_offset_out = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +0100336 if (ssl->in_buf != NULL) {
Andrzej Kurek4a063792020-10-21 15:08:44 +0200337 written_in = ssl->in_msg - ssl->in_buf;
338 iv_offset_in = ssl->in_iv - ssl->in_buf;
339 len_offset_in = ssl->in_len - ssl->in_buf;
Gilles Peskine449bd832023-01-11 14:50:10 +0100340 if (downsizing ?
Andrzej Kurek4a063792020-10-21 15:08:44 +0200341 ssl->in_buf_len > in_buf_new_len && ssl->in_left < in_buf_new_len :
Gilles Peskine449bd832023-01-11 14:50:10 +0100342 ssl->in_buf_len < in_buf_new_len) {
343 if (resize_buffer(&ssl->in_buf, in_buf_new_len, &ssl->in_buf_len) != 0) {
344 MBEDTLS_SSL_DEBUG_MSG(1, ("input buffer resizing failed - out of memory"));
345 } else {
346 MBEDTLS_SSL_DEBUG_MSG(2, ("Reallocating in_buf to %" MBEDTLS_PRINTF_SIZET,
347 in_buf_new_len));
Andrzej Kurek4a063792020-10-21 15:08:44 +0200348 modified = 1;
349 }
350 }
351 }
352
Gilles Peskine449bd832023-01-11 14:50:10 +0100353 if (ssl->out_buf != NULL) {
Andrzej Kurek4a063792020-10-21 15:08:44 +0200354 written_out = ssl->out_msg - ssl->out_buf;
355 iv_offset_out = ssl->out_iv - ssl->out_buf;
356 len_offset_out = ssl->out_len - ssl->out_buf;
Gilles Peskine449bd832023-01-11 14:50:10 +0100357 if (downsizing ?
Andrzej Kurek4a063792020-10-21 15:08:44 +0200358 ssl->out_buf_len > out_buf_new_len && ssl->out_left < out_buf_new_len :
Gilles Peskine449bd832023-01-11 14:50:10 +0100359 ssl->out_buf_len < out_buf_new_len) {
360 if (resize_buffer(&ssl->out_buf, out_buf_new_len, &ssl->out_buf_len) != 0) {
361 MBEDTLS_SSL_DEBUG_MSG(1, ("output buffer resizing failed - out of memory"));
362 } else {
363 MBEDTLS_SSL_DEBUG_MSG(2, ("Reallocating out_buf to %" MBEDTLS_PRINTF_SIZET,
364 out_buf_new_len));
Andrzej Kurek4a063792020-10-21 15:08:44 +0200365 modified = 1;
366 }
367 }
368 }
Gilles Peskine449bd832023-01-11 14:50:10 +0100369 if (modified) {
Andrzej Kurek4a063792020-10-21 15:08:44 +0200370 /* Update pointers here to avoid doing it twice. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100371 mbedtls_ssl_reset_in_out_pointers(ssl);
Andrzej Kurek4a063792020-10-21 15:08:44 +0200372 /* Fields below might not be properly updated with record
373 * splitting or with CID, so they are manually updated here. */
374 ssl->out_msg = ssl->out_buf + written_out;
375 ssl->out_len = ssl->out_buf + len_offset_out;
376 ssl->out_iv = ssl->out_buf + iv_offset_out;
377
378 ssl->in_msg = ssl->in_buf + written_in;
379 ssl->in_len = ssl->in_buf + len_offset_in;
380 ssl->in_iv = ssl->in_buf + iv_offset_in;
381 }
382}
Andrzej Kurek0afa2a12020-03-03 10:39:58 -0500383#endif /* MBEDTLS_SSL_VARIABLE_BUFFER_LENGTH */
384
Jerry Yudb8c48a2022-01-27 14:54:54 +0800385#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
Jerry Yued14c932022-02-17 13:40:45 +0800386
387#if defined(MBEDTLS_SSL_CONTEXT_SERIALIZATION)
Gilles Peskine449bd832023-01-11 14:50:10 +0100388typedef int (*tls_prf_fn)(const unsigned char *secret, size_t slen,
389 const char *label,
390 const unsigned char *random, size_t rlen,
391 unsigned char *dstbuf, size_t dlen);
Jerry Yued14c932022-02-17 13:40:45 +0800392
Gilles Peskine449bd832023-01-11 14:50:10 +0100393static tls_prf_fn ssl_tls12prf_from_cs(int ciphersuite_id);
Jerry Yued14c932022-02-17 13:40:45 +0800394
395#endif /* MBEDTLS_SSL_CONTEXT_SERIALIZATION */
396
397/* Type for the TLS PRF */
398typedef int ssl_tls_prf_t(const unsigned char *, size_t, const char *,
399 const unsigned char *, size_t,
400 unsigned char *, size_t);
401
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +0200402MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +0100403static int ssl_tls12_populate_transform(mbedtls_ssl_transform *transform,
404 int ciphersuite,
405 const unsigned char master[48],
Neil Armstrongf2c82f02022-04-05 11:16:53 +0200406#if defined(MBEDTLS_SSL_SOME_SUITES_USE_CBC_ETM)
Gilles Peskine449bd832023-01-11 14:50:10 +0100407 int encrypt_then_mac,
Neil Armstrongf2c82f02022-04-05 11:16:53 +0200408#endif /* MBEDTLS_SSL_SOME_SUITES_USE_CBC_ETM */
Gilles Peskine449bd832023-01-11 14:50:10 +0100409 ssl_tls_prf_t tls_prf,
410 const unsigned char randbytes[64],
411 mbedtls_ssl_protocol_version tls_version,
412 unsigned endpoint,
413 const mbedtls_ssl_context *ssl);
Jerry Yued14c932022-02-17 13:40:45 +0800414
Andrzej Kurek25f27152022-08-17 16:09:31 -0400415#if defined(MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +0200416MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +0100417static int tls_prf_sha256(const unsigned char *secret, size_t slen,
Ron Eldor51d3ab52019-05-12 14:54:30 +0300418 const char *label,
419 const unsigned char *random, size_t rlen,
Gilles Peskine449bd832023-01-11 14:50:10 +0100420 unsigned char *dstbuf, size_t dlen);
Manuel Pégourié-Gonnard226aa152023-02-05 09:46:59 +0100421static int ssl_calc_verify_tls_sha256(const mbedtls_ssl_context *, unsigned char *, size_t *);
422static int ssl_calc_finished_tls_sha256(mbedtls_ssl_context *, unsigned char *, int);
Gilles Peskine449bd832023-01-11 14:50:10 +0100423
424#endif /* MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA*/
425
426#if defined(MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
427MBEDTLS_CHECK_RETURN_CRITICAL
428static int tls_prf_sha384(const unsigned char *secret, size_t slen,
429 const char *label,
430 const unsigned char *random, size_t rlen,
431 unsigned char *dstbuf, size_t dlen);
432
Manuel Pégourié-Gonnard226aa152023-02-05 09:46:59 +0100433static int ssl_calc_verify_tls_sha384(const mbedtls_ssl_context *, unsigned char *, size_t *);
434static int ssl_calc_finished_tls_sha384(mbedtls_ssl_context *, unsigned char *, int);
Gilles Peskine449bd832023-01-11 14:50:10 +0100435#endif /* MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA*/
436
437static size_t ssl_tls12_session_save(const mbedtls_ssl_session *session,
438 unsigned char *buf,
439 size_t buf_len);
440
441MBEDTLS_CHECK_RETURN_CRITICAL
442static int ssl_tls12_session_load(mbedtls_ssl_session *session,
443 const unsigned char *buf,
444 size_t len);
445#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
446
Manuel Pégourié-Gonnard226aa152023-02-05 09:46:59 +0100447static int ssl_update_checksum_start(mbedtls_ssl_context *, const unsigned char *, size_t);
Gilles Peskine449bd832023-01-11 14:50:10 +0100448
449#if defined(MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Manuel Pégourié-Gonnard226aa152023-02-05 09:46:59 +0100450static int ssl_update_checksum_sha256(mbedtls_ssl_context *, const unsigned char *, size_t);
Gilles Peskine449bd832023-01-11 14:50:10 +0100451#endif /* MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA*/
452
453#if defined(MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Manuel Pégourié-Gonnard226aa152023-02-05 09:46:59 +0100454static int ssl_update_checksum_sha384(mbedtls_ssl_context *, const unsigned char *, size_t);
Gilles Peskine449bd832023-01-11 14:50:10 +0100455#endif /* MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA*/
456
457int mbedtls_ssl_tls_prf(const mbedtls_tls_prf_types prf,
458 const unsigned char *secret, size_t slen,
459 const char *label,
460 const unsigned char *random, size_t rlen,
461 unsigned char *dstbuf, size_t dlen)
Ron Eldor51d3ab52019-05-12 14:54:30 +0300462{
463 mbedtls_ssl_tls_prf_cb *tls_prf = NULL;
464
Gilles Peskine449bd832023-01-11 14:50:10 +0100465 switch (prf) {
Ron Eldord2f25f72019-05-15 14:54:22 +0300466#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
Andrzej Kurek25f27152022-08-17 16:09:31 -0400467#if defined(MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Ron Eldor51d3ab52019-05-12 14:54:30 +0300468 case MBEDTLS_SSL_TLS_PRF_SHA384:
469 tls_prf = tls_prf_sha384;
Gilles Peskine449bd832023-01-11 14:50:10 +0100470 break;
Andrzej Kurekcccb0442022-08-19 03:42:11 -0400471#endif /* MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA*/
Andrzej Kurek25f27152022-08-17 16:09:31 -0400472#if defined(MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Ron Eldor51d3ab52019-05-12 14:54:30 +0300473 case MBEDTLS_SSL_TLS_PRF_SHA256:
474 tls_prf = tls_prf_sha256;
Gilles Peskine449bd832023-01-11 14:50:10 +0100475 break;
Andrzej Kurekcccb0442022-08-19 03:42:11 -0400476#endif /* MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA*/
Ron Eldord2f25f72019-05-15 14:54:22 +0300477#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100478 default:
479 return MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
Ron Eldor51d3ab52019-05-12 14:54:30 +0300480 }
481
Gilles Peskine449bd832023-01-11 14:50:10 +0100482 return tls_prf(secret, slen, label, random, rlen, dstbuf, dlen);
Ron Eldor51d3ab52019-05-12 14:54:30 +0300483}
484
Jerry Yuc73c6182022-02-08 20:29:25 +0800485#if defined(MBEDTLS_X509_CRT_PARSE_C)
Gilles Peskine449bd832023-01-11 14:50:10 +0100486static void ssl_clear_peer_cert(mbedtls_ssl_session *session)
Jerry Yuc73c6182022-02-08 20:29:25 +0800487{
488#if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
Gilles Peskine449bd832023-01-11 14:50:10 +0100489 if (session->peer_cert != NULL) {
490 mbedtls_x509_crt_free(session->peer_cert);
491 mbedtls_free(session->peer_cert);
Jerry Yuc73c6182022-02-08 20:29:25 +0800492 session->peer_cert = NULL;
493 }
494#else /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
Gilles Peskine449bd832023-01-11 14:50:10 +0100495 if (session->peer_cert_digest != NULL) {
Jerry Yuc73c6182022-02-08 20:29:25 +0800496 /* Zeroization is not necessary. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100497 mbedtls_free(session->peer_cert_digest);
Jerry Yuc73c6182022-02-08 20:29:25 +0800498 session->peer_cert_digest = NULL;
499 session->peer_cert_digest_type = MBEDTLS_MD_NONE;
500 session->peer_cert_digest_len = 0;
501 }
502#endif /* !MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
503}
504#endif /* MBEDTLS_X509_CRT_PARSE_C */
505
Gilles Peskine449bd832023-01-11 14:50:10 +0100506uint32_t mbedtls_ssl_get_extension_id(unsigned int extension_type)
Jerry Yu7a485c12022-10-31 13:08:18 +0800507{
Gilles Peskine449bd832023-01-11 14:50:10 +0100508 switch (extension_type) {
Jerry Yu7a485c12022-10-31 13:08:18 +0800509 case MBEDTLS_TLS_EXT_SERVERNAME:
Gilles Peskine449bd832023-01-11 14:50:10 +0100510 return MBEDTLS_SSL_EXT_ID_SERVERNAME;
Jerry Yu7a485c12022-10-31 13:08:18 +0800511
512 case MBEDTLS_TLS_EXT_MAX_FRAGMENT_LENGTH:
Gilles Peskine449bd832023-01-11 14:50:10 +0100513 return MBEDTLS_SSL_EXT_ID_MAX_FRAGMENT_LENGTH;
Jerry Yu7a485c12022-10-31 13:08:18 +0800514
515 case MBEDTLS_TLS_EXT_STATUS_REQUEST:
Gilles Peskine449bd832023-01-11 14:50:10 +0100516 return MBEDTLS_SSL_EXT_ID_STATUS_REQUEST;
Jerry Yu7a485c12022-10-31 13:08:18 +0800517
518 case MBEDTLS_TLS_EXT_SUPPORTED_GROUPS:
Gilles Peskine449bd832023-01-11 14:50:10 +0100519 return MBEDTLS_SSL_EXT_ID_SUPPORTED_GROUPS;
Jerry Yu7a485c12022-10-31 13:08:18 +0800520
521 case MBEDTLS_TLS_EXT_SIG_ALG:
Gilles Peskine449bd832023-01-11 14:50:10 +0100522 return MBEDTLS_SSL_EXT_ID_SIG_ALG;
Jerry Yu7a485c12022-10-31 13:08:18 +0800523
524 case MBEDTLS_TLS_EXT_USE_SRTP:
Gilles Peskine449bd832023-01-11 14:50:10 +0100525 return MBEDTLS_SSL_EXT_ID_USE_SRTP;
Jerry Yu7a485c12022-10-31 13:08:18 +0800526
527 case MBEDTLS_TLS_EXT_HEARTBEAT:
Gilles Peskine449bd832023-01-11 14:50:10 +0100528 return MBEDTLS_SSL_EXT_ID_HEARTBEAT;
Jerry Yu7a485c12022-10-31 13:08:18 +0800529
530 case MBEDTLS_TLS_EXT_ALPN:
Gilles Peskine449bd832023-01-11 14:50:10 +0100531 return MBEDTLS_SSL_EXT_ID_ALPN;
Jerry Yu7a485c12022-10-31 13:08:18 +0800532
533 case MBEDTLS_TLS_EXT_SCT:
Gilles Peskine449bd832023-01-11 14:50:10 +0100534 return MBEDTLS_SSL_EXT_ID_SCT;
Jerry Yu7a485c12022-10-31 13:08:18 +0800535
536 case MBEDTLS_TLS_EXT_CLI_CERT_TYPE:
Gilles Peskine449bd832023-01-11 14:50:10 +0100537 return MBEDTLS_SSL_EXT_ID_CLI_CERT_TYPE;
Jerry Yu7a485c12022-10-31 13:08:18 +0800538
539 case MBEDTLS_TLS_EXT_SERV_CERT_TYPE:
Gilles Peskine449bd832023-01-11 14:50:10 +0100540 return MBEDTLS_SSL_EXT_ID_SERV_CERT_TYPE;
Jerry Yu7a485c12022-10-31 13:08:18 +0800541
542 case MBEDTLS_TLS_EXT_PADDING:
Gilles Peskine449bd832023-01-11 14:50:10 +0100543 return MBEDTLS_SSL_EXT_ID_PADDING;
Jerry Yu7a485c12022-10-31 13:08:18 +0800544
545 case MBEDTLS_TLS_EXT_PRE_SHARED_KEY:
Gilles Peskine449bd832023-01-11 14:50:10 +0100546 return MBEDTLS_SSL_EXT_ID_PRE_SHARED_KEY;
Jerry Yu7a485c12022-10-31 13:08:18 +0800547
548 case MBEDTLS_TLS_EXT_EARLY_DATA:
Gilles Peskine449bd832023-01-11 14:50:10 +0100549 return MBEDTLS_SSL_EXT_ID_EARLY_DATA;
Jerry Yu7a485c12022-10-31 13:08:18 +0800550
551 case MBEDTLS_TLS_EXT_SUPPORTED_VERSIONS:
Gilles Peskine449bd832023-01-11 14:50:10 +0100552 return MBEDTLS_SSL_EXT_ID_SUPPORTED_VERSIONS;
Jerry Yu7a485c12022-10-31 13:08:18 +0800553
554 case MBEDTLS_TLS_EXT_COOKIE:
Gilles Peskine449bd832023-01-11 14:50:10 +0100555 return MBEDTLS_SSL_EXT_ID_COOKIE;
Jerry Yu7a485c12022-10-31 13:08:18 +0800556
557 case MBEDTLS_TLS_EXT_PSK_KEY_EXCHANGE_MODES:
Gilles Peskine449bd832023-01-11 14:50:10 +0100558 return MBEDTLS_SSL_EXT_ID_PSK_KEY_EXCHANGE_MODES;
Jerry Yu7a485c12022-10-31 13:08:18 +0800559
560 case MBEDTLS_TLS_EXT_CERT_AUTH:
Gilles Peskine449bd832023-01-11 14:50:10 +0100561 return MBEDTLS_SSL_EXT_ID_CERT_AUTH;
Jerry Yu7a485c12022-10-31 13:08:18 +0800562
563 case MBEDTLS_TLS_EXT_OID_FILTERS:
Gilles Peskine449bd832023-01-11 14:50:10 +0100564 return MBEDTLS_SSL_EXT_ID_OID_FILTERS;
Jerry Yu7a485c12022-10-31 13:08:18 +0800565
566 case MBEDTLS_TLS_EXT_POST_HANDSHAKE_AUTH:
Gilles Peskine449bd832023-01-11 14:50:10 +0100567 return MBEDTLS_SSL_EXT_ID_POST_HANDSHAKE_AUTH;
Jerry Yu7a485c12022-10-31 13:08:18 +0800568
569 case MBEDTLS_TLS_EXT_SIG_ALG_CERT:
Gilles Peskine449bd832023-01-11 14:50:10 +0100570 return MBEDTLS_SSL_EXT_ID_SIG_ALG_CERT;
Jerry Yu7a485c12022-10-31 13:08:18 +0800571
572 case MBEDTLS_TLS_EXT_KEY_SHARE:
Gilles Peskine449bd832023-01-11 14:50:10 +0100573 return MBEDTLS_SSL_EXT_ID_KEY_SHARE;
Jerry Yu7a485c12022-10-31 13:08:18 +0800574
575 case MBEDTLS_TLS_EXT_TRUNCATED_HMAC:
Gilles Peskine449bd832023-01-11 14:50:10 +0100576 return MBEDTLS_SSL_EXT_ID_TRUNCATED_HMAC;
Jerry Yu7a485c12022-10-31 13:08:18 +0800577
578 case MBEDTLS_TLS_EXT_SUPPORTED_POINT_FORMATS:
Gilles Peskine449bd832023-01-11 14:50:10 +0100579 return MBEDTLS_SSL_EXT_ID_SUPPORTED_POINT_FORMATS;
Jerry Yu7a485c12022-10-31 13:08:18 +0800580
581 case MBEDTLS_TLS_EXT_ENCRYPT_THEN_MAC:
Gilles Peskine449bd832023-01-11 14:50:10 +0100582 return MBEDTLS_SSL_EXT_ID_ENCRYPT_THEN_MAC;
Jerry Yu7a485c12022-10-31 13:08:18 +0800583
584 case MBEDTLS_TLS_EXT_EXTENDED_MASTER_SECRET:
Gilles Peskine449bd832023-01-11 14:50:10 +0100585 return MBEDTLS_SSL_EXT_ID_EXTENDED_MASTER_SECRET;
Jerry Yu7a485c12022-10-31 13:08:18 +0800586
587 case MBEDTLS_TLS_EXT_SESSION_TICKET:
Gilles Peskine449bd832023-01-11 14:50:10 +0100588 return MBEDTLS_SSL_EXT_ID_SESSION_TICKET;
Jerry Yu7a485c12022-10-31 13:08:18 +0800589
590 }
591
Gilles Peskine449bd832023-01-11 14:50:10 +0100592 return MBEDTLS_SSL_EXT_ID_UNRECOGNIZED;
Jerry Yu7a485c12022-10-31 13:08:18 +0800593}
594
Gilles Peskine449bd832023-01-11 14:50:10 +0100595uint32_t mbedtls_ssl_get_extension_mask(unsigned int extension_type)
Jerry Yu7a485c12022-10-31 13:08:18 +0800596{
Gilles Peskine449bd832023-01-11 14:50:10 +0100597 return 1 << mbedtls_ssl_get_extension_id(extension_type);
Jerry Yu7a485c12022-10-31 13:08:18 +0800598}
599
Jerry Yud25cab02022-10-31 12:48:30 +0800600#if defined(MBEDTLS_DEBUG_C)
601static const char *extension_name_table[] = {
Jerry Yuea52ed92022-11-08 21:01:17 +0800602 [MBEDTLS_SSL_EXT_ID_UNRECOGNIZED] = "unrecognized",
Jerry Yud25cab02022-10-31 12:48:30 +0800603 [MBEDTLS_SSL_EXT_ID_SERVERNAME] = "server_name",
604 [MBEDTLS_SSL_EXT_ID_MAX_FRAGMENT_LENGTH] = "max_fragment_length",
605 [MBEDTLS_SSL_EXT_ID_STATUS_REQUEST] = "status_request",
606 [MBEDTLS_SSL_EXT_ID_SUPPORTED_GROUPS] = "supported_groups",
607 [MBEDTLS_SSL_EXT_ID_SIG_ALG] = "signature_algorithms",
608 [MBEDTLS_SSL_EXT_ID_USE_SRTP] = "use_srtp",
609 [MBEDTLS_SSL_EXT_ID_HEARTBEAT] = "heartbeat",
610 [MBEDTLS_SSL_EXT_ID_ALPN] = "application_layer_protocol_negotiation",
611 [MBEDTLS_SSL_EXT_ID_SCT] = "signed_certificate_timestamp",
612 [MBEDTLS_SSL_EXT_ID_CLI_CERT_TYPE] = "client_certificate_type",
613 [MBEDTLS_SSL_EXT_ID_SERV_CERT_TYPE] = "server_certificate_type",
614 [MBEDTLS_SSL_EXT_ID_PADDING] = "padding",
615 [MBEDTLS_SSL_EXT_ID_PRE_SHARED_KEY] = "pre_shared_key",
616 [MBEDTLS_SSL_EXT_ID_EARLY_DATA] = "early_data",
617 [MBEDTLS_SSL_EXT_ID_SUPPORTED_VERSIONS] = "supported_versions",
618 [MBEDTLS_SSL_EXT_ID_COOKIE] = "cookie",
619 [MBEDTLS_SSL_EXT_ID_PSK_KEY_EXCHANGE_MODES] = "psk_key_exchange_modes",
620 [MBEDTLS_SSL_EXT_ID_CERT_AUTH] = "certificate_authorities",
621 [MBEDTLS_SSL_EXT_ID_OID_FILTERS] = "oid_filters",
622 [MBEDTLS_SSL_EXT_ID_POST_HANDSHAKE_AUTH] = "post_handshake_auth",
623 [MBEDTLS_SSL_EXT_ID_SIG_ALG_CERT] = "signature_algorithms_cert",
624 [MBEDTLS_SSL_EXT_ID_KEY_SHARE] = "key_share",
625 [MBEDTLS_SSL_EXT_ID_TRUNCATED_HMAC] = "truncated_hmac",
626 [MBEDTLS_SSL_EXT_ID_SUPPORTED_POINT_FORMATS] = "supported_point_formats",
627 [MBEDTLS_SSL_EXT_ID_ENCRYPT_THEN_MAC] = "encrypt_then_mac",
628 [MBEDTLS_SSL_EXT_ID_EXTENDED_MASTER_SECRET] = "extended_master_secret",
629 [MBEDTLS_SSL_EXT_ID_SESSION_TICKET] = "session_ticket"
630};
631
Gilles Peskine449bd832023-01-11 14:50:10 +0100632static unsigned int extension_type_table[] = {
Jerry Yud25cab02022-10-31 12:48:30 +0800633 [MBEDTLS_SSL_EXT_ID_UNRECOGNIZED] = 0xff,
634 [MBEDTLS_SSL_EXT_ID_SERVERNAME] = MBEDTLS_TLS_EXT_SERVERNAME,
635 [MBEDTLS_SSL_EXT_ID_MAX_FRAGMENT_LENGTH] = MBEDTLS_TLS_EXT_MAX_FRAGMENT_LENGTH,
636 [MBEDTLS_SSL_EXT_ID_STATUS_REQUEST] = MBEDTLS_TLS_EXT_STATUS_REQUEST,
637 [MBEDTLS_SSL_EXT_ID_SUPPORTED_GROUPS] = MBEDTLS_TLS_EXT_SUPPORTED_GROUPS,
638 [MBEDTLS_SSL_EXT_ID_SIG_ALG] = MBEDTLS_TLS_EXT_SIG_ALG,
639 [MBEDTLS_SSL_EXT_ID_USE_SRTP] = MBEDTLS_TLS_EXT_USE_SRTP,
640 [MBEDTLS_SSL_EXT_ID_HEARTBEAT] = MBEDTLS_TLS_EXT_HEARTBEAT,
641 [MBEDTLS_SSL_EXT_ID_ALPN] = MBEDTLS_TLS_EXT_ALPN,
642 [MBEDTLS_SSL_EXT_ID_SCT] = MBEDTLS_TLS_EXT_SCT,
643 [MBEDTLS_SSL_EXT_ID_CLI_CERT_TYPE] = MBEDTLS_TLS_EXT_CLI_CERT_TYPE,
644 [MBEDTLS_SSL_EXT_ID_SERV_CERT_TYPE] = MBEDTLS_TLS_EXT_SERV_CERT_TYPE,
645 [MBEDTLS_SSL_EXT_ID_PADDING] = MBEDTLS_TLS_EXT_PADDING,
646 [MBEDTLS_SSL_EXT_ID_PRE_SHARED_KEY] = MBEDTLS_TLS_EXT_PRE_SHARED_KEY,
647 [MBEDTLS_SSL_EXT_ID_EARLY_DATA] = MBEDTLS_TLS_EXT_EARLY_DATA,
648 [MBEDTLS_SSL_EXT_ID_SUPPORTED_VERSIONS] = MBEDTLS_TLS_EXT_SUPPORTED_VERSIONS,
649 [MBEDTLS_SSL_EXT_ID_COOKIE] = MBEDTLS_TLS_EXT_COOKIE,
650 [MBEDTLS_SSL_EXT_ID_PSK_KEY_EXCHANGE_MODES] = MBEDTLS_TLS_EXT_PSK_KEY_EXCHANGE_MODES,
651 [MBEDTLS_SSL_EXT_ID_CERT_AUTH] = MBEDTLS_TLS_EXT_CERT_AUTH,
652 [MBEDTLS_SSL_EXT_ID_OID_FILTERS] = MBEDTLS_TLS_EXT_OID_FILTERS,
653 [MBEDTLS_SSL_EXT_ID_POST_HANDSHAKE_AUTH] = MBEDTLS_TLS_EXT_POST_HANDSHAKE_AUTH,
654 [MBEDTLS_SSL_EXT_ID_SIG_ALG_CERT] = MBEDTLS_TLS_EXT_SIG_ALG_CERT,
655 [MBEDTLS_SSL_EXT_ID_KEY_SHARE] = MBEDTLS_TLS_EXT_KEY_SHARE,
656 [MBEDTLS_SSL_EXT_ID_TRUNCATED_HMAC] = MBEDTLS_TLS_EXT_TRUNCATED_HMAC,
657 [MBEDTLS_SSL_EXT_ID_SUPPORTED_POINT_FORMATS] = MBEDTLS_TLS_EXT_SUPPORTED_POINT_FORMATS,
658 [MBEDTLS_SSL_EXT_ID_ENCRYPT_THEN_MAC] = MBEDTLS_TLS_EXT_ENCRYPT_THEN_MAC,
659 [MBEDTLS_SSL_EXT_ID_EXTENDED_MASTER_SECRET] = MBEDTLS_TLS_EXT_EXTENDED_MASTER_SECRET,
660 [MBEDTLS_SSL_EXT_ID_SESSION_TICKET] = MBEDTLS_TLS_EXT_SESSION_TICKET
661};
662
Gilles Peskine449bd832023-01-11 14:50:10 +0100663const char *mbedtls_ssl_get_extension_name(unsigned int extension_type)
Jerry Yud25cab02022-10-31 12:48:30 +0800664{
Gilles Peskine449bd832023-01-11 14:50:10 +0100665 return extension_name_table[
666 mbedtls_ssl_get_extension_id(extension_type)];
Jerry Yud25cab02022-10-31 12:48:30 +0800667}
668
Gilles Peskine449bd832023-01-11 14:50:10 +0100669static const char *ssl_tls13_get_hs_msg_name(int hs_msg_type)
Jerry Yud25cab02022-10-31 12:48:30 +0800670{
Gilles Peskine449bd832023-01-11 14:50:10 +0100671 switch (hs_msg_type) {
Jerry Yud25cab02022-10-31 12:48:30 +0800672 case MBEDTLS_SSL_HS_CLIENT_HELLO:
Gilles Peskine449bd832023-01-11 14:50:10 +0100673 return "ClientHello";
Jerry Yud25cab02022-10-31 12:48:30 +0800674 case MBEDTLS_SSL_HS_SERVER_HELLO:
Gilles Peskine449bd832023-01-11 14:50:10 +0100675 return "ServerHello";
Jerry Yud25cab02022-10-31 12:48:30 +0800676 case MBEDTLS_SSL_TLS1_3_HS_HELLO_RETRY_REQUEST:
Gilles Peskine449bd832023-01-11 14:50:10 +0100677 return "HelloRetryRequest";
Jerry Yud25cab02022-10-31 12:48:30 +0800678 case MBEDTLS_SSL_HS_NEW_SESSION_TICKET:
Gilles Peskine449bd832023-01-11 14:50:10 +0100679 return "NewSessionTicket";
Jerry Yud25cab02022-10-31 12:48:30 +0800680 case MBEDTLS_SSL_HS_ENCRYPTED_EXTENSIONS:
Gilles Peskine449bd832023-01-11 14:50:10 +0100681 return "EncryptedExtensions";
Jerry Yud25cab02022-10-31 12:48:30 +0800682 case MBEDTLS_SSL_HS_CERTIFICATE:
Gilles Peskine449bd832023-01-11 14:50:10 +0100683 return "Certificate";
Jerry Yud25cab02022-10-31 12:48:30 +0800684 case MBEDTLS_SSL_HS_CERTIFICATE_REQUEST:
Gilles Peskine449bd832023-01-11 14:50:10 +0100685 return "CertificateRequest";
Jerry Yud25cab02022-10-31 12:48:30 +0800686 }
Gilles Peskine449bd832023-01-11 14:50:10 +0100687 return "Unknown";
Jerry Yud25cab02022-10-31 12:48:30 +0800688}
689
Gilles Peskine449bd832023-01-11 14:50:10 +0100690void mbedtls_ssl_print_extension(const mbedtls_ssl_context *ssl,
691 int level, const char *file, int line,
692 int hs_msg_type, unsigned int extension_type,
693 const char *extra_msg0, const char *extra_msg1)
Jerry Yud25cab02022-10-31 12:48:30 +0800694{
695 const char *extra_msg;
Gilles Peskine449bd832023-01-11 14:50:10 +0100696 if (extra_msg0 && extra_msg1) {
Jerry Yud25cab02022-10-31 12:48:30 +0800697 mbedtls_debug_print_msg(
698 ssl, level, file, line,
699 "%s: %s(%u) extension %s %s.",
Gilles Peskine449bd832023-01-11 14:50:10 +0100700 ssl_tls13_get_hs_msg_name(hs_msg_type),
701 mbedtls_ssl_get_extension_name(extension_type),
Jerry Yud25cab02022-10-31 12:48:30 +0800702 extension_type,
Gilles Peskine449bd832023-01-11 14:50:10 +0100703 extra_msg0, extra_msg1);
Jerry Yud25cab02022-10-31 12:48:30 +0800704 return;
705 }
706
707 extra_msg = extra_msg0 ? extra_msg0 : extra_msg1;
Gilles Peskine449bd832023-01-11 14:50:10 +0100708 if (extra_msg) {
Jerry Yud25cab02022-10-31 12:48:30 +0800709 mbedtls_debug_print_msg(
710 ssl, level, file, line,
Gilles Peskine449bd832023-01-11 14:50:10 +0100711 "%s: %s(%u) extension %s.", ssl_tls13_get_hs_msg_name(hs_msg_type),
712 mbedtls_ssl_get_extension_name(extension_type), extension_type,
713 extra_msg);
Jerry Yud25cab02022-10-31 12:48:30 +0800714 return;
715 }
716
717 mbedtls_debug_print_msg(
718 ssl, level, file, line,
Gilles Peskine449bd832023-01-11 14:50:10 +0100719 "%s: %s(%u) extension.", ssl_tls13_get_hs_msg_name(hs_msg_type),
720 mbedtls_ssl_get_extension_name(extension_type), extension_type);
Jerry Yud25cab02022-10-31 12:48:30 +0800721}
722
Gilles Peskine449bd832023-01-11 14:50:10 +0100723void mbedtls_ssl_print_extensions(const mbedtls_ssl_context *ssl,
724 int level, const char *file, int line,
725 int hs_msg_type, uint32_t extensions_mask,
726 const char *extra)
Jerry Yud25cab02022-10-31 12:48:30 +0800727{
728
Gilles Peskine449bd832023-01-11 14:50:10 +0100729 for (unsigned i = 0;
730 i < sizeof(extension_name_table) / sizeof(extension_name_table[0]);
731 i++) {
Jerry Yu79aa7212022-11-08 21:30:21 +0800732 mbedtls_ssl_print_extension(
Jerry Yuea52ed92022-11-08 21:01:17 +0800733 ssl, level, file, line, hs_msg_type, extension_type_table[i],
Gilles Peskine449bd832023-01-11 14:50:10 +0100734 extensions_mask & (1 << i) ? "exists" : "does not exist", extra);
Jerry Yud25cab02022-10-31 12:48:30 +0800735 }
736}
737
Pengyu Lvee455c02023-01-12 14:37:24 +0800738#if defined(MBEDTLS_SSL_PROTO_TLS1_3) && defined(MBEDTLS_SSL_SESSION_TICKETS)
739#define ARRAY_LENGTH(a) (sizeof(a) / sizeof(*(a)))
740
741static const char *ticket_flag_name_table[] =
742{
743 [0] = "ALLOW_PSK_RESUMPTION",
744 [2] = "ALLOW_PSK_EPHEMERAL_RESUMPTION",
745 [3] = "ALLOW_EARLY_DATA",
746};
747
Pengyu Lv4938a562023-01-16 11:28:49 +0800748void mbedtls_ssl_print_ticket_flags(const mbedtls_ssl_context *ssl,
749 int level, const char *file, int line,
750 unsigned int flags)
Pengyu Lvee455c02023-01-12 14:37:24 +0800751{
752 size_t i;
753
754 mbedtls_debug_print_msg(ssl, level, file, line,
Pengyu Lv4938a562023-01-16 11:28:49 +0800755 "print ticket_flags (0x%02x)", flags);
756
757 flags = flags & MBEDTLS_SSL_TLS1_3_TICKET_FLAGS_MASK;
Pengyu Lvee455c02023-01-12 14:37:24 +0800758
759 for (i = 0; i < ARRAY_LENGTH(ticket_flag_name_table); i++) {
Pengyu Lv4938a562023-01-16 11:28:49 +0800760 if ((flags & (1 << i))) {
Pengyu Lvee455c02023-01-12 14:37:24 +0800761 mbedtls_debug_print_msg(ssl, level, file, line, "- %s is set.",
762 ticket_flag_name_table[i]);
763 }
764 }
765}
766#endif /* MBEDTLS_SSL_PROTO_TLS1_3 && MBEDTLS_SSL_SESSION_TICKETS */
767
Jerry Yud25cab02022-10-31 12:48:30 +0800768#endif /* MBEDTLS_DEBUG_C */
769
Gilles Peskine449bd832023-01-11 14:50:10 +0100770void mbedtls_ssl_optimize_checksum(mbedtls_ssl_context *ssl,
771 const mbedtls_ssl_ciphersuite_t *ciphersuite_info)
Jerry Yuc73c6182022-02-08 20:29:25 +0800772{
773 ((void) ciphersuite_info);
774
Andrzej Kurek25f27152022-08-17 16:09:31 -0400775#if defined(MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Gilles Peskine449bd832023-01-11 14:50:10 +0100776 if (ciphersuite_info->mac == MBEDTLS_MD_SHA384) {
Jerry Yuc73c6182022-02-08 20:29:25 +0800777 ssl->handshake->update_checksum = ssl_update_checksum_sha384;
Gilles Peskine449bd832023-01-11 14:50:10 +0100778 } else
Jerry Yuc73c6182022-02-08 20:29:25 +0800779#endif
Andrzej Kurek25f27152022-08-17 16:09:31 -0400780#if defined(MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Gilles Peskine449bd832023-01-11 14:50:10 +0100781 if (ciphersuite_info->mac != MBEDTLS_MD_SHA384) {
Jerry Yuc73c6182022-02-08 20:29:25 +0800782 ssl->handshake->update_checksum = ssl_update_checksum_sha256;
Gilles Peskine449bd832023-01-11 14:50:10 +0100783 } else
Jerry Yuc73c6182022-02-08 20:29:25 +0800784#endif
785 {
Gilles Peskine449bd832023-01-11 14:50:10 +0100786 MBEDTLS_SSL_DEBUG_MSG(1, ("should never happen"));
Jerry Yuc73c6182022-02-08 20:29:25 +0800787 return;
788 }
789}
790
Manuel Pégourié-Gonnardb8b07aa2023-02-06 00:34:21 +0100791int mbedtls_ssl_add_hs_hdr_to_checksum(mbedtls_ssl_context *ssl,
Manuel Pégourié-Gonnard43cc1272023-02-06 11:48:19 +0100792 unsigned hs_type,
793 size_t total_hs_len)
Ronald Cron8f6d39a2022-03-10 18:56:50 +0100794{
795 unsigned char hs_hdr[4];
796
797 /* Build HS header for checksum update. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100798 hs_hdr[0] = MBEDTLS_BYTE_0(hs_type);
799 hs_hdr[1] = MBEDTLS_BYTE_2(total_hs_len);
800 hs_hdr[2] = MBEDTLS_BYTE_1(total_hs_len);
801 hs_hdr[3] = MBEDTLS_BYTE_0(total_hs_len);
Ronald Cron8f6d39a2022-03-10 18:56:50 +0100802
Manuel Pégourié-Gonnardb8b07aa2023-02-06 00:34:21 +0100803 return ssl->handshake->update_checksum(ssl, hs_hdr, sizeof(hs_hdr));
Ronald Cron8f6d39a2022-03-10 18:56:50 +0100804}
805
Manuel Pégourié-Gonnardb8b07aa2023-02-06 00:34:21 +0100806int mbedtls_ssl_add_hs_msg_to_checksum(mbedtls_ssl_context *ssl,
Manuel Pégourié-Gonnard43cc1272023-02-06 11:48:19 +0100807 unsigned hs_type,
808 unsigned char const *msg,
809 size_t msg_len)
Ronald Cron8f6d39a2022-03-10 18:56:50 +0100810{
Manuel Pégourié-Gonnardb8b07aa2023-02-06 00:34:21 +0100811 int ret;
812 ret = mbedtls_ssl_add_hs_hdr_to_checksum(ssl, hs_type, msg_len);
Manuel Pégourié-Gonnard43cc1272023-02-06 11:48:19 +0100813 if (ret != 0) {
Manuel Pégourié-Gonnardb8b07aa2023-02-06 00:34:21 +0100814 return ret;
Manuel Pégourié-Gonnard43cc1272023-02-06 11:48:19 +0100815 }
Manuel Pégourié-Gonnardb8b07aa2023-02-06 00:34:21 +0100816 return ssl->handshake->update_checksum(ssl, msg, msg_len);
Ronald Cron8f6d39a2022-03-10 18:56:50 +0100817}
818
Manuel Pégourié-Gonnard226aa152023-02-05 09:46:59 +0100819int mbedtls_ssl_reset_checksum(mbedtls_ssl_context *ssl)
Jerry Yuc73c6182022-02-08 20:29:25 +0800820{
Manuel Pégourié-Gonnard626aaed2023-02-06 22:03:06 +0100821#if defined(MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA) || \
822 defined(MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Manuel Pégourié-Gonnardb72ff492023-02-06 09:54:49 +0100823#if defined(MBEDTLS_USE_PSA_CRYPTO)
824 psa_status_t status;
825#else
826 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
827#endif
Manuel Pégourié-Gonnard626aaed2023-02-06 22:03:06 +0100828#else /* SHA-256 or SHA-384 */
Jerry Yuc73c6182022-02-08 20:29:25 +0800829 ((void) ssl);
Manuel Pégourié-Gonnard626aaed2023-02-06 22:03:06 +0100830#endif /* SHA-256 or SHA-384 */
Andrzej Kurek25f27152022-08-17 16:09:31 -0400831#if defined(MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Jerry Yuc73c6182022-02-08 20:29:25 +0800832#if defined(MBEDTLS_USE_PSA_CRYPTO)
Manuel Pégourié-Gonnardb72ff492023-02-06 09:54:49 +0100833 status = psa_hash_abort(&ssl->handshake->fin_sha256_psa);
834 if (status != PSA_SUCCESS) {
835 return mbedtls_md_error_from_psa(status);
836 }
837 status = psa_hash_setup(&ssl->handshake->fin_sha256_psa, PSA_ALG_SHA_256);
838 if (status != PSA_SUCCESS) {
839 return mbedtls_md_error_from_psa(status);
840 }
Jerry Yuc73c6182022-02-08 20:29:25 +0800841#else
Manuel Pégourié-Gonnardb72ff492023-02-06 09:54:49 +0100842 ret = mbedtls_sha256_starts(&ssl->handshake->fin_sha256, 0);
843 if (ret != 0) {
844 return ret;
845 }
Jerry Yuc73c6182022-02-08 20:29:25 +0800846#endif
847#endif
Andrzej Kurek25f27152022-08-17 16:09:31 -0400848#if defined(MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Jerry Yuc73c6182022-02-08 20:29:25 +0800849#if defined(MBEDTLS_USE_PSA_CRYPTO)
Manuel Pégourié-Gonnardb72ff492023-02-06 09:54:49 +0100850 status = psa_hash_abort(&ssl->handshake->fin_sha384_psa);
851 if (status != PSA_SUCCESS) {
852 return mbedtls_md_error_from_psa(status);
853 }
854 status = psa_hash_setup(&ssl->handshake->fin_sha384_psa, PSA_ALG_SHA_384);
855 if (status != PSA_SUCCESS) {
856 return mbedtls_md_error_from_psa(status);
857 }
Jerry Yuc73c6182022-02-08 20:29:25 +0800858#else
Manuel Pégourié-Gonnardb72ff492023-02-06 09:54:49 +0100859 ret = mbedtls_sha512_starts(&ssl->handshake->fin_sha384, 1);
860 if (ret != 0) {
861 return ret;
862 }
Jerry Yuc73c6182022-02-08 20:29:25 +0800863#endif
864#endif
Manuel Pégourié-Gonnard226aa152023-02-05 09:46:59 +0100865 return 0;
Jerry Yuc73c6182022-02-08 20:29:25 +0800866}
867
Manuel Pégourié-Gonnard226aa152023-02-05 09:46:59 +0100868static int ssl_update_checksum_start(mbedtls_ssl_context *ssl,
Manuel Pégourié-Gonnard43cc1272023-02-06 11:48:19 +0100869 const unsigned char *buf, size_t len)
Jerry Yuc73c6182022-02-08 20:29:25 +0800870{
Manuel Pégourié-Gonnard626aaed2023-02-06 22:03:06 +0100871#if defined(MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA) || \
872 defined(MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Manuel Pégourié-Gonnarddf949012023-02-06 10:00:52 +0100873#if defined(MBEDTLS_USE_PSA_CRYPTO)
874 psa_status_t status;
875#else
876 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
877#endif
Manuel Pégourié-Gonnard626aaed2023-02-06 22:03:06 +0100878#else /* SHA-256 or SHA-384 */
879 ((void) ssl);
880 (void) buf;
881 (void) len;
882#endif /* SHA-256 or SHA-384 */
Andrzej Kurek25f27152022-08-17 16:09:31 -0400883#if defined(MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Jerry Yuc73c6182022-02-08 20:29:25 +0800884#if defined(MBEDTLS_USE_PSA_CRYPTO)
Manuel Pégourié-Gonnarddf949012023-02-06 10:00:52 +0100885 status = psa_hash_update(&ssl->handshake->fin_sha256_psa, buf, len);
886 if (status != PSA_SUCCESS) {
887 return mbedtls_md_error_from_psa(status);
888 }
Jerry Yuc73c6182022-02-08 20:29:25 +0800889#else
Manuel Pégourié-Gonnarddf949012023-02-06 10:00:52 +0100890 ret = mbedtls_sha256_update(&ssl->handshake->fin_sha256, buf, len);
891 if (ret != 0) {
892 return ret;
893 }
Jerry Yuc73c6182022-02-08 20:29:25 +0800894#endif
895#endif
Andrzej Kurek25f27152022-08-17 16:09:31 -0400896#if defined(MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Jerry Yuc73c6182022-02-08 20:29:25 +0800897#if defined(MBEDTLS_USE_PSA_CRYPTO)
Manuel Pégourié-Gonnarddf949012023-02-06 10:00:52 +0100898 status = psa_hash_update(&ssl->handshake->fin_sha384_psa, buf, len);
899 if (status != PSA_SUCCESS) {
900 return mbedtls_md_error_from_psa(status);
901 }
Jerry Yuc73c6182022-02-08 20:29:25 +0800902#else
Manuel Pégourié-Gonnarddf949012023-02-06 10:00:52 +0100903 ret = mbedtls_sha512_update(&ssl->handshake->fin_sha384, buf, len);
904 if (ret != 0) {
905 return ret;
906 }
Jerry Yuc73c6182022-02-08 20:29:25 +0800907#endif
908#endif
Manuel Pégourié-Gonnard226aa152023-02-05 09:46:59 +0100909 return 0;
Jerry Yuc73c6182022-02-08 20:29:25 +0800910}
911
Andrzej Kurek25f27152022-08-17 16:09:31 -0400912#if defined(MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Manuel Pégourié-Gonnard226aa152023-02-05 09:46:59 +0100913static int ssl_update_checksum_sha256(mbedtls_ssl_context *ssl,
Manuel Pégourié-Gonnard43cc1272023-02-06 11:48:19 +0100914 const unsigned char *buf, size_t len)
Jerry Yuc73c6182022-02-08 20:29:25 +0800915{
916#if defined(MBEDTLS_USE_PSA_CRYPTO)
Manuel Pégourié-Gonnarddf949012023-02-06 10:00:52 +0100917 return mbedtls_md_error_from_psa(psa_hash_update(
Manuel Pégourié-Gonnard43cc1272023-02-06 11:48:19 +0100918 &ssl->handshake->fin_sha256_psa, buf, len));
Jerry Yuc73c6182022-02-08 20:29:25 +0800919#else
Manuel Pégourié-Gonnarddf949012023-02-06 10:00:52 +0100920 return mbedtls_sha256_update(&ssl->handshake->fin_sha256, buf, len);
Jerry Yuc73c6182022-02-08 20:29:25 +0800921#endif
922}
923#endif
924
Andrzej Kurek25f27152022-08-17 16:09:31 -0400925#if defined(MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Manuel Pégourié-Gonnard226aa152023-02-05 09:46:59 +0100926static int ssl_update_checksum_sha384(mbedtls_ssl_context *ssl,
Manuel Pégourié-Gonnard43cc1272023-02-06 11:48:19 +0100927 const unsigned char *buf, size_t len)
Jerry Yuc73c6182022-02-08 20:29:25 +0800928{
929#if defined(MBEDTLS_USE_PSA_CRYPTO)
Manuel Pégourié-Gonnarddf949012023-02-06 10:00:52 +0100930 return mbedtls_md_error_from_psa(psa_hash_update(
Manuel Pégourié-Gonnard43cc1272023-02-06 11:48:19 +0100931 &ssl->handshake->fin_sha384_psa, buf, len));
Jerry Yuc73c6182022-02-08 20:29:25 +0800932#else
Manuel Pégourié-Gonnarddf949012023-02-06 10:00:52 +0100933 return mbedtls_sha512_update(&ssl->handshake->fin_sha384, buf, len);
Jerry Yuc73c6182022-02-08 20:29:25 +0800934#endif
935}
936#endif
937
Gilles Peskine449bd832023-01-11 14:50:10 +0100938static void ssl_handshake_params_init(mbedtls_ssl_handshake_params *handshake)
Paul Bakkeraccaffe2014-06-26 13:37:14 +0200939{
Gilles Peskine449bd832023-01-11 14:50:10 +0100940 memset(handshake, 0, sizeof(mbedtls_ssl_handshake_params));
Paul Bakkeraccaffe2014-06-26 13:37:14 +0200941
Andrzej Kurek25f27152022-08-17 16:09:31 -0400942#if defined(MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Andrzej Kurekeb342242019-01-29 09:14:33 -0500943#if defined(MBEDTLS_USE_PSA_CRYPTO)
944 handshake->fin_sha256_psa = psa_hash_operation_init();
Andrzej Kurekeb342242019-01-29 09:14:33 -0500945#else
Gilles Peskine449bd832023-01-11 14:50:10 +0100946 mbedtls_sha256_init(&handshake->fin_sha256);
Paul Bakkeraccaffe2014-06-26 13:37:14 +0200947#endif
Andrzej Kurekeb342242019-01-29 09:14:33 -0500948#endif
Andrzej Kurek25f27152022-08-17 16:09:31 -0400949#if defined(MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Andrzej Kurekeb342242019-01-29 09:14:33 -0500950#if defined(MBEDTLS_USE_PSA_CRYPTO)
Andrzej Kurek972fba52019-01-30 03:29:12 -0500951 handshake->fin_sha384_psa = psa_hash_operation_init();
Andrzej Kurekeb342242019-01-29 09:14:33 -0500952#else
Gilles Peskine449bd832023-01-11 14:50:10 +0100953 mbedtls_sha512_init(&handshake->fin_sha384);
Paul Bakkeraccaffe2014-06-26 13:37:14 +0200954#endif
Andrzej Kurekeb342242019-01-29 09:14:33 -0500955#endif
Paul Bakkeraccaffe2014-06-26 13:37:14 +0200956
957 handshake->update_checksum = ssl_update_checksum_start;
Hanno Becker7e5437a2017-04-28 17:15:26 +0100958
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200959#if defined(MBEDTLS_DHM_C)
Gilles Peskine449bd832023-01-11 14:50:10 +0100960 mbedtls_dhm_init(&handshake->dhm_ctx);
Paul Bakkeraccaffe2014-06-26 13:37:14 +0200961#endif
Neil Armstrongf3f46412022-04-12 14:43:39 +0200962#if !defined(MBEDTLS_USE_PSA_CRYPTO) && defined(MBEDTLS_ECDH_C)
Gilles Peskine449bd832023-01-11 14:50:10 +0100963 mbedtls_ecdh_init(&handshake->ecdh_ctx);
Paul Bakkeraccaffe2014-06-26 13:37:14 +0200964#endif
Manuel Pégourié-Gonnardeef142d2015-09-16 10:05:04 +0200965#if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
Neil Armstrongca7d5062022-05-31 14:43:23 +0200966#if defined(MBEDTLS_USE_PSA_CRYPTO)
967 handshake->psa_pake_ctx = psa_pake_operation_init();
968 handshake->psa_pake_password = MBEDTLS_SVC_KEY_ID_INIT;
969#else
Gilles Peskine449bd832023-01-11 14:50:10 +0100970 mbedtls_ecjpake_init(&handshake->ecjpake_ctx);
Neil Armstrongca7d5062022-05-31 14:43:23 +0200971#endif /* MBEDTLS_USE_PSA_CRYPTO */
Manuel Pégourié-Gonnard77c06462015-09-17 13:59:49 +0200972#if defined(MBEDTLS_SSL_CLI_C)
973 handshake->ecjpake_cache = NULL;
974 handshake->ecjpake_cache_len = 0;
975#endif
Manuel Pégourié-Gonnard76cfd3f2015-09-15 12:10:54 +0200976#endif
Manuel Pégourié-Gonnardcdc26ae2015-06-19 12:16:31 +0200977
Gilles Peskineeccd8882020-03-10 12:19:08 +0100978#if defined(MBEDTLS_SSL_ECP_RESTARTABLE_ENABLED)
Gilles Peskine449bd832023-01-11 14:50:10 +0100979 mbedtls_x509_crt_restart_init(&handshake->ecrs_ctx);
Manuel Pégourié-Gonnard862cde52017-05-17 11:56:15 +0200980#endif
981
Manuel Pégourié-Gonnardcdc26ae2015-06-19 12:16:31 +0200982#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
983 handshake->sni_authmode = MBEDTLS_SSL_VERIFY_UNSET;
984#endif
Hanno Becker75173122019-02-06 16:18:31 +0000985
986#if defined(MBEDTLS_X509_CRT_PARSE_C) && \
987 !defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
Gilles Peskine449bd832023-01-11 14:50:10 +0100988 mbedtls_pk_init(&handshake->peer_pubkey);
Hanno Becker75173122019-02-06 16:18:31 +0000989#endif
Paul Bakkeraccaffe2014-06-26 13:37:14 +0200990}
991
Gilles Peskine449bd832023-01-11 14:50:10 +0100992void mbedtls_ssl_transform_init(mbedtls_ssl_transform *transform)
Paul Bakkeraccaffe2014-06-26 13:37:14 +0200993{
Gilles Peskine449bd832023-01-11 14:50:10 +0100994 memset(transform, 0, sizeof(mbedtls_ssl_transform));
Paul Bakker84bbeb52014-07-01 14:53:22 +0200995
Przemyslaw Stekiel8f80fb92022-01-11 08:28:13 +0100996#if defined(MBEDTLS_USE_PSA_CRYPTO)
997 transform->psa_key_enc = MBEDTLS_SVC_KEY_ID_INIT;
998 transform->psa_key_dec = MBEDTLS_SVC_KEY_ID_INIT;
Przemyslaw Stekiel6be9cf52022-01-19 16:00:22 +0100999#else
Gilles Peskine449bd832023-01-11 14:50:10 +01001000 mbedtls_cipher_init(&transform->cipher_ctx_enc);
1001 mbedtls_cipher_init(&transform->cipher_ctx_dec);
Przemyslaw Stekiel8f80fb92022-01-11 08:28:13 +01001002#endif
1003
Hanno Beckerfd86ca82020-11-30 08:54:23 +00001004#if defined(MBEDTLS_SSL_SOME_SUITES_USE_MAC)
Neil Armstrong39b8e7d2022-02-23 09:24:45 +01001005#if defined(MBEDTLS_USE_PSA_CRYPTO)
1006 transform->psa_mac_enc = MBEDTLS_SVC_KEY_ID_INIT;
1007 transform->psa_mac_dec = MBEDTLS_SVC_KEY_ID_INIT;
Neil Armstrongcf8841a2022-02-24 11:17:45 +01001008#else
Gilles Peskine449bd832023-01-11 14:50:10 +01001009 mbedtls_md_init(&transform->md_ctx_enc);
1010 mbedtls_md_init(&transform->md_ctx_dec);
Hanno Beckerd56ed242018-01-03 15:32:51 +00001011#endif
Neil Armstrongcf8841a2022-02-24 11:17:45 +01001012#endif
Paul Bakkeraccaffe2014-06-26 13:37:14 +02001013}
1014
Gilles Peskine449bd832023-01-11 14:50:10 +01001015void mbedtls_ssl_session_init(mbedtls_ssl_session *session)
Paul Bakkeraccaffe2014-06-26 13:37:14 +02001016{
Gilles Peskine449bd832023-01-11 14:50:10 +01001017 memset(session, 0, sizeof(mbedtls_ssl_session));
Paul Bakkeraccaffe2014-06-26 13:37:14 +02001018}
1019
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02001020MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01001021static int ssl_handshake_init(mbedtls_ssl_context *ssl)
Paul Bakker48916f92012-09-16 19:57:18 +00001022{
Manuel Pégourié-Gonnardb8b07aa2023-02-06 00:34:21 +01001023 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
1024
Paul Bakkeraccaffe2014-06-26 13:37:14 +02001025 /* Clear old handshake information if present */
Jerry Yu2e199812022-12-01 18:57:19 +08001026#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
Gilles Peskine449bd832023-01-11 14:50:10 +01001027 if (ssl->transform_negotiate) {
1028 mbedtls_ssl_transform_free(ssl->transform_negotiate);
1029 }
Jerry Yu2e199812022-12-01 18:57:19 +08001030#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001031 if (ssl->session_negotiate) {
1032 mbedtls_ssl_session_free(ssl->session_negotiate);
1033 }
1034 if (ssl->handshake) {
1035 mbedtls_ssl_handshake_free(ssl);
1036 }
Paul Bakkeraccaffe2014-06-26 13:37:14 +02001037
Jerry Yu2e199812022-12-01 18:57:19 +08001038#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
Paul Bakkeraccaffe2014-06-26 13:37:14 +02001039 /*
1040 * Either the pointers are now NULL or cleared properly and can be freed.
1041 * Now allocate missing structures.
1042 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001043 if (ssl->transform_negotiate == NULL) {
1044 ssl->transform_negotiate = mbedtls_calloc(1, sizeof(mbedtls_ssl_transform));
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02001045 }
Jerry Yu2e199812022-12-01 18:57:19 +08001046#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakker48916f92012-09-16 19:57:18 +00001047
Gilles Peskine449bd832023-01-11 14:50:10 +01001048 if (ssl->session_negotiate == NULL) {
1049 ssl->session_negotiate = mbedtls_calloc(1, sizeof(mbedtls_ssl_session));
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02001050 }
Paul Bakker48916f92012-09-16 19:57:18 +00001051
Gilles Peskine449bd832023-01-11 14:50:10 +01001052 if (ssl->handshake == NULL) {
1053 ssl->handshake = mbedtls_calloc(1, sizeof(mbedtls_ssl_handshake_params));
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02001054 }
Andrzej Kurek0afa2a12020-03-03 10:39:58 -05001055#if defined(MBEDTLS_SSL_VARIABLE_BUFFER_LENGTH)
1056 /* If the buffers are too small - reallocate */
Andrzej Kurek8ea68722020-04-03 06:40:47 -04001057
Gilles Peskine449bd832023-01-11 14:50:10 +01001058 handle_buffer_resizing(ssl, 0, MBEDTLS_SSL_IN_BUFFER_LEN,
1059 MBEDTLS_SSL_OUT_BUFFER_LEN);
Andrzej Kurek0afa2a12020-03-03 10:39:58 -05001060#endif
Paul Bakker48916f92012-09-16 19:57:18 +00001061
Paul Bakkeraccaffe2014-06-26 13:37:14 +02001062 /* All pointers should exist and can be directly freed without issue */
Gilles Peskine449bd832023-01-11 14:50:10 +01001063 if (ssl->handshake == NULL ||
Jerry Yu2e199812022-12-01 18:57:19 +08001064#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
Paul Bakker48916f92012-09-16 19:57:18 +00001065 ssl->transform_negotiate == NULL ||
Jerry Yu2e199812022-12-01 18:57:19 +08001066#endif
Gilles Peskine449bd832023-01-11 14:50:10 +01001067 ssl->session_negotiate == NULL) {
1068 MBEDTLS_SSL_DEBUG_MSG(1, ("alloc() of ssl sub-contexts failed"));
Paul Bakkeraccaffe2014-06-26 13:37:14 +02001069
Gilles Peskine449bd832023-01-11 14:50:10 +01001070 mbedtls_free(ssl->handshake);
Paul Bakkeraccaffe2014-06-26 13:37:14 +02001071 ssl->handshake = NULL;
Jerry Yu2e199812022-12-01 18:57:19 +08001072
1073#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
Gilles Peskine449bd832023-01-11 14:50:10 +01001074 mbedtls_free(ssl->transform_negotiate);
Paul Bakkeraccaffe2014-06-26 13:37:14 +02001075 ssl->transform_negotiate = NULL;
Jerry Yu2e199812022-12-01 18:57:19 +08001076#endif
1077
Gilles Peskine449bd832023-01-11 14:50:10 +01001078 mbedtls_free(ssl->session_negotiate);
Paul Bakkeraccaffe2014-06-26 13:37:14 +02001079 ssl->session_negotiate = NULL;
1080
Gilles Peskine449bd832023-01-11 14:50:10 +01001081 return MBEDTLS_ERR_SSL_ALLOC_FAILED;
Paul Bakker48916f92012-09-16 19:57:18 +00001082 }
1083
Paul Bakkeraccaffe2014-06-26 13:37:14 +02001084 /* Initialize structures */
Gilles Peskine449bd832023-01-11 14:50:10 +01001085 mbedtls_ssl_session_init(ssl->session_negotiate);
1086 ssl_handshake_params_init(ssl->handshake);
Paul Bakker968afaa2014-07-09 11:09:24 +02001087
Jerry Yu2e199812022-12-01 18:57:19 +08001088#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
Gilles Peskine449bd832023-01-11 14:50:10 +01001089 mbedtls_ssl_transform_init(ssl->transform_negotiate);
Jerry Yu2e199812022-12-01 18:57:19 +08001090#endif
1091
Manuel Pégourié-Gonnard537f2312023-02-05 10:17:45 +01001092 /* Setup handshake checksums */
Manuel Pégourié-Gonnardb8b07aa2023-02-06 00:34:21 +01001093 ret = mbedtls_ssl_reset_checksum(ssl);
1094 if (ret != 0) {
1095 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ssl_reset_checksum", ret);
1096 return ret;
1097 }
Manuel Pégourié-Gonnard537f2312023-02-05 10:17:45 +01001098
Jerry Yud0766ec2022-09-22 10:46:57 +08001099#if defined(MBEDTLS_SSL_PROTO_TLS1_3) && \
1100 defined(MBEDTLS_SSL_SRV_C) && \
1101 defined(MBEDTLS_SSL_SESSION_TICKETS)
1102 ssl->handshake->new_session_tickets_count =
Gilles Peskine449bd832023-01-11 14:50:10 +01001103 ssl->conf->new_session_tickets_count;
Jerry Yud0766ec2022-09-22 10:46:57 +08001104#endif
1105
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001106#if defined(MBEDTLS_SSL_PROTO_DTLS)
Gilles Peskine449bd832023-01-11 14:50:10 +01001107 if (ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM) {
Manuel Pégourié-Gonnard06939ce2015-05-11 11:25:46 +02001108 ssl->handshake->alt_transform_out = ssl->transform_out;
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02001109
Gilles Peskine449bd832023-01-11 14:50:10 +01001110 if (ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT) {
Manuel Pégourié-Gonnard06939ce2015-05-11 11:25:46 +02001111 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_PREPARING;
Gilles Peskine449bd832023-01-11 14:50:10 +01001112 } else {
Manuel Pégourié-Gonnard06939ce2015-05-11 11:25:46 +02001113 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_WAITING;
Gilles Peskine449bd832023-01-11 14:50:10 +01001114 }
Manuel Pégourié-Gonnard286a1362015-05-13 16:22:05 +02001115
Gilles Peskine449bd832023-01-11 14:50:10 +01001116 mbedtls_ssl_set_timer(ssl, 0);
Manuel Pégourié-Gonnard06939ce2015-05-11 11:25:46 +02001117 }
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02001118#endif
1119
Brett Warrene0edc842021-08-17 09:53:13 +01001120/*
1121 * curve_list is translated to IANA TLS group identifiers here because
1122 * mbedtls_ssl_conf_curves returns void and so can't return
1123 * any error codes.
1124 */
1125#if defined(MBEDTLS_ECP_C)
1126#if !defined(MBEDTLS_DEPRECATED_REMOVED)
1127 /* Heap allocate and translate curve_list from internal to IANA group ids */
Gilles Peskine449bd832023-01-11 14:50:10 +01001128 if (ssl->conf->curve_list != NULL) {
Brett Warrene0edc842021-08-17 09:53:13 +01001129 size_t length;
1130 const mbedtls_ecp_group_id *curve_list = ssl->conf->curve_list;
1131
Gilles Peskine449bd832023-01-11 14:50:10 +01001132 for (length = 0; (curve_list[length] != MBEDTLS_ECP_DP_NONE) &&
1133 (length < MBEDTLS_ECP_DP_MAX); length++) {
1134 }
Brett Warrene0edc842021-08-17 09:53:13 +01001135
1136 /* Leave room for zero termination */
Gilles Peskine449bd832023-01-11 14:50:10 +01001137 uint16_t *group_list = mbedtls_calloc(length + 1, sizeof(uint16_t));
1138 if (group_list == NULL) {
1139 return MBEDTLS_ERR_SSL_ALLOC_FAILED;
1140 }
Brett Warrene0edc842021-08-17 09:53:13 +01001141
Gilles Peskine449bd832023-01-11 14:50:10 +01001142 for (size_t i = 0; i < length; i++) {
Valerio Setti18c9fed2022-12-30 17:44:24 +01001143 uint16_t tls_id = mbedtls_ssl_get_tls_id_from_ecp_group_id(
Gilles Peskine449bd832023-01-11 14:50:10 +01001144 curve_list[i]);
1145 if (tls_id == 0) {
1146 mbedtls_free(group_list);
1147 return MBEDTLS_ERR_SSL_BAD_CONFIG;
Brett Warrene0edc842021-08-17 09:53:13 +01001148 }
Valerio Setti18c9fed2022-12-30 17:44:24 +01001149 group_list[i] = tls_id;
Brett Warrene0edc842021-08-17 09:53:13 +01001150 }
1151
1152 group_list[length] = 0;
1153
1154 ssl->handshake->group_list = group_list;
1155 ssl->handshake->group_list_heap_allocated = 1;
Gilles Peskine449bd832023-01-11 14:50:10 +01001156 } else {
Brett Warrene0edc842021-08-17 09:53:13 +01001157 ssl->handshake->group_list = ssl->conf->group_list;
1158 ssl->handshake->group_list_heap_allocated = 0;
1159 }
1160#endif /* MBEDTLS_DEPRECATED_REMOVED */
1161#endif /* MBEDTLS_ECP_C */
1162
Ronald Crone68ab4f2022-10-05 12:46:29 +02001163#if defined(MBEDTLS_SSL_HANDSHAKE_WITH_CERT_ENABLED)
Jerry Yuf017ee42022-01-12 15:49:48 +08001164#if !defined(MBEDTLS_DEPRECATED_REMOVED)
Jerry Yua69269a2022-01-17 21:06:01 +08001165#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
Jerry Yu713013f2022-01-17 18:16:35 +08001166 /* Heap allocate and translate sig_hashes from internal hash identifiers to
1167 signature algorithms IANA identifiers. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001168 if (mbedtls_ssl_conf_is_tls12_only(ssl->conf) &&
1169 ssl->conf->sig_hashes != NULL) {
Jerry Yuf017ee42022-01-12 15:49:48 +08001170 const int *md;
1171 const int *sig_hashes = ssl->conf->sig_hashes;
Jerry Yub476a442022-01-21 18:14:45 +08001172 size_t sig_algs_len = 0;
Jerry Yuf017ee42022-01-12 15:49:48 +08001173 uint16_t *p;
1174
Jerry Yub476a442022-01-21 18:14:45 +08001175#if defined(static_assert)
Gilles Peskine449bd832023-01-11 14:50:10 +01001176 static_assert(MBEDTLS_SSL_MAX_SIG_ALG_LIST_LEN
1177 <= (SIZE_MAX - (2 * sizeof(uint16_t))),
1178 "MBEDTLS_SSL_MAX_SIG_ALG_LIST_LEN too big");
Jerry Yua68dca22022-01-20 16:28:27 +08001179#endif
1180
Gilles Peskine449bd832023-01-11 14:50:10 +01001181 for (md = sig_hashes; *md != MBEDTLS_MD_NONE; md++) {
1182 if (mbedtls_ssl_hash_from_md_alg(*md) == MBEDTLS_SSL_HASH_NONE) {
Jerry Yuf017ee42022-01-12 15:49:48 +08001183 continue;
Gilles Peskine449bd832023-01-11 14:50:10 +01001184 }
Jerry Yub476a442022-01-21 18:14:45 +08001185#if defined(MBEDTLS_ECDSA_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01001186 sig_algs_len += sizeof(uint16_t);
Jerry Yub476a442022-01-21 18:14:45 +08001187#endif
Jerry Yua68dca22022-01-20 16:28:27 +08001188
Jerry Yub476a442022-01-21 18:14:45 +08001189#if defined(MBEDTLS_RSA_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01001190 sig_algs_len += sizeof(uint16_t);
Jerry Yub476a442022-01-21 18:14:45 +08001191#endif
Gilles Peskine449bd832023-01-11 14:50:10 +01001192 if (sig_algs_len > MBEDTLS_SSL_MAX_SIG_ALG_LIST_LEN) {
1193 return MBEDTLS_ERR_SSL_BAD_CONFIG;
1194 }
Jerry Yuf017ee42022-01-12 15:49:48 +08001195 }
1196
Gilles Peskine449bd832023-01-11 14:50:10 +01001197 if (sig_algs_len < MBEDTLS_SSL_MIN_SIG_ALG_LIST_LEN) {
1198 return MBEDTLS_ERR_SSL_BAD_CONFIG;
1199 }
Jerry Yuf017ee42022-01-12 15:49:48 +08001200
Gilles Peskine449bd832023-01-11 14:50:10 +01001201 ssl->handshake->sig_algs = mbedtls_calloc(1, sig_algs_len +
1202 sizeof(uint16_t));
1203 if (ssl->handshake->sig_algs == NULL) {
1204 return MBEDTLS_ERR_SSL_ALLOC_FAILED;
1205 }
Jerry Yuf017ee42022-01-12 15:49:48 +08001206
Gilles Peskine449bd832023-01-11 14:50:10 +01001207 p = (uint16_t *) ssl->handshake->sig_algs;
1208 for (md = sig_hashes; *md != MBEDTLS_MD_NONE; md++) {
1209 unsigned char hash = mbedtls_ssl_hash_from_md_alg(*md);
1210 if (hash == MBEDTLS_SSL_HASH_NONE) {
Jerry Yuf017ee42022-01-12 15:49:48 +08001211 continue;
Gilles Peskine449bd832023-01-11 14:50:10 +01001212 }
Jerry Yu6106fdc2022-01-12 16:36:14 +08001213#if defined(MBEDTLS_ECDSA_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01001214 *p = ((hash << 8) | MBEDTLS_SSL_SIG_ECDSA);
Jerry Yuf017ee42022-01-12 15:49:48 +08001215 p++;
Jerry Yu6106fdc2022-01-12 16:36:14 +08001216#endif
1217#if defined(MBEDTLS_RSA_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01001218 *p = ((hash << 8) | MBEDTLS_SSL_SIG_RSA);
Jerry Yuf017ee42022-01-12 15:49:48 +08001219 p++;
Jerry Yu6106fdc2022-01-12 16:36:14 +08001220#endif
Jerry Yuf017ee42022-01-12 15:49:48 +08001221 }
Gabor Mezei15b95a62022-05-09 16:37:58 +02001222 *p = MBEDTLS_TLS_SIG_NONE;
Jerry Yuf017ee42022-01-12 15:49:48 +08001223 ssl->handshake->sig_algs_heap_allocated = 1;
Gilles Peskine449bd832023-01-11 14:50:10 +01001224 } else
Jerry Yua69269a2022-01-17 21:06:01 +08001225#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Jerry Yuf017ee42022-01-12 15:49:48 +08001226 {
Jerry Yuf017ee42022-01-12 15:49:48 +08001227 ssl->handshake->sig_algs_heap_allocated = 0;
1228 }
Jerry Yucc539102022-06-27 16:27:35 +08001229#endif /* !MBEDTLS_DEPRECATED_REMOVED */
Ronald Crone68ab4f2022-10-05 12:46:29 +02001230#endif /* MBEDTLS_SSL_HANDSHAKE_WITH_CERT_ENABLED */
Gilles Peskine449bd832023-01-11 14:50:10 +01001231 return 0;
Paul Bakker48916f92012-09-16 19:57:18 +00001232}
1233
Manuel Pégourié-Gonnarde057d3b2015-05-20 10:59:43 +02001234#if defined(MBEDTLS_SSL_DTLS_HELLO_VERIFY) && defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard7d38d212014-07-23 17:52:09 +02001235/* Dummy cookie callbacks for defaults */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02001236MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01001237static int ssl_cookie_write_dummy(void *ctx,
1238 unsigned char **p, unsigned char *end,
1239 const unsigned char *cli_id, size_t cli_id_len)
Manuel Pégourié-Gonnard7d38d212014-07-23 17:52:09 +02001240{
1241 ((void) ctx);
1242 ((void) p);
1243 ((void) end);
1244 ((void) cli_id);
1245 ((void) cli_id_len);
1246
Gilles Peskine449bd832023-01-11 14:50:10 +01001247 return MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
Manuel Pégourié-Gonnard7d38d212014-07-23 17:52:09 +02001248}
1249
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02001250MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01001251static int ssl_cookie_check_dummy(void *ctx,
1252 const unsigned char *cookie, size_t cookie_len,
1253 const unsigned char *cli_id, size_t cli_id_len)
Manuel Pégourié-Gonnard7d38d212014-07-23 17:52:09 +02001254{
1255 ((void) ctx);
1256 ((void) cookie);
1257 ((void) cookie_len);
1258 ((void) cli_id);
1259 ((void) cli_id_len);
1260
Gilles Peskine449bd832023-01-11 14:50:10 +01001261 return MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
Manuel Pégourié-Gonnard7d38d212014-07-23 17:52:09 +02001262}
Manuel Pégourié-Gonnarde057d3b2015-05-20 10:59:43 +02001263#endif /* MBEDTLS_SSL_DTLS_HELLO_VERIFY && MBEDTLS_SSL_SRV_C */
Manuel Pégourié-Gonnard7d38d212014-07-23 17:52:09 +02001264
Paul Bakker5121ce52009-01-03 21:22:43 +00001265/*
1266 * Initialize an SSL context
1267 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001268void mbedtls_ssl_init(mbedtls_ssl_context *ssl)
Manuel Pégourié-Gonnard41d479e2015-04-29 00:48:22 +02001269{
Gilles Peskine449bd832023-01-11 14:50:10 +01001270 memset(ssl, 0, sizeof(mbedtls_ssl_context));
Manuel Pégourié-Gonnard41d479e2015-04-29 00:48:22 +02001271}
1272
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02001273MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01001274static int ssl_conf_version_check(const mbedtls_ssl_context *ssl)
Jerry Yu60835a82021-08-04 10:13:52 +08001275{
Ronald Cron086ee0b2022-03-15 15:18:51 +01001276 const mbedtls_ssl_config *conf = ssl->conf;
1277
Ronald Cron6f135e12021-12-08 16:57:54 +01001278#if defined(MBEDTLS_SSL_PROTO_TLS1_3)
Gilles Peskine449bd832023-01-11 14:50:10 +01001279 if (mbedtls_ssl_conf_is_tls13_only(conf)) {
1280 if (conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM) {
1281 MBEDTLS_SSL_DEBUG_MSG(1, ("DTLS 1.3 is not yet supported."));
1282 return MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
XiaokangQianed582dd2022-04-13 08:21:05 +00001283 }
1284
Gilles Peskine449bd832023-01-11 14:50:10 +01001285 MBEDTLS_SSL_DEBUG_MSG(4, ("The SSL configuration is tls13 only."));
1286 return 0;
Jerry Yu60835a82021-08-04 10:13:52 +08001287 }
1288#endif
1289
1290#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
Gilles Peskine449bd832023-01-11 14:50:10 +01001291 if (mbedtls_ssl_conf_is_tls12_only(conf)) {
1292 MBEDTLS_SSL_DEBUG_MSG(4, ("The SSL configuration is tls12 only."));
1293 return 0;
Jerry Yu60835a82021-08-04 10:13:52 +08001294 }
1295#endif
1296
Ronald Cron6f135e12021-12-08 16:57:54 +01001297#if defined(MBEDTLS_SSL_PROTO_TLS1_2) && defined(MBEDTLS_SSL_PROTO_TLS1_3)
Gilles Peskine449bd832023-01-11 14:50:10 +01001298 if (mbedtls_ssl_conf_is_hybrid_tls12_tls13(conf)) {
1299 if (ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM) {
1300 MBEDTLS_SSL_DEBUG_MSG(1, ("DTLS not yet supported in Hybrid TLS 1.3 + TLS 1.2"));
1301 return MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
XiaokangQianed582dd2022-04-13 08:21:05 +00001302 }
1303
Gilles Peskine449bd832023-01-11 14:50:10 +01001304 if (conf->endpoint == MBEDTLS_SSL_IS_SERVER) {
1305 MBEDTLS_SSL_DEBUG_MSG(1, ("TLS 1.3 server is not supported yet."));
1306 return MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
XiaokangQian8f9dfe42022-04-15 02:52:39 +00001307 }
1308
1309
Gilles Peskine449bd832023-01-11 14:50:10 +01001310 MBEDTLS_SSL_DEBUG_MSG(4, ("The SSL configuration is TLS 1.3 or TLS 1.2."));
1311 return 0;
Jerry Yu60835a82021-08-04 10:13:52 +08001312 }
1313#endif
1314
Gilles Peskine449bd832023-01-11 14:50:10 +01001315 MBEDTLS_SSL_DEBUG_MSG(1, ("The SSL configuration is invalid."));
1316 return MBEDTLS_ERR_SSL_BAD_CONFIG;
Jerry Yu60835a82021-08-04 10:13:52 +08001317}
1318
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02001319MBEDTLS_CHECK_RETURN_CRITICAL
Jerry Yu60835a82021-08-04 10:13:52 +08001320static int ssl_conf_check(const mbedtls_ssl_context *ssl)
1321{
1322 int ret;
Gilles Peskine449bd832023-01-11 14:50:10 +01001323 ret = ssl_conf_version_check(ssl);
1324 if (ret != 0) {
1325 return ret;
1326 }
Jerry Yu60835a82021-08-04 10:13:52 +08001327
Jerry Yudef7ae42022-10-30 14:13:19 +08001328#if defined(MBEDTLS_SSL_PROTO_TLS1_3)
1329 /* RFC 8446 section 4.4.3
1330 *
1331 * If the verification fails, the receiver MUST terminate the handshake with
1332 * a "decrypt_error" alert.
1333 *
1334 * If the client is configured as TLS 1.3 only with optional verify, return
1335 * bad config.
1336 *
1337 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001338 if (mbedtls_ssl_conf_tls13_ephemeral_enabled(
1339 (mbedtls_ssl_context *) ssl) &&
Jerry Yudef7ae42022-10-30 14:13:19 +08001340 ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT &&
1341 ssl->conf->max_tls_version == MBEDTLS_SSL_VERSION_TLS1_3 &&
1342 ssl->conf->min_tls_version == MBEDTLS_SSL_VERSION_TLS1_3 &&
Gilles Peskine449bd832023-01-11 14:50:10 +01001343 ssl->conf->authmode == MBEDTLS_SSL_VERIFY_OPTIONAL) {
Jerry Yudef7ae42022-10-30 14:13:19 +08001344 MBEDTLS_SSL_DEBUG_MSG(
Gilles Peskine449bd832023-01-11 14:50:10 +01001345 1, ("Optional verify auth mode "
1346 "is not available for TLS 1.3 client"));
1347 return MBEDTLS_ERR_SSL_BAD_CONFIG;
Jerry Yudef7ae42022-10-30 14:13:19 +08001348 }
1349#endif /* MBEDTLS_SSL_PROTO_TLS1_3 */
1350
Jerry Yu60835a82021-08-04 10:13:52 +08001351 /* Space for further checks */
1352
Gilles Peskine449bd832023-01-11 14:50:10 +01001353 return 0;
Jerry Yu60835a82021-08-04 10:13:52 +08001354}
1355
Manuel Pégourié-Gonnard41d479e2015-04-29 00:48:22 +02001356/*
1357 * Setup an SSL context
1358 */
Hanno Becker2a43f6f2018-08-10 11:12:52 +01001359
Gilles Peskine449bd832023-01-11 14:50:10 +01001360int mbedtls_ssl_setup(mbedtls_ssl_context *ssl,
1361 const mbedtls_ssl_config *conf)
Paul Bakker5121ce52009-01-03 21:22:43 +00001362{
Janos Follath865b3eb2019-12-16 11:46:15 +00001363 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Darryl Greenb33cc762019-11-28 14:29:44 +00001364 size_t in_buf_len = MBEDTLS_SSL_IN_BUFFER_LEN;
1365 size_t out_buf_len = MBEDTLS_SSL_OUT_BUFFER_LEN;
Paul Bakker5121ce52009-01-03 21:22:43 +00001366
Manuel Pégourié-Gonnarddef0bbe2015-05-04 14:56:36 +02001367 ssl->conf = conf;
Paul Bakker62f2dee2012-09-28 07:31:51 +00001368
Gilles Peskine449bd832023-01-11 14:50:10 +01001369 if ((ret = ssl_conf_check(ssl)) != 0) {
1370 return ret;
1371 }
Jerry Yu60835a82021-08-04 10:13:52 +08001372
Paul Bakker62f2dee2012-09-28 07:31:51 +00001373 /*
Manuel Pégourié-Gonnard06193482014-02-14 08:39:32 +01001374 * Prepare base structures
Paul Bakker62f2dee2012-09-28 07:31:51 +00001375 */
k-stachowiakc9a5f022018-07-24 13:53:31 +02001376
1377 /* Set to NULL in case of an error condition */
1378 ssl->out_buf = NULL;
k-stachowiaka47911c2018-07-04 17:41:58 +02001379
Darryl Greenb33cc762019-11-28 14:29:44 +00001380#if defined(MBEDTLS_SSL_VARIABLE_BUFFER_LENGTH)
1381 ssl->in_buf_len = in_buf_len;
1382#endif
Gilles Peskine449bd832023-01-11 14:50:10 +01001383 ssl->in_buf = mbedtls_calloc(1, in_buf_len);
1384 if (ssl->in_buf == NULL) {
1385 MBEDTLS_SSL_DEBUG_MSG(1, ("alloc(%" MBEDTLS_PRINTF_SIZET " bytes) failed", in_buf_len));
k-stachowiak9f7798e2018-07-31 16:52:32 +02001386 ret = MBEDTLS_ERR_SSL_ALLOC_FAILED;
k-stachowiaka47911c2018-07-04 17:41:58 +02001387 goto error;
Angus Grattond8213d02016-05-25 20:56:48 +10001388 }
1389
Darryl Greenb33cc762019-11-28 14:29:44 +00001390#if defined(MBEDTLS_SSL_VARIABLE_BUFFER_LENGTH)
1391 ssl->out_buf_len = out_buf_len;
1392#endif
Gilles Peskine449bd832023-01-11 14:50:10 +01001393 ssl->out_buf = mbedtls_calloc(1, out_buf_len);
1394 if (ssl->out_buf == NULL) {
1395 MBEDTLS_SSL_DEBUG_MSG(1, ("alloc(%" MBEDTLS_PRINTF_SIZET " bytes) failed", out_buf_len));
k-stachowiak9f7798e2018-07-31 16:52:32 +02001396 ret = MBEDTLS_ERR_SSL_ALLOC_FAILED;
k-stachowiaka47911c2018-07-04 17:41:58 +02001397 goto error;
Paul Bakker5121ce52009-01-03 21:22:43 +00001398 }
1399
Gilles Peskine449bd832023-01-11 14:50:10 +01001400 mbedtls_ssl_reset_in_out_pointers(ssl);
Manuel Pégourié-Gonnard419d5ae2015-05-04 19:32:36 +02001401
Johan Pascalb62bb512015-12-03 21:56:45 +01001402#if defined(MBEDTLS_SSL_DTLS_SRTP)
Gilles Peskine449bd832023-01-11 14:50:10 +01001403 memset(&ssl->dtls_srtp_info, 0, sizeof(ssl->dtls_srtp_info));
Johan Pascalb62bb512015-12-03 21:56:45 +01001404#endif
1405
Gilles Peskine449bd832023-01-11 14:50:10 +01001406 if ((ret = ssl_handshake_init(ssl)) != 0) {
k-stachowiaka47911c2018-07-04 17:41:58 +02001407 goto error;
Gilles Peskine449bd832023-01-11 14:50:10 +01001408 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001409
Gilles Peskine449bd832023-01-11 14:50:10 +01001410 return 0;
k-stachowiaka47911c2018-07-04 17:41:58 +02001411
1412error:
Gilles Peskine449bd832023-01-11 14:50:10 +01001413 mbedtls_free(ssl->in_buf);
1414 mbedtls_free(ssl->out_buf);
k-stachowiaka47911c2018-07-04 17:41:58 +02001415
1416 ssl->conf = NULL;
1417
Darryl Greenb33cc762019-11-28 14:29:44 +00001418#if defined(MBEDTLS_SSL_VARIABLE_BUFFER_LENGTH)
1419 ssl->in_buf_len = 0;
1420 ssl->out_buf_len = 0;
1421#endif
k-stachowiaka47911c2018-07-04 17:41:58 +02001422 ssl->in_buf = NULL;
1423 ssl->out_buf = NULL;
1424
1425 ssl->in_hdr = NULL;
1426 ssl->in_ctr = NULL;
1427 ssl->in_len = NULL;
1428 ssl->in_iv = NULL;
1429 ssl->in_msg = NULL;
1430
1431 ssl->out_hdr = NULL;
1432 ssl->out_ctr = NULL;
1433 ssl->out_len = NULL;
1434 ssl->out_iv = NULL;
1435 ssl->out_msg = NULL;
1436
Gilles Peskine449bd832023-01-11 14:50:10 +01001437 return ret;
Paul Bakker5121ce52009-01-03 21:22:43 +00001438}
1439
1440/*
Paul Bakker7eb013f2011-10-06 12:37:39 +00001441 * Reset an initialized and used SSL context for re-use while retaining
1442 * all application-set variables, function pointers and data.
Manuel Pégourié-Gonnard3f09b6d2015-09-08 11:58:14 +02001443 *
1444 * If partial is non-zero, keep data in the input buffer and client ID.
1445 * (Use when a DTLS client reconnects from the same port.)
Paul Bakker7eb013f2011-10-06 12:37:39 +00001446 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001447void mbedtls_ssl_session_reset_msg_layer(mbedtls_ssl_context *ssl,
1448 int partial)
Paul Bakker7eb013f2011-10-06 12:37:39 +00001449{
Darryl Greenb33cc762019-11-28 14:29:44 +00001450#if defined(MBEDTLS_SSL_VARIABLE_BUFFER_LENGTH)
1451 size_t in_buf_len = ssl->in_buf_len;
1452 size_t out_buf_len = ssl->out_buf_len;
1453#else
1454 size_t in_buf_len = MBEDTLS_SSL_IN_BUFFER_LEN;
1455 size_t out_buf_len = MBEDTLS_SSL_OUT_BUFFER_LEN;
1456#endif
Paul Bakker48916f92012-09-16 19:57:18 +00001457
Hanno Beckerb0302c42021-08-03 09:39:42 +01001458#if !defined(MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE) || !defined(MBEDTLS_SSL_SRV_C)
1459 partial = 0;
Hanno Becker7e772132018-08-10 12:38:21 +01001460#endif
1461
Manuel Pégourié-Gonnard286a1362015-05-13 16:22:05 +02001462 /* Cancel any possibly running timer */
Gilles Peskine449bd832023-01-11 14:50:10 +01001463 mbedtls_ssl_set_timer(ssl, 0);
Manuel Pégourié-Gonnard286a1362015-05-13 16:22:05 +02001464
Gilles Peskine449bd832023-01-11 14:50:10 +01001465 mbedtls_ssl_reset_in_out_pointers(ssl);
Hanno Beckerb0302c42021-08-03 09:39:42 +01001466
1467 /* Reset incoming message parsing */
1468 ssl->in_offt = NULL;
1469 ssl->nb_zero = 0;
1470 ssl->in_msgtype = 0;
1471 ssl->in_msglen = 0;
1472 ssl->in_hslen = 0;
1473 ssl->keep_current_message = 0;
1474 ssl->transform_in = NULL;
1475
1476#if defined(MBEDTLS_SSL_PROTO_DTLS)
1477 ssl->next_record_offset = 0;
1478 ssl->in_epoch = 0;
1479#endif
1480
1481 /* Keep current datagram if partial == 1 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001482 if (partial == 0) {
Hanno Beckerb0302c42021-08-03 09:39:42 +01001483 ssl->in_left = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +01001484 memset(ssl->in_buf, 0, in_buf_len);
Hanno Beckerb0302c42021-08-03 09:39:42 +01001485 }
1486
Ronald Cronad8c17b2022-06-10 17:18:09 +02001487 ssl->send_alert = 0;
1488
Hanno Beckerb0302c42021-08-03 09:39:42 +01001489 /* Reset outgoing message writing */
1490 ssl->out_msgtype = 0;
1491 ssl->out_msglen = 0;
1492 ssl->out_left = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +01001493 memset(ssl->out_buf, 0, out_buf_len);
1494 memset(ssl->cur_out_ctr, 0, sizeof(ssl->cur_out_ctr));
Hanno Beckerb0302c42021-08-03 09:39:42 +01001495 ssl->transform_out = NULL;
1496
1497#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
Gilles Peskine449bd832023-01-11 14:50:10 +01001498 mbedtls_ssl_dtls_replay_reset(ssl);
Hanno Beckerb0302c42021-08-03 09:39:42 +01001499#endif
1500
XiaokangQian2b01dc32022-01-21 02:53:13 +00001501#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
Gilles Peskine449bd832023-01-11 14:50:10 +01001502 if (ssl->transform) {
1503 mbedtls_ssl_transform_free(ssl->transform);
1504 mbedtls_free(ssl->transform);
Hanno Beckerb0302c42021-08-03 09:39:42 +01001505 ssl->transform = NULL;
1506 }
XiaokangQian2b01dc32022-01-21 02:53:13 +00001507#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
1508
XiaokangQian2b01dc32022-01-21 02:53:13 +00001509#if defined(MBEDTLS_SSL_PROTO_TLS1_3)
Gilles Peskine449bd832023-01-11 14:50:10 +01001510 mbedtls_ssl_transform_free(ssl->transform_application);
1511 mbedtls_free(ssl->transform_application);
XiaokangQian2b01dc32022-01-21 02:53:13 +00001512 ssl->transform_application = NULL;
1513
Gilles Peskine449bd832023-01-11 14:50:10 +01001514 if (ssl->handshake != NULL) {
Jerry Yu3d9b5902022-11-04 14:07:25 +08001515#if defined(MBEDTLS_SSL_EARLY_DATA)
Gilles Peskine449bd832023-01-11 14:50:10 +01001516 mbedtls_ssl_transform_free(ssl->handshake->transform_earlydata);
1517 mbedtls_free(ssl->handshake->transform_earlydata);
XiaokangQian2b01dc32022-01-21 02:53:13 +00001518 ssl->handshake->transform_earlydata = NULL;
Jerry Yu3d9b5902022-11-04 14:07:25 +08001519#endif
XiaokangQian2b01dc32022-01-21 02:53:13 +00001520
Gilles Peskine449bd832023-01-11 14:50:10 +01001521 mbedtls_ssl_transform_free(ssl->handshake->transform_handshake);
1522 mbedtls_free(ssl->handshake->transform_handshake);
XiaokangQian2b01dc32022-01-21 02:53:13 +00001523 ssl->handshake->transform_handshake = NULL;
1524 }
1525
XiaokangQian2b01dc32022-01-21 02:53:13 +00001526#endif /* MBEDTLS_SSL_PROTO_TLS1_3 */
Hanno Beckerb0302c42021-08-03 09:39:42 +01001527}
1528
Gilles Peskine449bd832023-01-11 14:50:10 +01001529int mbedtls_ssl_session_reset_int(mbedtls_ssl_context *ssl, int partial)
Hanno Beckerb0302c42021-08-03 09:39:42 +01001530{
1531 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
1532
1533 ssl->state = MBEDTLS_SSL_HELLO_REQUEST;
1534
Gilles Peskine449bd832023-01-11 14:50:10 +01001535 mbedtls_ssl_session_reset_msg_layer(ssl, partial);
Hanno Beckerb0302c42021-08-03 09:39:42 +01001536
1537 /* Reset renegotiation state */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001538#if defined(MBEDTLS_SSL_RENEGOTIATION)
1539 ssl->renego_status = MBEDTLS_SSL_INITIAL_HANDSHAKE;
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01001540 ssl->renego_records_seen = 0;
Paul Bakker48916f92012-09-16 19:57:18 +00001541
1542 ssl->verify_data_len = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +01001543 memset(ssl->own_verify_data, 0, MBEDTLS_SSL_VERIFY_DATA_MAX_LEN);
1544 memset(ssl->peer_verify_data, 0, MBEDTLS_SSL_VERIFY_DATA_MAX_LEN);
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01001545#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001546 ssl->secure_renegotiation = MBEDTLS_SSL_LEGACY_RENEGOTIATION;
Paul Bakker48916f92012-09-16 19:57:18 +00001547
Hanno Beckerb0302c42021-08-03 09:39:42 +01001548 ssl->session_in = NULL;
Hanno Becker78640902018-08-13 16:35:15 +01001549 ssl->session_out = NULL;
Gilles Peskine449bd832023-01-11 14:50:10 +01001550 if (ssl->session) {
1551 mbedtls_ssl_session_free(ssl->session);
1552 mbedtls_free(ssl->session);
Paul Bakkerc0463502013-02-14 11:19:38 +01001553 ssl->session = NULL;
1554 }
1555
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001556#if defined(MBEDTLS_SSL_ALPN)
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02001557 ssl->alpn_chosen = NULL;
1558#endif
1559
Manuel Pégourié-Gonnarde057d3b2015-05-20 10:59:43 +02001560#if defined(MBEDTLS_SSL_DTLS_HELLO_VERIFY) && defined(MBEDTLS_SSL_SRV_C)
David Horstmann3b2276a2022-10-06 14:49:08 +01001561 int free_cli_id = 1;
Hanno Becker4ccbf062018-08-10 11:20:38 +01001562#if defined(MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE)
Gilles Peskine449bd832023-01-11 14:50:10 +01001563 free_cli_id = (partial == 0);
Hanno Becker4ccbf062018-08-10 11:20:38 +01001564#endif
Gilles Peskine449bd832023-01-11 14:50:10 +01001565 if (free_cli_id) {
1566 mbedtls_free(ssl->cli_id);
Manuel Pégourié-Gonnard3f09b6d2015-09-08 11:58:14 +02001567 ssl->cli_id = NULL;
1568 ssl->cli_id_len = 0;
1569 }
Manuel Pégourié-Gonnard43c02182014-07-22 17:32:01 +02001570#endif
1571
Gilles Peskine449bd832023-01-11 14:50:10 +01001572 if ((ret = ssl_handshake_init(ssl)) != 0) {
1573 return ret;
1574 }
Paul Bakker2770fbd2012-07-03 13:30:23 +00001575
Gilles Peskine449bd832023-01-11 14:50:10 +01001576 return 0;
Paul Bakker7eb013f2011-10-06 12:37:39 +00001577}
1578
Manuel Pégourié-Gonnard779e4292013-08-03 13:50:48 +02001579/*
Manuel Pégourié-Gonnard3f09b6d2015-09-08 11:58:14 +02001580 * Reset an initialized and used SSL context for re-use while retaining
1581 * all application-set variables, function pointers and data.
1582 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001583int mbedtls_ssl_session_reset(mbedtls_ssl_context *ssl)
Manuel Pégourié-Gonnard3f09b6d2015-09-08 11:58:14 +02001584{
Gilles Peskine449bd832023-01-11 14:50:10 +01001585 return mbedtls_ssl_session_reset_int(ssl, 0);
Manuel Pégourié-Gonnard3f09b6d2015-09-08 11:58:14 +02001586}
1587
1588/*
Paul Bakker5121ce52009-01-03 21:22:43 +00001589 * SSL set accessors
1590 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001591void mbedtls_ssl_conf_endpoint(mbedtls_ssl_config *conf, int endpoint)
Paul Bakker5121ce52009-01-03 21:22:43 +00001592{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02001593 conf->endpoint = endpoint;
Paul Bakker5121ce52009-01-03 21:22:43 +00001594}
1595
Gilles Peskine449bd832023-01-11 14:50:10 +01001596void mbedtls_ssl_conf_transport(mbedtls_ssl_config *conf, int transport)
Manuel Pégourié-Gonnard0b1ff292014-02-06 13:04:16 +01001597{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02001598 conf->transport = transport;
Manuel Pégourié-Gonnard0b1ff292014-02-06 13:04:16 +01001599}
1600
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001601#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
Gilles Peskine449bd832023-01-11 14:50:10 +01001602void mbedtls_ssl_conf_dtls_anti_replay(mbedtls_ssl_config *conf, char mode)
Manuel Pégourié-Gonnard27393132014-09-24 14:41:11 +02001603{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02001604 conf->anti_replay = mode;
Manuel Pégourié-Gonnard27393132014-09-24 14:41:11 +02001605}
1606#endif
1607
Gilles Peskine449bd832023-01-11 14:50:10 +01001608void mbedtls_ssl_conf_dtls_badmac_limit(mbedtls_ssl_config *conf, unsigned limit)
Manuel Pégourié-Gonnardb0643d12014-10-14 18:30:36 +02001609{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02001610 conf->badmac_limit = limit;
Manuel Pégourié-Gonnardb0643d12014-10-14 18:30:36 +02001611}
Manuel Pégourié-Gonnardb0643d12014-10-14 18:30:36 +02001612
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001613#if defined(MBEDTLS_SSL_PROTO_DTLS)
Hanno Becker04da1892018-08-14 13:22:10 +01001614
Gilles Peskine449bd832023-01-11 14:50:10 +01001615void mbedtls_ssl_set_datagram_packing(mbedtls_ssl_context *ssl,
1616 unsigned allow_packing)
Hanno Becker04da1892018-08-14 13:22:10 +01001617{
1618 ssl->disable_datagram_packing = !allow_packing;
1619}
1620
Gilles Peskine449bd832023-01-11 14:50:10 +01001621void mbedtls_ssl_conf_handshake_timeout(mbedtls_ssl_config *conf,
1622 uint32_t min, uint32_t max)
Manuel Pégourié-Gonnard905dd242014-10-01 12:03:55 +02001623{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02001624 conf->hs_timeout_min = min;
1625 conf->hs_timeout_max = max;
Manuel Pégourié-Gonnard905dd242014-10-01 12:03:55 +02001626}
1627#endif
1628
Gilles Peskine449bd832023-01-11 14:50:10 +01001629void mbedtls_ssl_conf_authmode(mbedtls_ssl_config *conf, int authmode)
Paul Bakker5121ce52009-01-03 21:22:43 +00001630{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02001631 conf->authmode = authmode;
Paul Bakker5121ce52009-01-03 21:22:43 +00001632}
1633
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001634#if defined(MBEDTLS_X509_CRT_PARSE_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01001635void mbedtls_ssl_conf_verify(mbedtls_ssl_config *conf,
1636 int (*f_vrfy)(void *, mbedtls_x509_crt *, int, uint32_t *),
1637 void *p_vrfy)
Paul Bakkerb63b0af2011-01-13 17:54:59 +00001638{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02001639 conf->f_vrfy = f_vrfy;
1640 conf->p_vrfy = p_vrfy;
Paul Bakkerb63b0af2011-01-13 17:54:59 +00001641}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001642#endif /* MBEDTLS_X509_CRT_PARSE_C */
Paul Bakkerb63b0af2011-01-13 17:54:59 +00001643
Gilles Peskine449bd832023-01-11 14:50:10 +01001644void mbedtls_ssl_conf_rng(mbedtls_ssl_config *conf,
1645 int (*f_rng)(void *, unsigned char *, size_t),
1646 void *p_rng)
Paul Bakker5121ce52009-01-03 21:22:43 +00001647{
Manuel Pégourié-Gonnard750e4d72015-05-07 12:35:38 +01001648 conf->f_rng = f_rng;
1649 conf->p_rng = p_rng;
Paul Bakker5121ce52009-01-03 21:22:43 +00001650}
1651
Gilles Peskine449bd832023-01-11 14:50:10 +01001652void mbedtls_ssl_conf_dbg(mbedtls_ssl_config *conf,
1653 void (*f_dbg)(void *, int, const char *, int, const char *),
1654 void *p_dbg)
Paul Bakker5121ce52009-01-03 21:22:43 +00001655{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02001656 conf->f_dbg = f_dbg;
1657 conf->p_dbg = p_dbg;
Paul Bakker5121ce52009-01-03 21:22:43 +00001658}
1659
Gilles Peskine449bd832023-01-11 14:50:10 +01001660void mbedtls_ssl_set_bio(mbedtls_ssl_context *ssl,
1661 void *p_bio,
1662 mbedtls_ssl_send_t *f_send,
1663 mbedtls_ssl_recv_t *f_recv,
1664 mbedtls_ssl_recv_timeout_t *f_recv_timeout)
Manuel Pégourié-Gonnard8fa6dfd2014-09-17 10:47:43 +02001665{
1666 ssl->p_bio = p_bio;
1667 ssl->f_send = f_send;
1668 ssl->f_recv = f_recv;
1669 ssl->f_recv_timeout = f_recv_timeout;
Manuel Pégourié-Gonnard97fd52c2015-05-06 15:38:52 +01001670}
1671
Manuel Pégourié-Gonnard6e7aaca2018-08-20 10:37:23 +02001672#if defined(MBEDTLS_SSL_PROTO_DTLS)
Gilles Peskine449bd832023-01-11 14:50:10 +01001673void mbedtls_ssl_set_mtu(mbedtls_ssl_context *ssl, uint16_t mtu)
Manuel Pégourié-Gonnard6e7aaca2018-08-20 10:37:23 +02001674{
1675 ssl->mtu = mtu;
1676}
1677#endif
1678
Gilles Peskine449bd832023-01-11 14:50:10 +01001679void mbedtls_ssl_conf_read_timeout(mbedtls_ssl_config *conf, uint32_t timeout)
Manuel Pégourié-Gonnard97fd52c2015-05-06 15:38:52 +01001680{
1681 conf->read_timeout = timeout;
Manuel Pégourié-Gonnard8fa6dfd2014-09-17 10:47:43 +02001682}
1683
Gilles Peskine449bd832023-01-11 14:50:10 +01001684void mbedtls_ssl_set_timer_cb(mbedtls_ssl_context *ssl,
1685 void *p_timer,
1686 mbedtls_ssl_set_timer_t *f_set_timer,
1687 mbedtls_ssl_get_timer_t *f_get_timer)
Manuel Pégourié-Gonnard2e012912015-05-12 20:55:41 +02001688{
1689 ssl->p_timer = p_timer;
1690 ssl->f_set_timer = f_set_timer;
1691 ssl->f_get_timer = f_get_timer;
Manuel Pégourié-Gonnard286a1362015-05-13 16:22:05 +02001692
1693 /* Make sure we start with no timer running */
Gilles Peskine449bd832023-01-11 14:50:10 +01001694 mbedtls_ssl_set_timer(ssl, 0);
Manuel Pégourié-Gonnard2e012912015-05-12 20:55:41 +02001695}
1696
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001697#if defined(MBEDTLS_SSL_SRV_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01001698void mbedtls_ssl_conf_session_cache(mbedtls_ssl_config *conf,
1699 void *p_cache,
1700 mbedtls_ssl_cache_get_t *f_get_cache,
1701 mbedtls_ssl_cache_set_t *f_set_cache)
Paul Bakker5121ce52009-01-03 21:22:43 +00001702{
Manuel Pégourié-Gonnard5cb33082015-05-06 18:06:26 +01001703 conf->p_cache = p_cache;
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02001704 conf->f_get_cache = f_get_cache;
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02001705 conf->f_set_cache = f_set_cache;
Paul Bakker5121ce52009-01-03 21:22:43 +00001706}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001707#endif /* MBEDTLS_SSL_SRV_C */
Paul Bakker5121ce52009-01-03 21:22:43 +00001708
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001709#if defined(MBEDTLS_SSL_CLI_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01001710int mbedtls_ssl_set_session(mbedtls_ssl_context *ssl, const mbedtls_ssl_session *session)
Paul Bakker5121ce52009-01-03 21:22:43 +00001711{
Janos Follath865b3eb2019-12-16 11:46:15 +00001712 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +02001713
Gilles Peskine449bd832023-01-11 14:50:10 +01001714 if (ssl == NULL ||
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +02001715 session == NULL ||
1716 ssl->session_negotiate == NULL ||
Gilles Peskine449bd832023-01-11 14:50:10 +01001717 ssl->conf->endpoint != MBEDTLS_SSL_IS_CLIENT) {
1718 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +02001719 }
1720
Gilles Peskine449bd832023-01-11 14:50:10 +01001721 if (ssl->handshake->resume == 1) {
1722 return MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
1723 }
Hanno Beckere810bbc2021-05-14 16:01:05 +01001724
Jerry Yu21092062022-10-10 21:21:31 +08001725#if defined(MBEDTLS_SSL_PROTO_TLS1_3)
Gilles Peskine449bd832023-01-11 14:50:10 +01001726 if (session->tls_version == MBEDTLS_SSL_VERSION_TLS1_3) {
Jerry Yu21092062022-10-10 21:21:31 +08001727 const mbedtls_ssl_ciphersuite_t *ciphersuite_info =
Gilles Peskine449bd832023-01-11 14:50:10 +01001728 mbedtls_ssl_ciphersuite_from_id(session->ciphersuite);
Jerry Yu21092062022-10-10 21:21:31 +08001729
Gilles Peskine449bd832023-01-11 14:50:10 +01001730 if (mbedtls_ssl_validate_ciphersuite(
Jerry Yu21092062022-10-10 21:21:31 +08001731 ssl, ciphersuite_info, MBEDTLS_SSL_VERSION_TLS1_3,
Gilles Peskine449bd832023-01-11 14:50:10 +01001732 MBEDTLS_SSL_VERSION_TLS1_3) != 0) {
1733 MBEDTLS_SSL_DEBUG_MSG(4, ("%d is not a valid TLS 1.3 ciphersuite.",
1734 session->ciphersuite));
1735 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
Jerry Yu21092062022-10-10 21:21:31 +08001736 }
Jerry Yu40afab62022-10-08 10:42:13 +08001737 }
Jerry Yu21092062022-10-10 21:21:31 +08001738#endif /* MBEDTLS_SSL_PROTO_TLS1_3 */
Jerry Yu40afab62022-10-08 10:42:13 +08001739
Gilles Peskine449bd832023-01-11 14:50:10 +01001740 if ((ret = mbedtls_ssl_session_copy(ssl->session_negotiate,
1741 session)) != 0) {
1742 return ret;
1743 }
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +02001744
Paul Bakker0a597072012-09-25 21:55:46 +00001745 ssl->handshake->resume = 1;
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +02001746
Gilles Peskine449bd832023-01-11 14:50:10 +01001747 return 0;
Paul Bakker5121ce52009-01-03 21:22:43 +00001748}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001749#endif /* MBEDTLS_SSL_CLI_C */
Paul Bakker5121ce52009-01-03 21:22:43 +00001750
Gilles Peskine449bd832023-01-11 14:50:10 +01001751void mbedtls_ssl_conf_ciphersuites(mbedtls_ssl_config *conf,
1752 const int *ciphersuites)
Mateusz Starzyk06b07fb2021-02-18 13:55:21 +01001753{
Hanno Beckerd60b6c62021-04-29 12:04:11 +01001754 conf->ciphersuite_list = ciphersuites;
Paul Bakker5121ce52009-01-03 21:22:43 +00001755}
1756
Ronald Cron6f135e12021-12-08 16:57:54 +01001757#if defined(MBEDTLS_SSL_PROTO_TLS1_3)
Gilles Peskine449bd832023-01-11 14:50:10 +01001758void mbedtls_ssl_conf_tls13_key_exchange_modes(mbedtls_ssl_config *conf,
1759 const int kex_modes)
Hanno Becker71f1ed62021-07-24 06:01:47 +01001760{
Xiaofei Bai746f9482021-11-12 08:53:56 +00001761 conf->tls13_kex_modes = kex_modes & MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_ALL;
Hanno Becker71f1ed62021-07-24 06:01:47 +01001762}
Xiaokang Qian72de95d2022-10-25 02:54:33 +00001763
1764#if defined(MBEDTLS_SSL_EARLY_DATA)
Gilles Peskine449bd832023-01-11 14:50:10 +01001765void mbedtls_ssl_tls13_conf_early_data(mbedtls_ssl_config *conf,
1766 int early_data_enabled)
Xiaokang Qian72de95d2022-10-25 02:54:33 +00001767{
1768 conf->early_data_enabled = early_data_enabled;
1769}
Jerry Yucc4e0072022-11-22 17:22:22 +08001770
1771#if defined(MBEDTLS_SSL_SRV_C)
1772void mbedtls_ssl_tls13_conf_max_early_data_size(
Gilles Peskine449bd832023-01-11 14:50:10 +01001773 mbedtls_ssl_config *conf, uint32_t max_early_data_size)
Jerry Yucc4e0072022-11-22 17:22:22 +08001774{
Jerry Yu39da9852022-12-06 16:58:36 +08001775 conf->max_early_data_size = max_early_data_size;
Jerry Yucc4e0072022-11-22 17:22:22 +08001776}
1777#endif /* MBEDTLS_SSL_SRV_C */
1778
Xiaokang Qian72de95d2022-10-25 02:54:33 +00001779#endif /* MBEDTLS_SSL_EARLY_DATA */
Ronald Cron6f135e12021-12-08 16:57:54 +01001780#endif /* MBEDTLS_SSL_PROTO_TLS1_3 */
Hanno Becker71f1ed62021-07-24 06:01:47 +01001781
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001782#if defined(MBEDTLS_X509_CRT_PARSE_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01001783void mbedtls_ssl_conf_cert_profile(mbedtls_ssl_config *conf,
1784 const mbedtls_x509_crt_profile *profile)
Manuel Pégourié-Gonnard6e3ee3a2015-06-17 10:58:20 +02001785{
1786 conf->cert_profile = profile;
1787}
1788
Gilles Peskine449bd832023-01-11 14:50:10 +01001789static void ssl_key_cert_free(mbedtls_ssl_key_cert *key_cert)
Glenn Strauss36872db2022-01-22 05:06:31 -05001790{
1791 mbedtls_ssl_key_cert *cur = key_cert, *next;
1792
Gilles Peskine449bd832023-01-11 14:50:10 +01001793 while (cur != NULL) {
Glenn Strauss36872db2022-01-22 05:06:31 -05001794 next = cur->next;
Gilles Peskine449bd832023-01-11 14:50:10 +01001795 mbedtls_free(cur);
Glenn Strauss36872db2022-01-22 05:06:31 -05001796 cur = next;
1797 }
1798}
1799
Manuel Pégourié-Gonnard8f618a82015-05-10 21:13:36 +02001800/* Append a new keycert entry to a (possibly empty) list */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02001801MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01001802static int ssl_append_key_cert(mbedtls_ssl_key_cert **head,
1803 mbedtls_x509_crt *cert,
1804 mbedtls_pk_context *key)
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02001805{
niisato8ee24222018-06-25 19:05:48 +09001806 mbedtls_ssl_key_cert *new_cert;
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02001807
Gilles Peskine449bd832023-01-11 14:50:10 +01001808 if (cert == NULL) {
Glenn Strauss36872db2022-01-22 05:06:31 -05001809 /* Free list if cert is null */
Gilles Peskine449bd832023-01-11 14:50:10 +01001810 ssl_key_cert_free(*head);
Glenn Strauss36872db2022-01-22 05:06:31 -05001811 *head = NULL;
Gilles Peskine449bd832023-01-11 14:50:10 +01001812 return 0;
Glenn Strauss36872db2022-01-22 05:06:31 -05001813 }
1814
Gilles Peskine449bd832023-01-11 14:50:10 +01001815 new_cert = mbedtls_calloc(1, sizeof(mbedtls_ssl_key_cert));
1816 if (new_cert == NULL) {
1817 return MBEDTLS_ERR_SSL_ALLOC_FAILED;
1818 }
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02001819
niisato8ee24222018-06-25 19:05:48 +09001820 new_cert->cert = cert;
1821 new_cert->key = key;
1822 new_cert->next = NULL;
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02001823
Glenn Strauss36872db2022-01-22 05:06:31 -05001824 /* Update head if the list was null, else add to the end */
Gilles Peskine449bd832023-01-11 14:50:10 +01001825 if (*head == NULL) {
niisato8ee24222018-06-25 19:05:48 +09001826 *head = new_cert;
Gilles Peskine449bd832023-01-11 14:50:10 +01001827 } else {
Manuel Pégourié-Gonnard8f618a82015-05-10 21:13:36 +02001828 mbedtls_ssl_key_cert *cur = *head;
Gilles Peskine449bd832023-01-11 14:50:10 +01001829 while (cur->next != NULL) {
Manuel Pégourié-Gonnard8f618a82015-05-10 21:13:36 +02001830 cur = cur->next;
Gilles Peskine449bd832023-01-11 14:50:10 +01001831 }
niisato8ee24222018-06-25 19:05:48 +09001832 cur->next = new_cert;
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02001833 }
1834
Gilles Peskine449bd832023-01-11 14:50:10 +01001835 return 0;
Manuel Pégourié-Gonnard8f618a82015-05-10 21:13:36 +02001836}
1837
Gilles Peskine449bd832023-01-11 14:50:10 +01001838int mbedtls_ssl_conf_own_cert(mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnard8f618a82015-05-10 21:13:36 +02001839 mbedtls_x509_crt *own_cert,
Gilles Peskine449bd832023-01-11 14:50:10 +01001840 mbedtls_pk_context *pk_key)
Manuel Pégourié-Gonnard8f618a82015-05-10 21:13:36 +02001841{
Gilles Peskine449bd832023-01-11 14:50:10 +01001842 return ssl_append_key_cert(&conf->key_cert, own_cert, pk_key);
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02001843}
1844
Gilles Peskine449bd832023-01-11 14:50:10 +01001845void mbedtls_ssl_conf_ca_chain(mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnardbc2b7712015-05-06 11:14:19 +01001846 mbedtls_x509_crt *ca_chain,
Gilles Peskine449bd832023-01-11 14:50:10 +01001847 mbedtls_x509_crl *ca_crl)
Paul Bakker5121ce52009-01-03 21:22:43 +00001848{
Manuel Pégourié-Gonnardbc2b7712015-05-06 11:14:19 +01001849 conf->ca_chain = ca_chain;
1850 conf->ca_crl = ca_crl;
Hanno Becker5adaad92019-03-27 16:54:37 +00001851
1852#if defined(MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK)
1853 /* mbedtls_ssl_conf_ca_chain() and mbedtls_ssl_conf_ca_cb()
1854 * cannot be used together. */
1855 conf->f_ca_cb = NULL;
1856 conf->p_ca_cb = NULL;
1857#endif /* MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK */
Paul Bakker5121ce52009-01-03 21:22:43 +00001858}
Hanno Becker5adaad92019-03-27 16:54:37 +00001859
1860#if defined(MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK)
Gilles Peskine449bd832023-01-11 14:50:10 +01001861void mbedtls_ssl_conf_ca_cb(mbedtls_ssl_config *conf,
1862 mbedtls_x509_crt_ca_cb_t f_ca_cb,
1863 void *p_ca_cb)
Hanno Becker5adaad92019-03-27 16:54:37 +00001864{
1865 conf->f_ca_cb = f_ca_cb;
1866 conf->p_ca_cb = p_ca_cb;
1867
1868 /* mbedtls_ssl_conf_ca_chain() and mbedtls_ssl_conf_ca_cb()
1869 * cannot be used together. */
1870 conf->ca_chain = NULL;
1871 conf->ca_crl = NULL;
1872}
1873#endif /* MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001874#endif /* MBEDTLS_X509_CRT_PARSE_C */
Paul Bakkereb2c6582012-09-27 19:15:01 +00001875
Manuel Pégourié-Gonnard1af6c852015-05-10 23:10:37 +02001876#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
Gilles Peskine449bd832023-01-11 14:50:10 +01001877const unsigned char *mbedtls_ssl_get_hs_sni(mbedtls_ssl_context *ssl,
1878 size_t *name_len)
Glenn Strauss69894072022-01-24 12:58:00 -05001879{
1880 *name_len = ssl->handshake->sni_name_len;
Gilles Peskine449bd832023-01-11 14:50:10 +01001881 return ssl->handshake->sni_name;
Glenn Strauss69894072022-01-24 12:58:00 -05001882}
1883
Gilles Peskine449bd832023-01-11 14:50:10 +01001884int mbedtls_ssl_set_hs_own_cert(mbedtls_ssl_context *ssl,
1885 mbedtls_x509_crt *own_cert,
1886 mbedtls_pk_context *pk_key)
Manuel Pégourié-Gonnard1af6c852015-05-10 23:10:37 +02001887{
Gilles Peskine449bd832023-01-11 14:50:10 +01001888 return ssl_append_key_cert(&ssl->handshake->sni_key_cert,
1889 own_cert, pk_key);
Manuel Pégourié-Gonnard1af6c852015-05-10 23:10:37 +02001890}
Manuel Pégourié-Gonnard22bfa4b2015-05-11 08:46:37 +02001891
Gilles Peskine449bd832023-01-11 14:50:10 +01001892void mbedtls_ssl_set_hs_ca_chain(mbedtls_ssl_context *ssl,
1893 mbedtls_x509_crt *ca_chain,
1894 mbedtls_x509_crl *ca_crl)
Manuel Pégourié-Gonnard22bfa4b2015-05-11 08:46:37 +02001895{
1896 ssl->handshake->sni_ca_chain = ca_chain;
1897 ssl->handshake->sni_ca_crl = ca_crl;
1898}
Manuel Pégourié-Gonnardcdc26ae2015-06-19 12:16:31 +02001899
Glenn Strauss999ef702022-03-11 01:37:23 -05001900#if defined(MBEDTLS_KEY_EXCHANGE_CERT_REQ_ALLOWED_ENABLED)
Gilles Peskine449bd832023-01-11 14:50:10 +01001901void mbedtls_ssl_set_hs_dn_hints(mbedtls_ssl_context *ssl,
1902 const mbedtls_x509_crt *crt)
Glenn Strauss999ef702022-03-11 01:37:23 -05001903{
1904 ssl->handshake->dn_hints = crt;
1905}
1906#endif /* MBEDTLS_KEY_EXCHANGE_CERT_REQ_ALLOWED_ENABLED */
1907
Gilles Peskine449bd832023-01-11 14:50:10 +01001908void mbedtls_ssl_set_hs_authmode(mbedtls_ssl_context *ssl,
1909 int authmode)
Manuel Pégourié-Gonnardcdc26ae2015-06-19 12:16:31 +02001910{
1911 ssl->handshake->sni_authmode = authmode;
1912}
Manuel Pégourié-Gonnard1af6c852015-05-10 23:10:37 +02001913#endif /* MBEDTLS_SSL_SERVER_NAME_INDICATION */
1914
Hanno Becker8927c832019-04-03 12:52:50 +01001915#if defined(MBEDTLS_X509_CRT_PARSE_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01001916void mbedtls_ssl_set_verify(mbedtls_ssl_context *ssl,
1917 int (*f_vrfy)(void *, mbedtls_x509_crt *, int, uint32_t *),
1918 void *p_vrfy)
Hanno Becker8927c832019-04-03 12:52:50 +01001919{
1920 ssl->f_vrfy = f_vrfy;
1921 ssl->p_vrfy = p_vrfy;
1922}
1923#endif
1924
Manuel Pégourié-Gonnardeef142d2015-09-16 10:05:04 +02001925#if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
Manuel Pégourié-Gonnard7002f4a2015-09-15 12:43:43 +02001926
Valerio Setti016f6822022-12-09 14:17:50 +01001927#if defined(MBEDTLS_USE_PSA_CRYPTO)
1928static psa_status_t mbedtls_ssl_set_hs_ecjpake_password_common(
Gilles Peskine449bd832023-01-11 14:50:10 +01001929 mbedtls_ssl_context *ssl,
1930 mbedtls_svc_key_id_t pwd)
Valerio Setti016f6822022-12-09 14:17:50 +01001931{
1932 psa_status_t status;
1933 psa_pake_role_t psa_role;
1934 psa_pake_cipher_suite_t cipher_suite = psa_pake_cipher_suite_init();
1935
Gilles Peskine449bd832023-01-11 14:50:10 +01001936 psa_pake_cs_set_algorithm(&cipher_suite, PSA_ALG_JPAKE);
1937 psa_pake_cs_set_primitive(&cipher_suite,
1938 PSA_PAKE_PRIMITIVE(PSA_PAKE_PRIMITIVE_TYPE_ECC,
1939 PSA_ECC_FAMILY_SECP_R1,
1940 256));
1941 psa_pake_cs_set_hash(&cipher_suite, PSA_ALG_SHA_256);
Valerio Setti016f6822022-12-09 14:17:50 +01001942
Gilles Peskine449bd832023-01-11 14:50:10 +01001943 status = psa_pake_setup(&ssl->handshake->psa_pake_ctx, &cipher_suite);
1944 if (status != PSA_SUCCESS) {
Valerio Setti016f6822022-12-09 14:17:50 +01001945 return status;
Gilles Peskine449bd832023-01-11 14:50:10 +01001946 }
Manuel Pégourié-Gonnard7002f4a2015-09-15 12:43:43 +02001947
Gilles Peskine449bd832023-01-11 14:50:10 +01001948 if (ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER) {
Neil Armstrongca7d5062022-05-31 14:43:23 +02001949 psa_role = PSA_PAKE_ROLE_SERVER;
Gilles Peskine449bd832023-01-11 14:50:10 +01001950 } else {
Neil Armstrongca7d5062022-05-31 14:43:23 +02001951 psa_role = PSA_PAKE_ROLE_CLIENT;
Gilles Peskine449bd832023-01-11 14:50:10 +01001952 }
Neil Armstrongca7d5062022-05-31 14:43:23 +02001953
Gilles Peskine449bd832023-01-11 14:50:10 +01001954 status = psa_pake_set_role(&ssl->handshake->psa_pake_ctx, psa_role);
1955 if (status != PSA_SUCCESS) {
Valerio Setti016f6822022-12-09 14:17:50 +01001956 return status;
Gilles Peskine449bd832023-01-11 14:50:10 +01001957 }
Valerio Setti016f6822022-12-09 14:17:50 +01001958
Gilles Peskine449bd832023-01-11 14:50:10 +01001959 status = psa_pake_set_password_key(&ssl->handshake->psa_pake_ctx, pwd);
1960 if (status != PSA_SUCCESS) {
Valerio Setti016f6822022-12-09 14:17:50 +01001961 return status;
Gilles Peskine449bd832023-01-11 14:50:10 +01001962 }
Valerio Setti016f6822022-12-09 14:17:50 +01001963
1964 ssl->handshake->psa_pake_ctx_is_ok = 1;
1965
Gilles Peskine449bd832023-01-11 14:50:10 +01001966 return PSA_SUCCESS;
Valerio Setti016f6822022-12-09 14:17:50 +01001967}
1968
Gilles Peskine449bd832023-01-11 14:50:10 +01001969int mbedtls_ssl_set_hs_ecjpake_password(mbedtls_ssl_context *ssl,
1970 const unsigned char *pw,
1971 size_t pw_len)
Valerio Setti016f6822022-12-09 14:17:50 +01001972{
1973 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
1974 psa_status_t status;
1975
Gilles Peskine449bd832023-01-11 14:50:10 +01001976 if (ssl->handshake == NULL || ssl->conf == NULL) {
1977 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
Neil Armstrongca7d5062022-05-31 14:43:23 +02001978 }
1979
Gilles Peskine449bd832023-01-11 14:50:10 +01001980 /* Empty password is not valid */
1981 if ((pw == NULL) || (pw_len == 0)) {
1982 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
1983 }
1984
1985 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_DERIVE);
1986 psa_set_key_algorithm(&attributes, PSA_ALG_JPAKE);
1987 psa_set_key_type(&attributes, PSA_KEY_TYPE_PASSWORD);
1988
1989 status = psa_import_key(&attributes, pw, pw_len,
1990 &ssl->handshake->psa_pake_password);
1991 if (status != PSA_SUCCESS) {
1992 return MBEDTLS_ERR_SSL_HW_ACCEL_FAILED;
1993 }
1994
1995 status = mbedtls_ssl_set_hs_ecjpake_password_common(ssl,
1996 ssl->handshake->psa_pake_password);
1997 if (status != PSA_SUCCESS) {
1998 psa_destroy_key(ssl->handshake->psa_pake_password);
1999 psa_pake_abort(&ssl->handshake->psa_pake_ctx);
2000 return MBEDTLS_ERR_SSL_HW_ACCEL_FAILED;
2001 }
2002
2003 return 0;
Valerio Setti02c25b52022-11-15 14:08:42 +01002004}
Valerio Settia9a97dc2022-11-28 18:26:16 +01002005
Gilles Peskine449bd832023-01-11 14:50:10 +01002006int mbedtls_ssl_set_hs_ecjpake_password_opaque(mbedtls_ssl_context *ssl,
2007 mbedtls_svc_key_id_t pwd)
Valerio Settia9a97dc2022-11-28 18:26:16 +01002008{
Valerio Settia9a97dc2022-11-28 18:26:16 +01002009 psa_status_t status;
2010
Gilles Peskine449bd832023-01-11 14:50:10 +01002011 if (ssl->handshake == NULL || ssl->conf == NULL) {
2012 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
Neil Armstrongca7d5062022-05-31 14:43:23 +02002013 }
2014
Gilles Peskine449bd832023-01-11 14:50:10 +01002015 if (mbedtls_svc_key_id_is_null(pwd)) {
2016 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
2017 }
2018
2019 status = mbedtls_ssl_set_hs_ecjpake_password_common(ssl, pwd);
2020 if (status != PSA_SUCCESS) {
2021 psa_pake_abort(&ssl->handshake->psa_pake_ctx);
2022 return MBEDTLS_ERR_SSL_HW_ACCEL_FAILED;
2023 }
2024
2025 return 0;
Valerio Setti02c25b52022-11-15 14:08:42 +01002026}
2027#else /* MBEDTLS_USE_PSA_CRYPTO */
Gilles Peskine449bd832023-01-11 14:50:10 +01002028int mbedtls_ssl_set_hs_ecjpake_password(mbedtls_ssl_context *ssl,
2029 const unsigned char *pw,
2030 size_t pw_len)
Manuel Pégourié-Gonnard7002f4a2015-09-15 12:43:43 +02002031{
2032 mbedtls_ecjpake_role role;
2033
Gilles Peskine449bd832023-01-11 14:50:10 +01002034 if (ssl->handshake == NULL || ssl->conf == NULL) {
2035 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
2036 }
Manuel Pégourié-Gonnard7002f4a2015-09-15 12:43:43 +02002037
Valerio Settic689ed82022-12-07 14:40:38 +01002038 /* Empty password is not valid */
Gilles Peskine449bd832023-01-11 14:50:10 +01002039 if ((pw == NULL) || (pw_len == 0)) {
2040 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
2041 }
Valerio Settic689ed82022-12-07 14:40:38 +01002042
Gilles Peskine449bd832023-01-11 14:50:10 +01002043 if (ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER) {
Manuel Pégourié-Gonnard7002f4a2015-09-15 12:43:43 +02002044 role = MBEDTLS_ECJPAKE_SERVER;
Gilles Peskine449bd832023-01-11 14:50:10 +01002045 } else {
Manuel Pégourié-Gonnard7002f4a2015-09-15 12:43:43 +02002046 role = MBEDTLS_ECJPAKE_CLIENT;
Gilles Peskine449bd832023-01-11 14:50:10 +01002047 }
Manuel Pégourié-Gonnard7002f4a2015-09-15 12:43:43 +02002048
Gilles Peskine449bd832023-01-11 14:50:10 +01002049 return mbedtls_ecjpake_setup(&ssl->handshake->ecjpake_ctx,
2050 role,
2051 MBEDTLS_MD_SHA256,
2052 MBEDTLS_ECP_DP_SECP256R1,
2053 pw, pw_len);
Manuel Pégourié-Gonnard7002f4a2015-09-15 12:43:43 +02002054}
Valerio Setti02c25b52022-11-15 14:08:42 +01002055#endif /* MBEDTLS_USE_PSA_CRYPTO */
Manuel Pégourié-Gonnardeef142d2015-09-16 10:05:04 +02002056#endif /* MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED */
Manuel Pégourié-Gonnard7002f4a2015-09-15 12:43:43 +02002057
Ronald Cron73fe8df2022-10-05 14:31:43 +02002058#if defined(MBEDTLS_SSL_HANDSHAKE_WITH_PSK_ENABLED)
Gilles Peskine449bd832023-01-11 14:50:10 +01002059int mbedtls_ssl_conf_has_static_psk(mbedtls_ssl_config const *conf)
Hanno Becker2ed3dce2021-04-19 21:59:14 +01002060{
Gilles Peskine449bd832023-01-11 14:50:10 +01002061 if (conf->psk_identity == NULL ||
2062 conf->psk_identity_len == 0) {
2063 return 0;
Ronald Crond29e13e2022-10-19 10:33:48 +02002064 }
2065
Hanno Becker2ed3dce2021-04-19 21:59:14 +01002066#if defined(MBEDTLS_USE_PSA_CRYPTO)
Gilles Peskine449bd832023-01-11 14:50:10 +01002067 if (!mbedtls_svc_key_id_is_null(conf->psk_opaque)) {
2068 return 1;
2069 }
Hanno Becker2ed3dce2021-04-19 21:59:14 +01002070#endif /* MBEDTLS_USE_PSA_CRYPTO */
Ronald Crond29e13e2022-10-19 10:33:48 +02002071
Gilles Peskine449bd832023-01-11 14:50:10 +01002072 if (conf->psk != NULL && conf->psk_len != 0) {
2073 return 1;
2074 }
Hanno Becker2ed3dce2021-04-19 21:59:14 +01002075
Gilles Peskine449bd832023-01-11 14:50:10 +01002076 return 0;
Hanno Becker2ed3dce2021-04-19 21:59:14 +01002077}
2078
Gilles Peskine449bd832023-01-11 14:50:10 +01002079static void ssl_conf_remove_psk(mbedtls_ssl_config *conf)
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01002080{
2081 /* Remove reference to existing PSK, if any. */
2082#if defined(MBEDTLS_USE_PSA_CRYPTO)
Gilles Peskine449bd832023-01-11 14:50:10 +01002083 if (!mbedtls_svc_key_id_is_null(conf->psk_opaque)) {
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01002084 /* The maintenance of the PSK key slot is the
2085 * user's responsibility. */
Ronald Croncf56a0a2020-08-04 09:51:30 +02002086 conf->psk_opaque = MBEDTLS_SVC_KEY_ID_INIT;
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01002087 }
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01002088#endif /* MBEDTLS_USE_PSA_CRYPTO */
Gilles Peskine449bd832023-01-11 14:50:10 +01002089 if (conf->psk != NULL) {
2090 mbedtls_platform_zeroize(conf->psk, conf->psk_len);
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01002091
Gilles Peskine449bd832023-01-11 14:50:10 +01002092 mbedtls_free(conf->psk);
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01002093 conf->psk = NULL;
2094 conf->psk_len = 0;
2095 }
2096
2097 /* Remove reference to PSK identity, if any. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002098 if (conf->psk_identity != NULL) {
2099 mbedtls_free(conf->psk_identity);
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01002100 conf->psk_identity = NULL;
2101 conf->psk_identity_len = 0;
2102 }
2103}
2104
Hanno Becker7390c712018-11-15 13:33:04 +00002105/* This function assumes that PSK identity in the SSL config is unset.
2106 * It checks that the provided identity is well-formed and attempts
2107 * to make a copy of it in the SSL config.
2108 * On failure, the PSK identity in the config remains unset. */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02002109MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01002110static int ssl_conf_set_psk_identity(mbedtls_ssl_config *conf,
2111 unsigned char const *psk_identity,
2112 size_t psk_identity_len)
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02002113{
Manuel Pégourié-Gonnardc6b5d832015-08-27 16:37:35 +02002114 /* Identity len will be encoded on two bytes */
Gilles Peskine449bd832023-01-11 14:50:10 +01002115 if (psk_identity == NULL ||
Ronald Cron2a87e9b2022-10-19 10:55:26 +02002116 psk_identity_len == 0 ||
Gilles Peskine449bd832023-01-11 14:50:10 +01002117 (psk_identity_len >> 16) != 0 ||
2118 psk_identity_len > MBEDTLS_SSL_OUT_CONTENT_LEN) {
2119 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
Manuel Pégourié-Gonnardc6b5d832015-08-27 16:37:35 +02002120 }
2121
Gilles Peskine449bd832023-01-11 14:50:10 +01002122 conf->psk_identity = mbedtls_calloc(1, psk_identity_len);
2123 if (conf->psk_identity == NULL) {
2124 return MBEDTLS_ERR_SSL_ALLOC_FAILED;
2125 }
Paul Bakker6db455e2013-09-18 17:29:31 +02002126
Manuel Pégourié-Gonnard120fdbd2015-05-07 17:07:50 +01002127 conf->psk_identity_len = psk_identity_len;
Gilles Peskine449bd832023-01-11 14:50:10 +01002128 memcpy(conf->psk_identity, psk_identity, conf->psk_identity_len);
Paul Bakker5ad403f2013-09-18 21:21:30 +02002129
Gilles Peskine449bd832023-01-11 14:50:10 +01002130 return 0;
Paul Bakker6db455e2013-09-18 17:29:31 +02002131}
2132
Gilles Peskine449bd832023-01-11 14:50:10 +01002133int mbedtls_ssl_conf_psk(mbedtls_ssl_config *conf,
2134 const unsigned char *psk, size_t psk_len,
2135 const unsigned char *psk_identity, size_t psk_identity_len)
Hanno Becker7390c712018-11-15 13:33:04 +00002136{
Janos Follath865b3eb2019-12-16 11:46:15 +00002137 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Hanno Becker2ed3dce2021-04-19 21:59:14 +01002138
2139 /* We currently only support one PSK, raw or opaque. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002140 if (mbedtls_ssl_conf_has_static_psk(conf)) {
2141 return MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
2142 }
Hanno Becker7390c712018-11-15 13:33:04 +00002143
2144 /* Check and set raw PSK */
Gilles Peskine449bd832023-01-11 14:50:10 +01002145 if (psk == NULL) {
2146 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
2147 }
2148 if (psk_len == 0) {
2149 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
2150 }
2151 if (psk_len > MBEDTLS_PSK_MAX_LEN) {
2152 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
2153 }
Piotr Nowicki9926eaf2019-11-20 14:54:36 +01002154
Gilles Peskine449bd832023-01-11 14:50:10 +01002155 if ((conf->psk = mbedtls_calloc(1, psk_len)) == NULL) {
2156 return MBEDTLS_ERR_SSL_ALLOC_FAILED;
2157 }
Hanno Becker7390c712018-11-15 13:33:04 +00002158 conf->psk_len = psk_len;
Gilles Peskine449bd832023-01-11 14:50:10 +01002159 memcpy(conf->psk, psk, conf->psk_len);
Hanno Becker7390c712018-11-15 13:33:04 +00002160
2161 /* Check and set PSK Identity */
Gilles Peskine449bd832023-01-11 14:50:10 +01002162 ret = ssl_conf_set_psk_identity(conf, psk_identity, psk_identity_len);
2163 if (ret != 0) {
2164 ssl_conf_remove_psk(conf);
2165 }
Hanno Becker7390c712018-11-15 13:33:04 +00002166
Gilles Peskine449bd832023-01-11 14:50:10 +01002167 return ret;
Hanno Becker7390c712018-11-15 13:33:04 +00002168}
2169
Gilles Peskine449bd832023-01-11 14:50:10 +01002170static void ssl_remove_psk(mbedtls_ssl_context *ssl)
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01002171{
2172#if defined(MBEDTLS_USE_PSA_CRYPTO)
Gilles Peskine449bd832023-01-11 14:50:10 +01002173 if (!mbedtls_svc_key_id_is_null(ssl->handshake->psk_opaque)) {
Neil Armstrong501c9322022-05-03 09:35:09 +02002174 /* The maintenance of the external PSK key slot is the
2175 * user's responsibility. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002176 if (ssl->handshake->psk_opaque_is_internal) {
2177 psa_destroy_key(ssl->handshake->psk_opaque);
Neil Armstrong501c9322022-05-03 09:35:09 +02002178 ssl->handshake->psk_opaque_is_internal = 0;
2179 }
Ronald Croncf56a0a2020-08-04 09:51:30 +02002180 ssl->handshake->psk_opaque = MBEDTLS_SVC_KEY_ID_INIT;
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01002181 }
Neil Armstronge952a302022-05-03 10:22:14 +02002182#else
Gilles Peskine449bd832023-01-11 14:50:10 +01002183 if (ssl->handshake->psk != NULL) {
2184 mbedtls_platform_zeroize(ssl->handshake->psk,
2185 ssl->handshake->psk_len);
2186 mbedtls_free(ssl->handshake->psk);
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01002187 ssl->handshake->psk_len = 0;
2188 }
Neil Armstronge952a302022-05-03 10:22:14 +02002189#endif /* MBEDTLS_USE_PSA_CRYPTO */
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01002190}
2191
Gilles Peskine449bd832023-01-11 14:50:10 +01002192int mbedtls_ssl_set_hs_psk(mbedtls_ssl_context *ssl,
2193 const unsigned char *psk, size_t psk_len)
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01002194{
Neil Armstrong501c9322022-05-03 09:35:09 +02002195#if defined(MBEDTLS_USE_PSA_CRYPTO)
2196 psa_key_attributes_t key_attributes = psa_key_attributes_init();
Jerry Yu5d01c052022-08-17 10:18:10 +08002197 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Jerry Yu24b8c812022-08-20 19:06:56 +08002198 psa_algorithm_t alg = PSA_ALG_NONE;
Jerry Yu5d01c052022-08-17 10:18:10 +08002199 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Neil Armstrong501c9322022-05-03 09:35:09 +02002200#endif /* MBEDTLS_USE_PSA_CRYPTO */
2201
Gilles Peskine449bd832023-01-11 14:50:10 +01002202 if (psk == NULL || ssl->handshake == NULL) {
2203 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
2204 }
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01002205
Gilles Peskine449bd832023-01-11 14:50:10 +01002206 if (psk_len > MBEDTLS_PSK_MAX_LEN) {
2207 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
2208 }
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01002209
Gilles Peskine449bd832023-01-11 14:50:10 +01002210 ssl_remove_psk(ssl);
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01002211
Neil Armstrong501c9322022-05-03 09:35:09 +02002212#if defined(MBEDTLS_USE_PSA_CRYPTO)
Jerry Yuccc68a42022-07-26 16:39:20 +08002213#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
Gilles Peskine449bd832023-01-11 14:50:10 +01002214 if (ssl->tls_version == MBEDTLS_SSL_VERSION_TLS1_2) {
2215 if (ssl->handshake->ciphersuite_info->mac == MBEDTLS_MD_SHA384) {
2216 alg = PSA_ALG_TLS12_PSK_TO_MS(PSA_ALG_SHA_384);
2217 } else {
2218 alg = PSA_ALG_TLS12_PSK_TO_MS(PSA_ALG_SHA_256);
2219 }
2220 psa_set_key_usage_flags(&key_attributes, PSA_KEY_USAGE_DERIVE);
Jerry Yuccc68a42022-07-26 16:39:20 +08002221 }
2222#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Neil Armstrong501c9322022-05-03 09:35:09 +02002223
Jerry Yu568ec252022-07-22 21:27:34 +08002224#if defined(MBEDTLS_SSL_PROTO_TLS1_3)
Gilles Peskine449bd832023-01-11 14:50:10 +01002225 if (ssl->tls_version == MBEDTLS_SSL_VERSION_TLS1_3) {
2226 alg = PSA_ALG_HKDF_EXTRACT(PSA_ALG_ANY_HASH);
2227 psa_set_key_usage_flags(&key_attributes,
2228 PSA_KEY_USAGE_DERIVE | PSA_KEY_USAGE_EXPORT);
Jerry Yuccc68a42022-07-26 16:39:20 +08002229 }
2230#endif /* MBEDTLS_SSL_PROTO_TLS1_3 */
2231
Gilles Peskine449bd832023-01-11 14:50:10 +01002232 psa_set_key_algorithm(&key_attributes, alg);
2233 psa_set_key_type(&key_attributes, PSA_KEY_TYPE_DERIVE);
Neil Armstrong501c9322022-05-03 09:35:09 +02002234
Gilles Peskine449bd832023-01-11 14:50:10 +01002235 status = psa_import_key(&key_attributes, psk, psk_len, &key);
2236 if (status != PSA_SUCCESS) {
2237 return MBEDTLS_ERR_SSL_HW_ACCEL_FAILED;
2238 }
Neil Armstrong501c9322022-05-03 09:35:09 +02002239
2240 /* Allow calling psa_destroy_key() on psk remove */
2241 ssl->handshake->psk_opaque_is_internal = 1;
Gilles Peskine449bd832023-01-11 14:50:10 +01002242 return mbedtls_ssl_set_hs_psk_opaque(ssl, key);
Neil Armstrong501c9322022-05-03 09:35:09 +02002243#else
Gilles Peskine449bd832023-01-11 14:50:10 +01002244 if ((ssl->handshake->psk = mbedtls_calloc(1, psk_len)) == NULL) {
2245 return MBEDTLS_ERR_SSL_ALLOC_FAILED;
2246 }
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01002247
2248 ssl->handshake->psk_len = psk_len;
Gilles Peskine449bd832023-01-11 14:50:10 +01002249 memcpy(ssl->handshake->psk, psk, ssl->handshake->psk_len);
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01002250
Gilles Peskine449bd832023-01-11 14:50:10 +01002251 return 0;
Neil Armstrong501c9322022-05-03 09:35:09 +02002252#endif /* MBEDTLS_USE_PSA_CRYPTO */
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01002253}
2254
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01002255#if defined(MBEDTLS_USE_PSA_CRYPTO)
Gilles Peskine449bd832023-01-11 14:50:10 +01002256int mbedtls_ssl_conf_psk_opaque(mbedtls_ssl_config *conf,
2257 mbedtls_svc_key_id_t psk,
2258 const unsigned char *psk_identity,
2259 size_t psk_identity_len)
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01002260{
Janos Follath865b3eb2019-12-16 11:46:15 +00002261 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Hanno Becker2ed3dce2021-04-19 21:59:14 +01002262
2263 /* We currently only support one PSK, raw or opaque. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002264 if (mbedtls_ssl_conf_has_static_psk(conf)) {
2265 return MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
2266 }
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01002267
Hanno Becker7390c712018-11-15 13:33:04 +00002268 /* Check and set opaque PSK */
Gilles Peskine449bd832023-01-11 14:50:10 +01002269 if (mbedtls_svc_key_id_is_null(psk)) {
2270 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
2271 }
Ronald Croncf56a0a2020-08-04 09:51:30 +02002272 conf->psk_opaque = psk;
Hanno Becker7390c712018-11-15 13:33:04 +00002273
2274 /* Check and set PSK Identity */
Gilles Peskine449bd832023-01-11 14:50:10 +01002275 ret = ssl_conf_set_psk_identity(conf, psk_identity,
2276 psk_identity_len);
2277 if (ret != 0) {
2278 ssl_conf_remove_psk(conf);
2279 }
Hanno Becker7390c712018-11-15 13:33:04 +00002280
Gilles Peskine449bd832023-01-11 14:50:10 +01002281 return ret;
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01002282}
2283
Gilles Peskine449bd832023-01-11 14:50:10 +01002284int mbedtls_ssl_set_hs_psk_opaque(mbedtls_ssl_context *ssl,
2285 mbedtls_svc_key_id_t psk)
Przemyslaw Stekiel6928a512022-02-03 13:50:35 +01002286{
Gilles Peskine449bd832023-01-11 14:50:10 +01002287 if ((mbedtls_svc_key_id_is_null(psk)) ||
2288 (ssl->handshake == NULL)) {
2289 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
2290 }
Przemyslaw Stekiel6928a512022-02-03 13:50:35 +01002291
Gilles Peskine449bd832023-01-11 14:50:10 +01002292 ssl_remove_psk(ssl);
Przemyslaw Stekiel6928a512022-02-03 13:50:35 +01002293 ssl->handshake->psk_opaque = psk;
Gilles Peskine449bd832023-01-11 14:50:10 +01002294 return 0;
Przemyslaw Stekiel6928a512022-02-03 13:50:35 +01002295}
2296#endif /* MBEDTLS_USE_PSA_CRYPTO */
2297
Jerry Yu8897c072022-08-12 13:56:53 +08002298#if defined(MBEDTLS_SSL_SRV_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01002299void mbedtls_ssl_conf_psk_cb(mbedtls_ssl_config *conf,
2300 int (*f_psk)(void *, mbedtls_ssl_context *, const unsigned char *,
2301 size_t),
2302 void *p_psk)
Przemyslaw Stekiel6928a512022-02-03 13:50:35 +01002303{
2304 conf->f_psk = f_psk;
2305 conf->p_psk = p_psk;
2306}
Jerry Yu8897c072022-08-12 13:56:53 +08002307#endif /* MBEDTLS_SSL_SRV_C */
2308
Ronald Cron73fe8df2022-10-05 14:31:43 +02002309#endif /* MBEDTLS_SSL_HANDSHAKE_WITH_PSK_ENABLED */
Przemyslaw Stekiel6928a512022-02-03 13:50:35 +01002310
2311#if defined(MBEDTLS_USE_PSA_CRYPTO)
Gilles Peskine301711e2022-04-26 16:57:05 +02002312static mbedtls_ssl_mode_t mbedtls_ssl_get_base_mode(
Gilles Peskine449bd832023-01-11 14:50:10 +01002313 psa_algorithm_t alg)
Neil Armstrong8a0f3e82022-03-30 10:57:37 +02002314{
Neil Armstrong8a0f3e82022-03-30 10:57:37 +02002315#if defined(MBEDTLS_SSL_SOME_SUITES_USE_MAC)
Gilles Peskine449bd832023-01-11 14:50:10 +01002316 if (alg == PSA_ALG_CBC_NO_PADDING) {
2317 return MBEDTLS_SSL_MODE_CBC;
2318 }
Neil Armstrong8a0f3e82022-03-30 10:57:37 +02002319#endif /* MBEDTLS_SSL_SOME_SUITES_USE_MAC */
Gilles Peskine449bd832023-01-11 14:50:10 +01002320 if (PSA_ALG_IS_AEAD(alg)) {
2321 return MBEDTLS_SSL_MODE_AEAD;
2322 }
2323 return MBEDTLS_SSL_MODE_STREAM;
Gilles Peskine301711e2022-04-26 16:57:05 +02002324}
2325
2326#else /* MBEDTLS_USE_PSA_CRYPTO */
2327
2328static mbedtls_ssl_mode_t mbedtls_ssl_get_base_mode(
Gilles Peskine449bd832023-01-11 14:50:10 +01002329 mbedtls_cipher_mode_t mode)
Gilles Peskine301711e2022-04-26 16:57:05 +02002330{
2331#if defined(MBEDTLS_SSL_SOME_SUITES_USE_MAC)
Gilles Peskine449bd832023-01-11 14:50:10 +01002332 if (mode == MBEDTLS_MODE_CBC) {
2333 return MBEDTLS_SSL_MODE_CBC;
2334 }
Gilles Peskine301711e2022-04-26 16:57:05 +02002335#endif /* MBEDTLS_SSL_SOME_SUITES_USE_MAC */
2336
Neil Armstrong8a0f3e82022-03-30 10:57:37 +02002337#if defined(MBEDTLS_GCM_C) || \
2338 defined(MBEDTLS_CCM_C) || \
2339 defined(MBEDTLS_CHACHAPOLY_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01002340 if (mode == MBEDTLS_MODE_GCM ||
Neil Armstrong8a0f3e82022-03-30 10:57:37 +02002341 mode == MBEDTLS_MODE_CCM ||
Gilles Peskine449bd832023-01-11 14:50:10 +01002342 mode == MBEDTLS_MODE_CHACHAPOLY) {
2343 return MBEDTLS_SSL_MODE_AEAD;
2344 }
Neil Armstrong8a0f3e82022-03-30 10:57:37 +02002345#endif /* MBEDTLS_GCM_C || MBEDTLS_CCM_C || MBEDTLS_CHACHAPOLY_C */
Neil Armstrong8a0f3e82022-03-30 10:57:37 +02002346
Gilles Peskine449bd832023-01-11 14:50:10 +01002347 return MBEDTLS_SSL_MODE_STREAM;
Neil Armstrong8a0f3e82022-03-30 10:57:37 +02002348}
Gilles Peskine301711e2022-04-26 16:57:05 +02002349#endif /* MBEDTLS_USE_PSA_CRYPTO */
Neil Armstrong8a0f3e82022-03-30 10:57:37 +02002350
Gilles Peskinee108d982022-04-26 16:50:40 +02002351static mbedtls_ssl_mode_t mbedtls_ssl_get_actual_mode(
2352 mbedtls_ssl_mode_t base_mode,
Gilles Peskine449bd832023-01-11 14:50:10 +01002353 int encrypt_then_mac)
Gilles Peskinee108d982022-04-26 16:50:40 +02002354{
2355#if defined(MBEDTLS_SSL_SOME_SUITES_USE_CBC_ETM)
Gilles Peskine449bd832023-01-11 14:50:10 +01002356 if (encrypt_then_mac == MBEDTLS_SSL_ETM_ENABLED &&
2357 base_mode == MBEDTLS_SSL_MODE_CBC) {
2358 return MBEDTLS_SSL_MODE_CBC_ETM;
Gilles Peskinee108d982022-04-26 16:50:40 +02002359 }
2360#else
2361 (void) encrypt_then_mac;
2362#endif
Gilles Peskine449bd832023-01-11 14:50:10 +01002363 return base_mode;
Gilles Peskinee108d982022-04-26 16:50:40 +02002364}
2365
Neil Armstrongab555e02022-04-04 11:07:59 +02002366mbedtls_ssl_mode_t mbedtls_ssl_get_mode_from_transform(
Gilles Peskine449bd832023-01-11 14:50:10 +01002367 const mbedtls_ssl_transform *transform)
Neil Armstrong4bf4c862022-04-01 10:35:48 +02002368{
Gilles Peskinee108d982022-04-26 16:50:40 +02002369 mbedtls_ssl_mode_t base_mode = mbedtls_ssl_get_base_mode(
Neil Armstrong4bf4c862022-04-01 10:35:48 +02002370#if defined(MBEDTLS_USE_PSA_CRYPTO)
Gilles Peskine449bd832023-01-11 14:50:10 +01002371 transform->psa_alg
Neil Armstrong4bf4c862022-04-01 10:35:48 +02002372#else
Gilles Peskine449bd832023-01-11 14:50:10 +01002373 mbedtls_cipher_get_cipher_mode(&transform->cipher_ctx_enc)
Gilles Peskinee108d982022-04-26 16:50:40 +02002374#endif
2375 );
Neil Armstrong4bf4c862022-04-01 10:35:48 +02002376
Gilles Peskinee108d982022-04-26 16:50:40 +02002377 int encrypt_then_mac = 0;
Neil Armstrongf2c82f02022-04-05 11:16:53 +02002378#if defined(MBEDTLS_SSL_SOME_SUITES_USE_CBC_ETM)
Gilles Peskinee108d982022-04-26 16:50:40 +02002379 encrypt_then_mac = transform->encrypt_then_mac;
2380#endif
Gilles Peskine449bd832023-01-11 14:50:10 +01002381 return mbedtls_ssl_get_actual_mode(base_mode, encrypt_then_mac);
Neil Armstrong4bf4c862022-04-01 10:35:48 +02002382}
2383
Neil Armstrongab555e02022-04-04 11:07:59 +02002384mbedtls_ssl_mode_t mbedtls_ssl_get_mode_from_ciphersuite(
Neil Armstrongf2c82f02022-04-05 11:16:53 +02002385#if defined(MBEDTLS_SSL_SOME_SUITES_USE_CBC_ETM)
Gilles Peskine449bd832023-01-11 14:50:10 +01002386 int encrypt_then_mac,
Neil Armstrongf2c82f02022-04-05 11:16:53 +02002387#endif /* MBEDTLS_SSL_SOME_SUITES_USE_CBC_ETM */
Gilles Peskine449bd832023-01-11 14:50:10 +01002388 const mbedtls_ssl_ciphersuite_t *suite)
Neil Armstrong4bf4c862022-04-01 10:35:48 +02002389{
Gilles Peskinee108d982022-04-26 16:50:40 +02002390 mbedtls_ssl_mode_t base_mode = MBEDTLS_SSL_MODE_STREAM;
2391
Neil Armstrong4bf4c862022-04-01 10:35:48 +02002392#if defined(MBEDTLS_USE_PSA_CRYPTO)
2393 psa_status_t status;
2394 psa_algorithm_t alg;
2395 psa_key_type_t type;
2396 size_t size;
Gilles Peskine449bd832023-01-11 14:50:10 +01002397 status = mbedtls_ssl_cipher_to_psa(suite->cipher, 0, &alg, &type, &size);
2398 if (status == PSA_SUCCESS) {
2399 base_mode = mbedtls_ssl_get_base_mode(alg);
2400 }
Neil Armstrong4bf4c862022-04-01 10:35:48 +02002401#else
2402 const mbedtls_cipher_info_t *cipher =
Gilles Peskine449bd832023-01-11 14:50:10 +01002403 mbedtls_cipher_info_from_type(suite->cipher);
2404 if (cipher != NULL) {
Gilles Peskinee108d982022-04-26 16:50:40 +02002405 base_mode =
2406 mbedtls_ssl_get_base_mode(
Gilles Peskine449bd832023-01-11 14:50:10 +01002407 mbedtls_cipher_info_get_mode(cipher));
Gilles Peskinee108d982022-04-26 16:50:40 +02002408 }
Neil Armstrong4bf4c862022-04-01 10:35:48 +02002409#endif /* MBEDTLS_USE_PSA_CRYPTO */
2410
Gilles Peskinee108d982022-04-26 16:50:40 +02002411#if !defined(MBEDTLS_SSL_SOME_SUITES_USE_CBC_ETM)
2412 int encrypt_then_mac = 0;
2413#endif
Gilles Peskine449bd832023-01-11 14:50:10 +01002414 return mbedtls_ssl_get_actual_mode(base_mode, encrypt_then_mac);
Neil Armstrong4bf4c862022-04-01 10:35:48 +02002415}
2416
Neil Armstrong8395d7a2022-05-18 11:44:56 +02002417#if defined(MBEDTLS_USE_PSA_CRYPTO) || defined(MBEDTLS_SSL_PROTO_TLS1_3)
Jerry Yu251a12e2022-07-13 15:15:48 +08002418
2419#if defined(MBEDTLS_SSL_PROTO_TLS1_3)
Jerry Yu438ddd82022-07-07 06:55:50 +00002420/* Serialization of TLS 1.3 sessions:
2421 *
2422 * struct {
Xiaokang Qian126bf8e2022-10-13 02:22:40 +00002423 * opaque hostname<0..2^16-1>;
Jerry Yu438ddd82022-07-07 06:55:50 +00002424 * uint64 ticket_received;
2425 * uint32 ticket_lifetime;
Jerry Yu34191072022-08-18 10:32:09 +08002426 * opaque ticket<1..2^16-1>;
Jerry Yu438ddd82022-07-07 06:55:50 +00002427 * } ClientOnlyData;
2428 *
2429 * struct {
2430 * uint8 endpoint;
2431 * uint8 ciphersuite[2];
2432 * uint32 ticket_age_add;
2433 * uint8 ticket_flags;
2434 * opaque resumption_key<0..255>;
2435 * select ( endpoint ) {
2436 * case client: ClientOnlyData;
2437 * case server: uint64 start_time;
2438 * };
2439 * } serialized_session_tls13;
2440 *
2441 */
2442#if defined(MBEDTLS_SSL_SESSION_TICKETS)
Jerry Yue36fdd62022-08-17 21:31:36 +08002443MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01002444static int ssl_tls13_session_save(const mbedtls_ssl_session *session,
2445 unsigned char *buf,
2446 size_t buf_len,
2447 size_t *olen)
Jerry Yu438ddd82022-07-07 06:55:50 +00002448{
2449 unsigned char *p = buf;
Xiaokang Qiana3b451f2022-10-11 06:20:56 +00002450#if defined(MBEDTLS_SSL_CLI_C) && \
2451 defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
Gilles Peskine449bd832023-01-11 14:50:10 +01002452 size_t hostname_len = (session->hostname == NULL) ?
2453 0 : strlen(session->hostname) + 1;
Xiaokang Qiana3b451f2022-10-11 06:20:56 +00002454#endif
Jerry Yubc7c1a42022-07-21 22:57:37 +08002455 size_t needed = 1 /* endpoint */
2456 + 2 /* ciphersuite */
2457 + 4 /* ticket_age_add */
Jerry Yu34191072022-08-18 10:32:09 +08002458 + 1 /* ticket_flags */
2459 + 1; /* resumption_key length */
Jerry Yue36fdd62022-08-17 21:31:36 +08002460 *olen = 0;
Jerry Yu34191072022-08-18 10:32:09 +08002461
Gilles Peskine449bd832023-01-11 14:50:10 +01002462 if (session->resumption_key_len > MBEDTLS_SSL_TLS1_3_TICKET_RESUMPTION_KEY_LEN) {
2463 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
2464 }
Jerry Yu34191072022-08-18 10:32:09 +08002465 needed += session->resumption_key_len; /* resumption_key */
2466
Jerry Yu438ddd82022-07-07 06:55:50 +00002467#if defined(MBEDTLS_HAVE_TIME)
2468 needed += 8; /* start_time or ticket_received */
2469#endif
2470
2471#if defined(MBEDTLS_SSL_CLI_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01002472 if (session->endpoint == MBEDTLS_SSL_IS_CLIENT) {
Xiaokang Qian126bf8e2022-10-13 02:22:40 +00002473#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
Xiaokang Qianbc663a02022-10-09 11:14:39 +00002474 needed += 2 /* hostname_len */
Gilles Peskine449bd832023-01-11 14:50:10 +01002475 + hostname_len; /* hostname */
Xiaokang Qian281fd1b2022-09-20 11:35:41 +00002476#endif
2477
Jerry Yu438ddd82022-07-07 06:55:50 +00002478 needed += 4 /* ticket_lifetime */
Jerry Yue36fdd62022-08-17 21:31:36 +08002479 + 2; /* ticket_len */
Jerry Yu34191072022-08-18 10:32:09 +08002480
2481 /* Check size_t overflow */
Gilles Peskine449bd832023-01-11 14:50:10 +01002482 if (session->ticket_len > SIZE_MAX - needed) {
2483 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
2484 }
Jerry Yu34191072022-08-18 10:32:09 +08002485
Jerry Yue28d9742022-08-18 15:44:03 +08002486 needed += session->ticket_len; /* ticket */
Jerry Yu438ddd82022-07-07 06:55:50 +00002487 }
2488#endif /* MBEDTLS_SSL_CLI_C */
2489
Jerry Yue36fdd62022-08-17 21:31:36 +08002490 *olen = needed;
Gilles Peskine449bd832023-01-11 14:50:10 +01002491 if (needed > buf_len) {
2492 return MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL;
2493 }
Jerry Yu438ddd82022-07-07 06:55:50 +00002494
2495 p[0] = session->endpoint;
Gilles Peskine449bd832023-01-11 14:50:10 +01002496 MBEDTLS_PUT_UINT16_BE(session->ciphersuite, p, 1);
2497 MBEDTLS_PUT_UINT32_BE(session->ticket_age_add, p, 3);
Jerry Yu438ddd82022-07-07 06:55:50 +00002498 p[7] = session->ticket_flags;
2499
2500 /* save resumption_key */
Jerry Yubc7c1a42022-07-21 22:57:37 +08002501 p[8] = session->resumption_key_len;
Jerry Yu438ddd82022-07-07 06:55:50 +00002502 p += 9;
Gilles Peskine449bd832023-01-11 14:50:10 +01002503 memcpy(p, session->resumption_key, session->resumption_key_len);
Jerry Yubc7c1a42022-07-21 22:57:37 +08002504 p += session->resumption_key_len;
Jerry Yu438ddd82022-07-07 06:55:50 +00002505
2506#if defined(MBEDTLS_HAVE_TIME) && defined(MBEDTLS_SSL_SRV_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01002507 if (session->endpoint == MBEDTLS_SSL_IS_SERVER) {
2508 MBEDTLS_PUT_UINT64_BE((uint64_t) session->start, p, 0);
Jerry Yu438ddd82022-07-07 06:55:50 +00002509 p += 8;
2510 }
2511#endif /* MBEDTLS_HAVE_TIME */
2512
2513#if defined(MBEDTLS_SSL_CLI_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01002514 if (session->endpoint == MBEDTLS_SSL_IS_CLIENT) {
Xiaokang Qianed0620c2022-10-12 06:58:13 +00002515#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
Gilles Peskine449bd832023-01-11 14:50:10 +01002516 MBEDTLS_PUT_UINT16_BE(hostname_len, p, 0);
Xiaokang Qianed0620c2022-10-12 06:58:13 +00002517 p += 2;
Gilles Peskine449bd832023-01-11 14:50:10 +01002518 if (hostname_len > 0) {
Xiaokang Qianed0620c2022-10-12 06:58:13 +00002519 /* save host name */
Gilles Peskine449bd832023-01-11 14:50:10 +01002520 memcpy(p, session->hostname, hostname_len);
Xiaokang Qianed0620c2022-10-12 06:58:13 +00002521 p += hostname_len;
2522 }
2523#endif /* MBEDTLS_SSL_SERVER_NAME_INDICATION */
2524
Jerry Yu438ddd82022-07-07 06:55:50 +00002525#if defined(MBEDTLS_HAVE_TIME)
Gilles Peskine449bd832023-01-11 14:50:10 +01002526 MBEDTLS_PUT_UINT64_BE((uint64_t) session->ticket_received, p, 0);
Jerry Yu438ddd82022-07-07 06:55:50 +00002527 p += 8;
2528#endif
Gilles Peskine449bd832023-01-11 14:50:10 +01002529 MBEDTLS_PUT_UINT32_BE(session->ticket_lifetime, p, 0);
Jerry Yu438ddd82022-07-07 06:55:50 +00002530 p += 4;
2531
Gilles Peskine449bd832023-01-11 14:50:10 +01002532 MBEDTLS_PUT_UINT16_BE(session->ticket_len, p, 0);
Jerry Yu438ddd82022-07-07 06:55:50 +00002533 p += 2;
Jerry Yu34191072022-08-18 10:32:09 +08002534
Gilles Peskine449bd832023-01-11 14:50:10 +01002535 if (session->ticket != NULL && session->ticket_len > 0) {
2536 memcpy(p, session->ticket, session->ticket_len);
Jerry Yu438ddd82022-07-07 06:55:50 +00002537 p += session->ticket_len;
2538 }
2539 }
2540#endif /* MBEDTLS_SSL_CLI_C */
Gilles Peskine449bd832023-01-11 14:50:10 +01002541 return 0;
Jerry Yu438ddd82022-07-07 06:55:50 +00002542}
2543
2544MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01002545static int ssl_tls13_session_load(mbedtls_ssl_session *session,
2546 const unsigned char *buf,
2547 size_t len)
Jerry Yu438ddd82022-07-07 06:55:50 +00002548{
2549 const unsigned char *p = buf;
2550 const unsigned char *end = buf + len;
2551
Gilles Peskine449bd832023-01-11 14:50:10 +01002552 if (end - p < 9) {
2553 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
2554 }
Jerry Yu438ddd82022-07-07 06:55:50 +00002555 session->endpoint = p[0];
Gilles Peskine449bd832023-01-11 14:50:10 +01002556 session->ciphersuite = MBEDTLS_GET_UINT16_BE(p, 1);
2557 session->ticket_age_add = MBEDTLS_GET_UINT32_BE(p, 3);
Jerry Yu438ddd82022-07-07 06:55:50 +00002558 session->ticket_flags = p[7];
2559
2560 /* load resumption_key */
Jerry Yubc7c1a42022-07-21 22:57:37 +08002561 session->resumption_key_len = p[8];
Jerry Yu438ddd82022-07-07 06:55:50 +00002562 p += 9;
2563
Gilles Peskine449bd832023-01-11 14:50:10 +01002564 if (end - p < session->resumption_key_len) {
2565 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
2566 }
Jerry Yu438ddd82022-07-07 06:55:50 +00002567
Gilles Peskine449bd832023-01-11 14:50:10 +01002568 if (sizeof(session->resumption_key) < session->resumption_key_len) {
2569 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
2570 }
2571 memcpy(session->resumption_key, p, session->resumption_key_len);
Jerry Yubc7c1a42022-07-21 22:57:37 +08002572 p += session->resumption_key_len;
Jerry Yu438ddd82022-07-07 06:55:50 +00002573
2574#if defined(MBEDTLS_HAVE_TIME) && defined(MBEDTLS_SSL_SRV_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01002575 if (session->endpoint == MBEDTLS_SSL_IS_SERVER) {
2576 if (end - p < 8) {
2577 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
2578 }
2579 session->start = MBEDTLS_GET_UINT64_BE(p, 0);
Jerry Yu438ddd82022-07-07 06:55:50 +00002580 p += 8;
2581 }
2582#endif /* MBEDTLS_HAVE_TIME */
2583
2584#if defined(MBEDTLS_SSL_CLI_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01002585 if (session->endpoint == MBEDTLS_SSL_IS_CLIENT) {
Xiaokang Qianed0620c2022-10-12 06:58:13 +00002586#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION) && \
Gilles Peskine449bd832023-01-11 14:50:10 +01002587 defined(MBEDTLS_SSL_SESSION_TICKETS)
Xiaokang Qianed0620c2022-10-12 06:58:13 +00002588 size_t hostname_len;
2589 /* load host name */
Gilles Peskine449bd832023-01-11 14:50:10 +01002590 if (end - p < 2) {
2591 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
2592 }
2593 hostname_len = MBEDTLS_GET_UINT16_BE(p, 0);
Xiaokang Qianed0620c2022-10-12 06:58:13 +00002594 p += 2;
2595
Gilles Peskine449bd832023-01-11 14:50:10 +01002596 if (end - p < (long int) hostname_len) {
2597 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
2598 }
2599 if (hostname_len > 0) {
2600 session->hostname = mbedtls_calloc(1, hostname_len);
2601 if (session->hostname == NULL) {
2602 return MBEDTLS_ERR_SSL_ALLOC_FAILED;
2603 }
2604 memcpy(session->hostname, p, hostname_len);
Xiaokang Qianed0620c2022-10-12 06:58:13 +00002605 p += hostname_len;
2606 }
2607#endif /* MBEDTLS_SSL_SERVER_NAME_INDICATION &&
2608 MBEDTLS_SSL_SESSION_TICKETS */
2609
Jerry Yu438ddd82022-07-07 06:55:50 +00002610#if defined(MBEDTLS_HAVE_TIME)
Gilles Peskine449bd832023-01-11 14:50:10 +01002611 if (end - p < 8) {
2612 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
2613 }
2614 session->ticket_received = MBEDTLS_GET_UINT64_BE(p, 0);
Jerry Yu438ddd82022-07-07 06:55:50 +00002615 p += 8;
2616#endif
Gilles Peskine449bd832023-01-11 14:50:10 +01002617 if (end - p < 4) {
2618 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
2619 }
2620 session->ticket_lifetime = MBEDTLS_GET_UINT32_BE(p, 0);
Jerry Yu438ddd82022-07-07 06:55:50 +00002621 p += 4;
2622
Gilles Peskine449bd832023-01-11 14:50:10 +01002623 if (end - p < 2) {
2624 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
2625 }
2626 session->ticket_len = MBEDTLS_GET_UINT16_BE(p, 0);
Jerry Yu438ddd82022-07-07 06:55:50 +00002627 p += 2;
2628
Gilles Peskine449bd832023-01-11 14:50:10 +01002629 if (end - p < (long int) session->ticket_len) {
2630 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
2631 }
2632 if (session->ticket_len > 0) {
2633 session->ticket = mbedtls_calloc(1, session->ticket_len);
2634 if (session->ticket == NULL) {
2635 return MBEDTLS_ERR_SSL_ALLOC_FAILED;
2636 }
2637 memcpy(session->ticket, p, session->ticket_len);
Jerry Yu438ddd82022-07-07 06:55:50 +00002638 p += session->ticket_len;
2639 }
2640 }
2641#endif /* MBEDTLS_SSL_CLI_C */
2642
Gilles Peskine449bd832023-01-11 14:50:10 +01002643 return 0;
Jerry Yu438ddd82022-07-07 06:55:50 +00002644
2645}
2646#else /* MBEDTLS_SSL_SESSION_TICKETS */
Jerry Yue36fdd62022-08-17 21:31:36 +08002647MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01002648static int ssl_tls13_session_save(const mbedtls_ssl_session *session,
2649 unsigned char *buf,
2650 size_t buf_len,
2651 size_t *olen)
Jerry Yu251a12e2022-07-13 15:15:48 +08002652{
2653 ((void) session);
2654 ((void) buf);
2655 ((void) buf_len);
Jerry Yue36fdd62022-08-17 21:31:36 +08002656 *olen = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +01002657 return MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
Jerry Yu251a12e2022-07-13 15:15:48 +08002658}
Jerry Yu438ddd82022-07-07 06:55:50 +00002659
Gilles Peskine449bd832023-01-11 14:50:10 +01002660static int ssl_tls13_session_load(const mbedtls_ssl_session *session,
2661 unsigned char *buf,
2662 size_t buf_len)
Jerry Yu438ddd82022-07-07 06:55:50 +00002663{
2664 ((void) session);
2665 ((void) buf);
2666 ((void) buf_len);
Gilles Peskine449bd832023-01-11 14:50:10 +01002667 return MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
Jerry Yu438ddd82022-07-07 06:55:50 +00002668}
2669#endif /* !MBEDTLS_SSL_SESSION_TICKETS */
Jerry Yu251a12e2022-07-13 15:15:48 +08002670#endif /* MBEDTLS_SSL_PROTO_TLS1_3 */
2671
Gilles Peskine449bd832023-01-11 14:50:10 +01002672psa_status_t mbedtls_ssl_cipher_to_psa(mbedtls_cipher_type_t mbedtls_cipher_type,
2673 size_t taglen,
2674 psa_algorithm_t *alg,
2675 psa_key_type_t *key_type,
2676 size_t *key_size)
Przemyslaw Stekiel430f3372022-01-10 11:55:46 +01002677{
Gilles Peskine449bd832023-01-11 14:50:10 +01002678 switch (mbedtls_cipher_type) {
Przemyslaw Stekiel430f3372022-01-10 11:55:46 +01002679 case MBEDTLS_CIPHER_AES_128_CBC:
2680 *alg = PSA_ALG_CBC_NO_PADDING;
2681 *key_type = PSA_KEY_TYPE_AES;
2682 *key_size = 128;
2683 break;
2684 case MBEDTLS_CIPHER_AES_128_CCM:
Gilles Peskine449bd832023-01-11 14:50:10 +01002685 *alg = taglen ? PSA_ALG_AEAD_WITH_SHORTENED_TAG(PSA_ALG_CCM, taglen) : PSA_ALG_CCM;
Przemyslaw Stekiel430f3372022-01-10 11:55:46 +01002686 *key_type = PSA_KEY_TYPE_AES;
2687 *key_size = 128;
2688 break;
2689 case MBEDTLS_CIPHER_AES_128_GCM:
2690 *alg = PSA_ALG_GCM;
2691 *key_type = PSA_KEY_TYPE_AES;
2692 *key_size = 128;
2693 break;
2694 case MBEDTLS_CIPHER_AES_192_CCM:
Gilles Peskine449bd832023-01-11 14:50:10 +01002695 *alg = taglen ? PSA_ALG_AEAD_WITH_SHORTENED_TAG(PSA_ALG_CCM, taglen) : PSA_ALG_CCM;
Przemyslaw Stekiel430f3372022-01-10 11:55:46 +01002696 *key_type = PSA_KEY_TYPE_AES;
2697 *key_size = 192;
2698 break;
2699 case MBEDTLS_CIPHER_AES_192_GCM:
2700 *alg = PSA_ALG_GCM;
2701 *key_type = PSA_KEY_TYPE_AES;
2702 *key_size = 192;
2703 break;
2704 case MBEDTLS_CIPHER_AES_256_CBC:
2705 *alg = PSA_ALG_CBC_NO_PADDING;
2706 *key_type = PSA_KEY_TYPE_AES;
2707 *key_size = 256;
2708 break;
2709 case MBEDTLS_CIPHER_AES_256_CCM:
Gilles Peskine449bd832023-01-11 14:50:10 +01002710 *alg = taglen ? PSA_ALG_AEAD_WITH_SHORTENED_TAG(PSA_ALG_CCM, taglen) : PSA_ALG_CCM;
Przemyslaw Stekiel430f3372022-01-10 11:55:46 +01002711 *key_type = PSA_KEY_TYPE_AES;
2712 *key_size = 256;
2713 break;
2714 case MBEDTLS_CIPHER_AES_256_GCM:
2715 *alg = PSA_ALG_GCM;
2716 *key_type = PSA_KEY_TYPE_AES;
2717 *key_size = 256;
2718 break;
2719 case MBEDTLS_CIPHER_ARIA_128_CBC:
2720 *alg = PSA_ALG_CBC_NO_PADDING;
2721 *key_type = PSA_KEY_TYPE_ARIA;
2722 *key_size = 128;
2723 break;
2724 case MBEDTLS_CIPHER_ARIA_128_CCM:
Gilles Peskine449bd832023-01-11 14:50:10 +01002725 *alg = taglen ? PSA_ALG_AEAD_WITH_SHORTENED_TAG(PSA_ALG_CCM, taglen) : PSA_ALG_CCM;
Przemyslaw Stekiel430f3372022-01-10 11:55:46 +01002726 *key_type = PSA_KEY_TYPE_ARIA;
2727 *key_size = 128;
2728 break;
2729 case MBEDTLS_CIPHER_ARIA_128_GCM:
2730 *alg = PSA_ALG_GCM;
2731 *key_type = PSA_KEY_TYPE_ARIA;
2732 *key_size = 128;
2733 break;
2734 case MBEDTLS_CIPHER_ARIA_192_CCM:
Gilles Peskine449bd832023-01-11 14:50:10 +01002735 *alg = taglen ? PSA_ALG_AEAD_WITH_SHORTENED_TAG(PSA_ALG_CCM, taglen) : PSA_ALG_CCM;
Przemyslaw Stekiel430f3372022-01-10 11:55:46 +01002736 *key_type = PSA_KEY_TYPE_ARIA;
2737 *key_size = 192;
2738 break;
2739 case MBEDTLS_CIPHER_ARIA_192_GCM:
2740 *alg = PSA_ALG_GCM;
2741 *key_type = PSA_KEY_TYPE_ARIA;
2742 *key_size = 192;
2743 break;
2744 case MBEDTLS_CIPHER_ARIA_256_CBC:
2745 *alg = PSA_ALG_CBC_NO_PADDING;
2746 *key_type = PSA_KEY_TYPE_ARIA;
2747 *key_size = 256;
2748 break;
2749 case MBEDTLS_CIPHER_ARIA_256_CCM:
Gilles Peskine449bd832023-01-11 14:50:10 +01002750 *alg = taglen ? PSA_ALG_AEAD_WITH_SHORTENED_TAG(PSA_ALG_CCM, taglen) : PSA_ALG_CCM;
Przemyslaw Stekiel430f3372022-01-10 11:55:46 +01002751 *key_type = PSA_KEY_TYPE_ARIA;
2752 *key_size = 256;
2753 break;
2754 case MBEDTLS_CIPHER_ARIA_256_GCM:
2755 *alg = PSA_ALG_GCM;
2756 *key_type = PSA_KEY_TYPE_ARIA;
2757 *key_size = 256;
2758 break;
2759 case MBEDTLS_CIPHER_CAMELLIA_128_CBC:
2760 *alg = PSA_ALG_CBC_NO_PADDING;
2761 *key_type = PSA_KEY_TYPE_CAMELLIA;
2762 *key_size = 128;
2763 break;
2764 case MBEDTLS_CIPHER_CAMELLIA_128_CCM:
Gilles Peskine449bd832023-01-11 14:50:10 +01002765 *alg = taglen ? PSA_ALG_AEAD_WITH_SHORTENED_TAG(PSA_ALG_CCM, taglen) : PSA_ALG_CCM;
Przemyslaw Stekiel430f3372022-01-10 11:55:46 +01002766 *key_type = PSA_KEY_TYPE_CAMELLIA;
2767 *key_size = 128;
2768 break;
2769 case MBEDTLS_CIPHER_CAMELLIA_128_GCM:
2770 *alg = PSA_ALG_GCM;
2771 *key_type = PSA_KEY_TYPE_CAMELLIA;
2772 *key_size = 128;
2773 break;
2774 case MBEDTLS_CIPHER_CAMELLIA_192_CCM:
Gilles Peskine449bd832023-01-11 14:50:10 +01002775 *alg = taglen ? PSA_ALG_AEAD_WITH_SHORTENED_TAG(PSA_ALG_CCM, taglen) : PSA_ALG_CCM;
Przemyslaw Stekiel430f3372022-01-10 11:55:46 +01002776 *key_type = PSA_KEY_TYPE_CAMELLIA;
2777 *key_size = 192;
2778 break;
2779 case MBEDTLS_CIPHER_CAMELLIA_192_GCM:
2780 *alg = PSA_ALG_GCM;
2781 *key_type = PSA_KEY_TYPE_CAMELLIA;
2782 *key_size = 192;
2783 break;
2784 case MBEDTLS_CIPHER_CAMELLIA_256_CBC:
2785 *alg = PSA_ALG_CBC_NO_PADDING;
2786 *key_type = PSA_KEY_TYPE_CAMELLIA;
2787 *key_size = 256;
2788 break;
2789 case MBEDTLS_CIPHER_CAMELLIA_256_CCM:
Gilles Peskine449bd832023-01-11 14:50:10 +01002790 *alg = taglen ? PSA_ALG_AEAD_WITH_SHORTENED_TAG(PSA_ALG_CCM, taglen) : PSA_ALG_CCM;
Przemyslaw Stekiel430f3372022-01-10 11:55:46 +01002791 *key_type = PSA_KEY_TYPE_CAMELLIA;
2792 *key_size = 256;
2793 break;
2794 case MBEDTLS_CIPHER_CAMELLIA_256_GCM:
2795 *alg = PSA_ALG_GCM;
2796 *key_type = PSA_KEY_TYPE_CAMELLIA;
2797 *key_size = 256;
2798 break;
2799 case MBEDTLS_CIPHER_CHACHA20_POLY1305:
2800 *alg = PSA_ALG_CHACHA20_POLY1305;
2801 *key_type = PSA_KEY_TYPE_CHACHA20;
2802 *key_size = 256;
2803 break;
2804 case MBEDTLS_CIPHER_NULL:
2805 *alg = MBEDTLS_SSL_NULL_CIPHER;
2806 *key_type = 0;
2807 *key_size = 0;
2808 break;
2809 default:
2810 return PSA_ERROR_NOT_SUPPORTED;
2811 }
2812
2813 return PSA_SUCCESS;
2814}
Neil Armstrong8395d7a2022-05-18 11:44:56 +02002815#endif /* MBEDTLS_USE_PSA_CRYPTO || MBEDTLS_SSL_PROTO_TLS1_3 */
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01002816
Manuel Pégourié-Gonnardcf141ca2015-05-20 10:35:51 +02002817#if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_SSL_SRV_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01002818int mbedtls_ssl_conf_dh_param_bin(mbedtls_ssl_config *conf,
2819 const unsigned char *dhm_P, size_t P_len,
2820 const unsigned char *dhm_G, size_t G_len)
Hanno Beckera90658f2017-10-04 15:29:08 +01002821{
Janos Follath865b3eb2019-12-16 11:46:15 +00002822 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Hanno Beckera90658f2017-10-04 15:29:08 +01002823
Gilles Peskine449bd832023-01-11 14:50:10 +01002824 mbedtls_mpi_free(&conf->dhm_P);
2825 mbedtls_mpi_free(&conf->dhm_G);
Glenn Strausscee11292021-12-20 01:43:17 -05002826
Gilles Peskine449bd832023-01-11 14:50:10 +01002827 if ((ret = mbedtls_mpi_read_binary(&conf->dhm_P, dhm_P, P_len)) != 0 ||
2828 (ret = mbedtls_mpi_read_binary(&conf->dhm_G, dhm_G, G_len)) != 0) {
2829 mbedtls_mpi_free(&conf->dhm_P);
2830 mbedtls_mpi_free(&conf->dhm_G);
2831 return ret;
Hanno Beckera90658f2017-10-04 15:29:08 +01002832 }
2833
Gilles Peskine449bd832023-01-11 14:50:10 +01002834 return 0;
Hanno Beckera90658f2017-10-04 15:29:08 +01002835}
Paul Bakker5121ce52009-01-03 21:22:43 +00002836
Gilles Peskine449bd832023-01-11 14:50:10 +01002837int mbedtls_ssl_conf_dh_param_ctx(mbedtls_ssl_config *conf, mbedtls_dhm_context *dhm_ctx)
Paul Bakker1b57b062011-01-06 15:48:19 +00002838{
Janos Follath865b3eb2019-12-16 11:46:15 +00002839 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker1b57b062011-01-06 15:48:19 +00002840
Gilles Peskine449bd832023-01-11 14:50:10 +01002841 mbedtls_mpi_free(&conf->dhm_P);
2842 mbedtls_mpi_free(&conf->dhm_G);
Glenn Strausscee11292021-12-20 01:43:17 -05002843
Gilles Peskine449bd832023-01-11 14:50:10 +01002844 if ((ret = mbedtls_dhm_get_value(dhm_ctx, MBEDTLS_DHM_PARAM_P,
2845 &conf->dhm_P)) != 0 ||
2846 (ret = mbedtls_dhm_get_value(dhm_ctx, MBEDTLS_DHM_PARAM_G,
2847 &conf->dhm_G)) != 0) {
2848 mbedtls_mpi_free(&conf->dhm_P);
2849 mbedtls_mpi_free(&conf->dhm_G);
2850 return ret;
Manuel Pégourié-Gonnard1028b742015-05-06 17:33:07 +01002851 }
Paul Bakker1b57b062011-01-06 15:48:19 +00002852
Gilles Peskine449bd832023-01-11 14:50:10 +01002853 return 0;
Paul Bakker1b57b062011-01-06 15:48:19 +00002854}
Manuel Pégourié-Gonnardcf141ca2015-05-20 10:35:51 +02002855#endif /* MBEDTLS_DHM_C && MBEDTLS_SSL_SRV_C */
Paul Bakker1b57b062011-01-06 15:48:19 +00002856
Manuel Pégourié-Gonnardbd990d62015-06-11 14:49:42 +02002857#if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_SSL_CLI_C)
2858/*
2859 * Set the minimum length for Diffie-Hellman parameters
2860 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002861void mbedtls_ssl_conf_dhm_min_bitlen(mbedtls_ssl_config *conf,
2862 unsigned int bitlen)
Manuel Pégourié-Gonnardbd990d62015-06-11 14:49:42 +02002863{
2864 conf->dhm_min_bitlen = bitlen;
2865}
2866#endif /* MBEDTLS_DHM_C && MBEDTLS_SSL_CLI_C */
2867
Ronald Crone68ab4f2022-10-05 12:46:29 +02002868#if defined(MBEDTLS_SSL_HANDSHAKE_WITH_CERT_ENABLED)
Jerry Yu7ddc38c2022-01-19 11:08:05 +08002869#if !defined(MBEDTLS_DEPRECATED_REMOVED) && defined(MBEDTLS_SSL_PROTO_TLS1_2)
Manuel Pégourié-Gonnard36a8b572015-06-17 12:43:26 +02002870/*
2871 * Set allowed/preferred hashes for handshake signatures
2872 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002873void mbedtls_ssl_conf_sig_hashes(mbedtls_ssl_config *conf,
2874 const int *hashes)
Manuel Pégourié-Gonnard36a8b572015-06-17 12:43:26 +02002875{
2876 conf->sig_hashes = hashes;
2877}
Jerry Yu7ddc38c2022-01-19 11:08:05 +08002878#endif /* !MBEDTLS_DEPRECATED_REMOVED && MBEDTLS_SSL_PROTO_TLS1_2 */
Hanno Becker1cd6e002021-08-10 13:27:10 +01002879
Jerry Yuf017ee42022-01-12 15:49:48 +08002880/* Configure allowed signature algorithms for handshake */
Gilles Peskine449bd832023-01-11 14:50:10 +01002881void mbedtls_ssl_conf_sig_algs(mbedtls_ssl_config *conf,
2882 const uint16_t *sig_algs)
Hanno Becker1cd6e002021-08-10 13:27:10 +01002883{
Jerry Yuf017ee42022-01-12 15:49:48 +08002884#if !defined(MBEDTLS_DEPRECATED_REMOVED)
2885 conf->sig_hashes = NULL;
2886#endif /* !MBEDTLS_DEPRECATED_REMOVED */
2887 conf->sig_algs = sig_algs;
Hanno Becker1cd6e002021-08-10 13:27:10 +01002888}
Ronald Crone68ab4f2022-10-05 12:46:29 +02002889#endif /* MBEDTLS_SSL_HANDSHAKE_WITH_CERT_ENABLED */
Manuel Pégourié-Gonnard36a8b572015-06-17 12:43:26 +02002890
Manuel Pégourié-Gonnardb541da62015-06-17 11:43:30 +02002891#if defined(MBEDTLS_ECP_C)
Brett Warrene0edc842021-08-17 09:53:13 +01002892#if !defined(MBEDTLS_DEPRECATED_REMOVED)
Manuel Pégourié-Gonnard7f38ed02014-02-04 15:52:33 +01002893/*
2894 * Set the allowed elliptic curves
Brett Warrene0edc842021-08-17 09:53:13 +01002895 *
2896 * mbedtls_ssl_setup() takes the provided list
2897 * and translates it to a list of IANA TLS group identifiers,
2898 * stored in ssl->handshake->group_list.
2899 *
Manuel Pégourié-Gonnard7f38ed02014-02-04 15:52:33 +01002900 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002901void mbedtls_ssl_conf_curves(mbedtls_ssl_config *conf,
2902 const mbedtls_ecp_group_id *curve_list)
Manuel Pégourié-Gonnard7f38ed02014-02-04 15:52:33 +01002903{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02002904 conf->curve_list = curve_list;
Brett Warrene0edc842021-08-17 09:53:13 +01002905 conf->group_list = NULL;
Manuel Pégourié-Gonnard7f38ed02014-02-04 15:52:33 +01002906}
Brett Warrene0edc842021-08-17 09:53:13 +01002907#endif /* MBEDTLS_DEPRECATED_REMOVED */
Hanno Becker947194e2017-04-07 13:25:49 +01002908#endif /* MBEDTLS_ECP_C */
Manuel Pégourié-Gonnard7f38ed02014-02-04 15:52:33 +01002909
Brett Warrene0edc842021-08-17 09:53:13 +01002910/*
2911 * Set the allowed groups
2912 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002913void mbedtls_ssl_conf_groups(mbedtls_ssl_config *conf,
2914 const uint16_t *group_list)
Brett Warrene0edc842021-08-17 09:53:13 +01002915{
2916#if defined(MBEDTLS_ECP_C) && !defined(MBEDTLS_DEPRECATED_REMOVED)
2917 conf->curve_list = NULL;
2918#endif
2919 conf->group_list = group_list;
2920}
2921
Manuel Pégourié-Gonnardbc2b7712015-05-06 11:14:19 +01002922#if defined(MBEDTLS_X509_CRT_PARSE_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01002923int mbedtls_ssl_set_hostname(mbedtls_ssl_context *ssl, const char *hostname)
Paul Bakker5121ce52009-01-03 21:22:43 +00002924{
Hanno Becker947194e2017-04-07 13:25:49 +01002925 /* Initialize to suppress unnecessary compiler warning */
2926 size_t hostname_len = 0;
2927
2928 /* Check if new hostname is valid before
2929 * making any change to current one */
Gilles Peskine449bd832023-01-11 14:50:10 +01002930 if (hostname != NULL) {
2931 hostname_len = strlen(hostname);
Hanno Becker947194e2017-04-07 13:25:49 +01002932
Gilles Peskine449bd832023-01-11 14:50:10 +01002933 if (hostname_len > MBEDTLS_SSL_MAX_HOST_NAME_LEN) {
2934 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
2935 }
Hanno Becker947194e2017-04-07 13:25:49 +01002936 }
2937
2938 /* Now it's clear that we will overwrite the old hostname,
2939 * so we can free it safely */
2940
Gilles Peskine449bd832023-01-11 14:50:10 +01002941 if (ssl->hostname != NULL) {
2942 mbedtls_platform_zeroize(ssl->hostname, strlen(ssl->hostname));
2943 mbedtls_free(ssl->hostname);
Hanno Becker947194e2017-04-07 13:25:49 +01002944 }
2945
2946 /* Passing NULL as hostname shall clear the old one */
Manuel Pégourié-Gonnardba26c242015-05-06 10:47:06 +01002947
Gilles Peskine449bd832023-01-11 14:50:10 +01002948 if (hostname == NULL) {
Hanno Becker947194e2017-04-07 13:25:49 +01002949 ssl->hostname = NULL;
Gilles Peskine449bd832023-01-11 14:50:10 +01002950 } else {
2951 ssl->hostname = mbedtls_calloc(1, hostname_len + 1);
2952 if (ssl->hostname == NULL) {
2953 return MBEDTLS_ERR_SSL_ALLOC_FAILED;
2954 }
Paul Bakker75c1a6f2013-08-19 14:25:29 +02002955
Gilles Peskine449bd832023-01-11 14:50:10 +01002956 memcpy(ssl->hostname, hostname, hostname_len);
Paul Bakker75c1a6f2013-08-19 14:25:29 +02002957
Hanno Becker947194e2017-04-07 13:25:49 +01002958 ssl->hostname[hostname_len] = '\0';
2959 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002960
Gilles Peskine449bd832023-01-11 14:50:10 +01002961 return 0;
Paul Bakker5121ce52009-01-03 21:22:43 +00002962}
Hanno Becker1a9a51c2017-04-07 13:02:16 +01002963#endif /* MBEDTLS_X509_CRT_PARSE_C */
Paul Bakker5121ce52009-01-03 21:22:43 +00002964
Manuel Pégourié-Gonnardbc2b7712015-05-06 11:14:19 +01002965#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
Gilles Peskine449bd832023-01-11 14:50:10 +01002966void mbedtls_ssl_conf_sni(mbedtls_ssl_config *conf,
2967 int (*f_sni)(void *, mbedtls_ssl_context *,
2968 const unsigned char *, size_t),
2969 void *p_sni)
Paul Bakker5701cdc2012-09-27 21:49:42 +00002970{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02002971 conf->f_sni = f_sni;
2972 conf->p_sni = p_sni;
Paul Bakker5701cdc2012-09-27 21:49:42 +00002973}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002974#endif /* MBEDTLS_SSL_SERVER_NAME_INDICATION */
Paul Bakker5701cdc2012-09-27 21:49:42 +00002975
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002976#if defined(MBEDTLS_SSL_ALPN)
Gilles Peskine449bd832023-01-11 14:50:10 +01002977int mbedtls_ssl_conf_alpn_protocols(mbedtls_ssl_config *conf, const char **protos)
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02002978{
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +02002979 size_t cur_len, tot_len;
2980 const char **p;
2981
2982 /*
Brian J Murray1903fb32016-11-06 04:45:15 -08002983 * RFC 7301 3.1: "Empty strings MUST NOT be included and byte strings
2984 * MUST NOT be truncated."
2985 * We check lengths now rather than later.
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +02002986 */
2987 tot_len = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +01002988 for (p = protos; *p != NULL; p++) {
2989 cur_len = strlen(*p);
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +02002990 tot_len += cur_len;
2991
Gilles Peskine449bd832023-01-11 14:50:10 +01002992 if ((cur_len == 0) ||
2993 (cur_len > MBEDTLS_SSL_MAX_ALPN_NAME_LEN) ||
2994 (tot_len > MBEDTLS_SSL_MAX_ALPN_LIST_LEN)) {
2995 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
2996 }
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +02002997 }
2998
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02002999 conf->alpn_list = protos;
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +02003000
Gilles Peskine449bd832023-01-11 14:50:10 +01003001 return 0;
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02003002}
3003
Gilles Peskine449bd832023-01-11 14:50:10 +01003004const char *mbedtls_ssl_get_alpn_protocol(const mbedtls_ssl_context *ssl)
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02003005{
Gilles Peskine449bd832023-01-11 14:50:10 +01003006 return ssl->alpn_chosen;
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02003007}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003008#endif /* MBEDTLS_SSL_ALPN */
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02003009
Johan Pascalb62bb512015-12-03 21:56:45 +01003010#if defined(MBEDTLS_SSL_DTLS_SRTP)
Gilles Peskine449bd832023-01-11 14:50:10 +01003011void mbedtls_ssl_conf_srtp_mki_value_supported(mbedtls_ssl_config *conf,
3012 int support_mki_value)
Ron Eldor591f1622018-01-22 12:30:04 +02003013{
3014 conf->dtls_srtp_mki_support = support_mki_value;
3015}
3016
Gilles Peskine449bd832023-01-11 14:50:10 +01003017int mbedtls_ssl_dtls_srtp_set_mki_value(mbedtls_ssl_context *ssl,
3018 unsigned char *mki_value,
3019 uint16_t mki_len)
Ron Eldor591f1622018-01-22 12:30:04 +02003020{
Gilles Peskine449bd832023-01-11 14:50:10 +01003021 if (mki_len > MBEDTLS_TLS_SRTP_MAX_MKI_LENGTH) {
3022 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
Ron Eldora9788042018-12-05 11:04:31 +02003023 }
Ron Eldor591f1622018-01-22 12:30:04 +02003024
Gilles Peskine449bd832023-01-11 14:50:10 +01003025 if (ssl->conf->dtls_srtp_mki_support == MBEDTLS_SSL_DTLS_SRTP_MKI_UNSUPPORTED) {
3026 return MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
Ron Eldora9788042018-12-05 11:04:31 +02003027 }
Ron Eldor591f1622018-01-22 12:30:04 +02003028
Gilles Peskine449bd832023-01-11 14:50:10 +01003029 memcpy(ssl->dtls_srtp_info.mki_value, mki_value, mki_len);
Ron Eldor591f1622018-01-22 12:30:04 +02003030 ssl->dtls_srtp_info.mki_len = mki_len;
Gilles Peskine449bd832023-01-11 14:50:10 +01003031 return 0;
Ron Eldor591f1622018-01-22 12:30:04 +02003032}
3033
Gilles Peskine449bd832023-01-11 14:50:10 +01003034int mbedtls_ssl_conf_dtls_srtp_protection_profiles(mbedtls_ssl_config *conf,
3035 const mbedtls_ssl_srtp_profile *profiles)
Johan Pascalb62bb512015-12-03 21:56:45 +01003036{
Johan Pascal253d0262020-09-22 13:04:45 +02003037 const mbedtls_ssl_srtp_profile *p;
3038 size_t list_size = 0;
Johan Pascalb62bb512015-12-03 21:56:45 +01003039
Johan Pascal253d0262020-09-22 13:04:45 +02003040 /* check the profiles list: all entry must be valid,
3041 * its size cannot be more than the total number of supported profiles, currently 4 */
Gilles Peskine449bd832023-01-11 14:50:10 +01003042 for (p = profiles; *p != MBEDTLS_TLS_SRTP_UNSET &&
3043 list_size <= MBEDTLS_TLS_SRTP_MAX_PROFILE_LIST_LENGTH;
3044 p++) {
3045 if (mbedtls_ssl_check_srtp_profile_value(*p) != MBEDTLS_TLS_SRTP_UNSET) {
Johan Pascal76fdf1d2020-10-22 23:31:00 +02003046 list_size++;
Gilles Peskine449bd832023-01-11 14:50:10 +01003047 } else {
Johan Pascal76fdf1d2020-10-22 23:31:00 +02003048 /* unsupported value, stop parsing and set the size to an error value */
3049 list_size = MBEDTLS_TLS_SRTP_MAX_PROFILE_LIST_LENGTH + 1;
Johan Pascalb62bb512015-12-03 21:56:45 +01003050 }
3051 }
3052
Gilles Peskine449bd832023-01-11 14:50:10 +01003053 if (list_size > MBEDTLS_TLS_SRTP_MAX_PROFILE_LIST_LENGTH) {
3054 conf->dtls_srtp_profile_list = NULL;
3055 conf->dtls_srtp_profile_list_len = 0;
3056 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
Johan Pascal253d0262020-09-22 13:04:45 +02003057 }
3058
Johan Pascal9bc97ca2020-09-21 23:44:45 +02003059 conf->dtls_srtp_profile_list = profiles;
Johan Pascal253d0262020-09-22 13:04:45 +02003060 conf->dtls_srtp_profile_list_len = list_size;
Johan Pascalb62bb512015-12-03 21:56:45 +01003061
Gilles Peskine449bd832023-01-11 14:50:10 +01003062 return 0;
Johan Pascalb62bb512015-12-03 21:56:45 +01003063}
3064
Gilles Peskine449bd832023-01-11 14:50:10 +01003065void mbedtls_ssl_get_dtls_srtp_negotiation_result(const mbedtls_ssl_context *ssl,
3066 mbedtls_dtls_srtp_info *dtls_srtp_info)
Johan Pascalb62bb512015-12-03 21:56:45 +01003067{
Johan Pascal2258a4f2020-10-28 13:53:09 +01003068 dtls_srtp_info->chosen_dtls_srtp_profile = ssl->dtls_srtp_info.chosen_dtls_srtp_profile;
3069 /* do not copy the mki value if there is no chosen profile */
Gilles Peskine449bd832023-01-11 14:50:10 +01003070 if (dtls_srtp_info->chosen_dtls_srtp_profile == MBEDTLS_TLS_SRTP_UNSET) {
Johan Pascal2258a4f2020-10-28 13:53:09 +01003071 dtls_srtp_info->mki_len = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +01003072 } else {
Johan Pascal2258a4f2020-10-28 13:53:09 +01003073 dtls_srtp_info->mki_len = ssl->dtls_srtp_info.mki_len;
Gilles Peskine449bd832023-01-11 14:50:10 +01003074 memcpy(dtls_srtp_info->mki_value, ssl->dtls_srtp_info.mki_value,
3075 ssl->dtls_srtp_info.mki_len);
Johan Pascal2258a4f2020-10-28 13:53:09 +01003076 }
Johan Pascalb62bb512015-12-03 21:56:45 +01003077}
Johan Pascalb62bb512015-12-03 21:56:45 +01003078#endif /* MBEDTLS_SSL_DTLS_SRTP */
3079
Aditya Patwardhan3096f332022-07-26 14:31:46 +05303080#if !defined(MBEDTLS_DEPRECATED_REMOVED)
Gilles Peskine449bd832023-01-11 14:50:10 +01003081void mbedtls_ssl_conf_max_version(mbedtls_ssl_config *conf, int major, int minor)
Paul Bakker490ecc82011-10-06 13:04:09 +00003082{
Glenn Strauss2dfcea22022-03-14 17:26:42 -04003083 conf->max_tls_version = (major << 8) | minor;
Paul Bakker490ecc82011-10-06 13:04:09 +00003084}
3085
Gilles Peskine449bd832023-01-11 14:50:10 +01003086void mbedtls_ssl_conf_min_version(mbedtls_ssl_config *conf, int major, int minor)
Paul Bakker1d29fb52012-09-28 13:28:45 +00003087{
Glenn Strauss2dfcea22022-03-14 17:26:42 -04003088 conf->min_tls_version = (major << 8) | minor;
Paul Bakker1d29fb52012-09-28 13:28:45 +00003089}
Aditya Patwardhan3096f332022-07-26 14:31:46 +05303090#endif /* MBEDTLS_DEPRECATED_REMOVED */
Paul Bakker1d29fb52012-09-28 13:28:45 +00003091
Janos Follath088ce432017-04-10 12:42:31 +01003092#if defined(MBEDTLS_SSL_SRV_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01003093void mbedtls_ssl_conf_cert_req_ca_list(mbedtls_ssl_config *conf,
3094 char cert_req_ca_list)
Janos Follath088ce432017-04-10 12:42:31 +01003095{
3096 conf->cert_req_ca_list = cert_req_ca_list;
3097}
3098#endif
3099
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003100#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
Gilles Peskine449bd832023-01-11 14:50:10 +01003101void mbedtls_ssl_conf_encrypt_then_mac(mbedtls_ssl_config *conf, char etm)
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +01003102{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02003103 conf->encrypt_then_mac = etm;
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +01003104}
3105#endif
3106
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003107#if defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET)
Gilles Peskine449bd832023-01-11 14:50:10 +01003108void mbedtls_ssl_conf_extended_master_secret(mbedtls_ssl_config *conf, char ems)
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +02003109{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02003110 conf->extended_ms = ems;
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +02003111}
3112#endif
3113
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003114#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
Gilles Peskine449bd832023-01-11 14:50:10 +01003115int mbedtls_ssl_conf_max_frag_len(mbedtls_ssl_config *conf, unsigned char mfl_code)
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02003116{
Gilles Peskine449bd832023-01-11 14:50:10 +01003117 if (mfl_code >= MBEDTLS_SSL_MAX_FRAG_LEN_INVALID ||
3118 ssl_mfl_code_to_length(mfl_code) > MBEDTLS_TLS_EXT_ADV_CONTENT_LEN) {
3119 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02003120 }
3121
Manuel Pégourié-Gonnard6bf89d62015-05-05 17:01:57 +01003122 conf->mfl_code = mfl_code;
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02003123
Gilles Peskine449bd832023-01-11 14:50:10 +01003124 return 0;
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02003125}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003126#endif /* MBEDTLS_SSL_MAX_FRAGMENT_LENGTH */
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02003127
Gilles Peskine449bd832023-01-11 14:50:10 +01003128void mbedtls_ssl_conf_legacy_renegotiation(mbedtls_ssl_config *conf, int allow_legacy)
Paul Bakker48916f92012-09-16 19:57:18 +00003129{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02003130 conf->allow_legacy_renegotiation = allow_legacy;
Paul Bakker48916f92012-09-16 19:57:18 +00003131}
3132
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003133#if defined(MBEDTLS_SSL_RENEGOTIATION)
Gilles Peskine449bd832023-01-11 14:50:10 +01003134void mbedtls_ssl_conf_renegotiation(mbedtls_ssl_config *conf, int renegotiation)
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01003135{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02003136 conf->disable_renegotiation = renegotiation;
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01003137}
3138
Gilles Peskine449bd832023-01-11 14:50:10 +01003139void mbedtls_ssl_conf_renegotiation_enforced(mbedtls_ssl_config *conf, int max_records)
Manuel Pégourié-Gonnarda9964db2014-07-03 19:29:16 +02003140{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02003141 conf->renego_max_records = max_records;
Manuel Pégourié-Gonnarda9964db2014-07-03 19:29:16 +02003142}
3143
Gilles Peskine449bd832023-01-11 14:50:10 +01003144void mbedtls_ssl_conf_renegotiation_period(mbedtls_ssl_config *conf,
3145 const unsigned char period[8])
Manuel Pégourié-Gonnard837f0fe2014-11-05 13:58:53 +01003146{
Gilles Peskine449bd832023-01-11 14:50:10 +01003147 memcpy(conf->renego_period, period, 8);
Manuel Pégourié-Gonnard837f0fe2014-11-05 13:58:53 +01003148}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003149#endif /* MBEDTLS_SSL_RENEGOTIATION */
Paul Bakker5121ce52009-01-03 21:22:43 +00003150
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003151#if defined(MBEDTLS_SSL_SESSION_TICKETS)
Manuel Pégourié-Gonnardb596abf2015-05-20 10:45:29 +02003152#if defined(MBEDTLS_SSL_CLI_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01003153void mbedtls_ssl_conf_session_tickets(mbedtls_ssl_config *conf, int use_tickets)
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +02003154{
Manuel Pégourié-Gonnard2b494452015-05-06 10:05:11 +01003155 conf->session_tickets = use_tickets;
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +02003156}
Manuel Pégourié-Gonnardb596abf2015-05-20 10:45:29 +02003157#endif
Paul Bakker606b4ba2013-08-14 16:52:14 +02003158
Manuel Pégourié-Gonnardb596abf2015-05-20 10:45:29 +02003159#if defined(MBEDTLS_SSL_SRV_C)
Jerry Yu1ad7ace2022-08-09 13:28:39 +08003160
Jerry Yud0766ec2022-09-22 10:46:57 +08003161#if defined(MBEDTLS_SSL_PROTO_TLS1_3) && defined(MBEDTLS_SSL_SESSION_TICKETS)
Gilles Peskine449bd832023-01-11 14:50:10 +01003162void mbedtls_ssl_conf_new_session_tickets(mbedtls_ssl_config *conf,
3163 uint16_t num_tickets)
Jerry Yu1ad7ace2022-08-09 13:28:39 +08003164{
Jerry Yud0766ec2022-09-22 10:46:57 +08003165 conf->new_session_tickets_count = num_tickets;
Jerry Yu1ad7ace2022-08-09 13:28:39 +08003166}
3167#endif
3168
Gilles Peskine449bd832023-01-11 14:50:10 +01003169void mbedtls_ssl_conf_session_tickets_cb(mbedtls_ssl_config *conf,
3170 mbedtls_ssl_ticket_write_t *f_ticket_write,
3171 mbedtls_ssl_ticket_parse_t *f_ticket_parse,
3172 void *p_ticket)
Paul Bakker606b4ba2013-08-14 16:52:14 +02003173{
Manuel Pégourié-Gonnardd59675d2015-05-19 15:28:00 +02003174 conf->f_ticket_write = f_ticket_write;
3175 conf->f_ticket_parse = f_ticket_parse;
3176 conf->p_ticket = p_ticket;
Paul Bakker606b4ba2013-08-14 16:52:14 +02003177}
Manuel Pégourié-Gonnardb596abf2015-05-20 10:45:29 +02003178#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003179#endif /* MBEDTLS_SSL_SESSION_TICKETS */
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +02003180
Gilles Peskine449bd832023-01-11 14:50:10 +01003181void mbedtls_ssl_set_export_keys_cb(mbedtls_ssl_context *ssl,
3182 mbedtls_ssl_export_keys_t *f_export_keys,
3183 void *p_export_keys)
Robert Cragie4feb7ae2015-10-02 13:33:37 +01003184{
Hanno Becker7e6c1782021-06-08 09:24:55 +01003185 ssl->f_export_keys = f_export_keys;
3186 ssl->p_export_keys = p_export_keys;
Ron Eldorf5cc10d2019-05-07 18:33:40 +03003187}
Robert Cragie4feb7ae2015-10-02 13:33:37 +01003188
Gilles Peskineb74a1c72018-04-24 13:09:22 +02003189#if defined(MBEDTLS_SSL_ASYNC_PRIVATE)
Gilles Peskine8bf79f62018-01-05 21:11:53 +01003190void mbedtls_ssl_conf_async_private_cb(
3191 mbedtls_ssl_config *conf,
3192 mbedtls_ssl_async_sign_t *f_async_sign,
3193 mbedtls_ssl_async_decrypt_t *f_async_decrypt,
3194 mbedtls_ssl_async_resume_t *f_async_resume,
3195 mbedtls_ssl_async_cancel_t *f_async_cancel,
Gilles Peskine449bd832023-01-11 14:50:10 +01003196 void *async_config_data)
Gilles Peskine8bf79f62018-01-05 21:11:53 +01003197{
3198 conf->f_async_sign_start = f_async_sign;
3199 conf->f_async_decrypt_start = f_async_decrypt;
3200 conf->f_async_resume = f_async_resume;
3201 conf->f_async_cancel = f_async_cancel;
Gilles Peskinedf13d5c2018-04-25 20:39:48 +02003202 conf->p_async_config_data = async_config_data;
3203}
3204
Gilles Peskine449bd832023-01-11 14:50:10 +01003205void *mbedtls_ssl_conf_get_async_config_data(const mbedtls_ssl_config *conf)
Gilles Peskine8f97af72018-04-26 11:46:10 +02003206{
Gilles Peskine449bd832023-01-11 14:50:10 +01003207 return conf->p_async_config_data;
Gilles Peskine8f97af72018-04-26 11:46:10 +02003208}
3209
Gilles Peskine449bd832023-01-11 14:50:10 +01003210void *mbedtls_ssl_get_async_operation_data(const mbedtls_ssl_context *ssl)
Gilles Peskinedf13d5c2018-04-25 20:39:48 +02003211{
Gilles Peskine449bd832023-01-11 14:50:10 +01003212 if (ssl->handshake == NULL) {
3213 return NULL;
3214 } else {
3215 return ssl->handshake->user_async_ctx;
3216 }
Gilles Peskinedf13d5c2018-04-25 20:39:48 +02003217}
3218
Gilles Peskine449bd832023-01-11 14:50:10 +01003219void mbedtls_ssl_set_async_operation_data(mbedtls_ssl_context *ssl,
3220 void *ctx)
Gilles Peskinedf13d5c2018-04-25 20:39:48 +02003221{
Gilles Peskine449bd832023-01-11 14:50:10 +01003222 if (ssl->handshake != NULL) {
Gilles Peskinedf13d5c2018-04-25 20:39:48 +02003223 ssl->handshake->user_async_ctx = ctx;
Gilles Peskine449bd832023-01-11 14:50:10 +01003224 }
Gilles Peskine8bf79f62018-01-05 21:11:53 +01003225}
Gilles Peskineb74a1c72018-04-24 13:09:22 +02003226#endif /* MBEDTLS_SSL_ASYNC_PRIVATE */
Gilles Peskine8bf79f62018-01-05 21:11:53 +01003227
Paul Bakker5121ce52009-01-03 21:22:43 +00003228/*
3229 * SSL get accessors
3230 */
Gilles Peskine449bd832023-01-11 14:50:10 +01003231uint32_t mbedtls_ssl_get_verify_result(const mbedtls_ssl_context *ssl)
Paul Bakker5121ce52009-01-03 21:22:43 +00003232{
Gilles Peskine449bd832023-01-11 14:50:10 +01003233 if (ssl->session != NULL) {
3234 return ssl->session->verify_result;
3235 }
Manuel Pégourié-Gonnarde89163c2015-01-23 14:30:57 +00003236
Gilles Peskine449bd832023-01-11 14:50:10 +01003237 if (ssl->session_negotiate != NULL) {
3238 return ssl->session_negotiate->verify_result;
3239 }
Manuel Pégourié-Gonnarde89163c2015-01-23 14:30:57 +00003240
Gilles Peskine449bd832023-01-11 14:50:10 +01003241 return 0xFFFFFFFF;
Paul Bakker5121ce52009-01-03 21:22:43 +00003242}
3243
Gilles Peskine449bd832023-01-11 14:50:10 +01003244int mbedtls_ssl_get_ciphersuite_id_from_ssl(const mbedtls_ssl_context *ssl)
Glenn Strauss8f526902022-01-13 00:04:49 -05003245{
Gilles Peskine449bd832023-01-11 14:50:10 +01003246 if (ssl == NULL || ssl->session == NULL) {
3247 return 0;
3248 }
Glenn Strauss8f526902022-01-13 00:04:49 -05003249
Gilles Peskine449bd832023-01-11 14:50:10 +01003250 return ssl->session->ciphersuite;
Glenn Strauss8f526902022-01-13 00:04:49 -05003251}
3252
Gilles Peskine449bd832023-01-11 14:50:10 +01003253const char *mbedtls_ssl_get_ciphersuite(const mbedtls_ssl_context *ssl)
Paul Bakker72f62662011-01-16 21:27:44 +00003254{
Gilles Peskine449bd832023-01-11 14:50:10 +01003255 if (ssl == NULL || ssl->session == NULL) {
3256 return NULL;
3257 }
Paul Bakker926c8e42013-03-06 10:23:34 +01003258
Gilles Peskine449bd832023-01-11 14:50:10 +01003259 return mbedtls_ssl_get_ciphersuite_name(ssl->session->ciphersuite);
Paul Bakker72f62662011-01-16 21:27:44 +00003260}
3261
Gilles Peskine449bd832023-01-11 14:50:10 +01003262const char *mbedtls_ssl_get_version(const mbedtls_ssl_context *ssl)
Paul Bakker43ca69c2011-01-15 17:35:19 +00003263{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003264#if defined(MBEDTLS_SSL_PROTO_DTLS)
Gilles Peskine449bd832023-01-11 14:50:10 +01003265 if (ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM) {
3266 switch (ssl->tls_version) {
Glenn Strauss60bfe602022-03-14 19:04:24 -04003267 case MBEDTLS_SSL_VERSION_TLS1_2:
Gilles Peskine449bd832023-01-11 14:50:10 +01003268 return "DTLSv1.2";
Manuel Pégourié-Gonnardb21ca2a2014-02-10 13:43:33 +01003269 default:
Gilles Peskine449bd832023-01-11 14:50:10 +01003270 return "unknown (DTLS)";
Manuel Pégourié-Gonnardb21ca2a2014-02-10 13:43:33 +01003271 }
3272 }
3273#endif
3274
Gilles Peskine449bd832023-01-11 14:50:10 +01003275 switch (ssl->tls_version) {
Glenn Strauss60bfe602022-03-14 19:04:24 -04003276 case MBEDTLS_SSL_VERSION_TLS1_2:
Gilles Peskine449bd832023-01-11 14:50:10 +01003277 return "TLSv1.2";
Glenn Strauss60bfe602022-03-14 19:04:24 -04003278 case MBEDTLS_SSL_VERSION_TLS1_3:
Gilles Peskine449bd832023-01-11 14:50:10 +01003279 return "TLSv1.3";
Paul Bakker43ca69c2011-01-15 17:35:19 +00003280 default:
Gilles Peskine449bd832023-01-11 14:50:10 +01003281 return "unknown";
Paul Bakker43ca69c2011-01-15 17:35:19 +00003282 }
Paul Bakker43ca69c2011-01-15 17:35:19 +00003283}
3284
Manuel Pégourié-Gonnarda2cda6b2015-08-31 18:30:52 +02003285#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
Gilles Peskine449bd832023-01-11 14:50:10 +01003286size_t mbedtls_ssl_get_input_max_frag_len(const mbedtls_ssl_context *ssl)
Andrzej Kurek90c6e842020-04-03 05:25:29 -04003287{
David Horstmann95d516f2021-05-04 18:36:56 +01003288 size_t max_len = MBEDTLS_SSL_IN_CONTENT_LEN;
Andrzej Kurek90c6e842020-04-03 05:25:29 -04003289 size_t read_mfl;
3290
Jerry Yuddda0502022-12-01 19:43:12 +08003291#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
Andrzej Kurek90c6e842020-04-03 05:25:29 -04003292 /* Use the configured MFL for the client if we're past SERVER_HELLO_DONE */
Gilles Peskine449bd832023-01-11 14:50:10 +01003293 if (ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT &&
3294 ssl->state >= MBEDTLS_SSL_SERVER_HELLO_DONE) {
3295 return ssl_mfl_code_to_length(ssl->conf->mfl_code);
Andrzej Kurek90c6e842020-04-03 05:25:29 -04003296 }
Jerry Yuddda0502022-12-01 19:43:12 +08003297#endif
Andrzej Kurek90c6e842020-04-03 05:25:29 -04003298
3299 /* Check if a smaller max length was negotiated */
Gilles Peskine449bd832023-01-11 14:50:10 +01003300 if (ssl->session_out != NULL) {
3301 read_mfl = ssl_mfl_code_to_length(ssl->session_out->mfl_code);
3302 if (read_mfl < max_len) {
Andrzej Kurek90c6e842020-04-03 05:25:29 -04003303 max_len = read_mfl;
3304 }
3305 }
3306
Jerry Yuddda0502022-12-01 19:43:12 +08003307 /* During a handshake, use the value being negotiated */
Gilles Peskine449bd832023-01-11 14:50:10 +01003308 if (ssl->session_negotiate != NULL) {
3309 read_mfl = ssl_mfl_code_to_length(ssl->session_negotiate->mfl_code);
3310 if (read_mfl < max_len) {
Andrzej Kurek90c6e842020-04-03 05:25:29 -04003311 max_len = read_mfl;
3312 }
3313 }
3314
Gilles Peskine449bd832023-01-11 14:50:10 +01003315 return max_len;
Andrzej Kurek90c6e842020-04-03 05:25:29 -04003316}
3317
Gilles Peskine449bd832023-01-11 14:50:10 +01003318size_t mbedtls_ssl_get_output_max_frag_len(const mbedtls_ssl_context *ssl)
Manuel Pégourié-Gonnarda2cda6b2015-08-31 18:30:52 +02003319{
3320 size_t max_len;
3321
3322 /*
3323 * Assume mfl_code is correct since it was checked when set
3324 */
Gilles Peskine449bd832023-01-11 14:50:10 +01003325 max_len = ssl_mfl_code_to_length(ssl->conf->mfl_code);
Manuel Pégourié-Gonnarda2cda6b2015-08-31 18:30:52 +02003326
Manuel Pégourié-Gonnard2cb17e22017-09-19 13:00:47 +02003327 /* Check if a smaller max length was negotiated */
Gilles Peskine449bd832023-01-11 14:50:10 +01003328 if (ssl->session_out != NULL &&
3329 ssl_mfl_code_to_length(ssl->session_out->mfl_code) < max_len) {
3330 max_len = ssl_mfl_code_to_length(ssl->session_out->mfl_code);
Manuel Pégourié-Gonnarda2cda6b2015-08-31 18:30:52 +02003331 }
3332
Manuel Pégourié-Gonnard2cb17e22017-09-19 13:00:47 +02003333 /* During a handshake, use the value being negotiated */
Gilles Peskine449bd832023-01-11 14:50:10 +01003334 if (ssl->session_negotiate != NULL &&
3335 ssl_mfl_code_to_length(ssl->session_negotiate->mfl_code) < max_len) {
3336 max_len = ssl_mfl_code_to_length(ssl->session_negotiate->mfl_code);
Manuel Pégourié-Gonnard2cb17e22017-09-19 13:00:47 +02003337 }
3338
Gilles Peskine449bd832023-01-11 14:50:10 +01003339 return max_len;
Manuel Pégourié-Gonnarda2cda6b2015-08-31 18:30:52 +02003340}
3341#endif /* MBEDTLS_SSL_MAX_FRAGMENT_LENGTH */
3342
Manuel Pégourié-Gonnardb8eec192018-08-20 09:34:02 +02003343#if defined(MBEDTLS_SSL_PROTO_DTLS)
Gilles Peskine449bd832023-01-11 14:50:10 +01003344size_t mbedtls_ssl_get_current_mtu(const mbedtls_ssl_context *ssl)
Manuel Pégourié-Gonnardb8eec192018-08-20 09:34:02 +02003345{
Andrzej Kurekef43ce62018-10-09 08:24:12 -04003346 /* Return unlimited mtu for client hello messages to avoid fragmentation. */
Gilles Peskine449bd832023-01-11 14:50:10 +01003347 if (ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT &&
3348 (ssl->state == MBEDTLS_SSL_CLIENT_HELLO ||
3349 ssl->state == MBEDTLS_SSL_SERVER_HELLO)) {
3350 return 0;
3351 }
Andrzej Kurekef43ce62018-10-09 08:24:12 -04003352
Gilles Peskine449bd832023-01-11 14:50:10 +01003353 if (ssl->handshake == NULL || ssl->handshake->mtu == 0) {
3354 return ssl->mtu;
3355 }
Manuel Pégourié-Gonnardb8eec192018-08-20 09:34:02 +02003356
Gilles Peskine449bd832023-01-11 14:50:10 +01003357 if (ssl->mtu == 0) {
3358 return ssl->handshake->mtu;
3359 }
Manuel Pégourié-Gonnardb8eec192018-08-20 09:34:02 +02003360
Gilles Peskine449bd832023-01-11 14:50:10 +01003361 return ssl->mtu < ssl->handshake->mtu ?
3362 ssl->mtu : ssl->handshake->mtu;
Manuel Pégourié-Gonnardb8eec192018-08-20 09:34:02 +02003363}
3364#endif /* MBEDTLS_SSL_PROTO_DTLS */
3365
Gilles Peskine449bd832023-01-11 14:50:10 +01003366int mbedtls_ssl_get_max_out_record_payload(const mbedtls_ssl_context *ssl)
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02003367{
3368 size_t max_len = MBEDTLS_SSL_OUT_CONTENT_LEN;
3369
Manuel Pégourié-Gonnard000281e2018-08-21 11:20:58 +02003370#if !defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH) && \
3371 !defined(MBEDTLS_SSL_PROTO_DTLS)
3372 (void) ssl;
3373#endif
3374
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02003375#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
Gilles Peskine449bd832023-01-11 14:50:10 +01003376 const size_t mfl = mbedtls_ssl_get_output_max_frag_len(ssl);
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02003377
Gilles Peskine449bd832023-01-11 14:50:10 +01003378 if (max_len > mfl) {
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02003379 max_len = mfl;
Gilles Peskine449bd832023-01-11 14:50:10 +01003380 }
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02003381#endif
3382
3383#if defined(MBEDTLS_SSL_PROTO_DTLS)
Gilles Peskine449bd832023-01-11 14:50:10 +01003384 if (mbedtls_ssl_get_current_mtu(ssl) != 0) {
3385 const size_t mtu = mbedtls_ssl_get_current_mtu(ssl);
3386 const int ret = mbedtls_ssl_get_record_expansion(ssl);
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02003387 const size_t overhead = (size_t) ret;
3388
Gilles Peskine449bd832023-01-11 14:50:10 +01003389 if (ret < 0) {
3390 return ret;
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02003391 }
3392
Gilles Peskine449bd832023-01-11 14:50:10 +01003393 if (mtu <= overhead) {
3394 MBEDTLS_SSL_DEBUG_MSG(1, ("MTU too low for record expansion"));
3395 return MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
3396 }
3397
3398 if (max_len > mtu - overhead) {
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02003399 max_len = mtu - overhead;
Gilles Peskine449bd832023-01-11 14:50:10 +01003400 }
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02003401 }
Manuel Pégourié-Gonnardb8eec192018-08-20 09:34:02 +02003402#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02003403
Hanno Becker0defedb2018-08-10 12:35:02 +01003404#if !defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH) && \
3405 !defined(MBEDTLS_SSL_PROTO_DTLS)
3406 ((void) ssl);
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02003407#endif
3408
Gilles Peskine449bd832023-01-11 14:50:10 +01003409 return (int) max_len;
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02003410}
3411
Gilles Peskine449bd832023-01-11 14:50:10 +01003412int mbedtls_ssl_get_max_in_record_payload(const mbedtls_ssl_context *ssl)
Hanno Becker2d8e99b2021-04-21 06:19:50 +01003413{
3414 size_t max_len = MBEDTLS_SSL_IN_CONTENT_LEN;
3415
3416#if !defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
3417 (void) ssl;
3418#endif
3419
3420#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
Gilles Peskine449bd832023-01-11 14:50:10 +01003421 const size_t mfl = mbedtls_ssl_get_input_max_frag_len(ssl);
Hanno Becker2d8e99b2021-04-21 06:19:50 +01003422
Gilles Peskine449bd832023-01-11 14:50:10 +01003423 if (max_len > mfl) {
Hanno Becker2d8e99b2021-04-21 06:19:50 +01003424 max_len = mfl;
Gilles Peskine449bd832023-01-11 14:50:10 +01003425 }
Hanno Becker2d8e99b2021-04-21 06:19:50 +01003426#endif
3427
Gilles Peskine449bd832023-01-11 14:50:10 +01003428 return (int) max_len;
Hanno Becker2d8e99b2021-04-21 06:19:50 +01003429}
3430
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003431#if defined(MBEDTLS_X509_CRT_PARSE_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01003432const mbedtls_x509_crt *mbedtls_ssl_get_peer_cert(const mbedtls_ssl_context *ssl)
Paul Bakkerb0550d92012-10-30 07:51:03 +00003433{
Gilles Peskine449bd832023-01-11 14:50:10 +01003434 if (ssl == NULL || ssl->session == NULL) {
3435 return NULL;
3436 }
Paul Bakkerb0550d92012-10-30 07:51:03 +00003437
Hanno Beckere6824572019-02-07 13:18:46 +00003438#if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
Gilles Peskine449bd832023-01-11 14:50:10 +01003439 return ssl->session->peer_cert;
Hanno Beckere6824572019-02-07 13:18:46 +00003440#else
Gilles Peskine449bd832023-01-11 14:50:10 +01003441 return NULL;
Hanno Beckere6824572019-02-07 13:18:46 +00003442#endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
Paul Bakkerb0550d92012-10-30 07:51:03 +00003443}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003444#endif /* MBEDTLS_X509_CRT_PARSE_C */
Paul Bakkerb0550d92012-10-30 07:51:03 +00003445
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003446#if defined(MBEDTLS_SSL_CLI_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01003447int mbedtls_ssl_get_session(const mbedtls_ssl_context *ssl,
3448 mbedtls_ssl_session *dst)
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02003449{
Hanno Beckere810bbc2021-05-14 16:01:05 +01003450 int ret;
3451
Gilles Peskine449bd832023-01-11 14:50:10 +01003452 if (ssl == NULL ||
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02003453 dst == NULL ||
3454 ssl->session == NULL ||
Gilles Peskine449bd832023-01-11 14:50:10 +01003455 ssl->conf->endpoint != MBEDTLS_SSL_IS_CLIENT) {
3456 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02003457 }
3458
Hanno Beckere810bbc2021-05-14 16:01:05 +01003459 /* Since Mbed TLS 3.0, mbedtls_ssl_get_session() is no longer
3460 * idempotent: Each session can only be exported once.
3461 *
3462 * (This is in preparation for TLS 1.3 support where we will
3463 * need the ability to export multiple sessions (aka tickets),
3464 * which will be achieved by calling mbedtls_ssl_get_session()
3465 * multiple times until it fails.)
3466 *
3467 * Check whether we have already exported the current session,
3468 * and fail if so.
3469 */
Gilles Peskine449bd832023-01-11 14:50:10 +01003470 if (ssl->session->exported == 1) {
3471 return MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
3472 }
Hanno Beckere810bbc2021-05-14 16:01:05 +01003473
Gilles Peskine449bd832023-01-11 14:50:10 +01003474 ret = mbedtls_ssl_session_copy(dst, ssl->session);
3475 if (ret != 0) {
3476 return ret;
3477 }
Hanno Beckere810bbc2021-05-14 16:01:05 +01003478
3479 /* Remember that we've exported the session. */
3480 ssl->session->exported = 1;
Gilles Peskine449bd832023-01-11 14:50:10 +01003481 return 0;
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02003482}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003483#endif /* MBEDTLS_SSL_CLI_C */
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02003484
Paul Bakker5121ce52009-01-03 21:22:43 +00003485/*
Hanno Beckera835da52019-05-16 12:39:07 +01003486 * Define ticket header determining Mbed TLS version
3487 * and structure of the ticket.
3488 */
3489
Hanno Becker94ef3b32019-05-16 12:50:45 +01003490/*
Hanno Becker50b59662019-05-28 14:30:45 +01003491 * Define bitflag determining compile-time settings influencing
3492 * structure of serialized SSL sessions.
Hanno Becker94ef3b32019-05-16 12:50:45 +01003493 */
3494
Hanno Becker50b59662019-05-28 14:30:45 +01003495#if defined(MBEDTLS_HAVE_TIME)
Hanno Becker3e088662019-05-29 11:10:18 +01003496#define SSL_SERIALIZED_SESSION_CONFIG_TIME 1
Hanno Becker50b59662019-05-28 14:30:45 +01003497#else
Hanno Becker3e088662019-05-29 11:10:18 +01003498#define SSL_SERIALIZED_SESSION_CONFIG_TIME 0
Hanno Becker94ef3b32019-05-16 12:50:45 +01003499#endif /* MBEDTLS_HAVE_TIME */
3500
3501#if defined(MBEDTLS_X509_CRT_PARSE_C)
Hanno Becker3e088662019-05-29 11:10:18 +01003502#define SSL_SERIALIZED_SESSION_CONFIG_CRT 1
Hanno Becker94ef3b32019-05-16 12:50:45 +01003503#else
Hanno Becker3e088662019-05-29 11:10:18 +01003504#define SSL_SERIALIZED_SESSION_CONFIG_CRT 0
Hanno Becker94ef3b32019-05-16 12:50:45 +01003505#endif /* MBEDTLS_X509_CRT_PARSE_C */
3506
3507#if defined(MBEDTLS_SSL_CLI_C) && defined(MBEDTLS_SSL_SESSION_TICKETS)
Hanno Becker3e088662019-05-29 11:10:18 +01003508#define SSL_SERIALIZED_SESSION_CONFIG_CLIENT_TICKET 1
Hanno Becker94ef3b32019-05-16 12:50:45 +01003509#else
Hanno Becker3e088662019-05-29 11:10:18 +01003510#define SSL_SERIALIZED_SESSION_CONFIG_CLIENT_TICKET 0
Hanno Becker94ef3b32019-05-16 12:50:45 +01003511#endif /* MBEDTLS_SSL_CLI_C && MBEDTLS_SSL_SESSION_TICKETS */
3512
3513#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
Hanno Becker3e088662019-05-29 11:10:18 +01003514#define SSL_SERIALIZED_SESSION_CONFIG_MFL 1
Hanno Becker94ef3b32019-05-16 12:50:45 +01003515#else
Hanno Becker3e088662019-05-29 11:10:18 +01003516#define SSL_SERIALIZED_SESSION_CONFIG_MFL 0
Hanno Becker94ef3b32019-05-16 12:50:45 +01003517#endif /* MBEDTLS_SSL_MAX_FRAGMENT_LENGTH */
3518
Hanno Becker94ef3b32019-05-16 12:50:45 +01003519#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
Hanno Becker3e088662019-05-29 11:10:18 +01003520#define SSL_SERIALIZED_SESSION_CONFIG_ETM 1
Hanno Becker94ef3b32019-05-16 12:50:45 +01003521#else
Hanno Becker3e088662019-05-29 11:10:18 +01003522#define SSL_SERIALIZED_SESSION_CONFIG_ETM 0
Hanno Becker94ef3b32019-05-16 12:50:45 +01003523#endif /* MBEDTLS_SSL_ENCRYPT_THEN_MAC */
3524
Hanno Becker94ef3b32019-05-16 12:50:45 +01003525#if defined(MBEDTLS_SSL_SESSION_TICKETS)
3526#define SSL_SERIALIZED_SESSION_CONFIG_TICKET 1
3527#else
3528#define SSL_SERIALIZED_SESSION_CONFIG_TICKET 0
3529#endif /* MBEDTLS_SSL_SESSION_TICKETS */
3530
Hanno Becker3e088662019-05-29 11:10:18 +01003531#define SSL_SERIALIZED_SESSION_CONFIG_TIME_BIT 0
3532#define SSL_SERIALIZED_SESSION_CONFIG_CRT_BIT 1
3533#define SSL_SERIALIZED_SESSION_CONFIG_CLIENT_TICKET_BIT 2
3534#define SSL_SERIALIZED_SESSION_CONFIG_MFL_BIT 3
Hanno Becker37bdbe62021-08-01 05:38:58 +01003535#define SSL_SERIALIZED_SESSION_CONFIG_ETM_BIT 4
3536#define SSL_SERIALIZED_SESSION_CONFIG_TICKET_BIT 5
Hanno Becker3e088662019-05-29 11:10:18 +01003537
Hanno Becker50b59662019-05-28 14:30:45 +01003538#define SSL_SERIALIZED_SESSION_CONFIG_BITFLAG \
Gilles Peskine449bd832023-01-11 14:50:10 +01003539 ((uint16_t) ( \
3540 (SSL_SERIALIZED_SESSION_CONFIG_TIME << SSL_SERIALIZED_SESSION_CONFIG_TIME_BIT) | \
3541 (SSL_SERIALIZED_SESSION_CONFIG_CRT << SSL_SERIALIZED_SESSION_CONFIG_CRT_BIT) | \
3542 (SSL_SERIALIZED_SESSION_CONFIG_CLIENT_TICKET << \
3543 SSL_SERIALIZED_SESSION_CONFIG_CLIENT_TICKET_BIT) | \
3544 (SSL_SERIALIZED_SESSION_CONFIG_MFL << SSL_SERIALIZED_SESSION_CONFIG_MFL_BIT) | \
3545 (SSL_SERIALIZED_SESSION_CONFIG_ETM << SSL_SERIALIZED_SESSION_CONFIG_ETM_BIT) | \
3546 (SSL_SERIALIZED_SESSION_CONFIG_TICKET << SSL_SERIALIZED_SESSION_CONFIG_TICKET_BIT)))
Hanno Becker94ef3b32019-05-16 12:50:45 +01003547
Hanno Beckerf8787072019-05-16 12:41:07 +01003548static unsigned char ssl_serialized_session_header[] = {
Hanno Becker94ef3b32019-05-16 12:50:45 +01003549 MBEDTLS_VERSION_MAJOR,
3550 MBEDTLS_VERSION_MINOR,
3551 MBEDTLS_VERSION_PATCH,
Gilles Peskine449bd832023-01-11 14:50:10 +01003552 MBEDTLS_BYTE_1(SSL_SERIALIZED_SESSION_CONFIG_BITFLAG),
3553 MBEDTLS_BYTE_0(SSL_SERIALIZED_SESSION_CONFIG_BITFLAG),
Hanno Beckerf8787072019-05-16 12:41:07 +01003554};
Hanno Beckera835da52019-05-16 12:39:07 +01003555
3556/*
Manuel Pégourié-Gonnarda3e7c652019-05-16 10:08:35 +02003557 * Serialize a session in the following format:
Manuel Pégourié-Gonnard35eb8022019-05-16 11:11:08 +02003558 * (in the presentation language of TLS, RFC 8446 section 3)
Manuel Pégourié-Gonnarda3e7c652019-05-16 10:08:35 +02003559 *
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01003560 * struct {
Hanno Beckerdc28b6c2019-05-29 11:08:00 +01003561 *
Hanno Beckerdce50972021-08-01 05:39:23 +01003562 * opaque mbedtls_version[3]; // library version: major, minor, patch
3563 * opaque session_format[2]; // library-version specific 16-bit field
3564 * // determining the format of the remaining
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01003565 * // serialized data.
Hanno Beckerdc28b6c2019-05-29 11:08:00 +01003566 *
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01003567 * Note: When updating the format, remember to keep
3568 * these version+format bytes.
Manuel Pégourié-Gonnarda3e7c652019-05-16 10:08:35 +02003569 *
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01003570 * // In this version, `session_format` determines
3571 * // the setting of those compile-time
3572 * // configuration options which influence
3573 * // the structure of mbedtls_ssl_session.
3574 *
Glenn Straussda7851c2022-03-14 13:29:48 -04003575 * uint8_t minor_ver; // Protocol minor version. Possible values:
Jerry Yu0c2a7382022-12-06 13:27:25 +08003576 * // - TLS 1.2 (0x0303)
3577 * // - TLS 1.3 (0x0304)
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01003578 *
Glenn Straussda7851c2022-03-14 13:29:48 -04003579 * select (serialized_session.tls_version) {
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01003580 *
Glenn Straussda7851c2022-03-14 13:29:48 -04003581 * case MBEDTLS_SSL_VERSION_TLS1_2:
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01003582 * serialized_session_tls12 data;
Jerry Yu0c2a7382022-12-06 13:27:25 +08003583 * case MBEDTLS_SSL_VERSION_TLS1_3:
Jerry Yuddda0502022-12-01 19:43:12 +08003584 * serialized_session_tls13 data;
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01003585 *
3586 * };
3587 *
3588 * } serialized_session;
3589 *
Manuel Pégourié-Gonnarda3e7c652019-05-16 10:08:35 +02003590 */
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01003591
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02003592MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01003593static int ssl_session_save(const mbedtls_ssl_session *session,
3594 unsigned char omit_header,
3595 unsigned char *buf,
3596 size_t buf_len,
3597 size_t *olen)
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01003598{
3599 unsigned char *p = buf;
3600 size_t used = 0;
Jerry Yu251a12e2022-07-13 15:15:48 +08003601 size_t remaining_len;
Jerry Yue36fdd62022-08-17 21:31:36 +08003602#if defined(MBEDTLS_SSL_PROTO_TLS1_3)
3603 size_t out_len;
3604 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
3605#endif
Gilles Peskine449bd832023-01-11 14:50:10 +01003606 if (session == NULL) {
3607 return MBEDTLS_ERR_SSL_INTERNAL_ERROR;
3608 }
Jerry Yu438ddd82022-07-07 06:55:50 +00003609
Gilles Peskine449bd832023-01-11 14:50:10 +01003610 if (!omit_header) {
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01003611 /*
3612 * Add Mbed TLS version identifier
3613 */
Gilles Peskine449bd832023-01-11 14:50:10 +01003614 used += sizeof(ssl_serialized_session_header);
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01003615
Gilles Peskine449bd832023-01-11 14:50:10 +01003616 if (used <= buf_len) {
3617 memcpy(p, ssl_serialized_session_header,
3618 sizeof(ssl_serialized_session_header));
3619 p += sizeof(ssl_serialized_session_header);
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01003620 }
3621 }
3622
3623 /*
3624 * TLS version identifier
3625 */
3626 used += 1;
Gilles Peskine449bd832023-01-11 14:50:10 +01003627 if (used <= buf_len) {
3628 *p++ = MBEDTLS_BYTE_0(session->tls_version);
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01003629 }
3630
3631 /* Forward to version-specific serialization routine. */
Jerry Yufca4d572022-07-21 10:37:48 +08003632 remaining_len = (buf_len >= used) ? buf_len - used : 0;
Gilles Peskine449bd832023-01-11 14:50:10 +01003633 switch (session->tls_version) {
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01003634#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
Gilles Peskine449bd832023-01-11 14:50:10 +01003635 case MBEDTLS_SSL_VERSION_TLS1_2:
3636 used += ssl_tls12_session_save(session, p, remaining_len);
3637 break;
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01003638#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
3639
Jerry Yu251a12e2022-07-13 15:15:48 +08003640#if defined(MBEDTLS_SSL_PROTO_TLS1_3)
Gilles Peskine449bd832023-01-11 14:50:10 +01003641 case MBEDTLS_SSL_VERSION_TLS1_3:
3642 ret = ssl_tls13_session_save(session, p, remaining_len, &out_len);
3643 if (ret != 0 && ret != MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL) {
3644 return ret;
3645 }
3646 used += out_len;
3647 break;
Jerry Yu251a12e2022-07-13 15:15:48 +08003648#endif /* MBEDTLS_SSL_PROTO_TLS1_3 */
3649
Gilles Peskine449bd832023-01-11 14:50:10 +01003650 default:
3651 return MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01003652 }
3653
3654 *olen = used;
Gilles Peskine449bd832023-01-11 14:50:10 +01003655 if (used > buf_len) {
3656 return MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL;
3657 }
Manuel Pégourié-Gonnarda3e7c652019-05-16 10:08:35 +02003658
Gilles Peskine449bd832023-01-11 14:50:10 +01003659 return 0;
Manuel Pégourié-Gonnarda3e7c652019-05-16 10:08:35 +02003660}
3661
3662/*
Manuel Pégourié-Gonnard45ac1f02019-07-23 16:52:45 +02003663 * Public wrapper for ssl_session_save()
3664 */
Gilles Peskine449bd832023-01-11 14:50:10 +01003665int mbedtls_ssl_session_save(const mbedtls_ssl_session *session,
3666 unsigned char *buf,
3667 size_t buf_len,
3668 size_t *olen)
Manuel Pégourié-Gonnard45ac1f02019-07-23 16:52:45 +02003669{
Gilles Peskine449bd832023-01-11 14:50:10 +01003670 return ssl_session_save(session, 0, buf, buf_len, olen);
Manuel Pégourié-Gonnard45ac1f02019-07-23 16:52:45 +02003671}
Manuel Pégourié-Gonnarda3e7c652019-05-16 10:08:35 +02003672
Jerry Yuf1b23ca2022-02-18 11:48:47 +08003673/*
3674 * Deserialize session, see mbedtls_ssl_session_save() for format.
3675 *
3676 * This internal version is wrapped by a public function that cleans up in
3677 * case of error, and has an extra option omit_header.
3678 */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02003679MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01003680static int ssl_session_load(mbedtls_ssl_session *session,
3681 unsigned char omit_header,
3682 const unsigned char *buf,
3683 size_t len)
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01003684{
3685 const unsigned char *p = buf;
3686 const unsigned char * const end = buf + len;
Jerry Yu438ddd82022-07-07 06:55:50 +00003687 size_t remaining_len;
3688
3689
Gilles Peskine449bd832023-01-11 14:50:10 +01003690 if (session == NULL) {
3691 return MBEDTLS_ERR_SSL_INTERNAL_ERROR;
3692 }
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01003693
Gilles Peskine449bd832023-01-11 14:50:10 +01003694 if (!omit_header) {
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01003695 /*
3696 * Check Mbed TLS version identifier
3697 */
3698
Gilles Peskine449bd832023-01-11 14:50:10 +01003699 if ((size_t) (end - p) < sizeof(ssl_serialized_session_header)) {
3700 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01003701 }
Gilles Peskine449bd832023-01-11 14:50:10 +01003702
3703 if (memcmp(p, ssl_serialized_session_header,
3704 sizeof(ssl_serialized_session_header)) != 0) {
3705 return MBEDTLS_ERR_SSL_VERSION_MISMATCH;
3706 }
3707 p += sizeof(ssl_serialized_session_header);
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01003708 }
3709
3710 /*
3711 * TLS version identifier
3712 */
Gilles Peskine449bd832023-01-11 14:50:10 +01003713 if (1 > (size_t) (end - p)) {
3714 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
3715 }
Glenn Straussda7851c2022-03-14 13:29:48 -04003716 session->tls_version = 0x0300 | *p++;
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01003717
3718 /* Dispatch according to TLS version. */
Gilles Peskine449bd832023-01-11 14:50:10 +01003719 remaining_len = (end - p);
3720 switch (session->tls_version) {
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01003721#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
Gilles Peskine449bd832023-01-11 14:50:10 +01003722 case MBEDTLS_SSL_VERSION_TLS1_2:
3723 return ssl_tls12_session_load(session, p, remaining_len);
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01003724#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
3725
Jerry Yu438ddd82022-07-07 06:55:50 +00003726#if defined(MBEDTLS_SSL_PROTO_TLS1_3)
Gilles Peskine449bd832023-01-11 14:50:10 +01003727 case MBEDTLS_SSL_VERSION_TLS1_3:
3728 return ssl_tls13_session_load(session, p, remaining_len);
Jerry Yu438ddd82022-07-07 06:55:50 +00003729#endif /* MBEDTLS_SSL_PROTO_TLS1_3 */
3730
Gilles Peskine449bd832023-01-11 14:50:10 +01003731 default:
3732 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01003733 }
3734}
3735
Manuel Pégourié-Gonnarda3e7c652019-05-16 10:08:35 +02003736/*
Manuel Pégourié-Gonnardb9dfc9f2019-07-12 10:50:19 +02003737 * Deserialize session: public wrapper for error cleaning
Manuel Pégourié-Gonnarda3d831b2019-05-23 12:28:45 +02003738 */
Gilles Peskine449bd832023-01-11 14:50:10 +01003739int mbedtls_ssl_session_load(mbedtls_ssl_session *session,
3740 const unsigned char *buf,
3741 size_t len)
Manuel Pégourié-Gonnarda3d831b2019-05-23 12:28:45 +02003742{
Gilles Peskine449bd832023-01-11 14:50:10 +01003743 int ret = ssl_session_load(session, 0, buf, len);
Manuel Pégourié-Gonnarda3d831b2019-05-23 12:28:45 +02003744
Gilles Peskine449bd832023-01-11 14:50:10 +01003745 if (ret != 0) {
3746 mbedtls_ssl_session_free(session);
3747 }
Manuel Pégourié-Gonnarda3d831b2019-05-23 12:28:45 +02003748
Gilles Peskine449bd832023-01-11 14:50:10 +01003749 return ret;
Manuel Pégourié-Gonnarda3d831b2019-05-23 12:28:45 +02003750}
3751
3752/*
Paul Bakker1961b702013-01-25 14:49:24 +01003753 * Perform a single step of the SSL handshake
Paul Bakker5121ce52009-01-03 21:22:43 +00003754 */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02003755MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01003756static int ssl_prepare_handshake_step(mbedtls_ssl_context *ssl)
Hanno Becker41934dd2021-08-07 19:13:43 +01003757{
3758 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
3759
Ronald Cron66dbf912022-02-02 15:33:46 +01003760 /*
3761 * We may have not been able to send to the peer all the handshake data
Ronald Cron3f20b772022-03-08 16:00:02 +01003762 * that were written into the output buffer by the previous handshake step,
3763 * if the write to the network callback returned with the
Ronald Cron66dbf912022-02-02 15:33:46 +01003764 * #MBEDTLS_ERR_SSL_WANT_WRITE error code.
3765 * We proceed to the next handshake step only when all data from the
3766 * previous one have been sent to the peer, thus we make sure that this is
3767 * the case here by calling `mbedtls_ssl_flush_output()`. The function may
3768 * return with the #MBEDTLS_ERR_SSL_WANT_WRITE error code in which case
3769 * we have to wait before to go ahead.
3770 * In the case of TLS 1.3, handshake step handlers do not send data to the
3771 * peer. Data are only sent here and through
3772 * `mbedtls_ssl_handle_pending_alert` in case an error that triggered an
Andrzej Kurek5c65c572022-04-13 14:28:52 -04003773 * alert occurred.
Ronald Cron66dbf912022-02-02 15:33:46 +01003774 */
Gilles Peskine449bd832023-01-11 14:50:10 +01003775 if ((ret = mbedtls_ssl_flush_output(ssl)) != 0) {
3776 return ret;
3777 }
Hanno Becker41934dd2021-08-07 19:13:43 +01003778
3779#if defined(MBEDTLS_SSL_PROTO_DTLS)
Gilles Peskine449bd832023-01-11 14:50:10 +01003780 if (ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
3781 ssl->handshake->retransmit_state == MBEDTLS_SSL_RETRANS_SENDING) {
3782 if ((ret = mbedtls_ssl_flight_transmit(ssl)) != 0) {
3783 return ret;
3784 }
Hanno Becker41934dd2021-08-07 19:13:43 +01003785 }
3786#endif /* MBEDTLS_SSL_PROTO_DTLS */
3787
Gilles Peskine449bd832023-01-11 14:50:10 +01003788 return ret;
Hanno Becker41934dd2021-08-07 19:13:43 +01003789}
3790
Gilles Peskine449bd832023-01-11 14:50:10 +01003791int mbedtls_ssl_handshake_step(mbedtls_ssl_context *ssl)
Paul Bakker5121ce52009-01-03 21:22:43 +00003792{
Hanno Becker41934dd2021-08-07 19:13:43 +01003793 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker5121ce52009-01-03 21:22:43 +00003794
Gilles Peskine449bd832023-01-11 14:50:10 +01003795 if (ssl == NULL ||
Hanno Becker41934dd2021-08-07 19:13:43 +01003796 ssl->conf == NULL ||
3797 ssl->handshake == NULL ||
Gilles Peskine449bd832023-01-11 14:50:10 +01003798 ssl->state == MBEDTLS_SSL_HANDSHAKE_OVER) {
3799 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
Hanno Becker41934dd2021-08-07 19:13:43 +01003800 }
3801
Gilles Peskine449bd832023-01-11 14:50:10 +01003802 ret = ssl_prepare_handshake_step(ssl);
3803 if (ret != 0) {
3804 return ret;
3805 }
Manuel Pégourié-Gonnardf81ee2e2015-09-01 17:43:40 +02003806
Gilles Peskine449bd832023-01-11 14:50:10 +01003807 ret = mbedtls_ssl_handle_pending_alert(ssl);
3808 if (ret != 0) {
Jerry Yue7047812021-09-13 19:26:39 +08003809 goto cleanup;
Gilles Peskine449bd832023-01-11 14:50:10 +01003810 }
Jerry Yue7047812021-09-13 19:26:39 +08003811
Tom Cosgrove2fdc7b32022-09-21 12:33:17 +01003812 /* If ssl->conf->endpoint is not one of MBEDTLS_SSL_IS_CLIENT or
3813 * MBEDTLS_SSL_IS_SERVER, this is the return code we give */
3814 ret = MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
3815
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003816#if defined(MBEDTLS_SSL_CLI_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01003817 if (ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT) {
3818 MBEDTLS_SSL_DEBUG_MSG(2, ("client state: %s",
3819 mbedtls_ssl_states_str(ssl->state)));
Jerry Yub9930e72021-08-06 17:11:51 +08003820
Gilles Peskine449bd832023-01-11 14:50:10 +01003821 switch (ssl->state) {
Ronald Cron9f0fba32022-02-10 16:45:15 +01003822 case MBEDTLS_SSL_HELLO_REQUEST:
3823 ssl->state = MBEDTLS_SSL_CLIENT_HELLO;
Tom Cosgrove87d9c6c2022-09-22 09:27:56 +01003824 ret = 0;
Ronald Cron9f0fba32022-02-10 16:45:15 +01003825 break;
Jerry Yub9930e72021-08-06 17:11:51 +08003826
Ronald Cron9f0fba32022-02-10 16:45:15 +01003827 case MBEDTLS_SSL_CLIENT_HELLO:
Gilles Peskine449bd832023-01-11 14:50:10 +01003828 ret = mbedtls_ssl_write_client_hello(ssl);
Ronald Cron9f0fba32022-02-10 16:45:15 +01003829 break;
3830
3831 default:
3832#if defined(MBEDTLS_SSL_PROTO_TLS1_2) && defined(MBEDTLS_SSL_PROTO_TLS1_3)
Gilles Peskine449bd832023-01-11 14:50:10 +01003833 if (ssl->tls_version == MBEDTLS_SSL_VERSION_TLS1_3) {
3834 ret = mbedtls_ssl_tls13_handshake_client_step(ssl);
3835 } else {
3836 ret = mbedtls_ssl_handshake_client_step(ssl);
3837 }
Ronald Cron9f0fba32022-02-10 16:45:15 +01003838#elif defined(MBEDTLS_SSL_PROTO_TLS1_2)
Gilles Peskine449bd832023-01-11 14:50:10 +01003839 ret = mbedtls_ssl_handshake_client_step(ssl);
Ronald Cron9f0fba32022-02-10 16:45:15 +01003840#else
Gilles Peskine449bd832023-01-11 14:50:10 +01003841 ret = mbedtls_ssl_tls13_handshake_client_step(ssl);
Ronald Cron9f0fba32022-02-10 16:45:15 +01003842#endif
3843 }
Jerry Yub9930e72021-08-06 17:11:51 +08003844 }
Paul Bakker5121ce52009-01-03 21:22:43 +00003845#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003846#if defined(MBEDTLS_SSL_SRV_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01003847 if (ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER) {
Ronald Cron6f135e12021-12-08 16:57:54 +01003848#if defined(MBEDTLS_SSL_PROTO_TLS1_3)
Gilles Peskine449bd832023-01-11 14:50:10 +01003849 if (mbedtls_ssl_conf_is_tls13_only(ssl->conf)) {
3850 ret = mbedtls_ssl_tls13_handshake_server_step(ssl);
3851 }
Ronald Cron6f135e12021-12-08 16:57:54 +01003852#endif /* MBEDTLS_SSL_PROTO_TLS1_3 */
Jerry Yub9930e72021-08-06 17:11:51 +08003853
3854#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
Gilles Peskine449bd832023-01-11 14:50:10 +01003855 if (mbedtls_ssl_conf_is_tls12_only(ssl->conf)) {
3856 ret = mbedtls_ssl_handshake_server_step(ssl);
3857 }
Jerry Yub9930e72021-08-06 17:11:51 +08003858#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
3859 }
Paul Bakker5121ce52009-01-03 21:22:43 +00003860#endif
3861
Gilles Peskine449bd832023-01-11 14:50:10 +01003862 if (ret != 0) {
Jerry Yubbd5a3f2021-09-18 20:50:22 +08003863 /* handshake_step return error. And it is same
3864 * with alert_reason.
3865 */
Gilles Peskine449bd832023-01-11 14:50:10 +01003866 if (ssl->send_alert) {
3867 ret = mbedtls_ssl_handle_pending_alert(ssl);
Jerry Yue7047812021-09-13 19:26:39 +08003868 goto cleanup;
3869 }
3870 }
3871
3872cleanup:
Gilles Peskine449bd832023-01-11 14:50:10 +01003873 return ret;
Paul Bakker1961b702013-01-25 14:49:24 +01003874}
3875
3876/*
3877 * Perform the SSL handshake
3878 */
Gilles Peskine449bd832023-01-11 14:50:10 +01003879int mbedtls_ssl_handshake(mbedtls_ssl_context *ssl)
Paul Bakker1961b702013-01-25 14:49:24 +01003880{
3881 int ret = 0;
3882
Hanno Beckera817ea42020-10-20 15:20:23 +01003883 /* Sanity checks */
3884
Gilles Peskine449bd832023-01-11 14:50:10 +01003885 if (ssl == NULL || ssl->conf == NULL) {
3886 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
3887 }
Manuel Pégourié-Gonnardf81ee2e2015-09-01 17:43:40 +02003888
Hanno Beckera817ea42020-10-20 15:20:23 +01003889#if defined(MBEDTLS_SSL_PROTO_DTLS)
Gilles Peskine449bd832023-01-11 14:50:10 +01003890 if (ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
3891 (ssl->f_set_timer == NULL || ssl->f_get_timer == NULL)) {
3892 MBEDTLS_SSL_DEBUG_MSG(1, ("You must use "
3893 "mbedtls_ssl_set_timer_cb() for DTLS"));
3894 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
Hanno Beckera817ea42020-10-20 15:20:23 +01003895 }
3896#endif /* MBEDTLS_SSL_PROTO_DTLS */
3897
Gilles Peskine449bd832023-01-11 14:50:10 +01003898 MBEDTLS_SSL_DEBUG_MSG(2, ("=> handshake"));
Paul Bakker1961b702013-01-25 14:49:24 +01003899
Hanno Beckera817ea42020-10-20 15:20:23 +01003900 /* Main handshake loop */
Gilles Peskine449bd832023-01-11 14:50:10 +01003901 while (ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER) {
3902 ret = mbedtls_ssl_handshake_step(ssl);
Paul Bakker1961b702013-01-25 14:49:24 +01003903
Gilles Peskine449bd832023-01-11 14:50:10 +01003904 if (ret != 0) {
Paul Bakker1961b702013-01-25 14:49:24 +01003905 break;
Gilles Peskine449bd832023-01-11 14:50:10 +01003906 }
Paul Bakker1961b702013-01-25 14:49:24 +01003907 }
3908
Gilles Peskine449bd832023-01-11 14:50:10 +01003909 MBEDTLS_SSL_DEBUG_MSG(2, ("<= handshake"));
Paul Bakker5121ce52009-01-03 21:22:43 +00003910
Gilles Peskine449bd832023-01-11 14:50:10 +01003911 return ret;
Paul Bakker5121ce52009-01-03 21:22:43 +00003912}
3913
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003914#if defined(MBEDTLS_SSL_RENEGOTIATION)
3915#if defined(MBEDTLS_SSL_SRV_C)
Paul Bakker5121ce52009-01-03 21:22:43 +00003916/*
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01003917 * Write HelloRequest to request renegotiation on server
Paul Bakker48916f92012-09-16 19:57:18 +00003918 */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02003919MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01003920static int ssl_write_hello_request(mbedtls_ssl_context *ssl)
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01003921{
Janos Follath865b3eb2019-12-16 11:46:15 +00003922 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01003923
Gilles Peskine449bd832023-01-11 14:50:10 +01003924 MBEDTLS_SSL_DEBUG_MSG(2, ("=> write hello request"));
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01003925
3926 ssl->out_msglen = 4;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003927 ssl->out_msgtype = MBEDTLS_SSL_MSG_HANDSHAKE;
3928 ssl->out_msg[0] = MBEDTLS_SSL_HS_HELLO_REQUEST;
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01003929
Gilles Peskine449bd832023-01-11 14:50:10 +01003930 if ((ret = mbedtls_ssl_write_handshake_msg(ssl)) != 0) {
3931 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ssl_write_handshake_msg", ret);
3932 return ret;
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01003933 }
3934
Gilles Peskine449bd832023-01-11 14:50:10 +01003935 MBEDTLS_SSL_DEBUG_MSG(2, ("<= write hello request"));
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01003936
Gilles Peskine449bd832023-01-11 14:50:10 +01003937 return 0;
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01003938}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003939#endif /* MBEDTLS_SSL_SRV_C */
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01003940
3941/*
3942 * Actually renegotiate current connection, triggered by either:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003943 * - any side: calling mbedtls_ssl_renegotiate(),
3944 * - client: receiving a HelloRequest during mbedtls_ssl_read(),
3945 * - server: receiving any handshake message on server during mbedtls_ssl_read() after
Manuel Pégourié-Gonnard55e4ff22014-08-19 11:16:35 +02003946 * the initial handshake is completed.
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01003947 * If the handshake doesn't complete due to waiting for I/O, it will continue
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003948 * during the next calls to mbedtls_ssl_renegotiate() or mbedtls_ssl_read() respectively.
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01003949 */
Gilles Peskine449bd832023-01-11 14:50:10 +01003950int mbedtls_ssl_start_renegotiation(mbedtls_ssl_context *ssl)
Paul Bakker48916f92012-09-16 19:57:18 +00003951{
Janos Follath865b3eb2019-12-16 11:46:15 +00003952 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker48916f92012-09-16 19:57:18 +00003953
Gilles Peskine449bd832023-01-11 14:50:10 +01003954 MBEDTLS_SSL_DEBUG_MSG(2, ("=> renegotiate"));
Paul Bakker48916f92012-09-16 19:57:18 +00003955
Gilles Peskine449bd832023-01-11 14:50:10 +01003956 if ((ret = ssl_handshake_init(ssl)) != 0) {
3957 return ret;
3958 }
Paul Bakker48916f92012-09-16 19:57:18 +00003959
Manuel Pégourié-Gonnard0557bd52014-08-19 19:18:39 +02003960 /* RFC 6347 4.2.2: "[...] the HelloRequest will have message_seq = 0 and
3961 * the ServerHello will have message_seq = 1" */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003962#if defined(MBEDTLS_SSL_PROTO_DTLS)
Gilles Peskine449bd832023-01-11 14:50:10 +01003963 if (ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
3964 ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_PENDING) {
3965 if (ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER) {
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02003966 ssl->handshake->out_msg_seq = 1;
Gilles Peskine449bd832023-01-11 14:50:10 +01003967 } else {
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02003968 ssl->handshake->in_msg_seq = 1;
Gilles Peskine449bd832023-01-11 14:50:10 +01003969 }
Manuel Pégourié-Gonnard0557bd52014-08-19 19:18:39 +02003970 }
3971#endif
3972
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003973 ssl->state = MBEDTLS_SSL_HELLO_REQUEST;
3974 ssl->renego_status = MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS;
Paul Bakker48916f92012-09-16 19:57:18 +00003975
Gilles Peskine449bd832023-01-11 14:50:10 +01003976 if ((ret = mbedtls_ssl_handshake(ssl)) != 0) {
3977 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ssl_handshake", ret);
3978 return ret;
Paul Bakker48916f92012-09-16 19:57:18 +00003979 }
3980
Gilles Peskine449bd832023-01-11 14:50:10 +01003981 MBEDTLS_SSL_DEBUG_MSG(2, ("<= renegotiate"));
Paul Bakker48916f92012-09-16 19:57:18 +00003982
Gilles Peskine449bd832023-01-11 14:50:10 +01003983 return 0;
Paul Bakker48916f92012-09-16 19:57:18 +00003984}
3985
3986/*
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01003987 * Renegotiate current connection on client,
3988 * or request renegotiation on server
3989 */
Gilles Peskine449bd832023-01-11 14:50:10 +01003990int mbedtls_ssl_renegotiate(mbedtls_ssl_context *ssl)
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01003991{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003992 int ret = MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01003993
Gilles Peskine449bd832023-01-11 14:50:10 +01003994 if (ssl == NULL || ssl->conf == NULL) {
3995 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
3996 }
Manuel Pégourié-Gonnardf81ee2e2015-09-01 17:43:40 +02003997
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003998#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01003999 /* On server, just send the request */
Gilles Peskine449bd832023-01-11 14:50:10 +01004000 if (ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER) {
4001 if (mbedtls_ssl_is_handshake_over(ssl) == 0) {
4002 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
4003 }
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01004004
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004005 ssl->renego_status = MBEDTLS_SSL_RENEGOTIATION_PENDING;
Manuel Pégourié-Gonnardf07f4212014-08-15 19:04:47 +02004006
4007 /* Did we already try/start sending HelloRequest? */
Gilles Peskine449bd832023-01-11 14:50:10 +01004008 if (ssl->out_left != 0) {
4009 return mbedtls_ssl_flush_output(ssl);
4010 }
Manuel Pégourié-Gonnardf07f4212014-08-15 19:04:47 +02004011
Gilles Peskine449bd832023-01-11 14:50:10 +01004012 return ssl_write_hello_request(ssl);
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01004013 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004014#endif /* MBEDTLS_SSL_SRV_C */
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01004015
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004016#if defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01004017 /*
4018 * On client, either start the renegotiation process or,
4019 * if already in progress, continue the handshake
4020 */
Gilles Peskine449bd832023-01-11 14:50:10 +01004021 if (ssl->renego_status != MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS) {
4022 if (mbedtls_ssl_is_handshake_over(ssl) == 0) {
4023 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01004024 }
Gilles Peskine449bd832023-01-11 14:50:10 +01004025
4026 if ((ret = mbedtls_ssl_start_renegotiation(ssl)) != 0) {
4027 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ssl_start_renegotiation", ret);
4028 return ret;
4029 }
4030 } else {
4031 if ((ret = mbedtls_ssl_handshake(ssl)) != 0) {
4032 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ssl_handshake", ret);
4033 return ret;
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01004034 }
4035 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004036#endif /* MBEDTLS_SSL_CLI_C */
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01004037
Gilles Peskine449bd832023-01-11 14:50:10 +01004038 return ret;
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01004039}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004040#endif /* MBEDTLS_SSL_RENEGOTIATION */
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01004041
Gilles Peskine449bd832023-01-11 14:50:10 +01004042void mbedtls_ssl_handshake_free(mbedtls_ssl_context *ssl)
Paul Bakker48916f92012-09-16 19:57:18 +00004043{
Gilles Peskine9b562d52018-04-25 20:32:43 +02004044 mbedtls_ssl_handshake_params *handshake = ssl->handshake;
4045
Gilles Peskine449bd832023-01-11 14:50:10 +01004046 if (handshake == NULL) {
Paul Bakkeraccaffe2014-06-26 13:37:14 +02004047 return;
Gilles Peskine449bd832023-01-11 14:50:10 +01004048 }
Paul Bakkeraccaffe2014-06-26 13:37:14 +02004049
Brett Warrene0edc842021-08-17 09:53:13 +01004050#if defined(MBEDTLS_ECP_C)
4051#if !defined(MBEDTLS_DEPRECATED_REMOVED)
Gilles Peskine449bd832023-01-11 14:50:10 +01004052 if (ssl->handshake->group_list_heap_allocated) {
4053 mbedtls_free((void *) handshake->group_list);
4054 }
Brett Warrene0edc842021-08-17 09:53:13 +01004055 handshake->group_list = NULL;
4056#endif /* MBEDTLS_DEPRECATED_REMOVED */
4057#endif /* MBEDTLS_ECP_C */
4058
Ronald Crone68ab4f2022-10-05 12:46:29 +02004059#if defined(MBEDTLS_SSL_HANDSHAKE_WITH_CERT_ENABLED)
Jerry Yuf017ee42022-01-12 15:49:48 +08004060#if !defined(MBEDTLS_DEPRECATED_REMOVED)
Gilles Peskine449bd832023-01-11 14:50:10 +01004061 if (ssl->handshake->sig_algs_heap_allocated) {
4062 mbedtls_free((void *) handshake->sig_algs);
4063 }
Jerry Yuf017ee42022-01-12 15:49:48 +08004064 handshake->sig_algs = NULL;
4065#endif /* MBEDTLS_DEPRECATED_REMOVED */
Xiaofei Baic234ecf2022-02-08 09:59:23 +00004066#if defined(MBEDTLS_SSL_PROTO_TLS1_3)
Gilles Peskine449bd832023-01-11 14:50:10 +01004067 if (ssl->handshake->certificate_request_context) {
4068 mbedtls_free((void *) handshake->certificate_request_context);
Xiaofei Baic234ecf2022-02-08 09:59:23 +00004069 }
4070#endif /* MBEDTLS_SSL_PROTO_TLS1_3 */
Ronald Crone68ab4f2022-10-05 12:46:29 +02004071#endif /* MBEDTLS_SSL_HANDSHAKE_WITH_CERT_ENABLED */
Jerry Yuf017ee42022-01-12 15:49:48 +08004072
Gilles Peskinedf13d5c2018-04-25 20:39:48 +02004073#if defined(MBEDTLS_SSL_ASYNC_PRIVATE)
Gilles Peskine449bd832023-01-11 14:50:10 +01004074 if (ssl->conf->f_async_cancel != NULL && handshake->async_in_progress != 0) {
4075 ssl->conf->f_async_cancel(ssl);
Gilles Peskinedf13d5c2018-04-25 20:39:48 +02004076 handshake->async_in_progress = 0;
4077 }
4078#endif /* MBEDTLS_SSL_ASYNC_PRIVATE */
4079
Andrzej Kurek25f27152022-08-17 16:09:31 -04004080#if defined(MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Andrzej Kurekeb342242019-01-29 09:14:33 -05004081#if defined(MBEDTLS_USE_PSA_CRYPTO)
Gilles Peskine449bd832023-01-11 14:50:10 +01004082 psa_hash_abort(&handshake->fin_sha256_psa);
Andrzej Kurekeb342242019-01-29 09:14:33 -05004083#else
Gilles Peskine449bd832023-01-11 14:50:10 +01004084 mbedtls_sha256_free(&handshake->fin_sha256);
Manuel Pégourié-Gonnardb9d64e52015-07-06 14:18:56 +02004085#endif
Andrzej Kurekeb342242019-01-29 09:14:33 -05004086#endif
Andrzej Kurek25f27152022-08-17 16:09:31 -04004087#if defined(MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Andrzej Kurekeb342242019-01-29 09:14:33 -05004088#if defined(MBEDTLS_USE_PSA_CRYPTO)
Gilles Peskine449bd832023-01-11 14:50:10 +01004089 psa_hash_abort(&handshake->fin_sha384_psa);
Andrzej Kurekeb342242019-01-29 09:14:33 -05004090#else
Gilles Peskine449bd832023-01-11 14:50:10 +01004091 mbedtls_sha512_free(&handshake->fin_sha384);
Manuel Pégourié-Gonnardb9d64e52015-07-06 14:18:56 +02004092#endif
Andrzej Kurekeb342242019-01-29 09:14:33 -05004093#endif
Manuel Pégourié-Gonnardb9d64e52015-07-06 14:18:56 +02004094
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004095#if defined(MBEDTLS_DHM_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01004096 mbedtls_dhm_free(&handshake->dhm_ctx);
Paul Bakker48916f92012-09-16 19:57:18 +00004097#endif
Neil Armstrongf3f46412022-04-12 14:43:39 +02004098#if !defined(MBEDTLS_USE_PSA_CRYPTO) && defined(MBEDTLS_ECDH_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01004099 mbedtls_ecdh_free(&handshake->ecdh_ctx);
Paul Bakker61d113b2013-07-04 11:51:43 +02004100#endif
Valerio Setti02c25b52022-11-15 14:08:42 +01004101
Manuel Pégourié-Gonnardeef142d2015-09-16 10:05:04 +02004102#if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
Neil Armstrongca7d5062022-05-31 14:43:23 +02004103#if defined(MBEDTLS_USE_PSA_CRYPTO)
Gilles Peskine449bd832023-01-11 14:50:10 +01004104 psa_pake_abort(&handshake->psa_pake_ctx);
Valerio Settieb3f7882022-12-08 18:42:58 +01004105 /*
4106 * Opaque keys are not stored in the handshake's data and it's the user
4107 * responsibility to destroy them. Clear ones, instead, are created by
4108 * the TLS library and should be destroyed at the same level
4109 */
Gilles Peskine449bd832023-01-11 14:50:10 +01004110 if (!mbedtls_svc_key_id_is_null(handshake->psa_pake_password)) {
4111 psa_destroy_key(handshake->psa_pake_password);
Valerio Settieb3f7882022-12-08 18:42:58 +01004112 }
Neil Armstrongca7d5062022-05-31 14:43:23 +02004113 handshake->psa_pake_password = MBEDTLS_SVC_KEY_ID_INIT;
4114#else
Gilles Peskine449bd832023-01-11 14:50:10 +01004115 mbedtls_ecjpake_free(&handshake->ecjpake_ctx);
Neil Armstrongca7d5062022-05-31 14:43:23 +02004116#endif /* MBEDTLS_USE_PSA_CRYPTO */
Manuel Pégourié-Gonnard77c06462015-09-17 13:59:49 +02004117#if defined(MBEDTLS_SSL_CLI_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01004118 mbedtls_free(handshake->ecjpake_cache);
Manuel Pégourié-Gonnard77c06462015-09-17 13:59:49 +02004119 handshake->ecjpake_cache = NULL;
4120 handshake->ecjpake_cache_len = 0;
4121#endif
Manuel Pégourié-Gonnard76cfd3f2015-09-15 12:10:54 +02004122#endif
Paul Bakker61d113b2013-07-04 11:51:43 +02004123
Janos Follath4ae5c292016-02-10 11:27:43 +00004124#if defined(MBEDTLS_ECDH_C) || defined(MBEDTLS_ECDSA_C) || \
4125 defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
Paul Bakker9af723c2014-05-01 13:03:14 +02004126 /* explicit void pointer cast for buggy MS compiler */
Gilles Peskine449bd832023-01-11 14:50:10 +01004127 mbedtls_free((void *) handshake->curves_tls_id);
Manuel Pégourié-Gonnardd09453c2013-09-23 19:11:32 +02004128#endif
4129
Ronald Cron73fe8df2022-10-05 14:31:43 +02004130#if defined(MBEDTLS_SSL_HANDSHAKE_WITH_PSK_ENABLED)
Neil Armstrong501c9322022-05-03 09:35:09 +02004131#if defined(MBEDTLS_USE_PSA_CRYPTO)
Gilles Peskine449bd832023-01-11 14:50:10 +01004132 if (!mbedtls_svc_key_id_is_null(ssl->handshake->psk_opaque)) {
Neil Armstrong501c9322022-05-03 09:35:09 +02004133 /* The maintenance of the external PSK key slot is the
4134 * user's responsibility. */
Gilles Peskine449bd832023-01-11 14:50:10 +01004135 if (ssl->handshake->psk_opaque_is_internal) {
4136 psa_destroy_key(ssl->handshake->psk_opaque);
Neil Armstrong501c9322022-05-03 09:35:09 +02004137 ssl->handshake->psk_opaque_is_internal = 0;
4138 }
4139 ssl->handshake->psk_opaque = MBEDTLS_SVC_KEY_ID_INIT;
4140 }
Neil Armstronge952a302022-05-03 10:22:14 +02004141#else
Gilles Peskine449bd832023-01-11 14:50:10 +01004142 if (handshake->psk != NULL) {
4143 mbedtls_platform_zeroize(handshake->psk, handshake->psk_len);
4144 mbedtls_free(handshake->psk);
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01004145 }
Neil Armstronge952a302022-05-03 10:22:14 +02004146#endif /* MBEDTLS_USE_PSA_CRYPTO */
Ronald Cron73fe8df2022-10-05 14:31:43 +02004147#endif /* MBEDTLS_SSL_HANDSHAKE_WITH_PSK_ENABLED */
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01004148
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004149#if defined(MBEDTLS_X509_CRT_PARSE_C) && \
4150 defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
Manuel Pégourié-Gonnard83724542013-09-24 22:30:56 +02004151 /*
4152 * Free only the linked list wrapper, not the keys themselves
4153 * since the belong to the SNI callback
4154 */
Gilles Peskine449bd832023-01-11 14:50:10 +01004155 ssl_key_cert_free(handshake->sni_key_cert);
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004156#endif /* MBEDTLS_X509_CRT_PARSE_C && MBEDTLS_SSL_SERVER_NAME_INDICATION */
Manuel Pégourié-Gonnard705fcca2013-09-23 20:04:20 +02004157
Gilles Peskineeccd8882020-03-10 12:19:08 +01004158#if defined(MBEDTLS_SSL_ECP_RESTARTABLE_ENABLED)
Gilles Peskine449bd832023-01-11 14:50:10 +01004159 mbedtls_x509_crt_restart_free(&handshake->ecrs_ctx);
4160 if (handshake->ecrs_peer_cert != NULL) {
4161 mbedtls_x509_crt_free(handshake->ecrs_peer_cert);
4162 mbedtls_free(handshake->ecrs_peer_cert);
Hanno Becker3dad3112019-02-05 17:19:52 +00004163 }
Manuel Pégourié-Gonnard862cde52017-05-17 11:56:15 +02004164#endif
4165
Hanno Becker75173122019-02-06 16:18:31 +00004166#if defined(MBEDTLS_X509_CRT_PARSE_C) && \
4167 !defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
Gilles Peskine449bd832023-01-11 14:50:10 +01004168 mbedtls_pk_free(&handshake->peer_pubkey);
Hanno Becker75173122019-02-06 16:18:31 +00004169#endif /* MBEDTLS_X509_CRT_PARSE_C && !MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
4170
XiaokangQian9b93c0d2022-02-09 06:02:25 +00004171#if defined(MBEDTLS_SSL_CLI_C) && \
Gilles Peskine449bd832023-01-11 14:50:10 +01004172 (defined(MBEDTLS_SSL_PROTO_DTLS) || defined(MBEDTLS_SSL_PROTO_TLS1_3))
4173 mbedtls_free(handshake->cookie);
XiaokangQian9b93c0d2022-02-09 06:02:25 +00004174#endif /* MBEDTLS_SSL_CLI_C &&
4175 ( MBEDTLS_SSL_PROTO_DTLS || MBEDTLS_SSL_PROTO_TLS1_3 ) */
XiaokangQian8499b6c2022-01-27 09:00:11 +00004176
4177#if defined(MBEDTLS_SSL_PROTO_DTLS)
Gilles Peskine449bd832023-01-11 14:50:10 +01004178 mbedtls_ssl_flight_free(handshake->flight);
4179 mbedtls_ssl_buffering_free(ssl);
XiaokangQian8499b6c2022-01-27 09:00:11 +00004180#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnard74848812014-07-11 02:43:49 +02004181
Ronald Cronf12b81d2022-03-15 10:42:41 +01004182#if defined(MBEDTLS_ECDH_C) && \
Gilles Peskine449bd832023-01-11 14:50:10 +01004183 (defined(MBEDTLS_USE_PSA_CRYPTO) || defined(MBEDTLS_SSL_PROTO_TLS1_3))
4184 if (handshake->ecdh_psa_privkey_is_external == 0) {
4185 psa_destroy_key(handshake->ecdh_psa_privkey);
4186 }
Hanno Becker4a63ed42019-01-08 11:39:35 +00004187#endif /* MBEDTLS_ECDH_C && MBEDTLS_USE_PSA_CRYPTO */
4188
Ronald Cron6f135e12021-12-08 16:57:54 +01004189#if defined(MBEDTLS_SSL_PROTO_TLS1_3)
Gilles Peskine449bd832023-01-11 14:50:10 +01004190 mbedtls_ssl_transform_free(handshake->transform_handshake);
4191 mbedtls_free(handshake->transform_handshake);
Jerry Yu3d9b5902022-11-04 14:07:25 +08004192#if defined(MBEDTLS_SSL_EARLY_DATA)
Gilles Peskine449bd832023-01-11 14:50:10 +01004193 mbedtls_ssl_transform_free(handshake->transform_earlydata);
4194 mbedtls_free(handshake->transform_earlydata);
Jerry Yu3d9b5902022-11-04 14:07:25 +08004195#endif
Ronald Cron6f135e12021-12-08 16:57:54 +01004196#endif /* MBEDTLS_SSL_PROTO_TLS1_3 */
Jerry Yuba9c7272021-10-30 11:54:10 +08004197
Andrzej Kurek0afa2a12020-03-03 10:39:58 -05004198
4199#if defined(MBEDTLS_SSL_VARIABLE_BUFFER_LENGTH)
4200 /* If the buffers are too big - reallocate. Because of the way Mbed TLS
4201 * processes datagrams and the fact that a datagram is allowed to have
4202 * several records in it, it is possible that the I/O buffers are not
4203 * empty at this stage */
Gilles Peskine449bd832023-01-11 14:50:10 +01004204 handle_buffer_resizing(ssl, 1, mbedtls_ssl_get_input_buflen(ssl),
4205 mbedtls_ssl_get_output_buflen(ssl));
Andrzej Kurek0afa2a12020-03-03 10:39:58 -05004206#endif
Hanno Becker3aa186f2021-08-10 09:24:19 +01004207
Jerry Yuba9c7272021-10-30 11:54:10 +08004208 /* mbedtls_platform_zeroize MUST be last one in this function */
Gilles Peskine449bd832023-01-11 14:50:10 +01004209 mbedtls_platform_zeroize(handshake,
4210 sizeof(mbedtls_ssl_handshake_params));
Paul Bakker48916f92012-09-16 19:57:18 +00004211}
4212
Gilles Peskine449bd832023-01-11 14:50:10 +01004213void mbedtls_ssl_session_free(mbedtls_ssl_session *session)
Paul Bakker48916f92012-09-16 19:57:18 +00004214{
Gilles Peskine449bd832023-01-11 14:50:10 +01004215 if (session == NULL) {
Paul Bakkeraccaffe2014-06-26 13:37:14 +02004216 return;
Gilles Peskine449bd832023-01-11 14:50:10 +01004217 }
Paul Bakkeraccaffe2014-06-26 13:37:14 +02004218
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004219#if defined(MBEDTLS_X509_CRT_PARSE_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01004220 ssl_clear_peer_cert(session);
Paul Bakkered27a042013-04-18 22:46:23 +02004221#endif
Paul Bakker0a597072012-09-25 21:55:46 +00004222
Manuel Pégourié-Gonnardb596abf2015-05-20 10:45:29 +02004223#if defined(MBEDTLS_SSL_SESSION_TICKETS) && defined(MBEDTLS_SSL_CLI_C)
Xiaokang Qianbc663a02022-10-09 11:14:39 +00004224#if defined(MBEDTLS_SSL_PROTO_TLS1_3) && \
4225 defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
Gilles Peskine449bd832023-01-11 14:50:10 +01004226 mbedtls_free(session->hostname);
Xiaokang Qian281fd1b2022-09-20 11:35:41 +00004227#endif
Gilles Peskine449bd832023-01-11 14:50:10 +01004228 mbedtls_free(session->ticket);
Paul Bakkera503a632013-08-14 13:48:06 +02004229#endif
Manuel Pégourié-Gonnard75d44012013-08-02 14:44:04 +02004230
Gilles Peskine449bd832023-01-11 14:50:10 +01004231 mbedtls_platform_zeroize(session, sizeof(mbedtls_ssl_session));
Paul Bakker48916f92012-09-16 19:57:18 +00004232}
4233
Manuel Pégourié-Gonnard5c0e3772019-07-23 16:13:17 +02004234#if defined(MBEDTLS_SSL_CONTEXT_SERIALIZATION)
Manuel Pégourié-Gonnard4e9370b2019-07-23 16:31:16 +02004235
4236#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
4237#define SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_CONNECTION_ID 1u
4238#else
4239#define SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_CONNECTION_ID 0u
4240#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
4241
Manuel Pégourié-Gonnard4e9370b2019-07-23 16:31:16 +02004242#define SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_BADMAC_LIMIT 1u
Manuel Pégourié-Gonnard4e9370b2019-07-23 16:31:16 +02004243
4244#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
4245#define SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_ANTI_REPLAY 1u
4246#else
4247#define SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_ANTI_REPLAY 0u
4248#endif /* MBEDTLS_SSL_DTLS_ANTI_REPLAY */
4249
4250#if defined(MBEDTLS_SSL_ALPN)
4251#define SSL_SERIALIZED_CONTEXT_CONFIG_ALPN 1u
4252#else
4253#define SSL_SERIALIZED_CONTEXT_CONFIG_ALPN 0u
4254#endif /* MBEDTLS_SSL_ALPN */
4255
4256#define SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_CONNECTION_ID_BIT 0
4257#define SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_BADMAC_LIMIT_BIT 1
4258#define SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_ANTI_REPLAY_BIT 2
4259#define SSL_SERIALIZED_CONTEXT_CONFIG_ALPN_BIT 3
4260
4261#define SSL_SERIALIZED_CONTEXT_CONFIG_BITFLAG \
Gilles Peskine449bd832023-01-11 14:50:10 +01004262 ((uint32_t) ( \
4263 (SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_CONNECTION_ID << \
4264 SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_CONNECTION_ID_BIT) | \
4265 (SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_BADMAC_LIMIT << \
4266 SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_BADMAC_LIMIT_BIT) | \
4267 (SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_ANTI_REPLAY << \
4268 SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_ANTI_REPLAY_BIT) | \
4269 (SSL_SERIALIZED_CONTEXT_CONFIG_ALPN << SSL_SERIALIZED_CONTEXT_CONFIG_ALPN_BIT) | \
4270 0u))
Manuel Pégourié-Gonnard4e9370b2019-07-23 16:31:16 +02004271
Manuel Pégourié-Gonnard4c90e852019-07-11 10:58:10 +02004272static unsigned char ssl_serialized_context_header[] = {
4273 MBEDTLS_VERSION_MAJOR,
4274 MBEDTLS_VERSION_MINOR,
4275 MBEDTLS_VERSION_PATCH,
Gilles Peskine449bd832023-01-11 14:50:10 +01004276 MBEDTLS_BYTE_1(SSL_SERIALIZED_SESSION_CONFIG_BITFLAG),
4277 MBEDTLS_BYTE_0(SSL_SERIALIZED_SESSION_CONFIG_BITFLAG),
4278 MBEDTLS_BYTE_2(SSL_SERIALIZED_CONTEXT_CONFIG_BITFLAG),
4279 MBEDTLS_BYTE_1(SSL_SERIALIZED_CONTEXT_CONFIG_BITFLAG),
4280 MBEDTLS_BYTE_0(SSL_SERIALIZED_CONTEXT_CONFIG_BITFLAG),
Manuel Pégourié-Gonnard4c90e852019-07-11 10:58:10 +02004281};
4282
Paul Bakker5121ce52009-01-03 21:22:43 +00004283/*
Manuel Pégourié-Gonnardac87e282019-05-28 13:02:16 +02004284 * Serialize a full SSL context
Manuel Pégourié-Gonnard00400c22019-07-10 14:58:45 +02004285 *
4286 * The format of the serialized data is:
4287 * (in the presentation language of TLS, RFC 8446 section 3)
4288 *
4289 * // header
4290 * opaque mbedtls_version[3]; // major, minor, patch
Manuel Pégourié-Gonnard4c90e852019-07-11 10:58:10 +02004291 * opaque context_format[5]; // version-specific field determining
Manuel Pégourié-Gonnard00400c22019-07-10 14:58:45 +02004292 * // the format of the remaining
Manuel Pégourié-Gonnard4c90e852019-07-11 10:58:10 +02004293 * // serialized data.
Manuel Pégourié-Gonnard4e9370b2019-07-23 16:31:16 +02004294 * Note: When updating the format, remember to keep these
4295 * version+format bytes. (We may make their size part of the API.)
Manuel Pégourié-Gonnard00400c22019-07-10 14:58:45 +02004296 *
4297 * // session sub-structure
4298 * opaque session<1..2^32-1>; // see mbedtls_ssl_session_save()
4299 * // transform sub-structure
4300 * uint8 random[64]; // ServerHello.random+ClientHello.random
4301 * uint8 in_cid<0..2^8-1> // Connection ID: expected incoming value
4302 * uint8 out_cid<0..2^8-1> // Connection ID: outgoing value to use
4303 * // fields from ssl_context
4304 * uint32 badmac_seen; // DTLS: number of records with failing MAC
4305 * uint64 in_window_top; // DTLS: last validated record seq_num
4306 * uint64 in_window; // DTLS: bitmask for replay protection
4307 * uint8 disable_datagram_packing; // DTLS: only one record per datagram
4308 * uint64 cur_out_ctr; // Record layer: outgoing sequence number
4309 * uint16 mtu; // DTLS: path mtu (max outgoing fragment size)
4310 * uint8 alpn_chosen<0..2^8-1> // ALPN: negotiated application protocol
4311 *
4312 * Note that many fields of the ssl_context or sub-structures are not
4313 * serialized, as they fall in one of the following categories:
4314 *
4315 * 1. forced value (eg in_left must be 0)
4316 * 2. pointer to dynamically-allocated memory (eg session, transform)
4317 * 3. value can be re-derived from other data (eg session keys from MS)
4318 * 4. value was temporary (eg content of input buffer)
4319 * 5. value will be provided by the user again (eg I/O callbacks and context)
Manuel Pégourié-Gonnardac87e282019-05-28 13:02:16 +02004320 */
Gilles Peskine449bd832023-01-11 14:50:10 +01004321int mbedtls_ssl_context_save(mbedtls_ssl_context *ssl,
4322 unsigned char *buf,
4323 size_t buf_len,
4324 size_t *olen)
Manuel Pégourié-Gonnardac87e282019-05-28 13:02:16 +02004325{
Manuel Pégourié-Gonnard4c90e852019-07-11 10:58:10 +02004326 unsigned char *p = buf;
4327 size_t used = 0;
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02004328 size_t session_len;
4329 int ret = 0;
Manuel Pégourié-Gonnard4c90e852019-07-11 10:58:10 +02004330
Manuel Pégourié-Gonnard1aaf6692019-07-10 14:14:05 +02004331 /*
Manuel Pégourié-Gonnarde4588692019-07-29 12:28:52 +02004332 * Enforce usage restrictions, see "return BAD_INPUT_DATA" in
4333 * this function's documentation.
4334 *
4335 * These are due to assumptions/limitations in the implementation. Some of
4336 * them are likely to stay (no handshake in progress) some might go away
4337 * (only DTLS) but are currently used to simplify the implementation.
Manuel Pégourié-Gonnard1aaf6692019-07-10 14:14:05 +02004338 */
Manuel Pégourié-Gonnarde4588692019-07-29 12:28:52 +02004339 /* The initial handshake must be over */
Gilles Peskine449bd832023-01-11 14:50:10 +01004340 if (mbedtls_ssl_is_handshake_over(ssl) == 0) {
4341 MBEDTLS_SSL_DEBUG_MSG(1, ("Initial handshake isn't over"));
4342 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
Jarno Lamsa8c51b7c2019-08-21 13:45:05 +03004343 }
Gilles Peskine449bd832023-01-11 14:50:10 +01004344 if (ssl->handshake != NULL) {
4345 MBEDTLS_SSL_DEBUG_MSG(1, ("Handshake isn't completed"));
4346 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
Jarno Lamsa8c51b7c2019-08-21 13:45:05 +03004347 }
Manuel Pégourié-Gonnarde4588692019-07-29 12:28:52 +02004348 /* Double-check that sub-structures are indeed ready */
Gilles Peskine449bd832023-01-11 14:50:10 +01004349 if (ssl->transform == NULL || ssl->session == NULL) {
4350 MBEDTLS_SSL_DEBUG_MSG(1, ("Serialised structures aren't ready"));
4351 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
Jarno Lamsa8c51b7c2019-08-21 13:45:05 +03004352 }
Manuel Pégourié-Gonnarde4588692019-07-29 12:28:52 +02004353 /* There must be no pending incoming or outgoing data */
Gilles Peskine449bd832023-01-11 14:50:10 +01004354 if (mbedtls_ssl_check_pending(ssl) != 0) {
4355 MBEDTLS_SSL_DEBUG_MSG(1, ("There is pending incoming data"));
4356 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
Jarno Lamsa8c51b7c2019-08-21 13:45:05 +03004357 }
Gilles Peskine449bd832023-01-11 14:50:10 +01004358 if (ssl->out_left != 0) {
4359 MBEDTLS_SSL_DEBUG_MSG(1, ("There is pending outgoing data"));
4360 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
Jarno Lamsa8c51b7c2019-08-21 13:45:05 +03004361 }
Dave Rodgman556e8a32022-12-06 16:31:25 +00004362 /* Protocol must be DTLS, not TLS */
Gilles Peskine449bd832023-01-11 14:50:10 +01004363 if (ssl->conf->transport != MBEDTLS_SSL_TRANSPORT_DATAGRAM) {
4364 MBEDTLS_SSL_DEBUG_MSG(1, ("Only DTLS is supported"));
4365 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
Jarno Lamsa8c51b7c2019-08-21 13:45:05 +03004366 }
Manuel Pégourié-Gonnarde4588692019-07-29 12:28:52 +02004367 /* Version must be 1.2 */
Gilles Peskine449bd832023-01-11 14:50:10 +01004368 if (ssl->tls_version != MBEDTLS_SSL_VERSION_TLS1_2) {
4369 MBEDTLS_SSL_DEBUG_MSG(1, ("Only version 1.2 supported"));
4370 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
Jarno Lamsa8c51b7c2019-08-21 13:45:05 +03004371 }
Manuel Pégourié-Gonnarde4588692019-07-29 12:28:52 +02004372 /* We must be using an AEAD ciphersuite */
Gilles Peskine449bd832023-01-11 14:50:10 +01004373 if (mbedtls_ssl_transform_uses_aead(ssl->transform) != 1) {
4374 MBEDTLS_SSL_DEBUG_MSG(1, ("Only AEAD ciphersuites supported"));
4375 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
Jarno Lamsa8c51b7c2019-08-21 13:45:05 +03004376 }
Manuel Pégourié-Gonnarde4588692019-07-29 12:28:52 +02004377 /* Renegotiation must not be enabled */
4378#if defined(MBEDTLS_SSL_RENEGOTIATION)
Gilles Peskine449bd832023-01-11 14:50:10 +01004379 if (ssl->conf->disable_renegotiation != MBEDTLS_SSL_RENEGOTIATION_DISABLED) {
4380 MBEDTLS_SSL_DEBUG_MSG(1, ("Renegotiation must not be enabled"));
4381 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
Jarno Lamsa8c51b7c2019-08-21 13:45:05 +03004382 }
Manuel Pégourié-Gonnarde4588692019-07-29 12:28:52 +02004383#endif
Manuel Pégourié-Gonnardac87e282019-05-28 13:02:16 +02004384
Manuel Pégourié-Gonnard4c90e852019-07-11 10:58:10 +02004385 /*
4386 * Version and format identifier
4387 */
Gilles Peskine449bd832023-01-11 14:50:10 +01004388 used += sizeof(ssl_serialized_context_header);
Manuel Pégourié-Gonnardac87e282019-05-28 13:02:16 +02004389
Gilles Peskine449bd832023-01-11 14:50:10 +01004390 if (used <= buf_len) {
4391 memcpy(p, ssl_serialized_context_header,
4392 sizeof(ssl_serialized_context_header));
4393 p += sizeof(ssl_serialized_context_header);
Manuel Pégourié-Gonnard4c90e852019-07-11 10:58:10 +02004394 }
4395
4396 /*
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02004397 * Session (length + data)
4398 */
Gilles Peskine449bd832023-01-11 14:50:10 +01004399 ret = ssl_session_save(ssl->session, 1, NULL, 0, &session_len);
4400 if (ret != MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL) {
4401 return ret;
4402 }
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02004403
4404 used += 4 + session_len;
Gilles Peskine449bd832023-01-11 14:50:10 +01004405 if (used <= buf_len) {
4406 MBEDTLS_PUT_UINT32_BE(session_len, p, 0);
Joe Subbiani1f6c3ae2021-08-20 11:44:44 +01004407 p += 4;
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02004408
Gilles Peskine449bd832023-01-11 14:50:10 +01004409 ret = ssl_session_save(ssl->session, 1,
4410 p, session_len, &session_len);
4411 if (ret != 0) {
4412 return ret;
4413 }
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02004414
4415 p += session_len;
4416 }
4417
4418 /*
Manuel Pégourié-Gonnardc2a7b892019-07-15 09:04:11 +02004419 * Transform
4420 */
Gilles Peskine449bd832023-01-11 14:50:10 +01004421 used += sizeof(ssl->transform->randbytes);
4422 if (used <= buf_len) {
4423 memcpy(p, ssl->transform->randbytes,
4424 sizeof(ssl->transform->randbytes));
4425 p += sizeof(ssl->transform->randbytes);
Manuel Pégourié-Gonnardc2a7b892019-07-15 09:04:11 +02004426 }
4427
4428#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
4429 used += 2 + ssl->transform->in_cid_len + ssl->transform->out_cid_len;
Gilles Peskine449bd832023-01-11 14:50:10 +01004430 if (used <= buf_len) {
Manuel Pégourié-Gonnardc2a7b892019-07-15 09:04:11 +02004431 *p++ = ssl->transform->in_cid_len;
Gilles Peskine449bd832023-01-11 14:50:10 +01004432 memcpy(p, ssl->transform->in_cid, ssl->transform->in_cid_len);
Manuel Pégourié-Gonnardc2a7b892019-07-15 09:04:11 +02004433 p += ssl->transform->in_cid_len;
4434
4435 *p++ = ssl->transform->out_cid_len;
Gilles Peskine449bd832023-01-11 14:50:10 +01004436 memcpy(p, ssl->transform->out_cid, ssl->transform->out_cid_len);
Manuel Pégourié-Gonnardc2a7b892019-07-15 09:04:11 +02004437 p += ssl->transform->out_cid_len;
4438 }
4439#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
4440
4441 /*
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02004442 * Saved fields from top-level ssl_context structure
4443 */
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02004444 used += 4;
Gilles Peskine449bd832023-01-11 14:50:10 +01004445 if (used <= buf_len) {
4446 MBEDTLS_PUT_UINT32_BE(ssl->badmac_seen, p, 0);
Joe Subbiani1f6c3ae2021-08-20 11:44:44 +01004447 p += 4;
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02004448 }
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02004449
4450#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
4451 used += 16;
Gilles Peskine449bd832023-01-11 14:50:10 +01004452 if (used <= buf_len) {
4453 MBEDTLS_PUT_UINT64_BE(ssl->in_window_top, p, 0);
Joe Subbiani1f6c3ae2021-08-20 11:44:44 +01004454 p += 8;
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02004455
Gilles Peskine449bd832023-01-11 14:50:10 +01004456 MBEDTLS_PUT_UINT64_BE(ssl->in_window, p, 0);
Joe Subbiani1f6c3ae2021-08-20 11:44:44 +01004457 p += 8;
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02004458 }
4459#endif /* MBEDTLS_SSL_DTLS_ANTI_REPLAY */
4460
4461#if defined(MBEDTLS_SSL_PROTO_DTLS)
4462 used += 1;
Gilles Peskine449bd832023-01-11 14:50:10 +01004463 if (used <= buf_len) {
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02004464 *p++ = ssl->disable_datagram_packing;
4465 }
4466#endif /* MBEDTLS_SSL_PROTO_DTLS */
4467
Jerry Yuae0b2e22021-10-08 15:21:19 +08004468 used += MBEDTLS_SSL_SEQUENCE_NUMBER_LEN;
Gilles Peskine449bd832023-01-11 14:50:10 +01004469 if (used <= buf_len) {
4470 memcpy(p, ssl->cur_out_ctr, MBEDTLS_SSL_SEQUENCE_NUMBER_LEN);
Jerry Yuae0b2e22021-10-08 15:21:19 +08004471 p += MBEDTLS_SSL_SEQUENCE_NUMBER_LEN;
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02004472 }
4473
4474#if defined(MBEDTLS_SSL_PROTO_DTLS)
4475 used += 2;
Gilles Peskine449bd832023-01-11 14:50:10 +01004476 if (used <= buf_len) {
4477 MBEDTLS_PUT_UINT16_BE(ssl->mtu, p, 0);
Joe Subbiani1f6c3ae2021-08-20 11:44:44 +01004478 p += 2;
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02004479 }
4480#endif /* MBEDTLS_SSL_PROTO_DTLS */
4481
4482#if defined(MBEDTLS_SSL_ALPN)
4483 {
4484 const uint8_t alpn_len = ssl->alpn_chosen
Gilles Peskine449bd832023-01-11 14:50:10 +01004485 ? (uint8_t) strlen(ssl->alpn_chosen)
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02004486 : 0;
4487
4488 used += 1 + alpn_len;
Gilles Peskine449bd832023-01-11 14:50:10 +01004489 if (used <= buf_len) {
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02004490 *p++ = alpn_len;
4491
Gilles Peskine449bd832023-01-11 14:50:10 +01004492 if (ssl->alpn_chosen != NULL) {
4493 memcpy(p, ssl->alpn_chosen, alpn_len);
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02004494 p += alpn_len;
4495 }
4496 }
4497 }
4498#endif /* MBEDTLS_SSL_ALPN */
4499
4500 /*
Manuel Pégourié-Gonnard4c90e852019-07-11 10:58:10 +02004501 * Done
4502 */
4503 *olen = used;
4504
Gilles Peskine449bd832023-01-11 14:50:10 +01004505 if (used > buf_len) {
4506 return MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL;
4507 }
Manuel Pégourié-Gonnardac87e282019-05-28 13:02:16 +02004508
Gilles Peskine449bd832023-01-11 14:50:10 +01004509 MBEDTLS_SSL_DEBUG_BUF(4, "saved context", buf, used);
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02004510
Gilles Peskine449bd832023-01-11 14:50:10 +01004511 return mbedtls_ssl_session_reset_int(ssl, 0);
Manuel Pégourié-Gonnardac87e282019-05-28 13:02:16 +02004512}
4513
4514/*
Manuel Pégourié-Gonnardb9dfc9f2019-07-12 10:50:19 +02004515 * Deserialize context, see mbedtls_ssl_context_save() for format.
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02004516 *
4517 * This internal version is wrapped by a public function that cleans up in
4518 * case of error.
Manuel Pégourié-Gonnardac87e282019-05-28 13:02:16 +02004519 */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02004520MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01004521static int ssl_context_load(mbedtls_ssl_context *ssl,
4522 const unsigned char *buf,
4523 size_t len)
Manuel Pégourié-Gonnardac87e282019-05-28 13:02:16 +02004524{
Manuel Pégourié-Gonnard4c90e852019-07-11 10:58:10 +02004525 const unsigned char *p = buf;
4526 const unsigned char * const end = buf + len;
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02004527 size_t session_len;
Janos Follath865b3eb2019-12-16 11:46:15 +00004528 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Andrzej Kurek2d59dbc2022-10-13 08:34:38 -04004529#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
Andrzej Kurek894edde2022-09-29 06:31:14 -04004530 tls_prf_fn prf_func = NULL;
Andrzej Kurek2d59dbc2022-10-13 08:34:38 -04004531#endif
Manuel Pégourié-Gonnard4c90e852019-07-11 10:58:10 +02004532
Manuel Pégourié-Gonnard0ff76402019-07-11 09:56:30 +02004533 /*
4534 * The context should have been freshly setup or reset.
4535 * Give the user an error in case of obvious misuse.
Manuel Pégourié-Gonnard4ca930f2019-07-26 16:31:53 +02004536 * (Checking session is useful because it won't be NULL if we're
Manuel Pégourié-Gonnard0ff76402019-07-11 09:56:30 +02004537 * renegotiating, or if the user mistakenly loaded a session first.)
4538 */
Gilles Peskine449bd832023-01-11 14:50:10 +01004539 if (ssl->state != MBEDTLS_SSL_HELLO_REQUEST ||
4540 ssl->session != NULL) {
4541 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
Manuel Pégourié-Gonnard0ff76402019-07-11 09:56:30 +02004542 }
4543
4544 /*
4545 * We can't check that the config matches the initial one, but we can at
4546 * least check it matches the requirements for serializing.
4547 */
Gilles Peskine449bd832023-01-11 14:50:10 +01004548 if (ssl->conf->transport != MBEDTLS_SSL_TRANSPORT_DATAGRAM ||
Glenn Strauss2dfcea22022-03-14 17:26:42 -04004549 ssl->conf->max_tls_version < MBEDTLS_SSL_VERSION_TLS1_2 ||
4550 ssl->conf->min_tls_version > MBEDTLS_SSL_VERSION_TLS1_2 ||
Manuel Pégourié-Gonnard0ff76402019-07-11 09:56:30 +02004551#if defined(MBEDTLS_SSL_RENEGOTIATION)
Manuel Pégourié-Gonnard9a96fd72019-07-23 17:11:24 +02004552 ssl->conf->disable_renegotiation != MBEDTLS_SSL_RENEGOTIATION_DISABLED ||
Manuel Pégourié-Gonnard0ff76402019-07-11 09:56:30 +02004553#endif
Gilles Peskine449bd832023-01-11 14:50:10 +01004554 0) {
4555 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
Manuel Pégourié-Gonnard0ff76402019-07-11 09:56:30 +02004556 }
4557
Gilles Peskine449bd832023-01-11 14:50:10 +01004558 MBEDTLS_SSL_DEBUG_BUF(4, "context to load", buf, len);
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02004559
Manuel Pégourié-Gonnard4c90e852019-07-11 10:58:10 +02004560 /*
4561 * Check version identifier
4562 */
Gilles Peskine449bd832023-01-11 14:50:10 +01004563 if ((size_t) (end - p) < sizeof(ssl_serialized_context_header)) {
4564 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
Manuel Pégourié-Gonnard4c90e852019-07-11 10:58:10 +02004565 }
Gilles Peskine449bd832023-01-11 14:50:10 +01004566
4567 if (memcmp(p, ssl_serialized_context_header,
4568 sizeof(ssl_serialized_context_header)) != 0) {
4569 return MBEDTLS_ERR_SSL_VERSION_MISMATCH;
4570 }
4571 p += sizeof(ssl_serialized_context_header);
Manuel Pégourié-Gonnard4c90e852019-07-11 10:58:10 +02004572
4573 /*
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02004574 * Session
4575 */
Gilles Peskine449bd832023-01-11 14:50:10 +01004576 if ((size_t) (end - p) < 4) {
4577 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
4578 }
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02004579
Gilles Peskine449bd832023-01-11 14:50:10 +01004580 session_len = ((size_t) p[0] << 24) |
4581 ((size_t) p[1] << 16) |
4582 ((size_t) p[2] << 8) |
4583 ((size_t) p[3]);
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02004584 p += 4;
4585
Manuel Pégourié-Gonnard142ba732019-07-23 14:43:30 +02004586 /* This has been allocated by ssl_handshake_init(), called by
Hanno Becker43aefe22020-02-05 10:44:56 +00004587 * by either mbedtls_ssl_session_reset_int() or mbedtls_ssl_setup(). */
Manuel Pégourié-Gonnard142ba732019-07-23 14:43:30 +02004588 ssl->session = ssl->session_negotiate;
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02004589 ssl->session_in = ssl->session;
4590 ssl->session_out = ssl->session;
Manuel Pégourié-Gonnard142ba732019-07-23 14:43:30 +02004591 ssl->session_negotiate = NULL;
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02004592
Gilles Peskine449bd832023-01-11 14:50:10 +01004593 if ((size_t) (end - p) < session_len) {
4594 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
4595 }
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02004596
Gilles Peskine449bd832023-01-11 14:50:10 +01004597 ret = ssl_session_load(ssl->session, 1, p, session_len);
4598 if (ret != 0) {
4599 mbedtls_ssl_session_free(ssl->session);
4600 return ret;
Manuel Pégourié-Gonnard45ac1f02019-07-23 16:52:45 +02004601 }
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02004602
4603 p += session_len;
4604
4605 /*
Manuel Pégourié-Gonnardc2a7b892019-07-15 09:04:11 +02004606 * Transform
4607 */
4608
Manuel Pégourié-Gonnard142ba732019-07-23 14:43:30 +02004609 /* This has been allocated by ssl_handshake_init(), called by
Hanno Becker43aefe22020-02-05 10:44:56 +00004610 * by either mbedtls_ssl_session_reset_int() or mbedtls_ssl_setup(). */
Jerry Yu2e199812022-12-01 18:57:19 +08004611#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
Manuel Pégourié-Gonnard142ba732019-07-23 14:43:30 +02004612 ssl->transform = ssl->transform_negotiate;
Manuel Pégourié-Gonnardc2a7b892019-07-15 09:04:11 +02004613 ssl->transform_in = ssl->transform;
4614 ssl->transform_out = ssl->transform;
Manuel Pégourié-Gonnard142ba732019-07-23 14:43:30 +02004615 ssl->transform_negotiate = NULL;
Jerry Yu2e199812022-12-01 18:57:19 +08004616#endif
Manuel Pégourié-Gonnardc2a7b892019-07-15 09:04:11 +02004617
Andrzej Kurek2d59dbc2022-10-13 08:34:38 -04004618#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
Gilles Peskine449bd832023-01-11 14:50:10 +01004619 prf_func = ssl_tls12prf_from_cs(ssl->session->ciphersuite);
4620 if (prf_func == NULL) {
4621 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
4622 }
Andrzej Kurek894edde2022-09-29 06:31:14 -04004623
Manuel Pégourié-Gonnardc2a7b892019-07-15 09:04:11 +02004624 /* Read random bytes and populate structure */
Gilles Peskine449bd832023-01-11 14:50:10 +01004625 if ((size_t) (end - p) < sizeof(ssl->transform->randbytes)) {
4626 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
4627 }
Andrzej Kurek2d59dbc2022-10-13 08:34:38 -04004628
Gilles Peskine449bd832023-01-11 14:50:10 +01004629 ret = ssl_tls12_populate_transform(ssl->transform,
4630 ssl->session->ciphersuite,
4631 ssl->session->master,
Neil Armstrongf2c82f02022-04-05 11:16:53 +02004632#if defined(MBEDTLS_SSL_SOME_SUITES_USE_CBC_ETM)
Gilles Peskine449bd832023-01-11 14:50:10 +01004633 ssl->session->encrypt_then_mac,
Neil Armstrongf2c82f02022-04-05 11:16:53 +02004634#endif /* MBEDTLS_SSL_SOME_SUITES_USE_CBC_ETM */
Gilles Peskine449bd832023-01-11 14:50:10 +01004635 prf_func,
4636 p, /* currently pointing to randbytes */
4637 MBEDTLS_SSL_VERSION_TLS1_2, /* (D)TLS 1.2 is forced */
4638 ssl->conf->endpoint,
4639 ssl);
4640 if (ret != 0) {
4641 return ret;
4642 }
Jerry Yu840fbb22022-02-17 14:59:29 +08004643#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Gilles Peskine449bd832023-01-11 14:50:10 +01004644 p += sizeof(ssl->transform->randbytes);
Manuel Pégourié-Gonnardc2a7b892019-07-15 09:04:11 +02004645
4646#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
4647 /* Read connection IDs and store them */
Gilles Peskine449bd832023-01-11 14:50:10 +01004648 if ((size_t) (end - p) < 1) {
4649 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
4650 }
Manuel Pégourié-Gonnardc2a7b892019-07-15 09:04:11 +02004651
4652 ssl->transform->in_cid_len = *p++;
4653
Gilles Peskine449bd832023-01-11 14:50:10 +01004654 if ((size_t) (end - p) < ssl->transform->in_cid_len + 1u) {
4655 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
4656 }
Manuel Pégourié-Gonnardc2a7b892019-07-15 09:04:11 +02004657
Gilles Peskine449bd832023-01-11 14:50:10 +01004658 memcpy(ssl->transform->in_cid, p, ssl->transform->in_cid_len);
Manuel Pégourié-Gonnardc2a7b892019-07-15 09:04:11 +02004659 p += ssl->transform->in_cid_len;
4660
4661 ssl->transform->out_cid_len = *p++;
4662
Gilles Peskine449bd832023-01-11 14:50:10 +01004663 if ((size_t) (end - p) < ssl->transform->out_cid_len) {
4664 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
4665 }
Manuel Pégourié-Gonnardc2a7b892019-07-15 09:04:11 +02004666
Gilles Peskine449bd832023-01-11 14:50:10 +01004667 memcpy(ssl->transform->out_cid, p, ssl->transform->out_cid_len);
Manuel Pégourié-Gonnardc2a7b892019-07-15 09:04:11 +02004668 p += ssl->transform->out_cid_len;
4669#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
4670
4671 /*
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02004672 * Saved fields from top-level ssl_context structure
4673 */
Gilles Peskine449bd832023-01-11 14:50:10 +01004674 if ((size_t) (end - p) < 4) {
4675 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
4676 }
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02004677
Gilles Peskine449bd832023-01-11 14:50:10 +01004678 ssl->badmac_seen = ((uint32_t) p[0] << 24) |
4679 ((uint32_t) p[1] << 16) |
4680 ((uint32_t) p[2] << 8) |
4681 ((uint32_t) p[3]);
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02004682 p += 4;
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02004683
4684#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
Gilles Peskine449bd832023-01-11 14:50:10 +01004685 if ((size_t) (end - p) < 16) {
4686 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
4687 }
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02004688
Gilles Peskine449bd832023-01-11 14:50:10 +01004689 ssl->in_window_top = ((uint64_t) p[0] << 56) |
4690 ((uint64_t) p[1] << 48) |
4691 ((uint64_t) p[2] << 40) |
4692 ((uint64_t) p[3] << 32) |
4693 ((uint64_t) p[4] << 24) |
4694 ((uint64_t) p[5] << 16) |
4695 ((uint64_t) p[6] << 8) |
4696 ((uint64_t) p[7]);
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02004697 p += 8;
4698
Gilles Peskine449bd832023-01-11 14:50:10 +01004699 ssl->in_window = ((uint64_t) p[0] << 56) |
4700 ((uint64_t) p[1] << 48) |
4701 ((uint64_t) p[2] << 40) |
4702 ((uint64_t) p[3] << 32) |
4703 ((uint64_t) p[4] << 24) |
4704 ((uint64_t) p[5] << 16) |
4705 ((uint64_t) p[6] << 8) |
4706 ((uint64_t) p[7]);
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02004707 p += 8;
4708#endif /* MBEDTLS_SSL_DTLS_ANTI_REPLAY */
4709
4710#if defined(MBEDTLS_SSL_PROTO_DTLS)
Gilles Peskine449bd832023-01-11 14:50:10 +01004711 if ((size_t) (end - p) < 1) {
4712 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
4713 }
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02004714
4715 ssl->disable_datagram_packing = *p++;
4716#endif /* MBEDTLS_SSL_PROTO_DTLS */
4717
Gilles Peskine449bd832023-01-11 14:50:10 +01004718 if ((size_t) (end - p) < sizeof(ssl->cur_out_ctr)) {
4719 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
4720 }
4721 memcpy(ssl->cur_out_ctr, p, sizeof(ssl->cur_out_ctr));
4722 p += sizeof(ssl->cur_out_ctr);
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02004723
4724#if defined(MBEDTLS_SSL_PROTO_DTLS)
Gilles Peskine449bd832023-01-11 14:50:10 +01004725 if ((size_t) (end - p) < 2) {
4726 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
4727 }
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02004728
Gilles Peskine449bd832023-01-11 14:50:10 +01004729 ssl->mtu = (p[0] << 8) | p[1];
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02004730 p += 2;
4731#endif /* MBEDTLS_SSL_PROTO_DTLS */
4732
4733#if defined(MBEDTLS_SSL_ALPN)
4734 {
4735 uint8_t alpn_len;
4736 const char **cur;
4737
Gilles Peskine449bd832023-01-11 14:50:10 +01004738 if ((size_t) (end - p) < 1) {
4739 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
4740 }
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02004741
4742 alpn_len = *p++;
4743
Gilles Peskine449bd832023-01-11 14:50:10 +01004744 if (alpn_len != 0 && ssl->conf->alpn_list != NULL) {
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02004745 /* alpn_chosen should point to an item in the configured list */
Gilles Peskine449bd832023-01-11 14:50:10 +01004746 for (cur = ssl->conf->alpn_list; *cur != NULL; cur++) {
4747 if (strlen(*cur) == alpn_len &&
4748 memcmp(p, cur, alpn_len) == 0) {
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02004749 ssl->alpn_chosen = *cur;
4750 break;
4751 }
4752 }
4753 }
4754
4755 /* can only happen on conf mismatch */
Gilles Peskine449bd832023-01-11 14:50:10 +01004756 if (alpn_len != 0 && ssl->alpn_chosen == NULL) {
4757 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
4758 }
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02004759
4760 p += alpn_len;
4761 }
4762#endif /* MBEDTLS_SSL_ALPN */
4763
4764 /*
Manuel Pégourié-Gonnard0eb3eac2019-07-15 11:53:51 +02004765 * Forced fields from top-level ssl_context structure
4766 *
4767 * Most of them already set to the correct value by mbedtls_ssl_init() and
4768 * mbedtls_ssl_reset(), so we only need to set the remaining ones.
4769 */
4770 ssl->state = MBEDTLS_SSL_HANDSHAKE_OVER;
Glenn Strauss60bfe602022-03-14 19:04:24 -04004771 ssl->tls_version = MBEDTLS_SSL_VERSION_TLS1_2;
Manuel Pégourié-Gonnard0eb3eac2019-07-15 11:53:51 +02004772
Hanno Becker361b10d2019-08-30 10:42:49 +01004773 /* Adjust pointers for header fields of outgoing records to
4774 * the given transform, accounting for explicit IV and CID. */
Gilles Peskine449bd832023-01-11 14:50:10 +01004775 mbedtls_ssl_update_out_pointers(ssl, ssl->transform);
Hanno Becker361b10d2019-08-30 10:42:49 +01004776
Manuel Pégourié-Gonnard0eb3eac2019-07-15 11:53:51 +02004777#if defined(MBEDTLS_SSL_PROTO_DTLS)
4778 ssl->in_epoch = 1;
4779#endif
4780
4781 /* mbedtls_ssl_reset() leaves the handshake sub-structure allocated,
4782 * which we don't want - otherwise we'd end up freeing the wrong transform
Hanno Beckerce5f5fd2020-02-05 10:47:44 +00004783 * by calling mbedtls_ssl_handshake_wrapup_free_hs_transform()
4784 * inappropriately. */
Gilles Peskine449bd832023-01-11 14:50:10 +01004785 if (ssl->handshake != NULL) {
4786 mbedtls_ssl_handshake_free(ssl);
4787 mbedtls_free(ssl->handshake);
Manuel Pégourié-Gonnard0eb3eac2019-07-15 11:53:51 +02004788 ssl->handshake = NULL;
4789 }
4790
4791 /*
Manuel Pégourié-Gonnard4c90e852019-07-11 10:58:10 +02004792 * Done - should have consumed entire buffer
4793 */
Gilles Peskine449bd832023-01-11 14:50:10 +01004794 if (p != end) {
4795 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
4796 }
Manuel Pégourié-Gonnardac87e282019-05-28 13:02:16 +02004797
Gilles Peskine449bd832023-01-11 14:50:10 +01004798 return 0;
Manuel Pégourié-Gonnardac87e282019-05-28 13:02:16 +02004799}
4800
4801/*
Manuel Pégourié-Gonnardb9dfc9f2019-07-12 10:50:19 +02004802 * Deserialize context: public wrapper for error cleaning
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02004803 */
Gilles Peskine449bd832023-01-11 14:50:10 +01004804int mbedtls_ssl_context_load(mbedtls_ssl_context *context,
4805 const unsigned char *buf,
4806 size_t len)
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02004807{
Gilles Peskine449bd832023-01-11 14:50:10 +01004808 int ret = ssl_context_load(context, buf, len);
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02004809
Gilles Peskine449bd832023-01-11 14:50:10 +01004810 if (ret != 0) {
4811 mbedtls_ssl_free(context);
4812 }
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02004813
Gilles Peskine449bd832023-01-11 14:50:10 +01004814 return ret;
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02004815}
Manuel Pégourié-Gonnard5c0e3772019-07-23 16:13:17 +02004816#endif /* MBEDTLS_SSL_CONTEXT_SERIALIZATION */
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02004817
4818/*
Paul Bakker5121ce52009-01-03 21:22:43 +00004819 * Free an SSL context
4820 */
Gilles Peskine449bd832023-01-11 14:50:10 +01004821void mbedtls_ssl_free(mbedtls_ssl_context *ssl)
Paul Bakker5121ce52009-01-03 21:22:43 +00004822{
Gilles Peskine449bd832023-01-11 14:50:10 +01004823 if (ssl == NULL) {
Paul Bakkeraccaffe2014-06-26 13:37:14 +02004824 return;
Gilles Peskine449bd832023-01-11 14:50:10 +01004825 }
Paul Bakkeraccaffe2014-06-26 13:37:14 +02004826
Gilles Peskine449bd832023-01-11 14:50:10 +01004827 MBEDTLS_SSL_DEBUG_MSG(2, ("=> free"));
Paul Bakker5121ce52009-01-03 21:22:43 +00004828
Gilles Peskine449bd832023-01-11 14:50:10 +01004829 if (ssl->out_buf != NULL) {
sander-visserb8aa2072020-05-06 22:05:13 +02004830#if defined(MBEDTLS_SSL_VARIABLE_BUFFER_LENGTH)
4831 size_t out_buf_len = ssl->out_buf_len;
4832#else
4833 size_t out_buf_len = MBEDTLS_SSL_OUT_BUFFER_LEN;
4834#endif
4835
Gilles Peskine449bd832023-01-11 14:50:10 +01004836 mbedtls_platform_zeroize(ssl->out_buf, out_buf_len);
4837 mbedtls_free(ssl->out_buf);
Andrzej Kurek0afa2a12020-03-03 10:39:58 -05004838 ssl->out_buf = NULL;
Paul Bakker5121ce52009-01-03 21:22:43 +00004839 }
4840
Gilles Peskine449bd832023-01-11 14:50:10 +01004841 if (ssl->in_buf != NULL) {
sander-visserb8aa2072020-05-06 22:05:13 +02004842#if defined(MBEDTLS_SSL_VARIABLE_BUFFER_LENGTH)
4843 size_t in_buf_len = ssl->in_buf_len;
4844#else
4845 size_t in_buf_len = MBEDTLS_SSL_IN_BUFFER_LEN;
4846#endif
4847
Gilles Peskine449bd832023-01-11 14:50:10 +01004848 mbedtls_platform_zeroize(ssl->in_buf, in_buf_len);
4849 mbedtls_free(ssl->in_buf);
Andrzej Kurek0afa2a12020-03-03 10:39:58 -05004850 ssl->in_buf = NULL;
Paul Bakker5121ce52009-01-03 21:22:43 +00004851 }
4852
Gilles Peskine449bd832023-01-11 14:50:10 +01004853 if (ssl->transform) {
4854 mbedtls_ssl_transform_free(ssl->transform);
4855 mbedtls_free(ssl->transform);
Paul Bakker48916f92012-09-16 19:57:18 +00004856 }
4857
Gilles Peskine449bd832023-01-11 14:50:10 +01004858 if (ssl->handshake) {
4859 mbedtls_ssl_handshake_free(ssl);
4860 mbedtls_free(ssl->handshake);
Jerry Yu2e199812022-12-01 18:57:19 +08004861
4862#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
Gilles Peskine449bd832023-01-11 14:50:10 +01004863 mbedtls_ssl_transform_free(ssl->transform_negotiate);
4864 mbedtls_free(ssl->transform_negotiate);
Jerry Yu2e199812022-12-01 18:57:19 +08004865#endif
4866
Gilles Peskine449bd832023-01-11 14:50:10 +01004867 mbedtls_ssl_session_free(ssl->session_negotiate);
4868 mbedtls_free(ssl->session_negotiate);
Paul Bakker48916f92012-09-16 19:57:18 +00004869 }
4870
Ronald Cron6f135e12021-12-08 16:57:54 +01004871#if defined(MBEDTLS_SSL_PROTO_TLS1_3)
Gilles Peskine449bd832023-01-11 14:50:10 +01004872 mbedtls_ssl_transform_free(ssl->transform_application);
4873 mbedtls_free(ssl->transform_application);
Ronald Cron6f135e12021-12-08 16:57:54 +01004874#endif /* MBEDTLS_SSL_PROTO_TLS1_3 */
Hanno Becker3aa186f2021-08-10 09:24:19 +01004875
Gilles Peskine449bd832023-01-11 14:50:10 +01004876 if (ssl->session) {
4877 mbedtls_ssl_session_free(ssl->session);
4878 mbedtls_free(ssl->session);
Paul Bakkerc0463502013-02-14 11:19:38 +01004879 }
4880
Manuel Pégourié-Gonnard55fab2d2015-05-11 16:15:19 +02004881#if defined(MBEDTLS_X509_CRT_PARSE_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01004882 if (ssl->hostname != NULL) {
4883 mbedtls_platform_zeroize(ssl->hostname, strlen(ssl->hostname));
4884 mbedtls_free(ssl->hostname);
Paul Bakker5121ce52009-01-03 21:22:43 +00004885 }
Paul Bakker0be444a2013-08-27 21:55:01 +02004886#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00004887
Manuel Pégourié-Gonnarde057d3b2015-05-20 10:59:43 +02004888#if defined(MBEDTLS_SSL_DTLS_HELLO_VERIFY) && defined(MBEDTLS_SSL_SRV_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01004889 mbedtls_free(ssl->cli_id);
Manuel Pégourié-Gonnard43c02182014-07-22 17:32:01 +02004890#endif
4891
Gilles Peskine449bd832023-01-11 14:50:10 +01004892 MBEDTLS_SSL_DEBUG_MSG(2, ("<= free"));
Paul Bakker2da561c2009-02-05 18:00:28 +00004893
Paul Bakker86f04f42013-02-14 11:20:09 +01004894 /* Actually clear after last debug message */
Gilles Peskine449bd832023-01-11 14:50:10 +01004895 mbedtls_platform_zeroize(ssl, sizeof(mbedtls_ssl_context));
Paul Bakker5121ce52009-01-03 21:22:43 +00004896}
4897
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02004898/*
Shaun Case8b0ecbc2021-12-20 21:14:10 -08004899 * Initialize mbedtls_ssl_config
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02004900 */
Gilles Peskine449bd832023-01-11 14:50:10 +01004901void mbedtls_ssl_config_init(mbedtls_ssl_config *conf)
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02004902{
Gilles Peskine449bd832023-01-11 14:50:10 +01004903 memset(conf, 0, sizeof(mbedtls_ssl_config));
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02004904}
4905
Gilles Peskineae270bf2021-06-02 00:05:29 +02004906/* The selection should be the same as mbedtls_x509_crt_profile_default in
4907 * x509_crt.c, plus Montgomery curves for ECDHE. Here, the order matters:
Gilles Peskineb1940a72021-06-02 15:18:12 +02004908 * curves with a lower resource usage come first.
Gilles Peskineae270bf2021-06-02 00:05:29 +02004909 * See the documentation of mbedtls_ssl_conf_curves() for what we promise
Gilles Peskineb1940a72021-06-02 15:18:12 +02004910 * about this list.
4911 */
Brett Warrene0edc842021-08-17 09:53:13 +01004912static uint16_t ssl_preset_default_groups[] = {
Gilles Peskineae270bf2021-06-02 00:05:29 +02004913#if defined(MBEDTLS_ECP_DP_CURVE25519_ENABLED)
Brett Warrene0edc842021-08-17 09:53:13 +01004914 MBEDTLS_SSL_IANA_TLS_GROUP_X25519,
Gilles Peskineae270bf2021-06-02 00:05:29 +02004915#endif
4916#if defined(MBEDTLS_ECP_DP_SECP256R1_ENABLED)
Brett Warrene0edc842021-08-17 09:53:13 +01004917 MBEDTLS_SSL_IANA_TLS_GROUP_SECP256R1,
Gilles Peskineae270bf2021-06-02 00:05:29 +02004918#endif
Gilles Peskineb1940a72021-06-02 15:18:12 +02004919#if defined(MBEDTLS_ECP_DP_SECP384R1_ENABLED)
Brett Warrene0edc842021-08-17 09:53:13 +01004920 MBEDTLS_SSL_IANA_TLS_GROUP_SECP384R1,
Gilles Peskineb1940a72021-06-02 15:18:12 +02004921#endif
4922#if defined(MBEDTLS_ECP_DP_CURVE448_ENABLED)
Brett Warrene0edc842021-08-17 09:53:13 +01004923 MBEDTLS_SSL_IANA_TLS_GROUP_X448,
Gilles Peskineb1940a72021-06-02 15:18:12 +02004924#endif
4925#if defined(MBEDTLS_ECP_DP_SECP521R1_ENABLED)
Brett Warrene0edc842021-08-17 09:53:13 +01004926 MBEDTLS_SSL_IANA_TLS_GROUP_SECP521R1,
Gilles Peskineb1940a72021-06-02 15:18:12 +02004927#endif
Gilles Peskineae270bf2021-06-02 00:05:29 +02004928#if defined(MBEDTLS_ECP_DP_BP256R1_ENABLED)
Brett Warrene0edc842021-08-17 09:53:13 +01004929 MBEDTLS_SSL_IANA_TLS_GROUP_BP256R1,
Gilles Peskineae270bf2021-06-02 00:05:29 +02004930#endif
Gilles Peskineb1940a72021-06-02 15:18:12 +02004931#if defined(MBEDTLS_ECP_DP_BP384R1_ENABLED)
Brett Warrene0edc842021-08-17 09:53:13 +01004932 MBEDTLS_SSL_IANA_TLS_GROUP_BP384R1,
Gilles Peskineb1940a72021-06-02 15:18:12 +02004933#endif
4934#if defined(MBEDTLS_ECP_DP_BP512R1_ENABLED)
Brett Warrene0edc842021-08-17 09:53:13 +01004935 MBEDTLS_SSL_IANA_TLS_GROUP_BP512R1,
Gilles Peskineb1940a72021-06-02 15:18:12 +02004936#endif
Brett Warrene0edc842021-08-17 09:53:13 +01004937 MBEDTLS_SSL_IANA_TLS_GROUP_NONE
Gilles Peskineae270bf2021-06-02 00:05:29 +02004938};
Gilles Peskineae270bf2021-06-02 00:05:29 +02004939
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02004940static int ssl_preset_suiteb_ciphersuites[] = {
4941 MBEDTLS_TLS_ECDHE_ECDSA_WITH_AES_128_GCM_SHA256,
4942 MBEDTLS_TLS_ECDHE_ECDSA_WITH_AES_256_GCM_SHA384,
4943 0
4944};
4945
Ronald Crone68ab4f2022-10-05 12:46:29 +02004946#if defined(MBEDTLS_SSL_HANDSHAKE_WITH_CERT_ENABLED)
Hanno Becker9c6aa7b2021-08-10 13:50:43 +01004947
Jerry Yu909df7b2022-01-22 11:56:27 +08004948/* NOTICE:
Jerry Yu0b994b82022-01-25 17:22:12 +08004949 * For ssl_preset_*_sig_algs and ssl_tls12_preset_*_sig_algs, the following
Jerry Yu370e1462022-01-25 10:36:53 +08004950 * rules SHOULD be upheld.
4951 * - No duplicate entries.
4952 * - But if there is a good reason, do not change the order of the algorithms.
Jerry Yu09a99fc2022-07-28 14:22:17 +08004953 * - ssl_tls12_preset* is for TLS 1.2 use only.
Jerry Yu370e1462022-01-25 10:36:53 +08004954 * - ssl_preset_* is for TLS 1.3 only or hybrid TLS 1.3/1.2 handshakes.
Jerry Yu1a8b4812022-01-20 17:56:50 +08004955 */
Hanno Becker9c6aa7b2021-08-10 13:50:43 +01004956static uint16_t ssl_preset_default_sig_algs[] = {
Jerry Yu1a8b4812022-01-20 17:56:50 +08004957
Andrzej Kurek25f27152022-08-17 16:09:31 -04004958#if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA) && \
Jerry Yued5e9f42022-01-26 11:21:34 +08004959 defined(MBEDTLS_ECP_DP_SECP256R1_ENABLED)
4960 MBEDTLS_TLS1_3_SIG_ECDSA_SECP256R1_SHA256,
Andrzej Kurekcccb0442022-08-19 03:42:11 -04004961#endif /* MBEDTLS_ECDSA_C && MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA &&
Jerry Yued5e9f42022-01-26 11:21:34 +08004962 MBEDTLS_ECP_DP_SECP256R1_ENABLED */
Jerry Yu909df7b2022-01-22 11:56:27 +08004963
Andrzej Kurek25f27152022-08-17 16:09:31 -04004964#if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA) && \
Jerry Yu909df7b2022-01-22 11:56:27 +08004965 defined(MBEDTLS_ECP_DP_SECP384R1_ENABLED)
4966 MBEDTLS_TLS1_3_SIG_ECDSA_SECP384R1_SHA384,
Andrzej Kurekcccb0442022-08-19 03:42:11 -04004967#endif /* MBEDTLS_ECDSA_C && MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA&&
Jerry Yu909df7b2022-01-22 11:56:27 +08004968 MBEDTLS_ECP_DP_SECP384R1_ENABLED */
4969
Andrzej Kurek25f27152022-08-17 16:09:31 -04004970#if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_HAS_ALG_SHA_512_VIA_MD_OR_PSA_BASED_ON_USE_PSA) && \
Jerry Yued5e9f42022-01-26 11:21:34 +08004971 defined(MBEDTLS_ECP_DP_SECP521R1_ENABLED)
4972 MBEDTLS_TLS1_3_SIG_ECDSA_SECP521R1_SHA512,
Andrzej Kurekcccb0442022-08-19 03:42:11 -04004973#endif /* MBEDTLS_ECDSA_C && MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA&&
Jerry Yued5e9f42022-01-26 11:21:34 +08004974 MBEDTLS_ECP_DP_SECP521R1_ENABLED */
Jerry Yu909df7b2022-01-22 11:56:27 +08004975
Gilles Peskine449bd832023-01-11 14:50:10 +01004976#if defined(MBEDTLS_X509_RSASSA_PSS_SUPPORT) && \
4977 defined(MBEDTLS_HAS_ALG_SHA_512_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Jerry Yu9bb3ee42022-06-23 10:16:33 +08004978 MBEDTLS_TLS1_3_SIG_RSA_PSS_RSAE_SHA512,
Gilles Peskine449bd832023-01-11 14:50:10 +01004979#endif \
4980 /* MBEDTLS_X509_RSASSA_PSS_SUPPORT && MBEDTLS_HAS_ALG_SHA_512_VIA_MD_OR_PSA_BASED_ON_USE_PSA */
Jerry Yu9bb3ee42022-06-23 10:16:33 +08004981
Gilles Peskine449bd832023-01-11 14:50:10 +01004982#if defined(MBEDTLS_X509_RSASSA_PSS_SUPPORT) && \
4983 defined(MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Jerry Yu9bb3ee42022-06-23 10:16:33 +08004984 MBEDTLS_TLS1_3_SIG_RSA_PSS_RSAE_SHA384,
Gilles Peskine449bd832023-01-11 14:50:10 +01004985#endif \
4986 /* MBEDTLS_X509_RSASSA_PSS_SUPPORT && MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA */
Jerry Yu9bb3ee42022-06-23 10:16:33 +08004987
Gilles Peskine449bd832023-01-11 14:50:10 +01004988#if defined(MBEDTLS_X509_RSASSA_PSS_SUPPORT) && \
4989 defined(MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Jerry Yu9bb3ee42022-06-23 10:16:33 +08004990 MBEDTLS_TLS1_3_SIG_RSA_PSS_RSAE_SHA256,
Gilles Peskine449bd832023-01-11 14:50:10 +01004991#endif \
4992 /* MBEDTLS_X509_RSASSA_PSS_SUPPORT && MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA */
Jerry Yu9bb3ee42022-06-23 10:16:33 +08004993
Andrzej Kurek25f27152022-08-17 16:09:31 -04004994#if defined(MBEDTLS_RSA_C) && defined(MBEDTLS_HAS_ALG_SHA_512_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Jerry Yu693a47a2022-06-23 14:02:28 +08004995 MBEDTLS_TLS1_3_SIG_RSA_PKCS1_SHA512,
4996#endif /* MBEDTLS_RSA_C && MBEDTLS_SHA512_C */
4997
Andrzej Kurek25f27152022-08-17 16:09:31 -04004998#if defined(MBEDTLS_RSA_C) && defined(MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Jerry Yu693a47a2022-06-23 14:02:28 +08004999 MBEDTLS_TLS1_3_SIG_RSA_PKCS1_SHA384,
5000#endif /* MBEDTLS_RSA_C && MBEDTLS_SHA384_C */
5001
Andrzej Kurek25f27152022-08-17 16:09:31 -04005002#if defined(MBEDTLS_RSA_C) && defined(MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Jerry Yu693a47a2022-06-23 14:02:28 +08005003 MBEDTLS_TLS1_3_SIG_RSA_PKCS1_SHA256,
5004#endif /* MBEDTLS_RSA_C && MBEDTLS_SHA256_C */
5005
Gabor Mezei15b95a62022-05-09 16:37:58 +02005006 MBEDTLS_TLS_SIG_NONE
Jerry Yu909df7b2022-01-22 11:56:27 +08005007};
5008
5009/* NOTICE: see above */
5010#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
5011static uint16_t ssl_tls12_preset_default_sig_algs[] = {
Andrzej Kurek25f27152022-08-17 16:09:31 -04005012#if defined(MBEDTLS_HAS_ALG_SHA_512_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Gabor Mezeic1051b62022-05-10 13:13:58 +02005013#if defined(MBEDTLS_ECDSA_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01005014 MBEDTLS_SSL_TLS12_SIG_AND_HASH_ALG(MBEDTLS_SSL_SIG_ECDSA, MBEDTLS_SSL_HASH_SHA512),
Jerry Yu713013f2022-01-17 18:16:35 +08005015#endif
Jerry Yu09a99fc2022-07-28 14:22:17 +08005016#if defined(MBEDTLS_X509_RSASSA_PSS_SUPPORT)
5017 MBEDTLS_TLS1_3_SIG_RSA_PSS_RSAE_SHA512,
5018#endif /* MBEDTLS_X509_RSASSA_PSS_SUPPORT */
Gabor Mezeic1051b62022-05-10 13:13:58 +02005019#if defined(MBEDTLS_RSA_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01005020 MBEDTLS_SSL_TLS12_SIG_AND_HASH_ALG(MBEDTLS_SSL_SIG_RSA, MBEDTLS_SSL_HASH_SHA512),
Gabor Mezeic1051b62022-05-10 13:13:58 +02005021#endif
Andrzej Kurekcccb0442022-08-19 03:42:11 -04005022#endif /* MBEDTLS_HAS_ALG_SHA_512_VIA_MD_OR_PSA_BASED_ON_USE_PSA*/
Andrzej Kurek25f27152022-08-17 16:09:31 -04005023#if defined(MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Gabor Mezeic1051b62022-05-10 13:13:58 +02005024#if defined(MBEDTLS_ECDSA_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01005025 MBEDTLS_SSL_TLS12_SIG_AND_HASH_ALG(MBEDTLS_SSL_SIG_ECDSA, MBEDTLS_SSL_HASH_SHA384),
Jerry Yu11f0a9c2022-01-12 18:43:08 +08005026#endif
Jerry Yu09a99fc2022-07-28 14:22:17 +08005027#if defined(MBEDTLS_X509_RSASSA_PSS_SUPPORT)
5028 MBEDTLS_TLS1_3_SIG_RSA_PSS_RSAE_SHA384,
5029#endif /* MBEDTLS_X509_RSASSA_PSS_SUPPORT */
Gabor Mezeic1051b62022-05-10 13:13:58 +02005030#if defined(MBEDTLS_RSA_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01005031 MBEDTLS_SSL_TLS12_SIG_AND_HASH_ALG(MBEDTLS_SSL_SIG_RSA, MBEDTLS_SSL_HASH_SHA384),
Gabor Mezeic1051b62022-05-10 13:13:58 +02005032#endif
Andrzej Kurekcccb0442022-08-19 03:42:11 -04005033#endif /* MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA*/
Andrzej Kurek25f27152022-08-17 16:09:31 -04005034#if defined(MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Gabor Mezeic1051b62022-05-10 13:13:58 +02005035#if defined(MBEDTLS_ECDSA_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01005036 MBEDTLS_SSL_TLS12_SIG_AND_HASH_ALG(MBEDTLS_SSL_SIG_ECDSA, MBEDTLS_SSL_HASH_SHA256),
Jerry Yu11f0a9c2022-01-12 18:43:08 +08005037#endif
Jerry Yu09a99fc2022-07-28 14:22:17 +08005038#if defined(MBEDTLS_X509_RSASSA_PSS_SUPPORT)
5039 MBEDTLS_TLS1_3_SIG_RSA_PSS_RSAE_SHA256,
5040#endif /* MBEDTLS_X509_RSASSA_PSS_SUPPORT */
Gabor Mezeic1051b62022-05-10 13:13:58 +02005041#if defined(MBEDTLS_RSA_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01005042 MBEDTLS_SSL_TLS12_SIG_AND_HASH_ALG(MBEDTLS_SSL_SIG_RSA, MBEDTLS_SSL_HASH_SHA256),
Gabor Mezeic1051b62022-05-10 13:13:58 +02005043#endif
Andrzej Kurekcccb0442022-08-19 03:42:11 -04005044#endif /* MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA*/
Gabor Mezei15b95a62022-05-09 16:37:58 +02005045 MBEDTLS_TLS_SIG_NONE
Jerry Yu909df7b2022-01-22 11:56:27 +08005046};
5047#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
5048/* NOTICE: see above */
5049static uint16_t ssl_preset_suiteb_sig_algs[] = {
5050
Andrzej Kurek25f27152022-08-17 16:09:31 -04005051#if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA) && \
Jerry Yu909df7b2022-01-22 11:56:27 +08005052 defined(MBEDTLS_ECP_DP_SECP256R1_ENABLED)
5053 MBEDTLS_TLS1_3_SIG_ECDSA_SECP256R1_SHA256,
Andrzej Kurekcccb0442022-08-19 03:42:11 -04005054#endif /* MBEDTLS_ECDSA_C && MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA&&
Jerry Yu909df7b2022-01-22 11:56:27 +08005055 MBEDTLS_ECP_DP_SECP256R1_ENABLED */
5056
Andrzej Kurek25f27152022-08-17 16:09:31 -04005057#if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA) && \
Jerry Yu53037892022-01-25 11:02:06 +08005058 defined(MBEDTLS_ECP_DP_SECP384R1_ENABLED)
5059 MBEDTLS_TLS1_3_SIG_ECDSA_SECP384R1_SHA384,
Andrzej Kurekcccb0442022-08-19 03:42:11 -04005060#endif /* MBEDTLS_ECDSA_C && MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA&&
Jerry Yu53037892022-01-25 11:02:06 +08005061 MBEDTLS_ECP_DP_SECP384R1_ENABLED */
Jerry Yu909df7b2022-01-22 11:56:27 +08005062
Gilles Peskine449bd832023-01-11 14:50:10 +01005063#if defined(MBEDTLS_X509_RSASSA_PSS_SUPPORT) && \
5064 defined(MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Jerry Yu909df7b2022-01-22 11:56:27 +08005065 MBEDTLS_TLS1_3_SIG_RSA_PSS_RSAE_SHA256,
Gilles Peskine449bd832023-01-11 14:50:10 +01005066#endif \
5067 /* MBEDTLS_X509_RSASSA_PSS_SUPPORT && MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA*/
Jerry Yu1a8b4812022-01-20 17:56:50 +08005068
Andrzej Kurek25f27152022-08-17 16:09:31 -04005069#if defined(MBEDTLS_RSA_C) && defined(MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Jerry Yu53037892022-01-25 11:02:06 +08005070 MBEDTLS_TLS1_3_SIG_RSA_PKCS1_SHA256,
Andrzej Kurekcccb0442022-08-19 03:42:11 -04005071#endif /* MBEDTLS_RSA_C && MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA*/
Jerry Yu53037892022-01-25 11:02:06 +08005072
Gabor Mezei15b95a62022-05-09 16:37:58 +02005073 MBEDTLS_TLS_SIG_NONE
Jerry Yu713013f2022-01-17 18:16:35 +08005074};
Jerry Yu6106fdc2022-01-12 16:36:14 +08005075
Jerry Yu909df7b2022-01-22 11:56:27 +08005076/* NOTICE: see above */
5077#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
5078static uint16_t ssl_tls12_preset_suiteb_sig_algs[] = {
Andrzej Kurek25f27152022-08-17 16:09:31 -04005079#if defined(MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Gabor Mezeic1051b62022-05-10 13:13:58 +02005080#if defined(MBEDTLS_ECDSA_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01005081 MBEDTLS_SSL_TLS12_SIG_AND_HASH_ALG(MBEDTLS_SSL_SIG_ECDSA, MBEDTLS_SSL_HASH_SHA256),
Jerry Yu713013f2022-01-17 18:16:35 +08005082#endif
Gabor Mezeic1051b62022-05-10 13:13:58 +02005083#if defined(MBEDTLS_RSA_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01005084 MBEDTLS_SSL_TLS12_SIG_AND_HASH_ALG(MBEDTLS_SSL_SIG_RSA, MBEDTLS_SSL_HASH_SHA256),
Gabor Mezeic1051b62022-05-10 13:13:58 +02005085#endif
Andrzej Kurekcccb0442022-08-19 03:42:11 -04005086#endif /* MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA*/
Andrzej Kurek25f27152022-08-17 16:09:31 -04005087#if defined(MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Gabor Mezeic1051b62022-05-10 13:13:58 +02005088#if defined(MBEDTLS_ECDSA_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01005089 MBEDTLS_SSL_TLS12_SIG_AND_HASH_ALG(MBEDTLS_SSL_SIG_ECDSA, MBEDTLS_SSL_HASH_SHA384),
Jerry Yu18c833e2022-01-25 10:55:47 +08005090#endif
Gabor Mezeic1051b62022-05-10 13:13:58 +02005091#if defined(MBEDTLS_RSA_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01005092 MBEDTLS_SSL_TLS12_SIG_AND_HASH_ALG(MBEDTLS_SSL_SIG_RSA, MBEDTLS_SSL_HASH_SHA384),
Gabor Mezeic1051b62022-05-10 13:13:58 +02005093#endif
Andrzej Kurekcccb0442022-08-19 03:42:11 -04005094#endif /* MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA*/
Gabor Mezei15b95a62022-05-09 16:37:58 +02005095 MBEDTLS_TLS_SIG_NONE
Hanno Becker9c6aa7b2021-08-10 13:50:43 +01005096};
Jerry Yu909df7b2022-01-22 11:56:27 +08005097#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
5098
Ronald Crone68ab4f2022-10-05 12:46:29 +02005099#endif /* MBEDTLS_SSL_HANDSHAKE_WITH_CERT_ENABLED */
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02005100
Brett Warrene0edc842021-08-17 09:53:13 +01005101static uint16_t ssl_preset_suiteb_groups[] = {
Jaeden Amerod4311042019-06-03 08:27:16 +01005102#if defined(MBEDTLS_ECP_DP_SECP256R1_ENABLED)
Brett Warrene0edc842021-08-17 09:53:13 +01005103 MBEDTLS_SSL_IANA_TLS_GROUP_SECP256R1,
Jaeden Amerod4311042019-06-03 08:27:16 +01005104#endif
5105#if defined(MBEDTLS_ECP_DP_SECP384R1_ENABLED)
Brett Warrene0edc842021-08-17 09:53:13 +01005106 MBEDTLS_SSL_IANA_TLS_GROUP_SECP384R1,
Jaeden Amerod4311042019-06-03 08:27:16 +01005107#endif
Brett Warrene0edc842021-08-17 09:53:13 +01005108 MBEDTLS_SSL_IANA_TLS_GROUP_NONE
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02005109};
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02005110
Ronald Crone68ab4f2022-10-05 12:46:29 +02005111#if defined(MBEDTLS_DEBUG_C) && defined(MBEDTLS_SSL_HANDSHAKE_WITH_CERT_ENABLED)
Jerry Yu909df7b2022-01-22 11:56:27 +08005112/* Function for checking `ssl_preset_*_sig_algs` and `ssl_tls12_preset_*_sig_algs`
Jerry Yu370e1462022-01-25 10:36:53 +08005113 * to make sure there are no duplicated signature algorithm entries. */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02005114MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01005115static int ssl_check_no_sig_alg_duplication(uint16_t *sig_algs)
Jerry Yu1a8b4812022-01-20 17:56:50 +08005116{
5117 size_t i, j;
5118 int ret = 0;
Jerry Yu909df7b2022-01-22 11:56:27 +08005119
Gilles Peskine449bd832023-01-11 14:50:10 +01005120 for (i = 0; sig_algs[i] != MBEDTLS_TLS_SIG_NONE; i++) {
5121 for (j = 0; j < i; j++) {
5122 if (sig_algs[i] != sig_algs[j]) {
Jerry Yuf377d642022-01-25 10:43:59 +08005123 continue;
Gilles Peskine449bd832023-01-11 14:50:10 +01005124 }
5125 mbedtls_printf(" entry(%04x,%" MBEDTLS_PRINTF_SIZET
5126 ") is duplicated at %" MBEDTLS_PRINTF_SIZET "\n",
5127 sig_algs[i], j, i);
Jerry Yuf377d642022-01-25 10:43:59 +08005128 ret = -1;
Jerry Yu1a8b4812022-01-20 17:56:50 +08005129 }
5130 }
Gilles Peskine449bd832023-01-11 14:50:10 +01005131 return ret;
Jerry Yu1a8b4812022-01-20 17:56:50 +08005132}
5133
Ronald Crone68ab4f2022-10-05 12:46:29 +02005134#endif /* MBEDTLS_DEBUG_C && MBEDTLS_SSL_HANDSHAKE_WITH_CERT_ENABLED */
Jerry Yu1a8b4812022-01-20 17:56:50 +08005135
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02005136/*
Tillmann Karras588ad502015-09-25 04:27:22 +02005137 * Load default in mbedtls_ssl_config
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02005138 */
Gilles Peskine449bd832023-01-11 14:50:10 +01005139int mbedtls_ssl_config_defaults(mbedtls_ssl_config *conf,
5140 int endpoint, int transport, int preset)
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02005141{
Manuel Pégourié-Gonnard8b431fb2015-05-11 12:54:52 +02005142#if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_SSL_SRV_C)
Janos Follath865b3eb2019-12-16 11:46:15 +00005143 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnard8b431fb2015-05-11 12:54:52 +02005144#endif
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02005145
Ronald Crone68ab4f2022-10-05 12:46:29 +02005146#if defined(MBEDTLS_DEBUG_C) && defined(MBEDTLS_SSL_HANDSHAKE_WITH_CERT_ENABLED)
Gilles Peskine449bd832023-01-11 14:50:10 +01005147 if (ssl_check_no_sig_alg_duplication(ssl_preset_suiteb_sig_algs)) {
5148 mbedtls_printf("ssl_preset_suiteb_sig_algs has duplicated entries\n");
5149 return MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Jerry Yu1a8b4812022-01-20 17:56:50 +08005150 }
5151
Gilles Peskine449bd832023-01-11 14:50:10 +01005152 if (ssl_check_no_sig_alg_duplication(ssl_preset_default_sig_algs)) {
5153 mbedtls_printf("ssl_preset_default_sig_algs has duplicated entries\n");
5154 return MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Jerry Yu1a8b4812022-01-20 17:56:50 +08005155 }
Jerry Yu909df7b2022-01-22 11:56:27 +08005156
5157#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
Gilles Peskine449bd832023-01-11 14:50:10 +01005158 if (ssl_check_no_sig_alg_duplication(ssl_tls12_preset_suiteb_sig_algs)) {
5159 mbedtls_printf("ssl_tls12_preset_suiteb_sig_algs has duplicated entries\n");
5160 return MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Jerry Yu909df7b2022-01-22 11:56:27 +08005161 }
5162
Gilles Peskine449bd832023-01-11 14:50:10 +01005163 if (ssl_check_no_sig_alg_duplication(ssl_tls12_preset_default_sig_algs)) {
5164 mbedtls_printf("ssl_tls12_preset_default_sig_algs has duplicated entries\n");
5165 return MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Jerry Yu909df7b2022-01-22 11:56:27 +08005166 }
5167#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Ronald Crone68ab4f2022-10-05 12:46:29 +02005168#endif /* MBEDTLS_DEBUG_C && MBEDTLS_SSL_HANDSHAKE_WITH_CERT_ENABLED */
Jerry Yu1a8b4812022-01-20 17:56:50 +08005169
Manuel Pégourié-Gonnard0de074f2015-05-14 12:58:01 +02005170 /* Use the functions here so that they are covered in tests,
5171 * but otherwise access member directly for efficiency */
Gilles Peskine449bd832023-01-11 14:50:10 +01005172 mbedtls_ssl_conf_endpoint(conf, endpoint);
5173 mbedtls_ssl_conf_transport(conf, transport);
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02005174
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02005175 /*
5176 * Things that are common to all presets
5177 */
Manuel Pégourié-Gonnard419d5ae2015-05-04 19:32:36 +02005178#if defined(MBEDTLS_SSL_CLI_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01005179 if (endpoint == MBEDTLS_SSL_IS_CLIENT) {
Manuel Pégourié-Gonnard419d5ae2015-05-04 19:32:36 +02005180 conf->authmode = MBEDTLS_SSL_VERIFY_REQUIRED;
5181#if defined(MBEDTLS_SSL_SESSION_TICKETS)
5182 conf->session_tickets = MBEDTLS_SSL_SESSION_TICKETS_ENABLED;
5183#endif
5184 }
5185#endif
5186
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02005187#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
5188 conf->encrypt_then_mac = MBEDTLS_SSL_ETM_ENABLED;
5189#endif
5190
5191#if defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET)
5192 conf->extended_ms = MBEDTLS_SSL_EXTENDED_MS_ENABLED;
5193#endif
5194
Manuel Pégourié-Gonnarde057d3b2015-05-20 10:59:43 +02005195#if defined(MBEDTLS_SSL_DTLS_HELLO_VERIFY) && defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02005196 conf->f_cookie_write = ssl_cookie_write_dummy;
5197 conf->f_cookie_check = ssl_cookie_check_dummy;
5198#endif
5199
5200#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
5201 conf->anti_replay = MBEDTLS_SSL_ANTI_REPLAY_ENABLED;
5202#endif
5203
Janos Follath088ce432017-04-10 12:42:31 +01005204#if defined(MBEDTLS_SSL_SRV_C)
5205 conf->cert_req_ca_list = MBEDTLS_SSL_CERT_REQ_CA_LIST_ENABLED;
TRodziewicz3946f792021-06-14 12:11:18 +02005206 conf->respect_cli_pref = MBEDTLS_SSL_SRV_CIPHERSUITE_ORDER_SERVER;
Janos Follath088ce432017-04-10 12:42:31 +01005207#endif
5208
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02005209#if defined(MBEDTLS_SSL_PROTO_DTLS)
5210 conf->hs_timeout_min = MBEDTLS_SSL_DTLS_TIMEOUT_DFL_MIN;
5211 conf->hs_timeout_max = MBEDTLS_SSL_DTLS_TIMEOUT_DFL_MAX;
5212#endif
5213
5214#if defined(MBEDTLS_SSL_RENEGOTIATION)
5215 conf->renego_max_records = MBEDTLS_SSL_RENEGO_MAX_RECORDS_DEFAULT;
Gilles Peskine449bd832023-01-11 14:50:10 +01005216 memset(conf->renego_period, 0x00, 2);
5217 memset(conf->renego_period + 2, 0xFF, 6);
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02005218#endif
5219
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02005220#if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_SSL_SRV_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01005221 if (endpoint == MBEDTLS_SSL_IS_SERVER) {
Hanno Beckere2defad2021-07-24 05:59:17 +01005222 const unsigned char dhm_p[] =
5223 MBEDTLS_DHM_RFC3526_MODP_2048_P_BIN;
5224 const unsigned char dhm_g[] =
5225 MBEDTLS_DHM_RFC3526_MODP_2048_G_BIN;
Hanno Becker00d0a682017-10-04 13:14:29 +01005226
Gilles Peskine449bd832023-01-11 14:50:10 +01005227 if ((ret = mbedtls_ssl_conf_dh_param_bin(conf,
5228 dhm_p, sizeof(dhm_p),
5229 dhm_g, sizeof(dhm_g))) != 0) {
5230 return ret;
Hanno Beckere2defad2021-07-24 05:59:17 +01005231 }
5232 }
Manuel Pégourié-Gonnardbd990d62015-06-11 14:49:42 +02005233#endif
5234
Ronald Cron6f135e12021-12-08 16:57:54 +01005235#if defined(MBEDTLS_SSL_PROTO_TLS1_3)
Jerry Yu6ee56aa2022-12-06 17:47:22 +08005236
5237#if defined(MBEDTLS_SSL_EARLY_DATA)
Gilles Peskine449bd832023-01-11 14:50:10 +01005238 mbedtls_ssl_tls13_conf_early_data(conf, MBEDTLS_SSL_EARLY_DATA_DISABLED);
Jerry Yu6ee56aa2022-12-06 17:47:22 +08005239#if defined(MBEDTLS_SSL_SRV_C)
5240 mbedtls_ssl_tls13_conf_max_early_data_size(
Gilles Peskine449bd832023-01-11 14:50:10 +01005241 conf, MBEDTLS_SSL_MAX_EARLY_DATA_SIZE);
Jerry Yu6ee56aa2022-12-06 17:47:22 +08005242#endif
5243#endif /* MBEDTLS_SSL_EARLY_DATA */
5244
Jerry Yud0766ec2022-09-22 10:46:57 +08005245#if defined(MBEDTLS_SSL_SRV_C) && defined(MBEDTLS_SSL_SESSION_TICKETS)
Jerry Yu1ad7ace2022-08-09 13:28:39 +08005246 mbedtls_ssl_conf_new_session_tickets(
Gilles Peskine449bd832023-01-11 14:50:10 +01005247 conf, MBEDTLS_SSL_TLS1_3_DEFAULT_NEW_SESSION_TICKETS);
Jerry Yu1ad7ace2022-08-09 13:28:39 +08005248#endif
Hanno Becker71f1ed62021-07-24 06:01:47 +01005249 /*
5250 * Allow all TLS 1.3 key exchange modes by default.
5251 */
Xiaofei Bai746f9482021-11-12 08:53:56 +00005252 conf->tls13_kex_modes = MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_ALL;
Ronald Cron6f135e12021-12-08 16:57:54 +01005253#endif /* MBEDTLS_SSL_PROTO_TLS1_3 */
Hanno Becker71f1ed62021-07-24 06:01:47 +01005254
Gilles Peskine449bd832023-01-11 14:50:10 +01005255 if (transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM) {
Glenn Strauss2dfcea22022-03-14 17:26:42 -04005256#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
XiaokangQian4d3a6042022-04-21 13:46:17 +00005257 conf->min_tls_version = MBEDTLS_SSL_VERSION_TLS1_2;
XiaokangQian060d8672022-04-21 09:24:56 +00005258 conf->max_tls_version = MBEDTLS_SSL_VERSION_TLS1_2;
XiaokangQian4d3a6042022-04-21 13:46:17 +00005259#else
Gilles Peskine449bd832023-01-11 14:50:10 +01005260 return MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
XiaokangQian060d8672022-04-21 09:24:56 +00005261#endif
Gilles Peskine449bd832023-01-11 14:50:10 +01005262 } else {
XiaokangQian4d3a6042022-04-21 13:46:17 +00005263#if defined(MBEDTLS_SSL_PROTO_TLS1_2) && defined(MBEDTLS_SSL_PROTO_TLS1_3)
Gilles Peskine449bd832023-01-11 14:50:10 +01005264 if (endpoint == MBEDTLS_SSL_IS_CLIENT) {
XiaokangQian4d3a6042022-04-21 13:46:17 +00005265 conf->min_tls_version = MBEDTLS_SSL_VERSION_TLS1_2;
5266 conf->max_tls_version = MBEDTLS_SSL_VERSION_TLS1_3;
Gilles Peskine449bd832023-01-11 14:50:10 +01005267 } else {
5268 /* Hybrid TLS 1.2 / 1.3 is not supported on server side yet */
XiaokangQian4d3a6042022-04-21 13:46:17 +00005269 conf->min_tls_version = MBEDTLS_SSL_VERSION_TLS1_2;
5270 conf->max_tls_version = MBEDTLS_SSL_VERSION_TLS1_2;
5271 }
5272#elif defined(MBEDTLS_SSL_PROTO_TLS1_3)
5273 conf->min_tls_version = MBEDTLS_SSL_VERSION_TLS1_3;
5274 conf->max_tls_version = MBEDTLS_SSL_VERSION_TLS1_3;
5275#elif defined(MBEDTLS_SSL_PROTO_TLS1_2)
5276 conf->min_tls_version = MBEDTLS_SSL_VERSION_TLS1_2;
5277 conf->max_tls_version = MBEDTLS_SSL_VERSION_TLS1_2;
5278#else
Gilles Peskine449bd832023-01-11 14:50:10 +01005279 return MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
XiaokangQian4d3a6042022-04-21 13:46:17 +00005280#endif
5281 }
Glenn Strauss2dfcea22022-03-14 17:26:42 -04005282
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02005283 /*
5284 * Preset-specific defaults
5285 */
Gilles Peskine449bd832023-01-11 14:50:10 +01005286 switch (preset) {
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02005287 /*
5288 * NSA Suite B
5289 */
5290 case MBEDTLS_SSL_PRESET_SUITEB:
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02005291
Hanno Beckerd60b6c62021-04-29 12:04:11 +01005292 conf->ciphersuite_list = ssl_preset_suiteb_ciphersuites;
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02005293
5294#if defined(MBEDTLS_X509_CRT_PARSE_C)
5295 conf->cert_profile = &mbedtls_x509_crt_profile_suiteb;
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02005296#endif
5297
Ronald Crone68ab4f2022-10-05 12:46:29 +02005298#if defined(MBEDTLS_SSL_HANDSHAKE_WITH_CERT_ENABLED)
Jerry Yu909df7b2022-01-22 11:56:27 +08005299#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
Gilles Peskine449bd832023-01-11 14:50:10 +01005300 if (mbedtls_ssl_conf_is_tls12_only(conf)) {
Jerry Yu909df7b2022-01-22 11:56:27 +08005301 conf->sig_algs = ssl_tls12_preset_suiteb_sig_algs;
Gilles Peskine449bd832023-01-11 14:50:10 +01005302 } else
Jerry Yu909df7b2022-01-22 11:56:27 +08005303#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Gilles Peskine449bd832023-01-11 14:50:10 +01005304 conf->sig_algs = ssl_preset_suiteb_sig_algs;
Ronald Crone68ab4f2022-10-05 12:46:29 +02005305#endif /* MBEDTLS_SSL_HANDSHAKE_WITH_CERT_ENABLED */
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02005306
Brett Warrene0edc842021-08-17 09:53:13 +01005307#if defined(MBEDTLS_ECP_C) && !defined(MBEDTLS_DEPRECATED_REMOVED)
5308 conf->curve_list = NULL;
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02005309#endif
Brett Warrene0edc842021-08-17 09:53:13 +01005310 conf->group_list = ssl_preset_suiteb_groups;
Manuel Pégourié-Gonnardc98204e2015-08-11 04:21:01 +02005311 break;
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02005312
5313 /*
5314 * Default
5315 */
5316 default:
Ronald Cronf6606552022-03-15 11:23:25 +01005317
Hanno Beckerd60b6c62021-04-29 12:04:11 +01005318 conf->ciphersuite_list = mbedtls_ssl_list_ciphersuites();
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02005319
5320#if defined(MBEDTLS_X509_CRT_PARSE_C)
5321 conf->cert_profile = &mbedtls_x509_crt_profile_default;
5322#endif
5323
Ronald Crone68ab4f2022-10-05 12:46:29 +02005324#if defined(MBEDTLS_SSL_HANDSHAKE_WITH_CERT_ENABLED)
Jerry Yu909df7b2022-01-22 11:56:27 +08005325#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
Gilles Peskine449bd832023-01-11 14:50:10 +01005326 if (mbedtls_ssl_conf_is_tls12_only(conf)) {
Jerry Yu909df7b2022-01-22 11:56:27 +08005327 conf->sig_algs = ssl_tls12_preset_default_sig_algs;
Gilles Peskine449bd832023-01-11 14:50:10 +01005328 } else
Jerry Yu909df7b2022-01-22 11:56:27 +08005329#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Gilles Peskine449bd832023-01-11 14:50:10 +01005330 conf->sig_algs = ssl_preset_default_sig_algs;
Ronald Crone68ab4f2022-10-05 12:46:29 +02005331#endif /* MBEDTLS_SSL_HANDSHAKE_WITH_CERT_ENABLED */
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02005332
Brett Warrene0edc842021-08-17 09:53:13 +01005333#if defined(MBEDTLS_ECP_C) && !defined(MBEDTLS_DEPRECATED_REMOVED)
5334 conf->curve_list = NULL;
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02005335#endif
Brett Warrene0edc842021-08-17 09:53:13 +01005336 conf->group_list = ssl_preset_default_groups;
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02005337
5338#if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_SSL_CLI_C)
5339 conf->dhm_min_bitlen = 1024;
5340#endif
5341 }
5342
Gilles Peskine449bd832023-01-11 14:50:10 +01005343 return 0;
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02005344}
5345
5346/*
5347 * Free mbedtls_ssl_config
5348 */
Gilles Peskine449bd832023-01-11 14:50:10 +01005349void mbedtls_ssl_config_free(mbedtls_ssl_config *conf)
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02005350{
5351#if defined(MBEDTLS_DHM_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01005352 mbedtls_mpi_free(&conf->dhm_P);
5353 mbedtls_mpi_free(&conf->dhm_G);
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02005354#endif
5355
Ronald Cron73fe8df2022-10-05 14:31:43 +02005356#if defined(MBEDTLS_SSL_HANDSHAKE_WITH_PSK_ENABLED)
Neil Armstrong501c9322022-05-03 09:35:09 +02005357#if defined(MBEDTLS_USE_PSA_CRYPTO)
Gilles Peskine449bd832023-01-11 14:50:10 +01005358 if (!mbedtls_svc_key_id_is_null(conf->psk_opaque)) {
Neil Armstrong501c9322022-05-03 09:35:09 +02005359 conf->psk_opaque = MBEDTLS_SVC_KEY_ID_INIT;
5360 }
Neil Armstrong8ecd6682022-05-05 11:40:35 +02005361#endif /* MBEDTLS_USE_PSA_CRYPTO */
Gilles Peskine449bd832023-01-11 14:50:10 +01005362 if (conf->psk != NULL) {
5363 mbedtls_platform_zeroize(conf->psk, conf->psk_len);
5364 mbedtls_free(conf->psk);
Azim Khan27e8a122018-03-21 14:24:11 +00005365 conf->psk = NULL;
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02005366 conf->psk_len = 0;
junyeonLEE316b1622017-12-20 16:29:30 +09005367 }
5368
Gilles Peskine449bd832023-01-11 14:50:10 +01005369 if (conf->psk_identity != NULL) {
5370 mbedtls_platform_zeroize(conf->psk_identity, conf->psk_identity_len);
5371 mbedtls_free(conf->psk_identity);
Azim Khan27e8a122018-03-21 14:24:11 +00005372 conf->psk_identity = NULL;
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02005373 conf->psk_identity_len = 0;
5374 }
Ronald Cron73fe8df2022-10-05 14:31:43 +02005375#endif /* MBEDTLS_SSL_HANDSHAKE_WITH_PSK_ENABLED */
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02005376
5377#if defined(MBEDTLS_X509_CRT_PARSE_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01005378 ssl_key_cert_free(conf->key_cert);
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02005379#endif
5380
Gilles Peskine449bd832023-01-11 14:50:10 +01005381 mbedtls_platform_zeroize(conf, sizeof(mbedtls_ssl_config));
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02005382}
5383
Manuel Pégourié-Gonnard5674a972015-10-19 15:14:03 +02005384#if defined(MBEDTLS_PK_C) && \
Gilles Peskine449bd832023-01-11 14:50:10 +01005385 (defined(MBEDTLS_RSA_C) || defined(MBEDTLS_ECDSA_C))
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02005386/*
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005387 * Convert between MBEDTLS_PK_XXX and SSL_SIG_XXX
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02005388 */
Gilles Peskine449bd832023-01-11 14:50:10 +01005389unsigned char mbedtls_ssl_sig_from_pk(mbedtls_pk_context *pk)
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02005390{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005391#if defined(MBEDTLS_RSA_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01005392 if (mbedtls_pk_can_do(pk, MBEDTLS_PK_RSA)) {
5393 return MBEDTLS_SSL_SIG_RSA;
5394 }
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02005395#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005396#if defined(MBEDTLS_ECDSA_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01005397 if (mbedtls_pk_can_do(pk, MBEDTLS_PK_ECDSA)) {
5398 return MBEDTLS_SSL_SIG_ECDSA;
5399 }
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02005400#endif
Gilles Peskine449bd832023-01-11 14:50:10 +01005401 return MBEDTLS_SSL_SIG_ANON;
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02005402}
5403
Gilles Peskine449bd832023-01-11 14:50:10 +01005404unsigned char mbedtls_ssl_sig_from_pk_alg(mbedtls_pk_type_t type)
Hanno Becker7e5437a2017-04-28 17:15:26 +01005405{
Gilles Peskine449bd832023-01-11 14:50:10 +01005406 switch (type) {
Hanno Becker7e5437a2017-04-28 17:15:26 +01005407 case MBEDTLS_PK_RSA:
Gilles Peskine449bd832023-01-11 14:50:10 +01005408 return MBEDTLS_SSL_SIG_RSA;
Hanno Becker7e5437a2017-04-28 17:15:26 +01005409 case MBEDTLS_PK_ECDSA:
5410 case MBEDTLS_PK_ECKEY:
Gilles Peskine449bd832023-01-11 14:50:10 +01005411 return MBEDTLS_SSL_SIG_ECDSA;
Hanno Becker7e5437a2017-04-28 17:15:26 +01005412 default:
Gilles Peskine449bd832023-01-11 14:50:10 +01005413 return MBEDTLS_SSL_SIG_ANON;
Hanno Becker7e5437a2017-04-28 17:15:26 +01005414 }
5415}
5416
Gilles Peskine449bd832023-01-11 14:50:10 +01005417mbedtls_pk_type_t mbedtls_ssl_pk_alg_from_sig(unsigned char sig)
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02005418{
Gilles Peskine449bd832023-01-11 14:50:10 +01005419 switch (sig) {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005420#if defined(MBEDTLS_RSA_C)
5421 case MBEDTLS_SSL_SIG_RSA:
Gilles Peskine449bd832023-01-11 14:50:10 +01005422 return MBEDTLS_PK_RSA;
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02005423#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005424#if defined(MBEDTLS_ECDSA_C)
5425 case MBEDTLS_SSL_SIG_ECDSA:
Gilles Peskine449bd832023-01-11 14:50:10 +01005426 return MBEDTLS_PK_ECDSA;
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02005427#endif
5428 default:
Gilles Peskine449bd832023-01-11 14:50:10 +01005429 return MBEDTLS_PK_NONE;
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02005430 }
5431}
Manuel Pégourié-Gonnard5674a972015-10-19 15:14:03 +02005432#endif /* MBEDTLS_PK_C && ( MBEDTLS_RSA_C || MBEDTLS_ECDSA_C ) */
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02005433
Manuel Pégourié-Gonnard1a483832013-09-20 12:29:15 +02005434/*
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +02005435 * Convert from MBEDTLS_SSL_HASH_XXX to MBEDTLS_MD_XXX
Manuel Pégourié-Gonnard1a483832013-09-20 12:29:15 +02005436 */
Gilles Peskine449bd832023-01-11 14:50:10 +01005437mbedtls_md_type_t mbedtls_ssl_md_alg_from_hash(unsigned char hash)
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02005438{
Gilles Peskine449bd832023-01-11 14:50:10 +01005439 switch (hash) {
Andrzej Kurek25f27152022-08-17 16:09:31 -04005440#if defined(MBEDTLS_HAS_ALG_MD5_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005441 case MBEDTLS_SSL_HASH_MD5:
Gilles Peskine449bd832023-01-11 14:50:10 +01005442 return MBEDTLS_MD_MD5;
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02005443#endif
Andrzej Kurek25f27152022-08-17 16:09:31 -04005444#if defined(MBEDTLS_HAS_ALG_SHA_1_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005445 case MBEDTLS_SSL_HASH_SHA1:
Gilles Peskine449bd832023-01-11 14:50:10 +01005446 return MBEDTLS_MD_SHA1;
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02005447#endif
Andrzej Kurek25f27152022-08-17 16:09:31 -04005448#if defined(MBEDTLS_HAS_ALG_SHA_224_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005449 case MBEDTLS_SSL_HASH_SHA224:
Gilles Peskine449bd832023-01-11 14:50:10 +01005450 return MBEDTLS_MD_SHA224;
Mateusz Starzyke3c48b42021-04-19 16:46:28 +02005451#endif
Andrzej Kurek25f27152022-08-17 16:09:31 -04005452#if defined(MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005453 case MBEDTLS_SSL_HASH_SHA256:
Gilles Peskine449bd832023-01-11 14:50:10 +01005454 return MBEDTLS_MD_SHA256;
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02005455#endif
Andrzej Kurek25f27152022-08-17 16:09:31 -04005456#if defined(MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005457 case MBEDTLS_SSL_HASH_SHA384:
Gilles Peskine449bd832023-01-11 14:50:10 +01005458 return MBEDTLS_MD_SHA384;
Mateusz Starzyk3352a532021-04-06 14:28:22 +02005459#endif
Andrzej Kurek25f27152022-08-17 16:09:31 -04005460#if defined(MBEDTLS_HAS_ALG_SHA_512_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005461 case MBEDTLS_SSL_HASH_SHA512:
Gilles Peskine449bd832023-01-11 14:50:10 +01005462 return MBEDTLS_MD_SHA512;
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02005463#endif
5464 default:
Gilles Peskine449bd832023-01-11 14:50:10 +01005465 return MBEDTLS_MD_NONE;
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02005466 }
5467}
5468
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +02005469/*
5470 * Convert from MBEDTLS_MD_XXX to MBEDTLS_SSL_HASH_XXX
5471 */
Gilles Peskine449bd832023-01-11 14:50:10 +01005472unsigned char mbedtls_ssl_hash_from_md_alg(int md)
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +02005473{
Gilles Peskine449bd832023-01-11 14:50:10 +01005474 switch (md) {
Andrzej Kurek25f27152022-08-17 16:09:31 -04005475#if defined(MBEDTLS_HAS_ALG_MD5_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +02005476 case MBEDTLS_MD_MD5:
Gilles Peskine449bd832023-01-11 14:50:10 +01005477 return MBEDTLS_SSL_HASH_MD5;
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +02005478#endif
Andrzej Kurek25f27152022-08-17 16:09:31 -04005479#if defined(MBEDTLS_HAS_ALG_SHA_1_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +02005480 case MBEDTLS_MD_SHA1:
Gilles Peskine449bd832023-01-11 14:50:10 +01005481 return MBEDTLS_SSL_HASH_SHA1;
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +02005482#endif
Andrzej Kurek25f27152022-08-17 16:09:31 -04005483#if defined(MBEDTLS_HAS_ALG_SHA_224_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +02005484 case MBEDTLS_MD_SHA224:
Gilles Peskine449bd832023-01-11 14:50:10 +01005485 return MBEDTLS_SSL_HASH_SHA224;
Mateusz Starzyke3c48b42021-04-19 16:46:28 +02005486#endif
Andrzej Kurek25f27152022-08-17 16:09:31 -04005487#if defined(MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +02005488 case MBEDTLS_MD_SHA256:
Gilles Peskine449bd832023-01-11 14:50:10 +01005489 return MBEDTLS_SSL_HASH_SHA256;
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +02005490#endif
Andrzej Kurek25f27152022-08-17 16:09:31 -04005491#if defined(MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +02005492 case MBEDTLS_MD_SHA384:
Gilles Peskine449bd832023-01-11 14:50:10 +01005493 return MBEDTLS_SSL_HASH_SHA384;
Mateusz Starzyk3352a532021-04-06 14:28:22 +02005494#endif
Andrzej Kurek25f27152022-08-17 16:09:31 -04005495#if defined(MBEDTLS_HAS_ALG_SHA_512_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +02005496 case MBEDTLS_MD_SHA512:
Gilles Peskine449bd832023-01-11 14:50:10 +01005497 return MBEDTLS_SSL_HASH_SHA512;
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +02005498#endif
5499 default:
Gilles Peskine449bd832023-01-11 14:50:10 +01005500 return MBEDTLS_SSL_HASH_NONE;
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +02005501 }
5502}
5503
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01005504/*
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +02005505 * Check if a curve proposed by the peer is in our list.
Manuel Pégourié-Gonnard9d412d82015-06-17 12:10:46 +02005506 * Return 0 if we're willing to use it, -1 otherwise.
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01005507 */
Gilles Peskine449bd832023-01-11 14:50:10 +01005508int mbedtls_ssl_check_curve_tls_id(const mbedtls_ssl_context *ssl, uint16_t tls_id)
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01005509{
Gilles Peskine449bd832023-01-11 14:50:10 +01005510 const uint16_t *group_list = mbedtls_ssl_get_groups(ssl);
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01005511
Gilles Peskine449bd832023-01-11 14:50:10 +01005512 if (group_list == NULL) {
5513 return -1;
Brett Warrene0edc842021-08-17 09:53:13 +01005514 }
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01005515
Gilles Peskine449bd832023-01-11 14:50:10 +01005516 for (; *group_list != 0; group_list++) {
5517 if (*group_list == tls_id) {
5518 return 0;
5519 }
5520 }
5521
5522 return -1;
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01005523}
Manuel Pégourié-Gonnard0d63b842022-01-18 13:10:56 +01005524
5525#if defined(MBEDTLS_ECP_C)
5526/*
5527 * Same as mbedtls_ssl_check_curve_tls_id() but with a mbedtls_ecp_group_id.
5528 */
Gilles Peskine449bd832023-01-11 14:50:10 +01005529int mbedtls_ssl_check_curve(const mbedtls_ssl_context *ssl, mbedtls_ecp_group_id grp_id)
Manuel Pégourié-Gonnard0d63b842022-01-18 13:10:56 +01005530{
Gilles Peskine449bd832023-01-11 14:50:10 +01005531 uint16_t tls_id = mbedtls_ssl_get_tls_id_from_ecp_group_id(grp_id);
Leonid Rozenboim19e59732022-08-08 16:52:38 -07005532
Gilles Peskine449bd832023-01-11 14:50:10 +01005533 if (tls_id == 0) {
Leonid Rozenboim19e59732022-08-08 16:52:38 -07005534 return -1;
Gilles Peskine449bd832023-01-11 14:50:10 +01005535 }
Leonid Rozenboim19e59732022-08-08 16:52:38 -07005536
Gilles Peskine449bd832023-01-11 14:50:10 +01005537 return mbedtls_ssl_check_curve_tls_id(ssl, tls_id);
Manuel Pégourié-Gonnard0d63b842022-01-18 13:10:56 +01005538}
Manuel Pégourié-Gonnardb541da62015-06-17 11:43:30 +02005539#endif /* MBEDTLS_ECP_C */
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02005540
Gilles Peskine449bd832023-01-11 14:50:10 +01005541#if defined(MBEDTLS_DEBUG_C)
Valerio Setti67419f02023-01-04 16:12:42 +01005542#define EC_NAME(_name_) _name_
5543#else
5544#define EC_NAME(_name_) NULL
5545#endif
5546
Valerio Setti18c9fed2022-12-30 17:44:24 +01005547static const struct {
5548 uint16_t tls_id;
5549 mbedtls_ecp_group_id ecp_group_id;
5550 psa_ecc_family_t psa_family;
5551 uint16_t bits;
Gilles Peskine449bd832023-01-11 14:50:10 +01005552 const char *name;
Valerio Setti18c9fed2022-12-30 17:44:24 +01005553} tls_id_match_table[] =
5554{
5555#if defined(MBEDTLS_ECP_DP_SECP521R1_ENABLED) || defined(PSA_WANT_ECC_SECP_R1_521)
Gilles Peskine449bd832023-01-11 14:50:10 +01005556 { 25, MBEDTLS_ECP_DP_SECP521R1, PSA_ECC_FAMILY_SECP_R1, 521, EC_NAME("secp521r1") },
Valerio Setti18c9fed2022-12-30 17:44:24 +01005557#endif
5558#if defined(MBEDTLS_ECP_DP_BP512R1_ENABLED) || defined(PSA_WANT_ECC_BRAINPOOL_P_R1_512)
Gilles Peskine449bd832023-01-11 14:50:10 +01005559 { 28, MBEDTLS_ECP_DP_BP512R1, PSA_ECC_FAMILY_BRAINPOOL_P_R1, 512, EC_NAME("brainpoolP512r1") },
Valerio Setti18c9fed2022-12-30 17:44:24 +01005560#endif
Valerio Setti67419f02023-01-04 16:12:42 +01005561#if defined(MBEDTLS_ECP_DP_SECP384R1_ENABLED) || defined(PSA_WANT_ECC_SECP_R1_384)
Gilles Peskine449bd832023-01-11 14:50:10 +01005562 { 24, MBEDTLS_ECP_DP_SECP384R1, PSA_ECC_FAMILY_SECP_R1, 384, EC_NAME("secp384r1") },
Valerio Setti18c9fed2022-12-30 17:44:24 +01005563#endif
Valerio Setti67419f02023-01-04 16:12:42 +01005564#if defined(MBEDTLS_ECP_DP_BP384R1_ENABLED) || defined(PSA_WANT_ECC_BRAINPOOL_P_R1_384)
Gilles Peskine449bd832023-01-11 14:50:10 +01005565 { 27, MBEDTLS_ECP_DP_BP384R1, PSA_ECC_FAMILY_BRAINPOOL_P_R1, 384, EC_NAME("brainpoolP384r1") },
Valerio Setti18c9fed2022-12-30 17:44:24 +01005566#endif
Valerio Setti67419f02023-01-04 16:12:42 +01005567#if defined(MBEDTLS_ECP_DP_SECP256R1_ENABLED) || defined(PSA_WANT_ECC_SECP_R1_256)
Gilles Peskine449bd832023-01-11 14:50:10 +01005568 { 23, MBEDTLS_ECP_DP_SECP256R1, PSA_ECC_FAMILY_SECP_R1, 256, EC_NAME("secp256r1") },
Valerio Setti18c9fed2022-12-30 17:44:24 +01005569#endif
5570#if defined(MBEDTLS_ECP_DP_SECP256K1_ENABLED) || defined(PSA_WANT_ECC_SECP_K1_256)
Gilles Peskine449bd832023-01-11 14:50:10 +01005571 { 22, MBEDTLS_ECP_DP_SECP256K1, PSA_ECC_FAMILY_SECP_K1, 256, EC_NAME("secp256k1") },
Valerio Setti18c9fed2022-12-30 17:44:24 +01005572#endif
Valerio Setti67419f02023-01-04 16:12:42 +01005573#if defined(MBEDTLS_ECP_DP_BP256R1_ENABLED) || defined(PSA_WANT_ECC_BRAINPOOL_P_R1_256)
Gilles Peskine449bd832023-01-11 14:50:10 +01005574 { 26, MBEDTLS_ECP_DP_BP256R1, PSA_ECC_FAMILY_BRAINPOOL_P_R1, 256, EC_NAME("brainpoolP256r1") },
Valerio Setti18c9fed2022-12-30 17:44:24 +01005575#endif
5576#if defined(MBEDTLS_ECP_DP_SECP224R1_ENABLED) || defined(PSA_WANT_ECC_SECP_R1_224)
Gilles Peskine449bd832023-01-11 14:50:10 +01005577 { 21, MBEDTLS_ECP_DP_SECP224R1, PSA_ECC_FAMILY_SECP_R1, 224, EC_NAME("secp224r1") },
Valerio Setti18c9fed2022-12-30 17:44:24 +01005578#endif
5579#if defined(MBEDTLS_ECP_DP_SECP224K1_ENABLED) || defined(PSA_WANT_ECC_SECP_K1_224)
Gilles Peskine449bd832023-01-11 14:50:10 +01005580 { 20, MBEDTLS_ECP_DP_SECP224K1, PSA_ECC_FAMILY_SECP_K1, 224, EC_NAME("secp224k1") },
Valerio Setti18c9fed2022-12-30 17:44:24 +01005581#endif
5582#if defined(MBEDTLS_ECP_DP_SECP192R1_ENABLED) || defined(PSA_WANT_ECC_SECP_R1_192)
Gilles Peskine449bd832023-01-11 14:50:10 +01005583 { 19, MBEDTLS_ECP_DP_SECP192R1, PSA_ECC_FAMILY_SECP_R1, 192, EC_NAME("secp192r1") },
Valerio Setti18c9fed2022-12-30 17:44:24 +01005584#endif
5585#if defined(MBEDTLS_ECP_DP_SECP192K1_ENABLED) || defined(PSA_WANT_ECC_SECP_K1_192)
Gilles Peskine449bd832023-01-11 14:50:10 +01005586 { 18, MBEDTLS_ECP_DP_SECP192K1, PSA_ECC_FAMILY_SECP_K1, 192, EC_NAME("secp192k1") },
Valerio Setti18c9fed2022-12-30 17:44:24 +01005587#endif
5588#if defined(MBEDTLS_ECP_DP_CURVE25519_ENABLED) || defined(PSA_WANT_ECC_MONTGOMERY_255)
Gilles Peskine449bd832023-01-11 14:50:10 +01005589 { 29, MBEDTLS_ECP_DP_CURVE25519, PSA_ECC_FAMILY_MONTGOMERY, 255, EC_NAME("x25519") },
Valerio Setti18c9fed2022-12-30 17:44:24 +01005590#endif
5591#if defined(MBEDTLS_ECP_DP_CURVE448_ENABLED) || defined(PSA_WANT_ECC_MONTGOMERY_448)
Gilles Peskine449bd832023-01-11 14:50:10 +01005592 { 30, MBEDTLS_ECP_DP_CURVE448, PSA_ECC_FAMILY_MONTGOMERY, 448, EC_NAME("x448") },
Valerio Setti18c9fed2022-12-30 17:44:24 +01005593#endif
5594 { 0, MBEDTLS_ECP_DP_NONE, 0, 0, NULL },
5595};
5596
Gilles Peskine449bd832023-01-11 14:50:10 +01005597int mbedtls_ssl_get_psa_curve_info_from_tls_id(uint16_t tls_id,
5598 psa_ecc_family_t *family,
5599 size_t *bits)
Valerio Setti18c9fed2022-12-30 17:44:24 +01005600{
Gilles Peskine449bd832023-01-11 14:50:10 +01005601 for (int i = 0; tls_id_match_table[i].tls_id != 0; i++) {
5602 if (tls_id_match_table[i].tls_id == tls_id) {
5603 if (family != NULL) {
Valerio Setti18c9fed2022-12-30 17:44:24 +01005604 *family = tls_id_match_table[i].psa_family;
Gilles Peskine449bd832023-01-11 14:50:10 +01005605 }
5606 if (bits != NULL) {
Valerio Setti18c9fed2022-12-30 17:44:24 +01005607 *bits = tls_id_match_table[i].bits;
Gilles Peskine449bd832023-01-11 14:50:10 +01005608 }
Valerio Setti18c9fed2022-12-30 17:44:24 +01005609 return PSA_SUCCESS;
5610 }
5611 }
5612
5613 return PSA_ERROR_NOT_SUPPORTED;
5614}
5615
Gilles Peskine449bd832023-01-11 14:50:10 +01005616mbedtls_ecp_group_id mbedtls_ssl_get_ecp_group_id_from_tls_id(uint16_t tls_id)
Valerio Setti18c9fed2022-12-30 17:44:24 +01005617{
Gilles Peskine449bd832023-01-11 14:50:10 +01005618 for (int i = 0; tls_id_match_table[i].tls_id != 0; i++) {
5619 if (tls_id_match_table[i].tls_id == tls_id) {
Valerio Setti18c9fed2022-12-30 17:44:24 +01005620 return tls_id_match_table[i].ecp_group_id;
Gilles Peskine449bd832023-01-11 14:50:10 +01005621 }
Valerio Setti18c9fed2022-12-30 17:44:24 +01005622 }
5623
5624 return MBEDTLS_ECP_DP_NONE;
5625}
5626
Gilles Peskine449bd832023-01-11 14:50:10 +01005627uint16_t mbedtls_ssl_get_tls_id_from_ecp_group_id(mbedtls_ecp_group_id grp_id)
Valerio Setti18c9fed2022-12-30 17:44:24 +01005628{
Gilles Peskine449bd832023-01-11 14:50:10 +01005629 for (int i = 0; tls_id_match_table[i].ecp_group_id != MBEDTLS_ECP_DP_NONE;
5630 i++) {
5631 if (tls_id_match_table[i].ecp_group_id == grp_id) {
Valerio Setti18c9fed2022-12-30 17:44:24 +01005632 return tls_id_match_table[i].tls_id;
Gilles Peskine449bd832023-01-11 14:50:10 +01005633 }
Valerio Setti18c9fed2022-12-30 17:44:24 +01005634 }
5635
5636 return 0;
5637}
5638
Valerio Setti67419f02023-01-04 16:12:42 +01005639#if defined(MBEDTLS_DEBUG_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01005640const char *mbedtls_ssl_get_curve_name_from_tls_id(uint16_t tls_id)
Valerio Setti18c9fed2022-12-30 17:44:24 +01005641{
Gilles Peskine449bd832023-01-11 14:50:10 +01005642 for (int i = 0; tls_id_match_table[i].tls_id != 0; i++) {
5643 if (tls_id_match_table[i].tls_id == tls_id) {
Valerio Setti18c9fed2022-12-30 17:44:24 +01005644 return tls_id_match_table[i].name;
Gilles Peskine449bd832023-01-11 14:50:10 +01005645 }
Valerio Setti18c9fed2022-12-30 17:44:24 +01005646 }
5647
5648 return NULL;
5649}
Valerio Setti67419f02023-01-04 16:12:42 +01005650#endif
Valerio Setti18c9fed2022-12-30 17:44:24 +01005651
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005652#if defined(MBEDTLS_X509_CRT_PARSE_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01005653int mbedtls_ssl_check_cert_usage(const mbedtls_x509_crt *cert,
5654 const mbedtls_ssl_ciphersuite_t *ciphersuite,
5655 int cert_endpoint,
5656 uint32_t *flags)
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02005657{
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +01005658 int ret = 0;
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02005659 int usage = 0;
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +02005660 const char *ext_oid;
5661 size_t ext_len;
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +02005662
Gilles Peskine449bd832023-01-11 14:50:10 +01005663 if (cert_endpoint == MBEDTLS_SSL_IS_SERVER) {
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02005664 /* Server part of the key exchange */
Gilles Peskine449bd832023-01-11 14:50:10 +01005665 switch (ciphersuite->key_exchange) {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005666 case MBEDTLS_KEY_EXCHANGE_RSA:
5667 case MBEDTLS_KEY_EXCHANGE_RSA_PSK:
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +01005668 usage = MBEDTLS_X509_KU_KEY_ENCIPHERMENT;
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02005669 break;
5670
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005671 case MBEDTLS_KEY_EXCHANGE_DHE_RSA:
5672 case MBEDTLS_KEY_EXCHANGE_ECDHE_RSA:
5673 case MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA:
5674 usage = MBEDTLS_X509_KU_DIGITAL_SIGNATURE;
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02005675 break;
5676
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005677 case MBEDTLS_KEY_EXCHANGE_ECDH_RSA:
5678 case MBEDTLS_KEY_EXCHANGE_ECDH_ECDSA:
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +01005679 usage = MBEDTLS_X509_KU_KEY_AGREEMENT;
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02005680 break;
5681
5682 /* Don't use default: we want warnings when adding new values */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005683 case MBEDTLS_KEY_EXCHANGE_NONE:
5684 case MBEDTLS_KEY_EXCHANGE_PSK:
5685 case MBEDTLS_KEY_EXCHANGE_DHE_PSK:
5686 case MBEDTLS_KEY_EXCHANGE_ECDHE_PSK:
Manuel Pégourié-Gonnard557535d2015-09-15 17:53:32 +02005687 case MBEDTLS_KEY_EXCHANGE_ECJPAKE:
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02005688 usage = 0;
5689 }
Gilles Peskine449bd832023-01-11 14:50:10 +01005690 } else {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005691 /* Client auth: we only implement rsa_sign and mbedtls_ecdsa_sign for now */
5692 usage = MBEDTLS_X509_KU_DIGITAL_SIGNATURE;
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02005693 }
5694
Gilles Peskine449bd832023-01-11 14:50:10 +01005695 if (mbedtls_x509_crt_check_key_usage(cert, usage) != 0) {
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +01005696 *flags |= MBEDTLS_X509_BADCERT_KEY_USAGE;
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +01005697 ret = -1;
5698 }
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02005699
Gilles Peskine449bd832023-01-11 14:50:10 +01005700 if (cert_endpoint == MBEDTLS_SSL_IS_SERVER) {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005701 ext_oid = MBEDTLS_OID_SERVER_AUTH;
Gilles Peskine449bd832023-01-11 14:50:10 +01005702 ext_len = MBEDTLS_OID_SIZE(MBEDTLS_OID_SERVER_AUTH);
5703 } else {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005704 ext_oid = MBEDTLS_OID_CLIENT_AUTH;
Gilles Peskine449bd832023-01-11 14:50:10 +01005705 ext_len = MBEDTLS_OID_SIZE(MBEDTLS_OID_CLIENT_AUTH);
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +02005706 }
5707
Gilles Peskine449bd832023-01-11 14:50:10 +01005708 if (mbedtls_x509_crt_check_extended_key_usage(cert, ext_oid, ext_len) != 0) {
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +01005709 *flags |= MBEDTLS_X509_BADCERT_EXT_KEY_USAGE;
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +01005710 ret = -1;
5711 }
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +02005712
Gilles Peskine449bd832023-01-11 14:50:10 +01005713 return ret;
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02005714}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005715#endif /* MBEDTLS_X509_CRT_PARSE_C */
Manuel Pégourié-Gonnard3a306b92014-04-29 15:11:17 +02005716
Jerry Yu148165c2021-09-24 23:20:59 +08005717#if defined(MBEDTLS_USE_PSA_CRYPTO)
Gilles Peskine449bd832023-01-11 14:50:10 +01005718int mbedtls_ssl_get_handshake_transcript(mbedtls_ssl_context *ssl,
5719 const mbedtls_md_type_t md,
5720 unsigned char *dst,
5721 size_t dst_len,
5722 size_t *olen)
Jerry Yu148165c2021-09-24 23:20:59 +08005723{
Ronald Cronf6893e12022-01-07 22:09:01 +01005724 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
5725 psa_hash_operation_t *hash_operation_to_clone;
5726 psa_hash_operation_t hash_operation = psa_hash_operation_init();
5727
Jerry Yu148165c2021-09-24 23:20:59 +08005728 *olen = 0;
Ronald Cronf6893e12022-01-07 22:09:01 +01005729
Gilles Peskine449bd832023-01-11 14:50:10 +01005730 switch (md) {
Andrzej Kurek25f27152022-08-17 16:09:31 -04005731#if defined(MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Gilles Peskine449bd832023-01-11 14:50:10 +01005732 case MBEDTLS_MD_SHA384:
5733 hash_operation_to_clone = &ssl->handshake->fin_sha384_psa;
5734 break;
Ronald Cronf6893e12022-01-07 22:09:01 +01005735#endif
5736
Andrzej Kurek25f27152022-08-17 16:09:31 -04005737#if defined(MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Gilles Peskine449bd832023-01-11 14:50:10 +01005738 case MBEDTLS_MD_SHA256:
5739 hash_operation_to_clone = &ssl->handshake->fin_sha256_psa;
5740 break;
Ronald Cronf6893e12022-01-07 22:09:01 +01005741#endif
5742
Gilles Peskine449bd832023-01-11 14:50:10 +01005743 default:
5744 goto exit;
5745 }
5746
5747 status = psa_hash_clone(hash_operation_to_clone, &hash_operation);
5748 if (status != PSA_SUCCESS) {
Ronald Cronf6893e12022-01-07 22:09:01 +01005749 goto exit;
5750 }
5751
Gilles Peskine449bd832023-01-11 14:50:10 +01005752 status = psa_hash_finish(&hash_operation, dst, dst_len, olen);
5753 if (status != PSA_SUCCESS) {
Ronald Cronf6893e12022-01-07 22:09:01 +01005754 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01005755 }
Ronald Cronf6893e12022-01-07 22:09:01 +01005756
5757exit:
Andrzej Kurekeabeb302022-10-17 07:52:51 -04005758#if !defined(MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA) && \
5759 !defined(MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
5760 (void) ssl;
5761#endif
Gilles Peskine449bd832023-01-11 14:50:10 +01005762 return psa_ssl_status_to_mbedtls(status);
Jerry Yu148165c2021-09-24 23:20:59 +08005763}
5764#else /* MBEDTLS_USE_PSA_CRYPTO */
5765
Andrzej Kurek25f27152022-08-17 16:09:31 -04005766#if defined(MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02005767MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01005768static int ssl_get_handshake_transcript_sha384(mbedtls_ssl_context *ssl,
5769 unsigned char *dst,
5770 size_t dst_len,
5771 size_t *olen)
Jerry Yu24c0ec32021-09-09 14:21:07 +08005772{
Jerry Yu24c0ec32021-09-09 14:21:07 +08005773 int ret;
5774 mbedtls_sha512_context sha512;
5775
Gilles Peskine449bd832023-01-11 14:50:10 +01005776 if (dst_len < 48) {
5777 return MBEDTLS_ERR_SSL_INTERNAL_ERROR;
5778 }
Jerry Yu24c0ec32021-09-09 14:21:07 +08005779
Gilles Peskine449bd832023-01-11 14:50:10 +01005780 mbedtls_sha512_init(&sha512);
5781 mbedtls_sha512_clone(&sha512, &ssl->handshake->fin_sha384);
Jerry Yu24c0ec32021-09-09 14:21:07 +08005782
Gilles Peskine449bd832023-01-11 14:50:10 +01005783 if ((ret = mbedtls_sha512_finish(&sha512, dst)) != 0) {
5784 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_sha512_finish", ret);
Jerry Yu24c0ec32021-09-09 14:21:07 +08005785 goto exit;
5786 }
5787
5788 *olen = 48;
5789
5790exit:
5791
Gilles Peskine449bd832023-01-11 14:50:10 +01005792 mbedtls_sha512_free(&sha512);
5793 return ret;
Jerry Yu24c0ec32021-09-09 14:21:07 +08005794}
Andrzej Kurek25f27152022-08-17 16:09:31 -04005795#endif /* MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA */
Jerry Yu24c0ec32021-09-09 14:21:07 +08005796
Andrzej Kurek25f27152022-08-17 16:09:31 -04005797#if defined(MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02005798MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01005799static int ssl_get_handshake_transcript_sha256(mbedtls_ssl_context *ssl,
5800 unsigned char *dst,
5801 size_t dst_len,
5802 size_t *olen)
Jerry Yu24c0ec32021-09-09 14:21:07 +08005803{
Jerry Yu24c0ec32021-09-09 14:21:07 +08005804 int ret;
5805 mbedtls_sha256_context sha256;
5806
Gilles Peskine449bd832023-01-11 14:50:10 +01005807 if (dst_len < 32) {
5808 return MBEDTLS_ERR_SSL_INTERNAL_ERROR;
5809 }
Jerry Yu24c0ec32021-09-09 14:21:07 +08005810
Gilles Peskine449bd832023-01-11 14:50:10 +01005811 mbedtls_sha256_init(&sha256);
5812 mbedtls_sha256_clone(&sha256, &ssl->handshake->fin_sha256);
Jerry Yuc5aef882021-12-23 20:15:02 +08005813
Gilles Peskine449bd832023-01-11 14:50:10 +01005814 if ((ret = mbedtls_sha256_finish(&sha256, dst)) != 0) {
5815 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_sha256_finish", ret);
Jerry Yu24c0ec32021-09-09 14:21:07 +08005816 goto exit;
5817 }
5818
5819 *olen = 32;
5820
5821exit:
5822
Gilles Peskine449bd832023-01-11 14:50:10 +01005823 mbedtls_sha256_free(&sha256);
5824 return ret;
Jerry Yu24c0ec32021-09-09 14:21:07 +08005825}
Andrzej Kurek25f27152022-08-17 16:09:31 -04005826#endif /* MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA */
Jerry Yu24c0ec32021-09-09 14:21:07 +08005827
Gilles Peskine449bd832023-01-11 14:50:10 +01005828int mbedtls_ssl_get_handshake_transcript(mbedtls_ssl_context *ssl,
5829 const mbedtls_md_type_t md,
5830 unsigned char *dst,
5831 size_t dst_len,
5832 size_t *olen)
Jerry Yu24c0ec32021-09-09 14:21:07 +08005833{
Gilles Peskine449bd832023-01-11 14:50:10 +01005834 switch (md) {
Jerry Yuc1ddeef2021-10-08 15:14:45 +08005835
Andrzej Kurek25f27152022-08-17 16:09:31 -04005836#if defined(MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Gilles Peskine449bd832023-01-11 14:50:10 +01005837 case MBEDTLS_MD_SHA384:
5838 return ssl_get_handshake_transcript_sha384(ssl, dst, dst_len, olen);
Andrzej Kurekcccb0442022-08-19 03:42:11 -04005839#endif /* MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA*/
Jerry Yuc1ddeef2021-10-08 15:14:45 +08005840
Andrzej Kurek25f27152022-08-17 16:09:31 -04005841#if defined(MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Gilles Peskine449bd832023-01-11 14:50:10 +01005842 case MBEDTLS_MD_SHA256:
5843 return ssl_get_handshake_transcript_sha256(ssl, dst, dst_len, olen);
Andrzej Kurekcccb0442022-08-19 03:42:11 -04005844#endif /* MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA*/
Jerry Yuc1ddeef2021-10-08 15:14:45 +08005845
Gilles Peskine449bd832023-01-11 14:50:10 +01005846 default:
Andrzej Kurek409248a2022-10-24 10:33:21 -04005847#if !defined(MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA) && \
Gilles Peskine449bd832023-01-11 14:50:10 +01005848 !defined(MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
5849 (void) ssl;
5850 (void) dst;
5851 (void) dst_len;
5852 (void) olen;
Andrzej Kurek409248a2022-10-24 10:33:21 -04005853#endif
Gilles Peskine449bd832023-01-11 14:50:10 +01005854 break;
Jerry Yuc1ddeef2021-10-08 15:14:45 +08005855 }
Gilles Peskine449bd832023-01-11 14:50:10 +01005856 return MBEDTLS_ERR_SSL_INTERNAL_ERROR;
Jerry Yu24c0ec32021-09-09 14:21:07 +08005857}
XiaokangQian647719a2021-12-07 09:16:29 +00005858
Jerry Yu148165c2021-09-24 23:20:59 +08005859#endif /* !MBEDTLS_USE_PSA_CRYPTO */
Jerry Yu24c0ec32021-09-09 14:21:07 +08005860
Ronald Crone68ab4f2022-10-05 12:46:29 +02005861#if defined(MBEDTLS_SSL_HANDSHAKE_WITH_CERT_ENABLED)
Gabor Mezei078e8032022-04-27 21:17:56 +02005862/* mbedtls_ssl_parse_sig_alg_ext()
5863 *
5864 * The `extension_data` field of signature algorithm contains a `SignatureSchemeList`
5865 * value (TLS 1.3 RFC8446):
5866 * enum {
5867 * ....
5868 * ecdsa_secp256r1_sha256( 0x0403 ),
5869 * ecdsa_secp384r1_sha384( 0x0503 ),
5870 * ecdsa_secp521r1_sha512( 0x0603 ),
5871 * ....
5872 * } SignatureScheme;
5873 *
5874 * struct {
5875 * SignatureScheme supported_signature_algorithms<2..2^16-2>;
5876 * } SignatureSchemeList;
5877 *
5878 * The `extension_data` field of signature algorithm contains a `SignatureAndHashAlgorithm`
5879 * value (TLS 1.2 RFC5246):
5880 * enum {
5881 * none(0), md5(1), sha1(2), sha224(3), sha256(4), sha384(5),
5882 * sha512(6), (255)
5883 * } HashAlgorithm;
5884 *
5885 * enum { anonymous(0), rsa(1), dsa(2), ecdsa(3), (255) }
5886 * SignatureAlgorithm;
5887 *
5888 * struct {
5889 * HashAlgorithm hash;
5890 * SignatureAlgorithm signature;
5891 * } SignatureAndHashAlgorithm;
5892 *
5893 * SignatureAndHashAlgorithm
5894 * supported_signature_algorithms<2..2^16-2>;
5895 *
5896 * The TLS 1.3 signature algorithm extension was defined to be a compatible
5897 * generalization of the TLS 1.2 signature algorithm extension.
5898 * `SignatureAndHashAlgorithm` field of TLS 1.2 can be represented by
5899 * `SignatureScheme` field of TLS 1.3
5900 *
5901 */
Gilles Peskine449bd832023-01-11 14:50:10 +01005902int mbedtls_ssl_parse_sig_alg_ext(mbedtls_ssl_context *ssl,
5903 const unsigned char *buf,
5904 const unsigned char *end)
Gabor Mezei078e8032022-04-27 21:17:56 +02005905{
5906 const unsigned char *p = buf;
5907 size_t supported_sig_algs_len = 0;
5908 const unsigned char *supported_sig_algs_end;
5909 uint16_t sig_alg;
5910 uint32_t common_idx = 0;
5911
Gilles Peskine449bd832023-01-11 14:50:10 +01005912 MBEDTLS_SSL_CHK_BUF_READ_PTR(p, end, 2);
5913 supported_sig_algs_len = MBEDTLS_GET_UINT16_BE(p, 0);
Gabor Mezei078e8032022-04-27 21:17:56 +02005914 p += 2;
5915
Gilles Peskine449bd832023-01-11 14:50:10 +01005916 memset(ssl->handshake->received_sig_algs, 0,
5917 sizeof(ssl->handshake->received_sig_algs));
Gabor Mezei078e8032022-04-27 21:17:56 +02005918
Gilles Peskine449bd832023-01-11 14:50:10 +01005919 MBEDTLS_SSL_CHK_BUF_READ_PTR(p, end, supported_sig_algs_len);
Gabor Mezei078e8032022-04-27 21:17:56 +02005920 supported_sig_algs_end = p + supported_sig_algs_len;
Gilles Peskine449bd832023-01-11 14:50:10 +01005921 while (p < supported_sig_algs_end) {
5922 MBEDTLS_SSL_CHK_BUF_READ_PTR(p, supported_sig_algs_end, 2);
5923 sig_alg = MBEDTLS_GET_UINT16_BE(p, 0);
Gabor Mezei078e8032022-04-27 21:17:56 +02005924 p += 2;
Gilles Peskine449bd832023-01-11 14:50:10 +01005925 MBEDTLS_SSL_DEBUG_MSG(4, ("received signature algorithm: 0x%x %s",
5926 sig_alg,
5927 mbedtls_ssl_sig_alg_to_str(sig_alg)));
Jerry Yu2fe6c632022-06-29 10:02:38 +08005928#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
Gilles Peskine449bd832023-01-11 14:50:10 +01005929 if (ssl->tls_version == MBEDTLS_SSL_VERSION_TLS1_2 &&
5930 (!(mbedtls_ssl_sig_alg_is_supported(ssl, sig_alg) &&
5931 mbedtls_ssl_sig_alg_is_offered(ssl, sig_alg)))) {
Gabor Mezei078e8032022-04-27 21:17:56 +02005932 continue;
Jerry Yu2fe6c632022-06-29 10:02:38 +08005933 }
5934#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Gabor Mezei078e8032022-04-27 21:17:56 +02005935
Gilles Peskine449bd832023-01-11 14:50:10 +01005936 MBEDTLS_SSL_DEBUG_MSG(4, ("valid signature algorithm: %s",
5937 mbedtls_ssl_sig_alg_to_str(sig_alg)));
Gabor Mezei078e8032022-04-27 21:17:56 +02005938
Gilles Peskine449bd832023-01-11 14:50:10 +01005939 if (common_idx + 1 < MBEDTLS_RECEIVED_SIG_ALGS_SIZE) {
Gabor Mezei078e8032022-04-27 21:17:56 +02005940 ssl->handshake->received_sig_algs[common_idx] = sig_alg;
5941 common_idx += 1;
5942 }
5943 }
5944 /* Check that we consumed all the message. */
Gilles Peskine449bd832023-01-11 14:50:10 +01005945 if (p != end) {
5946 MBEDTLS_SSL_DEBUG_MSG(1,
5947 ("Signature algorithms extension length misaligned"));
5948 MBEDTLS_SSL_PEND_FATAL_ALERT(MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR,
5949 MBEDTLS_ERR_SSL_DECODE_ERROR);
5950 return MBEDTLS_ERR_SSL_DECODE_ERROR;
Gabor Mezei078e8032022-04-27 21:17:56 +02005951 }
5952
Gilles Peskine449bd832023-01-11 14:50:10 +01005953 if (common_idx == 0) {
5954 MBEDTLS_SSL_DEBUG_MSG(3, ("no signature algorithm in common"));
5955 MBEDTLS_SSL_PEND_FATAL_ALERT(MBEDTLS_SSL_ALERT_MSG_HANDSHAKE_FAILURE,
5956 MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE);
5957 return MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE;
Gabor Mezei078e8032022-04-27 21:17:56 +02005958 }
5959
Gabor Mezei15b95a62022-05-09 16:37:58 +02005960 ssl->handshake->received_sig_algs[common_idx] = MBEDTLS_TLS_SIG_NONE;
Gilles Peskine449bd832023-01-11 14:50:10 +01005961 return 0;
Gabor Mezei078e8032022-04-27 21:17:56 +02005962}
5963
Ronald Crone68ab4f2022-10-05 12:46:29 +02005964#endif /* MBEDTLS_SSL_HANDSHAKE_WITH_CERT_ENABLED */
Gabor Mezei078e8032022-04-27 21:17:56 +02005965
Jerry Yudc7bd172022-02-17 13:44:15 +08005966#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
5967
5968#if defined(MBEDTLS_USE_PSA_CRYPTO)
5969
Gilles Peskine449bd832023-01-11 14:50:10 +01005970static psa_status_t setup_psa_key_derivation(psa_key_derivation_operation_t *derivation,
5971 mbedtls_svc_key_id_t key,
5972 psa_algorithm_t alg,
5973 const unsigned char *raw_psk, size_t raw_psk_length,
5974 const unsigned char *seed, size_t seed_length,
5975 const unsigned char *label, size_t label_length,
5976 const unsigned char *other_secret,
5977 size_t other_secret_length,
5978 size_t capacity)
Jerry Yudc7bd172022-02-17 13:44:15 +08005979{
5980 psa_status_t status;
5981
Gilles Peskine449bd832023-01-11 14:50:10 +01005982 status = psa_key_derivation_setup(derivation, alg);
5983 if (status != PSA_SUCCESS) {
5984 return status;
5985 }
Jerry Yudc7bd172022-02-17 13:44:15 +08005986
Gilles Peskine449bd832023-01-11 14:50:10 +01005987 if (PSA_ALG_IS_TLS12_PRF(alg) || PSA_ALG_IS_TLS12_PSK_TO_MS(alg)) {
5988 status = psa_key_derivation_input_bytes(derivation,
5989 PSA_KEY_DERIVATION_INPUT_SEED,
5990 seed, seed_length);
5991 if (status != PSA_SUCCESS) {
5992 return status;
Przemek Stekiel1f027032022-04-05 17:12:11 +02005993 }
5994
Gilles Peskine449bd832023-01-11 14:50:10 +01005995 if (other_secret != NULL) {
5996 status = psa_key_derivation_input_bytes(derivation,
5997 PSA_KEY_DERIVATION_INPUT_OTHER_SECRET,
5998 other_secret, other_secret_length);
5999 if (status != PSA_SUCCESS) {
6000 return status;
6001 }
6002 }
6003
6004 if (mbedtls_svc_key_id_is_null(key)) {
Jerry Yudc7bd172022-02-17 13:44:15 +08006005 status = psa_key_derivation_input_bytes(
6006 derivation, PSA_KEY_DERIVATION_INPUT_SECRET,
Gilles Peskine449bd832023-01-11 14:50:10 +01006007 raw_psk, raw_psk_length);
6008 } else {
Jerry Yudc7bd172022-02-17 13:44:15 +08006009 status = psa_key_derivation_input_key(
Gilles Peskine449bd832023-01-11 14:50:10 +01006010 derivation, PSA_KEY_DERIVATION_INPUT_SECRET, key);
Jerry Yudc7bd172022-02-17 13:44:15 +08006011 }
Gilles Peskine449bd832023-01-11 14:50:10 +01006012 if (status != PSA_SUCCESS) {
6013 return status;
6014 }
Jerry Yudc7bd172022-02-17 13:44:15 +08006015
Gilles Peskine449bd832023-01-11 14:50:10 +01006016 status = psa_key_derivation_input_bytes(derivation,
6017 PSA_KEY_DERIVATION_INPUT_LABEL,
6018 label, label_length);
6019 if (status != PSA_SUCCESS) {
6020 return status;
6021 }
6022 } else {
6023 return PSA_ERROR_NOT_SUPPORTED;
Jerry Yudc7bd172022-02-17 13:44:15 +08006024 }
6025
Gilles Peskine449bd832023-01-11 14:50:10 +01006026 status = psa_key_derivation_set_capacity(derivation, capacity);
6027 if (status != PSA_SUCCESS) {
6028 return status;
6029 }
Jerry Yudc7bd172022-02-17 13:44:15 +08006030
Gilles Peskine449bd832023-01-11 14:50:10 +01006031 return PSA_SUCCESS;
Jerry Yudc7bd172022-02-17 13:44:15 +08006032}
6033
Andrzej Kurek57d10632022-10-24 10:32:01 -04006034#if defined(PSA_WANT_ALG_SHA_384) || \
6035 defined(PSA_WANT_ALG_SHA_256)
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02006036MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01006037static int tls_prf_generic(mbedtls_md_type_t md_type,
6038 const unsigned char *secret, size_t slen,
6039 const char *label,
6040 const unsigned char *random, size_t rlen,
6041 unsigned char *dstbuf, size_t dlen)
Jerry Yudc7bd172022-02-17 13:44:15 +08006042{
6043 psa_status_t status;
6044 psa_algorithm_t alg;
6045 mbedtls_svc_key_id_t master_key = MBEDTLS_SVC_KEY_ID_INIT;
6046 psa_key_derivation_operation_t derivation =
6047 PSA_KEY_DERIVATION_OPERATION_INIT;
6048
Gilles Peskine449bd832023-01-11 14:50:10 +01006049 if (md_type == MBEDTLS_MD_SHA384) {
Jerry Yudc7bd172022-02-17 13:44:15 +08006050 alg = PSA_ALG_TLS12_PRF(PSA_ALG_SHA_384);
Gilles Peskine449bd832023-01-11 14:50:10 +01006051 } else {
Jerry Yudc7bd172022-02-17 13:44:15 +08006052 alg = PSA_ALG_TLS12_PRF(PSA_ALG_SHA_256);
Gilles Peskine449bd832023-01-11 14:50:10 +01006053 }
Jerry Yudc7bd172022-02-17 13:44:15 +08006054
6055 /* Normally a "secret" should be long enough to be impossible to
6056 * find by brute force, and in particular should not be empty. But
6057 * this PRF is also used to derive an IV, in particular in EAP-TLS,
6058 * and for this use case it makes sense to have a 0-length "secret".
6059 * Since the key API doesn't allow importing a key of length 0,
6060 * keep master_key=0, which setup_psa_key_derivation() understands
6061 * to mean a 0-length "secret" input. */
Gilles Peskine449bd832023-01-11 14:50:10 +01006062 if (slen != 0) {
Jerry Yudc7bd172022-02-17 13:44:15 +08006063 psa_key_attributes_t key_attributes = psa_key_attributes_init();
Gilles Peskine449bd832023-01-11 14:50:10 +01006064 psa_set_key_usage_flags(&key_attributes, PSA_KEY_USAGE_DERIVE);
6065 psa_set_key_algorithm(&key_attributes, alg);
6066 psa_set_key_type(&key_attributes, PSA_KEY_TYPE_DERIVE);
Jerry Yudc7bd172022-02-17 13:44:15 +08006067
Gilles Peskine449bd832023-01-11 14:50:10 +01006068 status = psa_import_key(&key_attributes, secret, slen, &master_key);
6069 if (status != PSA_SUCCESS) {
6070 return MBEDTLS_ERR_SSL_HW_ACCEL_FAILED;
6071 }
Jerry Yudc7bd172022-02-17 13:44:15 +08006072 }
6073
Gilles Peskine449bd832023-01-11 14:50:10 +01006074 status = setup_psa_key_derivation(&derivation,
6075 master_key, alg,
6076 NULL, 0,
6077 random, rlen,
6078 (unsigned char const *) label,
6079 (size_t) strlen(label),
6080 NULL, 0,
6081 dlen);
6082 if (status != PSA_SUCCESS) {
6083 psa_key_derivation_abort(&derivation);
6084 psa_destroy_key(master_key);
6085 return MBEDTLS_ERR_SSL_HW_ACCEL_FAILED;
Jerry Yudc7bd172022-02-17 13:44:15 +08006086 }
6087
Gilles Peskine449bd832023-01-11 14:50:10 +01006088 status = psa_key_derivation_output_bytes(&derivation, dstbuf, dlen);
6089 if (status != PSA_SUCCESS) {
6090 psa_key_derivation_abort(&derivation);
6091 psa_destroy_key(master_key);
6092 return MBEDTLS_ERR_SSL_HW_ACCEL_FAILED;
Jerry Yudc7bd172022-02-17 13:44:15 +08006093 }
6094
Gilles Peskine449bd832023-01-11 14:50:10 +01006095 status = psa_key_derivation_abort(&derivation);
6096 if (status != PSA_SUCCESS) {
6097 psa_destroy_key(master_key);
6098 return MBEDTLS_ERR_SSL_HW_ACCEL_FAILED;
Jerry Yudc7bd172022-02-17 13:44:15 +08006099 }
6100
Gilles Peskine449bd832023-01-11 14:50:10 +01006101 if (!mbedtls_svc_key_id_is_null(master_key)) {
6102 status = psa_destroy_key(master_key);
6103 }
6104 if (status != PSA_SUCCESS) {
6105 return MBEDTLS_ERR_SSL_HW_ACCEL_FAILED;
6106 }
Jerry Yudc7bd172022-02-17 13:44:15 +08006107
Gilles Peskine449bd832023-01-11 14:50:10 +01006108 return 0;
Jerry Yudc7bd172022-02-17 13:44:15 +08006109}
Andrzej Kurek57d10632022-10-24 10:32:01 -04006110#endif /* PSA_WANT_ALG_SHA_256 || PSA_WANT_ALG_SHA_384 */
Jerry Yudc7bd172022-02-17 13:44:15 +08006111#else /* MBEDTLS_USE_PSA_CRYPTO */
6112
Andrzej Kurek57d10632022-10-24 10:32:01 -04006113#if defined(MBEDTLS_MD_C) && \
Gilles Peskine449bd832023-01-11 14:50:10 +01006114 (defined(MBEDTLS_SHA256_C) || \
6115 defined(MBEDTLS_SHA384_C))
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02006116MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01006117static int tls_prf_generic(mbedtls_md_type_t md_type,
6118 const unsigned char *secret, size_t slen,
6119 const char *label,
6120 const unsigned char *random, size_t rlen,
6121 unsigned char *dstbuf, size_t dlen)
Jerry Yudc7bd172022-02-17 13:44:15 +08006122{
6123 size_t nb;
6124 size_t i, j, k, md_len;
6125 unsigned char *tmp;
6126 size_t tmp_len = 0;
6127 unsigned char h_i[MBEDTLS_MD_MAX_SIZE];
6128 const mbedtls_md_info_t *md_info;
6129 mbedtls_md_context_t md_ctx;
6130 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
6131
Gilles Peskine449bd832023-01-11 14:50:10 +01006132 mbedtls_md_init(&md_ctx);
Jerry Yudc7bd172022-02-17 13:44:15 +08006133
Gilles Peskine449bd832023-01-11 14:50:10 +01006134 if ((md_info = mbedtls_md_info_from_type(md_type)) == NULL) {
6135 return MBEDTLS_ERR_SSL_INTERNAL_ERROR;
6136 }
Jerry Yudc7bd172022-02-17 13:44:15 +08006137
Gilles Peskine449bd832023-01-11 14:50:10 +01006138 md_len = mbedtls_md_get_size(md_info);
Jerry Yudc7bd172022-02-17 13:44:15 +08006139
Gilles Peskine449bd832023-01-11 14:50:10 +01006140 tmp_len = md_len + strlen(label) + rlen;
6141 tmp = mbedtls_calloc(1, tmp_len);
6142 if (tmp == NULL) {
Jerry Yudc7bd172022-02-17 13:44:15 +08006143 ret = MBEDTLS_ERR_SSL_ALLOC_FAILED;
6144 goto exit;
6145 }
6146
Gilles Peskine449bd832023-01-11 14:50:10 +01006147 nb = strlen(label);
6148 memcpy(tmp + md_len, label, nb);
6149 memcpy(tmp + md_len + nb, random, rlen);
Jerry Yudc7bd172022-02-17 13:44:15 +08006150 nb += rlen;
6151
6152 /*
6153 * Compute P_<hash>(secret, label + random)[0..dlen]
6154 */
Gilles Peskine449bd832023-01-11 14:50:10 +01006155 if ((ret = mbedtls_md_setup(&md_ctx, md_info, 1)) != 0) {
Jerry Yudc7bd172022-02-17 13:44:15 +08006156 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01006157 }
Jerry Yudc7bd172022-02-17 13:44:15 +08006158
Gilles Peskine449bd832023-01-11 14:50:10 +01006159 ret = mbedtls_md_hmac_starts(&md_ctx, secret, slen);
6160 if (ret != 0) {
Jerry Yudc7bd172022-02-17 13:44:15 +08006161 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01006162 }
6163 ret = mbedtls_md_hmac_update(&md_ctx, tmp + md_len, nb);
6164 if (ret != 0) {
Jerry Yudc7bd172022-02-17 13:44:15 +08006165 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01006166 }
6167 ret = mbedtls_md_hmac_finish(&md_ctx, tmp);
6168 if (ret != 0) {
Jerry Yudc7bd172022-02-17 13:44:15 +08006169 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01006170 }
Jerry Yudc7bd172022-02-17 13:44:15 +08006171
Gilles Peskine449bd832023-01-11 14:50:10 +01006172 for (i = 0; i < dlen; i += md_len) {
6173 ret = mbedtls_md_hmac_reset(&md_ctx);
6174 if (ret != 0) {
Jerry Yudc7bd172022-02-17 13:44:15 +08006175 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01006176 }
6177 ret = mbedtls_md_hmac_update(&md_ctx, tmp, md_len + nb);
6178 if (ret != 0) {
Jerry Yudc7bd172022-02-17 13:44:15 +08006179 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01006180 }
6181 ret = mbedtls_md_hmac_finish(&md_ctx, h_i);
6182 if (ret != 0) {
Jerry Yudc7bd172022-02-17 13:44:15 +08006183 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01006184 }
Jerry Yudc7bd172022-02-17 13:44:15 +08006185
Gilles Peskine449bd832023-01-11 14:50:10 +01006186 ret = mbedtls_md_hmac_reset(&md_ctx);
6187 if (ret != 0) {
Jerry Yudc7bd172022-02-17 13:44:15 +08006188 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01006189 }
6190 ret = mbedtls_md_hmac_update(&md_ctx, tmp, md_len);
6191 if (ret != 0) {
Jerry Yudc7bd172022-02-17 13:44:15 +08006192 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01006193 }
6194 ret = mbedtls_md_hmac_finish(&md_ctx, tmp);
6195 if (ret != 0) {
Jerry Yudc7bd172022-02-17 13:44:15 +08006196 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01006197 }
Jerry Yudc7bd172022-02-17 13:44:15 +08006198
Gilles Peskine449bd832023-01-11 14:50:10 +01006199 k = (i + md_len > dlen) ? dlen % md_len : md_len;
Jerry Yudc7bd172022-02-17 13:44:15 +08006200
Gilles Peskine449bd832023-01-11 14:50:10 +01006201 for (j = 0; j < k; j++) {
Jerry Yudc7bd172022-02-17 13:44:15 +08006202 dstbuf[i + j] = h_i[j];
Gilles Peskine449bd832023-01-11 14:50:10 +01006203 }
Jerry Yudc7bd172022-02-17 13:44:15 +08006204 }
6205
6206exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01006207 mbedtls_md_free(&md_ctx);
Jerry Yudc7bd172022-02-17 13:44:15 +08006208
Gilles Peskine449bd832023-01-11 14:50:10 +01006209 if (tmp != NULL) {
6210 mbedtls_platform_zeroize(tmp, tmp_len);
6211 }
Dave Rodgman29b9b2b2022-11-01 16:08:14 +00006212
Gilles Peskine449bd832023-01-11 14:50:10 +01006213 mbedtls_platform_zeroize(h_i, sizeof(h_i));
Jerry Yudc7bd172022-02-17 13:44:15 +08006214
Gilles Peskine449bd832023-01-11 14:50:10 +01006215 mbedtls_free(tmp);
Jerry Yudc7bd172022-02-17 13:44:15 +08006216
Gilles Peskine449bd832023-01-11 14:50:10 +01006217 return ret;
Jerry Yudc7bd172022-02-17 13:44:15 +08006218}
Andrzej Kurek57d10632022-10-24 10:32:01 -04006219#endif /* MBEDTLS_MD_C && ( MBEDTLS_SHA256_C || MBEDTLS_SHA384_C ) */
Jerry Yudc7bd172022-02-17 13:44:15 +08006220#endif /* MBEDTLS_USE_PSA_CRYPTO */
6221
Andrzej Kurek25f27152022-08-17 16:09:31 -04006222#if defined(MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02006223MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01006224static int tls_prf_sha256(const unsigned char *secret, size_t slen,
6225 const char *label,
6226 const unsigned char *random, size_t rlen,
6227 unsigned char *dstbuf, size_t dlen)
Jerry Yudc7bd172022-02-17 13:44:15 +08006228{
Gilles Peskine449bd832023-01-11 14:50:10 +01006229 return tls_prf_generic(MBEDTLS_MD_SHA256, secret, slen,
6230 label, random, rlen, dstbuf, dlen);
Jerry Yudc7bd172022-02-17 13:44:15 +08006231}
Andrzej Kurekcccb0442022-08-19 03:42:11 -04006232#endif /* MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA*/
Jerry Yudc7bd172022-02-17 13:44:15 +08006233
Andrzej Kurek25f27152022-08-17 16:09:31 -04006234#if defined(MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02006235MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01006236static int tls_prf_sha384(const unsigned char *secret, size_t slen,
6237 const char *label,
6238 const unsigned char *random, size_t rlen,
6239 unsigned char *dstbuf, size_t dlen)
Jerry Yudc7bd172022-02-17 13:44:15 +08006240{
Gilles Peskine449bd832023-01-11 14:50:10 +01006241 return tls_prf_generic(MBEDTLS_MD_SHA384, secret, slen,
6242 label, random, rlen, dstbuf, dlen);
Jerry Yudc7bd172022-02-17 13:44:15 +08006243}
Andrzej Kurekcccb0442022-08-19 03:42:11 -04006244#endif /* MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA*/
Jerry Yudc7bd172022-02-17 13:44:15 +08006245
Jerry Yuf009d862022-02-17 14:01:37 +08006246/*
6247 * Set appropriate PRF function and other SSL / TLS1.2 functions
6248 *
6249 * Inputs:
Jerry Yuf009d862022-02-17 14:01:37 +08006250 * - hash associated with the ciphersuite (only used by TLS 1.2)
6251 *
6252 * Outputs:
6253 * - the tls_prf, calc_verify and calc_finished members of handshake structure
6254 */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02006255MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01006256static int ssl_set_handshake_prfs(mbedtls_ssl_handshake_params *handshake,
6257 mbedtls_md_type_t hash)
Jerry Yuf009d862022-02-17 14:01:37 +08006258{
Andrzej Kurek25f27152022-08-17 16:09:31 -04006259#if defined(MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Gilles Peskine449bd832023-01-11 14:50:10 +01006260 if (hash == MBEDTLS_MD_SHA384) {
Jerry Yuf009d862022-02-17 14:01:37 +08006261 handshake->tls_prf = tls_prf_sha384;
6262 handshake->calc_verify = ssl_calc_verify_tls_sha384;
6263 handshake->calc_finished = ssl_calc_finished_tls_sha384;
Gilles Peskine449bd832023-01-11 14:50:10 +01006264 } else
Jerry Yuf009d862022-02-17 14:01:37 +08006265#endif
Andrzej Kurek25f27152022-08-17 16:09:31 -04006266#if defined(MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Jerry Yuf009d862022-02-17 14:01:37 +08006267 {
Ronald Cron81591aa2022-03-07 09:05:51 +01006268 (void) hash;
Jerry Yuf009d862022-02-17 14:01:37 +08006269 handshake->tls_prf = tls_prf_sha256;
6270 handshake->calc_verify = ssl_calc_verify_tls_sha256;
6271 handshake->calc_finished = ssl_calc_finished_tls_sha256;
6272 }
Ronald Cron81591aa2022-03-07 09:05:51 +01006273#else
Jerry Yuf009d862022-02-17 14:01:37 +08006274 {
Jerry Yuf009d862022-02-17 14:01:37 +08006275 (void) handshake;
Ronald Cron81591aa2022-03-07 09:05:51 +01006276 (void) hash;
Gilles Peskine449bd832023-01-11 14:50:10 +01006277 return MBEDTLS_ERR_SSL_INTERNAL_ERROR;
Jerry Yuf009d862022-02-17 14:01:37 +08006278 }
Ronald Cron81591aa2022-03-07 09:05:51 +01006279#endif
Jerry Yuf009d862022-02-17 14:01:37 +08006280
Gilles Peskine449bd832023-01-11 14:50:10 +01006281 return 0;
Jerry Yuf009d862022-02-17 14:01:37 +08006282}
Jerry Yud6ab2352022-02-17 14:03:43 +08006283
Jerry Yu2a7b5ac2022-02-17 14:07:00 +08006284/*
6285 * Compute master secret if needed
6286 *
6287 * Parameters:
6288 * [in/out] handshake
6289 * [in] resume, premaster, extended_ms, calc_verify, tls_prf
6290 * (PSA-PSK) ciphersuite_info, psk_opaque
6291 * [out] premaster (cleared)
6292 * [out] master
6293 * [in] ssl: optionally used for debugging, EMS and PSA-PSK
6294 * debug: conf->f_dbg, conf->p_dbg
6295 * EMS: passed to calc_verify (debug + session_negotiate)
Ronald Crona25cf582022-03-07 11:10:36 +01006296 * PSA-PSA: conf
Jerry Yu2a7b5ac2022-02-17 14:07:00 +08006297 */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02006298MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01006299static int ssl_compute_master(mbedtls_ssl_handshake_params *handshake,
6300 unsigned char *master,
6301 const mbedtls_ssl_context *ssl)
Jerry Yu2a7b5ac2022-02-17 14:07:00 +08006302{
6303 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
6304
6305 /* cf. RFC 5246, Section 8.1:
6306 * "The master secret is always exactly 48 bytes in length." */
6307 size_t const master_secret_len = 48;
6308
6309#if defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET)
6310 unsigned char session_hash[48];
6311#endif /* MBEDTLS_SSL_EXTENDED_MASTER_SECRET */
6312
6313 /* The label for the KDF used for key expansion.
6314 * This is either "master secret" or "extended master secret"
6315 * depending on whether the Extended Master Secret extension
6316 * is used. */
6317 char const *lbl = "master secret";
6318
Przemek Stekielae4ed302022-04-05 17:15:55 +02006319 /* The seed for the KDF used for key expansion.
Jerry Yu2a7b5ac2022-02-17 14:07:00 +08006320 * - If the Extended Master Secret extension is not used,
6321 * this is ClientHello.Random + ServerHello.Random
6322 * (see Sect. 8.1 in RFC 5246).
6323 * - If the Extended Master Secret extension is used,
6324 * this is the transcript of the handshake so far.
6325 * (see Sect. 4 in RFC 7627). */
Przemek Stekielae4ed302022-04-05 17:15:55 +02006326 unsigned char const *seed = handshake->randbytes;
6327 size_t seed_len = 64;
Jerry Yu2a7b5ac2022-02-17 14:07:00 +08006328
6329#if !defined(MBEDTLS_DEBUG_C) && \
6330 !defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET) && \
6331 !(defined(MBEDTLS_USE_PSA_CRYPTO) && \
Gilles Peskine449bd832023-01-11 14:50:10 +01006332 defined(MBEDTLS_KEY_EXCHANGE_PSK_ENABLED))
Jerry Yu2a7b5ac2022-02-17 14:07:00 +08006333 ssl = NULL; /* make sure we don't use it except for those cases */
6334 (void) ssl;
6335#endif
6336
Gilles Peskine449bd832023-01-11 14:50:10 +01006337 if (handshake->resume != 0) {
6338 MBEDTLS_SSL_DEBUG_MSG(3, ("no premaster (session resumed)"));
6339 return 0;
Jerry Yu2a7b5ac2022-02-17 14:07:00 +08006340 }
6341
6342#if defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET)
Gilles Peskine449bd832023-01-11 14:50:10 +01006343 if (handshake->extended_ms == MBEDTLS_SSL_EXTENDED_MS_ENABLED) {
Jerry Yu2a7b5ac2022-02-17 14:07:00 +08006344 lbl = "extended master secret";
Przemek Stekielae4ed302022-04-05 17:15:55 +02006345 seed = session_hash;
Manuel Pégourié-Gonnardb8b07aa2023-02-06 00:34:21 +01006346 ret = handshake->calc_verify(ssl, session_hash, &seed_len);
6347 if (ret != 0) {
6348 MBEDTLS_SSL_DEBUG_RET(1, "calc_verify", ret);
6349 }
Jerry Yu2a7b5ac2022-02-17 14:07:00 +08006350
Gilles Peskine449bd832023-01-11 14:50:10 +01006351 MBEDTLS_SSL_DEBUG_BUF(3, "session hash for extended master secret",
6352 session_hash, seed_len);
Jerry Yu2a7b5ac2022-02-17 14:07:00 +08006353 }
Przemek Stekiel169bf0b2022-04-29 07:53:29 +02006354#endif /* MBEDTLS_SSL_EXTENDED_MASTER_SECRET */
Jerry Yu2a7b5ac2022-02-17 14:07:00 +08006355
Przemek Stekiel99114f32022-04-22 11:20:09 +02006356#if defined(MBEDTLS_USE_PSA_CRYPTO) && \
Przemek Stekiel8a4b7fd2022-04-28 09:22:22 +02006357 defined(MBEDTLS_KEY_EXCHANGE_SOME_PSK_ENABLED)
Gilles Peskine449bd832023-01-11 14:50:10 +01006358 if (mbedtls_ssl_ciphersuite_uses_psk(handshake->ciphersuite_info) == 1) {
Jerry Yu2a7b5ac2022-02-17 14:07:00 +08006359 /* Perform PSK-to-MS expansion in a single step. */
6360 psa_status_t status;
6361 psa_algorithm_t alg;
6362 mbedtls_svc_key_id_t psk;
6363 psa_key_derivation_operation_t derivation =
6364 PSA_KEY_DERIVATION_OPERATION_INIT;
6365 mbedtls_md_type_t hash_alg = handshake->ciphersuite_info->mac;
6366
Gilles Peskine449bd832023-01-11 14:50:10 +01006367 MBEDTLS_SSL_DEBUG_MSG(2, ("perform PSA-based PSK-to-MS expansion"));
Jerry Yu2a7b5ac2022-02-17 14:07:00 +08006368
Gilles Peskine449bd832023-01-11 14:50:10 +01006369 psk = mbedtls_ssl_get_opaque_psk(ssl);
Jerry Yu2a7b5ac2022-02-17 14:07:00 +08006370
Gilles Peskine449bd832023-01-11 14:50:10 +01006371 if (hash_alg == MBEDTLS_MD_SHA384) {
Jerry Yu2a7b5ac2022-02-17 14:07:00 +08006372 alg = PSA_ALG_TLS12_PSK_TO_MS(PSA_ALG_SHA_384);
Gilles Peskine449bd832023-01-11 14:50:10 +01006373 } else {
Jerry Yu2a7b5ac2022-02-17 14:07:00 +08006374 alg = PSA_ALG_TLS12_PSK_TO_MS(PSA_ALG_SHA_256);
Gilles Peskine449bd832023-01-11 14:50:10 +01006375 }
Jerry Yu2a7b5ac2022-02-17 14:07:00 +08006376
Przemek Stekiel51a1f362022-04-13 08:57:06 +02006377 size_t other_secret_len = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +01006378 unsigned char *other_secret = NULL;
Przemek Stekielc2033402022-04-05 17:19:41 +02006379
Gilles Peskine449bd832023-01-11 14:50:10 +01006380 switch (handshake->ciphersuite_info->key_exchange) {
Przemek Stekiel19b80f82022-04-14 08:29:31 +02006381 /* Provide other secret.
Przemek Stekiel51a1f362022-04-13 08:57:06 +02006382 * Other secret is stored in premaster, where first 2 bytes hold the
Przemek Stekiel19b80f82022-04-14 08:29:31 +02006383 * length of the other key.
Przemek Stekielc2033402022-04-05 17:19:41 +02006384 */
Przemek Stekiel19b80f82022-04-14 08:29:31 +02006385 case MBEDTLS_KEY_EXCHANGE_RSA_PSK:
Przemek Stekiel8abcee92022-04-28 09:16:28 +02006386 /* For RSA-PSK other key length is always 48 bytes. */
Przemek Stekiel19b80f82022-04-14 08:29:31 +02006387 other_secret_len = 48;
6388 other_secret = handshake->premaster + 2;
6389 break;
6390 case MBEDTLS_KEY_EXCHANGE_ECDHE_PSK:
Przemek Stekielb293aaa2022-04-19 12:22:38 +02006391 case MBEDTLS_KEY_EXCHANGE_DHE_PSK:
Przemek Stekiel19b80f82022-04-14 08:29:31 +02006392 other_secret_len = MBEDTLS_GET_UINT16_BE(handshake->premaster, 0);
6393 other_secret = handshake->premaster + 2;
6394 break;
6395 default:
6396 break;
Przemek Stekielc2033402022-04-05 17:19:41 +02006397 }
6398
Gilles Peskine449bd832023-01-11 14:50:10 +01006399 status = setup_psa_key_derivation(&derivation, psk, alg,
6400 ssl->conf->psk, ssl->conf->psk_len,
6401 seed, seed_len,
6402 (unsigned char const *) lbl,
6403 (size_t) strlen(lbl),
6404 other_secret, other_secret_len,
6405 master_secret_len);
6406 if (status != PSA_SUCCESS) {
6407 psa_key_derivation_abort(&derivation);
6408 return MBEDTLS_ERR_SSL_HW_ACCEL_FAILED;
Jerry Yu2a7b5ac2022-02-17 14:07:00 +08006409 }
6410
Gilles Peskine449bd832023-01-11 14:50:10 +01006411 status = psa_key_derivation_output_bytes(&derivation,
6412 master,
6413 master_secret_len);
6414 if (status != PSA_SUCCESS) {
6415 psa_key_derivation_abort(&derivation);
6416 return MBEDTLS_ERR_SSL_HW_ACCEL_FAILED;
Jerry Yu2a7b5ac2022-02-17 14:07:00 +08006417 }
6418
Gilles Peskine449bd832023-01-11 14:50:10 +01006419 status = psa_key_derivation_abort(&derivation);
6420 if (status != PSA_SUCCESS) {
6421 return MBEDTLS_ERR_SSL_HW_ACCEL_FAILED;
6422 }
6423 } else
Jerry Yu2a7b5ac2022-02-17 14:07:00 +08006424#endif
6425 {
Neil Armstrongca7d5062022-05-31 14:43:23 +02006426#if defined(MBEDTLS_USE_PSA_CRYPTO) && \
Gilles Peskine449bd832023-01-11 14:50:10 +01006427 defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
6428 if (handshake->ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECJPAKE) {
Neil Armstrongca7d5062022-05-31 14:43:23 +02006429 psa_status_t status;
6430 psa_algorithm_t alg = PSA_ALG_TLS12_ECJPAKE_TO_PMS;
6431 psa_key_derivation_operation_t derivation =
6432 PSA_KEY_DERIVATION_OPERATION_INIT;
6433
Gilles Peskine449bd832023-01-11 14:50:10 +01006434 MBEDTLS_SSL_DEBUG_MSG(2, ("perform PSA-based PMS KDF for ECJPAKE"));
Neil Armstrongca7d5062022-05-31 14:43:23 +02006435
6436 handshake->pmslen = PSA_TLS12_ECJPAKE_TO_PMS_DATA_SIZE;
6437
Gilles Peskine449bd832023-01-11 14:50:10 +01006438 status = psa_key_derivation_setup(&derivation, alg);
6439 if (status != PSA_SUCCESS) {
6440 return MBEDTLS_ERR_SSL_HW_ACCEL_FAILED;
Neil Armstrongca7d5062022-05-31 14:43:23 +02006441 }
6442
Gilles Peskine449bd832023-01-11 14:50:10 +01006443 status = psa_key_derivation_set_capacity(&derivation,
6444 PSA_TLS12_ECJPAKE_TO_PMS_DATA_SIZE);
6445 if (status != PSA_SUCCESS) {
6446 psa_key_derivation_abort(&derivation);
6447 return MBEDTLS_ERR_SSL_HW_ACCEL_FAILED;
Neil Armstrongca7d5062022-05-31 14:43:23 +02006448 }
6449
Gilles Peskine449bd832023-01-11 14:50:10 +01006450 status = psa_pake_get_implicit_key(&handshake->psa_pake_ctx,
6451 &derivation);
6452 if (status != PSA_SUCCESS) {
6453 psa_key_derivation_abort(&derivation);
6454 return MBEDTLS_ERR_SSL_HW_ACCEL_FAILED;
Neil Armstrongca7d5062022-05-31 14:43:23 +02006455 }
6456
Gilles Peskine449bd832023-01-11 14:50:10 +01006457 status = psa_key_derivation_output_bytes(&derivation,
6458 handshake->premaster,
6459 handshake->pmslen);
6460 if (status != PSA_SUCCESS) {
6461 psa_key_derivation_abort(&derivation);
6462 return MBEDTLS_ERR_SSL_HW_ACCEL_FAILED;
6463 }
6464
6465 status = psa_key_derivation_abort(&derivation);
6466 if (status != PSA_SUCCESS) {
6467 return MBEDTLS_ERR_SSL_HW_ACCEL_FAILED;
Neil Armstrongca7d5062022-05-31 14:43:23 +02006468 }
6469 }
6470#endif
Gilles Peskine449bd832023-01-11 14:50:10 +01006471 ret = handshake->tls_prf(handshake->premaster, handshake->pmslen,
6472 lbl, seed, seed_len,
6473 master,
6474 master_secret_len);
6475 if (ret != 0) {
6476 MBEDTLS_SSL_DEBUG_RET(1, "prf", ret);
6477 return ret;
Jerry Yu2a7b5ac2022-02-17 14:07:00 +08006478 }
6479
Gilles Peskine449bd832023-01-11 14:50:10 +01006480 MBEDTLS_SSL_DEBUG_BUF(3, "premaster secret",
6481 handshake->premaster,
6482 handshake->pmslen);
Jerry Yu2a7b5ac2022-02-17 14:07:00 +08006483
Gilles Peskine449bd832023-01-11 14:50:10 +01006484 mbedtls_platform_zeroize(handshake->premaster,
6485 sizeof(handshake->premaster));
Jerry Yu2a7b5ac2022-02-17 14:07:00 +08006486 }
6487
Gilles Peskine449bd832023-01-11 14:50:10 +01006488 return 0;
Jerry Yu2a7b5ac2022-02-17 14:07:00 +08006489}
6490
Gilles Peskine449bd832023-01-11 14:50:10 +01006491int mbedtls_ssl_derive_keys(mbedtls_ssl_context *ssl)
Jerry Yud62f87e2022-02-17 14:09:02 +08006492{
6493 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
6494 const mbedtls_ssl_ciphersuite_t * const ciphersuite_info =
6495 ssl->handshake->ciphersuite_info;
6496
Gilles Peskine449bd832023-01-11 14:50:10 +01006497 MBEDTLS_SSL_DEBUG_MSG(2, ("=> derive keys"));
Jerry Yud62f87e2022-02-17 14:09:02 +08006498
6499 /* Set PRF, calc_verify and calc_finished function pointers */
Gilles Peskine449bd832023-01-11 14:50:10 +01006500 ret = ssl_set_handshake_prfs(ssl->handshake,
6501 ciphersuite_info->mac);
6502 if (ret != 0) {
6503 MBEDTLS_SSL_DEBUG_RET(1, "ssl_set_handshake_prfs", ret);
6504 return ret;
Jerry Yud62f87e2022-02-17 14:09:02 +08006505 }
6506
6507 /* Compute master secret if needed */
Gilles Peskine449bd832023-01-11 14:50:10 +01006508 ret = ssl_compute_master(ssl->handshake,
6509 ssl->session_negotiate->master,
6510 ssl);
6511 if (ret != 0) {
6512 MBEDTLS_SSL_DEBUG_RET(1, "ssl_compute_master", ret);
6513 return ret;
Jerry Yud62f87e2022-02-17 14:09:02 +08006514 }
6515
6516 /* Swap the client and server random values:
6517 * - MS derivation wanted client+server (RFC 5246 8.1)
6518 * - key derivation wants server+client (RFC 5246 6.3) */
6519 {
6520 unsigned char tmp[64];
Gilles Peskine449bd832023-01-11 14:50:10 +01006521 memcpy(tmp, ssl->handshake->randbytes, 64);
6522 memcpy(ssl->handshake->randbytes, tmp + 32, 32);
6523 memcpy(ssl->handshake->randbytes + 32, tmp, 32);
6524 mbedtls_platform_zeroize(tmp, sizeof(tmp));
Jerry Yud62f87e2022-02-17 14:09:02 +08006525 }
6526
6527 /* Populate transform structure */
Gilles Peskine449bd832023-01-11 14:50:10 +01006528 ret = ssl_tls12_populate_transform(ssl->transform_negotiate,
6529 ssl->session_negotiate->ciphersuite,
6530 ssl->session_negotiate->master,
Neil Armstrongf2c82f02022-04-05 11:16:53 +02006531#if defined(MBEDTLS_SSL_SOME_SUITES_USE_CBC_ETM)
Gilles Peskine449bd832023-01-11 14:50:10 +01006532 ssl->session_negotiate->encrypt_then_mac,
Neil Armstrongf2c82f02022-04-05 11:16:53 +02006533#endif /* MBEDTLS_SSL_SOME_SUITES_USE_CBC_ETM */
Gilles Peskine449bd832023-01-11 14:50:10 +01006534 ssl->handshake->tls_prf,
6535 ssl->handshake->randbytes,
6536 ssl->tls_version,
6537 ssl->conf->endpoint,
6538 ssl);
6539 if (ret != 0) {
6540 MBEDTLS_SSL_DEBUG_RET(1, "ssl_tls12_populate_transform", ret);
6541 return ret;
Jerry Yud62f87e2022-02-17 14:09:02 +08006542 }
6543
6544 /* We no longer need Server/ClientHello.random values */
Gilles Peskine449bd832023-01-11 14:50:10 +01006545 mbedtls_platform_zeroize(ssl->handshake->randbytes,
6546 sizeof(ssl->handshake->randbytes));
Jerry Yud62f87e2022-02-17 14:09:02 +08006547
Gilles Peskine449bd832023-01-11 14:50:10 +01006548 MBEDTLS_SSL_DEBUG_MSG(2, ("<= derive keys"));
Jerry Yud62f87e2022-02-17 14:09:02 +08006549
Gilles Peskine449bd832023-01-11 14:50:10 +01006550 return 0;
Jerry Yud62f87e2022-02-17 14:09:02 +08006551}
Jerry Yu8392e0d2022-02-17 14:10:24 +08006552
Gilles Peskine449bd832023-01-11 14:50:10 +01006553int mbedtls_ssl_set_calc_verify_md(mbedtls_ssl_context *ssl, int md)
Ronald Cron4dcbca92022-03-07 10:21:40 +01006554{
Gilles Peskine449bd832023-01-11 14:50:10 +01006555 switch (md) {
Andrzej Kurek25f27152022-08-17 16:09:31 -04006556#if defined(MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Ronald Cron4dcbca92022-03-07 10:21:40 +01006557 case MBEDTLS_SSL_HASH_SHA384:
6558 ssl->handshake->calc_verify = ssl_calc_verify_tls_sha384;
6559 break;
6560#endif
Andrzej Kurek25f27152022-08-17 16:09:31 -04006561#if defined(MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Ronald Cron4dcbca92022-03-07 10:21:40 +01006562 case MBEDTLS_SSL_HASH_SHA256:
6563 ssl->handshake->calc_verify = ssl_calc_verify_tls_sha256;
6564 break;
6565#endif
6566 default:
Gilles Peskine449bd832023-01-11 14:50:10 +01006567 return -1;
Ronald Cron4dcbca92022-03-07 10:21:40 +01006568 }
Andrzej Kurekeabeb302022-10-17 07:52:51 -04006569#if !defined(MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA) && \
6570 !defined(MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
6571 (void) ssl;
6572#endif
Gilles Peskine449bd832023-01-11 14:50:10 +01006573 return 0;
Ronald Cron4dcbca92022-03-07 10:21:40 +01006574}
6575
Andrzej Kurek25f27152022-08-17 16:09:31 -04006576#if defined(MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Manuel Pégourié-Gonnard226aa152023-02-05 09:46:59 +01006577int ssl_calc_verify_tls_sha256(const mbedtls_ssl_context *ssl,
6578 unsigned char *hash,
6579 size_t *hlen)
Jerry Yu8392e0d2022-02-17 14:10:24 +08006580{
6581#if defined(MBEDTLS_USE_PSA_CRYPTO)
6582 size_t hash_size;
6583 psa_status_t status;
6584 psa_hash_operation_t sha256_psa = psa_hash_operation_init();
6585
Gilles Peskine449bd832023-01-11 14:50:10 +01006586 MBEDTLS_SSL_DEBUG_MSG(2, ("=> PSA calc verify sha256"));
6587 status = psa_hash_clone(&ssl->handshake->fin_sha256_psa, &sha256_psa);
6588 if (status != PSA_SUCCESS) {
Manuel Pégourié-Gonnardb9b564e2023-02-06 10:06:04 +01006589 goto exit;
Jerry Yu8392e0d2022-02-17 14:10:24 +08006590 }
6591
Gilles Peskine449bd832023-01-11 14:50:10 +01006592 status = psa_hash_finish(&sha256_psa, hash, 32, &hash_size);
6593 if (status != PSA_SUCCESS) {
Manuel Pégourié-Gonnardb9b564e2023-02-06 10:06:04 +01006594 goto exit;
Jerry Yu8392e0d2022-02-17 14:10:24 +08006595 }
6596
6597 *hlen = 32;
Gilles Peskine449bd832023-01-11 14:50:10 +01006598 MBEDTLS_SSL_DEBUG_BUF(3, "PSA calculated verify result", hash, *hlen);
6599 MBEDTLS_SSL_DEBUG_MSG(2, ("<= PSA calc verify"));
Manuel Pégourié-Gonnardb9b564e2023-02-06 10:06:04 +01006600
6601exit:
6602 psa_hash_abort(&sha256_psa);
6603 return mbedtls_md_error_from_psa(status);
Jerry Yu8392e0d2022-02-17 14:10:24 +08006604#else
Manuel Pégourié-Gonnardb9b564e2023-02-06 10:06:04 +01006605 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Jerry Yu8392e0d2022-02-17 14:10:24 +08006606 mbedtls_sha256_context sha256;
6607
Gilles Peskine449bd832023-01-11 14:50:10 +01006608 mbedtls_sha256_init(&sha256);
Jerry Yu8392e0d2022-02-17 14:10:24 +08006609
Gilles Peskine449bd832023-01-11 14:50:10 +01006610 MBEDTLS_SSL_DEBUG_MSG(2, ("=> calc verify sha256"));
Jerry Yu8392e0d2022-02-17 14:10:24 +08006611
Gilles Peskine449bd832023-01-11 14:50:10 +01006612 mbedtls_sha256_clone(&sha256, &ssl->handshake->fin_sha256);
Manuel Pégourié-Gonnardb9b564e2023-02-06 10:06:04 +01006613
6614 ret = mbedtls_sha256_finish(&sha256, hash);
6615 if (ret != 0) {
6616 goto exit;
6617 }
Jerry Yu8392e0d2022-02-17 14:10:24 +08006618
6619 *hlen = 32;
6620
Gilles Peskine449bd832023-01-11 14:50:10 +01006621 MBEDTLS_SSL_DEBUG_BUF(3, "calculated verify result", hash, *hlen);
6622 MBEDTLS_SSL_DEBUG_MSG(2, ("<= calc verify"));
Jerry Yu8392e0d2022-02-17 14:10:24 +08006623
Manuel Pégourié-Gonnardb9b564e2023-02-06 10:06:04 +01006624exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01006625 mbedtls_sha256_free(&sha256);
Manuel Pégourié-Gonnard8e176f72023-02-09 10:33:54 +01006626 return ret;
Jerry Yu8392e0d2022-02-17 14:10:24 +08006627#endif /* MBEDTLS_USE_PSA_CRYPTO */
Jerry Yu8392e0d2022-02-17 14:10:24 +08006628}
Andrzej Kurek25f27152022-08-17 16:09:31 -04006629#endif /* MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA */
Jerry Yu8392e0d2022-02-17 14:10:24 +08006630
Andrzej Kurek25f27152022-08-17 16:09:31 -04006631#if defined(MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Manuel Pégourié-Gonnard226aa152023-02-05 09:46:59 +01006632int ssl_calc_verify_tls_sha384(const mbedtls_ssl_context *ssl,
6633 unsigned char *hash,
6634 size_t *hlen)
Jerry Yuc1cb3842022-02-17 14:13:48 +08006635{
6636#if defined(MBEDTLS_USE_PSA_CRYPTO)
6637 size_t hash_size;
6638 psa_status_t status;
6639 psa_hash_operation_t sha384_psa = psa_hash_operation_init();
6640
Gilles Peskine449bd832023-01-11 14:50:10 +01006641 MBEDTLS_SSL_DEBUG_MSG(2, ("=> PSA calc verify sha384"));
6642 status = psa_hash_clone(&ssl->handshake->fin_sha384_psa, &sha384_psa);
6643 if (status != PSA_SUCCESS) {
Manuel Pégourié-Gonnardb9b564e2023-02-06 10:06:04 +01006644 goto exit;
Jerry Yuc1cb3842022-02-17 14:13:48 +08006645 }
6646
Gilles Peskine449bd832023-01-11 14:50:10 +01006647 status = psa_hash_finish(&sha384_psa, hash, 48, &hash_size);
6648 if (status != PSA_SUCCESS) {
Manuel Pégourié-Gonnardb9b564e2023-02-06 10:06:04 +01006649 goto exit;
Jerry Yuc1cb3842022-02-17 14:13:48 +08006650 }
6651
6652 *hlen = 48;
Gilles Peskine449bd832023-01-11 14:50:10 +01006653 MBEDTLS_SSL_DEBUG_BUF(3, "PSA calculated verify result", hash, *hlen);
6654 MBEDTLS_SSL_DEBUG_MSG(2, ("<= PSA calc verify"));
Manuel Pégourié-Gonnardb9b564e2023-02-06 10:06:04 +01006655
6656exit:
6657 psa_hash_abort(&sha384_psa);
6658 return mbedtls_md_error_from_psa(status);
Jerry Yuc1cb3842022-02-17 14:13:48 +08006659#else
Manuel Pégourié-Gonnardb9b564e2023-02-06 10:06:04 +01006660 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Jerry Yuc1cb3842022-02-17 14:13:48 +08006661 mbedtls_sha512_context sha512;
6662
Gilles Peskine449bd832023-01-11 14:50:10 +01006663 mbedtls_sha512_init(&sha512);
Jerry Yuc1cb3842022-02-17 14:13:48 +08006664
Gilles Peskine449bd832023-01-11 14:50:10 +01006665 MBEDTLS_SSL_DEBUG_MSG(2, ("=> calc verify sha384"));
Jerry Yuc1cb3842022-02-17 14:13:48 +08006666
Gilles Peskine449bd832023-01-11 14:50:10 +01006667 mbedtls_sha512_clone(&sha512, &ssl->handshake->fin_sha384);
Manuel Pégourié-Gonnardb9b564e2023-02-06 10:06:04 +01006668
6669 ret = mbedtls_sha512_finish(&sha512, hash);
6670 if (ret != 0) {
6671 goto exit;
6672 }
Jerry Yuc1cb3842022-02-17 14:13:48 +08006673
6674 *hlen = 48;
6675
Gilles Peskine449bd832023-01-11 14:50:10 +01006676 MBEDTLS_SSL_DEBUG_BUF(3, "calculated verify result", hash, *hlen);
6677 MBEDTLS_SSL_DEBUG_MSG(2, ("<= calc verify"));
Jerry Yuc1cb3842022-02-17 14:13:48 +08006678
Manuel Pégourié-Gonnardb9b564e2023-02-06 10:06:04 +01006679exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01006680 mbedtls_sha512_free(&sha512);
Manuel Pégourié-Gonnardb9b564e2023-02-06 10:06:04 +01006681 return ret;
Jerry Yuc1cb3842022-02-17 14:13:48 +08006682#endif /* MBEDTLS_USE_PSA_CRYPTO */
Jerry Yuc1cb3842022-02-17 14:13:48 +08006683}
Andrzej Kurek25f27152022-08-17 16:09:31 -04006684#endif /* MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA */
Jerry Yuc1cb3842022-02-17 14:13:48 +08006685
Neil Armstrong80f6f322022-05-03 17:56:38 +02006686#if !defined(MBEDTLS_USE_PSA_CRYPTO) && \
6687 defined(MBEDTLS_KEY_EXCHANGE_SOME_PSK_ENABLED)
Gilles Peskine449bd832023-01-11 14:50:10 +01006688int mbedtls_ssl_psk_derive_premaster(mbedtls_ssl_context *ssl, mbedtls_key_exchange_type_t key_ex)
Jerry Yuce3dca42022-02-17 14:16:37 +08006689{
6690 unsigned char *p = ssl->handshake->premaster;
Gilles Peskine449bd832023-01-11 14:50:10 +01006691 unsigned char *end = p + sizeof(ssl->handshake->premaster);
Jerry Yuce3dca42022-02-17 14:16:37 +08006692 const unsigned char *psk = NULL;
6693 size_t psk_len = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +01006694 int psk_ret = mbedtls_ssl_get_psk(ssl, &psk, &psk_len);
Jerry Yuce3dca42022-02-17 14:16:37 +08006695
Gilles Peskine449bd832023-01-11 14:50:10 +01006696 if (psk_ret == MBEDTLS_ERR_SSL_PRIVATE_KEY_REQUIRED) {
Jerry Yuce3dca42022-02-17 14:16:37 +08006697 /*
6698 * This should never happen because the existence of a PSK is always
Przemek Stekielb293aaa2022-04-19 12:22:38 +02006699 * checked before calling this function.
6700 *
6701 * The exception is opaque DHE-PSK. For DHE-PSK fill premaster with
Przemek Stekiel8abcee92022-04-28 09:16:28 +02006702 * the shared secret without PSK.
Jerry Yuce3dca42022-02-17 14:16:37 +08006703 */
Gilles Peskine449bd832023-01-11 14:50:10 +01006704 if (key_ex != MBEDTLS_KEY_EXCHANGE_DHE_PSK) {
6705 MBEDTLS_SSL_DEBUG_MSG(1, ("should never happen"));
6706 return MBEDTLS_ERR_SSL_INTERNAL_ERROR;
Przemek Stekielb293aaa2022-04-19 12:22:38 +02006707 }
Jerry Yuce3dca42022-02-17 14:16:37 +08006708 }
6709
6710 /*
6711 * PMS = struct {
6712 * opaque other_secret<0..2^16-1>;
6713 * opaque psk<0..2^16-1>;
6714 * };
6715 * with "other_secret" depending on the particular key exchange
6716 */
6717#if defined(MBEDTLS_KEY_EXCHANGE_PSK_ENABLED)
Gilles Peskine449bd832023-01-11 14:50:10 +01006718 if (key_ex == MBEDTLS_KEY_EXCHANGE_PSK) {
6719 if (end - p < 2) {
6720 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
6721 }
Jerry Yuce3dca42022-02-17 14:16:37 +08006722
Gilles Peskine449bd832023-01-11 14:50:10 +01006723 MBEDTLS_PUT_UINT16_BE(psk_len, p, 0);
Jerry Yuce3dca42022-02-17 14:16:37 +08006724 p += 2;
6725
Gilles Peskine449bd832023-01-11 14:50:10 +01006726 if (end < p || (size_t) (end - p) < psk_len) {
6727 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
6728 }
Jerry Yuce3dca42022-02-17 14:16:37 +08006729
Gilles Peskine449bd832023-01-11 14:50:10 +01006730 memset(p, 0, psk_len);
Jerry Yuce3dca42022-02-17 14:16:37 +08006731 p += psk_len;
Gilles Peskine449bd832023-01-11 14:50:10 +01006732 } else
Jerry Yuce3dca42022-02-17 14:16:37 +08006733#endif /* MBEDTLS_KEY_EXCHANGE_PSK_ENABLED */
6734#if defined(MBEDTLS_KEY_EXCHANGE_RSA_PSK_ENABLED)
Gilles Peskine449bd832023-01-11 14:50:10 +01006735 if (key_ex == MBEDTLS_KEY_EXCHANGE_RSA_PSK) {
Jerry Yuce3dca42022-02-17 14:16:37 +08006736 /*
6737 * other_secret already set by the ClientKeyExchange message,
6738 * and is 48 bytes long
6739 */
Gilles Peskine449bd832023-01-11 14:50:10 +01006740 if (end - p < 2) {
6741 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
6742 }
Jerry Yuce3dca42022-02-17 14:16:37 +08006743
6744 *p++ = 0;
6745 *p++ = 48;
6746 p += 48;
Gilles Peskine449bd832023-01-11 14:50:10 +01006747 } else
Jerry Yuce3dca42022-02-17 14:16:37 +08006748#endif /* MBEDTLS_KEY_EXCHANGE_RSA_PSK_ENABLED */
6749#if defined(MBEDTLS_KEY_EXCHANGE_DHE_PSK_ENABLED)
Gilles Peskine449bd832023-01-11 14:50:10 +01006750 if (key_ex == MBEDTLS_KEY_EXCHANGE_DHE_PSK) {
Jerry Yuce3dca42022-02-17 14:16:37 +08006751 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
6752 size_t len;
6753
6754 /* Write length only when we know the actual value */
Gilles Peskine449bd832023-01-11 14:50:10 +01006755 if ((ret = mbedtls_dhm_calc_secret(&ssl->handshake->dhm_ctx,
6756 p + 2, end - (p + 2), &len,
6757 ssl->conf->f_rng, ssl->conf->p_rng)) != 0) {
6758 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_dhm_calc_secret", ret);
6759 return ret;
Jerry Yuce3dca42022-02-17 14:16:37 +08006760 }
Gilles Peskine449bd832023-01-11 14:50:10 +01006761 MBEDTLS_PUT_UINT16_BE(len, p, 0);
Jerry Yuce3dca42022-02-17 14:16:37 +08006762 p += 2 + len;
6763
Gilles Peskine449bd832023-01-11 14:50:10 +01006764 MBEDTLS_SSL_DEBUG_MPI(3, "DHM: K ", &ssl->handshake->dhm_ctx.K);
6765 } else
Jerry Yuce3dca42022-02-17 14:16:37 +08006766#endif /* MBEDTLS_KEY_EXCHANGE_DHE_PSK_ENABLED */
Neil Armstrong80f6f322022-05-03 17:56:38 +02006767#if defined(MBEDTLS_KEY_EXCHANGE_ECDHE_PSK_ENABLED)
Gilles Peskine449bd832023-01-11 14:50:10 +01006768 if (key_ex == MBEDTLS_KEY_EXCHANGE_ECDHE_PSK) {
Jerry Yuce3dca42022-02-17 14:16:37 +08006769 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
6770 size_t zlen;
6771
Gilles Peskine449bd832023-01-11 14:50:10 +01006772 if ((ret = mbedtls_ecdh_calc_secret(&ssl->handshake->ecdh_ctx, &zlen,
6773 p + 2, end - (p + 2),
6774 ssl->conf->f_rng, ssl->conf->p_rng)) != 0) {
6775 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ecdh_calc_secret", ret);
6776 return ret;
Jerry Yuce3dca42022-02-17 14:16:37 +08006777 }
6778
Gilles Peskine449bd832023-01-11 14:50:10 +01006779 MBEDTLS_PUT_UINT16_BE(zlen, p, 0);
Jerry Yuce3dca42022-02-17 14:16:37 +08006780 p += 2 + zlen;
6781
Gilles Peskine449bd832023-01-11 14:50:10 +01006782 MBEDTLS_SSL_DEBUG_ECDH(3, &ssl->handshake->ecdh_ctx,
6783 MBEDTLS_DEBUG_ECDH_Z);
6784 } else
Neil Armstrong80f6f322022-05-03 17:56:38 +02006785#endif /* MBEDTLS_KEY_EXCHANGE_ECDHE_PSK_ENABLED */
Jerry Yuce3dca42022-02-17 14:16:37 +08006786 {
Gilles Peskine449bd832023-01-11 14:50:10 +01006787 MBEDTLS_SSL_DEBUG_MSG(1, ("should never happen"));
6788 return MBEDTLS_ERR_SSL_INTERNAL_ERROR;
Jerry Yuce3dca42022-02-17 14:16:37 +08006789 }
6790
6791 /* opaque psk<0..2^16-1>; */
Gilles Peskine449bd832023-01-11 14:50:10 +01006792 if (end - p < 2) {
6793 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
6794 }
Jerry Yuce3dca42022-02-17 14:16:37 +08006795
Gilles Peskine449bd832023-01-11 14:50:10 +01006796 MBEDTLS_PUT_UINT16_BE(psk_len, p, 0);
Jerry Yuce3dca42022-02-17 14:16:37 +08006797 p += 2;
6798
Gilles Peskine449bd832023-01-11 14:50:10 +01006799 if (end < p || (size_t) (end - p) < psk_len) {
6800 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
6801 }
Jerry Yuce3dca42022-02-17 14:16:37 +08006802
Gilles Peskine449bd832023-01-11 14:50:10 +01006803 memcpy(p, psk, psk_len);
Jerry Yuce3dca42022-02-17 14:16:37 +08006804 p += psk_len;
6805
6806 ssl->handshake->pmslen = p - ssl->handshake->premaster;
6807
Gilles Peskine449bd832023-01-11 14:50:10 +01006808 return 0;
Jerry Yuce3dca42022-02-17 14:16:37 +08006809}
Neil Armstrong80f6f322022-05-03 17:56:38 +02006810#endif /* !MBEDTLS_USE_PSA_CRYPTO && MBEDTLS_KEY_EXCHANGE_SOME_PSK_ENABLED */
Jerry Yuc2c673d2022-02-17 14:20:39 +08006811
6812#if defined(MBEDTLS_SSL_SRV_C) && defined(MBEDTLS_SSL_RENEGOTIATION)
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02006813MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01006814static int ssl_write_hello_request(mbedtls_ssl_context *ssl);
Jerry Yuc2c673d2022-02-17 14:20:39 +08006815
6816#if defined(MBEDTLS_SSL_PROTO_DTLS)
Gilles Peskine449bd832023-01-11 14:50:10 +01006817int mbedtls_ssl_resend_hello_request(mbedtls_ssl_context *ssl)
Jerry Yuc2c673d2022-02-17 14:20:39 +08006818{
6819 /* If renegotiation is not enforced, retransmit until we would reach max
6820 * timeout if we were using the usual handshake doubling scheme */
Gilles Peskine449bd832023-01-11 14:50:10 +01006821 if (ssl->conf->renego_max_records < 0) {
Jerry Yuc2c673d2022-02-17 14:20:39 +08006822 uint32_t ratio = ssl->conf->hs_timeout_max / ssl->conf->hs_timeout_min + 1;
6823 unsigned char doublings = 1;
6824
Gilles Peskine449bd832023-01-11 14:50:10 +01006825 while (ratio != 0) {
Jerry Yuc2c673d2022-02-17 14:20:39 +08006826 ++doublings;
6827 ratio >>= 1;
6828 }
6829
Gilles Peskine449bd832023-01-11 14:50:10 +01006830 if (++ssl->renego_records_seen > doublings) {
6831 MBEDTLS_SSL_DEBUG_MSG(2, ("no longer retransmitting hello request"));
6832 return 0;
Jerry Yuc2c673d2022-02-17 14:20:39 +08006833 }
6834 }
6835
Gilles Peskine449bd832023-01-11 14:50:10 +01006836 return ssl_write_hello_request(ssl);
Jerry Yuc2c673d2022-02-17 14:20:39 +08006837}
6838#endif
6839#endif /* MBEDTLS_SSL_SRV_C && MBEDTLS_SSL_RENEGOTIATION */
Jerry Yud9526692022-02-17 14:23:47 +08006840
Jerry Yud9526692022-02-17 14:23:47 +08006841/*
6842 * Handshake functions
6843 */
6844#if !defined(MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED)
6845/* No certificate support -> dummy functions */
Gilles Peskine449bd832023-01-11 14:50:10 +01006846int mbedtls_ssl_write_certificate(mbedtls_ssl_context *ssl)
Jerry Yud9526692022-02-17 14:23:47 +08006847{
6848 const mbedtls_ssl_ciphersuite_t *ciphersuite_info =
6849 ssl->handshake->ciphersuite_info;
6850
Gilles Peskine449bd832023-01-11 14:50:10 +01006851 MBEDTLS_SSL_DEBUG_MSG(2, ("=> write certificate"));
Jerry Yud9526692022-02-17 14:23:47 +08006852
Gilles Peskine449bd832023-01-11 14:50:10 +01006853 if (!mbedtls_ssl_ciphersuite_uses_srv_cert(ciphersuite_info)) {
6854 MBEDTLS_SSL_DEBUG_MSG(2, ("<= skip write certificate"));
Jerry Yud9526692022-02-17 14:23:47 +08006855 ssl->state++;
Gilles Peskine449bd832023-01-11 14:50:10 +01006856 return 0;
Jerry Yud9526692022-02-17 14:23:47 +08006857 }
6858
Gilles Peskine449bd832023-01-11 14:50:10 +01006859 MBEDTLS_SSL_DEBUG_MSG(1, ("should never happen"));
6860 return MBEDTLS_ERR_SSL_INTERNAL_ERROR;
Jerry Yud9526692022-02-17 14:23:47 +08006861}
6862
Gilles Peskine449bd832023-01-11 14:50:10 +01006863int mbedtls_ssl_parse_certificate(mbedtls_ssl_context *ssl)
Jerry Yud9526692022-02-17 14:23:47 +08006864{
6865 const mbedtls_ssl_ciphersuite_t *ciphersuite_info =
6866 ssl->handshake->ciphersuite_info;
6867
Gilles Peskine449bd832023-01-11 14:50:10 +01006868 MBEDTLS_SSL_DEBUG_MSG(2, ("=> parse certificate"));
Jerry Yud9526692022-02-17 14:23:47 +08006869
Gilles Peskine449bd832023-01-11 14:50:10 +01006870 if (!mbedtls_ssl_ciphersuite_uses_srv_cert(ciphersuite_info)) {
6871 MBEDTLS_SSL_DEBUG_MSG(2, ("<= skip parse certificate"));
Jerry Yud9526692022-02-17 14:23:47 +08006872 ssl->state++;
Gilles Peskine449bd832023-01-11 14:50:10 +01006873 return 0;
Jerry Yud9526692022-02-17 14:23:47 +08006874 }
6875
Gilles Peskine449bd832023-01-11 14:50:10 +01006876 MBEDTLS_SSL_DEBUG_MSG(1, ("should never happen"));
6877 return MBEDTLS_ERR_SSL_INTERNAL_ERROR;
Jerry Yud9526692022-02-17 14:23:47 +08006878}
6879
6880#else /* MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED */
6881/* Some certificate support -> implement write and parse */
6882
Gilles Peskine449bd832023-01-11 14:50:10 +01006883int mbedtls_ssl_write_certificate(mbedtls_ssl_context *ssl)
Jerry Yud9526692022-02-17 14:23:47 +08006884{
6885 int ret = MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
6886 size_t i, n;
6887 const mbedtls_x509_crt *crt;
6888 const mbedtls_ssl_ciphersuite_t *ciphersuite_info =
6889 ssl->handshake->ciphersuite_info;
6890
Gilles Peskine449bd832023-01-11 14:50:10 +01006891 MBEDTLS_SSL_DEBUG_MSG(2, ("=> write certificate"));
Jerry Yud9526692022-02-17 14:23:47 +08006892
Gilles Peskine449bd832023-01-11 14:50:10 +01006893 if (!mbedtls_ssl_ciphersuite_uses_srv_cert(ciphersuite_info)) {
6894 MBEDTLS_SSL_DEBUG_MSG(2, ("<= skip write certificate"));
Jerry Yud9526692022-02-17 14:23:47 +08006895 ssl->state++;
Gilles Peskine449bd832023-01-11 14:50:10 +01006896 return 0;
Jerry Yud9526692022-02-17 14:23:47 +08006897 }
6898
6899#if defined(MBEDTLS_SSL_CLI_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01006900 if (ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT) {
6901 if (ssl->handshake->client_auth == 0) {
6902 MBEDTLS_SSL_DEBUG_MSG(2, ("<= skip write certificate"));
Jerry Yud9526692022-02-17 14:23:47 +08006903 ssl->state++;
Gilles Peskine449bd832023-01-11 14:50:10 +01006904 return 0;
Jerry Yud9526692022-02-17 14:23:47 +08006905 }
6906 }
6907#endif /* MBEDTLS_SSL_CLI_C */
6908#if defined(MBEDTLS_SSL_SRV_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01006909 if (ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER) {
6910 if (mbedtls_ssl_own_cert(ssl) == NULL) {
Jerry Yud9526692022-02-17 14:23:47 +08006911 /* Should never happen because we shouldn't have picked the
6912 * ciphersuite if we don't have a certificate. */
Gilles Peskine449bd832023-01-11 14:50:10 +01006913 return MBEDTLS_ERR_SSL_INTERNAL_ERROR;
Jerry Yud9526692022-02-17 14:23:47 +08006914 }
6915 }
6916#endif
6917
Gilles Peskine449bd832023-01-11 14:50:10 +01006918 MBEDTLS_SSL_DEBUG_CRT(3, "own certificate", mbedtls_ssl_own_cert(ssl));
Jerry Yud9526692022-02-17 14:23:47 +08006919
6920 /*
6921 * 0 . 0 handshake type
6922 * 1 . 3 handshake length
6923 * 4 . 6 length of all certs
6924 * 7 . 9 length of cert. 1
6925 * 10 . n-1 peer certificate
6926 * n . n+2 length of cert. 2
6927 * n+3 . ... upper level cert, etc.
6928 */
6929 i = 7;
Gilles Peskine449bd832023-01-11 14:50:10 +01006930 crt = mbedtls_ssl_own_cert(ssl);
Jerry Yud9526692022-02-17 14:23:47 +08006931
Gilles Peskine449bd832023-01-11 14:50:10 +01006932 while (crt != NULL) {
Jerry Yud9526692022-02-17 14:23:47 +08006933 n = crt->raw.len;
Gilles Peskine449bd832023-01-11 14:50:10 +01006934 if (n > MBEDTLS_SSL_OUT_CONTENT_LEN - 3 - i) {
6935 MBEDTLS_SSL_DEBUG_MSG(1, ("certificate too large, %" MBEDTLS_PRINTF_SIZET
6936 " > %" MBEDTLS_PRINTF_SIZET,
6937 i + 3 + n, (size_t) MBEDTLS_SSL_OUT_CONTENT_LEN));
6938 return MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL;
Jerry Yud9526692022-02-17 14:23:47 +08006939 }
6940
Gilles Peskine449bd832023-01-11 14:50:10 +01006941 ssl->out_msg[i] = MBEDTLS_BYTE_2(n);
6942 ssl->out_msg[i + 1] = MBEDTLS_BYTE_1(n);
6943 ssl->out_msg[i + 2] = MBEDTLS_BYTE_0(n);
Jerry Yud9526692022-02-17 14:23:47 +08006944
Gilles Peskine449bd832023-01-11 14:50:10 +01006945 i += 3; memcpy(ssl->out_msg + i, crt->raw.p, n);
Jerry Yud9526692022-02-17 14:23:47 +08006946 i += n; crt = crt->next;
6947 }
6948
Gilles Peskine449bd832023-01-11 14:50:10 +01006949 ssl->out_msg[4] = MBEDTLS_BYTE_2(i - 7);
6950 ssl->out_msg[5] = MBEDTLS_BYTE_1(i - 7);
6951 ssl->out_msg[6] = MBEDTLS_BYTE_0(i - 7);
Jerry Yud9526692022-02-17 14:23:47 +08006952
6953 ssl->out_msglen = i;
6954 ssl->out_msgtype = MBEDTLS_SSL_MSG_HANDSHAKE;
6955 ssl->out_msg[0] = MBEDTLS_SSL_HS_CERTIFICATE;
6956
6957 ssl->state++;
6958
Gilles Peskine449bd832023-01-11 14:50:10 +01006959 if ((ret = mbedtls_ssl_write_handshake_msg(ssl)) != 0) {
6960 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ssl_write_handshake_msg", ret);
6961 return ret;
Jerry Yud9526692022-02-17 14:23:47 +08006962 }
6963
Gilles Peskine449bd832023-01-11 14:50:10 +01006964 MBEDTLS_SSL_DEBUG_MSG(2, ("<= write certificate"));
Jerry Yud9526692022-02-17 14:23:47 +08006965
Gilles Peskine449bd832023-01-11 14:50:10 +01006966 return ret;
Jerry Yud9526692022-02-17 14:23:47 +08006967}
6968
6969#if defined(MBEDTLS_SSL_RENEGOTIATION) && defined(MBEDTLS_SSL_CLI_C)
6970
6971#if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02006972MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01006973static int ssl_check_peer_crt_unchanged(mbedtls_ssl_context *ssl,
6974 unsigned char *crt_buf,
6975 size_t crt_buf_len)
Jerry Yud9526692022-02-17 14:23:47 +08006976{
6977 mbedtls_x509_crt const * const peer_crt = ssl->session->peer_cert;
6978
Gilles Peskine449bd832023-01-11 14:50:10 +01006979 if (peer_crt == NULL) {
6980 return -1;
6981 }
Jerry Yud9526692022-02-17 14:23:47 +08006982
Gilles Peskine449bd832023-01-11 14:50:10 +01006983 if (peer_crt->raw.len != crt_buf_len) {
6984 return -1;
6985 }
Jerry Yud9526692022-02-17 14:23:47 +08006986
Gilles Peskine449bd832023-01-11 14:50:10 +01006987 return memcmp(peer_crt->raw.p, crt_buf, peer_crt->raw.len);
Jerry Yud9526692022-02-17 14:23:47 +08006988}
6989#else /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02006990MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01006991static int ssl_check_peer_crt_unchanged(mbedtls_ssl_context *ssl,
6992 unsigned char *crt_buf,
6993 size_t crt_buf_len)
Jerry Yud9526692022-02-17 14:23:47 +08006994{
6995 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
6996 unsigned char const * const peer_cert_digest =
6997 ssl->session->peer_cert_digest;
6998 mbedtls_md_type_t const peer_cert_digest_type =
6999 ssl->session->peer_cert_digest_type;
7000 mbedtls_md_info_t const * const digest_info =
Gilles Peskine449bd832023-01-11 14:50:10 +01007001 mbedtls_md_info_from_type(peer_cert_digest_type);
Jerry Yud9526692022-02-17 14:23:47 +08007002 unsigned char tmp_digest[MBEDTLS_SSL_PEER_CERT_DIGEST_MAX_LEN];
7003 size_t digest_len;
7004
Gilles Peskine449bd832023-01-11 14:50:10 +01007005 if (peer_cert_digest == NULL || digest_info == NULL) {
7006 return -1;
7007 }
Jerry Yud9526692022-02-17 14:23:47 +08007008
Gilles Peskine449bd832023-01-11 14:50:10 +01007009 digest_len = mbedtls_md_get_size(digest_info);
7010 if (digest_len > MBEDTLS_SSL_PEER_CERT_DIGEST_MAX_LEN) {
7011 return -1;
7012 }
Jerry Yud9526692022-02-17 14:23:47 +08007013
Gilles Peskine449bd832023-01-11 14:50:10 +01007014 ret = mbedtls_md(digest_info, crt_buf, crt_buf_len, tmp_digest);
7015 if (ret != 0) {
7016 return -1;
7017 }
Jerry Yud9526692022-02-17 14:23:47 +08007018
Gilles Peskine449bd832023-01-11 14:50:10 +01007019 return memcmp(tmp_digest, peer_cert_digest, digest_len);
Jerry Yud9526692022-02-17 14:23:47 +08007020}
7021#endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
7022#endif /* MBEDTLS_SSL_RENEGOTIATION && MBEDTLS_SSL_CLI_C */
7023
7024/*
7025 * Once the certificate message is read, parse it into a cert chain and
7026 * perform basic checks, but leave actual verification to the caller
7027 */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02007028MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01007029static int ssl_parse_certificate_chain(mbedtls_ssl_context *ssl,
7030 mbedtls_x509_crt *chain)
Jerry Yud9526692022-02-17 14:23:47 +08007031{
7032 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
7033#if defined(MBEDTLS_SSL_RENEGOTIATION) && defined(MBEDTLS_SSL_CLI_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01007034 int crt_cnt = 0;
Jerry Yud9526692022-02-17 14:23:47 +08007035#endif
7036 size_t i, n;
7037 uint8_t alert;
7038
Gilles Peskine449bd832023-01-11 14:50:10 +01007039 if (ssl->in_msgtype != MBEDTLS_SSL_MSG_HANDSHAKE) {
7040 MBEDTLS_SSL_DEBUG_MSG(1, ("bad certificate message"));
7041 mbedtls_ssl_send_alert_message(ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
7042 MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE);
7043 return MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE;
Jerry Yud9526692022-02-17 14:23:47 +08007044 }
7045
Gilles Peskine449bd832023-01-11 14:50:10 +01007046 if (ssl->in_msg[0] != MBEDTLS_SSL_HS_CERTIFICATE) {
7047 mbedtls_ssl_send_alert_message(ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
7048 MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE);
7049 return MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE;
Jerry Yud9526692022-02-17 14:23:47 +08007050 }
7051
Gilles Peskine449bd832023-01-11 14:50:10 +01007052 if (ssl->in_hslen < mbedtls_ssl_hs_hdr_len(ssl) + 3 + 3) {
7053 MBEDTLS_SSL_DEBUG_MSG(1, ("bad certificate message"));
7054 mbedtls_ssl_send_alert_message(ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
7055 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR);
7056 return MBEDTLS_ERR_SSL_DECODE_ERROR;
Jerry Yud9526692022-02-17 14:23:47 +08007057 }
7058
Gilles Peskine449bd832023-01-11 14:50:10 +01007059 i = mbedtls_ssl_hs_hdr_len(ssl);
Jerry Yud9526692022-02-17 14:23:47 +08007060
7061 /*
7062 * Same message structure as in mbedtls_ssl_write_certificate()
7063 */
Gilles Peskine449bd832023-01-11 14:50:10 +01007064 n = (ssl->in_msg[i+1] << 8) | ssl->in_msg[i+2];
Jerry Yud9526692022-02-17 14:23:47 +08007065
Gilles Peskine449bd832023-01-11 14:50:10 +01007066 if (ssl->in_msg[i] != 0 ||
7067 ssl->in_hslen != n + 3 + mbedtls_ssl_hs_hdr_len(ssl)) {
7068 MBEDTLS_SSL_DEBUG_MSG(1, ("bad certificate message"));
7069 mbedtls_ssl_send_alert_message(ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
7070 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR);
7071 return MBEDTLS_ERR_SSL_DECODE_ERROR;
Jerry Yud9526692022-02-17 14:23:47 +08007072 }
7073
7074 /* Make &ssl->in_msg[i] point to the beginning of the CRT chain. */
7075 i += 3;
7076
7077 /* Iterate through and parse the CRTs in the provided chain. */
Gilles Peskine449bd832023-01-11 14:50:10 +01007078 while (i < ssl->in_hslen) {
Jerry Yud9526692022-02-17 14:23:47 +08007079 /* Check that there's room for the next CRT's length fields. */
Gilles Peskine449bd832023-01-11 14:50:10 +01007080 if (i + 3 > ssl->in_hslen) {
7081 MBEDTLS_SSL_DEBUG_MSG(1, ("bad certificate message"));
7082 mbedtls_ssl_send_alert_message(ssl,
7083 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
7084 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR);
7085 return MBEDTLS_ERR_SSL_DECODE_ERROR;
Jerry Yud9526692022-02-17 14:23:47 +08007086 }
7087 /* In theory, the CRT can be up to 2**24 Bytes, but we don't support
7088 * anything beyond 2**16 ~ 64K. */
Gilles Peskine449bd832023-01-11 14:50:10 +01007089 if (ssl->in_msg[i] != 0) {
7090 MBEDTLS_SSL_DEBUG_MSG(1, ("bad certificate message"));
7091 mbedtls_ssl_send_alert_message(ssl,
7092 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
7093 MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT);
7094 return MBEDTLS_ERR_SSL_BAD_CERTIFICATE;
Jerry Yud9526692022-02-17 14:23:47 +08007095 }
7096
7097 /* Read length of the next CRT in the chain. */
Gilles Peskine449bd832023-01-11 14:50:10 +01007098 n = ((unsigned int) ssl->in_msg[i + 1] << 8)
Jerry Yud9526692022-02-17 14:23:47 +08007099 | (unsigned int) ssl->in_msg[i + 2];
7100 i += 3;
7101
Gilles Peskine449bd832023-01-11 14:50:10 +01007102 if (n < 128 || i + n > ssl->in_hslen) {
7103 MBEDTLS_SSL_DEBUG_MSG(1, ("bad certificate message"));
7104 mbedtls_ssl_send_alert_message(ssl,
7105 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
7106 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR);
7107 return MBEDTLS_ERR_SSL_DECODE_ERROR;
Jerry Yud9526692022-02-17 14:23:47 +08007108 }
7109
7110 /* Check if we're handling the first CRT in the chain. */
7111#if defined(MBEDTLS_SSL_RENEGOTIATION) && defined(MBEDTLS_SSL_CLI_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01007112 if (crt_cnt++ == 0 &&
Jerry Yud9526692022-02-17 14:23:47 +08007113 ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT &&
Gilles Peskine449bd832023-01-11 14:50:10 +01007114 ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS) {
Jerry Yud9526692022-02-17 14:23:47 +08007115 /* During client-side renegotiation, check that the server's
7116 * end-CRTs hasn't changed compared to the initial handshake,
7117 * mitigating the triple handshake attack. On success, reuse
7118 * the original end-CRT instead of parsing it again. */
Gilles Peskine449bd832023-01-11 14:50:10 +01007119 MBEDTLS_SSL_DEBUG_MSG(3, ("Check that peer CRT hasn't changed during renegotiation"));
7120 if (ssl_check_peer_crt_unchanged(ssl,
7121 &ssl->in_msg[i],
7122 n) != 0) {
7123 MBEDTLS_SSL_DEBUG_MSG(1, ("new server cert during renegotiation"));
7124 mbedtls_ssl_send_alert_message(ssl,
7125 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
7126 MBEDTLS_SSL_ALERT_MSG_ACCESS_DENIED);
7127 return MBEDTLS_ERR_SSL_BAD_CERTIFICATE;
Jerry Yud9526692022-02-17 14:23:47 +08007128 }
7129
7130 /* Now we can safely free the original chain. */
Gilles Peskine449bd832023-01-11 14:50:10 +01007131 ssl_clear_peer_cert(ssl->session);
Jerry Yud9526692022-02-17 14:23:47 +08007132 }
7133#endif /* MBEDTLS_SSL_RENEGOTIATION && MBEDTLS_SSL_CLI_C */
7134
7135 /* Parse the next certificate in the chain. */
7136#if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
Gilles Peskine449bd832023-01-11 14:50:10 +01007137 ret = mbedtls_x509_crt_parse_der(chain, ssl->in_msg + i, n);
Jerry Yud9526692022-02-17 14:23:47 +08007138#else
7139 /* If we don't need to store the CRT chain permanently, parse
7140 * it in-place from the input buffer instead of making a copy. */
Gilles Peskine449bd832023-01-11 14:50:10 +01007141 ret = mbedtls_x509_crt_parse_der_nocopy(chain, ssl->in_msg + i, n);
Jerry Yud9526692022-02-17 14:23:47 +08007142#endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
Gilles Peskine449bd832023-01-11 14:50:10 +01007143 switch (ret) {
Jerry Yud9526692022-02-17 14:23:47 +08007144 case 0: /*ok*/
7145 case MBEDTLS_ERR_X509_UNKNOWN_SIG_ALG + MBEDTLS_ERR_OID_NOT_FOUND:
7146 /* Ignore certificate with an unknown algorithm: maybe a
7147 prior certificate was already trusted. */
7148 break;
7149
7150 case MBEDTLS_ERR_X509_ALLOC_FAILED:
7151 alert = MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR;
7152 goto crt_parse_der_failed;
7153
7154 case MBEDTLS_ERR_X509_UNKNOWN_VERSION:
7155 alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT;
7156 goto crt_parse_der_failed;
7157
7158 default:
7159 alert = MBEDTLS_SSL_ALERT_MSG_BAD_CERT;
Gilles Peskine449bd832023-01-11 14:50:10 +01007160crt_parse_der_failed:
7161 mbedtls_ssl_send_alert_message(ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL, alert);
7162 MBEDTLS_SSL_DEBUG_RET(1, " mbedtls_x509_crt_parse_der", ret);
7163 return ret;
Jerry Yud9526692022-02-17 14:23:47 +08007164 }
7165
7166 i += n;
7167 }
7168
Gilles Peskine449bd832023-01-11 14:50:10 +01007169 MBEDTLS_SSL_DEBUG_CRT(3, "peer certificate", chain);
7170 return 0;
Jerry Yud9526692022-02-17 14:23:47 +08007171}
7172
7173#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02007174MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01007175static int ssl_srv_check_client_no_crt_notification(mbedtls_ssl_context *ssl)
Jerry Yud9526692022-02-17 14:23:47 +08007176{
Gilles Peskine449bd832023-01-11 14:50:10 +01007177 if (ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT) {
7178 return -1;
7179 }
Jerry Yud9526692022-02-17 14:23:47 +08007180
Gilles Peskine449bd832023-01-11 14:50:10 +01007181 if (ssl->in_hslen == 3 + mbedtls_ssl_hs_hdr_len(ssl) &&
Jerry Yud9526692022-02-17 14:23:47 +08007182 ssl->in_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE &&
7183 ssl->in_msg[0] == MBEDTLS_SSL_HS_CERTIFICATE &&
Gilles Peskine449bd832023-01-11 14:50:10 +01007184 memcmp(ssl->in_msg + mbedtls_ssl_hs_hdr_len(ssl), "\0\0\0", 3) == 0) {
7185 MBEDTLS_SSL_DEBUG_MSG(1, ("peer has no certificate"));
7186 return 0;
Jerry Yud9526692022-02-17 14:23:47 +08007187 }
Gilles Peskine449bd832023-01-11 14:50:10 +01007188 return -1;
Jerry Yud9526692022-02-17 14:23:47 +08007189}
7190#endif /* MBEDTLS_SSL_SRV_C */
7191
7192/* Check if a certificate message is expected.
7193 * Return either
7194 * - SSL_CERTIFICATE_EXPECTED, or
7195 * - SSL_CERTIFICATE_SKIP
7196 * indicating whether a Certificate message is expected or not.
7197 */
7198#define SSL_CERTIFICATE_EXPECTED 0
7199#define SSL_CERTIFICATE_SKIP 1
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02007200MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01007201static int ssl_parse_certificate_coordinate(mbedtls_ssl_context *ssl,
7202 int authmode)
Jerry Yud9526692022-02-17 14:23:47 +08007203{
7204 const mbedtls_ssl_ciphersuite_t *ciphersuite_info =
7205 ssl->handshake->ciphersuite_info;
7206
Gilles Peskine449bd832023-01-11 14:50:10 +01007207 if (!mbedtls_ssl_ciphersuite_uses_srv_cert(ciphersuite_info)) {
7208 return SSL_CERTIFICATE_SKIP;
7209 }
Jerry Yud9526692022-02-17 14:23:47 +08007210
7211#if defined(MBEDTLS_SSL_SRV_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01007212 if (ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER) {
7213 if (ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_RSA_PSK) {
7214 return SSL_CERTIFICATE_SKIP;
7215 }
Jerry Yud9526692022-02-17 14:23:47 +08007216
Gilles Peskine449bd832023-01-11 14:50:10 +01007217 if (authmode == MBEDTLS_SSL_VERIFY_NONE) {
Jerry Yud9526692022-02-17 14:23:47 +08007218 ssl->session_negotiate->verify_result =
7219 MBEDTLS_X509_BADCERT_SKIP_VERIFY;
Gilles Peskine449bd832023-01-11 14:50:10 +01007220 return SSL_CERTIFICATE_SKIP;
Jerry Yud9526692022-02-17 14:23:47 +08007221 }
7222 }
7223#else
7224 ((void) authmode);
7225#endif /* MBEDTLS_SSL_SRV_C */
7226
Gilles Peskine449bd832023-01-11 14:50:10 +01007227 return SSL_CERTIFICATE_EXPECTED;
Jerry Yud9526692022-02-17 14:23:47 +08007228}
7229
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02007230MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01007231static int ssl_parse_certificate_verify(mbedtls_ssl_context *ssl,
7232 int authmode,
7233 mbedtls_x509_crt *chain,
7234 void *rs_ctx)
Jerry Yud9526692022-02-17 14:23:47 +08007235{
7236 int ret = 0;
7237 const mbedtls_ssl_ciphersuite_t *ciphersuite_info =
7238 ssl->handshake->ciphersuite_info;
7239 int have_ca_chain = 0;
7240
7241 int (*f_vrfy)(void *, mbedtls_x509_crt *, int, uint32_t *);
7242 void *p_vrfy;
7243
Gilles Peskine449bd832023-01-11 14:50:10 +01007244 if (authmode == MBEDTLS_SSL_VERIFY_NONE) {
7245 return 0;
7246 }
Jerry Yud9526692022-02-17 14:23:47 +08007247
Gilles Peskine449bd832023-01-11 14:50:10 +01007248 if (ssl->f_vrfy != NULL) {
7249 MBEDTLS_SSL_DEBUG_MSG(3, ("Use context-specific verification callback"));
Jerry Yud9526692022-02-17 14:23:47 +08007250 f_vrfy = ssl->f_vrfy;
7251 p_vrfy = ssl->p_vrfy;
Gilles Peskine449bd832023-01-11 14:50:10 +01007252 } else {
7253 MBEDTLS_SSL_DEBUG_MSG(3, ("Use configuration-specific verification callback"));
Jerry Yud9526692022-02-17 14:23:47 +08007254 f_vrfy = ssl->conf->f_vrfy;
7255 p_vrfy = ssl->conf->p_vrfy;
7256 }
7257
7258 /*
7259 * Main check: verify certificate
7260 */
7261#if defined(MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK)
Gilles Peskine449bd832023-01-11 14:50:10 +01007262 if (ssl->conf->f_ca_cb != NULL) {
Jerry Yud9526692022-02-17 14:23:47 +08007263 ((void) rs_ctx);
7264 have_ca_chain = 1;
7265
Gilles Peskine449bd832023-01-11 14:50:10 +01007266 MBEDTLS_SSL_DEBUG_MSG(3, ("use CA callback for X.509 CRT verification"));
Jerry Yud9526692022-02-17 14:23:47 +08007267 ret = mbedtls_x509_crt_verify_with_ca_cb(
7268 chain,
7269 ssl->conf->f_ca_cb,
7270 ssl->conf->p_ca_cb,
7271 ssl->conf->cert_profile,
7272 ssl->hostname,
7273 &ssl->session_negotiate->verify_result,
Gilles Peskine449bd832023-01-11 14:50:10 +01007274 f_vrfy, p_vrfy);
7275 } else
Jerry Yud9526692022-02-17 14:23:47 +08007276#endif /* MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK */
7277 {
7278 mbedtls_x509_crt *ca_chain;
7279 mbedtls_x509_crl *ca_crl;
7280
7281#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
Gilles Peskine449bd832023-01-11 14:50:10 +01007282 if (ssl->handshake->sni_ca_chain != NULL) {
Jerry Yud9526692022-02-17 14:23:47 +08007283 ca_chain = ssl->handshake->sni_ca_chain;
7284 ca_crl = ssl->handshake->sni_ca_crl;
Gilles Peskine449bd832023-01-11 14:50:10 +01007285 } else
Jerry Yud9526692022-02-17 14:23:47 +08007286#endif
7287 {
7288 ca_chain = ssl->conf->ca_chain;
7289 ca_crl = ssl->conf->ca_crl;
7290 }
7291
Gilles Peskine449bd832023-01-11 14:50:10 +01007292 if (ca_chain != NULL) {
Jerry Yud9526692022-02-17 14:23:47 +08007293 have_ca_chain = 1;
Gilles Peskine449bd832023-01-11 14:50:10 +01007294 }
Jerry Yud9526692022-02-17 14:23:47 +08007295
7296 ret = mbedtls_x509_crt_verify_restartable(
7297 chain,
7298 ca_chain, ca_crl,
7299 ssl->conf->cert_profile,
7300 ssl->hostname,
7301 &ssl->session_negotiate->verify_result,
Gilles Peskine449bd832023-01-11 14:50:10 +01007302 f_vrfy, p_vrfy, rs_ctx);
Jerry Yud9526692022-02-17 14:23:47 +08007303 }
7304
Gilles Peskine449bd832023-01-11 14:50:10 +01007305 if (ret != 0) {
7306 MBEDTLS_SSL_DEBUG_RET(1, "x509_verify_cert", ret);
Jerry Yud9526692022-02-17 14:23:47 +08007307 }
7308
7309#if defined(MBEDTLS_SSL_ECP_RESTARTABLE_ENABLED)
Gilles Peskine449bd832023-01-11 14:50:10 +01007310 if (ret == MBEDTLS_ERR_ECP_IN_PROGRESS) {
7311 return MBEDTLS_ERR_SSL_CRYPTO_IN_PROGRESS;
7312 }
Jerry Yud9526692022-02-17 14:23:47 +08007313#endif
7314
7315 /*
7316 * Secondary checks: always done, but change 'ret' only if it was 0
7317 */
7318
7319#if defined(MBEDTLS_ECP_C)
7320 {
7321 const mbedtls_pk_context *pk = &chain->pk;
7322
Manuel Pégourié-Gonnard66b0d612022-06-17 10:49:29 +02007323 /* If certificate uses an EC key, make sure the curve is OK.
7324 * This is a public key, so it can't be opaque, so can_do() is a good
7325 * enough check to ensure pk_ec() is safe to use here. */
Gilles Peskine449bd832023-01-11 14:50:10 +01007326 if (mbedtls_pk_can_do(pk, MBEDTLS_PK_ECKEY)) {
Leonid Rozenboim19e59732022-08-08 16:52:38 -07007327 /* and in the unlikely case the above assumption no longer holds
7328 * we are making sure that pk_ec() here does not return a NULL
7329 */
Gilles Peskine449bd832023-01-11 14:50:10 +01007330 const mbedtls_ecp_keypair *ec = mbedtls_pk_ec(*pk);
7331 if (ec == NULL) {
7332 MBEDTLS_SSL_DEBUG_MSG(1, ("mbedtls_pk_ec() returned NULL"));
7333 return MBEDTLS_ERR_SSL_INTERNAL_ERROR;
Leonid Rozenboim19e59732022-08-08 16:52:38 -07007334 }
Jerry Yud9526692022-02-17 14:23:47 +08007335
Gilles Peskine449bd832023-01-11 14:50:10 +01007336 if (mbedtls_ssl_check_curve(ssl, ec->grp.id) != 0) {
Leonid Rozenboim19e59732022-08-08 16:52:38 -07007337 ssl->session_negotiate->verify_result |=
7338 MBEDTLS_X509_BADCERT_BAD_KEY;
7339
Gilles Peskine449bd832023-01-11 14:50:10 +01007340 MBEDTLS_SSL_DEBUG_MSG(1, ("bad certificate (EC key curve)"));
7341 if (ret == 0) {
Leonid Rozenboim19e59732022-08-08 16:52:38 -07007342 ret = MBEDTLS_ERR_SSL_BAD_CERTIFICATE;
Gilles Peskine449bd832023-01-11 14:50:10 +01007343 }
Leonid Rozenboim19e59732022-08-08 16:52:38 -07007344 }
Jerry Yud9526692022-02-17 14:23:47 +08007345 }
7346 }
7347#endif /* MBEDTLS_ECP_C */
7348
Gilles Peskine449bd832023-01-11 14:50:10 +01007349 if (mbedtls_ssl_check_cert_usage(chain,
7350 ciphersuite_info,
7351 !ssl->conf->endpoint,
7352 &ssl->session_negotiate->verify_result) != 0) {
7353 MBEDTLS_SSL_DEBUG_MSG(1, ("bad certificate (usage extensions)"));
7354 if (ret == 0) {
Jerry Yud9526692022-02-17 14:23:47 +08007355 ret = MBEDTLS_ERR_SSL_BAD_CERTIFICATE;
Gilles Peskine449bd832023-01-11 14:50:10 +01007356 }
Jerry Yud9526692022-02-17 14:23:47 +08007357 }
7358
7359 /* mbedtls_x509_crt_verify_with_profile is supposed to report a
7360 * verification failure through MBEDTLS_ERR_X509_CERT_VERIFY_FAILED,
7361 * with details encoded in the verification flags. All other kinds
7362 * of error codes, including those from the user provided f_vrfy
7363 * functions, are treated as fatal and lead to a failure of
7364 * ssl_parse_certificate even if verification was optional. */
Gilles Peskine449bd832023-01-11 14:50:10 +01007365 if (authmode == MBEDTLS_SSL_VERIFY_OPTIONAL &&
7366 (ret == MBEDTLS_ERR_X509_CERT_VERIFY_FAILED ||
7367 ret == MBEDTLS_ERR_SSL_BAD_CERTIFICATE)) {
Jerry Yud9526692022-02-17 14:23:47 +08007368 ret = 0;
7369 }
7370
Gilles Peskine449bd832023-01-11 14:50:10 +01007371 if (have_ca_chain == 0 && authmode == MBEDTLS_SSL_VERIFY_REQUIRED) {
7372 MBEDTLS_SSL_DEBUG_MSG(1, ("got no CA chain"));
Jerry Yud9526692022-02-17 14:23:47 +08007373 ret = MBEDTLS_ERR_SSL_CA_CHAIN_REQUIRED;
7374 }
7375
Gilles Peskine449bd832023-01-11 14:50:10 +01007376 if (ret != 0) {
Jerry Yud9526692022-02-17 14:23:47 +08007377 uint8_t alert;
7378
7379 /* The certificate may have been rejected for several reasons.
7380 Pick one and send the corresponding alert. Which alert to send
7381 may be a subject of debate in some cases. */
Gilles Peskine449bd832023-01-11 14:50:10 +01007382 if (ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_OTHER) {
Jerry Yud9526692022-02-17 14:23:47 +08007383 alert = MBEDTLS_SSL_ALERT_MSG_ACCESS_DENIED;
Gilles Peskine449bd832023-01-11 14:50:10 +01007384 } else if (ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_CN_MISMATCH) {
Jerry Yud9526692022-02-17 14:23:47 +08007385 alert = MBEDTLS_SSL_ALERT_MSG_BAD_CERT;
Gilles Peskine449bd832023-01-11 14:50:10 +01007386 } else if (ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_KEY_USAGE) {
Jerry Yud9526692022-02-17 14:23:47 +08007387 alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT;
Gilles Peskine449bd832023-01-11 14:50:10 +01007388 } else if (ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_EXT_KEY_USAGE) {
Jerry Yud9526692022-02-17 14:23:47 +08007389 alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT;
Gilles Peskine449bd832023-01-11 14:50:10 +01007390 } else if (ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_NS_CERT_TYPE) {
Jerry Yud9526692022-02-17 14:23:47 +08007391 alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT;
Gilles Peskine449bd832023-01-11 14:50:10 +01007392 } else if (ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_BAD_PK) {
Jerry Yud9526692022-02-17 14:23:47 +08007393 alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT;
Gilles Peskine449bd832023-01-11 14:50:10 +01007394 } else if (ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_BAD_KEY) {
Jerry Yud9526692022-02-17 14:23:47 +08007395 alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT;
Gilles Peskine449bd832023-01-11 14:50:10 +01007396 } else if (ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_EXPIRED) {
Jerry Yud9526692022-02-17 14:23:47 +08007397 alert = MBEDTLS_SSL_ALERT_MSG_CERT_EXPIRED;
Gilles Peskine449bd832023-01-11 14:50:10 +01007398 } else if (ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_REVOKED) {
Jerry Yud9526692022-02-17 14:23:47 +08007399 alert = MBEDTLS_SSL_ALERT_MSG_CERT_REVOKED;
Gilles Peskine449bd832023-01-11 14:50:10 +01007400 } else if (ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_NOT_TRUSTED) {
Jerry Yud9526692022-02-17 14:23:47 +08007401 alert = MBEDTLS_SSL_ALERT_MSG_UNKNOWN_CA;
Gilles Peskine449bd832023-01-11 14:50:10 +01007402 } else {
Jerry Yud9526692022-02-17 14:23:47 +08007403 alert = MBEDTLS_SSL_ALERT_MSG_CERT_UNKNOWN;
Gilles Peskine449bd832023-01-11 14:50:10 +01007404 }
7405 mbedtls_ssl_send_alert_message(ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
7406 alert);
Jerry Yud9526692022-02-17 14:23:47 +08007407 }
7408
7409#if defined(MBEDTLS_DEBUG_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01007410 if (ssl->session_negotiate->verify_result != 0) {
7411 MBEDTLS_SSL_DEBUG_MSG(3, ("! Certificate verification flags %08x",
7412 (unsigned int) ssl->session_negotiate->verify_result));
7413 } else {
7414 MBEDTLS_SSL_DEBUG_MSG(3, ("Certificate verification flags clear"));
Jerry Yud9526692022-02-17 14:23:47 +08007415 }
7416#endif /* MBEDTLS_DEBUG_C */
7417
Gilles Peskine449bd832023-01-11 14:50:10 +01007418 return ret;
Jerry Yud9526692022-02-17 14:23:47 +08007419}
7420
7421#if !defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02007422MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01007423static int ssl_remember_peer_crt_digest(mbedtls_ssl_context *ssl,
7424 unsigned char *start, size_t len)
Jerry Yud9526692022-02-17 14:23:47 +08007425{
7426 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
7427 /* Remember digest of the peer's end-CRT. */
7428 ssl->session_negotiate->peer_cert_digest =
Gilles Peskine449bd832023-01-11 14:50:10 +01007429 mbedtls_calloc(1, MBEDTLS_SSL_PEER_CERT_DIGEST_DFL_LEN);
7430 if (ssl->session_negotiate->peer_cert_digest == NULL) {
7431 MBEDTLS_SSL_DEBUG_MSG(1, ("alloc(%d bytes) failed",
7432 MBEDTLS_SSL_PEER_CERT_DIGEST_DFL_LEN));
7433 mbedtls_ssl_send_alert_message(ssl,
7434 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
7435 MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR);
Jerry Yud9526692022-02-17 14:23:47 +08007436
Gilles Peskine449bd832023-01-11 14:50:10 +01007437 return MBEDTLS_ERR_SSL_ALLOC_FAILED;
Jerry Yud9526692022-02-17 14:23:47 +08007438 }
7439
Gilles Peskine449bd832023-01-11 14:50:10 +01007440 ret = mbedtls_md(mbedtls_md_info_from_type(
7441 MBEDTLS_SSL_PEER_CERT_DIGEST_DFL_TYPE),
7442 start, len,
7443 ssl->session_negotiate->peer_cert_digest);
Jerry Yud9526692022-02-17 14:23:47 +08007444
7445 ssl->session_negotiate->peer_cert_digest_type =
7446 MBEDTLS_SSL_PEER_CERT_DIGEST_DFL_TYPE;
7447 ssl->session_negotiate->peer_cert_digest_len =
7448 MBEDTLS_SSL_PEER_CERT_DIGEST_DFL_LEN;
7449
Gilles Peskine449bd832023-01-11 14:50:10 +01007450 return ret;
Jerry Yud9526692022-02-17 14:23:47 +08007451}
7452
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02007453MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01007454static int ssl_remember_peer_pubkey(mbedtls_ssl_context *ssl,
7455 unsigned char *start, size_t len)
Jerry Yud9526692022-02-17 14:23:47 +08007456{
7457 unsigned char *end = start + len;
7458 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
7459
7460 /* Make a copy of the peer's raw public key. */
Gilles Peskine449bd832023-01-11 14:50:10 +01007461 mbedtls_pk_init(&ssl->handshake->peer_pubkey);
7462 ret = mbedtls_pk_parse_subpubkey(&start, end,
7463 &ssl->handshake->peer_pubkey);
7464 if (ret != 0) {
Jerry Yud9526692022-02-17 14:23:47 +08007465 /* We should have parsed the public key before. */
Gilles Peskine449bd832023-01-11 14:50:10 +01007466 return MBEDTLS_ERR_SSL_INTERNAL_ERROR;
Jerry Yud9526692022-02-17 14:23:47 +08007467 }
7468
Gilles Peskine449bd832023-01-11 14:50:10 +01007469 return 0;
Jerry Yud9526692022-02-17 14:23:47 +08007470}
7471#endif /* !MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
7472
Gilles Peskine449bd832023-01-11 14:50:10 +01007473int mbedtls_ssl_parse_certificate(mbedtls_ssl_context *ssl)
Jerry Yud9526692022-02-17 14:23:47 +08007474{
7475 int ret = 0;
7476 int crt_expected;
7477#if defined(MBEDTLS_SSL_SRV_C) && defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
7478 const int authmode = ssl->handshake->sni_authmode != MBEDTLS_SSL_VERIFY_UNSET
7479 ? ssl->handshake->sni_authmode
7480 : ssl->conf->authmode;
7481#else
7482 const int authmode = ssl->conf->authmode;
7483#endif
7484 void *rs_ctx = NULL;
7485 mbedtls_x509_crt *chain = NULL;
7486
Gilles Peskine449bd832023-01-11 14:50:10 +01007487 MBEDTLS_SSL_DEBUG_MSG(2, ("=> parse certificate"));
Jerry Yud9526692022-02-17 14:23:47 +08007488
Gilles Peskine449bd832023-01-11 14:50:10 +01007489 crt_expected = ssl_parse_certificate_coordinate(ssl, authmode);
7490 if (crt_expected == SSL_CERTIFICATE_SKIP) {
7491 MBEDTLS_SSL_DEBUG_MSG(2, ("<= skip parse certificate"));
Jerry Yud9526692022-02-17 14:23:47 +08007492 goto exit;
7493 }
7494
7495#if defined(MBEDTLS_SSL_ECP_RESTARTABLE_ENABLED)
Gilles Peskine449bd832023-01-11 14:50:10 +01007496 if (ssl->handshake->ecrs_enabled &&
7497 ssl->handshake->ecrs_state == ssl_ecrs_crt_verify) {
Jerry Yud9526692022-02-17 14:23:47 +08007498 chain = ssl->handshake->ecrs_peer_cert;
7499 ssl->handshake->ecrs_peer_cert = NULL;
7500 goto crt_verify;
7501 }
7502#endif
7503
Gilles Peskine449bd832023-01-11 14:50:10 +01007504 if ((ret = mbedtls_ssl_read_record(ssl, 1)) != 0) {
Jerry Yud9526692022-02-17 14:23:47 +08007505 /* mbedtls_ssl_read_record may have sent an alert already. We
7506 let it decide whether to alert. */
Gilles Peskine449bd832023-01-11 14:50:10 +01007507 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ssl_read_record", ret);
Jerry Yud9526692022-02-17 14:23:47 +08007508 goto exit;
7509 }
7510
7511#if defined(MBEDTLS_SSL_SRV_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01007512 if (ssl_srv_check_client_no_crt_notification(ssl) == 0) {
Jerry Yud9526692022-02-17 14:23:47 +08007513 ssl->session_negotiate->verify_result = MBEDTLS_X509_BADCERT_MISSING;
7514
Gilles Peskine449bd832023-01-11 14:50:10 +01007515 if (authmode != MBEDTLS_SSL_VERIFY_OPTIONAL) {
Jerry Yud9526692022-02-17 14:23:47 +08007516 ret = MBEDTLS_ERR_SSL_NO_CLIENT_CERTIFICATE;
Gilles Peskine449bd832023-01-11 14:50:10 +01007517 }
Jerry Yud9526692022-02-17 14:23:47 +08007518
7519 goto exit;
7520 }
7521#endif /* MBEDTLS_SSL_SRV_C */
7522
7523 /* Clear existing peer CRT structure in case we tried to
7524 * reuse a session but it failed, and allocate a new one. */
Gilles Peskine449bd832023-01-11 14:50:10 +01007525 ssl_clear_peer_cert(ssl->session_negotiate);
Jerry Yud9526692022-02-17 14:23:47 +08007526
Gilles Peskine449bd832023-01-11 14:50:10 +01007527 chain = mbedtls_calloc(1, sizeof(mbedtls_x509_crt));
7528 if (chain == NULL) {
7529 MBEDTLS_SSL_DEBUG_MSG(1, ("alloc(%" MBEDTLS_PRINTF_SIZET " bytes) failed",
7530 sizeof(mbedtls_x509_crt)));
7531 mbedtls_ssl_send_alert_message(ssl,
7532 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
7533 MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR);
Jerry Yud9526692022-02-17 14:23:47 +08007534
7535 ret = MBEDTLS_ERR_SSL_ALLOC_FAILED;
7536 goto exit;
7537 }
Gilles Peskine449bd832023-01-11 14:50:10 +01007538 mbedtls_x509_crt_init(chain);
Jerry Yud9526692022-02-17 14:23:47 +08007539
Gilles Peskine449bd832023-01-11 14:50:10 +01007540 ret = ssl_parse_certificate_chain(ssl, chain);
7541 if (ret != 0) {
Jerry Yud9526692022-02-17 14:23:47 +08007542 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01007543 }
Jerry Yud9526692022-02-17 14:23:47 +08007544
7545#if defined(MBEDTLS_SSL_ECP_RESTARTABLE_ENABLED)
Gilles Peskine449bd832023-01-11 14:50:10 +01007546 if (ssl->handshake->ecrs_enabled) {
Jerry Yud9526692022-02-17 14:23:47 +08007547 ssl->handshake->ecrs_state = ssl_ecrs_crt_verify;
Gilles Peskine449bd832023-01-11 14:50:10 +01007548 }
Jerry Yud9526692022-02-17 14:23:47 +08007549
7550crt_verify:
Gilles Peskine449bd832023-01-11 14:50:10 +01007551 if (ssl->handshake->ecrs_enabled) {
Jerry Yud9526692022-02-17 14:23:47 +08007552 rs_ctx = &ssl->handshake->ecrs_ctx;
Gilles Peskine449bd832023-01-11 14:50:10 +01007553 }
Jerry Yud9526692022-02-17 14:23:47 +08007554#endif
7555
Gilles Peskine449bd832023-01-11 14:50:10 +01007556 ret = ssl_parse_certificate_verify(ssl, authmode,
7557 chain, rs_ctx);
7558 if (ret != 0) {
Jerry Yud9526692022-02-17 14:23:47 +08007559 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01007560 }
Jerry Yud9526692022-02-17 14:23:47 +08007561
7562#if !defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
7563 {
7564 unsigned char *crt_start, *pk_start;
7565 size_t crt_len, pk_len;
7566
7567 /* We parse the CRT chain without copying, so
7568 * these pointers point into the input buffer,
7569 * and are hence still valid after freeing the
7570 * CRT chain. */
7571
7572 crt_start = chain->raw.p;
7573 crt_len = chain->raw.len;
7574
7575 pk_start = chain->pk_raw.p;
7576 pk_len = chain->pk_raw.len;
7577
7578 /* Free the CRT structures before computing
7579 * digest and copying the peer's public key. */
Gilles Peskine449bd832023-01-11 14:50:10 +01007580 mbedtls_x509_crt_free(chain);
7581 mbedtls_free(chain);
Jerry Yud9526692022-02-17 14:23:47 +08007582 chain = NULL;
7583
Gilles Peskine449bd832023-01-11 14:50:10 +01007584 ret = ssl_remember_peer_crt_digest(ssl, crt_start, crt_len);
7585 if (ret != 0) {
Jerry Yud9526692022-02-17 14:23:47 +08007586 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01007587 }
Jerry Yud9526692022-02-17 14:23:47 +08007588
Gilles Peskine449bd832023-01-11 14:50:10 +01007589 ret = ssl_remember_peer_pubkey(ssl, pk_start, pk_len);
7590 if (ret != 0) {
Jerry Yud9526692022-02-17 14:23:47 +08007591 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01007592 }
Jerry Yud9526692022-02-17 14:23:47 +08007593 }
7594#else /* !MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
7595 /* Pass ownership to session structure. */
7596 ssl->session_negotiate->peer_cert = chain;
7597 chain = NULL;
7598#endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
7599
Gilles Peskine449bd832023-01-11 14:50:10 +01007600 MBEDTLS_SSL_DEBUG_MSG(2, ("<= parse certificate"));
Jerry Yud9526692022-02-17 14:23:47 +08007601
7602exit:
7603
Gilles Peskine449bd832023-01-11 14:50:10 +01007604 if (ret == 0) {
Jerry Yud9526692022-02-17 14:23:47 +08007605 ssl->state++;
Gilles Peskine449bd832023-01-11 14:50:10 +01007606 }
Jerry Yud9526692022-02-17 14:23:47 +08007607
7608#if defined(MBEDTLS_SSL_ECP_RESTARTABLE_ENABLED)
Gilles Peskine449bd832023-01-11 14:50:10 +01007609 if (ret == MBEDTLS_ERR_SSL_CRYPTO_IN_PROGRESS) {
Jerry Yud9526692022-02-17 14:23:47 +08007610 ssl->handshake->ecrs_peer_cert = chain;
7611 chain = NULL;
7612 }
7613#endif
7614
Gilles Peskine449bd832023-01-11 14:50:10 +01007615 if (chain != NULL) {
7616 mbedtls_x509_crt_free(chain);
7617 mbedtls_free(chain);
Jerry Yud9526692022-02-17 14:23:47 +08007618 }
7619
Gilles Peskine449bd832023-01-11 14:50:10 +01007620 return ret;
Jerry Yud9526692022-02-17 14:23:47 +08007621}
7622#endif /* MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED */
7623
Andrzej Kurek25f27152022-08-17 16:09:31 -04007624#if defined(MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Manuel Pégourié-Gonnard226aa152023-02-05 09:46:59 +01007625static int ssl_calc_finished_tls_sha256(
Gilles Peskine449bd832023-01-11 14:50:10 +01007626 mbedtls_ssl_context *ssl, unsigned char *buf, int from)
Jerry Yu615bd6f2022-02-17 14:25:15 +08007627{
7628 int len = 12;
7629 const char *sender;
7630 unsigned char padbuf[32];
7631#if defined(MBEDTLS_USE_PSA_CRYPTO)
7632 size_t hash_size;
7633 psa_hash_operation_t sha256_psa = PSA_HASH_OPERATION_INIT;
7634 psa_status_t status;
7635#else
Manuel Pégourié-Gonnarde1a4caa2023-02-06 10:14:25 +01007636 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Jerry Yu615bd6f2022-02-17 14:25:15 +08007637 mbedtls_sha256_context sha256;
7638#endif
7639
7640 mbedtls_ssl_session *session = ssl->session_negotiate;
Gilles Peskine449bd832023-01-11 14:50:10 +01007641 if (!session) {
Jerry Yu615bd6f2022-02-17 14:25:15 +08007642 session = ssl->session;
Gilles Peskine449bd832023-01-11 14:50:10 +01007643 }
Jerry Yu615bd6f2022-02-17 14:25:15 +08007644
Gilles Peskine449bd832023-01-11 14:50:10 +01007645 sender = (from == MBEDTLS_SSL_IS_CLIENT)
Jerry Yu615bd6f2022-02-17 14:25:15 +08007646 ? "client finished"
7647 : "server finished";
7648
7649#if defined(MBEDTLS_USE_PSA_CRYPTO)
7650 sha256_psa = psa_hash_operation_init();
7651
Gilles Peskine449bd832023-01-11 14:50:10 +01007652 MBEDTLS_SSL_DEBUG_MSG(2, ("=> calc PSA finished tls sha256"));
Jerry Yu615bd6f2022-02-17 14:25:15 +08007653
Gilles Peskine449bd832023-01-11 14:50:10 +01007654 status = psa_hash_clone(&ssl->handshake->fin_sha256_psa, &sha256_psa);
7655 if (status != PSA_SUCCESS) {
Manuel Pégourié-Gonnarde1a4caa2023-02-06 10:14:25 +01007656 goto exit;
Jerry Yu615bd6f2022-02-17 14:25:15 +08007657 }
7658
Gilles Peskine449bd832023-01-11 14:50:10 +01007659 status = psa_hash_finish(&sha256_psa, padbuf, sizeof(padbuf), &hash_size);
7660 if (status != PSA_SUCCESS) {
Manuel Pégourié-Gonnarde1a4caa2023-02-06 10:14:25 +01007661 goto exit;
Jerry Yu615bd6f2022-02-17 14:25:15 +08007662 }
Gilles Peskine449bd832023-01-11 14:50:10 +01007663 MBEDTLS_SSL_DEBUG_BUF(3, "PSA calculated padbuf", padbuf, 32);
Jerry Yu615bd6f2022-02-17 14:25:15 +08007664#else
7665
Gilles Peskine449bd832023-01-11 14:50:10 +01007666 mbedtls_sha256_init(&sha256);
Jerry Yu615bd6f2022-02-17 14:25:15 +08007667
Gilles Peskine449bd832023-01-11 14:50:10 +01007668 MBEDTLS_SSL_DEBUG_MSG(2, ("=> calc finished tls sha256"));
Jerry Yu615bd6f2022-02-17 14:25:15 +08007669
Gilles Peskine449bd832023-01-11 14:50:10 +01007670 mbedtls_sha256_clone(&sha256, &ssl->handshake->fin_sha256);
Jerry Yu615bd6f2022-02-17 14:25:15 +08007671
7672 /*
7673 * TLSv1.2:
7674 * hash = PRF( master, finished_label,
7675 * Hash( handshake ) )[0.11]
7676 */
7677
Manuel Pégourié-Gonnarde1a4caa2023-02-06 10:14:25 +01007678 ret = mbedtls_sha256_finish(&sha256, padbuf);
7679 if (ret != 0) {
7680 goto exit;
7681 }
Jerry Yu615bd6f2022-02-17 14:25:15 +08007682#endif /* MBEDTLS_USE_PSA_CRYPTO */
7683
Manuel Pégourié-Gonnard0ac71c02023-02-24 12:13:55 +01007684 MBEDTLS_SSL_DEBUG_BUF(4, "finished sha256 output", padbuf, 32);
7685
Gilles Peskine449bd832023-01-11 14:50:10 +01007686 ssl->handshake->tls_prf(session->master, 48, sender,
7687 padbuf, 32, buf, len);
Jerry Yu615bd6f2022-02-17 14:25:15 +08007688
Gilles Peskine449bd832023-01-11 14:50:10 +01007689 MBEDTLS_SSL_DEBUG_BUF(3, "calc finished result", buf, len);
Jerry Yu615bd6f2022-02-17 14:25:15 +08007690
Gilles Peskine449bd832023-01-11 14:50:10 +01007691 mbedtls_platform_zeroize(padbuf, sizeof(padbuf));
Jerry Yu615bd6f2022-02-17 14:25:15 +08007692
Gilles Peskine449bd832023-01-11 14:50:10 +01007693 MBEDTLS_SSL_DEBUG_MSG(2, ("<= calc finished"));
Manuel Pégourié-Gonnarde1a4caa2023-02-06 10:14:25 +01007694
7695exit:
7696#if defined(MBEDTLS_USE_PSA_CRYPTO)
7697 psa_hash_abort(&sha256_psa);
7698 return mbedtls_md_error_from_psa(status);
7699#else
7700 mbedtls_sha256_free(&sha256);
7701 return ret;
7702#endif /* MBEDTLS_USE_PSA_CRYPTO */
Jerry Yu615bd6f2022-02-17 14:25:15 +08007703}
Andrzej Kurekcccb0442022-08-19 03:42:11 -04007704#endif /* MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA*/
Jerry Yu615bd6f2022-02-17 14:25:15 +08007705
Jerry Yub7ba49e2022-02-17 14:25:53 +08007706
Andrzej Kurek25f27152022-08-17 16:09:31 -04007707#if defined(MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Manuel Pégourié-Gonnard226aa152023-02-05 09:46:59 +01007708static int ssl_calc_finished_tls_sha384(
Gilles Peskine449bd832023-01-11 14:50:10 +01007709 mbedtls_ssl_context *ssl, unsigned char *buf, int from)
Jerry Yub7ba49e2022-02-17 14:25:53 +08007710{
7711 int len = 12;
7712 const char *sender;
7713 unsigned char padbuf[48];
7714#if defined(MBEDTLS_USE_PSA_CRYPTO)
7715 size_t hash_size;
7716 psa_hash_operation_t sha384_psa = PSA_HASH_OPERATION_INIT;
7717 psa_status_t status;
7718#else
Manuel Pégourié-Gonnarde1a4caa2023-02-06 10:14:25 +01007719 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Jerry Yub7ba49e2022-02-17 14:25:53 +08007720 mbedtls_sha512_context sha512;
7721#endif
7722
7723 mbedtls_ssl_session *session = ssl->session_negotiate;
Gilles Peskine449bd832023-01-11 14:50:10 +01007724 if (!session) {
Jerry Yub7ba49e2022-02-17 14:25:53 +08007725 session = ssl->session;
Gilles Peskine449bd832023-01-11 14:50:10 +01007726 }
Jerry Yub7ba49e2022-02-17 14:25:53 +08007727
Gilles Peskine449bd832023-01-11 14:50:10 +01007728 sender = (from == MBEDTLS_SSL_IS_CLIENT)
Jerry Yub7ba49e2022-02-17 14:25:53 +08007729 ? "client finished"
7730 : "server finished";
7731
7732#if defined(MBEDTLS_USE_PSA_CRYPTO)
7733 sha384_psa = psa_hash_operation_init();
7734
Gilles Peskine449bd832023-01-11 14:50:10 +01007735 MBEDTLS_SSL_DEBUG_MSG(2, ("=> calc PSA finished tls sha384"));
Jerry Yub7ba49e2022-02-17 14:25:53 +08007736
Gilles Peskine449bd832023-01-11 14:50:10 +01007737 status = psa_hash_clone(&ssl->handshake->fin_sha384_psa, &sha384_psa);
7738 if (status != PSA_SUCCESS) {
Manuel Pégourié-Gonnarde1a4caa2023-02-06 10:14:25 +01007739 goto exit;
Jerry Yub7ba49e2022-02-17 14:25:53 +08007740 }
7741
Gilles Peskine449bd832023-01-11 14:50:10 +01007742 status = psa_hash_finish(&sha384_psa, padbuf, sizeof(padbuf), &hash_size);
7743 if (status != PSA_SUCCESS) {
Manuel Pégourié-Gonnarde1a4caa2023-02-06 10:14:25 +01007744 goto exit;
Jerry Yub7ba49e2022-02-17 14:25:53 +08007745 }
Gilles Peskine449bd832023-01-11 14:50:10 +01007746 MBEDTLS_SSL_DEBUG_BUF(3, "PSA calculated padbuf", padbuf, 48);
Jerry Yub7ba49e2022-02-17 14:25:53 +08007747#else
Gilles Peskine449bd832023-01-11 14:50:10 +01007748 mbedtls_sha512_init(&sha512);
Jerry Yub7ba49e2022-02-17 14:25:53 +08007749
Gilles Peskine449bd832023-01-11 14:50:10 +01007750 MBEDTLS_SSL_DEBUG_MSG(2, ("=> calc finished tls sha384"));
Jerry Yub7ba49e2022-02-17 14:25:53 +08007751
Gilles Peskine449bd832023-01-11 14:50:10 +01007752 mbedtls_sha512_clone(&sha512, &ssl->handshake->fin_sha384);
Jerry Yub7ba49e2022-02-17 14:25:53 +08007753
7754 /*
7755 * TLSv1.2:
7756 * hash = PRF( master, finished_label,
7757 * Hash( handshake ) )[0.11]
7758 */
7759
Manuel Pégourié-Gonnarde1a4caa2023-02-06 10:14:25 +01007760 ret = mbedtls_sha512_finish(&sha512, padbuf);
7761 if (ret != 0) {
7762 goto exit;
7763 }
Jerry Yub7ba49e2022-02-17 14:25:53 +08007764#endif
7765
Manuel Pégourié-Gonnard0ac71c02023-02-24 12:13:55 +01007766 MBEDTLS_SSL_DEBUG_BUF(4, "finished sha384 output", padbuf, 48);
7767
Gilles Peskine449bd832023-01-11 14:50:10 +01007768 ssl->handshake->tls_prf(session->master, 48, sender,
7769 padbuf, 48, buf, len);
Jerry Yub7ba49e2022-02-17 14:25:53 +08007770
Gilles Peskine449bd832023-01-11 14:50:10 +01007771 MBEDTLS_SSL_DEBUG_BUF(3, "calc finished result", buf, len);
Jerry Yub7ba49e2022-02-17 14:25:53 +08007772
Gilles Peskine449bd832023-01-11 14:50:10 +01007773 mbedtls_platform_zeroize(padbuf, sizeof(padbuf));
Jerry Yub7ba49e2022-02-17 14:25:53 +08007774
Gilles Peskine449bd832023-01-11 14:50:10 +01007775 MBEDTLS_SSL_DEBUG_MSG(2, ("<= calc finished"));
Manuel Pégourié-Gonnarde1a4caa2023-02-06 10:14:25 +01007776
7777exit:
7778#if defined(MBEDTLS_USE_PSA_CRYPTO)
7779 psa_hash_abort(&sha384_psa);
7780 return mbedtls_md_error_from_psa(status);
7781#else
7782 mbedtls_sha512_free(&sha512);
7783 return ret;
7784#endif /* MBEDTLS_USE_PSA_CRYPTO */
Jerry Yub7ba49e2022-02-17 14:25:53 +08007785}
Andrzej Kurekcccb0442022-08-19 03:42:11 -04007786#endif /* MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA*/
Jerry Yub7ba49e2022-02-17 14:25:53 +08007787
Gilles Peskine449bd832023-01-11 14:50:10 +01007788void mbedtls_ssl_handshake_wrapup_free_hs_transform(mbedtls_ssl_context *ssl)
Jerry Yuaef00152022-02-17 14:27:31 +08007789{
Gilles Peskine449bd832023-01-11 14:50:10 +01007790 MBEDTLS_SSL_DEBUG_MSG(3, ("=> handshake wrapup: final free"));
Jerry Yuaef00152022-02-17 14:27:31 +08007791
7792 /*
7793 * Free our handshake params
7794 */
Gilles Peskine449bd832023-01-11 14:50:10 +01007795 mbedtls_ssl_handshake_free(ssl);
7796 mbedtls_free(ssl->handshake);
Jerry Yuaef00152022-02-17 14:27:31 +08007797 ssl->handshake = NULL;
7798
7799 /*
Shaun Case8b0ecbc2021-12-20 21:14:10 -08007800 * Free the previous transform and switch in the current one
Jerry Yuaef00152022-02-17 14:27:31 +08007801 */
Gilles Peskine449bd832023-01-11 14:50:10 +01007802 if (ssl->transform) {
7803 mbedtls_ssl_transform_free(ssl->transform);
7804 mbedtls_free(ssl->transform);
Jerry Yuaef00152022-02-17 14:27:31 +08007805 }
7806 ssl->transform = ssl->transform_negotiate;
7807 ssl->transform_negotiate = NULL;
7808
Gilles Peskine449bd832023-01-11 14:50:10 +01007809 MBEDTLS_SSL_DEBUG_MSG(3, ("<= handshake wrapup: final free"));
Jerry Yuaef00152022-02-17 14:27:31 +08007810}
7811
Gilles Peskine449bd832023-01-11 14:50:10 +01007812void mbedtls_ssl_handshake_wrapup(mbedtls_ssl_context *ssl)
Jerry Yu2a9fff52022-02-17 14:28:51 +08007813{
7814 int resume = ssl->handshake->resume;
7815
Gilles Peskine449bd832023-01-11 14:50:10 +01007816 MBEDTLS_SSL_DEBUG_MSG(3, ("=> handshake wrapup"));
Jerry Yu2a9fff52022-02-17 14:28:51 +08007817
7818#if defined(MBEDTLS_SSL_RENEGOTIATION)
Gilles Peskine449bd832023-01-11 14:50:10 +01007819 if (ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS) {
Jerry Yu2a9fff52022-02-17 14:28:51 +08007820 ssl->renego_status = MBEDTLS_SSL_RENEGOTIATION_DONE;
7821 ssl->renego_records_seen = 0;
7822 }
7823#endif
7824
7825 /*
7826 * Free the previous session and switch in the current one
7827 */
Gilles Peskine449bd832023-01-11 14:50:10 +01007828 if (ssl->session) {
Jerry Yu2a9fff52022-02-17 14:28:51 +08007829#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
7830 /* RFC 7366 3.1: keep the EtM state */
7831 ssl->session_negotiate->encrypt_then_mac =
Gilles Peskine449bd832023-01-11 14:50:10 +01007832 ssl->session->encrypt_then_mac;
Jerry Yu2a9fff52022-02-17 14:28:51 +08007833#endif
7834
Gilles Peskine449bd832023-01-11 14:50:10 +01007835 mbedtls_ssl_session_free(ssl->session);
7836 mbedtls_free(ssl->session);
Jerry Yu2a9fff52022-02-17 14:28:51 +08007837 }
7838 ssl->session = ssl->session_negotiate;
7839 ssl->session_negotiate = NULL;
7840
7841 /*
7842 * Add cache entry
7843 */
Gilles Peskine449bd832023-01-11 14:50:10 +01007844 if (ssl->conf->f_set_cache != NULL &&
Jerry Yu2a9fff52022-02-17 14:28:51 +08007845 ssl->session->id_len != 0 &&
Gilles Peskine449bd832023-01-11 14:50:10 +01007846 resume == 0) {
7847 if (ssl->conf->f_set_cache(ssl->conf->p_cache,
7848 ssl->session->id,
7849 ssl->session->id_len,
7850 ssl->session) != 0) {
7851 MBEDTLS_SSL_DEBUG_MSG(1, ("cache did not store session"));
7852 }
Jerry Yu2a9fff52022-02-17 14:28:51 +08007853 }
7854
7855#if defined(MBEDTLS_SSL_PROTO_DTLS)
Gilles Peskine449bd832023-01-11 14:50:10 +01007856 if (ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
7857 ssl->handshake->flight != NULL) {
Jerry Yu2a9fff52022-02-17 14:28:51 +08007858 /* Cancel handshake timer */
Gilles Peskine449bd832023-01-11 14:50:10 +01007859 mbedtls_ssl_set_timer(ssl, 0);
Jerry Yu2a9fff52022-02-17 14:28:51 +08007860
7861 /* Keep last flight around in case we need to resend it:
7862 * we need the handshake and transform structures for that */
Gilles Peskine449bd832023-01-11 14:50:10 +01007863 MBEDTLS_SSL_DEBUG_MSG(3, ("skip freeing handshake and transform"));
7864 } else
Jerry Yu2a9fff52022-02-17 14:28:51 +08007865#endif
Gilles Peskine449bd832023-01-11 14:50:10 +01007866 mbedtls_ssl_handshake_wrapup_free_hs_transform(ssl);
Jerry Yu2a9fff52022-02-17 14:28:51 +08007867
Jerry Yu5ed73ff2022-10-27 13:08:42 +08007868 ssl->state = MBEDTLS_SSL_HANDSHAKE_OVER;
Jerry Yu2a9fff52022-02-17 14:28:51 +08007869
Gilles Peskine449bd832023-01-11 14:50:10 +01007870 MBEDTLS_SSL_DEBUG_MSG(3, ("<= handshake wrapup"));
Jerry Yu2a9fff52022-02-17 14:28:51 +08007871}
7872
Gilles Peskine449bd832023-01-11 14:50:10 +01007873int mbedtls_ssl_write_finished(mbedtls_ssl_context *ssl)
Jerry Yu3c8e47b2022-02-17 14:30:01 +08007874{
7875 int ret, hash_len;
7876
Gilles Peskine449bd832023-01-11 14:50:10 +01007877 MBEDTLS_SSL_DEBUG_MSG(2, ("=> write finished"));
Jerry Yu3c8e47b2022-02-17 14:30:01 +08007878
Gilles Peskine449bd832023-01-11 14:50:10 +01007879 mbedtls_ssl_update_out_pointers(ssl, ssl->transform_negotiate);
Jerry Yu3c8e47b2022-02-17 14:30:01 +08007880
Manuel Pégourié-Gonnardb8b07aa2023-02-06 00:34:21 +01007881 ret = ssl->handshake->calc_finished(ssl, ssl->out_msg + 4, ssl->conf->endpoint);
7882 if (ret != 0) {
7883 MBEDTLS_SSL_DEBUG_RET(1, "calc_finished", ret);
7884 }
Jerry Yu3c8e47b2022-02-17 14:30:01 +08007885
7886 /*
7887 * RFC 5246 7.4.9 (Page 63) says 12 is the default length and ciphersuites
7888 * may define some other value. Currently (early 2016), no defined
7889 * ciphersuite does this (and this is unlikely to change as activity has
7890 * moved to TLS 1.3 now) so we can keep the hardcoded 12 here.
7891 */
7892 hash_len = 12;
7893
7894#if defined(MBEDTLS_SSL_RENEGOTIATION)
7895 ssl->verify_data_len = hash_len;
Gilles Peskine449bd832023-01-11 14:50:10 +01007896 memcpy(ssl->own_verify_data, ssl->out_msg + 4, hash_len);
Jerry Yu3c8e47b2022-02-17 14:30:01 +08007897#endif
7898
7899 ssl->out_msglen = 4 + hash_len;
7900 ssl->out_msgtype = MBEDTLS_SSL_MSG_HANDSHAKE;
7901 ssl->out_msg[0] = MBEDTLS_SSL_HS_FINISHED;
7902
7903 /*
7904 * In case of session resuming, invert the client and server
7905 * ChangeCipherSpec messages order.
7906 */
Gilles Peskine449bd832023-01-11 14:50:10 +01007907 if (ssl->handshake->resume != 0) {
Jerry Yu3c8e47b2022-02-17 14:30:01 +08007908#if defined(MBEDTLS_SSL_CLI_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01007909 if (ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT) {
Jerry Yu3c8e47b2022-02-17 14:30:01 +08007910 ssl->state = MBEDTLS_SSL_HANDSHAKE_WRAPUP;
Gilles Peskine449bd832023-01-11 14:50:10 +01007911 }
Jerry Yu3c8e47b2022-02-17 14:30:01 +08007912#endif
7913#if defined(MBEDTLS_SSL_SRV_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01007914 if (ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER) {
Jerry Yu3c8e47b2022-02-17 14:30:01 +08007915 ssl->state = MBEDTLS_SSL_CLIENT_CHANGE_CIPHER_SPEC;
Gilles Peskine449bd832023-01-11 14:50:10 +01007916 }
Jerry Yu3c8e47b2022-02-17 14:30:01 +08007917#endif
Gilles Peskine449bd832023-01-11 14:50:10 +01007918 } else {
Jerry Yu3c8e47b2022-02-17 14:30:01 +08007919 ssl->state++;
Gilles Peskine449bd832023-01-11 14:50:10 +01007920 }
Jerry Yu3c8e47b2022-02-17 14:30:01 +08007921
7922 /*
7923 * Switch to our negotiated transform and session parameters for outbound
7924 * data.
7925 */
Gilles Peskine449bd832023-01-11 14:50:10 +01007926 MBEDTLS_SSL_DEBUG_MSG(3, ("switching to new transform spec for outbound data"));
Jerry Yu3c8e47b2022-02-17 14:30:01 +08007927
7928#if defined(MBEDTLS_SSL_PROTO_DTLS)
Gilles Peskine449bd832023-01-11 14:50:10 +01007929 if (ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM) {
Jerry Yu3c8e47b2022-02-17 14:30:01 +08007930 unsigned char i;
7931
7932 /* Remember current epoch settings for resending */
7933 ssl->handshake->alt_transform_out = ssl->transform_out;
Gilles Peskine449bd832023-01-11 14:50:10 +01007934 memcpy(ssl->handshake->alt_out_ctr, ssl->cur_out_ctr,
7935 sizeof(ssl->handshake->alt_out_ctr));
Jerry Yu3c8e47b2022-02-17 14:30:01 +08007936
7937 /* Set sequence_number to zero */
Gilles Peskine449bd832023-01-11 14:50:10 +01007938 memset(&ssl->cur_out_ctr[2], 0, sizeof(ssl->cur_out_ctr) - 2);
Jerry Yu3c8e47b2022-02-17 14:30:01 +08007939
7940
7941 /* Increment epoch */
Gilles Peskine449bd832023-01-11 14:50:10 +01007942 for (i = 2; i > 0; i--) {
7943 if (++ssl->cur_out_ctr[i - 1] != 0) {
Jerry Yu3c8e47b2022-02-17 14:30:01 +08007944 break;
Gilles Peskine449bd832023-01-11 14:50:10 +01007945 }
7946 }
Jerry Yu3c8e47b2022-02-17 14:30:01 +08007947
7948 /* The loop goes to its end iff the counter is wrapping */
Gilles Peskine449bd832023-01-11 14:50:10 +01007949 if (i == 0) {
7950 MBEDTLS_SSL_DEBUG_MSG(1, ("DTLS epoch would wrap"));
7951 return MBEDTLS_ERR_SSL_COUNTER_WRAPPING;
Jerry Yu3c8e47b2022-02-17 14:30:01 +08007952 }
Gilles Peskine449bd832023-01-11 14:50:10 +01007953 } else
Jerry Yu3c8e47b2022-02-17 14:30:01 +08007954#endif /* MBEDTLS_SSL_PROTO_DTLS */
Gilles Peskine449bd832023-01-11 14:50:10 +01007955 memset(ssl->cur_out_ctr, 0, sizeof(ssl->cur_out_ctr));
Jerry Yu3c8e47b2022-02-17 14:30:01 +08007956
7957 ssl->transform_out = ssl->transform_negotiate;
7958 ssl->session_out = ssl->session_negotiate;
7959
7960#if defined(MBEDTLS_SSL_PROTO_DTLS)
Gilles Peskine449bd832023-01-11 14:50:10 +01007961 if (ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM) {
7962 mbedtls_ssl_send_flight_completed(ssl);
7963 }
Jerry Yu3c8e47b2022-02-17 14:30:01 +08007964#endif
7965
Gilles Peskine449bd832023-01-11 14:50:10 +01007966 if ((ret = mbedtls_ssl_write_handshake_msg(ssl)) != 0) {
7967 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ssl_write_handshake_msg", ret);
7968 return ret;
Jerry Yu3c8e47b2022-02-17 14:30:01 +08007969 }
7970
7971#if defined(MBEDTLS_SSL_PROTO_DTLS)
Gilles Peskine449bd832023-01-11 14:50:10 +01007972 if (ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
7973 (ret = mbedtls_ssl_flight_transmit(ssl)) != 0) {
7974 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ssl_flight_transmit", ret);
7975 return ret;
Jerry Yu3c8e47b2022-02-17 14:30:01 +08007976 }
7977#endif
7978
Gilles Peskine449bd832023-01-11 14:50:10 +01007979 MBEDTLS_SSL_DEBUG_MSG(2, ("<= write finished"));
Jerry Yu3c8e47b2022-02-17 14:30:01 +08007980
Gilles Peskine449bd832023-01-11 14:50:10 +01007981 return 0;
Jerry Yu3c8e47b2022-02-17 14:30:01 +08007982}
7983
Jerry Yu0b3d7c12022-02-17 14:30:51 +08007984#define SSL_MAX_HASH_LEN 12
7985
Gilles Peskine449bd832023-01-11 14:50:10 +01007986int mbedtls_ssl_parse_finished(mbedtls_ssl_context *ssl)
Jerry Yu0b3d7c12022-02-17 14:30:51 +08007987{
7988 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
7989 unsigned int hash_len = 12;
7990 unsigned char buf[SSL_MAX_HASH_LEN];
7991
Gilles Peskine449bd832023-01-11 14:50:10 +01007992 MBEDTLS_SSL_DEBUG_MSG(2, ("=> parse finished"));
Jerry Yu0b3d7c12022-02-17 14:30:51 +08007993
Manuel Pégourié-Gonnardb8b07aa2023-02-06 00:34:21 +01007994 ret = ssl->handshake->calc_finished(ssl, buf, ssl->conf->endpoint ^ 1);
7995 if (ret != 0) {
7996 MBEDTLS_SSL_DEBUG_RET(1, "calc_finished", ret);
7997 }
Jerry Yu0b3d7c12022-02-17 14:30:51 +08007998
Gilles Peskine449bd832023-01-11 14:50:10 +01007999 if ((ret = mbedtls_ssl_read_record(ssl, 1)) != 0) {
8000 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ssl_read_record", ret);
Jerry Yu0b3d7c12022-02-17 14:30:51 +08008001 goto exit;
8002 }
8003
Gilles Peskine449bd832023-01-11 14:50:10 +01008004 if (ssl->in_msgtype != MBEDTLS_SSL_MSG_HANDSHAKE) {
8005 MBEDTLS_SSL_DEBUG_MSG(1, ("bad finished message"));
8006 mbedtls_ssl_send_alert_message(ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
8007 MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE);
Jerry Yu0b3d7c12022-02-17 14:30:51 +08008008 ret = MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE;
8009 goto exit;
8010 }
8011
Gilles Peskine449bd832023-01-11 14:50:10 +01008012 if (ssl->in_msg[0] != MBEDTLS_SSL_HS_FINISHED) {
8013 mbedtls_ssl_send_alert_message(ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
8014 MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE);
Jerry Yu0b3d7c12022-02-17 14:30:51 +08008015 ret = MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE;
8016 goto exit;
8017 }
8018
Gilles Peskine449bd832023-01-11 14:50:10 +01008019 if (ssl->in_hslen != mbedtls_ssl_hs_hdr_len(ssl) + hash_len) {
8020 MBEDTLS_SSL_DEBUG_MSG(1, ("bad finished message"));
8021 mbedtls_ssl_send_alert_message(ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
8022 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR);
Jerry Yu0b3d7c12022-02-17 14:30:51 +08008023 ret = MBEDTLS_ERR_SSL_DECODE_ERROR;
8024 goto exit;
8025 }
8026
Gilles Peskine449bd832023-01-11 14:50:10 +01008027 if (mbedtls_ct_memcmp(ssl->in_msg + mbedtls_ssl_hs_hdr_len(ssl),
8028 buf, hash_len) != 0) {
8029 MBEDTLS_SSL_DEBUG_MSG(1, ("bad finished message"));
8030 mbedtls_ssl_send_alert_message(ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
8031 MBEDTLS_SSL_ALERT_MSG_DECRYPT_ERROR);
Jerry Yu0b3d7c12022-02-17 14:30:51 +08008032 ret = MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE;
8033 goto exit;
8034 }
8035
8036#if defined(MBEDTLS_SSL_RENEGOTIATION)
8037 ssl->verify_data_len = hash_len;
Gilles Peskine449bd832023-01-11 14:50:10 +01008038 memcpy(ssl->peer_verify_data, buf, hash_len);
Jerry Yu0b3d7c12022-02-17 14:30:51 +08008039#endif
8040
Gilles Peskine449bd832023-01-11 14:50:10 +01008041 if (ssl->handshake->resume != 0) {
Jerry Yu0b3d7c12022-02-17 14:30:51 +08008042#if defined(MBEDTLS_SSL_CLI_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01008043 if (ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT) {
Jerry Yu0b3d7c12022-02-17 14:30:51 +08008044 ssl->state = MBEDTLS_SSL_CLIENT_CHANGE_CIPHER_SPEC;
Gilles Peskine449bd832023-01-11 14:50:10 +01008045 }
Jerry Yu0b3d7c12022-02-17 14:30:51 +08008046#endif
8047#if defined(MBEDTLS_SSL_SRV_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01008048 if (ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER) {
Jerry Yu0b3d7c12022-02-17 14:30:51 +08008049 ssl->state = MBEDTLS_SSL_HANDSHAKE_WRAPUP;
Gilles Peskine449bd832023-01-11 14:50:10 +01008050 }
Jerry Yu0b3d7c12022-02-17 14:30:51 +08008051#endif
Gilles Peskine449bd832023-01-11 14:50:10 +01008052 } else {
Jerry Yu0b3d7c12022-02-17 14:30:51 +08008053 ssl->state++;
Gilles Peskine449bd832023-01-11 14:50:10 +01008054 }
Jerry Yu0b3d7c12022-02-17 14:30:51 +08008055
8056#if defined(MBEDTLS_SSL_PROTO_DTLS)
Gilles Peskine449bd832023-01-11 14:50:10 +01008057 if (ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM) {
8058 mbedtls_ssl_recv_flight_completed(ssl);
8059 }
Jerry Yu0b3d7c12022-02-17 14:30:51 +08008060#endif
8061
Gilles Peskine449bd832023-01-11 14:50:10 +01008062 MBEDTLS_SSL_DEBUG_MSG(2, ("<= parse finished"));
Jerry Yu0b3d7c12022-02-17 14:30:51 +08008063
8064exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01008065 mbedtls_platform_zeroize(buf, hash_len);
8066 return ret;
Jerry Yu0b3d7c12022-02-17 14:30:51 +08008067}
8068
Jerry Yu392112c2022-02-17 14:34:10 +08008069#if defined(MBEDTLS_SSL_CONTEXT_SERIALIZATION)
8070/*
8071 * Helper to get TLS 1.2 PRF from ciphersuite
8072 * (Duplicates bits of logic from ssl_set_handshake_prfs().)
8073 */
Gilles Peskine449bd832023-01-11 14:50:10 +01008074static tls_prf_fn ssl_tls12prf_from_cs(int ciphersuite_id)
Jerry Yu392112c2022-02-17 14:34:10 +08008075{
Jerry Yu392112c2022-02-17 14:34:10 +08008076 const mbedtls_ssl_ciphersuite_t * const ciphersuite_info =
Gilles Peskine449bd832023-01-11 14:50:10 +01008077 mbedtls_ssl_ciphersuite_from_id(ciphersuite_id);
Andrzej Kurek68327742022-10-03 06:18:18 -04008078#if defined(MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Gilles Peskine449bd832023-01-11 14:50:10 +01008079 if (ciphersuite_info != NULL && ciphersuite_info->mac == MBEDTLS_MD_SHA384) {
8080 return tls_prf_sha384;
8081 } else
Jerry Yu392112c2022-02-17 14:34:10 +08008082#endif
Andrzej Kurek894edde2022-09-29 06:31:14 -04008083#if defined(MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
8084 {
Gilles Peskine449bd832023-01-11 14:50:10 +01008085 if (ciphersuite_info != NULL && ciphersuite_info->mac == MBEDTLS_MD_SHA256) {
8086 return tls_prf_sha256;
8087 }
Andrzej Kurek894edde2022-09-29 06:31:14 -04008088 }
8089#endif
8090#if !defined(MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA) && \
8091 !defined(MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
8092 (void) ciphersuite_info;
8093#endif
Andrzej Kurekeabeb302022-10-17 07:52:51 -04008094
Gilles Peskine449bd832023-01-11 14:50:10 +01008095 return NULL;
Jerry Yu392112c2022-02-17 14:34:10 +08008096}
8097#endif /* MBEDTLS_SSL_CONTEXT_SERIALIZATION */
Jerry Yue93ffcd2022-02-17 14:37:06 +08008098
Gilles Peskine449bd832023-01-11 14:50:10 +01008099static mbedtls_tls_prf_types tls_prf_get_type(mbedtls_ssl_tls_prf_cb *tls_prf)
Jerry Yue93ffcd2022-02-17 14:37:06 +08008100{
8101 ((void) tls_prf);
Andrzej Kurek25f27152022-08-17 16:09:31 -04008102#if defined(MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Gilles Peskine449bd832023-01-11 14:50:10 +01008103 if (tls_prf == tls_prf_sha384) {
8104 return MBEDTLS_SSL_TLS_PRF_SHA384;
8105 } else
Jerry Yue93ffcd2022-02-17 14:37:06 +08008106#endif
Andrzej Kurek25f27152022-08-17 16:09:31 -04008107#if defined(MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Gilles Peskine449bd832023-01-11 14:50:10 +01008108 if (tls_prf == tls_prf_sha256) {
8109 return MBEDTLS_SSL_TLS_PRF_SHA256;
8110 } else
Jerry Yue93ffcd2022-02-17 14:37:06 +08008111#endif
Gilles Peskine449bd832023-01-11 14:50:10 +01008112 return MBEDTLS_SSL_TLS_PRF_NONE;
Jerry Yue93ffcd2022-02-17 14:37:06 +08008113}
8114
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008115/*
8116 * Populate a transform structure with session keys and all the other
8117 * necessary information.
8118 *
8119 * Parameters:
8120 * - [in/out]: transform: structure to populate
8121 * [in] must be just initialised with mbedtls_ssl_transform_init()
8122 * [out] fully populated, ready for use by mbedtls_ssl_{en,de}crypt_buf()
8123 * - [in] ciphersuite
8124 * - [in] master
8125 * - [in] encrypt_then_mac
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008126 * - [in] tls_prf: pointer to PRF to use for key derivation
8127 * - [in] randbytes: buffer holding ServerHello.random + ClientHello.random
Glenn Strauss07c64162022-03-14 12:34:51 -04008128 * - [in] tls_version: TLS version
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008129 * - [in] endpoint: client or server
8130 * - [in] ssl: used for:
8131 * - ssl->conf->{f,p}_export_keys
8132 * [in] optionally used for:
8133 * - MBEDTLS_DEBUG_C: ssl->conf->{f,p}_dbg
8134 */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02008135MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01008136static int ssl_tls12_populate_transform(mbedtls_ssl_transform *transform,
8137 int ciphersuite,
8138 const unsigned char master[48],
Neil Armstrongf2c82f02022-04-05 11:16:53 +02008139#if defined(MBEDTLS_SSL_SOME_SUITES_USE_CBC_ETM)
Gilles Peskine449bd832023-01-11 14:50:10 +01008140 int encrypt_then_mac,
Neil Armstrongf2c82f02022-04-05 11:16:53 +02008141#endif /* MBEDTLS_SSL_SOME_SUITES_USE_CBC_ETM */
Gilles Peskine449bd832023-01-11 14:50:10 +01008142 ssl_tls_prf_t tls_prf,
8143 const unsigned char randbytes[64],
8144 mbedtls_ssl_protocol_version tls_version,
8145 unsigned endpoint,
8146 const mbedtls_ssl_context *ssl)
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008147{
8148 int ret = 0;
8149 unsigned char keyblk[256];
8150 unsigned char *key1;
8151 unsigned char *key2;
8152 unsigned char *mac_enc;
8153 unsigned char *mac_dec;
8154 size_t mac_key_len = 0;
8155 size_t iv_copy_len;
8156 size_t keylen;
8157 const mbedtls_ssl_ciphersuite_t *ciphersuite_info;
Neil Armstrong7fea33e2022-04-01 15:40:25 +02008158 mbedtls_ssl_mode_t ssl_mode;
Neil Armstronge4512952022-03-08 09:08:22 +01008159#if !defined(MBEDTLS_USE_PSA_CRYPTO)
Neil Armstronga0eeb7f2022-04-01 17:36:10 +02008160 const mbedtls_cipher_info_t *cipher_info;
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008161 const mbedtls_md_info_t *md_info;
Neil Armstronge4512952022-03-08 09:08:22 +01008162#endif /* !MBEDTLS_USE_PSA_CRYPTO */
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008163
8164#if defined(MBEDTLS_USE_PSA_CRYPTO)
8165 psa_key_type_t key_type;
8166 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
8167 psa_algorithm_t alg;
Neil Armstronge4512952022-03-08 09:08:22 +01008168 psa_algorithm_t mac_alg = 0;
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008169 size_t key_bits;
8170 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
8171#endif
8172
8173#if !defined(MBEDTLS_DEBUG_C) && \
8174 !defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Gilles Peskine449bd832023-01-11 14:50:10 +01008175 if (ssl->f_export_keys == NULL) {
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008176 ssl = NULL; /* make sure we don't use it except for these cases */
8177 (void) ssl;
8178 }
8179#endif
8180
8181 /*
8182 * Some data just needs copying into the structure
8183 */
Neil Armstrongf2c82f02022-04-05 11:16:53 +02008184#if defined(MBEDTLS_SSL_SOME_SUITES_USE_CBC_ETM)
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008185 transform->encrypt_then_mac = encrypt_then_mac;
Neil Armstrongf2c82f02022-04-05 11:16:53 +02008186#endif /* MBEDTLS_SSL_SOME_SUITES_USE_CBC_ETM */
Glenn Strauss07c64162022-03-14 12:34:51 -04008187 transform->tls_version = tls_version;
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008188
8189#if defined(MBEDTLS_SSL_CONTEXT_SERIALIZATION)
Gilles Peskine449bd832023-01-11 14:50:10 +01008190 memcpy(transform->randbytes, randbytes, sizeof(transform->randbytes));
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008191#endif
8192
8193#if defined(MBEDTLS_SSL_PROTO_TLS1_3)
Gilles Peskine449bd832023-01-11 14:50:10 +01008194 if (tls_version == MBEDTLS_SSL_VERSION_TLS1_3) {
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008195 /* At the moment, we keep TLS <= 1.2 and TLS 1.3 transform
8196 * generation separate. This should never happen. */
Gilles Peskine449bd832023-01-11 14:50:10 +01008197 return MBEDTLS_ERR_SSL_INTERNAL_ERROR;
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008198 }
8199#endif /* MBEDTLS_SSL_PROTO_TLS1_3 */
8200
8201 /*
8202 * Get various info structures
8203 */
Gilles Peskine449bd832023-01-11 14:50:10 +01008204 ciphersuite_info = mbedtls_ssl_ciphersuite_from_id(ciphersuite);
8205 if (ciphersuite_info == NULL) {
8206 MBEDTLS_SSL_DEBUG_MSG(1, ("ciphersuite info for %d not found",
8207 ciphersuite));
8208 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008209 }
8210
Neil Armstrongab555e02022-04-04 11:07:59 +02008211 ssl_mode = mbedtls_ssl_get_mode_from_ciphersuite(
Neil Armstrongf2c82f02022-04-05 11:16:53 +02008212#if defined(MBEDTLS_SSL_SOME_SUITES_USE_CBC_ETM)
Gilles Peskine449bd832023-01-11 14:50:10 +01008213 encrypt_then_mac,
Neil Armstrongf2c82f02022-04-05 11:16:53 +02008214#endif /* MBEDTLS_SSL_SOME_SUITES_USE_CBC_ETM */
Gilles Peskine449bd832023-01-11 14:50:10 +01008215 ciphersuite_info);
Neil Armstrong7fea33e2022-04-01 15:40:25 +02008216
Gilles Peskine449bd832023-01-11 14:50:10 +01008217 if (ssl_mode == MBEDTLS_SSL_MODE_AEAD) {
Neil Armstronga0eeb7f2022-04-01 17:36:10 +02008218 transform->taglen =
8219 ciphersuite_info->flags & MBEDTLS_CIPHERSUITE_SHORT_TAG ? 8 : 16;
Gilles Peskine449bd832023-01-11 14:50:10 +01008220 }
Neil Armstronga0eeb7f2022-04-01 17:36:10 +02008221
8222#if defined(MBEDTLS_USE_PSA_CRYPTO)
Gilles Peskine449bd832023-01-11 14:50:10 +01008223 if ((status = mbedtls_ssl_cipher_to_psa(ciphersuite_info->cipher,
8224 transform->taglen,
8225 &alg,
8226 &key_type,
8227 &key_bits)) != PSA_SUCCESS) {
8228 ret = psa_ssl_status_to_mbedtls(status);
8229 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ssl_cipher_to_psa", ret);
Neil Armstronga0eeb7f2022-04-01 17:36:10 +02008230 goto end;
8231 }
8232#else
Gilles Peskine449bd832023-01-11 14:50:10 +01008233 cipher_info = mbedtls_cipher_info_from_type(ciphersuite_info->cipher);
8234 if (cipher_info == NULL) {
8235 MBEDTLS_SSL_DEBUG_MSG(1, ("cipher info for %u not found",
8236 ciphersuite_info->cipher));
8237 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008238 }
Neil Armstronga0eeb7f2022-04-01 17:36:10 +02008239#endif /* MBEDTLS_USE_PSA_CRYPTO */
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008240
Neil Armstronge4512952022-03-08 09:08:22 +01008241#if defined(MBEDTLS_USE_PSA_CRYPTO)
Gilles Peskine449bd832023-01-11 14:50:10 +01008242 mac_alg = mbedtls_hash_info_psa_from_md(ciphersuite_info->mac);
8243 if (mac_alg == 0) {
8244 MBEDTLS_SSL_DEBUG_MSG(1, ("mbedtls_hash_info_psa_from_md for %u not found",
8245 (unsigned) ciphersuite_info->mac));
8246 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
Neil Armstronge4512952022-03-08 09:08:22 +01008247 }
8248#else
Gilles Peskine449bd832023-01-11 14:50:10 +01008249 md_info = mbedtls_md_info_from_type(ciphersuite_info->mac);
8250 if (md_info == NULL) {
8251 MBEDTLS_SSL_DEBUG_MSG(1, ("mbedtls_md info for %u not found",
8252 (unsigned) ciphersuite_info->mac));
8253 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008254 }
Neil Armstronge4512952022-03-08 09:08:22 +01008255#endif /* MBEDTLS_USE_PSA_CRYPTO */
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008256
8257#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
8258 /* Copy own and peer's CID if the use of the CID
8259 * extension has been negotiated. */
Gilles Peskine449bd832023-01-11 14:50:10 +01008260 if (ssl->handshake->cid_in_use == MBEDTLS_SSL_CID_ENABLED) {
8261 MBEDTLS_SSL_DEBUG_MSG(3, ("Copy CIDs into SSL transform"));
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008262
8263 transform->in_cid_len = ssl->own_cid_len;
Gilles Peskine449bd832023-01-11 14:50:10 +01008264 memcpy(transform->in_cid, ssl->own_cid, ssl->own_cid_len);
8265 MBEDTLS_SSL_DEBUG_BUF(3, "Incoming CID", transform->in_cid,
8266 transform->in_cid_len);
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008267
8268 transform->out_cid_len = ssl->handshake->peer_cid_len;
Gilles Peskine449bd832023-01-11 14:50:10 +01008269 memcpy(transform->out_cid, ssl->handshake->peer_cid,
8270 ssl->handshake->peer_cid_len);
8271 MBEDTLS_SSL_DEBUG_BUF(3, "Outgoing CID", transform->out_cid,
8272 transform->out_cid_len);
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008273 }
8274#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
8275
8276 /*
8277 * Compute key block using the PRF
8278 */
Gilles Peskine449bd832023-01-11 14:50:10 +01008279 ret = tls_prf(master, 48, "key expansion", randbytes, 64, keyblk, 256);
8280 if (ret != 0) {
8281 MBEDTLS_SSL_DEBUG_RET(1, "prf", ret);
8282 return ret;
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008283 }
8284
Gilles Peskine449bd832023-01-11 14:50:10 +01008285 MBEDTLS_SSL_DEBUG_MSG(3, ("ciphersuite = %s",
8286 mbedtls_ssl_get_ciphersuite_name(ciphersuite)));
8287 MBEDTLS_SSL_DEBUG_BUF(3, "master secret", master, 48);
8288 MBEDTLS_SSL_DEBUG_BUF(4, "random bytes", randbytes, 64);
8289 MBEDTLS_SSL_DEBUG_BUF(4, "key block", keyblk, 256);
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008290
8291 /*
8292 * Determine the appropriate key, IV and MAC length.
8293 */
8294
Neil Armstronga0eeb7f2022-04-01 17:36:10 +02008295#if defined(MBEDTLS_USE_PSA_CRYPTO)
8296 keylen = PSA_BITS_TO_BYTES(key_bits);
8297#else
Gilles Peskine449bd832023-01-11 14:50:10 +01008298 keylen = mbedtls_cipher_info_get_key_bitlen(cipher_info) / 8;
Neil Armstronga0eeb7f2022-04-01 17:36:10 +02008299#endif
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008300
8301#if defined(MBEDTLS_GCM_C) || \
8302 defined(MBEDTLS_CCM_C) || \
8303 defined(MBEDTLS_CHACHAPOLY_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01008304 if (ssl_mode == MBEDTLS_SSL_MODE_AEAD) {
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008305 size_t explicit_ivlen;
8306
8307 transform->maclen = 0;
8308 mac_key_len = 0;
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008309
8310 /* All modes haves 96-bit IVs, but the length of the static parts vary
8311 * with mode and version:
8312 * - For GCM and CCM in TLS 1.2, there's a static IV of 4 Bytes
8313 * (to be concatenated with a dynamically chosen IV of 8 Bytes)
8314 * - For ChaChaPoly in TLS 1.2, and all modes in TLS 1.3, there's
8315 * a static IV of 12 Bytes (to be XOR'ed with the 8 Byte record
8316 * sequence number).
8317 */
8318 transform->ivlen = 12;
David Horstmann3b2276a2022-10-06 14:49:08 +01008319
8320 int is_chachapoly = 0;
Neil Armstronga0eeb7f2022-04-01 17:36:10 +02008321#if defined(MBEDTLS_USE_PSA_CRYPTO)
Gilles Peskine449bd832023-01-11 14:50:10 +01008322 is_chachapoly = (key_type == PSA_KEY_TYPE_CHACHA20);
Neil Armstronga0eeb7f2022-04-01 17:36:10 +02008323#else
Gilles Peskine449bd832023-01-11 14:50:10 +01008324 is_chachapoly = (mbedtls_cipher_info_get_mode(cipher_info)
8325 == MBEDTLS_MODE_CHACHAPOLY);
Neil Armstronga0eeb7f2022-04-01 17:36:10 +02008326#endif /* MBEDTLS_USE_PSA_CRYPTO */
David Horstmann3b2276a2022-10-06 14:49:08 +01008327
Gilles Peskine449bd832023-01-11 14:50:10 +01008328 if (is_chachapoly) {
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008329 transform->fixed_ivlen = 12;
Gilles Peskine449bd832023-01-11 14:50:10 +01008330 } else {
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008331 transform->fixed_ivlen = 4;
Gilles Peskine449bd832023-01-11 14:50:10 +01008332 }
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008333
8334 /* Minimum length of encrypted record */
8335 explicit_ivlen = transform->ivlen - transform->fixed_ivlen;
8336 transform->minlen = explicit_ivlen + transform->taglen;
Gilles Peskine449bd832023-01-11 14:50:10 +01008337 } else
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008338#endif /* MBEDTLS_GCM_C || MBEDTLS_CCM_C || MBEDTLS_CHACHAPOLY_C */
8339#if defined(MBEDTLS_SSL_SOME_SUITES_USE_MAC)
Gilles Peskine449bd832023-01-11 14:50:10 +01008340 if (ssl_mode == MBEDTLS_SSL_MODE_STREAM ||
Neil Armstrong7fea33e2022-04-01 15:40:25 +02008341 ssl_mode == MBEDTLS_SSL_MODE_CBC ||
Gilles Peskine449bd832023-01-11 14:50:10 +01008342 ssl_mode == MBEDTLS_SSL_MODE_CBC_ETM) {
Neil Armstronge4512952022-03-08 09:08:22 +01008343#if defined(MBEDTLS_USE_PSA_CRYPTO)
Gilles Peskine449bd832023-01-11 14:50:10 +01008344 size_t block_size = PSA_BLOCK_CIPHER_BLOCK_LENGTH(key_type);
Neil Armstronga0eeb7f2022-04-01 17:36:10 +02008345#else
8346 size_t block_size = cipher_info->block_size;
8347#endif /* MBEDTLS_USE_PSA_CRYPTO */
8348
8349#if defined(MBEDTLS_USE_PSA_CRYPTO)
Neil Armstronge4512952022-03-08 09:08:22 +01008350 /* Get MAC length */
8351 mac_key_len = PSA_HASH_LENGTH(mac_alg);
8352#else
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008353 /* Initialize HMAC contexts */
Gilles Peskine449bd832023-01-11 14:50:10 +01008354 if ((ret = mbedtls_md_setup(&transform->md_ctx_enc, md_info, 1)) != 0 ||
8355 (ret = mbedtls_md_setup(&transform->md_ctx_dec, md_info, 1)) != 0) {
8356 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_md_setup", ret);
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008357 goto end;
8358 }
8359
8360 /* Get MAC length */
Gilles Peskine449bd832023-01-11 14:50:10 +01008361 mac_key_len = mbedtls_md_get_size(md_info);
Neil Armstronge4512952022-03-08 09:08:22 +01008362#endif /* MBEDTLS_USE_PSA_CRYPTO */
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008363 transform->maclen = mac_key_len;
8364
8365 /* IV length */
Neil Armstronga0eeb7f2022-04-01 17:36:10 +02008366#if defined(MBEDTLS_USE_PSA_CRYPTO)
Gilles Peskine449bd832023-01-11 14:50:10 +01008367 transform->ivlen = PSA_CIPHER_IV_LENGTH(key_type, alg);
Neil Armstronga0eeb7f2022-04-01 17:36:10 +02008368#else
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008369 transform->ivlen = cipher_info->iv_size;
Neil Armstronga0eeb7f2022-04-01 17:36:10 +02008370#endif /* MBEDTLS_USE_PSA_CRYPTO */
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008371
8372 /* Minimum length */
Gilles Peskine449bd832023-01-11 14:50:10 +01008373 if (ssl_mode == MBEDTLS_SSL_MODE_STREAM) {
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008374 transform->minlen = transform->maclen;
Gilles Peskine449bd832023-01-11 14:50:10 +01008375 } else {
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008376 /*
8377 * GenericBlockCipher:
8378 * 1. if EtM is in use: one block plus MAC
8379 * otherwise: * first multiple of blocklen greater than maclen
8380 * 2. IV
8381 */
8382#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
Gilles Peskine449bd832023-01-11 14:50:10 +01008383 if (ssl_mode == MBEDTLS_SSL_MODE_CBC_ETM) {
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008384 transform->minlen = transform->maclen
Gilles Peskine449bd832023-01-11 14:50:10 +01008385 + block_size;
8386 } else
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008387#endif
8388 {
8389 transform->minlen = transform->maclen
Gilles Peskine449bd832023-01-11 14:50:10 +01008390 + block_size
8391 - transform->maclen % block_size;
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008392 }
8393
Gilles Peskine449bd832023-01-11 14:50:10 +01008394 if (tls_version == MBEDTLS_SSL_VERSION_TLS1_2) {
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008395 transform->minlen += transform->ivlen;
Gilles Peskine449bd832023-01-11 14:50:10 +01008396 } else {
8397 MBEDTLS_SSL_DEBUG_MSG(1, ("should never happen"));
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008398 ret = MBEDTLS_ERR_SSL_INTERNAL_ERROR;
8399 goto end;
8400 }
8401 }
Gilles Peskine449bd832023-01-11 14:50:10 +01008402 } else
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008403#endif /* MBEDTLS_SSL_SOME_SUITES_USE_MAC */
8404 {
Gilles Peskine449bd832023-01-11 14:50:10 +01008405 MBEDTLS_SSL_DEBUG_MSG(1, ("should never happen"));
8406 return MBEDTLS_ERR_SSL_INTERNAL_ERROR;
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008407 }
8408
Gilles Peskine449bd832023-01-11 14:50:10 +01008409 MBEDTLS_SSL_DEBUG_MSG(3, ("keylen: %u, minlen: %u, ivlen: %u, maclen: %u",
8410 (unsigned) keylen,
8411 (unsigned) transform->minlen,
8412 (unsigned) transform->ivlen,
8413 (unsigned) transform->maclen));
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008414
8415 /*
8416 * Finally setup the cipher contexts, IVs and MAC secrets.
8417 */
8418#if defined(MBEDTLS_SSL_CLI_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01008419 if (endpoint == MBEDTLS_SSL_IS_CLIENT) {
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008420 key1 = keyblk + mac_key_len * 2;
8421 key2 = keyblk + mac_key_len * 2 + keylen;
8422
8423 mac_enc = keyblk;
8424 mac_dec = keyblk + mac_key_len;
8425
Gilles Peskine449bd832023-01-11 14:50:10 +01008426 iv_copy_len = (transform->fixed_ivlen) ?
8427 transform->fixed_ivlen : transform->ivlen;
8428 memcpy(transform->iv_enc, key2 + keylen, iv_copy_len);
8429 memcpy(transform->iv_dec, key2 + keylen + iv_copy_len,
8430 iv_copy_len);
8431 } else
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008432#endif /* MBEDTLS_SSL_CLI_C */
8433#if defined(MBEDTLS_SSL_SRV_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01008434 if (endpoint == MBEDTLS_SSL_IS_SERVER) {
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008435 key1 = keyblk + mac_key_len * 2 + keylen;
8436 key2 = keyblk + mac_key_len * 2;
8437
8438 mac_enc = keyblk + mac_key_len;
8439 mac_dec = keyblk;
8440
Gilles Peskine449bd832023-01-11 14:50:10 +01008441 iv_copy_len = (transform->fixed_ivlen) ?
8442 transform->fixed_ivlen : transform->ivlen;
8443 memcpy(transform->iv_dec, key1 + keylen, iv_copy_len);
8444 memcpy(transform->iv_enc, key1 + keylen + iv_copy_len,
8445 iv_copy_len);
8446 } else
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008447#endif /* MBEDTLS_SSL_SRV_C */
8448 {
Gilles Peskine449bd832023-01-11 14:50:10 +01008449 MBEDTLS_SSL_DEBUG_MSG(1, ("should never happen"));
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008450 ret = MBEDTLS_ERR_SSL_INTERNAL_ERROR;
8451 goto end;
8452 }
8453
Gilles Peskine449bd832023-01-11 14:50:10 +01008454 if (ssl != NULL && ssl->f_export_keys != NULL) {
8455 ssl->f_export_keys(ssl->p_export_keys,
8456 MBEDTLS_SSL_KEY_EXPORT_TLS12_MASTER_SECRET,
8457 master, 48,
8458 randbytes + 32,
8459 randbytes,
8460 tls_prf_get_type(tls_prf));
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008461 }
8462
8463#if defined(MBEDTLS_USE_PSA_CRYPTO)
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008464 transform->psa_alg = alg;
8465
Gilles Peskine449bd832023-01-11 14:50:10 +01008466 if (alg != MBEDTLS_SSL_NULL_CIPHER) {
8467 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_ENCRYPT);
8468 psa_set_key_algorithm(&attributes, alg);
8469 psa_set_key_type(&attributes, key_type);
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008470
Gilles Peskine449bd832023-01-11 14:50:10 +01008471 if ((status = psa_import_key(&attributes,
8472 key1,
8473 PSA_BITS_TO_BYTES(key_bits),
8474 &transform->psa_key_enc)) != PSA_SUCCESS) {
8475 MBEDTLS_SSL_DEBUG_RET(3, "psa_import_key", (int) status);
8476 ret = psa_ssl_status_to_mbedtls(status);
8477 MBEDTLS_SSL_DEBUG_RET(1, "psa_import_key", ret);
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008478 goto end;
8479 }
8480
Gilles Peskine449bd832023-01-11 14:50:10 +01008481 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_DECRYPT);
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008482
Gilles Peskine449bd832023-01-11 14:50:10 +01008483 if ((status = psa_import_key(&attributes,
8484 key2,
8485 PSA_BITS_TO_BYTES(key_bits),
8486 &transform->psa_key_dec)) != PSA_SUCCESS) {
8487 ret = psa_ssl_status_to_mbedtls(status);
8488 MBEDTLS_SSL_DEBUG_RET(1, "psa_import_key", ret);
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008489 goto end;
8490 }
8491 }
8492#else
Gilles Peskine449bd832023-01-11 14:50:10 +01008493 if ((ret = mbedtls_cipher_setup(&transform->cipher_ctx_enc,
8494 cipher_info)) != 0) {
8495 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_cipher_setup", ret);
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008496 goto end;
8497 }
8498
Gilles Peskine449bd832023-01-11 14:50:10 +01008499 if ((ret = mbedtls_cipher_setup(&transform->cipher_ctx_dec,
8500 cipher_info)) != 0) {
8501 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_cipher_setup", ret);
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008502 goto end;
8503 }
8504
Gilles Peskine449bd832023-01-11 14:50:10 +01008505 if ((ret = mbedtls_cipher_setkey(&transform->cipher_ctx_enc, key1,
8506 (int) mbedtls_cipher_info_get_key_bitlen(cipher_info),
8507 MBEDTLS_ENCRYPT)) != 0) {
8508 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_cipher_setkey", ret);
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008509 goto end;
8510 }
8511
Gilles Peskine449bd832023-01-11 14:50:10 +01008512 if ((ret = mbedtls_cipher_setkey(&transform->cipher_ctx_dec, key2,
8513 (int) mbedtls_cipher_info_get_key_bitlen(cipher_info),
8514 MBEDTLS_DECRYPT)) != 0) {
8515 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_cipher_setkey", ret);
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008516 goto end;
8517 }
8518
8519#if defined(MBEDTLS_CIPHER_MODE_CBC)
Gilles Peskine449bd832023-01-11 14:50:10 +01008520 if (mbedtls_cipher_info_get_mode(cipher_info) == MBEDTLS_MODE_CBC) {
8521 if ((ret = mbedtls_cipher_set_padding_mode(&transform->cipher_ctx_enc,
8522 MBEDTLS_PADDING_NONE)) != 0) {
8523 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_cipher_set_padding_mode", ret);
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008524 goto end;
8525 }
8526
Gilles Peskine449bd832023-01-11 14:50:10 +01008527 if ((ret = mbedtls_cipher_set_padding_mode(&transform->cipher_ctx_dec,
8528 MBEDTLS_PADDING_NONE)) != 0) {
8529 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_cipher_set_padding_mode", ret);
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008530 goto end;
8531 }
8532 }
8533#endif /* MBEDTLS_CIPHER_MODE_CBC */
8534#endif /* MBEDTLS_USE_PSA_CRYPTO */
8535
Neil Armstrong29c0c042022-03-17 17:47:28 +01008536#if defined(MBEDTLS_SSL_SOME_SUITES_USE_MAC)
8537 /* For HMAC-based ciphersuites, initialize the HMAC transforms.
8538 For AEAD-based ciphersuites, there is nothing to do here. */
Gilles Peskine449bd832023-01-11 14:50:10 +01008539 if (mac_key_len != 0) {
Neil Armstrong29c0c042022-03-17 17:47:28 +01008540#if defined(MBEDTLS_USE_PSA_CRYPTO)
Gilles Peskine449bd832023-01-11 14:50:10 +01008541 transform->psa_mac_alg = PSA_ALG_HMAC(mac_alg);
Neil Armstrong29c0c042022-03-17 17:47:28 +01008542
Gilles Peskine449bd832023-01-11 14:50:10 +01008543 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_SIGN_MESSAGE);
8544 psa_set_key_algorithm(&attributes, PSA_ALG_HMAC(mac_alg));
8545 psa_set_key_type(&attributes, PSA_KEY_TYPE_HMAC);
Neil Armstrong29c0c042022-03-17 17:47:28 +01008546
Gilles Peskine449bd832023-01-11 14:50:10 +01008547 if ((status = psa_import_key(&attributes,
8548 mac_enc, mac_key_len,
8549 &transform->psa_mac_enc)) != PSA_SUCCESS) {
8550 ret = psa_ssl_status_to_mbedtls(status);
8551 MBEDTLS_SSL_DEBUG_RET(1, "psa_import_mac_key", ret);
Neil Armstrong29c0c042022-03-17 17:47:28 +01008552 goto end;
8553 }
8554
Gilles Peskine449bd832023-01-11 14:50:10 +01008555 if ((transform->psa_alg == MBEDTLS_SSL_NULL_CIPHER) ||
8556 ((transform->psa_alg == PSA_ALG_CBC_NO_PADDING)
Andrzej Kurek8c95ac42022-08-17 16:17:00 -04008557#if defined(MBEDTLS_SSL_SOME_SUITES_USE_CBC_ETM)
Gilles Peskine449bd832023-01-11 14:50:10 +01008558 && (transform->encrypt_then_mac == MBEDTLS_SSL_ETM_DISABLED)
Andrzej Kurek8c95ac42022-08-17 16:17:00 -04008559#endif
Gilles Peskine449bd832023-01-11 14:50:10 +01008560 )) {
Neil Armstrong29c0c042022-03-17 17:47:28 +01008561 /* mbedtls_ct_hmac() requires the key to be exportable */
Gilles Peskine449bd832023-01-11 14:50:10 +01008562 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_EXPORT |
8563 PSA_KEY_USAGE_VERIFY_HASH);
8564 } else {
8565 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_VERIFY_HASH);
8566 }
Neil Armstrong29c0c042022-03-17 17:47:28 +01008567
Gilles Peskine449bd832023-01-11 14:50:10 +01008568 if ((status = psa_import_key(&attributes,
8569 mac_dec, mac_key_len,
8570 &transform->psa_mac_dec)) != PSA_SUCCESS) {
8571 ret = psa_ssl_status_to_mbedtls(status);
8572 MBEDTLS_SSL_DEBUG_RET(1, "psa_import_mac_key", ret);
Neil Armstrong29c0c042022-03-17 17:47:28 +01008573 goto end;
8574 }
8575#else
Gilles Peskine449bd832023-01-11 14:50:10 +01008576 ret = mbedtls_md_hmac_starts(&transform->md_ctx_enc, mac_enc, mac_key_len);
8577 if (ret != 0) {
Neil Armstrong29c0c042022-03-17 17:47:28 +01008578 goto end;
Gilles Peskine449bd832023-01-11 14:50:10 +01008579 }
8580 ret = mbedtls_md_hmac_starts(&transform->md_ctx_dec, mac_dec, mac_key_len);
8581 if (ret != 0) {
Neil Armstrong29c0c042022-03-17 17:47:28 +01008582 goto end;
Gilles Peskine449bd832023-01-11 14:50:10 +01008583 }
Neil Armstrong29c0c042022-03-17 17:47:28 +01008584#endif /* MBEDTLS_USE_PSA_CRYPTO */
8585 }
8586#endif /* MBEDTLS_SSL_SOME_SUITES_USE_MAC */
8587
8588 ((void) mac_dec);
8589 ((void) mac_enc);
8590
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008591end:
Gilles Peskine449bd832023-01-11 14:50:10 +01008592 mbedtls_platform_zeroize(keyblk, sizeof(keyblk));
8593 return ret;
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008594}
8595
Valerio Settia08b1a42022-11-17 15:10:02 +01008596#if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED) && \
8597 defined(MBEDTLS_USE_PSA_CRYPTO)
Valerio Setti6b3dab02022-11-17 17:14:54 +01008598int mbedtls_psa_ecjpake_read_round(
Gilles Peskine449bd832023-01-11 14:50:10 +01008599 psa_pake_operation_t *pake_ctx,
8600 const unsigned char *buf,
8601 size_t len, mbedtls_ecjpake_rounds_t round)
Valerio Settia08b1a42022-11-17 15:10:02 +01008602{
8603 psa_status_t status;
8604 size_t input_offset = 0;
Valerio Setti819de862022-11-17 18:05:19 +01008605 /*
Valerio Setti6b3dab02022-11-17 17:14:54 +01008606 * At round one repeat the KEY_SHARE, ZK_PUBLIC & ZF_PROOF twice
8607 * At round two perform a single cycle
8608 */
Gilles Peskine449bd832023-01-11 14:50:10 +01008609 unsigned int remaining_steps = (round == MBEDTLS_ECJPAKE_ROUND_ONE) ? 2 : 1;
Valerio Settia08b1a42022-11-17 15:10:02 +01008610
Gilles Peskine449bd832023-01-11 14:50:10 +01008611 for (; remaining_steps > 0; remaining_steps--) {
8612 for (psa_pake_step_t step = PSA_PAKE_STEP_KEY_SHARE;
Valerio Settia08b1a42022-11-17 15:10:02 +01008613 step <= PSA_PAKE_STEP_ZK_PROOF;
Gilles Peskine449bd832023-01-11 14:50:10 +01008614 ++step) {
Valerio Settia08b1a42022-11-17 15:10:02 +01008615 /* Length is stored at the first byte */
8616 size_t length = buf[input_offset];
8617 input_offset += 1;
8618
Gilles Peskine449bd832023-01-11 14:50:10 +01008619 if (input_offset + length > len) {
Valerio Settia08b1a42022-11-17 15:10:02 +01008620 return MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE;
8621 }
8622
Gilles Peskine449bd832023-01-11 14:50:10 +01008623 status = psa_pake_input(pake_ctx, step,
8624 buf + input_offset, length);
8625 if (status != PSA_SUCCESS) {
8626 return psa_ssl_status_to_mbedtls(status);
Valerio Settia08b1a42022-11-17 15:10:02 +01008627 }
8628
8629 input_offset += length;
8630 }
8631 }
8632
Gilles Peskine449bd832023-01-11 14:50:10 +01008633 if (input_offset != len) {
Valerio Setti61ea17d2022-11-18 12:11:00 +01008634 return MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE;
Gilles Peskine449bd832023-01-11 14:50:10 +01008635 }
Valerio Setti30ebe112022-11-17 16:23:34 +01008636
Gilles Peskine449bd832023-01-11 14:50:10 +01008637 return 0;
Valerio Settia08b1a42022-11-17 15:10:02 +01008638}
8639
Valerio Setti6b3dab02022-11-17 17:14:54 +01008640int mbedtls_psa_ecjpake_write_round(
Gilles Peskine449bd832023-01-11 14:50:10 +01008641 psa_pake_operation_t *pake_ctx,
8642 unsigned char *buf,
8643 size_t len, size_t *olen,
8644 mbedtls_ecjpake_rounds_t round)
Valerio Settia08b1a42022-11-17 15:10:02 +01008645{
8646 psa_status_t status;
8647 size_t output_offset = 0;
8648 size_t output_len;
Valerio Setti819de862022-11-17 18:05:19 +01008649 /*
Valerio Setti6b3dab02022-11-17 17:14:54 +01008650 * At round one repeat the KEY_SHARE, ZK_PUBLIC & ZF_PROOF twice
8651 * At round two perform a single cycle
8652 */
Gilles Peskine449bd832023-01-11 14:50:10 +01008653 unsigned int remaining_steps = (round == MBEDTLS_ECJPAKE_ROUND_ONE) ? 2 : 1;
Valerio Settia08b1a42022-11-17 15:10:02 +01008654
Gilles Peskine449bd832023-01-11 14:50:10 +01008655 for (; remaining_steps > 0; remaining_steps--) {
8656 for (psa_pake_step_t step = PSA_PAKE_STEP_KEY_SHARE;
8657 step <= PSA_PAKE_STEP_ZK_PROOF;
8658 ++step) {
Valerio Setti79f6b6b2022-11-21 14:17:03 +01008659 /*
Valerio Settid4a9b1a2022-11-22 11:11:10 +01008660 * For each step, prepend 1 byte with the length of the data as
8661 * given by psa_pake_output().
Valerio Setti79f6b6b2022-11-21 14:17:03 +01008662 */
Gilles Peskine449bd832023-01-11 14:50:10 +01008663 status = psa_pake_output(pake_ctx, step,
8664 buf + output_offset + 1,
8665 len - output_offset - 1,
8666 &output_len);
8667 if (status != PSA_SUCCESS) {
8668 return psa_ssl_status_to_mbedtls(status);
Valerio Settia08b1a42022-11-17 15:10:02 +01008669 }
8670
Valerio Setti99d88c12022-11-22 16:03:43 +01008671 *(buf + output_offset) = (uint8_t) output_len;
Valerio Setti79f6b6b2022-11-21 14:17:03 +01008672
8673 output_offset += output_len + 1;
Valerio Settia08b1a42022-11-17 15:10:02 +01008674 }
8675 }
8676
8677 *olen = output_offset;
8678
Gilles Peskine449bd832023-01-11 14:50:10 +01008679 return 0;
Valerio Settia08b1a42022-11-17 15:10:02 +01008680}
Valerio Settia08b1a42022-11-17 15:10:02 +01008681#endif //MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED && MBEDTLS_USE_PSA_CRYPTO
8682
Jerry Yuee40f9d2022-02-17 14:55:16 +08008683#if defined(MBEDTLS_USE_PSA_CRYPTO)
Gilles Peskine449bd832023-01-11 14:50:10 +01008684int mbedtls_ssl_get_key_exchange_md_tls1_2(mbedtls_ssl_context *ssl,
8685 unsigned char *hash, size_t *hashlen,
8686 unsigned char *data, size_t data_len,
8687 mbedtls_md_type_t md_alg)
Jerry Yuee40f9d2022-02-17 14:55:16 +08008688{
8689 psa_status_t status;
8690 psa_hash_operation_t hash_operation = PSA_HASH_OPERATION_INIT;
Gilles Peskine449bd832023-01-11 14:50:10 +01008691 psa_algorithm_t hash_alg = mbedtls_hash_info_psa_from_md(md_alg);
Jerry Yuee40f9d2022-02-17 14:55:16 +08008692
Gilles Peskine449bd832023-01-11 14:50:10 +01008693 MBEDTLS_SSL_DEBUG_MSG(3, ("Perform PSA-based computation of digest of ServerKeyExchange"));
Jerry Yuee40f9d2022-02-17 14:55:16 +08008694
Gilles Peskine449bd832023-01-11 14:50:10 +01008695 if ((status = psa_hash_setup(&hash_operation,
8696 hash_alg)) != PSA_SUCCESS) {
8697 MBEDTLS_SSL_DEBUG_RET(1, "psa_hash_setup", status);
Jerry Yuee40f9d2022-02-17 14:55:16 +08008698 goto exit;
8699 }
8700
Gilles Peskine449bd832023-01-11 14:50:10 +01008701 if ((status = psa_hash_update(&hash_operation, ssl->handshake->randbytes,
8702 64)) != PSA_SUCCESS) {
8703 MBEDTLS_SSL_DEBUG_RET(1, "psa_hash_update", status);
Jerry Yuee40f9d2022-02-17 14:55:16 +08008704 goto exit;
8705 }
8706
Gilles Peskine449bd832023-01-11 14:50:10 +01008707 if ((status = psa_hash_update(&hash_operation,
8708 data, data_len)) != PSA_SUCCESS) {
8709 MBEDTLS_SSL_DEBUG_RET(1, "psa_hash_update", status);
Jerry Yuee40f9d2022-02-17 14:55:16 +08008710 goto exit;
8711 }
8712
Gilles Peskine449bd832023-01-11 14:50:10 +01008713 if ((status = psa_hash_finish(&hash_operation, hash, PSA_HASH_MAX_SIZE,
8714 hashlen)) != PSA_SUCCESS) {
8715 MBEDTLS_SSL_DEBUG_RET(1, "psa_hash_finish", status);
8716 goto exit;
Jerry Yuee40f9d2022-02-17 14:55:16 +08008717 }
8718
8719exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01008720 if (status != PSA_SUCCESS) {
8721 mbedtls_ssl_send_alert_message(ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
8722 MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR);
8723 switch (status) {
Jerry Yuee40f9d2022-02-17 14:55:16 +08008724 case PSA_ERROR_NOT_SUPPORTED:
Gilles Peskine449bd832023-01-11 14:50:10 +01008725 return MBEDTLS_ERR_MD_FEATURE_UNAVAILABLE;
Jerry Yuee40f9d2022-02-17 14:55:16 +08008726 case PSA_ERROR_BAD_STATE: /* Intentional fallthrough */
8727 case PSA_ERROR_BUFFER_TOO_SMALL:
Gilles Peskine449bd832023-01-11 14:50:10 +01008728 return MBEDTLS_ERR_MD_BAD_INPUT_DATA;
Jerry Yuee40f9d2022-02-17 14:55:16 +08008729 case PSA_ERROR_INSUFFICIENT_MEMORY:
Gilles Peskine449bd832023-01-11 14:50:10 +01008730 return MBEDTLS_ERR_MD_ALLOC_FAILED;
Jerry Yuee40f9d2022-02-17 14:55:16 +08008731 default:
Gilles Peskine449bd832023-01-11 14:50:10 +01008732 return MBEDTLS_ERR_PLATFORM_HW_ACCEL_FAILED;
Jerry Yuee40f9d2022-02-17 14:55:16 +08008733 }
8734 }
Gilles Peskine449bd832023-01-11 14:50:10 +01008735 return 0;
Jerry Yuee40f9d2022-02-17 14:55:16 +08008736}
8737
8738#else
8739
Gilles Peskine449bd832023-01-11 14:50:10 +01008740int mbedtls_ssl_get_key_exchange_md_tls1_2(mbedtls_ssl_context *ssl,
8741 unsigned char *hash, size_t *hashlen,
8742 unsigned char *data, size_t data_len,
8743 mbedtls_md_type_t md_alg)
Jerry Yuee40f9d2022-02-17 14:55:16 +08008744{
8745 int ret = 0;
8746 mbedtls_md_context_t ctx;
Gilles Peskine449bd832023-01-11 14:50:10 +01008747 const mbedtls_md_info_t *md_info = mbedtls_md_info_from_type(md_alg);
8748 *hashlen = mbedtls_md_get_size(md_info);
Jerry Yuee40f9d2022-02-17 14:55:16 +08008749
Gilles Peskine449bd832023-01-11 14:50:10 +01008750 MBEDTLS_SSL_DEBUG_MSG(3, ("Perform mbedtls-based computation of digest of ServerKeyExchange"));
Jerry Yuee40f9d2022-02-17 14:55:16 +08008751
Gilles Peskine449bd832023-01-11 14:50:10 +01008752 mbedtls_md_init(&ctx);
Jerry Yuee40f9d2022-02-17 14:55:16 +08008753
8754 /*
8755 * digitally-signed struct {
8756 * opaque client_random[32];
8757 * opaque server_random[32];
8758 * ServerDHParams params;
8759 * };
8760 */
Gilles Peskine449bd832023-01-11 14:50:10 +01008761 if ((ret = mbedtls_md_setup(&ctx, md_info, 0)) != 0) {
8762 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_md_setup", ret);
Jerry Yuee40f9d2022-02-17 14:55:16 +08008763 goto exit;
8764 }
Gilles Peskine449bd832023-01-11 14:50:10 +01008765 if ((ret = mbedtls_md_starts(&ctx)) != 0) {
8766 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_md_starts", ret);
Jerry Yuee40f9d2022-02-17 14:55:16 +08008767 goto exit;
8768 }
Gilles Peskine449bd832023-01-11 14:50:10 +01008769 if ((ret = mbedtls_md_update(&ctx, ssl->handshake->randbytes, 64)) != 0) {
8770 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_md_update", ret);
Jerry Yuee40f9d2022-02-17 14:55:16 +08008771 goto exit;
8772 }
Gilles Peskine449bd832023-01-11 14:50:10 +01008773 if ((ret = mbedtls_md_update(&ctx, data, data_len)) != 0) {
8774 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_md_update", ret);
Jerry Yuee40f9d2022-02-17 14:55:16 +08008775 goto exit;
8776 }
Gilles Peskine449bd832023-01-11 14:50:10 +01008777 if ((ret = mbedtls_md_finish(&ctx, hash)) != 0) {
8778 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_md_finish", ret);
Jerry Yuee40f9d2022-02-17 14:55:16 +08008779 goto exit;
8780 }
8781
8782exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01008783 mbedtls_md_free(&ctx);
Jerry Yuee40f9d2022-02-17 14:55:16 +08008784
Gilles Peskine449bd832023-01-11 14:50:10 +01008785 if (ret != 0) {
8786 mbedtls_ssl_send_alert_message(ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
8787 MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR);
8788 }
Jerry Yuee40f9d2022-02-17 14:55:16 +08008789
Gilles Peskine449bd832023-01-11 14:50:10 +01008790 return ret;
Jerry Yuee40f9d2022-02-17 14:55:16 +08008791}
8792#endif /* MBEDTLS_USE_PSA_CRYPTO */
8793
Jerry Yud9d91da2022-02-17 14:57:06 +08008794#if defined(MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED)
8795
Gabor Mezeia3d016c2022-05-10 12:44:09 +02008796/* Find the preferred hash for a given signature algorithm. */
8797unsigned int mbedtls_ssl_tls12_get_preferred_hash_for_sig_alg(
Gilles Peskine449bd832023-01-11 14:50:10 +01008798 mbedtls_ssl_context *ssl,
8799 unsigned int sig_alg)
Jerry Yud9d91da2022-02-17 14:57:06 +08008800{
Gabor Mezei078e8032022-04-27 21:17:56 +02008801 unsigned int i;
Gabor Mezeia3d016c2022-05-10 12:44:09 +02008802 uint16_t *received_sig_algs = ssl->handshake->received_sig_algs;
Gabor Mezei078e8032022-04-27 21:17:56 +02008803
Gilles Peskine449bd832023-01-11 14:50:10 +01008804 if (sig_alg == MBEDTLS_SSL_SIG_ANON) {
8805 return MBEDTLS_SSL_HASH_NONE;
8806 }
Gabor Mezei078e8032022-04-27 21:17:56 +02008807
Gilles Peskine449bd832023-01-11 14:50:10 +01008808 for (i = 0; received_sig_algs[i] != MBEDTLS_TLS_SIG_NONE; i++) {
Neil Armstrong9f1176a2022-06-24 18:19:19 +02008809 unsigned int hash_alg_received =
Gilles Peskine449bd832023-01-11 14:50:10 +01008810 MBEDTLS_SSL_TLS12_HASH_ALG_FROM_SIG_AND_HASH_ALG(
8811 received_sig_algs[i]);
Neil Armstrong9f1176a2022-06-24 18:19:19 +02008812 unsigned int sig_alg_received =
Gilles Peskine449bd832023-01-11 14:50:10 +01008813 MBEDTLS_SSL_TLS12_SIG_ALG_FROM_SIG_AND_HASH_ALG(
8814 received_sig_algs[i]);
Neil Armstrong9f1176a2022-06-24 18:19:19 +02008815
Gilles Peskine449bd832023-01-11 14:50:10 +01008816 if (sig_alg == sig_alg_received) {
Neil Armstrong9f1176a2022-06-24 18:19:19 +02008817#if defined(MBEDTLS_USE_PSA_CRYPTO)
Gilles Peskine449bd832023-01-11 14:50:10 +01008818 if (ssl->handshake->key_cert && ssl->handshake->key_cert->key) {
Neil Armstrong96eceb82022-06-30 18:05:05 +02008819 psa_algorithm_t psa_hash_alg =
Gilles Peskine449bd832023-01-11 14:50:10 +01008820 mbedtls_hash_info_psa_from_md(hash_alg_received);
Neil Armstrong9f1176a2022-06-24 18:19:19 +02008821
Gilles Peskine449bd832023-01-11 14:50:10 +01008822 if (sig_alg_received == MBEDTLS_SSL_SIG_ECDSA &&
8823 !mbedtls_pk_can_do_ext(ssl->handshake->key_cert->key,
8824 PSA_ALG_ECDSA(psa_hash_alg),
8825 PSA_KEY_USAGE_SIGN_HASH)) {
Neil Armstrong9f1176a2022-06-24 18:19:19 +02008826 continue;
Gilles Peskine449bd832023-01-11 14:50:10 +01008827 }
Neil Armstrong9f1176a2022-06-24 18:19:19 +02008828
Gilles Peskine449bd832023-01-11 14:50:10 +01008829 if (sig_alg_received == MBEDTLS_SSL_SIG_RSA &&
8830 !mbedtls_pk_can_do_ext(ssl->handshake->key_cert->key,
8831 PSA_ALG_RSA_PKCS1V15_SIGN(
8832 psa_hash_alg),
8833 PSA_KEY_USAGE_SIGN_HASH)) {
Neil Armstrong9f1176a2022-06-24 18:19:19 +02008834 continue;
Gilles Peskine449bd832023-01-11 14:50:10 +01008835 }
Neil Armstrong9f1176a2022-06-24 18:19:19 +02008836 }
Neil Armstrong9f1176a2022-06-24 18:19:19 +02008837#endif /* MBEDTLS_USE_PSA_CRYPTO */
Neil Armstrong96eceb82022-06-30 18:05:05 +02008838
Gilles Peskine449bd832023-01-11 14:50:10 +01008839 return hash_alg_received;
Neil Armstrong9f1176a2022-06-24 18:19:19 +02008840 }
Jerry Yud9d91da2022-02-17 14:57:06 +08008841 }
Jerry Yud9d91da2022-02-17 14:57:06 +08008842
Gilles Peskine449bd832023-01-11 14:50:10 +01008843 return MBEDTLS_SSL_HASH_NONE;
Jerry Yud9d91da2022-02-17 14:57:06 +08008844}
8845
8846#endif /* MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED */
8847
Jerry Yu4f9e3ef2022-02-17 14:58:27 +08008848/* Serialization of TLS 1.2 sessions:
8849 *
8850 * struct {
8851 * uint64 start_time;
8852 * uint8 ciphersuite[2]; // defined by the standard
Jerry Yu4f9e3ef2022-02-17 14:58:27 +08008853 * uint8 session_id_len; // at most 32
8854 * opaque session_id[32];
8855 * opaque master[48]; // fixed length in the standard
8856 * uint32 verify_result;
8857 * opaque peer_cert<0..2^24-1>; // length 0 means no peer cert
8858 * opaque ticket<0..2^24-1>; // length 0 means no ticket
8859 * uint32 ticket_lifetime;
8860 * uint8 mfl_code; // up to 255 according to standard
8861 * uint8 encrypt_then_mac; // 0 or 1
8862 * } serialized_session_tls12;
8863 *
8864 */
Gilles Peskine449bd832023-01-11 14:50:10 +01008865static size_t ssl_tls12_session_save(const mbedtls_ssl_session *session,
8866 unsigned char *buf,
8867 size_t buf_len)
Jerry Yu4f9e3ef2022-02-17 14:58:27 +08008868{
8869 unsigned char *p = buf;
8870 size_t used = 0;
8871
8872#if defined(MBEDTLS_HAVE_TIME)
8873 uint64_t start;
8874#endif
8875#if defined(MBEDTLS_X509_CRT_PARSE_C)
8876#if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
8877 size_t cert_len;
8878#endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
8879#endif /* MBEDTLS_X509_CRT_PARSE_C */
8880
8881 /*
8882 * Time
8883 */
8884#if defined(MBEDTLS_HAVE_TIME)
8885 used += 8;
8886
Gilles Peskine449bd832023-01-11 14:50:10 +01008887 if (used <= buf_len) {
Jerry Yu4f9e3ef2022-02-17 14:58:27 +08008888 start = (uint64_t) session->start;
8889
Gilles Peskine449bd832023-01-11 14:50:10 +01008890 MBEDTLS_PUT_UINT64_BE(start, p, 0);
Jerry Yu4f9e3ef2022-02-17 14:58:27 +08008891 p += 8;
8892 }
8893#endif /* MBEDTLS_HAVE_TIME */
8894
8895 /*
8896 * Basic mandatory fields
8897 */
8898 used += 2 /* ciphersuite */
Gilles Peskine449bd832023-01-11 14:50:10 +01008899 + 1 /* id_len */
8900 + sizeof(session->id)
8901 + sizeof(session->master)
8902 + 4; /* verify_result */
Jerry Yu4f9e3ef2022-02-17 14:58:27 +08008903
Gilles Peskine449bd832023-01-11 14:50:10 +01008904 if (used <= buf_len) {
8905 MBEDTLS_PUT_UINT16_BE(session->ciphersuite, p, 0);
Jerry Yu4f9e3ef2022-02-17 14:58:27 +08008906 p += 2;
8907
Gilles Peskine449bd832023-01-11 14:50:10 +01008908 *p++ = MBEDTLS_BYTE_0(session->id_len);
8909 memcpy(p, session->id, 32);
Jerry Yu4f9e3ef2022-02-17 14:58:27 +08008910 p += 32;
8911
Gilles Peskine449bd832023-01-11 14:50:10 +01008912 memcpy(p, session->master, 48);
Jerry Yu4f9e3ef2022-02-17 14:58:27 +08008913 p += 48;
8914
Gilles Peskine449bd832023-01-11 14:50:10 +01008915 MBEDTLS_PUT_UINT32_BE(session->verify_result, p, 0);
Jerry Yu4f9e3ef2022-02-17 14:58:27 +08008916 p += 4;
8917 }
8918
8919 /*
8920 * Peer's end-entity certificate
8921 */
8922#if defined(MBEDTLS_X509_CRT_PARSE_C)
8923#if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
Gilles Peskine449bd832023-01-11 14:50:10 +01008924 if (session->peer_cert == NULL) {
Jerry Yu4f9e3ef2022-02-17 14:58:27 +08008925 cert_len = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +01008926 } else {
Jerry Yu4f9e3ef2022-02-17 14:58:27 +08008927 cert_len = session->peer_cert->raw.len;
Gilles Peskine449bd832023-01-11 14:50:10 +01008928 }
Jerry Yu4f9e3ef2022-02-17 14:58:27 +08008929
8930 used += 3 + cert_len;
8931
Gilles Peskine449bd832023-01-11 14:50:10 +01008932 if (used <= buf_len) {
8933 *p++ = MBEDTLS_BYTE_2(cert_len);
8934 *p++ = MBEDTLS_BYTE_1(cert_len);
8935 *p++ = MBEDTLS_BYTE_0(cert_len);
Jerry Yu4f9e3ef2022-02-17 14:58:27 +08008936
Gilles Peskine449bd832023-01-11 14:50:10 +01008937 if (session->peer_cert != NULL) {
8938 memcpy(p, session->peer_cert->raw.p, cert_len);
Jerry Yu4f9e3ef2022-02-17 14:58:27 +08008939 p += cert_len;
8940 }
8941 }
8942#else /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
Gilles Peskine449bd832023-01-11 14:50:10 +01008943 if (session->peer_cert_digest != NULL) {
Jerry Yu4f9e3ef2022-02-17 14:58:27 +08008944 used += 1 /* type */ + 1 /* length */ + session->peer_cert_digest_len;
Gilles Peskine449bd832023-01-11 14:50:10 +01008945 if (used <= buf_len) {
Jerry Yu4f9e3ef2022-02-17 14:58:27 +08008946 *p++ = (unsigned char) session->peer_cert_digest_type;
8947 *p++ = (unsigned char) session->peer_cert_digest_len;
Gilles Peskine449bd832023-01-11 14:50:10 +01008948 memcpy(p, session->peer_cert_digest,
8949 session->peer_cert_digest_len);
Jerry Yu4f9e3ef2022-02-17 14:58:27 +08008950 p += session->peer_cert_digest_len;
8951 }
Gilles Peskine449bd832023-01-11 14:50:10 +01008952 } else {
Jerry Yu4f9e3ef2022-02-17 14:58:27 +08008953 used += 2;
Gilles Peskine449bd832023-01-11 14:50:10 +01008954 if (used <= buf_len) {
Jerry Yu4f9e3ef2022-02-17 14:58:27 +08008955 *p++ = (unsigned char) MBEDTLS_MD_NONE;
8956 *p++ = 0;
8957 }
8958 }
8959#endif /* !MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
8960#endif /* MBEDTLS_X509_CRT_PARSE_C */
8961
8962 /*
8963 * Session ticket if any, plus associated data
8964 */
8965#if defined(MBEDTLS_SSL_SESSION_TICKETS) && defined(MBEDTLS_SSL_CLI_C)
8966 used += 3 + session->ticket_len + 4; /* len + ticket + lifetime */
8967
Gilles Peskine449bd832023-01-11 14:50:10 +01008968 if (used <= buf_len) {
8969 *p++ = MBEDTLS_BYTE_2(session->ticket_len);
8970 *p++ = MBEDTLS_BYTE_1(session->ticket_len);
8971 *p++ = MBEDTLS_BYTE_0(session->ticket_len);
Jerry Yu4f9e3ef2022-02-17 14:58:27 +08008972
Gilles Peskine449bd832023-01-11 14:50:10 +01008973 if (session->ticket != NULL) {
8974 memcpy(p, session->ticket, session->ticket_len);
Jerry Yu4f9e3ef2022-02-17 14:58:27 +08008975 p += session->ticket_len;
8976 }
8977
Gilles Peskine449bd832023-01-11 14:50:10 +01008978 MBEDTLS_PUT_UINT32_BE(session->ticket_lifetime, p, 0);
Jerry Yu4f9e3ef2022-02-17 14:58:27 +08008979 p += 4;
8980 }
8981#endif /* MBEDTLS_SSL_SESSION_TICKETS && MBEDTLS_SSL_CLI_C */
8982
8983 /*
8984 * Misc extension-related info
8985 */
8986#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
8987 used += 1;
8988
Gilles Peskine449bd832023-01-11 14:50:10 +01008989 if (used <= buf_len) {
Jerry Yu4f9e3ef2022-02-17 14:58:27 +08008990 *p++ = session->mfl_code;
Gilles Peskine449bd832023-01-11 14:50:10 +01008991 }
Jerry Yu4f9e3ef2022-02-17 14:58:27 +08008992#endif
8993
8994#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
8995 used += 1;
8996
Gilles Peskine449bd832023-01-11 14:50:10 +01008997 if (used <= buf_len) {
8998 *p++ = MBEDTLS_BYTE_0(session->encrypt_then_mac);
8999 }
Jerry Yu4f9e3ef2022-02-17 14:58:27 +08009000#endif
9001
Gilles Peskine449bd832023-01-11 14:50:10 +01009002 return used;
Jerry Yu4f9e3ef2022-02-17 14:58:27 +08009003}
9004
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02009005MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01009006static int ssl_tls12_session_load(mbedtls_ssl_session *session,
9007 const unsigned char *buf,
9008 size_t len)
Jerry Yu4f9e3ef2022-02-17 14:58:27 +08009009{
9010#if defined(MBEDTLS_HAVE_TIME)
9011 uint64_t start;
9012#endif
9013#if defined(MBEDTLS_X509_CRT_PARSE_C)
9014#if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
9015 size_t cert_len;
9016#endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
9017#endif /* MBEDTLS_X509_CRT_PARSE_C */
9018
9019 const unsigned char *p = buf;
9020 const unsigned char * const end = buf + len;
9021
9022 /*
9023 * Time
9024 */
9025#if defined(MBEDTLS_HAVE_TIME)
Gilles Peskine449bd832023-01-11 14:50:10 +01009026 if (8 > (size_t) (end - p)) {
9027 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
9028 }
Jerry Yu4f9e3ef2022-02-17 14:58:27 +08009029
Gilles Peskine449bd832023-01-11 14:50:10 +01009030 start = ((uint64_t) p[0] << 56) |
9031 ((uint64_t) p[1] << 48) |
9032 ((uint64_t) p[2] << 40) |
9033 ((uint64_t) p[3] << 32) |
9034 ((uint64_t) p[4] << 24) |
9035 ((uint64_t) p[5] << 16) |
9036 ((uint64_t) p[6] << 8) |
9037 ((uint64_t) p[7]);
Jerry Yu4f9e3ef2022-02-17 14:58:27 +08009038 p += 8;
9039
9040 session->start = (time_t) start;
9041#endif /* MBEDTLS_HAVE_TIME */
9042
9043 /*
9044 * Basic mandatory fields
9045 */
Gilles Peskine449bd832023-01-11 14:50:10 +01009046 if (2 + 1 + 32 + 48 + 4 > (size_t) (end - p)) {
9047 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
9048 }
Jerry Yu4f9e3ef2022-02-17 14:58:27 +08009049
Gilles Peskine449bd832023-01-11 14:50:10 +01009050 session->ciphersuite = (p[0] << 8) | p[1];
Jerry Yu4f9e3ef2022-02-17 14:58:27 +08009051 p += 2;
9052
Jerry Yu4f9e3ef2022-02-17 14:58:27 +08009053 session->id_len = *p++;
Gilles Peskine449bd832023-01-11 14:50:10 +01009054 memcpy(session->id, p, 32);
Jerry Yu4f9e3ef2022-02-17 14:58:27 +08009055 p += 32;
9056
Gilles Peskine449bd832023-01-11 14:50:10 +01009057 memcpy(session->master, p, 48);
Jerry Yu4f9e3ef2022-02-17 14:58:27 +08009058 p += 48;
9059
Gilles Peskine449bd832023-01-11 14:50:10 +01009060 session->verify_result = ((uint32_t) p[0] << 24) |
9061 ((uint32_t) p[1] << 16) |
9062 ((uint32_t) p[2] << 8) |
9063 ((uint32_t) p[3]);
Jerry Yu4f9e3ef2022-02-17 14:58:27 +08009064 p += 4;
9065
9066 /* Immediately clear invalid pointer values that have been read, in case
9067 * we exit early before we replaced them with valid ones. */
9068#if defined(MBEDTLS_X509_CRT_PARSE_C)
9069#if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
9070 session->peer_cert = NULL;
9071#else
9072 session->peer_cert_digest = NULL;
9073#endif /* !MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
9074#endif /* MBEDTLS_X509_CRT_PARSE_C */
9075#if defined(MBEDTLS_SSL_SESSION_TICKETS) && defined(MBEDTLS_SSL_CLI_C)
9076 session->ticket = NULL;
9077#endif /* MBEDTLS_SSL_SESSION_TICKETS && MBEDTLS_SSL_CLI_C */
9078
9079 /*
9080 * Peer certificate
9081 */
9082#if defined(MBEDTLS_X509_CRT_PARSE_C)
9083#if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
9084 /* Deserialize CRT from the end of the ticket. */
Gilles Peskine449bd832023-01-11 14:50:10 +01009085 if (3 > (size_t) (end - p)) {
9086 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
9087 }
Jerry Yu4f9e3ef2022-02-17 14:58:27 +08009088
Gilles Peskine449bd832023-01-11 14:50:10 +01009089 cert_len = (p[0] << 16) | (p[1] << 8) | p[2];
Jerry Yu4f9e3ef2022-02-17 14:58:27 +08009090 p += 3;
9091
Gilles Peskine449bd832023-01-11 14:50:10 +01009092 if (cert_len != 0) {
Jerry Yu4f9e3ef2022-02-17 14:58:27 +08009093 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
9094
Gilles Peskine449bd832023-01-11 14:50:10 +01009095 if (cert_len > (size_t) (end - p)) {
9096 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
9097 }
Jerry Yu4f9e3ef2022-02-17 14:58:27 +08009098
Gilles Peskine449bd832023-01-11 14:50:10 +01009099 session->peer_cert = mbedtls_calloc(1, sizeof(mbedtls_x509_crt));
Jerry Yu4f9e3ef2022-02-17 14:58:27 +08009100
Gilles Peskine449bd832023-01-11 14:50:10 +01009101 if (session->peer_cert == NULL) {
9102 return MBEDTLS_ERR_SSL_ALLOC_FAILED;
9103 }
Jerry Yu4f9e3ef2022-02-17 14:58:27 +08009104
Gilles Peskine449bd832023-01-11 14:50:10 +01009105 mbedtls_x509_crt_init(session->peer_cert);
Jerry Yu4f9e3ef2022-02-17 14:58:27 +08009106
Gilles Peskine449bd832023-01-11 14:50:10 +01009107 if ((ret = mbedtls_x509_crt_parse_der(session->peer_cert,
9108 p, cert_len)) != 0) {
9109 mbedtls_x509_crt_free(session->peer_cert);
9110 mbedtls_free(session->peer_cert);
Jerry Yu4f9e3ef2022-02-17 14:58:27 +08009111 session->peer_cert = NULL;
Gilles Peskine449bd832023-01-11 14:50:10 +01009112 return ret;
Jerry Yu4f9e3ef2022-02-17 14:58:27 +08009113 }
9114
9115 p += cert_len;
9116 }
9117#else /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
9118 /* Deserialize CRT digest from the end of the ticket. */
Gilles Peskine449bd832023-01-11 14:50:10 +01009119 if (2 > (size_t) (end - p)) {
9120 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
9121 }
Jerry Yu4f9e3ef2022-02-17 14:58:27 +08009122
9123 session->peer_cert_digest_type = (mbedtls_md_type_t) *p++;
9124 session->peer_cert_digest_len = (size_t) *p++;
9125
Gilles Peskine449bd832023-01-11 14:50:10 +01009126 if (session->peer_cert_digest_len != 0) {
Jerry Yu4f9e3ef2022-02-17 14:58:27 +08009127 const mbedtls_md_info_t *md_info =
Gilles Peskine449bd832023-01-11 14:50:10 +01009128 mbedtls_md_info_from_type(session->peer_cert_digest_type);
9129 if (md_info == NULL) {
9130 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
9131 }
9132 if (session->peer_cert_digest_len != mbedtls_md_get_size(md_info)) {
9133 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
9134 }
Jerry Yu4f9e3ef2022-02-17 14:58:27 +08009135
Gilles Peskine449bd832023-01-11 14:50:10 +01009136 if (session->peer_cert_digest_len > (size_t) (end - p)) {
9137 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
9138 }
Jerry Yu4f9e3ef2022-02-17 14:58:27 +08009139
9140 session->peer_cert_digest =
Gilles Peskine449bd832023-01-11 14:50:10 +01009141 mbedtls_calloc(1, session->peer_cert_digest_len);
9142 if (session->peer_cert_digest == NULL) {
9143 return MBEDTLS_ERR_SSL_ALLOC_FAILED;
9144 }
Jerry Yu4f9e3ef2022-02-17 14:58:27 +08009145
Gilles Peskine449bd832023-01-11 14:50:10 +01009146 memcpy(session->peer_cert_digest, p,
9147 session->peer_cert_digest_len);
Jerry Yu4f9e3ef2022-02-17 14:58:27 +08009148 p += session->peer_cert_digest_len;
9149 }
9150#endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
9151#endif /* MBEDTLS_X509_CRT_PARSE_C */
9152
9153 /*
9154 * Session ticket and associated data
9155 */
9156#if defined(MBEDTLS_SSL_SESSION_TICKETS) && defined(MBEDTLS_SSL_CLI_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01009157 if (3 > (size_t) (end - p)) {
9158 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
9159 }
Jerry Yu4f9e3ef2022-02-17 14:58:27 +08009160
Gilles Peskine449bd832023-01-11 14:50:10 +01009161 session->ticket_len = (p[0] << 16) | (p[1] << 8) | p[2];
Jerry Yu4f9e3ef2022-02-17 14:58:27 +08009162 p += 3;
9163
Gilles Peskine449bd832023-01-11 14:50:10 +01009164 if (session->ticket_len != 0) {
9165 if (session->ticket_len > (size_t) (end - p)) {
9166 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
9167 }
Jerry Yu4f9e3ef2022-02-17 14:58:27 +08009168
Gilles Peskine449bd832023-01-11 14:50:10 +01009169 session->ticket = mbedtls_calloc(1, session->ticket_len);
9170 if (session->ticket == NULL) {
9171 return MBEDTLS_ERR_SSL_ALLOC_FAILED;
9172 }
Jerry Yu4f9e3ef2022-02-17 14:58:27 +08009173
Gilles Peskine449bd832023-01-11 14:50:10 +01009174 memcpy(session->ticket, p, session->ticket_len);
Jerry Yu4f9e3ef2022-02-17 14:58:27 +08009175 p += session->ticket_len;
9176 }
9177
Gilles Peskine449bd832023-01-11 14:50:10 +01009178 if (4 > (size_t) (end - p)) {
9179 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
9180 }
Jerry Yu4f9e3ef2022-02-17 14:58:27 +08009181
Gilles Peskine449bd832023-01-11 14:50:10 +01009182 session->ticket_lifetime = ((uint32_t) p[0] << 24) |
9183 ((uint32_t) p[1] << 16) |
9184 ((uint32_t) p[2] << 8) |
9185 ((uint32_t) p[3]);
Jerry Yu4f9e3ef2022-02-17 14:58:27 +08009186 p += 4;
9187#endif /* MBEDTLS_SSL_SESSION_TICKETS && MBEDTLS_SSL_CLI_C */
9188
9189 /*
9190 * Misc extension-related info
9191 */
9192#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
Gilles Peskine449bd832023-01-11 14:50:10 +01009193 if (1 > (size_t) (end - p)) {
9194 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
9195 }
Jerry Yu4f9e3ef2022-02-17 14:58:27 +08009196
9197 session->mfl_code = *p++;
9198#endif
9199
9200#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
Gilles Peskine449bd832023-01-11 14:50:10 +01009201 if (1 > (size_t) (end - p)) {
9202 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
9203 }
Jerry Yu4f9e3ef2022-02-17 14:58:27 +08009204
9205 session->encrypt_then_mac = *p++;
9206#endif
9207
9208 /* Done, should have consumed entire buffer */
Gilles Peskine449bd832023-01-11 14:50:10 +01009209 if (p != end) {
9210 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
9211 }
Jerry Yu4f9e3ef2022-02-17 14:58:27 +08009212
Gilles Peskine449bd832023-01-11 14:50:10 +01009213 return 0;
Jerry Yu4f9e3ef2022-02-17 14:58:27 +08009214}
Jerry Yudc7bd172022-02-17 13:44:15 +08009215#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
9216
XiaokangQian75d40ef2022-04-20 11:05:24 +00009217int mbedtls_ssl_validate_ciphersuite(
9218 const mbedtls_ssl_context *ssl,
9219 const mbedtls_ssl_ciphersuite_t *suite_info,
9220 mbedtls_ssl_protocol_version min_tls_version,
Gilles Peskine449bd832023-01-11 14:50:10 +01009221 mbedtls_ssl_protocol_version max_tls_version)
XiaokangQian75d40ef2022-04-20 11:05:24 +00009222{
9223 (void) ssl;
9224
Gilles Peskine449bd832023-01-11 14:50:10 +01009225 if (suite_info == NULL) {
9226 return -1;
9227 }
XiaokangQian75d40ef2022-04-20 11:05:24 +00009228
Gilles Peskine449bd832023-01-11 14:50:10 +01009229 if ((suite_info->min_tls_version > max_tls_version) ||
9230 (suite_info->max_tls_version < min_tls_version)) {
9231 return -1;
XiaokangQian75d40ef2022-04-20 11:05:24 +00009232 }
9233
XiaokangQian060d8672022-04-21 09:24:56 +00009234#if defined(MBEDTLS_SSL_PROTO_TLS1_2) && defined(MBEDTLS_SSL_CLI_C)
XiaokangQian75d40ef2022-04-20 11:05:24 +00009235#if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
Neil Armstrongca7d5062022-05-31 14:43:23 +02009236#if defined(MBEDTLS_USE_PSA_CRYPTO)
Gilles Peskine449bd832023-01-11 14:50:10 +01009237 if (suite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECJPAKE &&
9238 ssl->handshake->psa_pake_ctx_is_ok != 1)
Neil Armstrongca7d5062022-05-31 14:43:23 +02009239#else
Gilles Peskine449bd832023-01-11 14:50:10 +01009240 if (suite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECJPAKE &&
9241 mbedtls_ecjpake_check(&ssl->handshake->ecjpake_ctx) != 0)
Neil Armstrongca7d5062022-05-31 14:43:23 +02009242#endif /* MBEDTLS_USE_PSA_CRYPTO */
XiaokangQian75d40ef2022-04-20 11:05:24 +00009243 {
Gilles Peskine449bd832023-01-11 14:50:10 +01009244 return -1;
XiaokangQian75d40ef2022-04-20 11:05:24 +00009245 }
9246#endif
9247
9248 /* Don't suggest PSK-based ciphersuite if no PSK is available. */
9249#if defined(MBEDTLS_KEY_EXCHANGE_SOME_PSK_ENABLED)
Gilles Peskine449bd832023-01-11 14:50:10 +01009250 if (mbedtls_ssl_ciphersuite_uses_psk(suite_info) &&
9251 mbedtls_ssl_conf_has_static_psk(ssl->conf) == 0) {
9252 return -1;
XiaokangQian75d40ef2022-04-20 11:05:24 +00009253 }
9254#endif /* MBEDTLS_KEY_EXCHANGE_SOME_PSK_ENABLED */
9255#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
9256
Gilles Peskine449bd832023-01-11 14:50:10 +01009257 return 0;
XiaokangQian75d40ef2022-04-20 11:05:24 +00009258}
9259
Ronald Crone68ab4f2022-10-05 12:46:29 +02009260#if defined(MBEDTLS_SSL_HANDSHAKE_WITH_CERT_ENABLED)
XiaokangQianeaf36512022-04-24 09:07:44 +00009261/*
9262 * Function for writing a signature algorithm extension.
9263 *
9264 * The `extension_data` field of signature algorithm contains a `SignatureSchemeList`
9265 * value (TLS 1.3 RFC8446):
9266 * enum {
9267 * ....
9268 * ecdsa_secp256r1_sha256( 0x0403 ),
9269 * ecdsa_secp384r1_sha384( 0x0503 ),
9270 * ecdsa_secp521r1_sha512( 0x0603 ),
9271 * ....
9272 * } SignatureScheme;
9273 *
9274 * struct {
9275 * SignatureScheme supported_signature_algorithms<2..2^16-2>;
9276 * } SignatureSchemeList;
9277 *
9278 * The `extension_data` field of signature algorithm contains a `SignatureAndHashAlgorithm`
9279 * value (TLS 1.2 RFC5246):
9280 * enum {
9281 * none(0), md5(1), sha1(2), sha224(3), sha256(4), sha384(5),
9282 * sha512(6), (255)
9283 * } HashAlgorithm;
9284 *
9285 * enum { anonymous(0), rsa(1), dsa(2), ecdsa(3), (255) }
9286 * SignatureAlgorithm;
9287 *
9288 * struct {
9289 * HashAlgorithm hash;
9290 * SignatureAlgorithm signature;
9291 * } SignatureAndHashAlgorithm;
9292 *
9293 * SignatureAndHashAlgorithm
9294 * supported_signature_algorithms<2..2^16-2>;
9295 *
9296 * The TLS 1.3 signature algorithm extension was defined to be a compatible
9297 * generalization of the TLS 1.2 signature algorithm extension.
9298 * `SignatureAndHashAlgorithm` field of TLS 1.2 can be represented by
9299 * `SignatureScheme` field of TLS 1.3
9300 *
9301 */
Gilles Peskine449bd832023-01-11 14:50:10 +01009302int mbedtls_ssl_write_sig_alg_ext(mbedtls_ssl_context *ssl, unsigned char *buf,
9303 const unsigned char *end, size_t *out_len)
XiaokangQianeaf36512022-04-24 09:07:44 +00009304{
9305 unsigned char *p = buf;
9306 unsigned char *supported_sig_alg; /* Start of supported_signature_algorithms */
9307 size_t supported_sig_alg_len = 0; /* Length of supported_signature_algorithms */
9308
9309 *out_len = 0;
9310
Gilles Peskine449bd832023-01-11 14:50:10 +01009311 MBEDTLS_SSL_DEBUG_MSG(3, ("adding signature_algorithms extension"));
XiaokangQianeaf36512022-04-24 09:07:44 +00009312
9313 /* Check if we have space for header and length field:
9314 * - extension_type (2 bytes)
9315 * - extension_data_length (2 bytes)
9316 * - supported_signature_algorithms_length (2 bytes)
9317 */
Gilles Peskine449bd832023-01-11 14:50:10 +01009318 MBEDTLS_SSL_CHK_BUF_PTR(p, end, 6);
XiaokangQianeaf36512022-04-24 09:07:44 +00009319 p += 6;
9320
9321 /*
9322 * Write supported_signature_algorithms
9323 */
9324 supported_sig_alg = p;
Gilles Peskine449bd832023-01-11 14:50:10 +01009325 const uint16_t *sig_alg = mbedtls_ssl_get_sig_algs(ssl);
9326 if (sig_alg == NULL) {
9327 return MBEDTLS_ERR_SSL_BAD_CONFIG;
9328 }
XiaokangQianeaf36512022-04-24 09:07:44 +00009329
Gilles Peskine449bd832023-01-11 14:50:10 +01009330 for (; *sig_alg != MBEDTLS_TLS1_3_SIG_NONE; sig_alg++) {
9331 MBEDTLS_SSL_DEBUG_MSG(3, ("got signature scheme [%x] %s",
9332 *sig_alg,
9333 mbedtls_ssl_sig_alg_to_str(*sig_alg)));
9334 if (!mbedtls_ssl_sig_alg_is_supported(ssl, *sig_alg)) {
XiaokangQianeaf36512022-04-24 09:07:44 +00009335 continue;
Gilles Peskine449bd832023-01-11 14:50:10 +01009336 }
9337 MBEDTLS_SSL_CHK_BUF_PTR(p, end, 2);
9338 MBEDTLS_PUT_UINT16_BE(*sig_alg, p, 0);
XiaokangQianeaf36512022-04-24 09:07:44 +00009339 p += 2;
Gilles Peskine449bd832023-01-11 14:50:10 +01009340 MBEDTLS_SSL_DEBUG_MSG(3, ("sent signature scheme [%x] %s",
9341 *sig_alg,
9342 mbedtls_ssl_sig_alg_to_str(*sig_alg)));
XiaokangQianeaf36512022-04-24 09:07:44 +00009343 }
9344
9345 /* Length of supported_signature_algorithms */
9346 supported_sig_alg_len = p - supported_sig_alg;
Gilles Peskine449bd832023-01-11 14:50:10 +01009347 if (supported_sig_alg_len == 0) {
9348 MBEDTLS_SSL_DEBUG_MSG(1, ("No signature algorithms defined."));
9349 return MBEDTLS_ERR_SSL_INTERNAL_ERROR;
XiaokangQianeaf36512022-04-24 09:07:44 +00009350 }
9351
Gilles Peskine449bd832023-01-11 14:50:10 +01009352 MBEDTLS_PUT_UINT16_BE(MBEDTLS_TLS_EXT_SIG_ALG, buf, 0);
9353 MBEDTLS_PUT_UINT16_BE(supported_sig_alg_len + 2, buf, 2);
9354 MBEDTLS_PUT_UINT16_BE(supported_sig_alg_len, buf, 4);
XiaokangQianeaf36512022-04-24 09:07:44 +00009355
XiaokangQianeaf36512022-04-24 09:07:44 +00009356 *out_len = p - buf;
9357
9358#if defined(MBEDTLS_SSL_PROTO_TLS1_3)
Gilles Peskine449bd832023-01-11 14:50:10 +01009359 mbedtls_ssl_tls13_set_hs_sent_ext_mask(ssl, MBEDTLS_TLS_EXT_SIG_ALG);
XiaokangQianeaf36512022-04-24 09:07:44 +00009360#endif /* MBEDTLS_SSL_PROTO_TLS1_3 */
Jerry Yu0c354a22022-08-29 15:25:36 +08009361
Gilles Peskine449bd832023-01-11 14:50:10 +01009362 return 0;
XiaokangQianeaf36512022-04-24 09:07:44 +00009363}
Ronald Crone68ab4f2022-10-05 12:46:29 +02009364#endif /* MBEDTLS_SSL_HANDSHAKE_WITH_CERT_ENABLED */
XiaokangQianeaf36512022-04-24 09:07:44 +00009365
XiaokangQian40a35232022-05-07 09:02:40 +00009366#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
XiaokangQian9b2b7712022-05-17 02:57:00 +00009367/*
9368 * mbedtls_ssl_parse_server_name_ext
9369 *
9370 * Structure of server_name extension:
9371 *
9372 * enum {
9373 * host_name(0), (255)
9374 * } NameType;
9375 * opaque HostName<1..2^16-1>;
9376 *
9377 * struct {
9378 * NameType name_type;
9379 * select (name_type) {
9380 * case host_name: HostName;
9381 * } name;
9382 * } ServerName;
9383 * struct {
9384 * ServerName server_name_list<1..2^16-1>
9385 * } ServerNameList;
9386 */
Ronald Cronce7d76e2022-07-08 18:56:49 +02009387MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01009388int mbedtls_ssl_parse_server_name_ext(mbedtls_ssl_context *ssl,
9389 const unsigned char *buf,
9390 const unsigned char *end)
XiaokangQian40a35232022-05-07 09:02:40 +00009391{
9392 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
9393 const unsigned char *p = buf;
XiaokangQian9b2b7712022-05-17 02:57:00 +00009394 size_t server_name_list_len, hostname_len;
9395 const unsigned char *server_name_list_end;
XiaokangQian40a35232022-05-07 09:02:40 +00009396
Gilles Peskine449bd832023-01-11 14:50:10 +01009397 MBEDTLS_SSL_DEBUG_MSG(3, ("parse ServerName extension"));
XiaokangQian40a35232022-05-07 09:02:40 +00009398
Gilles Peskine449bd832023-01-11 14:50:10 +01009399 MBEDTLS_SSL_CHK_BUF_READ_PTR(p, end, 2);
9400 server_name_list_len = MBEDTLS_GET_UINT16_BE(p, 0);
XiaokangQian40a35232022-05-07 09:02:40 +00009401 p += 2;
9402
Gilles Peskine449bd832023-01-11 14:50:10 +01009403 MBEDTLS_SSL_CHK_BUF_READ_PTR(p, end, server_name_list_len);
XiaokangQian9b2b7712022-05-17 02:57:00 +00009404 server_name_list_end = p + server_name_list_len;
Gilles Peskine449bd832023-01-11 14:50:10 +01009405 while (p < server_name_list_end) {
9406 MBEDTLS_SSL_CHK_BUF_READ_PTR(p, server_name_list_end, 3);
9407 hostname_len = MBEDTLS_GET_UINT16_BE(p, 1);
9408 MBEDTLS_SSL_CHK_BUF_READ_PTR(p, server_name_list_end,
9409 hostname_len + 3);
XiaokangQian40a35232022-05-07 09:02:40 +00009410
Gilles Peskine449bd832023-01-11 14:50:10 +01009411 if (p[0] == MBEDTLS_TLS_EXT_SERVERNAME_HOSTNAME) {
XiaokangQian75fe8c72022-06-15 09:42:45 +00009412 /* sni_name is intended to be used only during the parsing of the
9413 * ClientHello message (it is reset to NULL before the end of
9414 * the message parsing). Thus it is ok to just point to the
9415 * reception buffer and not make a copy of it.
9416 */
XiaokangQianf2a94202022-05-20 06:44:24 +00009417 ssl->handshake->sni_name = p + 3;
9418 ssl->handshake->sni_name_len = hostname_len;
Gilles Peskine449bd832023-01-11 14:50:10 +01009419 if (ssl->conf->f_sni == NULL) {
9420 return 0;
XiaokangQian40a35232022-05-07 09:02:40 +00009421 }
Gilles Peskine449bd832023-01-11 14:50:10 +01009422 ret = ssl->conf->f_sni(ssl->conf->p_sni,
9423 ssl, p + 3, hostname_len);
9424 if (ret != 0) {
9425 MBEDTLS_SSL_DEBUG_RET(1, "ssl_sni_wrapper", ret);
9426 MBEDTLS_SSL_PEND_FATAL_ALERT(MBEDTLS_SSL_ALERT_MSG_UNRECOGNIZED_NAME,
9427 MBEDTLS_ERR_SSL_UNRECOGNIZED_NAME);
9428 return MBEDTLS_ERR_SSL_UNRECOGNIZED_NAME;
9429 }
9430 return 0;
XiaokangQian40a35232022-05-07 09:02:40 +00009431 }
9432
9433 p += hostname_len + 3;
9434 }
9435
Gilles Peskine449bd832023-01-11 14:50:10 +01009436 return 0;
XiaokangQian40a35232022-05-07 09:02:40 +00009437}
9438#endif /* MBEDTLS_SSL_SERVER_NAME_INDICATION */
9439
XiaokangQianacb39922022-06-17 10:18:48 +00009440#if defined(MBEDTLS_SSL_ALPN)
Ronald Cronce7d76e2022-07-08 18:56:49 +02009441MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01009442int mbedtls_ssl_parse_alpn_ext(mbedtls_ssl_context *ssl,
9443 const unsigned char *buf,
9444 const unsigned char *end)
XiaokangQianacb39922022-06-17 10:18:48 +00009445{
9446 const unsigned char *p = buf;
XiaokangQianc7403452022-06-23 03:24:12 +00009447 size_t protocol_name_list_len;
XiaokangQian95d5f542022-06-24 02:29:26 +00009448 const unsigned char *protocol_name_list;
9449 const unsigned char *protocol_name_list_end;
XiaokangQianc7403452022-06-23 03:24:12 +00009450 size_t protocol_name_len;
XiaokangQianacb39922022-06-17 10:18:48 +00009451
9452 /* If ALPN not configured, just ignore the extension */
Gilles Peskine449bd832023-01-11 14:50:10 +01009453 if (ssl->conf->alpn_list == NULL) {
9454 return 0;
9455 }
XiaokangQianacb39922022-06-17 10:18:48 +00009456
9457 /*
XiaokangQianc7403452022-06-23 03:24:12 +00009458 * RFC7301, section 3.1
9459 * opaque ProtocolName<1..2^8-1>;
XiaokangQianacb39922022-06-17 10:18:48 +00009460 *
XiaokangQianc7403452022-06-23 03:24:12 +00009461 * struct {
9462 * ProtocolName protocol_name_list<2..2^16-1>
9463 * } ProtocolNameList;
XiaokangQianacb39922022-06-17 10:18:48 +00009464 */
9465
XiaokangQianc7403452022-06-23 03:24:12 +00009466 /*
XiaokangQian0b776e22022-06-24 09:04:59 +00009467 * protocol_name_list_len 2 bytes
9468 * protocol_name_len 1 bytes
9469 * protocol_name >=1 byte
XiaokangQianc7403452022-06-23 03:24:12 +00009470 */
Gilles Peskine449bd832023-01-11 14:50:10 +01009471 MBEDTLS_SSL_CHK_BUF_READ_PTR(p, end, 4);
XiaokangQianacb39922022-06-17 10:18:48 +00009472
Gilles Peskine449bd832023-01-11 14:50:10 +01009473 protocol_name_list_len = MBEDTLS_GET_UINT16_BE(p, 0);
XiaokangQianacb39922022-06-17 10:18:48 +00009474 p += 2;
Gilles Peskine449bd832023-01-11 14:50:10 +01009475 MBEDTLS_SSL_CHK_BUF_READ_PTR(p, end, protocol_name_list_len);
XiaokangQian95d5f542022-06-24 02:29:26 +00009476 protocol_name_list = p;
9477 protocol_name_list_end = p + protocol_name_list_len;
XiaokangQianacb39922022-06-17 10:18:48 +00009478
9479 /* Validate peer's list (lengths) */
Gilles Peskine449bd832023-01-11 14:50:10 +01009480 while (p < protocol_name_list_end) {
XiaokangQian95d5f542022-06-24 02:29:26 +00009481 protocol_name_len = *p++;
Gilles Peskine449bd832023-01-11 14:50:10 +01009482 MBEDTLS_SSL_CHK_BUF_READ_PTR(p, protocol_name_list_end,
9483 protocol_name_len);
9484 if (protocol_name_len == 0) {
XiaokangQian95d5f542022-06-24 02:29:26 +00009485 MBEDTLS_SSL_PEND_FATAL_ALERT(
9486 MBEDTLS_SSL_ALERT_MSG_ILLEGAL_PARAMETER,
Gilles Peskine449bd832023-01-11 14:50:10 +01009487 MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER);
9488 return MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER;
XiaokangQian95d5f542022-06-24 02:29:26 +00009489 }
9490
9491 p += protocol_name_len;
XiaokangQianacb39922022-06-17 10:18:48 +00009492 }
9493
9494 /* Use our order of preference */
Gilles Peskine449bd832023-01-11 14:50:10 +01009495 for (const char **alpn = ssl->conf->alpn_list; *alpn != NULL; alpn++) {
9496 size_t const alpn_len = strlen(*alpn);
XiaokangQian95d5f542022-06-24 02:29:26 +00009497 p = protocol_name_list;
Gilles Peskine449bd832023-01-11 14:50:10 +01009498 while (p < protocol_name_list_end) {
XiaokangQian95d5f542022-06-24 02:29:26 +00009499 protocol_name_len = *p++;
Gilles Peskine449bd832023-01-11 14:50:10 +01009500 if (protocol_name_len == alpn_len &&
9501 memcmp(p, *alpn, alpn_len) == 0) {
XiaokangQianacb39922022-06-17 10:18:48 +00009502 ssl->alpn_chosen = *alpn;
Gilles Peskine449bd832023-01-11 14:50:10 +01009503 return 0;
XiaokangQianacb39922022-06-17 10:18:48 +00009504 }
XiaokangQian95d5f542022-06-24 02:29:26 +00009505
9506 p += protocol_name_len;
XiaokangQianacb39922022-06-17 10:18:48 +00009507 }
9508 }
9509
XiaokangQian95d5f542022-06-24 02:29:26 +00009510 /* If we get here, no match was found */
XiaokangQianacb39922022-06-17 10:18:48 +00009511 MBEDTLS_SSL_PEND_FATAL_ALERT(
Gilles Peskine449bd832023-01-11 14:50:10 +01009512 MBEDTLS_SSL_ALERT_MSG_NO_APPLICATION_PROTOCOL,
9513 MBEDTLS_ERR_SSL_NO_APPLICATION_PROTOCOL);
9514 return MBEDTLS_ERR_SSL_NO_APPLICATION_PROTOCOL;
XiaokangQianacb39922022-06-17 10:18:48 +00009515}
9516
Gilles Peskine449bd832023-01-11 14:50:10 +01009517int mbedtls_ssl_write_alpn_ext(mbedtls_ssl_context *ssl,
9518 unsigned char *buf,
9519 unsigned char *end,
9520 size_t *out_len)
XiaokangQianacb39922022-06-17 10:18:48 +00009521{
9522 unsigned char *p = buf;
XiaokangQian95d5f542022-06-24 02:29:26 +00009523 size_t protocol_name_len;
XiaokangQianc7403452022-06-23 03:24:12 +00009524 *out_len = 0;
XiaokangQianacb39922022-06-17 10:18:48 +00009525
Gilles Peskine449bd832023-01-11 14:50:10 +01009526 if (ssl->alpn_chosen == NULL) {
9527 return 0;
XiaokangQianacb39922022-06-17 10:18:48 +00009528 }
9529
Gilles Peskine449bd832023-01-11 14:50:10 +01009530 protocol_name_len = strlen(ssl->alpn_chosen);
9531 MBEDTLS_SSL_CHK_BUF_PTR(p, end, 7 + protocol_name_len);
XiaokangQianacb39922022-06-17 10:18:48 +00009532
Gilles Peskine449bd832023-01-11 14:50:10 +01009533 MBEDTLS_SSL_DEBUG_MSG(3, ("server side, adding alpn extension"));
XiaokangQianacb39922022-06-17 10:18:48 +00009534 /*
9535 * 0 . 1 ext identifier
9536 * 2 . 3 ext length
9537 * 4 . 5 protocol list length
9538 * 6 . 6 protocol name length
9539 * 7 . 7+n protocol name
9540 */
Gilles Peskine449bd832023-01-11 14:50:10 +01009541 MBEDTLS_PUT_UINT16_BE(MBEDTLS_TLS_EXT_ALPN, p, 0);
XiaokangQianacb39922022-06-17 10:18:48 +00009542
XiaokangQian95d5f542022-06-24 02:29:26 +00009543 *out_len = 7 + protocol_name_len;
XiaokangQianacb39922022-06-17 10:18:48 +00009544
Gilles Peskine449bd832023-01-11 14:50:10 +01009545 MBEDTLS_PUT_UINT16_BE(protocol_name_len + 3, p, 2);
9546 MBEDTLS_PUT_UINT16_BE(protocol_name_len + 1, p, 4);
XiaokangQian0b776e22022-06-24 09:04:59 +00009547 /* Note: the length of the chosen protocol has been checked to be less
9548 * than 255 bytes in `mbedtls_ssl_conf_alpn_protocols`.
9549 */
Gilles Peskine449bd832023-01-11 14:50:10 +01009550 p[6] = MBEDTLS_BYTE_0(protocol_name_len);
XiaokangQianacb39922022-06-17 10:18:48 +00009551
Gilles Peskine449bd832023-01-11 14:50:10 +01009552 memcpy(p + 7, ssl->alpn_chosen, protocol_name_len);
Jerry Yub95dd362022-11-08 21:19:34 +08009553
9554#if defined(MBEDTLS_SSL_PROTO_TLS1_3)
Gilles Peskine449bd832023-01-11 14:50:10 +01009555 mbedtls_ssl_tls13_set_hs_sent_ext_mask(ssl, MBEDTLS_TLS_EXT_ALPN);
Jerry Yub95dd362022-11-08 21:19:34 +08009556#endif
9557
Gilles Peskine449bd832023-01-11 14:50:10 +01009558 return 0;
XiaokangQianacb39922022-06-17 10:18:48 +00009559}
9560#endif /* MBEDTLS_SSL_ALPN */
9561
Xiaokang Qiana3b451f2022-10-11 06:20:56 +00009562#if defined(MBEDTLS_SSL_PROTO_TLS1_3) && \
Xiaokang Qian03409292022-10-12 02:49:52 +00009563 defined(MBEDTLS_SSL_SESSION_TICKETS) && \
Xiaokang Qianed0620c2022-10-12 06:58:13 +00009564 defined(MBEDTLS_SSL_SERVER_NAME_INDICATION) && \
Xiaokang Qianed3afcd2022-10-12 08:31:11 +00009565 defined(MBEDTLS_SSL_CLI_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01009566int mbedtls_ssl_session_set_hostname(mbedtls_ssl_session *session,
9567 const char *hostname)
Xiaokang Qiana3b451f2022-10-11 06:20:56 +00009568{
9569 /* Initialize to suppress unnecessary compiler warning */
9570 size_t hostname_len = 0;
9571
9572 /* Check if new hostname is valid before
9573 * making any change to current one */
Gilles Peskine449bd832023-01-11 14:50:10 +01009574 if (hostname != NULL) {
9575 hostname_len = strlen(hostname);
Xiaokang Qiana3b451f2022-10-11 06:20:56 +00009576
Gilles Peskine449bd832023-01-11 14:50:10 +01009577 if (hostname_len > MBEDTLS_SSL_MAX_HOST_NAME_LEN) {
9578 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
9579 }
Xiaokang Qiana3b451f2022-10-11 06:20:56 +00009580 }
9581
9582 /* Now it's clear that we will overwrite the old hostname,
9583 * so we can free it safely */
Gilles Peskine449bd832023-01-11 14:50:10 +01009584 if (session->hostname != NULL) {
9585 mbedtls_platform_zeroize(session->hostname,
9586 strlen(session->hostname));
9587 mbedtls_free(session->hostname);
Xiaokang Qiana3b451f2022-10-11 06:20:56 +00009588 }
9589
9590 /* Passing NULL as hostname shall clear the old one */
Gilles Peskine449bd832023-01-11 14:50:10 +01009591 if (hostname == NULL) {
Xiaokang Qiana3b451f2022-10-11 06:20:56 +00009592 session->hostname = NULL;
Gilles Peskine449bd832023-01-11 14:50:10 +01009593 } else {
9594 session->hostname = mbedtls_calloc(1, hostname_len + 1);
9595 if (session->hostname == NULL) {
9596 return MBEDTLS_ERR_SSL_ALLOC_FAILED;
9597 }
Xiaokang Qiana3b451f2022-10-11 06:20:56 +00009598
Gilles Peskine449bd832023-01-11 14:50:10 +01009599 memcpy(session->hostname, hostname, hostname_len);
Xiaokang Qiana3b451f2022-10-11 06:20:56 +00009600 }
9601
Gilles Peskine449bd832023-01-11 14:50:10 +01009602 return 0;
Xiaokang Qiana3b451f2022-10-11 06:20:56 +00009603}
Xiaokang Qian03409292022-10-12 02:49:52 +00009604#endif /* MBEDTLS_SSL_PROTO_TLS1_3 &&
9605 MBEDTLS_SSL_SESSION_TICKETS &&
Xiaokang Qianed0620c2022-10-12 06:58:13 +00009606 MBEDTLS_SSL_SERVER_NAME_INDICATION &&
Xiaokang Qianed3afcd2022-10-12 08:31:11 +00009607 MBEDTLS_SSL_CLI_C */
Xiaokang Qiana3b451f2022-10-11 06:20:56 +00009608
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009609#endif /* MBEDTLS_SSL_TLS_C */