blob: 8ee1ddc21c202220a41f82e1ffbc4382c6d54325 [file] [log] [blame]
Paul Bakker5121ce52009-01-03 21:22:43 +00001/*
Mateusz Starzyk06b07fb2021-02-18 13:55:21 +01002 * TLS shared functions
Paul Bakker5121ce52009-01-03 21:22:43 +00003 *
Bence Szépkúti1e148272020-08-07 13:07:28 +02004 * Copyright The Mbed TLS Contributors
Manuel Pégourié-Gonnard37ff1402015-09-04 14:21:07 +02005 * SPDX-License-Identifier: Apache-2.0
6 *
7 * Licensed under the Apache License, Version 2.0 (the "License"); you may
8 * not use this file except in compliance with the License.
9 * You may obtain a copy of the License at
10 *
11 * http://www.apache.org/licenses/LICENSE-2.0
12 *
13 * Unless required by applicable law or agreed to in writing, software
14 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
15 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 * See the License for the specific language governing permissions and
17 * limitations under the License.
Paul Bakker5121ce52009-01-03 21:22:43 +000018 */
19/*
Paul Bakker5121ce52009-01-03 21:22:43 +000020 * http://www.ietf.org/rfc/rfc2246.txt
21 * http://www.ietf.org/rfc/rfc4346.txt
22 */
23
Gilles Peskinedb09ef62020-06-03 01:43:33 +020024#include "common.h"
Paul Bakker5121ce52009-01-03 21:22:43 +000025
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020026#if defined(MBEDTLS_SSL_TLS_C)
Paul Bakker5121ce52009-01-03 21:22:43 +000027
Jerry Yub476a442022-01-21 18:14:45 +080028#include <assert.h>
29
SimonBd5800b72016-04-26 07:43:27 +010030#include "mbedtls/platform.h"
SimonBd5800b72016-04-26 07:43:27 +010031
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000032#include "mbedtls/ssl.h"
Ronald Cron9f0fba32022-02-10 16:45:15 +010033#include "ssl_client.h"
Ronald Cron27c85e72022-03-08 11:37:55 +010034#include "ssl_debug_helpers.h"
Chris Jones84a773f2021-03-05 18:38:47 +000035#include "ssl_misc.h"
Andrzej Kurek25f27152022-08-17 16:09:31 -040036
Janos Follath73c616b2019-12-18 15:07:04 +000037#include "mbedtls/debug.h"
38#include "mbedtls/error.h"
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -050039#include "mbedtls/platform_util.h"
Hanno Beckera835da52019-05-16 12:39:07 +010040#include "mbedtls/version.h"
Gabor Mezei765862c2021-10-19 12:22:25 +020041#include "mbedtls/constant_time.h"
Paul Bakker0be444a2013-08-27 21:55:01 +020042
Rich Evans00ab4702015-02-06 13:43:58 +000043#include <string.h>
44
Andrzej Kurekd6db9be2019-01-10 05:27:10 -050045#if defined(MBEDTLS_USE_PSA_CRYPTO)
46#include "mbedtls/psa_util.h"
47#include "psa/crypto.h"
48#endif
Manuel Pégourié-Gonnard07018f92022-09-15 11:29:35 +020049#include "mbedtls/legacy_or_psa.h"
Andrzej Kurekd6db9be2019-01-10 05:27:10 -050050
Janos Follath23bdca02016-10-07 14:47:14 +010051#if defined(MBEDTLS_X509_CRT_PARSE_C)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000052#include "mbedtls/oid.h"
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +020053#endif
54
Ronald Cronad8c17b2022-06-10 17:18:09 +020055#if defined(MBEDTLS_TEST_HOOKS)
56static mbedtls_ssl_chk_buf_ptr_args chk_buf_ptr_fail_args;
57
58void mbedtls_ssl_set_chk_buf_ptr_fail_args(
Gilles Peskine449bd832023-01-11 14:50:10 +010059 const uint8_t *cur, const uint8_t *end, size_t need)
Ronald Cronad8c17b2022-06-10 17:18:09 +020060{
61 chk_buf_ptr_fail_args.cur = cur;
62 chk_buf_ptr_fail_args.end = end;
63 chk_buf_ptr_fail_args.need = need;
64}
65
Gilles Peskine449bd832023-01-11 14:50:10 +010066void mbedtls_ssl_reset_chk_buf_ptr_fail_args(void)
Ronald Cronad8c17b2022-06-10 17:18:09 +020067{
Gilles Peskine449bd832023-01-11 14:50:10 +010068 memset(&chk_buf_ptr_fail_args, 0, sizeof(chk_buf_ptr_fail_args));
Ronald Cronad8c17b2022-06-10 17:18:09 +020069}
70
Gilles Peskine449bd832023-01-11 14:50:10 +010071int mbedtls_ssl_cmp_chk_buf_ptr_fail_args(mbedtls_ssl_chk_buf_ptr_args *args)
Ronald Cronad8c17b2022-06-10 17:18:09 +020072{
Gilles Peskine449bd832023-01-11 14:50:10 +010073 return (chk_buf_ptr_fail_args.cur != args->cur) ||
74 (chk_buf_ptr_fail_args.end != args->end) ||
75 (chk_buf_ptr_fail_args.need != args->need);
Ronald Cronad8c17b2022-06-10 17:18:09 +020076}
77#endif /* MBEDTLS_TEST_HOOKS */
78
Manuel Pégourié-Gonnard286a1362015-05-13 16:22:05 +020079#if defined(MBEDTLS_SSL_PROTO_DTLS)
Hanno Becker2b1e3542018-08-06 11:19:13 +010080
Hanno Beckera0e20d02019-05-15 14:03:01 +010081#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Beckerf8542cf2019-04-09 15:22:03 +010082/* Top-level Connection ID API */
83
Gilles Peskine449bd832023-01-11 14:50:10 +010084int mbedtls_ssl_conf_cid(mbedtls_ssl_config *conf,
85 size_t len,
86 int ignore_other_cid)
Hanno Beckerad4a1372019-05-03 13:06:44 +010087{
Gilles Peskine449bd832023-01-11 14:50:10 +010088 if (len > MBEDTLS_SSL_CID_IN_LEN_MAX) {
89 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
90 }
Hanno Beckerad4a1372019-05-03 13:06:44 +010091
Gilles Peskine449bd832023-01-11 14:50:10 +010092 if (ignore_other_cid != MBEDTLS_SSL_UNEXPECTED_CID_FAIL &&
93 ignore_other_cid != MBEDTLS_SSL_UNEXPECTED_CID_IGNORE) {
94 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
Hanno Becker611ac772019-05-14 11:45:26 +010095 }
96
97 conf->ignore_unexpected_cid = ignore_other_cid;
Hanno Beckerad4a1372019-05-03 13:06:44 +010098 conf->cid_len = len;
Gilles Peskine449bd832023-01-11 14:50:10 +010099 return 0;
Hanno Beckerad4a1372019-05-03 13:06:44 +0100100}
101
Gilles Peskine449bd832023-01-11 14:50:10 +0100102int mbedtls_ssl_set_cid(mbedtls_ssl_context *ssl,
103 int enable,
104 unsigned char const *own_cid,
105 size_t own_cid_len)
Hanno Beckerf8542cf2019-04-09 15:22:03 +0100106{
Gilles Peskine449bd832023-01-11 14:50:10 +0100107 if (ssl->conf->transport != MBEDTLS_SSL_TRANSPORT_DATAGRAM) {
108 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
109 }
Hanno Becker76a79ab2019-05-03 14:38:32 +0100110
Hanno Beckerca092242019-04-25 16:01:49 +0100111 ssl->negotiate_cid = enable;
Gilles Peskine449bd832023-01-11 14:50:10 +0100112 if (enable == MBEDTLS_SSL_CID_DISABLED) {
113 MBEDTLS_SSL_DEBUG_MSG(3, ("Disable use of CID extension."));
114 return 0;
Hanno Beckerca092242019-04-25 16:01:49 +0100115 }
Gilles Peskine449bd832023-01-11 14:50:10 +0100116 MBEDTLS_SSL_DEBUG_MSG(3, ("Enable use of CID extension."));
117 MBEDTLS_SSL_DEBUG_BUF(3, "Own CID", own_cid, own_cid_len);
Hanno Beckerca092242019-04-25 16:01:49 +0100118
Gilles Peskine449bd832023-01-11 14:50:10 +0100119 if (own_cid_len != ssl->conf->cid_len) {
120 MBEDTLS_SSL_DEBUG_MSG(3, ("CID length %u does not match CID length %u in config",
121 (unsigned) own_cid_len,
122 (unsigned) ssl->conf->cid_len));
123 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
Hanno Beckerca092242019-04-25 16:01:49 +0100124 }
125
Gilles Peskine449bd832023-01-11 14:50:10 +0100126 memcpy(ssl->own_cid, own_cid, own_cid_len);
Hanno Beckerb7ee0cf2019-04-30 14:07:31 +0100127 /* Truncation is not an issue here because
128 * MBEDTLS_SSL_CID_IN_LEN_MAX at most 255. */
129 ssl->own_cid_len = (uint8_t) own_cid_len;
Hanno Beckerca092242019-04-25 16:01:49 +0100130
Gilles Peskine449bd832023-01-11 14:50:10 +0100131 return 0;
Hanno Beckerf8542cf2019-04-09 15:22:03 +0100132}
133
Gilles Peskine449bd832023-01-11 14:50:10 +0100134int mbedtls_ssl_get_own_cid(mbedtls_ssl_context *ssl,
135 int *enabled,
136 unsigned char own_cid[MBEDTLS_SSL_CID_OUT_LEN_MAX],
137 size_t *own_cid_len)
Paul Elliott0113cf12022-03-11 20:26:47 +0000138{
139 *enabled = MBEDTLS_SSL_CID_DISABLED;
140
Gilles Peskine449bd832023-01-11 14:50:10 +0100141 if (ssl->conf->transport != MBEDTLS_SSL_TRANSPORT_DATAGRAM) {
142 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
143 }
Paul Elliott0113cf12022-03-11 20:26:47 +0000144
145 /* We report MBEDTLS_SSL_CID_DISABLED in case the CID length is
146 * zero as this is indistinguishable from not requesting to use
147 * the CID extension. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100148 if (ssl->own_cid_len == 0 || ssl->negotiate_cid == MBEDTLS_SSL_CID_DISABLED) {
149 return 0;
150 }
Paul Elliott0113cf12022-03-11 20:26:47 +0000151
Gilles Peskine449bd832023-01-11 14:50:10 +0100152 if (own_cid_len != NULL) {
Paul Elliott0113cf12022-03-11 20:26:47 +0000153 *own_cid_len = ssl->own_cid_len;
Gilles Peskine449bd832023-01-11 14:50:10 +0100154 if (own_cid != NULL) {
155 memcpy(own_cid, ssl->own_cid, ssl->own_cid_len);
156 }
Paul Elliott0113cf12022-03-11 20:26:47 +0000157 }
158
159 *enabled = MBEDTLS_SSL_CID_ENABLED;
160
Gilles Peskine449bd832023-01-11 14:50:10 +0100161 return 0;
Paul Elliott0113cf12022-03-11 20:26:47 +0000162}
163
Gilles Peskine449bd832023-01-11 14:50:10 +0100164int mbedtls_ssl_get_peer_cid(mbedtls_ssl_context *ssl,
165 int *enabled,
166 unsigned char peer_cid[MBEDTLS_SSL_CID_OUT_LEN_MAX],
167 size_t *peer_cid_len)
Hanno Beckerf8542cf2019-04-09 15:22:03 +0100168{
Hanno Beckerf8542cf2019-04-09 15:22:03 +0100169 *enabled = MBEDTLS_SSL_CID_DISABLED;
Hanno Beckerb1f89cd2019-04-26 17:08:02 +0100170
Gilles Peskine449bd832023-01-11 14:50:10 +0100171 if (ssl->conf->transport != MBEDTLS_SSL_TRANSPORT_DATAGRAM ||
172 mbedtls_ssl_is_handshake_over(ssl) == 0) {
173 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
Hanno Becker76a79ab2019-05-03 14:38:32 +0100174 }
Hanno Beckerb1f89cd2019-04-26 17:08:02 +0100175
Hanno Beckerc5f24222019-05-03 12:54:52 +0100176 /* We report MBEDTLS_SSL_CID_DISABLED in case the CID extensions
177 * were used, but client and server requested the empty CID.
178 * This is indistinguishable from not using the CID extension
179 * in the first place. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100180 if (ssl->transform_in->in_cid_len == 0 &&
181 ssl->transform_in->out_cid_len == 0) {
182 return 0;
Hanno Beckerb1f89cd2019-04-26 17:08:02 +0100183 }
184
Gilles Peskine449bd832023-01-11 14:50:10 +0100185 if (peer_cid_len != NULL) {
Hanno Becker615ef172019-05-22 16:50:35 +0100186 *peer_cid_len = ssl->transform_in->out_cid_len;
Gilles Peskine449bd832023-01-11 14:50:10 +0100187 if (peer_cid != NULL) {
188 memcpy(peer_cid, ssl->transform_in->out_cid,
189 ssl->transform_in->out_cid_len);
Hanno Becker615ef172019-05-22 16:50:35 +0100190 }
191 }
Hanno Beckerb1f89cd2019-04-26 17:08:02 +0100192
193 *enabled = MBEDTLS_SSL_CID_ENABLED;
194
Gilles Peskine449bd832023-01-11 14:50:10 +0100195 return 0;
Hanno Beckerf8542cf2019-04-09 15:22:03 +0100196}
Hanno Beckera0e20d02019-05-15 14:03:01 +0100197#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Beckerf8542cf2019-04-09 15:22:03 +0100198
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200199#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +0200200
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200201#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
Manuel Pégourié-Gonnard581e6b62013-07-18 12:32:27 +0200202/*
203 * Convert max_fragment_length codes to length.
204 * RFC 6066 says:
205 * enum{
206 * 2^9(1), 2^10(2), 2^11(3), 2^12(4), (255)
207 * } MaxFragmentLength;
208 * and we add 0 -> extension unused
209 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100210static unsigned int ssl_mfl_code_to_length(int mfl)
Manuel Pégourié-Gonnard581e6b62013-07-18 12:32:27 +0200211{
Gilles Peskine449bd832023-01-11 14:50:10 +0100212 switch (mfl) {
213 case MBEDTLS_SSL_MAX_FRAG_LEN_NONE:
214 return MBEDTLS_TLS_EXT_ADV_CONTENT_LEN;
215 case MBEDTLS_SSL_MAX_FRAG_LEN_512:
216 return 512;
217 case MBEDTLS_SSL_MAX_FRAG_LEN_1024:
218 return 1024;
219 case MBEDTLS_SSL_MAX_FRAG_LEN_2048:
220 return 2048;
221 case MBEDTLS_SSL_MAX_FRAG_LEN_4096:
222 return 4096;
223 default:
224 return MBEDTLS_TLS_EXT_ADV_CONTENT_LEN;
Angus Grattond8213d02016-05-25 20:56:48 +1000225 }
226}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200227#endif /* MBEDTLS_SSL_MAX_FRAGMENT_LENGTH */
Manuel Pégourié-Gonnard581e6b62013-07-18 12:32:27 +0200228
Gilles Peskine449bd832023-01-11 14:50:10 +0100229int mbedtls_ssl_session_copy(mbedtls_ssl_session *dst,
230 const mbedtls_ssl_session *src)
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200231{
Gilles Peskine449bd832023-01-11 14:50:10 +0100232 mbedtls_ssl_session_free(dst);
233 memcpy(dst, src, sizeof(mbedtls_ssl_session));
吴敬辉0b716112021-11-29 10:46:35 +0800234#if defined(MBEDTLS_SSL_SESSION_TICKETS) && defined(MBEDTLS_SSL_CLI_C)
235 dst->ticket = NULL;
Xiaokang Qian87306442022-10-12 09:47:38 +0000236#if defined(MBEDTLS_SSL_PROTO_TLS1_3) && \
237 defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
Xiaokang Qian126bf8e2022-10-13 02:22:40 +0000238 dst->hostname = NULL;
吴敬辉0b716112021-11-29 10:46:35 +0800239#endif
Xiaokang Qian87306442022-10-12 09:47:38 +0000240#endif /* MBEDTLS_SSL_SESSION_TICKETS && MBEDTLS_SSL_CLI_C */
吴敬辉0b716112021-11-29 10:46:35 +0800241
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200242#if defined(MBEDTLS_X509_CRT_PARSE_C)
Hanno Becker6d1986e2019-02-07 12:27:42 +0000243
244#if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
Gilles Peskine449bd832023-01-11 14:50:10 +0100245 if (src->peer_cert != NULL) {
Janos Follath865b3eb2019-12-16 11:46:15 +0000246 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker2292d1f2013-09-15 17:06:49 +0200247
Gilles Peskine449bd832023-01-11 14:50:10 +0100248 dst->peer_cert = mbedtls_calloc(1, sizeof(mbedtls_x509_crt));
249 if (dst->peer_cert == NULL) {
250 return MBEDTLS_ERR_SSL_ALLOC_FAILED;
251 }
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200252
Gilles Peskine449bd832023-01-11 14:50:10 +0100253 mbedtls_x509_crt_init(dst->peer_cert);
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200254
Gilles Peskine449bd832023-01-11 14:50:10 +0100255 if ((ret = mbedtls_x509_crt_parse_der(dst->peer_cert, src->peer_cert->raw.p,
256 src->peer_cert->raw.len)) != 0) {
257 mbedtls_free(dst->peer_cert);
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200258 dst->peer_cert = NULL;
Gilles Peskine449bd832023-01-11 14:50:10 +0100259 return ret;
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200260 }
261 }
Hanno Becker6d1986e2019-02-07 12:27:42 +0000262#else /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
Gilles Peskine449bd832023-01-11 14:50:10 +0100263 if (src->peer_cert_digest != NULL) {
Hanno Becker9198ad12019-02-05 17:00:50 +0000264 dst->peer_cert_digest =
Gilles Peskine449bd832023-01-11 14:50:10 +0100265 mbedtls_calloc(1, src->peer_cert_digest_len);
266 if (dst->peer_cert_digest == NULL) {
267 return MBEDTLS_ERR_SSL_ALLOC_FAILED;
268 }
Hanno Becker9198ad12019-02-05 17:00:50 +0000269
Gilles Peskine449bd832023-01-11 14:50:10 +0100270 memcpy(dst->peer_cert_digest, src->peer_cert_digest,
271 src->peer_cert_digest_len);
Hanno Becker9198ad12019-02-05 17:00:50 +0000272 dst->peer_cert_digest_type = src->peer_cert_digest_type;
Hanno Beckeraccc5992019-02-25 10:06:59 +0000273 dst->peer_cert_digest_len = src->peer_cert_digest_len;
Hanno Becker9198ad12019-02-05 17:00:50 +0000274 }
275#endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
276
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200277#endif /* MBEDTLS_X509_CRT_PARSE_C */
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200278
Manuel Pégourié-Gonnardb596abf2015-05-20 10:45:29 +0200279#if defined(MBEDTLS_SSL_SESSION_TICKETS) && defined(MBEDTLS_SSL_CLI_C)
Gilles Peskine449bd832023-01-11 14:50:10 +0100280 if (src->ticket != NULL) {
281 dst->ticket = mbedtls_calloc(1, src->ticket_len);
282 if (dst->ticket == NULL) {
283 return MBEDTLS_ERR_SSL_ALLOC_FAILED;
284 }
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200285
Gilles Peskine449bd832023-01-11 14:50:10 +0100286 memcpy(dst->ticket, src->ticket, src->ticket_len);
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200287 }
Xiaokang Qian126bf8e2022-10-13 02:22:40 +0000288
289#if defined(MBEDTLS_SSL_PROTO_TLS1_3) && \
290 defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
Gilles Peskine449bd832023-01-11 14:50:10 +0100291 if (src->endpoint == MBEDTLS_SSL_IS_CLIENT) {
Xiaokang Qian126bf8e2022-10-13 02:22:40 +0000292 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Gilles Peskine449bd832023-01-11 14:50:10 +0100293 ret = mbedtls_ssl_session_set_hostname(dst, src->hostname);
294 if (ret != 0) {
295 return ret;
296 }
Xiaokang Qian126bf8e2022-10-13 02:22:40 +0000297 }
298#endif /* MBEDTLS_SSL_PROTO_TLS1_3 &&
299 MBEDTLS_SSL_SERVER_NAME_INDICATION */
Manuel Pégourié-Gonnardb596abf2015-05-20 10:45:29 +0200300#endif /* MBEDTLS_SSL_SESSION_TICKETS && MBEDTLS_SSL_CLI_C */
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200301
Gilles Peskine449bd832023-01-11 14:50:10 +0100302 return 0;
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200303}
304
Andrzej Kurek0afa2a12020-03-03 10:39:58 -0500305#if defined(MBEDTLS_SSL_VARIABLE_BUFFER_LENGTH)
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +0200306MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +0100307static int resize_buffer(unsigned char **buffer, size_t len_new, size_t *len_old)
Andrzej Kurek0afa2a12020-03-03 10:39:58 -0500308{
Gilles Peskine449bd832023-01-11 14:50:10 +0100309 unsigned char *resized_buffer = mbedtls_calloc(1, len_new);
310 if (resized_buffer == NULL) {
Andrzej Kurek0afa2a12020-03-03 10:39:58 -0500311 return -1;
Gilles Peskine449bd832023-01-11 14:50:10 +0100312 }
Andrzej Kurek0afa2a12020-03-03 10:39:58 -0500313
314 /* We want to copy len_new bytes when downsizing the buffer, and
315 * len_old bytes when upsizing, so we choose the smaller of two sizes,
316 * to fit one buffer into another. Size checks, ensuring that no data is
317 * lost, are done outside of this function. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100318 memcpy(resized_buffer, *buffer,
319 (len_new < *len_old) ? len_new : *len_old);
320 mbedtls_platform_zeroize(*buffer, *len_old);
321 mbedtls_free(*buffer);
Andrzej Kurek0afa2a12020-03-03 10:39:58 -0500322
323 *buffer = resized_buffer;
324 *len_old = len_new;
325
326 return 0;
327}
Andrzej Kurek4a063792020-10-21 15:08:44 +0200328
Gilles Peskine449bd832023-01-11 14:50:10 +0100329static void handle_buffer_resizing(mbedtls_ssl_context *ssl, int downsizing,
330 size_t in_buf_new_len,
331 size_t out_buf_new_len)
Andrzej Kurek4a063792020-10-21 15:08:44 +0200332{
333 int modified = 0;
334 size_t written_in = 0, iv_offset_in = 0, len_offset_in = 0;
335 size_t written_out = 0, iv_offset_out = 0, len_offset_out = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +0100336 if (ssl->in_buf != NULL) {
Andrzej Kurek4a063792020-10-21 15:08:44 +0200337 written_in = ssl->in_msg - ssl->in_buf;
338 iv_offset_in = ssl->in_iv - ssl->in_buf;
339 len_offset_in = ssl->in_len - ssl->in_buf;
Gilles Peskine449bd832023-01-11 14:50:10 +0100340 if (downsizing ?
Andrzej Kurek4a063792020-10-21 15:08:44 +0200341 ssl->in_buf_len > in_buf_new_len && ssl->in_left < in_buf_new_len :
Gilles Peskine449bd832023-01-11 14:50:10 +0100342 ssl->in_buf_len < in_buf_new_len) {
343 if (resize_buffer(&ssl->in_buf, in_buf_new_len, &ssl->in_buf_len) != 0) {
344 MBEDTLS_SSL_DEBUG_MSG(1, ("input buffer resizing failed - out of memory"));
345 } else {
346 MBEDTLS_SSL_DEBUG_MSG(2, ("Reallocating in_buf to %" MBEDTLS_PRINTF_SIZET,
347 in_buf_new_len));
Andrzej Kurek4a063792020-10-21 15:08:44 +0200348 modified = 1;
349 }
350 }
351 }
352
Gilles Peskine449bd832023-01-11 14:50:10 +0100353 if (ssl->out_buf != NULL) {
Andrzej Kurek4a063792020-10-21 15:08:44 +0200354 written_out = ssl->out_msg - ssl->out_buf;
355 iv_offset_out = ssl->out_iv - ssl->out_buf;
356 len_offset_out = ssl->out_len - ssl->out_buf;
Gilles Peskine449bd832023-01-11 14:50:10 +0100357 if (downsizing ?
Andrzej Kurek4a063792020-10-21 15:08:44 +0200358 ssl->out_buf_len > out_buf_new_len && ssl->out_left < out_buf_new_len :
Gilles Peskine449bd832023-01-11 14:50:10 +0100359 ssl->out_buf_len < out_buf_new_len) {
360 if (resize_buffer(&ssl->out_buf, out_buf_new_len, &ssl->out_buf_len) != 0) {
361 MBEDTLS_SSL_DEBUG_MSG(1, ("output buffer resizing failed - out of memory"));
362 } else {
363 MBEDTLS_SSL_DEBUG_MSG(2, ("Reallocating out_buf to %" MBEDTLS_PRINTF_SIZET,
364 out_buf_new_len));
Andrzej Kurek4a063792020-10-21 15:08:44 +0200365 modified = 1;
366 }
367 }
368 }
Gilles Peskine449bd832023-01-11 14:50:10 +0100369 if (modified) {
Andrzej Kurek4a063792020-10-21 15:08:44 +0200370 /* Update pointers here to avoid doing it twice. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100371 mbedtls_ssl_reset_in_out_pointers(ssl);
Andrzej Kurek4a063792020-10-21 15:08:44 +0200372 /* Fields below might not be properly updated with record
373 * splitting or with CID, so they are manually updated here. */
374 ssl->out_msg = ssl->out_buf + written_out;
375 ssl->out_len = ssl->out_buf + len_offset_out;
376 ssl->out_iv = ssl->out_buf + iv_offset_out;
377
378 ssl->in_msg = ssl->in_buf + written_in;
379 ssl->in_len = ssl->in_buf + len_offset_in;
380 ssl->in_iv = ssl->in_buf + iv_offset_in;
381 }
382}
Andrzej Kurek0afa2a12020-03-03 10:39:58 -0500383#endif /* MBEDTLS_SSL_VARIABLE_BUFFER_LENGTH */
384
Jerry Yudb8c48a2022-01-27 14:54:54 +0800385#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
Jerry Yued14c932022-02-17 13:40:45 +0800386
387#if defined(MBEDTLS_SSL_CONTEXT_SERIALIZATION)
Gilles Peskine449bd832023-01-11 14:50:10 +0100388typedef int (*tls_prf_fn)(const unsigned char *secret, size_t slen,
389 const char *label,
390 const unsigned char *random, size_t rlen,
391 unsigned char *dstbuf, size_t dlen);
Jerry Yued14c932022-02-17 13:40:45 +0800392
Gilles Peskine449bd832023-01-11 14:50:10 +0100393static tls_prf_fn ssl_tls12prf_from_cs(int ciphersuite_id);
Jerry Yued14c932022-02-17 13:40:45 +0800394
395#endif /* MBEDTLS_SSL_CONTEXT_SERIALIZATION */
396
397/* Type for the TLS PRF */
398typedef int ssl_tls_prf_t(const unsigned char *, size_t, const char *,
399 const unsigned char *, size_t,
400 unsigned char *, size_t);
401
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +0200402MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +0100403static int ssl_tls12_populate_transform(mbedtls_ssl_transform *transform,
404 int ciphersuite,
405 const unsigned char master[48],
Neil Armstrongf2c82f02022-04-05 11:16:53 +0200406#if defined(MBEDTLS_SSL_SOME_SUITES_USE_CBC_ETM)
Gilles Peskine449bd832023-01-11 14:50:10 +0100407 int encrypt_then_mac,
Neil Armstrongf2c82f02022-04-05 11:16:53 +0200408#endif /* MBEDTLS_SSL_SOME_SUITES_USE_CBC_ETM */
Gilles Peskine449bd832023-01-11 14:50:10 +0100409 ssl_tls_prf_t tls_prf,
410 const unsigned char randbytes[64],
411 mbedtls_ssl_protocol_version tls_version,
412 unsigned endpoint,
413 const mbedtls_ssl_context *ssl);
Jerry Yued14c932022-02-17 13:40:45 +0800414
Andrzej Kurek25f27152022-08-17 16:09:31 -0400415#if defined(MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +0200416MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +0100417static int tls_prf_sha256(const unsigned char *secret, size_t slen,
Ron Eldor51d3ab52019-05-12 14:54:30 +0300418 const char *label,
419 const unsigned char *random, size_t rlen,
Gilles Peskine449bd832023-01-11 14:50:10 +0100420 unsigned char *dstbuf, size_t dlen);
Manuel Pégourié-Gonnard226aa152023-02-05 09:46:59 +0100421static int ssl_calc_verify_tls_sha256(const mbedtls_ssl_context *, unsigned char *, size_t *);
422static int ssl_calc_finished_tls_sha256(mbedtls_ssl_context *, unsigned char *, int);
Gilles Peskine449bd832023-01-11 14:50:10 +0100423
424#endif /* MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA*/
425
426#if defined(MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
427MBEDTLS_CHECK_RETURN_CRITICAL
428static int tls_prf_sha384(const unsigned char *secret, size_t slen,
429 const char *label,
430 const unsigned char *random, size_t rlen,
431 unsigned char *dstbuf, size_t dlen);
432
Manuel Pégourié-Gonnard226aa152023-02-05 09:46:59 +0100433static int ssl_calc_verify_tls_sha384(const mbedtls_ssl_context *, unsigned char *, size_t *);
434static int ssl_calc_finished_tls_sha384(mbedtls_ssl_context *, unsigned char *, int);
Gilles Peskine449bd832023-01-11 14:50:10 +0100435#endif /* MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA*/
436
437static size_t ssl_tls12_session_save(const mbedtls_ssl_session *session,
438 unsigned char *buf,
439 size_t buf_len);
440
441MBEDTLS_CHECK_RETURN_CRITICAL
442static int ssl_tls12_session_load(mbedtls_ssl_session *session,
443 const unsigned char *buf,
444 size_t len);
445#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
446
Manuel Pégourié-Gonnard226aa152023-02-05 09:46:59 +0100447static int ssl_update_checksum_start(mbedtls_ssl_context *, const unsigned char *, size_t);
Gilles Peskine449bd832023-01-11 14:50:10 +0100448
449#if defined(MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Manuel Pégourié-Gonnard226aa152023-02-05 09:46:59 +0100450static int ssl_update_checksum_sha256(mbedtls_ssl_context *, const unsigned char *, size_t);
Gilles Peskine449bd832023-01-11 14:50:10 +0100451#endif /* MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA*/
452
453#if defined(MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Manuel Pégourié-Gonnard226aa152023-02-05 09:46:59 +0100454static int ssl_update_checksum_sha384(mbedtls_ssl_context *, const unsigned char *, size_t);
Gilles Peskine449bd832023-01-11 14:50:10 +0100455#endif /* MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA*/
456
457int mbedtls_ssl_tls_prf(const mbedtls_tls_prf_types prf,
458 const unsigned char *secret, size_t slen,
459 const char *label,
460 const unsigned char *random, size_t rlen,
461 unsigned char *dstbuf, size_t dlen)
Ron Eldor51d3ab52019-05-12 14:54:30 +0300462{
463 mbedtls_ssl_tls_prf_cb *tls_prf = NULL;
464
Gilles Peskine449bd832023-01-11 14:50:10 +0100465 switch (prf) {
Ron Eldord2f25f72019-05-15 14:54:22 +0300466#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
Andrzej Kurek25f27152022-08-17 16:09:31 -0400467#if defined(MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Ron Eldor51d3ab52019-05-12 14:54:30 +0300468 case MBEDTLS_SSL_TLS_PRF_SHA384:
469 tls_prf = tls_prf_sha384;
Gilles Peskine449bd832023-01-11 14:50:10 +0100470 break;
Andrzej Kurekcccb0442022-08-19 03:42:11 -0400471#endif /* MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA*/
Andrzej Kurek25f27152022-08-17 16:09:31 -0400472#if defined(MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Ron Eldor51d3ab52019-05-12 14:54:30 +0300473 case MBEDTLS_SSL_TLS_PRF_SHA256:
474 tls_prf = tls_prf_sha256;
Gilles Peskine449bd832023-01-11 14:50:10 +0100475 break;
Andrzej Kurekcccb0442022-08-19 03:42:11 -0400476#endif /* MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA*/
Ron Eldord2f25f72019-05-15 14:54:22 +0300477#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100478 default:
479 return MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
Ron Eldor51d3ab52019-05-12 14:54:30 +0300480 }
481
Gilles Peskine449bd832023-01-11 14:50:10 +0100482 return tls_prf(secret, slen, label, random, rlen, dstbuf, dlen);
Ron Eldor51d3ab52019-05-12 14:54:30 +0300483}
484
Jerry Yuc73c6182022-02-08 20:29:25 +0800485#if defined(MBEDTLS_X509_CRT_PARSE_C)
Gilles Peskine449bd832023-01-11 14:50:10 +0100486static void ssl_clear_peer_cert(mbedtls_ssl_session *session)
Jerry Yuc73c6182022-02-08 20:29:25 +0800487{
488#if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
Gilles Peskine449bd832023-01-11 14:50:10 +0100489 if (session->peer_cert != NULL) {
490 mbedtls_x509_crt_free(session->peer_cert);
491 mbedtls_free(session->peer_cert);
Jerry Yuc73c6182022-02-08 20:29:25 +0800492 session->peer_cert = NULL;
493 }
494#else /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
Gilles Peskine449bd832023-01-11 14:50:10 +0100495 if (session->peer_cert_digest != NULL) {
Jerry Yuc73c6182022-02-08 20:29:25 +0800496 /* Zeroization is not necessary. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100497 mbedtls_free(session->peer_cert_digest);
Jerry Yuc73c6182022-02-08 20:29:25 +0800498 session->peer_cert_digest = NULL;
499 session->peer_cert_digest_type = MBEDTLS_MD_NONE;
500 session->peer_cert_digest_len = 0;
501 }
502#endif /* !MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
503}
504#endif /* MBEDTLS_X509_CRT_PARSE_C */
505
Gilles Peskine449bd832023-01-11 14:50:10 +0100506uint32_t mbedtls_ssl_get_extension_id(unsigned int extension_type)
Jerry Yu7a485c12022-10-31 13:08:18 +0800507{
Gilles Peskine449bd832023-01-11 14:50:10 +0100508 switch (extension_type) {
Jerry Yu7a485c12022-10-31 13:08:18 +0800509 case MBEDTLS_TLS_EXT_SERVERNAME:
Gilles Peskine449bd832023-01-11 14:50:10 +0100510 return MBEDTLS_SSL_EXT_ID_SERVERNAME;
Jerry Yu7a485c12022-10-31 13:08:18 +0800511
512 case MBEDTLS_TLS_EXT_MAX_FRAGMENT_LENGTH:
Gilles Peskine449bd832023-01-11 14:50:10 +0100513 return MBEDTLS_SSL_EXT_ID_MAX_FRAGMENT_LENGTH;
Jerry Yu7a485c12022-10-31 13:08:18 +0800514
515 case MBEDTLS_TLS_EXT_STATUS_REQUEST:
Gilles Peskine449bd832023-01-11 14:50:10 +0100516 return MBEDTLS_SSL_EXT_ID_STATUS_REQUEST;
Jerry Yu7a485c12022-10-31 13:08:18 +0800517
518 case MBEDTLS_TLS_EXT_SUPPORTED_GROUPS:
Gilles Peskine449bd832023-01-11 14:50:10 +0100519 return MBEDTLS_SSL_EXT_ID_SUPPORTED_GROUPS;
Jerry Yu7a485c12022-10-31 13:08:18 +0800520
521 case MBEDTLS_TLS_EXT_SIG_ALG:
Gilles Peskine449bd832023-01-11 14:50:10 +0100522 return MBEDTLS_SSL_EXT_ID_SIG_ALG;
Jerry Yu7a485c12022-10-31 13:08:18 +0800523
524 case MBEDTLS_TLS_EXT_USE_SRTP:
Gilles Peskine449bd832023-01-11 14:50:10 +0100525 return MBEDTLS_SSL_EXT_ID_USE_SRTP;
Jerry Yu7a485c12022-10-31 13:08:18 +0800526
527 case MBEDTLS_TLS_EXT_HEARTBEAT:
Gilles Peskine449bd832023-01-11 14:50:10 +0100528 return MBEDTLS_SSL_EXT_ID_HEARTBEAT;
Jerry Yu7a485c12022-10-31 13:08:18 +0800529
530 case MBEDTLS_TLS_EXT_ALPN:
Gilles Peskine449bd832023-01-11 14:50:10 +0100531 return MBEDTLS_SSL_EXT_ID_ALPN;
Jerry Yu7a485c12022-10-31 13:08:18 +0800532
533 case MBEDTLS_TLS_EXT_SCT:
Gilles Peskine449bd832023-01-11 14:50:10 +0100534 return MBEDTLS_SSL_EXT_ID_SCT;
Jerry Yu7a485c12022-10-31 13:08:18 +0800535
536 case MBEDTLS_TLS_EXT_CLI_CERT_TYPE:
Gilles Peskine449bd832023-01-11 14:50:10 +0100537 return MBEDTLS_SSL_EXT_ID_CLI_CERT_TYPE;
Jerry Yu7a485c12022-10-31 13:08:18 +0800538
539 case MBEDTLS_TLS_EXT_SERV_CERT_TYPE:
Gilles Peskine449bd832023-01-11 14:50:10 +0100540 return MBEDTLS_SSL_EXT_ID_SERV_CERT_TYPE;
Jerry Yu7a485c12022-10-31 13:08:18 +0800541
542 case MBEDTLS_TLS_EXT_PADDING:
Gilles Peskine449bd832023-01-11 14:50:10 +0100543 return MBEDTLS_SSL_EXT_ID_PADDING;
Jerry Yu7a485c12022-10-31 13:08:18 +0800544
545 case MBEDTLS_TLS_EXT_PRE_SHARED_KEY:
Gilles Peskine449bd832023-01-11 14:50:10 +0100546 return MBEDTLS_SSL_EXT_ID_PRE_SHARED_KEY;
Jerry Yu7a485c12022-10-31 13:08:18 +0800547
548 case MBEDTLS_TLS_EXT_EARLY_DATA:
Gilles Peskine449bd832023-01-11 14:50:10 +0100549 return MBEDTLS_SSL_EXT_ID_EARLY_DATA;
Jerry Yu7a485c12022-10-31 13:08:18 +0800550
551 case MBEDTLS_TLS_EXT_SUPPORTED_VERSIONS:
Gilles Peskine449bd832023-01-11 14:50:10 +0100552 return MBEDTLS_SSL_EXT_ID_SUPPORTED_VERSIONS;
Jerry Yu7a485c12022-10-31 13:08:18 +0800553
554 case MBEDTLS_TLS_EXT_COOKIE:
Gilles Peskine449bd832023-01-11 14:50:10 +0100555 return MBEDTLS_SSL_EXT_ID_COOKIE;
Jerry Yu7a485c12022-10-31 13:08:18 +0800556
557 case MBEDTLS_TLS_EXT_PSK_KEY_EXCHANGE_MODES:
Gilles Peskine449bd832023-01-11 14:50:10 +0100558 return MBEDTLS_SSL_EXT_ID_PSK_KEY_EXCHANGE_MODES;
Jerry Yu7a485c12022-10-31 13:08:18 +0800559
560 case MBEDTLS_TLS_EXT_CERT_AUTH:
Gilles Peskine449bd832023-01-11 14:50:10 +0100561 return MBEDTLS_SSL_EXT_ID_CERT_AUTH;
Jerry Yu7a485c12022-10-31 13:08:18 +0800562
563 case MBEDTLS_TLS_EXT_OID_FILTERS:
Gilles Peskine449bd832023-01-11 14:50:10 +0100564 return MBEDTLS_SSL_EXT_ID_OID_FILTERS;
Jerry Yu7a485c12022-10-31 13:08:18 +0800565
566 case MBEDTLS_TLS_EXT_POST_HANDSHAKE_AUTH:
Gilles Peskine449bd832023-01-11 14:50:10 +0100567 return MBEDTLS_SSL_EXT_ID_POST_HANDSHAKE_AUTH;
Jerry Yu7a485c12022-10-31 13:08:18 +0800568
569 case MBEDTLS_TLS_EXT_SIG_ALG_CERT:
Gilles Peskine449bd832023-01-11 14:50:10 +0100570 return MBEDTLS_SSL_EXT_ID_SIG_ALG_CERT;
Jerry Yu7a485c12022-10-31 13:08:18 +0800571
572 case MBEDTLS_TLS_EXT_KEY_SHARE:
Gilles Peskine449bd832023-01-11 14:50:10 +0100573 return MBEDTLS_SSL_EXT_ID_KEY_SHARE;
Jerry Yu7a485c12022-10-31 13:08:18 +0800574
575 case MBEDTLS_TLS_EXT_TRUNCATED_HMAC:
Gilles Peskine449bd832023-01-11 14:50:10 +0100576 return MBEDTLS_SSL_EXT_ID_TRUNCATED_HMAC;
Jerry Yu7a485c12022-10-31 13:08:18 +0800577
578 case MBEDTLS_TLS_EXT_SUPPORTED_POINT_FORMATS:
Gilles Peskine449bd832023-01-11 14:50:10 +0100579 return MBEDTLS_SSL_EXT_ID_SUPPORTED_POINT_FORMATS;
Jerry Yu7a485c12022-10-31 13:08:18 +0800580
581 case MBEDTLS_TLS_EXT_ENCRYPT_THEN_MAC:
Gilles Peskine449bd832023-01-11 14:50:10 +0100582 return MBEDTLS_SSL_EXT_ID_ENCRYPT_THEN_MAC;
Jerry Yu7a485c12022-10-31 13:08:18 +0800583
584 case MBEDTLS_TLS_EXT_EXTENDED_MASTER_SECRET:
Gilles Peskine449bd832023-01-11 14:50:10 +0100585 return MBEDTLS_SSL_EXT_ID_EXTENDED_MASTER_SECRET;
Jerry Yu7a485c12022-10-31 13:08:18 +0800586
587 case MBEDTLS_TLS_EXT_SESSION_TICKET:
Gilles Peskine449bd832023-01-11 14:50:10 +0100588 return MBEDTLS_SSL_EXT_ID_SESSION_TICKET;
Jerry Yu7a485c12022-10-31 13:08:18 +0800589
590 }
591
Gilles Peskine449bd832023-01-11 14:50:10 +0100592 return MBEDTLS_SSL_EXT_ID_UNRECOGNIZED;
Jerry Yu7a485c12022-10-31 13:08:18 +0800593}
594
Gilles Peskine449bd832023-01-11 14:50:10 +0100595uint32_t mbedtls_ssl_get_extension_mask(unsigned int extension_type)
Jerry Yu7a485c12022-10-31 13:08:18 +0800596{
Gilles Peskine449bd832023-01-11 14:50:10 +0100597 return 1 << mbedtls_ssl_get_extension_id(extension_type);
Jerry Yu7a485c12022-10-31 13:08:18 +0800598}
599
Jerry Yud25cab02022-10-31 12:48:30 +0800600#if defined(MBEDTLS_DEBUG_C)
601static const char *extension_name_table[] = {
Jerry Yuea52ed92022-11-08 21:01:17 +0800602 [MBEDTLS_SSL_EXT_ID_UNRECOGNIZED] = "unrecognized",
Jerry Yud25cab02022-10-31 12:48:30 +0800603 [MBEDTLS_SSL_EXT_ID_SERVERNAME] = "server_name",
604 [MBEDTLS_SSL_EXT_ID_MAX_FRAGMENT_LENGTH] = "max_fragment_length",
605 [MBEDTLS_SSL_EXT_ID_STATUS_REQUEST] = "status_request",
606 [MBEDTLS_SSL_EXT_ID_SUPPORTED_GROUPS] = "supported_groups",
607 [MBEDTLS_SSL_EXT_ID_SIG_ALG] = "signature_algorithms",
608 [MBEDTLS_SSL_EXT_ID_USE_SRTP] = "use_srtp",
609 [MBEDTLS_SSL_EXT_ID_HEARTBEAT] = "heartbeat",
610 [MBEDTLS_SSL_EXT_ID_ALPN] = "application_layer_protocol_negotiation",
611 [MBEDTLS_SSL_EXT_ID_SCT] = "signed_certificate_timestamp",
612 [MBEDTLS_SSL_EXT_ID_CLI_CERT_TYPE] = "client_certificate_type",
613 [MBEDTLS_SSL_EXT_ID_SERV_CERT_TYPE] = "server_certificate_type",
614 [MBEDTLS_SSL_EXT_ID_PADDING] = "padding",
615 [MBEDTLS_SSL_EXT_ID_PRE_SHARED_KEY] = "pre_shared_key",
616 [MBEDTLS_SSL_EXT_ID_EARLY_DATA] = "early_data",
617 [MBEDTLS_SSL_EXT_ID_SUPPORTED_VERSIONS] = "supported_versions",
618 [MBEDTLS_SSL_EXT_ID_COOKIE] = "cookie",
619 [MBEDTLS_SSL_EXT_ID_PSK_KEY_EXCHANGE_MODES] = "psk_key_exchange_modes",
620 [MBEDTLS_SSL_EXT_ID_CERT_AUTH] = "certificate_authorities",
621 [MBEDTLS_SSL_EXT_ID_OID_FILTERS] = "oid_filters",
622 [MBEDTLS_SSL_EXT_ID_POST_HANDSHAKE_AUTH] = "post_handshake_auth",
623 [MBEDTLS_SSL_EXT_ID_SIG_ALG_CERT] = "signature_algorithms_cert",
624 [MBEDTLS_SSL_EXT_ID_KEY_SHARE] = "key_share",
625 [MBEDTLS_SSL_EXT_ID_TRUNCATED_HMAC] = "truncated_hmac",
626 [MBEDTLS_SSL_EXT_ID_SUPPORTED_POINT_FORMATS] = "supported_point_formats",
627 [MBEDTLS_SSL_EXT_ID_ENCRYPT_THEN_MAC] = "encrypt_then_mac",
628 [MBEDTLS_SSL_EXT_ID_EXTENDED_MASTER_SECRET] = "extended_master_secret",
629 [MBEDTLS_SSL_EXT_ID_SESSION_TICKET] = "session_ticket"
630};
631
Gilles Peskine449bd832023-01-11 14:50:10 +0100632static unsigned int extension_type_table[] = {
Jerry Yud25cab02022-10-31 12:48:30 +0800633 [MBEDTLS_SSL_EXT_ID_UNRECOGNIZED] = 0xff,
634 [MBEDTLS_SSL_EXT_ID_SERVERNAME] = MBEDTLS_TLS_EXT_SERVERNAME,
635 [MBEDTLS_SSL_EXT_ID_MAX_FRAGMENT_LENGTH] = MBEDTLS_TLS_EXT_MAX_FRAGMENT_LENGTH,
636 [MBEDTLS_SSL_EXT_ID_STATUS_REQUEST] = MBEDTLS_TLS_EXT_STATUS_REQUEST,
637 [MBEDTLS_SSL_EXT_ID_SUPPORTED_GROUPS] = MBEDTLS_TLS_EXT_SUPPORTED_GROUPS,
638 [MBEDTLS_SSL_EXT_ID_SIG_ALG] = MBEDTLS_TLS_EXT_SIG_ALG,
639 [MBEDTLS_SSL_EXT_ID_USE_SRTP] = MBEDTLS_TLS_EXT_USE_SRTP,
640 [MBEDTLS_SSL_EXT_ID_HEARTBEAT] = MBEDTLS_TLS_EXT_HEARTBEAT,
641 [MBEDTLS_SSL_EXT_ID_ALPN] = MBEDTLS_TLS_EXT_ALPN,
642 [MBEDTLS_SSL_EXT_ID_SCT] = MBEDTLS_TLS_EXT_SCT,
643 [MBEDTLS_SSL_EXT_ID_CLI_CERT_TYPE] = MBEDTLS_TLS_EXT_CLI_CERT_TYPE,
644 [MBEDTLS_SSL_EXT_ID_SERV_CERT_TYPE] = MBEDTLS_TLS_EXT_SERV_CERT_TYPE,
645 [MBEDTLS_SSL_EXT_ID_PADDING] = MBEDTLS_TLS_EXT_PADDING,
646 [MBEDTLS_SSL_EXT_ID_PRE_SHARED_KEY] = MBEDTLS_TLS_EXT_PRE_SHARED_KEY,
647 [MBEDTLS_SSL_EXT_ID_EARLY_DATA] = MBEDTLS_TLS_EXT_EARLY_DATA,
648 [MBEDTLS_SSL_EXT_ID_SUPPORTED_VERSIONS] = MBEDTLS_TLS_EXT_SUPPORTED_VERSIONS,
649 [MBEDTLS_SSL_EXT_ID_COOKIE] = MBEDTLS_TLS_EXT_COOKIE,
650 [MBEDTLS_SSL_EXT_ID_PSK_KEY_EXCHANGE_MODES] = MBEDTLS_TLS_EXT_PSK_KEY_EXCHANGE_MODES,
651 [MBEDTLS_SSL_EXT_ID_CERT_AUTH] = MBEDTLS_TLS_EXT_CERT_AUTH,
652 [MBEDTLS_SSL_EXT_ID_OID_FILTERS] = MBEDTLS_TLS_EXT_OID_FILTERS,
653 [MBEDTLS_SSL_EXT_ID_POST_HANDSHAKE_AUTH] = MBEDTLS_TLS_EXT_POST_HANDSHAKE_AUTH,
654 [MBEDTLS_SSL_EXT_ID_SIG_ALG_CERT] = MBEDTLS_TLS_EXT_SIG_ALG_CERT,
655 [MBEDTLS_SSL_EXT_ID_KEY_SHARE] = MBEDTLS_TLS_EXT_KEY_SHARE,
656 [MBEDTLS_SSL_EXT_ID_TRUNCATED_HMAC] = MBEDTLS_TLS_EXT_TRUNCATED_HMAC,
657 [MBEDTLS_SSL_EXT_ID_SUPPORTED_POINT_FORMATS] = MBEDTLS_TLS_EXT_SUPPORTED_POINT_FORMATS,
658 [MBEDTLS_SSL_EXT_ID_ENCRYPT_THEN_MAC] = MBEDTLS_TLS_EXT_ENCRYPT_THEN_MAC,
659 [MBEDTLS_SSL_EXT_ID_EXTENDED_MASTER_SECRET] = MBEDTLS_TLS_EXT_EXTENDED_MASTER_SECRET,
660 [MBEDTLS_SSL_EXT_ID_SESSION_TICKET] = MBEDTLS_TLS_EXT_SESSION_TICKET
661};
662
Gilles Peskine449bd832023-01-11 14:50:10 +0100663const char *mbedtls_ssl_get_extension_name(unsigned int extension_type)
Jerry Yud25cab02022-10-31 12:48:30 +0800664{
Gilles Peskine449bd832023-01-11 14:50:10 +0100665 return extension_name_table[
666 mbedtls_ssl_get_extension_id(extension_type)];
Jerry Yud25cab02022-10-31 12:48:30 +0800667}
668
Gilles Peskine449bd832023-01-11 14:50:10 +0100669static const char *ssl_tls13_get_hs_msg_name(int hs_msg_type)
Jerry Yud25cab02022-10-31 12:48:30 +0800670{
Gilles Peskine449bd832023-01-11 14:50:10 +0100671 switch (hs_msg_type) {
Jerry Yud25cab02022-10-31 12:48:30 +0800672 case MBEDTLS_SSL_HS_CLIENT_HELLO:
Gilles Peskine449bd832023-01-11 14:50:10 +0100673 return "ClientHello";
Jerry Yud25cab02022-10-31 12:48:30 +0800674 case MBEDTLS_SSL_HS_SERVER_HELLO:
Gilles Peskine449bd832023-01-11 14:50:10 +0100675 return "ServerHello";
Jerry Yud25cab02022-10-31 12:48:30 +0800676 case MBEDTLS_SSL_TLS1_3_HS_HELLO_RETRY_REQUEST:
Gilles Peskine449bd832023-01-11 14:50:10 +0100677 return "HelloRetryRequest";
Jerry Yud25cab02022-10-31 12:48:30 +0800678 case MBEDTLS_SSL_HS_NEW_SESSION_TICKET:
Gilles Peskine449bd832023-01-11 14:50:10 +0100679 return "NewSessionTicket";
Jerry Yud25cab02022-10-31 12:48:30 +0800680 case MBEDTLS_SSL_HS_ENCRYPTED_EXTENSIONS:
Gilles Peskine449bd832023-01-11 14:50:10 +0100681 return "EncryptedExtensions";
Jerry Yud25cab02022-10-31 12:48:30 +0800682 case MBEDTLS_SSL_HS_CERTIFICATE:
Gilles Peskine449bd832023-01-11 14:50:10 +0100683 return "Certificate";
Jerry Yud25cab02022-10-31 12:48:30 +0800684 case MBEDTLS_SSL_HS_CERTIFICATE_REQUEST:
Gilles Peskine449bd832023-01-11 14:50:10 +0100685 return "CertificateRequest";
Jerry Yud25cab02022-10-31 12:48:30 +0800686 }
Gilles Peskine449bd832023-01-11 14:50:10 +0100687 return "Unknown";
Jerry Yud25cab02022-10-31 12:48:30 +0800688}
689
Gilles Peskine449bd832023-01-11 14:50:10 +0100690void mbedtls_ssl_print_extension(const mbedtls_ssl_context *ssl,
691 int level, const char *file, int line,
692 int hs_msg_type, unsigned int extension_type,
693 const char *extra_msg0, const char *extra_msg1)
Jerry Yud25cab02022-10-31 12:48:30 +0800694{
695 const char *extra_msg;
Gilles Peskine449bd832023-01-11 14:50:10 +0100696 if (extra_msg0 && extra_msg1) {
Jerry Yud25cab02022-10-31 12:48:30 +0800697 mbedtls_debug_print_msg(
698 ssl, level, file, line,
699 "%s: %s(%u) extension %s %s.",
Gilles Peskine449bd832023-01-11 14:50:10 +0100700 ssl_tls13_get_hs_msg_name(hs_msg_type),
701 mbedtls_ssl_get_extension_name(extension_type),
Jerry Yud25cab02022-10-31 12:48:30 +0800702 extension_type,
Gilles Peskine449bd832023-01-11 14:50:10 +0100703 extra_msg0, extra_msg1);
Jerry Yud25cab02022-10-31 12:48:30 +0800704 return;
705 }
706
707 extra_msg = extra_msg0 ? extra_msg0 : extra_msg1;
Gilles Peskine449bd832023-01-11 14:50:10 +0100708 if (extra_msg) {
Jerry Yud25cab02022-10-31 12:48:30 +0800709 mbedtls_debug_print_msg(
710 ssl, level, file, line,
Gilles Peskine449bd832023-01-11 14:50:10 +0100711 "%s: %s(%u) extension %s.", ssl_tls13_get_hs_msg_name(hs_msg_type),
712 mbedtls_ssl_get_extension_name(extension_type), extension_type,
713 extra_msg);
Jerry Yud25cab02022-10-31 12:48:30 +0800714 return;
715 }
716
717 mbedtls_debug_print_msg(
718 ssl, level, file, line,
Gilles Peskine449bd832023-01-11 14:50:10 +0100719 "%s: %s(%u) extension.", ssl_tls13_get_hs_msg_name(hs_msg_type),
720 mbedtls_ssl_get_extension_name(extension_type), extension_type);
Jerry Yud25cab02022-10-31 12:48:30 +0800721}
722
Gilles Peskine449bd832023-01-11 14:50:10 +0100723void mbedtls_ssl_print_extensions(const mbedtls_ssl_context *ssl,
724 int level, const char *file, int line,
725 int hs_msg_type, uint32_t extensions_mask,
726 const char *extra)
Jerry Yud25cab02022-10-31 12:48:30 +0800727{
728
Gilles Peskine449bd832023-01-11 14:50:10 +0100729 for (unsigned i = 0;
730 i < sizeof(extension_name_table) / sizeof(extension_name_table[0]);
731 i++) {
Jerry Yu79aa7212022-11-08 21:30:21 +0800732 mbedtls_ssl_print_extension(
Jerry Yuea52ed92022-11-08 21:01:17 +0800733 ssl, level, file, line, hs_msg_type, extension_type_table[i],
Gilles Peskine449bd832023-01-11 14:50:10 +0100734 extensions_mask & (1 << i) ? "exists" : "does not exist", extra);
Jerry Yud25cab02022-10-31 12:48:30 +0800735 }
736}
737
Pengyu Lvee455c02023-01-12 14:37:24 +0800738#if defined(MBEDTLS_SSL_PROTO_TLS1_3) && defined(MBEDTLS_SSL_SESSION_TICKETS)
739#define ARRAY_LENGTH(a) (sizeof(a) / sizeof(*(a)))
740
741static const char *ticket_flag_name_table[] =
742{
743 [0] = "ALLOW_PSK_RESUMPTION",
744 [2] = "ALLOW_PSK_EPHEMERAL_RESUMPTION",
745 [3] = "ALLOW_EARLY_DATA",
746};
747
Pengyu Lv4938a562023-01-16 11:28:49 +0800748void mbedtls_ssl_print_ticket_flags(const mbedtls_ssl_context *ssl,
749 int level, const char *file, int line,
750 unsigned int flags)
Pengyu Lvee455c02023-01-12 14:37:24 +0800751{
752 size_t i;
753
754 mbedtls_debug_print_msg(ssl, level, file, line,
Pengyu Lv4938a562023-01-16 11:28:49 +0800755 "print ticket_flags (0x%02x)", flags);
756
757 flags = flags & MBEDTLS_SSL_TLS1_3_TICKET_FLAGS_MASK;
Pengyu Lvee455c02023-01-12 14:37:24 +0800758
759 for (i = 0; i < ARRAY_LENGTH(ticket_flag_name_table); i++) {
Pengyu Lv4938a562023-01-16 11:28:49 +0800760 if ((flags & (1 << i))) {
Pengyu Lvee455c02023-01-12 14:37:24 +0800761 mbedtls_debug_print_msg(ssl, level, file, line, "- %s is set.",
762 ticket_flag_name_table[i]);
763 }
764 }
765}
766#endif /* MBEDTLS_SSL_PROTO_TLS1_3 && MBEDTLS_SSL_SESSION_TICKETS */
767
Jerry Yud25cab02022-10-31 12:48:30 +0800768#endif /* MBEDTLS_DEBUG_C */
769
Gilles Peskine449bd832023-01-11 14:50:10 +0100770void mbedtls_ssl_optimize_checksum(mbedtls_ssl_context *ssl,
771 const mbedtls_ssl_ciphersuite_t *ciphersuite_info)
Jerry Yuc73c6182022-02-08 20:29:25 +0800772{
773 ((void) ciphersuite_info);
774
Andrzej Kurek25f27152022-08-17 16:09:31 -0400775#if defined(MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Gilles Peskine449bd832023-01-11 14:50:10 +0100776 if (ciphersuite_info->mac == MBEDTLS_MD_SHA384) {
Jerry Yuc73c6182022-02-08 20:29:25 +0800777 ssl->handshake->update_checksum = ssl_update_checksum_sha384;
Gilles Peskine449bd832023-01-11 14:50:10 +0100778 } else
Jerry Yuc73c6182022-02-08 20:29:25 +0800779#endif
Andrzej Kurek25f27152022-08-17 16:09:31 -0400780#if defined(MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Gilles Peskine449bd832023-01-11 14:50:10 +0100781 if (ciphersuite_info->mac != MBEDTLS_MD_SHA384) {
Jerry Yuc73c6182022-02-08 20:29:25 +0800782 ssl->handshake->update_checksum = ssl_update_checksum_sha256;
Gilles Peskine449bd832023-01-11 14:50:10 +0100783 } else
Jerry Yuc73c6182022-02-08 20:29:25 +0800784#endif
785 {
Gilles Peskine449bd832023-01-11 14:50:10 +0100786 MBEDTLS_SSL_DEBUG_MSG(1, ("should never happen"));
Jerry Yuc73c6182022-02-08 20:29:25 +0800787 return;
788 }
789}
790
Manuel Pégourié-Gonnardb8b07aa2023-02-06 00:34:21 +0100791int mbedtls_ssl_add_hs_hdr_to_checksum(mbedtls_ssl_context *ssl,
Manuel Pégourié-Gonnard43cc1272023-02-06 11:48:19 +0100792 unsigned hs_type,
793 size_t total_hs_len)
Ronald Cron8f6d39a2022-03-10 18:56:50 +0100794{
795 unsigned char hs_hdr[4];
796
797 /* Build HS header for checksum update. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100798 hs_hdr[0] = MBEDTLS_BYTE_0(hs_type);
799 hs_hdr[1] = MBEDTLS_BYTE_2(total_hs_len);
800 hs_hdr[2] = MBEDTLS_BYTE_1(total_hs_len);
801 hs_hdr[3] = MBEDTLS_BYTE_0(total_hs_len);
Ronald Cron8f6d39a2022-03-10 18:56:50 +0100802
Manuel Pégourié-Gonnardb8b07aa2023-02-06 00:34:21 +0100803 return ssl->handshake->update_checksum(ssl, hs_hdr, sizeof(hs_hdr));
Ronald Cron8f6d39a2022-03-10 18:56:50 +0100804}
805
Manuel Pégourié-Gonnardb8b07aa2023-02-06 00:34:21 +0100806int mbedtls_ssl_add_hs_msg_to_checksum(mbedtls_ssl_context *ssl,
Manuel Pégourié-Gonnard43cc1272023-02-06 11:48:19 +0100807 unsigned hs_type,
808 unsigned char const *msg,
809 size_t msg_len)
Ronald Cron8f6d39a2022-03-10 18:56:50 +0100810{
Manuel Pégourié-Gonnardb8b07aa2023-02-06 00:34:21 +0100811 int ret;
812 ret = mbedtls_ssl_add_hs_hdr_to_checksum(ssl, hs_type, msg_len);
Manuel Pégourié-Gonnard43cc1272023-02-06 11:48:19 +0100813 if (ret != 0) {
Manuel Pégourié-Gonnardb8b07aa2023-02-06 00:34:21 +0100814 return ret;
Manuel Pégourié-Gonnard43cc1272023-02-06 11:48:19 +0100815 }
Manuel Pégourié-Gonnardb8b07aa2023-02-06 00:34:21 +0100816 return ssl->handshake->update_checksum(ssl, msg, msg_len);
Ronald Cron8f6d39a2022-03-10 18:56:50 +0100817}
818
Manuel Pégourié-Gonnard226aa152023-02-05 09:46:59 +0100819int mbedtls_ssl_reset_checksum(mbedtls_ssl_context *ssl)
Jerry Yuc73c6182022-02-08 20:29:25 +0800820{
Manuel Pégourié-Gonnard626aaed2023-02-06 22:03:06 +0100821#if defined(MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA) || \
822 defined(MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Manuel Pégourié-Gonnardb72ff492023-02-06 09:54:49 +0100823#if defined(MBEDTLS_USE_PSA_CRYPTO)
824 psa_status_t status;
825#else
826 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
827#endif
Manuel Pégourié-Gonnard626aaed2023-02-06 22:03:06 +0100828#else /* SHA-256 or SHA-384 */
Jerry Yuc73c6182022-02-08 20:29:25 +0800829 ((void) ssl);
Manuel Pégourié-Gonnard626aaed2023-02-06 22:03:06 +0100830#endif /* SHA-256 or SHA-384 */
Andrzej Kurek25f27152022-08-17 16:09:31 -0400831#if defined(MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Jerry Yuc73c6182022-02-08 20:29:25 +0800832#if defined(MBEDTLS_USE_PSA_CRYPTO)
Manuel Pégourié-Gonnardb72ff492023-02-06 09:54:49 +0100833 status = psa_hash_abort(&ssl->handshake->fin_sha256_psa);
834 if (status != PSA_SUCCESS) {
835 return mbedtls_md_error_from_psa(status);
836 }
837 status = psa_hash_setup(&ssl->handshake->fin_sha256_psa, PSA_ALG_SHA_256);
838 if (status != PSA_SUCCESS) {
839 return mbedtls_md_error_from_psa(status);
840 }
Jerry Yuc73c6182022-02-08 20:29:25 +0800841#else
Manuel Pégourié-Gonnard947cee12023-03-06 11:59:59 +0100842 mbedtls_md_free(&ssl->handshake->fin_sha256);
843 mbedtls_md_init(&ssl->handshake->fin_sha256);
Manuel Pégourié-Gonnardf057ecf2023-02-24 13:19:17 +0100844 ret = mbedtls_md_setup(&ssl->handshake->fin_sha256,
845 mbedtls_md_info_from_type(MBEDTLS_MD_SHA256),
846 0);
847 if (ret != 0) {
848 return ret;
849 }
850 ret = mbedtls_md_starts(&ssl->handshake->fin_sha256);
Manuel Pégourié-Gonnardb72ff492023-02-06 09:54:49 +0100851 if (ret != 0) {
852 return ret;
853 }
Jerry Yuc73c6182022-02-08 20:29:25 +0800854#endif
855#endif
Andrzej Kurek25f27152022-08-17 16:09:31 -0400856#if defined(MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Jerry Yuc73c6182022-02-08 20:29:25 +0800857#if defined(MBEDTLS_USE_PSA_CRYPTO)
Manuel Pégourié-Gonnardb72ff492023-02-06 09:54:49 +0100858 status = psa_hash_abort(&ssl->handshake->fin_sha384_psa);
859 if (status != PSA_SUCCESS) {
860 return mbedtls_md_error_from_psa(status);
861 }
862 status = psa_hash_setup(&ssl->handshake->fin_sha384_psa, PSA_ALG_SHA_384);
863 if (status != PSA_SUCCESS) {
864 return mbedtls_md_error_from_psa(status);
865 }
Jerry Yuc73c6182022-02-08 20:29:25 +0800866#else
Manuel Pégourié-Gonnard947cee12023-03-06 11:59:59 +0100867 mbedtls_md_free(&ssl->handshake->fin_sha384);
868 mbedtls_md_init(&ssl->handshake->fin_sha384);
Manuel Pégourié-Gonnardf057ecf2023-02-24 13:19:17 +0100869 ret = mbedtls_md_setup(&ssl->handshake->fin_sha384,
870 mbedtls_md_info_from_type(MBEDTLS_MD_SHA384), 0);
871 if (ret != 0) {
872 return ret;
873 }
874 ret = mbedtls_md_starts(&ssl->handshake->fin_sha384);
Manuel Pégourié-Gonnardb72ff492023-02-06 09:54:49 +0100875 if (ret != 0) {
876 return ret;
877 }
Jerry Yuc73c6182022-02-08 20:29:25 +0800878#endif
879#endif
Manuel Pégourié-Gonnard226aa152023-02-05 09:46:59 +0100880 return 0;
Jerry Yuc73c6182022-02-08 20:29:25 +0800881}
882
Manuel Pégourié-Gonnard226aa152023-02-05 09:46:59 +0100883static int ssl_update_checksum_start(mbedtls_ssl_context *ssl,
Manuel Pégourié-Gonnard43cc1272023-02-06 11:48:19 +0100884 const unsigned char *buf, size_t len)
Jerry Yuc73c6182022-02-08 20:29:25 +0800885{
Manuel Pégourié-Gonnard626aaed2023-02-06 22:03:06 +0100886#if defined(MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA) || \
887 defined(MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Manuel Pégourié-Gonnarddf949012023-02-06 10:00:52 +0100888#if defined(MBEDTLS_USE_PSA_CRYPTO)
889 psa_status_t status;
890#else
891 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
892#endif
Manuel Pégourié-Gonnard626aaed2023-02-06 22:03:06 +0100893#else /* SHA-256 or SHA-384 */
894 ((void) ssl);
895 (void) buf;
896 (void) len;
897#endif /* SHA-256 or SHA-384 */
Andrzej Kurek25f27152022-08-17 16:09:31 -0400898#if defined(MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Jerry Yuc73c6182022-02-08 20:29:25 +0800899#if defined(MBEDTLS_USE_PSA_CRYPTO)
Manuel Pégourié-Gonnarddf949012023-02-06 10:00:52 +0100900 status = psa_hash_update(&ssl->handshake->fin_sha256_psa, buf, len);
901 if (status != PSA_SUCCESS) {
902 return mbedtls_md_error_from_psa(status);
903 }
Jerry Yuc73c6182022-02-08 20:29:25 +0800904#else
Manuel Pégourié-Gonnardf057ecf2023-02-24 13:19:17 +0100905 ret = mbedtls_md_update(&ssl->handshake->fin_sha256, buf, len);
Manuel Pégourié-Gonnarddf949012023-02-06 10:00:52 +0100906 if (ret != 0) {
907 return ret;
908 }
Jerry Yuc73c6182022-02-08 20:29:25 +0800909#endif
910#endif
Andrzej Kurek25f27152022-08-17 16:09:31 -0400911#if defined(MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Jerry Yuc73c6182022-02-08 20:29:25 +0800912#if defined(MBEDTLS_USE_PSA_CRYPTO)
Manuel Pégourié-Gonnarddf949012023-02-06 10:00:52 +0100913 status = psa_hash_update(&ssl->handshake->fin_sha384_psa, buf, len);
914 if (status != PSA_SUCCESS) {
915 return mbedtls_md_error_from_psa(status);
916 }
Jerry Yuc73c6182022-02-08 20:29:25 +0800917#else
Manuel Pégourié-Gonnardf057ecf2023-02-24 13:19:17 +0100918 ret = mbedtls_md_update(&ssl->handshake->fin_sha384, buf, len);
Manuel Pégourié-Gonnarddf949012023-02-06 10:00:52 +0100919 if (ret != 0) {
920 return ret;
921 }
Jerry Yuc73c6182022-02-08 20:29:25 +0800922#endif
923#endif
Manuel Pégourié-Gonnard226aa152023-02-05 09:46:59 +0100924 return 0;
Jerry Yuc73c6182022-02-08 20:29:25 +0800925}
926
Andrzej Kurek25f27152022-08-17 16:09:31 -0400927#if defined(MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Manuel Pégourié-Gonnard226aa152023-02-05 09:46:59 +0100928static int ssl_update_checksum_sha256(mbedtls_ssl_context *ssl,
Manuel Pégourié-Gonnard43cc1272023-02-06 11:48:19 +0100929 const unsigned char *buf, size_t len)
Jerry Yuc73c6182022-02-08 20:29:25 +0800930{
931#if defined(MBEDTLS_USE_PSA_CRYPTO)
Manuel Pégourié-Gonnarddf949012023-02-06 10:00:52 +0100932 return mbedtls_md_error_from_psa(psa_hash_update(
Manuel Pégourié-Gonnard43cc1272023-02-06 11:48:19 +0100933 &ssl->handshake->fin_sha256_psa, buf, len));
Jerry Yuc73c6182022-02-08 20:29:25 +0800934#else
Manuel Pégourié-Gonnardf057ecf2023-02-24 13:19:17 +0100935 return mbedtls_md_update(&ssl->handshake->fin_sha256, buf, len);
Jerry Yuc73c6182022-02-08 20:29:25 +0800936#endif
937}
938#endif
939
Andrzej Kurek25f27152022-08-17 16:09:31 -0400940#if defined(MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Manuel Pégourié-Gonnard226aa152023-02-05 09:46:59 +0100941static int ssl_update_checksum_sha384(mbedtls_ssl_context *ssl,
Manuel Pégourié-Gonnard43cc1272023-02-06 11:48:19 +0100942 const unsigned char *buf, size_t len)
Jerry Yuc73c6182022-02-08 20:29:25 +0800943{
944#if defined(MBEDTLS_USE_PSA_CRYPTO)
Manuel Pégourié-Gonnarddf949012023-02-06 10:00:52 +0100945 return mbedtls_md_error_from_psa(psa_hash_update(
Manuel Pégourié-Gonnard43cc1272023-02-06 11:48:19 +0100946 &ssl->handshake->fin_sha384_psa, buf, len));
Jerry Yuc73c6182022-02-08 20:29:25 +0800947#else
Manuel Pégourié-Gonnardf057ecf2023-02-24 13:19:17 +0100948 return mbedtls_md_update(&ssl->handshake->fin_sha384, buf, len);
Jerry Yuc73c6182022-02-08 20:29:25 +0800949#endif
950}
951#endif
952
Gilles Peskine449bd832023-01-11 14:50:10 +0100953static void ssl_handshake_params_init(mbedtls_ssl_handshake_params *handshake)
Paul Bakkeraccaffe2014-06-26 13:37:14 +0200954{
Gilles Peskine449bd832023-01-11 14:50:10 +0100955 memset(handshake, 0, sizeof(mbedtls_ssl_handshake_params));
Paul Bakkeraccaffe2014-06-26 13:37:14 +0200956
Andrzej Kurek25f27152022-08-17 16:09:31 -0400957#if defined(MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Andrzej Kurekeb342242019-01-29 09:14:33 -0500958#if defined(MBEDTLS_USE_PSA_CRYPTO)
959 handshake->fin_sha256_psa = psa_hash_operation_init();
Andrzej Kurekeb342242019-01-29 09:14:33 -0500960#else
Manuel Pégourié-Gonnardf057ecf2023-02-24 13:19:17 +0100961 mbedtls_md_init(&handshake->fin_sha256);
Paul Bakkeraccaffe2014-06-26 13:37:14 +0200962#endif
Andrzej Kurekeb342242019-01-29 09:14:33 -0500963#endif
Andrzej Kurek25f27152022-08-17 16:09:31 -0400964#if defined(MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Andrzej Kurekeb342242019-01-29 09:14:33 -0500965#if defined(MBEDTLS_USE_PSA_CRYPTO)
Andrzej Kurek972fba52019-01-30 03:29:12 -0500966 handshake->fin_sha384_psa = psa_hash_operation_init();
Andrzej Kurekeb342242019-01-29 09:14:33 -0500967#else
Manuel Pégourié-Gonnardf057ecf2023-02-24 13:19:17 +0100968 mbedtls_md_init(&handshake->fin_sha384);
Paul Bakkeraccaffe2014-06-26 13:37:14 +0200969#endif
Andrzej Kurekeb342242019-01-29 09:14:33 -0500970#endif
Paul Bakkeraccaffe2014-06-26 13:37:14 +0200971
972 handshake->update_checksum = ssl_update_checksum_start;
Hanno Becker7e5437a2017-04-28 17:15:26 +0100973
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200974#if defined(MBEDTLS_DHM_C)
Gilles Peskine449bd832023-01-11 14:50:10 +0100975 mbedtls_dhm_init(&handshake->dhm_ctx);
Paul Bakkeraccaffe2014-06-26 13:37:14 +0200976#endif
Neil Armstrongf3f46412022-04-12 14:43:39 +0200977#if !defined(MBEDTLS_USE_PSA_CRYPTO) && defined(MBEDTLS_ECDH_C)
Gilles Peskine449bd832023-01-11 14:50:10 +0100978 mbedtls_ecdh_init(&handshake->ecdh_ctx);
Paul Bakkeraccaffe2014-06-26 13:37:14 +0200979#endif
Manuel Pégourié-Gonnardeef142d2015-09-16 10:05:04 +0200980#if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
Neil Armstrongca7d5062022-05-31 14:43:23 +0200981#if defined(MBEDTLS_USE_PSA_CRYPTO)
982 handshake->psa_pake_ctx = psa_pake_operation_init();
983 handshake->psa_pake_password = MBEDTLS_SVC_KEY_ID_INIT;
984#else
Gilles Peskine449bd832023-01-11 14:50:10 +0100985 mbedtls_ecjpake_init(&handshake->ecjpake_ctx);
Neil Armstrongca7d5062022-05-31 14:43:23 +0200986#endif /* MBEDTLS_USE_PSA_CRYPTO */
Manuel Pégourié-Gonnard77c06462015-09-17 13:59:49 +0200987#if defined(MBEDTLS_SSL_CLI_C)
988 handshake->ecjpake_cache = NULL;
989 handshake->ecjpake_cache_len = 0;
990#endif
Manuel Pégourié-Gonnard76cfd3f2015-09-15 12:10:54 +0200991#endif
Manuel Pégourié-Gonnardcdc26ae2015-06-19 12:16:31 +0200992
Gilles Peskineeccd8882020-03-10 12:19:08 +0100993#if defined(MBEDTLS_SSL_ECP_RESTARTABLE_ENABLED)
Gilles Peskine449bd832023-01-11 14:50:10 +0100994 mbedtls_x509_crt_restart_init(&handshake->ecrs_ctx);
Manuel Pégourié-Gonnard862cde52017-05-17 11:56:15 +0200995#endif
996
Manuel Pégourié-Gonnardcdc26ae2015-06-19 12:16:31 +0200997#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
998 handshake->sni_authmode = MBEDTLS_SSL_VERIFY_UNSET;
999#endif
Hanno Becker75173122019-02-06 16:18:31 +00001000
1001#if defined(MBEDTLS_X509_CRT_PARSE_C) && \
1002 !defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
Gilles Peskine449bd832023-01-11 14:50:10 +01001003 mbedtls_pk_init(&handshake->peer_pubkey);
Hanno Becker75173122019-02-06 16:18:31 +00001004#endif
Paul Bakkeraccaffe2014-06-26 13:37:14 +02001005}
1006
Gilles Peskine449bd832023-01-11 14:50:10 +01001007void mbedtls_ssl_transform_init(mbedtls_ssl_transform *transform)
Paul Bakkeraccaffe2014-06-26 13:37:14 +02001008{
Gilles Peskine449bd832023-01-11 14:50:10 +01001009 memset(transform, 0, sizeof(mbedtls_ssl_transform));
Paul Bakker84bbeb52014-07-01 14:53:22 +02001010
Przemyslaw Stekiel8f80fb92022-01-11 08:28:13 +01001011#if defined(MBEDTLS_USE_PSA_CRYPTO)
1012 transform->psa_key_enc = MBEDTLS_SVC_KEY_ID_INIT;
1013 transform->psa_key_dec = MBEDTLS_SVC_KEY_ID_INIT;
Przemyslaw Stekiel6be9cf52022-01-19 16:00:22 +01001014#else
Gilles Peskine449bd832023-01-11 14:50:10 +01001015 mbedtls_cipher_init(&transform->cipher_ctx_enc);
1016 mbedtls_cipher_init(&transform->cipher_ctx_dec);
Przemyslaw Stekiel8f80fb92022-01-11 08:28:13 +01001017#endif
1018
Hanno Beckerfd86ca82020-11-30 08:54:23 +00001019#if defined(MBEDTLS_SSL_SOME_SUITES_USE_MAC)
Neil Armstrong39b8e7d2022-02-23 09:24:45 +01001020#if defined(MBEDTLS_USE_PSA_CRYPTO)
1021 transform->psa_mac_enc = MBEDTLS_SVC_KEY_ID_INIT;
1022 transform->psa_mac_dec = MBEDTLS_SVC_KEY_ID_INIT;
Neil Armstrongcf8841a2022-02-24 11:17:45 +01001023#else
Gilles Peskine449bd832023-01-11 14:50:10 +01001024 mbedtls_md_init(&transform->md_ctx_enc);
1025 mbedtls_md_init(&transform->md_ctx_dec);
Hanno Beckerd56ed242018-01-03 15:32:51 +00001026#endif
Neil Armstrongcf8841a2022-02-24 11:17:45 +01001027#endif
Paul Bakkeraccaffe2014-06-26 13:37:14 +02001028}
1029
Gilles Peskine449bd832023-01-11 14:50:10 +01001030void mbedtls_ssl_session_init(mbedtls_ssl_session *session)
Paul Bakkeraccaffe2014-06-26 13:37:14 +02001031{
Gilles Peskine449bd832023-01-11 14:50:10 +01001032 memset(session, 0, sizeof(mbedtls_ssl_session));
Paul Bakkeraccaffe2014-06-26 13:37:14 +02001033}
1034
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02001035MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01001036static int ssl_handshake_init(mbedtls_ssl_context *ssl)
Paul Bakker48916f92012-09-16 19:57:18 +00001037{
Manuel Pégourié-Gonnardb8b07aa2023-02-06 00:34:21 +01001038 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
1039
Paul Bakkeraccaffe2014-06-26 13:37:14 +02001040 /* Clear old handshake information if present */
Jerry Yu2e199812022-12-01 18:57:19 +08001041#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
Gilles Peskine449bd832023-01-11 14:50:10 +01001042 if (ssl->transform_negotiate) {
1043 mbedtls_ssl_transform_free(ssl->transform_negotiate);
1044 }
Jerry Yu2e199812022-12-01 18:57:19 +08001045#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001046 if (ssl->session_negotiate) {
1047 mbedtls_ssl_session_free(ssl->session_negotiate);
1048 }
1049 if (ssl->handshake) {
1050 mbedtls_ssl_handshake_free(ssl);
1051 }
Paul Bakkeraccaffe2014-06-26 13:37:14 +02001052
Jerry Yu2e199812022-12-01 18:57:19 +08001053#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
Paul Bakkeraccaffe2014-06-26 13:37:14 +02001054 /*
1055 * Either the pointers are now NULL or cleared properly and can be freed.
1056 * Now allocate missing structures.
1057 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001058 if (ssl->transform_negotiate == NULL) {
1059 ssl->transform_negotiate = mbedtls_calloc(1, sizeof(mbedtls_ssl_transform));
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02001060 }
Jerry Yu2e199812022-12-01 18:57:19 +08001061#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakker48916f92012-09-16 19:57:18 +00001062
Gilles Peskine449bd832023-01-11 14:50:10 +01001063 if (ssl->session_negotiate == NULL) {
1064 ssl->session_negotiate = mbedtls_calloc(1, sizeof(mbedtls_ssl_session));
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02001065 }
Paul Bakker48916f92012-09-16 19:57:18 +00001066
Gilles Peskine449bd832023-01-11 14:50:10 +01001067 if (ssl->handshake == NULL) {
1068 ssl->handshake = mbedtls_calloc(1, sizeof(mbedtls_ssl_handshake_params));
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02001069 }
Andrzej Kurek0afa2a12020-03-03 10:39:58 -05001070#if defined(MBEDTLS_SSL_VARIABLE_BUFFER_LENGTH)
1071 /* If the buffers are too small - reallocate */
Andrzej Kurek8ea68722020-04-03 06:40:47 -04001072
Gilles Peskine449bd832023-01-11 14:50:10 +01001073 handle_buffer_resizing(ssl, 0, MBEDTLS_SSL_IN_BUFFER_LEN,
1074 MBEDTLS_SSL_OUT_BUFFER_LEN);
Andrzej Kurek0afa2a12020-03-03 10:39:58 -05001075#endif
Paul Bakker48916f92012-09-16 19:57:18 +00001076
Paul Bakkeraccaffe2014-06-26 13:37:14 +02001077 /* All pointers should exist and can be directly freed without issue */
Gilles Peskine449bd832023-01-11 14:50:10 +01001078 if (ssl->handshake == NULL ||
Jerry Yu2e199812022-12-01 18:57:19 +08001079#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
Paul Bakker48916f92012-09-16 19:57:18 +00001080 ssl->transform_negotiate == NULL ||
Jerry Yu2e199812022-12-01 18:57:19 +08001081#endif
Gilles Peskine449bd832023-01-11 14:50:10 +01001082 ssl->session_negotiate == NULL) {
1083 MBEDTLS_SSL_DEBUG_MSG(1, ("alloc() of ssl sub-contexts failed"));
Paul Bakkeraccaffe2014-06-26 13:37:14 +02001084
Gilles Peskine449bd832023-01-11 14:50:10 +01001085 mbedtls_free(ssl->handshake);
Paul Bakkeraccaffe2014-06-26 13:37:14 +02001086 ssl->handshake = NULL;
Jerry Yu2e199812022-12-01 18:57:19 +08001087
1088#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
Gilles Peskine449bd832023-01-11 14:50:10 +01001089 mbedtls_free(ssl->transform_negotiate);
Paul Bakkeraccaffe2014-06-26 13:37:14 +02001090 ssl->transform_negotiate = NULL;
Jerry Yu2e199812022-12-01 18:57:19 +08001091#endif
1092
Gilles Peskine449bd832023-01-11 14:50:10 +01001093 mbedtls_free(ssl->session_negotiate);
Paul Bakkeraccaffe2014-06-26 13:37:14 +02001094 ssl->session_negotiate = NULL;
1095
Gilles Peskine449bd832023-01-11 14:50:10 +01001096 return MBEDTLS_ERR_SSL_ALLOC_FAILED;
Paul Bakker48916f92012-09-16 19:57:18 +00001097 }
1098
Paul Bakkeraccaffe2014-06-26 13:37:14 +02001099 /* Initialize structures */
Gilles Peskine449bd832023-01-11 14:50:10 +01001100 mbedtls_ssl_session_init(ssl->session_negotiate);
1101 ssl_handshake_params_init(ssl->handshake);
Paul Bakker968afaa2014-07-09 11:09:24 +02001102
Jerry Yu2e199812022-12-01 18:57:19 +08001103#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
Gilles Peskine449bd832023-01-11 14:50:10 +01001104 mbedtls_ssl_transform_init(ssl->transform_negotiate);
Jerry Yu2e199812022-12-01 18:57:19 +08001105#endif
1106
Manuel Pégourié-Gonnard537f2312023-02-05 10:17:45 +01001107 /* Setup handshake checksums */
Manuel Pégourié-Gonnardb8b07aa2023-02-06 00:34:21 +01001108 ret = mbedtls_ssl_reset_checksum(ssl);
1109 if (ret != 0) {
1110 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ssl_reset_checksum", ret);
1111 return ret;
1112 }
Manuel Pégourié-Gonnard537f2312023-02-05 10:17:45 +01001113
Jerry Yud0766ec2022-09-22 10:46:57 +08001114#if defined(MBEDTLS_SSL_PROTO_TLS1_3) && \
1115 defined(MBEDTLS_SSL_SRV_C) && \
1116 defined(MBEDTLS_SSL_SESSION_TICKETS)
1117 ssl->handshake->new_session_tickets_count =
Gilles Peskine449bd832023-01-11 14:50:10 +01001118 ssl->conf->new_session_tickets_count;
Jerry Yud0766ec2022-09-22 10:46:57 +08001119#endif
1120
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001121#if defined(MBEDTLS_SSL_PROTO_DTLS)
Gilles Peskine449bd832023-01-11 14:50:10 +01001122 if (ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM) {
Manuel Pégourié-Gonnard06939ce2015-05-11 11:25:46 +02001123 ssl->handshake->alt_transform_out = ssl->transform_out;
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02001124
Gilles Peskine449bd832023-01-11 14:50:10 +01001125 if (ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT) {
Manuel Pégourié-Gonnard06939ce2015-05-11 11:25:46 +02001126 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_PREPARING;
Gilles Peskine449bd832023-01-11 14:50:10 +01001127 } else {
Manuel Pégourié-Gonnard06939ce2015-05-11 11:25:46 +02001128 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_WAITING;
Gilles Peskine449bd832023-01-11 14:50:10 +01001129 }
Manuel Pégourié-Gonnard286a1362015-05-13 16:22:05 +02001130
Gilles Peskine449bd832023-01-11 14:50:10 +01001131 mbedtls_ssl_set_timer(ssl, 0);
Manuel Pégourié-Gonnard06939ce2015-05-11 11:25:46 +02001132 }
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02001133#endif
1134
Brett Warrene0edc842021-08-17 09:53:13 +01001135/*
1136 * curve_list is translated to IANA TLS group identifiers here because
1137 * mbedtls_ssl_conf_curves returns void and so can't return
1138 * any error codes.
1139 */
1140#if defined(MBEDTLS_ECP_C)
1141#if !defined(MBEDTLS_DEPRECATED_REMOVED)
1142 /* Heap allocate and translate curve_list from internal to IANA group ids */
Gilles Peskine449bd832023-01-11 14:50:10 +01001143 if (ssl->conf->curve_list != NULL) {
Brett Warrene0edc842021-08-17 09:53:13 +01001144 size_t length;
1145 const mbedtls_ecp_group_id *curve_list = ssl->conf->curve_list;
1146
Gilles Peskine449bd832023-01-11 14:50:10 +01001147 for (length = 0; (curve_list[length] != MBEDTLS_ECP_DP_NONE) &&
1148 (length < MBEDTLS_ECP_DP_MAX); length++) {
1149 }
Brett Warrene0edc842021-08-17 09:53:13 +01001150
1151 /* Leave room for zero termination */
Gilles Peskine449bd832023-01-11 14:50:10 +01001152 uint16_t *group_list = mbedtls_calloc(length + 1, sizeof(uint16_t));
1153 if (group_list == NULL) {
1154 return MBEDTLS_ERR_SSL_ALLOC_FAILED;
1155 }
Brett Warrene0edc842021-08-17 09:53:13 +01001156
Gilles Peskine449bd832023-01-11 14:50:10 +01001157 for (size_t i = 0; i < length; i++) {
Valerio Setti18c9fed2022-12-30 17:44:24 +01001158 uint16_t tls_id = mbedtls_ssl_get_tls_id_from_ecp_group_id(
Gilles Peskine449bd832023-01-11 14:50:10 +01001159 curve_list[i]);
1160 if (tls_id == 0) {
1161 mbedtls_free(group_list);
1162 return MBEDTLS_ERR_SSL_BAD_CONFIG;
Brett Warrene0edc842021-08-17 09:53:13 +01001163 }
Valerio Setti18c9fed2022-12-30 17:44:24 +01001164 group_list[i] = tls_id;
Brett Warrene0edc842021-08-17 09:53:13 +01001165 }
1166
1167 group_list[length] = 0;
1168
1169 ssl->handshake->group_list = group_list;
1170 ssl->handshake->group_list_heap_allocated = 1;
Gilles Peskine449bd832023-01-11 14:50:10 +01001171 } else {
Brett Warrene0edc842021-08-17 09:53:13 +01001172 ssl->handshake->group_list = ssl->conf->group_list;
1173 ssl->handshake->group_list_heap_allocated = 0;
1174 }
1175#endif /* MBEDTLS_DEPRECATED_REMOVED */
1176#endif /* MBEDTLS_ECP_C */
1177
Ronald Crone68ab4f2022-10-05 12:46:29 +02001178#if defined(MBEDTLS_SSL_HANDSHAKE_WITH_CERT_ENABLED)
Jerry Yuf017ee42022-01-12 15:49:48 +08001179#if !defined(MBEDTLS_DEPRECATED_REMOVED)
Jerry Yua69269a2022-01-17 21:06:01 +08001180#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
Jerry Yu713013f2022-01-17 18:16:35 +08001181 /* Heap allocate and translate sig_hashes from internal hash identifiers to
1182 signature algorithms IANA identifiers. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001183 if (mbedtls_ssl_conf_is_tls12_only(ssl->conf) &&
1184 ssl->conf->sig_hashes != NULL) {
Jerry Yuf017ee42022-01-12 15:49:48 +08001185 const int *md;
1186 const int *sig_hashes = ssl->conf->sig_hashes;
Jerry Yub476a442022-01-21 18:14:45 +08001187 size_t sig_algs_len = 0;
Jerry Yuf017ee42022-01-12 15:49:48 +08001188 uint16_t *p;
1189
Jerry Yub476a442022-01-21 18:14:45 +08001190#if defined(static_assert)
Gilles Peskine449bd832023-01-11 14:50:10 +01001191 static_assert(MBEDTLS_SSL_MAX_SIG_ALG_LIST_LEN
1192 <= (SIZE_MAX - (2 * sizeof(uint16_t))),
1193 "MBEDTLS_SSL_MAX_SIG_ALG_LIST_LEN too big");
Jerry Yua68dca22022-01-20 16:28:27 +08001194#endif
1195
Gilles Peskine449bd832023-01-11 14:50:10 +01001196 for (md = sig_hashes; *md != MBEDTLS_MD_NONE; md++) {
1197 if (mbedtls_ssl_hash_from_md_alg(*md) == MBEDTLS_SSL_HASH_NONE) {
Jerry Yuf017ee42022-01-12 15:49:48 +08001198 continue;
Gilles Peskine449bd832023-01-11 14:50:10 +01001199 }
Jerry Yub476a442022-01-21 18:14:45 +08001200#if defined(MBEDTLS_ECDSA_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01001201 sig_algs_len += sizeof(uint16_t);
Jerry Yub476a442022-01-21 18:14:45 +08001202#endif
Jerry Yua68dca22022-01-20 16:28:27 +08001203
Jerry Yub476a442022-01-21 18:14:45 +08001204#if defined(MBEDTLS_RSA_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01001205 sig_algs_len += sizeof(uint16_t);
Jerry Yub476a442022-01-21 18:14:45 +08001206#endif
Gilles Peskine449bd832023-01-11 14:50:10 +01001207 if (sig_algs_len > MBEDTLS_SSL_MAX_SIG_ALG_LIST_LEN) {
1208 return MBEDTLS_ERR_SSL_BAD_CONFIG;
1209 }
Jerry Yuf017ee42022-01-12 15:49:48 +08001210 }
1211
Gilles Peskine449bd832023-01-11 14:50:10 +01001212 if (sig_algs_len < MBEDTLS_SSL_MIN_SIG_ALG_LIST_LEN) {
1213 return MBEDTLS_ERR_SSL_BAD_CONFIG;
1214 }
Jerry Yuf017ee42022-01-12 15:49:48 +08001215
Gilles Peskine449bd832023-01-11 14:50:10 +01001216 ssl->handshake->sig_algs = mbedtls_calloc(1, sig_algs_len +
1217 sizeof(uint16_t));
1218 if (ssl->handshake->sig_algs == NULL) {
1219 return MBEDTLS_ERR_SSL_ALLOC_FAILED;
1220 }
Jerry Yuf017ee42022-01-12 15:49:48 +08001221
Gilles Peskine449bd832023-01-11 14:50:10 +01001222 p = (uint16_t *) ssl->handshake->sig_algs;
1223 for (md = sig_hashes; *md != MBEDTLS_MD_NONE; md++) {
1224 unsigned char hash = mbedtls_ssl_hash_from_md_alg(*md);
1225 if (hash == MBEDTLS_SSL_HASH_NONE) {
Jerry Yuf017ee42022-01-12 15:49:48 +08001226 continue;
Gilles Peskine449bd832023-01-11 14:50:10 +01001227 }
Jerry Yu6106fdc2022-01-12 16:36:14 +08001228#if defined(MBEDTLS_ECDSA_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01001229 *p = ((hash << 8) | MBEDTLS_SSL_SIG_ECDSA);
Jerry Yuf017ee42022-01-12 15:49:48 +08001230 p++;
Jerry Yu6106fdc2022-01-12 16:36:14 +08001231#endif
1232#if defined(MBEDTLS_RSA_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01001233 *p = ((hash << 8) | MBEDTLS_SSL_SIG_RSA);
Jerry Yuf017ee42022-01-12 15:49:48 +08001234 p++;
Jerry Yu6106fdc2022-01-12 16:36:14 +08001235#endif
Jerry Yuf017ee42022-01-12 15:49:48 +08001236 }
Gabor Mezei15b95a62022-05-09 16:37:58 +02001237 *p = MBEDTLS_TLS_SIG_NONE;
Jerry Yuf017ee42022-01-12 15:49:48 +08001238 ssl->handshake->sig_algs_heap_allocated = 1;
Gilles Peskine449bd832023-01-11 14:50:10 +01001239 } else
Jerry Yua69269a2022-01-17 21:06:01 +08001240#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Jerry Yuf017ee42022-01-12 15:49:48 +08001241 {
Jerry Yuf017ee42022-01-12 15:49:48 +08001242 ssl->handshake->sig_algs_heap_allocated = 0;
1243 }
Jerry Yucc539102022-06-27 16:27:35 +08001244#endif /* !MBEDTLS_DEPRECATED_REMOVED */
Ronald Crone68ab4f2022-10-05 12:46:29 +02001245#endif /* MBEDTLS_SSL_HANDSHAKE_WITH_CERT_ENABLED */
Gilles Peskine449bd832023-01-11 14:50:10 +01001246 return 0;
Paul Bakker48916f92012-09-16 19:57:18 +00001247}
1248
Manuel Pégourié-Gonnarde057d3b2015-05-20 10:59:43 +02001249#if defined(MBEDTLS_SSL_DTLS_HELLO_VERIFY) && defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard7d38d212014-07-23 17:52:09 +02001250/* Dummy cookie callbacks for defaults */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02001251MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01001252static int ssl_cookie_write_dummy(void *ctx,
1253 unsigned char **p, unsigned char *end,
1254 const unsigned char *cli_id, size_t cli_id_len)
Manuel Pégourié-Gonnard7d38d212014-07-23 17:52:09 +02001255{
1256 ((void) ctx);
1257 ((void) p);
1258 ((void) end);
1259 ((void) cli_id);
1260 ((void) cli_id_len);
1261
Gilles Peskine449bd832023-01-11 14:50:10 +01001262 return MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
Manuel Pégourié-Gonnard7d38d212014-07-23 17:52:09 +02001263}
1264
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02001265MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01001266static int ssl_cookie_check_dummy(void *ctx,
1267 const unsigned char *cookie, size_t cookie_len,
1268 const unsigned char *cli_id, size_t cli_id_len)
Manuel Pégourié-Gonnard7d38d212014-07-23 17:52:09 +02001269{
1270 ((void) ctx);
1271 ((void) cookie);
1272 ((void) cookie_len);
1273 ((void) cli_id);
1274 ((void) cli_id_len);
1275
Gilles Peskine449bd832023-01-11 14:50:10 +01001276 return MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
Manuel Pégourié-Gonnard7d38d212014-07-23 17:52:09 +02001277}
Manuel Pégourié-Gonnarde057d3b2015-05-20 10:59:43 +02001278#endif /* MBEDTLS_SSL_DTLS_HELLO_VERIFY && MBEDTLS_SSL_SRV_C */
Manuel Pégourié-Gonnard7d38d212014-07-23 17:52:09 +02001279
Paul Bakker5121ce52009-01-03 21:22:43 +00001280/*
1281 * Initialize an SSL context
1282 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001283void mbedtls_ssl_init(mbedtls_ssl_context *ssl)
Manuel Pégourié-Gonnard41d479e2015-04-29 00:48:22 +02001284{
Gilles Peskine449bd832023-01-11 14:50:10 +01001285 memset(ssl, 0, sizeof(mbedtls_ssl_context));
Manuel Pégourié-Gonnard41d479e2015-04-29 00:48:22 +02001286}
1287
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02001288MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01001289static int ssl_conf_version_check(const mbedtls_ssl_context *ssl)
Jerry Yu60835a82021-08-04 10:13:52 +08001290{
Ronald Cron086ee0b2022-03-15 15:18:51 +01001291 const mbedtls_ssl_config *conf = ssl->conf;
1292
Ronald Cron6f135e12021-12-08 16:57:54 +01001293#if defined(MBEDTLS_SSL_PROTO_TLS1_3)
Gilles Peskine449bd832023-01-11 14:50:10 +01001294 if (mbedtls_ssl_conf_is_tls13_only(conf)) {
1295 if (conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM) {
1296 MBEDTLS_SSL_DEBUG_MSG(1, ("DTLS 1.3 is not yet supported."));
1297 return MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
XiaokangQianed582dd2022-04-13 08:21:05 +00001298 }
1299
Gilles Peskine449bd832023-01-11 14:50:10 +01001300 MBEDTLS_SSL_DEBUG_MSG(4, ("The SSL configuration is tls13 only."));
1301 return 0;
Jerry Yu60835a82021-08-04 10:13:52 +08001302 }
1303#endif
1304
1305#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
Gilles Peskine449bd832023-01-11 14:50:10 +01001306 if (mbedtls_ssl_conf_is_tls12_only(conf)) {
1307 MBEDTLS_SSL_DEBUG_MSG(4, ("The SSL configuration is tls12 only."));
1308 return 0;
Jerry Yu60835a82021-08-04 10:13:52 +08001309 }
1310#endif
1311
Ronald Cron6f135e12021-12-08 16:57:54 +01001312#if defined(MBEDTLS_SSL_PROTO_TLS1_2) && defined(MBEDTLS_SSL_PROTO_TLS1_3)
Gilles Peskine449bd832023-01-11 14:50:10 +01001313 if (mbedtls_ssl_conf_is_hybrid_tls12_tls13(conf)) {
1314 if (ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM) {
1315 MBEDTLS_SSL_DEBUG_MSG(1, ("DTLS not yet supported in Hybrid TLS 1.3 + TLS 1.2"));
1316 return MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
XiaokangQianed582dd2022-04-13 08:21:05 +00001317 }
1318
Gilles Peskine449bd832023-01-11 14:50:10 +01001319 if (conf->endpoint == MBEDTLS_SSL_IS_SERVER) {
1320 MBEDTLS_SSL_DEBUG_MSG(1, ("TLS 1.3 server is not supported yet."));
1321 return MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
XiaokangQian8f9dfe42022-04-15 02:52:39 +00001322 }
1323
1324
Gilles Peskine449bd832023-01-11 14:50:10 +01001325 MBEDTLS_SSL_DEBUG_MSG(4, ("The SSL configuration is TLS 1.3 or TLS 1.2."));
1326 return 0;
Jerry Yu60835a82021-08-04 10:13:52 +08001327 }
1328#endif
1329
Gilles Peskine449bd832023-01-11 14:50:10 +01001330 MBEDTLS_SSL_DEBUG_MSG(1, ("The SSL configuration is invalid."));
1331 return MBEDTLS_ERR_SSL_BAD_CONFIG;
Jerry Yu60835a82021-08-04 10:13:52 +08001332}
1333
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02001334MBEDTLS_CHECK_RETURN_CRITICAL
Jerry Yu60835a82021-08-04 10:13:52 +08001335static int ssl_conf_check(const mbedtls_ssl_context *ssl)
1336{
1337 int ret;
Gilles Peskine449bd832023-01-11 14:50:10 +01001338 ret = ssl_conf_version_check(ssl);
1339 if (ret != 0) {
1340 return ret;
1341 }
Jerry Yu60835a82021-08-04 10:13:52 +08001342
Jerry Yudef7ae42022-10-30 14:13:19 +08001343#if defined(MBEDTLS_SSL_PROTO_TLS1_3)
1344 /* RFC 8446 section 4.4.3
1345 *
1346 * If the verification fails, the receiver MUST terminate the handshake with
1347 * a "decrypt_error" alert.
1348 *
1349 * If the client is configured as TLS 1.3 only with optional verify, return
1350 * bad config.
1351 *
1352 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001353 if (mbedtls_ssl_conf_tls13_ephemeral_enabled(
1354 (mbedtls_ssl_context *) ssl) &&
Jerry Yudef7ae42022-10-30 14:13:19 +08001355 ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT &&
1356 ssl->conf->max_tls_version == MBEDTLS_SSL_VERSION_TLS1_3 &&
1357 ssl->conf->min_tls_version == MBEDTLS_SSL_VERSION_TLS1_3 &&
Gilles Peskine449bd832023-01-11 14:50:10 +01001358 ssl->conf->authmode == MBEDTLS_SSL_VERIFY_OPTIONAL) {
Jerry Yudef7ae42022-10-30 14:13:19 +08001359 MBEDTLS_SSL_DEBUG_MSG(
Gilles Peskine449bd832023-01-11 14:50:10 +01001360 1, ("Optional verify auth mode "
1361 "is not available for TLS 1.3 client"));
1362 return MBEDTLS_ERR_SSL_BAD_CONFIG;
Jerry Yudef7ae42022-10-30 14:13:19 +08001363 }
1364#endif /* MBEDTLS_SSL_PROTO_TLS1_3 */
1365
Jerry Yu60835a82021-08-04 10:13:52 +08001366 /* Space for further checks */
1367
Gilles Peskine449bd832023-01-11 14:50:10 +01001368 return 0;
Jerry Yu60835a82021-08-04 10:13:52 +08001369}
1370
Manuel Pégourié-Gonnard41d479e2015-04-29 00:48:22 +02001371/*
1372 * Setup an SSL context
1373 */
Hanno Becker2a43f6f2018-08-10 11:12:52 +01001374
Gilles Peskine449bd832023-01-11 14:50:10 +01001375int mbedtls_ssl_setup(mbedtls_ssl_context *ssl,
1376 const mbedtls_ssl_config *conf)
Paul Bakker5121ce52009-01-03 21:22:43 +00001377{
Janos Follath865b3eb2019-12-16 11:46:15 +00001378 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Darryl Greenb33cc762019-11-28 14:29:44 +00001379 size_t in_buf_len = MBEDTLS_SSL_IN_BUFFER_LEN;
1380 size_t out_buf_len = MBEDTLS_SSL_OUT_BUFFER_LEN;
Paul Bakker5121ce52009-01-03 21:22:43 +00001381
Manuel Pégourié-Gonnarddef0bbe2015-05-04 14:56:36 +02001382 ssl->conf = conf;
Paul Bakker62f2dee2012-09-28 07:31:51 +00001383
Gilles Peskine449bd832023-01-11 14:50:10 +01001384 if ((ret = ssl_conf_check(ssl)) != 0) {
1385 return ret;
1386 }
Jerry Yu60835a82021-08-04 10:13:52 +08001387
Paul Bakker62f2dee2012-09-28 07:31:51 +00001388 /*
Manuel Pégourié-Gonnard06193482014-02-14 08:39:32 +01001389 * Prepare base structures
Paul Bakker62f2dee2012-09-28 07:31:51 +00001390 */
k-stachowiakc9a5f022018-07-24 13:53:31 +02001391
1392 /* Set to NULL in case of an error condition */
1393 ssl->out_buf = NULL;
k-stachowiaka47911c2018-07-04 17:41:58 +02001394
Darryl Greenb33cc762019-11-28 14:29:44 +00001395#if defined(MBEDTLS_SSL_VARIABLE_BUFFER_LENGTH)
1396 ssl->in_buf_len = in_buf_len;
1397#endif
Gilles Peskine449bd832023-01-11 14:50:10 +01001398 ssl->in_buf = mbedtls_calloc(1, in_buf_len);
1399 if (ssl->in_buf == NULL) {
1400 MBEDTLS_SSL_DEBUG_MSG(1, ("alloc(%" MBEDTLS_PRINTF_SIZET " bytes) failed", in_buf_len));
k-stachowiak9f7798e2018-07-31 16:52:32 +02001401 ret = MBEDTLS_ERR_SSL_ALLOC_FAILED;
k-stachowiaka47911c2018-07-04 17:41:58 +02001402 goto error;
Angus Grattond8213d02016-05-25 20:56:48 +10001403 }
1404
Darryl Greenb33cc762019-11-28 14:29:44 +00001405#if defined(MBEDTLS_SSL_VARIABLE_BUFFER_LENGTH)
1406 ssl->out_buf_len = out_buf_len;
1407#endif
Gilles Peskine449bd832023-01-11 14:50:10 +01001408 ssl->out_buf = mbedtls_calloc(1, out_buf_len);
1409 if (ssl->out_buf == NULL) {
1410 MBEDTLS_SSL_DEBUG_MSG(1, ("alloc(%" MBEDTLS_PRINTF_SIZET " bytes) failed", out_buf_len));
k-stachowiak9f7798e2018-07-31 16:52:32 +02001411 ret = MBEDTLS_ERR_SSL_ALLOC_FAILED;
k-stachowiaka47911c2018-07-04 17:41:58 +02001412 goto error;
Paul Bakker5121ce52009-01-03 21:22:43 +00001413 }
1414
Gilles Peskine449bd832023-01-11 14:50:10 +01001415 mbedtls_ssl_reset_in_out_pointers(ssl);
Manuel Pégourié-Gonnard419d5ae2015-05-04 19:32:36 +02001416
Johan Pascalb62bb512015-12-03 21:56:45 +01001417#if defined(MBEDTLS_SSL_DTLS_SRTP)
Gilles Peskine449bd832023-01-11 14:50:10 +01001418 memset(&ssl->dtls_srtp_info, 0, sizeof(ssl->dtls_srtp_info));
Johan Pascalb62bb512015-12-03 21:56:45 +01001419#endif
1420
Gilles Peskine449bd832023-01-11 14:50:10 +01001421 if ((ret = ssl_handshake_init(ssl)) != 0) {
k-stachowiaka47911c2018-07-04 17:41:58 +02001422 goto error;
Gilles Peskine449bd832023-01-11 14:50:10 +01001423 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001424
Gilles Peskine449bd832023-01-11 14:50:10 +01001425 return 0;
k-stachowiaka47911c2018-07-04 17:41:58 +02001426
1427error:
Gilles Peskine449bd832023-01-11 14:50:10 +01001428 mbedtls_free(ssl->in_buf);
1429 mbedtls_free(ssl->out_buf);
k-stachowiaka47911c2018-07-04 17:41:58 +02001430
1431 ssl->conf = NULL;
1432
Darryl Greenb33cc762019-11-28 14:29:44 +00001433#if defined(MBEDTLS_SSL_VARIABLE_BUFFER_LENGTH)
1434 ssl->in_buf_len = 0;
1435 ssl->out_buf_len = 0;
1436#endif
k-stachowiaka47911c2018-07-04 17:41:58 +02001437 ssl->in_buf = NULL;
1438 ssl->out_buf = NULL;
1439
1440 ssl->in_hdr = NULL;
1441 ssl->in_ctr = NULL;
1442 ssl->in_len = NULL;
1443 ssl->in_iv = NULL;
1444 ssl->in_msg = NULL;
1445
1446 ssl->out_hdr = NULL;
1447 ssl->out_ctr = NULL;
1448 ssl->out_len = NULL;
1449 ssl->out_iv = NULL;
1450 ssl->out_msg = NULL;
1451
Gilles Peskine449bd832023-01-11 14:50:10 +01001452 return ret;
Paul Bakker5121ce52009-01-03 21:22:43 +00001453}
1454
1455/*
Paul Bakker7eb013f2011-10-06 12:37:39 +00001456 * Reset an initialized and used SSL context for re-use while retaining
1457 * all application-set variables, function pointers and data.
Manuel Pégourié-Gonnard3f09b6d2015-09-08 11:58:14 +02001458 *
1459 * If partial is non-zero, keep data in the input buffer and client ID.
1460 * (Use when a DTLS client reconnects from the same port.)
Paul Bakker7eb013f2011-10-06 12:37:39 +00001461 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001462void mbedtls_ssl_session_reset_msg_layer(mbedtls_ssl_context *ssl,
1463 int partial)
Paul Bakker7eb013f2011-10-06 12:37:39 +00001464{
Darryl Greenb33cc762019-11-28 14:29:44 +00001465#if defined(MBEDTLS_SSL_VARIABLE_BUFFER_LENGTH)
1466 size_t in_buf_len = ssl->in_buf_len;
1467 size_t out_buf_len = ssl->out_buf_len;
1468#else
1469 size_t in_buf_len = MBEDTLS_SSL_IN_BUFFER_LEN;
1470 size_t out_buf_len = MBEDTLS_SSL_OUT_BUFFER_LEN;
1471#endif
Paul Bakker48916f92012-09-16 19:57:18 +00001472
Hanno Beckerb0302c42021-08-03 09:39:42 +01001473#if !defined(MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE) || !defined(MBEDTLS_SSL_SRV_C)
1474 partial = 0;
Hanno Becker7e772132018-08-10 12:38:21 +01001475#endif
1476
Manuel Pégourié-Gonnard286a1362015-05-13 16:22:05 +02001477 /* Cancel any possibly running timer */
Gilles Peskine449bd832023-01-11 14:50:10 +01001478 mbedtls_ssl_set_timer(ssl, 0);
Manuel Pégourié-Gonnard286a1362015-05-13 16:22:05 +02001479
Gilles Peskine449bd832023-01-11 14:50:10 +01001480 mbedtls_ssl_reset_in_out_pointers(ssl);
Hanno Beckerb0302c42021-08-03 09:39:42 +01001481
1482 /* Reset incoming message parsing */
1483 ssl->in_offt = NULL;
1484 ssl->nb_zero = 0;
1485 ssl->in_msgtype = 0;
1486 ssl->in_msglen = 0;
1487 ssl->in_hslen = 0;
1488 ssl->keep_current_message = 0;
1489 ssl->transform_in = NULL;
1490
1491#if defined(MBEDTLS_SSL_PROTO_DTLS)
1492 ssl->next_record_offset = 0;
1493 ssl->in_epoch = 0;
1494#endif
1495
1496 /* Keep current datagram if partial == 1 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001497 if (partial == 0) {
Hanno Beckerb0302c42021-08-03 09:39:42 +01001498 ssl->in_left = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +01001499 memset(ssl->in_buf, 0, in_buf_len);
Hanno Beckerb0302c42021-08-03 09:39:42 +01001500 }
1501
Ronald Cronad8c17b2022-06-10 17:18:09 +02001502 ssl->send_alert = 0;
1503
Hanno Beckerb0302c42021-08-03 09:39:42 +01001504 /* Reset outgoing message writing */
1505 ssl->out_msgtype = 0;
1506 ssl->out_msglen = 0;
1507 ssl->out_left = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +01001508 memset(ssl->out_buf, 0, out_buf_len);
1509 memset(ssl->cur_out_ctr, 0, sizeof(ssl->cur_out_ctr));
Hanno Beckerb0302c42021-08-03 09:39:42 +01001510 ssl->transform_out = NULL;
1511
1512#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
Gilles Peskine449bd832023-01-11 14:50:10 +01001513 mbedtls_ssl_dtls_replay_reset(ssl);
Hanno Beckerb0302c42021-08-03 09:39:42 +01001514#endif
1515
XiaokangQian2b01dc32022-01-21 02:53:13 +00001516#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
Gilles Peskine449bd832023-01-11 14:50:10 +01001517 if (ssl->transform) {
1518 mbedtls_ssl_transform_free(ssl->transform);
1519 mbedtls_free(ssl->transform);
Hanno Beckerb0302c42021-08-03 09:39:42 +01001520 ssl->transform = NULL;
1521 }
XiaokangQian2b01dc32022-01-21 02:53:13 +00001522#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
1523
XiaokangQian2b01dc32022-01-21 02:53:13 +00001524#if defined(MBEDTLS_SSL_PROTO_TLS1_3)
Gilles Peskine449bd832023-01-11 14:50:10 +01001525 mbedtls_ssl_transform_free(ssl->transform_application);
1526 mbedtls_free(ssl->transform_application);
XiaokangQian2b01dc32022-01-21 02:53:13 +00001527 ssl->transform_application = NULL;
1528
Gilles Peskine449bd832023-01-11 14:50:10 +01001529 if (ssl->handshake != NULL) {
Jerry Yu3d9b5902022-11-04 14:07:25 +08001530#if defined(MBEDTLS_SSL_EARLY_DATA)
Gilles Peskine449bd832023-01-11 14:50:10 +01001531 mbedtls_ssl_transform_free(ssl->handshake->transform_earlydata);
1532 mbedtls_free(ssl->handshake->transform_earlydata);
XiaokangQian2b01dc32022-01-21 02:53:13 +00001533 ssl->handshake->transform_earlydata = NULL;
Jerry Yu3d9b5902022-11-04 14:07:25 +08001534#endif
XiaokangQian2b01dc32022-01-21 02:53:13 +00001535
Gilles Peskine449bd832023-01-11 14:50:10 +01001536 mbedtls_ssl_transform_free(ssl->handshake->transform_handshake);
1537 mbedtls_free(ssl->handshake->transform_handshake);
XiaokangQian2b01dc32022-01-21 02:53:13 +00001538 ssl->handshake->transform_handshake = NULL;
1539 }
1540
XiaokangQian2b01dc32022-01-21 02:53:13 +00001541#endif /* MBEDTLS_SSL_PROTO_TLS1_3 */
Hanno Beckerb0302c42021-08-03 09:39:42 +01001542}
1543
Gilles Peskine449bd832023-01-11 14:50:10 +01001544int mbedtls_ssl_session_reset_int(mbedtls_ssl_context *ssl, int partial)
Hanno Beckerb0302c42021-08-03 09:39:42 +01001545{
1546 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
1547
1548 ssl->state = MBEDTLS_SSL_HELLO_REQUEST;
1549
Gilles Peskine449bd832023-01-11 14:50:10 +01001550 mbedtls_ssl_session_reset_msg_layer(ssl, partial);
Hanno Beckerb0302c42021-08-03 09:39:42 +01001551
1552 /* Reset renegotiation state */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001553#if defined(MBEDTLS_SSL_RENEGOTIATION)
1554 ssl->renego_status = MBEDTLS_SSL_INITIAL_HANDSHAKE;
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01001555 ssl->renego_records_seen = 0;
Paul Bakker48916f92012-09-16 19:57:18 +00001556
1557 ssl->verify_data_len = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +01001558 memset(ssl->own_verify_data, 0, MBEDTLS_SSL_VERIFY_DATA_MAX_LEN);
1559 memset(ssl->peer_verify_data, 0, MBEDTLS_SSL_VERIFY_DATA_MAX_LEN);
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01001560#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001561 ssl->secure_renegotiation = MBEDTLS_SSL_LEGACY_RENEGOTIATION;
Paul Bakker48916f92012-09-16 19:57:18 +00001562
Hanno Beckerb0302c42021-08-03 09:39:42 +01001563 ssl->session_in = NULL;
Hanno Becker78640902018-08-13 16:35:15 +01001564 ssl->session_out = NULL;
Gilles Peskine449bd832023-01-11 14:50:10 +01001565 if (ssl->session) {
1566 mbedtls_ssl_session_free(ssl->session);
1567 mbedtls_free(ssl->session);
Paul Bakkerc0463502013-02-14 11:19:38 +01001568 ssl->session = NULL;
1569 }
1570
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001571#if defined(MBEDTLS_SSL_ALPN)
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02001572 ssl->alpn_chosen = NULL;
1573#endif
1574
Manuel Pégourié-Gonnarde057d3b2015-05-20 10:59:43 +02001575#if defined(MBEDTLS_SSL_DTLS_HELLO_VERIFY) && defined(MBEDTLS_SSL_SRV_C)
David Horstmann3b2276a2022-10-06 14:49:08 +01001576 int free_cli_id = 1;
Hanno Becker4ccbf062018-08-10 11:20:38 +01001577#if defined(MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE)
Gilles Peskine449bd832023-01-11 14:50:10 +01001578 free_cli_id = (partial == 0);
Hanno Becker4ccbf062018-08-10 11:20:38 +01001579#endif
Gilles Peskine449bd832023-01-11 14:50:10 +01001580 if (free_cli_id) {
1581 mbedtls_free(ssl->cli_id);
Manuel Pégourié-Gonnard3f09b6d2015-09-08 11:58:14 +02001582 ssl->cli_id = NULL;
1583 ssl->cli_id_len = 0;
1584 }
Manuel Pégourié-Gonnard43c02182014-07-22 17:32:01 +02001585#endif
1586
Gilles Peskine449bd832023-01-11 14:50:10 +01001587 if ((ret = ssl_handshake_init(ssl)) != 0) {
1588 return ret;
1589 }
Paul Bakker2770fbd2012-07-03 13:30:23 +00001590
Gilles Peskine449bd832023-01-11 14:50:10 +01001591 return 0;
Paul Bakker7eb013f2011-10-06 12:37:39 +00001592}
1593
Manuel Pégourié-Gonnard779e4292013-08-03 13:50:48 +02001594/*
Manuel Pégourié-Gonnard3f09b6d2015-09-08 11:58:14 +02001595 * Reset an initialized and used SSL context for re-use while retaining
1596 * all application-set variables, function pointers and data.
1597 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001598int mbedtls_ssl_session_reset(mbedtls_ssl_context *ssl)
Manuel Pégourié-Gonnard3f09b6d2015-09-08 11:58:14 +02001599{
Gilles Peskine449bd832023-01-11 14:50:10 +01001600 return mbedtls_ssl_session_reset_int(ssl, 0);
Manuel Pégourié-Gonnard3f09b6d2015-09-08 11:58:14 +02001601}
1602
1603/*
Paul Bakker5121ce52009-01-03 21:22:43 +00001604 * SSL set accessors
1605 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001606void mbedtls_ssl_conf_endpoint(mbedtls_ssl_config *conf, int endpoint)
Paul Bakker5121ce52009-01-03 21:22:43 +00001607{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02001608 conf->endpoint = endpoint;
Paul Bakker5121ce52009-01-03 21:22:43 +00001609}
1610
Gilles Peskine449bd832023-01-11 14:50:10 +01001611void mbedtls_ssl_conf_transport(mbedtls_ssl_config *conf, int transport)
Manuel Pégourié-Gonnard0b1ff292014-02-06 13:04:16 +01001612{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02001613 conf->transport = transport;
Manuel Pégourié-Gonnard0b1ff292014-02-06 13:04:16 +01001614}
1615
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001616#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
Gilles Peskine449bd832023-01-11 14:50:10 +01001617void mbedtls_ssl_conf_dtls_anti_replay(mbedtls_ssl_config *conf, char mode)
Manuel Pégourié-Gonnard27393132014-09-24 14:41:11 +02001618{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02001619 conf->anti_replay = mode;
Manuel Pégourié-Gonnard27393132014-09-24 14:41:11 +02001620}
1621#endif
1622
Gilles Peskine449bd832023-01-11 14:50:10 +01001623void mbedtls_ssl_conf_dtls_badmac_limit(mbedtls_ssl_config *conf, unsigned limit)
Manuel Pégourié-Gonnardb0643d12014-10-14 18:30:36 +02001624{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02001625 conf->badmac_limit = limit;
Manuel Pégourié-Gonnardb0643d12014-10-14 18:30:36 +02001626}
Manuel Pégourié-Gonnardb0643d12014-10-14 18:30:36 +02001627
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001628#if defined(MBEDTLS_SSL_PROTO_DTLS)
Hanno Becker04da1892018-08-14 13:22:10 +01001629
Gilles Peskine449bd832023-01-11 14:50:10 +01001630void mbedtls_ssl_set_datagram_packing(mbedtls_ssl_context *ssl,
1631 unsigned allow_packing)
Hanno Becker04da1892018-08-14 13:22:10 +01001632{
1633 ssl->disable_datagram_packing = !allow_packing;
1634}
1635
Gilles Peskine449bd832023-01-11 14:50:10 +01001636void mbedtls_ssl_conf_handshake_timeout(mbedtls_ssl_config *conf,
1637 uint32_t min, uint32_t max)
Manuel Pégourié-Gonnard905dd242014-10-01 12:03:55 +02001638{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02001639 conf->hs_timeout_min = min;
1640 conf->hs_timeout_max = max;
Manuel Pégourié-Gonnard905dd242014-10-01 12:03:55 +02001641}
1642#endif
1643
Gilles Peskine449bd832023-01-11 14:50:10 +01001644void mbedtls_ssl_conf_authmode(mbedtls_ssl_config *conf, int authmode)
Paul Bakker5121ce52009-01-03 21:22:43 +00001645{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02001646 conf->authmode = authmode;
Paul Bakker5121ce52009-01-03 21:22:43 +00001647}
1648
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001649#if defined(MBEDTLS_X509_CRT_PARSE_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01001650void mbedtls_ssl_conf_verify(mbedtls_ssl_config *conf,
1651 int (*f_vrfy)(void *, mbedtls_x509_crt *, int, uint32_t *),
1652 void *p_vrfy)
Paul Bakkerb63b0af2011-01-13 17:54:59 +00001653{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02001654 conf->f_vrfy = f_vrfy;
1655 conf->p_vrfy = p_vrfy;
Paul Bakkerb63b0af2011-01-13 17:54:59 +00001656}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001657#endif /* MBEDTLS_X509_CRT_PARSE_C */
Paul Bakkerb63b0af2011-01-13 17:54:59 +00001658
Gilles Peskine449bd832023-01-11 14:50:10 +01001659void mbedtls_ssl_conf_rng(mbedtls_ssl_config *conf,
1660 int (*f_rng)(void *, unsigned char *, size_t),
1661 void *p_rng)
Paul Bakker5121ce52009-01-03 21:22:43 +00001662{
Manuel Pégourié-Gonnard750e4d72015-05-07 12:35:38 +01001663 conf->f_rng = f_rng;
1664 conf->p_rng = p_rng;
Paul Bakker5121ce52009-01-03 21:22:43 +00001665}
1666
Gilles Peskine449bd832023-01-11 14:50:10 +01001667void mbedtls_ssl_conf_dbg(mbedtls_ssl_config *conf,
1668 void (*f_dbg)(void *, int, const char *, int, const char *),
1669 void *p_dbg)
Paul Bakker5121ce52009-01-03 21:22:43 +00001670{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02001671 conf->f_dbg = f_dbg;
1672 conf->p_dbg = p_dbg;
Paul Bakker5121ce52009-01-03 21:22:43 +00001673}
1674
Gilles Peskine449bd832023-01-11 14:50:10 +01001675void mbedtls_ssl_set_bio(mbedtls_ssl_context *ssl,
1676 void *p_bio,
1677 mbedtls_ssl_send_t *f_send,
1678 mbedtls_ssl_recv_t *f_recv,
1679 mbedtls_ssl_recv_timeout_t *f_recv_timeout)
Manuel Pégourié-Gonnard8fa6dfd2014-09-17 10:47:43 +02001680{
1681 ssl->p_bio = p_bio;
1682 ssl->f_send = f_send;
1683 ssl->f_recv = f_recv;
1684 ssl->f_recv_timeout = f_recv_timeout;
Manuel Pégourié-Gonnard97fd52c2015-05-06 15:38:52 +01001685}
1686
Manuel Pégourié-Gonnard6e7aaca2018-08-20 10:37:23 +02001687#if defined(MBEDTLS_SSL_PROTO_DTLS)
Gilles Peskine449bd832023-01-11 14:50:10 +01001688void mbedtls_ssl_set_mtu(mbedtls_ssl_context *ssl, uint16_t mtu)
Manuel Pégourié-Gonnard6e7aaca2018-08-20 10:37:23 +02001689{
1690 ssl->mtu = mtu;
1691}
1692#endif
1693
Gilles Peskine449bd832023-01-11 14:50:10 +01001694void mbedtls_ssl_conf_read_timeout(mbedtls_ssl_config *conf, uint32_t timeout)
Manuel Pégourié-Gonnard97fd52c2015-05-06 15:38:52 +01001695{
1696 conf->read_timeout = timeout;
Manuel Pégourié-Gonnard8fa6dfd2014-09-17 10:47:43 +02001697}
1698
Gilles Peskine449bd832023-01-11 14:50:10 +01001699void mbedtls_ssl_set_timer_cb(mbedtls_ssl_context *ssl,
1700 void *p_timer,
1701 mbedtls_ssl_set_timer_t *f_set_timer,
1702 mbedtls_ssl_get_timer_t *f_get_timer)
Manuel Pégourié-Gonnard2e012912015-05-12 20:55:41 +02001703{
1704 ssl->p_timer = p_timer;
1705 ssl->f_set_timer = f_set_timer;
1706 ssl->f_get_timer = f_get_timer;
Manuel Pégourié-Gonnard286a1362015-05-13 16:22:05 +02001707
1708 /* Make sure we start with no timer running */
Gilles Peskine449bd832023-01-11 14:50:10 +01001709 mbedtls_ssl_set_timer(ssl, 0);
Manuel Pégourié-Gonnard2e012912015-05-12 20:55:41 +02001710}
1711
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001712#if defined(MBEDTLS_SSL_SRV_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01001713void mbedtls_ssl_conf_session_cache(mbedtls_ssl_config *conf,
1714 void *p_cache,
1715 mbedtls_ssl_cache_get_t *f_get_cache,
1716 mbedtls_ssl_cache_set_t *f_set_cache)
Paul Bakker5121ce52009-01-03 21:22:43 +00001717{
Manuel Pégourié-Gonnard5cb33082015-05-06 18:06:26 +01001718 conf->p_cache = p_cache;
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02001719 conf->f_get_cache = f_get_cache;
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02001720 conf->f_set_cache = f_set_cache;
Paul Bakker5121ce52009-01-03 21:22:43 +00001721}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001722#endif /* MBEDTLS_SSL_SRV_C */
Paul Bakker5121ce52009-01-03 21:22:43 +00001723
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001724#if defined(MBEDTLS_SSL_CLI_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01001725int mbedtls_ssl_set_session(mbedtls_ssl_context *ssl, const mbedtls_ssl_session *session)
Paul Bakker5121ce52009-01-03 21:22:43 +00001726{
Janos Follath865b3eb2019-12-16 11:46:15 +00001727 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +02001728
Gilles Peskine449bd832023-01-11 14:50:10 +01001729 if (ssl == NULL ||
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +02001730 session == NULL ||
1731 ssl->session_negotiate == NULL ||
Gilles Peskine449bd832023-01-11 14:50:10 +01001732 ssl->conf->endpoint != MBEDTLS_SSL_IS_CLIENT) {
1733 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +02001734 }
1735
Gilles Peskine449bd832023-01-11 14:50:10 +01001736 if (ssl->handshake->resume == 1) {
1737 return MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
1738 }
Hanno Beckere810bbc2021-05-14 16:01:05 +01001739
Jerry Yu21092062022-10-10 21:21:31 +08001740#if defined(MBEDTLS_SSL_PROTO_TLS1_3)
Gilles Peskine449bd832023-01-11 14:50:10 +01001741 if (session->tls_version == MBEDTLS_SSL_VERSION_TLS1_3) {
Jerry Yu21092062022-10-10 21:21:31 +08001742 const mbedtls_ssl_ciphersuite_t *ciphersuite_info =
Gilles Peskine449bd832023-01-11 14:50:10 +01001743 mbedtls_ssl_ciphersuite_from_id(session->ciphersuite);
Jerry Yu21092062022-10-10 21:21:31 +08001744
Gilles Peskine449bd832023-01-11 14:50:10 +01001745 if (mbedtls_ssl_validate_ciphersuite(
Jerry Yu21092062022-10-10 21:21:31 +08001746 ssl, ciphersuite_info, MBEDTLS_SSL_VERSION_TLS1_3,
Gilles Peskine449bd832023-01-11 14:50:10 +01001747 MBEDTLS_SSL_VERSION_TLS1_3) != 0) {
1748 MBEDTLS_SSL_DEBUG_MSG(4, ("%d is not a valid TLS 1.3 ciphersuite.",
1749 session->ciphersuite));
1750 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
Jerry Yu21092062022-10-10 21:21:31 +08001751 }
Jerry Yu40afab62022-10-08 10:42:13 +08001752 }
Jerry Yu21092062022-10-10 21:21:31 +08001753#endif /* MBEDTLS_SSL_PROTO_TLS1_3 */
Jerry Yu40afab62022-10-08 10:42:13 +08001754
Gilles Peskine449bd832023-01-11 14:50:10 +01001755 if ((ret = mbedtls_ssl_session_copy(ssl->session_negotiate,
1756 session)) != 0) {
1757 return ret;
1758 }
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +02001759
Paul Bakker0a597072012-09-25 21:55:46 +00001760 ssl->handshake->resume = 1;
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +02001761
Gilles Peskine449bd832023-01-11 14:50:10 +01001762 return 0;
Paul Bakker5121ce52009-01-03 21:22:43 +00001763}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001764#endif /* MBEDTLS_SSL_CLI_C */
Paul Bakker5121ce52009-01-03 21:22:43 +00001765
Gilles Peskine449bd832023-01-11 14:50:10 +01001766void mbedtls_ssl_conf_ciphersuites(mbedtls_ssl_config *conf,
1767 const int *ciphersuites)
Mateusz Starzyk06b07fb2021-02-18 13:55:21 +01001768{
Hanno Beckerd60b6c62021-04-29 12:04:11 +01001769 conf->ciphersuite_list = ciphersuites;
Paul Bakker5121ce52009-01-03 21:22:43 +00001770}
1771
Ronald Cron6f135e12021-12-08 16:57:54 +01001772#if defined(MBEDTLS_SSL_PROTO_TLS1_3)
Gilles Peskine449bd832023-01-11 14:50:10 +01001773void mbedtls_ssl_conf_tls13_key_exchange_modes(mbedtls_ssl_config *conf,
1774 const int kex_modes)
Hanno Becker71f1ed62021-07-24 06:01:47 +01001775{
Xiaofei Bai746f9482021-11-12 08:53:56 +00001776 conf->tls13_kex_modes = kex_modes & MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_ALL;
Hanno Becker71f1ed62021-07-24 06:01:47 +01001777}
Xiaokang Qian72de95d2022-10-25 02:54:33 +00001778
1779#if defined(MBEDTLS_SSL_EARLY_DATA)
Gilles Peskine449bd832023-01-11 14:50:10 +01001780void mbedtls_ssl_tls13_conf_early_data(mbedtls_ssl_config *conf,
1781 int early_data_enabled)
Xiaokang Qian72de95d2022-10-25 02:54:33 +00001782{
1783 conf->early_data_enabled = early_data_enabled;
1784}
Jerry Yucc4e0072022-11-22 17:22:22 +08001785
1786#if defined(MBEDTLS_SSL_SRV_C)
1787void mbedtls_ssl_tls13_conf_max_early_data_size(
Gilles Peskine449bd832023-01-11 14:50:10 +01001788 mbedtls_ssl_config *conf, uint32_t max_early_data_size)
Jerry Yucc4e0072022-11-22 17:22:22 +08001789{
Jerry Yu39da9852022-12-06 16:58:36 +08001790 conf->max_early_data_size = max_early_data_size;
Jerry Yucc4e0072022-11-22 17:22:22 +08001791}
1792#endif /* MBEDTLS_SSL_SRV_C */
1793
Xiaokang Qian72de95d2022-10-25 02:54:33 +00001794#endif /* MBEDTLS_SSL_EARLY_DATA */
Ronald Cron6f135e12021-12-08 16:57:54 +01001795#endif /* MBEDTLS_SSL_PROTO_TLS1_3 */
Hanno Becker71f1ed62021-07-24 06:01:47 +01001796
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001797#if defined(MBEDTLS_X509_CRT_PARSE_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01001798void mbedtls_ssl_conf_cert_profile(mbedtls_ssl_config *conf,
1799 const mbedtls_x509_crt_profile *profile)
Manuel Pégourié-Gonnard6e3ee3a2015-06-17 10:58:20 +02001800{
1801 conf->cert_profile = profile;
1802}
1803
Gilles Peskine449bd832023-01-11 14:50:10 +01001804static void ssl_key_cert_free(mbedtls_ssl_key_cert *key_cert)
Glenn Strauss36872db2022-01-22 05:06:31 -05001805{
1806 mbedtls_ssl_key_cert *cur = key_cert, *next;
1807
Gilles Peskine449bd832023-01-11 14:50:10 +01001808 while (cur != NULL) {
Glenn Strauss36872db2022-01-22 05:06:31 -05001809 next = cur->next;
Gilles Peskine449bd832023-01-11 14:50:10 +01001810 mbedtls_free(cur);
Glenn Strauss36872db2022-01-22 05:06:31 -05001811 cur = next;
1812 }
1813}
1814
Manuel Pégourié-Gonnard8f618a82015-05-10 21:13:36 +02001815/* Append a new keycert entry to a (possibly empty) list */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02001816MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01001817static int ssl_append_key_cert(mbedtls_ssl_key_cert **head,
1818 mbedtls_x509_crt *cert,
1819 mbedtls_pk_context *key)
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02001820{
niisato8ee24222018-06-25 19:05:48 +09001821 mbedtls_ssl_key_cert *new_cert;
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02001822
Gilles Peskine449bd832023-01-11 14:50:10 +01001823 if (cert == NULL) {
Glenn Strauss36872db2022-01-22 05:06:31 -05001824 /* Free list if cert is null */
Gilles Peskine449bd832023-01-11 14:50:10 +01001825 ssl_key_cert_free(*head);
Glenn Strauss36872db2022-01-22 05:06:31 -05001826 *head = NULL;
Gilles Peskine449bd832023-01-11 14:50:10 +01001827 return 0;
Glenn Strauss36872db2022-01-22 05:06:31 -05001828 }
1829
Gilles Peskine449bd832023-01-11 14:50:10 +01001830 new_cert = mbedtls_calloc(1, sizeof(mbedtls_ssl_key_cert));
1831 if (new_cert == NULL) {
1832 return MBEDTLS_ERR_SSL_ALLOC_FAILED;
1833 }
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02001834
niisato8ee24222018-06-25 19:05:48 +09001835 new_cert->cert = cert;
1836 new_cert->key = key;
1837 new_cert->next = NULL;
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02001838
Glenn Strauss36872db2022-01-22 05:06:31 -05001839 /* Update head if the list was null, else add to the end */
Gilles Peskine449bd832023-01-11 14:50:10 +01001840 if (*head == NULL) {
niisato8ee24222018-06-25 19:05:48 +09001841 *head = new_cert;
Gilles Peskine449bd832023-01-11 14:50:10 +01001842 } else {
Manuel Pégourié-Gonnard8f618a82015-05-10 21:13:36 +02001843 mbedtls_ssl_key_cert *cur = *head;
Gilles Peskine449bd832023-01-11 14:50:10 +01001844 while (cur->next != NULL) {
Manuel Pégourié-Gonnard8f618a82015-05-10 21:13:36 +02001845 cur = cur->next;
Gilles Peskine449bd832023-01-11 14:50:10 +01001846 }
niisato8ee24222018-06-25 19:05:48 +09001847 cur->next = new_cert;
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02001848 }
1849
Gilles Peskine449bd832023-01-11 14:50:10 +01001850 return 0;
Manuel Pégourié-Gonnard8f618a82015-05-10 21:13:36 +02001851}
1852
Gilles Peskine449bd832023-01-11 14:50:10 +01001853int mbedtls_ssl_conf_own_cert(mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnard8f618a82015-05-10 21:13:36 +02001854 mbedtls_x509_crt *own_cert,
Gilles Peskine449bd832023-01-11 14:50:10 +01001855 mbedtls_pk_context *pk_key)
Manuel Pégourié-Gonnard8f618a82015-05-10 21:13:36 +02001856{
Gilles Peskine449bd832023-01-11 14:50:10 +01001857 return ssl_append_key_cert(&conf->key_cert, own_cert, pk_key);
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02001858}
1859
Gilles Peskine449bd832023-01-11 14:50:10 +01001860void mbedtls_ssl_conf_ca_chain(mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnardbc2b7712015-05-06 11:14:19 +01001861 mbedtls_x509_crt *ca_chain,
Gilles Peskine449bd832023-01-11 14:50:10 +01001862 mbedtls_x509_crl *ca_crl)
Paul Bakker5121ce52009-01-03 21:22:43 +00001863{
Manuel Pégourié-Gonnardbc2b7712015-05-06 11:14:19 +01001864 conf->ca_chain = ca_chain;
1865 conf->ca_crl = ca_crl;
Hanno Becker5adaad92019-03-27 16:54:37 +00001866
1867#if defined(MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK)
1868 /* mbedtls_ssl_conf_ca_chain() and mbedtls_ssl_conf_ca_cb()
1869 * cannot be used together. */
1870 conf->f_ca_cb = NULL;
1871 conf->p_ca_cb = NULL;
1872#endif /* MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK */
Paul Bakker5121ce52009-01-03 21:22:43 +00001873}
Hanno Becker5adaad92019-03-27 16:54:37 +00001874
1875#if defined(MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK)
Gilles Peskine449bd832023-01-11 14:50:10 +01001876void mbedtls_ssl_conf_ca_cb(mbedtls_ssl_config *conf,
1877 mbedtls_x509_crt_ca_cb_t f_ca_cb,
1878 void *p_ca_cb)
Hanno Becker5adaad92019-03-27 16:54:37 +00001879{
1880 conf->f_ca_cb = f_ca_cb;
1881 conf->p_ca_cb = p_ca_cb;
1882
1883 /* mbedtls_ssl_conf_ca_chain() and mbedtls_ssl_conf_ca_cb()
1884 * cannot be used together. */
1885 conf->ca_chain = NULL;
1886 conf->ca_crl = NULL;
1887}
1888#endif /* MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001889#endif /* MBEDTLS_X509_CRT_PARSE_C */
Paul Bakkereb2c6582012-09-27 19:15:01 +00001890
Manuel Pégourié-Gonnard1af6c852015-05-10 23:10:37 +02001891#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
Gilles Peskine449bd832023-01-11 14:50:10 +01001892const unsigned char *mbedtls_ssl_get_hs_sni(mbedtls_ssl_context *ssl,
1893 size_t *name_len)
Glenn Strauss69894072022-01-24 12:58:00 -05001894{
1895 *name_len = ssl->handshake->sni_name_len;
Gilles Peskine449bd832023-01-11 14:50:10 +01001896 return ssl->handshake->sni_name;
Glenn Strauss69894072022-01-24 12:58:00 -05001897}
1898
Gilles Peskine449bd832023-01-11 14:50:10 +01001899int mbedtls_ssl_set_hs_own_cert(mbedtls_ssl_context *ssl,
1900 mbedtls_x509_crt *own_cert,
1901 mbedtls_pk_context *pk_key)
Manuel Pégourié-Gonnard1af6c852015-05-10 23:10:37 +02001902{
Gilles Peskine449bd832023-01-11 14:50:10 +01001903 return ssl_append_key_cert(&ssl->handshake->sni_key_cert,
1904 own_cert, pk_key);
Manuel Pégourié-Gonnard1af6c852015-05-10 23:10:37 +02001905}
Manuel Pégourié-Gonnard22bfa4b2015-05-11 08:46:37 +02001906
Gilles Peskine449bd832023-01-11 14:50:10 +01001907void mbedtls_ssl_set_hs_ca_chain(mbedtls_ssl_context *ssl,
1908 mbedtls_x509_crt *ca_chain,
1909 mbedtls_x509_crl *ca_crl)
Manuel Pégourié-Gonnard22bfa4b2015-05-11 08:46:37 +02001910{
1911 ssl->handshake->sni_ca_chain = ca_chain;
1912 ssl->handshake->sni_ca_crl = ca_crl;
1913}
Manuel Pégourié-Gonnardcdc26ae2015-06-19 12:16:31 +02001914
Glenn Strauss999ef702022-03-11 01:37:23 -05001915#if defined(MBEDTLS_KEY_EXCHANGE_CERT_REQ_ALLOWED_ENABLED)
Gilles Peskine449bd832023-01-11 14:50:10 +01001916void mbedtls_ssl_set_hs_dn_hints(mbedtls_ssl_context *ssl,
1917 const mbedtls_x509_crt *crt)
Glenn Strauss999ef702022-03-11 01:37:23 -05001918{
1919 ssl->handshake->dn_hints = crt;
1920}
1921#endif /* MBEDTLS_KEY_EXCHANGE_CERT_REQ_ALLOWED_ENABLED */
1922
Gilles Peskine449bd832023-01-11 14:50:10 +01001923void mbedtls_ssl_set_hs_authmode(mbedtls_ssl_context *ssl,
1924 int authmode)
Manuel Pégourié-Gonnardcdc26ae2015-06-19 12:16:31 +02001925{
1926 ssl->handshake->sni_authmode = authmode;
1927}
Manuel Pégourié-Gonnard1af6c852015-05-10 23:10:37 +02001928#endif /* MBEDTLS_SSL_SERVER_NAME_INDICATION */
1929
Hanno Becker8927c832019-04-03 12:52:50 +01001930#if defined(MBEDTLS_X509_CRT_PARSE_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01001931void mbedtls_ssl_set_verify(mbedtls_ssl_context *ssl,
1932 int (*f_vrfy)(void *, mbedtls_x509_crt *, int, uint32_t *),
1933 void *p_vrfy)
Hanno Becker8927c832019-04-03 12:52:50 +01001934{
1935 ssl->f_vrfy = f_vrfy;
1936 ssl->p_vrfy = p_vrfy;
1937}
1938#endif
1939
Manuel Pégourié-Gonnardeef142d2015-09-16 10:05:04 +02001940#if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
Manuel Pégourié-Gonnard7002f4a2015-09-15 12:43:43 +02001941
Valerio Setti016f6822022-12-09 14:17:50 +01001942#if defined(MBEDTLS_USE_PSA_CRYPTO)
1943static psa_status_t mbedtls_ssl_set_hs_ecjpake_password_common(
Gilles Peskine449bd832023-01-11 14:50:10 +01001944 mbedtls_ssl_context *ssl,
1945 mbedtls_svc_key_id_t pwd)
Valerio Setti016f6822022-12-09 14:17:50 +01001946{
1947 psa_status_t status;
1948 psa_pake_role_t psa_role;
1949 psa_pake_cipher_suite_t cipher_suite = psa_pake_cipher_suite_init();
1950
Gilles Peskine449bd832023-01-11 14:50:10 +01001951 psa_pake_cs_set_algorithm(&cipher_suite, PSA_ALG_JPAKE);
1952 psa_pake_cs_set_primitive(&cipher_suite,
1953 PSA_PAKE_PRIMITIVE(PSA_PAKE_PRIMITIVE_TYPE_ECC,
1954 PSA_ECC_FAMILY_SECP_R1,
1955 256));
1956 psa_pake_cs_set_hash(&cipher_suite, PSA_ALG_SHA_256);
Valerio Setti016f6822022-12-09 14:17:50 +01001957
Gilles Peskine449bd832023-01-11 14:50:10 +01001958 status = psa_pake_setup(&ssl->handshake->psa_pake_ctx, &cipher_suite);
1959 if (status != PSA_SUCCESS) {
Valerio Setti016f6822022-12-09 14:17:50 +01001960 return status;
Gilles Peskine449bd832023-01-11 14:50:10 +01001961 }
Manuel Pégourié-Gonnard7002f4a2015-09-15 12:43:43 +02001962
Gilles Peskine449bd832023-01-11 14:50:10 +01001963 if (ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER) {
Neil Armstrongca7d5062022-05-31 14:43:23 +02001964 psa_role = PSA_PAKE_ROLE_SERVER;
Gilles Peskine449bd832023-01-11 14:50:10 +01001965 } else {
Neil Armstrongca7d5062022-05-31 14:43:23 +02001966 psa_role = PSA_PAKE_ROLE_CLIENT;
Gilles Peskine449bd832023-01-11 14:50:10 +01001967 }
Neil Armstrongca7d5062022-05-31 14:43:23 +02001968
Gilles Peskine449bd832023-01-11 14:50:10 +01001969 status = psa_pake_set_role(&ssl->handshake->psa_pake_ctx, psa_role);
1970 if (status != PSA_SUCCESS) {
Valerio Setti016f6822022-12-09 14:17:50 +01001971 return status;
Gilles Peskine449bd832023-01-11 14:50:10 +01001972 }
Valerio Setti016f6822022-12-09 14:17:50 +01001973
Gilles Peskine449bd832023-01-11 14:50:10 +01001974 status = psa_pake_set_password_key(&ssl->handshake->psa_pake_ctx, pwd);
1975 if (status != PSA_SUCCESS) {
Valerio Setti016f6822022-12-09 14:17:50 +01001976 return status;
Gilles Peskine449bd832023-01-11 14:50:10 +01001977 }
Valerio Setti016f6822022-12-09 14:17:50 +01001978
1979 ssl->handshake->psa_pake_ctx_is_ok = 1;
1980
Gilles Peskine449bd832023-01-11 14:50:10 +01001981 return PSA_SUCCESS;
Valerio Setti016f6822022-12-09 14:17:50 +01001982}
1983
Gilles Peskine449bd832023-01-11 14:50:10 +01001984int mbedtls_ssl_set_hs_ecjpake_password(mbedtls_ssl_context *ssl,
1985 const unsigned char *pw,
1986 size_t pw_len)
Valerio Setti016f6822022-12-09 14:17:50 +01001987{
1988 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
1989 psa_status_t status;
1990
Gilles Peskine449bd832023-01-11 14:50:10 +01001991 if (ssl->handshake == NULL || ssl->conf == NULL) {
1992 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
Neil Armstrongca7d5062022-05-31 14:43:23 +02001993 }
1994
Gilles Peskine449bd832023-01-11 14:50:10 +01001995 /* Empty password is not valid */
1996 if ((pw == NULL) || (pw_len == 0)) {
1997 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
1998 }
1999
2000 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_DERIVE);
2001 psa_set_key_algorithm(&attributes, PSA_ALG_JPAKE);
2002 psa_set_key_type(&attributes, PSA_KEY_TYPE_PASSWORD);
2003
2004 status = psa_import_key(&attributes, pw, pw_len,
2005 &ssl->handshake->psa_pake_password);
2006 if (status != PSA_SUCCESS) {
2007 return MBEDTLS_ERR_SSL_HW_ACCEL_FAILED;
2008 }
2009
2010 status = mbedtls_ssl_set_hs_ecjpake_password_common(ssl,
2011 ssl->handshake->psa_pake_password);
2012 if (status != PSA_SUCCESS) {
2013 psa_destroy_key(ssl->handshake->psa_pake_password);
2014 psa_pake_abort(&ssl->handshake->psa_pake_ctx);
2015 return MBEDTLS_ERR_SSL_HW_ACCEL_FAILED;
2016 }
2017
2018 return 0;
Valerio Setti02c25b52022-11-15 14:08:42 +01002019}
Valerio Settia9a97dc2022-11-28 18:26:16 +01002020
Gilles Peskine449bd832023-01-11 14:50:10 +01002021int mbedtls_ssl_set_hs_ecjpake_password_opaque(mbedtls_ssl_context *ssl,
2022 mbedtls_svc_key_id_t pwd)
Valerio Settia9a97dc2022-11-28 18:26:16 +01002023{
Valerio Settia9a97dc2022-11-28 18:26:16 +01002024 psa_status_t status;
2025
Gilles Peskine449bd832023-01-11 14:50:10 +01002026 if (ssl->handshake == NULL || ssl->conf == NULL) {
2027 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
Neil Armstrongca7d5062022-05-31 14:43:23 +02002028 }
2029
Gilles Peskine449bd832023-01-11 14:50:10 +01002030 if (mbedtls_svc_key_id_is_null(pwd)) {
2031 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
2032 }
2033
2034 status = mbedtls_ssl_set_hs_ecjpake_password_common(ssl, pwd);
2035 if (status != PSA_SUCCESS) {
2036 psa_pake_abort(&ssl->handshake->psa_pake_ctx);
2037 return MBEDTLS_ERR_SSL_HW_ACCEL_FAILED;
2038 }
2039
2040 return 0;
Valerio Setti02c25b52022-11-15 14:08:42 +01002041}
2042#else /* MBEDTLS_USE_PSA_CRYPTO */
Gilles Peskine449bd832023-01-11 14:50:10 +01002043int mbedtls_ssl_set_hs_ecjpake_password(mbedtls_ssl_context *ssl,
2044 const unsigned char *pw,
2045 size_t pw_len)
Manuel Pégourié-Gonnard7002f4a2015-09-15 12:43:43 +02002046{
2047 mbedtls_ecjpake_role role;
2048
Gilles Peskine449bd832023-01-11 14:50:10 +01002049 if (ssl->handshake == NULL || ssl->conf == NULL) {
2050 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
2051 }
Manuel Pégourié-Gonnard7002f4a2015-09-15 12:43:43 +02002052
Valerio Settic689ed82022-12-07 14:40:38 +01002053 /* Empty password is not valid */
Gilles Peskine449bd832023-01-11 14:50:10 +01002054 if ((pw == NULL) || (pw_len == 0)) {
2055 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
2056 }
Valerio Settic689ed82022-12-07 14:40:38 +01002057
Gilles Peskine449bd832023-01-11 14:50:10 +01002058 if (ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER) {
Manuel Pégourié-Gonnard7002f4a2015-09-15 12:43:43 +02002059 role = MBEDTLS_ECJPAKE_SERVER;
Gilles Peskine449bd832023-01-11 14:50:10 +01002060 } else {
Manuel Pégourié-Gonnard7002f4a2015-09-15 12:43:43 +02002061 role = MBEDTLS_ECJPAKE_CLIENT;
Gilles Peskine449bd832023-01-11 14:50:10 +01002062 }
Manuel Pégourié-Gonnard7002f4a2015-09-15 12:43:43 +02002063
Gilles Peskine449bd832023-01-11 14:50:10 +01002064 return mbedtls_ecjpake_setup(&ssl->handshake->ecjpake_ctx,
2065 role,
2066 MBEDTLS_MD_SHA256,
2067 MBEDTLS_ECP_DP_SECP256R1,
2068 pw, pw_len);
Manuel Pégourié-Gonnard7002f4a2015-09-15 12:43:43 +02002069}
Valerio Setti02c25b52022-11-15 14:08:42 +01002070#endif /* MBEDTLS_USE_PSA_CRYPTO */
Manuel Pégourié-Gonnardeef142d2015-09-16 10:05:04 +02002071#endif /* MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED */
Manuel Pégourié-Gonnard7002f4a2015-09-15 12:43:43 +02002072
Ronald Cron73fe8df2022-10-05 14:31:43 +02002073#if defined(MBEDTLS_SSL_HANDSHAKE_WITH_PSK_ENABLED)
Gilles Peskine449bd832023-01-11 14:50:10 +01002074int mbedtls_ssl_conf_has_static_psk(mbedtls_ssl_config const *conf)
Hanno Becker2ed3dce2021-04-19 21:59:14 +01002075{
Gilles Peskine449bd832023-01-11 14:50:10 +01002076 if (conf->psk_identity == NULL ||
2077 conf->psk_identity_len == 0) {
2078 return 0;
Ronald Crond29e13e2022-10-19 10:33:48 +02002079 }
2080
Hanno Becker2ed3dce2021-04-19 21:59:14 +01002081#if defined(MBEDTLS_USE_PSA_CRYPTO)
Gilles Peskine449bd832023-01-11 14:50:10 +01002082 if (!mbedtls_svc_key_id_is_null(conf->psk_opaque)) {
2083 return 1;
2084 }
Hanno Becker2ed3dce2021-04-19 21:59:14 +01002085#endif /* MBEDTLS_USE_PSA_CRYPTO */
Ronald Crond29e13e2022-10-19 10:33:48 +02002086
Gilles Peskine449bd832023-01-11 14:50:10 +01002087 if (conf->psk != NULL && conf->psk_len != 0) {
2088 return 1;
2089 }
Hanno Becker2ed3dce2021-04-19 21:59:14 +01002090
Gilles Peskine449bd832023-01-11 14:50:10 +01002091 return 0;
Hanno Becker2ed3dce2021-04-19 21:59:14 +01002092}
2093
Gilles Peskine449bd832023-01-11 14:50:10 +01002094static void ssl_conf_remove_psk(mbedtls_ssl_config *conf)
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01002095{
2096 /* Remove reference to existing PSK, if any. */
2097#if defined(MBEDTLS_USE_PSA_CRYPTO)
Gilles Peskine449bd832023-01-11 14:50:10 +01002098 if (!mbedtls_svc_key_id_is_null(conf->psk_opaque)) {
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01002099 /* The maintenance of the PSK key slot is the
2100 * user's responsibility. */
Ronald Croncf56a0a2020-08-04 09:51:30 +02002101 conf->psk_opaque = MBEDTLS_SVC_KEY_ID_INIT;
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01002102 }
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01002103#endif /* MBEDTLS_USE_PSA_CRYPTO */
Gilles Peskine449bd832023-01-11 14:50:10 +01002104 if (conf->psk != NULL) {
2105 mbedtls_platform_zeroize(conf->psk, conf->psk_len);
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01002106
Gilles Peskine449bd832023-01-11 14:50:10 +01002107 mbedtls_free(conf->psk);
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01002108 conf->psk = NULL;
2109 conf->psk_len = 0;
2110 }
2111
2112 /* Remove reference to PSK identity, if any. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002113 if (conf->psk_identity != NULL) {
2114 mbedtls_free(conf->psk_identity);
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01002115 conf->psk_identity = NULL;
2116 conf->psk_identity_len = 0;
2117 }
2118}
2119
Hanno Becker7390c712018-11-15 13:33:04 +00002120/* This function assumes that PSK identity in the SSL config is unset.
2121 * It checks that the provided identity is well-formed and attempts
2122 * to make a copy of it in the SSL config.
2123 * On failure, the PSK identity in the config remains unset. */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02002124MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01002125static int ssl_conf_set_psk_identity(mbedtls_ssl_config *conf,
2126 unsigned char const *psk_identity,
2127 size_t psk_identity_len)
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02002128{
Manuel Pégourié-Gonnardc6b5d832015-08-27 16:37:35 +02002129 /* Identity len will be encoded on two bytes */
Gilles Peskine449bd832023-01-11 14:50:10 +01002130 if (psk_identity == NULL ||
Ronald Cron2a87e9b2022-10-19 10:55:26 +02002131 psk_identity_len == 0 ||
Gilles Peskine449bd832023-01-11 14:50:10 +01002132 (psk_identity_len >> 16) != 0 ||
2133 psk_identity_len > MBEDTLS_SSL_OUT_CONTENT_LEN) {
2134 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
Manuel Pégourié-Gonnardc6b5d832015-08-27 16:37:35 +02002135 }
2136
Gilles Peskine449bd832023-01-11 14:50:10 +01002137 conf->psk_identity = mbedtls_calloc(1, psk_identity_len);
2138 if (conf->psk_identity == NULL) {
2139 return MBEDTLS_ERR_SSL_ALLOC_FAILED;
2140 }
Paul Bakker6db455e2013-09-18 17:29:31 +02002141
Manuel Pégourié-Gonnard120fdbd2015-05-07 17:07:50 +01002142 conf->psk_identity_len = psk_identity_len;
Gilles Peskine449bd832023-01-11 14:50:10 +01002143 memcpy(conf->psk_identity, psk_identity, conf->psk_identity_len);
Paul Bakker5ad403f2013-09-18 21:21:30 +02002144
Gilles Peskine449bd832023-01-11 14:50:10 +01002145 return 0;
Paul Bakker6db455e2013-09-18 17:29:31 +02002146}
2147
Gilles Peskine449bd832023-01-11 14:50:10 +01002148int mbedtls_ssl_conf_psk(mbedtls_ssl_config *conf,
2149 const unsigned char *psk, size_t psk_len,
2150 const unsigned char *psk_identity, size_t psk_identity_len)
Hanno Becker7390c712018-11-15 13:33:04 +00002151{
Janos Follath865b3eb2019-12-16 11:46:15 +00002152 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Hanno Becker2ed3dce2021-04-19 21:59:14 +01002153
2154 /* We currently only support one PSK, raw or opaque. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002155 if (mbedtls_ssl_conf_has_static_psk(conf)) {
2156 return MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
2157 }
Hanno Becker7390c712018-11-15 13:33:04 +00002158
2159 /* Check and set raw PSK */
Gilles Peskine449bd832023-01-11 14:50:10 +01002160 if (psk == NULL) {
2161 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
2162 }
2163 if (psk_len == 0) {
2164 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
2165 }
2166 if (psk_len > MBEDTLS_PSK_MAX_LEN) {
2167 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
2168 }
Piotr Nowicki9926eaf2019-11-20 14:54:36 +01002169
Gilles Peskine449bd832023-01-11 14:50:10 +01002170 if ((conf->psk = mbedtls_calloc(1, psk_len)) == NULL) {
2171 return MBEDTLS_ERR_SSL_ALLOC_FAILED;
2172 }
Hanno Becker7390c712018-11-15 13:33:04 +00002173 conf->psk_len = psk_len;
Gilles Peskine449bd832023-01-11 14:50:10 +01002174 memcpy(conf->psk, psk, conf->psk_len);
Hanno Becker7390c712018-11-15 13:33:04 +00002175
2176 /* Check and set PSK Identity */
Gilles Peskine449bd832023-01-11 14:50:10 +01002177 ret = ssl_conf_set_psk_identity(conf, psk_identity, psk_identity_len);
2178 if (ret != 0) {
2179 ssl_conf_remove_psk(conf);
2180 }
Hanno Becker7390c712018-11-15 13:33:04 +00002181
Gilles Peskine449bd832023-01-11 14:50:10 +01002182 return ret;
Hanno Becker7390c712018-11-15 13:33:04 +00002183}
2184
Gilles Peskine449bd832023-01-11 14:50:10 +01002185static void ssl_remove_psk(mbedtls_ssl_context *ssl)
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01002186{
2187#if defined(MBEDTLS_USE_PSA_CRYPTO)
Gilles Peskine449bd832023-01-11 14:50:10 +01002188 if (!mbedtls_svc_key_id_is_null(ssl->handshake->psk_opaque)) {
Neil Armstrong501c9322022-05-03 09:35:09 +02002189 /* The maintenance of the external PSK key slot is the
2190 * user's responsibility. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002191 if (ssl->handshake->psk_opaque_is_internal) {
2192 psa_destroy_key(ssl->handshake->psk_opaque);
Neil Armstrong501c9322022-05-03 09:35:09 +02002193 ssl->handshake->psk_opaque_is_internal = 0;
2194 }
Ronald Croncf56a0a2020-08-04 09:51:30 +02002195 ssl->handshake->psk_opaque = MBEDTLS_SVC_KEY_ID_INIT;
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01002196 }
Neil Armstronge952a302022-05-03 10:22:14 +02002197#else
Gilles Peskine449bd832023-01-11 14:50:10 +01002198 if (ssl->handshake->psk != NULL) {
2199 mbedtls_platform_zeroize(ssl->handshake->psk,
2200 ssl->handshake->psk_len);
2201 mbedtls_free(ssl->handshake->psk);
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01002202 ssl->handshake->psk_len = 0;
2203 }
Neil Armstronge952a302022-05-03 10:22:14 +02002204#endif /* MBEDTLS_USE_PSA_CRYPTO */
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01002205}
2206
Gilles Peskine449bd832023-01-11 14:50:10 +01002207int mbedtls_ssl_set_hs_psk(mbedtls_ssl_context *ssl,
2208 const unsigned char *psk, size_t psk_len)
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01002209{
Neil Armstrong501c9322022-05-03 09:35:09 +02002210#if defined(MBEDTLS_USE_PSA_CRYPTO)
2211 psa_key_attributes_t key_attributes = psa_key_attributes_init();
Jerry Yu5d01c052022-08-17 10:18:10 +08002212 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Jerry Yu24b8c812022-08-20 19:06:56 +08002213 psa_algorithm_t alg = PSA_ALG_NONE;
Jerry Yu5d01c052022-08-17 10:18:10 +08002214 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Neil Armstrong501c9322022-05-03 09:35:09 +02002215#endif /* MBEDTLS_USE_PSA_CRYPTO */
2216
Gilles Peskine449bd832023-01-11 14:50:10 +01002217 if (psk == NULL || ssl->handshake == NULL) {
2218 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
2219 }
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01002220
Gilles Peskine449bd832023-01-11 14:50:10 +01002221 if (psk_len > MBEDTLS_PSK_MAX_LEN) {
2222 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
2223 }
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01002224
Gilles Peskine449bd832023-01-11 14:50:10 +01002225 ssl_remove_psk(ssl);
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01002226
Neil Armstrong501c9322022-05-03 09:35:09 +02002227#if defined(MBEDTLS_USE_PSA_CRYPTO)
Jerry Yuccc68a42022-07-26 16:39:20 +08002228#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
Gilles Peskine449bd832023-01-11 14:50:10 +01002229 if (ssl->tls_version == MBEDTLS_SSL_VERSION_TLS1_2) {
2230 if (ssl->handshake->ciphersuite_info->mac == MBEDTLS_MD_SHA384) {
2231 alg = PSA_ALG_TLS12_PSK_TO_MS(PSA_ALG_SHA_384);
2232 } else {
2233 alg = PSA_ALG_TLS12_PSK_TO_MS(PSA_ALG_SHA_256);
2234 }
2235 psa_set_key_usage_flags(&key_attributes, PSA_KEY_USAGE_DERIVE);
Jerry Yuccc68a42022-07-26 16:39:20 +08002236 }
2237#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Neil Armstrong501c9322022-05-03 09:35:09 +02002238
Jerry Yu568ec252022-07-22 21:27:34 +08002239#if defined(MBEDTLS_SSL_PROTO_TLS1_3)
Gilles Peskine449bd832023-01-11 14:50:10 +01002240 if (ssl->tls_version == MBEDTLS_SSL_VERSION_TLS1_3) {
2241 alg = PSA_ALG_HKDF_EXTRACT(PSA_ALG_ANY_HASH);
2242 psa_set_key_usage_flags(&key_attributes,
2243 PSA_KEY_USAGE_DERIVE | PSA_KEY_USAGE_EXPORT);
Jerry Yuccc68a42022-07-26 16:39:20 +08002244 }
2245#endif /* MBEDTLS_SSL_PROTO_TLS1_3 */
2246
Gilles Peskine449bd832023-01-11 14:50:10 +01002247 psa_set_key_algorithm(&key_attributes, alg);
2248 psa_set_key_type(&key_attributes, PSA_KEY_TYPE_DERIVE);
Neil Armstrong501c9322022-05-03 09:35:09 +02002249
Gilles Peskine449bd832023-01-11 14:50:10 +01002250 status = psa_import_key(&key_attributes, psk, psk_len, &key);
2251 if (status != PSA_SUCCESS) {
2252 return MBEDTLS_ERR_SSL_HW_ACCEL_FAILED;
2253 }
Neil Armstrong501c9322022-05-03 09:35:09 +02002254
2255 /* Allow calling psa_destroy_key() on psk remove */
2256 ssl->handshake->psk_opaque_is_internal = 1;
Gilles Peskine449bd832023-01-11 14:50:10 +01002257 return mbedtls_ssl_set_hs_psk_opaque(ssl, key);
Neil Armstrong501c9322022-05-03 09:35:09 +02002258#else
Gilles Peskine449bd832023-01-11 14:50:10 +01002259 if ((ssl->handshake->psk = mbedtls_calloc(1, psk_len)) == NULL) {
2260 return MBEDTLS_ERR_SSL_ALLOC_FAILED;
2261 }
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01002262
2263 ssl->handshake->psk_len = psk_len;
Gilles Peskine449bd832023-01-11 14:50:10 +01002264 memcpy(ssl->handshake->psk, psk, ssl->handshake->psk_len);
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01002265
Gilles Peskine449bd832023-01-11 14:50:10 +01002266 return 0;
Neil Armstrong501c9322022-05-03 09:35:09 +02002267#endif /* MBEDTLS_USE_PSA_CRYPTO */
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01002268}
2269
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01002270#if defined(MBEDTLS_USE_PSA_CRYPTO)
Gilles Peskine449bd832023-01-11 14:50:10 +01002271int mbedtls_ssl_conf_psk_opaque(mbedtls_ssl_config *conf,
2272 mbedtls_svc_key_id_t psk,
2273 const unsigned char *psk_identity,
2274 size_t psk_identity_len)
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01002275{
Janos Follath865b3eb2019-12-16 11:46:15 +00002276 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Hanno Becker2ed3dce2021-04-19 21:59:14 +01002277
2278 /* We currently only support one PSK, raw or opaque. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002279 if (mbedtls_ssl_conf_has_static_psk(conf)) {
2280 return MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
2281 }
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01002282
Hanno Becker7390c712018-11-15 13:33:04 +00002283 /* Check and set opaque PSK */
Gilles Peskine449bd832023-01-11 14:50:10 +01002284 if (mbedtls_svc_key_id_is_null(psk)) {
2285 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
2286 }
Ronald Croncf56a0a2020-08-04 09:51:30 +02002287 conf->psk_opaque = psk;
Hanno Becker7390c712018-11-15 13:33:04 +00002288
2289 /* Check and set PSK Identity */
Gilles Peskine449bd832023-01-11 14:50:10 +01002290 ret = ssl_conf_set_psk_identity(conf, psk_identity,
2291 psk_identity_len);
2292 if (ret != 0) {
2293 ssl_conf_remove_psk(conf);
2294 }
Hanno Becker7390c712018-11-15 13:33:04 +00002295
Gilles Peskine449bd832023-01-11 14:50:10 +01002296 return ret;
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01002297}
2298
Gilles Peskine449bd832023-01-11 14:50:10 +01002299int mbedtls_ssl_set_hs_psk_opaque(mbedtls_ssl_context *ssl,
2300 mbedtls_svc_key_id_t psk)
Przemyslaw Stekiel6928a512022-02-03 13:50:35 +01002301{
Gilles Peskine449bd832023-01-11 14:50:10 +01002302 if ((mbedtls_svc_key_id_is_null(psk)) ||
2303 (ssl->handshake == NULL)) {
2304 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
2305 }
Przemyslaw Stekiel6928a512022-02-03 13:50:35 +01002306
Gilles Peskine449bd832023-01-11 14:50:10 +01002307 ssl_remove_psk(ssl);
Przemyslaw Stekiel6928a512022-02-03 13:50:35 +01002308 ssl->handshake->psk_opaque = psk;
Gilles Peskine449bd832023-01-11 14:50:10 +01002309 return 0;
Przemyslaw Stekiel6928a512022-02-03 13:50:35 +01002310}
2311#endif /* MBEDTLS_USE_PSA_CRYPTO */
2312
Jerry Yu8897c072022-08-12 13:56:53 +08002313#if defined(MBEDTLS_SSL_SRV_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01002314void mbedtls_ssl_conf_psk_cb(mbedtls_ssl_config *conf,
2315 int (*f_psk)(void *, mbedtls_ssl_context *, const unsigned char *,
2316 size_t),
2317 void *p_psk)
Przemyslaw Stekiel6928a512022-02-03 13:50:35 +01002318{
2319 conf->f_psk = f_psk;
2320 conf->p_psk = p_psk;
2321}
Jerry Yu8897c072022-08-12 13:56:53 +08002322#endif /* MBEDTLS_SSL_SRV_C */
2323
Ronald Cron73fe8df2022-10-05 14:31:43 +02002324#endif /* MBEDTLS_SSL_HANDSHAKE_WITH_PSK_ENABLED */
Przemyslaw Stekiel6928a512022-02-03 13:50:35 +01002325
2326#if defined(MBEDTLS_USE_PSA_CRYPTO)
Gilles Peskine301711e2022-04-26 16:57:05 +02002327static mbedtls_ssl_mode_t mbedtls_ssl_get_base_mode(
Gilles Peskine449bd832023-01-11 14:50:10 +01002328 psa_algorithm_t alg)
Neil Armstrong8a0f3e82022-03-30 10:57:37 +02002329{
Neil Armstrong8a0f3e82022-03-30 10:57:37 +02002330#if defined(MBEDTLS_SSL_SOME_SUITES_USE_MAC)
Gilles Peskine449bd832023-01-11 14:50:10 +01002331 if (alg == PSA_ALG_CBC_NO_PADDING) {
2332 return MBEDTLS_SSL_MODE_CBC;
2333 }
Neil Armstrong8a0f3e82022-03-30 10:57:37 +02002334#endif /* MBEDTLS_SSL_SOME_SUITES_USE_MAC */
Gilles Peskine449bd832023-01-11 14:50:10 +01002335 if (PSA_ALG_IS_AEAD(alg)) {
2336 return MBEDTLS_SSL_MODE_AEAD;
2337 }
2338 return MBEDTLS_SSL_MODE_STREAM;
Gilles Peskine301711e2022-04-26 16:57:05 +02002339}
2340
2341#else /* MBEDTLS_USE_PSA_CRYPTO */
2342
2343static mbedtls_ssl_mode_t mbedtls_ssl_get_base_mode(
Gilles Peskine449bd832023-01-11 14:50:10 +01002344 mbedtls_cipher_mode_t mode)
Gilles Peskine301711e2022-04-26 16:57:05 +02002345{
2346#if defined(MBEDTLS_SSL_SOME_SUITES_USE_MAC)
Gilles Peskine449bd832023-01-11 14:50:10 +01002347 if (mode == MBEDTLS_MODE_CBC) {
2348 return MBEDTLS_SSL_MODE_CBC;
2349 }
Gilles Peskine301711e2022-04-26 16:57:05 +02002350#endif /* MBEDTLS_SSL_SOME_SUITES_USE_MAC */
2351
Neil Armstrong8a0f3e82022-03-30 10:57:37 +02002352#if defined(MBEDTLS_GCM_C) || \
2353 defined(MBEDTLS_CCM_C) || \
2354 defined(MBEDTLS_CHACHAPOLY_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01002355 if (mode == MBEDTLS_MODE_GCM ||
Neil Armstrong8a0f3e82022-03-30 10:57:37 +02002356 mode == MBEDTLS_MODE_CCM ||
Gilles Peskine449bd832023-01-11 14:50:10 +01002357 mode == MBEDTLS_MODE_CHACHAPOLY) {
2358 return MBEDTLS_SSL_MODE_AEAD;
2359 }
Neil Armstrong8a0f3e82022-03-30 10:57:37 +02002360#endif /* MBEDTLS_GCM_C || MBEDTLS_CCM_C || MBEDTLS_CHACHAPOLY_C */
Neil Armstrong8a0f3e82022-03-30 10:57:37 +02002361
Gilles Peskine449bd832023-01-11 14:50:10 +01002362 return MBEDTLS_SSL_MODE_STREAM;
Neil Armstrong8a0f3e82022-03-30 10:57:37 +02002363}
Gilles Peskine301711e2022-04-26 16:57:05 +02002364#endif /* MBEDTLS_USE_PSA_CRYPTO */
Neil Armstrong8a0f3e82022-03-30 10:57:37 +02002365
Gilles Peskinee108d982022-04-26 16:50:40 +02002366static mbedtls_ssl_mode_t mbedtls_ssl_get_actual_mode(
2367 mbedtls_ssl_mode_t base_mode,
Gilles Peskine449bd832023-01-11 14:50:10 +01002368 int encrypt_then_mac)
Gilles Peskinee108d982022-04-26 16:50:40 +02002369{
2370#if defined(MBEDTLS_SSL_SOME_SUITES_USE_CBC_ETM)
Gilles Peskine449bd832023-01-11 14:50:10 +01002371 if (encrypt_then_mac == MBEDTLS_SSL_ETM_ENABLED &&
2372 base_mode == MBEDTLS_SSL_MODE_CBC) {
2373 return MBEDTLS_SSL_MODE_CBC_ETM;
Gilles Peskinee108d982022-04-26 16:50:40 +02002374 }
2375#else
2376 (void) encrypt_then_mac;
2377#endif
Gilles Peskine449bd832023-01-11 14:50:10 +01002378 return base_mode;
Gilles Peskinee108d982022-04-26 16:50:40 +02002379}
2380
Neil Armstrongab555e02022-04-04 11:07:59 +02002381mbedtls_ssl_mode_t mbedtls_ssl_get_mode_from_transform(
Gilles Peskine449bd832023-01-11 14:50:10 +01002382 const mbedtls_ssl_transform *transform)
Neil Armstrong4bf4c862022-04-01 10:35:48 +02002383{
Gilles Peskinee108d982022-04-26 16:50:40 +02002384 mbedtls_ssl_mode_t base_mode = mbedtls_ssl_get_base_mode(
Neil Armstrong4bf4c862022-04-01 10:35:48 +02002385#if defined(MBEDTLS_USE_PSA_CRYPTO)
Gilles Peskine449bd832023-01-11 14:50:10 +01002386 transform->psa_alg
Neil Armstrong4bf4c862022-04-01 10:35:48 +02002387#else
Gilles Peskine449bd832023-01-11 14:50:10 +01002388 mbedtls_cipher_get_cipher_mode(&transform->cipher_ctx_enc)
Gilles Peskinee108d982022-04-26 16:50:40 +02002389#endif
2390 );
Neil Armstrong4bf4c862022-04-01 10:35:48 +02002391
Gilles Peskinee108d982022-04-26 16:50:40 +02002392 int encrypt_then_mac = 0;
Neil Armstrongf2c82f02022-04-05 11:16:53 +02002393#if defined(MBEDTLS_SSL_SOME_SUITES_USE_CBC_ETM)
Gilles Peskinee108d982022-04-26 16:50:40 +02002394 encrypt_then_mac = transform->encrypt_then_mac;
2395#endif
Gilles Peskine449bd832023-01-11 14:50:10 +01002396 return mbedtls_ssl_get_actual_mode(base_mode, encrypt_then_mac);
Neil Armstrong4bf4c862022-04-01 10:35:48 +02002397}
2398
Neil Armstrongab555e02022-04-04 11:07:59 +02002399mbedtls_ssl_mode_t mbedtls_ssl_get_mode_from_ciphersuite(
Neil Armstrongf2c82f02022-04-05 11:16:53 +02002400#if defined(MBEDTLS_SSL_SOME_SUITES_USE_CBC_ETM)
Gilles Peskine449bd832023-01-11 14:50:10 +01002401 int encrypt_then_mac,
Neil Armstrongf2c82f02022-04-05 11:16:53 +02002402#endif /* MBEDTLS_SSL_SOME_SUITES_USE_CBC_ETM */
Gilles Peskine449bd832023-01-11 14:50:10 +01002403 const mbedtls_ssl_ciphersuite_t *suite)
Neil Armstrong4bf4c862022-04-01 10:35:48 +02002404{
Gilles Peskinee108d982022-04-26 16:50:40 +02002405 mbedtls_ssl_mode_t base_mode = MBEDTLS_SSL_MODE_STREAM;
2406
Neil Armstrong4bf4c862022-04-01 10:35:48 +02002407#if defined(MBEDTLS_USE_PSA_CRYPTO)
2408 psa_status_t status;
2409 psa_algorithm_t alg;
2410 psa_key_type_t type;
2411 size_t size;
Gilles Peskine449bd832023-01-11 14:50:10 +01002412 status = mbedtls_ssl_cipher_to_psa(suite->cipher, 0, &alg, &type, &size);
2413 if (status == PSA_SUCCESS) {
2414 base_mode = mbedtls_ssl_get_base_mode(alg);
2415 }
Neil Armstrong4bf4c862022-04-01 10:35:48 +02002416#else
2417 const mbedtls_cipher_info_t *cipher =
Gilles Peskine449bd832023-01-11 14:50:10 +01002418 mbedtls_cipher_info_from_type(suite->cipher);
2419 if (cipher != NULL) {
Gilles Peskinee108d982022-04-26 16:50:40 +02002420 base_mode =
2421 mbedtls_ssl_get_base_mode(
Gilles Peskine449bd832023-01-11 14:50:10 +01002422 mbedtls_cipher_info_get_mode(cipher));
Gilles Peskinee108d982022-04-26 16:50:40 +02002423 }
Neil Armstrong4bf4c862022-04-01 10:35:48 +02002424#endif /* MBEDTLS_USE_PSA_CRYPTO */
2425
Gilles Peskinee108d982022-04-26 16:50:40 +02002426#if !defined(MBEDTLS_SSL_SOME_SUITES_USE_CBC_ETM)
2427 int encrypt_then_mac = 0;
2428#endif
Gilles Peskine449bd832023-01-11 14:50:10 +01002429 return mbedtls_ssl_get_actual_mode(base_mode, encrypt_then_mac);
Neil Armstrong4bf4c862022-04-01 10:35:48 +02002430}
2431
Neil Armstrong8395d7a2022-05-18 11:44:56 +02002432#if defined(MBEDTLS_USE_PSA_CRYPTO) || defined(MBEDTLS_SSL_PROTO_TLS1_3)
Jerry Yu251a12e2022-07-13 15:15:48 +08002433
2434#if defined(MBEDTLS_SSL_PROTO_TLS1_3)
Jerry Yu438ddd82022-07-07 06:55:50 +00002435/* Serialization of TLS 1.3 sessions:
2436 *
2437 * struct {
Xiaokang Qian126bf8e2022-10-13 02:22:40 +00002438 * opaque hostname<0..2^16-1>;
Jerry Yu438ddd82022-07-07 06:55:50 +00002439 * uint64 ticket_received;
2440 * uint32 ticket_lifetime;
Jerry Yu34191072022-08-18 10:32:09 +08002441 * opaque ticket<1..2^16-1>;
Jerry Yu438ddd82022-07-07 06:55:50 +00002442 * } ClientOnlyData;
2443 *
2444 * struct {
2445 * uint8 endpoint;
2446 * uint8 ciphersuite[2];
2447 * uint32 ticket_age_add;
2448 * uint8 ticket_flags;
2449 * opaque resumption_key<0..255>;
2450 * select ( endpoint ) {
2451 * case client: ClientOnlyData;
2452 * case server: uint64 start_time;
2453 * };
2454 * } serialized_session_tls13;
2455 *
2456 */
2457#if defined(MBEDTLS_SSL_SESSION_TICKETS)
Jerry Yue36fdd62022-08-17 21:31:36 +08002458MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01002459static int ssl_tls13_session_save(const mbedtls_ssl_session *session,
2460 unsigned char *buf,
2461 size_t buf_len,
2462 size_t *olen)
Jerry Yu438ddd82022-07-07 06:55:50 +00002463{
2464 unsigned char *p = buf;
Xiaokang Qiana3b451f2022-10-11 06:20:56 +00002465#if defined(MBEDTLS_SSL_CLI_C) && \
2466 defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
Gilles Peskine449bd832023-01-11 14:50:10 +01002467 size_t hostname_len = (session->hostname == NULL) ?
2468 0 : strlen(session->hostname) + 1;
Xiaokang Qiana3b451f2022-10-11 06:20:56 +00002469#endif
Jerry Yubc7c1a42022-07-21 22:57:37 +08002470 size_t needed = 1 /* endpoint */
2471 + 2 /* ciphersuite */
2472 + 4 /* ticket_age_add */
Jerry Yu34191072022-08-18 10:32:09 +08002473 + 1 /* ticket_flags */
2474 + 1; /* resumption_key length */
Jerry Yue36fdd62022-08-17 21:31:36 +08002475 *olen = 0;
Jerry Yu34191072022-08-18 10:32:09 +08002476
Gilles Peskine449bd832023-01-11 14:50:10 +01002477 if (session->resumption_key_len > MBEDTLS_SSL_TLS1_3_TICKET_RESUMPTION_KEY_LEN) {
2478 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
2479 }
Jerry Yu34191072022-08-18 10:32:09 +08002480 needed += session->resumption_key_len; /* resumption_key */
2481
Jerry Yu438ddd82022-07-07 06:55:50 +00002482#if defined(MBEDTLS_HAVE_TIME)
2483 needed += 8; /* start_time or ticket_received */
2484#endif
2485
2486#if defined(MBEDTLS_SSL_CLI_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01002487 if (session->endpoint == MBEDTLS_SSL_IS_CLIENT) {
Xiaokang Qian126bf8e2022-10-13 02:22:40 +00002488#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
Xiaokang Qianbc663a02022-10-09 11:14:39 +00002489 needed += 2 /* hostname_len */
Gilles Peskine449bd832023-01-11 14:50:10 +01002490 + hostname_len; /* hostname */
Xiaokang Qian281fd1b2022-09-20 11:35:41 +00002491#endif
2492
Jerry Yu438ddd82022-07-07 06:55:50 +00002493 needed += 4 /* ticket_lifetime */
Jerry Yue36fdd62022-08-17 21:31:36 +08002494 + 2; /* ticket_len */
Jerry Yu34191072022-08-18 10:32:09 +08002495
2496 /* Check size_t overflow */
Gilles Peskine449bd832023-01-11 14:50:10 +01002497 if (session->ticket_len > SIZE_MAX - needed) {
2498 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
2499 }
Jerry Yu34191072022-08-18 10:32:09 +08002500
Jerry Yue28d9742022-08-18 15:44:03 +08002501 needed += session->ticket_len; /* ticket */
Jerry Yu438ddd82022-07-07 06:55:50 +00002502 }
2503#endif /* MBEDTLS_SSL_CLI_C */
2504
Jerry Yue36fdd62022-08-17 21:31:36 +08002505 *olen = needed;
Gilles Peskine449bd832023-01-11 14:50:10 +01002506 if (needed > buf_len) {
2507 return MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL;
2508 }
Jerry Yu438ddd82022-07-07 06:55:50 +00002509
2510 p[0] = session->endpoint;
Gilles Peskine449bd832023-01-11 14:50:10 +01002511 MBEDTLS_PUT_UINT16_BE(session->ciphersuite, p, 1);
2512 MBEDTLS_PUT_UINT32_BE(session->ticket_age_add, p, 3);
Jerry Yu438ddd82022-07-07 06:55:50 +00002513 p[7] = session->ticket_flags;
2514
2515 /* save resumption_key */
Jerry Yubc7c1a42022-07-21 22:57:37 +08002516 p[8] = session->resumption_key_len;
Jerry Yu438ddd82022-07-07 06:55:50 +00002517 p += 9;
Gilles Peskine449bd832023-01-11 14:50:10 +01002518 memcpy(p, session->resumption_key, session->resumption_key_len);
Jerry Yubc7c1a42022-07-21 22:57:37 +08002519 p += session->resumption_key_len;
Jerry Yu438ddd82022-07-07 06:55:50 +00002520
2521#if defined(MBEDTLS_HAVE_TIME) && defined(MBEDTLS_SSL_SRV_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01002522 if (session->endpoint == MBEDTLS_SSL_IS_SERVER) {
2523 MBEDTLS_PUT_UINT64_BE((uint64_t) session->start, p, 0);
Jerry Yu438ddd82022-07-07 06:55:50 +00002524 p += 8;
2525 }
2526#endif /* MBEDTLS_HAVE_TIME */
2527
2528#if defined(MBEDTLS_SSL_CLI_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01002529 if (session->endpoint == MBEDTLS_SSL_IS_CLIENT) {
Xiaokang Qianed0620c2022-10-12 06:58:13 +00002530#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
Gilles Peskine449bd832023-01-11 14:50:10 +01002531 MBEDTLS_PUT_UINT16_BE(hostname_len, p, 0);
Xiaokang Qianed0620c2022-10-12 06:58:13 +00002532 p += 2;
Gilles Peskine449bd832023-01-11 14:50:10 +01002533 if (hostname_len > 0) {
Xiaokang Qianed0620c2022-10-12 06:58:13 +00002534 /* save host name */
Gilles Peskine449bd832023-01-11 14:50:10 +01002535 memcpy(p, session->hostname, hostname_len);
Xiaokang Qianed0620c2022-10-12 06:58:13 +00002536 p += hostname_len;
2537 }
2538#endif /* MBEDTLS_SSL_SERVER_NAME_INDICATION */
2539
Jerry Yu438ddd82022-07-07 06:55:50 +00002540#if defined(MBEDTLS_HAVE_TIME)
Gilles Peskine449bd832023-01-11 14:50:10 +01002541 MBEDTLS_PUT_UINT64_BE((uint64_t) session->ticket_received, p, 0);
Jerry Yu438ddd82022-07-07 06:55:50 +00002542 p += 8;
2543#endif
Gilles Peskine449bd832023-01-11 14:50:10 +01002544 MBEDTLS_PUT_UINT32_BE(session->ticket_lifetime, p, 0);
Jerry Yu438ddd82022-07-07 06:55:50 +00002545 p += 4;
2546
Gilles Peskine449bd832023-01-11 14:50:10 +01002547 MBEDTLS_PUT_UINT16_BE(session->ticket_len, p, 0);
Jerry Yu438ddd82022-07-07 06:55:50 +00002548 p += 2;
Jerry Yu34191072022-08-18 10:32:09 +08002549
Gilles Peskine449bd832023-01-11 14:50:10 +01002550 if (session->ticket != NULL && session->ticket_len > 0) {
2551 memcpy(p, session->ticket, session->ticket_len);
Jerry Yu438ddd82022-07-07 06:55:50 +00002552 p += session->ticket_len;
2553 }
2554 }
2555#endif /* MBEDTLS_SSL_CLI_C */
Gilles Peskine449bd832023-01-11 14:50:10 +01002556 return 0;
Jerry Yu438ddd82022-07-07 06:55:50 +00002557}
2558
2559MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01002560static int ssl_tls13_session_load(mbedtls_ssl_session *session,
2561 const unsigned char *buf,
2562 size_t len)
Jerry Yu438ddd82022-07-07 06:55:50 +00002563{
2564 const unsigned char *p = buf;
2565 const unsigned char *end = buf + len;
2566
Gilles Peskine449bd832023-01-11 14:50:10 +01002567 if (end - p < 9) {
2568 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
2569 }
Jerry Yu438ddd82022-07-07 06:55:50 +00002570 session->endpoint = p[0];
Gilles Peskine449bd832023-01-11 14:50:10 +01002571 session->ciphersuite = MBEDTLS_GET_UINT16_BE(p, 1);
2572 session->ticket_age_add = MBEDTLS_GET_UINT32_BE(p, 3);
Jerry Yu438ddd82022-07-07 06:55:50 +00002573 session->ticket_flags = p[7];
2574
2575 /* load resumption_key */
Jerry Yubc7c1a42022-07-21 22:57:37 +08002576 session->resumption_key_len = p[8];
Jerry Yu438ddd82022-07-07 06:55:50 +00002577 p += 9;
2578
Gilles Peskine449bd832023-01-11 14:50:10 +01002579 if (end - p < session->resumption_key_len) {
2580 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
2581 }
Jerry Yu438ddd82022-07-07 06:55:50 +00002582
Gilles Peskine449bd832023-01-11 14:50:10 +01002583 if (sizeof(session->resumption_key) < session->resumption_key_len) {
2584 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
2585 }
2586 memcpy(session->resumption_key, p, session->resumption_key_len);
Jerry Yubc7c1a42022-07-21 22:57:37 +08002587 p += session->resumption_key_len;
Jerry Yu438ddd82022-07-07 06:55:50 +00002588
2589#if defined(MBEDTLS_HAVE_TIME) && defined(MBEDTLS_SSL_SRV_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01002590 if (session->endpoint == MBEDTLS_SSL_IS_SERVER) {
2591 if (end - p < 8) {
2592 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
2593 }
2594 session->start = MBEDTLS_GET_UINT64_BE(p, 0);
Jerry Yu438ddd82022-07-07 06:55:50 +00002595 p += 8;
2596 }
2597#endif /* MBEDTLS_HAVE_TIME */
2598
2599#if defined(MBEDTLS_SSL_CLI_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01002600 if (session->endpoint == MBEDTLS_SSL_IS_CLIENT) {
Xiaokang Qianed0620c2022-10-12 06:58:13 +00002601#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION) && \
Gilles Peskine449bd832023-01-11 14:50:10 +01002602 defined(MBEDTLS_SSL_SESSION_TICKETS)
Xiaokang Qianed0620c2022-10-12 06:58:13 +00002603 size_t hostname_len;
2604 /* load host name */
Gilles Peskine449bd832023-01-11 14:50:10 +01002605 if (end - p < 2) {
2606 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
2607 }
2608 hostname_len = MBEDTLS_GET_UINT16_BE(p, 0);
Xiaokang Qianed0620c2022-10-12 06:58:13 +00002609 p += 2;
2610
Gilles Peskine449bd832023-01-11 14:50:10 +01002611 if (end - p < (long int) hostname_len) {
2612 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
2613 }
2614 if (hostname_len > 0) {
2615 session->hostname = mbedtls_calloc(1, hostname_len);
2616 if (session->hostname == NULL) {
2617 return MBEDTLS_ERR_SSL_ALLOC_FAILED;
2618 }
2619 memcpy(session->hostname, p, hostname_len);
Xiaokang Qianed0620c2022-10-12 06:58:13 +00002620 p += hostname_len;
2621 }
2622#endif /* MBEDTLS_SSL_SERVER_NAME_INDICATION &&
2623 MBEDTLS_SSL_SESSION_TICKETS */
2624
Jerry Yu438ddd82022-07-07 06:55:50 +00002625#if defined(MBEDTLS_HAVE_TIME)
Gilles Peskine449bd832023-01-11 14:50:10 +01002626 if (end - p < 8) {
2627 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
2628 }
2629 session->ticket_received = MBEDTLS_GET_UINT64_BE(p, 0);
Jerry Yu438ddd82022-07-07 06:55:50 +00002630 p += 8;
2631#endif
Gilles Peskine449bd832023-01-11 14:50:10 +01002632 if (end - p < 4) {
2633 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
2634 }
2635 session->ticket_lifetime = MBEDTLS_GET_UINT32_BE(p, 0);
Jerry Yu438ddd82022-07-07 06:55:50 +00002636 p += 4;
2637
Gilles Peskine449bd832023-01-11 14:50:10 +01002638 if (end - p < 2) {
2639 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
2640 }
2641 session->ticket_len = MBEDTLS_GET_UINT16_BE(p, 0);
Jerry Yu438ddd82022-07-07 06:55:50 +00002642 p += 2;
2643
Gilles Peskine449bd832023-01-11 14:50:10 +01002644 if (end - p < (long int) session->ticket_len) {
2645 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
2646 }
2647 if (session->ticket_len > 0) {
2648 session->ticket = mbedtls_calloc(1, session->ticket_len);
2649 if (session->ticket == NULL) {
2650 return MBEDTLS_ERR_SSL_ALLOC_FAILED;
2651 }
2652 memcpy(session->ticket, p, session->ticket_len);
Jerry Yu438ddd82022-07-07 06:55:50 +00002653 p += session->ticket_len;
2654 }
2655 }
2656#endif /* MBEDTLS_SSL_CLI_C */
2657
Gilles Peskine449bd832023-01-11 14:50:10 +01002658 return 0;
Jerry Yu438ddd82022-07-07 06:55:50 +00002659
2660}
2661#else /* MBEDTLS_SSL_SESSION_TICKETS */
Jerry Yue36fdd62022-08-17 21:31:36 +08002662MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01002663static int ssl_tls13_session_save(const mbedtls_ssl_session *session,
2664 unsigned char *buf,
2665 size_t buf_len,
2666 size_t *olen)
Jerry Yu251a12e2022-07-13 15:15:48 +08002667{
2668 ((void) session);
2669 ((void) buf);
2670 ((void) buf_len);
Jerry Yue36fdd62022-08-17 21:31:36 +08002671 *olen = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +01002672 return MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
Jerry Yu251a12e2022-07-13 15:15:48 +08002673}
Jerry Yu438ddd82022-07-07 06:55:50 +00002674
Gilles Peskine449bd832023-01-11 14:50:10 +01002675static int ssl_tls13_session_load(const mbedtls_ssl_session *session,
2676 unsigned char *buf,
2677 size_t buf_len)
Jerry Yu438ddd82022-07-07 06:55:50 +00002678{
2679 ((void) session);
2680 ((void) buf);
2681 ((void) buf_len);
Gilles Peskine449bd832023-01-11 14:50:10 +01002682 return MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
Jerry Yu438ddd82022-07-07 06:55:50 +00002683}
2684#endif /* !MBEDTLS_SSL_SESSION_TICKETS */
Jerry Yu251a12e2022-07-13 15:15:48 +08002685#endif /* MBEDTLS_SSL_PROTO_TLS1_3 */
2686
Gilles Peskine449bd832023-01-11 14:50:10 +01002687psa_status_t mbedtls_ssl_cipher_to_psa(mbedtls_cipher_type_t mbedtls_cipher_type,
2688 size_t taglen,
2689 psa_algorithm_t *alg,
2690 psa_key_type_t *key_type,
2691 size_t *key_size)
Przemyslaw Stekiel430f3372022-01-10 11:55:46 +01002692{
Gilles Peskine449bd832023-01-11 14:50:10 +01002693 switch (mbedtls_cipher_type) {
Przemyslaw Stekiel430f3372022-01-10 11:55:46 +01002694 case MBEDTLS_CIPHER_AES_128_CBC:
2695 *alg = PSA_ALG_CBC_NO_PADDING;
2696 *key_type = PSA_KEY_TYPE_AES;
2697 *key_size = 128;
2698 break;
2699 case MBEDTLS_CIPHER_AES_128_CCM:
Gilles Peskine449bd832023-01-11 14:50:10 +01002700 *alg = taglen ? PSA_ALG_AEAD_WITH_SHORTENED_TAG(PSA_ALG_CCM, taglen) : PSA_ALG_CCM;
Przemyslaw Stekiel430f3372022-01-10 11:55:46 +01002701 *key_type = PSA_KEY_TYPE_AES;
2702 *key_size = 128;
2703 break;
2704 case MBEDTLS_CIPHER_AES_128_GCM:
2705 *alg = PSA_ALG_GCM;
2706 *key_type = PSA_KEY_TYPE_AES;
2707 *key_size = 128;
2708 break;
2709 case MBEDTLS_CIPHER_AES_192_CCM:
Gilles Peskine449bd832023-01-11 14:50:10 +01002710 *alg = taglen ? PSA_ALG_AEAD_WITH_SHORTENED_TAG(PSA_ALG_CCM, taglen) : PSA_ALG_CCM;
Przemyslaw Stekiel430f3372022-01-10 11:55:46 +01002711 *key_type = PSA_KEY_TYPE_AES;
2712 *key_size = 192;
2713 break;
2714 case MBEDTLS_CIPHER_AES_192_GCM:
2715 *alg = PSA_ALG_GCM;
2716 *key_type = PSA_KEY_TYPE_AES;
2717 *key_size = 192;
2718 break;
2719 case MBEDTLS_CIPHER_AES_256_CBC:
2720 *alg = PSA_ALG_CBC_NO_PADDING;
2721 *key_type = PSA_KEY_TYPE_AES;
2722 *key_size = 256;
2723 break;
2724 case MBEDTLS_CIPHER_AES_256_CCM:
Gilles Peskine449bd832023-01-11 14:50:10 +01002725 *alg = taglen ? PSA_ALG_AEAD_WITH_SHORTENED_TAG(PSA_ALG_CCM, taglen) : PSA_ALG_CCM;
Przemyslaw Stekiel430f3372022-01-10 11:55:46 +01002726 *key_type = PSA_KEY_TYPE_AES;
2727 *key_size = 256;
2728 break;
2729 case MBEDTLS_CIPHER_AES_256_GCM:
2730 *alg = PSA_ALG_GCM;
2731 *key_type = PSA_KEY_TYPE_AES;
2732 *key_size = 256;
2733 break;
2734 case MBEDTLS_CIPHER_ARIA_128_CBC:
2735 *alg = PSA_ALG_CBC_NO_PADDING;
2736 *key_type = PSA_KEY_TYPE_ARIA;
2737 *key_size = 128;
2738 break;
2739 case MBEDTLS_CIPHER_ARIA_128_CCM:
Gilles Peskine449bd832023-01-11 14:50:10 +01002740 *alg = taglen ? PSA_ALG_AEAD_WITH_SHORTENED_TAG(PSA_ALG_CCM, taglen) : PSA_ALG_CCM;
Przemyslaw Stekiel430f3372022-01-10 11:55:46 +01002741 *key_type = PSA_KEY_TYPE_ARIA;
2742 *key_size = 128;
2743 break;
2744 case MBEDTLS_CIPHER_ARIA_128_GCM:
2745 *alg = PSA_ALG_GCM;
2746 *key_type = PSA_KEY_TYPE_ARIA;
2747 *key_size = 128;
2748 break;
2749 case MBEDTLS_CIPHER_ARIA_192_CCM:
Gilles Peskine449bd832023-01-11 14:50:10 +01002750 *alg = taglen ? PSA_ALG_AEAD_WITH_SHORTENED_TAG(PSA_ALG_CCM, taglen) : PSA_ALG_CCM;
Przemyslaw Stekiel430f3372022-01-10 11:55:46 +01002751 *key_type = PSA_KEY_TYPE_ARIA;
2752 *key_size = 192;
2753 break;
2754 case MBEDTLS_CIPHER_ARIA_192_GCM:
2755 *alg = PSA_ALG_GCM;
2756 *key_type = PSA_KEY_TYPE_ARIA;
2757 *key_size = 192;
2758 break;
2759 case MBEDTLS_CIPHER_ARIA_256_CBC:
2760 *alg = PSA_ALG_CBC_NO_PADDING;
2761 *key_type = PSA_KEY_TYPE_ARIA;
2762 *key_size = 256;
2763 break;
2764 case MBEDTLS_CIPHER_ARIA_256_CCM:
Gilles Peskine449bd832023-01-11 14:50:10 +01002765 *alg = taglen ? PSA_ALG_AEAD_WITH_SHORTENED_TAG(PSA_ALG_CCM, taglen) : PSA_ALG_CCM;
Przemyslaw Stekiel430f3372022-01-10 11:55:46 +01002766 *key_type = PSA_KEY_TYPE_ARIA;
2767 *key_size = 256;
2768 break;
2769 case MBEDTLS_CIPHER_ARIA_256_GCM:
2770 *alg = PSA_ALG_GCM;
2771 *key_type = PSA_KEY_TYPE_ARIA;
2772 *key_size = 256;
2773 break;
2774 case MBEDTLS_CIPHER_CAMELLIA_128_CBC:
2775 *alg = PSA_ALG_CBC_NO_PADDING;
2776 *key_type = PSA_KEY_TYPE_CAMELLIA;
2777 *key_size = 128;
2778 break;
2779 case MBEDTLS_CIPHER_CAMELLIA_128_CCM:
Gilles Peskine449bd832023-01-11 14:50:10 +01002780 *alg = taglen ? PSA_ALG_AEAD_WITH_SHORTENED_TAG(PSA_ALG_CCM, taglen) : PSA_ALG_CCM;
Przemyslaw Stekiel430f3372022-01-10 11:55:46 +01002781 *key_type = PSA_KEY_TYPE_CAMELLIA;
2782 *key_size = 128;
2783 break;
2784 case MBEDTLS_CIPHER_CAMELLIA_128_GCM:
2785 *alg = PSA_ALG_GCM;
2786 *key_type = PSA_KEY_TYPE_CAMELLIA;
2787 *key_size = 128;
2788 break;
2789 case MBEDTLS_CIPHER_CAMELLIA_192_CCM:
Gilles Peskine449bd832023-01-11 14:50:10 +01002790 *alg = taglen ? PSA_ALG_AEAD_WITH_SHORTENED_TAG(PSA_ALG_CCM, taglen) : PSA_ALG_CCM;
Przemyslaw Stekiel430f3372022-01-10 11:55:46 +01002791 *key_type = PSA_KEY_TYPE_CAMELLIA;
2792 *key_size = 192;
2793 break;
2794 case MBEDTLS_CIPHER_CAMELLIA_192_GCM:
2795 *alg = PSA_ALG_GCM;
2796 *key_type = PSA_KEY_TYPE_CAMELLIA;
2797 *key_size = 192;
2798 break;
2799 case MBEDTLS_CIPHER_CAMELLIA_256_CBC:
2800 *alg = PSA_ALG_CBC_NO_PADDING;
2801 *key_type = PSA_KEY_TYPE_CAMELLIA;
2802 *key_size = 256;
2803 break;
2804 case MBEDTLS_CIPHER_CAMELLIA_256_CCM:
Gilles Peskine449bd832023-01-11 14:50:10 +01002805 *alg = taglen ? PSA_ALG_AEAD_WITH_SHORTENED_TAG(PSA_ALG_CCM, taglen) : PSA_ALG_CCM;
Przemyslaw Stekiel430f3372022-01-10 11:55:46 +01002806 *key_type = PSA_KEY_TYPE_CAMELLIA;
2807 *key_size = 256;
2808 break;
2809 case MBEDTLS_CIPHER_CAMELLIA_256_GCM:
2810 *alg = PSA_ALG_GCM;
2811 *key_type = PSA_KEY_TYPE_CAMELLIA;
2812 *key_size = 256;
2813 break;
2814 case MBEDTLS_CIPHER_CHACHA20_POLY1305:
2815 *alg = PSA_ALG_CHACHA20_POLY1305;
2816 *key_type = PSA_KEY_TYPE_CHACHA20;
2817 *key_size = 256;
2818 break;
2819 case MBEDTLS_CIPHER_NULL:
2820 *alg = MBEDTLS_SSL_NULL_CIPHER;
2821 *key_type = 0;
2822 *key_size = 0;
2823 break;
2824 default:
2825 return PSA_ERROR_NOT_SUPPORTED;
2826 }
2827
2828 return PSA_SUCCESS;
2829}
Neil Armstrong8395d7a2022-05-18 11:44:56 +02002830#endif /* MBEDTLS_USE_PSA_CRYPTO || MBEDTLS_SSL_PROTO_TLS1_3 */
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01002831
Manuel Pégourié-Gonnardcf141ca2015-05-20 10:35:51 +02002832#if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_SSL_SRV_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01002833int mbedtls_ssl_conf_dh_param_bin(mbedtls_ssl_config *conf,
2834 const unsigned char *dhm_P, size_t P_len,
2835 const unsigned char *dhm_G, size_t G_len)
Hanno Beckera90658f2017-10-04 15:29:08 +01002836{
Janos Follath865b3eb2019-12-16 11:46:15 +00002837 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Hanno Beckera90658f2017-10-04 15:29:08 +01002838
Gilles Peskine449bd832023-01-11 14:50:10 +01002839 mbedtls_mpi_free(&conf->dhm_P);
2840 mbedtls_mpi_free(&conf->dhm_G);
Glenn Strausscee11292021-12-20 01:43:17 -05002841
Gilles Peskine449bd832023-01-11 14:50:10 +01002842 if ((ret = mbedtls_mpi_read_binary(&conf->dhm_P, dhm_P, P_len)) != 0 ||
2843 (ret = mbedtls_mpi_read_binary(&conf->dhm_G, dhm_G, G_len)) != 0) {
2844 mbedtls_mpi_free(&conf->dhm_P);
2845 mbedtls_mpi_free(&conf->dhm_G);
2846 return ret;
Hanno Beckera90658f2017-10-04 15:29:08 +01002847 }
2848
Gilles Peskine449bd832023-01-11 14:50:10 +01002849 return 0;
Hanno Beckera90658f2017-10-04 15:29:08 +01002850}
Paul Bakker5121ce52009-01-03 21:22:43 +00002851
Gilles Peskine449bd832023-01-11 14:50:10 +01002852int mbedtls_ssl_conf_dh_param_ctx(mbedtls_ssl_config *conf, mbedtls_dhm_context *dhm_ctx)
Paul Bakker1b57b062011-01-06 15:48:19 +00002853{
Janos Follath865b3eb2019-12-16 11:46:15 +00002854 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker1b57b062011-01-06 15:48:19 +00002855
Gilles Peskine449bd832023-01-11 14:50:10 +01002856 mbedtls_mpi_free(&conf->dhm_P);
2857 mbedtls_mpi_free(&conf->dhm_G);
Glenn Strausscee11292021-12-20 01:43:17 -05002858
Gilles Peskine449bd832023-01-11 14:50:10 +01002859 if ((ret = mbedtls_dhm_get_value(dhm_ctx, MBEDTLS_DHM_PARAM_P,
2860 &conf->dhm_P)) != 0 ||
2861 (ret = mbedtls_dhm_get_value(dhm_ctx, MBEDTLS_DHM_PARAM_G,
2862 &conf->dhm_G)) != 0) {
2863 mbedtls_mpi_free(&conf->dhm_P);
2864 mbedtls_mpi_free(&conf->dhm_G);
2865 return ret;
Manuel Pégourié-Gonnard1028b742015-05-06 17:33:07 +01002866 }
Paul Bakker1b57b062011-01-06 15:48:19 +00002867
Gilles Peskine449bd832023-01-11 14:50:10 +01002868 return 0;
Paul Bakker1b57b062011-01-06 15:48:19 +00002869}
Manuel Pégourié-Gonnardcf141ca2015-05-20 10:35:51 +02002870#endif /* MBEDTLS_DHM_C && MBEDTLS_SSL_SRV_C */
Paul Bakker1b57b062011-01-06 15:48:19 +00002871
Manuel Pégourié-Gonnardbd990d62015-06-11 14:49:42 +02002872#if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_SSL_CLI_C)
2873/*
2874 * Set the minimum length for Diffie-Hellman parameters
2875 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002876void mbedtls_ssl_conf_dhm_min_bitlen(mbedtls_ssl_config *conf,
2877 unsigned int bitlen)
Manuel Pégourié-Gonnardbd990d62015-06-11 14:49:42 +02002878{
2879 conf->dhm_min_bitlen = bitlen;
2880}
2881#endif /* MBEDTLS_DHM_C && MBEDTLS_SSL_CLI_C */
2882
Ronald Crone68ab4f2022-10-05 12:46:29 +02002883#if defined(MBEDTLS_SSL_HANDSHAKE_WITH_CERT_ENABLED)
Jerry Yu7ddc38c2022-01-19 11:08:05 +08002884#if !defined(MBEDTLS_DEPRECATED_REMOVED) && defined(MBEDTLS_SSL_PROTO_TLS1_2)
Manuel Pégourié-Gonnard36a8b572015-06-17 12:43:26 +02002885/*
2886 * Set allowed/preferred hashes for handshake signatures
2887 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002888void mbedtls_ssl_conf_sig_hashes(mbedtls_ssl_config *conf,
2889 const int *hashes)
Manuel Pégourié-Gonnard36a8b572015-06-17 12:43:26 +02002890{
2891 conf->sig_hashes = hashes;
2892}
Jerry Yu7ddc38c2022-01-19 11:08:05 +08002893#endif /* !MBEDTLS_DEPRECATED_REMOVED && MBEDTLS_SSL_PROTO_TLS1_2 */
Hanno Becker1cd6e002021-08-10 13:27:10 +01002894
Jerry Yuf017ee42022-01-12 15:49:48 +08002895/* Configure allowed signature algorithms for handshake */
Gilles Peskine449bd832023-01-11 14:50:10 +01002896void mbedtls_ssl_conf_sig_algs(mbedtls_ssl_config *conf,
2897 const uint16_t *sig_algs)
Hanno Becker1cd6e002021-08-10 13:27:10 +01002898{
Jerry Yuf017ee42022-01-12 15:49:48 +08002899#if !defined(MBEDTLS_DEPRECATED_REMOVED)
2900 conf->sig_hashes = NULL;
2901#endif /* !MBEDTLS_DEPRECATED_REMOVED */
2902 conf->sig_algs = sig_algs;
Hanno Becker1cd6e002021-08-10 13:27:10 +01002903}
Ronald Crone68ab4f2022-10-05 12:46:29 +02002904#endif /* MBEDTLS_SSL_HANDSHAKE_WITH_CERT_ENABLED */
Manuel Pégourié-Gonnard36a8b572015-06-17 12:43:26 +02002905
Manuel Pégourié-Gonnardb541da62015-06-17 11:43:30 +02002906#if defined(MBEDTLS_ECP_C)
Brett Warrene0edc842021-08-17 09:53:13 +01002907#if !defined(MBEDTLS_DEPRECATED_REMOVED)
Manuel Pégourié-Gonnard7f38ed02014-02-04 15:52:33 +01002908/*
2909 * Set the allowed elliptic curves
Brett Warrene0edc842021-08-17 09:53:13 +01002910 *
2911 * mbedtls_ssl_setup() takes the provided list
2912 * and translates it to a list of IANA TLS group identifiers,
2913 * stored in ssl->handshake->group_list.
2914 *
Manuel Pégourié-Gonnard7f38ed02014-02-04 15:52:33 +01002915 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002916void mbedtls_ssl_conf_curves(mbedtls_ssl_config *conf,
2917 const mbedtls_ecp_group_id *curve_list)
Manuel Pégourié-Gonnard7f38ed02014-02-04 15:52:33 +01002918{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02002919 conf->curve_list = curve_list;
Brett Warrene0edc842021-08-17 09:53:13 +01002920 conf->group_list = NULL;
Manuel Pégourié-Gonnard7f38ed02014-02-04 15:52:33 +01002921}
Brett Warrene0edc842021-08-17 09:53:13 +01002922#endif /* MBEDTLS_DEPRECATED_REMOVED */
Hanno Becker947194e2017-04-07 13:25:49 +01002923#endif /* MBEDTLS_ECP_C */
Manuel Pégourié-Gonnard7f38ed02014-02-04 15:52:33 +01002924
Brett Warrene0edc842021-08-17 09:53:13 +01002925/*
2926 * Set the allowed groups
2927 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002928void mbedtls_ssl_conf_groups(mbedtls_ssl_config *conf,
2929 const uint16_t *group_list)
Brett Warrene0edc842021-08-17 09:53:13 +01002930{
2931#if defined(MBEDTLS_ECP_C) && !defined(MBEDTLS_DEPRECATED_REMOVED)
2932 conf->curve_list = NULL;
2933#endif
2934 conf->group_list = group_list;
2935}
2936
Manuel Pégourié-Gonnardbc2b7712015-05-06 11:14:19 +01002937#if defined(MBEDTLS_X509_CRT_PARSE_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01002938int mbedtls_ssl_set_hostname(mbedtls_ssl_context *ssl, const char *hostname)
Paul Bakker5121ce52009-01-03 21:22:43 +00002939{
Hanno Becker947194e2017-04-07 13:25:49 +01002940 /* Initialize to suppress unnecessary compiler warning */
2941 size_t hostname_len = 0;
2942
2943 /* Check if new hostname is valid before
2944 * making any change to current one */
Gilles Peskine449bd832023-01-11 14:50:10 +01002945 if (hostname != NULL) {
2946 hostname_len = strlen(hostname);
Hanno Becker947194e2017-04-07 13:25:49 +01002947
Gilles Peskine449bd832023-01-11 14:50:10 +01002948 if (hostname_len > MBEDTLS_SSL_MAX_HOST_NAME_LEN) {
2949 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
2950 }
Hanno Becker947194e2017-04-07 13:25:49 +01002951 }
2952
2953 /* Now it's clear that we will overwrite the old hostname,
2954 * so we can free it safely */
2955
Gilles Peskine449bd832023-01-11 14:50:10 +01002956 if (ssl->hostname != NULL) {
2957 mbedtls_platform_zeroize(ssl->hostname, strlen(ssl->hostname));
2958 mbedtls_free(ssl->hostname);
Hanno Becker947194e2017-04-07 13:25:49 +01002959 }
2960
2961 /* Passing NULL as hostname shall clear the old one */
Manuel Pégourié-Gonnardba26c242015-05-06 10:47:06 +01002962
Gilles Peskine449bd832023-01-11 14:50:10 +01002963 if (hostname == NULL) {
Hanno Becker947194e2017-04-07 13:25:49 +01002964 ssl->hostname = NULL;
Gilles Peskine449bd832023-01-11 14:50:10 +01002965 } else {
2966 ssl->hostname = mbedtls_calloc(1, hostname_len + 1);
2967 if (ssl->hostname == NULL) {
2968 return MBEDTLS_ERR_SSL_ALLOC_FAILED;
2969 }
Paul Bakker75c1a6f2013-08-19 14:25:29 +02002970
Gilles Peskine449bd832023-01-11 14:50:10 +01002971 memcpy(ssl->hostname, hostname, hostname_len);
Paul Bakker75c1a6f2013-08-19 14:25:29 +02002972
Hanno Becker947194e2017-04-07 13:25:49 +01002973 ssl->hostname[hostname_len] = '\0';
2974 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002975
Gilles Peskine449bd832023-01-11 14:50:10 +01002976 return 0;
Paul Bakker5121ce52009-01-03 21:22:43 +00002977}
Hanno Becker1a9a51c2017-04-07 13:02:16 +01002978#endif /* MBEDTLS_X509_CRT_PARSE_C */
Paul Bakker5121ce52009-01-03 21:22:43 +00002979
Manuel Pégourié-Gonnardbc2b7712015-05-06 11:14:19 +01002980#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
Gilles Peskine449bd832023-01-11 14:50:10 +01002981void mbedtls_ssl_conf_sni(mbedtls_ssl_config *conf,
2982 int (*f_sni)(void *, mbedtls_ssl_context *,
2983 const unsigned char *, size_t),
2984 void *p_sni)
Paul Bakker5701cdc2012-09-27 21:49:42 +00002985{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02002986 conf->f_sni = f_sni;
2987 conf->p_sni = p_sni;
Paul Bakker5701cdc2012-09-27 21:49:42 +00002988}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002989#endif /* MBEDTLS_SSL_SERVER_NAME_INDICATION */
Paul Bakker5701cdc2012-09-27 21:49:42 +00002990
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002991#if defined(MBEDTLS_SSL_ALPN)
Gilles Peskine449bd832023-01-11 14:50:10 +01002992int mbedtls_ssl_conf_alpn_protocols(mbedtls_ssl_config *conf, const char **protos)
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02002993{
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +02002994 size_t cur_len, tot_len;
2995 const char **p;
2996
2997 /*
Brian J Murray1903fb32016-11-06 04:45:15 -08002998 * RFC 7301 3.1: "Empty strings MUST NOT be included and byte strings
2999 * MUST NOT be truncated."
3000 * We check lengths now rather than later.
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +02003001 */
3002 tot_len = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +01003003 for (p = protos; *p != NULL; p++) {
3004 cur_len = strlen(*p);
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +02003005 tot_len += cur_len;
3006
Gilles Peskine449bd832023-01-11 14:50:10 +01003007 if ((cur_len == 0) ||
3008 (cur_len > MBEDTLS_SSL_MAX_ALPN_NAME_LEN) ||
3009 (tot_len > MBEDTLS_SSL_MAX_ALPN_LIST_LEN)) {
3010 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
3011 }
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +02003012 }
3013
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02003014 conf->alpn_list = protos;
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +02003015
Gilles Peskine449bd832023-01-11 14:50:10 +01003016 return 0;
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02003017}
3018
Gilles Peskine449bd832023-01-11 14:50:10 +01003019const char *mbedtls_ssl_get_alpn_protocol(const mbedtls_ssl_context *ssl)
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02003020{
Gilles Peskine449bd832023-01-11 14:50:10 +01003021 return ssl->alpn_chosen;
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02003022}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003023#endif /* MBEDTLS_SSL_ALPN */
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02003024
Johan Pascalb62bb512015-12-03 21:56:45 +01003025#if defined(MBEDTLS_SSL_DTLS_SRTP)
Gilles Peskine449bd832023-01-11 14:50:10 +01003026void mbedtls_ssl_conf_srtp_mki_value_supported(mbedtls_ssl_config *conf,
3027 int support_mki_value)
Ron Eldor591f1622018-01-22 12:30:04 +02003028{
3029 conf->dtls_srtp_mki_support = support_mki_value;
3030}
3031
Gilles Peskine449bd832023-01-11 14:50:10 +01003032int mbedtls_ssl_dtls_srtp_set_mki_value(mbedtls_ssl_context *ssl,
3033 unsigned char *mki_value,
3034 uint16_t mki_len)
Ron Eldor591f1622018-01-22 12:30:04 +02003035{
Gilles Peskine449bd832023-01-11 14:50:10 +01003036 if (mki_len > MBEDTLS_TLS_SRTP_MAX_MKI_LENGTH) {
3037 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
Ron Eldora9788042018-12-05 11:04:31 +02003038 }
Ron Eldor591f1622018-01-22 12:30:04 +02003039
Gilles Peskine449bd832023-01-11 14:50:10 +01003040 if (ssl->conf->dtls_srtp_mki_support == MBEDTLS_SSL_DTLS_SRTP_MKI_UNSUPPORTED) {
3041 return MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
Ron Eldora9788042018-12-05 11:04:31 +02003042 }
Ron Eldor591f1622018-01-22 12:30:04 +02003043
Gilles Peskine449bd832023-01-11 14:50:10 +01003044 memcpy(ssl->dtls_srtp_info.mki_value, mki_value, mki_len);
Ron Eldor591f1622018-01-22 12:30:04 +02003045 ssl->dtls_srtp_info.mki_len = mki_len;
Gilles Peskine449bd832023-01-11 14:50:10 +01003046 return 0;
Ron Eldor591f1622018-01-22 12:30:04 +02003047}
3048
Gilles Peskine449bd832023-01-11 14:50:10 +01003049int mbedtls_ssl_conf_dtls_srtp_protection_profiles(mbedtls_ssl_config *conf,
3050 const mbedtls_ssl_srtp_profile *profiles)
Johan Pascalb62bb512015-12-03 21:56:45 +01003051{
Johan Pascal253d0262020-09-22 13:04:45 +02003052 const mbedtls_ssl_srtp_profile *p;
3053 size_t list_size = 0;
Johan Pascalb62bb512015-12-03 21:56:45 +01003054
Johan Pascal253d0262020-09-22 13:04:45 +02003055 /* check the profiles list: all entry must be valid,
3056 * its size cannot be more than the total number of supported profiles, currently 4 */
Gilles Peskine449bd832023-01-11 14:50:10 +01003057 for (p = profiles; *p != MBEDTLS_TLS_SRTP_UNSET &&
3058 list_size <= MBEDTLS_TLS_SRTP_MAX_PROFILE_LIST_LENGTH;
3059 p++) {
3060 if (mbedtls_ssl_check_srtp_profile_value(*p) != MBEDTLS_TLS_SRTP_UNSET) {
Johan Pascal76fdf1d2020-10-22 23:31:00 +02003061 list_size++;
Gilles Peskine449bd832023-01-11 14:50:10 +01003062 } else {
Johan Pascal76fdf1d2020-10-22 23:31:00 +02003063 /* unsupported value, stop parsing and set the size to an error value */
3064 list_size = MBEDTLS_TLS_SRTP_MAX_PROFILE_LIST_LENGTH + 1;
Johan Pascalb62bb512015-12-03 21:56:45 +01003065 }
3066 }
3067
Gilles Peskine449bd832023-01-11 14:50:10 +01003068 if (list_size > MBEDTLS_TLS_SRTP_MAX_PROFILE_LIST_LENGTH) {
3069 conf->dtls_srtp_profile_list = NULL;
3070 conf->dtls_srtp_profile_list_len = 0;
3071 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
Johan Pascal253d0262020-09-22 13:04:45 +02003072 }
3073
Johan Pascal9bc97ca2020-09-21 23:44:45 +02003074 conf->dtls_srtp_profile_list = profiles;
Johan Pascal253d0262020-09-22 13:04:45 +02003075 conf->dtls_srtp_profile_list_len = list_size;
Johan Pascalb62bb512015-12-03 21:56:45 +01003076
Gilles Peskine449bd832023-01-11 14:50:10 +01003077 return 0;
Johan Pascalb62bb512015-12-03 21:56:45 +01003078}
3079
Gilles Peskine449bd832023-01-11 14:50:10 +01003080void mbedtls_ssl_get_dtls_srtp_negotiation_result(const mbedtls_ssl_context *ssl,
3081 mbedtls_dtls_srtp_info *dtls_srtp_info)
Johan Pascalb62bb512015-12-03 21:56:45 +01003082{
Johan Pascal2258a4f2020-10-28 13:53:09 +01003083 dtls_srtp_info->chosen_dtls_srtp_profile = ssl->dtls_srtp_info.chosen_dtls_srtp_profile;
3084 /* do not copy the mki value if there is no chosen profile */
Gilles Peskine449bd832023-01-11 14:50:10 +01003085 if (dtls_srtp_info->chosen_dtls_srtp_profile == MBEDTLS_TLS_SRTP_UNSET) {
Johan Pascal2258a4f2020-10-28 13:53:09 +01003086 dtls_srtp_info->mki_len = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +01003087 } else {
Johan Pascal2258a4f2020-10-28 13:53:09 +01003088 dtls_srtp_info->mki_len = ssl->dtls_srtp_info.mki_len;
Gilles Peskine449bd832023-01-11 14:50:10 +01003089 memcpy(dtls_srtp_info->mki_value, ssl->dtls_srtp_info.mki_value,
3090 ssl->dtls_srtp_info.mki_len);
Johan Pascal2258a4f2020-10-28 13:53:09 +01003091 }
Johan Pascalb62bb512015-12-03 21:56:45 +01003092}
Johan Pascalb62bb512015-12-03 21:56:45 +01003093#endif /* MBEDTLS_SSL_DTLS_SRTP */
3094
Aditya Patwardhan3096f332022-07-26 14:31:46 +05303095#if !defined(MBEDTLS_DEPRECATED_REMOVED)
Gilles Peskine449bd832023-01-11 14:50:10 +01003096void mbedtls_ssl_conf_max_version(mbedtls_ssl_config *conf, int major, int minor)
Paul Bakker490ecc82011-10-06 13:04:09 +00003097{
Glenn Strauss2dfcea22022-03-14 17:26:42 -04003098 conf->max_tls_version = (major << 8) | minor;
Paul Bakker490ecc82011-10-06 13:04:09 +00003099}
3100
Gilles Peskine449bd832023-01-11 14:50:10 +01003101void mbedtls_ssl_conf_min_version(mbedtls_ssl_config *conf, int major, int minor)
Paul Bakker1d29fb52012-09-28 13:28:45 +00003102{
Glenn Strauss2dfcea22022-03-14 17:26:42 -04003103 conf->min_tls_version = (major << 8) | minor;
Paul Bakker1d29fb52012-09-28 13:28:45 +00003104}
Aditya Patwardhan3096f332022-07-26 14:31:46 +05303105#endif /* MBEDTLS_DEPRECATED_REMOVED */
Paul Bakker1d29fb52012-09-28 13:28:45 +00003106
Janos Follath088ce432017-04-10 12:42:31 +01003107#if defined(MBEDTLS_SSL_SRV_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01003108void mbedtls_ssl_conf_cert_req_ca_list(mbedtls_ssl_config *conf,
3109 char cert_req_ca_list)
Janos Follath088ce432017-04-10 12:42:31 +01003110{
3111 conf->cert_req_ca_list = cert_req_ca_list;
3112}
3113#endif
3114
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003115#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
Gilles Peskine449bd832023-01-11 14:50:10 +01003116void mbedtls_ssl_conf_encrypt_then_mac(mbedtls_ssl_config *conf, char etm)
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +01003117{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02003118 conf->encrypt_then_mac = etm;
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +01003119}
3120#endif
3121
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003122#if defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET)
Gilles Peskine449bd832023-01-11 14:50:10 +01003123void mbedtls_ssl_conf_extended_master_secret(mbedtls_ssl_config *conf, char ems)
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +02003124{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02003125 conf->extended_ms = ems;
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +02003126}
3127#endif
3128
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003129#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
Gilles Peskine449bd832023-01-11 14:50:10 +01003130int mbedtls_ssl_conf_max_frag_len(mbedtls_ssl_config *conf, unsigned char mfl_code)
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02003131{
Gilles Peskine449bd832023-01-11 14:50:10 +01003132 if (mfl_code >= MBEDTLS_SSL_MAX_FRAG_LEN_INVALID ||
3133 ssl_mfl_code_to_length(mfl_code) > MBEDTLS_TLS_EXT_ADV_CONTENT_LEN) {
3134 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02003135 }
3136
Manuel Pégourié-Gonnard6bf89d62015-05-05 17:01:57 +01003137 conf->mfl_code = mfl_code;
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02003138
Gilles Peskine449bd832023-01-11 14:50:10 +01003139 return 0;
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02003140}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003141#endif /* MBEDTLS_SSL_MAX_FRAGMENT_LENGTH */
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02003142
Gilles Peskine449bd832023-01-11 14:50:10 +01003143void mbedtls_ssl_conf_legacy_renegotiation(mbedtls_ssl_config *conf, int allow_legacy)
Paul Bakker48916f92012-09-16 19:57:18 +00003144{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02003145 conf->allow_legacy_renegotiation = allow_legacy;
Paul Bakker48916f92012-09-16 19:57:18 +00003146}
3147
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003148#if defined(MBEDTLS_SSL_RENEGOTIATION)
Gilles Peskine449bd832023-01-11 14:50:10 +01003149void mbedtls_ssl_conf_renegotiation(mbedtls_ssl_config *conf, int renegotiation)
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01003150{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02003151 conf->disable_renegotiation = renegotiation;
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01003152}
3153
Gilles Peskine449bd832023-01-11 14:50:10 +01003154void mbedtls_ssl_conf_renegotiation_enforced(mbedtls_ssl_config *conf, int max_records)
Manuel Pégourié-Gonnarda9964db2014-07-03 19:29:16 +02003155{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02003156 conf->renego_max_records = max_records;
Manuel Pégourié-Gonnarda9964db2014-07-03 19:29:16 +02003157}
3158
Gilles Peskine449bd832023-01-11 14:50:10 +01003159void mbedtls_ssl_conf_renegotiation_period(mbedtls_ssl_config *conf,
3160 const unsigned char period[8])
Manuel Pégourié-Gonnard837f0fe2014-11-05 13:58:53 +01003161{
Gilles Peskine449bd832023-01-11 14:50:10 +01003162 memcpy(conf->renego_period, period, 8);
Manuel Pégourié-Gonnard837f0fe2014-11-05 13:58:53 +01003163}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003164#endif /* MBEDTLS_SSL_RENEGOTIATION */
Paul Bakker5121ce52009-01-03 21:22:43 +00003165
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003166#if defined(MBEDTLS_SSL_SESSION_TICKETS)
Manuel Pégourié-Gonnardb596abf2015-05-20 10:45:29 +02003167#if defined(MBEDTLS_SSL_CLI_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01003168void mbedtls_ssl_conf_session_tickets(mbedtls_ssl_config *conf, int use_tickets)
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +02003169{
Manuel Pégourié-Gonnard2b494452015-05-06 10:05:11 +01003170 conf->session_tickets = use_tickets;
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +02003171}
Manuel Pégourié-Gonnardb596abf2015-05-20 10:45:29 +02003172#endif
Paul Bakker606b4ba2013-08-14 16:52:14 +02003173
Manuel Pégourié-Gonnardb596abf2015-05-20 10:45:29 +02003174#if defined(MBEDTLS_SSL_SRV_C)
Jerry Yu1ad7ace2022-08-09 13:28:39 +08003175
Jerry Yud0766ec2022-09-22 10:46:57 +08003176#if defined(MBEDTLS_SSL_PROTO_TLS1_3) && defined(MBEDTLS_SSL_SESSION_TICKETS)
Gilles Peskine449bd832023-01-11 14:50:10 +01003177void mbedtls_ssl_conf_new_session_tickets(mbedtls_ssl_config *conf,
3178 uint16_t num_tickets)
Jerry Yu1ad7ace2022-08-09 13:28:39 +08003179{
Jerry Yud0766ec2022-09-22 10:46:57 +08003180 conf->new_session_tickets_count = num_tickets;
Jerry Yu1ad7ace2022-08-09 13:28:39 +08003181}
3182#endif
3183
Gilles Peskine449bd832023-01-11 14:50:10 +01003184void mbedtls_ssl_conf_session_tickets_cb(mbedtls_ssl_config *conf,
3185 mbedtls_ssl_ticket_write_t *f_ticket_write,
3186 mbedtls_ssl_ticket_parse_t *f_ticket_parse,
3187 void *p_ticket)
Paul Bakker606b4ba2013-08-14 16:52:14 +02003188{
Manuel Pégourié-Gonnardd59675d2015-05-19 15:28:00 +02003189 conf->f_ticket_write = f_ticket_write;
3190 conf->f_ticket_parse = f_ticket_parse;
3191 conf->p_ticket = p_ticket;
Paul Bakker606b4ba2013-08-14 16:52:14 +02003192}
Manuel Pégourié-Gonnardb596abf2015-05-20 10:45:29 +02003193#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003194#endif /* MBEDTLS_SSL_SESSION_TICKETS */
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +02003195
Gilles Peskine449bd832023-01-11 14:50:10 +01003196void mbedtls_ssl_set_export_keys_cb(mbedtls_ssl_context *ssl,
3197 mbedtls_ssl_export_keys_t *f_export_keys,
3198 void *p_export_keys)
Robert Cragie4feb7ae2015-10-02 13:33:37 +01003199{
Hanno Becker7e6c1782021-06-08 09:24:55 +01003200 ssl->f_export_keys = f_export_keys;
3201 ssl->p_export_keys = p_export_keys;
Ron Eldorf5cc10d2019-05-07 18:33:40 +03003202}
Robert Cragie4feb7ae2015-10-02 13:33:37 +01003203
Gilles Peskineb74a1c72018-04-24 13:09:22 +02003204#if defined(MBEDTLS_SSL_ASYNC_PRIVATE)
Gilles Peskine8bf79f62018-01-05 21:11:53 +01003205void mbedtls_ssl_conf_async_private_cb(
3206 mbedtls_ssl_config *conf,
3207 mbedtls_ssl_async_sign_t *f_async_sign,
3208 mbedtls_ssl_async_decrypt_t *f_async_decrypt,
3209 mbedtls_ssl_async_resume_t *f_async_resume,
3210 mbedtls_ssl_async_cancel_t *f_async_cancel,
Gilles Peskine449bd832023-01-11 14:50:10 +01003211 void *async_config_data)
Gilles Peskine8bf79f62018-01-05 21:11:53 +01003212{
3213 conf->f_async_sign_start = f_async_sign;
3214 conf->f_async_decrypt_start = f_async_decrypt;
3215 conf->f_async_resume = f_async_resume;
3216 conf->f_async_cancel = f_async_cancel;
Gilles Peskinedf13d5c2018-04-25 20:39:48 +02003217 conf->p_async_config_data = async_config_data;
3218}
3219
Gilles Peskine449bd832023-01-11 14:50:10 +01003220void *mbedtls_ssl_conf_get_async_config_data(const mbedtls_ssl_config *conf)
Gilles Peskine8f97af72018-04-26 11:46:10 +02003221{
Gilles Peskine449bd832023-01-11 14:50:10 +01003222 return conf->p_async_config_data;
Gilles Peskine8f97af72018-04-26 11:46:10 +02003223}
3224
Gilles Peskine449bd832023-01-11 14:50:10 +01003225void *mbedtls_ssl_get_async_operation_data(const mbedtls_ssl_context *ssl)
Gilles Peskinedf13d5c2018-04-25 20:39:48 +02003226{
Gilles Peskine449bd832023-01-11 14:50:10 +01003227 if (ssl->handshake == NULL) {
3228 return NULL;
3229 } else {
3230 return ssl->handshake->user_async_ctx;
3231 }
Gilles Peskinedf13d5c2018-04-25 20:39:48 +02003232}
3233
Gilles Peskine449bd832023-01-11 14:50:10 +01003234void mbedtls_ssl_set_async_operation_data(mbedtls_ssl_context *ssl,
3235 void *ctx)
Gilles Peskinedf13d5c2018-04-25 20:39:48 +02003236{
Gilles Peskine449bd832023-01-11 14:50:10 +01003237 if (ssl->handshake != NULL) {
Gilles Peskinedf13d5c2018-04-25 20:39:48 +02003238 ssl->handshake->user_async_ctx = ctx;
Gilles Peskine449bd832023-01-11 14:50:10 +01003239 }
Gilles Peskine8bf79f62018-01-05 21:11:53 +01003240}
Gilles Peskineb74a1c72018-04-24 13:09:22 +02003241#endif /* MBEDTLS_SSL_ASYNC_PRIVATE */
Gilles Peskine8bf79f62018-01-05 21:11:53 +01003242
Paul Bakker5121ce52009-01-03 21:22:43 +00003243/*
3244 * SSL get accessors
3245 */
Gilles Peskine449bd832023-01-11 14:50:10 +01003246uint32_t mbedtls_ssl_get_verify_result(const mbedtls_ssl_context *ssl)
Paul Bakker5121ce52009-01-03 21:22:43 +00003247{
Gilles Peskine449bd832023-01-11 14:50:10 +01003248 if (ssl->session != NULL) {
3249 return ssl->session->verify_result;
3250 }
Manuel Pégourié-Gonnarde89163c2015-01-23 14:30:57 +00003251
Gilles Peskine449bd832023-01-11 14:50:10 +01003252 if (ssl->session_negotiate != NULL) {
3253 return ssl->session_negotiate->verify_result;
3254 }
Manuel Pégourié-Gonnarde89163c2015-01-23 14:30:57 +00003255
Gilles Peskine449bd832023-01-11 14:50:10 +01003256 return 0xFFFFFFFF;
Paul Bakker5121ce52009-01-03 21:22:43 +00003257}
3258
Gilles Peskine449bd832023-01-11 14:50:10 +01003259int mbedtls_ssl_get_ciphersuite_id_from_ssl(const mbedtls_ssl_context *ssl)
Glenn Strauss8f526902022-01-13 00:04:49 -05003260{
Gilles Peskine449bd832023-01-11 14:50:10 +01003261 if (ssl == NULL || ssl->session == NULL) {
3262 return 0;
3263 }
Glenn Strauss8f526902022-01-13 00:04:49 -05003264
Gilles Peskine449bd832023-01-11 14:50:10 +01003265 return ssl->session->ciphersuite;
Glenn Strauss8f526902022-01-13 00:04:49 -05003266}
3267
Gilles Peskine449bd832023-01-11 14:50:10 +01003268const char *mbedtls_ssl_get_ciphersuite(const mbedtls_ssl_context *ssl)
Paul Bakker72f62662011-01-16 21:27:44 +00003269{
Gilles Peskine449bd832023-01-11 14:50:10 +01003270 if (ssl == NULL || ssl->session == NULL) {
3271 return NULL;
3272 }
Paul Bakker926c8e42013-03-06 10:23:34 +01003273
Gilles Peskine449bd832023-01-11 14:50:10 +01003274 return mbedtls_ssl_get_ciphersuite_name(ssl->session->ciphersuite);
Paul Bakker72f62662011-01-16 21:27:44 +00003275}
3276
Gilles Peskine449bd832023-01-11 14:50:10 +01003277const char *mbedtls_ssl_get_version(const mbedtls_ssl_context *ssl)
Paul Bakker43ca69c2011-01-15 17:35:19 +00003278{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003279#if defined(MBEDTLS_SSL_PROTO_DTLS)
Gilles Peskine449bd832023-01-11 14:50:10 +01003280 if (ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM) {
3281 switch (ssl->tls_version) {
Glenn Strauss60bfe602022-03-14 19:04:24 -04003282 case MBEDTLS_SSL_VERSION_TLS1_2:
Gilles Peskine449bd832023-01-11 14:50:10 +01003283 return "DTLSv1.2";
Manuel Pégourié-Gonnardb21ca2a2014-02-10 13:43:33 +01003284 default:
Gilles Peskine449bd832023-01-11 14:50:10 +01003285 return "unknown (DTLS)";
Manuel Pégourié-Gonnardb21ca2a2014-02-10 13:43:33 +01003286 }
3287 }
3288#endif
3289
Gilles Peskine449bd832023-01-11 14:50:10 +01003290 switch (ssl->tls_version) {
Glenn Strauss60bfe602022-03-14 19:04:24 -04003291 case MBEDTLS_SSL_VERSION_TLS1_2:
Gilles Peskine449bd832023-01-11 14:50:10 +01003292 return "TLSv1.2";
Glenn Strauss60bfe602022-03-14 19:04:24 -04003293 case MBEDTLS_SSL_VERSION_TLS1_3:
Gilles Peskine449bd832023-01-11 14:50:10 +01003294 return "TLSv1.3";
Paul Bakker43ca69c2011-01-15 17:35:19 +00003295 default:
Gilles Peskine449bd832023-01-11 14:50:10 +01003296 return "unknown";
Paul Bakker43ca69c2011-01-15 17:35:19 +00003297 }
Paul Bakker43ca69c2011-01-15 17:35:19 +00003298}
3299
Manuel Pégourié-Gonnarda2cda6b2015-08-31 18:30:52 +02003300#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
Gilles Peskine449bd832023-01-11 14:50:10 +01003301size_t mbedtls_ssl_get_input_max_frag_len(const mbedtls_ssl_context *ssl)
Andrzej Kurek90c6e842020-04-03 05:25:29 -04003302{
David Horstmann95d516f2021-05-04 18:36:56 +01003303 size_t max_len = MBEDTLS_SSL_IN_CONTENT_LEN;
Andrzej Kurek90c6e842020-04-03 05:25:29 -04003304 size_t read_mfl;
3305
Jerry Yuddda0502022-12-01 19:43:12 +08003306#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
Andrzej Kurek90c6e842020-04-03 05:25:29 -04003307 /* Use the configured MFL for the client if we're past SERVER_HELLO_DONE */
Gilles Peskine449bd832023-01-11 14:50:10 +01003308 if (ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT &&
3309 ssl->state >= MBEDTLS_SSL_SERVER_HELLO_DONE) {
3310 return ssl_mfl_code_to_length(ssl->conf->mfl_code);
Andrzej Kurek90c6e842020-04-03 05:25:29 -04003311 }
Jerry Yuddda0502022-12-01 19:43:12 +08003312#endif
Andrzej Kurek90c6e842020-04-03 05:25:29 -04003313
3314 /* Check if a smaller max length was negotiated */
Gilles Peskine449bd832023-01-11 14:50:10 +01003315 if (ssl->session_out != NULL) {
3316 read_mfl = ssl_mfl_code_to_length(ssl->session_out->mfl_code);
3317 if (read_mfl < max_len) {
Andrzej Kurek90c6e842020-04-03 05:25:29 -04003318 max_len = read_mfl;
3319 }
3320 }
3321
Jerry Yuddda0502022-12-01 19:43:12 +08003322 /* During a handshake, use the value being negotiated */
Gilles Peskine449bd832023-01-11 14:50:10 +01003323 if (ssl->session_negotiate != NULL) {
3324 read_mfl = ssl_mfl_code_to_length(ssl->session_negotiate->mfl_code);
3325 if (read_mfl < max_len) {
Andrzej Kurek90c6e842020-04-03 05:25:29 -04003326 max_len = read_mfl;
3327 }
3328 }
3329
Gilles Peskine449bd832023-01-11 14:50:10 +01003330 return max_len;
Andrzej Kurek90c6e842020-04-03 05:25:29 -04003331}
3332
Gilles Peskine449bd832023-01-11 14:50:10 +01003333size_t mbedtls_ssl_get_output_max_frag_len(const mbedtls_ssl_context *ssl)
Manuel Pégourié-Gonnarda2cda6b2015-08-31 18:30:52 +02003334{
3335 size_t max_len;
3336
3337 /*
3338 * Assume mfl_code is correct since it was checked when set
3339 */
Gilles Peskine449bd832023-01-11 14:50:10 +01003340 max_len = ssl_mfl_code_to_length(ssl->conf->mfl_code);
Manuel Pégourié-Gonnarda2cda6b2015-08-31 18:30:52 +02003341
Manuel Pégourié-Gonnard2cb17e22017-09-19 13:00:47 +02003342 /* Check if a smaller max length was negotiated */
Gilles Peskine449bd832023-01-11 14:50:10 +01003343 if (ssl->session_out != NULL &&
3344 ssl_mfl_code_to_length(ssl->session_out->mfl_code) < max_len) {
3345 max_len = ssl_mfl_code_to_length(ssl->session_out->mfl_code);
Manuel Pégourié-Gonnarda2cda6b2015-08-31 18:30:52 +02003346 }
3347
Manuel Pégourié-Gonnard2cb17e22017-09-19 13:00:47 +02003348 /* During a handshake, use the value being negotiated */
Gilles Peskine449bd832023-01-11 14:50:10 +01003349 if (ssl->session_negotiate != NULL &&
3350 ssl_mfl_code_to_length(ssl->session_negotiate->mfl_code) < max_len) {
3351 max_len = ssl_mfl_code_to_length(ssl->session_negotiate->mfl_code);
Manuel Pégourié-Gonnard2cb17e22017-09-19 13:00:47 +02003352 }
3353
Gilles Peskine449bd832023-01-11 14:50:10 +01003354 return max_len;
Manuel Pégourié-Gonnarda2cda6b2015-08-31 18:30:52 +02003355}
3356#endif /* MBEDTLS_SSL_MAX_FRAGMENT_LENGTH */
3357
Manuel Pégourié-Gonnardb8eec192018-08-20 09:34:02 +02003358#if defined(MBEDTLS_SSL_PROTO_DTLS)
Gilles Peskine449bd832023-01-11 14:50:10 +01003359size_t mbedtls_ssl_get_current_mtu(const mbedtls_ssl_context *ssl)
Manuel Pégourié-Gonnardb8eec192018-08-20 09:34:02 +02003360{
Andrzej Kurekef43ce62018-10-09 08:24:12 -04003361 /* Return unlimited mtu for client hello messages to avoid fragmentation. */
Gilles Peskine449bd832023-01-11 14:50:10 +01003362 if (ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT &&
3363 (ssl->state == MBEDTLS_SSL_CLIENT_HELLO ||
3364 ssl->state == MBEDTLS_SSL_SERVER_HELLO)) {
3365 return 0;
3366 }
Andrzej Kurekef43ce62018-10-09 08:24:12 -04003367
Gilles Peskine449bd832023-01-11 14:50:10 +01003368 if (ssl->handshake == NULL || ssl->handshake->mtu == 0) {
3369 return ssl->mtu;
3370 }
Manuel Pégourié-Gonnardb8eec192018-08-20 09:34:02 +02003371
Gilles Peskine449bd832023-01-11 14:50:10 +01003372 if (ssl->mtu == 0) {
3373 return ssl->handshake->mtu;
3374 }
Manuel Pégourié-Gonnardb8eec192018-08-20 09:34:02 +02003375
Gilles Peskine449bd832023-01-11 14:50:10 +01003376 return ssl->mtu < ssl->handshake->mtu ?
3377 ssl->mtu : ssl->handshake->mtu;
Manuel Pégourié-Gonnardb8eec192018-08-20 09:34:02 +02003378}
3379#endif /* MBEDTLS_SSL_PROTO_DTLS */
3380
Gilles Peskine449bd832023-01-11 14:50:10 +01003381int mbedtls_ssl_get_max_out_record_payload(const mbedtls_ssl_context *ssl)
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02003382{
3383 size_t max_len = MBEDTLS_SSL_OUT_CONTENT_LEN;
3384
Manuel Pégourié-Gonnard000281e2018-08-21 11:20:58 +02003385#if !defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH) && \
3386 !defined(MBEDTLS_SSL_PROTO_DTLS)
3387 (void) ssl;
3388#endif
3389
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02003390#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
Gilles Peskine449bd832023-01-11 14:50:10 +01003391 const size_t mfl = mbedtls_ssl_get_output_max_frag_len(ssl);
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02003392
Gilles Peskine449bd832023-01-11 14:50:10 +01003393 if (max_len > mfl) {
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02003394 max_len = mfl;
Gilles Peskine449bd832023-01-11 14:50:10 +01003395 }
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02003396#endif
3397
3398#if defined(MBEDTLS_SSL_PROTO_DTLS)
Gilles Peskine449bd832023-01-11 14:50:10 +01003399 if (mbedtls_ssl_get_current_mtu(ssl) != 0) {
3400 const size_t mtu = mbedtls_ssl_get_current_mtu(ssl);
3401 const int ret = mbedtls_ssl_get_record_expansion(ssl);
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02003402 const size_t overhead = (size_t) ret;
3403
Gilles Peskine449bd832023-01-11 14:50:10 +01003404 if (ret < 0) {
3405 return ret;
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02003406 }
3407
Gilles Peskine449bd832023-01-11 14:50:10 +01003408 if (mtu <= overhead) {
3409 MBEDTLS_SSL_DEBUG_MSG(1, ("MTU too low for record expansion"));
3410 return MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
3411 }
3412
3413 if (max_len > mtu - overhead) {
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02003414 max_len = mtu - overhead;
Gilles Peskine449bd832023-01-11 14:50:10 +01003415 }
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02003416 }
Manuel Pégourié-Gonnardb8eec192018-08-20 09:34:02 +02003417#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02003418
Hanno Becker0defedb2018-08-10 12:35:02 +01003419#if !defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH) && \
3420 !defined(MBEDTLS_SSL_PROTO_DTLS)
3421 ((void) ssl);
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02003422#endif
3423
Gilles Peskine449bd832023-01-11 14:50:10 +01003424 return (int) max_len;
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02003425}
3426
Gilles Peskine449bd832023-01-11 14:50:10 +01003427int mbedtls_ssl_get_max_in_record_payload(const mbedtls_ssl_context *ssl)
Hanno Becker2d8e99b2021-04-21 06:19:50 +01003428{
3429 size_t max_len = MBEDTLS_SSL_IN_CONTENT_LEN;
3430
3431#if !defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
3432 (void) ssl;
3433#endif
3434
3435#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
Gilles Peskine449bd832023-01-11 14:50:10 +01003436 const size_t mfl = mbedtls_ssl_get_input_max_frag_len(ssl);
Hanno Becker2d8e99b2021-04-21 06:19:50 +01003437
Gilles Peskine449bd832023-01-11 14:50:10 +01003438 if (max_len > mfl) {
Hanno Becker2d8e99b2021-04-21 06:19:50 +01003439 max_len = mfl;
Gilles Peskine449bd832023-01-11 14:50:10 +01003440 }
Hanno Becker2d8e99b2021-04-21 06:19:50 +01003441#endif
3442
Gilles Peskine449bd832023-01-11 14:50:10 +01003443 return (int) max_len;
Hanno Becker2d8e99b2021-04-21 06:19:50 +01003444}
3445
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003446#if defined(MBEDTLS_X509_CRT_PARSE_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01003447const mbedtls_x509_crt *mbedtls_ssl_get_peer_cert(const mbedtls_ssl_context *ssl)
Paul Bakkerb0550d92012-10-30 07:51:03 +00003448{
Gilles Peskine449bd832023-01-11 14:50:10 +01003449 if (ssl == NULL || ssl->session == NULL) {
3450 return NULL;
3451 }
Paul Bakkerb0550d92012-10-30 07:51:03 +00003452
Hanno Beckere6824572019-02-07 13:18:46 +00003453#if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
Gilles Peskine449bd832023-01-11 14:50:10 +01003454 return ssl->session->peer_cert;
Hanno Beckere6824572019-02-07 13:18:46 +00003455#else
Gilles Peskine449bd832023-01-11 14:50:10 +01003456 return NULL;
Hanno Beckere6824572019-02-07 13:18:46 +00003457#endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
Paul Bakkerb0550d92012-10-30 07:51:03 +00003458}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003459#endif /* MBEDTLS_X509_CRT_PARSE_C */
Paul Bakkerb0550d92012-10-30 07:51:03 +00003460
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003461#if defined(MBEDTLS_SSL_CLI_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01003462int mbedtls_ssl_get_session(const mbedtls_ssl_context *ssl,
3463 mbedtls_ssl_session *dst)
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02003464{
Hanno Beckere810bbc2021-05-14 16:01:05 +01003465 int ret;
3466
Gilles Peskine449bd832023-01-11 14:50:10 +01003467 if (ssl == NULL ||
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02003468 dst == NULL ||
3469 ssl->session == NULL ||
Gilles Peskine449bd832023-01-11 14:50:10 +01003470 ssl->conf->endpoint != MBEDTLS_SSL_IS_CLIENT) {
3471 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02003472 }
3473
Hanno Beckere810bbc2021-05-14 16:01:05 +01003474 /* Since Mbed TLS 3.0, mbedtls_ssl_get_session() is no longer
3475 * idempotent: Each session can only be exported once.
3476 *
3477 * (This is in preparation for TLS 1.3 support where we will
3478 * need the ability to export multiple sessions (aka tickets),
3479 * which will be achieved by calling mbedtls_ssl_get_session()
3480 * multiple times until it fails.)
3481 *
3482 * Check whether we have already exported the current session,
3483 * and fail if so.
3484 */
Gilles Peskine449bd832023-01-11 14:50:10 +01003485 if (ssl->session->exported == 1) {
3486 return MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
3487 }
Hanno Beckere810bbc2021-05-14 16:01:05 +01003488
Gilles Peskine449bd832023-01-11 14:50:10 +01003489 ret = mbedtls_ssl_session_copy(dst, ssl->session);
3490 if (ret != 0) {
3491 return ret;
3492 }
Hanno Beckere810bbc2021-05-14 16:01:05 +01003493
3494 /* Remember that we've exported the session. */
3495 ssl->session->exported = 1;
Gilles Peskine449bd832023-01-11 14:50:10 +01003496 return 0;
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02003497}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003498#endif /* MBEDTLS_SSL_CLI_C */
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02003499
Paul Bakker5121ce52009-01-03 21:22:43 +00003500/*
Hanno Beckera835da52019-05-16 12:39:07 +01003501 * Define ticket header determining Mbed TLS version
3502 * and structure of the ticket.
3503 */
3504
Hanno Becker94ef3b32019-05-16 12:50:45 +01003505/*
Hanno Becker50b59662019-05-28 14:30:45 +01003506 * Define bitflag determining compile-time settings influencing
3507 * structure of serialized SSL sessions.
Hanno Becker94ef3b32019-05-16 12:50:45 +01003508 */
3509
Hanno Becker50b59662019-05-28 14:30:45 +01003510#if defined(MBEDTLS_HAVE_TIME)
Hanno Becker3e088662019-05-29 11:10:18 +01003511#define SSL_SERIALIZED_SESSION_CONFIG_TIME 1
Hanno Becker50b59662019-05-28 14:30:45 +01003512#else
Hanno Becker3e088662019-05-29 11:10:18 +01003513#define SSL_SERIALIZED_SESSION_CONFIG_TIME 0
Hanno Becker94ef3b32019-05-16 12:50:45 +01003514#endif /* MBEDTLS_HAVE_TIME */
3515
3516#if defined(MBEDTLS_X509_CRT_PARSE_C)
Hanno Becker3e088662019-05-29 11:10:18 +01003517#define SSL_SERIALIZED_SESSION_CONFIG_CRT 1
Hanno Becker94ef3b32019-05-16 12:50:45 +01003518#else
Hanno Becker3e088662019-05-29 11:10:18 +01003519#define SSL_SERIALIZED_SESSION_CONFIG_CRT 0
Hanno Becker94ef3b32019-05-16 12:50:45 +01003520#endif /* MBEDTLS_X509_CRT_PARSE_C */
3521
3522#if defined(MBEDTLS_SSL_CLI_C) && defined(MBEDTLS_SSL_SESSION_TICKETS)
Hanno Becker3e088662019-05-29 11:10:18 +01003523#define SSL_SERIALIZED_SESSION_CONFIG_CLIENT_TICKET 1
Hanno Becker94ef3b32019-05-16 12:50:45 +01003524#else
Hanno Becker3e088662019-05-29 11:10:18 +01003525#define SSL_SERIALIZED_SESSION_CONFIG_CLIENT_TICKET 0
Hanno Becker94ef3b32019-05-16 12:50:45 +01003526#endif /* MBEDTLS_SSL_CLI_C && MBEDTLS_SSL_SESSION_TICKETS */
3527
3528#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
Hanno Becker3e088662019-05-29 11:10:18 +01003529#define SSL_SERIALIZED_SESSION_CONFIG_MFL 1
Hanno Becker94ef3b32019-05-16 12:50:45 +01003530#else
Hanno Becker3e088662019-05-29 11:10:18 +01003531#define SSL_SERIALIZED_SESSION_CONFIG_MFL 0
Hanno Becker94ef3b32019-05-16 12:50:45 +01003532#endif /* MBEDTLS_SSL_MAX_FRAGMENT_LENGTH */
3533
Hanno Becker94ef3b32019-05-16 12:50:45 +01003534#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
Hanno Becker3e088662019-05-29 11:10:18 +01003535#define SSL_SERIALIZED_SESSION_CONFIG_ETM 1
Hanno Becker94ef3b32019-05-16 12:50:45 +01003536#else
Hanno Becker3e088662019-05-29 11:10:18 +01003537#define SSL_SERIALIZED_SESSION_CONFIG_ETM 0
Hanno Becker94ef3b32019-05-16 12:50:45 +01003538#endif /* MBEDTLS_SSL_ENCRYPT_THEN_MAC */
3539
Hanno Becker94ef3b32019-05-16 12:50:45 +01003540#if defined(MBEDTLS_SSL_SESSION_TICKETS)
3541#define SSL_SERIALIZED_SESSION_CONFIG_TICKET 1
3542#else
3543#define SSL_SERIALIZED_SESSION_CONFIG_TICKET 0
3544#endif /* MBEDTLS_SSL_SESSION_TICKETS */
3545
Hanno Becker3e088662019-05-29 11:10:18 +01003546#define SSL_SERIALIZED_SESSION_CONFIG_TIME_BIT 0
3547#define SSL_SERIALIZED_SESSION_CONFIG_CRT_BIT 1
3548#define SSL_SERIALIZED_SESSION_CONFIG_CLIENT_TICKET_BIT 2
3549#define SSL_SERIALIZED_SESSION_CONFIG_MFL_BIT 3
Hanno Becker37bdbe62021-08-01 05:38:58 +01003550#define SSL_SERIALIZED_SESSION_CONFIG_ETM_BIT 4
3551#define SSL_SERIALIZED_SESSION_CONFIG_TICKET_BIT 5
Hanno Becker3e088662019-05-29 11:10:18 +01003552
Hanno Becker50b59662019-05-28 14:30:45 +01003553#define SSL_SERIALIZED_SESSION_CONFIG_BITFLAG \
Gilles Peskine449bd832023-01-11 14:50:10 +01003554 ((uint16_t) ( \
3555 (SSL_SERIALIZED_SESSION_CONFIG_TIME << SSL_SERIALIZED_SESSION_CONFIG_TIME_BIT) | \
3556 (SSL_SERIALIZED_SESSION_CONFIG_CRT << SSL_SERIALIZED_SESSION_CONFIG_CRT_BIT) | \
3557 (SSL_SERIALIZED_SESSION_CONFIG_CLIENT_TICKET << \
3558 SSL_SERIALIZED_SESSION_CONFIG_CLIENT_TICKET_BIT) | \
3559 (SSL_SERIALIZED_SESSION_CONFIG_MFL << SSL_SERIALIZED_SESSION_CONFIG_MFL_BIT) | \
3560 (SSL_SERIALIZED_SESSION_CONFIG_ETM << SSL_SERIALIZED_SESSION_CONFIG_ETM_BIT) | \
3561 (SSL_SERIALIZED_SESSION_CONFIG_TICKET << SSL_SERIALIZED_SESSION_CONFIG_TICKET_BIT)))
Hanno Becker94ef3b32019-05-16 12:50:45 +01003562
Hanno Beckerf8787072019-05-16 12:41:07 +01003563static unsigned char ssl_serialized_session_header[] = {
Hanno Becker94ef3b32019-05-16 12:50:45 +01003564 MBEDTLS_VERSION_MAJOR,
3565 MBEDTLS_VERSION_MINOR,
3566 MBEDTLS_VERSION_PATCH,
Gilles Peskine449bd832023-01-11 14:50:10 +01003567 MBEDTLS_BYTE_1(SSL_SERIALIZED_SESSION_CONFIG_BITFLAG),
3568 MBEDTLS_BYTE_0(SSL_SERIALIZED_SESSION_CONFIG_BITFLAG),
Hanno Beckerf8787072019-05-16 12:41:07 +01003569};
Hanno Beckera835da52019-05-16 12:39:07 +01003570
3571/*
Manuel Pégourié-Gonnarda3e7c652019-05-16 10:08:35 +02003572 * Serialize a session in the following format:
Manuel Pégourié-Gonnard35eb8022019-05-16 11:11:08 +02003573 * (in the presentation language of TLS, RFC 8446 section 3)
Manuel Pégourié-Gonnarda3e7c652019-05-16 10:08:35 +02003574 *
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01003575 * struct {
Hanno Beckerdc28b6c2019-05-29 11:08:00 +01003576 *
Hanno Beckerdce50972021-08-01 05:39:23 +01003577 * opaque mbedtls_version[3]; // library version: major, minor, patch
3578 * opaque session_format[2]; // library-version specific 16-bit field
3579 * // determining the format of the remaining
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01003580 * // serialized data.
Hanno Beckerdc28b6c2019-05-29 11:08:00 +01003581 *
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01003582 * Note: When updating the format, remember to keep
3583 * these version+format bytes.
Manuel Pégourié-Gonnarda3e7c652019-05-16 10:08:35 +02003584 *
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01003585 * // In this version, `session_format` determines
3586 * // the setting of those compile-time
3587 * // configuration options which influence
3588 * // the structure of mbedtls_ssl_session.
3589 *
Glenn Straussda7851c2022-03-14 13:29:48 -04003590 * uint8_t minor_ver; // Protocol minor version. Possible values:
Jerry Yu0c2a7382022-12-06 13:27:25 +08003591 * // - TLS 1.2 (0x0303)
3592 * // - TLS 1.3 (0x0304)
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01003593 *
Glenn Straussda7851c2022-03-14 13:29:48 -04003594 * select (serialized_session.tls_version) {
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01003595 *
Glenn Straussda7851c2022-03-14 13:29:48 -04003596 * case MBEDTLS_SSL_VERSION_TLS1_2:
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01003597 * serialized_session_tls12 data;
Jerry Yu0c2a7382022-12-06 13:27:25 +08003598 * case MBEDTLS_SSL_VERSION_TLS1_3:
Jerry Yuddda0502022-12-01 19:43:12 +08003599 * serialized_session_tls13 data;
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01003600 *
3601 * };
3602 *
3603 * } serialized_session;
3604 *
Manuel Pégourié-Gonnarda3e7c652019-05-16 10:08:35 +02003605 */
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01003606
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02003607MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01003608static int ssl_session_save(const mbedtls_ssl_session *session,
3609 unsigned char omit_header,
3610 unsigned char *buf,
3611 size_t buf_len,
3612 size_t *olen)
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01003613{
3614 unsigned char *p = buf;
3615 size_t used = 0;
Jerry Yu251a12e2022-07-13 15:15:48 +08003616 size_t remaining_len;
Jerry Yue36fdd62022-08-17 21:31:36 +08003617#if defined(MBEDTLS_SSL_PROTO_TLS1_3)
3618 size_t out_len;
3619 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
3620#endif
Gilles Peskine449bd832023-01-11 14:50:10 +01003621 if (session == NULL) {
3622 return MBEDTLS_ERR_SSL_INTERNAL_ERROR;
3623 }
Jerry Yu438ddd82022-07-07 06:55:50 +00003624
Gilles Peskine449bd832023-01-11 14:50:10 +01003625 if (!omit_header) {
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01003626 /*
3627 * Add Mbed TLS version identifier
3628 */
Gilles Peskine449bd832023-01-11 14:50:10 +01003629 used += sizeof(ssl_serialized_session_header);
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01003630
Gilles Peskine449bd832023-01-11 14:50:10 +01003631 if (used <= buf_len) {
3632 memcpy(p, ssl_serialized_session_header,
3633 sizeof(ssl_serialized_session_header));
3634 p += sizeof(ssl_serialized_session_header);
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01003635 }
3636 }
3637
3638 /*
3639 * TLS version identifier
3640 */
3641 used += 1;
Gilles Peskine449bd832023-01-11 14:50:10 +01003642 if (used <= buf_len) {
3643 *p++ = MBEDTLS_BYTE_0(session->tls_version);
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01003644 }
3645
3646 /* Forward to version-specific serialization routine. */
Jerry Yufca4d572022-07-21 10:37:48 +08003647 remaining_len = (buf_len >= used) ? buf_len - used : 0;
Gilles Peskine449bd832023-01-11 14:50:10 +01003648 switch (session->tls_version) {
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01003649#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
Gilles Peskine449bd832023-01-11 14:50:10 +01003650 case MBEDTLS_SSL_VERSION_TLS1_2:
3651 used += ssl_tls12_session_save(session, p, remaining_len);
3652 break;
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01003653#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
3654
Jerry Yu251a12e2022-07-13 15:15:48 +08003655#if defined(MBEDTLS_SSL_PROTO_TLS1_3)
Gilles Peskine449bd832023-01-11 14:50:10 +01003656 case MBEDTLS_SSL_VERSION_TLS1_3:
3657 ret = ssl_tls13_session_save(session, p, remaining_len, &out_len);
3658 if (ret != 0 && ret != MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL) {
3659 return ret;
3660 }
3661 used += out_len;
3662 break;
Jerry Yu251a12e2022-07-13 15:15:48 +08003663#endif /* MBEDTLS_SSL_PROTO_TLS1_3 */
3664
Gilles Peskine449bd832023-01-11 14:50:10 +01003665 default:
3666 return MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01003667 }
3668
3669 *olen = used;
Gilles Peskine449bd832023-01-11 14:50:10 +01003670 if (used > buf_len) {
3671 return MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL;
3672 }
Manuel Pégourié-Gonnarda3e7c652019-05-16 10:08:35 +02003673
Gilles Peskine449bd832023-01-11 14:50:10 +01003674 return 0;
Manuel Pégourié-Gonnarda3e7c652019-05-16 10:08:35 +02003675}
3676
3677/*
Manuel Pégourié-Gonnard45ac1f02019-07-23 16:52:45 +02003678 * Public wrapper for ssl_session_save()
3679 */
Gilles Peskine449bd832023-01-11 14:50:10 +01003680int mbedtls_ssl_session_save(const mbedtls_ssl_session *session,
3681 unsigned char *buf,
3682 size_t buf_len,
3683 size_t *olen)
Manuel Pégourié-Gonnard45ac1f02019-07-23 16:52:45 +02003684{
Gilles Peskine449bd832023-01-11 14:50:10 +01003685 return ssl_session_save(session, 0, buf, buf_len, olen);
Manuel Pégourié-Gonnard45ac1f02019-07-23 16:52:45 +02003686}
Manuel Pégourié-Gonnarda3e7c652019-05-16 10:08:35 +02003687
Jerry Yuf1b23ca2022-02-18 11:48:47 +08003688/*
3689 * Deserialize session, see mbedtls_ssl_session_save() for format.
3690 *
3691 * This internal version is wrapped by a public function that cleans up in
3692 * case of error, and has an extra option omit_header.
3693 */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02003694MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01003695static int ssl_session_load(mbedtls_ssl_session *session,
3696 unsigned char omit_header,
3697 const unsigned char *buf,
3698 size_t len)
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01003699{
3700 const unsigned char *p = buf;
3701 const unsigned char * const end = buf + len;
Jerry Yu438ddd82022-07-07 06:55:50 +00003702 size_t remaining_len;
3703
3704
Gilles Peskine449bd832023-01-11 14:50:10 +01003705 if (session == NULL) {
3706 return MBEDTLS_ERR_SSL_INTERNAL_ERROR;
3707 }
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01003708
Gilles Peskine449bd832023-01-11 14:50:10 +01003709 if (!omit_header) {
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01003710 /*
3711 * Check Mbed TLS version identifier
3712 */
3713
Gilles Peskine449bd832023-01-11 14:50:10 +01003714 if ((size_t) (end - p) < sizeof(ssl_serialized_session_header)) {
3715 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01003716 }
Gilles Peskine449bd832023-01-11 14:50:10 +01003717
3718 if (memcmp(p, ssl_serialized_session_header,
3719 sizeof(ssl_serialized_session_header)) != 0) {
3720 return MBEDTLS_ERR_SSL_VERSION_MISMATCH;
3721 }
3722 p += sizeof(ssl_serialized_session_header);
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01003723 }
3724
3725 /*
3726 * TLS version identifier
3727 */
Gilles Peskine449bd832023-01-11 14:50:10 +01003728 if (1 > (size_t) (end - p)) {
3729 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
3730 }
Glenn Straussda7851c2022-03-14 13:29:48 -04003731 session->tls_version = 0x0300 | *p++;
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01003732
3733 /* Dispatch according to TLS version. */
Gilles Peskine449bd832023-01-11 14:50:10 +01003734 remaining_len = (end - p);
3735 switch (session->tls_version) {
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01003736#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
Gilles Peskine449bd832023-01-11 14:50:10 +01003737 case MBEDTLS_SSL_VERSION_TLS1_2:
3738 return ssl_tls12_session_load(session, p, remaining_len);
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01003739#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
3740
Jerry Yu438ddd82022-07-07 06:55:50 +00003741#if defined(MBEDTLS_SSL_PROTO_TLS1_3)
Gilles Peskine449bd832023-01-11 14:50:10 +01003742 case MBEDTLS_SSL_VERSION_TLS1_3:
3743 return ssl_tls13_session_load(session, p, remaining_len);
Jerry Yu438ddd82022-07-07 06:55:50 +00003744#endif /* MBEDTLS_SSL_PROTO_TLS1_3 */
3745
Gilles Peskine449bd832023-01-11 14:50:10 +01003746 default:
3747 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01003748 }
3749}
3750
Manuel Pégourié-Gonnarda3e7c652019-05-16 10:08:35 +02003751/*
Manuel Pégourié-Gonnardb9dfc9f2019-07-12 10:50:19 +02003752 * Deserialize session: public wrapper for error cleaning
Manuel Pégourié-Gonnarda3d831b2019-05-23 12:28:45 +02003753 */
Gilles Peskine449bd832023-01-11 14:50:10 +01003754int mbedtls_ssl_session_load(mbedtls_ssl_session *session,
3755 const unsigned char *buf,
3756 size_t len)
Manuel Pégourié-Gonnarda3d831b2019-05-23 12:28:45 +02003757{
Gilles Peskine449bd832023-01-11 14:50:10 +01003758 int ret = ssl_session_load(session, 0, buf, len);
Manuel Pégourié-Gonnarda3d831b2019-05-23 12:28:45 +02003759
Gilles Peskine449bd832023-01-11 14:50:10 +01003760 if (ret != 0) {
3761 mbedtls_ssl_session_free(session);
3762 }
Manuel Pégourié-Gonnarda3d831b2019-05-23 12:28:45 +02003763
Gilles Peskine449bd832023-01-11 14:50:10 +01003764 return ret;
Manuel Pégourié-Gonnarda3d831b2019-05-23 12:28:45 +02003765}
3766
3767/*
Paul Bakker1961b702013-01-25 14:49:24 +01003768 * Perform a single step of the SSL handshake
Paul Bakker5121ce52009-01-03 21:22:43 +00003769 */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02003770MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01003771static int ssl_prepare_handshake_step(mbedtls_ssl_context *ssl)
Hanno Becker41934dd2021-08-07 19:13:43 +01003772{
3773 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
3774
Ronald Cron66dbf912022-02-02 15:33:46 +01003775 /*
3776 * We may have not been able to send to the peer all the handshake data
Ronald Cron3f20b772022-03-08 16:00:02 +01003777 * that were written into the output buffer by the previous handshake step,
3778 * if the write to the network callback returned with the
Ronald Cron66dbf912022-02-02 15:33:46 +01003779 * #MBEDTLS_ERR_SSL_WANT_WRITE error code.
3780 * We proceed to the next handshake step only when all data from the
3781 * previous one have been sent to the peer, thus we make sure that this is
3782 * the case here by calling `mbedtls_ssl_flush_output()`. The function may
3783 * return with the #MBEDTLS_ERR_SSL_WANT_WRITE error code in which case
3784 * we have to wait before to go ahead.
3785 * In the case of TLS 1.3, handshake step handlers do not send data to the
3786 * peer. Data are only sent here and through
3787 * `mbedtls_ssl_handle_pending_alert` in case an error that triggered an
Andrzej Kurek5c65c572022-04-13 14:28:52 -04003788 * alert occurred.
Ronald Cron66dbf912022-02-02 15:33:46 +01003789 */
Gilles Peskine449bd832023-01-11 14:50:10 +01003790 if ((ret = mbedtls_ssl_flush_output(ssl)) != 0) {
3791 return ret;
3792 }
Hanno Becker41934dd2021-08-07 19:13:43 +01003793
3794#if defined(MBEDTLS_SSL_PROTO_DTLS)
Gilles Peskine449bd832023-01-11 14:50:10 +01003795 if (ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
3796 ssl->handshake->retransmit_state == MBEDTLS_SSL_RETRANS_SENDING) {
3797 if ((ret = mbedtls_ssl_flight_transmit(ssl)) != 0) {
3798 return ret;
3799 }
Hanno Becker41934dd2021-08-07 19:13:43 +01003800 }
3801#endif /* MBEDTLS_SSL_PROTO_DTLS */
3802
Gilles Peskine449bd832023-01-11 14:50:10 +01003803 return ret;
Hanno Becker41934dd2021-08-07 19:13:43 +01003804}
3805
Gilles Peskine449bd832023-01-11 14:50:10 +01003806int mbedtls_ssl_handshake_step(mbedtls_ssl_context *ssl)
Paul Bakker5121ce52009-01-03 21:22:43 +00003807{
Hanno Becker41934dd2021-08-07 19:13:43 +01003808 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker5121ce52009-01-03 21:22:43 +00003809
Gilles Peskine449bd832023-01-11 14:50:10 +01003810 if (ssl == NULL ||
Hanno Becker41934dd2021-08-07 19:13:43 +01003811 ssl->conf == NULL ||
3812 ssl->handshake == NULL ||
Gilles Peskine449bd832023-01-11 14:50:10 +01003813 ssl->state == MBEDTLS_SSL_HANDSHAKE_OVER) {
3814 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
Hanno Becker41934dd2021-08-07 19:13:43 +01003815 }
3816
Gilles Peskine449bd832023-01-11 14:50:10 +01003817 ret = ssl_prepare_handshake_step(ssl);
3818 if (ret != 0) {
3819 return ret;
3820 }
Manuel Pégourié-Gonnardf81ee2e2015-09-01 17:43:40 +02003821
Gilles Peskine449bd832023-01-11 14:50:10 +01003822 ret = mbedtls_ssl_handle_pending_alert(ssl);
3823 if (ret != 0) {
Jerry Yue7047812021-09-13 19:26:39 +08003824 goto cleanup;
Gilles Peskine449bd832023-01-11 14:50:10 +01003825 }
Jerry Yue7047812021-09-13 19:26:39 +08003826
Tom Cosgrove2fdc7b32022-09-21 12:33:17 +01003827 /* If ssl->conf->endpoint is not one of MBEDTLS_SSL_IS_CLIENT or
3828 * MBEDTLS_SSL_IS_SERVER, this is the return code we give */
3829 ret = MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
3830
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003831#if defined(MBEDTLS_SSL_CLI_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01003832 if (ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT) {
3833 MBEDTLS_SSL_DEBUG_MSG(2, ("client state: %s",
3834 mbedtls_ssl_states_str(ssl->state)));
Jerry Yub9930e72021-08-06 17:11:51 +08003835
Gilles Peskine449bd832023-01-11 14:50:10 +01003836 switch (ssl->state) {
Ronald Cron9f0fba32022-02-10 16:45:15 +01003837 case MBEDTLS_SSL_HELLO_REQUEST:
3838 ssl->state = MBEDTLS_SSL_CLIENT_HELLO;
Tom Cosgrove87d9c6c2022-09-22 09:27:56 +01003839 ret = 0;
Ronald Cron9f0fba32022-02-10 16:45:15 +01003840 break;
Jerry Yub9930e72021-08-06 17:11:51 +08003841
Ronald Cron9f0fba32022-02-10 16:45:15 +01003842 case MBEDTLS_SSL_CLIENT_HELLO:
Gilles Peskine449bd832023-01-11 14:50:10 +01003843 ret = mbedtls_ssl_write_client_hello(ssl);
Ronald Cron9f0fba32022-02-10 16:45:15 +01003844 break;
3845
3846 default:
3847#if defined(MBEDTLS_SSL_PROTO_TLS1_2) && defined(MBEDTLS_SSL_PROTO_TLS1_3)
Gilles Peskine449bd832023-01-11 14:50:10 +01003848 if (ssl->tls_version == MBEDTLS_SSL_VERSION_TLS1_3) {
3849 ret = mbedtls_ssl_tls13_handshake_client_step(ssl);
3850 } else {
3851 ret = mbedtls_ssl_handshake_client_step(ssl);
3852 }
Ronald Cron9f0fba32022-02-10 16:45:15 +01003853#elif defined(MBEDTLS_SSL_PROTO_TLS1_2)
Gilles Peskine449bd832023-01-11 14:50:10 +01003854 ret = mbedtls_ssl_handshake_client_step(ssl);
Ronald Cron9f0fba32022-02-10 16:45:15 +01003855#else
Gilles Peskine449bd832023-01-11 14:50:10 +01003856 ret = mbedtls_ssl_tls13_handshake_client_step(ssl);
Ronald Cron9f0fba32022-02-10 16:45:15 +01003857#endif
3858 }
Jerry Yub9930e72021-08-06 17:11:51 +08003859 }
Paul Bakker5121ce52009-01-03 21:22:43 +00003860#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003861#if defined(MBEDTLS_SSL_SRV_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01003862 if (ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER) {
Ronald Cron6f135e12021-12-08 16:57:54 +01003863#if defined(MBEDTLS_SSL_PROTO_TLS1_3)
Gilles Peskine449bd832023-01-11 14:50:10 +01003864 if (mbedtls_ssl_conf_is_tls13_only(ssl->conf)) {
3865 ret = mbedtls_ssl_tls13_handshake_server_step(ssl);
3866 }
Ronald Cron6f135e12021-12-08 16:57:54 +01003867#endif /* MBEDTLS_SSL_PROTO_TLS1_3 */
Jerry Yub9930e72021-08-06 17:11:51 +08003868
3869#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
Gilles Peskine449bd832023-01-11 14:50:10 +01003870 if (mbedtls_ssl_conf_is_tls12_only(ssl->conf)) {
3871 ret = mbedtls_ssl_handshake_server_step(ssl);
3872 }
Jerry Yub9930e72021-08-06 17:11:51 +08003873#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
3874 }
Paul Bakker5121ce52009-01-03 21:22:43 +00003875#endif
3876
Gilles Peskine449bd832023-01-11 14:50:10 +01003877 if (ret != 0) {
Jerry Yubbd5a3f2021-09-18 20:50:22 +08003878 /* handshake_step return error. And it is same
3879 * with alert_reason.
3880 */
Gilles Peskine449bd832023-01-11 14:50:10 +01003881 if (ssl->send_alert) {
3882 ret = mbedtls_ssl_handle_pending_alert(ssl);
Jerry Yue7047812021-09-13 19:26:39 +08003883 goto cleanup;
3884 }
3885 }
3886
3887cleanup:
Gilles Peskine449bd832023-01-11 14:50:10 +01003888 return ret;
Paul Bakker1961b702013-01-25 14:49:24 +01003889}
3890
3891/*
3892 * Perform the SSL handshake
3893 */
Gilles Peskine449bd832023-01-11 14:50:10 +01003894int mbedtls_ssl_handshake(mbedtls_ssl_context *ssl)
Paul Bakker1961b702013-01-25 14:49:24 +01003895{
3896 int ret = 0;
3897
Hanno Beckera817ea42020-10-20 15:20:23 +01003898 /* Sanity checks */
3899
Gilles Peskine449bd832023-01-11 14:50:10 +01003900 if (ssl == NULL || ssl->conf == NULL) {
3901 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
3902 }
Manuel Pégourié-Gonnardf81ee2e2015-09-01 17:43:40 +02003903
Hanno Beckera817ea42020-10-20 15:20:23 +01003904#if defined(MBEDTLS_SSL_PROTO_DTLS)
Gilles Peskine449bd832023-01-11 14:50:10 +01003905 if (ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
3906 (ssl->f_set_timer == NULL || ssl->f_get_timer == NULL)) {
3907 MBEDTLS_SSL_DEBUG_MSG(1, ("You must use "
3908 "mbedtls_ssl_set_timer_cb() for DTLS"));
3909 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
Hanno Beckera817ea42020-10-20 15:20:23 +01003910 }
3911#endif /* MBEDTLS_SSL_PROTO_DTLS */
3912
Gilles Peskine449bd832023-01-11 14:50:10 +01003913 MBEDTLS_SSL_DEBUG_MSG(2, ("=> handshake"));
Paul Bakker1961b702013-01-25 14:49:24 +01003914
Hanno Beckera817ea42020-10-20 15:20:23 +01003915 /* Main handshake loop */
Gilles Peskine449bd832023-01-11 14:50:10 +01003916 while (ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER) {
3917 ret = mbedtls_ssl_handshake_step(ssl);
Paul Bakker1961b702013-01-25 14:49:24 +01003918
Gilles Peskine449bd832023-01-11 14:50:10 +01003919 if (ret != 0) {
Paul Bakker1961b702013-01-25 14:49:24 +01003920 break;
Gilles Peskine449bd832023-01-11 14:50:10 +01003921 }
Paul Bakker1961b702013-01-25 14:49:24 +01003922 }
3923
Gilles Peskine449bd832023-01-11 14:50:10 +01003924 MBEDTLS_SSL_DEBUG_MSG(2, ("<= handshake"));
Paul Bakker5121ce52009-01-03 21:22:43 +00003925
Gilles Peskine449bd832023-01-11 14:50:10 +01003926 return ret;
Paul Bakker5121ce52009-01-03 21:22:43 +00003927}
3928
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003929#if defined(MBEDTLS_SSL_RENEGOTIATION)
3930#if defined(MBEDTLS_SSL_SRV_C)
Paul Bakker5121ce52009-01-03 21:22:43 +00003931/*
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01003932 * Write HelloRequest to request renegotiation on server
Paul Bakker48916f92012-09-16 19:57:18 +00003933 */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02003934MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01003935static int ssl_write_hello_request(mbedtls_ssl_context *ssl)
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01003936{
Janos Follath865b3eb2019-12-16 11:46:15 +00003937 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01003938
Gilles Peskine449bd832023-01-11 14:50:10 +01003939 MBEDTLS_SSL_DEBUG_MSG(2, ("=> write hello request"));
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01003940
3941 ssl->out_msglen = 4;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003942 ssl->out_msgtype = MBEDTLS_SSL_MSG_HANDSHAKE;
3943 ssl->out_msg[0] = MBEDTLS_SSL_HS_HELLO_REQUEST;
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01003944
Gilles Peskine449bd832023-01-11 14:50:10 +01003945 if ((ret = mbedtls_ssl_write_handshake_msg(ssl)) != 0) {
3946 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ssl_write_handshake_msg", ret);
3947 return ret;
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01003948 }
3949
Gilles Peskine449bd832023-01-11 14:50:10 +01003950 MBEDTLS_SSL_DEBUG_MSG(2, ("<= write hello request"));
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01003951
Gilles Peskine449bd832023-01-11 14:50:10 +01003952 return 0;
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01003953}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003954#endif /* MBEDTLS_SSL_SRV_C */
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01003955
3956/*
3957 * Actually renegotiate current connection, triggered by either:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003958 * - any side: calling mbedtls_ssl_renegotiate(),
3959 * - client: receiving a HelloRequest during mbedtls_ssl_read(),
3960 * - server: receiving any handshake message on server during mbedtls_ssl_read() after
Manuel Pégourié-Gonnard55e4ff22014-08-19 11:16:35 +02003961 * the initial handshake is completed.
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01003962 * If the handshake doesn't complete due to waiting for I/O, it will continue
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003963 * during the next calls to mbedtls_ssl_renegotiate() or mbedtls_ssl_read() respectively.
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01003964 */
Gilles Peskine449bd832023-01-11 14:50:10 +01003965int mbedtls_ssl_start_renegotiation(mbedtls_ssl_context *ssl)
Paul Bakker48916f92012-09-16 19:57:18 +00003966{
Janos Follath865b3eb2019-12-16 11:46:15 +00003967 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker48916f92012-09-16 19:57:18 +00003968
Gilles Peskine449bd832023-01-11 14:50:10 +01003969 MBEDTLS_SSL_DEBUG_MSG(2, ("=> renegotiate"));
Paul Bakker48916f92012-09-16 19:57:18 +00003970
Gilles Peskine449bd832023-01-11 14:50:10 +01003971 if ((ret = ssl_handshake_init(ssl)) != 0) {
3972 return ret;
3973 }
Paul Bakker48916f92012-09-16 19:57:18 +00003974
Manuel Pégourié-Gonnard0557bd52014-08-19 19:18:39 +02003975 /* RFC 6347 4.2.2: "[...] the HelloRequest will have message_seq = 0 and
3976 * the ServerHello will have message_seq = 1" */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003977#if defined(MBEDTLS_SSL_PROTO_DTLS)
Gilles Peskine449bd832023-01-11 14:50:10 +01003978 if (ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
3979 ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_PENDING) {
3980 if (ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER) {
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02003981 ssl->handshake->out_msg_seq = 1;
Gilles Peskine449bd832023-01-11 14:50:10 +01003982 } else {
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02003983 ssl->handshake->in_msg_seq = 1;
Gilles Peskine449bd832023-01-11 14:50:10 +01003984 }
Manuel Pégourié-Gonnard0557bd52014-08-19 19:18:39 +02003985 }
3986#endif
3987
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003988 ssl->state = MBEDTLS_SSL_HELLO_REQUEST;
3989 ssl->renego_status = MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS;
Paul Bakker48916f92012-09-16 19:57:18 +00003990
Gilles Peskine449bd832023-01-11 14:50:10 +01003991 if ((ret = mbedtls_ssl_handshake(ssl)) != 0) {
3992 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ssl_handshake", ret);
3993 return ret;
Paul Bakker48916f92012-09-16 19:57:18 +00003994 }
3995
Gilles Peskine449bd832023-01-11 14:50:10 +01003996 MBEDTLS_SSL_DEBUG_MSG(2, ("<= renegotiate"));
Paul Bakker48916f92012-09-16 19:57:18 +00003997
Gilles Peskine449bd832023-01-11 14:50:10 +01003998 return 0;
Paul Bakker48916f92012-09-16 19:57:18 +00003999}
4000
4001/*
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01004002 * Renegotiate current connection on client,
4003 * or request renegotiation on server
4004 */
Gilles Peskine449bd832023-01-11 14:50:10 +01004005int mbedtls_ssl_renegotiate(mbedtls_ssl_context *ssl)
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01004006{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004007 int ret = MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01004008
Gilles Peskine449bd832023-01-11 14:50:10 +01004009 if (ssl == NULL || ssl->conf == NULL) {
4010 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
4011 }
Manuel Pégourié-Gonnardf81ee2e2015-09-01 17:43:40 +02004012
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004013#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01004014 /* On server, just send the request */
Gilles Peskine449bd832023-01-11 14:50:10 +01004015 if (ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER) {
4016 if (mbedtls_ssl_is_handshake_over(ssl) == 0) {
4017 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
4018 }
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01004019
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004020 ssl->renego_status = MBEDTLS_SSL_RENEGOTIATION_PENDING;
Manuel Pégourié-Gonnardf07f4212014-08-15 19:04:47 +02004021
4022 /* Did we already try/start sending HelloRequest? */
Gilles Peskine449bd832023-01-11 14:50:10 +01004023 if (ssl->out_left != 0) {
4024 return mbedtls_ssl_flush_output(ssl);
4025 }
Manuel Pégourié-Gonnardf07f4212014-08-15 19:04:47 +02004026
Gilles Peskine449bd832023-01-11 14:50:10 +01004027 return ssl_write_hello_request(ssl);
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01004028 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004029#endif /* MBEDTLS_SSL_SRV_C */
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01004030
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004031#if defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01004032 /*
4033 * On client, either start the renegotiation process or,
4034 * if already in progress, continue the handshake
4035 */
Gilles Peskine449bd832023-01-11 14:50:10 +01004036 if (ssl->renego_status != MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS) {
4037 if (mbedtls_ssl_is_handshake_over(ssl) == 0) {
4038 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01004039 }
Gilles Peskine449bd832023-01-11 14:50:10 +01004040
4041 if ((ret = mbedtls_ssl_start_renegotiation(ssl)) != 0) {
4042 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ssl_start_renegotiation", ret);
4043 return ret;
4044 }
4045 } else {
4046 if ((ret = mbedtls_ssl_handshake(ssl)) != 0) {
4047 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ssl_handshake", ret);
4048 return ret;
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01004049 }
4050 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004051#endif /* MBEDTLS_SSL_CLI_C */
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01004052
Gilles Peskine449bd832023-01-11 14:50:10 +01004053 return ret;
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01004054}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004055#endif /* MBEDTLS_SSL_RENEGOTIATION */
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01004056
Gilles Peskine449bd832023-01-11 14:50:10 +01004057void mbedtls_ssl_handshake_free(mbedtls_ssl_context *ssl)
Paul Bakker48916f92012-09-16 19:57:18 +00004058{
Gilles Peskine9b562d52018-04-25 20:32:43 +02004059 mbedtls_ssl_handshake_params *handshake = ssl->handshake;
4060
Gilles Peskine449bd832023-01-11 14:50:10 +01004061 if (handshake == NULL) {
Paul Bakkeraccaffe2014-06-26 13:37:14 +02004062 return;
Gilles Peskine449bd832023-01-11 14:50:10 +01004063 }
Paul Bakkeraccaffe2014-06-26 13:37:14 +02004064
Brett Warrene0edc842021-08-17 09:53:13 +01004065#if defined(MBEDTLS_ECP_C)
4066#if !defined(MBEDTLS_DEPRECATED_REMOVED)
Gilles Peskine449bd832023-01-11 14:50:10 +01004067 if (ssl->handshake->group_list_heap_allocated) {
4068 mbedtls_free((void *) handshake->group_list);
4069 }
Brett Warrene0edc842021-08-17 09:53:13 +01004070 handshake->group_list = NULL;
4071#endif /* MBEDTLS_DEPRECATED_REMOVED */
4072#endif /* MBEDTLS_ECP_C */
4073
Ronald Crone68ab4f2022-10-05 12:46:29 +02004074#if defined(MBEDTLS_SSL_HANDSHAKE_WITH_CERT_ENABLED)
Jerry Yuf017ee42022-01-12 15:49:48 +08004075#if !defined(MBEDTLS_DEPRECATED_REMOVED)
Gilles Peskine449bd832023-01-11 14:50:10 +01004076 if (ssl->handshake->sig_algs_heap_allocated) {
4077 mbedtls_free((void *) handshake->sig_algs);
4078 }
Jerry Yuf017ee42022-01-12 15:49:48 +08004079 handshake->sig_algs = NULL;
4080#endif /* MBEDTLS_DEPRECATED_REMOVED */
Xiaofei Baic234ecf2022-02-08 09:59:23 +00004081#if defined(MBEDTLS_SSL_PROTO_TLS1_3)
Gilles Peskine449bd832023-01-11 14:50:10 +01004082 if (ssl->handshake->certificate_request_context) {
4083 mbedtls_free((void *) handshake->certificate_request_context);
Xiaofei Baic234ecf2022-02-08 09:59:23 +00004084 }
4085#endif /* MBEDTLS_SSL_PROTO_TLS1_3 */
Ronald Crone68ab4f2022-10-05 12:46:29 +02004086#endif /* MBEDTLS_SSL_HANDSHAKE_WITH_CERT_ENABLED */
Jerry Yuf017ee42022-01-12 15:49:48 +08004087
Gilles Peskinedf13d5c2018-04-25 20:39:48 +02004088#if defined(MBEDTLS_SSL_ASYNC_PRIVATE)
Gilles Peskine449bd832023-01-11 14:50:10 +01004089 if (ssl->conf->f_async_cancel != NULL && handshake->async_in_progress != 0) {
4090 ssl->conf->f_async_cancel(ssl);
Gilles Peskinedf13d5c2018-04-25 20:39:48 +02004091 handshake->async_in_progress = 0;
4092 }
4093#endif /* MBEDTLS_SSL_ASYNC_PRIVATE */
4094
Andrzej Kurek25f27152022-08-17 16:09:31 -04004095#if defined(MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Andrzej Kurekeb342242019-01-29 09:14:33 -05004096#if defined(MBEDTLS_USE_PSA_CRYPTO)
Gilles Peskine449bd832023-01-11 14:50:10 +01004097 psa_hash_abort(&handshake->fin_sha256_psa);
Andrzej Kurekeb342242019-01-29 09:14:33 -05004098#else
Manuel Pégourié-Gonnardf057ecf2023-02-24 13:19:17 +01004099 mbedtls_md_free(&handshake->fin_sha256);
Manuel Pégourié-Gonnardb9d64e52015-07-06 14:18:56 +02004100#endif
Andrzej Kurekeb342242019-01-29 09:14:33 -05004101#endif
Andrzej Kurek25f27152022-08-17 16:09:31 -04004102#if defined(MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Andrzej Kurekeb342242019-01-29 09:14:33 -05004103#if defined(MBEDTLS_USE_PSA_CRYPTO)
Gilles Peskine449bd832023-01-11 14:50:10 +01004104 psa_hash_abort(&handshake->fin_sha384_psa);
Andrzej Kurekeb342242019-01-29 09:14:33 -05004105#else
Manuel Pégourié-Gonnardf057ecf2023-02-24 13:19:17 +01004106 mbedtls_md_free(&handshake->fin_sha384);
Manuel Pégourié-Gonnardb9d64e52015-07-06 14:18:56 +02004107#endif
Andrzej Kurekeb342242019-01-29 09:14:33 -05004108#endif
Manuel Pégourié-Gonnardb9d64e52015-07-06 14:18:56 +02004109
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004110#if defined(MBEDTLS_DHM_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01004111 mbedtls_dhm_free(&handshake->dhm_ctx);
Paul Bakker48916f92012-09-16 19:57:18 +00004112#endif
Neil Armstrongf3f46412022-04-12 14:43:39 +02004113#if !defined(MBEDTLS_USE_PSA_CRYPTO) && defined(MBEDTLS_ECDH_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01004114 mbedtls_ecdh_free(&handshake->ecdh_ctx);
Paul Bakker61d113b2013-07-04 11:51:43 +02004115#endif
Valerio Setti02c25b52022-11-15 14:08:42 +01004116
Manuel Pégourié-Gonnardeef142d2015-09-16 10:05:04 +02004117#if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
Neil Armstrongca7d5062022-05-31 14:43:23 +02004118#if defined(MBEDTLS_USE_PSA_CRYPTO)
Gilles Peskine449bd832023-01-11 14:50:10 +01004119 psa_pake_abort(&handshake->psa_pake_ctx);
Valerio Settieb3f7882022-12-08 18:42:58 +01004120 /*
4121 * Opaque keys are not stored in the handshake's data and it's the user
4122 * responsibility to destroy them. Clear ones, instead, are created by
4123 * the TLS library and should be destroyed at the same level
4124 */
Gilles Peskine449bd832023-01-11 14:50:10 +01004125 if (!mbedtls_svc_key_id_is_null(handshake->psa_pake_password)) {
4126 psa_destroy_key(handshake->psa_pake_password);
Valerio Settieb3f7882022-12-08 18:42:58 +01004127 }
Neil Armstrongca7d5062022-05-31 14:43:23 +02004128 handshake->psa_pake_password = MBEDTLS_SVC_KEY_ID_INIT;
4129#else
Gilles Peskine449bd832023-01-11 14:50:10 +01004130 mbedtls_ecjpake_free(&handshake->ecjpake_ctx);
Neil Armstrongca7d5062022-05-31 14:43:23 +02004131#endif /* MBEDTLS_USE_PSA_CRYPTO */
Manuel Pégourié-Gonnard77c06462015-09-17 13:59:49 +02004132#if defined(MBEDTLS_SSL_CLI_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01004133 mbedtls_free(handshake->ecjpake_cache);
Manuel Pégourié-Gonnard77c06462015-09-17 13:59:49 +02004134 handshake->ecjpake_cache = NULL;
4135 handshake->ecjpake_cache_len = 0;
4136#endif
Manuel Pégourié-Gonnard76cfd3f2015-09-15 12:10:54 +02004137#endif
Paul Bakker61d113b2013-07-04 11:51:43 +02004138
Janos Follath4ae5c292016-02-10 11:27:43 +00004139#if defined(MBEDTLS_ECDH_C) || defined(MBEDTLS_ECDSA_C) || \
4140 defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
Paul Bakker9af723c2014-05-01 13:03:14 +02004141 /* explicit void pointer cast for buggy MS compiler */
Gilles Peskine449bd832023-01-11 14:50:10 +01004142 mbedtls_free((void *) handshake->curves_tls_id);
Manuel Pégourié-Gonnardd09453c2013-09-23 19:11:32 +02004143#endif
4144
Ronald Cron73fe8df2022-10-05 14:31:43 +02004145#if defined(MBEDTLS_SSL_HANDSHAKE_WITH_PSK_ENABLED)
Neil Armstrong501c9322022-05-03 09:35:09 +02004146#if defined(MBEDTLS_USE_PSA_CRYPTO)
Gilles Peskine449bd832023-01-11 14:50:10 +01004147 if (!mbedtls_svc_key_id_is_null(ssl->handshake->psk_opaque)) {
Neil Armstrong501c9322022-05-03 09:35:09 +02004148 /* The maintenance of the external PSK key slot is the
4149 * user's responsibility. */
Gilles Peskine449bd832023-01-11 14:50:10 +01004150 if (ssl->handshake->psk_opaque_is_internal) {
4151 psa_destroy_key(ssl->handshake->psk_opaque);
Neil Armstrong501c9322022-05-03 09:35:09 +02004152 ssl->handshake->psk_opaque_is_internal = 0;
4153 }
4154 ssl->handshake->psk_opaque = MBEDTLS_SVC_KEY_ID_INIT;
4155 }
Neil Armstronge952a302022-05-03 10:22:14 +02004156#else
Gilles Peskine449bd832023-01-11 14:50:10 +01004157 if (handshake->psk != NULL) {
4158 mbedtls_platform_zeroize(handshake->psk, handshake->psk_len);
4159 mbedtls_free(handshake->psk);
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01004160 }
Neil Armstronge952a302022-05-03 10:22:14 +02004161#endif /* MBEDTLS_USE_PSA_CRYPTO */
Ronald Cron73fe8df2022-10-05 14:31:43 +02004162#endif /* MBEDTLS_SSL_HANDSHAKE_WITH_PSK_ENABLED */
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01004163
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004164#if defined(MBEDTLS_X509_CRT_PARSE_C) && \
4165 defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
Manuel Pégourié-Gonnard83724542013-09-24 22:30:56 +02004166 /*
4167 * Free only the linked list wrapper, not the keys themselves
4168 * since the belong to the SNI callback
4169 */
Gilles Peskine449bd832023-01-11 14:50:10 +01004170 ssl_key_cert_free(handshake->sni_key_cert);
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004171#endif /* MBEDTLS_X509_CRT_PARSE_C && MBEDTLS_SSL_SERVER_NAME_INDICATION */
Manuel Pégourié-Gonnard705fcca2013-09-23 20:04:20 +02004172
Gilles Peskineeccd8882020-03-10 12:19:08 +01004173#if defined(MBEDTLS_SSL_ECP_RESTARTABLE_ENABLED)
Gilles Peskine449bd832023-01-11 14:50:10 +01004174 mbedtls_x509_crt_restart_free(&handshake->ecrs_ctx);
4175 if (handshake->ecrs_peer_cert != NULL) {
4176 mbedtls_x509_crt_free(handshake->ecrs_peer_cert);
4177 mbedtls_free(handshake->ecrs_peer_cert);
Hanno Becker3dad3112019-02-05 17:19:52 +00004178 }
Manuel Pégourié-Gonnard862cde52017-05-17 11:56:15 +02004179#endif
4180
Hanno Becker75173122019-02-06 16:18:31 +00004181#if defined(MBEDTLS_X509_CRT_PARSE_C) && \
4182 !defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
Gilles Peskine449bd832023-01-11 14:50:10 +01004183 mbedtls_pk_free(&handshake->peer_pubkey);
Hanno Becker75173122019-02-06 16:18:31 +00004184#endif /* MBEDTLS_X509_CRT_PARSE_C && !MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
4185
XiaokangQian9b93c0d2022-02-09 06:02:25 +00004186#if defined(MBEDTLS_SSL_CLI_C) && \
Gilles Peskine449bd832023-01-11 14:50:10 +01004187 (defined(MBEDTLS_SSL_PROTO_DTLS) || defined(MBEDTLS_SSL_PROTO_TLS1_3))
4188 mbedtls_free(handshake->cookie);
XiaokangQian9b93c0d2022-02-09 06:02:25 +00004189#endif /* MBEDTLS_SSL_CLI_C &&
4190 ( MBEDTLS_SSL_PROTO_DTLS || MBEDTLS_SSL_PROTO_TLS1_3 ) */
XiaokangQian8499b6c2022-01-27 09:00:11 +00004191
4192#if defined(MBEDTLS_SSL_PROTO_DTLS)
Gilles Peskine449bd832023-01-11 14:50:10 +01004193 mbedtls_ssl_flight_free(handshake->flight);
4194 mbedtls_ssl_buffering_free(ssl);
XiaokangQian8499b6c2022-01-27 09:00:11 +00004195#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnard74848812014-07-11 02:43:49 +02004196
Ronald Cronf12b81d2022-03-15 10:42:41 +01004197#if defined(MBEDTLS_ECDH_C) && \
Gilles Peskine449bd832023-01-11 14:50:10 +01004198 (defined(MBEDTLS_USE_PSA_CRYPTO) || defined(MBEDTLS_SSL_PROTO_TLS1_3))
4199 if (handshake->ecdh_psa_privkey_is_external == 0) {
4200 psa_destroy_key(handshake->ecdh_psa_privkey);
4201 }
Hanno Becker4a63ed42019-01-08 11:39:35 +00004202#endif /* MBEDTLS_ECDH_C && MBEDTLS_USE_PSA_CRYPTO */
4203
Ronald Cron6f135e12021-12-08 16:57:54 +01004204#if defined(MBEDTLS_SSL_PROTO_TLS1_3)
Gilles Peskine449bd832023-01-11 14:50:10 +01004205 mbedtls_ssl_transform_free(handshake->transform_handshake);
4206 mbedtls_free(handshake->transform_handshake);
Jerry Yu3d9b5902022-11-04 14:07:25 +08004207#if defined(MBEDTLS_SSL_EARLY_DATA)
Gilles Peskine449bd832023-01-11 14:50:10 +01004208 mbedtls_ssl_transform_free(handshake->transform_earlydata);
4209 mbedtls_free(handshake->transform_earlydata);
Jerry Yu3d9b5902022-11-04 14:07:25 +08004210#endif
Ronald Cron6f135e12021-12-08 16:57:54 +01004211#endif /* MBEDTLS_SSL_PROTO_TLS1_3 */
Jerry Yuba9c7272021-10-30 11:54:10 +08004212
Andrzej Kurek0afa2a12020-03-03 10:39:58 -05004213
4214#if defined(MBEDTLS_SSL_VARIABLE_BUFFER_LENGTH)
4215 /* If the buffers are too big - reallocate. Because of the way Mbed TLS
4216 * processes datagrams and the fact that a datagram is allowed to have
4217 * several records in it, it is possible that the I/O buffers are not
4218 * empty at this stage */
Gilles Peskine449bd832023-01-11 14:50:10 +01004219 handle_buffer_resizing(ssl, 1, mbedtls_ssl_get_input_buflen(ssl),
4220 mbedtls_ssl_get_output_buflen(ssl));
Andrzej Kurek0afa2a12020-03-03 10:39:58 -05004221#endif
Hanno Becker3aa186f2021-08-10 09:24:19 +01004222
Jerry Yuba9c7272021-10-30 11:54:10 +08004223 /* mbedtls_platform_zeroize MUST be last one in this function */
Gilles Peskine449bd832023-01-11 14:50:10 +01004224 mbedtls_platform_zeroize(handshake,
4225 sizeof(mbedtls_ssl_handshake_params));
Paul Bakker48916f92012-09-16 19:57:18 +00004226}
4227
Gilles Peskine449bd832023-01-11 14:50:10 +01004228void mbedtls_ssl_session_free(mbedtls_ssl_session *session)
Paul Bakker48916f92012-09-16 19:57:18 +00004229{
Gilles Peskine449bd832023-01-11 14:50:10 +01004230 if (session == NULL) {
Paul Bakkeraccaffe2014-06-26 13:37:14 +02004231 return;
Gilles Peskine449bd832023-01-11 14:50:10 +01004232 }
Paul Bakkeraccaffe2014-06-26 13:37:14 +02004233
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004234#if defined(MBEDTLS_X509_CRT_PARSE_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01004235 ssl_clear_peer_cert(session);
Paul Bakkered27a042013-04-18 22:46:23 +02004236#endif
Paul Bakker0a597072012-09-25 21:55:46 +00004237
Manuel Pégourié-Gonnardb596abf2015-05-20 10:45:29 +02004238#if defined(MBEDTLS_SSL_SESSION_TICKETS) && defined(MBEDTLS_SSL_CLI_C)
Xiaokang Qianbc663a02022-10-09 11:14:39 +00004239#if defined(MBEDTLS_SSL_PROTO_TLS1_3) && \
4240 defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
Gilles Peskine449bd832023-01-11 14:50:10 +01004241 mbedtls_free(session->hostname);
Xiaokang Qian281fd1b2022-09-20 11:35:41 +00004242#endif
Gilles Peskine449bd832023-01-11 14:50:10 +01004243 mbedtls_free(session->ticket);
Paul Bakkera503a632013-08-14 13:48:06 +02004244#endif
Manuel Pégourié-Gonnard75d44012013-08-02 14:44:04 +02004245
Gilles Peskine449bd832023-01-11 14:50:10 +01004246 mbedtls_platform_zeroize(session, sizeof(mbedtls_ssl_session));
Paul Bakker48916f92012-09-16 19:57:18 +00004247}
4248
Manuel Pégourié-Gonnard5c0e3772019-07-23 16:13:17 +02004249#if defined(MBEDTLS_SSL_CONTEXT_SERIALIZATION)
Manuel Pégourié-Gonnard4e9370b2019-07-23 16:31:16 +02004250
4251#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
4252#define SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_CONNECTION_ID 1u
4253#else
4254#define SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_CONNECTION_ID 0u
4255#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
4256
Manuel Pégourié-Gonnard4e9370b2019-07-23 16:31:16 +02004257#define SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_BADMAC_LIMIT 1u
Manuel Pégourié-Gonnard4e9370b2019-07-23 16:31:16 +02004258
4259#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
4260#define SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_ANTI_REPLAY 1u
4261#else
4262#define SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_ANTI_REPLAY 0u
4263#endif /* MBEDTLS_SSL_DTLS_ANTI_REPLAY */
4264
4265#if defined(MBEDTLS_SSL_ALPN)
4266#define SSL_SERIALIZED_CONTEXT_CONFIG_ALPN 1u
4267#else
4268#define SSL_SERIALIZED_CONTEXT_CONFIG_ALPN 0u
4269#endif /* MBEDTLS_SSL_ALPN */
4270
4271#define SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_CONNECTION_ID_BIT 0
4272#define SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_BADMAC_LIMIT_BIT 1
4273#define SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_ANTI_REPLAY_BIT 2
4274#define SSL_SERIALIZED_CONTEXT_CONFIG_ALPN_BIT 3
4275
4276#define SSL_SERIALIZED_CONTEXT_CONFIG_BITFLAG \
Gilles Peskine449bd832023-01-11 14:50:10 +01004277 ((uint32_t) ( \
4278 (SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_CONNECTION_ID << \
4279 SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_CONNECTION_ID_BIT) | \
4280 (SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_BADMAC_LIMIT << \
4281 SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_BADMAC_LIMIT_BIT) | \
4282 (SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_ANTI_REPLAY << \
4283 SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_ANTI_REPLAY_BIT) | \
4284 (SSL_SERIALIZED_CONTEXT_CONFIG_ALPN << SSL_SERIALIZED_CONTEXT_CONFIG_ALPN_BIT) | \
4285 0u))
Manuel Pégourié-Gonnard4e9370b2019-07-23 16:31:16 +02004286
Manuel Pégourié-Gonnard4c90e852019-07-11 10:58:10 +02004287static unsigned char ssl_serialized_context_header[] = {
4288 MBEDTLS_VERSION_MAJOR,
4289 MBEDTLS_VERSION_MINOR,
4290 MBEDTLS_VERSION_PATCH,
Gilles Peskine449bd832023-01-11 14:50:10 +01004291 MBEDTLS_BYTE_1(SSL_SERIALIZED_SESSION_CONFIG_BITFLAG),
4292 MBEDTLS_BYTE_0(SSL_SERIALIZED_SESSION_CONFIG_BITFLAG),
4293 MBEDTLS_BYTE_2(SSL_SERIALIZED_CONTEXT_CONFIG_BITFLAG),
4294 MBEDTLS_BYTE_1(SSL_SERIALIZED_CONTEXT_CONFIG_BITFLAG),
4295 MBEDTLS_BYTE_0(SSL_SERIALIZED_CONTEXT_CONFIG_BITFLAG),
Manuel Pégourié-Gonnard4c90e852019-07-11 10:58:10 +02004296};
4297
Paul Bakker5121ce52009-01-03 21:22:43 +00004298/*
Manuel Pégourié-Gonnardac87e282019-05-28 13:02:16 +02004299 * Serialize a full SSL context
Manuel Pégourié-Gonnard00400c22019-07-10 14:58:45 +02004300 *
4301 * The format of the serialized data is:
4302 * (in the presentation language of TLS, RFC 8446 section 3)
4303 *
4304 * // header
4305 * opaque mbedtls_version[3]; // major, minor, patch
Manuel Pégourié-Gonnard4c90e852019-07-11 10:58:10 +02004306 * opaque context_format[5]; // version-specific field determining
Manuel Pégourié-Gonnard00400c22019-07-10 14:58:45 +02004307 * // the format of the remaining
Manuel Pégourié-Gonnard4c90e852019-07-11 10:58:10 +02004308 * // serialized data.
Manuel Pégourié-Gonnard4e9370b2019-07-23 16:31:16 +02004309 * Note: When updating the format, remember to keep these
4310 * version+format bytes. (We may make their size part of the API.)
Manuel Pégourié-Gonnard00400c22019-07-10 14:58:45 +02004311 *
4312 * // session sub-structure
4313 * opaque session<1..2^32-1>; // see mbedtls_ssl_session_save()
4314 * // transform sub-structure
4315 * uint8 random[64]; // ServerHello.random+ClientHello.random
4316 * uint8 in_cid<0..2^8-1> // Connection ID: expected incoming value
4317 * uint8 out_cid<0..2^8-1> // Connection ID: outgoing value to use
4318 * // fields from ssl_context
4319 * uint32 badmac_seen; // DTLS: number of records with failing MAC
4320 * uint64 in_window_top; // DTLS: last validated record seq_num
4321 * uint64 in_window; // DTLS: bitmask for replay protection
4322 * uint8 disable_datagram_packing; // DTLS: only one record per datagram
4323 * uint64 cur_out_ctr; // Record layer: outgoing sequence number
4324 * uint16 mtu; // DTLS: path mtu (max outgoing fragment size)
4325 * uint8 alpn_chosen<0..2^8-1> // ALPN: negotiated application protocol
4326 *
4327 * Note that many fields of the ssl_context or sub-structures are not
4328 * serialized, as they fall in one of the following categories:
4329 *
4330 * 1. forced value (eg in_left must be 0)
4331 * 2. pointer to dynamically-allocated memory (eg session, transform)
4332 * 3. value can be re-derived from other data (eg session keys from MS)
4333 * 4. value was temporary (eg content of input buffer)
4334 * 5. value will be provided by the user again (eg I/O callbacks and context)
Manuel Pégourié-Gonnardac87e282019-05-28 13:02:16 +02004335 */
Gilles Peskine449bd832023-01-11 14:50:10 +01004336int mbedtls_ssl_context_save(mbedtls_ssl_context *ssl,
4337 unsigned char *buf,
4338 size_t buf_len,
4339 size_t *olen)
Manuel Pégourié-Gonnardac87e282019-05-28 13:02:16 +02004340{
Manuel Pégourié-Gonnard4c90e852019-07-11 10:58:10 +02004341 unsigned char *p = buf;
4342 size_t used = 0;
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02004343 size_t session_len;
4344 int ret = 0;
Manuel Pégourié-Gonnard4c90e852019-07-11 10:58:10 +02004345
Manuel Pégourié-Gonnard1aaf6692019-07-10 14:14:05 +02004346 /*
Manuel Pégourié-Gonnarde4588692019-07-29 12:28:52 +02004347 * Enforce usage restrictions, see "return BAD_INPUT_DATA" in
4348 * this function's documentation.
4349 *
4350 * These are due to assumptions/limitations in the implementation. Some of
4351 * them are likely to stay (no handshake in progress) some might go away
4352 * (only DTLS) but are currently used to simplify the implementation.
Manuel Pégourié-Gonnard1aaf6692019-07-10 14:14:05 +02004353 */
Manuel Pégourié-Gonnarde4588692019-07-29 12:28:52 +02004354 /* The initial handshake must be over */
Gilles Peskine449bd832023-01-11 14:50:10 +01004355 if (mbedtls_ssl_is_handshake_over(ssl) == 0) {
4356 MBEDTLS_SSL_DEBUG_MSG(1, ("Initial handshake isn't over"));
4357 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
Jarno Lamsa8c51b7c2019-08-21 13:45:05 +03004358 }
Gilles Peskine449bd832023-01-11 14:50:10 +01004359 if (ssl->handshake != NULL) {
4360 MBEDTLS_SSL_DEBUG_MSG(1, ("Handshake isn't completed"));
4361 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
Jarno Lamsa8c51b7c2019-08-21 13:45:05 +03004362 }
Manuel Pégourié-Gonnarde4588692019-07-29 12:28:52 +02004363 /* Double-check that sub-structures are indeed ready */
Gilles Peskine449bd832023-01-11 14:50:10 +01004364 if (ssl->transform == NULL || ssl->session == NULL) {
4365 MBEDTLS_SSL_DEBUG_MSG(1, ("Serialised structures aren't ready"));
4366 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
Jarno Lamsa8c51b7c2019-08-21 13:45:05 +03004367 }
Manuel Pégourié-Gonnarde4588692019-07-29 12:28:52 +02004368 /* There must be no pending incoming or outgoing data */
Gilles Peskine449bd832023-01-11 14:50:10 +01004369 if (mbedtls_ssl_check_pending(ssl) != 0) {
4370 MBEDTLS_SSL_DEBUG_MSG(1, ("There is pending incoming data"));
4371 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
Jarno Lamsa8c51b7c2019-08-21 13:45:05 +03004372 }
Gilles Peskine449bd832023-01-11 14:50:10 +01004373 if (ssl->out_left != 0) {
4374 MBEDTLS_SSL_DEBUG_MSG(1, ("There is pending outgoing data"));
4375 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
Jarno Lamsa8c51b7c2019-08-21 13:45:05 +03004376 }
Dave Rodgman556e8a32022-12-06 16:31:25 +00004377 /* Protocol must be DTLS, not TLS */
Gilles Peskine449bd832023-01-11 14:50:10 +01004378 if (ssl->conf->transport != MBEDTLS_SSL_TRANSPORT_DATAGRAM) {
4379 MBEDTLS_SSL_DEBUG_MSG(1, ("Only DTLS is supported"));
4380 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
Jarno Lamsa8c51b7c2019-08-21 13:45:05 +03004381 }
Manuel Pégourié-Gonnarde4588692019-07-29 12:28:52 +02004382 /* Version must be 1.2 */
Gilles Peskine449bd832023-01-11 14:50:10 +01004383 if (ssl->tls_version != MBEDTLS_SSL_VERSION_TLS1_2) {
4384 MBEDTLS_SSL_DEBUG_MSG(1, ("Only version 1.2 supported"));
4385 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
Jarno Lamsa8c51b7c2019-08-21 13:45:05 +03004386 }
Manuel Pégourié-Gonnarde4588692019-07-29 12:28:52 +02004387 /* We must be using an AEAD ciphersuite */
Gilles Peskine449bd832023-01-11 14:50:10 +01004388 if (mbedtls_ssl_transform_uses_aead(ssl->transform) != 1) {
4389 MBEDTLS_SSL_DEBUG_MSG(1, ("Only AEAD ciphersuites supported"));
4390 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
Jarno Lamsa8c51b7c2019-08-21 13:45:05 +03004391 }
Manuel Pégourié-Gonnarde4588692019-07-29 12:28:52 +02004392 /* Renegotiation must not be enabled */
4393#if defined(MBEDTLS_SSL_RENEGOTIATION)
Gilles Peskine449bd832023-01-11 14:50:10 +01004394 if (ssl->conf->disable_renegotiation != MBEDTLS_SSL_RENEGOTIATION_DISABLED) {
4395 MBEDTLS_SSL_DEBUG_MSG(1, ("Renegotiation must not be enabled"));
4396 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
Jarno Lamsa8c51b7c2019-08-21 13:45:05 +03004397 }
Manuel Pégourié-Gonnarde4588692019-07-29 12:28:52 +02004398#endif
Manuel Pégourié-Gonnardac87e282019-05-28 13:02:16 +02004399
Manuel Pégourié-Gonnard4c90e852019-07-11 10:58:10 +02004400 /*
4401 * Version and format identifier
4402 */
Gilles Peskine449bd832023-01-11 14:50:10 +01004403 used += sizeof(ssl_serialized_context_header);
Manuel Pégourié-Gonnardac87e282019-05-28 13:02:16 +02004404
Gilles Peskine449bd832023-01-11 14:50:10 +01004405 if (used <= buf_len) {
4406 memcpy(p, ssl_serialized_context_header,
4407 sizeof(ssl_serialized_context_header));
4408 p += sizeof(ssl_serialized_context_header);
Manuel Pégourié-Gonnard4c90e852019-07-11 10:58:10 +02004409 }
4410
4411 /*
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02004412 * Session (length + data)
4413 */
Gilles Peskine449bd832023-01-11 14:50:10 +01004414 ret = ssl_session_save(ssl->session, 1, NULL, 0, &session_len);
4415 if (ret != MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL) {
4416 return ret;
4417 }
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02004418
4419 used += 4 + session_len;
Gilles Peskine449bd832023-01-11 14:50:10 +01004420 if (used <= buf_len) {
4421 MBEDTLS_PUT_UINT32_BE(session_len, p, 0);
Joe Subbiani1f6c3ae2021-08-20 11:44:44 +01004422 p += 4;
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02004423
Gilles Peskine449bd832023-01-11 14:50:10 +01004424 ret = ssl_session_save(ssl->session, 1,
4425 p, session_len, &session_len);
4426 if (ret != 0) {
4427 return ret;
4428 }
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02004429
4430 p += session_len;
4431 }
4432
4433 /*
Manuel Pégourié-Gonnardc2a7b892019-07-15 09:04:11 +02004434 * Transform
4435 */
Gilles Peskine449bd832023-01-11 14:50:10 +01004436 used += sizeof(ssl->transform->randbytes);
4437 if (used <= buf_len) {
4438 memcpy(p, ssl->transform->randbytes,
4439 sizeof(ssl->transform->randbytes));
4440 p += sizeof(ssl->transform->randbytes);
Manuel Pégourié-Gonnardc2a7b892019-07-15 09:04:11 +02004441 }
4442
4443#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
4444 used += 2 + ssl->transform->in_cid_len + ssl->transform->out_cid_len;
Gilles Peskine449bd832023-01-11 14:50:10 +01004445 if (used <= buf_len) {
Manuel Pégourié-Gonnardc2a7b892019-07-15 09:04:11 +02004446 *p++ = ssl->transform->in_cid_len;
Gilles Peskine449bd832023-01-11 14:50:10 +01004447 memcpy(p, ssl->transform->in_cid, ssl->transform->in_cid_len);
Manuel Pégourié-Gonnardc2a7b892019-07-15 09:04:11 +02004448 p += ssl->transform->in_cid_len;
4449
4450 *p++ = ssl->transform->out_cid_len;
Gilles Peskine449bd832023-01-11 14:50:10 +01004451 memcpy(p, ssl->transform->out_cid, ssl->transform->out_cid_len);
Manuel Pégourié-Gonnardc2a7b892019-07-15 09:04:11 +02004452 p += ssl->transform->out_cid_len;
4453 }
4454#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
4455
4456 /*
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02004457 * Saved fields from top-level ssl_context structure
4458 */
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02004459 used += 4;
Gilles Peskine449bd832023-01-11 14:50:10 +01004460 if (used <= buf_len) {
4461 MBEDTLS_PUT_UINT32_BE(ssl->badmac_seen, p, 0);
Joe Subbiani1f6c3ae2021-08-20 11:44:44 +01004462 p += 4;
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02004463 }
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02004464
4465#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
4466 used += 16;
Gilles Peskine449bd832023-01-11 14:50:10 +01004467 if (used <= buf_len) {
4468 MBEDTLS_PUT_UINT64_BE(ssl->in_window_top, p, 0);
Joe Subbiani1f6c3ae2021-08-20 11:44:44 +01004469 p += 8;
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02004470
Gilles Peskine449bd832023-01-11 14:50:10 +01004471 MBEDTLS_PUT_UINT64_BE(ssl->in_window, p, 0);
Joe Subbiani1f6c3ae2021-08-20 11:44:44 +01004472 p += 8;
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02004473 }
4474#endif /* MBEDTLS_SSL_DTLS_ANTI_REPLAY */
4475
4476#if defined(MBEDTLS_SSL_PROTO_DTLS)
4477 used += 1;
Gilles Peskine449bd832023-01-11 14:50:10 +01004478 if (used <= buf_len) {
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02004479 *p++ = ssl->disable_datagram_packing;
4480 }
4481#endif /* MBEDTLS_SSL_PROTO_DTLS */
4482
Jerry Yuae0b2e22021-10-08 15:21:19 +08004483 used += MBEDTLS_SSL_SEQUENCE_NUMBER_LEN;
Gilles Peskine449bd832023-01-11 14:50:10 +01004484 if (used <= buf_len) {
4485 memcpy(p, ssl->cur_out_ctr, MBEDTLS_SSL_SEQUENCE_NUMBER_LEN);
Jerry Yuae0b2e22021-10-08 15:21:19 +08004486 p += MBEDTLS_SSL_SEQUENCE_NUMBER_LEN;
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02004487 }
4488
4489#if defined(MBEDTLS_SSL_PROTO_DTLS)
4490 used += 2;
Gilles Peskine449bd832023-01-11 14:50:10 +01004491 if (used <= buf_len) {
4492 MBEDTLS_PUT_UINT16_BE(ssl->mtu, p, 0);
Joe Subbiani1f6c3ae2021-08-20 11:44:44 +01004493 p += 2;
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02004494 }
4495#endif /* MBEDTLS_SSL_PROTO_DTLS */
4496
4497#if defined(MBEDTLS_SSL_ALPN)
4498 {
4499 const uint8_t alpn_len = ssl->alpn_chosen
Gilles Peskine449bd832023-01-11 14:50:10 +01004500 ? (uint8_t) strlen(ssl->alpn_chosen)
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02004501 : 0;
4502
4503 used += 1 + alpn_len;
Gilles Peskine449bd832023-01-11 14:50:10 +01004504 if (used <= buf_len) {
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02004505 *p++ = alpn_len;
4506
Gilles Peskine449bd832023-01-11 14:50:10 +01004507 if (ssl->alpn_chosen != NULL) {
4508 memcpy(p, ssl->alpn_chosen, alpn_len);
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02004509 p += alpn_len;
4510 }
4511 }
4512 }
4513#endif /* MBEDTLS_SSL_ALPN */
4514
4515 /*
Manuel Pégourié-Gonnard4c90e852019-07-11 10:58:10 +02004516 * Done
4517 */
4518 *olen = used;
4519
Gilles Peskine449bd832023-01-11 14:50:10 +01004520 if (used > buf_len) {
4521 return MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL;
4522 }
Manuel Pégourié-Gonnardac87e282019-05-28 13:02:16 +02004523
Gilles Peskine449bd832023-01-11 14:50:10 +01004524 MBEDTLS_SSL_DEBUG_BUF(4, "saved context", buf, used);
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02004525
Gilles Peskine449bd832023-01-11 14:50:10 +01004526 return mbedtls_ssl_session_reset_int(ssl, 0);
Manuel Pégourié-Gonnardac87e282019-05-28 13:02:16 +02004527}
4528
4529/*
Manuel Pégourié-Gonnardb9dfc9f2019-07-12 10:50:19 +02004530 * Deserialize context, see mbedtls_ssl_context_save() for format.
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02004531 *
4532 * This internal version is wrapped by a public function that cleans up in
4533 * case of error.
Manuel Pégourié-Gonnardac87e282019-05-28 13:02:16 +02004534 */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02004535MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01004536static int ssl_context_load(mbedtls_ssl_context *ssl,
4537 const unsigned char *buf,
4538 size_t len)
Manuel Pégourié-Gonnardac87e282019-05-28 13:02:16 +02004539{
Manuel Pégourié-Gonnard4c90e852019-07-11 10:58:10 +02004540 const unsigned char *p = buf;
4541 const unsigned char * const end = buf + len;
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02004542 size_t session_len;
Janos Follath865b3eb2019-12-16 11:46:15 +00004543 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Andrzej Kurek2d59dbc2022-10-13 08:34:38 -04004544#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
Andrzej Kurek894edde2022-09-29 06:31:14 -04004545 tls_prf_fn prf_func = NULL;
Andrzej Kurek2d59dbc2022-10-13 08:34:38 -04004546#endif
Manuel Pégourié-Gonnard4c90e852019-07-11 10:58:10 +02004547
Manuel Pégourié-Gonnard0ff76402019-07-11 09:56:30 +02004548 /*
4549 * The context should have been freshly setup or reset.
4550 * Give the user an error in case of obvious misuse.
Manuel Pégourié-Gonnard4ca930f2019-07-26 16:31:53 +02004551 * (Checking session is useful because it won't be NULL if we're
Manuel Pégourié-Gonnard0ff76402019-07-11 09:56:30 +02004552 * renegotiating, or if the user mistakenly loaded a session first.)
4553 */
Gilles Peskine449bd832023-01-11 14:50:10 +01004554 if (ssl->state != MBEDTLS_SSL_HELLO_REQUEST ||
4555 ssl->session != NULL) {
4556 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
Manuel Pégourié-Gonnard0ff76402019-07-11 09:56:30 +02004557 }
4558
4559 /*
4560 * We can't check that the config matches the initial one, but we can at
4561 * least check it matches the requirements for serializing.
4562 */
Gilles Peskine449bd832023-01-11 14:50:10 +01004563 if (ssl->conf->transport != MBEDTLS_SSL_TRANSPORT_DATAGRAM ||
Glenn Strauss2dfcea22022-03-14 17:26:42 -04004564 ssl->conf->max_tls_version < MBEDTLS_SSL_VERSION_TLS1_2 ||
4565 ssl->conf->min_tls_version > MBEDTLS_SSL_VERSION_TLS1_2 ||
Manuel Pégourié-Gonnard0ff76402019-07-11 09:56:30 +02004566#if defined(MBEDTLS_SSL_RENEGOTIATION)
Manuel Pégourié-Gonnard9a96fd72019-07-23 17:11:24 +02004567 ssl->conf->disable_renegotiation != MBEDTLS_SSL_RENEGOTIATION_DISABLED ||
Manuel Pégourié-Gonnard0ff76402019-07-11 09:56:30 +02004568#endif
Gilles Peskine449bd832023-01-11 14:50:10 +01004569 0) {
4570 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
Manuel Pégourié-Gonnard0ff76402019-07-11 09:56:30 +02004571 }
4572
Gilles Peskine449bd832023-01-11 14:50:10 +01004573 MBEDTLS_SSL_DEBUG_BUF(4, "context to load", buf, len);
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02004574
Manuel Pégourié-Gonnard4c90e852019-07-11 10:58:10 +02004575 /*
4576 * Check version identifier
4577 */
Gilles Peskine449bd832023-01-11 14:50:10 +01004578 if ((size_t) (end - p) < sizeof(ssl_serialized_context_header)) {
4579 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
Manuel Pégourié-Gonnard4c90e852019-07-11 10:58:10 +02004580 }
Gilles Peskine449bd832023-01-11 14:50:10 +01004581
4582 if (memcmp(p, ssl_serialized_context_header,
4583 sizeof(ssl_serialized_context_header)) != 0) {
4584 return MBEDTLS_ERR_SSL_VERSION_MISMATCH;
4585 }
4586 p += sizeof(ssl_serialized_context_header);
Manuel Pégourié-Gonnard4c90e852019-07-11 10:58:10 +02004587
4588 /*
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02004589 * Session
4590 */
Gilles Peskine449bd832023-01-11 14:50:10 +01004591 if ((size_t) (end - p) < 4) {
4592 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
4593 }
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02004594
Gilles Peskine449bd832023-01-11 14:50:10 +01004595 session_len = ((size_t) p[0] << 24) |
4596 ((size_t) p[1] << 16) |
4597 ((size_t) p[2] << 8) |
4598 ((size_t) p[3]);
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02004599 p += 4;
4600
Manuel Pégourié-Gonnard142ba732019-07-23 14:43:30 +02004601 /* This has been allocated by ssl_handshake_init(), called by
Hanno Becker43aefe22020-02-05 10:44:56 +00004602 * by either mbedtls_ssl_session_reset_int() or mbedtls_ssl_setup(). */
Manuel Pégourié-Gonnard142ba732019-07-23 14:43:30 +02004603 ssl->session = ssl->session_negotiate;
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02004604 ssl->session_in = ssl->session;
4605 ssl->session_out = ssl->session;
Manuel Pégourié-Gonnard142ba732019-07-23 14:43:30 +02004606 ssl->session_negotiate = NULL;
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02004607
Gilles Peskine449bd832023-01-11 14:50:10 +01004608 if ((size_t) (end - p) < session_len) {
4609 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
4610 }
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02004611
Gilles Peskine449bd832023-01-11 14:50:10 +01004612 ret = ssl_session_load(ssl->session, 1, p, session_len);
4613 if (ret != 0) {
4614 mbedtls_ssl_session_free(ssl->session);
4615 return ret;
Manuel Pégourié-Gonnard45ac1f02019-07-23 16:52:45 +02004616 }
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02004617
4618 p += session_len;
4619
4620 /*
Manuel Pégourié-Gonnardc2a7b892019-07-15 09:04:11 +02004621 * Transform
4622 */
4623
Manuel Pégourié-Gonnard142ba732019-07-23 14:43:30 +02004624 /* This has been allocated by ssl_handshake_init(), called by
Hanno Becker43aefe22020-02-05 10:44:56 +00004625 * by either mbedtls_ssl_session_reset_int() or mbedtls_ssl_setup(). */
Jerry Yu2e199812022-12-01 18:57:19 +08004626#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
Manuel Pégourié-Gonnard142ba732019-07-23 14:43:30 +02004627 ssl->transform = ssl->transform_negotiate;
Manuel Pégourié-Gonnardc2a7b892019-07-15 09:04:11 +02004628 ssl->transform_in = ssl->transform;
4629 ssl->transform_out = ssl->transform;
Manuel Pégourié-Gonnard142ba732019-07-23 14:43:30 +02004630 ssl->transform_negotiate = NULL;
Jerry Yu2e199812022-12-01 18:57:19 +08004631#endif
Manuel Pégourié-Gonnardc2a7b892019-07-15 09:04:11 +02004632
Andrzej Kurek2d59dbc2022-10-13 08:34:38 -04004633#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
Gilles Peskine449bd832023-01-11 14:50:10 +01004634 prf_func = ssl_tls12prf_from_cs(ssl->session->ciphersuite);
4635 if (prf_func == NULL) {
4636 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
4637 }
Andrzej Kurek894edde2022-09-29 06:31:14 -04004638
Manuel Pégourié-Gonnardc2a7b892019-07-15 09:04:11 +02004639 /* Read random bytes and populate structure */
Gilles Peskine449bd832023-01-11 14:50:10 +01004640 if ((size_t) (end - p) < sizeof(ssl->transform->randbytes)) {
4641 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
4642 }
Andrzej Kurek2d59dbc2022-10-13 08:34:38 -04004643
Gilles Peskine449bd832023-01-11 14:50:10 +01004644 ret = ssl_tls12_populate_transform(ssl->transform,
4645 ssl->session->ciphersuite,
4646 ssl->session->master,
Neil Armstrongf2c82f02022-04-05 11:16:53 +02004647#if defined(MBEDTLS_SSL_SOME_SUITES_USE_CBC_ETM)
Gilles Peskine449bd832023-01-11 14:50:10 +01004648 ssl->session->encrypt_then_mac,
Neil Armstrongf2c82f02022-04-05 11:16:53 +02004649#endif /* MBEDTLS_SSL_SOME_SUITES_USE_CBC_ETM */
Gilles Peskine449bd832023-01-11 14:50:10 +01004650 prf_func,
4651 p, /* currently pointing to randbytes */
4652 MBEDTLS_SSL_VERSION_TLS1_2, /* (D)TLS 1.2 is forced */
4653 ssl->conf->endpoint,
4654 ssl);
4655 if (ret != 0) {
4656 return ret;
4657 }
Jerry Yu840fbb22022-02-17 14:59:29 +08004658#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Gilles Peskine449bd832023-01-11 14:50:10 +01004659 p += sizeof(ssl->transform->randbytes);
Manuel Pégourié-Gonnardc2a7b892019-07-15 09:04:11 +02004660
4661#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
4662 /* Read connection IDs and store them */
Gilles Peskine449bd832023-01-11 14:50:10 +01004663 if ((size_t) (end - p) < 1) {
4664 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
4665 }
Manuel Pégourié-Gonnardc2a7b892019-07-15 09:04:11 +02004666
4667 ssl->transform->in_cid_len = *p++;
4668
Gilles Peskine449bd832023-01-11 14:50:10 +01004669 if ((size_t) (end - p) < ssl->transform->in_cid_len + 1u) {
4670 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
4671 }
Manuel Pégourié-Gonnardc2a7b892019-07-15 09:04:11 +02004672
Gilles Peskine449bd832023-01-11 14:50:10 +01004673 memcpy(ssl->transform->in_cid, p, ssl->transform->in_cid_len);
Manuel Pégourié-Gonnardc2a7b892019-07-15 09:04:11 +02004674 p += ssl->transform->in_cid_len;
4675
4676 ssl->transform->out_cid_len = *p++;
4677
Gilles Peskine449bd832023-01-11 14:50:10 +01004678 if ((size_t) (end - p) < ssl->transform->out_cid_len) {
4679 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
4680 }
Manuel Pégourié-Gonnardc2a7b892019-07-15 09:04:11 +02004681
Gilles Peskine449bd832023-01-11 14:50:10 +01004682 memcpy(ssl->transform->out_cid, p, ssl->transform->out_cid_len);
Manuel Pégourié-Gonnardc2a7b892019-07-15 09:04:11 +02004683 p += ssl->transform->out_cid_len;
4684#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
4685
4686 /*
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02004687 * Saved fields from top-level ssl_context structure
4688 */
Gilles Peskine449bd832023-01-11 14:50:10 +01004689 if ((size_t) (end - p) < 4) {
4690 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
4691 }
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02004692
Gilles Peskine449bd832023-01-11 14:50:10 +01004693 ssl->badmac_seen = ((uint32_t) p[0] << 24) |
4694 ((uint32_t) p[1] << 16) |
4695 ((uint32_t) p[2] << 8) |
4696 ((uint32_t) p[3]);
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02004697 p += 4;
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02004698
4699#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
Gilles Peskine449bd832023-01-11 14:50:10 +01004700 if ((size_t) (end - p) < 16) {
4701 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
4702 }
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02004703
Gilles Peskine449bd832023-01-11 14:50:10 +01004704 ssl->in_window_top = ((uint64_t) p[0] << 56) |
4705 ((uint64_t) p[1] << 48) |
4706 ((uint64_t) p[2] << 40) |
4707 ((uint64_t) p[3] << 32) |
4708 ((uint64_t) p[4] << 24) |
4709 ((uint64_t) p[5] << 16) |
4710 ((uint64_t) p[6] << 8) |
4711 ((uint64_t) p[7]);
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02004712 p += 8;
4713
Gilles Peskine449bd832023-01-11 14:50:10 +01004714 ssl->in_window = ((uint64_t) p[0] << 56) |
4715 ((uint64_t) p[1] << 48) |
4716 ((uint64_t) p[2] << 40) |
4717 ((uint64_t) p[3] << 32) |
4718 ((uint64_t) p[4] << 24) |
4719 ((uint64_t) p[5] << 16) |
4720 ((uint64_t) p[6] << 8) |
4721 ((uint64_t) p[7]);
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02004722 p += 8;
4723#endif /* MBEDTLS_SSL_DTLS_ANTI_REPLAY */
4724
4725#if defined(MBEDTLS_SSL_PROTO_DTLS)
Gilles Peskine449bd832023-01-11 14:50:10 +01004726 if ((size_t) (end - p) < 1) {
4727 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
4728 }
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02004729
4730 ssl->disable_datagram_packing = *p++;
4731#endif /* MBEDTLS_SSL_PROTO_DTLS */
4732
Gilles Peskine449bd832023-01-11 14:50:10 +01004733 if ((size_t) (end - p) < sizeof(ssl->cur_out_ctr)) {
4734 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
4735 }
4736 memcpy(ssl->cur_out_ctr, p, sizeof(ssl->cur_out_ctr));
4737 p += sizeof(ssl->cur_out_ctr);
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02004738
4739#if defined(MBEDTLS_SSL_PROTO_DTLS)
Gilles Peskine449bd832023-01-11 14:50:10 +01004740 if ((size_t) (end - p) < 2) {
4741 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
4742 }
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02004743
Gilles Peskine449bd832023-01-11 14:50:10 +01004744 ssl->mtu = (p[0] << 8) | p[1];
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02004745 p += 2;
4746#endif /* MBEDTLS_SSL_PROTO_DTLS */
4747
4748#if defined(MBEDTLS_SSL_ALPN)
4749 {
4750 uint8_t alpn_len;
4751 const char **cur;
4752
Gilles Peskine449bd832023-01-11 14:50:10 +01004753 if ((size_t) (end - p) < 1) {
4754 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
4755 }
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02004756
4757 alpn_len = *p++;
4758
Gilles Peskine449bd832023-01-11 14:50:10 +01004759 if (alpn_len != 0 && ssl->conf->alpn_list != NULL) {
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02004760 /* alpn_chosen should point to an item in the configured list */
Gilles Peskine449bd832023-01-11 14:50:10 +01004761 for (cur = ssl->conf->alpn_list; *cur != NULL; cur++) {
4762 if (strlen(*cur) == alpn_len &&
4763 memcmp(p, cur, alpn_len) == 0) {
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02004764 ssl->alpn_chosen = *cur;
4765 break;
4766 }
4767 }
4768 }
4769
4770 /* can only happen on conf mismatch */
Gilles Peskine449bd832023-01-11 14:50:10 +01004771 if (alpn_len != 0 && ssl->alpn_chosen == NULL) {
4772 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
4773 }
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02004774
4775 p += alpn_len;
4776 }
4777#endif /* MBEDTLS_SSL_ALPN */
4778
4779 /*
Manuel Pégourié-Gonnard0eb3eac2019-07-15 11:53:51 +02004780 * Forced fields from top-level ssl_context structure
4781 *
4782 * Most of them already set to the correct value by mbedtls_ssl_init() and
4783 * mbedtls_ssl_reset(), so we only need to set the remaining ones.
4784 */
4785 ssl->state = MBEDTLS_SSL_HANDSHAKE_OVER;
Glenn Strauss60bfe602022-03-14 19:04:24 -04004786 ssl->tls_version = MBEDTLS_SSL_VERSION_TLS1_2;
Manuel Pégourié-Gonnard0eb3eac2019-07-15 11:53:51 +02004787
Hanno Becker361b10d2019-08-30 10:42:49 +01004788 /* Adjust pointers for header fields of outgoing records to
4789 * the given transform, accounting for explicit IV and CID. */
Gilles Peskine449bd832023-01-11 14:50:10 +01004790 mbedtls_ssl_update_out_pointers(ssl, ssl->transform);
Hanno Becker361b10d2019-08-30 10:42:49 +01004791
Manuel Pégourié-Gonnard0eb3eac2019-07-15 11:53:51 +02004792#if defined(MBEDTLS_SSL_PROTO_DTLS)
4793 ssl->in_epoch = 1;
4794#endif
4795
4796 /* mbedtls_ssl_reset() leaves the handshake sub-structure allocated,
4797 * which we don't want - otherwise we'd end up freeing the wrong transform
Hanno Beckerce5f5fd2020-02-05 10:47:44 +00004798 * by calling mbedtls_ssl_handshake_wrapup_free_hs_transform()
4799 * inappropriately. */
Gilles Peskine449bd832023-01-11 14:50:10 +01004800 if (ssl->handshake != NULL) {
4801 mbedtls_ssl_handshake_free(ssl);
4802 mbedtls_free(ssl->handshake);
Manuel Pégourié-Gonnard0eb3eac2019-07-15 11:53:51 +02004803 ssl->handshake = NULL;
4804 }
4805
4806 /*
Manuel Pégourié-Gonnard4c90e852019-07-11 10:58:10 +02004807 * Done - should have consumed entire buffer
4808 */
Gilles Peskine449bd832023-01-11 14:50:10 +01004809 if (p != end) {
4810 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
4811 }
Manuel Pégourié-Gonnardac87e282019-05-28 13:02:16 +02004812
Gilles Peskine449bd832023-01-11 14:50:10 +01004813 return 0;
Manuel Pégourié-Gonnardac87e282019-05-28 13:02:16 +02004814}
4815
4816/*
Manuel Pégourié-Gonnardb9dfc9f2019-07-12 10:50:19 +02004817 * Deserialize context: public wrapper for error cleaning
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02004818 */
Gilles Peskine449bd832023-01-11 14:50:10 +01004819int mbedtls_ssl_context_load(mbedtls_ssl_context *context,
4820 const unsigned char *buf,
4821 size_t len)
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02004822{
Gilles Peskine449bd832023-01-11 14:50:10 +01004823 int ret = ssl_context_load(context, buf, len);
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02004824
Gilles Peskine449bd832023-01-11 14:50:10 +01004825 if (ret != 0) {
4826 mbedtls_ssl_free(context);
4827 }
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02004828
Gilles Peskine449bd832023-01-11 14:50:10 +01004829 return ret;
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02004830}
Manuel Pégourié-Gonnard5c0e3772019-07-23 16:13:17 +02004831#endif /* MBEDTLS_SSL_CONTEXT_SERIALIZATION */
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02004832
4833/*
Paul Bakker5121ce52009-01-03 21:22:43 +00004834 * Free an SSL context
4835 */
Gilles Peskine449bd832023-01-11 14:50:10 +01004836void mbedtls_ssl_free(mbedtls_ssl_context *ssl)
Paul Bakker5121ce52009-01-03 21:22:43 +00004837{
Gilles Peskine449bd832023-01-11 14:50:10 +01004838 if (ssl == NULL) {
Paul Bakkeraccaffe2014-06-26 13:37:14 +02004839 return;
Gilles Peskine449bd832023-01-11 14:50:10 +01004840 }
Paul Bakkeraccaffe2014-06-26 13:37:14 +02004841
Gilles Peskine449bd832023-01-11 14:50:10 +01004842 MBEDTLS_SSL_DEBUG_MSG(2, ("=> free"));
Paul Bakker5121ce52009-01-03 21:22:43 +00004843
Gilles Peskine449bd832023-01-11 14:50:10 +01004844 if (ssl->out_buf != NULL) {
sander-visserb8aa2072020-05-06 22:05:13 +02004845#if defined(MBEDTLS_SSL_VARIABLE_BUFFER_LENGTH)
4846 size_t out_buf_len = ssl->out_buf_len;
4847#else
4848 size_t out_buf_len = MBEDTLS_SSL_OUT_BUFFER_LEN;
4849#endif
4850
Gilles Peskine449bd832023-01-11 14:50:10 +01004851 mbedtls_platform_zeroize(ssl->out_buf, out_buf_len);
4852 mbedtls_free(ssl->out_buf);
Andrzej Kurek0afa2a12020-03-03 10:39:58 -05004853 ssl->out_buf = NULL;
Paul Bakker5121ce52009-01-03 21:22:43 +00004854 }
4855
Gilles Peskine449bd832023-01-11 14:50:10 +01004856 if (ssl->in_buf != NULL) {
sander-visserb8aa2072020-05-06 22:05:13 +02004857#if defined(MBEDTLS_SSL_VARIABLE_BUFFER_LENGTH)
4858 size_t in_buf_len = ssl->in_buf_len;
4859#else
4860 size_t in_buf_len = MBEDTLS_SSL_IN_BUFFER_LEN;
4861#endif
4862
Gilles Peskine449bd832023-01-11 14:50:10 +01004863 mbedtls_platform_zeroize(ssl->in_buf, in_buf_len);
4864 mbedtls_free(ssl->in_buf);
Andrzej Kurek0afa2a12020-03-03 10:39:58 -05004865 ssl->in_buf = NULL;
Paul Bakker5121ce52009-01-03 21:22:43 +00004866 }
4867
Gilles Peskine449bd832023-01-11 14:50:10 +01004868 if (ssl->transform) {
4869 mbedtls_ssl_transform_free(ssl->transform);
4870 mbedtls_free(ssl->transform);
Paul Bakker48916f92012-09-16 19:57:18 +00004871 }
4872
Gilles Peskine449bd832023-01-11 14:50:10 +01004873 if (ssl->handshake) {
4874 mbedtls_ssl_handshake_free(ssl);
4875 mbedtls_free(ssl->handshake);
Jerry Yu2e199812022-12-01 18:57:19 +08004876
4877#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
Gilles Peskine449bd832023-01-11 14:50:10 +01004878 mbedtls_ssl_transform_free(ssl->transform_negotiate);
4879 mbedtls_free(ssl->transform_negotiate);
Jerry Yu2e199812022-12-01 18:57:19 +08004880#endif
4881
Gilles Peskine449bd832023-01-11 14:50:10 +01004882 mbedtls_ssl_session_free(ssl->session_negotiate);
4883 mbedtls_free(ssl->session_negotiate);
Paul Bakker48916f92012-09-16 19:57:18 +00004884 }
4885
Ronald Cron6f135e12021-12-08 16:57:54 +01004886#if defined(MBEDTLS_SSL_PROTO_TLS1_3)
Gilles Peskine449bd832023-01-11 14:50:10 +01004887 mbedtls_ssl_transform_free(ssl->transform_application);
4888 mbedtls_free(ssl->transform_application);
Ronald Cron6f135e12021-12-08 16:57:54 +01004889#endif /* MBEDTLS_SSL_PROTO_TLS1_3 */
Hanno Becker3aa186f2021-08-10 09:24:19 +01004890
Gilles Peskine449bd832023-01-11 14:50:10 +01004891 if (ssl->session) {
4892 mbedtls_ssl_session_free(ssl->session);
4893 mbedtls_free(ssl->session);
Paul Bakkerc0463502013-02-14 11:19:38 +01004894 }
4895
Manuel Pégourié-Gonnard55fab2d2015-05-11 16:15:19 +02004896#if defined(MBEDTLS_X509_CRT_PARSE_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01004897 if (ssl->hostname != NULL) {
4898 mbedtls_platform_zeroize(ssl->hostname, strlen(ssl->hostname));
4899 mbedtls_free(ssl->hostname);
Paul Bakker5121ce52009-01-03 21:22:43 +00004900 }
Paul Bakker0be444a2013-08-27 21:55:01 +02004901#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00004902
Manuel Pégourié-Gonnarde057d3b2015-05-20 10:59:43 +02004903#if defined(MBEDTLS_SSL_DTLS_HELLO_VERIFY) && defined(MBEDTLS_SSL_SRV_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01004904 mbedtls_free(ssl->cli_id);
Manuel Pégourié-Gonnard43c02182014-07-22 17:32:01 +02004905#endif
4906
Gilles Peskine449bd832023-01-11 14:50:10 +01004907 MBEDTLS_SSL_DEBUG_MSG(2, ("<= free"));
Paul Bakker2da561c2009-02-05 18:00:28 +00004908
Paul Bakker86f04f42013-02-14 11:20:09 +01004909 /* Actually clear after last debug message */
Gilles Peskine449bd832023-01-11 14:50:10 +01004910 mbedtls_platform_zeroize(ssl, sizeof(mbedtls_ssl_context));
Paul Bakker5121ce52009-01-03 21:22:43 +00004911}
4912
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02004913/*
Shaun Case8b0ecbc2021-12-20 21:14:10 -08004914 * Initialize mbedtls_ssl_config
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02004915 */
Gilles Peskine449bd832023-01-11 14:50:10 +01004916void mbedtls_ssl_config_init(mbedtls_ssl_config *conf)
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02004917{
Gilles Peskine449bd832023-01-11 14:50:10 +01004918 memset(conf, 0, sizeof(mbedtls_ssl_config));
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02004919}
4920
Gilles Peskineae270bf2021-06-02 00:05:29 +02004921/* The selection should be the same as mbedtls_x509_crt_profile_default in
4922 * x509_crt.c, plus Montgomery curves for ECDHE. Here, the order matters:
Gilles Peskineb1940a72021-06-02 15:18:12 +02004923 * curves with a lower resource usage come first.
Gilles Peskineae270bf2021-06-02 00:05:29 +02004924 * See the documentation of mbedtls_ssl_conf_curves() for what we promise
Gilles Peskineb1940a72021-06-02 15:18:12 +02004925 * about this list.
4926 */
Brett Warrene0edc842021-08-17 09:53:13 +01004927static uint16_t ssl_preset_default_groups[] = {
Gilles Peskineae270bf2021-06-02 00:05:29 +02004928#if defined(MBEDTLS_ECP_DP_CURVE25519_ENABLED)
Brett Warrene0edc842021-08-17 09:53:13 +01004929 MBEDTLS_SSL_IANA_TLS_GROUP_X25519,
Gilles Peskineae270bf2021-06-02 00:05:29 +02004930#endif
4931#if defined(MBEDTLS_ECP_DP_SECP256R1_ENABLED)
Brett Warrene0edc842021-08-17 09:53:13 +01004932 MBEDTLS_SSL_IANA_TLS_GROUP_SECP256R1,
Gilles Peskineae270bf2021-06-02 00:05:29 +02004933#endif
Gilles Peskineb1940a72021-06-02 15:18:12 +02004934#if defined(MBEDTLS_ECP_DP_SECP384R1_ENABLED)
Brett Warrene0edc842021-08-17 09:53:13 +01004935 MBEDTLS_SSL_IANA_TLS_GROUP_SECP384R1,
Gilles Peskineb1940a72021-06-02 15:18:12 +02004936#endif
4937#if defined(MBEDTLS_ECP_DP_CURVE448_ENABLED)
Brett Warrene0edc842021-08-17 09:53:13 +01004938 MBEDTLS_SSL_IANA_TLS_GROUP_X448,
Gilles Peskineb1940a72021-06-02 15:18:12 +02004939#endif
4940#if defined(MBEDTLS_ECP_DP_SECP521R1_ENABLED)
Brett Warrene0edc842021-08-17 09:53:13 +01004941 MBEDTLS_SSL_IANA_TLS_GROUP_SECP521R1,
Gilles Peskineb1940a72021-06-02 15:18:12 +02004942#endif
Gilles Peskineae270bf2021-06-02 00:05:29 +02004943#if defined(MBEDTLS_ECP_DP_BP256R1_ENABLED)
Brett Warrene0edc842021-08-17 09:53:13 +01004944 MBEDTLS_SSL_IANA_TLS_GROUP_BP256R1,
Gilles Peskineae270bf2021-06-02 00:05:29 +02004945#endif
Gilles Peskineb1940a72021-06-02 15:18:12 +02004946#if defined(MBEDTLS_ECP_DP_BP384R1_ENABLED)
Brett Warrene0edc842021-08-17 09:53:13 +01004947 MBEDTLS_SSL_IANA_TLS_GROUP_BP384R1,
Gilles Peskineb1940a72021-06-02 15:18:12 +02004948#endif
4949#if defined(MBEDTLS_ECP_DP_BP512R1_ENABLED)
Brett Warrene0edc842021-08-17 09:53:13 +01004950 MBEDTLS_SSL_IANA_TLS_GROUP_BP512R1,
Gilles Peskineb1940a72021-06-02 15:18:12 +02004951#endif
Brett Warrene0edc842021-08-17 09:53:13 +01004952 MBEDTLS_SSL_IANA_TLS_GROUP_NONE
Gilles Peskineae270bf2021-06-02 00:05:29 +02004953};
Gilles Peskineae270bf2021-06-02 00:05:29 +02004954
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02004955static int ssl_preset_suiteb_ciphersuites[] = {
4956 MBEDTLS_TLS_ECDHE_ECDSA_WITH_AES_128_GCM_SHA256,
4957 MBEDTLS_TLS_ECDHE_ECDSA_WITH_AES_256_GCM_SHA384,
4958 0
4959};
4960
Ronald Crone68ab4f2022-10-05 12:46:29 +02004961#if defined(MBEDTLS_SSL_HANDSHAKE_WITH_CERT_ENABLED)
Hanno Becker9c6aa7b2021-08-10 13:50:43 +01004962
Jerry Yu909df7b2022-01-22 11:56:27 +08004963/* NOTICE:
Jerry Yu0b994b82022-01-25 17:22:12 +08004964 * For ssl_preset_*_sig_algs and ssl_tls12_preset_*_sig_algs, the following
Jerry Yu370e1462022-01-25 10:36:53 +08004965 * rules SHOULD be upheld.
4966 * - No duplicate entries.
4967 * - But if there is a good reason, do not change the order of the algorithms.
Jerry Yu09a99fc2022-07-28 14:22:17 +08004968 * - ssl_tls12_preset* is for TLS 1.2 use only.
Jerry Yu370e1462022-01-25 10:36:53 +08004969 * - ssl_preset_* is for TLS 1.3 only or hybrid TLS 1.3/1.2 handshakes.
Jerry Yu1a8b4812022-01-20 17:56:50 +08004970 */
Hanno Becker9c6aa7b2021-08-10 13:50:43 +01004971static uint16_t ssl_preset_default_sig_algs[] = {
Jerry Yu1a8b4812022-01-20 17:56:50 +08004972
Andrzej Kurek25f27152022-08-17 16:09:31 -04004973#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 +08004974 defined(MBEDTLS_ECP_DP_SECP256R1_ENABLED)
4975 MBEDTLS_TLS1_3_SIG_ECDSA_SECP256R1_SHA256,
Andrzej Kurekcccb0442022-08-19 03:42:11 -04004976#endif /* MBEDTLS_ECDSA_C && MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA &&
Jerry Yued5e9f42022-01-26 11:21:34 +08004977 MBEDTLS_ECP_DP_SECP256R1_ENABLED */
Jerry Yu909df7b2022-01-22 11:56:27 +08004978
Andrzej Kurek25f27152022-08-17 16:09:31 -04004979#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 +08004980 defined(MBEDTLS_ECP_DP_SECP384R1_ENABLED)
4981 MBEDTLS_TLS1_3_SIG_ECDSA_SECP384R1_SHA384,
Andrzej Kurekcccb0442022-08-19 03:42:11 -04004982#endif /* MBEDTLS_ECDSA_C && MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA&&
Jerry Yu909df7b2022-01-22 11:56:27 +08004983 MBEDTLS_ECP_DP_SECP384R1_ENABLED */
4984
Andrzej Kurek25f27152022-08-17 16:09:31 -04004985#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 +08004986 defined(MBEDTLS_ECP_DP_SECP521R1_ENABLED)
4987 MBEDTLS_TLS1_3_SIG_ECDSA_SECP521R1_SHA512,
Andrzej Kurekcccb0442022-08-19 03:42:11 -04004988#endif /* MBEDTLS_ECDSA_C && MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA&&
Jerry Yued5e9f42022-01-26 11:21:34 +08004989 MBEDTLS_ECP_DP_SECP521R1_ENABLED */
Jerry Yu909df7b2022-01-22 11:56:27 +08004990
Gilles Peskine449bd832023-01-11 14:50:10 +01004991#if defined(MBEDTLS_X509_RSASSA_PSS_SUPPORT) && \
4992 defined(MBEDTLS_HAS_ALG_SHA_512_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Jerry Yu9bb3ee42022-06-23 10:16:33 +08004993 MBEDTLS_TLS1_3_SIG_RSA_PSS_RSAE_SHA512,
Gilles Peskine449bd832023-01-11 14:50:10 +01004994#endif \
4995 /* 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 +08004996
Gilles Peskine449bd832023-01-11 14:50:10 +01004997#if defined(MBEDTLS_X509_RSASSA_PSS_SUPPORT) && \
4998 defined(MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Jerry Yu9bb3ee42022-06-23 10:16:33 +08004999 MBEDTLS_TLS1_3_SIG_RSA_PSS_RSAE_SHA384,
Gilles Peskine449bd832023-01-11 14:50:10 +01005000#endif \
5001 /* 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 +08005002
Gilles Peskine449bd832023-01-11 14:50:10 +01005003#if defined(MBEDTLS_X509_RSASSA_PSS_SUPPORT) && \
5004 defined(MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Jerry Yu9bb3ee42022-06-23 10:16:33 +08005005 MBEDTLS_TLS1_3_SIG_RSA_PSS_RSAE_SHA256,
Gilles Peskine449bd832023-01-11 14:50:10 +01005006#endif \
5007 /* 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 +08005008
Andrzej Kurek25f27152022-08-17 16:09:31 -04005009#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 +08005010 MBEDTLS_TLS1_3_SIG_RSA_PKCS1_SHA512,
5011#endif /* MBEDTLS_RSA_C && MBEDTLS_SHA512_C */
5012
Andrzej Kurek25f27152022-08-17 16:09:31 -04005013#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 +08005014 MBEDTLS_TLS1_3_SIG_RSA_PKCS1_SHA384,
5015#endif /* MBEDTLS_RSA_C && MBEDTLS_SHA384_C */
5016
Andrzej Kurek25f27152022-08-17 16:09:31 -04005017#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 +08005018 MBEDTLS_TLS1_3_SIG_RSA_PKCS1_SHA256,
5019#endif /* MBEDTLS_RSA_C && MBEDTLS_SHA256_C */
5020
Gabor Mezei15b95a62022-05-09 16:37:58 +02005021 MBEDTLS_TLS_SIG_NONE
Jerry Yu909df7b2022-01-22 11:56:27 +08005022};
5023
5024/* NOTICE: see above */
5025#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
5026static uint16_t ssl_tls12_preset_default_sig_algs[] = {
Andrzej Kurek25f27152022-08-17 16:09:31 -04005027#if defined(MBEDTLS_HAS_ALG_SHA_512_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Gabor Mezeic1051b62022-05-10 13:13:58 +02005028#if defined(MBEDTLS_ECDSA_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01005029 MBEDTLS_SSL_TLS12_SIG_AND_HASH_ALG(MBEDTLS_SSL_SIG_ECDSA, MBEDTLS_SSL_HASH_SHA512),
Jerry Yu713013f2022-01-17 18:16:35 +08005030#endif
Jerry Yu09a99fc2022-07-28 14:22:17 +08005031#if defined(MBEDTLS_X509_RSASSA_PSS_SUPPORT)
5032 MBEDTLS_TLS1_3_SIG_RSA_PSS_RSAE_SHA512,
5033#endif /* MBEDTLS_X509_RSASSA_PSS_SUPPORT */
Gabor Mezeic1051b62022-05-10 13:13:58 +02005034#if defined(MBEDTLS_RSA_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01005035 MBEDTLS_SSL_TLS12_SIG_AND_HASH_ALG(MBEDTLS_SSL_SIG_RSA, MBEDTLS_SSL_HASH_SHA512),
Gabor Mezeic1051b62022-05-10 13:13:58 +02005036#endif
Andrzej Kurekcccb0442022-08-19 03:42:11 -04005037#endif /* MBEDTLS_HAS_ALG_SHA_512_VIA_MD_OR_PSA_BASED_ON_USE_PSA*/
Andrzej Kurek25f27152022-08-17 16:09:31 -04005038#if defined(MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Gabor Mezeic1051b62022-05-10 13:13:58 +02005039#if defined(MBEDTLS_ECDSA_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01005040 MBEDTLS_SSL_TLS12_SIG_AND_HASH_ALG(MBEDTLS_SSL_SIG_ECDSA, MBEDTLS_SSL_HASH_SHA384),
Jerry Yu11f0a9c2022-01-12 18:43:08 +08005041#endif
Jerry Yu09a99fc2022-07-28 14:22:17 +08005042#if defined(MBEDTLS_X509_RSASSA_PSS_SUPPORT)
5043 MBEDTLS_TLS1_3_SIG_RSA_PSS_RSAE_SHA384,
5044#endif /* MBEDTLS_X509_RSASSA_PSS_SUPPORT */
Gabor Mezeic1051b62022-05-10 13:13:58 +02005045#if defined(MBEDTLS_RSA_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01005046 MBEDTLS_SSL_TLS12_SIG_AND_HASH_ALG(MBEDTLS_SSL_SIG_RSA, MBEDTLS_SSL_HASH_SHA384),
Gabor Mezeic1051b62022-05-10 13:13:58 +02005047#endif
Andrzej Kurekcccb0442022-08-19 03:42:11 -04005048#endif /* MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA*/
Andrzej Kurek25f27152022-08-17 16:09:31 -04005049#if defined(MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Gabor Mezeic1051b62022-05-10 13:13:58 +02005050#if defined(MBEDTLS_ECDSA_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01005051 MBEDTLS_SSL_TLS12_SIG_AND_HASH_ALG(MBEDTLS_SSL_SIG_ECDSA, MBEDTLS_SSL_HASH_SHA256),
Jerry Yu11f0a9c2022-01-12 18:43:08 +08005052#endif
Jerry Yu09a99fc2022-07-28 14:22:17 +08005053#if defined(MBEDTLS_X509_RSASSA_PSS_SUPPORT)
5054 MBEDTLS_TLS1_3_SIG_RSA_PSS_RSAE_SHA256,
5055#endif /* MBEDTLS_X509_RSASSA_PSS_SUPPORT */
Gabor Mezeic1051b62022-05-10 13:13:58 +02005056#if defined(MBEDTLS_RSA_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01005057 MBEDTLS_SSL_TLS12_SIG_AND_HASH_ALG(MBEDTLS_SSL_SIG_RSA, MBEDTLS_SSL_HASH_SHA256),
Gabor Mezeic1051b62022-05-10 13:13:58 +02005058#endif
Andrzej Kurekcccb0442022-08-19 03:42:11 -04005059#endif /* MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA*/
Gabor Mezei15b95a62022-05-09 16:37:58 +02005060 MBEDTLS_TLS_SIG_NONE
Jerry Yu909df7b2022-01-22 11:56:27 +08005061};
5062#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
5063/* NOTICE: see above */
5064static uint16_t ssl_preset_suiteb_sig_algs[] = {
5065
Andrzej Kurek25f27152022-08-17 16:09:31 -04005066#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 +08005067 defined(MBEDTLS_ECP_DP_SECP256R1_ENABLED)
5068 MBEDTLS_TLS1_3_SIG_ECDSA_SECP256R1_SHA256,
Andrzej Kurekcccb0442022-08-19 03:42:11 -04005069#endif /* MBEDTLS_ECDSA_C && MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA&&
Jerry Yu909df7b2022-01-22 11:56:27 +08005070 MBEDTLS_ECP_DP_SECP256R1_ENABLED */
5071
Andrzej Kurek25f27152022-08-17 16:09:31 -04005072#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 +08005073 defined(MBEDTLS_ECP_DP_SECP384R1_ENABLED)
5074 MBEDTLS_TLS1_3_SIG_ECDSA_SECP384R1_SHA384,
Andrzej Kurekcccb0442022-08-19 03:42:11 -04005075#endif /* MBEDTLS_ECDSA_C && MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA&&
Jerry Yu53037892022-01-25 11:02:06 +08005076 MBEDTLS_ECP_DP_SECP384R1_ENABLED */
Jerry Yu909df7b2022-01-22 11:56:27 +08005077
Gilles Peskine449bd832023-01-11 14:50:10 +01005078#if defined(MBEDTLS_X509_RSASSA_PSS_SUPPORT) && \
5079 defined(MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Jerry Yu909df7b2022-01-22 11:56:27 +08005080 MBEDTLS_TLS1_3_SIG_RSA_PSS_RSAE_SHA256,
Gilles Peskine449bd832023-01-11 14:50:10 +01005081#endif \
5082 /* 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 +08005083
Andrzej Kurek25f27152022-08-17 16:09:31 -04005084#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 +08005085 MBEDTLS_TLS1_3_SIG_RSA_PKCS1_SHA256,
Andrzej Kurekcccb0442022-08-19 03:42:11 -04005086#endif /* MBEDTLS_RSA_C && MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA*/
Jerry Yu53037892022-01-25 11:02:06 +08005087
Gabor Mezei15b95a62022-05-09 16:37:58 +02005088 MBEDTLS_TLS_SIG_NONE
Jerry Yu713013f2022-01-17 18:16:35 +08005089};
Jerry Yu6106fdc2022-01-12 16:36:14 +08005090
Jerry Yu909df7b2022-01-22 11:56:27 +08005091/* NOTICE: see above */
5092#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
5093static uint16_t ssl_tls12_preset_suiteb_sig_algs[] = {
Andrzej Kurek25f27152022-08-17 16:09:31 -04005094#if defined(MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Gabor Mezeic1051b62022-05-10 13:13:58 +02005095#if defined(MBEDTLS_ECDSA_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01005096 MBEDTLS_SSL_TLS12_SIG_AND_HASH_ALG(MBEDTLS_SSL_SIG_ECDSA, MBEDTLS_SSL_HASH_SHA256),
Jerry Yu713013f2022-01-17 18:16:35 +08005097#endif
Gabor Mezeic1051b62022-05-10 13:13:58 +02005098#if defined(MBEDTLS_RSA_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01005099 MBEDTLS_SSL_TLS12_SIG_AND_HASH_ALG(MBEDTLS_SSL_SIG_RSA, MBEDTLS_SSL_HASH_SHA256),
Gabor Mezeic1051b62022-05-10 13:13:58 +02005100#endif
Andrzej Kurekcccb0442022-08-19 03:42:11 -04005101#endif /* MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA*/
Andrzej Kurek25f27152022-08-17 16:09:31 -04005102#if defined(MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Gabor Mezeic1051b62022-05-10 13:13:58 +02005103#if defined(MBEDTLS_ECDSA_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01005104 MBEDTLS_SSL_TLS12_SIG_AND_HASH_ALG(MBEDTLS_SSL_SIG_ECDSA, MBEDTLS_SSL_HASH_SHA384),
Jerry Yu18c833e2022-01-25 10:55:47 +08005105#endif
Gabor Mezeic1051b62022-05-10 13:13:58 +02005106#if defined(MBEDTLS_RSA_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01005107 MBEDTLS_SSL_TLS12_SIG_AND_HASH_ALG(MBEDTLS_SSL_SIG_RSA, MBEDTLS_SSL_HASH_SHA384),
Gabor Mezeic1051b62022-05-10 13:13:58 +02005108#endif
Andrzej Kurekcccb0442022-08-19 03:42:11 -04005109#endif /* MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA*/
Gabor Mezei15b95a62022-05-09 16:37:58 +02005110 MBEDTLS_TLS_SIG_NONE
Hanno Becker9c6aa7b2021-08-10 13:50:43 +01005111};
Jerry Yu909df7b2022-01-22 11:56:27 +08005112#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
5113
Ronald Crone68ab4f2022-10-05 12:46:29 +02005114#endif /* MBEDTLS_SSL_HANDSHAKE_WITH_CERT_ENABLED */
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02005115
Brett Warrene0edc842021-08-17 09:53:13 +01005116static uint16_t ssl_preset_suiteb_groups[] = {
Jaeden Amerod4311042019-06-03 08:27:16 +01005117#if defined(MBEDTLS_ECP_DP_SECP256R1_ENABLED)
Brett Warrene0edc842021-08-17 09:53:13 +01005118 MBEDTLS_SSL_IANA_TLS_GROUP_SECP256R1,
Jaeden Amerod4311042019-06-03 08:27:16 +01005119#endif
5120#if defined(MBEDTLS_ECP_DP_SECP384R1_ENABLED)
Brett Warrene0edc842021-08-17 09:53:13 +01005121 MBEDTLS_SSL_IANA_TLS_GROUP_SECP384R1,
Jaeden Amerod4311042019-06-03 08:27:16 +01005122#endif
Brett Warrene0edc842021-08-17 09:53:13 +01005123 MBEDTLS_SSL_IANA_TLS_GROUP_NONE
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02005124};
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02005125
Ronald Crone68ab4f2022-10-05 12:46:29 +02005126#if defined(MBEDTLS_DEBUG_C) && defined(MBEDTLS_SSL_HANDSHAKE_WITH_CERT_ENABLED)
Jerry Yu909df7b2022-01-22 11:56:27 +08005127/* Function for checking `ssl_preset_*_sig_algs` and `ssl_tls12_preset_*_sig_algs`
Jerry Yu370e1462022-01-25 10:36:53 +08005128 * to make sure there are no duplicated signature algorithm entries. */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02005129MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01005130static int ssl_check_no_sig_alg_duplication(uint16_t *sig_algs)
Jerry Yu1a8b4812022-01-20 17:56:50 +08005131{
5132 size_t i, j;
5133 int ret = 0;
Jerry Yu909df7b2022-01-22 11:56:27 +08005134
Gilles Peskine449bd832023-01-11 14:50:10 +01005135 for (i = 0; sig_algs[i] != MBEDTLS_TLS_SIG_NONE; i++) {
5136 for (j = 0; j < i; j++) {
5137 if (sig_algs[i] != sig_algs[j]) {
Jerry Yuf377d642022-01-25 10:43:59 +08005138 continue;
Gilles Peskine449bd832023-01-11 14:50:10 +01005139 }
5140 mbedtls_printf(" entry(%04x,%" MBEDTLS_PRINTF_SIZET
5141 ") is duplicated at %" MBEDTLS_PRINTF_SIZET "\n",
5142 sig_algs[i], j, i);
Jerry Yuf377d642022-01-25 10:43:59 +08005143 ret = -1;
Jerry Yu1a8b4812022-01-20 17:56:50 +08005144 }
5145 }
Gilles Peskine449bd832023-01-11 14:50:10 +01005146 return ret;
Jerry Yu1a8b4812022-01-20 17:56:50 +08005147}
5148
Ronald Crone68ab4f2022-10-05 12:46:29 +02005149#endif /* MBEDTLS_DEBUG_C && MBEDTLS_SSL_HANDSHAKE_WITH_CERT_ENABLED */
Jerry Yu1a8b4812022-01-20 17:56:50 +08005150
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02005151/*
Tillmann Karras588ad502015-09-25 04:27:22 +02005152 * Load default in mbedtls_ssl_config
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02005153 */
Gilles Peskine449bd832023-01-11 14:50:10 +01005154int mbedtls_ssl_config_defaults(mbedtls_ssl_config *conf,
5155 int endpoint, int transport, int preset)
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02005156{
Manuel Pégourié-Gonnard8b431fb2015-05-11 12:54:52 +02005157#if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_SSL_SRV_C)
Janos Follath865b3eb2019-12-16 11:46:15 +00005158 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnard8b431fb2015-05-11 12:54:52 +02005159#endif
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02005160
Ronald Crone68ab4f2022-10-05 12:46:29 +02005161#if defined(MBEDTLS_DEBUG_C) && defined(MBEDTLS_SSL_HANDSHAKE_WITH_CERT_ENABLED)
Gilles Peskine449bd832023-01-11 14:50:10 +01005162 if (ssl_check_no_sig_alg_duplication(ssl_preset_suiteb_sig_algs)) {
5163 mbedtls_printf("ssl_preset_suiteb_sig_algs has duplicated entries\n");
5164 return MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Jerry Yu1a8b4812022-01-20 17:56:50 +08005165 }
5166
Gilles Peskine449bd832023-01-11 14:50:10 +01005167 if (ssl_check_no_sig_alg_duplication(ssl_preset_default_sig_algs)) {
5168 mbedtls_printf("ssl_preset_default_sig_algs has duplicated entries\n");
5169 return MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Jerry Yu1a8b4812022-01-20 17:56:50 +08005170 }
Jerry Yu909df7b2022-01-22 11:56:27 +08005171
5172#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
Gilles Peskine449bd832023-01-11 14:50:10 +01005173 if (ssl_check_no_sig_alg_duplication(ssl_tls12_preset_suiteb_sig_algs)) {
5174 mbedtls_printf("ssl_tls12_preset_suiteb_sig_algs has duplicated entries\n");
5175 return MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Jerry Yu909df7b2022-01-22 11:56:27 +08005176 }
5177
Gilles Peskine449bd832023-01-11 14:50:10 +01005178 if (ssl_check_no_sig_alg_duplication(ssl_tls12_preset_default_sig_algs)) {
5179 mbedtls_printf("ssl_tls12_preset_default_sig_algs has duplicated entries\n");
5180 return MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Jerry Yu909df7b2022-01-22 11:56:27 +08005181 }
5182#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Ronald Crone68ab4f2022-10-05 12:46:29 +02005183#endif /* MBEDTLS_DEBUG_C && MBEDTLS_SSL_HANDSHAKE_WITH_CERT_ENABLED */
Jerry Yu1a8b4812022-01-20 17:56:50 +08005184
Manuel Pégourié-Gonnard0de074f2015-05-14 12:58:01 +02005185 /* Use the functions here so that they are covered in tests,
5186 * but otherwise access member directly for efficiency */
Gilles Peskine449bd832023-01-11 14:50:10 +01005187 mbedtls_ssl_conf_endpoint(conf, endpoint);
5188 mbedtls_ssl_conf_transport(conf, transport);
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02005189
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02005190 /*
5191 * Things that are common to all presets
5192 */
Manuel Pégourié-Gonnard419d5ae2015-05-04 19:32:36 +02005193#if defined(MBEDTLS_SSL_CLI_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01005194 if (endpoint == MBEDTLS_SSL_IS_CLIENT) {
Manuel Pégourié-Gonnard419d5ae2015-05-04 19:32:36 +02005195 conf->authmode = MBEDTLS_SSL_VERIFY_REQUIRED;
5196#if defined(MBEDTLS_SSL_SESSION_TICKETS)
5197 conf->session_tickets = MBEDTLS_SSL_SESSION_TICKETS_ENABLED;
5198#endif
5199 }
5200#endif
5201
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02005202#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
5203 conf->encrypt_then_mac = MBEDTLS_SSL_ETM_ENABLED;
5204#endif
5205
5206#if defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET)
5207 conf->extended_ms = MBEDTLS_SSL_EXTENDED_MS_ENABLED;
5208#endif
5209
Manuel Pégourié-Gonnarde057d3b2015-05-20 10:59:43 +02005210#if defined(MBEDTLS_SSL_DTLS_HELLO_VERIFY) && defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02005211 conf->f_cookie_write = ssl_cookie_write_dummy;
5212 conf->f_cookie_check = ssl_cookie_check_dummy;
5213#endif
5214
5215#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
5216 conf->anti_replay = MBEDTLS_SSL_ANTI_REPLAY_ENABLED;
5217#endif
5218
Janos Follath088ce432017-04-10 12:42:31 +01005219#if defined(MBEDTLS_SSL_SRV_C)
5220 conf->cert_req_ca_list = MBEDTLS_SSL_CERT_REQ_CA_LIST_ENABLED;
TRodziewicz3946f792021-06-14 12:11:18 +02005221 conf->respect_cli_pref = MBEDTLS_SSL_SRV_CIPHERSUITE_ORDER_SERVER;
Janos Follath088ce432017-04-10 12:42:31 +01005222#endif
5223
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02005224#if defined(MBEDTLS_SSL_PROTO_DTLS)
5225 conf->hs_timeout_min = MBEDTLS_SSL_DTLS_TIMEOUT_DFL_MIN;
5226 conf->hs_timeout_max = MBEDTLS_SSL_DTLS_TIMEOUT_DFL_MAX;
5227#endif
5228
5229#if defined(MBEDTLS_SSL_RENEGOTIATION)
5230 conf->renego_max_records = MBEDTLS_SSL_RENEGO_MAX_RECORDS_DEFAULT;
Gilles Peskine449bd832023-01-11 14:50:10 +01005231 memset(conf->renego_period, 0x00, 2);
5232 memset(conf->renego_period + 2, 0xFF, 6);
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02005233#endif
5234
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02005235#if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_SSL_SRV_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01005236 if (endpoint == MBEDTLS_SSL_IS_SERVER) {
Hanno Beckere2defad2021-07-24 05:59:17 +01005237 const unsigned char dhm_p[] =
5238 MBEDTLS_DHM_RFC3526_MODP_2048_P_BIN;
5239 const unsigned char dhm_g[] =
5240 MBEDTLS_DHM_RFC3526_MODP_2048_G_BIN;
Hanno Becker00d0a682017-10-04 13:14:29 +01005241
Gilles Peskine449bd832023-01-11 14:50:10 +01005242 if ((ret = mbedtls_ssl_conf_dh_param_bin(conf,
5243 dhm_p, sizeof(dhm_p),
5244 dhm_g, sizeof(dhm_g))) != 0) {
5245 return ret;
Hanno Beckere2defad2021-07-24 05:59:17 +01005246 }
5247 }
Manuel Pégourié-Gonnardbd990d62015-06-11 14:49:42 +02005248#endif
5249
Ronald Cron6f135e12021-12-08 16:57:54 +01005250#if defined(MBEDTLS_SSL_PROTO_TLS1_3)
Jerry Yu6ee56aa2022-12-06 17:47:22 +08005251
5252#if defined(MBEDTLS_SSL_EARLY_DATA)
Gilles Peskine449bd832023-01-11 14:50:10 +01005253 mbedtls_ssl_tls13_conf_early_data(conf, MBEDTLS_SSL_EARLY_DATA_DISABLED);
Jerry Yu6ee56aa2022-12-06 17:47:22 +08005254#if defined(MBEDTLS_SSL_SRV_C)
5255 mbedtls_ssl_tls13_conf_max_early_data_size(
Gilles Peskine449bd832023-01-11 14:50:10 +01005256 conf, MBEDTLS_SSL_MAX_EARLY_DATA_SIZE);
Jerry Yu6ee56aa2022-12-06 17:47:22 +08005257#endif
5258#endif /* MBEDTLS_SSL_EARLY_DATA */
5259
Jerry Yud0766ec2022-09-22 10:46:57 +08005260#if defined(MBEDTLS_SSL_SRV_C) && defined(MBEDTLS_SSL_SESSION_TICKETS)
Jerry Yu1ad7ace2022-08-09 13:28:39 +08005261 mbedtls_ssl_conf_new_session_tickets(
Gilles Peskine449bd832023-01-11 14:50:10 +01005262 conf, MBEDTLS_SSL_TLS1_3_DEFAULT_NEW_SESSION_TICKETS);
Jerry Yu1ad7ace2022-08-09 13:28:39 +08005263#endif
Hanno Becker71f1ed62021-07-24 06:01:47 +01005264 /*
5265 * Allow all TLS 1.3 key exchange modes by default.
5266 */
Xiaofei Bai746f9482021-11-12 08:53:56 +00005267 conf->tls13_kex_modes = MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_ALL;
Ronald Cron6f135e12021-12-08 16:57:54 +01005268#endif /* MBEDTLS_SSL_PROTO_TLS1_3 */
Hanno Becker71f1ed62021-07-24 06:01:47 +01005269
Gilles Peskine449bd832023-01-11 14:50:10 +01005270 if (transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM) {
Glenn Strauss2dfcea22022-03-14 17:26:42 -04005271#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
XiaokangQian4d3a6042022-04-21 13:46:17 +00005272 conf->min_tls_version = MBEDTLS_SSL_VERSION_TLS1_2;
XiaokangQian060d8672022-04-21 09:24:56 +00005273 conf->max_tls_version = MBEDTLS_SSL_VERSION_TLS1_2;
XiaokangQian4d3a6042022-04-21 13:46:17 +00005274#else
Gilles Peskine449bd832023-01-11 14:50:10 +01005275 return MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
XiaokangQian060d8672022-04-21 09:24:56 +00005276#endif
Gilles Peskine449bd832023-01-11 14:50:10 +01005277 } else {
XiaokangQian4d3a6042022-04-21 13:46:17 +00005278#if defined(MBEDTLS_SSL_PROTO_TLS1_2) && defined(MBEDTLS_SSL_PROTO_TLS1_3)
Gilles Peskine449bd832023-01-11 14:50:10 +01005279 if (endpoint == MBEDTLS_SSL_IS_CLIENT) {
XiaokangQian4d3a6042022-04-21 13:46:17 +00005280 conf->min_tls_version = MBEDTLS_SSL_VERSION_TLS1_2;
5281 conf->max_tls_version = MBEDTLS_SSL_VERSION_TLS1_3;
Gilles Peskine449bd832023-01-11 14:50:10 +01005282 } else {
5283 /* Hybrid TLS 1.2 / 1.3 is not supported on server side yet */
XiaokangQian4d3a6042022-04-21 13:46:17 +00005284 conf->min_tls_version = MBEDTLS_SSL_VERSION_TLS1_2;
5285 conf->max_tls_version = MBEDTLS_SSL_VERSION_TLS1_2;
5286 }
5287#elif defined(MBEDTLS_SSL_PROTO_TLS1_3)
5288 conf->min_tls_version = MBEDTLS_SSL_VERSION_TLS1_3;
5289 conf->max_tls_version = MBEDTLS_SSL_VERSION_TLS1_3;
5290#elif defined(MBEDTLS_SSL_PROTO_TLS1_2)
5291 conf->min_tls_version = MBEDTLS_SSL_VERSION_TLS1_2;
5292 conf->max_tls_version = MBEDTLS_SSL_VERSION_TLS1_2;
5293#else
Gilles Peskine449bd832023-01-11 14:50:10 +01005294 return MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
XiaokangQian4d3a6042022-04-21 13:46:17 +00005295#endif
5296 }
Glenn Strauss2dfcea22022-03-14 17:26:42 -04005297
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02005298 /*
5299 * Preset-specific defaults
5300 */
Gilles Peskine449bd832023-01-11 14:50:10 +01005301 switch (preset) {
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02005302 /*
5303 * NSA Suite B
5304 */
5305 case MBEDTLS_SSL_PRESET_SUITEB:
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02005306
Hanno Beckerd60b6c62021-04-29 12:04:11 +01005307 conf->ciphersuite_list = ssl_preset_suiteb_ciphersuites;
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02005308
5309#if defined(MBEDTLS_X509_CRT_PARSE_C)
5310 conf->cert_profile = &mbedtls_x509_crt_profile_suiteb;
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02005311#endif
5312
Ronald Crone68ab4f2022-10-05 12:46:29 +02005313#if defined(MBEDTLS_SSL_HANDSHAKE_WITH_CERT_ENABLED)
Jerry Yu909df7b2022-01-22 11:56:27 +08005314#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
Gilles Peskine449bd832023-01-11 14:50:10 +01005315 if (mbedtls_ssl_conf_is_tls12_only(conf)) {
Jerry Yu909df7b2022-01-22 11:56:27 +08005316 conf->sig_algs = ssl_tls12_preset_suiteb_sig_algs;
Gilles Peskine449bd832023-01-11 14:50:10 +01005317 } else
Jerry Yu909df7b2022-01-22 11:56:27 +08005318#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Gilles Peskine449bd832023-01-11 14:50:10 +01005319 conf->sig_algs = ssl_preset_suiteb_sig_algs;
Ronald Crone68ab4f2022-10-05 12:46:29 +02005320#endif /* MBEDTLS_SSL_HANDSHAKE_WITH_CERT_ENABLED */
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02005321
Brett Warrene0edc842021-08-17 09:53:13 +01005322#if defined(MBEDTLS_ECP_C) && !defined(MBEDTLS_DEPRECATED_REMOVED)
5323 conf->curve_list = NULL;
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02005324#endif
Brett Warrene0edc842021-08-17 09:53:13 +01005325 conf->group_list = ssl_preset_suiteb_groups;
Manuel Pégourié-Gonnardc98204e2015-08-11 04:21:01 +02005326 break;
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02005327
5328 /*
5329 * Default
5330 */
5331 default:
Ronald Cronf6606552022-03-15 11:23:25 +01005332
Hanno Beckerd60b6c62021-04-29 12:04:11 +01005333 conf->ciphersuite_list = mbedtls_ssl_list_ciphersuites();
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02005334
5335#if defined(MBEDTLS_X509_CRT_PARSE_C)
5336 conf->cert_profile = &mbedtls_x509_crt_profile_default;
5337#endif
5338
Ronald Crone68ab4f2022-10-05 12:46:29 +02005339#if defined(MBEDTLS_SSL_HANDSHAKE_WITH_CERT_ENABLED)
Jerry Yu909df7b2022-01-22 11:56:27 +08005340#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
Gilles Peskine449bd832023-01-11 14:50:10 +01005341 if (mbedtls_ssl_conf_is_tls12_only(conf)) {
Jerry Yu909df7b2022-01-22 11:56:27 +08005342 conf->sig_algs = ssl_tls12_preset_default_sig_algs;
Gilles Peskine449bd832023-01-11 14:50:10 +01005343 } else
Jerry Yu909df7b2022-01-22 11:56:27 +08005344#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Gilles Peskine449bd832023-01-11 14:50:10 +01005345 conf->sig_algs = ssl_preset_default_sig_algs;
Ronald Crone68ab4f2022-10-05 12:46:29 +02005346#endif /* MBEDTLS_SSL_HANDSHAKE_WITH_CERT_ENABLED */
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02005347
Brett Warrene0edc842021-08-17 09:53:13 +01005348#if defined(MBEDTLS_ECP_C) && !defined(MBEDTLS_DEPRECATED_REMOVED)
5349 conf->curve_list = NULL;
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02005350#endif
Brett Warrene0edc842021-08-17 09:53:13 +01005351 conf->group_list = ssl_preset_default_groups;
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02005352
5353#if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_SSL_CLI_C)
5354 conf->dhm_min_bitlen = 1024;
5355#endif
5356 }
5357
Gilles Peskine449bd832023-01-11 14:50:10 +01005358 return 0;
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02005359}
5360
5361/*
5362 * Free mbedtls_ssl_config
5363 */
Gilles Peskine449bd832023-01-11 14:50:10 +01005364void mbedtls_ssl_config_free(mbedtls_ssl_config *conf)
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02005365{
5366#if defined(MBEDTLS_DHM_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01005367 mbedtls_mpi_free(&conf->dhm_P);
5368 mbedtls_mpi_free(&conf->dhm_G);
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02005369#endif
5370
Ronald Cron73fe8df2022-10-05 14:31:43 +02005371#if defined(MBEDTLS_SSL_HANDSHAKE_WITH_PSK_ENABLED)
Neil Armstrong501c9322022-05-03 09:35:09 +02005372#if defined(MBEDTLS_USE_PSA_CRYPTO)
Gilles Peskine449bd832023-01-11 14:50:10 +01005373 if (!mbedtls_svc_key_id_is_null(conf->psk_opaque)) {
Neil Armstrong501c9322022-05-03 09:35:09 +02005374 conf->psk_opaque = MBEDTLS_SVC_KEY_ID_INIT;
5375 }
Neil Armstrong8ecd6682022-05-05 11:40:35 +02005376#endif /* MBEDTLS_USE_PSA_CRYPTO */
Gilles Peskine449bd832023-01-11 14:50:10 +01005377 if (conf->psk != NULL) {
5378 mbedtls_platform_zeroize(conf->psk, conf->psk_len);
5379 mbedtls_free(conf->psk);
Azim Khan27e8a122018-03-21 14:24:11 +00005380 conf->psk = NULL;
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02005381 conf->psk_len = 0;
junyeonLEE316b1622017-12-20 16:29:30 +09005382 }
5383
Gilles Peskine449bd832023-01-11 14:50:10 +01005384 if (conf->psk_identity != NULL) {
5385 mbedtls_platform_zeroize(conf->psk_identity, conf->psk_identity_len);
5386 mbedtls_free(conf->psk_identity);
Azim Khan27e8a122018-03-21 14:24:11 +00005387 conf->psk_identity = NULL;
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02005388 conf->psk_identity_len = 0;
5389 }
Ronald Cron73fe8df2022-10-05 14:31:43 +02005390#endif /* MBEDTLS_SSL_HANDSHAKE_WITH_PSK_ENABLED */
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02005391
5392#if defined(MBEDTLS_X509_CRT_PARSE_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01005393 ssl_key_cert_free(conf->key_cert);
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02005394#endif
5395
Gilles Peskine449bd832023-01-11 14:50:10 +01005396 mbedtls_platform_zeroize(conf, sizeof(mbedtls_ssl_config));
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02005397}
5398
Manuel Pégourié-Gonnard5674a972015-10-19 15:14:03 +02005399#if defined(MBEDTLS_PK_C) && \
Gilles Peskine449bd832023-01-11 14:50:10 +01005400 (defined(MBEDTLS_RSA_C) || defined(MBEDTLS_ECDSA_C))
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02005401/*
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005402 * Convert between MBEDTLS_PK_XXX and SSL_SIG_XXX
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02005403 */
Gilles Peskine449bd832023-01-11 14:50:10 +01005404unsigned char mbedtls_ssl_sig_from_pk(mbedtls_pk_context *pk)
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02005405{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005406#if defined(MBEDTLS_RSA_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01005407 if (mbedtls_pk_can_do(pk, MBEDTLS_PK_RSA)) {
5408 return MBEDTLS_SSL_SIG_RSA;
5409 }
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02005410#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005411#if defined(MBEDTLS_ECDSA_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01005412 if (mbedtls_pk_can_do(pk, MBEDTLS_PK_ECDSA)) {
5413 return MBEDTLS_SSL_SIG_ECDSA;
5414 }
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02005415#endif
Gilles Peskine449bd832023-01-11 14:50:10 +01005416 return MBEDTLS_SSL_SIG_ANON;
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02005417}
5418
Gilles Peskine449bd832023-01-11 14:50:10 +01005419unsigned char mbedtls_ssl_sig_from_pk_alg(mbedtls_pk_type_t type)
Hanno Becker7e5437a2017-04-28 17:15:26 +01005420{
Gilles Peskine449bd832023-01-11 14:50:10 +01005421 switch (type) {
Hanno Becker7e5437a2017-04-28 17:15:26 +01005422 case MBEDTLS_PK_RSA:
Gilles Peskine449bd832023-01-11 14:50:10 +01005423 return MBEDTLS_SSL_SIG_RSA;
Hanno Becker7e5437a2017-04-28 17:15:26 +01005424 case MBEDTLS_PK_ECDSA:
5425 case MBEDTLS_PK_ECKEY:
Gilles Peskine449bd832023-01-11 14:50:10 +01005426 return MBEDTLS_SSL_SIG_ECDSA;
Hanno Becker7e5437a2017-04-28 17:15:26 +01005427 default:
Gilles Peskine449bd832023-01-11 14:50:10 +01005428 return MBEDTLS_SSL_SIG_ANON;
Hanno Becker7e5437a2017-04-28 17:15:26 +01005429 }
5430}
5431
Gilles Peskine449bd832023-01-11 14:50:10 +01005432mbedtls_pk_type_t mbedtls_ssl_pk_alg_from_sig(unsigned char sig)
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02005433{
Gilles Peskine449bd832023-01-11 14:50:10 +01005434 switch (sig) {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005435#if defined(MBEDTLS_RSA_C)
5436 case MBEDTLS_SSL_SIG_RSA:
Gilles Peskine449bd832023-01-11 14:50:10 +01005437 return MBEDTLS_PK_RSA;
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02005438#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005439#if defined(MBEDTLS_ECDSA_C)
5440 case MBEDTLS_SSL_SIG_ECDSA:
Gilles Peskine449bd832023-01-11 14:50:10 +01005441 return MBEDTLS_PK_ECDSA;
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02005442#endif
5443 default:
Gilles Peskine449bd832023-01-11 14:50:10 +01005444 return MBEDTLS_PK_NONE;
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02005445 }
5446}
Manuel Pégourié-Gonnard5674a972015-10-19 15:14:03 +02005447#endif /* MBEDTLS_PK_C && ( MBEDTLS_RSA_C || MBEDTLS_ECDSA_C ) */
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02005448
Manuel Pégourié-Gonnard1a483832013-09-20 12:29:15 +02005449/*
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +02005450 * Convert from MBEDTLS_SSL_HASH_XXX to MBEDTLS_MD_XXX
Manuel Pégourié-Gonnard1a483832013-09-20 12:29:15 +02005451 */
Gilles Peskine449bd832023-01-11 14:50:10 +01005452mbedtls_md_type_t mbedtls_ssl_md_alg_from_hash(unsigned char hash)
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02005453{
Gilles Peskine449bd832023-01-11 14:50:10 +01005454 switch (hash) {
Andrzej Kurek25f27152022-08-17 16:09:31 -04005455#if defined(MBEDTLS_HAS_ALG_MD5_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005456 case MBEDTLS_SSL_HASH_MD5:
Gilles Peskine449bd832023-01-11 14:50:10 +01005457 return MBEDTLS_MD_MD5;
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02005458#endif
Andrzej Kurek25f27152022-08-17 16:09:31 -04005459#if defined(MBEDTLS_HAS_ALG_SHA_1_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005460 case MBEDTLS_SSL_HASH_SHA1:
Gilles Peskine449bd832023-01-11 14:50:10 +01005461 return MBEDTLS_MD_SHA1;
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02005462#endif
Andrzej Kurek25f27152022-08-17 16:09:31 -04005463#if defined(MBEDTLS_HAS_ALG_SHA_224_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005464 case MBEDTLS_SSL_HASH_SHA224:
Gilles Peskine449bd832023-01-11 14:50:10 +01005465 return MBEDTLS_MD_SHA224;
Mateusz Starzyke3c48b42021-04-19 16:46:28 +02005466#endif
Andrzej Kurek25f27152022-08-17 16:09:31 -04005467#if defined(MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005468 case MBEDTLS_SSL_HASH_SHA256:
Gilles Peskine449bd832023-01-11 14:50:10 +01005469 return MBEDTLS_MD_SHA256;
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02005470#endif
Andrzej Kurek25f27152022-08-17 16:09:31 -04005471#if defined(MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005472 case MBEDTLS_SSL_HASH_SHA384:
Gilles Peskine449bd832023-01-11 14:50:10 +01005473 return MBEDTLS_MD_SHA384;
Mateusz Starzyk3352a532021-04-06 14:28:22 +02005474#endif
Andrzej Kurek25f27152022-08-17 16:09:31 -04005475#if defined(MBEDTLS_HAS_ALG_SHA_512_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005476 case MBEDTLS_SSL_HASH_SHA512:
Gilles Peskine449bd832023-01-11 14:50:10 +01005477 return MBEDTLS_MD_SHA512;
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02005478#endif
5479 default:
Gilles Peskine449bd832023-01-11 14:50:10 +01005480 return MBEDTLS_MD_NONE;
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02005481 }
5482}
5483
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +02005484/*
5485 * Convert from MBEDTLS_MD_XXX to MBEDTLS_SSL_HASH_XXX
5486 */
Gilles Peskine449bd832023-01-11 14:50:10 +01005487unsigned char mbedtls_ssl_hash_from_md_alg(int md)
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +02005488{
Gilles Peskine449bd832023-01-11 14:50:10 +01005489 switch (md) {
Andrzej Kurek25f27152022-08-17 16:09:31 -04005490#if defined(MBEDTLS_HAS_ALG_MD5_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +02005491 case MBEDTLS_MD_MD5:
Gilles Peskine449bd832023-01-11 14:50:10 +01005492 return MBEDTLS_SSL_HASH_MD5;
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +02005493#endif
Andrzej Kurek25f27152022-08-17 16:09:31 -04005494#if defined(MBEDTLS_HAS_ALG_SHA_1_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +02005495 case MBEDTLS_MD_SHA1:
Gilles Peskine449bd832023-01-11 14:50:10 +01005496 return MBEDTLS_SSL_HASH_SHA1;
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +02005497#endif
Andrzej Kurek25f27152022-08-17 16:09:31 -04005498#if defined(MBEDTLS_HAS_ALG_SHA_224_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +02005499 case MBEDTLS_MD_SHA224:
Gilles Peskine449bd832023-01-11 14:50:10 +01005500 return MBEDTLS_SSL_HASH_SHA224;
Mateusz Starzyke3c48b42021-04-19 16:46:28 +02005501#endif
Andrzej Kurek25f27152022-08-17 16:09:31 -04005502#if defined(MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +02005503 case MBEDTLS_MD_SHA256:
Gilles Peskine449bd832023-01-11 14:50:10 +01005504 return MBEDTLS_SSL_HASH_SHA256;
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +02005505#endif
Andrzej Kurek25f27152022-08-17 16:09:31 -04005506#if defined(MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +02005507 case MBEDTLS_MD_SHA384:
Gilles Peskine449bd832023-01-11 14:50:10 +01005508 return MBEDTLS_SSL_HASH_SHA384;
Mateusz Starzyk3352a532021-04-06 14:28:22 +02005509#endif
Andrzej Kurek25f27152022-08-17 16:09:31 -04005510#if defined(MBEDTLS_HAS_ALG_SHA_512_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +02005511 case MBEDTLS_MD_SHA512:
Gilles Peskine449bd832023-01-11 14:50:10 +01005512 return MBEDTLS_SSL_HASH_SHA512;
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +02005513#endif
5514 default:
Gilles Peskine449bd832023-01-11 14:50:10 +01005515 return MBEDTLS_SSL_HASH_NONE;
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +02005516 }
5517}
5518
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01005519/*
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +02005520 * Check if a curve proposed by the peer is in our list.
Manuel Pégourié-Gonnard9d412d82015-06-17 12:10:46 +02005521 * Return 0 if we're willing to use it, -1 otherwise.
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01005522 */
Gilles Peskine449bd832023-01-11 14:50:10 +01005523int mbedtls_ssl_check_curve_tls_id(const mbedtls_ssl_context *ssl, uint16_t tls_id)
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01005524{
Gilles Peskine449bd832023-01-11 14:50:10 +01005525 const uint16_t *group_list = mbedtls_ssl_get_groups(ssl);
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01005526
Gilles Peskine449bd832023-01-11 14:50:10 +01005527 if (group_list == NULL) {
5528 return -1;
Brett Warrene0edc842021-08-17 09:53:13 +01005529 }
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01005530
Gilles Peskine449bd832023-01-11 14:50:10 +01005531 for (; *group_list != 0; group_list++) {
5532 if (*group_list == tls_id) {
5533 return 0;
5534 }
5535 }
5536
5537 return -1;
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01005538}
Manuel Pégourié-Gonnard0d63b842022-01-18 13:10:56 +01005539
5540#if defined(MBEDTLS_ECP_C)
5541/*
5542 * Same as mbedtls_ssl_check_curve_tls_id() but with a mbedtls_ecp_group_id.
5543 */
Gilles Peskine449bd832023-01-11 14:50:10 +01005544int mbedtls_ssl_check_curve(const mbedtls_ssl_context *ssl, mbedtls_ecp_group_id grp_id)
Manuel Pégourié-Gonnard0d63b842022-01-18 13:10:56 +01005545{
Gilles Peskine449bd832023-01-11 14:50:10 +01005546 uint16_t tls_id = mbedtls_ssl_get_tls_id_from_ecp_group_id(grp_id);
Leonid Rozenboim19e59732022-08-08 16:52:38 -07005547
Gilles Peskine449bd832023-01-11 14:50:10 +01005548 if (tls_id == 0) {
Leonid Rozenboim19e59732022-08-08 16:52:38 -07005549 return -1;
Gilles Peskine449bd832023-01-11 14:50:10 +01005550 }
Leonid Rozenboim19e59732022-08-08 16:52:38 -07005551
Gilles Peskine449bd832023-01-11 14:50:10 +01005552 return mbedtls_ssl_check_curve_tls_id(ssl, tls_id);
Manuel Pégourié-Gonnard0d63b842022-01-18 13:10:56 +01005553}
Manuel Pégourié-Gonnardb541da62015-06-17 11:43:30 +02005554#endif /* MBEDTLS_ECP_C */
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02005555
Gilles Peskine449bd832023-01-11 14:50:10 +01005556#if defined(MBEDTLS_DEBUG_C)
Valerio Setti67419f02023-01-04 16:12:42 +01005557#define EC_NAME(_name_) _name_
5558#else
5559#define EC_NAME(_name_) NULL
5560#endif
5561
Valerio Setti18c9fed2022-12-30 17:44:24 +01005562static const struct {
5563 uint16_t tls_id;
5564 mbedtls_ecp_group_id ecp_group_id;
5565 psa_ecc_family_t psa_family;
5566 uint16_t bits;
Gilles Peskine449bd832023-01-11 14:50:10 +01005567 const char *name;
Valerio Setti18c9fed2022-12-30 17:44:24 +01005568} tls_id_match_table[] =
5569{
5570#if defined(MBEDTLS_ECP_DP_SECP521R1_ENABLED) || defined(PSA_WANT_ECC_SECP_R1_521)
Gilles Peskine449bd832023-01-11 14:50:10 +01005571 { 25, MBEDTLS_ECP_DP_SECP521R1, PSA_ECC_FAMILY_SECP_R1, 521, EC_NAME("secp521r1") },
Valerio Setti18c9fed2022-12-30 17:44:24 +01005572#endif
5573#if defined(MBEDTLS_ECP_DP_BP512R1_ENABLED) || defined(PSA_WANT_ECC_BRAINPOOL_P_R1_512)
Gilles Peskine449bd832023-01-11 14:50:10 +01005574 { 28, MBEDTLS_ECP_DP_BP512R1, PSA_ECC_FAMILY_BRAINPOOL_P_R1, 512, EC_NAME("brainpoolP512r1") },
Valerio Setti18c9fed2022-12-30 17:44:24 +01005575#endif
Valerio Setti67419f02023-01-04 16:12:42 +01005576#if defined(MBEDTLS_ECP_DP_SECP384R1_ENABLED) || defined(PSA_WANT_ECC_SECP_R1_384)
Gilles Peskine449bd832023-01-11 14:50:10 +01005577 { 24, MBEDTLS_ECP_DP_SECP384R1, PSA_ECC_FAMILY_SECP_R1, 384, EC_NAME("secp384r1") },
Valerio Setti18c9fed2022-12-30 17:44:24 +01005578#endif
Valerio Setti67419f02023-01-04 16:12:42 +01005579#if defined(MBEDTLS_ECP_DP_BP384R1_ENABLED) || defined(PSA_WANT_ECC_BRAINPOOL_P_R1_384)
Gilles Peskine449bd832023-01-11 14:50:10 +01005580 { 27, MBEDTLS_ECP_DP_BP384R1, PSA_ECC_FAMILY_BRAINPOOL_P_R1, 384, EC_NAME("brainpoolP384r1") },
Valerio Setti18c9fed2022-12-30 17:44:24 +01005581#endif
Valerio Setti67419f02023-01-04 16:12:42 +01005582#if defined(MBEDTLS_ECP_DP_SECP256R1_ENABLED) || defined(PSA_WANT_ECC_SECP_R1_256)
Gilles Peskine449bd832023-01-11 14:50:10 +01005583 { 23, MBEDTLS_ECP_DP_SECP256R1, PSA_ECC_FAMILY_SECP_R1, 256, EC_NAME("secp256r1") },
Valerio Setti18c9fed2022-12-30 17:44:24 +01005584#endif
5585#if defined(MBEDTLS_ECP_DP_SECP256K1_ENABLED) || defined(PSA_WANT_ECC_SECP_K1_256)
Gilles Peskine449bd832023-01-11 14:50:10 +01005586 { 22, MBEDTLS_ECP_DP_SECP256K1, PSA_ECC_FAMILY_SECP_K1, 256, EC_NAME("secp256k1") },
Valerio Setti18c9fed2022-12-30 17:44:24 +01005587#endif
Valerio Setti67419f02023-01-04 16:12:42 +01005588#if defined(MBEDTLS_ECP_DP_BP256R1_ENABLED) || defined(PSA_WANT_ECC_BRAINPOOL_P_R1_256)
Gilles Peskine449bd832023-01-11 14:50:10 +01005589 { 26, MBEDTLS_ECP_DP_BP256R1, PSA_ECC_FAMILY_BRAINPOOL_P_R1, 256, EC_NAME("brainpoolP256r1") },
Valerio Setti18c9fed2022-12-30 17:44:24 +01005590#endif
5591#if defined(MBEDTLS_ECP_DP_SECP224R1_ENABLED) || defined(PSA_WANT_ECC_SECP_R1_224)
Gilles Peskine449bd832023-01-11 14:50:10 +01005592 { 21, MBEDTLS_ECP_DP_SECP224R1, PSA_ECC_FAMILY_SECP_R1, 224, EC_NAME("secp224r1") },
Valerio Setti18c9fed2022-12-30 17:44:24 +01005593#endif
5594#if defined(MBEDTLS_ECP_DP_SECP224K1_ENABLED) || defined(PSA_WANT_ECC_SECP_K1_224)
Gilles Peskine449bd832023-01-11 14:50:10 +01005595 { 20, MBEDTLS_ECP_DP_SECP224K1, PSA_ECC_FAMILY_SECP_K1, 224, EC_NAME("secp224k1") },
Valerio Setti18c9fed2022-12-30 17:44:24 +01005596#endif
5597#if defined(MBEDTLS_ECP_DP_SECP192R1_ENABLED) || defined(PSA_WANT_ECC_SECP_R1_192)
Gilles Peskine449bd832023-01-11 14:50:10 +01005598 { 19, MBEDTLS_ECP_DP_SECP192R1, PSA_ECC_FAMILY_SECP_R1, 192, EC_NAME("secp192r1") },
Valerio Setti18c9fed2022-12-30 17:44:24 +01005599#endif
5600#if defined(MBEDTLS_ECP_DP_SECP192K1_ENABLED) || defined(PSA_WANT_ECC_SECP_K1_192)
Gilles Peskine449bd832023-01-11 14:50:10 +01005601 { 18, MBEDTLS_ECP_DP_SECP192K1, PSA_ECC_FAMILY_SECP_K1, 192, EC_NAME("secp192k1") },
Valerio Setti18c9fed2022-12-30 17:44:24 +01005602#endif
5603#if defined(MBEDTLS_ECP_DP_CURVE25519_ENABLED) || defined(PSA_WANT_ECC_MONTGOMERY_255)
Gilles Peskine449bd832023-01-11 14:50:10 +01005604 { 29, MBEDTLS_ECP_DP_CURVE25519, PSA_ECC_FAMILY_MONTGOMERY, 255, EC_NAME("x25519") },
Valerio Setti18c9fed2022-12-30 17:44:24 +01005605#endif
5606#if defined(MBEDTLS_ECP_DP_CURVE448_ENABLED) || defined(PSA_WANT_ECC_MONTGOMERY_448)
Gilles Peskine449bd832023-01-11 14:50:10 +01005607 { 30, MBEDTLS_ECP_DP_CURVE448, PSA_ECC_FAMILY_MONTGOMERY, 448, EC_NAME("x448") },
Valerio Setti18c9fed2022-12-30 17:44:24 +01005608#endif
5609 { 0, MBEDTLS_ECP_DP_NONE, 0, 0, NULL },
5610};
5611
Gilles Peskine449bd832023-01-11 14:50:10 +01005612int mbedtls_ssl_get_psa_curve_info_from_tls_id(uint16_t tls_id,
5613 psa_ecc_family_t *family,
5614 size_t *bits)
Valerio Setti18c9fed2022-12-30 17:44:24 +01005615{
Gilles Peskine449bd832023-01-11 14:50:10 +01005616 for (int i = 0; tls_id_match_table[i].tls_id != 0; i++) {
5617 if (tls_id_match_table[i].tls_id == tls_id) {
5618 if (family != NULL) {
Valerio Setti18c9fed2022-12-30 17:44:24 +01005619 *family = tls_id_match_table[i].psa_family;
Gilles Peskine449bd832023-01-11 14:50:10 +01005620 }
5621 if (bits != NULL) {
Valerio Setti18c9fed2022-12-30 17:44:24 +01005622 *bits = tls_id_match_table[i].bits;
Gilles Peskine449bd832023-01-11 14:50:10 +01005623 }
Valerio Setti18c9fed2022-12-30 17:44:24 +01005624 return PSA_SUCCESS;
5625 }
5626 }
5627
5628 return PSA_ERROR_NOT_SUPPORTED;
5629}
5630
Gilles Peskine449bd832023-01-11 14:50:10 +01005631mbedtls_ecp_group_id mbedtls_ssl_get_ecp_group_id_from_tls_id(uint16_t tls_id)
Valerio Setti18c9fed2022-12-30 17:44:24 +01005632{
Gilles Peskine449bd832023-01-11 14:50:10 +01005633 for (int i = 0; tls_id_match_table[i].tls_id != 0; i++) {
5634 if (tls_id_match_table[i].tls_id == tls_id) {
Valerio Setti18c9fed2022-12-30 17:44:24 +01005635 return tls_id_match_table[i].ecp_group_id;
Gilles Peskine449bd832023-01-11 14:50:10 +01005636 }
Valerio Setti18c9fed2022-12-30 17:44:24 +01005637 }
5638
5639 return MBEDTLS_ECP_DP_NONE;
5640}
5641
Gilles Peskine449bd832023-01-11 14:50:10 +01005642uint16_t mbedtls_ssl_get_tls_id_from_ecp_group_id(mbedtls_ecp_group_id grp_id)
Valerio Setti18c9fed2022-12-30 17:44:24 +01005643{
Gilles Peskine449bd832023-01-11 14:50:10 +01005644 for (int i = 0; tls_id_match_table[i].ecp_group_id != MBEDTLS_ECP_DP_NONE;
5645 i++) {
5646 if (tls_id_match_table[i].ecp_group_id == grp_id) {
Valerio Setti18c9fed2022-12-30 17:44:24 +01005647 return tls_id_match_table[i].tls_id;
Gilles Peskine449bd832023-01-11 14:50:10 +01005648 }
Valerio Setti18c9fed2022-12-30 17:44:24 +01005649 }
5650
5651 return 0;
5652}
5653
Valerio Setti67419f02023-01-04 16:12:42 +01005654#if defined(MBEDTLS_DEBUG_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01005655const char *mbedtls_ssl_get_curve_name_from_tls_id(uint16_t tls_id)
Valerio Setti18c9fed2022-12-30 17:44:24 +01005656{
Gilles Peskine449bd832023-01-11 14:50:10 +01005657 for (int i = 0; tls_id_match_table[i].tls_id != 0; i++) {
5658 if (tls_id_match_table[i].tls_id == tls_id) {
Valerio Setti18c9fed2022-12-30 17:44:24 +01005659 return tls_id_match_table[i].name;
Gilles Peskine449bd832023-01-11 14:50:10 +01005660 }
Valerio Setti18c9fed2022-12-30 17:44:24 +01005661 }
5662
5663 return NULL;
5664}
Valerio Setti67419f02023-01-04 16:12:42 +01005665#endif
Valerio Setti18c9fed2022-12-30 17:44:24 +01005666
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005667#if defined(MBEDTLS_X509_CRT_PARSE_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01005668int mbedtls_ssl_check_cert_usage(const mbedtls_x509_crt *cert,
5669 const mbedtls_ssl_ciphersuite_t *ciphersuite,
5670 int cert_endpoint,
5671 uint32_t *flags)
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02005672{
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +01005673 int ret = 0;
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02005674 int usage = 0;
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +02005675 const char *ext_oid;
5676 size_t ext_len;
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +02005677
Gilles Peskine449bd832023-01-11 14:50:10 +01005678 if (cert_endpoint == MBEDTLS_SSL_IS_SERVER) {
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02005679 /* Server part of the key exchange */
Gilles Peskine449bd832023-01-11 14:50:10 +01005680 switch (ciphersuite->key_exchange) {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005681 case MBEDTLS_KEY_EXCHANGE_RSA:
5682 case MBEDTLS_KEY_EXCHANGE_RSA_PSK:
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +01005683 usage = MBEDTLS_X509_KU_KEY_ENCIPHERMENT;
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02005684 break;
5685
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005686 case MBEDTLS_KEY_EXCHANGE_DHE_RSA:
5687 case MBEDTLS_KEY_EXCHANGE_ECDHE_RSA:
5688 case MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA:
5689 usage = MBEDTLS_X509_KU_DIGITAL_SIGNATURE;
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02005690 break;
5691
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005692 case MBEDTLS_KEY_EXCHANGE_ECDH_RSA:
5693 case MBEDTLS_KEY_EXCHANGE_ECDH_ECDSA:
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +01005694 usage = MBEDTLS_X509_KU_KEY_AGREEMENT;
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02005695 break;
5696
5697 /* Don't use default: we want warnings when adding new values */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005698 case MBEDTLS_KEY_EXCHANGE_NONE:
5699 case MBEDTLS_KEY_EXCHANGE_PSK:
5700 case MBEDTLS_KEY_EXCHANGE_DHE_PSK:
5701 case MBEDTLS_KEY_EXCHANGE_ECDHE_PSK:
Manuel Pégourié-Gonnard557535d2015-09-15 17:53:32 +02005702 case MBEDTLS_KEY_EXCHANGE_ECJPAKE:
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02005703 usage = 0;
5704 }
Gilles Peskine449bd832023-01-11 14:50:10 +01005705 } else {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005706 /* Client auth: we only implement rsa_sign and mbedtls_ecdsa_sign for now */
5707 usage = MBEDTLS_X509_KU_DIGITAL_SIGNATURE;
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02005708 }
5709
Gilles Peskine449bd832023-01-11 14:50:10 +01005710 if (mbedtls_x509_crt_check_key_usage(cert, usage) != 0) {
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +01005711 *flags |= MBEDTLS_X509_BADCERT_KEY_USAGE;
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +01005712 ret = -1;
5713 }
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02005714
Gilles Peskine449bd832023-01-11 14:50:10 +01005715 if (cert_endpoint == MBEDTLS_SSL_IS_SERVER) {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005716 ext_oid = MBEDTLS_OID_SERVER_AUTH;
Gilles Peskine449bd832023-01-11 14:50:10 +01005717 ext_len = MBEDTLS_OID_SIZE(MBEDTLS_OID_SERVER_AUTH);
5718 } else {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005719 ext_oid = MBEDTLS_OID_CLIENT_AUTH;
Gilles Peskine449bd832023-01-11 14:50:10 +01005720 ext_len = MBEDTLS_OID_SIZE(MBEDTLS_OID_CLIENT_AUTH);
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +02005721 }
5722
Gilles Peskine449bd832023-01-11 14:50:10 +01005723 if (mbedtls_x509_crt_check_extended_key_usage(cert, ext_oid, ext_len) != 0) {
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +01005724 *flags |= MBEDTLS_X509_BADCERT_EXT_KEY_USAGE;
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +01005725 ret = -1;
5726 }
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +02005727
Gilles Peskine449bd832023-01-11 14:50:10 +01005728 return ret;
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02005729}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005730#endif /* MBEDTLS_X509_CRT_PARSE_C */
Manuel Pégourié-Gonnard3a306b92014-04-29 15:11:17 +02005731
Jerry Yu148165c2021-09-24 23:20:59 +08005732#if defined(MBEDTLS_USE_PSA_CRYPTO)
Gilles Peskine449bd832023-01-11 14:50:10 +01005733int mbedtls_ssl_get_handshake_transcript(mbedtls_ssl_context *ssl,
5734 const mbedtls_md_type_t md,
5735 unsigned char *dst,
5736 size_t dst_len,
5737 size_t *olen)
Jerry Yu148165c2021-09-24 23:20:59 +08005738{
Ronald Cronf6893e12022-01-07 22:09:01 +01005739 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
5740 psa_hash_operation_t *hash_operation_to_clone;
5741 psa_hash_operation_t hash_operation = psa_hash_operation_init();
5742
Jerry Yu148165c2021-09-24 23:20:59 +08005743 *olen = 0;
Ronald Cronf6893e12022-01-07 22:09:01 +01005744
Gilles Peskine449bd832023-01-11 14:50:10 +01005745 switch (md) {
Andrzej Kurek25f27152022-08-17 16:09:31 -04005746#if defined(MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Gilles Peskine449bd832023-01-11 14:50:10 +01005747 case MBEDTLS_MD_SHA384:
5748 hash_operation_to_clone = &ssl->handshake->fin_sha384_psa;
5749 break;
Ronald Cronf6893e12022-01-07 22:09:01 +01005750#endif
5751
Andrzej Kurek25f27152022-08-17 16:09:31 -04005752#if defined(MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Gilles Peskine449bd832023-01-11 14:50:10 +01005753 case MBEDTLS_MD_SHA256:
5754 hash_operation_to_clone = &ssl->handshake->fin_sha256_psa;
5755 break;
Ronald Cronf6893e12022-01-07 22:09:01 +01005756#endif
5757
Gilles Peskine449bd832023-01-11 14:50:10 +01005758 default:
5759 goto exit;
5760 }
5761
5762 status = psa_hash_clone(hash_operation_to_clone, &hash_operation);
5763 if (status != PSA_SUCCESS) {
Ronald Cronf6893e12022-01-07 22:09:01 +01005764 goto exit;
5765 }
5766
Gilles Peskine449bd832023-01-11 14:50:10 +01005767 status = psa_hash_finish(&hash_operation, dst, dst_len, olen);
5768 if (status != PSA_SUCCESS) {
Ronald Cronf6893e12022-01-07 22:09:01 +01005769 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01005770 }
Ronald Cronf6893e12022-01-07 22:09:01 +01005771
5772exit:
Andrzej Kurekeabeb302022-10-17 07:52:51 -04005773#if !defined(MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA) && \
5774 !defined(MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
5775 (void) ssl;
5776#endif
Gilles Peskine449bd832023-01-11 14:50:10 +01005777 return psa_ssl_status_to_mbedtls(status);
Jerry Yu148165c2021-09-24 23:20:59 +08005778}
5779#else /* MBEDTLS_USE_PSA_CRYPTO */
5780
Andrzej Kurek25f27152022-08-17 16:09:31 -04005781#if defined(MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02005782MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01005783static int ssl_get_handshake_transcript_sha384(mbedtls_ssl_context *ssl,
5784 unsigned char *dst,
5785 size_t dst_len,
5786 size_t *olen)
Jerry Yu24c0ec32021-09-09 14:21:07 +08005787{
Jerry Yu24c0ec32021-09-09 14:21:07 +08005788 int ret;
Manuel Pégourié-Gonnard02d55d52023-02-24 13:21:16 +01005789 mbedtls_md_context_t sha384;
Jerry Yu24c0ec32021-09-09 14:21:07 +08005790
Gilles Peskine449bd832023-01-11 14:50:10 +01005791 if (dst_len < 48) {
5792 return MBEDTLS_ERR_SSL_INTERNAL_ERROR;
5793 }
Jerry Yu24c0ec32021-09-09 14:21:07 +08005794
Manuel Pégourié-Gonnard02d55d52023-02-24 13:21:16 +01005795 mbedtls_md_init(&sha384);
5796 ret = mbedtls_md_setup(&sha384, mbedtls_md_info_from_type(MBEDTLS_MD_SHA384), 0);
Manuel Pégourié-Gonnardf057ecf2023-02-24 13:19:17 +01005797 if (ret != 0) {
5798 goto exit;
5799 }
Manuel Pégourié-Gonnard02d55d52023-02-24 13:21:16 +01005800 ret = mbedtls_md_clone(&sha384, &ssl->handshake->fin_sha384);
Manuel Pégourié-Gonnardf057ecf2023-02-24 13:19:17 +01005801 if (ret != 0) {
5802 goto exit;
5803 }
Jerry Yu24c0ec32021-09-09 14:21:07 +08005804
Manuel Pégourié-Gonnard02d55d52023-02-24 13:21:16 +01005805 if ((ret = mbedtls_md_finish(&sha384, dst)) != 0) {
Manuel Pégourié-Gonnardf057ecf2023-02-24 13:19:17 +01005806 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_md_finish", ret);
Jerry Yu24c0ec32021-09-09 14:21:07 +08005807 goto exit;
5808 }
5809
5810 *olen = 48;
5811
5812exit:
5813
Manuel Pégourié-Gonnard02d55d52023-02-24 13:21:16 +01005814 mbedtls_md_free(&sha384);
Gilles Peskine449bd832023-01-11 14:50:10 +01005815 return ret;
Jerry Yu24c0ec32021-09-09 14:21:07 +08005816}
Andrzej Kurek25f27152022-08-17 16:09:31 -04005817#endif /* MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA */
Jerry Yu24c0ec32021-09-09 14:21:07 +08005818
Andrzej Kurek25f27152022-08-17 16:09:31 -04005819#if defined(MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02005820MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01005821static int ssl_get_handshake_transcript_sha256(mbedtls_ssl_context *ssl,
5822 unsigned char *dst,
5823 size_t dst_len,
5824 size_t *olen)
Jerry Yu24c0ec32021-09-09 14:21:07 +08005825{
Jerry Yu24c0ec32021-09-09 14:21:07 +08005826 int ret;
Manuel Pégourié-Gonnardf057ecf2023-02-24 13:19:17 +01005827 mbedtls_md_context_t sha256;
Jerry Yu24c0ec32021-09-09 14:21:07 +08005828
Gilles Peskine449bd832023-01-11 14:50:10 +01005829 if (dst_len < 32) {
5830 return MBEDTLS_ERR_SSL_INTERNAL_ERROR;
5831 }
Jerry Yu24c0ec32021-09-09 14:21:07 +08005832
Manuel Pégourié-Gonnardf057ecf2023-02-24 13:19:17 +01005833 mbedtls_md_init(&sha256);
5834 ret = mbedtls_md_setup(&sha256, mbedtls_md_info_from_type(MBEDTLS_MD_SHA256), 0);
5835 if (ret != 0) {
5836 goto exit;
5837 }
5838 ret = mbedtls_md_clone(&sha256, &ssl->handshake->fin_sha256);
5839 if (ret != 0) {
5840 goto exit;
5841 }
Jerry Yuc5aef882021-12-23 20:15:02 +08005842
Manuel Pégourié-Gonnardf057ecf2023-02-24 13:19:17 +01005843 if ((ret = mbedtls_md_finish(&sha256, dst)) != 0) {
5844 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_md_finish", ret);
Jerry Yu24c0ec32021-09-09 14:21:07 +08005845 goto exit;
5846 }
5847
5848 *olen = 32;
5849
5850exit:
5851
Manuel Pégourié-Gonnardf057ecf2023-02-24 13:19:17 +01005852 mbedtls_md_free(&sha256);
Gilles Peskine449bd832023-01-11 14:50:10 +01005853 return ret;
Jerry Yu24c0ec32021-09-09 14:21:07 +08005854}
Andrzej Kurek25f27152022-08-17 16:09:31 -04005855#endif /* MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA */
Jerry Yu24c0ec32021-09-09 14:21:07 +08005856
Gilles Peskine449bd832023-01-11 14:50:10 +01005857int mbedtls_ssl_get_handshake_transcript(mbedtls_ssl_context *ssl,
5858 const mbedtls_md_type_t md,
5859 unsigned char *dst,
5860 size_t dst_len,
5861 size_t *olen)
Jerry Yu24c0ec32021-09-09 14:21:07 +08005862{
Gilles Peskine449bd832023-01-11 14:50:10 +01005863 switch (md) {
Jerry Yuc1ddeef2021-10-08 15:14:45 +08005864
Andrzej Kurek25f27152022-08-17 16:09:31 -04005865#if defined(MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Gilles Peskine449bd832023-01-11 14:50:10 +01005866 case MBEDTLS_MD_SHA384:
5867 return ssl_get_handshake_transcript_sha384(ssl, dst, dst_len, olen);
Andrzej Kurekcccb0442022-08-19 03:42:11 -04005868#endif /* MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA*/
Jerry Yuc1ddeef2021-10-08 15:14:45 +08005869
Andrzej Kurek25f27152022-08-17 16:09:31 -04005870#if defined(MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Gilles Peskine449bd832023-01-11 14:50:10 +01005871 case MBEDTLS_MD_SHA256:
5872 return ssl_get_handshake_transcript_sha256(ssl, dst, dst_len, olen);
Andrzej Kurekcccb0442022-08-19 03:42:11 -04005873#endif /* MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA*/
Jerry Yuc1ddeef2021-10-08 15:14:45 +08005874
Gilles Peskine449bd832023-01-11 14:50:10 +01005875 default:
Andrzej Kurek409248a2022-10-24 10:33:21 -04005876#if !defined(MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA) && \
Gilles Peskine449bd832023-01-11 14:50:10 +01005877 !defined(MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
5878 (void) ssl;
5879 (void) dst;
5880 (void) dst_len;
5881 (void) olen;
Andrzej Kurek409248a2022-10-24 10:33:21 -04005882#endif
Gilles Peskine449bd832023-01-11 14:50:10 +01005883 break;
Jerry Yuc1ddeef2021-10-08 15:14:45 +08005884 }
Gilles Peskine449bd832023-01-11 14:50:10 +01005885 return MBEDTLS_ERR_SSL_INTERNAL_ERROR;
Jerry Yu24c0ec32021-09-09 14:21:07 +08005886}
XiaokangQian647719a2021-12-07 09:16:29 +00005887
Jerry Yu148165c2021-09-24 23:20:59 +08005888#endif /* !MBEDTLS_USE_PSA_CRYPTO */
Jerry Yu24c0ec32021-09-09 14:21:07 +08005889
Ronald Crone68ab4f2022-10-05 12:46:29 +02005890#if defined(MBEDTLS_SSL_HANDSHAKE_WITH_CERT_ENABLED)
Gabor Mezei078e8032022-04-27 21:17:56 +02005891/* mbedtls_ssl_parse_sig_alg_ext()
5892 *
5893 * The `extension_data` field of signature algorithm contains a `SignatureSchemeList`
5894 * value (TLS 1.3 RFC8446):
5895 * enum {
5896 * ....
5897 * ecdsa_secp256r1_sha256( 0x0403 ),
5898 * ecdsa_secp384r1_sha384( 0x0503 ),
5899 * ecdsa_secp521r1_sha512( 0x0603 ),
5900 * ....
5901 * } SignatureScheme;
5902 *
5903 * struct {
5904 * SignatureScheme supported_signature_algorithms<2..2^16-2>;
5905 * } SignatureSchemeList;
5906 *
5907 * The `extension_data` field of signature algorithm contains a `SignatureAndHashAlgorithm`
5908 * value (TLS 1.2 RFC5246):
5909 * enum {
5910 * none(0), md5(1), sha1(2), sha224(3), sha256(4), sha384(5),
5911 * sha512(6), (255)
5912 * } HashAlgorithm;
5913 *
5914 * enum { anonymous(0), rsa(1), dsa(2), ecdsa(3), (255) }
5915 * SignatureAlgorithm;
5916 *
5917 * struct {
5918 * HashAlgorithm hash;
5919 * SignatureAlgorithm signature;
5920 * } SignatureAndHashAlgorithm;
5921 *
5922 * SignatureAndHashAlgorithm
5923 * supported_signature_algorithms<2..2^16-2>;
5924 *
5925 * The TLS 1.3 signature algorithm extension was defined to be a compatible
5926 * generalization of the TLS 1.2 signature algorithm extension.
5927 * `SignatureAndHashAlgorithm` field of TLS 1.2 can be represented by
5928 * `SignatureScheme` field of TLS 1.3
5929 *
5930 */
Gilles Peskine449bd832023-01-11 14:50:10 +01005931int mbedtls_ssl_parse_sig_alg_ext(mbedtls_ssl_context *ssl,
5932 const unsigned char *buf,
5933 const unsigned char *end)
Gabor Mezei078e8032022-04-27 21:17:56 +02005934{
5935 const unsigned char *p = buf;
5936 size_t supported_sig_algs_len = 0;
5937 const unsigned char *supported_sig_algs_end;
5938 uint16_t sig_alg;
5939 uint32_t common_idx = 0;
5940
Gilles Peskine449bd832023-01-11 14:50:10 +01005941 MBEDTLS_SSL_CHK_BUF_READ_PTR(p, end, 2);
5942 supported_sig_algs_len = MBEDTLS_GET_UINT16_BE(p, 0);
Gabor Mezei078e8032022-04-27 21:17:56 +02005943 p += 2;
5944
Gilles Peskine449bd832023-01-11 14:50:10 +01005945 memset(ssl->handshake->received_sig_algs, 0,
5946 sizeof(ssl->handshake->received_sig_algs));
Gabor Mezei078e8032022-04-27 21:17:56 +02005947
Gilles Peskine449bd832023-01-11 14:50:10 +01005948 MBEDTLS_SSL_CHK_BUF_READ_PTR(p, end, supported_sig_algs_len);
Gabor Mezei078e8032022-04-27 21:17:56 +02005949 supported_sig_algs_end = p + supported_sig_algs_len;
Gilles Peskine449bd832023-01-11 14:50:10 +01005950 while (p < supported_sig_algs_end) {
5951 MBEDTLS_SSL_CHK_BUF_READ_PTR(p, supported_sig_algs_end, 2);
5952 sig_alg = MBEDTLS_GET_UINT16_BE(p, 0);
Gabor Mezei078e8032022-04-27 21:17:56 +02005953 p += 2;
Gilles Peskine449bd832023-01-11 14:50:10 +01005954 MBEDTLS_SSL_DEBUG_MSG(4, ("received signature algorithm: 0x%x %s",
5955 sig_alg,
5956 mbedtls_ssl_sig_alg_to_str(sig_alg)));
Jerry Yu2fe6c632022-06-29 10:02:38 +08005957#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
Gilles Peskine449bd832023-01-11 14:50:10 +01005958 if (ssl->tls_version == MBEDTLS_SSL_VERSION_TLS1_2 &&
5959 (!(mbedtls_ssl_sig_alg_is_supported(ssl, sig_alg) &&
5960 mbedtls_ssl_sig_alg_is_offered(ssl, sig_alg)))) {
Gabor Mezei078e8032022-04-27 21:17:56 +02005961 continue;
Jerry Yu2fe6c632022-06-29 10:02:38 +08005962 }
5963#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Gabor Mezei078e8032022-04-27 21:17:56 +02005964
Gilles Peskine449bd832023-01-11 14:50:10 +01005965 MBEDTLS_SSL_DEBUG_MSG(4, ("valid signature algorithm: %s",
5966 mbedtls_ssl_sig_alg_to_str(sig_alg)));
Gabor Mezei078e8032022-04-27 21:17:56 +02005967
Gilles Peskine449bd832023-01-11 14:50:10 +01005968 if (common_idx + 1 < MBEDTLS_RECEIVED_SIG_ALGS_SIZE) {
Gabor Mezei078e8032022-04-27 21:17:56 +02005969 ssl->handshake->received_sig_algs[common_idx] = sig_alg;
5970 common_idx += 1;
5971 }
5972 }
5973 /* Check that we consumed all the message. */
Gilles Peskine449bd832023-01-11 14:50:10 +01005974 if (p != end) {
5975 MBEDTLS_SSL_DEBUG_MSG(1,
5976 ("Signature algorithms extension length misaligned"));
5977 MBEDTLS_SSL_PEND_FATAL_ALERT(MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR,
5978 MBEDTLS_ERR_SSL_DECODE_ERROR);
5979 return MBEDTLS_ERR_SSL_DECODE_ERROR;
Gabor Mezei078e8032022-04-27 21:17:56 +02005980 }
5981
Gilles Peskine449bd832023-01-11 14:50:10 +01005982 if (common_idx == 0) {
5983 MBEDTLS_SSL_DEBUG_MSG(3, ("no signature algorithm in common"));
5984 MBEDTLS_SSL_PEND_FATAL_ALERT(MBEDTLS_SSL_ALERT_MSG_HANDSHAKE_FAILURE,
5985 MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE);
5986 return MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE;
Gabor Mezei078e8032022-04-27 21:17:56 +02005987 }
5988
Gabor Mezei15b95a62022-05-09 16:37:58 +02005989 ssl->handshake->received_sig_algs[common_idx] = MBEDTLS_TLS_SIG_NONE;
Gilles Peskine449bd832023-01-11 14:50:10 +01005990 return 0;
Gabor Mezei078e8032022-04-27 21:17:56 +02005991}
5992
Ronald Crone68ab4f2022-10-05 12:46:29 +02005993#endif /* MBEDTLS_SSL_HANDSHAKE_WITH_CERT_ENABLED */
Gabor Mezei078e8032022-04-27 21:17:56 +02005994
Jerry Yudc7bd172022-02-17 13:44:15 +08005995#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
5996
5997#if defined(MBEDTLS_USE_PSA_CRYPTO)
5998
Gilles Peskine449bd832023-01-11 14:50:10 +01005999static psa_status_t setup_psa_key_derivation(psa_key_derivation_operation_t *derivation,
6000 mbedtls_svc_key_id_t key,
6001 psa_algorithm_t alg,
6002 const unsigned char *raw_psk, size_t raw_psk_length,
6003 const unsigned char *seed, size_t seed_length,
6004 const unsigned char *label, size_t label_length,
6005 const unsigned char *other_secret,
6006 size_t other_secret_length,
6007 size_t capacity)
Jerry Yudc7bd172022-02-17 13:44:15 +08006008{
6009 psa_status_t status;
6010
Gilles Peskine449bd832023-01-11 14:50:10 +01006011 status = psa_key_derivation_setup(derivation, alg);
6012 if (status != PSA_SUCCESS) {
6013 return status;
6014 }
Jerry Yudc7bd172022-02-17 13:44:15 +08006015
Gilles Peskine449bd832023-01-11 14:50:10 +01006016 if (PSA_ALG_IS_TLS12_PRF(alg) || PSA_ALG_IS_TLS12_PSK_TO_MS(alg)) {
6017 status = psa_key_derivation_input_bytes(derivation,
6018 PSA_KEY_DERIVATION_INPUT_SEED,
6019 seed, seed_length);
6020 if (status != PSA_SUCCESS) {
6021 return status;
Przemek Stekiel1f027032022-04-05 17:12:11 +02006022 }
6023
Gilles Peskine449bd832023-01-11 14:50:10 +01006024 if (other_secret != NULL) {
6025 status = psa_key_derivation_input_bytes(derivation,
6026 PSA_KEY_DERIVATION_INPUT_OTHER_SECRET,
6027 other_secret, other_secret_length);
6028 if (status != PSA_SUCCESS) {
6029 return status;
6030 }
6031 }
6032
6033 if (mbedtls_svc_key_id_is_null(key)) {
Jerry Yudc7bd172022-02-17 13:44:15 +08006034 status = psa_key_derivation_input_bytes(
6035 derivation, PSA_KEY_DERIVATION_INPUT_SECRET,
Gilles Peskine449bd832023-01-11 14:50:10 +01006036 raw_psk, raw_psk_length);
6037 } else {
Jerry Yudc7bd172022-02-17 13:44:15 +08006038 status = psa_key_derivation_input_key(
Gilles Peskine449bd832023-01-11 14:50:10 +01006039 derivation, PSA_KEY_DERIVATION_INPUT_SECRET, key);
Jerry Yudc7bd172022-02-17 13:44:15 +08006040 }
Gilles Peskine449bd832023-01-11 14:50:10 +01006041 if (status != PSA_SUCCESS) {
6042 return status;
6043 }
Jerry Yudc7bd172022-02-17 13:44:15 +08006044
Gilles Peskine449bd832023-01-11 14:50:10 +01006045 status = psa_key_derivation_input_bytes(derivation,
6046 PSA_KEY_DERIVATION_INPUT_LABEL,
6047 label, label_length);
6048 if (status != PSA_SUCCESS) {
6049 return status;
6050 }
6051 } else {
6052 return PSA_ERROR_NOT_SUPPORTED;
Jerry Yudc7bd172022-02-17 13:44:15 +08006053 }
6054
Gilles Peskine449bd832023-01-11 14:50:10 +01006055 status = psa_key_derivation_set_capacity(derivation, capacity);
6056 if (status != PSA_SUCCESS) {
6057 return status;
6058 }
Jerry Yudc7bd172022-02-17 13:44:15 +08006059
Gilles Peskine449bd832023-01-11 14:50:10 +01006060 return PSA_SUCCESS;
Jerry Yudc7bd172022-02-17 13:44:15 +08006061}
6062
Andrzej Kurek57d10632022-10-24 10:32:01 -04006063#if defined(PSA_WANT_ALG_SHA_384) || \
6064 defined(PSA_WANT_ALG_SHA_256)
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02006065MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01006066static int tls_prf_generic(mbedtls_md_type_t md_type,
6067 const unsigned char *secret, size_t slen,
6068 const char *label,
6069 const unsigned char *random, size_t rlen,
6070 unsigned char *dstbuf, size_t dlen)
Jerry Yudc7bd172022-02-17 13:44:15 +08006071{
6072 psa_status_t status;
6073 psa_algorithm_t alg;
6074 mbedtls_svc_key_id_t master_key = MBEDTLS_SVC_KEY_ID_INIT;
6075 psa_key_derivation_operation_t derivation =
6076 PSA_KEY_DERIVATION_OPERATION_INIT;
6077
Gilles Peskine449bd832023-01-11 14:50:10 +01006078 if (md_type == MBEDTLS_MD_SHA384) {
Jerry Yudc7bd172022-02-17 13:44:15 +08006079 alg = PSA_ALG_TLS12_PRF(PSA_ALG_SHA_384);
Gilles Peskine449bd832023-01-11 14:50:10 +01006080 } else {
Jerry Yudc7bd172022-02-17 13:44:15 +08006081 alg = PSA_ALG_TLS12_PRF(PSA_ALG_SHA_256);
Gilles Peskine449bd832023-01-11 14:50:10 +01006082 }
Jerry Yudc7bd172022-02-17 13:44:15 +08006083
6084 /* Normally a "secret" should be long enough to be impossible to
6085 * find by brute force, and in particular should not be empty. But
6086 * this PRF is also used to derive an IV, in particular in EAP-TLS,
6087 * and for this use case it makes sense to have a 0-length "secret".
6088 * Since the key API doesn't allow importing a key of length 0,
6089 * keep master_key=0, which setup_psa_key_derivation() understands
6090 * to mean a 0-length "secret" input. */
Gilles Peskine449bd832023-01-11 14:50:10 +01006091 if (slen != 0) {
Jerry Yudc7bd172022-02-17 13:44:15 +08006092 psa_key_attributes_t key_attributes = psa_key_attributes_init();
Gilles Peskine449bd832023-01-11 14:50:10 +01006093 psa_set_key_usage_flags(&key_attributes, PSA_KEY_USAGE_DERIVE);
6094 psa_set_key_algorithm(&key_attributes, alg);
6095 psa_set_key_type(&key_attributes, PSA_KEY_TYPE_DERIVE);
Jerry Yudc7bd172022-02-17 13:44:15 +08006096
Gilles Peskine449bd832023-01-11 14:50:10 +01006097 status = psa_import_key(&key_attributes, secret, slen, &master_key);
6098 if (status != PSA_SUCCESS) {
6099 return MBEDTLS_ERR_SSL_HW_ACCEL_FAILED;
6100 }
Jerry Yudc7bd172022-02-17 13:44:15 +08006101 }
6102
Gilles Peskine449bd832023-01-11 14:50:10 +01006103 status = setup_psa_key_derivation(&derivation,
6104 master_key, alg,
6105 NULL, 0,
6106 random, rlen,
6107 (unsigned char const *) label,
6108 (size_t) strlen(label),
6109 NULL, 0,
6110 dlen);
6111 if (status != PSA_SUCCESS) {
6112 psa_key_derivation_abort(&derivation);
6113 psa_destroy_key(master_key);
6114 return MBEDTLS_ERR_SSL_HW_ACCEL_FAILED;
Jerry Yudc7bd172022-02-17 13:44:15 +08006115 }
6116
Gilles Peskine449bd832023-01-11 14:50:10 +01006117 status = psa_key_derivation_output_bytes(&derivation, dstbuf, dlen);
6118 if (status != PSA_SUCCESS) {
6119 psa_key_derivation_abort(&derivation);
6120 psa_destroy_key(master_key);
6121 return MBEDTLS_ERR_SSL_HW_ACCEL_FAILED;
Jerry Yudc7bd172022-02-17 13:44:15 +08006122 }
6123
Gilles Peskine449bd832023-01-11 14:50:10 +01006124 status = psa_key_derivation_abort(&derivation);
6125 if (status != PSA_SUCCESS) {
6126 psa_destroy_key(master_key);
6127 return MBEDTLS_ERR_SSL_HW_ACCEL_FAILED;
Jerry Yudc7bd172022-02-17 13:44:15 +08006128 }
6129
Gilles Peskine449bd832023-01-11 14:50:10 +01006130 if (!mbedtls_svc_key_id_is_null(master_key)) {
6131 status = psa_destroy_key(master_key);
6132 }
6133 if (status != PSA_SUCCESS) {
6134 return MBEDTLS_ERR_SSL_HW_ACCEL_FAILED;
6135 }
Jerry Yudc7bd172022-02-17 13:44:15 +08006136
Gilles Peskine449bd832023-01-11 14:50:10 +01006137 return 0;
Jerry Yudc7bd172022-02-17 13:44:15 +08006138}
Andrzej Kurek57d10632022-10-24 10:32:01 -04006139#endif /* PSA_WANT_ALG_SHA_256 || PSA_WANT_ALG_SHA_384 */
Jerry Yudc7bd172022-02-17 13:44:15 +08006140#else /* MBEDTLS_USE_PSA_CRYPTO */
6141
Andrzej Kurek57d10632022-10-24 10:32:01 -04006142#if defined(MBEDTLS_MD_C) && \
Gilles Peskine449bd832023-01-11 14:50:10 +01006143 (defined(MBEDTLS_SHA256_C) || \
6144 defined(MBEDTLS_SHA384_C))
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02006145MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01006146static int tls_prf_generic(mbedtls_md_type_t md_type,
6147 const unsigned char *secret, size_t slen,
6148 const char *label,
6149 const unsigned char *random, size_t rlen,
6150 unsigned char *dstbuf, size_t dlen)
Jerry Yudc7bd172022-02-17 13:44:15 +08006151{
6152 size_t nb;
6153 size_t i, j, k, md_len;
6154 unsigned char *tmp;
6155 size_t tmp_len = 0;
6156 unsigned char h_i[MBEDTLS_MD_MAX_SIZE];
6157 const mbedtls_md_info_t *md_info;
6158 mbedtls_md_context_t md_ctx;
6159 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
6160
Gilles Peskine449bd832023-01-11 14:50:10 +01006161 mbedtls_md_init(&md_ctx);
Jerry Yudc7bd172022-02-17 13:44:15 +08006162
Gilles Peskine449bd832023-01-11 14:50:10 +01006163 if ((md_info = mbedtls_md_info_from_type(md_type)) == NULL) {
6164 return MBEDTLS_ERR_SSL_INTERNAL_ERROR;
6165 }
Jerry Yudc7bd172022-02-17 13:44:15 +08006166
Gilles Peskine449bd832023-01-11 14:50:10 +01006167 md_len = mbedtls_md_get_size(md_info);
Jerry Yudc7bd172022-02-17 13:44:15 +08006168
Gilles Peskine449bd832023-01-11 14:50:10 +01006169 tmp_len = md_len + strlen(label) + rlen;
6170 tmp = mbedtls_calloc(1, tmp_len);
6171 if (tmp == NULL) {
Jerry Yudc7bd172022-02-17 13:44:15 +08006172 ret = MBEDTLS_ERR_SSL_ALLOC_FAILED;
6173 goto exit;
6174 }
6175
Gilles Peskine449bd832023-01-11 14:50:10 +01006176 nb = strlen(label);
6177 memcpy(tmp + md_len, label, nb);
6178 memcpy(tmp + md_len + nb, random, rlen);
Jerry Yudc7bd172022-02-17 13:44:15 +08006179 nb += rlen;
6180
6181 /*
6182 * Compute P_<hash>(secret, label + random)[0..dlen]
6183 */
Gilles Peskine449bd832023-01-11 14:50:10 +01006184 if ((ret = mbedtls_md_setup(&md_ctx, md_info, 1)) != 0) {
Jerry Yudc7bd172022-02-17 13:44:15 +08006185 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01006186 }
Jerry Yudc7bd172022-02-17 13:44:15 +08006187
Gilles Peskine449bd832023-01-11 14:50:10 +01006188 ret = mbedtls_md_hmac_starts(&md_ctx, secret, slen);
6189 if (ret != 0) {
Jerry Yudc7bd172022-02-17 13:44:15 +08006190 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01006191 }
6192 ret = mbedtls_md_hmac_update(&md_ctx, tmp + md_len, nb);
6193 if (ret != 0) {
Jerry Yudc7bd172022-02-17 13:44:15 +08006194 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01006195 }
6196 ret = mbedtls_md_hmac_finish(&md_ctx, tmp);
6197 if (ret != 0) {
Jerry Yudc7bd172022-02-17 13:44:15 +08006198 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01006199 }
Jerry Yudc7bd172022-02-17 13:44:15 +08006200
Gilles Peskine449bd832023-01-11 14:50:10 +01006201 for (i = 0; i < dlen; i += md_len) {
6202 ret = mbedtls_md_hmac_reset(&md_ctx);
6203 if (ret != 0) {
Jerry Yudc7bd172022-02-17 13:44:15 +08006204 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01006205 }
6206 ret = mbedtls_md_hmac_update(&md_ctx, tmp, md_len + nb);
6207 if (ret != 0) {
Jerry Yudc7bd172022-02-17 13:44:15 +08006208 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01006209 }
6210 ret = mbedtls_md_hmac_finish(&md_ctx, h_i);
6211 if (ret != 0) {
Jerry Yudc7bd172022-02-17 13:44:15 +08006212 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01006213 }
Jerry Yudc7bd172022-02-17 13:44:15 +08006214
Gilles Peskine449bd832023-01-11 14:50:10 +01006215 ret = mbedtls_md_hmac_reset(&md_ctx);
6216 if (ret != 0) {
Jerry Yudc7bd172022-02-17 13:44:15 +08006217 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01006218 }
6219 ret = mbedtls_md_hmac_update(&md_ctx, tmp, md_len);
6220 if (ret != 0) {
Jerry Yudc7bd172022-02-17 13:44:15 +08006221 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01006222 }
6223 ret = mbedtls_md_hmac_finish(&md_ctx, tmp);
6224 if (ret != 0) {
Jerry Yudc7bd172022-02-17 13:44:15 +08006225 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01006226 }
Jerry Yudc7bd172022-02-17 13:44:15 +08006227
Gilles Peskine449bd832023-01-11 14:50:10 +01006228 k = (i + md_len > dlen) ? dlen % md_len : md_len;
Jerry Yudc7bd172022-02-17 13:44:15 +08006229
Gilles Peskine449bd832023-01-11 14:50:10 +01006230 for (j = 0; j < k; j++) {
Jerry Yudc7bd172022-02-17 13:44:15 +08006231 dstbuf[i + j] = h_i[j];
Gilles Peskine449bd832023-01-11 14:50:10 +01006232 }
Jerry Yudc7bd172022-02-17 13:44:15 +08006233 }
6234
6235exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01006236 mbedtls_md_free(&md_ctx);
Jerry Yudc7bd172022-02-17 13:44:15 +08006237
Gilles Peskine449bd832023-01-11 14:50:10 +01006238 if (tmp != NULL) {
6239 mbedtls_platform_zeroize(tmp, tmp_len);
6240 }
Dave Rodgman29b9b2b2022-11-01 16:08:14 +00006241
Gilles Peskine449bd832023-01-11 14:50:10 +01006242 mbedtls_platform_zeroize(h_i, sizeof(h_i));
Jerry Yudc7bd172022-02-17 13:44:15 +08006243
Gilles Peskine449bd832023-01-11 14:50:10 +01006244 mbedtls_free(tmp);
Jerry Yudc7bd172022-02-17 13:44:15 +08006245
Gilles Peskine449bd832023-01-11 14:50:10 +01006246 return ret;
Jerry Yudc7bd172022-02-17 13:44:15 +08006247}
Andrzej Kurek57d10632022-10-24 10:32:01 -04006248#endif /* MBEDTLS_MD_C && ( MBEDTLS_SHA256_C || MBEDTLS_SHA384_C ) */
Jerry Yudc7bd172022-02-17 13:44:15 +08006249#endif /* MBEDTLS_USE_PSA_CRYPTO */
6250
Andrzej Kurek25f27152022-08-17 16:09:31 -04006251#if defined(MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02006252MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01006253static int tls_prf_sha256(const unsigned char *secret, size_t slen,
6254 const char *label,
6255 const unsigned char *random, size_t rlen,
6256 unsigned char *dstbuf, size_t dlen)
Jerry Yudc7bd172022-02-17 13:44:15 +08006257{
Gilles Peskine449bd832023-01-11 14:50:10 +01006258 return tls_prf_generic(MBEDTLS_MD_SHA256, secret, slen,
6259 label, random, rlen, dstbuf, dlen);
Jerry Yudc7bd172022-02-17 13:44:15 +08006260}
Andrzej Kurekcccb0442022-08-19 03:42:11 -04006261#endif /* MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA*/
Jerry Yudc7bd172022-02-17 13:44:15 +08006262
Andrzej Kurek25f27152022-08-17 16:09:31 -04006263#if defined(MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02006264MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01006265static int tls_prf_sha384(const unsigned char *secret, size_t slen,
6266 const char *label,
6267 const unsigned char *random, size_t rlen,
6268 unsigned char *dstbuf, size_t dlen)
Jerry Yudc7bd172022-02-17 13:44:15 +08006269{
Gilles Peskine449bd832023-01-11 14:50:10 +01006270 return tls_prf_generic(MBEDTLS_MD_SHA384, secret, slen,
6271 label, random, rlen, dstbuf, dlen);
Jerry Yudc7bd172022-02-17 13:44:15 +08006272}
Andrzej Kurekcccb0442022-08-19 03:42:11 -04006273#endif /* MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA*/
Jerry Yudc7bd172022-02-17 13:44:15 +08006274
Jerry Yuf009d862022-02-17 14:01:37 +08006275/*
6276 * Set appropriate PRF function and other SSL / TLS1.2 functions
6277 *
6278 * Inputs:
Jerry Yuf009d862022-02-17 14:01:37 +08006279 * - hash associated with the ciphersuite (only used by TLS 1.2)
6280 *
6281 * Outputs:
6282 * - the tls_prf, calc_verify and calc_finished members of handshake structure
6283 */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02006284MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01006285static int ssl_set_handshake_prfs(mbedtls_ssl_handshake_params *handshake,
6286 mbedtls_md_type_t hash)
Jerry Yuf009d862022-02-17 14:01:37 +08006287{
Andrzej Kurek25f27152022-08-17 16:09:31 -04006288#if defined(MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Gilles Peskine449bd832023-01-11 14:50:10 +01006289 if (hash == MBEDTLS_MD_SHA384) {
Jerry Yuf009d862022-02-17 14:01:37 +08006290 handshake->tls_prf = tls_prf_sha384;
6291 handshake->calc_verify = ssl_calc_verify_tls_sha384;
6292 handshake->calc_finished = ssl_calc_finished_tls_sha384;
Gilles Peskine449bd832023-01-11 14:50:10 +01006293 } else
Jerry Yuf009d862022-02-17 14:01:37 +08006294#endif
Andrzej Kurek25f27152022-08-17 16:09:31 -04006295#if defined(MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Jerry Yuf009d862022-02-17 14:01:37 +08006296 {
Ronald Cron81591aa2022-03-07 09:05:51 +01006297 (void) hash;
Jerry Yuf009d862022-02-17 14:01:37 +08006298 handshake->tls_prf = tls_prf_sha256;
6299 handshake->calc_verify = ssl_calc_verify_tls_sha256;
6300 handshake->calc_finished = ssl_calc_finished_tls_sha256;
6301 }
Ronald Cron81591aa2022-03-07 09:05:51 +01006302#else
Jerry Yuf009d862022-02-17 14:01:37 +08006303 {
Jerry Yuf009d862022-02-17 14:01:37 +08006304 (void) handshake;
Ronald Cron81591aa2022-03-07 09:05:51 +01006305 (void) hash;
Gilles Peskine449bd832023-01-11 14:50:10 +01006306 return MBEDTLS_ERR_SSL_INTERNAL_ERROR;
Jerry Yuf009d862022-02-17 14:01:37 +08006307 }
Ronald Cron81591aa2022-03-07 09:05:51 +01006308#endif
Jerry Yuf009d862022-02-17 14:01:37 +08006309
Gilles Peskine449bd832023-01-11 14:50:10 +01006310 return 0;
Jerry Yuf009d862022-02-17 14:01:37 +08006311}
Jerry Yud6ab2352022-02-17 14:03:43 +08006312
Jerry Yu2a7b5ac2022-02-17 14:07:00 +08006313/*
6314 * Compute master secret if needed
6315 *
6316 * Parameters:
6317 * [in/out] handshake
6318 * [in] resume, premaster, extended_ms, calc_verify, tls_prf
6319 * (PSA-PSK) ciphersuite_info, psk_opaque
6320 * [out] premaster (cleared)
6321 * [out] master
6322 * [in] ssl: optionally used for debugging, EMS and PSA-PSK
6323 * debug: conf->f_dbg, conf->p_dbg
6324 * EMS: passed to calc_verify (debug + session_negotiate)
Ronald Crona25cf582022-03-07 11:10:36 +01006325 * PSA-PSA: conf
Jerry Yu2a7b5ac2022-02-17 14:07:00 +08006326 */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02006327MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01006328static int ssl_compute_master(mbedtls_ssl_handshake_params *handshake,
6329 unsigned char *master,
6330 const mbedtls_ssl_context *ssl)
Jerry Yu2a7b5ac2022-02-17 14:07:00 +08006331{
6332 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
6333
6334 /* cf. RFC 5246, Section 8.1:
6335 * "The master secret is always exactly 48 bytes in length." */
6336 size_t const master_secret_len = 48;
6337
6338#if defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET)
6339 unsigned char session_hash[48];
6340#endif /* MBEDTLS_SSL_EXTENDED_MASTER_SECRET */
6341
6342 /* The label for the KDF used for key expansion.
6343 * This is either "master secret" or "extended master secret"
6344 * depending on whether the Extended Master Secret extension
6345 * is used. */
6346 char const *lbl = "master secret";
6347
Przemek Stekielae4ed302022-04-05 17:15:55 +02006348 /* The seed for the KDF used for key expansion.
Jerry Yu2a7b5ac2022-02-17 14:07:00 +08006349 * - If the Extended Master Secret extension is not used,
6350 * this is ClientHello.Random + ServerHello.Random
6351 * (see Sect. 8.1 in RFC 5246).
6352 * - If the Extended Master Secret extension is used,
6353 * this is the transcript of the handshake so far.
6354 * (see Sect. 4 in RFC 7627). */
Przemek Stekielae4ed302022-04-05 17:15:55 +02006355 unsigned char const *seed = handshake->randbytes;
6356 size_t seed_len = 64;
Jerry Yu2a7b5ac2022-02-17 14:07:00 +08006357
6358#if !defined(MBEDTLS_DEBUG_C) && \
6359 !defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET) && \
6360 !(defined(MBEDTLS_USE_PSA_CRYPTO) && \
Gilles Peskine449bd832023-01-11 14:50:10 +01006361 defined(MBEDTLS_KEY_EXCHANGE_PSK_ENABLED))
Jerry Yu2a7b5ac2022-02-17 14:07:00 +08006362 ssl = NULL; /* make sure we don't use it except for those cases */
6363 (void) ssl;
6364#endif
6365
Gilles Peskine449bd832023-01-11 14:50:10 +01006366 if (handshake->resume != 0) {
6367 MBEDTLS_SSL_DEBUG_MSG(3, ("no premaster (session resumed)"));
6368 return 0;
Jerry Yu2a7b5ac2022-02-17 14:07:00 +08006369 }
6370
6371#if defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET)
Gilles Peskine449bd832023-01-11 14:50:10 +01006372 if (handshake->extended_ms == MBEDTLS_SSL_EXTENDED_MS_ENABLED) {
Jerry Yu2a7b5ac2022-02-17 14:07:00 +08006373 lbl = "extended master secret";
Przemek Stekielae4ed302022-04-05 17:15:55 +02006374 seed = session_hash;
Manuel Pégourié-Gonnardb8b07aa2023-02-06 00:34:21 +01006375 ret = handshake->calc_verify(ssl, session_hash, &seed_len);
6376 if (ret != 0) {
6377 MBEDTLS_SSL_DEBUG_RET(1, "calc_verify", ret);
6378 }
Jerry Yu2a7b5ac2022-02-17 14:07:00 +08006379
Gilles Peskine449bd832023-01-11 14:50:10 +01006380 MBEDTLS_SSL_DEBUG_BUF(3, "session hash for extended master secret",
6381 session_hash, seed_len);
Jerry Yu2a7b5ac2022-02-17 14:07:00 +08006382 }
Przemek Stekiel169bf0b2022-04-29 07:53:29 +02006383#endif /* MBEDTLS_SSL_EXTENDED_MASTER_SECRET */
Jerry Yu2a7b5ac2022-02-17 14:07:00 +08006384
Przemek Stekiel99114f32022-04-22 11:20:09 +02006385#if defined(MBEDTLS_USE_PSA_CRYPTO) && \
Przemek Stekiel8a4b7fd2022-04-28 09:22:22 +02006386 defined(MBEDTLS_KEY_EXCHANGE_SOME_PSK_ENABLED)
Gilles Peskine449bd832023-01-11 14:50:10 +01006387 if (mbedtls_ssl_ciphersuite_uses_psk(handshake->ciphersuite_info) == 1) {
Jerry Yu2a7b5ac2022-02-17 14:07:00 +08006388 /* Perform PSK-to-MS expansion in a single step. */
6389 psa_status_t status;
6390 psa_algorithm_t alg;
6391 mbedtls_svc_key_id_t psk;
6392 psa_key_derivation_operation_t derivation =
6393 PSA_KEY_DERIVATION_OPERATION_INIT;
6394 mbedtls_md_type_t hash_alg = handshake->ciphersuite_info->mac;
6395
Gilles Peskine449bd832023-01-11 14:50:10 +01006396 MBEDTLS_SSL_DEBUG_MSG(2, ("perform PSA-based PSK-to-MS expansion"));
Jerry Yu2a7b5ac2022-02-17 14:07:00 +08006397
Gilles Peskine449bd832023-01-11 14:50:10 +01006398 psk = mbedtls_ssl_get_opaque_psk(ssl);
Jerry Yu2a7b5ac2022-02-17 14:07:00 +08006399
Gilles Peskine449bd832023-01-11 14:50:10 +01006400 if (hash_alg == MBEDTLS_MD_SHA384) {
Jerry Yu2a7b5ac2022-02-17 14:07:00 +08006401 alg = PSA_ALG_TLS12_PSK_TO_MS(PSA_ALG_SHA_384);
Gilles Peskine449bd832023-01-11 14:50:10 +01006402 } else {
Jerry Yu2a7b5ac2022-02-17 14:07:00 +08006403 alg = PSA_ALG_TLS12_PSK_TO_MS(PSA_ALG_SHA_256);
Gilles Peskine449bd832023-01-11 14:50:10 +01006404 }
Jerry Yu2a7b5ac2022-02-17 14:07:00 +08006405
Przemek Stekiel51a1f362022-04-13 08:57:06 +02006406 size_t other_secret_len = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +01006407 unsigned char *other_secret = NULL;
Przemek Stekielc2033402022-04-05 17:19:41 +02006408
Gilles Peskine449bd832023-01-11 14:50:10 +01006409 switch (handshake->ciphersuite_info->key_exchange) {
Przemek Stekiel19b80f82022-04-14 08:29:31 +02006410 /* Provide other secret.
Przemek Stekiel51a1f362022-04-13 08:57:06 +02006411 * Other secret is stored in premaster, where first 2 bytes hold the
Przemek Stekiel19b80f82022-04-14 08:29:31 +02006412 * length of the other key.
Przemek Stekielc2033402022-04-05 17:19:41 +02006413 */
Przemek Stekiel19b80f82022-04-14 08:29:31 +02006414 case MBEDTLS_KEY_EXCHANGE_RSA_PSK:
Przemek Stekiel8abcee92022-04-28 09:16:28 +02006415 /* For RSA-PSK other key length is always 48 bytes. */
Przemek Stekiel19b80f82022-04-14 08:29:31 +02006416 other_secret_len = 48;
6417 other_secret = handshake->premaster + 2;
6418 break;
6419 case MBEDTLS_KEY_EXCHANGE_ECDHE_PSK:
Przemek Stekielb293aaa2022-04-19 12:22:38 +02006420 case MBEDTLS_KEY_EXCHANGE_DHE_PSK:
Przemek Stekiel19b80f82022-04-14 08:29:31 +02006421 other_secret_len = MBEDTLS_GET_UINT16_BE(handshake->premaster, 0);
6422 other_secret = handshake->premaster + 2;
6423 break;
6424 default:
6425 break;
Przemek Stekielc2033402022-04-05 17:19:41 +02006426 }
6427
Gilles Peskine449bd832023-01-11 14:50:10 +01006428 status = setup_psa_key_derivation(&derivation, psk, alg,
6429 ssl->conf->psk, ssl->conf->psk_len,
6430 seed, seed_len,
6431 (unsigned char const *) lbl,
6432 (size_t) strlen(lbl),
6433 other_secret, other_secret_len,
6434 master_secret_len);
6435 if (status != PSA_SUCCESS) {
6436 psa_key_derivation_abort(&derivation);
6437 return MBEDTLS_ERR_SSL_HW_ACCEL_FAILED;
Jerry Yu2a7b5ac2022-02-17 14:07:00 +08006438 }
6439
Gilles Peskine449bd832023-01-11 14:50:10 +01006440 status = psa_key_derivation_output_bytes(&derivation,
6441 master,
6442 master_secret_len);
6443 if (status != PSA_SUCCESS) {
6444 psa_key_derivation_abort(&derivation);
6445 return MBEDTLS_ERR_SSL_HW_ACCEL_FAILED;
Jerry Yu2a7b5ac2022-02-17 14:07:00 +08006446 }
6447
Gilles Peskine449bd832023-01-11 14:50:10 +01006448 status = psa_key_derivation_abort(&derivation);
6449 if (status != PSA_SUCCESS) {
6450 return MBEDTLS_ERR_SSL_HW_ACCEL_FAILED;
6451 }
6452 } else
Jerry Yu2a7b5ac2022-02-17 14:07:00 +08006453#endif
6454 {
Neil Armstrongca7d5062022-05-31 14:43:23 +02006455#if defined(MBEDTLS_USE_PSA_CRYPTO) && \
Gilles Peskine449bd832023-01-11 14:50:10 +01006456 defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
6457 if (handshake->ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECJPAKE) {
Neil Armstrongca7d5062022-05-31 14:43:23 +02006458 psa_status_t status;
6459 psa_algorithm_t alg = PSA_ALG_TLS12_ECJPAKE_TO_PMS;
6460 psa_key_derivation_operation_t derivation =
6461 PSA_KEY_DERIVATION_OPERATION_INIT;
6462
Gilles Peskine449bd832023-01-11 14:50:10 +01006463 MBEDTLS_SSL_DEBUG_MSG(2, ("perform PSA-based PMS KDF for ECJPAKE"));
Neil Armstrongca7d5062022-05-31 14:43:23 +02006464
6465 handshake->pmslen = PSA_TLS12_ECJPAKE_TO_PMS_DATA_SIZE;
6466
Gilles Peskine449bd832023-01-11 14:50:10 +01006467 status = psa_key_derivation_setup(&derivation, alg);
6468 if (status != PSA_SUCCESS) {
6469 return MBEDTLS_ERR_SSL_HW_ACCEL_FAILED;
Neil Armstrongca7d5062022-05-31 14:43:23 +02006470 }
6471
Gilles Peskine449bd832023-01-11 14:50:10 +01006472 status = psa_key_derivation_set_capacity(&derivation,
6473 PSA_TLS12_ECJPAKE_TO_PMS_DATA_SIZE);
6474 if (status != PSA_SUCCESS) {
6475 psa_key_derivation_abort(&derivation);
6476 return MBEDTLS_ERR_SSL_HW_ACCEL_FAILED;
Neil Armstrongca7d5062022-05-31 14:43:23 +02006477 }
6478
Gilles Peskine449bd832023-01-11 14:50:10 +01006479 status = psa_pake_get_implicit_key(&handshake->psa_pake_ctx,
6480 &derivation);
6481 if (status != PSA_SUCCESS) {
6482 psa_key_derivation_abort(&derivation);
6483 return MBEDTLS_ERR_SSL_HW_ACCEL_FAILED;
Neil Armstrongca7d5062022-05-31 14:43:23 +02006484 }
6485
Gilles Peskine449bd832023-01-11 14:50:10 +01006486 status = psa_key_derivation_output_bytes(&derivation,
6487 handshake->premaster,
6488 handshake->pmslen);
6489 if (status != PSA_SUCCESS) {
6490 psa_key_derivation_abort(&derivation);
6491 return MBEDTLS_ERR_SSL_HW_ACCEL_FAILED;
6492 }
6493
6494 status = psa_key_derivation_abort(&derivation);
6495 if (status != PSA_SUCCESS) {
6496 return MBEDTLS_ERR_SSL_HW_ACCEL_FAILED;
Neil Armstrongca7d5062022-05-31 14:43:23 +02006497 }
6498 }
6499#endif
Gilles Peskine449bd832023-01-11 14:50:10 +01006500 ret = handshake->tls_prf(handshake->premaster, handshake->pmslen,
6501 lbl, seed, seed_len,
6502 master,
6503 master_secret_len);
6504 if (ret != 0) {
6505 MBEDTLS_SSL_DEBUG_RET(1, "prf", ret);
6506 return ret;
Jerry Yu2a7b5ac2022-02-17 14:07:00 +08006507 }
6508
Gilles Peskine449bd832023-01-11 14:50:10 +01006509 MBEDTLS_SSL_DEBUG_BUF(3, "premaster secret",
6510 handshake->premaster,
6511 handshake->pmslen);
Jerry Yu2a7b5ac2022-02-17 14:07:00 +08006512
Gilles Peskine449bd832023-01-11 14:50:10 +01006513 mbedtls_platform_zeroize(handshake->premaster,
6514 sizeof(handshake->premaster));
Jerry Yu2a7b5ac2022-02-17 14:07:00 +08006515 }
6516
Gilles Peskine449bd832023-01-11 14:50:10 +01006517 return 0;
Jerry Yu2a7b5ac2022-02-17 14:07:00 +08006518}
6519
Gilles Peskine449bd832023-01-11 14:50:10 +01006520int mbedtls_ssl_derive_keys(mbedtls_ssl_context *ssl)
Jerry Yud62f87e2022-02-17 14:09:02 +08006521{
6522 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
6523 const mbedtls_ssl_ciphersuite_t * const ciphersuite_info =
6524 ssl->handshake->ciphersuite_info;
6525
Gilles Peskine449bd832023-01-11 14:50:10 +01006526 MBEDTLS_SSL_DEBUG_MSG(2, ("=> derive keys"));
Jerry Yud62f87e2022-02-17 14:09:02 +08006527
6528 /* Set PRF, calc_verify and calc_finished function pointers */
Gilles Peskine449bd832023-01-11 14:50:10 +01006529 ret = ssl_set_handshake_prfs(ssl->handshake,
6530 ciphersuite_info->mac);
6531 if (ret != 0) {
6532 MBEDTLS_SSL_DEBUG_RET(1, "ssl_set_handshake_prfs", ret);
6533 return ret;
Jerry Yud62f87e2022-02-17 14:09:02 +08006534 }
6535
6536 /* Compute master secret if needed */
Gilles Peskine449bd832023-01-11 14:50:10 +01006537 ret = ssl_compute_master(ssl->handshake,
6538 ssl->session_negotiate->master,
6539 ssl);
6540 if (ret != 0) {
6541 MBEDTLS_SSL_DEBUG_RET(1, "ssl_compute_master", ret);
6542 return ret;
Jerry Yud62f87e2022-02-17 14:09:02 +08006543 }
6544
6545 /* Swap the client and server random values:
6546 * - MS derivation wanted client+server (RFC 5246 8.1)
6547 * - key derivation wants server+client (RFC 5246 6.3) */
6548 {
6549 unsigned char tmp[64];
Gilles Peskine449bd832023-01-11 14:50:10 +01006550 memcpy(tmp, ssl->handshake->randbytes, 64);
6551 memcpy(ssl->handshake->randbytes, tmp + 32, 32);
6552 memcpy(ssl->handshake->randbytes + 32, tmp, 32);
6553 mbedtls_platform_zeroize(tmp, sizeof(tmp));
Jerry Yud62f87e2022-02-17 14:09:02 +08006554 }
6555
6556 /* Populate transform structure */
Gilles Peskine449bd832023-01-11 14:50:10 +01006557 ret = ssl_tls12_populate_transform(ssl->transform_negotiate,
6558 ssl->session_negotiate->ciphersuite,
6559 ssl->session_negotiate->master,
Neil Armstrongf2c82f02022-04-05 11:16:53 +02006560#if defined(MBEDTLS_SSL_SOME_SUITES_USE_CBC_ETM)
Gilles Peskine449bd832023-01-11 14:50:10 +01006561 ssl->session_negotiate->encrypt_then_mac,
Neil Armstrongf2c82f02022-04-05 11:16:53 +02006562#endif /* MBEDTLS_SSL_SOME_SUITES_USE_CBC_ETM */
Gilles Peskine449bd832023-01-11 14:50:10 +01006563 ssl->handshake->tls_prf,
6564 ssl->handshake->randbytes,
6565 ssl->tls_version,
6566 ssl->conf->endpoint,
6567 ssl);
6568 if (ret != 0) {
6569 MBEDTLS_SSL_DEBUG_RET(1, "ssl_tls12_populate_transform", ret);
6570 return ret;
Jerry Yud62f87e2022-02-17 14:09:02 +08006571 }
6572
6573 /* We no longer need Server/ClientHello.random values */
Gilles Peskine449bd832023-01-11 14:50:10 +01006574 mbedtls_platform_zeroize(ssl->handshake->randbytes,
6575 sizeof(ssl->handshake->randbytes));
Jerry Yud62f87e2022-02-17 14:09:02 +08006576
Gilles Peskine449bd832023-01-11 14:50:10 +01006577 MBEDTLS_SSL_DEBUG_MSG(2, ("<= derive keys"));
Jerry Yud62f87e2022-02-17 14:09:02 +08006578
Gilles Peskine449bd832023-01-11 14:50:10 +01006579 return 0;
Jerry Yud62f87e2022-02-17 14:09:02 +08006580}
Jerry Yu8392e0d2022-02-17 14:10:24 +08006581
Gilles Peskine449bd832023-01-11 14:50:10 +01006582int mbedtls_ssl_set_calc_verify_md(mbedtls_ssl_context *ssl, int md)
Ronald Cron4dcbca92022-03-07 10:21:40 +01006583{
Gilles Peskine449bd832023-01-11 14:50:10 +01006584 switch (md) {
Andrzej Kurek25f27152022-08-17 16:09:31 -04006585#if defined(MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Ronald Cron4dcbca92022-03-07 10:21:40 +01006586 case MBEDTLS_SSL_HASH_SHA384:
6587 ssl->handshake->calc_verify = ssl_calc_verify_tls_sha384;
6588 break;
6589#endif
Andrzej Kurek25f27152022-08-17 16:09:31 -04006590#if defined(MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Ronald Cron4dcbca92022-03-07 10:21:40 +01006591 case MBEDTLS_SSL_HASH_SHA256:
6592 ssl->handshake->calc_verify = ssl_calc_verify_tls_sha256;
6593 break;
6594#endif
6595 default:
Gilles Peskine449bd832023-01-11 14:50:10 +01006596 return -1;
Ronald Cron4dcbca92022-03-07 10:21:40 +01006597 }
Andrzej Kurekeabeb302022-10-17 07:52:51 -04006598#if !defined(MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA) && \
6599 !defined(MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
6600 (void) ssl;
6601#endif
Gilles Peskine449bd832023-01-11 14:50:10 +01006602 return 0;
Ronald Cron4dcbca92022-03-07 10:21:40 +01006603}
6604
Andrzej Kurek25f27152022-08-17 16:09:31 -04006605#if defined(MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Manuel Pégourié-Gonnard226aa152023-02-05 09:46:59 +01006606int ssl_calc_verify_tls_sha256(const mbedtls_ssl_context *ssl,
6607 unsigned char *hash,
6608 size_t *hlen)
Jerry Yu8392e0d2022-02-17 14:10:24 +08006609{
6610#if defined(MBEDTLS_USE_PSA_CRYPTO)
6611 size_t hash_size;
6612 psa_status_t status;
6613 psa_hash_operation_t sha256_psa = psa_hash_operation_init();
6614
Gilles Peskine449bd832023-01-11 14:50:10 +01006615 MBEDTLS_SSL_DEBUG_MSG(2, ("=> PSA calc verify sha256"));
6616 status = psa_hash_clone(&ssl->handshake->fin_sha256_psa, &sha256_psa);
6617 if (status != PSA_SUCCESS) {
Manuel Pégourié-Gonnardb9b564e2023-02-06 10:06:04 +01006618 goto exit;
Jerry Yu8392e0d2022-02-17 14:10:24 +08006619 }
6620
Gilles Peskine449bd832023-01-11 14:50:10 +01006621 status = psa_hash_finish(&sha256_psa, hash, 32, &hash_size);
6622 if (status != PSA_SUCCESS) {
Manuel Pégourié-Gonnardb9b564e2023-02-06 10:06:04 +01006623 goto exit;
Jerry Yu8392e0d2022-02-17 14:10:24 +08006624 }
6625
6626 *hlen = 32;
Gilles Peskine449bd832023-01-11 14:50:10 +01006627 MBEDTLS_SSL_DEBUG_BUF(3, "PSA calculated verify result", hash, *hlen);
6628 MBEDTLS_SSL_DEBUG_MSG(2, ("<= PSA calc verify"));
Manuel Pégourié-Gonnardb9b564e2023-02-06 10:06:04 +01006629
6630exit:
6631 psa_hash_abort(&sha256_psa);
6632 return mbedtls_md_error_from_psa(status);
Jerry Yu8392e0d2022-02-17 14:10:24 +08006633#else
Manuel Pégourié-Gonnardb9b564e2023-02-06 10:06:04 +01006634 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnardf057ecf2023-02-24 13:19:17 +01006635 mbedtls_md_context_t sha256;
Jerry Yu8392e0d2022-02-17 14:10:24 +08006636
Manuel Pégourié-Gonnardf057ecf2023-02-24 13:19:17 +01006637 mbedtls_md_init(&sha256);
Jerry Yu8392e0d2022-02-17 14:10:24 +08006638
Gilles Peskine449bd832023-01-11 14:50:10 +01006639 MBEDTLS_SSL_DEBUG_MSG(2, ("=> calc verify sha256"));
Jerry Yu8392e0d2022-02-17 14:10:24 +08006640
Manuel Pégourié-Gonnardf057ecf2023-02-24 13:19:17 +01006641 ret = mbedtls_md_setup(&sha256, mbedtls_md_info_from_type(MBEDTLS_MD_SHA256), 0);
6642 if (ret != 0) {
6643 goto exit;
6644 }
6645 ret = mbedtls_md_clone(&sha256, &ssl->handshake->fin_sha256);
6646 if (ret != 0) {
6647 goto exit;
6648 }
Manuel Pégourié-Gonnardb9b564e2023-02-06 10:06:04 +01006649
Manuel Pégourié-Gonnardf057ecf2023-02-24 13:19:17 +01006650 ret = mbedtls_md_finish(&sha256, hash);
Manuel Pégourié-Gonnardb9b564e2023-02-06 10:06:04 +01006651 if (ret != 0) {
6652 goto exit;
6653 }
Jerry Yu8392e0d2022-02-17 14:10:24 +08006654
6655 *hlen = 32;
6656
Gilles Peskine449bd832023-01-11 14:50:10 +01006657 MBEDTLS_SSL_DEBUG_BUF(3, "calculated verify result", hash, *hlen);
6658 MBEDTLS_SSL_DEBUG_MSG(2, ("<= calc verify"));
Jerry Yu8392e0d2022-02-17 14:10:24 +08006659
Manuel Pégourié-Gonnardb9b564e2023-02-06 10:06:04 +01006660exit:
Manuel Pégourié-Gonnardf057ecf2023-02-24 13:19:17 +01006661 mbedtls_md_free(&sha256);
Manuel Pégourié-Gonnard8e176f72023-02-09 10:33:54 +01006662 return ret;
Jerry Yu8392e0d2022-02-17 14:10:24 +08006663#endif /* MBEDTLS_USE_PSA_CRYPTO */
Jerry Yu8392e0d2022-02-17 14:10:24 +08006664}
Andrzej Kurek25f27152022-08-17 16:09:31 -04006665#endif /* MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA */
Jerry Yu8392e0d2022-02-17 14:10:24 +08006666
Andrzej Kurek25f27152022-08-17 16:09:31 -04006667#if defined(MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Manuel Pégourié-Gonnard226aa152023-02-05 09:46:59 +01006668int ssl_calc_verify_tls_sha384(const mbedtls_ssl_context *ssl,
6669 unsigned char *hash,
6670 size_t *hlen)
Jerry Yuc1cb3842022-02-17 14:13:48 +08006671{
6672#if defined(MBEDTLS_USE_PSA_CRYPTO)
6673 size_t hash_size;
6674 psa_status_t status;
6675 psa_hash_operation_t sha384_psa = psa_hash_operation_init();
6676
Gilles Peskine449bd832023-01-11 14:50:10 +01006677 MBEDTLS_SSL_DEBUG_MSG(2, ("=> PSA calc verify sha384"));
6678 status = psa_hash_clone(&ssl->handshake->fin_sha384_psa, &sha384_psa);
6679 if (status != PSA_SUCCESS) {
Manuel Pégourié-Gonnardb9b564e2023-02-06 10:06:04 +01006680 goto exit;
Jerry Yuc1cb3842022-02-17 14:13:48 +08006681 }
6682
Gilles Peskine449bd832023-01-11 14:50:10 +01006683 status = psa_hash_finish(&sha384_psa, hash, 48, &hash_size);
6684 if (status != PSA_SUCCESS) {
Manuel Pégourié-Gonnardb9b564e2023-02-06 10:06:04 +01006685 goto exit;
Jerry Yuc1cb3842022-02-17 14:13:48 +08006686 }
6687
6688 *hlen = 48;
Gilles Peskine449bd832023-01-11 14:50:10 +01006689 MBEDTLS_SSL_DEBUG_BUF(3, "PSA calculated verify result", hash, *hlen);
6690 MBEDTLS_SSL_DEBUG_MSG(2, ("<= PSA calc verify"));
Manuel Pégourié-Gonnardb9b564e2023-02-06 10:06:04 +01006691
6692exit:
6693 psa_hash_abort(&sha384_psa);
6694 return mbedtls_md_error_from_psa(status);
Jerry Yuc1cb3842022-02-17 14:13:48 +08006695#else
Manuel Pégourié-Gonnardb9b564e2023-02-06 10:06:04 +01006696 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnard02d55d52023-02-24 13:21:16 +01006697 mbedtls_md_context_t sha384;
Jerry Yuc1cb3842022-02-17 14:13:48 +08006698
Manuel Pégourié-Gonnard02d55d52023-02-24 13:21:16 +01006699 mbedtls_md_init(&sha384);
Jerry Yuc1cb3842022-02-17 14:13:48 +08006700
Gilles Peskine449bd832023-01-11 14:50:10 +01006701 MBEDTLS_SSL_DEBUG_MSG(2, ("=> calc verify sha384"));
Jerry Yuc1cb3842022-02-17 14:13:48 +08006702
Manuel Pégourié-Gonnard02d55d52023-02-24 13:21:16 +01006703 ret = mbedtls_md_setup(&sha384, mbedtls_md_info_from_type(MBEDTLS_MD_SHA384), 0);
Manuel Pégourié-Gonnardf057ecf2023-02-24 13:19:17 +01006704 if (ret != 0) {
6705 goto exit;
6706 }
Manuel Pégourié-Gonnard02d55d52023-02-24 13:21:16 +01006707 ret = mbedtls_md_clone(&sha384, &ssl->handshake->fin_sha384);
Manuel Pégourié-Gonnardf057ecf2023-02-24 13:19:17 +01006708 if (ret != 0) {
6709 goto exit;
6710 }
Manuel Pégourié-Gonnardb9b564e2023-02-06 10:06:04 +01006711
Manuel Pégourié-Gonnard02d55d52023-02-24 13:21:16 +01006712 ret = mbedtls_md_finish(&sha384, hash);
Manuel Pégourié-Gonnardb9b564e2023-02-06 10:06:04 +01006713 if (ret != 0) {
6714 goto exit;
6715 }
Jerry Yuc1cb3842022-02-17 14:13:48 +08006716
6717 *hlen = 48;
6718
Gilles Peskine449bd832023-01-11 14:50:10 +01006719 MBEDTLS_SSL_DEBUG_BUF(3, "calculated verify result", hash, *hlen);
6720 MBEDTLS_SSL_DEBUG_MSG(2, ("<= calc verify"));
Jerry Yuc1cb3842022-02-17 14:13:48 +08006721
Manuel Pégourié-Gonnardb9b564e2023-02-06 10:06:04 +01006722exit:
Manuel Pégourié-Gonnard02d55d52023-02-24 13:21:16 +01006723 mbedtls_md_free(&sha384);
Manuel Pégourié-Gonnardb9b564e2023-02-06 10:06:04 +01006724 return ret;
Jerry Yuc1cb3842022-02-17 14:13:48 +08006725#endif /* MBEDTLS_USE_PSA_CRYPTO */
Jerry Yuc1cb3842022-02-17 14:13:48 +08006726}
Andrzej Kurek25f27152022-08-17 16:09:31 -04006727#endif /* MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA */
Jerry Yuc1cb3842022-02-17 14:13:48 +08006728
Neil Armstrong80f6f322022-05-03 17:56:38 +02006729#if !defined(MBEDTLS_USE_PSA_CRYPTO) && \
6730 defined(MBEDTLS_KEY_EXCHANGE_SOME_PSK_ENABLED)
Gilles Peskine449bd832023-01-11 14:50:10 +01006731int mbedtls_ssl_psk_derive_premaster(mbedtls_ssl_context *ssl, mbedtls_key_exchange_type_t key_ex)
Jerry Yuce3dca42022-02-17 14:16:37 +08006732{
6733 unsigned char *p = ssl->handshake->premaster;
Gilles Peskine449bd832023-01-11 14:50:10 +01006734 unsigned char *end = p + sizeof(ssl->handshake->premaster);
Jerry Yuce3dca42022-02-17 14:16:37 +08006735 const unsigned char *psk = NULL;
6736 size_t psk_len = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +01006737 int psk_ret = mbedtls_ssl_get_psk(ssl, &psk, &psk_len);
Jerry Yuce3dca42022-02-17 14:16:37 +08006738
Gilles Peskine449bd832023-01-11 14:50:10 +01006739 if (psk_ret == MBEDTLS_ERR_SSL_PRIVATE_KEY_REQUIRED) {
Jerry Yuce3dca42022-02-17 14:16:37 +08006740 /*
6741 * This should never happen because the existence of a PSK is always
Przemek Stekielb293aaa2022-04-19 12:22:38 +02006742 * checked before calling this function.
6743 *
6744 * The exception is opaque DHE-PSK. For DHE-PSK fill premaster with
Przemek Stekiel8abcee92022-04-28 09:16:28 +02006745 * the shared secret without PSK.
Jerry Yuce3dca42022-02-17 14:16:37 +08006746 */
Gilles Peskine449bd832023-01-11 14:50:10 +01006747 if (key_ex != MBEDTLS_KEY_EXCHANGE_DHE_PSK) {
6748 MBEDTLS_SSL_DEBUG_MSG(1, ("should never happen"));
6749 return MBEDTLS_ERR_SSL_INTERNAL_ERROR;
Przemek Stekielb293aaa2022-04-19 12:22:38 +02006750 }
Jerry Yuce3dca42022-02-17 14:16:37 +08006751 }
6752
6753 /*
6754 * PMS = struct {
6755 * opaque other_secret<0..2^16-1>;
6756 * opaque psk<0..2^16-1>;
6757 * };
6758 * with "other_secret" depending on the particular key exchange
6759 */
6760#if defined(MBEDTLS_KEY_EXCHANGE_PSK_ENABLED)
Gilles Peskine449bd832023-01-11 14:50:10 +01006761 if (key_ex == MBEDTLS_KEY_EXCHANGE_PSK) {
6762 if (end - p < 2) {
6763 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
6764 }
Jerry Yuce3dca42022-02-17 14:16:37 +08006765
Gilles Peskine449bd832023-01-11 14:50:10 +01006766 MBEDTLS_PUT_UINT16_BE(psk_len, p, 0);
Jerry Yuce3dca42022-02-17 14:16:37 +08006767 p += 2;
6768
Gilles Peskine449bd832023-01-11 14:50:10 +01006769 if (end < p || (size_t) (end - p) < psk_len) {
6770 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
6771 }
Jerry Yuce3dca42022-02-17 14:16:37 +08006772
Gilles Peskine449bd832023-01-11 14:50:10 +01006773 memset(p, 0, psk_len);
Jerry Yuce3dca42022-02-17 14:16:37 +08006774 p += psk_len;
Gilles Peskine449bd832023-01-11 14:50:10 +01006775 } else
Jerry Yuce3dca42022-02-17 14:16:37 +08006776#endif /* MBEDTLS_KEY_EXCHANGE_PSK_ENABLED */
6777#if defined(MBEDTLS_KEY_EXCHANGE_RSA_PSK_ENABLED)
Gilles Peskine449bd832023-01-11 14:50:10 +01006778 if (key_ex == MBEDTLS_KEY_EXCHANGE_RSA_PSK) {
Jerry Yuce3dca42022-02-17 14:16:37 +08006779 /*
6780 * other_secret already set by the ClientKeyExchange message,
6781 * and is 48 bytes long
6782 */
Gilles Peskine449bd832023-01-11 14:50:10 +01006783 if (end - p < 2) {
6784 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
6785 }
Jerry Yuce3dca42022-02-17 14:16:37 +08006786
6787 *p++ = 0;
6788 *p++ = 48;
6789 p += 48;
Gilles Peskine449bd832023-01-11 14:50:10 +01006790 } else
Jerry Yuce3dca42022-02-17 14:16:37 +08006791#endif /* MBEDTLS_KEY_EXCHANGE_RSA_PSK_ENABLED */
6792#if defined(MBEDTLS_KEY_EXCHANGE_DHE_PSK_ENABLED)
Gilles Peskine449bd832023-01-11 14:50:10 +01006793 if (key_ex == MBEDTLS_KEY_EXCHANGE_DHE_PSK) {
Jerry Yuce3dca42022-02-17 14:16:37 +08006794 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
6795 size_t len;
6796
6797 /* Write length only when we know the actual value */
Gilles Peskine449bd832023-01-11 14:50:10 +01006798 if ((ret = mbedtls_dhm_calc_secret(&ssl->handshake->dhm_ctx,
6799 p + 2, end - (p + 2), &len,
6800 ssl->conf->f_rng, ssl->conf->p_rng)) != 0) {
6801 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_dhm_calc_secret", ret);
6802 return ret;
Jerry Yuce3dca42022-02-17 14:16:37 +08006803 }
Gilles Peskine449bd832023-01-11 14:50:10 +01006804 MBEDTLS_PUT_UINT16_BE(len, p, 0);
Jerry Yuce3dca42022-02-17 14:16:37 +08006805 p += 2 + len;
6806
Gilles Peskine449bd832023-01-11 14:50:10 +01006807 MBEDTLS_SSL_DEBUG_MPI(3, "DHM: K ", &ssl->handshake->dhm_ctx.K);
6808 } else
Jerry Yuce3dca42022-02-17 14:16:37 +08006809#endif /* MBEDTLS_KEY_EXCHANGE_DHE_PSK_ENABLED */
Neil Armstrong80f6f322022-05-03 17:56:38 +02006810#if defined(MBEDTLS_KEY_EXCHANGE_ECDHE_PSK_ENABLED)
Gilles Peskine449bd832023-01-11 14:50:10 +01006811 if (key_ex == MBEDTLS_KEY_EXCHANGE_ECDHE_PSK) {
Jerry Yuce3dca42022-02-17 14:16:37 +08006812 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
6813 size_t zlen;
6814
Gilles Peskine449bd832023-01-11 14:50:10 +01006815 if ((ret = mbedtls_ecdh_calc_secret(&ssl->handshake->ecdh_ctx, &zlen,
6816 p + 2, end - (p + 2),
6817 ssl->conf->f_rng, ssl->conf->p_rng)) != 0) {
6818 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ecdh_calc_secret", ret);
6819 return ret;
Jerry Yuce3dca42022-02-17 14:16:37 +08006820 }
6821
Gilles Peskine449bd832023-01-11 14:50:10 +01006822 MBEDTLS_PUT_UINT16_BE(zlen, p, 0);
Jerry Yuce3dca42022-02-17 14:16:37 +08006823 p += 2 + zlen;
6824
Gilles Peskine449bd832023-01-11 14:50:10 +01006825 MBEDTLS_SSL_DEBUG_ECDH(3, &ssl->handshake->ecdh_ctx,
6826 MBEDTLS_DEBUG_ECDH_Z);
6827 } else
Neil Armstrong80f6f322022-05-03 17:56:38 +02006828#endif /* MBEDTLS_KEY_EXCHANGE_ECDHE_PSK_ENABLED */
Jerry Yuce3dca42022-02-17 14:16:37 +08006829 {
Gilles Peskine449bd832023-01-11 14:50:10 +01006830 MBEDTLS_SSL_DEBUG_MSG(1, ("should never happen"));
6831 return MBEDTLS_ERR_SSL_INTERNAL_ERROR;
Jerry Yuce3dca42022-02-17 14:16:37 +08006832 }
6833
6834 /* opaque psk<0..2^16-1>; */
Gilles Peskine449bd832023-01-11 14:50:10 +01006835 if (end - p < 2) {
6836 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
6837 }
Jerry Yuce3dca42022-02-17 14:16:37 +08006838
Gilles Peskine449bd832023-01-11 14:50:10 +01006839 MBEDTLS_PUT_UINT16_BE(psk_len, p, 0);
Jerry Yuce3dca42022-02-17 14:16:37 +08006840 p += 2;
6841
Gilles Peskine449bd832023-01-11 14:50:10 +01006842 if (end < p || (size_t) (end - p) < psk_len) {
6843 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
6844 }
Jerry Yuce3dca42022-02-17 14:16:37 +08006845
Gilles Peskine449bd832023-01-11 14:50:10 +01006846 memcpy(p, psk, psk_len);
Jerry Yuce3dca42022-02-17 14:16:37 +08006847 p += psk_len;
6848
6849 ssl->handshake->pmslen = p - ssl->handshake->premaster;
6850
Gilles Peskine449bd832023-01-11 14:50:10 +01006851 return 0;
Jerry Yuce3dca42022-02-17 14:16:37 +08006852}
Neil Armstrong80f6f322022-05-03 17:56:38 +02006853#endif /* !MBEDTLS_USE_PSA_CRYPTO && MBEDTLS_KEY_EXCHANGE_SOME_PSK_ENABLED */
Jerry Yuc2c673d2022-02-17 14:20:39 +08006854
6855#if defined(MBEDTLS_SSL_SRV_C) && defined(MBEDTLS_SSL_RENEGOTIATION)
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02006856MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01006857static int ssl_write_hello_request(mbedtls_ssl_context *ssl);
Jerry Yuc2c673d2022-02-17 14:20:39 +08006858
6859#if defined(MBEDTLS_SSL_PROTO_DTLS)
Gilles Peskine449bd832023-01-11 14:50:10 +01006860int mbedtls_ssl_resend_hello_request(mbedtls_ssl_context *ssl)
Jerry Yuc2c673d2022-02-17 14:20:39 +08006861{
6862 /* If renegotiation is not enforced, retransmit until we would reach max
6863 * timeout if we were using the usual handshake doubling scheme */
Gilles Peskine449bd832023-01-11 14:50:10 +01006864 if (ssl->conf->renego_max_records < 0) {
Jerry Yuc2c673d2022-02-17 14:20:39 +08006865 uint32_t ratio = ssl->conf->hs_timeout_max / ssl->conf->hs_timeout_min + 1;
6866 unsigned char doublings = 1;
6867
Gilles Peskine449bd832023-01-11 14:50:10 +01006868 while (ratio != 0) {
Jerry Yuc2c673d2022-02-17 14:20:39 +08006869 ++doublings;
6870 ratio >>= 1;
6871 }
6872
Gilles Peskine449bd832023-01-11 14:50:10 +01006873 if (++ssl->renego_records_seen > doublings) {
6874 MBEDTLS_SSL_DEBUG_MSG(2, ("no longer retransmitting hello request"));
6875 return 0;
Jerry Yuc2c673d2022-02-17 14:20:39 +08006876 }
6877 }
6878
Gilles Peskine449bd832023-01-11 14:50:10 +01006879 return ssl_write_hello_request(ssl);
Jerry Yuc2c673d2022-02-17 14:20:39 +08006880}
6881#endif
6882#endif /* MBEDTLS_SSL_SRV_C && MBEDTLS_SSL_RENEGOTIATION */
Jerry Yud9526692022-02-17 14:23:47 +08006883
Jerry Yud9526692022-02-17 14:23:47 +08006884/*
6885 * Handshake functions
6886 */
6887#if !defined(MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED)
6888/* No certificate support -> dummy functions */
Gilles Peskine449bd832023-01-11 14:50:10 +01006889int mbedtls_ssl_write_certificate(mbedtls_ssl_context *ssl)
Jerry Yud9526692022-02-17 14:23:47 +08006890{
6891 const mbedtls_ssl_ciphersuite_t *ciphersuite_info =
6892 ssl->handshake->ciphersuite_info;
6893
Gilles Peskine449bd832023-01-11 14:50:10 +01006894 MBEDTLS_SSL_DEBUG_MSG(2, ("=> write certificate"));
Jerry Yud9526692022-02-17 14:23:47 +08006895
Gilles Peskine449bd832023-01-11 14:50:10 +01006896 if (!mbedtls_ssl_ciphersuite_uses_srv_cert(ciphersuite_info)) {
6897 MBEDTLS_SSL_DEBUG_MSG(2, ("<= skip write certificate"));
Jerry Yud9526692022-02-17 14:23:47 +08006898 ssl->state++;
Gilles Peskine449bd832023-01-11 14:50:10 +01006899 return 0;
Jerry Yud9526692022-02-17 14:23:47 +08006900 }
6901
Gilles Peskine449bd832023-01-11 14:50:10 +01006902 MBEDTLS_SSL_DEBUG_MSG(1, ("should never happen"));
6903 return MBEDTLS_ERR_SSL_INTERNAL_ERROR;
Jerry Yud9526692022-02-17 14:23:47 +08006904}
6905
Gilles Peskine449bd832023-01-11 14:50:10 +01006906int mbedtls_ssl_parse_certificate(mbedtls_ssl_context *ssl)
Jerry Yud9526692022-02-17 14:23:47 +08006907{
6908 const mbedtls_ssl_ciphersuite_t *ciphersuite_info =
6909 ssl->handshake->ciphersuite_info;
6910
Gilles Peskine449bd832023-01-11 14:50:10 +01006911 MBEDTLS_SSL_DEBUG_MSG(2, ("=> parse certificate"));
Jerry Yud9526692022-02-17 14:23:47 +08006912
Gilles Peskine449bd832023-01-11 14:50:10 +01006913 if (!mbedtls_ssl_ciphersuite_uses_srv_cert(ciphersuite_info)) {
6914 MBEDTLS_SSL_DEBUG_MSG(2, ("<= skip parse certificate"));
Jerry Yud9526692022-02-17 14:23:47 +08006915 ssl->state++;
Gilles Peskine449bd832023-01-11 14:50:10 +01006916 return 0;
Jerry Yud9526692022-02-17 14:23:47 +08006917 }
6918
Gilles Peskine449bd832023-01-11 14:50:10 +01006919 MBEDTLS_SSL_DEBUG_MSG(1, ("should never happen"));
6920 return MBEDTLS_ERR_SSL_INTERNAL_ERROR;
Jerry Yud9526692022-02-17 14:23:47 +08006921}
6922
6923#else /* MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED */
6924/* Some certificate support -> implement write and parse */
6925
Gilles Peskine449bd832023-01-11 14:50:10 +01006926int mbedtls_ssl_write_certificate(mbedtls_ssl_context *ssl)
Jerry Yud9526692022-02-17 14:23:47 +08006927{
6928 int ret = MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
6929 size_t i, n;
6930 const mbedtls_x509_crt *crt;
6931 const mbedtls_ssl_ciphersuite_t *ciphersuite_info =
6932 ssl->handshake->ciphersuite_info;
6933
Gilles Peskine449bd832023-01-11 14:50:10 +01006934 MBEDTLS_SSL_DEBUG_MSG(2, ("=> write certificate"));
Jerry Yud9526692022-02-17 14:23:47 +08006935
Gilles Peskine449bd832023-01-11 14:50:10 +01006936 if (!mbedtls_ssl_ciphersuite_uses_srv_cert(ciphersuite_info)) {
6937 MBEDTLS_SSL_DEBUG_MSG(2, ("<= skip write certificate"));
Jerry Yud9526692022-02-17 14:23:47 +08006938 ssl->state++;
Gilles Peskine449bd832023-01-11 14:50:10 +01006939 return 0;
Jerry Yud9526692022-02-17 14:23:47 +08006940 }
6941
6942#if defined(MBEDTLS_SSL_CLI_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01006943 if (ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT) {
6944 if (ssl->handshake->client_auth == 0) {
6945 MBEDTLS_SSL_DEBUG_MSG(2, ("<= skip write certificate"));
Jerry Yud9526692022-02-17 14:23:47 +08006946 ssl->state++;
Gilles Peskine449bd832023-01-11 14:50:10 +01006947 return 0;
Jerry Yud9526692022-02-17 14:23:47 +08006948 }
6949 }
6950#endif /* MBEDTLS_SSL_CLI_C */
6951#if defined(MBEDTLS_SSL_SRV_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01006952 if (ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER) {
6953 if (mbedtls_ssl_own_cert(ssl) == NULL) {
Jerry Yud9526692022-02-17 14:23:47 +08006954 /* Should never happen because we shouldn't have picked the
6955 * ciphersuite if we don't have a certificate. */
Gilles Peskine449bd832023-01-11 14:50:10 +01006956 return MBEDTLS_ERR_SSL_INTERNAL_ERROR;
Jerry Yud9526692022-02-17 14:23:47 +08006957 }
6958 }
6959#endif
6960
Gilles Peskine449bd832023-01-11 14:50:10 +01006961 MBEDTLS_SSL_DEBUG_CRT(3, "own certificate", mbedtls_ssl_own_cert(ssl));
Jerry Yud9526692022-02-17 14:23:47 +08006962
6963 /*
6964 * 0 . 0 handshake type
6965 * 1 . 3 handshake length
6966 * 4 . 6 length of all certs
6967 * 7 . 9 length of cert. 1
6968 * 10 . n-1 peer certificate
6969 * n . n+2 length of cert. 2
6970 * n+3 . ... upper level cert, etc.
6971 */
6972 i = 7;
Gilles Peskine449bd832023-01-11 14:50:10 +01006973 crt = mbedtls_ssl_own_cert(ssl);
Jerry Yud9526692022-02-17 14:23:47 +08006974
Gilles Peskine449bd832023-01-11 14:50:10 +01006975 while (crt != NULL) {
Jerry Yud9526692022-02-17 14:23:47 +08006976 n = crt->raw.len;
Gilles Peskine449bd832023-01-11 14:50:10 +01006977 if (n > MBEDTLS_SSL_OUT_CONTENT_LEN - 3 - i) {
6978 MBEDTLS_SSL_DEBUG_MSG(1, ("certificate too large, %" MBEDTLS_PRINTF_SIZET
6979 " > %" MBEDTLS_PRINTF_SIZET,
6980 i + 3 + n, (size_t) MBEDTLS_SSL_OUT_CONTENT_LEN));
6981 return MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL;
Jerry Yud9526692022-02-17 14:23:47 +08006982 }
6983
Gilles Peskine449bd832023-01-11 14:50:10 +01006984 ssl->out_msg[i] = MBEDTLS_BYTE_2(n);
6985 ssl->out_msg[i + 1] = MBEDTLS_BYTE_1(n);
6986 ssl->out_msg[i + 2] = MBEDTLS_BYTE_0(n);
Jerry Yud9526692022-02-17 14:23:47 +08006987
Gilles Peskine449bd832023-01-11 14:50:10 +01006988 i += 3; memcpy(ssl->out_msg + i, crt->raw.p, n);
Jerry Yud9526692022-02-17 14:23:47 +08006989 i += n; crt = crt->next;
6990 }
6991
Gilles Peskine449bd832023-01-11 14:50:10 +01006992 ssl->out_msg[4] = MBEDTLS_BYTE_2(i - 7);
6993 ssl->out_msg[5] = MBEDTLS_BYTE_1(i - 7);
6994 ssl->out_msg[6] = MBEDTLS_BYTE_0(i - 7);
Jerry Yud9526692022-02-17 14:23:47 +08006995
6996 ssl->out_msglen = i;
6997 ssl->out_msgtype = MBEDTLS_SSL_MSG_HANDSHAKE;
6998 ssl->out_msg[0] = MBEDTLS_SSL_HS_CERTIFICATE;
6999
7000 ssl->state++;
7001
Gilles Peskine449bd832023-01-11 14:50:10 +01007002 if ((ret = mbedtls_ssl_write_handshake_msg(ssl)) != 0) {
7003 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ssl_write_handshake_msg", ret);
7004 return ret;
Jerry Yud9526692022-02-17 14:23:47 +08007005 }
7006
Gilles Peskine449bd832023-01-11 14:50:10 +01007007 MBEDTLS_SSL_DEBUG_MSG(2, ("<= write certificate"));
Jerry Yud9526692022-02-17 14:23:47 +08007008
Gilles Peskine449bd832023-01-11 14:50:10 +01007009 return ret;
Jerry Yud9526692022-02-17 14:23:47 +08007010}
7011
7012#if defined(MBEDTLS_SSL_RENEGOTIATION) && defined(MBEDTLS_SSL_CLI_C)
7013
7014#if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02007015MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01007016static int ssl_check_peer_crt_unchanged(mbedtls_ssl_context *ssl,
7017 unsigned char *crt_buf,
7018 size_t crt_buf_len)
Jerry Yud9526692022-02-17 14:23:47 +08007019{
7020 mbedtls_x509_crt const * const peer_crt = ssl->session->peer_cert;
7021
Gilles Peskine449bd832023-01-11 14:50:10 +01007022 if (peer_crt == NULL) {
7023 return -1;
7024 }
Jerry Yud9526692022-02-17 14:23:47 +08007025
Gilles Peskine449bd832023-01-11 14:50:10 +01007026 if (peer_crt->raw.len != crt_buf_len) {
7027 return -1;
7028 }
Jerry Yud9526692022-02-17 14:23:47 +08007029
Gilles Peskine449bd832023-01-11 14:50:10 +01007030 return memcmp(peer_crt->raw.p, crt_buf, peer_crt->raw.len);
Jerry Yud9526692022-02-17 14:23:47 +08007031}
7032#else /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02007033MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01007034static int ssl_check_peer_crt_unchanged(mbedtls_ssl_context *ssl,
7035 unsigned char *crt_buf,
7036 size_t crt_buf_len)
Jerry Yud9526692022-02-17 14:23:47 +08007037{
7038 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
7039 unsigned char const * const peer_cert_digest =
7040 ssl->session->peer_cert_digest;
7041 mbedtls_md_type_t const peer_cert_digest_type =
7042 ssl->session->peer_cert_digest_type;
7043 mbedtls_md_info_t const * const digest_info =
Gilles Peskine449bd832023-01-11 14:50:10 +01007044 mbedtls_md_info_from_type(peer_cert_digest_type);
Jerry Yud9526692022-02-17 14:23:47 +08007045 unsigned char tmp_digest[MBEDTLS_SSL_PEER_CERT_DIGEST_MAX_LEN];
7046 size_t digest_len;
7047
Gilles Peskine449bd832023-01-11 14:50:10 +01007048 if (peer_cert_digest == NULL || digest_info == NULL) {
7049 return -1;
7050 }
Jerry Yud9526692022-02-17 14:23:47 +08007051
Gilles Peskine449bd832023-01-11 14:50:10 +01007052 digest_len = mbedtls_md_get_size(digest_info);
7053 if (digest_len > MBEDTLS_SSL_PEER_CERT_DIGEST_MAX_LEN) {
7054 return -1;
7055 }
Jerry Yud9526692022-02-17 14:23:47 +08007056
Gilles Peskine449bd832023-01-11 14:50:10 +01007057 ret = mbedtls_md(digest_info, crt_buf, crt_buf_len, tmp_digest);
7058 if (ret != 0) {
7059 return -1;
7060 }
Jerry Yud9526692022-02-17 14:23:47 +08007061
Gilles Peskine449bd832023-01-11 14:50:10 +01007062 return memcmp(tmp_digest, peer_cert_digest, digest_len);
Jerry Yud9526692022-02-17 14:23:47 +08007063}
7064#endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
7065#endif /* MBEDTLS_SSL_RENEGOTIATION && MBEDTLS_SSL_CLI_C */
7066
7067/*
7068 * Once the certificate message is read, parse it into a cert chain and
7069 * perform basic checks, but leave actual verification to the caller
7070 */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02007071MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01007072static int ssl_parse_certificate_chain(mbedtls_ssl_context *ssl,
7073 mbedtls_x509_crt *chain)
Jerry Yud9526692022-02-17 14:23:47 +08007074{
7075 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
7076#if defined(MBEDTLS_SSL_RENEGOTIATION) && defined(MBEDTLS_SSL_CLI_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01007077 int crt_cnt = 0;
Jerry Yud9526692022-02-17 14:23:47 +08007078#endif
7079 size_t i, n;
7080 uint8_t alert;
7081
Gilles Peskine449bd832023-01-11 14:50:10 +01007082 if (ssl->in_msgtype != MBEDTLS_SSL_MSG_HANDSHAKE) {
7083 MBEDTLS_SSL_DEBUG_MSG(1, ("bad certificate message"));
7084 mbedtls_ssl_send_alert_message(ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
7085 MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE);
7086 return MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE;
Jerry Yud9526692022-02-17 14:23:47 +08007087 }
7088
Gilles Peskine449bd832023-01-11 14:50:10 +01007089 if (ssl->in_msg[0] != MBEDTLS_SSL_HS_CERTIFICATE) {
7090 mbedtls_ssl_send_alert_message(ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
7091 MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE);
7092 return MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE;
Jerry Yud9526692022-02-17 14:23:47 +08007093 }
7094
Gilles Peskine449bd832023-01-11 14:50:10 +01007095 if (ssl->in_hslen < mbedtls_ssl_hs_hdr_len(ssl) + 3 + 3) {
7096 MBEDTLS_SSL_DEBUG_MSG(1, ("bad certificate message"));
7097 mbedtls_ssl_send_alert_message(ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
7098 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR);
7099 return MBEDTLS_ERR_SSL_DECODE_ERROR;
Jerry Yud9526692022-02-17 14:23:47 +08007100 }
7101
Gilles Peskine449bd832023-01-11 14:50:10 +01007102 i = mbedtls_ssl_hs_hdr_len(ssl);
Jerry Yud9526692022-02-17 14:23:47 +08007103
7104 /*
7105 * Same message structure as in mbedtls_ssl_write_certificate()
7106 */
Gilles Peskine449bd832023-01-11 14:50:10 +01007107 n = (ssl->in_msg[i+1] << 8) | ssl->in_msg[i+2];
Jerry Yud9526692022-02-17 14:23:47 +08007108
Gilles Peskine449bd832023-01-11 14:50:10 +01007109 if (ssl->in_msg[i] != 0 ||
7110 ssl->in_hslen != n + 3 + mbedtls_ssl_hs_hdr_len(ssl)) {
7111 MBEDTLS_SSL_DEBUG_MSG(1, ("bad certificate message"));
7112 mbedtls_ssl_send_alert_message(ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
7113 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR);
7114 return MBEDTLS_ERR_SSL_DECODE_ERROR;
Jerry Yud9526692022-02-17 14:23:47 +08007115 }
7116
7117 /* Make &ssl->in_msg[i] point to the beginning of the CRT chain. */
7118 i += 3;
7119
7120 /* Iterate through and parse the CRTs in the provided chain. */
Gilles Peskine449bd832023-01-11 14:50:10 +01007121 while (i < ssl->in_hslen) {
Jerry Yud9526692022-02-17 14:23:47 +08007122 /* Check that there's room for the next CRT's length fields. */
Gilles Peskine449bd832023-01-11 14:50:10 +01007123 if (i + 3 > ssl->in_hslen) {
7124 MBEDTLS_SSL_DEBUG_MSG(1, ("bad certificate message"));
7125 mbedtls_ssl_send_alert_message(ssl,
7126 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
7127 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR);
7128 return MBEDTLS_ERR_SSL_DECODE_ERROR;
Jerry Yud9526692022-02-17 14:23:47 +08007129 }
7130 /* In theory, the CRT can be up to 2**24 Bytes, but we don't support
7131 * anything beyond 2**16 ~ 64K. */
Gilles Peskine449bd832023-01-11 14:50:10 +01007132 if (ssl->in_msg[i] != 0) {
7133 MBEDTLS_SSL_DEBUG_MSG(1, ("bad certificate message"));
7134 mbedtls_ssl_send_alert_message(ssl,
7135 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
7136 MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT);
7137 return MBEDTLS_ERR_SSL_BAD_CERTIFICATE;
Jerry Yud9526692022-02-17 14:23:47 +08007138 }
7139
7140 /* Read length of the next CRT in the chain. */
Gilles Peskine449bd832023-01-11 14:50:10 +01007141 n = ((unsigned int) ssl->in_msg[i + 1] << 8)
Jerry Yud9526692022-02-17 14:23:47 +08007142 | (unsigned int) ssl->in_msg[i + 2];
7143 i += 3;
7144
Gilles Peskine449bd832023-01-11 14:50:10 +01007145 if (n < 128 || i + n > ssl->in_hslen) {
7146 MBEDTLS_SSL_DEBUG_MSG(1, ("bad certificate message"));
7147 mbedtls_ssl_send_alert_message(ssl,
7148 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
7149 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR);
7150 return MBEDTLS_ERR_SSL_DECODE_ERROR;
Jerry Yud9526692022-02-17 14:23:47 +08007151 }
7152
7153 /* Check if we're handling the first CRT in the chain. */
7154#if defined(MBEDTLS_SSL_RENEGOTIATION) && defined(MBEDTLS_SSL_CLI_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01007155 if (crt_cnt++ == 0 &&
Jerry Yud9526692022-02-17 14:23:47 +08007156 ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT &&
Gilles Peskine449bd832023-01-11 14:50:10 +01007157 ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS) {
Jerry Yud9526692022-02-17 14:23:47 +08007158 /* During client-side renegotiation, check that the server's
7159 * end-CRTs hasn't changed compared to the initial handshake,
7160 * mitigating the triple handshake attack. On success, reuse
7161 * the original end-CRT instead of parsing it again. */
Gilles Peskine449bd832023-01-11 14:50:10 +01007162 MBEDTLS_SSL_DEBUG_MSG(3, ("Check that peer CRT hasn't changed during renegotiation"));
7163 if (ssl_check_peer_crt_unchanged(ssl,
7164 &ssl->in_msg[i],
7165 n) != 0) {
7166 MBEDTLS_SSL_DEBUG_MSG(1, ("new server cert during renegotiation"));
7167 mbedtls_ssl_send_alert_message(ssl,
7168 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
7169 MBEDTLS_SSL_ALERT_MSG_ACCESS_DENIED);
7170 return MBEDTLS_ERR_SSL_BAD_CERTIFICATE;
Jerry Yud9526692022-02-17 14:23:47 +08007171 }
7172
7173 /* Now we can safely free the original chain. */
Gilles Peskine449bd832023-01-11 14:50:10 +01007174 ssl_clear_peer_cert(ssl->session);
Jerry Yud9526692022-02-17 14:23:47 +08007175 }
7176#endif /* MBEDTLS_SSL_RENEGOTIATION && MBEDTLS_SSL_CLI_C */
7177
7178 /* Parse the next certificate in the chain. */
7179#if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
Gilles Peskine449bd832023-01-11 14:50:10 +01007180 ret = mbedtls_x509_crt_parse_der(chain, ssl->in_msg + i, n);
Jerry Yud9526692022-02-17 14:23:47 +08007181#else
7182 /* If we don't need to store the CRT chain permanently, parse
7183 * it in-place from the input buffer instead of making a copy. */
Gilles Peskine449bd832023-01-11 14:50:10 +01007184 ret = mbedtls_x509_crt_parse_der_nocopy(chain, ssl->in_msg + i, n);
Jerry Yud9526692022-02-17 14:23:47 +08007185#endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
Gilles Peskine449bd832023-01-11 14:50:10 +01007186 switch (ret) {
Jerry Yud9526692022-02-17 14:23:47 +08007187 case 0: /*ok*/
7188 case MBEDTLS_ERR_X509_UNKNOWN_SIG_ALG + MBEDTLS_ERR_OID_NOT_FOUND:
7189 /* Ignore certificate with an unknown algorithm: maybe a
7190 prior certificate was already trusted. */
7191 break;
7192
7193 case MBEDTLS_ERR_X509_ALLOC_FAILED:
7194 alert = MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR;
7195 goto crt_parse_der_failed;
7196
7197 case MBEDTLS_ERR_X509_UNKNOWN_VERSION:
7198 alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT;
7199 goto crt_parse_der_failed;
7200
7201 default:
7202 alert = MBEDTLS_SSL_ALERT_MSG_BAD_CERT;
Gilles Peskine449bd832023-01-11 14:50:10 +01007203crt_parse_der_failed:
7204 mbedtls_ssl_send_alert_message(ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL, alert);
7205 MBEDTLS_SSL_DEBUG_RET(1, " mbedtls_x509_crt_parse_der", ret);
7206 return ret;
Jerry Yud9526692022-02-17 14:23:47 +08007207 }
7208
7209 i += n;
7210 }
7211
Gilles Peskine449bd832023-01-11 14:50:10 +01007212 MBEDTLS_SSL_DEBUG_CRT(3, "peer certificate", chain);
7213 return 0;
Jerry Yud9526692022-02-17 14:23:47 +08007214}
7215
7216#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02007217MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01007218static int ssl_srv_check_client_no_crt_notification(mbedtls_ssl_context *ssl)
Jerry Yud9526692022-02-17 14:23:47 +08007219{
Gilles Peskine449bd832023-01-11 14:50:10 +01007220 if (ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT) {
7221 return -1;
7222 }
Jerry Yud9526692022-02-17 14:23:47 +08007223
Gilles Peskine449bd832023-01-11 14:50:10 +01007224 if (ssl->in_hslen == 3 + mbedtls_ssl_hs_hdr_len(ssl) &&
Jerry Yud9526692022-02-17 14:23:47 +08007225 ssl->in_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE &&
7226 ssl->in_msg[0] == MBEDTLS_SSL_HS_CERTIFICATE &&
Gilles Peskine449bd832023-01-11 14:50:10 +01007227 memcmp(ssl->in_msg + mbedtls_ssl_hs_hdr_len(ssl), "\0\0\0", 3) == 0) {
7228 MBEDTLS_SSL_DEBUG_MSG(1, ("peer has no certificate"));
7229 return 0;
Jerry Yud9526692022-02-17 14:23:47 +08007230 }
Gilles Peskine449bd832023-01-11 14:50:10 +01007231 return -1;
Jerry Yud9526692022-02-17 14:23:47 +08007232}
7233#endif /* MBEDTLS_SSL_SRV_C */
7234
7235/* Check if a certificate message is expected.
7236 * Return either
7237 * - SSL_CERTIFICATE_EXPECTED, or
7238 * - SSL_CERTIFICATE_SKIP
7239 * indicating whether a Certificate message is expected or not.
7240 */
7241#define SSL_CERTIFICATE_EXPECTED 0
7242#define SSL_CERTIFICATE_SKIP 1
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02007243MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01007244static int ssl_parse_certificate_coordinate(mbedtls_ssl_context *ssl,
7245 int authmode)
Jerry Yud9526692022-02-17 14:23:47 +08007246{
7247 const mbedtls_ssl_ciphersuite_t *ciphersuite_info =
7248 ssl->handshake->ciphersuite_info;
7249
Gilles Peskine449bd832023-01-11 14:50:10 +01007250 if (!mbedtls_ssl_ciphersuite_uses_srv_cert(ciphersuite_info)) {
7251 return SSL_CERTIFICATE_SKIP;
7252 }
Jerry Yud9526692022-02-17 14:23:47 +08007253
7254#if defined(MBEDTLS_SSL_SRV_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01007255 if (ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER) {
7256 if (ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_RSA_PSK) {
7257 return SSL_CERTIFICATE_SKIP;
7258 }
Jerry Yud9526692022-02-17 14:23:47 +08007259
Gilles Peskine449bd832023-01-11 14:50:10 +01007260 if (authmode == MBEDTLS_SSL_VERIFY_NONE) {
Jerry Yud9526692022-02-17 14:23:47 +08007261 ssl->session_negotiate->verify_result =
7262 MBEDTLS_X509_BADCERT_SKIP_VERIFY;
Gilles Peskine449bd832023-01-11 14:50:10 +01007263 return SSL_CERTIFICATE_SKIP;
Jerry Yud9526692022-02-17 14:23:47 +08007264 }
7265 }
7266#else
7267 ((void) authmode);
7268#endif /* MBEDTLS_SSL_SRV_C */
7269
Gilles Peskine449bd832023-01-11 14:50:10 +01007270 return SSL_CERTIFICATE_EXPECTED;
Jerry Yud9526692022-02-17 14:23:47 +08007271}
7272
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02007273MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01007274static int ssl_parse_certificate_verify(mbedtls_ssl_context *ssl,
7275 int authmode,
7276 mbedtls_x509_crt *chain,
7277 void *rs_ctx)
Jerry Yud9526692022-02-17 14:23:47 +08007278{
7279 int ret = 0;
7280 const mbedtls_ssl_ciphersuite_t *ciphersuite_info =
7281 ssl->handshake->ciphersuite_info;
7282 int have_ca_chain = 0;
7283
7284 int (*f_vrfy)(void *, mbedtls_x509_crt *, int, uint32_t *);
7285 void *p_vrfy;
7286
Gilles Peskine449bd832023-01-11 14:50:10 +01007287 if (authmode == MBEDTLS_SSL_VERIFY_NONE) {
7288 return 0;
7289 }
Jerry Yud9526692022-02-17 14:23:47 +08007290
Gilles Peskine449bd832023-01-11 14:50:10 +01007291 if (ssl->f_vrfy != NULL) {
7292 MBEDTLS_SSL_DEBUG_MSG(3, ("Use context-specific verification callback"));
Jerry Yud9526692022-02-17 14:23:47 +08007293 f_vrfy = ssl->f_vrfy;
7294 p_vrfy = ssl->p_vrfy;
Gilles Peskine449bd832023-01-11 14:50:10 +01007295 } else {
7296 MBEDTLS_SSL_DEBUG_MSG(3, ("Use configuration-specific verification callback"));
Jerry Yud9526692022-02-17 14:23:47 +08007297 f_vrfy = ssl->conf->f_vrfy;
7298 p_vrfy = ssl->conf->p_vrfy;
7299 }
7300
7301 /*
7302 * Main check: verify certificate
7303 */
7304#if defined(MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK)
Gilles Peskine449bd832023-01-11 14:50:10 +01007305 if (ssl->conf->f_ca_cb != NULL) {
Jerry Yud9526692022-02-17 14:23:47 +08007306 ((void) rs_ctx);
7307 have_ca_chain = 1;
7308
Gilles Peskine449bd832023-01-11 14:50:10 +01007309 MBEDTLS_SSL_DEBUG_MSG(3, ("use CA callback for X.509 CRT verification"));
Jerry Yud9526692022-02-17 14:23:47 +08007310 ret = mbedtls_x509_crt_verify_with_ca_cb(
7311 chain,
7312 ssl->conf->f_ca_cb,
7313 ssl->conf->p_ca_cb,
7314 ssl->conf->cert_profile,
7315 ssl->hostname,
7316 &ssl->session_negotiate->verify_result,
Gilles Peskine449bd832023-01-11 14:50:10 +01007317 f_vrfy, p_vrfy);
7318 } else
Jerry Yud9526692022-02-17 14:23:47 +08007319#endif /* MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK */
7320 {
7321 mbedtls_x509_crt *ca_chain;
7322 mbedtls_x509_crl *ca_crl;
7323
7324#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
Gilles Peskine449bd832023-01-11 14:50:10 +01007325 if (ssl->handshake->sni_ca_chain != NULL) {
Jerry Yud9526692022-02-17 14:23:47 +08007326 ca_chain = ssl->handshake->sni_ca_chain;
7327 ca_crl = ssl->handshake->sni_ca_crl;
Gilles Peskine449bd832023-01-11 14:50:10 +01007328 } else
Jerry Yud9526692022-02-17 14:23:47 +08007329#endif
7330 {
7331 ca_chain = ssl->conf->ca_chain;
7332 ca_crl = ssl->conf->ca_crl;
7333 }
7334
Gilles Peskine449bd832023-01-11 14:50:10 +01007335 if (ca_chain != NULL) {
Jerry Yud9526692022-02-17 14:23:47 +08007336 have_ca_chain = 1;
Gilles Peskine449bd832023-01-11 14:50:10 +01007337 }
Jerry Yud9526692022-02-17 14:23:47 +08007338
7339 ret = mbedtls_x509_crt_verify_restartable(
7340 chain,
7341 ca_chain, ca_crl,
7342 ssl->conf->cert_profile,
7343 ssl->hostname,
7344 &ssl->session_negotiate->verify_result,
Gilles Peskine449bd832023-01-11 14:50:10 +01007345 f_vrfy, p_vrfy, rs_ctx);
Jerry Yud9526692022-02-17 14:23:47 +08007346 }
7347
Gilles Peskine449bd832023-01-11 14:50:10 +01007348 if (ret != 0) {
7349 MBEDTLS_SSL_DEBUG_RET(1, "x509_verify_cert", ret);
Jerry Yud9526692022-02-17 14:23:47 +08007350 }
7351
7352#if defined(MBEDTLS_SSL_ECP_RESTARTABLE_ENABLED)
Gilles Peskine449bd832023-01-11 14:50:10 +01007353 if (ret == MBEDTLS_ERR_ECP_IN_PROGRESS) {
7354 return MBEDTLS_ERR_SSL_CRYPTO_IN_PROGRESS;
7355 }
Jerry Yud9526692022-02-17 14:23:47 +08007356#endif
7357
7358 /*
7359 * Secondary checks: always done, but change 'ret' only if it was 0
7360 */
7361
7362#if defined(MBEDTLS_ECP_C)
7363 {
7364 const mbedtls_pk_context *pk = &chain->pk;
7365
Manuel Pégourié-Gonnard66b0d612022-06-17 10:49:29 +02007366 /* If certificate uses an EC key, make sure the curve is OK.
7367 * This is a public key, so it can't be opaque, so can_do() is a good
7368 * enough check to ensure pk_ec() is safe to use here. */
Gilles Peskine449bd832023-01-11 14:50:10 +01007369 if (mbedtls_pk_can_do(pk, MBEDTLS_PK_ECKEY)) {
Leonid Rozenboim19e59732022-08-08 16:52:38 -07007370 /* and in the unlikely case the above assumption no longer holds
7371 * we are making sure that pk_ec() here does not return a NULL
7372 */
Gilles Peskine449bd832023-01-11 14:50:10 +01007373 const mbedtls_ecp_keypair *ec = mbedtls_pk_ec(*pk);
7374 if (ec == NULL) {
7375 MBEDTLS_SSL_DEBUG_MSG(1, ("mbedtls_pk_ec() returned NULL"));
7376 return MBEDTLS_ERR_SSL_INTERNAL_ERROR;
Leonid Rozenboim19e59732022-08-08 16:52:38 -07007377 }
Jerry Yud9526692022-02-17 14:23:47 +08007378
Gilles Peskine449bd832023-01-11 14:50:10 +01007379 if (mbedtls_ssl_check_curve(ssl, ec->grp.id) != 0) {
Leonid Rozenboim19e59732022-08-08 16:52:38 -07007380 ssl->session_negotiate->verify_result |=
7381 MBEDTLS_X509_BADCERT_BAD_KEY;
7382
Gilles Peskine449bd832023-01-11 14:50:10 +01007383 MBEDTLS_SSL_DEBUG_MSG(1, ("bad certificate (EC key curve)"));
7384 if (ret == 0) {
Leonid Rozenboim19e59732022-08-08 16:52:38 -07007385 ret = MBEDTLS_ERR_SSL_BAD_CERTIFICATE;
Gilles Peskine449bd832023-01-11 14:50:10 +01007386 }
Leonid Rozenboim19e59732022-08-08 16:52:38 -07007387 }
Jerry Yud9526692022-02-17 14:23:47 +08007388 }
7389 }
7390#endif /* MBEDTLS_ECP_C */
7391
Gilles Peskine449bd832023-01-11 14:50:10 +01007392 if (mbedtls_ssl_check_cert_usage(chain,
7393 ciphersuite_info,
7394 !ssl->conf->endpoint,
7395 &ssl->session_negotiate->verify_result) != 0) {
7396 MBEDTLS_SSL_DEBUG_MSG(1, ("bad certificate (usage extensions)"));
7397 if (ret == 0) {
Jerry Yud9526692022-02-17 14:23:47 +08007398 ret = MBEDTLS_ERR_SSL_BAD_CERTIFICATE;
Gilles Peskine449bd832023-01-11 14:50:10 +01007399 }
Jerry Yud9526692022-02-17 14:23:47 +08007400 }
7401
7402 /* mbedtls_x509_crt_verify_with_profile is supposed to report a
7403 * verification failure through MBEDTLS_ERR_X509_CERT_VERIFY_FAILED,
7404 * with details encoded in the verification flags. All other kinds
7405 * of error codes, including those from the user provided f_vrfy
7406 * functions, are treated as fatal and lead to a failure of
7407 * ssl_parse_certificate even if verification was optional. */
Gilles Peskine449bd832023-01-11 14:50:10 +01007408 if (authmode == MBEDTLS_SSL_VERIFY_OPTIONAL &&
7409 (ret == MBEDTLS_ERR_X509_CERT_VERIFY_FAILED ||
7410 ret == MBEDTLS_ERR_SSL_BAD_CERTIFICATE)) {
Jerry Yud9526692022-02-17 14:23:47 +08007411 ret = 0;
7412 }
7413
Gilles Peskine449bd832023-01-11 14:50:10 +01007414 if (have_ca_chain == 0 && authmode == MBEDTLS_SSL_VERIFY_REQUIRED) {
7415 MBEDTLS_SSL_DEBUG_MSG(1, ("got no CA chain"));
Jerry Yud9526692022-02-17 14:23:47 +08007416 ret = MBEDTLS_ERR_SSL_CA_CHAIN_REQUIRED;
7417 }
7418
Gilles Peskine449bd832023-01-11 14:50:10 +01007419 if (ret != 0) {
Jerry Yud9526692022-02-17 14:23:47 +08007420 uint8_t alert;
7421
7422 /* The certificate may have been rejected for several reasons.
7423 Pick one and send the corresponding alert. Which alert to send
7424 may be a subject of debate in some cases. */
Gilles Peskine449bd832023-01-11 14:50:10 +01007425 if (ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_OTHER) {
Jerry Yud9526692022-02-17 14:23:47 +08007426 alert = MBEDTLS_SSL_ALERT_MSG_ACCESS_DENIED;
Gilles Peskine449bd832023-01-11 14:50:10 +01007427 } else if (ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_CN_MISMATCH) {
Jerry Yud9526692022-02-17 14:23:47 +08007428 alert = MBEDTLS_SSL_ALERT_MSG_BAD_CERT;
Gilles Peskine449bd832023-01-11 14:50:10 +01007429 } else if (ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_KEY_USAGE) {
Jerry Yud9526692022-02-17 14:23:47 +08007430 alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT;
Gilles Peskine449bd832023-01-11 14:50:10 +01007431 } else if (ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_EXT_KEY_USAGE) {
Jerry Yud9526692022-02-17 14:23:47 +08007432 alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT;
Gilles Peskine449bd832023-01-11 14:50:10 +01007433 } else if (ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_NS_CERT_TYPE) {
Jerry Yud9526692022-02-17 14:23:47 +08007434 alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT;
Gilles Peskine449bd832023-01-11 14:50:10 +01007435 } else if (ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_BAD_PK) {
Jerry Yud9526692022-02-17 14:23:47 +08007436 alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT;
Gilles Peskine449bd832023-01-11 14:50:10 +01007437 } else if (ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_BAD_KEY) {
Jerry Yud9526692022-02-17 14:23:47 +08007438 alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT;
Gilles Peskine449bd832023-01-11 14:50:10 +01007439 } else if (ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_EXPIRED) {
Jerry Yud9526692022-02-17 14:23:47 +08007440 alert = MBEDTLS_SSL_ALERT_MSG_CERT_EXPIRED;
Gilles Peskine449bd832023-01-11 14:50:10 +01007441 } else if (ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_REVOKED) {
Jerry Yud9526692022-02-17 14:23:47 +08007442 alert = MBEDTLS_SSL_ALERT_MSG_CERT_REVOKED;
Gilles Peskine449bd832023-01-11 14:50:10 +01007443 } else if (ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_NOT_TRUSTED) {
Jerry Yud9526692022-02-17 14:23:47 +08007444 alert = MBEDTLS_SSL_ALERT_MSG_UNKNOWN_CA;
Gilles Peskine449bd832023-01-11 14:50:10 +01007445 } else {
Jerry Yud9526692022-02-17 14:23:47 +08007446 alert = MBEDTLS_SSL_ALERT_MSG_CERT_UNKNOWN;
Gilles Peskine449bd832023-01-11 14:50:10 +01007447 }
7448 mbedtls_ssl_send_alert_message(ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
7449 alert);
Jerry Yud9526692022-02-17 14:23:47 +08007450 }
7451
7452#if defined(MBEDTLS_DEBUG_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01007453 if (ssl->session_negotiate->verify_result != 0) {
7454 MBEDTLS_SSL_DEBUG_MSG(3, ("! Certificate verification flags %08x",
7455 (unsigned int) ssl->session_negotiate->verify_result));
7456 } else {
7457 MBEDTLS_SSL_DEBUG_MSG(3, ("Certificate verification flags clear"));
Jerry Yud9526692022-02-17 14:23:47 +08007458 }
7459#endif /* MBEDTLS_DEBUG_C */
7460
Gilles Peskine449bd832023-01-11 14:50:10 +01007461 return ret;
Jerry Yud9526692022-02-17 14:23:47 +08007462}
7463
7464#if !defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02007465MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01007466static int ssl_remember_peer_crt_digest(mbedtls_ssl_context *ssl,
7467 unsigned char *start, size_t len)
Jerry Yud9526692022-02-17 14:23:47 +08007468{
7469 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
7470 /* Remember digest of the peer's end-CRT. */
7471 ssl->session_negotiate->peer_cert_digest =
Gilles Peskine449bd832023-01-11 14:50:10 +01007472 mbedtls_calloc(1, MBEDTLS_SSL_PEER_CERT_DIGEST_DFL_LEN);
7473 if (ssl->session_negotiate->peer_cert_digest == NULL) {
7474 MBEDTLS_SSL_DEBUG_MSG(1, ("alloc(%d bytes) failed",
7475 MBEDTLS_SSL_PEER_CERT_DIGEST_DFL_LEN));
7476 mbedtls_ssl_send_alert_message(ssl,
7477 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
7478 MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR);
Jerry Yud9526692022-02-17 14:23:47 +08007479
Gilles Peskine449bd832023-01-11 14:50:10 +01007480 return MBEDTLS_ERR_SSL_ALLOC_FAILED;
Jerry Yud9526692022-02-17 14:23:47 +08007481 }
7482
Gilles Peskine449bd832023-01-11 14:50:10 +01007483 ret = mbedtls_md(mbedtls_md_info_from_type(
7484 MBEDTLS_SSL_PEER_CERT_DIGEST_DFL_TYPE),
7485 start, len,
7486 ssl->session_negotiate->peer_cert_digest);
Jerry Yud9526692022-02-17 14:23:47 +08007487
7488 ssl->session_negotiate->peer_cert_digest_type =
7489 MBEDTLS_SSL_PEER_CERT_DIGEST_DFL_TYPE;
7490 ssl->session_negotiate->peer_cert_digest_len =
7491 MBEDTLS_SSL_PEER_CERT_DIGEST_DFL_LEN;
7492
Gilles Peskine449bd832023-01-11 14:50:10 +01007493 return ret;
Jerry Yud9526692022-02-17 14:23:47 +08007494}
7495
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02007496MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01007497static int ssl_remember_peer_pubkey(mbedtls_ssl_context *ssl,
7498 unsigned char *start, size_t len)
Jerry Yud9526692022-02-17 14:23:47 +08007499{
7500 unsigned char *end = start + len;
7501 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
7502
7503 /* Make a copy of the peer's raw public key. */
Gilles Peskine449bd832023-01-11 14:50:10 +01007504 mbedtls_pk_init(&ssl->handshake->peer_pubkey);
7505 ret = mbedtls_pk_parse_subpubkey(&start, end,
7506 &ssl->handshake->peer_pubkey);
7507 if (ret != 0) {
Jerry Yud9526692022-02-17 14:23:47 +08007508 /* We should have parsed the public key before. */
Gilles Peskine449bd832023-01-11 14:50:10 +01007509 return MBEDTLS_ERR_SSL_INTERNAL_ERROR;
Jerry Yud9526692022-02-17 14:23:47 +08007510 }
7511
Gilles Peskine449bd832023-01-11 14:50:10 +01007512 return 0;
Jerry Yud9526692022-02-17 14:23:47 +08007513}
7514#endif /* !MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
7515
Gilles Peskine449bd832023-01-11 14:50:10 +01007516int mbedtls_ssl_parse_certificate(mbedtls_ssl_context *ssl)
Jerry Yud9526692022-02-17 14:23:47 +08007517{
7518 int ret = 0;
7519 int crt_expected;
7520#if defined(MBEDTLS_SSL_SRV_C) && defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
7521 const int authmode = ssl->handshake->sni_authmode != MBEDTLS_SSL_VERIFY_UNSET
7522 ? ssl->handshake->sni_authmode
7523 : ssl->conf->authmode;
7524#else
7525 const int authmode = ssl->conf->authmode;
7526#endif
7527 void *rs_ctx = NULL;
7528 mbedtls_x509_crt *chain = NULL;
7529
Gilles Peskine449bd832023-01-11 14:50:10 +01007530 MBEDTLS_SSL_DEBUG_MSG(2, ("=> parse certificate"));
Jerry Yud9526692022-02-17 14:23:47 +08007531
Gilles Peskine449bd832023-01-11 14:50:10 +01007532 crt_expected = ssl_parse_certificate_coordinate(ssl, authmode);
7533 if (crt_expected == SSL_CERTIFICATE_SKIP) {
7534 MBEDTLS_SSL_DEBUG_MSG(2, ("<= skip parse certificate"));
Jerry Yud9526692022-02-17 14:23:47 +08007535 goto exit;
7536 }
7537
7538#if defined(MBEDTLS_SSL_ECP_RESTARTABLE_ENABLED)
Gilles Peskine449bd832023-01-11 14:50:10 +01007539 if (ssl->handshake->ecrs_enabled &&
7540 ssl->handshake->ecrs_state == ssl_ecrs_crt_verify) {
Jerry Yud9526692022-02-17 14:23:47 +08007541 chain = ssl->handshake->ecrs_peer_cert;
7542 ssl->handshake->ecrs_peer_cert = NULL;
7543 goto crt_verify;
7544 }
7545#endif
7546
Gilles Peskine449bd832023-01-11 14:50:10 +01007547 if ((ret = mbedtls_ssl_read_record(ssl, 1)) != 0) {
Jerry Yud9526692022-02-17 14:23:47 +08007548 /* mbedtls_ssl_read_record may have sent an alert already. We
7549 let it decide whether to alert. */
Gilles Peskine449bd832023-01-11 14:50:10 +01007550 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ssl_read_record", ret);
Jerry Yud9526692022-02-17 14:23:47 +08007551 goto exit;
7552 }
7553
7554#if defined(MBEDTLS_SSL_SRV_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01007555 if (ssl_srv_check_client_no_crt_notification(ssl) == 0) {
Jerry Yud9526692022-02-17 14:23:47 +08007556 ssl->session_negotiate->verify_result = MBEDTLS_X509_BADCERT_MISSING;
7557
Gilles Peskine449bd832023-01-11 14:50:10 +01007558 if (authmode != MBEDTLS_SSL_VERIFY_OPTIONAL) {
Jerry Yud9526692022-02-17 14:23:47 +08007559 ret = MBEDTLS_ERR_SSL_NO_CLIENT_CERTIFICATE;
Gilles Peskine449bd832023-01-11 14:50:10 +01007560 }
Jerry Yud9526692022-02-17 14:23:47 +08007561
7562 goto exit;
7563 }
7564#endif /* MBEDTLS_SSL_SRV_C */
7565
7566 /* Clear existing peer CRT structure in case we tried to
7567 * reuse a session but it failed, and allocate a new one. */
Gilles Peskine449bd832023-01-11 14:50:10 +01007568 ssl_clear_peer_cert(ssl->session_negotiate);
Jerry Yud9526692022-02-17 14:23:47 +08007569
Gilles Peskine449bd832023-01-11 14:50:10 +01007570 chain = mbedtls_calloc(1, sizeof(mbedtls_x509_crt));
7571 if (chain == NULL) {
7572 MBEDTLS_SSL_DEBUG_MSG(1, ("alloc(%" MBEDTLS_PRINTF_SIZET " bytes) failed",
7573 sizeof(mbedtls_x509_crt)));
7574 mbedtls_ssl_send_alert_message(ssl,
7575 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
7576 MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR);
Jerry Yud9526692022-02-17 14:23:47 +08007577
7578 ret = MBEDTLS_ERR_SSL_ALLOC_FAILED;
7579 goto exit;
7580 }
Gilles Peskine449bd832023-01-11 14:50:10 +01007581 mbedtls_x509_crt_init(chain);
Jerry Yud9526692022-02-17 14:23:47 +08007582
Gilles Peskine449bd832023-01-11 14:50:10 +01007583 ret = ssl_parse_certificate_chain(ssl, chain);
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#if defined(MBEDTLS_SSL_ECP_RESTARTABLE_ENABLED)
Gilles Peskine449bd832023-01-11 14:50:10 +01007589 if (ssl->handshake->ecrs_enabled) {
Jerry Yud9526692022-02-17 14:23:47 +08007590 ssl->handshake->ecrs_state = ssl_ecrs_crt_verify;
Gilles Peskine449bd832023-01-11 14:50:10 +01007591 }
Jerry Yud9526692022-02-17 14:23:47 +08007592
7593crt_verify:
Gilles Peskine449bd832023-01-11 14:50:10 +01007594 if (ssl->handshake->ecrs_enabled) {
Jerry Yud9526692022-02-17 14:23:47 +08007595 rs_ctx = &ssl->handshake->ecrs_ctx;
Gilles Peskine449bd832023-01-11 14:50:10 +01007596 }
Jerry Yud9526692022-02-17 14:23:47 +08007597#endif
7598
Gilles Peskine449bd832023-01-11 14:50:10 +01007599 ret = ssl_parse_certificate_verify(ssl, authmode,
7600 chain, rs_ctx);
7601 if (ret != 0) {
Jerry Yud9526692022-02-17 14:23:47 +08007602 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01007603 }
Jerry Yud9526692022-02-17 14:23:47 +08007604
7605#if !defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
7606 {
7607 unsigned char *crt_start, *pk_start;
7608 size_t crt_len, pk_len;
7609
7610 /* We parse the CRT chain without copying, so
7611 * these pointers point into the input buffer,
7612 * and are hence still valid after freeing the
7613 * CRT chain. */
7614
7615 crt_start = chain->raw.p;
7616 crt_len = chain->raw.len;
7617
7618 pk_start = chain->pk_raw.p;
7619 pk_len = chain->pk_raw.len;
7620
7621 /* Free the CRT structures before computing
7622 * digest and copying the peer's public key. */
Gilles Peskine449bd832023-01-11 14:50:10 +01007623 mbedtls_x509_crt_free(chain);
7624 mbedtls_free(chain);
Jerry Yud9526692022-02-17 14:23:47 +08007625 chain = NULL;
7626
Gilles Peskine449bd832023-01-11 14:50:10 +01007627 ret = ssl_remember_peer_crt_digest(ssl, crt_start, crt_len);
7628 if (ret != 0) {
Jerry Yud9526692022-02-17 14:23:47 +08007629 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01007630 }
Jerry Yud9526692022-02-17 14:23:47 +08007631
Gilles Peskine449bd832023-01-11 14:50:10 +01007632 ret = ssl_remember_peer_pubkey(ssl, pk_start, pk_len);
7633 if (ret != 0) {
Jerry Yud9526692022-02-17 14:23:47 +08007634 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01007635 }
Jerry Yud9526692022-02-17 14:23:47 +08007636 }
7637#else /* !MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
7638 /* Pass ownership to session structure. */
7639 ssl->session_negotiate->peer_cert = chain;
7640 chain = NULL;
7641#endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
7642
Gilles Peskine449bd832023-01-11 14:50:10 +01007643 MBEDTLS_SSL_DEBUG_MSG(2, ("<= parse certificate"));
Jerry Yud9526692022-02-17 14:23:47 +08007644
7645exit:
7646
Gilles Peskine449bd832023-01-11 14:50:10 +01007647 if (ret == 0) {
Jerry Yud9526692022-02-17 14:23:47 +08007648 ssl->state++;
Gilles Peskine449bd832023-01-11 14:50:10 +01007649 }
Jerry Yud9526692022-02-17 14:23:47 +08007650
7651#if defined(MBEDTLS_SSL_ECP_RESTARTABLE_ENABLED)
Gilles Peskine449bd832023-01-11 14:50:10 +01007652 if (ret == MBEDTLS_ERR_SSL_CRYPTO_IN_PROGRESS) {
Jerry Yud9526692022-02-17 14:23:47 +08007653 ssl->handshake->ecrs_peer_cert = chain;
7654 chain = NULL;
7655 }
7656#endif
7657
Gilles Peskine449bd832023-01-11 14:50:10 +01007658 if (chain != NULL) {
7659 mbedtls_x509_crt_free(chain);
7660 mbedtls_free(chain);
Jerry Yud9526692022-02-17 14:23:47 +08007661 }
7662
Gilles Peskine449bd832023-01-11 14:50:10 +01007663 return ret;
Jerry Yud9526692022-02-17 14:23:47 +08007664}
7665#endif /* MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED */
7666
Andrzej Kurek25f27152022-08-17 16:09:31 -04007667#if defined(MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Manuel Pégourié-Gonnard226aa152023-02-05 09:46:59 +01007668static int ssl_calc_finished_tls_sha256(
Gilles Peskine449bd832023-01-11 14:50:10 +01007669 mbedtls_ssl_context *ssl, unsigned char *buf, int from)
Jerry Yu615bd6f2022-02-17 14:25:15 +08007670{
7671 int len = 12;
7672 const char *sender;
7673 unsigned char padbuf[32];
7674#if defined(MBEDTLS_USE_PSA_CRYPTO)
7675 size_t hash_size;
7676 psa_hash_operation_t sha256_psa = PSA_HASH_OPERATION_INIT;
7677 psa_status_t status;
7678#else
Manuel Pégourié-Gonnarde1a4caa2023-02-06 10:14:25 +01007679 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnardf057ecf2023-02-24 13:19:17 +01007680 mbedtls_md_context_t sha256;
Jerry Yu615bd6f2022-02-17 14:25:15 +08007681#endif
7682
7683 mbedtls_ssl_session *session = ssl->session_negotiate;
Gilles Peskine449bd832023-01-11 14:50:10 +01007684 if (!session) {
Jerry Yu615bd6f2022-02-17 14:25:15 +08007685 session = ssl->session;
Gilles Peskine449bd832023-01-11 14:50:10 +01007686 }
Jerry Yu615bd6f2022-02-17 14:25:15 +08007687
Gilles Peskine449bd832023-01-11 14:50:10 +01007688 sender = (from == MBEDTLS_SSL_IS_CLIENT)
Jerry Yu615bd6f2022-02-17 14:25:15 +08007689 ? "client finished"
7690 : "server finished";
7691
7692#if defined(MBEDTLS_USE_PSA_CRYPTO)
7693 sha256_psa = psa_hash_operation_init();
7694
Gilles Peskine449bd832023-01-11 14:50:10 +01007695 MBEDTLS_SSL_DEBUG_MSG(2, ("=> calc PSA finished tls sha256"));
Jerry Yu615bd6f2022-02-17 14:25:15 +08007696
Gilles Peskine449bd832023-01-11 14:50:10 +01007697 status = psa_hash_clone(&ssl->handshake->fin_sha256_psa, &sha256_psa);
7698 if (status != PSA_SUCCESS) {
Manuel Pégourié-Gonnarde1a4caa2023-02-06 10:14:25 +01007699 goto exit;
Jerry Yu615bd6f2022-02-17 14:25:15 +08007700 }
7701
Gilles Peskine449bd832023-01-11 14:50:10 +01007702 status = psa_hash_finish(&sha256_psa, padbuf, sizeof(padbuf), &hash_size);
7703 if (status != PSA_SUCCESS) {
Manuel Pégourié-Gonnarde1a4caa2023-02-06 10:14:25 +01007704 goto exit;
Jerry Yu615bd6f2022-02-17 14:25:15 +08007705 }
Gilles Peskine449bd832023-01-11 14:50:10 +01007706 MBEDTLS_SSL_DEBUG_BUF(3, "PSA calculated padbuf", padbuf, 32);
Jerry Yu615bd6f2022-02-17 14:25:15 +08007707#else
7708
Manuel Pégourié-Gonnardf057ecf2023-02-24 13:19:17 +01007709 mbedtls_md_init(&sha256);
Jerry Yu615bd6f2022-02-17 14:25:15 +08007710
Gilles Peskine449bd832023-01-11 14:50:10 +01007711 MBEDTLS_SSL_DEBUG_MSG(2, ("=> calc finished tls sha256"));
Jerry Yu615bd6f2022-02-17 14:25:15 +08007712
Manuel Pégourié-Gonnardf057ecf2023-02-24 13:19:17 +01007713 ret = mbedtls_md_setup(&sha256, mbedtls_md_info_from_type(MBEDTLS_MD_SHA256), 0);
7714 if (ret != 0) {
7715 goto exit;
7716 }
7717 ret = mbedtls_md_clone(&sha256, &ssl->handshake->fin_sha256);
7718 if (ret != 0) {
7719 goto exit;
7720 }
Jerry Yu615bd6f2022-02-17 14:25:15 +08007721
7722 /*
7723 * TLSv1.2:
7724 * hash = PRF( master, finished_label,
7725 * Hash( handshake ) )[0.11]
7726 */
7727
Manuel Pégourié-Gonnardf057ecf2023-02-24 13:19:17 +01007728 ret = mbedtls_md_finish(&sha256, padbuf);
Manuel Pégourié-Gonnarde1a4caa2023-02-06 10:14:25 +01007729 if (ret != 0) {
7730 goto exit;
7731 }
Jerry Yu615bd6f2022-02-17 14:25:15 +08007732#endif /* MBEDTLS_USE_PSA_CRYPTO */
7733
Manuel Pégourié-Gonnard0ac71c02023-02-24 12:13:55 +01007734 MBEDTLS_SSL_DEBUG_BUF(4, "finished sha256 output", padbuf, 32);
7735
Gilles Peskine449bd832023-01-11 14:50:10 +01007736 ssl->handshake->tls_prf(session->master, 48, sender,
7737 padbuf, 32, buf, len);
Jerry Yu615bd6f2022-02-17 14:25:15 +08007738
Gilles Peskine449bd832023-01-11 14:50:10 +01007739 MBEDTLS_SSL_DEBUG_BUF(3, "calc finished result", buf, len);
Jerry Yu615bd6f2022-02-17 14:25:15 +08007740
Gilles Peskine449bd832023-01-11 14:50:10 +01007741 mbedtls_platform_zeroize(padbuf, sizeof(padbuf));
Jerry Yu615bd6f2022-02-17 14:25:15 +08007742
Gilles Peskine449bd832023-01-11 14:50:10 +01007743 MBEDTLS_SSL_DEBUG_MSG(2, ("<= calc finished"));
Manuel Pégourié-Gonnarde1a4caa2023-02-06 10:14:25 +01007744
7745exit:
7746#if defined(MBEDTLS_USE_PSA_CRYPTO)
7747 psa_hash_abort(&sha256_psa);
7748 return mbedtls_md_error_from_psa(status);
7749#else
Manuel Pégourié-Gonnardf057ecf2023-02-24 13:19:17 +01007750 mbedtls_md_free(&sha256);
Manuel Pégourié-Gonnarde1a4caa2023-02-06 10:14:25 +01007751 return ret;
7752#endif /* MBEDTLS_USE_PSA_CRYPTO */
Jerry Yu615bd6f2022-02-17 14:25:15 +08007753}
Andrzej Kurekcccb0442022-08-19 03:42:11 -04007754#endif /* MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA*/
Jerry Yu615bd6f2022-02-17 14:25:15 +08007755
Jerry Yub7ba49e2022-02-17 14:25:53 +08007756
Andrzej Kurek25f27152022-08-17 16:09:31 -04007757#if defined(MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Manuel Pégourié-Gonnard226aa152023-02-05 09:46:59 +01007758static int ssl_calc_finished_tls_sha384(
Gilles Peskine449bd832023-01-11 14:50:10 +01007759 mbedtls_ssl_context *ssl, unsigned char *buf, int from)
Jerry Yub7ba49e2022-02-17 14:25:53 +08007760{
7761 int len = 12;
7762 const char *sender;
7763 unsigned char padbuf[48];
7764#if defined(MBEDTLS_USE_PSA_CRYPTO)
7765 size_t hash_size;
7766 psa_hash_operation_t sha384_psa = PSA_HASH_OPERATION_INIT;
7767 psa_status_t status;
7768#else
Manuel Pégourié-Gonnarde1a4caa2023-02-06 10:14:25 +01007769 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnard02d55d52023-02-24 13:21:16 +01007770 mbedtls_md_context_t sha384;
Jerry Yub7ba49e2022-02-17 14:25:53 +08007771#endif
7772
7773 mbedtls_ssl_session *session = ssl->session_negotiate;
Gilles Peskine449bd832023-01-11 14:50:10 +01007774 if (!session) {
Jerry Yub7ba49e2022-02-17 14:25:53 +08007775 session = ssl->session;
Gilles Peskine449bd832023-01-11 14:50:10 +01007776 }
Jerry Yub7ba49e2022-02-17 14:25:53 +08007777
Gilles Peskine449bd832023-01-11 14:50:10 +01007778 sender = (from == MBEDTLS_SSL_IS_CLIENT)
Jerry Yub7ba49e2022-02-17 14:25:53 +08007779 ? "client finished"
7780 : "server finished";
7781
7782#if defined(MBEDTLS_USE_PSA_CRYPTO)
7783 sha384_psa = psa_hash_operation_init();
7784
Gilles Peskine449bd832023-01-11 14:50:10 +01007785 MBEDTLS_SSL_DEBUG_MSG(2, ("=> calc PSA finished tls sha384"));
Jerry Yub7ba49e2022-02-17 14:25:53 +08007786
Gilles Peskine449bd832023-01-11 14:50:10 +01007787 status = psa_hash_clone(&ssl->handshake->fin_sha384_psa, &sha384_psa);
7788 if (status != PSA_SUCCESS) {
Manuel Pégourié-Gonnarde1a4caa2023-02-06 10:14:25 +01007789 goto exit;
Jerry Yub7ba49e2022-02-17 14:25:53 +08007790 }
7791
Gilles Peskine449bd832023-01-11 14:50:10 +01007792 status = psa_hash_finish(&sha384_psa, padbuf, sizeof(padbuf), &hash_size);
7793 if (status != PSA_SUCCESS) {
Manuel Pégourié-Gonnarde1a4caa2023-02-06 10:14:25 +01007794 goto exit;
Jerry Yub7ba49e2022-02-17 14:25:53 +08007795 }
Gilles Peskine449bd832023-01-11 14:50:10 +01007796 MBEDTLS_SSL_DEBUG_BUF(3, "PSA calculated padbuf", padbuf, 48);
Jerry Yub7ba49e2022-02-17 14:25:53 +08007797#else
Manuel Pégourié-Gonnard02d55d52023-02-24 13:21:16 +01007798 mbedtls_md_init(&sha384);
Jerry Yub7ba49e2022-02-17 14:25:53 +08007799
Gilles Peskine449bd832023-01-11 14:50:10 +01007800 MBEDTLS_SSL_DEBUG_MSG(2, ("=> calc finished tls sha384"));
Jerry Yub7ba49e2022-02-17 14:25:53 +08007801
Manuel Pégourié-Gonnard02d55d52023-02-24 13:21:16 +01007802 ret = mbedtls_md_setup(&sha384, mbedtls_md_info_from_type(MBEDTLS_MD_SHA384), 0);
Manuel Pégourié-Gonnardf057ecf2023-02-24 13:19:17 +01007803 if (ret != 0) {
7804 goto exit;
7805 }
Manuel Pégourié-Gonnard02d55d52023-02-24 13:21:16 +01007806 ret = mbedtls_md_clone(&sha384, &ssl->handshake->fin_sha384);
Manuel Pégourié-Gonnardf057ecf2023-02-24 13:19:17 +01007807 if (ret != 0) {
7808 goto exit;
7809 }
Jerry Yub7ba49e2022-02-17 14:25:53 +08007810
7811 /*
7812 * TLSv1.2:
7813 * hash = PRF( master, finished_label,
7814 * Hash( handshake ) )[0.11]
7815 */
7816
Manuel Pégourié-Gonnard02d55d52023-02-24 13:21:16 +01007817 ret = mbedtls_md_finish(&sha384, padbuf);
Manuel Pégourié-Gonnarde1a4caa2023-02-06 10:14:25 +01007818 if (ret != 0) {
7819 goto exit;
7820 }
Jerry Yub7ba49e2022-02-17 14:25:53 +08007821#endif
7822
Manuel Pégourié-Gonnard0ac71c02023-02-24 12:13:55 +01007823 MBEDTLS_SSL_DEBUG_BUF(4, "finished sha384 output", padbuf, 48);
7824
Gilles Peskine449bd832023-01-11 14:50:10 +01007825 ssl->handshake->tls_prf(session->master, 48, sender,
7826 padbuf, 48, buf, len);
Jerry Yub7ba49e2022-02-17 14:25:53 +08007827
Gilles Peskine449bd832023-01-11 14:50:10 +01007828 MBEDTLS_SSL_DEBUG_BUF(3, "calc finished result", buf, len);
Jerry Yub7ba49e2022-02-17 14:25:53 +08007829
Gilles Peskine449bd832023-01-11 14:50:10 +01007830 mbedtls_platform_zeroize(padbuf, sizeof(padbuf));
Jerry Yub7ba49e2022-02-17 14:25:53 +08007831
Gilles Peskine449bd832023-01-11 14:50:10 +01007832 MBEDTLS_SSL_DEBUG_MSG(2, ("<= calc finished"));
Manuel Pégourié-Gonnarde1a4caa2023-02-06 10:14:25 +01007833
7834exit:
7835#if defined(MBEDTLS_USE_PSA_CRYPTO)
7836 psa_hash_abort(&sha384_psa);
7837 return mbedtls_md_error_from_psa(status);
7838#else
Manuel Pégourié-Gonnard02d55d52023-02-24 13:21:16 +01007839 mbedtls_md_free(&sha384);
Manuel Pégourié-Gonnarde1a4caa2023-02-06 10:14:25 +01007840 return ret;
7841#endif /* MBEDTLS_USE_PSA_CRYPTO */
Jerry Yub7ba49e2022-02-17 14:25:53 +08007842}
Andrzej Kurekcccb0442022-08-19 03:42:11 -04007843#endif /* MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA*/
Jerry Yub7ba49e2022-02-17 14:25:53 +08007844
Gilles Peskine449bd832023-01-11 14:50:10 +01007845void mbedtls_ssl_handshake_wrapup_free_hs_transform(mbedtls_ssl_context *ssl)
Jerry Yuaef00152022-02-17 14:27:31 +08007846{
Gilles Peskine449bd832023-01-11 14:50:10 +01007847 MBEDTLS_SSL_DEBUG_MSG(3, ("=> handshake wrapup: final free"));
Jerry Yuaef00152022-02-17 14:27:31 +08007848
7849 /*
7850 * Free our handshake params
7851 */
Gilles Peskine449bd832023-01-11 14:50:10 +01007852 mbedtls_ssl_handshake_free(ssl);
7853 mbedtls_free(ssl->handshake);
Jerry Yuaef00152022-02-17 14:27:31 +08007854 ssl->handshake = NULL;
7855
7856 /*
Shaun Case8b0ecbc2021-12-20 21:14:10 -08007857 * Free the previous transform and switch in the current one
Jerry Yuaef00152022-02-17 14:27:31 +08007858 */
Gilles Peskine449bd832023-01-11 14:50:10 +01007859 if (ssl->transform) {
7860 mbedtls_ssl_transform_free(ssl->transform);
7861 mbedtls_free(ssl->transform);
Jerry Yuaef00152022-02-17 14:27:31 +08007862 }
7863 ssl->transform = ssl->transform_negotiate;
7864 ssl->transform_negotiate = NULL;
7865
Gilles Peskine449bd832023-01-11 14:50:10 +01007866 MBEDTLS_SSL_DEBUG_MSG(3, ("<= handshake wrapup: final free"));
Jerry Yuaef00152022-02-17 14:27:31 +08007867}
7868
Gilles Peskine449bd832023-01-11 14:50:10 +01007869void mbedtls_ssl_handshake_wrapup(mbedtls_ssl_context *ssl)
Jerry Yu2a9fff52022-02-17 14:28:51 +08007870{
7871 int resume = ssl->handshake->resume;
7872
Gilles Peskine449bd832023-01-11 14:50:10 +01007873 MBEDTLS_SSL_DEBUG_MSG(3, ("=> handshake wrapup"));
Jerry Yu2a9fff52022-02-17 14:28:51 +08007874
7875#if defined(MBEDTLS_SSL_RENEGOTIATION)
Gilles Peskine449bd832023-01-11 14:50:10 +01007876 if (ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS) {
Jerry Yu2a9fff52022-02-17 14:28:51 +08007877 ssl->renego_status = MBEDTLS_SSL_RENEGOTIATION_DONE;
7878 ssl->renego_records_seen = 0;
7879 }
7880#endif
7881
7882 /*
7883 * Free the previous session and switch in the current one
7884 */
Gilles Peskine449bd832023-01-11 14:50:10 +01007885 if (ssl->session) {
Jerry Yu2a9fff52022-02-17 14:28:51 +08007886#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
7887 /* RFC 7366 3.1: keep the EtM state */
7888 ssl->session_negotiate->encrypt_then_mac =
Gilles Peskine449bd832023-01-11 14:50:10 +01007889 ssl->session->encrypt_then_mac;
Jerry Yu2a9fff52022-02-17 14:28:51 +08007890#endif
7891
Gilles Peskine449bd832023-01-11 14:50:10 +01007892 mbedtls_ssl_session_free(ssl->session);
7893 mbedtls_free(ssl->session);
Jerry Yu2a9fff52022-02-17 14:28:51 +08007894 }
7895 ssl->session = ssl->session_negotiate;
7896 ssl->session_negotiate = NULL;
7897
7898 /*
7899 * Add cache entry
7900 */
Gilles Peskine449bd832023-01-11 14:50:10 +01007901 if (ssl->conf->f_set_cache != NULL &&
Jerry Yu2a9fff52022-02-17 14:28:51 +08007902 ssl->session->id_len != 0 &&
Gilles Peskine449bd832023-01-11 14:50:10 +01007903 resume == 0) {
7904 if (ssl->conf->f_set_cache(ssl->conf->p_cache,
7905 ssl->session->id,
7906 ssl->session->id_len,
7907 ssl->session) != 0) {
7908 MBEDTLS_SSL_DEBUG_MSG(1, ("cache did not store session"));
7909 }
Jerry Yu2a9fff52022-02-17 14:28:51 +08007910 }
7911
7912#if defined(MBEDTLS_SSL_PROTO_DTLS)
Gilles Peskine449bd832023-01-11 14:50:10 +01007913 if (ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
7914 ssl->handshake->flight != NULL) {
Jerry Yu2a9fff52022-02-17 14:28:51 +08007915 /* Cancel handshake timer */
Gilles Peskine449bd832023-01-11 14:50:10 +01007916 mbedtls_ssl_set_timer(ssl, 0);
Jerry Yu2a9fff52022-02-17 14:28:51 +08007917
7918 /* Keep last flight around in case we need to resend it:
7919 * we need the handshake and transform structures for that */
Gilles Peskine449bd832023-01-11 14:50:10 +01007920 MBEDTLS_SSL_DEBUG_MSG(3, ("skip freeing handshake and transform"));
7921 } else
Jerry Yu2a9fff52022-02-17 14:28:51 +08007922#endif
Gilles Peskine449bd832023-01-11 14:50:10 +01007923 mbedtls_ssl_handshake_wrapup_free_hs_transform(ssl);
Jerry Yu2a9fff52022-02-17 14:28:51 +08007924
Jerry Yu5ed73ff2022-10-27 13:08:42 +08007925 ssl->state = MBEDTLS_SSL_HANDSHAKE_OVER;
Jerry Yu2a9fff52022-02-17 14:28:51 +08007926
Gilles Peskine449bd832023-01-11 14:50:10 +01007927 MBEDTLS_SSL_DEBUG_MSG(3, ("<= handshake wrapup"));
Jerry Yu2a9fff52022-02-17 14:28:51 +08007928}
7929
Gilles Peskine449bd832023-01-11 14:50:10 +01007930int mbedtls_ssl_write_finished(mbedtls_ssl_context *ssl)
Jerry Yu3c8e47b2022-02-17 14:30:01 +08007931{
7932 int ret, hash_len;
7933
Gilles Peskine449bd832023-01-11 14:50:10 +01007934 MBEDTLS_SSL_DEBUG_MSG(2, ("=> write finished"));
Jerry Yu3c8e47b2022-02-17 14:30:01 +08007935
Gilles Peskine449bd832023-01-11 14:50:10 +01007936 mbedtls_ssl_update_out_pointers(ssl, ssl->transform_negotiate);
Jerry Yu3c8e47b2022-02-17 14:30:01 +08007937
Manuel Pégourié-Gonnardb8b07aa2023-02-06 00:34:21 +01007938 ret = ssl->handshake->calc_finished(ssl, ssl->out_msg + 4, ssl->conf->endpoint);
7939 if (ret != 0) {
7940 MBEDTLS_SSL_DEBUG_RET(1, "calc_finished", ret);
7941 }
Jerry Yu3c8e47b2022-02-17 14:30:01 +08007942
7943 /*
7944 * RFC 5246 7.4.9 (Page 63) says 12 is the default length and ciphersuites
7945 * may define some other value. Currently (early 2016), no defined
7946 * ciphersuite does this (and this is unlikely to change as activity has
7947 * moved to TLS 1.3 now) so we can keep the hardcoded 12 here.
7948 */
7949 hash_len = 12;
7950
7951#if defined(MBEDTLS_SSL_RENEGOTIATION)
7952 ssl->verify_data_len = hash_len;
Gilles Peskine449bd832023-01-11 14:50:10 +01007953 memcpy(ssl->own_verify_data, ssl->out_msg + 4, hash_len);
Jerry Yu3c8e47b2022-02-17 14:30:01 +08007954#endif
7955
7956 ssl->out_msglen = 4 + hash_len;
7957 ssl->out_msgtype = MBEDTLS_SSL_MSG_HANDSHAKE;
7958 ssl->out_msg[0] = MBEDTLS_SSL_HS_FINISHED;
7959
7960 /*
7961 * In case of session resuming, invert the client and server
7962 * ChangeCipherSpec messages order.
7963 */
Gilles Peskine449bd832023-01-11 14:50:10 +01007964 if (ssl->handshake->resume != 0) {
Jerry Yu3c8e47b2022-02-17 14:30:01 +08007965#if defined(MBEDTLS_SSL_CLI_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01007966 if (ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT) {
Jerry Yu3c8e47b2022-02-17 14:30:01 +08007967 ssl->state = MBEDTLS_SSL_HANDSHAKE_WRAPUP;
Gilles Peskine449bd832023-01-11 14:50:10 +01007968 }
Jerry Yu3c8e47b2022-02-17 14:30:01 +08007969#endif
7970#if defined(MBEDTLS_SSL_SRV_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01007971 if (ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER) {
Jerry Yu3c8e47b2022-02-17 14:30:01 +08007972 ssl->state = MBEDTLS_SSL_CLIENT_CHANGE_CIPHER_SPEC;
Gilles Peskine449bd832023-01-11 14:50:10 +01007973 }
Jerry Yu3c8e47b2022-02-17 14:30:01 +08007974#endif
Gilles Peskine449bd832023-01-11 14:50:10 +01007975 } else {
Jerry Yu3c8e47b2022-02-17 14:30:01 +08007976 ssl->state++;
Gilles Peskine449bd832023-01-11 14:50:10 +01007977 }
Jerry Yu3c8e47b2022-02-17 14:30:01 +08007978
7979 /*
7980 * Switch to our negotiated transform and session parameters for outbound
7981 * data.
7982 */
Gilles Peskine449bd832023-01-11 14:50:10 +01007983 MBEDTLS_SSL_DEBUG_MSG(3, ("switching to new transform spec for outbound data"));
Jerry Yu3c8e47b2022-02-17 14:30:01 +08007984
7985#if defined(MBEDTLS_SSL_PROTO_DTLS)
Gilles Peskine449bd832023-01-11 14:50:10 +01007986 if (ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM) {
Jerry Yu3c8e47b2022-02-17 14:30:01 +08007987 unsigned char i;
7988
7989 /* Remember current epoch settings for resending */
7990 ssl->handshake->alt_transform_out = ssl->transform_out;
Gilles Peskine449bd832023-01-11 14:50:10 +01007991 memcpy(ssl->handshake->alt_out_ctr, ssl->cur_out_ctr,
7992 sizeof(ssl->handshake->alt_out_ctr));
Jerry Yu3c8e47b2022-02-17 14:30:01 +08007993
7994 /* Set sequence_number to zero */
Gilles Peskine449bd832023-01-11 14:50:10 +01007995 memset(&ssl->cur_out_ctr[2], 0, sizeof(ssl->cur_out_ctr) - 2);
Jerry Yu3c8e47b2022-02-17 14:30:01 +08007996
7997
7998 /* Increment epoch */
Gilles Peskine449bd832023-01-11 14:50:10 +01007999 for (i = 2; i > 0; i--) {
8000 if (++ssl->cur_out_ctr[i - 1] != 0) {
Jerry Yu3c8e47b2022-02-17 14:30:01 +08008001 break;
Gilles Peskine449bd832023-01-11 14:50:10 +01008002 }
8003 }
Jerry Yu3c8e47b2022-02-17 14:30:01 +08008004
8005 /* The loop goes to its end iff the counter is wrapping */
Gilles Peskine449bd832023-01-11 14:50:10 +01008006 if (i == 0) {
8007 MBEDTLS_SSL_DEBUG_MSG(1, ("DTLS epoch would wrap"));
8008 return MBEDTLS_ERR_SSL_COUNTER_WRAPPING;
Jerry Yu3c8e47b2022-02-17 14:30:01 +08008009 }
Gilles Peskine449bd832023-01-11 14:50:10 +01008010 } else
Jerry Yu3c8e47b2022-02-17 14:30:01 +08008011#endif /* MBEDTLS_SSL_PROTO_DTLS */
Gilles Peskine449bd832023-01-11 14:50:10 +01008012 memset(ssl->cur_out_ctr, 0, sizeof(ssl->cur_out_ctr));
Jerry Yu3c8e47b2022-02-17 14:30:01 +08008013
8014 ssl->transform_out = ssl->transform_negotiate;
8015 ssl->session_out = ssl->session_negotiate;
8016
8017#if defined(MBEDTLS_SSL_PROTO_DTLS)
Gilles Peskine449bd832023-01-11 14:50:10 +01008018 if (ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM) {
8019 mbedtls_ssl_send_flight_completed(ssl);
8020 }
Jerry Yu3c8e47b2022-02-17 14:30:01 +08008021#endif
8022
Gilles Peskine449bd832023-01-11 14:50:10 +01008023 if ((ret = mbedtls_ssl_write_handshake_msg(ssl)) != 0) {
8024 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ssl_write_handshake_msg", ret);
8025 return ret;
Jerry Yu3c8e47b2022-02-17 14:30:01 +08008026 }
8027
8028#if defined(MBEDTLS_SSL_PROTO_DTLS)
Gilles Peskine449bd832023-01-11 14:50:10 +01008029 if (ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
8030 (ret = mbedtls_ssl_flight_transmit(ssl)) != 0) {
8031 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ssl_flight_transmit", ret);
8032 return ret;
Jerry Yu3c8e47b2022-02-17 14:30:01 +08008033 }
8034#endif
8035
Gilles Peskine449bd832023-01-11 14:50:10 +01008036 MBEDTLS_SSL_DEBUG_MSG(2, ("<= write finished"));
Jerry Yu3c8e47b2022-02-17 14:30:01 +08008037
Gilles Peskine449bd832023-01-11 14:50:10 +01008038 return 0;
Jerry Yu3c8e47b2022-02-17 14:30:01 +08008039}
8040
Jerry Yu0b3d7c12022-02-17 14:30:51 +08008041#define SSL_MAX_HASH_LEN 12
8042
Gilles Peskine449bd832023-01-11 14:50:10 +01008043int mbedtls_ssl_parse_finished(mbedtls_ssl_context *ssl)
Jerry Yu0b3d7c12022-02-17 14:30:51 +08008044{
8045 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
8046 unsigned int hash_len = 12;
8047 unsigned char buf[SSL_MAX_HASH_LEN];
8048
Gilles Peskine449bd832023-01-11 14:50:10 +01008049 MBEDTLS_SSL_DEBUG_MSG(2, ("=> parse finished"));
Jerry Yu0b3d7c12022-02-17 14:30:51 +08008050
Manuel Pégourié-Gonnardb8b07aa2023-02-06 00:34:21 +01008051 ret = ssl->handshake->calc_finished(ssl, buf, ssl->conf->endpoint ^ 1);
8052 if (ret != 0) {
8053 MBEDTLS_SSL_DEBUG_RET(1, "calc_finished", ret);
8054 }
Jerry Yu0b3d7c12022-02-17 14:30:51 +08008055
Gilles Peskine449bd832023-01-11 14:50:10 +01008056 if ((ret = mbedtls_ssl_read_record(ssl, 1)) != 0) {
8057 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ssl_read_record", ret);
Jerry Yu0b3d7c12022-02-17 14:30:51 +08008058 goto exit;
8059 }
8060
Gilles Peskine449bd832023-01-11 14:50:10 +01008061 if (ssl->in_msgtype != MBEDTLS_SSL_MSG_HANDSHAKE) {
8062 MBEDTLS_SSL_DEBUG_MSG(1, ("bad finished message"));
8063 mbedtls_ssl_send_alert_message(ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
8064 MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE);
Jerry Yu0b3d7c12022-02-17 14:30:51 +08008065 ret = MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE;
8066 goto exit;
8067 }
8068
Gilles Peskine449bd832023-01-11 14:50:10 +01008069 if (ssl->in_msg[0] != MBEDTLS_SSL_HS_FINISHED) {
8070 mbedtls_ssl_send_alert_message(ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
8071 MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE);
Jerry Yu0b3d7c12022-02-17 14:30:51 +08008072 ret = MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE;
8073 goto exit;
8074 }
8075
Gilles Peskine449bd832023-01-11 14:50:10 +01008076 if (ssl->in_hslen != mbedtls_ssl_hs_hdr_len(ssl) + hash_len) {
8077 MBEDTLS_SSL_DEBUG_MSG(1, ("bad finished message"));
8078 mbedtls_ssl_send_alert_message(ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
8079 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR);
Jerry Yu0b3d7c12022-02-17 14:30:51 +08008080 ret = MBEDTLS_ERR_SSL_DECODE_ERROR;
8081 goto exit;
8082 }
8083
Gilles Peskine449bd832023-01-11 14:50:10 +01008084 if (mbedtls_ct_memcmp(ssl->in_msg + mbedtls_ssl_hs_hdr_len(ssl),
8085 buf, hash_len) != 0) {
8086 MBEDTLS_SSL_DEBUG_MSG(1, ("bad finished message"));
8087 mbedtls_ssl_send_alert_message(ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
8088 MBEDTLS_SSL_ALERT_MSG_DECRYPT_ERROR);
Jerry Yu0b3d7c12022-02-17 14:30:51 +08008089 ret = MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE;
8090 goto exit;
8091 }
8092
8093#if defined(MBEDTLS_SSL_RENEGOTIATION)
8094 ssl->verify_data_len = hash_len;
Gilles Peskine449bd832023-01-11 14:50:10 +01008095 memcpy(ssl->peer_verify_data, buf, hash_len);
Jerry Yu0b3d7c12022-02-17 14:30:51 +08008096#endif
8097
Gilles Peskine449bd832023-01-11 14:50:10 +01008098 if (ssl->handshake->resume != 0) {
Jerry Yu0b3d7c12022-02-17 14:30:51 +08008099#if defined(MBEDTLS_SSL_CLI_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01008100 if (ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT) {
Jerry Yu0b3d7c12022-02-17 14:30:51 +08008101 ssl->state = MBEDTLS_SSL_CLIENT_CHANGE_CIPHER_SPEC;
Gilles Peskine449bd832023-01-11 14:50:10 +01008102 }
Jerry Yu0b3d7c12022-02-17 14:30:51 +08008103#endif
8104#if defined(MBEDTLS_SSL_SRV_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01008105 if (ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER) {
Jerry Yu0b3d7c12022-02-17 14:30:51 +08008106 ssl->state = MBEDTLS_SSL_HANDSHAKE_WRAPUP;
Gilles Peskine449bd832023-01-11 14:50:10 +01008107 }
Jerry Yu0b3d7c12022-02-17 14:30:51 +08008108#endif
Gilles Peskine449bd832023-01-11 14:50:10 +01008109 } else {
Jerry Yu0b3d7c12022-02-17 14:30:51 +08008110 ssl->state++;
Gilles Peskine449bd832023-01-11 14:50:10 +01008111 }
Jerry Yu0b3d7c12022-02-17 14:30:51 +08008112
8113#if defined(MBEDTLS_SSL_PROTO_DTLS)
Gilles Peskine449bd832023-01-11 14:50:10 +01008114 if (ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM) {
8115 mbedtls_ssl_recv_flight_completed(ssl);
8116 }
Jerry Yu0b3d7c12022-02-17 14:30:51 +08008117#endif
8118
Gilles Peskine449bd832023-01-11 14:50:10 +01008119 MBEDTLS_SSL_DEBUG_MSG(2, ("<= parse finished"));
Jerry Yu0b3d7c12022-02-17 14:30:51 +08008120
8121exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01008122 mbedtls_platform_zeroize(buf, hash_len);
8123 return ret;
Jerry Yu0b3d7c12022-02-17 14:30:51 +08008124}
8125
Jerry Yu392112c2022-02-17 14:34:10 +08008126#if defined(MBEDTLS_SSL_CONTEXT_SERIALIZATION)
8127/*
8128 * Helper to get TLS 1.2 PRF from ciphersuite
8129 * (Duplicates bits of logic from ssl_set_handshake_prfs().)
8130 */
Gilles Peskine449bd832023-01-11 14:50:10 +01008131static tls_prf_fn ssl_tls12prf_from_cs(int ciphersuite_id)
Jerry Yu392112c2022-02-17 14:34:10 +08008132{
Jerry Yu392112c2022-02-17 14:34:10 +08008133 const mbedtls_ssl_ciphersuite_t * const ciphersuite_info =
Gilles Peskine449bd832023-01-11 14:50:10 +01008134 mbedtls_ssl_ciphersuite_from_id(ciphersuite_id);
Andrzej Kurek68327742022-10-03 06:18:18 -04008135#if defined(MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Gilles Peskine449bd832023-01-11 14:50:10 +01008136 if (ciphersuite_info != NULL && ciphersuite_info->mac == MBEDTLS_MD_SHA384) {
8137 return tls_prf_sha384;
8138 } else
Jerry Yu392112c2022-02-17 14:34:10 +08008139#endif
Andrzej Kurek894edde2022-09-29 06:31:14 -04008140#if defined(MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
8141 {
Gilles Peskine449bd832023-01-11 14:50:10 +01008142 if (ciphersuite_info != NULL && ciphersuite_info->mac == MBEDTLS_MD_SHA256) {
8143 return tls_prf_sha256;
8144 }
Andrzej Kurek894edde2022-09-29 06:31:14 -04008145 }
8146#endif
8147#if !defined(MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA) && \
8148 !defined(MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
8149 (void) ciphersuite_info;
8150#endif
Andrzej Kurekeabeb302022-10-17 07:52:51 -04008151
Gilles Peskine449bd832023-01-11 14:50:10 +01008152 return NULL;
Jerry Yu392112c2022-02-17 14:34:10 +08008153}
8154#endif /* MBEDTLS_SSL_CONTEXT_SERIALIZATION */
Jerry Yue93ffcd2022-02-17 14:37:06 +08008155
Gilles Peskine449bd832023-01-11 14:50:10 +01008156static mbedtls_tls_prf_types tls_prf_get_type(mbedtls_ssl_tls_prf_cb *tls_prf)
Jerry Yue93ffcd2022-02-17 14:37:06 +08008157{
8158 ((void) tls_prf);
Andrzej Kurek25f27152022-08-17 16:09:31 -04008159#if defined(MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Gilles Peskine449bd832023-01-11 14:50:10 +01008160 if (tls_prf == tls_prf_sha384) {
8161 return MBEDTLS_SSL_TLS_PRF_SHA384;
8162 } else
Jerry Yue93ffcd2022-02-17 14:37:06 +08008163#endif
Andrzej Kurek25f27152022-08-17 16:09:31 -04008164#if defined(MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Gilles Peskine449bd832023-01-11 14:50:10 +01008165 if (tls_prf == tls_prf_sha256) {
8166 return MBEDTLS_SSL_TLS_PRF_SHA256;
8167 } else
Jerry Yue93ffcd2022-02-17 14:37:06 +08008168#endif
Gilles Peskine449bd832023-01-11 14:50:10 +01008169 return MBEDTLS_SSL_TLS_PRF_NONE;
Jerry Yue93ffcd2022-02-17 14:37:06 +08008170}
8171
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008172/*
8173 * Populate a transform structure with session keys and all the other
8174 * necessary information.
8175 *
8176 * Parameters:
8177 * - [in/out]: transform: structure to populate
8178 * [in] must be just initialised with mbedtls_ssl_transform_init()
8179 * [out] fully populated, ready for use by mbedtls_ssl_{en,de}crypt_buf()
8180 * - [in] ciphersuite
8181 * - [in] master
8182 * - [in] encrypt_then_mac
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008183 * - [in] tls_prf: pointer to PRF to use for key derivation
8184 * - [in] randbytes: buffer holding ServerHello.random + ClientHello.random
Glenn Strauss07c64162022-03-14 12:34:51 -04008185 * - [in] tls_version: TLS version
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008186 * - [in] endpoint: client or server
8187 * - [in] ssl: used for:
8188 * - ssl->conf->{f,p}_export_keys
8189 * [in] optionally used for:
8190 * - MBEDTLS_DEBUG_C: ssl->conf->{f,p}_dbg
8191 */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02008192MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01008193static int ssl_tls12_populate_transform(mbedtls_ssl_transform *transform,
8194 int ciphersuite,
8195 const unsigned char master[48],
Neil Armstrongf2c82f02022-04-05 11:16:53 +02008196#if defined(MBEDTLS_SSL_SOME_SUITES_USE_CBC_ETM)
Gilles Peskine449bd832023-01-11 14:50:10 +01008197 int encrypt_then_mac,
Neil Armstrongf2c82f02022-04-05 11:16:53 +02008198#endif /* MBEDTLS_SSL_SOME_SUITES_USE_CBC_ETM */
Gilles Peskine449bd832023-01-11 14:50:10 +01008199 ssl_tls_prf_t tls_prf,
8200 const unsigned char randbytes[64],
8201 mbedtls_ssl_protocol_version tls_version,
8202 unsigned endpoint,
8203 const mbedtls_ssl_context *ssl)
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008204{
8205 int ret = 0;
8206 unsigned char keyblk[256];
8207 unsigned char *key1;
8208 unsigned char *key2;
8209 unsigned char *mac_enc;
8210 unsigned char *mac_dec;
8211 size_t mac_key_len = 0;
8212 size_t iv_copy_len;
8213 size_t keylen;
8214 const mbedtls_ssl_ciphersuite_t *ciphersuite_info;
Neil Armstrong7fea33e2022-04-01 15:40:25 +02008215 mbedtls_ssl_mode_t ssl_mode;
Neil Armstronge4512952022-03-08 09:08:22 +01008216#if !defined(MBEDTLS_USE_PSA_CRYPTO)
Neil Armstronga0eeb7f2022-04-01 17:36:10 +02008217 const mbedtls_cipher_info_t *cipher_info;
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008218 const mbedtls_md_info_t *md_info;
Neil Armstronge4512952022-03-08 09:08:22 +01008219#endif /* !MBEDTLS_USE_PSA_CRYPTO */
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008220
8221#if defined(MBEDTLS_USE_PSA_CRYPTO)
8222 psa_key_type_t key_type;
8223 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
8224 psa_algorithm_t alg;
Neil Armstronge4512952022-03-08 09:08:22 +01008225 psa_algorithm_t mac_alg = 0;
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008226 size_t key_bits;
8227 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
8228#endif
8229
8230#if !defined(MBEDTLS_DEBUG_C) && \
8231 !defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Gilles Peskine449bd832023-01-11 14:50:10 +01008232 if (ssl->f_export_keys == NULL) {
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008233 ssl = NULL; /* make sure we don't use it except for these cases */
8234 (void) ssl;
8235 }
8236#endif
8237
8238 /*
8239 * Some data just needs copying into the structure
8240 */
Neil Armstrongf2c82f02022-04-05 11:16:53 +02008241#if defined(MBEDTLS_SSL_SOME_SUITES_USE_CBC_ETM)
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008242 transform->encrypt_then_mac = encrypt_then_mac;
Neil Armstrongf2c82f02022-04-05 11:16:53 +02008243#endif /* MBEDTLS_SSL_SOME_SUITES_USE_CBC_ETM */
Glenn Strauss07c64162022-03-14 12:34:51 -04008244 transform->tls_version = tls_version;
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008245
8246#if defined(MBEDTLS_SSL_CONTEXT_SERIALIZATION)
Gilles Peskine449bd832023-01-11 14:50:10 +01008247 memcpy(transform->randbytes, randbytes, sizeof(transform->randbytes));
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008248#endif
8249
8250#if defined(MBEDTLS_SSL_PROTO_TLS1_3)
Gilles Peskine449bd832023-01-11 14:50:10 +01008251 if (tls_version == MBEDTLS_SSL_VERSION_TLS1_3) {
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008252 /* At the moment, we keep TLS <= 1.2 and TLS 1.3 transform
8253 * generation separate. This should never happen. */
Gilles Peskine449bd832023-01-11 14:50:10 +01008254 return MBEDTLS_ERR_SSL_INTERNAL_ERROR;
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008255 }
8256#endif /* MBEDTLS_SSL_PROTO_TLS1_3 */
8257
8258 /*
8259 * Get various info structures
8260 */
Gilles Peskine449bd832023-01-11 14:50:10 +01008261 ciphersuite_info = mbedtls_ssl_ciphersuite_from_id(ciphersuite);
8262 if (ciphersuite_info == NULL) {
8263 MBEDTLS_SSL_DEBUG_MSG(1, ("ciphersuite info for %d not found",
8264 ciphersuite));
8265 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008266 }
8267
Neil Armstrongab555e02022-04-04 11:07:59 +02008268 ssl_mode = mbedtls_ssl_get_mode_from_ciphersuite(
Neil Armstrongf2c82f02022-04-05 11:16:53 +02008269#if defined(MBEDTLS_SSL_SOME_SUITES_USE_CBC_ETM)
Gilles Peskine449bd832023-01-11 14:50:10 +01008270 encrypt_then_mac,
Neil Armstrongf2c82f02022-04-05 11:16:53 +02008271#endif /* MBEDTLS_SSL_SOME_SUITES_USE_CBC_ETM */
Gilles Peskine449bd832023-01-11 14:50:10 +01008272 ciphersuite_info);
Neil Armstrong7fea33e2022-04-01 15:40:25 +02008273
Gilles Peskine449bd832023-01-11 14:50:10 +01008274 if (ssl_mode == MBEDTLS_SSL_MODE_AEAD) {
Neil Armstronga0eeb7f2022-04-01 17:36:10 +02008275 transform->taglen =
8276 ciphersuite_info->flags & MBEDTLS_CIPHERSUITE_SHORT_TAG ? 8 : 16;
Gilles Peskine449bd832023-01-11 14:50:10 +01008277 }
Neil Armstronga0eeb7f2022-04-01 17:36:10 +02008278
8279#if defined(MBEDTLS_USE_PSA_CRYPTO)
Gilles Peskine449bd832023-01-11 14:50:10 +01008280 if ((status = mbedtls_ssl_cipher_to_psa(ciphersuite_info->cipher,
8281 transform->taglen,
8282 &alg,
8283 &key_type,
8284 &key_bits)) != PSA_SUCCESS) {
8285 ret = psa_ssl_status_to_mbedtls(status);
8286 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ssl_cipher_to_psa", ret);
Neil Armstronga0eeb7f2022-04-01 17:36:10 +02008287 goto end;
8288 }
8289#else
Gilles Peskine449bd832023-01-11 14:50:10 +01008290 cipher_info = mbedtls_cipher_info_from_type(ciphersuite_info->cipher);
8291 if (cipher_info == NULL) {
8292 MBEDTLS_SSL_DEBUG_MSG(1, ("cipher info for %u not found",
8293 ciphersuite_info->cipher));
8294 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008295 }
Neil Armstronga0eeb7f2022-04-01 17:36:10 +02008296#endif /* MBEDTLS_USE_PSA_CRYPTO */
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008297
Neil Armstronge4512952022-03-08 09:08:22 +01008298#if defined(MBEDTLS_USE_PSA_CRYPTO)
Gilles Peskine449bd832023-01-11 14:50:10 +01008299 mac_alg = mbedtls_hash_info_psa_from_md(ciphersuite_info->mac);
8300 if (mac_alg == 0) {
8301 MBEDTLS_SSL_DEBUG_MSG(1, ("mbedtls_hash_info_psa_from_md for %u not found",
8302 (unsigned) ciphersuite_info->mac));
8303 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
Neil Armstronge4512952022-03-08 09:08:22 +01008304 }
8305#else
Gilles Peskine449bd832023-01-11 14:50:10 +01008306 md_info = mbedtls_md_info_from_type(ciphersuite_info->mac);
8307 if (md_info == NULL) {
8308 MBEDTLS_SSL_DEBUG_MSG(1, ("mbedtls_md info for %u not found",
8309 (unsigned) ciphersuite_info->mac));
8310 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008311 }
Neil Armstronge4512952022-03-08 09:08:22 +01008312#endif /* MBEDTLS_USE_PSA_CRYPTO */
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008313
8314#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
8315 /* Copy own and peer's CID if the use of the CID
8316 * extension has been negotiated. */
Gilles Peskine449bd832023-01-11 14:50:10 +01008317 if (ssl->handshake->cid_in_use == MBEDTLS_SSL_CID_ENABLED) {
8318 MBEDTLS_SSL_DEBUG_MSG(3, ("Copy CIDs into SSL transform"));
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008319
8320 transform->in_cid_len = ssl->own_cid_len;
Gilles Peskine449bd832023-01-11 14:50:10 +01008321 memcpy(transform->in_cid, ssl->own_cid, ssl->own_cid_len);
8322 MBEDTLS_SSL_DEBUG_BUF(3, "Incoming CID", transform->in_cid,
8323 transform->in_cid_len);
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008324
8325 transform->out_cid_len = ssl->handshake->peer_cid_len;
Gilles Peskine449bd832023-01-11 14:50:10 +01008326 memcpy(transform->out_cid, ssl->handshake->peer_cid,
8327 ssl->handshake->peer_cid_len);
8328 MBEDTLS_SSL_DEBUG_BUF(3, "Outgoing CID", transform->out_cid,
8329 transform->out_cid_len);
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008330 }
8331#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
8332
8333 /*
8334 * Compute key block using the PRF
8335 */
Gilles Peskine449bd832023-01-11 14:50:10 +01008336 ret = tls_prf(master, 48, "key expansion", randbytes, 64, keyblk, 256);
8337 if (ret != 0) {
8338 MBEDTLS_SSL_DEBUG_RET(1, "prf", ret);
8339 return ret;
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008340 }
8341
Gilles Peskine449bd832023-01-11 14:50:10 +01008342 MBEDTLS_SSL_DEBUG_MSG(3, ("ciphersuite = %s",
8343 mbedtls_ssl_get_ciphersuite_name(ciphersuite)));
8344 MBEDTLS_SSL_DEBUG_BUF(3, "master secret", master, 48);
8345 MBEDTLS_SSL_DEBUG_BUF(4, "random bytes", randbytes, 64);
8346 MBEDTLS_SSL_DEBUG_BUF(4, "key block", keyblk, 256);
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008347
8348 /*
8349 * Determine the appropriate key, IV and MAC length.
8350 */
8351
Neil Armstronga0eeb7f2022-04-01 17:36:10 +02008352#if defined(MBEDTLS_USE_PSA_CRYPTO)
8353 keylen = PSA_BITS_TO_BYTES(key_bits);
8354#else
Gilles Peskine449bd832023-01-11 14:50:10 +01008355 keylen = mbedtls_cipher_info_get_key_bitlen(cipher_info) / 8;
Neil Armstronga0eeb7f2022-04-01 17:36:10 +02008356#endif
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008357
8358#if defined(MBEDTLS_GCM_C) || \
8359 defined(MBEDTLS_CCM_C) || \
8360 defined(MBEDTLS_CHACHAPOLY_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01008361 if (ssl_mode == MBEDTLS_SSL_MODE_AEAD) {
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008362 size_t explicit_ivlen;
8363
8364 transform->maclen = 0;
8365 mac_key_len = 0;
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008366
8367 /* All modes haves 96-bit IVs, but the length of the static parts vary
8368 * with mode and version:
8369 * - For GCM and CCM in TLS 1.2, there's a static IV of 4 Bytes
8370 * (to be concatenated with a dynamically chosen IV of 8 Bytes)
8371 * - For ChaChaPoly in TLS 1.2, and all modes in TLS 1.3, there's
8372 * a static IV of 12 Bytes (to be XOR'ed with the 8 Byte record
8373 * sequence number).
8374 */
8375 transform->ivlen = 12;
David Horstmann3b2276a2022-10-06 14:49:08 +01008376
8377 int is_chachapoly = 0;
Neil Armstronga0eeb7f2022-04-01 17:36:10 +02008378#if defined(MBEDTLS_USE_PSA_CRYPTO)
Gilles Peskine449bd832023-01-11 14:50:10 +01008379 is_chachapoly = (key_type == PSA_KEY_TYPE_CHACHA20);
Neil Armstronga0eeb7f2022-04-01 17:36:10 +02008380#else
Gilles Peskine449bd832023-01-11 14:50:10 +01008381 is_chachapoly = (mbedtls_cipher_info_get_mode(cipher_info)
8382 == MBEDTLS_MODE_CHACHAPOLY);
Neil Armstronga0eeb7f2022-04-01 17:36:10 +02008383#endif /* MBEDTLS_USE_PSA_CRYPTO */
David Horstmann3b2276a2022-10-06 14:49:08 +01008384
Gilles Peskine449bd832023-01-11 14:50:10 +01008385 if (is_chachapoly) {
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008386 transform->fixed_ivlen = 12;
Gilles Peskine449bd832023-01-11 14:50:10 +01008387 } else {
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008388 transform->fixed_ivlen = 4;
Gilles Peskine449bd832023-01-11 14:50:10 +01008389 }
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008390
8391 /* Minimum length of encrypted record */
8392 explicit_ivlen = transform->ivlen - transform->fixed_ivlen;
8393 transform->minlen = explicit_ivlen + transform->taglen;
Gilles Peskine449bd832023-01-11 14:50:10 +01008394 } else
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008395#endif /* MBEDTLS_GCM_C || MBEDTLS_CCM_C || MBEDTLS_CHACHAPOLY_C */
8396#if defined(MBEDTLS_SSL_SOME_SUITES_USE_MAC)
Gilles Peskine449bd832023-01-11 14:50:10 +01008397 if (ssl_mode == MBEDTLS_SSL_MODE_STREAM ||
Neil Armstrong7fea33e2022-04-01 15:40:25 +02008398 ssl_mode == MBEDTLS_SSL_MODE_CBC ||
Gilles Peskine449bd832023-01-11 14:50:10 +01008399 ssl_mode == MBEDTLS_SSL_MODE_CBC_ETM) {
Neil Armstronge4512952022-03-08 09:08:22 +01008400#if defined(MBEDTLS_USE_PSA_CRYPTO)
Gilles Peskine449bd832023-01-11 14:50:10 +01008401 size_t block_size = PSA_BLOCK_CIPHER_BLOCK_LENGTH(key_type);
Neil Armstronga0eeb7f2022-04-01 17:36:10 +02008402#else
8403 size_t block_size = cipher_info->block_size;
8404#endif /* MBEDTLS_USE_PSA_CRYPTO */
8405
8406#if defined(MBEDTLS_USE_PSA_CRYPTO)
Neil Armstronge4512952022-03-08 09:08:22 +01008407 /* Get MAC length */
8408 mac_key_len = PSA_HASH_LENGTH(mac_alg);
8409#else
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008410 /* Initialize HMAC contexts */
Gilles Peskine449bd832023-01-11 14:50:10 +01008411 if ((ret = mbedtls_md_setup(&transform->md_ctx_enc, md_info, 1)) != 0 ||
8412 (ret = mbedtls_md_setup(&transform->md_ctx_dec, md_info, 1)) != 0) {
8413 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_md_setup", ret);
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008414 goto end;
8415 }
8416
8417 /* Get MAC length */
Gilles Peskine449bd832023-01-11 14:50:10 +01008418 mac_key_len = mbedtls_md_get_size(md_info);
Neil Armstronge4512952022-03-08 09:08:22 +01008419#endif /* MBEDTLS_USE_PSA_CRYPTO */
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008420 transform->maclen = mac_key_len;
8421
8422 /* IV length */
Neil Armstronga0eeb7f2022-04-01 17:36:10 +02008423#if defined(MBEDTLS_USE_PSA_CRYPTO)
Gilles Peskine449bd832023-01-11 14:50:10 +01008424 transform->ivlen = PSA_CIPHER_IV_LENGTH(key_type, alg);
Neil Armstronga0eeb7f2022-04-01 17:36:10 +02008425#else
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008426 transform->ivlen = cipher_info->iv_size;
Neil Armstronga0eeb7f2022-04-01 17:36:10 +02008427#endif /* MBEDTLS_USE_PSA_CRYPTO */
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008428
8429 /* Minimum length */
Gilles Peskine449bd832023-01-11 14:50:10 +01008430 if (ssl_mode == MBEDTLS_SSL_MODE_STREAM) {
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008431 transform->minlen = transform->maclen;
Gilles Peskine449bd832023-01-11 14:50:10 +01008432 } else {
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008433 /*
8434 * GenericBlockCipher:
8435 * 1. if EtM is in use: one block plus MAC
8436 * otherwise: * first multiple of blocklen greater than maclen
8437 * 2. IV
8438 */
8439#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
Gilles Peskine449bd832023-01-11 14:50:10 +01008440 if (ssl_mode == MBEDTLS_SSL_MODE_CBC_ETM) {
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008441 transform->minlen = transform->maclen
Gilles Peskine449bd832023-01-11 14:50:10 +01008442 + block_size;
8443 } else
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008444#endif
8445 {
8446 transform->minlen = transform->maclen
Gilles Peskine449bd832023-01-11 14:50:10 +01008447 + block_size
8448 - transform->maclen % block_size;
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008449 }
8450
Gilles Peskine449bd832023-01-11 14:50:10 +01008451 if (tls_version == MBEDTLS_SSL_VERSION_TLS1_2) {
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008452 transform->minlen += transform->ivlen;
Gilles Peskine449bd832023-01-11 14:50:10 +01008453 } else {
8454 MBEDTLS_SSL_DEBUG_MSG(1, ("should never happen"));
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008455 ret = MBEDTLS_ERR_SSL_INTERNAL_ERROR;
8456 goto end;
8457 }
8458 }
Gilles Peskine449bd832023-01-11 14:50:10 +01008459 } else
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008460#endif /* MBEDTLS_SSL_SOME_SUITES_USE_MAC */
8461 {
Gilles Peskine449bd832023-01-11 14:50:10 +01008462 MBEDTLS_SSL_DEBUG_MSG(1, ("should never happen"));
8463 return MBEDTLS_ERR_SSL_INTERNAL_ERROR;
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008464 }
8465
Gilles Peskine449bd832023-01-11 14:50:10 +01008466 MBEDTLS_SSL_DEBUG_MSG(3, ("keylen: %u, minlen: %u, ivlen: %u, maclen: %u",
8467 (unsigned) keylen,
8468 (unsigned) transform->minlen,
8469 (unsigned) transform->ivlen,
8470 (unsigned) transform->maclen));
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008471
8472 /*
8473 * Finally setup the cipher contexts, IVs and MAC secrets.
8474 */
8475#if defined(MBEDTLS_SSL_CLI_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01008476 if (endpoint == MBEDTLS_SSL_IS_CLIENT) {
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008477 key1 = keyblk + mac_key_len * 2;
8478 key2 = keyblk + mac_key_len * 2 + keylen;
8479
8480 mac_enc = keyblk;
8481 mac_dec = keyblk + mac_key_len;
8482
Gilles Peskine449bd832023-01-11 14:50:10 +01008483 iv_copy_len = (transform->fixed_ivlen) ?
8484 transform->fixed_ivlen : transform->ivlen;
8485 memcpy(transform->iv_enc, key2 + keylen, iv_copy_len);
8486 memcpy(transform->iv_dec, key2 + keylen + iv_copy_len,
8487 iv_copy_len);
8488 } else
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008489#endif /* MBEDTLS_SSL_CLI_C */
8490#if defined(MBEDTLS_SSL_SRV_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01008491 if (endpoint == MBEDTLS_SSL_IS_SERVER) {
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008492 key1 = keyblk + mac_key_len * 2 + keylen;
8493 key2 = keyblk + mac_key_len * 2;
8494
8495 mac_enc = keyblk + mac_key_len;
8496 mac_dec = keyblk;
8497
Gilles Peskine449bd832023-01-11 14:50:10 +01008498 iv_copy_len = (transform->fixed_ivlen) ?
8499 transform->fixed_ivlen : transform->ivlen;
8500 memcpy(transform->iv_dec, key1 + keylen, iv_copy_len);
8501 memcpy(transform->iv_enc, key1 + keylen + iv_copy_len,
8502 iv_copy_len);
8503 } else
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008504#endif /* MBEDTLS_SSL_SRV_C */
8505 {
Gilles Peskine449bd832023-01-11 14:50:10 +01008506 MBEDTLS_SSL_DEBUG_MSG(1, ("should never happen"));
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008507 ret = MBEDTLS_ERR_SSL_INTERNAL_ERROR;
8508 goto end;
8509 }
8510
Gilles Peskine449bd832023-01-11 14:50:10 +01008511 if (ssl != NULL && ssl->f_export_keys != NULL) {
8512 ssl->f_export_keys(ssl->p_export_keys,
8513 MBEDTLS_SSL_KEY_EXPORT_TLS12_MASTER_SECRET,
8514 master, 48,
8515 randbytes + 32,
8516 randbytes,
8517 tls_prf_get_type(tls_prf));
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008518 }
8519
8520#if defined(MBEDTLS_USE_PSA_CRYPTO)
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008521 transform->psa_alg = alg;
8522
Gilles Peskine449bd832023-01-11 14:50:10 +01008523 if (alg != MBEDTLS_SSL_NULL_CIPHER) {
8524 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_ENCRYPT);
8525 psa_set_key_algorithm(&attributes, alg);
8526 psa_set_key_type(&attributes, key_type);
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008527
Gilles Peskine449bd832023-01-11 14:50:10 +01008528 if ((status = psa_import_key(&attributes,
8529 key1,
8530 PSA_BITS_TO_BYTES(key_bits),
8531 &transform->psa_key_enc)) != PSA_SUCCESS) {
8532 MBEDTLS_SSL_DEBUG_RET(3, "psa_import_key", (int) status);
8533 ret = psa_ssl_status_to_mbedtls(status);
8534 MBEDTLS_SSL_DEBUG_RET(1, "psa_import_key", ret);
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008535 goto end;
8536 }
8537
Gilles Peskine449bd832023-01-11 14:50:10 +01008538 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_DECRYPT);
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008539
Gilles Peskine449bd832023-01-11 14:50:10 +01008540 if ((status = psa_import_key(&attributes,
8541 key2,
8542 PSA_BITS_TO_BYTES(key_bits),
8543 &transform->psa_key_dec)) != PSA_SUCCESS) {
8544 ret = psa_ssl_status_to_mbedtls(status);
8545 MBEDTLS_SSL_DEBUG_RET(1, "psa_import_key", ret);
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008546 goto end;
8547 }
8548 }
8549#else
Gilles Peskine449bd832023-01-11 14:50:10 +01008550 if ((ret = mbedtls_cipher_setup(&transform->cipher_ctx_enc,
8551 cipher_info)) != 0) {
8552 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_cipher_setup", ret);
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008553 goto end;
8554 }
8555
Gilles Peskine449bd832023-01-11 14:50:10 +01008556 if ((ret = mbedtls_cipher_setup(&transform->cipher_ctx_dec,
8557 cipher_info)) != 0) {
8558 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_cipher_setup", ret);
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008559 goto end;
8560 }
8561
Gilles Peskine449bd832023-01-11 14:50:10 +01008562 if ((ret = mbedtls_cipher_setkey(&transform->cipher_ctx_enc, key1,
8563 (int) mbedtls_cipher_info_get_key_bitlen(cipher_info),
8564 MBEDTLS_ENCRYPT)) != 0) {
8565 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_cipher_setkey", ret);
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008566 goto end;
8567 }
8568
Gilles Peskine449bd832023-01-11 14:50:10 +01008569 if ((ret = mbedtls_cipher_setkey(&transform->cipher_ctx_dec, key2,
8570 (int) mbedtls_cipher_info_get_key_bitlen(cipher_info),
8571 MBEDTLS_DECRYPT)) != 0) {
8572 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_cipher_setkey", ret);
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008573 goto end;
8574 }
8575
8576#if defined(MBEDTLS_CIPHER_MODE_CBC)
Gilles Peskine449bd832023-01-11 14:50:10 +01008577 if (mbedtls_cipher_info_get_mode(cipher_info) == MBEDTLS_MODE_CBC) {
8578 if ((ret = mbedtls_cipher_set_padding_mode(&transform->cipher_ctx_enc,
8579 MBEDTLS_PADDING_NONE)) != 0) {
8580 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_cipher_set_padding_mode", ret);
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008581 goto end;
8582 }
8583
Gilles Peskine449bd832023-01-11 14:50:10 +01008584 if ((ret = mbedtls_cipher_set_padding_mode(&transform->cipher_ctx_dec,
8585 MBEDTLS_PADDING_NONE)) != 0) {
8586 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_cipher_set_padding_mode", ret);
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008587 goto end;
8588 }
8589 }
8590#endif /* MBEDTLS_CIPHER_MODE_CBC */
8591#endif /* MBEDTLS_USE_PSA_CRYPTO */
8592
Neil Armstrong29c0c042022-03-17 17:47:28 +01008593#if defined(MBEDTLS_SSL_SOME_SUITES_USE_MAC)
8594 /* For HMAC-based ciphersuites, initialize the HMAC transforms.
8595 For AEAD-based ciphersuites, there is nothing to do here. */
Gilles Peskine449bd832023-01-11 14:50:10 +01008596 if (mac_key_len != 0) {
Neil Armstrong29c0c042022-03-17 17:47:28 +01008597#if defined(MBEDTLS_USE_PSA_CRYPTO)
Gilles Peskine449bd832023-01-11 14:50:10 +01008598 transform->psa_mac_alg = PSA_ALG_HMAC(mac_alg);
Neil Armstrong29c0c042022-03-17 17:47:28 +01008599
Gilles Peskine449bd832023-01-11 14:50:10 +01008600 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_SIGN_MESSAGE);
8601 psa_set_key_algorithm(&attributes, PSA_ALG_HMAC(mac_alg));
8602 psa_set_key_type(&attributes, PSA_KEY_TYPE_HMAC);
Neil Armstrong29c0c042022-03-17 17:47:28 +01008603
Gilles Peskine449bd832023-01-11 14:50:10 +01008604 if ((status = psa_import_key(&attributes,
8605 mac_enc, mac_key_len,
8606 &transform->psa_mac_enc)) != PSA_SUCCESS) {
8607 ret = psa_ssl_status_to_mbedtls(status);
8608 MBEDTLS_SSL_DEBUG_RET(1, "psa_import_mac_key", ret);
Neil Armstrong29c0c042022-03-17 17:47:28 +01008609 goto end;
8610 }
8611
Gilles Peskine449bd832023-01-11 14:50:10 +01008612 if ((transform->psa_alg == MBEDTLS_SSL_NULL_CIPHER) ||
8613 ((transform->psa_alg == PSA_ALG_CBC_NO_PADDING)
Andrzej Kurek8c95ac42022-08-17 16:17:00 -04008614#if defined(MBEDTLS_SSL_SOME_SUITES_USE_CBC_ETM)
Gilles Peskine449bd832023-01-11 14:50:10 +01008615 && (transform->encrypt_then_mac == MBEDTLS_SSL_ETM_DISABLED)
Andrzej Kurek8c95ac42022-08-17 16:17:00 -04008616#endif
Gilles Peskine449bd832023-01-11 14:50:10 +01008617 )) {
Neil Armstrong29c0c042022-03-17 17:47:28 +01008618 /* mbedtls_ct_hmac() requires the key to be exportable */
Gilles Peskine449bd832023-01-11 14:50:10 +01008619 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_EXPORT |
8620 PSA_KEY_USAGE_VERIFY_HASH);
8621 } else {
8622 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_VERIFY_HASH);
8623 }
Neil Armstrong29c0c042022-03-17 17:47:28 +01008624
Gilles Peskine449bd832023-01-11 14:50:10 +01008625 if ((status = psa_import_key(&attributes,
8626 mac_dec, mac_key_len,
8627 &transform->psa_mac_dec)) != PSA_SUCCESS) {
8628 ret = psa_ssl_status_to_mbedtls(status);
8629 MBEDTLS_SSL_DEBUG_RET(1, "psa_import_mac_key", ret);
Neil Armstrong29c0c042022-03-17 17:47:28 +01008630 goto end;
8631 }
8632#else
Gilles Peskine449bd832023-01-11 14:50:10 +01008633 ret = mbedtls_md_hmac_starts(&transform->md_ctx_enc, mac_enc, mac_key_len);
8634 if (ret != 0) {
Neil Armstrong29c0c042022-03-17 17:47:28 +01008635 goto end;
Gilles Peskine449bd832023-01-11 14:50:10 +01008636 }
8637 ret = mbedtls_md_hmac_starts(&transform->md_ctx_dec, mac_dec, mac_key_len);
8638 if (ret != 0) {
Neil Armstrong29c0c042022-03-17 17:47:28 +01008639 goto end;
Gilles Peskine449bd832023-01-11 14:50:10 +01008640 }
Neil Armstrong29c0c042022-03-17 17:47:28 +01008641#endif /* MBEDTLS_USE_PSA_CRYPTO */
8642 }
8643#endif /* MBEDTLS_SSL_SOME_SUITES_USE_MAC */
8644
8645 ((void) mac_dec);
8646 ((void) mac_enc);
8647
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008648end:
Gilles Peskine449bd832023-01-11 14:50:10 +01008649 mbedtls_platform_zeroize(keyblk, sizeof(keyblk));
8650 return ret;
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008651}
8652
Valerio Settia08b1a42022-11-17 15:10:02 +01008653#if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED) && \
8654 defined(MBEDTLS_USE_PSA_CRYPTO)
Valerio Setti6b3dab02022-11-17 17:14:54 +01008655int mbedtls_psa_ecjpake_read_round(
Gilles Peskine449bd832023-01-11 14:50:10 +01008656 psa_pake_operation_t *pake_ctx,
8657 const unsigned char *buf,
8658 size_t len, mbedtls_ecjpake_rounds_t round)
Valerio Settia08b1a42022-11-17 15:10:02 +01008659{
8660 psa_status_t status;
8661 size_t input_offset = 0;
Valerio Setti819de862022-11-17 18:05:19 +01008662 /*
Valerio Setti6b3dab02022-11-17 17:14:54 +01008663 * At round one repeat the KEY_SHARE, ZK_PUBLIC & ZF_PROOF twice
8664 * At round two perform a single cycle
8665 */
Gilles Peskine449bd832023-01-11 14:50:10 +01008666 unsigned int remaining_steps = (round == MBEDTLS_ECJPAKE_ROUND_ONE) ? 2 : 1;
Valerio Settia08b1a42022-11-17 15:10:02 +01008667
Gilles Peskine449bd832023-01-11 14:50:10 +01008668 for (; remaining_steps > 0; remaining_steps--) {
8669 for (psa_pake_step_t step = PSA_PAKE_STEP_KEY_SHARE;
Valerio Settia08b1a42022-11-17 15:10:02 +01008670 step <= PSA_PAKE_STEP_ZK_PROOF;
Gilles Peskine449bd832023-01-11 14:50:10 +01008671 ++step) {
Valerio Settia08b1a42022-11-17 15:10:02 +01008672 /* Length is stored at the first byte */
8673 size_t length = buf[input_offset];
8674 input_offset += 1;
8675
Gilles Peskine449bd832023-01-11 14:50:10 +01008676 if (input_offset + length > len) {
Valerio Settia08b1a42022-11-17 15:10:02 +01008677 return MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE;
8678 }
8679
Gilles Peskine449bd832023-01-11 14:50:10 +01008680 status = psa_pake_input(pake_ctx, step,
8681 buf + input_offset, length);
8682 if (status != PSA_SUCCESS) {
8683 return psa_ssl_status_to_mbedtls(status);
Valerio Settia08b1a42022-11-17 15:10:02 +01008684 }
8685
8686 input_offset += length;
8687 }
8688 }
8689
Gilles Peskine449bd832023-01-11 14:50:10 +01008690 if (input_offset != len) {
Valerio Setti61ea17d2022-11-18 12:11:00 +01008691 return MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE;
Gilles Peskine449bd832023-01-11 14:50:10 +01008692 }
Valerio Setti30ebe112022-11-17 16:23:34 +01008693
Gilles Peskine449bd832023-01-11 14:50:10 +01008694 return 0;
Valerio Settia08b1a42022-11-17 15:10:02 +01008695}
8696
Valerio Setti6b3dab02022-11-17 17:14:54 +01008697int mbedtls_psa_ecjpake_write_round(
Gilles Peskine449bd832023-01-11 14:50:10 +01008698 psa_pake_operation_t *pake_ctx,
8699 unsigned char *buf,
8700 size_t len, size_t *olen,
8701 mbedtls_ecjpake_rounds_t round)
Valerio Settia08b1a42022-11-17 15:10:02 +01008702{
8703 psa_status_t status;
8704 size_t output_offset = 0;
8705 size_t output_len;
Valerio Setti819de862022-11-17 18:05:19 +01008706 /*
Valerio Setti6b3dab02022-11-17 17:14:54 +01008707 * At round one repeat the KEY_SHARE, ZK_PUBLIC & ZF_PROOF twice
8708 * At round two perform a single cycle
8709 */
Gilles Peskine449bd832023-01-11 14:50:10 +01008710 unsigned int remaining_steps = (round == MBEDTLS_ECJPAKE_ROUND_ONE) ? 2 : 1;
Valerio Settia08b1a42022-11-17 15:10:02 +01008711
Gilles Peskine449bd832023-01-11 14:50:10 +01008712 for (; remaining_steps > 0; remaining_steps--) {
8713 for (psa_pake_step_t step = PSA_PAKE_STEP_KEY_SHARE;
8714 step <= PSA_PAKE_STEP_ZK_PROOF;
8715 ++step) {
Valerio Setti79f6b6b2022-11-21 14:17:03 +01008716 /*
Valerio Settid4a9b1a2022-11-22 11:11:10 +01008717 * For each step, prepend 1 byte with the length of the data as
8718 * given by psa_pake_output().
Valerio Setti79f6b6b2022-11-21 14:17:03 +01008719 */
Gilles Peskine449bd832023-01-11 14:50:10 +01008720 status = psa_pake_output(pake_ctx, step,
8721 buf + output_offset + 1,
8722 len - output_offset - 1,
8723 &output_len);
8724 if (status != PSA_SUCCESS) {
8725 return psa_ssl_status_to_mbedtls(status);
Valerio Settia08b1a42022-11-17 15:10:02 +01008726 }
8727
Valerio Setti99d88c12022-11-22 16:03:43 +01008728 *(buf + output_offset) = (uint8_t) output_len;
Valerio Setti79f6b6b2022-11-21 14:17:03 +01008729
8730 output_offset += output_len + 1;
Valerio Settia08b1a42022-11-17 15:10:02 +01008731 }
8732 }
8733
8734 *olen = output_offset;
8735
Gilles Peskine449bd832023-01-11 14:50:10 +01008736 return 0;
Valerio Settia08b1a42022-11-17 15:10:02 +01008737}
Valerio Settia08b1a42022-11-17 15:10:02 +01008738#endif //MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED && MBEDTLS_USE_PSA_CRYPTO
8739
Jerry Yuee40f9d2022-02-17 14:55:16 +08008740#if defined(MBEDTLS_USE_PSA_CRYPTO)
Gilles Peskine449bd832023-01-11 14:50:10 +01008741int mbedtls_ssl_get_key_exchange_md_tls1_2(mbedtls_ssl_context *ssl,
8742 unsigned char *hash, size_t *hashlen,
8743 unsigned char *data, size_t data_len,
8744 mbedtls_md_type_t md_alg)
Jerry Yuee40f9d2022-02-17 14:55:16 +08008745{
8746 psa_status_t status;
8747 psa_hash_operation_t hash_operation = PSA_HASH_OPERATION_INIT;
Gilles Peskine449bd832023-01-11 14:50:10 +01008748 psa_algorithm_t hash_alg = mbedtls_hash_info_psa_from_md(md_alg);
Jerry Yuee40f9d2022-02-17 14:55:16 +08008749
Gilles Peskine449bd832023-01-11 14:50:10 +01008750 MBEDTLS_SSL_DEBUG_MSG(3, ("Perform PSA-based computation of digest of ServerKeyExchange"));
Jerry Yuee40f9d2022-02-17 14:55:16 +08008751
Gilles Peskine449bd832023-01-11 14:50:10 +01008752 if ((status = psa_hash_setup(&hash_operation,
8753 hash_alg)) != PSA_SUCCESS) {
8754 MBEDTLS_SSL_DEBUG_RET(1, "psa_hash_setup", status);
Jerry Yuee40f9d2022-02-17 14:55:16 +08008755 goto exit;
8756 }
8757
Gilles Peskine449bd832023-01-11 14:50:10 +01008758 if ((status = psa_hash_update(&hash_operation, ssl->handshake->randbytes,
8759 64)) != PSA_SUCCESS) {
8760 MBEDTLS_SSL_DEBUG_RET(1, "psa_hash_update", status);
Jerry Yuee40f9d2022-02-17 14:55:16 +08008761 goto exit;
8762 }
8763
Gilles Peskine449bd832023-01-11 14:50:10 +01008764 if ((status = psa_hash_update(&hash_operation,
8765 data, data_len)) != PSA_SUCCESS) {
8766 MBEDTLS_SSL_DEBUG_RET(1, "psa_hash_update", status);
Jerry Yuee40f9d2022-02-17 14:55:16 +08008767 goto exit;
8768 }
8769
Gilles Peskine449bd832023-01-11 14:50:10 +01008770 if ((status = psa_hash_finish(&hash_operation, hash, PSA_HASH_MAX_SIZE,
8771 hashlen)) != PSA_SUCCESS) {
8772 MBEDTLS_SSL_DEBUG_RET(1, "psa_hash_finish", status);
8773 goto exit;
Jerry Yuee40f9d2022-02-17 14:55:16 +08008774 }
8775
8776exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01008777 if (status != PSA_SUCCESS) {
8778 mbedtls_ssl_send_alert_message(ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
8779 MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR);
8780 switch (status) {
Jerry Yuee40f9d2022-02-17 14:55:16 +08008781 case PSA_ERROR_NOT_SUPPORTED:
Gilles Peskine449bd832023-01-11 14:50:10 +01008782 return MBEDTLS_ERR_MD_FEATURE_UNAVAILABLE;
Jerry Yuee40f9d2022-02-17 14:55:16 +08008783 case PSA_ERROR_BAD_STATE: /* Intentional fallthrough */
8784 case PSA_ERROR_BUFFER_TOO_SMALL:
Gilles Peskine449bd832023-01-11 14:50:10 +01008785 return MBEDTLS_ERR_MD_BAD_INPUT_DATA;
Jerry Yuee40f9d2022-02-17 14:55:16 +08008786 case PSA_ERROR_INSUFFICIENT_MEMORY:
Gilles Peskine449bd832023-01-11 14:50:10 +01008787 return MBEDTLS_ERR_MD_ALLOC_FAILED;
Jerry Yuee40f9d2022-02-17 14:55:16 +08008788 default:
Gilles Peskine449bd832023-01-11 14:50:10 +01008789 return MBEDTLS_ERR_PLATFORM_HW_ACCEL_FAILED;
Jerry Yuee40f9d2022-02-17 14:55:16 +08008790 }
8791 }
Gilles Peskine449bd832023-01-11 14:50:10 +01008792 return 0;
Jerry Yuee40f9d2022-02-17 14:55:16 +08008793}
8794
8795#else
8796
Gilles Peskine449bd832023-01-11 14:50:10 +01008797int mbedtls_ssl_get_key_exchange_md_tls1_2(mbedtls_ssl_context *ssl,
8798 unsigned char *hash, size_t *hashlen,
8799 unsigned char *data, size_t data_len,
8800 mbedtls_md_type_t md_alg)
Jerry Yuee40f9d2022-02-17 14:55:16 +08008801{
8802 int ret = 0;
8803 mbedtls_md_context_t ctx;
Gilles Peskine449bd832023-01-11 14:50:10 +01008804 const mbedtls_md_info_t *md_info = mbedtls_md_info_from_type(md_alg);
8805 *hashlen = mbedtls_md_get_size(md_info);
Jerry Yuee40f9d2022-02-17 14:55:16 +08008806
Gilles Peskine449bd832023-01-11 14:50:10 +01008807 MBEDTLS_SSL_DEBUG_MSG(3, ("Perform mbedtls-based computation of digest of ServerKeyExchange"));
Jerry Yuee40f9d2022-02-17 14:55:16 +08008808
Gilles Peskine449bd832023-01-11 14:50:10 +01008809 mbedtls_md_init(&ctx);
Jerry Yuee40f9d2022-02-17 14:55:16 +08008810
8811 /*
8812 * digitally-signed struct {
8813 * opaque client_random[32];
8814 * opaque server_random[32];
8815 * ServerDHParams params;
8816 * };
8817 */
Gilles Peskine449bd832023-01-11 14:50:10 +01008818 if ((ret = mbedtls_md_setup(&ctx, md_info, 0)) != 0) {
8819 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_md_setup", ret);
Jerry Yuee40f9d2022-02-17 14:55:16 +08008820 goto exit;
8821 }
Gilles Peskine449bd832023-01-11 14:50:10 +01008822 if ((ret = mbedtls_md_starts(&ctx)) != 0) {
8823 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_md_starts", ret);
Jerry Yuee40f9d2022-02-17 14:55:16 +08008824 goto exit;
8825 }
Gilles Peskine449bd832023-01-11 14:50:10 +01008826 if ((ret = mbedtls_md_update(&ctx, ssl->handshake->randbytes, 64)) != 0) {
8827 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_md_update", ret);
Jerry Yuee40f9d2022-02-17 14:55:16 +08008828 goto exit;
8829 }
Gilles Peskine449bd832023-01-11 14:50:10 +01008830 if ((ret = mbedtls_md_update(&ctx, data, data_len)) != 0) {
8831 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_md_update", ret);
Jerry Yuee40f9d2022-02-17 14:55:16 +08008832 goto exit;
8833 }
Gilles Peskine449bd832023-01-11 14:50:10 +01008834 if ((ret = mbedtls_md_finish(&ctx, hash)) != 0) {
8835 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_md_finish", ret);
Jerry Yuee40f9d2022-02-17 14:55:16 +08008836 goto exit;
8837 }
8838
8839exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01008840 mbedtls_md_free(&ctx);
Jerry Yuee40f9d2022-02-17 14:55:16 +08008841
Gilles Peskine449bd832023-01-11 14:50:10 +01008842 if (ret != 0) {
8843 mbedtls_ssl_send_alert_message(ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
8844 MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR);
8845 }
Jerry Yuee40f9d2022-02-17 14:55:16 +08008846
Gilles Peskine449bd832023-01-11 14:50:10 +01008847 return ret;
Jerry Yuee40f9d2022-02-17 14:55:16 +08008848}
8849#endif /* MBEDTLS_USE_PSA_CRYPTO */
8850
Jerry Yud9d91da2022-02-17 14:57:06 +08008851#if defined(MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED)
8852
Gabor Mezeia3d016c2022-05-10 12:44:09 +02008853/* Find the preferred hash for a given signature algorithm. */
8854unsigned int mbedtls_ssl_tls12_get_preferred_hash_for_sig_alg(
Gilles Peskine449bd832023-01-11 14:50:10 +01008855 mbedtls_ssl_context *ssl,
8856 unsigned int sig_alg)
Jerry Yud9d91da2022-02-17 14:57:06 +08008857{
Gabor Mezei078e8032022-04-27 21:17:56 +02008858 unsigned int i;
Gabor Mezeia3d016c2022-05-10 12:44:09 +02008859 uint16_t *received_sig_algs = ssl->handshake->received_sig_algs;
Gabor Mezei078e8032022-04-27 21:17:56 +02008860
Gilles Peskine449bd832023-01-11 14:50:10 +01008861 if (sig_alg == MBEDTLS_SSL_SIG_ANON) {
8862 return MBEDTLS_SSL_HASH_NONE;
8863 }
Gabor Mezei078e8032022-04-27 21:17:56 +02008864
Gilles Peskine449bd832023-01-11 14:50:10 +01008865 for (i = 0; received_sig_algs[i] != MBEDTLS_TLS_SIG_NONE; i++) {
Neil Armstrong9f1176a2022-06-24 18:19:19 +02008866 unsigned int hash_alg_received =
Gilles Peskine449bd832023-01-11 14:50:10 +01008867 MBEDTLS_SSL_TLS12_HASH_ALG_FROM_SIG_AND_HASH_ALG(
8868 received_sig_algs[i]);
Neil Armstrong9f1176a2022-06-24 18:19:19 +02008869 unsigned int sig_alg_received =
Gilles Peskine449bd832023-01-11 14:50:10 +01008870 MBEDTLS_SSL_TLS12_SIG_ALG_FROM_SIG_AND_HASH_ALG(
8871 received_sig_algs[i]);
Neil Armstrong9f1176a2022-06-24 18:19:19 +02008872
Gilles Peskine449bd832023-01-11 14:50:10 +01008873 if (sig_alg == sig_alg_received) {
Neil Armstrong9f1176a2022-06-24 18:19:19 +02008874#if defined(MBEDTLS_USE_PSA_CRYPTO)
Gilles Peskine449bd832023-01-11 14:50:10 +01008875 if (ssl->handshake->key_cert && ssl->handshake->key_cert->key) {
Neil Armstrong96eceb82022-06-30 18:05:05 +02008876 psa_algorithm_t psa_hash_alg =
Gilles Peskine449bd832023-01-11 14:50:10 +01008877 mbedtls_hash_info_psa_from_md(hash_alg_received);
Neil Armstrong9f1176a2022-06-24 18:19:19 +02008878
Gilles Peskine449bd832023-01-11 14:50:10 +01008879 if (sig_alg_received == MBEDTLS_SSL_SIG_ECDSA &&
8880 !mbedtls_pk_can_do_ext(ssl->handshake->key_cert->key,
8881 PSA_ALG_ECDSA(psa_hash_alg),
8882 PSA_KEY_USAGE_SIGN_HASH)) {
Neil Armstrong9f1176a2022-06-24 18:19:19 +02008883 continue;
Gilles Peskine449bd832023-01-11 14:50:10 +01008884 }
Neil Armstrong9f1176a2022-06-24 18:19:19 +02008885
Gilles Peskine449bd832023-01-11 14:50:10 +01008886 if (sig_alg_received == MBEDTLS_SSL_SIG_RSA &&
8887 !mbedtls_pk_can_do_ext(ssl->handshake->key_cert->key,
8888 PSA_ALG_RSA_PKCS1V15_SIGN(
8889 psa_hash_alg),
8890 PSA_KEY_USAGE_SIGN_HASH)) {
Neil Armstrong9f1176a2022-06-24 18:19:19 +02008891 continue;
Gilles Peskine449bd832023-01-11 14:50:10 +01008892 }
Neil Armstrong9f1176a2022-06-24 18:19:19 +02008893 }
Neil Armstrong9f1176a2022-06-24 18:19:19 +02008894#endif /* MBEDTLS_USE_PSA_CRYPTO */
Neil Armstrong96eceb82022-06-30 18:05:05 +02008895
Gilles Peskine449bd832023-01-11 14:50:10 +01008896 return hash_alg_received;
Neil Armstrong9f1176a2022-06-24 18:19:19 +02008897 }
Jerry Yud9d91da2022-02-17 14:57:06 +08008898 }
Jerry Yud9d91da2022-02-17 14:57:06 +08008899
Gilles Peskine449bd832023-01-11 14:50:10 +01008900 return MBEDTLS_SSL_HASH_NONE;
Jerry Yud9d91da2022-02-17 14:57:06 +08008901}
8902
8903#endif /* MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED */
8904
Jerry Yu4f9e3ef2022-02-17 14:58:27 +08008905/* Serialization of TLS 1.2 sessions:
8906 *
8907 * struct {
8908 * uint64 start_time;
8909 * uint8 ciphersuite[2]; // defined by the standard
Jerry Yu4f9e3ef2022-02-17 14:58:27 +08008910 * uint8 session_id_len; // at most 32
8911 * opaque session_id[32];
8912 * opaque master[48]; // fixed length in the standard
8913 * uint32 verify_result;
8914 * opaque peer_cert<0..2^24-1>; // length 0 means no peer cert
8915 * opaque ticket<0..2^24-1>; // length 0 means no ticket
8916 * uint32 ticket_lifetime;
8917 * uint8 mfl_code; // up to 255 according to standard
8918 * uint8 encrypt_then_mac; // 0 or 1
8919 * } serialized_session_tls12;
8920 *
8921 */
Gilles Peskine449bd832023-01-11 14:50:10 +01008922static size_t ssl_tls12_session_save(const mbedtls_ssl_session *session,
8923 unsigned char *buf,
8924 size_t buf_len)
Jerry Yu4f9e3ef2022-02-17 14:58:27 +08008925{
8926 unsigned char *p = buf;
8927 size_t used = 0;
8928
8929#if defined(MBEDTLS_HAVE_TIME)
8930 uint64_t start;
8931#endif
8932#if defined(MBEDTLS_X509_CRT_PARSE_C)
8933#if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
8934 size_t cert_len;
8935#endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
8936#endif /* MBEDTLS_X509_CRT_PARSE_C */
8937
8938 /*
8939 * Time
8940 */
8941#if defined(MBEDTLS_HAVE_TIME)
8942 used += 8;
8943
Gilles Peskine449bd832023-01-11 14:50:10 +01008944 if (used <= buf_len) {
Jerry Yu4f9e3ef2022-02-17 14:58:27 +08008945 start = (uint64_t) session->start;
8946
Gilles Peskine449bd832023-01-11 14:50:10 +01008947 MBEDTLS_PUT_UINT64_BE(start, p, 0);
Jerry Yu4f9e3ef2022-02-17 14:58:27 +08008948 p += 8;
8949 }
8950#endif /* MBEDTLS_HAVE_TIME */
8951
8952 /*
8953 * Basic mandatory fields
8954 */
8955 used += 2 /* ciphersuite */
Gilles Peskine449bd832023-01-11 14:50:10 +01008956 + 1 /* id_len */
8957 + sizeof(session->id)
8958 + sizeof(session->master)
8959 + 4; /* verify_result */
Jerry Yu4f9e3ef2022-02-17 14:58:27 +08008960
Gilles Peskine449bd832023-01-11 14:50:10 +01008961 if (used <= buf_len) {
8962 MBEDTLS_PUT_UINT16_BE(session->ciphersuite, p, 0);
Jerry Yu4f9e3ef2022-02-17 14:58:27 +08008963 p += 2;
8964
Gilles Peskine449bd832023-01-11 14:50:10 +01008965 *p++ = MBEDTLS_BYTE_0(session->id_len);
8966 memcpy(p, session->id, 32);
Jerry Yu4f9e3ef2022-02-17 14:58:27 +08008967 p += 32;
8968
Gilles Peskine449bd832023-01-11 14:50:10 +01008969 memcpy(p, session->master, 48);
Jerry Yu4f9e3ef2022-02-17 14:58:27 +08008970 p += 48;
8971
Gilles Peskine449bd832023-01-11 14:50:10 +01008972 MBEDTLS_PUT_UINT32_BE(session->verify_result, p, 0);
Jerry Yu4f9e3ef2022-02-17 14:58:27 +08008973 p += 4;
8974 }
8975
8976 /*
8977 * Peer's end-entity certificate
8978 */
8979#if defined(MBEDTLS_X509_CRT_PARSE_C)
8980#if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
Gilles Peskine449bd832023-01-11 14:50:10 +01008981 if (session->peer_cert == NULL) {
Jerry Yu4f9e3ef2022-02-17 14:58:27 +08008982 cert_len = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +01008983 } else {
Jerry Yu4f9e3ef2022-02-17 14:58:27 +08008984 cert_len = session->peer_cert->raw.len;
Gilles Peskine449bd832023-01-11 14:50:10 +01008985 }
Jerry Yu4f9e3ef2022-02-17 14:58:27 +08008986
8987 used += 3 + cert_len;
8988
Gilles Peskine449bd832023-01-11 14:50:10 +01008989 if (used <= buf_len) {
8990 *p++ = MBEDTLS_BYTE_2(cert_len);
8991 *p++ = MBEDTLS_BYTE_1(cert_len);
8992 *p++ = MBEDTLS_BYTE_0(cert_len);
Jerry Yu4f9e3ef2022-02-17 14:58:27 +08008993
Gilles Peskine449bd832023-01-11 14:50:10 +01008994 if (session->peer_cert != NULL) {
8995 memcpy(p, session->peer_cert->raw.p, cert_len);
Jerry Yu4f9e3ef2022-02-17 14:58:27 +08008996 p += cert_len;
8997 }
8998 }
8999#else /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
Gilles Peskine449bd832023-01-11 14:50:10 +01009000 if (session->peer_cert_digest != NULL) {
Jerry Yu4f9e3ef2022-02-17 14:58:27 +08009001 used += 1 /* type */ + 1 /* length */ + session->peer_cert_digest_len;
Gilles Peskine449bd832023-01-11 14:50:10 +01009002 if (used <= buf_len) {
Jerry Yu4f9e3ef2022-02-17 14:58:27 +08009003 *p++ = (unsigned char) session->peer_cert_digest_type;
9004 *p++ = (unsigned char) session->peer_cert_digest_len;
Gilles Peskine449bd832023-01-11 14:50:10 +01009005 memcpy(p, session->peer_cert_digest,
9006 session->peer_cert_digest_len);
Jerry Yu4f9e3ef2022-02-17 14:58:27 +08009007 p += session->peer_cert_digest_len;
9008 }
Gilles Peskine449bd832023-01-11 14:50:10 +01009009 } else {
Jerry Yu4f9e3ef2022-02-17 14:58:27 +08009010 used += 2;
Gilles Peskine449bd832023-01-11 14:50:10 +01009011 if (used <= buf_len) {
Jerry Yu4f9e3ef2022-02-17 14:58:27 +08009012 *p++ = (unsigned char) MBEDTLS_MD_NONE;
9013 *p++ = 0;
9014 }
9015 }
9016#endif /* !MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
9017#endif /* MBEDTLS_X509_CRT_PARSE_C */
9018
9019 /*
9020 * Session ticket if any, plus associated data
9021 */
9022#if defined(MBEDTLS_SSL_SESSION_TICKETS) && defined(MBEDTLS_SSL_CLI_C)
9023 used += 3 + session->ticket_len + 4; /* len + ticket + lifetime */
9024
Gilles Peskine449bd832023-01-11 14:50:10 +01009025 if (used <= buf_len) {
9026 *p++ = MBEDTLS_BYTE_2(session->ticket_len);
9027 *p++ = MBEDTLS_BYTE_1(session->ticket_len);
9028 *p++ = MBEDTLS_BYTE_0(session->ticket_len);
Jerry Yu4f9e3ef2022-02-17 14:58:27 +08009029
Gilles Peskine449bd832023-01-11 14:50:10 +01009030 if (session->ticket != NULL) {
9031 memcpy(p, session->ticket, session->ticket_len);
Jerry Yu4f9e3ef2022-02-17 14:58:27 +08009032 p += session->ticket_len;
9033 }
9034
Gilles Peskine449bd832023-01-11 14:50:10 +01009035 MBEDTLS_PUT_UINT32_BE(session->ticket_lifetime, p, 0);
Jerry Yu4f9e3ef2022-02-17 14:58:27 +08009036 p += 4;
9037 }
9038#endif /* MBEDTLS_SSL_SESSION_TICKETS && MBEDTLS_SSL_CLI_C */
9039
9040 /*
9041 * Misc extension-related info
9042 */
9043#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
9044 used += 1;
9045
Gilles Peskine449bd832023-01-11 14:50:10 +01009046 if (used <= buf_len) {
Jerry Yu4f9e3ef2022-02-17 14:58:27 +08009047 *p++ = session->mfl_code;
Gilles Peskine449bd832023-01-11 14:50:10 +01009048 }
Jerry Yu4f9e3ef2022-02-17 14:58:27 +08009049#endif
9050
9051#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
9052 used += 1;
9053
Gilles Peskine449bd832023-01-11 14:50:10 +01009054 if (used <= buf_len) {
9055 *p++ = MBEDTLS_BYTE_0(session->encrypt_then_mac);
9056 }
Jerry Yu4f9e3ef2022-02-17 14:58:27 +08009057#endif
9058
Gilles Peskine449bd832023-01-11 14:50:10 +01009059 return used;
Jerry Yu4f9e3ef2022-02-17 14:58:27 +08009060}
9061
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02009062MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01009063static int ssl_tls12_session_load(mbedtls_ssl_session *session,
9064 const unsigned char *buf,
9065 size_t len)
Jerry Yu4f9e3ef2022-02-17 14:58:27 +08009066{
9067#if defined(MBEDTLS_HAVE_TIME)
9068 uint64_t start;
9069#endif
9070#if defined(MBEDTLS_X509_CRT_PARSE_C)
9071#if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
9072 size_t cert_len;
9073#endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
9074#endif /* MBEDTLS_X509_CRT_PARSE_C */
9075
9076 const unsigned char *p = buf;
9077 const unsigned char * const end = buf + len;
9078
9079 /*
9080 * Time
9081 */
9082#if defined(MBEDTLS_HAVE_TIME)
Gilles Peskine449bd832023-01-11 14:50:10 +01009083 if (8 > (size_t) (end - p)) {
9084 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
9085 }
Jerry Yu4f9e3ef2022-02-17 14:58:27 +08009086
Gilles Peskine449bd832023-01-11 14:50:10 +01009087 start = ((uint64_t) p[0] << 56) |
9088 ((uint64_t) p[1] << 48) |
9089 ((uint64_t) p[2] << 40) |
9090 ((uint64_t) p[3] << 32) |
9091 ((uint64_t) p[4] << 24) |
9092 ((uint64_t) p[5] << 16) |
9093 ((uint64_t) p[6] << 8) |
9094 ((uint64_t) p[7]);
Jerry Yu4f9e3ef2022-02-17 14:58:27 +08009095 p += 8;
9096
9097 session->start = (time_t) start;
9098#endif /* MBEDTLS_HAVE_TIME */
9099
9100 /*
9101 * Basic mandatory fields
9102 */
Gilles Peskine449bd832023-01-11 14:50:10 +01009103 if (2 + 1 + 32 + 48 + 4 > (size_t) (end - p)) {
9104 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
9105 }
Jerry Yu4f9e3ef2022-02-17 14:58:27 +08009106
Gilles Peskine449bd832023-01-11 14:50:10 +01009107 session->ciphersuite = (p[0] << 8) | p[1];
Jerry Yu4f9e3ef2022-02-17 14:58:27 +08009108 p += 2;
9109
Jerry Yu4f9e3ef2022-02-17 14:58:27 +08009110 session->id_len = *p++;
Gilles Peskine449bd832023-01-11 14:50:10 +01009111 memcpy(session->id, p, 32);
Jerry Yu4f9e3ef2022-02-17 14:58:27 +08009112 p += 32;
9113
Gilles Peskine449bd832023-01-11 14:50:10 +01009114 memcpy(session->master, p, 48);
Jerry Yu4f9e3ef2022-02-17 14:58:27 +08009115 p += 48;
9116
Gilles Peskine449bd832023-01-11 14:50:10 +01009117 session->verify_result = ((uint32_t) p[0] << 24) |
9118 ((uint32_t) p[1] << 16) |
9119 ((uint32_t) p[2] << 8) |
9120 ((uint32_t) p[3]);
Jerry Yu4f9e3ef2022-02-17 14:58:27 +08009121 p += 4;
9122
9123 /* Immediately clear invalid pointer values that have been read, in case
9124 * we exit early before we replaced them with valid ones. */
9125#if defined(MBEDTLS_X509_CRT_PARSE_C)
9126#if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
9127 session->peer_cert = NULL;
9128#else
9129 session->peer_cert_digest = NULL;
9130#endif /* !MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
9131#endif /* MBEDTLS_X509_CRT_PARSE_C */
9132#if defined(MBEDTLS_SSL_SESSION_TICKETS) && defined(MBEDTLS_SSL_CLI_C)
9133 session->ticket = NULL;
9134#endif /* MBEDTLS_SSL_SESSION_TICKETS && MBEDTLS_SSL_CLI_C */
9135
9136 /*
9137 * Peer certificate
9138 */
9139#if defined(MBEDTLS_X509_CRT_PARSE_C)
9140#if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
9141 /* Deserialize CRT from the end of the ticket. */
Gilles Peskine449bd832023-01-11 14:50:10 +01009142 if (3 > (size_t) (end - p)) {
9143 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
9144 }
Jerry Yu4f9e3ef2022-02-17 14:58:27 +08009145
Gilles Peskine449bd832023-01-11 14:50:10 +01009146 cert_len = (p[0] << 16) | (p[1] << 8) | p[2];
Jerry Yu4f9e3ef2022-02-17 14:58:27 +08009147 p += 3;
9148
Gilles Peskine449bd832023-01-11 14:50:10 +01009149 if (cert_len != 0) {
Jerry Yu4f9e3ef2022-02-17 14:58:27 +08009150 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
9151
Gilles Peskine449bd832023-01-11 14:50:10 +01009152 if (cert_len > (size_t) (end - p)) {
9153 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
9154 }
Jerry Yu4f9e3ef2022-02-17 14:58:27 +08009155
Gilles Peskine449bd832023-01-11 14:50:10 +01009156 session->peer_cert = mbedtls_calloc(1, sizeof(mbedtls_x509_crt));
Jerry Yu4f9e3ef2022-02-17 14:58:27 +08009157
Gilles Peskine449bd832023-01-11 14:50:10 +01009158 if (session->peer_cert == NULL) {
9159 return MBEDTLS_ERR_SSL_ALLOC_FAILED;
9160 }
Jerry Yu4f9e3ef2022-02-17 14:58:27 +08009161
Gilles Peskine449bd832023-01-11 14:50:10 +01009162 mbedtls_x509_crt_init(session->peer_cert);
Jerry Yu4f9e3ef2022-02-17 14:58:27 +08009163
Gilles Peskine449bd832023-01-11 14:50:10 +01009164 if ((ret = mbedtls_x509_crt_parse_der(session->peer_cert,
9165 p, cert_len)) != 0) {
9166 mbedtls_x509_crt_free(session->peer_cert);
9167 mbedtls_free(session->peer_cert);
Jerry Yu4f9e3ef2022-02-17 14:58:27 +08009168 session->peer_cert = NULL;
Gilles Peskine449bd832023-01-11 14:50:10 +01009169 return ret;
Jerry Yu4f9e3ef2022-02-17 14:58:27 +08009170 }
9171
9172 p += cert_len;
9173 }
9174#else /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
9175 /* Deserialize CRT digest from the end of the ticket. */
Gilles Peskine449bd832023-01-11 14:50:10 +01009176 if (2 > (size_t) (end - p)) {
9177 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
9178 }
Jerry Yu4f9e3ef2022-02-17 14:58:27 +08009179
9180 session->peer_cert_digest_type = (mbedtls_md_type_t) *p++;
9181 session->peer_cert_digest_len = (size_t) *p++;
9182
Gilles Peskine449bd832023-01-11 14:50:10 +01009183 if (session->peer_cert_digest_len != 0) {
Jerry Yu4f9e3ef2022-02-17 14:58:27 +08009184 const mbedtls_md_info_t *md_info =
Gilles Peskine449bd832023-01-11 14:50:10 +01009185 mbedtls_md_info_from_type(session->peer_cert_digest_type);
9186 if (md_info == NULL) {
9187 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
9188 }
9189 if (session->peer_cert_digest_len != mbedtls_md_get_size(md_info)) {
9190 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
9191 }
Jerry Yu4f9e3ef2022-02-17 14:58:27 +08009192
Gilles Peskine449bd832023-01-11 14:50:10 +01009193 if (session->peer_cert_digest_len > (size_t) (end - p)) {
9194 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
9195 }
Jerry Yu4f9e3ef2022-02-17 14:58:27 +08009196
9197 session->peer_cert_digest =
Gilles Peskine449bd832023-01-11 14:50:10 +01009198 mbedtls_calloc(1, session->peer_cert_digest_len);
9199 if (session->peer_cert_digest == NULL) {
9200 return MBEDTLS_ERR_SSL_ALLOC_FAILED;
9201 }
Jerry Yu4f9e3ef2022-02-17 14:58:27 +08009202
Gilles Peskine449bd832023-01-11 14:50:10 +01009203 memcpy(session->peer_cert_digest, p,
9204 session->peer_cert_digest_len);
Jerry Yu4f9e3ef2022-02-17 14:58:27 +08009205 p += session->peer_cert_digest_len;
9206 }
9207#endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
9208#endif /* MBEDTLS_X509_CRT_PARSE_C */
9209
9210 /*
9211 * Session ticket and associated data
9212 */
9213#if defined(MBEDTLS_SSL_SESSION_TICKETS) && defined(MBEDTLS_SSL_CLI_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01009214 if (3 > (size_t) (end - p)) {
9215 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
9216 }
Jerry Yu4f9e3ef2022-02-17 14:58:27 +08009217
Gilles Peskine449bd832023-01-11 14:50:10 +01009218 session->ticket_len = (p[0] << 16) | (p[1] << 8) | p[2];
Jerry Yu4f9e3ef2022-02-17 14:58:27 +08009219 p += 3;
9220
Gilles Peskine449bd832023-01-11 14:50:10 +01009221 if (session->ticket_len != 0) {
9222 if (session->ticket_len > (size_t) (end - p)) {
9223 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
9224 }
Jerry Yu4f9e3ef2022-02-17 14:58:27 +08009225
Gilles Peskine449bd832023-01-11 14:50:10 +01009226 session->ticket = mbedtls_calloc(1, session->ticket_len);
9227 if (session->ticket == NULL) {
9228 return MBEDTLS_ERR_SSL_ALLOC_FAILED;
9229 }
Jerry Yu4f9e3ef2022-02-17 14:58:27 +08009230
Gilles Peskine449bd832023-01-11 14:50:10 +01009231 memcpy(session->ticket, p, session->ticket_len);
Jerry Yu4f9e3ef2022-02-17 14:58:27 +08009232 p += session->ticket_len;
9233 }
9234
Gilles Peskine449bd832023-01-11 14:50:10 +01009235 if (4 > (size_t) (end - p)) {
9236 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
9237 }
Jerry Yu4f9e3ef2022-02-17 14:58:27 +08009238
Gilles Peskine449bd832023-01-11 14:50:10 +01009239 session->ticket_lifetime = ((uint32_t) p[0] << 24) |
9240 ((uint32_t) p[1] << 16) |
9241 ((uint32_t) p[2] << 8) |
9242 ((uint32_t) p[3]);
Jerry Yu4f9e3ef2022-02-17 14:58:27 +08009243 p += 4;
9244#endif /* MBEDTLS_SSL_SESSION_TICKETS && MBEDTLS_SSL_CLI_C */
9245
9246 /*
9247 * Misc extension-related info
9248 */
9249#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
Gilles Peskine449bd832023-01-11 14:50:10 +01009250 if (1 > (size_t) (end - p)) {
9251 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
9252 }
Jerry Yu4f9e3ef2022-02-17 14:58:27 +08009253
9254 session->mfl_code = *p++;
9255#endif
9256
9257#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
Gilles Peskine449bd832023-01-11 14:50:10 +01009258 if (1 > (size_t) (end - p)) {
9259 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
9260 }
Jerry Yu4f9e3ef2022-02-17 14:58:27 +08009261
9262 session->encrypt_then_mac = *p++;
9263#endif
9264
9265 /* Done, should have consumed entire buffer */
Gilles Peskine449bd832023-01-11 14:50:10 +01009266 if (p != end) {
9267 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
9268 }
Jerry Yu4f9e3ef2022-02-17 14:58:27 +08009269
Gilles Peskine449bd832023-01-11 14:50:10 +01009270 return 0;
Jerry Yu4f9e3ef2022-02-17 14:58:27 +08009271}
Jerry Yudc7bd172022-02-17 13:44:15 +08009272#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
9273
XiaokangQian75d40ef2022-04-20 11:05:24 +00009274int mbedtls_ssl_validate_ciphersuite(
9275 const mbedtls_ssl_context *ssl,
9276 const mbedtls_ssl_ciphersuite_t *suite_info,
9277 mbedtls_ssl_protocol_version min_tls_version,
Gilles Peskine449bd832023-01-11 14:50:10 +01009278 mbedtls_ssl_protocol_version max_tls_version)
XiaokangQian75d40ef2022-04-20 11:05:24 +00009279{
9280 (void) ssl;
9281
Gilles Peskine449bd832023-01-11 14:50:10 +01009282 if (suite_info == NULL) {
9283 return -1;
9284 }
XiaokangQian75d40ef2022-04-20 11:05:24 +00009285
Gilles Peskine449bd832023-01-11 14:50:10 +01009286 if ((suite_info->min_tls_version > max_tls_version) ||
9287 (suite_info->max_tls_version < min_tls_version)) {
9288 return -1;
XiaokangQian75d40ef2022-04-20 11:05:24 +00009289 }
9290
XiaokangQian060d8672022-04-21 09:24:56 +00009291#if defined(MBEDTLS_SSL_PROTO_TLS1_2) && defined(MBEDTLS_SSL_CLI_C)
XiaokangQian75d40ef2022-04-20 11:05:24 +00009292#if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
Neil Armstrongca7d5062022-05-31 14:43:23 +02009293#if defined(MBEDTLS_USE_PSA_CRYPTO)
Gilles Peskine449bd832023-01-11 14:50:10 +01009294 if (suite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECJPAKE &&
9295 ssl->handshake->psa_pake_ctx_is_ok != 1)
Neil Armstrongca7d5062022-05-31 14:43:23 +02009296#else
Gilles Peskine449bd832023-01-11 14:50:10 +01009297 if (suite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECJPAKE &&
9298 mbedtls_ecjpake_check(&ssl->handshake->ecjpake_ctx) != 0)
Neil Armstrongca7d5062022-05-31 14:43:23 +02009299#endif /* MBEDTLS_USE_PSA_CRYPTO */
XiaokangQian75d40ef2022-04-20 11:05:24 +00009300 {
Gilles Peskine449bd832023-01-11 14:50:10 +01009301 return -1;
XiaokangQian75d40ef2022-04-20 11:05:24 +00009302 }
9303#endif
9304
9305 /* Don't suggest PSK-based ciphersuite if no PSK is available. */
9306#if defined(MBEDTLS_KEY_EXCHANGE_SOME_PSK_ENABLED)
Gilles Peskine449bd832023-01-11 14:50:10 +01009307 if (mbedtls_ssl_ciphersuite_uses_psk(suite_info) &&
9308 mbedtls_ssl_conf_has_static_psk(ssl->conf) == 0) {
9309 return -1;
XiaokangQian75d40ef2022-04-20 11:05:24 +00009310 }
9311#endif /* MBEDTLS_KEY_EXCHANGE_SOME_PSK_ENABLED */
9312#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
9313
Gilles Peskine449bd832023-01-11 14:50:10 +01009314 return 0;
XiaokangQian75d40ef2022-04-20 11:05:24 +00009315}
9316
Ronald Crone68ab4f2022-10-05 12:46:29 +02009317#if defined(MBEDTLS_SSL_HANDSHAKE_WITH_CERT_ENABLED)
XiaokangQianeaf36512022-04-24 09:07:44 +00009318/*
9319 * Function for writing a signature algorithm extension.
9320 *
9321 * The `extension_data` field of signature algorithm contains a `SignatureSchemeList`
9322 * value (TLS 1.3 RFC8446):
9323 * enum {
9324 * ....
9325 * ecdsa_secp256r1_sha256( 0x0403 ),
9326 * ecdsa_secp384r1_sha384( 0x0503 ),
9327 * ecdsa_secp521r1_sha512( 0x0603 ),
9328 * ....
9329 * } SignatureScheme;
9330 *
9331 * struct {
9332 * SignatureScheme supported_signature_algorithms<2..2^16-2>;
9333 * } SignatureSchemeList;
9334 *
9335 * The `extension_data` field of signature algorithm contains a `SignatureAndHashAlgorithm`
9336 * value (TLS 1.2 RFC5246):
9337 * enum {
9338 * none(0), md5(1), sha1(2), sha224(3), sha256(4), sha384(5),
9339 * sha512(6), (255)
9340 * } HashAlgorithm;
9341 *
9342 * enum { anonymous(0), rsa(1), dsa(2), ecdsa(3), (255) }
9343 * SignatureAlgorithm;
9344 *
9345 * struct {
9346 * HashAlgorithm hash;
9347 * SignatureAlgorithm signature;
9348 * } SignatureAndHashAlgorithm;
9349 *
9350 * SignatureAndHashAlgorithm
9351 * supported_signature_algorithms<2..2^16-2>;
9352 *
9353 * The TLS 1.3 signature algorithm extension was defined to be a compatible
9354 * generalization of the TLS 1.2 signature algorithm extension.
9355 * `SignatureAndHashAlgorithm` field of TLS 1.2 can be represented by
9356 * `SignatureScheme` field of TLS 1.3
9357 *
9358 */
Gilles Peskine449bd832023-01-11 14:50:10 +01009359int mbedtls_ssl_write_sig_alg_ext(mbedtls_ssl_context *ssl, unsigned char *buf,
9360 const unsigned char *end, size_t *out_len)
XiaokangQianeaf36512022-04-24 09:07:44 +00009361{
9362 unsigned char *p = buf;
9363 unsigned char *supported_sig_alg; /* Start of supported_signature_algorithms */
9364 size_t supported_sig_alg_len = 0; /* Length of supported_signature_algorithms */
9365
9366 *out_len = 0;
9367
Gilles Peskine449bd832023-01-11 14:50:10 +01009368 MBEDTLS_SSL_DEBUG_MSG(3, ("adding signature_algorithms extension"));
XiaokangQianeaf36512022-04-24 09:07:44 +00009369
9370 /* Check if we have space for header and length field:
9371 * - extension_type (2 bytes)
9372 * - extension_data_length (2 bytes)
9373 * - supported_signature_algorithms_length (2 bytes)
9374 */
Gilles Peskine449bd832023-01-11 14:50:10 +01009375 MBEDTLS_SSL_CHK_BUF_PTR(p, end, 6);
XiaokangQianeaf36512022-04-24 09:07:44 +00009376 p += 6;
9377
9378 /*
9379 * Write supported_signature_algorithms
9380 */
9381 supported_sig_alg = p;
Gilles Peskine449bd832023-01-11 14:50:10 +01009382 const uint16_t *sig_alg = mbedtls_ssl_get_sig_algs(ssl);
9383 if (sig_alg == NULL) {
9384 return MBEDTLS_ERR_SSL_BAD_CONFIG;
9385 }
XiaokangQianeaf36512022-04-24 09:07:44 +00009386
Gilles Peskine449bd832023-01-11 14:50:10 +01009387 for (; *sig_alg != MBEDTLS_TLS1_3_SIG_NONE; sig_alg++) {
9388 MBEDTLS_SSL_DEBUG_MSG(3, ("got signature scheme [%x] %s",
9389 *sig_alg,
9390 mbedtls_ssl_sig_alg_to_str(*sig_alg)));
9391 if (!mbedtls_ssl_sig_alg_is_supported(ssl, *sig_alg)) {
XiaokangQianeaf36512022-04-24 09:07:44 +00009392 continue;
Gilles Peskine449bd832023-01-11 14:50:10 +01009393 }
9394 MBEDTLS_SSL_CHK_BUF_PTR(p, end, 2);
9395 MBEDTLS_PUT_UINT16_BE(*sig_alg, p, 0);
XiaokangQianeaf36512022-04-24 09:07:44 +00009396 p += 2;
Gilles Peskine449bd832023-01-11 14:50:10 +01009397 MBEDTLS_SSL_DEBUG_MSG(3, ("sent signature scheme [%x] %s",
9398 *sig_alg,
9399 mbedtls_ssl_sig_alg_to_str(*sig_alg)));
XiaokangQianeaf36512022-04-24 09:07:44 +00009400 }
9401
9402 /* Length of supported_signature_algorithms */
9403 supported_sig_alg_len = p - supported_sig_alg;
Gilles Peskine449bd832023-01-11 14:50:10 +01009404 if (supported_sig_alg_len == 0) {
9405 MBEDTLS_SSL_DEBUG_MSG(1, ("No signature algorithms defined."));
9406 return MBEDTLS_ERR_SSL_INTERNAL_ERROR;
XiaokangQianeaf36512022-04-24 09:07:44 +00009407 }
9408
Gilles Peskine449bd832023-01-11 14:50:10 +01009409 MBEDTLS_PUT_UINT16_BE(MBEDTLS_TLS_EXT_SIG_ALG, buf, 0);
9410 MBEDTLS_PUT_UINT16_BE(supported_sig_alg_len + 2, buf, 2);
9411 MBEDTLS_PUT_UINT16_BE(supported_sig_alg_len, buf, 4);
XiaokangQianeaf36512022-04-24 09:07:44 +00009412
XiaokangQianeaf36512022-04-24 09:07:44 +00009413 *out_len = p - buf;
9414
9415#if defined(MBEDTLS_SSL_PROTO_TLS1_3)
Gilles Peskine449bd832023-01-11 14:50:10 +01009416 mbedtls_ssl_tls13_set_hs_sent_ext_mask(ssl, MBEDTLS_TLS_EXT_SIG_ALG);
XiaokangQianeaf36512022-04-24 09:07:44 +00009417#endif /* MBEDTLS_SSL_PROTO_TLS1_3 */
Jerry Yu0c354a22022-08-29 15:25:36 +08009418
Gilles Peskine449bd832023-01-11 14:50:10 +01009419 return 0;
XiaokangQianeaf36512022-04-24 09:07:44 +00009420}
Ronald Crone68ab4f2022-10-05 12:46:29 +02009421#endif /* MBEDTLS_SSL_HANDSHAKE_WITH_CERT_ENABLED */
XiaokangQianeaf36512022-04-24 09:07:44 +00009422
XiaokangQian40a35232022-05-07 09:02:40 +00009423#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
XiaokangQian9b2b7712022-05-17 02:57:00 +00009424/*
9425 * mbedtls_ssl_parse_server_name_ext
9426 *
9427 * Structure of server_name extension:
9428 *
9429 * enum {
9430 * host_name(0), (255)
9431 * } NameType;
9432 * opaque HostName<1..2^16-1>;
9433 *
9434 * struct {
9435 * NameType name_type;
9436 * select (name_type) {
9437 * case host_name: HostName;
9438 * } name;
9439 * } ServerName;
9440 * struct {
9441 * ServerName server_name_list<1..2^16-1>
9442 * } ServerNameList;
9443 */
Ronald Cronce7d76e2022-07-08 18:56:49 +02009444MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01009445int mbedtls_ssl_parse_server_name_ext(mbedtls_ssl_context *ssl,
9446 const unsigned char *buf,
9447 const unsigned char *end)
XiaokangQian40a35232022-05-07 09:02:40 +00009448{
9449 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
9450 const unsigned char *p = buf;
XiaokangQian9b2b7712022-05-17 02:57:00 +00009451 size_t server_name_list_len, hostname_len;
9452 const unsigned char *server_name_list_end;
XiaokangQian40a35232022-05-07 09:02:40 +00009453
Gilles Peskine449bd832023-01-11 14:50:10 +01009454 MBEDTLS_SSL_DEBUG_MSG(3, ("parse ServerName extension"));
XiaokangQian40a35232022-05-07 09:02:40 +00009455
Gilles Peskine449bd832023-01-11 14:50:10 +01009456 MBEDTLS_SSL_CHK_BUF_READ_PTR(p, end, 2);
9457 server_name_list_len = MBEDTLS_GET_UINT16_BE(p, 0);
XiaokangQian40a35232022-05-07 09:02:40 +00009458 p += 2;
9459
Gilles Peskine449bd832023-01-11 14:50:10 +01009460 MBEDTLS_SSL_CHK_BUF_READ_PTR(p, end, server_name_list_len);
XiaokangQian9b2b7712022-05-17 02:57:00 +00009461 server_name_list_end = p + server_name_list_len;
Gilles Peskine449bd832023-01-11 14:50:10 +01009462 while (p < server_name_list_end) {
9463 MBEDTLS_SSL_CHK_BUF_READ_PTR(p, server_name_list_end, 3);
9464 hostname_len = MBEDTLS_GET_UINT16_BE(p, 1);
9465 MBEDTLS_SSL_CHK_BUF_READ_PTR(p, server_name_list_end,
9466 hostname_len + 3);
XiaokangQian40a35232022-05-07 09:02:40 +00009467
Gilles Peskine449bd832023-01-11 14:50:10 +01009468 if (p[0] == MBEDTLS_TLS_EXT_SERVERNAME_HOSTNAME) {
XiaokangQian75fe8c72022-06-15 09:42:45 +00009469 /* sni_name is intended to be used only during the parsing of the
9470 * ClientHello message (it is reset to NULL before the end of
9471 * the message parsing). Thus it is ok to just point to the
9472 * reception buffer and not make a copy of it.
9473 */
XiaokangQianf2a94202022-05-20 06:44:24 +00009474 ssl->handshake->sni_name = p + 3;
9475 ssl->handshake->sni_name_len = hostname_len;
Gilles Peskine449bd832023-01-11 14:50:10 +01009476 if (ssl->conf->f_sni == NULL) {
9477 return 0;
XiaokangQian40a35232022-05-07 09:02:40 +00009478 }
Gilles Peskine449bd832023-01-11 14:50:10 +01009479 ret = ssl->conf->f_sni(ssl->conf->p_sni,
9480 ssl, p + 3, hostname_len);
9481 if (ret != 0) {
9482 MBEDTLS_SSL_DEBUG_RET(1, "ssl_sni_wrapper", ret);
9483 MBEDTLS_SSL_PEND_FATAL_ALERT(MBEDTLS_SSL_ALERT_MSG_UNRECOGNIZED_NAME,
9484 MBEDTLS_ERR_SSL_UNRECOGNIZED_NAME);
9485 return MBEDTLS_ERR_SSL_UNRECOGNIZED_NAME;
9486 }
9487 return 0;
XiaokangQian40a35232022-05-07 09:02:40 +00009488 }
9489
9490 p += hostname_len + 3;
9491 }
9492
Gilles Peskine449bd832023-01-11 14:50:10 +01009493 return 0;
XiaokangQian40a35232022-05-07 09:02:40 +00009494}
9495#endif /* MBEDTLS_SSL_SERVER_NAME_INDICATION */
9496
XiaokangQianacb39922022-06-17 10:18:48 +00009497#if defined(MBEDTLS_SSL_ALPN)
Ronald Cronce7d76e2022-07-08 18:56:49 +02009498MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01009499int mbedtls_ssl_parse_alpn_ext(mbedtls_ssl_context *ssl,
9500 const unsigned char *buf,
9501 const unsigned char *end)
XiaokangQianacb39922022-06-17 10:18:48 +00009502{
9503 const unsigned char *p = buf;
XiaokangQianc7403452022-06-23 03:24:12 +00009504 size_t protocol_name_list_len;
XiaokangQian95d5f542022-06-24 02:29:26 +00009505 const unsigned char *protocol_name_list;
9506 const unsigned char *protocol_name_list_end;
XiaokangQianc7403452022-06-23 03:24:12 +00009507 size_t protocol_name_len;
XiaokangQianacb39922022-06-17 10:18:48 +00009508
9509 /* If ALPN not configured, just ignore the extension */
Gilles Peskine449bd832023-01-11 14:50:10 +01009510 if (ssl->conf->alpn_list == NULL) {
9511 return 0;
9512 }
XiaokangQianacb39922022-06-17 10:18:48 +00009513
9514 /*
XiaokangQianc7403452022-06-23 03:24:12 +00009515 * RFC7301, section 3.1
9516 * opaque ProtocolName<1..2^8-1>;
XiaokangQianacb39922022-06-17 10:18:48 +00009517 *
XiaokangQianc7403452022-06-23 03:24:12 +00009518 * struct {
9519 * ProtocolName protocol_name_list<2..2^16-1>
9520 * } ProtocolNameList;
XiaokangQianacb39922022-06-17 10:18:48 +00009521 */
9522
XiaokangQianc7403452022-06-23 03:24:12 +00009523 /*
XiaokangQian0b776e22022-06-24 09:04:59 +00009524 * protocol_name_list_len 2 bytes
9525 * protocol_name_len 1 bytes
9526 * protocol_name >=1 byte
XiaokangQianc7403452022-06-23 03:24:12 +00009527 */
Gilles Peskine449bd832023-01-11 14:50:10 +01009528 MBEDTLS_SSL_CHK_BUF_READ_PTR(p, end, 4);
XiaokangQianacb39922022-06-17 10:18:48 +00009529
Gilles Peskine449bd832023-01-11 14:50:10 +01009530 protocol_name_list_len = MBEDTLS_GET_UINT16_BE(p, 0);
XiaokangQianacb39922022-06-17 10:18:48 +00009531 p += 2;
Gilles Peskine449bd832023-01-11 14:50:10 +01009532 MBEDTLS_SSL_CHK_BUF_READ_PTR(p, end, protocol_name_list_len);
XiaokangQian95d5f542022-06-24 02:29:26 +00009533 protocol_name_list = p;
9534 protocol_name_list_end = p + protocol_name_list_len;
XiaokangQianacb39922022-06-17 10:18:48 +00009535
9536 /* Validate peer's list (lengths) */
Gilles Peskine449bd832023-01-11 14:50:10 +01009537 while (p < protocol_name_list_end) {
XiaokangQian95d5f542022-06-24 02:29:26 +00009538 protocol_name_len = *p++;
Gilles Peskine449bd832023-01-11 14:50:10 +01009539 MBEDTLS_SSL_CHK_BUF_READ_PTR(p, protocol_name_list_end,
9540 protocol_name_len);
9541 if (protocol_name_len == 0) {
XiaokangQian95d5f542022-06-24 02:29:26 +00009542 MBEDTLS_SSL_PEND_FATAL_ALERT(
9543 MBEDTLS_SSL_ALERT_MSG_ILLEGAL_PARAMETER,
Gilles Peskine449bd832023-01-11 14:50:10 +01009544 MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER);
9545 return MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER;
XiaokangQian95d5f542022-06-24 02:29:26 +00009546 }
9547
9548 p += protocol_name_len;
XiaokangQianacb39922022-06-17 10:18:48 +00009549 }
9550
9551 /* Use our order of preference */
Gilles Peskine449bd832023-01-11 14:50:10 +01009552 for (const char **alpn = ssl->conf->alpn_list; *alpn != NULL; alpn++) {
9553 size_t const alpn_len = strlen(*alpn);
XiaokangQian95d5f542022-06-24 02:29:26 +00009554 p = protocol_name_list;
Gilles Peskine449bd832023-01-11 14:50:10 +01009555 while (p < protocol_name_list_end) {
XiaokangQian95d5f542022-06-24 02:29:26 +00009556 protocol_name_len = *p++;
Gilles Peskine449bd832023-01-11 14:50:10 +01009557 if (protocol_name_len == alpn_len &&
9558 memcmp(p, *alpn, alpn_len) == 0) {
XiaokangQianacb39922022-06-17 10:18:48 +00009559 ssl->alpn_chosen = *alpn;
Gilles Peskine449bd832023-01-11 14:50:10 +01009560 return 0;
XiaokangQianacb39922022-06-17 10:18:48 +00009561 }
XiaokangQian95d5f542022-06-24 02:29:26 +00009562
9563 p += protocol_name_len;
XiaokangQianacb39922022-06-17 10:18:48 +00009564 }
9565 }
9566
XiaokangQian95d5f542022-06-24 02:29:26 +00009567 /* If we get here, no match was found */
XiaokangQianacb39922022-06-17 10:18:48 +00009568 MBEDTLS_SSL_PEND_FATAL_ALERT(
Gilles Peskine449bd832023-01-11 14:50:10 +01009569 MBEDTLS_SSL_ALERT_MSG_NO_APPLICATION_PROTOCOL,
9570 MBEDTLS_ERR_SSL_NO_APPLICATION_PROTOCOL);
9571 return MBEDTLS_ERR_SSL_NO_APPLICATION_PROTOCOL;
XiaokangQianacb39922022-06-17 10:18:48 +00009572}
9573
Gilles Peskine449bd832023-01-11 14:50:10 +01009574int mbedtls_ssl_write_alpn_ext(mbedtls_ssl_context *ssl,
9575 unsigned char *buf,
9576 unsigned char *end,
9577 size_t *out_len)
XiaokangQianacb39922022-06-17 10:18:48 +00009578{
9579 unsigned char *p = buf;
XiaokangQian95d5f542022-06-24 02:29:26 +00009580 size_t protocol_name_len;
XiaokangQianc7403452022-06-23 03:24:12 +00009581 *out_len = 0;
XiaokangQianacb39922022-06-17 10:18:48 +00009582
Gilles Peskine449bd832023-01-11 14:50:10 +01009583 if (ssl->alpn_chosen == NULL) {
9584 return 0;
XiaokangQianacb39922022-06-17 10:18:48 +00009585 }
9586
Gilles Peskine449bd832023-01-11 14:50:10 +01009587 protocol_name_len = strlen(ssl->alpn_chosen);
9588 MBEDTLS_SSL_CHK_BUF_PTR(p, end, 7 + protocol_name_len);
XiaokangQianacb39922022-06-17 10:18:48 +00009589
Gilles Peskine449bd832023-01-11 14:50:10 +01009590 MBEDTLS_SSL_DEBUG_MSG(3, ("server side, adding alpn extension"));
XiaokangQianacb39922022-06-17 10:18:48 +00009591 /*
9592 * 0 . 1 ext identifier
9593 * 2 . 3 ext length
9594 * 4 . 5 protocol list length
9595 * 6 . 6 protocol name length
9596 * 7 . 7+n protocol name
9597 */
Gilles Peskine449bd832023-01-11 14:50:10 +01009598 MBEDTLS_PUT_UINT16_BE(MBEDTLS_TLS_EXT_ALPN, p, 0);
XiaokangQianacb39922022-06-17 10:18:48 +00009599
XiaokangQian95d5f542022-06-24 02:29:26 +00009600 *out_len = 7 + protocol_name_len;
XiaokangQianacb39922022-06-17 10:18:48 +00009601
Gilles Peskine449bd832023-01-11 14:50:10 +01009602 MBEDTLS_PUT_UINT16_BE(protocol_name_len + 3, p, 2);
9603 MBEDTLS_PUT_UINT16_BE(protocol_name_len + 1, p, 4);
XiaokangQian0b776e22022-06-24 09:04:59 +00009604 /* Note: the length of the chosen protocol has been checked to be less
9605 * than 255 bytes in `mbedtls_ssl_conf_alpn_protocols`.
9606 */
Gilles Peskine449bd832023-01-11 14:50:10 +01009607 p[6] = MBEDTLS_BYTE_0(protocol_name_len);
XiaokangQianacb39922022-06-17 10:18:48 +00009608
Gilles Peskine449bd832023-01-11 14:50:10 +01009609 memcpy(p + 7, ssl->alpn_chosen, protocol_name_len);
Jerry Yub95dd362022-11-08 21:19:34 +08009610
9611#if defined(MBEDTLS_SSL_PROTO_TLS1_3)
Gilles Peskine449bd832023-01-11 14:50:10 +01009612 mbedtls_ssl_tls13_set_hs_sent_ext_mask(ssl, MBEDTLS_TLS_EXT_ALPN);
Jerry Yub95dd362022-11-08 21:19:34 +08009613#endif
9614
Gilles Peskine449bd832023-01-11 14:50:10 +01009615 return 0;
XiaokangQianacb39922022-06-17 10:18:48 +00009616}
9617#endif /* MBEDTLS_SSL_ALPN */
9618
Xiaokang Qiana3b451f2022-10-11 06:20:56 +00009619#if defined(MBEDTLS_SSL_PROTO_TLS1_3) && \
Xiaokang Qian03409292022-10-12 02:49:52 +00009620 defined(MBEDTLS_SSL_SESSION_TICKETS) && \
Xiaokang Qianed0620c2022-10-12 06:58:13 +00009621 defined(MBEDTLS_SSL_SERVER_NAME_INDICATION) && \
Xiaokang Qianed3afcd2022-10-12 08:31:11 +00009622 defined(MBEDTLS_SSL_CLI_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01009623int mbedtls_ssl_session_set_hostname(mbedtls_ssl_session *session,
9624 const char *hostname)
Xiaokang Qiana3b451f2022-10-11 06:20:56 +00009625{
9626 /* Initialize to suppress unnecessary compiler warning */
9627 size_t hostname_len = 0;
9628
9629 /* Check if new hostname is valid before
9630 * making any change to current one */
Gilles Peskine449bd832023-01-11 14:50:10 +01009631 if (hostname != NULL) {
9632 hostname_len = strlen(hostname);
Xiaokang Qiana3b451f2022-10-11 06:20:56 +00009633
Gilles Peskine449bd832023-01-11 14:50:10 +01009634 if (hostname_len > MBEDTLS_SSL_MAX_HOST_NAME_LEN) {
9635 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
9636 }
Xiaokang Qiana3b451f2022-10-11 06:20:56 +00009637 }
9638
9639 /* Now it's clear that we will overwrite the old hostname,
9640 * so we can free it safely */
Gilles Peskine449bd832023-01-11 14:50:10 +01009641 if (session->hostname != NULL) {
9642 mbedtls_platform_zeroize(session->hostname,
9643 strlen(session->hostname));
9644 mbedtls_free(session->hostname);
Xiaokang Qiana3b451f2022-10-11 06:20:56 +00009645 }
9646
9647 /* Passing NULL as hostname shall clear the old one */
Gilles Peskine449bd832023-01-11 14:50:10 +01009648 if (hostname == NULL) {
Xiaokang Qiana3b451f2022-10-11 06:20:56 +00009649 session->hostname = NULL;
Gilles Peskine449bd832023-01-11 14:50:10 +01009650 } else {
9651 session->hostname = mbedtls_calloc(1, hostname_len + 1);
9652 if (session->hostname == NULL) {
9653 return MBEDTLS_ERR_SSL_ALLOC_FAILED;
9654 }
Xiaokang Qiana3b451f2022-10-11 06:20:56 +00009655
Gilles Peskine449bd832023-01-11 14:50:10 +01009656 memcpy(session->hostname, hostname, hostname_len);
Xiaokang Qiana3b451f2022-10-11 06:20:56 +00009657 }
9658
Gilles Peskine449bd832023-01-11 14:50:10 +01009659 return 0;
Xiaokang Qiana3b451f2022-10-11 06:20:56 +00009660}
Xiaokang Qian03409292022-10-12 02:49:52 +00009661#endif /* MBEDTLS_SSL_PROTO_TLS1_3 &&
9662 MBEDTLS_SSL_SESSION_TICKETS &&
Xiaokang Qianed0620c2022-10-12 06:58:13 +00009663 MBEDTLS_SSL_SERVER_NAME_INDICATION &&
Xiaokang Qianed3afcd2022-10-12 08:31:11 +00009664 MBEDTLS_SSL_CLI_C */
Xiaokang Qiana3b451f2022-10-11 06:20:56 +00009665
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009666#endif /* MBEDTLS_SSL_TLS_C */