blob: d072ddb1b87f4811b0a8d6bd3d281e89c69b960c [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,
Gilles Peskine449bd832023-01-11 14:50:10 +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,
Gilles Peskine449bd832023-01-11 14:50:10 +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);
813 if (ret != 0)
814 return ret;
815 return ssl->handshake->update_checksum(ssl, msg, msg_len);
Ronald Cron8f6d39a2022-03-10 18:56:50 +0100816}
817
Manuel Pégourié-Gonnard226aa152023-02-05 09:46:59 +0100818int mbedtls_ssl_reset_checksum(mbedtls_ssl_context *ssl)
Jerry Yuc73c6182022-02-08 20:29:25 +0800819{
Manuel Pégourié-Gonnardb72ff492023-02-06 09:54:49 +0100820#if defined(MBEDTLS_USE_PSA_CRYPTO)
821 psa_status_t status;
822#else
823 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
824#endif
Jerry Yuc73c6182022-02-08 20:29:25 +0800825 ((void) ssl);
Andrzej Kurek25f27152022-08-17 16:09:31 -0400826#if defined(MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Jerry Yuc73c6182022-02-08 20:29:25 +0800827#if defined(MBEDTLS_USE_PSA_CRYPTO)
Manuel Pégourié-Gonnardb72ff492023-02-06 09:54:49 +0100828 status = psa_hash_abort(&ssl->handshake->fin_sha256_psa);
829 if (status != PSA_SUCCESS) {
830 return mbedtls_md_error_from_psa(status);
831 }
832 status = psa_hash_setup(&ssl->handshake->fin_sha256_psa, PSA_ALG_SHA_256);
833 if (status != PSA_SUCCESS) {
834 return mbedtls_md_error_from_psa(status);
835 }
Jerry Yuc73c6182022-02-08 20:29:25 +0800836#else
Manuel Pégourié-Gonnardb72ff492023-02-06 09:54:49 +0100837 ret = mbedtls_sha256_starts(&ssl->handshake->fin_sha256, 0);
838 if (ret != 0) {
839 return ret;
840 }
Jerry Yuc73c6182022-02-08 20:29:25 +0800841#endif
842#endif
Andrzej Kurek25f27152022-08-17 16:09:31 -0400843#if defined(MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Jerry Yuc73c6182022-02-08 20:29:25 +0800844#if defined(MBEDTLS_USE_PSA_CRYPTO)
Manuel Pégourié-Gonnardb72ff492023-02-06 09:54:49 +0100845 status = psa_hash_abort(&ssl->handshake->fin_sha384_psa);
846 if (status != PSA_SUCCESS) {
847 return mbedtls_md_error_from_psa(status);
848 }
849 status = psa_hash_setup(&ssl->handshake->fin_sha384_psa, PSA_ALG_SHA_384);
850 if (status != PSA_SUCCESS) {
851 return mbedtls_md_error_from_psa(status);
852 }
Jerry Yuc73c6182022-02-08 20:29:25 +0800853#else
Manuel Pégourié-Gonnardb72ff492023-02-06 09:54:49 +0100854 ret = mbedtls_sha512_starts(&ssl->handshake->fin_sha384, 1);
855 if (ret != 0) {
856 return ret;
857 }
Jerry Yuc73c6182022-02-08 20:29:25 +0800858#endif
859#endif
Manuel Pégourié-Gonnard226aa152023-02-05 09:46:59 +0100860 return 0;
Jerry Yuc73c6182022-02-08 20:29:25 +0800861}
862
Manuel Pégourié-Gonnard226aa152023-02-05 09:46:59 +0100863static int ssl_update_checksum_start(mbedtls_ssl_context *ssl,
Gilles Peskine449bd832023-01-11 14:50:10 +0100864 const unsigned char *buf, size_t len)
Jerry Yuc73c6182022-02-08 20:29:25 +0800865{
Manuel Pégourié-Gonnarddf949012023-02-06 10:00:52 +0100866#if defined(MBEDTLS_USE_PSA_CRYPTO)
867 psa_status_t status;
868#else
869 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
870#endif
Andrzej Kurek25f27152022-08-17 16:09:31 -0400871#if defined(MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Jerry Yuc73c6182022-02-08 20:29:25 +0800872#if defined(MBEDTLS_USE_PSA_CRYPTO)
Manuel Pégourié-Gonnarddf949012023-02-06 10:00:52 +0100873 status = psa_hash_update(&ssl->handshake->fin_sha256_psa, buf, len);
874 if (status != PSA_SUCCESS) {
875 return mbedtls_md_error_from_psa(status);
876 }
Jerry Yuc73c6182022-02-08 20:29:25 +0800877#else
Manuel Pégourié-Gonnarddf949012023-02-06 10:00:52 +0100878 ret = mbedtls_sha256_update(&ssl->handshake->fin_sha256, buf, len);
879 if (ret != 0) {
880 return ret;
881 }
Jerry Yuc73c6182022-02-08 20:29:25 +0800882#endif
883#endif
Andrzej Kurek25f27152022-08-17 16:09:31 -0400884#if defined(MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Jerry Yuc73c6182022-02-08 20:29:25 +0800885#if defined(MBEDTLS_USE_PSA_CRYPTO)
Manuel Pégourié-Gonnarddf949012023-02-06 10:00:52 +0100886 status = psa_hash_update(&ssl->handshake->fin_sha384_psa, buf, len);
887 if (status != PSA_SUCCESS) {
888 return mbedtls_md_error_from_psa(status);
889 }
Jerry Yuc73c6182022-02-08 20:29:25 +0800890#else
Manuel Pégourié-Gonnarddf949012023-02-06 10:00:52 +0100891 ret = mbedtls_sha512_update(&ssl->handshake->fin_sha384, buf, len);
892 if (ret != 0) {
893 return ret;
894 }
Jerry Yuc73c6182022-02-08 20:29:25 +0800895#endif
896#endif
Andrzej Kurekeabeb302022-10-17 07:52:51 -0400897#if !defined(MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA) && \
898 !defined(MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
899 (void) ssl;
900 (void) buf;
901 (void) len;
902#endif
Manuel Pégourié-Gonnard226aa152023-02-05 09:46:59 +0100903 return 0;
Jerry Yuc73c6182022-02-08 20:29:25 +0800904}
905
Andrzej Kurek25f27152022-08-17 16:09:31 -0400906#if defined(MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Manuel Pégourié-Gonnard226aa152023-02-05 09:46:59 +0100907static int ssl_update_checksum_sha256(mbedtls_ssl_context *ssl,
Gilles Peskine449bd832023-01-11 14:50:10 +0100908 const unsigned char *buf, size_t len)
Jerry Yuc73c6182022-02-08 20:29:25 +0800909{
910#if defined(MBEDTLS_USE_PSA_CRYPTO)
Manuel Pégourié-Gonnarddf949012023-02-06 10:00:52 +0100911 return mbedtls_md_error_from_psa(psa_hash_update(
912 &ssl->handshake->fin_sha256_psa, buf, len));
Jerry Yuc73c6182022-02-08 20:29:25 +0800913#else
Manuel Pégourié-Gonnarddf949012023-02-06 10:00:52 +0100914 return mbedtls_sha256_update(&ssl->handshake->fin_sha256, buf, len);
Jerry Yuc73c6182022-02-08 20:29:25 +0800915#endif
916}
917#endif
918
Andrzej Kurek25f27152022-08-17 16:09:31 -0400919#if defined(MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Manuel Pégourié-Gonnard226aa152023-02-05 09:46:59 +0100920static int ssl_update_checksum_sha384(mbedtls_ssl_context *ssl,
Gilles Peskine449bd832023-01-11 14:50:10 +0100921 const unsigned char *buf, size_t len)
Jerry Yuc73c6182022-02-08 20:29:25 +0800922{
923#if defined(MBEDTLS_USE_PSA_CRYPTO)
Manuel Pégourié-Gonnarddf949012023-02-06 10:00:52 +0100924 return mbedtls_md_error_from_psa(psa_hash_update(
925 &ssl->handshake->fin_sha384_psa, buf, len));
Jerry Yuc73c6182022-02-08 20:29:25 +0800926#else
Manuel Pégourié-Gonnarddf949012023-02-06 10:00:52 +0100927 return mbedtls_sha512_update(&ssl->handshake->fin_sha384, buf, len);
Jerry Yuc73c6182022-02-08 20:29:25 +0800928#endif
929}
930#endif
931
Gilles Peskine449bd832023-01-11 14:50:10 +0100932static void ssl_handshake_params_init(mbedtls_ssl_handshake_params *handshake)
Paul Bakkeraccaffe2014-06-26 13:37:14 +0200933{
Gilles Peskine449bd832023-01-11 14:50:10 +0100934 memset(handshake, 0, sizeof(mbedtls_ssl_handshake_params));
Paul Bakkeraccaffe2014-06-26 13:37:14 +0200935
Andrzej Kurek25f27152022-08-17 16:09:31 -0400936#if defined(MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Andrzej Kurekeb342242019-01-29 09:14:33 -0500937#if defined(MBEDTLS_USE_PSA_CRYPTO)
938 handshake->fin_sha256_psa = psa_hash_operation_init();
Andrzej Kurekeb342242019-01-29 09:14:33 -0500939#else
Gilles Peskine449bd832023-01-11 14:50:10 +0100940 mbedtls_sha256_init(&handshake->fin_sha256);
Paul Bakkeraccaffe2014-06-26 13:37:14 +0200941#endif
Andrzej Kurekeb342242019-01-29 09:14:33 -0500942#endif
Andrzej Kurek25f27152022-08-17 16:09:31 -0400943#if defined(MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Andrzej Kurekeb342242019-01-29 09:14:33 -0500944#if defined(MBEDTLS_USE_PSA_CRYPTO)
Andrzej Kurek972fba52019-01-30 03:29:12 -0500945 handshake->fin_sha384_psa = psa_hash_operation_init();
Andrzej Kurekeb342242019-01-29 09:14:33 -0500946#else
Gilles Peskine449bd832023-01-11 14:50:10 +0100947 mbedtls_sha512_init(&handshake->fin_sha384);
Paul Bakkeraccaffe2014-06-26 13:37:14 +0200948#endif
Andrzej Kurekeb342242019-01-29 09:14:33 -0500949#endif
Paul Bakkeraccaffe2014-06-26 13:37:14 +0200950
951 handshake->update_checksum = ssl_update_checksum_start;
Hanno Becker7e5437a2017-04-28 17:15:26 +0100952
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200953#if defined(MBEDTLS_DHM_C)
Gilles Peskine449bd832023-01-11 14:50:10 +0100954 mbedtls_dhm_init(&handshake->dhm_ctx);
Paul Bakkeraccaffe2014-06-26 13:37:14 +0200955#endif
Neil Armstrongf3f46412022-04-12 14:43:39 +0200956#if !defined(MBEDTLS_USE_PSA_CRYPTO) && defined(MBEDTLS_ECDH_C)
Gilles Peskine449bd832023-01-11 14:50:10 +0100957 mbedtls_ecdh_init(&handshake->ecdh_ctx);
Paul Bakkeraccaffe2014-06-26 13:37:14 +0200958#endif
Manuel Pégourié-Gonnardeef142d2015-09-16 10:05:04 +0200959#if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
Neil Armstrongca7d5062022-05-31 14:43:23 +0200960#if defined(MBEDTLS_USE_PSA_CRYPTO)
961 handshake->psa_pake_ctx = psa_pake_operation_init();
962 handshake->psa_pake_password = MBEDTLS_SVC_KEY_ID_INIT;
963#else
Gilles Peskine449bd832023-01-11 14:50:10 +0100964 mbedtls_ecjpake_init(&handshake->ecjpake_ctx);
Neil Armstrongca7d5062022-05-31 14:43:23 +0200965#endif /* MBEDTLS_USE_PSA_CRYPTO */
Manuel Pégourié-Gonnard77c06462015-09-17 13:59:49 +0200966#if defined(MBEDTLS_SSL_CLI_C)
967 handshake->ecjpake_cache = NULL;
968 handshake->ecjpake_cache_len = 0;
969#endif
Manuel Pégourié-Gonnard76cfd3f2015-09-15 12:10:54 +0200970#endif
Manuel Pégourié-Gonnardcdc26ae2015-06-19 12:16:31 +0200971
Gilles Peskineeccd8882020-03-10 12:19:08 +0100972#if defined(MBEDTLS_SSL_ECP_RESTARTABLE_ENABLED)
Gilles Peskine449bd832023-01-11 14:50:10 +0100973 mbedtls_x509_crt_restart_init(&handshake->ecrs_ctx);
Manuel Pégourié-Gonnard862cde52017-05-17 11:56:15 +0200974#endif
975
Manuel Pégourié-Gonnardcdc26ae2015-06-19 12:16:31 +0200976#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
977 handshake->sni_authmode = MBEDTLS_SSL_VERIFY_UNSET;
978#endif
Hanno Becker75173122019-02-06 16:18:31 +0000979
980#if defined(MBEDTLS_X509_CRT_PARSE_C) && \
981 !defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
Gilles Peskine449bd832023-01-11 14:50:10 +0100982 mbedtls_pk_init(&handshake->peer_pubkey);
Hanno Becker75173122019-02-06 16:18:31 +0000983#endif
Paul Bakkeraccaffe2014-06-26 13:37:14 +0200984}
985
Gilles Peskine449bd832023-01-11 14:50:10 +0100986void mbedtls_ssl_transform_init(mbedtls_ssl_transform *transform)
Paul Bakkeraccaffe2014-06-26 13:37:14 +0200987{
Gilles Peskine449bd832023-01-11 14:50:10 +0100988 memset(transform, 0, sizeof(mbedtls_ssl_transform));
Paul Bakker84bbeb52014-07-01 14:53:22 +0200989
Przemyslaw Stekiel8f80fb92022-01-11 08:28:13 +0100990#if defined(MBEDTLS_USE_PSA_CRYPTO)
991 transform->psa_key_enc = MBEDTLS_SVC_KEY_ID_INIT;
992 transform->psa_key_dec = MBEDTLS_SVC_KEY_ID_INIT;
Przemyslaw Stekiel6be9cf52022-01-19 16:00:22 +0100993#else
Gilles Peskine449bd832023-01-11 14:50:10 +0100994 mbedtls_cipher_init(&transform->cipher_ctx_enc);
995 mbedtls_cipher_init(&transform->cipher_ctx_dec);
Przemyslaw Stekiel8f80fb92022-01-11 08:28:13 +0100996#endif
997
Hanno Beckerfd86ca82020-11-30 08:54:23 +0000998#if defined(MBEDTLS_SSL_SOME_SUITES_USE_MAC)
Neil Armstrong39b8e7d2022-02-23 09:24:45 +0100999#if defined(MBEDTLS_USE_PSA_CRYPTO)
1000 transform->psa_mac_enc = MBEDTLS_SVC_KEY_ID_INIT;
1001 transform->psa_mac_dec = MBEDTLS_SVC_KEY_ID_INIT;
Neil Armstrongcf8841a2022-02-24 11:17:45 +01001002#else
Gilles Peskine449bd832023-01-11 14:50:10 +01001003 mbedtls_md_init(&transform->md_ctx_enc);
1004 mbedtls_md_init(&transform->md_ctx_dec);
Hanno Beckerd56ed242018-01-03 15:32:51 +00001005#endif
Neil Armstrongcf8841a2022-02-24 11:17:45 +01001006#endif
Paul Bakkeraccaffe2014-06-26 13:37:14 +02001007}
1008
Gilles Peskine449bd832023-01-11 14:50:10 +01001009void mbedtls_ssl_session_init(mbedtls_ssl_session *session)
Paul Bakkeraccaffe2014-06-26 13:37:14 +02001010{
Gilles Peskine449bd832023-01-11 14:50:10 +01001011 memset(session, 0, sizeof(mbedtls_ssl_session));
Paul Bakkeraccaffe2014-06-26 13:37:14 +02001012}
1013
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02001014MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01001015static int ssl_handshake_init(mbedtls_ssl_context *ssl)
Paul Bakker48916f92012-09-16 19:57:18 +00001016{
Manuel Pégourié-Gonnardb8b07aa2023-02-06 00:34:21 +01001017 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
1018
Paul Bakkeraccaffe2014-06-26 13:37:14 +02001019 /* Clear old handshake information if present */
Jerry Yu2e199812022-12-01 18:57:19 +08001020#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
Gilles Peskine449bd832023-01-11 14:50:10 +01001021 if (ssl->transform_negotiate) {
1022 mbedtls_ssl_transform_free(ssl->transform_negotiate);
1023 }
Jerry Yu2e199812022-12-01 18:57:19 +08001024#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001025 if (ssl->session_negotiate) {
1026 mbedtls_ssl_session_free(ssl->session_negotiate);
1027 }
1028 if (ssl->handshake) {
1029 mbedtls_ssl_handshake_free(ssl);
1030 }
Paul Bakkeraccaffe2014-06-26 13:37:14 +02001031
Jerry Yu2e199812022-12-01 18:57:19 +08001032#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
Paul Bakkeraccaffe2014-06-26 13:37:14 +02001033 /*
1034 * Either the pointers are now NULL or cleared properly and can be freed.
1035 * Now allocate missing structures.
1036 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001037 if (ssl->transform_negotiate == NULL) {
1038 ssl->transform_negotiate = mbedtls_calloc(1, sizeof(mbedtls_ssl_transform));
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02001039 }
Jerry Yu2e199812022-12-01 18:57:19 +08001040#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakker48916f92012-09-16 19:57:18 +00001041
Gilles Peskine449bd832023-01-11 14:50:10 +01001042 if (ssl->session_negotiate == NULL) {
1043 ssl->session_negotiate = mbedtls_calloc(1, sizeof(mbedtls_ssl_session));
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02001044 }
Paul Bakker48916f92012-09-16 19:57:18 +00001045
Gilles Peskine449bd832023-01-11 14:50:10 +01001046 if (ssl->handshake == NULL) {
1047 ssl->handshake = mbedtls_calloc(1, sizeof(mbedtls_ssl_handshake_params));
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02001048 }
Andrzej Kurek0afa2a12020-03-03 10:39:58 -05001049#if defined(MBEDTLS_SSL_VARIABLE_BUFFER_LENGTH)
1050 /* If the buffers are too small - reallocate */
Andrzej Kurek8ea68722020-04-03 06:40:47 -04001051
Gilles Peskine449bd832023-01-11 14:50:10 +01001052 handle_buffer_resizing(ssl, 0, MBEDTLS_SSL_IN_BUFFER_LEN,
1053 MBEDTLS_SSL_OUT_BUFFER_LEN);
Andrzej Kurek0afa2a12020-03-03 10:39:58 -05001054#endif
Paul Bakker48916f92012-09-16 19:57:18 +00001055
Paul Bakkeraccaffe2014-06-26 13:37:14 +02001056 /* All pointers should exist and can be directly freed without issue */
Gilles Peskine449bd832023-01-11 14:50:10 +01001057 if (ssl->handshake == NULL ||
Jerry Yu2e199812022-12-01 18:57:19 +08001058#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
Paul Bakker48916f92012-09-16 19:57:18 +00001059 ssl->transform_negotiate == NULL ||
Jerry Yu2e199812022-12-01 18:57:19 +08001060#endif
Gilles Peskine449bd832023-01-11 14:50:10 +01001061 ssl->session_negotiate == NULL) {
1062 MBEDTLS_SSL_DEBUG_MSG(1, ("alloc() of ssl sub-contexts failed"));
Paul Bakkeraccaffe2014-06-26 13:37:14 +02001063
Gilles Peskine449bd832023-01-11 14:50:10 +01001064 mbedtls_free(ssl->handshake);
Paul Bakkeraccaffe2014-06-26 13:37:14 +02001065 ssl->handshake = NULL;
Jerry Yu2e199812022-12-01 18:57:19 +08001066
1067#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
Gilles Peskine449bd832023-01-11 14:50:10 +01001068 mbedtls_free(ssl->transform_negotiate);
Paul Bakkeraccaffe2014-06-26 13:37:14 +02001069 ssl->transform_negotiate = NULL;
Jerry Yu2e199812022-12-01 18:57:19 +08001070#endif
1071
Gilles Peskine449bd832023-01-11 14:50:10 +01001072 mbedtls_free(ssl->session_negotiate);
Paul Bakkeraccaffe2014-06-26 13:37:14 +02001073 ssl->session_negotiate = NULL;
1074
Gilles Peskine449bd832023-01-11 14:50:10 +01001075 return MBEDTLS_ERR_SSL_ALLOC_FAILED;
Paul Bakker48916f92012-09-16 19:57:18 +00001076 }
1077
Paul Bakkeraccaffe2014-06-26 13:37:14 +02001078 /* Initialize structures */
Gilles Peskine449bd832023-01-11 14:50:10 +01001079 mbedtls_ssl_session_init(ssl->session_negotiate);
1080 ssl_handshake_params_init(ssl->handshake);
Paul Bakker968afaa2014-07-09 11:09:24 +02001081
Jerry Yu2e199812022-12-01 18:57:19 +08001082#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
Gilles Peskine449bd832023-01-11 14:50:10 +01001083 mbedtls_ssl_transform_init(ssl->transform_negotiate);
Jerry Yu2e199812022-12-01 18:57:19 +08001084#endif
1085
Manuel Pégourié-Gonnard537f2312023-02-05 10:17:45 +01001086 /* Setup handshake checksums */
Manuel Pégourié-Gonnardb8b07aa2023-02-06 00:34:21 +01001087 ret = mbedtls_ssl_reset_checksum(ssl);
1088 if (ret != 0) {
1089 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ssl_reset_checksum", ret);
1090 return ret;
1091 }
Manuel Pégourié-Gonnard537f2312023-02-05 10:17:45 +01001092
Jerry Yud0766ec2022-09-22 10:46:57 +08001093#if defined(MBEDTLS_SSL_PROTO_TLS1_3) && \
1094 defined(MBEDTLS_SSL_SRV_C) && \
1095 defined(MBEDTLS_SSL_SESSION_TICKETS)
1096 ssl->handshake->new_session_tickets_count =
Gilles Peskine449bd832023-01-11 14:50:10 +01001097 ssl->conf->new_session_tickets_count;
Jerry Yud0766ec2022-09-22 10:46:57 +08001098#endif
1099
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001100#if defined(MBEDTLS_SSL_PROTO_DTLS)
Gilles Peskine449bd832023-01-11 14:50:10 +01001101 if (ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM) {
Manuel Pégourié-Gonnard06939ce2015-05-11 11:25:46 +02001102 ssl->handshake->alt_transform_out = ssl->transform_out;
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02001103
Gilles Peskine449bd832023-01-11 14:50:10 +01001104 if (ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT) {
Manuel Pégourié-Gonnard06939ce2015-05-11 11:25:46 +02001105 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_PREPARING;
Gilles Peskine449bd832023-01-11 14:50:10 +01001106 } else {
Manuel Pégourié-Gonnard06939ce2015-05-11 11:25:46 +02001107 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_WAITING;
Gilles Peskine449bd832023-01-11 14:50:10 +01001108 }
Manuel Pégourié-Gonnard286a1362015-05-13 16:22:05 +02001109
Gilles Peskine449bd832023-01-11 14:50:10 +01001110 mbedtls_ssl_set_timer(ssl, 0);
Manuel Pégourié-Gonnard06939ce2015-05-11 11:25:46 +02001111 }
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02001112#endif
1113
Brett Warrene0edc842021-08-17 09:53:13 +01001114/*
1115 * curve_list is translated to IANA TLS group identifiers here because
1116 * mbedtls_ssl_conf_curves returns void and so can't return
1117 * any error codes.
1118 */
1119#if defined(MBEDTLS_ECP_C)
1120#if !defined(MBEDTLS_DEPRECATED_REMOVED)
1121 /* Heap allocate and translate curve_list from internal to IANA group ids */
Gilles Peskine449bd832023-01-11 14:50:10 +01001122 if (ssl->conf->curve_list != NULL) {
Brett Warrene0edc842021-08-17 09:53:13 +01001123 size_t length;
1124 const mbedtls_ecp_group_id *curve_list = ssl->conf->curve_list;
1125
Gilles Peskine449bd832023-01-11 14:50:10 +01001126 for (length = 0; (curve_list[length] != MBEDTLS_ECP_DP_NONE) &&
1127 (length < MBEDTLS_ECP_DP_MAX); length++) {
1128 }
Brett Warrene0edc842021-08-17 09:53:13 +01001129
1130 /* Leave room for zero termination */
Gilles Peskine449bd832023-01-11 14:50:10 +01001131 uint16_t *group_list = mbedtls_calloc(length + 1, sizeof(uint16_t));
1132 if (group_list == NULL) {
1133 return MBEDTLS_ERR_SSL_ALLOC_FAILED;
1134 }
Brett Warrene0edc842021-08-17 09:53:13 +01001135
Gilles Peskine449bd832023-01-11 14:50:10 +01001136 for (size_t i = 0; i < length; i++) {
Valerio Setti18c9fed2022-12-30 17:44:24 +01001137 uint16_t tls_id = mbedtls_ssl_get_tls_id_from_ecp_group_id(
Gilles Peskine449bd832023-01-11 14:50:10 +01001138 curve_list[i]);
1139 if (tls_id == 0) {
1140 mbedtls_free(group_list);
1141 return MBEDTLS_ERR_SSL_BAD_CONFIG;
Brett Warrene0edc842021-08-17 09:53:13 +01001142 }
Valerio Setti18c9fed2022-12-30 17:44:24 +01001143 group_list[i] = tls_id;
Brett Warrene0edc842021-08-17 09:53:13 +01001144 }
1145
1146 group_list[length] = 0;
1147
1148 ssl->handshake->group_list = group_list;
1149 ssl->handshake->group_list_heap_allocated = 1;
Gilles Peskine449bd832023-01-11 14:50:10 +01001150 } else {
Brett Warrene0edc842021-08-17 09:53:13 +01001151 ssl->handshake->group_list = ssl->conf->group_list;
1152 ssl->handshake->group_list_heap_allocated = 0;
1153 }
1154#endif /* MBEDTLS_DEPRECATED_REMOVED */
1155#endif /* MBEDTLS_ECP_C */
1156
Ronald Crone68ab4f2022-10-05 12:46:29 +02001157#if defined(MBEDTLS_SSL_HANDSHAKE_WITH_CERT_ENABLED)
Jerry Yuf017ee42022-01-12 15:49:48 +08001158#if !defined(MBEDTLS_DEPRECATED_REMOVED)
Jerry Yua69269a2022-01-17 21:06:01 +08001159#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
Jerry Yu713013f2022-01-17 18:16:35 +08001160 /* Heap allocate and translate sig_hashes from internal hash identifiers to
1161 signature algorithms IANA identifiers. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001162 if (mbedtls_ssl_conf_is_tls12_only(ssl->conf) &&
1163 ssl->conf->sig_hashes != NULL) {
Jerry Yuf017ee42022-01-12 15:49:48 +08001164 const int *md;
1165 const int *sig_hashes = ssl->conf->sig_hashes;
Jerry Yub476a442022-01-21 18:14:45 +08001166 size_t sig_algs_len = 0;
Jerry Yuf017ee42022-01-12 15:49:48 +08001167 uint16_t *p;
1168
Jerry Yub476a442022-01-21 18:14:45 +08001169#if defined(static_assert)
Gilles Peskine449bd832023-01-11 14:50:10 +01001170 static_assert(MBEDTLS_SSL_MAX_SIG_ALG_LIST_LEN
1171 <= (SIZE_MAX - (2 * sizeof(uint16_t))),
1172 "MBEDTLS_SSL_MAX_SIG_ALG_LIST_LEN too big");
Jerry Yua68dca22022-01-20 16:28:27 +08001173#endif
1174
Gilles Peskine449bd832023-01-11 14:50:10 +01001175 for (md = sig_hashes; *md != MBEDTLS_MD_NONE; md++) {
1176 if (mbedtls_ssl_hash_from_md_alg(*md) == MBEDTLS_SSL_HASH_NONE) {
Jerry Yuf017ee42022-01-12 15:49:48 +08001177 continue;
Gilles Peskine449bd832023-01-11 14:50:10 +01001178 }
Jerry Yub476a442022-01-21 18:14:45 +08001179#if defined(MBEDTLS_ECDSA_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01001180 sig_algs_len += sizeof(uint16_t);
Jerry Yub476a442022-01-21 18:14:45 +08001181#endif
Jerry Yua68dca22022-01-20 16:28:27 +08001182
Jerry Yub476a442022-01-21 18:14:45 +08001183#if defined(MBEDTLS_RSA_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01001184 sig_algs_len += sizeof(uint16_t);
Jerry Yub476a442022-01-21 18:14:45 +08001185#endif
Gilles Peskine449bd832023-01-11 14:50:10 +01001186 if (sig_algs_len > MBEDTLS_SSL_MAX_SIG_ALG_LIST_LEN) {
1187 return MBEDTLS_ERR_SSL_BAD_CONFIG;
1188 }
Jerry Yuf017ee42022-01-12 15:49:48 +08001189 }
1190
Gilles Peskine449bd832023-01-11 14:50:10 +01001191 if (sig_algs_len < MBEDTLS_SSL_MIN_SIG_ALG_LIST_LEN) {
1192 return MBEDTLS_ERR_SSL_BAD_CONFIG;
1193 }
Jerry Yuf017ee42022-01-12 15:49:48 +08001194
Gilles Peskine449bd832023-01-11 14:50:10 +01001195 ssl->handshake->sig_algs = mbedtls_calloc(1, sig_algs_len +
1196 sizeof(uint16_t));
1197 if (ssl->handshake->sig_algs == NULL) {
1198 return MBEDTLS_ERR_SSL_ALLOC_FAILED;
1199 }
Jerry Yuf017ee42022-01-12 15:49:48 +08001200
Gilles Peskine449bd832023-01-11 14:50:10 +01001201 p = (uint16_t *) ssl->handshake->sig_algs;
1202 for (md = sig_hashes; *md != MBEDTLS_MD_NONE; md++) {
1203 unsigned char hash = mbedtls_ssl_hash_from_md_alg(*md);
1204 if (hash == MBEDTLS_SSL_HASH_NONE) {
Jerry Yuf017ee42022-01-12 15:49:48 +08001205 continue;
Gilles Peskine449bd832023-01-11 14:50:10 +01001206 }
Jerry Yu6106fdc2022-01-12 16:36:14 +08001207#if defined(MBEDTLS_ECDSA_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01001208 *p = ((hash << 8) | MBEDTLS_SSL_SIG_ECDSA);
Jerry Yuf017ee42022-01-12 15:49:48 +08001209 p++;
Jerry Yu6106fdc2022-01-12 16:36:14 +08001210#endif
1211#if defined(MBEDTLS_RSA_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01001212 *p = ((hash << 8) | MBEDTLS_SSL_SIG_RSA);
Jerry Yuf017ee42022-01-12 15:49:48 +08001213 p++;
Jerry Yu6106fdc2022-01-12 16:36:14 +08001214#endif
Jerry Yuf017ee42022-01-12 15:49:48 +08001215 }
Gabor Mezei15b95a62022-05-09 16:37:58 +02001216 *p = MBEDTLS_TLS_SIG_NONE;
Jerry Yuf017ee42022-01-12 15:49:48 +08001217 ssl->handshake->sig_algs_heap_allocated = 1;
Gilles Peskine449bd832023-01-11 14:50:10 +01001218 } else
Jerry Yua69269a2022-01-17 21:06:01 +08001219#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Jerry Yuf017ee42022-01-12 15:49:48 +08001220 {
Jerry Yuf017ee42022-01-12 15:49:48 +08001221 ssl->handshake->sig_algs_heap_allocated = 0;
1222 }
Jerry Yucc539102022-06-27 16:27:35 +08001223#endif /* !MBEDTLS_DEPRECATED_REMOVED */
Ronald Crone68ab4f2022-10-05 12:46:29 +02001224#endif /* MBEDTLS_SSL_HANDSHAKE_WITH_CERT_ENABLED */
Gilles Peskine449bd832023-01-11 14:50:10 +01001225 return 0;
Paul Bakker48916f92012-09-16 19:57:18 +00001226}
1227
Manuel Pégourié-Gonnarde057d3b2015-05-20 10:59:43 +02001228#if defined(MBEDTLS_SSL_DTLS_HELLO_VERIFY) && defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard7d38d212014-07-23 17:52:09 +02001229/* Dummy cookie callbacks for defaults */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02001230MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01001231static int ssl_cookie_write_dummy(void *ctx,
1232 unsigned char **p, unsigned char *end,
1233 const unsigned char *cli_id, size_t cli_id_len)
Manuel Pégourié-Gonnard7d38d212014-07-23 17:52:09 +02001234{
1235 ((void) ctx);
1236 ((void) p);
1237 ((void) end);
1238 ((void) cli_id);
1239 ((void) cli_id_len);
1240
Gilles Peskine449bd832023-01-11 14:50:10 +01001241 return MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
Manuel Pégourié-Gonnard7d38d212014-07-23 17:52:09 +02001242}
1243
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02001244MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01001245static int ssl_cookie_check_dummy(void *ctx,
1246 const unsigned char *cookie, size_t cookie_len,
1247 const unsigned char *cli_id, size_t cli_id_len)
Manuel Pégourié-Gonnard7d38d212014-07-23 17:52:09 +02001248{
1249 ((void) ctx);
1250 ((void) cookie);
1251 ((void) cookie_len);
1252 ((void) cli_id);
1253 ((void) cli_id_len);
1254
Gilles Peskine449bd832023-01-11 14:50:10 +01001255 return MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
Manuel Pégourié-Gonnard7d38d212014-07-23 17:52:09 +02001256}
Manuel Pégourié-Gonnarde057d3b2015-05-20 10:59:43 +02001257#endif /* MBEDTLS_SSL_DTLS_HELLO_VERIFY && MBEDTLS_SSL_SRV_C */
Manuel Pégourié-Gonnard7d38d212014-07-23 17:52:09 +02001258
Paul Bakker5121ce52009-01-03 21:22:43 +00001259/*
1260 * Initialize an SSL context
1261 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001262void mbedtls_ssl_init(mbedtls_ssl_context *ssl)
Manuel Pégourié-Gonnard41d479e2015-04-29 00:48:22 +02001263{
Gilles Peskine449bd832023-01-11 14:50:10 +01001264 memset(ssl, 0, sizeof(mbedtls_ssl_context));
Manuel Pégourié-Gonnard41d479e2015-04-29 00:48:22 +02001265}
1266
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02001267MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01001268static int ssl_conf_version_check(const mbedtls_ssl_context *ssl)
Jerry Yu60835a82021-08-04 10:13:52 +08001269{
Ronald Cron086ee0b2022-03-15 15:18:51 +01001270 const mbedtls_ssl_config *conf = ssl->conf;
1271
Ronald Cron6f135e12021-12-08 16:57:54 +01001272#if defined(MBEDTLS_SSL_PROTO_TLS1_3)
Gilles Peskine449bd832023-01-11 14:50:10 +01001273 if (mbedtls_ssl_conf_is_tls13_only(conf)) {
1274 if (conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM) {
1275 MBEDTLS_SSL_DEBUG_MSG(1, ("DTLS 1.3 is not yet supported."));
1276 return MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
XiaokangQianed582dd2022-04-13 08:21:05 +00001277 }
1278
Gilles Peskine449bd832023-01-11 14:50:10 +01001279 MBEDTLS_SSL_DEBUG_MSG(4, ("The SSL configuration is tls13 only."));
1280 return 0;
Jerry Yu60835a82021-08-04 10:13:52 +08001281 }
1282#endif
1283
1284#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
Gilles Peskine449bd832023-01-11 14:50:10 +01001285 if (mbedtls_ssl_conf_is_tls12_only(conf)) {
1286 MBEDTLS_SSL_DEBUG_MSG(4, ("The SSL configuration is tls12 only."));
1287 return 0;
Jerry Yu60835a82021-08-04 10:13:52 +08001288 }
1289#endif
1290
Ronald Cron6f135e12021-12-08 16:57:54 +01001291#if defined(MBEDTLS_SSL_PROTO_TLS1_2) && defined(MBEDTLS_SSL_PROTO_TLS1_3)
Gilles Peskine449bd832023-01-11 14:50:10 +01001292 if (mbedtls_ssl_conf_is_hybrid_tls12_tls13(conf)) {
1293 if (ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM) {
1294 MBEDTLS_SSL_DEBUG_MSG(1, ("DTLS not yet supported in Hybrid TLS 1.3 + TLS 1.2"));
1295 return MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
XiaokangQianed582dd2022-04-13 08:21:05 +00001296 }
1297
Gilles Peskine449bd832023-01-11 14:50:10 +01001298 if (conf->endpoint == MBEDTLS_SSL_IS_SERVER) {
1299 MBEDTLS_SSL_DEBUG_MSG(1, ("TLS 1.3 server is not supported yet."));
1300 return MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
XiaokangQian8f9dfe42022-04-15 02:52:39 +00001301 }
1302
1303
Gilles Peskine449bd832023-01-11 14:50:10 +01001304 MBEDTLS_SSL_DEBUG_MSG(4, ("The SSL configuration is TLS 1.3 or TLS 1.2."));
1305 return 0;
Jerry Yu60835a82021-08-04 10:13:52 +08001306 }
1307#endif
1308
Gilles Peskine449bd832023-01-11 14:50:10 +01001309 MBEDTLS_SSL_DEBUG_MSG(1, ("The SSL configuration is invalid."));
1310 return MBEDTLS_ERR_SSL_BAD_CONFIG;
Jerry Yu60835a82021-08-04 10:13:52 +08001311}
1312
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02001313MBEDTLS_CHECK_RETURN_CRITICAL
Jerry Yu60835a82021-08-04 10:13:52 +08001314static int ssl_conf_check(const mbedtls_ssl_context *ssl)
1315{
1316 int ret;
Gilles Peskine449bd832023-01-11 14:50:10 +01001317 ret = ssl_conf_version_check(ssl);
1318 if (ret != 0) {
1319 return ret;
1320 }
Jerry Yu60835a82021-08-04 10:13:52 +08001321
Jerry Yudef7ae42022-10-30 14:13:19 +08001322#if defined(MBEDTLS_SSL_PROTO_TLS1_3)
1323 /* RFC 8446 section 4.4.3
1324 *
1325 * If the verification fails, the receiver MUST terminate the handshake with
1326 * a "decrypt_error" alert.
1327 *
1328 * If the client is configured as TLS 1.3 only with optional verify, return
1329 * bad config.
1330 *
1331 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001332 if (mbedtls_ssl_conf_tls13_ephemeral_enabled(
1333 (mbedtls_ssl_context *) ssl) &&
Jerry Yudef7ae42022-10-30 14:13:19 +08001334 ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT &&
1335 ssl->conf->max_tls_version == MBEDTLS_SSL_VERSION_TLS1_3 &&
1336 ssl->conf->min_tls_version == MBEDTLS_SSL_VERSION_TLS1_3 &&
Gilles Peskine449bd832023-01-11 14:50:10 +01001337 ssl->conf->authmode == MBEDTLS_SSL_VERIFY_OPTIONAL) {
Jerry Yudef7ae42022-10-30 14:13:19 +08001338 MBEDTLS_SSL_DEBUG_MSG(
Gilles Peskine449bd832023-01-11 14:50:10 +01001339 1, ("Optional verify auth mode "
1340 "is not available for TLS 1.3 client"));
1341 return MBEDTLS_ERR_SSL_BAD_CONFIG;
Jerry Yudef7ae42022-10-30 14:13:19 +08001342 }
1343#endif /* MBEDTLS_SSL_PROTO_TLS1_3 */
1344
Jerry Yu60835a82021-08-04 10:13:52 +08001345 /* Space for further checks */
1346
Gilles Peskine449bd832023-01-11 14:50:10 +01001347 return 0;
Jerry Yu60835a82021-08-04 10:13:52 +08001348}
1349
Manuel Pégourié-Gonnard41d479e2015-04-29 00:48:22 +02001350/*
1351 * Setup an SSL context
1352 */
Hanno Becker2a43f6f2018-08-10 11:12:52 +01001353
Gilles Peskine449bd832023-01-11 14:50:10 +01001354int mbedtls_ssl_setup(mbedtls_ssl_context *ssl,
1355 const mbedtls_ssl_config *conf)
Paul Bakker5121ce52009-01-03 21:22:43 +00001356{
Janos Follath865b3eb2019-12-16 11:46:15 +00001357 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Darryl Greenb33cc762019-11-28 14:29:44 +00001358 size_t in_buf_len = MBEDTLS_SSL_IN_BUFFER_LEN;
1359 size_t out_buf_len = MBEDTLS_SSL_OUT_BUFFER_LEN;
Paul Bakker5121ce52009-01-03 21:22:43 +00001360
Manuel Pégourié-Gonnarddef0bbe2015-05-04 14:56:36 +02001361 ssl->conf = conf;
Paul Bakker62f2dee2012-09-28 07:31:51 +00001362
Gilles Peskine449bd832023-01-11 14:50:10 +01001363 if ((ret = ssl_conf_check(ssl)) != 0) {
1364 return ret;
1365 }
Jerry Yu60835a82021-08-04 10:13:52 +08001366
Paul Bakker62f2dee2012-09-28 07:31:51 +00001367 /*
Manuel Pégourié-Gonnard06193482014-02-14 08:39:32 +01001368 * Prepare base structures
Paul Bakker62f2dee2012-09-28 07:31:51 +00001369 */
k-stachowiakc9a5f022018-07-24 13:53:31 +02001370
1371 /* Set to NULL in case of an error condition */
1372 ssl->out_buf = NULL;
k-stachowiaka47911c2018-07-04 17:41:58 +02001373
Darryl Greenb33cc762019-11-28 14:29:44 +00001374#if defined(MBEDTLS_SSL_VARIABLE_BUFFER_LENGTH)
1375 ssl->in_buf_len = in_buf_len;
1376#endif
Gilles Peskine449bd832023-01-11 14:50:10 +01001377 ssl->in_buf = mbedtls_calloc(1, in_buf_len);
1378 if (ssl->in_buf == NULL) {
1379 MBEDTLS_SSL_DEBUG_MSG(1, ("alloc(%" MBEDTLS_PRINTF_SIZET " bytes) failed", in_buf_len));
k-stachowiak9f7798e2018-07-31 16:52:32 +02001380 ret = MBEDTLS_ERR_SSL_ALLOC_FAILED;
k-stachowiaka47911c2018-07-04 17:41:58 +02001381 goto error;
Angus Grattond8213d02016-05-25 20:56:48 +10001382 }
1383
Darryl Greenb33cc762019-11-28 14:29:44 +00001384#if defined(MBEDTLS_SSL_VARIABLE_BUFFER_LENGTH)
1385 ssl->out_buf_len = out_buf_len;
1386#endif
Gilles Peskine449bd832023-01-11 14:50:10 +01001387 ssl->out_buf = mbedtls_calloc(1, out_buf_len);
1388 if (ssl->out_buf == NULL) {
1389 MBEDTLS_SSL_DEBUG_MSG(1, ("alloc(%" MBEDTLS_PRINTF_SIZET " bytes) failed", out_buf_len));
k-stachowiak9f7798e2018-07-31 16:52:32 +02001390 ret = MBEDTLS_ERR_SSL_ALLOC_FAILED;
k-stachowiaka47911c2018-07-04 17:41:58 +02001391 goto error;
Paul Bakker5121ce52009-01-03 21:22:43 +00001392 }
1393
Gilles Peskine449bd832023-01-11 14:50:10 +01001394 mbedtls_ssl_reset_in_out_pointers(ssl);
Manuel Pégourié-Gonnard419d5ae2015-05-04 19:32:36 +02001395
Johan Pascalb62bb512015-12-03 21:56:45 +01001396#if defined(MBEDTLS_SSL_DTLS_SRTP)
Gilles Peskine449bd832023-01-11 14:50:10 +01001397 memset(&ssl->dtls_srtp_info, 0, sizeof(ssl->dtls_srtp_info));
Johan Pascalb62bb512015-12-03 21:56:45 +01001398#endif
1399
Gilles Peskine449bd832023-01-11 14:50:10 +01001400 if ((ret = ssl_handshake_init(ssl)) != 0) {
k-stachowiaka47911c2018-07-04 17:41:58 +02001401 goto error;
Gilles Peskine449bd832023-01-11 14:50:10 +01001402 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001403
Gilles Peskine449bd832023-01-11 14:50:10 +01001404 return 0;
k-stachowiaka47911c2018-07-04 17:41:58 +02001405
1406error:
Gilles Peskine449bd832023-01-11 14:50:10 +01001407 mbedtls_free(ssl->in_buf);
1408 mbedtls_free(ssl->out_buf);
k-stachowiaka47911c2018-07-04 17:41:58 +02001409
1410 ssl->conf = NULL;
1411
Darryl Greenb33cc762019-11-28 14:29:44 +00001412#if defined(MBEDTLS_SSL_VARIABLE_BUFFER_LENGTH)
1413 ssl->in_buf_len = 0;
1414 ssl->out_buf_len = 0;
1415#endif
k-stachowiaka47911c2018-07-04 17:41:58 +02001416 ssl->in_buf = NULL;
1417 ssl->out_buf = NULL;
1418
1419 ssl->in_hdr = NULL;
1420 ssl->in_ctr = NULL;
1421 ssl->in_len = NULL;
1422 ssl->in_iv = NULL;
1423 ssl->in_msg = NULL;
1424
1425 ssl->out_hdr = NULL;
1426 ssl->out_ctr = NULL;
1427 ssl->out_len = NULL;
1428 ssl->out_iv = NULL;
1429 ssl->out_msg = NULL;
1430
Gilles Peskine449bd832023-01-11 14:50:10 +01001431 return ret;
Paul Bakker5121ce52009-01-03 21:22:43 +00001432}
1433
1434/*
Paul Bakker7eb013f2011-10-06 12:37:39 +00001435 * Reset an initialized and used SSL context for re-use while retaining
1436 * all application-set variables, function pointers and data.
Manuel Pégourié-Gonnard3f09b6d2015-09-08 11:58:14 +02001437 *
1438 * If partial is non-zero, keep data in the input buffer and client ID.
1439 * (Use when a DTLS client reconnects from the same port.)
Paul Bakker7eb013f2011-10-06 12:37:39 +00001440 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001441void mbedtls_ssl_session_reset_msg_layer(mbedtls_ssl_context *ssl,
1442 int partial)
Paul Bakker7eb013f2011-10-06 12:37:39 +00001443{
Darryl Greenb33cc762019-11-28 14:29:44 +00001444#if defined(MBEDTLS_SSL_VARIABLE_BUFFER_LENGTH)
1445 size_t in_buf_len = ssl->in_buf_len;
1446 size_t out_buf_len = ssl->out_buf_len;
1447#else
1448 size_t in_buf_len = MBEDTLS_SSL_IN_BUFFER_LEN;
1449 size_t out_buf_len = MBEDTLS_SSL_OUT_BUFFER_LEN;
1450#endif
Paul Bakker48916f92012-09-16 19:57:18 +00001451
Hanno Beckerb0302c42021-08-03 09:39:42 +01001452#if !defined(MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE) || !defined(MBEDTLS_SSL_SRV_C)
1453 partial = 0;
Hanno Becker7e772132018-08-10 12:38:21 +01001454#endif
1455
Manuel Pégourié-Gonnard286a1362015-05-13 16:22:05 +02001456 /* Cancel any possibly running timer */
Gilles Peskine449bd832023-01-11 14:50:10 +01001457 mbedtls_ssl_set_timer(ssl, 0);
Manuel Pégourié-Gonnard286a1362015-05-13 16:22:05 +02001458
Gilles Peskine449bd832023-01-11 14:50:10 +01001459 mbedtls_ssl_reset_in_out_pointers(ssl);
Hanno Beckerb0302c42021-08-03 09:39:42 +01001460
1461 /* Reset incoming message parsing */
1462 ssl->in_offt = NULL;
1463 ssl->nb_zero = 0;
1464 ssl->in_msgtype = 0;
1465 ssl->in_msglen = 0;
1466 ssl->in_hslen = 0;
1467 ssl->keep_current_message = 0;
1468 ssl->transform_in = NULL;
1469
1470#if defined(MBEDTLS_SSL_PROTO_DTLS)
1471 ssl->next_record_offset = 0;
1472 ssl->in_epoch = 0;
1473#endif
1474
1475 /* Keep current datagram if partial == 1 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001476 if (partial == 0) {
Hanno Beckerb0302c42021-08-03 09:39:42 +01001477 ssl->in_left = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +01001478 memset(ssl->in_buf, 0, in_buf_len);
Hanno Beckerb0302c42021-08-03 09:39:42 +01001479 }
1480
Ronald Cronad8c17b2022-06-10 17:18:09 +02001481 ssl->send_alert = 0;
1482
Hanno Beckerb0302c42021-08-03 09:39:42 +01001483 /* Reset outgoing message writing */
1484 ssl->out_msgtype = 0;
1485 ssl->out_msglen = 0;
1486 ssl->out_left = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +01001487 memset(ssl->out_buf, 0, out_buf_len);
1488 memset(ssl->cur_out_ctr, 0, sizeof(ssl->cur_out_ctr));
Hanno Beckerb0302c42021-08-03 09:39:42 +01001489 ssl->transform_out = NULL;
1490
1491#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
Gilles Peskine449bd832023-01-11 14:50:10 +01001492 mbedtls_ssl_dtls_replay_reset(ssl);
Hanno Beckerb0302c42021-08-03 09:39:42 +01001493#endif
1494
XiaokangQian2b01dc32022-01-21 02:53:13 +00001495#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
Gilles Peskine449bd832023-01-11 14:50:10 +01001496 if (ssl->transform) {
1497 mbedtls_ssl_transform_free(ssl->transform);
1498 mbedtls_free(ssl->transform);
Hanno Beckerb0302c42021-08-03 09:39:42 +01001499 ssl->transform = NULL;
1500 }
XiaokangQian2b01dc32022-01-21 02:53:13 +00001501#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
1502
XiaokangQian2b01dc32022-01-21 02:53:13 +00001503#if defined(MBEDTLS_SSL_PROTO_TLS1_3)
Gilles Peskine449bd832023-01-11 14:50:10 +01001504 mbedtls_ssl_transform_free(ssl->transform_application);
1505 mbedtls_free(ssl->transform_application);
XiaokangQian2b01dc32022-01-21 02:53:13 +00001506 ssl->transform_application = NULL;
1507
Gilles Peskine449bd832023-01-11 14:50:10 +01001508 if (ssl->handshake != NULL) {
Jerry Yu3d9b5902022-11-04 14:07:25 +08001509#if defined(MBEDTLS_SSL_EARLY_DATA)
Gilles Peskine449bd832023-01-11 14:50:10 +01001510 mbedtls_ssl_transform_free(ssl->handshake->transform_earlydata);
1511 mbedtls_free(ssl->handshake->transform_earlydata);
XiaokangQian2b01dc32022-01-21 02:53:13 +00001512 ssl->handshake->transform_earlydata = NULL;
Jerry Yu3d9b5902022-11-04 14:07:25 +08001513#endif
XiaokangQian2b01dc32022-01-21 02:53:13 +00001514
Gilles Peskine449bd832023-01-11 14:50:10 +01001515 mbedtls_ssl_transform_free(ssl->handshake->transform_handshake);
1516 mbedtls_free(ssl->handshake->transform_handshake);
XiaokangQian2b01dc32022-01-21 02:53:13 +00001517 ssl->handshake->transform_handshake = NULL;
1518 }
1519
XiaokangQian2b01dc32022-01-21 02:53:13 +00001520#endif /* MBEDTLS_SSL_PROTO_TLS1_3 */
Hanno Beckerb0302c42021-08-03 09:39:42 +01001521}
1522
Gilles Peskine449bd832023-01-11 14:50:10 +01001523int mbedtls_ssl_session_reset_int(mbedtls_ssl_context *ssl, int partial)
Hanno Beckerb0302c42021-08-03 09:39:42 +01001524{
1525 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
1526
1527 ssl->state = MBEDTLS_SSL_HELLO_REQUEST;
1528
Gilles Peskine449bd832023-01-11 14:50:10 +01001529 mbedtls_ssl_session_reset_msg_layer(ssl, partial);
Hanno Beckerb0302c42021-08-03 09:39:42 +01001530
1531 /* Reset renegotiation state */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001532#if defined(MBEDTLS_SSL_RENEGOTIATION)
1533 ssl->renego_status = MBEDTLS_SSL_INITIAL_HANDSHAKE;
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01001534 ssl->renego_records_seen = 0;
Paul Bakker48916f92012-09-16 19:57:18 +00001535
1536 ssl->verify_data_len = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +01001537 memset(ssl->own_verify_data, 0, MBEDTLS_SSL_VERIFY_DATA_MAX_LEN);
1538 memset(ssl->peer_verify_data, 0, MBEDTLS_SSL_VERIFY_DATA_MAX_LEN);
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01001539#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001540 ssl->secure_renegotiation = MBEDTLS_SSL_LEGACY_RENEGOTIATION;
Paul Bakker48916f92012-09-16 19:57:18 +00001541
Hanno Beckerb0302c42021-08-03 09:39:42 +01001542 ssl->session_in = NULL;
Hanno Becker78640902018-08-13 16:35:15 +01001543 ssl->session_out = NULL;
Gilles Peskine449bd832023-01-11 14:50:10 +01001544 if (ssl->session) {
1545 mbedtls_ssl_session_free(ssl->session);
1546 mbedtls_free(ssl->session);
Paul Bakkerc0463502013-02-14 11:19:38 +01001547 ssl->session = NULL;
1548 }
1549
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001550#if defined(MBEDTLS_SSL_ALPN)
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02001551 ssl->alpn_chosen = NULL;
1552#endif
1553
Manuel Pégourié-Gonnarde057d3b2015-05-20 10:59:43 +02001554#if defined(MBEDTLS_SSL_DTLS_HELLO_VERIFY) && defined(MBEDTLS_SSL_SRV_C)
David Horstmann3b2276a2022-10-06 14:49:08 +01001555 int free_cli_id = 1;
Hanno Becker4ccbf062018-08-10 11:20:38 +01001556#if defined(MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE)
Gilles Peskine449bd832023-01-11 14:50:10 +01001557 free_cli_id = (partial == 0);
Hanno Becker4ccbf062018-08-10 11:20:38 +01001558#endif
Gilles Peskine449bd832023-01-11 14:50:10 +01001559 if (free_cli_id) {
1560 mbedtls_free(ssl->cli_id);
Manuel Pégourié-Gonnard3f09b6d2015-09-08 11:58:14 +02001561 ssl->cli_id = NULL;
1562 ssl->cli_id_len = 0;
1563 }
Manuel Pégourié-Gonnard43c02182014-07-22 17:32:01 +02001564#endif
1565
Gilles Peskine449bd832023-01-11 14:50:10 +01001566 if ((ret = ssl_handshake_init(ssl)) != 0) {
1567 return ret;
1568 }
Paul Bakker2770fbd2012-07-03 13:30:23 +00001569
Gilles Peskine449bd832023-01-11 14:50:10 +01001570 return 0;
Paul Bakker7eb013f2011-10-06 12:37:39 +00001571}
1572
Manuel Pégourié-Gonnard779e4292013-08-03 13:50:48 +02001573/*
Manuel Pégourié-Gonnard3f09b6d2015-09-08 11:58:14 +02001574 * Reset an initialized and used SSL context for re-use while retaining
1575 * all application-set variables, function pointers and data.
1576 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001577int mbedtls_ssl_session_reset(mbedtls_ssl_context *ssl)
Manuel Pégourié-Gonnard3f09b6d2015-09-08 11:58:14 +02001578{
Gilles Peskine449bd832023-01-11 14:50:10 +01001579 return mbedtls_ssl_session_reset_int(ssl, 0);
Manuel Pégourié-Gonnard3f09b6d2015-09-08 11:58:14 +02001580}
1581
1582/*
Paul Bakker5121ce52009-01-03 21:22:43 +00001583 * SSL set accessors
1584 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001585void mbedtls_ssl_conf_endpoint(mbedtls_ssl_config *conf, int endpoint)
Paul Bakker5121ce52009-01-03 21:22:43 +00001586{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02001587 conf->endpoint = endpoint;
Paul Bakker5121ce52009-01-03 21:22:43 +00001588}
1589
Gilles Peskine449bd832023-01-11 14:50:10 +01001590void mbedtls_ssl_conf_transport(mbedtls_ssl_config *conf, int transport)
Manuel Pégourié-Gonnard0b1ff292014-02-06 13:04:16 +01001591{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02001592 conf->transport = transport;
Manuel Pégourié-Gonnard0b1ff292014-02-06 13:04:16 +01001593}
1594
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001595#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
Gilles Peskine449bd832023-01-11 14:50:10 +01001596void mbedtls_ssl_conf_dtls_anti_replay(mbedtls_ssl_config *conf, char mode)
Manuel Pégourié-Gonnard27393132014-09-24 14:41:11 +02001597{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02001598 conf->anti_replay = mode;
Manuel Pégourié-Gonnard27393132014-09-24 14:41:11 +02001599}
1600#endif
1601
Gilles Peskine449bd832023-01-11 14:50:10 +01001602void mbedtls_ssl_conf_dtls_badmac_limit(mbedtls_ssl_config *conf, unsigned limit)
Manuel Pégourié-Gonnardb0643d12014-10-14 18:30:36 +02001603{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02001604 conf->badmac_limit = limit;
Manuel Pégourié-Gonnardb0643d12014-10-14 18:30:36 +02001605}
Manuel Pégourié-Gonnardb0643d12014-10-14 18:30:36 +02001606
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001607#if defined(MBEDTLS_SSL_PROTO_DTLS)
Hanno Becker04da1892018-08-14 13:22:10 +01001608
Gilles Peskine449bd832023-01-11 14:50:10 +01001609void mbedtls_ssl_set_datagram_packing(mbedtls_ssl_context *ssl,
1610 unsigned allow_packing)
Hanno Becker04da1892018-08-14 13:22:10 +01001611{
1612 ssl->disable_datagram_packing = !allow_packing;
1613}
1614
Gilles Peskine449bd832023-01-11 14:50:10 +01001615void mbedtls_ssl_conf_handshake_timeout(mbedtls_ssl_config *conf,
1616 uint32_t min, uint32_t max)
Manuel Pégourié-Gonnard905dd242014-10-01 12:03:55 +02001617{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02001618 conf->hs_timeout_min = min;
1619 conf->hs_timeout_max = max;
Manuel Pégourié-Gonnard905dd242014-10-01 12:03:55 +02001620}
1621#endif
1622
Gilles Peskine449bd832023-01-11 14:50:10 +01001623void mbedtls_ssl_conf_authmode(mbedtls_ssl_config *conf, int authmode)
Paul Bakker5121ce52009-01-03 21:22:43 +00001624{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02001625 conf->authmode = authmode;
Paul Bakker5121ce52009-01-03 21:22:43 +00001626}
1627
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001628#if defined(MBEDTLS_X509_CRT_PARSE_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01001629void mbedtls_ssl_conf_verify(mbedtls_ssl_config *conf,
1630 int (*f_vrfy)(void *, mbedtls_x509_crt *, int, uint32_t *),
1631 void *p_vrfy)
Paul Bakkerb63b0af2011-01-13 17:54:59 +00001632{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02001633 conf->f_vrfy = f_vrfy;
1634 conf->p_vrfy = p_vrfy;
Paul Bakkerb63b0af2011-01-13 17:54:59 +00001635}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001636#endif /* MBEDTLS_X509_CRT_PARSE_C */
Paul Bakkerb63b0af2011-01-13 17:54:59 +00001637
Gilles Peskine449bd832023-01-11 14:50:10 +01001638void mbedtls_ssl_conf_rng(mbedtls_ssl_config *conf,
1639 int (*f_rng)(void *, unsigned char *, size_t),
1640 void *p_rng)
Paul Bakker5121ce52009-01-03 21:22:43 +00001641{
Manuel Pégourié-Gonnard750e4d72015-05-07 12:35:38 +01001642 conf->f_rng = f_rng;
1643 conf->p_rng = p_rng;
Paul Bakker5121ce52009-01-03 21:22:43 +00001644}
1645
Gilles Peskine449bd832023-01-11 14:50:10 +01001646void mbedtls_ssl_conf_dbg(mbedtls_ssl_config *conf,
1647 void (*f_dbg)(void *, int, const char *, int, const char *),
1648 void *p_dbg)
Paul Bakker5121ce52009-01-03 21:22:43 +00001649{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02001650 conf->f_dbg = f_dbg;
1651 conf->p_dbg = p_dbg;
Paul Bakker5121ce52009-01-03 21:22:43 +00001652}
1653
Gilles Peskine449bd832023-01-11 14:50:10 +01001654void mbedtls_ssl_set_bio(mbedtls_ssl_context *ssl,
1655 void *p_bio,
1656 mbedtls_ssl_send_t *f_send,
1657 mbedtls_ssl_recv_t *f_recv,
1658 mbedtls_ssl_recv_timeout_t *f_recv_timeout)
Manuel Pégourié-Gonnard8fa6dfd2014-09-17 10:47:43 +02001659{
1660 ssl->p_bio = p_bio;
1661 ssl->f_send = f_send;
1662 ssl->f_recv = f_recv;
1663 ssl->f_recv_timeout = f_recv_timeout;
Manuel Pégourié-Gonnard97fd52c2015-05-06 15:38:52 +01001664}
1665
Manuel Pégourié-Gonnard6e7aaca2018-08-20 10:37:23 +02001666#if defined(MBEDTLS_SSL_PROTO_DTLS)
Gilles Peskine449bd832023-01-11 14:50:10 +01001667void mbedtls_ssl_set_mtu(mbedtls_ssl_context *ssl, uint16_t mtu)
Manuel Pégourié-Gonnard6e7aaca2018-08-20 10:37:23 +02001668{
1669 ssl->mtu = mtu;
1670}
1671#endif
1672
Gilles Peskine449bd832023-01-11 14:50:10 +01001673void mbedtls_ssl_conf_read_timeout(mbedtls_ssl_config *conf, uint32_t timeout)
Manuel Pégourié-Gonnard97fd52c2015-05-06 15:38:52 +01001674{
1675 conf->read_timeout = timeout;
Manuel Pégourié-Gonnard8fa6dfd2014-09-17 10:47:43 +02001676}
1677
Gilles Peskine449bd832023-01-11 14:50:10 +01001678void mbedtls_ssl_set_timer_cb(mbedtls_ssl_context *ssl,
1679 void *p_timer,
1680 mbedtls_ssl_set_timer_t *f_set_timer,
1681 mbedtls_ssl_get_timer_t *f_get_timer)
Manuel Pégourié-Gonnard2e012912015-05-12 20:55:41 +02001682{
1683 ssl->p_timer = p_timer;
1684 ssl->f_set_timer = f_set_timer;
1685 ssl->f_get_timer = f_get_timer;
Manuel Pégourié-Gonnard286a1362015-05-13 16:22:05 +02001686
1687 /* Make sure we start with no timer running */
Gilles Peskine449bd832023-01-11 14:50:10 +01001688 mbedtls_ssl_set_timer(ssl, 0);
Manuel Pégourié-Gonnard2e012912015-05-12 20:55:41 +02001689}
1690
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001691#if defined(MBEDTLS_SSL_SRV_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01001692void mbedtls_ssl_conf_session_cache(mbedtls_ssl_config *conf,
1693 void *p_cache,
1694 mbedtls_ssl_cache_get_t *f_get_cache,
1695 mbedtls_ssl_cache_set_t *f_set_cache)
Paul Bakker5121ce52009-01-03 21:22:43 +00001696{
Manuel Pégourié-Gonnard5cb33082015-05-06 18:06:26 +01001697 conf->p_cache = p_cache;
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02001698 conf->f_get_cache = f_get_cache;
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02001699 conf->f_set_cache = f_set_cache;
Paul Bakker5121ce52009-01-03 21:22:43 +00001700}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001701#endif /* MBEDTLS_SSL_SRV_C */
Paul Bakker5121ce52009-01-03 21:22:43 +00001702
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001703#if defined(MBEDTLS_SSL_CLI_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01001704int mbedtls_ssl_set_session(mbedtls_ssl_context *ssl, const mbedtls_ssl_session *session)
Paul Bakker5121ce52009-01-03 21:22:43 +00001705{
Janos Follath865b3eb2019-12-16 11:46:15 +00001706 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +02001707
Gilles Peskine449bd832023-01-11 14:50:10 +01001708 if (ssl == NULL ||
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +02001709 session == NULL ||
1710 ssl->session_negotiate == NULL ||
Gilles Peskine449bd832023-01-11 14:50:10 +01001711 ssl->conf->endpoint != MBEDTLS_SSL_IS_CLIENT) {
1712 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +02001713 }
1714
Gilles Peskine449bd832023-01-11 14:50:10 +01001715 if (ssl->handshake->resume == 1) {
1716 return MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
1717 }
Hanno Beckere810bbc2021-05-14 16:01:05 +01001718
Jerry Yu21092062022-10-10 21:21:31 +08001719#if defined(MBEDTLS_SSL_PROTO_TLS1_3)
Gilles Peskine449bd832023-01-11 14:50:10 +01001720 if (session->tls_version == MBEDTLS_SSL_VERSION_TLS1_3) {
Jerry Yu21092062022-10-10 21:21:31 +08001721 const mbedtls_ssl_ciphersuite_t *ciphersuite_info =
Gilles Peskine449bd832023-01-11 14:50:10 +01001722 mbedtls_ssl_ciphersuite_from_id(session->ciphersuite);
Jerry Yu21092062022-10-10 21:21:31 +08001723
Gilles Peskine449bd832023-01-11 14:50:10 +01001724 if (mbedtls_ssl_validate_ciphersuite(
Jerry Yu21092062022-10-10 21:21:31 +08001725 ssl, ciphersuite_info, MBEDTLS_SSL_VERSION_TLS1_3,
Gilles Peskine449bd832023-01-11 14:50:10 +01001726 MBEDTLS_SSL_VERSION_TLS1_3) != 0) {
1727 MBEDTLS_SSL_DEBUG_MSG(4, ("%d is not a valid TLS 1.3 ciphersuite.",
1728 session->ciphersuite));
1729 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
Jerry Yu21092062022-10-10 21:21:31 +08001730 }
Jerry Yu40afab62022-10-08 10:42:13 +08001731 }
Jerry Yu21092062022-10-10 21:21:31 +08001732#endif /* MBEDTLS_SSL_PROTO_TLS1_3 */
Jerry Yu40afab62022-10-08 10:42:13 +08001733
Gilles Peskine449bd832023-01-11 14:50:10 +01001734 if ((ret = mbedtls_ssl_session_copy(ssl->session_negotiate,
1735 session)) != 0) {
1736 return ret;
1737 }
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +02001738
Paul Bakker0a597072012-09-25 21:55:46 +00001739 ssl->handshake->resume = 1;
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +02001740
Gilles Peskine449bd832023-01-11 14:50:10 +01001741 return 0;
Paul Bakker5121ce52009-01-03 21:22:43 +00001742}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001743#endif /* MBEDTLS_SSL_CLI_C */
Paul Bakker5121ce52009-01-03 21:22:43 +00001744
Gilles Peskine449bd832023-01-11 14:50:10 +01001745void mbedtls_ssl_conf_ciphersuites(mbedtls_ssl_config *conf,
1746 const int *ciphersuites)
Mateusz Starzyk06b07fb2021-02-18 13:55:21 +01001747{
Hanno Beckerd60b6c62021-04-29 12:04:11 +01001748 conf->ciphersuite_list = ciphersuites;
Paul Bakker5121ce52009-01-03 21:22:43 +00001749}
1750
Ronald Cron6f135e12021-12-08 16:57:54 +01001751#if defined(MBEDTLS_SSL_PROTO_TLS1_3)
Gilles Peskine449bd832023-01-11 14:50:10 +01001752void mbedtls_ssl_conf_tls13_key_exchange_modes(mbedtls_ssl_config *conf,
1753 const int kex_modes)
Hanno Becker71f1ed62021-07-24 06:01:47 +01001754{
Xiaofei Bai746f9482021-11-12 08:53:56 +00001755 conf->tls13_kex_modes = kex_modes & MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_ALL;
Hanno Becker71f1ed62021-07-24 06:01:47 +01001756}
Xiaokang Qian72de95d2022-10-25 02:54:33 +00001757
1758#if defined(MBEDTLS_SSL_EARLY_DATA)
Gilles Peskine449bd832023-01-11 14:50:10 +01001759void mbedtls_ssl_tls13_conf_early_data(mbedtls_ssl_config *conf,
1760 int early_data_enabled)
Xiaokang Qian72de95d2022-10-25 02:54:33 +00001761{
1762 conf->early_data_enabled = early_data_enabled;
1763}
Jerry Yucc4e0072022-11-22 17:22:22 +08001764
1765#if defined(MBEDTLS_SSL_SRV_C)
1766void mbedtls_ssl_tls13_conf_max_early_data_size(
Gilles Peskine449bd832023-01-11 14:50:10 +01001767 mbedtls_ssl_config *conf, uint32_t max_early_data_size)
Jerry Yucc4e0072022-11-22 17:22:22 +08001768{
Jerry Yu39da9852022-12-06 16:58:36 +08001769 conf->max_early_data_size = max_early_data_size;
Jerry Yucc4e0072022-11-22 17:22:22 +08001770}
1771#endif /* MBEDTLS_SSL_SRV_C */
1772
Xiaokang Qian72de95d2022-10-25 02:54:33 +00001773#endif /* MBEDTLS_SSL_EARLY_DATA */
Ronald Cron6f135e12021-12-08 16:57:54 +01001774#endif /* MBEDTLS_SSL_PROTO_TLS1_3 */
Hanno Becker71f1ed62021-07-24 06:01:47 +01001775
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001776#if defined(MBEDTLS_X509_CRT_PARSE_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01001777void mbedtls_ssl_conf_cert_profile(mbedtls_ssl_config *conf,
1778 const mbedtls_x509_crt_profile *profile)
Manuel Pégourié-Gonnard6e3ee3a2015-06-17 10:58:20 +02001779{
1780 conf->cert_profile = profile;
1781}
1782
Gilles Peskine449bd832023-01-11 14:50:10 +01001783static void ssl_key_cert_free(mbedtls_ssl_key_cert *key_cert)
Glenn Strauss36872db2022-01-22 05:06:31 -05001784{
1785 mbedtls_ssl_key_cert *cur = key_cert, *next;
1786
Gilles Peskine449bd832023-01-11 14:50:10 +01001787 while (cur != NULL) {
Glenn Strauss36872db2022-01-22 05:06:31 -05001788 next = cur->next;
Gilles Peskine449bd832023-01-11 14:50:10 +01001789 mbedtls_free(cur);
Glenn Strauss36872db2022-01-22 05:06:31 -05001790 cur = next;
1791 }
1792}
1793
Manuel Pégourié-Gonnard8f618a82015-05-10 21:13:36 +02001794/* Append a new keycert entry to a (possibly empty) list */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02001795MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01001796static int ssl_append_key_cert(mbedtls_ssl_key_cert **head,
1797 mbedtls_x509_crt *cert,
1798 mbedtls_pk_context *key)
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02001799{
niisato8ee24222018-06-25 19:05:48 +09001800 mbedtls_ssl_key_cert *new_cert;
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02001801
Gilles Peskine449bd832023-01-11 14:50:10 +01001802 if (cert == NULL) {
Glenn Strauss36872db2022-01-22 05:06:31 -05001803 /* Free list if cert is null */
Gilles Peskine449bd832023-01-11 14:50:10 +01001804 ssl_key_cert_free(*head);
Glenn Strauss36872db2022-01-22 05:06:31 -05001805 *head = NULL;
Gilles Peskine449bd832023-01-11 14:50:10 +01001806 return 0;
Glenn Strauss36872db2022-01-22 05:06:31 -05001807 }
1808
Gilles Peskine449bd832023-01-11 14:50:10 +01001809 new_cert = mbedtls_calloc(1, sizeof(mbedtls_ssl_key_cert));
1810 if (new_cert == NULL) {
1811 return MBEDTLS_ERR_SSL_ALLOC_FAILED;
1812 }
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02001813
niisato8ee24222018-06-25 19:05:48 +09001814 new_cert->cert = cert;
1815 new_cert->key = key;
1816 new_cert->next = NULL;
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02001817
Glenn Strauss36872db2022-01-22 05:06:31 -05001818 /* Update head if the list was null, else add to the end */
Gilles Peskine449bd832023-01-11 14:50:10 +01001819 if (*head == NULL) {
niisato8ee24222018-06-25 19:05:48 +09001820 *head = new_cert;
Gilles Peskine449bd832023-01-11 14:50:10 +01001821 } else {
Manuel Pégourié-Gonnard8f618a82015-05-10 21:13:36 +02001822 mbedtls_ssl_key_cert *cur = *head;
Gilles Peskine449bd832023-01-11 14:50:10 +01001823 while (cur->next != NULL) {
Manuel Pégourié-Gonnard8f618a82015-05-10 21:13:36 +02001824 cur = cur->next;
Gilles Peskine449bd832023-01-11 14:50:10 +01001825 }
niisato8ee24222018-06-25 19:05:48 +09001826 cur->next = new_cert;
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02001827 }
1828
Gilles Peskine449bd832023-01-11 14:50:10 +01001829 return 0;
Manuel Pégourié-Gonnard8f618a82015-05-10 21:13:36 +02001830}
1831
Gilles Peskine449bd832023-01-11 14:50:10 +01001832int mbedtls_ssl_conf_own_cert(mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnard8f618a82015-05-10 21:13:36 +02001833 mbedtls_x509_crt *own_cert,
Gilles Peskine449bd832023-01-11 14:50:10 +01001834 mbedtls_pk_context *pk_key)
Manuel Pégourié-Gonnard8f618a82015-05-10 21:13:36 +02001835{
Gilles Peskine449bd832023-01-11 14:50:10 +01001836 return ssl_append_key_cert(&conf->key_cert, own_cert, pk_key);
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02001837}
1838
Gilles Peskine449bd832023-01-11 14:50:10 +01001839void mbedtls_ssl_conf_ca_chain(mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnardbc2b7712015-05-06 11:14:19 +01001840 mbedtls_x509_crt *ca_chain,
Gilles Peskine449bd832023-01-11 14:50:10 +01001841 mbedtls_x509_crl *ca_crl)
Paul Bakker5121ce52009-01-03 21:22:43 +00001842{
Manuel Pégourié-Gonnardbc2b7712015-05-06 11:14:19 +01001843 conf->ca_chain = ca_chain;
1844 conf->ca_crl = ca_crl;
Hanno Becker5adaad92019-03-27 16:54:37 +00001845
1846#if defined(MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK)
1847 /* mbedtls_ssl_conf_ca_chain() and mbedtls_ssl_conf_ca_cb()
1848 * cannot be used together. */
1849 conf->f_ca_cb = NULL;
1850 conf->p_ca_cb = NULL;
1851#endif /* MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK */
Paul Bakker5121ce52009-01-03 21:22:43 +00001852}
Hanno Becker5adaad92019-03-27 16:54:37 +00001853
1854#if defined(MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK)
Gilles Peskine449bd832023-01-11 14:50:10 +01001855void mbedtls_ssl_conf_ca_cb(mbedtls_ssl_config *conf,
1856 mbedtls_x509_crt_ca_cb_t f_ca_cb,
1857 void *p_ca_cb)
Hanno Becker5adaad92019-03-27 16:54:37 +00001858{
1859 conf->f_ca_cb = f_ca_cb;
1860 conf->p_ca_cb = p_ca_cb;
1861
1862 /* mbedtls_ssl_conf_ca_chain() and mbedtls_ssl_conf_ca_cb()
1863 * cannot be used together. */
1864 conf->ca_chain = NULL;
1865 conf->ca_crl = NULL;
1866}
1867#endif /* MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001868#endif /* MBEDTLS_X509_CRT_PARSE_C */
Paul Bakkereb2c6582012-09-27 19:15:01 +00001869
Manuel Pégourié-Gonnard1af6c852015-05-10 23:10:37 +02001870#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
Gilles Peskine449bd832023-01-11 14:50:10 +01001871const unsigned char *mbedtls_ssl_get_hs_sni(mbedtls_ssl_context *ssl,
1872 size_t *name_len)
Glenn Strauss69894072022-01-24 12:58:00 -05001873{
1874 *name_len = ssl->handshake->sni_name_len;
Gilles Peskine449bd832023-01-11 14:50:10 +01001875 return ssl->handshake->sni_name;
Glenn Strauss69894072022-01-24 12:58:00 -05001876}
1877
Gilles Peskine449bd832023-01-11 14:50:10 +01001878int mbedtls_ssl_set_hs_own_cert(mbedtls_ssl_context *ssl,
1879 mbedtls_x509_crt *own_cert,
1880 mbedtls_pk_context *pk_key)
Manuel Pégourié-Gonnard1af6c852015-05-10 23:10:37 +02001881{
Gilles Peskine449bd832023-01-11 14:50:10 +01001882 return ssl_append_key_cert(&ssl->handshake->sni_key_cert,
1883 own_cert, pk_key);
Manuel Pégourié-Gonnard1af6c852015-05-10 23:10:37 +02001884}
Manuel Pégourié-Gonnard22bfa4b2015-05-11 08:46:37 +02001885
Gilles Peskine449bd832023-01-11 14:50:10 +01001886void mbedtls_ssl_set_hs_ca_chain(mbedtls_ssl_context *ssl,
1887 mbedtls_x509_crt *ca_chain,
1888 mbedtls_x509_crl *ca_crl)
Manuel Pégourié-Gonnard22bfa4b2015-05-11 08:46:37 +02001889{
1890 ssl->handshake->sni_ca_chain = ca_chain;
1891 ssl->handshake->sni_ca_crl = ca_crl;
1892}
Manuel Pégourié-Gonnardcdc26ae2015-06-19 12:16:31 +02001893
Glenn Strauss999ef702022-03-11 01:37:23 -05001894#if defined(MBEDTLS_KEY_EXCHANGE_CERT_REQ_ALLOWED_ENABLED)
Gilles Peskine449bd832023-01-11 14:50:10 +01001895void mbedtls_ssl_set_hs_dn_hints(mbedtls_ssl_context *ssl,
1896 const mbedtls_x509_crt *crt)
Glenn Strauss999ef702022-03-11 01:37:23 -05001897{
1898 ssl->handshake->dn_hints = crt;
1899}
1900#endif /* MBEDTLS_KEY_EXCHANGE_CERT_REQ_ALLOWED_ENABLED */
1901
Gilles Peskine449bd832023-01-11 14:50:10 +01001902void mbedtls_ssl_set_hs_authmode(mbedtls_ssl_context *ssl,
1903 int authmode)
Manuel Pégourié-Gonnardcdc26ae2015-06-19 12:16:31 +02001904{
1905 ssl->handshake->sni_authmode = authmode;
1906}
Manuel Pégourié-Gonnard1af6c852015-05-10 23:10:37 +02001907#endif /* MBEDTLS_SSL_SERVER_NAME_INDICATION */
1908
Hanno Becker8927c832019-04-03 12:52:50 +01001909#if defined(MBEDTLS_X509_CRT_PARSE_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01001910void mbedtls_ssl_set_verify(mbedtls_ssl_context *ssl,
1911 int (*f_vrfy)(void *, mbedtls_x509_crt *, int, uint32_t *),
1912 void *p_vrfy)
Hanno Becker8927c832019-04-03 12:52:50 +01001913{
1914 ssl->f_vrfy = f_vrfy;
1915 ssl->p_vrfy = p_vrfy;
1916}
1917#endif
1918
Manuel Pégourié-Gonnardeef142d2015-09-16 10:05:04 +02001919#if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
Manuel Pégourié-Gonnard7002f4a2015-09-15 12:43:43 +02001920
Valerio Setti016f6822022-12-09 14:17:50 +01001921#if defined(MBEDTLS_USE_PSA_CRYPTO)
1922static psa_status_t mbedtls_ssl_set_hs_ecjpake_password_common(
Gilles Peskine449bd832023-01-11 14:50:10 +01001923 mbedtls_ssl_context *ssl,
1924 mbedtls_svc_key_id_t pwd)
Valerio Setti016f6822022-12-09 14:17:50 +01001925{
1926 psa_status_t status;
1927 psa_pake_role_t psa_role;
1928 psa_pake_cipher_suite_t cipher_suite = psa_pake_cipher_suite_init();
1929
Gilles Peskine449bd832023-01-11 14:50:10 +01001930 psa_pake_cs_set_algorithm(&cipher_suite, PSA_ALG_JPAKE);
1931 psa_pake_cs_set_primitive(&cipher_suite,
1932 PSA_PAKE_PRIMITIVE(PSA_PAKE_PRIMITIVE_TYPE_ECC,
1933 PSA_ECC_FAMILY_SECP_R1,
1934 256));
1935 psa_pake_cs_set_hash(&cipher_suite, PSA_ALG_SHA_256);
Valerio Setti016f6822022-12-09 14:17:50 +01001936
Gilles Peskine449bd832023-01-11 14:50:10 +01001937 status = psa_pake_setup(&ssl->handshake->psa_pake_ctx, &cipher_suite);
1938 if (status != PSA_SUCCESS) {
Valerio Setti016f6822022-12-09 14:17:50 +01001939 return status;
Gilles Peskine449bd832023-01-11 14:50:10 +01001940 }
Manuel Pégourié-Gonnard7002f4a2015-09-15 12:43:43 +02001941
Gilles Peskine449bd832023-01-11 14:50:10 +01001942 if (ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER) {
Neil Armstrongca7d5062022-05-31 14:43:23 +02001943 psa_role = PSA_PAKE_ROLE_SERVER;
Gilles Peskine449bd832023-01-11 14:50:10 +01001944 } else {
Neil Armstrongca7d5062022-05-31 14:43:23 +02001945 psa_role = PSA_PAKE_ROLE_CLIENT;
Gilles Peskine449bd832023-01-11 14:50:10 +01001946 }
Neil Armstrongca7d5062022-05-31 14:43:23 +02001947
Gilles Peskine449bd832023-01-11 14:50:10 +01001948 status = psa_pake_set_role(&ssl->handshake->psa_pake_ctx, psa_role);
1949 if (status != PSA_SUCCESS) {
Valerio Setti016f6822022-12-09 14:17:50 +01001950 return status;
Gilles Peskine449bd832023-01-11 14:50:10 +01001951 }
Valerio Setti016f6822022-12-09 14:17:50 +01001952
Gilles Peskine449bd832023-01-11 14:50:10 +01001953 status = psa_pake_set_password_key(&ssl->handshake->psa_pake_ctx, pwd);
1954 if (status != PSA_SUCCESS) {
Valerio Setti016f6822022-12-09 14:17:50 +01001955 return status;
Gilles Peskine449bd832023-01-11 14:50:10 +01001956 }
Valerio Setti016f6822022-12-09 14:17:50 +01001957
1958 ssl->handshake->psa_pake_ctx_is_ok = 1;
1959
Gilles Peskine449bd832023-01-11 14:50:10 +01001960 return PSA_SUCCESS;
Valerio Setti016f6822022-12-09 14:17:50 +01001961}
1962
Gilles Peskine449bd832023-01-11 14:50:10 +01001963int mbedtls_ssl_set_hs_ecjpake_password(mbedtls_ssl_context *ssl,
1964 const unsigned char *pw,
1965 size_t pw_len)
Valerio Setti016f6822022-12-09 14:17:50 +01001966{
1967 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
1968 psa_status_t status;
1969
Gilles Peskine449bd832023-01-11 14:50:10 +01001970 if (ssl->handshake == NULL || ssl->conf == NULL) {
1971 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
Neil Armstrongca7d5062022-05-31 14:43:23 +02001972 }
1973
Gilles Peskine449bd832023-01-11 14:50:10 +01001974 /* Empty password is not valid */
1975 if ((pw == NULL) || (pw_len == 0)) {
1976 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
1977 }
1978
1979 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_DERIVE);
1980 psa_set_key_algorithm(&attributes, PSA_ALG_JPAKE);
1981 psa_set_key_type(&attributes, PSA_KEY_TYPE_PASSWORD);
1982
1983 status = psa_import_key(&attributes, pw, pw_len,
1984 &ssl->handshake->psa_pake_password);
1985 if (status != PSA_SUCCESS) {
1986 return MBEDTLS_ERR_SSL_HW_ACCEL_FAILED;
1987 }
1988
1989 status = mbedtls_ssl_set_hs_ecjpake_password_common(ssl,
1990 ssl->handshake->psa_pake_password);
1991 if (status != PSA_SUCCESS) {
1992 psa_destroy_key(ssl->handshake->psa_pake_password);
1993 psa_pake_abort(&ssl->handshake->psa_pake_ctx);
1994 return MBEDTLS_ERR_SSL_HW_ACCEL_FAILED;
1995 }
1996
1997 return 0;
Valerio Setti02c25b52022-11-15 14:08:42 +01001998}
Valerio Settia9a97dc2022-11-28 18:26:16 +01001999
Gilles Peskine449bd832023-01-11 14:50:10 +01002000int mbedtls_ssl_set_hs_ecjpake_password_opaque(mbedtls_ssl_context *ssl,
2001 mbedtls_svc_key_id_t pwd)
Valerio Settia9a97dc2022-11-28 18:26:16 +01002002{
Valerio Settia9a97dc2022-11-28 18:26:16 +01002003 psa_status_t status;
2004
Gilles Peskine449bd832023-01-11 14:50:10 +01002005 if (ssl->handshake == NULL || ssl->conf == NULL) {
2006 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
Neil Armstrongca7d5062022-05-31 14:43:23 +02002007 }
2008
Gilles Peskine449bd832023-01-11 14:50:10 +01002009 if (mbedtls_svc_key_id_is_null(pwd)) {
2010 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
2011 }
2012
2013 status = mbedtls_ssl_set_hs_ecjpake_password_common(ssl, pwd);
2014 if (status != PSA_SUCCESS) {
2015 psa_pake_abort(&ssl->handshake->psa_pake_ctx);
2016 return MBEDTLS_ERR_SSL_HW_ACCEL_FAILED;
2017 }
2018
2019 return 0;
Valerio Setti02c25b52022-11-15 14:08:42 +01002020}
2021#else /* MBEDTLS_USE_PSA_CRYPTO */
Gilles Peskine449bd832023-01-11 14:50:10 +01002022int mbedtls_ssl_set_hs_ecjpake_password(mbedtls_ssl_context *ssl,
2023 const unsigned char *pw,
2024 size_t pw_len)
Manuel Pégourié-Gonnard7002f4a2015-09-15 12:43:43 +02002025{
2026 mbedtls_ecjpake_role role;
2027
Gilles Peskine449bd832023-01-11 14:50:10 +01002028 if (ssl->handshake == NULL || ssl->conf == NULL) {
2029 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
2030 }
Manuel Pégourié-Gonnard7002f4a2015-09-15 12:43:43 +02002031
Valerio Settic689ed82022-12-07 14:40:38 +01002032 /* Empty password is not valid */
Gilles Peskine449bd832023-01-11 14:50:10 +01002033 if ((pw == NULL) || (pw_len == 0)) {
2034 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
2035 }
Valerio Settic689ed82022-12-07 14:40:38 +01002036
Gilles Peskine449bd832023-01-11 14:50:10 +01002037 if (ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER) {
Manuel Pégourié-Gonnard7002f4a2015-09-15 12:43:43 +02002038 role = MBEDTLS_ECJPAKE_SERVER;
Gilles Peskine449bd832023-01-11 14:50:10 +01002039 } else {
Manuel Pégourié-Gonnard7002f4a2015-09-15 12:43:43 +02002040 role = MBEDTLS_ECJPAKE_CLIENT;
Gilles Peskine449bd832023-01-11 14:50:10 +01002041 }
Manuel Pégourié-Gonnard7002f4a2015-09-15 12:43:43 +02002042
Gilles Peskine449bd832023-01-11 14:50:10 +01002043 return mbedtls_ecjpake_setup(&ssl->handshake->ecjpake_ctx,
2044 role,
2045 MBEDTLS_MD_SHA256,
2046 MBEDTLS_ECP_DP_SECP256R1,
2047 pw, pw_len);
Manuel Pégourié-Gonnard7002f4a2015-09-15 12:43:43 +02002048}
Valerio Setti02c25b52022-11-15 14:08:42 +01002049#endif /* MBEDTLS_USE_PSA_CRYPTO */
Manuel Pégourié-Gonnardeef142d2015-09-16 10:05:04 +02002050#endif /* MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED */
Manuel Pégourié-Gonnard7002f4a2015-09-15 12:43:43 +02002051
Ronald Cron73fe8df2022-10-05 14:31:43 +02002052#if defined(MBEDTLS_SSL_HANDSHAKE_WITH_PSK_ENABLED)
Gilles Peskine449bd832023-01-11 14:50:10 +01002053int mbedtls_ssl_conf_has_static_psk(mbedtls_ssl_config const *conf)
Hanno Becker2ed3dce2021-04-19 21:59:14 +01002054{
Gilles Peskine449bd832023-01-11 14:50:10 +01002055 if (conf->psk_identity == NULL ||
2056 conf->psk_identity_len == 0) {
2057 return 0;
Ronald Crond29e13e2022-10-19 10:33:48 +02002058 }
2059
Hanno Becker2ed3dce2021-04-19 21:59:14 +01002060#if defined(MBEDTLS_USE_PSA_CRYPTO)
Gilles Peskine449bd832023-01-11 14:50:10 +01002061 if (!mbedtls_svc_key_id_is_null(conf->psk_opaque)) {
2062 return 1;
2063 }
Hanno Becker2ed3dce2021-04-19 21:59:14 +01002064#endif /* MBEDTLS_USE_PSA_CRYPTO */
Ronald Crond29e13e2022-10-19 10:33:48 +02002065
Gilles Peskine449bd832023-01-11 14:50:10 +01002066 if (conf->psk != NULL && conf->psk_len != 0) {
2067 return 1;
2068 }
Hanno Becker2ed3dce2021-04-19 21:59:14 +01002069
Gilles Peskine449bd832023-01-11 14:50:10 +01002070 return 0;
Hanno Becker2ed3dce2021-04-19 21:59:14 +01002071}
2072
Gilles Peskine449bd832023-01-11 14:50:10 +01002073static void ssl_conf_remove_psk(mbedtls_ssl_config *conf)
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01002074{
2075 /* Remove reference to existing PSK, if any. */
2076#if defined(MBEDTLS_USE_PSA_CRYPTO)
Gilles Peskine449bd832023-01-11 14:50:10 +01002077 if (!mbedtls_svc_key_id_is_null(conf->psk_opaque)) {
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01002078 /* The maintenance of the PSK key slot is the
2079 * user's responsibility. */
Ronald Croncf56a0a2020-08-04 09:51:30 +02002080 conf->psk_opaque = MBEDTLS_SVC_KEY_ID_INIT;
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01002081 }
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01002082#endif /* MBEDTLS_USE_PSA_CRYPTO */
Gilles Peskine449bd832023-01-11 14:50:10 +01002083 if (conf->psk != NULL) {
2084 mbedtls_platform_zeroize(conf->psk, conf->psk_len);
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01002085
Gilles Peskine449bd832023-01-11 14:50:10 +01002086 mbedtls_free(conf->psk);
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01002087 conf->psk = NULL;
2088 conf->psk_len = 0;
2089 }
2090
2091 /* Remove reference to PSK identity, if any. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002092 if (conf->psk_identity != NULL) {
2093 mbedtls_free(conf->psk_identity);
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01002094 conf->psk_identity = NULL;
2095 conf->psk_identity_len = 0;
2096 }
2097}
2098
Hanno Becker7390c712018-11-15 13:33:04 +00002099/* This function assumes that PSK identity in the SSL config is unset.
2100 * It checks that the provided identity is well-formed and attempts
2101 * to make a copy of it in the SSL config.
2102 * On failure, the PSK identity in the config remains unset. */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02002103MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01002104static int ssl_conf_set_psk_identity(mbedtls_ssl_config *conf,
2105 unsigned char const *psk_identity,
2106 size_t psk_identity_len)
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02002107{
Manuel Pégourié-Gonnardc6b5d832015-08-27 16:37:35 +02002108 /* Identity len will be encoded on two bytes */
Gilles Peskine449bd832023-01-11 14:50:10 +01002109 if (psk_identity == NULL ||
Ronald Cron2a87e9b2022-10-19 10:55:26 +02002110 psk_identity_len == 0 ||
Gilles Peskine449bd832023-01-11 14:50:10 +01002111 (psk_identity_len >> 16) != 0 ||
2112 psk_identity_len > MBEDTLS_SSL_OUT_CONTENT_LEN) {
2113 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
Manuel Pégourié-Gonnardc6b5d832015-08-27 16:37:35 +02002114 }
2115
Gilles Peskine449bd832023-01-11 14:50:10 +01002116 conf->psk_identity = mbedtls_calloc(1, psk_identity_len);
2117 if (conf->psk_identity == NULL) {
2118 return MBEDTLS_ERR_SSL_ALLOC_FAILED;
2119 }
Paul Bakker6db455e2013-09-18 17:29:31 +02002120
Manuel Pégourié-Gonnard120fdbd2015-05-07 17:07:50 +01002121 conf->psk_identity_len = psk_identity_len;
Gilles Peskine449bd832023-01-11 14:50:10 +01002122 memcpy(conf->psk_identity, psk_identity, conf->psk_identity_len);
Paul Bakker5ad403f2013-09-18 21:21:30 +02002123
Gilles Peskine449bd832023-01-11 14:50:10 +01002124 return 0;
Paul Bakker6db455e2013-09-18 17:29:31 +02002125}
2126
Gilles Peskine449bd832023-01-11 14:50:10 +01002127int mbedtls_ssl_conf_psk(mbedtls_ssl_config *conf,
2128 const unsigned char *psk, size_t psk_len,
2129 const unsigned char *psk_identity, size_t psk_identity_len)
Hanno Becker7390c712018-11-15 13:33:04 +00002130{
Janos Follath865b3eb2019-12-16 11:46:15 +00002131 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Hanno Becker2ed3dce2021-04-19 21:59:14 +01002132
2133 /* We currently only support one PSK, raw or opaque. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002134 if (mbedtls_ssl_conf_has_static_psk(conf)) {
2135 return MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
2136 }
Hanno Becker7390c712018-11-15 13:33:04 +00002137
2138 /* Check and set raw PSK */
Gilles Peskine449bd832023-01-11 14:50:10 +01002139 if (psk == NULL) {
2140 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
2141 }
2142 if (psk_len == 0) {
2143 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
2144 }
2145 if (psk_len > MBEDTLS_PSK_MAX_LEN) {
2146 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
2147 }
Piotr Nowicki9926eaf2019-11-20 14:54:36 +01002148
Gilles Peskine449bd832023-01-11 14:50:10 +01002149 if ((conf->psk = mbedtls_calloc(1, psk_len)) == NULL) {
2150 return MBEDTLS_ERR_SSL_ALLOC_FAILED;
2151 }
Hanno Becker7390c712018-11-15 13:33:04 +00002152 conf->psk_len = psk_len;
Gilles Peskine449bd832023-01-11 14:50:10 +01002153 memcpy(conf->psk, psk, conf->psk_len);
Hanno Becker7390c712018-11-15 13:33:04 +00002154
2155 /* Check and set PSK Identity */
Gilles Peskine449bd832023-01-11 14:50:10 +01002156 ret = ssl_conf_set_psk_identity(conf, psk_identity, psk_identity_len);
2157 if (ret != 0) {
2158 ssl_conf_remove_psk(conf);
2159 }
Hanno Becker7390c712018-11-15 13:33:04 +00002160
Gilles Peskine449bd832023-01-11 14:50:10 +01002161 return ret;
Hanno Becker7390c712018-11-15 13:33:04 +00002162}
2163
Gilles Peskine449bd832023-01-11 14:50:10 +01002164static void ssl_remove_psk(mbedtls_ssl_context *ssl)
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01002165{
2166#if defined(MBEDTLS_USE_PSA_CRYPTO)
Gilles Peskine449bd832023-01-11 14:50:10 +01002167 if (!mbedtls_svc_key_id_is_null(ssl->handshake->psk_opaque)) {
Neil Armstrong501c9322022-05-03 09:35:09 +02002168 /* The maintenance of the external PSK key slot is the
2169 * user's responsibility. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002170 if (ssl->handshake->psk_opaque_is_internal) {
2171 psa_destroy_key(ssl->handshake->psk_opaque);
Neil Armstrong501c9322022-05-03 09:35:09 +02002172 ssl->handshake->psk_opaque_is_internal = 0;
2173 }
Ronald Croncf56a0a2020-08-04 09:51:30 +02002174 ssl->handshake->psk_opaque = MBEDTLS_SVC_KEY_ID_INIT;
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01002175 }
Neil Armstronge952a302022-05-03 10:22:14 +02002176#else
Gilles Peskine449bd832023-01-11 14:50:10 +01002177 if (ssl->handshake->psk != NULL) {
2178 mbedtls_platform_zeroize(ssl->handshake->psk,
2179 ssl->handshake->psk_len);
2180 mbedtls_free(ssl->handshake->psk);
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01002181 ssl->handshake->psk_len = 0;
2182 }
Neil Armstronge952a302022-05-03 10:22:14 +02002183#endif /* MBEDTLS_USE_PSA_CRYPTO */
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01002184}
2185
Gilles Peskine449bd832023-01-11 14:50:10 +01002186int mbedtls_ssl_set_hs_psk(mbedtls_ssl_context *ssl,
2187 const unsigned char *psk, size_t psk_len)
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01002188{
Neil Armstrong501c9322022-05-03 09:35:09 +02002189#if defined(MBEDTLS_USE_PSA_CRYPTO)
2190 psa_key_attributes_t key_attributes = psa_key_attributes_init();
Jerry Yu5d01c052022-08-17 10:18:10 +08002191 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Jerry Yu24b8c812022-08-20 19:06:56 +08002192 psa_algorithm_t alg = PSA_ALG_NONE;
Jerry Yu5d01c052022-08-17 10:18:10 +08002193 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Neil Armstrong501c9322022-05-03 09:35:09 +02002194#endif /* MBEDTLS_USE_PSA_CRYPTO */
2195
Gilles Peskine449bd832023-01-11 14:50:10 +01002196 if (psk == NULL || ssl->handshake == NULL) {
2197 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
2198 }
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01002199
Gilles Peskine449bd832023-01-11 14:50:10 +01002200 if (psk_len > MBEDTLS_PSK_MAX_LEN) {
2201 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
2202 }
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01002203
Gilles Peskine449bd832023-01-11 14:50:10 +01002204 ssl_remove_psk(ssl);
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01002205
Neil Armstrong501c9322022-05-03 09:35:09 +02002206#if defined(MBEDTLS_USE_PSA_CRYPTO)
Jerry Yuccc68a42022-07-26 16:39:20 +08002207#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
Gilles Peskine449bd832023-01-11 14:50:10 +01002208 if (ssl->tls_version == MBEDTLS_SSL_VERSION_TLS1_2) {
2209 if (ssl->handshake->ciphersuite_info->mac == MBEDTLS_MD_SHA384) {
2210 alg = PSA_ALG_TLS12_PSK_TO_MS(PSA_ALG_SHA_384);
2211 } else {
2212 alg = PSA_ALG_TLS12_PSK_TO_MS(PSA_ALG_SHA_256);
2213 }
2214 psa_set_key_usage_flags(&key_attributes, PSA_KEY_USAGE_DERIVE);
Jerry Yuccc68a42022-07-26 16:39:20 +08002215 }
2216#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Neil Armstrong501c9322022-05-03 09:35:09 +02002217
Jerry Yu568ec252022-07-22 21:27:34 +08002218#if defined(MBEDTLS_SSL_PROTO_TLS1_3)
Gilles Peskine449bd832023-01-11 14:50:10 +01002219 if (ssl->tls_version == MBEDTLS_SSL_VERSION_TLS1_3) {
2220 alg = PSA_ALG_HKDF_EXTRACT(PSA_ALG_ANY_HASH);
2221 psa_set_key_usage_flags(&key_attributes,
2222 PSA_KEY_USAGE_DERIVE | PSA_KEY_USAGE_EXPORT);
Jerry Yuccc68a42022-07-26 16:39:20 +08002223 }
2224#endif /* MBEDTLS_SSL_PROTO_TLS1_3 */
2225
Gilles Peskine449bd832023-01-11 14:50:10 +01002226 psa_set_key_algorithm(&key_attributes, alg);
2227 psa_set_key_type(&key_attributes, PSA_KEY_TYPE_DERIVE);
Neil Armstrong501c9322022-05-03 09:35:09 +02002228
Gilles Peskine449bd832023-01-11 14:50:10 +01002229 status = psa_import_key(&key_attributes, psk, psk_len, &key);
2230 if (status != PSA_SUCCESS) {
2231 return MBEDTLS_ERR_SSL_HW_ACCEL_FAILED;
2232 }
Neil Armstrong501c9322022-05-03 09:35:09 +02002233
2234 /* Allow calling psa_destroy_key() on psk remove */
2235 ssl->handshake->psk_opaque_is_internal = 1;
Gilles Peskine449bd832023-01-11 14:50:10 +01002236 return mbedtls_ssl_set_hs_psk_opaque(ssl, key);
Neil Armstrong501c9322022-05-03 09:35:09 +02002237#else
Gilles Peskine449bd832023-01-11 14:50:10 +01002238 if ((ssl->handshake->psk = mbedtls_calloc(1, psk_len)) == NULL) {
2239 return MBEDTLS_ERR_SSL_ALLOC_FAILED;
2240 }
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01002241
2242 ssl->handshake->psk_len = psk_len;
Gilles Peskine449bd832023-01-11 14:50:10 +01002243 memcpy(ssl->handshake->psk, psk, ssl->handshake->psk_len);
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01002244
Gilles Peskine449bd832023-01-11 14:50:10 +01002245 return 0;
Neil Armstrong501c9322022-05-03 09:35:09 +02002246#endif /* MBEDTLS_USE_PSA_CRYPTO */
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01002247}
2248
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01002249#if defined(MBEDTLS_USE_PSA_CRYPTO)
Gilles Peskine449bd832023-01-11 14:50:10 +01002250int mbedtls_ssl_conf_psk_opaque(mbedtls_ssl_config *conf,
2251 mbedtls_svc_key_id_t psk,
2252 const unsigned char *psk_identity,
2253 size_t psk_identity_len)
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01002254{
Janos Follath865b3eb2019-12-16 11:46:15 +00002255 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Hanno Becker2ed3dce2021-04-19 21:59:14 +01002256
2257 /* We currently only support one PSK, raw or opaque. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002258 if (mbedtls_ssl_conf_has_static_psk(conf)) {
2259 return MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
2260 }
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01002261
Hanno Becker7390c712018-11-15 13:33:04 +00002262 /* Check and set opaque PSK */
Gilles Peskine449bd832023-01-11 14:50:10 +01002263 if (mbedtls_svc_key_id_is_null(psk)) {
2264 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
2265 }
Ronald Croncf56a0a2020-08-04 09:51:30 +02002266 conf->psk_opaque = psk;
Hanno Becker7390c712018-11-15 13:33:04 +00002267
2268 /* Check and set PSK Identity */
Gilles Peskine449bd832023-01-11 14:50:10 +01002269 ret = ssl_conf_set_psk_identity(conf, psk_identity,
2270 psk_identity_len);
2271 if (ret != 0) {
2272 ssl_conf_remove_psk(conf);
2273 }
Hanno Becker7390c712018-11-15 13:33:04 +00002274
Gilles Peskine449bd832023-01-11 14:50:10 +01002275 return ret;
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01002276}
2277
Gilles Peskine449bd832023-01-11 14:50:10 +01002278int mbedtls_ssl_set_hs_psk_opaque(mbedtls_ssl_context *ssl,
2279 mbedtls_svc_key_id_t psk)
Przemyslaw Stekiel6928a512022-02-03 13:50:35 +01002280{
Gilles Peskine449bd832023-01-11 14:50:10 +01002281 if ((mbedtls_svc_key_id_is_null(psk)) ||
2282 (ssl->handshake == NULL)) {
2283 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
2284 }
Przemyslaw Stekiel6928a512022-02-03 13:50:35 +01002285
Gilles Peskine449bd832023-01-11 14:50:10 +01002286 ssl_remove_psk(ssl);
Przemyslaw Stekiel6928a512022-02-03 13:50:35 +01002287 ssl->handshake->psk_opaque = psk;
Gilles Peskine449bd832023-01-11 14:50:10 +01002288 return 0;
Przemyslaw Stekiel6928a512022-02-03 13:50:35 +01002289}
2290#endif /* MBEDTLS_USE_PSA_CRYPTO */
2291
Jerry Yu8897c072022-08-12 13:56:53 +08002292#if defined(MBEDTLS_SSL_SRV_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01002293void mbedtls_ssl_conf_psk_cb(mbedtls_ssl_config *conf,
2294 int (*f_psk)(void *, mbedtls_ssl_context *, const unsigned char *,
2295 size_t),
2296 void *p_psk)
Przemyslaw Stekiel6928a512022-02-03 13:50:35 +01002297{
2298 conf->f_psk = f_psk;
2299 conf->p_psk = p_psk;
2300}
Jerry Yu8897c072022-08-12 13:56:53 +08002301#endif /* MBEDTLS_SSL_SRV_C */
2302
Ronald Cron73fe8df2022-10-05 14:31:43 +02002303#endif /* MBEDTLS_SSL_HANDSHAKE_WITH_PSK_ENABLED */
Przemyslaw Stekiel6928a512022-02-03 13:50:35 +01002304
2305#if defined(MBEDTLS_USE_PSA_CRYPTO)
Gilles Peskine301711e2022-04-26 16:57:05 +02002306static mbedtls_ssl_mode_t mbedtls_ssl_get_base_mode(
Gilles Peskine449bd832023-01-11 14:50:10 +01002307 psa_algorithm_t alg)
Neil Armstrong8a0f3e82022-03-30 10:57:37 +02002308{
Neil Armstrong8a0f3e82022-03-30 10:57:37 +02002309#if defined(MBEDTLS_SSL_SOME_SUITES_USE_MAC)
Gilles Peskine449bd832023-01-11 14:50:10 +01002310 if (alg == PSA_ALG_CBC_NO_PADDING) {
2311 return MBEDTLS_SSL_MODE_CBC;
2312 }
Neil Armstrong8a0f3e82022-03-30 10:57:37 +02002313#endif /* MBEDTLS_SSL_SOME_SUITES_USE_MAC */
Gilles Peskine449bd832023-01-11 14:50:10 +01002314 if (PSA_ALG_IS_AEAD(alg)) {
2315 return MBEDTLS_SSL_MODE_AEAD;
2316 }
2317 return MBEDTLS_SSL_MODE_STREAM;
Gilles Peskine301711e2022-04-26 16:57:05 +02002318}
2319
2320#else /* MBEDTLS_USE_PSA_CRYPTO */
2321
2322static mbedtls_ssl_mode_t mbedtls_ssl_get_base_mode(
Gilles Peskine449bd832023-01-11 14:50:10 +01002323 mbedtls_cipher_mode_t mode)
Gilles Peskine301711e2022-04-26 16:57:05 +02002324{
2325#if defined(MBEDTLS_SSL_SOME_SUITES_USE_MAC)
Gilles Peskine449bd832023-01-11 14:50:10 +01002326 if (mode == MBEDTLS_MODE_CBC) {
2327 return MBEDTLS_SSL_MODE_CBC;
2328 }
Gilles Peskine301711e2022-04-26 16:57:05 +02002329#endif /* MBEDTLS_SSL_SOME_SUITES_USE_MAC */
2330
Neil Armstrong8a0f3e82022-03-30 10:57:37 +02002331#if defined(MBEDTLS_GCM_C) || \
2332 defined(MBEDTLS_CCM_C) || \
2333 defined(MBEDTLS_CHACHAPOLY_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01002334 if (mode == MBEDTLS_MODE_GCM ||
Neil Armstrong8a0f3e82022-03-30 10:57:37 +02002335 mode == MBEDTLS_MODE_CCM ||
Gilles Peskine449bd832023-01-11 14:50:10 +01002336 mode == MBEDTLS_MODE_CHACHAPOLY) {
2337 return MBEDTLS_SSL_MODE_AEAD;
2338 }
Neil Armstrong8a0f3e82022-03-30 10:57:37 +02002339#endif /* MBEDTLS_GCM_C || MBEDTLS_CCM_C || MBEDTLS_CHACHAPOLY_C */
Neil Armstrong8a0f3e82022-03-30 10:57:37 +02002340
Gilles Peskine449bd832023-01-11 14:50:10 +01002341 return MBEDTLS_SSL_MODE_STREAM;
Neil Armstrong8a0f3e82022-03-30 10:57:37 +02002342}
Gilles Peskine301711e2022-04-26 16:57:05 +02002343#endif /* MBEDTLS_USE_PSA_CRYPTO */
Neil Armstrong8a0f3e82022-03-30 10:57:37 +02002344
Gilles Peskinee108d982022-04-26 16:50:40 +02002345static mbedtls_ssl_mode_t mbedtls_ssl_get_actual_mode(
2346 mbedtls_ssl_mode_t base_mode,
Gilles Peskine449bd832023-01-11 14:50:10 +01002347 int encrypt_then_mac)
Gilles Peskinee108d982022-04-26 16:50:40 +02002348{
2349#if defined(MBEDTLS_SSL_SOME_SUITES_USE_CBC_ETM)
Gilles Peskine449bd832023-01-11 14:50:10 +01002350 if (encrypt_then_mac == MBEDTLS_SSL_ETM_ENABLED &&
2351 base_mode == MBEDTLS_SSL_MODE_CBC) {
2352 return MBEDTLS_SSL_MODE_CBC_ETM;
Gilles Peskinee108d982022-04-26 16:50:40 +02002353 }
2354#else
2355 (void) encrypt_then_mac;
2356#endif
Gilles Peskine449bd832023-01-11 14:50:10 +01002357 return base_mode;
Gilles Peskinee108d982022-04-26 16:50:40 +02002358}
2359
Neil Armstrongab555e02022-04-04 11:07:59 +02002360mbedtls_ssl_mode_t mbedtls_ssl_get_mode_from_transform(
Gilles Peskine449bd832023-01-11 14:50:10 +01002361 const mbedtls_ssl_transform *transform)
Neil Armstrong4bf4c862022-04-01 10:35:48 +02002362{
Gilles Peskinee108d982022-04-26 16:50:40 +02002363 mbedtls_ssl_mode_t base_mode = mbedtls_ssl_get_base_mode(
Neil Armstrong4bf4c862022-04-01 10:35:48 +02002364#if defined(MBEDTLS_USE_PSA_CRYPTO)
Gilles Peskine449bd832023-01-11 14:50:10 +01002365 transform->psa_alg
Neil Armstrong4bf4c862022-04-01 10:35:48 +02002366#else
Gilles Peskine449bd832023-01-11 14:50:10 +01002367 mbedtls_cipher_get_cipher_mode(&transform->cipher_ctx_enc)
Gilles Peskinee108d982022-04-26 16:50:40 +02002368#endif
2369 );
Neil Armstrong4bf4c862022-04-01 10:35:48 +02002370
Gilles Peskinee108d982022-04-26 16:50:40 +02002371 int encrypt_then_mac = 0;
Neil Armstrongf2c82f02022-04-05 11:16:53 +02002372#if defined(MBEDTLS_SSL_SOME_SUITES_USE_CBC_ETM)
Gilles Peskinee108d982022-04-26 16:50:40 +02002373 encrypt_then_mac = transform->encrypt_then_mac;
2374#endif
Gilles Peskine449bd832023-01-11 14:50:10 +01002375 return mbedtls_ssl_get_actual_mode(base_mode, encrypt_then_mac);
Neil Armstrong4bf4c862022-04-01 10:35:48 +02002376}
2377
Neil Armstrongab555e02022-04-04 11:07:59 +02002378mbedtls_ssl_mode_t mbedtls_ssl_get_mode_from_ciphersuite(
Neil Armstrongf2c82f02022-04-05 11:16:53 +02002379#if defined(MBEDTLS_SSL_SOME_SUITES_USE_CBC_ETM)
Gilles Peskine449bd832023-01-11 14:50:10 +01002380 int encrypt_then_mac,
Neil Armstrongf2c82f02022-04-05 11:16:53 +02002381#endif /* MBEDTLS_SSL_SOME_SUITES_USE_CBC_ETM */
Gilles Peskine449bd832023-01-11 14:50:10 +01002382 const mbedtls_ssl_ciphersuite_t *suite)
Neil Armstrong4bf4c862022-04-01 10:35:48 +02002383{
Gilles Peskinee108d982022-04-26 16:50:40 +02002384 mbedtls_ssl_mode_t base_mode = MBEDTLS_SSL_MODE_STREAM;
2385
Neil Armstrong4bf4c862022-04-01 10:35:48 +02002386#if defined(MBEDTLS_USE_PSA_CRYPTO)
2387 psa_status_t status;
2388 psa_algorithm_t alg;
2389 psa_key_type_t type;
2390 size_t size;
Gilles Peskine449bd832023-01-11 14:50:10 +01002391 status = mbedtls_ssl_cipher_to_psa(suite->cipher, 0, &alg, &type, &size);
2392 if (status == PSA_SUCCESS) {
2393 base_mode = mbedtls_ssl_get_base_mode(alg);
2394 }
Neil Armstrong4bf4c862022-04-01 10:35:48 +02002395#else
2396 const mbedtls_cipher_info_t *cipher =
Gilles Peskine449bd832023-01-11 14:50:10 +01002397 mbedtls_cipher_info_from_type(suite->cipher);
2398 if (cipher != NULL) {
Gilles Peskinee108d982022-04-26 16:50:40 +02002399 base_mode =
2400 mbedtls_ssl_get_base_mode(
Gilles Peskine449bd832023-01-11 14:50:10 +01002401 mbedtls_cipher_info_get_mode(cipher));
Gilles Peskinee108d982022-04-26 16:50:40 +02002402 }
Neil Armstrong4bf4c862022-04-01 10:35:48 +02002403#endif /* MBEDTLS_USE_PSA_CRYPTO */
2404
Gilles Peskinee108d982022-04-26 16:50:40 +02002405#if !defined(MBEDTLS_SSL_SOME_SUITES_USE_CBC_ETM)
2406 int encrypt_then_mac = 0;
2407#endif
Gilles Peskine449bd832023-01-11 14:50:10 +01002408 return mbedtls_ssl_get_actual_mode(base_mode, encrypt_then_mac);
Neil Armstrong4bf4c862022-04-01 10:35:48 +02002409}
2410
Neil Armstrong8395d7a2022-05-18 11:44:56 +02002411#if defined(MBEDTLS_USE_PSA_CRYPTO) || defined(MBEDTLS_SSL_PROTO_TLS1_3)
Jerry Yu251a12e2022-07-13 15:15:48 +08002412
2413#if defined(MBEDTLS_SSL_PROTO_TLS1_3)
Jerry Yu438ddd82022-07-07 06:55:50 +00002414/* Serialization of TLS 1.3 sessions:
2415 *
2416 * struct {
Xiaokang Qian126bf8e2022-10-13 02:22:40 +00002417 * opaque hostname<0..2^16-1>;
Jerry Yu438ddd82022-07-07 06:55:50 +00002418 * uint64 ticket_received;
2419 * uint32 ticket_lifetime;
Jerry Yu34191072022-08-18 10:32:09 +08002420 * opaque ticket<1..2^16-1>;
Jerry Yu438ddd82022-07-07 06:55:50 +00002421 * } ClientOnlyData;
2422 *
2423 * struct {
2424 * uint8 endpoint;
2425 * uint8 ciphersuite[2];
2426 * uint32 ticket_age_add;
2427 * uint8 ticket_flags;
2428 * opaque resumption_key<0..255>;
2429 * select ( endpoint ) {
2430 * case client: ClientOnlyData;
2431 * case server: uint64 start_time;
2432 * };
2433 * } serialized_session_tls13;
2434 *
2435 */
2436#if defined(MBEDTLS_SSL_SESSION_TICKETS)
Jerry Yue36fdd62022-08-17 21:31:36 +08002437MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01002438static int ssl_tls13_session_save(const mbedtls_ssl_session *session,
2439 unsigned char *buf,
2440 size_t buf_len,
2441 size_t *olen)
Jerry Yu438ddd82022-07-07 06:55:50 +00002442{
2443 unsigned char *p = buf;
Xiaokang Qiana3b451f2022-10-11 06:20:56 +00002444#if defined(MBEDTLS_SSL_CLI_C) && \
2445 defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
Gilles Peskine449bd832023-01-11 14:50:10 +01002446 size_t hostname_len = (session->hostname == NULL) ?
2447 0 : strlen(session->hostname) + 1;
Xiaokang Qiana3b451f2022-10-11 06:20:56 +00002448#endif
Jerry Yubc7c1a42022-07-21 22:57:37 +08002449 size_t needed = 1 /* endpoint */
2450 + 2 /* ciphersuite */
2451 + 4 /* ticket_age_add */
Jerry Yu34191072022-08-18 10:32:09 +08002452 + 1 /* ticket_flags */
2453 + 1; /* resumption_key length */
Jerry Yue36fdd62022-08-17 21:31:36 +08002454 *olen = 0;
Jerry Yu34191072022-08-18 10:32:09 +08002455
Gilles Peskine449bd832023-01-11 14:50:10 +01002456 if (session->resumption_key_len > MBEDTLS_SSL_TLS1_3_TICKET_RESUMPTION_KEY_LEN) {
2457 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
2458 }
Jerry Yu34191072022-08-18 10:32:09 +08002459 needed += session->resumption_key_len; /* resumption_key */
2460
Jerry Yu438ddd82022-07-07 06:55:50 +00002461#if defined(MBEDTLS_HAVE_TIME)
2462 needed += 8; /* start_time or ticket_received */
2463#endif
2464
2465#if defined(MBEDTLS_SSL_CLI_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01002466 if (session->endpoint == MBEDTLS_SSL_IS_CLIENT) {
Xiaokang Qian126bf8e2022-10-13 02:22:40 +00002467#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
Xiaokang Qianbc663a02022-10-09 11:14:39 +00002468 needed += 2 /* hostname_len */
Gilles Peskine449bd832023-01-11 14:50:10 +01002469 + hostname_len; /* hostname */
Xiaokang Qian281fd1b2022-09-20 11:35:41 +00002470#endif
2471
Jerry Yu438ddd82022-07-07 06:55:50 +00002472 needed += 4 /* ticket_lifetime */
Jerry Yue36fdd62022-08-17 21:31:36 +08002473 + 2; /* ticket_len */
Jerry Yu34191072022-08-18 10:32:09 +08002474
2475 /* Check size_t overflow */
Gilles Peskine449bd832023-01-11 14:50:10 +01002476 if (session->ticket_len > SIZE_MAX - needed) {
2477 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
2478 }
Jerry Yu34191072022-08-18 10:32:09 +08002479
Jerry Yue28d9742022-08-18 15:44:03 +08002480 needed += session->ticket_len; /* ticket */
Jerry Yu438ddd82022-07-07 06:55:50 +00002481 }
2482#endif /* MBEDTLS_SSL_CLI_C */
2483
Jerry Yue36fdd62022-08-17 21:31:36 +08002484 *olen = needed;
Gilles Peskine449bd832023-01-11 14:50:10 +01002485 if (needed > buf_len) {
2486 return MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL;
2487 }
Jerry Yu438ddd82022-07-07 06:55:50 +00002488
2489 p[0] = session->endpoint;
Gilles Peskine449bd832023-01-11 14:50:10 +01002490 MBEDTLS_PUT_UINT16_BE(session->ciphersuite, p, 1);
2491 MBEDTLS_PUT_UINT32_BE(session->ticket_age_add, p, 3);
Jerry Yu438ddd82022-07-07 06:55:50 +00002492 p[7] = session->ticket_flags;
2493
2494 /* save resumption_key */
Jerry Yubc7c1a42022-07-21 22:57:37 +08002495 p[8] = session->resumption_key_len;
Jerry Yu438ddd82022-07-07 06:55:50 +00002496 p += 9;
Gilles Peskine449bd832023-01-11 14:50:10 +01002497 memcpy(p, session->resumption_key, session->resumption_key_len);
Jerry Yubc7c1a42022-07-21 22:57:37 +08002498 p += session->resumption_key_len;
Jerry Yu438ddd82022-07-07 06:55:50 +00002499
2500#if defined(MBEDTLS_HAVE_TIME) && defined(MBEDTLS_SSL_SRV_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01002501 if (session->endpoint == MBEDTLS_SSL_IS_SERVER) {
2502 MBEDTLS_PUT_UINT64_BE((uint64_t) session->start, p, 0);
Jerry Yu438ddd82022-07-07 06:55:50 +00002503 p += 8;
2504 }
2505#endif /* MBEDTLS_HAVE_TIME */
2506
2507#if defined(MBEDTLS_SSL_CLI_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01002508 if (session->endpoint == MBEDTLS_SSL_IS_CLIENT) {
Xiaokang Qianed0620c2022-10-12 06:58:13 +00002509#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
Gilles Peskine449bd832023-01-11 14:50:10 +01002510 MBEDTLS_PUT_UINT16_BE(hostname_len, p, 0);
Xiaokang Qianed0620c2022-10-12 06:58:13 +00002511 p += 2;
Gilles Peskine449bd832023-01-11 14:50:10 +01002512 if (hostname_len > 0) {
Xiaokang Qianed0620c2022-10-12 06:58:13 +00002513 /* save host name */
Gilles Peskine449bd832023-01-11 14:50:10 +01002514 memcpy(p, session->hostname, hostname_len);
Xiaokang Qianed0620c2022-10-12 06:58:13 +00002515 p += hostname_len;
2516 }
2517#endif /* MBEDTLS_SSL_SERVER_NAME_INDICATION */
2518
Jerry Yu438ddd82022-07-07 06:55:50 +00002519#if defined(MBEDTLS_HAVE_TIME)
Gilles Peskine449bd832023-01-11 14:50:10 +01002520 MBEDTLS_PUT_UINT64_BE((uint64_t) session->ticket_received, p, 0);
Jerry Yu438ddd82022-07-07 06:55:50 +00002521 p += 8;
2522#endif
Gilles Peskine449bd832023-01-11 14:50:10 +01002523 MBEDTLS_PUT_UINT32_BE(session->ticket_lifetime, p, 0);
Jerry Yu438ddd82022-07-07 06:55:50 +00002524 p += 4;
2525
Gilles Peskine449bd832023-01-11 14:50:10 +01002526 MBEDTLS_PUT_UINT16_BE(session->ticket_len, p, 0);
Jerry Yu438ddd82022-07-07 06:55:50 +00002527 p += 2;
Jerry Yu34191072022-08-18 10:32:09 +08002528
Gilles Peskine449bd832023-01-11 14:50:10 +01002529 if (session->ticket != NULL && session->ticket_len > 0) {
2530 memcpy(p, session->ticket, session->ticket_len);
Jerry Yu438ddd82022-07-07 06:55:50 +00002531 p += session->ticket_len;
2532 }
2533 }
2534#endif /* MBEDTLS_SSL_CLI_C */
Gilles Peskine449bd832023-01-11 14:50:10 +01002535 return 0;
Jerry Yu438ddd82022-07-07 06:55:50 +00002536}
2537
2538MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01002539static int ssl_tls13_session_load(mbedtls_ssl_session *session,
2540 const unsigned char *buf,
2541 size_t len)
Jerry Yu438ddd82022-07-07 06:55:50 +00002542{
2543 const unsigned char *p = buf;
2544 const unsigned char *end = buf + len;
2545
Gilles Peskine449bd832023-01-11 14:50:10 +01002546 if (end - p < 9) {
2547 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
2548 }
Jerry Yu438ddd82022-07-07 06:55:50 +00002549 session->endpoint = p[0];
Gilles Peskine449bd832023-01-11 14:50:10 +01002550 session->ciphersuite = MBEDTLS_GET_UINT16_BE(p, 1);
2551 session->ticket_age_add = MBEDTLS_GET_UINT32_BE(p, 3);
Jerry Yu438ddd82022-07-07 06:55:50 +00002552 session->ticket_flags = p[7];
2553
2554 /* load resumption_key */
Jerry Yubc7c1a42022-07-21 22:57:37 +08002555 session->resumption_key_len = p[8];
Jerry Yu438ddd82022-07-07 06:55:50 +00002556 p += 9;
2557
Gilles Peskine449bd832023-01-11 14:50:10 +01002558 if (end - p < session->resumption_key_len) {
2559 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
2560 }
Jerry Yu438ddd82022-07-07 06:55:50 +00002561
Gilles Peskine449bd832023-01-11 14:50:10 +01002562 if (sizeof(session->resumption_key) < session->resumption_key_len) {
2563 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
2564 }
2565 memcpy(session->resumption_key, p, session->resumption_key_len);
Jerry Yubc7c1a42022-07-21 22:57:37 +08002566 p += session->resumption_key_len;
Jerry Yu438ddd82022-07-07 06:55:50 +00002567
2568#if defined(MBEDTLS_HAVE_TIME) && defined(MBEDTLS_SSL_SRV_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01002569 if (session->endpoint == MBEDTLS_SSL_IS_SERVER) {
2570 if (end - p < 8) {
2571 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
2572 }
2573 session->start = MBEDTLS_GET_UINT64_BE(p, 0);
Jerry Yu438ddd82022-07-07 06:55:50 +00002574 p += 8;
2575 }
2576#endif /* MBEDTLS_HAVE_TIME */
2577
2578#if defined(MBEDTLS_SSL_CLI_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01002579 if (session->endpoint == MBEDTLS_SSL_IS_CLIENT) {
Xiaokang Qianed0620c2022-10-12 06:58:13 +00002580#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION) && \
Gilles Peskine449bd832023-01-11 14:50:10 +01002581 defined(MBEDTLS_SSL_SESSION_TICKETS)
Xiaokang Qianed0620c2022-10-12 06:58:13 +00002582 size_t hostname_len;
2583 /* load host name */
Gilles Peskine449bd832023-01-11 14:50:10 +01002584 if (end - p < 2) {
2585 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
2586 }
2587 hostname_len = MBEDTLS_GET_UINT16_BE(p, 0);
Xiaokang Qianed0620c2022-10-12 06:58:13 +00002588 p += 2;
2589
Gilles Peskine449bd832023-01-11 14:50:10 +01002590 if (end - p < (long int) hostname_len) {
2591 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
2592 }
2593 if (hostname_len > 0) {
2594 session->hostname = mbedtls_calloc(1, hostname_len);
2595 if (session->hostname == NULL) {
2596 return MBEDTLS_ERR_SSL_ALLOC_FAILED;
2597 }
2598 memcpy(session->hostname, p, hostname_len);
Xiaokang Qianed0620c2022-10-12 06:58:13 +00002599 p += hostname_len;
2600 }
2601#endif /* MBEDTLS_SSL_SERVER_NAME_INDICATION &&
2602 MBEDTLS_SSL_SESSION_TICKETS */
2603
Jerry Yu438ddd82022-07-07 06:55:50 +00002604#if defined(MBEDTLS_HAVE_TIME)
Gilles Peskine449bd832023-01-11 14:50:10 +01002605 if (end - p < 8) {
2606 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
2607 }
2608 session->ticket_received = MBEDTLS_GET_UINT64_BE(p, 0);
Jerry Yu438ddd82022-07-07 06:55:50 +00002609 p += 8;
2610#endif
Gilles Peskine449bd832023-01-11 14:50:10 +01002611 if (end - p < 4) {
2612 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
2613 }
2614 session->ticket_lifetime = MBEDTLS_GET_UINT32_BE(p, 0);
Jerry Yu438ddd82022-07-07 06:55:50 +00002615 p += 4;
2616
Gilles Peskine449bd832023-01-11 14:50:10 +01002617 if (end - p < 2) {
2618 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
2619 }
2620 session->ticket_len = MBEDTLS_GET_UINT16_BE(p, 0);
Jerry Yu438ddd82022-07-07 06:55:50 +00002621 p += 2;
2622
Gilles Peskine449bd832023-01-11 14:50:10 +01002623 if (end - p < (long int) session->ticket_len) {
2624 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
2625 }
2626 if (session->ticket_len > 0) {
2627 session->ticket = mbedtls_calloc(1, session->ticket_len);
2628 if (session->ticket == NULL) {
2629 return MBEDTLS_ERR_SSL_ALLOC_FAILED;
2630 }
2631 memcpy(session->ticket, p, session->ticket_len);
Jerry Yu438ddd82022-07-07 06:55:50 +00002632 p += session->ticket_len;
2633 }
2634 }
2635#endif /* MBEDTLS_SSL_CLI_C */
2636
Gilles Peskine449bd832023-01-11 14:50:10 +01002637 return 0;
Jerry Yu438ddd82022-07-07 06:55:50 +00002638
2639}
2640#else /* MBEDTLS_SSL_SESSION_TICKETS */
Jerry Yue36fdd62022-08-17 21:31:36 +08002641MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01002642static int ssl_tls13_session_save(const mbedtls_ssl_session *session,
2643 unsigned char *buf,
2644 size_t buf_len,
2645 size_t *olen)
Jerry Yu251a12e2022-07-13 15:15:48 +08002646{
2647 ((void) session);
2648 ((void) buf);
2649 ((void) buf_len);
Jerry Yue36fdd62022-08-17 21:31:36 +08002650 *olen = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +01002651 return MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
Jerry Yu251a12e2022-07-13 15:15:48 +08002652}
Jerry Yu438ddd82022-07-07 06:55:50 +00002653
Gilles Peskine449bd832023-01-11 14:50:10 +01002654static int ssl_tls13_session_load(const mbedtls_ssl_session *session,
2655 unsigned char *buf,
2656 size_t buf_len)
Jerry Yu438ddd82022-07-07 06:55:50 +00002657{
2658 ((void) session);
2659 ((void) buf);
2660 ((void) buf_len);
Gilles Peskine449bd832023-01-11 14:50:10 +01002661 return MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
Jerry Yu438ddd82022-07-07 06:55:50 +00002662}
2663#endif /* !MBEDTLS_SSL_SESSION_TICKETS */
Jerry Yu251a12e2022-07-13 15:15:48 +08002664#endif /* MBEDTLS_SSL_PROTO_TLS1_3 */
2665
Gilles Peskine449bd832023-01-11 14:50:10 +01002666psa_status_t mbedtls_ssl_cipher_to_psa(mbedtls_cipher_type_t mbedtls_cipher_type,
2667 size_t taglen,
2668 psa_algorithm_t *alg,
2669 psa_key_type_t *key_type,
2670 size_t *key_size)
Przemyslaw Stekiel430f3372022-01-10 11:55:46 +01002671{
Gilles Peskine449bd832023-01-11 14:50:10 +01002672 switch (mbedtls_cipher_type) {
Przemyslaw Stekiel430f3372022-01-10 11:55:46 +01002673 case MBEDTLS_CIPHER_AES_128_CBC:
2674 *alg = PSA_ALG_CBC_NO_PADDING;
2675 *key_type = PSA_KEY_TYPE_AES;
2676 *key_size = 128;
2677 break;
2678 case MBEDTLS_CIPHER_AES_128_CCM:
Gilles Peskine449bd832023-01-11 14:50:10 +01002679 *alg = taglen ? PSA_ALG_AEAD_WITH_SHORTENED_TAG(PSA_ALG_CCM, taglen) : PSA_ALG_CCM;
Przemyslaw Stekiel430f3372022-01-10 11:55:46 +01002680 *key_type = PSA_KEY_TYPE_AES;
2681 *key_size = 128;
2682 break;
2683 case MBEDTLS_CIPHER_AES_128_GCM:
2684 *alg = PSA_ALG_GCM;
2685 *key_type = PSA_KEY_TYPE_AES;
2686 *key_size = 128;
2687 break;
2688 case MBEDTLS_CIPHER_AES_192_CCM:
Gilles Peskine449bd832023-01-11 14:50:10 +01002689 *alg = taglen ? PSA_ALG_AEAD_WITH_SHORTENED_TAG(PSA_ALG_CCM, taglen) : PSA_ALG_CCM;
Przemyslaw Stekiel430f3372022-01-10 11:55:46 +01002690 *key_type = PSA_KEY_TYPE_AES;
2691 *key_size = 192;
2692 break;
2693 case MBEDTLS_CIPHER_AES_192_GCM:
2694 *alg = PSA_ALG_GCM;
2695 *key_type = PSA_KEY_TYPE_AES;
2696 *key_size = 192;
2697 break;
2698 case MBEDTLS_CIPHER_AES_256_CBC:
2699 *alg = PSA_ALG_CBC_NO_PADDING;
2700 *key_type = PSA_KEY_TYPE_AES;
2701 *key_size = 256;
2702 break;
2703 case MBEDTLS_CIPHER_AES_256_CCM:
Gilles Peskine449bd832023-01-11 14:50:10 +01002704 *alg = taglen ? PSA_ALG_AEAD_WITH_SHORTENED_TAG(PSA_ALG_CCM, taglen) : PSA_ALG_CCM;
Przemyslaw Stekiel430f3372022-01-10 11:55:46 +01002705 *key_type = PSA_KEY_TYPE_AES;
2706 *key_size = 256;
2707 break;
2708 case MBEDTLS_CIPHER_AES_256_GCM:
2709 *alg = PSA_ALG_GCM;
2710 *key_type = PSA_KEY_TYPE_AES;
2711 *key_size = 256;
2712 break;
2713 case MBEDTLS_CIPHER_ARIA_128_CBC:
2714 *alg = PSA_ALG_CBC_NO_PADDING;
2715 *key_type = PSA_KEY_TYPE_ARIA;
2716 *key_size = 128;
2717 break;
2718 case MBEDTLS_CIPHER_ARIA_128_CCM:
Gilles Peskine449bd832023-01-11 14:50:10 +01002719 *alg = taglen ? PSA_ALG_AEAD_WITH_SHORTENED_TAG(PSA_ALG_CCM, taglen) : PSA_ALG_CCM;
Przemyslaw Stekiel430f3372022-01-10 11:55:46 +01002720 *key_type = PSA_KEY_TYPE_ARIA;
2721 *key_size = 128;
2722 break;
2723 case MBEDTLS_CIPHER_ARIA_128_GCM:
2724 *alg = PSA_ALG_GCM;
2725 *key_type = PSA_KEY_TYPE_ARIA;
2726 *key_size = 128;
2727 break;
2728 case MBEDTLS_CIPHER_ARIA_192_CCM:
Gilles Peskine449bd832023-01-11 14:50:10 +01002729 *alg = taglen ? PSA_ALG_AEAD_WITH_SHORTENED_TAG(PSA_ALG_CCM, taglen) : PSA_ALG_CCM;
Przemyslaw Stekiel430f3372022-01-10 11:55:46 +01002730 *key_type = PSA_KEY_TYPE_ARIA;
2731 *key_size = 192;
2732 break;
2733 case MBEDTLS_CIPHER_ARIA_192_GCM:
2734 *alg = PSA_ALG_GCM;
2735 *key_type = PSA_KEY_TYPE_ARIA;
2736 *key_size = 192;
2737 break;
2738 case MBEDTLS_CIPHER_ARIA_256_CBC:
2739 *alg = PSA_ALG_CBC_NO_PADDING;
2740 *key_type = PSA_KEY_TYPE_ARIA;
2741 *key_size = 256;
2742 break;
2743 case MBEDTLS_CIPHER_ARIA_256_CCM:
Gilles Peskine449bd832023-01-11 14:50:10 +01002744 *alg = taglen ? PSA_ALG_AEAD_WITH_SHORTENED_TAG(PSA_ALG_CCM, taglen) : PSA_ALG_CCM;
Przemyslaw Stekiel430f3372022-01-10 11:55:46 +01002745 *key_type = PSA_KEY_TYPE_ARIA;
2746 *key_size = 256;
2747 break;
2748 case MBEDTLS_CIPHER_ARIA_256_GCM:
2749 *alg = PSA_ALG_GCM;
2750 *key_type = PSA_KEY_TYPE_ARIA;
2751 *key_size = 256;
2752 break;
2753 case MBEDTLS_CIPHER_CAMELLIA_128_CBC:
2754 *alg = PSA_ALG_CBC_NO_PADDING;
2755 *key_type = PSA_KEY_TYPE_CAMELLIA;
2756 *key_size = 128;
2757 break;
2758 case MBEDTLS_CIPHER_CAMELLIA_128_CCM:
Gilles Peskine449bd832023-01-11 14:50:10 +01002759 *alg = taglen ? PSA_ALG_AEAD_WITH_SHORTENED_TAG(PSA_ALG_CCM, taglen) : PSA_ALG_CCM;
Przemyslaw Stekiel430f3372022-01-10 11:55:46 +01002760 *key_type = PSA_KEY_TYPE_CAMELLIA;
2761 *key_size = 128;
2762 break;
2763 case MBEDTLS_CIPHER_CAMELLIA_128_GCM:
2764 *alg = PSA_ALG_GCM;
2765 *key_type = PSA_KEY_TYPE_CAMELLIA;
2766 *key_size = 128;
2767 break;
2768 case MBEDTLS_CIPHER_CAMELLIA_192_CCM:
Gilles Peskine449bd832023-01-11 14:50:10 +01002769 *alg = taglen ? PSA_ALG_AEAD_WITH_SHORTENED_TAG(PSA_ALG_CCM, taglen) : PSA_ALG_CCM;
Przemyslaw Stekiel430f3372022-01-10 11:55:46 +01002770 *key_type = PSA_KEY_TYPE_CAMELLIA;
2771 *key_size = 192;
2772 break;
2773 case MBEDTLS_CIPHER_CAMELLIA_192_GCM:
2774 *alg = PSA_ALG_GCM;
2775 *key_type = PSA_KEY_TYPE_CAMELLIA;
2776 *key_size = 192;
2777 break;
2778 case MBEDTLS_CIPHER_CAMELLIA_256_CBC:
2779 *alg = PSA_ALG_CBC_NO_PADDING;
2780 *key_type = PSA_KEY_TYPE_CAMELLIA;
2781 *key_size = 256;
2782 break;
2783 case MBEDTLS_CIPHER_CAMELLIA_256_CCM:
Gilles Peskine449bd832023-01-11 14:50:10 +01002784 *alg = taglen ? PSA_ALG_AEAD_WITH_SHORTENED_TAG(PSA_ALG_CCM, taglen) : PSA_ALG_CCM;
Przemyslaw Stekiel430f3372022-01-10 11:55:46 +01002785 *key_type = PSA_KEY_TYPE_CAMELLIA;
2786 *key_size = 256;
2787 break;
2788 case MBEDTLS_CIPHER_CAMELLIA_256_GCM:
2789 *alg = PSA_ALG_GCM;
2790 *key_type = PSA_KEY_TYPE_CAMELLIA;
2791 *key_size = 256;
2792 break;
2793 case MBEDTLS_CIPHER_CHACHA20_POLY1305:
2794 *alg = PSA_ALG_CHACHA20_POLY1305;
2795 *key_type = PSA_KEY_TYPE_CHACHA20;
2796 *key_size = 256;
2797 break;
2798 case MBEDTLS_CIPHER_NULL:
2799 *alg = MBEDTLS_SSL_NULL_CIPHER;
2800 *key_type = 0;
2801 *key_size = 0;
2802 break;
2803 default:
2804 return PSA_ERROR_NOT_SUPPORTED;
2805 }
2806
2807 return PSA_SUCCESS;
2808}
Neil Armstrong8395d7a2022-05-18 11:44:56 +02002809#endif /* MBEDTLS_USE_PSA_CRYPTO || MBEDTLS_SSL_PROTO_TLS1_3 */
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01002810
Manuel Pégourié-Gonnardcf141ca2015-05-20 10:35:51 +02002811#if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_SSL_SRV_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01002812int mbedtls_ssl_conf_dh_param_bin(mbedtls_ssl_config *conf,
2813 const unsigned char *dhm_P, size_t P_len,
2814 const unsigned char *dhm_G, size_t G_len)
Hanno Beckera90658f2017-10-04 15:29:08 +01002815{
Janos Follath865b3eb2019-12-16 11:46:15 +00002816 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Hanno Beckera90658f2017-10-04 15:29:08 +01002817
Gilles Peskine449bd832023-01-11 14:50:10 +01002818 mbedtls_mpi_free(&conf->dhm_P);
2819 mbedtls_mpi_free(&conf->dhm_G);
Glenn Strausscee11292021-12-20 01:43:17 -05002820
Gilles Peskine449bd832023-01-11 14:50:10 +01002821 if ((ret = mbedtls_mpi_read_binary(&conf->dhm_P, dhm_P, P_len)) != 0 ||
2822 (ret = mbedtls_mpi_read_binary(&conf->dhm_G, dhm_G, G_len)) != 0) {
2823 mbedtls_mpi_free(&conf->dhm_P);
2824 mbedtls_mpi_free(&conf->dhm_G);
2825 return ret;
Hanno Beckera90658f2017-10-04 15:29:08 +01002826 }
2827
Gilles Peskine449bd832023-01-11 14:50:10 +01002828 return 0;
Hanno Beckera90658f2017-10-04 15:29:08 +01002829}
Paul Bakker5121ce52009-01-03 21:22:43 +00002830
Gilles Peskine449bd832023-01-11 14:50:10 +01002831int mbedtls_ssl_conf_dh_param_ctx(mbedtls_ssl_config *conf, mbedtls_dhm_context *dhm_ctx)
Paul Bakker1b57b062011-01-06 15:48:19 +00002832{
Janos Follath865b3eb2019-12-16 11:46:15 +00002833 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker1b57b062011-01-06 15:48:19 +00002834
Gilles Peskine449bd832023-01-11 14:50:10 +01002835 mbedtls_mpi_free(&conf->dhm_P);
2836 mbedtls_mpi_free(&conf->dhm_G);
Glenn Strausscee11292021-12-20 01:43:17 -05002837
Gilles Peskine449bd832023-01-11 14:50:10 +01002838 if ((ret = mbedtls_dhm_get_value(dhm_ctx, MBEDTLS_DHM_PARAM_P,
2839 &conf->dhm_P)) != 0 ||
2840 (ret = mbedtls_dhm_get_value(dhm_ctx, MBEDTLS_DHM_PARAM_G,
2841 &conf->dhm_G)) != 0) {
2842 mbedtls_mpi_free(&conf->dhm_P);
2843 mbedtls_mpi_free(&conf->dhm_G);
2844 return ret;
Manuel Pégourié-Gonnard1028b742015-05-06 17:33:07 +01002845 }
Paul Bakker1b57b062011-01-06 15:48:19 +00002846
Gilles Peskine449bd832023-01-11 14:50:10 +01002847 return 0;
Paul Bakker1b57b062011-01-06 15:48:19 +00002848}
Manuel Pégourié-Gonnardcf141ca2015-05-20 10:35:51 +02002849#endif /* MBEDTLS_DHM_C && MBEDTLS_SSL_SRV_C */
Paul Bakker1b57b062011-01-06 15:48:19 +00002850
Manuel Pégourié-Gonnardbd990d62015-06-11 14:49:42 +02002851#if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_SSL_CLI_C)
2852/*
2853 * Set the minimum length for Diffie-Hellman parameters
2854 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002855void mbedtls_ssl_conf_dhm_min_bitlen(mbedtls_ssl_config *conf,
2856 unsigned int bitlen)
Manuel Pégourié-Gonnardbd990d62015-06-11 14:49:42 +02002857{
2858 conf->dhm_min_bitlen = bitlen;
2859}
2860#endif /* MBEDTLS_DHM_C && MBEDTLS_SSL_CLI_C */
2861
Ronald Crone68ab4f2022-10-05 12:46:29 +02002862#if defined(MBEDTLS_SSL_HANDSHAKE_WITH_CERT_ENABLED)
Jerry Yu7ddc38c2022-01-19 11:08:05 +08002863#if !defined(MBEDTLS_DEPRECATED_REMOVED) && defined(MBEDTLS_SSL_PROTO_TLS1_2)
Manuel Pégourié-Gonnard36a8b572015-06-17 12:43:26 +02002864/*
2865 * Set allowed/preferred hashes for handshake signatures
2866 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002867void mbedtls_ssl_conf_sig_hashes(mbedtls_ssl_config *conf,
2868 const int *hashes)
Manuel Pégourié-Gonnard36a8b572015-06-17 12:43:26 +02002869{
2870 conf->sig_hashes = hashes;
2871}
Jerry Yu7ddc38c2022-01-19 11:08:05 +08002872#endif /* !MBEDTLS_DEPRECATED_REMOVED && MBEDTLS_SSL_PROTO_TLS1_2 */
Hanno Becker1cd6e002021-08-10 13:27:10 +01002873
Jerry Yuf017ee42022-01-12 15:49:48 +08002874/* Configure allowed signature algorithms for handshake */
Gilles Peskine449bd832023-01-11 14:50:10 +01002875void mbedtls_ssl_conf_sig_algs(mbedtls_ssl_config *conf,
2876 const uint16_t *sig_algs)
Hanno Becker1cd6e002021-08-10 13:27:10 +01002877{
Jerry Yuf017ee42022-01-12 15:49:48 +08002878#if !defined(MBEDTLS_DEPRECATED_REMOVED)
2879 conf->sig_hashes = NULL;
2880#endif /* !MBEDTLS_DEPRECATED_REMOVED */
2881 conf->sig_algs = sig_algs;
Hanno Becker1cd6e002021-08-10 13:27:10 +01002882}
Ronald Crone68ab4f2022-10-05 12:46:29 +02002883#endif /* MBEDTLS_SSL_HANDSHAKE_WITH_CERT_ENABLED */
Manuel Pégourié-Gonnard36a8b572015-06-17 12:43:26 +02002884
Manuel Pégourié-Gonnardb541da62015-06-17 11:43:30 +02002885#if defined(MBEDTLS_ECP_C)
Brett Warrene0edc842021-08-17 09:53:13 +01002886#if !defined(MBEDTLS_DEPRECATED_REMOVED)
Manuel Pégourié-Gonnard7f38ed02014-02-04 15:52:33 +01002887/*
2888 * Set the allowed elliptic curves
Brett Warrene0edc842021-08-17 09:53:13 +01002889 *
2890 * mbedtls_ssl_setup() takes the provided list
2891 * and translates it to a list of IANA TLS group identifiers,
2892 * stored in ssl->handshake->group_list.
2893 *
Manuel Pégourié-Gonnard7f38ed02014-02-04 15:52:33 +01002894 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002895void mbedtls_ssl_conf_curves(mbedtls_ssl_config *conf,
2896 const mbedtls_ecp_group_id *curve_list)
Manuel Pégourié-Gonnard7f38ed02014-02-04 15:52:33 +01002897{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02002898 conf->curve_list = curve_list;
Brett Warrene0edc842021-08-17 09:53:13 +01002899 conf->group_list = NULL;
Manuel Pégourié-Gonnard7f38ed02014-02-04 15:52:33 +01002900}
Brett Warrene0edc842021-08-17 09:53:13 +01002901#endif /* MBEDTLS_DEPRECATED_REMOVED */
Hanno Becker947194e2017-04-07 13:25:49 +01002902#endif /* MBEDTLS_ECP_C */
Manuel Pégourié-Gonnard7f38ed02014-02-04 15:52:33 +01002903
Brett Warrene0edc842021-08-17 09:53:13 +01002904/*
2905 * Set the allowed groups
2906 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002907void mbedtls_ssl_conf_groups(mbedtls_ssl_config *conf,
2908 const uint16_t *group_list)
Brett Warrene0edc842021-08-17 09:53:13 +01002909{
2910#if defined(MBEDTLS_ECP_C) && !defined(MBEDTLS_DEPRECATED_REMOVED)
2911 conf->curve_list = NULL;
2912#endif
2913 conf->group_list = group_list;
2914}
2915
Manuel Pégourié-Gonnardbc2b7712015-05-06 11:14:19 +01002916#if defined(MBEDTLS_X509_CRT_PARSE_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01002917int mbedtls_ssl_set_hostname(mbedtls_ssl_context *ssl, const char *hostname)
Paul Bakker5121ce52009-01-03 21:22:43 +00002918{
Hanno Becker947194e2017-04-07 13:25:49 +01002919 /* Initialize to suppress unnecessary compiler warning */
2920 size_t hostname_len = 0;
2921
2922 /* Check if new hostname is valid before
2923 * making any change to current one */
Gilles Peskine449bd832023-01-11 14:50:10 +01002924 if (hostname != NULL) {
2925 hostname_len = strlen(hostname);
Hanno Becker947194e2017-04-07 13:25:49 +01002926
Gilles Peskine449bd832023-01-11 14:50:10 +01002927 if (hostname_len > MBEDTLS_SSL_MAX_HOST_NAME_LEN) {
2928 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
2929 }
Hanno Becker947194e2017-04-07 13:25:49 +01002930 }
2931
2932 /* Now it's clear that we will overwrite the old hostname,
2933 * so we can free it safely */
2934
Gilles Peskine449bd832023-01-11 14:50:10 +01002935 if (ssl->hostname != NULL) {
2936 mbedtls_platform_zeroize(ssl->hostname, strlen(ssl->hostname));
2937 mbedtls_free(ssl->hostname);
Hanno Becker947194e2017-04-07 13:25:49 +01002938 }
2939
2940 /* Passing NULL as hostname shall clear the old one */
Manuel Pégourié-Gonnardba26c242015-05-06 10:47:06 +01002941
Gilles Peskine449bd832023-01-11 14:50:10 +01002942 if (hostname == NULL) {
Hanno Becker947194e2017-04-07 13:25:49 +01002943 ssl->hostname = NULL;
Gilles Peskine449bd832023-01-11 14:50:10 +01002944 } else {
2945 ssl->hostname = mbedtls_calloc(1, hostname_len + 1);
2946 if (ssl->hostname == NULL) {
2947 return MBEDTLS_ERR_SSL_ALLOC_FAILED;
2948 }
Paul Bakker75c1a6f2013-08-19 14:25:29 +02002949
Gilles Peskine449bd832023-01-11 14:50:10 +01002950 memcpy(ssl->hostname, hostname, hostname_len);
Paul Bakker75c1a6f2013-08-19 14:25:29 +02002951
Hanno Becker947194e2017-04-07 13:25:49 +01002952 ssl->hostname[hostname_len] = '\0';
2953 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002954
Gilles Peskine449bd832023-01-11 14:50:10 +01002955 return 0;
Paul Bakker5121ce52009-01-03 21:22:43 +00002956}
Hanno Becker1a9a51c2017-04-07 13:02:16 +01002957#endif /* MBEDTLS_X509_CRT_PARSE_C */
Paul Bakker5121ce52009-01-03 21:22:43 +00002958
Manuel Pégourié-Gonnardbc2b7712015-05-06 11:14:19 +01002959#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
Gilles Peskine449bd832023-01-11 14:50:10 +01002960void mbedtls_ssl_conf_sni(mbedtls_ssl_config *conf,
2961 int (*f_sni)(void *, mbedtls_ssl_context *,
2962 const unsigned char *, size_t),
2963 void *p_sni)
Paul Bakker5701cdc2012-09-27 21:49:42 +00002964{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02002965 conf->f_sni = f_sni;
2966 conf->p_sni = p_sni;
Paul Bakker5701cdc2012-09-27 21:49:42 +00002967}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002968#endif /* MBEDTLS_SSL_SERVER_NAME_INDICATION */
Paul Bakker5701cdc2012-09-27 21:49:42 +00002969
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002970#if defined(MBEDTLS_SSL_ALPN)
Gilles Peskine449bd832023-01-11 14:50:10 +01002971int mbedtls_ssl_conf_alpn_protocols(mbedtls_ssl_config *conf, const char **protos)
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02002972{
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +02002973 size_t cur_len, tot_len;
2974 const char **p;
2975
2976 /*
Brian J Murray1903fb32016-11-06 04:45:15 -08002977 * RFC 7301 3.1: "Empty strings MUST NOT be included and byte strings
2978 * MUST NOT be truncated."
2979 * We check lengths now rather than later.
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +02002980 */
2981 tot_len = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +01002982 for (p = protos; *p != NULL; p++) {
2983 cur_len = strlen(*p);
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +02002984 tot_len += cur_len;
2985
Gilles Peskine449bd832023-01-11 14:50:10 +01002986 if ((cur_len == 0) ||
2987 (cur_len > MBEDTLS_SSL_MAX_ALPN_NAME_LEN) ||
2988 (tot_len > MBEDTLS_SSL_MAX_ALPN_LIST_LEN)) {
2989 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
2990 }
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +02002991 }
2992
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02002993 conf->alpn_list = protos;
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +02002994
Gilles Peskine449bd832023-01-11 14:50:10 +01002995 return 0;
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02002996}
2997
Gilles Peskine449bd832023-01-11 14:50:10 +01002998const char *mbedtls_ssl_get_alpn_protocol(const mbedtls_ssl_context *ssl)
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02002999{
Gilles Peskine449bd832023-01-11 14:50:10 +01003000 return ssl->alpn_chosen;
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02003001}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003002#endif /* MBEDTLS_SSL_ALPN */
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02003003
Johan Pascalb62bb512015-12-03 21:56:45 +01003004#if defined(MBEDTLS_SSL_DTLS_SRTP)
Gilles Peskine449bd832023-01-11 14:50:10 +01003005void mbedtls_ssl_conf_srtp_mki_value_supported(mbedtls_ssl_config *conf,
3006 int support_mki_value)
Ron Eldor591f1622018-01-22 12:30:04 +02003007{
3008 conf->dtls_srtp_mki_support = support_mki_value;
3009}
3010
Gilles Peskine449bd832023-01-11 14:50:10 +01003011int mbedtls_ssl_dtls_srtp_set_mki_value(mbedtls_ssl_context *ssl,
3012 unsigned char *mki_value,
3013 uint16_t mki_len)
Ron Eldor591f1622018-01-22 12:30:04 +02003014{
Gilles Peskine449bd832023-01-11 14:50:10 +01003015 if (mki_len > MBEDTLS_TLS_SRTP_MAX_MKI_LENGTH) {
3016 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
Ron Eldora9788042018-12-05 11:04:31 +02003017 }
Ron Eldor591f1622018-01-22 12:30:04 +02003018
Gilles Peskine449bd832023-01-11 14:50:10 +01003019 if (ssl->conf->dtls_srtp_mki_support == MBEDTLS_SSL_DTLS_SRTP_MKI_UNSUPPORTED) {
3020 return MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
Ron Eldora9788042018-12-05 11:04:31 +02003021 }
Ron Eldor591f1622018-01-22 12:30:04 +02003022
Gilles Peskine449bd832023-01-11 14:50:10 +01003023 memcpy(ssl->dtls_srtp_info.mki_value, mki_value, mki_len);
Ron Eldor591f1622018-01-22 12:30:04 +02003024 ssl->dtls_srtp_info.mki_len = mki_len;
Gilles Peskine449bd832023-01-11 14:50:10 +01003025 return 0;
Ron Eldor591f1622018-01-22 12:30:04 +02003026}
3027
Gilles Peskine449bd832023-01-11 14:50:10 +01003028int mbedtls_ssl_conf_dtls_srtp_protection_profiles(mbedtls_ssl_config *conf,
3029 const mbedtls_ssl_srtp_profile *profiles)
Johan Pascalb62bb512015-12-03 21:56:45 +01003030{
Johan Pascal253d0262020-09-22 13:04:45 +02003031 const mbedtls_ssl_srtp_profile *p;
3032 size_t list_size = 0;
Johan Pascalb62bb512015-12-03 21:56:45 +01003033
Johan Pascal253d0262020-09-22 13:04:45 +02003034 /* check the profiles list: all entry must be valid,
3035 * its size cannot be more than the total number of supported profiles, currently 4 */
Gilles Peskine449bd832023-01-11 14:50:10 +01003036 for (p = profiles; *p != MBEDTLS_TLS_SRTP_UNSET &&
3037 list_size <= MBEDTLS_TLS_SRTP_MAX_PROFILE_LIST_LENGTH;
3038 p++) {
3039 if (mbedtls_ssl_check_srtp_profile_value(*p) != MBEDTLS_TLS_SRTP_UNSET) {
Johan Pascal76fdf1d2020-10-22 23:31:00 +02003040 list_size++;
Gilles Peskine449bd832023-01-11 14:50:10 +01003041 } else {
Johan Pascal76fdf1d2020-10-22 23:31:00 +02003042 /* unsupported value, stop parsing and set the size to an error value */
3043 list_size = MBEDTLS_TLS_SRTP_MAX_PROFILE_LIST_LENGTH + 1;
Johan Pascalb62bb512015-12-03 21:56:45 +01003044 }
3045 }
3046
Gilles Peskine449bd832023-01-11 14:50:10 +01003047 if (list_size > MBEDTLS_TLS_SRTP_MAX_PROFILE_LIST_LENGTH) {
3048 conf->dtls_srtp_profile_list = NULL;
3049 conf->dtls_srtp_profile_list_len = 0;
3050 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
Johan Pascal253d0262020-09-22 13:04:45 +02003051 }
3052
Johan Pascal9bc97ca2020-09-21 23:44:45 +02003053 conf->dtls_srtp_profile_list = profiles;
Johan Pascal253d0262020-09-22 13:04:45 +02003054 conf->dtls_srtp_profile_list_len = list_size;
Johan Pascalb62bb512015-12-03 21:56:45 +01003055
Gilles Peskine449bd832023-01-11 14:50:10 +01003056 return 0;
Johan Pascalb62bb512015-12-03 21:56:45 +01003057}
3058
Gilles Peskine449bd832023-01-11 14:50:10 +01003059void mbedtls_ssl_get_dtls_srtp_negotiation_result(const mbedtls_ssl_context *ssl,
3060 mbedtls_dtls_srtp_info *dtls_srtp_info)
Johan Pascalb62bb512015-12-03 21:56:45 +01003061{
Johan Pascal2258a4f2020-10-28 13:53:09 +01003062 dtls_srtp_info->chosen_dtls_srtp_profile = ssl->dtls_srtp_info.chosen_dtls_srtp_profile;
3063 /* do not copy the mki value if there is no chosen profile */
Gilles Peskine449bd832023-01-11 14:50:10 +01003064 if (dtls_srtp_info->chosen_dtls_srtp_profile == MBEDTLS_TLS_SRTP_UNSET) {
Johan Pascal2258a4f2020-10-28 13:53:09 +01003065 dtls_srtp_info->mki_len = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +01003066 } else {
Johan Pascal2258a4f2020-10-28 13:53:09 +01003067 dtls_srtp_info->mki_len = ssl->dtls_srtp_info.mki_len;
Gilles Peskine449bd832023-01-11 14:50:10 +01003068 memcpy(dtls_srtp_info->mki_value, ssl->dtls_srtp_info.mki_value,
3069 ssl->dtls_srtp_info.mki_len);
Johan Pascal2258a4f2020-10-28 13:53:09 +01003070 }
Johan Pascalb62bb512015-12-03 21:56:45 +01003071}
Johan Pascalb62bb512015-12-03 21:56:45 +01003072#endif /* MBEDTLS_SSL_DTLS_SRTP */
3073
Aditya Patwardhan3096f332022-07-26 14:31:46 +05303074#if !defined(MBEDTLS_DEPRECATED_REMOVED)
Gilles Peskine449bd832023-01-11 14:50:10 +01003075void mbedtls_ssl_conf_max_version(mbedtls_ssl_config *conf, int major, int minor)
Paul Bakker490ecc82011-10-06 13:04:09 +00003076{
Glenn Strauss2dfcea22022-03-14 17:26:42 -04003077 conf->max_tls_version = (major << 8) | minor;
Paul Bakker490ecc82011-10-06 13:04:09 +00003078}
3079
Gilles Peskine449bd832023-01-11 14:50:10 +01003080void mbedtls_ssl_conf_min_version(mbedtls_ssl_config *conf, int major, int minor)
Paul Bakker1d29fb52012-09-28 13:28:45 +00003081{
Glenn Strauss2dfcea22022-03-14 17:26:42 -04003082 conf->min_tls_version = (major << 8) | minor;
Paul Bakker1d29fb52012-09-28 13:28:45 +00003083}
Aditya Patwardhan3096f332022-07-26 14:31:46 +05303084#endif /* MBEDTLS_DEPRECATED_REMOVED */
Paul Bakker1d29fb52012-09-28 13:28:45 +00003085
Janos Follath088ce432017-04-10 12:42:31 +01003086#if defined(MBEDTLS_SSL_SRV_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01003087void mbedtls_ssl_conf_cert_req_ca_list(mbedtls_ssl_config *conf,
3088 char cert_req_ca_list)
Janos Follath088ce432017-04-10 12:42:31 +01003089{
3090 conf->cert_req_ca_list = cert_req_ca_list;
3091}
3092#endif
3093
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003094#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
Gilles Peskine449bd832023-01-11 14:50:10 +01003095void mbedtls_ssl_conf_encrypt_then_mac(mbedtls_ssl_config *conf, char etm)
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +01003096{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02003097 conf->encrypt_then_mac = etm;
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +01003098}
3099#endif
3100
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003101#if defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET)
Gilles Peskine449bd832023-01-11 14:50:10 +01003102void mbedtls_ssl_conf_extended_master_secret(mbedtls_ssl_config *conf, char ems)
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +02003103{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02003104 conf->extended_ms = ems;
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +02003105}
3106#endif
3107
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003108#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
Gilles Peskine449bd832023-01-11 14:50:10 +01003109int mbedtls_ssl_conf_max_frag_len(mbedtls_ssl_config *conf, unsigned char mfl_code)
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02003110{
Gilles Peskine449bd832023-01-11 14:50:10 +01003111 if (mfl_code >= MBEDTLS_SSL_MAX_FRAG_LEN_INVALID ||
3112 ssl_mfl_code_to_length(mfl_code) > MBEDTLS_TLS_EXT_ADV_CONTENT_LEN) {
3113 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02003114 }
3115
Manuel Pégourié-Gonnard6bf89d62015-05-05 17:01:57 +01003116 conf->mfl_code = mfl_code;
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02003117
Gilles Peskine449bd832023-01-11 14:50:10 +01003118 return 0;
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02003119}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003120#endif /* MBEDTLS_SSL_MAX_FRAGMENT_LENGTH */
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02003121
Gilles Peskine449bd832023-01-11 14:50:10 +01003122void mbedtls_ssl_conf_legacy_renegotiation(mbedtls_ssl_config *conf, int allow_legacy)
Paul Bakker48916f92012-09-16 19:57:18 +00003123{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02003124 conf->allow_legacy_renegotiation = allow_legacy;
Paul Bakker48916f92012-09-16 19:57:18 +00003125}
3126
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003127#if defined(MBEDTLS_SSL_RENEGOTIATION)
Gilles Peskine449bd832023-01-11 14:50:10 +01003128void mbedtls_ssl_conf_renegotiation(mbedtls_ssl_config *conf, int renegotiation)
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01003129{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02003130 conf->disable_renegotiation = renegotiation;
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01003131}
3132
Gilles Peskine449bd832023-01-11 14:50:10 +01003133void mbedtls_ssl_conf_renegotiation_enforced(mbedtls_ssl_config *conf, int max_records)
Manuel Pégourié-Gonnarda9964db2014-07-03 19:29:16 +02003134{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02003135 conf->renego_max_records = max_records;
Manuel Pégourié-Gonnarda9964db2014-07-03 19:29:16 +02003136}
3137
Gilles Peskine449bd832023-01-11 14:50:10 +01003138void mbedtls_ssl_conf_renegotiation_period(mbedtls_ssl_config *conf,
3139 const unsigned char period[8])
Manuel Pégourié-Gonnard837f0fe2014-11-05 13:58:53 +01003140{
Gilles Peskine449bd832023-01-11 14:50:10 +01003141 memcpy(conf->renego_period, period, 8);
Manuel Pégourié-Gonnard837f0fe2014-11-05 13:58:53 +01003142}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003143#endif /* MBEDTLS_SSL_RENEGOTIATION */
Paul Bakker5121ce52009-01-03 21:22:43 +00003144
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003145#if defined(MBEDTLS_SSL_SESSION_TICKETS)
Manuel Pégourié-Gonnardb596abf2015-05-20 10:45:29 +02003146#if defined(MBEDTLS_SSL_CLI_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01003147void mbedtls_ssl_conf_session_tickets(mbedtls_ssl_config *conf, int use_tickets)
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +02003148{
Manuel Pégourié-Gonnard2b494452015-05-06 10:05:11 +01003149 conf->session_tickets = use_tickets;
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +02003150}
Manuel Pégourié-Gonnardb596abf2015-05-20 10:45:29 +02003151#endif
Paul Bakker606b4ba2013-08-14 16:52:14 +02003152
Manuel Pégourié-Gonnardb596abf2015-05-20 10:45:29 +02003153#if defined(MBEDTLS_SSL_SRV_C)
Jerry Yu1ad7ace2022-08-09 13:28:39 +08003154
Jerry Yud0766ec2022-09-22 10:46:57 +08003155#if defined(MBEDTLS_SSL_PROTO_TLS1_3) && defined(MBEDTLS_SSL_SESSION_TICKETS)
Gilles Peskine449bd832023-01-11 14:50:10 +01003156void mbedtls_ssl_conf_new_session_tickets(mbedtls_ssl_config *conf,
3157 uint16_t num_tickets)
Jerry Yu1ad7ace2022-08-09 13:28:39 +08003158{
Jerry Yud0766ec2022-09-22 10:46:57 +08003159 conf->new_session_tickets_count = num_tickets;
Jerry Yu1ad7ace2022-08-09 13:28:39 +08003160}
3161#endif
3162
Gilles Peskine449bd832023-01-11 14:50:10 +01003163void mbedtls_ssl_conf_session_tickets_cb(mbedtls_ssl_config *conf,
3164 mbedtls_ssl_ticket_write_t *f_ticket_write,
3165 mbedtls_ssl_ticket_parse_t *f_ticket_parse,
3166 void *p_ticket)
Paul Bakker606b4ba2013-08-14 16:52:14 +02003167{
Manuel Pégourié-Gonnardd59675d2015-05-19 15:28:00 +02003168 conf->f_ticket_write = f_ticket_write;
3169 conf->f_ticket_parse = f_ticket_parse;
3170 conf->p_ticket = p_ticket;
Paul Bakker606b4ba2013-08-14 16:52:14 +02003171}
Manuel Pégourié-Gonnardb596abf2015-05-20 10:45:29 +02003172#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003173#endif /* MBEDTLS_SSL_SESSION_TICKETS */
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +02003174
Gilles Peskine449bd832023-01-11 14:50:10 +01003175void mbedtls_ssl_set_export_keys_cb(mbedtls_ssl_context *ssl,
3176 mbedtls_ssl_export_keys_t *f_export_keys,
3177 void *p_export_keys)
Robert Cragie4feb7ae2015-10-02 13:33:37 +01003178{
Hanno Becker7e6c1782021-06-08 09:24:55 +01003179 ssl->f_export_keys = f_export_keys;
3180 ssl->p_export_keys = p_export_keys;
Ron Eldorf5cc10d2019-05-07 18:33:40 +03003181}
Robert Cragie4feb7ae2015-10-02 13:33:37 +01003182
Gilles Peskineb74a1c72018-04-24 13:09:22 +02003183#if defined(MBEDTLS_SSL_ASYNC_PRIVATE)
Gilles Peskine8bf79f62018-01-05 21:11:53 +01003184void mbedtls_ssl_conf_async_private_cb(
3185 mbedtls_ssl_config *conf,
3186 mbedtls_ssl_async_sign_t *f_async_sign,
3187 mbedtls_ssl_async_decrypt_t *f_async_decrypt,
3188 mbedtls_ssl_async_resume_t *f_async_resume,
3189 mbedtls_ssl_async_cancel_t *f_async_cancel,
Gilles Peskine449bd832023-01-11 14:50:10 +01003190 void *async_config_data)
Gilles Peskine8bf79f62018-01-05 21:11:53 +01003191{
3192 conf->f_async_sign_start = f_async_sign;
3193 conf->f_async_decrypt_start = f_async_decrypt;
3194 conf->f_async_resume = f_async_resume;
3195 conf->f_async_cancel = f_async_cancel;
Gilles Peskinedf13d5c2018-04-25 20:39:48 +02003196 conf->p_async_config_data = async_config_data;
3197}
3198
Gilles Peskine449bd832023-01-11 14:50:10 +01003199void *mbedtls_ssl_conf_get_async_config_data(const mbedtls_ssl_config *conf)
Gilles Peskine8f97af72018-04-26 11:46:10 +02003200{
Gilles Peskine449bd832023-01-11 14:50:10 +01003201 return conf->p_async_config_data;
Gilles Peskine8f97af72018-04-26 11:46:10 +02003202}
3203
Gilles Peskine449bd832023-01-11 14:50:10 +01003204void *mbedtls_ssl_get_async_operation_data(const mbedtls_ssl_context *ssl)
Gilles Peskinedf13d5c2018-04-25 20:39:48 +02003205{
Gilles Peskine449bd832023-01-11 14:50:10 +01003206 if (ssl->handshake == NULL) {
3207 return NULL;
3208 } else {
3209 return ssl->handshake->user_async_ctx;
3210 }
Gilles Peskinedf13d5c2018-04-25 20:39:48 +02003211}
3212
Gilles Peskine449bd832023-01-11 14:50:10 +01003213void mbedtls_ssl_set_async_operation_data(mbedtls_ssl_context *ssl,
3214 void *ctx)
Gilles Peskinedf13d5c2018-04-25 20:39:48 +02003215{
Gilles Peskine449bd832023-01-11 14:50:10 +01003216 if (ssl->handshake != NULL) {
Gilles Peskinedf13d5c2018-04-25 20:39:48 +02003217 ssl->handshake->user_async_ctx = ctx;
Gilles Peskine449bd832023-01-11 14:50:10 +01003218 }
Gilles Peskine8bf79f62018-01-05 21:11:53 +01003219}
Gilles Peskineb74a1c72018-04-24 13:09:22 +02003220#endif /* MBEDTLS_SSL_ASYNC_PRIVATE */
Gilles Peskine8bf79f62018-01-05 21:11:53 +01003221
Paul Bakker5121ce52009-01-03 21:22:43 +00003222/*
3223 * SSL get accessors
3224 */
Gilles Peskine449bd832023-01-11 14:50:10 +01003225uint32_t mbedtls_ssl_get_verify_result(const mbedtls_ssl_context *ssl)
Paul Bakker5121ce52009-01-03 21:22:43 +00003226{
Gilles Peskine449bd832023-01-11 14:50:10 +01003227 if (ssl->session != NULL) {
3228 return ssl->session->verify_result;
3229 }
Manuel Pégourié-Gonnarde89163c2015-01-23 14:30:57 +00003230
Gilles Peskine449bd832023-01-11 14:50:10 +01003231 if (ssl->session_negotiate != NULL) {
3232 return ssl->session_negotiate->verify_result;
3233 }
Manuel Pégourié-Gonnarde89163c2015-01-23 14:30:57 +00003234
Gilles Peskine449bd832023-01-11 14:50:10 +01003235 return 0xFFFFFFFF;
Paul Bakker5121ce52009-01-03 21:22:43 +00003236}
3237
Gilles Peskine449bd832023-01-11 14:50:10 +01003238int mbedtls_ssl_get_ciphersuite_id_from_ssl(const mbedtls_ssl_context *ssl)
Glenn Strauss8f526902022-01-13 00:04:49 -05003239{
Gilles Peskine449bd832023-01-11 14:50:10 +01003240 if (ssl == NULL || ssl->session == NULL) {
3241 return 0;
3242 }
Glenn Strauss8f526902022-01-13 00:04:49 -05003243
Gilles Peskine449bd832023-01-11 14:50:10 +01003244 return ssl->session->ciphersuite;
Glenn Strauss8f526902022-01-13 00:04:49 -05003245}
3246
Gilles Peskine449bd832023-01-11 14:50:10 +01003247const char *mbedtls_ssl_get_ciphersuite(const mbedtls_ssl_context *ssl)
Paul Bakker72f62662011-01-16 21:27:44 +00003248{
Gilles Peskine449bd832023-01-11 14:50:10 +01003249 if (ssl == NULL || ssl->session == NULL) {
3250 return NULL;
3251 }
Paul Bakker926c8e42013-03-06 10:23:34 +01003252
Gilles Peskine449bd832023-01-11 14:50:10 +01003253 return mbedtls_ssl_get_ciphersuite_name(ssl->session->ciphersuite);
Paul Bakker72f62662011-01-16 21:27:44 +00003254}
3255
Gilles Peskine449bd832023-01-11 14:50:10 +01003256const char *mbedtls_ssl_get_version(const mbedtls_ssl_context *ssl)
Paul Bakker43ca69c2011-01-15 17:35:19 +00003257{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003258#if defined(MBEDTLS_SSL_PROTO_DTLS)
Gilles Peskine449bd832023-01-11 14:50:10 +01003259 if (ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM) {
3260 switch (ssl->tls_version) {
Glenn Strauss60bfe602022-03-14 19:04:24 -04003261 case MBEDTLS_SSL_VERSION_TLS1_2:
Gilles Peskine449bd832023-01-11 14:50:10 +01003262 return "DTLSv1.2";
Manuel Pégourié-Gonnardb21ca2a2014-02-10 13:43:33 +01003263 default:
Gilles Peskine449bd832023-01-11 14:50:10 +01003264 return "unknown (DTLS)";
Manuel Pégourié-Gonnardb21ca2a2014-02-10 13:43:33 +01003265 }
3266 }
3267#endif
3268
Gilles Peskine449bd832023-01-11 14:50:10 +01003269 switch (ssl->tls_version) {
Glenn Strauss60bfe602022-03-14 19:04:24 -04003270 case MBEDTLS_SSL_VERSION_TLS1_2:
Gilles Peskine449bd832023-01-11 14:50:10 +01003271 return "TLSv1.2";
Glenn Strauss60bfe602022-03-14 19:04:24 -04003272 case MBEDTLS_SSL_VERSION_TLS1_3:
Gilles Peskine449bd832023-01-11 14:50:10 +01003273 return "TLSv1.3";
Paul Bakker43ca69c2011-01-15 17:35:19 +00003274 default:
Gilles Peskine449bd832023-01-11 14:50:10 +01003275 return "unknown";
Paul Bakker43ca69c2011-01-15 17:35:19 +00003276 }
Paul Bakker43ca69c2011-01-15 17:35:19 +00003277}
3278
Manuel Pégourié-Gonnarda2cda6b2015-08-31 18:30:52 +02003279#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
Gilles Peskine449bd832023-01-11 14:50:10 +01003280size_t mbedtls_ssl_get_input_max_frag_len(const mbedtls_ssl_context *ssl)
Andrzej Kurek90c6e842020-04-03 05:25:29 -04003281{
David Horstmann95d516f2021-05-04 18:36:56 +01003282 size_t max_len = MBEDTLS_SSL_IN_CONTENT_LEN;
Andrzej Kurek90c6e842020-04-03 05:25:29 -04003283 size_t read_mfl;
3284
Jerry Yuddda0502022-12-01 19:43:12 +08003285#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
Andrzej Kurek90c6e842020-04-03 05:25:29 -04003286 /* Use the configured MFL for the client if we're past SERVER_HELLO_DONE */
Gilles Peskine449bd832023-01-11 14:50:10 +01003287 if (ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT &&
3288 ssl->state >= MBEDTLS_SSL_SERVER_HELLO_DONE) {
3289 return ssl_mfl_code_to_length(ssl->conf->mfl_code);
Andrzej Kurek90c6e842020-04-03 05:25:29 -04003290 }
Jerry Yuddda0502022-12-01 19:43:12 +08003291#endif
Andrzej Kurek90c6e842020-04-03 05:25:29 -04003292
3293 /* Check if a smaller max length was negotiated */
Gilles Peskine449bd832023-01-11 14:50:10 +01003294 if (ssl->session_out != NULL) {
3295 read_mfl = ssl_mfl_code_to_length(ssl->session_out->mfl_code);
3296 if (read_mfl < max_len) {
Andrzej Kurek90c6e842020-04-03 05:25:29 -04003297 max_len = read_mfl;
3298 }
3299 }
3300
Jerry Yuddda0502022-12-01 19:43:12 +08003301 /* During a handshake, use the value being negotiated */
Gilles Peskine449bd832023-01-11 14:50:10 +01003302 if (ssl->session_negotiate != NULL) {
3303 read_mfl = ssl_mfl_code_to_length(ssl->session_negotiate->mfl_code);
3304 if (read_mfl < max_len) {
Andrzej Kurek90c6e842020-04-03 05:25:29 -04003305 max_len = read_mfl;
3306 }
3307 }
3308
Gilles Peskine449bd832023-01-11 14:50:10 +01003309 return max_len;
Andrzej Kurek90c6e842020-04-03 05:25:29 -04003310}
3311
Gilles Peskine449bd832023-01-11 14:50:10 +01003312size_t mbedtls_ssl_get_output_max_frag_len(const mbedtls_ssl_context *ssl)
Manuel Pégourié-Gonnarda2cda6b2015-08-31 18:30:52 +02003313{
3314 size_t max_len;
3315
3316 /*
3317 * Assume mfl_code is correct since it was checked when set
3318 */
Gilles Peskine449bd832023-01-11 14:50:10 +01003319 max_len = ssl_mfl_code_to_length(ssl->conf->mfl_code);
Manuel Pégourié-Gonnarda2cda6b2015-08-31 18:30:52 +02003320
Manuel Pégourié-Gonnard2cb17e22017-09-19 13:00:47 +02003321 /* Check if a smaller max length was negotiated */
Gilles Peskine449bd832023-01-11 14:50:10 +01003322 if (ssl->session_out != NULL &&
3323 ssl_mfl_code_to_length(ssl->session_out->mfl_code) < max_len) {
3324 max_len = ssl_mfl_code_to_length(ssl->session_out->mfl_code);
Manuel Pégourié-Gonnarda2cda6b2015-08-31 18:30:52 +02003325 }
3326
Manuel Pégourié-Gonnard2cb17e22017-09-19 13:00:47 +02003327 /* During a handshake, use the value being negotiated */
Gilles Peskine449bd832023-01-11 14:50:10 +01003328 if (ssl->session_negotiate != NULL &&
3329 ssl_mfl_code_to_length(ssl->session_negotiate->mfl_code) < max_len) {
3330 max_len = ssl_mfl_code_to_length(ssl->session_negotiate->mfl_code);
Manuel Pégourié-Gonnard2cb17e22017-09-19 13:00:47 +02003331 }
3332
Gilles Peskine449bd832023-01-11 14:50:10 +01003333 return max_len;
Manuel Pégourié-Gonnarda2cda6b2015-08-31 18:30:52 +02003334}
3335#endif /* MBEDTLS_SSL_MAX_FRAGMENT_LENGTH */
3336
Manuel Pégourié-Gonnardb8eec192018-08-20 09:34:02 +02003337#if defined(MBEDTLS_SSL_PROTO_DTLS)
Gilles Peskine449bd832023-01-11 14:50:10 +01003338size_t mbedtls_ssl_get_current_mtu(const mbedtls_ssl_context *ssl)
Manuel Pégourié-Gonnardb8eec192018-08-20 09:34:02 +02003339{
Andrzej Kurekef43ce62018-10-09 08:24:12 -04003340 /* Return unlimited mtu for client hello messages to avoid fragmentation. */
Gilles Peskine449bd832023-01-11 14:50:10 +01003341 if (ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT &&
3342 (ssl->state == MBEDTLS_SSL_CLIENT_HELLO ||
3343 ssl->state == MBEDTLS_SSL_SERVER_HELLO)) {
3344 return 0;
3345 }
Andrzej Kurekef43ce62018-10-09 08:24:12 -04003346
Gilles Peskine449bd832023-01-11 14:50:10 +01003347 if (ssl->handshake == NULL || ssl->handshake->mtu == 0) {
3348 return ssl->mtu;
3349 }
Manuel Pégourié-Gonnardb8eec192018-08-20 09:34:02 +02003350
Gilles Peskine449bd832023-01-11 14:50:10 +01003351 if (ssl->mtu == 0) {
3352 return ssl->handshake->mtu;
3353 }
Manuel Pégourié-Gonnardb8eec192018-08-20 09:34:02 +02003354
Gilles Peskine449bd832023-01-11 14:50:10 +01003355 return ssl->mtu < ssl->handshake->mtu ?
3356 ssl->mtu : ssl->handshake->mtu;
Manuel Pégourié-Gonnardb8eec192018-08-20 09:34:02 +02003357}
3358#endif /* MBEDTLS_SSL_PROTO_DTLS */
3359
Gilles Peskine449bd832023-01-11 14:50:10 +01003360int mbedtls_ssl_get_max_out_record_payload(const mbedtls_ssl_context *ssl)
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02003361{
3362 size_t max_len = MBEDTLS_SSL_OUT_CONTENT_LEN;
3363
Manuel Pégourié-Gonnard000281e2018-08-21 11:20:58 +02003364#if !defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH) && \
3365 !defined(MBEDTLS_SSL_PROTO_DTLS)
3366 (void) ssl;
3367#endif
3368
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02003369#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
Gilles Peskine449bd832023-01-11 14:50:10 +01003370 const size_t mfl = mbedtls_ssl_get_output_max_frag_len(ssl);
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02003371
Gilles Peskine449bd832023-01-11 14:50:10 +01003372 if (max_len > mfl) {
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02003373 max_len = mfl;
Gilles Peskine449bd832023-01-11 14:50:10 +01003374 }
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02003375#endif
3376
3377#if defined(MBEDTLS_SSL_PROTO_DTLS)
Gilles Peskine449bd832023-01-11 14:50:10 +01003378 if (mbedtls_ssl_get_current_mtu(ssl) != 0) {
3379 const size_t mtu = mbedtls_ssl_get_current_mtu(ssl);
3380 const int ret = mbedtls_ssl_get_record_expansion(ssl);
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02003381 const size_t overhead = (size_t) ret;
3382
Gilles Peskine449bd832023-01-11 14:50:10 +01003383 if (ret < 0) {
3384 return ret;
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02003385 }
3386
Gilles Peskine449bd832023-01-11 14:50:10 +01003387 if (mtu <= overhead) {
3388 MBEDTLS_SSL_DEBUG_MSG(1, ("MTU too low for record expansion"));
3389 return MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
3390 }
3391
3392 if (max_len > mtu - overhead) {
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02003393 max_len = mtu - overhead;
Gilles Peskine449bd832023-01-11 14:50:10 +01003394 }
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02003395 }
Manuel Pégourié-Gonnardb8eec192018-08-20 09:34:02 +02003396#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02003397
Hanno Becker0defedb2018-08-10 12:35:02 +01003398#if !defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH) && \
3399 !defined(MBEDTLS_SSL_PROTO_DTLS)
3400 ((void) ssl);
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02003401#endif
3402
Gilles Peskine449bd832023-01-11 14:50:10 +01003403 return (int) max_len;
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02003404}
3405
Gilles Peskine449bd832023-01-11 14:50:10 +01003406int mbedtls_ssl_get_max_in_record_payload(const mbedtls_ssl_context *ssl)
Hanno Becker2d8e99b2021-04-21 06:19:50 +01003407{
3408 size_t max_len = MBEDTLS_SSL_IN_CONTENT_LEN;
3409
3410#if !defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
3411 (void) ssl;
3412#endif
3413
3414#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
Gilles Peskine449bd832023-01-11 14:50:10 +01003415 const size_t mfl = mbedtls_ssl_get_input_max_frag_len(ssl);
Hanno Becker2d8e99b2021-04-21 06:19:50 +01003416
Gilles Peskine449bd832023-01-11 14:50:10 +01003417 if (max_len > mfl) {
Hanno Becker2d8e99b2021-04-21 06:19:50 +01003418 max_len = mfl;
Gilles Peskine449bd832023-01-11 14:50:10 +01003419 }
Hanno Becker2d8e99b2021-04-21 06:19:50 +01003420#endif
3421
Gilles Peskine449bd832023-01-11 14:50:10 +01003422 return (int) max_len;
Hanno Becker2d8e99b2021-04-21 06:19:50 +01003423}
3424
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003425#if defined(MBEDTLS_X509_CRT_PARSE_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01003426const mbedtls_x509_crt *mbedtls_ssl_get_peer_cert(const mbedtls_ssl_context *ssl)
Paul Bakkerb0550d92012-10-30 07:51:03 +00003427{
Gilles Peskine449bd832023-01-11 14:50:10 +01003428 if (ssl == NULL || ssl->session == NULL) {
3429 return NULL;
3430 }
Paul Bakkerb0550d92012-10-30 07:51:03 +00003431
Hanno Beckere6824572019-02-07 13:18:46 +00003432#if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
Gilles Peskine449bd832023-01-11 14:50:10 +01003433 return ssl->session->peer_cert;
Hanno Beckere6824572019-02-07 13:18:46 +00003434#else
Gilles Peskine449bd832023-01-11 14:50:10 +01003435 return NULL;
Hanno Beckere6824572019-02-07 13:18:46 +00003436#endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
Paul Bakkerb0550d92012-10-30 07:51:03 +00003437}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003438#endif /* MBEDTLS_X509_CRT_PARSE_C */
Paul Bakkerb0550d92012-10-30 07:51:03 +00003439
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003440#if defined(MBEDTLS_SSL_CLI_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01003441int mbedtls_ssl_get_session(const mbedtls_ssl_context *ssl,
3442 mbedtls_ssl_session *dst)
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02003443{
Hanno Beckere810bbc2021-05-14 16:01:05 +01003444 int ret;
3445
Gilles Peskine449bd832023-01-11 14:50:10 +01003446 if (ssl == NULL ||
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02003447 dst == NULL ||
3448 ssl->session == NULL ||
Gilles Peskine449bd832023-01-11 14:50:10 +01003449 ssl->conf->endpoint != MBEDTLS_SSL_IS_CLIENT) {
3450 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02003451 }
3452
Hanno Beckere810bbc2021-05-14 16:01:05 +01003453 /* Since Mbed TLS 3.0, mbedtls_ssl_get_session() is no longer
3454 * idempotent: Each session can only be exported once.
3455 *
3456 * (This is in preparation for TLS 1.3 support where we will
3457 * need the ability to export multiple sessions (aka tickets),
3458 * which will be achieved by calling mbedtls_ssl_get_session()
3459 * multiple times until it fails.)
3460 *
3461 * Check whether we have already exported the current session,
3462 * and fail if so.
3463 */
Gilles Peskine449bd832023-01-11 14:50:10 +01003464 if (ssl->session->exported == 1) {
3465 return MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
3466 }
Hanno Beckere810bbc2021-05-14 16:01:05 +01003467
Gilles Peskine449bd832023-01-11 14:50:10 +01003468 ret = mbedtls_ssl_session_copy(dst, ssl->session);
3469 if (ret != 0) {
3470 return ret;
3471 }
Hanno Beckere810bbc2021-05-14 16:01:05 +01003472
3473 /* Remember that we've exported the session. */
3474 ssl->session->exported = 1;
Gilles Peskine449bd832023-01-11 14:50:10 +01003475 return 0;
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02003476}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003477#endif /* MBEDTLS_SSL_CLI_C */
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02003478
Paul Bakker5121ce52009-01-03 21:22:43 +00003479/*
Hanno Beckera835da52019-05-16 12:39:07 +01003480 * Define ticket header determining Mbed TLS version
3481 * and structure of the ticket.
3482 */
3483
Hanno Becker94ef3b32019-05-16 12:50:45 +01003484/*
Hanno Becker50b59662019-05-28 14:30:45 +01003485 * Define bitflag determining compile-time settings influencing
3486 * structure of serialized SSL sessions.
Hanno Becker94ef3b32019-05-16 12:50:45 +01003487 */
3488
Hanno Becker50b59662019-05-28 14:30:45 +01003489#if defined(MBEDTLS_HAVE_TIME)
Hanno Becker3e088662019-05-29 11:10:18 +01003490#define SSL_SERIALIZED_SESSION_CONFIG_TIME 1
Hanno Becker50b59662019-05-28 14:30:45 +01003491#else
Hanno Becker3e088662019-05-29 11:10:18 +01003492#define SSL_SERIALIZED_SESSION_CONFIG_TIME 0
Hanno Becker94ef3b32019-05-16 12:50:45 +01003493#endif /* MBEDTLS_HAVE_TIME */
3494
3495#if defined(MBEDTLS_X509_CRT_PARSE_C)
Hanno Becker3e088662019-05-29 11:10:18 +01003496#define SSL_SERIALIZED_SESSION_CONFIG_CRT 1
Hanno Becker94ef3b32019-05-16 12:50:45 +01003497#else
Hanno Becker3e088662019-05-29 11:10:18 +01003498#define SSL_SERIALIZED_SESSION_CONFIG_CRT 0
Hanno Becker94ef3b32019-05-16 12:50:45 +01003499#endif /* MBEDTLS_X509_CRT_PARSE_C */
3500
3501#if defined(MBEDTLS_SSL_CLI_C) && defined(MBEDTLS_SSL_SESSION_TICKETS)
Hanno Becker3e088662019-05-29 11:10:18 +01003502#define SSL_SERIALIZED_SESSION_CONFIG_CLIENT_TICKET 1
Hanno Becker94ef3b32019-05-16 12:50:45 +01003503#else
Hanno Becker3e088662019-05-29 11:10:18 +01003504#define SSL_SERIALIZED_SESSION_CONFIG_CLIENT_TICKET 0
Hanno Becker94ef3b32019-05-16 12:50:45 +01003505#endif /* MBEDTLS_SSL_CLI_C && MBEDTLS_SSL_SESSION_TICKETS */
3506
3507#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
Hanno Becker3e088662019-05-29 11:10:18 +01003508#define SSL_SERIALIZED_SESSION_CONFIG_MFL 1
Hanno Becker94ef3b32019-05-16 12:50:45 +01003509#else
Hanno Becker3e088662019-05-29 11:10:18 +01003510#define SSL_SERIALIZED_SESSION_CONFIG_MFL 0
Hanno Becker94ef3b32019-05-16 12:50:45 +01003511#endif /* MBEDTLS_SSL_MAX_FRAGMENT_LENGTH */
3512
Hanno Becker94ef3b32019-05-16 12:50:45 +01003513#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
Hanno Becker3e088662019-05-29 11:10:18 +01003514#define SSL_SERIALIZED_SESSION_CONFIG_ETM 1
Hanno Becker94ef3b32019-05-16 12:50:45 +01003515#else
Hanno Becker3e088662019-05-29 11:10:18 +01003516#define SSL_SERIALIZED_SESSION_CONFIG_ETM 0
Hanno Becker94ef3b32019-05-16 12:50:45 +01003517#endif /* MBEDTLS_SSL_ENCRYPT_THEN_MAC */
3518
Hanno Becker94ef3b32019-05-16 12:50:45 +01003519#if defined(MBEDTLS_SSL_SESSION_TICKETS)
3520#define SSL_SERIALIZED_SESSION_CONFIG_TICKET 1
3521#else
3522#define SSL_SERIALIZED_SESSION_CONFIG_TICKET 0
3523#endif /* MBEDTLS_SSL_SESSION_TICKETS */
3524
Hanno Becker3e088662019-05-29 11:10:18 +01003525#define SSL_SERIALIZED_SESSION_CONFIG_TIME_BIT 0
3526#define SSL_SERIALIZED_SESSION_CONFIG_CRT_BIT 1
3527#define SSL_SERIALIZED_SESSION_CONFIG_CLIENT_TICKET_BIT 2
3528#define SSL_SERIALIZED_SESSION_CONFIG_MFL_BIT 3
Hanno Becker37bdbe62021-08-01 05:38:58 +01003529#define SSL_SERIALIZED_SESSION_CONFIG_ETM_BIT 4
3530#define SSL_SERIALIZED_SESSION_CONFIG_TICKET_BIT 5
Hanno Becker3e088662019-05-29 11:10:18 +01003531
Hanno Becker50b59662019-05-28 14:30:45 +01003532#define SSL_SERIALIZED_SESSION_CONFIG_BITFLAG \
Gilles Peskine449bd832023-01-11 14:50:10 +01003533 ((uint16_t) ( \
3534 (SSL_SERIALIZED_SESSION_CONFIG_TIME << SSL_SERIALIZED_SESSION_CONFIG_TIME_BIT) | \
3535 (SSL_SERIALIZED_SESSION_CONFIG_CRT << SSL_SERIALIZED_SESSION_CONFIG_CRT_BIT) | \
3536 (SSL_SERIALIZED_SESSION_CONFIG_CLIENT_TICKET << \
3537 SSL_SERIALIZED_SESSION_CONFIG_CLIENT_TICKET_BIT) | \
3538 (SSL_SERIALIZED_SESSION_CONFIG_MFL << SSL_SERIALIZED_SESSION_CONFIG_MFL_BIT) | \
3539 (SSL_SERIALIZED_SESSION_CONFIG_ETM << SSL_SERIALIZED_SESSION_CONFIG_ETM_BIT) | \
3540 (SSL_SERIALIZED_SESSION_CONFIG_TICKET << SSL_SERIALIZED_SESSION_CONFIG_TICKET_BIT)))
Hanno Becker94ef3b32019-05-16 12:50:45 +01003541
Hanno Beckerf8787072019-05-16 12:41:07 +01003542static unsigned char ssl_serialized_session_header[] = {
Hanno Becker94ef3b32019-05-16 12:50:45 +01003543 MBEDTLS_VERSION_MAJOR,
3544 MBEDTLS_VERSION_MINOR,
3545 MBEDTLS_VERSION_PATCH,
Gilles Peskine449bd832023-01-11 14:50:10 +01003546 MBEDTLS_BYTE_1(SSL_SERIALIZED_SESSION_CONFIG_BITFLAG),
3547 MBEDTLS_BYTE_0(SSL_SERIALIZED_SESSION_CONFIG_BITFLAG),
Hanno Beckerf8787072019-05-16 12:41:07 +01003548};
Hanno Beckera835da52019-05-16 12:39:07 +01003549
3550/*
Manuel Pégourié-Gonnarda3e7c652019-05-16 10:08:35 +02003551 * Serialize a session in the following format:
Manuel Pégourié-Gonnard35eb8022019-05-16 11:11:08 +02003552 * (in the presentation language of TLS, RFC 8446 section 3)
Manuel Pégourié-Gonnarda3e7c652019-05-16 10:08:35 +02003553 *
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01003554 * struct {
Hanno Beckerdc28b6c2019-05-29 11:08:00 +01003555 *
Hanno Beckerdce50972021-08-01 05:39:23 +01003556 * opaque mbedtls_version[3]; // library version: major, minor, patch
3557 * opaque session_format[2]; // library-version specific 16-bit field
3558 * // determining the format of the remaining
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01003559 * // serialized data.
Hanno Beckerdc28b6c2019-05-29 11:08:00 +01003560 *
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01003561 * Note: When updating the format, remember to keep
3562 * these version+format bytes.
Manuel Pégourié-Gonnarda3e7c652019-05-16 10:08:35 +02003563 *
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01003564 * // In this version, `session_format` determines
3565 * // the setting of those compile-time
3566 * // configuration options which influence
3567 * // the structure of mbedtls_ssl_session.
3568 *
Glenn Straussda7851c2022-03-14 13:29:48 -04003569 * uint8_t minor_ver; // Protocol minor version. Possible values:
Jerry Yu0c2a7382022-12-06 13:27:25 +08003570 * // - TLS 1.2 (0x0303)
3571 * // - TLS 1.3 (0x0304)
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01003572 *
Glenn Straussda7851c2022-03-14 13:29:48 -04003573 * select (serialized_session.tls_version) {
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01003574 *
Glenn Straussda7851c2022-03-14 13:29:48 -04003575 * case MBEDTLS_SSL_VERSION_TLS1_2:
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01003576 * serialized_session_tls12 data;
Jerry Yu0c2a7382022-12-06 13:27:25 +08003577 * case MBEDTLS_SSL_VERSION_TLS1_3:
Jerry Yuddda0502022-12-01 19:43:12 +08003578 * serialized_session_tls13 data;
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01003579 *
3580 * };
3581 *
3582 * } serialized_session;
3583 *
Manuel Pégourié-Gonnarda3e7c652019-05-16 10:08:35 +02003584 */
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01003585
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02003586MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01003587static int ssl_session_save(const mbedtls_ssl_session *session,
3588 unsigned char omit_header,
3589 unsigned char *buf,
3590 size_t buf_len,
3591 size_t *olen)
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01003592{
3593 unsigned char *p = buf;
3594 size_t used = 0;
Jerry Yu251a12e2022-07-13 15:15:48 +08003595 size_t remaining_len;
Jerry Yue36fdd62022-08-17 21:31:36 +08003596#if defined(MBEDTLS_SSL_PROTO_TLS1_3)
3597 size_t out_len;
3598 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
3599#endif
Gilles Peskine449bd832023-01-11 14:50:10 +01003600 if (session == NULL) {
3601 return MBEDTLS_ERR_SSL_INTERNAL_ERROR;
3602 }
Jerry Yu438ddd82022-07-07 06:55:50 +00003603
Gilles Peskine449bd832023-01-11 14:50:10 +01003604 if (!omit_header) {
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01003605 /*
3606 * Add Mbed TLS version identifier
3607 */
Gilles Peskine449bd832023-01-11 14:50:10 +01003608 used += sizeof(ssl_serialized_session_header);
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01003609
Gilles Peskine449bd832023-01-11 14:50:10 +01003610 if (used <= buf_len) {
3611 memcpy(p, ssl_serialized_session_header,
3612 sizeof(ssl_serialized_session_header));
3613 p += sizeof(ssl_serialized_session_header);
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01003614 }
3615 }
3616
3617 /*
3618 * TLS version identifier
3619 */
3620 used += 1;
Gilles Peskine449bd832023-01-11 14:50:10 +01003621 if (used <= buf_len) {
3622 *p++ = MBEDTLS_BYTE_0(session->tls_version);
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01003623 }
3624
3625 /* Forward to version-specific serialization routine. */
Jerry Yufca4d572022-07-21 10:37:48 +08003626 remaining_len = (buf_len >= used) ? buf_len - used : 0;
Gilles Peskine449bd832023-01-11 14:50:10 +01003627 switch (session->tls_version) {
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01003628#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
Gilles Peskine449bd832023-01-11 14:50:10 +01003629 case MBEDTLS_SSL_VERSION_TLS1_2:
3630 used += ssl_tls12_session_save(session, p, remaining_len);
3631 break;
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01003632#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
3633
Jerry Yu251a12e2022-07-13 15:15:48 +08003634#if defined(MBEDTLS_SSL_PROTO_TLS1_3)
Gilles Peskine449bd832023-01-11 14:50:10 +01003635 case MBEDTLS_SSL_VERSION_TLS1_3:
3636 ret = ssl_tls13_session_save(session, p, remaining_len, &out_len);
3637 if (ret != 0 && ret != MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL) {
3638 return ret;
3639 }
3640 used += out_len;
3641 break;
Jerry Yu251a12e2022-07-13 15:15:48 +08003642#endif /* MBEDTLS_SSL_PROTO_TLS1_3 */
3643
Gilles Peskine449bd832023-01-11 14:50:10 +01003644 default:
3645 return MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01003646 }
3647
3648 *olen = used;
Gilles Peskine449bd832023-01-11 14:50:10 +01003649 if (used > buf_len) {
3650 return MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL;
3651 }
Manuel Pégourié-Gonnarda3e7c652019-05-16 10:08:35 +02003652
Gilles Peskine449bd832023-01-11 14:50:10 +01003653 return 0;
Manuel Pégourié-Gonnarda3e7c652019-05-16 10:08:35 +02003654}
3655
3656/*
Manuel Pégourié-Gonnard45ac1f02019-07-23 16:52:45 +02003657 * Public wrapper for ssl_session_save()
3658 */
Gilles Peskine449bd832023-01-11 14:50:10 +01003659int mbedtls_ssl_session_save(const mbedtls_ssl_session *session,
3660 unsigned char *buf,
3661 size_t buf_len,
3662 size_t *olen)
Manuel Pégourié-Gonnard45ac1f02019-07-23 16:52:45 +02003663{
Gilles Peskine449bd832023-01-11 14:50:10 +01003664 return ssl_session_save(session, 0, buf, buf_len, olen);
Manuel Pégourié-Gonnard45ac1f02019-07-23 16:52:45 +02003665}
Manuel Pégourié-Gonnarda3e7c652019-05-16 10:08:35 +02003666
Jerry Yuf1b23ca2022-02-18 11:48:47 +08003667/*
3668 * Deserialize session, see mbedtls_ssl_session_save() for format.
3669 *
3670 * This internal version is wrapped by a public function that cleans up in
3671 * case of error, and has an extra option omit_header.
3672 */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02003673MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01003674static int ssl_session_load(mbedtls_ssl_session *session,
3675 unsigned char omit_header,
3676 const unsigned char *buf,
3677 size_t len)
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01003678{
3679 const unsigned char *p = buf;
3680 const unsigned char * const end = buf + len;
Jerry Yu438ddd82022-07-07 06:55:50 +00003681 size_t remaining_len;
3682
3683
Gilles Peskine449bd832023-01-11 14:50:10 +01003684 if (session == NULL) {
3685 return MBEDTLS_ERR_SSL_INTERNAL_ERROR;
3686 }
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01003687
Gilles Peskine449bd832023-01-11 14:50:10 +01003688 if (!omit_header) {
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01003689 /*
3690 * Check Mbed TLS version identifier
3691 */
3692
Gilles Peskine449bd832023-01-11 14:50:10 +01003693 if ((size_t) (end - p) < sizeof(ssl_serialized_session_header)) {
3694 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01003695 }
Gilles Peskine449bd832023-01-11 14:50:10 +01003696
3697 if (memcmp(p, ssl_serialized_session_header,
3698 sizeof(ssl_serialized_session_header)) != 0) {
3699 return MBEDTLS_ERR_SSL_VERSION_MISMATCH;
3700 }
3701 p += sizeof(ssl_serialized_session_header);
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01003702 }
3703
3704 /*
3705 * TLS version identifier
3706 */
Gilles Peskine449bd832023-01-11 14:50:10 +01003707 if (1 > (size_t) (end - p)) {
3708 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
3709 }
Glenn Straussda7851c2022-03-14 13:29:48 -04003710 session->tls_version = 0x0300 | *p++;
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01003711
3712 /* Dispatch according to TLS version. */
Gilles Peskine449bd832023-01-11 14:50:10 +01003713 remaining_len = (end - p);
3714 switch (session->tls_version) {
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01003715#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
Gilles Peskine449bd832023-01-11 14:50:10 +01003716 case MBEDTLS_SSL_VERSION_TLS1_2:
3717 return ssl_tls12_session_load(session, p, remaining_len);
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01003718#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
3719
Jerry Yu438ddd82022-07-07 06:55:50 +00003720#if defined(MBEDTLS_SSL_PROTO_TLS1_3)
Gilles Peskine449bd832023-01-11 14:50:10 +01003721 case MBEDTLS_SSL_VERSION_TLS1_3:
3722 return ssl_tls13_session_load(session, p, remaining_len);
Jerry Yu438ddd82022-07-07 06:55:50 +00003723#endif /* MBEDTLS_SSL_PROTO_TLS1_3 */
3724
Gilles Peskine449bd832023-01-11 14:50:10 +01003725 default:
3726 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01003727 }
3728}
3729
Manuel Pégourié-Gonnarda3e7c652019-05-16 10:08:35 +02003730/*
Manuel Pégourié-Gonnardb9dfc9f2019-07-12 10:50:19 +02003731 * Deserialize session: public wrapper for error cleaning
Manuel Pégourié-Gonnarda3d831b2019-05-23 12:28:45 +02003732 */
Gilles Peskine449bd832023-01-11 14:50:10 +01003733int mbedtls_ssl_session_load(mbedtls_ssl_session *session,
3734 const unsigned char *buf,
3735 size_t len)
Manuel Pégourié-Gonnarda3d831b2019-05-23 12:28:45 +02003736{
Gilles Peskine449bd832023-01-11 14:50:10 +01003737 int ret = ssl_session_load(session, 0, buf, len);
Manuel Pégourié-Gonnarda3d831b2019-05-23 12:28:45 +02003738
Gilles Peskine449bd832023-01-11 14:50:10 +01003739 if (ret != 0) {
3740 mbedtls_ssl_session_free(session);
3741 }
Manuel Pégourié-Gonnarda3d831b2019-05-23 12:28:45 +02003742
Gilles Peskine449bd832023-01-11 14:50:10 +01003743 return ret;
Manuel Pégourié-Gonnarda3d831b2019-05-23 12:28:45 +02003744}
3745
3746/*
Paul Bakker1961b702013-01-25 14:49:24 +01003747 * Perform a single step of the SSL handshake
Paul Bakker5121ce52009-01-03 21:22:43 +00003748 */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02003749MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01003750static int ssl_prepare_handshake_step(mbedtls_ssl_context *ssl)
Hanno Becker41934dd2021-08-07 19:13:43 +01003751{
3752 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
3753
Ronald Cron66dbf912022-02-02 15:33:46 +01003754 /*
3755 * We may have not been able to send to the peer all the handshake data
Ronald Cron3f20b772022-03-08 16:00:02 +01003756 * that were written into the output buffer by the previous handshake step,
3757 * if the write to the network callback returned with the
Ronald Cron66dbf912022-02-02 15:33:46 +01003758 * #MBEDTLS_ERR_SSL_WANT_WRITE error code.
3759 * We proceed to the next handshake step only when all data from the
3760 * previous one have been sent to the peer, thus we make sure that this is
3761 * the case here by calling `mbedtls_ssl_flush_output()`. The function may
3762 * return with the #MBEDTLS_ERR_SSL_WANT_WRITE error code in which case
3763 * we have to wait before to go ahead.
3764 * In the case of TLS 1.3, handshake step handlers do not send data to the
3765 * peer. Data are only sent here and through
3766 * `mbedtls_ssl_handle_pending_alert` in case an error that triggered an
Andrzej Kurek5c65c572022-04-13 14:28:52 -04003767 * alert occurred.
Ronald Cron66dbf912022-02-02 15:33:46 +01003768 */
Gilles Peskine449bd832023-01-11 14:50:10 +01003769 if ((ret = mbedtls_ssl_flush_output(ssl)) != 0) {
3770 return ret;
3771 }
Hanno Becker41934dd2021-08-07 19:13:43 +01003772
3773#if defined(MBEDTLS_SSL_PROTO_DTLS)
Gilles Peskine449bd832023-01-11 14:50:10 +01003774 if (ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
3775 ssl->handshake->retransmit_state == MBEDTLS_SSL_RETRANS_SENDING) {
3776 if ((ret = mbedtls_ssl_flight_transmit(ssl)) != 0) {
3777 return ret;
3778 }
Hanno Becker41934dd2021-08-07 19:13:43 +01003779 }
3780#endif /* MBEDTLS_SSL_PROTO_DTLS */
3781
Gilles Peskine449bd832023-01-11 14:50:10 +01003782 return ret;
Hanno Becker41934dd2021-08-07 19:13:43 +01003783}
3784
Gilles Peskine449bd832023-01-11 14:50:10 +01003785int mbedtls_ssl_handshake_step(mbedtls_ssl_context *ssl)
Paul Bakker5121ce52009-01-03 21:22:43 +00003786{
Hanno Becker41934dd2021-08-07 19:13:43 +01003787 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker5121ce52009-01-03 21:22:43 +00003788
Gilles Peskine449bd832023-01-11 14:50:10 +01003789 if (ssl == NULL ||
Hanno Becker41934dd2021-08-07 19:13:43 +01003790 ssl->conf == NULL ||
3791 ssl->handshake == NULL ||
Gilles Peskine449bd832023-01-11 14:50:10 +01003792 ssl->state == MBEDTLS_SSL_HANDSHAKE_OVER) {
3793 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
Hanno Becker41934dd2021-08-07 19:13:43 +01003794 }
3795
Gilles Peskine449bd832023-01-11 14:50:10 +01003796 ret = ssl_prepare_handshake_step(ssl);
3797 if (ret != 0) {
3798 return ret;
3799 }
Manuel Pégourié-Gonnardf81ee2e2015-09-01 17:43:40 +02003800
Gilles Peskine449bd832023-01-11 14:50:10 +01003801 ret = mbedtls_ssl_handle_pending_alert(ssl);
3802 if (ret != 0) {
Jerry Yue7047812021-09-13 19:26:39 +08003803 goto cleanup;
Gilles Peskine449bd832023-01-11 14:50:10 +01003804 }
Jerry Yue7047812021-09-13 19:26:39 +08003805
Tom Cosgrove2fdc7b32022-09-21 12:33:17 +01003806 /* If ssl->conf->endpoint is not one of MBEDTLS_SSL_IS_CLIENT or
3807 * MBEDTLS_SSL_IS_SERVER, this is the return code we give */
3808 ret = MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
3809
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003810#if defined(MBEDTLS_SSL_CLI_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01003811 if (ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT) {
3812 MBEDTLS_SSL_DEBUG_MSG(2, ("client state: %s",
3813 mbedtls_ssl_states_str(ssl->state)));
Jerry Yub9930e72021-08-06 17:11:51 +08003814
Gilles Peskine449bd832023-01-11 14:50:10 +01003815 switch (ssl->state) {
Ronald Cron9f0fba32022-02-10 16:45:15 +01003816 case MBEDTLS_SSL_HELLO_REQUEST:
3817 ssl->state = MBEDTLS_SSL_CLIENT_HELLO;
Tom Cosgrove87d9c6c2022-09-22 09:27:56 +01003818 ret = 0;
Ronald Cron9f0fba32022-02-10 16:45:15 +01003819 break;
Jerry Yub9930e72021-08-06 17:11:51 +08003820
Ronald Cron9f0fba32022-02-10 16:45:15 +01003821 case MBEDTLS_SSL_CLIENT_HELLO:
Gilles Peskine449bd832023-01-11 14:50:10 +01003822 ret = mbedtls_ssl_write_client_hello(ssl);
Ronald Cron9f0fba32022-02-10 16:45:15 +01003823 break;
3824
3825 default:
3826#if defined(MBEDTLS_SSL_PROTO_TLS1_2) && defined(MBEDTLS_SSL_PROTO_TLS1_3)
Gilles Peskine449bd832023-01-11 14:50:10 +01003827 if (ssl->tls_version == MBEDTLS_SSL_VERSION_TLS1_3) {
3828 ret = mbedtls_ssl_tls13_handshake_client_step(ssl);
3829 } else {
3830 ret = mbedtls_ssl_handshake_client_step(ssl);
3831 }
Ronald Cron9f0fba32022-02-10 16:45:15 +01003832#elif defined(MBEDTLS_SSL_PROTO_TLS1_2)
Gilles Peskine449bd832023-01-11 14:50:10 +01003833 ret = mbedtls_ssl_handshake_client_step(ssl);
Ronald Cron9f0fba32022-02-10 16:45:15 +01003834#else
Gilles Peskine449bd832023-01-11 14:50:10 +01003835 ret = mbedtls_ssl_tls13_handshake_client_step(ssl);
Ronald Cron9f0fba32022-02-10 16:45:15 +01003836#endif
3837 }
Jerry Yub9930e72021-08-06 17:11:51 +08003838 }
Paul Bakker5121ce52009-01-03 21:22:43 +00003839#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003840#if defined(MBEDTLS_SSL_SRV_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01003841 if (ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER) {
Ronald Cron6f135e12021-12-08 16:57:54 +01003842#if defined(MBEDTLS_SSL_PROTO_TLS1_3)
Gilles Peskine449bd832023-01-11 14:50:10 +01003843 if (mbedtls_ssl_conf_is_tls13_only(ssl->conf)) {
3844 ret = mbedtls_ssl_tls13_handshake_server_step(ssl);
3845 }
Ronald Cron6f135e12021-12-08 16:57:54 +01003846#endif /* MBEDTLS_SSL_PROTO_TLS1_3 */
Jerry Yub9930e72021-08-06 17:11:51 +08003847
3848#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
Gilles Peskine449bd832023-01-11 14:50:10 +01003849 if (mbedtls_ssl_conf_is_tls12_only(ssl->conf)) {
3850 ret = mbedtls_ssl_handshake_server_step(ssl);
3851 }
Jerry Yub9930e72021-08-06 17:11:51 +08003852#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
3853 }
Paul Bakker5121ce52009-01-03 21:22:43 +00003854#endif
3855
Gilles Peskine449bd832023-01-11 14:50:10 +01003856 if (ret != 0) {
Jerry Yubbd5a3f2021-09-18 20:50:22 +08003857 /* handshake_step return error. And it is same
3858 * with alert_reason.
3859 */
Gilles Peskine449bd832023-01-11 14:50:10 +01003860 if (ssl->send_alert) {
3861 ret = mbedtls_ssl_handle_pending_alert(ssl);
Jerry Yue7047812021-09-13 19:26:39 +08003862 goto cleanup;
3863 }
3864 }
3865
3866cleanup:
Gilles Peskine449bd832023-01-11 14:50:10 +01003867 return ret;
Paul Bakker1961b702013-01-25 14:49:24 +01003868}
3869
3870/*
3871 * Perform the SSL handshake
3872 */
Gilles Peskine449bd832023-01-11 14:50:10 +01003873int mbedtls_ssl_handshake(mbedtls_ssl_context *ssl)
Paul Bakker1961b702013-01-25 14:49:24 +01003874{
3875 int ret = 0;
3876
Hanno Beckera817ea42020-10-20 15:20:23 +01003877 /* Sanity checks */
3878
Gilles Peskine449bd832023-01-11 14:50:10 +01003879 if (ssl == NULL || ssl->conf == NULL) {
3880 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
3881 }
Manuel Pégourié-Gonnardf81ee2e2015-09-01 17:43:40 +02003882
Hanno Beckera817ea42020-10-20 15:20:23 +01003883#if defined(MBEDTLS_SSL_PROTO_DTLS)
Gilles Peskine449bd832023-01-11 14:50:10 +01003884 if (ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
3885 (ssl->f_set_timer == NULL || ssl->f_get_timer == NULL)) {
3886 MBEDTLS_SSL_DEBUG_MSG(1, ("You must use "
3887 "mbedtls_ssl_set_timer_cb() for DTLS"));
3888 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
Hanno Beckera817ea42020-10-20 15:20:23 +01003889 }
3890#endif /* MBEDTLS_SSL_PROTO_DTLS */
3891
Gilles Peskine449bd832023-01-11 14:50:10 +01003892 MBEDTLS_SSL_DEBUG_MSG(2, ("=> handshake"));
Paul Bakker1961b702013-01-25 14:49:24 +01003893
Hanno Beckera817ea42020-10-20 15:20:23 +01003894 /* Main handshake loop */
Gilles Peskine449bd832023-01-11 14:50:10 +01003895 while (ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER) {
3896 ret = mbedtls_ssl_handshake_step(ssl);
Paul Bakker1961b702013-01-25 14:49:24 +01003897
Gilles Peskine449bd832023-01-11 14:50:10 +01003898 if (ret != 0) {
Paul Bakker1961b702013-01-25 14:49:24 +01003899 break;
Gilles Peskine449bd832023-01-11 14:50:10 +01003900 }
Paul Bakker1961b702013-01-25 14:49:24 +01003901 }
3902
Gilles Peskine449bd832023-01-11 14:50:10 +01003903 MBEDTLS_SSL_DEBUG_MSG(2, ("<= handshake"));
Paul Bakker5121ce52009-01-03 21:22:43 +00003904
Gilles Peskine449bd832023-01-11 14:50:10 +01003905 return ret;
Paul Bakker5121ce52009-01-03 21:22:43 +00003906}
3907
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003908#if defined(MBEDTLS_SSL_RENEGOTIATION)
3909#if defined(MBEDTLS_SSL_SRV_C)
Paul Bakker5121ce52009-01-03 21:22:43 +00003910/*
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01003911 * Write HelloRequest to request renegotiation on server
Paul Bakker48916f92012-09-16 19:57:18 +00003912 */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02003913MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01003914static int ssl_write_hello_request(mbedtls_ssl_context *ssl)
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01003915{
Janos Follath865b3eb2019-12-16 11:46:15 +00003916 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01003917
Gilles Peskine449bd832023-01-11 14:50:10 +01003918 MBEDTLS_SSL_DEBUG_MSG(2, ("=> write hello request"));
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01003919
3920 ssl->out_msglen = 4;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003921 ssl->out_msgtype = MBEDTLS_SSL_MSG_HANDSHAKE;
3922 ssl->out_msg[0] = MBEDTLS_SSL_HS_HELLO_REQUEST;
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01003923
Gilles Peskine449bd832023-01-11 14:50:10 +01003924 if ((ret = mbedtls_ssl_write_handshake_msg(ssl)) != 0) {
3925 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ssl_write_handshake_msg", ret);
3926 return ret;
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01003927 }
3928
Gilles Peskine449bd832023-01-11 14:50:10 +01003929 MBEDTLS_SSL_DEBUG_MSG(2, ("<= write hello request"));
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01003930
Gilles Peskine449bd832023-01-11 14:50:10 +01003931 return 0;
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01003932}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003933#endif /* MBEDTLS_SSL_SRV_C */
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01003934
3935/*
3936 * Actually renegotiate current connection, triggered by either:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003937 * - any side: calling mbedtls_ssl_renegotiate(),
3938 * - client: receiving a HelloRequest during mbedtls_ssl_read(),
3939 * - server: receiving any handshake message on server during mbedtls_ssl_read() after
Manuel Pégourié-Gonnard55e4ff22014-08-19 11:16:35 +02003940 * the initial handshake is completed.
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01003941 * If the handshake doesn't complete due to waiting for I/O, it will continue
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003942 * during the next calls to mbedtls_ssl_renegotiate() or mbedtls_ssl_read() respectively.
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01003943 */
Gilles Peskine449bd832023-01-11 14:50:10 +01003944int mbedtls_ssl_start_renegotiation(mbedtls_ssl_context *ssl)
Paul Bakker48916f92012-09-16 19:57:18 +00003945{
Janos Follath865b3eb2019-12-16 11:46:15 +00003946 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker48916f92012-09-16 19:57:18 +00003947
Gilles Peskine449bd832023-01-11 14:50:10 +01003948 MBEDTLS_SSL_DEBUG_MSG(2, ("=> renegotiate"));
Paul Bakker48916f92012-09-16 19:57:18 +00003949
Gilles Peskine449bd832023-01-11 14:50:10 +01003950 if ((ret = ssl_handshake_init(ssl)) != 0) {
3951 return ret;
3952 }
Paul Bakker48916f92012-09-16 19:57:18 +00003953
Manuel Pégourié-Gonnard0557bd52014-08-19 19:18:39 +02003954 /* RFC 6347 4.2.2: "[...] the HelloRequest will have message_seq = 0 and
3955 * the ServerHello will have message_seq = 1" */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003956#if defined(MBEDTLS_SSL_PROTO_DTLS)
Gilles Peskine449bd832023-01-11 14:50:10 +01003957 if (ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
3958 ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_PENDING) {
3959 if (ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER) {
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02003960 ssl->handshake->out_msg_seq = 1;
Gilles Peskine449bd832023-01-11 14:50:10 +01003961 } else {
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02003962 ssl->handshake->in_msg_seq = 1;
Gilles Peskine449bd832023-01-11 14:50:10 +01003963 }
Manuel Pégourié-Gonnard0557bd52014-08-19 19:18:39 +02003964 }
3965#endif
3966
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003967 ssl->state = MBEDTLS_SSL_HELLO_REQUEST;
3968 ssl->renego_status = MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS;
Paul Bakker48916f92012-09-16 19:57:18 +00003969
Gilles Peskine449bd832023-01-11 14:50:10 +01003970 if ((ret = mbedtls_ssl_handshake(ssl)) != 0) {
3971 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ssl_handshake", ret);
3972 return ret;
Paul Bakker48916f92012-09-16 19:57:18 +00003973 }
3974
Gilles Peskine449bd832023-01-11 14:50:10 +01003975 MBEDTLS_SSL_DEBUG_MSG(2, ("<= renegotiate"));
Paul Bakker48916f92012-09-16 19:57:18 +00003976
Gilles Peskine449bd832023-01-11 14:50:10 +01003977 return 0;
Paul Bakker48916f92012-09-16 19:57:18 +00003978}
3979
3980/*
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01003981 * Renegotiate current connection on client,
3982 * or request renegotiation on server
3983 */
Gilles Peskine449bd832023-01-11 14:50:10 +01003984int mbedtls_ssl_renegotiate(mbedtls_ssl_context *ssl)
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01003985{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003986 int ret = MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01003987
Gilles Peskine449bd832023-01-11 14:50:10 +01003988 if (ssl == NULL || ssl->conf == NULL) {
3989 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
3990 }
Manuel Pégourié-Gonnardf81ee2e2015-09-01 17:43:40 +02003991
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003992#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01003993 /* On server, just send the request */
Gilles Peskine449bd832023-01-11 14:50:10 +01003994 if (ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER) {
3995 if (mbedtls_ssl_is_handshake_over(ssl) == 0) {
3996 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
3997 }
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01003998
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003999 ssl->renego_status = MBEDTLS_SSL_RENEGOTIATION_PENDING;
Manuel Pégourié-Gonnardf07f4212014-08-15 19:04:47 +02004000
4001 /* Did we already try/start sending HelloRequest? */
Gilles Peskine449bd832023-01-11 14:50:10 +01004002 if (ssl->out_left != 0) {
4003 return mbedtls_ssl_flush_output(ssl);
4004 }
Manuel Pégourié-Gonnardf07f4212014-08-15 19:04:47 +02004005
Gilles Peskine449bd832023-01-11 14:50:10 +01004006 return ssl_write_hello_request(ssl);
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01004007 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004008#endif /* MBEDTLS_SSL_SRV_C */
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01004009
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004010#if defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01004011 /*
4012 * On client, either start the renegotiation process or,
4013 * if already in progress, continue the handshake
4014 */
Gilles Peskine449bd832023-01-11 14:50:10 +01004015 if (ssl->renego_status != MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS) {
4016 if (mbedtls_ssl_is_handshake_over(ssl) == 0) {
4017 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01004018 }
Gilles Peskine449bd832023-01-11 14:50:10 +01004019
4020 if ((ret = mbedtls_ssl_start_renegotiation(ssl)) != 0) {
4021 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ssl_start_renegotiation", ret);
4022 return ret;
4023 }
4024 } else {
4025 if ((ret = mbedtls_ssl_handshake(ssl)) != 0) {
4026 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ssl_handshake", ret);
4027 return ret;
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01004028 }
4029 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004030#endif /* MBEDTLS_SSL_CLI_C */
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01004031
Gilles Peskine449bd832023-01-11 14:50:10 +01004032 return ret;
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01004033}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004034#endif /* MBEDTLS_SSL_RENEGOTIATION */
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01004035
Gilles Peskine449bd832023-01-11 14:50:10 +01004036void mbedtls_ssl_handshake_free(mbedtls_ssl_context *ssl)
Paul Bakker48916f92012-09-16 19:57:18 +00004037{
Gilles Peskine9b562d52018-04-25 20:32:43 +02004038 mbedtls_ssl_handshake_params *handshake = ssl->handshake;
4039
Gilles Peskine449bd832023-01-11 14:50:10 +01004040 if (handshake == NULL) {
Paul Bakkeraccaffe2014-06-26 13:37:14 +02004041 return;
Gilles Peskine449bd832023-01-11 14:50:10 +01004042 }
Paul Bakkeraccaffe2014-06-26 13:37:14 +02004043
Brett Warrene0edc842021-08-17 09:53:13 +01004044#if defined(MBEDTLS_ECP_C)
4045#if !defined(MBEDTLS_DEPRECATED_REMOVED)
Gilles Peskine449bd832023-01-11 14:50:10 +01004046 if (ssl->handshake->group_list_heap_allocated) {
4047 mbedtls_free((void *) handshake->group_list);
4048 }
Brett Warrene0edc842021-08-17 09:53:13 +01004049 handshake->group_list = NULL;
4050#endif /* MBEDTLS_DEPRECATED_REMOVED */
4051#endif /* MBEDTLS_ECP_C */
4052
Ronald Crone68ab4f2022-10-05 12:46:29 +02004053#if defined(MBEDTLS_SSL_HANDSHAKE_WITH_CERT_ENABLED)
Jerry Yuf017ee42022-01-12 15:49:48 +08004054#if !defined(MBEDTLS_DEPRECATED_REMOVED)
Gilles Peskine449bd832023-01-11 14:50:10 +01004055 if (ssl->handshake->sig_algs_heap_allocated) {
4056 mbedtls_free((void *) handshake->sig_algs);
4057 }
Jerry Yuf017ee42022-01-12 15:49:48 +08004058 handshake->sig_algs = NULL;
4059#endif /* MBEDTLS_DEPRECATED_REMOVED */
Xiaofei Baic234ecf2022-02-08 09:59:23 +00004060#if defined(MBEDTLS_SSL_PROTO_TLS1_3)
Gilles Peskine449bd832023-01-11 14:50:10 +01004061 if (ssl->handshake->certificate_request_context) {
4062 mbedtls_free((void *) handshake->certificate_request_context);
Xiaofei Baic234ecf2022-02-08 09:59:23 +00004063 }
4064#endif /* MBEDTLS_SSL_PROTO_TLS1_3 */
Ronald Crone68ab4f2022-10-05 12:46:29 +02004065#endif /* MBEDTLS_SSL_HANDSHAKE_WITH_CERT_ENABLED */
Jerry Yuf017ee42022-01-12 15:49:48 +08004066
Gilles Peskinedf13d5c2018-04-25 20:39:48 +02004067#if defined(MBEDTLS_SSL_ASYNC_PRIVATE)
Gilles Peskine449bd832023-01-11 14:50:10 +01004068 if (ssl->conf->f_async_cancel != NULL && handshake->async_in_progress != 0) {
4069 ssl->conf->f_async_cancel(ssl);
Gilles Peskinedf13d5c2018-04-25 20:39:48 +02004070 handshake->async_in_progress = 0;
4071 }
4072#endif /* MBEDTLS_SSL_ASYNC_PRIVATE */
4073
Andrzej Kurek25f27152022-08-17 16:09:31 -04004074#if defined(MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Andrzej Kurekeb342242019-01-29 09:14:33 -05004075#if defined(MBEDTLS_USE_PSA_CRYPTO)
Gilles Peskine449bd832023-01-11 14:50:10 +01004076 psa_hash_abort(&handshake->fin_sha256_psa);
Andrzej Kurekeb342242019-01-29 09:14:33 -05004077#else
Gilles Peskine449bd832023-01-11 14:50:10 +01004078 mbedtls_sha256_free(&handshake->fin_sha256);
Manuel Pégourié-Gonnardb9d64e52015-07-06 14:18:56 +02004079#endif
Andrzej Kurekeb342242019-01-29 09:14:33 -05004080#endif
Andrzej Kurek25f27152022-08-17 16:09:31 -04004081#if defined(MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Andrzej Kurekeb342242019-01-29 09:14:33 -05004082#if defined(MBEDTLS_USE_PSA_CRYPTO)
Gilles Peskine449bd832023-01-11 14:50:10 +01004083 psa_hash_abort(&handshake->fin_sha384_psa);
Andrzej Kurekeb342242019-01-29 09:14:33 -05004084#else
Gilles Peskine449bd832023-01-11 14:50:10 +01004085 mbedtls_sha512_free(&handshake->fin_sha384);
Manuel Pégourié-Gonnardb9d64e52015-07-06 14:18:56 +02004086#endif
Andrzej Kurekeb342242019-01-29 09:14:33 -05004087#endif
Manuel Pégourié-Gonnardb9d64e52015-07-06 14:18:56 +02004088
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004089#if defined(MBEDTLS_DHM_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01004090 mbedtls_dhm_free(&handshake->dhm_ctx);
Paul Bakker48916f92012-09-16 19:57:18 +00004091#endif
Neil Armstrongf3f46412022-04-12 14:43:39 +02004092#if !defined(MBEDTLS_USE_PSA_CRYPTO) && defined(MBEDTLS_ECDH_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01004093 mbedtls_ecdh_free(&handshake->ecdh_ctx);
Paul Bakker61d113b2013-07-04 11:51:43 +02004094#endif
Valerio Setti02c25b52022-11-15 14:08:42 +01004095
Manuel Pégourié-Gonnardeef142d2015-09-16 10:05:04 +02004096#if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
Neil Armstrongca7d5062022-05-31 14:43:23 +02004097#if defined(MBEDTLS_USE_PSA_CRYPTO)
Gilles Peskine449bd832023-01-11 14:50:10 +01004098 psa_pake_abort(&handshake->psa_pake_ctx);
Valerio Settieb3f7882022-12-08 18:42:58 +01004099 /*
4100 * Opaque keys are not stored in the handshake's data and it's the user
4101 * responsibility to destroy them. Clear ones, instead, are created by
4102 * the TLS library and should be destroyed at the same level
4103 */
Gilles Peskine449bd832023-01-11 14:50:10 +01004104 if (!mbedtls_svc_key_id_is_null(handshake->psa_pake_password)) {
4105 psa_destroy_key(handshake->psa_pake_password);
Valerio Settieb3f7882022-12-08 18:42:58 +01004106 }
Neil Armstrongca7d5062022-05-31 14:43:23 +02004107 handshake->psa_pake_password = MBEDTLS_SVC_KEY_ID_INIT;
4108#else
Gilles Peskine449bd832023-01-11 14:50:10 +01004109 mbedtls_ecjpake_free(&handshake->ecjpake_ctx);
Neil Armstrongca7d5062022-05-31 14:43:23 +02004110#endif /* MBEDTLS_USE_PSA_CRYPTO */
Manuel Pégourié-Gonnard77c06462015-09-17 13:59:49 +02004111#if defined(MBEDTLS_SSL_CLI_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01004112 mbedtls_free(handshake->ecjpake_cache);
Manuel Pégourié-Gonnard77c06462015-09-17 13:59:49 +02004113 handshake->ecjpake_cache = NULL;
4114 handshake->ecjpake_cache_len = 0;
4115#endif
Manuel Pégourié-Gonnard76cfd3f2015-09-15 12:10:54 +02004116#endif
Paul Bakker61d113b2013-07-04 11:51:43 +02004117
Janos Follath4ae5c292016-02-10 11:27:43 +00004118#if defined(MBEDTLS_ECDH_C) || defined(MBEDTLS_ECDSA_C) || \
4119 defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
Paul Bakker9af723c2014-05-01 13:03:14 +02004120 /* explicit void pointer cast for buggy MS compiler */
Gilles Peskine449bd832023-01-11 14:50:10 +01004121 mbedtls_free((void *) handshake->curves_tls_id);
Manuel Pégourié-Gonnardd09453c2013-09-23 19:11:32 +02004122#endif
4123
Ronald Cron73fe8df2022-10-05 14:31:43 +02004124#if defined(MBEDTLS_SSL_HANDSHAKE_WITH_PSK_ENABLED)
Neil Armstrong501c9322022-05-03 09:35:09 +02004125#if defined(MBEDTLS_USE_PSA_CRYPTO)
Gilles Peskine449bd832023-01-11 14:50:10 +01004126 if (!mbedtls_svc_key_id_is_null(ssl->handshake->psk_opaque)) {
Neil Armstrong501c9322022-05-03 09:35:09 +02004127 /* The maintenance of the external PSK key slot is the
4128 * user's responsibility. */
Gilles Peskine449bd832023-01-11 14:50:10 +01004129 if (ssl->handshake->psk_opaque_is_internal) {
4130 psa_destroy_key(ssl->handshake->psk_opaque);
Neil Armstrong501c9322022-05-03 09:35:09 +02004131 ssl->handshake->psk_opaque_is_internal = 0;
4132 }
4133 ssl->handshake->psk_opaque = MBEDTLS_SVC_KEY_ID_INIT;
4134 }
Neil Armstronge952a302022-05-03 10:22:14 +02004135#else
Gilles Peskine449bd832023-01-11 14:50:10 +01004136 if (handshake->psk != NULL) {
4137 mbedtls_platform_zeroize(handshake->psk, handshake->psk_len);
4138 mbedtls_free(handshake->psk);
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01004139 }
Neil Armstronge952a302022-05-03 10:22:14 +02004140#endif /* MBEDTLS_USE_PSA_CRYPTO */
Ronald Cron73fe8df2022-10-05 14:31:43 +02004141#endif /* MBEDTLS_SSL_HANDSHAKE_WITH_PSK_ENABLED */
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01004142
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004143#if defined(MBEDTLS_X509_CRT_PARSE_C) && \
4144 defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
Manuel Pégourié-Gonnard83724542013-09-24 22:30:56 +02004145 /*
4146 * Free only the linked list wrapper, not the keys themselves
4147 * since the belong to the SNI callback
4148 */
Gilles Peskine449bd832023-01-11 14:50:10 +01004149 ssl_key_cert_free(handshake->sni_key_cert);
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004150#endif /* MBEDTLS_X509_CRT_PARSE_C && MBEDTLS_SSL_SERVER_NAME_INDICATION */
Manuel Pégourié-Gonnard705fcca2013-09-23 20:04:20 +02004151
Gilles Peskineeccd8882020-03-10 12:19:08 +01004152#if defined(MBEDTLS_SSL_ECP_RESTARTABLE_ENABLED)
Gilles Peskine449bd832023-01-11 14:50:10 +01004153 mbedtls_x509_crt_restart_free(&handshake->ecrs_ctx);
4154 if (handshake->ecrs_peer_cert != NULL) {
4155 mbedtls_x509_crt_free(handshake->ecrs_peer_cert);
4156 mbedtls_free(handshake->ecrs_peer_cert);
Hanno Becker3dad3112019-02-05 17:19:52 +00004157 }
Manuel Pégourié-Gonnard862cde52017-05-17 11:56:15 +02004158#endif
4159
Hanno Becker75173122019-02-06 16:18:31 +00004160#if defined(MBEDTLS_X509_CRT_PARSE_C) && \
4161 !defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
Gilles Peskine449bd832023-01-11 14:50:10 +01004162 mbedtls_pk_free(&handshake->peer_pubkey);
Hanno Becker75173122019-02-06 16:18:31 +00004163#endif /* MBEDTLS_X509_CRT_PARSE_C && !MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
4164
XiaokangQian9b93c0d2022-02-09 06:02:25 +00004165#if defined(MBEDTLS_SSL_CLI_C) && \
Gilles Peskine449bd832023-01-11 14:50:10 +01004166 (defined(MBEDTLS_SSL_PROTO_DTLS) || defined(MBEDTLS_SSL_PROTO_TLS1_3))
4167 mbedtls_free(handshake->cookie);
XiaokangQian9b93c0d2022-02-09 06:02:25 +00004168#endif /* MBEDTLS_SSL_CLI_C &&
4169 ( MBEDTLS_SSL_PROTO_DTLS || MBEDTLS_SSL_PROTO_TLS1_3 ) */
XiaokangQian8499b6c2022-01-27 09:00:11 +00004170
4171#if defined(MBEDTLS_SSL_PROTO_DTLS)
Gilles Peskine449bd832023-01-11 14:50:10 +01004172 mbedtls_ssl_flight_free(handshake->flight);
4173 mbedtls_ssl_buffering_free(ssl);
XiaokangQian8499b6c2022-01-27 09:00:11 +00004174#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnard74848812014-07-11 02:43:49 +02004175
Ronald Cronf12b81d2022-03-15 10:42:41 +01004176#if defined(MBEDTLS_ECDH_C) && \
Gilles Peskine449bd832023-01-11 14:50:10 +01004177 (defined(MBEDTLS_USE_PSA_CRYPTO) || defined(MBEDTLS_SSL_PROTO_TLS1_3))
4178 if (handshake->ecdh_psa_privkey_is_external == 0) {
4179 psa_destroy_key(handshake->ecdh_psa_privkey);
4180 }
Hanno Becker4a63ed42019-01-08 11:39:35 +00004181#endif /* MBEDTLS_ECDH_C && MBEDTLS_USE_PSA_CRYPTO */
4182
Ronald Cron6f135e12021-12-08 16:57:54 +01004183#if defined(MBEDTLS_SSL_PROTO_TLS1_3)
Gilles Peskine449bd832023-01-11 14:50:10 +01004184 mbedtls_ssl_transform_free(handshake->transform_handshake);
4185 mbedtls_free(handshake->transform_handshake);
Jerry Yu3d9b5902022-11-04 14:07:25 +08004186#if defined(MBEDTLS_SSL_EARLY_DATA)
Gilles Peskine449bd832023-01-11 14:50:10 +01004187 mbedtls_ssl_transform_free(handshake->transform_earlydata);
4188 mbedtls_free(handshake->transform_earlydata);
Jerry Yu3d9b5902022-11-04 14:07:25 +08004189#endif
Ronald Cron6f135e12021-12-08 16:57:54 +01004190#endif /* MBEDTLS_SSL_PROTO_TLS1_3 */
Jerry Yuba9c7272021-10-30 11:54:10 +08004191
Andrzej Kurek0afa2a12020-03-03 10:39:58 -05004192
4193#if defined(MBEDTLS_SSL_VARIABLE_BUFFER_LENGTH)
4194 /* If the buffers are too big - reallocate. Because of the way Mbed TLS
4195 * processes datagrams and the fact that a datagram is allowed to have
4196 * several records in it, it is possible that the I/O buffers are not
4197 * empty at this stage */
Gilles Peskine449bd832023-01-11 14:50:10 +01004198 handle_buffer_resizing(ssl, 1, mbedtls_ssl_get_input_buflen(ssl),
4199 mbedtls_ssl_get_output_buflen(ssl));
Andrzej Kurek0afa2a12020-03-03 10:39:58 -05004200#endif
Hanno Becker3aa186f2021-08-10 09:24:19 +01004201
Jerry Yuba9c7272021-10-30 11:54:10 +08004202 /* mbedtls_platform_zeroize MUST be last one in this function */
Gilles Peskine449bd832023-01-11 14:50:10 +01004203 mbedtls_platform_zeroize(handshake,
4204 sizeof(mbedtls_ssl_handshake_params));
Paul Bakker48916f92012-09-16 19:57:18 +00004205}
4206
Gilles Peskine449bd832023-01-11 14:50:10 +01004207void mbedtls_ssl_session_free(mbedtls_ssl_session *session)
Paul Bakker48916f92012-09-16 19:57:18 +00004208{
Gilles Peskine449bd832023-01-11 14:50:10 +01004209 if (session == NULL) {
Paul Bakkeraccaffe2014-06-26 13:37:14 +02004210 return;
Gilles Peskine449bd832023-01-11 14:50:10 +01004211 }
Paul Bakkeraccaffe2014-06-26 13:37:14 +02004212
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004213#if defined(MBEDTLS_X509_CRT_PARSE_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01004214 ssl_clear_peer_cert(session);
Paul Bakkered27a042013-04-18 22:46:23 +02004215#endif
Paul Bakker0a597072012-09-25 21:55:46 +00004216
Manuel Pégourié-Gonnardb596abf2015-05-20 10:45:29 +02004217#if defined(MBEDTLS_SSL_SESSION_TICKETS) && defined(MBEDTLS_SSL_CLI_C)
Xiaokang Qianbc663a02022-10-09 11:14:39 +00004218#if defined(MBEDTLS_SSL_PROTO_TLS1_3) && \
4219 defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
Gilles Peskine449bd832023-01-11 14:50:10 +01004220 mbedtls_free(session->hostname);
Xiaokang Qian281fd1b2022-09-20 11:35:41 +00004221#endif
Gilles Peskine449bd832023-01-11 14:50:10 +01004222 mbedtls_free(session->ticket);
Paul Bakkera503a632013-08-14 13:48:06 +02004223#endif
Manuel Pégourié-Gonnard75d44012013-08-02 14:44:04 +02004224
Gilles Peskine449bd832023-01-11 14:50:10 +01004225 mbedtls_platform_zeroize(session, sizeof(mbedtls_ssl_session));
Paul Bakker48916f92012-09-16 19:57:18 +00004226}
4227
Manuel Pégourié-Gonnard5c0e3772019-07-23 16:13:17 +02004228#if defined(MBEDTLS_SSL_CONTEXT_SERIALIZATION)
Manuel Pégourié-Gonnard4e9370b2019-07-23 16:31:16 +02004229
4230#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
4231#define SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_CONNECTION_ID 1u
4232#else
4233#define SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_CONNECTION_ID 0u
4234#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
4235
Manuel Pégourié-Gonnard4e9370b2019-07-23 16:31:16 +02004236#define SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_BADMAC_LIMIT 1u
Manuel Pégourié-Gonnard4e9370b2019-07-23 16:31:16 +02004237
4238#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
4239#define SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_ANTI_REPLAY 1u
4240#else
4241#define SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_ANTI_REPLAY 0u
4242#endif /* MBEDTLS_SSL_DTLS_ANTI_REPLAY */
4243
4244#if defined(MBEDTLS_SSL_ALPN)
4245#define SSL_SERIALIZED_CONTEXT_CONFIG_ALPN 1u
4246#else
4247#define SSL_SERIALIZED_CONTEXT_CONFIG_ALPN 0u
4248#endif /* MBEDTLS_SSL_ALPN */
4249
4250#define SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_CONNECTION_ID_BIT 0
4251#define SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_BADMAC_LIMIT_BIT 1
4252#define SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_ANTI_REPLAY_BIT 2
4253#define SSL_SERIALIZED_CONTEXT_CONFIG_ALPN_BIT 3
4254
4255#define SSL_SERIALIZED_CONTEXT_CONFIG_BITFLAG \
Gilles Peskine449bd832023-01-11 14:50:10 +01004256 ((uint32_t) ( \
4257 (SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_CONNECTION_ID << \
4258 SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_CONNECTION_ID_BIT) | \
4259 (SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_BADMAC_LIMIT << \
4260 SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_BADMAC_LIMIT_BIT) | \
4261 (SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_ANTI_REPLAY << \
4262 SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_ANTI_REPLAY_BIT) | \
4263 (SSL_SERIALIZED_CONTEXT_CONFIG_ALPN << SSL_SERIALIZED_CONTEXT_CONFIG_ALPN_BIT) | \
4264 0u))
Manuel Pégourié-Gonnard4e9370b2019-07-23 16:31:16 +02004265
Manuel Pégourié-Gonnard4c90e852019-07-11 10:58:10 +02004266static unsigned char ssl_serialized_context_header[] = {
4267 MBEDTLS_VERSION_MAJOR,
4268 MBEDTLS_VERSION_MINOR,
4269 MBEDTLS_VERSION_PATCH,
Gilles Peskine449bd832023-01-11 14:50:10 +01004270 MBEDTLS_BYTE_1(SSL_SERIALIZED_SESSION_CONFIG_BITFLAG),
4271 MBEDTLS_BYTE_0(SSL_SERIALIZED_SESSION_CONFIG_BITFLAG),
4272 MBEDTLS_BYTE_2(SSL_SERIALIZED_CONTEXT_CONFIG_BITFLAG),
4273 MBEDTLS_BYTE_1(SSL_SERIALIZED_CONTEXT_CONFIG_BITFLAG),
4274 MBEDTLS_BYTE_0(SSL_SERIALIZED_CONTEXT_CONFIG_BITFLAG),
Manuel Pégourié-Gonnard4c90e852019-07-11 10:58:10 +02004275};
4276
Paul Bakker5121ce52009-01-03 21:22:43 +00004277/*
Manuel Pégourié-Gonnardac87e282019-05-28 13:02:16 +02004278 * Serialize a full SSL context
Manuel Pégourié-Gonnard00400c22019-07-10 14:58:45 +02004279 *
4280 * The format of the serialized data is:
4281 * (in the presentation language of TLS, RFC 8446 section 3)
4282 *
4283 * // header
4284 * opaque mbedtls_version[3]; // major, minor, patch
Manuel Pégourié-Gonnard4c90e852019-07-11 10:58:10 +02004285 * opaque context_format[5]; // version-specific field determining
Manuel Pégourié-Gonnard00400c22019-07-10 14:58:45 +02004286 * // the format of the remaining
Manuel Pégourié-Gonnard4c90e852019-07-11 10:58:10 +02004287 * // serialized data.
Manuel Pégourié-Gonnard4e9370b2019-07-23 16:31:16 +02004288 * Note: When updating the format, remember to keep these
4289 * version+format bytes. (We may make their size part of the API.)
Manuel Pégourié-Gonnard00400c22019-07-10 14:58:45 +02004290 *
4291 * // session sub-structure
4292 * opaque session<1..2^32-1>; // see mbedtls_ssl_session_save()
4293 * // transform sub-structure
4294 * uint8 random[64]; // ServerHello.random+ClientHello.random
4295 * uint8 in_cid<0..2^8-1> // Connection ID: expected incoming value
4296 * uint8 out_cid<0..2^8-1> // Connection ID: outgoing value to use
4297 * // fields from ssl_context
4298 * uint32 badmac_seen; // DTLS: number of records with failing MAC
4299 * uint64 in_window_top; // DTLS: last validated record seq_num
4300 * uint64 in_window; // DTLS: bitmask for replay protection
4301 * uint8 disable_datagram_packing; // DTLS: only one record per datagram
4302 * uint64 cur_out_ctr; // Record layer: outgoing sequence number
4303 * uint16 mtu; // DTLS: path mtu (max outgoing fragment size)
4304 * uint8 alpn_chosen<0..2^8-1> // ALPN: negotiated application protocol
4305 *
4306 * Note that many fields of the ssl_context or sub-structures are not
4307 * serialized, as they fall in one of the following categories:
4308 *
4309 * 1. forced value (eg in_left must be 0)
4310 * 2. pointer to dynamically-allocated memory (eg session, transform)
4311 * 3. value can be re-derived from other data (eg session keys from MS)
4312 * 4. value was temporary (eg content of input buffer)
4313 * 5. value will be provided by the user again (eg I/O callbacks and context)
Manuel Pégourié-Gonnardac87e282019-05-28 13:02:16 +02004314 */
Gilles Peskine449bd832023-01-11 14:50:10 +01004315int mbedtls_ssl_context_save(mbedtls_ssl_context *ssl,
4316 unsigned char *buf,
4317 size_t buf_len,
4318 size_t *olen)
Manuel Pégourié-Gonnardac87e282019-05-28 13:02:16 +02004319{
Manuel Pégourié-Gonnard4c90e852019-07-11 10:58:10 +02004320 unsigned char *p = buf;
4321 size_t used = 0;
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02004322 size_t session_len;
4323 int ret = 0;
Manuel Pégourié-Gonnard4c90e852019-07-11 10:58:10 +02004324
Manuel Pégourié-Gonnard1aaf6692019-07-10 14:14:05 +02004325 /*
Manuel Pégourié-Gonnarde4588692019-07-29 12:28:52 +02004326 * Enforce usage restrictions, see "return BAD_INPUT_DATA" in
4327 * this function's documentation.
4328 *
4329 * These are due to assumptions/limitations in the implementation. Some of
4330 * them are likely to stay (no handshake in progress) some might go away
4331 * (only DTLS) but are currently used to simplify the implementation.
Manuel Pégourié-Gonnard1aaf6692019-07-10 14:14:05 +02004332 */
Manuel Pégourié-Gonnarde4588692019-07-29 12:28:52 +02004333 /* The initial handshake must be over */
Gilles Peskine449bd832023-01-11 14:50:10 +01004334 if (mbedtls_ssl_is_handshake_over(ssl) == 0) {
4335 MBEDTLS_SSL_DEBUG_MSG(1, ("Initial handshake isn't over"));
4336 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
Jarno Lamsa8c51b7c2019-08-21 13:45:05 +03004337 }
Gilles Peskine449bd832023-01-11 14:50:10 +01004338 if (ssl->handshake != NULL) {
4339 MBEDTLS_SSL_DEBUG_MSG(1, ("Handshake isn't completed"));
4340 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
Jarno Lamsa8c51b7c2019-08-21 13:45:05 +03004341 }
Manuel Pégourié-Gonnarde4588692019-07-29 12:28:52 +02004342 /* Double-check that sub-structures are indeed ready */
Gilles Peskine449bd832023-01-11 14:50:10 +01004343 if (ssl->transform == NULL || ssl->session == NULL) {
4344 MBEDTLS_SSL_DEBUG_MSG(1, ("Serialised structures aren't ready"));
4345 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
Jarno Lamsa8c51b7c2019-08-21 13:45:05 +03004346 }
Manuel Pégourié-Gonnarde4588692019-07-29 12:28:52 +02004347 /* There must be no pending incoming or outgoing data */
Gilles Peskine449bd832023-01-11 14:50:10 +01004348 if (mbedtls_ssl_check_pending(ssl) != 0) {
4349 MBEDTLS_SSL_DEBUG_MSG(1, ("There is pending incoming data"));
4350 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
Jarno Lamsa8c51b7c2019-08-21 13:45:05 +03004351 }
Gilles Peskine449bd832023-01-11 14:50:10 +01004352 if (ssl->out_left != 0) {
4353 MBEDTLS_SSL_DEBUG_MSG(1, ("There is pending outgoing data"));
4354 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
Jarno Lamsa8c51b7c2019-08-21 13:45:05 +03004355 }
Dave Rodgman556e8a32022-12-06 16:31:25 +00004356 /* Protocol must be DTLS, not TLS */
Gilles Peskine449bd832023-01-11 14:50:10 +01004357 if (ssl->conf->transport != MBEDTLS_SSL_TRANSPORT_DATAGRAM) {
4358 MBEDTLS_SSL_DEBUG_MSG(1, ("Only DTLS is supported"));
4359 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
Jarno Lamsa8c51b7c2019-08-21 13:45:05 +03004360 }
Manuel Pégourié-Gonnarde4588692019-07-29 12:28:52 +02004361 /* Version must be 1.2 */
Gilles Peskine449bd832023-01-11 14:50:10 +01004362 if (ssl->tls_version != MBEDTLS_SSL_VERSION_TLS1_2) {
4363 MBEDTLS_SSL_DEBUG_MSG(1, ("Only version 1.2 supported"));
4364 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
Jarno Lamsa8c51b7c2019-08-21 13:45:05 +03004365 }
Manuel Pégourié-Gonnarde4588692019-07-29 12:28:52 +02004366 /* We must be using an AEAD ciphersuite */
Gilles Peskine449bd832023-01-11 14:50:10 +01004367 if (mbedtls_ssl_transform_uses_aead(ssl->transform) != 1) {
4368 MBEDTLS_SSL_DEBUG_MSG(1, ("Only AEAD ciphersuites supported"));
4369 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
Jarno Lamsa8c51b7c2019-08-21 13:45:05 +03004370 }
Manuel Pégourié-Gonnarde4588692019-07-29 12:28:52 +02004371 /* Renegotiation must not be enabled */
4372#if defined(MBEDTLS_SSL_RENEGOTIATION)
Gilles Peskine449bd832023-01-11 14:50:10 +01004373 if (ssl->conf->disable_renegotiation != MBEDTLS_SSL_RENEGOTIATION_DISABLED) {
4374 MBEDTLS_SSL_DEBUG_MSG(1, ("Renegotiation must not be enabled"));
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#endif
Manuel Pégourié-Gonnardac87e282019-05-28 13:02:16 +02004378
Manuel Pégourié-Gonnard4c90e852019-07-11 10:58:10 +02004379 /*
4380 * Version and format identifier
4381 */
Gilles Peskine449bd832023-01-11 14:50:10 +01004382 used += sizeof(ssl_serialized_context_header);
Manuel Pégourié-Gonnardac87e282019-05-28 13:02:16 +02004383
Gilles Peskine449bd832023-01-11 14:50:10 +01004384 if (used <= buf_len) {
4385 memcpy(p, ssl_serialized_context_header,
4386 sizeof(ssl_serialized_context_header));
4387 p += sizeof(ssl_serialized_context_header);
Manuel Pégourié-Gonnard4c90e852019-07-11 10:58:10 +02004388 }
4389
4390 /*
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02004391 * Session (length + data)
4392 */
Gilles Peskine449bd832023-01-11 14:50:10 +01004393 ret = ssl_session_save(ssl->session, 1, NULL, 0, &session_len);
4394 if (ret != MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL) {
4395 return ret;
4396 }
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02004397
4398 used += 4 + session_len;
Gilles Peskine449bd832023-01-11 14:50:10 +01004399 if (used <= buf_len) {
4400 MBEDTLS_PUT_UINT32_BE(session_len, p, 0);
Joe Subbiani1f6c3ae2021-08-20 11:44:44 +01004401 p += 4;
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02004402
Gilles Peskine449bd832023-01-11 14:50:10 +01004403 ret = ssl_session_save(ssl->session, 1,
4404 p, session_len, &session_len);
4405 if (ret != 0) {
4406 return ret;
4407 }
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02004408
4409 p += session_len;
4410 }
4411
4412 /*
Manuel Pégourié-Gonnardc2a7b892019-07-15 09:04:11 +02004413 * Transform
4414 */
Gilles Peskine449bd832023-01-11 14:50:10 +01004415 used += sizeof(ssl->transform->randbytes);
4416 if (used <= buf_len) {
4417 memcpy(p, ssl->transform->randbytes,
4418 sizeof(ssl->transform->randbytes));
4419 p += sizeof(ssl->transform->randbytes);
Manuel Pégourié-Gonnardc2a7b892019-07-15 09:04:11 +02004420 }
4421
4422#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
4423 used += 2 + ssl->transform->in_cid_len + ssl->transform->out_cid_len;
Gilles Peskine449bd832023-01-11 14:50:10 +01004424 if (used <= buf_len) {
Manuel Pégourié-Gonnardc2a7b892019-07-15 09:04:11 +02004425 *p++ = ssl->transform->in_cid_len;
Gilles Peskine449bd832023-01-11 14:50:10 +01004426 memcpy(p, ssl->transform->in_cid, ssl->transform->in_cid_len);
Manuel Pégourié-Gonnardc2a7b892019-07-15 09:04:11 +02004427 p += ssl->transform->in_cid_len;
4428
4429 *p++ = ssl->transform->out_cid_len;
Gilles Peskine449bd832023-01-11 14:50:10 +01004430 memcpy(p, ssl->transform->out_cid, ssl->transform->out_cid_len);
Manuel Pégourié-Gonnardc2a7b892019-07-15 09:04:11 +02004431 p += ssl->transform->out_cid_len;
4432 }
4433#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
4434
4435 /*
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02004436 * Saved fields from top-level ssl_context structure
4437 */
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02004438 used += 4;
Gilles Peskine449bd832023-01-11 14:50:10 +01004439 if (used <= buf_len) {
4440 MBEDTLS_PUT_UINT32_BE(ssl->badmac_seen, p, 0);
Joe Subbiani1f6c3ae2021-08-20 11:44:44 +01004441 p += 4;
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02004442 }
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02004443
4444#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
4445 used += 16;
Gilles Peskine449bd832023-01-11 14:50:10 +01004446 if (used <= buf_len) {
4447 MBEDTLS_PUT_UINT64_BE(ssl->in_window_top, p, 0);
Joe Subbiani1f6c3ae2021-08-20 11:44:44 +01004448 p += 8;
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02004449
Gilles Peskine449bd832023-01-11 14:50:10 +01004450 MBEDTLS_PUT_UINT64_BE(ssl->in_window, p, 0);
Joe Subbiani1f6c3ae2021-08-20 11:44:44 +01004451 p += 8;
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02004452 }
4453#endif /* MBEDTLS_SSL_DTLS_ANTI_REPLAY */
4454
4455#if defined(MBEDTLS_SSL_PROTO_DTLS)
4456 used += 1;
Gilles Peskine449bd832023-01-11 14:50:10 +01004457 if (used <= buf_len) {
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02004458 *p++ = ssl->disable_datagram_packing;
4459 }
4460#endif /* MBEDTLS_SSL_PROTO_DTLS */
4461
Jerry Yuae0b2e22021-10-08 15:21:19 +08004462 used += MBEDTLS_SSL_SEQUENCE_NUMBER_LEN;
Gilles Peskine449bd832023-01-11 14:50:10 +01004463 if (used <= buf_len) {
4464 memcpy(p, ssl->cur_out_ctr, MBEDTLS_SSL_SEQUENCE_NUMBER_LEN);
Jerry Yuae0b2e22021-10-08 15:21:19 +08004465 p += MBEDTLS_SSL_SEQUENCE_NUMBER_LEN;
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02004466 }
4467
4468#if defined(MBEDTLS_SSL_PROTO_DTLS)
4469 used += 2;
Gilles Peskine449bd832023-01-11 14:50:10 +01004470 if (used <= buf_len) {
4471 MBEDTLS_PUT_UINT16_BE(ssl->mtu, p, 0);
Joe Subbiani1f6c3ae2021-08-20 11:44:44 +01004472 p += 2;
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02004473 }
4474#endif /* MBEDTLS_SSL_PROTO_DTLS */
4475
4476#if defined(MBEDTLS_SSL_ALPN)
4477 {
4478 const uint8_t alpn_len = ssl->alpn_chosen
Gilles Peskine449bd832023-01-11 14:50:10 +01004479 ? (uint8_t) strlen(ssl->alpn_chosen)
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02004480 : 0;
4481
4482 used += 1 + alpn_len;
Gilles Peskine449bd832023-01-11 14:50:10 +01004483 if (used <= buf_len) {
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02004484 *p++ = alpn_len;
4485
Gilles Peskine449bd832023-01-11 14:50:10 +01004486 if (ssl->alpn_chosen != NULL) {
4487 memcpy(p, ssl->alpn_chosen, alpn_len);
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02004488 p += alpn_len;
4489 }
4490 }
4491 }
4492#endif /* MBEDTLS_SSL_ALPN */
4493
4494 /*
Manuel Pégourié-Gonnard4c90e852019-07-11 10:58:10 +02004495 * Done
4496 */
4497 *olen = used;
4498
Gilles Peskine449bd832023-01-11 14:50:10 +01004499 if (used > buf_len) {
4500 return MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL;
4501 }
Manuel Pégourié-Gonnardac87e282019-05-28 13:02:16 +02004502
Gilles Peskine449bd832023-01-11 14:50:10 +01004503 MBEDTLS_SSL_DEBUG_BUF(4, "saved context", buf, used);
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02004504
Gilles Peskine449bd832023-01-11 14:50:10 +01004505 return mbedtls_ssl_session_reset_int(ssl, 0);
Manuel Pégourié-Gonnardac87e282019-05-28 13:02:16 +02004506}
4507
4508/*
Manuel Pégourié-Gonnardb9dfc9f2019-07-12 10:50:19 +02004509 * Deserialize context, see mbedtls_ssl_context_save() for format.
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02004510 *
4511 * This internal version is wrapped by a public function that cleans up in
4512 * case of error.
Manuel Pégourié-Gonnardac87e282019-05-28 13:02:16 +02004513 */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02004514MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01004515static int ssl_context_load(mbedtls_ssl_context *ssl,
4516 const unsigned char *buf,
4517 size_t len)
Manuel Pégourié-Gonnardac87e282019-05-28 13:02:16 +02004518{
Manuel Pégourié-Gonnard4c90e852019-07-11 10:58:10 +02004519 const unsigned char *p = buf;
4520 const unsigned char * const end = buf + len;
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02004521 size_t session_len;
Janos Follath865b3eb2019-12-16 11:46:15 +00004522 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Andrzej Kurek2d59dbc2022-10-13 08:34:38 -04004523#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
Andrzej Kurek894edde2022-09-29 06:31:14 -04004524 tls_prf_fn prf_func = NULL;
Andrzej Kurek2d59dbc2022-10-13 08:34:38 -04004525#endif
Manuel Pégourié-Gonnard4c90e852019-07-11 10:58:10 +02004526
Manuel Pégourié-Gonnard0ff76402019-07-11 09:56:30 +02004527 /*
4528 * The context should have been freshly setup or reset.
4529 * Give the user an error in case of obvious misuse.
Manuel Pégourié-Gonnard4ca930f2019-07-26 16:31:53 +02004530 * (Checking session is useful because it won't be NULL if we're
Manuel Pégourié-Gonnard0ff76402019-07-11 09:56:30 +02004531 * renegotiating, or if the user mistakenly loaded a session first.)
4532 */
Gilles Peskine449bd832023-01-11 14:50:10 +01004533 if (ssl->state != MBEDTLS_SSL_HELLO_REQUEST ||
4534 ssl->session != NULL) {
4535 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
Manuel Pégourié-Gonnard0ff76402019-07-11 09:56:30 +02004536 }
4537
4538 /*
4539 * We can't check that the config matches the initial one, but we can at
4540 * least check it matches the requirements for serializing.
4541 */
Gilles Peskine449bd832023-01-11 14:50:10 +01004542 if (ssl->conf->transport != MBEDTLS_SSL_TRANSPORT_DATAGRAM ||
Glenn Strauss2dfcea22022-03-14 17:26:42 -04004543 ssl->conf->max_tls_version < MBEDTLS_SSL_VERSION_TLS1_2 ||
4544 ssl->conf->min_tls_version > MBEDTLS_SSL_VERSION_TLS1_2 ||
Manuel Pégourié-Gonnard0ff76402019-07-11 09:56:30 +02004545#if defined(MBEDTLS_SSL_RENEGOTIATION)
Manuel Pégourié-Gonnard9a96fd72019-07-23 17:11:24 +02004546 ssl->conf->disable_renegotiation != MBEDTLS_SSL_RENEGOTIATION_DISABLED ||
Manuel Pégourié-Gonnard0ff76402019-07-11 09:56:30 +02004547#endif
Gilles Peskine449bd832023-01-11 14:50:10 +01004548 0) {
4549 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
Manuel Pégourié-Gonnard0ff76402019-07-11 09:56:30 +02004550 }
4551
Gilles Peskine449bd832023-01-11 14:50:10 +01004552 MBEDTLS_SSL_DEBUG_BUF(4, "context to load", buf, len);
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02004553
Manuel Pégourié-Gonnard4c90e852019-07-11 10:58:10 +02004554 /*
4555 * Check version identifier
4556 */
Gilles Peskine449bd832023-01-11 14:50:10 +01004557 if ((size_t) (end - p) < sizeof(ssl_serialized_context_header)) {
4558 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
Manuel Pégourié-Gonnard4c90e852019-07-11 10:58:10 +02004559 }
Gilles Peskine449bd832023-01-11 14:50:10 +01004560
4561 if (memcmp(p, ssl_serialized_context_header,
4562 sizeof(ssl_serialized_context_header)) != 0) {
4563 return MBEDTLS_ERR_SSL_VERSION_MISMATCH;
4564 }
4565 p += sizeof(ssl_serialized_context_header);
Manuel Pégourié-Gonnard4c90e852019-07-11 10:58:10 +02004566
4567 /*
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02004568 * Session
4569 */
Gilles Peskine449bd832023-01-11 14:50:10 +01004570 if ((size_t) (end - p) < 4) {
4571 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
4572 }
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02004573
Gilles Peskine449bd832023-01-11 14:50:10 +01004574 session_len = ((size_t) p[0] << 24) |
4575 ((size_t) p[1] << 16) |
4576 ((size_t) p[2] << 8) |
4577 ((size_t) p[3]);
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02004578 p += 4;
4579
Manuel Pégourié-Gonnard142ba732019-07-23 14:43:30 +02004580 /* This has been allocated by ssl_handshake_init(), called by
Hanno Becker43aefe22020-02-05 10:44:56 +00004581 * by either mbedtls_ssl_session_reset_int() or mbedtls_ssl_setup(). */
Manuel Pégourié-Gonnard142ba732019-07-23 14:43:30 +02004582 ssl->session = ssl->session_negotiate;
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02004583 ssl->session_in = ssl->session;
4584 ssl->session_out = ssl->session;
Manuel Pégourié-Gonnard142ba732019-07-23 14:43:30 +02004585 ssl->session_negotiate = NULL;
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02004586
Gilles Peskine449bd832023-01-11 14:50:10 +01004587 if ((size_t) (end - p) < session_len) {
4588 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
4589 }
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02004590
Gilles Peskine449bd832023-01-11 14:50:10 +01004591 ret = ssl_session_load(ssl->session, 1, p, session_len);
4592 if (ret != 0) {
4593 mbedtls_ssl_session_free(ssl->session);
4594 return ret;
Manuel Pégourié-Gonnard45ac1f02019-07-23 16:52:45 +02004595 }
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02004596
4597 p += session_len;
4598
4599 /*
Manuel Pégourié-Gonnardc2a7b892019-07-15 09:04:11 +02004600 * Transform
4601 */
4602
Manuel Pégourié-Gonnard142ba732019-07-23 14:43:30 +02004603 /* This has been allocated by ssl_handshake_init(), called by
Hanno Becker43aefe22020-02-05 10:44:56 +00004604 * by either mbedtls_ssl_session_reset_int() or mbedtls_ssl_setup(). */
Jerry Yu2e199812022-12-01 18:57:19 +08004605#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
Manuel Pégourié-Gonnard142ba732019-07-23 14:43:30 +02004606 ssl->transform = ssl->transform_negotiate;
Manuel Pégourié-Gonnardc2a7b892019-07-15 09:04:11 +02004607 ssl->transform_in = ssl->transform;
4608 ssl->transform_out = ssl->transform;
Manuel Pégourié-Gonnard142ba732019-07-23 14:43:30 +02004609 ssl->transform_negotiate = NULL;
Jerry Yu2e199812022-12-01 18:57:19 +08004610#endif
Manuel Pégourié-Gonnardc2a7b892019-07-15 09:04:11 +02004611
Andrzej Kurek2d59dbc2022-10-13 08:34:38 -04004612#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
Gilles Peskine449bd832023-01-11 14:50:10 +01004613 prf_func = ssl_tls12prf_from_cs(ssl->session->ciphersuite);
4614 if (prf_func == NULL) {
4615 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
4616 }
Andrzej Kurek894edde2022-09-29 06:31:14 -04004617
Manuel Pégourié-Gonnardc2a7b892019-07-15 09:04:11 +02004618 /* Read random bytes and populate structure */
Gilles Peskine449bd832023-01-11 14:50:10 +01004619 if ((size_t) (end - p) < sizeof(ssl->transform->randbytes)) {
4620 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
4621 }
Andrzej Kurek2d59dbc2022-10-13 08:34:38 -04004622
Gilles Peskine449bd832023-01-11 14:50:10 +01004623 ret = ssl_tls12_populate_transform(ssl->transform,
4624 ssl->session->ciphersuite,
4625 ssl->session->master,
Neil Armstrongf2c82f02022-04-05 11:16:53 +02004626#if defined(MBEDTLS_SSL_SOME_SUITES_USE_CBC_ETM)
Gilles Peskine449bd832023-01-11 14:50:10 +01004627 ssl->session->encrypt_then_mac,
Neil Armstrongf2c82f02022-04-05 11:16:53 +02004628#endif /* MBEDTLS_SSL_SOME_SUITES_USE_CBC_ETM */
Gilles Peskine449bd832023-01-11 14:50:10 +01004629 prf_func,
4630 p, /* currently pointing to randbytes */
4631 MBEDTLS_SSL_VERSION_TLS1_2, /* (D)TLS 1.2 is forced */
4632 ssl->conf->endpoint,
4633 ssl);
4634 if (ret != 0) {
4635 return ret;
4636 }
Jerry Yu840fbb22022-02-17 14:59:29 +08004637#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Gilles Peskine449bd832023-01-11 14:50:10 +01004638 p += sizeof(ssl->transform->randbytes);
Manuel Pégourié-Gonnardc2a7b892019-07-15 09:04:11 +02004639
4640#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
4641 /* Read connection IDs and store them */
Gilles Peskine449bd832023-01-11 14:50:10 +01004642 if ((size_t) (end - p) < 1) {
4643 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
4644 }
Manuel Pégourié-Gonnardc2a7b892019-07-15 09:04:11 +02004645
4646 ssl->transform->in_cid_len = *p++;
4647
Gilles Peskine449bd832023-01-11 14:50:10 +01004648 if ((size_t) (end - p) < ssl->transform->in_cid_len + 1u) {
4649 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
4650 }
Manuel Pégourié-Gonnardc2a7b892019-07-15 09:04:11 +02004651
Gilles Peskine449bd832023-01-11 14:50:10 +01004652 memcpy(ssl->transform->in_cid, p, ssl->transform->in_cid_len);
Manuel Pégourié-Gonnardc2a7b892019-07-15 09:04:11 +02004653 p += ssl->transform->in_cid_len;
4654
4655 ssl->transform->out_cid_len = *p++;
4656
Gilles Peskine449bd832023-01-11 14:50:10 +01004657 if ((size_t) (end - p) < ssl->transform->out_cid_len) {
4658 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
4659 }
Manuel Pégourié-Gonnardc2a7b892019-07-15 09:04:11 +02004660
Gilles Peskine449bd832023-01-11 14:50:10 +01004661 memcpy(ssl->transform->out_cid, p, ssl->transform->out_cid_len);
Manuel Pégourié-Gonnardc2a7b892019-07-15 09:04:11 +02004662 p += ssl->transform->out_cid_len;
4663#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
4664
4665 /*
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02004666 * Saved fields from top-level ssl_context structure
4667 */
Gilles Peskine449bd832023-01-11 14:50:10 +01004668 if ((size_t) (end - p) < 4) {
4669 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
4670 }
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02004671
Gilles Peskine449bd832023-01-11 14:50:10 +01004672 ssl->badmac_seen = ((uint32_t) p[0] << 24) |
4673 ((uint32_t) p[1] << 16) |
4674 ((uint32_t) p[2] << 8) |
4675 ((uint32_t) p[3]);
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02004676 p += 4;
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02004677
4678#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
Gilles Peskine449bd832023-01-11 14:50:10 +01004679 if ((size_t) (end - p) < 16) {
4680 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
4681 }
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02004682
Gilles Peskine449bd832023-01-11 14:50:10 +01004683 ssl->in_window_top = ((uint64_t) p[0] << 56) |
4684 ((uint64_t) p[1] << 48) |
4685 ((uint64_t) p[2] << 40) |
4686 ((uint64_t) p[3] << 32) |
4687 ((uint64_t) p[4] << 24) |
4688 ((uint64_t) p[5] << 16) |
4689 ((uint64_t) p[6] << 8) |
4690 ((uint64_t) p[7]);
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02004691 p += 8;
4692
Gilles Peskine449bd832023-01-11 14:50:10 +01004693 ssl->in_window = ((uint64_t) p[0] << 56) |
4694 ((uint64_t) p[1] << 48) |
4695 ((uint64_t) p[2] << 40) |
4696 ((uint64_t) p[3] << 32) |
4697 ((uint64_t) p[4] << 24) |
4698 ((uint64_t) p[5] << 16) |
4699 ((uint64_t) p[6] << 8) |
4700 ((uint64_t) p[7]);
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02004701 p += 8;
4702#endif /* MBEDTLS_SSL_DTLS_ANTI_REPLAY */
4703
4704#if defined(MBEDTLS_SSL_PROTO_DTLS)
Gilles Peskine449bd832023-01-11 14:50:10 +01004705 if ((size_t) (end - p) < 1) {
4706 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
4707 }
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02004708
4709 ssl->disable_datagram_packing = *p++;
4710#endif /* MBEDTLS_SSL_PROTO_DTLS */
4711
Gilles Peskine449bd832023-01-11 14:50:10 +01004712 if ((size_t) (end - p) < sizeof(ssl->cur_out_ctr)) {
4713 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
4714 }
4715 memcpy(ssl->cur_out_ctr, p, sizeof(ssl->cur_out_ctr));
4716 p += sizeof(ssl->cur_out_ctr);
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02004717
4718#if defined(MBEDTLS_SSL_PROTO_DTLS)
Gilles Peskine449bd832023-01-11 14:50:10 +01004719 if ((size_t) (end - p) < 2) {
4720 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
4721 }
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02004722
Gilles Peskine449bd832023-01-11 14:50:10 +01004723 ssl->mtu = (p[0] << 8) | p[1];
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02004724 p += 2;
4725#endif /* MBEDTLS_SSL_PROTO_DTLS */
4726
4727#if defined(MBEDTLS_SSL_ALPN)
4728 {
4729 uint8_t alpn_len;
4730 const char **cur;
4731
Gilles Peskine449bd832023-01-11 14:50:10 +01004732 if ((size_t) (end - p) < 1) {
4733 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
4734 }
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02004735
4736 alpn_len = *p++;
4737
Gilles Peskine449bd832023-01-11 14:50:10 +01004738 if (alpn_len != 0 && ssl->conf->alpn_list != NULL) {
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02004739 /* alpn_chosen should point to an item in the configured list */
Gilles Peskine449bd832023-01-11 14:50:10 +01004740 for (cur = ssl->conf->alpn_list; *cur != NULL; cur++) {
4741 if (strlen(*cur) == alpn_len &&
4742 memcmp(p, cur, alpn_len) == 0) {
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02004743 ssl->alpn_chosen = *cur;
4744 break;
4745 }
4746 }
4747 }
4748
4749 /* can only happen on conf mismatch */
Gilles Peskine449bd832023-01-11 14:50:10 +01004750 if (alpn_len != 0 && ssl->alpn_chosen == NULL) {
4751 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
4752 }
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02004753
4754 p += alpn_len;
4755 }
4756#endif /* MBEDTLS_SSL_ALPN */
4757
4758 /*
Manuel Pégourié-Gonnard0eb3eac2019-07-15 11:53:51 +02004759 * Forced fields from top-level ssl_context structure
4760 *
4761 * Most of them already set to the correct value by mbedtls_ssl_init() and
4762 * mbedtls_ssl_reset(), so we only need to set the remaining ones.
4763 */
4764 ssl->state = MBEDTLS_SSL_HANDSHAKE_OVER;
Glenn Strauss60bfe602022-03-14 19:04:24 -04004765 ssl->tls_version = MBEDTLS_SSL_VERSION_TLS1_2;
Manuel Pégourié-Gonnard0eb3eac2019-07-15 11:53:51 +02004766
Hanno Becker361b10d2019-08-30 10:42:49 +01004767 /* Adjust pointers for header fields of outgoing records to
4768 * the given transform, accounting for explicit IV and CID. */
Gilles Peskine449bd832023-01-11 14:50:10 +01004769 mbedtls_ssl_update_out_pointers(ssl, ssl->transform);
Hanno Becker361b10d2019-08-30 10:42:49 +01004770
Manuel Pégourié-Gonnard0eb3eac2019-07-15 11:53:51 +02004771#if defined(MBEDTLS_SSL_PROTO_DTLS)
4772 ssl->in_epoch = 1;
4773#endif
4774
4775 /* mbedtls_ssl_reset() leaves the handshake sub-structure allocated,
4776 * which we don't want - otherwise we'd end up freeing the wrong transform
Hanno Beckerce5f5fd2020-02-05 10:47:44 +00004777 * by calling mbedtls_ssl_handshake_wrapup_free_hs_transform()
4778 * inappropriately. */
Gilles Peskine449bd832023-01-11 14:50:10 +01004779 if (ssl->handshake != NULL) {
4780 mbedtls_ssl_handshake_free(ssl);
4781 mbedtls_free(ssl->handshake);
Manuel Pégourié-Gonnard0eb3eac2019-07-15 11:53:51 +02004782 ssl->handshake = NULL;
4783 }
4784
4785 /*
Manuel Pégourié-Gonnard4c90e852019-07-11 10:58:10 +02004786 * Done - should have consumed entire buffer
4787 */
Gilles Peskine449bd832023-01-11 14:50:10 +01004788 if (p != end) {
4789 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
4790 }
Manuel Pégourié-Gonnardac87e282019-05-28 13:02:16 +02004791
Gilles Peskine449bd832023-01-11 14:50:10 +01004792 return 0;
Manuel Pégourié-Gonnardac87e282019-05-28 13:02:16 +02004793}
4794
4795/*
Manuel Pégourié-Gonnardb9dfc9f2019-07-12 10:50:19 +02004796 * Deserialize context: public wrapper for error cleaning
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02004797 */
Gilles Peskine449bd832023-01-11 14:50:10 +01004798int mbedtls_ssl_context_load(mbedtls_ssl_context *context,
4799 const unsigned char *buf,
4800 size_t len)
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02004801{
Gilles Peskine449bd832023-01-11 14:50:10 +01004802 int ret = ssl_context_load(context, buf, len);
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02004803
Gilles Peskine449bd832023-01-11 14:50:10 +01004804 if (ret != 0) {
4805 mbedtls_ssl_free(context);
4806 }
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02004807
Gilles Peskine449bd832023-01-11 14:50:10 +01004808 return ret;
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02004809}
Manuel Pégourié-Gonnard5c0e3772019-07-23 16:13:17 +02004810#endif /* MBEDTLS_SSL_CONTEXT_SERIALIZATION */
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02004811
4812/*
Paul Bakker5121ce52009-01-03 21:22:43 +00004813 * Free an SSL context
4814 */
Gilles Peskine449bd832023-01-11 14:50:10 +01004815void mbedtls_ssl_free(mbedtls_ssl_context *ssl)
Paul Bakker5121ce52009-01-03 21:22:43 +00004816{
Gilles Peskine449bd832023-01-11 14:50:10 +01004817 if (ssl == NULL) {
Paul Bakkeraccaffe2014-06-26 13:37:14 +02004818 return;
Gilles Peskine449bd832023-01-11 14:50:10 +01004819 }
Paul Bakkeraccaffe2014-06-26 13:37:14 +02004820
Gilles Peskine449bd832023-01-11 14:50:10 +01004821 MBEDTLS_SSL_DEBUG_MSG(2, ("=> free"));
Paul Bakker5121ce52009-01-03 21:22:43 +00004822
Gilles Peskine449bd832023-01-11 14:50:10 +01004823 if (ssl->out_buf != NULL) {
sander-visserb8aa2072020-05-06 22:05:13 +02004824#if defined(MBEDTLS_SSL_VARIABLE_BUFFER_LENGTH)
4825 size_t out_buf_len = ssl->out_buf_len;
4826#else
4827 size_t out_buf_len = MBEDTLS_SSL_OUT_BUFFER_LEN;
4828#endif
4829
Gilles Peskine449bd832023-01-11 14:50:10 +01004830 mbedtls_platform_zeroize(ssl->out_buf, out_buf_len);
4831 mbedtls_free(ssl->out_buf);
Andrzej Kurek0afa2a12020-03-03 10:39:58 -05004832 ssl->out_buf = NULL;
Paul Bakker5121ce52009-01-03 21:22:43 +00004833 }
4834
Gilles Peskine449bd832023-01-11 14:50:10 +01004835 if (ssl->in_buf != NULL) {
sander-visserb8aa2072020-05-06 22:05:13 +02004836#if defined(MBEDTLS_SSL_VARIABLE_BUFFER_LENGTH)
4837 size_t in_buf_len = ssl->in_buf_len;
4838#else
4839 size_t in_buf_len = MBEDTLS_SSL_IN_BUFFER_LEN;
4840#endif
4841
Gilles Peskine449bd832023-01-11 14:50:10 +01004842 mbedtls_platform_zeroize(ssl->in_buf, in_buf_len);
4843 mbedtls_free(ssl->in_buf);
Andrzej Kurek0afa2a12020-03-03 10:39:58 -05004844 ssl->in_buf = NULL;
Paul Bakker5121ce52009-01-03 21:22:43 +00004845 }
4846
Gilles Peskine449bd832023-01-11 14:50:10 +01004847 if (ssl->transform) {
4848 mbedtls_ssl_transform_free(ssl->transform);
4849 mbedtls_free(ssl->transform);
Paul Bakker48916f92012-09-16 19:57:18 +00004850 }
4851
Gilles Peskine449bd832023-01-11 14:50:10 +01004852 if (ssl->handshake) {
4853 mbedtls_ssl_handshake_free(ssl);
4854 mbedtls_free(ssl->handshake);
Jerry Yu2e199812022-12-01 18:57:19 +08004855
4856#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
Gilles Peskine449bd832023-01-11 14:50:10 +01004857 mbedtls_ssl_transform_free(ssl->transform_negotiate);
4858 mbedtls_free(ssl->transform_negotiate);
Jerry Yu2e199812022-12-01 18:57:19 +08004859#endif
4860
Gilles Peskine449bd832023-01-11 14:50:10 +01004861 mbedtls_ssl_session_free(ssl->session_negotiate);
4862 mbedtls_free(ssl->session_negotiate);
Paul Bakker48916f92012-09-16 19:57:18 +00004863 }
4864
Ronald Cron6f135e12021-12-08 16:57:54 +01004865#if defined(MBEDTLS_SSL_PROTO_TLS1_3)
Gilles Peskine449bd832023-01-11 14:50:10 +01004866 mbedtls_ssl_transform_free(ssl->transform_application);
4867 mbedtls_free(ssl->transform_application);
Ronald Cron6f135e12021-12-08 16:57:54 +01004868#endif /* MBEDTLS_SSL_PROTO_TLS1_3 */
Hanno Becker3aa186f2021-08-10 09:24:19 +01004869
Gilles Peskine449bd832023-01-11 14:50:10 +01004870 if (ssl->session) {
4871 mbedtls_ssl_session_free(ssl->session);
4872 mbedtls_free(ssl->session);
Paul Bakkerc0463502013-02-14 11:19:38 +01004873 }
4874
Manuel Pégourié-Gonnard55fab2d2015-05-11 16:15:19 +02004875#if defined(MBEDTLS_X509_CRT_PARSE_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01004876 if (ssl->hostname != NULL) {
4877 mbedtls_platform_zeroize(ssl->hostname, strlen(ssl->hostname));
4878 mbedtls_free(ssl->hostname);
Paul Bakker5121ce52009-01-03 21:22:43 +00004879 }
Paul Bakker0be444a2013-08-27 21:55:01 +02004880#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00004881
Manuel Pégourié-Gonnarde057d3b2015-05-20 10:59:43 +02004882#if defined(MBEDTLS_SSL_DTLS_HELLO_VERIFY) && defined(MBEDTLS_SSL_SRV_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01004883 mbedtls_free(ssl->cli_id);
Manuel Pégourié-Gonnard43c02182014-07-22 17:32:01 +02004884#endif
4885
Gilles Peskine449bd832023-01-11 14:50:10 +01004886 MBEDTLS_SSL_DEBUG_MSG(2, ("<= free"));
Paul Bakker2da561c2009-02-05 18:00:28 +00004887
Paul Bakker86f04f42013-02-14 11:20:09 +01004888 /* Actually clear after last debug message */
Gilles Peskine449bd832023-01-11 14:50:10 +01004889 mbedtls_platform_zeroize(ssl, sizeof(mbedtls_ssl_context));
Paul Bakker5121ce52009-01-03 21:22:43 +00004890}
4891
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02004892/*
Shaun Case8b0ecbc2021-12-20 21:14:10 -08004893 * Initialize mbedtls_ssl_config
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02004894 */
Gilles Peskine449bd832023-01-11 14:50:10 +01004895void mbedtls_ssl_config_init(mbedtls_ssl_config *conf)
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02004896{
Gilles Peskine449bd832023-01-11 14:50:10 +01004897 memset(conf, 0, sizeof(mbedtls_ssl_config));
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02004898}
4899
Gilles Peskineae270bf2021-06-02 00:05:29 +02004900/* The selection should be the same as mbedtls_x509_crt_profile_default in
4901 * x509_crt.c, plus Montgomery curves for ECDHE. Here, the order matters:
Gilles Peskineb1940a72021-06-02 15:18:12 +02004902 * curves with a lower resource usage come first.
Gilles Peskineae270bf2021-06-02 00:05:29 +02004903 * See the documentation of mbedtls_ssl_conf_curves() for what we promise
Gilles Peskineb1940a72021-06-02 15:18:12 +02004904 * about this list.
4905 */
Brett Warrene0edc842021-08-17 09:53:13 +01004906static uint16_t ssl_preset_default_groups[] = {
Gilles Peskineae270bf2021-06-02 00:05:29 +02004907#if defined(MBEDTLS_ECP_DP_CURVE25519_ENABLED)
Brett Warrene0edc842021-08-17 09:53:13 +01004908 MBEDTLS_SSL_IANA_TLS_GROUP_X25519,
Gilles Peskineae270bf2021-06-02 00:05:29 +02004909#endif
4910#if defined(MBEDTLS_ECP_DP_SECP256R1_ENABLED)
Brett Warrene0edc842021-08-17 09:53:13 +01004911 MBEDTLS_SSL_IANA_TLS_GROUP_SECP256R1,
Gilles Peskineae270bf2021-06-02 00:05:29 +02004912#endif
Gilles Peskineb1940a72021-06-02 15:18:12 +02004913#if defined(MBEDTLS_ECP_DP_SECP384R1_ENABLED)
Brett Warrene0edc842021-08-17 09:53:13 +01004914 MBEDTLS_SSL_IANA_TLS_GROUP_SECP384R1,
Gilles Peskineb1940a72021-06-02 15:18:12 +02004915#endif
4916#if defined(MBEDTLS_ECP_DP_CURVE448_ENABLED)
Brett Warrene0edc842021-08-17 09:53:13 +01004917 MBEDTLS_SSL_IANA_TLS_GROUP_X448,
Gilles Peskineb1940a72021-06-02 15:18:12 +02004918#endif
4919#if defined(MBEDTLS_ECP_DP_SECP521R1_ENABLED)
Brett Warrene0edc842021-08-17 09:53:13 +01004920 MBEDTLS_SSL_IANA_TLS_GROUP_SECP521R1,
Gilles Peskineb1940a72021-06-02 15:18:12 +02004921#endif
Gilles Peskineae270bf2021-06-02 00:05:29 +02004922#if defined(MBEDTLS_ECP_DP_BP256R1_ENABLED)
Brett Warrene0edc842021-08-17 09:53:13 +01004923 MBEDTLS_SSL_IANA_TLS_GROUP_BP256R1,
Gilles Peskineae270bf2021-06-02 00:05:29 +02004924#endif
Gilles Peskineb1940a72021-06-02 15:18:12 +02004925#if defined(MBEDTLS_ECP_DP_BP384R1_ENABLED)
Brett Warrene0edc842021-08-17 09:53:13 +01004926 MBEDTLS_SSL_IANA_TLS_GROUP_BP384R1,
Gilles Peskineb1940a72021-06-02 15:18:12 +02004927#endif
4928#if defined(MBEDTLS_ECP_DP_BP512R1_ENABLED)
Brett Warrene0edc842021-08-17 09:53:13 +01004929 MBEDTLS_SSL_IANA_TLS_GROUP_BP512R1,
Gilles Peskineb1940a72021-06-02 15:18:12 +02004930#endif
Brett Warrene0edc842021-08-17 09:53:13 +01004931 MBEDTLS_SSL_IANA_TLS_GROUP_NONE
Gilles Peskineae270bf2021-06-02 00:05:29 +02004932};
Gilles Peskineae270bf2021-06-02 00:05:29 +02004933
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02004934static int ssl_preset_suiteb_ciphersuites[] = {
4935 MBEDTLS_TLS_ECDHE_ECDSA_WITH_AES_128_GCM_SHA256,
4936 MBEDTLS_TLS_ECDHE_ECDSA_WITH_AES_256_GCM_SHA384,
4937 0
4938};
4939
Ronald Crone68ab4f2022-10-05 12:46:29 +02004940#if defined(MBEDTLS_SSL_HANDSHAKE_WITH_CERT_ENABLED)
Hanno Becker9c6aa7b2021-08-10 13:50:43 +01004941
Jerry Yu909df7b2022-01-22 11:56:27 +08004942/* NOTICE:
Jerry Yu0b994b82022-01-25 17:22:12 +08004943 * For ssl_preset_*_sig_algs and ssl_tls12_preset_*_sig_algs, the following
Jerry Yu370e1462022-01-25 10:36:53 +08004944 * rules SHOULD be upheld.
4945 * - No duplicate entries.
4946 * - But if there is a good reason, do not change the order of the algorithms.
Jerry Yu09a99fc2022-07-28 14:22:17 +08004947 * - ssl_tls12_preset* is for TLS 1.2 use only.
Jerry Yu370e1462022-01-25 10:36:53 +08004948 * - ssl_preset_* is for TLS 1.3 only or hybrid TLS 1.3/1.2 handshakes.
Jerry Yu1a8b4812022-01-20 17:56:50 +08004949 */
Hanno Becker9c6aa7b2021-08-10 13:50:43 +01004950static uint16_t ssl_preset_default_sig_algs[] = {
Jerry Yu1a8b4812022-01-20 17:56:50 +08004951
Andrzej Kurek25f27152022-08-17 16:09:31 -04004952#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 +08004953 defined(MBEDTLS_ECP_DP_SECP256R1_ENABLED)
4954 MBEDTLS_TLS1_3_SIG_ECDSA_SECP256R1_SHA256,
Andrzej Kurekcccb0442022-08-19 03:42:11 -04004955#endif /* MBEDTLS_ECDSA_C && MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA &&
Jerry Yued5e9f42022-01-26 11:21:34 +08004956 MBEDTLS_ECP_DP_SECP256R1_ENABLED */
Jerry Yu909df7b2022-01-22 11:56:27 +08004957
Andrzej Kurek25f27152022-08-17 16:09:31 -04004958#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 +08004959 defined(MBEDTLS_ECP_DP_SECP384R1_ENABLED)
4960 MBEDTLS_TLS1_3_SIG_ECDSA_SECP384R1_SHA384,
Andrzej Kurekcccb0442022-08-19 03:42:11 -04004961#endif /* MBEDTLS_ECDSA_C && MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA&&
Jerry Yu909df7b2022-01-22 11:56:27 +08004962 MBEDTLS_ECP_DP_SECP384R1_ENABLED */
4963
Andrzej Kurek25f27152022-08-17 16:09:31 -04004964#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 +08004965 defined(MBEDTLS_ECP_DP_SECP521R1_ENABLED)
4966 MBEDTLS_TLS1_3_SIG_ECDSA_SECP521R1_SHA512,
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 Yued5e9f42022-01-26 11:21:34 +08004968 MBEDTLS_ECP_DP_SECP521R1_ENABLED */
Jerry Yu909df7b2022-01-22 11:56:27 +08004969
Gilles Peskine449bd832023-01-11 14:50:10 +01004970#if defined(MBEDTLS_X509_RSASSA_PSS_SUPPORT) && \
4971 defined(MBEDTLS_HAS_ALG_SHA_512_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Jerry Yu9bb3ee42022-06-23 10:16:33 +08004972 MBEDTLS_TLS1_3_SIG_RSA_PSS_RSAE_SHA512,
Gilles Peskine449bd832023-01-11 14:50:10 +01004973#endif \
4974 /* 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 +08004975
Gilles Peskine449bd832023-01-11 14:50:10 +01004976#if defined(MBEDTLS_X509_RSASSA_PSS_SUPPORT) && \
4977 defined(MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Jerry Yu9bb3ee42022-06-23 10:16:33 +08004978 MBEDTLS_TLS1_3_SIG_RSA_PSS_RSAE_SHA384,
Gilles Peskine449bd832023-01-11 14:50:10 +01004979#endif \
4980 /* 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 +08004981
Gilles Peskine449bd832023-01-11 14:50:10 +01004982#if defined(MBEDTLS_X509_RSASSA_PSS_SUPPORT) && \
4983 defined(MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Jerry Yu9bb3ee42022-06-23 10:16:33 +08004984 MBEDTLS_TLS1_3_SIG_RSA_PSS_RSAE_SHA256,
Gilles Peskine449bd832023-01-11 14:50:10 +01004985#endif \
4986 /* 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 +08004987
Andrzej Kurek25f27152022-08-17 16:09:31 -04004988#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 +08004989 MBEDTLS_TLS1_3_SIG_RSA_PKCS1_SHA512,
4990#endif /* MBEDTLS_RSA_C && MBEDTLS_SHA512_C */
4991
Andrzej Kurek25f27152022-08-17 16:09:31 -04004992#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 +08004993 MBEDTLS_TLS1_3_SIG_RSA_PKCS1_SHA384,
4994#endif /* MBEDTLS_RSA_C && MBEDTLS_SHA384_C */
4995
Andrzej Kurek25f27152022-08-17 16:09:31 -04004996#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 +08004997 MBEDTLS_TLS1_3_SIG_RSA_PKCS1_SHA256,
4998#endif /* MBEDTLS_RSA_C && MBEDTLS_SHA256_C */
4999
Gabor Mezei15b95a62022-05-09 16:37:58 +02005000 MBEDTLS_TLS_SIG_NONE
Jerry Yu909df7b2022-01-22 11:56:27 +08005001};
5002
5003/* NOTICE: see above */
5004#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
5005static uint16_t ssl_tls12_preset_default_sig_algs[] = {
Andrzej Kurek25f27152022-08-17 16:09:31 -04005006#if defined(MBEDTLS_HAS_ALG_SHA_512_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Gabor Mezeic1051b62022-05-10 13:13:58 +02005007#if defined(MBEDTLS_ECDSA_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01005008 MBEDTLS_SSL_TLS12_SIG_AND_HASH_ALG(MBEDTLS_SSL_SIG_ECDSA, MBEDTLS_SSL_HASH_SHA512),
Jerry Yu713013f2022-01-17 18:16:35 +08005009#endif
Jerry Yu09a99fc2022-07-28 14:22:17 +08005010#if defined(MBEDTLS_X509_RSASSA_PSS_SUPPORT)
5011 MBEDTLS_TLS1_3_SIG_RSA_PSS_RSAE_SHA512,
5012#endif /* MBEDTLS_X509_RSASSA_PSS_SUPPORT */
Gabor Mezeic1051b62022-05-10 13:13:58 +02005013#if defined(MBEDTLS_RSA_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01005014 MBEDTLS_SSL_TLS12_SIG_AND_HASH_ALG(MBEDTLS_SSL_SIG_RSA, MBEDTLS_SSL_HASH_SHA512),
Gabor Mezeic1051b62022-05-10 13:13:58 +02005015#endif
Andrzej Kurekcccb0442022-08-19 03:42:11 -04005016#endif /* MBEDTLS_HAS_ALG_SHA_512_VIA_MD_OR_PSA_BASED_ON_USE_PSA*/
Andrzej Kurek25f27152022-08-17 16:09:31 -04005017#if defined(MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Gabor Mezeic1051b62022-05-10 13:13:58 +02005018#if defined(MBEDTLS_ECDSA_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01005019 MBEDTLS_SSL_TLS12_SIG_AND_HASH_ALG(MBEDTLS_SSL_SIG_ECDSA, MBEDTLS_SSL_HASH_SHA384),
Jerry Yu11f0a9c2022-01-12 18:43:08 +08005020#endif
Jerry Yu09a99fc2022-07-28 14:22:17 +08005021#if defined(MBEDTLS_X509_RSASSA_PSS_SUPPORT)
5022 MBEDTLS_TLS1_3_SIG_RSA_PSS_RSAE_SHA384,
5023#endif /* MBEDTLS_X509_RSASSA_PSS_SUPPORT */
Gabor Mezeic1051b62022-05-10 13:13:58 +02005024#if defined(MBEDTLS_RSA_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01005025 MBEDTLS_SSL_TLS12_SIG_AND_HASH_ALG(MBEDTLS_SSL_SIG_RSA, MBEDTLS_SSL_HASH_SHA384),
Gabor Mezeic1051b62022-05-10 13:13:58 +02005026#endif
Andrzej Kurekcccb0442022-08-19 03:42:11 -04005027#endif /* MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA*/
Andrzej Kurek25f27152022-08-17 16:09:31 -04005028#if defined(MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Gabor Mezeic1051b62022-05-10 13:13:58 +02005029#if defined(MBEDTLS_ECDSA_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01005030 MBEDTLS_SSL_TLS12_SIG_AND_HASH_ALG(MBEDTLS_SSL_SIG_ECDSA, MBEDTLS_SSL_HASH_SHA256),
Jerry Yu11f0a9c2022-01-12 18:43:08 +08005031#endif
Jerry Yu09a99fc2022-07-28 14:22:17 +08005032#if defined(MBEDTLS_X509_RSASSA_PSS_SUPPORT)
5033 MBEDTLS_TLS1_3_SIG_RSA_PSS_RSAE_SHA256,
5034#endif /* MBEDTLS_X509_RSASSA_PSS_SUPPORT */
Gabor Mezeic1051b62022-05-10 13:13:58 +02005035#if defined(MBEDTLS_RSA_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01005036 MBEDTLS_SSL_TLS12_SIG_AND_HASH_ALG(MBEDTLS_SSL_SIG_RSA, MBEDTLS_SSL_HASH_SHA256),
Gabor Mezeic1051b62022-05-10 13:13:58 +02005037#endif
Andrzej Kurekcccb0442022-08-19 03:42:11 -04005038#endif /* MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA*/
Gabor Mezei15b95a62022-05-09 16:37:58 +02005039 MBEDTLS_TLS_SIG_NONE
Jerry Yu909df7b2022-01-22 11:56:27 +08005040};
5041#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
5042/* NOTICE: see above */
5043static uint16_t ssl_preset_suiteb_sig_algs[] = {
5044
Andrzej Kurek25f27152022-08-17 16:09:31 -04005045#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 +08005046 defined(MBEDTLS_ECP_DP_SECP256R1_ENABLED)
5047 MBEDTLS_TLS1_3_SIG_ECDSA_SECP256R1_SHA256,
Andrzej Kurekcccb0442022-08-19 03:42:11 -04005048#endif /* MBEDTLS_ECDSA_C && MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA&&
Jerry Yu909df7b2022-01-22 11:56:27 +08005049 MBEDTLS_ECP_DP_SECP256R1_ENABLED */
5050
Andrzej Kurek25f27152022-08-17 16:09:31 -04005051#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 +08005052 defined(MBEDTLS_ECP_DP_SECP384R1_ENABLED)
5053 MBEDTLS_TLS1_3_SIG_ECDSA_SECP384R1_SHA384,
Andrzej Kurekcccb0442022-08-19 03:42:11 -04005054#endif /* MBEDTLS_ECDSA_C && MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA&&
Jerry Yu53037892022-01-25 11:02:06 +08005055 MBEDTLS_ECP_DP_SECP384R1_ENABLED */
Jerry Yu909df7b2022-01-22 11:56:27 +08005056
Gilles Peskine449bd832023-01-11 14:50:10 +01005057#if defined(MBEDTLS_X509_RSASSA_PSS_SUPPORT) && \
5058 defined(MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Jerry Yu909df7b2022-01-22 11:56:27 +08005059 MBEDTLS_TLS1_3_SIG_RSA_PSS_RSAE_SHA256,
Gilles Peskine449bd832023-01-11 14:50:10 +01005060#endif \
5061 /* 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 +08005062
Andrzej Kurek25f27152022-08-17 16:09:31 -04005063#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 +08005064 MBEDTLS_TLS1_3_SIG_RSA_PKCS1_SHA256,
Andrzej Kurekcccb0442022-08-19 03:42:11 -04005065#endif /* MBEDTLS_RSA_C && MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA*/
Jerry Yu53037892022-01-25 11:02:06 +08005066
Gabor Mezei15b95a62022-05-09 16:37:58 +02005067 MBEDTLS_TLS_SIG_NONE
Jerry Yu713013f2022-01-17 18:16:35 +08005068};
Jerry Yu6106fdc2022-01-12 16:36:14 +08005069
Jerry Yu909df7b2022-01-22 11:56:27 +08005070/* NOTICE: see above */
5071#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
5072static uint16_t ssl_tls12_preset_suiteb_sig_algs[] = {
Andrzej Kurek25f27152022-08-17 16:09:31 -04005073#if defined(MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Gabor Mezeic1051b62022-05-10 13:13:58 +02005074#if defined(MBEDTLS_ECDSA_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01005075 MBEDTLS_SSL_TLS12_SIG_AND_HASH_ALG(MBEDTLS_SSL_SIG_ECDSA, MBEDTLS_SSL_HASH_SHA256),
Jerry Yu713013f2022-01-17 18:16:35 +08005076#endif
Gabor Mezeic1051b62022-05-10 13:13:58 +02005077#if defined(MBEDTLS_RSA_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01005078 MBEDTLS_SSL_TLS12_SIG_AND_HASH_ALG(MBEDTLS_SSL_SIG_RSA, MBEDTLS_SSL_HASH_SHA256),
Gabor Mezeic1051b62022-05-10 13:13:58 +02005079#endif
Andrzej Kurekcccb0442022-08-19 03:42:11 -04005080#endif /* MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA*/
Andrzej Kurek25f27152022-08-17 16:09:31 -04005081#if defined(MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Gabor Mezeic1051b62022-05-10 13:13:58 +02005082#if defined(MBEDTLS_ECDSA_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01005083 MBEDTLS_SSL_TLS12_SIG_AND_HASH_ALG(MBEDTLS_SSL_SIG_ECDSA, MBEDTLS_SSL_HASH_SHA384),
Jerry Yu18c833e2022-01-25 10:55:47 +08005084#endif
Gabor Mezeic1051b62022-05-10 13:13:58 +02005085#if defined(MBEDTLS_RSA_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01005086 MBEDTLS_SSL_TLS12_SIG_AND_HASH_ALG(MBEDTLS_SSL_SIG_RSA, MBEDTLS_SSL_HASH_SHA384),
Gabor Mezeic1051b62022-05-10 13:13:58 +02005087#endif
Andrzej Kurekcccb0442022-08-19 03:42:11 -04005088#endif /* MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA*/
Gabor Mezei15b95a62022-05-09 16:37:58 +02005089 MBEDTLS_TLS_SIG_NONE
Hanno Becker9c6aa7b2021-08-10 13:50:43 +01005090};
Jerry Yu909df7b2022-01-22 11:56:27 +08005091#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
5092
Ronald Crone68ab4f2022-10-05 12:46:29 +02005093#endif /* MBEDTLS_SSL_HANDSHAKE_WITH_CERT_ENABLED */
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02005094
Brett Warrene0edc842021-08-17 09:53:13 +01005095static uint16_t ssl_preset_suiteb_groups[] = {
Jaeden Amerod4311042019-06-03 08:27:16 +01005096#if defined(MBEDTLS_ECP_DP_SECP256R1_ENABLED)
Brett Warrene0edc842021-08-17 09:53:13 +01005097 MBEDTLS_SSL_IANA_TLS_GROUP_SECP256R1,
Jaeden Amerod4311042019-06-03 08:27:16 +01005098#endif
5099#if defined(MBEDTLS_ECP_DP_SECP384R1_ENABLED)
Brett Warrene0edc842021-08-17 09:53:13 +01005100 MBEDTLS_SSL_IANA_TLS_GROUP_SECP384R1,
Jaeden Amerod4311042019-06-03 08:27:16 +01005101#endif
Brett Warrene0edc842021-08-17 09:53:13 +01005102 MBEDTLS_SSL_IANA_TLS_GROUP_NONE
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02005103};
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02005104
Ronald Crone68ab4f2022-10-05 12:46:29 +02005105#if defined(MBEDTLS_DEBUG_C) && defined(MBEDTLS_SSL_HANDSHAKE_WITH_CERT_ENABLED)
Jerry Yu909df7b2022-01-22 11:56:27 +08005106/* Function for checking `ssl_preset_*_sig_algs` and `ssl_tls12_preset_*_sig_algs`
Jerry Yu370e1462022-01-25 10:36:53 +08005107 * to make sure there are no duplicated signature algorithm entries. */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02005108MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01005109static int ssl_check_no_sig_alg_duplication(uint16_t *sig_algs)
Jerry Yu1a8b4812022-01-20 17:56:50 +08005110{
5111 size_t i, j;
5112 int ret = 0;
Jerry Yu909df7b2022-01-22 11:56:27 +08005113
Gilles Peskine449bd832023-01-11 14:50:10 +01005114 for (i = 0; sig_algs[i] != MBEDTLS_TLS_SIG_NONE; i++) {
5115 for (j = 0; j < i; j++) {
5116 if (sig_algs[i] != sig_algs[j]) {
Jerry Yuf377d642022-01-25 10:43:59 +08005117 continue;
Gilles Peskine449bd832023-01-11 14:50:10 +01005118 }
5119 mbedtls_printf(" entry(%04x,%" MBEDTLS_PRINTF_SIZET
5120 ") is duplicated at %" MBEDTLS_PRINTF_SIZET "\n",
5121 sig_algs[i], j, i);
Jerry Yuf377d642022-01-25 10:43:59 +08005122 ret = -1;
Jerry Yu1a8b4812022-01-20 17:56:50 +08005123 }
5124 }
Gilles Peskine449bd832023-01-11 14:50:10 +01005125 return ret;
Jerry Yu1a8b4812022-01-20 17:56:50 +08005126}
5127
Ronald Crone68ab4f2022-10-05 12:46:29 +02005128#endif /* MBEDTLS_DEBUG_C && MBEDTLS_SSL_HANDSHAKE_WITH_CERT_ENABLED */
Jerry Yu1a8b4812022-01-20 17:56:50 +08005129
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02005130/*
Tillmann Karras588ad502015-09-25 04:27:22 +02005131 * Load default in mbedtls_ssl_config
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02005132 */
Gilles Peskine449bd832023-01-11 14:50:10 +01005133int mbedtls_ssl_config_defaults(mbedtls_ssl_config *conf,
5134 int endpoint, int transport, int preset)
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02005135{
Manuel Pégourié-Gonnard8b431fb2015-05-11 12:54:52 +02005136#if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_SSL_SRV_C)
Janos Follath865b3eb2019-12-16 11:46:15 +00005137 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnard8b431fb2015-05-11 12:54:52 +02005138#endif
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02005139
Ronald Crone68ab4f2022-10-05 12:46:29 +02005140#if defined(MBEDTLS_DEBUG_C) && defined(MBEDTLS_SSL_HANDSHAKE_WITH_CERT_ENABLED)
Gilles Peskine449bd832023-01-11 14:50:10 +01005141 if (ssl_check_no_sig_alg_duplication(ssl_preset_suiteb_sig_algs)) {
5142 mbedtls_printf("ssl_preset_suiteb_sig_algs has duplicated entries\n");
5143 return MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Jerry Yu1a8b4812022-01-20 17:56:50 +08005144 }
5145
Gilles Peskine449bd832023-01-11 14:50:10 +01005146 if (ssl_check_no_sig_alg_duplication(ssl_preset_default_sig_algs)) {
5147 mbedtls_printf("ssl_preset_default_sig_algs has duplicated entries\n");
5148 return MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Jerry Yu1a8b4812022-01-20 17:56:50 +08005149 }
Jerry Yu909df7b2022-01-22 11:56:27 +08005150
5151#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
Gilles Peskine449bd832023-01-11 14:50:10 +01005152 if (ssl_check_no_sig_alg_duplication(ssl_tls12_preset_suiteb_sig_algs)) {
5153 mbedtls_printf("ssl_tls12_preset_suiteb_sig_algs has duplicated entries\n");
5154 return MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Jerry Yu909df7b2022-01-22 11:56:27 +08005155 }
5156
Gilles Peskine449bd832023-01-11 14:50:10 +01005157 if (ssl_check_no_sig_alg_duplication(ssl_tls12_preset_default_sig_algs)) {
5158 mbedtls_printf("ssl_tls12_preset_default_sig_algs has duplicated entries\n");
5159 return MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Jerry Yu909df7b2022-01-22 11:56:27 +08005160 }
5161#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Ronald Crone68ab4f2022-10-05 12:46:29 +02005162#endif /* MBEDTLS_DEBUG_C && MBEDTLS_SSL_HANDSHAKE_WITH_CERT_ENABLED */
Jerry Yu1a8b4812022-01-20 17:56:50 +08005163
Manuel Pégourié-Gonnard0de074f2015-05-14 12:58:01 +02005164 /* Use the functions here so that they are covered in tests,
5165 * but otherwise access member directly for efficiency */
Gilles Peskine449bd832023-01-11 14:50:10 +01005166 mbedtls_ssl_conf_endpoint(conf, endpoint);
5167 mbedtls_ssl_conf_transport(conf, transport);
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02005168
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02005169 /*
5170 * Things that are common to all presets
5171 */
Manuel Pégourié-Gonnard419d5ae2015-05-04 19:32:36 +02005172#if defined(MBEDTLS_SSL_CLI_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01005173 if (endpoint == MBEDTLS_SSL_IS_CLIENT) {
Manuel Pégourié-Gonnard419d5ae2015-05-04 19:32:36 +02005174 conf->authmode = MBEDTLS_SSL_VERIFY_REQUIRED;
5175#if defined(MBEDTLS_SSL_SESSION_TICKETS)
5176 conf->session_tickets = MBEDTLS_SSL_SESSION_TICKETS_ENABLED;
5177#endif
5178 }
5179#endif
5180
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02005181#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
5182 conf->encrypt_then_mac = MBEDTLS_SSL_ETM_ENABLED;
5183#endif
5184
5185#if defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET)
5186 conf->extended_ms = MBEDTLS_SSL_EXTENDED_MS_ENABLED;
5187#endif
5188
Manuel Pégourié-Gonnarde057d3b2015-05-20 10:59:43 +02005189#if defined(MBEDTLS_SSL_DTLS_HELLO_VERIFY) && defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02005190 conf->f_cookie_write = ssl_cookie_write_dummy;
5191 conf->f_cookie_check = ssl_cookie_check_dummy;
5192#endif
5193
5194#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
5195 conf->anti_replay = MBEDTLS_SSL_ANTI_REPLAY_ENABLED;
5196#endif
5197
Janos Follath088ce432017-04-10 12:42:31 +01005198#if defined(MBEDTLS_SSL_SRV_C)
5199 conf->cert_req_ca_list = MBEDTLS_SSL_CERT_REQ_CA_LIST_ENABLED;
TRodziewicz3946f792021-06-14 12:11:18 +02005200 conf->respect_cli_pref = MBEDTLS_SSL_SRV_CIPHERSUITE_ORDER_SERVER;
Janos Follath088ce432017-04-10 12:42:31 +01005201#endif
5202
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02005203#if defined(MBEDTLS_SSL_PROTO_DTLS)
5204 conf->hs_timeout_min = MBEDTLS_SSL_DTLS_TIMEOUT_DFL_MIN;
5205 conf->hs_timeout_max = MBEDTLS_SSL_DTLS_TIMEOUT_DFL_MAX;
5206#endif
5207
5208#if defined(MBEDTLS_SSL_RENEGOTIATION)
5209 conf->renego_max_records = MBEDTLS_SSL_RENEGO_MAX_RECORDS_DEFAULT;
Gilles Peskine449bd832023-01-11 14:50:10 +01005210 memset(conf->renego_period, 0x00, 2);
5211 memset(conf->renego_period + 2, 0xFF, 6);
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02005212#endif
5213
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02005214#if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_SSL_SRV_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01005215 if (endpoint == MBEDTLS_SSL_IS_SERVER) {
Hanno Beckere2defad2021-07-24 05:59:17 +01005216 const unsigned char dhm_p[] =
5217 MBEDTLS_DHM_RFC3526_MODP_2048_P_BIN;
5218 const unsigned char dhm_g[] =
5219 MBEDTLS_DHM_RFC3526_MODP_2048_G_BIN;
Hanno Becker00d0a682017-10-04 13:14:29 +01005220
Gilles Peskine449bd832023-01-11 14:50:10 +01005221 if ((ret = mbedtls_ssl_conf_dh_param_bin(conf,
5222 dhm_p, sizeof(dhm_p),
5223 dhm_g, sizeof(dhm_g))) != 0) {
5224 return ret;
Hanno Beckere2defad2021-07-24 05:59:17 +01005225 }
5226 }
Manuel Pégourié-Gonnardbd990d62015-06-11 14:49:42 +02005227#endif
5228
Ronald Cron6f135e12021-12-08 16:57:54 +01005229#if defined(MBEDTLS_SSL_PROTO_TLS1_3)
Jerry Yu6ee56aa2022-12-06 17:47:22 +08005230
5231#if defined(MBEDTLS_SSL_EARLY_DATA)
Gilles Peskine449bd832023-01-11 14:50:10 +01005232 mbedtls_ssl_tls13_conf_early_data(conf, MBEDTLS_SSL_EARLY_DATA_DISABLED);
Jerry Yu6ee56aa2022-12-06 17:47:22 +08005233#if defined(MBEDTLS_SSL_SRV_C)
5234 mbedtls_ssl_tls13_conf_max_early_data_size(
Gilles Peskine449bd832023-01-11 14:50:10 +01005235 conf, MBEDTLS_SSL_MAX_EARLY_DATA_SIZE);
Jerry Yu6ee56aa2022-12-06 17:47:22 +08005236#endif
5237#endif /* MBEDTLS_SSL_EARLY_DATA */
5238
Jerry Yud0766ec2022-09-22 10:46:57 +08005239#if defined(MBEDTLS_SSL_SRV_C) && defined(MBEDTLS_SSL_SESSION_TICKETS)
Jerry Yu1ad7ace2022-08-09 13:28:39 +08005240 mbedtls_ssl_conf_new_session_tickets(
Gilles Peskine449bd832023-01-11 14:50:10 +01005241 conf, MBEDTLS_SSL_TLS1_3_DEFAULT_NEW_SESSION_TICKETS);
Jerry Yu1ad7ace2022-08-09 13:28:39 +08005242#endif
Hanno Becker71f1ed62021-07-24 06:01:47 +01005243 /*
5244 * Allow all TLS 1.3 key exchange modes by default.
5245 */
Xiaofei Bai746f9482021-11-12 08:53:56 +00005246 conf->tls13_kex_modes = MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_ALL;
Ronald Cron6f135e12021-12-08 16:57:54 +01005247#endif /* MBEDTLS_SSL_PROTO_TLS1_3 */
Hanno Becker71f1ed62021-07-24 06:01:47 +01005248
Gilles Peskine449bd832023-01-11 14:50:10 +01005249 if (transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM) {
Glenn Strauss2dfcea22022-03-14 17:26:42 -04005250#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
XiaokangQian4d3a6042022-04-21 13:46:17 +00005251 conf->min_tls_version = MBEDTLS_SSL_VERSION_TLS1_2;
XiaokangQian060d8672022-04-21 09:24:56 +00005252 conf->max_tls_version = MBEDTLS_SSL_VERSION_TLS1_2;
XiaokangQian4d3a6042022-04-21 13:46:17 +00005253#else
Gilles Peskine449bd832023-01-11 14:50:10 +01005254 return MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
XiaokangQian060d8672022-04-21 09:24:56 +00005255#endif
Gilles Peskine449bd832023-01-11 14:50:10 +01005256 } else {
XiaokangQian4d3a6042022-04-21 13:46:17 +00005257#if defined(MBEDTLS_SSL_PROTO_TLS1_2) && defined(MBEDTLS_SSL_PROTO_TLS1_3)
Gilles Peskine449bd832023-01-11 14:50:10 +01005258 if (endpoint == MBEDTLS_SSL_IS_CLIENT) {
XiaokangQian4d3a6042022-04-21 13:46:17 +00005259 conf->min_tls_version = MBEDTLS_SSL_VERSION_TLS1_2;
5260 conf->max_tls_version = MBEDTLS_SSL_VERSION_TLS1_3;
Gilles Peskine449bd832023-01-11 14:50:10 +01005261 } else {
5262 /* Hybrid TLS 1.2 / 1.3 is not supported on server side yet */
XiaokangQian4d3a6042022-04-21 13:46:17 +00005263 conf->min_tls_version = MBEDTLS_SSL_VERSION_TLS1_2;
5264 conf->max_tls_version = MBEDTLS_SSL_VERSION_TLS1_2;
5265 }
5266#elif defined(MBEDTLS_SSL_PROTO_TLS1_3)
5267 conf->min_tls_version = MBEDTLS_SSL_VERSION_TLS1_3;
5268 conf->max_tls_version = MBEDTLS_SSL_VERSION_TLS1_3;
5269#elif defined(MBEDTLS_SSL_PROTO_TLS1_2)
5270 conf->min_tls_version = MBEDTLS_SSL_VERSION_TLS1_2;
5271 conf->max_tls_version = MBEDTLS_SSL_VERSION_TLS1_2;
5272#else
Gilles Peskine449bd832023-01-11 14:50:10 +01005273 return MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
XiaokangQian4d3a6042022-04-21 13:46:17 +00005274#endif
5275 }
Glenn Strauss2dfcea22022-03-14 17:26:42 -04005276
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02005277 /*
5278 * Preset-specific defaults
5279 */
Gilles Peskine449bd832023-01-11 14:50:10 +01005280 switch (preset) {
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02005281 /*
5282 * NSA Suite B
5283 */
5284 case MBEDTLS_SSL_PRESET_SUITEB:
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02005285
Hanno Beckerd60b6c62021-04-29 12:04:11 +01005286 conf->ciphersuite_list = ssl_preset_suiteb_ciphersuites;
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02005287
5288#if defined(MBEDTLS_X509_CRT_PARSE_C)
5289 conf->cert_profile = &mbedtls_x509_crt_profile_suiteb;
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02005290#endif
5291
Ronald Crone68ab4f2022-10-05 12:46:29 +02005292#if defined(MBEDTLS_SSL_HANDSHAKE_WITH_CERT_ENABLED)
Jerry Yu909df7b2022-01-22 11:56:27 +08005293#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
Gilles Peskine449bd832023-01-11 14:50:10 +01005294 if (mbedtls_ssl_conf_is_tls12_only(conf)) {
Jerry Yu909df7b2022-01-22 11:56:27 +08005295 conf->sig_algs = ssl_tls12_preset_suiteb_sig_algs;
Gilles Peskine449bd832023-01-11 14:50:10 +01005296 } else
Jerry Yu909df7b2022-01-22 11:56:27 +08005297#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Gilles Peskine449bd832023-01-11 14:50:10 +01005298 conf->sig_algs = ssl_preset_suiteb_sig_algs;
Ronald Crone68ab4f2022-10-05 12:46:29 +02005299#endif /* MBEDTLS_SSL_HANDSHAKE_WITH_CERT_ENABLED */
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02005300
Brett Warrene0edc842021-08-17 09:53:13 +01005301#if defined(MBEDTLS_ECP_C) && !defined(MBEDTLS_DEPRECATED_REMOVED)
5302 conf->curve_list = NULL;
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02005303#endif
Brett Warrene0edc842021-08-17 09:53:13 +01005304 conf->group_list = ssl_preset_suiteb_groups;
Manuel Pégourié-Gonnardc98204e2015-08-11 04:21:01 +02005305 break;
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02005306
5307 /*
5308 * Default
5309 */
5310 default:
Ronald Cronf6606552022-03-15 11:23:25 +01005311
Hanno Beckerd60b6c62021-04-29 12:04:11 +01005312 conf->ciphersuite_list = mbedtls_ssl_list_ciphersuites();
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02005313
5314#if defined(MBEDTLS_X509_CRT_PARSE_C)
5315 conf->cert_profile = &mbedtls_x509_crt_profile_default;
5316#endif
5317
Ronald Crone68ab4f2022-10-05 12:46:29 +02005318#if defined(MBEDTLS_SSL_HANDSHAKE_WITH_CERT_ENABLED)
Jerry Yu909df7b2022-01-22 11:56:27 +08005319#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
Gilles Peskine449bd832023-01-11 14:50:10 +01005320 if (mbedtls_ssl_conf_is_tls12_only(conf)) {
Jerry Yu909df7b2022-01-22 11:56:27 +08005321 conf->sig_algs = ssl_tls12_preset_default_sig_algs;
Gilles Peskine449bd832023-01-11 14:50:10 +01005322 } else
Jerry Yu909df7b2022-01-22 11:56:27 +08005323#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Gilles Peskine449bd832023-01-11 14:50:10 +01005324 conf->sig_algs = ssl_preset_default_sig_algs;
Ronald Crone68ab4f2022-10-05 12:46:29 +02005325#endif /* MBEDTLS_SSL_HANDSHAKE_WITH_CERT_ENABLED */
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02005326
Brett Warrene0edc842021-08-17 09:53:13 +01005327#if defined(MBEDTLS_ECP_C) && !defined(MBEDTLS_DEPRECATED_REMOVED)
5328 conf->curve_list = NULL;
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02005329#endif
Brett Warrene0edc842021-08-17 09:53:13 +01005330 conf->group_list = ssl_preset_default_groups;
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02005331
5332#if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_SSL_CLI_C)
5333 conf->dhm_min_bitlen = 1024;
5334#endif
5335 }
5336
Gilles Peskine449bd832023-01-11 14:50:10 +01005337 return 0;
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02005338}
5339
5340/*
5341 * Free mbedtls_ssl_config
5342 */
Gilles Peskine449bd832023-01-11 14:50:10 +01005343void mbedtls_ssl_config_free(mbedtls_ssl_config *conf)
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02005344{
5345#if defined(MBEDTLS_DHM_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01005346 mbedtls_mpi_free(&conf->dhm_P);
5347 mbedtls_mpi_free(&conf->dhm_G);
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02005348#endif
5349
Ronald Cron73fe8df2022-10-05 14:31:43 +02005350#if defined(MBEDTLS_SSL_HANDSHAKE_WITH_PSK_ENABLED)
Neil Armstrong501c9322022-05-03 09:35:09 +02005351#if defined(MBEDTLS_USE_PSA_CRYPTO)
Gilles Peskine449bd832023-01-11 14:50:10 +01005352 if (!mbedtls_svc_key_id_is_null(conf->psk_opaque)) {
Neil Armstrong501c9322022-05-03 09:35:09 +02005353 conf->psk_opaque = MBEDTLS_SVC_KEY_ID_INIT;
5354 }
Neil Armstrong8ecd6682022-05-05 11:40:35 +02005355#endif /* MBEDTLS_USE_PSA_CRYPTO */
Gilles Peskine449bd832023-01-11 14:50:10 +01005356 if (conf->psk != NULL) {
5357 mbedtls_platform_zeroize(conf->psk, conf->psk_len);
5358 mbedtls_free(conf->psk);
Azim Khan27e8a122018-03-21 14:24:11 +00005359 conf->psk = NULL;
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02005360 conf->psk_len = 0;
junyeonLEE316b1622017-12-20 16:29:30 +09005361 }
5362
Gilles Peskine449bd832023-01-11 14:50:10 +01005363 if (conf->psk_identity != NULL) {
5364 mbedtls_platform_zeroize(conf->psk_identity, conf->psk_identity_len);
5365 mbedtls_free(conf->psk_identity);
Azim Khan27e8a122018-03-21 14:24:11 +00005366 conf->psk_identity = NULL;
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02005367 conf->psk_identity_len = 0;
5368 }
Ronald Cron73fe8df2022-10-05 14:31:43 +02005369#endif /* MBEDTLS_SSL_HANDSHAKE_WITH_PSK_ENABLED */
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02005370
5371#if defined(MBEDTLS_X509_CRT_PARSE_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01005372 ssl_key_cert_free(conf->key_cert);
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02005373#endif
5374
Gilles Peskine449bd832023-01-11 14:50:10 +01005375 mbedtls_platform_zeroize(conf, sizeof(mbedtls_ssl_config));
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02005376}
5377
Manuel Pégourié-Gonnard5674a972015-10-19 15:14:03 +02005378#if defined(MBEDTLS_PK_C) && \
Gilles Peskine449bd832023-01-11 14:50:10 +01005379 (defined(MBEDTLS_RSA_C) || defined(MBEDTLS_ECDSA_C))
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02005380/*
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005381 * Convert between MBEDTLS_PK_XXX and SSL_SIG_XXX
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02005382 */
Gilles Peskine449bd832023-01-11 14:50:10 +01005383unsigned char mbedtls_ssl_sig_from_pk(mbedtls_pk_context *pk)
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02005384{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005385#if defined(MBEDTLS_RSA_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01005386 if (mbedtls_pk_can_do(pk, MBEDTLS_PK_RSA)) {
5387 return MBEDTLS_SSL_SIG_RSA;
5388 }
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02005389#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005390#if defined(MBEDTLS_ECDSA_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01005391 if (mbedtls_pk_can_do(pk, MBEDTLS_PK_ECDSA)) {
5392 return MBEDTLS_SSL_SIG_ECDSA;
5393 }
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02005394#endif
Gilles Peskine449bd832023-01-11 14:50:10 +01005395 return MBEDTLS_SSL_SIG_ANON;
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02005396}
5397
Gilles Peskine449bd832023-01-11 14:50:10 +01005398unsigned char mbedtls_ssl_sig_from_pk_alg(mbedtls_pk_type_t type)
Hanno Becker7e5437a2017-04-28 17:15:26 +01005399{
Gilles Peskine449bd832023-01-11 14:50:10 +01005400 switch (type) {
Hanno Becker7e5437a2017-04-28 17:15:26 +01005401 case MBEDTLS_PK_RSA:
Gilles Peskine449bd832023-01-11 14:50:10 +01005402 return MBEDTLS_SSL_SIG_RSA;
Hanno Becker7e5437a2017-04-28 17:15:26 +01005403 case MBEDTLS_PK_ECDSA:
5404 case MBEDTLS_PK_ECKEY:
Gilles Peskine449bd832023-01-11 14:50:10 +01005405 return MBEDTLS_SSL_SIG_ECDSA;
Hanno Becker7e5437a2017-04-28 17:15:26 +01005406 default:
Gilles Peskine449bd832023-01-11 14:50:10 +01005407 return MBEDTLS_SSL_SIG_ANON;
Hanno Becker7e5437a2017-04-28 17:15:26 +01005408 }
5409}
5410
Gilles Peskine449bd832023-01-11 14:50:10 +01005411mbedtls_pk_type_t mbedtls_ssl_pk_alg_from_sig(unsigned char sig)
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02005412{
Gilles Peskine449bd832023-01-11 14:50:10 +01005413 switch (sig) {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005414#if defined(MBEDTLS_RSA_C)
5415 case MBEDTLS_SSL_SIG_RSA:
Gilles Peskine449bd832023-01-11 14:50:10 +01005416 return MBEDTLS_PK_RSA;
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02005417#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005418#if defined(MBEDTLS_ECDSA_C)
5419 case MBEDTLS_SSL_SIG_ECDSA:
Gilles Peskine449bd832023-01-11 14:50:10 +01005420 return MBEDTLS_PK_ECDSA;
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02005421#endif
5422 default:
Gilles Peskine449bd832023-01-11 14:50:10 +01005423 return MBEDTLS_PK_NONE;
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02005424 }
5425}
Manuel Pégourié-Gonnard5674a972015-10-19 15:14:03 +02005426#endif /* MBEDTLS_PK_C && ( MBEDTLS_RSA_C || MBEDTLS_ECDSA_C ) */
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02005427
Manuel Pégourié-Gonnard1a483832013-09-20 12:29:15 +02005428/*
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +02005429 * Convert from MBEDTLS_SSL_HASH_XXX to MBEDTLS_MD_XXX
Manuel Pégourié-Gonnard1a483832013-09-20 12:29:15 +02005430 */
Gilles Peskine449bd832023-01-11 14:50:10 +01005431mbedtls_md_type_t mbedtls_ssl_md_alg_from_hash(unsigned char hash)
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02005432{
Gilles Peskine449bd832023-01-11 14:50:10 +01005433 switch (hash) {
Andrzej Kurek25f27152022-08-17 16:09:31 -04005434#if defined(MBEDTLS_HAS_ALG_MD5_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005435 case MBEDTLS_SSL_HASH_MD5:
Gilles Peskine449bd832023-01-11 14:50:10 +01005436 return MBEDTLS_MD_MD5;
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02005437#endif
Andrzej Kurek25f27152022-08-17 16:09:31 -04005438#if defined(MBEDTLS_HAS_ALG_SHA_1_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005439 case MBEDTLS_SSL_HASH_SHA1:
Gilles Peskine449bd832023-01-11 14:50:10 +01005440 return MBEDTLS_MD_SHA1;
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02005441#endif
Andrzej Kurek25f27152022-08-17 16:09:31 -04005442#if defined(MBEDTLS_HAS_ALG_SHA_224_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005443 case MBEDTLS_SSL_HASH_SHA224:
Gilles Peskine449bd832023-01-11 14:50:10 +01005444 return MBEDTLS_MD_SHA224;
Mateusz Starzyke3c48b42021-04-19 16:46:28 +02005445#endif
Andrzej Kurek25f27152022-08-17 16:09:31 -04005446#if defined(MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005447 case MBEDTLS_SSL_HASH_SHA256:
Gilles Peskine449bd832023-01-11 14:50:10 +01005448 return MBEDTLS_MD_SHA256;
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02005449#endif
Andrzej Kurek25f27152022-08-17 16:09:31 -04005450#if defined(MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005451 case MBEDTLS_SSL_HASH_SHA384:
Gilles Peskine449bd832023-01-11 14:50:10 +01005452 return MBEDTLS_MD_SHA384;
Mateusz Starzyk3352a532021-04-06 14:28:22 +02005453#endif
Andrzej Kurek25f27152022-08-17 16:09:31 -04005454#if defined(MBEDTLS_HAS_ALG_SHA_512_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005455 case MBEDTLS_SSL_HASH_SHA512:
Gilles Peskine449bd832023-01-11 14:50:10 +01005456 return MBEDTLS_MD_SHA512;
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02005457#endif
5458 default:
Gilles Peskine449bd832023-01-11 14:50:10 +01005459 return MBEDTLS_MD_NONE;
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02005460 }
5461}
5462
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +02005463/*
5464 * Convert from MBEDTLS_MD_XXX to MBEDTLS_SSL_HASH_XXX
5465 */
Gilles Peskine449bd832023-01-11 14:50:10 +01005466unsigned char mbedtls_ssl_hash_from_md_alg(int md)
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +02005467{
Gilles Peskine449bd832023-01-11 14:50:10 +01005468 switch (md) {
Andrzej Kurek25f27152022-08-17 16:09:31 -04005469#if defined(MBEDTLS_HAS_ALG_MD5_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +02005470 case MBEDTLS_MD_MD5:
Gilles Peskine449bd832023-01-11 14:50:10 +01005471 return MBEDTLS_SSL_HASH_MD5;
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +02005472#endif
Andrzej Kurek25f27152022-08-17 16:09:31 -04005473#if defined(MBEDTLS_HAS_ALG_SHA_1_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +02005474 case MBEDTLS_MD_SHA1:
Gilles Peskine449bd832023-01-11 14:50:10 +01005475 return MBEDTLS_SSL_HASH_SHA1;
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +02005476#endif
Andrzej Kurek25f27152022-08-17 16:09:31 -04005477#if defined(MBEDTLS_HAS_ALG_SHA_224_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +02005478 case MBEDTLS_MD_SHA224:
Gilles Peskine449bd832023-01-11 14:50:10 +01005479 return MBEDTLS_SSL_HASH_SHA224;
Mateusz Starzyke3c48b42021-04-19 16:46:28 +02005480#endif
Andrzej Kurek25f27152022-08-17 16:09:31 -04005481#if defined(MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +02005482 case MBEDTLS_MD_SHA256:
Gilles Peskine449bd832023-01-11 14:50:10 +01005483 return MBEDTLS_SSL_HASH_SHA256;
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +02005484#endif
Andrzej Kurek25f27152022-08-17 16:09:31 -04005485#if defined(MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +02005486 case MBEDTLS_MD_SHA384:
Gilles Peskine449bd832023-01-11 14:50:10 +01005487 return MBEDTLS_SSL_HASH_SHA384;
Mateusz Starzyk3352a532021-04-06 14:28:22 +02005488#endif
Andrzej Kurek25f27152022-08-17 16:09:31 -04005489#if defined(MBEDTLS_HAS_ALG_SHA_512_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +02005490 case MBEDTLS_MD_SHA512:
Gilles Peskine449bd832023-01-11 14:50:10 +01005491 return MBEDTLS_SSL_HASH_SHA512;
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +02005492#endif
5493 default:
Gilles Peskine449bd832023-01-11 14:50:10 +01005494 return MBEDTLS_SSL_HASH_NONE;
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +02005495 }
5496}
5497
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01005498/*
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +02005499 * Check if a curve proposed by the peer is in our list.
Manuel Pégourié-Gonnard9d412d82015-06-17 12:10:46 +02005500 * Return 0 if we're willing to use it, -1 otherwise.
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01005501 */
Gilles Peskine449bd832023-01-11 14:50:10 +01005502int mbedtls_ssl_check_curve_tls_id(const mbedtls_ssl_context *ssl, uint16_t tls_id)
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01005503{
Gilles Peskine449bd832023-01-11 14:50:10 +01005504 const uint16_t *group_list = mbedtls_ssl_get_groups(ssl);
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01005505
Gilles Peskine449bd832023-01-11 14:50:10 +01005506 if (group_list == NULL) {
5507 return -1;
Brett Warrene0edc842021-08-17 09:53:13 +01005508 }
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01005509
Gilles Peskine449bd832023-01-11 14:50:10 +01005510 for (; *group_list != 0; group_list++) {
5511 if (*group_list == tls_id) {
5512 return 0;
5513 }
5514 }
5515
5516 return -1;
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01005517}
Manuel Pégourié-Gonnard0d63b842022-01-18 13:10:56 +01005518
5519#if defined(MBEDTLS_ECP_C)
5520/*
5521 * Same as mbedtls_ssl_check_curve_tls_id() but with a mbedtls_ecp_group_id.
5522 */
Gilles Peskine449bd832023-01-11 14:50:10 +01005523int mbedtls_ssl_check_curve(const mbedtls_ssl_context *ssl, mbedtls_ecp_group_id grp_id)
Manuel Pégourié-Gonnard0d63b842022-01-18 13:10:56 +01005524{
Gilles Peskine449bd832023-01-11 14:50:10 +01005525 uint16_t tls_id = mbedtls_ssl_get_tls_id_from_ecp_group_id(grp_id);
Leonid Rozenboim19e59732022-08-08 16:52:38 -07005526
Gilles Peskine449bd832023-01-11 14:50:10 +01005527 if (tls_id == 0) {
Leonid Rozenboim19e59732022-08-08 16:52:38 -07005528 return -1;
Gilles Peskine449bd832023-01-11 14:50:10 +01005529 }
Leonid Rozenboim19e59732022-08-08 16:52:38 -07005530
Gilles Peskine449bd832023-01-11 14:50:10 +01005531 return mbedtls_ssl_check_curve_tls_id(ssl, tls_id);
Manuel Pégourié-Gonnard0d63b842022-01-18 13:10:56 +01005532}
Manuel Pégourié-Gonnardb541da62015-06-17 11:43:30 +02005533#endif /* MBEDTLS_ECP_C */
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02005534
Gilles Peskine449bd832023-01-11 14:50:10 +01005535#if defined(MBEDTLS_DEBUG_C)
Valerio Setti67419f02023-01-04 16:12:42 +01005536#define EC_NAME(_name_) _name_
5537#else
5538#define EC_NAME(_name_) NULL
5539#endif
5540
Valerio Setti18c9fed2022-12-30 17:44:24 +01005541static const struct {
5542 uint16_t tls_id;
5543 mbedtls_ecp_group_id ecp_group_id;
5544 psa_ecc_family_t psa_family;
5545 uint16_t bits;
Gilles Peskine449bd832023-01-11 14:50:10 +01005546 const char *name;
Valerio Setti18c9fed2022-12-30 17:44:24 +01005547} tls_id_match_table[] =
5548{
5549#if defined(MBEDTLS_ECP_DP_SECP521R1_ENABLED) || defined(PSA_WANT_ECC_SECP_R1_521)
Gilles Peskine449bd832023-01-11 14:50:10 +01005550 { 25, MBEDTLS_ECP_DP_SECP521R1, PSA_ECC_FAMILY_SECP_R1, 521, EC_NAME("secp521r1") },
Valerio Setti18c9fed2022-12-30 17:44:24 +01005551#endif
5552#if defined(MBEDTLS_ECP_DP_BP512R1_ENABLED) || defined(PSA_WANT_ECC_BRAINPOOL_P_R1_512)
Gilles Peskine449bd832023-01-11 14:50:10 +01005553 { 28, MBEDTLS_ECP_DP_BP512R1, PSA_ECC_FAMILY_BRAINPOOL_P_R1, 512, EC_NAME("brainpoolP512r1") },
Valerio Setti18c9fed2022-12-30 17:44:24 +01005554#endif
Valerio Setti67419f02023-01-04 16:12:42 +01005555#if defined(MBEDTLS_ECP_DP_SECP384R1_ENABLED) || defined(PSA_WANT_ECC_SECP_R1_384)
Gilles Peskine449bd832023-01-11 14:50:10 +01005556 { 24, MBEDTLS_ECP_DP_SECP384R1, PSA_ECC_FAMILY_SECP_R1, 384, EC_NAME("secp384r1") },
Valerio Setti18c9fed2022-12-30 17:44:24 +01005557#endif
Valerio Setti67419f02023-01-04 16:12:42 +01005558#if defined(MBEDTLS_ECP_DP_BP384R1_ENABLED) || defined(PSA_WANT_ECC_BRAINPOOL_P_R1_384)
Gilles Peskine449bd832023-01-11 14:50:10 +01005559 { 27, MBEDTLS_ECP_DP_BP384R1, PSA_ECC_FAMILY_BRAINPOOL_P_R1, 384, EC_NAME("brainpoolP384r1") },
Valerio Setti18c9fed2022-12-30 17:44:24 +01005560#endif
Valerio Setti67419f02023-01-04 16:12:42 +01005561#if defined(MBEDTLS_ECP_DP_SECP256R1_ENABLED) || defined(PSA_WANT_ECC_SECP_R1_256)
Gilles Peskine449bd832023-01-11 14:50:10 +01005562 { 23, MBEDTLS_ECP_DP_SECP256R1, PSA_ECC_FAMILY_SECP_R1, 256, EC_NAME("secp256r1") },
Valerio Setti18c9fed2022-12-30 17:44:24 +01005563#endif
5564#if defined(MBEDTLS_ECP_DP_SECP256K1_ENABLED) || defined(PSA_WANT_ECC_SECP_K1_256)
Gilles Peskine449bd832023-01-11 14:50:10 +01005565 { 22, MBEDTLS_ECP_DP_SECP256K1, PSA_ECC_FAMILY_SECP_K1, 256, EC_NAME("secp256k1") },
Valerio Setti18c9fed2022-12-30 17:44:24 +01005566#endif
Valerio Setti67419f02023-01-04 16:12:42 +01005567#if defined(MBEDTLS_ECP_DP_BP256R1_ENABLED) || defined(PSA_WANT_ECC_BRAINPOOL_P_R1_256)
Gilles Peskine449bd832023-01-11 14:50:10 +01005568 { 26, MBEDTLS_ECP_DP_BP256R1, PSA_ECC_FAMILY_BRAINPOOL_P_R1, 256, EC_NAME("brainpoolP256r1") },
Valerio Setti18c9fed2022-12-30 17:44:24 +01005569#endif
5570#if defined(MBEDTLS_ECP_DP_SECP224R1_ENABLED) || defined(PSA_WANT_ECC_SECP_R1_224)
Gilles Peskine449bd832023-01-11 14:50:10 +01005571 { 21, MBEDTLS_ECP_DP_SECP224R1, PSA_ECC_FAMILY_SECP_R1, 224, EC_NAME("secp224r1") },
Valerio Setti18c9fed2022-12-30 17:44:24 +01005572#endif
5573#if defined(MBEDTLS_ECP_DP_SECP224K1_ENABLED) || defined(PSA_WANT_ECC_SECP_K1_224)
Gilles Peskine449bd832023-01-11 14:50:10 +01005574 { 20, MBEDTLS_ECP_DP_SECP224K1, PSA_ECC_FAMILY_SECP_K1, 224, EC_NAME("secp224k1") },
Valerio Setti18c9fed2022-12-30 17:44:24 +01005575#endif
5576#if defined(MBEDTLS_ECP_DP_SECP192R1_ENABLED) || defined(PSA_WANT_ECC_SECP_R1_192)
Gilles Peskine449bd832023-01-11 14:50:10 +01005577 { 19, MBEDTLS_ECP_DP_SECP192R1, PSA_ECC_FAMILY_SECP_R1, 192, EC_NAME("secp192r1") },
Valerio Setti18c9fed2022-12-30 17:44:24 +01005578#endif
5579#if defined(MBEDTLS_ECP_DP_SECP192K1_ENABLED) || defined(PSA_WANT_ECC_SECP_K1_192)
Gilles Peskine449bd832023-01-11 14:50:10 +01005580 { 18, MBEDTLS_ECP_DP_SECP192K1, PSA_ECC_FAMILY_SECP_K1, 192, EC_NAME("secp192k1") },
Valerio Setti18c9fed2022-12-30 17:44:24 +01005581#endif
5582#if defined(MBEDTLS_ECP_DP_CURVE25519_ENABLED) || defined(PSA_WANT_ECC_MONTGOMERY_255)
Gilles Peskine449bd832023-01-11 14:50:10 +01005583 { 29, MBEDTLS_ECP_DP_CURVE25519, PSA_ECC_FAMILY_MONTGOMERY, 255, EC_NAME("x25519") },
Valerio Setti18c9fed2022-12-30 17:44:24 +01005584#endif
5585#if defined(MBEDTLS_ECP_DP_CURVE448_ENABLED) || defined(PSA_WANT_ECC_MONTGOMERY_448)
Gilles Peskine449bd832023-01-11 14:50:10 +01005586 { 30, MBEDTLS_ECP_DP_CURVE448, PSA_ECC_FAMILY_MONTGOMERY, 448, EC_NAME("x448") },
Valerio Setti18c9fed2022-12-30 17:44:24 +01005587#endif
5588 { 0, MBEDTLS_ECP_DP_NONE, 0, 0, NULL },
5589};
5590
Gilles Peskine449bd832023-01-11 14:50:10 +01005591int mbedtls_ssl_get_psa_curve_info_from_tls_id(uint16_t tls_id,
5592 psa_ecc_family_t *family,
5593 size_t *bits)
Valerio Setti18c9fed2022-12-30 17:44:24 +01005594{
Gilles Peskine449bd832023-01-11 14:50:10 +01005595 for (int i = 0; tls_id_match_table[i].tls_id != 0; i++) {
5596 if (tls_id_match_table[i].tls_id == tls_id) {
5597 if (family != NULL) {
Valerio Setti18c9fed2022-12-30 17:44:24 +01005598 *family = tls_id_match_table[i].psa_family;
Gilles Peskine449bd832023-01-11 14:50:10 +01005599 }
5600 if (bits != NULL) {
Valerio Setti18c9fed2022-12-30 17:44:24 +01005601 *bits = tls_id_match_table[i].bits;
Gilles Peskine449bd832023-01-11 14:50:10 +01005602 }
Valerio Setti18c9fed2022-12-30 17:44:24 +01005603 return PSA_SUCCESS;
5604 }
5605 }
5606
5607 return PSA_ERROR_NOT_SUPPORTED;
5608}
5609
Gilles Peskine449bd832023-01-11 14:50:10 +01005610mbedtls_ecp_group_id mbedtls_ssl_get_ecp_group_id_from_tls_id(uint16_t tls_id)
Valerio Setti18c9fed2022-12-30 17:44:24 +01005611{
Gilles Peskine449bd832023-01-11 14:50:10 +01005612 for (int i = 0; tls_id_match_table[i].tls_id != 0; i++) {
5613 if (tls_id_match_table[i].tls_id == tls_id) {
Valerio Setti18c9fed2022-12-30 17:44:24 +01005614 return tls_id_match_table[i].ecp_group_id;
Gilles Peskine449bd832023-01-11 14:50:10 +01005615 }
Valerio Setti18c9fed2022-12-30 17:44:24 +01005616 }
5617
5618 return MBEDTLS_ECP_DP_NONE;
5619}
5620
Gilles Peskine449bd832023-01-11 14:50:10 +01005621uint16_t mbedtls_ssl_get_tls_id_from_ecp_group_id(mbedtls_ecp_group_id grp_id)
Valerio Setti18c9fed2022-12-30 17:44:24 +01005622{
Gilles Peskine449bd832023-01-11 14:50:10 +01005623 for (int i = 0; tls_id_match_table[i].ecp_group_id != MBEDTLS_ECP_DP_NONE;
5624 i++) {
5625 if (tls_id_match_table[i].ecp_group_id == grp_id) {
Valerio Setti18c9fed2022-12-30 17:44:24 +01005626 return tls_id_match_table[i].tls_id;
Gilles Peskine449bd832023-01-11 14:50:10 +01005627 }
Valerio Setti18c9fed2022-12-30 17:44:24 +01005628 }
5629
5630 return 0;
5631}
5632
Valerio Setti67419f02023-01-04 16:12:42 +01005633#if defined(MBEDTLS_DEBUG_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01005634const char *mbedtls_ssl_get_curve_name_from_tls_id(uint16_t tls_id)
Valerio Setti18c9fed2022-12-30 17:44:24 +01005635{
Gilles Peskine449bd832023-01-11 14:50:10 +01005636 for (int i = 0; tls_id_match_table[i].tls_id != 0; i++) {
5637 if (tls_id_match_table[i].tls_id == tls_id) {
Valerio Setti18c9fed2022-12-30 17:44:24 +01005638 return tls_id_match_table[i].name;
Gilles Peskine449bd832023-01-11 14:50:10 +01005639 }
Valerio Setti18c9fed2022-12-30 17:44:24 +01005640 }
5641
5642 return NULL;
5643}
Valerio Setti67419f02023-01-04 16:12:42 +01005644#endif
Valerio Setti18c9fed2022-12-30 17:44:24 +01005645
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005646#if defined(MBEDTLS_X509_CRT_PARSE_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01005647int mbedtls_ssl_check_cert_usage(const mbedtls_x509_crt *cert,
5648 const mbedtls_ssl_ciphersuite_t *ciphersuite,
5649 int cert_endpoint,
5650 uint32_t *flags)
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02005651{
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +01005652 int ret = 0;
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02005653 int usage = 0;
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +02005654 const char *ext_oid;
5655 size_t ext_len;
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +02005656
Gilles Peskine449bd832023-01-11 14:50:10 +01005657 if (cert_endpoint == MBEDTLS_SSL_IS_SERVER) {
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02005658 /* Server part of the key exchange */
Gilles Peskine449bd832023-01-11 14:50:10 +01005659 switch (ciphersuite->key_exchange) {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005660 case MBEDTLS_KEY_EXCHANGE_RSA:
5661 case MBEDTLS_KEY_EXCHANGE_RSA_PSK:
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +01005662 usage = MBEDTLS_X509_KU_KEY_ENCIPHERMENT;
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02005663 break;
5664
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005665 case MBEDTLS_KEY_EXCHANGE_DHE_RSA:
5666 case MBEDTLS_KEY_EXCHANGE_ECDHE_RSA:
5667 case MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA:
5668 usage = MBEDTLS_X509_KU_DIGITAL_SIGNATURE;
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_ECDH_RSA:
5672 case MBEDTLS_KEY_EXCHANGE_ECDH_ECDSA:
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +01005673 usage = MBEDTLS_X509_KU_KEY_AGREEMENT;
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02005674 break;
5675
5676 /* Don't use default: we want warnings when adding new values */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005677 case MBEDTLS_KEY_EXCHANGE_NONE:
5678 case MBEDTLS_KEY_EXCHANGE_PSK:
5679 case MBEDTLS_KEY_EXCHANGE_DHE_PSK:
5680 case MBEDTLS_KEY_EXCHANGE_ECDHE_PSK:
Manuel Pégourié-Gonnard557535d2015-09-15 17:53:32 +02005681 case MBEDTLS_KEY_EXCHANGE_ECJPAKE:
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02005682 usage = 0;
5683 }
Gilles Peskine449bd832023-01-11 14:50:10 +01005684 } else {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005685 /* Client auth: we only implement rsa_sign and mbedtls_ecdsa_sign for now */
5686 usage = MBEDTLS_X509_KU_DIGITAL_SIGNATURE;
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02005687 }
5688
Gilles Peskine449bd832023-01-11 14:50:10 +01005689 if (mbedtls_x509_crt_check_key_usage(cert, usage) != 0) {
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +01005690 *flags |= MBEDTLS_X509_BADCERT_KEY_USAGE;
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +01005691 ret = -1;
5692 }
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02005693
Gilles Peskine449bd832023-01-11 14:50:10 +01005694 if (cert_endpoint == MBEDTLS_SSL_IS_SERVER) {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005695 ext_oid = MBEDTLS_OID_SERVER_AUTH;
Gilles Peskine449bd832023-01-11 14:50:10 +01005696 ext_len = MBEDTLS_OID_SIZE(MBEDTLS_OID_SERVER_AUTH);
5697 } else {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005698 ext_oid = MBEDTLS_OID_CLIENT_AUTH;
Gilles Peskine449bd832023-01-11 14:50:10 +01005699 ext_len = MBEDTLS_OID_SIZE(MBEDTLS_OID_CLIENT_AUTH);
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +02005700 }
5701
Gilles Peskine449bd832023-01-11 14:50:10 +01005702 if (mbedtls_x509_crt_check_extended_key_usage(cert, ext_oid, ext_len) != 0) {
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +01005703 *flags |= MBEDTLS_X509_BADCERT_EXT_KEY_USAGE;
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +01005704 ret = -1;
5705 }
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +02005706
Gilles Peskine449bd832023-01-11 14:50:10 +01005707 return ret;
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02005708}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005709#endif /* MBEDTLS_X509_CRT_PARSE_C */
Manuel Pégourié-Gonnard3a306b92014-04-29 15:11:17 +02005710
Jerry Yu148165c2021-09-24 23:20:59 +08005711#if defined(MBEDTLS_USE_PSA_CRYPTO)
Gilles Peskine449bd832023-01-11 14:50:10 +01005712int mbedtls_ssl_get_handshake_transcript(mbedtls_ssl_context *ssl,
5713 const mbedtls_md_type_t md,
5714 unsigned char *dst,
5715 size_t dst_len,
5716 size_t *olen)
Jerry Yu148165c2021-09-24 23:20:59 +08005717{
Ronald Cronf6893e12022-01-07 22:09:01 +01005718 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
5719 psa_hash_operation_t *hash_operation_to_clone;
5720 psa_hash_operation_t hash_operation = psa_hash_operation_init();
5721
Jerry Yu148165c2021-09-24 23:20:59 +08005722 *olen = 0;
Ronald Cronf6893e12022-01-07 22:09:01 +01005723
Gilles Peskine449bd832023-01-11 14:50:10 +01005724 switch (md) {
Andrzej Kurek25f27152022-08-17 16:09:31 -04005725#if defined(MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Gilles Peskine449bd832023-01-11 14:50:10 +01005726 case MBEDTLS_MD_SHA384:
5727 hash_operation_to_clone = &ssl->handshake->fin_sha384_psa;
5728 break;
Ronald Cronf6893e12022-01-07 22:09:01 +01005729#endif
5730
Andrzej Kurek25f27152022-08-17 16:09:31 -04005731#if defined(MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Gilles Peskine449bd832023-01-11 14:50:10 +01005732 case MBEDTLS_MD_SHA256:
5733 hash_operation_to_clone = &ssl->handshake->fin_sha256_psa;
5734 break;
Ronald Cronf6893e12022-01-07 22:09:01 +01005735#endif
5736
Gilles Peskine449bd832023-01-11 14:50:10 +01005737 default:
5738 goto exit;
5739 }
5740
5741 status = psa_hash_clone(hash_operation_to_clone, &hash_operation);
5742 if (status != PSA_SUCCESS) {
Ronald Cronf6893e12022-01-07 22:09:01 +01005743 goto exit;
5744 }
5745
Gilles Peskine449bd832023-01-11 14:50:10 +01005746 status = psa_hash_finish(&hash_operation, dst, dst_len, olen);
5747 if (status != PSA_SUCCESS) {
Ronald Cronf6893e12022-01-07 22:09:01 +01005748 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01005749 }
Ronald Cronf6893e12022-01-07 22:09:01 +01005750
5751exit:
Andrzej Kurekeabeb302022-10-17 07:52:51 -04005752#if !defined(MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA) && \
5753 !defined(MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
5754 (void) ssl;
5755#endif
Gilles Peskine449bd832023-01-11 14:50:10 +01005756 return psa_ssl_status_to_mbedtls(status);
Jerry Yu148165c2021-09-24 23:20:59 +08005757}
5758#else /* MBEDTLS_USE_PSA_CRYPTO */
5759
Andrzej Kurek25f27152022-08-17 16:09:31 -04005760#if defined(MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02005761MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01005762static int ssl_get_handshake_transcript_sha384(mbedtls_ssl_context *ssl,
5763 unsigned char *dst,
5764 size_t dst_len,
5765 size_t *olen)
Jerry Yu24c0ec32021-09-09 14:21:07 +08005766{
Jerry Yu24c0ec32021-09-09 14:21:07 +08005767 int ret;
5768 mbedtls_sha512_context sha512;
5769
Gilles Peskine449bd832023-01-11 14:50:10 +01005770 if (dst_len < 48) {
5771 return MBEDTLS_ERR_SSL_INTERNAL_ERROR;
5772 }
Jerry Yu24c0ec32021-09-09 14:21:07 +08005773
Gilles Peskine449bd832023-01-11 14:50:10 +01005774 mbedtls_sha512_init(&sha512);
5775 mbedtls_sha512_clone(&sha512, &ssl->handshake->fin_sha384);
Jerry Yu24c0ec32021-09-09 14:21:07 +08005776
Gilles Peskine449bd832023-01-11 14:50:10 +01005777 if ((ret = mbedtls_sha512_finish(&sha512, dst)) != 0) {
5778 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_sha512_finish", ret);
Jerry Yu24c0ec32021-09-09 14:21:07 +08005779 goto exit;
5780 }
5781
5782 *olen = 48;
5783
5784exit:
5785
Gilles Peskine449bd832023-01-11 14:50:10 +01005786 mbedtls_sha512_free(&sha512);
5787 return ret;
Jerry Yu24c0ec32021-09-09 14:21:07 +08005788}
Andrzej Kurek25f27152022-08-17 16:09:31 -04005789#endif /* MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA */
Jerry Yu24c0ec32021-09-09 14:21:07 +08005790
Andrzej Kurek25f27152022-08-17 16:09:31 -04005791#if defined(MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02005792MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01005793static int ssl_get_handshake_transcript_sha256(mbedtls_ssl_context *ssl,
5794 unsigned char *dst,
5795 size_t dst_len,
5796 size_t *olen)
Jerry Yu24c0ec32021-09-09 14:21:07 +08005797{
Jerry Yu24c0ec32021-09-09 14:21:07 +08005798 int ret;
5799 mbedtls_sha256_context sha256;
5800
Gilles Peskine449bd832023-01-11 14:50:10 +01005801 if (dst_len < 32) {
5802 return MBEDTLS_ERR_SSL_INTERNAL_ERROR;
5803 }
Jerry Yu24c0ec32021-09-09 14:21:07 +08005804
Gilles Peskine449bd832023-01-11 14:50:10 +01005805 mbedtls_sha256_init(&sha256);
5806 mbedtls_sha256_clone(&sha256, &ssl->handshake->fin_sha256);
Jerry Yuc5aef882021-12-23 20:15:02 +08005807
Gilles Peskine449bd832023-01-11 14:50:10 +01005808 if ((ret = mbedtls_sha256_finish(&sha256, dst)) != 0) {
5809 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_sha256_finish", ret);
Jerry Yu24c0ec32021-09-09 14:21:07 +08005810 goto exit;
5811 }
5812
5813 *olen = 32;
5814
5815exit:
5816
Gilles Peskine449bd832023-01-11 14:50:10 +01005817 mbedtls_sha256_free(&sha256);
5818 return ret;
Jerry Yu24c0ec32021-09-09 14:21:07 +08005819}
Andrzej Kurek25f27152022-08-17 16:09:31 -04005820#endif /* MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA */
Jerry Yu24c0ec32021-09-09 14:21:07 +08005821
Gilles Peskine449bd832023-01-11 14:50:10 +01005822int mbedtls_ssl_get_handshake_transcript(mbedtls_ssl_context *ssl,
5823 const mbedtls_md_type_t md,
5824 unsigned char *dst,
5825 size_t dst_len,
5826 size_t *olen)
Jerry Yu24c0ec32021-09-09 14:21:07 +08005827{
Gilles Peskine449bd832023-01-11 14:50:10 +01005828 switch (md) {
Jerry Yuc1ddeef2021-10-08 15:14:45 +08005829
Andrzej Kurek25f27152022-08-17 16:09:31 -04005830#if defined(MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Gilles Peskine449bd832023-01-11 14:50:10 +01005831 case MBEDTLS_MD_SHA384:
5832 return ssl_get_handshake_transcript_sha384(ssl, dst, dst_len, olen);
Andrzej Kurekcccb0442022-08-19 03:42:11 -04005833#endif /* MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA*/
Jerry Yuc1ddeef2021-10-08 15:14:45 +08005834
Andrzej Kurek25f27152022-08-17 16:09:31 -04005835#if defined(MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Gilles Peskine449bd832023-01-11 14:50:10 +01005836 case MBEDTLS_MD_SHA256:
5837 return ssl_get_handshake_transcript_sha256(ssl, dst, dst_len, olen);
Andrzej Kurekcccb0442022-08-19 03:42:11 -04005838#endif /* MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA*/
Jerry Yuc1ddeef2021-10-08 15:14:45 +08005839
Gilles Peskine449bd832023-01-11 14:50:10 +01005840 default:
Andrzej Kurek409248a2022-10-24 10:33:21 -04005841#if !defined(MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA) && \
Gilles Peskine449bd832023-01-11 14:50:10 +01005842 !defined(MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
5843 (void) ssl;
5844 (void) dst;
5845 (void) dst_len;
5846 (void) olen;
Andrzej Kurek409248a2022-10-24 10:33:21 -04005847#endif
Gilles Peskine449bd832023-01-11 14:50:10 +01005848 break;
Jerry Yuc1ddeef2021-10-08 15:14:45 +08005849 }
Gilles Peskine449bd832023-01-11 14:50:10 +01005850 return MBEDTLS_ERR_SSL_INTERNAL_ERROR;
Jerry Yu24c0ec32021-09-09 14:21:07 +08005851}
XiaokangQian647719a2021-12-07 09:16:29 +00005852
Jerry Yu148165c2021-09-24 23:20:59 +08005853#endif /* !MBEDTLS_USE_PSA_CRYPTO */
Jerry Yu24c0ec32021-09-09 14:21:07 +08005854
Ronald Crone68ab4f2022-10-05 12:46:29 +02005855#if defined(MBEDTLS_SSL_HANDSHAKE_WITH_CERT_ENABLED)
Gabor Mezei078e8032022-04-27 21:17:56 +02005856/* mbedtls_ssl_parse_sig_alg_ext()
5857 *
5858 * The `extension_data` field of signature algorithm contains a `SignatureSchemeList`
5859 * value (TLS 1.3 RFC8446):
5860 * enum {
5861 * ....
5862 * ecdsa_secp256r1_sha256( 0x0403 ),
5863 * ecdsa_secp384r1_sha384( 0x0503 ),
5864 * ecdsa_secp521r1_sha512( 0x0603 ),
5865 * ....
5866 * } SignatureScheme;
5867 *
5868 * struct {
5869 * SignatureScheme supported_signature_algorithms<2..2^16-2>;
5870 * } SignatureSchemeList;
5871 *
5872 * The `extension_data` field of signature algorithm contains a `SignatureAndHashAlgorithm`
5873 * value (TLS 1.2 RFC5246):
5874 * enum {
5875 * none(0), md5(1), sha1(2), sha224(3), sha256(4), sha384(5),
5876 * sha512(6), (255)
5877 * } HashAlgorithm;
5878 *
5879 * enum { anonymous(0), rsa(1), dsa(2), ecdsa(3), (255) }
5880 * SignatureAlgorithm;
5881 *
5882 * struct {
5883 * HashAlgorithm hash;
5884 * SignatureAlgorithm signature;
5885 * } SignatureAndHashAlgorithm;
5886 *
5887 * SignatureAndHashAlgorithm
5888 * supported_signature_algorithms<2..2^16-2>;
5889 *
5890 * The TLS 1.3 signature algorithm extension was defined to be a compatible
5891 * generalization of the TLS 1.2 signature algorithm extension.
5892 * `SignatureAndHashAlgorithm` field of TLS 1.2 can be represented by
5893 * `SignatureScheme` field of TLS 1.3
5894 *
5895 */
Gilles Peskine449bd832023-01-11 14:50:10 +01005896int mbedtls_ssl_parse_sig_alg_ext(mbedtls_ssl_context *ssl,
5897 const unsigned char *buf,
5898 const unsigned char *end)
Gabor Mezei078e8032022-04-27 21:17:56 +02005899{
5900 const unsigned char *p = buf;
5901 size_t supported_sig_algs_len = 0;
5902 const unsigned char *supported_sig_algs_end;
5903 uint16_t sig_alg;
5904 uint32_t common_idx = 0;
5905
Gilles Peskine449bd832023-01-11 14:50:10 +01005906 MBEDTLS_SSL_CHK_BUF_READ_PTR(p, end, 2);
5907 supported_sig_algs_len = MBEDTLS_GET_UINT16_BE(p, 0);
Gabor Mezei078e8032022-04-27 21:17:56 +02005908 p += 2;
5909
Gilles Peskine449bd832023-01-11 14:50:10 +01005910 memset(ssl->handshake->received_sig_algs, 0,
5911 sizeof(ssl->handshake->received_sig_algs));
Gabor Mezei078e8032022-04-27 21:17:56 +02005912
Gilles Peskine449bd832023-01-11 14:50:10 +01005913 MBEDTLS_SSL_CHK_BUF_READ_PTR(p, end, supported_sig_algs_len);
Gabor Mezei078e8032022-04-27 21:17:56 +02005914 supported_sig_algs_end = p + supported_sig_algs_len;
Gilles Peskine449bd832023-01-11 14:50:10 +01005915 while (p < supported_sig_algs_end) {
5916 MBEDTLS_SSL_CHK_BUF_READ_PTR(p, supported_sig_algs_end, 2);
5917 sig_alg = MBEDTLS_GET_UINT16_BE(p, 0);
Gabor Mezei078e8032022-04-27 21:17:56 +02005918 p += 2;
Gilles Peskine449bd832023-01-11 14:50:10 +01005919 MBEDTLS_SSL_DEBUG_MSG(4, ("received signature algorithm: 0x%x %s",
5920 sig_alg,
5921 mbedtls_ssl_sig_alg_to_str(sig_alg)));
Jerry Yu2fe6c632022-06-29 10:02:38 +08005922#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
Gilles Peskine449bd832023-01-11 14:50:10 +01005923 if (ssl->tls_version == MBEDTLS_SSL_VERSION_TLS1_2 &&
5924 (!(mbedtls_ssl_sig_alg_is_supported(ssl, sig_alg) &&
5925 mbedtls_ssl_sig_alg_is_offered(ssl, sig_alg)))) {
Gabor Mezei078e8032022-04-27 21:17:56 +02005926 continue;
Jerry Yu2fe6c632022-06-29 10:02:38 +08005927 }
5928#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Gabor Mezei078e8032022-04-27 21:17:56 +02005929
Gilles Peskine449bd832023-01-11 14:50:10 +01005930 MBEDTLS_SSL_DEBUG_MSG(4, ("valid signature algorithm: %s",
5931 mbedtls_ssl_sig_alg_to_str(sig_alg)));
Gabor Mezei078e8032022-04-27 21:17:56 +02005932
Gilles Peskine449bd832023-01-11 14:50:10 +01005933 if (common_idx + 1 < MBEDTLS_RECEIVED_SIG_ALGS_SIZE) {
Gabor Mezei078e8032022-04-27 21:17:56 +02005934 ssl->handshake->received_sig_algs[common_idx] = sig_alg;
5935 common_idx += 1;
5936 }
5937 }
5938 /* Check that we consumed all the message. */
Gilles Peskine449bd832023-01-11 14:50:10 +01005939 if (p != end) {
5940 MBEDTLS_SSL_DEBUG_MSG(1,
5941 ("Signature algorithms extension length misaligned"));
5942 MBEDTLS_SSL_PEND_FATAL_ALERT(MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR,
5943 MBEDTLS_ERR_SSL_DECODE_ERROR);
5944 return MBEDTLS_ERR_SSL_DECODE_ERROR;
Gabor Mezei078e8032022-04-27 21:17:56 +02005945 }
5946
Gilles Peskine449bd832023-01-11 14:50:10 +01005947 if (common_idx == 0) {
5948 MBEDTLS_SSL_DEBUG_MSG(3, ("no signature algorithm in common"));
5949 MBEDTLS_SSL_PEND_FATAL_ALERT(MBEDTLS_SSL_ALERT_MSG_HANDSHAKE_FAILURE,
5950 MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE);
5951 return MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE;
Gabor Mezei078e8032022-04-27 21:17:56 +02005952 }
5953
Gabor Mezei15b95a62022-05-09 16:37:58 +02005954 ssl->handshake->received_sig_algs[common_idx] = MBEDTLS_TLS_SIG_NONE;
Gilles Peskine449bd832023-01-11 14:50:10 +01005955 return 0;
Gabor Mezei078e8032022-04-27 21:17:56 +02005956}
5957
Ronald Crone68ab4f2022-10-05 12:46:29 +02005958#endif /* MBEDTLS_SSL_HANDSHAKE_WITH_CERT_ENABLED */
Gabor Mezei078e8032022-04-27 21:17:56 +02005959
Jerry Yudc7bd172022-02-17 13:44:15 +08005960#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
5961
5962#if defined(MBEDTLS_USE_PSA_CRYPTO)
5963
Gilles Peskine449bd832023-01-11 14:50:10 +01005964static psa_status_t setup_psa_key_derivation(psa_key_derivation_operation_t *derivation,
5965 mbedtls_svc_key_id_t key,
5966 psa_algorithm_t alg,
5967 const unsigned char *raw_psk, size_t raw_psk_length,
5968 const unsigned char *seed, size_t seed_length,
5969 const unsigned char *label, size_t label_length,
5970 const unsigned char *other_secret,
5971 size_t other_secret_length,
5972 size_t capacity)
Jerry Yudc7bd172022-02-17 13:44:15 +08005973{
5974 psa_status_t status;
5975
Gilles Peskine449bd832023-01-11 14:50:10 +01005976 status = psa_key_derivation_setup(derivation, alg);
5977 if (status != PSA_SUCCESS) {
5978 return status;
5979 }
Jerry Yudc7bd172022-02-17 13:44:15 +08005980
Gilles Peskine449bd832023-01-11 14:50:10 +01005981 if (PSA_ALG_IS_TLS12_PRF(alg) || PSA_ALG_IS_TLS12_PSK_TO_MS(alg)) {
5982 status = psa_key_derivation_input_bytes(derivation,
5983 PSA_KEY_DERIVATION_INPUT_SEED,
5984 seed, seed_length);
5985 if (status != PSA_SUCCESS) {
5986 return status;
Przemek Stekiel1f027032022-04-05 17:12:11 +02005987 }
5988
Gilles Peskine449bd832023-01-11 14:50:10 +01005989 if (other_secret != NULL) {
5990 status = psa_key_derivation_input_bytes(derivation,
5991 PSA_KEY_DERIVATION_INPUT_OTHER_SECRET,
5992 other_secret, other_secret_length);
5993 if (status != PSA_SUCCESS) {
5994 return status;
5995 }
5996 }
5997
5998 if (mbedtls_svc_key_id_is_null(key)) {
Jerry Yudc7bd172022-02-17 13:44:15 +08005999 status = psa_key_derivation_input_bytes(
6000 derivation, PSA_KEY_DERIVATION_INPUT_SECRET,
Gilles Peskine449bd832023-01-11 14:50:10 +01006001 raw_psk, raw_psk_length);
6002 } else {
Jerry Yudc7bd172022-02-17 13:44:15 +08006003 status = psa_key_derivation_input_key(
Gilles Peskine449bd832023-01-11 14:50:10 +01006004 derivation, PSA_KEY_DERIVATION_INPUT_SECRET, key);
Jerry Yudc7bd172022-02-17 13:44:15 +08006005 }
Gilles Peskine449bd832023-01-11 14:50:10 +01006006 if (status != PSA_SUCCESS) {
6007 return status;
6008 }
Jerry Yudc7bd172022-02-17 13:44:15 +08006009
Gilles Peskine449bd832023-01-11 14:50:10 +01006010 status = psa_key_derivation_input_bytes(derivation,
6011 PSA_KEY_DERIVATION_INPUT_LABEL,
6012 label, label_length);
6013 if (status != PSA_SUCCESS) {
6014 return status;
6015 }
6016 } else {
6017 return PSA_ERROR_NOT_SUPPORTED;
Jerry Yudc7bd172022-02-17 13:44:15 +08006018 }
6019
Gilles Peskine449bd832023-01-11 14:50:10 +01006020 status = psa_key_derivation_set_capacity(derivation, capacity);
6021 if (status != PSA_SUCCESS) {
6022 return status;
6023 }
Jerry Yudc7bd172022-02-17 13:44:15 +08006024
Gilles Peskine449bd832023-01-11 14:50:10 +01006025 return PSA_SUCCESS;
Jerry Yudc7bd172022-02-17 13:44:15 +08006026}
6027
Andrzej Kurek57d10632022-10-24 10:32:01 -04006028#if defined(PSA_WANT_ALG_SHA_384) || \
6029 defined(PSA_WANT_ALG_SHA_256)
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02006030MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01006031static int tls_prf_generic(mbedtls_md_type_t md_type,
6032 const unsigned char *secret, size_t slen,
6033 const char *label,
6034 const unsigned char *random, size_t rlen,
6035 unsigned char *dstbuf, size_t dlen)
Jerry Yudc7bd172022-02-17 13:44:15 +08006036{
6037 psa_status_t status;
6038 psa_algorithm_t alg;
6039 mbedtls_svc_key_id_t master_key = MBEDTLS_SVC_KEY_ID_INIT;
6040 psa_key_derivation_operation_t derivation =
6041 PSA_KEY_DERIVATION_OPERATION_INIT;
6042
Gilles Peskine449bd832023-01-11 14:50:10 +01006043 if (md_type == MBEDTLS_MD_SHA384) {
Jerry Yudc7bd172022-02-17 13:44:15 +08006044 alg = PSA_ALG_TLS12_PRF(PSA_ALG_SHA_384);
Gilles Peskine449bd832023-01-11 14:50:10 +01006045 } else {
Jerry Yudc7bd172022-02-17 13:44:15 +08006046 alg = PSA_ALG_TLS12_PRF(PSA_ALG_SHA_256);
Gilles Peskine449bd832023-01-11 14:50:10 +01006047 }
Jerry Yudc7bd172022-02-17 13:44:15 +08006048
6049 /* Normally a "secret" should be long enough to be impossible to
6050 * find by brute force, and in particular should not be empty. But
6051 * this PRF is also used to derive an IV, in particular in EAP-TLS,
6052 * and for this use case it makes sense to have a 0-length "secret".
6053 * Since the key API doesn't allow importing a key of length 0,
6054 * keep master_key=0, which setup_psa_key_derivation() understands
6055 * to mean a 0-length "secret" input. */
Gilles Peskine449bd832023-01-11 14:50:10 +01006056 if (slen != 0) {
Jerry Yudc7bd172022-02-17 13:44:15 +08006057 psa_key_attributes_t key_attributes = psa_key_attributes_init();
Gilles Peskine449bd832023-01-11 14:50:10 +01006058 psa_set_key_usage_flags(&key_attributes, PSA_KEY_USAGE_DERIVE);
6059 psa_set_key_algorithm(&key_attributes, alg);
6060 psa_set_key_type(&key_attributes, PSA_KEY_TYPE_DERIVE);
Jerry Yudc7bd172022-02-17 13:44:15 +08006061
Gilles Peskine449bd832023-01-11 14:50:10 +01006062 status = psa_import_key(&key_attributes, secret, slen, &master_key);
6063 if (status != PSA_SUCCESS) {
6064 return MBEDTLS_ERR_SSL_HW_ACCEL_FAILED;
6065 }
Jerry Yudc7bd172022-02-17 13:44:15 +08006066 }
6067
Gilles Peskine449bd832023-01-11 14:50:10 +01006068 status = setup_psa_key_derivation(&derivation,
6069 master_key, alg,
6070 NULL, 0,
6071 random, rlen,
6072 (unsigned char const *) label,
6073 (size_t) strlen(label),
6074 NULL, 0,
6075 dlen);
6076 if (status != PSA_SUCCESS) {
6077 psa_key_derivation_abort(&derivation);
6078 psa_destroy_key(master_key);
6079 return MBEDTLS_ERR_SSL_HW_ACCEL_FAILED;
Jerry Yudc7bd172022-02-17 13:44:15 +08006080 }
6081
Gilles Peskine449bd832023-01-11 14:50:10 +01006082 status = psa_key_derivation_output_bytes(&derivation, dstbuf, dlen);
6083 if (status != PSA_SUCCESS) {
6084 psa_key_derivation_abort(&derivation);
6085 psa_destroy_key(master_key);
6086 return MBEDTLS_ERR_SSL_HW_ACCEL_FAILED;
Jerry Yudc7bd172022-02-17 13:44:15 +08006087 }
6088
Gilles Peskine449bd832023-01-11 14:50:10 +01006089 status = psa_key_derivation_abort(&derivation);
6090 if (status != PSA_SUCCESS) {
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 if (!mbedtls_svc_key_id_is_null(master_key)) {
6096 status = psa_destroy_key(master_key);
6097 }
6098 if (status != PSA_SUCCESS) {
6099 return MBEDTLS_ERR_SSL_HW_ACCEL_FAILED;
6100 }
Jerry Yudc7bd172022-02-17 13:44:15 +08006101
Gilles Peskine449bd832023-01-11 14:50:10 +01006102 return 0;
Jerry Yudc7bd172022-02-17 13:44:15 +08006103}
Andrzej Kurek57d10632022-10-24 10:32:01 -04006104#endif /* PSA_WANT_ALG_SHA_256 || PSA_WANT_ALG_SHA_384 */
Jerry Yudc7bd172022-02-17 13:44:15 +08006105#else /* MBEDTLS_USE_PSA_CRYPTO */
6106
Andrzej Kurek57d10632022-10-24 10:32:01 -04006107#if defined(MBEDTLS_MD_C) && \
Gilles Peskine449bd832023-01-11 14:50:10 +01006108 (defined(MBEDTLS_SHA256_C) || \
6109 defined(MBEDTLS_SHA384_C))
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02006110MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01006111static int tls_prf_generic(mbedtls_md_type_t md_type,
6112 const unsigned char *secret, size_t slen,
6113 const char *label,
6114 const unsigned char *random, size_t rlen,
6115 unsigned char *dstbuf, size_t dlen)
Jerry Yudc7bd172022-02-17 13:44:15 +08006116{
6117 size_t nb;
6118 size_t i, j, k, md_len;
6119 unsigned char *tmp;
6120 size_t tmp_len = 0;
6121 unsigned char h_i[MBEDTLS_MD_MAX_SIZE];
6122 const mbedtls_md_info_t *md_info;
6123 mbedtls_md_context_t md_ctx;
6124 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
6125
Gilles Peskine449bd832023-01-11 14:50:10 +01006126 mbedtls_md_init(&md_ctx);
Jerry Yudc7bd172022-02-17 13:44:15 +08006127
Gilles Peskine449bd832023-01-11 14:50:10 +01006128 if ((md_info = mbedtls_md_info_from_type(md_type)) == NULL) {
6129 return MBEDTLS_ERR_SSL_INTERNAL_ERROR;
6130 }
Jerry Yudc7bd172022-02-17 13:44:15 +08006131
Gilles Peskine449bd832023-01-11 14:50:10 +01006132 md_len = mbedtls_md_get_size(md_info);
Jerry Yudc7bd172022-02-17 13:44:15 +08006133
Gilles Peskine449bd832023-01-11 14:50:10 +01006134 tmp_len = md_len + strlen(label) + rlen;
6135 tmp = mbedtls_calloc(1, tmp_len);
6136 if (tmp == NULL) {
Jerry Yudc7bd172022-02-17 13:44:15 +08006137 ret = MBEDTLS_ERR_SSL_ALLOC_FAILED;
6138 goto exit;
6139 }
6140
Gilles Peskine449bd832023-01-11 14:50:10 +01006141 nb = strlen(label);
6142 memcpy(tmp + md_len, label, nb);
6143 memcpy(tmp + md_len + nb, random, rlen);
Jerry Yudc7bd172022-02-17 13:44:15 +08006144 nb += rlen;
6145
6146 /*
6147 * Compute P_<hash>(secret, label + random)[0..dlen]
6148 */
Gilles Peskine449bd832023-01-11 14:50:10 +01006149 if ((ret = mbedtls_md_setup(&md_ctx, md_info, 1)) != 0) {
Jerry Yudc7bd172022-02-17 13:44:15 +08006150 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01006151 }
Jerry Yudc7bd172022-02-17 13:44:15 +08006152
Gilles Peskine449bd832023-01-11 14:50:10 +01006153 ret = mbedtls_md_hmac_starts(&md_ctx, secret, slen);
6154 if (ret != 0) {
Jerry Yudc7bd172022-02-17 13:44:15 +08006155 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01006156 }
6157 ret = mbedtls_md_hmac_update(&md_ctx, tmp + md_len, nb);
6158 if (ret != 0) {
Jerry Yudc7bd172022-02-17 13:44:15 +08006159 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01006160 }
6161 ret = mbedtls_md_hmac_finish(&md_ctx, tmp);
6162 if (ret != 0) {
Jerry Yudc7bd172022-02-17 13:44:15 +08006163 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01006164 }
Jerry Yudc7bd172022-02-17 13:44:15 +08006165
Gilles Peskine449bd832023-01-11 14:50:10 +01006166 for (i = 0; i < dlen; i += md_len) {
6167 ret = mbedtls_md_hmac_reset(&md_ctx);
6168 if (ret != 0) {
Jerry Yudc7bd172022-02-17 13:44:15 +08006169 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01006170 }
6171 ret = mbedtls_md_hmac_update(&md_ctx, tmp, md_len + nb);
6172 if (ret != 0) {
Jerry Yudc7bd172022-02-17 13:44:15 +08006173 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01006174 }
6175 ret = mbedtls_md_hmac_finish(&md_ctx, h_i);
6176 if (ret != 0) {
Jerry Yudc7bd172022-02-17 13:44:15 +08006177 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01006178 }
Jerry Yudc7bd172022-02-17 13:44:15 +08006179
Gilles Peskine449bd832023-01-11 14:50:10 +01006180 ret = mbedtls_md_hmac_reset(&md_ctx);
6181 if (ret != 0) {
Jerry Yudc7bd172022-02-17 13:44:15 +08006182 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01006183 }
6184 ret = mbedtls_md_hmac_update(&md_ctx, tmp, md_len);
6185 if (ret != 0) {
Jerry Yudc7bd172022-02-17 13:44:15 +08006186 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01006187 }
6188 ret = mbedtls_md_hmac_finish(&md_ctx, tmp);
6189 if (ret != 0) {
Jerry Yudc7bd172022-02-17 13:44:15 +08006190 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01006191 }
Jerry Yudc7bd172022-02-17 13:44:15 +08006192
Gilles Peskine449bd832023-01-11 14:50:10 +01006193 k = (i + md_len > dlen) ? dlen % md_len : md_len;
Jerry Yudc7bd172022-02-17 13:44:15 +08006194
Gilles Peskine449bd832023-01-11 14:50:10 +01006195 for (j = 0; j < k; j++) {
Jerry Yudc7bd172022-02-17 13:44:15 +08006196 dstbuf[i + j] = h_i[j];
Gilles Peskine449bd832023-01-11 14:50:10 +01006197 }
Jerry Yudc7bd172022-02-17 13:44:15 +08006198 }
6199
6200exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01006201 mbedtls_md_free(&md_ctx);
Jerry Yudc7bd172022-02-17 13:44:15 +08006202
Gilles Peskine449bd832023-01-11 14:50:10 +01006203 if (tmp != NULL) {
6204 mbedtls_platform_zeroize(tmp, tmp_len);
6205 }
Dave Rodgman29b9b2b2022-11-01 16:08:14 +00006206
Gilles Peskine449bd832023-01-11 14:50:10 +01006207 mbedtls_platform_zeroize(h_i, sizeof(h_i));
Jerry Yudc7bd172022-02-17 13:44:15 +08006208
Gilles Peskine449bd832023-01-11 14:50:10 +01006209 mbedtls_free(tmp);
Jerry Yudc7bd172022-02-17 13:44:15 +08006210
Gilles Peskine449bd832023-01-11 14:50:10 +01006211 return ret;
Jerry Yudc7bd172022-02-17 13:44:15 +08006212}
Andrzej Kurek57d10632022-10-24 10:32:01 -04006213#endif /* MBEDTLS_MD_C && ( MBEDTLS_SHA256_C || MBEDTLS_SHA384_C ) */
Jerry Yudc7bd172022-02-17 13:44:15 +08006214#endif /* MBEDTLS_USE_PSA_CRYPTO */
6215
Andrzej Kurek25f27152022-08-17 16:09:31 -04006216#if defined(MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02006217MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01006218static int tls_prf_sha256(const unsigned char *secret, size_t slen,
6219 const char *label,
6220 const unsigned char *random, size_t rlen,
6221 unsigned char *dstbuf, size_t dlen)
Jerry Yudc7bd172022-02-17 13:44:15 +08006222{
Gilles Peskine449bd832023-01-11 14:50:10 +01006223 return tls_prf_generic(MBEDTLS_MD_SHA256, secret, slen,
6224 label, random, rlen, dstbuf, dlen);
Jerry Yudc7bd172022-02-17 13:44:15 +08006225}
Andrzej Kurekcccb0442022-08-19 03:42:11 -04006226#endif /* MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA*/
Jerry Yudc7bd172022-02-17 13:44:15 +08006227
Andrzej Kurek25f27152022-08-17 16:09:31 -04006228#if defined(MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02006229MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01006230static int tls_prf_sha384(const unsigned char *secret, size_t slen,
6231 const char *label,
6232 const unsigned char *random, size_t rlen,
6233 unsigned char *dstbuf, size_t dlen)
Jerry Yudc7bd172022-02-17 13:44:15 +08006234{
Gilles Peskine449bd832023-01-11 14:50:10 +01006235 return tls_prf_generic(MBEDTLS_MD_SHA384, secret, slen,
6236 label, random, rlen, dstbuf, dlen);
Jerry Yudc7bd172022-02-17 13:44:15 +08006237}
Andrzej Kurekcccb0442022-08-19 03:42:11 -04006238#endif /* MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA*/
Jerry Yudc7bd172022-02-17 13:44:15 +08006239
Jerry Yuf009d862022-02-17 14:01:37 +08006240/*
6241 * Set appropriate PRF function and other SSL / TLS1.2 functions
6242 *
6243 * Inputs:
Jerry Yuf009d862022-02-17 14:01:37 +08006244 * - hash associated with the ciphersuite (only used by TLS 1.2)
6245 *
6246 * Outputs:
6247 * - the tls_prf, calc_verify and calc_finished members of handshake structure
6248 */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02006249MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01006250static int ssl_set_handshake_prfs(mbedtls_ssl_handshake_params *handshake,
6251 mbedtls_md_type_t hash)
Jerry Yuf009d862022-02-17 14:01:37 +08006252{
Andrzej Kurek25f27152022-08-17 16:09:31 -04006253#if defined(MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Gilles Peskine449bd832023-01-11 14:50:10 +01006254 if (hash == MBEDTLS_MD_SHA384) {
Jerry Yuf009d862022-02-17 14:01:37 +08006255 handshake->tls_prf = tls_prf_sha384;
6256 handshake->calc_verify = ssl_calc_verify_tls_sha384;
6257 handshake->calc_finished = ssl_calc_finished_tls_sha384;
Gilles Peskine449bd832023-01-11 14:50:10 +01006258 } else
Jerry Yuf009d862022-02-17 14:01:37 +08006259#endif
Andrzej Kurek25f27152022-08-17 16:09:31 -04006260#if defined(MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Jerry Yuf009d862022-02-17 14:01:37 +08006261 {
Ronald Cron81591aa2022-03-07 09:05:51 +01006262 (void) hash;
Jerry Yuf009d862022-02-17 14:01:37 +08006263 handshake->tls_prf = tls_prf_sha256;
6264 handshake->calc_verify = ssl_calc_verify_tls_sha256;
6265 handshake->calc_finished = ssl_calc_finished_tls_sha256;
6266 }
Ronald Cron81591aa2022-03-07 09:05:51 +01006267#else
Jerry Yuf009d862022-02-17 14:01:37 +08006268 {
Jerry Yuf009d862022-02-17 14:01:37 +08006269 (void) handshake;
Ronald Cron81591aa2022-03-07 09:05:51 +01006270 (void) hash;
Gilles Peskine449bd832023-01-11 14:50:10 +01006271 return MBEDTLS_ERR_SSL_INTERNAL_ERROR;
Jerry Yuf009d862022-02-17 14:01:37 +08006272 }
Ronald Cron81591aa2022-03-07 09:05:51 +01006273#endif
Jerry Yuf009d862022-02-17 14:01:37 +08006274
Gilles Peskine449bd832023-01-11 14:50:10 +01006275 return 0;
Jerry Yuf009d862022-02-17 14:01:37 +08006276}
Jerry Yud6ab2352022-02-17 14:03:43 +08006277
Jerry Yu2a7b5ac2022-02-17 14:07:00 +08006278/*
6279 * Compute master secret if needed
6280 *
6281 * Parameters:
6282 * [in/out] handshake
6283 * [in] resume, premaster, extended_ms, calc_verify, tls_prf
6284 * (PSA-PSK) ciphersuite_info, psk_opaque
6285 * [out] premaster (cleared)
6286 * [out] master
6287 * [in] ssl: optionally used for debugging, EMS and PSA-PSK
6288 * debug: conf->f_dbg, conf->p_dbg
6289 * EMS: passed to calc_verify (debug + session_negotiate)
Ronald Crona25cf582022-03-07 11:10:36 +01006290 * PSA-PSA: conf
Jerry Yu2a7b5ac2022-02-17 14:07:00 +08006291 */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02006292MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01006293static int ssl_compute_master(mbedtls_ssl_handshake_params *handshake,
6294 unsigned char *master,
6295 const mbedtls_ssl_context *ssl)
Jerry Yu2a7b5ac2022-02-17 14:07:00 +08006296{
6297 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
6298
6299 /* cf. RFC 5246, Section 8.1:
6300 * "The master secret is always exactly 48 bytes in length." */
6301 size_t const master_secret_len = 48;
6302
6303#if defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET)
6304 unsigned char session_hash[48];
6305#endif /* MBEDTLS_SSL_EXTENDED_MASTER_SECRET */
6306
6307 /* The label for the KDF used for key expansion.
6308 * This is either "master secret" or "extended master secret"
6309 * depending on whether the Extended Master Secret extension
6310 * is used. */
6311 char const *lbl = "master secret";
6312
Przemek Stekielae4ed302022-04-05 17:15:55 +02006313 /* The seed for the KDF used for key expansion.
Jerry Yu2a7b5ac2022-02-17 14:07:00 +08006314 * - If the Extended Master Secret extension is not used,
6315 * this is ClientHello.Random + ServerHello.Random
6316 * (see Sect. 8.1 in RFC 5246).
6317 * - If the Extended Master Secret extension is used,
6318 * this is the transcript of the handshake so far.
6319 * (see Sect. 4 in RFC 7627). */
Przemek Stekielae4ed302022-04-05 17:15:55 +02006320 unsigned char const *seed = handshake->randbytes;
6321 size_t seed_len = 64;
Jerry Yu2a7b5ac2022-02-17 14:07:00 +08006322
6323#if !defined(MBEDTLS_DEBUG_C) && \
6324 !defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET) && \
6325 !(defined(MBEDTLS_USE_PSA_CRYPTO) && \
Gilles Peskine449bd832023-01-11 14:50:10 +01006326 defined(MBEDTLS_KEY_EXCHANGE_PSK_ENABLED))
Jerry Yu2a7b5ac2022-02-17 14:07:00 +08006327 ssl = NULL; /* make sure we don't use it except for those cases */
6328 (void) ssl;
6329#endif
6330
Gilles Peskine449bd832023-01-11 14:50:10 +01006331 if (handshake->resume != 0) {
6332 MBEDTLS_SSL_DEBUG_MSG(3, ("no premaster (session resumed)"));
6333 return 0;
Jerry Yu2a7b5ac2022-02-17 14:07:00 +08006334 }
6335
6336#if defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET)
Gilles Peskine449bd832023-01-11 14:50:10 +01006337 if (handshake->extended_ms == MBEDTLS_SSL_EXTENDED_MS_ENABLED) {
Jerry Yu2a7b5ac2022-02-17 14:07:00 +08006338 lbl = "extended master secret";
Przemek Stekielae4ed302022-04-05 17:15:55 +02006339 seed = session_hash;
Manuel Pégourié-Gonnardb8b07aa2023-02-06 00:34:21 +01006340 ret = handshake->calc_verify(ssl, session_hash, &seed_len);
6341 if (ret != 0) {
6342 MBEDTLS_SSL_DEBUG_RET(1, "calc_verify", ret);
6343 }
Jerry Yu2a7b5ac2022-02-17 14:07:00 +08006344
Gilles Peskine449bd832023-01-11 14:50:10 +01006345 MBEDTLS_SSL_DEBUG_BUF(3, "session hash for extended master secret",
6346 session_hash, seed_len);
Jerry Yu2a7b5ac2022-02-17 14:07:00 +08006347 }
Przemek Stekiel169bf0b2022-04-29 07:53:29 +02006348#endif /* MBEDTLS_SSL_EXTENDED_MASTER_SECRET */
Jerry Yu2a7b5ac2022-02-17 14:07:00 +08006349
Przemek Stekiel99114f32022-04-22 11:20:09 +02006350#if defined(MBEDTLS_USE_PSA_CRYPTO) && \
Przemek Stekiel8a4b7fd2022-04-28 09:22:22 +02006351 defined(MBEDTLS_KEY_EXCHANGE_SOME_PSK_ENABLED)
Gilles Peskine449bd832023-01-11 14:50:10 +01006352 if (mbedtls_ssl_ciphersuite_uses_psk(handshake->ciphersuite_info) == 1) {
Jerry Yu2a7b5ac2022-02-17 14:07:00 +08006353 /* Perform PSK-to-MS expansion in a single step. */
6354 psa_status_t status;
6355 psa_algorithm_t alg;
6356 mbedtls_svc_key_id_t psk;
6357 psa_key_derivation_operation_t derivation =
6358 PSA_KEY_DERIVATION_OPERATION_INIT;
6359 mbedtls_md_type_t hash_alg = handshake->ciphersuite_info->mac;
6360
Gilles Peskine449bd832023-01-11 14:50:10 +01006361 MBEDTLS_SSL_DEBUG_MSG(2, ("perform PSA-based PSK-to-MS expansion"));
Jerry Yu2a7b5ac2022-02-17 14:07:00 +08006362
Gilles Peskine449bd832023-01-11 14:50:10 +01006363 psk = mbedtls_ssl_get_opaque_psk(ssl);
Jerry Yu2a7b5ac2022-02-17 14:07:00 +08006364
Gilles Peskine449bd832023-01-11 14:50:10 +01006365 if (hash_alg == MBEDTLS_MD_SHA384) {
Jerry Yu2a7b5ac2022-02-17 14:07:00 +08006366 alg = PSA_ALG_TLS12_PSK_TO_MS(PSA_ALG_SHA_384);
Gilles Peskine449bd832023-01-11 14:50:10 +01006367 } else {
Jerry Yu2a7b5ac2022-02-17 14:07:00 +08006368 alg = PSA_ALG_TLS12_PSK_TO_MS(PSA_ALG_SHA_256);
Gilles Peskine449bd832023-01-11 14:50:10 +01006369 }
Jerry Yu2a7b5ac2022-02-17 14:07:00 +08006370
Przemek Stekiel51a1f362022-04-13 08:57:06 +02006371 size_t other_secret_len = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +01006372 unsigned char *other_secret = NULL;
Przemek Stekielc2033402022-04-05 17:19:41 +02006373
Gilles Peskine449bd832023-01-11 14:50:10 +01006374 switch (handshake->ciphersuite_info->key_exchange) {
Przemek Stekiel19b80f82022-04-14 08:29:31 +02006375 /* Provide other secret.
Przemek Stekiel51a1f362022-04-13 08:57:06 +02006376 * Other secret is stored in premaster, where first 2 bytes hold the
Przemek Stekiel19b80f82022-04-14 08:29:31 +02006377 * length of the other key.
Przemek Stekielc2033402022-04-05 17:19:41 +02006378 */
Przemek Stekiel19b80f82022-04-14 08:29:31 +02006379 case MBEDTLS_KEY_EXCHANGE_RSA_PSK:
Przemek Stekiel8abcee92022-04-28 09:16:28 +02006380 /* For RSA-PSK other key length is always 48 bytes. */
Przemek Stekiel19b80f82022-04-14 08:29:31 +02006381 other_secret_len = 48;
6382 other_secret = handshake->premaster + 2;
6383 break;
6384 case MBEDTLS_KEY_EXCHANGE_ECDHE_PSK:
Przemek Stekielb293aaa2022-04-19 12:22:38 +02006385 case MBEDTLS_KEY_EXCHANGE_DHE_PSK:
Przemek Stekiel19b80f82022-04-14 08:29:31 +02006386 other_secret_len = MBEDTLS_GET_UINT16_BE(handshake->premaster, 0);
6387 other_secret = handshake->premaster + 2;
6388 break;
6389 default:
6390 break;
Przemek Stekielc2033402022-04-05 17:19:41 +02006391 }
6392
Gilles Peskine449bd832023-01-11 14:50:10 +01006393 status = setup_psa_key_derivation(&derivation, psk, alg,
6394 ssl->conf->psk, ssl->conf->psk_len,
6395 seed, seed_len,
6396 (unsigned char const *) lbl,
6397 (size_t) strlen(lbl),
6398 other_secret, other_secret_len,
6399 master_secret_len);
6400 if (status != PSA_SUCCESS) {
6401 psa_key_derivation_abort(&derivation);
6402 return MBEDTLS_ERR_SSL_HW_ACCEL_FAILED;
Jerry Yu2a7b5ac2022-02-17 14:07:00 +08006403 }
6404
Gilles Peskine449bd832023-01-11 14:50:10 +01006405 status = psa_key_derivation_output_bytes(&derivation,
6406 master,
6407 master_secret_len);
6408 if (status != PSA_SUCCESS) {
6409 psa_key_derivation_abort(&derivation);
6410 return MBEDTLS_ERR_SSL_HW_ACCEL_FAILED;
Jerry Yu2a7b5ac2022-02-17 14:07:00 +08006411 }
6412
Gilles Peskine449bd832023-01-11 14:50:10 +01006413 status = psa_key_derivation_abort(&derivation);
6414 if (status != PSA_SUCCESS) {
6415 return MBEDTLS_ERR_SSL_HW_ACCEL_FAILED;
6416 }
6417 } else
Jerry Yu2a7b5ac2022-02-17 14:07:00 +08006418#endif
6419 {
Neil Armstrongca7d5062022-05-31 14:43:23 +02006420#if defined(MBEDTLS_USE_PSA_CRYPTO) && \
Gilles Peskine449bd832023-01-11 14:50:10 +01006421 defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
6422 if (handshake->ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECJPAKE) {
Neil Armstrongca7d5062022-05-31 14:43:23 +02006423 psa_status_t status;
6424 psa_algorithm_t alg = PSA_ALG_TLS12_ECJPAKE_TO_PMS;
6425 psa_key_derivation_operation_t derivation =
6426 PSA_KEY_DERIVATION_OPERATION_INIT;
6427
Gilles Peskine449bd832023-01-11 14:50:10 +01006428 MBEDTLS_SSL_DEBUG_MSG(2, ("perform PSA-based PMS KDF for ECJPAKE"));
Neil Armstrongca7d5062022-05-31 14:43:23 +02006429
6430 handshake->pmslen = PSA_TLS12_ECJPAKE_TO_PMS_DATA_SIZE;
6431
Gilles Peskine449bd832023-01-11 14:50:10 +01006432 status = psa_key_derivation_setup(&derivation, alg);
6433 if (status != PSA_SUCCESS) {
6434 return MBEDTLS_ERR_SSL_HW_ACCEL_FAILED;
Neil Armstrongca7d5062022-05-31 14:43:23 +02006435 }
6436
Gilles Peskine449bd832023-01-11 14:50:10 +01006437 status = psa_key_derivation_set_capacity(&derivation,
6438 PSA_TLS12_ECJPAKE_TO_PMS_DATA_SIZE);
6439 if (status != PSA_SUCCESS) {
6440 psa_key_derivation_abort(&derivation);
6441 return MBEDTLS_ERR_SSL_HW_ACCEL_FAILED;
Neil Armstrongca7d5062022-05-31 14:43:23 +02006442 }
6443
Gilles Peskine449bd832023-01-11 14:50:10 +01006444 status = psa_pake_get_implicit_key(&handshake->psa_pake_ctx,
6445 &derivation);
6446 if (status != PSA_SUCCESS) {
6447 psa_key_derivation_abort(&derivation);
6448 return MBEDTLS_ERR_SSL_HW_ACCEL_FAILED;
Neil Armstrongca7d5062022-05-31 14:43:23 +02006449 }
6450
Gilles Peskine449bd832023-01-11 14:50:10 +01006451 status = psa_key_derivation_output_bytes(&derivation,
6452 handshake->premaster,
6453 handshake->pmslen);
6454 if (status != PSA_SUCCESS) {
6455 psa_key_derivation_abort(&derivation);
6456 return MBEDTLS_ERR_SSL_HW_ACCEL_FAILED;
6457 }
6458
6459 status = psa_key_derivation_abort(&derivation);
6460 if (status != PSA_SUCCESS) {
6461 return MBEDTLS_ERR_SSL_HW_ACCEL_FAILED;
Neil Armstrongca7d5062022-05-31 14:43:23 +02006462 }
6463 }
6464#endif
Gilles Peskine449bd832023-01-11 14:50:10 +01006465 ret = handshake->tls_prf(handshake->premaster, handshake->pmslen,
6466 lbl, seed, seed_len,
6467 master,
6468 master_secret_len);
6469 if (ret != 0) {
6470 MBEDTLS_SSL_DEBUG_RET(1, "prf", ret);
6471 return ret;
Jerry Yu2a7b5ac2022-02-17 14:07:00 +08006472 }
6473
Gilles Peskine449bd832023-01-11 14:50:10 +01006474 MBEDTLS_SSL_DEBUG_BUF(3, "premaster secret",
6475 handshake->premaster,
6476 handshake->pmslen);
Jerry Yu2a7b5ac2022-02-17 14:07:00 +08006477
Gilles Peskine449bd832023-01-11 14:50:10 +01006478 mbedtls_platform_zeroize(handshake->premaster,
6479 sizeof(handshake->premaster));
Jerry Yu2a7b5ac2022-02-17 14:07:00 +08006480 }
6481
Gilles Peskine449bd832023-01-11 14:50:10 +01006482 return 0;
Jerry Yu2a7b5ac2022-02-17 14:07:00 +08006483}
6484
Gilles Peskine449bd832023-01-11 14:50:10 +01006485int mbedtls_ssl_derive_keys(mbedtls_ssl_context *ssl)
Jerry Yud62f87e2022-02-17 14:09:02 +08006486{
6487 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
6488 const mbedtls_ssl_ciphersuite_t * const ciphersuite_info =
6489 ssl->handshake->ciphersuite_info;
6490
Gilles Peskine449bd832023-01-11 14:50:10 +01006491 MBEDTLS_SSL_DEBUG_MSG(2, ("=> derive keys"));
Jerry Yud62f87e2022-02-17 14:09:02 +08006492
6493 /* Set PRF, calc_verify and calc_finished function pointers */
Gilles Peskine449bd832023-01-11 14:50:10 +01006494 ret = ssl_set_handshake_prfs(ssl->handshake,
6495 ciphersuite_info->mac);
6496 if (ret != 0) {
6497 MBEDTLS_SSL_DEBUG_RET(1, "ssl_set_handshake_prfs", ret);
6498 return ret;
Jerry Yud62f87e2022-02-17 14:09:02 +08006499 }
6500
6501 /* Compute master secret if needed */
Gilles Peskine449bd832023-01-11 14:50:10 +01006502 ret = ssl_compute_master(ssl->handshake,
6503 ssl->session_negotiate->master,
6504 ssl);
6505 if (ret != 0) {
6506 MBEDTLS_SSL_DEBUG_RET(1, "ssl_compute_master", ret);
6507 return ret;
Jerry Yud62f87e2022-02-17 14:09:02 +08006508 }
6509
6510 /* Swap the client and server random values:
6511 * - MS derivation wanted client+server (RFC 5246 8.1)
6512 * - key derivation wants server+client (RFC 5246 6.3) */
6513 {
6514 unsigned char tmp[64];
Gilles Peskine449bd832023-01-11 14:50:10 +01006515 memcpy(tmp, ssl->handshake->randbytes, 64);
6516 memcpy(ssl->handshake->randbytes, tmp + 32, 32);
6517 memcpy(ssl->handshake->randbytes + 32, tmp, 32);
6518 mbedtls_platform_zeroize(tmp, sizeof(tmp));
Jerry Yud62f87e2022-02-17 14:09:02 +08006519 }
6520
6521 /* Populate transform structure */
Gilles Peskine449bd832023-01-11 14:50:10 +01006522 ret = ssl_tls12_populate_transform(ssl->transform_negotiate,
6523 ssl->session_negotiate->ciphersuite,
6524 ssl->session_negotiate->master,
Neil Armstrongf2c82f02022-04-05 11:16:53 +02006525#if defined(MBEDTLS_SSL_SOME_SUITES_USE_CBC_ETM)
Gilles Peskine449bd832023-01-11 14:50:10 +01006526 ssl->session_negotiate->encrypt_then_mac,
Neil Armstrongf2c82f02022-04-05 11:16:53 +02006527#endif /* MBEDTLS_SSL_SOME_SUITES_USE_CBC_ETM */
Gilles Peskine449bd832023-01-11 14:50:10 +01006528 ssl->handshake->tls_prf,
6529 ssl->handshake->randbytes,
6530 ssl->tls_version,
6531 ssl->conf->endpoint,
6532 ssl);
6533 if (ret != 0) {
6534 MBEDTLS_SSL_DEBUG_RET(1, "ssl_tls12_populate_transform", ret);
6535 return ret;
Jerry Yud62f87e2022-02-17 14:09:02 +08006536 }
6537
6538 /* We no longer need Server/ClientHello.random values */
Gilles Peskine449bd832023-01-11 14:50:10 +01006539 mbedtls_platform_zeroize(ssl->handshake->randbytes,
6540 sizeof(ssl->handshake->randbytes));
Jerry Yud62f87e2022-02-17 14:09:02 +08006541
Gilles Peskine449bd832023-01-11 14:50:10 +01006542 MBEDTLS_SSL_DEBUG_MSG(2, ("<= derive keys"));
Jerry Yud62f87e2022-02-17 14:09:02 +08006543
Gilles Peskine449bd832023-01-11 14:50:10 +01006544 return 0;
Jerry Yud62f87e2022-02-17 14:09:02 +08006545}
Jerry Yu8392e0d2022-02-17 14:10:24 +08006546
Gilles Peskine449bd832023-01-11 14:50:10 +01006547int mbedtls_ssl_set_calc_verify_md(mbedtls_ssl_context *ssl, int md)
Ronald Cron4dcbca92022-03-07 10:21:40 +01006548{
Gilles Peskine449bd832023-01-11 14:50:10 +01006549 switch (md) {
Andrzej Kurek25f27152022-08-17 16:09:31 -04006550#if defined(MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Ronald Cron4dcbca92022-03-07 10:21:40 +01006551 case MBEDTLS_SSL_HASH_SHA384:
6552 ssl->handshake->calc_verify = ssl_calc_verify_tls_sha384;
6553 break;
6554#endif
Andrzej Kurek25f27152022-08-17 16:09:31 -04006555#if defined(MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Ronald Cron4dcbca92022-03-07 10:21:40 +01006556 case MBEDTLS_SSL_HASH_SHA256:
6557 ssl->handshake->calc_verify = ssl_calc_verify_tls_sha256;
6558 break;
6559#endif
6560 default:
Gilles Peskine449bd832023-01-11 14:50:10 +01006561 return -1;
Ronald Cron4dcbca92022-03-07 10:21:40 +01006562 }
Andrzej Kurekeabeb302022-10-17 07:52:51 -04006563#if !defined(MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA) && \
6564 !defined(MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
6565 (void) ssl;
6566#endif
Gilles Peskine449bd832023-01-11 14:50:10 +01006567 return 0;
Ronald Cron4dcbca92022-03-07 10:21:40 +01006568}
6569
Andrzej Kurek25f27152022-08-17 16:09:31 -04006570#if defined(MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Manuel Pégourié-Gonnard226aa152023-02-05 09:46:59 +01006571int ssl_calc_verify_tls_sha256(const mbedtls_ssl_context *ssl,
6572 unsigned char *hash,
6573 size_t *hlen)
Jerry Yu8392e0d2022-02-17 14:10:24 +08006574{
6575#if defined(MBEDTLS_USE_PSA_CRYPTO)
6576 size_t hash_size;
6577 psa_status_t status;
6578 psa_hash_operation_t sha256_psa = psa_hash_operation_init();
6579
Gilles Peskine449bd832023-01-11 14:50:10 +01006580 MBEDTLS_SSL_DEBUG_MSG(2, ("=> PSA calc verify sha256"));
6581 status = psa_hash_clone(&ssl->handshake->fin_sha256_psa, &sha256_psa);
6582 if (status != PSA_SUCCESS) {
Manuel Pégourié-Gonnardb9b564e2023-02-06 10:06:04 +01006583 goto exit;
Jerry Yu8392e0d2022-02-17 14:10:24 +08006584 }
6585
Gilles Peskine449bd832023-01-11 14:50:10 +01006586 status = psa_hash_finish(&sha256_psa, hash, 32, &hash_size);
6587 if (status != PSA_SUCCESS) {
Manuel Pégourié-Gonnardb9b564e2023-02-06 10:06:04 +01006588 goto exit;
Jerry Yu8392e0d2022-02-17 14:10:24 +08006589 }
6590
6591 *hlen = 32;
Gilles Peskine449bd832023-01-11 14:50:10 +01006592 MBEDTLS_SSL_DEBUG_BUF(3, "PSA calculated verify result", hash, *hlen);
6593 MBEDTLS_SSL_DEBUG_MSG(2, ("<= PSA calc verify"));
Manuel Pégourié-Gonnardb9b564e2023-02-06 10:06:04 +01006594
6595exit:
6596 psa_hash_abort(&sha256_psa);
6597 return mbedtls_md_error_from_psa(status);
Jerry Yu8392e0d2022-02-17 14:10:24 +08006598#else
Manuel Pégourié-Gonnardb9b564e2023-02-06 10:06:04 +01006599 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Jerry Yu8392e0d2022-02-17 14:10:24 +08006600 mbedtls_sha256_context sha256;
6601
Gilles Peskine449bd832023-01-11 14:50:10 +01006602 mbedtls_sha256_init(&sha256);
Jerry Yu8392e0d2022-02-17 14:10:24 +08006603
Gilles Peskine449bd832023-01-11 14:50:10 +01006604 MBEDTLS_SSL_DEBUG_MSG(2, ("=> calc verify sha256"));
Jerry Yu8392e0d2022-02-17 14:10:24 +08006605
Gilles Peskine449bd832023-01-11 14:50:10 +01006606 mbedtls_sha256_clone(&sha256, &ssl->handshake->fin_sha256);
Manuel Pégourié-Gonnardb9b564e2023-02-06 10:06:04 +01006607
6608 ret = mbedtls_sha256_finish(&sha256, hash);
6609 if (ret != 0) {
6610 goto exit;
6611 }
Jerry Yu8392e0d2022-02-17 14:10:24 +08006612
6613 *hlen = 32;
6614
Gilles Peskine449bd832023-01-11 14:50:10 +01006615 MBEDTLS_SSL_DEBUG_BUF(3, "calculated verify result", hash, *hlen);
6616 MBEDTLS_SSL_DEBUG_MSG(2, ("<= calc verify"));
Jerry Yu8392e0d2022-02-17 14:10:24 +08006617
Manuel Pégourié-Gonnardb9b564e2023-02-06 10:06:04 +01006618exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01006619 mbedtls_sha256_free(&sha256);
Jerry Yu8392e0d2022-02-17 14:10:24 +08006620#endif /* MBEDTLS_USE_PSA_CRYPTO */
Manuel Pégourié-Gonnard226aa152023-02-05 09:46:59 +01006621 return 0;
Jerry Yu8392e0d2022-02-17 14:10:24 +08006622}
Andrzej Kurek25f27152022-08-17 16:09:31 -04006623#endif /* MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA */
Jerry Yu8392e0d2022-02-17 14:10:24 +08006624
Andrzej Kurek25f27152022-08-17 16:09:31 -04006625#if defined(MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Manuel Pégourié-Gonnard226aa152023-02-05 09:46:59 +01006626int ssl_calc_verify_tls_sha384(const mbedtls_ssl_context *ssl,
6627 unsigned char *hash,
6628 size_t *hlen)
Jerry Yuc1cb3842022-02-17 14:13:48 +08006629{
6630#if defined(MBEDTLS_USE_PSA_CRYPTO)
6631 size_t hash_size;
6632 psa_status_t status;
6633 psa_hash_operation_t sha384_psa = psa_hash_operation_init();
6634
Gilles Peskine449bd832023-01-11 14:50:10 +01006635 MBEDTLS_SSL_DEBUG_MSG(2, ("=> PSA calc verify sha384"));
6636 status = psa_hash_clone(&ssl->handshake->fin_sha384_psa, &sha384_psa);
6637 if (status != PSA_SUCCESS) {
Manuel Pégourié-Gonnardb9b564e2023-02-06 10:06:04 +01006638 goto exit;
Jerry Yuc1cb3842022-02-17 14:13:48 +08006639 }
6640
Gilles Peskine449bd832023-01-11 14:50:10 +01006641 status = psa_hash_finish(&sha384_psa, hash, 48, &hash_size);
6642 if (status != PSA_SUCCESS) {
Manuel Pégourié-Gonnardb9b564e2023-02-06 10:06:04 +01006643 goto exit;
Jerry Yuc1cb3842022-02-17 14:13:48 +08006644 }
6645
6646 *hlen = 48;
Gilles Peskine449bd832023-01-11 14:50:10 +01006647 MBEDTLS_SSL_DEBUG_BUF(3, "PSA calculated verify result", hash, *hlen);
6648 MBEDTLS_SSL_DEBUG_MSG(2, ("<= PSA calc verify"));
Manuel Pégourié-Gonnardb9b564e2023-02-06 10:06:04 +01006649
6650exit:
6651 psa_hash_abort(&sha384_psa);
6652 return mbedtls_md_error_from_psa(status);
Jerry Yuc1cb3842022-02-17 14:13:48 +08006653#else
Manuel Pégourié-Gonnardb9b564e2023-02-06 10:06:04 +01006654 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Jerry Yuc1cb3842022-02-17 14:13:48 +08006655 mbedtls_sha512_context sha512;
6656
Gilles Peskine449bd832023-01-11 14:50:10 +01006657 mbedtls_sha512_init(&sha512);
Jerry Yuc1cb3842022-02-17 14:13:48 +08006658
Gilles Peskine449bd832023-01-11 14:50:10 +01006659 MBEDTLS_SSL_DEBUG_MSG(2, ("=> calc verify sha384"));
Jerry Yuc1cb3842022-02-17 14:13:48 +08006660
Gilles Peskine449bd832023-01-11 14:50:10 +01006661 mbedtls_sha512_clone(&sha512, &ssl->handshake->fin_sha384);
Manuel Pégourié-Gonnardb9b564e2023-02-06 10:06:04 +01006662
6663 ret = mbedtls_sha512_finish(&sha512, hash);
6664 if (ret != 0) {
6665 goto exit;
6666 }
Jerry Yuc1cb3842022-02-17 14:13:48 +08006667
6668 *hlen = 48;
6669
Gilles Peskine449bd832023-01-11 14:50:10 +01006670 MBEDTLS_SSL_DEBUG_BUF(3, "calculated verify result", hash, *hlen);
6671 MBEDTLS_SSL_DEBUG_MSG(2, ("<= calc verify"));
Jerry Yuc1cb3842022-02-17 14:13:48 +08006672
Manuel Pégourié-Gonnardb9b564e2023-02-06 10:06:04 +01006673exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01006674 mbedtls_sha512_free(&sha512);
Manuel Pégourié-Gonnardb9b564e2023-02-06 10:06:04 +01006675 return ret;
Jerry Yuc1cb3842022-02-17 14:13:48 +08006676#endif /* MBEDTLS_USE_PSA_CRYPTO */
Jerry Yuc1cb3842022-02-17 14:13:48 +08006677}
Andrzej Kurek25f27152022-08-17 16:09:31 -04006678#endif /* MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA */
Jerry Yuc1cb3842022-02-17 14:13:48 +08006679
Neil Armstrong80f6f322022-05-03 17:56:38 +02006680#if !defined(MBEDTLS_USE_PSA_CRYPTO) && \
6681 defined(MBEDTLS_KEY_EXCHANGE_SOME_PSK_ENABLED)
Gilles Peskine449bd832023-01-11 14:50:10 +01006682int mbedtls_ssl_psk_derive_premaster(mbedtls_ssl_context *ssl, mbedtls_key_exchange_type_t key_ex)
Jerry Yuce3dca42022-02-17 14:16:37 +08006683{
6684 unsigned char *p = ssl->handshake->premaster;
Gilles Peskine449bd832023-01-11 14:50:10 +01006685 unsigned char *end = p + sizeof(ssl->handshake->premaster);
Jerry Yuce3dca42022-02-17 14:16:37 +08006686 const unsigned char *psk = NULL;
6687 size_t psk_len = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +01006688 int psk_ret = mbedtls_ssl_get_psk(ssl, &psk, &psk_len);
Jerry Yuce3dca42022-02-17 14:16:37 +08006689
Gilles Peskine449bd832023-01-11 14:50:10 +01006690 if (psk_ret == MBEDTLS_ERR_SSL_PRIVATE_KEY_REQUIRED) {
Jerry Yuce3dca42022-02-17 14:16:37 +08006691 /*
6692 * This should never happen because the existence of a PSK is always
Przemek Stekielb293aaa2022-04-19 12:22:38 +02006693 * checked before calling this function.
6694 *
6695 * The exception is opaque DHE-PSK. For DHE-PSK fill premaster with
Przemek Stekiel8abcee92022-04-28 09:16:28 +02006696 * the shared secret without PSK.
Jerry Yuce3dca42022-02-17 14:16:37 +08006697 */
Gilles Peskine449bd832023-01-11 14:50:10 +01006698 if (key_ex != MBEDTLS_KEY_EXCHANGE_DHE_PSK) {
6699 MBEDTLS_SSL_DEBUG_MSG(1, ("should never happen"));
6700 return MBEDTLS_ERR_SSL_INTERNAL_ERROR;
Przemek Stekielb293aaa2022-04-19 12:22:38 +02006701 }
Jerry Yuce3dca42022-02-17 14:16:37 +08006702 }
6703
6704 /*
6705 * PMS = struct {
6706 * opaque other_secret<0..2^16-1>;
6707 * opaque psk<0..2^16-1>;
6708 * };
6709 * with "other_secret" depending on the particular key exchange
6710 */
6711#if defined(MBEDTLS_KEY_EXCHANGE_PSK_ENABLED)
Gilles Peskine449bd832023-01-11 14:50:10 +01006712 if (key_ex == MBEDTLS_KEY_EXCHANGE_PSK) {
6713 if (end - p < 2) {
6714 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
6715 }
Jerry Yuce3dca42022-02-17 14:16:37 +08006716
Gilles Peskine449bd832023-01-11 14:50:10 +01006717 MBEDTLS_PUT_UINT16_BE(psk_len, p, 0);
Jerry Yuce3dca42022-02-17 14:16:37 +08006718 p += 2;
6719
Gilles Peskine449bd832023-01-11 14:50:10 +01006720 if (end < p || (size_t) (end - p) < psk_len) {
6721 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
6722 }
Jerry Yuce3dca42022-02-17 14:16:37 +08006723
Gilles Peskine449bd832023-01-11 14:50:10 +01006724 memset(p, 0, psk_len);
Jerry Yuce3dca42022-02-17 14:16:37 +08006725 p += psk_len;
Gilles Peskine449bd832023-01-11 14:50:10 +01006726 } else
Jerry Yuce3dca42022-02-17 14:16:37 +08006727#endif /* MBEDTLS_KEY_EXCHANGE_PSK_ENABLED */
6728#if defined(MBEDTLS_KEY_EXCHANGE_RSA_PSK_ENABLED)
Gilles Peskine449bd832023-01-11 14:50:10 +01006729 if (key_ex == MBEDTLS_KEY_EXCHANGE_RSA_PSK) {
Jerry Yuce3dca42022-02-17 14:16:37 +08006730 /*
6731 * other_secret already set by the ClientKeyExchange message,
6732 * and is 48 bytes long
6733 */
Gilles Peskine449bd832023-01-11 14:50:10 +01006734 if (end - p < 2) {
6735 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
6736 }
Jerry Yuce3dca42022-02-17 14:16:37 +08006737
6738 *p++ = 0;
6739 *p++ = 48;
6740 p += 48;
Gilles Peskine449bd832023-01-11 14:50:10 +01006741 } else
Jerry Yuce3dca42022-02-17 14:16:37 +08006742#endif /* MBEDTLS_KEY_EXCHANGE_RSA_PSK_ENABLED */
6743#if defined(MBEDTLS_KEY_EXCHANGE_DHE_PSK_ENABLED)
Gilles Peskine449bd832023-01-11 14:50:10 +01006744 if (key_ex == MBEDTLS_KEY_EXCHANGE_DHE_PSK) {
Jerry Yuce3dca42022-02-17 14:16:37 +08006745 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
6746 size_t len;
6747
6748 /* Write length only when we know the actual value */
Gilles Peskine449bd832023-01-11 14:50:10 +01006749 if ((ret = mbedtls_dhm_calc_secret(&ssl->handshake->dhm_ctx,
6750 p + 2, end - (p + 2), &len,
6751 ssl->conf->f_rng, ssl->conf->p_rng)) != 0) {
6752 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_dhm_calc_secret", ret);
6753 return ret;
Jerry Yuce3dca42022-02-17 14:16:37 +08006754 }
Gilles Peskine449bd832023-01-11 14:50:10 +01006755 MBEDTLS_PUT_UINT16_BE(len, p, 0);
Jerry Yuce3dca42022-02-17 14:16:37 +08006756 p += 2 + len;
6757
Gilles Peskine449bd832023-01-11 14:50:10 +01006758 MBEDTLS_SSL_DEBUG_MPI(3, "DHM: K ", &ssl->handshake->dhm_ctx.K);
6759 } else
Jerry Yuce3dca42022-02-17 14:16:37 +08006760#endif /* MBEDTLS_KEY_EXCHANGE_DHE_PSK_ENABLED */
Neil Armstrong80f6f322022-05-03 17:56:38 +02006761#if defined(MBEDTLS_KEY_EXCHANGE_ECDHE_PSK_ENABLED)
Gilles Peskine449bd832023-01-11 14:50:10 +01006762 if (key_ex == MBEDTLS_KEY_EXCHANGE_ECDHE_PSK) {
Jerry Yuce3dca42022-02-17 14:16:37 +08006763 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
6764 size_t zlen;
6765
Gilles Peskine449bd832023-01-11 14:50:10 +01006766 if ((ret = mbedtls_ecdh_calc_secret(&ssl->handshake->ecdh_ctx, &zlen,
6767 p + 2, end - (p + 2),
6768 ssl->conf->f_rng, ssl->conf->p_rng)) != 0) {
6769 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ecdh_calc_secret", ret);
6770 return ret;
Jerry Yuce3dca42022-02-17 14:16:37 +08006771 }
6772
Gilles Peskine449bd832023-01-11 14:50:10 +01006773 MBEDTLS_PUT_UINT16_BE(zlen, p, 0);
Jerry Yuce3dca42022-02-17 14:16:37 +08006774 p += 2 + zlen;
6775
Gilles Peskine449bd832023-01-11 14:50:10 +01006776 MBEDTLS_SSL_DEBUG_ECDH(3, &ssl->handshake->ecdh_ctx,
6777 MBEDTLS_DEBUG_ECDH_Z);
6778 } else
Neil Armstrong80f6f322022-05-03 17:56:38 +02006779#endif /* MBEDTLS_KEY_EXCHANGE_ECDHE_PSK_ENABLED */
Jerry Yuce3dca42022-02-17 14:16:37 +08006780 {
Gilles Peskine449bd832023-01-11 14:50:10 +01006781 MBEDTLS_SSL_DEBUG_MSG(1, ("should never happen"));
6782 return MBEDTLS_ERR_SSL_INTERNAL_ERROR;
Jerry Yuce3dca42022-02-17 14:16:37 +08006783 }
6784
6785 /* opaque psk<0..2^16-1>; */
Gilles Peskine449bd832023-01-11 14:50:10 +01006786 if (end - p < 2) {
6787 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
6788 }
Jerry Yuce3dca42022-02-17 14:16:37 +08006789
Gilles Peskine449bd832023-01-11 14:50:10 +01006790 MBEDTLS_PUT_UINT16_BE(psk_len, p, 0);
Jerry Yuce3dca42022-02-17 14:16:37 +08006791 p += 2;
6792
Gilles Peskine449bd832023-01-11 14:50:10 +01006793 if (end < p || (size_t) (end - p) < psk_len) {
6794 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
6795 }
Jerry Yuce3dca42022-02-17 14:16:37 +08006796
Gilles Peskine449bd832023-01-11 14:50:10 +01006797 memcpy(p, psk, psk_len);
Jerry Yuce3dca42022-02-17 14:16:37 +08006798 p += psk_len;
6799
6800 ssl->handshake->pmslen = p - ssl->handshake->premaster;
6801
Gilles Peskine449bd832023-01-11 14:50:10 +01006802 return 0;
Jerry Yuce3dca42022-02-17 14:16:37 +08006803}
Neil Armstrong80f6f322022-05-03 17:56:38 +02006804#endif /* !MBEDTLS_USE_PSA_CRYPTO && MBEDTLS_KEY_EXCHANGE_SOME_PSK_ENABLED */
Jerry Yuc2c673d2022-02-17 14:20:39 +08006805
6806#if defined(MBEDTLS_SSL_SRV_C) && defined(MBEDTLS_SSL_RENEGOTIATION)
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02006807MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01006808static int ssl_write_hello_request(mbedtls_ssl_context *ssl);
Jerry Yuc2c673d2022-02-17 14:20:39 +08006809
6810#if defined(MBEDTLS_SSL_PROTO_DTLS)
Gilles Peskine449bd832023-01-11 14:50:10 +01006811int mbedtls_ssl_resend_hello_request(mbedtls_ssl_context *ssl)
Jerry Yuc2c673d2022-02-17 14:20:39 +08006812{
6813 /* If renegotiation is not enforced, retransmit until we would reach max
6814 * timeout if we were using the usual handshake doubling scheme */
Gilles Peskine449bd832023-01-11 14:50:10 +01006815 if (ssl->conf->renego_max_records < 0) {
Jerry Yuc2c673d2022-02-17 14:20:39 +08006816 uint32_t ratio = ssl->conf->hs_timeout_max / ssl->conf->hs_timeout_min + 1;
6817 unsigned char doublings = 1;
6818
Gilles Peskine449bd832023-01-11 14:50:10 +01006819 while (ratio != 0) {
Jerry Yuc2c673d2022-02-17 14:20:39 +08006820 ++doublings;
6821 ratio >>= 1;
6822 }
6823
Gilles Peskine449bd832023-01-11 14:50:10 +01006824 if (++ssl->renego_records_seen > doublings) {
6825 MBEDTLS_SSL_DEBUG_MSG(2, ("no longer retransmitting hello request"));
6826 return 0;
Jerry Yuc2c673d2022-02-17 14:20:39 +08006827 }
6828 }
6829
Gilles Peskine449bd832023-01-11 14:50:10 +01006830 return ssl_write_hello_request(ssl);
Jerry Yuc2c673d2022-02-17 14:20:39 +08006831}
6832#endif
6833#endif /* MBEDTLS_SSL_SRV_C && MBEDTLS_SSL_RENEGOTIATION */
Jerry Yud9526692022-02-17 14:23:47 +08006834
Jerry Yud9526692022-02-17 14:23:47 +08006835/*
6836 * Handshake functions
6837 */
6838#if !defined(MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED)
6839/* No certificate support -> dummy functions */
Gilles Peskine449bd832023-01-11 14:50:10 +01006840int mbedtls_ssl_write_certificate(mbedtls_ssl_context *ssl)
Jerry Yud9526692022-02-17 14:23:47 +08006841{
6842 const mbedtls_ssl_ciphersuite_t *ciphersuite_info =
6843 ssl->handshake->ciphersuite_info;
6844
Gilles Peskine449bd832023-01-11 14:50:10 +01006845 MBEDTLS_SSL_DEBUG_MSG(2, ("=> write certificate"));
Jerry Yud9526692022-02-17 14:23:47 +08006846
Gilles Peskine449bd832023-01-11 14:50:10 +01006847 if (!mbedtls_ssl_ciphersuite_uses_srv_cert(ciphersuite_info)) {
6848 MBEDTLS_SSL_DEBUG_MSG(2, ("<= skip write certificate"));
Jerry Yud9526692022-02-17 14:23:47 +08006849 ssl->state++;
Gilles Peskine449bd832023-01-11 14:50:10 +01006850 return 0;
Jerry Yud9526692022-02-17 14:23:47 +08006851 }
6852
Gilles Peskine449bd832023-01-11 14:50:10 +01006853 MBEDTLS_SSL_DEBUG_MSG(1, ("should never happen"));
6854 return MBEDTLS_ERR_SSL_INTERNAL_ERROR;
Jerry Yud9526692022-02-17 14:23:47 +08006855}
6856
Gilles Peskine449bd832023-01-11 14:50:10 +01006857int mbedtls_ssl_parse_certificate(mbedtls_ssl_context *ssl)
Jerry Yud9526692022-02-17 14:23:47 +08006858{
6859 const mbedtls_ssl_ciphersuite_t *ciphersuite_info =
6860 ssl->handshake->ciphersuite_info;
6861
Gilles Peskine449bd832023-01-11 14:50:10 +01006862 MBEDTLS_SSL_DEBUG_MSG(2, ("=> parse certificate"));
Jerry Yud9526692022-02-17 14:23:47 +08006863
Gilles Peskine449bd832023-01-11 14:50:10 +01006864 if (!mbedtls_ssl_ciphersuite_uses_srv_cert(ciphersuite_info)) {
6865 MBEDTLS_SSL_DEBUG_MSG(2, ("<= skip parse certificate"));
Jerry Yud9526692022-02-17 14:23:47 +08006866 ssl->state++;
Gilles Peskine449bd832023-01-11 14:50:10 +01006867 return 0;
Jerry Yud9526692022-02-17 14:23:47 +08006868 }
6869
Gilles Peskine449bd832023-01-11 14:50:10 +01006870 MBEDTLS_SSL_DEBUG_MSG(1, ("should never happen"));
6871 return MBEDTLS_ERR_SSL_INTERNAL_ERROR;
Jerry Yud9526692022-02-17 14:23:47 +08006872}
6873
6874#else /* MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED */
6875/* Some certificate support -> implement write and parse */
6876
Gilles Peskine449bd832023-01-11 14:50:10 +01006877int mbedtls_ssl_write_certificate(mbedtls_ssl_context *ssl)
Jerry Yud9526692022-02-17 14:23:47 +08006878{
6879 int ret = MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
6880 size_t i, n;
6881 const mbedtls_x509_crt *crt;
6882 const mbedtls_ssl_ciphersuite_t *ciphersuite_info =
6883 ssl->handshake->ciphersuite_info;
6884
Gilles Peskine449bd832023-01-11 14:50:10 +01006885 MBEDTLS_SSL_DEBUG_MSG(2, ("=> write certificate"));
Jerry Yud9526692022-02-17 14:23:47 +08006886
Gilles Peskine449bd832023-01-11 14:50:10 +01006887 if (!mbedtls_ssl_ciphersuite_uses_srv_cert(ciphersuite_info)) {
6888 MBEDTLS_SSL_DEBUG_MSG(2, ("<= skip write certificate"));
Jerry Yud9526692022-02-17 14:23:47 +08006889 ssl->state++;
Gilles Peskine449bd832023-01-11 14:50:10 +01006890 return 0;
Jerry Yud9526692022-02-17 14:23:47 +08006891 }
6892
6893#if defined(MBEDTLS_SSL_CLI_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01006894 if (ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT) {
6895 if (ssl->handshake->client_auth == 0) {
6896 MBEDTLS_SSL_DEBUG_MSG(2, ("<= skip write certificate"));
Jerry Yud9526692022-02-17 14:23:47 +08006897 ssl->state++;
Gilles Peskine449bd832023-01-11 14:50:10 +01006898 return 0;
Jerry Yud9526692022-02-17 14:23:47 +08006899 }
6900 }
6901#endif /* MBEDTLS_SSL_CLI_C */
6902#if defined(MBEDTLS_SSL_SRV_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01006903 if (ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER) {
6904 if (mbedtls_ssl_own_cert(ssl) == NULL) {
Jerry Yud9526692022-02-17 14:23:47 +08006905 /* Should never happen because we shouldn't have picked the
6906 * ciphersuite if we don't have a certificate. */
Gilles Peskine449bd832023-01-11 14:50:10 +01006907 return MBEDTLS_ERR_SSL_INTERNAL_ERROR;
Jerry Yud9526692022-02-17 14:23:47 +08006908 }
6909 }
6910#endif
6911
Gilles Peskine449bd832023-01-11 14:50:10 +01006912 MBEDTLS_SSL_DEBUG_CRT(3, "own certificate", mbedtls_ssl_own_cert(ssl));
Jerry Yud9526692022-02-17 14:23:47 +08006913
6914 /*
6915 * 0 . 0 handshake type
6916 * 1 . 3 handshake length
6917 * 4 . 6 length of all certs
6918 * 7 . 9 length of cert. 1
6919 * 10 . n-1 peer certificate
6920 * n . n+2 length of cert. 2
6921 * n+3 . ... upper level cert, etc.
6922 */
6923 i = 7;
Gilles Peskine449bd832023-01-11 14:50:10 +01006924 crt = mbedtls_ssl_own_cert(ssl);
Jerry Yud9526692022-02-17 14:23:47 +08006925
Gilles Peskine449bd832023-01-11 14:50:10 +01006926 while (crt != NULL) {
Jerry Yud9526692022-02-17 14:23:47 +08006927 n = crt->raw.len;
Gilles Peskine449bd832023-01-11 14:50:10 +01006928 if (n > MBEDTLS_SSL_OUT_CONTENT_LEN - 3 - i) {
6929 MBEDTLS_SSL_DEBUG_MSG(1, ("certificate too large, %" MBEDTLS_PRINTF_SIZET
6930 " > %" MBEDTLS_PRINTF_SIZET,
6931 i + 3 + n, (size_t) MBEDTLS_SSL_OUT_CONTENT_LEN));
6932 return MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL;
Jerry Yud9526692022-02-17 14:23:47 +08006933 }
6934
Gilles Peskine449bd832023-01-11 14:50:10 +01006935 ssl->out_msg[i] = MBEDTLS_BYTE_2(n);
6936 ssl->out_msg[i + 1] = MBEDTLS_BYTE_1(n);
6937 ssl->out_msg[i + 2] = MBEDTLS_BYTE_0(n);
Jerry Yud9526692022-02-17 14:23:47 +08006938
Gilles Peskine449bd832023-01-11 14:50:10 +01006939 i += 3; memcpy(ssl->out_msg + i, crt->raw.p, n);
Jerry Yud9526692022-02-17 14:23:47 +08006940 i += n; crt = crt->next;
6941 }
6942
Gilles Peskine449bd832023-01-11 14:50:10 +01006943 ssl->out_msg[4] = MBEDTLS_BYTE_2(i - 7);
6944 ssl->out_msg[5] = MBEDTLS_BYTE_1(i - 7);
6945 ssl->out_msg[6] = MBEDTLS_BYTE_0(i - 7);
Jerry Yud9526692022-02-17 14:23:47 +08006946
6947 ssl->out_msglen = i;
6948 ssl->out_msgtype = MBEDTLS_SSL_MSG_HANDSHAKE;
6949 ssl->out_msg[0] = MBEDTLS_SSL_HS_CERTIFICATE;
6950
6951 ssl->state++;
6952
Gilles Peskine449bd832023-01-11 14:50:10 +01006953 if ((ret = mbedtls_ssl_write_handshake_msg(ssl)) != 0) {
6954 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ssl_write_handshake_msg", ret);
6955 return ret;
Jerry Yud9526692022-02-17 14:23:47 +08006956 }
6957
Gilles Peskine449bd832023-01-11 14:50:10 +01006958 MBEDTLS_SSL_DEBUG_MSG(2, ("<= write certificate"));
Jerry Yud9526692022-02-17 14:23:47 +08006959
Gilles Peskine449bd832023-01-11 14:50:10 +01006960 return ret;
Jerry Yud9526692022-02-17 14:23:47 +08006961}
6962
6963#if defined(MBEDTLS_SSL_RENEGOTIATION) && defined(MBEDTLS_SSL_CLI_C)
6964
6965#if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02006966MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01006967static int ssl_check_peer_crt_unchanged(mbedtls_ssl_context *ssl,
6968 unsigned char *crt_buf,
6969 size_t crt_buf_len)
Jerry Yud9526692022-02-17 14:23:47 +08006970{
6971 mbedtls_x509_crt const * const peer_crt = ssl->session->peer_cert;
6972
Gilles Peskine449bd832023-01-11 14:50:10 +01006973 if (peer_crt == NULL) {
6974 return -1;
6975 }
Jerry Yud9526692022-02-17 14:23:47 +08006976
Gilles Peskine449bd832023-01-11 14:50:10 +01006977 if (peer_crt->raw.len != crt_buf_len) {
6978 return -1;
6979 }
Jerry Yud9526692022-02-17 14:23:47 +08006980
Gilles Peskine449bd832023-01-11 14:50:10 +01006981 return memcmp(peer_crt->raw.p, crt_buf, peer_crt->raw.len);
Jerry Yud9526692022-02-17 14:23:47 +08006982}
6983#else /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02006984MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01006985static int ssl_check_peer_crt_unchanged(mbedtls_ssl_context *ssl,
6986 unsigned char *crt_buf,
6987 size_t crt_buf_len)
Jerry Yud9526692022-02-17 14:23:47 +08006988{
6989 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
6990 unsigned char const * const peer_cert_digest =
6991 ssl->session->peer_cert_digest;
6992 mbedtls_md_type_t const peer_cert_digest_type =
6993 ssl->session->peer_cert_digest_type;
6994 mbedtls_md_info_t const * const digest_info =
Gilles Peskine449bd832023-01-11 14:50:10 +01006995 mbedtls_md_info_from_type(peer_cert_digest_type);
Jerry Yud9526692022-02-17 14:23:47 +08006996 unsigned char tmp_digest[MBEDTLS_SSL_PEER_CERT_DIGEST_MAX_LEN];
6997 size_t digest_len;
6998
Gilles Peskine449bd832023-01-11 14:50:10 +01006999 if (peer_cert_digest == NULL || digest_info == NULL) {
7000 return -1;
7001 }
Jerry Yud9526692022-02-17 14:23:47 +08007002
Gilles Peskine449bd832023-01-11 14:50:10 +01007003 digest_len = mbedtls_md_get_size(digest_info);
7004 if (digest_len > MBEDTLS_SSL_PEER_CERT_DIGEST_MAX_LEN) {
7005 return -1;
7006 }
Jerry Yud9526692022-02-17 14:23:47 +08007007
Gilles Peskine449bd832023-01-11 14:50:10 +01007008 ret = mbedtls_md(digest_info, crt_buf, crt_buf_len, tmp_digest);
7009 if (ret != 0) {
7010 return -1;
7011 }
Jerry Yud9526692022-02-17 14:23:47 +08007012
Gilles Peskine449bd832023-01-11 14:50:10 +01007013 return memcmp(tmp_digest, peer_cert_digest, digest_len);
Jerry Yud9526692022-02-17 14:23:47 +08007014}
7015#endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
7016#endif /* MBEDTLS_SSL_RENEGOTIATION && MBEDTLS_SSL_CLI_C */
7017
7018/*
7019 * Once the certificate message is read, parse it into a cert chain and
7020 * perform basic checks, but leave actual verification to the caller
7021 */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02007022MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01007023static int ssl_parse_certificate_chain(mbedtls_ssl_context *ssl,
7024 mbedtls_x509_crt *chain)
Jerry Yud9526692022-02-17 14:23:47 +08007025{
7026 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
7027#if defined(MBEDTLS_SSL_RENEGOTIATION) && defined(MBEDTLS_SSL_CLI_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01007028 int crt_cnt = 0;
Jerry Yud9526692022-02-17 14:23:47 +08007029#endif
7030 size_t i, n;
7031 uint8_t alert;
7032
Gilles Peskine449bd832023-01-11 14:50:10 +01007033 if (ssl->in_msgtype != MBEDTLS_SSL_MSG_HANDSHAKE) {
7034 MBEDTLS_SSL_DEBUG_MSG(1, ("bad certificate message"));
7035 mbedtls_ssl_send_alert_message(ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
7036 MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE);
7037 return MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE;
Jerry Yud9526692022-02-17 14:23:47 +08007038 }
7039
Gilles Peskine449bd832023-01-11 14:50:10 +01007040 if (ssl->in_msg[0] != MBEDTLS_SSL_HS_CERTIFICATE) {
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_hslen < mbedtls_ssl_hs_hdr_len(ssl) + 3 + 3) {
7047 MBEDTLS_SSL_DEBUG_MSG(1, ("bad certificate message"));
7048 mbedtls_ssl_send_alert_message(ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
7049 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR);
7050 return MBEDTLS_ERR_SSL_DECODE_ERROR;
Jerry Yud9526692022-02-17 14:23:47 +08007051 }
7052
Gilles Peskine449bd832023-01-11 14:50:10 +01007053 i = mbedtls_ssl_hs_hdr_len(ssl);
Jerry Yud9526692022-02-17 14:23:47 +08007054
7055 /*
7056 * Same message structure as in mbedtls_ssl_write_certificate()
7057 */
Gilles Peskine449bd832023-01-11 14:50:10 +01007058 n = (ssl->in_msg[i+1] << 8) | ssl->in_msg[i+2];
Jerry Yud9526692022-02-17 14:23:47 +08007059
Gilles Peskine449bd832023-01-11 14:50:10 +01007060 if (ssl->in_msg[i] != 0 ||
7061 ssl->in_hslen != n + 3 + mbedtls_ssl_hs_hdr_len(ssl)) {
7062 MBEDTLS_SSL_DEBUG_MSG(1, ("bad certificate message"));
7063 mbedtls_ssl_send_alert_message(ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
7064 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR);
7065 return MBEDTLS_ERR_SSL_DECODE_ERROR;
Jerry Yud9526692022-02-17 14:23:47 +08007066 }
7067
7068 /* Make &ssl->in_msg[i] point to the beginning of the CRT chain. */
7069 i += 3;
7070
7071 /* Iterate through and parse the CRTs in the provided chain. */
Gilles Peskine449bd832023-01-11 14:50:10 +01007072 while (i < ssl->in_hslen) {
Jerry Yud9526692022-02-17 14:23:47 +08007073 /* Check that there's room for the next CRT's length fields. */
Gilles Peskine449bd832023-01-11 14:50:10 +01007074 if (i + 3 > ssl->in_hslen) {
7075 MBEDTLS_SSL_DEBUG_MSG(1, ("bad certificate message"));
7076 mbedtls_ssl_send_alert_message(ssl,
7077 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
7078 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR);
7079 return MBEDTLS_ERR_SSL_DECODE_ERROR;
Jerry Yud9526692022-02-17 14:23:47 +08007080 }
7081 /* In theory, the CRT can be up to 2**24 Bytes, but we don't support
7082 * anything beyond 2**16 ~ 64K. */
Gilles Peskine449bd832023-01-11 14:50:10 +01007083 if (ssl->in_msg[i] != 0) {
7084 MBEDTLS_SSL_DEBUG_MSG(1, ("bad certificate message"));
7085 mbedtls_ssl_send_alert_message(ssl,
7086 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
7087 MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT);
7088 return MBEDTLS_ERR_SSL_BAD_CERTIFICATE;
Jerry Yud9526692022-02-17 14:23:47 +08007089 }
7090
7091 /* Read length of the next CRT in the chain. */
Gilles Peskine449bd832023-01-11 14:50:10 +01007092 n = ((unsigned int) ssl->in_msg[i + 1] << 8)
Jerry Yud9526692022-02-17 14:23:47 +08007093 | (unsigned int) ssl->in_msg[i + 2];
7094 i += 3;
7095
Gilles Peskine449bd832023-01-11 14:50:10 +01007096 if (n < 128 || i + n > ssl->in_hslen) {
7097 MBEDTLS_SSL_DEBUG_MSG(1, ("bad certificate message"));
7098 mbedtls_ssl_send_alert_message(ssl,
7099 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
7100 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR);
7101 return MBEDTLS_ERR_SSL_DECODE_ERROR;
Jerry Yud9526692022-02-17 14:23:47 +08007102 }
7103
7104 /* Check if we're handling the first CRT in the chain. */
7105#if defined(MBEDTLS_SSL_RENEGOTIATION) && defined(MBEDTLS_SSL_CLI_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01007106 if (crt_cnt++ == 0 &&
Jerry Yud9526692022-02-17 14:23:47 +08007107 ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT &&
Gilles Peskine449bd832023-01-11 14:50:10 +01007108 ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS) {
Jerry Yud9526692022-02-17 14:23:47 +08007109 /* During client-side renegotiation, check that the server's
7110 * end-CRTs hasn't changed compared to the initial handshake,
7111 * mitigating the triple handshake attack. On success, reuse
7112 * the original end-CRT instead of parsing it again. */
Gilles Peskine449bd832023-01-11 14:50:10 +01007113 MBEDTLS_SSL_DEBUG_MSG(3, ("Check that peer CRT hasn't changed during renegotiation"));
7114 if (ssl_check_peer_crt_unchanged(ssl,
7115 &ssl->in_msg[i],
7116 n) != 0) {
7117 MBEDTLS_SSL_DEBUG_MSG(1, ("new server cert during renegotiation"));
7118 mbedtls_ssl_send_alert_message(ssl,
7119 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
7120 MBEDTLS_SSL_ALERT_MSG_ACCESS_DENIED);
7121 return MBEDTLS_ERR_SSL_BAD_CERTIFICATE;
Jerry Yud9526692022-02-17 14:23:47 +08007122 }
7123
7124 /* Now we can safely free the original chain. */
Gilles Peskine449bd832023-01-11 14:50:10 +01007125 ssl_clear_peer_cert(ssl->session);
Jerry Yud9526692022-02-17 14:23:47 +08007126 }
7127#endif /* MBEDTLS_SSL_RENEGOTIATION && MBEDTLS_SSL_CLI_C */
7128
7129 /* Parse the next certificate in the chain. */
7130#if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
Gilles Peskine449bd832023-01-11 14:50:10 +01007131 ret = mbedtls_x509_crt_parse_der(chain, ssl->in_msg + i, n);
Jerry Yud9526692022-02-17 14:23:47 +08007132#else
7133 /* If we don't need to store the CRT chain permanently, parse
7134 * it in-place from the input buffer instead of making a copy. */
Gilles Peskine449bd832023-01-11 14:50:10 +01007135 ret = mbedtls_x509_crt_parse_der_nocopy(chain, ssl->in_msg + i, n);
Jerry Yud9526692022-02-17 14:23:47 +08007136#endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
Gilles Peskine449bd832023-01-11 14:50:10 +01007137 switch (ret) {
Jerry Yud9526692022-02-17 14:23:47 +08007138 case 0: /*ok*/
7139 case MBEDTLS_ERR_X509_UNKNOWN_SIG_ALG + MBEDTLS_ERR_OID_NOT_FOUND:
7140 /* Ignore certificate with an unknown algorithm: maybe a
7141 prior certificate was already trusted. */
7142 break;
7143
7144 case MBEDTLS_ERR_X509_ALLOC_FAILED:
7145 alert = MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR;
7146 goto crt_parse_der_failed;
7147
7148 case MBEDTLS_ERR_X509_UNKNOWN_VERSION:
7149 alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT;
7150 goto crt_parse_der_failed;
7151
7152 default:
7153 alert = MBEDTLS_SSL_ALERT_MSG_BAD_CERT;
Gilles Peskine449bd832023-01-11 14:50:10 +01007154crt_parse_der_failed:
7155 mbedtls_ssl_send_alert_message(ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL, alert);
7156 MBEDTLS_SSL_DEBUG_RET(1, " mbedtls_x509_crt_parse_der", ret);
7157 return ret;
Jerry Yud9526692022-02-17 14:23:47 +08007158 }
7159
7160 i += n;
7161 }
7162
Gilles Peskine449bd832023-01-11 14:50:10 +01007163 MBEDTLS_SSL_DEBUG_CRT(3, "peer certificate", chain);
7164 return 0;
Jerry Yud9526692022-02-17 14:23:47 +08007165}
7166
7167#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02007168MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01007169static int ssl_srv_check_client_no_crt_notification(mbedtls_ssl_context *ssl)
Jerry Yud9526692022-02-17 14:23:47 +08007170{
Gilles Peskine449bd832023-01-11 14:50:10 +01007171 if (ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT) {
7172 return -1;
7173 }
Jerry Yud9526692022-02-17 14:23:47 +08007174
Gilles Peskine449bd832023-01-11 14:50:10 +01007175 if (ssl->in_hslen == 3 + mbedtls_ssl_hs_hdr_len(ssl) &&
Jerry Yud9526692022-02-17 14:23:47 +08007176 ssl->in_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE &&
7177 ssl->in_msg[0] == MBEDTLS_SSL_HS_CERTIFICATE &&
Gilles Peskine449bd832023-01-11 14:50:10 +01007178 memcmp(ssl->in_msg + mbedtls_ssl_hs_hdr_len(ssl), "\0\0\0", 3) == 0) {
7179 MBEDTLS_SSL_DEBUG_MSG(1, ("peer has no certificate"));
7180 return 0;
Jerry Yud9526692022-02-17 14:23:47 +08007181 }
Gilles Peskine449bd832023-01-11 14:50:10 +01007182 return -1;
Jerry Yud9526692022-02-17 14:23:47 +08007183}
7184#endif /* MBEDTLS_SSL_SRV_C */
7185
7186/* Check if a certificate message is expected.
7187 * Return either
7188 * - SSL_CERTIFICATE_EXPECTED, or
7189 * - SSL_CERTIFICATE_SKIP
7190 * indicating whether a Certificate message is expected or not.
7191 */
7192#define SSL_CERTIFICATE_EXPECTED 0
7193#define SSL_CERTIFICATE_SKIP 1
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02007194MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01007195static int ssl_parse_certificate_coordinate(mbedtls_ssl_context *ssl,
7196 int authmode)
Jerry Yud9526692022-02-17 14:23:47 +08007197{
7198 const mbedtls_ssl_ciphersuite_t *ciphersuite_info =
7199 ssl->handshake->ciphersuite_info;
7200
Gilles Peskine449bd832023-01-11 14:50:10 +01007201 if (!mbedtls_ssl_ciphersuite_uses_srv_cert(ciphersuite_info)) {
7202 return SSL_CERTIFICATE_SKIP;
7203 }
Jerry Yud9526692022-02-17 14:23:47 +08007204
7205#if defined(MBEDTLS_SSL_SRV_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01007206 if (ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER) {
7207 if (ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_RSA_PSK) {
7208 return SSL_CERTIFICATE_SKIP;
7209 }
Jerry Yud9526692022-02-17 14:23:47 +08007210
Gilles Peskine449bd832023-01-11 14:50:10 +01007211 if (authmode == MBEDTLS_SSL_VERIFY_NONE) {
Jerry Yud9526692022-02-17 14:23:47 +08007212 ssl->session_negotiate->verify_result =
7213 MBEDTLS_X509_BADCERT_SKIP_VERIFY;
Gilles Peskine449bd832023-01-11 14:50:10 +01007214 return SSL_CERTIFICATE_SKIP;
Jerry Yud9526692022-02-17 14:23:47 +08007215 }
7216 }
7217#else
7218 ((void) authmode);
7219#endif /* MBEDTLS_SSL_SRV_C */
7220
Gilles Peskine449bd832023-01-11 14:50:10 +01007221 return SSL_CERTIFICATE_EXPECTED;
Jerry Yud9526692022-02-17 14:23:47 +08007222}
7223
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02007224MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01007225static int ssl_parse_certificate_verify(mbedtls_ssl_context *ssl,
7226 int authmode,
7227 mbedtls_x509_crt *chain,
7228 void *rs_ctx)
Jerry Yud9526692022-02-17 14:23:47 +08007229{
7230 int ret = 0;
7231 const mbedtls_ssl_ciphersuite_t *ciphersuite_info =
7232 ssl->handshake->ciphersuite_info;
7233 int have_ca_chain = 0;
7234
7235 int (*f_vrfy)(void *, mbedtls_x509_crt *, int, uint32_t *);
7236 void *p_vrfy;
7237
Gilles Peskine449bd832023-01-11 14:50:10 +01007238 if (authmode == MBEDTLS_SSL_VERIFY_NONE) {
7239 return 0;
7240 }
Jerry Yud9526692022-02-17 14:23:47 +08007241
Gilles Peskine449bd832023-01-11 14:50:10 +01007242 if (ssl->f_vrfy != NULL) {
7243 MBEDTLS_SSL_DEBUG_MSG(3, ("Use context-specific verification callback"));
Jerry Yud9526692022-02-17 14:23:47 +08007244 f_vrfy = ssl->f_vrfy;
7245 p_vrfy = ssl->p_vrfy;
Gilles Peskine449bd832023-01-11 14:50:10 +01007246 } else {
7247 MBEDTLS_SSL_DEBUG_MSG(3, ("Use configuration-specific verification callback"));
Jerry Yud9526692022-02-17 14:23:47 +08007248 f_vrfy = ssl->conf->f_vrfy;
7249 p_vrfy = ssl->conf->p_vrfy;
7250 }
7251
7252 /*
7253 * Main check: verify certificate
7254 */
7255#if defined(MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK)
Gilles Peskine449bd832023-01-11 14:50:10 +01007256 if (ssl->conf->f_ca_cb != NULL) {
Jerry Yud9526692022-02-17 14:23:47 +08007257 ((void) rs_ctx);
7258 have_ca_chain = 1;
7259
Gilles Peskine449bd832023-01-11 14:50:10 +01007260 MBEDTLS_SSL_DEBUG_MSG(3, ("use CA callback for X.509 CRT verification"));
Jerry Yud9526692022-02-17 14:23:47 +08007261 ret = mbedtls_x509_crt_verify_with_ca_cb(
7262 chain,
7263 ssl->conf->f_ca_cb,
7264 ssl->conf->p_ca_cb,
7265 ssl->conf->cert_profile,
7266 ssl->hostname,
7267 &ssl->session_negotiate->verify_result,
Gilles Peskine449bd832023-01-11 14:50:10 +01007268 f_vrfy, p_vrfy);
7269 } else
Jerry Yud9526692022-02-17 14:23:47 +08007270#endif /* MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK */
7271 {
7272 mbedtls_x509_crt *ca_chain;
7273 mbedtls_x509_crl *ca_crl;
7274
7275#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
Gilles Peskine449bd832023-01-11 14:50:10 +01007276 if (ssl->handshake->sni_ca_chain != NULL) {
Jerry Yud9526692022-02-17 14:23:47 +08007277 ca_chain = ssl->handshake->sni_ca_chain;
7278 ca_crl = ssl->handshake->sni_ca_crl;
Gilles Peskine449bd832023-01-11 14:50:10 +01007279 } else
Jerry Yud9526692022-02-17 14:23:47 +08007280#endif
7281 {
7282 ca_chain = ssl->conf->ca_chain;
7283 ca_crl = ssl->conf->ca_crl;
7284 }
7285
Gilles Peskine449bd832023-01-11 14:50:10 +01007286 if (ca_chain != NULL) {
Jerry Yud9526692022-02-17 14:23:47 +08007287 have_ca_chain = 1;
Gilles Peskine449bd832023-01-11 14:50:10 +01007288 }
Jerry Yud9526692022-02-17 14:23:47 +08007289
7290 ret = mbedtls_x509_crt_verify_restartable(
7291 chain,
7292 ca_chain, ca_crl,
7293 ssl->conf->cert_profile,
7294 ssl->hostname,
7295 &ssl->session_negotiate->verify_result,
Gilles Peskine449bd832023-01-11 14:50:10 +01007296 f_vrfy, p_vrfy, rs_ctx);
Jerry Yud9526692022-02-17 14:23:47 +08007297 }
7298
Gilles Peskine449bd832023-01-11 14:50:10 +01007299 if (ret != 0) {
7300 MBEDTLS_SSL_DEBUG_RET(1, "x509_verify_cert", ret);
Jerry Yud9526692022-02-17 14:23:47 +08007301 }
7302
7303#if defined(MBEDTLS_SSL_ECP_RESTARTABLE_ENABLED)
Gilles Peskine449bd832023-01-11 14:50:10 +01007304 if (ret == MBEDTLS_ERR_ECP_IN_PROGRESS) {
7305 return MBEDTLS_ERR_SSL_CRYPTO_IN_PROGRESS;
7306 }
Jerry Yud9526692022-02-17 14:23:47 +08007307#endif
7308
7309 /*
7310 * Secondary checks: always done, but change 'ret' only if it was 0
7311 */
7312
7313#if defined(MBEDTLS_ECP_C)
7314 {
7315 const mbedtls_pk_context *pk = &chain->pk;
7316
Manuel Pégourié-Gonnard66b0d612022-06-17 10:49:29 +02007317 /* If certificate uses an EC key, make sure the curve is OK.
7318 * This is a public key, so it can't be opaque, so can_do() is a good
7319 * enough check to ensure pk_ec() is safe to use here. */
Gilles Peskine449bd832023-01-11 14:50:10 +01007320 if (mbedtls_pk_can_do(pk, MBEDTLS_PK_ECKEY)) {
Leonid Rozenboim19e59732022-08-08 16:52:38 -07007321 /* and in the unlikely case the above assumption no longer holds
7322 * we are making sure that pk_ec() here does not return a NULL
7323 */
Gilles Peskine449bd832023-01-11 14:50:10 +01007324 const mbedtls_ecp_keypair *ec = mbedtls_pk_ec(*pk);
7325 if (ec == NULL) {
7326 MBEDTLS_SSL_DEBUG_MSG(1, ("mbedtls_pk_ec() returned NULL"));
7327 return MBEDTLS_ERR_SSL_INTERNAL_ERROR;
Leonid Rozenboim19e59732022-08-08 16:52:38 -07007328 }
Jerry Yud9526692022-02-17 14:23:47 +08007329
Gilles Peskine449bd832023-01-11 14:50:10 +01007330 if (mbedtls_ssl_check_curve(ssl, ec->grp.id) != 0) {
Leonid Rozenboim19e59732022-08-08 16:52:38 -07007331 ssl->session_negotiate->verify_result |=
7332 MBEDTLS_X509_BADCERT_BAD_KEY;
7333
Gilles Peskine449bd832023-01-11 14:50:10 +01007334 MBEDTLS_SSL_DEBUG_MSG(1, ("bad certificate (EC key curve)"));
7335 if (ret == 0) {
Leonid Rozenboim19e59732022-08-08 16:52:38 -07007336 ret = MBEDTLS_ERR_SSL_BAD_CERTIFICATE;
Gilles Peskine449bd832023-01-11 14:50:10 +01007337 }
Leonid Rozenboim19e59732022-08-08 16:52:38 -07007338 }
Jerry Yud9526692022-02-17 14:23:47 +08007339 }
7340 }
7341#endif /* MBEDTLS_ECP_C */
7342
Gilles Peskine449bd832023-01-11 14:50:10 +01007343 if (mbedtls_ssl_check_cert_usage(chain,
7344 ciphersuite_info,
7345 !ssl->conf->endpoint,
7346 &ssl->session_negotiate->verify_result) != 0) {
7347 MBEDTLS_SSL_DEBUG_MSG(1, ("bad certificate (usage extensions)"));
7348 if (ret == 0) {
Jerry Yud9526692022-02-17 14:23:47 +08007349 ret = MBEDTLS_ERR_SSL_BAD_CERTIFICATE;
Gilles Peskine449bd832023-01-11 14:50:10 +01007350 }
Jerry Yud9526692022-02-17 14:23:47 +08007351 }
7352
7353 /* mbedtls_x509_crt_verify_with_profile is supposed to report a
7354 * verification failure through MBEDTLS_ERR_X509_CERT_VERIFY_FAILED,
7355 * with details encoded in the verification flags. All other kinds
7356 * of error codes, including those from the user provided f_vrfy
7357 * functions, are treated as fatal and lead to a failure of
7358 * ssl_parse_certificate even if verification was optional. */
Gilles Peskine449bd832023-01-11 14:50:10 +01007359 if (authmode == MBEDTLS_SSL_VERIFY_OPTIONAL &&
7360 (ret == MBEDTLS_ERR_X509_CERT_VERIFY_FAILED ||
7361 ret == MBEDTLS_ERR_SSL_BAD_CERTIFICATE)) {
Jerry Yud9526692022-02-17 14:23:47 +08007362 ret = 0;
7363 }
7364
Gilles Peskine449bd832023-01-11 14:50:10 +01007365 if (have_ca_chain == 0 && authmode == MBEDTLS_SSL_VERIFY_REQUIRED) {
7366 MBEDTLS_SSL_DEBUG_MSG(1, ("got no CA chain"));
Jerry Yud9526692022-02-17 14:23:47 +08007367 ret = MBEDTLS_ERR_SSL_CA_CHAIN_REQUIRED;
7368 }
7369
Gilles Peskine449bd832023-01-11 14:50:10 +01007370 if (ret != 0) {
Jerry Yud9526692022-02-17 14:23:47 +08007371 uint8_t alert;
7372
7373 /* The certificate may have been rejected for several reasons.
7374 Pick one and send the corresponding alert. Which alert to send
7375 may be a subject of debate in some cases. */
Gilles Peskine449bd832023-01-11 14:50:10 +01007376 if (ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_OTHER) {
Jerry Yud9526692022-02-17 14:23:47 +08007377 alert = MBEDTLS_SSL_ALERT_MSG_ACCESS_DENIED;
Gilles Peskine449bd832023-01-11 14:50:10 +01007378 } else if (ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_CN_MISMATCH) {
Jerry Yud9526692022-02-17 14:23:47 +08007379 alert = MBEDTLS_SSL_ALERT_MSG_BAD_CERT;
Gilles Peskine449bd832023-01-11 14:50:10 +01007380 } else if (ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_KEY_USAGE) {
Jerry Yud9526692022-02-17 14:23:47 +08007381 alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT;
Gilles Peskine449bd832023-01-11 14:50:10 +01007382 } else if (ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_EXT_KEY_USAGE) {
Jerry Yud9526692022-02-17 14:23:47 +08007383 alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT;
Gilles Peskine449bd832023-01-11 14:50:10 +01007384 } else if (ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_NS_CERT_TYPE) {
Jerry Yud9526692022-02-17 14:23:47 +08007385 alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT;
Gilles Peskine449bd832023-01-11 14:50:10 +01007386 } else if (ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_BAD_PK) {
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_BAD_KEY) {
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_EXPIRED) {
Jerry Yud9526692022-02-17 14:23:47 +08007391 alert = MBEDTLS_SSL_ALERT_MSG_CERT_EXPIRED;
Gilles Peskine449bd832023-01-11 14:50:10 +01007392 } else if (ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_REVOKED) {
Jerry Yud9526692022-02-17 14:23:47 +08007393 alert = MBEDTLS_SSL_ALERT_MSG_CERT_REVOKED;
Gilles Peskine449bd832023-01-11 14:50:10 +01007394 } else if (ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_NOT_TRUSTED) {
Jerry Yud9526692022-02-17 14:23:47 +08007395 alert = MBEDTLS_SSL_ALERT_MSG_UNKNOWN_CA;
Gilles Peskine449bd832023-01-11 14:50:10 +01007396 } else {
Jerry Yud9526692022-02-17 14:23:47 +08007397 alert = MBEDTLS_SSL_ALERT_MSG_CERT_UNKNOWN;
Gilles Peskine449bd832023-01-11 14:50:10 +01007398 }
7399 mbedtls_ssl_send_alert_message(ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
7400 alert);
Jerry Yud9526692022-02-17 14:23:47 +08007401 }
7402
7403#if defined(MBEDTLS_DEBUG_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01007404 if (ssl->session_negotiate->verify_result != 0) {
7405 MBEDTLS_SSL_DEBUG_MSG(3, ("! Certificate verification flags %08x",
7406 (unsigned int) ssl->session_negotiate->verify_result));
7407 } else {
7408 MBEDTLS_SSL_DEBUG_MSG(3, ("Certificate verification flags clear"));
Jerry Yud9526692022-02-17 14:23:47 +08007409 }
7410#endif /* MBEDTLS_DEBUG_C */
7411
Gilles Peskine449bd832023-01-11 14:50:10 +01007412 return ret;
Jerry Yud9526692022-02-17 14:23:47 +08007413}
7414
7415#if !defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02007416MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01007417static int ssl_remember_peer_crt_digest(mbedtls_ssl_context *ssl,
7418 unsigned char *start, size_t len)
Jerry Yud9526692022-02-17 14:23:47 +08007419{
7420 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
7421 /* Remember digest of the peer's end-CRT. */
7422 ssl->session_negotiate->peer_cert_digest =
Gilles Peskine449bd832023-01-11 14:50:10 +01007423 mbedtls_calloc(1, MBEDTLS_SSL_PEER_CERT_DIGEST_DFL_LEN);
7424 if (ssl->session_negotiate->peer_cert_digest == NULL) {
7425 MBEDTLS_SSL_DEBUG_MSG(1, ("alloc(%d bytes) failed",
7426 MBEDTLS_SSL_PEER_CERT_DIGEST_DFL_LEN));
7427 mbedtls_ssl_send_alert_message(ssl,
7428 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
7429 MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR);
Jerry Yud9526692022-02-17 14:23:47 +08007430
Gilles Peskine449bd832023-01-11 14:50:10 +01007431 return MBEDTLS_ERR_SSL_ALLOC_FAILED;
Jerry Yud9526692022-02-17 14:23:47 +08007432 }
7433
Gilles Peskine449bd832023-01-11 14:50:10 +01007434 ret = mbedtls_md(mbedtls_md_info_from_type(
7435 MBEDTLS_SSL_PEER_CERT_DIGEST_DFL_TYPE),
7436 start, len,
7437 ssl->session_negotiate->peer_cert_digest);
Jerry Yud9526692022-02-17 14:23:47 +08007438
7439 ssl->session_negotiate->peer_cert_digest_type =
7440 MBEDTLS_SSL_PEER_CERT_DIGEST_DFL_TYPE;
7441 ssl->session_negotiate->peer_cert_digest_len =
7442 MBEDTLS_SSL_PEER_CERT_DIGEST_DFL_LEN;
7443
Gilles Peskine449bd832023-01-11 14:50:10 +01007444 return ret;
Jerry Yud9526692022-02-17 14:23:47 +08007445}
7446
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02007447MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01007448static int ssl_remember_peer_pubkey(mbedtls_ssl_context *ssl,
7449 unsigned char *start, size_t len)
Jerry Yud9526692022-02-17 14:23:47 +08007450{
7451 unsigned char *end = start + len;
7452 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
7453
7454 /* Make a copy of the peer's raw public key. */
Gilles Peskine449bd832023-01-11 14:50:10 +01007455 mbedtls_pk_init(&ssl->handshake->peer_pubkey);
7456 ret = mbedtls_pk_parse_subpubkey(&start, end,
7457 &ssl->handshake->peer_pubkey);
7458 if (ret != 0) {
Jerry Yud9526692022-02-17 14:23:47 +08007459 /* We should have parsed the public key before. */
Gilles Peskine449bd832023-01-11 14:50:10 +01007460 return MBEDTLS_ERR_SSL_INTERNAL_ERROR;
Jerry Yud9526692022-02-17 14:23:47 +08007461 }
7462
Gilles Peskine449bd832023-01-11 14:50:10 +01007463 return 0;
Jerry Yud9526692022-02-17 14:23:47 +08007464}
7465#endif /* !MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
7466
Gilles Peskine449bd832023-01-11 14:50:10 +01007467int mbedtls_ssl_parse_certificate(mbedtls_ssl_context *ssl)
Jerry Yud9526692022-02-17 14:23:47 +08007468{
7469 int ret = 0;
7470 int crt_expected;
7471#if defined(MBEDTLS_SSL_SRV_C) && defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
7472 const int authmode = ssl->handshake->sni_authmode != MBEDTLS_SSL_VERIFY_UNSET
7473 ? ssl->handshake->sni_authmode
7474 : ssl->conf->authmode;
7475#else
7476 const int authmode = ssl->conf->authmode;
7477#endif
7478 void *rs_ctx = NULL;
7479 mbedtls_x509_crt *chain = NULL;
7480
Gilles Peskine449bd832023-01-11 14:50:10 +01007481 MBEDTLS_SSL_DEBUG_MSG(2, ("=> parse certificate"));
Jerry Yud9526692022-02-17 14:23:47 +08007482
Gilles Peskine449bd832023-01-11 14:50:10 +01007483 crt_expected = ssl_parse_certificate_coordinate(ssl, authmode);
7484 if (crt_expected == SSL_CERTIFICATE_SKIP) {
7485 MBEDTLS_SSL_DEBUG_MSG(2, ("<= skip parse certificate"));
Jerry Yud9526692022-02-17 14:23:47 +08007486 goto exit;
7487 }
7488
7489#if defined(MBEDTLS_SSL_ECP_RESTARTABLE_ENABLED)
Gilles Peskine449bd832023-01-11 14:50:10 +01007490 if (ssl->handshake->ecrs_enabled &&
7491 ssl->handshake->ecrs_state == ssl_ecrs_crt_verify) {
Jerry Yud9526692022-02-17 14:23:47 +08007492 chain = ssl->handshake->ecrs_peer_cert;
7493 ssl->handshake->ecrs_peer_cert = NULL;
7494 goto crt_verify;
7495 }
7496#endif
7497
Gilles Peskine449bd832023-01-11 14:50:10 +01007498 if ((ret = mbedtls_ssl_read_record(ssl, 1)) != 0) {
Jerry Yud9526692022-02-17 14:23:47 +08007499 /* mbedtls_ssl_read_record may have sent an alert already. We
7500 let it decide whether to alert. */
Gilles Peskine449bd832023-01-11 14:50:10 +01007501 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ssl_read_record", ret);
Jerry Yud9526692022-02-17 14:23:47 +08007502 goto exit;
7503 }
7504
7505#if defined(MBEDTLS_SSL_SRV_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01007506 if (ssl_srv_check_client_no_crt_notification(ssl) == 0) {
Jerry Yud9526692022-02-17 14:23:47 +08007507 ssl->session_negotiate->verify_result = MBEDTLS_X509_BADCERT_MISSING;
7508
Gilles Peskine449bd832023-01-11 14:50:10 +01007509 if (authmode != MBEDTLS_SSL_VERIFY_OPTIONAL) {
Jerry Yud9526692022-02-17 14:23:47 +08007510 ret = MBEDTLS_ERR_SSL_NO_CLIENT_CERTIFICATE;
Gilles Peskine449bd832023-01-11 14:50:10 +01007511 }
Jerry Yud9526692022-02-17 14:23:47 +08007512
7513 goto exit;
7514 }
7515#endif /* MBEDTLS_SSL_SRV_C */
7516
7517 /* Clear existing peer CRT structure in case we tried to
7518 * reuse a session but it failed, and allocate a new one. */
Gilles Peskine449bd832023-01-11 14:50:10 +01007519 ssl_clear_peer_cert(ssl->session_negotiate);
Jerry Yud9526692022-02-17 14:23:47 +08007520
Gilles Peskine449bd832023-01-11 14:50:10 +01007521 chain = mbedtls_calloc(1, sizeof(mbedtls_x509_crt));
7522 if (chain == NULL) {
7523 MBEDTLS_SSL_DEBUG_MSG(1, ("alloc(%" MBEDTLS_PRINTF_SIZET " bytes) failed",
7524 sizeof(mbedtls_x509_crt)));
7525 mbedtls_ssl_send_alert_message(ssl,
7526 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
7527 MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR);
Jerry Yud9526692022-02-17 14:23:47 +08007528
7529 ret = MBEDTLS_ERR_SSL_ALLOC_FAILED;
7530 goto exit;
7531 }
Gilles Peskine449bd832023-01-11 14:50:10 +01007532 mbedtls_x509_crt_init(chain);
Jerry Yud9526692022-02-17 14:23:47 +08007533
Gilles Peskine449bd832023-01-11 14:50:10 +01007534 ret = ssl_parse_certificate_chain(ssl, chain);
7535 if (ret != 0) {
Jerry Yud9526692022-02-17 14:23:47 +08007536 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01007537 }
Jerry Yud9526692022-02-17 14:23:47 +08007538
7539#if defined(MBEDTLS_SSL_ECP_RESTARTABLE_ENABLED)
Gilles Peskine449bd832023-01-11 14:50:10 +01007540 if (ssl->handshake->ecrs_enabled) {
Jerry Yud9526692022-02-17 14:23:47 +08007541 ssl->handshake->ecrs_state = ssl_ecrs_crt_verify;
Gilles Peskine449bd832023-01-11 14:50:10 +01007542 }
Jerry Yud9526692022-02-17 14:23:47 +08007543
7544crt_verify:
Gilles Peskine449bd832023-01-11 14:50:10 +01007545 if (ssl->handshake->ecrs_enabled) {
Jerry Yud9526692022-02-17 14:23:47 +08007546 rs_ctx = &ssl->handshake->ecrs_ctx;
Gilles Peskine449bd832023-01-11 14:50:10 +01007547 }
Jerry Yud9526692022-02-17 14:23:47 +08007548#endif
7549
Gilles Peskine449bd832023-01-11 14:50:10 +01007550 ret = ssl_parse_certificate_verify(ssl, authmode,
7551 chain, rs_ctx);
7552 if (ret != 0) {
Jerry Yud9526692022-02-17 14:23:47 +08007553 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01007554 }
Jerry Yud9526692022-02-17 14:23:47 +08007555
7556#if !defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
7557 {
7558 unsigned char *crt_start, *pk_start;
7559 size_t crt_len, pk_len;
7560
7561 /* We parse the CRT chain without copying, so
7562 * these pointers point into the input buffer,
7563 * and are hence still valid after freeing the
7564 * CRT chain. */
7565
7566 crt_start = chain->raw.p;
7567 crt_len = chain->raw.len;
7568
7569 pk_start = chain->pk_raw.p;
7570 pk_len = chain->pk_raw.len;
7571
7572 /* Free the CRT structures before computing
7573 * digest and copying the peer's public key. */
Gilles Peskine449bd832023-01-11 14:50:10 +01007574 mbedtls_x509_crt_free(chain);
7575 mbedtls_free(chain);
Jerry Yud9526692022-02-17 14:23:47 +08007576 chain = NULL;
7577
Gilles Peskine449bd832023-01-11 14:50:10 +01007578 ret = ssl_remember_peer_crt_digest(ssl, crt_start, crt_len);
7579 if (ret != 0) {
Jerry Yud9526692022-02-17 14:23:47 +08007580 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01007581 }
Jerry Yud9526692022-02-17 14:23:47 +08007582
Gilles Peskine449bd832023-01-11 14:50:10 +01007583 ret = ssl_remember_peer_pubkey(ssl, pk_start, pk_len);
7584 if (ret != 0) {
Jerry Yud9526692022-02-17 14:23:47 +08007585 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01007586 }
Jerry Yud9526692022-02-17 14:23:47 +08007587 }
7588#else /* !MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
7589 /* Pass ownership to session structure. */
7590 ssl->session_negotiate->peer_cert = chain;
7591 chain = NULL;
7592#endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
7593
Gilles Peskine449bd832023-01-11 14:50:10 +01007594 MBEDTLS_SSL_DEBUG_MSG(2, ("<= parse certificate"));
Jerry Yud9526692022-02-17 14:23:47 +08007595
7596exit:
7597
Gilles Peskine449bd832023-01-11 14:50:10 +01007598 if (ret == 0) {
Jerry Yud9526692022-02-17 14:23:47 +08007599 ssl->state++;
Gilles Peskine449bd832023-01-11 14:50:10 +01007600 }
Jerry Yud9526692022-02-17 14:23:47 +08007601
7602#if defined(MBEDTLS_SSL_ECP_RESTARTABLE_ENABLED)
Gilles Peskine449bd832023-01-11 14:50:10 +01007603 if (ret == MBEDTLS_ERR_SSL_CRYPTO_IN_PROGRESS) {
Jerry Yud9526692022-02-17 14:23:47 +08007604 ssl->handshake->ecrs_peer_cert = chain;
7605 chain = NULL;
7606 }
7607#endif
7608
Gilles Peskine449bd832023-01-11 14:50:10 +01007609 if (chain != NULL) {
7610 mbedtls_x509_crt_free(chain);
7611 mbedtls_free(chain);
Jerry Yud9526692022-02-17 14:23:47 +08007612 }
7613
Gilles Peskine449bd832023-01-11 14:50:10 +01007614 return ret;
Jerry Yud9526692022-02-17 14:23:47 +08007615}
7616#endif /* MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED */
7617
Andrzej Kurek25f27152022-08-17 16:09:31 -04007618#if defined(MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Manuel Pégourié-Gonnard226aa152023-02-05 09:46:59 +01007619static int ssl_calc_finished_tls_sha256(
Gilles Peskine449bd832023-01-11 14:50:10 +01007620 mbedtls_ssl_context *ssl, unsigned char *buf, int from)
Jerry Yu615bd6f2022-02-17 14:25:15 +08007621{
7622 int len = 12;
7623 const char *sender;
7624 unsigned char padbuf[32];
7625#if defined(MBEDTLS_USE_PSA_CRYPTO)
7626 size_t hash_size;
7627 psa_hash_operation_t sha256_psa = PSA_HASH_OPERATION_INIT;
7628 psa_status_t status;
7629#else
7630 mbedtls_sha256_context sha256;
7631#endif
7632
7633 mbedtls_ssl_session *session = ssl->session_negotiate;
Gilles Peskine449bd832023-01-11 14:50:10 +01007634 if (!session) {
Jerry Yu615bd6f2022-02-17 14:25:15 +08007635 session = ssl->session;
Gilles Peskine449bd832023-01-11 14:50:10 +01007636 }
Jerry Yu615bd6f2022-02-17 14:25:15 +08007637
Gilles Peskine449bd832023-01-11 14:50:10 +01007638 sender = (from == MBEDTLS_SSL_IS_CLIENT)
Jerry Yu615bd6f2022-02-17 14:25:15 +08007639 ? "client finished"
7640 : "server finished";
7641
7642#if defined(MBEDTLS_USE_PSA_CRYPTO)
7643 sha256_psa = psa_hash_operation_init();
7644
Gilles Peskine449bd832023-01-11 14:50:10 +01007645 MBEDTLS_SSL_DEBUG_MSG(2, ("=> calc PSA finished tls sha256"));
Jerry Yu615bd6f2022-02-17 14:25:15 +08007646
Gilles Peskine449bd832023-01-11 14:50:10 +01007647 status = psa_hash_clone(&ssl->handshake->fin_sha256_psa, &sha256_psa);
7648 if (status != PSA_SUCCESS) {
7649 MBEDTLS_SSL_DEBUG_MSG(2, ("PSA hash clone failed"));
Manuel Pégourié-Gonnard226aa152023-02-05 09:46:59 +01007650 return 0;
Jerry Yu615bd6f2022-02-17 14:25:15 +08007651 }
7652
Gilles Peskine449bd832023-01-11 14:50:10 +01007653 status = psa_hash_finish(&sha256_psa, padbuf, sizeof(padbuf), &hash_size);
7654 if (status != PSA_SUCCESS) {
7655 MBEDTLS_SSL_DEBUG_MSG(2, ("PSA hash finish failed"));
Manuel Pégourié-Gonnard226aa152023-02-05 09:46:59 +01007656 return 0;
Jerry Yu615bd6f2022-02-17 14:25:15 +08007657 }
Gilles Peskine449bd832023-01-11 14:50:10 +01007658 MBEDTLS_SSL_DEBUG_BUF(3, "PSA calculated padbuf", padbuf, 32);
Jerry Yu615bd6f2022-02-17 14:25:15 +08007659#else
7660
Gilles Peskine449bd832023-01-11 14:50:10 +01007661 mbedtls_sha256_init(&sha256);
Jerry Yu615bd6f2022-02-17 14:25:15 +08007662
Gilles Peskine449bd832023-01-11 14:50:10 +01007663 MBEDTLS_SSL_DEBUG_MSG(2, ("=> calc finished tls sha256"));
Jerry Yu615bd6f2022-02-17 14:25:15 +08007664
Gilles Peskine449bd832023-01-11 14:50:10 +01007665 mbedtls_sha256_clone(&sha256, &ssl->handshake->fin_sha256);
Jerry Yu615bd6f2022-02-17 14:25:15 +08007666
7667 /*
7668 * TLSv1.2:
7669 * hash = PRF( master, finished_label,
7670 * Hash( handshake ) )[0.11]
7671 */
7672
7673#if !defined(MBEDTLS_SHA256_ALT)
Gilles Peskine449bd832023-01-11 14:50:10 +01007674 MBEDTLS_SSL_DEBUG_BUF(4, "finished sha2 state", (unsigned char *)
7675 sha256.state, sizeof(sha256.state));
Jerry Yu615bd6f2022-02-17 14:25:15 +08007676#endif
7677
Gilles Peskine449bd832023-01-11 14:50:10 +01007678 mbedtls_sha256_finish(&sha256, padbuf);
7679 mbedtls_sha256_free(&sha256);
Jerry Yu615bd6f2022-02-17 14:25:15 +08007680#endif /* MBEDTLS_USE_PSA_CRYPTO */
7681
Gilles Peskine449bd832023-01-11 14:50:10 +01007682 ssl->handshake->tls_prf(session->master, 48, sender,
7683 padbuf, 32, buf, len);
Jerry Yu615bd6f2022-02-17 14:25:15 +08007684
Gilles Peskine449bd832023-01-11 14:50:10 +01007685 MBEDTLS_SSL_DEBUG_BUF(3, "calc finished result", buf, len);
Jerry Yu615bd6f2022-02-17 14:25:15 +08007686
Gilles Peskine449bd832023-01-11 14:50:10 +01007687 mbedtls_platform_zeroize(padbuf, sizeof(padbuf));
Jerry Yu615bd6f2022-02-17 14:25:15 +08007688
Gilles Peskine449bd832023-01-11 14:50:10 +01007689 MBEDTLS_SSL_DEBUG_MSG(2, ("<= calc finished"));
Manuel Pégourié-Gonnard226aa152023-02-05 09:46:59 +01007690 return 0;
Jerry Yu615bd6f2022-02-17 14:25:15 +08007691}
Andrzej Kurekcccb0442022-08-19 03:42:11 -04007692#endif /* MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA*/
Jerry Yu615bd6f2022-02-17 14:25:15 +08007693
Jerry Yub7ba49e2022-02-17 14:25:53 +08007694
Andrzej Kurek25f27152022-08-17 16:09:31 -04007695#if defined(MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Manuel Pégourié-Gonnard226aa152023-02-05 09:46:59 +01007696static int ssl_calc_finished_tls_sha384(
Gilles Peskine449bd832023-01-11 14:50:10 +01007697 mbedtls_ssl_context *ssl, unsigned char *buf, int from)
Jerry Yub7ba49e2022-02-17 14:25:53 +08007698{
7699 int len = 12;
7700 const char *sender;
7701 unsigned char padbuf[48];
7702#if defined(MBEDTLS_USE_PSA_CRYPTO)
7703 size_t hash_size;
7704 psa_hash_operation_t sha384_psa = PSA_HASH_OPERATION_INIT;
7705 psa_status_t status;
7706#else
7707 mbedtls_sha512_context sha512;
7708#endif
7709
7710 mbedtls_ssl_session *session = ssl->session_negotiate;
Gilles Peskine449bd832023-01-11 14:50:10 +01007711 if (!session) {
Jerry Yub7ba49e2022-02-17 14:25:53 +08007712 session = ssl->session;
Gilles Peskine449bd832023-01-11 14:50:10 +01007713 }
Jerry Yub7ba49e2022-02-17 14:25:53 +08007714
Gilles Peskine449bd832023-01-11 14:50:10 +01007715 sender = (from == MBEDTLS_SSL_IS_CLIENT)
Jerry Yub7ba49e2022-02-17 14:25:53 +08007716 ? "client finished"
7717 : "server finished";
7718
7719#if defined(MBEDTLS_USE_PSA_CRYPTO)
7720 sha384_psa = psa_hash_operation_init();
7721
Gilles Peskine449bd832023-01-11 14:50:10 +01007722 MBEDTLS_SSL_DEBUG_MSG(2, ("=> calc PSA finished tls sha384"));
Jerry Yub7ba49e2022-02-17 14:25:53 +08007723
Gilles Peskine449bd832023-01-11 14:50:10 +01007724 status = psa_hash_clone(&ssl->handshake->fin_sha384_psa, &sha384_psa);
7725 if (status != PSA_SUCCESS) {
7726 MBEDTLS_SSL_DEBUG_MSG(2, ("PSA hash clone failed"));
Manuel Pégourié-Gonnard226aa152023-02-05 09:46:59 +01007727 return 0;
Jerry Yub7ba49e2022-02-17 14:25:53 +08007728 }
7729
Gilles Peskine449bd832023-01-11 14:50:10 +01007730 status = psa_hash_finish(&sha384_psa, padbuf, sizeof(padbuf), &hash_size);
7731 if (status != PSA_SUCCESS) {
7732 MBEDTLS_SSL_DEBUG_MSG(2, ("PSA hash finish failed"));
Manuel Pégourié-Gonnard226aa152023-02-05 09:46:59 +01007733 return 0;
Jerry Yub7ba49e2022-02-17 14:25:53 +08007734 }
Gilles Peskine449bd832023-01-11 14:50:10 +01007735 MBEDTLS_SSL_DEBUG_BUF(3, "PSA calculated padbuf", padbuf, 48);
Jerry Yub7ba49e2022-02-17 14:25:53 +08007736#else
Gilles Peskine449bd832023-01-11 14:50:10 +01007737 mbedtls_sha512_init(&sha512);
Jerry Yub7ba49e2022-02-17 14:25:53 +08007738
Gilles Peskine449bd832023-01-11 14:50:10 +01007739 MBEDTLS_SSL_DEBUG_MSG(2, ("=> calc finished tls sha384"));
Jerry Yub7ba49e2022-02-17 14:25:53 +08007740
Gilles Peskine449bd832023-01-11 14:50:10 +01007741 mbedtls_sha512_clone(&sha512, &ssl->handshake->fin_sha384);
Jerry Yub7ba49e2022-02-17 14:25:53 +08007742
7743 /*
7744 * TLSv1.2:
7745 * hash = PRF( master, finished_label,
7746 * Hash( handshake ) )[0.11]
7747 */
7748
7749#if !defined(MBEDTLS_SHA512_ALT)
Gilles Peskine449bd832023-01-11 14:50:10 +01007750 MBEDTLS_SSL_DEBUG_BUF(4, "finished sha512 state", (unsigned char *)
7751 sha512.state, sizeof(sha512.state));
Jerry Yub7ba49e2022-02-17 14:25:53 +08007752#endif
Gilles Peskine449bd832023-01-11 14:50:10 +01007753 mbedtls_sha512_finish(&sha512, padbuf);
Jerry Yub7ba49e2022-02-17 14:25:53 +08007754
Gilles Peskine449bd832023-01-11 14:50:10 +01007755 mbedtls_sha512_free(&sha512);
Jerry Yub7ba49e2022-02-17 14:25:53 +08007756#endif
7757
Gilles Peskine449bd832023-01-11 14:50:10 +01007758 ssl->handshake->tls_prf(session->master, 48, sender,
7759 padbuf, 48, buf, len);
Jerry Yub7ba49e2022-02-17 14:25:53 +08007760
Gilles Peskine449bd832023-01-11 14:50:10 +01007761 MBEDTLS_SSL_DEBUG_BUF(3, "calc finished result", buf, len);
Jerry Yub7ba49e2022-02-17 14:25:53 +08007762
Gilles Peskine449bd832023-01-11 14:50:10 +01007763 mbedtls_platform_zeroize(padbuf, sizeof(padbuf));
Jerry Yub7ba49e2022-02-17 14:25:53 +08007764
Gilles Peskine449bd832023-01-11 14:50:10 +01007765 MBEDTLS_SSL_DEBUG_MSG(2, ("<= calc finished"));
Manuel Pégourié-Gonnard226aa152023-02-05 09:46:59 +01007766 return 0;
Jerry Yub7ba49e2022-02-17 14:25:53 +08007767}
Andrzej Kurekcccb0442022-08-19 03:42:11 -04007768#endif /* MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA*/
Jerry Yub7ba49e2022-02-17 14:25:53 +08007769
Gilles Peskine449bd832023-01-11 14:50:10 +01007770void mbedtls_ssl_handshake_wrapup_free_hs_transform(mbedtls_ssl_context *ssl)
Jerry Yuaef00152022-02-17 14:27:31 +08007771{
Gilles Peskine449bd832023-01-11 14:50:10 +01007772 MBEDTLS_SSL_DEBUG_MSG(3, ("=> handshake wrapup: final free"));
Jerry Yuaef00152022-02-17 14:27:31 +08007773
7774 /*
7775 * Free our handshake params
7776 */
Gilles Peskine449bd832023-01-11 14:50:10 +01007777 mbedtls_ssl_handshake_free(ssl);
7778 mbedtls_free(ssl->handshake);
Jerry Yuaef00152022-02-17 14:27:31 +08007779 ssl->handshake = NULL;
7780
7781 /*
Shaun Case8b0ecbc2021-12-20 21:14:10 -08007782 * Free the previous transform and switch in the current one
Jerry Yuaef00152022-02-17 14:27:31 +08007783 */
Gilles Peskine449bd832023-01-11 14:50:10 +01007784 if (ssl->transform) {
7785 mbedtls_ssl_transform_free(ssl->transform);
7786 mbedtls_free(ssl->transform);
Jerry Yuaef00152022-02-17 14:27:31 +08007787 }
7788 ssl->transform = ssl->transform_negotiate;
7789 ssl->transform_negotiate = NULL;
7790
Gilles Peskine449bd832023-01-11 14:50:10 +01007791 MBEDTLS_SSL_DEBUG_MSG(3, ("<= handshake wrapup: final free"));
Jerry Yuaef00152022-02-17 14:27:31 +08007792}
7793
Gilles Peskine449bd832023-01-11 14:50:10 +01007794void mbedtls_ssl_handshake_wrapup(mbedtls_ssl_context *ssl)
Jerry Yu2a9fff52022-02-17 14:28:51 +08007795{
7796 int resume = ssl->handshake->resume;
7797
Gilles Peskine449bd832023-01-11 14:50:10 +01007798 MBEDTLS_SSL_DEBUG_MSG(3, ("=> handshake wrapup"));
Jerry Yu2a9fff52022-02-17 14:28:51 +08007799
7800#if defined(MBEDTLS_SSL_RENEGOTIATION)
Gilles Peskine449bd832023-01-11 14:50:10 +01007801 if (ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS) {
Jerry Yu2a9fff52022-02-17 14:28:51 +08007802 ssl->renego_status = MBEDTLS_SSL_RENEGOTIATION_DONE;
7803 ssl->renego_records_seen = 0;
7804 }
7805#endif
7806
7807 /*
7808 * Free the previous session and switch in the current one
7809 */
Gilles Peskine449bd832023-01-11 14:50:10 +01007810 if (ssl->session) {
Jerry Yu2a9fff52022-02-17 14:28:51 +08007811#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
7812 /* RFC 7366 3.1: keep the EtM state */
7813 ssl->session_negotiate->encrypt_then_mac =
Gilles Peskine449bd832023-01-11 14:50:10 +01007814 ssl->session->encrypt_then_mac;
Jerry Yu2a9fff52022-02-17 14:28:51 +08007815#endif
7816
Gilles Peskine449bd832023-01-11 14:50:10 +01007817 mbedtls_ssl_session_free(ssl->session);
7818 mbedtls_free(ssl->session);
Jerry Yu2a9fff52022-02-17 14:28:51 +08007819 }
7820 ssl->session = ssl->session_negotiate;
7821 ssl->session_negotiate = NULL;
7822
7823 /*
7824 * Add cache entry
7825 */
Gilles Peskine449bd832023-01-11 14:50:10 +01007826 if (ssl->conf->f_set_cache != NULL &&
Jerry Yu2a9fff52022-02-17 14:28:51 +08007827 ssl->session->id_len != 0 &&
Gilles Peskine449bd832023-01-11 14:50:10 +01007828 resume == 0) {
7829 if (ssl->conf->f_set_cache(ssl->conf->p_cache,
7830 ssl->session->id,
7831 ssl->session->id_len,
7832 ssl->session) != 0) {
7833 MBEDTLS_SSL_DEBUG_MSG(1, ("cache did not store session"));
7834 }
Jerry Yu2a9fff52022-02-17 14:28:51 +08007835 }
7836
7837#if defined(MBEDTLS_SSL_PROTO_DTLS)
Gilles Peskine449bd832023-01-11 14:50:10 +01007838 if (ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
7839 ssl->handshake->flight != NULL) {
Jerry Yu2a9fff52022-02-17 14:28:51 +08007840 /* Cancel handshake timer */
Gilles Peskine449bd832023-01-11 14:50:10 +01007841 mbedtls_ssl_set_timer(ssl, 0);
Jerry Yu2a9fff52022-02-17 14:28:51 +08007842
7843 /* Keep last flight around in case we need to resend it:
7844 * we need the handshake and transform structures for that */
Gilles Peskine449bd832023-01-11 14:50:10 +01007845 MBEDTLS_SSL_DEBUG_MSG(3, ("skip freeing handshake and transform"));
7846 } else
Jerry Yu2a9fff52022-02-17 14:28:51 +08007847#endif
Gilles Peskine449bd832023-01-11 14:50:10 +01007848 mbedtls_ssl_handshake_wrapup_free_hs_transform(ssl);
Jerry Yu2a9fff52022-02-17 14:28:51 +08007849
Jerry Yu5ed73ff2022-10-27 13:08:42 +08007850 ssl->state = MBEDTLS_SSL_HANDSHAKE_OVER;
Jerry Yu2a9fff52022-02-17 14:28:51 +08007851
Gilles Peskine449bd832023-01-11 14:50:10 +01007852 MBEDTLS_SSL_DEBUG_MSG(3, ("<= handshake wrapup"));
Jerry Yu2a9fff52022-02-17 14:28:51 +08007853}
7854
Gilles Peskine449bd832023-01-11 14:50:10 +01007855int mbedtls_ssl_write_finished(mbedtls_ssl_context *ssl)
Jerry Yu3c8e47b2022-02-17 14:30:01 +08007856{
7857 int ret, hash_len;
7858
Gilles Peskine449bd832023-01-11 14:50:10 +01007859 MBEDTLS_SSL_DEBUG_MSG(2, ("=> write finished"));
Jerry Yu3c8e47b2022-02-17 14:30:01 +08007860
Gilles Peskine449bd832023-01-11 14:50:10 +01007861 mbedtls_ssl_update_out_pointers(ssl, ssl->transform_negotiate);
Jerry Yu3c8e47b2022-02-17 14:30:01 +08007862
Manuel Pégourié-Gonnardb8b07aa2023-02-06 00:34:21 +01007863 ret = ssl->handshake->calc_finished(ssl, ssl->out_msg + 4, ssl->conf->endpoint);
7864 if (ret != 0) {
7865 MBEDTLS_SSL_DEBUG_RET(1, "calc_finished", ret);
7866 }
Jerry Yu3c8e47b2022-02-17 14:30:01 +08007867
7868 /*
7869 * RFC 5246 7.4.9 (Page 63) says 12 is the default length and ciphersuites
7870 * may define some other value. Currently (early 2016), no defined
7871 * ciphersuite does this (and this is unlikely to change as activity has
7872 * moved to TLS 1.3 now) so we can keep the hardcoded 12 here.
7873 */
7874 hash_len = 12;
7875
7876#if defined(MBEDTLS_SSL_RENEGOTIATION)
7877 ssl->verify_data_len = hash_len;
Gilles Peskine449bd832023-01-11 14:50:10 +01007878 memcpy(ssl->own_verify_data, ssl->out_msg + 4, hash_len);
Jerry Yu3c8e47b2022-02-17 14:30:01 +08007879#endif
7880
7881 ssl->out_msglen = 4 + hash_len;
7882 ssl->out_msgtype = MBEDTLS_SSL_MSG_HANDSHAKE;
7883 ssl->out_msg[0] = MBEDTLS_SSL_HS_FINISHED;
7884
7885 /*
7886 * In case of session resuming, invert the client and server
7887 * ChangeCipherSpec messages order.
7888 */
Gilles Peskine449bd832023-01-11 14:50:10 +01007889 if (ssl->handshake->resume != 0) {
Jerry Yu3c8e47b2022-02-17 14:30:01 +08007890#if defined(MBEDTLS_SSL_CLI_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01007891 if (ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT) {
Jerry Yu3c8e47b2022-02-17 14:30:01 +08007892 ssl->state = MBEDTLS_SSL_HANDSHAKE_WRAPUP;
Gilles Peskine449bd832023-01-11 14:50:10 +01007893 }
Jerry Yu3c8e47b2022-02-17 14:30:01 +08007894#endif
7895#if defined(MBEDTLS_SSL_SRV_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01007896 if (ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER) {
Jerry Yu3c8e47b2022-02-17 14:30:01 +08007897 ssl->state = MBEDTLS_SSL_CLIENT_CHANGE_CIPHER_SPEC;
Gilles Peskine449bd832023-01-11 14:50:10 +01007898 }
Jerry Yu3c8e47b2022-02-17 14:30:01 +08007899#endif
Gilles Peskine449bd832023-01-11 14:50:10 +01007900 } else {
Jerry Yu3c8e47b2022-02-17 14:30:01 +08007901 ssl->state++;
Gilles Peskine449bd832023-01-11 14:50:10 +01007902 }
Jerry Yu3c8e47b2022-02-17 14:30:01 +08007903
7904 /*
7905 * Switch to our negotiated transform and session parameters for outbound
7906 * data.
7907 */
Gilles Peskine449bd832023-01-11 14:50:10 +01007908 MBEDTLS_SSL_DEBUG_MSG(3, ("switching to new transform spec for outbound data"));
Jerry Yu3c8e47b2022-02-17 14:30:01 +08007909
7910#if defined(MBEDTLS_SSL_PROTO_DTLS)
Gilles Peskine449bd832023-01-11 14:50:10 +01007911 if (ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM) {
Jerry Yu3c8e47b2022-02-17 14:30:01 +08007912 unsigned char i;
7913
7914 /* Remember current epoch settings for resending */
7915 ssl->handshake->alt_transform_out = ssl->transform_out;
Gilles Peskine449bd832023-01-11 14:50:10 +01007916 memcpy(ssl->handshake->alt_out_ctr, ssl->cur_out_ctr,
7917 sizeof(ssl->handshake->alt_out_ctr));
Jerry Yu3c8e47b2022-02-17 14:30:01 +08007918
7919 /* Set sequence_number to zero */
Gilles Peskine449bd832023-01-11 14:50:10 +01007920 memset(&ssl->cur_out_ctr[2], 0, sizeof(ssl->cur_out_ctr) - 2);
Jerry Yu3c8e47b2022-02-17 14:30:01 +08007921
7922
7923 /* Increment epoch */
Gilles Peskine449bd832023-01-11 14:50:10 +01007924 for (i = 2; i > 0; i--) {
7925 if (++ssl->cur_out_ctr[i - 1] != 0) {
Jerry Yu3c8e47b2022-02-17 14:30:01 +08007926 break;
Gilles Peskine449bd832023-01-11 14:50:10 +01007927 }
7928 }
Jerry Yu3c8e47b2022-02-17 14:30:01 +08007929
7930 /* The loop goes to its end iff the counter is wrapping */
Gilles Peskine449bd832023-01-11 14:50:10 +01007931 if (i == 0) {
7932 MBEDTLS_SSL_DEBUG_MSG(1, ("DTLS epoch would wrap"));
7933 return MBEDTLS_ERR_SSL_COUNTER_WRAPPING;
Jerry Yu3c8e47b2022-02-17 14:30:01 +08007934 }
Gilles Peskine449bd832023-01-11 14:50:10 +01007935 } else
Jerry Yu3c8e47b2022-02-17 14:30:01 +08007936#endif /* MBEDTLS_SSL_PROTO_DTLS */
Gilles Peskine449bd832023-01-11 14:50:10 +01007937 memset(ssl->cur_out_ctr, 0, sizeof(ssl->cur_out_ctr));
Jerry Yu3c8e47b2022-02-17 14:30:01 +08007938
7939 ssl->transform_out = ssl->transform_negotiate;
7940 ssl->session_out = ssl->session_negotiate;
7941
7942#if defined(MBEDTLS_SSL_PROTO_DTLS)
Gilles Peskine449bd832023-01-11 14:50:10 +01007943 if (ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM) {
7944 mbedtls_ssl_send_flight_completed(ssl);
7945 }
Jerry Yu3c8e47b2022-02-17 14:30:01 +08007946#endif
7947
Gilles Peskine449bd832023-01-11 14:50:10 +01007948 if ((ret = mbedtls_ssl_write_handshake_msg(ssl)) != 0) {
7949 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ssl_write_handshake_msg", ret);
7950 return ret;
Jerry Yu3c8e47b2022-02-17 14:30:01 +08007951 }
7952
7953#if defined(MBEDTLS_SSL_PROTO_DTLS)
Gilles Peskine449bd832023-01-11 14:50:10 +01007954 if (ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
7955 (ret = mbedtls_ssl_flight_transmit(ssl)) != 0) {
7956 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ssl_flight_transmit", ret);
7957 return ret;
Jerry Yu3c8e47b2022-02-17 14:30:01 +08007958 }
7959#endif
7960
Gilles Peskine449bd832023-01-11 14:50:10 +01007961 MBEDTLS_SSL_DEBUG_MSG(2, ("<= write finished"));
Jerry Yu3c8e47b2022-02-17 14:30:01 +08007962
Gilles Peskine449bd832023-01-11 14:50:10 +01007963 return 0;
Jerry Yu3c8e47b2022-02-17 14:30:01 +08007964}
7965
Jerry Yu0b3d7c12022-02-17 14:30:51 +08007966#define SSL_MAX_HASH_LEN 12
7967
Gilles Peskine449bd832023-01-11 14:50:10 +01007968int mbedtls_ssl_parse_finished(mbedtls_ssl_context *ssl)
Jerry Yu0b3d7c12022-02-17 14:30:51 +08007969{
7970 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
7971 unsigned int hash_len = 12;
7972 unsigned char buf[SSL_MAX_HASH_LEN];
7973
Gilles Peskine449bd832023-01-11 14:50:10 +01007974 MBEDTLS_SSL_DEBUG_MSG(2, ("=> parse finished"));
Jerry Yu0b3d7c12022-02-17 14:30:51 +08007975
Manuel Pégourié-Gonnardb8b07aa2023-02-06 00:34:21 +01007976 ret = ssl->handshake->calc_finished(ssl, buf, ssl->conf->endpoint ^ 1);
7977 if (ret != 0) {
7978 MBEDTLS_SSL_DEBUG_RET(1, "calc_finished", ret);
7979 }
Jerry Yu0b3d7c12022-02-17 14:30:51 +08007980
Gilles Peskine449bd832023-01-11 14:50:10 +01007981 if ((ret = mbedtls_ssl_read_record(ssl, 1)) != 0) {
7982 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ssl_read_record", ret);
Jerry Yu0b3d7c12022-02-17 14:30:51 +08007983 goto exit;
7984 }
7985
Gilles Peskine449bd832023-01-11 14:50:10 +01007986 if (ssl->in_msgtype != MBEDTLS_SSL_MSG_HANDSHAKE) {
7987 MBEDTLS_SSL_DEBUG_MSG(1, ("bad finished message"));
7988 mbedtls_ssl_send_alert_message(ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
7989 MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE);
Jerry Yu0b3d7c12022-02-17 14:30:51 +08007990 ret = MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE;
7991 goto exit;
7992 }
7993
Gilles Peskine449bd832023-01-11 14:50:10 +01007994 if (ssl->in_msg[0] != MBEDTLS_SSL_HS_FINISHED) {
7995 mbedtls_ssl_send_alert_message(ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
7996 MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE);
Jerry Yu0b3d7c12022-02-17 14:30:51 +08007997 ret = MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE;
7998 goto exit;
7999 }
8000
Gilles Peskine449bd832023-01-11 14:50:10 +01008001 if (ssl->in_hslen != mbedtls_ssl_hs_hdr_len(ssl) + hash_len) {
8002 MBEDTLS_SSL_DEBUG_MSG(1, ("bad finished message"));
8003 mbedtls_ssl_send_alert_message(ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
8004 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR);
Jerry Yu0b3d7c12022-02-17 14:30:51 +08008005 ret = MBEDTLS_ERR_SSL_DECODE_ERROR;
8006 goto exit;
8007 }
8008
Gilles Peskine449bd832023-01-11 14:50:10 +01008009 if (mbedtls_ct_memcmp(ssl->in_msg + mbedtls_ssl_hs_hdr_len(ssl),
8010 buf, hash_len) != 0) {
8011 MBEDTLS_SSL_DEBUG_MSG(1, ("bad finished message"));
8012 mbedtls_ssl_send_alert_message(ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
8013 MBEDTLS_SSL_ALERT_MSG_DECRYPT_ERROR);
Jerry Yu0b3d7c12022-02-17 14:30:51 +08008014 ret = MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE;
8015 goto exit;
8016 }
8017
8018#if defined(MBEDTLS_SSL_RENEGOTIATION)
8019 ssl->verify_data_len = hash_len;
Gilles Peskine449bd832023-01-11 14:50:10 +01008020 memcpy(ssl->peer_verify_data, buf, hash_len);
Jerry Yu0b3d7c12022-02-17 14:30:51 +08008021#endif
8022
Gilles Peskine449bd832023-01-11 14:50:10 +01008023 if (ssl->handshake->resume != 0) {
Jerry Yu0b3d7c12022-02-17 14:30:51 +08008024#if defined(MBEDTLS_SSL_CLI_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01008025 if (ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT) {
Jerry Yu0b3d7c12022-02-17 14:30:51 +08008026 ssl->state = MBEDTLS_SSL_CLIENT_CHANGE_CIPHER_SPEC;
Gilles Peskine449bd832023-01-11 14:50:10 +01008027 }
Jerry Yu0b3d7c12022-02-17 14:30:51 +08008028#endif
8029#if defined(MBEDTLS_SSL_SRV_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01008030 if (ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER) {
Jerry Yu0b3d7c12022-02-17 14:30:51 +08008031 ssl->state = MBEDTLS_SSL_HANDSHAKE_WRAPUP;
Gilles Peskine449bd832023-01-11 14:50:10 +01008032 }
Jerry Yu0b3d7c12022-02-17 14:30:51 +08008033#endif
Gilles Peskine449bd832023-01-11 14:50:10 +01008034 } else {
Jerry Yu0b3d7c12022-02-17 14:30:51 +08008035 ssl->state++;
Gilles Peskine449bd832023-01-11 14:50:10 +01008036 }
Jerry Yu0b3d7c12022-02-17 14:30:51 +08008037
8038#if defined(MBEDTLS_SSL_PROTO_DTLS)
Gilles Peskine449bd832023-01-11 14:50:10 +01008039 if (ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM) {
8040 mbedtls_ssl_recv_flight_completed(ssl);
8041 }
Jerry Yu0b3d7c12022-02-17 14:30:51 +08008042#endif
8043
Gilles Peskine449bd832023-01-11 14:50:10 +01008044 MBEDTLS_SSL_DEBUG_MSG(2, ("<= parse finished"));
Jerry Yu0b3d7c12022-02-17 14:30:51 +08008045
8046exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01008047 mbedtls_platform_zeroize(buf, hash_len);
8048 return ret;
Jerry Yu0b3d7c12022-02-17 14:30:51 +08008049}
8050
Jerry Yu392112c2022-02-17 14:34:10 +08008051#if defined(MBEDTLS_SSL_CONTEXT_SERIALIZATION)
8052/*
8053 * Helper to get TLS 1.2 PRF from ciphersuite
8054 * (Duplicates bits of logic from ssl_set_handshake_prfs().)
8055 */
Gilles Peskine449bd832023-01-11 14:50:10 +01008056static tls_prf_fn ssl_tls12prf_from_cs(int ciphersuite_id)
Jerry Yu392112c2022-02-17 14:34:10 +08008057{
Jerry Yu392112c2022-02-17 14:34:10 +08008058 const mbedtls_ssl_ciphersuite_t * const ciphersuite_info =
Gilles Peskine449bd832023-01-11 14:50:10 +01008059 mbedtls_ssl_ciphersuite_from_id(ciphersuite_id);
Andrzej Kurek68327742022-10-03 06:18:18 -04008060#if defined(MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Gilles Peskine449bd832023-01-11 14:50:10 +01008061 if (ciphersuite_info != NULL && ciphersuite_info->mac == MBEDTLS_MD_SHA384) {
8062 return tls_prf_sha384;
8063 } else
Jerry Yu392112c2022-02-17 14:34:10 +08008064#endif
Andrzej Kurek894edde2022-09-29 06:31:14 -04008065#if defined(MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
8066 {
Gilles Peskine449bd832023-01-11 14:50:10 +01008067 if (ciphersuite_info != NULL && ciphersuite_info->mac == MBEDTLS_MD_SHA256) {
8068 return tls_prf_sha256;
8069 }
Andrzej Kurek894edde2022-09-29 06:31:14 -04008070 }
8071#endif
8072#if !defined(MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA) && \
8073 !defined(MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
8074 (void) ciphersuite_info;
8075#endif
Andrzej Kurekeabeb302022-10-17 07:52:51 -04008076
Gilles Peskine449bd832023-01-11 14:50:10 +01008077 return NULL;
Jerry Yu392112c2022-02-17 14:34:10 +08008078}
8079#endif /* MBEDTLS_SSL_CONTEXT_SERIALIZATION */
Jerry Yue93ffcd2022-02-17 14:37:06 +08008080
Gilles Peskine449bd832023-01-11 14:50:10 +01008081static mbedtls_tls_prf_types tls_prf_get_type(mbedtls_ssl_tls_prf_cb *tls_prf)
Jerry Yue93ffcd2022-02-17 14:37:06 +08008082{
8083 ((void) tls_prf);
Andrzej Kurek25f27152022-08-17 16:09:31 -04008084#if defined(MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Gilles Peskine449bd832023-01-11 14:50:10 +01008085 if (tls_prf == tls_prf_sha384) {
8086 return MBEDTLS_SSL_TLS_PRF_SHA384;
8087 } else
Jerry Yue93ffcd2022-02-17 14:37:06 +08008088#endif
Andrzej Kurek25f27152022-08-17 16:09:31 -04008089#if defined(MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Gilles Peskine449bd832023-01-11 14:50:10 +01008090 if (tls_prf == tls_prf_sha256) {
8091 return MBEDTLS_SSL_TLS_PRF_SHA256;
8092 } else
Jerry Yue93ffcd2022-02-17 14:37:06 +08008093#endif
Gilles Peskine449bd832023-01-11 14:50:10 +01008094 return MBEDTLS_SSL_TLS_PRF_NONE;
Jerry Yue93ffcd2022-02-17 14:37:06 +08008095}
8096
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008097/*
8098 * Populate a transform structure with session keys and all the other
8099 * necessary information.
8100 *
8101 * Parameters:
8102 * - [in/out]: transform: structure to populate
8103 * [in] must be just initialised with mbedtls_ssl_transform_init()
8104 * [out] fully populated, ready for use by mbedtls_ssl_{en,de}crypt_buf()
8105 * - [in] ciphersuite
8106 * - [in] master
8107 * - [in] encrypt_then_mac
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008108 * - [in] tls_prf: pointer to PRF to use for key derivation
8109 * - [in] randbytes: buffer holding ServerHello.random + ClientHello.random
Glenn Strauss07c64162022-03-14 12:34:51 -04008110 * - [in] tls_version: TLS version
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008111 * - [in] endpoint: client or server
8112 * - [in] ssl: used for:
8113 * - ssl->conf->{f,p}_export_keys
8114 * [in] optionally used for:
8115 * - MBEDTLS_DEBUG_C: ssl->conf->{f,p}_dbg
8116 */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02008117MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01008118static int ssl_tls12_populate_transform(mbedtls_ssl_transform *transform,
8119 int ciphersuite,
8120 const unsigned char master[48],
Neil Armstrongf2c82f02022-04-05 11:16:53 +02008121#if defined(MBEDTLS_SSL_SOME_SUITES_USE_CBC_ETM)
Gilles Peskine449bd832023-01-11 14:50:10 +01008122 int encrypt_then_mac,
Neil Armstrongf2c82f02022-04-05 11:16:53 +02008123#endif /* MBEDTLS_SSL_SOME_SUITES_USE_CBC_ETM */
Gilles Peskine449bd832023-01-11 14:50:10 +01008124 ssl_tls_prf_t tls_prf,
8125 const unsigned char randbytes[64],
8126 mbedtls_ssl_protocol_version tls_version,
8127 unsigned endpoint,
8128 const mbedtls_ssl_context *ssl)
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008129{
8130 int ret = 0;
8131 unsigned char keyblk[256];
8132 unsigned char *key1;
8133 unsigned char *key2;
8134 unsigned char *mac_enc;
8135 unsigned char *mac_dec;
8136 size_t mac_key_len = 0;
8137 size_t iv_copy_len;
8138 size_t keylen;
8139 const mbedtls_ssl_ciphersuite_t *ciphersuite_info;
Neil Armstrong7fea33e2022-04-01 15:40:25 +02008140 mbedtls_ssl_mode_t ssl_mode;
Neil Armstronge4512952022-03-08 09:08:22 +01008141#if !defined(MBEDTLS_USE_PSA_CRYPTO)
Neil Armstronga0eeb7f2022-04-01 17:36:10 +02008142 const mbedtls_cipher_info_t *cipher_info;
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008143 const mbedtls_md_info_t *md_info;
Neil Armstronge4512952022-03-08 09:08:22 +01008144#endif /* !MBEDTLS_USE_PSA_CRYPTO */
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008145
8146#if defined(MBEDTLS_USE_PSA_CRYPTO)
8147 psa_key_type_t key_type;
8148 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
8149 psa_algorithm_t alg;
Neil Armstronge4512952022-03-08 09:08:22 +01008150 psa_algorithm_t mac_alg = 0;
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008151 size_t key_bits;
8152 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
8153#endif
8154
8155#if !defined(MBEDTLS_DEBUG_C) && \
8156 !defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Gilles Peskine449bd832023-01-11 14:50:10 +01008157 if (ssl->f_export_keys == NULL) {
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008158 ssl = NULL; /* make sure we don't use it except for these cases */
8159 (void) ssl;
8160 }
8161#endif
8162
8163 /*
8164 * Some data just needs copying into the structure
8165 */
Neil Armstrongf2c82f02022-04-05 11:16:53 +02008166#if defined(MBEDTLS_SSL_SOME_SUITES_USE_CBC_ETM)
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008167 transform->encrypt_then_mac = encrypt_then_mac;
Neil Armstrongf2c82f02022-04-05 11:16:53 +02008168#endif /* MBEDTLS_SSL_SOME_SUITES_USE_CBC_ETM */
Glenn Strauss07c64162022-03-14 12:34:51 -04008169 transform->tls_version = tls_version;
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008170
8171#if defined(MBEDTLS_SSL_CONTEXT_SERIALIZATION)
Gilles Peskine449bd832023-01-11 14:50:10 +01008172 memcpy(transform->randbytes, randbytes, sizeof(transform->randbytes));
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008173#endif
8174
8175#if defined(MBEDTLS_SSL_PROTO_TLS1_3)
Gilles Peskine449bd832023-01-11 14:50:10 +01008176 if (tls_version == MBEDTLS_SSL_VERSION_TLS1_3) {
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008177 /* At the moment, we keep TLS <= 1.2 and TLS 1.3 transform
8178 * generation separate. This should never happen. */
Gilles Peskine449bd832023-01-11 14:50:10 +01008179 return MBEDTLS_ERR_SSL_INTERNAL_ERROR;
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008180 }
8181#endif /* MBEDTLS_SSL_PROTO_TLS1_3 */
8182
8183 /*
8184 * Get various info structures
8185 */
Gilles Peskine449bd832023-01-11 14:50:10 +01008186 ciphersuite_info = mbedtls_ssl_ciphersuite_from_id(ciphersuite);
8187 if (ciphersuite_info == NULL) {
8188 MBEDTLS_SSL_DEBUG_MSG(1, ("ciphersuite info for %d not found",
8189 ciphersuite));
8190 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008191 }
8192
Neil Armstrongab555e02022-04-04 11:07:59 +02008193 ssl_mode = mbedtls_ssl_get_mode_from_ciphersuite(
Neil Armstrongf2c82f02022-04-05 11:16:53 +02008194#if defined(MBEDTLS_SSL_SOME_SUITES_USE_CBC_ETM)
Gilles Peskine449bd832023-01-11 14:50:10 +01008195 encrypt_then_mac,
Neil Armstrongf2c82f02022-04-05 11:16:53 +02008196#endif /* MBEDTLS_SSL_SOME_SUITES_USE_CBC_ETM */
Gilles Peskine449bd832023-01-11 14:50:10 +01008197 ciphersuite_info);
Neil Armstrong7fea33e2022-04-01 15:40:25 +02008198
Gilles Peskine449bd832023-01-11 14:50:10 +01008199 if (ssl_mode == MBEDTLS_SSL_MODE_AEAD) {
Neil Armstronga0eeb7f2022-04-01 17:36:10 +02008200 transform->taglen =
8201 ciphersuite_info->flags & MBEDTLS_CIPHERSUITE_SHORT_TAG ? 8 : 16;
Gilles Peskine449bd832023-01-11 14:50:10 +01008202 }
Neil Armstronga0eeb7f2022-04-01 17:36:10 +02008203
8204#if defined(MBEDTLS_USE_PSA_CRYPTO)
Gilles Peskine449bd832023-01-11 14:50:10 +01008205 if ((status = mbedtls_ssl_cipher_to_psa(ciphersuite_info->cipher,
8206 transform->taglen,
8207 &alg,
8208 &key_type,
8209 &key_bits)) != PSA_SUCCESS) {
8210 ret = psa_ssl_status_to_mbedtls(status);
8211 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ssl_cipher_to_psa", ret);
Neil Armstronga0eeb7f2022-04-01 17:36:10 +02008212 goto end;
8213 }
8214#else
Gilles Peskine449bd832023-01-11 14:50:10 +01008215 cipher_info = mbedtls_cipher_info_from_type(ciphersuite_info->cipher);
8216 if (cipher_info == NULL) {
8217 MBEDTLS_SSL_DEBUG_MSG(1, ("cipher info for %u not found",
8218 ciphersuite_info->cipher));
8219 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008220 }
Neil Armstronga0eeb7f2022-04-01 17:36:10 +02008221#endif /* MBEDTLS_USE_PSA_CRYPTO */
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008222
Neil Armstronge4512952022-03-08 09:08:22 +01008223#if defined(MBEDTLS_USE_PSA_CRYPTO)
Gilles Peskine449bd832023-01-11 14:50:10 +01008224 mac_alg = mbedtls_hash_info_psa_from_md(ciphersuite_info->mac);
8225 if (mac_alg == 0) {
8226 MBEDTLS_SSL_DEBUG_MSG(1, ("mbedtls_hash_info_psa_from_md for %u not found",
8227 (unsigned) ciphersuite_info->mac));
8228 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
Neil Armstronge4512952022-03-08 09:08:22 +01008229 }
8230#else
Gilles Peskine449bd832023-01-11 14:50:10 +01008231 md_info = mbedtls_md_info_from_type(ciphersuite_info->mac);
8232 if (md_info == NULL) {
8233 MBEDTLS_SSL_DEBUG_MSG(1, ("mbedtls_md info for %u not found",
8234 (unsigned) ciphersuite_info->mac));
8235 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008236 }
Neil Armstronge4512952022-03-08 09:08:22 +01008237#endif /* MBEDTLS_USE_PSA_CRYPTO */
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008238
8239#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
8240 /* Copy own and peer's CID if the use of the CID
8241 * extension has been negotiated. */
Gilles Peskine449bd832023-01-11 14:50:10 +01008242 if (ssl->handshake->cid_in_use == MBEDTLS_SSL_CID_ENABLED) {
8243 MBEDTLS_SSL_DEBUG_MSG(3, ("Copy CIDs into SSL transform"));
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008244
8245 transform->in_cid_len = ssl->own_cid_len;
Gilles Peskine449bd832023-01-11 14:50:10 +01008246 memcpy(transform->in_cid, ssl->own_cid, ssl->own_cid_len);
8247 MBEDTLS_SSL_DEBUG_BUF(3, "Incoming CID", transform->in_cid,
8248 transform->in_cid_len);
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008249
8250 transform->out_cid_len = ssl->handshake->peer_cid_len;
Gilles Peskine449bd832023-01-11 14:50:10 +01008251 memcpy(transform->out_cid, ssl->handshake->peer_cid,
8252 ssl->handshake->peer_cid_len);
8253 MBEDTLS_SSL_DEBUG_BUF(3, "Outgoing CID", transform->out_cid,
8254 transform->out_cid_len);
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008255 }
8256#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
8257
8258 /*
8259 * Compute key block using the PRF
8260 */
Gilles Peskine449bd832023-01-11 14:50:10 +01008261 ret = tls_prf(master, 48, "key expansion", randbytes, 64, keyblk, 256);
8262 if (ret != 0) {
8263 MBEDTLS_SSL_DEBUG_RET(1, "prf", ret);
8264 return ret;
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008265 }
8266
Gilles Peskine449bd832023-01-11 14:50:10 +01008267 MBEDTLS_SSL_DEBUG_MSG(3, ("ciphersuite = %s",
8268 mbedtls_ssl_get_ciphersuite_name(ciphersuite)));
8269 MBEDTLS_SSL_DEBUG_BUF(3, "master secret", master, 48);
8270 MBEDTLS_SSL_DEBUG_BUF(4, "random bytes", randbytes, 64);
8271 MBEDTLS_SSL_DEBUG_BUF(4, "key block", keyblk, 256);
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008272
8273 /*
8274 * Determine the appropriate key, IV and MAC length.
8275 */
8276
Neil Armstronga0eeb7f2022-04-01 17:36:10 +02008277#if defined(MBEDTLS_USE_PSA_CRYPTO)
8278 keylen = PSA_BITS_TO_BYTES(key_bits);
8279#else
Gilles Peskine449bd832023-01-11 14:50:10 +01008280 keylen = mbedtls_cipher_info_get_key_bitlen(cipher_info) / 8;
Neil Armstronga0eeb7f2022-04-01 17:36:10 +02008281#endif
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008282
8283#if defined(MBEDTLS_GCM_C) || \
8284 defined(MBEDTLS_CCM_C) || \
8285 defined(MBEDTLS_CHACHAPOLY_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01008286 if (ssl_mode == MBEDTLS_SSL_MODE_AEAD) {
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008287 size_t explicit_ivlen;
8288
8289 transform->maclen = 0;
8290 mac_key_len = 0;
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008291
8292 /* All modes haves 96-bit IVs, but the length of the static parts vary
8293 * with mode and version:
8294 * - For GCM and CCM in TLS 1.2, there's a static IV of 4 Bytes
8295 * (to be concatenated with a dynamically chosen IV of 8 Bytes)
8296 * - For ChaChaPoly in TLS 1.2, and all modes in TLS 1.3, there's
8297 * a static IV of 12 Bytes (to be XOR'ed with the 8 Byte record
8298 * sequence number).
8299 */
8300 transform->ivlen = 12;
David Horstmann3b2276a2022-10-06 14:49:08 +01008301
8302 int is_chachapoly = 0;
Neil Armstronga0eeb7f2022-04-01 17:36:10 +02008303#if defined(MBEDTLS_USE_PSA_CRYPTO)
Gilles Peskine449bd832023-01-11 14:50:10 +01008304 is_chachapoly = (key_type == PSA_KEY_TYPE_CHACHA20);
Neil Armstronga0eeb7f2022-04-01 17:36:10 +02008305#else
Gilles Peskine449bd832023-01-11 14:50:10 +01008306 is_chachapoly = (mbedtls_cipher_info_get_mode(cipher_info)
8307 == MBEDTLS_MODE_CHACHAPOLY);
Neil Armstronga0eeb7f2022-04-01 17:36:10 +02008308#endif /* MBEDTLS_USE_PSA_CRYPTO */
David Horstmann3b2276a2022-10-06 14:49:08 +01008309
Gilles Peskine449bd832023-01-11 14:50:10 +01008310 if (is_chachapoly) {
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008311 transform->fixed_ivlen = 12;
Gilles Peskine449bd832023-01-11 14:50:10 +01008312 } else {
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008313 transform->fixed_ivlen = 4;
Gilles Peskine449bd832023-01-11 14:50:10 +01008314 }
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008315
8316 /* Minimum length of encrypted record */
8317 explicit_ivlen = transform->ivlen - transform->fixed_ivlen;
8318 transform->minlen = explicit_ivlen + transform->taglen;
Gilles Peskine449bd832023-01-11 14:50:10 +01008319 } else
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008320#endif /* MBEDTLS_GCM_C || MBEDTLS_CCM_C || MBEDTLS_CHACHAPOLY_C */
8321#if defined(MBEDTLS_SSL_SOME_SUITES_USE_MAC)
Gilles Peskine449bd832023-01-11 14:50:10 +01008322 if (ssl_mode == MBEDTLS_SSL_MODE_STREAM ||
Neil Armstrong7fea33e2022-04-01 15:40:25 +02008323 ssl_mode == MBEDTLS_SSL_MODE_CBC ||
Gilles Peskine449bd832023-01-11 14:50:10 +01008324 ssl_mode == MBEDTLS_SSL_MODE_CBC_ETM) {
Neil Armstronge4512952022-03-08 09:08:22 +01008325#if defined(MBEDTLS_USE_PSA_CRYPTO)
Gilles Peskine449bd832023-01-11 14:50:10 +01008326 size_t block_size = PSA_BLOCK_CIPHER_BLOCK_LENGTH(key_type);
Neil Armstronga0eeb7f2022-04-01 17:36:10 +02008327#else
8328 size_t block_size = cipher_info->block_size;
8329#endif /* MBEDTLS_USE_PSA_CRYPTO */
8330
8331#if defined(MBEDTLS_USE_PSA_CRYPTO)
Neil Armstronge4512952022-03-08 09:08:22 +01008332 /* Get MAC length */
8333 mac_key_len = PSA_HASH_LENGTH(mac_alg);
8334#else
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008335 /* Initialize HMAC contexts */
Gilles Peskine449bd832023-01-11 14:50:10 +01008336 if ((ret = mbedtls_md_setup(&transform->md_ctx_enc, md_info, 1)) != 0 ||
8337 (ret = mbedtls_md_setup(&transform->md_ctx_dec, md_info, 1)) != 0) {
8338 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_md_setup", ret);
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008339 goto end;
8340 }
8341
8342 /* Get MAC length */
Gilles Peskine449bd832023-01-11 14:50:10 +01008343 mac_key_len = mbedtls_md_get_size(md_info);
Neil Armstronge4512952022-03-08 09:08:22 +01008344#endif /* MBEDTLS_USE_PSA_CRYPTO */
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008345 transform->maclen = mac_key_len;
8346
8347 /* IV length */
Neil Armstronga0eeb7f2022-04-01 17:36:10 +02008348#if defined(MBEDTLS_USE_PSA_CRYPTO)
Gilles Peskine449bd832023-01-11 14:50:10 +01008349 transform->ivlen = PSA_CIPHER_IV_LENGTH(key_type, alg);
Neil Armstronga0eeb7f2022-04-01 17:36:10 +02008350#else
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008351 transform->ivlen = cipher_info->iv_size;
Neil Armstronga0eeb7f2022-04-01 17:36:10 +02008352#endif /* MBEDTLS_USE_PSA_CRYPTO */
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008353
8354 /* Minimum length */
Gilles Peskine449bd832023-01-11 14:50:10 +01008355 if (ssl_mode == MBEDTLS_SSL_MODE_STREAM) {
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008356 transform->minlen = transform->maclen;
Gilles Peskine449bd832023-01-11 14:50:10 +01008357 } else {
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008358 /*
8359 * GenericBlockCipher:
8360 * 1. if EtM is in use: one block plus MAC
8361 * otherwise: * first multiple of blocklen greater than maclen
8362 * 2. IV
8363 */
8364#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
Gilles Peskine449bd832023-01-11 14:50:10 +01008365 if (ssl_mode == MBEDTLS_SSL_MODE_CBC_ETM) {
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008366 transform->minlen = transform->maclen
Gilles Peskine449bd832023-01-11 14:50:10 +01008367 + block_size;
8368 } else
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008369#endif
8370 {
8371 transform->minlen = transform->maclen
Gilles Peskine449bd832023-01-11 14:50:10 +01008372 + block_size
8373 - transform->maclen % block_size;
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008374 }
8375
Gilles Peskine449bd832023-01-11 14:50:10 +01008376 if (tls_version == MBEDTLS_SSL_VERSION_TLS1_2) {
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008377 transform->minlen += transform->ivlen;
Gilles Peskine449bd832023-01-11 14:50:10 +01008378 } else {
8379 MBEDTLS_SSL_DEBUG_MSG(1, ("should never happen"));
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008380 ret = MBEDTLS_ERR_SSL_INTERNAL_ERROR;
8381 goto end;
8382 }
8383 }
Gilles Peskine449bd832023-01-11 14:50:10 +01008384 } else
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008385#endif /* MBEDTLS_SSL_SOME_SUITES_USE_MAC */
8386 {
Gilles Peskine449bd832023-01-11 14:50:10 +01008387 MBEDTLS_SSL_DEBUG_MSG(1, ("should never happen"));
8388 return MBEDTLS_ERR_SSL_INTERNAL_ERROR;
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008389 }
8390
Gilles Peskine449bd832023-01-11 14:50:10 +01008391 MBEDTLS_SSL_DEBUG_MSG(3, ("keylen: %u, minlen: %u, ivlen: %u, maclen: %u",
8392 (unsigned) keylen,
8393 (unsigned) transform->minlen,
8394 (unsigned) transform->ivlen,
8395 (unsigned) transform->maclen));
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008396
8397 /*
8398 * Finally setup the cipher contexts, IVs and MAC secrets.
8399 */
8400#if defined(MBEDTLS_SSL_CLI_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01008401 if (endpoint == MBEDTLS_SSL_IS_CLIENT) {
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008402 key1 = keyblk + mac_key_len * 2;
8403 key2 = keyblk + mac_key_len * 2 + keylen;
8404
8405 mac_enc = keyblk;
8406 mac_dec = keyblk + mac_key_len;
8407
Gilles Peskine449bd832023-01-11 14:50:10 +01008408 iv_copy_len = (transform->fixed_ivlen) ?
8409 transform->fixed_ivlen : transform->ivlen;
8410 memcpy(transform->iv_enc, key2 + keylen, iv_copy_len);
8411 memcpy(transform->iv_dec, key2 + keylen + iv_copy_len,
8412 iv_copy_len);
8413 } else
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008414#endif /* MBEDTLS_SSL_CLI_C */
8415#if defined(MBEDTLS_SSL_SRV_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01008416 if (endpoint == MBEDTLS_SSL_IS_SERVER) {
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008417 key1 = keyblk + mac_key_len * 2 + keylen;
8418 key2 = keyblk + mac_key_len * 2;
8419
8420 mac_enc = keyblk + mac_key_len;
8421 mac_dec = keyblk;
8422
Gilles Peskine449bd832023-01-11 14:50:10 +01008423 iv_copy_len = (transform->fixed_ivlen) ?
8424 transform->fixed_ivlen : transform->ivlen;
8425 memcpy(transform->iv_dec, key1 + keylen, iv_copy_len);
8426 memcpy(transform->iv_enc, key1 + keylen + iv_copy_len,
8427 iv_copy_len);
8428 } else
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008429#endif /* MBEDTLS_SSL_SRV_C */
8430 {
Gilles Peskine449bd832023-01-11 14:50:10 +01008431 MBEDTLS_SSL_DEBUG_MSG(1, ("should never happen"));
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008432 ret = MBEDTLS_ERR_SSL_INTERNAL_ERROR;
8433 goto end;
8434 }
8435
Gilles Peskine449bd832023-01-11 14:50:10 +01008436 if (ssl != NULL && ssl->f_export_keys != NULL) {
8437 ssl->f_export_keys(ssl->p_export_keys,
8438 MBEDTLS_SSL_KEY_EXPORT_TLS12_MASTER_SECRET,
8439 master, 48,
8440 randbytes + 32,
8441 randbytes,
8442 tls_prf_get_type(tls_prf));
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008443 }
8444
8445#if defined(MBEDTLS_USE_PSA_CRYPTO)
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008446 transform->psa_alg = alg;
8447
Gilles Peskine449bd832023-01-11 14:50:10 +01008448 if (alg != MBEDTLS_SSL_NULL_CIPHER) {
8449 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_ENCRYPT);
8450 psa_set_key_algorithm(&attributes, alg);
8451 psa_set_key_type(&attributes, key_type);
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008452
Gilles Peskine449bd832023-01-11 14:50:10 +01008453 if ((status = psa_import_key(&attributes,
8454 key1,
8455 PSA_BITS_TO_BYTES(key_bits),
8456 &transform->psa_key_enc)) != PSA_SUCCESS) {
8457 MBEDTLS_SSL_DEBUG_RET(3, "psa_import_key", (int) status);
8458 ret = psa_ssl_status_to_mbedtls(status);
8459 MBEDTLS_SSL_DEBUG_RET(1, "psa_import_key", ret);
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008460 goto end;
8461 }
8462
Gilles Peskine449bd832023-01-11 14:50:10 +01008463 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_DECRYPT);
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008464
Gilles Peskine449bd832023-01-11 14:50:10 +01008465 if ((status = psa_import_key(&attributes,
8466 key2,
8467 PSA_BITS_TO_BYTES(key_bits),
8468 &transform->psa_key_dec)) != PSA_SUCCESS) {
8469 ret = psa_ssl_status_to_mbedtls(status);
8470 MBEDTLS_SSL_DEBUG_RET(1, "psa_import_key", ret);
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008471 goto end;
8472 }
8473 }
8474#else
Gilles Peskine449bd832023-01-11 14:50:10 +01008475 if ((ret = mbedtls_cipher_setup(&transform->cipher_ctx_enc,
8476 cipher_info)) != 0) {
8477 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_cipher_setup", ret);
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008478 goto end;
8479 }
8480
Gilles Peskine449bd832023-01-11 14:50:10 +01008481 if ((ret = mbedtls_cipher_setup(&transform->cipher_ctx_dec,
8482 cipher_info)) != 0) {
8483 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_cipher_setup", ret);
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008484 goto end;
8485 }
8486
Gilles Peskine449bd832023-01-11 14:50:10 +01008487 if ((ret = mbedtls_cipher_setkey(&transform->cipher_ctx_enc, key1,
8488 (int) mbedtls_cipher_info_get_key_bitlen(cipher_info),
8489 MBEDTLS_ENCRYPT)) != 0) {
8490 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_cipher_setkey", ret);
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008491 goto end;
8492 }
8493
Gilles Peskine449bd832023-01-11 14:50:10 +01008494 if ((ret = mbedtls_cipher_setkey(&transform->cipher_ctx_dec, key2,
8495 (int) mbedtls_cipher_info_get_key_bitlen(cipher_info),
8496 MBEDTLS_DECRYPT)) != 0) {
8497 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_cipher_setkey", ret);
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008498 goto end;
8499 }
8500
8501#if defined(MBEDTLS_CIPHER_MODE_CBC)
Gilles Peskine449bd832023-01-11 14:50:10 +01008502 if (mbedtls_cipher_info_get_mode(cipher_info) == MBEDTLS_MODE_CBC) {
8503 if ((ret = mbedtls_cipher_set_padding_mode(&transform->cipher_ctx_enc,
8504 MBEDTLS_PADDING_NONE)) != 0) {
8505 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_cipher_set_padding_mode", ret);
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008506 goto end;
8507 }
8508
Gilles Peskine449bd832023-01-11 14:50:10 +01008509 if ((ret = mbedtls_cipher_set_padding_mode(&transform->cipher_ctx_dec,
8510 MBEDTLS_PADDING_NONE)) != 0) {
8511 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_cipher_set_padding_mode", ret);
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008512 goto end;
8513 }
8514 }
8515#endif /* MBEDTLS_CIPHER_MODE_CBC */
8516#endif /* MBEDTLS_USE_PSA_CRYPTO */
8517
Neil Armstrong29c0c042022-03-17 17:47:28 +01008518#if defined(MBEDTLS_SSL_SOME_SUITES_USE_MAC)
8519 /* For HMAC-based ciphersuites, initialize the HMAC transforms.
8520 For AEAD-based ciphersuites, there is nothing to do here. */
Gilles Peskine449bd832023-01-11 14:50:10 +01008521 if (mac_key_len != 0) {
Neil Armstrong29c0c042022-03-17 17:47:28 +01008522#if defined(MBEDTLS_USE_PSA_CRYPTO)
Gilles Peskine449bd832023-01-11 14:50:10 +01008523 transform->psa_mac_alg = PSA_ALG_HMAC(mac_alg);
Neil Armstrong29c0c042022-03-17 17:47:28 +01008524
Gilles Peskine449bd832023-01-11 14:50:10 +01008525 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_SIGN_MESSAGE);
8526 psa_set_key_algorithm(&attributes, PSA_ALG_HMAC(mac_alg));
8527 psa_set_key_type(&attributes, PSA_KEY_TYPE_HMAC);
Neil Armstrong29c0c042022-03-17 17:47:28 +01008528
Gilles Peskine449bd832023-01-11 14:50:10 +01008529 if ((status = psa_import_key(&attributes,
8530 mac_enc, mac_key_len,
8531 &transform->psa_mac_enc)) != PSA_SUCCESS) {
8532 ret = psa_ssl_status_to_mbedtls(status);
8533 MBEDTLS_SSL_DEBUG_RET(1, "psa_import_mac_key", ret);
Neil Armstrong29c0c042022-03-17 17:47:28 +01008534 goto end;
8535 }
8536
Gilles Peskine449bd832023-01-11 14:50:10 +01008537 if ((transform->psa_alg == MBEDTLS_SSL_NULL_CIPHER) ||
8538 ((transform->psa_alg == PSA_ALG_CBC_NO_PADDING)
Andrzej Kurek8c95ac42022-08-17 16:17:00 -04008539#if defined(MBEDTLS_SSL_SOME_SUITES_USE_CBC_ETM)
Gilles Peskine449bd832023-01-11 14:50:10 +01008540 && (transform->encrypt_then_mac == MBEDTLS_SSL_ETM_DISABLED)
Andrzej Kurek8c95ac42022-08-17 16:17:00 -04008541#endif
Gilles Peskine449bd832023-01-11 14:50:10 +01008542 )) {
Neil Armstrong29c0c042022-03-17 17:47:28 +01008543 /* mbedtls_ct_hmac() requires the key to be exportable */
Gilles Peskine449bd832023-01-11 14:50:10 +01008544 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_EXPORT |
8545 PSA_KEY_USAGE_VERIFY_HASH);
8546 } else {
8547 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_VERIFY_HASH);
8548 }
Neil Armstrong29c0c042022-03-17 17:47:28 +01008549
Gilles Peskine449bd832023-01-11 14:50:10 +01008550 if ((status = psa_import_key(&attributes,
8551 mac_dec, mac_key_len,
8552 &transform->psa_mac_dec)) != PSA_SUCCESS) {
8553 ret = psa_ssl_status_to_mbedtls(status);
8554 MBEDTLS_SSL_DEBUG_RET(1, "psa_import_mac_key", ret);
Neil Armstrong29c0c042022-03-17 17:47:28 +01008555 goto end;
8556 }
8557#else
Gilles Peskine449bd832023-01-11 14:50:10 +01008558 ret = mbedtls_md_hmac_starts(&transform->md_ctx_enc, mac_enc, mac_key_len);
8559 if (ret != 0) {
Neil Armstrong29c0c042022-03-17 17:47:28 +01008560 goto end;
Gilles Peskine449bd832023-01-11 14:50:10 +01008561 }
8562 ret = mbedtls_md_hmac_starts(&transform->md_ctx_dec, mac_dec, mac_key_len);
8563 if (ret != 0) {
Neil Armstrong29c0c042022-03-17 17:47:28 +01008564 goto end;
Gilles Peskine449bd832023-01-11 14:50:10 +01008565 }
Neil Armstrong29c0c042022-03-17 17:47:28 +01008566#endif /* MBEDTLS_USE_PSA_CRYPTO */
8567 }
8568#endif /* MBEDTLS_SSL_SOME_SUITES_USE_MAC */
8569
8570 ((void) mac_dec);
8571 ((void) mac_enc);
8572
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008573end:
Gilles Peskine449bd832023-01-11 14:50:10 +01008574 mbedtls_platform_zeroize(keyblk, sizeof(keyblk));
8575 return ret;
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008576}
8577
Valerio Settia08b1a42022-11-17 15:10:02 +01008578#if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED) && \
8579 defined(MBEDTLS_USE_PSA_CRYPTO)
Valerio Setti6b3dab02022-11-17 17:14:54 +01008580int mbedtls_psa_ecjpake_read_round(
Gilles Peskine449bd832023-01-11 14:50:10 +01008581 psa_pake_operation_t *pake_ctx,
8582 const unsigned char *buf,
8583 size_t len, mbedtls_ecjpake_rounds_t round)
Valerio Settia08b1a42022-11-17 15:10:02 +01008584{
8585 psa_status_t status;
8586 size_t input_offset = 0;
Valerio Setti819de862022-11-17 18:05:19 +01008587 /*
Valerio Setti6b3dab02022-11-17 17:14:54 +01008588 * At round one repeat the KEY_SHARE, ZK_PUBLIC & ZF_PROOF twice
8589 * At round two perform a single cycle
8590 */
Gilles Peskine449bd832023-01-11 14:50:10 +01008591 unsigned int remaining_steps = (round == MBEDTLS_ECJPAKE_ROUND_ONE) ? 2 : 1;
Valerio Settia08b1a42022-11-17 15:10:02 +01008592
Gilles Peskine449bd832023-01-11 14:50:10 +01008593 for (; remaining_steps > 0; remaining_steps--) {
8594 for (psa_pake_step_t step = PSA_PAKE_STEP_KEY_SHARE;
Valerio Settia08b1a42022-11-17 15:10:02 +01008595 step <= PSA_PAKE_STEP_ZK_PROOF;
Gilles Peskine449bd832023-01-11 14:50:10 +01008596 ++step) {
Valerio Settia08b1a42022-11-17 15:10:02 +01008597 /* Length is stored at the first byte */
8598 size_t length = buf[input_offset];
8599 input_offset += 1;
8600
Gilles Peskine449bd832023-01-11 14:50:10 +01008601 if (input_offset + length > len) {
Valerio Settia08b1a42022-11-17 15:10:02 +01008602 return MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE;
8603 }
8604
Gilles Peskine449bd832023-01-11 14:50:10 +01008605 status = psa_pake_input(pake_ctx, step,
8606 buf + input_offset, length);
8607 if (status != PSA_SUCCESS) {
8608 return psa_ssl_status_to_mbedtls(status);
Valerio Settia08b1a42022-11-17 15:10:02 +01008609 }
8610
8611 input_offset += length;
8612 }
8613 }
8614
Gilles Peskine449bd832023-01-11 14:50:10 +01008615 if (input_offset != len) {
Valerio Setti61ea17d2022-11-18 12:11:00 +01008616 return MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE;
Gilles Peskine449bd832023-01-11 14:50:10 +01008617 }
Valerio Setti30ebe112022-11-17 16:23:34 +01008618
Gilles Peskine449bd832023-01-11 14:50:10 +01008619 return 0;
Valerio Settia08b1a42022-11-17 15:10:02 +01008620}
8621
Valerio Setti6b3dab02022-11-17 17:14:54 +01008622int mbedtls_psa_ecjpake_write_round(
Gilles Peskine449bd832023-01-11 14:50:10 +01008623 psa_pake_operation_t *pake_ctx,
8624 unsigned char *buf,
8625 size_t len, size_t *olen,
8626 mbedtls_ecjpake_rounds_t round)
Valerio Settia08b1a42022-11-17 15:10:02 +01008627{
8628 psa_status_t status;
8629 size_t output_offset = 0;
8630 size_t output_len;
Valerio Setti819de862022-11-17 18:05:19 +01008631 /*
Valerio Setti6b3dab02022-11-17 17:14:54 +01008632 * At round one repeat the KEY_SHARE, ZK_PUBLIC & ZF_PROOF twice
8633 * At round two perform a single cycle
8634 */
Gilles Peskine449bd832023-01-11 14:50:10 +01008635 unsigned int remaining_steps = (round == MBEDTLS_ECJPAKE_ROUND_ONE) ? 2 : 1;
Valerio Settia08b1a42022-11-17 15:10:02 +01008636
Gilles Peskine449bd832023-01-11 14:50:10 +01008637 for (; remaining_steps > 0; remaining_steps--) {
8638 for (psa_pake_step_t step = PSA_PAKE_STEP_KEY_SHARE;
8639 step <= PSA_PAKE_STEP_ZK_PROOF;
8640 ++step) {
Valerio Setti79f6b6b2022-11-21 14:17:03 +01008641 /*
Valerio Settid4a9b1a2022-11-22 11:11:10 +01008642 * For each step, prepend 1 byte with the length of the data as
8643 * given by psa_pake_output().
Valerio Setti79f6b6b2022-11-21 14:17:03 +01008644 */
Gilles Peskine449bd832023-01-11 14:50:10 +01008645 status = psa_pake_output(pake_ctx, step,
8646 buf + output_offset + 1,
8647 len - output_offset - 1,
8648 &output_len);
8649 if (status != PSA_SUCCESS) {
8650 return psa_ssl_status_to_mbedtls(status);
Valerio Settia08b1a42022-11-17 15:10:02 +01008651 }
8652
Valerio Setti99d88c12022-11-22 16:03:43 +01008653 *(buf + output_offset) = (uint8_t) output_len;
Valerio Setti79f6b6b2022-11-21 14:17:03 +01008654
8655 output_offset += output_len + 1;
Valerio Settia08b1a42022-11-17 15:10:02 +01008656 }
8657 }
8658
8659 *olen = output_offset;
8660
Gilles Peskine449bd832023-01-11 14:50:10 +01008661 return 0;
Valerio Settia08b1a42022-11-17 15:10:02 +01008662}
Valerio Settia08b1a42022-11-17 15:10:02 +01008663#endif //MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED && MBEDTLS_USE_PSA_CRYPTO
8664
Jerry Yuee40f9d2022-02-17 14:55:16 +08008665#if defined(MBEDTLS_USE_PSA_CRYPTO)
Gilles Peskine449bd832023-01-11 14:50:10 +01008666int mbedtls_ssl_get_key_exchange_md_tls1_2(mbedtls_ssl_context *ssl,
8667 unsigned char *hash, size_t *hashlen,
8668 unsigned char *data, size_t data_len,
8669 mbedtls_md_type_t md_alg)
Jerry Yuee40f9d2022-02-17 14:55:16 +08008670{
8671 psa_status_t status;
8672 psa_hash_operation_t hash_operation = PSA_HASH_OPERATION_INIT;
Gilles Peskine449bd832023-01-11 14:50:10 +01008673 psa_algorithm_t hash_alg = mbedtls_hash_info_psa_from_md(md_alg);
Jerry Yuee40f9d2022-02-17 14:55:16 +08008674
Gilles Peskine449bd832023-01-11 14:50:10 +01008675 MBEDTLS_SSL_DEBUG_MSG(3, ("Perform PSA-based computation of digest of ServerKeyExchange"));
Jerry Yuee40f9d2022-02-17 14:55:16 +08008676
Gilles Peskine449bd832023-01-11 14:50:10 +01008677 if ((status = psa_hash_setup(&hash_operation,
8678 hash_alg)) != PSA_SUCCESS) {
8679 MBEDTLS_SSL_DEBUG_RET(1, "psa_hash_setup", status);
Jerry Yuee40f9d2022-02-17 14:55:16 +08008680 goto exit;
8681 }
8682
Gilles Peskine449bd832023-01-11 14:50:10 +01008683 if ((status = psa_hash_update(&hash_operation, ssl->handshake->randbytes,
8684 64)) != PSA_SUCCESS) {
8685 MBEDTLS_SSL_DEBUG_RET(1, "psa_hash_update", status);
Jerry Yuee40f9d2022-02-17 14:55:16 +08008686 goto exit;
8687 }
8688
Gilles Peskine449bd832023-01-11 14:50:10 +01008689 if ((status = psa_hash_update(&hash_operation,
8690 data, data_len)) != PSA_SUCCESS) {
8691 MBEDTLS_SSL_DEBUG_RET(1, "psa_hash_update", status);
Jerry Yuee40f9d2022-02-17 14:55:16 +08008692 goto exit;
8693 }
8694
Gilles Peskine449bd832023-01-11 14:50:10 +01008695 if ((status = psa_hash_finish(&hash_operation, hash, PSA_HASH_MAX_SIZE,
8696 hashlen)) != PSA_SUCCESS) {
8697 MBEDTLS_SSL_DEBUG_RET(1, "psa_hash_finish", status);
8698 goto exit;
Jerry Yuee40f9d2022-02-17 14:55:16 +08008699 }
8700
8701exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01008702 if (status != PSA_SUCCESS) {
8703 mbedtls_ssl_send_alert_message(ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
8704 MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR);
8705 switch (status) {
Jerry Yuee40f9d2022-02-17 14:55:16 +08008706 case PSA_ERROR_NOT_SUPPORTED:
Gilles Peskine449bd832023-01-11 14:50:10 +01008707 return MBEDTLS_ERR_MD_FEATURE_UNAVAILABLE;
Jerry Yuee40f9d2022-02-17 14:55:16 +08008708 case PSA_ERROR_BAD_STATE: /* Intentional fallthrough */
8709 case PSA_ERROR_BUFFER_TOO_SMALL:
Gilles Peskine449bd832023-01-11 14:50:10 +01008710 return MBEDTLS_ERR_MD_BAD_INPUT_DATA;
Jerry Yuee40f9d2022-02-17 14:55:16 +08008711 case PSA_ERROR_INSUFFICIENT_MEMORY:
Gilles Peskine449bd832023-01-11 14:50:10 +01008712 return MBEDTLS_ERR_MD_ALLOC_FAILED;
Jerry Yuee40f9d2022-02-17 14:55:16 +08008713 default:
Gilles Peskine449bd832023-01-11 14:50:10 +01008714 return MBEDTLS_ERR_PLATFORM_HW_ACCEL_FAILED;
Jerry Yuee40f9d2022-02-17 14:55:16 +08008715 }
8716 }
Gilles Peskine449bd832023-01-11 14:50:10 +01008717 return 0;
Jerry Yuee40f9d2022-02-17 14:55:16 +08008718}
8719
8720#else
8721
Gilles Peskine449bd832023-01-11 14:50:10 +01008722int mbedtls_ssl_get_key_exchange_md_tls1_2(mbedtls_ssl_context *ssl,
8723 unsigned char *hash, size_t *hashlen,
8724 unsigned char *data, size_t data_len,
8725 mbedtls_md_type_t md_alg)
Jerry Yuee40f9d2022-02-17 14:55:16 +08008726{
8727 int ret = 0;
8728 mbedtls_md_context_t ctx;
Gilles Peskine449bd832023-01-11 14:50:10 +01008729 const mbedtls_md_info_t *md_info = mbedtls_md_info_from_type(md_alg);
8730 *hashlen = mbedtls_md_get_size(md_info);
Jerry Yuee40f9d2022-02-17 14:55:16 +08008731
Gilles Peskine449bd832023-01-11 14:50:10 +01008732 MBEDTLS_SSL_DEBUG_MSG(3, ("Perform mbedtls-based computation of digest of ServerKeyExchange"));
Jerry Yuee40f9d2022-02-17 14:55:16 +08008733
Gilles Peskine449bd832023-01-11 14:50:10 +01008734 mbedtls_md_init(&ctx);
Jerry Yuee40f9d2022-02-17 14:55:16 +08008735
8736 /*
8737 * digitally-signed struct {
8738 * opaque client_random[32];
8739 * opaque server_random[32];
8740 * ServerDHParams params;
8741 * };
8742 */
Gilles Peskine449bd832023-01-11 14:50:10 +01008743 if ((ret = mbedtls_md_setup(&ctx, md_info, 0)) != 0) {
8744 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_md_setup", ret);
Jerry Yuee40f9d2022-02-17 14:55:16 +08008745 goto exit;
8746 }
Gilles Peskine449bd832023-01-11 14:50:10 +01008747 if ((ret = mbedtls_md_starts(&ctx)) != 0) {
8748 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_md_starts", ret);
Jerry Yuee40f9d2022-02-17 14:55:16 +08008749 goto exit;
8750 }
Gilles Peskine449bd832023-01-11 14:50:10 +01008751 if ((ret = mbedtls_md_update(&ctx, ssl->handshake->randbytes, 64)) != 0) {
8752 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_md_update", ret);
Jerry Yuee40f9d2022-02-17 14:55:16 +08008753 goto exit;
8754 }
Gilles Peskine449bd832023-01-11 14:50:10 +01008755 if ((ret = mbedtls_md_update(&ctx, data, data_len)) != 0) {
8756 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_md_update", ret);
Jerry Yuee40f9d2022-02-17 14:55:16 +08008757 goto exit;
8758 }
Gilles Peskine449bd832023-01-11 14:50:10 +01008759 if ((ret = mbedtls_md_finish(&ctx, hash)) != 0) {
8760 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_md_finish", ret);
Jerry Yuee40f9d2022-02-17 14:55:16 +08008761 goto exit;
8762 }
8763
8764exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01008765 mbedtls_md_free(&ctx);
Jerry Yuee40f9d2022-02-17 14:55:16 +08008766
Gilles Peskine449bd832023-01-11 14:50:10 +01008767 if (ret != 0) {
8768 mbedtls_ssl_send_alert_message(ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
8769 MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR);
8770 }
Jerry Yuee40f9d2022-02-17 14:55:16 +08008771
Gilles Peskine449bd832023-01-11 14:50:10 +01008772 return ret;
Jerry Yuee40f9d2022-02-17 14:55:16 +08008773}
8774#endif /* MBEDTLS_USE_PSA_CRYPTO */
8775
Jerry Yud9d91da2022-02-17 14:57:06 +08008776#if defined(MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED)
8777
Gabor Mezeia3d016c2022-05-10 12:44:09 +02008778/* Find the preferred hash for a given signature algorithm. */
8779unsigned int mbedtls_ssl_tls12_get_preferred_hash_for_sig_alg(
Gilles Peskine449bd832023-01-11 14:50:10 +01008780 mbedtls_ssl_context *ssl,
8781 unsigned int sig_alg)
Jerry Yud9d91da2022-02-17 14:57:06 +08008782{
Gabor Mezei078e8032022-04-27 21:17:56 +02008783 unsigned int i;
Gabor Mezeia3d016c2022-05-10 12:44:09 +02008784 uint16_t *received_sig_algs = ssl->handshake->received_sig_algs;
Gabor Mezei078e8032022-04-27 21:17:56 +02008785
Gilles Peskine449bd832023-01-11 14:50:10 +01008786 if (sig_alg == MBEDTLS_SSL_SIG_ANON) {
8787 return MBEDTLS_SSL_HASH_NONE;
8788 }
Gabor Mezei078e8032022-04-27 21:17:56 +02008789
Gilles Peskine449bd832023-01-11 14:50:10 +01008790 for (i = 0; received_sig_algs[i] != MBEDTLS_TLS_SIG_NONE; i++) {
Neil Armstrong9f1176a2022-06-24 18:19:19 +02008791 unsigned int hash_alg_received =
Gilles Peskine449bd832023-01-11 14:50:10 +01008792 MBEDTLS_SSL_TLS12_HASH_ALG_FROM_SIG_AND_HASH_ALG(
8793 received_sig_algs[i]);
Neil Armstrong9f1176a2022-06-24 18:19:19 +02008794 unsigned int sig_alg_received =
Gilles Peskine449bd832023-01-11 14:50:10 +01008795 MBEDTLS_SSL_TLS12_SIG_ALG_FROM_SIG_AND_HASH_ALG(
8796 received_sig_algs[i]);
Neil Armstrong9f1176a2022-06-24 18:19:19 +02008797
Gilles Peskine449bd832023-01-11 14:50:10 +01008798 if (sig_alg == sig_alg_received) {
Neil Armstrong9f1176a2022-06-24 18:19:19 +02008799#if defined(MBEDTLS_USE_PSA_CRYPTO)
Gilles Peskine449bd832023-01-11 14:50:10 +01008800 if (ssl->handshake->key_cert && ssl->handshake->key_cert->key) {
Neil Armstrong96eceb82022-06-30 18:05:05 +02008801 psa_algorithm_t psa_hash_alg =
Gilles Peskine449bd832023-01-11 14:50:10 +01008802 mbedtls_hash_info_psa_from_md(hash_alg_received);
Neil Armstrong9f1176a2022-06-24 18:19:19 +02008803
Gilles Peskine449bd832023-01-11 14:50:10 +01008804 if (sig_alg_received == MBEDTLS_SSL_SIG_ECDSA &&
8805 !mbedtls_pk_can_do_ext(ssl->handshake->key_cert->key,
8806 PSA_ALG_ECDSA(psa_hash_alg),
8807 PSA_KEY_USAGE_SIGN_HASH)) {
Neil Armstrong9f1176a2022-06-24 18:19:19 +02008808 continue;
Gilles Peskine449bd832023-01-11 14:50:10 +01008809 }
Neil Armstrong9f1176a2022-06-24 18:19:19 +02008810
Gilles Peskine449bd832023-01-11 14:50:10 +01008811 if (sig_alg_received == MBEDTLS_SSL_SIG_RSA &&
8812 !mbedtls_pk_can_do_ext(ssl->handshake->key_cert->key,
8813 PSA_ALG_RSA_PKCS1V15_SIGN(
8814 psa_hash_alg),
8815 PSA_KEY_USAGE_SIGN_HASH)) {
Neil Armstrong9f1176a2022-06-24 18:19:19 +02008816 continue;
Gilles Peskine449bd832023-01-11 14:50:10 +01008817 }
Neil Armstrong9f1176a2022-06-24 18:19:19 +02008818 }
Neil Armstrong9f1176a2022-06-24 18:19:19 +02008819#endif /* MBEDTLS_USE_PSA_CRYPTO */
Neil Armstrong96eceb82022-06-30 18:05:05 +02008820
Gilles Peskine449bd832023-01-11 14:50:10 +01008821 return hash_alg_received;
Neil Armstrong9f1176a2022-06-24 18:19:19 +02008822 }
Jerry Yud9d91da2022-02-17 14:57:06 +08008823 }
Jerry Yud9d91da2022-02-17 14:57:06 +08008824
Gilles Peskine449bd832023-01-11 14:50:10 +01008825 return MBEDTLS_SSL_HASH_NONE;
Jerry Yud9d91da2022-02-17 14:57:06 +08008826}
8827
8828#endif /* MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED */
8829
Jerry Yu4f9e3ef2022-02-17 14:58:27 +08008830/* Serialization of TLS 1.2 sessions:
8831 *
8832 * struct {
8833 * uint64 start_time;
8834 * uint8 ciphersuite[2]; // defined by the standard
Jerry Yu4f9e3ef2022-02-17 14:58:27 +08008835 * uint8 session_id_len; // at most 32
8836 * opaque session_id[32];
8837 * opaque master[48]; // fixed length in the standard
8838 * uint32 verify_result;
8839 * opaque peer_cert<0..2^24-1>; // length 0 means no peer cert
8840 * opaque ticket<0..2^24-1>; // length 0 means no ticket
8841 * uint32 ticket_lifetime;
8842 * uint8 mfl_code; // up to 255 according to standard
8843 * uint8 encrypt_then_mac; // 0 or 1
8844 * } serialized_session_tls12;
8845 *
8846 */
Gilles Peskine449bd832023-01-11 14:50:10 +01008847static size_t ssl_tls12_session_save(const mbedtls_ssl_session *session,
8848 unsigned char *buf,
8849 size_t buf_len)
Jerry Yu4f9e3ef2022-02-17 14:58:27 +08008850{
8851 unsigned char *p = buf;
8852 size_t used = 0;
8853
8854#if defined(MBEDTLS_HAVE_TIME)
8855 uint64_t start;
8856#endif
8857#if defined(MBEDTLS_X509_CRT_PARSE_C)
8858#if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
8859 size_t cert_len;
8860#endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
8861#endif /* MBEDTLS_X509_CRT_PARSE_C */
8862
8863 /*
8864 * Time
8865 */
8866#if defined(MBEDTLS_HAVE_TIME)
8867 used += 8;
8868
Gilles Peskine449bd832023-01-11 14:50:10 +01008869 if (used <= buf_len) {
Jerry Yu4f9e3ef2022-02-17 14:58:27 +08008870 start = (uint64_t) session->start;
8871
Gilles Peskine449bd832023-01-11 14:50:10 +01008872 MBEDTLS_PUT_UINT64_BE(start, p, 0);
Jerry Yu4f9e3ef2022-02-17 14:58:27 +08008873 p += 8;
8874 }
8875#endif /* MBEDTLS_HAVE_TIME */
8876
8877 /*
8878 * Basic mandatory fields
8879 */
8880 used += 2 /* ciphersuite */
Gilles Peskine449bd832023-01-11 14:50:10 +01008881 + 1 /* id_len */
8882 + sizeof(session->id)
8883 + sizeof(session->master)
8884 + 4; /* verify_result */
Jerry Yu4f9e3ef2022-02-17 14:58:27 +08008885
Gilles Peskine449bd832023-01-11 14:50:10 +01008886 if (used <= buf_len) {
8887 MBEDTLS_PUT_UINT16_BE(session->ciphersuite, p, 0);
Jerry Yu4f9e3ef2022-02-17 14:58:27 +08008888 p += 2;
8889
Gilles Peskine449bd832023-01-11 14:50:10 +01008890 *p++ = MBEDTLS_BYTE_0(session->id_len);
8891 memcpy(p, session->id, 32);
Jerry Yu4f9e3ef2022-02-17 14:58:27 +08008892 p += 32;
8893
Gilles Peskine449bd832023-01-11 14:50:10 +01008894 memcpy(p, session->master, 48);
Jerry Yu4f9e3ef2022-02-17 14:58:27 +08008895 p += 48;
8896
Gilles Peskine449bd832023-01-11 14:50:10 +01008897 MBEDTLS_PUT_UINT32_BE(session->verify_result, p, 0);
Jerry Yu4f9e3ef2022-02-17 14:58:27 +08008898 p += 4;
8899 }
8900
8901 /*
8902 * Peer's end-entity certificate
8903 */
8904#if defined(MBEDTLS_X509_CRT_PARSE_C)
8905#if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
Gilles Peskine449bd832023-01-11 14:50:10 +01008906 if (session->peer_cert == NULL) {
Jerry Yu4f9e3ef2022-02-17 14:58:27 +08008907 cert_len = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +01008908 } else {
Jerry Yu4f9e3ef2022-02-17 14:58:27 +08008909 cert_len = session->peer_cert->raw.len;
Gilles Peskine449bd832023-01-11 14:50:10 +01008910 }
Jerry Yu4f9e3ef2022-02-17 14:58:27 +08008911
8912 used += 3 + cert_len;
8913
Gilles Peskine449bd832023-01-11 14:50:10 +01008914 if (used <= buf_len) {
8915 *p++ = MBEDTLS_BYTE_2(cert_len);
8916 *p++ = MBEDTLS_BYTE_1(cert_len);
8917 *p++ = MBEDTLS_BYTE_0(cert_len);
Jerry Yu4f9e3ef2022-02-17 14:58:27 +08008918
Gilles Peskine449bd832023-01-11 14:50:10 +01008919 if (session->peer_cert != NULL) {
8920 memcpy(p, session->peer_cert->raw.p, cert_len);
Jerry Yu4f9e3ef2022-02-17 14:58:27 +08008921 p += cert_len;
8922 }
8923 }
8924#else /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
Gilles Peskine449bd832023-01-11 14:50:10 +01008925 if (session->peer_cert_digest != NULL) {
Jerry Yu4f9e3ef2022-02-17 14:58:27 +08008926 used += 1 /* type */ + 1 /* length */ + session->peer_cert_digest_len;
Gilles Peskine449bd832023-01-11 14:50:10 +01008927 if (used <= buf_len) {
Jerry Yu4f9e3ef2022-02-17 14:58:27 +08008928 *p++ = (unsigned char) session->peer_cert_digest_type;
8929 *p++ = (unsigned char) session->peer_cert_digest_len;
Gilles Peskine449bd832023-01-11 14:50:10 +01008930 memcpy(p, session->peer_cert_digest,
8931 session->peer_cert_digest_len);
Jerry Yu4f9e3ef2022-02-17 14:58:27 +08008932 p += session->peer_cert_digest_len;
8933 }
Gilles Peskine449bd832023-01-11 14:50:10 +01008934 } else {
Jerry Yu4f9e3ef2022-02-17 14:58:27 +08008935 used += 2;
Gilles Peskine449bd832023-01-11 14:50:10 +01008936 if (used <= buf_len) {
Jerry Yu4f9e3ef2022-02-17 14:58:27 +08008937 *p++ = (unsigned char) MBEDTLS_MD_NONE;
8938 *p++ = 0;
8939 }
8940 }
8941#endif /* !MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
8942#endif /* MBEDTLS_X509_CRT_PARSE_C */
8943
8944 /*
8945 * Session ticket if any, plus associated data
8946 */
8947#if defined(MBEDTLS_SSL_SESSION_TICKETS) && defined(MBEDTLS_SSL_CLI_C)
8948 used += 3 + session->ticket_len + 4; /* len + ticket + lifetime */
8949
Gilles Peskine449bd832023-01-11 14:50:10 +01008950 if (used <= buf_len) {
8951 *p++ = MBEDTLS_BYTE_2(session->ticket_len);
8952 *p++ = MBEDTLS_BYTE_1(session->ticket_len);
8953 *p++ = MBEDTLS_BYTE_0(session->ticket_len);
Jerry Yu4f9e3ef2022-02-17 14:58:27 +08008954
Gilles Peskine449bd832023-01-11 14:50:10 +01008955 if (session->ticket != NULL) {
8956 memcpy(p, session->ticket, session->ticket_len);
Jerry Yu4f9e3ef2022-02-17 14:58:27 +08008957 p += session->ticket_len;
8958 }
8959
Gilles Peskine449bd832023-01-11 14:50:10 +01008960 MBEDTLS_PUT_UINT32_BE(session->ticket_lifetime, p, 0);
Jerry Yu4f9e3ef2022-02-17 14:58:27 +08008961 p += 4;
8962 }
8963#endif /* MBEDTLS_SSL_SESSION_TICKETS && MBEDTLS_SSL_CLI_C */
8964
8965 /*
8966 * Misc extension-related info
8967 */
8968#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
8969 used += 1;
8970
Gilles Peskine449bd832023-01-11 14:50:10 +01008971 if (used <= buf_len) {
Jerry Yu4f9e3ef2022-02-17 14:58:27 +08008972 *p++ = session->mfl_code;
Gilles Peskine449bd832023-01-11 14:50:10 +01008973 }
Jerry Yu4f9e3ef2022-02-17 14:58:27 +08008974#endif
8975
8976#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
8977 used += 1;
8978
Gilles Peskine449bd832023-01-11 14:50:10 +01008979 if (used <= buf_len) {
8980 *p++ = MBEDTLS_BYTE_0(session->encrypt_then_mac);
8981 }
Jerry Yu4f9e3ef2022-02-17 14:58:27 +08008982#endif
8983
Gilles Peskine449bd832023-01-11 14:50:10 +01008984 return used;
Jerry Yu4f9e3ef2022-02-17 14:58:27 +08008985}
8986
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02008987MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01008988static int ssl_tls12_session_load(mbedtls_ssl_session *session,
8989 const unsigned char *buf,
8990 size_t len)
Jerry Yu4f9e3ef2022-02-17 14:58:27 +08008991{
8992#if defined(MBEDTLS_HAVE_TIME)
8993 uint64_t start;
8994#endif
8995#if defined(MBEDTLS_X509_CRT_PARSE_C)
8996#if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
8997 size_t cert_len;
8998#endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
8999#endif /* MBEDTLS_X509_CRT_PARSE_C */
9000
9001 const unsigned char *p = buf;
9002 const unsigned char * const end = buf + len;
9003
9004 /*
9005 * Time
9006 */
9007#if defined(MBEDTLS_HAVE_TIME)
Gilles Peskine449bd832023-01-11 14:50:10 +01009008 if (8 > (size_t) (end - p)) {
9009 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
9010 }
Jerry Yu4f9e3ef2022-02-17 14:58:27 +08009011
Gilles Peskine449bd832023-01-11 14:50:10 +01009012 start = ((uint64_t) p[0] << 56) |
9013 ((uint64_t) p[1] << 48) |
9014 ((uint64_t) p[2] << 40) |
9015 ((uint64_t) p[3] << 32) |
9016 ((uint64_t) p[4] << 24) |
9017 ((uint64_t) p[5] << 16) |
9018 ((uint64_t) p[6] << 8) |
9019 ((uint64_t) p[7]);
Jerry Yu4f9e3ef2022-02-17 14:58:27 +08009020 p += 8;
9021
9022 session->start = (time_t) start;
9023#endif /* MBEDTLS_HAVE_TIME */
9024
9025 /*
9026 * Basic mandatory fields
9027 */
Gilles Peskine449bd832023-01-11 14:50:10 +01009028 if (2 + 1 + 32 + 48 + 4 > (size_t) (end - p)) {
9029 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
9030 }
Jerry Yu4f9e3ef2022-02-17 14:58:27 +08009031
Gilles Peskine449bd832023-01-11 14:50:10 +01009032 session->ciphersuite = (p[0] << 8) | p[1];
Jerry Yu4f9e3ef2022-02-17 14:58:27 +08009033 p += 2;
9034
Jerry Yu4f9e3ef2022-02-17 14:58:27 +08009035 session->id_len = *p++;
Gilles Peskine449bd832023-01-11 14:50:10 +01009036 memcpy(session->id, p, 32);
Jerry Yu4f9e3ef2022-02-17 14:58:27 +08009037 p += 32;
9038
Gilles Peskine449bd832023-01-11 14:50:10 +01009039 memcpy(session->master, p, 48);
Jerry Yu4f9e3ef2022-02-17 14:58:27 +08009040 p += 48;
9041
Gilles Peskine449bd832023-01-11 14:50:10 +01009042 session->verify_result = ((uint32_t) p[0] << 24) |
9043 ((uint32_t) p[1] << 16) |
9044 ((uint32_t) p[2] << 8) |
9045 ((uint32_t) p[3]);
Jerry Yu4f9e3ef2022-02-17 14:58:27 +08009046 p += 4;
9047
9048 /* Immediately clear invalid pointer values that have been read, in case
9049 * we exit early before we replaced them with valid ones. */
9050#if defined(MBEDTLS_X509_CRT_PARSE_C)
9051#if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
9052 session->peer_cert = NULL;
9053#else
9054 session->peer_cert_digest = NULL;
9055#endif /* !MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
9056#endif /* MBEDTLS_X509_CRT_PARSE_C */
9057#if defined(MBEDTLS_SSL_SESSION_TICKETS) && defined(MBEDTLS_SSL_CLI_C)
9058 session->ticket = NULL;
9059#endif /* MBEDTLS_SSL_SESSION_TICKETS && MBEDTLS_SSL_CLI_C */
9060
9061 /*
9062 * Peer certificate
9063 */
9064#if defined(MBEDTLS_X509_CRT_PARSE_C)
9065#if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
9066 /* Deserialize CRT from the end of the ticket. */
Gilles Peskine449bd832023-01-11 14:50:10 +01009067 if (3 > (size_t) (end - p)) {
9068 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
9069 }
Jerry Yu4f9e3ef2022-02-17 14:58:27 +08009070
Gilles Peskine449bd832023-01-11 14:50:10 +01009071 cert_len = (p[0] << 16) | (p[1] << 8) | p[2];
Jerry Yu4f9e3ef2022-02-17 14:58:27 +08009072 p += 3;
9073
Gilles Peskine449bd832023-01-11 14:50:10 +01009074 if (cert_len != 0) {
Jerry Yu4f9e3ef2022-02-17 14:58:27 +08009075 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
9076
Gilles Peskine449bd832023-01-11 14:50:10 +01009077 if (cert_len > (size_t) (end - p)) {
9078 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
9079 }
Jerry Yu4f9e3ef2022-02-17 14:58:27 +08009080
Gilles Peskine449bd832023-01-11 14:50:10 +01009081 session->peer_cert = mbedtls_calloc(1, sizeof(mbedtls_x509_crt));
Jerry Yu4f9e3ef2022-02-17 14:58:27 +08009082
Gilles Peskine449bd832023-01-11 14:50:10 +01009083 if (session->peer_cert == NULL) {
9084 return MBEDTLS_ERR_SSL_ALLOC_FAILED;
9085 }
Jerry Yu4f9e3ef2022-02-17 14:58:27 +08009086
Gilles Peskine449bd832023-01-11 14:50:10 +01009087 mbedtls_x509_crt_init(session->peer_cert);
Jerry Yu4f9e3ef2022-02-17 14:58:27 +08009088
Gilles Peskine449bd832023-01-11 14:50:10 +01009089 if ((ret = mbedtls_x509_crt_parse_der(session->peer_cert,
9090 p, cert_len)) != 0) {
9091 mbedtls_x509_crt_free(session->peer_cert);
9092 mbedtls_free(session->peer_cert);
Jerry Yu4f9e3ef2022-02-17 14:58:27 +08009093 session->peer_cert = NULL;
Gilles Peskine449bd832023-01-11 14:50:10 +01009094 return ret;
Jerry Yu4f9e3ef2022-02-17 14:58:27 +08009095 }
9096
9097 p += cert_len;
9098 }
9099#else /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
9100 /* Deserialize CRT digest from the end of the ticket. */
Gilles Peskine449bd832023-01-11 14:50:10 +01009101 if (2 > (size_t) (end - p)) {
9102 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
9103 }
Jerry Yu4f9e3ef2022-02-17 14:58:27 +08009104
9105 session->peer_cert_digest_type = (mbedtls_md_type_t) *p++;
9106 session->peer_cert_digest_len = (size_t) *p++;
9107
Gilles Peskine449bd832023-01-11 14:50:10 +01009108 if (session->peer_cert_digest_len != 0) {
Jerry Yu4f9e3ef2022-02-17 14:58:27 +08009109 const mbedtls_md_info_t *md_info =
Gilles Peskine449bd832023-01-11 14:50:10 +01009110 mbedtls_md_info_from_type(session->peer_cert_digest_type);
9111 if (md_info == NULL) {
9112 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
9113 }
9114 if (session->peer_cert_digest_len != mbedtls_md_get_size(md_info)) {
9115 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
9116 }
Jerry Yu4f9e3ef2022-02-17 14:58:27 +08009117
Gilles Peskine449bd832023-01-11 14:50:10 +01009118 if (session->peer_cert_digest_len > (size_t) (end - p)) {
9119 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
9120 }
Jerry Yu4f9e3ef2022-02-17 14:58:27 +08009121
9122 session->peer_cert_digest =
Gilles Peskine449bd832023-01-11 14:50:10 +01009123 mbedtls_calloc(1, session->peer_cert_digest_len);
9124 if (session->peer_cert_digest == NULL) {
9125 return MBEDTLS_ERR_SSL_ALLOC_FAILED;
9126 }
Jerry Yu4f9e3ef2022-02-17 14:58:27 +08009127
Gilles Peskine449bd832023-01-11 14:50:10 +01009128 memcpy(session->peer_cert_digest, p,
9129 session->peer_cert_digest_len);
Jerry Yu4f9e3ef2022-02-17 14:58:27 +08009130 p += session->peer_cert_digest_len;
9131 }
9132#endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
9133#endif /* MBEDTLS_X509_CRT_PARSE_C */
9134
9135 /*
9136 * Session ticket and associated data
9137 */
9138#if defined(MBEDTLS_SSL_SESSION_TICKETS) && defined(MBEDTLS_SSL_CLI_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01009139 if (3 > (size_t) (end - p)) {
9140 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
9141 }
Jerry Yu4f9e3ef2022-02-17 14:58:27 +08009142
Gilles Peskine449bd832023-01-11 14:50:10 +01009143 session->ticket_len = (p[0] << 16) | (p[1] << 8) | p[2];
Jerry Yu4f9e3ef2022-02-17 14:58:27 +08009144 p += 3;
9145
Gilles Peskine449bd832023-01-11 14:50:10 +01009146 if (session->ticket_len != 0) {
9147 if (session->ticket_len > (size_t) (end - p)) {
9148 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
9149 }
Jerry Yu4f9e3ef2022-02-17 14:58:27 +08009150
Gilles Peskine449bd832023-01-11 14:50:10 +01009151 session->ticket = mbedtls_calloc(1, session->ticket_len);
9152 if (session->ticket == NULL) {
9153 return MBEDTLS_ERR_SSL_ALLOC_FAILED;
9154 }
Jerry Yu4f9e3ef2022-02-17 14:58:27 +08009155
Gilles Peskine449bd832023-01-11 14:50:10 +01009156 memcpy(session->ticket, p, session->ticket_len);
Jerry Yu4f9e3ef2022-02-17 14:58:27 +08009157 p += session->ticket_len;
9158 }
9159
Gilles Peskine449bd832023-01-11 14:50:10 +01009160 if (4 > (size_t) (end - p)) {
9161 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
9162 }
Jerry Yu4f9e3ef2022-02-17 14:58:27 +08009163
Gilles Peskine449bd832023-01-11 14:50:10 +01009164 session->ticket_lifetime = ((uint32_t) p[0] << 24) |
9165 ((uint32_t) p[1] << 16) |
9166 ((uint32_t) p[2] << 8) |
9167 ((uint32_t) p[3]);
Jerry Yu4f9e3ef2022-02-17 14:58:27 +08009168 p += 4;
9169#endif /* MBEDTLS_SSL_SESSION_TICKETS && MBEDTLS_SSL_CLI_C */
9170
9171 /*
9172 * Misc extension-related info
9173 */
9174#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
Gilles Peskine449bd832023-01-11 14:50:10 +01009175 if (1 > (size_t) (end - p)) {
9176 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
9177 }
Jerry Yu4f9e3ef2022-02-17 14:58:27 +08009178
9179 session->mfl_code = *p++;
9180#endif
9181
9182#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
Gilles Peskine449bd832023-01-11 14:50:10 +01009183 if (1 > (size_t) (end - p)) {
9184 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
9185 }
Jerry Yu4f9e3ef2022-02-17 14:58:27 +08009186
9187 session->encrypt_then_mac = *p++;
9188#endif
9189
9190 /* Done, should have consumed entire buffer */
Gilles Peskine449bd832023-01-11 14:50:10 +01009191 if (p != end) {
9192 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
9193 }
Jerry Yu4f9e3ef2022-02-17 14:58:27 +08009194
Gilles Peskine449bd832023-01-11 14:50:10 +01009195 return 0;
Jerry Yu4f9e3ef2022-02-17 14:58:27 +08009196}
Jerry Yudc7bd172022-02-17 13:44:15 +08009197#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
9198
XiaokangQian75d40ef2022-04-20 11:05:24 +00009199int mbedtls_ssl_validate_ciphersuite(
9200 const mbedtls_ssl_context *ssl,
9201 const mbedtls_ssl_ciphersuite_t *suite_info,
9202 mbedtls_ssl_protocol_version min_tls_version,
Gilles Peskine449bd832023-01-11 14:50:10 +01009203 mbedtls_ssl_protocol_version max_tls_version)
XiaokangQian75d40ef2022-04-20 11:05:24 +00009204{
9205 (void) ssl;
9206
Gilles Peskine449bd832023-01-11 14:50:10 +01009207 if (suite_info == NULL) {
9208 return -1;
9209 }
XiaokangQian75d40ef2022-04-20 11:05:24 +00009210
Gilles Peskine449bd832023-01-11 14:50:10 +01009211 if ((suite_info->min_tls_version > max_tls_version) ||
9212 (suite_info->max_tls_version < min_tls_version)) {
9213 return -1;
XiaokangQian75d40ef2022-04-20 11:05:24 +00009214 }
9215
XiaokangQian060d8672022-04-21 09:24:56 +00009216#if defined(MBEDTLS_SSL_PROTO_TLS1_2) && defined(MBEDTLS_SSL_CLI_C)
XiaokangQian75d40ef2022-04-20 11:05:24 +00009217#if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
Neil Armstrongca7d5062022-05-31 14:43:23 +02009218#if defined(MBEDTLS_USE_PSA_CRYPTO)
Gilles Peskine449bd832023-01-11 14:50:10 +01009219 if (suite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECJPAKE &&
9220 ssl->handshake->psa_pake_ctx_is_ok != 1)
Neil Armstrongca7d5062022-05-31 14:43:23 +02009221#else
Gilles Peskine449bd832023-01-11 14:50:10 +01009222 if (suite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECJPAKE &&
9223 mbedtls_ecjpake_check(&ssl->handshake->ecjpake_ctx) != 0)
Neil Armstrongca7d5062022-05-31 14:43:23 +02009224#endif /* MBEDTLS_USE_PSA_CRYPTO */
XiaokangQian75d40ef2022-04-20 11:05:24 +00009225 {
Gilles Peskine449bd832023-01-11 14:50:10 +01009226 return -1;
XiaokangQian75d40ef2022-04-20 11:05:24 +00009227 }
9228#endif
9229
9230 /* Don't suggest PSK-based ciphersuite if no PSK is available. */
9231#if defined(MBEDTLS_KEY_EXCHANGE_SOME_PSK_ENABLED)
Gilles Peskine449bd832023-01-11 14:50:10 +01009232 if (mbedtls_ssl_ciphersuite_uses_psk(suite_info) &&
9233 mbedtls_ssl_conf_has_static_psk(ssl->conf) == 0) {
9234 return -1;
XiaokangQian75d40ef2022-04-20 11:05:24 +00009235 }
9236#endif /* MBEDTLS_KEY_EXCHANGE_SOME_PSK_ENABLED */
9237#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
9238
Gilles Peskine449bd832023-01-11 14:50:10 +01009239 return 0;
XiaokangQian75d40ef2022-04-20 11:05:24 +00009240}
9241
Ronald Crone68ab4f2022-10-05 12:46:29 +02009242#if defined(MBEDTLS_SSL_HANDSHAKE_WITH_CERT_ENABLED)
XiaokangQianeaf36512022-04-24 09:07:44 +00009243/*
9244 * Function for writing a signature algorithm extension.
9245 *
9246 * The `extension_data` field of signature algorithm contains a `SignatureSchemeList`
9247 * value (TLS 1.3 RFC8446):
9248 * enum {
9249 * ....
9250 * ecdsa_secp256r1_sha256( 0x0403 ),
9251 * ecdsa_secp384r1_sha384( 0x0503 ),
9252 * ecdsa_secp521r1_sha512( 0x0603 ),
9253 * ....
9254 * } SignatureScheme;
9255 *
9256 * struct {
9257 * SignatureScheme supported_signature_algorithms<2..2^16-2>;
9258 * } SignatureSchemeList;
9259 *
9260 * The `extension_data` field of signature algorithm contains a `SignatureAndHashAlgorithm`
9261 * value (TLS 1.2 RFC5246):
9262 * enum {
9263 * none(0), md5(1), sha1(2), sha224(3), sha256(4), sha384(5),
9264 * sha512(6), (255)
9265 * } HashAlgorithm;
9266 *
9267 * enum { anonymous(0), rsa(1), dsa(2), ecdsa(3), (255) }
9268 * SignatureAlgorithm;
9269 *
9270 * struct {
9271 * HashAlgorithm hash;
9272 * SignatureAlgorithm signature;
9273 * } SignatureAndHashAlgorithm;
9274 *
9275 * SignatureAndHashAlgorithm
9276 * supported_signature_algorithms<2..2^16-2>;
9277 *
9278 * The TLS 1.3 signature algorithm extension was defined to be a compatible
9279 * generalization of the TLS 1.2 signature algorithm extension.
9280 * `SignatureAndHashAlgorithm` field of TLS 1.2 can be represented by
9281 * `SignatureScheme` field of TLS 1.3
9282 *
9283 */
Gilles Peskine449bd832023-01-11 14:50:10 +01009284int mbedtls_ssl_write_sig_alg_ext(mbedtls_ssl_context *ssl, unsigned char *buf,
9285 const unsigned char *end, size_t *out_len)
XiaokangQianeaf36512022-04-24 09:07:44 +00009286{
9287 unsigned char *p = buf;
9288 unsigned char *supported_sig_alg; /* Start of supported_signature_algorithms */
9289 size_t supported_sig_alg_len = 0; /* Length of supported_signature_algorithms */
9290
9291 *out_len = 0;
9292
Gilles Peskine449bd832023-01-11 14:50:10 +01009293 MBEDTLS_SSL_DEBUG_MSG(3, ("adding signature_algorithms extension"));
XiaokangQianeaf36512022-04-24 09:07:44 +00009294
9295 /* Check if we have space for header and length field:
9296 * - extension_type (2 bytes)
9297 * - extension_data_length (2 bytes)
9298 * - supported_signature_algorithms_length (2 bytes)
9299 */
Gilles Peskine449bd832023-01-11 14:50:10 +01009300 MBEDTLS_SSL_CHK_BUF_PTR(p, end, 6);
XiaokangQianeaf36512022-04-24 09:07:44 +00009301 p += 6;
9302
9303 /*
9304 * Write supported_signature_algorithms
9305 */
9306 supported_sig_alg = p;
Gilles Peskine449bd832023-01-11 14:50:10 +01009307 const uint16_t *sig_alg = mbedtls_ssl_get_sig_algs(ssl);
9308 if (sig_alg == NULL) {
9309 return MBEDTLS_ERR_SSL_BAD_CONFIG;
9310 }
XiaokangQianeaf36512022-04-24 09:07:44 +00009311
Gilles Peskine449bd832023-01-11 14:50:10 +01009312 for (; *sig_alg != MBEDTLS_TLS1_3_SIG_NONE; sig_alg++) {
9313 MBEDTLS_SSL_DEBUG_MSG(3, ("got signature scheme [%x] %s",
9314 *sig_alg,
9315 mbedtls_ssl_sig_alg_to_str(*sig_alg)));
9316 if (!mbedtls_ssl_sig_alg_is_supported(ssl, *sig_alg)) {
XiaokangQianeaf36512022-04-24 09:07:44 +00009317 continue;
Gilles Peskine449bd832023-01-11 14:50:10 +01009318 }
9319 MBEDTLS_SSL_CHK_BUF_PTR(p, end, 2);
9320 MBEDTLS_PUT_UINT16_BE(*sig_alg, p, 0);
XiaokangQianeaf36512022-04-24 09:07:44 +00009321 p += 2;
Gilles Peskine449bd832023-01-11 14:50:10 +01009322 MBEDTLS_SSL_DEBUG_MSG(3, ("sent signature scheme [%x] %s",
9323 *sig_alg,
9324 mbedtls_ssl_sig_alg_to_str(*sig_alg)));
XiaokangQianeaf36512022-04-24 09:07:44 +00009325 }
9326
9327 /* Length of supported_signature_algorithms */
9328 supported_sig_alg_len = p - supported_sig_alg;
Gilles Peskine449bd832023-01-11 14:50:10 +01009329 if (supported_sig_alg_len == 0) {
9330 MBEDTLS_SSL_DEBUG_MSG(1, ("No signature algorithms defined."));
9331 return MBEDTLS_ERR_SSL_INTERNAL_ERROR;
XiaokangQianeaf36512022-04-24 09:07:44 +00009332 }
9333
Gilles Peskine449bd832023-01-11 14:50:10 +01009334 MBEDTLS_PUT_UINT16_BE(MBEDTLS_TLS_EXT_SIG_ALG, buf, 0);
9335 MBEDTLS_PUT_UINT16_BE(supported_sig_alg_len + 2, buf, 2);
9336 MBEDTLS_PUT_UINT16_BE(supported_sig_alg_len, buf, 4);
XiaokangQianeaf36512022-04-24 09:07:44 +00009337
XiaokangQianeaf36512022-04-24 09:07:44 +00009338 *out_len = p - buf;
9339
9340#if defined(MBEDTLS_SSL_PROTO_TLS1_3)
Gilles Peskine449bd832023-01-11 14:50:10 +01009341 mbedtls_ssl_tls13_set_hs_sent_ext_mask(ssl, MBEDTLS_TLS_EXT_SIG_ALG);
XiaokangQianeaf36512022-04-24 09:07:44 +00009342#endif /* MBEDTLS_SSL_PROTO_TLS1_3 */
Jerry Yu0c354a22022-08-29 15:25:36 +08009343
Gilles Peskine449bd832023-01-11 14:50:10 +01009344 return 0;
XiaokangQianeaf36512022-04-24 09:07:44 +00009345}
Ronald Crone68ab4f2022-10-05 12:46:29 +02009346#endif /* MBEDTLS_SSL_HANDSHAKE_WITH_CERT_ENABLED */
XiaokangQianeaf36512022-04-24 09:07:44 +00009347
XiaokangQian40a35232022-05-07 09:02:40 +00009348#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
XiaokangQian9b2b7712022-05-17 02:57:00 +00009349/*
9350 * mbedtls_ssl_parse_server_name_ext
9351 *
9352 * Structure of server_name extension:
9353 *
9354 * enum {
9355 * host_name(0), (255)
9356 * } NameType;
9357 * opaque HostName<1..2^16-1>;
9358 *
9359 * struct {
9360 * NameType name_type;
9361 * select (name_type) {
9362 * case host_name: HostName;
9363 * } name;
9364 * } ServerName;
9365 * struct {
9366 * ServerName server_name_list<1..2^16-1>
9367 * } ServerNameList;
9368 */
Ronald Cronce7d76e2022-07-08 18:56:49 +02009369MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01009370int mbedtls_ssl_parse_server_name_ext(mbedtls_ssl_context *ssl,
9371 const unsigned char *buf,
9372 const unsigned char *end)
XiaokangQian40a35232022-05-07 09:02:40 +00009373{
9374 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
9375 const unsigned char *p = buf;
XiaokangQian9b2b7712022-05-17 02:57:00 +00009376 size_t server_name_list_len, hostname_len;
9377 const unsigned char *server_name_list_end;
XiaokangQian40a35232022-05-07 09:02:40 +00009378
Gilles Peskine449bd832023-01-11 14:50:10 +01009379 MBEDTLS_SSL_DEBUG_MSG(3, ("parse ServerName extension"));
XiaokangQian40a35232022-05-07 09:02:40 +00009380
Gilles Peskine449bd832023-01-11 14:50:10 +01009381 MBEDTLS_SSL_CHK_BUF_READ_PTR(p, end, 2);
9382 server_name_list_len = MBEDTLS_GET_UINT16_BE(p, 0);
XiaokangQian40a35232022-05-07 09:02:40 +00009383 p += 2;
9384
Gilles Peskine449bd832023-01-11 14:50:10 +01009385 MBEDTLS_SSL_CHK_BUF_READ_PTR(p, end, server_name_list_len);
XiaokangQian9b2b7712022-05-17 02:57:00 +00009386 server_name_list_end = p + server_name_list_len;
Gilles Peskine449bd832023-01-11 14:50:10 +01009387 while (p < server_name_list_end) {
9388 MBEDTLS_SSL_CHK_BUF_READ_PTR(p, server_name_list_end, 3);
9389 hostname_len = MBEDTLS_GET_UINT16_BE(p, 1);
9390 MBEDTLS_SSL_CHK_BUF_READ_PTR(p, server_name_list_end,
9391 hostname_len + 3);
XiaokangQian40a35232022-05-07 09:02:40 +00009392
Gilles Peskine449bd832023-01-11 14:50:10 +01009393 if (p[0] == MBEDTLS_TLS_EXT_SERVERNAME_HOSTNAME) {
XiaokangQian75fe8c72022-06-15 09:42:45 +00009394 /* sni_name is intended to be used only during the parsing of the
9395 * ClientHello message (it is reset to NULL before the end of
9396 * the message parsing). Thus it is ok to just point to the
9397 * reception buffer and not make a copy of it.
9398 */
XiaokangQianf2a94202022-05-20 06:44:24 +00009399 ssl->handshake->sni_name = p + 3;
9400 ssl->handshake->sni_name_len = hostname_len;
Gilles Peskine449bd832023-01-11 14:50:10 +01009401 if (ssl->conf->f_sni == NULL) {
9402 return 0;
XiaokangQian40a35232022-05-07 09:02:40 +00009403 }
Gilles Peskine449bd832023-01-11 14:50:10 +01009404 ret = ssl->conf->f_sni(ssl->conf->p_sni,
9405 ssl, p + 3, hostname_len);
9406 if (ret != 0) {
9407 MBEDTLS_SSL_DEBUG_RET(1, "ssl_sni_wrapper", ret);
9408 MBEDTLS_SSL_PEND_FATAL_ALERT(MBEDTLS_SSL_ALERT_MSG_UNRECOGNIZED_NAME,
9409 MBEDTLS_ERR_SSL_UNRECOGNIZED_NAME);
9410 return MBEDTLS_ERR_SSL_UNRECOGNIZED_NAME;
9411 }
9412 return 0;
XiaokangQian40a35232022-05-07 09:02:40 +00009413 }
9414
9415 p += hostname_len + 3;
9416 }
9417
Gilles Peskine449bd832023-01-11 14:50:10 +01009418 return 0;
XiaokangQian40a35232022-05-07 09:02:40 +00009419}
9420#endif /* MBEDTLS_SSL_SERVER_NAME_INDICATION */
9421
XiaokangQianacb39922022-06-17 10:18:48 +00009422#if defined(MBEDTLS_SSL_ALPN)
Ronald Cronce7d76e2022-07-08 18:56:49 +02009423MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01009424int mbedtls_ssl_parse_alpn_ext(mbedtls_ssl_context *ssl,
9425 const unsigned char *buf,
9426 const unsigned char *end)
XiaokangQianacb39922022-06-17 10:18:48 +00009427{
9428 const unsigned char *p = buf;
XiaokangQianc7403452022-06-23 03:24:12 +00009429 size_t protocol_name_list_len;
XiaokangQian95d5f542022-06-24 02:29:26 +00009430 const unsigned char *protocol_name_list;
9431 const unsigned char *protocol_name_list_end;
XiaokangQianc7403452022-06-23 03:24:12 +00009432 size_t protocol_name_len;
XiaokangQianacb39922022-06-17 10:18:48 +00009433
9434 /* If ALPN not configured, just ignore the extension */
Gilles Peskine449bd832023-01-11 14:50:10 +01009435 if (ssl->conf->alpn_list == NULL) {
9436 return 0;
9437 }
XiaokangQianacb39922022-06-17 10:18:48 +00009438
9439 /*
XiaokangQianc7403452022-06-23 03:24:12 +00009440 * RFC7301, section 3.1
9441 * opaque ProtocolName<1..2^8-1>;
XiaokangQianacb39922022-06-17 10:18:48 +00009442 *
XiaokangQianc7403452022-06-23 03:24:12 +00009443 * struct {
9444 * ProtocolName protocol_name_list<2..2^16-1>
9445 * } ProtocolNameList;
XiaokangQianacb39922022-06-17 10:18:48 +00009446 */
9447
XiaokangQianc7403452022-06-23 03:24:12 +00009448 /*
XiaokangQian0b776e22022-06-24 09:04:59 +00009449 * protocol_name_list_len 2 bytes
9450 * protocol_name_len 1 bytes
9451 * protocol_name >=1 byte
XiaokangQianc7403452022-06-23 03:24:12 +00009452 */
Gilles Peskine449bd832023-01-11 14:50:10 +01009453 MBEDTLS_SSL_CHK_BUF_READ_PTR(p, end, 4);
XiaokangQianacb39922022-06-17 10:18:48 +00009454
Gilles Peskine449bd832023-01-11 14:50:10 +01009455 protocol_name_list_len = MBEDTLS_GET_UINT16_BE(p, 0);
XiaokangQianacb39922022-06-17 10:18:48 +00009456 p += 2;
Gilles Peskine449bd832023-01-11 14:50:10 +01009457 MBEDTLS_SSL_CHK_BUF_READ_PTR(p, end, protocol_name_list_len);
XiaokangQian95d5f542022-06-24 02:29:26 +00009458 protocol_name_list = p;
9459 protocol_name_list_end = p + protocol_name_list_len;
XiaokangQianacb39922022-06-17 10:18:48 +00009460
9461 /* Validate peer's list (lengths) */
Gilles Peskine449bd832023-01-11 14:50:10 +01009462 while (p < protocol_name_list_end) {
XiaokangQian95d5f542022-06-24 02:29:26 +00009463 protocol_name_len = *p++;
Gilles Peskine449bd832023-01-11 14:50:10 +01009464 MBEDTLS_SSL_CHK_BUF_READ_PTR(p, protocol_name_list_end,
9465 protocol_name_len);
9466 if (protocol_name_len == 0) {
XiaokangQian95d5f542022-06-24 02:29:26 +00009467 MBEDTLS_SSL_PEND_FATAL_ALERT(
9468 MBEDTLS_SSL_ALERT_MSG_ILLEGAL_PARAMETER,
Gilles Peskine449bd832023-01-11 14:50:10 +01009469 MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER);
9470 return MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER;
XiaokangQian95d5f542022-06-24 02:29:26 +00009471 }
9472
9473 p += protocol_name_len;
XiaokangQianacb39922022-06-17 10:18:48 +00009474 }
9475
9476 /* Use our order of preference */
Gilles Peskine449bd832023-01-11 14:50:10 +01009477 for (const char **alpn = ssl->conf->alpn_list; *alpn != NULL; alpn++) {
9478 size_t const alpn_len = strlen(*alpn);
XiaokangQian95d5f542022-06-24 02:29:26 +00009479 p = protocol_name_list;
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 if (protocol_name_len == alpn_len &&
9483 memcmp(p, *alpn, alpn_len) == 0) {
XiaokangQianacb39922022-06-17 10:18:48 +00009484 ssl->alpn_chosen = *alpn;
Gilles Peskine449bd832023-01-11 14:50:10 +01009485 return 0;
XiaokangQianacb39922022-06-17 10:18:48 +00009486 }
XiaokangQian95d5f542022-06-24 02:29:26 +00009487
9488 p += protocol_name_len;
XiaokangQianacb39922022-06-17 10:18:48 +00009489 }
9490 }
9491
XiaokangQian95d5f542022-06-24 02:29:26 +00009492 /* If we get here, no match was found */
XiaokangQianacb39922022-06-17 10:18:48 +00009493 MBEDTLS_SSL_PEND_FATAL_ALERT(
Gilles Peskine449bd832023-01-11 14:50:10 +01009494 MBEDTLS_SSL_ALERT_MSG_NO_APPLICATION_PROTOCOL,
9495 MBEDTLS_ERR_SSL_NO_APPLICATION_PROTOCOL);
9496 return MBEDTLS_ERR_SSL_NO_APPLICATION_PROTOCOL;
XiaokangQianacb39922022-06-17 10:18:48 +00009497}
9498
Gilles Peskine449bd832023-01-11 14:50:10 +01009499int mbedtls_ssl_write_alpn_ext(mbedtls_ssl_context *ssl,
9500 unsigned char *buf,
9501 unsigned char *end,
9502 size_t *out_len)
XiaokangQianacb39922022-06-17 10:18:48 +00009503{
9504 unsigned char *p = buf;
XiaokangQian95d5f542022-06-24 02:29:26 +00009505 size_t protocol_name_len;
XiaokangQianc7403452022-06-23 03:24:12 +00009506 *out_len = 0;
XiaokangQianacb39922022-06-17 10:18:48 +00009507
Gilles Peskine449bd832023-01-11 14:50:10 +01009508 if (ssl->alpn_chosen == NULL) {
9509 return 0;
XiaokangQianacb39922022-06-17 10:18:48 +00009510 }
9511
Gilles Peskine449bd832023-01-11 14:50:10 +01009512 protocol_name_len = strlen(ssl->alpn_chosen);
9513 MBEDTLS_SSL_CHK_BUF_PTR(p, end, 7 + protocol_name_len);
XiaokangQianacb39922022-06-17 10:18:48 +00009514
Gilles Peskine449bd832023-01-11 14:50:10 +01009515 MBEDTLS_SSL_DEBUG_MSG(3, ("server side, adding alpn extension"));
XiaokangQianacb39922022-06-17 10:18:48 +00009516 /*
9517 * 0 . 1 ext identifier
9518 * 2 . 3 ext length
9519 * 4 . 5 protocol list length
9520 * 6 . 6 protocol name length
9521 * 7 . 7+n protocol name
9522 */
Gilles Peskine449bd832023-01-11 14:50:10 +01009523 MBEDTLS_PUT_UINT16_BE(MBEDTLS_TLS_EXT_ALPN, p, 0);
XiaokangQianacb39922022-06-17 10:18:48 +00009524
XiaokangQian95d5f542022-06-24 02:29:26 +00009525 *out_len = 7 + protocol_name_len;
XiaokangQianacb39922022-06-17 10:18:48 +00009526
Gilles Peskine449bd832023-01-11 14:50:10 +01009527 MBEDTLS_PUT_UINT16_BE(protocol_name_len + 3, p, 2);
9528 MBEDTLS_PUT_UINT16_BE(protocol_name_len + 1, p, 4);
XiaokangQian0b776e22022-06-24 09:04:59 +00009529 /* Note: the length of the chosen protocol has been checked to be less
9530 * than 255 bytes in `mbedtls_ssl_conf_alpn_protocols`.
9531 */
Gilles Peskine449bd832023-01-11 14:50:10 +01009532 p[6] = MBEDTLS_BYTE_0(protocol_name_len);
XiaokangQianacb39922022-06-17 10:18:48 +00009533
Gilles Peskine449bd832023-01-11 14:50:10 +01009534 memcpy(p + 7, ssl->alpn_chosen, protocol_name_len);
Jerry Yub95dd362022-11-08 21:19:34 +08009535
9536#if defined(MBEDTLS_SSL_PROTO_TLS1_3)
Gilles Peskine449bd832023-01-11 14:50:10 +01009537 mbedtls_ssl_tls13_set_hs_sent_ext_mask(ssl, MBEDTLS_TLS_EXT_ALPN);
Jerry Yub95dd362022-11-08 21:19:34 +08009538#endif
9539
Gilles Peskine449bd832023-01-11 14:50:10 +01009540 return 0;
XiaokangQianacb39922022-06-17 10:18:48 +00009541}
9542#endif /* MBEDTLS_SSL_ALPN */
9543
Xiaokang Qiana3b451f2022-10-11 06:20:56 +00009544#if defined(MBEDTLS_SSL_PROTO_TLS1_3) && \
Xiaokang Qian03409292022-10-12 02:49:52 +00009545 defined(MBEDTLS_SSL_SESSION_TICKETS) && \
Xiaokang Qianed0620c2022-10-12 06:58:13 +00009546 defined(MBEDTLS_SSL_SERVER_NAME_INDICATION) && \
Xiaokang Qianed3afcd2022-10-12 08:31:11 +00009547 defined(MBEDTLS_SSL_CLI_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01009548int mbedtls_ssl_session_set_hostname(mbedtls_ssl_session *session,
9549 const char *hostname)
Xiaokang Qiana3b451f2022-10-11 06:20:56 +00009550{
9551 /* Initialize to suppress unnecessary compiler warning */
9552 size_t hostname_len = 0;
9553
9554 /* Check if new hostname is valid before
9555 * making any change to current one */
Gilles Peskine449bd832023-01-11 14:50:10 +01009556 if (hostname != NULL) {
9557 hostname_len = strlen(hostname);
Xiaokang Qiana3b451f2022-10-11 06:20:56 +00009558
Gilles Peskine449bd832023-01-11 14:50:10 +01009559 if (hostname_len > MBEDTLS_SSL_MAX_HOST_NAME_LEN) {
9560 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
9561 }
Xiaokang Qiana3b451f2022-10-11 06:20:56 +00009562 }
9563
9564 /* Now it's clear that we will overwrite the old hostname,
9565 * so we can free it safely */
Gilles Peskine449bd832023-01-11 14:50:10 +01009566 if (session->hostname != NULL) {
9567 mbedtls_platform_zeroize(session->hostname,
9568 strlen(session->hostname));
9569 mbedtls_free(session->hostname);
Xiaokang Qiana3b451f2022-10-11 06:20:56 +00009570 }
9571
9572 /* Passing NULL as hostname shall clear the old one */
Gilles Peskine449bd832023-01-11 14:50:10 +01009573 if (hostname == NULL) {
Xiaokang Qiana3b451f2022-10-11 06:20:56 +00009574 session->hostname = NULL;
Gilles Peskine449bd832023-01-11 14:50:10 +01009575 } else {
9576 session->hostname = mbedtls_calloc(1, hostname_len + 1);
9577 if (session->hostname == NULL) {
9578 return MBEDTLS_ERR_SSL_ALLOC_FAILED;
9579 }
Xiaokang Qiana3b451f2022-10-11 06:20:56 +00009580
Gilles Peskine449bd832023-01-11 14:50:10 +01009581 memcpy(session->hostname, hostname, hostname_len);
Xiaokang Qiana3b451f2022-10-11 06:20:56 +00009582 }
9583
Gilles Peskine449bd832023-01-11 14:50:10 +01009584 return 0;
Xiaokang Qiana3b451f2022-10-11 06:20:56 +00009585}
Xiaokang Qian03409292022-10-12 02:49:52 +00009586#endif /* MBEDTLS_SSL_PROTO_TLS1_3 &&
9587 MBEDTLS_SSL_SESSION_TICKETS &&
Xiaokang Qianed0620c2022-10-12 06:58:13 +00009588 MBEDTLS_SSL_SERVER_NAME_INDICATION &&
Xiaokang Qianed3afcd2022-10-12 08:31:11 +00009589 MBEDTLS_SSL_CLI_C */
Xiaokang Qiana3b451f2022-10-11 06:20:56 +00009590
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009591#endif /* MBEDTLS_SSL_TLS_C */