blob: afcec4671bad7b2ca39ed773e913655bb3f7dbba [file] [log] [blame]
Paul Bakker5121ce52009-01-03 21:22:43 +00001/*
Mateusz Starzyk06b07fb2021-02-18 13:55:21 +01002 * TLS shared functions
Paul Bakker5121ce52009-01-03 21:22:43 +00003 *
Bence Szépkúti1e148272020-08-07 13:07:28 +02004 * Copyright The Mbed TLS Contributors
Manuel Pégourié-Gonnard37ff1402015-09-04 14:21:07 +02005 * SPDX-License-Identifier: Apache-2.0
6 *
7 * Licensed under the Apache License, Version 2.0 (the "License"); you may
8 * not use this file except in compliance with the License.
9 * You may obtain a copy of the License at
10 *
11 * http://www.apache.org/licenses/LICENSE-2.0
12 *
13 * Unless required by applicable law or agreed to in writing, software
14 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
15 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 * See the License for the specific language governing permissions and
17 * limitations under the License.
Paul Bakker5121ce52009-01-03 21:22:43 +000018 */
19/*
Paul Bakker5121ce52009-01-03 21:22:43 +000020 * http://www.ietf.org/rfc/rfc2246.txt
21 * http://www.ietf.org/rfc/rfc4346.txt
22 */
23
Gilles Peskinedb09ef62020-06-03 01:43:33 +020024#include "common.h"
Paul Bakker5121ce52009-01-03 21:22:43 +000025
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020026#if defined(MBEDTLS_SSL_TLS_C)
Paul Bakker5121ce52009-01-03 21:22:43 +000027
Jerry Yub476a442022-01-21 18:14:45 +080028#include <assert.h>
29
SimonBd5800b72016-04-26 07:43:27 +010030#include "mbedtls/platform.h"
SimonBd5800b72016-04-26 07:43:27 +010031
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000032#include "mbedtls/ssl.h"
Ronald Cron9f0fba32022-02-10 16:45:15 +010033#include "ssl_client.h"
Ronald Cron27c85e72022-03-08 11:37:55 +010034#include "ssl_debug_helpers.h"
Chris Jones84a773f2021-03-05 18:38:47 +000035#include "ssl_misc.h"
Andrzej Kurek25f27152022-08-17 16:09:31 -040036
Janos Follath73c616b2019-12-18 15:07:04 +000037#include "mbedtls/debug.h"
38#include "mbedtls/error.h"
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -050039#include "mbedtls/platform_util.h"
Hanno Beckera835da52019-05-16 12:39:07 +010040#include "mbedtls/version.h"
Gabor Mezei765862c2021-10-19 12:22:25 +020041#include "mbedtls/constant_time.h"
Paul Bakker0be444a2013-08-27 21:55:01 +020042
Rich Evans00ab4702015-02-06 13:43:58 +000043#include <string.h>
44
Andrzej Kurekd6db9be2019-01-10 05:27:10 -050045#if defined(MBEDTLS_USE_PSA_CRYPTO)
46#include "mbedtls/psa_util.h"
47#include "psa/crypto.h"
48#endif
Manuel Pégourié-Gonnard07018f92022-09-15 11:29:35 +020049#include "mbedtls/legacy_or_psa.h"
Andrzej Kurekd6db9be2019-01-10 05:27:10 -050050
Janos Follath23bdca02016-10-07 14:47:14 +010051#if defined(MBEDTLS_X509_CRT_PARSE_C)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000052#include "mbedtls/oid.h"
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +020053#endif
54
Ronald Cronad8c17b2022-06-10 17:18:09 +020055#if defined(MBEDTLS_TEST_HOOKS)
56static mbedtls_ssl_chk_buf_ptr_args chk_buf_ptr_fail_args;
57
58void mbedtls_ssl_set_chk_buf_ptr_fail_args(
Gilles Peskine449bd832023-01-11 14:50:10 +010059 const uint8_t *cur, const uint8_t *end, size_t need)
Ronald Cronad8c17b2022-06-10 17:18:09 +020060{
61 chk_buf_ptr_fail_args.cur = cur;
62 chk_buf_ptr_fail_args.end = end;
63 chk_buf_ptr_fail_args.need = need;
64}
65
Gilles Peskine449bd832023-01-11 14:50:10 +010066void mbedtls_ssl_reset_chk_buf_ptr_fail_args(void)
Ronald Cronad8c17b2022-06-10 17:18:09 +020067{
Gilles Peskine449bd832023-01-11 14:50:10 +010068 memset(&chk_buf_ptr_fail_args, 0, sizeof(chk_buf_ptr_fail_args));
Ronald Cronad8c17b2022-06-10 17:18:09 +020069}
70
Gilles Peskine449bd832023-01-11 14:50:10 +010071int mbedtls_ssl_cmp_chk_buf_ptr_fail_args(mbedtls_ssl_chk_buf_ptr_args *args)
Ronald Cronad8c17b2022-06-10 17:18:09 +020072{
Gilles Peskine449bd832023-01-11 14:50:10 +010073 return (chk_buf_ptr_fail_args.cur != args->cur) ||
74 (chk_buf_ptr_fail_args.end != args->end) ||
75 (chk_buf_ptr_fail_args.need != args->need);
Ronald Cronad8c17b2022-06-10 17:18:09 +020076}
77#endif /* MBEDTLS_TEST_HOOKS */
78
Manuel Pégourié-Gonnard286a1362015-05-13 16:22:05 +020079#if defined(MBEDTLS_SSL_PROTO_DTLS)
Hanno Becker2b1e3542018-08-06 11:19:13 +010080
Hanno Beckera0e20d02019-05-15 14:03:01 +010081#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Beckerf8542cf2019-04-09 15:22:03 +010082/* Top-level Connection ID API */
83
Gilles Peskine449bd832023-01-11 14:50:10 +010084int mbedtls_ssl_conf_cid(mbedtls_ssl_config *conf,
85 size_t len,
86 int ignore_other_cid)
Hanno Beckerad4a1372019-05-03 13:06:44 +010087{
Gilles Peskine449bd832023-01-11 14:50:10 +010088 if (len > MBEDTLS_SSL_CID_IN_LEN_MAX) {
89 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
90 }
Hanno Beckerad4a1372019-05-03 13:06:44 +010091
Gilles Peskine449bd832023-01-11 14:50:10 +010092 if (ignore_other_cid != MBEDTLS_SSL_UNEXPECTED_CID_FAIL &&
93 ignore_other_cid != MBEDTLS_SSL_UNEXPECTED_CID_IGNORE) {
94 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
Hanno Becker611ac772019-05-14 11:45:26 +010095 }
96
97 conf->ignore_unexpected_cid = ignore_other_cid;
Hanno Beckerad4a1372019-05-03 13:06:44 +010098 conf->cid_len = len;
Gilles Peskine449bd832023-01-11 14:50:10 +010099 return 0;
Hanno Beckerad4a1372019-05-03 13:06:44 +0100100}
101
Gilles Peskine449bd832023-01-11 14:50:10 +0100102int mbedtls_ssl_set_cid(mbedtls_ssl_context *ssl,
103 int enable,
104 unsigned char const *own_cid,
105 size_t own_cid_len)
Hanno Beckerf8542cf2019-04-09 15:22:03 +0100106{
Gilles Peskine449bd832023-01-11 14:50:10 +0100107 if (ssl->conf->transport != MBEDTLS_SSL_TRANSPORT_DATAGRAM) {
108 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
109 }
Hanno Becker76a79ab2019-05-03 14:38:32 +0100110
Hanno Beckerca092242019-04-25 16:01:49 +0100111 ssl->negotiate_cid = enable;
Gilles Peskine449bd832023-01-11 14:50:10 +0100112 if (enable == MBEDTLS_SSL_CID_DISABLED) {
113 MBEDTLS_SSL_DEBUG_MSG(3, ("Disable use of CID extension."));
114 return 0;
Hanno Beckerca092242019-04-25 16:01:49 +0100115 }
Gilles Peskine449bd832023-01-11 14:50:10 +0100116 MBEDTLS_SSL_DEBUG_MSG(3, ("Enable use of CID extension."));
117 MBEDTLS_SSL_DEBUG_BUF(3, "Own CID", own_cid, own_cid_len);
Hanno Beckerca092242019-04-25 16:01:49 +0100118
Gilles Peskine449bd832023-01-11 14:50:10 +0100119 if (own_cid_len != ssl->conf->cid_len) {
120 MBEDTLS_SSL_DEBUG_MSG(3, ("CID length %u does not match CID length %u in config",
121 (unsigned) own_cid_len,
122 (unsigned) ssl->conf->cid_len));
123 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
Hanno Beckerca092242019-04-25 16:01:49 +0100124 }
125
Gilles Peskine449bd832023-01-11 14:50:10 +0100126 memcpy(ssl->own_cid, own_cid, own_cid_len);
Hanno Beckerb7ee0cf2019-04-30 14:07:31 +0100127 /* Truncation is not an issue here because
128 * MBEDTLS_SSL_CID_IN_LEN_MAX at most 255. */
129 ssl->own_cid_len = (uint8_t) own_cid_len;
Hanno Beckerca092242019-04-25 16:01:49 +0100130
Gilles Peskine449bd832023-01-11 14:50:10 +0100131 return 0;
Hanno Beckerf8542cf2019-04-09 15:22:03 +0100132}
133
Gilles Peskine449bd832023-01-11 14:50:10 +0100134int mbedtls_ssl_get_own_cid(mbedtls_ssl_context *ssl,
135 int *enabled,
136 unsigned char own_cid[MBEDTLS_SSL_CID_OUT_LEN_MAX],
137 size_t *own_cid_len)
Paul Elliott0113cf12022-03-11 20:26:47 +0000138{
139 *enabled = MBEDTLS_SSL_CID_DISABLED;
140
Gilles Peskine449bd832023-01-11 14:50:10 +0100141 if (ssl->conf->transport != MBEDTLS_SSL_TRANSPORT_DATAGRAM) {
142 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
143 }
Paul Elliott0113cf12022-03-11 20:26:47 +0000144
145 /* We report MBEDTLS_SSL_CID_DISABLED in case the CID length is
146 * zero as this is indistinguishable from not requesting to use
147 * the CID extension. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100148 if (ssl->own_cid_len == 0 || ssl->negotiate_cid == MBEDTLS_SSL_CID_DISABLED) {
149 return 0;
150 }
Paul Elliott0113cf12022-03-11 20:26:47 +0000151
Gilles Peskine449bd832023-01-11 14:50:10 +0100152 if (own_cid_len != NULL) {
Paul Elliott0113cf12022-03-11 20:26:47 +0000153 *own_cid_len = ssl->own_cid_len;
Gilles Peskine449bd832023-01-11 14:50:10 +0100154 if (own_cid != NULL) {
155 memcpy(own_cid, ssl->own_cid, ssl->own_cid_len);
156 }
Paul Elliott0113cf12022-03-11 20:26:47 +0000157 }
158
159 *enabled = MBEDTLS_SSL_CID_ENABLED;
160
Gilles Peskine449bd832023-01-11 14:50:10 +0100161 return 0;
Paul Elliott0113cf12022-03-11 20:26:47 +0000162}
163
Gilles Peskine449bd832023-01-11 14:50:10 +0100164int mbedtls_ssl_get_peer_cid(mbedtls_ssl_context *ssl,
165 int *enabled,
166 unsigned char peer_cid[MBEDTLS_SSL_CID_OUT_LEN_MAX],
167 size_t *peer_cid_len)
Hanno Beckerf8542cf2019-04-09 15:22:03 +0100168{
Hanno Beckerf8542cf2019-04-09 15:22:03 +0100169 *enabled = MBEDTLS_SSL_CID_DISABLED;
Hanno Beckerb1f89cd2019-04-26 17:08:02 +0100170
Gilles Peskine449bd832023-01-11 14:50:10 +0100171 if (ssl->conf->transport != MBEDTLS_SSL_TRANSPORT_DATAGRAM ||
172 mbedtls_ssl_is_handshake_over(ssl) == 0) {
173 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
Hanno Becker76a79ab2019-05-03 14:38:32 +0100174 }
Hanno Beckerb1f89cd2019-04-26 17:08:02 +0100175
Hanno Beckerc5f24222019-05-03 12:54:52 +0100176 /* We report MBEDTLS_SSL_CID_DISABLED in case the CID extensions
177 * were used, but client and server requested the empty CID.
178 * This is indistinguishable from not using the CID extension
179 * in the first place. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100180 if (ssl->transform_in->in_cid_len == 0 &&
181 ssl->transform_in->out_cid_len == 0) {
182 return 0;
Hanno Beckerb1f89cd2019-04-26 17:08:02 +0100183 }
184
Gilles Peskine449bd832023-01-11 14:50:10 +0100185 if (peer_cid_len != NULL) {
Hanno Becker615ef172019-05-22 16:50:35 +0100186 *peer_cid_len = ssl->transform_in->out_cid_len;
Gilles Peskine449bd832023-01-11 14:50:10 +0100187 if (peer_cid != NULL) {
188 memcpy(peer_cid, ssl->transform_in->out_cid,
189 ssl->transform_in->out_cid_len);
Hanno Becker615ef172019-05-22 16:50:35 +0100190 }
191 }
Hanno Beckerb1f89cd2019-04-26 17:08:02 +0100192
193 *enabled = MBEDTLS_SSL_CID_ENABLED;
194
Gilles Peskine449bd832023-01-11 14:50:10 +0100195 return 0;
Hanno Beckerf8542cf2019-04-09 15:22:03 +0100196}
Hanno Beckera0e20d02019-05-15 14:03:01 +0100197#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Beckerf8542cf2019-04-09 15:22:03 +0100198
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200199#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +0200200
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200201#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
Manuel Pégourié-Gonnard581e6b62013-07-18 12:32:27 +0200202/*
203 * Convert max_fragment_length codes to length.
204 * RFC 6066 says:
205 * enum{
206 * 2^9(1), 2^10(2), 2^11(3), 2^12(4), (255)
207 * } MaxFragmentLength;
208 * and we add 0 -> extension unused
209 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100210static unsigned int ssl_mfl_code_to_length(int mfl)
Manuel Pégourié-Gonnard581e6b62013-07-18 12:32:27 +0200211{
Gilles Peskine449bd832023-01-11 14:50:10 +0100212 switch (mfl) {
213 case MBEDTLS_SSL_MAX_FRAG_LEN_NONE:
214 return MBEDTLS_TLS_EXT_ADV_CONTENT_LEN;
215 case MBEDTLS_SSL_MAX_FRAG_LEN_512:
216 return 512;
217 case MBEDTLS_SSL_MAX_FRAG_LEN_1024:
218 return 1024;
219 case MBEDTLS_SSL_MAX_FRAG_LEN_2048:
220 return 2048;
221 case MBEDTLS_SSL_MAX_FRAG_LEN_4096:
222 return 4096;
223 default:
224 return MBEDTLS_TLS_EXT_ADV_CONTENT_LEN;
Angus Grattond8213d02016-05-25 20:56:48 +1000225 }
226}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200227#endif /* MBEDTLS_SSL_MAX_FRAGMENT_LENGTH */
Manuel Pégourié-Gonnard581e6b62013-07-18 12:32:27 +0200228
Gilles Peskine449bd832023-01-11 14:50:10 +0100229int mbedtls_ssl_session_copy(mbedtls_ssl_session *dst,
230 const mbedtls_ssl_session *src)
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200231{
Gilles Peskine449bd832023-01-11 14:50:10 +0100232 mbedtls_ssl_session_free(dst);
233 memcpy(dst, src, sizeof(mbedtls_ssl_session));
吴敬辉0b716112021-11-29 10:46:35 +0800234#if defined(MBEDTLS_SSL_SESSION_TICKETS) && defined(MBEDTLS_SSL_CLI_C)
235 dst->ticket = NULL;
Xiaokang Qian87306442022-10-12 09:47:38 +0000236#if defined(MBEDTLS_SSL_PROTO_TLS1_3) && \
237 defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
Xiaokang Qian126bf8e2022-10-13 02:22:40 +0000238 dst->hostname = NULL;
吴敬辉0b716112021-11-29 10:46:35 +0800239#endif
Xiaokang Qian87306442022-10-12 09:47:38 +0000240#endif /* MBEDTLS_SSL_SESSION_TICKETS && MBEDTLS_SSL_CLI_C */
吴敬辉0b716112021-11-29 10:46:35 +0800241
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200242#if defined(MBEDTLS_X509_CRT_PARSE_C)
Hanno Becker6d1986e2019-02-07 12:27:42 +0000243
244#if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
Gilles Peskine449bd832023-01-11 14:50:10 +0100245 if (src->peer_cert != NULL) {
Janos Follath865b3eb2019-12-16 11:46:15 +0000246 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker2292d1f2013-09-15 17:06:49 +0200247
Gilles Peskine449bd832023-01-11 14:50:10 +0100248 dst->peer_cert = mbedtls_calloc(1, sizeof(mbedtls_x509_crt));
249 if (dst->peer_cert == NULL) {
250 return MBEDTLS_ERR_SSL_ALLOC_FAILED;
251 }
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200252
Gilles Peskine449bd832023-01-11 14:50:10 +0100253 mbedtls_x509_crt_init(dst->peer_cert);
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200254
Gilles Peskine449bd832023-01-11 14:50:10 +0100255 if ((ret = mbedtls_x509_crt_parse_der(dst->peer_cert, src->peer_cert->raw.p,
256 src->peer_cert->raw.len)) != 0) {
257 mbedtls_free(dst->peer_cert);
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200258 dst->peer_cert = NULL;
Gilles Peskine449bd832023-01-11 14:50:10 +0100259 return ret;
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200260 }
261 }
Hanno Becker6d1986e2019-02-07 12:27:42 +0000262#else /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
Gilles Peskine449bd832023-01-11 14:50:10 +0100263 if (src->peer_cert_digest != NULL) {
Hanno Becker9198ad12019-02-05 17:00:50 +0000264 dst->peer_cert_digest =
Gilles Peskine449bd832023-01-11 14:50:10 +0100265 mbedtls_calloc(1, src->peer_cert_digest_len);
266 if (dst->peer_cert_digest == NULL) {
267 return MBEDTLS_ERR_SSL_ALLOC_FAILED;
268 }
Hanno Becker9198ad12019-02-05 17:00:50 +0000269
Gilles Peskine449bd832023-01-11 14:50:10 +0100270 memcpy(dst->peer_cert_digest, src->peer_cert_digest,
271 src->peer_cert_digest_len);
Hanno Becker9198ad12019-02-05 17:00:50 +0000272 dst->peer_cert_digest_type = src->peer_cert_digest_type;
Hanno Beckeraccc5992019-02-25 10:06:59 +0000273 dst->peer_cert_digest_len = src->peer_cert_digest_len;
Hanno Becker9198ad12019-02-05 17:00:50 +0000274 }
275#endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
276
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200277#endif /* MBEDTLS_X509_CRT_PARSE_C */
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200278
Manuel Pégourié-Gonnardb596abf2015-05-20 10:45:29 +0200279#if defined(MBEDTLS_SSL_SESSION_TICKETS) && defined(MBEDTLS_SSL_CLI_C)
Gilles Peskine449bd832023-01-11 14:50:10 +0100280 if (src->ticket != NULL) {
281 dst->ticket = mbedtls_calloc(1, src->ticket_len);
282 if (dst->ticket == NULL) {
283 return MBEDTLS_ERR_SSL_ALLOC_FAILED;
284 }
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200285
Gilles Peskine449bd832023-01-11 14:50:10 +0100286 memcpy(dst->ticket, src->ticket, src->ticket_len);
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200287 }
Xiaokang Qian126bf8e2022-10-13 02:22:40 +0000288
289#if defined(MBEDTLS_SSL_PROTO_TLS1_3) && \
290 defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
Gilles Peskine449bd832023-01-11 14:50:10 +0100291 if (src->endpoint == MBEDTLS_SSL_IS_CLIENT) {
Xiaokang Qian126bf8e2022-10-13 02:22:40 +0000292 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Gilles Peskine449bd832023-01-11 14:50:10 +0100293 ret = mbedtls_ssl_session_set_hostname(dst, src->hostname);
294 if (ret != 0) {
295 return ret;
296 }
Xiaokang Qian126bf8e2022-10-13 02:22:40 +0000297 }
298#endif /* MBEDTLS_SSL_PROTO_TLS1_3 &&
299 MBEDTLS_SSL_SERVER_NAME_INDICATION */
Manuel Pégourié-Gonnardb596abf2015-05-20 10:45:29 +0200300#endif /* MBEDTLS_SSL_SESSION_TICKETS && MBEDTLS_SSL_CLI_C */
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200301
Gilles Peskine449bd832023-01-11 14:50:10 +0100302 return 0;
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200303}
304
Andrzej Kurek0afa2a12020-03-03 10:39:58 -0500305#if defined(MBEDTLS_SSL_VARIABLE_BUFFER_LENGTH)
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +0200306MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +0100307static int resize_buffer(unsigned char **buffer, size_t len_new, size_t *len_old)
Andrzej Kurek0afa2a12020-03-03 10:39:58 -0500308{
Gilles Peskine449bd832023-01-11 14:50:10 +0100309 unsigned char *resized_buffer = mbedtls_calloc(1, len_new);
310 if (resized_buffer == NULL) {
Andrzej Kurek0afa2a12020-03-03 10:39:58 -0500311 return -1;
Gilles Peskine449bd832023-01-11 14:50:10 +0100312 }
Andrzej Kurek0afa2a12020-03-03 10:39:58 -0500313
314 /* We want to copy len_new bytes when downsizing the buffer, and
315 * len_old bytes when upsizing, so we choose the smaller of two sizes,
316 * to fit one buffer into another. Size checks, ensuring that no data is
317 * lost, are done outside of this function. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100318 memcpy(resized_buffer, *buffer,
319 (len_new < *len_old) ? len_new : *len_old);
320 mbedtls_platform_zeroize(*buffer, *len_old);
321 mbedtls_free(*buffer);
Andrzej Kurek0afa2a12020-03-03 10:39:58 -0500322
323 *buffer = resized_buffer;
324 *len_old = len_new;
325
326 return 0;
327}
Andrzej Kurek4a063792020-10-21 15:08:44 +0200328
Gilles Peskine449bd832023-01-11 14:50:10 +0100329static void handle_buffer_resizing(mbedtls_ssl_context *ssl, int downsizing,
330 size_t in_buf_new_len,
331 size_t out_buf_new_len)
Andrzej Kurek4a063792020-10-21 15:08:44 +0200332{
333 int modified = 0;
334 size_t written_in = 0, iv_offset_in = 0, len_offset_in = 0;
335 size_t written_out = 0, iv_offset_out = 0, len_offset_out = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +0100336 if (ssl->in_buf != NULL) {
Andrzej Kurek4a063792020-10-21 15:08:44 +0200337 written_in = ssl->in_msg - ssl->in_buf;
338 iv_offset_in = ssl->in_iv - ssl->in_buf;
339 len_offset_in = ssl->in_len - ssl->in_buf;
Gilles Peskine449bd832023-01-11 14:50:10 +0100340 if (downsizing ?
Andrzej Kurek4a063792020-10-21 15:08:44 +0200341 ssl->in_buf_len > in_buf_new_len && ssl->in_left < in_buf_new_len :
Gilles Peskine449bd832023-01-11 14:50:10 +0100342 ssl->in_buf_len < in_buf_new_len) {
343 if (resize_buffer(&ssl->in_buf, in_buf_new_len, &ssl->in_buf_len) != 0) {
344 MBEDTLS_SSL_DEBUG_MSG(1, ("input buffer resizing failed - out of memory"));
345 } else {
346 MBEDTLS_SSL_DEBUG_MSG(2, ("Reallocating in_buf to %" MBEDTLS_PRINTF_SIZET,
347 in_buf_new_len));
Andrzej Kurek4a063792020-10-21 15:08:44 +0200348 modified = 1;
349 }
350 }
351 }
352
Gilles Peskine449bd832023-01-11 14:50:10 +0100353 if (ssl->out_buf != NULL) {
Andrzej Kurek4a063792020-10-21 15:08:44 +0200354 written_out = ssl->out_msg - ssl->out_buf;
355 iv_offset_out = ssl->out_iv - ssl->out_buf;
356 len_offset_out = ssl->out_len - ssl->out_buf;
Gilles Peskine449bd832023-01-11 14:50:10 +0100357 if (downsizing ?
Andrzej Kurek4a063792020-10-21 15:08:44 +0200358 ssl->out_buf_len > out_buf_new_len && ssl->out_left < out_buf_new_len :
Gilles Peskine449bd832023-01-11 14:50:10 +0100359 ssl->out_buf_len < out_buf_new_len) {
360 if (resize_buffer(&ssl->out_buf, out_buf_new_len, &ssl->out_buf_len) != 0) {
361 MBEDTLS_SSL_DEBUG_MSG(1, ("output buffer resizing failed - out of memory"));
362 } else {
363 MBEDTLS_SSL_DEBUG_MSG(2, ("Reallocating out_buf to %" MBEDTLS_PRINTF_SIZET,
364 out_buf_new_len));
Andrzej Kurek4a063792020-10-21 15:08:44 +0200365 modified = 1;
366 }
367 }
368 }
Gilles Peskine449bd832023-01-11 14:50:10 +0100369 if (modified) {
Andrzej Kurek4a063792020-10-21 15:08:44 +0200370 /* Update pointers here to avoid doing it twice. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100371 mbedtls_ssl_reset_in_out_pointers(ssl);
Andrzej Kurek4a063792020-10-21 15:08:44 +0200372 /* Fields below might not be properly updated with record
373 * splitting or with CID, so they are manually updated here. */
374 ssl->out_msg = ssl->out_buf + written_out;
375 ssl->out_len = ssl->out_buf + len_offset_out;
376 ssl->out_iv = ssl->out_buf + iv_offset_out;
377
378 ssl->in_msg = ssl->in_buf + written_in;
379 ssl->in_len = ssl->in_buf + len_offset_in;
380 ssl->in_iv = ssl->in_buf + iv_offset_in;
381 }
382}
Andrzej Kurek0afa2a12020-03-03 10:39:58 -0500383#endif /* MBEDTLS_SSL_VARIABLE_BUFFER_LENGTH */
384
Jerry Yudb8c48a2022-01-27 14:54:54 +0800385#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
Jerry Yued14c932022-02-17 13:40:45 +0800386
387#if defined(MBEDTLS_SSL_CONTEXT_SERIALIZATION)
Gilles Peskine449bd832023-01-11 14:50:10 +0100388typedef int (*tls_prf_fn)(const unsigned char *secret, size_t slen,
389 const char *label,
390 const unsigned char *random, size_t rlen,
391 unsigned char *dstbuf, size_t dlen);
Jerry Yued14c932022-02-17 13:40:45 +0800392
Gilles Peskine449bd832023-01-11 14:50:10 +0100393static tls_prf_fn ssl_tls12prf_from_cs(int ciphersuite_id);
Jerry Yued14c932022-02-17 13:40:45 +0800394
395#endif /* MBEDTLS_SSL_CONTEXT_SERIALIZATION */
396
397/* Type for the TLS PRF */
398typedef int ssl_tls_prf_t(const unsigned char *, size_t, const char *,
399 const unsigned char *, size_t,
400 unsigned char *, size_t);
401
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +0200402MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +0100403static int ssl_tls12_populate_transform(mbedtls_ssl_transform *transform,
404 int ciphersuite,
405 const unsigned char master[48],
Neil Armstrongf2c82f02022-04-05 11:16:53 +0200406#if defined(MBEDTLS_SSL_SOME_SUITES_USE_CBC_ETM)
Gilles Peskine449bd832023-01-11 14:50:10 +0100407 int encrypt_then_mac,
Neil Armstrongf2c82f02022-04-05 11:16:53 +0200408#endif /* MBEDTLS_SSL_SOME_SUITES_USE_CBC_ETM */
Gilles Peskine449bd832023-01-11 14:50:10 +0100409 ssl_tls_prf_t tls_prf,
410 const unsigned char randbytes[64],
411 mbedtls_ssl_protocol_version tls_version,
412 unsigned endpoint,
413 const mbedtls_ssl_context *ssl);
Jerry Yued14c932022-02-17 13:40:45 +0800414
Andrzej Kurek25f27152022-08-17 16:09:31 -0400415#if defined(MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +0200416MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +0100417static int tls_prf_sha256(const unsigned char *secret, size_t slen,
Ron Eldor51d3ab52019-05-12 14:54:30 +0300418 const char *label,
419 const unsigned char *random, size_t rlen,
Gilles Peskine449bd832023-01-11 14:50:10 +0100420 unsigned char *dstbuf, size_t dlen);
Manuel Pégourié-Gonnard226aa152023-02-05 09:46:59 +0100421static int ssl_calc_verify_tls_sha256(const mbedtls_ssl_context *, unsigned char *, size_t *);
422static int ssl_calc_finished_tls_sha256(mbedtls_ssl_context *, unsigned char *, int);
Gilles Peskine449bd832023-01-11 14:50:10 +0100423
424#endif /* MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA*/
425
426#if defined(MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
427MBEDTLS_CHECK_RETURN_CRITICAL
428static int tls_prf_sha384(const unsigned char *secret, size_t slen,
429 const char *label,
430 const unsigned char *random, size_t rlen,
431 unsigned char *dstbuf, size_t dlen);
432
Manuel Pégourié-Gonnard226aa152023-02-05 09:46:59 +0100433static int ssl_calc_verify_tls_sha384(const mbedtls_ssl_context *, unsigned char *, size_t *);
434static int ssl_calc_finished_tls_sha384(mbedtls_ssl_context *, unsigned char *, int);
Gilles Peskine449bd832023-01-11 14:50:10 +0100435#endif /* MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA*/
436
437static size_t ssl_tls12_session_save(const mbedtls_ssl_session *session,
438 unsigned char *buf,
439 size_t buf_len);
440
441MBEDTLS_CHECK_RETURN_CRITICAL
442static int ssl_tls12_session_load(mbedtls_ssl_session *session,
443 const unsigned char *buf,
444 size_t len);
445#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
446
Manuel Pégourié-Gonnard226aa152023-02-05 09:46:59 +0100447static int ssl_update_checksum_start(mbedtls_ssl_context *, const unsigned char *, size_t);
Gilles Peskine449bd832023-01-11 14:50:10 +0100448
449#if defined(MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Manuel Pégourié-Gonnard226aa152023-02-05 09:46:59 +0100450static int ssl_update_checksum_sha256(mbedtls_ssl_context *, const unsigned char *, size_t);
Gilles Peskine449bd832023-01-11 14:50:10 +0100451#endif /* MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA*/
452
453#if defined(MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Manuel Pégourié-Gonnard226aa152023-02-05 09:46:59 +0100454static int ssl_update_checksum_sha384(mbedtls_ssl_context *, const unsigned char *, size_t);
Gilles Peskine449bd832023-01-11 14:50:10 +0100455#endif /* MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA*/
456
457int mbedtls_ssl_tls_prf(const mbedtls_tls_prf_types prf,
458 const unsigned char *secret, size_t slen,
459 const char *label,
460 const unsigned char *random, size_t rlen,
461 unsigned char *dstbuf, size_t dlen)
Ron Eldor51d3ab52019-05-12 14:54:30 +0300462{
463 mbedtls_ssl_tls_prf_cb *tls_prf = NULL;
464
Gilles Peskine449bd832023-01-11 14:50:10 +0100465 switch (prf) {
Ron Eldord2f25f72019-05-15 14:54:22 +0300466#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
Andrzej Kurek25f27152022-08-17 16:09:31 -0400467#if defined(MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Ron Eldor51d3ab52019-05-12 14:54:30 +0300468 case MBEDTLS_SSL_TLS_PRF_SHA384:
469 tls_prf = tls_prf_sha384;
Gilles Peskine449bd832023-01-11 14:50:10 +0100470 break;
Andrzej Kurekcccb0442022-08-19 03:42:11 -0400471#endif /* MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA*/
Andrzej Kurek25f27152022-08-17 16:09:31 -0400472#if defined(MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Ron Eldor51d3ab52019-05-12 14:54:30 +0300473 case MBEDTLS_SSL_TLS_PRF_SHA256:
474 tls_prf = tls_prf_sha256;
Gilles Peskine449bd832023-01-11 14:50:10 +0100475 break;
Andrzej Kurekcccb0442022-08-19 03:42:11 -0400476#endif /* MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA*/
Ron Eldord2f25f72019-05-15 14:54:22 +0300477#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100478 default:
479 return MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
Ron Eldor51d3ab52019-05-12 14:54:30 +0300480 }
481
Gilles Peskine449bd832023-01-11 14:50:10 +0100482 return tls_prf(secret, slen, label, random, rlen, dstbuf, dlen);
Ron Eldor51d3ab52019-05-12 14:54:30 +0300483}
484
Jerry Yuc73c6182022-02-08 20:29:25 +0800485#if defined(MBEDTLS_X509_CRT_PARSE_C)
Gilles Peskine449bd832023-01-11 14:50:10 +0100486static void ssl_clear_peer_cert(mbedtls_ssl_session *session)
Jerry Yuc73c6182022-02-08 20:29:25 +0800487{
488#if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
Gilles Peskine449bd832023-01-11 14:50:10 +0100489 if (session->peer_cert != NULL) {
490 mbedtls_x509_crt_free(session->peer_cert);
491 mbedtls_free(session->peer_cert);
Jerry Yuc73c6182022-02-08 20:29:25 +0800492 session->peer_cert = NULL;
493 }
494#else /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
Gilles Peskine449bd832023-01-11 14:50:10 +0100495 if (session->peer_cert_digest != NULL) {
Jerry Yuc73c6182022-02-08 20:29:25 +0800496 /* Zeroization is not necessary. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100497 mbedtls_free(session->peer_cert_digest);
Jerry Yuc73c6182022-02-08 20:29:25 +0800498 session->peer_cert_digest = NULL;
499 session->peer_cert_digest_type = MBEDTLS_MD_NONE;
500 session->peer_cert_digest_len = 0;
501 }
502#endif /* !MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
503}
504#endif /* MBEDTLS_X509_CRT_PARSE_C */
505
Gilles Peskine449bd832023-01-11 14:50:10 +0100506uint32_t mbedtls_ssl_get_extension_id(unsigned int extension_type)
Jerry Yu7a485c12022-10-31 13:08:18 +0800507{
Gilles Peskine449bd832023-01-11 14:50:10 +0100508 switch (extension_type) {
Jerry Yu7a485c12022-10-31 13:08:18 +0800509 case MBEDTLS_TLS_EXT_SERVERNAME:
Gilles Peskine449bd832023-01-11 14:50:10 +0100510 return MBEDTLS_SSL_EXT_ID_SERVERNAME;
Jerry Yu7a485c12022-10-31 13:08:18 +0800511
512 case MBEDTLS_TLS_EXT_MAX_FRAGMENT_LENGTH:
Gilles Peskine449bd832023-01-11 14:50:10 +0100513 return MBEDTLS_SSL_EXT_ID_MAX_FRAGMENT_LENGTH;
Jerry Yu7a485c12022-10-31 13:08:18 +0800514
515 case MBEDTLS_TLS_EXT_STATUS_REQUEST:
Gilles Peskine449bd832023-01-11 14:50:10 +0100516 return MBEDTLS_SSL_EXT_ID_STATUS_REQUEST;
Jerry Yu7a485c12022-10-31 13:08:18 +0800517
518 case MBEDTLS_TLS_EXT_SUPPORTED_GROUPS:
Gilles Peskine449bd832023-01-11 14:50:10 +0100519 return MBEDTLS_SSL_EXT_ID_SUPPORTED_GROUPS;
Jerry Yu7a485c12022-10-31 13:08:18 +0800520
521 case MBEDTLS_TLS_EXT_SIG_ALG:
Gilles Peskine449bd832023-01-11 14:50:10 +0100522 return MBEDTLS_SSL_EXT_ID_SIG_ALG;
Jerry Yu7a485c12022-10-31 13:08:18 +0800523
524 case MBEDTLS_TLS_EXT_USE_SRTP:
Gilles Peskine449bd832023-01-11 14:50:10 +0100525 return MBEDTLS_SSL_EXT_ID_USE_SRTP;
Jerry Yu7a485c12022-10-31 13:08:18 +0800526
527 case MBEDTLS_TLS_EXT_HEARTBEAT:
Gilles Peskine449bd832023-01-11 14:50:10 +0100528 return MBEDTLS_SSL_EXT_ID_HEARTBEAT;
Jerry Yu7a485c12022-10-31 13:08:18 +0800529
530 case MBEDTLS_TLS_EXT_ALPN:
Gilles Peskine449bd832023-01-11 14:50:10 +0100531 return MBEDTLS_SSL_EXT_ID_ALPN;
Jerry Yu7a485c12022-10-31 13:08:18 +0800532
533 case MBEDTLS_TLS_EXT_SCT:
Gilles Peskine449bd832023-01-11 14:50:10 +0100534 return MBEDTLS_SSL_EXT_ID_SCT;
Jerry Yu7a485c12022-10-31 13:08:18 +0800535
536 case MBEDTLS_TLS_EXT_CLI_CERT_TYPE:
Gilles Peskine449bd832023-01-11 14:50:10 +0100537 return MBEDTLS_SSL_EXT_ID_CLI_CERT_TYPE;
Jerry Yu7a485c12022-10-31 13:08:18 +0800538
539 case MBEDTLS_TLS_EXT_SERV_CERT_TYPE:
Gilles Peskine449bd832023-01-11 14:50:10 +0100540 return MBEDTLS_SSL_EXT_ID_SERV_CERT_TYPE;
Jerry Yu7a485c12022-10-31 13:08:18 +0800541
542 case MBEDTLS_TLS_EXT_PADDING:
Gilles Peskine449bd832023-01-11 14:50:10 +0100543 return MBEDTLS_SSL_EXT_ID_PADDING;
Jerry Yu7a485c12022-10-31 13:08:18 +0800544
545 case MBEDTLS_TLS_EXT_PRE_SHARED_KEY:
Gilles Peskine449bd832023-01-11 14:50:10 +0100546 return MBEDTLS_SSL_EXT_ID_PRE_SHARED_KEY;
Jerry Yu7a485c12022-10-31 13:08:18 +0800547
548 case MBEDTLS_TLS_EXT_EARLY_DATA:
Gilles Peskine449bd832023-01-11 14:50:10 +0100549 return MBEDTLS_SSL_EXT_ID_EARLY_DATA;
Jerry Yu7a485c12022-10-31 13:08:18 +0800550
551 case MBEDTLS_TLS_EXT_SUPPORTED_VERSIONS:
Gilles Peskine449bd832023-01-11 14:50:10 +0100552 return MBEDTLS_SSL_EXT_ID_SUPPORTED_VERSIONS;
Jerry Yu7a485c12022-10-31 13:08:18 +0800553
554 case MBEDTLS_TLS_EXT_COOKIE:
Gilles Peskine449bd832023-01-11 14:50:10 +0100555 return MBEDTLS_SSL_EXT_ID_COOKIE;
Jerry Yu7a485c12022-10-31 13:08:18 +0800556
557 case MBEDTLS_TLS_EXT_PSK_KEY_EXCHANGE_MODES:
Gilles Peskine449bd832023-01-11 14:50:10 +0100558 return MBEDTLS_SSL_EXT_ID_PSK_KEY_EXCHANGE_MODES;
Jerry Yu7a485c12022-10-31 13:08:18 +0800559
560 case MBEDTLS_TLS_EXT_CERT_AUTH:
Gilles Peskine449bd832023-01-11 14:50:10 +0100561 return MBEDTLS_SSL_EXT_ID_CERT_AUTH;
Jerry Yu7a485c12022-10-31 13:08:18 +0800562
563 case MBEDTLS_TLS_EXT_OID_FILTERS:
Gilles Peskine449bd832023-01-11 14:50:10 +0100564 return MBEDTLS_SSL_EXT_ID_OID_FILTERS;
Jerry Yu7a485c12022-10-31 13:08:18 +0800565
566 case MBEDTLS_TLS_EXT_POST_HANDSHAKE_AUTH:
Gilles Peskine449bd832023-01-11 14:50:10 +0100567 return MBEDTLS_SSL_EXT_ID_POST_HANDSHAKE_AUTH;
Jerry Yu7a485c12022-10-31 13:08:18 +0800568
569 case MBEDTLS_TLS_EXT_SIG_ALG_CERT:
Gilles Peskine449bd832023-01-11 14:50:10 +0100570 return MBEDTLS_SSL_EXT_ID_SIG_ALG_CERT;
Jerry Yu7a485c12022-10-31 13:08:18 +0800571
572 case MBEDTLS_TLS_EXT_KEY_SHARE:
Gilles Peskine449bd832023-01-11 14:50:10 +0100573 return MBEDTLS_SSL_EXT_ID_KEY_SHARE;
Jerry Yu7a485c12022-10-31 13:08:18 +0800574
575 case MBEDTLS_TLS_EXT_TRUNCATED_HMAC:
Gilles Peskine449bd832023-01-11 14:50:10 +0100576 return MBEDTLS_SSL_EXT_ID_TRUNCATED_HMAC;
Jerry Yu7a485c12022-10-31 13:08:18 +0800577
578 case MBEDTLS_TLS_EXT_SUPPORTED_POINT_FORMATS:
Gilles Peskine449bd832023-01-11 14:50:10 +0100579 return MBEDTLS_SSL_EXT_ID_SUPPORTED_POINT_FORMATS;
Jerry Yu7a485c12022-10-31 13:08:18 +0800580
581 case MBEDTLS_TLS_EXT_ENCRYPT_THEN_MAC:
Gilles Peskine449bd832023-01-11 14:50:10 +0100582 return MBEDTLS_SSL_EXT_ID_ENCRYPT_THEN_MAC;
Jerry Yu7a485c12022-10-31 13:08:18 +0800583
584 case MBEDTLS_TLS_EXT_EXTENDED_MASTER_SECRET:
Gilles Peskine449bd832023-01-11 14:50:10 +0100585 return MBEDTLS_SSL_EXT_ID_EXTENDED_MASTER_SECRET;
Jerry Yu7a485c12022-10-31 13:08:18 +0800586
587 case MBEDTLS_TLS_EXT_SESSION_TICKET:
Gilles Peskine449bd832023-01-11 14:50:10 +0100588 return MBEDTLS_SSL_EXT_ID_SESSION_TICKET;
Jerry Yu7a485c12022-10-31 13:08:18 +0800589
590 }
591
Gilles Peskine449bd832023-01-11 14:50:10 +0100592 return MBEDTLS_SSL_EXT_ID_UNRECOGNIZED;
Jerry Yu7a485c12022-10-31 13:08:18 +0800593}
594
Gilles Peskine449bd832023-01-11 14:50:10 +0100595uint32_t mbedtls_ssl_get_extension_mask(unsigned int extension_type)
Jerry Yu7a485c12022-10-31 13:08:18 +0800596{
Gilles Peskine449bd832023-01-11 14:50:10 +0100597 return 1 << mbedtls_ssl_get_extension_id(extension_type);
Jerry Yu7a485c12022-10-31 13:08:18 +0800598}
599
Jerry Yud25cab02022-10-31 12:48:30 +0800600#if defined(MBEDTLS_DEBUG_C)
601static const char *extension_name_table[] = {
Jerry Yuea52ed92022-11-08 21:01:17 +0800602 [MBEDTLS_SSL_EXT_ID_UNRECOGNIZED] = "unrecognized",
Jerry Yud25cab02022-10-31 12:48:30 +0800603 [MBEDTLS_SSL_EXT_ID_SERVERNAME] = "server_name",
604 [MBEDTLS_SSL_EXT_ID_MAX_FRAGMENT_LENGTH] = "max_fragment_length",
605 [MBEDTLS_SSL_EXT_ID_STATUS_REQUEST] = "status_request",
606 [MBEDTLS_SSL_EXT_ID_SUPPORTED_GROUPS] = "supported_groups",
607 [MBEDTLS_SSL_EXT_ID_SIG_ALG] = "signature_algorithms",
608 [MBEDTLS_SSL_EXT_ID_USE_SRTP] = "use_srtp",
609 [MBEDTLS_SSL_EXT_ID_HEARTBEAT] = "heartbeat",
610 [MBEDTLS_SSL_EXT_ID_ALPN] = "application_layer_protocol_negotiation",
611 [MBEDTLS_SSL_EXT_ID_SCT] = "signed_certificate_timestamp",
612 [MBEDTLS_SSL_EXT_ID_CLI_CERT_TYPE] = "client_certificate_type",
613 [MBEDTLS_SSL_EXT_ID_SERV_CERT_TYPE] = "server_certificate_type",
614 [MBEDTLS_SSL_EXT_ID_PADDING] = "padding",
615 [MBEDTLS_SSL_EXT_ID_PRE_SHARED_KEY] = "pre_shared_key",
616 [MBEDTLS_SSL_EXT_ID_EARLY_DATA] = "early_data",
617 [MBEDTLS_SSL_EXT_ID_SUPPORTED_VERSIONS] = "supported_versions",
618 [MBEDTLS_SSL_EXT_ID_COOKIE] = "cookie",
619 [MBEDTLS_SSL_EXT_ID_PSK_KEY_EXCHANGE_MODES] = "psk_key_exchange_modes",
620 [MBEDTLS_SSL_EXT_ID_CERT_AUTH] = "certificate_authorities",
621 [MBEDTLS_SSL_EXT_ID_OID_FILTERS] = "oid_filters",
622 [MBEDTLS_SSL_EXT_ID_POST_HANDSHAKE_AUTH] = "post_handshake_auth",
623 [MBEDTLS_SSL_EXT_ID_SIG_ALG_CERT] = "signature_algorithms_cert",
624 [MBEDTLS_SSL_EXT_ID_KEY_SHARE] = "key_share",
625 [MBEDTLS_SSL_EXT_ID_TRUNCATED_HMAC] = "truncated_hmac",
626 [MBEDTLS_SSL_EXT_ID_SUPPORTED_POINT_FORMATS] = "supported_point_formats",
627 [MBEDTLS_SSL_EXT_ID_ENCRYPT_THEN_MAC] = "encrypt_then_mac",
628 [MBEDTLS_SSL_EXT_ID_EXTENDED_MASTER_SECRET] = "extended_master_secret",
629 [MBEDTLS_SSL_EXT_ID_SESSION_TICKET] = "session_ticket"
630};
631
Gilles Peskine449bd832023-01-11 14:50:10 +0100632static unsigned int extension_type_table[] = {
Jerry Yud25cab02022-10-31 12:48:30 +0800633 [MBEDTLS_SSL_EXT_ID_UNRECOGNIZED] = 0xff,
634 [MBEDTLS_SSL_EXT_ID_SERVERNAME] = MBEDTLS_TLS_EXT_SERVERNAME,
635 [MBEDTLS_SSL_EXT_ID_MAX_FRAGMENT_LENGTH] = MBEDTLS_TLS_EXT_MAX_FRAGMENT_LENGTH,
636 [MBEDTLS_SSL_EXT_ID_STATUS_REQUEST] = MBEDTLS_TLS_EXT_STATUS_REQUEST,
637 [MBEDTLS_SSL_EXT_ID_SUPPORTED_GROUPS] = MBEDTLS_TLS_EXT_SUPPORTED_GROUPS,
638 [MBEDTLS_SSL_EXT_ID_SIG_ALG] = MBEDTLS_TLS_EXT_SIG_ALG,
639 [MBEDTLS_SSL_EXT_ID_USE_SRTP] = MBEDTLS_TLS_EXT_USE_SRTP,
640 [MBEDTLS_SSL_EXT_ID_HEARTBEAT] = MBEDTLS_TLS_EXT_HEARTBEAT,
641 [MBEDTLS_SSL_EXT_ID_ALPN] = MBEDTLS_TLS_EXT_ALPN,
642 [MBEDTLS_SSL_EXT_ID_SCT] = MBEDTLS_TLS_EXT_SCT,
643 [MBEDTLS_SSL_EXT_ID_CLI_CERT_TYPE] = MBEDTLS_TLS_EXT_CLI_CERT_TYPE,
644 [MBEDTLS_SSL_EXT_ID_SERV_CERT_TYPE] = MBEDTLS_TLS_EXT_SERV_CERT_TYPE,
645 [MBEDTLS_SSL_EXT_ID_PADDING] = MBEDTLS_TLS_EXT_PADDING,
646 [MBEDTLS_SSL_EXT_ID_PRE_SHARED_KEY] = MBEDTLS_TLS_EXT_PRE_SHARED_KEY,
647 [MBEDTLS_SSL_EXT_ID_EARLY_DATA] = MBEDTLS_TLS_EXT_EARLY_DATA,
648 [MBEDTLS_SSL_EXT_ID_SUPPORTED_VERSIONS] = MBEDTLS_TLS_EXT_SUPPORTED_VERSIONS,
649 [MBEDTLS_SSL_EXT_ID_COOKIE] = MBEDTLS_TLS_EXT_COOKIE,
650 [MBEDTLS_SSL_EXT_ID_PSK_KEY_EXCHANGE_MODES] = MBEDTLS_TLS_EXT_PSK_KEY_EXCHANGE_MODES,
651 [MBEDTLS_SSL_EXT_ID_CERT_AUTH] = MBEDTLS_TLS_EXT_CERT_AUTH,
652 [MBEDTLS_SSL_EXT_ID_OID_FILTERS] = MBEDTLS_TLS_EXT_OID_FILTERS,
653 [MBEDTLS_SSL_EXT_ID_POST_HANDSHAKE_AUTH] = MBEDTLS_TLS_EXT_POST_HANDSHAKE_AUTH,
654 [MBEDTLS_SSL_EXT_ID_SIG_ALG_CERT] = MBEDTLS_TLS_EXT_SIG_ALG_CERT,
655 [MBEDTLS_SSL_EXT_ID_KEY_SHARE] = MBEDTLS_TLS_EXT_KEY_SHARE,
656 [MBEDTLS_SSL_EXT_ID_TRUNCATED_HMAC] = MBEDTLS_TLS_EXT_TRUNCATED_HMAC,
657 [MBEDTLS_SSL_EXT_ID_SUPPORTED_POINT_FORMATS] = MBEDTLS_TLS_EXT_SUPPORTED_POINT_FORMATS,
658 [MBEDTLS_SSL_EXT_ID_ENCRYPT_THEN_MAC] = MBEDTLS_TLS_EXT_ENCRYPT_THEN_MAC,
659 [MBEDTLS_SSL_EXT_ID_EXTENDED_MASTER_SECRET] = MBEDTLS_TLS_EXT_EXTENDED_MASTER_SECRET,
660 [MBEDTLS_SSL_EXT_ID_SESSION_TICKET] = MBEDTLS_TLS_EXT_SESSION_TICKET
661};
662
Gilles Peskine449bd832023-01-11 14:50:10 +0100663const char *mbedtls_ssl_get_extension_name(unsigned int extension_type)
Jerry Yud25cab02022-10-31 12:48:30 +0800664{
Gilles Peskine449bd832023-01-11 14:50:10 +0100665 return extension_name_table[
666 mbedtls_ssl_get_extension_id(extension_type)];
Jerry Yud25cab02022-10-31 12:48:30 +0800667}
668
Gilles Peskine449bd832023-01-11 14:50:10 +0100669static const char *ssl_tls13_get_hs_msg_name(int hs_msg_type)
Jerry Yud25cab02022-10-31 12:48:30 +0800670{
Gilles Peskine449bd832023-01-11 14:50:10 +0100671 switch (hs_msg_type) {
Jerry Yud25cab02022-10-31 12:48:30 +0800672 case MBEDTLS_SSL_HS_CLIENT_HELLO:
Gilles Peskine449bd832023-01-11 14:50:10 +0100673 return "ClientHello";
Jerry Yud25cab02022-10-31 12:48:30 +0800674 case MBEDTLS_SSL_HS_SERVER_HELLO:
Gilles Peskine449bd832023-01-11 14:50:10 +0100675 return "ServerHello";
Jerry Yud25cab02022-10-31 12:48:30 +0800676 case MBEDTLS_SSL_TLS1_3_HS_HELLO_RETRY_REQUEST:
Gilles Peskine449bd832023-01-11 14:50:10 +0100677 return "HelloRetryRequest";
Jerry Yud25cab02022-10-31 12:48:30 +0800678 case MBEDTLS_SSL_HS_NEW_SESSION_TICKET:
Gilles Peskine449bd832023-01-11 14:50:10 +0100679 return "NewSessionTicket";
Jerry Yud25cab02022-10-31 12:48:30 +0800680 case MBEDTLS_SSL_HS_ENCRYPTED_EXTENSIONS:
Gilles Peskine449bd832023-01-11 14:50:10 +0100681 return "EncryptedExtensions";
Jerry Yud25cab02022-10-31 12:48:30 +0800682 case MBEDTLS_SSL_HS_CERTIFICATE:
Gilles Peskine449bd832023-01-11 14:50:10 +0100683 return "Certificate";
Jerry Yud25cab02022-10-31 12:48:30 +0800684 case MBEDTLS_SSL_HS_CERTIFICATE_REQUEST:
Gilles Peskine449bd832023-01-11 14:50:10 +0100685 return "CertificateRequest";
Jerry Yud25cab02022-10-31 12:48:30 +0800686 }
Gilles Peskine449bd832023-01-11 14:50:10 +0100687 return "Unknown";
Jerry Yud25cab02022-10-31 12:48:30 +0800688}
689
Gilles Peskine449bd832023-01-11 14:50:10 +0100690void mbedtls_ssl_print_extension(const mbedtls_ssl_context *ssl,
691 int level, const char *file, int line,
692 int hs_msg_type, unsigned int extension_type,
693 const char *extra_msg0, const char *extra_msg1)
Jerry Yud25cab02022-10-31 12:48:30 +0800694{
695 const char *extra_msg;
Gilles Peskine449bd832023-01-11 14:50:10 +0100696 if (extra_msg0 && extra_msg1) {
Jerry Yud25cab02022-10-31 12:48:30 +0800697 mbedtls_debug_print_msg(
698 ssl, level, file, line,
699 "%s: %s(%u) extension %s %s.",
Gilles Peskine449bd832023-01-11 14:50:10 +0100700 ssl_tls13_get_hs_msg_name(hs_msg_type),
701 mbedtls_ssl_get_extension_name(extension_type),
Jerry Yud25cab02022-10-31 12:48:30 +0800702 extension_type,
Gilles Peskine449bd832023-01-11 14:50:10 +0100703 extra_msg0, extra_msg1);
Jerry Yud25cab02022-10-31 12:48:30 +0800704 return;
705 }
706
707 extra_msg = extra_msg0 ? extra_msg0 : extra_msg1;
Gilles Peskine449bd832023-01-11 14:50:10 +0100708 if (extra_msg) {
Jerry Yud25cab02022-10-31 12:48:30 +0800709 mbedtls_debug_print_msg(
710 ssl, level, file, line,
Gilles Peskine449bd832023-01-11 14:50:10 +0100711 "%s: %s(%u) extension %s.", ssl_tls13_get_hs_msg_name(hs_msg_type),
712 mbedtls_ssl_get_extension_name(extension_type), extension_type,
713 extra_msg);
Jerry Yud25cab02022-10-31 12:48:30 +0800714 return;
715 }
716
717 mbedtls_debug_print_msg(
718 ssl, level, file, line,
Gilles Peskine449bd832023-01-11 14:50:10 +0100719 "%s: %s(%u) extension.", ssl_tls13_get_hs_msg_name(hs_msg_type),
720 mbedtls_ssl_get_extension_name(extension_type), extension_type);
Jerry Yud25cab02022-10-31 12:48:30 +0800721}
722
Gilles Peskine449bd832023-01-11 14:50:10 +0100723void mbedtls_ssl_print_extensions(const mbedtls_ssl_context *ssl,
724 int level, const char *file, int line,
725 int hs_msg_type, uint32_t extensions_mask,
726 const char *extra)
Jerry Yud25cab02022-10-31 12:48:30 +0800727{
728
Gilles Peskine449bd832023-01-11 14:50:10 +0100729 for (unsigned i = 0;
730 i < sizeof(extension_name_table) / sizeof(extension_name_table[0]);
731 i++) {
Jerry Yu79aa7212022-11-08 21:30:21 +0800732 mbedtls_ssl_print_extension(
Jerry Yuea52ed92022-11-08 21:01:17 +0800733 ssl, level, file, line, hs_msg_type, extension_type_table[i],
Gilles Peskine449bd832023-01-11 14:50:10 +0100734 extensions_mask & (1 << i) ? "exists" : "does not exist", extra);
Jerry Yud25cab02022-10-31 12:48:30 +0800735 }
736}
737
Pengyu Lvee455c02023-01-12 14:37:24 +0800738#if defined(MBEDTLS_SSL_PROTO_TLS1_3) && defined(MBEDTLS_SSL_SESSION_TICKETS)
739#define ARRAY_LENGTH(a) (sizeof(a) / sizeof(*(a)))
740
741static const char *ticket_flag_name_table[] =
742{
743 [0] = "ALLOW_PSK_RESUMPTION",
744 [2] = "ALLOW_PSK_EPHEMERAL_RESUMPTION",
745 [3] = "ALLOW_EARLY_DATA",
746};
747
Pengyu Lv4938a562023-01-16 11:28:49 +0800748void mbedtls_ssl_print_ticket_flags(const mbedtls_ssl_context *ssl,
749 int level, const char *file, int line,
750 unsigned int flags)
Pengyu Lvee455c02023-01-12 14:37:24 +0800751{
752 size_t i;
753
754 mbedtls_debug_print_msg(ssl, level, file, line,
Pengyu Lv4938a562023-01-16 11:28:49 +0800755 "print ticket_flags (0x%02x)", flags);
756
757 flags = flags & MBEDTLS_SSL_TLS1_3_TICKET_FLAGS_MASK;
Pengyu Lvee455c02023-01-12 14:37:24 +0800758
759 for (i = 0; i < ARRAY_LENGTH(ticket_flag_name_table); i++) {
Pengyu Lv4938a562023-01-16 11:28:49 +0800760 if ((flags & (1 << i))) {
Pengyu Lvee455c02023-01-12 14:37:24 +0800761 mbedtls_debug_print_msg(ssl, level, file, line, "- %s is set.",
762 ticket_flag_name_table[i]);
763 }
764 }
765}
766#endif /* MBEDTLS_SSL_PROTO_TLS1_3 && MBEDTLS_SSL_SESSION_TICKETS */
767
Jerry Yud25cab02022-10-31 12:48:30 +0800768#endif /* MBEDTLS_DEBUG_C */
769
Gilles Peskine449bd832023-01-11 14:50:10 +0100770void mbedtls_ssl_optimize_checksum(mbedtls_ssl_context *ssl,
771 const mbedtls_ssl_ciphersuite_t *ciphersuite_info)
Jerry Yuc73c6182022-02-08 20:29:25 +0800772{
773 ((void) ciphersuite_info);
774
Andrzej Kurek25f27152022-08-17 16:09:31 -0400775#if defined(MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Gilles Peskine449bd832023-01-11 14:50:10 +0100776 if (ciphersuite_info->mac == MBEDTLS_MD_SHA384) {
Jerry Yuc73c6182022-02-08 20:29:25 +0800777 ssl->handshake->update_checksum = ssl_update_checksum_sha384;
Gilles Peskine449bd832023-01-11 14:50:10 +0100778 } else
Jerry Yuc73c6182022-02-08 20:29:25 +0800779#endif
Andrzej Kurek25f27152022-08-17 16:09:31 -0400780#if defined(MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Gilles Peskine449bd832023-01-11 14:50:10 +0100781 if (ciphersuite_info->mac != MBEDTLS_MD_SHA384) {
Jerry Yuc73c6182022-02-08 20:29:25 +0800782 ssl->handshake->update_checksum = ssl_update_checksum_sha256;
Gilles Peskine449bd832023-01-11 14:50:10 +0100783 } else
Jerry Yuc73c6182022-02-08 20:29:25 +0800784#endif
785 {
Gilles Peskine449bd832023-01-11 14:50:10 +0100786 MBEDTLS_SSL_DEBUG_MSG(1, ("should never happen"));
Jerry Yuc73c6182022-02-08 20:29:25 +0800787 return;
788 }
789}
790
Manuel Pégourié-Gonnardb8b07aa2023-02-06 00:34:21 +0100791int mbedtls_ssl_add_hs_hdr_to_checksum(mbedtls_ssl_context *ssl,
Gilles Peskine449bd832023-01-11 14:50:10 +0100792 unsigned hs_type,
793 size_t total_hs_len)
Ronald Cron8f6d39a2022-03-10 18:56:50 +0100794{
795 unsigned char hs_hdr[4];
796
797 /* Build HS header for checksum update. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100798 hs_hdr[0] = MBEDTLS_BYTE_0(hs_type);
799 hs_hdr[1] = MBEDTLS_BYTE_2(total_hs_len);
800 hs_hdr[2] = MBEDTLS_BYTE_1(total_hs_len);
801 hs_hdr[3] = MBEDTLS_BYTE_0(total_hs_len);
Ronald Cron8f6d39a2022-03-10 18:56:50 +0100802
Manuel Pégourié-Gonnardb8b07aa2023-02-06 00:34:21 +0100803 return ssl->handshake->update_checksum(ssl, hs_hdr, sizeof(hs_hdr));
Ronald Cron8f6d39a2022-03-10 18:56:50 +0100804}
805
Manuel Pégourié-Gonnardb8b07aa2023-02-06 00:34:21 +0100806int mbedtls_ssl_add_hs_msg_to_checksum(mbedtls_ssl_context *ssl,
Gilles Peskine449bd832023-01-11 14:50:10 +0100807 unsigned hs_type,
808 unsigned char const *msg,
809 size_t msg_len)
Ronald Cron8f6d39a2022-03-10 18:56:50 +0100810{
Manuel Pégourié-Gonnardb8b07aa2023-02-06 00:34:21 +0100811 int ret;
812 ret = mbedtls_ssl_add_hs_hdr_to_checksum(ssl, hs_type, msg_len);
813 if (ret != 0)
814 return ret;
815 return ssl->handshake->update_checksum(ssl, msg, msg_len);
Ronald Cron8f6d39a2022-03-10 18:56:50 +0100816}
817
Manuel Pégourié-Gonnard226aa152023-02-05 09:46:59 +0100818int mbedtls_ssl_reset_checksum(mbedtls_ssl_context *ssl)
Jerry Yuc73c6182022-02-08 20:29:25 +0800819{
Manuel Pégourié-Gonnardb72ff492023-02-06 09:54:49 +0100820#if defined(MBEDTLS_USE_PSA_CRYPTO)
821 psa_status_t status;
822#else
823 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
824#endif
Jerry Yuc73c6182022-02-08 20:29:25 +0800825 ((void) ssl);
Andrzej Kurek25f27152022-08-17 16:09:31 -0400826#if defined(MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Jerry Yuc73c6182022-02-08 20:29:25 +0800827#if defined(MBEDTLS_USE_PSA_CRYPTO)
Manuel Pégourié-Gonnardb72ff492023-02-06 09:54:49 +0100828 status = psa_hash_abort(&ssl->handshake->fin_sha256_psa);
829 if (status != PSA_SUCCESS) {
830 return mbedtls_md_error_from_psa(status);
831 }
832 status = psa_hash_setup(&ssl->handshake->fin_sha256_psa, PSA_ALG_SHA_256);
833 if (status != PSA_SUCCESS) {
834 return mbedtls_md_error_from_psa(status);
835 }
Jerry Yuc73c6182022-02-08 20:29:25 +0800836#else
Manuel Pégourié-Gonnardb72ff492023-02-06 09:54:49 +0100837 ret = mbedtls_sha256_starts(&ssl->handshake->fin_sha256, 0);
838 if (ret != 0) {
839 return ret;
840 }
Jerry Yuc73c6182022-02-08 20:29:25 +0800841#endif
842#endif
Andrzej Kurek25f27152022-08-17 16:09:31 -0400843#if defined(MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Jerry Yuc73c6182022-02-08 20:29:25 +0800844#if defined(MBEDTLS_USE_PSA_CRYPTO)
Manuel Pégourié-Gonnardb72ff492023-02-06 09:54:49 +0100845 status = psa_hash_abort(&ssl->handshake->fin_sha384_psa);
846 if (status != PSA_SUCCESS) {
847 return mbedtls_md_error_from_psa(status);
848 }
849 status = psa_hash_setup(&ssl->handshake->fin_sha384_psa, PSA_ALG_SHA_384);
850 if (status != PSA_SUCCESS) {
851 return mbedtls_md_error_from_psa(status);
852 }
Jerry Yuc73c6182022-02-08 20:29:25 +0800853#else
Manuel Pégourié-Gonnardb72ff492023-02-06 09:54:49 +0100854 ret = mbedtls_sha512_starts(&ssl->handshake->fin_sha384, 1);
855 if (ret != 0) {
856 return ret;
857 }
Jerry Yuc73c6182022-02-08 20:29:25 +0800858#endif
859#endif
Manuel Pégourié-Gonnard226aa152023-02-05 09:46:59 +0100860 return 0;
Jerry Yuc73c6182022-02-08 20:29:25 +0800861}
862
Manuel Pégourié-Gonnard226aa152023-02-05 09:46:59 +0100863static int ssl_update_checksum_start(mbedtls_ssl_context *ssl,
Gilles Peskine449bd832023-01-11 14:50:10 +0100864 const unsigned char *buf, size_t len)
Jerry Yuc73c6182022-02-08 20:29:25 +0800865{
Andrzej Kurek25f27152022-08-17 16:09:31 -0400866#if defined(MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Jerry Yuc73c6182022-02-08 20:29:25 +0800867#if defined(MBEDTLS_USE_PSA_CRYPTO)
Gilles Peskine449bd832023-01-11 14:50:10 +0100868 psa_hash_update(&ssl->handshake->fin_sha256_psa, buf, len);
Jerry Yuc73c6182022-02-08 20:29:25 +0800869#else
Gilles Peskine449bd832023-01-11 14:50:10 +0100870 mbedtls_sha256_update(&ssl->handshake->fin_sha256, buf, len);
Jerry Yuc73c6182022-02-08 20:29:25 +0800871#endif
872#endif
Andrzej Kurek25f27152022-08-17 16:09:31 -0400873#if defined(MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Jerry Yuc73c6182022-02-08 20:29:25 +0800874#if defined(MBEDTLS_USE_PSA_CRYPTO)
Gilles Peskine449bd832023-01-11 14:50:10 +0100875 psa_hash_update(&ssl->handshake->fin_sha384_psa, buf, len);
Jerry Yuc73c6182022-02-08 20:29:25 +0800876#else
Gilles Peskine449bd832023-01-11 14:50:10 +0100877 mbedtls_sha512_update(&ssl->handshake->fin_sha384, buf, len);
Jerry Yuc73c6182022-02-08 20:29:25 +0800878#endif
879#endif
Andrzej Kurekeabeb302022-10-17 07:52:51 -0400880#if !defined(MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA) && \
881 !defined(MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
882 (void) ssl;
883 (void) buf;
884 (void) len;
885#endif
Manuel Pégourié-Gonnard226aa152023-02-05 09:46:59 +0100886 return 0;
Jerry Yuc73c6182022-02-08 20:29:25 +0800887}
888
Andrzej Kurek25f27152022-08-17 16:09:31 -0400889#if defined(MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Manuel Pégourié-Gonnard226aa152023-02-05 09:46:59 +0100890static int ssl_update_checksum_sha256(mbedtls_ssl_context *ssl,
Gilles Peskine449bd832023-01-11 14:50:10 +0100891 const unsigned char *buf, size_t len)
Jerry Yuc73c6182022-02-08 20:29:25 +0800892{
893#if defined(MBEDTLS_USE_PSA_CRYPTO)
Gilles Peskine449bd832023-01-11 14:50:10 +0100894 psa_hash_update(&ssl->handshake->fin_sha256_psa, buf, len);
Jerry Yuc73c6182022-02-08 20:29:25 +0800895#else
Gilles Peskine449bd832023-01-11 14:50:10 +0100896 mbedtls_sha256_update(&ssl->handshake->fin_sha256, buf, len);
Jerry Yuc73c6182022-02-08 20:29:25 +0800897#endif
Manuel Pégourié-Gonnard226aa152023-02-05 09:46:59 +0100898 return 0;
Jerry Yuc73c6182022-02-08 20:29:25 +0800899}
900#endif
901
Andrzej Kurek25f27152022-08-17 16:09:31 -0400902#if defined(MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Manuel Pégourié-Gonnard226aa152023-02-05 09:46:59 +0100903static int ssl_update_checksum_sha384(mbedtls_ssl_context *ssl,
Gilles Peskine449bd832023-01-11 14:50:10 +0100904 const unsigned char *buf, size_t len)
Jerry Yuc73c6182022-02-08 20:29:25 +0800905{
906#if defined(MBEDTLS_USE_PSA_CRYPTO)
Gilles Peskine449bd832023-01-11 14:50:10 +0100907 psa_hash_update(&ssl->handshake->fin_sha384_psa, buf, len);
Jerry Yuc73c6182022-02-08 20:29:25 +0800908#else
Gilles Peskine449bd832023-01-11 14:50:10 +0100909 mbedtls_sha512_update(&ssl->handshake->fin_sha384, buf, len);
Jerry Yuc73c6182022-02-08 20:29:25 +0800910#endif
Manuel Pégourié-Gonnard226aa152023-02-05 09:46:59 +0100911 return 0;
Jerry Yuc73c6182022-02-08 20:29:25 +0800912}
913#endif
914
Gilles Peskine449bd832023-01-11 14:50:10 +0100915static void ssl_handshake_params_init(mbedtls_ssl_handshake_params *handshake)
Paul Bakkeraccaffe2014-06-26 13:37:14 +0200916{
Gilles Peskine449bd832023-01-11 14:50:10 +0100917 memset(handshake, 0, sizeof(mbedtls_ssl_handshake_params));
Paul Bakkeraccaffe2014-06-26 13:37:14 +0200918
Andrzej Kurek25f27152022-08-17 16:09:31 -0400919#if defined(MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Andrzej Kurekeb342242019-01-29 09:14:33 -0500920#if defined(MBEDTLS_USE_PSA_CRYPTO)
921 handshake->fin_sha256_psa = psa_hash_operation_init();
Andrzej Kurekeb342242019-01-29 09:14:33 -0500922#else
Gilles Peskine449bd832023-01-11 14:50:10 +0100923 mbedtls_sha256_init(&handshake->fin_sha256);
Paul Bakkeraccaffe2014-06-26 13:37:14 +0200924#endif
Andrzej Kurekeb342242019-01-29 09:14:33 -0500925#endif
Andrzej Kurek25f27152022-08-17 16:09:31 -0400926#if defined(MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Andrzej Kurekeb342242019-01-29 09:14:33 -0500927#if defined(MBEDTLS_USE_PSA_CRYPTO)
Andrzej Kurek972fba52019-01-30 03:29:12 -0500928 handshake->fin_sha384_psa = psa_hash_operation_init();
Andrzej Kurekeb342242019-01-29 09:14:33 -0500929#else
Gilles Peskine449bd832023-01-11 14:50:10 +0100930 mbedtls_sha512_init(&handshake->fin_sha384);
Paul Bakkeraccaffe2014-06-26 13:37:14 +0200931#endif
Andrzej Kurekeb342242019-01-29 09:14:33 -0500932#endif
Paul Bakkeraccaffe2014-06-26 13:37:14 +0200933
934 handshake->update_checksum = ssl_update_checksum_start;
Hanno Becker7e5437a2017-04-28 17:15:26 +0100935
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200936#if defined(MBEDTLS_DHM_C)
Gilles Peskine449bd832023-01-11 14:50:10 +0100937 mbedtls_dhm_init(&handshake->dhm_ctx);
Paul Bakkeraccaffe2014-06-26 13:37:14 +0200938#endif
Neil Armstrongf3f46412022-04-12 14:43:39 +0200939#if !defined(MBEDTLS_USE_PSA_CRYPTO) && defined(MBEDTLS_ECDH_C)
Gilles Peskine449bd832023-01-11 14:50:10 +0100940 mbedtls_ecdh_init(&handshake->ecdh_ctx);
Paul Bakkeraccaffe2014-06-26 13:37:14 +0200941#endif
Manuel Pégourié-Gonnardeef142d2015-09-16 10:05:04 +0200942#if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
Neil Armstrongca7d5062022-05-31 14:43:23 +0200943#if defined(MBEDTLS_USE_PSA_CRYPTO)
944 handshake->psa_pake_ctx = psa_pake_operation_init();
945 handshake->psa_pake_password = MBEDTLS_SVC_KEY_ID_INIT;
946#else
Gilles Peskine449bd832023-01-11 14:50:10 +0100947 mbedtls_ecjpake_init(&handshake->ecjpake_ctx);
Neil Armstrongca7d5062022-05-31 14:43:23 +0200948#endif /* MBEDTLS_USE_PSA_CRYPTO */
Manuel Pégourié-Gonnard77c06462015-09-17 13:59:49 +0200949#if defined(MBEDTLS_SSL_CLI_C)
950 handshake->ecjpake_cache = NULL;
951 handshake->ecjpake_cache_len = 0;
952#endif
Manuel Pégourié-Gonnard76cfd3f2015-09-15 12:10:54 +0200953#endif
Manuel Pégourié-Gonnardcdc26ae2015-06-19 12:16:31 +0200954
Gilles Peskineeccd8882020-03-10 12:19:08 +0100955#if defined(MBEDTLS_SSL_ECP_RESTARTABLE_ENABLED)
Gilles Peskine449bd832023-01-11 14:50:10 +0100956 mbedtls_x509_crt_restart_init(&handshake->ecrs_ctx);
Manuel Pégourié-Gonnard862cde52017-05-17 11:56:15 +0200957#endif
958
Manuel Pégourié-Gonnardcdc26ae2015-06-19 12:16:31 +0200959#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
960 handshake->sni_authmode = MBEDTLS_SSL_VERIFY_UNSET;
961#endif
Hanno Becker75173122019-02-06 16:18:31 +0000962
963#if defined(MBEDTLS_X509_CRT_PARSE_C) && \
964 !defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
Gilles Peskine449bd832023-01-11 14:50:10 +0100965 mbedtls_pk_init(&handshake->peer_pubkey);
Hanno Becker75173122019-02-06 16:18:31 +0000966#endif
Paul Bakkeraccaffe2014-06-26 13:37:14 +0200967}
968
Gilles Peskine449bd832023-01-11 14:50:10 +0100969void mbedtls_ssl_transform_init(mbedtls_ssl_transform *transform)
Paul Bakkeraccaffe2014-06-26 13:37:14 +0200970{
Gilles Peskine449bd832023-01-11 14:50:10 +0100971 memset(transform, 0, sizeof(mbedtls_ssl_transform));
Paul Bakker84bbeb52014-07-01 14:53:22 +0200972
Przemyslaw Stekiel8f80fb92022-01-11 08:28:13 +0100973#if defined(MBEDTLS_USE_PSA_CRYPTO)
974 transform->psa_key_enc = MBEDTLS_SVC_KEY_ID_INIT;
975 transform->psa_key_dec = MBEDTLS_SVC_KEY_ID_INIT;
Przemyslaw Stekiel6be9cf52022-01-19 16:00:22 +0100976#else
Gilles Peskine449bd832023-01-11 14:50:10 +0100977 mbedtls_cipher_init(&transform->cipher_ctx_enc);
978 mbedtls_cipher_init(&transform->cipher_ctx_dec);
Przemyslaw Stekiel8f80fb92022-01-11 08:28:13 +0100979#endif
980
Hanno Beckerfd86ca82020-11-30 08:54:23 +0000981#if defined(MBEDTLS_SSL_SOME_SUITES_USE_MAC)
Neil Armstrong39b8e7d2022-02-23 09:24:45 +0100982#if defined(MBEDTLS_USE_PSA_CRYPTO)
983 transform->psa_mac_enc = MBEDTLS_SVC_KEY_ID_INIT;
984 transform->psa_mac_dec = MBEDTLS_SVC_KEY_ID_INIT;
Neil Armstrongcf8841a2022-02-24 11:17:45 +0100985#else
Gilles Peskine449bd832023-01-11 14:50:10 +0100986 mbedtls_md_init(&transform->md_ctx_enc);
987 mbedtls_md_init(&transform->md_ctx_dec);
Hanno Beckerd56ed242018-01-03 15:32:51 +0000988#endif
Neil Armstrongcf8841a2022-02-24 11:17:45 +0100989#endif
Paul Bakkeraccaffe2014-06-26 13:37:14 +0200990}
991
Gilles Peskine449bd832023-01-11 14:50:10 +0100992void mbedtls_ssl_session_init(mbedtls_ssl_session *session)
Paul Bakkeraccaffe2014-06-26 13:37:14 +0200993{
Gilles Peskine449bd832023-01-11 14:50:10 +0100994 memset(session, 0, sizeof(mbedtls_ssl_session));
Paul Bakkeraccaffe2014-06-26 13:37:14 +0200995}
996
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +0200997MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +0100998static int ssl_handshake_init(mbedtls_ssl_context *ssl)
Paul Bakker48916f92012-09-16 19:57:18 +0000999{
Manuel Pégourié-Gonnardb8b07aa2023-02-06 00:34:21 +01001000 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
1001
Paul Bakkeraccaffe2014-06-26 13:37:14 +02001002 /* Clear old handshake information if present */
Jerry Yu2e199812022-12-01 18:57:19 +08001003#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
Gilles Peskine449bd832023-01-11 14:50:10 +01001004 if (ssl->transform_negotiate) {
1005 mbedtls_ssl_transform_free(ssl->transform_negotiate);
1006 }
Jerry Yu2e199812022-12-01 18:57:19 +08001007#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001008 if (ssl->session_negotiate) {
1009 mbedtls_ssl_session_free(ssl->session_negotiate);
1010 }
1011 if (ssl->handshake) {
1012 mbedtls_ssl_handshake_free(ssl);
1013 }
Paul Bakkeraccaffe2014-06-26 13:37:14 +02001014
Jerry Yu2e199812022-12-01 18:57:19 +08001015#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
Paul Bakkeraccaffe2014-06-26 13:37:14 +02001016 /*
1017 * Either the pointers are now NULL or cleared properly and can be freed.
1018 * Now allocate missing structures.
1019 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001020 if (ssl->transform_negotiate == NULL) {
1021 ssl->transform_negotiate = mbedtls_calloc(1, sizeof(mbedtls_ssl_transform));
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02001022 }
Jerry Yu2e199812022-12-01 18:57:19 +08001023#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakker48916f92012-09-16 19:57:18 +00001024
Gilles Peskine449bd832023-01-11 14:50:10 +01001025 if (ssl->session_negotiate == NULL) {
1026 ssl->session_negotiate = mbedtls_calloc(1, sizeof(mbedtls_ssl_session));
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02001027 }
Paul Bakker48916f92012-09-16 19:57:18 +00001028
Gilles Peskine449bd832023-01-11 14:50:10 +01001029 if (ssl->handshake == NULL) {
1030 ssl->handshake = mbedtls_calloc(1, sizeof(mbedtls_ssl_handshake_params));
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02001031 }
Andrzej Kurek0afa2a12020-03-03 10:39:58 -05001032#if defined(MBEDTLS_SSL_VARIABLE_BUFFER_LENGTH)
1033 /* If the buffers are too small - reallocate */
Andrzej Kurek8ea68722020-04-03 06:40:47 -04001034
Gilles Peskine449bd832023-01-11 14:50:10 +01001035 handle_buffer_resizing(ssl, 0, MBEDTLS_SSL_IN_BUFFER_LEN,
1036 MBEDTLS_SSL_OUT_BUFFER_LEN);
Andrzej Kurek0afa2a12020-03-03 10:39:58 -05001037#endif
Paul Bakker48916f92012-09-16 19:57:18 +00001038
Paul Bakkeraccaffe2014-06-26 13:37:14 +02001039 /* All pointers should exist and can be directly freed without issue */
Gilles Peskine449bd832023-01-11 14:50:10 +01001040 if (ssl->handshake == NULL ||
Jerry Yu2e199812022-12-01 18:57:19 +08001041#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
Paul Bakker48916f92012-09-16 19:57:18 +00001042 ssl->transform_negotiate == NULL ||
Jerry Yu2e199812022-12-01 18:57:19 +08001043#endif
Gilles Peskine449bd832023-01-11 14:50:10 +01001044 ssl->session_negotiate == NULL) {
1045 MBEDTLS_SSL_DEBUG_MSG(1, ("alloc() of ssl sub-contexts failed"));
Paul Bakkeraccaffe2014-06-26 13:37:14 +02001046
Gilles Peskine449bd832023-01-11 14:50:10 +01001047 mbedtls_free(ssl->handshake);
Paul Bakkeraccaffe2014-06-26 13:37:14 +02001048 ssl->handshake = NULL;
Jerry Yu2e199812022-12-01 18:57:19 +08001049
1050#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
Gilles Peskine449bd832023-01-11 14:50:10 +01001051 mbedtls_free(ssl->transform_negotiate);
Paul Bakkeraccaffe2014-06-26 13:37:14 +02001052 ssl->transform_negotiate = NULL;
Jerry Yu2e199812022-12-01 18:57:19 +08001053#endif
1054
Gilles Peskine449bd832023-01-11 14:50:10 +01001055 mbedtls_free(ssl->session_negotiate);
Paul Bakkeraccaffe2014-06-26 13:37:14 +02001056 ssl->session_negotiate = NULL;
1057
Gilles Peskine449bd832023-01-11 14:50:10 +01001058 return MBEDTLS_ERR_SSL_ALLOC_FAILED;
Paul Bakker48916f92012-09-16 19:57:18 +00001059 }
1060
Paul Bakkeraccaffe2014-06-26 13:37:14 +02001061 /* Initialize structures */
Gilles Peskine449bd832023-01-11 14:50:10 +01001062 mbedtls_ssl_session_init(ssl->session_negotiate);
1063 ssl_handshake_params_init(ssl->handshake);
Paul Bakker968afaa2014-07-09 11:09:24 +02001064
Jerry Yu2e199812022-12-01 18:57:19 +08001065#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
Gilles Peskine449bd832023-01-11 14:50:10 +01001066 mbedtls_ssl_transform_init(ssl->transform_negotiate);
Jerry Yu2e199812022-12-01 18:57:19 +08001067#endif
1068
Manuel Pégourié-Gonnard537f2312023-02-05 10:17:45 +01001069 /* Setup handshake checksums */
Manuel Pégourié-Gonnardb8b07aa2023-02-06 00:34:21 +01001070 ret = mbedtls_ssl_reset_checksum(ssl);
1071 if (ret != 0) {
1072 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ssl_reset_checksum", ret);
1073 return ret;
1074 }
Manuel Pégourié-Gonnard537f2312023-02-05 10:17:45 +01001075
Jerry Yud0766ec2022-09-22 10:46:57 +08001076#if defined(MBEDTLS_SSL_PROTO_TLS1_3) && \
1077 defined(MBEDTLS_SSL_SRV_C) && \
1078 defined(MBEDTLS_SSL_SESSION_TICKETS)
1079 ssl->handshake->new_session_tickets_count =
Gilles Peskine449bd832023-01-11 14:50:10 +01001080 ssl->conf->new_session_tickets_count;
Jerry Yud0766ec2022-09-22 10:46:57 +08001081#endif
1082
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001083#if defined(MBEDTLS_SSL_PROTO_DTLS)
Gilles Peskine449bd832023-01-11 14:50:10 +01001084 if (ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM) {
Manuel Pégourié-Gonnard06939ce2015-05-11 11:25:46 +02001085 ssl->handshake->alt_transform_out = ssl->transform_out;
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02001086
Gilles Peskine449bd832023-01-11 14:50:10 +01001087 if (ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT) {
Manuel Pégourié-Gonnard06939ce2015-05-11 11:25:46 +02001088 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_PREPARING;
Gilles Peskine449bd832023-01-11 14:50:10 +01001089 } else {
Manuel Pégourié-Gonnard06939ce2015-05-11 11:25:46 +02001090 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_WAITING;
Gilles Peskine449bd832023-01-11 14:50:10 +01001091 }
Manuel Pégourié-Gonnard286a1362015-05-13 16:22:05 +02001092
Gilles Peskine449bd832023-01-11 14:50:10 +01001093 mbedtls_ssl_set_timer(ssl, 0);
Manuel Pégourié-Gonnard06939ce2015-05-11 11:25:46 +02001094 }
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02001095#endif
1096
Brett Warrene0edc842021-08-17 09:53:13 +01001097/*
1098 * curve_list is translated to IANA TLS group identifiers here because
1099 * mbedtls_ssl_conf_curves returns void and so can't return
1100 * any error codes.
1101 */
1102#if defined(MBEDTLS_ECP_C)
1103#if !defined(MBEDTLS_DEPRECATED_REMOVED)
1104 /* Heap allocate and translate curve_list from internal to IANA group ids */
Gilles Peskine449bd832023-01-11 14:50:10 +01001105 if (ssl->conf->curve_list != NULL) {
Brett Warrene0edc842021-08-17 09:53:13 +01001106 size_t length;
1107 const mbedtls_ecp_group_id *curve_list = ssl->conf->curve_list;
1108
Gilles Peskine449bd832023-01-11 14:50:10 +01001109 for (length = 0; (curve_list[length] != MBEDTLS_ECP_DP_NONE) &&
1110 (length < MBEDTLS_ECP_DP_MAX); length++) {
1111 }
Brett Warrene0edc842021-08-17 09:53:13 +01001112
1113 /* Leave room for zero termination */
Gilles Peskine449bd832023-01-11 14:50:10 +01001114 uint16_t *group_list = mbedtls_calloc(length + 1, sizeof(uint16_t));
1115 if (group_list == NULL) {
1116 return MBEDTLS_ERR_SSL_ALLOC_FAILED;
1117 }
Brett Warrene0edc842021-08-17 09:53:13 +01001118
Gilles Peskine449bd832023-01-11 14:50:10 +01001119 for (size_t i = 0; i < length; i++) {
Valerio Setti18c9fed2022-12-30 17:44:24 +01001120 uint16_t tls_id = mbedtls_ssl_get_tls_id_from_ecp_group_id(
Gilles Peskine449bd832023-01-11 14:50:10 +01001121 curve_list[i]);
1122 if (tls_id == 0) {
1123 mbedtls_free(group_list);
1124 return MBEDTLS_ERR_SSL_BAD_CONFIG;
Brett Warrene0edc842021-08-17 09:53:13 +01001125 }
Valerio Setti18c9fed2022-12-30 17:44:24 +01001126 group_list[i] = tls_id;
Brett Warrene0edc842021-08-17 09:53:13 +01001127 }
1128
1129 group_list[length] = 0;
1130
1131 ssl->handshake->group_list = group_list;
1132 ssl->handshake->group_list_heap_allocated = 1;
Gilles Peskine449bd832023-01-11 14:50:10 +01001133 } else {
Brett Warrene0edc842021-08-17 09:53:13 +01001134 ssl->handshake->group_list = ssl->conf->group_list;
1135 ssl->handshake->group_list_heap_allocated = 0;
1136 }
1137#endif /* MBEDTLS_DEPRECATED_REMOVED */
1138#endif /* MBEDTLS_ECP_C */
1139
Ronald Crone68ab4f2022-10-05 12:46:29 +02001140#if defined(MBEDTLS_SSL_HANDSHAKE_WITH_CERT_ENABLED)
Jerry Yuf017ee42022-01-12 15:49:48 +08001141#if !defined(MBEDTLS_DEPRECATED_REMOVED)
Jerry Yua69269a2022-01-17 21:06:01 +08001142#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
Jerry Yu713013f2022-01-17 18:16:35 +08001143 /* Heap allocate and translate sig_hashes from internal hash identifiers to
1144 signature algorithms IANA identifiers. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001145 if (mbedtls_ssl_conf_is_tls12_only(ssl->conf) &&
1146 ssl->conf->sig_hashes != NULL) {
Jerry Yuf017ee42022-01-12 15:49:48 +08001147 const int *md;
1148 const int *sig_hashes = ssl->conf->sig_hashes;
Jerry Yub476a442022-01-21 18:14:45 +08001149 size_t sig_algs_len = 0;
Jerry Yuf017ee42022-01-12 15:49:48 +08001150 uint16_t *p;
1151
Jerry Yub476a442022-01-21 18:14:45 +08001152#if defined(static_assert)
Gilles Peskine449bd832023-01-11 14:50:10 +01001153 static_assert(MBEDTLS_SSL_MAX_SIG_ALG_LIST_LEN
1154 <= (SIZE_MAX - (2 * sizeof(uint16_t))),
1155 "MBEDTLS_SSL_MAX_SIG_ALG_LIST_LEN too big");
Jerry Yua68dca22022-01-20 16:28:27 +08001156#endif
1157
Gilles Peskine449bd832023-01-11 14:50:10 +01001158 for (md = sig_hashes; *md != MBEDTLS_MD_NONE; md++) {
1159 if (mbedtls_ssl_hash_from_md_alg(*md) == MBEDTLS_SSL_HASH_NONE) {
Jerry Yuf017ee42022-01-12 15:49:48 +08001160 continue;
Gilles Peskine449bd832023-01-11 14:50:10 +01001161 }
Jerry Yub476a442022-01-21 18:14:45 +08001162#if defined(MBEDTLS_ECDSA_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01001163 sig_algs_len += sizeof(uint16_t);
Jerry Yub476a442022-01-21 18:14:45 +08001164#endif
Jerry Yua68dca22022-01-20 16:28:27 +08001165
Jerry Yub476a442022-01-21 18:14:45 +08001166#if defined(MBEDTLS_RSA_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01001167 sig_algs_len += sizeof(uint16_t);
Jerry Yub476a442022-01-21 18:14:45 +08001168#endif
Gilles Peskine449bd832023-01-11 14:50:10 +01001169 if (sig_algs_len > MBEDTLS_SSL_MAX_SIG_ALG_LIST_LEN) {
1170 return MBEDTLS_ERR_SSL_BAD_CONFIG;
1171 }
Jerry Yuf017ee42022-01-12 15:49:48 +08001172 }
1173
Gilles Peskine449bd832023-01-11 14:50:10 +01001174 if (sig_algs_len < MBEDTLS_SSL_MIN_SIG_ALG_LIST_LEN) {
1175 return MBEDTLS_ERR_SSL_BAD_CONFIG;
1176 }
Jerry Yuf017ee42022-01-12 15:49:48 +08001177
Gilles Peskine449bd832023-01-11 14:50:10 +01001178 ssl->handshake->sig_algs = mbedtls_calloc(1, sig_algs_len +
1179 sizeof(uint16_t));
1180 if (ssl->handshake->sig_algs == NULL) {
1181 return MBEDTLS_ERR_SSL_ALLOC_FAILED;
1182 }
Jerry Yuf017ee42022-01-12 15:49:48 +08001183
Gilles Peskine449bd832023-01-11 14:50:10 +01001184 p = (uint16_t *) ssl->handshake->sig_algs;
1185 for (md = sig_hashes; *md != MBEDTLS_MD_NONE; md++) {
1186 unsigned char hash = mbedtls_ssl_hash_from_md_alg(*md);
1187 if (hash == MBEDTLS_SSL_HASH_NONE) {
Jerry Yuf017ee42022-01-12 15:49:48 +08001188 continue;
Gilles Peskine449bd832023-01-11 14:50:10 +01001189 }
Jerry Yu6106fdc2022-01-12 16:36:14 +08001190#if defined(MBEDTLS_ECDSA_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01001191 *p = ((hash << 8) | MBEDTLS_SSL_SIG_ECDSA);
Jerry Yuf017ee42022-01-12 15:49:48 +08001192 p++;
Jerry Yu6106fdc2022-01-12 16:36:14 +08001193#endif
1194#if defined(MBEDTLS_RSA_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01001195 *p = ((hash << 8) | MBEDTLS_SSL_SIG_RSA);
Jerry Yuf017ee42022-01-12 15:49:48 +08001196 p++;
Jerry Yu6106fdc2022-01-12 16:36:14 +08001197#endif
Jerry Yuf017ee42022-01-12 15:49:48 +08001198 }
Gabor Mezei15b95a62022-05-09 16:37:58 +02001199 *p = MBEDTLS_TLS_SIG_NONE;
Jerry Yuf017ee42022-01-12 15:49:48 +08001200 ssl->handshake->sig_algs_heap_allocated = 1;
Gilles Peskine449bd832023-01-11 14:50:10 +01001201 } else
Jerry Yua69269a2022-01-17 21:06:01 +08001202#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Jerry Yuf017ee42022-01-12 15:49:48 +08001203 {
Jerry Yuf017ee42022-01-12 15:49:48 +08001204 ssl->handshake->sig_algs_heap_allocated = 0;
1205 }
Jerry Yucc539102022-06-27 16:27:35 +08001206#endif /* !MBEDTLS_DEPRECATED_REMOVED */
Ronald Crone68ab4f2022-10-05 12:46:29 +02001207#endif /* MBEDTLS_SSL_HANDSHAKE_WITH_CERT_ENABLED */
Gilles Peskine449bd832023-01-11 14:50:10 +01001208 return 0;
Paul Bakker48916f92012-09-16 19:57:18 +00001209}
1210
Manuel Pégourié-Gonnarde057d3b2015-05-20 10:59:43 +02001211#if defined(MBEDTLS_SSL_DTLS_HELLO_VERIFY) && defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard7d38d212014-07-23 17:52:09 +02001212/* Dummy cookie callbacks for defaults */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02001213MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01001214static int ssl_cookie_write_dummy(void *ctx,
1215 unsigned char **p, unsigned char *end,
1216 const unsigned char *cli_id, size_t cli_id_len)
Manuel Pégourié-Gonnard7d38d212014-07-23 17:52:09 +02001217{
1218 ((void) ctx);
1219 ((void) p);
1220 ((void) end);
1221 ((void) cli_id);
1222 ((void) cli_id_len);
1223
Gilles Peskine449bd832023-01-11 14:50:10 +01001224 return MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
Manuel Pégourié-Gonnard7d38d212014-07-23 17:52:09 +02001225}
1226
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02001227MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01001228static int ssl_cookie_check_dummy(void *ctx,
1229 const unsigned char *cookie, size_t cookie_len,
1230 const unsigned char *cli_id, size_t cli_id_len)
Manuel Pégourié-Gonnard7d38d212014-07-23 17:52:09 +02001231{
1232 ((void) ctx);
1233 ((void) cookie);
1234 ((void) cookie_len);
1235 ((void) cli_id);
1236 ((void) cli_id_len);
1237
Gilles Peskine449bd832023-01-11 14:50:10 +01001238 return MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
Manuel Pégourié-Gonnard7d38d212014-07-23 17:52:09 +02001239}
Manuel Pégourié-Gonnarde057d3b2015-05-20 10:59:43 +02001240#endif /* MBEDTLS_SSL_DTLS_HELLO_VERIFY && MBEDTLS_SSL_SRV_C */
Manuel Pégourié-Gonnard7d38d212014-07-23 17:52:09 +02001241
Paul Bakker5121ce52009-01-03 21:22:43 +00001242/*
1243 * Initialize an SSL context
1244 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001245void mbedtls_ssl_init(mbedtls_ssl_context *ssl)
Manuel Pégourié-Gonnard41d479e2015-04-29 00:48:22 +02001246{
Gilles Peskine449bd832023-01-11 14:50:10 +01001247 memset(ssl, 0, sizeof(mbedtls_ssl_context));
Manuel Pégourié-Gonnard41d479e2015-04-29 00:48:22 +02001248}
1249
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02001250MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01001251static int ssl_conf_version_check(const mbedtls_ssl_context *ssl)
Jerry Yu60835a82021-08-04 10:13:52 +08001252{
Ronald Cron086ee0b2022-03-15 15:18:51 +01001253 const mbedtls_ssl_config *conf = ssl->conf;
1254
Ronald Cron6f135e12021-12-08 16:57:54 +01001255#if defined(MBEDTLS_SSL_PROTO_TLS1_3)
Gilles Peskine449bd832023-01-11 14:50:10 +01001256 if (mbedtls_ssl_conf_is_tls13_only(conf)) {
1257 if (conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM) {
1258 MBEDTLS_SSL_DEBUG_MSG(1, ("DTLS 1.3 is not yet supported."));
1259 return MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
XiaokangQianed582dd2022-04-13 08:21:05 +00001260 }
1261
Gilles Peskine449bd832023-01-11 14:50:10 +01001262 MBEDTLS_SSL_DEBUG_MSG(4, ("The SSL configuration is tls13 only."));
1263 return 0;
Jerry Yu60835a82021-08-04 10:13:52 +08001264 }
1265#endif
1266
1267#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
Gilles Peskine449bd832023-01-11 14:50:10 +01001268 if (mbedtls_ssl_conf_is_tls12_only(conf)) {
1269 MBEDTLS_SSL_DEBUG_MSG(4, ("The SSL configuration is tls12 only."));
1270 return 0;
Jerry Yu60835a82021-08-04 10:13:52 +08001271 }
1272#endif
1273
Ronald Cron6f135e12021-12-08 16:57:54 +01001274#if defined(MBEDTLS_SSL_PROTO_TLS1_2) && defined(MBEDTLS_SSL_PROTO_TLS1_3)
Gilles Peskine449bd832023-01-11 14:50:10 +01001275 if (mbedtls_ssl_conf_is_hybrid_tls12_tls13(conf)) {
1276 if (ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM) {
1277 MBEDTLS_SSL_DEBUG_MSG(1, ("DTLS not yet supported in Hybrid TLS 1.3 + TLS 1.2"));
1278 return MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
XiaokangQianed582dd2022-04-13 08:21:05 +00001279 }
1280
Gilles Peskine449bd832023-01-11 14:50:10 +01001281 if (conf->endpoint == MBEDTLS_SSL_IS_SERVER) {
1282 MBEDTLS_SSL_DEBUG_MSG(1, ("TLS 1.3 server is not supported yet."));
1283 return MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
XiaokangQian8f9dfe42022-04-15 02:52:39 +00001284 }
1285
1286
Gilles Peskine449bd832023-01-11 14:50:10 +01001287 MBEDTLS_SSL_DEBUG_MSG(4, ("The SSL configuration is TLS 1.3 or TLS 1.2."));
1288 return 0;
Jerry Yu60835a82021-08-04 10:13:52 +08001289 }
1290#endif
1291
Gilles Peskine449bd832023-01-11 14:50:10 +01001292 MBEDTLS_SSL_DEBUG_MSG(1, ("The SSL configuration is invalid."));
1293 return MBEDTLS_ERR_SSL_BAD_CONFIG;
Jerry Yu60835a82021-08-04 10:13:52 +08001294}
1295
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02001296MBEDTLS_CHECK_RETURN_CRITICAL
Jerry Yu60835a82021-08-04 10:13:52 +08001297static int ssl_conf_check(const mbedtls_ssl_context *ssl)
1298{
1299 int ret;
Gilles Peskine449bd832023-01-11 14:50:10 +01001300 ret = ssl_conf_version_check(ssl);
1301 if (ret != 0) {
1302 return ret;
1303 }
Jerry Yu60835a82021-08-04 10:13:52 +08001304
Jerry Yudef7ae42022-10-30 14:13:19 +08001305#if defined(MBEDTLS_SSL_PROTO_TLS1_3)
1306 /* RFC 8446 section 4.4.3
1307 *
1308 * If the verification fails, the receiver MUST terminate the handshake with
1309 * a "decrypt_error" alert.
1310 *
1311 * If the client is configured as TLS 1.3 only with optional verify, return
1312 * bad config.
1313 *
1314 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001315 if (mbedtls_ssl_conf_tls13_ephemeral_enabled(
1316 (mbedtls_ssl_context *) ssl) &&
Jerry Yudef7ae42022-10-30 14:13:19 +08001317 ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT &&
1318 ssl->conf->max_tls_version == MBEDTLS_SSL_VERSION_TLS1_3 &&
1319 ssl->conf->min_tls_version == MBEDTLS_SSL_VERSION_TLS1_3 &&
Gilles Peskine449bd832023-01-11 14:50:10 +01001320 ssl->conf->authmode == MBEDTLS_SSL_VERIFY_OPTIONAL) {
Jerry Yudef7ae42022-10-30 14:13:19 +08001321 MBEDTLS_SSL_DEBUG_MSG(
Gilles Peskine449bd832023-01-11 14:50:10 +01001322 1, ("Optional verify auth mode "
1323 "is not available for TLS 1.3 client"));
1324 return MBEDTLS_ERR_SSL_BAD_CONFIG;
Jerry Yudef7ae42022-10-30 14:13:19 +08001325 }
1326#endif /* MBEDTLS_SSL_PROTO_TLS1_3 */
1327
Jerry Yu60835a82021-08-04 10:13:52 +08001328 /* Space for further checks */
1329
Gilles Peskine449bd832023-01-11 14:50:10 +01001330 return 0;
Jerry Yu60835a82021-08-04 10:13:52 +08001331}
1332
Manuel Pégourié-Gonnard41d479e2015-04-29 00:48:22 +02001333/*
1334 * Setup an SSL context
1335 */
Hanno Becker2a43f6f2018-08-10 11:12:52 +01001336
Gilles Peskine449bd832023-01-11 14:50:10 +01001337int mbedtls_ssl_setup(mbedtls_ssl_context *ssl,
1338 const mbedtls_ssl_config *conf)
Paul Bakker5121ce52009-01-03 21:22:43 +00001339{
Janos Follath865b3eb2019-12-16 11:46:15 +00001340 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Darryl Greenb33cc762019-11-28 14:29:44 +00001341 size_t in_buf_len = MBEDTLS_SSL_IN_BUFFER_LEN;
1342 size_t out_buf_len = MBEDTLS_SSL_OUT_BUFFER_LEN;
Paul Bakker5121ce52009-01-03 21:22:43 +00001343
Manuel Pégourié-Gonnarddef0bbe2015-05-04 14:56:36 +02001344 ssl->conf = conf;
Paul Bakker62f2dee2012-09-28 07:31:51 +00001345
Gilles Peskine449bd832023-01-11 14:50:10 +01001346 if ((ret = ssl_conf_check(ssl)) != 0) {
1347 return ret;
1348 }
Jerry Yu60835a82021-08-04 10:13:52 +08001349
Paul Bakker62f2dee2012-09-28 07:31:51 +00001350 /*
Manuel Pégourié-Gonnard06193482014-02-14 08:39:32 +01001351 * Prepare base structures
Paul Bakker62f2dee2012-09-28 07:31:51 +00001352 */
k-stachowiakc9a5f022018-07-24 13:53:31 +02001353
1354 /* Set to NULL in case of an error condition */
1355 ssl->out_buf = NULL;
k-stachowiaka47911c2018-07-04 17:41:58 +02001356
Darryl Greenb33cc762019-11-28 14:29:44 +00001357#if defined(MBEDTLS_SSL_VARIABLE_BUFFER_LENGTH)
1358 ssl->in_buf_len = in_buf_len;
1359#endif
Gilles Peskine449bd832023-01-11 14:50:10 +01001360 ssl->in_buf = mbedtls_calloc(1, in_buf_len);
1361 if (ssl->in_buf == NULL) {
1362 MBEDTLS_SSL_DEBUG_MSG(1, ("alloc(%" MBEDTLS_PRINTF_SIZET " bytes) failed", in_buf_len));
k-stachowiak9f7798e2018-07-31 16:52:32 +02001363 ret = MBEDTLS_ERR_SSL_ALLOC_FAILED;
k-stachowiaka47911c2018-07-04 17:41:58 +02001364 goto error;
Angus Grattond8213d02016-05-25 20:56:48 +10001365 }
1366
Darryl Greenb33cc762019-11-28 14:29:44 +00001367#if defined(MBEDTLS_SSL_VARIABLE_BUFFER_LENGTH)
1368 ssl->out_buf_len = out_buf_len;
1369#endif
Gilles Peskine449bd832023-01-11 14:50:10 +01001370 ssl->out_buf = mbedtls_calloc(1, out_buf_len);
1371 if (ssl->out_buf == NULL) {
1372 MBEDTLS_SSL_DEBUG_MSG(1, ("alloc(%" MBEDTLS_PRINTF_SIZET " bytes) failed", out_buf_len));
k-stachowiak9f7798e2018-07-31 16:52:32 +02001373 ret = MBEDTLS_ERR_SSL_ALLOC_FAILED;
k-stachowiaka47911c2018-07-04 17:41:58 +02001374 goto error;
Paul Bakker5121ce52009-01-03 21:22:43 +00001375 }
1376
Gilles Peskine449bd832023-01-11 14:50:10 +01001377 mbedtls_ssl_reset_in_out_pointers(ssl);
Manuel Pégourié-Gonnard419d5ae2015-05-04 19:32:36 +02001378
Johan Pascalb62bb512015-12-03 21:56:45 +01001379#if defined(MBEDTLS_SSL_DTLS_SRTP)
Gilles Peskine449bd832023-01-11 14:50:10 +01001380 memset(&ssl->dtls_srtp_info, 0, sizeof(ssl->dtls_srtp_info));
Johan Pascalb62bb512015-12-03 21:56:45 +01001381#endif
1382
Gilles Peskine449bd832023-01-11 14:50:10 +01001383 if ((ret = ssl_handshake_init(ssl)) != 0) {
k-stachowiaka47911c2018-07-04 17:41:58 +02001384 goto error;
Gilles Peskine449bd832023-01-11 14:50:10 +01001385 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001386
Gilles Peskine449bd832023-01-11 14:50:10 +01001387 return 0;
k-stachowiaka47911c2018-07-04 17:41:58 +02001388
1389error:
Gilles Peskine449bd832023-01-11 14:50:10 +01001390 mbedtls_free(ssl->in_buf);
1391 mbedtls_free(ssl->out_buf);
k-stachowiaka47911c2018-07-04 17:41:58 +02001392
1393 ssl->conf = NULL;
1394
Darryl Greenb33cc762019-11-28 14:29:44 +00001395#if defined(MBEDTLS_SSL_VARIABLE_BUFFER_LENGTH)
1396 ssl->in_buf_len = 0;
1397 ssl->out_buf_len = 0;
1398#endif
k-stachowiaka47911c2018-07-04 17:41:58 +02001399 ssl->in_buf = NULL;
1400 ssl->out_buf = NULL;
1401
1402 ssl->in_hdr = NULL;
1403 ssl->in_ctr = NULL;
1404 ssl->in_len = NULL;
1405 ssl->in_iv = NULL;
1406 ssl->in_msg = NULL;
1407
1408 ssl->out_hdr = NULL;
1409 ssl->out_ctr = NULL;
1410 ssl->out_len = NULL;
1411 ssl->out_iv = NULL;
1412 ssl->out_msg = NULL;
1413
Gilles Peskine449bd832023-01-11 14:50:10 +01001414 return ret;
Paul Bakker5121ce52009-01-03 21:22:43 +00001415}
1416
1417/*
Paul Bakker7eb013f2011-10-06 12:37:39 +00001418 * Reset an initialized and used SSL context for re-use while retaining
1419 * all application-set variables, function pointers and data.
Manuel Pégourié-Gonnard3f09b6d2015-09-08 11:58:14 +02001420 *
1421 * If partial is non-zero, keep data in the input buffer and client ID.
1422 * (Use when a DTLS client reconnects from the same port.)
Paul Bakker7eb013f2011-10-06 12:37:39 +00001423 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001424void mbedtls_ssl_session_reset_msg_layer(mbedtls_ssl_context *ssl,
1425 int partial)
Paul Bakker7eb013f2011-10-06 12:37:39 +00001426{
Darryl Greenb33cc762019-11-28 14:29:44 +00001427#if defined(MBEDTLS_SSL_VARIABLE_BUFFER_LENGTH)
1428 size_t in_buf_len = ssl->in_buf_len;
1429 size_t out_buf_len = ssl->out_buf_len;
1430#else
1431 size_t in_buf_len = MBEDTLS_SSL_IN_BUFFER_LEN;
1432 size_t out_buf_len = MBEDTLS_SSL_OUT_BUFFER_LEN;
1433#endif
Paul Bakker48916f92012-09-16 19:57:18 +00001434
Hanno Beckerb0302c42021-08-03 09:39:42 +01001435#if !defined(MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE) || !defined(MBEDTLS_SSL_SRV_C)
1436 partial = 0;
Hanno Becker7e772132018-08-10 12:38:21 +01001437#endif
1438
Manuel Pégourié-Gonnard286a1362015-05-13 16:22:05 +02001439 /* Cancel any possibly running timer */
Gilles Peskine449bd832023-01-11 14:50:10 +01001440 mbedtls_ssl_set_timer(ssl, 0);
Manuel Pégourié-Gonnard286a1362015-05-13 16:22:05 +02001441
Gilles Peskine449bd832023-01-11 14:50:10 +01001442 mbedtls_ssl_reset_in_out_pointers(ssl);
Hanno Beckerb0302c42021-08-03 09:39:42 +01001443
1444 /* Reset incoming message parsing */
1445 ssl->in_offt = NULL;
1446 ssl->nb_zero = 0;
1447 ssl->in_msgtype = 0;
1448 ssl->in_msglen = 0;
1449 ssl->in_hslen = 0;
1450 ssl->keep_current_message = 0;
1451 ssl->transform_in = NULL;
1452
1453#if defined(MBEDTLS_SSL_PROTO_DTLS)
1454 ssl->next_record_offset = 0;
1455 ssl->in_epoch = 0;
1456#endif
1457
1458 /* Keep current datagram if partial == 1 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001459 if (partial == 0) {
Hanno Beckerb0302c42021-08-03 09:39:42 +01001460 ssl->in_left = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +01001461 memset(ssl->in_buf, 0, in_buf_len);
Hanno Beckerb0302c42021-08-03 09:39:42 +01001462 }
1463
Ronald Cronad8c17b2022-06-10 17:18:09 +02001464 ssl->send_alert = 0;
1465
Hanno Beckerb0302c42021-08-03 09:39:42 +01001466 /* Reset outgoing message writing */
1467 ssl->out_msgtype = 0;
1468 ssl->out_msglen = 0;
1469 ssl->out_left = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +01001470 memset(ssl->out_buf, 0, out_buf_len);
1471 memset(ssl->cur_out_ctr, 0, sizeof(ssl->cur_out_ctr));
Hanno Beckerb0302c42021-08-03 09:39:42 +01001472 ssl->transform_out = NULL;
1473
1474#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
Gilles Peskine449bd832023-01-11 14:50:10 +01001475 mbedtls_ssl_dtls_replay_reset(ssl);
Hanno Beckerb0302c42021-08-03 09:39:42 +01001476#endif
1477
XiaokangQian2b01dc32022-01-21 02:53:13 +00001478#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
Gilles Peskine449bd832023-01-11 14:50:10 +01001479 if (ssl->transform) {
1480 mbedtls_ssl_transform_free(ssl->transform);
1481 mbedtls_free(ssl->transform);
Hanno Beckerb0302c42021-08-03 09:39:42 +01001482 ssl->transform = NULL;
1483 }
XiaokangQian2b01dc32022-01-21 02:53:13 +00001484#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
1485
XiaokangQian2b01dc32022-01-21 02:53:13 +00001486#if defined(MBEDTLS_SSL_PROTO_TLS1_3)
Gilles Peskine449bd832023-01-11 14:50:10 +01001487 mbedtls_ssl_transform_free(ssl->transform_application);
1488 mbedtls_free(ssl->transform_application);
XiaokangQian2b01dc32022-01-21 02:53:13 +00001489 ssl->transform_application = NULL;
1490
Gilles Peskine449bd832023-01-11 14:50:10 +01001491 if (ssl->handshake != NULL) {
Jerry Yu3d9b5902022-11-04 14:07:25 +08001492#if defined(MBEDTLS_SSL_EARLY_DATA)
Gilles Peskine449bd832023-01-11 14:50:10 +01001493 mbedtls_ssl_transform_free(ssl->handshake->transform_earlydata);
1494 mbedtls_free(ssl->handshake->transform_earlydata);
XiaokangQian2b01dc32022-01-21 02:53:13 +00001495 ssl->handshake->transform_earlydata = NULL;
Jerry Yu3d9b5902022-11-04 14:07:25 +08001496#endif
XiaokangQian2b01dc32022-01-21 02:53:13 +00001497
Gilles Peskine449bd832023-01-11 14:50:10 +01001498 mbedtls_ssl_transform_free(ssl->handshake->transform_handshake);
1499 mbedtls_free(ssl->handshake->transform_handshake);
XiaokangQian2b01dc32022-01-21 02:53:13 +00001500 ssl->handshake->transform_handshake = NULL;
1501 }
1502
XiaokangQian2b01dc32022-01-21 02:53:13 +00001503#endif /* MBEDTLS_SSL_PROTO_TLS1_3 */
Hanno Beckerb0302c42021-08-03 09:39:42 +01001504}
1505
Gilles Peskine449bd832023-01-11 14:50:10 +01001506int mbedtls_ssl_session_reset_int(mbedtls_ssl_context *ssl, int partial)
Hanno Beckerb0302c42021-08-03 09:39:42 +01001507{
1508 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
1509
1510 ssl->state = MBEDTLS_SSL_HELLO_REQUEST;
1511
Gilles Peskine449bd832023-01-11 14:50:10 +01001512 mbedtls_ssl_session_reset_msg_layer(ssl, partial);
Hanno Beckerb0302c42021-08-03 09:39:42 +01001513
1514 /* Reset renegotiation state */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001515#if defined(MBEDTLS_SSL_RENEGOTIATION)
1516 ssl->renego_status = MBEDTLS_SSL_INITIAL_HANDSHAKE;
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01001517 ssl->renego_records_seen = 0;
Paul Bakker48916f92012-09-16 19:57:18 +00001518
1519 ssl->verify_data_len = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +01001520 memset(ssl->own_verify_data, 0, MBEDTLS_SSL_VERIFY_DATA_MAX_LEN);
1521 memset(ssl->peer_verify_data, 0, MBEDTLS_SSL_VERIFY_DATA_MAX_LEN);
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01001522#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001523 ssl->secure_renegotiation = MBEDTLS_SSL_LEGACY_RENEGOTIATION;
Paul Bakker48916f92012-09-16 19:57:18 +00001524
Hanno Beckerb0302c42021-08-03 09:39:42 +01001525 ssl->session_in = NULL;
Hanno Becker78640902018-08-13 16:35:15 +01001526 ssl->session_out = NULL;
Gilles Peskine449bd832023-01-11 14:50:10 +01001527 if (ssl->session) {
1528 mbedtls_ssl_session_free(ssl->session);
1529 mbedtls_free(ssl->session);
Paul Bakkerc0463502013-02-14 11:19:38 +01001530 ssl->session = NULL;
1531 }
1532
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001533#if defined(MBEDTLS_SSL_ALPN)
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02001534 ssl->alpn_chosen = NULL;
1535#endif
1536
Manuel Pégourié-Gonnarde057d3b2015-05-20 10:59:43 +02001537#if defined(MBEDTLS_SSL_DTLS_HELLO_VERIFY) && defined(MBEDTLS_SSL_SRV_C)
David Horstmann3b2276a2022-10-06 14:49:08 +01001538 int free_cli_id = 1;
Hanno Becker4ccbf062018-08-10 11:20:38 +01001539#if defined(MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE)
Gilles Peskine449bd832023-01-11 14:50:10 +01001540 free_cli_id = (partial == 0);
Hanno Becker4ccbf062018-08-10 11:20:38 +01001541#endif
Gilles Peskine449bd832023-01-11 14:50:10 +01001542 if (free_cli_id) {
1543 mbedtls_free(ssl->cli_id);
Manuel Pégourié-Gonnard3f09b6d2015-09-08 11:58:14 +02001544 ssl->cli_id = NULL;
1545 ssl->cli_id_len = 0;
1546 }
Manuel Pégourié-Gonnard43c02182014-07-22 17:32:01 +02001547#endif
1548
Gilles Peskine449bd832023-01-11 14:50:10 +01001549 if ((ret = ssl_handshake_init(ssl)) != 0) {
1550 return ret;
1551 }
Paul Bakker2770fbd2012-07-03 13:30:23 +00001552
Gilles Peskine449bd832023-01-11 14:50:10 +01001553 return 0;
Paul Bakker7eb013f2011-10-06 12:37:39 +00001554}
1555
Manuel Pégourié-Gonnard779e4292013-08-03 13:50:48 +02001556/*
Manuel Pégourié-Gonnard3f09b6d2015-09-08 11:58:14 +02001557 * Reset an initialized and used SSL context for re-use while retaining
1558 * all application-set variables, function pointers and data.
1559 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001560int mbedtls_ssl_session_reset(mbedtls_ssl_context *ssl)
Manuel Pégourié-Gonnard3f09b6d2015-09-08 11:58:14 +02001561{
Gilles Peskine449bd832023-01-11 14:50:10 +01001562 return mbedtls_ssl_session_reset_int(ssl, 0);
Manuel Pégourié-Gonnard3f09b6d2015-09-08 11:58:14 +02001563}
1564
1565/*
Paul Bakker5121ce52009-01-03 21:22:43 +00001566 * SSL set accessors
1567 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001568void mbedtls_ssl_conf_endpoint(mbedtls_ssl_config *conf, int endpoint)
Paul Bakker5121ce52009-01-03 21:22:43 +00001569{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02001570 conf->endpoint = endpoint;
Paul Bakker5121ce52009-01-03 21:22:43 +00001571}
1572
Gilles Peskine449bd832023-01-11 14:50:10 +01001573void mbedtls_ssl_conf_transport(mbedtls_ssl_config *conf, int transport)
Manuel Pégourié-Gonnard0b1ff292014-02-06 13:04:16 +01001574{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02001575 conf->transport = transport;
Manuel Pégourié-Gonnard0b1ff292014-02-06 13:04:16 +01001576}
1577
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001578#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
Gilles Peskine449bd832023-01-11 14:50:10 +01001579void mbedtls_ssl_conf_dtls_anti_replay(mbedtls_ssl_config *conf, char mode)
Manuel Pégourié-Gonnard27393132014-09-24 14:41:11 +02001580{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02001581 conf->anti_replay = mode;
Manuel Pégourié-Gonnard27393132014-09-24 14:41:11 +02001582}
1583#endif
1584
Gilles Peskine449bd832023-01-11 14:50:10 +01001585void mbedtls_ssl_conf_dtls_badmac_limit(mbedtls_ssl_config *conf, unsigned limit)
Manuel Pégourié-Gonnardb0643d12014-10-14 18:30:36 +02001586{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02001587 conf->badmac_limit = limit;
Manuel Pégourié-Gonnardb0643d12014-10-14 18:30:36 +02001588}
Manuel Pégourié-Gonnardb0643d12014-10-14 18:30:36 +02001589
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001590#if defined(MBEDTLS_SSL_PROTO_DTLS)
Hanno Becker04da1892018-08-14 13:22:10 +01001591
Gilles Peskine449bd832023-01-11 14:50:10 +01001592void mbedtls_ssl_set_datagram_packing(mbedtls_ssl_context *ssl,
1593 unsigned allow_packing)
Hanno Becker04da1892018-08-14 13:22:10 +01001594{
1595 ssl->disable_datagram_packing = !allow_packing;
1596}
1597
Gilles Peskine449bd832023-01-11 14:50:10 +01001598void mbedtls_ssl_conf_handshake_timeout(mbedtls_ssl_config *conf,
1599 uint32_t min, uint32_t max)
Manuel Pégourié-Gonnard905dd242014-10-01 12:03:55 +02001600{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02001601 conf->hs_timeout_min = min;
1602 conf->hs_timeout_max = max;
Manuel Pégourié-Gonnard905dd242014-10-01 12:03:55 +02001603}
1604#endif
1605
Gilles Peskine449bd832023-01-11 14:50:10 +01001606void mbedtls_ssl_conf_authmode(mbedtls_ssl_config *conf, int authmode)
Paul Bakker5121ce52009-01-03 21:22:43 +00001607{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02001608 conf->authmode = authmode;
Paul Bakker5121ce52009-01-03 21:22:43 +00001609}
1610
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001611#if defined(MBEDTLS_X509_CRT_PARSE_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01001612void mbedtls_ssl_conf_verify(mbedtls_ssl_config *conf,
1613 int (*f_vrfy)(void *, mbedtls_x509_crt *, int, uint32_t *),
1614 void *p_vrfy)
Paul Bakkerb63b0af2011-01-13 17:54:59 +00001615{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02001616 conf->f_vrfy = f_vrfy;
1617 conf->p_vrfy = p_vrfy;
Paul Bakkerb63b0af2011-01-13 17:54:59 +00001618}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001619#endif /* MBEDTLS_X509_CRT_PARSE_C */
Paul Bakkerb63b0af2011-01-13 17:54:59 +00001620
Gilles Peskine449bd832023-01-11 14:50:10 +01001621void mbedtls_ssl_conf_rng(mbedtls_ssl_config *conf,
1622 int (*f_rng)(void *, unsigned char *, size_t),
1623 void *p_rng)
Paul Bakker5121ce52009-01-03 21:22:43 +00001624{
Manuel Pégourié-Gonnard750e4d72015-05-07 12:35:38 +01001625 conf->f_rng = f_rng;
1626 conf->p_rng = p_rng;
Paul Bakker5121ce52009-01-03 21:22:43 +00001627}
1628
Gilles Peskine449bd832023-01-11 14:50:10 +01001629void mbedtls_ssl_conf_dbg(mbedtls_ssl_config *conf,
1630 void (*f_dbg)(void *, int, const char *, int, const char *),
1631 void *p_dbg)
Paul Bakker5121ce52009-01-03 21:22:43 +00001632{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02001633 conf->f_dbg = f_dbg;
1634 conf->p_dbg = p_dbg;
Paul Bakker5121ce52009-01-03 21:22:43 +00001635}
1636
Gilles Peskine449bd832023-01-11 14:50:10 +01001637void mbedtls_ssl_set_bio(mbedtls_ssl_context *ssl,
1638 void *p_bio,
1639 mbedtls_ssl_send_t *f_send,
1640 mbedtls_ssl_recv_t *f_recv,
1641 mbedtls_ssl_recv_timeout_t *f_recv_timeout)
Manuel Pégourié-Gonnard8fa6dfd2014-09-17 10:47:43 +02001642{
1643 ssl->p_bio = p_bio;
1644 ssl->f_send = f_send;
1645 ssl->f_recv = f_recv;
1646 ssl->f_recv_timeout = f_recv_timeout;
Manuel Pégourié-Gonnard97fd52c2015-05-06 15:38:52 +01001647}
1648
Manuel Pégourié-Gonnard6e7aaca2018-08-20 10:37:23 +02001649#if defined(MBEDTLS_SSL_PROTO_DTLS)
Gilles Peskine449bd832023-01-11 14:50:10 +01001650void mbedtls_ssl_set_mtu(mbedtls_ssl_context *ssl, uint16_t mtu)
Manuel Pégourié-Gonnard6e7aaca2018-08-20 10:37:23 +02001651{
1652 ssl->mtu = mtu;
1653}
1654#endif
1655
Gilles Peskine449bd832023-01-11 14:50:10 +01001656void mbedtls_ssl_conf_read_timeout(mbedtls_ssl_config *conf, uint32_t timeout)
Manuel Pégourié-Gonnard97fd52c2015-05-06 15:38:52 +01001657{
1658 conf->read_timeout = timeout;
Manuel Pégourié-Gonnard8fa6dfd2014-09-17 10:47:43 +02001659}
1660
Gilles Peskine449bd832023-01-11 14:50:10 +01001661void mbedtls_ssl_set_timer_cb(mbedtls_ssl_context *ssl,
1662 void *p_timer,
1663 mbedtls_ssl_set_timer_t *f_set_timer,
1664 mbedtls_ssl_get_timer_t *f_get_timer)
Manuel Pégourié-Gonnard2e012912015-05-12 20:55:41 +02001665{
1666 ssl->p_timer = p_timer;
1667 ssl->f_set_timer = f_set_timer;
1668 ssl->f_get_timer = f_get_timer;
Manuel Pégourié-Gonnard286a1362015-05-13 16:22:05 +02001669
1670 /* Make sure we start with no timer running */
Gilles Peskine449bd832023-01-11 14:50:10 +01001671 mbedtls_ssl_set_timer(ssl, 0);
Manuel Pégourié-Gonnard2e012912015-05-12 20:55:41 +02001672}
1673
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001674#if defined(MBEDTLS_SSL_SRV_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01001675void mbedtls_ssl_conf_session_cache(mbedtls_ssl_config *conf,
1676 void *p_cache,
1677 mbedtls_ssl_cache_get_t *f_get_cache,
1678 mbedtls_ssl_cache_set_t *f_set_cache)
Paul Bakker5121ce52009-01-03 21:22:43 +00001679{
Manuel Pégourié-Gonnard5cb33082015-05-06 18:06:26 +01001680 conf->p_cache = p_cache;
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02001681 conf->f_get_cache = f_get_cache;
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02001682 conf->f_set_cache = f_set_cache;
Paul Bakker5121ce52009-01-03 21:22:43 +00001683}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001684#endif /* MBEDTLS_SSL_SRV_C */
Paul Bakker5121ce52009-01-03 21:22:43 +00001685
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001686#if defined(MBEDTLS_SSL_CLI_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01001687int mbedtls_ssl_set_session(mbedtls_ssl_context *ssl, const mbedtls_ssl_session *session)
Paul Bakker5121ce52009-01-03 21:22:43 +00001688{
Janos Follath865b3eb2019-12-16 11:46:15 +00001689 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +02001690
Gilles Peskine449bd832023-01-11 14:50:10 +01001691 if (ssl == NULL ||
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +02001692 session == NULL ||
1693 ssl->session_negotiate == NULL ||
Gilles Peskine449bd832023-01-11 14:50:10 +01001694 ssl->conf->endpoint != MBEDTLS_SSL_IS_CLIENT) {
1695 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +02001696 }
1697
Gilles Peskine449bd832023-01-11 14:50:10 +01001698 if (ssl->handshake->resume == 1) {
1699 return MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
1700 }
Hanno Beckere810bbc2021-05-14 16:01:05 +01001701
Jerry Yu21092062022-10-10 21:21:31 +08001702#if defined(MBEDTLS_SSL_PROTO_TLS1_3)
Gilles Peskine449bd832023-01-11 14:50:10 +01001703 if (session->tls_version == MBEDTLS_SSL_VERSION_TLS1_3) {
Jerry Yu21092062022-10-10 21:21:31 +08001704 const mbedtls_ssl_ciphersuite_t *ciphersuite_info =
Gilles Peskine449bd832023-01-11 14:50:10 +01001705 mbedtls_ssl_ciphersuite_from_id(session->ciphersuite);
Jerry Yu21092062022-10-10 21:21:31 +08001706
Gilles Peskine449bd832023-01-11 14:50:10 +01001707 if (mbedtls_ssl_validate_ciphersuite(
Jerry Yu21092062022-10-10 21:21:31 +08001708 ssl, ciphersuite_info, MBEDTLS_SSL_VERSION_TLS1_3,
Gilles Peskine449bd832023-01-11 14:50:10 +01001709 MBEDTLS_SSL_VERSION_TLS1_3) != 0) {
1710 MBEDTLS_SSL_DEBUG_MSG(4, ("%d is not a valid TLS 1.3 ciphersuite.",
1711 session->ciphersuite));
1712 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
Jerry Yu21092062022-10-10 21:21:31 +08001713 }
Jerry Yu40afab62022-10-08 10:42:13 +08001714 }
Jerry Yu21092062022-10-10 21:21:31 +08001715#endif /* MBEDTLS_SSL_PROTO_TLS1_3 */
Jerry Yu40afab62022-10-08 10:42:13 +08001716
Gilles Peskine449bd832023-01-11 14:50:10 +01001717 if ((ret = mbedtls_ssl_session_copy(ssl->session_negotiate,
1718 session)) != 0) {
1719 return ret;
1720 }
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +02001721
Paul Bakker0a597072012-09-25 21:55:46 +00001722 ssl->handshake->resume = 1;
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +02001723
Gilles Peskine449bd832023-01-11 14:50:10 +01001724 return 0;
Paul Bakker5121ce52009-01-03 21:22:43 +00001725}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001726#endif /* MBEDTLS_SSL_CLI_C */
Paul Bakker5121ce52009-01-03 21:22:43 +00001727
Gilles Peskine449bd832023-01-11 14:50:10 +01001728void mbedtls_ssl_conf_ciphersuites(mbedtls_ssl_config *conf,
1729 const int *ciphersuites)
Mateusz Starzyk06b07fb2021-02-18 13:55:21 +01001730{
Hanno Beckerd60b6c62021-04-29 12:04:11 +01001731 conf->ciphersuite_list = ciphersuites;
Paul Bakker5121ce52009-01-03 21:22:43 +00001732}
1733
Ronald Cron6f135e12021-12-08 16:57:54 +01001734#if defined(MBEDTLS_SSL_PROTO_TLS1_3)
Gilles Peskine449bd832023-01-11 14:50:10 +01001735void mbedtls_ssl_conf_tls13_key_exchange_modes(mbedtls_ssl_config *conf,
1736 const int kex_modes)
Hanno Becker71f1ed62021-07-24 06:01:47 +01001737{
Xiaofei Bai746f9482021-11-12 08:53:56 +00001738 conf->tls13_kex_modes = kex_modes & MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_ALL;
Hanno Becker71f1ed62021-07-24 06:01:47 +01001739}
Xiaokang Qian72de95d2022-10-25 02:54:33 +00001740
1741#if defined(MBEDTLS_SSL_EARLY_DATA)
Gilles Peskine449bd832023-01-11 14:50:10 +01001742void mbedtls_ssl_tls13_conf_early_data(mbedtls_ssl_config *conf,
1743 int early_data_enabled)
Xiaokang Qian72de95d2022-10-25 02:54:33 +00001744{
1745 conf->early_data_enabled = early_data_enabled;
1746}
Jerry Yucc4e0072022-11-22 17:22:22 +08001747
1748#if defined(MBEDTLS_SSL_SRV_C)
1749void mbedtls_ssl_tls13_conf_max_early_data_size(
Gilles Peskine449bd832023-01-11 14:50:10 +01001750 mbedtls_ssl_config *conf, uint32_t max_early_data_size)
Jerry Yucc4e0072022-11-22 17:22:22 +08001751{
Jerry Yu39da9852022-12-06 16:58:36 +08001752 conf->max_early_data_size = max_early_data_size;
Jerry Yucc4e0072022-11-22 17:22:22 +08001753}
1754#endif /* MBEDTLS_SSL_SRV_C */
1755
Xiaokang Qian72de95d2022-10-25 02:54:33 +00001756#endif /* MBEDTLS_SSL_EARLY_DATA */
Ronald Cron6f135e12021-12-08 16:57:54 +01001757#endif /* MBEDTLS_SSL_PROTO_TLS1_3 */
Hanno Becker71f1ed62021-07-24 06:01:47 +01001758
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001759#if defined(MBEDTLS_X509_CRT_PARSE_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01001760void mbedtls_ssl_conf_cert_profile(mbedtls_ssl_config *conf,
1761 const mbedtls_x509_crt_profile *profile)
Manuel Pégourié-Gonnard6e3ee3a2015-06-17 10:58:20 +02001762{
1763 conf->cert_profile = profile;
1764}
1765
Gilles Peskine449bd832023-01-11 14:50:10 +01001766static void ssl_key_cert_free(mbedtls_ssl_key_cert *key_cert)
Glenn Strauss36872db2022-01-22 05:06:31 -05001767{
1768 mbedtls_ssl_key_cert *cur = key_cert, *next;
1769
Gilles Peskine449bd832023-01-11 14:50:10 +01001770 while (cur != NULL) {
Glenn Strauss36872db2022-01-22 05:06:31 -05001771 next = cur->next;
Gilles Peskine449bd832023-01-11 14:50:10 +01001772 mbedtls_free(cur);
Glenn Strauss36872db2022-01-22 05:06:31 -05001773 cur = next;
1774 }
1775}
1776
Manuel Pégourié-Gonnard8f618a82015-05-10 21:13:36 +02001777/* Append a new keycert entry to a (possibly empty) list */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02001778MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01001779static int ssl_append_key_cert(mbedtls_ssl_key_cert **head,
1780 mbedtls_x509_crt *cert,
1781 mbedtls_pk_context *key)
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02001782{
niisato8ee24222018-06-25 19:05:48 +09001783 mbedtls_ssl_key_cert *new_cert;
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02001784
Gilles Peskine449bd832023-01-11 14:50:10 +01001785 if (cert == NULL) {
Glenn Strauss36872db2022-01-22 05:06:31 -05001786 /* Free list if cert is null */
Gilles Peskine449bd832023-01-11 14:50:10 +01001787 ssl_key_cert_free(*head);
Glenn Strauss36872db2022-01-22 05:06:31 -05001788 *head = NULL;
Gilles Peskine449bd832023-01-11 14:50:10 +01001789 return 0;
Glenn Strauss36872db2022-01-22 05:06:31 -05001790 }
1791
Gilles Peskine449bd832023-01-11 14:50:10 +01001792 new_cert = mbedtls_calloc(1, sizeof(mbedtls_ssl_key_cert));
1793 if (new_cert == NULL) {
1794 return MBEDTLS_ERR_SSL_ALLOC_FAILED;
1795 }
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02001796
niisato8ee24222018-06-25 19:05:48 +09001797 new_cert->cert = cert;
1798 new_cert->key = key;
1799 new_cert->next = NULL;
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02001800
Glenn Strauss36872db2022-01-22 05:06:31 -05001801 /* Update head if the list was null, else add to the end */
Gilles Peskine449bd832023-01-11 14:50:10 +01001802 if (*head == NULL) {
niisato8ee24222018-06-25 19:05:48 +09001803 *head = new_cert;
Gilles Peskine449bd832023-01-11 14:50:10 +01001804 } else {
Manuel Pégourié-Gonnard8f618a82015-05-10 21:13:36 +02001805 mbedtls_ssl_key_cert *cur = *head;
Gilles Peskine449bd832023-01-11 14:50:10 +01001806 while (cur->next != NULL) {
Manuel Pégourié-Gonnard8f618a82015-05-10 21:13:36 +02001807 cur = cur->next;
Gilles Peskine449bd832023-01-11 14:50:10 +01001808 }
niisato8ee24222018-06-25 19:05:48 +09001809 cur->next = new_cert;
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02001810 }
1811
Gilles Peskine449bd832023-01-11 14:50:10 +01001812 return 0;
Manuel Pégourié-Gonnard8f618a82015-05-10 21:13:36 +02001813}
1814
Gilles Peskine449bd832023-01-11 14:50:10 +01001815int mbedtls_ssl_conf_own_cert(mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnard8f618a82015-05-10 21:13:36 +02001816 mbedtls_x509_crt *own_cert,
Gilles Peskine449bd832023-01-11 14:50:10 +01001817 mbedtls_pk_context *pk_key)
Manuel Pégourié-Gonnard8f618a82015-05-10 21:13:36 +02001818{
Gilles Peskine449bd832023-01-11 14:50:10 +01001819 return ssl_append_key_cert(&conf->key_cert, own_cert, pk_key);
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02001820}
1821
Gilles Peskine449bd832023-01-11 14:50:10 +01001822void mbedtls_ssl_conf_ca_chain(mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnardbc2b7712015-05-06 11:14:19 +01001823 mbedtls_x509_crt *ca_chain,
Gilles Peskine449bd832023-01-11 14:50:10 +01001824 mbedtls_x509_crl *ca_crl)
Paul Bakker5121ce52009-01-03 21:22:43 +00001825{
Manuel Pégourié-Gonnardbc2b7712015-05-06 11:14:19 +01001826 conf->ca_chain = ca_chain;
1827 conf->ca_crl = ca_crl;
Hanno Becker5adaad92019-03-27 16:54:37 +00001828
1829#if defined(MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK)
1830 /* mbedtls_ssl_conf_ca_chain() and mbedtls_ssl_conf_ca_cb()
1831 * cannot be used together. */
1832 conf->f_ca_cb = NULL;
1833 conf->p_ca_cb = NULL;
1834#endif /* MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK */
Paul Bakker5121ce52009-01-03 21:22:43 +00001835}
Hanno Becker5adaad92019-03-27 16:54:37 +00001836
1837#if defined(MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK)
Gilles Peskine449bd832023-01-11 14:50:10 +01001838void mbedtls_ssl_conf_ca_cb(mbedtls_ssl_config *conf,
1839 mbedtls_x509_crt_ca_cb_t f_ca_cb,
1840 void *p_ca_cb)
Hanno Becker5adaad92019-03-27 16:54:37 +00001841{
1842 conf->f_ca_cb = f_ca_cb;
1843 conf->p_ca_cb = p_ca_cb;
1844
1845 /* mbedtls_ssl_conf_ca_chain() and mbedtls_ssl_conf_ca_cb()
1846 * cannot be used together. */
1847 conf->ca_chain = NULL;
1848 conf->ca_crl = NULL;
1849}
1850#endif /* MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001851#endif /* MBEDTLS_X509_CRT_PARSE_C */
Paul Bakkereb2c6582012-09-27 19:15:01 +00001852
Manuel Pégourié-Gonnard1af6c852015-05-10 23:10:37 +02001853#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
Gilles Peskine449bd832023-01-11 14:50:10 +01001854const unsigned char *mbedtls_ssl_get_hs_sni(mbedtls_ssl_context *ssl,
1855 size_t *name_len)
Glenn Strauss69894072022-01-24 12:58:00 -05001856{
1857 *name_len = ssl->handshake->sni_name_len;
Gilles Peskine449bd832023-01-11 14:50:10 +01001858 return ssl->handshake->sni_name;
Glenn Strauss69894072022-01-24 12:58:00 -05001859}
1860
Gilles Peskine449bd832023-01-11 14:50:10 +01001861int mbedtls_ssl_set_hs_own_cert(mbedtls_ssl_context *ssl,
1862 mbedtls_x509_crt *own_cert,
1863 mbedtls_pk_context *pk_key)
Manuel Pégourié-Gonnard1af6c852015-05-10 23:10:37 +02001864{
Gilles Peskine449bd832023-01-11 14:50:10 +01001865 return ssl_append_key_cert(&ssl->handshake->sni_key_cert,
1866 own_cert, pk_key);
Manuel Pégourié-Gonnard1af6c852015-05-10 23:10:37 +02001867}
Manuel Pégourié-Gonnard22bfa4b2015-05-11 08:46:37 +02001868
Gilles Peskine449bd832023-01-11 14:50:10 +01001869void mbedtls_ssl_set_hs_ca_chain(mbedtls_ssl_context *ssl,
1870 mbedtls_x509_crt *ca_chain,
1871 mbedtls_x509_crl *ca_crl)
Manuel Pégourié-Gonnard22bfa4b2015-05-11 08:46:37 +02001872{
1873 ssl->handshake->sni_ca_chain = ca_chain;
1874 ssl->handshake->sni_ca_crl = ca_crl;
1875}
Manuel Pégourié-Gonnardcdc26ae2015-06-19 12:16:31 +02001876
Glenn Strauss999ef702022-03-11 01:37:23 -05001877#if defined(MBEDTLS_KEY_EXCHANGE_CERT_REQ_ALLOWED_ENABLED)
Gilles Peskine449bd832023-01-11 14:50:10 +01001878void mbedtls_ssl_set_hs_dn_hints(mbedtls_ssl_context *ssl,
1879 const mbedtls_x509_crt *crt)
Glenn Strauss999ef702022-03-11 01:37:23 -05001880{
1881 ssl->handshake->dn_hints = crt;
1882}
1883#endif /* MBEDTLS_KEY_EXCHANGE_CERT_REQ_ALLOWED_ENABLED */
1884
Gilles Peskine449bd832023-01-11 14:50:10 +01001885void mbedtls_ssl_set_hs_authmode(mbedtls_ssl_context *ssl,
1886 int authmode)
Manuel Pégourié-Gonnardcdc26ae2015-06-19 12:16:31 +02001887{
1888 ssl->handshake->sni_authmode = authmode;
1889}
Manuel Pégourié-Gonnard1af6c852015-05-10 23:10:37 +02001890#endif /* MBEDTLS_SSL_SERVER_NAME_INDICATION */
1891
Hanno Becker8927c832019-04-03 12:52:50 +01001892#if defined(MBEDTLS_X509_CRT_PARSE_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01001893void mbedtls_ssl_set_verify(mbedtls_ssl_context *ssl,
1894 int (*f_vrfy)(void *, mbedtls_x509_crt *, int, uint32_t *),
1895 void *p_vrfy)
Hanno Becker8927c832019-04-03 12:52:50 +01001896{
1897 ssl->f_vrfy = f_vrfy;
1898 ssl->p_vrfy = p_vrfy;
1899}
1900#endif
1901
Manuel Pégourié-Gonnardeef142d2015-09-16 10:05:04 +02001902#if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
Manuel Pégourié-Gonnard7002f4a2015-09-15 12:43:43 +02001903
Valerio Setti016f6822022-12-09 14:17:50 +01001904#if defined(MBEDTLS_USE_PSA_CRYPTO)
1905static psa_status_t mbedtls_ssl_set_hs_ecjpake_password_common(
Gilles Peskine449bd832023-01-11 14:50:10 +01001906 mbedtls_ssl_context *ssl,
1907 mbedtls_svc_key_id_t pwd)
Valerio Setti016f6822022-12-09 14:17:50 +01001908{
1909 psa_status_t status;
1910 psa_pake_role_t psa_role;
1911 psa_pake_cipher_suite_t cipher_suite = psa_pake_cipher_suite_init();
1912
Gilles Peskine449bd832023-01-11 14:50:10 +01001913 psa_pake_cs_set_algorithm(&cipher_suite, PSA_ALG_JPAKE);
1914 psa_pake_cs_set_primitive(&cipher_suite,
1915 PSA_PAKE_PRIMITIVE(PSA_PAKE_PRIMITIVE_TYPE_ECC,
1916 PSA_ECC_FAMILY_SECP_R1,
1917 256));
1918 psa_pake_cs_set_hash(&cipher_suite, PSA_ALG_SHA_256);
Valerio Setti016f6822022-12-09 14:17:50 +01001919
Gilles Peskine449bd832023-01-11 14:50:10 +01001920 status = psa_pake_setup(&ssl->handshake->psa_pake_ctx, &cipher_suite);
1921 if (status != PSA_SUCCESS) {
Valerio Setti016f6822022-12-09 14:17:50 +01001922 return status;
Gilles Peskine449bd832023-01-11 14:50:10 +01001923 }
Manuel Pégourié-Gonnard7002f4a2015-09-15 12:43:43 +02001924
Gilles Peskine449bd832023-01-11 14:50:10 +01001925 if (ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER) {
Neil Armstrongca7d5062022-05-31 14:43:23 +02001926 psa_role = PSA_PAKE_ROLE_SERVER;
Gilles Peskine449bd832023-01-11 14:50:10 +01001927 } else {
Neil Armstrongca7d5062022-05-31 14:43:23 +02001928 psa_role = PSA_PAKE_ROLE_CLIENT;
Gilles Peskine449bd832023-01-11 14:50:10 +01001929 }
Neil Armstrongca7d5062022-05-31 14:43:23 +02001930
Gilles Peskine449bd832023-01-11 14:50:10 +01001931 status = psa_pake_set_role(&ssl->handshake->psa_pake_ctx, psa_role);
1932 if (status != PSA_SUCCESS) {
Valerio Setti016f6822022-12-09 14:17:50 +01001933 return status;
Gilles Peskine449bd832023-01-11 14:50:10 +01001934 }
Valerio Setti016f6822022-12-09 14:17:50 +01001935
Gilles Peskine449bd832023-01-11 14:50:10 +01001936 status = psa_pake_set_password_key(&ssl->handshake->psa_pake_ctx, pwd);
1937 if (status != PSA_SUCCESS) {
Valerio Setti016f6822022-12-09 14:17:50 +01001938 return status;
Gilles Peskine449bd832023-01-11 14:50:10 +01001939 }
Valerio Setti016f6822022-12-09 14:17:50 +01001940
1941 ssl->handshake->psa_pake_ctx_is_ok = 1;
1942
Gilles Peskine449bd832023-01-11 14:50:10 +01001943 return PSA_SUCCESS;
Valerio Setti016f6822022-12-09 14:17:50 +01001944}
1945
Gilles Peskine449bd832023-01-11 14:50:10 +01001946int mbedtls_ssl_set_hs_ecjpake_password(mbedtls_ssl_context *ssl,
1947 const unsigned char *pw,
1948 size_t pw_len)
Valerio Setti016f6822022-12-09 14:17:50 +01001949{
1950 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
1951 psa_status_t status;
1952
Gilles Peskine449bd832023-01-11 14:50:10 +01001953 if (ssl->handshake == NULL || ssl->conf == NULL) {
1954 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
Neil Armstrongca7d5062022-05-31 14:43:23 +02001955 }
1956
Gilles Peskine449bd832023-01-11 14:50:10 +01001957 /* Empty password is not valid */
1958 if ((pw == NULL) || (pw_len == 0)) {
1959 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
1960 }
1961
1962 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_DERIVE);
1963 psa_set_key_algorithm(&attributes, PSA_ALG_JPAKE);
1964 psa_set_key_type(&attributes, PSA_KEY_TYPE_PASSWORD);
1965
1966 status = psa_import_key(&attributes, pw, pw_len,
1967 &ssl->handshake->psa_pake_password);
1968 if (status != PSA_SUCCESS) {
1969 return MBEDTLS_ERR_SSL_HW_ACCEL_FAILED;
1970 }
1971
1972 status = mbedtls_ssl_set_hs_ecjpake_password_common(ssl,
1973 ssl->handshake->psa_pake_password);
1974 if (status != PSA_SUCCESS) {
1975 psa_destroy_key(ssl->handshake->psa_pake_password);
1976 psa_pake_abort(&ssl->handshake->psa_pake_ctx);
1977 return MBEDTLS_ERR_SSL_HW_ACCEL_FAILED;
1978 }
1979
1980 return 0;
Valerio Setti02c25b52022-11-15 14:08:42 +01001981}
Valerio Settia9a97dc2022-11-28 18:26:16 +01001982
Gilles Peskine449bd832023-01-11 14:50:10 +01001983int mbedtls_ssl_set_hs_ecjpake_password_opaque(mbedtls_ssl_context *ssl,
1984 mbedtls_svc_key_id_t pwd)
Valerio Settia9a97dc2022-11-28 18:26:16 +01001985{
Valerio Settia9a97dc2022-11-28 18:26:16 +01001986 psa_status_t status;
1987
Gilles Peskine449bd832023-01-11 14:50:10 +01001988 if (ssl->handshake == NULL || ssl->conf == NULL) {
1989 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
Neil Armstrongca7d5062022-05-31 14:43:23 +02001990 }
1991
Gilles Peskine449bd832023-01-11 14:50:10 +01001992 if (mbedtls_svc_key_id_is_null(pwd)) {
1993 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
1994 }
1995
1996 status = mbedtls_ssl_set_hs_ecjpake_password_common(ssl, pwd);
1997 if (status != PSA_SUCCESS) {
1998 psa_pake_abort(&ssl->handshake->psa_pake_ctx);
1999 return MBEDTLS_ERR_SSL_HW_ACCEL_FAILED;
2000 }
2001
2002 return 0;
Valerio Setti02c25b52022-11-15 14:08:42 +01002003}
2004#else /* MBEDTLS_USE_PSA_CRYPTO */
Gilles Peskine449bd832023-01-11 14:50:10 +01002005int mbedtls_ssl_set_hs_ecjpake_password(mbedtls_ssl_context *ssl,
2006 const unsigned char *pw,
2007 size_t pw_len)
Manuel Pégourié-Gonnard7002f4a2015-09-15 12:43:43 +02002008{
2009 mbedtls_ecjpake_role role;
2010
Gilles Peskine449bd832023-01-11 14:50:10 +01002011 if (ssl->handshake == NULL || ssl->conf == NULL) {
2012 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
2013 }
Manuel Pégourié-Gonnard7002f4a2015-09-15 12:43:43 +02002014
Valerio Settic689ed82022-12-07 14:40:38 +01002015 /* Empty password is not valid */
Gilles Peskine449bd832023-01-11 14:50:10 +01002016 if ((pw == NULL) || (pw_len == 0)) {
2017 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
2018 }
Valerio Settic689ed82022-12-07 14:40:38 +01002019
Gilles Peskine449bd832023-01-11 14:50:10 +01002020 if (ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER) {
Manuel Pégourié-Gonnard7002f4a2015-09-15 12:43:43 +02002021 role = MBEDTLS_ECJPAKE_SERVER;
Gilles Peskine449bd832023-01-11 14:50:10 +01002022 } else {
Manuel Pégourié-Gonnard7002f4a2015-09-15 12:43:43 +02002023 role = MBEDTLS_ECJPAKE_CLIENT;
Gilles Peskine449bd832023-01-11 14:50:10 +01002024 }
Manuel Pégourié-Gonnard7002f4a2015-09-15 12:43:43 +02002025
Gilles Peskine449bd832023-01-11 14:50:10 +01002026 return mbedtls_ecjpake_setup(&ssl->handshake->ecjpake_ctx,
2027 role,
2028 MBEDTLS_MD_SHA256,
2029 MBEDTLS_ECP_DP_SECP256R1,
2030 pw, pw_len);
Manuel Pégourié-Gonnard7002f4a2015-09-15 12:43:43 +02002031}
Valerio Setti02c25b52022-11-15 14:08:42 +01002032#endif /* MBEDTLS_USE_PSA_CRYPTO */
Manuel Pégourié-Gonnardeef142d2015-09-16 10:05:04 +02002033#endif /* MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED */
Manuel Pégourié-Gonnard7002f4a2015-09-15 12:43:43 +02002034
Ronald Cron73fe8df2022-10-05 14:31:43 +02002035#if defined(MBEDTLS_SSL_HANDSHAKE_WITH_PSK_ENABLED)
Gilles Peskine449bd832023-01-11 14:50:10 +01002036int mbedtls_ssl_conf_has_static_psk(mbedtls_ssl_config const *conf)
Hanno Becker2ed3dce2021-04-19 21:59:14 +01002037{
Gilles Peskine449bd832023-01-11 14:50:10 +01002038 if (conf->psk_identity == NULL ||
2039 conf->psk_identity_len == 0) {
2040 return 0;
Ronald Crond29e13e2022-10-19 10:33:48 +02002041 }
2042
Hanno Becker2ed3dce2021-04-19 21:59:14 +01002043#if defined(MBEDTLS_USE_PSA_CRYPTO)
Gilles Peskine449bd832023-01-11 14:50:10 +01002044 if (!mbedtls_svc_key_id_is_null(conf->psk_opaque)) {
2045 return 1;
2046 }
Hanno Becker2ed3dce2021-04-19 21:59:14 +01002047#endif /* MBEDTLS_USE_PSA_CRYPTO */
Ronald Crond29e13e2022-10-19 10:33:48 +02002048
Gilles Peskine449bd832023-01-11 14:50:10 +01002049 if (conf->psk != NULL && conf->psk_len != 0) {
2050 return 1;
2051 }
Hanno Becker2ed3dce2021-04-19 21:59:14 +01002052
Gilles Peskine449bd832023-01-11 14:50:10 +01002053 return 0;
Hanno Becker2ed3dce2021-04-19 21:59:14 +01002054}
2055
Gilles Peskine449bd832023-01-11 14:50:10 +01002056static void ssl_conf_remove_psk(mbedtls_ssl_config *conf)
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01002057{
2058 /* Remove reference to existing PSK, if any. */
2059#if defined(MBEDTLS_USE_PSA_CRYPTO)
Gilles Peskine449bd832023-01-11 14:50:10 +01002060 if (!mbedtls_svc_key_id_is_null(conf->psk_opaque)) {
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01002061 /* The maintenance of the PSK key slot is the
2062 * user's responsibility. */
Ronald Croncf56a0a2020-08-04 09:51:30 +02002063 conf->psk_opaque = MBEDTLS_SVC_KEY_ID_INIT;
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01002064 }
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01002065#endif /* MBEDTLS_USE_PSA_CRYPTO */
Gilles Peskine449bd832023-01-11 14:50:10 +01002066 if (conf->psk != NULL) {
2067 mbedtls_platform_zeroize(conf->psk, conf->psk_len);
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01002068
Gilles Peskine449bd832023-01-11 14:50:10 +01002069 mbedtls_free(conf->psk);
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01002070 conf->psk = NULL;
2071 conf->psk_len = 0;
2072 }
2073
2074 /* Remove reference to PSK identity, if any. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002075 if (conf->psk_identity != NULL) {
2076 mbedtls_free(conf->psk_identity);
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01002077 conf->psk_identity = NULL;
2078 conf->psk_identity_len = 0;
2079 }
2080}
2081
Hanno Becker7390c712018-11-15 13:33:04 +00002082/* This function assumes that PSK identity in the SSL config is unset.
2083 * It checks that the provided identity is well-formed and attempts
2084 * to make a copy of it in the SSL config.
2085 * On failure, the PSK identity in the config remains unset. */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02002086MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01002087static int ssl_conf_set_psk_identity(mbedtls_ssl_config *conf,
2088 unsigned char const *psk_identity,
2089 size_t psk_identity_len)
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02002090{
Manuel Pégourié-Gonnardc6b5d832015-08-27 16:37:35 +02002091 /* Identity len will be encoded on two bytes */
Gilles Peskine449bd832023-01-11 14:50:10 +01002092 if (psk_identity == NULL ||
Ronald Cron2a87e9b2022-10-19 10:55:26 +02002093 psk_identity_len == 0 ||
Gilles Peskine449bd832023-01-11 14:50:10 +01002094 (psk_identity_len >> 16) != 0 ||
2095 psk_identity_len > MBEDTLS_SSL_OUT_CONTENT_LEN) {
2096 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
Manuel Pégourié-Gonnardc6b5d832015-08-27 16:37:35 +02002097 }
2098
Gilles Peskine449bd832023-01-11 14:50:10 +01002099 conf->psk_identity = mbedtls_calloc(1, psk_identity_len);
2100 if (conf->psk_identity == NULL) {
2101 return MBEDTLS_ERR_SSL_ALLOC_FAILED;
2102 }
Paul Bakker6db455e2013-09-18 17:29:31 +02002103
Manuel Pégourié-Gonnard120fdbd2015-05-07 17:07:50 +01002104 conf->psk_identity_len = psk_identity_len;
Gilles Peskine449bd832023-01-11 14:50:10 +01002105 memcpy(conf->psk_identity, psk_identity, conf->psk_identity_len);
Paul Bakker5ad403f2013-09-18 21:21:30 +02002106
Gilles Peskine449bd832023-01-11 14:50:10 +01002107 return 0;
Paul Bakker6db455e2013-09-18 17:29:31 +02002108}
2109
Gilles Peskine449bd832023-01-11 14:50:10 +01002110int mbedtls_ssl_conf_psk(mbedtls_ssl_config *conf,
2111 const unsigned char *psk, size_t psk_len,
2112 const unsigned char *psk_identity, size_t psk_identity_len)
Hanno Becker7390c712018-11-15 13:33:04 +00002113{
Janos Follath865b3eb2019-12-16 11:46:15 +00002114 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Hanno Becker2ed3dce2021-04-19 21:59:14 +01002115
2116 /* We currently only support one PSK, raw or opaque. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002117 if (mbedtls_ssl_conf_has_static_psk(conf)) {
2118 return MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
2119 }
Hanno Becker7390c712018-11-15 13:33:04 +00002120
2121 /* Check and set raw PSK */
Gilles Peskine449bd832023-01-11 14:50:10 +01002122 if (psk == NULL) {
2123 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
2124 }
2125 if (psk_len == 0) {
2126 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
2127 }
2128 if (psk_len > MBEDTLS_PSK_MAX_LEN) {
2129 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
2130 }
Piotr Nowicki9926eaf2019-11-20 14:54:36 +01002131
Gilles Peskine449bd832023-01-11 14:50:10 +01002132 if ((conf->psk = mbedtls_calloc(1, psk_len)) == NULL) {
2133 return MBEDTLS_ERR_SSL_ALLOC_FAILED;
2134 }
Hanno Becker7390c712018-11-15 13:33:04 +00002135 conf->psk_len = psk_len;
Gilles Peskine449bd832023-01-11 14:50:10 +01002136 memcpy(conf->psk, psk, conf->psk_len);
Hanno Becker7390c712018-11-15 13:33:04 +00002137
2138 /* Check and set PSK Identity */
Gilles Peskine449bd832023-01-11 14:50:10 +01002139 ret = ssl_conf_set_psk_identity(conf, psk_identity, psk_identity_len);
2140 if (ret != 0) {
2141 ssl_conf_remove_psk(conf);
2142 }
Hanno Becker7390c712018-11-15 13:33:04 +00002143
Gilles Peskine449bd832023-01-11 14:50:10 +01002144 return ret;
Hanno Becker7390c712018-11-15 13:33:04 +00002145}
2146
Gilles Peskine449bd832023-01-11 14:50:10 +01002147static void ssl_remove_psk(mbedtls_ssl_context *ssl)
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01002148{
2149#if defined(MBEDTLS_USE_PSA_CRYPTO)
Gilles Peskine449bd832023-01-11 14:50:10 +01002150 if (!mbedtls_svc_key_id_is_null(ssl->handshake->psk_opaque)) {
Neil Armstrong501c9322022-05-03 09:35:09 +02002151 /* The maintenance of the external PSK key slot is the
2152 * user's responsibility. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002153 if (ssl->handshake->psk_opaque_is_internal) {
2154 psa_destroy_key(ssl->handshake->psk_opaque);
Neil Armstrong501c9322022-05-03 09:35:09 +02002155 ssl->handshake->psk_opaque_is_internal = 0;
2156 }
Ronald Croncf56a0a2020-08-04 09:51:30 +02002157 ssl->handshake->psk_opaque = MBEDTLS_SVC_KEY_ID_INIT;
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01002158 }
Neil Armstronge952a302022-05-03 10:22:14 +02002159#else
Gilles Peskine449bd832023-01-11 14:50:10 +01002160 if (ssl->handshake->psk != NULL) {
2161 mbedtls_platform_zeroize(ssl->handshake->psk,
2162 ssl->handshake->psk_len);
2163 mbedtls_free(ssl->handshake->psk);
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01002164 ssl->handshake->psk_len = 0;
2165 }
Neil Armstronge952a302022-05-03 10:22:14 +02002166#endif /* MBEDTLS_USE_PSA_CRYPTO */
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01002167}
2168
Gilles Peskine449bd832023-01-11 14:50:10 +01002169int mbedtls_ssl_set_hs_psk(mbedtls_ssl_context *ssl,
2170 const unsigned char *psk, size_t psk_len)
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01002171{
Neil Armstrong501c9322022-05-03 09:35:09 +02002172#if defined(MBEDTLS_USE_PSA_CRYPTO)
2173 psa_key_attributes_t key_attributes = psa_key_attributes_init();
Jerry Yu5d01c052022-08-17 10:18:10 +08002174 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Jerry Yu24b8c812022-08-20 19:06:56 +08002175 psa_algorithm_t alg = PSA_ALG_NONE;
Jerry Yu5d01c052022-08-17 10:18:10 +08002176 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Neil Armstrong501c9322022-05-03 09:35:09 +02002177#endif /* MBEDTLS_USE_PSA_CRYPTO */
2178
Gilles Peskine449bd832023-01-11 14:50:10 +01002179 if (psk == NULL || ssl->handshake == NULL) {
2180 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
2181 }
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01002182
Gilles Peskine449bd832023-01-11 14:50:10 +01002183 if (psk_len > MBEDTLS_PSK_MAX_LEN) {
2184 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
2185 }
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01002186
Gilles Peskine449bd832023-01-11 14:50:10 +01002187 ssl_remove_psk(ssl);
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01002188
Neil Armstrong501c9322022-05-03 09:35:09 +02002189#if defined(MBEDTLS_USE_PSA_CRYPTO)
Jerry Yuccc68a42022-07-26 16:39:20 +08002190#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
Gilles Peskine449bd832023-01-11 14:50:10 +01002191 if (ssl->tls_version == MBEDTLS_SSL_VERSION_TLS1_2) {
2192 if (ssl->handshake->ciphersuite_info->mac == MBEDTLS_MD_SHA384) {
2193 alg = PSA_ALG_TLS12_PSK_TO_MS(PSA_ALG_SHA_384);
2194 } else {
2195 alg = PSA_ALG_TLS12_PSK_TO_MS(PSA_ALG_SHA_256);
2196 }
2197 psa_set_key_usage_flags(&key_attributes, PSA_KEY_USAGE_DERIVE);
Jerry Yuccc68a42022-07-26 16:39:20 +08002198 }
2199#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Neil Armstrong501c9322022-05-03 09:35:09 +02002200
Jerry Yu568ec252022-07-22 21:27:34 +08002201#if defined(MBEDTLS_SSL_PROTO_TLS1_3)
Gilles Peskine449bd832023-01-11 14:50:10 +01002202 if (ssl->tls_version == MBEDTLS_SSL_VERSION_TLS1_3) {
2203 alg = PSA_ALG_HKDF_EXTRACT(PSA_ALG_ANY_HASH);
2204 psa_set_key_usage_flags(&key_attributes,
2205 PSA_KEY_USAGE_DERIVE | PSA_KEY_USAGE_EXPORT);
Jerry Yuccc68a42022-07-26 16:39:20 +08002206 }
2207#endif /* MBEDTLS_SSL_PROTO_TLS1_3 */
2208
Gilles Peskine449bd832023-01-11 14:50:10 +01002209 psa_set_key_algorithm(&key_attributes, alg);
2210 psa_set_key_type(&key_attributes, PSA_KEY_TYPE_DERIVE);
Neil Armstrong501c9322022-05-03 09:35:09 +02002211
Gilles Peskine449bd832023-01-11 14:50:10 +01002212 status = psa_import_key(&key_attributes, psk, psk_len, &key);
2213 if (status != PSA_SUCCESS) {
2214 return MBEDTLS_ERR_SSL_HW_ACCEL_FAILED;
2215 }
Neil Armstrong501c9322022-05-03 09:35:09 +02002216
2217 /* Allow calling psa_destroy_key() on psk remove */
2218 ssl->handshake->psk_opaque_is_internal = 1;
Gilles Peskine449bd832023-01-11 14:50:10 +01002219 return mbedtls_ssl_set_hs_psk_opaque(ssl, key);
Neil Armstrong501c9322022-05-03 09:35:09 +02002220#else
Gilles Peskine449bd832023-01-11 14:50:10 +01002221 if ((ssl->handshake->psk = mbedtls_calloc(1, psk_len)) == NULL) {
2222 return MBEDTLS_ERR_SSL_ALLOC_FAILED;
2223 }
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01002224
2225 ssl->handshake->psk_len = psk_len;
Gilles Peskine449bd832023-01-11 14:50:10 +01002226 memcpy(ssl->handshake->psk, psk, ssl->handshake->psk_len);
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01002227
Gilles Peskine449bd832023-01-11 14:50:10 +01002228 return 0;
Neil Armstrong501c9322022-05-03 09:35:09 +02002229#endif /* MBEDTLS_USE_PSA_CRYPTO */
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01002230}
2231
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01002232#if defined(MBEDTLS_USE_PSA_CRYPTO)
Gilles Peskine449bd832023-01-11 14:50:10 +01002233int mbedtls_ssl_conf_psk_opaque(mbedtls_ssl_config *conf,
2234 mbedtls_svc_key_id_t psk,
2235 const unsigned char *psk_identity,
2236 size_t psk_identity_len)
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01002237{
Janos Follath865b3eb2019-12-16 11:46:15 +00002238 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Hanno Becker2ed3dce2021-04-19 21:59:14 +01002239
2240 /* We currently only support one PSK, raw or opaque. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002241 if (mbedtls_ssl_conf_has_static_psk(conf)) {
2242 return MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
2243 }
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01002244
Hanno Becker7390c712018-11-15 13:33:04 +00002245 /* Check and set opaque PSK */
Gilles Peskine449bd832023-01-11 14:50:10 +01002246 if (mbedtls_svc_key_id_is_null(psk)) {
2247 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
2248 }
Ronald Croncf56a0a2020-08-04 09:51:30 +02002249 conf->psk_opaque = psk;
Hanno Becker7390c712018-11-15 13:33:04 +00002250
2251 /* Check and set PSK Identity */
Gilles Peskine449bd832023-01-11 14:50:10 +01002252 ret = ssl_conf_set_psk_identity(conf, psk_identity,
2253 psk_identity_len);
2254 if (ret != 0) {
2255 ssl_conf_remove_psk(conf);
2256 }
Hanno Becker7390c712018-11-15 13:33:04 +00002257
Gilles Peskine449bd832023-01-11 14:50:10 +01002258 return ret;
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01002259}
2260
Gilles Peskine449bd832023-01-11 14:50:10 +01002261int mbedtls_ssl_set_hs_psk_opaque(mbedtls_ssl_context *ssl,
2262 mbedtls_svc_key_id_t psk)
Przemyslaw Stekiel6928a512022-02-03 13:50:35 +01002263{
Gilles Peskine449bd832023-01-11 14:50:10 +01002264 if ((mbedtls_svc_key_id_is_null(psk)) ||
2265 (ssl->handshake == NULL)) {
2266 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
2267 }
Przemyslaw Stekiel6928a512022-02-03 13:50:35 +01002268
Gilles Peskine449bd832023-01-11 14:50:10 +01002269 ssl_remove_psk(ssl);
Przemyslaw Stekiel6928a512022-02-03 13:50:35 +01002270 ssl->handshake->psk_opaque = psk;
Gilles Peskine449bd832023-01-11 14:50:10 +01002271 return 0;
Przemyslaw Stekiel6928a512022-02-03 13:50:35 +01002272}
2273#endif /* MBEDTLS_USE_PSA_CRYPTO */
2274
Jerry Yu8897c072022-08-12 13:56:53 +08002275#if defined(MBEDTLS_SSL_SRV_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01002276void mbedtls_ssl_conf_psk_cb(mbedtls_ssl_config *conf,
2277 int (*f_psk)(void *, mbedtls_ssl_context *, const unsigned char *,
2278 size_t),
2279 void *p_psk)
Przemyslaw Stekiel6928a512022-02-03 13:50:35 +01002280{
2281 conf->f_psk = f_psk;
2282 conf->p_psk = p_psk;
2283}
Jerry Yu8897c072022-08-12 13:56:53 +08002284#endif /* MBEDTLS_SSL_SRV_C */
2285
Ronald Cron73fe8df2022-10-05 14:31:43 +02002286#endif /* MBEDTLS_SSL_HANDSHAKE_WITH_PSK_ENABLED */
Przemyslaw Stekiel6928a512022-02-03 13:50:35 +01002287
2288#if defined(MBEDTLS_USE_PSA_CRYPTO)
Gilles Peskine301711e2022-04-26 16:57:05 +02002289static mbedtls_ssl_mode_t mbedtls_ssl_get_base_mode(
Gilles Peskine449bd832023-01-11 14:50:10 +01002290 psa_algorithm_t alg)
Neil Armstrong8a0f3e82022-03-30 10:57:37 +02002291{
Neil Armstrong8a0f3e82022-03-30 10:57:37 +02002292#if defined(MBEDTLS_SSL_SOME_SUITES_USE_MAC)
Gilles Peskine449bd832023-01-11 14:50:10 +01002293 if (alg == PSA_ALG_CBC_NO_PADDING) {
2294 return MBEDTLS_SSL_MODE_CBC;
2295 }
Neil Armstrong8a0f3e82022-03-30 10:57:37 +02002296#endif /* MBEDTLS_SSL_SOME_SUITES_USE_MAC */
Gilles Peskine449bd832023-01-11 14:50:10 +01002297 if (PSA_ALG_IS_AEAD(alg)) {
2298 return MBEDTLS_SSL_MODE_AEAD;
2299 }
2300 return MBEDTLS_SSL_MODE_STREAM;
Gilles Peskine301711e2022-04-26 16:57:05 +02002301}
2302
2303#else /* MBEDTLS_USE_PSA_CRYPTO */
2304
2305static mbedtls_ssl_mode_t mbedtls_ssl_get_base_mode(
Gilles Peskine449bd832023-01-11 14:50:10 +01002306 mbedtls_cipher_mode_t mode)
Gilles Peskine301711e2022-04-26 16:57:05 +02002307{
2308#if defined(MBEDTLS_SSL_SOME_SUITES_USE_MAC)
Gilles Peskine449bd832023-01-11 14:50:10 +01002309 if (mode == MBEDTLS_MODE_CBC) {
2310 return MBEDTLS_SSL_MODE_CBC;
2311 }
Gilles Peskine301711e2022-04-26 16:57:05 +02002312#endif /* MBEDTLS_SSL_SOME_SUITES_USE_MAC */
2313
Neil Armstrong8a0f3e82022-03-30 10:57:37 +02002314#if defined(MBEDTLS_GCM_C) || \
2315 defined(MBEDTLS_CCM_C) || \
2316 defined(MBEDTLS_CHACHAPOLY_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01002317 if (mode == MBEDTLS_MODE_GCM ||
Neil Armstrong8a0f3e82022-03-30 10:57:37 +02002318 mode == MBEDTLS_MODE_CCM ||
Gilles Peskine449bd832023-01-11 14:50:10 +01002319 mode == MBEDTLS_MODE_CHACHAPOLY) {
2320 return MBEDTLS_SSL_MODE_AEAD;
2321 }
Neil Armstrong8a0f3e82022-03-30 10:57:37 +02002322#endif /* MBEDTLS_GCM_C || MBEDTLS_CCM_C || MBEDTLS_CHACHAPOLY_C */
Neil Armstrong8a0f3e82022-03-30 10:57:37 +02002323
Gilles Peskine449bd832023-01-11 14:50:10 +01002324 return MBEDTLS_SSL_MODE_STREAM;
Neil Armstrong8a0f3e82022-03-30 10:57:37 +02002325}
Gilles Peskine301711e2022-04-26 16:57:05 +02002326#endif /* MBEDTLS_USE_PSA_CRYPTO */
Neil Armstrong8a0f3e82022-03-30 10:57:37 +02002327
Gilles Peskinee108d982022-04-26 16:50:40 +02002328static mbedtls_ssl_mode_t mbedtls_ssl_get_actual_mode(
2329 mbedtls_ssl_mode_t base_mode,
Gilles Peskine449bd832023-01-11 14:50:10 +01002330 int encrypt_then_mac)
Gilles Peskinee108d982022-04-26 16:50:40 +02002331{
2332#if defined(MBEDTLS_SSL_SOME_SUITES_USE_CBC_ETM)
Gilles Peskine449bd832023-01-11 14:50:10 +01002333 if (encrypt_then_mac == MBEDTLS_SSL_ETM_ENABLED &&
2334 base_mode == MBEDTLS_SSL_MODE_CBC) {
2335 return MBEDTLS_SSL_MODE_CBC_ETM;
Gilles Peskinee108d982022-04-26 16:50:40 +02002336 }
2337#else
2338 (void) encrypt_then_mac;
2339#endif
Gilles Peskine449bd832023-01-11 14:50:10 +01002340 return base_mode;
Gilles Peskinee108d982022-04-26 16:50:40 +02002341}
2342
Neil Armstrongab555e02022-04-04 11:07:59 +02002343mbedtls_ssl_mode_t mbedtls_ssl_get_mode_from_transform(
Gilles Peskine449bd832023-01-11 14:50:10 +01002344 const mbedtls_ssl_transform *transform)
Neil Armstrong4bf4c862022-04-01 10:35:48 +02002345{
Gilles Peskinee108d982022-04-26 16:50:40 +02002346 mbedtls_ssl_mode_t base_mode = mbedtls_ssl_get_base_mode(
Neil Armstrong4bf4c862022-04-01 10:35:48 +02002347#if defined(MBEDTLS_USE_PSA_CRYPTO)
Gilles Peskine449bd832023-01-11 14:50:10 +01002348 transform->psa_alg
Neil Armstrong4bf4c862022-04-01 10:35:48 +02002349#else
Gilles Peskine449bd832023-01-11 14:50:10 +01002350 mbedtls_cipher_get_cipher_mode(&transform->cipher_ctx_enc)
Gilles Peskinee108d982022-04-26 16:50:40 +02002351#endif
2352 );
Neil Armstrong4bf4c862022-04-01 10:35:48 +02002353
Gilles Peskinee108d982022-04-26 16:50:40 +02002354 int encrypt_then_mac = 0;
Neil Armstrongf2c82f02022-04-05 11:16:53 +02002355#if defined(MBEDTLS_SSL_SOME_SUITES_USE_CBC_ETM)
Gilles Peskinee108d982022-04-26 16:50:40 +02002356 encrypt_then_mac = transform->encrypt_then_mac;
2357#endif
Gilles Peskine449bd832023-01-11 14:50:10 +01002358 return mbedtls_ssl_get_actual_mode(base_mode, encrypt_then_mac);
Neil Armstrong4bf4c862022-04-01 10:35:48 +02002359}
2360
Neil Armstrongab555e02022-04-04 11:07:59 +02002361mbedtls_ssl_mode_t mbedtls_ssl_get_mode_from_ciphersuite(
Neil Armstrongf2c82f02022-04-05 11:16:53 +02002362#if defined(MBEDTLS_SSL_SOME_SUITES_USE_CBC_ETM)
Gilles Peskine449bd832023-01-11 14:50:10 +01002363 int encrypt_then_mac,
Neil Armstrongf2c82f02022-04-05 11:16:53 +02002364#endif /* MBEDTLS_SSL_SOME_SUITES_USE_CBC_ETM */
Gilles Peskine449bd832023-01-11 14:50:10 +01002365 const mbedtls_ssl_ciphersuite_t *suite)
Neil Armstrong4bf4c862022-04-01 10:35:48 +02002366{
Gilles Peskinee108d982022-04-26 16:50:40 +02002367 mbedtls_ssl_mode_t base_mode = MBEDTLS_SSL_MODE_STREAM;
2368
Neil Armstrong4bf4c862022-04-01 10:35:48 +02002369#if defined(MBEDTLS_USE_PSA_CRYPTO)
2370 psa_status_t status;
2371 psa_algorithm_t alg;
2372 psa_key_type_t type;
2373 size_t size;
Gilles Peskine449bd832023-01-11 14:50:10 +01002374 status = mbedtls_ssl_cipher_to_psa(suite->cipher, 0, &alg, &type, &size);
2375 if (status == PSA_SUCCESS) {
2376 base_mode = mbedtls_ssl_get_base_mode(alg);
2377 }
Neil Armstrong4bf4c862022-04-01 10:35:48 +02002378#else
2379 const mbedtls_cipher_info_t *cipher =
Gilles Peskine449bd832023-01-11 14:50:10 +01002380 mbedtls_cipher_info_from_type(suite->cipher);
2381 if (cipher != NULL) {
Gilles Peskinee108d982022-04-26 16:50:40 +02002382 base_mode =
2383 mbedtls_ssl_get_base_mode(
Gilles Peskine449bd832023-01-11 14:50:10 +01002384 mbedtls_cipher_info_get_mode(cipher));
Gilles Peskinee108d982022-04-26 16:50:40 +02002385 }
Neil Armstrong4bf4c862022-04-01 10:35:48 +02002386#endif /* MBEDTLS_USE_PSA_CRYPTO */
2387
Gilles Peskinee108d982022-04-26 16:50:40 +02002388#if !defined(MBEDTLS_SSL_SOME_SUITES_USE_CBC_ETM)
2389 int encrypt_then_mac = 0;
2390#endif
Gilles Peskine449bd832023-01-11 14:50:10 +01002391 return mbedtls_ssl_get_actual_mode(base_mode, encrypt_then_mac);
Neil Armstrong4bf4c862022-04-01 10:35:48 +02002392}
2393
Neil Armstrong8395d7a2022-05-18 11:44:56 +02002394#if defined(MBEDTLS_USE_PSA_CRYPTO) || defined(MBEDTLS_SSL_PROTO_TLS1_3)
Jerry Yu251a12e2022-07-13 15:15:48 +08002395
2396#if defined(MBEDTLS_SSL_PROTO_TLS1_3)
Jerry Yu438ddd82022-07-07 06:55:50 +00002397/* Serialization of TLS 1.3 sessions:
2398 *
2399 * struct {
Xiaokang Qian126bf8e2022-10-13 02:22:40 +00002400 * opaque hostname<0..2^16-1>;
Jerry Yu438ddd82022-07-07 06:55:50 +00002401 * uint64 ticket_received;
2402 * uint32 ticket_lifetime;
Jerry Yu34191072022-08-18 10:32:09 +08002403 * opaque ticket<1..2^16-1>;
Jerry Yu438ddd82022-07-07 06:55:50 +00002404 * } ClientOnlyData;
2405 *
2406 * struct {
2407 * uint8 endpoint;
2408 * uint8 ciphersuite[2];
2409 * uint32 ticket_age_add;
2410 * uint8 ticket_flags;
2411 * opaque resumption_key<0..255>;
2412 * select ( endpoint ) {
2413 * case client: ClientOnlyData;
2414 * case server: uint64 start_time;
2415 * };
2416 * } serialized_session_tls13;
2417 *
2418 */
2419#if defined(MBEDTLS_SSL_SESSION_TICKETS)
Jerry Yue36fdd62022-08-17 21:31:36 +08002420MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01002421static int ssl_tls13_session_save(const mbedtls_ssl_session *session,
2422 unsigned char *buf,
2423 size_t buf_len,
2424 size_t *olen)
Jerry Yu438ddd82022-07-07 06:55:50 +00002425{
2426 unsigned char *p = buf;
Xiaokang Qiana3b451f2022-10-11 06:20:56 +00002427#if defined(MBEDTLS_SSL_CLI_C) && \
2428 defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
Gilles Peskine449bd832023-01-11 14:50:10 +01002429 size_t hostname_len = (session->hostname == NULL) ?
2430 0 : strlen(session->hostname) + 1;
Xiaokang Qiana3b451f2022-10-11 06:20:56 +00002431#endif
Jerry Yubc7c1a42022-07-21 22:57:37 +08002432 size_t needed = 1 /* endpoint */
2433 + 2 /* ciphersuite */
2434 + 4 /* ticket_age_add */
Jerry Yu34191072022-08-18 10:32:09 +08002435 + 1 /* ticket_flags */
2436 + 1; /* resumption_key length */
Jerry Yue36fdd62022-08-17 21:31:36 +08002437 *olen = 0;
Jerry Yu34191072022-08-18 10:32:09 +08002438
Gilles Peskine449bd832023-01-11 14:50:10 +01002439 if (session->resumption_key_len > MBEDTLS_SSL_TLS1_3_TICKET_RESUMPTION_KEY_LEN) {
2440 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
2441 }
Jerry Yu34191072022-08-18 10:32:09 +08002442 needed += session->resumption_key_len; /* resumption_key */
2443
Jerry Yu438ddd82022-07-07 06:55:50 +00002444#if defined(MBEDTLS_HAVE_TIME)
2445 needed += 8; /* start_time or ticket_received */
2446#endif
2447
2448#if defined(MBEDTLS_SSL_CLI_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01002449 if (session->endpoint == MBEDTLS_SSL_IS_CLIENT) {
Xiaokang Qian126bf8e2022-10-13 02:22:40 +00002450#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
Xiaokang Qianbc663a02022-10-09 11:14:39 +00002451 needed += 2 /* hostname_len */
Gilles Peskine449bd832023-01-11 14:50:10 +01002452 + hostname_len; /* hostname */
Xiaokang Qian281fd1b2022-09-20 11:35:41 +00002453#endif
2454
Jerry Yu438ddd82022-07-07 06:55:50 +00002455 needed += 4 /* ticket_lifetime */
Jerry Yue36fdd62022-08-17 21:31:36 +08002456 + 2; /* ticket_len */
Jerry Yu34191072022-08-18 10:32:09 +08002457
2458 /* Check size_t overflow */
Gilles Peskine449bd832023-01-11 14:50:10 +01002459 if (session->ticket_len > SIZE_MAX - needed) {
2460 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
2461 }
Jerry Yu34191072022-08-18 10:32:09 +08002462
Jerry Yue28d9742022-08-18 15:44:03 +08002463 needed += session->ticket_len; /* ticket */
Jerry Yu438ddd82022-07-07 06:55:50 +00002464 }
2465#endif /* MBEDTLS_SSL_CLI_C */
2466
Jerry Yue36fdd62022-08-17 21:31:36 +08002467 *olen = needed;
Gilles Peskine449bd832023-01-11 14:50:10 +01002468 if (needed > buf_len) {
2469 return MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL;
2470 }
Jerry Yu438ddd82022-07-07 06:55:50 +00002471
2472 p[0] = session->endpoint;
Gilles Peskine449bd832023-01-11 14:50:10 +01002473 MBEDTLS_PUT_UINT16_BE(session->ciphersuite, p, 1);
2474 MBEDTLS_PUT_UINT32_BE(session->ticket_age_add, p, 3);
Jerry Yu438ddd82022-07-07 06:55:50 +00002475 p[7] = session->ticket_flags;
2476
2477 /* save resumption_key */
Jerry Yubc7c1a42022-07-21 22:57:37 +08002478 p[8] = session->resumption_key_len;
Jerry Yu438ddd82022-07-07 06:55:50 +00002479 p += 9;
Gilles Peskine449bd832023-01-11 14:50:10 +01002480 memcpy(p, session->resumption_key, session->resumption_key_len);
Jerry Yubc7c1a42022-07-21 22:57:37 +08002481 p += session->resumption_key_len;
Jerry Yu438ddd82022-07-07 06:55:50 +00002482
2483#if defined(MBEDTLS_HAVE_TIME) && defined(MBEDTLS_SSL_SRV_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01002484 if (session->endpoint == MBEDTLS_SSL_IS_SERVER) {
2485 MBEDTLS_PUT_UINT64_BE((uint64_t) session->start, p, 0);
Jerry Yu438ddd82022-07-07 06:55:50 +00002486 p += 8;
2487 }
2488#endif /* MBEDTLS_HAVE_TIME */
2489
2490#if defined(MBEDTLS_SSL_CLI_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01002491 if (session->endpoint == MBEDTLS_SSL_IS_CLIENT) {
Xiaokang Qianed0620c2022-10-12 06:58:13 +00002492#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
Gilles Peskine449bd832023-01-11 14:50:10 +01002493 MBEDTLS_PUT_UINT16_BE(hostname_len, p, 0);
Xiaokang Qianed0620c2022-10-12 06:58:13 +00002494 p += 2;
Gilles Peskine449bd832023-01-11 14:50:10 +01002495 if (hostname_len > 0) {
Xiaokang Qianed0620c2022-10-12 06:58:13 +00002496 /* save host name */
Gilles Peskine449bd832023-01-11 14:50:10 +01002497 memcpy(p, session->hostname, hostname_len);
Xiaokang Qianed0620c2022-10-12 06:58:13 +00002498 p += hostname_len;
2499 }
2500#endif /* MBEDTLS_SSL_SERVER_NAME_INDICATION */
2501
Jerry Yu438ddd82022-07-07 06:55:50 +00002502#if defined(MBEDTLS_HAVE_TIME)
Gilles Peskine449bd832023-01-11 14:50:10 +01002503 MBEDTLS_PUT_UINT64_BE((uint64_t) session->ticket_received, p, 0);
Jerry Yu438ddd82022-07-07 06:55:50 +00002504 p += 8;
2505#endif
Gilles Peskine449bd832023-01-11 14:50:10 +01002506 MBEDTLS_PUT_UINT32_BE(session->ticket_lifetime, p, 0);
Jerry Yu438ddd82022-07-07 06:55:50 +00002507 p += 4;
2508
Gilles Peskine449bd832023-01-11 14:50:10 +01002509 MBEDTLS_PUT_UINT16_BE(session->ticket_len, p, 0);
Jerry Yu438ddd82022-07-07 06:55:50 +00002510 p += 2;
Jerry Yu34191072022-08-18 10:32:09 +08002511
Gilles Peskine449bd832023-01-11 14:50:10 +01002512 if (session->ticket != NULL && session->ticket_len > 0) {
2513 memcpy(p, session->ticket, session->ticket_len);
Jerry Yu438ddd82022-07-07 06:55:50 +00002514 p += session->ticket_len;
2515 }
2516 }
2517#endif /* MBEDTLS_SSL_CLI_C */
Gilles Peskine449bd832023-01-11 14:50:10 +01002518 return 0;
Jerry Yu438ddd82022-07-07 06:55:50 +00002519}
2520
2521MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01002522static int ssl_tls13_session_load(mbedtls_ssl_session *session,
2523 const unsigned char *buf,
2524 size_t len)
Jerry Yu438ddd82022-07-07 06:55:50 +00002525{
2526 const unsigned char *p = buf;
2527 const unsigned char *end = buf + len;
2528
Gilles Peskine449bd832023-01-11 14:50:10 +01002529 if (end - p < 9) {
2530 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
2531 }
Jerry Yu438ddd82022-07-07 06:55:50 +00002532 session->endpoint = p[0];
Gilles Peskine449bd832023-01-11 14:50:10 +01002533 session->ciphersuite = MBEDTLS_GET_UINT16_BE(p, 1);
2534 session->ticket_age_add = MBEDTLS_GET_UINT32_BE(p, 3);
Jerry Yu438ddd82022-07-07 06:55:50 +00002535 session->ticket_flags = p[7];
2536
2537 /* load resumption_key */
Jerry Yubc7c1a42022-07-21 22:57:37 +08002538 session->resumption_key_len = p[8];
Jerry Yu438ddd82022-07-07 06:55:50 +00002539 p += 9;
2540
Gilles Peskine449bd832023-01-11 14:50:10 +01002541 if (end - p < session->resumption_key_len) {
2542 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
2543 }
Jerry Yu438ddd82022-07-07 06:55:50 +00002544
Gilles Peskine449bd832023-01-11 14:50:10 +01002545 if (sizeof(session->resumption_key) < session->resumption_key_len) {
2546 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
2547 }
2548 memcpy(session->resumption_key, p, session->resumption_key_len);
Jerry Yubc7c1a42022-07-21 22:57:37 +08002549 p += session->resumption_key_len;
Jerry Yu438ddd82022-07-07 06:55:50 +00002550
2551#if defined(MBEDTLS_HAVE_TIME) && defined(MBEDTLS_SSL_SRV_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01002552 if (session->endpoint == MBEDTLS_SSL_IS_SERVER) {
2553 if (end - p < 8) {
2554 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
2555 }
2556 session->start = MBEDTLS_GET_UINT64_BE(p, 0);
Jerry Yu438ddd82022-07-07 06:55:50 +00002557 p += 8;
2558 }
2559#endif /* MBEDTLS_HAVE_TIME */
2560
2561#if defined(MBEDTLS_SSL_CLI_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01002562 if (session->endpoint == MBEDTLS_SSL_IS_CLIENT) {
Xiaokang Qianed0620c2022-10-12 06:58:13 +00002563#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION) && \
Gilles Peskine449bd832023-01-11 14:50:10 +01002564 defined(MBEDTLS_SSL_SESSION_TICKETS)
Xiaokang Qianed0620c2022-10-12 06:58:13 +00002565 size_t hostname_len;
2566 /* load host name */
Gilles Peskine449bd832023-01-11 14:50:10 +01002567 if (end - p < 2) {
2568 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
2569 }
2570 hostname_len = MBEDTLS_GET_UINT16_BE(p, 0);
Xiaokang Qianed0620c2022-10-12 06:58:13 +00002571 p += 2;
2572
Gilles Peskine449bd832023-01-11 14:50:10 +01002573 if (end - p < (long int) hostname_len) {
2574 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
2575 }
2576 if (hostname_len > 0) {
2577 session->hostname = mbedtls_calloc(1, hostname_len);
2578 if (session->hostname == NULL) {
2579 return MBEDTLS_ERR_SSL_ALLOC_FAILED;
2580 }
2581 memcpy(session->hostname, p, hostname_len);
Xiaokang Qianed0620c2022-10-12 06:58:13 +00002582 p += hostname_len;
2583 }
2584#endif /* MBEDTLS_SSL_SERVER_NAME_INDICATION &&
2585 MBEDTLS_SSL_SESSION_TICKETS */
2586
Jerry Yu438ddd82022-07-07 06:55:50 +00002587#if defined(MBEDTLS_HAVE_TIME)
Gilles Peskine449bd832023-01-11 14:50:10 +01002588 if (end - p < 8) {
2589 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
2590 }
2591 session->ticket_received = MBEDTLS_GET_UINT64_BE(p, 0);
Jerry Yu438ddd82022-07-07 06:55:50 +00002592 p += 8;
2593#endif
Gilles Peskine449bd832023-01-11 14:50:10 +01002594 if (end - p < 4) {
2595 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
2596 }
2597 session->ticket_lifetime = MBEDTLS_GET_UINT32_BE(p, 0);
Jerry Yu438ddd82022-07-07 06:55:50 +00002598 p += 4;
2599
Gilles Peskine449bd832023-01-11 14:50:10 +01002600 if (end - p < 2) {
2601 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
2602 }
2603 session->ticket_len = MBEDTLS_GET_UINT16_BE(p, 0);
Jerry Yu438ddd82022-07-07 06:55:50 +00002604 p += 2;
2605
Gilles Peskine449bd832023-01-11 14:50:10 +01002606 if (end - p < (long int) session->ticket_len) {
2607 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
2608 }
2609 if (session->ticket_len > 0) {
2610 session->ticket = mbedtls_calloc(1, session->ticket_len);
2611 if (session->ticket == NULL) {
2612 return MBEDTLS_ERR_SSL_ALLOC_FAILED;
2613 }
2614 memcpy(session->ticket, p, session->ticket_len);
Jerry Yu438ddd82022-07-07 06:55:50 +00002615 p += session->ticket_len;
2616 }
2617 }
2618#endif /* MBEDTLS_SSL_CLI_C */
2619
Gilles Peskine449bd832023-01-11 14:50:10 +01002620 return 0;
Jerry Yu438ddd82022-07-07 06:55:50 +00002621
2622}
2623#else /* MBEDTLS_SSL_SESSION_TICKETS */
Jerry Yue36fdd62022-08-17 21:31:36 +08002624MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01002625static int ssl_tls13_session_save(const mbedtls_ssl_session *session,
2626 unsigned char *buf,
2627 size_t buf_len,
2628 size_t *olen)
Jerry Yu251a12e2022-07-13 15:15:48 +08002629{
2630 ((void) session);
2631 ((void) buf);
2632 ((void) buf_len);
Jerry Yue36fdd62022-08-17 21:31:36 +08002633 *olen = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +01002634 return MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
Jerry Yu251a12e2022-07-13 15:15:48 +08002635}
Jerry Yu438ddd82022-07-07 06:55:50 +00002636
Gilles Peskine449bd832023-01-11 14:50:10 +01002637static int ssl_tls13_session_load(const mbedtls_ssl_session *session,
2638 unsigned char *buf,
2639 size_t buf_len)
Jerry Yu438ddd82022-07-07 06:55:50 +00002640{
2641 ((void) session);
2642 ((void) buf);
2643 ((void) buf_len);
Gilles Peskine449bd832023-01-11 14:50:10 +01002644 return MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
Jerry Yu438ddd82022-07-07 06:55:50 +00002645}
2646#endif /* !MBEDTLS_SSL_SESSION_TICKETS */
Jerry Yu251a12e2022-07-13 15:15:48 +08002647#endif /* MBEDTLS_SSL_PROTO_TLS1_3 */
2648
Gilles Peskine449bd832023-01-11 14:50:10 +01002649psa_status_t mbedtls_ssl_cipher_to_psa(mbedtls_cipher_type_t mbedtls_cipher_type,
2650 size_t taglen,
2651 psa_algorithm_t *alg,
2652 psa_key_type_t *key_type,
2653 size_t *key_size)
Przemyslaw Stekiel430f3372022-01-10 11:55:46 +01002654{
Gilles Peskine449bd832023-01-11 14:50:10 +01002655 switch (mbedtls_cipher_type) {
Przemyslaw Stekiel430f3372022-01-10 11:55:46 +01002656 case MBEDTLS_CIPHER_AES_128_CBC:
2657 *alg = PSA_ALG_CBC_NO_PADDING;
2658 *key_type = PSA_KEY_TYPE_AES;
2659 *key_size = 128;
2660 break;
2661 case MBEDTLS_CIPHER_AES_128_CCM:
Gilles Peskine449bd832023-01-11 14:50:10 +01002662 *alg = taglen ? PSA_ALG_AEAD_WITH_SHORTENED_TAG(PSA_ALG_CCM, taglen) : PSA_ALG_CCM;
Przemyslaw Stekiel430f3372022-01-10 11:55:46 +01002663 *key_type = PSA_KEY_TYPE_AES;
2664 *key_size = 128;
2665 break;
2666 case MBEDTLS_CIPHER_AES_128_GCM:
2667 *alg = PSA_ALG_GCM;
2668 *key_type = PSA_KEY_TYPE_AES;
2669 *key_size = 128;
2670 break;
2671 case MBEDTLS_CIPHER_AES_192_CCM:
Gilles Peskine449bd832023-01-11 14:50:10 +01002672 *alg = taglen ? PSA_ALG_AEAD_WITH_SHORTENED_TAG(PSA_ALG_CCM, taglen) : PSA_ALG_CCM;
Przemyslaw Stekiel430f3372022-01-10 11:55:46 +01002673 *key_type = PSA_KEY_TYPE_AES;
2674 *key_size = 192;
2675 break;
2676 case MBEDTLS_CIPHER_AES_192_GCM:
2677 *alg = PSA_ALG_GCM;
2678 *key_type = PSA_KEY_TYPE_AES;
2679 *key_size = 192;
2680 break;
2681 case MBEDTLS_CIPHER_AES_256_CBC:
2682 *alg = PSA_ALG_CBC_NO_PADDING;
2683 *key_type = PSA_KEY_TYPE_AES;
2684 *key_size = 256;
2685 break;
2686 case MBEDTLS_CIPHER_AES_256_CCM:
Gilles Peskine449bd832023-01-11 14:50:10 +01002687 *alg = taglen ? PSA_ALG_AEAD_WITH_SHORTENED_TAG(PSA_ALG_CCM, taglen) : PSA_ALG_CCM;
Przemyslaw Stekiel430f3372022-01-10 11:55:46 +01002688 *key_type = PSA_KEY_TYPE_AES;
2689 *key_size = 256;
2690 break;
2691 case MBEDTLS_CIPHER_AES_256_GCM:
2692 *alg = PSA_ALG_GCM;
2693 *key_type = PSA_KEY_TYPE_AES;
2694 *key_size = 256;
2695 break;
2696 case MBEDTLS_CIPHER_ARIA_128_CBC:
2697 *alg = PSA_ALG_CBC_NO_PADDING;
2698 *key_type = PSA_KEY_TYPE_ARIA;
2699 *key_size = 128;
2700 break;
2701 case MBEDTLS_CIPHER_ARIA_128_CCM:
Gilles Peskine449bd832023-01-11 14:50:10 +01002702 *alg = taglen ? PSA_ALG_AEAD_WITH_SHORTENED_TAG(PSA_ALG_CCM, taglen) : PSA_ALG_CCM;
Przemyslaw Stekiel430f3372022-01-10 11:55:46 +01002703 *key_type = PSA_KEY_TYPE_ARIA;
2704 *key_size = 128;
2705 break;
2706 case MBEDTLS_CIPHER_ARIA_128_GCM:
2707 *alg = PSA_ALG_GCM;
2708 *key_type = PSA_KEY_TYPE_ARIA;
2709 *key_size = 128;
2710 break;
2711 case MBEDTLS_CIPHER_ARIA_192_CCM:
Gilles Peskine449bd832023-01-11 14:50:10 +01002712 *alg = taglen ? PSA_ALG_AEAD_WITH_SHORTENED_TAG(PSA_ALG_CCM, taglen) : PSA_ALG_CCM;
Przemyslaw Stekiel430f3372022-01-10 11:55:46 +01002713 *key_type = PSA_KEY_TYPE_ARIA;
2714 *key_size = 192;
2715 break;
2716 case MBEDTLS_CIPHER_ARIA_192_GCM:
2717 *alg = PSA_ALG_GCM;
2718 *key_type = PSA_KEY_TYPE_ARIA;
2719 *key_size = 192;
2720 break;
2721 case MBEDTLS_CIPHER_ARIA_256_CBC:
2722 *alg = PSA_ALG_CBC_NO_PADDING;
2723 *key_type = PSA_KEY_TYPE_ARIA;
2724 *key_size = 256;
2725 break;
2726 case MBEDTLS_CIPHER_ARIA_256_CCM:
Gilles Peskine449bd832023-01-11 14:50:10 +01002727 *alg = taglen ? PSA_ALG_AEAD_WITH_SHORTENED_TAG(PSA_ALG_CCM, taglen) : PSA_ALG_CCM;
Przemyslaw Stekiel430f3372022-01-10 11:55:46 +01002728 *key_type = PSA_KEY_TYPE_ARIA;
2729 *key_size = 256;
2730 break;
2731 case MBEDTLS_CIPHER_ARIA_256_GCM:
2732 *alg = PSA_ALG_GCM;
2733 *key_type = PSA_KEY_TYPE_ARIA;
2734 *key_size = 256;
2735 break;
2736 case MBEDTLS_CIPHER_CAMELLIA_128_CBC:
2737 *alg = PSA_ALG_CBC_NO_PADDING;
2738 *key_type = PSA_KEY_TYPE_CAMELLIA;
2739 *key_size = 128;
2740 break;
2741 case MBEDTLS_CIPHER_CAMELLIA_128_CCM:
Gilles Peskine449bd832023-01-11 14:50:10 +01002742 *alg = taglen ? PSA_ALG_AEAD_WITH_SHORTENED_TAG(PSA_ALG_CCM, taglen) : PSA_ALG_CCM;
Przemyslaw Stekiel430f3372022-01-10 11:55:46 +01002743 *key_type = PSA_KEY_TYPE_CAMELLIA;
2744 *key_size = 128;
2745 break;
2746 case MBEDTLS_CIPHER_CAMELLIA_128_GCM:
2747 *alg = PSA_ALG_GCM;
2748 *key_type = PSA_KEY_TYPE_CAMELLIA;
2749 *key_size = 128;
2750 break;
2751 case MBEDTLS_CIPHER_CAMELLIA_192_CCM:
Gilles Peskine449bd832023-01-11 14:50:10 +01002752 *alg = taglen ? PSA_ALG_AEAD_WITH_SHORTENED_TAG(PSA_ALG_CCM, taglen) : PSA_ALG_CCM;
Przemyslaw Stekiel430f3372022-01-10 11:55:46 +01002753 *key_type = PSA_KEY_TYPE_CAMELLIA;
2754 *key_size = 192;
2755 break;
2756 case MBEDTLS_CIPHER_CAMELLIA_192_GCM:
2757 *alg = PSA_ALG_GCM;
2758 *key_type = PSA_KEY_TYPE_CAMELLIA;
2759 *key_size = 192;
2760 break;
2761 case MBEDTLS_CIPHER_CAMELLIA_256_CBC:
2762 *alg = PSA_ALG_CBC_NO_PADDING;
2763 *key_type = PSA_KEY_TYPE_CAMELLIA;
2764 *key_size = 256;
2765 break;
2766 case MBEDTLS_CIPHER_CAMELLIA_256_CCM:
Gilles Peskine449bd832023-01-11 14:50:10 +01002767 *alg = taglen ? PSA_ALG_AEAD_WITH_SHORTENED_TAG(PSA_ALG_CCM, taglen) : PSA_ALG_CCM;
Przemyslaw Stekiel430f3372022-01-10 11:55:46 +01002768 *key_type = PSA_KEY_TYPE_CAMELLIA;
2769 *key_size = 256;
2770 break;
2771 case MBEDTLS_CIPHER_CAMELLIA_256_GCM:
2772 *alg = PSA_ALG_GCM;
2773 *key_type = PSA_KEY_TYPE_CAMELLIA;
2774 *key_size = 256;
2775 break;
2776 case MBEDTLS_CIPHER_CHACHA20_POLY1305:
2777 *alg = PSA_ALG_CHACHA20_POLY1305;
2778 *key_type = PSA_KEY_TYPE_CHACHA20;
2779 *key_size = 256;
2780 break;
2781 case MBEDTLS_CIPHER_NULL:
2782 *alg = MBEDTLS_SSL_NULL_CIPHER;
2783 *key_type = 0;
2784 *key_size = 0;
2785 break;
2786 default:
2787 return PSA_ERROR_NOT_SUPPORTED;
2788 }
2789
2790 return PSA_SUCCESS;
2791}
Neil Armstrong8395d7a2022-05-18 11:44:56 +02002792#endif /* MBEDTLS_USE_PSA_CRYPTO || MBEDTLS_SSL_PROTO_TLS1_3 */
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01002793
Manuel Pégourié-Gonnardcf141ca2015-05-20 10:35:51 +02002794#if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_SSL_SRV_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01002795int mbedtls_ssl_conf_dh_param_bin(mbedtls_ssl_config *conf,
2796 const unsigned char *dhm_P, size_t P_len,
2797 const unsigned char *dhm_G, size_t G_len)
Hanno Beckera90658f2017-10-04 15:29:08 +01002798{
Janos Follath865b3eb2019-12-16 11:46:15 +00002799 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Hanno Beckera90658f2017-10-04 15:29:08 +01002800
Gilles Peskine449bd832023-01-11 14:50:10 +01002801 mbedtls_mpi_free(&conf->dhm_P);
2802 mbedtls_mpi_free(&conf->dhm_G);
Glenn Strausscee11292021-12-20 01:43:17 -05002803
Gilles Peskine449bd832023-01-11 14:50:10 +01002804 if ((ret = mbedtls_mpi_read_binary(&conf->dhm_P, dhm_P, P_len)) != 0 ||
2805 (ret = mbedtls_mpi_read_binary(&conf->dhm_G, dhm_G, G_len)) != 0) {
2806 mbedtls_mpi_free(&conf->dhm_P);
2807 mbedtls_mpi_free(&conf->dhm_G);
2808 return ret;
Hanno Beckera90658f2017-10-04 15:29:08 +01002809 }
2810
Gilles Peskine449bd832023-01-11 14:50:10 +01002811 return 0;
Hanno Beckera90658f2017-10-04 15:29:08 +01002812}
Paul Bakker5121ce52009-01-03 21:22:43 +00002813
Gilles Peskine449bd832023-01-11 14:50:10 +01002814int mbedtls_ssl_conf_dh_param_ctx(mbedtls_ssl_config *conf, mbedtls_dhm_context *dhm_ctx)
Paul Bakker1b57b062011-01-06 15:48:19 +00002815{
Janos Follath865b3eb2019-12-16 11:46:15 +00002816 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker1b57b062011-01-06 15:48:19 +00002817
Gilles Peskine449bd832023-01-11 14:50:10 +01002818 mbedtls_mpi_free(&conf->dhm_P);
2819 mbedtls_mpi_free(&conf->dhm_G);
Glenn Strausscee11292021-12-20 01:43:17 -05002820
Gilles Peskine449bd832023-01-11 14:50:10 +01002821 if ((ret = mbedtls_dhm_get_value(dhm_ctx, MBEDTLS_DHM_PARAM_P,
2822 &conf->dhm_P)) != 0 ||
2823 (ret = mbedtls_dhm_get_value(dhm_ctx, MBEDTLS_DHM_PARAM_G,
2824 &conf->dhm_G)) != 0) {
2825 mbedtls_mpi_free(&conf->dhm_P);
2826 mbedtls_mpi_free(&conf->dhm_G);
2827 return ret;
Manuel Pégourié-Gonnard1028b742015-05-06 17:33:07 +01002828 }
Paul Bakker1b57b062011-01-06 15:48:19 +00002829
Gilles Peskine449bd832023-01-11 14:50:10 +01002830 return 0;
Paul Bakker1b57b062011-01-06 15:48:19 +00002831}
Manuel Pégourié-Gonnardcf141ca2015-05-20 10:35:51 +02002832#endif /* MBEDTLS_DHM_C && MBEDTLS_SSL_SRV_C */
Paul Bakker1b57b062011-01-06 15:48:19 +00002833
Manuel Pégourié-Gonnardbd990d62015-06-11 14:49:42 +02002834#if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_SSL_CLI_C)
2835/*
2836 * Set the minimum length for Diffie-Hellman parameters
2837 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002838void mbedtls_ssl_conf_dhm_min_bitlen(mbedtls_ssl_config *conf,
2839 unsigned int bitlen)
Manuel Pégourié-Gonnardbd990d62015-06-11 14:49:42 +02002840{
2841 conf->dhm_min_bitlen = bitlen;
2842}
2843#endif /* MBEDTLS_DHM_C && MBEDTLS_SSL_CLI_C */
2844
Ronald Crone68ab4f2022-10-05 12:46:29 +02002845#if defined(MBEDTLS_SSL_HANDSHAKE_WITH_CERT_ENABLED)
Jerry Yu7ddc38c2022-01-19 11:08:05 +08002846#if !defined(MBEDTLS_DEPRECATED_REMOVED) && defined(MBEDTLS_SSL_PROTO_TLS1_2)
Manuel Pégourié-Gonnard36a8b572015-06-17 12:43:26 +02002847/*
2848 * Set allowed/preferred hashes for handshake signatures
2849 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002850void mbedtls_ssl_conf_sig_hashes(mbedtls_ssl_config *conf,
2851 const int *hashes)
Manuel Pégourié-Gonnard36a8b572015-06-17 12:43:26 +02002852{
2853 conf->sig_hashes = hashes;
2854}
Jerry Yu7ddc38c2022-01-19 11:08:05 +08002855#endif /* !MBEDTLS_DEPRECATED_REMOVED && MBEDTLS_SSL_PROTO_TLS1_2 */
Hanno Becker1cd6e002021-08-10 13:27:10 +01002856
Jerry Yuf017ee42022-01-12 15:49:48 +08002857/* Configure allowed signature algorithms for handshake */
Gilles Peskine449bd832023-01-11 14:50:10 +01002858void mbedtls_ssl_conf_sig_algs(mbedtls_ssl_config *conf,
2859 const uint16_t *sig_algs)
Hanno Becker1cd6e002021-08-10 13:27:10 +01002860{
Jerry Yuf017ee42022-01-12 15:49:48 +08002861#if !defined(MBEDTLS_DEPRECATED_REMOVED)
2862 conf->sig_hashes = NULL;
2863#endif /* !MBEDTLS_DEPRECATED_REMOVED */
2864 conf->sig_algs = sig_algs;
Hanno Becker1cd6e002021-08-10 13:27:10 +01002865}
Ronald Crone68ab4f2022-10-05 12:46:29 +02002866#endif /* MBEDTLS_SSL_HANDSHAKE_WITH_CERT_ENABLED */
Manuel Pégourié-Gonnard36a8b572015-06-17 12:43:26 +02002867
Manuel Pégourié-Gonnardb541da62015-06-17 11:43:30 +02002868#if defined(MBEDTLS_ECP_C)
Brett Warrene0edc842021-08-17 09:53:13 +01002869#if !defined(MBEDTLS_DEPRECATED_REMOVED)
Manuel Pégourié-Gonnard7f38ed02014-02-04 15:52:33 +01002870/*
2871 * Set the allowed elliptic curves
Brett Warrene0edc842021-08-17 09:53:13 +01002872 *
2873 * mbedtls_ssl_setup() takes the provided list
2874 * and translates it to a list of IANA TLS group identifiers,
2875 * stored in ssl->handshake->group_list.
2876 *
Manuel Pégourié-Gonnard7f38ed02014-02-04 15:52:33 +01002877 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002878void mbedtls_ssl_conf_curves(mbedtls_ssl_config *conf,
2879 const mbedtls_ecp_group_id *curve_list)
Manuel Pégourié-Gonnard7f38ed02014-02-04 15:52:33 +01002880{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02002881 conf->curve_list = curve_list;
Brett Warrene0edc842021-08-17 09:53:13 +01002882 conf->group_list = NULL;
Manuel Pégourié-Gonnard7f38ed02014-02-04 15:52:33 +01002883}
Brett Warrene0edc842021-08-17 09:53:13 +01002884#endif /* MBEDTLS_DEPRECATED_REMOVED */
Hanno Becker947194e2017-04-07 13:25:49 +01002885#endif /* MBEDTLS_ECP_C */
Manuel Pégourié-Gonnard7f38ed02014-02-04 15:52:33 +01002886
Brett Warrene0edc842021-08-17 09:53:13 +01002887/*
2888 * Set the allowed groups
2889 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002890void mbedtls_ssl_conf_groups(mbedtls_ssl_config *conf,
2891 const uint16_t *group_list)
Brett Warrene0edc842021-08-17 09:53:13 +01002892{
2893#if defined(MBEDTLS_ECP_C) && !defined(MBEDTLS_DEPRECATED_REMOVED)
2894 conf->curve_list = NULL;
2895#endif
2896 conf->group_list = group_list;
2897}
2898
Manuel Pégourié-Gonnardbc2b7712015-05-06 11:14:19 +01002899#if defined(MBEDTLS_X509_CRT_PARSE_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01002900int mbedtls_ssl_set_hostname(mbedtls_ssl_context *ssl, const char *hostname)
Paul Bakker5121ce52009-01-03 21:22:43 +00002901{
Hanno Becker947194e2017-04-07 13:25:49 +01002902 /* Initialize to suppress unnecessary compiler warning */
2903 size_t hostname_len = 0;
2904
2905 /* Check if new hostname is valid before
2906 * making any change to current one */
Gilles Peskine449bd832023-01-11 14:50:10 +01002907 if (hostname != NULL) {
2908 hostname_len = strlen(hostname);
Hanno Becker947194e2017-04-07 13:25:49 +01002909
Gilles Peskine449bd832023-01-11 14:50:10 +01002910 if (hostname_len > MBEDTLS_SSL_MAX_HOST_NAME_LEN) {
2911 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
2912 }
Hanno Becker947194e2017-04-07 13:25:49 +01002913 }
2914
2915 /* Now it's clear that we will overwrite the old hostname,
2916 * so we can free it safely */
2917
Gilles Peskine449bd832023-01-11 14:50:10 +01002918 if (ssl->hostname != NULL) {
2919 mbedtls_platform_zeroize(ssl->hostname, strlen(ssl->hostname));
2920 mbedtls_free(ssl->hostname);
Hanno Becker947194e2017-04-07 13:25:49 +01002921 }
2922
2923 /* Passing NULL as hostname shall clear the old one */
Manuel Pégourié-Gonnardba26c242015-05-06 10:47:06 +01002924
Gilles Peskine449bd832023-01-11 14:50:10 +01002925 if (hostname == NULL) {
Hanno Becker947194e2017-04-07 13:25:49 +01002926 ssl->hostname = NULL;
Gilles Peskine449bd832023-01-11 14:50:10 +01002927 } else {
2928 ssl->hostname = mbedtls_calloc(1, hostname_len + 1);
2929 if (ssl->hostname == NULL) {
2930 return MBEDTLS_ERR_SSL_ALLOC_FAILED;
2931 }
Paul Bakker75c1a6f2013-08-19 14:25:29 +02002932
Gilles Peskine449bd832023-01-11 14:50:10 +01002933 memcpy(ssl->hostname, hostname, hostname_len);
Paul Bakker75c1a6f2013-08-19 14:25:29 +02002934
Hanno Becker947194e2017-04-07 13:25:49 +01002935 ssl->hostname[hostname_len] = '\0';
2936 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002937
Gilles Peskine449bd832023-01-11 14:50:10 +01002938 return 0;
Paul Bakker5121ce52009-01-03 21:22:43 +00002939}
Hanno Becker1a9a51c2017-04-07 13:02:16 +01002940#endif /* MBEDTLS_X509_CRT_PARSE_C */
Paul Bakker5121ce52009-01-03 21:22:43 +00002941
Manuel Pégourié-Gonnardbc2b7712015-05-06 11:14:19 +01002942#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
Gilles Peskine449bd832023-01-11 14:50:10 +01002943void mbedtls_ssl_conf_sni(mbedtls_ssl_config *conf,
2944 int (*f_sni)(void *, mbedtls_ssl_context *,
2945 const unsigned char *, size_t),
2946 void *p_sni)
Paul Bakker5701cdc2012-09-27 21:49:42 +00002947{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02002948 conf->f_sni = f_sni;
2949 conf->p_sni = p_sni;
Paul Bakker5701cdc2012-09-27 21:49:42 +00002950}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002951#endif /* MBEDTLS_SSL_SERVER_NAME_INDICATION */
Paul Bakker5701cdc2012-09-27 21:49:42 +00002952
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002953#if defined(MBEDTLS_SSL_ALPN)
Gilles Peskine449bd832023-01-11 14:50:10 +01002954int mbedtls_ssl_conf_alpn_protocols(mbedtls_ssl_config *conf, const char **protos)
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02002955{
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +02002956 size_t cur_len, tot_len;
2957 const char **p;
2958
2959 /*
Brian J Murray1903fb32016-11-06 04:45:15 -08002960 * RFC 7301 3.1: "Empty strings MUST NOT be included and byte strings
2961 * MUST NOT be truncated."
2962 * We check lengths now rather than later.
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +02002963 */
2964 tot_len = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +01002965 for (p = protos; *p != NULL; p++) {
2966 cur_len = strlen(*p);
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +02002967 tot_len += cur_len;
2968
Gilles Peskine449bd832023-01-11 14:50:10 +01002969 if ((cur_len == 0) ||
2970 (cur_len > MBEDTLS_SSL_MAX_ALPN_NAME_LEN) ||
2971 (tot_len > MBEDTLS_SSL_MAX_ALPN_LIST_LEN)) {
2972 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
2973 }
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +02002974 }
2975
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02002976 conf->alpn_list = protos;
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +02002977
Gilles Peskine449bd832023-01-11 14:50:10 +01002978 return 0;
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02002979}
2980
Gilles Peskine449bd832023-01-11 14:50:10 +01002981const char *mbedtls_ssl_get_alpn_protocol(const mbedtls_ssl_context *ssl)
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02002982{
Gilles Peskine449bd832023-01-11 14:50:10 +01002983 return ssl->alpn_chosen;
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02002984}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002985#endif /* MBEDTLS_SSL_ALPN */
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02002986
Johan Pascalb62bb512015-12-03 21:56:45 +01002987#if defined(MBEDTLS_SSL_DTLS_SRTP)
Gilles Peskine449bd832023-01-11 14:50:10 +01002988void mbedtls_ssl_conf_srtp_mki_value_supported(mbedtls_ssl_config *conf,
2989 int support_mki_value)
Ron Eldor591f1622018-01-22 12:30:04 +02002990{
2991 conf->dtls_srtp_mki_support = support_mki_value;
2992}
2993
Gilles Peskine449bd832023-01-11 14:50:10 +01002994int mbedtls_ssl_dtls_srtp_set_mki_value(mbedtls_ssl_context *ssl,
2995 unsigned char *mki_value,
2996 uint16_t mki_len)
Ron Eldor591f1622018-01-22 12:30:04 +02002997{
Gilles Peskine449bd832023-01-11 14:50:10 +01002998 if (mki_len > MBEDTLS_TLS_SRTP_MAX_MKI_LENGTH) {
2999 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
Ron Eldora9788042018-12-05 11:04:31 +02003000 }
Ron Eldor591f1622018-01-22 12:30:04 +02003001
Gilles Peskine449bd832023-01-11 14:50:10 +01003002 if (ssl->conf->dtls_srtp_mki_support == MBEDTLS_SSL_DTLS_SRTP_MKI_UNSUPPORTED) {
3003 return MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
Ron Eldora9788042018-12-05 11:04:31 +02003004 }
Ron Eldor591f1622018-01-22 12:30:04 +02003005
Gilles Peskine449bd832023-01-11 14:50:10 +01003006 memcpy(ssl->dtls_srtp_info.mki_value, mki_value, mki_len);
Ron Eldor591f1622018-01-22 12:30:04 +02003007 ssl->dtls_srtp_info.mki_len = mki_len;
Gilles Peskine449bd832023-01-11 14:50:10 +01003008 return 0;
Ron Eldor591f1622018-01-22 12:30:04 +02003009}
3010
Gilles Peskine449bd832023-01-11 14:50:10 +01003011int mbedtls_ssl_conf_dtls_srtp_protection_profiles(mbedtls_ssl_config *conf,
3012 const mbedtls_ssl_srtp_profile *profiles)
Johan Pascalb62bb512015-12-03 21:56:45 +01003013{
Johan Pascal253d0262020-09-22 13:04:45 +02003014 const mbedtls_ssl_srtp_profile *p;
3015 size_t list_size = 0;
Johan Pascalb62bb512015-12-03 21:56:45 +01003016
Johan Pascal253d0262020-09-22 13:04:45 +02003017 /* check the profiles list: all entry must be valid,
3018 * its size cannot be more than the total number of supported profiles, currently 4 */
Gilles Peskine449bd832023-01-11 14:50:10 +01003019 for (p = profiles; *p != MBEDTLS_TLS_SRTP_UNSET &&
3020 list_size <= MBEDTLS_TLS_SRTP_MAX_PROFILE_LIST_LENGTH;
3021 p++) {
3022 if (mbedtls_ssl_check_srtp_profile_value(*p) != MBEDTLS_TLS_SRTP_UNSET) {
Johan Pascal76fdf1d2020-10-22 23:31:00 +02003023 list_size++;
Gilles Peskine449bd832023-01-11 14:50:10 +01003024 } else {
Johan Pascal76fdf1d2020-10-22 23:31:00 +02003025 /* unsupported value, stop parsing and set the size to an error value */
3026 list_size = MBEDTLS_TLS_SRTP_MAX_PROFILE_LIST_LENGTH + 1;
Johan Pascalb62bb512015-12-03 21:56:45 +01003027 }
3028 }
3029
Gilles Peskine449bd832023-01-11 14:50:10 +01003030 if (list_size > MBEDTLS_TLS_SRTP_MAX_PROFILE_LIST_LENGTH) {
3031 conf->dtls_srtp_profile_list = NULL;
3032 conf->dtls_srtp_profile_list_len = 0;
3033 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
Johan Pascal253d0262020-09-22 13:04:45 +02003034 }
3035
Johan Pascal9bc97ca2020-09-21 23:44:45 +02003036 conf->dtls_srtp_profile_list = profiles;
Johan Pascal253d0262020-09-22 13:04:45 +02003037 conf->dtls_srtp_profile_list_len = list_size;
Johan Pascalb62bb512015-12-03 21:56:45 +01003038
Gilles Peskine449bd832023-01-11 14:50:10 +01003039 return 0;
Johan Pascalb62bb512015-12-03 21:56:45 +01003040}
3041
Gilles Peskine449bd832023-01-11 14:50:10 +01003042void mbedtls_ssl_get_dtls_srtp_negotiation_result(const mbedtls_ssl_context *ssl,
3043 mbedtls_dtls_srtp_info *dtls_srtp_info)
Johan Pascalb62bb512015-12-03 21:56:45 +01003044{
Johan Pascal2258a4f2020-10-28 13:53:09 +01003045 dtls_srtp_info->chosen_dtls_srtp_profile = ssl->dtls_srtp_info.chosen_dtls_srtp_profile;
3046 /* do not copy the mki value if there is no chosen profile */
Gilles Peskine449bd832023-01-11 14:50:10 +01003047 if (dtls_srtp_info->chosen_dtls_srtp_profile == MBEDTLS_TLS_SRTP_UNSET) {
Johan Pascal2258a4f2020-10-28 13:53:09 +01003048 dtls_srtp_info->mki_len = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +01003049 } else {
Johan Pascal2258a4f2020-10-28 13:53:09 +01003050 dtls_srtp_info->mki_len = ssl->dtls_srtp_info.mki_len;
Gilles Peskine449bd832023-01-11 14:50:10 +01003051 memcpy(dtls_srtp_info->mki_value, ssl->dtls_srtp_info.mki_value,
3052 ssl->dtls_srtp_info.mki_len);
Johan Pascal2258a4f2020-10-28 13:53:09 +01003053 }
Johan Pascalb62bb512015-12-03 21:56:45 +01003054}
Johan Pascalb62bb512015-12-03 21:56:45 +01003055#endif /* MBEDTLS_SSL_DTLS_SRTP */
3056
Aditya Patwardhan3096f332022-07-26 14:31:46 +05303057#if !defined(MBEDTLS_DEPRECATED_REMOVED)
Gilles Peskine449bd832023-01-11 14:50:10 +01003058void mbedtls_ssl_conf_max_version(mbedtls_ssl_config *conf, int major, int minor)
Paul Bakker490ecc82011-10-06 13:04:09 +00003059{
Glenn Strauss2dfcea22022-03-14 17:26:42 -04003060 conf->max_tls_version = (major << 8) | minor;
Paul Bakker490ecc82011-10-06 13:04:09 +00003061}
3062
Gilles Peskine449bd832023-01-11 14:50:10 +01003063void mbedtls_ssl_conf_min_version(mbedtls_ssl_config *conf, int major, int minor)
Paul Bakker1d29fb52012-09-28 13:28:45 +00003064{
Glenn Strauss2dfcea22022-03-14 17:26:42 -04003065 conf->min_tls_version = (major << 8) | minor;
Paul Bakker1d29fb52012-09-28 13:28:45 +00003066}
Aditya Patwardhan3096f332022-07-26 14:31:46 +05303067#endif /* MBEDTLS_DEPRECATED_REMOVED */
Paul Bakker1d29fb52012-09-28 13:28:45 +00003068
Janos Follath088ce432017-04-10 12:42:31 +01003069#if defined(MBEDTLS_SSL_SRV_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01003070void mbedtls_ssl_conf_cert_req_ca_list(mbedtls_ssl_config *conf,
3071 char cert_req_ca_list)
Janos Follath088ce432017-04-10 12:42:31 +01003072{
3073 conf->cert_req_ca_list = cert_req_ca_list;
3074}
3075#endif
3076
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003077#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
Gilles Peskine449bd832023-01-11 14:50:10 +01003078void mbedtls_ssl_conf_encrypt_then_mac(mbedtls_ssl_config *conf, char etm)
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +01003079{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02003080 conf->encrypt_then_mac = etm;
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +01003081}
3082#endif
3083
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003084#if defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET)
Gilles Peskine449bd832023-01-11 14:50:10 +01003085void mbedtls_ssl_conf_extended_master_secret(mbedtls_ssl_config *conf, char ems)
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +02003086{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02003087 conf->extended_ms = ems;
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +02003088}
3089#endif
3090
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003091#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
Gilles Peskine449bd832023-01-11 14:50:10 +01003092int mbedtls_ssl_conf_max_frag_len(mbedtls_ssl_config *conf, unsigned char mfl_code)
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02003093{
Gilles Peskine449bd832023-01-11 14:50:10 +01003094 if (mfl_code >= MBEDTLS_SSL_MAX_FRAG_LEN_INVALID ||
3095 ssl_mfl_code_to_length(mfl_code) > MBEDTLS_TLS_EXT_ADV_CONTENT_LEN) {
3096 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02003097 }
3098
Manuel Pégourié-Gonnard6bf89d62015-05-05 17:01:57 +01003099 conf->mfl_code = mfl_code;
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02003100
Gilles Peskine449bd832023-01-11 14:50:10 +01003101 return 0;
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02003102}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003103#endif /* MBEDTLS_SSL_MAX_FRAGMENT_LENGTH */
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02003104
Gilles Peskine449bd832023-01-11 14:50:10 +01003105void mbedtls_ssl_conf_legacy_renegotiation(mbedtls_ssl_config *conf, int allow_legacy)
Paul Bakker48916f92012-09-16 19:57:18 +00003106{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02003107 conf->allow_legacy_renegotiation = allow_legacy;
Paul Bakker48916f92012-09-16 19:57:18 +00003108}
3109
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003110#if defined(MBEDTLS_SSL_RENEGOTIATION)
Gilles Peskine449bd832023-01-11 14:50:10 +01003111void mbedtls_ssl_conf_renegotiation(mbedtls_ssl_config *conf, int renegotiation)
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01003112{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02003113 conf->disable_renegotiation = renegotiation;
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01003114}
3115
Gilles Peskine449bd832023-01-11 14:50:10 +01003116void mbedtls_ssl_conf_renegotiation_enforced(mbedtls_ssl_config *conf, int max_records)
Manuel Pégourié-Gonnarda9964db2014-07-03 19:29:16 +02003117{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02003118 conf->renego_max_records = max_records;
Manuel Pégourié-Gonnarda9964db2014-07-03 19:29:16 +02003119}
3120
Gilles Peskine449bd832023-01-11 14:50:10 +01003121void mbedtls_ssl_conf_renegotiation_period(mbedtls_ssl_config *conf,
3122 const unsigned char period[8])
Manuel Pégourié-Gonnard837f0fe2014-11-05 13:58:53 +01003123{
Gilles Peskine449bd832023-01-11 14:50:10 +01003124 memcpy(conf->renego_period, period, 8);
Manuel Pégourié-Gonnard837f0fe2014-11-05 13:58:53 +01003125}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003126#endif /* MBEDTLS_SSL_RENEGOTIATION */
Paul Bakker5121ce52009-01-03 21:22:43 +00003127
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003128#if defined(MBEDTLS_SSL_SESSION_TICKETS)
Manuel Pégourié-Gonnardb596abf2015-05-20 10:45:29 +02003129#if defined(MBEDTLS_SSL_CLI_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01003130void mbedtls_ssl_conf_session_tickets(mbedtls_ssl_config *conf, int use_tickets)
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +02003131{
Manuel Pégourié-Gonnard2b494452015-05-06 10:05:11 +01003132 conf->session_tickets = use_tickets;
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +02003133}
Manuel Pégourié-Gonnardb596abf2015-05-20 10:45:29 +02003134#endif
Paul Bakker606b4ba2013-08-14 16:52:14 +02003135
Manuel Pégourié-Gonnardb596abf2015-05-20 10:45:29 +02003136#if defined(MBEDTLS_SSL_SRV_C)
Jerry Yu1ad7ace2022-08-09 13:28:39 +08003137
Jerry Yud0766ec2022-09-22 10:46:57 +08003138#if defined(MBEDTLS_SSL_PROTO_TLS1_3) && defined(MBEDTLS_SSL_SESSION_TICKETS)
Gilles Peskine449bd832023-01-11 14:50:10 +01003139void mbedtls_ssl_conf_new_session_tickets(mbedtls_ssl_config *conf,
3140 uint16_t num_tickets)
Jerry Yu1ad7ace2022-08-09 13:28:39 +08003141{
Jerry Yud0766ec2022-09-22 10:46:57 +08003142 conf->new_session_tickets_count = num_tickets;
Jerry Yu1ad7ace2022-08-09 13:28:39 +08003143}
3144#endif
3145
Gilles Peskine449bd832023-01-11 14:50:10 +01003146void mbedtls_ssl_conf_session_tickets_cb(mbedtls_ssl_config *conf,
3147 mbedtls_ssl_ticket_write_t *f_ticket_write,
3148 mbedtls_ssl_ticket_parse_t *f_ticket_parse,
3149 void *p_ticket)
Paul Bakker606b4ba2013-08-14 16:52:14 +02003150{
Manuel Pégourié-Gonnardd59675d2015-05-19 15:28:00 +02003151 conf->f_ticket_write = f_ticket_write;
3152 conf->f_ticket_parse = f_ticket_parse;
3153 conf->p_ticket = p_ticket;
Paul Bakker606b4ba2013-08-14 16:52:14 +02003154}
Manuel Pégourié-Gonnardb596abf2015-05-20 10:45:29 +02003155#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003156#endif /* MBEDTLS_SSL_SESSION_TICKETS */
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +02003157
Gilles Peskine449bd832023-01-11 14:50:10 +01003158void mbedtls_ssl_set_export_keys_cb(mbedtls_ssl_context *ssl,
3159 mbedtls_ssl_export_keys_t *f_export_keys,
3160 void *p_export_keys)
Robert Cragie4feb7ae2015-10-02 13:33:37 +01003161{
Hanno Becker7e6c1782021-06-08 09:24:55 +01003162 ssl->f_export_keys = f_export_keys;
3163 ssl->p_export_keys = p_export_keys;
Ron Eldorf5cc10d2019-05-07 18:33:40 +03003164}
Robert Cragie4feb7ae2015-10-02 13:33:37 +01003165
Gilles Peskineb74a1c72018-04-24 13:09:22 +02003166#if defined(MBEDTLS_SSL_ASYNC_PRIVATE)
Gilles Peskine8bf79f62018-01-05 21:11:53 +01003167void mbedtls_ssl_conf_async_private_cb(
3168 mbedtls_ssl_config *conf,
3169 mbedtls_ssl_async_sign_t *f_async_sign,
3170 mbedtls_ssl_async_decrypt_t *f_async_decrypt,
3171 mbedtls_ssl_async_resume_t *f_async_resume,
3172 mbedtls_ssl_async_cancel_t *f_async_cancel,
Gilles Peskine449bd832023-01-11 14:50:10 +01003173 void *async_config_data)
Gilles Peskine8bf79f62018-01-05 21:11:53 +01003174{
3175 conf->f_async_sign_start = f_async_sign;
3176 conf->f_async_decrypt_start = f_async_decrypt;
3177 conf->f_async_resume = f_async_resume;
3178 conf->f_async_cancel = f_async_cancel;
Gilles Peskinedf13d5c2018-04-25 20:39:48 +02003179 conf->p_async_config_data = async_config_data;
3180}
3181
Gilles Peskine449bd832023-01-11 14:50:10 +01003182void *mbedtls_ssl_conf_get_async_config_data(const mbedtls_ssl_config *conf)
Gilles Peskine8f97af72018-04-26 11:46:10 +02003183{
Gilles Peskine449bd832023-01-11 14:50:10 +01003184 return conf->p_async_config_data;
Gilles Peskine8f97af72018-04-26 11:46:10 +02003185}
3186
Gilles Peskine449bd832023-01-11 14:50:10 +01003187void *mbedtls_ssl_get_async_operation_data(const mbedtls_ssl_context *ssl)
Gilles Peskinedf13d5c2018-04-25 20:39:48 +02003188{
Gilles Peskine449bd832023-01-11 14:50:10 +01003189 if (ssl->handshake == NULL) {
3190 return NULL;
3191 } else {
3192 return ssl->handshake->user_async_ctx;
3193 }
Gilles Peskinedf13d5c2018-04-25 20:39:48 +02003194}
3195
Gilles Peskine449bd832023-01-11 14:50:10 +01003196void mbedtls_ssl_set_async_operation_data(mbedtls_ssl_context *ssl,
3197 void *ctx)
Gilles Peskinedf13d5c2018-04-25 20:39:48 +02003198{
Gilles Peskine449bd832023-01-11 14:50:10 +01003199 if (ssl->handshake != NULL) {
Gilles Peskinedf13d5c2018-04-25 20:39:48 +02003200 ssl->handshake->user_async_ctx = ctx;
Gilles Peskine449bd832023-01-11 14:50:10 +01003201 }
Gilles Peskine8bf79f62018-01-05 21:11:53 +01003202}
Gilles Peskineb74a1c72018-04-24 13:09:22 +02003203#endif /* MBEDTLS_SSL_ASYNC_PRIVATE */
Gilles Peskine8bf79f62018-01-05 21:11:53 +01003204
Paul Bakker5121ce52009-01-03 21:22:43 +00003205/*
3206 * SSL get accessors
3207 */
Gilles Peskine449bd832023-01-11 14:50:10 +01003208uint32_t mbedtls_ssl_get_verify_result(const mbedtls_ssl_context *ssl)
Paul Bakker5121ce52009-01-03 21:22:43 +00003209{
Gilles Peskine449bd832023-01-11 14:50:10 +01003210 if (ssl->session != NULL) {
3211 return ssl->session->verify_result;
3212 }
Manuel Pégourié-Gonnarde89163c2015-01-23 14:30:57 +00003213
Gilles Peskine449bd832023-01-11 14:50:10 +01003214 if (ssl->session_negotiate != NULL) {
3215 return ssl->session_negotiate->verify_result;
3216 }
Manuel Pégourié-Gonnarde89163c2015-01-23 14:30:57 +00003217
Gilles Peskine449bd832023-01-11 14:50:10 +01003218 return 0xFFFFFFFF;
Paul Bakker5121ce52009-01-03 21:22:43 +00003219}
3220
Gilles Peskine449bd832023-01-11 14:50:10 +01003221int mbedtls_ssl_get_ciphersuite_id_from_ssl(const mbedtls_ssl_context *ssl)
Glenn Strauss8f526902022-01-13 00:04:49 -05003222{
Gilles Peskine449bd832023-01-11 14:50:10 +01003223 if (ssl == NULL || ssl->session == NULL) {
3224 return 0;
3225 }
Glenn Strauss8f526902022-01-13 00:04:49 -05003226
Gilles Peskine449bd832023-01-11 14:50:10 +01003227 return ssl->session->ciphersuite;
Glenn Strauss8f526902022-01-13 00:04:49 -05003228}
3229
Gilles Peskine449bd832023-01-11 14:50:10 +01003230const char *mbedtls_ssl_get_ciphersuite(const mbedtls_ssl_context *ssl)
Paul Bakker72f62662011-01-16 21:27:44 +00003231{
Gilles Peskine449bd832023-01-11 14:50:10 +01003232 if (ssl == NULL || ssl->session == NULL) {
3233 return NULL;
3234 }
Paul Bakker926c8e42013-03-06 10:23:34 +01003235
Gilles Peskine449bd832023-01-11 14:50:10 +01003236 return mbedtls_ssl_get_ciphersuite_name(ssl->session->ciphersuite);
Paul Bakker72f62662011-01-16 21:27:44 +00003237}
3238
Gilles Peskine449bd832023-01-11 14:50:10 +01003239const char *mbedtls_ssl_get_version(const mbedtls_ssl_context *ssl)
Paul Bakker43ca69c2011-01-15 17:35:19 +00003240{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003241#if defined(MBEDTLS_SSL_PROTO_DTLS)
Gilles Peskine449bd832023-01-11 14:50:10 +01003242 if (ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM) {
3243 switch (ssl->tls_version) {
Glenn Strauss60bfe602022-03-14 19:04:24 -04003244 case MBEDTLS_SSL_VERSION_TLS1_2:
Gilles Peskine449bd832023-01-11 14:50:10 +01003245 return "DTLSv1.2";
Manuel Pégourié-Gonnardb21ca2a2014-02-10 13:43:33 +01003246 default:
Gilles Peskine449bd832023-01-11 14:50:10 +01003247 return "unknown (DTLS)";
Manuel Pégourié-Gonnardb21ca2a2014-02-10 13:43:33 +01003248 }
3249 }
3250#endif
3251
Gilles Peskine449bd832023-01-11 14:50:10 +01003252 switch (ssl->tls_version) {
Glenn Strauss60bfe602022-03-14 19:04:24 -04003253 case MBEDTLS_SSL_VERSION_TLS1_2:
Gilles Peskine449bd832023-01-11 14:50:10 +01003254 return "TLSv1.2";
Glenn Strauss60bfe602022-03-14 19:04:24 -04003255 case MBEDTLS_SSL_VERSION_TLS1_3:
Gilles Peskine449bd832023-01-11 14:50:10 +01003256 return "TLSv1.3";
Paul Bakker43ca69c2011-01-15 17:35:19 +00003257 default:
Gilles Peskine449bd832023-01-11 14:50:10 +01003258 return "unknown";
Paul Bakker43ca69c2011-01-15 17:35:19 +00003259 }
Paul Bakker43ca69c2011-01-15 17:35:19 +00003260}
3261
Manuel Pégourié-Gonnarda2cda6b2015-08-31 18:30:52 +02003262#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
Gilles Peskine449bd832023-01-11 14:50:10 +01003263size_t mbedtls_ssl_get_input_max_frag_len(const mbedtls_ssl_context *ssl)
Andrzej Kurek90c6e842020-04-03 05:25:29 -04003264{
David Horstmann95d516f2021-05-04 18:36:56 +01003265 size_t max_len = MBEDTLS_SSL_IN_CONTENT_LEN;
Andrzej Kurek90c6e842020-04-03 05:25:29 -04003266 size_t read_mfl;
3267
Jerry Yuddda0502022-12-01 19:43:12 +08003268#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
Andrzej Kurek90c6e842020-04-03 05:25:29 -04003269 /* Use the configured MFL for the client if we're past SERVER_HELLO_DONE */
Gilles Peskine449bd832023-01-11 14:50:10 +01003270 if (ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT &&
3271 ssl->state >= MBEDTLS_SSL_SERVER_HELLO_DONE) {
3272 return ssl_mfl_code_to_length(ssl->conf->mfl_code);
Andrzej Kurek90c6e842020-04-03 05:25:29 -04003273 }
Jerry Yuddda0502022-12-01 19:43:12 +08003274#endif
Andrzej Kurek90c6e842020-04-03 05:25:29 -04003275
3276 /* Check if a smaller max length was negotiated */
Gilles Peskine449bd832023-01-11 14:50:10 +01003277 if (ssl->session_out != NULL) {
3278 read_mfl = ssl_mfl_code_to_length(ssl->session_out->mfl_code);
3279 if (read_mfl < max_len) {
Andrzej Kurek90c6e842020-04-03 05:25:29 -04003280 max_len = read_mfl;
3281 }
3282 }
3283
Jerry Yuddda0502022-12-01 19:43:12 +08003284 /* During a handshake, use the value being negotiated */
Gilles Peskine449bd832023-01-11 14:50:10 +01003285 if (ssl->session_negotiate != NULL) {
3286 read_mfl = ssl_mfl_code_to_length(ssl->session_negotiate->mfl_code);
3287 if (read_mfl < max_len) {
Andrzej Kurek90c6e842020-04-03 05:25:29 -04003288 max_len = read_mfl;
3289 }
3290 }
3291
Gilles Peskine449bd832023-01-11 14:50:10 +01003292 return max_len;
Andrzej Kurek90c6e842020-04-03 05:25:29 -04003293}
3294
Gilles Peskine449bd832023-01-11 14:50:10 +01003295size_t mbedtls_ssl_get_output_max_frag_len(const mbedtls_ssl_context *ssl)
Manuel Pégourié-Gonnarda2cda6b2015-08-31 18:30:52 +02003296{
3297 size_t max_len;
3298
3299 /*
3300 * Assume mfl_code is correct since it was checked when set
3301 */
Gilles Peskine449bd832023-01-11 14:50:10 +01003302 max_len = ssl_mfl_code_to_length(ssl->conf->mfl_code);
Manuel Pégourié-Gonnarda2cda6b2015-08-31 18:30:52 +02003303
Manuel Pégourié-Gonnard2cb17e22017-09-19 13:00:47 +02003304 /* Check if a smaller max length was negotiated */
Gilles Peskine449bd832023-01-11 14:50:10 +01003305 if (ssl->session_out != NULL &&
3306 ssl_mfl_code_to_length(ssl->session_out->mfl_code) < max_len) {
3307 max_len = ssl_mfl_code_to_length(ssl->session_out->mfl_code);
Manuel Pégourié-Gonnarda2cda6b2015-08-31 18:30:52 +02003308 }
3309
Manuel Pégourié-Gonnard2cb17e22017-09-19 13:00:47 +02003310 /* During a handshake, use the value being negotiated */
Gilles Peskine449bd832023-01-11 14:50:10 +01003311 if (ssl->session_negotiate != NULL &&
3312 ssl_mfl_code_to_length(ssl->session_negotiate->mfl_code) < max_len) {
3313 max_len = ssl_mfl_code_to_length(ssl->session_negotiate->mfl_code);
Manuel Pégourié-Gonnard2cb17e22017-09-19 13:00:47 +02003314 }
3315
Gilles Peskine449bd832023-01-11 14:50:10 +01003316 return max_len;
Manuel Pégourié-Gonnarda2cda6b2015-08-31 18:30:52 +02003317}
3318#endif /* MBEDTLS_SSL_MAX_FRAGMENT_LENGTH */
3319
Manuel Pégourié-Gonnardb8eec192018-08-20 09:34:02 +02003320#if defined(MBEDTLS_SSL_PROTO_DTLS)
Gilles Peskine449bd832023-01-11 14:50:10 +01003321size_t mbedtls_ssl_get_current_mtu(const mbedtls_ssl_context *ssl)
Manuel Pégourié-Gonnardb8eec192018-08-20 09:34:02 +02003322{
Andrzej Kurekef43ce62018-10-09 08:24:12 -04003323 /* Return unlimited mtu for client hello messages to avoid fragmentation. */
Gilles Peskine449bd832023-01-11 14:50:10 +01003324 if (ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT &&
3325 (ssl->state == MBEDTLS_SSL_CLIENT_HELLO ||
3326 ssl->state == MBEDTLS_SSL_SERVER_HELLO)) {
3327 return 0;
3328 }
Andrzej Kurekef43ce62018-10-09 08:24:12 -04003329
Gilles Peskine449bd832023-01-11 14:50:10 +01003330 if (ssl->handshake == NULL || ssl->handshake->mtu == 0) {
3331 return ssl->mtu;
3332 }
Manuel Pégourié-Gonnardb8eec192018-08-20 09:34:02 +02003333
Gilles Peskine449bd832023-01-11 14:50:10 +01003334 if (ssl->mtu == 0) {
3335 return ssl->handshake->mtu;
3336 }
Manuel Pégourié-Gonnardb8eec192018-08-20 09:34:02 +02003337
Gilles Peskine449bd832023-01-11 14:50:10 +01003338 return ssl->mtu < ssl->handshake->mtu ?
3339 ssl->mtu : ssl->handshake->mtu;
Manuel Pégourié-Gonnardb8eec192018-08-20 09:34:02 +02003340}
3341#endif /* MBEDTLS_SSL_PROTO_DTLS */
3342
Gilles Peskine449bd832023-01-11 14:50:10 +01003343int mbedtls_ssl_get_max_out_record_payload(const mbedtls_ssl_context *ssl)
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02003344{
3345 size_t max_len = MBEDTLS_SSL_OUT_CONTENT_LEN;
3346
Manuel Pégourié-Gonnard000281e2018-08-21 11:20:58 +02003347#if !defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH) && \
3348 !defined(MBEDTLS_SSL_PROTO_DTLS)
3349 (void) ssl;
3350#endif
3351
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02003352#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
Gilles Peskine449bd832023-01-11 14:50:10 +01003353 const size_t mfl = mbedtls_ssl_get_output_max_frag_len(ssl);
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02003354
Gilles Peskine449bd832023-01-11 14:50:10 +01003355 if (max_len > mfl) {
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02003356 max_len = mfl;
Gilles Peskine449bd832023-01-11 14:50:10 +01003357 }
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02003358#endif
3359
3360#if defined(MBEDTLS_SSL_PROTO_DTLS)
Gilles Peskine449bd832023-01-11 14:50:10 +01003361 if (mbedtls_ssl_get_current_mtu(ssl) != 0) {
3362 const size_t mtu = mbedtls_ssl_get_current_mtu(ssl);
3363 const int ret = mbedtls_ssl_get_record_expansion(ssl);
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02003364 const size_t overhead = (size_t) ret;
3365
Gilles Peskine449bd832023-01-11 14:50:10 +01003366 if (ret < 0) {
3367 return ret;
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02003368 }
3369
Gilles Peskine449bd832023-01-11 14:50:10 +01003370 if (mtu <= overhead) {
3371 MBEDTLS_SSL_DEBUG_MSG(1, ("MTU too low for record expansion"));
3372 return MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
3373 }
3374
3375 if (max_len > mtu - overhead) {
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02003376 max_len = mtu - overhead;
Gilles Peskine449bd832023-01-11 14:50:10 +01003377 }
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02003378 }
Manuel Pégourié-Gonnardb8eec192018-08-20 09:34:02 +02003379#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02003380
Hanno Becker0defedb2018-08-10 12:35:02 +01003381#if !defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH) && \
3382 !defined(MBEDTLS_SSL_PROTO_DTLS)
3383 ((void) ssl);
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02003384#endif
3385
Gilles Peskine449bd832023-01-11 14:50:10 +01003386 return (int) max_len;
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02003387}
3388
Gilles Peskine449bd832023-01-11 14:50:10 +01003389int mbedtls_ssl_get_max_in_record_payload(const mbedtls_ssl_context *ssl)
Hanno Becker2d8e99b2021-04-21 06:19:50 +01003390{
3391 size_t max_len = MBEDTLS_SSL_IN_CONTENT_LEN;
3392
3393#if !defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
3394 (void) ssl;
3395#endif
3396
3397#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
Gilles Peskine449bd832023-01-11 14:50:10 +01003398 const size_t mfl = mbedtls_ssl_get_input_max_frag_len(ssl);
Hanno Becker2d8e99b2021-04-21 06:19:50 +01003399
Gilles Peskine449bd832023-01-11 14:50:10 +01003400 if (max_len > mfl) {
Hanno Becker2d8e99b2021-04-21 06:19:50 +01003401 max_len = mfl;
Gilles Peskine449bd832023-01-11 14:50:10 +01003402 }
Hanno Becker2d8e99b2021-04-21 06:19:50 +01003403#endif
3404
Gilles Peskine449bd832023-01-11 14:50:10 +01003405 return (int) max_len;
Hanno Becker2d8e99b2021-04-21 06:19:50 +01003406}
3407
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003408#if defined(MBEDTLS_X509_CRT_PARSE_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01003409const mbedtls_x509_crt *mbedtls_ssl_get_peer_cert(const mbedtls_ssl_context *ssl)
Paul Bakkerb0550d92012-10-30 07:51:03 +00003410{
Gilles Peskine449bd832023-01-11 14:50:10 +01003411 if (ssl == NULL || ssl->session == NULL) {
3412 return NULL;
3413 }
Paul Bakkerb0550d92012-10-30 07:51:03 +00003414
Hanno Beckere6824572019-02-07 13:18:46 +00003415#if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
Gilles Peskine449bd832023-01-11 14:50:10 +01003416 return ssl->session->peer_cert;
Hanno Beckere6824572019-02-07 13:18:46 +00003417#else
Gilles Peskine449bd832023-01-11 14:50:10 +01003418 return NULL;
Hanno Beckere6824572019-02-07 13:18:46 +00003419#endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
Paul Bakkerb0550d92012-10-30 07:51:03 +00003420}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003421#endif /* MBEDTLS_X509_CRT_PARSE_C */
Paul Bakkerb0550d92012-10-30 07:51:03 +00003422
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003423#if defined(MBEDTLS_SSL_CLI_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01003424int mbedtls_ssl_get_session(const mbedtls_ssl_context *ssl,
3425 mbedtls_ssl_session *dst)
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02003426{
Hanno Beckere810bbc2021-05-14 16:01:05 +01003427 int ret;
3428
Gilles Peskine449bd832023-01-11 14:50:10 +01003429 if (ssl == NULL ||
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02003430 dst == NULL ||
3431 ssl->session == NULL ||
Gilles Peskine449bd832023-01-11 14:50:10 +01003432 ssl->conf->endpoint != MBEDTLS_SSL_IS_CLIENT) {
3433 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02003434 }
3435
Hanno Beckere810bbc2021-05-14 16:01:05 +01003436 /* Since Mbed TLS 3.0, mbedtls_ssl_get_session() is no longer
3437 * idempotent: Each session can only be exported once.
3438 *
3439 * (This is in preparation for TLS 1.3 support where we will
3440 * need the ability to export multiple sessions (aka tickets),
3441 * which will be achieved by calling mbedtls_ssl_get_session()
3442 * multiple times until it fails.)
3443 *
3444 * Check whether we have already exported the current session,
3445 * and fail if so.
3446 */
Gilles Peskine449bd832023-01-11 14:50:10 +01003447 if (ssl->session->exported == 1) {
3448 return MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
3449 }
Hanno Beckere810bbc2021-05-14 16:01:05 +01003450
Gilles Peskine449bd832023-01-11 14:50:10 +01003451 ret = mbedtls_ssl_session_copy(dst, ssl->session);
3452 if (ret != 0) {
3453 return ret;
3454 }
Hanno Beckere810bbc2021-05-14 16:01:05 +01003455
3456 /* Remember that we've exported the session. */
3457 ssl->session->exported = 1;
Gilles Peskine449bd832023-01-11 14:50:10 +01003458 return 0;
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02003459}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003460#endif /* MBEDTLS_SSL_CLI_C */
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02003461
Paul Bakker5121ce52009-01-03 21:22:43 +00003462/*
Hanno Beckera835da52019-05-16 12:39:07 +01003463 * Define ticket header determining Mbed TLS version
3464 * and structure of the ticket.
3465 */
3466
Hanno Becker94ef3b32019-05-16 12:50:45 +01003467/*
Hanno Becker50b59662019-05-28 14:30:45 +01003468 * Define bitflag determining compile-time settings influencing
3469 * structure of serialized SSL sessions.
Hanno Becker94ef3b32019-05-16 12:50:45 +01003470 */
3471
Hanno Becker50b59662019-05-28 14:30:45 +01003472#if defined(MBEDTLS_HAVE_TIME)
Hanno Becker3e088662019-05-29 11:10:18 +01003473#define SSL_SERIALIZED_SESSION_CONFIG_TIME 1
Hanno Becker50b59662019-05-28 14:30:45 +01003474#else
Hanno Becker3e088662019-05-29 11:10:18 +01003475#define SSL_SERIALIZED_SESSION_CONFIG_TIME 0
Hanno Becker94ef3b32019-05-16 12:50:45 +01003476#endif /* MBEDTLS_HAVE_TIME */
3477
3478#if defined(MBEDTLS_X509_CRT_PARSE_C)
Hanno Becker3e088662019-05-29 11:10:18 +01003479#define SSL_SERIALIZED_SESSION_CONFIG_CRT 1
Hanno Becker94ef3b32019-05-16 12:50:45 +01003480#else
Hanno Becker3e088662019-05-29 11:10:18 +01003481#define SSL_SERIALIZED_SESSION_CONFIG_CRT 0
Hanno Becker94ef3b32019-05-16 12:50:45 +01003482#endif /* MBEDTLS_X509_CRT_PARSE_C */
3483
3484#if defined(MBEDTLS_SSL_CLI_C) && defined(MBEDTLS_SSL_SESSION_TICKETS)
Hanno Becker3e088662019-05-29 11:10:18 +01003485#define SSL_SERIALIZED_SESSION_CONFIG_CLIENT_TICKET 1
Hanno Becker94ef3b32019-05-16 12:50:45 +01003486#else
Hanno Becker3e088662019-05-29 11:10:18 +01003487#define SSL_SERIALIZED_SESSION_CONFIG_CLIENT_TICKET 0
Hanno Becker94ef3b32019-05-16 12:50:45 +01003488#endif /* MBEDTLS_SSL_CLI_C && MBEDTLS_SSL_SESSION_TICKETS */
3489
3490#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
Hanno Becker3e088662019-05-29 11:10:18 +01003491#define SSL_SERIALIZED_SESSION_CONFIG_MFL 1
Hanno Becker94ef3b32019-05-16 12:50:45 +01003492#else
Hanno Becker3e088662019-05-29 11:10:18 +01003493#define SSL_SERIALIZED_SESSION_CONFIG_MFL 0
Hanno Becker94ef3b32019-05-16 12:50:45 +01003494#endif /* MBEDTLS_SSL_MAX_FRAGMENT_LENGTH */
3495
Hanno Becker94ef3b32019-05-16 12:50:45 +01003496#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
Hanno Becker3e088662019-05-29 11:10:18 +01003497#define SSL_SERIALIZED_SESSION_CONFIG_ETM 1
Hanno Becker94ef3b32019-05-16 12:50:45 +01003498#else
Hanno Becker3e088662019-05-29 11:10:18 +01003499#define SSL_SERIALIZED_SESSION_CONFIG_ETM 0
Hanno Becker94ef3b32019-05-16 12:50:45 +01003500#endif /* MBEDTLS_SSL_ENCRYPT_THEN_MAC */
3501
Hanno Becker94ef3b32019-05-16 12:50:45 +01003502#if defined(MBEDTLS_SSL_SESSION_TICKETS)
3503#define SSL_SERIALIZED_SESSION_CONFIG_TICKET 1
3504#else
3505#define SSL_SERIALIZED_SESSION_CONFIG_TICKET 0
3506#endif /* MBEDTLS_SSL_SESSION_TICKETS */
3507
Hanno Becker3e088662019-05-29 11:10:18 +01003508#define SSL_SERIALIZED_SESSION_CONFIG_TIME_BIT 0
3509#define SSL_SERIALIZED_SESSION_CONFIG_CRT_BIT 1
3510#define SSL_SERIALIZED_SESSION_CONFIG_CLIENT_TICKET_BIT 2
3511#define SSL_SERIALIZED_SESSION_CONFIG_MFL_BIT 3
Hanno Becker37bdbe62021-08-01 05:38:58 +01003512#define SSL_SERIALIZED_SESSION_CONFIG_ETM_BIT 4
3513#define SSL_SERIALIZED_SESSION_CONFIG_TICKET_BIT 5
Hanno Becker3e088662019-05-29 11:10:18 +01003514
Hanno Becker50b59662019-05-28 14:30:45 +01003515#define SSL_SERIALIZED_SESSION_CONFIG_BITFLAG \
Gilles Peskine449bd832023-01-11 14:50:10 +01003516 ((uint16_t) ( \
3517 (SSL_SERIALIZED_SESSION_CONFIG_TIME << SSL_SERIALIZED_SESSION_CONFIG_TIME_BIT) | \
3518 (SSL_SERIALIZED_SESSION_CONFIG_CRT << SSL_SERIALIZED_SESSION_CONFIG_CRT_BIT) | \
3519 (SSL_SERIALIZED_SESSION_CONFIG_CLIENT_TICKET << \
3520 SSL_SERIALIZED_SESSION_CONFIG_CLIENT_TICKET_BIT) | \
3521 (SSL_SERIALIZED_SESSION_CONFIG_MFL << SSL_SERIALIZED_SESSION_CONFIG_MFL_BIT) | \
3522 (SSL_SERIALIZED_SESSION_CONFIG_ETM << SSL_SERIALIZED_SESSION_CONFIG_ETM_BIT) | \
3523 (SSL_SERIALIZED_SESSION_CONFIG_TICKET << SSL_SERIALIZED_SESSION_CONFIG_TICKET_BIT)))
Hanno Becker94ef3b32019-05-16 12:50:45 +01003524
Hanno Beckerf8787072019-05-16 12:41:07 +01003525static unsigned char ssl_serialized_session_header[] = {
Hanno Becker94ef3b32019-05-16 12:50:45 +01003526 MBEDTLS_VERSION_MAJOR,
3527 MBEDTLS_VERSION_MINOR,
3528 MBEDTLS_VERSION_PATCH,
Gilles Peskine449bd832023-01-11 14:50:10 +01003529 MBEDTLS_BYTE_1(SSL_SERIALIZED_SESSION_CONFIG_BITFLAG),
3530 MBEDTLS_BYTE_0(SSL_SERIALIZED_SESSION_CONFIG_BITFLAG),
Hanno Beckerf8787072019-05-16 12:41:07 +01003531};
Hanno Beckera835da52019-05-16 12:39:07 +01003532
3533/*
Manuel Pégourié-Gonnarda3e7c652019-05-16 10:08:35 +02003534 * Serialize a session in the following format:
Manuel Pégourié-Gonnard35eb8022019-05-16 11:11:08 +02003535 * (in the presentation language of TLS, RFC 8446 section 3)
Manuel Pégourié-Gonnarda3e7c652019-05-16 10:08:35 +02003536 *
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01003537 * struct {
Hanno Beckerdc28b6c2019-05-29 11:08:00 +01003538 *
Hanno Beckerdce50972021-08-01 05:39:23 +01003539 * opaque mbedtls_version[3]; // library version: major, minor, patch
3540 * opaque session_format[2]; // library-version specific 16-bit field
3541 * // determining the format of the remaining
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01003542 * // serialized data.
Hanno Beckerdc28b6c2019-05-29 11:08:00 +01003543 *
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01003544 * Note: When updating the format, remember to keep
3545 * these version+format bytes.
Manuel Pégourié-Gonnarda3e7c652019-05-16 10:08:35 +02003546 *
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01003547 * // In this version, `session_format` determines
3548 * // the setting of those compile-time
3549 * // configuration options which influence
3550 * // the structure of mbedtls_ssl_session.
3551 *
Glenn Straussda7851c2022-03-14 13:29:48 -04003552 * uint8_t minor_ver; // Protocol minor version. Possible values:
Jerry Yu0c2a7382022-12-06 13:27:25 +08003553 * // - TLS 1.2 (0x0303)
3554 * // - TLS 1.3 (0x0304)
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01003555 *
Glenn Straussda7851c2022-03-14 13:29:48 -04003556 * select (serialized_session.tls_version) {
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01003557 *
Glenn Straussda7851c2022-03-14 13:29:48 -04003558 * case MBEDTLS_SSL_VERSION_TLS1_2:
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01003559 * serialized_session_tls12 data;
Jerry Yu0c2a7382022-12-06 13:27:25 +08003560 * case MBEDTLS_SSL_VERSION_TLS1_3:
Jerry Yuddda0502022-12-01 19:43:12 +08003561 * serialized_session_tls13 data;
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01003562 *
3563 * };
3564 *
3565 * } serialized_session;
3566 *
Manuel Pégourié-Gonnarda3e7c652019-05-16 10:08:35 +02003567 */
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01003568
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02003569MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01003570static int ssl_session_save(const mbedtls_ssl_session *session,
3571 unsigned char omit_header,
3572 unsigned char *buf,
3573 size_t buf_len,
3574 size_t *olen)
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01003575{
3576 unsigned char *p = buf;
3577 size_t used = 0;
Jerry Yu251a12e2022-07-13 15:15:48 +08003578 size_t remaining_len;
Jerry Yue36fdd62022-08-17 21:31:36 +08003579#if defined(MBEDTLS_SSL_PROTO_TLS1_3)
3580 size_t out_len;
3581 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
3582#endif
Gilles Peskine449bd832023-01-11 14:50:10 +01003583 if (session == NULL) {
3584 return MBEDTLS_ERR_SSL_INTERNAL_ERROR;
3585 }
Jerry Yu438ddd82022-07-07 06:55:50 +00003586
Gilles Peskine449bd832023-01-11 14:50:10 +01003587 if (!omit_header) {
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01003588 /*
3589 * Add Mbed TLS version identifier
3590 */
Gilles Peskine449bd832023-01-11 14:50:10 +01003591 used += sizeof(ssl_serialized_session_header);
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01003592
Gilles Peskine449bd832023-01-11 14:50:10 +01003593 if (used <= buf_len) {
3594 memcpy(p, ssl_serialized_session_header,
3595 sizeof(ssl_serialized_session_header));
3596 p += sizeof(ssl_serialized_session_header);
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01003597 }
3598 }
3599
3600 /*
3601 * TLS version identifier
3602 */
3603 used += 1;
Gilles Peskine449bd832023-01-11 14:50:10 +01003604 if (used <= buf_len) {
3605 *p++ = MBEDTLS_BYTE_0(session->tls_version);
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01003606 }
3607
3608 /* Forward to version-specific serialization routine. */
Jerry Yufca4d572022-07-21 10:37:48 +08003609 remaining_len = (buf_len >= used) ? buf_len - used : 0;
Gilles Peskine449bd832023-01-11 14:50:10 +01003610 switch (session->tls_version) {
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01003611#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
Gilles Peskine449bd832023-01-11 14:50:10 +01003612 case MBEDTLS_SSL_VERSION_TLS1_2:
3613 used += ssl_tls12_session_save(session, p, remaining_len);
3614 break;
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01003615#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
3616
Jerry Yu251a12e2022-07-13 15:15:48 +08003617#if defined(MBEDTLS_SSL_PROTO_TLS1_3)
Gilles Peskine449bd832023-01-11 14:50:10 +01003618 case MBEDTLS_SSL_VERSION_TLS1_3:
3619 ret = ssl_tls13_session_save(session, p, remaining_len, &out_len);
3620 if (ret != 0 && ret != MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL) {
3621 return ret;
3622 }
3623 used += out_len;
3624 break;
Jerry Yu251a12e2022-07-13 15:15:48 +08003625#endif /* MBEDTLS_SSL_PROTO_TLS1_3 */
3626
Gilles Peskine449bd832023-01-11 14:50:10 +01003627 default:
3628 return MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01003629 }
3630
3631 *olen = used;
Gilles Peskine449bd832023-01-11 14:50:10 +01003632 if (used > buf_len) {
3633 return MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL;
3634 }
Manuel Pégourié-Gonnarda3e7c652019-05-16 10:08:35 +02003635
Gilles Peskine449bd832023-01-11 14:50:10 +01003636 return 0;
Manuel Pégourié-Gonnarda3e7c652019-05-16 10:08:35 +02003637}
3638
3639/*
Manuel Pégourié-Gonnard45ac1f02019-07-23 16:52:45 +02003640 * Public wrapper for ssl_session_save()
3641 */
Gilles Peskine449bd832023-01-11 14:50:10 +01003642int mbedtls_ssl_session_save(const mbedtls_ssl_session *session,
3643 unsigned char *buf,
3644 size_t buf_len,
3645 size_t *olen)
Manuel Pégourié-Gonnard45ac1f02019-07-23 16:52:45 +02003646{
Gilles Peskine449bd832023-01-11 14:50:10 +01003647 return ssl_session_save(session, 0, buf, buf_len, olen);
Manuel Pégourié-Gonnard45ac1f02019-07-23 16:52:45 +02003648}
Manuel Pégourié-Gonnarda3e7c652019-05-16 10:08:35 +02003649
Jerry Yuf1b23ca2022-02-18 11:48:47 +08003650/*
3651 * Deserialize session, see mbedtls_ssl_session_save() for format.
3652 *
3653 * This internal version is wrapped by a public function that cleans up in
3654 * case of error, and has an extra option omit_header.
3655 */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02003656MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01003657static int ssl_session_load(mbedtls_ssl_session *session,
3658 unsigned char omit_header,
3659 const unsigned char *buf,
3660 size_t len)
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01003661{
3662 const unsigned char *p = buf;
3663 const unsigned char * const end = buf + len;
Jerry Yu438ddd82022-07-07 06:55:50 +00003664 size_t remaining_len;
3665
3666
Gilles Peskine449bd832023-01-11 14:50:10 +01003667 if (session == NULL) {
3668 return MBEDTLS_ERR_SSL_INTERNAL_ERROR;
3669 }
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01003670
Gilles Peskine449bd832023-01-11 14:50:10 +01003671 if (!omit_header) {
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01003672 /*
3673 * Check Mbed TLS version identifier
3674 */
3675
Gilles Peskine449bd832023-01-11 14:50:10 +01003676 if ((size_t) (end - p) < sizeof(ssl_serialized_session_header)) {
3677 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01003678 }
Gilles Peskine449bd832023-01-11 14:50:10 +01003679
3680 if (memcmp(p, ssl_serialized_session_header,
3681 sizeof(ssl_serialized_session_header)) != 0) {
3682 return MBEDTLS_ERR_SSL_VERSION_MISMATCH;
3683 }
3684 p += sizeof(ssl_serialized_session_header);
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01003685 }
3686
3687 /*
3688 * TLS version identifier
3689 */
Gilles Peskine449bd832023-01-11 14:50:10 +01003690 if (1 > (size_t) (end - p)) {
3691 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
3692 }
Glenn Straussda7851c2022-03-14 13:29:48 -04003693 session->tls_version = 0x0300 | *p++;
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01003694
3695 /* Dispatch according to TLS version. */
Gilles Peskine449bd832023-01-11 14:50:10 +01003696 remaining_len = (end - p);
3697 switch (session->tls_version) {
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01003698#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
Gilles Peskine449bd832023-01-11 14:50:10 +01003699 case MBEDTLS_SSL_VERSION_TLS1_2:
3700 return ssl_tls12_session_load(session, p, remaining_len);
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01003701#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
3702
Jerry Yu438ddd82022-07-07 06:55:50 +00003703#if defined(MBEDTLS_SSL_PROTO_TLS1_3)
Gilles Peskine449bd832023-01-11 14:50:10 +01003704 case MBEDTLS_SSL_VERSION_TLS1_3:
3705 return ssl_tls13_session_load(session, p, remaining_len);
Jerry Yu438ddd82022-07-07 06:55:50 +00003706#endif /* MBEDTLS_SSL_PROTO_TLS1_3 */
3707
Gilles Peskine449bd832023-01-11 14:50:10 +01003708 default:
3709 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01003710 }
3711}
3712
Manuel Pégourié-Gonnarda3e7c652019-05-16 10:08:35 +02003713/*
Manuel Pégourié-Gonnardb9dfc9f2019-07-12 10:50:19 +02003714 * Deserialize session: public wrapper for error cleaning
Manuel Pégourié-Gonnarda3d831b2019-05-23 12:28:45 +02003715 */
Gilles Peskine449bd832023-01-11 14:50:10 +01003716int mbedtls_ssl_session_load(mbedtls_ssl_session *session,
3717 const unsigned char *buf,
3718 size_t len)
Manuel Pégourié-Gonnarda3d831b2019-05-23 12:28:45 +02003719{
Gilles Peskine449bd832023-01-11 14:50:10 +01003720 int ret = ssl_session_load(session, 0, buf, len);
Manuel Pégourié-Gonnarda3d831b2019-05-23 12:28:45 +02003721
Gilles Peskine449bd832023-01-11 14:50:10 +01003722 if (ret != 0) {
3723 mbedtls_ssl_session_free(session);
3724 }
Manuel Pégourié-Gonnarda3d831b2019-05-23 12:28:45 +02003725
Gilles Peskine449bd832023-01-11 14:50:10 +01003726 return ret;
Manuel Pégourié-Gonnarda3d831b2019-05-23 12:28:45 +02003727}
3728
3729/*
Paul Bakker1961b702013-01-25 14:49:24 +01003730 * Perform a single step of the SSL handshake
Paul Bakker5121ce52009-01-03 21:22:43 +00003731 */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02003732MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01003733static int ssl_prepare_handshake_step(mbedtls_ssl_context *ssl)
Hanno Becker41934dd2021-08-07 19:13:43 +01003734{
3735 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
3736
Ronald Cron66dbf912022-02-02 15:33:46 +01003737 /*
3738 * We may have not been able to send to the peer all the handshake data
Ronald Cron3f20b772022-03-08 16:00:02 +01003739 * that were written into the output buffer by the previous handshake step,
3740 * if the write to the network callback returned with the
Ronald Cron66dbf912022-02-02 15:33:46 +01003741 * #MBEDTLS_ERR_SSL_WANT_WRITE error code.
3742 * We proceed to the next handshake step only when all data from the
3743 * previous one have been sent to the peer, thus we make sure that this is
3744 * the case here by calling `mbedtls_ssl_flush_output()`. The function may
3745 * return with the #MBEDTLS_ERR_SSL_WANT_WRITE error code in which case
3746 * we have to wait before to go ahead.
3747 * In the case of TLS 1.3, handshake step handlers do not send data to the
3748 * peer. Data are only sent here and through
3749 * `mbedtls_ssl_handle_pending_alert` in case an error that triggered an
Andrzej Kurek5c65c572022-04-13 14:28:52 -04003750 * alert occurred.
Ronald Cron66dbf912022-02-02 15:33:46 +01003751 */
Gilles Peskine449bd832023-01-11 14:50:10 +01003752 if ((ret = mbedtls_ssl_flush_output(ssl)) != 0) {
3753 return ret;
3754 }
Hanno Becker41934dd2021-08-07 19:13:43 +01003755
3756#if defined(MBEDTLS_SSL_PROTO_DTLS)
Gilles Peskine449bd832023-01-11 14:50:10 +01003757 if (ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
3758 ssl->handshake->retransmit_state == MBEDTLS_SSL_RETRANS_SENDING) {
3759 if ((ret = mbedtls_ssl_flight_transmit(ssl)) != 0) {
3760 return ret;
3761 }
Hanno Becker41934dd2021-08-07 19:13:43 +01003762 }
3763#endif /* MBEDTLS_SSL_PROTO_DTLS */
3764
Gilles Peskine449bd832023-01-11 14:50:10 +01003765 return ret;
Hanno Becker41934dd2021-08-07 19:13:43 +01003766}
3767
Gilles Peskine449bd832023-01-11 14:50:10 +01003768int mbedtls_ssl_handshake_step(mbedtls_ssl_context *ssl)
Paul Bakker5121ce52009-01-03 21:22:43 +00003769{
Hanno Becker41934dd2021-08-07 19:13:43 +01003770 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker5121ce52009-01-03 21:22:43 +00003771
Gilles Peskine449bd832023-01-11 14:50:10 +01003772 if (ssl == NULL ||
Hanno Becker41934dd2021-08-07 19:13:43 +01003773 ssl->conf == NULL ||
3774 ssl->handshake == NULL ||
Gilles Peskine449bd832023-01-11 14:50:10 +01003775 ssl->state == MBEDTLS_SSL_HANDSHAKE_OVER) {
3776 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
Hanno Becker41934dd2021-08-07 19:13:43 +01003777 }
3778
Gilles Peskine449bd832023-01-11 14:50:10 +01003779 ret = ssl_prepare_handshake_step(ssl);
3780 if (ret != 0) {
3781 return ret;
3782 }
Manuel Pégourié-Gonnardf81ee2e2015-09-01 17:43:40 +02003783
Gilles Peskine449bd832023-01-11 14:50:10 +01003784 ret = mbedtls_ssl_handle_pending_alert(ssl);
3785 if (ret != 0) {
Jerry Yue7047812021-09-13 19:26:39 +08003786 goto cleanup;
Gilles Peskine449bd832023-01-11 14:50:10 +01003787 }
Jerry Yue7047812021-09-13 19:26:39 +08003788
Tom Cosgrove2fdc7b32022-09-21 12:33:17 +01003789 /* If ssl->conf->endpoint is not one of MBEDTLS_SSL_IS_CLIENT or
3790 * MBEDTLS_SSL_IS_SERVER, this is the return code we give */
3791 ret = MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
3792
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003793#if defined(MBEDTLS_SSL_CLI_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01003794 if (ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT) {
3795 MBEDTLS_SSL_DEBUG_MSG(2, ("client state: %s",
3796 mbedtls_ssl_states_str(ssl->state)));
Jerry Yub9930e72021-08-06 17:11:51 +08003797
Gilles Peskine449bd832023-01-11 14:50:10 +01003798 switch (ssl->state) {
Ronald Cron9f0fba32022-02-10 16:45:15 +01003799 case MBEDTLS_SSL_HELLO_REQUEST:
3800 ssl->state = MBEDTLS_SSL_CLIENT_HELLO;
Tom Cosgrove87d9c6c2022-09-22 09:27:56 +01003801 ret = 0;
Ronald Cron9f0fba32022-02-10 16:45:15 +01003802 break;
Jerry Yub9930e72021-08-06 17:11:51 +08003803
Ronald Cron9f0fba32022-02-10 16:45:15 +01003804 case MBEDTLS_SSL_CLIENT_HELLO:
Gilles Peskine449bd832023-01-11 14:50:10 +01003805 ret = mbedtls_ssl_write_client_hello(ssl);
Ronald Cron9f0fba32022-02-10 16:45:15 +01003806 break;
3807
3808 default:
3809#if defined(MBEDTLS_SSL_PROTO_TLS1_2) && defined(MBEDTLS_SSL_PROTO_TLS1_3)
Gilles Peskine449bd832023-01-11 14:50:10 +01003810 if (ssl->tls_version == MBEDTLS_SSL_VERSION_TLS1_3) {
3811 ret = mbedtls_ssl_tls13_handshake_client_step(ssl);
3812 } else {
3813 ret = mbedtls_ssl_handshake_client_step(ssl);
3814 }
Ronald Cron9f0fba32022-02-10 16:45:15 +01003815#elif defined(MBEDTLS_SSL_PROTO_TLS1_2)
Gilles Peskine449bd832023-01-11 14:50:10 +01003816 ret = mbedtls_ssl_handshake_client_step(ssl);
Ronald Cron9f0fba32022-02-10 16:45:15 +01003817#else
Gilles Peskine449bd832023-01-11 14:50:10 +01003818 ret = mbedtls_ssl_tls13_handshake_client_step(ssl);
Ronald Cron9f0fba32022-02-10 16:45:15 +01003819#endif
3820 }
Jerry Yub9930e72021-08-06 17:11:51 +08003821 }
Paul Bakker5121ce52009-01-03 21:22:43 +00003822#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003823#if defined(MBEDTLS_SSL_SRV_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01003824 if (ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER) {
Ronald Cron6f135e12021-12-08 16:57:54 +01003825#if defined(MBEDTLS_SSL_PROTO_TLS1_3)
Gilles Peskine449bd832023-01-11 14:50:10 +01003826 if (mbedtls_ssl_conf_is_tls13_only(ssl->conf)) {
3827 ret = mbedtls_ssl_tls13_handshake_server_step(ssl);
3828 }
Ronald Cron6f135e12021-12-08 16:57:54 +01003829#endif /* MBEDTLS_SSL_PROTO_TLS1_3 */
Jerry Yub9930e72021-08-06 17:11:51 +08003830
3831#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
Gilles Peskine449bd832023-01-11 14:50:10 +01003832 if (mbedtls_ssl_conf_is_tls12_only(ssl->conf)) {
3833 ret = mbedtls_ssl_handshake_server_step(ssl);
3834 }
Jerry Yub9930e72021-08-06 17:11:51 +08003835#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
3836 }
Paul Bakker5121ce52009-01-03 21:22:43 +00003837#endif
3838
Gilles Peskine449bd832023-01-11 14:50:10 +01003839 if (ret != 0) {
Jerry Yubbd5a3f2021-09-18 20:50:22 +08003840 /* handshake_step return error. And it is same
3841 * with alert_reason.
3842 */
Gilles Peskine449bd832023-01-11 14:50:10 +01003843 if (ssl->send_alert) {
3844 ret = mbedtls_ssl_handle_pending_alert(ssl);
Jerry Yue7047812021-09-13 19:26:39 +08003845 goto cleanup;
3846 }
3847 }
3848
3849cleanup:
Gilles Peskine449bd832023-01-11 14:50:10 +01003850 return ret;
Paul Bakker1961b702013-01-25 14:49:24 +01003851}
3852
3853/*
3854 * Perform the SSL handshake
3855 */
Gilles Peskine449bd832023-01-11 14:50:10 +01003856int mbedtls_ssl_handshake(mbedtls_ssl_context *ssl)
Paul Bakker1961b702013-01-25 14:49:24 +01003857{
3858 int ret = 0;
3859
Hanno Beckera817ea42020-10-20 15:20:23 +01003860 /* Sanity checks */
3861
Gilles Peskine449bd832023-01-11 14:50:10 +01003862 if (ssl == NULL || ssl->conf == NULL) {
3863 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
3864 }
Manuel Pégourié-Gonnardf81ee2e2015-09-01 17:43:40 +02003865
Hanno Beckera817ea42020-10-20 15:20:23 +01003866#if defined(MBEDTLS_SSL_PROTO_DTLS)
Gilles Peskine449bd832023-01-11 14:50:10 +01003867 if (ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
3868 (ssl->f_set_timer == NULL || ssl->f_get_timer == NULL)) {
3869 MBEDTLS_SSL_DEBUG_MSG(1, ("You must use "
3870 "mbedtls_ssl_set_timer_cb() for DTLS"));
3871 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
Hanno Beckera817ea42020-10-20 15:20:23 +01003872 }
3873#endif /* MBEDTLS_SSL_PROTO_DTLS */
3874
Gilles Peskine449bd832023-01-11 14:50:10 +01003875 MBEDTLS_SSL_DEBUG_MSG(2, ("=> handshake"));
Paul Bakker1961b702013-01-25 14:49:24 +01003876
Hanno Beckera817ea42020-10-20 15:20:23 +01003877 /* Main handshake loop */
Gilles Peskine449bd832023-01-11 14:50:10 +01003878 while (ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER) {
3879 ret = mbedtls_ssl_handshake_step(ssl);
Paul Bakker1961b702013-01-25 14:49:24 +01003880
Gilles Peskine449bd832023-01-11 14:50:10 +01003881 if (ret != 0) {
Paul Bakker1961b702013-01-25 14:49:24 +01003882 break;
Gilles Peskine449bd832023-01-11 14:50:10 +01003883 }
Paul Bakker1961b702013-01-25 14:49:24 +01003884 }
3885
Gilles Peskine449bd832023-01-11 14:50:10 +01003886 MBEDTLS_SSL_DEBUG_MSG(2, ("<= handshake"));
Paul Bakker5121ce52009-01-03 21:22:43 +00003887
Gilles Peskine449bd832023-01-11 14:50:10 +01003888 return ret;
Paul Bakker5121ce52009-01-03 21:22:43 +00003889}
3890
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003891#if defined(MBEDTLS_SSL_RENEGOTIATION)
3892#if defined(MBEDTLS_SSL_SRV_C)
Paul Bakker5121ce52009-01-03 21:22:43 +00003893/*
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01003894 * Write HelloRequest to request renegotiation on server
Paul Bakker48916f92012-09-16 19:57:18 +00003895 */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02003896MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01003897static int ssl_write_hello_request(mbedtls_ssl_context *ssl)
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01003898{
Janos Follath865b3eb2019-12-16 11:46:15 +00003899 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01003900
Gilles Peskine449bd832023-01-11 14:50:10 +01003901 MBEDTLS_SSL_DEBUG_MSG(2, ("=> write hello request"));
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01003902
3903 ssl->out_msglen = 4;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003904 ssl->out_msgtype = MBEDTLS_SSL_MSG_HANDSHAKE;
3905 ssl->out_msg[0] = MBEDTLS_SSL_HS_HELLO_REQUEST;
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01003906
Gilles Peskine449bd832023-01-11 14:50:10 +01003907 if ((ret = mbedtls_ssl_write_handshake_msg(ssl)) != 0) {
3908 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ssl_write_handshake_msg", ret);
3909 return ret;
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01003910 }
3911
Gilles Peskine449bd832023-01-11 14:50:10 +01003912 MBEDTLS_SSL_DEBUG_MSG(2, ("<= write hello request"));
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01003913
Gilles Peskine449bd832023-01-11 14:50:10 +01003914 return 0;
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01003915}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003916#endif /* MBEDTLS_SSL_SRV_C */
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01003917
3918/*
3919 * Actually renegotiate current connection, triggered by either:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003920 * - any side: calling mbedtls_ssl_renegotiate(),
3921 * - client: receiving a HelloRequest during mbedtls_ssl_read(),
3922 * - server: receiving any handshake message on server during mbedtls_ssl_read() after
Manuel Pégourié-Gonnard55e4ff22014-08-19 11:16:35 +02003923 * the initial handshake is completed.
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01003924 * If the handshake doesn't complete due to waiting for I/O, it will continue
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003925 * during the next calls to mbedtls_ssl_renegotiate() or mbedtls_ssl_read() respectively.
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01003926 */
Gilles Peskine449bd832023-01-11 14:50:10 +01003927int mbedtls_ssl_start_renegotiation(mbedtls_ssl_context *ssl)
Paul Bakker48916f92012-09-16 19:57:18 +00003928{
Janos Follath865b3eb2019-12-16 11:46:15 +00003929 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker48916f92012-09-16 19:57:18 +00003930
Gilles Peskine449bd832023-01-11 14:50:10 +01003931 MBEDTLS_SSL_DEBUG_MSG(2, ("=> renegotiate"));
Paul Bakker48916f92012-09-16 19:57:18 +00003932
Gilles Peskine449bd832023-01-11 14:50:10 +01003933 if ((ret = ssl_handshake_init(ssl)) != 0) {
3934 return ret;
3935 }
Paul Bakker48916f92012-09-16 19:57:18 +00003936
Manuel Pégourié-Gonnard0557bd52014-08-19 19:18:39 +02003937 /* RFC 6347 4.2.2: "[...] the HelloRequest will have message_seq = 0 and
3938 * the ServerHello will have message_seq = 1" */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003939#if defined(MBEDTLS_SSL_PROTO_DTLS)
Gilles Peskine449bd832023-01-11 14:50:10 +01003940 if (ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
3941 ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_PENDING) {
3942 if (ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER) {
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02003943 ssl->handshake->out_msg_seq = 1;
Gilles Peskine449bd832023-01-11 14:50:10 +01003944 } else {
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02003945 ssl->handshake->in_msg_seq = 1;
Gilles Peskine449bd832023-01-11 14:50:10 +01003946 }
Manuel Pégourié-Gonnard0557bd52014-08-19 19:18:39 +02003947 }
3948#endif
3949
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003950 ssl->state = MBEDTLS_SSL_HELLO_REQUEST;
3951 ssl->renego_status = MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS;
Paul Bakker48916f92012-09-16 19:57:18 +00003952
Gilles Peskine449bd832023-01-11 14:50:10 +01003953 if ((ret = mbedtls_ssl_handshake(ssl)) != 0) {
3954 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ssl_handshake", ret);
3955 return ret;
Paul Bakker48916f92012-09-16 19:57:18 +00003956 }
3957
Gilles Peskine449bd832023-01-11 14:50:10 +01003958 MBEDTLS_SSL_DEBUG_MSG(2, ("<= renegotiate"));
Paul Bakker48916f92012-09-16 19:57:18 +00003959
Gilles Peskine449bd832023-01-11 14:50:10 +01003960 return 0;
Paul Bakker48916f92012-09-16 19:57:18 +00003961}
3962
3963/*
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01003964 * Renegotiate current connection on client,
3965 * or request renegotiation on server
3966 */
Gilles Peskine449bd832023-01-11 14:50:10 +01003967int mbedtls_ssl_renegotiate(mbedtls_ssl_context *ssl)
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01003968{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003969 int ret = MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01003970
Gilles Peskine449bd832023-01-11 14:50:10 +01003971 if (ssl == NULL || ssl->conf == NULL) {
3972 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
3973 }
Manuel Pégourié-Gonnardf81ee2e2015-09-01 17:43:40 +02003974
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003975#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01003976 /* On server, just send the request */
Gilles Peskine449bd832023-01-11 14:50:10 +01003977 if (ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER) {
3978 if (mbedtls_ssl_is_handshake_over(ssl) == 0) {
3979 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
3980 }
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01003981
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003982 ssl->renego_status = MBEDTLS_SSL_RENEGOTIATION_PENDING;
Manuel Pégourié-Gonnardf07f4212014-08-15 19:04:47 +02003983
3984 /* Did we already try/start sending HelloRequest? */
Gilles Peskine449bd832023-01-11 14:50:10 +01003985 if (ssl->out_left != 0) {
3986 return mbedtls_ssl_flush_output(ssl);
3987 }
Manuel Pégourié-Gonnardf07f4212014-08-15 19:04:47 +02003988
Gilles Peskine449bd832023-01-11 14:50:10 +01003989 return ssl_write_hello_request(ssl);
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01003990 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003991#endif /* MBEDTLS_SSL_SRV_C */
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01003992
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003993#if defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01003994 /*
3995 * On client, either start the renegotiation process or,
3996 * if already in progress, continue the handshake
3997 */
Gilles Peskine449bd832023-01-11 14:50:10 +01003998 if (ssl->renego_status != MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS) {
3999 if (mbedtls_ssl_is_handshake_over(ssl) == 0) {
4000 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01004001 }
Gilles Peskine449bd832023-01-11 14:50:10 +01004002
4003 if ((ret = mbedtls_ssl_start_renegotiation(ssl)) != 0) {
4004 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ssl_start_renegotiation", ret);
4005 return ret;
4006 }
4007 } else {
4008 if ((ret = mbedtls_ssl_handshake(ssl)) != 0) {
4009 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ssl_handshake", ret);
4010 return ret;
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01004011 }
4012 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004013#endif /* MBEDTLS_SSL_CLI_C */
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01004014
Gilles Peskine449bd832023-01-11 14:50:10 +01004015 return ret;
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01004016}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004017#endif /* MBEDTLS_SSL_RENEGOTIATION */
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01004018
Gilles Peskine449bd832023-01-11 14:50:10 +01004019void mbedtls_ssl_handshake_free(mbedtls_ssl_context *ssl)
Paul Bakker48916f92012-09-16 19:57:18 +00004020{
Gilles Peskine9b562d52018-04-25 20:32:43 +02004021 mbedtls_ssl_handshake_params *handshake = ssl->handshake;
4022
Gilles Peskine449bd832023-01-11 14:50:10 +01004023 if (handshake == NULL) {
Paul Bakkeraccaffe2014-06-26 13:37:14 +02004024 return;
Gilles Peskine449bd832023-01-11 14:50:10 +01004025 }
Paul Bakkeraccaffe2014-06-26 13:37:14 +02004026
Brett Warrene0edc842021-08-17 09:53:13 +01004027#if defined(MBEDTLS_ECP_C)
4028#if !defined(MBEDTLS_DEPRECATED_REMOVED)
Gilles Peskine449bd832023-01-11 14:50:10 +01004029 if (ssl->handshake->group_list_heap_allocated) {
4030 mbedtls_free((void *) handshake->group_list);
4031 }
Brett Warrene0edc842021-08-17 09:53:13 +01004032 handshake->group_list = NULL;
4033#endif /* MBEDTLS_DEPRECATED_REMOVED */
4034#endif /* MBEDTLS_ECP_C */
4035
Ronald Crone68ab4f2022-10-05 12:46:29 +02004036#if defined(MBEDTLS_SSL_HANDSHAKE_WITH_CERT_ENABLED)
Jerry Yuf017ee42022-01-12 15:49:48 +08004037#if !defined(MBEDTLS_DEPRECATED_REMOVED)
Gilles Peskine449bd832023-01-11 14:50:10 +01004038 if (ssl->handshake->sig_algs_heap_allocated) {
4039 mbedtls_free((void *) handshake->sig_algs);
4040 }
Jerry Yuf017ee42022-01-12 15:49:48 +08004041 handshake->sig_algs = NULL;
4042#endif /* MBEDTLS_DEPRECATED_REMOVED */
Xiaofei Baic234ecf2022-02-08 09:59:23 +00004043#if defined(MBEDTLS_SSL_PROTO_TLS1_3)
Gilles Peskine449bd832023-01-11 14:50:10 +01004044 if (ssl->handshake->certificate_request_context) {
4045 mbedtls_free((void *) handshake->certificate_request_context);
Xiaofei Baic234ecf2022-02-08 09:59:23 +00004046 }
4047#endif /* MBEDTLS_SSL_PROTO_TLS1_3 */
Ronald Crone68ab4f2022-10-05 12:46:29 +02004048#endif /* MBEDTLS_SSL_HANDSHAKE_WITH_CERT_ENABLED */
Jerry Yuf017ee42022-01-12 15:49:48 +08004049
Gilles Peskinedf13d5c2018-04-25 20:39:48 +02004050#if defined(MBEDTLS_SSL_ASYNC_PRIVATE)
Gilles Peskine449bd832023-01-11 14:50:10 +01004051 if (ssl->conf->f_async_cancel != NULL && handshake->async_in_progress != 0) {
4052 ssl->conf->f_async_cancel(ssl);
Gilles Peskinedf13d5c2018-04-25 20:39:48 +02004053 handshake->async_in_progress = 0;
4054 }
4055#endif /* MBEDTLS_SSL_ASYNC_PRIVATE */
4056
Andrzej Kurek25f27152022-08-17 16:09:31 -04004057#if defined(MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Andrzej Kurekeb342242019-01-29 09:14:33 -05004058#if defined(MBEDTLS_USE_PSA_CRYPTO)
Gilles Peskine449bd832023-01-11 14:50:10 +01004059 psa_hash_abort(&handshake->fin_sha256_psa);
Andrzej Kurekeb342242019-01-29 09:14:33 -05004060#else
Gilles Peskine449bd832023-01-11 14:50:10 +01004061 mbedtls_sha256_free(&handshake->fin_sha256);
Manuel Pégourié-Gonnardb9d64e52015-07-06 14:18:56 +02004062#endif
Andrzej Kurekeb342242019-01-29 09:14:33 -05004063#endif
Andrzej Kurek25f27152022-08-17 16:09:31 -04004064#if defined(MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Andrzej Kurekeb342242019-01-29 09:14:33 -05004065#if defined(MBEDTLS_USE_PSA_CRYPTO)
Gilles Peskine449bd832023-01-11 14:50:10 +01004066 psa_hash_abort(&handshake->fin_sha384_psa);
Andrzej Kurekeb342242019-01-29 09:14:33 -05004067#else
Gilles Peskine449bd832023-01-11 14:50:10 +01004068 mbedtls_sha512_free(&handshake->fin_sha384);
Manuel Pégourié-Gonnardb9d64e52015-07-06 14:18:56 +02004069#endif
Andrzej Kurekeb342242019-01-29 09:14:33 -05004070#endif
Manuel Pégourié-Gonnardb9d64e52015-07-06 14:18:56 +02004071
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004072#if defined(MBEDTLS_DHM_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01004073 mbedtls_dhm_free(&handshake->dhm_ctx);
Paul Bakker48916f92012-09-16 19:57:18 +00004074#endif
Neil Armstrongf3f46412022-04-12 14:43:39 +02004075#if !defined(MBEDTLS_USE_PSA_CRYPTO) && defined(MBEDTLS_ECDH_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01004076 mbedtls_ecdh_free(&handshake->ecdh_ctx);
Paul Bakker61d113b2013-07-04 11:51:43 +02004077#endif
Valerio Setti02c25b52022-11-15 14:08:42 +01004078
Manuel Pégourié-Gonnardeef142d2015-09-16 10:05:04 +02004079#if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
Neil Armstrongca7d5062022-05-31 14:43:23 +02004080#if defined(MBEDTLS_USE_PSA_CRYPTO)
Gilles Peskine449bd832023-01-11 14:50:10 +01004081 psa_pake_abort(&handshake->psa_pake_ctx);
Valerio Settieb3f7882022-12-08 18:42:58 +01004082 /*
4083 * Opaque keys are not stored in the handshake's data and it's the user
4084 * responsibility to destroy them. Clear ones, instead, are created by
4085 * the TLS library and should be destroyed at the same level
4086 */
Gilles Peskine449bd832023-01-11 14:50:10 +01004087 if (!mbedtls_svc_key_id_is_null(handshake->psa_pake_password)) {
4088 psa_destroy_key(handshake->psa_pake_password);
Valerio Settieb3f7882022-12-08 18:42:58 +01004089 }
Neil Armstrongca7d5062022-05-31 14:43:23 +02004090 handshake->psa_pake_password = MBEDTLS_SVC_KEY_ID_INIT;
4091#else
Gilles Peskine449bd832023-01-11 14:50:10 +01004092 mbedtls_ecjpake_free(&handshake->ecjpake_ctx);
Neil Armstrongca7d5062022-05-31 14:43:23 +02004093#endif /* MBEDTLS_USE_PSA_CRYPTO */
Manuel Pégourié-Gonnard77c06462015-09-17 13:59:49 +02004094#if defined(MBEDTLS_SSL_CLI_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01004095 mbedtls_free(handshake->ecjpake_cache);
Manuel Pégourié-Gonnard77c06462015-09-17 13:59:49 +02004096 handshake->ecjpake_cache = NULL;
4097 handshake->ecjpake_cache_len = 0;
4098#endif
Manuel Pégourié-Gonnard76cfd3f2015-09-15 12:10:54 +02004099#endif
Paul Bakker61d113b2013-07-04 11:51:43 +02004100
Janos Follath4ae5c292016-02-10 11:27:43 +00004101#if defined(MBEDTLS_ECDH_C) || defined(MBEDTLS_ECDSA_C) || \
4102 defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
Paul Bakker9af723c2014-05-01 13:03:14 +02004103 /* explicit void pointer cast for buggy MS compiler */
Gilles Peskine449bd832023-01-11 14:50:10 +01004104 mbedtls_free((void *) handshake->curves_tls_id);
Manuel Pégourié-Gonnardd09453c2013-09-23 19:11:32 +02004105#endif
4106
Ronald Cron73fe8df2022-10-05 14:31:43 +02004107#if defined(MBEDTLS_SSL_HANDSHAKE_WITH_PSK_ENABLED)
Neil Armstrong501c9322022-05-03 09:35:09 +02004108#if defined(MBEDTLS_USE_PSA_CRYPTO)
Gilles Peskine449bd832023-01-11 14:50:10 +01004109 if (!mbedtls_svc_key_id_is_null(ssl->handshake->psk_opaque)) {
Neil Armstrong501c9322022-05-03 09:35:09 +02004110 /* The maintenance of the external PSK key slot is the
4111 * user's responsibility. */
Gilles Peskine449bd832023-01-11 14:50:10 +01004112 if (ssl->handshake->psk_opaque_is_internal) {
4113 psa_destroy_key(ssl->handshake->psk_opaque);
Neil Armstrong501c9322022-05-03 09:35:09 +02004114 ssl->handshake->psk_opaque_is_internal = 0;
4115 }
4116 ssl->handshake->psk_opaque = MBEDTLS_SVC_KEY_ID_INIT;
4117 }
Neil Armstronge952a302022-05-03 10:22:14 +02004118#else
Gilles Peskine449bd832023-01-11 14:50:10 +01004119 if (handshake->psk != NULL) {
4120 mbedtls_platform_zeroize(handshake->psk, handshake->psk_len);
4121 mbedtls_free(handshake->psk);
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01004122 }
Neil Armstronge952a302022-05-03 10:22:14 +02004123#endif /* MBEDTLS_USE_PSA_CRYPTO */
Ronald Cron73fe8df2022-10-05 14:31:43 +02004124#endif /* MBEDTLS_SSL_HANDSHAKE_WITH_PSK_ENABLED */
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01004125
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004126#if defined(MBEDTLS_X509_CRT_PARSE_C) && \
4127 defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
Manuel Pégourié-Gonnard83724542013-09-24 22:30:56 +02004128 /*
4129 * Free only the linked list wrapper, not the keys themselves
4130 * since the belong to the SNI callback
4131 */
Gilles Peskine449bd832023-01-11 14:50:10 +01004132 ssl_key_cert_free(handshake->sni_key_cert);
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004133#endif /* MBEDTLS_X509_CRT_PARSE_C && MBEDTLS_SSL_SERVER_NAME_INDICATION */
Manuel Pégourié-Gonnard705fcca2013-09-23 20:04:20 +02004134
Gilles Peskineeccd8882020-03-10 12:19:08 +01004135#if defined(MBEDTLS_SSL_ECP_RESTARTABLE_ENABLED)
Gilles Peskine449bd832023-01-11 14:50:10 +01004136 mbedtls_x509_crt_restart_free(&handshake->ecrs_ctx);
4137 if (handshake->ecrs_peer_cert != NULL) {
4138 mbedtls_x509_crt_free(handshake->ecrs_peer_cert);
4139 mbedtls_free(handshake->ecrs_peer_cert);
Hanno Becker3dad3112019-02-05 17:19:52 +00004140 }
Manuel Pégourié-Gonnard862cde52017-05-17 11:56:15 +02004141#endif
4142
Hanno Becker75173122019-02-06 16:18:31 +00004143#if defined(MBEDTLS_X509_CRT_PARSE_C) && \
4144 !defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
Gilles Peskine449bd832023-01-11 14:50:10 +01004145 mbedtls_pk_free(&handshake->peer_pubkey);
Hanno Becker75173122019-02-06 16:18:31 +00004146#endif /* MBEDTLS_X509_CRT_PARSE_C && !MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
4147
XiaokangQian9b93c0d2022-02-09 06:02:25 +00004148#if defined(MBEDTLS_SSL_CLI_C) && \
Gilles Peskine449bd832023-01-11 14:50:10 +01004149 (defined(MBEDTLS_SSL_PROTO_DTLS) || defined(MBEDTLS_SSL_PROTO_TLS1_3))
4150 mbedtls_free(handshake->cookie);
XiaokangQian9b93c0d2022-02-09 06:02:25 +00004151#endif /* MBEDTLS_SSL_CLI_C &&
4152 ( MBEDTLS_SSL_PROTO_DTLS || MBEDTLS_SSL_PROTO_TLS1_3 ) */
XiaokangQian8499b6c2022-01-27 09:00:11 +00004153
4154#if defined(MBEDTLS_SSL_PROTO_DTLS)
Gilles Peskine449bd832023-01-11 14:50:10 +01004155 mbedtls_ssl_flight_free(handshake->flight);
4156 mbedtls_ssl_buffering_free(ssl);
XiaokangQian8499b6c2022-01-27 09:00:11 +00004157#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnard74848812014-07-11 02:43:49 +02004158
Ronald Cronf12b81d2022-03-15 10:42:41 +01004159#if defined(MBEDTLS_ECDH_C) && \
Gilles Peskine449bd832023-01-11 14:50:10 +01004160 (defined(MBEDTLS_USE_PSA_CRYPTO) || defined(MBEDTLS_SSL_PROTO_TLS1_3))
4161 if (handshake->ecdh_psa_privkey_is_external == 0) {
4162 psa_destroy_key(handshake->ecdh_psa_privkey);
4163 }
Hanno Becker4a63ed42019-01-08 11:39:35 +00004164#endif /* MBEDTLS_ECDH_C && MBEDTLS_USE_PSA_CRYPTO */
4165
Ronald Cron6f135e12021-12-08 16:57:54 +01004166#if defined(MBEDTLS_SSL_PROTO_TLS1_3)
Gilles Peskine449bd832023-01-11 14:50:10 +01004167 mbedtls_ssl_transform_free(handshake->transform_handshake);
4168 mbedtls_free(handshake->transform_handshake);
Jerry Yu3d9b5902022-11-04 14:07:25 +08004169#if defined(MBEDTLS_SSL_EARLY_DATA)
Gilles Peskine449bd832023-01-11 14:50:10 +01004170 mbedtls_ssl_transform_free(handshake->transform_earlydata);
4171 mbedtls_free(handshake->transform_earlydata);
Jerry Yu3d9b5902022-11-04 14:07:25 +08004172#endif
Ronald Cron6f135e12021-12-08 16:57:54 +01004173#endif /* MBEDTLS_SSL_PROTO_TLS1_3 */
Jerry Yuba9c7272021-10-30 11:54:10 +08004174
Andrzej Kurek0afa2a12020-03-03 10:39:58 -05004175
4176#if defined(MBEDTLS_SSL_VARIABLE_BUFFER_LENGTH)
4177 /* If the buffers are too big - reallocate. Because of the way Mbed TLS
4178 * processes datagrams and the fact that a datagram is allowed to have
4179 * several records in it, it is possible that the I/O buffers are not
4180 * empty at this stage */
Gilles Peskine449bd832023-01-11 14:50:10 +01004181 handle_buffer_resizing(ssl, 1, mbedtls_ssl_get_input_buflen(ssl),
4182 mbedtls_ssl_get_output_buflen(ssl));
Andrzej Kurek0afa2a12020-03-03 10:39:58 -05004183#endif
Hanno Becker3aa186f2021-08-10 09:24:19 +01004184
Jerry Yuba9c7272021-10-30 11:54:10 +08004185 /* mbedtls_platform_zeroize MUST be last one in this function */
Gilles Peskine449bd832023-01-11 14:50:10 +01004186 mbedtls_platform_zeroize(handshake,
4187 sizeof(mbedtls_ssl_handshake_params));
Paul Bakker48916f92012-09-16 19:57:18 +00004188}
4189
Gilles Peskine449bd832023-01-11 14:50:10 +01004190void mbedtls_ssl_session_free(mbedtls_ssl_session *session)
Paul Bakker48916f92012-09-16 19:57:18 +00004191{
Gilles Peskine449bd832023-01-11 14:50:10 +01004192 if (session == NULL) {
Paul Bakkeraccaffe2014-06-26 13:37:14 +02004193 return;
Gilles Peskine449bd832023-01-11 14:50:10 +01004194 }
Paul Bakkeraccaffe2014-06-26 13:37:14 +02004195
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004196#if defined(MBEDTLS_X509_CRT_PARSE_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01004197 ssl_clear_peer_cert(session);
Paul Bakkered27a042013-04-18 22:46:23 +02004198#endif
Paul Bakker0a597072012-09-25 21:55:46 +00004199
Manuel Pégourié-Gonnardb596abf2015-05-20 10:45:29 +02004200#if defined(MBEDTLS_SSL_SESSION_TICKETS) && defined(MBEDTLS_SSL_CLI_C)
Xiaokang Qianbc663a02022-10-09 11:14:39 +00004201#if defined(MBEDTLS_SSL_PROTO_TLS1_3) && \
4202 defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
Gilles Peskine449bd832023-01-11 14:50:10 +01004203 mbedtls_free(session->hostname);
Xiaokang Qian281fd1b2022-09-20 11:35:41 +00004204#endif
Gilles Peskine449bd832023-01-11 14:50:10 +01004205 mbedtls_free(session->ticket);
Paul Bakkera503a632013-08-14 13:48:06 +02004206#endif
Manuel Pégourié-Gonnard75d44012013-08-02 14:44:04 +02004207
Gilles Peskine449bd832023-01-11 14:50:10 +01004208 mbedtls_platform_zeroize(session, sizeof(mbedtls_ssl_session));
Paul Bakker48916f92012-09-16 19:57:18 +00004209}
4210
Manuel Pégourié-Gonnard5c0e3772019-07-23 16:13:17 +02004211#if defined(MBEDTLS_SSL_CONTEXT_SERIALIZATION)
Manuel Pégourié-Gonnard4e9370b2019-07-23 16:31:16 +02004212
4213#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
4214#define SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_CONNECTION_ID 1u
4215#else
4216#define SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_CONNECTION_ID 0u
4217#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
4218
Manuel Pégourié-Gonnard4e9370b2019-07-23 16:31:16 +02004219#define SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_BADMAC_LIMIT 1u
Manuel Pégourié-Gonnard4e9370b2019-07-23 16:31:16 +02004220
4221#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
4222#define SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_ANTI_REPLAY 1u
4223#else
4224#define SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_ANTI_REPLAY 0u
4225#endif /* MBEDTLS_SSL_DTLS_ANTI_REPLAY */
4226
4227#if defined(MBEDTLS_SSL_ALPN)
4228#define SSL_SERIALIZED_CONTEXT_CONFIG_ALPN 1u
4229#else
4230#define SSL_SERIALIZED_CONTEXT_CONFIG_ALPN 0u
4231#endif /* MBEDTLS_SSL_ALPN */
4232
4233#define SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_CONNECTION_ID_BIT 0
4234#define SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_BADMAC_LIMIT_BIT 1
4235#define SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_ANTI_REPLAY_BIT 2
4236#define SSL_SERIALIZED_CONTEXT_CONFIG_ALPN_BIT 3
4237
4238#define SSL_SERIALIZED_CONTEXT_CONFIG_BITFLAG \
Gilles Peskine449bd832023-01-11 14:50:10 +01004239 ((uint32_t) ( \
4240 (SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_CONNECTION_ID << \
4241 SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_CONNECTION_ID_BIT) | \
4242 (SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_BADMAC_LIMIT << \
4243 SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_BADMAC_LIMIT_BIT) | \
4244 (SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_ANTI_REPLAY << \
4245 SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_ANTI_REPLAY_BIT) | \
4246 (SSL_SERIALIZED_CONTEXT_CONFIG_ALPN << SSL_SERIALIZED_CONTEXT_CONFIG_ALPN_BIT) | \
4247 0u))
Manuel Pégourié-Gonnard4e9370b2019-07-23 16:31:16 +02004248
Manuel Pégourié-Gonnard4c90e852019-07-11 10:58:10 +02004249static unsigned char ssl_serialized_context_header[] = {
4250 MBEDTLS_VERSION_MAJOR,
4251 MBEDTLS_VERSION_MINOR,
4252 MBEDTLS_VERSION_PATCH,
Gilles Peskine449bd832023-01-11 14:50:10 +01004253 MBEDTLS_BYTE_1(SSL_SERIALIZED_SESSION_CONFIG_BITFLAG),
4254 MBEDTLS_BYTE_0(SSL_SERIALIZED_SESSION_CONFIG_BITFLAG),
4255 MBEDTLS_BYTE_2(SSL_SERIALIZED_CONTEXT_CONFIG_BITFLAG),
4256 MBEDTLS_BYTE_1(SSL_SERIALIZED_CONTEXT_CONFIG_BITFLAG),
4257 MBEDTLS_BYTE_0(SSL_SERIALIZED_CONTEXT_CONFIG_BITFLAG),
Manuel Pégourié-Gonnard4c90e852019-07-11 10:58:10 +02004258};
4259
Paul Bakker5121ce52009-01-03 21:22:43 +00004260/*
Manuel Pégourié-Gonnardac87e282019-05-28 13:02:16 +02004261 * Serialize a full SSL context
Manuel Pégourié-Gonnard00400c22019-07-10 14:58:45 +02004262 *
4263 * The format of the serialized data is:
4264 * (in the presentation language of TLS, RFC 8446 section 3)
4265 *
4266 * // header
4267 * opaque mbedtls_version[3]; // major, minor, patch
Manuel Pégourié-Gonnard4c90e852019-07-11 10:58:10 +02004268 * opaque context_format[5]; // version-specific field determining
Manuel Pégourié-Gonnard00400c22019-07-10 14:58:45 +02004269 * // the format of the remaining
Manuel Pégourié-Gonnard4c90e852019-07-11 10:58:10 +02004270 * // serialized data.
Manuel Pégourié-Gonnard4e9370b2019-07-23 16:31:16 +02004271 * Note: When updating the format, remember to keep these
4272 * version+format bytes. (We may make their size part of the API.)
Manuel Pégourié-Gonnard00400c22019-07-10 14:58:45 +02004273 *
4274 * // session sub-structure
4275 * opaque session<1..2^32-1>; // see mbedtls_ssl_session_save()
4276 * // transform sub-structure
4277 * uint8 random[64]; // ServerHello.random+ClientHello.random
4278 * uint8 in_cid<0..2^8-1> // Connection ID: expected incoming value
4279 * uint8 out_cid<0..2^8-1> // Connection ID: outgoing value to use
4280 * // fields from ssl_context
4281 * uint32 badmac_seen; // DTLS: number of records with failing MAC
4282 * uint64 in_window_top; // DTLS: last validated record seq_num
4283 * uint64 in_window; // DTLS: bitmask for replay protection
4284 * uint8 disable_datagram_packing; // DTLS: only one record per datagram
4285 * uint64 cur_out_ctr; // Record layer: outgoing sequence number
4286 * uint16 mtu; // DTLS: path mtu (max outgoing fragment size)
4287 * uint8 alpn_chosen<0..2^8-1> // ALPN: negotiated application protocol
4288 *
4289 * Note that many fields of the ssl_context or sub-structures are not
4290 * serialized, as they fall in one of the following categories:
4291 *
4292 * 1. forced value (eg in_left must be 0)
4293 * 2. pointer to dynamically-allocated memory (eg session, transform)
4294 * 3. value can be re-derived from other data (eg session keys from MS)
4295 * 4. value was temporary (eg content of input buffer)
4296 * 5. value will be provided by the user again (eg I/O callbacks and context)
Manuel Pégourié-Gonnardac87e282019-05-28 13:02:16 +02004297 */
Gilles Peskine449bd832023-01-11 14:50:10 +01004298int mbedtls_ssl_context_save(mbedtls_ssl_context *ssl,
4299 unsigned char *buf,
4300 size_t buf_len,
4301 size_t *olen)
Manuel Pégourié-Gonnardac87e282019-05-28 13:02:16 +02004302{
Manuel Pégourié-Gonnard4c90e852019-07-11 10:58:10 +02004303 unsigned char *p = buf;
4304 size_t used = 0;
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02004305 size_t session_len;
4306 int ret = 0;
Manuel Pégourié-Gonnard4c90e852019-07-11 10:58:10 +02004307
Manuel Pégourié-Gonnard1aaf6692019-07-10 14:14:05 +02004308 /*
Manuel Pégourié-Gonnarde4588692019-07-29 12:28:52 +02004309 * Enforce usage restrictions, see "return BAD_INPUT_DATA" in
4310 * this function's documentation.
4311 *
4312 * These are due to assumptions/limitations in the implementation. Some of
4313 * them are likely to stay (no handshake in progress) some might go away
4314 * (only DTLS) but are currently used to simplify the implementation.
Manuel Pégourié-Gonnard1aaf6692019-07-10 14:14:05 +02004315 */
Manuel Pégourié-Gonnarde4588692019-07-29 12:28:52 +02004316 /* The initial handshake must be over */
Gilles Peskine449bd832023-01-11 14:50:10 +01004317 if (mbedtls_ssl_is_handshake_over(ssl) == 0) {
4318 MBEDTLS_SSL_DEBUG_MSG(1, ("Initial handshake isn't over"));
4319 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
Jarno Lamsa8c51b7c2019-08-21 13:45:05 +03004320 }
Gilles Peskine449bd832023-01-11 14:50:10 +01004321 if (ssl->handshake != NULL) {
4322 MBEDTLS_SSL_DEBUG_MSG(1, ("Handshake isn't completed"));
4323 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
Jarno Lamsa8c51b7c2019-08-21 13:45:05 +03004324 }
Manuel Pégourié-Gonnarde4588692019-07-29 12:28:52 +02004325 /* Double-check that sub-structures are indeed ready */
Gilles Peskine449bd832023-01-11 14:50:10 +01004326 if (ssl->transform == NULL || ssl->session == NULL) {
4327 MBEDTLS_SSL_DEBUG_MSG(1, ("Serialised structures aren't ready"));
4328 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
Jarno Lamsa8c51b7c2019-08-21 13:45:05 +03004329 }
Manuel Pégourié-Gonnarde4588692019-07-29 12:28:52 +02004330 /* There must be no pending incoming or outgoing data */
Gilles Peskine449bd832023-01-11 14:50:10 +01004331 if (mbedtls_ssl_check_pending(ssl) != 0) {
4332 MBEDTLS_SSL_DEBUG_MSG(1, ("There is pending incoming data"));
4333 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
Jarno Lamsa8c51b7c2019-08-21 13:45:05 +03004334 }
Gilles Peskine449bd832023-01-11 14:50:10 +01004335 if (ssl->out_left != 0) {
4336 MBEDTLS_SSL_DEBUG_MSG(1, ("There is pending outgoing data"));
4337 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
Jarno Lamsa8c51b7c2019-08-21 13:45:05 +03004338 }
Dave Rodgman556e8a32022-12-06 16:31:25 +00004339 /* Protocol must be DTLS, not TLS */
Gilles Peskine449bd832023-01-11 14:50:10 +01004340 if (ssl->conf->transport != MBEDTLS_SSL_TRANSPORT_DATAGRAM) {
4341 MBEDTLS_SSL_DEBUG_MSG(1, ("Only DTLS is supported"));
4342 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
Jarno Lamsa8c51b7c2019-08-21 13:45:05 +03004343 }
Manuel Pégourié-Gonnarde4588692019-07-29 12:28:52 +02004344 /* Version must be 1.2 */
Gilles Peskine449bd832023-01-11 14:50:10 +01004345 if (ssl->tls_version != MBEDTLS_SSL_VERSION_TLS1_2) {
4346 MBEDTLS_SSL_DEBUG_MSG(1, ("Only version 1.2 supported"));
4347 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
Jarno Lamsa8c51b7c2019-08-21 13:45:05 +03004348 }
Manuel Pégourié-Gonnarde4588692019-07-29 12:28:52 +02004349 /* We must be using an AEAD ciphersuite */
Gilles Peskine449bd832023-01-11 14:50:10 +01004350 if (mbedtls_ssl_transform_uses_aead(ssl->transform) != 1) {
4351 MBEDTLS_SSL_DEBUG_MSG(1, ("Only AEAD ciphersuites supported"));
4352 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
Jarno Lamsa8c51b7c2019-08-21 13:45:05 +03004353 }
Manuel Pégourié-Gonnarde4588692019-07-29 12:28:52 +02004354 /* Renegotiation must not be enabled */
4355#if defined(MBEDTLS_SSL_RENEGOTIATION)
Gilles Peskine449bd832023-01-11 14:50:10 +01004356 if (ssl->conf->disable_renegotiation != MBEDTLS_SSL_RENEGOTIATION_DISABLED) {
4357 MBEDTLS_SSL_DEBUG_MSG(1, ("Renegotiation must not be enabled"));
4358 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
Jarno Lamsa8c51b7c2019-08-21 13:45:05 +03004359 }
Manuel Pégourié-Gonnarde4588692019-07-29 12:28:52 +02004360#endif
Manuel Pégourié-Gonnardac87e282019-05-28 13:02:16 +02004361
Manuel Pégourié-Gonnard4c90e852019-07-11 10:58:10 +02004362 /*
4363 * Version and format identifier
4364 */
Gilles Peskine449bd832023-01-11 14:50:10 +01004365 used += sizeof(ssl_serialized_context_header);
Manuel Pégourié-Gonnardac87e282019-05-28 13:02:16 +02004366
Gilles Peskine449bd832023-01-11 14:50:10 +01004367 if (used <= buf_len) {
4368 memcpy(p, ssl_serialized_context_header,
4369 sizeof(ssl_serialized_context_header));
4370 p += sizeof(ssl_serialized_context_header);
Manuel Pégourié-Gonnard4c90e852019-07-11 10:58:10 +02004371 }
4372
4373 /*
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02004374 * Session (length + data)
4375 */
Gilles Peskine449bd832023-01-11 14:50:10 +01004376 ret = ssl_session_save(ssl->session, 1, NULL, 0, &session_len);
4377 if (ret != MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL) {
4378 return ret;
4379 }
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02004380
4381 used += 4 + session_len;
Gilles Peskine449bd832023-01-11 14:50:10 +01004382 if (used <= buf_len) {
4383 MBEDTLS_PUT_UINT32_BE(session_len, p, 0);
Joe Subbiani1f6c3ae2021-08-20 11:44:44 +01004384 p += 4;
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02004385
Gilles Peskine449bd832023-01-11 14:50:10 +01004386 ret = ssl_session_save(ssl->session, 1,
4387 p, session_len, &session_len);
4388 if (ret != 0) {
4389 return ret;
4390 }
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02004391
4392 p += session_len;
4393 }
4394
4395 /*
Manuel Pégourié-Gonnardc2a7b892019-07-15 09:04:11 +02004396 * Transform
4397 */
Gilles Peskine449bd832023-01-11 14:50:10 +01004398 used += sizeof(ssl->transform->randbytes);
4399 if (used <= buf_len) {
4400 memcpy(p, ssl->transform->randbytes,
4401 sizeof(ssl->transform->randbytes));
4402 p += sizeof(ssl->transform->randbytes);
Manuel Pégourié-Gonnardc2a7b892019-07-15 09:04:11 +02004403 }
4404
4405#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
4406 used += 2 + ssl->transform->in_cid_len + ssl->transform->out_cid_len;
Gilles Peskine449bd832023-01-11 14:50:10 +01004407 if (used <= buf_len) {
Manuel Pégourié-Gonnardc2a7b892019-07-15 09:04:11 +02004408 *p++ = ssl->transform->in_cid_len;
Gilles Peskine449bd832023-01-11 14:50:10 +01004409 memcpy(p, ssl->transform->in_cid, ssl->transform->in_cid_len);
Manuel Pégourié-Gonnardc2a7b892019-07-15 09:04:11 +02004410 p += ssl->transform->in_cid_len;
4411
4412 *p++ = ssl->transform->out_cid_len;
Gilles Peskine449bd832023-01-11 14:50:10 +01004413 memcpy(p, ssl->transform->out_cid, ssl->transform->out_cid_len);
Manuel Pégourié-Gonnardc2a7b892019-07-15 09:04:11 +02004414 p += ssl->transform->out_cid_len;
4415 }
4416#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
4417
4418 /*
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02004419 * Saved fields from top-level ssl_context structure
4420 */
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02004421 used += 4;
Gilles Peskine449bd832023-01-11 14:50:10 +01004422 if (used <= buf_len) {
4423 MBEDTLS_PUT_UINT32_BE(ssl->badmac_seen, p, 0);
Joe Subbiani1f6c3ae2021-08-20 11:44:44 +01004424 p += 4;
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02004425 }
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02004426
4427#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
4428 used += 16;
Gilles Peskine449bd832023-01-11 14:50:10 +01004429 if (used <= buf_len) {
4430 MBEDTLS_PUT_UINT64_BE(ssl->in_window_top, p, 0);
Joe Subbiani1f6c3ae2021-08-20 11:44:44 +01004431 p += 8;
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02004432
Gilles Peskine449bd832023-01-11 14:50:10 +01004433 MBEDTLS_PUT_UINT64_BE(ssl->in_window, p, 0);
Joe Subbiani1f6c3ae2021-08-20 11:44:44 +01004434 p += 8;
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02004435 }
4436#endif /* MBEDTLS_SSL_DTLS_ANTI_REPLAY */
4437
4438#if defined(MBEDTLS_SSL_PROTO_DTLS)
4439 used += 1;
Gilles Peskine449bd832023-01-11 14:50:10 +01004440 if (used <= buf_len) {
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02004441 *p++ = ssl->disable_datagram_packing;
4442 }
4443#endif /* MBEDTLS_SSL_PROTO_DTLS */
4444
Jerry Yuae0b2e22021-10-08 15:21:19 +08004445 used += MBEDTLS_SSL_SEQUENCE_NUMBER_LEN;
Gilles Peskine449bd832023-01-11 14:50:10 +01004446 if (used <= buf_len) {
4447 memcpy(p, ssl->cur_out_ctr, MBEDTLS_SSL_SEQUENCE_NUMBER_LEN);
Jerry Yuae0b2e22021-10-08 15:21:19 +08004448 p += MBEDTLS_SSL_SEQUENCE_NUMBER_LEN;
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02004449 }
4450
4451#if defined(MBEDTLS_SSL_PROTO_DTLS)
4452 used += 2;
Gilles Peskine449bd832023-01-11 14:50:10 +01004453 if (used <= buf_len) {
4454 MBEDTLS_PUT_UINT16_BE(ssl->mtu, p, 0);
Joe Subbiani1f6c3ae2021-08-20 11:44:44 +01004455 p += 2;
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02004456 }
4457#endif /* MBEDTLS_SSL_PROTO_DTLS */
4458
4459#if defined(MBEDTLS_SSL_ALPN)
4460 {
4461 const uint8_t alpn_len = ssl->alpn_chosen
Gilles Peskine449bd832023-01-11 14:50:10 +01004462 ? (uint8_t) strlen(ssl->alpn_chosen)
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02004463 : 0;
4464
4465 used += 1 + alpn_len;
Gilles Peskine449bd832023-01-11 14:50:10 +01004466 if (used <= buf_len) {
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02004467 *p++ = alpn_len;
4468
Gilles Peskine449bd832023-01-11 14:50:10 +01004469 if (ssl->alpn_chosen != NULL) {
4470 memcpy(p, ssl->alpn_chosen, alpn_len);
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02004471 p += alpn_len;
4472 }
4473 }
4474 }
4475#endif /* MBEDTLS_SSL_ALPN */
4476
4477 /*
Manuel Pégourié-Gonnard4c90e852019-07-11 10:58:10 +02004478 * Done
4479 */
4480 *olen = used;
4481
Gilles Peskine449bd832023-01-11 14:50:10 +01004482 if (used > buf_len) {
4483 return MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL;
4484 }
Manuel Pégourié-Gonnardac87e282019-05-28 13:02:16 +02004485
Gilles Peskine449bd832023-01-11 14:50:10 +01004486 MBEDTLS_SSL_DEBUG_BUF(4, "saved context", buf, used);
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02004487
Gilles Peskine449bd832023-01-11 14:50:10 +01004488 return mbedtls_ssl_session_reset_int(ssl, 0);
Manuel Pégourié-Gonnardac87e282019-05-28 13:02:16 +02004489}
4490
4491/*
Manuel Pégourié-Gonnardb9dfc9f2019-07-12 10:50:19 +02004492 * Deserialize context, see mbedtls_ssl_context_save() for format.
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02004493 *
4494 * This internal version is wrapped by a public function that cleans up in
4495 * case of error.
Manuel Pégourié-Gonnardac87e282019-05-28 13:02:16 +02004496 */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02004497MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01004498static int ssl_context_load(mbedtls_ssl_context *ssl,
4499 const unsigned char *buf,
4500 size_t len)
Manuel Pégourié-Gonnardac87e282019-05-28 13:02:16 +02004501{
Manuel Pégourié-Gonnard4c90e852019-07-11 10:58:10 +02004502 const unsigned char *p = buf;
4503 const unsigned char * const end = buf + len;
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02004504 size_t session_len;
Janos Follath865b3eb2019-12-16 11:46:15 +00004505 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Andrzej Kurek2d59dbc2022-10-13 08:34:38 -04004506#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
Andrzej Kurek894edde2022-09-29 06:31:14 -04004507 tls_prf_fn prf_func = NULL;
Andrzej Kurek2d59dbc2022-10-13 08:34:38 -04004508#endif
Manuel Pégourié-Gonnard4c90e852019-07-11 10:58:10 +02004509
Manuel Pégourié-Gonnard0ff76402019-07-11 09:56:30 +02004510 /*
4511 * The context should have been freshly setup or reset.
4512 * Give the user an error in case of obvious misuse.
Manuel Pégourié-Gonnard4ca930f2019-07-26 16:31:53 +02004513 * (Checking session is useful because it won't be NULL if we're
Manuel Pégourié-Gonnard0ff76402019-07-11 09:56:30 +02004514 * renegotiating, or if the user mistakenly loaded a session first.)
4515 */
Gilles Peskine449bd832023-01-11 14:50:10 +01004516 if (ssl->state != MBEDTLS_SSL_HELLO_REQUEST ||
4517 ssl->session != NULL) {
4518 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
Manuel Pégourié-Gonnard0ff76402019-07-11 09:56:30 +02004519 }
4520
4521 /*
4522 * We can't check that the config matches the initial one, but we can at
4523 * least check it matches the requirements for serializing.
4524 */
Gilles Peskine449bd832023-01-11 14:50:10 +01004525 if (ssl->conf->transport != MBEDTLS_SSL_TRANSPORT_DATAGRAM ||
Glenn Strauss2dfcea22022-03-14 17:26:42 -04004526 ssl->conf->max_tls_version < MBEDTLS_SSL_VERSION_TLS1_2 ||
4527 ssl->conf->min_tls_version > MBEDTLS_SSL_VERSION_TLS1_2 ||
Manuel Pégourié-Gonnard0ff76402019-07-11 09:56:30 +02004528#if defined(MBEDTLS_SSL_RENEGOTIATION)
Manuel Pégourié-Gonnard9a96fd72019-07-23 17:11:24 +02004529 ssl->conf->disable_renegotiation != MBEDTLS_SSL_RENEGOTIATION_DISABLED ||
Manuel Pégourié-Gonnard0ff76402019-07-11 09:56:30 +02004530#endif
Gilles Peskine449bd832023-01-11 14:50:10 +01004531 0) {
4532 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
Manuel Pégourié-Gonnard0ff76402019-07-11 09:56:30 +02004533 }
4534
Gilles Peskine449bd832023-01-11 14:50:10 +01004535 MBEDTLS_SSL_DEBUG_BUF(4, "context to load", buf, len);
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02004536
Manuel Pégourié-Gonnard4c90e852019-07-11 10:58:10 +02004537 /*
4538 * Check version identifier
4539 */
Gilles Peskine449bd832023-01-11 14:50:10 +01004540 if ((size_t) (end - p) < sizeof(ssl_serialized_context_header)) {
4541 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
Manuel Pégourié-Gonnard4c90e852019-07-11 10:58:10 +02004542 }
Gilles Peskine449bd832023-01-11 14:50:10 +01004543
4544 if (memcmp(p, ssl_serialized_context_header,
4545 sizeof(ssl_serialized_context_header)) != 0) {
4546 return MBEDTLS_ERR_SSL_VERSION_MISMATCH;
4547 }
4548 p += sizeof(ssl_serialized_context_header);
Manuel Pégourié-Gonnard4c90e852019-07-11 10:58:10 +02004549
4550 /*
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02004551 * Session
4552 */
Gilles Peskine449bd832023-01-11 14:50:10 +01004553 if ((size_t) (end - p) < 4) {
4554 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
4555 }
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02004556
Gilles Peskine449bd832023-01-11 14:50:10 +01004557 session_len = ((size_t) p[0] << 24) |
4558 ((size_t) p[1] << 16) |
4559 ((size_t) p[2] << 8) |
4560 ((size_t) p[3]);
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02004561 p += 4;
4562
Manuel Pégourié-Gonnard142ba732019-07-23 14:43:30 +02004563 /* This has been allocated by ssl_handshake_init(), called by
Hanno Becker43aefe22020-02-05 10:44:56 +00004564 * by either mbedtls_ssl_session_reset_int() or mbedtls_ssl_setup(). */
Manuel Pégourié-Gonnard142ba732019-07-23 14:43:30 +02004565 ssl->session = ssl->session_negotiate;
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02004566 ssl->session_in = ssl->session;
4567 ssl->session_out = ssl->session;
Manuel Pégourié-Gonnard142ba732019-07-23 14:43:30 +02004568 ssl->session_negotiate = NULL;
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02004569
Gilles Peskine449bd832023-01-11 14:50:10 +01004570 if ((size_t) (end - p) < session_len) {
4571 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
4572 }
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02004573
Gilles Peskine449bd832023-01-11 14:50:10 +01004574 ret = ssl_session_load(ssl->session, 1, p, session_len);
4575 if (ret != 0) {
4576 mbedtls_ssl_session_free(ssl->session);
4577 return ret;
Manuel Pégourié-Gonnard45ac1f02019-07-23 16:52:45 +02004578 }
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02004579
4580 p += session_len;
4581
4582 /*
Manuel Pégourié-Gonnardc2a7b892019-07-15 09:04:11 +02004583 * Transform
4584 */
4585
Manuel Pégourié-Gonnard142ba732019-07-23 14:43:30 +02004586 /* This has been allocated by ssl_handshake_init(), called by
Hanno Becker43aefe22020-02-05 10:44:56 +00004587 * by either mbedtls_ssl_session_reset_int() or mbedtls_ssl_setup(). */
Jerry Yu2e199812022-12-01 18:57:19 +08004588#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
Manuel Pégourié-Gonnard142ba732019-07-23 14:43:30 +02004589 ssl->transform = ssl->transform_negotiate;
Manuel Pégourié-Gonnardc2a7b892019-07-15 09:04:11 +02004590 ssl->transform_in = ssl->transform;
4591 ssl->transform_out = ssl->transform;
Manuel Pégourié-Gonnard142ba732019-07-23 14:43:30 +02004592 ssl->transform_negotiate = NULL;
Jerry Yu2e199812022-12-01 18:57:19 +08004593#endif
Manuel Pégourié-Gonnardc2a7b892019-07-15 09:04:11 +02004594
Andrzej Kurek2d59dbc2022-10-13 08:34:38 -04004595#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
Gilles Peskine449bd832023-01-11 14:50:10 +01004596 prf_func = ssl_tls12prf_from_cs(ssl->session->ciphersuite);
4597 if (prf_func == NULL) {
4598 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
4599 }
Andrzej Kurek894edde2022-09-29 06:31:14 -04004600
Manuel Pégourié-Gonnardc2a7b892019-07-15 09:04:11 +02004601 /* Read random bytes and populate structure */
Gilles Peskine449bd832023-01-11 14:50:10 +01004602 if ((size_t) (end - p) < sizeof(ssl->transform->randbytes)) {
4603 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
4604 }
Andrzej Kurek2d59dbc2022-10-13 08:34:38 -04004605
Gilles Peskine449bd832023-01-11 14:50:10 +01004606 ret = ssl_tls12_populate_transform(ssl->transform,
4607 ssl->session->ciphersuite,
4608 ssl->session->master,
Neil Armstrongf2c82f02022-04-05 11:16:53 +02004609#if defined(MBEDTLS_SSL_SOME_SUITES_USE_CBC_ETM)
Gilles Peskine449bd832023-01-11 14:50:10 +01004610 ssl->session->encrypt_then_mac,
Neil Armstrongf2c82f02022-04-05 11:16:53 +02004611#endif /* MBEDTLS_SSL_SOME_SUITES_USE_CBC_ETM */
Gilles Peskine449bd832023-01-11 14:50:10 +01004612 prf_func,
4613 p, /* currently pointing to randbytes */
4614 MBEDTLS_SSL_VERSION_TLS1_2, /* (D)TLS 1.2 is forced */
4615 ssl->conf->endpoint,
4616 ssl);
4617 if (ret != 0) {
4618 return ret;
4619 }
Jerry Yu840fbb22022-02-17 14:59:29 +08004620#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Gilles Peskine449bd832023-01-11 14:50:10 +01004621 p += sizeof(ssl->transform->randbytes);
Manuel Pégourié-Gonnardc2a7b892019-07-15 09:04:11 +02004622
4623#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
4624 /* Read connection IDs and store them */
Gilles Peskine449bd832023-01-11 14:50:10 +01004625 if ((size_t) (end - p) < 1) {
4626 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
4627 }
Manuel Pégourié-Gonnardc2a7b892019-07-15 09:04:11 +02004628
4629 ssl->transform->in_cid_len = *p++;
4630
Gilles Peskine449bd832023-01-11 14:50:10 +01004631 if ((size_t) (end - p) < ssl->transform->in_cid_len + 1u) {
4632 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
4633 }
Manuel Pégourié-Gonnardc2a7b892019-07-15 09:04:11 +02004634
Gilles Peskine449bd832023-01-11 14:50:10 +01004635 memcpy(ssl->transform->in_cid, p, ssl->transform->in_cid_len);
Manuel Pégourié-Gonnardc2a7b892019-07-15 09:04:11 +02004636 p += ssl->transform->in_cid_len;
4637
4638 ssl->transform->out_cid_len = *p++;
4639
Gilles Peskine449bd832023-01-11 14:50:10 +01004640 if ((size_t) (end - p) < ssl->transform->out_cid_len) {
4641 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
4642 }
Manuel Pégourié-Gonnardc2a7b892019-07-15 09:04:11 +02004643
Gilles Peskine449bd832023-01-11 14:50:10 +01004644 memcpy(ssl->transform->out_cid, p, ssl->transform->out_cid_len);
Manuel Pégourié-Gonnardc2a7b892019-07-15 09:04:11 +02004645 p += ssl->transform->out_cid_len;
4646#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
4647
4648 /*
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02004649 * Saved fields from top-level ssl_context structure
4650 */
Gilles Peskine449bd832023-01-11 14:50:10 +01004651 if ((size_t) (end - p) < 4) {
4652 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
4653 }
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02004654
Gilles Peskine449bd832023-01-11 14:50:10 +01004655 ssl->badmac_seen = ((uint32_t) p[0] << 24) |
4656 ((uint32_t) p[1] << 16) |
4657 ((uint32_t) p[2] << 8) |
4658 ((uint32_t) p[3]);
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02004659 p += 4;
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02004660
4661#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
Gilles Peskine449bd832023-01-11 14:50:10 +01004662 if ((size_t) (end - p) < 16) {
4663 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
4664 }
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02004665
Gilles Peskine449bd832023-01-11 14:50:10 +01004666 ssl->in_window_top = ((uint64_t) p[0] << 56) |
4667 ((uint64_t) p[1] << 48) |
4668 ((uint64_t) p[2] << 40) |
4669 ((uint64_t) p[3] << 32) |
4670 ((uint64_t) p[4] << 24) |
4671 ((uint64_t) p[5] << 16) |
4672 ((uint64_t) p[6] << 8) |
4673 ((uint64_t) p[7]);
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02004674 p += 8;
4675
Gilles Peskine449bd832023-01-11 14:50:10 +01004676 ssl->in_window = ((uint64_t) p[0] << 56) |
4677 ((uint64_t) p[1] << 48) |
4678 ((uint64_t) p[2] << 40) |
4679 ((uint64_t) p[3] << 32) |
4680 ((uint64_t) p[4] << 24) |
4681 ((uint64_t) p[5] << 16) |
4682 ((uint64_t) p[6] << 8) |
4683 ((uint64_t) p[7]);
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02004684 p += 8;
4685#endif /* MBEDTLS_SSL_DTLS_ANTI_REPLAY */
4686
4687#if defined(MBEDTLS_SSL_PROTO_DTLS)
Gilles Peskine449bd832023-01-11 14:50:10 +01004688 if ((size_t) (end - p) < 1) {
4689 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
4690 }
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02004691
4692 ssl->disable_datagram_packing = *p++;
4693#endif /* MBEDTLS_SSL_PROTO_DTLS */
4694
Gilles Peskine449bd832023-01-11 14:50:10 +01004695 if ((size_t) (end - p) < sizeof(ssl->cur_out_ctr)) {
4696 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
4697 }
4698 memcpy(ssl->cur_out_ctr, p, sizeof(ssl->cur_out_ctr));
4699 p += sizeof(ssl->cur_out_ctr);
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02004700
4701#if defined(MBEDTLS_SSL_PROTO_DTLS)
Gilles Peskine449bd832023-01-11 14:50:10 +01004702 if ((size_t) (end - p) < 2) {
4703 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
4704 }
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02004705
Gilles Peskine449bd832023-01-11 14:50:10 +01004706 ssl->mtu = (p[0] << 8) | p[1];
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02004707 p += 2;
4708#endif /* MBEDTLS_SSL_PROTO_DTLS */
4709
4710#if defined(MBEDTLS_SSL_ALPN)
4711 {
4712 uint8_t alpn_len;
4713 const char **cur;
4714
Gilles Peskine449bd832023-01-11 14:50:10 +01004715 if ((size_t) (end - p) < 1) {
4716 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
4717 }
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02004718
4719 alpn_len = *p++;
4720
Gilles Peskine449bd832023-01-11 14:50:10 +01004721 if (alpn_len != 0 && ssl->conf->alpn_list != NULL) {
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02004722 /* alpn_chosen should point to an item in the configured list */
Gilles Peskine449bd832023-01-11 14:50:10 +01004723 for (cur = ssl->conf->alpn_list; *cur != NULL; cur++) {
4724 if (strlen(*cur) == alpn_len &&
4725 memcmp(p, cur, alpn_len) == 0) {
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02004726 ssl->alpn_chosen = *cur;
4727 break;
4728 }
4729 }
4730 }
4731
4732 /* can only happen on conf mismatch */
Gilles Peskine449bd832023-01-11 14:50:10 +01004733 if (alpn_len != 0 && ssl->alpn_chosen == NULL) {
4734 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
4735 }
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02004736
4737 p += alpn_len;
4738 }
4739#endif /* MBEDTLS_SSL_ALPN */
4740
4741 /*
Manuel Pégourié-Gonnard0eb3eac2019-07-15 11:53:51 +02004742 * Forced fields from top-level ssl_context structure
4743 *
4744 * Most of them already set to the correct value by mbedtls_ssl_init() and
4745 * mbedtls_ssl_reset(), so we only need to set the remaining ones.
4746 */
4747 ssl->state = MBEDTLS_SSL_HANDSHAKE_OVER;
Glenn Strauss60bfe602022-03-14 19:04:24 -04004748 ssl->tls_version = MBEDTLS_SSL_VERSION_TLS1_2;
Manuel Pégourié-Gonnard0eb3eac2019-07-15 11:53:51 +02004749
Hanno Becker361b10d2019-08-30 10:42:49 +01004750 /* Adjust pointers for header fields of outgoing records to
4751 * the given transform, accounting for explicit IV and CID. */
Gilles Peskine449bd832023-01-11 14:50:10 +01004752 mbedtls_ssl_update_out_pointers(ssl, ssl->transform);
Hanno Becker361b10d2019-08-30 10:42:49 +01004753
Manuel Pégourié-Gonnard0eb3eac2019-07-15 11:53:51 +02004754#if defined(MBEDTLS_SSL_PROTO_DTLS)
4755 ssl->in_epoch = 1;
4756#endif
4757
4758 /* mbedtls_ssl_reset() leaves the handshake sub-structure allocated,
4759 * which we don't want - otherwise we'd end up freeing the wrong transform
Hanno Beckerce5f5fd2020-02-05 10:47:44 +00004760 * by calling mbedtls_ssl_handshake_wrapup_free_hs_transform()
4761 * inappropriately. */
Gilles Peskine449bd832023-01-11 14:50:10 +01004762 if (ssl->handshake != NULL) {
4763 mbedtls_ssl_handshake_free(ssl);
4764 mbedtls_free(ssl->handshake);
Manuel Pégourié-Gonnard0eb3eac2019-07-15 11:53:51 +02004765 ssl->handshake = NULL;
4766 }
4767
4768 /*
Manuel Pégourié-Gonnard4c90e852019-07-11 10:58:10 +02004769 * Done - should have consumed entire buffer
4770 */
Gilles Peskine449bd832023-01-11 14:50:10 +01004771 if (p != end) {
4772 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
4773 }
Manuel Pégourié-Gonnardac87e282019-05-28 13:02:16 +02004774
Gilles Peskine449bd832023-01-11 14:50:10 +01004775 return 0;
Manuel Pégourié-Gonnardac87e282019-05-28 13:02:16 +02004776}
4777
4778/*
Manuel Pégourié-Gonnardb9dfc9f2019-07-12 10:50:19 +02004779 * Deserialize context: public wrapper for error cleaning
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02004780 */
Gilles Peskine449bd832023-01-11 14:50:10 +01004781int mbedtls_ssl_context_load(mbedtls_ssl_context *context,
4782 const unsigned char *buf,
4783 size_t len)
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02004784{
Gilles Peskine449bd832023-01-11 14:50:10 +01004785 int ret = ssl_context_load(context, buf, len);
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02004786
Gilles Peskine449bd832023-01-11 14:50:10 +01004787 if (ret != 0) {
4788 mbedtls_ssl_free(context);
4789 }
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02004790
Gilles Peskine449bd832023-01-11 14:50:10 +01004791 return ret;
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02004792}
Manuel Pégourié-Gonnard5c0e3772019-07-23 16:13:17 +02004793#endif /* MBEDTLS_SSL_CONTEXT_SERIALIZATION */
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02004794
4795/*
Paul Bakker5121ce52009-01-03 21:22:43 +00004796 * Free an SSL context
4797 */
Gilles Peskine449bd832023-01-11 14:50:10 +01004798void mbedtls_ssl_free(mbedtls_ssl_context *ssl)
Paul Bakker5121ce52009-01-03 21:22:43 +00004799{
Gilles Peskine449bd832023-01-11 14:50:10 +01004800 if (ssl == NULL) {
Paul Bakkeraccaffe2014-06-26 13:37:14 +02004801 return;
Gilles Peskine449bd832023-01-11 14:50:10 +01004802 }
Paul Bakkeraccaffe2014-06-26 13:37:14 +02004803
Gilles Peskine449bd832023-01-11 14:50:10 +01004804 MBEDTLS_SSL_DEBUG_MSG(2, ("=> free"));
Paul Bakker5121ce52009-01-03 21:22:43 +00004805
Gilles Peskine449bd832023-01-11 14:50:10 +01004806 if (ssl->out_buf != NULL) {
sander-visserb8aa2072020-05-06 22:05:13 +02004807#if defined(MBEDTLS_SSL_VARIABLE_BUFFER_LENGTH)
4808 size_t out_buf_len = ssl->out_buf_len;
4809#else
4810 size_t out_buf_len = MBEDTLS_SSL_OUT_BUFFER_LEN;
4811#endif
4812
Gilles Peskine449bd832023-01-11 14:50:10 +01004813 mbedtls_platform_zeroize(ssl->out_buf, out_buf_len);
4814 mbedtls_free(ssl->out_buf);
Andrzej Kurek0afa2a12020-03-03 10:39:58 -05004815 ssl->out_buf = NULL;
Paul Bakker5121ce52009-01-03 21:22:43 +00004816 }
4817
Gilles Peskine449bd832023-01-11 14:50:10 +01004818 if (ssl->in_buf != NULL) {
sander-visserb8aa2072020-05-06 22:05:13 +02004819#if defined(MBEDTLS_SSL_VARIABLE_BUFFER_LENGTH)
4820 size_t in_buf_len = ssl->in_buf_len;
4821#else
4822 size_t in_buf_len = MBEDTLS_SSL_IN_BUFFER_LEN;
4823#endif
4824
Gilles Peskine449bd832023-01-11 14:50:10 +01004825 mbedtls_platform_zeroize(ssl->in_buf, in_buf_len);
4826 mbedtls_free(ssl->in_buf);
Andrzej Kurek0afa2a12020-03-03 10:39:58 -05004827 ssl->in_buf = NULL;
Paul Bakker5121ce52009-01-03 21:22:43 +00004828 }
4829
Gilles Peskine449bd832023-01-11 14:50:10 +01004830 if (ssl->transform) {
4831 mbedtls_ssl_transform_free(ssl->transform);
4832 mbedtls_free(ssl->transform);
Paul Bakker48916f92012-09-16 19:57:18 +00004833 }
4834
Gilles Peskine449bd832023-01-11 14:50:10 +01004835 if (ssl->handshake) {
4836 mbedtls_ssl_handshake_free(ssl);
4837 mbedtls_free(ssl->handshake);
Jerry Yu2e199812022-12-01 18:57:19 +08004838
4839#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
Gilles Peskine449bd832023-01-11 14:50:10 +01004840 mbedtls_ssl_transform_free(ssl->transform_negotiate);
4841 mbedtls_free(ssl->transform_negotiate);
Jerry Yu2e199812022-12-01 18:57:19 +08004842#endif
4843
Gilles Peskine449bd832023-01-11 14:50:10 +01004844 mbedtls_ssl_session_free(ssl->session_negotiate);
4845 mbedtls_free(ssl->session_negotiate);
Paul Bakker48916f92012-09-16 19:57:18 +00004846 }
4847
Ronald Cron6f135e12021-12-08 16:57:54 +01004848#if defined(MBEDTLS_SSL_PROTO_TLS1_3)
Gilles Peskine449bd832023-01-11 14:50:10 +01004849 mbedtls_ssl_transform_free(ssl->transform_application);
4850 mbedtls_free(ssl->transform_application);
Ronald Cron6f135e12021-12-08 16:57:54 +01004851#endif /* MBEDTLS_SSL_PROTO_TLS1_3 */
Hanno Becker3aa186f2021-08-10 09:24:19 +01004852
Gilles Peskine449bd832023-01-11 14:50:10 +01004853 if (ssl->session) {
4854 mbedtls_ssl_session_free(ssl->session);
4855 mbedtls_free(ssl->session);
Paul Bakkerc0463502013-02-14 11:19:38 +01004856 }
4857
Manuel Pégourié-Gonnard55fab2d2015-05-11 16:15:19 +02004858#if defined(MBEDTLS_X509_CRT_PARSE_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01004859 if (ssl->hostname != NULL) {
4860 mbedtls_platform_zeroize(ssl->hostname, strlen(ssl->hostname));
4861 mbedtls_free(ssl->hostname);
Paul Bakker5121ce52009-01-03 21:22:43 +00004862 }
Paul Bakker0be444a2013-08-27 21:55:01 +02004863#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00004864
Manuel Pégourié-Gonnarde057d3b2015-05-20 10:59:43 +02004865#if defined(MBEDTLS_SSL_DTLS_HELLO_VERIFY) && defined(MBEDTLS_SSL_SRV_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01004866 mbedtls_free(ssl->cli_id);
Manuel Pégourié-Gonnard43c02182014-07-22 17:32:01 +02004867#endif
4868
Gilles Peskine449bd832023-01-11 14:50:10 +01004869 MBEDTLS_SSL_DEBUG_MSG(2, ("<= free"));
Paul Bakker2da561c2009-02-05 18:00:28 +00004870
Paul Bakker86f04f42013-02-14 11:20:09 +01004871 /* Actually clear after last debug message */
Gilles Peskine449bd832023-01-11 14:50:10 +01004872 mbedtls_platform_zeroize(ssl, sizeof(mbedtls_ssl_context));
Paul Bakker5121ce52009-01-03 21:22:43 +00004873}
4874
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02004875/*
Shaun Case8b0ecbc2021-12-20 21:14:10 -08004876 * Initialize mbedtls_ssl_config
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02004877 */
Gilles Peskine449bd832023-01-11 14:50:10 +01004878void mbedtls_ssl_config_init(mbedtls_ssl_config *conf)
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02004879{
Gilles Peskine449bd832023-01-11 14:50:10 +01004880 memset(conf, 0, sizeof(mbedtls_ssl_config));
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02004881}
4882
Gilles Peskineae270bf2021-06-02 00:05:29 +02004883/* The selection should be the same as mbedtls_x509_crt_profile_default in
4884 * x509_crt.c, plus Montgomery curves for ECDHE. Here, the order matters:
Gilles Peskineb1940a72021-06-02 15:18:12 +02004885 * curves with a lower resource usage come first.
Gilles Peskineae270bf2021-06-02 00:05:29 +02004886 * See the documentation of mbedtls_ssl_conf_curves() for what we promise
Gilles Peskineb1940a72021-06-02 15:18:12 +02004887 * about this list.
4888 */
Brett Warrene0edc842021-08-17 09:53:13 +01004889static uint16_t ssl_preset_default_groups[] = {
Gilles Peskineae270bf2021-06-02 00:05:29 +02004890#if defined(MBEDTLS_ECP_DP_CURVE25519_ENABLED)
Brett Warrene0edc842021-08-17 09:53:13 +01004891 MBEDTLS_SSL_IANA_TLS_GROUP_X25519,
Gilles Peskineae270bf2021-06-02 00:05:29 +02004892#endif
4893#if defined(MBEDTLS_ECP_DP_SECP256R1_ENABLED)
Brett Warrene0edc842021-08-17 09:53:13 +01004894 MBEDTLS_SSL_IANA_TLS_GROUP_SECP256R1,
Gilles Peskineae270bf2021-06-02 00:05:29 +02004895#endif
Gilles Peskineb1940a72021-06-02 15:18:12 +02004896#if defined(MBEDTLS_ECP_DP_SECP384R1_ENABLED)
Brett Warrene0edc842021-08-17 09:53:13 +01004897 MBEDTLS_SSL_IANA_TLS_GROUP_SECP384R1,
Gilles Peskineb1940a72021-06-02 15:18:12 +02004898#endif
4899#if defined(MBEDTLS_ECP_DP_CURVE448_ENABLED)
Brett Warrene0edc842021-08-17 09:53:13 +01004900 MBEDTLS_SSL_IANA_TLS_GROUP_X448,
Gilles Peskineb1940a72021-06-02 15:18:12 +02004901#endif
4902#if defined(MBEDTLS_ECP_DP_SECP521R1_ENABLED)
Brett Warrene0edc842021-08-17 09:53:13 +01004903 MBEDTLS_SSL_IANA_TLS_GROUP_SECP521R1,
Gilles Peskineb1940a72021-06-02 15:18:12 +02004904#endif
Gilles Peskineae270bf2021-06-02 00:05:29 +02004905#if defined(MBEDTLS_ECP_DP_BP256R1_ENABLED)
Brett Warrene0edc842021-08-17 09:53:13 +01004906 MBEDTLS_SSL_IANA_TLS_GROUP_BP256R1,
Gilles Peskineae270bf2021-06-02 00:05:29 +02004907#endif
Gilles Peskineb1940a72021-06-02 15:18:12 +02004908#if defined(MBEDTLS_ECP_DP_BP384R1_ENABLED)
Brett Warrene0edc842021-08-17 09:53:13 +01004909 MBEDTLS_SSL_IANA_TLS_GROUP_BP384R1,
Gilles Peskineb1940a72021-06-02 15:18:12 +02004910#endif
4911#if defined(MBEDTLS_ECP_DP_BP512R1_ENABLED)
Brett Warrene0edc842021-08-17 09:53:13 +01004912 MBEDTLS_SSL_IANA_TLS_GROUP_BP512R1,
Gilles Peskineb1940a72021-06-02 15:18:12 +02004913#endif
Brett Warrene0edc842021-08-17 09:53:13 +01004914 MBEDTLS_SSL_IANA_TLS_GROUP_NONE
Gilles Peskineae270bf2021-06-02 00:05:29 +02004915};
Gilles Peskineae270bf2021-06-02 00:05:29 +02004916
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02004917static int ssl_preset_suiteb_ciphersuites[] = {
4918 MBEDTLS_TLS_ECDHE_ECDSA_WITH_AES_128_GCM_SHA256,
4919 MBEDTLS_TLS_ECDHE_ECDSA_WITH_AES_256_GCM_SHA384,
4920 0
4921};
4922
Ronald Crone68ab4f2022-10-05 12:46:29 +02004923#if defined(MBEDTLS_SSL_HANDSHAKE_WITH_CERT_ENABLED)
Hanno Becker9c6aa7b2021-08-10 13:50:43 +01004924
Jerry Yu909df7b2022-01-22 11:56:27 +08004925/* NOTICE:
Jerry Yu0b994b82022-01-25 17:22:12 +08004926 * For ssl_preset_*_sig_algs and ssl_tls12_preset_*_sig_algs, the following
Jerry Yu370e1462022-01-25 10:36:53 +08004927 * rules SHOULD be upheld.
4928 * - No duplicate entries.
4929 * - But if there is a good reason, do not change the order of the algorithms.
Jerry Yu09a99fc2022-07-28 14:22:17 +08004930 * - ssl_tls12_preset* is for TLS 1.2 use only.
Jerry Yu370e1462022-01-25 10:36:53 +08004931 * - ssl_preset_* is for TLS 1.3 only or hybrid TLS 1.3/1.2 handshakes.
Jerry Yu1a8b4812022-01-20 17:56:50 +08004932 */
Hanno Becker9c6aa7b2021-08-10 13:50:43 +01004933static uint16_t ssl_preset_default_sig_algs[] = {
Jerry Yu1a8b4812022-01-20 17:56:50 +08004934
Andrzej Kurek25f27152022-08-17 16:09:31 -04004935#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 +08004936 defined(MBEDTLS_ECP_DP_SECP256R1_ENABLED)
4937 MBEDTLS_TLS1_3_SIG_ECDSA_SECP256R1_SHA256,
Andrzej Kurekcccb0442022-08-19 03:42:11 -04004938#endif /* MBEDTLS_ECDSA_C && MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA &&
Jerry Yued5e9f42022-01-26 11:21:34 +08004939 MBEDTLS_ECP_DP_SECP256R1_ENABLED */
Jerry Yu909df7b2022-01-22 11:56:27 +08004940
Andrzej Kurek25f27152022-08-17 16:09:31 -04004941#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 +08004942 defined(MBEDTLS_ECP_DP_SECP384R1_ENABLED)
4943 MBEDTLS_TLS1_3_SIG_ECDSA_SECP384R1_SHA384,
Andrzej Kurekcccb0442022-08-19 03:42:11 -04004944#endif /* MBEDTLS_ECDSA_C && MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA&&
Jerry Yu909df7b2022-01-22 11:56:27 +08004945 MBEDTLS_ECP_DP_SECP384R1_ENABLED */
4946
Andrzej Kurek25f27152022-08-17 16:09:31 -04004947#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 +08004948 defined(MBEDTLS_ECP_DP_SECP521R1_ENABLED)
4949 MBEDTLS_TLS1_3_SIG_ECDSA_SECP521R1_SHA512,
Andrzej Kurekcccb0442022-08-19 03:42:11 -04004950#endif /* MBEDTLS_ECDSA_C && MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA&&
Jerry Yued5e9f42022-01-26 11:21:34 +08004951 MBEDTLS_ECP_DP_SECP521R1_ENABLED */
Jerry Yu909df7b2022-01-22 11:56:27 +08004952
Gilles Peskine449bd832023-01-11 14:50:10 +01004953#if defined(MBEDTLS_X509_RSASSA_PSS_SUPPORT) && \
4954 defined(MBEDTLS_HAS_ALG_SHA_512_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Jerry Yu9bb3ee42022-06-23 10:16:33 +08004955 MBEDTLS_TLS1_3_SIG_RSA_PSS_RSAE_SHA512,
Gilles Peskine449bd832023-01-11 14:50:10 +01004956#endif \
4957 /* 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 +08004958
Gilles Peskine449bd832023-01-11 14:50:10 +01004959#if defined(MBEDTLS_X509_RSASSA_PSS_SUPPORT) && \
4960 defined(MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Jerry Yu9bb3ee42022-06-23 10:16:33 +08004961 MBEDTLS_TLS1_3_SIG_RSA_PSS_RSAE_SHA384,
Gilles Peskine449bd832023-01-11 14:50:10 +01004962#endif \
4963 /* 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 +08004964
Gilles Peskine449bd832023-01-11 14:50:10 +01004965#if defined(MBEDTLS_X509_RSASSA_PSS_SUPPORT) && \
4966 defined(MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Jerry Yu9bb3ee42022-06-23 10:16:33 +08004967 MBEDTLS_TLS1_3_SIG_RSA_PSS_RSAE_SHA256,
Gilles Peskine449bd832023-01-11 14:50:10 +01004968#endif \
4969 /* 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 +08004970
Andrzej Kurek25f27152022-08-17 16:09:31 -04004971#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 +08004972 MBEDTLS_TLS1_3_SIG_RSA_PKCS1_SHA512,
4973#endif /* MBEDTLS_RSA_C && MBEDTLS_SHA512_C */
4974
Andrzej Kurek25f27152022-08-17 16:09:31 -04004975#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 +08004976 MBEDTLS_TLS1_3_SIG_RSA_PKCS1_SHA384,
4977#endif /* MBEDTLS_RSA_C && MBEDTLS_SHA384_C */
4978
Andrzej Kurek25f27152022-08-17 16:09:31 -04004979#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 +08004980 MBEDTLS_TLS1_3_SIG_RSA_PKCS1_SHA256,
4981#endif /* MBEDTLS_RSA_C && MBEDTLS_SHA256_C */
4982
Gabor Mezei15b95a62022-05-09 16:37:58 +02004983 MBEDTLS_TLS_SIG_NONE
Jerry Yu909df7b2022-01-22 11:56:27 +08004984};
4985
4986/* NOTICE: see above */
4987#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
4988static uint16_t ssl_tls12_preset_default_sig_algs[] = {
Andrzej Kurek25f27152022-08-17 16:09:31 -04004989#if defined(MBEDTLS_HAS_ALG_SHA_512_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Gabor Mezeic1051b62022-05-10 13:13:58 +02004990#if defined(MBEDTLS_ECDSA_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01004991 MBEDTLS_SSL_TLS12_SIG_AND_HASH_ALG(MBEDTLS_SSL_SIG_ECDSA, MBEDTLS_SSL_HASH_SHA512),
Jerry Yu713013f2022-01-17 18:16:35 +08004992#endif
Jerry Yu09a99fc2022-07-28 14:22:17 +08004993#if defined(MBEDTLS_X509_RSASSA_PSS_SUPPORT)
4994 MBEDTLS_TLS1_3_SIG_RSA_PSS_RSAE_SHA512,
4995#endif /* MBEDTLS_X509_RSASSA_PSS_SUPPORT */
Gabor Mezeic1051b62022-05-10 13:13:58 +02004996#if defined(MBEDTLS_RSA_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01004997 MBEDTLS_SSL_TLS12_SIG_AND_HASH_ALG(MBEDTLS_SSL_SIG_RSA, MBEDTLS_SSL_HASH_SHA512),
Gabor Mezeic1051b62022-05-10 13:13:58 +02004998#endif
Andrzej Kurekcccb0442022-08-19 03:42:11 -04004999#endif /* MBEDTLS_HAS_ALG_SHA_512_VIA_MD_OR_PSA_BASED_ON_USE_PSA*/
Andrzej Kurek25f27152022-08-17 16:09:31 -04005000#if defined(MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Gabor Mezeic1051b62022-05-10 13:13:58 +02005001#if defined(MBEDTLS_ECDSA_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01005002 MBEDTLS_SSL_TLS12_SIG_AND_HASH_ALG(MBEDTLS_SSL_SIG_ECDSA, MBEDTLS_SSL_HASH_SHA384),
Jerry Yu11f0a9c2022-01-12 18:43:08 +08005003#endif
Jerry Yu09a99fc2022-07-28 14:22:17 +08005004#if defined(MBEDTLS_X509_RSASSA_PSS_SUPPORT)
5005 MBEDTLS_TLS1_3_SIG_RSA_PSS_RSAE_SHA384,
5006#endif /* MBEDTLS_X509_RSASSA_PSS_SUPPORT */
Gabor Mezeic1051b62022-05-10 13:13:58 +02005007#if defined(MBEDTLS_RSA_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01005008 MBEDTLS_SSL_TLS12_SIG_AND_HASH_ALG(MBEDTLS_SSL_SIG_RSA, MBEDTLS_SSL_HASH_SHA384),
Gabor Mezeic1051b62022-05-10 13:13:58 +02005009#endif
Andrzej Kurekcccb0442022-08-19 03:42:11 -04005010#endif /* MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA*/
Andrzej Kurek25f27152022-08-17 16:09:31 -04005011#if defined(MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Gabor Mezeic1051b62022-05-10 13:13:58 +02005012#if defined(MBEDTLS_ECDSA_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01005013 MBEDTLS_SSL_TLS12_SIG_AND_HASH_ALG(MBEDTLS_SSL_SIG_ECDSA, MBEDTLS_SSL_HASH_SHA256),
Jerry Yu11f0a9c2022-01-12 18:43:08 +08005014#endif
Jerry Yu09a99fc2022-07-28 14:22:17 +08005015#if defined(MBEDTLS_X509_RSASSA_PSS_SUPPORT)
5016 MBEDTLS_TLS1_3_SIG_RSA_PSS_RSAE_SHA256,
5017#endif /* MBEDTLS_X509_RSASSA_PSS_SUPPORT */
Gabor Mezeic1051b62022-05-10 13:13:58 +02005018#if defined(MBEDTLS_RSA_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01005019 MBEDTLS_SSL_TLS12_SIG_AND_HASH_ALG(MBEDTLS_SSL_SIG_RSA, MBEDTLS_SSL_HASH_SHA256),
Gabor Mezeic1051b62022-05-10 13:13:58 +02005020#endif
Andrzej Kurekcccb0442022-08-19 03:42:11 -04005021#endif /* MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA*/
Gabor Mezei15b95a62022-05-09 16:37:58 +02005022 MBEDTLS_TLS_SIG_NONE
Jerry Yu909df7b2022-01-22 11:56:27 +08005023};
5024#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
5025/* NOTICE: see above */
5026static uint16_t ssl_preset_suiteb_sig_algs[] = {
5027
Andrzej Kurek25f27152022-08-17 16:09:31 -04005028#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 +08005029 defined(MBEDTLS_ECP_DP_SECP256R1_ENABLED)
5030 MBEDTLS_TLS1_3_SIG_ECDSA_SECP256R1_SHA256,
Andrzej Kurekcccb0442022-08-19 03:42:11 -04005031#endif /* MBEDTLS_ECDSA_C && MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA&&
Jerry Yu909df7b2022-01-22 11:56:27 +08005032 MBEDTLS_ECP_DP_SECP256R1_ENABLED */
5033
Andrzej Kurek25f27152022-08-17 16:09:31 -04005034#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 +08005035 defined(MBEDTLS_ECP_DP_SECP384R1_ENABLED)
5036 MBEDTLS_TLS1_3_SIG_ECDSA_SECP384R1_SHA384,
Andrzej Kurekcccb0442022-08-19 03:42:11 -04005037#endif /* MBEDTLS_ECDSA_C && MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA&&
Jerry Yu53037892022-01-25 11:02:06 +08005038 MBEDTLS_ECP_DP_SECP384R1_ENABLED */
Jerry Yu909df7b2022-01-22 11:56:27 +08005039
Gilles Peskine449bd832023-01-11 14:50:10 +01005040#if defined(MBEDTLS_X509_RSASSA_PSS_SUPPORT) && \
5041 defined(MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Jerry Yu909df7b2022-01-22 11:56:27 +08005042 MBEDTLS_TLS1_3_SIG_RSA_PSS_RSAE_SHA256,
Gilles Peskine449bd832023-01-11 14:50:10 +01005043#endif \
5044 /* 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 +08005045
Andrzej Kurek25f27152022-08-17 16:09:31 -04005046#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 +08005047 MBEDTLS_TLS1_3_SIG_RSA_PKCS1_SHA256,
Andrzej Kurekcccb0442022-08-19 03:42:11 -04005048#endif /* MBEDTLS_RSA_C && MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA*/
Jerry Yu53037892022-01-25 11:02:06 +08005049
Gabor Mezei15b95a62022-05-09 16:37:58 +02005050 MBEDTLS_TLS_SIG_NONE
Jerry Yu713013f2022-01-17 18:16:35 +08005051};
Jerry Yu6106fdc2022-01-12 16:36:14 +08005052
Jerry Yu909df7b2022-01-22 11:56:27 +08005053/* NOTICE: see above */
5054#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
5055static uint16_t ssl_tls12_preset_suiteb_sig_algs[] = {
Andrzej Kurek25f27152022-08-17 16:09:31 -04005056#if defined(MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Gabor Mezeic1051b62022-05-10 13:13:58 +02005057#if defined(MBEDTLS_ECDSA_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01005058 MBEDTLS_SSL_TLS12_SIG_AND_HASH_ALG(MBEDTLS_SSL_SIG_ECDSA, MBEDTLS_SSL_HASH_SHA256),
Jerry Yu713013f2022-01-17 18:16:35 +08005059#endif
Gabor Mezeic1051b62022-05-10 13:13:58 +02005060#if defined(MBEDTLS_RSA_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01005061 MBEDTLS_SSL_TLS12_SIG_AND_HASH_ALG(MBEDTLS_SSL_SIG_RSA, MBEDTLS_SSL_HASH_SHA256),
Gabor Mezeic1051b62022-05-10 13:13:58 +02005062#endif
Andrzej Kurekcccb0442022-08-19 03:42:11 -04005063#endif /* MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA*/
Andrzej Kurek25f27152022-08-17 16:09:31 -04005064#if defined(MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Gabor Mezeic1051b62022-05-10 13:13:58 +02005065#if defined(MBEDTLS_ECDSA_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01005066 MBEDTLS_SSL_TLS12_SIG_AND_HASH_ALG(MBEDTLS_SSL_SIG_ECDSA, MBEDTLS_SSL_HASH_SHA384),
Jerry Yu18c833e2022-01-25 10:55:47 +08005067#endif
Gabor Mezeic1051b62022-05-10 13:13:58 +02005068#if defined(MBEDTLS_RSA_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01005069 MBEDTLS_SSL_TLS12_SIG_AND_HASH_ALG(MBEDTLS_SSL_SIG_RSA, MBEDTLS_SSL_HASH_SHA384),
Gabor Mezeic1051b62022-05-10 13:13:58 +02005070#endif
Andrzej Kurekcccb0442022-08-19 03:42:11 -04005071#endif /* MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA*/
Gabor Mezei15b95a62022-05-09 16:37:58 +02005072 MBEDTLS_TLS_SIG_NONE
Hanno Becker9c6aa7b2021-08-10 13:50:43 +01005073};
Jerry Yu909df7b2022-01-22 11:56:27 +08005074#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
5075
Ronald Crone68ab4f2022-10-05 12:46:29 +02005076#endif /* MBEDTLS_SSL_HANDSHAKE_WITH_CERT_ENABLED */
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02005077
Brett Warrene0edc842021-08-17 09:53:13 +01005078static uint16_t ssl_preset_suiteb_groups[] = {
Jaeden Amerod4311042019-06-03 08:27:16 +01005079#if defined(MBEDTLS_ECP_DP_SECP256R1_ENABLED)
Brett Warrene0edc842021-08-17 09:53:13 +01005080 MBEDTLS_SSL_IANA_TLS_GROUP_SECP256R1,
Jaeden Amerod4311042019-06-03 08:27:16 +01005081#endif
5082#if defined(MBEDTLS_ECP_DP_SECP384R1_ENABLED)
Brett Warrene0edc842021-08-17 09:53:13 +01005083 MBEDTLS_SSL_IANA_TLS_GROUP_SECP384R1,
Jaeden Amerod4311042019-06-03 08:27:16 +01005084#endif
Brett Warrene0edc842021-08-17 09:53:13 +01005085 MBEDTLS_SSL_IANA_TLS_GROUP_NONE
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02005086};
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02005087
Ronald Crone68ab4f2022-10-05 12:46:29 +02005088#if defined(MBEDTLS_DEBUG_C) && defined(MBEDTLS_SSL_HANDSHAKE_WITH_CERT_ENABLED)
Jerry Yu909df7b2022-01-22 11:56:27 +08005089/* Function for checking `ssl_preset_*_sig_algs` and `ssl_tls12_preset_*_sig_algs`
Jerry Yu370e1462022-01-25 10:36:53 +08005090 * to make sure there are no duplicated signature algorithm entries. */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02005091MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01005092static int ssl_check_no_sig_alg_duplication(uint16_t *sig_algs)
Jerry Yu1a8b4812022-01-20 17:56:50 +08005093{
5094 size_t i, j;
5095 int ret = 0;
Jerry Yu909df7b2022-01-22 11:56:27 +08005096
Gilles Peskine449bd832023-01-11 14:50:10 +01005097 for (i = 0; sig_algs[i] != MBEDTLS_TLS_SIG_NONE; i++) {
5098 for (j = 0; j < i; j++) {
5099 if (sig_algs[i] != sig_algs[j]) {
Jerry Yuf377d642022-01-25 10:43:59 +08005100 continue;
Gilles Peskine449bd832023-01-11 14:50:10 +01005101 }
5102 mbedtls_printf(" entry(%04x,%" MBEDTLS_PRINTF_SIZET
5103 ") is duplicated at %" MBEDTLS_PRINTF_SIZET "\n",
5104 sig_algs[i], j, i);
Jerry Yuf377d642022-01-25 10:43:59 +08005105 ret = -1;
Jerry Yu1a8b4812022-01-20 17:56:50 +08005106 }
5107 }
Gilles Peskine449bd832023-01-11 14:50:10 +01005108 return ret;
Jerry Yu1a8b4812022-01-20 17:56:50 +08005109}
5110
Ronald Crone68ab4f2022-10-05 12:46:29 +02005111#endif /* MBEDTLS_DEBUG_C && MBEDTLS_SSL_HANDSHAKE_WITH_CERT_ENABLED */
Jerry Yu1a8b4812022-01-20 17:56:50 +08005112
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02005113/*
Tillmann Karras588ad502015-09-25 04:27:22 +02005114 * Load default in mbedtls_ssl_config
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02005115 */
Gilles Peskine449bd832023-01-11 14:50:10 +01005116int mbedtls_ssl_config_defaults(mbedtls_ssl_config *conf,
5117 int endpoint, int transport, int preset)
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02005118{
Manuel Pégourié-Gonnard8b431fb2015-05-11 12:54:52 +02005119#if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_SSL_SRV_C)
Janos Follath865b3eb2019-12-16 11:46:15 +00005120 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnard8b431fb2015-05-11 12:54:52 +02005121#endif
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02005122
Ronald Crone68ab4f2022-10-05 12:46:29 +02005123#if defined(MBEDTLS_DEBUG_C) && defined(MBEDTLS_SSL_HANDSHAKE_WITH_CERT_ENABLED)
Gilles Peskine449bd832023-01-11 14:50:10 +01005124 if (ssl_check_no_sig_alg_duplication(ssl_preset_suiteb_sig_algs)) {
5125 mbedtls_printf("ssl_preset_suiteb_sig_algs has duplicated entries\n");
5126 return MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Jerry Yu1a8b4812022-01-20 17:56:50 +08005127 }
5128
Gilles Peskine449bd832023-01-11 14:50:10 +01005129 if (ssl_check_no_sig_alg_duplication(ssl_preset_default_sig_algs)) {
5130 mbedtls_printf("ssl_preset_default_sig_algs has duplicated entries\n");
5131 return MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Jerry Yu1a8b4812022-01-20 17:56:50 +08005132 }
Jerry Yu909df7b2022-01-22 11:56:27 +08005133
5134#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
Gilles Peskine449bd832023-01-11 14:50:10 +01005135 if (ssl_check_no_sig_alg_duplication(ssl_tls12_preset_suiteb_sig_algs)) {
5136 mbedtls_printf("ssl_tls12_preset_suiteb_sig_algs has duplicated entries\n");
5137 return MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Jerry Yu909df7b2022-01-22 11:56:27 +08005138 }
5139
Gilles Peskine449bd832023-01-11 14:50:10 +01005140 if (ssl_check_no_sig_alg_duplication(ssl_tls12_preset_default_sig_algs)) {
5141 mbedtls_printf("ssl_tls12_preset_default_sig_algs has duplicated entries\n");
5142 return MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Jerry Yu909df7b2022-01-22 11:56:27 +08005143 }
5144#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Ronald Crone68ab4f2022-10-05 12:46:29 +02005145#endif /* MBEDTLS_DEBUG_C && MBEDTLS_SSL_HANDSHAKE_WITH_CERT_ENABLED */
Jerry Yu1a8b4812022-01-20 17:56:50 +08005146
Manuel Pégourié-Gonnard0de074f2015-05-14 12:58:01 +02005147 /* Use the functions here so that they are covered in tests,
5148 * but otherwise access member directly for efficiency */
Gilles Peskine449bd832023-01-11 14:50:10 +01005149 mbedtls_ssl_conf_endpoint(conf, endpoint);
5150 mbedtls_ssl_conf_transport(conf, transport);
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02005151
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02005152 /*
5153 * Things that are common to all presets
5154 */
Manuel Pégourié-Gonnard419d5ae2015-05-04 19:32:36 +02005155#if defined(MBEDTLS_SSL_CLI_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01005156 if (endpoint == MBEDTLS_SSL_IS_CLIENT) {
Manuel Pégourié-Gonnard419d5ae2015-05-04 19:32:36 +02005157 conf->authmode = MBEDTLS_SSL_VERIFY_REQUIRED;
5158#if defined(MBEDTLS_SSL_SESSION_TICKETS)
5159 conf->session_tickets = MBEDTLS_SSL_SESSION_TICKETS_ENABLED;
5160#endif
5161 }
5162#endif
5163
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02005164#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
5165 conf->encrypt_then_mac = MBEDTLS_SSL_ETM_ENABLED;
5166#endif
5167
5168#if defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET)
5169 conf->extended_ms = MBEDTLS_SSL_EXTENDED_MS_ENABLED;
5170#endif
5171
Manuel Pégourié-Gonnarde057d3b2015-05-20 10:59:43 +02005172#if defined(MBEDTLS_SSL_DTLS_HELLO_VERIFY) && defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02005173 conf->f_cookie_write = ssl_cookie_write_dummy;
5174 conf->f_cookie_check = ssl_cookie_check_dummy;
5175#endif
5176
5177#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
5178 conf->anti_replay = MBEDTLS_SSL_ANTI_REPLAY_ENABLED;
5179#endif
5180
Janos Follath088ce432017-04-10 12:42:31 +01005181#if defined(MBEDTLS_SSL_SRV_C)
5182 conf->cert_req_ca_list = MBEDTLS_SSL_CERT_REQ_CA_LIST_ENABLED;
TRodziewicz3946f792021-06-14 12:11:18 +02005183 conf->respect_cli_pref = MBEDTLS_SSL_SRV_CIPHERSUITE_ORDER_SERVER;
Janos Follath088ce432017-04-10 12:42:31 +01005184#endif
5185
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02005186#if defined(MBEDTLS_SSL_PROTO_DTLS)
5187 conf->hs_timeout_min = MBEDTLS_SSL_DTLS_TIMEOUT_DFL_MIN;
5188 conf->hs_timeout_max = MBEDTLS_SSL_DTLS_TIMEOUT_DFL_MAX;
5189#endif
5190
5191#if defined(MBEDTLS_SSL_RENEGOTIATION)
5192 conf->renego_max_records = MBEDTLS_SSL_RENEGO_MAX_RECORDS_DEFAULT;
Gilles Peskine449bd832023-01-11 14:50:10 +01005193 memset(conf->renego_period, 0x00, 2);
5194 memset(conf->renego_period + 2, 0xFF, 6);
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02005195#endif
5196
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02005197#if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_SSL_SRV_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01005198 if (endpoint == MBEDTLS_SSL_IS_SERVER) {
Hanno Beckere2defad2021-07-24 05:59:17 +01005199 const unsigned char dhm_p[] =
5200 MBEDTLS_DHM_RFC3526_MODP_2048_P_BIN;
5201 const unsigned char dhm_g[] =
5202 MBEDTLS_DHM_RFC3526_MODP_2048_G_BIN;
Hanno Becker00d0a682017-10-04 13:14:29 +01005203
Gilles Peskine449bd832023-01-11 14:50:10 +01005204 if ((ret = mbedtls_ssl_conf_dh_param_bin(conf,
5205 dhm_p, sizeof(dhm_p),
5206 dhm_g, sizeof(dhm_g))) != 0) {
5207 return ret;
Hanno Beckere2defad2021-07-24 05:59:17 +01005208 }
5209 }
Manuel Pégourié-Gonnardbd990d62015-06-11 14:49:42 +02005210#endif
5211
Ronald Cron6f135e12021-12-08 16:57:54 +01005212#if defined(MBEDTLS_SSL_PROTO_TLS1_3)
Jerry Yu6ee56aa2022-12-06 17:47:22 +08005213
5214#if defined(MBEDTLS_SSL_EARLY_DATA)
Gilles Peskine449bd832023-01-11 14:50:10 +01005215 mbedtls_ssl_tls13_conf_early_data(conf, MBEDTLS_SSL_EARLY_DATA_DISABLED);
Jerry Yu6ee56aa2022-12-06 17:47:22 +08005216#if defined(MBEDTLS_SSL_SRV_C)
5217 mbedtls_ssl_tls13_conf_max_early_data_size(
Gilles Peskine449bd832023-01-11 14:50:10 +01005218 conf, MBEDTLS_SSL_MAX_EARLY_DATA_SIZE);
Jerry Yu6ee56aa2022-12-06 17:47:22 +08005219#endif
5220#endif /* MBEDTLS_SSL_EARLY_DATA */
5221
Jerry Yud0766ec2022-09-22 10:46:57 +08005222#if defined(MBEDTLS_SSL_SRV_C) && defined(MBEDTLS_SSL_SESSION_TICKETS)
Jerry Yu1ad7ace2022-08-09 13:28:39 +08005223 mbedtls_ssl_conf_new_session_tickets(
Gilles Peskine449bd832023-01-11 14:50:10 +01005224 conf, MBEDTLS_SSL_TLS1_3_DEFAULT_NEW_SESSION_TICKETS);
Jerry Yu1ad7ace2022-08-09 13:28:39 +08005225#endif
Hanno Becker71f1ed62021-07-24 06:01:47 +01005226 /*
5227 * Allow all TLS 1.3 key exchange modes by default.
5228 */
Xiaofei Bai746f9482021-11-12 08:53:56 +00005229 conf->tls13_kex_modes = MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_ALL;
Ronald Cron6f135e12021-12-08 16:57:54 +01005230#endif /* MBEDTLS_SSL_PROTO_TLS1_3 */
Hanno Becker71f1ed62021-07-24 06:01:47 +01005231
Gilles Peskine449bd832023-01-11 14:50:10 +01005232 if (transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM) {
Glenn Strauss2dfcea22022-03-14 17:26:42 -04005233#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
XiaokangQian4d3a6042022-04-21 13:46:17 +00005234 conf->min_tls_version = MBEDTLS_SSL_VERSION_TLS1_2;
XiaokangQian060d8672022-04-21 09:24:56 +00005235 conf->max_tls_version = MBEDTLS_SSL_VERSION_TLS1_2;
XiaokangQian4d3a6042022-04-21 13:46:17 +00005236#else
Gilles Peskine449bd832023-01-11 14:50:10 +01005237 return MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
XiaokangQian060d8672022-04-21 09:24:56 +00005238#endif
Gilles Peskine449bd832023-01-11 14:50:10 +01005239 } else {
XiaokangQian4d3a6042022-04-21 13:46:17 +00005240#if defined(MBEDTLS_SSL_PROTO_TLS1_2) && defined(MBEDTLS_SSL_PROTO_TLS1_3)
Gilles Peskine449bd832023-01-11 14:50:10 +01005241 if (endpoint == MBEDTLS_SSL_IS_CLIENT) {
XiaokangQian4d3a6042022-04-21 13:46:17 +00005242 conf->min_tls_version = MBEDTLS_SSL_VERSION_TLS1_2;
5243 conf->max_tls_version = MBEDTLS_SSL_VERSION_TLS1_3;
Gilles Peskine449bd832023-01-11 14:50:10 +01005244 } else {
5245 /* Hybrid TLS 1.2 / 1.3 is not supported on server side yet */
XiaokangQian4d3a6042022-04-21 13:46:17 +00005246 conf->min_tls_version = MBEDTLS_SSL_VERSION_TLS1_2;
5247 conf->max_tls_version = MBEDTLS_SSL_VERSION_TLS1_2;
5248 }
5249#elif defined(MBEDTLS_SSL_PROTO_TLS1_3)
5250 conf->min_tls_version = MBEDTLS_SSL_VERSION_TLS1_3;
5251 conf->max_tls_version = MBEDTLS_SSL_VERSION_TLS1_3;
5252#elif defined(MBEDTLS_SSL_PROTO_TLS1_2)
5253 conf->min_tls_version = MBEDTLS_SSL_VERSION_TLS1_2;
5254 conf->max_tls_version = MBEDTLS_SSL_VERSION_TLS1_2;
5255#else
Gilles Peskine449bd832023-01-11 14:50:10 +01005256 return MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
XiaokangQian4d3a6042022-04-21 13:46:17 +00005257#endif
5258 }
Glenn Strauss2dfcea22022-03-14 17:26:42 -04005259
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02005260 /*
5261 * Preset-specific defaults
5262 */
Gilles Peskine449bd832023-01-11 14:50:10 +01005263 switch (preset) {
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02005264 /*
5265 * NSA Suite B
5266 */
5267 case MBEDTLS_SSL_PRESET_SUITEB:
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02005268
Hanno Beckerd60b6c62021-04-29 12:04:11 +01005269 conf->ciphersuite_list = ssl_preset_suiteb_ciphersuites;
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02005270
5271#if defined(MBEDTLS_X509_CRT_PARSE_C)
5272 conf->cert_profile = &mbedtls_x509_crt_profile_suiteb;
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02005273#endif
5274
Ronald Crone68ab4f2022-10-05 12:46:29 +02005275#if defined(MBEDTLS_SSL_HANDSHAKE_WITH_CERT_ENABLED)
Jerry Yu909df7b2022-01-22 11:56:27 +08005276#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
Gilles Peskine449bd832023-01-11 14:50:10 +01005277 if (mbedtls_ssl_conf_is_tls12_only(conf)) {
Jerry Yu909df7b2022-01-22 11:56:27 +08005278 conf->sig_algs = ssl_tls12_preset_suiteb_sig_algs;
Gilles Peskine449bd832023-01-11 14:50:10 +01005279 } else
Jerry Yu909df7b2022-01-22 11:56:27 +08005280#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Gilles Peskine449bd832023-01-11 14:50:10 +01005281 conf->sig_algs = ssl_preset_suiteb_sig_algs;
Ronald Crone68ab4f2022-10-05 12:46:29 +02005282#endif /* MBEDTLS_SSL_HANDSHAKE_WITH_CERT_ENABLED */
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02005283
Brett Warrene0edc842021-08-17 09:53:13 +01005284#if defined(MBEDTLS_ECP_C) && !defined(MBEDTLS_DEPRECATED_REMOVED)
5285 conf->curve_list = NULL;
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02005286#endif
Brett Warrene0edc842021-08-17 09:53:13 +01005287 conf->group_list = ssl_preset_suiteb_groups;
Manuel Pégourié-Gonnardc98204e2015-08-11 04:21:01 +02005288 break;
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02005289
5290 /*
5291 * Default
5292 */
5293 default:
Ronald Cronf6606552022-03-15 11:23:25 +01005294
Hanno Beckerd60b6c62021-04-29 12:04:11 +01005295 conf->ciphersuite_list = mbedtls_ssl_list_ciphersuites();
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02005296
5297#if defined(MBEDTLS_X509_CRT_PARSE_C)
5298 conf->cert_profile = &mbedtls_x509_crt_profile_default;
5299#endif
5300
Ronald Crone68ab4f2022-10-05 12:46:29 +02005301#if defined(MBEDTLS_SSL_HANDSHAKE_WITH_CERT_ENABLED)
Jerry Yu909df7b2022-01-22 11:56:27 +08005302#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
Gilles Peskine449bd832023-01-11 14:50:10 +01005303 if (mbedtls_ssl_conf_is_tls12_only(conf)) {
Jerry Yu909df7b2022-01-22 11:56:27 +08005304 conf->sig_algs = ssl_tls12_preset_default_sig_algs;
Gilles Peskine449bd832023-01-11 14:50:10 +01005305 } else
Jerry Yu909df7b2022-01-22 11:56:27 +08005306#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Gilles Peskine449bd832023-01-11 14:50:10 +01005307 conf->sig_algs = ssl_preset_default_sig_algs;
Ronald Crone68ab4f2022-10-05 12:46:29 +02005308#endif /* MBEDTLS_SSL_HANDSHAKE_WITH_CERT_ENABLED */
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02005309
Brett Warrene0edc842021-08-17 09:53:13 +01005310#if defined(MBEDTLS_ECP_C) && !defined(MBEDTLS_DEPRECATED_REMOVED)
5311 conf->curve_list = NULL;
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02005312#endif
Brett Warrene0edc842021-08-17 09:53:13 +01005313 conf->group_list = ssl_preset_default_groups;
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02005314
5315#if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_SSL_CLI_C)
5316 conf->dhm_min_bitlen = 1024;
5317#endif
5318 }
5319
Gilles Peskine449bd832023-01-11 14:50:10 +01005320 return 0;
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02005321}
5322
5323/*
5324 * Free mbedtls_ssl_config
5325 */
Gilles Peskine449bd832023-01-11 14:50:10 +01005326void mbedtls_ssl_config_free(mbedtls_ssl_config *conf)
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02005327{
5328#if defined(MBEDTLS_DHM_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01005329 mbedtls_mpi_free(&conf->dhm_P);
5330 mbedtls_mpi_free(&conf->dhm_G);
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02005331#endif
5332
Ronald Cron73fe8df2022-10-05 14:31:43 +02005333#if defined(MBEDTLS_SSL_HANDSHAKE_WITH_PSK_ENABLED)
Neil Armstrong501c9322022-05-03 09:35:09 +02005334#if defined(MBEDTLS_USE_PSA_CRYPTO)
Gilles Peskine449bd832023-01-11 14:50:10 +01005335 if (!mbedtls_svc_key_id_is_null(conf->psk_opaque)) {
Neil Armstrong501c9322022-05-03 09:35:09 +02005336 conf->psk_opaque = MBEDTLS_SVC_KEY_ID_INIT;
5337 }
Neil Armstrong8ecd6682022-05-05 11:40:35 +02005338#endif /* MBEDTLS_USE_PSA_CRYPTO */
Gilles Peskine449bd832023-01-11 14:50:10 +01005339 if (conf->psk != NULL) {
5340 mbedtls_platform_zeroize(conf->psk, conf->psk_len);
5341 mbedtls_free(conf->psk);
Azim Khan27e8a122018-03-21 14:24:11 +00005342 conf->psk = NULL;
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02005343 conf->psk_len = 0;
junyeonLEE316b1622017-12-20 16:29:30 +09005344 }
5345
Gilles Peskine449bd832023-01-11 14:50:10 +01005346 if (conf->psk_identity != NULL) {
5347 mbedtls_platform_zeroize(conf->psk_identity, conf->psk_identity_len);
5348 mbedtls_free(conf->psk_identity);
Azim Khan27e8a122018-03-21 14:24:11 +00005349 conf->psk_identity = NULL;
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02005350 conf->psk_identity_len = 0;
5351 }
Ronald Cron73fe8df2022-10-05 14:31:43 +02005352#endif /* MBEDTLS_SSL_HANDSHAKE_WITH_PSK_ENABLED */
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02005353
5354#if defined(MBEDTLS_X509_CRT_PARSE_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01005355 ssl_key_cert_free(conf->key_cert);
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02005356#endif
5357
Gilles Peskine449bd832023-01-11 14:50:10 +01005358 mbedtls_platform_zeroize(conf, sizeof(mbedtls_ssl_config));
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02005359}
5360
Manuel Pégourié-Gonnard5674a972015-10-19 15:14:03 +02005361#if defined(MBEDTLS_PK_C) && \
Gilles Peskine449bd832023-01-11 14:50:10 +01005362 (defined(MBEDTLS_RSA_C) || defined(MBEDTLS_ECDSA_C))
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02005363/*
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005364 * Convert between MBEDTLS_PK_XXX and SSL_SIG_XXX
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02005365 */
Gilles Peskine449bd832023-01-11 14:50:10 +01005366unsigned char mbedtls_ssl_sig_from_pk(mbedtls_pk_context *pk)
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02005367{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005368#if defined(MBEDTLS_RSA_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01005369 if (mbedtls_pk_can_do(pk, MBEDTLS_PK_RSA)) {
5370 return MBEDTLS_SSL_SIG_RSA;
5371 }
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02005372#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005373#if defined(MBEDTLS_ECDSA_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01005374 if (mbedtls_pk_can_do(pk, MBEDTLS_PK_ECDSA)) {
5375 return MBEDTLS_SSL_SIG_ECDSA;
5376 }
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02005377#endif
Gilles Peskine449bd832023-01-11 14:50:10 +01005378 return MBEDTLS_SSL_SIG_ANON;
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02005379}
5380
Gilles Peskine449bd832023-01-11 14:50:10 +01005381unsigned char mbedtls_ssl_sig_from_pk_alg(mbedtls_pk_type_t type)
Hanno Becker7e5437a2017-04-28 17:15:26 +01005382{
Gilles Peskine449bd832023-01-11 14:50:10 +01005383 switch (type) {
Hanno Becker7e5437a2017-04-28 17:15:26 +01005384 case MBEDTLS_PK_RSA:
Gilles Peskine449bd832023-01-11 14:50:10 +01005385 return MBEDTLS_SSL_SIG_RSA;
Hanno Becker7e5437a2017-04-28 17:15:26 +01005386 case MBEDTLS_PK_ECDSA:
5387 case MBEDTLS_PK_ECKEY:
Gilles Peskine449bd832023-01-11 14:50:10 +01005388 return MBEDTLS_SSL_SIG_ECDSA;
Hanno Becker7e5437a2017-04-28 17:15:26 +01005389 default:
Gilles Peskine449bd832023-01-11 14:50:10 +01005390 return MBEDTLS_SSL_SIG_ANON;
Hanno Becker7e5437a2017-04-28 17:15:26 +01005391 }
5392}
5393
Gilles Peskine449bd832023-01-11 14:50:10 +01005394mbedtls_pk_type_t mbedtls_ssl_pk_alg_from_sig(unsigned char sig)
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02005395{
Gilles Peskine449bd832023-01-11 14:50:10 +01005396 switch (sig) {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005397#if defined(MBEDTLS_RSA_C)
5398 case MBEDTLS_SSL_SIG_RSA:
Gilles Peskine449bd832023-01-11 14:50:10 +01005399 return MBEDTLS_PK_RSA;
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02005400#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005401#if defined(MBEDTLS_ECDSA_C)
5402 case MBEDTLS_SSL_SIG_ECDSA:
Gilles Peskine449bd832023-01-11 14:50:10 +01005403 return MBEDTLS_PK_ECDSA;
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02005404#endif
5405 default:
Gilles Peskine449bd832023-01-11 14:50:10 +01005406 return MBEDTLS_PK_NONE;
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02005407 }
5408}
Manuel Pégourié-Gonnard5674a972015-10-19 15:14:03 +02005409#endif /* MBEDTLS_PK_C && ( MBEDTLS_RSA_C || MBEDTLS_ECDSA_C ) */
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02005410
Manuel Pégourié-Gonnard1a483832013-09-20 12:29:15 +02005411/*
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +02005412 * Convert from MBEDTLS_SSL_HASH_XXX to MBEDTLS_MD_XXX
Manuel Pégourié-Gonnard1a483832013-09-20 12:29:15 +02005413 */
Gilles Peskine449bd832023-01-11 14:50:10 +01005414mbedtls_md_type_t mbedtls_ssl_md_alg_from_hash(unsigned char hash)
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02005415{
Gilles Peskine449bd832023-01-11 14:50:10 +01005416 switch (hash) {
Andrzej Kurek25f27152022-08-17 16:09:31 -04005417#if defined(MBEDTLS_HAS_ALG_MD5_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005418 case MBEDTLS_SSL_HASH_MD5:
Gilles Peskine449bd832023-01-11 14:50:10 +01005419 return MBEDTLS_MD_MD5;
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02005420#endif
Andrzej Kurek25f27152022-08-17 16:09:31 -04005421#if defined(MBEDTLS_HAS_ALG_SHA_1_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005422 case MBEDTLS_SSL_HASH_SHA1:
Gilles Peskine449bd832023-01-11 14:50:10 +01005423 return MBEDTLS_MD_SHA1;
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02005424#endif
Andrzej Kurek25f27152022-08-17 16:09:31 -04005425#if defined(MBEDTLS_HAS_ALG_SHA_224_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005426 case MBEDTLS_SSL_HASH_SHA224:
Gilles Peskine449bd832023-01-11 14:50:10 +01005427 return MBEDTLS_MD_SHA224;
Mateusz Starzyke3c48b42021-04-19 16:46:28 +02005428#endif
Andrzej Kurek25f27152022-08-17 16:09:31 -04005429#if defined(MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005430 case MBEDTLS_SSL_HASH_SHA256:
Gilles Peskine449bd832023-01-11 14:50:10 +01005431 return MBEDTLS_MD_SHA256;
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02005432#endif
Andrzej Kurek25f27152022-08-17 16:09:31 -04005433#if defined(MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005434 case MBEDTLS_SSL_HASH_SHA384:
Gilles Peskine449bd832023-01-11 14:50:10 +01005435 return MBEDTLS_MD_SHA384;
Mateusz Starzyk3352a532021-04-06 14:28:22 +02005436#endif
Andrzej Kurek25f27152022-08-17 16:09:31 -04005437#if defined(MBEDTLS_HAS_ALG_SHA_512_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005438 case MBEDTLS_SSL_HASH_SHA512:
Gilles Peskine449bd832023-01-11 14:50:10 +01005439 return MBEDTLS_MD_SHA512;
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02005440#endif
5441 default:
Gilles Peskine449bd832023-01-11 14:50:10 +01005442 return MBEDTLS_MD_NONE;
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02005443 }
5444}
5445
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +02005446/*
5447 * Convert from MBEDTLS_MD_XXX to MBEDTLS_SSL_HASH_XXX
5448 */
Gilles Peskine449bd832023-01-11 14:50:10 +01005449unsigned char mbedtls_ssl_hash_from_md_alg(int md)
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +02005450{
Gilles Peskine449bd832023-01-11 14:50:10 +01005451 switch (md) {
Andrzej Kurek25f27152022-08-17 16:09:31 -04005452#if defined(MBEDTLS_HAS_ALG_MD5_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +02005453 case MBEDTLS_MD_MD5:
Gilles Peskine449bd832023-01-11 14:50:10 +01005454 return MBEDTLS_SSL_HASH_MD5;
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +02005455#endif
Andrzej Kurek25f27152022-08-17 16:09:31 -04005456#if defined(MBEDTLS_HAS_ALG_SHA_1_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +02005457 case MBEDTLS_MD_SHA1:
Gilles Peskine449bd832023-01-11 14:50:10 +01005458 return MBEDTLS_SSL_HASH_SHA1;
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +02005459#endif
Andrzej Kurek25f27152022-08-17 16:09:31 -04005460#if defined(MBEDTLS_HAS_ALG_SHA_224_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +02005461 case MBEDTLS_MD_SHA224:
Gilles Peskine449bd832023-01-11 14:50:10 +01005462 return MBEDTLS_SSL_HASH_SHA224;
Mateusz Starzyke3c48b42021-04-19 16:46:28 +02005463#endif
Andrzej Kurek25f27152022-08-17 16:09:31 -04005464#if defined(MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +02005465 case MBEDTLS_MD_SHA256:
Gilles Peskine449bd832023-01-11 14:50:10 +01005466 return MBEDTLS_SSL_HASH_SHA256;
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +02005467#endif
Andrzej Kurek25f27152022-08-17 16:09:31 -04005468#if defined(MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +02005469 case MBEDTLS_MD_SHA384:
Gilles Peskine449bd832023-01-11 14:50:10 +01005470 return MBEDTLS_SSL_HASH_SHA384;
Mateusz Starzyk3352a532021-04-06 14:28:22 +02005471#endif
Andrzej Kurek25f27152022-08-17 16:09:31 -04005472#if defined(MBEDTLS_HAS_ALG_SHA_512_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +02005473 case MBEDTLS_MD_SHA512:
Gilles Peskine449bd832023-01-11 14:50:10 +01005474 return MBEDTLS_SSL_HASH_SHA512;
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +02005475#endif
5476 default:
Gilles Peskine449bd832023-01-11 14:50:10 +01005477 return MBEDTLS_SSL_HASH_NONE;
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +02005478 }
5479}
5480
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01005481/*
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +02005482 * Check if a curve proposed by the peer is in our list.
Manuel Pégourié-Gonnard9d412d82015-06-17 12:10:46 +02005483 * Return 0 if we're willing to use it, -1 otherwise.
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01005484 */
Gilles Peskine449bd832023-01-11 14:50:10 +01005485int mbedtls_ssl_check_curve_tls_id(const mbedtls_ssl_context *ssl, uint16_t tls_id)
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01005486{
Gilles Peskine449bd832023-01-11 14:50:10 +01005487 const uint16_t *group_list = mbedtls_ssl_get_groups(ssl);
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01005488
Gilles Peskine449bd832023-01-11 14:50:10 +01005489 if (group_list == NULL) {
5490 return -1;
Brett Warrene0edc842021-08-17 09:53:13 +01005491 }
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01005492
Gilles Peskine449bd832023-01-11 14:50:10 +01005493 for (; *group_list != 0; group_list++) {
5494 if (*group_list == tls_id) {
5495 return 0;
5496 }
5497 }
5498
5499 return -1;
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01005500}
Manuel Pégourié-Gonnard0d63b842022-01-18 13:10:56 +01005501
5502#if defined(MBEDTLS_ECP_C)
5503/*
5504 * Same as mbedtls_ssl_check_curve_tls_id() but with a mbedtls_ecp_group_id.
5505 */
Gilles Peskine449bd832023-01-11 14:50:10 +01005506int mbedtls_ssl_check_curve(const mbedtls_ssl_context *ssl, mbedtls_ecp_group_id grp_id)
Manuel Pégourié-Gonnard0d63b842022-01-18 13:10:56 +01005507{
Gilles Peskine449bd832023-01-11 14:50:10 +01005508 uint16_t tls_id = mbedtls_ssl_get_tls_id_from_ecp_group_id(grp_id);
Leonid Rozenboim19e59732022-08-08 16:52:38 -07005509
Gilles Peskine449bd832023-01-11 14:50:10 +01005510 if (tls_id == 0) {
Leonid Rozenboim19e59732022-08-08 16:52:38 -07005511 return -1;
Gilles Peskine449bd832023-01-11 14:50:10 +01005512 }
Leonid Rozenboim19e59732022-08-08 16:52:38 -07005513
Gilles Peskine449bd832023-01-11 14:50:10 +01005514 return mbedtls_ssl_check_curve_tls_id(ssl, tls_id);
Manuel Pégourié-Gonnard0d63b842022-01-18 13:10:56 +01005515}
Manuel Pégourié-Gonnardb541da62015-06-17 11:43:30 +02005516#endif /* MBEDTLS_ECP_C */
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02005517
Gilles Peskine449bd832023-01-11 14:50:10 +01005518#if defined(MBEDTLS_DEBUG_C)
Valerio Setti67419f02023-01-04 16:12:42 +01005519#define EC_NAME(_name_) _name_
5520#else
5521#define EC_NAME(_name_) NULL
5522#endif
5523
Valerio Setti18c9fed2022-12-30 17:44:24 +01005524static const struct {
5525 uint16_t tls_id;
5526 mbedtls_ecp_group_id ecp_group_id;
5527 psa_ecc_family_t psa_family;
5528 uint16_t bits;
Gilles Peskine449bd832023-01-11 14:50:10 +01005529 const char *name;
Valerio Setti18c9fed2022-12-30 17:44:24 +01005530} tls_id_match_table[] =
5531{
5532#if defined(MBEDTLS_ECP_DP_SECP521R1_ENABLED) || defined(PSA_WANT_ECC_SECP_R1_521)
Gilles Peskine449bd832023-01-11 14:50:10 +01005533 { 25, MBEDTLS_ECP_DP_SECP521R1, PSA_ECC_FAMILY_SECP_R1, 521, EC_NAME("secp521r1") },
Valerio Setti18c9fed2022-12-30 17:44:24 +01005534#endif
5535#if defined(MBEDTLS_ECP_DP_BP512R1_ENABLED) || defined(PSA_WANT_ECC_BRAINPOOL_P_R1_512)
Gilles Peskine449bd832023-01-11 14:50:10 +01005536 { 28, MBEDTLS_ECP_DP_BP512R1, PSA_ECC_FAMILY_BRAINPOOL_P_R1, 512, EC_NAME("brainpoolP512r1") },
Valerio Setti18c9fed2022-12-30 17:44:24 +01005537#endif
Valerio Setti67419f02023-01-04 16:12:42 +01005538#if defined(MBEDTLS_ECP_DP_SECP384R1_ENABLED) || defined(PSA_WANT_ECC_SECP_R1_384)
Gilles Peskine449bd832023-01-11 14:50:10 +01005539 { 24, MBEDTLS_ECP_DP_SECP384R1, PSA_ECC_FAMILY_SECP_R1, 384, EC_NAME("secp384r1") },
Valerio Setti18c9fed2022-12-30 17:44:24 +01005540#endif
Valerio Setti67419f02023-01-04 16:12:42 +01005541#if defined(MBEDTLS_ECP_DP_BP384R1_ENABLED) || defined(PSA_WANT_ECC_BRAINPOOL_P_R1_384)
Gilles Peskine449bd832023-01-11 14:50:10 +01005542 { 27, MBEDTLS_ECP_DP_BP384R1, PSA_ECC_FAMILY_BRAINPOOL_P_R1, 384, EC_NAME("brainpoolP384r1") },
Valerio Setti18c9fed2022-12-30 17:44:24 +01005543#endif
Valerio Setti67419f02023-01-04 16:12:42 +01005544#if defined(MBEDTLS_ECP_DP_SECP256R1_ENABLED) || defined(PSA_WANT_ECC_SECP_R1_256)
Gilles Peskine449bd832023-01-11 14:50:10 +01005545 { 23, MBEDTLS_ECP_DP_SECP256R1, PSA_ECC_FAMILY_SECP_R1, 256, EC_NAME("secp256r1") },
Valerio Setti18c9fed2022-12-30 17:44:24 +01005546#endif
5547#if defined(MBEDTLS_ECP_DP_SECP256K1_ENABLED) || defined(PSA_WANT_ECC_SECP_K1_256)
Gilles Peskine449bd832023-01-11 14:50:10 +01005548 { 22, MBEDTLS_ECP_DP_SECP256K1, PSA_ECC_FAMILY_SECP_K1, 256, EC_NAME("secp256k1") },
Valerio Setti18c9fed2022-12-30 17:44:24 +01005549#endif
Valerio Setti67419f02023-01-04 16:12:42 +01005550#if defined(MBEDTLS_ECP_DP_BP256R1_ENABLED) || defined(PSA_WANT_ECC_BRAINPOOL_P_R1_256)
Gilles Peskine449bd832023-01-11 14:50:10 +01005551 { 26, MBEDTLS_ECP_DP_BP256R1, PSA_ECC_FAMILY_BRAINPOOL_P_R1, 256, EC_NAME("brainpoolP256r1") },
Valerio Setti18c9fed2022-12-30 17:44:24 +01005552#endif
5553#if defined(MBEDTLS_ECP_DP_SECP224R1_ENABLED) || defined(PSA_WANT_ECC_SECP_R1_224)
Gilles Peskine449bd832023-01-11 14:50:10 +01005554 { 21, MBEDTLS_ECP_DP_SECP224R1, PSA_ECC_FAMILY_SECP_R1, 224, EC_NAME("secp224r1") },
Valerio Setti18c9fed2022-12-30 17:44:24 +01005555#endif
5556#if defined(MBEDTLS_ECP_DP_SECP224K1_ENABLED) || defined(PSA_WANT_ECC_SECP_K1_224)
Gilles Peskine449bd832023-01-11 14:50:10 +01005557 { 20, MBEDTLS_ECP_DP_SECP224K1, PSA_ECC_FAMILY_SECP_K1, 224, EC_NAME("secp224k1") },
Valerio Setti18c9fed2022-12-30 17:44:24 +01005558#endif
5559#if defined(MBEDTLS_ECP_DP_SECP192R1_ENABLED) || defined(PSA_WANT_ECC_SECP_R1_192)
Gilles Peskine449bd832023-01-11 14:50:10 +01005560 { 19, MBEDTLS_ECP_DP_SECP192R1, PSA_ECC_FAMILY_SECP_R1, 192, EC_NAME("secp192r1") },
Valerio Setti18c9fed2022-12-30 17:44:24 +01005561#endif
5562#if defined(MBEDTLS_ECP_DP_SECP192K1_ENABLED) || defined(PSA_WANT_ECC_SECP_K1_192)
Gilles Peskine449bd832023-01-11 14:50:10 +01005563 { 18, MBEDTLS_ECP_DP_SECP192K1, PSA_ECC_FAMILY_SECP_K1, 192, EC_NAME("secp192k1") },
Valerio Setti18c9fed2022-12-30 17:44:24 +01005564#endif
5565#if defined(MBEDTLS_ECP_DP_CURVE25519_ENABLED) || defined(PSA_WANT_ECC_MONTGOMERY_255)
Gilles Peskine449bd832023-01-11 14:50:10 +01005566 { 29, MBEDTLS_ECP_DP_CURVE25519, PSA_ECC_FAMILY_MONTGOMERY, 255, EC_NAME("x25519") },
Valerio Setti18c9fed2022-12-30 17:44:24 +01005567#endif
5568#if defined(MBEDTLS_ECP_DP_CURVE448_ENABLED) || defined(PSA_WANT_ECC_MONTGOMERY_448)
Gilles Peskine449bd832023-01-11 14:50:10 +01005569 { 30, MBEDTLS_ECP_DP_CURVE448, PSA_ECC_FAMILY_MONTGOMERY, 448, EC_NAME("x448") },
Valerio Setti18c9fed2022-12-30 17:44:24 +01005570#endif
5571 { 0, MBEDTLS_ECP_DP_NONE, 0, 0, NULL },
5572};
5573
Gilles Peskine449bd832023-01-11 14:50:10 +01005574int mbedtls_ssl_get_psa_curve_info_from_tls_id(uint16_t tls_id,
5575 psa_ecc_family_t *family,
5576 size_t *bits)
Valerio Setti18c9fed2022-12-30 17:44:24 +01005577{
Gilles Peskine449bd832023-01-11 14:50:10 +01005578 for (int i = 0; tls_id_match_table[i].tls_id != 0; i++) {
5579 if (tls_id_match_table[i].tls_id == tls_id) {
5580 if (family != NULL) {
Valerio Setti18c9fed2022-12-30 17:44:24 +01005581 *family = tls_id_match_table[i].psa_family;
Gilles Peskine449bd832023-01-11 14:50:10 +01005582 }
5583 if (bits != NULL) {
Valerio Setti18c9fed2022-12-30 17:44:24 +01005584 *bits = tls_id_match_table[i].bits;
Gilles Peskine449bd832023-01-11 14:50:10 +01005585 }
Valerio Setti18c9fed2022-12-30 17:44:24 +01005586 return PSA_SUCCESS;
5587 }
5588 }
5589
5590 return PSA_ERROR_NOT_SUPPORTED;
5591}
5592
Gilles Peskine449bd832023-01-11 14:50:10 +01005593mbedtls_ecp_group_id mbedtls_ssl_get_ecp_group_id_from_tls_id(uint16_t tls_id)
Valerio Setti18c9fed2022-12-30 17:44:24 +01005594{
Gilles Peskine449bd832023-01-11 14:50:10 +01005595 for (int i = 0; tls_id_match_table[i].tls_id != 0; i++) {
5596 if (tls_id_match_table[i].tls_id == tls_id) {
Valerio Setti18c9fed2022-12-30 17:44:24 +01005597 return tls_id_match_table[i].ecp_group_id;
Gilles Peskine449bd832023-01-11 14:50:10 +01005598 }
Valerio Setti18c9fed2022-12-30 17:44:24 +01005599 }
5600
5601 return MBEDTLS_ECP_DP_NONE;
5602}
5603
Gilles Peskine449bd832023-01-11 14:50:10 +01005604uint16_t mbedtls_ssl_get_tls_id_from_ecp_group_id(mbedtls_ecp_group_id grp_id)
Valerio Setti18c9fed2022-12-30 17:44:24 +01005605{
Gilles Peskine449bd832023-01-11 14:50:10 +01005606 for (int i = 0; tls_id_match_table[i].ecp_group_id != MBEDTLS_ECP_DP_NONE;
5607 i++) {
5608 if (tls_id_match_table[i].ecp_group_id == grp_id) {
Valerio Setti18c9fed2022-12-30 17:44:24 +01005609 return tls_id_match_table[i].tls_id;
Gilles Peskine449bd832023-01-11 14:50:10 +01005610 }
Valerio Setti18c9fed2022-12-30 17:44:24 +01005611 }
5612
5613 return 0;
5614}
5615
Valerio Setti67419f02023-01-04 16:12:42 +01005616#if defined(MBEDTLS_DEBUG_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01005617const char *mbedtls_ssl_get_curve_name_from_tls_id(uint16_t tls_id)
Valerio Setti18c9fed2022-12-30 17:44:24 +01005618{
Gilles Peskine449bd832023-01-11 14:50:10 +01005619 for (int i = 0; tls_id_match_table[i].tls_id != 0; i++) {
5620 if (tls_id_match_table[i].tls_id == tls_id) {
Valerio Setti18c9fed2022-12-30 17:44:24 +01005621 return tls_id_match_table[i].name;
Gilles Peskine449bd832023-01-11 14:50:10 +01005622 }
Valerio Setti18c9fed2022-12-30 17:44:24 +01005623 }
5624
5625 return NULL;
5626}
Valerio Setti67419f02023-01-04 16:12:42 +01005627#endif
Valerio Setti18c9fed2022-12-30 17:44:24 +01005628
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005629#if defined(MBEDTLS_X509_CRT_PARSE_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01005630int mbedtls_ssl_check_cert_usage(const mbedtls_x509_crt *cert,
5631 const mbedtls_ssl_ciphersuite_t *ciphersuite,
5632 int cert_endpoint,
5633 uint32_t *flags)
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02005634{
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +01005635 int ret = 0;
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02005636 int usage = 0;
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +02005637 const char *ext_oid;
5638 size_t ext_len;
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +02005639
Gilles Peskine449bd832023-01-11 14:50:10 +01005640 if (cert_endpoint == MBEDTLS_SSL_IS_SERVER) {
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02005641 /* Server part of the key exchange */
Gilles Peskine449bd832023-01-11 14:50:10 +01005642 switch (ciphersuite->key_exchange) {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005643 case MBEDTLS_KEY_EXCHANGE_RSA:
5644 case MBEDTLS_KEY_EXCHANGE_RSA_PSK:
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +01005645 usage = MBEDTLS_X509_KU_KEY_ENCIPHERMENT;
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02005646 break;
5647
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005648 case MBEDTLS_KEY_EXCHANGE_DHE_RSA:
5649 case MBEDTLS_KEY_EXCHANGE_ECDHE_RSA:
5650 case MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA:
5651 usage = MBEDTLS_X509_KU_DIGITAL_SIGNATURE;
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02005652 break;
5653
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005654 case MBEDTLS_KEY_EXCHANGE_ECDH_RSA:
5655 case MBEDTLS_KEY_EXCHANGE_ECDH_ECDSA:
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +01005656 usage = MBEDTLS_X509_KU_KEY_AGREEMENT;
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02005657 break;
5658
5659 /* Don't use default: we want warnings when adding new values */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005660 case MBEDTLS_KEY_EXCHANGE_NONE:
5661 case MBEDTLS_KEY_EXCHANGE_PSK:
5662 case MBEDTLS_KEY_EXCHANGE_DHE_PSK:
5663 case MBEDTLS_KEY_EXCHANGE_ECDHE_PSK:
Manuel Pégourié-Gonnard557535d2015-09-15 17:53:32 +02005664 case MBEDTLS_KEY_EXCHANGE_ECJPAKE:
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02005665 usage = 0;
5666 }
Gilles Peskine449bd832023-01-11 14:50:10 +01005667 } else {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005668 /* Client auth: we only implement rsa_sign and mbedtls_ecdsa_sign for now */
5669 usage = MBEDTLS_X509_KU_DIGITAL_SIGNATURE;
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02005670 }
5671
Gilles Peskine449bd832023-01-11 14:50:10 +01005672 if (mbedtls_x509_crt_check_key_usage(cert, usage) != 0) {
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +01005673 *flags |= MBEDTLS_X509_BADCERT_KEY_USAGE;
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +01005674 ret = -1;
5675 }
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02005676
Gilles Peskine449bd832023-01-11 14:50:10 +01005677 if (cert_endpoint == MBEDTLS_SSL_IS_SERVER) {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005678 ext_oid = MBEDTLS_OID_SERVER_AUTH;
Gilles Peskine449bd832023-01-11 14:50:10 +01005679 ext_len = MBEDTLS_OID_SIZE(MBEDTLS_OID_SERVER_AUTH);
5680 } else {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005681 ext_oid = MBEDTLS_OID_CLIENT_AUTH;
Gilles Peskine449bd832023-01-11 14:50:10 +01005682 ext_len = MBEDTLS_OID_SIZE(MBEDTLS_OID_CLIENT_AUTH);
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +02005683 }
5684
Gilles Peskine449bd832023-01-11 14:50:10 +01005685 if (mbedtls_x509_crt_check_extended_key_usage(cert, ext_oid, ext_len) != 0) {
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +01005686 *flags |= MBEDTLS_X509_BADCERT_EXT_KEY_USAGE;
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +01005687 ret = -1;
5688 }
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +02005689
Gilles Peskine449bd832023-01-11 14:50:10 +01005690 return ret;
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02005691}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005692#endif /* MBEDTLS_X509_CRT_PARSE_C */
Manuel Pégourié-Gonnard3a306b92014-04-29 15:11:17 +02005693
Jerry Yu148165c2021-09-24 23:20:59 +08005694#if defined(MBEDTLS_USE_PSA_CRYPTO)
Gilles Peskine449bd832023-01-11 14:50:10 +01005695int mbedtls_ssl_get_handshake_transcript(mbedtls_ssl_context *ssl,
5696 const mbedtls_md_type_t md,
5697 unsigned char *dst,
5698 size_t dst_len,
5699 size_t *olen)
Jerry Yu148165c2021-09-24 23:20:59 +08005700{
Ronald Cronf6893e12022-01-07 22:09:01 +01005701 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
5702 psa_hash_operation_t *hash_operation_to_clone;
5703 psa_hash_operation_t hash_operation = psa_hash_operation_init();
5704
Jerry Yu148165c2021-09-24 23:20:59 +08005705 *olen = 0;
Ronald Cronf6893e12022-01-07 22:09:01 +01005706
Gilles Peskine449bd832023-01-11 14:50:10 +01005707 switch (md) {
Andrzej Kurek25f27152022-08-17 16:09:31 -04005708#if defined(MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Gilles Peskine449bd832023-01-11 14:50:10 +01005709 case MBEDTLS_MD_SHA384:
5710 hash_operation_to_clone = &ssl->handshake->fin_sha384_psa;
5711 break;
Ronald Cronf6893e12022-01-07 22:09:01 +01005712#endif
5713
Andrzej Kurek25f27152022-08-17 16:09:31 -04005714#if defined(MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Gilles Peskine449bd832023-01-11 14:50:10 +01005715 case MBEDTLS_MD_SHA256:
5716 hash_operation_to_clone = &ssl->handshake->fin_sha256_psa;
5717 break;
Ronald Cronf6893e12022-01-07 22:09:01 +01005718#endif
5719
Gilles Peskine449bd832023-01-11 14:50:10 +01005720 default:
5721 goto exit;
5722 }
5723
5724 status = psa_hash_clone(hash_operation_to_clone, &hash_operation);
5725 if (status != PSA_SUCCESS) {
Ronald Cronf6893e12022-01-07 22:09:01 +01005726 goto exit;
5727 }
5728
Gilles Peskine449bd832023-01-11 14:50:10 +01005729 status = psa_hash_finish(&hash_operation, dst, dst_len, olen);
5730 if (status != PSA_SUCCESS) {
Ronald Cronf6893e12022-01-07 22:09:01 +01005731 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01005732 }
Ronald Cronf6893e12022-01-07 22:09:01 +01005733
5734exit:
Andrzej Kurekeabeb302022-10-17 07:52:51 -04005735#if !defined(MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA) && \
5736 !defined(MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
5737 (void) ssl;
5738#endif
Gilles Peskine449bd832023-01-11 14:50:10 +01005739 return psa_ssl_status_to_mbedtls(status);
Jerry Yu148165c2021-09-24 23:20:59 +08005740}
5741#else /* MBEDTLS_USE_PSA_CRYPTO */
5742
Andrzej Kurek25f27152022-08-17 16:09:31 -04005743#if defined(MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02005744MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01005745static int ssl_get_handshake_transcript_sha384(mbedtls_ssl_context *ssl,
5746 unsigned char *dst,
5747 size_t dst_len,
5748 size_t *olen)
Jerry Yu24c0ec32021-09-09 14:21:07 +08005749{
Jerry Yu24c0ec32021-09-09 14:21:07 +08005750 int ret;
5751 mbedtls_sha512_context sha512;
5752
Gilles Peskine449bd832023-01-11 14:50:10 +01005753 if (dst_len < 48) {
5754 return MBEDTLS_ERR_SSL_INTERNAL_ERROR;
5755 }
Jerry Yu24c0ec32021-09-09 14:21:07 +08005756
Gilles Peskine449bd832023-01-11 14:50:10 +01005757 mbedtls_sha512_init(&sha512);
5758 mbedtls_sha512_clone(&sha512, &ssl->handshake->fin_sha384);
Jerry Yu24c0ec32021-09-09 14:21:07 +08005759
Gilles Peskine449bd832023-01-11 14:50:10 +01005760 if ((ret = mbedtls_sha512_finish(&sha512, dst)) != 0) {
5761 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_sha512_finish", ret);
Jerry Yu24c0ec32021-09-09 14:21:07 +08005762 goto exit;
5763 }
5764
5765 *olen = 48;
5766
5767exit:
5768
Gilles Peskine449bd832023-01-11 14:50:10 +01005769 mbedtls_sha512_free(&sha512);
5770 return ret;
Jerry Yu24c0ec32021-09-09 14:21:07 +08005771}
Andrzej Kurek25f27152022-08-17 16:09:31 -04005772#endif /* MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA */
Jerry Yu24c0ec32021-09-09 14:21:07 +08005773
Andrzej Kurek25f27152022-08-17 16:09:31 -04005774#if defined(MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02005775MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01005776static int ssl_get_handshake_transcript_sha256(mbedtls_ssl_context *ssl,
5777 unsigned char *dst,
5778 size_t dst_len,
5779 size_t *olen)
Jerry Yu24c0ec32021-09-09 14:21:07 +08005780{
Jerry Yu24c0ec32021-09-09 14:21:07 +08005781 int ret;
5782 mbedtls_sha256_context sha256;
5783
Gilles Peskine449bd832023-01-11 14:50:10 +01005784 if (dst_len < 32) {
5785 return MBEDTLS_ERR_SSL_INTERNAL_ERROR;
5786 }
Jerry Yu24c0ec32021-09-09 14:21:07 +08005787
Gilles Peskine449bd832023-01-11 14:50:10 +01005788 mbedtls_sha256_init(&sha256);
5789 mbedtls_sha256_clone(&sha256, &ssl->handshake->fin_sha256);
Jerry Yuc5aef882021-12-23 20:15:02 +08005790
Gilles Peskine449bd832023-01-11 14:50:10 +01005791 if ((ret = mbedtls_sha256_finish(&sha256, dst)) != 0) {
5792 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_sha256_finish", ret);
Jerry Yu24c0ec32021-09-09 14:21:07 +08005793 goto exit;
5794 }
5795
5796 *olen = 32;
5797
5798exit:
5799
Gilles Peskine449bd832023-01-11 14:50:10 +01005800 mbedtls_sha256_free(&sha256);
5801 return ret;
Jerry Yu24c0ec32021-09-09 14:21:07 +08005802}
Andrzej Kurek25f27152022-08-17 16:09:31 -04005803#endif /* MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA */
Jerry Yu24c0ec32021-09-09 14:21:07 +08005804
Gilles Peskine449bd832023-01-11 14:50:10 +01005805int mbedtls_ssl_get_handshake_transcript(mbedtls_ssl_context *ssl,
5806 const mbedtls_md_type_t md,
5807 unsigned char *dst,
5808 size_t dst_len,
5809 size_t *olen)
Jerry Yu24c0ec32021-09-09 14:21:07 +08005810{
Gilles Peskine449bd832023-01-11 14:50:10 +01005811 switch (md) {
Jerry Yuc1ddeef2021-10-08 15:14:45 +08005812
Andrzej Kurek25f27152022-08-17 16:09:31 -04005813#if defined(MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Gilles Peskine449bd832023-01-11 14:50:10 +01005814 case MBEDTLS_MD_SHA384:
5815 return ssl_get_handshake_transcript_sha384(ssl, dst, dst_len, olen);
Andrzej Kurekcccb0442022-08-19 03:42:11 -04005816#endif /* MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA*/
Jerry Yuc1ddeef2021-10-08 15:14:45 +08005817
Andrzej Kurek25f27152022-08-17 16:09:31 -04005818#if defined(MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Gilles Peskine449bd832023-01-11 14:50:10 +01005819 case MBEDTLS_MD_SHA256:
5820 return ssl_get_handshake_transcript_sha256(ssl, dst, dst_len, olen);
Andrzej Kurekcccb0442022-08-19 03:42:11 -04005821#endif /* MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA*/
Jerry Yuc1ddeef2021-10-08 15:14:45 +08005822
Gilles Peskine449bd832023-01-11 14:50:10 +01005823 default:
Andrzej Kurek409248a2022-10-24 10:33:21 -04005824#if !defined(MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA) && \
Gilles Peskine449bd832023-01-11 14:50:10 +01005825 !defined(MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
5826 (void) ssl;
5827 (void) dst;
5828 (void) dst_len;
5829 (void) olen;
Andrzej Kurek409248a2022-10-24 10:33:21 -04005830#endif
Gilles Peskine449bd832023-01-11 14:50:10 +01005831 break;
Jerry Yuc1ddeef2021-10-08 15:14:45 +08005832 }
Gilles Peskine449bd832023-01-11 14:50:10 +01005833 return MBEDTLS_ERR_SSL_INTERNAL_ERROR;
Jerry Yu24c0ec32021-09-09 14:21:07 +08005834}
XiaokangQian647719a2021-12-07 09:16:29 +00005835
Jerry Yu148165c2021-09-24 23:20:59 +08005836#endif /* !MBEDTLS_USE_PSA_CRYPTO */
Jerry Yu24c0ec32021-09-09 14:21:07 +08005837
Ronald Crone68ab4f2022-10-05 12:46:29 +02005838#if defined(MBEDTLS_SSL_HANDSHAKE_WITH_CERT_ENABLED)
Gabor Mezei078e8032022-04-27 21:17:56 +02005839/* mbedtls_ssl_parse_sig_alg_ext()
5840 *
5841 * The `extension_data` field of signature algorithm contains a `SignatureSchemeList`
5842 * value (TLS 1.3 RFC8446):
5843 * enum {
5844 * ....
5845 * ecdsa_secp256r1_sha256( 0x0403 ),
5846 * ecdsa_secp384r1_sha384( 0x0503 ),
5847 * ecdsa_secp521r1_sha512( 0x0603 ),
5848 * ....
5849 * } SignatureScheme;
5850 *
5851 * struct {
5852 * SignatureScheme supported_signature_algorithms<2..2^16-2>;
5853 * } SignatureSchemeList;
5854 *
5855 * The `extension_data` field of signature algorithm contains a `SignatureAndHashAlgorithm`
5856 * value (TLS 1.2 RFC5246):
5857 * enum {
5858 * none(0), md5(1), sha1(2), sha224(3), sha256(4), sha384(5),
5859 * sha512(6), (255)
5860 * } HashAlgorithm;
5861 *
5862 * enum { anonymous(0), rsa(1), dsa(2), ecdsa(3), (255) }
5863 * SignatureAlgorithm;
5864 *
5865 * struct {
5866 * HashAlgorithm hash;
5867 * SignatureAlgorithm signature;
5868 * } SignatureAndHashAlgorithm;
5869 *
5870 * SignatureAndHashAlgorithm
5871 * supported_signature_algorithms<2..2^16-2>;
5872 *
5873 * The TLS 1.3 signature algorithm extension was defined to be a compatible
5874 * generalization of the TLS 1.2 signature algorithm extension.
5875 * `SignatureAndHashAlgorithm` field of TLS 1.2 can be represented by
5876 * `SignatureScheme` field of TLS 1.3
5877 *
5878 */
Gilles Peskine449bd832023-01-11 14:50:10 +01005879int mbedtls_ssl_parse_sig_alg_ext(mbedtls_ssl_context *ssl,
5880 const unsigned char *buf,
5881 const unsigned char *end)
Gabor Mezei078e8032022-04-27 21:17:56 +02005882{
5883 const unsigned char *p = buf;
5884 size_t supported_sig_algs_len = 0;
5885 const unsigned char *supported_sig_algs_end;
5886 uint16_t sig_alg;
5887 uint32_t common_idx = 0;
5888
Gilles Peskine449bd832023-01-11 14:50:10 +01005889 MBEDTLS_SSL_CHK_BUF_READ_PTR(p, end, 2);
5890 supported_sig_algs_len = MBEDTLS_GET_UINT16_BE(p, 0);
Gabor Mezei078e8032022-04-27 21:17:56 +02005891 p += 2;
5892
Gilles Peskine449bd832023-01-11 14:50:10 +01005893 memset(ssl->handshake->received_sig_algs, 0,
5894 sizeof(ssl->handshake->received_sig_algs));
Gabor Mezei078e8032022-04-27 21:17:56 +02005895
Gilles Peskine449bd832023-01-11 14:50:10 +01005896 MBEDTLS_SSL_CHK_BUF_READ_PTR(p, end, supported_sig_algs_len);
Gabor Mezei078e8032022-04-27 21:17:56 +02005897 supported_sig_algs_end = p + supported_sig_algs_len;
Gilles Peskine449bd832023-01-11 14:50:10 +01005898 while (p < supported_sig_algs_end) {
5899 MBEDTLS_SSL_CHK_BUF_READ_PTR(p, supported_sig_algs_end, 2);
5900 sig_alg = MBEDTLS_GET_UINT16_BE(p, 0);
Gabor Mezei078e8032022-04-27 21:17:56 +02005901 p += 2;
Gilles Peskine449bd832023-01-11 14:50:10 +01005902 MBEDTLS_SSL_DEBUG_MSG(4, ("received signature algorithm: 0x%x %s",
5903 sig_alg,
5904 mbedtls_ssl_sig_alg_to_str(sig_alg)));
Jerry Yu2fe6c632022-06-29 10:02:38 +08005905#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
Gilles Peskine449bd832023-01-11 14:50:10 +01005906 if (ssl->tls_version == MBEDTLS_SSL_VERSION_TLS1_2 &&
5907 (!(mbedtls_ssl_sig_alg_is_supported(ssl, sig_alg) &&
5908 mbedtls_ssl_sig_alg_is_offered(ssl, sig_alg)))) {
Gabor Mezei078e8032022-04-27 21:17:56 +02005909 continue;
Jerry Yu2fe6c632022-06-29 10:02:38 +08005910 }
5911#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Gabor Mezei078e8032022-04-27 21:17:56 +02005912
Gilles Peskine449bd832023-01-11 14:50:10 +01005913 MBEDTLS_SSL_DEBUG_MSG(4, ("valid signature algorithm: %s",
5914 mbedtls_ssl_sig_alg_to_str(sig_alg)));
Gabor Mezei078e8032022-04-27 21:17:56 +02005915
Gilles Peskine449bd832023-01-11 14:50:10 +01005916 if (common_idx + 1 < MBEDTLS_RECEIVED_SIG_ALGS_SIZE) {
Gabor Mezei078e8032022-04-27 21:17:56 +02005917 ssl->handshake->received_sig_algs[common_idx] = sig_alg;
5918 common_idx += 1;
5919 }
5920 }
5921 /* Check that we consumed all the message. */
Gilles Peskine449bd832023-01-11 14:50:10 +01005922 if (p != end) {
5923 MBEDTLS_SSL_DEBUG_MSG(1,
5924 ("Signature algorithms extension length misaligned"));
5925 MBEDTLS_SSL_PEND_FATAL_ALERT(MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR,
5926 MBEDTLS_ERR_SSL_DECODE_ERROR);
5927 return MBEDTLS_ERR_SSL_DECODE_ERROR;
Gabor Mezei078e8032022-04-27 21:17:56 +02005928 }
5929
Gilles Peskine449bd832023-01-11 14:50:10 +01005930 if (common_idx == 0) {
5931 MBEDTLS_SSL_DEBUG_MSG(3, ("no signature algorithm in common"));
5932 MBEDTLS_SSL_PEND_FATAL_ALERT(MBEDTLS_SSL_ALERT_MSG_HANDSHAKE_FAILURE,
5933 MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE);
5934 return MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE;
Gabor Mezei078e8032022-04-27 21:17:56 +02005935 }
5936
Gabor Mezei15b95a62022-05-09 16:37:58 +02005937 ssl->handshake->received_sig_algs[common_idx] = MBEDTLS_TLS_SIG_NONE;
Gilles Peskine449bd832023-01-11 14:50:10 +01005938 return 0;
Gabor Mezei078e8032022-04-27 21:17:56 +02005939}
5940
Ronald Crone68ab4f2022-10-05 12:46:29 +02005941#endif /* MBEDTLS_SSL_HANDSHAKE_WITH_CERT_ENABLED */
Gabor Mezei078e8032022-04-27 21:17:56 +02005942
Jerry Yudc7bd172022-02-17 13:44:15 +08005943#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
5944
5945#if defined(MBEDTLS_USE_PSA_CRYPTO)
5946
Gilles Peskine449bd832023-01-11 14:50:10 +01005947static psa_status_t setup_psa_key_derivation(psa_key_derivation_operation_t *derivation,
5948 mbedtls_svc_key_id_t key,
5949 psa_algorithm_t alg,
5950 const unsigned char *raw_psk, size_t raw_psk_length,
5951 const unsigned char *seed, size_t seed_length,
5952 const unsigned char *label, size_t label_length,
5953 const unsigned char *other_secret,
5954 size_t other_secret_length,
5955 size_t capacity)
Jerry Yudc7bd172022-02-17 13:44:15 +08005956{
5957 psa_status_t status;
5958
Gilles Peskine449bd832023-01-11 14:50:10 +01005959 status = psa_key_derivation_setup(derivation, alg);
5960 if (status != PSA_SUCCESS) {
5961 return status;
5962 }
Jerry Yudc7bd172022-02-17 13:44:15 +08005963
Gilles Peskine449bd832023-01-11 14:50:10 +01005964 if (PSA_ALG_IS_TLS12_PRF(alg) || PSA_ALG_IS_TLS12_PSK_TO_MS(alg)) {
5965 status = psa_key_derivation_input_bytes(derivation,
5966 PSA_KEY_DERIVATION_INPUT_SEED,
5967 seed, seed_length);
5968 if (status != PSA_SUCCESS) {
5969 return status;
Przemek Stekiel1f027032022-04-05 17:12:11 +02005970 }
5971
Gilles Peskine449bd832023-01-11 14:50:10 +01005972 if (other_secret != NULL) {
5973 status = psa_key_derivation_input_bytes(derivation,
5974 PSA_KEY_DERIVATION_INPUT_OTHER_SECRET,
5975 other_secret, other_secret_length);
5976 if (status != PSA_SUCCESS) {
5977 return status;
5978 }
5979 }
5980
5981 if (mbedtls_svc_key_id_is_null(key)) {
Jerry Yudc7bd172022-02-17 13:44:15 +08005982 status = psa_key_derivation_input_bytes(
5983 derivation, PSA_KEY_DERIVATION_INPUT_SECRET,
Gilles Peskine449bd832023-01-11 14:50:10 +01005984 raw_psk, raw_psk_length);
5985 } else {
Jerry Yudc7bd172022-02-17 13:44:15 +08005986 status = psa_key_derivation_input_key(
Gilles Peskine449bd832023-01-11 14:50:10 +01005987 derivation, PSA_KEY_DERIVATION_INPUT_SECRET, key);
Jerry Yudc7bd172022-02-17 13:44:15 +08005988 }
Gilles Peskine449bd832023-01-11 14:50:10 +01005989 if (status != PSA_SUCCESS) {
5990 return status;
5991 }
Jerry Yudc7bd172022-02-17 13:44:15 +08005992
Gilles Peskine449bd832023-01-11 14:50:10 +01005993 status = psa_key_derivation_input_bytes(derivation,
5994 PSA_KEY_DERIVATION_INPUT_LABEL,
5995 label, label_length);
5996 if (status != PSA_SUCCESS) {
5997 return status;
5998 }
5999 } else {
6000 return PSA_ERROR_NOT_SUPPORTED;
Jerry Yudc7bd172022-02-17 13:44:15 +08006001 }
6002
Gilles Peskine449bd832023-01-11 14:50:10 +01006003 status = psa_key_derivation_set_capacity(derivation, capacity);
6004 if (status != PSA_SUCCESS) {
6005 return status;
6006 }
Jerry Yudc7bd172022-02-17 13:44:15 +08006007
Gilles Peskine449bd832023-01-11 14:50:10 +01006008 return PSA_SUCCESS;
Jerry Yudc7bd172022-02-17 13:44:15 +08006009}
6010
Andrzej Kurek57d10632022-10-24 10:32:01 -04006011#if defined(PSA_WANT_ALG_SHA_384) || \
6012 defined(PSA_WANT_ALG_SHA_256)
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02006013MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01006014static int tls_prf_generic(mbedtls_md_type_t md_type,
6015 const unsigned char *secret, size_t slen,
6016 const char *label,
6017 const unsigned char *random, size_t rlen,
6018 unsigned char *dstbuf, size_t dlen)
Jerry Yudc7bd172022-02-17 13:44:15 +08006019{
6020 psa_status_t status;
6021 psa_algorithm_t alg;
6022 mbedtls_svc_key_id_t master_key = MBEDTLS_SVC_KEY_ID_INIT;
6023 psa_key_derivation_operation_t derivation =
6024 PSA_KEY_DERIVATION_OPERATION_INIT;
6025
Gilles Peskine449bd832023-01-11 14:50:10 +01006026 if (md_type == MBEDTLS_MD_SHA384) {
Jerry Yudc7bd172022-02-17 13:44:15 +08006027 alg = PSA_ALG_TLS12_PRF(PSA_ALG_SHA_384);
Gilles Peskine449bd832023-01-11 14:50:10 +01006028 } else {
Jerry Yudc7bd172022-02-17 13:44:15 +08006029 alg = PSA_ALG_TLS12_PRF(PSA_ALG_SHA_256);
Gilles Peskine449bd832023-01-11 14:50:10 +01006030 }
Jerry Yudc7bd172022-02-17 13:44:15 +08006031
6032 /* Normally a "secret" should be long enough to be impossible to
6033 * find by brute force, and in particular should not be empty. But
6034 * this PRF is also used to derive an IV, in particular in EAP-TLS,
6035 * and for this use case it makes sense to have a 0-length "secret".
6036 * Since the key API doesn't allow importing a key of length 0,
6037 * keep master_key=0, which setup_psa_key_derivation() understands
6038 * to mean a 0-length "secret" input. */
Gilles Peskine449bd832023-01-11 14:50:10 +01006039 if (slen != 0) {
Jerry Yudc7bd172022-02-17 13:44:15 +08006040 psa_key_attributes_t key_attributes = psa_key_attributes_init();
Gilles Peskine449bd832023-01-11 14:50:10 +01006041 psa_set_key_usage_flags(&key_attributes, PSA_KEY_USAGE_DERIVE);
6042 psa_set_key_algorithm(&key_attributes, alg);
6043 psa_set_key_type(&key_attributes, PSA_KEY_TYPE_DERIVE);
Jerry Yudc7bd172022-02-17 13:44:15 +08006044
Gilles Peskine449bd832023-01-11 14:50:10 +01006045 status = psa_import_key(&key_attributes, secret, slen, &master_key);
6046 if (status != PSA_SUCCESS) {
6047 return MBEDTLS_ERR_SSL_HW_ACCEL_FAILED;
6048 }
Jerry Yudc7bd172022-02-17 13:44:15 +08006049 }
6050
Gilles Peskine449bd832023-01-11 14:50:10 +01006051 status = setup_psa_key_derivation(&derivation,
6052 master_key, alg,
6053 NULL, 0,
6054 random, rlen,
6055 (unsigned char const *) label,
6056 (size_t) strlen(label),
6057 NULL, 0,
6058 dlen);
6059 if (status != PSA_SUCCESS) {
6060 psa_key_derivation_abort(&derivation);
6061 psa_destroy_key(master_key);
6062 return MBEDTLS_ERR_SSL_HW_ACCEL_FAILED;
Jerry Yudc7bd172022-02-17 13:44:15 +08006063 }
6064
Gilles Peskine449bd832023-01-11 14:50:10 +01006065 status = psa_key_derivation_output_bytes(&derivation, dstbuf, dlen);
6066 if (status != PSA_SUCCESS) {
6067 psa_key_derivation_abort(&derivation);
6068 psa_destroy_key(master_key);
6069 return MBEDTLS_ERR_SSL_HW_ACCEL_FAILED;
Jerry Yudc7bd172022-02-17 13:44:15 +08006070 }
6071
Gilles Peskine449bd832023-01-11 14:50:10 +01006072 status = psa_key_derivation_abort(&derivation);
6073 if (status != PSA_SUCCESS) {
6074 psa_destroy_key(master_key);
6075 return MBEDTLS_ERR_SSL_HW_ACCEL_FAILED;
Jerry Yudc7bd172022-02-17 13:44:15 +08006076 }
6077
Gilles Peskine449bd832023-01-11 14:50:10 +01006078 if (!mbedtls_svc_key_id_is_null(master_key)) {
6079 status = psa_destroy_key(master_key);
6080 }
6081 if (status != PSA_SUCCESS) {
6082 return MBEDTLS_ERR_SSL_HW_ACCEL_FAILED;
6083 }
Jerry Yudc7bd172022-02-17 13:44:15 +08006084
Gilles Peskine449bd832023-01-11 14:50:10 +01006085 return 0;
Jerry Yudc7bd172022-02-17 13:44:15 +08006086}
Andrzej Kurek57d10632022-10-24 10:32:01 -04006087#endif /* PSA_WANT_ALG_SHA_256 || PSA_WANT_ALG_SHA_384 */
Jerry Yudc7bd172022-02-17 13:44:15 +08006088#else /* MBEDTLS_USE_PSA_CRYPTO */
6089
Andrzej Kurek57d10632022-10-24 10:32:01 -04006090#if defined(MBEDTLS_MD_C) && \
Gilles Peskine449bd832023-01-11 14:50:10 +01006091 (defined(MBEDTLS_SHA256_C) || \
6092 defined(MBEDTLS_SHA384_C))
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02006093MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01006094static int tls_prf_generic(mbedtls_md_type_t md_type,
6095 const unsigned char *secret, size_t slen,
6096 const char *label,
6097 const unsigned char *random, size_t rlen,
6098 unsigned char *dstbuf, size_t dlen)
Jerry Yudc7bd172022-02-17 13:44:15 +08006099{
6100 size_t nb;
6101 size_t i, j, k, md_len;
6102 unsigned char *tmp;
6103 size_t tmp_len = 0;
6104 unsigned char h_i[MBEDTLS_MD_MAX_SIZE];
6105 const mbedtls_md_info_t *md_info;
6106 mbedtls_md_context_t md_ctx;
6107 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
6108
Gilles Peskine449bd832023-01-11 14:50:10 +01006109 mbedtls_md_init(&md_ctx);
Jerry Yudc7bd172022-02-17 13:44:15 +08006110
Gilles Peskine449bd832023-01-11 14:50:10 +01006111 if ((md_info = mbedtls_md_info_from_type(md_type)) == NULL) {
6112 return MBEDTLS_ERR_SSL_INTERNAL_ERROR;
6113 }
Jerry Yudc7bd172022-02-17 13:44:15 +08006114
Gilles Peskine449bd832023-01-11 14:50:10 +01006115 md_len = mbedtls_md_get_size(md_info);
Jerry Yudc7bd172022-02-17 13:44:15 +08006116
Gilles Peskine449bd832023-01-11 14:50:10 +01006117 tmp_len = md_len + strlen(label) + rlen;
6118 tmp = mbedtls_calloc(1, tmp_len);
6119 if (tmp == NULL) {
Jerry Yudc7bd172022-02-17 13:44:15 +08006120 ret = MBEDTLS_ERR_SSL_ALLOC_FAILED;
6121 goto exit;
6122 }
6123
Gilles Peskine449bd832023-01-11 14:50:10 +01006124 nb = strlen(label);
6125 memcpy(tmp + md_len, label, nb);
6126 memcpy(tmp + md_len + nb, random, rlen);
Jerry Yudc7bd172022-02-17 13:44:15 +08006127 nb += rlen;
6128
6129 /*
6130 * Compute P_<hash>(secret, label + random)[0..dlen]
6131 */
Gilles Peskine449bd832023-01-11 14:50:10 +01006132 if ((ret = mbedtls_md_setup(&md_ctx, md_info, 1)) != 0) {
Jerry Yudc7bd172022-02-17 13:44:15 +08006133 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01006134 }
Jerry Yudc7bd172022-02-17 13:44:15 +08006135
Gilles Peskine449bd832023-01-11 14:50:10 +01006136 ret = mbedtls_md_hmac_starts(&md_ctx, secret, slen);
6137 if (ret != 0) {
Jerry Yudc7bd172022-02-17 13:44:15 +08006138 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01006139 }
6140 ret = mbedtls_md_hmac_update(&md_ctx, tmp + md_len, nb);
6141 if (ret != 0) {
Jerry Yudc7bd172022-02-17 13:44:15 +08006142 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01006143 }
6144 ret = mbedtls_md_hmac_finish(&md_ctx, tmp);
6145 if (ret != 0) {
Jerry Yudc7bd172022-02-17 13:44:15 +08006146 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01006147 }
Jerry Yudc7bd172022-02-17 13:44:15 +08006148
Gilles Peskine449bd832023-01-11 14:50:10 +01006149 for (i = 0; i < dlen; i += md_len) {
6150 ret = mbedtls_md_hmac_reset(&md_ctx);
6151 if (ret != 0) {
Jerry Yudc7bd172022-02-17 13:44:15 +08006152 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01006153 }
6154 ret = mbedtls_md_hmac_update(&md_ctx, tmp, md_len + nb);
6155 if (ret != 0) {
Jerry Yudc7bd172022-02-17 13:44:15 +08006156 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01006157 }
6158 ret = mbedtls_md_hmac_finish(&md_ctx, h_i);
6159 if (ret != 0) {
Jerry Yudc7bd172022-02-17 13:44:15 +08006160 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01006161 }
Jerry Yudc7bd172022-02-17 13:44:15 +08006162
Gilles Peskine449bd832023-01-11 14:50:10 +01006163 ret = mbedtls_md_hmac_reset(&md_ctx);
6164 if (ret != 0) {
Jerry Yudc7bd172022-02-17 13:44:15 +08006165 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01006166 }
6167 ret = mbedtls_md_hmac_update(&md_ctx, tmp, md_len);
6168 if (ret != 0) {
Jerry Yudc7bd172022-02-17 13:44:15 +08006169 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01006170 }
6171 ret = mbedtls_md_hmac_finish(&md_ctx, tmp);
6172 if (ret != 0) {
Jerry Yudc7bd172022-02-17 13:44:15 +08006173 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01006174 }
Jerry Yudc7bd172022-02-17 13:44:15 +08006175
Gilles Peskine449bd832023-01-11 14:50:10 +01006176 k = (i + md_len > dlen) ? dlen % md_len : md_len;
Jerry Yudc7bd172022-02-17 13:44:15 +08006177
Gilles Peskine449bd832023-01-11 14:50:10 +01006178 for (j = 0; j < k; j++) {
Jerry Yudc7bd172022-02-17 13:44:15 +08006179 dstbuf[i + j] = h_i[j];
Gilles Peskine449bd832023-01-11 14:50:10 +01006180 }
Jerry Yudc7bd172022-02-17 13:44:15 +08006181 }
6182
6183exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01006184 mbedtls_md_free(&md_ctx);
Jerry Yudc7bd172022-02-17 13:44:15 +08006185
Gilles Peskine449bd832023-01-11 14:50:10 +01006186 if (tmp != NULL) {
6187 mbedtls_platform_zeroize(tmp, tmp_len);
6188 }
Dave Rodgman29b9b2b2022-11-01 16:08:14 +00006189
Gilles Peskine449bd832023-01-11 14:50:10 +01006190 mbedtls_platform_zeroize(h_i, sizeof(h_i));
Jerry Yudc7bd172022-02-17 13:44:15 +08006191
Gilles Peskine449bd832023-01-11 14:50:10 +01006192 mbedtls_free(tmp);
Jerry Yudc7bd172022-02-17 13:44:15 +08006193
Gilles Peskine449bd832023-01-11 14:50:10 +01006194 return ret;
Jerry Yudc7bd172022-02-17 13:44:15 +08006195}
Andrzej Kurek57d10632022-10-24 10:32:01 -04006196#endif /* MBEDTLS_MD_C && ( MBEDTLS_SHA256_C || MBEDTLS_SHA384_C ) */
Jerry Yudc7bd172022-02-17 13:44:15 +08006197#endif /* MBEDTLS_USE_PSA_CRYPTO */
6198
Andrzej Kurek25f27152022-08-17 16:09:31 -04006199#if defined(MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02006200MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01006201static int tls_prf_sha256(const unsigned char *secret, size_t slen,
6202 const char *label,
6203 const unsigned char *random, size_t rlen,
6204 unsigned char *dstbuf, size_t dlen)
Jerry Yudc7bd172022-02-17 13:44:15 +08006205{
Gilles Peskine449bd832023-01-11 14:50:10 +01006206 return tls_prf_generic(MBEDTLS_MD_SHA256, secret, slen,
6207 label, random, rlen, dstbuf, dlen);
Jerry Yudc7bd172022-02-17 13:44:15 +08006208}
Andrzej Kurekcccb0442022-08-19 03:42:11 -04006209#endif /* MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA*/
Jerry Yudc7bd172022-02-17 13:44:15 +08006210
Andrzej Kurek25f27152022-08-17 16:09:31 -04006211#if defined(MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02006212MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01006213static int tls_prf_sha384(const unsigned char *secret, size_t slen,
6214 const char *label,
6215 const unsigned char *random, size_t rlen,
6216 unsigned char *dstbuf, size_t dlen)
Jerry Yudc7bd172022-02-17 13:44:15 +08006217{
Gilles Peskine449bd832023-01-11 14:50:10 +01006218 return tls_prf_generic(MBEDTLS_MD_SHA384, secret, slen,
6219 label, random, rlen, dstbuf, dlen);
Jerry Yudc7bd172022-02-17 13:44:15 +08006220}
Andrzej Kurekcccb0442022-08-19 03:42:11 -04006221#endif /* MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA*/
Jerry Yudc7bd172022-02-17 13:44:15 +08006222
Jerry Yuf009d862022-02-17 14:01:37 +08006223/*
6224 * Set appropriate PRF function and other SSL / TLS1.2 functions
6225 *
6226 * Inputs:
Jerry Yuf009d862022-02-17 14:01:37 +08006227 * - hash associated with the ciphersuite (only used by TLS 1.2)
6228 *
6229 * Outputs:
6230 * - the tls_prf, calc_verify and calc_finished members of handshake structure
6231 */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02006232MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01006233static int ssl_set_handshake_prfs(mbedtls_ssl_handshake_params *handshake,
6234 mbedtls_md_type_t hash)
Jerry Yuf009d862022-02-17 14:01:37 +08006235{
Andrzej Kurek25f27152022-08-17 16:09:31 -04006236#if defined(MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Gilles Peskine449bd832023-01-11 14:50:10 +01006237 if (hash == MBEDTLS_MD_SHA384) {
Jerry Yuf009d862022-02-17 14:01:37 +08006238 handshake->tls_prf = tls_prf_sha384;
6239 handshake->calc_verify = ssl_calc_verify_tls_sha384;
6240 handshake->calc_finished = ssl_calc_finished_tls_sha384;
Gilles Peskine449bd832023-01-11 14:50:10 +01006241 } else
Jerry Yuf009d862022-02-17 14:01:37 +08006242#endif
Andrzej Kurek25f27152022-08-17 16:09:31 -04006243#if defined(MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Jerry Yuf009d862022-02-17 14:01:37 +08006244 {
Ronald Cron81591aa2022-03-07 09:05:51 +01006245 (void) hash;
Jerry Yuf009d862022-02-17 14:01:37 +08006246 handshake->tls_prf = tls_prf_sha256;
6247 handshake->calc_verify = ssl_calc_verify_tls_sha256;
6248 handshake->calc_finished = ssl_calc_finished_tls_sha256;
6249 }
Ronald Cron81591aa2022-03-07 09:05:51 +01006250#else
Jerry Yuf009d862022-02-17 14:01:37 +08006251 {
Jerry Yuf009d862022-02-17 14:01:37 +08006252 (void) handshake;
Ronald Cron81591aa2022-03-07 09:05:51 +01006253 (void) hash;
Gilles Peskine449bd832023-01-11 14:50:10 +01006254 return MBEDTLS_ERR_SSL_INTERNAL_ERROR;
Jerry Yuf009d862022-02-17 14:01:37 +08006255 }
Ronald Cron81591aa2022-03-07 09:05:51 +01006256#endif
Jerry Yuf009d862022-02-17 14:01:37 +08006257
Gilles Peskine449bd832023-01-11 14:50:10 +01006258 return 0;
Jerry Yuf009d862022-02-17 14:01:37 +08006259}
Jerry Yud6ab2352022-02-17 14:03:43 +08006260
Jerry Yu2a7b5ac2022-02-17 14:07:00 +08006261/*
6262 * Compute master secret if needed
6263 *
6264 * Parameters:
6265 * [in/out] handshake
6266 * [in] resume, premaster, extended_ms, calc_verify, tls_prf
6267 * (PSA-PSK) ciphersuite_info, psk_opaque
6268 * [out] premaster (cleared)
6269 * [out] master
6270 * [in] ssl: optionally used for debugging, EMS and PSA-PSK
6271 * debug: conf->f_dbg, conf->p_dbg
6272 * EMS: passed to calc_verify (debug + session_negotiate)
Ronald Crona25cf582022-03-07 11:10:36 +01006273 * PSA-PSA: conf
Jerry Yu2a7b5ac2022-02-17 14:07:00 +08006274 */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02006275MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01006276static int ssl_compute_master(mbedtls_ssl_handshake_params *handshake,
6277 unsigned char *master,
6278 const mbedtls_ssl_context *ssl)
Jerry Yu2a7b5ac2022-02-17 14:07:00 +08006279{
6280 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
6281
6282 /* cf. RFC 5246, Section 8.1:
6283 * "The master secret is always exactly 48 bytes in length." */
6284 size_t const master_secret_len = 48;
6285
6286#if defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET)
6287 unsigned char session_hash[48];
6288#endif /* MBEDTLS_SSL_EXTENDED_MASTER_SECRET */
6289
6290 /* The label for the KDF used for key expansion.
6291 * This is either "master secret" or "extended master secret"
6292 * depending on whether the Extended Master Secret extension
6293 * is used. */
6294 char const *lbl = "master secret";
6295
Przemek Stekielae4ed302022-04-05 17:15:55 +02006296 /* The seed for the KDF used for key expansion.
Jerry Yu2a7b5ac2022-02-17 14:07:00 +08006297 * - If the Extended Master Secret extension is not used,
6298 * this is ClientHello.Random + ServerHello.Random
6299 * (see Sect. 8.1 in RFC 5246).
6300 * - If the Extended Master Secret extension is used,
6301 * this is the transcript of the handshake so far.
6302 * (see Sect. 4 in RFC 7627). */
Przemek Stekielae4ed302022-04-05 17:15:55 +02006303 unsigned char const *seed = handshake->randbytes;
6304 size_t seed_len = 64;
Jerry Yu2a7b5ac2022-02-17 14:07:00 +08006305
6306#if !defined(MBEDTLS_DEBUG_C) && \
6307 !defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET) && \
6308 !(defined(MBEDTLS_USE_PSA_CRYPTO) && \
Gilles Peskine449bd832023-01-11 14:50:10 +01006309 defined(MBEDTLS_KEY_EXCHANGE_PSK_ENABLED))
Jerry Yu2a7b5ac2022-02-17 14:07:00 +08006310 ssl = NULL; /* make sure we don't use it except for those cases */
6311 (void) ssl;
6312#endif
6313
Gilles Peskine449bd832023-01-11 14:50:10 +01006314 if (handshake->resume != 0) {
6315 MBEDTLS_SSL_DEBUG_MSG(3, ("no premaster (session resumed)"));
6316 return 0;
Jerry Yu2a7b5ac2022-02-17 14:07:00 +08006317 }
6318
6319#if defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET)
Gilles Peskine449bd832023-01-11 14:50:10 +01006320 if (handshake->extended_ms == MBEDTLS_SSL_EXTENDED_MS_ENABLED) {
Jerry Yu2a7b5ac2022-02-17 14:07:00 +08006321 lbl = "extended master secret";
Przemek Stekielae4ed302022-04-05 17:15:55 +02006322 seed = session_hash;
Manuel Pégourié-Gonnardb8b07aa2023-02-06 00:34:21 +01006323 ret = handshake->calc_verify(ssl, session_hash, &seed_len);
6324 if (ret != 0) {
6325 MBEDTLS_SSL_DEBUG_RET(1, "calc_verify", ret);
6326 }
Jerry Yu2a7b5ac2022-02-17 14:07:00 +08006327
Gilles Peskine449bd832023-01-11 14:50:10 +01006328 MBEDTLS_SSL_DEBUG_BUF(3, "session hash for extended master secret",
6329 session_hash, seed_len);
Jerry Yu2a7b5ac2022-02-17 14:07:00 +08006330 }
Przemek Stekiel169bf0b2022-04-29 07:53:29 +02006331#endif /* MBEDTLS_SSL_EXTENDED_MASTER_SECRET */
Jerry Yu2a7b5ac2022-02-17 14:07:00 +08006332
Przemek Stekiel99114f32022-04-22 11:20:09 +02006333#if defined(MBEDTLS_USE_PSA_CRYPTO) && \
Przemek Stekiel8a4b7fd2022-04-28 09:22:22 +02006334 defined(MBEDTLS_KEY_EXCHANGE_SOME_PSK_ENABLED)
Gilles Peskine449bd832023-01-11 14:50:10 +01006335 if (mbedtls_ssl_ciphersuite_uses_psk(handshake->ciphersuite_info) == 1) {
Jerry Yu2a7b5ac2022-02-17 14:07:00 +08006336 /* Perform PSK-to-MS expansion in a single step. */
6337 psa_status_t status;
6338 psa_algorithm_t alg;
6339 mbedtls_svc_key_id_t psk;
6340 psa_key_derivation_operation_t derivation =
6341 PSA_KEY_DERIVATION_OPERATION_INIT;
6342 mbedtls_md_type_t hash_alg = handshake->ciphersuite_info->mac;
6343
Gilles Peskine449bd832023-01-11 14:50:10 +01006344 MBEDTLS_SSL_DEBUG_MSG(2, ("perform PSA-based PSK-to-MS expansion"));
Jerry Yu2a7b5ac2022-02-17 14:07:00 +08006345
Gilles Peskine449bd832023-01-11 14:50:10 +01006346 psk = mbedtls_ssl_get_opaque_psk(ssl);
Jerry Yu2a7b5ac2022-02-17 14:07:00 +08006347
Gilles Peskine449bd832023-01-11 14:50:10 +01006348 if (hash_alg == MBEDTLS_MD_SHA384) {
Jerry Yu2a7b5ac2022-02-17 14:07:00 +08006349 alg = PSA_ALG_TLS12_PSK_TO_MS(PSA_ALG_SHA_384);
Gilles Peskine449bd832023-01-11 14:50:10 +01006350 } else {
Jerry Yu2a7b5ac2022-02-17 14:07:00 +08006351 alg = PSA_ALG_TLS12_PSK_TO_MS(PSA_ALG_SHA_256);
Gilles Peskine449bd832023-01-11 14:50:10 +01006352 }
Jerry Yu2a7b5ac2022-02-17 14:07:00 +08006353
Przemek Stekiel51a1f362022-04-13 08:57:06 +02006354 size_t other_secret_len = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +01006355 unsigned char *other_secret = NULL;
Przemek Stekielc2033402022-04-05 17:19:41 +02006356
Gilles Peskine449bd832023-01-11 14:50:10 +01006357 switch (handshake->ciphersuite_info->key_exchange) {
Przemek Stekiel19b80f82022-04-14 08:29:31 +02006358 /* Provide other secret.
Przemek Stekiel51a1f362022-04-13 08:57:06 +02006359 * Other secret is stored in premaster, where first 2 bytes hold the
Przemek Stekiel19b80f82022-04-14 08:29:31 +02006360 * length of the other key.
Przemek Stekielc2033402022-04-05 17:19:41 +02006361 */
Przemek Stekiel19b80f82022-04-14 08:29:31 +02006362 case MBEDTLS_KEY_EXCHANGE_RSA_PSK:
Przemek Stekiel8abcee92022-04-28 09:16:28 +02006363 /* For RSA-PSK other key length is always 48 bytes. */
Przemek Stekiel19b80f82022-04-14 08:29:31 +02006364 other_secret_len = 48;
6365 other_secret = handshake->premaster + 2;
6366 break;
6367 case MBEDTLS_KEY_EXCHANGE_ECDHE_PSK:
Przemek Stekielb293aaa2022-04-19 12:22:38 +02006368 case MBEDTLS_KEY_EXCHANGE_DHE_PSK:
Przemek Stekiel19b80f82022-04-14 08:29:31 +02006369 other_secret_len = MBEDTLS_GET_UINT16_BE(handshake->premaster, 0);
6370 other_secret = handshake->premaster + 2;
6371 break;
6372 default:
6373 break;
Przemek Stekielc2033402022-04-05 17:19:41 +02006374 }
6375
Gilles Peskine449bd832023-01-11 14:50:10 +01006376 status = setup_psa_key_derivation(&derivation, psk, alg,
6377 ssl->conf->psk, ssl->conf->psk_len,
6378 seed, seed_len,
6379 (unsigned char const *) lbl,
6380 (size_t) strlen(lbl),
6381 other_secret, other_secret_len,
6382 master_secret_len);
6383 if (status != PSA_SUCCESS) {
6384 psa_key_derivation_abort(&derivation);
6385 return MBEDTLS_ERR_SSL_HW_ACCEL_FAILED;
Jerry Yu2a7b5ac2022-02-17 14:07:00 +08006386 }
6387
Gilles Peskine449bd832023-01-11 14:50:10 +01006388 status = psa_key_derivation_output_bytes(&derivation,
6389 master,
6390 master_secret_len);
6391 if (status != PSA_SUCCESS) {
6392 psa_key_derivation_abort(&derivation);
6393 return MBEDTLS_ERR_SSL_HW_ACCEL_FAILED;
Jerry Yu2a7b5ac2022-02-17 14:07:00 +08006394 }
6395
Gilles Peskine449bd832023-01-11 14:50:10 +01006396 status = psa_key_derivation_abort(&derivation);
6397 if (status != PSA_SUCCESS) {
6398 return MBEDTLS_ERR_SSL_HW_ACCEL_FAILED;
6399 }
6400 } else
Jerry Yu2a7b5ac2022-02-17 14:07:00 +08006401#endif
6402 {
Neil Armstrongca7d5062022-05-31 14:43:23 +02006403#if defined(MBEDTLS_USE_PSA_CRYPTO) && \
Gilles Peskine449bd832023-01-11 14:50:10 +01006404 defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
6405 if (handshake->ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECJPAKE) {
Neil Armstrongca7d5062022-05-31 14:43:23 +02006406 psa_status_t status;
6407 psa_algorithm_t alg = PSA_ALG_TLS12_ECJPAKE_TO_PMS;
6408 psa_key_derivation_operation_t derivation =
6409 PSA_KEY_DERIVATION_OPERATION_INIT;
6410
Gilles Peskine449bd832023-01-11 14:50:10 +01006411 MBEDTLS_SSL_DEBUG_MSG(2, ("perform PSA-based PMS KDF for ECJPAKE"));
Neil Armstrongca7d5062022-05-31 14:43:23 +02006412
6413 handshake->pmslen = PSA_TLS12_ECJPAKE_TO_PMS_DATA_SIZE;
6414
Gilles Peskine449bd832023-01-11 14:50:10 +01006415 status = psa_key_derivation_setup(&derivation, alg);
6416 if (status != PSA_SUCCESS) {
6417 return MBEDTLS_ERR_SSL_HW_ACCEL_FAILED;
Neil Armstrongca7d5062022-05-31 14:43:23 +02006418 }
6419
Gilles Peskine449bd832023-01-11 14:50:10 +01006420 status = psa_key_derivation_set_capacity(&derivation,
6421 PSA_TLS12_ECJPAKE_TO_PMS_DATA_SIZE);
6422 if (status != PSA_SUCCESS) {
6423 psa_key_derivation_abort(&derivation);
6424 return MBEDTLS_ERR_SSL_HW_ACCEL_FAILED;
Neil Armstrongca7d5062022-05-31 14:43:23 +02006425 }
6426
Gilles Peskine449bd832023-01-11 14:50:10 +01006427 status = psa_pake_get_implicit_key(&handshake->psa_pake_ctx,
6428 &derivation);
6429 if (status != PSA_SUCCESS) {
6430 psa_key_derivation_abort(&derivation);
6431 return MBEDTLS_ERR_SSL_HW_ACCEL_FAILED;
Neil Armstrongca7d5062022-05-31 14:43:23 +02006432 }
6433
Gilles Peskine449bd832023-01-11 14:50:10 +01006434 status = psa_key_derivation_output_bytes(&derivation,
6435 handshake->premaster,
6436 handshake->pmslen);
6437 if (status != PSA_SUCCESS) {
6438 psa_key_derivation_abort(&derivation);
6439 return MBEDTLS_ERR_SSL_HW_ACCEL_FAILED;
6440 }
6441
6442 status = psa_key_derivation_abort(&derivation);
6443 if (status != PSA_SUCCESS) {
6444 return MBEDTLS_ERR_SSL_HW_ACCEL_FAILED;
Neil Armstrongca7d5062022-05-31 14:43:23 +02006445 }
6446 }
6447#endif
Gilles Peskine449bd832023-01-11 14:50:10 +01006448 ret = handshake->tls_prf(handshake->premaster, handshake->pmslen,
6449 lbl, seed, seed_len,
6450 master,
6451 master_secret_len);
6452 if (ret != 0) {
6453 MBEDTLS_SSL_DEBUG_RET(1, "prf", ret);
6454 return ret;
Jerry Yu2a7b5ac2022-02-17 14:07:00 +08006455 }
6456
Gilles Peskine449bd832023-01-11 14:50:10 +01006457 MBEDTLS_SSL_DEBUG_BUF(3, "premaster secret",
6458 handshake->premaster,
6459 handshake->pmslen);
Jerry Yu2a7b5ac2022-02-17 14:07:00 +08006460
Gilles Peskine449bd832023-01-11 14:50:10 +01006461 mbedtls_platform_zeroize(handshake->premaster,
6462 sizeof(handshake->premaster));
Jerry Yu2a7b5ac2022-02-17 14:07:00 +08006463 }
6464
Gilles Peskine449bd832023-01-11 14:50:10 +01006465 return 0;
Jerry Yu2a7b5ac2022-02-17 14:07:00 +08006466}
6467
Gilles Peskine449bd832023-01-11 14:50:10 +01006468int mbedtls_ssl_derive_keys(mbedtls_ssl_context *ssl)
Jerry Yud62f87e2022-02-17 14:09:02 +08006469{
6470 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
6471 const mbedtls_ssl_ciphersuite_t * const ciphersuite_info =
6472 ssl->handshake->ciphersuite_info;
6473
Gilles Peskine449bd832023-01-11 14:50:10 +01006474 MBEDTLS_SSL_DEBUG_MSG(2, ("=> derive keys"));
Jerry Yud62f87e2022-02-17 14:09:02 +08006475
6476 /* Set PRF, calc_verify and calc_finished function pointers */
Gilles Peskine449bd832023-01-11 14:50:10 +01006477 ret = ssl_set_handshake_prfs(ssl->handshake,
6478 ciphersuite_info->mac);
6479 if (ret != 0) {
6480 MBEDTLS_SSL_DEBUG_RET(1, "ssl_set_handshake_prfs", ret);
6481 return ret;
Jerry Yud62f87e2022-02-17 14:09:02 +08006482 }
6483
6484 /* Compute master secret if needed */
Gilles Peskine449bd832023-01-11 14:50:10 +01006485 ret = ssl_compute_master(ssl->handshake,
6486 ssl->session_negotiate->master,
6487 ssl);
6488 if (ret != 0) {
6489 MBEDTLS_SSL_DEBUG_RET(1, "ssl_compute_master", ret);
6490 return ret;
Jerry Yud62f87e2022-02-17 14:09:02 +08006491 }
6492
6493 /* Swap the client and server random values:
6494 * - MS derivation wanted client+server (RFC 5246 8.1)
6495 * - key derivation wants server+client (RFC 5246 6.3) */
6496 {
6497 unsigned char tmp[64];
Gilles Peskine449bd832023-01-11 14:50:10 +01006498 memcpy(tmp, ssl->handshake->randbytes, 64);
6499 memcpy(ssl->handshake->randbytes, tmp + 32, 32);
6500 memcpy(ssl->handshake->randbytes + 32, tmp, 32);
6501 mbedtls_platform_zeroize(tmp, sizeof(tmp));
Jerry Yud62f87e2022-02-17 14:09:02 +08006502 }
6503
6504 /* Populate transform structure */
Gilles Peskine449bd832023-01-11 14:50:10 +01006505 ret = ssl_tls12_populate_transform(ssl->transform_negotiate,
6506 ssl->session_negotiate->ciphersuite,
6507 ssl->session_negotiate->master,
Neil Armstrongf2c82f02022-04-05 11:16:53 +02006508#if defined(MBEDTLS_SSL_SOME_SUITES_USE_CBC_ETM)
Gilles Peskine449bd832023-01-11 14:50:10 +01006509 ssl->session_negotiate->encrypt_then_mac,
Neil Armstrongf2c82f02022-04-05 11:16:53 +02006510#endif /* MBEDTLS_SSL_SOME_SUITES_USE_CBC_ETM */
Gilles Peskine449bd832023-01-11 14:50:10 +01006511 ssl->handshake->tls_prf,
6512 ssl->handshake->randbytes,
6513 ssl->tls_version,
6514 ssl->conf->endpoint,
6515 ssl);
6516 if (ret != 0) {
6517 MBEDTLS_SSL_DEBUG_RET(1, "ssl_tls12_populate_transform", ret);
6518 return ret;
Jerry Yud62f87e2022-02-17 14:09:02 +08006519 }
6520
6521 /* We no longer need Server/ClientHello.random values */
Gilles Peskine449bd832023-01-11 14:50:10 +01006522 mbedtls_platform_zeroize(ssl->handshake->randbytes,
6523 sizeof(ssl->handshake->randbytes));
Jerry Yud62f87e2022-02-17 14:09:02 +08006524
Gilles Peskine449bd832023-01-11 14:50:10 +01006525 MBEDTLS_SSL_DEBUG_MSG(2, ("<= derive keys"));
Jerry Yud62f87e2022-02-17 14:09:02 +08006526
Gilles Peskine449bd832023-01-11 14:50:10 +01006527 return 0;
Jerry Yud62f87e2022-02-17 14:09:02 +08006528}
Jerry Yu8392e0d2022-02-17 14:10:24 +08006529
Gilles Peskine449bd832023-01-11 14:50:10 +01006530int mbedtls_ssl_set_calc_verify_md(mbedtls_ssl_context *ssl, int md)
Ronald Cron4dcbca92022-03-07 10:21:40 +01006531{
Gilles Peskine449bd832023-01-11 14:50:10 +01006532 switch (md) {
Andrzej Kurek25f27152022-08-17 16:09:31 -04006533#if defined(MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Ronald Cron4dcbca92022-03-07 10:21:40 +01006534 case MBEDTLS_SSL_HASH_SHA384:
6535 ssl->handshake->calc_verify = ssl_calc_verify_tls_sha384;
6536 break;
6537#endif
Andrzej Kurek25f27152022-08-17 16:09:31 -04006538#if defined(MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Ronald Cron4dcbca92022-03-07 10:21:40 +01006539 case MBEDTLS_SSL_HASH_SHA256:
6540 ssl->handshake->calc_verify = ssl_calc_verify_tls_sha256;
6541 break;
6542#endif
6543 default:
Gilles Peskine449bd832023-01-11 14:50:10 +01006544 return -1;
Ronald Cron4dcbca92022-03-07 10:21:40 +01006545 }
Andrzej Kurekeabeb302022-10-17 07:52:51 -04006546#if !defined(MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA) && \
6547 !defined(MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
6548 (void) ssl;
6549#endif
Gilles Peskine449bd832023-01-11 14:50:10 +01006550 return 0;
Ronald Cron4dcbca92022-03-07 10:21:40 +01006551}
6552
Andrzej Kurek25f27152022-08-17 16:09:31 -04006553#if defined(MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Manuel Pégourié-Gonnard226aa152023-02-05 09:46:59 +01006554int ssl_calc_verify_tls_sha256(const mbedtls_ssl_context *ssl,
6555 unsigned char *hash,
6556 size_t *hlen)
Jerry Yu8392e0d2022-02-17 14:10:24 +08006557{
6558#if defined(MBEDTLS_USE_PSA_CRYPTO)
6559 size_t hash_size;
6560 psa_status_t status;
6561 psa_hash_operation_t sha256_psa = psa_hash_operation_init();
6562
Gilles Peskine449bd832023-01-11 14:50:10 +01006563 MBEDTLS_SSL_DEBUG_MSG(2, ("=> PSA calc verify sha256"));
6564 status = psa_hash_clone(&ssl->handshake->fin_sha256_psa, &sha256_psa);
6565 if (status != PSA_SUCCESS) {
6566 MBEDTLS_SSL_DEBUG_MSG(2, ("PSA hash clone failed"));
Manuel Pégourié-Gonnard226aa152023-02-05 09:46:59 +01006567 return 0;
Jerry Yu8392e0d2022-02-17 14:10:24 +08006568 }
6569
Gilles Peskine449bd832023-01-11 14:50:10 +01006570 status = psa_hash_finish(&sha256_psa, hash, 32, &hash_size);
6571 if (status != PSA_SUCCESS) {
6572 MBEDTLS_SSL_DEBUG_MSG(2, ("PSA hash finish failed"));
Manuel Pégourié-Gonnard226aa152023-02-05 09:46:59 +01006573 return 0;
Jerry Yu8392e0d2022-02-17 14:10:24 +08006574 }
6575
6576 *hlen = 32;
Gilles Peskine449bd832023-01-11 14:50:10 +01006577 MBEDTLS_SSL_DEBUG_BUF(3, "PSA calculated verify result", hash, *hlen);
6578 MBEDTLS_SSL_DEBUG_MSG(2, ("<= PSA calc verify"));
Jerry Yu8392e0d2022-02-17 14:10:24 +08006579#else
6580 mbedtls_sha256_context sha256;
6581
Gilles Peskine449bd832023-01-11 14:50:10 +01006582 mbedtls_sha256_init(&sha256);
Jerry Yu8392e0d2022-02-17 14:10:24 +08006583
Gilles Peskine449bd832023-01-11 14:50:10 +01006584 MBEDTLS_SSL_DEBUG_MSG(2, ("=> calc verify sha256"));
Jerry Yu8392e0d2022-02-17 14:10:24 +08006585
Gilles Peskine449bd832023-01-11 14:50:10 +01006586 mbedtls_sha256_clone(&sha256, &ssl->handshake->fin_sha256);
6587 mbedtls_sha256_finish(&sha256, hash);
Jerry Yu8392e0d2022-02-17 14:10:24 +08006588
6589 *hlen = 32;
6590
Gilles Peskine449bd832023-01-11 14:50:10 +01006591 MBEDTLS_SSL_DEBUG_BUF(3, "calculated verify result", hash, *hlen);
6592 MBEDTLS_SSL_DEBUG_MSG(2, ("<= calc verify"));
Jerry Yu8392e0d2022-02-17 14:10:24 +08006593
Gilles Peskine449bd832023-01-11 14:50:10 +01006594 mbedtls_sha256_free(&sha256);
Jerry Yu8392e0d2022-02-17 14:10:24 +08006595#endif /* MBEDTLS_USE_PSA_CRYPTO */
Manuel Pégourié-Gonnard226aa152023-02-05 09:46:59 +01006596 return 0;
Jerry Yu8392e0d2022-02-17 14:10:24 +08006597}
Andrzej Kurek25f27152022-08-17 16:09:31 -04006598#endif /* MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA */
Jerry Yu8392e0d2022-02-17 14:10:24 +08006599
Andrzej Kurek25f27152022-08-17 16:09:31 -04006600#if defined(MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Manuel Pégourié-Gonnard226aa152023-02-05 09:46:59 +01006601int ssl_calc_verify_tls_sha384(const mbedtls_ssl_context *ssl,
6602 unsigned char *hash,
6603 size_t *hlen)
Jerry Yuc1cb3842022-02-17 14:13:48 +08006604{
6605#if defined(MBEDTLS_USE_PSA_CRYPTO)
6606 size_t hash_size;
6607 psa_status_t status;
6608 psa_hash_operation_t sha384_psa = psa_hash_operation_init();
6609
Gilles Peskine449bd832023-01-11 14:50:10 +01006610 MBEDTLS_SSL_DEBUG_MSG(2, ("=> PSA calc verify sha384"));
6611 status = psa_hash_clone(&ssl->handshake->fin_sha384_psa, &sha384_psa);
6612 if (status != PSA_SUCCESS) {
6613 MBEDTLS_SSL_DEBUG_MSG(2, ("PSA hash clone failed"));
Manuel Pégourié-Gonnard226aa152023-02-05 09:46:59 +01006614 return 0;
Jerry Yuc1cb3842022-02-17 14:13:48 +08006615 }
6616
Gilles Peskine449bd832023-01-11 14:50:10 +01006617 status = psa_hash_finish(&sha384_psa, hash, 48, &hash_size);
6618 if (status != PSA_SUCCESS) {
6619 MBEDTLS_SSL_DEBUG_MSG(2, ("PSA hash finish failed"));
Manuel Pégourié-Gonnard226aa152023-02-05 09:46:59 +01006620 return 0;
Jerry Yuc1cb3842022-02-17 14:13:48 +08006621 }
6622
6623 *hlen = 48;
Gilles Peskine449bd832023-01-11 14:50:10 +01006624 MBEDTLS_SSL_DEBUG_BUF(3, "PSA calculated verify result", hash, *hlen);
6625 MBEDTLS_SSL_DEBUG_MSG(2, ("<= PSA calc verify"));
Jerry Yuc1cb3842022-02-17 14:13:48 +08006626#else
6627 mbedtls_sha512_context sha512;
6628
Gilles Peskine449bd832023-01-11 14:50:10 +01006629 mbedtls_sha512_init(&sha512);
Jerry Yuc1cb3842022-02-17 14:13:48 +08006630
Gilles Peskine449bd832023-01-11 14:50:10 +01006631 MBEDTLS_SSL_DEBUG_MSG(2, ("=> calc verify sha384"));
Jerry Yuc1cb3842022-02-17 14:13:48 +08006632
Gilles Peskine449bd832023-01-11 14:50:10 +01006633 mbedtls_sha512_clone(&sha512, &ssl->handshake->fin_sha384);
6634 mbedtls_sha512_finish(&sha512, hash);
Jerry Yuc1cb3842022-02-17 14:13:48 +08006635
6636 *hlen = 48;
6637
Gilles Peskine449bd832023-01-11 14:50:10 +01006638 MBEDTLS_SSL_DEBUG_BUF(3, "calculated verify result", hash, *hlen);
6639 MBEDTLS_SSL_DEBUG_MSG(2, ("<= calc verify"));
Jerry Yuc1cb3842022-02-17 14:13:48 +08006640
Gilles Peskine449bd832023-01-11 14:50:10 +01006641 mbedtls_sha512_free(&sha512);
Jerry Yuc1cb3842022-02-17 14:13:48 +08006642#endif /* MBEDTLS_USE_PSA_CRYPTO */
Manuel Pégourié-Gonnard226aa152023-02-05 09:46:59 +01006643 return 0;
Jerry Yuc1cb3842022-02-17 14:13:48 +08006644}
Andrzej Kurek25f27152022-08-17 16:09:31 -04006645#endif /* MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA */
Jerry Yuc1cb3842022-02-17 14:13:48 +08006646
Neil Armstrong80f6f322022-05-03 17:56:38 +02006647#if !defined(MBEDTLS_USE_PSA_CRYPTO) && \
6648 defined(MBEDTLS_KEY_EXCHANGE_SOME_PSK_ENABLED)
Gilles Peskine449bd832023-01-11 14:50:10 +01006649int mbedtls_ssl_psk_derive_premaster(mbedtls_ssl_context *ssl, mbedtls_key_exchange_type_t key_ex)
Jerry Yuce3dca42022-02-17 14:16:37 +08006650{
6651 unsigned char *p = ssl->handshake->premaster;
Gilles Peskine449bd832023-01-11 14:50:10 +01006652 unsigned char *end = p + sizeof(ssl->handshake->premaster);
Jerry Yuce3dca42022-02-17 14:16:37 +08006653 const unsigned char *psk = NULL;
6654 size_t psk_len = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +01006655 int psk_ret = mbedtls_ssl_get_psk(ssl, &psk, &psk_len);
Jerry Yuce3dca42022-02-17 14:16:37 +08006656
Gilles Peskine449bd832023-01-11 14:50:10 +01006657 if (psk_ret == MBEDTLS_ERR_SSL_PRIVATE_KEY_REQUIRED) {
Jerry Yuce3dca42022-02-17 14:16:37 +08006658 /*
6659 * This should never happen because the existence of a PSK is always
Przemek Stekielb293aaa2022-04-19 12:22:38 +02006660 * checked before calling this function.
6661 *
6662 * The exception is opaque DHE-PSK. For DHE-PSK fill premaster with
Przemek Stekiel8abcee92022-04-28 09:16:28 +02006663 * the shared secret without PSK.
Jerry Yuce3dca42022-02-17 14:16:37 +08006664 */
Gilles Peskine449bd832023-01-11 14:50:10 +01006665 if (key_ex != MBEDTLS_KEY_EXCHANGE_DHE_PSK) {
6666 MBEDTLS_SSL_DEBUG_MSG(1, ("should never happen"));
6667 return MBEDTLS_ERR_SSL_INTERNAL_ERROR;
Przemek Stekielb293aaa2022-04-19 12:22:38 +02006668 }
Jerry Yuce3dca42022-02-17 14:16:37 +08006669 }
6670
6671 /*
6672 * PMS = struct {
6673 * opaque other_secret<0..2^16-1>;
6674 * opaque psk<0..2^16-1>;
6675 * };
6676 * with "other_secret" depending on the particular key exchange
6677 */
6678#if defined(MBEDTLS_KEY_EXCHANGE_PSK_ENABLED)
Gilles Peskine449bd832023-01-11 14:50:10 +01006679 if (key_ex == MBEDTLS_KEY_EXCHANGE_PSK) {
6680 if (end - p < 2) {
6681 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
6682 }
Jerry Yuce3dca42022-02-17 14:16:37 +08006683
Gilles Peskine449bd832023-01-11 14:50:10 +01006684 MBEDTLS_PUT_UINT16_BE(psk_len, p, 0);
Jerry Yuce3dca42022-02-17 14:16:37 +08006685 p += 2;
6686
Gilles Peskine449bd832023-01-11 14:50:10 +01006687 if (end < p || (size_t) (end - p) < psk_len) {
6688 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
6689 }
Jerry Yuce3dca42022-02-17 14:16:37 +08006690
Gilles Peskine449bd832023-01-11 14:50:10 +01006691 memset(p, 0, psk_len);
Jerry Yuce3dca42022-02-17 14:16:37 +08006692 p += psk_len;
Gilles Peskine449bd832023-01-11 14:50:10 +01006693 } else
Jerry Yuce3dca42022-02-17 14:16:37 +08006694#endif /* MBEDTLS_KEY_EXCHANGE_PSK_ENABLED */
6695#if defined(MBEDTLS_KEY_EXCHANGE_RSA_PSK_ENABLED)
Gilles Peskine449bd832023-01-11 14:50:10 +01006696 if (key_ex == MBEDTLS_KEY_EXCHANGE_RSA_PSK) {
Jerry Yuce3dca42022-02-17 14:16:37 +08006697 /*
6698 * other_secret already set by the ClientKeyExchange message,
6699 * and is 48 bytes long
6700 */
Gilles Peskine449bd832023-01-11 14:50:10 +01006701 if (end - p < 2) {
6702 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
6703 }
Jerry Yuce3dca42022-02-17 14:16:37 +08006704
6705 *p++ = 0;
6706 *p++ = 48;
6707 p += 48;
Gilles Peskine449bd832023-01-11 14:50:10 +01006708 } else
Jerry Yuce3dca42022-02-17 14:16:37 +08006709#endif /* MBEDTLS_KEY_EXCHANGE_RSA_PSK_ENABLED */
6710#if defined(MBEDTLS_KEY_EXCHANGE_DHE_PSK_ENABLED)
Gilles Peskine449bd832023-01-11 14:50:10 +01006711 if (key_ex == MBEDTLS_KEY_EXCHANGE_DHE_PSK) {
Jerry Yuce3dca42022-02-17 14:16:37 +08006712 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
6713 size_t len;
6714
6715 /* Write length only when we know the actual value */
Gilles Peskine449bd832023-01-11 14:50:10 +01006716 if ((ret = mbedtls_dhm_calc_secret(&ssl->handshake->dhm_ctx,
6717 p + 2, end - (p + 2), &len,
6718 ssl->conf->f_rng, ssl->conf->p_rng)) != 0) {
6719 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_dhm_calc_secret", ret);
6720 return ret;
Jerry Yuce3dca42022-02-17 14:16:37 +08006721 }
Gilles Peskine449bd832023-01-11 14:50:10 +01006722 MBEDTLS_PUT_UINT16_BE(len, p, 0);
Jerry Yuce3dca42022-02-17 14:16:37 +08006723 p += 2 + len;
6724
Gilles Peskine449bd832023-01-11 14:50:10 +01006725 MBEDTLS_SSL_DEBUG_MPI(3, "DHM: K ", &ssl->handshake->dhm_ctx.K);
6726 } else
Jerry Yuce3dca42022-02-17 14:16:37 +08006727#endif /* MBEDTLS_KEY_EXCHANGE_DHE_PSK_ENABLED */
Neil Armstrong80f6f322022-05-03 17:56:38 +02006728#if defined(MBEDTLS_KEY_EXCHANGE_ECDHE_PSK_ENABLED)
Gilles Peskine449bd832023-01-11 14:50:10 +01006729 if (key_ex == MBEDTLS_KEY_EXCHANGE_ECDHE_PSK) {
Jerry Yuce3dca42022-02-17 14:16:37 +08006730 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
6731 size_t zlen;
6732
Gilles Peskine449bd832023-01-11 14:50:10 +01006733 if ((ret = mbedtls_ecdh_calc_secret(&ssl->handshake->ecdh_ctx, &zlen,
6734 p + 2, end - (p + 2),
6735 ssl->conf->f_rng, ssl->conf->p_rng)) != 0) {
6736 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ecdh_calc_secret", ret);
6737 return ret;
Jerry Yuce3dca42022-02-17 14:16:37 +08006738 }
6739
Gilles Peskine449bd832023-01-11 14:50:10 +01006740 MBEDTLS_PUT_UINT16_BE(zlen, p, 0);
Jerry Yuce3dca42022-02-17 14:16:37 +08006741 p += 2 + zlen;
6742
Gilles Peskine449bd832023-01-11 14:50:10 +01006743 MBEDTLS_SSL_DEBUG_ECDH(3, &ssl->handshake->ecdh_ctx,
6744 MBEDTLS_DEBUG_ECDH_Z);
6745 } else
Neil Armstrong80f6f322022-05-03 17:56:38 +02006746#endif /* MBEDTLS_KEY_EXCHANGE_ECDHE_PSK_ENABLED */
Jerry Yuce3dca42022-02-17 14:16:37 +08006747 {
Gilles Peskine449bd832023-01-11 14:50:10 +01006748 MBEDTLS_SSL_DEBUG_MSG(1, ("should never happen"));
6749 return MBEDTLS_ERR_SSL_INTERNAL_ERROR;
Jerry Yuce3dca42022-02-17 14:16:37 +08006750 }
6751
6752 /* opaque psk<0..2^16-1>; */
Gilles Peskine449bd832023-01-11 14:50:10 +01006753 if (end - p < 2) {
6754 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
6755 }
Jerry Yuce3dca42022-02-17 14:16:37 +08006756
Gilles Peskine449bd832023-01-11 14:50:10 +01006757 MBEDTLS_PUT_UINT16_BE(psk_len, p, 0);
Jerry Yuce3dca42022-02-17 14:16:37 +08006758 p += 2;
6759
Gilles Peskine449bd832023-01-11 14:50:10 +01006760 if (end < p || (size_t) (end - p) < psk_len) {
6761 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
6762 }
Jerry Yuce3dca42022-02-17 14:16:37 +08006763
Gilles Peskine449bd832023-01-11 14:50:10 +01006764 memcpy(p, psk, psk_len);
Jerry Yuce3dca42022-02-17 14:16:37 +08006765 p += psk_len;
6766
6767 ssl->handshake->pmslen = p - ssl->handshake->premaster;
6768
Gilles Peskine449bd832023-01-11 14:50:10 +01006769 return 0;
Jerry Yuce3dca42022-02-17 14:16:37 +08006770}
Neil Armstrong80f6f322022-05-03 17:56:38 +02006771#endif /* !MBEDTLS_USE_PSA_CRYPTO && MBEDTLS_KEY_EXCHANGE_SOME_PSK_ENABLED */
Jerry Yuc2c673d2022-02-17 14:20:39 +08006772
6773#if defined(MBEDTLS_SSL_SRV_C) && defined(MBEDTLS_SSL_RENEGOTIATION)
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02006774MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01006775static int ssl_write_hello_request(mbedtls_ssl_context *ssl);
Jerry Yuc2c673d2022-02-17 14:20:39 +08006776
6777#if defined(MBEDTLS_SSL_PROTO_DTLS)
Gilles Peskine449bd832023-01-11 14:50:10 +01006778int mbedtls_ssl_resend_hello_request(mbedtls_ssl_context *ssl)
Jerry Yuc2c673d2022-02-17 14:20:39 +08006779{
6780 /* If renegotiation is not enforced, retransmit until we would reach max
6781 * timeout if we were using the usual handshake doubling scheme */
Gilles Peskine449bd832023-01-11 14:50:10 +01006782 if (ssl->conf->renego_max_records < 0) {
Jerry Yuc2c673d2022-02-17 14:20:39 +08006783 uint32_t ratio = ssl->conf->hs_timeout_max / ssl->conf->hs_timeout_min + 1;
6784 unsigned char doublings = 1;
6785
Gilles Peskine449bd832023-01-11 14:50:10 +01006786 while (ratio != 0) {
Jerry Yuc2c673d2022-02-17 14:20:39 +08006787 ++doublings;
6788 ratio >>= 1;
6789 }
6790
Gilles Peskine449bd832023-01-11 14:50:10 +01006791 if (++ssl->renego_records_seen > doublings) {
6792 MBEDTLS_SSL_DEBUG_MSG(2, ("no longer retransmitting hello request"));
6793 return 0;
Jerry Yuc2c673d2022-02-17 14:20:39 +08006794 }
6795 }
6796
Gilles Peskine449bd832023-01-11 14:50:10 +01006797 return ssl_write_hello_request(ssl);
Jerry Yuc2c673d2022-02-17 14:20:39 +08006798}
6799#endif
6800#endif /* MBEDTLS_SSL_SRV_C && MBEDTLS_SSL_RENEGOTIATION */
Jerry Yud9526692022-02-17 14:23:47 +08006801
Jerry Yud9526692022-02-17 14:23:47 +08006802/*
6803 * Handshake functions
6804 */
6805#if !defined(MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED)
6806/* No certificate support -> dummy functions */
Gilles Peskine449bd832023-01-11 14:50:10 +01006807int mbedtls_ssl_write_certificate(mbedtls_ssl_context *ssl)
Jerry Yud9526692022-02-17 14:23:47 +08006808{
6809 const mbedtls_ssl_ciphersuite_t *ciphersuite_info =
6810 ssl->handshake->ciphersuite_info;
6811
Gilles Peskine449bd832023-01-11 14:50:10 +01006812 MBEDTLS_SSL_DEBUG_MSG(2, ("=> write certificate"));
Jerry Yud9526692022-02-17 14:23:47 +08006813
Gilles Peskine449bd832023-01-11 14:50:10 +01006814 if (!mbedtls_ssl_ciphersuite_uses_srv_cert(ciphersuite_info)) {
6815 MBEDTLS_SSL_DEBUG_MSG(2, ("<= skip write certificate"));
Jerry Yud9526692022-02-17 14:23:47 +08006816 ssl->state++;
Gilles Peskine449bd832023-01-11 14:50:10 +01006817 return 0;
Jerry Yud9526692022-02-17 14:23:47 +08006818 }
6819
Gilles Peskine449bd832023-01-11 14:50:10 +01006820 MBEDTLS_SSL_DEBUG_MSG(1, ("should never happen"));
6821 return MBEDTLS_ERR_SSL_INTERNAL_ERROR;
Jerry Yud9526692022-02-17 14:23:47 +08006822}
6823
Gilles Peskine449bd832023-01-11 14:50:10 +01006824int mbedtls_ssl_parse_certificate(mbedtls_ssl_context *ssl)
Jerry Yud9526692022-02-17 14:23:47 +08006825{
6826 const mbedtls_ssl_ciphersuite_t *ciphersuite_info =
6827 ssl->handshake->ciphersuite_info;
6828
Gilles Peskine449bd832023-01-11 14:50:10 +01006829 MBEDTLS_SSL_DEBUG_MSG(2, ("=> parse certificate"));
Jerry Yud9526692022-02-17 14:23:47 +08006830
Gilles Peskine449bd832023-01-11 14:50:10 +01006831 if (!mbedtls_ssl_ciphersuite_uses_srv_cert(ciphersuite_info)) {
6832 MBEDTLS_SSL_DEBUG_MSG(2, ("<= skip parse certificate"));
Jerry Yud9526692022-02-17 14:23:47 +08006833 ssl->state++;
Gilles Peskine449bd832023-01-11 14:50:10 +01006834 return 0;
Jerry Yud9526692022-02-17 14:23:47 +08006835 }
6836
Gilles Peskine449bd832023-01-11 14:50:10 +01006837 MBEDTLS_SSL_DEBUG_MSG(1, ("should never happen"));
6838 return MBEDTLS_ERR_SSL_INTERNAL_ERROR;
Jerry Yud9526692022-02-17 14:23:47 +08006839}
6840
6841#else /* MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED */
6842/* Some certificate support -> implement write and parse */
6843
Gilles Peskine449bd832023-01-11 14:50:10 +01006844int mbedtls_ssl_write_certificate(mbedtls_ssl_context *ssl)
Jerry Yud9526692022-02-17 14:23:47 +08006845{
6846 int ret = MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
6847 size_t i, n;
6848 const mbedtls_x509_crt *crt;
6849 const mbedtls_ssl_ciphersuite_t *ciphersuite_info =
6850 ssl->handshake->ciphersuite_info;
6851
Gilles Peskine449bd832023-01-11 14:50:10 +01006852 MBEDTLS_SSL_DEBUG_MSG(2, ("=> write certificate"));
Jerry Yud9526692022-02-17 14:23:47 +08006853
Gilles Peskine449bd832023-01-11 14:50:10 +01006854 if (!mbedtls_ssl_ciphersuite_uses_srv_cert(ciphersuite_info)) {
6855 MBEDTLS_SSL_DEBUG_MSG(2, ("<= skip write certificate"));
Jerry Yud9526692022-02-17 14:23:47 +08006856 ssl->state++;
Gilles Peskine449bd832023-01-11 14:50:10 +01006857 return 0;
Jerry Yud9526692022-02-17 14:23:47 +08006858 }
6859
6860#if defined(MBEDTLS_SSL_CLI_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01006861 if (ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT) {
6862 if (ssl->handshake->client_auth == 0) {
6863 MBEDTLS_SSL_DEBUG_MSG(2, ("<= skip write certificate"));
Jerry Yud9526692022-02-17 14:23:47 +08006864 ssl->state++;
Gilles Peskine449bd832023-01-11 14:50:10 +01006865 return 0;
Jerry Yud9526692022-02-17 14:23:47 +08006866 }
6867 }
6868#endif /* MBEDTLS_SSL_CLI_C */
6869#if defined(MBEDTLS_SSL_SRV_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01006870 if (ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER) {
6871 if (mbedtls_ssl_own_cert(ssl) == NULL) {
Jerry Yud9526692022-02-17 14:23:47 +08006872 /* Should never happen because we shouldn't have picked the
6873 * ciphersuite if we don't have a certificate. */
Gilles Peskine449bd832023-01-11 14:50:10 +01006874 return MBEDTLS_ERR_SSL_INTERNAL_ERROR;
Jerry Yud9526692022-02-17 14:23:47 +08006875 }
6876 }
6877#endif
6878
Gilles Peskine449bd832023-01-11 14:50:10 +01006879 MBEDTLS_SSL_DEBUG_CRT(3, "own certificate", mbedtls_ssl_own_cert(ssl));
Jerry Yud9526692022-02-17 14:23:47 +08006880
6881 /*
6882 * 0 . 0 handshake type
6883 * 1 . 3 handshake length
6884 * 4 . 6 length of all certs
6885 * 7 . 9 length of cert. 1
6886 * 10 . n-1 peer certificate
6887 * n . n+2 length of cert. 2
6888 * n+3 . ... upper level cert, etc.
6889 */
6890 i = 7;
Gilles Peskine449bd832023-01-11 14:50:10 +01006891 crt = mbedtls_ssl_own_cert(ssl);
Jerry Yud9526692022-02-17 14:23:47 +08006892
Gilles Peskine449bd832023-01-11 14:50:10 +01006893 while (crt != NULL) {
Jerry Yud9526692022-02-17 14:23:47 +08006894 n = crt->raw.len;
Gilles Peskine449bd832023-01-11 14:50:10 +01006895 if (n > MBEDTLS_SSL_OUT_CONTENT_LEN - 3 - i) {
6896 MBEDTLS_SSL_DEBUG_MSG(1, ("certificate too large, %" MBEDTLS_PRINTF_SIZET
6897 " > %" MBEDTLS_PRINTF_SIZET,
6898 i + 3 + n, (size_t) MBEDTLS_SSL_OUT_CONTENT_LEN));
6899 return MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL;
Jerry Yud9526692022-02-17 14:23:47 +08006900 }
6901
Gilles Peskine449bd832023-01-11 14:50:10 +01006902 ssl->out_msg[i] = MBEDTLS_BYTE_2(n);
6903 ssl->out_msg[i + 1] = MBEDTLS_BYTE_1(n);
6904 ssl->out_msg[i + 2] = MBEDTLS_BYTE_0(n);
Jerry Yud9526692022-02-17 14:23:47 +08006905
Gilles Peskine449bd832023-01-11 14:50:10 +01006906 i += 3; memcpy(ssl->out_msg + i, crt->raw.p, n);
Jerry Yud9526692022-02-17 14:23:47 +08006907 i += n; crt = crt->next;
6908 }
6909
Gilles Peskine449bd832023-01-11 14:50:10 +01006910 ssl->out_msg[4] = MBEDTLS_BYTE_2(i - 7);
6911 ssl->out_msg[5] = MBEDTLS_BYTE_1(i - 7);
6912 ssl->out_msg[6] = MBEDTLS_BYTE_0(i - 7);
Jerry Yud9526692022-02-17 14:23:47 +08006913
6914 ssl->out_msglen = i;
6915 ssl->out_msgtype = MBEDTLS_SSL_MSG_HANDSHAKE;
6916 ssl->out_msg[0] = MBEDTLS_SSL_HS_CERTIFICATE;
6917
6918 ssl->state++;
6919
Gilles Peskine449bd832023-01-11 14:50:10 +01006920 if ((ret = mbedtls_ssl_write_handshake_msg(ssl)) != 0) {
6921 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ssl_write_handshake_msg", ret);
6922 return ret;
Jerry Yud9526692022-02-17 14:23:47 +08006923 }
6924
Gilles Peskine449bd832023-01-11 14:50:10 +01006925 MBEDTLS_SSL_DEBUG_MSG(2, ("<= write certificate"));
Jerry Yud9526692022-02-17 14:23:47 +08006926
Gilles Peskine449bd832023-01-11 14:50:10 +01006927 return ret;
Jerry Yud9526692022-02-17 14:23:47 +08006928}
6929
6930#if defined(MBEDTLS_SSL_RENEGOTIATION) && defined(MBEDTLS_SSL_CLI_C)
6931
6932#if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02006933MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01006934static int ssl_check_peer_crt_unchanged(mbedtls_ssl_context *ssl,
6935 unsigned char *crt_buf,
6936 size_t crt_buf_len)
Jerry Yud9526692022-02-17 14:23:47 +08006937{
6938 mbedtls_x509_crt const * const peer_crt = ssl->session->peer_cert;
6939
Gilles Peskine449bd832023-01-11 14:50:10 +01006940 if (peer_crt == NULL) {
6941 return -1;
6942 }
Jerry Yud9526692022-02-17 14:23:47 +08006943
Gilles Peskine449bd832023-01-11 14:50:10 +01006944 if (peer_crt->raw.len != crt_buf_len) {
6945 return -1;
6946 }
Jerry Yud9526692022-02-17 14:23:47 +08006947
Gilles Peskine449bd832023-01-11 14:50:10 +01006948 return memcmp(peer_crt->raw.p, crt_buf, peer_crt->raw.len);
Jerry Yud9526692022-02-17 14:23:47 +08006949}
6950#else /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02006951MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01006952static int ssl_check_peer_crt_unchanged(mbedtls_ssl_context *ssl,
6953 unsigned char *crt_buf,
6954 size_t crt_buf_len)
Jerry Yud9526692022-02-17 14:23:47 +08006955{
6956 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
6957 unsigned char const * const peer_cert_digest =
6958 ssl->session->peer_cert_digest;
6959 mbedtls_md_type_t const peer_cert_digest_type =
6960 ssl->session->peer_cert_digest_type;
6961 mbedtls_md_info_t const * const digest_info =
Gilles Peskine449bd832023-01-11 14:50:10 +01006962 mbedtls_md_info_from_type(peer_cert_digest_type);
Jerry Yud9526692022-02-17 14:23:47 +08006963 unsigned char tmp_digest[MBEDTLS_SSL_PEER_CERT_DIGEST_MAX_LEN];
6964 size_t digest_len;
6965
Gilles Peskine449bd832023-01-11 14:50:10 +01006966 if (peer_cert_digest == NULL || digest_info == NULL) {
6967 return -1;
6968 }
Jerry Yud9526692022-02-17 14:23:47 +08006969
Gilles Peskine449bd832023-01-11 14:50:10 +01006970 digest_len = mbedtls_md_get_size(digest_info);
6971 if (digest_len > MBEDTLS_SSL_PEER_CERT_DIGEST_MAX_LEN) {
6972 return -1;
6973 }
Jerry Yud9526692022-02-17 14:23:47 +08006974
Gilles Peskine449bd832023-01-11 14:50:10 +01006975 ret = mbedtls_md(digest_info, crt_buf, crt_buf_len, tmp_digest);
6976 if (ret != 0) {
6977 return -1;
6978 }
Jerry Yud9526692022-02-17 14:23:47 +08006979
Gilles Peskine449bd832023-01-11 14:50:10 +01006980 return memcmp(tmp_digest, peer_cert_digest, digest_len);
Jerry Yud9526692022-02-17 14:23:47 +08006981}
6982#endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
6983#endif /* MBEDTLS_SSL_RENEGOTIATION && MBEDTLS_SSL_CLI_C */
6984
6985/*
6986 * Once the certificate message is read, parse it into a cert chain and
6987 * perform basic checks, but leave actual verification to the caller
6988 */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02006989MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01006990static int ssl_parse_certificate_chain(mbedtls_ssl_context *ssl,
6991 mbedtls_x509_crt *chain)
Jerry Yud9526692022-02-17 14:23:47 +08006992{
6993 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
6994#if defined(MBEDTLS_SSL_RENEGOTIATION) && defined(MBEDTLS_SSL_CLI_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01006995 int crt_cnt = 0;
Jerry Yud9526692022-02-17 14:23:47 +08006996#endif
6997 size_t i, n;
6998 uint8_t alert;
6999
Gilles Peskine449bd832023-01-11 14:50:10 +01007000 if (ssl->in_msgtype != MBEDTLS_SSL_MSG_HANDSHAKE) {
7001 MBEDTLS_SSL_DEBUG_MSG(1, ("bad certificate message"));
7002 mbedtls_ssl_send_alert_message(ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
7003 MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE);
7004 return MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE;
Jerry Yud9526692022-02-17 14:23:47 +08007005 }
7006
Gilles Peskine449bd832023-01-11 14:50:10 +01007007 if (ssl->in_msg[0] != MBEDTLS_SSL_HS_CERTIFICATE) {
7008 mbedtls_ssl_send_alert_message(ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
7009 MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE);
7010 return MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE;
Jerry Yud9526692022-02-17 14:23:47 +08007011 }
7012
Gilles Peskine449bd832023-01-11 14:50:10 +01007013 if (ssl->in_hslen < mbedtls_ssl_hs_hdr_len(ssl) + 3 + 3) {
7014 MBEDTLS_SSL_DEBUG_MSG(1, ("bad certificate message"));
7015 mbedtls_ssl_send_alert_message(ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
7016 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR);
7017 return MBEDTLS_ERR_SSL_DECODE_ERROR;
Jerry Yud9526692022-02-17 14:23:47 +08007018 }
7019
Gilles Peskine449bd832023-01-11 14:50:10 +01007020 i = mbedtls_ssl_hs_hdr_len(ssl);
Jerry Yud9526692022-02-17 14:23:47 +08007021
7022 /*
7023 * Same message structure as in mbedtls_ssl_write_certificate()
7024 */
Gilles Peskine449bd832023-01-11 14:50:10 +01007025 n = (ssl->in_msg[i+1] << 8) | ssl->in_msg[i+2];
Jerry Yud9526692022-02-17 14:23:47 +08007026
Gilles Peskine449bd832023-01-11 14:50:10 +01007027 if (ssl->in_msg[i] != 0 ||
7028 ssl->in_hslen != n + 3 + mbedtls_ssl_hs_hdr_len(ssl)) {
7029 MBEDTLS_SSL_DEBUG_MSG(1, ("bad certificate message"));
7030 mbedtls_ssl_send_alert_message(ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
7031 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR);
7032 return MBEDTLS_ERR_SSL_DECODE_ERROR;
Jerry Yud9526692022-02-17 14:23:47 +08007033 }
7034
7035 /* Make &ssl->in_msg[i] point to the beginning of the CRT chain. */
7036 i += 3;
7037
7038 /* Iterate through and parse the CRTs in the provided chain. */
Gilles Peskine449bd832023-01-11 14:50:10 +01007039 while (i < ssl->in_hslen) {
Jerry Yud9526692022-02-17 14:23:47 +08007040 /* Check that there's room for the next CRT's length fields. */
Gilles Peskine449bd832023-01-11 14:50:10 +01007041 if (i + 3 > ssl->in_hslen) {
7042 MBEDTLS_SSL_DEBUG_MSG(1, ("bad certificate message"));
7043 mbedtls_ssl_send_alert_message(ssl,
7044 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
7045 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR);
7046 return MBEDTLS_ERR_SSL_DECODE_ERROR;
Jerry Yud9526692022-02-17 14:23:47 +08007047 }
7048 /* In theory, the CRT can be up to 2**24 Bytes, but we don't support
7049 * anything beyond 2**16 ~ 64K. */
Gilles Peskine449bd832023-01-11 14:50:10 +01007050 if (ssl->in_msg[i] != 0) {
7051 MBEDTLS_SSL_DEBUG_MSG(1, ("bad certificate message"));
7052 mbedtls_ssl_send_alert_message(ssl,
7053 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
7054 MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT);
7055 return MBEDTLS_ERR_SSL_BAD_CERTIFICATE;
Jerry Yud9526692022-02-17 14:23:47 +08007056 }
7057
7058 /* Read length of the next CRT in the chain. */
Gilles Peskine449bd832023-01-11 14:50:10 +01007059 n = ((unsigned int) ssl->in_msg[i + 1] << 8)
Jerry Yud9526692022-02-17 14:23:47 +08007060 | (unsigned int) ssl->in_msg[i + 2];
7061 i += 3;
7062
Gilles Peskine449bd832023-01-11 14:50:10 +01007063 if (n < 128 || i + n > ssl->in_hslen) {
7064 MBEDTLS_SSL_DEBUG_MSG(1, ("bad certificate message"));
7065 mbedtls_ssl_send_alert_message(ssl,
7066 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
7067 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR);
7068 return MBEDTLS_ERR_SSL_DECODE_ERROR;
Jerry Yud9526692022-02-17 14:23:47 +08007069 }
7070
7071 /* Check if we're handling the first CRT in the chain. */
7072#if defined(MBEDTLS_SSL_RENEGOTIATION) && defined(MBEDTLS_SSL_CLI_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01007073 if (crt_cnt++ == 0 &&
Jerry Yud9526692022-02-17 14:23:47 +08007074 ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT &&
Gilles Peskine449bd832023-01-11 14:50:10 +01007075 ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS) {
Jerry Yud9526692022-02-17 14:23:47 +08007076 /* During client-side renegotiation, check that the server's
7077 * end-CRTs hasn't changed compared to the initial handshake,
7078 * mitigating the triple handshake attack. On success, reuse
7079 * the original end-CRT instead of parsing it again. */
Gilles Peskine449bd832023-01-11 14:50:10 +01007080 MBEDTLS_SSL_DEBUG_MSG(3, ("Check that peer CRT hasn't changed during renegotiation"));
7081 if (ssl_check_peer_crt_unchanged(ssl,
7082 &ssl->in_msg[i],
7083 n) != 0) {
7084 MBEDTLS_SSL_DEBUG_MSG(1, ("new server cert during renegotiation"));
7085 mbedtls_ssl_send_alert_message(ssl,
7086 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
7087 MBEDTLS_SSL_ALERT_MSG_ACCESS_DENIED);
7088 return MBEDTLS_ERR_SSL_BAD_CERTIFICATE;
Jerry Yud9526692022-02-17 14:23:47 +08007089 }
7090
7091 /* Now we can safely free the original chain. */
Gilles Peskine449bd832023-01-11 14:50:10 +01007092 ssl_clear_peer_cert(ssl->session);
Jerry Yud9526692022-02-17 14:23:47 +08007093 }
7094#endif /* MBEDTLS_SSL_RENEGOTIATION && MBEDTLS_SSL_CLI_C */
7095
7096 /* Parse the next certificate in the chain. */
7097#if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
Gilles Peskine449bd832023-01-11 14:50:10 +01007098 ret = mbedtls_x509_crt_parse_der(chain, ssl->in_msg + i, n);
Jerry Yud9526692022-02-17 14:23:47 +08007099#else
7100 /* If we don't need to store the CRT chain permanently, parse
7101 * it in-place from the input buffer instead of making a copy. */
Gilles Peskine449bd832023-01-11 14:50:10 +01007102 ret = mbedtls_x509_crt_parse_der_nocopy(chain, ssl->in_msg + i, n);
Jerry Yud9526692022-02-17 14:23:47 +08007103#endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
Gilles Peskine449bd832023-01-11 14:50:10 +01007104 switch (ret) {
Jerry Yud9526692022-02-17 14:23:47 +08007105 case 0: /*ok*/
7106 case MBEDTLS_ERR_X509_UNKNOWN_SIG_ALG + MBEDTLS_ERR_OID_NOT_FOUND:
7107 /* Ignore certificate with an unknown algorithm: maybe a
7108 prior certificate was already trusted. */
7109 break;
7110
7111 case MBEDTLS_ERR_X509_ALLOC_FAILED:
7112 alert = MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR;
7113 goto crt_parse_der_failed;
7114
7115 case MBEDTLS_ERR_X509_UNKNOWN_VERSION:
7116 alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT;
7117 goto crt_parse_der_failed;
7118
7119 default:
7120 alert = MBEDTLS_SSL_ALERT_MSG_BAD_CERT;
Gilles Peskine449bd832023-01-11 14:50:10 +01007121crt_parse_der_failed:
7122 mbedtls_ssl_send_alert_message(ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL, alert);
7123 MBEDTLS_SSL_DEBUG_RET(1, " mbedtls_x509_crt_parse_der", ret);
7124 return ret;
Jerry Yud9526692022-02-17 14:23:47 +08007125 }
7126
7127 i += n;
7128 }
7129
Gilles Peskine449bd832023-01-11 14:50:10 +01007130 MBEDTLS_SSL_DEBUG_CRT(3, "peer certificate", chain);
7131 return 0;
Jerry Yud9526692022-02-17 14:23:47 +08007132}
7133
7134#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02007135MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01007136static int ssl_srv_check_client_no_crt_notification(mbedtls_ssl_context *ssl)
Jerry Yud9526692022-02-17 14:23:47 +08007137{
Gilles Peskine449bd832023-01-11 14:50:10 +01007138 if (ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT) {
7139 return -1;
7140 }
Jerry Yud9526692022-02-17 14:23:47 +08007141
Gilles Peskine449bd832023-01-11 14:50:10 +01007142 if (ssl->in_hslen == 3 + mbedtls_ssl_hs_hdr_len(ssl) &&
Jerry Yud9526692022-02-17 14:23:47 +08007143 ssl->in_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE &&
7144 ssl->in_msg[0] == MBEDTLS_SSL_HS_CERTIFICATE &&
Gilles Peskine449bd832023-01-11 14:50:10 +01007145 memcmp(ssl->in_msg + mbedtls_ssl_hs_hdr_len(ssl), "\0\0\0", 3) == 0) {
7146 MBEDTLS_SSL_DEBUG_MSG(1, ("peer has no certificate"));
7147 return 0;
Jerry Yud9526692022-02-17 14:23:47 +08007148 }
Gilles Peskine449bd832023-01-11 14:50:10 +01007149 return -1;
Jerry Yud9526692022-02-17 14:23:47 +08007150}
7151#endif /* MBEDTLS_SSL_SRV_C */
7152
7153/* Check if a certificate message is expected.
7154 * Return either
7155 * - SSL_CERTIFICATE_EXPECTED, or
7156 * - SSL_CERTIFICATE_SKIP
7157 * indicating whether a Certificate message is expected or not.
7158 */
7159#define SSL_CERTIFICATE_EXPECTED 0
7160#define SSL_CERTIFICATE_SKIP 1
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02007161MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01007162static int ssl_parse_certificate_coordinate(mbedtls_ssl_context *ssl,
7163 int authmode)
Jerry Yud9526692022-02-17 14:23:47 +08007164{
7165 const mbedtls_ssl_ciphersuite_t *ciphersuite_info =
7166 ssl->handshake->ciphersuite_info;
7167
Gilles Peskine449bd832023-01-11 14:50:10 +01007168 if (!mbedtls_ssl_ciphersuite_uses_srv_cert(ciphersuite_info)) {
7169 return SSL_CERTIFICATE_SKIP;
7170 }
Jerry Yud9526692022-02-17 14:23:47 +08007171
7172#if defined(MBEDTLS_SSL_SRV_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01007173 if (ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER) {
7174 if (ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_RSA_PSK) {
7175 return SSL_CERTIFICATE_SKIP;
7176 }
Jerry Yud9526692022-02-17 14:23:47 +08007177
Gilles Peskine449bd832023-01-11 14:50:10 +01007178 if (authmode == MBEDTLS_SSL_VERIFY_NONE) {
Jerry Yud9526692022-02-17 14:23:47 +08007179 ssl->session_negotiate->verify_result =
7180 MBEDTLS_X509_BADCERT_SKIP_VERIFY;
Gilles Peskine449bd832023-01-11 14:50:10 +01007181 return SSL_CERTIFICATE_SKIP;
Jerry Yud9526692022-02-17 14:23:47 +08007182 }
7183 }
7184#else
7185 ((void) authmode);
7186#endif /* MBEDTLS_SSL_SRV_C */
7187
Gilles Peskine449bd832023-01-11 14:50:10 +01007188 return SSL_CERTIFICATE_EXPECTED;
Jerry Yud9526692022-02-17 14:23:47 +08007189}
7190
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02007191MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01007192static int ssl_parse_certificate_verify(mbedtls_ssl_context *ssl,
7193 int authmode,
7194 mbedtls_x509_crt *chain,
7195 void *rs_ctx)
Jerry Yud9526692022-02-17 14:23:47 +08007196{
7197 int ret = 0;
7198 const mbedtls_ssl_ciphersuite_t *ciphersuite_info =
7199 ssl->handshake->ciphersuite_info;
7200 int have_ca_chain = 0;
7201
7202 int (*f_vrfy)(void *, mbedtls_x509_crt *, int, uint32_t *);
7203 void *p_vrfy;
7204
Gilles Peskine449bd832023-01-11 14:50:10 +01007205 if (authmode == MBEDTLS_SSL_VERIFY_NONE) {
7206 return 0;
7207 }
Jerry Yud9526692022-02-17 14:23:47 +08007208
Gilles Peskine449bd832023-01-11 14:50:10 +01007209 if (ssl->f_vrfy != NULL) {
7210 MBEDTLS_SSL_DEBUG_MSG(3, ("Use context-specific verification callback"));
Jerry Yud9526692022-02-17 14:23:47 +08007211 f_vrfy = ssl->f_vrfy;
7212 p_vrfy = ssl->p_vrfy;
Gilles Peskine449bd832023-01-11 14:50:10 +01007213 } else {
7214 MBEDTLS_SSL_DEBUG_MSG(3, ("Use configuration-specific verification callback"));
Jerry Yud9526692022-02-17 14:23:47 +08007215 f_vrfy = ssl->conf->f_vrfy;
7216 p_vrfy = ssl->conf->p_vrfy;
7217 }
7218
7219 /*
7220 * Main check: verify certificate
7221 */
7222#if defined(MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK)
Gilles Peskine449bd832023-01-11 14:50:10 +01007223 if (ssl->conf->f_ca_cb != NULL) {
Jerry Yud9526692022-02-17 14:23:47 +08007224 ((void) rs_ctx);
7225 have_ca_chain = 1;
7226
Gilles Peskine449bd832023-01-11 14:50:10 +01007227 MBEDTLS_SSL_DEBUG_MSG(3, ("use CA callback for X.509 CRT verification"));
Jerry Yud9526692022-02-17 14:23:47 +08007228 ret = mbedtls_x509_crt_verify_with_ca_cb(
7229 chain,
7230 ssl->conf->f_ca_cb,
7231 ssl->conf->p_ca_cb,
7232 ssl->conf->cert_profile,
7233 ssl->hostname,
7234 &ssl->session_negotiate->verify_result,
Gilles Peskine449bd832023-01-11 14:50:10 +01007235 f_vrfy, p_vrfy);
7236 } else
Jerry Yud9526692022-02-17 14:23:47 +08007237#endif /* MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK */
7238 {
7239 mbedtls_x509_crt *ca_chain;
7240 mbedtls_x509_crl *ca_crl;
7241
7242#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
Gilles Peskine449bd832023-01-11 14:50:10 +01007243 if (ssl->handshake->sni_ca_chain != NULL) {
Jerry Yud9526692022-02-17 14:23:47 +08007244 ca_chain = ssl->handshake->sni_ca_chain;
7245 ca_crl = ssl->handshake->sni_ca_crl;
Gilles Peskine449bd832023-01-11 14:50:10 +01007246 } else
Jerry Yud9526692022-02-17 14:23:47 +08007247#endif
7248 {
7249 ca_chain = ssl->conf->ca_chain;
7250 ca_crl = ssl->conf->ca_crl;
7251 }
7252
Gilles Peskine449bd832023-01-11 14:50:10 +01007253 if (ca_chain != NULL) {
Jerry Yud9526692022-02-17 14:23:47 +08007254 have_ca_chain = 1;
Gilles Peskine449bd832023-01-11 14:50:10 +01007255 }
Jerry Yud9526692022-02-17 14:23:47 +08007256
7257 ret = mbedtls_x509_crt_verify_restartable(
7258 chain,
7259 ca_chain, ca_crl,
7260 ssl->conf->cert_profile,
7261 ssl->hostname,
7262 &ssl->session_negotiate->verify_result,
Gilles Peskine449bd832023-01-11 14:50:10 +01007263 f_vrfy, p_vrfy, rs_ctx);
Jerry Yud9526692022-02-17 14:23:47 +08007264 }
7265
Gilles Peskine449bd832023-01-11 14:50:10 +01007266 if (ret != 0) {
7267 MBEDTLS_SSL_DEBUG_RET(1, "x509_verify_cert", ret);
Jerry Yud9526692022-02-17 14:23:47 +08007268 }
7269
7270#if defined(MBEDTLS_SSL_ECP_RESTARTABLE_ENABLED)
Gilles Peskine449bd832023-01-11 14:50:10 +01007271 if (ret == MBEDTLS_ERR_ECP_IN_PROGRESS) {
7272 return MBEDTLS_ERR_SSL_CRYPTO_IN_PROGRESS;
7273 }
Jerry Yud9526692022-02-17 14:23:47 +08007274#endif
7275
7276 /*
7277 * Secondary checks: always done, but change 'ret' only if it was 0
7278 */
7279
7280#if defined(MBEDTLS_ECP_C)
7281 {
7282 const mbedtls_pk_context *pk = &chain->pk;
7283
Manuel Pégourié-Gonnard66b0d612022-06-17 10:49:29 +02007284 /* If certificate uses an EC key, make sure the curve is OK.
7285 * This is a public key, so it can't be opaque, so can_do() is a good
7286 * enough check to ensure pk_ec() is safe to use here. */
Gilles Peskine449bd832023-01-11 14:50:10 +01007287 if (mbedtls_pk_can_do(pk, MBEDTLS_PK_ECKEY)) {
Leonid Rozenboim19e59732022-08-08 16:52:38 -07007288 /* and in the unlikely case the above assumption no longer holds
7289 * we are making sure that pk_ec() here does not return a NULL
7290 */
Gilles Peskine449bd832023-01-11 14:50:10 +01007291 const mbedtls_ecp_keypair *ec = mbedtls_pk_ec(*pk);
7292 if (ec == NULL) {
7293 MBEDTLS_SSL_DEBUG_MSG(1, ("mbedtls_pk_ec() returned NULL"));
7294 return MBEDTLS_ERR_SSL_INTERNAL_ERROR;
Leonid Rozenboim19e59732022-08-08 16:52:38 -07007295 }
Jerry Yud9526692022-02-17 14:23:47 +08007296
Gilles Peskine449bd832023-01-11 14:50:10 +01007297 if (mbedtls_ssl_check_curve(ssl, ec->grp.id) != 0) {
Leonid Rozenboim19e59732022-08-08 16:52:38 -07007298 ssl->session_negotiate->verify_result |=
7299 MBEDTLS_X509_BADCERT_BAD_KEY;
7300
Gilles Peskine449bd832023-01-11 14:50:10 +01007301 MBEDTLS_SSL_DEBUG_MSG(1, ("bad certificate (EC key curve)"));
7302 if (ret == 0) {
Leonid Rozenboim19e59732022-08-08 16:52:38 -07007303 ret = MBEDTLS_ERR_SSL_BAD_CERTIFICATE;
Gilles Peskine449bd832023-01-11 14:50:10 +01007304 }
Leonid Rozenboim19e59732022-08-08 16:52:38 -07007305 }
Jerry Yud9526692022-02-17 14:23:47 +08007306 }
7307 }
7308#endif /* MBEDTLS_ECP_C */
7309
Gilles Peskine449bd832023-01-11 14:50:10 +01007310 if (mbedtls_ssl_check_cert_usage(chain,
7311 ciphersuite_info,
7312 !ssl->conf->endpoint,
7313 &ssl->session_negotiate->verify_result) != 0) {
7314 MBEDTLS_SSL_DEBUG_MSG(1, ("bad certificate (usage extensions)"));
7315 if (ret == 0) {
Jerry Yud9526692022-02-17 14:23:47 +08007316 ret = MBEDTLS_ERR_SSL_BAD_CERTIFICATE;
Gilles Peskine449bd832023-01-11 14:50:10 +01007317 }
Jerry Yud9526692022-02-17 14:23:47 +08007318 }
7319
7320 /* mbedtls_x509_crt_verify_with_profile is supposed to report a
7321 * verification failure through MBEDTLS_ERR_X509_CERT_VERIFY_FAILED,
7322 * with details encoded in the verification flags. All other kinds
7323 * of error codes, including those from the user provided f_vrfy
7324 * functions, are treated as fatal and lead to a failure of
7325 * ssl_parse_certificate even if verification was optional. */
Gilles Peskine449bd832023-01-11 14:50:10 +01007326 if (authmode == MBEDTLS_SSL_VERIFY_OPTIONAL &&
7327 (ret == MBEDTLS_ERR_X509_CERT_VERIFY_FAILED ||
7328 ret == MBEDTLS_ERR_SSL_BAD_CERTIFICATE)) {
Jerry Yud9526692022-02-17 14:23:47 +08007329 ret = 0;
7330 }
7331
Gilles Peskine449bd832023-01-11 14:50:10 +01007332 if (have_ca_chain == 0 && authmode == MBEDTLS_SSL_VERIFY_REQUIRED) {
7333 MBEDTLS_SSL_DEBUG_MSG(1, ("got no CA chain"));
Jerry Yud9526692022-02-17 14:23:47 +08007334 ret = MBEDTLS_ERR_SSL_CA_CHAIN_REQUIRED;
7335 }
7336
Gilles Peskine449bd832023-01-11 14:50:10 +01007337 if (ret != 0) {
Jerry Yud9526692022-02-17 14:23:47 +08007338 uint8_t alert;
7339
7340 /* The certificate may have been rejected for several reasons.
7341 Pick one and send the corresponding alert. Which alert to send
7342 may be a subject of debate in some cases. */
Gilles Peskine449bd832023-01-11 14:50:10 +01007343 if (ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_OTHER) {
Jerry Yud9526692022-02-17 14:23:47 +08007344 alert = MBEDTLS_SSL_ALERT_MSG_ACCESS_DENIED;
Gilles Peskine449bd832023-01-11 14:50:10 +01007345 } else if (ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_CN_MISMATCH) {
Jerry Yud9526692022-02-17 14:23:47 +08007346 alert = MBEDTLS_SSL_ALERT_MSG_BAD_CERT;
Gilles Peskine449bd832023-01-11 14:50:10 +01007347 } else if (ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_KEY_USAGE) {
Jerry Yud9526692022-02-17 14:23:47 +08007348 alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT;
Gilles Peskine449bd832023-01-11 14:50:10 +01007349 } else if (ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_EXT_KEY_USAGE) {
Jerry Yud9526692022-02-17 14:23:47 +08007350 alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT;
Gilles Peskine449bd832023-01-11 14:50:10 +01007351 } else if (ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_NS_CERT_TYPE) {
Jerry Yud9526692022-02-17 14:23:47 +08007352 alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT;
Gilles Peskine449bd832023-01-11 14:50:10 +01007353 } else if (ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_BAD_PK) {
Jerry Yud9526692022-02-17 14:23:47 +08007354 alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT;
Gilles Peskine449bd832023-01-11 14:50:10 +01007355 } else if (ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_BAD_KEY) {
Jerry Yud9526692022-02-17 14:23:47 +08007356 alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT;
Gilles Peskine449bd832023-01-11 14:50:10 +01007357 } else if (ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_EXPIRED) {
Jerry Yud9526692022-02-17 14:23:47 +08007358 alert = MBEDTLS_SSL_ALERT_MSG_CERT_EXPIRED;
Gilles Peskine449bd832023-01-11 14:50:10 +01007359 } else if (ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_REVOKED) {
Jerry Yud9526692022-02-17 14:23:47 +08007360 alert = MBEDTLS_SSL_ALERT_MSG_CERT_REVOKED;
Gilles Peskine449bd832023-01-11 14:50:10 +01007361 } else if (ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_NOT_TRUSTED) {
Jerry Yud9526692022-02-17 14:23:47 +08007362 alert = MBEDTLS_SSL_ALERT_MSG_UNKNOWN_CA;
Gilles Peskine449bd832023-01-11 14:50:10 +01007363 } else {
Jerry Yud9526692022-02-17 14:23:47 +08007364 alert = MBEDTLS_SSL_ALERT_MSG_CERT_UNKNOWN;
Gilles Peskine449bd832023-01-11 14:50:10 +01007365 }
7366 mbedtls_ssl_send_alert_message(ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
7367 alert);
Jerry Yud9526692022-02-17 14:23:47 +08007368 }
7369
7370#if defined(MBEDTLS_DEBUG_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01007371 if (ssl->session_negotiate->verify_result != 0) {
7372 MBEDTLS_SSL_DEBUG_MSG(3, ("! Certificate verification flags %08x",
7373 (unsigned int) ssl->session_negotiate->verify_result));
7374 } else {
7375 MBEDTLS_SSL_DEBUG_MSG(3, ("Certificate verification flags clear"));
Jerry Yud9526692022-02-17 14:23:47 +08007376 }
7377#endif /* MBEDTLS_DEBUG_C */
7378
Gilles Peskine449bd832023-01-11 14:50:10 +01007379 return ret;
Jerry Yud9526692022-02-17 14:23:47 +08007380}
7381
7382#if !defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02007383MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01007384static int ssl_remember_peer_crt_digest(mbedtls_ssl_context *ssl,
7385 unsigned char *start, size_t len)
Jerry Yud9526692022-02-17 14:23:47 +08007386{
7387 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
7388 /* Remember digest of the peer's end-CRT. */
7389 ssl->session_negotiate->peer_cert_digest =
Gilles Peskine449bd832023-01-11 14:50:10 +01007390 mbedtls_calloc(1, MBEDTLS_SSL_PEER_CERT_DIGEST_DFL_LEN);
7391 if (ssl->session_negotiate->peer_cert_digest == NULL) {
7392 MBEDTLS_SSL_DEBUG_MSG(1, ("alloc(%d bytes) failed",
7393 MBEDTLS_SSL_PEER_CERT_DIGEST_DFL_LEN));
7394 mbedtls_ssl_send_alert_message(ssl,
7395 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
7396 MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR);
Jerry Yud9526692022-02-17 14:23:47 +08007397
Gilles Peskine449bd832023-01-11 14:50:10 +01007398 return MBEDTLS_ERR_SSL_ALLOC_FAILED;
Jerry Yud9526692022-02-17 14:23:47 +08007399 }
7400
Gilles Peskine449bd832023-01-11 14:50:10 +01007401 ret = mbedtls_md(mbedtls_md_info_from_type(
7402 MBEDTLS_SSL_PEER_CERT_DIGEST_DFL_TYPE),
7403 start, len,
7404 ssl->session_negotiate->peer_cert_digest);
Jerry Yud9526692022-02-17 14:23:47 +08007405
7406 ssl->session_negotiate->peer_cert_digest_type =
7407 MBEDTLS_SSL_PEER_CERT_DIGEST_DFL_TYPE;
7408 ssl->session_negotiate->peer_cert_digest_len =
7409 MBEDTLS_SSL_PEER_CERT_DIGEST_DFL_LEN;
7410
Gilles Peskine449bd832023-01-11 14:50:10 +01007411 return ret;
Jerry Yud9526692022-02-17 14:23:47 +08007412}
7413
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02007414MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01007415static int ssl_remember_peer_pubkey(mbedtls_ssl_context *ssl,
7416 unsigned char *start, size_t len)
Jerry Yud9526692022-02-17 14:23:47 +08007417{
7418 unsigned char *end = start + len;
7419 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
7420
7421 /* Make a copy of the peer's raw public key. */
Gilles Peskine449bd832023-01-11 14:50:10 +01007422 mbedtls_pk_init(&ssl->handshake->peer_pubkey);
7423 ret = mbedtls_pk_parse_subpubkey(&start, end,
7424 &ssl->handshake->peer_pubkey);
7425 if (ret != 0) {
Jerry Yud9526692022-02-17 14:23:47 +08007426 /* We should have parsed the public key before. */
Gilles Peskine449bd832023-01-11 14:50:10 +01007427 return MBEDTLS_ERR_SSL_INTERNAL_ERROR;
Jerry Yud9526692022-02-17 14:23:47 +08007428 }
7429
Gilles Peskine449bd832023-01-11 14:50:10 +01007430 return 0;
Jerry Yud9526692022-02-17 14:23:47 +08007431}
7432#endif /* !MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
7433
Gilles Peskine449bd832023-01-11 14:50:10 +01007434int mbedtls_ssl_parse_certificate(mbedtls_ssl_context *ssl)
Jerry Yud9526692022-02-17 14:23:47 +08007435{
7436 int ret = 0;
7437 int crt_expected;
7438#if defined(MBEDTLS_SSL_SRV_C) && defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
7439 const int authmode = ssl->handshake->sni_authmode != MBEDTLS_SSL_VERIFY_UNSET
7440 ? ssl->handshake->sni_authmode
7441 : ssl->conf->authmode;
7442#else
7443 const int authmode = ssl->conf->authmode;
7444#endif
7445 void *rs_ctx = NULL;
7446 mbedtls_x509_crt *chain = NULL;
7447
Gilles Peskine449bd832023-01-11 14:50:10 +01007448 MBEDTLS_SSL_DEBUG_MSG(2, ("=> parse certificate"));
Jerry Yud9526692022-02-17 14:23:47 +08007449
Gilles Peskine449bd832023-01-11 14:50:10 +01007450 crt_expected = ssl_parse_certificate_coordinate(ssl, authmode);
7451 if (crt_expected == SSL_CERTIFICATE_SKIP) {
7452 MBEDTLS_SSL_DEBUG_MSG(2, ("<= skip parse certificate"));
Jerry Yud9526692022-02-17 14:23:47 +08007453 goto exit;
7454 }
7455
7456#if defined(MBEDTLS_SSL_ECP_RESTARTABLE_ENABLED)
Gilles Peskine449bd832023-01-11 14:50:10 +01007457 if (ssl->handshake->ecrs_enabled &&
7458 ssl->handshake->ecrs_state == ssl_ecrs_crt_verify) {
Jerry Yud9526692022-02-17 14:23:47 +08007459 chain = ssl->handshake->ecrs_peer_cert;
7460 ssl->handshake->ecrs_peer_cert = NULL;
7461 goto crt_verify;
7462 }
7463#endif
7464
Gilles Peskine449bd832023-01-11 14:50:10 +01007465 if ((ret = mbedtls_ssl_read_record(ssl, 1)) != 0) {
Jerry Yud9526692022-02-17 14:23:47 +08007466 /* mbedtls_ssl_read_record may have sent an alert already. We
7467 let it decide whether to alert. */
Gilles Peskine449bd832023-01-11 14:50:10 +01007468 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ssl_read_record", ret);
Jerry Yud9526692022-02-17 14:23:47 +08007469 goto exit;
7470 }
7471
7472#if defined(MBEDTLS_SSL_SRV_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01007473 if (ssl_srv_check_client_no_crt_notification(ssl) == 0) {
Jerry Yud9526692022-02-17 14:23:47 +08007474 ssl->session_negotiate->verify_result = MBEDTLS_X509_BADCERT_MISSING;
7475
Gilles Peskine449bd832023-01-11 14:50:10 +01007476 if (authmode != MBEDTLS_SSL_VERIFY_OPTIONAL) {
Jerry Yud9526692022-02-17 14:23:47 +08007477 ret = MBEDTLS_ERR_SSL_NO_CLIENT_CERTIFICATE;
Gilles Peskine449bd832023-01-11 14:50:10 +01007478 }
Jerry Yud9526692022-02-17 14:23:47 +08007479
7480 goto exit;
7481 }
7482#endif /* MBEDTLS_SSL_SRV_C */
7483
7484 /* Clear existing peer CRT structure in case we tried to
7485 * reuse a session but it failed, and allocate a new one. */
Gilles Peskine449bd832023-01-11 14:50:10 +01007486 ssl_clear_peer_cert(ssl->session_negotiate);
Jerry Yud9526692022-02-17 14:23:47 +08007487
Gilles Peskine449bd832023-01-11 14:50:10 +01007488 chain = mbedtls_calloc(1, sizeof(mbedtls_x509_crt));
7489 if (chain == NULL) {
7490 MBEDTLS_SSL_DEBUG_MSG(1, ("alloc(%" MBEDTLS_PRINTF_SIZET " bytes) failed",
7491 sizeof(mbedtls_x509_crt)));
7492 mbedtls_ssl_send_alert_message(ssl,
7493 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
7494 MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR);
Jerry Yud9526692022-02-17 14:23:47 +08007495
7496 ret = MBEDTLS_ERR_SSL_ALLOC_FAILED;
7497 goto exit;
7498 }
Gilles Peskine449bd832023-01-11 14:50:10 +01007499 mbedtls_x509_crt_init(chain);
Jerry Yud9526692022-02-17 14:23:47 +08007500
Gilles Peskine449bd832023-01-11 14:50:10 +01007501 ret = ssl_parse_certificate_chain(ssl, chain);
7502 if (ret != 0) {
Jerry Yud9526692022-02-17 14:23:47 +08007503 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01007504 }
Jerry Yud9526692022-02-17 14:23:47 +08007505
7506#if defined(MBEDTLS_SSL_ECP_RESTARTABLE_ENABLED)
Gilles Peskine449bd832023-01-11 14:50:10 +01007507 if (ssl->handshake->ecrs_enabled) {
Jerry Yud9526692022-02-17 14:23:47 +08007508 ssl->handshake->ecrs_state = ssl_ecrs_crt_verify;
Gilles Peskine449bd832023-01-11 14:50:10 +01007509 }
Jerry Yud9526692022-02-17 14:23:47 +08007510
7511crt_verify:
Gilles Peskine449bd832023-01-11 14:50:10 +01007512 if (ssl->handshake->ecrs_enabled) {
Jerry Yud9526692022-02-17 14:23:47 +08007513 rs_ctx = &ssl->handshake->ecrs_ctx;
Gilles Peskine449bd832023-01-11 14:50:10 +01007514 }
Jerry Yud9526692022-02-17 14:23:47 +08007515#endif
7516
Gilles Peskine449bd832023-01-11 14:50:10 +01007517 ret = ssl_parse_certificate_verify(ssl, authmode,
7518 chain, rs_ctx);
7519 if (ret != 0) {
Jerry Yud9526692022-02-17 14:23:47 +08007520 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01007521 }
Jerry Yud9526692022-02-17 14:23:47 +08007522
7523#if !defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
7524 {
7525 unsigned char *crt_start, *pk_start;
7526 size_t crt_len, pk_len;
7527
7528 /* We parse the CRT chain without copying, so
7529 * these pointers point into the input buffer,
7530 * and are hence still valid after freeing the
7531 * CRT chain. */
7532
7533 crt_start = chain->raw.p;
7534 crt_len = chain->raw.len;
7535
7536 pk_start = chain->pk_raw.p;
7537 pk_len = chain->pk_raw.len;
7538
7539 /* Free the CRT structures before computing
7540 * digest and copying the peer's public key. */
Gilles Peskine449bd832023-01-11 14:50:10 +01007541 mbedtls_x509_crt_free(chain);
7542 mbedtls_free(chain);
Jerry Yud9526692022-02-17 14:23:47 +08007543 chain = NULL;
7544
Gilles Peskine449bd832023-01-11 14:50:10 +01007545 ret = ssl_remember_peer_crt_digest(ssl, crt_start, crt_len);
7546 if (ret != 0) {
Jerry Yud9526692022-02-17 14:23:47 +08007547 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01007548 }
Jerry Yud9526692022-02-17 14:23:47 +08007549
Gilles Peskine449bd832023-01-11 14:50:10 +01007550 ret = ssl_remember_peer_pubkey(ssl, pk_start, pk_len);
7551 if (ret != 0) {
Jerry Yud9526692022-02-17 14:23:47 +08007552 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01007553 }
Jerry Yud9526692022-02-17 14:23:47 +08007554 }
7555#else /* !MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
7556 /* Pass ownership to session structure. */
7557 ssl->session_negotiate->peer_cert = chain;
7558 chain = NULL;
7559#endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
7560
Gilles Peskine449bd832023-01-11 14:50:10 +01007561 MBEDTLS_SSL_DEBUG_MSG(2, ("<= parse certificate"));
Jerry Yud9526692022-02-17 14:23:47 +08007562
7563exit:
7564
Gilles Peskine449bd832023-01-11 14:50:10 +01007565 if (ret == 0) {
Jerry Yud9526692022-02-17 14:23:47 +08007566 ssl->state++;
Gilles Peskine449bd832023-01-11 14:50:10 +01007567 }
Jerry Yud9526692022-02-17 14:23:47 +08007568
7569#if defined(MBEDTLS_SSL_ECP_RESTARTABLE_ENABLED)
Gilles Peskine449bd832023-01-11 14:50:10 +01007570 if (ret == MBEDTLS_ERR_SSL_CRYPTO_IN_PROGRESS) {
Jerry Yud9526692022-02-17 14:23:47 +08007571 ssl->handshake->ecrs_peer_cert = chain;
7572 chain = NULL;
7573 }
7574#endif
7575
Gilles Peskine449bd832023-01-11 14:50:10 +01007576 if (chain != NULL) {
7577 mbedtls_x509_crt_free(chain);
7578 mbedtls_free(chain);
Jerry Yud9526692022-02-17 14:23:47 +08007579 }
7580
Gilles Peskine449bd832023-01-11 14:50:10 +01007581 return ret;
Jerry Yud9526692022-02-17 14:23:47 +08007582}
7583#endif /* MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED */
7584
Andrzej Kurek25f27152022-08-17 16:09:31 -04007585#if defined(MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Manuel Pégourié-Gonnard226aa152023-02-05 09:46:59 +01007586static int ssl_calc_finished_tls_sha256(
Gilles Peskine449bd832023-01-11 14:50:10 +01007587 mbedtls_ssl_context *ssl, unsigned char *buf, int from)
Jerry Yu615bd6f2022-02-17 14:25:15 +08007588{
7589 int len = 12;
7590 const char *sender;
7591 unsigned char padbuf[32];
7592#if defined(MBEDTLS_USE_PSA_CRYPTO)
7593 size_t hash_size;
7594 psa_hash_operation_t sha256_psa = PSA_HASH_OPERATION_INIT;
7595 psa_status_t status;
7596#else
7597 mbedtls_sha256_context sha256;
7598#endif
7599
7600 mbedtls_ssl_session *session = ssl->session_negotiate;
Gilles Peskine449bd832023-01-11 14:50:10 +01007601 if (!session) {
Jerry Yu615bd6f2022-02-17 14:25:15 +08007602 session = ssl->session;
Gilles Peskine449bd832023-01-11 14:50:10 +01007603 }
Jerry Yu615bd6f2022-02-17 14:25:15 +08007604
Gilles Peskine449bd832023-01-11 14:50:10 +01007605 sender = (from == MBEDTLS_SSL_IS_CLIENT)
Jerry Yu615bd6f2022-02-17 14:25:15 +08007606 ? "client finished"
7607 : "server finished";
7608
7609#if defined(MBEDTLS_USE_PSA_CRYPTO)
7610 sha256_psa = psa_hash_operation_init();
7611
Gilles Peskine449bd832023-01-11 14:50:10 +01007612 MBEDTLS_SSL_DEBUG_MSG(2, ("=> calc PSA finished tls sha256"));
Jerry Yu615bd6f2022-02-17 14:25:15 +08007613
Gilles Peskine449bd832023-01-11 14:50:10 +01007614 status = psa_hash_clone(&ssl->handshake->fin_sha256_psa, &sha256_psa);
7615 if (status != PSA_SUCCESS) {
7616 MBEDTLS_SSL_DEBUG_MSG(2, ("PSA hash clone failed"));
Manuel Pégourié-Gonnard226aa152023-02-05 09:46:59 +01007617 return 0;
Jerry Yu615bd6f2022-02-17 14:25:15 +08007618 }
7619
Gilles Peskine449bd832023-01-11 14:50:10 +01007620 status = psa_hash_finish(&sha256_psa, padbuf, sizeof(padbuf), &hash_size);
7621 if (status != PSA_SUCCESS) {
7622 MBEDTLS_SSL_DEBUG_MSG(2, ("PSA hash finish failed"));
Manuel Pégourié-Gonnard226aa152023-02-05 09:46:59 +01007623 return 0;
Jerry Yu615bd6f2022-02-17 14:25:15 +08007624 }
Gilles Peskine449bd832023-01-11 14:50:10 +01007625 MBEDTLS_SSL_DEBUG_BUF(3, "PSA calculated padbuf", padbuf, 32);
Jerry Yu615bd6f2022-02-17 14:25:15 +08007626#else
7627
Gilles Peskine449bd832023-01-11 14:50:10 +01007628 mbedtls_sha256_init(&sha256);
Jerry Yu615bd6f2022-02-17 14:25:15 +08007629
Gilles Peskine449bd832023-01-11 14:50:10 +01007630 MBEDTLS_SSL_DEBUG_MSG(2, ("=> calc finished tls sha256"));
Jerry Yu615bd6f2022-02-17 14:25:15 +08007631
Gilles Peskine449bd832023-01-11 14:50:10 +01007632 mbedtls_sha256_clone(&sha256, &ssl->handshake->fin_sha256);
Jerry Yu615bd6f2022-02-17 14:25:15 +08007633
7634 /*
7635 * TLSv1.2:
7636 * hash = PRF( master, finished_label,
7637 * Hash( handshake ) )[0.11]
7638 */
7639
7640#if !defined(MBEDTLS_SHA256_ALT)
Gilles Peskine449bd832023-01-11 14:50:10 +01007641 MBEDTLS_SSL_DEBUG_BUF(4, "finished sha2 state", (unsigned char *)
7642 sha256.state, sizeof(sha256.state));
Jerry Yu615bd6f2022-02-17 14:25:15 +08007643#endif
7644
Gilles Peskine449bd832023-01-11 14:50:10 +01007645 mbedtls_sha256_finish(&sha256, padbuf);
7646 mbedtls_sha256_free(&sha256);
Jerry Yu615bd6f2022-02-17 14:25:15 +08007647#endif /* MBEDTLS_USE_PSA_CRYPTO */
7648
Gilles Peskine449bd832023-01-11 14:50:10 +01007649 ssl->handshake->tls_prf(session->master, 48, sender,
7650 padbuf, 32, buf, len);
Jerry Yu615bd6f2022-02-17 14:25:15 +08007651
Gilles Peskine449bd832023-01-11 14:50:10 +01007652 MBEDTLS_SSL_DEBUG_BUF(3, "calc finished result", buf, len);
Jerry Yu615bd6f2022-02-17 14:25:15 +08007653
Gilles Peskine449bd832023-01-11 14:50:10 +01007654 mbedtls_platform_zeroize(padbuf, sizeof(padbuf));
Jerry Yu615bd6f2022-02-17 14:25:15 +08007655
Gilles Peskine449bd832023-01-11 14:50:10 +01007656 MBEDTLS_SSL_DEBUG_MSG(2, ("<= calc finished"));
Manuel Pégourié-Gonnard226aa152023-02-05 09:46:59 +01007657 return 0;
Jerry Yu615bd6f2022-02-17 14:25:15 +08007658}
Andrzej Kurekcccb0442022-08-19 03:42:11 -04007659#endif /* MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA*/
Jerry Yu615bd6f2022-02-17 14:25:15 +08007660
Jerry Yub7ba49e2022-02-17 14:25:53 +08007661
Andrzej Kurek25f27152022-08-17 16:09:31 -04007662#if defined(MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Manuel Pégourié-Gonnard226aa152023-02-05 09:46:59 +01007663static int ssl_calc_finished_tls_sha384(
Gilles Peskine449bd832023-01-11 14:50:10 +01007664 mbedtls_ssl_context *ssl, unsigned char *buf, int from)
Jerry Yub7ba49e2022-02-17 14:25:53 +08007665{
7666 int len = 12;
7667 const char *sender;
7668 unsigned char padbuf[48];
7669#if defined(MBEDTLS_USE_PSA_CRYPTO)
7670 size_t hash_size;
7671 psa_hash_operation_t sha384_psa = PSA_HASH_OPERATION_INIT;
7672 psa_status_t status;
7673#else
7674 mbedtls_sha512_context sha512;
7675#endif
7676
7677 mbedtls_ssl_session *session = ssl->session_negotiate;
Gilles Peskine449bd832023-01-11 14:50:10 +01007678 if (!session) {
Jerry Yub7ba49e2022-02-17 14:25:53 +08007679 session = ssl->session;
Gilles Peskine449bd832023-01-11 14:50:10 +01007680 }
Jerry Yub7ba49e2022-02-17 14:25:53 +08007681
Gilles Peskine449bd832023-01-11 14:50:10 +01007682 sender = (from == MBEDTLS_SSL_IS_CLIENT)
Jerry Yub7ba49e2022-02-17 14:25:53 +08007683 ? "client finished"
7684 : "server finished";
7685
7686#if defined(MBEDTLS_USE_PSA_CRYPTO)
7687 sha384_psa = psa_hash_operation_init();
7688
Gilles Peskine449bd832023-01-11 14:50:10 +01007689 MBEDTLS_SSL_DEBUG_MSG(2, ("=> calc PSA finished tls sha384"));
Jerry Yub7ba49e2022-02-17 14:25:53 +08007690
Gilles Peskine449bd832023-01-11 14:50:10 +01007691 status = psa_hash_clone(&ssl->handshake->fin_sha384_psa, &sha384_psa);
7692 if (status != PSA_SUCCESS) {
7693 MBEDTLS_SSL_DEBUG_MSG(2, ("PSA hash clone failed"));
Manuel Pégourié-Gonnard226aa152023-02-05 09:46:59 +01007694 return 0;
Jerry Yub7ba49e2022-02-17 14:25:53 +08007695 }
7696
Gilles Peskine449bd832023-01-11 14:50:10 +01007697 status = psa_hash_finish(&sha384_psa, padbuf, sizeof(padbuf), &hash_size);
7698 if (status != PSA_SUCCESS) {
7699 MBEDTLS_SSL_DEBUG_MSG(2, ("PSA hash finish failed"));
Manuel Pégourié-Gonnard226aa152023-02-05 09:46:59 +01007700 return 0;
Jerry Yub7ba49e2022-02-17 14:25:53 +08007701 }
Gilles Peskine449bd832023-01-11 14:50:10 +01007702 MBEDTLS_SSL_DEBUG_BUF(3, "PSA calculated padbuf", padbuf, 48);
Jerry Yub7ba49e2022-02-17 14:25:53 +08007703#else
Gilles Peskine449bd832023-01-11 14:50:10 +01007704 mbedtls_sha512_init(&sha512);
Jerry Yub7ba49e2022-02-17 14:25:53 +08007705
Gilles Peskine449bd832023-01-11 14:50:10 +01007706 MBEDTLS_SSL_DEBUG_MSG(2, ("=> calc finished tls sha384"));
Jerry Yub7ba49e2022-02-17 14:25:53 +08007707
Gilles Peskine449bd832023-01-11 14:50:10 +01007708 mbedtls_sha512_clone(&sha512, &ssl->handshake->fin_sha384);
Jerry Yub7ba49e2022-02-17 14:25:53 +08007709
7710 /*
7711 * TLSv1.2:
7712 * hash = PRF( master, finished_label,
7713 * Hash( handshake ) )[0.11]
7714 */
7715
7716#if !defined(MBEDTLS_SHA512_ALT)
Gilles Peskine449bd832023-01-11 14:50:10 +01007717 MBEDTLS_SSL_DEBUG_BUF(4, "finished sha512 state", (unsigned char *)
7718 sha512.state, sizeof(sha512.state));
Jerry Yub7ba49e2022-02-17 14:25:53 +08007719#endif
Gilles Peskine449bd832023-01-11 14:50:10 +01007720 mbedtls_sha512_finish(&sha512, padbuf);
Jerry Yub7ba49e2022-02-17 14:25:53 +08007721
Gilles Peskine449bd832023-01-11 14:50:10 +01007722 mbedtls_sha512_free(&sha512);
Jerry Yub7ba49e2022-02-17 14:25:53 +08007723#endif
7724
Gilles Peskine449bd832023-01-11 14:50:10 +01007725 ssl->handshake->tls_prf(session->master, 48, sender,
7726 padbuf, 48, buf, len);
Jerry Yub7ba49e2022-02-17 14:25:53 +08007727
Gilles Peskine449bd832023-01-11 14:50:10 +01007728 MBEDTLS_SSL_DEBUG_BUF(3, "calc finished result", buf, len);
Jerry Yub7ba49e2022-02-17 14:25:53 +08007729
Gilles Peskine449bd832023-01-11 14:50:10 +01007730 mbedtls_platform_zeroize(padbuf, sizeof(padbuf));
Jerry Yub7ba49e2022-02-17 14:25:53 +08007731
Gilles Peskine449bd832023-01-11 14:50:10 +01007732 MBEDTLS_SSL_DEBUG_MSG(2, ("<= calc finished"));
Manuel Pégourié-Gonnard226aa152023-02-05 09:46:59 +01007733 return 0;
Jerry Yub7ba49e2022-02-17 14:25:53 +08007734}
Andrzej Kurekcccb0442022-08-19 03:42:11 -04007735#endif /* MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA*/
Jerry Yub7ba49e2022-02-17 14:25:53 +08007736
Gilles Peskine449bd832023-01-11 14:50:10 +01007737void mbedtls_ssl_handshake_wrapup_free_hs_transform(mbedtls_ssl_context *ssl)
Jerry Yuaef00152022-02-17 14:27:31 +08007738{
Gilles Peskine449bd832023-01-11 14:50:10 +01007739 MBEDTLS_SSL_DEBUG_MSG(3, ("=> handshake wrapup: final free"));
Jerry Yuaef00152022-02-17 14:27:31 +08007740
7741 /*
7742 * Free our handshake params
7743 */
Gilles Peskine449bd832023-01-11 14:50:10 +01007744 mbedtls_ssl_handshake_free(ssl);
7745 mbedtls_free(ssl->handshake);
Jerry Yuaef00152022-02-17 14:27:31 +08007746 ssl->handshake = NULL;
7747
7748 /*
Shaun Case8b0ecbc2021-12-20 21:14:10 -08007749 * Free the previous transform and switch in the current one
Jerry Yuaef00152022-02-17 14:27:31 +08007750 */
Gilles Peskine449bd832023-01-11 14:50:10 +01007751 if (ssl->transform) {
7752 mbedtls_ssl_transform_free(ssl->transform);
7753 mbedtls_free(ssl->transform);
Jerry Yuaef00152022-02-17 14:27:31 +08007754 }
7755 ssl->transform = ssl->transform_negotiate;
7756 ssl->transform_negotiate = NULL;
7757
Gilles Peskine449bd832023-01-11 14:50:10 +01007758 MBEDTLS_SSL_DEBUG_MSG(3, ("<= handshake wrapup: final free"));
Jerry Yuaef00152022-02-17 14:27:31 +08007759}
7760
Gilles Peskine449bd832023-01-11 14:50:10 +01007761void mbedtls_ssl_handshake_wrapup(mbedtls_ssl_context *ssl)
Jerry Yu2a9fff52022-02-17 14:28:51 +08007762{
7763 int resume = ssl->handshake->resume;
7764
Gilles Peskine449bd832023-01-11 14:50:10 +01007765 MBEDTLS_SSL_DEBUG_MSG(3, ("=> handshake wrapup"));
Jerry Yu2a9fff52022-02-17 14:28:51 +08007766
7767#if defined(MBEDTLS_SSL_RENEGOTIATION)
Gilles Peskine449bd832023-01-11 14:50:10 +01007768 if (ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS) {
Jerry Yu2a9fff52022-02-17 14:28:51 +08007769 ssl->renego_status = MBEDTLS_SSL_RENEGOTIATION_DONE;
7770 ssl->renego_records_seen = 0;
7771 }
7772#endif
7773
7774 /*
7775 * Free the previous session and switch in the current one
7776 */
Gilles Peskine449bd832023-01-11 14:50:10 +01007777 if (ssl->session) {
Jerry Yu2a9fff52022-02-17 14:28:51 +08007778#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
7779 /* RFC 7366 3.1: keep the EtM state */
7780 ssl->session_negotiate->encrypt_then_mac =
Gilles Peskine449bd832023-01-11 14:50:10 +01007781 ssl->session->encrypt_then_mac;
Jerry Yu2a9fff52022-02-17 14:28:51 +08007782#endif
7783
Gilles Peskine449bd832023-01-11 14:50:10 +01007784 mbedtls_ssl_session_free(ssl->session);
7785 mbedtls_free(ssl->session);
Jerry Yu2a9fff52022-02-17 14:28:51 +08007786 }
7787 ssl->session = ssl->session_negotiate;
7788 ssl->session_negotiate = NULL;
7789
7790 /*
7791 * Add cache entry
7792 */
Gilles Peskine449bd832023-01-11 14:50:10 +01007793 if (ssl->conf->f_set_cache != NULL &&
Jerry Yu2a9fff52022-02-17 14:28:51 +08007794 ssl->session->id_len != 0 &&
Gilles Peskine449bd832023-01-11 14:50:10 +01007795 resume == 0) {
7796 if (ssl->conf->f_set_cache(ssl->conf->p_cache,
7797 ssl->session->id,
7798 ssl->session->id_len,
7799 ssl->session) != 0) {
7800 MBEDTLS_SSL_DEBUG_MSG(1, ("cache did not store session"));
7801 }
Jerry Yu2a9fff52022-02-17 14:28:51 +08007802 }
7803
7804#if defined(MBEDTLS_SSL_PROTO_DTLS)
Gilles Peskine449bd832023-01-11 14:50:10 +01007805 if (ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
7806 ssl->handshake->flight != NULL) {
Jerry Yu2a9fff52022-02-17 14:28:51 +08007807 /* Cancel handshake timer */
Gilles Peskine449bd832023-01-11 14:50:10 +01007808 mbedtls_ssl_set_timer(ssl, 0);
Jerry Yu2a9fff52022-02-17 14:28:51 +08007809
7810 /* Keep last flight around in case we need to resend it:
7811 * we need the handshake and transform structures for that */
Gilles Peskine449bd832023-01-11 14:50:10 +01007812 MBEDTLS_SSL_DEBUG_MSG(3, ("skip freeing handshake and transform"));
7813 } else
Jerry Yu2a9fff52022-02-17 14:28:51 +08007814#endif
Gilles Peskine449bd832023-01-11 14:50:10 +01007815 mbedtls_ssl_handshake_wrapup_free_hs_transform(ssl);
Jerry Yu2a9fff52022-02-17 14:28:51 +08007816
Jerry Yu5ed73ff2022-10-27 13:08:42 +08007817 ssl->state = MBEDTLS_SSL_HANDSHAKE_OVER;
Jerry Yu2a9fff52022-02-17 14:28:51 +08007818
Gilles Peskine449bd832023-01-11 14:50:10 +01007819 MBEDTLS_SSL_DEBUG_MSG(3, ("<= handshake wrapup"));
Jerry Yu2a9fff52022-02-17 14:28:51 +08007820}
7821
Gilles Peskine449bd832023-01-11 14:50:10 +01007822int mbedtls_ssl_write_finished(mbedtls_ssl_context *ssl)
Jerry Yu3c8e47b2022-02-17 14:30:01 +08007823{
7824 int ret, hash_len;
7825
Gilles Peskine449bd832023-01-11 14:50:10 +01007826 MBEDTLS_SSL_DEBUG_MSG(2, ("=> write finished"));
Jerry Yu3c8e47b2022-02-17 14:30:01 +08007827
Gilles Peskine449bd832023-01-11 14:50:10 +01007828 mbedtls_ssl_update_out_pointers(ssl, ssl->transform_negotiate);
Jerry Yu3c8e47b2022-02-17 14:30:01 +08007829
Manuel Pégourié-Gonnardb8b07aa2023-02-06 00:34:21 +01007830 ret = ssl->handshake->calc_finished(ssl, ssl->out_msg + 4, ssl->conf->endpoint);
7831 if (ret != 0) {
7832 MBEDTLS_SSL_DEBUG_RET(1, "calc_finished", ret);
7833 }
Jerry Yu3c8e47b2022-02-17 14:30:01 +08007834
7835 /*
7836 * RFC 5246 7.4.9 (Page 63) says 12 is the default length and ciphersuites
7837 * may define some other value. Currently (early 2016), no defined
7838 * ciphersuite does this (and this is unlikely to change as activity has
7839 * moved to TLS 1.3 now) so we can keep the hardcoded 12 here.
7840 */
7841 hash_len = 12;
7842
7843#if defined(MBEDTLS_SSL_RENEGOTIATION)
7844 ssl->verify_data_len = hash_len;
Gilles Peskine449bd832023-01-11 14:50:10 +01007845 memcpy(ssl->own_verify_data, ssl->out_msg + 4, hash_len);
Jerry Yu3c8e47b2022-02-17 14:30:01 +08007846#endif
7847
7848 ssl->out_msglen = 4 + hash_len;
7849 ssl->out_msgtype = MBEDTLS_SSL_MSG_HANDSHAKE;
7850 ssl->out_msg[0] = MBEDTLS_SSL_HS_FINISHED;
7851
7852 /*
7853 * In case of session resuming, invert the client and server
7854 * ChangeCipherSpec messages order.
7855 */
Gilles Peskine449bd832023-01-11 14:50:10 +01007856 if (ssl->handshake->resume != 0) {
Jerry Yu3c8e47b2022-02-17 14:30:01 +08007857#if defined(MBEDTLS_SSL_CLI_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01007858 if (ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT) {
Jerry Yu3c8e47b2022-02-17 14:30:01 +08007859 ssl->state = MBEDTLS_SSL_HANDSHAKE_WRAPUP;
Gilles Peskine449bd832023-01-11 14:50:10 +01007860 }
Jerry Yu3c8e47b2022-02-17 14:30:01 +08007861#endif
7862#if defined(MBEDTLS_SSL_SRV_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01007863 if (ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER) {
Jerry Yu3c8e47b2022-02-17 14:30:01 +08007864 ssl->state = MBEDTLS_SSL_CLIENT_CHANGE_CIPHER_SPEC;
Gilles Peskine449bd832023-01-11 14:50:10 +01007865 }
Jerry Yu3c8e47b2022-02-17 14:30:01 +08007866#endif
Gilles Peskine449bd832023-01-11 14:50:10 +01007867 } else {
Jerry Yu3c8e47b2022-02-17 14:30:01 +08007868 ssl->state++;
Gilles Peskine449bd832023-01-11 14:50:10 +01007869 }
Jerry Yu3c8e47b2022-02-17 14:30:01 +08007870
7871 /*
7872 * Switch to our negotiated transform and session parameters for outbound
7873 * data.
7874 */
Gilles Peskine449bd832023-01-11 14:50:10 +01007875 MBEDTLS_SSL_DEBUG_MSG(3, ("switching to new transform spec for outbound data"));
Jerry Yu3c8e47b2022-02-17 14:30:01 +08007876
7877#if defined(MBEDTLS_SSL_PROTO_DTLS)
Gilles Peskine449bd832023-01-11 14:50:10 +01007878 if (ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM) {
Jerry Yu3c8e47b2022-02-17 14:30:01 +08007879 unsigned char i;
7880
7881 /* Remember current epoch settings for resending */
7882 ssl->handshake->alt_transform_out = ssl->transform_out;
Gilles Peskine449bd832023-01-11 14:50:10 +01007883 memcpy(ssl->handshake->alt_out_ctr, ssl->cur_out_ctr,
7884 sizeof(ssl->handshake->alt_out_ctr));
Jerry Yu3c8e47b2022-02-17 14:30:01 +08007885
7886 /* Set sequence_number to zero */
Gilles Peskine449bd832023-01-11 14:50:10 +01007887 memset(&ssl->cur_out_ctr[2], 0, sizeof(ssl->cur_out_ctr) - 2);
Jerry Yu3c8e47b2022-02-17 14:30:01 +08007888
7889
7890 /* Increment epoch */
Gilles Peskine449bd832023-01-11 14:50:10 +01007891 for (i = 2; i > 0; i--) {
7892 if (++ssl->cur_out_ctr[i - 1] != 0) {
Jerry Yu3c8e47b2022-02-17 14:30:01 +08007893 break;
Gilles Peskine449bd832023-01-11 14:50:10 +01007894 }
7895 }
Jerry Yu3c8e47b2022-02-17 14:30:01 +08007896
7897 /* The loop goes to its end iff the counter is wrapping */
Gilles Peskine449bd832023-01-11 14:50:10 +01007898 if (i == 0) {
7899 MBEDTLS_SSL_DEBUG_MSG(1, ("DTLS epoch would wrap"));
7900 return MBEDTLS_ERR_SSL_COUNTER_WRAPPING;
Jerry Yu3c8e47b2022-02-17 14:30:01 +08007901 }
Gilles Peskine449bd832023-01-11 14:50:10 +01007902 } else
Jerry Yu3c8e47b2022-02-17 14:30:01 +08007903#endif /* MBEDTLS_SSL_PROTO_DTLS */
Gilles Peskine449bd832023-01-11 14:50:10 +01007904 memset(ssl->cur_out_ctr, 0, sizeof(ssl->cur_out_ctr));
Jerry Yu3c8e47b2022-02-17 14:30:01 +08007905
7906 ssl->transform_out = ssl->transform_negotiate;
7907 ssl->session_out = ssl->session_negotiate;
7908
7909#if defined(MBEDTLS_SSL_PROTO_DTLS)
Gilles Peskine449bd832023-01-11 14:50:10 +01007910 if (ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM) {
7911 mbedtls_ssl_send_flight_completed(ssl);
7912 }
Jerry Yu3c8e47b2022-02-17 14:30:01 +08007913#endif
7914
Gilles Peskine449bd832023-01-11 14:50:10 +01007915 if ((ret = mbedtls_ssl_write_handshake_msg(ssl)) != 0) {
7916 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ssl_write_handshake_msg", ret);
7917 return ret;
Jerry Yu3c8e47b2022-02-17 14:30:01 +08007918 }
7919
7920#if defined(MBEDTLS_SSL_PROTO_DTLS)
Gilles Peskine449bd832023-01-11 14:50:10 +01007921 if (ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
7922 (ret = mbedtls_ssl_flight_transmit(ssl)) != 0) {
7923 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ssl_flight_transmit", ret);
7924 return ret;
Jerry Yu3c8e47b2022-02-17 14:30:01 +08007925 }
7926#endif
7927
Gilles Peskine449bd832023-01-11 14:50:10 +01007928 MBEDTLS_SSL_DEBUG_MSG(2, ("<= write finished"));
Jerry Yu3c8e47b2022-02-17 14:30:01 +08007929
Gilles Peskine449bd832023-01-11 14:50:10 +01007930 return 0;
Jerry Yu3c8e47b2022-02-17 14:30:01 +08007931}
7932
Jerry Yu0b3d7c12022-02-17 14:30:51 +08007933#define SSL_MAX_HASH_LEN 12
7934
Gilles Peskine449bd832023-01-11 14:50:10 +01007935int mbedtls_ssl_parse_finished(mbedtls_ssl_context *ssl)
Jerry Yu0b3d7c12022-02-17 14:30:51 +08007936{
7937 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
7938 unsigned int hash_len = 12;
7939 unsigned char buf[SSL_MAX_HASH_LEN];
7940
Gilles Peskine449bd832023-01-11 14:50:10 +01007941 MBEDTLS_SSL_DEBUG_MSG(2, ("=> parse finished"));
Jerry Yu0b3d7c12022-02-17 14:30:51 +08007942
Manuel Pégourié-Gonnardb8b07aa2023-02-06 00:34:21 +01007943 ret = ssl->handshake->calc_finished(ssl, buf, ssl->conf->endpoint ^ 1);
7944 if (ret != 0) {
7945 MBEDTLS_SSL_DEBUG_RET(1, "calc_finished", ret);
7946 }
Jerry Yu0b3d7c12022-02-17 14:30:51 +08007947
Gilles Peskine449bd832023-01-11 14:50:10 +01007948 if ((ret = mbedtls_ssl_read_record(ssl, 1)) != 0) {
7949 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ssl_read_record", ret);
Jerry Yu0b3d7c12022-02-17 14:30:51 +08007950 goto exit;
7951 }
7952
Gilles Peskine449bd832023-01-11 14:50:10 +01007953 if (ssl->in_msgtype != MBEDTLS_SSL_MSG_HANDSHAKE) {
7954 MBEDTLS_SSL_DEBUG_MSG(1, ("bad finished message"));
7955 mbedtls_ssl_send_alert_message(ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
7956 MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE);
Jerry Yu0b3d7c12022-02-17 14:30:51 +08007957 ret = MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE;
7958 goto exit;
7959 }
7960
Gilles Peskine449bd832023-01-11 14:50:10 +01007961 if (ssl->in_msg[0] != MBEDTLS_SSL_HS_FINISHED) {
7962 mbedtls_ssl_send_alert_message(ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
7963 MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE);
Jerry Yu0b3d7c12022-02-17 14:30:51 +08007964 ret = MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE;
7965 goto exit;
7966 }
7967
Gilles Peskine449bd832023-01-11 14:50:10 +01007968 if (ssl->in_hslen != mbedtls_ssl_hs_hdr_len(ssl) + hash_len) {
7969 MBEDTLS_SSL_DEBUG_MSG(1, ("bad finished message"));
7970 mbedtls_ssl_send_alert_message(ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
7971 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR);
Jerry Yu0b3d7c12022-02-17 14:30:51 +08007972 ret = MBEDTLS_ERR_SSL_DECODE_ERROR;
7973 goto exit;
7974 }
7975
Gilles Peskine449bd832023-01-11 14:50:10 +01007976 if (mbedtls_ct_memcmp(ssl->in_msg + mbedtls_ssl_hs_hdr_len(ssl),
7977 buf, hash_len) != 0) {
7978 MBEDTLS_SSL_DEBUG_MSG(1, ("bad finished message"));
7979 mbedtls_ssl_send_alert_message(ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
7980 MBEDTLS_SSL_ALERT_MSG_DECRYPT_ERROR);
Jerry Yu0b3d7c12022-02-17 14:30:51 +08007981 ret = MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE;
7982 goto exit;
7983 }
7984
7985#if defined(MBEDTLS_SSL_RENEGOTIATION)
7986 ssl->verify_data_len = hash_len;
Gilles Peskine449bd832023-01-11 14:50:10 +01007987 memcpy(ssl->peer_verify_data, buf, hash_len);
Jerry Yu0b3d7c12022-02-17 14:30:51 +08007988#endif
7989
Gilles Peskine449bd832023-01-11 14:50:10 +01007990 if (ssl->handshake->resume != 0) {
Jerry Yu0b3d7c12022-02-17 14:30:51 +08007991#if defined(MBEDTLS_SSL_CLI_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01007992 if (ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT) {
Jerry Yu0b3d7c12022-02-17 14:30:51 +08007993 ssl->state = MBEDTLS_SSL_CLIENT_CHANGE_CIPHER_SPEC;
Gilles Peskine449bd832023-01-11 14:50:10 +01007994 }
Jerry Yu0b3d7c12022-02-17 14:30:51 +08007995#endif
7996#if defined(MBEDTLS_SSL_SRV_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01007997 if (ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER) {
Jerry Yu0b3d7c12022-02-17 14:30:51 +08007998 ssl->state = MBEDTLS_SSL_HANDSHAKE_WRAPUP;
Gilles Peskine449bd832023-01-11 14:50:10 +01007999 }
Jerry Yu0b3d7c12022-02-17 14:30:51 +08008000#endif
Gilles Peskine449bd832023-01-11 14:50:10 +01008001 } else {
Jerry Yu0b3d7c12022-02-17 14:30:51 +08008002 ssl->state++;
Gilles Peskine449bd832023-01-11 14:50:10 +01008003 }
Jerry Yu0b3d7c12022-02-17 14:30:51 +08008004
8005#if defined(MBEDTLS_SSL_PROTO_DTLS)
Gilles Peskine449bd832023-01-11 14:50:10 +01008006 if (ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM) {
8007 mbedtls_ssl_recv_flight_completed(ssl);
8008 }
Jerry Yu0b3d7c12022-02-17 14:30:51 +08008009#endif
8010
Gilles Peskine449bd832023-01-11 14:50:10 +01008011 MBEDTLS_SSL_DEBUG_MSG(2, ("<= parse finished"));
Jerry Yu0b3d7c12022-02-17 14:30:51 +08008012
8013exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01008014 mbedtls_platform_zeroize(buf, hash_len);
8015 return ret;
Jerry Yu0b3d7c12022-02-17 14:30:51 +08008016}
8017
Jerry Yu392112c2022-02-17 14:34:10 +08008018#if defined(MBEDTLS_SSL_CONTEXT_SERIALIZATION)
8019/*
8020 * Helper to get TLS 1.2 PRF from ciphersuite
8021 * (Duplicates bits of logic from ssl_set_handshake_prfs().)
8022 */
Gilles Peskine449bd832023-01-11 14:50:10 +01008023static tls_prf_fn ssl_tls12prf_from_cs(int ciphersuite_id)
Jerry Yu392112c2022-02-17 14:34:10 +08008024{
Jerry Yu392112c2022-02-17 14:34:10 +08008025 const mbedtls_ssl_ciphersuite_t * const ciphersuite_info =
Gilles Peskine449bd832023-01-11 14:50:10 +01008026 mbedtls_ssl_ciphersuite_from_id(ciphersuite_id);
Andrzej Kurek68327742022-10-03 06:18:18 -04008027#if defined(MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Gilles Peskine449bd832023-01-11 14:50:10 +01008028 if (ciphersuite_info != NULL && ciphersuite_info->mac == MBEDTLS_MD_SHA384) {
8029 return tls_prf_sha384;
8030 } else
Jerry Yu392112c2022-02-17 14:34:10 +08008031#endif
Andrzej Kurek894edde2022-09-29 06:31:14 -04008032#if defined(MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
8033 {
Gilles Peskine449bd832023-01-11 14:50:10 +01008034 if (ciphersuite_info != NULL && ciphersuite_info->mac == MBEDTLS_MD_SHA256) {
8035 return tls_prf_sha256;
8036 }
Andrzej Kurek894edde2022-09-29 06:31:14 -04008037 }
8038#endif
8039#if !defined(MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA) && \
8040 !defined(MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
8041 (void) ciphersuite_info;
8042#endif
Andrzej Kurekeabeb302022-10-17 07:52:51 -04008043
Gilles Peskine449bd832023-01-11 14:50:10 +01008044 return NULL;
Jerry Yu392112c2022-02-17 14:34:10 +08008045}
8046#endif /* MBEDTLS_SSL_CONTEXT_SERIALIZATION */
Jerry Yue93ffcd2022-02-17 14:37:06 +08008047
Gilles Peskine449bd832023-01-11 14:50:10 +01008048static mbedtls_tls_prf_types tls_prf_get_type(mbedtls_ssl_tls_prf_cb *tls_prf)
Jerry Yue93ffcd2022-02-17 14:37:06 +08008049{
8050 ((void) tls_prf);
Andrzej Kurek25f27152022-08-17 16:09:31 -04008051#if defined(MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Gilles Peskine449bd832023-01-11 14:50:10 +01008052 if (tls_prf == tls_prf_sha384) {
8053 return MBEDTLS_SSL_TLS_PRF_SHA384;
8054 } else
Jerry Yue93ffcd2022-02-17 14:37:06 +08008055#endif
Andrzej Kurek25f27152022-08-17 16:09:31 -04008056#if defined(MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Gilles Peskine449bd832023-01-11 14:50:10 +01008057 if (tls_prf == tls_prf_sha256) {
8058 return MBEDTLS_SSL_TLS_PRF_SHA256;
8059 } else
Jerry Yue93ffcd2022-02-17 14:37:06 +08008060#endif
Gilles Peskine449bd832023-01-11 14:50:10 +01008061 return MBEDTLS_SSL_TLS_PRF_NONE;
Jerry Yue93ffcd2022-02-17 14:37:06 +08008062}
8063
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008064/*
8065 * Populate a transform structure with session keys and all the other
8066 * necessary information.
8067 *
8068 * Parameters:
8069 * - [in/out]: transform: structure to populate
8070 * [in] must be just initialised with mbedtls_ssl_transform_init()
8071 * [out] fully populated, ready for use by mbedtls_ssl_{en,de}crypt_buf()
8072 * - [in] ciphersuite
8073 * - [in] master
8074 * - [in] encrypt_then_mac
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008075 * - [in] tls_prf: pointer to PRF to use for key derivation
8076 * - [in] randbytes: buffer holding ServerHello.random + ClientHello.random
Glenn Strauss07c64162022-03-14 12:34:51 -04008077 * - [in] tls_version: TLS version
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008078 * - [in] endpoint: client or server
8079 * - [in] ssl: used for:
8080 * - ssl->conf->{f,p}_export_keys
8081 * [in] optionally used for:
8082 * - MBEDTLS_DEBUG_C: ssl->conf->{f,p}_dbg
8083 */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02008084MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01008085static int ssl_tls12_populate_transform(mbedtls_ssl_transform *transform,
8086 int ciphersuite,
8087 const unsigned char master[48],
Neil Armstrongf2c82f02022-04-05 11:16:53 +02008088#if defined(MBEDTLS_SSL_SOME_SUITES_USE_CBC_ETM)
Gilles Peskine449bd832023-01-11 14:50:10 +01008089 int encrypt_then_mac,
Neil Armstrongf2c82f02022-04-05 11:16:53 +02008090#endif /* MBEDTLS_SSL_SOME_SUITES_USE_CBC_ETM */
Gilles Peskine449bd832023-01-11 14:50:10 +01008091 ssl_tls_prf_t tls_prf,
8092 const unsigned char randbytes[64],
8093 mbedtls_ssl_protocol_version tls_version,
8094 unsigned endpoint,
8095 const mbedtls_ssl_context *ssl)
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008096{
8097 int ret = 0;
8098 unsigned char keyblk[256];
8099 unsigned char *key1;
8100 unsigned char *key2;
8101 unsigned char *mac_enc;
8102 unsigned char *mac_dec;
8103 size_t mac_key_len = 0;
8104 size_t iv_copy_len;
8105 size_t keylen;
8106 const mbedtls_ssl_ciphersuite_t *ciphersuite_info;
Neil Armstrong7fea33e2022-04-01 15:40:25 +02008107 mbedtls_ssl_mode_t ssl_mode;
Neil Armstronge4512952022-03-08 09:08:22 +01008108#if !defined(MBEDTLS_USE_PSA_CRYPTO)
Neil Armstronga0eeb7f2022-04-01 17:36:10 +02008109 const mbedtls_cipher_info_t *cipher_info;
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008110 const mbedtls_md_info_t *md_info;
Neil Armstronge4512952022-03-08 09:08:22 +01008111#endif /* !MBEDTLS_USE_PSA_CRYPTO */
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008112
8113#if defined(MBEDTLS_USE_PSA_CRYPTO)
8114 psa_key_type_t key_type;
8115 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
8116 psa_algorithm_t alg;
Neil Armstronge4512952022-03-08 09:08:22 +01008117 psa_algorithm_t mac_alg = 0;
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008118 size_t key_bits;
8119 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
8120#endif
8121
8122#if !defined(MBEDTLS_DEBUG_C) && \
8123 !defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Gilles Peskine449bd832023-01-11 14:50:10 +01008124 if (ssl->f_export_keys == NULL) {
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008125 ssl = NULL; /* make sure we don't use it except for these cases */
8126 (void) ssl;
8127 }
8128#endif
8129
8130 /*
8131 * Some data just needs copying into the structure
8132 */
Neil Armstrongf2c82f02022-04-05 11:16:53 +02008133#if defined(MBEDTLS_SSL_SOME_SUITES_USE_CBC_ETM)
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008134 transform->encrypt_then_mac = encrypt_then_mac;
Neil Armstrongf2c82f02022-04-05 11:16:53 +02008135#endif /* MBEDTLS_SSL_SOME_SUITES_USE_CBC_ETM */
Glenn Strauss07c64162022-03-14 12:34:51 -04008136 transform->tls_version = tls_version;
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008137
8138#if defined(MBEDTLS_SSL_CONTEXT_SERIALIZATION)
Gilles Peskine449bd832023-01-11 14:50:10 +01008139 memcpy(transform->randbytes, randbytes, sizeof(transform->randbytes));
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008140#endif
8141
8142#if defined(MBEDTLS_SSL_PROTO_TLS1_3)
Gilles Peskine449bd832023-01-11 14:50:10 +01008143 if (tls_version == MBEDTLS_SSL_VERSION_TLS1_3) {
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008144 /* At the moment, we keep TLS <= 1.2 and TLS 1.3 transform
8145 * generation separate. This should never happen. */
Gilles Peskine449bd832023-01-11 14:50:10 +01008146 return MBEDTLS_ERR_SSL_INTERNAL_ERROR;
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008147 }
8148#endif /* MBEDTLS_SSL_PROTO_TLS1_3 */
8149
8150 /*
8151 * Get various info structures
8152 */
Gilles Peskine449bd832023-01-11 14:50:10 +01008153 ciphersuite_info = mbedtls_ssl_ciphersuite_from_id(ciphersuite);
8154 if (ciphersuite_info == NULL) {
8155 MBEDTLS_SSL_DEBUG_MSG(1, ("ciphersuite info for %d not found",
8156 ciphersuite));
8157 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008158 }
8159
Neil Armstrongab555e02022-04-04 11:07:59 +02008160 ssl_mode = mbedtls_ssl_get_mode_from_ciphersuite(
Neil Armstrongf2c82f02022-04-05 11:16:53 +02008161#if defined(MBEDTLS_SSL_SOME_SUITES_USE_CBC_ETM)
Gilles Peskine449bd832023-01-11 14:50:10 +01008162 encrypt_then_mac,
Neil Armstrongf2c82f02022-04-05 11:16:53 +02008163#endif /* MBEDTLS_SSL_SOME_SUITES_USE_CBC_ETM */
Gilles Peskine449bd832023-01-11 14:50:10 +01008164 ciphersuite_info);
Neil Armstrong7fea33e2022-04-01 15:40:25 +02008165
Gilles Peskine449bd832023-01-11 14:50:10 +01008166 if (ssl_mode == MBEDTLS_SSL_MODE_AEAD) {
Neil Armstronga0eeb7f2022-04-01 17:36:10 +02008167 transform->taglen =
8168 ciphersuite_info->flags & MBEDTLS_CIPHERSUITE_SHORT_TAG ? 8 : 16;
Gilles Peskine449bd832023-01-11 14:50:10 +01008169 }
Neil Armstronga0eeb7f2022-04-01 17:36:10 +02008170
8171#if defined(MBEDTLS_USE_PSA_CRYPTO)
Gilles Peskine449bd832023-01-11 14:50:10 +01008172 if ((status = mbedtls_ssl_cipher_to_psa(ciphersuite_info->cipher,
8173 transform->taglen,
8174 &alg,
8175 &key_type,
8176 &key_bits)) != PSA_SUCCESS) {
8177 ret = psa_ssl_status_to_mbedtls(status);
8178 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ssl_cipher_to_psa", ret);
Neil Armstronga0eeb7f2022-04-01 17:36:10 +02008179 goto end;
8180 }
8181#else
Gilles Peskine449bd832023-01-11 14:50:10 +01008182 cipher_info = mbedtls_cipher_info_from_type(ciphersuite_info->cipher);
8183 if (cipher_info == NULL) {
8184 MBEDTLS_SSL_DEBUG_MSG(1, ("cipher info for %u not found",
8185 ciphersuite_info->cipher));
8186 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008187 }
Neil Armstronga0eeb7f2022-04-01 17:36:10 +02008188#endif /* MBEDTLS_USE_PSA_CRYPTO */
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008189
Neil Armstronge4512952022-03-08 09:08:22 +01008190#if defined(MBEDTLS_USE_PSA_CRYPTO)
Gilles Peskine449bd832023-01-11 14:50:10 +01008191 mac_alg = mbedtls_hash_info_psa_from_md(ciphersuite_info->mac);
8192 if (mac_alg == 0) {
8193 MBEDTLS_SSL_DEBUG_MSG(1, ("mbedtls_hash_info_psa_from_md for %u not found",
8194 (unsigned) ciphersuite_info->mac));
8195 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
Neil Armstronge4512952022-03-08 09:08:22 +01008196 }
8197#else
Gilles Peskine449bd832023-01-11 14:50:10 +01008198 md_info = mbedtls_md_info_from_type(ciphersuite_info->mac);
8199 if (md_info == NULL) {
8200 MBEDTLS_SSL_DEBUG_MSG(1, ("mbedtls_md info for %u not found",
8201 (unsigned) ciphersuite_info->mac));
8202 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008203 }
Neil Armstronge4512952022-03-08 09:08:22 +01008204#endif /* MBEDTLS_USE_PSA_CRYPTO */
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008205
8206#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
8207 /* Copy own and peer's CID if the use of the CID
8208 * extension has been negotiated. */
Gilles Peskine449bd832023-01-11 14:50:10 +01008209 if (ssl->handshake->cid_in_use == MBEDTLS_SSL_CID_ENABLED) {
8210 MBEDTLS_SSL_DEBUG_MSG(3, ("Copy CIDs into SSL transform"));
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008211
8212 transform->in_cid_len = ssl->own_cid_len;
Gilles Peskine449bd832023-01-11 14:50:10 +01008213 memcpy(transform->in_cid, ssl->own_cid, ssl->own_cid_len);
8214 MBEDTLS_SSL_DEBUG_BUF(3, "Incoming CID", transform->in_cid,
8215 transform->in_cid_len);
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008216
8217 transform->out_cid_len = ssl->handshake->peer_cid_len;
Gilles Peskine449bd832023-01-11 14:50:10 +01008218 memcpy(transform->out_cid, ssl->handshake->peer_cid,
8219 ssl->handshake->peer_cid_len);
8220 MBEDTLS_SSL_DEBUG_BUF(3, "Outgoing CID", transform->out_cid,
8221 transform->out_cid_len);
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008222 }
8223#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
8224
8225 /*
8226 * Compute key block using the PRF
8227 */
Gilles Peskine449bd832023-01-11 14:50:10 +01008228 ret = tls_prf(master, 48, "key expansion", randbytes, 64, keyblk, 256);
8229 if (ret != 0) {
8230 MBEDTLS_SSL_DEBUG_RET(1, "prf", ret);
8231 return ret;
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008232 }
8233
Gilles Peskine449bd832023-01-11 14:50:10 +01008234 MBEDTLS_SSL_DEBUG_MSG(3, ("ciphersuite = %s",
8235 mbedtls_ssl_get_ciphersuite_name(ciphersuite)));
8236 MBEDTLS_SSL_DEBUG_BUF(3, "master secret", master, 48);
8237 MBEDTLS_SSL_DEBUG_BUF(4, "random bytes", randbytes, 64);
8238 MBEDTLS_SSL_DEBUG_BUF(4, "key block", keyblk, 256);
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008239
8240 /*
8241 * Determine the appropriate key, IV and MAC length.
8242 */
8243
Neil Armstronga0eeb7f2022-04-01 17:36:10 +02008244#if defined(MBEDTLS_USE_PSA_CRYPTO)
8245 keylen = PSA_BITS_TO_BYTES(key_bits);
8246#else
Gilles Peskine449bd832023-01-11 14:50:10 +01008247 keylen = mbedtls_cipher_info_get_key_bitlen(cipher_info) / 8;
Neil Armstronga0eeb7f2022-04-01 17:36:10 +02008248#endif
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008249
8250#if defined(MBEDTLS_GCM_C) || \
8251 defined(MBEDTLS_CCM_C) || \
8252 defined(MBEDTLS_CHACHAPOLY_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01008253 if (ssl_mode == MBEDTLS_SSL_MODE_AEAD) {
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008254 size_t explicit_ivlen;
8255
8256 transform->maclen = 0;
8257 mac_key_len = 0;
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008258
8259 /* All modes haves 96-bit IVs, but the length of the static parts vary
8260 * with mode and version:
8261 * - For GCM and CCM in TLS 1.2, there's a static IV of 4 Bytes
8262 * (to be concatenated with a dynamically chosen IV of 8 Bytes)
8263 * - For ChaChaPoly in TLS 1.2, and all modes in TLS 1.3, there's
8264 * a static IV of 12 Bytes (to be XOR'ed with the 8 Byte record
8265 * sequence number).
8266 */
8267 transform->ivlen = 12;
David Horstmann3b2276a2022-10-06 14:49:08 +01008268
8269 int is_chachapoly = 0;
Neil Armstronga0eeb7f2022-04-01 17:36:10 +02008270#if defined(MBEDTLS_USE_PSA_CRYPTO)
Gilles Peskine449bd832023-01-11 14:50:10 +01008271 is_chachapoly = (key_type == PSA_KEY_TYPE_CHACHA20);
Neil Armstronga0eeb7f2022-04-01 17:36:10 +02008272#else
Gilles Peskine449bd832023-01-11 14:50:10 +01008273 is_chachapoly = (mbedtls_cipher_info_get_mode(cipher_info)
8274 == MBEDTLS_MODE_CHACHAPOLY);
Neil Armstronga0eeb7f2022-04-01 17:36:10 +02008275#endif /* MBEDTLS_USE_PSA_CRYPTO */
David Horstmann3b2276a2022-10-06 14:49:08 +01008276
Gilles Peskine449bd832023-01-11 14:50:10 +01008277 if (is_chachapoly) {
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008278 transform->fixed_ivlen = 12;
Gilles Peskine449bd832023-01-11 14:50:10 +01008279 } else {
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008280 transform->fixed_ivlen = 4;
Gilles Peskine449bd832023-01-11 14:50:10 +01008281 }
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008282
8283 /* Minimum length of encrypted record */
8284 explicit_ivlen = transform->ivlen - transform->fixed_ivlen;
8285 transform->minlen = explicit_ivlen + transform->taglen;
Gilles Peskine449bd832023-01-11 14:50:10 +01008286 } else
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008287#endif /* MBEDTLS_GCM_C || MBEDTLS_CCM_C || MBEDTLS_CHACHAPOLY_C */
8288#if defined(MBEDTLS_SSL_SOME_SUITES_USE_MAC)
Gilles Peskine449bd832023-01-11 14:50:10 +01008289 if (ssl_mode == MBEDTLS_SSL_MODE_STREAM ||
Neil Armstrong7fea33e2022-04-01 15:40:25 +02008290 ssl_mode == MBEDTLS_SSL_MODE_CBC ||
Gilles Peskine449bd832023-01-11 14:50:10 +01008291 ssl_mode == MBEDTLS_SSL_MODE_CBC_ETM) {
Neil Armstronge4512952022-03-08 09:08:22 +01008292#if defined(MBEDTLS_USE_PSA_CRYPTO)
Gilles Peskine449bd832023-01-11 14:50:10 +01008293 size_t block_size = PSA_BLOCK_CIPHER_BLOCK_LENGTH(key_type);
Neil Armstronga0eeb7f2022-04-01 17:36:10 +02008294#else
8295 size_t block_size = cipher_info->block_size;
8296#endif /* MBEDTLS_USE_PSA_CRYPTO */
8297
8298#if defined(MBEDTLS_USE_PSA_CRYPTO)
Neil Armstronge4512952022-03-08 09:08:22 +01008299 /* Get MAC length */
8300 mac_key_len = PSA_HASH_LENGTH(mac_alg);
8301#else
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008302 /* Initialize HMAC contexts */
Gilles Peskine449bd832023-01-11 14:50:10 +01008303 if ((ret = mbedtls_md_setup(&transform->md_ctx_enc, md_info, 1)) != 0 ||
8304 (ret = mbedtls_md_setup(&transform->md_ctx_dec, md_info, 1)) != 0) {
8305 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_md_setup", ret);
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008306 goto end;
8307 }
8308
8309 /* Get MAC length */
Gilles Peskine449bd832023-01-11 14:50:10 +01008310 mac_key_len = mbedtls_md_get_size(md_info);
Neil Armstronge4512952022-03-08 09:08:22 +01008311#endif /* MBEDTLS_USE_PSA_CRYPTO */
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008312 transform->maclen = mac_key_len;
8313
8314 /* IV length */
Neil Armstronga0eeb7f2022-04-01 17:36:10 +02008315#if defined(MBEDTLS_USE_PSA_CRYPTO)
Gilles Peskine449bd832023-01-11 14:50:10 +01008316 transform->ivlen = PSA_CIPHER_IV_LENGTH(key_type, alg);
Neil Armstronga0eeb7f2022-04-01 17:36:10 +02008317#else
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008318 transform->ivlen = cipher_info->iv_size;
Neil Armstronga0eeb7f2022-04-01 17:36:10 +02008319#endif /* MBEDTLS_USE_PSA_CRYPTO */
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008320
8321 /* Minimum length */
Gilles Peskine449bd832023-01-11 14:50:10 +01008322 if (ssl_mode == MBEDTLS_SSL_MODE_STREAM) {
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008323 transform->minlen = transform->maclen;
Gilles Peskine449bd832023-01-11 14:50:10 +01008324 } else {
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008325 /*
8326 * GenericBlockCipher:
8327 * 1. if EtM is in use: one block plus MAC
8328 * otherwise: * first multiple of blocklen greater than maclen
8329 * 2. IV
8330 */
8331#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
Gilles Peskine449bd832023-01-11 14:50:10 +01008332 if (ssl_mode == MBEDTLS_SSL_MODE_CBC_ETM) {
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008333 transform->minlen = transform->maclen
Gilles Peskine449bd832023-01-11 14:50:10 +01008334 + block_size;
8335 } else
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008336#endif
8337 {
8338 transform->minlen = transform->maclen
Gilles Peskine449bd832023-01-11 14:50:10 +01008339 + block_size
8340 - transform->maclen % block_size;
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008341 }
8342
Gilles Peskine449bd832023-01-11 14:50:10 +01008343 if (tls_version == MBEDTLS_SSL_VERSION_TLS1_2) {
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008344 transform->minlen += transform->ivlen;
Gilles Peskine449bd832023-01-11 14:50:10 +01008345 } else {
8346 MBEDTLS_SSL_DEBUG_MSG(1, ("should never happen"));
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008347 ret = MBEDTLS_ERR_SSL_INTERNAL_ERROR;
8348 goto end;
8349 }
8350 }
Gilles Peskine449bd832023-01-11 14:50:10 +01008351 } else
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008352#endif /* MBEDTLS_SSL_SOME_SUITES_USE_MAC */
8353 {
Gilles Peskine449bd832023-01-11 14:50:10 +01008354 MBEDTLS_SSL_DEBUG_MSG(1, ("should never happen"));
8355 return MBEDTLS_ERR_SSL_INTERNAL_ERROR;
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008356 }
8357
Gilles Peskine449bd832023-01-11 14:50:10 +01008358 MBEDTLS_SSL_DEBUG_MSG(3, ("keylen: %u, minlen: %u, ivlen: %u, maclen: %u",
8359 (unsigned) keylen,
8360 (unsigned) transform->minlen,
8361 (unsigned) transform->ivlen,
8362 (unsigned) transform->maclen));
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008363
8364 /*
8365 * Finally setup the cipher contexts, IVs and MAC secrets.
8366 */
8367#if defined(MBEDTLS_SSL_CLI_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01008368 if (endpoint == MBEDTLS_SSL_IS_CLIENT) {
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008369 key1 = keyblk + mac_key_len * 2;
8370 key2 = keyblk + mac_key_len * 2 + keylen;
8371
8372 mac_enc = keyblk;
8373 mac_dec = keyblk + mac_key_len;
8374
Gilles Peskine449bd832023-01-11 14:50:10 +01008375 iv_copy_len = (transform->fixed_ivlen) ?
8376 transform->fixed_ivlen : transform->ivlen;
8377 memcpy(transform->iv_enc, key2 + keylen, iv_copy_len);
8378 memcpy(transform->iv_dec, key2 + keylen + iv_copy_len,
8379 iv_copy_len);
8380 } else
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008381#endif /* MBEDTLS_SSL_CLI_C */
8382#if defined(MBEDTLS_SSL_SRV_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01008383 if (endpoint == MBEDTLS_SSL_IS_SERVER) {
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008384 key1 = keyblk + mac_key_len * 2 + keylen;
8385 key2 = keyblk + mac_key_len * 2;
8386
8387 mac_enc = keyblk + mac_key_len;
8388 mac_dec = keyblk;
8389
Gilles Peskine449bd832023-01-11 14:50:10 +01008390 iv_copy_len = (transform->fixed_ivlen) ?
8391 transform->fixed_ivlen : transform->ivlen;
8392 memcpy(transform->iv_dec, key1 + keylen, iv_copy_len);
8393 memcpy(transform->iv_enc, key1 + keylen + iv_copy_len,
8394 iv_copy_len);
8395 } else
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008396#endif /* MBEDTLS_SSL_SRV_C */
8397 {
Gilles Peskine449bd832023-01-11 14:50:10 +01008398 MBEDTLS_SSL_DEBUG_MSG(1, ("should never happen"));
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008399 ret = MBEDTLS_ERR_SSL_INTERNAL_ERROR;
8400 goto end;
8401 }
8402
Gilles Peskine449bd832023-01-11 14:50:10 +01008403 if (ssl != NULL && ssl->f_export_keys != NULL) {
8404 ssl->f_export_keys(ssl->p_export_keys,
8405 MBEDTLS_SSL_KEY_EXPORT_TLS12_MASTER_SECRET,
8406 master, 48,
8407 randbytes + 32,
8408 randbytes,
8409 tls_prf_get_type(tls_prf));
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008410 }
8411
8412#if defined(MBEDTLS_USE_PSA_CRYPTO)
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008413 transform->psa_alg = alg;
8414
Gilles Peskine449bd832023-01-11 14:50:10 +01008415 if (alg != MBEDTLS_SSL_NULL_CIPHER) {
8416 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_ENCRYPT);
8417 psa_set_key_algorithm(&attributes, alg);
8418 psa_set_key_type(&attributes, key_type);
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008419
Gilles Peskine449bd832023-01-11 14:50:10 +01008420 if ((status = psa_import_key(&attributes,
8421 key1,
8422 PSA_BITS_TO_BYTES(key_bits),
8423 &transform->psa_key_enc)) != PSA_SUCCESS) {
8424 MBEDTLS_SSL_DEBUG_RET(3, "psa_import_key", (int) status);
8425 ret = psa_ssl_status_to_mbedtls(status);
8426 MBEDTLS_SSL_DEBUG_RET(1, "psa_import_key", ret);
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008427 goto end;
8428 }
8429
Gilles Peskine449bd832023-01-11 14:50:10 +01008430 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_DECRYPT);
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008431
Gilles Peskine449bd832023-01-11 14:50:10 +01008432 if ((status = psa_import_key(&attributes,
8433 key2,
8434 PSA_BITS_TO_BYTES(key_bits),
8435 &transform->psa_key_dec)) != PSA_SUCCESS) {
8436 ret = psa_ssl_status_to_mbedtls(status);
8437 MBEDTLS_SSL_DEBUG_RET(1, "psa_import_key", ret);
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008438 goto end;
8439 }
8440 }
8441#else
Gilles Peskine449bd832023-01-11 14:50:10 +01008442 if ((ret = mbedtls_cipher_setup(&transform->cipher_ctx_enc,
8443 cipher_info)) != 0) {
8444 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_cipher_setup", ret);
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008445 goto end;
8446 }
8447
Gilles Peskine449bd832023-01-11 14:50:10 +01008448 if ((ret = mbedtls_cipher_setup(&transform->cipher_ctx_dec,
8449 cipher_info)) != 0) {
8450 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_cipher_setup", ret);
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008451 goto end;
8452 }
8453
Gilles Peskine449bd832023-01-11 14:50:10 +01008454 if ((ret = mbedtls_cipher_setkey(&transform->cipher_ctx_enc, key1,
8455 (int) mbedtls_cipher_info_get_key_bitlen(cipher_info),
8456 MBEDTLS_ENCRYPT)) != 0) {
8457 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_cipher_setkey", ret);
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008458 goto end;
8459 }
8460
Gilles Peskine449bd832023-01-11 14:50:10 +01008461 if ((ret = mbedtls_cipher_setkey(&transform->cipher_ctx_dec, key2,
8462 (int) mbedtls_cipher_info_get_key_bitlen(cipher_info),
8463 MBEDTLS_DECRYPT)) != 0) {
8464 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_cipher_setkey", ret);
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008465 goto end;
8466 }
8467
8468#if defined(MBEDTLS_CIPHER_MODE_CBC)
Gilles Peskine449bd832023-01-11 14:50:10 +01008469 if (mbedtls_cipher_info_get_mode(cipher_info) == MBEDTLS_MODE_CBC) {
8470 if ((ret = mbedtls_cipher_set_padding_mode(&transform->cipher_ctx_enc,
8471 MBEDTLS_PADDING_NONE)) != 0) {
8472 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_cipher_set_padding_mode", ret);
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008473 goto end;
8474 }
8475
Gilles Peskine449bd832023-01-11 14:50:10 +01008476 if ((ret = mbedtls_cipher_set_padding_mode(&transform->cipher_ctx_dec,
8477 MBEDTLS_PADDING_NONE)) != 0) {
8478 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_cipher_set_padding_mode", ret);
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008479 goto end;
8480 }
8481 }
8482#endif /* MBEDTLS_CIPHER_MODE_CBC */
8483#endif /* MBEDTLS_USE_PSA_CRYPTO */
8484
Neil Armstrong29c0c042022-03-17 17:47:28 +01008485#if defined(MBEDTLS_SSL_SOME_SUITES_USE_MAC)
8486 /* For HMAC-based ciphersuites, initialize the HMAC transforms.
8487 For AEAD-based ciphersuites, there is nothing to do here. */
Gilles Peskine449bd832023-01-11 14:50:10 +01008488 if (mac_key_len != 0) {
Neil Armstrong29c0c042022-03-17 17:47:28 +01008489#if defined(MBEDTLS_USE_PSA_CRYPTO)
Gilles Peskine449bd832023-01-11 14:50:10 +01008490 transform->psa_mac_alg = PSA_ALG_HMAC(mac_alg);
Neil Armstrong29c0c042022-03-17 17:47:28 +01008491
Gilles Peskine449bd832023-01-11 14:50:10 +01008492 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_SIGN_MESSAGE);
8493 psa_set_key_algorithm(&attributes, PSA_ALG_HMAC(mac_alg));
8494 psa_set_key_type(&attributes, PSA_KEY_TYPE_HMAC);
Neil Armstrong29c0c042022-03-17 17:47:28 +01008495
Gilles Peskine449bd832023-01-11 14:50:10 +01008496 if ((status = psa_import_key(&attributes,
8497 mac_enc, mac_key_len,
8498 &transform->psa_mac_enc)) != PSA_SUCCESS) {
8499 ret = psa_ssl_status_to_mbedtls(status);
8500 MBEDTLS_SSL_DEBUG_RET(1, "psa_import_mac_key", ret);
Neil Armstrong29c0c042022-03-17 17:47:28 +01008501 goto end;
8502 }
8503
Gilles Peskine449bd832023-01-11 14:50:10 +01008504 if ((transform->psa_alg == MBEDTLS_SSL_NULL_CIPHER) ||
8505 ((transform->psa_alg == PSA_ALG_CBC_NO_PADDING)
Andrzej Kurek8c95ac42022-08-17 16:17:00 -04008506#if defined(MBEDTLS_SSL_SOME_SUITES_USE_CBC_ETM)
Gilles Peskine449bd832023-01-11 14:50:10 +01008507 && (transform->encrypt_then_mac == MBEDTLS_SSL_ETM_DISABLED)
Andrzej Kurek8c95ac42022-08-17 16:17:00 -04008508#endif
Gilles Peskine449bd832023-01-11 14:50:10 +01008509 )) {
Neil Armstrong29c0c042022-03-17 17:47:28 +01008510 /* mbedtls_ct_hmac() requires the key to be exportable */
Gilles Peskine449bd832023-01-11 14:50:10 +01008511 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_EXPORT |
8512 PSA_KEY_USAGE_VERIFY_HASH);
8513 } else {
8514 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_VERIFY_HASH);
8515 }
Neil Armstrong29c0c042022-03-17 17:47:28 +01008516
Gilles Peskine449bd832023-01-11 14:50:10 +01008517 if ((status = psa_import_key(&attributes,
8518 mac_dec, mac_key_len,
8519 &transform->psa_mac_dec)) != PSA_SUCCESS) {
8520 ret = psa_ssl_status_to_mbedtls(status);
8521 MBEDTLS_SSL_DEBUG_RET(1, "psa_import_mac_key", ret);
Neil Armstrong29c0c042022-03-17 17:47:28 +01008522 goto end;
8523 }
8524#else
Gilles Peskine449bd832023-01-11 14:50:10 +01008525 ret = mbedtls_md_hmac_starts(&transform->md_ctx_enc, mac_enc, mac_key_len);
8526 if (ret != 0) {
Neil Armstrong29c0c042022-03-17 17:47:28 +01008527 goto end;
Gilles Peskine449bd832023-01-11 14:50:10 +01008528 }
8529 ret = mbedtls_md_hmac_starts(&transform->md_ctx_dec, mac_dec, mac_key_len);
8530 if (ret != 0) {
Neil Armstrong29c0c042022-03-17 17:47:28 +01008531 goto end;
Gilles Peskine449bd832023-01-11 14:50:10 +01008532 }
Neil Armstrong29c0c042022-03-17 17:47:28 +01008533#endif /* MBEDTLS_USE_PSA_CRYPTO */
8534 }
8535#endif /* MBEDTLS_SSL_SOME_SUITES_USE_MAC */
8536
8537 ((void) mac_dec);
8538 ((void) mac_enc);
8539
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008540end:
Gilles Peskine449bd832023-01-11 14:50:10 +01008541 mbedtls_platform_zeroize(keyblk, sizeof(keyblk));
8542 return ret;
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008543}
8544
Valerio Settia08b1a42022-11-17 15:10:02 +01008545#if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED) && \
8546 defined(MBEDTLS_USE_PSA_CRYPTO)
Valerio Setti6b3dab02022-11-17 17:14:54 +01008547int mbedtls_psa_ecjpake_read_round(
Gilles Peskine449bd832023-01-11 14:50:10 +01008548 psa_pake_operation_t *pake_ctx,
8549 const unsigned char *buf,
8550 size_t len, mbedtls_ecjpake_rounds_t round)
Valerio Settia08b1a42022-11-17 15:10:02 +01008551{
8552 psa_status_t status;
8553 size_t input_offset = 0;
Valerio Setti819de862022-11-17 18:05:19 +01008554 /*
Valerio Setti6b3dab02022-11-17 17:14:54 +01008555 * At round one repeat the KEY_SHARE, ZK_PUBLIC & ZF_PROOF twice
8556 * At round two perform a single cycle
8557 */
Gilles Peskine449bd832023-01-11 14:50:10 +01008558 unsigned int remaining_steps = (round == MBEDTLS_ECJPAKE_ROUND_ONE) ? 2 : 1;
Valerio Settia08b1a42022-11-17 15:10:02 +01008559
Gilles Peskine449bd832023-01-11 14:50:10 +01008560 for (; remaining_steps > 0; remaining_steps--) {
8561 for (psa_pake_step_t step = PSA_PAKE_STEP_KEY_SHARE;
Valerio Settia08b1a42022-11-17 15:10:02 +01008562 step <= PSA_PAKE_STEP_ZK_PROOF;
Gilles Peskine449bd832023-01-11 14:50:10 +01008563 ++step) {
Valerio Settia08b1a42022-11-17 15:10:02 +01008564 /* Length is stored at the first byte */
8565 size_t length = buf[input_offset];
8566 input_offset += 1;
8567
Gilles Peskine449bd832023-01-11 14:50:10 +01008568 if (input_offset + length > len) {
Valerio Settia08b1a42022-11-17 15:10:02 +01008569 return MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE;
8570 }
8571
Gilles Peskine449bd832023-01-11 14:50:10 +01008572 status = psa_pake_input(pake_ctx, step,
8573 buf + input_offset, length);
8574 if (status != PSA_SUCCESS) {
8575 return psa_ssl_status_to_mbedtls(status);
Valerio Settia08b1a42022-11-17 15:10:02 +01008576 }
8577
8578 input_offset += length;
8579 }
8580 }
8581
Gilles Peskine449bd832023-01-11 14:50:10 +01008582 if (input_offset != len) {
Valerio Setti61ea17d2022-11-18 12:11:00 +01008583 return MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE;
Gilles Peskine449bd832023-01-11 14:50:10 +01008584 }
Valerio Setti30ebe112022-11-17 16:23:34 +01008585
Gilles Peskine449bd832023-01-11 14:50:10 +01008586 return 0;
Valerio Settia08b1a42022-11-17 15:10:02 +01008587}
8588
Valerio Setti6b3dab02022-11-17 17:14:54 +01008589int mbedtls_psa_ecjpake_write_round(
Gilles Peskine449bd832023-01-11 14:50:10 +01008590 psa_pake_operation_t *pake_ctx,
8591 unsigned char *buf,
8592 size_t len, size_t *olen,
8593 mbedtls_ecjpake_rounds_t round)
Valerio Settia08b1a42022-11-17 15:10:02 +01008594{
8595 psa_status_t status;
8596 size_t output_offset = 0;
8597 size_t output_len;
Valerio Setti819de862022-11-17 18:05:19 +01008598 /*
Valerio Setti6b3dab02022-11-17 17:14:54 +01008599 * At round one repeat the KEY_SHARE, ZK_PUBLIC & ZF_PROOF twice
8600 * At round two perform a single cycle
8601 */
Gilles Peskine449bd832023-01-11 14:50:10 +01008602 unsigned int remaining_steps = (round == MBEDTLS_ECJPAKE_ROUND_ONE) ? 2 : 1;
Valerio Settia08b1a42022-11-17 15:10:02 +01008603
Gilles Peskine449bd832023-01-11 14:50:10 +01008604 for (; remaining_steps > 0; remaining_steps--) {
8605 for (psa_pake_step_t step = PSA_PAKE_STEP_KEY_SHARE;
8606 step <= PSA_PAKE_STEP_ZK_PROOF;
8607 ++step) {
Valerio Setti79f6b6b2022-11-21 14:17:03 +01008608 /*
Valerio Settid4a9b1a2022-11-22 11:11:10 +01008609 * For each step, prepend 1 byte with the length of the data as
8610 * given by psa_pake_output().
Valerio Setti79f6b6b2022-11-21 14:17:03 +01008611 */
Gilles Peskine449bd832023-01-11 14:50:10 +01008612 status = psa_pake_output(pake_ctx, step,
8613 buf + output_offset + 1,
8614 len - output_offset - 1,
8615 &output_len);
8616 if (status != PSA_SUCCESS) {
8617 return psa_ssl_status_to_mbedtls(status);
Valerio Settia08b1a42022-11-17 15:10:02 +01008618 }
8619
Valerio Setti99d88c12022-11-22 16:03:43 +01008620 *(buf + output_offset) = (uint8_t) output_len;
Valerio Setti79f6b6b2022-11-21 14:17:03 +01008621
8622 output_offset += output_len + 1;
Valerio Settia08b1a42022-11-17 15:10:02 +01008623 }
8624 }
8625
8626 *olen = output_offset;
8627
Gilles Peskine449bd832023-01-11 14:50:10 +01008628 return 0;
Valerio Settia08b1a42022-11-17 15:10:02 +01008629}
Valerio Settia08b1a42022-11-17 15:10:02 +01008630#endif //MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED && MBEDTLS_USE_PSA_CRYPTO
8631
Jerry Yuee40f9d2022-02-17 14:55:16 +08008632#if defined(MBEDTLS_USE_PSA_CRYPTO)
Gilles Peskine449bd832023-01-11 14:50:10 +01008633int mbedtls_ssl_get_key_exchange_md_tls1_2(mbedtls_ssl_context *ssl,
8634 unsigned char *hash, size_t *hashlen,
8635 unsigned char *data, size_t data_len,
8636 mbedtls_md_type_t md_alg)
Jerry Yuee40f9d2022-02-17 14:55:16 +08008637{
8638 psa_status_t status;
8639 psa_hash_operation_t hash_operation = PSA_HASH_OPERATION_INIT;
Gilles Peskine449bd832023-01-11 14:50:10 +01008640 psa_algorithm_t hash_alg = mbedtls_hash_info_psa_from_md(md_alg);
Jerry Yuee40f9d2022-02-17 14:55:16 +08008641
Gilles Peskine449bd832023-01-11 14:50:10 +01008642 MBEDTLS_SSL_DEBUG_MSG(3, ("Perform PSA-based computation of digest of ServerKeyExchange"));
Jerry Yuee40f9d2022-02-17 14:55:16 +08008643
Gilles Peskine449bd832023-01-11 14:50:10 +01008644 if ((status = psa_hash_setup(&hash_operation,
8645 hash_alg)) != PSA_SUCCESS) {
8646 MBEDTLS_SSL_DEBUG_RET(1, "psa_hash_setup", status);
Jerry Yuee40f9d2022-02-17 14:55:16 +08008647 goto exit;
8648 }
8649
Gilles Peskine449bd832023-01-11 14:50:10 +01008650 if ((status = psa_hash_update(&hash_operation, ssl->handshake->randbytes,
8651 64)) != PSA_SUCCESS) {
8652 MBEDTLS_SSL_DEBUG_RET(1, "psa_hash_update", status);
Jerry Yuee40f9d2022-02-17 14:55:16 +08008653 goto exit;
8654 }
8655
Gilles Peskine449bd832023-01-11 14:50:10 +01008656 if ((status = psa_hash_update(&hash_operation,
8657 data, data_len)) != PSA_SUCCESS) {
8658 MBEDTLS_SSL_DEBUG_RET(1, "psa_hash_update", status);
Jerry Yuee40f9d2022-02-17 14:55:16 +08008659 goto exit;
8660 }
8661
Gilles Peskine449bd832023-01-11 14:50:10 +01008662 if ((status = psa_hash_finish(&hash_operation, hash, PSA_HASH_MAX_SIZE,
8663 hashlen)) != PSA_SUCCESS) {
8664 MBEDTLS_SSL_DEBUG_RET(1, "psa_hash_finish", status);
8665 goto exit;
Jerry Yuee40f9d2022-02-17 14:55:16 +08008666 }
8667
8668exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01008669 if (status != PSA_SUCCESS) {
8670 mbedtls_ssl_send_alert_message(ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
8671 MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR);
8672 switch (status) {
Jerry Yuee40f9d2022-02-17 14:55:16 +08008673 case PSA_ERROR_NOT_SUPPORTED:
Gilles Peskine449bd832023-01-11 14:50:10 +01008674 return MBEDTLS_ERR_MD_FEATURE_UNAVAILABLE;
Jerry Yuee40f9d2022-02-17 14:55:16 +08008675 case PSA_ERROR_BAD_STATE: /* Intentional fallthrough */
8676 case PSA_ERROR_BUFFER_TOO_SMALL:
Gilles Peskine449bd832023-01-11 14:50:10 +01008677 return MBEDTLS_ERR_MD_BAD_INPUT_DATA;
Jerry Yuee40f9d2022-02-17 14:55:16 +08008678 case PSA_ERROR_INSUFFICIENT_MEMORY:
Gilles Peskine449bd832023-01-11 14:50:10 +01008679 return MBEDTLS_ERR_MD_ALLOC_FAILED;
Jerry Yuee40f9d2022-02-17 14:55:16 +08008680 default:
Gilles Peskine449bd832023-01-11 14:50:10 +01008681 return MBEDTLS_ERR_PLATFORM_HW_ACCEL_FAILED;
Jerry Yuee40f9d2022-02-17 14:55:16 +08008682 }
8683 }
Gilles Peskine449bd832023-01-11 14:50:10 +01008684 return 0;
Jerry Yuee40f9d2022-02-17 14:55:16 +08008685}
8686
8687#else
8688
Gilles Peskine449bd832023-01-11 14:50:10 +01008689int mbedtls_ssl_get_key_exchange_md_tls1_2(mbedtls_ssl_context *ssl,
8690 unsigned char *hash, size_t *hashlen,
8691 unsigned char *data, size_t data_len,
8692 mbedtls_md_type_t md_alg)
Jerry Yuee40f9d2022-02-17 14:55:16 +08008693{
8694 int ret = 0;
8695 mbedtls_md_context_t ctx;
Gilles Peskine449bd832023-01-11 14:50:10 +01008696 const mbedtls_md_info_t *md_info = mbedtls_md_info_from_type(md_alg);
8697 *hashlen = mbedtls_md_get_size(md_info);
Jerry Yuee40f9d2022-02-17 14:55:16 +08008698
Gilles Peskine449bd832023-01-11 14:50:10 +01008699 MBEDTLS_SSL_DEBUG_MSG(3, ("Perform mbedtls-based computation of digest of ServerKeyExchange"));
Jerry Yuee40f9d2022-02-17 14:55:16 +08008700
Gilles Peskine449bd832023-01-11 14:50:10 +01008701 mbedtls_md_init(&ctx);
Jerry Yuee40f9d2022-02-17 14:55:16 +08008702
8703 /*
8704 * digitally-signed struct {
8705 * opaque client_random[32];
8706 * opaque server_random[32];
8707 * ServerDHParams params;
8708 * };
8709 */
Gilles Peskine449bd832023-01-11 14:50:10 +01008710 if ((ret = mbedtls_md_setup(&ctx, md_info, 0)) != 0) {
8711 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_md_setup", ret);
Jerry Yuee40f9d2022-02-17 14:55:16 +08008712 goto exit;
8713 }
Gilles Peskine449bd832023-01-11 14:50:10 +01008714 if ((ret = mbedtls_md_starts(&ctx)) != 0) {
8715 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_md_starts", ret);
Jerry Yuee40f9d2022-02-17 14:55:16 +08008716 goto exit;
8717 }
Gilles Peskine449bd832023-01-11 14:50:10 +01008718 if ((ret = mbedtls_md_update(&ctx, ssl->handshake->randbytes, 64)) != 0) {
8719 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_md_update", ret);
Jerry Yuee40f9d2022-02-17 14:55:16 +08008720 goto exit;
8721 }
Gilles Peskine449bd832023-01-11 14:50:10 +01008722 if ((ret = mbedtls_md_update(&ctx, data, data_len)) != 0) {
8723 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_md_update", ret);
Jerry Yuee40f9d2022-02-17 14:55:16 +08008724 goto exit;
8725 }
Gilles Peskine449bd832023-01-11 14:50:10 +01008726 if ((ret = mbedtls_md_finish(&ctx, hash)) != 0) {
8727 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_md_finish", ret);
Jerry Yuee40f9d2022-02-17 14:55:16 +08008728 goto exit;
8729 }
8730
8731exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01008732 mbedtls_md_free(&ctx);
Jerry Yuee40f9d2022-02-17 14:55:16 +08008733
Gilles Peskine449bd832023-01-11 14:50:10 +01008734 if (ret != 0) {
8735 mbedtls_ssl_send_alert_message(ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
8736 MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR);
8737 }
Jerry Yuee40f9d2022-02-17 14:55:16 +08008738
Gilles Peskine449bd832023-01-11 14:50:10 +01008739 return ret;
Jerry Yuee40f9d2022-02-17 14:55:16 +08008740}
8741#endif /* MBEDTLS_USE_PSA_CRYPTO */
8742
Jerry Yud9d91da2022-02-17 14:57:06 +08008743#if defined(MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED)
8744
Gabor Mezeia3d016c2022-05-10 12:44:09 +02008745/* Find the preferred hash for a given signature algorithm. */
8746unsigned int mbedtls_ssl_tls12_get_preferred_hash_for_sig_alg(
Gilles Peskine449bd832023-01-11 14:50:10 +01008747 mbedtls_ssl_context *ssl,
8748 unsigned int sig_alg)
Jerry Yud9d91da2022-02-17 14:57:06 +08008749{
Gabor Mezei078e8032022-04-27 21:17:56 +02008750 unsigned int i;
Gabor Mezeia3d016c2022-05-10 12:44:09 +02008751 uint16_t *received_sig_algs = ssl->handshake->received_sig_algs;
Gabor Mezei078e8032022-04-27 21:17:56 +02008752
Gilles Peskine449bd832023-01-11 14:50:10 +01008753 if (sig_alg == MBEDTLS_SSL_SIG_ANON) {
8754 return MBEDTLS_SSL_HASH_NONE;
8755 }
Gabor Mezei078e8032022-04-27 21:17:56 +02008756
Gilles Peskine449bd832023-01-11 14:50:10 +01008757 for (i = 0; received_sig_algs[i] != MBEDTLS_TLS_SIG_NONE; i++) {
Neil Armstrong9f1176a2022-06-24 18:19:19 +02008758 unsigned int hash_alg_received =
Gilles Peskine449bd832023-01-11 14:50:10 +01008759 MBEDTLS_SSL_TLS12_HASH_ALG_FROM_SIG_AND_HASH_ALG(
8760 received_sig_algs[i]);
Neil Armstrong9f1176a2022-06-24 18:19:19 +02008761 unsigned int sig_alg_received =
Gilles Peskine449bd832023-01-11 14:50:10 +01008762 MBEDTLS_SSL_TLS12_SIG_ALG_FROM_SIG_AND_HASH_ALG(
8763 received_sig_algs[i]);
Neil Armstrong9f1176a2022-06-24 18:19:19 +02008764
Gilles Peskine449bd832023-01-11 14:50:10 +01008765 if (sig_alg == sig_alg_received) {
Neil Armstrong9f1176a2022-06-24 18:19:19 +02008766#if defined(MBEDTLS_USE_PSA_CRYPTO)
Gilles Peskine449bd832023-01-11 14:50:10 +01008767 if (ssl->handshake->key_cert && ssl->handshake->key_cert->key) {
Neil Armstrong96eceb82022-06-30 18:05:05 +02008768 psa_algorithm_t psa_hash_alg =
Gilles Peskine449bd832023-01-11 14:50:10 +01008769 mbedtls_hash_info_psa_from_md(hash_alg_received);
Neil Armstrong9f1176a2022-06-24 18:19:19 +02008770
Gilles Peskine449bd832023-01-11 14:50:10 +01008771 if (sig_alg_received == MBEDTLS_SSL_SIG_ECDSA &&
8772 !mbedtls_pk_can_do_ext(ssl->handshake->key_cert->key,
8773 PSA_ALG_ECDSA(psa_hash_alg),
8774 PSA_KEY_USAGE_SIGN_HASH)) {
Neil Armstrong9f1176a2022-06-24 18:19:19 +02008775 continue;
Gilles Peskine449bd832023-01-11 14:50:10 +01008776 }
Neil Armstrong9f1176a2022-06-24 18:19:19 +02008777
Gilles Peskine449bd832023-01-11 14:50:10 +01008778 if (sig_alg_received == MBEDTLS_SSL_SIG_RSA &&
8779 !mbedtls_pk_can_do_ext(ssl->handshake->key_cert->key,
8780 PSA_ALG_RSA_PKCS1V15_SIGN(
8781 psa_hash_alg),
8782 PSA_KEY_USAGE_SIGN_HASH)) {
Neil Armstrong9f1176a2022-06-24 18:19:19 +02008783 continue;
Gilles Peskine449bd832023-01-11 14:50:10 +01008784 }
Neil Armstrong9f1176a2022-06-24 18:19:19 +02008785 }
Neil Armstrong9f1176a2022-06-24 18:19:19 +02008786#endif /* MBEDTLS_USE_PSA_CRYPTO */
Neil Armstrong96eceb82022-06-30 18:05:05 +02008787
Gilles Peskine449bd832023-01-11 14:50:10 +01008788 return hash_alg_received;
Neil Armstrong9f1176a2022-06-24 18:19:19 +02008789 }
Jerry Yud9d91da2022-02-17 14:57:06 +08008790 }
Jerry Yud9d91da2022-02-17 14:57:06 +08008791
Gilles Peskine449bd832023-01-11 14:50:10 +01008792 return MBEDTLS_SSL_HASH_NONE;
Jerry Yud9d91da2022-02-17 14:57:06 +08008793}
8794
8795#endif /* MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED */
8796
Jerry Yu4f9e3ef2022-02-17 14:58:27 +08008797/* Serialization of TLS 1.2 sessions:
8798 *
8799 * struct {
8800 * uint64 start_time;
8801 * uint8 ciphersuite[2]; // defined by the standard
Jerry Yu4f9e3ef2022-02-17 14:58:27 +08008802 * uint8 session_id_len; // at most 32
8803 * opaque session_id[32];
8804 * opaque master[48]; // fixed length in the standard
8805 * uint32 verify_result;
8806 * opaque peer_cert<0..2^24-1>; // length 0 means no peer cert
8807 * opaque ticket<0..2^24-1>; // length 0 means no ticket
8808 * uint32 ticket_lifetime;
8809 * uint8 mfl_code; // up to 255 according to standard
8810 * uint8 encrypt_then_mac; // 0 or 1
8811 * } serialized_session_tls12;
8812 *
8813 */
Gilles Peskine449bd832023-01-11 14:50:10 +01008814static size_t ssl_tls12_session_save(const mbedtls_ssl_session *session,
8815 unsigned char *buf,
8816 size_t buf_len)
Jerry Yu4f9e3ef2022-02-17 14:58:27 +08008817{
8818 unsigned char *p = buf;
8819 size_t used = 0;
8820
8821#if defined(MBEDTLS_HAVE_TIME)
8822 uint64_t start;
8823#endif
8824#if defined(MBEDTLS_X509_CRT_PARSE_C)
8825#if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
8826 size_t cert_len;
8827#endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
8828#endif /* MBEDTLS_X509_CRT_PARSE_C */
8829
8830 /*
8831 * Time
8832 */
8833#if defined(MBEDTLS_HAVE_TIME)
8834 used += 8;
8835
Gilles Peskine449bd832023-01-11 14:50:10 +01008836 if (used <= buf_len) {
Jerry Yu4f9e3ef2022-02-17 14:58:27 +08008837 start = (uint64_t) session->start;
8838
Gilles Peskine449bd832023-01-11 14:50:10 +01008839 MBEDTLS_PUT_UINT64_BE(start, p, 0);
Jerry Yu4f9e3ef2022-02-17 14:58:27 +08008840 p += 8;
8841 }
8842#endif /* MBEDTLS_HAVE_TIME */
8843
8844 /*
8845 * Basic mandatory fields
8846 */
8847 used += 2 /* ciphersuite */
Gilles Peskine449bd832023-01-11 14:50:10 +01008848 + 1 /* id_len */
8849 + sizeof(session->id)
8850 + sizeof(session->master)
8851 + 4; /* verify_result */
Jerry Yu4f9e3ef2022-02-17 14:58:27 +08008852
Gilles Peskine449bd832023-01-11 14:50:10 +01008853 if (used <= buf_len) {
8854 MBEDTLS_PUT_UINT16_BE(session->ciphersuite, p, 0);
Jerry Yu4f9e3ef2022-02-17 14:58:27 +08008855 p += 2;
8856
Gilles Peskine449bd832023-01-11 14:50:10 +01008857 *p++ = MBEDTLS_BYTE_0(session->id_len);
8858 memcpy(p, session->id, 32);
Jerry Yu4f9e3ef2022-02-17 14:58:27 +08008859 p += 32;
8860
Gilles Peskine449bd832023-01-11 14:50:10 +01008861 memcpy(p, session->master, 48);
Jerry Yu4f9e3ef2022-02-17 14:58:27 +08008862 p += 48;
8863
Gilles Peskine449bd832023-01-11 14:50:10 +01008864 MBEDTLS_PUT_UINT32_BE(session->verify_result, p, 0);
Jerry Yu4f9e3ef2022-02-17 14:58:27 +08008865 p += 4;
8866 }
8867
8868 /*
8869 * Peer's end-entity certificate
8870 */
8871#if defined(MBEDTLS_X509_CRT_PARSE_C)
8872#if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
Gilles Peskine449bd832023-01-11 14:50:10 +01008873 if (session->peer_cert == NULL) {
Jerry Yu4f9e3ef2022-02-17 14:58:27 +08008874 cert_len = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +01008875 } else {
Jerry Yu4f9e3ef2022-02-17 14:58:27 +08008876 cert_len = session->peer_cert->raw.len;
Gilles Peskine449bd832023-01-11 14:50:10 +01008877 }
Jerry Yu4f9e3ef2022-02-17 14:58:27 +08008878
8879 used += 3 + cert_len;
8880
Gilles Peskine449bd832023-01-11 14:50:10 +01008881 if (used <= buf_len) {
8882 *p++ = MBEDTLS_BYTE_2(cert_len);
8883 *p++ = MBEDTLS_BYTE_1(cert_len);
8884 *p++ = MBEDTLS_BYTE_0(cert_len);
Jerry Yu4f9e3ef2022-02-17 14:58:27 +08008885
Gilles Peskine449bd832023-01-11 14:50:10 +01008886 if (session->peer_cert != NULL) {
8887 memcpy(p, session->peer_cert->raw.p, cert_len);
Jerry Yu4f9e3ef2022-02-17 14:58:27 +08008888 p += cert_len;
8889 }
8890 }
8891#else /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
Gilles Peskine449bd832023-01-11 14:50:10 +01008892 if (session->peer_cert_digest != NULL) {
Jerry Yu4f9e3ef2022-02-17 14:58:27 +08008893 used += 1 /* type */ + 1 /* length */ + session->peer_cert_digest_len;
Gilles Peskine449bd832023-01-11 14:50:10 +01008894 if (used <= buf_len) {
Jerry Yu4f9e3ef2022-02-17 14:58:27 +08008895 *p++ = (unsigned char) session->peer_cert_digest_type;
8896 *p++ = (unsigned char) session->peer_cert_digest_len;
Gilles Peskine449bd832023-01-11 14:50:10 +01008897 memcpy(p, session->peer_cert_digest,
8898 session->peer_cert_digest_len);
Jerry Yu4f9e3ef2022-02-17 14:58:27 +08008899 p += session->peer_cert_digest_len;
8900 }
Gilles Peskine449bd832023-01-11 14:50:10 +01008901 } else {
Jerry Yu4f9e3ef2022-02-17 14:58:27 +08008902 used += 2;
Gilles Peskine449bd832023-01-11 14:50:10 +01008903 if (used <= buf_len) {
Jerry Yu4f9e3ef2022-02-17 14:58:27 +08008904 *p++ = (unsigned char) MBEDTLS_MD_NONE;
8905 *p++ = 0;
8906 }
8907 }
8908#endif /* !MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
8909#endif /* MBEDTLS_X509_CRT_PARSE_C */
8910
8911 /*
8912 * Session ticket if any, plus associated data
8913 */
8914#if defined(MBEDTLS_SSL_SESSION_TICKETS) && defined(MBEDTLS_SSL_CLI_C)
8915 used += 3 + session->ticket_len + 4; /* len + ticket + lifetime */
8916
Gilles Peskine449bd832023-01-11 14:50:10 +01008917 if (used <= buf_len) {
8918 *p++ = MBEDTLS_BYTE_2(session->ticket_len);
8919 *p++ = MBEDTLS_BYTE_1(session->ticket_len);
8920 *p++ = MBEDTLS_BYTE_0(session->ticket_len);
Jerry Yu4f9e3ef2022-02-17 14:58:27 +08008921
Gilles Peskine449bd832023-01-11 14:50:10 +01008922 if (session->ticket != NULL) {
8923 memcpy(p, session->ticket, session->ticket_len);
Jerry Yu4f9e3ef2022-02-17 14:58:27 +08008924 p += session->ticket_len;
8925 }
8926
Gilles Peskine449bd832023-01-11 14:50:10 +01008927 MBEDTLS_PUT_UINT32_BE(session->ticket_lifetime, p, 0);
Jerry Yu4f9e3ef2022-02-17 14:58:27 +08008928 p += 4;
8929 }
8930#endif /* MBEDTLS_SSL_SESSION_TICKETS && MBEDTLS_SSL_CLI_C */
8931
8932 /*
8933 * Misc extension-related info
8934 */
8935#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
8936 used += 1;
8937
Gilles Peskine449bd832023-01-11 14:50:10 +01008938 if (used <= buf_len) {
Jerry Yu4f9e3ef2022-02-17 14:58:27 +08008939 *p++ = session->mfl_code;
Gilles Peskine449bd832023-01-11 14:50:10 +01008940 }
Jerry Yu4f9e3ef2022-02-17 14:58:27 +08008941#endif
8942
8943#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
8944 used += 1;
8945
Gilles Peskine449bd832023-01-11 14:50:10 +01008946 if (used <= buf_len) {
8947 *p++ = MBEDTLS_BYTE_0(session->encrypt_then_mac);
8948 }
Jerry Yu4f9e3ef2022-02-17 14:58:27 +08008949#endif
8950
Gilles Peskine449bd832023-01-11 14:50:10 +01008951 return used;
Jerry Yu4f9e3ef2022-02-17 14:58:27 +08008952}
8953
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02008954MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01008955static int ssl_tls12_session_load(mbedtls_ssl_session *session,
8956 const unsigned char *buf,
8957 size_t len)
Jerry Yu4f9e3ef2022-02-17 14:58:27 +08008958{
8959#if defined(MBEDTLS_HAVE_TIME)
8960 uint64_t start;
8961#endif
8962#if defined(MBEDTLS_X509_CRT_PARSE_C)
8963#if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
8964 size_t cert_len;
8965#endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
8966#endif /* MBEDTLS_X509_CRT_PARSE_C */
8967
8968 const unsigned char *p = buf;
8969 const unsigned char * const end = buf + len;
8970
8971 /*
8972 * Time
8973 */
8974#if defined(MBEDTLS_HAVE_TIME)
Gilles Peskine449bd832023-01-11 14:50:10 +01008975 if (8 > (size_t) (end - p)) {
8976 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
8977 }
Jerry Yu4f9e3ef2022-02-17 14:58:27 +08008978
Gilles Peskine449bd832023-01-11 14:50:10 +01008979 start = ((uint64_t) p[0] << 56) |
8980 ((uint64_t) p[1] << 48) |
8981 ((uint64_t) p[2] << 40) |
8982 ((uint64_t) p[3] << 32) |
8983 ((uint64_t) p[4] << 24) |
8984 ((uint64_t) p[5] << 16) |
8985 ((uint64_t) p[6] << 8) |
8986 ((uint64_t) p[7]);
Jerry Yu4f9e3ef2022-02-17 14:58:27 +08008987 p += 8;
8988
8989 session->start = (time_t) start;
8990#endif /* MBEDTLS_HAVE_TIME */
8991
8992 /*
8993 * Basic mandatory fields
8994 */
Gilles Peskine449bd832023-01-11 14:50:10 +01008995 if (2 + 1 + 32 + 48 + 4 > (size_t) (end - p)) {
8996 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
8997 }
Jerry Yu4f9e3ef2022-02-17 14:58:27 +08008998
Gilles Peskine449bd832023-01-11 14:50:10 +01008999 session->ciphersuite = (p[0] << 8) | p[1];
Jerry Yu4f9e3ef2022-02-17 14:58:27 +08009000 p += 2;
9001
Jerry Yu4f9e3ef2022-02-17 14:58:27 +08009002 session->id_len = *p++;
Gilles Peskine449bd832023-01-11 14:50:10 +01009003 memcpy(session->id, p, 32);
Jerry Yu4f9e3ef2022-02-17 14:58:27 +08009004 p += 32;
9005
Gilles Peskine449bd832023-01-11 14:50:10 +01009006 memcpy(session->master, p, 48);
Jerry Yu4f9e3ef2022-02-17 14:58:27 +08009007 p += 48;
9008
Gilles Peskine449bd832023-01-11 14:50:10 +01009009 session->verify_result = ((uint32_t) p[0] << 24) |
9010 ((uint32_t) p[1] << 16) |
9011 ((uint32_t) p[2] << 8) |
9012 ((uint32_t) p[3]);
Jerry Yu4f9e3ef2022-02-17 14:58:27 +08009013 p += 4;
9014
9015 /* Immediately clear invalid pointer values that have been read, in case
9016 * we exit early before we replaced them with valid ones. */
9017#if defined(MBEDTLS_X509_CRT_PARSE_C)
9018#if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
9019 session->peer_cert = NULL;
9020#else
9021 session->peer_cert_digest = NULL;
9022#endif /* !MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
9023#endif /* MBEDTLS_X509_CRT_PARSE_C */
9024#if defined(MBEDTLS_SSL_SESSION_TICKETS) && defined(MBEDTLS_SSL_CLI_C)
9025 session->ticket = NULL;
9026#endif /* MBEDTLS_SSL_SESSION_TICKETS && MBEDTLS_SSL_CLI_C */
9027
9028 /*
9029 * Peer certificate
9030 */
9031#if defined(MBEDTLS_X509_CRT_PARSE_C)
9032#if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
9033 /* Deserialize CRT from the end of the ticket. */
Gilles Peskine449bd832023-01-11 14:50:10 +01009034 if (3 > (size_t) (end - p)) {
9035 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
9036 }
Jerry Yu4f9e3ef2022-02-17 14:58:27 +08009037
Gilles Peskine449bd832023-01-11 14:50:10 +01009038 cert_len = (p[0] << 16) | (p[1] << 8) | p[2];
Jerry Yu4f9e3ef2022-02-17 14:58:27 +08009039 p += 3;
9040
Gilles Peskine449bd832023-01-11 14:50:10 +01009041 if (cert_len != 0) {
Jerry Yu4f9e3ef2022-02-17 14:58:27 +08009042 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
9043
Gilles Peskine449bd832023-01-11 14:50:10 +01009044 if (cert_len > (size_t) (end - p)) {
9045 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
9046 }
Jerry Yu4f9e3ef2022-02-17 14:58:27 +08009047
Gilles Peskine449bd832023-01-11 14:50:10 +01009048 session->peer_cert = mbedtls_calloc(1, sizeof(mbedtls_x509_crt));
Jerry Yu4f9e3ef2022-02-17 14:58:27 +08009049
Gilles Peskine449bd832023-01-11 14:50:10 +01009050 if (session->peer_cert == NULL) {
9051 return MBEDTLS_ERR_SSL_ALLOC_FAILED;
9052 }
Jerry Yu4f9e3ef2022-02-17 14:58:27 +08009053
Gilles Peskine449bd832023-01-11 14:50:10 +01009054 mbedtls_x509_crt_init(session->peer_cert);
Jerry Yu4f9e3ef2022-02-17 14:58:27 +08009055
Gilles Peskine449bd832023-01-11 14:50:10 +01009056 if ((ret = mbedtls_x509_crt_parse_der(session->peer_cert,
9057 p, cert_len)) != 0) {
9058 mbedtls_x509_crt_free(session->peer_cert);
9059 mbedtls_free(session->peer_cert);
Jerry Yu4f9e3ef2022-02-17 14:58:27 +08009060 session->peer_cert = NULL;
Gilles Peskine449bd832023-01-11 14:50:10 +01009061 return ret;
Jerry Yu4f9e3ef2022-02-17 14:58:27 +08009062 }
9063
9064 p += cert_len;
9065 }
9066#else /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
9067 /* Deserialize CRT digest from the end of the ticket. */
Gilles Peskine449bd832023-01-11 14:50:10 +01009068 if (2 > (size_t) (end - p)) {
9069 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
9070 }
Jerry Yu4f9e3ef2022-02-17 14:58:27 +08009071
9072 session->peer_cert_digest_type = (mbedtls_md_type_t) *p++;
9073 session->peer_cert_digest_len = (size_t) *p++;
9074
Gilles Peskine449bd832023-01-11 14:50:10 +01009075 if (session->peer_cert_digest_len != 0) {
Jerry Yu4f9e3ef2022-02-17 14:58:27 +08009076 const mbedtls_md_info_t *md_info =
Gilles Peskine449bd832023-01-11 14:50:10 +01009077 mbedtls_md_info_from_type(session->peer_cert_digest_type);
9078 if (md_info == NULL) {
9079 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
9080 }
9081 if (session->peer_cert_digest_len != mbedtls_md_get_size(md_info)) {
9082 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
9083 }
Jerry Yu4f9e3ef2022-02-17 14:58:27 +08009084
Gilles Peskine449bd832023-01-11 14:50:10 +01009085 if (session->peer_cert_digest_len > (size_t) (end - p)) {
9086 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
9087 }
Jerry Yu4f9e3ef2022-02-17 14:58:27 +08009088
9089 session->peer_cert_digest =
Gilles Peskine449bd832023-01-11 14:50:10 +01009090 mbedtls_calloc(1, session->peer_cert_digest_len);
9091 if (session->peer_cert_digest == NULL) {
9092 return MBEDTLS_ERR_SSL_ALLOC_FAILED;
9093 }
Jerry Yu4f9e3ef2022-02-17 14:58:27 +08009094
Gilles Peskine449bd832023-01-11 14:50:10 +01009095 memcpy(session->peer_cert_digest, p,
9096 session->peer_cert_digest_len);
Jerry Yu4f9e3ef2022-02-17 14:58:27 +08009097 p += session->peer_cert_digest_len;
9098 }
9099#endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
9100#endif /* MBEDTLS_X509_CRT_PARSE_C */
9101
9102 /*
9103 * Session ticket and associated data
9104 */
9105#if defined(MBEDTLS_SSL_SESSION_TICKETS) && defined(MBEDTLS_SSL_CLI_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01009106 if (3 > (size_t) (end - p)) {
9107 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
9108 }
Jerry Yu4f9e3ef2022-02-17 14:58:27 +08009109
Gilles Peskine449bd832023-01-11 14:50:10 +01009110 session->ticket_len = (p[0] << 16) | (p[1] << 8) | p[2];
Jerry Yu4f9e3ef2022-02-17 14:58:27 +08009111 p += 3;
9112
Gilles Peskine449bd832023-01-11 14:50:10 +01009113 if (session->ticket_len != 0) {
9114 if (session->ticket_len > (size_t) (end - p)) {
9115 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
9116 }
Jerry Yu4f9e3ef2022-02-17 14:58:27 +08009117
Gilles Peskine449bd832023-01-11 14:50:10 +01009118 session->ticket = mbedtls_calloc(1, session->ticket_len);
9119 if (session->ticket == NULL) {
9120 return MBEDTLS_ERR_SSL_ALLOC_FAILED;
9121 }
Jerry Yu4f9e3ef2022-02-17 14:58:27 +08009122
Gilles Peskine449bd832023-01-11 14:50:10 +01009123 memcpy(session->ticket, p, session->ticket_len);
Jerry Yu4f9e3ef2022-02-17 14:58:27 +08009124 p += session->ticket_len;
9125 }
9126
Gilles Peskine449bd832023-01-11 14:50:10 +01009127 if (4 > (size_t) (end - p)) {
9128 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
9129 }
Jerry Yu4f9e3ef2022-02-17 14:58:27 +08009130
Gilles Peskine449bd832023-01-11 14:50:10 +01009131 session->ticket_lifetime = ((uint32_t) p[0] << 24) |
9132 ((uint32_t) p[1] << 16) |
9133 ((uint32_t) p[2] << 8) |
9134 ((uint32_t) p[3]);
Jerry Yu4f9e3ef2022-02-17 14:58:27 +08009135 p += 4;
9136#endif /* MBEDTLS_SSL_SESSION_TICKETS && MBEDTLS_SSL_CLI_C */
9137
9138 /*
9139 * Misc extension-related info
9140 */
9141#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
Gilles Peskine449bd832023-01-11 14:50:10 +01009142 if (1 > (size_t) (end - p)) {
9143 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
9144 }
Jerry Yu4f9e3ef2022-02-17 14:58:27 +08009145
9146 session->mfl_code = *p++;
9147#endif
9148
9149#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
Gilles Peskine449bd832023-01-11 14:50:10 +01009150 if (1 > (size_t) (end - p)) {
9151 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
9152 }
Jerry Yu4f9e3ef2022-02-17 14:58:27 +08009153
9154 session->encrypt_then_mac = *p++;
9155#endif
9156
9157 /* Done, should have consumed entire buffer */
Gilles Peskine449bd832023-01-11 14:50:10 +01009158 if (p != end) {
9159 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
9160 }
Jerry Yu4f9e3ef2022-02-17 14:58:27 +08009161
Gilles Peskine449bd832023-01-11 14:50:10 +01009162 return 0;
Jerry Yu4f9e3ef2022-02-17 14:58:27 +08009163}
Jerry Yudc7bd172022-02-17 13:44:15 +08009164#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
9165
XiaokangQian75d40ef2022-04-20 11:05:24 +00009166int mbedtls_ssl_validate_ciphersuite(
9167 const mbedtls_ssl_context *ssl,
9168 const mbedtls_ssl_ciphersuite_t *suite_info,
9169 mbedtls_ssl_protocol_version min_tls_version,
Gilles Peskine449bd832023-01-11 14:50:10 +01009170 mbedtls_ssl_protocol_version max_tls_version)
XiaokangQian75d40ef2022-04-20 11:05:24 +00009171{
9172 (void) ssl;
9173
Gilles Peskine449bd832023-01-11 14:50:10 +01009174 if (suite_info == NULL) {
9175 return -1;
9176 }
XiaokangQian75d40ef2022-04-20 11:05:24 +00009177
Gilles Peskine449bd832023-01-11 14:50:10 +01009178 if ((suite_info->min_tls_version > max_tls_version) ||
9179 (suite_info->max_tls_version < min_tls_version)) {
9180 return -1;
XiaokangQian75d40ef2022-04-20 11:05:24 +00009181 }
9182
XiaokangQian060d8672022-04-21 09:24:56 +00009183#if defined(MBEDTLS_SSL_PROTO_TLS1_2) && defined(MBEDTLS_SSL_CLI_C)
XiaokangQian75d40ef2022-04-20 11:05:24 +00009184#if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
Neil Armstrongca7d5062022-05-31 14:43:23 +02009185#if defined(MBEDTLS_USE_PSA_CRYPTO)
Gilles Peskine449bd832023-01-11 14:50:10 +01009186 if (suite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECJPAKE &&
9187 ssl->handshake->psa_pake_ctx_is_ok != 1)
Neil Armstrongca7d5062022-05-31 14:43:23 +02009188#else
Gilles Peskine449bd832023-01-11 14:50:10 +01009189 if (suite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECJPAKE &&
9190 mbedtls_ecjpake_check(&ssl->handshake->ecjpake_ctx) != 0)
Neil Armstrongca7d5062022-05-31 14:43:23 +02009191#endif /* MBEDTLS_USE_PSA_CRYPTO */
XiaokangQian75d40ef2022-04-20 11:05:24 +00009192 {
Gilles Peskine449bd832023-01-11 14:50:10 +01009193 return -1;
XiaokangQian75d40ef2022-04-20 11:05:24 +00009194 }
9195#endif
9196
9197 /* Don't suggest PSK-based ciphersuite if no PSK is available. */
9198#if defined(MBEDTLS_KEY_EXCHANGE_SOME_PSK_ENABLED)
Gilles Peskine449bd832023-01-11 14:50:10 +01009199 if (mbedtls_ssl_ciphersuite_uses_psk(suite_info) &&
9200 mbedtls_ssl_conf_has_static_psk(ssl->conf) == 0) {
9201 return -1;
XiaokangQian75d40ef2022-04-20 11:05:24 +00009202 }
9203#endif /* MBEDTLS_KEY_EXCHANGE_SOME_PSK_ENABLED */
9204#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
9205
Gilles Peskine449bd832023-01-11 14:50:10 +01009206 return 0;
XiaokangQian75d40ef2022-04-20 11:05:24 +00009207}
9208
Ronald Crone68ab4f2022-10-05 12:46:29 +02009209#if defined(MBEDTLS_SSL_HANDSHAKE_WITH_CERT_ENABLED)
XiaokangQianeaf36512022-04-24 09:07:44 +00009210/*
9211 * Function for writing a signature algorithm extension.
9212 *
9213 * The `extension_data` field of signature algorithm contains a `SignatureSchemeList`
9214 * value (TLS 1.3 RFC8446):
9215 * enum {
9216 * ....
9217 * ecdsa_secp256r1_sha256( 0x0403 ),
9218 * ecdsa_secp384r1_sha384( 0x0503 ),
9219 * ecdsa_secp521r1_sha512( 0x0603 ),
9220 * ....
9221 * } SignatureScheme;
9222 *
9223 * struct {
9224 * SignatureScheme supported_signature_algorithms<2..2^16-2>;
9225 * } SignatureSchemeList;
9226 *
9227 * The `extension_data` field of signature algorithm contains a `SignatureAndHashAlgorithm`
9228 * value (TLS 1.2 RFC5246):
9229 * enum {
9230 * none(0), md5(1), sha1(2), sha224(3), sha256(4), sha384(5),
9231 * sha512(6), (255)
9232 * } HashAlgorithm;
9233 *
9234 * enum { anonymous(0), rsa(1), dsa(2), ecdsa(3), (255) }
9235 * SignatureAlgorithm;
9236 *
9237 * struct {
9238 * HashAlgorithm hash;
9239 * SignatureAlgorithm signature;
9240 * } SignatureAndHashAlgorithm;
9241 *
9242 * SignatureAndHashAlgorithm
9243 * supported_signature_algorithms<2..2^16-2>;
9244 *
9245 * The TLS 1.3 signature algorithm extension was defined to be a compatible
9246 * generalization of the TLS 1.2 signature algorithm extension.
9247 * `SignatureAndHashAlgorithm` field of TLS 1.2 can be represented by
9248 * `SignatureScheme` field of TLS 1.3
9249 *
9250 */
Gilles Peskine449bd832023-01-11 14:50:10 +01009251int mbedtls_ssl_write_sig_alg_ext(mbedtls_ssl_context *ssl, unsigned char *buf,
9252 const unsigned char *end, size_t *out_len)
XiaokangQianeaf36512022-04-24 09:07:44 +00009253{
9254 unsigned char *p = buf;
9255 unsigned char *supported_sig_alg; /* Start of supported_signature_algorithms */
9256 size_t supported_sig_alg_len = 0; /* Length of supported_signature_algorithms */
9257
9258 *out_len = 0;
9259
Gilles Peskine449bd832023-01-11 14:50:10 +01009260 MBEDTLS_SSL_DEBUG_MSG(3, ("adding signature_algorithms extension"));
XiaokangQianeaf36512022-04-24 09:07:44 +00009261
9262 /* Check if we have space for header and length field:
9263 * - extension_type (2 bytes)
9264 * - extension_data_length (2 bytes)
9265 * - supported_signature_algorithms_length (2 bytes)
9266 */
Gilles Peskine449bd832023-01-11 14:50:10 +01009267 MBEDTLS_SSL_CHK_BUF_PTR(p, end, 6);
XiaokangQianeaf36512022-04-24 09:07:44 +00009268 p += 6;
9269
9270 /*
9271 * Write supported_signature_algorithms
9272 */
9273 supported_sig_alg = p;
Gilles Peskine449bd832023-01-11 14:50:10 +01009274 const uint16_t *sig_alg = mbedtls_ssl_get_sig_algs(ssl);
9275 if (sig_alg == NULL) {
9276 return MBEDTLS_ERR_SSL_BAD_CONFIG;
9277 }
XiaokangQianeaf36512022-04-24 09:07:44 +00009278
Gilles Peskine449bd832023-01-11 14:50:10 +01009279 for (; *sig_alg != MBEDTLS_TLS1_3_SIG_NONE; sig_alg++) {
9280 MBEDTLS_SSL_DEBUG_MSG(3, ("got signature scheme [%x] %s",
9281 *sig_alg,
9282 mbedtls_ssl_sig_alg_to_str(*sig_alg)));
9283 if (!mbedtls_ssl_sig_alg_is_supported(ssl, *sig_alg)) {
XiaokangQianeaf36512022-04-24 09:07:44 +00009284 continue;
Gilles Peskine449bd832023-01-11 14:50:10 +01009285 }
9286 MBEDTLS_SSL_CHK_BUF_PTR(p, end, 2);
9287 MBEDTLS_PUT_UINT16_BE(*sig_alg, p, 0);
XiaokangQianeaf36512022-04-24 09:07:44 +00009288 p += 2;
Gilles Peskine449bd832023-01-11 14:50:10 +01009289 MBEDTLS_SSL_DEBUG_MSG(3, ("sent signature scheme [%x] %s",
9290 *sig_alg,
9291 mbedtls_ssl_sig_alg_to_str(*sig_alg)));
XiaokangQianeaf36512022-04-24 09:07:44 +00009292 }
9293
9294 /* Length of supported_signature_algorithms */
9295 supported_sig_alg_len = p - supported_sig_alg;
Gilles Peskine449bd832023-01-11 14:50:10 +01009296 if (supported_sig_alg_len == 0) {
9297 MBEDTLS_SSL_DEBUG_MSG(1, ("No signature algorithms defined."));
9298 return MBEDTLS_ERR_SSL_INTERNAL_ERROR;
XiaokangQianeaf36512022-04-24 09:07:44 +00009299 }
9300
Gilles Peskine449bd832023-01-11 14:50:10 +01009301 MBEDTLS_PUT_UINT16_BE(MBEDTLS_TLS_EXT_SIG_ALG, buf, 0);
9302 MBEDTLS_PUT_UINT16_BE(supported_sig_alg_len + 2, buf, 2);
9303 MBEDTLS_PUT_UINT16_BE(supported_sig_alg_len, buf, 4);
XiaokangQianeaf36512022-04-24 09:07:44 +00009304
XiaokangQianeaf36512022-04-24 09:07:44 +00009305 *out_len = p - buf;
9306
9307#if defined(MBEDTLS_SSL_PROTO_TLS1_3)
Gilles Peskine449bd832023-01-11 14:50:10 +01009308 mbedtls_ssl_tls13_set_hs_sent_ext_mask(ssl, MBEDTLS_TLS_EXT_SIG_ALG);
XiaokangQianeaf36512022-04-24 09:07:44 +00009309#endif /* MBEDTLS_SSL_PROTO_TLS1_3 */
Jerry Yu0c354a22022-08-29 15:25:36 +08009310
Gilles Peskine449bd832023-01-11 14:50:10 +01009311 return 0;
XiaokangQianeaf36512022-04-24 09:07:44 +00009312}
Ronald Crone68ab4f2022-10-05 12:46:29 +02009313#endif /* MBEDTLS_SSL_HANDSHAKE_WITH_CERT_ENABLED */
XiaokangQianeaf36512022-04-24 09:07:44 +00009314
XiaokangQian40a35232022-05-07 09:02:40 +00009315#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
XiaokangQian9b2b7712022-05-17 02:57:00 +00009316/*
9317 * mbedtls_ssl_parse_server_name_ext
9318 *
9319 * Structure of server_name extension:
9320 *
9321 * enum {
9322 * host_name(0), (255)
9323 * } NameType;
9324 * opaque HostName<1..2^16-1>;
9325 *
9326 * struct {
9327 * NameType name_type;
9328 * select (name_type) {
9329 * case host_name: HostName;
9330 * } name;
9331 * } ServerName;
9332 * struct {
9333 * ServerName server_name_list<1..2^16-1>
9334 * } ServerNameList;
9335 */
Ronald Cronce7d76e2022-07-08 18:56:49 +02009336MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01009337int mbedtls_ssl_parse_server_name_ext(mbedtls_ssl_context *ssl,
9338 const unsigned char *buf,
9339 const unsigned char *end)
XiaokangQian40a35232022-05-07 09:02:40 +00009340{
9341 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
9342 const unsigned char *p = buf;
XiaokangQian9b2b7712022-05-17 02:57:00 +00009343 size_t server_name_list_len, hostname_len;
9344 const unsigned char *server_name_list_end;
XiaokangQian40a35232022-05-07 09:02:40 +00009345
Gilles Peskine449bd832023-01-11 14:50:10 +01009346 MBEDTLS_SSL_DEBUG_MSG(3, ("parse ServerName extension"));
XiaokangQian40a35232022-05-07 09:02:40 +00009347
Gilles Peskine449bd832023-01-11 14:50:10 +01009348 MBEDTLS_SSL_CHK_BUF_READ_PTR(p, end, 2);
9349 server_name_list_len = MBEDTLS_GET_UINT16_BE(p, 0);
XiaokangQian40a35232022-05-07 09:02:40 +00009350 p += 2;
9351
Gilles Peskine449bd832023-01-11 14:50:10 +01009352 MBEDTLS_SSL_CHK_BUF_READ_PTR(p, end, server_name_list_len);
XiaokangQian9b2b7712022-05-17 02:57:00 +00009353 server_name_list_end = p + server_name_list_len;
Gilles Peskine449bd832023-01-11 14:50:10 +01009354 while (p < server_name_list_end) {
9355 MBEDTLS_SSL_CHK_BUF_READ_PTR(p, server_name_list_end, 3);
9356 hostname_len = MBEDTLS_GET_UINT16_BE(p, 1);
9357 MBEDTLS_SSL_CHK_BUF_READ_PTR(p, server_name_list_end,
9358 hostname_len + 3);
XiaokangQian40a35232022-05-07 09:02:40 +00009359
Gilles Peskine449bd832023-01-11 14:50:10 +01009360 if (p[0] == MBEDTLS_TLS_EXT_SERVERNAME_HOSTNAME) {
XiaokangQian75fe8c72022-06-15 09:42:45 +00009361 /* sni_name is intended to be used only during the parsing of the
9362 * ClientHello message (it is reset to NULL before the end of
9363 * the message parsing). Thus it is ok to just point to the
9364 * reception buffer and not make a copy of it.
9365 */
XiaokangQianf2a94202022-05-20 06:44:24 +00009366 ssl->handshake->sni_name = p + 3;
9367 ssl->handshake->sni_name_len = hostname_len;
Gilles Peskine449bd832023-01-11 14:50:10 +01009368 if (ssl->conf->f_sni == NULL) {
9369 return 0;
XiaokangQian40a35232022-05-07 09:02:40 +00009370 }
Gilles Peskine449bd832023-01-11 14:50:10 +01009371 ret = ssl->conf->f_sni(ssl->conf->p_sni,
9372 ssl, p + 3, hostname_len);
9373 if (ret != 0) {
9374 MBEDTLS_SSL_DEBUG_RET(1, "ssl_sni_wrapper", ret);
9375 MBEDTLS_SSL_PEND_FATAL_ALERT(MBEDTLS_SSL_ALERT_MSG_UNRECOGNIZED_NAME,
9376 MBEDTLS_ERR_SSL_UNRECOGNIZED_NAME);
9377 return MBEDTLS_ERR_SSL_UNRECOGNIZED_NAME;
9378 }
9379 return 0;
XiaokangQian40a35232022-05-07 09:02:40 +00009380 }
9381
9382 p += hostname_len + 3;
9383 }
9384
Gilles Peskine449bd832023-01-11 14:50:10 +01009385 return 0;
XiaokangQian40a35232022-05-07 09:02:40 +00009386}
9387#endif /* MBEDTLS_SSL_SERVER_NAME_INDICATION */
9388
XiaokangQianacb39922022-06-17 10:18:48 +00009389#if defined(MBEDTLS_SSL_ALPN)
Ronald Cronce7d76e2022-07-08 18:56:49 +02009390MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01009391int mbedtls_ssl_parse_alpn_ext(mbedtls_ssl_context *ssl,
9392 const unsigned char *buf,
9393 const unsigned char *end)
XiaokangQianacb39922022-06-17 10:18:48 +00009394{
9395 const unsigned char *p = buf;
XiaokangQianc7403452022-06-23 03:24:12 +00009396 size_t protocol_name_list_len;
XiaokangQian95d5f542022-06-24 02:29:26 +00009397 const unsigned char *protocol_name_list;
9398 const unsigned char *protocol_name_list_end;
XiaokangQianc7403452022-06-23 03:24:12 +00009399 size_t protocol_name_len;
XiaokangQianacb39922022-06-17 10:18:48 +00009400
9401 /* If ALPN not configured, just ignore the extension */
Gilles Peskine449bd832023-01-11 14:50:10 +01009402 if (ssl->conf->alpn_list == NULL) {
9403 return 0;
9404 }
XiaokangQianacb39922022-06-17 10:18:48 +00009405
9406 /*
XiaokangQianc7403452022-06-23 03:24:12 +00009407 * RFC7301, section 3.1
9408 * opaque ProtocolName<1..2^8-1>;
XiaokangQianacb39922022-06-17 10:18:48 +00009409 *
XiaokangQianc7403452022-06-23 03:24:12 +00009410 * struct {
9411 * ProtocolName protocol_name_list<2..2^16-1>
9412 * } ProtocolNameList;
XiaokangQianacb39922022-06-17 10:18:48 +00009413 */
9414
XiaokangQianc7403452022-06-23 03:24:12 +00009415 /*
XiaokangQian0b776e22022-06-24 09:04:59 +00009416 * protocol_name_list_len 2 bytes
9417 * protocol_name_len 1 bytes
9418 * protocol_name >=1 byte
XiaokangQianc7403452022-06-23 03:24:12 +00009419 */
Gilles Peskine449bd832023-01-11 14:50:10 +01009420 MBEDTLS_SSL_CHK_BUF_READ_PTR(p, end, 4);
XiaokangQianacb39922022-06-17 10:18:48 +00009421
Gilles Peskine449bd832023-01-11 14:50:10 +01009422 protocol_name_list_len = MBEDTLS_GET_UINT16_BE(p, 0);
XiaokangQianacb39922022-06-17 10:18:48 +00009423 p += 2;
Gilles Peskine449bd832023-01-11 14:50:10 +01009424 MBEDTLS_SSL_CHK_BUF_READ_PTR(p, end, protocol_name_list_len);
XiaokangQian95d5f542022-06-24 02:29:26 +00009425 protocol_name_list = p;
9426 protocol_name_list_end = p + protocol_name_list_len;
XiaokangQianacb39922022-06-17 10:18:48 +00009427
9428 /* Validate peer's list (lengths) */
Gilles Peskine449bd832023-01-11 14:50:10 +01009429 while (p < protocol_name_list_end) {
XiaokangQian95d5f542022-06-24 02:29:26 +00009430 protocol_name_len = *p++;
Gilles Peskine449bd832023-01-11 14:50:10 +01009431 MBEDTLS_SSL_CHK_BUF_READ_PTR(p, protocol_name_list_end,
9432 protocol_name_len);
9433 if (protocol_name_len == 0) {
XiaokangQian95d5f542022-06-24 02:29:26 +00009434 MBEDTLS_SSL_PEND_FATAL_ALERT(
9435 MBEDTLS_SSL_ALERT_MSG_ILLEGAL_PARAMETER,
Gilles Peskine449bd832023-01-11 14:50:10 +01009436 MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER);
9437 return MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER;
XiaokangQian95d5f542022-06-24 02:29:26 +00009438 }
9439
9440 p += protocol_name_len;
XiaokangQianacb39922022-06-17 10:18:48 +00009441 }
9442
9443 /* Use our order of preference */
Gilles Peskine449bd832023-01-11 14:50:10 +01009444 for (const char **alpn = ssl->conf->alpn_list; *alpn != NULL; alpn++) {
9445 size_t const alpn_len = strlen(*alpn);
XiaokangQian95d5f542022-06-24 02:29:26 +00009446 p = protocol_name_list;
Gilles Peskine449bd832023-01-11 14:50:10 +01009447 while (p < protocol_name_list_end) {
XiaokangQian95d5f542022-06-24 02:29:26 +00009448 protocol_name_len = *p++;
Gilles Peskine449bd832023-01-11 14:50:10 +01009449 if (protocol_name_len == alpn_len &&
9450 memcmp(p, *alpn, alpn_len) == 0) {
XiaokangQianacb39922022-06-17 10:18:48 +00009451 ssl->alpn_chosen = *alpn;
Gilles Peskine449bd832023-01-11 14:50:10 +01009452 return 0;
XiaokangQianacb39922022-06-17 10:18:48 +00009453 }
XiaokangQian95d5f542022-06-24 02:29:26 +00009454
9455 p += protocol_name_len;
XiaokangQianacb39922022-06-17 10:18:48 +00009456 }
9457 }
9458
XiaokangQian95d5f542022-06-24 02:29:26 +00009459 /* If we get here, no match was found */
XiaokangQianacb39922022-06-17 10:18:48 +00009460 MBEDTLS_SSL_PEND_FATAL_ALERT(
Gilles Peskine449bd832023-01-11 14:50:10 +01009461 MBEDTLS_SSL_ALERT_MSG_NO_APPLICATION_PROTOCOL,
9462 MBEDTLS_ERR_SSL_NO_APPLICATION_PROTOCOL);
9463 return MBEDTLS_ERR_SSL_NO_APPLICATION_PROTOCOL;
XiaokangQianacb39922022-06-17 10:18:48 +00009464}
9465
Gilles Peskine449bd832023-01-11 14:50:10 +01009466int mbedtls_ssl_write_alpn_ext(mbedtls_ssl_context *ssl,
9467 unsigned char *buf,
9468 unsigned char *end,
9469 size_t *out_len)
XiaokangQianacb39922022-06-17 10:18:48 +00009470{
9471 unsigned char *p = buf;
XiaokangQian95d5f542022-06-24 02:29:26 +00009472 size_t protocol_name_len;
XiaokangQianc7403452022-06-23 03:24:12 +00009473 *out_len = 0;
XiaokangQianacb39922022-06-17 10:18:48 +00009474
Gilles Peskine449bd832023-01-11 14:50:10 +01009475 if (ssl->alpn_chosen == NULL) {
9476 return 0;
XiaokangQianacb39922022-06-17 10:18:48 +00009477 }
9478
Gilles Peskine449bd832023-01-11 14:50:10 +01009479 protocol_name_len = strlen(ssl->alpn_chosen);
9480 MBEDTLS_SSL_CHK_BUF_PTR(p, end, 7 + protocol_name_len);
XiaokangQianacb39922022-06-17 10:18:48 +00009481
Gilles Peskine449bd832023-01-11 14:50:10 +01009482 MBEDTLS_SSL_DEBUG_MSG(3, ("server side, adding alpn extension"));
XiaokangQianacb39922022-06-17 10:18:48 +00009483 /*
9484 * 0 . 1 ext identifier
9485 * 2 . 3 ext length
9486 * 4 . 5 protocol list length
9487 * 6 . 6 protocol name length
9488 * 7 . 7+n protocol name
9489 */
Gilles Peskine449bd832023-01-11 14:50:10 +01009490 MBEDTLS_PUT_UINT16_BE(MBEDTLS_TLS_EXT_ALPN, p, 0);
XiaokangQianacb39922022-06-17 10:18:48 +00009491
XiaokangQian95d5f542022-06-24 02:29:26 +00009492 *out_len = 7 + protocol_name_len;
XiaokangQianacb39922022-06-17 10:18:48 +00009493
Gilles Peskine449bd832023-01-11 14:50:10 +01009494 MBEDTLS_PUT_UINT16_BE(protocol_name_len + 3, p, 2);
9495 MBEDTLS_PUT_UINT16_BE(protocol_name_len + 1, p, 4);
XiaokangQian0b776e22022-06-24 09:04:59 +00009496 /* Note: the length of the chosen protocol has been checked to be less
9497 * than 255 bytes in `mbedtls_ssl_conf_alpn_protocols`.
9498 */
Gilles Peskine449bd832023-01-11 14:50:10 +01009499 p[6] = MBEDTLS_BYTE_0(protocol_name_len);
XiaokangQianacb39922022-06-17 10:18:48 +00009500
Gilles Peskine449bd832023-01-11 14:50:10 +01009501 memcpy(p + 7, ssl->alpn_chosen, protocol_name_len);
Jerry Yub95dd362022-11-08 21:19:34 +08009502
9503#if defined(MBEDTLS_SSL_PROTO_TLS1_3)
Gilles Peskine449bd832023-01-11 14:50:10 +01009504 mbedtls_ssl_tls13_set_hs_sent_ext_mask(ssl, MBEDTLS_TLS_EXT_ALPN);
Jerry Yub95dd362022-11-08 21:19:34 +08009505#endif
9506
Gilles Peskine449bd832023-01-11 14:50:10 +01009507 return 0;
XiaokangQianacb39922022-06-17 10:18:48 +00009508}
9509#endif /* MBEDTLS_SSL_ALPN */
9510
Xiaokang Qiana3b451f2022-10-11 06:20:56 +00009511#if defined(MBEDTLS_SSL_PROTO_TLS1_3) && \
Xiaokang Qian03409292022-10-12 02:49:52 +00009512 defined(MBEDTLS_SSL_SESSION_TICKETS) && \
Xiaokang Qianed0620c2022-10-12 06:58:13 +00009513 defined(MBEDTLS_SSL_SERVER_NAME_INDICATION) && \
Xiaokang Qianed3afcd2022-10-12 08:31:11 +00009514 defined(MBEDTLS_SSL_CLI_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01009515int mbedtls_ssl_session_set_hostname(mbedtls_ssl_session *session,
9516 const char *hostname)
Xiaokang Qiana3b451f2022-10-11 06:20:56 +00009517{
9518 /* Initialize to suppress unnecessary compiler warning */
9519 size_t hostname_len = 0;
9520
9521 /* Check if new hostname is valid before
9522 * making any change to current one */
Gilles Peskine449bd832023-01-11 14:50:10 +01009523 if (hostname != NULL) {
9524 hostname_len = strlen(hostname);
Xiaokang Qiana3b451f2022-10-11 06:20:56 +00009525
Gilles Peskine449bd832023-01-11 14:50:10 +01009526 if (hostname_len > MBEDTLS_SSL_MAX_HOST_NAME_LEN) {
9527 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
9528 }
Xiaokang Qiana3b451f2022-10-11 06:20:56 +00009529 }
9530
9531 /* Now it's clear that we will overwrite the old hostname,
9532 * so we can free it safely */
Gilles Peskine449bd832023-01-11 14:50:10 +01009533 if (session->hostname != NULL) {
9534 mbedtls_platform_zeroize(session->hostname,
9535 strlen(session->hostname));
9536 mbedtls_free(session->hostname);
Xiaokang Qiana3b451f2022-10-11 06:20:56 +00009537 }
9538
9539 /* Passing NULL as hostname shall clear the old one */
Gilles Peskine449bd832023-01-11 14:50:10 +01009540 if (hostname == NULL) {
Xiaokang Qiana3b451f2022-10-11 06:20:56 +00009541 session->hostname = NULL;
Gilles Peskine449bd832023-01-11 14:50:10 +01009542 } else {
9543 session->hostname = mbedtls_calloc(1, hostname_len + 1);
9544 if (session->hostname == NULL) {
9545 return MBEDTLS_ERR_SSL_ALLOC_FAILED;
9546 }
Xiaokang Qiana3b451f2022-10-11 06:20:56 +00009547
Gilles Peskine449bd832023-01-11 14:50:10 +01009548 memcpy(session->hostname, hostname, hostname_len);
Xiaokang Qiana3b451f2022-10-11 06:20:56 +00009549 }
9550
Gilles Peskine449bd832023-01-11 14:50:10 +01009551 return 0;
Xiaokang Qiana3b451f2022-10-11 06:20:56 +00009552}
Xiaokang Qian03409292022-10-12 02:49:52 +00009553#endif /* MBEDTLS_SSL_PROTO_TLS1_3 &&
9554 MBEDTLS_SSL_SESSION_TICKETS &&
Xiaokang Qianed0620c2022-10-12 06:58:13 +00009555 MBEDTLS_SSL_SERVER_NAME_INDICATION &&
Xiaokang Qianed3afcd2022-10-12 08:31:11 +00009556 MBEDTLS_SSL_CLI_C */
Xiaokang Qiana3b451f2022-10-11 06:20:56 +00009557
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009558#endif /* MBEDTLS_SSL_TLS_C */