blob: 319628529af4c0504e7258faeabdca0fa29c5f74 [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
Gilles Peskine449bd832023-01-11 14:50:10 +0100791void mbedtls_ssl_add_hs_hdr_to_checksum(mbedtls_ssl_context *ssl,
792 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
Gilles Peskine449bd832023-01-11 14:50:10 +0100803 ssl->handshake->update_checksum(ssl, hs_hdr, sizeof(hs_hdr));
Ronald Cron8f6d39a2022-03-10 18:56:50 +0100804}
805
Gilles Peskine449bd832023-01-11 14:50:10 +0100806void mbedtls_ssl_add_hs_msg_to_checksum(mbedtls_ssl_context *ssl,
807 unsigned hs_type,
808 unsigned char const *msg,
809 size_t msg_len)
Ronald Cron8f6d39a2022-03-10 18:56:50 +0100810{
Gilles Peskine449bd832023-01-11 14:50:10 +0100811 mbedtls_ssl_add_hs_hdr_to_checksum(ssl, hs_type, msg_len);
812 ssl->handshake->update_checksum(ssl, msg, msg_len);
Ronald Cron8f6d39a2022-03-10 18:56:50 +0100813}
814
Manuel Pégourié-Gonnard226aa152023-02-05 09:46:59 +0100815int mbedtls_ssl_reset_checksum(mbedtls_ssl_context *ssl)
Jerry Yuc73c6182022-02-08 20:29:25 +0800816{
817 ((void) ssl);
Andrzej Kurek25f27152022-08-17 16:09:31 -0400818#if defined(MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Jerry Yuc73c6182022-02-08 20:29:25 +0800819#if defined(MBEDTLS_USE_PSA_CRYPTO)
Gilles Peskine449bd832023-01-11 14:50:10 +0100820 psa_hash_abort(&ssl->handshake->fin_sha256_psa);
821 psa_hash_setup(&ssl->handshake->fin_sha256_psa, PSA_ALG_SHA_256);
Jerry Yuc73c6182022-02-08 20:29:25 +0800822#else
Gilles Peskine449bd832023-01-11 14:50:10 +0100823 mbedtls_sha256_starts(&ssl->handshake->fin_sha256, 0);
Jerry Yuc73c6182022-02-08 20:29:25 +0800824#endif
825#endif
Andrzej Kurek25f27152022-08-17 16:09:31 -0400826#if defined(MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Jerry Yuc73c6182022-02-08 20:29:25 +0800827#if defined(MBEDTLS_USE_PSA_CRYPTO)
Gilles Peskine449bd832023-01-11 14:50:10 +0100828 psa_hash_abort(&ssl->handshake->fin_sha384_psa);
829 psa_hash_setup(&ssl->handshake->fin_sha384_psa, PSA_ALG_SHA_384);
Jerry Yuc73c6182022-02-08 20:29:25 +0800830#else
Gilles Peskine449bd832023-01-11 14:50:10 +0100831 mbedtls_sha512_starts(&ssl->handshake->fin_sha384, 1);
Jerry Yuc73c6182022-02-08 20:29:25 +0800832#endif
833#endif
Manuel Pégourié-Gonnard226aa152023-02-05 09:46:59 +0100834 return 0;
Jerry Yuc73c6182022-02-08 20:29:25 +0800835}
836
Manuel Pégourié-Gonnard226aa152023-02-05 09:46:59 +0100837static int ssl_update_checksum_start(mbedtls_ssl_context *ssl,
Gilles Peskine449bd832023-01-11 14:50:10 +0100838 const unsigned char *buf, size_t len)
Jerry Yuc73c6182022-02-08 20:29:25 +0800839{
Andrzej Kurek25f27152022-08-17 16:09:31 -0400840#if defined(MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Jerry Yuc73c6182022-02-08 20:29:25 +0800841#if defined(MBEDTLS_USE_PSA_CRYPTO)
Gilles Peskine449bd832023-01-11 14:50:10 +0100842 psa_hash_update(&ssl->handshake->fin_sha256_psa, buf, len);
Jerry Yuc73c6182022-02-08 20:29:25 +0800843#else
Gilles Peskine449bd832023-01-11 14:50:10 +0100844 mbedtls_sha256_update(&ssl->handshake->fin_sha256, buf, len);
Jerry Yuc73c6182022-02-08 20:29:25 +0800845#endif
846#endif
Andrzej Kurek25f27152022-08-17 16:09:31 -0400847#if defined(MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Jerry Yuc73c6182022-02-08 20:29:25 +0800848#if defined(MBEDTLS_USE_PSA_CRYPTO)
Gilles Peskine449bd832023-01-11 14:50:10 +0100849 psa_hash_update(&ssl->handshake->fin_sha384_psa, buf, len);
Jerry Yuc73c6182022-02-08 20:29:25 +0800850#else
Gilles Peskine449bd832023-01-11 14:50:10 +0100851 mbedtls_sha512_update(&ssl->handshake->fin_sha384, buf, len);
Jerry Yuc73c6182022-02-08 20:29:25 +0800852#endif
853#endif
Andrzej Kurekeabeb302022-10-17 07:52:51 -0400854#if !defined(MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA) && \
855 !defined(MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
856 (void) ssl;
857 (void) buf;
858 (void) len;
859#endif
Manuel Pégourié-Gonnard226aa152023-02-05 09:46:59 +0100860 return 0;
Jerry Yuc73c6182022-02-08 20:29:25 +0800861}
862
Andrzej Kurek25f27152022-08-17 16:09:31 -0400863#if defined(MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Manuel Pégourié-Gonnard226aa152023-02-05 09:46:59 +0100864static int ssl_update_checksum_sha256(mbedtls_ssl_context *ssl,
Gilles Peskine449bd832023-01-11 14:50:10 +0100865 const unsigned char *buf, size_t len)
Jerry Yuc73c6182022-02-08 20:29:25 +0800866{
867#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
Manuel Pégourié-Gonnard226aa152023-02-05 09:46:59 +0100872 return 0;
Jerry Yuc73c6182022-02-08 20:29:25 +0800873}
874#endif
875
Andrzej Kurek25f27152022-08-17 16:09:31 -0400876#if defined(MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Manuel Pégourié-Gonnard226aa152023-02-05 09:46:59 +0100877static int ssl_update_checksum_sha384(mbedtls_ssl_context *ssl,
Gilles Peskine449bd832023-01-11 14:50:10 +0100878 const unsigned char *buf, size_t len)
Jerry Yuc73c6182022-02-08 20:29:25 +0800879{
880#if defined(MBEDTLS_USE_PSA_CRYPTO)
Gilles Peskine449bd832023-01-11 14:50:10 +0100881 psa_hash_update(&ssl->handshake->fin_sha384_psa, buf, len);
Jerry Yuc73c6182022-02-08 20:29:25 +0800882#else
Gilles Peskine449bd832023-01-11 14:50:10 +0100883 mbedtls_sha512_update(&ssl->handshake->fin_sha384, buf, len);
Jerry Yuc73c6182022-02-08 20:29:25 +0800884#endif
Manuel Pégourié-Gonnard226aa152023-02-05 09:46:59 +0100885 return 0;
Jerry Yuc73c6182022-02-08 20:29:25 +0800886}
887#endif
888
Gilles Peskine449bd832023-01-11 14:50:10 +0100889static void ssl_handshake_params_init(mbedtls_ssl_handshake_params *handshake)
Paul Bakkeraccaffe2014-06-26 13:37:14 +0200890{
Gilles Peskine449bd832023-01-11 14:50:10 +0100891 memset(handshake, 0, sizeof(mbedtls_ssl_handshake_params));
Paul Bakkeraccaffe2014-06-26 13:37:14 +0200892
Andrzej Kurek25f27152022-08-17 16:09:31 -0400893#if defined(MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Andrzej Kurekeb342242019-01-29 09:14:33 -0500894#if defined(MBEDTLS_USE_PSA_CRYPTO)
895 handshake->fin_sha256_psa = psa_hash_operation_init();
Gilles Peskine449bd832023-01-11 14:50:10 +0100896 psa_hash_setup(&handshake->fin_sha256_psa, PSA_ALG_SHA_256);
Andrzej Kurekeb342242019-01-29 09:14:33 -0500897#else
Gilles Peskine449bd832023-01-11 14:50:10 +0100898 mbedtls_sha256_init(&handshake->fin_sha256);
899 mbedtls_sha256_starts(&handshake->fin_sha256, 0);
Paul Bakkeraccaffe2014-06-26 13:37:14 +0200900#endif
Andrzej Kurekeb342242019-01-29 09:14:33 -0500901#endif
Andrzej Kurek25f27152022-08-17 16:09:31 -0400902#if defined(MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Andrzej Kurekeb342242019-01-29 09:14:33 -0500903#if defined(MBEDTLS_USE_PSA_CRYPTO)
Andrzej Kurek972fba52019-01-30 03:29:12 -0500904 handshake->fin_sha384_psa = psa_hash_operation_init();
Gilles Peskine449bd832023-01-11 14:50:10 +0100905 psa_hash_setup(&handshake->fin_sha384_psa, PSA_ALG_SHA_384);
Andrzej Kurekeb342242019-01-29 09:14:33 -0500906#else
Gilles Peskine449bd832023-01-11 14:50:10 +0100907 mbedtls_sha512_init(&handshake->fin_sha384);
908 mbedtls_sha512_starts(&handshake->fin_sha384, 1);
Paul Bakkeraccaffe2014-06-26 13:37:14 +0200909#endif
Andrzej Kurekeb342242019-01-29 09:14:33 -0500910#endif
Paul Bakkeraccaffe2014-06-26 13:37:14 +0200911
912 handshake->update_checksum = ssl_update_checksum_start;
Hanno Becker7e5437a2017-04-28 17:15:26 +0100913
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200914#if defined(MBEDTLS_DHM_C)
Gilles Peskine449bd832023-01-11 14:50:10 +0100915 mbedtls_dhm_init(&handshake->dhm_ctx);
Paul Bakkeraccaffe2014-06-26 13:37:14 +0200916#endif
Neil Armstrongf3f46412022-04-12 14:43:39 +0200917#if !defined(MBEDTLS_USE_PSA_CRYPTO) && defined(MBEDTLS_ECDH_C)
Gilles Peskine449bd832023-01-11 14:50:10 +0100918 mbedtls_ecdh_init(&handshake->ecdh_ctx);
Paul Bakkeraccaffe2014-06-26 13:37:14 +0200919#endif
Manuel Pégourié-Gonnardeef142d2015-09-16 10:05:04 +0200920#if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
Neil Armstrongca7d5062022-05-31 14:43:23 +0200921#if defined(MBEDTLS_USE_PSA_CRYPTO)
922 handshake->psa_pake_ctx = psa_pake_operation_init();
923 handshake->psa_pake_password = MBEDTLS_SVC_KEY_ID_INIT;
924#else
Gilles Peskine449bd832023-01-11 14:50:10 +0100925 mbedtls_ecjpake_init(&handshake->ecjpake_ctx);
Neil Armstrongca7d5062022-05-31 14:43:23 +0200926#endif /* MBEDTLS_USE_PSA_CRYPTO */
Manuel Pégourié-Gonnard77c06462015-09-17 13:59:49 +0200927#if defined(MBEDTLS_SSL_CLI_C)
928 handshake->ecjpake_cache = NULL;
929 handshake->ecjpake_cache_len = 0;
930#endif
Manuel Pégourié-Gonnard76cfd3f2015-09-15 12:10:54 +0200931#endif
Manuel Pégourié-Gonnardcdc26ae2015-06-19 12:16:31 +0200932
Gilles Peskineeccd8882020-03-10 12:19:08 +0100933#if defined(MBEDTLS_SSL_ECP_RESTARTABLE_ENABLED)
Gilles Peskine449bd832023-01-11 14:50:10 +0100934 mbedtls_x509_crt_restart_init(&handshake->ecrs_ctx);
Manuel Pégourié-Gonnard862cde52017-05-17 11:56:15 +0200935#endif
936
Manuel Pégourié-Gonnardcdc26ae2015-06-19 12:16:31 +0200937#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
938 handshake->sni_authmode = MBEDTLS_SSL_VERIFY_UNSET;
939#endif
Hanno Becker75173122019-02-06 16:18:31 +0000940
941#if defined(MBEDTLS_X509_CRT_PARSE_C) && \
942 !defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
Gilles Peskine449bd832023-01-11 14:50:10 +0100943 mbedtls_pk_init(&handshake->peer_pubkey);
Hanno Becker75173122019-02-06 16:18:31 +0000944#endif
Paul Bakkeraccaffe2014-06-26 13:37:14 +0200945}
946
Gilles Peskine449bd832023-01-11 14:50:10 +0100947void mbedtls_ssl_transform_init(mbedtls_ssl_transform *transform)
Paul Bakkeraccaffe2014-06-26 13:37:14 +0200948{
Gilles Peskine449bd832023-01-11 14:50:10 +0100949 memset(transform, 0, sizeof(mbedtls_ssl_transform));
Paul Bakker84bbeb52014-07-01 14:53:22 +0200950
Przemyslaw Stekiel8f80fb92022-01-11 08:28:13 +0100951#if defined(MBEDTLS_USE_PSA_CRYPTO)
952 transform->psa_key_enc = MBEDTLS_SVC_KEY_ID_INIT;
953 transform->psa_key_dec = MBEDTLS_SVC_KEY_ID_INIT;
Przemyslaw Stekiel6be9cf52022-01-19 16:00:22 +0100954#else
Gilles Peskine449bd832023-01-11 14:50:10 +0100955 mbedtls_cipher_init(&transform->cipher_ctx_enc);
956 mbedtls_cipher_init(&transform->cipher_ctx_dec);
Przemyslaw Stekiel8f80fb92022-01-11 08:28:13 +0100957#endif
958
Hanno Beckerfd86ca82020-11-30 08:54:23 +0000959#if defined(MBEDTLS_SSL_SOME_SUITES_USE_MAC)
Neil Armstrong39b8e7d2022-02-23 09:24:45 +0100960#if defined(MBEDTLS_USE_PSA_CRYPTO)
961 transform->psa_mac_enc = MBEDTLS_SVC_KEY_ID_INIT;
962 transform->psa_mac_dec = MBEDTLS_SVC_KEY_ID_INIT;
Neil Armstrongcf8841a2022-02-24 11:17:45 +0100963#else
Gilles Peskine449bd832023-01-11 14:50:10 +0100964 mbedtls_md_init(&transform->md_ctx_enc);
965 mbedtls_md_init(&transform->md_ctx_dec);
Hanno Beckerd56ed242018-01-03 15:32:51 +0000966#endif
Neil Armstrongcf8841a2022-02-24 11:17:45 +0100967#endif
Paul Bakkeraccaffe2014-06-26 13:37:14 +0200968}
969
Gilles Peskine449bd832023-01-11 14:50:10 +0100970void mbedtls_ssl_session_init(mbedtls_ssl_session *session)
Paul Bakkeraccaffe2014-06-26 13:37:14 +0200971{
Gilles Peskine449bd832023-01-11 14:50:10 +0100972 memset(session, 0, sizeof(mbedtls_ssl_session));
Paul Bakkeraccaffe2014-06-26 13:37:14 +0200973}
974
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +0200975MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +0100976static int ssl_handshake_init(mbedtls_ssl_context *ssl)
Paul Bakker48916f92012-09-16 19:57:18 +0000977{
Paul Bakkeraccaffe2014-06-26 13:37:14 +0200978 /* Clear old handshake information if present */
Jerry Yu2e199812022-12-01 18:57:19 +0800979#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
Gilles Peskine449bd832023-01-11 14:50:10 +0100980 if (ssl->transform_negotiate) {
981 mbedtls_ssl_transform_free(ssl->transform_negotiate);
982 }
Jerry Yu2e199812022-12-01 18:57:19 +0800983#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100984 if (ssl->session_negotiate) {
985 mbedtls_ssl_session_free(ssl->session_negotiate);
986 }
987 if (ssl->handshake) {
988 mbedtls_ssl_handshake_free(ssl);
989 }
Paul Bakkeraccaffe2014-06-26 13:37:14 +0200990
Jerry Yu2e199812022-12-01 18:57:19 +0800991#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
Paul Bakkeraccaffe2014-06-26 13:37:14 +0200992 /*
993 * Either the pointers are now NULL or cleared properly and can be freed.
994 * Now allocate missing structures.
995 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100996 if (ssl->transform_negotiate == NULL) {
997 ssl->transform_negotiate = mbedtls_calloc(1, sizeof(mbedtls_ssl_transform));
Paul Bakkerb9cfaa02013-10-11 18:58:55 +0200998 }
Jerry Yu2e199812022-12-01 18:57:19 +0800999#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakker48916f92012-09-16 19:57:18 +00001000
Gilles Peskine449bd832023-01-11 14:50:10 +01001001 if (ssl->session_negotiate == NULL) {
1002 ssl->session_negotiate = mbedtls_calloc(1, sizeof(mbedtls_ssl_session));
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02001003 }
Paul Bakker48916f92012-09-16 19:57:18 +00001004
Gilles Peskine449bd832023-01-11 14:50:10 +01001005 if (ssl->handshake == NULL) {
1006 ssl->handshake = mbedtls_calloc(1, sizeof(mbedtls_ssl_handshake_params));
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02001007 }
Andrzej Kurek0afa2a12020-03-03 10:39:58 -05001008#if defined(MBEDTLS_SSL_VARIABLE_BUFFER_LENGTH)
1009 /* If the buffers are too small - reallocate */
Andrzej Kurek8ea68722020-04-03 06:40:47 -04001010
Gilles Peskine449bd832023-01-11 14:50:10 +01001011 handle_buffer_resizing(ssl, 0, MBEDTLS_SSL_IN_BUFFER_LEN,
1012 MBEDTLS_SSL_OUT_BUFFER_LEN);
Andrzej Kurek0afa2a12020-03-03 10:39:58 -05001013#endif
Paul Bakker48916f92012-09-16 19:57:18 +00001014
Paul Bakkeraccaffe2014-06-26 13:37:14 +02001015 /* All pointers should exist and can be directly freed without issue */
Gilles Peskine449bd832023-01-11 14:50:10 +01001016 if (ssl->handshake == NULL ||
Jerry Yu2e199812022-12-01 18:57:19 +08001017#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
Paul Bakker48916f92012-09-16 19:57:18 +00001018 ssl->transform_negotiate == NULL ||
Jerry Yu2e199812022-12-01 18:57:19 +08001019#endif
Gilles Peskine449bd832023-01-11 14:50:10 +01001020 ssl->session_negotiate == NULL) {
1021 MBEDTLS_SSL_DEBUG_MSG(1, ("alloc() of ssl sub-contexts failed"));
Paul Bakkeraccaffe2014-06-26 13:37:14 +02001022
Gilles Peskine449bd832023-01-11 14:50:10 +01001023 mbedtls_free(ssl->handshake);
Paul Bakkeraccaffe2014-06-26 13:37:14 +02001024 ssl->handshake = NULL;
Jerry Yu2e199812022-12-01 18:57:19 +08001025
1026#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
Gilles Peskine449bd832023-01-11 14:50:10 +01001027 mbedtls_free(ssl->transform_negotiate);
Paul Bakkeraccaffe2014-06-26 13:37:14 +02001028 ssl->transform_negotiate = NULL;
Jerry Yu2e199812022-12-01 18:57:19 +08001029#endif
1030
Gilles Peskine449bd832023-01-11 14:50:10 +01001031 mbedtls_free(ssl->session_negotiate);
Paul Bakkeraccaffe2014-06-26 13:37:14 +02001032 ssl->session_negotiate = NULL;
1033
Gilles Peskine449bd832023-01-11 14:50:10 +01001034 return MBEDTLS_ERR_SSL_ALLOC_FAILED;
Paul Bakker48916f92012-09-16 19:57:18 +00001035 }
1036
Paul Bakkeraccaffe2014-06-26 13:37:14 +02001037 /* Initialize structures */
Gilles Peskine449bd832023-01-11 14:50:10 +01001038 mbedtls_ssl_session_init(ssl->session_negotiate);
1039 ssl_handshake_params_init(ssl->handshake);
Paul Bakker968afaa2014-07-09 11:09:24 +02001040
Jerry Yu2e199812022-12-01 18:57:19 +08001041#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
Gilles Peskine449bd832023-01-11 14:50:10 +01001042 mbedtls_ssl_transform_init(ssl->transform_negotiate);
Jerry Yu2e199812022-12-01 18:57:19 +08001043#endif
1044
Jerry Yud0766ec2022-09-22 10:46:57 +08001045#if defined(MBEDTLS_SSL_PROTO_TLS1_3) && \
1046 defined(MBEDTLS_SSL_SRV_C) && \
1047 defined(MBEDTLS_SSL_SESSION_TICKETS)
1048 ssl->handshake->new_session_tickets_count =
Gilles Peskine449bd832023-01-11 14:50:10 +01001049 ssl->conf->new_session_tickets_count;
Jerry Yud0766ec2022-09-22 10:46:57 +08001050#endif
1051
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001052#if defined(MBEDTLS_SSL_PROTO_DTLS)
Gilles Peskine449bd832023-01-11 14:50:10 +01001053 if (ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM) {
Manuel Pégourié-Gonnard06939ce2015-05-11 11:25:46 +02001054 ssl->handshake->alt_transform_out = ssl->transform_out;
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02001055
Gilles Peskine449bd832023-01-11 14:50:10 +01001056 if (ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT) {
Manuel Pégourié-Gonnard06939ce2015-05-11 11:25:46 +02001057 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_PREPARING;
Gilles Peskine449bd832023-01-11 14:50:10 +01001058 } else {
Manuel Pégourié-Gonnard06939ce2015-05-11 11:25:46 +02001059 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_WAITING;
Gilles Peskine449bd832023-01-11 14:50:10 +01001060 }
Manuel Pégourié-Gonnard286a1362015-05-13 16:22:05 +02001061
Gilles Peskine449bd832023-01-11 14:50:10 +01001062 mbedtls_ssl_set_timer(ssl, 0);
Manuel Pégourié-Gonnard06939ce2015-05-11 11:25:46 +02001063 }
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02001064#endif
1065
Brett Warrene0edc842021-08-17 09:53:13 +01001066/*
1067 * curve_list is translated to IANA TLS group identifiers here because
1068 * mbedtls_ssl_conf_curves returns void and so can't return
1069 * any error codes.
1070 */
1071#if defined(MBEDTLS_ECP_C)
1072#if !defined(MBEDTLS_DEPRECATED_REMOVED)
1073 /* Heap allocate and translate curve_list from internal to IANA group ids */
Gilles Peskine449bd832023-01-11 14:50:10 +01001074 if (ssl->conf->curve_list != NULL) {
Brett Warrene0edc842021-08-17 09:53:13 +01001075 size_t length;
1076 const mbedtls_ecp_group_id *curve_list = ssl->conf->curve_list;
1077
Gilles Peskine449bd832023-01-11 14:50:10 +01001078 for (length = 0; (curve_list[length] != MBEDTLS_ECP_DP_NONE) &&
1079 (length < MBEDTLS_ECP_DP_MAX); length++) {
1080 }
Brett Warrene0edc842021-08-17 09:53:13 +01001081
1082 /* Leave room for zero termination */
Gilles Peskine449bd832023-01-11 14:50:10 +01001083 uint16_t *group_list = mbedtls_calloc(length + 1, sizeof(uint16_t));
1084 if (group_list == NULL) {
1085 return MBEDTLS_ERR_SSL_ALLOC_FAILED;
1086 }
Brett Warrene0edc842021-08-17 09:53:13 +01001087
Gilles Peskine449bd832023-01-11 14:50:10 +01001088 for (size_t i = 0; i < length; i++) {
Valerio Setti18c9fed2022-12-30 17:44:24 +01001089 uint16_t tls_id = mbedtls_ssl_get_tls_id_from_ecp_group_id(
Gilles Peskine449bd832023-01-11 14:50:10 +01001090 curve_list[i]);
1091 if (tls_id == 0) {
1092 mbedtls_free(group_list);
1093 return MBEDTLS_ERR_SSL_BAD_CONFIG;
Brett Warrene0edc842021-08-17 09:53:13 +01001094 }
Valerio Setti18c9fed2022-12-30 17:44:24 +01001095 group_list[i] = tls_id;
Brett Warrene0edc842021-08-17 09:53:13 +01001096 }
1097
1098 group_list[length] = 0;
1099
1100 ssl->handshake->group_list = group_list;
1101 ssl->handshake->group_list_heap_allocated = 1;
Gilles Peskine449bd832023-01-11 14:50:10 +01001102 } else {
Brett Warrene0edc842021-08-17 09:53:13 +01001103 ssl->handshake->group_list = ssl->conf->group_list;
1104 ssl->handshake->group_list_heap_allocated = 0;
1105 }
1106#endif /* MBEDTLS_DEPRECATED_REMOVED */
1107#endif /* MBEDTLS_ECP_C */
1108
Ronald Crone68ab4f2022-10-05 12:46:29 +02001109#if defined(MBEDTLS_SSL_HANDSHAKE_WITH_CERT_ENABLED)
Jerry Yuf017ee42022-01-12 15:49:48 +08001110#if !defined(MBEDTLS_DEPRECATED_REMOVED)
Jerry Yua69269a2022-01-17 21:06:01 +08001111#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
Jerry Yu713013f2022-01-17 18:16:35 +08001112 /* Heap allocate and translate sig_hashes from internal hash identifiers to
1113 signature algorithms IANA identifiers. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001114 if (mbedtls_ssl_conf_is_tls12_only(ssl->conf) &&
1115 ssl->conf->sig_hashes != NULL) {
Jerry Yuf017ee42022-01-12 15:49:48 +08001116 const int *md;
1117 const int *sig_hashes = ssl->conf->sig_hashes;
Jerry Yub476a442022-01-21 18:14:45 +08001118 size_t sig_algs_len = 0;
Jerry Yuf017ee42022-01-12 15:49:48 +08001119 uint16_t *p;
1120
Jerry Yub476a442022-01-21 18:14:45 +08001121#if defined(static_assert)
Gilles Peskine449bd832023-01-11 14:50:10 +01001122 static_assert(MBEDTLS_SSL_MAX_SIG_ALG_LIST_LEN
1123 <= (SIZE_MAX - (2 * sizeof(uint16_t))),
1124 "MBEDTLS_SSL_MAX_SIG_ALG_LIST_LEN too big");
Jerry Yua68dca22022-01-20 16:28:27 +08001125#endif
1126
Gilles Peskine449bd832023-01-11 14:50:10 +01001127 for (md = sig_hashes; *md != MBEDTLS_MD_NONE; md++) {
1128 if (mbedtls_ssl_hash_from_md_alg(*md) == MBEDTLS_SSL_HASH_NONE) {
Jerry Yuf017ee42022-01-12 15:49:48 +08001129 continue;
Gilles Peskine449bd832023-01-11 14:50:10 +01001130 }
Jerry Yub476a442022-01-21 18:14:45 +08001131#if defined(MBEDTLS_ECDSA_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01001132 sig_algs_len += sizeof(uint16_t);
Jerry Yub476a442022-01-21 18:14:45 +08001133#endif
Jerry Yua68dca22022-01-20 16:28:27 +08001134
Jerry Yub476a442022-01-21 18:14:45 +08001135#if defined(MBEDTLS_RSA_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01001136 sig_algs_len += sizeof(uint16_t);
Jerry Yub476a442022-01-21 18:14:45 +08001137#endif
Gilles Peskine449bd832023-01-11 14:50:10 +01001138 if (sig_algs_len > MBEDTLS_SSL_MAX_SIG_ALG_LIST_LEN) {
1139 return MBEDTLS_ERR_SSL_BAD_CONFIG;
1140 }
Jerry Yuf017ee42022-01-12 15:49:48 +08001141 }
1142
Gilles Peskine449bd832023-01-11 14:50:10 +01001143 if (sig_algs_len < MBEDTLS_SSL_MIN_SIG_ALG_LIST_LEN) {
1144 return MBEDTLS_ERR_SSL_BAD_CONFIG;
1145 }
Jerry Yuf017ee42022-01-12 15:49:48 +08001146
Gilles Peskine449bd832023-01-11 14:50:10 +01001147 ssl->handshake->sig_algs = mbedtls_calloc(1, sig_algs_len +
1148 sizeof(uint16_t));
1149 if (ssl->handshake->sig_algs == NULL) {
1150 return MBEDTLS_ERR_SSL_ALLOC_FAILED;
1151 }
Jerry Yuf017ee42022-01-12 15:49:48 +08001152
Gilles Peskine449bd832023-01-11 14:50:10 +01001153 p = (uint16_t *) ssl->handshake->sig_algs;
1154 for (md = sig_hashes; *md != MBEDTLS_MD_NONE; md++) {
1155 unsigned char hash = mbedtls_ssl_hash_from_md_alg(*md);
1156 if (hash == MBEDTLS_SSL_HASH_NONE) {
Jerry Yuf017ee42022-01-12 15:49:48 +08001157 continue;
Gilles Peskine449bd832023-01-11 14:50:10 +01001158 }
Jerry Yu6106fdc2022-01-12 16:36:14 +08001159#if defined(MBEDTLS_ECDSA_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01001160 *p = ((hash << 8) | MBEDTLS_SSL_SIG_ECDSA);
Jerry Yuf017ee42022-01-12 15:49:48 +08001161 p++;
Jerry Yu6106fdc2022-01-12 16:36:14 +08001162#endif
1163#if defined(MBEDTLS_RSA_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01001164 *p = ((hash << 8) | MBEDTLS_SSL_SIG_RSA);
Jerry Yuf017ee42022-01-12 15:49:48 +08001165 p++;
Jerry Yu6106fdc2022-01-12 16:36:14 +08001166#endif
Jerry Yuf017ee42022-01-12 15:49:48 +08001167 }
Gabor Mezei15b95a62022-05-09 16:37:58 +02001168 *p = MBEDTLS_TLS_SIG_NONE;
Jerry Yuf017ee42022-01-12 15:49:48 +08001169 ssl->handshake->sig_algs_heap_allocated = 1;
Gilles Peskine449bd832023-01-11 14:50:10 +01001170 } else
Jerry Yua69269a2022-01-17 21:06:01 +08001171#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Jerry Yuf017ee42022-01-12 15:49:48 +08001172 {
Jerry Yuf017ee42022-01-12 15:49:48 +08001173 ssl->handshake->sig_algs_heap_allocated = 0;
1174 }
Jerry Yucc539102022-06-27 16:27:35 +08001175#endif /* !MBEDTLS_DEPRECATED_REMOVED */
Ronald Crone68ab4f2022-10-05 12:46:29 +02001176#endif /* MBEDTLS_SSL_HANDSHAKE_WITH_CERT_ENABLED */
Gilles Peskine449bd832023-01-11 14:50:10 +01001177 return 0;
Paul Bakker48916f92012-09-16 19:57:18 +00001178}
1179
Manuel Pégourié-Gonnarde057d3b2015-05-20 10:59:43 +02001180#if defined(MBEDTLS_SSL_DTLS_HELLO_VERIFY) && defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard7d38d212014-07-23 17:52:09 +02001181/* Dummy cookie callbacks for defaults */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02001182MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01001183static int ssl_cookie_write_dummy(void *ctx,
1184 unsigned char **p, unsigned char *end,
1185 const unsigned char *cli_id, size_t cli_id_len)
Manuel Pégourié-Gonnard7d38d212014-07-23 17:52:09 +02001186{
1187 ((void) ctx);
1188 ((void) p);
1189 ((void) end);
1190 ((void) cli_id);
1191 ((void) cli_id_len);
1192
Gilles Peskine449bd832023-01-11 14:50:10 +01001193 return MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
Manuel Pégourié-Gonnard7d38d212014-07-23 17:52:09 +02001194}
1195
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02001196MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01001197static int ssl_cookie_check_dummy(void *ctx,
1198 const unsigned char *cookie, size_t cookie_len,
1199 const unsigned char *cli_id, size_t cli_id_len)
Manuel Pégourié-Gonnard7d38d212014-07-23 17:52:09 +02001200{
1201 ((void) ctx);
1202 ((void) cookie);
1203 ((void) cookie_len);
1204 ((void) cli_id);
1205 ((void) cli_id_len);
1206
Gilles Peskine449bd832023-01-11 14:50:10 +01001207 return MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
Manuel Pégourié-Gonnard7d38d212014-07-23 17:52:09 +02001208}
Manuel Pégourié-Gonnarde057d3b2015-05-20 10:59:43 +02001209#endif /* MBEDTLS_SSL_DTLS_HELLO_VERIFY && MBEDTLS_SSL_SRV_C */
Manuel Pégourié-Gonnard7d38d212014-07-23 17:52:09 +02001210
Paul Bakker5121ce52009-01-03 21:22:43 +00001211/*
1212 * Initialize an SSL context
1213 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001214void mbedtls_ssl_init(mbedtls_ssl_context *ssl)
Manuel Pégourié-Gonnard41d479e2015-04-29 00:48:22 +02001215{
Gilles Peskine449bd832023-01-11 14:50:10 +01001216 memset(ssl, 0, sizeof(mbedtls_ssl_context));
Manuel Pégourié-Gonnard41d479e2015-04-29 00:48:22 +02001217}
1218
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02001219MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01001220static int ssl_conf_version_check(const mbedtls_ssl_context *ssl)
Jerry Yu60835a82021-08-04 10:13:52 +08001221{
Ronald Cron086ee0b2022-03-15 15:18:51 +01001222 const mbedtls_ssl_config *conf = ssl->conf;
1223
Ronald Cron6f135e12021-12-08 16:57:54 +01001224#if defined(MBEDTLS_SSL_PROTO_TLS1_3)
Gilles Peskine449bd832023-01-11 14:50:10 +01001225 if (mbedtls_ssl_conf_is_tls13_only(conf)) {
1226 if (conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM) {
1227 MBEDTLS_SSL_DEBUG_MSG(1, ("DTLS 1.3 is not yet supported."));
1228 return MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
XiaokangQianed582dd2022-04-13 08:21:05 +00001229 }
1230
Gilles Peskine449bd832023-01-11 14:50:10 +01001231 MBEDTLS_SSL_DEBUG_MSG(4, ("The SSL configuration is tls13 only."));
1232 return 0;
Jerry Yu60835a82021-08-04 10:13:52 +08001233 }
1234#endif
1235
1236#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
Gilles Peskine449bd832023-01-11 14:50:10 +01001237 if (mbedtls_ssl_conf_is_tls12_only(conf)) {
1238 MBEDTLS_SSL_DEBUG_MSG(4, ("The SSL configuration is tls12 only."));
1239 return 0;
Jerry Yu60835a82021-08-04 10:13:52 +08001240 }
1241#endif
1242
Ronald Cron6f135e12021-12-08 16:57:54 +01001243#if defined(MBEDTLS_SSL_PROTO_TLS1_2) && defined(MBEDTLS_SSL_PROTO_TLS1_3)
Gilles Peskine449bd832023-01-11 14:50:10 +01001244 if (mbedtls_ssl_conf_is_hybrid_tls12_tls13(conf)) {
1245 if (ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM) {
1246 MBEDTLS_SSL_DEBUG_MSG(1, ("DTLS not yet supported in Hybrid TLS 1.3 + TLS 1.2"));
1247 return MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
XiaokangQianed582dd2022-04-13 08:21:05 +00001248 }
1249
Gilles Peskine449bd832023-01-11 14:50:10 +01001250 if (conf->endpoint == MBEDTLS_SSL_IS_SERVER) {
1251 MBEDTLS_SSL_DEBUG_MSG(1, ("TLS 1.3 server is not supported yet."));
1252 return MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
XiaokangQian8f9dfe42022-04-15 02:52:39 +00001253 }
1254
1255
Gilles Peskine449bd832023-01-11 14:50:10 +01001256 MBEDTLS_SSL_DEBUG_MSG(4, ("The SSL configuration is TLS 1.3 or TLS 1.2."));
1257 return 0;
Jerry Yu60835a82021-08-04 10:13:52 +08001258 }
1259#endif
1260
Gilles Peskine449bd832023-01-11 14:50:10 +01001261 MBEDTLS_SSL_DEBUG_MSG(1, ("The SSL configuration is invalid."));
1262 return MBEDTLS_ERR_SSL_BAD_CONFIG;
Jerry Yu60835a82021-08-04 10:13:52 +08001263}
1264
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02001265MBEDTLS_CHECK_RETURN_CRITICAL
Jerry Yu60835a82021-08-04 10:13:52 +08001266static int ssl_conf_check(const mbedtls_ssl_context *ssl)
1267{
1268 int ret;
Gilles Peskine449bd832023-01-11 14:50:10 +01001269 ret = ssl_conf_version_check(ssl);
1270 if (ret != 0) {
1271 return ret;
1272 }
Jerry Yu60835a82021-08-04 10:13:52 +08001273
Jerry Yudef7ae42022-10-30 14:13:19 +08001274#if defined(MBEDTLS_SSL_PROTO_TLS1_3)
1275 /* RFC 8446 section 4.4.3
1276 *
1277 * If the verification fails, the receiver MUST terminate the handshake with
1278 * a "decrypt_error" alert.
1279 *
1280 * If the client is configured as TLS 1.3 only with optional verify, return
1281 * bad config.
1282 *
1283 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001284 if (mbedtls_ssl_conf_tls13_ephemeral_enabled(
1285 (mbedtls_ssl_context *) ssl) &&
Jerry Yudef7ae42022-10-30 14:13:19 +08001286 ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT &&
1287 ssl->conf->max_tls_version == MBEDTLS_SSL_VERSION_TLS1_3 &&
1288 ssl->conf->min_tls_version == MBEDTLS_SSL_VERSION_TLS1_3 &&
Gilles Peskine449bd832023-01-11 14:50:10 +01001289 ssl->conf->authmode == MBEDTLS_SSL_VERIFY_OPTIONAL) {
Jerry Yudef7ae42022-10-30 14:13:19 +08001290 MBEDTLS_SSL_DEBUG_MSG(
Gilles Peskine449bd832023-01-11 14:50:10 +01001291 1, ("Optional verify auth mode "
1292 "is not available for TLS 1.3 client"));
1293 return MBEDTLS_ERR_SSL_BAD_CONFIG;
Jerry Yudef7ae42022-10-30 14:13:19 +08001294 }
1295#endif /* MBEDTLS_SSL_PROTO_TLS1_3 */
1296
Jerry Yu60835a82021-08-04 10:13:52 +08001297 /* Space for further checks */
1298
Gilles Peskine449bd832023-01-11 14:50:10 +01001299 return 0;
Jerry Yu60835a82021-08-04 10:13:52 +08001300}
1301
Manuel Pégourié-Gonnard41d479e2015-04-29 00:48:22 +02001302/*
1303 * Setup an SSL context
1304 */
Hanno Becker2a43f6f2018-08-10 11:12:52 +01001305
Gilles Peskine449bd832023-01-11 14:50:10 +01001306int mbedtls_ssl_setup(mbedtls_ssl_context *ssl,
1307 const mbedtls_ssl_config *conf)
Paul Bakker5121ce52009-01-03 21:22:43 +00001308{
Janos Follath865b3eb2019-12-16 11:46:15 +00001309 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Darryl Greenb33cc762019-11-28 14:29:44 +00001310 size_t in_buf_len = MBEDTLS_SSL_IN_BUFFER_LEN;
1311 size_t out_buf_len = MBEDTLS_SSL_OUT_BUFFER_LEN;
Paul Bakker5121ce52009-01-03 21:22:43 +00001312
Manuel Pégourié-Gonnarddef0bbe2015-05-04 14:56:36 +02001313 ssl->conf = conf;
Paul Bakker62f2dee2012-09-28 07:31:51 +00001314
Gilles Peskine449bd832023-01-11 14:50:10 +01001315 if ((ret = ssl_conf_check(ssl)) != 0) {
1316 return ret;
1317 }
Jerry Yu60835a82021-08-04 10:13:52 +08001318
Paul Bakker62f2dee2012-09-28 07:31:51 +00001319 /*
Manuel Pégourié-Gonnard06193482014-02-14 08:39:32 +01001320 * Prepare base structures
Paul Bakker62f2dee2012-09-28 07:31:51 +00001321 */
k-stachowiakc9a5f022018-07-24 13:53:31 +02001322
1323 /* Set to NULL in case of an error condition */
1324 ssl->out_buf = NULL;
k-stachowiaka47911c2018-07-04 17:41:58 +02001325
Darryl Greenb33cc762019-11-28 14:29:44 +00001326#if defined(MBEDTLS_SSL_VARIABLE_BUFFER_LENGTH)
1327 ssl->in_buf_len = in_buf_len;
1328#endif
Gilles Peskine449bd832023-01-11 14:50:10 +01001329 ssl->in_buf = mbedtls_calloc(1, in_buf_len);
1330 if (ssl->in_buf == NULL) {
1331 MBEDTLS_SSL_DEBUG_MSG(1, ("alloc(%" MBEDTLS_PRINTF_SIZET " bytes) failed", in_buf_len));
k-stachowiak9f7798e2018-07-31 16:52:32 +02001332 ret = MBEDTLS_ERR_SSL_ALLOC_FAILED;
k-stachowiaka47911c2018-07-04 17:41:58 +02001333 goto error;
Angus Grattond8213d02016-05-25 20:56:48 +10001334 }
1335
Darryl Greenb33cc762019-11-28 14:29:44 +00001336#if defined(MBEDTLS_SSL_VARIABLE_BUFFER_LENGTH)
1337 ssl->out_buf_len = out_buf_len;
1338#endif
Gilles Peskine449bd832023-01-11 14:50:10 +01001339 ssl->out_buf = mbedtls_calloc(1, out_buf_len);
1340 if (ssl->out_buf == NULL) {
1341 MBEDTLS_SSL_DEBUG_MSG(1, ("alloc(%" MBEDTLS_PRINTF_SIZET " bytes) failed", out_buf_len));
k-stachowiak9f7798e2018-07-31 16:52:32 +02001342 ret = MBEDTLS_ERR_SSL_ALLOC_FAILED;
k-stachowiaka47911c2018-07-04 17:41:58 +02001343 goto error;
Paul Bakker5121ce52009-01-03 21:22:43 +00001344 }
1345
Gilles Peskine449bd832023-01-11 14:50:10 +01001346 mbedtls_ssl_reset_in_out_pointers(ssl);
Manuel Pégourié-Gonnard419d5ae2015-05-04 19:32:36 +02001347
Johan Pascalb62bb512015-12-03 21:56:45 +01001348#if defined(MBEDTLS_SSL_DTLS_SRTP)
Gilles Peskine449bd832023-01-11 14:50:10 +01001349 memset(&ssl->dtls_srtp_info, 0, sizeof(ssl->dtls_srtp_info));
Johan Pascalb62bb512015-12-03 21:56:45 +01001350#endif
1351
Gilles Peskine449bd832023-01-11 14:50:10 +01001352 if ((ret = ssl_handshake_init(ssl)) != 0) {
k-stachowiaka47911c2018-07-04 17:41:58 +02001353 goto error;
Gilles Peskine449bd832023-01-11 14:50:10 +01001354 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001355
Gilles Peskine449bd832023-01-11 14:50:10 +01001356 return 0;
k-stachowiaka47911c2018-07-04 17:41:58 +02001357
1358error:
Gilles Peskine449bd832023-01-11 14:50:10 +01001359 mbedtls_free(ssl->in_buf);
1360 mbedtls_free(ssl->out_buf);
k-stachowiaka47911c2018-07-04 17:41:58 +02001361
1362 ssl->conf = NULL;
1363
Darryl Greenb33cc762019-11-28 14:29:44 +00001364#if defined(MBEDTLS_SSL_VARIABLE_BUFFER_LENGTH)
1365 ssl->in_buf_len = 0;
1366 ssl->out_buf_len = 0;
1367#endif
k-stachowiaka47911c2018-07-04 17:41:58 +02001368 ssl->in_buf = NULL;
1369 ssl->out_buf = NULL;
1370
1371 ssl->in_hdr = NULL;
1372 ssl->in_ctr = NULL;
1373 ssl->in_len = NULL;
1374 ssl->in_iv = NULL;
1375 ssl->in_msg = NULL;
1376
1377 ssl->out_hdr = NULL;
1378 ssl->out_ctr = NULL;
1379 ssl->out_len = NULL;
1380 ssl->out_iv = NULL;
1381 ssl->out_msg = NULL;
1382
Gilles Peskine449bd832023-01-11 14:50:10 +01001383 return ret;
Paul Bakker5121ce52009-01-03 21:22:43 +00001384}
1385
1386/*
Paul Bakker7eb013f2011-10-06 12:37:39 +00001387 * Reset an initialized and used SSL context for re-use while retaining
1388 * all application-set variables, function pointers and data.
Manuel Pégourié-Gonnard3f09b6d2015-09-08 11:58:14 +02001389 *
1390 * If partial is non-zero, keep data in the input buffer and client ID.
1391 * (Use when a DTLS client reconnects from the same port.)
Paul Bakker7eb013f2011-10-06 12:37:39 +00001392 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001393void mbedtls_ssl_session_reset_msg_layer(mbedtls_ssl_context *ssl,
1394 int partial)
Paul Bakker7eb013f2011-10-06 12:37:39 +00001395{
Darryl Greenb33cc762019-11-28 14:29:44 +00001396#if defined(MBEDTLS_SSL_VARIABLE_BUFFER_LENGTH)
1397 size_t in_buf_len = ssl->in_buf_len;
1398 size_t out_buf_len = ssl->out_buf_len;
1399#else
1400 size_t in_buf_len = MBEDTLS_SSL_IN_BUFFER_LEN;
1401 size_t out_buf_len = MBEDTLS_SSL_OUT_BUFFER_LEN;
1402#endif
Paul Bakker48916f92012-09-16 19:57:18 +00001403
Hanno Beckerb0302c42021-08-03 09:39:42 +01001404#if !defined(MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE) || !defined(MBEDTLS_SSL_SRV_C)
1405 partial = 0;
Hanno Becker7e772132018-08-10 12:38:21 +01001406#endif
1407
Manuel Pégourié-Gonnard286a1362015-05-13 16:22:05 +02001408 /* Cancel any possibly running timer */
Gilles Peskine449bd832023-01-11 14:50:10 +01001409 mbedtls_ssl_set_timer(ssl, 0);
Manuel Pégourié-Gonnard286a1362015-05-13 16:22:05 +02001410
Gilles Peskine449bd832023-01-11 14:50:10 +01001411 mbedtls_ssl_reset_in_out_pointers(ssl);
Hanno Beckerb0302c42021-08-03 09:39:42 +01001412
1413 /* Reset incoming message parsing */
1414 ssl->in_offt = NULL;
1415 ssl->nb_zero = 0;
1416 ssl->in_msgtype = 0;
1417 ssl->in_msglen = 0;
1418 ssl->in_hslen = 0;
1419 ssl->keep_current_message = 0;
1420 ssl->transform_in = NULL;
1421
1422#if defined(MBEDTLS_SSL_PROTO_DTLS)
1423 ssl->next_record_offset = 0;
1424 ssl->in_epoch = 0;
1425#endif
1426
1427 /* Keep current datagram if partial == 1 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001428 if (partial == 0) {
Hanno Beckerb0302c42021-08-03 09:39:42 +01001429 ssl->in_left = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +01001430 memset(ssl->in_buf, 0, in_buf_len);
Hanno Beckerb0302c42021-08-03 09:39:42 +01001431 }
1432
Ronald Cronad8c17b2022-06-10 17:18:09 +02001433 ssl->send_alert = 0;
1434
Hanno Beckerb0302c42021-08-03 09:39:42 +01001435 /* Reset outgoing message writing */
1436 ssl->out_msgtype = 0;
1437 ssl->out_msglen = 0;
1438 ssl->out_left = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +01001439 memset(ssl->out_buf, 0, out_buf_len);
1440 memset(ssl->cur_out_ctr, 0, sizeof(ssl->cur_out_ctr));
Hanno Beckerb0302c42021-08-03 09:39:42 +01001441 ssl->transform_out = NULL;
1442
1443#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
Gilles Peskine449bd832023-01-11 14:50:10 +01001444 mbedtls_ssl_dtls_replay_reset(ssl);
Hanno Beckerb0302c42021-08-03 09:39:42 +01001445#endif
1446
XiaokangQian2b01dc32022-01-21 02:53:13 +00001447#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
Gilles Peskine449bd832023-01-11 14:50:10 +01001448 if (ssl->transform) {
1449 mbedtls_ssl_transform_free(ssl->transform);
1450 mbedtls_free(ssl->transform);
Hanno Beckerb0302c42021-08-03 09:39:42 +01001451 ssl->transform = NULL;
1452 }
XiaokangQian2b01dc32022-01-21 02:53:13 +00001453#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
1454
XiaokangQian2b01dc32022-01-21 02:53:13 +00001455#if defined(MBEDTLS_SSL_PROTO_TLS1_3)
Gilles Peskine449bd832023-01-11 14:50:10 +01001456 mbedtls_ssl_transform_free(ssl->transform_application);
1457 mbedtls_free(ssl->transform_application);
XiaokangQian2b01dc32022-01-21 02:53:13 +00001458 ssl->transform_application = NULL;
1459
Gilles Peskine449bd832023-01-11 14:50:10 +01001460 if (ssl->handshake != NULL) {
Jerry Yu3d9b5902022-11-04 14:07:25 +08001461#if defined(MBEDTLS_SSL_EARLY_DATA)
Gilles Peskine449bd832023-01-11 14:50:10 +01001462 mbedtls_ssl_transform_free(ssl->handshake->transform_earlydata);
1463 mbedtls_free(ssl->handshake->transform_earlydata);
XiaokangQian2b01dc32022-01-21 02:53:13 +00001464 ssl->handshake->transform_earlydata = NULL;
Jerry Yu3d9b5902022-11-04 14:07:25 +08001465#endif
XiaokangQian2b01dc32022-01-21 02:53:13 +00001466
Gilles Peskine449bd832023-01-11 14:50:10 +01001467 mbedtls_ssl_transform_free(ssl->handshake->transform_handshake);
1468 mbedtls_free(ssl->handshake->transform_handshake);
XiaokangQian2b01dc32022-01-21 02:53:13 +00001469 ssl->handshake->transform_handshake = NULL;
1470 }
1471
XiaokangQian2b01dc32022-01-21 02:53:13 +00001472#endif /* MBEDTLS_SSL_PROTO_TLS1_3 */
Hanno Beckerb0302c42021-08-03 09:39:42 +01001473}
1474
Gilles Peskine449bd832023-01-11 14:50:10 +01001475int mbedtls_ssl_session_reset_int(mbedtls_ssl_context *ssl, int partial)
Hanno Beckerb0302c42021-08-03 09:39:42 +01001476{
1477 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
1478
1479 ssl->state = MBEDTLS_SSL_HELLO_REQUEST;
1480
Gilles Peskine449bd832023-01-11 14:50:10 +01001481 mbedtls_ssl_session_reset_msg_layer(ssl, partial);
Hanno Beckerb0302c42021-08-03 09:39:42 +01001482
1483 /* Reset renegotiation state */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001484#if defined(MBEDTLS_SSL_RENEGOTIATION)
1485 ssl->renego_status = MBEDTLS_SSL_INITIAL_HANDSHAKE;
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01001486 ssl->renego_records_seen = 0;
Paul Bakker48916f92012-09-16 19:57:18 +00001487
1488 ssl->verify_data_len = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +01001489 memset(ssl->own_verify_data, 0, MBEDTLS_SSL_VERIFY_DATA_MAX_LEN);
1490 memset(ssl->peer_verify_data, 0, MBEDTLS_SSL_VERIFY_DATA_MAX_LEN);
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01001491#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001492 ssl->secure_renegotiation = MBEDTLS_SSL_LEGACY_RENEGOTIATION;
Paul Bakker48916f92012-09-16 19:57:18 +00001493
Hanno Beckerb0302c42021-08-03 09:39:42 +01001494 ssl->session_in = NULL;
Hanno Becker78640902018-08-13 16:35:15 +01001495 ssl->session_out = NULL;
Gilles Peskine449bd832023-01-11 14:50:10 +01001496 if (ssl->session) {
1497 mbedtls_ssl_session_free(ssl->session);
1498 mbedtls_free(ssl->session);
Paul Bakkerc0463502013-02-14 11:19:38 +01001499 ssl->session = NULL;
1500 }
1501
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001502#if defined(MBEDTLS_SSL_ALPN)
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02001503 ssl->alpn_chosen = NULL;
1504#endif
1505
Manuel Pégourié-Gonnarde057d3b2015-05-20 10:59:43 +02001506#if defined(MBEDTLS_SSL_DTLS_HELLO_VERIFY) && defined(MBEDTLS_SSL_SRV_C)
David Horstmann3b2276a2022-10-06 14:49:08 +01001507 int free_cli_id = 1;
Hanno Becker4ccbf062018-08-10 11:20:38 +01001508#if defined(MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE)
Gilles Peskine449bd832023-01-11 14:50:10 +01001509 free_cli_id = (partial == 0);
Hanno Becker4ccbf062018-08-10 11:20:38 +01001510#endif
Gilles Peskine449bd832023-01-11 14:50:10 +01001511 if (free_cli_id) {
1512 mbedtls_free(ssl->cli_id);
Manuel Pégourié-Gonnard3f09b6d2015-09-08 11:58:14 +02001513 ssl->cli_id = NULL;
1514 ssl->cli_id_len = 0;
1515 }
Manuel Pégourié-Gonnard43c02182014-07-22 17:32:01 +02001516#endif
1517
Gilles Peskine449bd832023-01-11 14:50:10 +01001518 if ((ret = ssl_handshake_init(ssl)) != 0) {
1519 return ret;
1520 }
Paul Bakker2770fbd2012-07-03 13:30:23 +00001521
Gilles Peskine449bd832023-01-11 14:50:10 +01001522 return 0;
Paul Bakker7eb013f2011-10-06 12:37:39 +00001523}
1524
Manuel Pégourié-Gonnard779e4292013-08-03 13:50:48 +02001525/*
Manuel Pégourié-Gonnard3f09b6d2015-09-08 11:58:14 +02001526 * Reset an initialized and used SSL context for re-use while retaining
1527 * all application-set variables, function pointers and data.
1528 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001529int mbedtls_ssl_session_reset(mbedtls_ssl_context *ssl)
Manuel Pégourié-Gonnard3f09b6d2015-09-08 11:58:14 +02001530{
Gilles Peskine449bd832023-01-11 14:50:10 +01001531 return mbedtls_ssl_session_reset_int(ssl, 0);
Manuel Pégourié-Gonnard3f09b6d2015-09-08 11:58:14 +02001532}
1533
1534/*
Paul Bakker5121ce52009-01-03 21:22:43 +00001535 * SSL set accessors
1536 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001537void mbedtls_ssl_conf_endpoint(mbedtls_ssl_config *conf, int endpoint)
Paul Bakker5121ce52009-01-03 21:22:43 +00001538{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02001539 conf->endpoint = endpoint;
Paul Bakker5121ce52009-01-03 21:22:43 +00001540}
1541
Gilles Peskine449bd832023-01-11 14:50:10 +01001542void mbedtls_ssl_conf_transport(mbedtls_ssl_config *conf, int transport)
Manuel Pégourié-Gonnard0b1ff292014-02-06 13:04:16 +01001543{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02001544 conf->transport = transport;
Manuel Pégourié-Gonnard0b1ff292014-02-06 13:04:16 +01001545}
1546
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001547#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
Gilles Peskine449bd832023-01-11 14:50:10 +01001548void mbedtls_ssl_conf_dtls_anti_replay(mbedtls_ssl_config *conf, char mode)
Manuel Pégourié-Gonnard27393132014-09-24 14:41:11 +02001549{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02001550 conf->anti_replay = mode;
Manuel Pégourié-Gonnard27393132014-09-24 14:41:11 +02001551}
1552#endif
1553
Gilles Peskine449bd832023-01-11 14:50:10 +01001554void mbedtls_ssl_conf_dtls_badmac_limit(mbedtls_ssl_config *conf, unsigned limit)
Manuel Pégourié-Gonnardb0643d12014-10-14 18:30:36 +02001555{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02001556 conf->badmac_limit = limit;
Manuel Pégourié-Gonnardb0643d12014-10-14 18:30:36 +02001557}
Manuel Pégourié-Gonnardb0643d12014-10-14 18:30:36 +02001558
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001559#if defined(MBEDTLS_SSL_PROTO_DTLS)
Hanno Becker04da1892018-08-14 13:22:10 +01001560
Gilles Peskine449bd832023-01-11 14:50:10 +01001561void mbedtls_ssl_set_datagram_packing(mbedtls_ssl_context *ssl,
1562 unsigned allow_packing)
Hanno Becker04da1892018-08-14 13:22:10 +01001563{
1564 ssl->disable_datagram_packing = !allow_packing;
1565}
1566
Gilles Peskine449bd832023-01-11 14:50:10 +01001567void mbedtls_ssl_conf_handshake_timeout(mbedtls_ssl_config *conf,
1568 uint32_t min, uint32_t max)
Manuel Pégourié-Gonnard905dd242014-10-01 12:03:55 +02001569{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02001570 conf->hs_timeout_min = min;
1571 conf->hs_timeout_max = max;
Manuel Pégourié-Gonnard905dd242014-10-01 12:03:55 +02001572}
1573#endif
1574
Gilles Peskine449bd832023-01-11 14:50:10 +01001575void mbedtls_ssl_conf_authmode(mbedtls_ssl_config *conf, int authmode)
Paul Bakker5121ce52009-01-03 21:22:43 +00001576{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02001577 conf->authmode = authmode;
Paul Bakker5121ce52009-01-03 21:22:43 +00001578}
1579
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001580#if defined(MBEDTLS_X509_CRT_PARSE_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01001581void mbedtls_ssl_conf_verify(mbedtls_ssl_config *conf,
1582 int (*f_vrfy)(void *, mbedtls_x509_crt *, int, uint32_t *),
1583 void *p_vrfy)
Paul Bakkerb63b0af2011-01-13 17:54:59 +00001584{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02001585 conf->f_vrfy = f_vrfy;
1586 conf->p_vrfy = p_vrfy;
Paul Bakkerb63b0af2011-01-13 17:54:59 +00001587}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001588#endif /* MBEDTLS_X509_CRT_PARSE_C */
Paul Bakkerb63b0af2011-01-13 17:54:59 +00001589
Gilles Peskine449bd832023-01-11 14:50:10 +01001590void mbedtls_ssl_conf_rng(mbedtls_ssl_config *conf,
1591 int (*f_rng)(void *, unsigned char *, size_t),
1592 void *p_rng)
Paul Bakker5121ce52009-01-03 21:22:43 +00001593{
Manuel Pégourié-Gonnard750e4d72015-05-07 12:35:38 +01001594 conf->f_rng = f_rng;
1595 conf->p_rng = p_rng;
Paul Bakker5121ce52009-01-03 21:22:43 +00001596}
1597
Gilles Peskine449bd832023-01-11 14:50:10 +01001598void mbedtls_ssl_conf_dbg(mbedtls_ssl_config *conf,
1599 void (*f_dbg)(void *, int, const char *, int, const char *),
1600 void *p_dbg)
Paul Bakker5121ce52009-01-03 21:22:43 +00001601{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02001602 conf->f_dbg = f_dbg;
1603 conf->p_dbg = p_dbg;
Paul Bakker5121ce52009-01-03 21:22:43 +00001604}
1605
Gilles Peskine449bd832023-01-11 14:50:10 +01001606void mbedtls_ssl_set_bio(mbedtls_ssl_context *ssl,
1607 void *p_bio,
1608 mbedtls_ssl_send_t *f_send,
1609 mbedtls_ssl_recv_t *f_recv,
1610 mbedtls_ssl_recv_timeout_t *f_recv_timeout)
Manuel Pégourié-Gonnard8fa6dfd2014-09-17 10:47:43 +02001611{
1612 ssl->p_bio = p_bio;
1613 ssl->f_send = f_send;
1614 ssl->f_recv = f_recv;
1615 ssl->f_recv_timeout = f_recv_timeout;
Manuel Pégourié-Gonnard97fd52c2015-05-06 15:38:52 +01001616}
1617
Manuel Pégourié-Gonnard6e7aaca2018-08-20 10:37:23 +02001618#if defined(MBEDTLS_SSL_PROTO_DTLS)
Gilles Peskine449bd832023-01-11 14:50:10 +01001619void mbedtls_ssl_set_mtu(mbedtls_ssl_context *ssl, uint16_t mtu)
Manuel Pégourié-Gonnard6e7aaca2018-08-20 10:37:23 +02001620{
1621 ssl->mtu = mtu;
1622}
1623#endif
1624
Gilles Peskine449bd832023-01-11 14:50:10 +01001625void mbedtls_ssl_conf_read_timeout(mbedtls_ssl_config *conf, uint32_t timeout)
Manuel Pégourié-Gonnard97fd52c2015-05-06 15:38:52 +01001626{
1627 conf->read_timeout = timeout;
Manuel Pégourié-Gonnard8fa6dfd2014-09-17 10:47:43 +02001628}
1629
Gilles Peskine449bd832023-01-11 14:50:10 +01001630void mbedtls_ssl_set_timer_cb(mbedtls_ssl_context *ssl,
1631 void *p_timer,
1632 mbedtls_ssl_set_timer_t *f_set_timer,
1633 mbedtls_ssl_get_timer_t *f_get_timer)
Manuel Pégourié-Gonnard2e012912015-05-12 20:55:41 +02001634{
1635 ssl->p_timer = p_timer;
1636 ssl->f_set_timer = f_set_timer;
1637 ssl->f_get_timer = f_get_timer;
Manuel Pégourié-Gonnard286a1362015-05-13 16:22:05 +02001638
1639 /* Make sure we start with no timer running */
Gilles Peskine449bd832023-01-11 14:50:10 +01001640 mbedtls_ssl_set_timer(ssl, 0);
Manuel Pégourié-Gonnard2e012912015-05-12 20:55:41 +02001641}
1642
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001643#if defined(MBEDTLS_SSL_SRV_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01001644void mbedtls_ssl_conf_session_cache(mbedtls_ssl_config *conf,
1645 void *p_cache,
1646 mbedtls_ssl_cache_get_t *f_get_cache,
1647 mbedtls_ssl_cache_set_t *f_set_cache)
Paul Bakker5121ce52009-01-03 21:22:43 +00001648{
Manuel Pégourié-Gonnard5cb33082015-05-06 18:06:26 +01001649 conf->p_cache = p_cache;
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02001650 conf->f_get_cache = f_get_cache;
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02001651 conf->f_set_cache = f_set_cache;
Paul Bakker5121ce52009-01-03 21:22:43 +00001652}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001653#endif /* MBEDTLS_SSL_SRV_C */
Paul Bakker5121ce52009-01-03 21:22:43 +00001654
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001655#if defined(MBEDTLS_SSL_CLI_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01001656int mbedtls_ssl_set_session(mbedtls_ssl_context *ssl, const mbedtls_ssl_session *session)
Paul Bakker5121ce52009-01-03 21:22:43 +00001657{
Janos Follath865b3eb2019-12-16 11:46:15 +00001658 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +02001659
Gilles Peskine449bd832023-01-11 14:50:10 +01001660 if (ssl == NULL ||
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +02001661 session == NULL ||
1662 ssl->session_negotiate == NULL ||
Gilles Peskine449bd832023-01-11 14:50:10 +01001663 ssl->conf->endpoint != MBEDTLS_SSL_IS_CLIENT) {
1664 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +02001665 }
1666
Gilles Peskine449bd832023-01-11 14:50:10 +01001667 if (ssl->handshake->resume == 1) {
1668 return MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
1669 }
Hanno Beckere810bbc2021-05-14 16:01:05 +01001670
Jerry Yu21092062022-10-10 21:21:31 +08001671#if defined(MBEDTLS_SSL_PROTO_TLS1_3)
Gilles Peskine449bd832023-01-11 14:50:10 +01001672 if (session->tls_version == MBEDTLS_SSL_VERSION_TLS1_3) {
Jerry Yu21092062022-10-10 21:21:31 +08001673 const mbedtls_ssl_ciphersuite_t *ciphersuite_info =
Gilles Peskine449bd832023-01-11 14:50:10 +01001674 mbedtls_ssl_ciphersuite_from_id(session->ciphersuite);
Jerry Yu21092062022-10-10 21:21:31 +08001675
Gilles Peskine449bd832023-01-11 14:50:10 +01001676 if (mbedtls_ssl_validate_ciphersuite(
Jerry Yu21092062022-10-10 21:21:31 +08001677 ssl, ciphersuite_info, MBEDTLS_SSL_VERSION_TLS1_3,
Gilles Peskine449bd832023-01-11 14:50:10 +01001678 MBEDTLS_SSL_VERSION_TLS1_3) != 0) {
1679 MBEDTLS_SSL_DEBUG_MSG(4, ("%d is not a valid TLS 1.3 ciphersuite.",
1680 session->ciphersuite));
1681 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
Jerry Yu21092062022-10-10 21:21:31 +08001682 }
Jerry Yu40afab62022-10-08 10:42:13 +08001683 }
Jerry Yu21092062022-10-10 21:21:31 +08001684#endif /* MBEDTLS_SSL_PROTO_TLS1_3 */
Jerry Yu40afab62022-10-08 10:42:13 +08001685
Gilles Peskine449bd832023-01-11 14:50:10 +01001686 if ((ret = mbedtls_ssl_session_copy(ssl->session_negotiate,
1687 session)) != 0) {
1688 return ret;
1689 }
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +02001690
Paul Bakker0a597072012-09-25 21:55:46 +00001691 ssl->handshake->resume = 1;
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +02001692
Gilles Peskine449bd832023-01-11 14:50:10 +01001693 return 0;
Paul Bakker5121ce52009-01-03 21:22:43 +00001694}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001695#endif /* MBEDTLS_SSL_CLI_C */
Paul Bakker5121ce52009-01-03 21:22:43 +00001696
Gilles Peskine449bd832023-01-11 14:50:10 +01001697void mbedtls_ssl_conf_ciphersuites(mbedtls_ssl_config *conf,
1698 const int *ciphersuites)
Mateusz Starzyk06b07fb2021-02-18 13:55:21 +01001699{
Hanno Beckerd60b6c62021-04-29 12:04:11 +01001700 conf->ciphersuite_list = ciphersuites;
Paul Bakker5121ce52009-01-03 21:22:43 +00001701}
1702
Ronald Cron6f135e12021-12-08 16:57:54 +01001703#if defined(MBEDTLS_SSL_PROTO_TLS1_3)
Gilles Peskine449bd832023-01-11 14:50:10 +01001704void mbedtls_ssl_conf_tls13_key_exchange_modes(mbedtls_ssl_config *conf,
1705 const int kex_modes)
Hanno Becker71f1ed62021-07-24 06:01:47 +01001706{
Xiaofei Bai746f9482021-11-12 08:53:56 +00001707 conf->tls13_kex_modes = kex_modes & MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_ALL;
Hanno Becker71f1ed62021-07-24 06:01:47 +01001708}
Xiaokang Qian72de95d2022-10-25 02:54:33 +00001709
1710#if defined(MBEDTLS_SSL_EARLY_DATA)
Gilles Peskine449bd832023-01-11 14:50:10 +01001711void mbedtls_ssl_tls13_conf_early_data(mbedtls_ssl_config *conf,
1712 int early_data_enabled)
Xiaokang Qian72de95d2022-10-25 02:54:33 +00001713{
1714 conf->early_data_enabled = early_data_enabled;
1715}
Jerry Yucc4e0072022-11-22 17:22:22 +08001716
1717#if defined(MBEDTLS_SSL_SRV_C)
1718void mbedtls_ssl_tls13_conf_max_early_data_size(
Gilles Peskine449bd832023-01-11 14:50:10 +01001719 mbedtls_ssl_config *conf, uint32_t max_early_data_size)
Jerry Yucc4e0072022-11-22 17:22:22 +08001720{
Jerry Yu39da9852022-12-06 16:58:36 +08001721 conf->max_early_data_size = max_early_data_size;
Jerry Yucc4e0072022-11-22 17:22:22 +08001722}
1723#endif /* MBEDTLS_SSL_SRV_C */
1724
Xiaokang Qian72de95d2022-10-25 02:54:33 +00001725#endif /* MBEDTLS_SSL_EARLY_DATA */
Ronald Cron6f135e12021-12-08 16:57:54 +01001726#endif /* MBEDTLS_SSL_PROTO_TLS1_3 */
Hanno Becker71f1ed62021-07-24 06:01:47 +01001727
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001728#if defined(MBEDTLS_X509_CRT_PARSE_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01001729void mbedtls_ssl_conf_cert_profile(mbedtls_ssl_config *conf,
1730 const mbedtls_x509_crt_profile *profile)
Manuel Pégourié-Gonnard6e3ee3a2015-06-17 10:58:20 +02001731{
1732 conf->cert_profile = profile;
1733}
1734
Gilles Peskine449bd832023-01-11 14:50:10 +01001735static void ssl_key_cert_free(mbedtls_ssl_key_cert *key_cert)
Glenn Strauss36872db2022-01-22 05:06:31 -05001736{
1737 mbedtls_ssl_key_cert *cur = key_cert, *next;
1738
Gilles Peskine449bd832023-01-11 14:50:10 +01001739 while (cur != NULL) {
Glenn Strauss36872db2022-01-22 05:06:31 -05001740 next = cur->next;
Gilles Peskine449bd832023-01-11 14:50:10 +01001741 mbedtls_free(cur);
Glenn Strauss36872db2022-01-22 05:06:31 -05001742 cur = next;
1743 }
1744}
1745
Manuel Pégourié-Gonnard8f618a82015-05-10 21:13:36 +02001746/* Append a new keycert entry to a (possibly empty) list */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02001747MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01001748static int ssl_append_key_cert(mbedtls_ssl_key_cert **head,
1749 mbedtls_x509_crt *cert,
1750 mbedtls_pk_context *key)
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02001751{
niisato8ee24222018-06-25 19:05:48 +09001752 mbedtls_ssl_key_cert *new_cert;
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02001753
Gilles Peskine449bd832023-01-11 14:50:10 +01001754 if (cert == NULL) {
Glenn Strauss36872db2022-01-22 05:06:31 -05001755 /* Free list if cert is null */
Gilles Peskine449bd832023-01-11 14:50:10 +01001756 ssl_key_cert_free(*head);
Glenn Strauss36872db2022-01-22 05:06:31 -05001757 *head = NULL;
Gilles Peskine449bd832023-01-11 14:50:10 +01001758 return 0;
Glenn Strauss36872db2022-01-22 05:06:31 -05001759 }
1760
Gilles Peskine449bd832023-01-11 14:50:10 +01001761 new_cert = mbedtls_calloc(1, sizeof(mbedtls_ssl_key_cert));
1762 if (new_cert == NULL) {
1763 return MBEDTLS_ERR_SSL_ALLOC_FAILED;
1764 }
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02001765
niisato8ee24222018-06-25 19:05:48 +09001766 new_cert->cert = cert;
1767 new_cert->key = key;
1768 new_cert->next = NULL;
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02001769
Glenn Strauss36872db2022-01-22 05:06:31 -05001770 /* Update head if the list was null, else add to the end */
Gilles Peskine449bd832023-01-11 14:50:10 +01001771 if (*head == NULL) {
niisato8ee24222018-06-25 19:05:48 +09001772 *head = new_cert;
Gilles Peskine449bd832023-01-11 14:50:10 +01001773 } else {
Manuel Pégourié-Gonnard8f618a82015-05-10 21:13:36 +02001774 mbedtls_ssl_key_cert *cur = *head;
Gilles Peskine449bd832023-01-11 14:50:10 +01001775 while (cur->next != NULL) {
Manuel Pégourié-Gonnard8f618a82015-05-10 21:13:36 +02001776 cur = cur->next;
Gilles Peskine449bd832023-01-11 14:50:10 +01001777 }
niisato8ee24222018-06-25 19:05:48 +09001778 cur->next = new_cert;
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02001779 }
1780
Gilles Peskine449bd832023-01-11 14:50:10 +01001781 return 0;
Manuel Pégourié-Gonnard8f618a82015-05-10 21:13:36 +02001782}
1783
Gilles Peskine449bd832023-01-11 14:50:10 +01001784int mbedtls_ssl_conf_own_cert(mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnard8f618a82015-05-10 21:13:36 +02001785 mbedtls_x509_crt *own_cert,
Gilles Peskine449bd832023-01-11 14:50:10 +01001786 mbedtls_pk_context *pk_key)
Manuel Pégourié-Gonnard8f618a82015-05-10 21:13:36 +02001787{
Gilles Peskine449bd832023-01-11 14:50:10 +01001788 return ssl_append_key_cert(&conf->key_cert, own_cert, pk_key);
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02001789}
1790
Gilles Peskine449bd832023-01-11 14:50:10 +01001791void mbedtls_ssl_conf_ca_chain(mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnardbc2b7712015-05-06 11:14:19 +01001792 mbedtls_x509_crt *ca_chain,
Gilles Peskine449bd832023-01-11 14:50:10 +01001793 mbedtls_x509_crl *ca_crl)
Paul Bakker5121ce52009-01-03 21:22:43 +00001794{
Manuel Pégourié-Gonnardbc2b7712015-05-06 11:14:19 +01001795 conf->ca_chain = ca_chain;
1796 conf->ca_crl = ca_crl;
Hanno Becker5adaad92019-03-27 16:54:37 +00001797
1798#if defined(MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK)
1799 /* mbedtls_ssl_conf_ca_chain() and mbedtls_ssl_conf_ca_cb()
1800 * cannot be used together. */
1801 conf->f_ca_cb = NULL;
1802 conf->p_ca_cb = NULL;
1803#endif /* MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK */
Paul Bakker5121ce52009-01-03 21:22:43 +00001804}
Hanno Becker5adaad92019-03-27 16:54:37 +00001805
1806#if defined(MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK)
Gilles Peskine449bd832023-01-11 14:50:10 +01001807void mbedtls_ssl_conf_ca_cb(mbedtls_ssl_config *conf,
1808 mbedtls_x509_crt_ca_cb_t f_ca_cb,
1809 void *p_ca_cb)
Hanno Becker5adaad92019-03-27 16:54:37 +00001810{
1811 conf->f_ca_cb = f_ca_cb;
1812 conf->p_ca_cb = p_ca_cb;
1813
1814 /* mbedtls_ssl_conf_ca_chain() and mbedtls_ssl_conf_ca_cb()
1815 * cannot be used together. */
1816 conf->ca_chain = NULL;
1817 conf->ca_crl = NULL;
1818}
1819#endif /* MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001820#endif /* MBEDTLS_X509_CRT_PARSE_C */
Paul Bakkereb2c6582012-09-27 19:15:01 +00001821
Manuel Pégourié-Gonnard1af6c852015-05-10 23:10:37 +02001822#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
Gilles Peskine449bd832023-01-11 14:50:10 +01001823const unsigned char *mbedtls_ssl_get_hs_sni(mbedtls_ssl_context *ssl,
1824 size_t *name_len)
Glenn Strauss69894072022-01-24 12:58:00 -05001825{
1826 *name_len = ssl->handshake->sni_name_len;
Gilles Peskine449bd832023-01-11 14:50:10 +01001827 return ssl->handshake->sni_name;
Glenn Strauss69894072022-01-24 12:58:00 -05001828}
1829
Gilles Peskine449bd832023-01-11 14:50:10 +01001830int mbedtls_ssl_set_hs_own_cert(mbedtls_ssl_context *ssl,
1831 mbedtls_x509_crt *own_cert,
1832 mbedtls_pk_context *pk_key)
Manuel Pégourié-Gonnard1af6c852015-05-10 23:10:37 +02001833{
Gilles Peskine449bd832023-01-11 14:50:10 +01001834 return ssl_append_key_cert(&ssl->handshake->sni_key_cert,
1835 own_cert, pk_key);
Manuel Pégourié-Gonnard1af6c852015-05-10 23:10:37 +02001836}
Manuel Pégourié-Gonnard22bfa4b2015-05-11 08:46:37 +02001837
Gilles Peskine449bd832023-01-11 14:50:10 +01001838void mbedtls_ssl_set_hs_ca_chain(mbedtls_ssl_context *ssl,
1839 mbedtls_x509_crt *ca_chain,
1840 mbedtls_x509_crl *ca_crl)
Manuel Pégourié-Gonnard22bfa4b2015-05-11 08:46:37 +02001841{
1842 ssl->handshake->sni_ca_chain = ca_chain;
1843 ssl->handshake->sni_ca_crl = ca_crl;
1844}
Manuel Pégourié-Gonnardcdc26ae2015-06-19 12:16:31 +02001845
Glenn Strauss999ef702022-03-11 01:37:23 -05001846#if defined(MBEDTLS_KEY_EXCHANGE_CERT_REQ_ALLOWED_ENABLED)
Gilles Peskine449bd832023-01-11 14:50:10 +01001847void mbedtls_ssl_set_hs_dn_hints(mbedtls_ssl_context *ssl,
1848 const mbedtls_x509_crt *crt)
Glenn Strauss999ef702022-03-11 01:37:23 -05001849{
1850 ssl->handshake->dn_hints = crt;
1851}
1852#endif /* MBEDTLS_KEY_EXCHANGE_CERT_REQ_ALLOWED_ENABLED */
1853
Gilles Peskine449bd832023-01-11 14:50:10 +01001854void mbedtls_ssl_set_hs_authmode(mbedtls_ssl_context *ssl,
1855 int authmode)
Manuel Pégourié-Gonnardcdc26ae2015-06-19 12:16:31 +02001856{
1857 ssl->handshake->sni_authmode = authmode;
1858}
Manuel Pégourié-Gonnard1af6c852015-05-10 23:10:37 +02001859#endif /* MBEDTLS_SSL_SERVER_NAME_INDICATION */
1860
Hanno Becker8927c832019-04-03 12:52:50 +01001861#if defined(MBEDTLS_X509_CRT_PARSE_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01001862void mbedtls_ssl_set_verify(mbedtls_ssl_context *ssl,
1863 int (*f_vrfy)(void *, mbedtls_x509_crt *, int, uint32_t *),
1864 void *p_vrfy)
Hanno Becker8927c832019-04-03 12:52:50 +01001865{
1866 ssl->f_vrfy = f_vrfy;
1867 ssl->p_vrfy = p_vrfy;
1868}
1869#endif
1870
Manuel Pégourié-Gonnardeef142d2015-09-16 10:05:04 +02001871#if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
Manuel Pégourié-Gonnard7002f4a2015-09-15 12:43:43 +02001872
Valerio Setti016f6822022-12-09 14:17:50 +01001873#if defined(MBEDTLS_USE_PSA_CRYPTO)
1874static psa_status_t mbedtls_ssl_set_hs_ecjpake_password_common(
Gilles Peskine449bd832023-01-11 14:50:10 +01001875 mbedtls_ssl_context *ssl,
1876 mbedtls_svc_key_id_t pwd)
Valerio Setti016f6822022-12-09 14:17:50 +01001877{
1878 psa_status_t status;
1879 psa_pake_role_t psa_role;
1880 psa_pake_cipher_suite_t cipher_suite = psa_pake_cipher_suite_init();
1881
Gilles Peskine449bd832023-01-11 14:50:10 +01001882 psa_pake_cs_set_algorithm(&cipher_suite, PSA_ALG_JPAKE);
1883 psa_pake_cs_set_primitive(&cipher_suite,
1884 PSA_PAKE_PRIMITIVE(PSA_PAKE_PRIMITIVE_TYPE_ECC,
1885 PSA_ECC_FAMILY_SECP_R1,
1886 256));
1887 psa_pake_cs_set_hash(&cipher_suite, PSA_ALG_SHA_256);
Valerio Setti016f6822022-12-09 14:17:50 +01001888
Gilles Peskine449bd832023-01-11 14:50:10 +01001889 status = psa_pake_setup(&ssl->handshake->psa_pake_ctx, &cipher_suite);
1890 if (status != PSA_SUCCESS) {
Valerio Setti016f6822022-12-09 14:17:50 +01001891 return status;
Gilles Peskine449bd832023-01-11 14:50:10 +01001892 }
Manuel Pégourié-Gonnard7002f4a2015-09-15 12:43:43 +02001893
Gilles Peskine449bd832023-01-11 14:50:10 +01001894 if (ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER) {
Neil Armstrongca7d5062022-05-31 14:43:23 +02001895 psa_role = PSA_PAKE_ROLE_SERVER;
Gilles Peskine449bd832023-01-11 14:50:10 +01001896 } else {
Neil Armstrongca7d5062022-05-31 14:43:23 +02001897 psa_role = PSA_PAKE_ROLE_CLIENT;
Gilles Peskine449bd832023-01-11 14:50:10 +01001898 }
Neil Armstrongca7d5062022-05-31 14:43:23 +02001899
Gilles Peskine449bd832023-01-11 14:50:10 +01001900 status = psa_pake_set_role(&ssl->handshake->psa_pake_ctx, psa_role);
1901 if (status != PSA_SUCCESS) {
Valerio Setti016f6822022-12-09 14:17:50 +01001902 return status;
Gilles Peskine449bd832023-01-11 14:50:10 +01001903 }
Valerio Setti016f6822022-12-09 14:17:50 +01001904
Gilles Peskine449bd832023-01-11 14:50:10 +01001905 status = psa_pake_set_password_key(&ssl->handshake->psa_pake_ctx, pwd);
1906 if (status != PSA_SUCCESS) {
Valerio Setti016f6822022-12-09 14:17:50 +01001907 return status;
Gilles Peskine449bd832023-01-11 14:50:10 +01001908 }
Valerio Setti016f6822022-12-09 14:17:50 +01001909
1910 ssl->handshake->psa_pake_ctx_is_ok = 1;
1911
Gilles Peskine449bd832023-01-11 14:50:10 +01001912 return PSA_SUCCESS;
Valerio Setti016f6822022-12-09 14:17:50 +01001913}
1914
Gilles Peskine449bd832023-01-11 14:50:10 +01001915int mbedtls_ssl_set_hs_ecjpake_password(mbedtls_ssl_context *ssl,
1916 const unsigned char *pw,
1917 size_t pw_len)
Valerio Setti016f6822022-12-09 14:17:50 +01001918{
1919 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
1920 psa_status_t status;
1921
Gilles Peskine449bd832023-01-11 14:50:10 +01001922 if (ssl->handshake == NULL || ssl->conf == NULL) {
1923 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
Neil Armstrongca7d5062022-05-31 14:43:23 +02001924 }
1925
Gilles Peskine449bd832023-01-11 14:50:10 +01001926 /* Empty password is not valid */
1927 if ((pw == NULL) || (pw_len == 0)) {
1928 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
1929 }
1930
1931 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_DERIVE);
1932 psa_set_key_algorithm(&attributes, PSA_ALG_JPAKE);
1933 psa_set_key_type(&attributes, PSA_KEY_TYPE_PASSWORD);
1934
1935 status = psa_import_key(&attributes, pw, pw_len,
1936 &ssl->handshake->psa_pake_password);
1937 if (status != PSA_SUCCESS) {
1938 return MBEDTLS_ERR_SSL_HW_ACCEL_FAILED;
1939 }
1940
1941 status = mbedtls_ssl_set_hs_ecjpake_password_common(ssl,
1942 ssl->handshake->psa_pake_password);
1943 if (status != PSA_SUCCESS) {
1944 psa_destroy_key(ssl->handshake->psa_pake_password);
1945 psa_pake_abort(&ssl->handshake->psa_pake_ctx);
1946 return MBEDTLS_ERR_SSL_HW_ACCEL_FAILED;
1947 }
1948
1949 return 0;
Valerio Setti02c25b52022-11-15 14:08:42 +01001950}
Valerio Settia9a97dc2022-11-28 18:26:16 +01001951
Gilles Peskine449bd832023-01-11 14:50:10 +01001952int mbedtls_ssl_set_hs_ecjpake_password_opaque(mbedtls_ssl_context *ssl,
1953 mbedtls_svc_key_id_t pwd)
Valerio Settia9a97dc2022-11-28 18:26:16 +01001954{
Valerio Settia9a97dc2022-11-28 18:26:16 +01001955 psa_status_t status;
1956
Gilles Peskine449bd832023-01-11 14:50:10 +01001957 if (ssl->handshake == NULL || ssl->conf == NULL) {
1958 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
Neil Armstrongca7d5062022-05-31 14:43:23 +02001959 }
1960
Gilles Peskine449bd832023-01-11 14:50:10 +01001961 if (mbedtls_svc_key_id_is_null(pwd)) {
1962 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
1963 }
1964
1965 status = mbedtls_ssl_set_hs_ecjpake_password_common(ssl, pwd);
1966 if (status != PSA_SUCCESS) {
1967 psa_pake_abort(&ssl->handshake->psa_pake_ctx);
1968 return MBEDTLS_ERR_SSL_HW_ACCEL_FAILED;
1969 }
1970
1971 return 0;
Valerio Setti02c25b52022-11-15 14:08:42 +01001972}
1973#else /* MBEDTLS_USE_PSA_CRYPTO */
Gilles Peskine449bd832023-01-11 14:50:10 +01001974int mbedtls_ssl_set_hs_ecjpake_password(mbedtls_ssl_context *ssl,
1975 const unsigned char *pw,
1976 size_t pw_len)
Manuel Pégourié-Gonnard7002f4a2015-09-15 12:43:43 +02001977{
1978 mbedtls_ecjpake_role role;
1979
Gilles Peskine449bd832023-01-11 14:50:10 +01001980 if (ssl->handshake == NULL || ssl->conf == NULL) {
1981 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
1982 }
Manuel Pégourié-Gonnard7002f4a2015-09-15 12:43:43 +02001983
Valerio Settic689ed82022-12-07 14:40:38 +01001984 /* Empty password is not valid */
Gilles Peskine449bd832023-01-11 14:50:10 +01001985 if ((pw == NULL) || (pw_len == 0)) {
1986 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
1987 }
Valerio Settic689ed82022-12-07 14:40:38 +01001988
Gilles Peskine449bd832023-01-11 14:50:10 +01001989 if (ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER) {
Manuel Pégourié-Gonnard7002f4a2015-09-15 12:43:43 +02001990 role = MBEDTLS_ECJPAKE_SERVER;
Gilles Peskine449bd832023-01-11 14:50:10 +01001991 } else {
Manuel Pégourié-Gonnard7002f4a2015-09-15 12:43:43 +02001992 role = MBEDTLS_ECJPAKE_CLIENT;
Gilles Peskine449bd832023-01-11 14:50:10 +01001993 }
Manuel Pégourié-Gonnard7002f4a2015-09-15 12:43:43 +02001994
Gilles Peskine449bd832023-01-11 14:50:10 +01001995 return mbedtls_ecjpake_setup(&ssl->handshake->ecjpake_ctx,
1996 role,
1997 MBEDTLS_MD_SHA256,
1998 MBEDTLS_ECP_DP_SECP256R1,
1999 pw, pw_len);
Manuel Pégourié-Gonnard7002f4a2015-09-15 12:43:43 +02002000}
Valerio Setti02c25b52022-11-15 14:08:42 +01002001#endif /* MBEDTLS_USE_PSA_CRYPTO */
Manuel Pégourié-Gonnardeef142d2015-09-16 10:05:04 +02002002#endif /* MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED */
Manuel Pégourié-Gonnard7002f4a2015-09-15 12:43:43 +02002003
Ronald Cron73fe8df2022-10-05 14:31:43 +02002004#if defined(MBEDTLS_SSL_HANDSHAKE_WITH_PSK_ENABLED)
Gilles Peskine449bd832023-01-11 14:50:10 +01002005int mbedtls_ssl_conf_has_static_psk(mbedtls_ssl_config const *conf)
Hanno Becker2ed3dce2021-04-19 21:59:14 +01002006{
Gilles Peskine449bd832023-01-11 14:50:10 +01002007 if (conf->psk_identity == NULL ||
2008 conf->psk_identity_len == 0) {
2009 return 0;
Ronald Crond29e13e2022-10-19 10:33:48 +02002010 }
2011
Hanno Becker2ed3dce2021-04-19 21:59:14 +01002012#if defined(MBEDTLS_USE_PSA_CRYPTO)
Gilles Peskine449bd832023-01-11 14:50:10 +01002013 if (!mbedtls_svc_key_id_is_null(conf->psk_opaque)) {
2014 return 1;
2015 }
Hanno Becker2ed3dce2021-04-19 21:59:14 +01002016#endif /* MBEDTLS_USE_PSA_CRYPTO */
Ronald Crond29e13e2022-10-19 10:33:48 +02002017
Gilles Peskine449bd832023-01-11 14:50:10 +01002018 if (conf->psk != NULL && conf->psk_len != 0) {
2019 return 1;
2020 }
Hanno Becker2ed3dce2021-04-19 21:59:14 +01002021
Gilles Peskine449bd832023-01-11 14:50:10 +01002022 return 0;
Hanno Becker2ed3dce2021-04-19 21:59:14 +01002023}
2024
Gilles Peskine449bd832023-01-11 14:50:10 +01002025static void ssl_conf_remove_psk(mbedtls_ssl_config *conf)
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01002026{
2027 /* Remove reference to existing PSK, if any. */
2028#if defined(MBEDTLS_USE_PSA_CRYPTO)
Gilles Peskine449bd832023-01-11 14:50:10 +01002029 if (!mbedtls_svc_key_id_is_null(conf->psk_opaque)) {
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01002030 /* The maintenance of the PSK key slot is the
2031 * user's responsibility. */
Ronald Croncf56a0a2020-08-04 09:51:30 +02002032 conf->psk_opaque = MBEDTLS_SVC_KEY_ID_INIT;
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01002033 }
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01002034#endif /* MBEDTLS_USE_PSA_CRYPTO */
Gilles Peskine449bd832023-01-11 14:50:10 +01002035 if (conf->psk != NULL) {
2036 mbedtls_platform_zeroize(conf->psk, conf->psk_len);
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01002037
Gilles Peskine449bd832023-01-11 14:50:10 +01002038 mbedtls_free(conf->psk);
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01002039 conf->psk = NULL;
2040 conf->psk_len = 0;
2041 }
2042
2043 /* Remove reference to PSK identity, if any. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002044 if (conf->psk_identity != NULL) {
2045 mbedtls_free(conf->psk_identity);
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01002046 conf->psk_identity = NULL;
2047 conf->psk_identity_len = 0;
2048 }
2049}
2050
Hanno Becker7390c712018-11-15 13:33:04 +00002051/* This function assumes that PSK identity in the SSL config is unset.
2052 * It checks that the provided identity is well-formed and attempts
2053 * to make a copy of it in the SSL config.
2054 * On failure, the PSK identity in the config remains unset. */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02002055MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01002056static int ssl_conf_set_psk_identity(mbedtls_ssl_config *conf,
2057 unsigned char const *psk_identity,
2058 size_t psk_identity_len)
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02002059{
Manuel Pégourié-Gonnardc6b5d832015-08-27 16:37:35 +02002060 /* Identity len will be encoded on two bytes */
Gilles Peskine449bd832023-01-11 14:50:10 +01002061 if (psk_identity == NULL ||
Ronald Cron2a87e9b2022-10-19 10:55:26 +02002062 psk_identity_len == 0 ||
Gilles Peskine449bd832023-01-11 14:50:10 +01002063 (psk_identity_len >> 16) != 0 ||
2064 psk_identity_len > MBEDTLS_SSL_OUT_CONTENT_LEN) {
2065 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
Manuel Pégourié-Gonnardc6b5d832015-08-27 16:37:35 +02002066 }
2067
Gilles Peskine449bd832023-01-11 14:50:10 +01002068 conf->psk_identity = mbedtls_calloc(1, psk_identity_len);
2069 if (conf->psk_identity == NULL) {
2070 return MBEDTLS_ERR_SSL_ALLOC_FAILED;
2071 }
Paul Bakker6db455e2013-09-18 17:29:31 +02002072
Manuel Pégourié-Gonnard120fdbd2015-05-07 17:07:50 +01002073 conf->psk_identity_len = psk_identity_len;
Gilles Peskine449bd832023-01-11 14:50:10 +01002074 memcpy(conf->psk_identity, psk_identity, conf->psk_identity_len);
Paul Bakker5ad403f2013-09-18 21:21:30 +02002075
Gilles Peskine449bd832023-01-11 14:50:10 +01002076 return 0;
Paul Bakker6db455e2013-09-18 17:29:31 +02002077}
2078
Gilles Peskine449bd832023-01-11 14:50:10 +01002079int mbedtls_ssl_conf_psk(mbedtls_ssl_config *conf,
2080 const unsigned char *psk, size_t psk_len,
2081 const unsigned char *psk_identity, size_t psk_identity_len)
Hanno Becker7390c712018-11-15 13:33:04 +00002082{
Janos Follath865b3eb2019-12-16 11:46:15 +00002083 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Hanno Becker2ed3dce2021-04-19 21:59:14 +01002084
2085 /* We currently only support one PSK, raw or opaque. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002086 if (mbedtls_ssl_conf_has_static_psk(conf)) {
2087 return MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
2088 }
Hanno Becker7390c712018-11-15 13:33:04 +00002089
2090 /* Check and set raw PSK */
Gilles Peskine449bd832023-01-11 14:50:10 +01002091 if (psk == NULL) {
2092 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
2093 }
2094 if (psk_len == 0) {
2095 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
2096 }
2097 if (psk_len > MBEDTLS_PSK_MAX_LEN) {
2098 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
2099 }
Piotr Nowicki9926eaf2019-11-20 14:54:36 +01002100
Gilles Peskine449bd832023-01-11 14:50:10 +01002101 if ((conf->psk = mbedtls_calloc(1, psk_len)) == NULL) {
2102 return MBEDTLS_ERR_SSL_ALLOC_FAILED;
2103 }
Hanno Becker7390c712018-11-15 13:33:04 +00002104 conf->psk_len = psk_len;
Gilles Peskine449bd832023-01-11 14:50:10 +01002105 memcpy(conf->psk, psk, conf->psk_len);
Hanno Becker7390c712018-11-15 13:33:04 +00002106
2107 /* Check and set PSK Identity */
Gilles Peskine449bd832023-01-11 14:50:10 +01002108 ret = ssl_conf_set_psk_identity(conf, psk_identity, psk_identity_len);
2109 if (ret != 0) {
2110 ssl_conf_remove_psk(conf);
2111 }
Hanno Becker7390c712018-11-15 13:33:04 +00002112
Gilles Peskine449bd832023-01-11 14:50:10 +01002113 return ret;
Hanno Becker7390c712018-11-15 13:33:04 +00002114}
2115
Gilles Peskine449bd832023-01-11 14:50:10 +01002116static void ssl_remove_psk(mbedtls_ssl_context *ssl)
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01002117{
2118#if defined(MBEDTLS_USE_PSA_CRYPTO)
Gilles Peskine449bd832023-01-11 14:50:10 +01002119 if (!mbedtls_svc_key_id_is_null(ssl->handshake->psk_opaque)) {
Neil Armstrong501c9322022-05-03 09:35:09 +02002120 /* The maintenance of the external PSK key slot is the
2121 * user's responsibility. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002122 if (ssl->handshake->psk_opaque_is_internal) {
2123 psa_destroy_key(ssl->handshake->psk_opaque);
Neil Armstrong501c9322022-05-03 09:35:09 +02002124 ssl->handshake->psk_opaque_is_internal = 0;
2125 }
Ronald Croncf56a0a2020-08-04 09:51:30 +02002126 ssl->handshake->psk_opaque = MBEDTLS_SVC_KEY_ID_INIT;
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01002127 }
Neil Armstronge952a302022-05-03 10:22:14 +02002128#else
Gilles Peskine449bd832023-01-11 14:50:10 +01002129 if (ssl->handshake->psk != NULL) {
2130 mbedtls_platform_zeroize(ssl->handshake->psk,
2131 ssl->handshake->psk_len);
2132 mbedtls_free(ssl->handshake->psk);
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01002133 ssl->handshake->psk_len = 0;
2134 }
Neil Armstronge952a302022-05-03 10:22:14 +02002135#endif /* MBEDTLS_USE_PSA_CRYPTO */
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01002136}
2137
Gilles Peskine449bd832023-01-11 14:50:10 +01002138int mbedtls_ssl_set_hs_psk(mbedtls_ssl_context *ssl,
2139 const unsigned char *psk, size_t psk_len)
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01002140{
Neil Armstrong501c9322022-05-03 09:35:09 +02002141#if defined(MBEDTLS_USE_PSA_CRYPTO)
2142 psa_key_attributes_t key_attributes = psa_key_attributes_init();
Jerry Yu5d01c052022-08-17 10:18:10 +08002143 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Jerry Yu24b8c812022-08-20 19:06:56 +08002144 psa_algorithm_t alg = PSA_ALG_NONE;
Jerry Yu5d01c052022-08-17 10:18:10 +08002145 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Neil Armstrong501c9322022-05-03 09:35:09 +02002146#endif /* MBEDTLS_USE_PSA_CRYPTO */
2147
Gilles Peskine449bd832023-01-11 14:50:10 +01002148 if (psk == NULL || ssl->handshake == NULL) {
2149 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
2150 }
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01002151
Gilles Peskine449bd832023-01-11 14:50:10 +01002152 if (psk_len > MBEDTLS_PSK_MAX_LEN) {
2153 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
2154 }
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01002155
Gilles Peskine449bd832023-01-11 14:50:10 +01002156 ssl_remove_psk(ssl);
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01002157
Neil Armstrong501c9322022-05-03 09:35:09 +02002158#if defined(MBEDTLS_USE_PSA_CRYPTO)
Jerry Yuccc68a42022-07-26 16:39:20 +08002159#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
Gilles Peskine449bd832023-01-11 14:50:10 +01002160 if (ssl->tls_version == MBEDTLS_SSL_VERSION_TLS1_2) {
2161 if (ssl->handshake->ciphersuite_info->mac == MBEDTLS_MD_SHA384) {
2162 alg = PSA_ALG_TLS12_PSK_TO_MS(PSA_ALG_SHA_384);
2163 } else {
2164 alg = PSA_ALG_TLS12_PSK_TO_MS(PSA_ALG_SHA_256);
2165 }
2166 psa_set_key_usage_flags(&key_attributes, PSA_KEY_USAGE_DERIVE);
Jerry Yuccc68a42022-07-26 16:39:20 +08002167 }
2168#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Neil Armstrong501c9322022-05-03 09:35:09 +02002169
Jerry Yu568ec252022-07-22 21:27:34 +08002170#if defined(MBEDTLS_SSL_PROTO_TLS1_3)
Gilles Peskine449bd832023-01-11 14:50:10 +01002171 if (ssl->tls_version == MBEDTLS_SSL_VERSION_TLS1_3) {
2172 alg = PSA_ALG_HKDF_EXTRACT(PSA_ALG_ANY_HASH);
2173 psa_set_key_usage_flags(&key_attributes,
2174 PSA_KEY_USAGE_DERIVE | PSA_KEY_USAGE_EXPORT);
Jerry Yuccc68a42022-07-26 16:39:20 +08002175 }
2176#endif /* MBEDTLS_SSL_PROTO_TLS1_3 */
2177
Gilles Peskine449bd832023-01-11 14:50:10 +01002178 psa_set_key_algorithm(&key_attributes, alg);
2179 psa_set_key_type(&key_attributes, PSA_KEY_TYPE_DERIVE);
Neil Armstrong501c9322022-05-03 09:35:09 +02002180
Gilles Peskine449bd832023-01-11 14:50:10 +01002181 status = psa_import_key(&key_attributes, psk, psk_len, &key);
2182 if (status != PSA_SUCCESS) {
2183 return MBEDTLS_ERR_SSL_HW_ACCEL_FAILED;
2184 }
Neil Armstrong501c9322022-05-03 09:35:09 +02002185
2186 /* Allow calling psa_destroy_key() on psk remove */
2187 ssl->handshake->psk_opaque_is_internal = 1;
Gilles Peskine449bd832023-01-11 14:50:10 +01002188 return mbedtls_ssl_set_hs_psk_opaque(ssl, key);
Neil Armstrong501c9322022-05-03 09:35:09 +02002189#else
Gilles Peskine449bd832023-01-11 14:50:10 +01002190 if ((ssl->handshake->psk = mbedtls_calloc(1, psk_len)) == NULL) {
2191 return MBEDTLS_ERR_SSL_ALLOC_FAILED;
2192 }
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01002193
2194 ssl->handshake->psk_len = psk_len;
Gilles Peskine449bd832023-01-11 14:50:10 +01002195 memcpy(ssl->handshake->psk, psk, ssl->handshake->psk_len);
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01002196
Gilles Peskine449bd832023-01-11 14:50:10 +01002197 return 0;
Neil Armstrong501c9322022-05-03 09:35:09 +02002198#endif /* MBEDTLS_USE_PSA_CRYPTO */
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01002199}
2200
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01002201#if defined(MBEDTLS_USE_PSA_CRYPTO)
Gilles Peskine449bd832023-01-11 14:50:10 +01002202int mbedtls_ssl_conf_psk_opaque(mbedtls_ssl_config *conf,
2203 mbedtls_svc_key_id_t psk,
2204 const unsigned char *psk_identity,
2205 size_t psk_identity_len)
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01002206{
Janos Follath865b3eb2019-12-16 11:46:15 +00002207 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Hanno Becker2ed3dce2021-04-19 21:59:14 +01002208
2209 /* We currently only support one PSK, raw or opaque. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002210 if (mbedtls_ssl_conf_has_static_psk(conf)) {
2211 return MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
2212 }
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01002213
Hanno Becker7390c712018-11-15 13:33:04 +00002214 /* Check and set opaque PSK */
Gilles Peskine449bd832023-01-11 14:50:10 +01002215 if (mbedtls_svc_key_id_is_null(psk)) {
2216 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
2217 }
Ronald Croncf56a0a2020-08-04 09:51:30 +02002218 conf->psk_opaque = psk;
Hanno Becker7390c712018-11-15 13:33:04 +00002219
2220 /* Check and set PSK Identity */
Gilles Peskine449bd832023-01-11 14:50:10 +01002221 ret = ssl_conf_set_psk_identity(conf, psk_identity,
2222 psk_identity_len);
2223 if (ret != 0) {
2224 ssl_conf_remove_psk(conf);
2225 }
Hanno Becker7390c712018-11-15 13:33:04 +00002226
Gilles Peskine449bd832023-01-11 14:50:10 +01002227 return ret;
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01002228}
2229
Gilles Peskine449bd832023-01-11 14:50:10 +01002230int mbedtls_ssl_set_hs_psk_opaque(mbedtls_ssl_context *ssl,
2231 mbedtls_svc_key_id_t psk)
Przemyslaw Stekiel6928a512022-02-03 13:50:35 +01002232{
Gilles Peskine449bd832023-01-11 14:50:10 +01002233 if ((mbedtls_svc_key_id_is_null(psk)) ||
2234 (ssl->handshake == NULL)) {
2235 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
2236 }
Przemyslaw Stekiel6928a512022-02-03 13:50:35 +01002237
Gilles Peskine449bd832023-01-11 14:50:10 +01002238 ssl_remove_psk(ssl);
Przemyslaw Stekiel6928a512022-02-03 13:50:35 +01002239 ssl->handshake->psk_opaque = psk;
Gilles Peskine449bd832023-01-11 14:50:10 +01002240 return 0;
Przemyslaw Stekiel6928a512022-02-03 13:50:35 +01002241}
2242#endif /* MBEDTLS_USE_PSA_CRYPTO */
2243
Jerry Yu8897c072022-08-12 13:56:53 +08002244#if defined(MBEDTLS_SSL_SRV_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01002245void mbedtls_ssl_conf_psk_cb(mbedtls_ssl_config *conf,
2246 int (*f_psk)(void *, mbedtls_ssl_context *, const unsigned char *,
2247 size_t),
2248 void *p_psk)
Przemyslaw Stekiel6928a512022-02-03 13:50:35 +01002249{
2250 conf->f_psk = f_psk;
2251 conf->p_psk = p_psk;
2252}
Jerry Yu8897c072022-08-12 13:56:53 +08002253#endif /* MBEDTLS_SSL_SRV_C */
2254
Ronald Cron73fe8df2022-10-05 14:31:43 +02002255#endif /* MBEDTLS_SSL_HANDSHAKE_WITH_PSK_ENABLED */
Przemyslaw Stekiel6928a512022-02-03 13:50:35 +01002256
2257#if defined(MBEDTLS_USE_PSA_CRYPTO)
Gilles Peskine301711e2022-04-26 16:57:05 +02002258static mbedtls_ssl_mode_t mbedtls_ssl_get_base_mode(
Gilles Peskine449bd832023-01-11 14:50:10 +01002259 psa_algorithm_t alg)
Neil Armstrong8a0f3e82022-03-30 10:57:37 +02002260{
Neil Armstrong8a0f3e82022-03-30 10:57:37 +02002261#if defined(MBEDTLS_SSL_SOME_SUITES_USE_MAC)
Gilles Peskine449bd832023-01-11 14:50:10 +01002262 if (alg == PSA_ALG_CBC_NO_PADDING) {
2263 return MBEDTLS_SSL_MODE_CBC;
2264 }
Neil Armstrong8a0f3e82022-03-30 10:57:37 +02002265#endif /* MBEDTLS_SSL_SOME_SUITES_USE_MAC */
Gilles Peskine449bd832023-01-11 14:50:10 +01002266 if (PSA_ALG_IS_AEAD(alg)) {
2267 return MBEDTLS_SSL_MODE_AEAD;
2268 }
2269 return MBEDTLS_SSL_MODE_STREAM;
Gilles Peskine301711e2022-04-26 16:57:05 +02002270}
2271
2272#else /* MBEDTLS_USE_PSA_CRYPTO */
2273
2274static mbedtls_ssl_mode_t mbedtls_ssl_get_base_mode(
Gilles Peskine449bd832023-01-11 14:50:10 +01002275 mbedtls_cipher_mode_t mode)
Gilles Peskine301711e2022-04-26 16:57:05 +02002276{
2277#if defined(MBEDTLS_SSL_SOME_SUITES_USE_MAC)
Gilles Peskine449bd832023-01-11 14:50:10 +01002278 if (mode == MBEDTLS_MODE_CBC) {
2279 return MBEDTLS_SSL_MODE_CBC;
2280 }
Gilles Peskine301711e2022-04-26 16:57:05 +02002281#endif /* MBEDTLS_SSL_SOME_SUITES_USE_MAC */
2282
Neil Armstrong8a0f3e82022-03-30 10:57:37 +02002283#if defined(MBEDTLS_GCM_C) || \
2284 defined(MBEDTLS_CCM_C) || \
2285 defined(MBEDTLS_CHACHAPOLY_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01002286 if (mode == MBEDTLS_MODE_GCM ||
Neil Armstrong8a0f3e82022-03-30 10:57:37 +02002287 mode == MBEDTLS_MODE_CCM ||
Gilles Peskine449bd832023-01-11 14:50:10 +01002288 mode == MBEDTLS_MODE_CHACHAPOLY) {
2289 return MBEDTLS_SSL_MODE_AEAD;
2290 }
Neil Armstrong8a0f3e82022-03-30 10:57:37 +02002291#endif /* MBEDTLS_GCM_C || MBEDTLS_CCM_C || MBEDTLS_CHACHAPOLY_C */
Neil Armstrong8a0f3e82022-03-30 10:57:37 +02002292
Gilles Peskine449bd832023-01-11 14:50:10 +01002293 return MBEDTLS_SSL_MODE_STREAM;
Neil Armstrong8a0f3e82022-03-30 10:57:37 +02002294}
Gilles Peskine301711e2022-04-26 16:57:05 +02002295#endif /* MBEDTLS_USE_PSA_CRYPTO */
Neil Armstrong8a0f3e82022-03-30 10:57:37 +02002296
Gilles Peskinee108d982022-04-26 16:50:40 +02002297static mbedtls_ssl_mode_t mbedtls_ssl_get_actual_mode(
2298 mbedtls_ssl_mode_t base_mode,
Gilles Peskine449bd832023-01-11 14:50:10 +01002299 int encrypt_then_mac)
Gilles Peskinee108d982022-04-26 16:50:40 +02002300{
2301#if defined(MBEDTLS_SSL_SOME_SUITES_USE_CBC_ETM)
Gilles Peskine449bd832023-01-11 14:50:10 +01002302 if (encrypt_then_mac == MBEDTLS_SSL_ETM_ENABLED &&
2303 base_mode == MBEDTLS_SSL_MODE_CBC) {
2304 return MBEDTLS_SSL_MODE_CBC_ETM;
Gilles Peskinee108d982022-04-26 16:50:40 +02002305 }
2306#else
2307 (void) encrypt_then_mac;
2308#endif
Gilles Peskine449bd832023-01-11 14:50:10 +01002309 return base_mode;
Gilles Peskinee108d982022-04-26 16:50:40 +02002310}
2311
Neil Armstrongab555e02022-04-04 11:07:59 +02002312mbedtls_ssl_mode_t mbedtls_ssl_get_mode_from_transform(
Gilles Peskine449bd832023-01-11 14:50:10 +01002313 const mbedtls_ssl_transform *transform)
Neil Armstrong4bf4c862022-04-01 10:35:48 +02002314{
Gilles Peskinee108d982022-04-26 16:50:40 +02002315 mbedtls_ssl_mode_t base_mode = mbedtls_ssl_get_base_mode(
Neil Armstrong4bf4c862022-04-01 10:35:48 +02002316#if defined(MBEDTLS_USE_PSA_CRYPTO)
Gilles Peskine449bd832023-01-11 14:50:10 +01002317 transform->psa_alg
Neil Armstrong4bf4c862022-04-01 10:35:48 +02002318#else
Gilles Peskine449bd832023-01-11 14:50:10 +01002319 mbedtls_cipher_get_cipher_mode(&transform->cipher_ctx_enc)
Gilles Peskinee108d982022-04-26 16:50:40 +02002320#endif
2321 );
Neil Armstrong4bf4c862022-04-01 10:35:48 +02002322
Gilles Peskinee108d982022-04-26 16:50:40 +02002323 int encrypt_then_mac = 0;
Neil Armstrongf2c82f02022-04-05 11:16:53 +02002324#if defined(MBEDTLS_SSL_SOME_SUITES_USE_CBC_ETM)
Gilles Peskinee108d982022-04-26 16:50:40 +02002325 encrypt_then_mac = transform->encrypt_then_mac;
2326#endif
Gilles Peskine449bd832023-01-11 14:50:10 +01002327 return mbedtls_ssl_get_actual_mode(base_mode, encrypt_then_mac);
Neil Armstrong4bf4c862022-04-01 10:35:48 +02002328}
2329
Neil Armstrongab555e02022-04-04 11:07:59 +02002330mbedtls_ssl_mode_t mbedtls_ssl_get_mode_from_ciphersuite(
Neil Armstrongf2c82f02022-04-05 11:16:53 +02002331#if defined(MBEDTLS_SSL_SOME_SUITES_USE_CBC_ETM)
Gilles Peskine449bd832023-01-11 14:50:10 +01002332 int encrypt_then_mac,
Neil Armstrongf2c82f02022-04-05 11:16:53 +02002333#endif /* MBEDTLS_SSL_SOME_SUITES_USE_CBC_ETM */
Gilles Peskine449bd832023-01-11 14:50:10 +01002334 const mbedtls_ssl_ciphersuite_t *suite)
Neil Armstrong4bf4c862022-04-01 10:35:48 +02002335{
Gilles Peskinee108d982022-04-26 16:50:40 +02002336 mbedtls_ssl_mode_t base_mode = MBEDTLS_SSL_MODE_STREAM;
2337
Neil Armstrong4bf4c862022-04-01 10:35:48 +02002338#if defined(MBEDTLS_USE_PSA_CRYPTO)
2339 psa_status_t status;
2340 psa_algorithm_t alg;
2341 psa_key_type_t type;
2342 size_t size;
Gilles Peskine449bd832023-01-11 14:50:10 +01002343 status = mbedtls_ssl_cipher_to_psa(suite->cipher, 0, &alg, &type, &size);
2344 if (status == PSA_SUCCESS) {
2345 base_mode = mbedtls_ssl_get_base_mode(alg);
2346 }
Neil Armstrong4bf4c862022-04-01 10:35:48 +02002347#else
2348 const mbedtls_cipher_info_t *cipher =
Gilles Peskine449bd832023-01-11 14:50:10 +01002349 mbedtls_cipher_info_from_type(suite->cipher);
2350 if (cipher != NULL) {
Gilles Peskinee108d982022-04-26 16:50:40 +02002351 base_mode =
2352 mbedtls_ssl_get_base_mode(
Gilles Peskine449bd832023-01-11 14:50:10 +01002353 mbedtls_cipher_info_get_mode(cipher));
Gilles Peskinee108d982022-04-26 16:50:40 +02002354 }
Neil Armstrong4bf4c862022-04-01 10:35:48 +02002355#endif /* MBEDTLS_USE_PSA_CRYPTO */
2356
Gilles Peskinee108d982022-04-26 16:50:40 +02002357#if !defined(MBEDTLS_SSL_SOME_SUITES_USE_CBC_ETM)
2358 int encrypt_then_mac = 0;
2359#endif
Gilles Peskine449bd832023-01-11 14:50:10 +01002360 return mbedtls_ssl_get_actual_mode(base_mode, encrypt_then_mac);
Neil Armstrong4bf4c862022-04-01 10:35:48 +02002361}
2362
Neil Armstrong8395d7a2022-05-18 11:44:56 +02002363#if defined(MBEDTLS_USE_PSA_CRYPTO) || defined(MBEDTLS_SSL_PROTO_TLS1_3)
Jerry Yu251a12e2022-07-13 15:15:48 +08002364
2365#if defined(MBEDTLS_SSL_PROTO_TLS1_3)
Jerry Yu438ddd82022-07-07 06:55:50 +00002366/* Serialization of TLS 1.3 sessions:
2367 *
2368 * struct {
Xiaokang Qian126bf8e2022-10-13 02:22:40 +00002369 * opaque hostname<0..2^16-1>;
Jerry Yu438ddd82022-07-07 06:55:50 +00002370 * uint64 ticket_received;
2371 * uint32 ticket_lifetime;
Jerry Yu34191072022-08-18 10:32:09 +08002372 * opaque ticket<1..2^16-1>;
Jerry Yu438ddd82022-07-07 06:55:50 +00002373 * } ClientOnlyData;
2374 *
2375 * struct {
2376 * uint8 endpoint;
2377 * uint8 ciphersuite[2];
2378 * uint32 ticket_age_add;
2379 * uint8 ticket_flags;
2380 * opaque resumption_key<0..255>;
2381 * select ( endpoint ) {
2382 * case client: ClientOnlyData;
2383 * case server: uint64 start_time;
2384 * };
2385 * } serialized_session_tls13;
2386 *
2387 */
2388#if defined(MBEDTLS_SSL_SESSION_TICKETS)
Jerry Yue36fdd62022-08-17 21:31:36 +08002389MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01002390static int ssl_tls13_session_save(const mbedtls_ssl_session *session,
2391 unsigned char *buf,
2392 size_t buf_len,
2393 size_t *olen)
Jerry Yu438ddd82022-07-07 06:55:50 +00002394{
2395 unsigned char *p = buf;
Xiaokang Qiana3b451f2022-10-11 06:20:56 +00002396#if defined(MBEDTLS_SSL_CLI_C) && \
2397 defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
Gilles Peskine449bd832023-01-11 14:50:10 +01002398 size_t hostname_len = (session->hostname == NULL) ?
2399 0 : strlen(session->hostname) + 1;
Xiaokang Qiana3b451f2022-10-11 06:20:56 +00002400#endif
Jerry Yubc7c1a42022-07-21 22:57:37 +08002401 size_t needed = 1 /* endpoint */
2402 + 2 /* ciphersuite */
2403 + 4 /* ticket_age_add */
Jerry Yu34191072022-08-18 10:32:09 +08002404 + 1 /* ticket_flags */
2405 + 1; /* resumption_key length */
Jerry Yue36fdd62022-08-17 21:31:36 +08002406 *olen = 0;
Jerry Yu34191072022-08-18 10:32:09 +08002407
Gilles Peskine449bd832023-01-11 14:50:10 +01002408 if (session->resumption_key_len > MBEDTLS_SSL_TLS1_3_TICKET_RESUMPTION_KEY_LEN) {
2409 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
2410 }
Jerry Yu34191072022-08-18 10:32:09 +08002411 needed += session->resumption_key_len; /* resumption_key */
2412
Jerry Yu438ddd82022-07-07 06:55:50 +00002413#if defined(MBEDTLS_HAVE_TIME)
2414 needed += 8; /* start_time or ticket_received */
2415#endif
2416
2417#if defined(MBEDTLS_SSL_CLI_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01002418 if (session->endpoint == MBEDTLS_SSL_IS_CLIENT) {
Xiaokang Qian126bf8e2022-10-13 02:22:40 +00002419#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
Xiaokang Qianbc663a02022-10-09 11:14:39 +00002420 needed += 2 /* hostname_len */
Gilles Peskine449bd832023-01-11 14:50:10 +01002421 + hostname_len; /* hostname */
Xiaokang Qian281fd1b2022-09-20 11:35:41 +00002422#endif
2423
Jerry Yu438ddd82022-07-07 06:55:50 +00002424 needed += 4 /* ticket_lifetime */
Jerry Yue36fdd62022-08-17 21:31:36 +08002425 + 2; /* ticket_len */
Jerry Yu34191072022-08-18 10:32:09 +08002426
2427 /* Check size_t overflow */
Gilles Peskine449bd832023-01-11 14:50:10 +01002428 if (session->ticket_len > SIZE_MAX - needed) {
2429 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
2430 }
Jerry Yu34191072022-08-18 10:32:09 +08002431
Jerry Yue28d9742022-08-18 15:44:03 +08002432 needed += session->ticket_len; /* ticket */
Jerry Yu438ddd82022-07-07 06:55:50 +00002433 }
2434#endif /* MBEDTLS_SSL_CLI_C */
2435
Jerry Yue36fdd62022-08-17 21:31:36 +08002436 *olen = needed;
Gilles Peskine449bd832023-01-11 14:50:10 +01002437 if (needed > buf_len) {
2438 return MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL;
2439 }
Jerry Yu438ddd82022-07-07 06:55:50 +00002440
2441 p[0] = session->endpoint;
Gilles Peskine449bd832023-01-11 14:50:10 +01002442 MBEDTLS_PUT_UINT16_BE(session->ciphersuite, p, 1);
2443 MBEDTLS_PUT_UINT32_BE(session->ticket_age_add, p, 3);
Jerry Yu438ddd82022-07-07 06:55:50 +00002444 p[7] = session->ticket_flags;
2445
2446 /* save resumption_key */
Jerry Yubc7c1a42022-07-21 22:57:37 +08002447 p[8] = session->resumption_key_len;
Jerry Yu438ddd82022-07-07 06:55:50 +00002448 p += 9;
Gilles Peskine449bd832023-01-11 14:50:10 +01002449 memcpy(p, session->resumption_key, session->resumption_key_len);
Jerry Yubc7c1a42022-07-21 22:57:37 +08002450 p += session->resumption_key_len;
Jerry Yu438ddd82022-07-07 06:55:50 +00002451
2452#if defined(MBEDTLS_HAVE_TIME) && defined(MBEDTLS_SSL_SRV_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01002453 if (session->endpoint == MBEDTLS_SSL_IS_SERVER) {
2454 MBEDTLS_PUT_UINT64_BE((uint64_t) session->start, p, 0);
Jerry Yu438ddd82022-07-07 06:55:50 +00002455 p += 8;
2456 }
2457#endif /* MBEDTLS_HAVE_TIME */
2458
2459#if defined(MBEDTLS_SSL_CLI_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01002460 if (session->endpoint == MBEDTLS_SSL_IS_CLIENT) {
Xiaokang Qianed0620c2022-10-12 06:58:13 +00002461#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
Gilles Peskine449bd832023-01-11 14:50:10 +01002462 MBEDTLS_PUT_UINT16_BE(hostname_len, p, 0);
Xiaokang Qianed0620c2022-10-12 06:58:13 +00002463 p += 2;
Gilles Peskine449bd832023-01-11 14:50:10 +01002464 if (hostname_len > 0) {
Xiaokang Qianed0620c2022-10-12 06:58:13 +00002465 /* save host name */
Gilles Peskine449bd832023-01-11 14:50:10 +01002466 memcpy(p, session->hostname, hostname_len);
Xiaokang Qianed0620c2022-10-12 06:58:13 +00002467 p += hostname_len;
2468 }
2469#endif /* MBEDTLS_SSL_SERVER_NAME_INDICATION */
2470
Jerry Yu438ddd82022-07-07 06:55:50 +00002471#if defined(MBEDTLS_HAVE_TIME)
Gilles Peskine449bd832023-01-11 14:50:10 +01002472 MBEDTLS_PUT_UINT64_BE((uint64_t) session->ticket_received, p, 0);
Jerry Yu438ddd82022-07-07 06:55:50 +00002473 p += 8;
2474#endif
Gilles Peskine449bd832023-01-11 14:50:10 +01002475 MBEDTLS_PUT_UINT32_BE(session->ticket_lifetime, p, 0);
Jerry Yu438ddd82022-07-07 06:55:50 +00002476 p += 4;
2477
Gilles Peskine449bd832023-01-11 14:50:10 +01002478 MBEDTLS_PUT_UINT16_BE(session->ticket_len, p, 0);
Jerry Yu438ddd82022-07-07 06:55:50 +00002479 p += 2;
Jerry Yu34191072022-08-18 10:32:09 +08002480
Gilles Peskine449bd832023-01-11 14:50:10 +01002481 if (session->ticket != NULL && session->ticket_len > 0) {
2482 memcpy(p, session->ticket, session->ticket_len);
Jerry Yu438ddd82022-07-07 06:55:50 +00002483 p += session->ticket_len;
2484 }
2485 }
2486#endif /* MBEDTLS_SSL_CLI_C */
Gilles Peskine449bd832023-01-11 14:50:10 +01002487 return 0;
Jerry Yu438ddd82022-07-07 06:55:50 +00002488}
2489
2490MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01002491static int ssl_tls13_session_load(mbedtls_ssl_session *session,
2492 const unsigned char *buf,
2493 size_t len)
Jerry Yu438ddd82022-07-07 06:55:50 +00002494{
2495 const unsigned char *p = buf;
2496 const unsigned char *end = buf + len;
2497
Gilles Peskine449bd832023-01-11 14:50:10 +01002498 if (end - p < 9) {
2499 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
2500 }
Jerry Yu438ddd82022-07-07 06:55:50 +00002501 session->endpoint = p[0];
Gilles Peskine449bd832023-01-11 14:50:10 +01002502 session->ciphersuite = MBEDTLS_GET_UINT16_BE(p, 1);
2503 session->ticket_age_add = MBEDTLS_GET_UINT32_BE(p, 3);
Jerry Yu438ddd82022-07-07 06:55:50 +00002504 session->ticket_flags = p[7];
2505
2506 /* load resumption_key */
Jerry Yubc7c1a42022-07-21 22:57:37 +08002507 session->resumption_key_len = p[8];
Jerry Yu438ddd82022-07-07 06:55:50 +00002508 p += 9;
2509
Gilles Peskine449bd832023-01-11 14:50:10 +01002510 if (end - p < session->resumption_key_len) {
2511 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
2512 }
Jerry Yu438ddd82022-07-07 06:55:50 +00002513
Gilles Peskine449bd832023-01-11 14:50:10 +01002514 if (sizeof(session->resumption_key) < session->resumption_key_len) {
2515 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
2516 }
2517 memcpy(session->resumption_key, p, session->resumption_key_len);
Jerry Yubc7c1a42022-07-21 22:57:37 +08002518 p += session->resumption_key_len;
Jerry Yu438ddd82022-07-07 06:55:50 +00002519
2520#if defined(MBEDTLS_HAVE_TIME) && defined(MBEDTLS_SSL_SRV_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01002521 if (session->endpoint == MBEDTLS_SSL_IS_SERVER) {
2522 if (end - p < 8) {
2523 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
2524 }
2525 session->start = MBEDTLS_GET_UINT64_BE(p, 0);
Jerry Yu438ddd82022-07-07 06:55:50 +00002526 p += 8;
2527 }
2528#endif /* MBEDTLS_HAVE_TIME */
2529
2530#if defined(MBEDTLS_SSL_CLI_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01002531 if (session->endpoint == MBEDTLS_SSL_IS_CLIENT) {
Xiaokang Qianed0620c2022-10-12 06:58:13 +00002532#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION) && \
Gilles Peskine449bd832023-01-11 14:50:10 +01002533 defined(MBEDTLS_SSL_SESSION_TICKETS)
Xiaokang Qianed0620c2022-10-12 06:58:13 +00002534 size_t hostname_len;
2535 /* load host name */
Gilles Peskine449bd832023-01-11 14:50:10 +01002536 if (end - p < 2) {
2537 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
2538 }
2539 hostname_len = MBEDTLS_GET_UINT16_BE(p, 0);
Xiaokang Qianed0620c2022-10-12 06:58:13 +00002540 p += 2;
2541
Gilles Peskine449bd832023-01-11 14:50:10 +01002542 if (end - p < (long int) hostname_len) {
2543 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
2544 }
2545 if (hostname_len > 0) {
2546 session->hostname = mbedtls_calloc(1, hostname_len);
2547 if (session->hostname == NULL) {
2548 return MBEDTLS_ERR_SSL_ALLOC_FAILED;
2549 }
2550 memcpy(session->hostname, p, hostname_len);
Xiaokang Qianed0620c2022-10-12 06:58:13 +00002551 p += hostname_len;
2552 }
2553#endif /* MBEDTLS_SSL_SERVER_NAME_INDICATION &&
2554 MBEDTLS_SSL_SESSION_TICKETS */
2555
Jerry Yu438ddd82022-07-07 06:55:50 +00002556#if defined(MBEDTLS_HAVE_TIME)
Gilles Peskine449bd832023-01-11 14:50:10 +01002557 if (end - p < 8) {
2558 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
2559 }
2560 session->ticket_received = MBEDTLS_GET_UINT64_BE(p, 0);
Jerry Yu438ddd82022-07-07 06:55:50 +00002561 p += 8;
2562#endif
Gilles Peskine449bd832023-01-11 14:50:10 +01002563 if (end - p < 4) {
2564 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
2565 }
2566 session->ticket_lifetime = MBEDTLS_GET_UINT32_BE(p, 0);
Jerry Yu438ddd82022-07-07 06:55:50 +00002567 p += 4;
2568
Gilles Peskine449bd832023-01-11 14:50:10 +01002569 if (end - p < 2) {
2570 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
2571 }
2572 session->ticket_len = MBEDTLS_GET_UINT16_BE(p, 0);
Jerry Yu438ddd82022-07-07 06:55:50 +00002573 p += 2;
2574
Gilles Peskine449bd832023-01-11 14:50:10 +01002575 if (end - p < (long int) session->ticket_len) {
2576 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
2577 }
2578 if (session->ticket_len > 0) {
2579 session->ticket = mbedtls_calloc(1, session->ticket_len);
2580 if (session->ticket == NULL) {
2581 return MBEDTLS_ERR_SSL_ALLOC_FAILED;
2582 }
2583 memcpy(session->ticket, p, session->ticket_len);
Jerry Yu438ddd82022-07-07 06:55:50 +00002584 p += session->ticket_len;
2585 }
2586 }
2587#endif /* MBEDTLS_SSL_CLI_C */
2588
Gilles Peskine449bd832023-01-11 14:50:10 +01002589 return 0;
Jerry Yu438ddd82022-07-07 06:55:50 +00002590
2591}
2592#else /* MBEDTLS_SSL_SESSION_TICKETS */
Jerry Yue36fdd62022-08-17 21:31:36 +08002593MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01002594static int ssl_tls13_session_save(const mbedtls_ssl_session *session,
2595 unsigned char *buf,
2596 size_t buf_len,
2597 size_t *olen)
Jerry Yu251a12e2022-07-13 15:15:48 +08002598{
2599 ((void) session);
2600 ((void) buf);
2601 ((void) buf_len);
Jerry Yue36fdd62022-08-17 21:31:36 +08002602 *olen = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +01002603 return MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
Jerry Yu251a12e2022-07-13 15:15:48 +08002604}
Jerry Yu438ddd82022-07-07 06:55:50 +00002605
Gilles Peskine449bd832023-01-11 14:50:10 +01002606static int ssl_tls13_session_load(const mbedtls_ssl_session *session,
2607 unsigned char *buf,
2608 size_t buf_len)
Jerry Yu438ddd82022-07-07 06:55:50 +00002609{
2610 ((void) session);
2611 ((void) buf);
2612 ((void) buf_len);
Gilles Peskine449bd832023-01-11 14:50:10 +01002613 return MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
Jerry Yu438ddd82022-07-07 06:55:50 +00002614}
2615#endif /* !MBEDTLS_SSL_SESSION_TICKETS */
Jerry Yu251a12e2022-07-13 15:15:48 +08002616#endif /* MBEDTLS_SSL_PROTO_TLS1_3 */
2617
Gilles Peskine449bd832023-01-11 14:50:10 +01002618psa_status_t mbedtls_ssl_cipher_to_psa(mbedtls_cipher_type_t mbedtls_cipher_type,
2619 size_t taglen,
2620 psa_algorithm_t *alg,
2621 psa_key_type_t *key_type,
2622 size_t *key_size)
Przemyslaw Stekiel430f3372022-01-10 11:55:46 +01002623{
Gilles Peskine449bd832023-01-11 14:50:10 +01002624 switch (mbedtls_cipher_type) {
Przemyslaw Stekiel430f3372022-01-10 11:55:46 +01002625 case MBEDTLS_CIPHER_AES_128_CBC:
2626 *alg = PSA_ALG_CBC_NO_PADDING;
2627 *key_type = PSA_KEY_TYPE_AES;
2628 *key_size = 128;
2629 break;
2630 case MBEDTLS_CIPHER_AES_128_CCM:
Gilles Peskine449bd832023-01-11 14:50:10 +01002631 *alg = taglen ? PSA_ALG_AEAD_WITH_SHORTENED_TAG(PSA_ALG_CCM, taglen) : PSA_ALG_CCM;
Przemyslaw Stekiel430f3372022-01-10 11:55:46 +01002632 *key_type = PSA_KEY_TYPE_AES;
2633 *key_size = 128;
2634 break;
2635 case MBEDTLS_CIPHER_AES_128_GCM:
2636 *alg = PSA_ALG_GCM;
2637 *key_type = PSA_KEY_TYPE_AES;
2638 *key_size = 128;
2639 break;
2640 case MBEDTLS_CIPHER_AES_192_CCM:
Gilles Peskine449bd832023-01-11 14:50:10 +01002641 *alg = taglen ? PSA_ALG_AEAD_WITH_SHORTENED_TAG(PSA_ALG_CCM, taglen) : PSA_ALG_CCM;
Przemyslaw Stekiel430f3372022-01-10 11:55:46 +01002642 *key_type = PSA_KEY_TYPE_AES;
2643 *key_size = 192;
2644 break;
2645 case MBEDTLS_CIPHER_AES_192_GCM:
2646 *alg = PSA_ALG_GCM;
2647 *key_type = PSA_KEY_TYPE_AES;
2648 *key_size = 192;
2649 break;
2650 case MBEDTLS_CIPHER_AES_256_CBC:
2651 *alg = PSA_ALG_CBC_NO_PADDING;
2652 *key_type = PSA_KEY_TYPE_AES;
2653 *key_size = 256;
2654 break;
2655 case MBEDTLS_CIPHER_AES_256_CCM:
Gilles Peskine449bd832023-01-11 14:50:10 +01002656 *alg = taglen ? PSA_ALG_AEAD_WITH_SHORTENED_TAG(PSA_ALG_CCM, taglen) : PSA_ALG_CCM;
Przemyslaw Stekiel430f3372022-01-10 11:55:46 +01002657 *key_type = PSA_KEY_TYPE_AES;
2658 *key_size = 256;
2659 break;
2660 case MBEDTLS_CIPHER_AES_256_GCM:
2661 *alg = PSA_ALG_GCM;
2662 *key_type = PSA_KEY_TYPE_AES;
2663 *key_size = 256;
2664 break;
2665 case MBEDTLS_CIPHER_ARIA_128_CBC:
2666 *alg = PSA_ALG_CBC_NO_PADDING;
2667 *key_type = PSA_KEY_TYPE_ARIA;
2668 *key_size = 128;
2669 break;
2670 case MBEDTLS_CIPHER_ARIA_128_CCM:
Gilles Peskine449bd832023-01-11 14:50:10 +01002671 *alg = taglen ? PSA_ALG_AEAD_WITH_SHORTENED_TAG(PSA_ALG_CCM, taglen) : PSA_ALG_CCM;
Przemyslaw Stekiel430f3372022-01-10 11:55:46 +01002672 *key_type = PSA_KEY_TYPE_ARIA;
2673 *key_size = 128;
2674 break;
2675 case MBEDTLS_CIPHER_ARIA_128_GCM:
2676 *alg = PSA_ALG_GCM;
2677 *key_type = PSA_KEY_TYPE_ARIA;
2678 *key_size = 128;
2679 break;
2680 case MBEDTLS_CIPHER_ARIA_192_CCM:
Gilles Peskine449bd832023-01-11 14:50:10 +01002681 *alg = taglen ? PSA_ALG_AEAD_WITH_SHORTENED_TAG(PSA_ALG_CCM, taglen) : PSA_ALG_CCM;
Przemyslaw Stekiel430f3372022-01-10 11:55:46 +01002682 *key_type = PSA_KEY_TYPE_ARIA;
2683 *key_size = 192;
2684 break;
2685 case MBEDTLS_CIPHER_ARIA_192_GCM:
2686 *alg = PSA_ALG_GCM;
2687 *key_type = PSA_KEY_TYPE_ARIA;
2688 *key_size = 192;
2689 break;
2690 case MBEDTLS_CIPHER_ARIA_256_CBC:
2691 *alg = PSA_ALG_CBC_NO_PADDING;
2692 *key_type = PSA_KEY_TYPE_ARIA;
2693 *key_size = 256;
2694 break;
2695 case MBEDTLS_CIPHER_ARIA_256_CCM:
Gilles Peskine449bd832023-01-11 14:50:10 +01002696 *alg = taglen ? PSA_ALG_AEAD_WITH_SHORTENED_TAG(PSA_ALG_CCM, taglen) : PSA_ALG_CCM;
Przemyslaw Stekiel430f3372022-01-10 11:55:46 +01002697 *key_type = PSA_KEY_TYPE_ARIA;
2698 *key_size = 256;
2699 break;
2700 case MBEDTLS_CIPHER_ARIA_256_GCM:
2701 *alg = PSA_ALG_GCM;
2702 *key_type = PSA_KEY_TYPE_ARIA;
2703 *key_size = 256;
2704 break;
2705 case MBEDTLS_CIPHER_CAMELLIA_128_CBC:
2706 *alg = PSA_ALG_CBC_NO_PADDING;
2707 *key_type = PSA_KEY_TYPE_CAMELLIA;
2708 *key_size = 128;
2709 break;
2710 case MBEDTLS_CIPHER_CAMELLIA_128_CCM:
Gilles Peskine449bd832023-01-11 14:50:10 +01002711 *alg = taglen ? PSA_ALG_AEAD_WITH_SHORTENED_TAG(PSA_ALG_CCM, taglen) : PSA_ALG_CCM;
Przemyslaw Stekiel430f3372022-01-10 11:55:46 +01002712 *key_type = PSA_KEY_TYPE_CAMELLIA;
2713 *key_size = 128;
2714 break;
2715 case MBEDTLS_CIPHER_CAMELLIA_128_GCM:
2716 *alg = PSA_ALG_GCM;
2717 *key_type = PSA_KEY_TYPE_CAMELLIA;
2718 *key_size = 128;
2719 break;
2720 case MBEDTLS_CIPHER_CAMELLIA_192_CCM:
Gilles Peskine449bd832023-01-11 14:50:10 +01002721 *alg = taglen ? PSA_ALG_AEAD_WITH_SHORTENED_TAG(PSA_ALG_CCM, taglen) : PSA_ALG_CCM;
Przemyslaw Stekiel430f3372022-01-10 11:55:46 +01002722 *key_type = PSA_KEY_TYPE_CAMELLIA;
2723 *key_size = 192;
2724 break;
2725 case MBEDTLS_CIPHER_CAMELLIA_192_GCM:
2726 *alg = PSA_ALG_GCM;
2727 *key_type = PSA_KEY_TYPE_CAMELLIA;
2728 *key_size = 192;
2729 break;
2730 case MBEDTLS_CIPHER_CAMELLIA_256_CBC:
2731 *alg = PSA_ALG_CBC_NO_PADDING;
2732 *key_type = PSA_KEY_TYPE_CAMELLIA;
2733 *key_size = 256;
2734 break;
2735 case MBEDTLS_CIPHER_CAMELLIA_256_CCM:
Gilles Peskine449bd832023-01-11 14:50:10 +01002736 *alg = taglen ? PSA_ALG_AEAD_WITH_SHORTENED_TAG(PSA_ALG_CCM, taglen) : PSA_ALG_CCM;
Przemyslaw Stekiel430f3372022-01-10 11:55:46 +01002737 *key_type = PSA_KEY_TYPE_CAMELLIA;
2738 *key_size = 256;
2739 break;
2740 case MBEDTLS_CIPHER_CAMELLIA_256_GCM:
2741 *alg = PSA_ALG_GCM;
2742 *key_type = PSA_KEY_TYPE_CAMELLIA;
2743 *key_size = 256;
2744 break;
2745 case MBEDTLS_CIPHER_CHACHA20_POLY1305:
2746 *alg = PSA_ALG_CHACHA20_POLY1305;
2747 *key_type = PSA_KEY_TYPE_CHACHA20;
2748 *key_size = 256;
2749 break;
2750 case MBEDTLS_CIPHER_NULL:
2751 *alg = MBEDTLS_SSL_NULL_CIPHER;
2752 *key_type = 0;
2753 *key_size = 0;
2754 break;
2755 default:
2756 return PSA_ERROR_NOT_SUPPORTED;
2757 }
2758
2759 return PSA_SUCCESS;
2760}
Neil Armstrong8395d7a2022-05-18 11:44:56 +02002761#endif /* MBEDTLS_USE_PSA_CRYPTO || MBEDTLS_SSL_PROTO_TLS1_3 */
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01002762
Manuel Pégourié-Gonnardcf141ca2015-05-20 10:35:51 +02002763#if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_SSL_SRV_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01002764int mbedtls_ssl_conf_dh_param_bin(mbedtls_ssl_config *conf,
2765 const unsigned char *dhm_P, size_t P_len,
2766 const unsigned char *dhm_G, size_t G_len)
Hanno Beckera90658f2017-10-04 15:29:08 +01002767{
Janos Follath865b3eb2019-12-16 11:46:15 +00002768 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Hanno Beckera90658f2017-10-04 15:29:08 +01002769
Gilles Peskine449bd832023-01-11 14:50:10 +01002770 mbedtls_mpi_free(&conf->dhm_P);
2771 mbedtls_mpi_free(&conf->dhm_G);
Glenn Strausscee11292021-12-20 01:43:17 -05002772
Gilles Peskine449bd832023-01-11 14:50:10 +01002773 if ((ret = mbedtls_mpi_read_binary(&conf->dhm_P, dhm_P, P_len)) != 0 ||
2774 (ret = mbedtls_mpi_read_binary(&conf->dhm_G, dhm_G, G_len)) != 0) {
2775 mbedtls_mpi_free(&conf->dhm_P);
2776 mbedtls_mpi_free(&conf->dhm_G);
2777 return ret;
Hanno Beckera90658f2017-10-04 15:29:08 +01002778 }
2779
Gilles Peskine449bd832023-01-11 14:50:10 +01002780 return 0;
Hanno Beckera90658f2017-10-04 15:29:08 +01002781}
Paul Bakker5121ce52009-01-03 21:22:43 +00002782
Gilles Peskine449bd832023-01-11 14:50:10 +01002783int mbedtls_ssl_conf_dh_param_ctx(mbedtls_ssl_config *conf, mbedtls_dhm_context *dhm_ctx)
Paul Bakker1b57b062011-01-06 15:48:19 +00002784{
Janos Follath865b3eb2019-12-16 11:46:15 +00002785 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker1b57b062011-01-06 15:48:19 +00002786
Gilles Peskine449bd832023-01-11 14:50:10 +01002787 mbedtls_mpi_free(&conf->dhm_P);
2788 mbedtls_mpi_free(&conf->dhm_G);
Glenn Strausscee11292021-12-20 01:43:17 -05002789
Gilles Peskine449bd832023-01-11 14:50:10 +01002790 if ((ret = mbedtls_dhm_get_value(dhm_ctx, MBEDTLS_DHM_PARAM_P,
2791 &conf->dhm_P)) != 0 ||
2792 (ret = mbedtls_dhm_get_value(dhm_ctx, MBEDTLS_DHM_PARAM_G,
2793 &conf->dhm_G)) != 0) {
2794 mbedtls_mpi_free(&conf->dhm_P);
2795 mbedtls_mpi_free(&conf->dhm_G);
2796 return ret;
Manuel Pégourié-Gonnard1028b742015-05-06 17:33:07 +01002797 }
Paul Bakker1b57b062011-01-06 15:48:19 +00002798
Gilles Peskine449bd832023-01-11 14:50:10 +01002799 return 0;
Paul Bakker1b57b062011-01-06 15:48:19 +00002800}
Manuel Pégourié-Gonnardcf141ca2015-05-20 10:35:51 +02002801#endif /* MBEDTLS_DHM_C && MBEDTLS_SSL_SRV_C */
Paul Bakker1b57b062011-01-06 15:48:19 +00002802
Manuel Pégourié-Gonnardbd990d62015-06-11 14:49:42 +02002803#if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_SSL_CLI_C)
2804/*
2805 * Set the minimum length for Diffie-Hellman parameters
2806 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002807void mbedtls_ssl_conf_dhm_min_bitlen(mbedtls_ssl_config *conf,
2808 unsigned int bitlen)
Manuel Pégourié-Gonnardbd990d62015-06-11 14:49:42 +02002809{
2810 conf->dhm_min_bitlen = bitlen;
2811}
2812#endif /* MBEDTLS_DHM_C && MBEDTLS_SSL_CLI_C */
2813
Ronald Crone68ab4f2022-10-05 12:46:29 +02002814#if defined(MBEDTLS_SSL_HANDSHAKE_WITH_CERT_ENABLED)
Jerry Yu7ddc38c2022-01-19 11:08:05 +08002815#if !defined(MBEDTLS_DEPRECATED_REMOVED) && defined(MBEDTLS_SSL_PROTO_TLS1_2)
Manuel Pégourié-Gonnard36a8b572015-06-17 12:43:26 +02002816/*
2817 * Set allowed/preferred hashes for handshake signatures
2818 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002819void mbedtls_ssl_conf_sig_hashes(mbedtls_ssl_config *conf,
2820 const int *hashes)
Manuel Pégourié-Gonnard36a8b572015-06-17 12:43:26 +02002821{
2822 conf->sig_hashes = hashes;
2823}
Jerry Yu7ddc38c2022-01-19 11:08:05 +08002824#endif /* !MBEDTLS_DEPRECATED_REMOVED && MBEDTLS_SSL_PROTO_TLS1_2 */
Hanno Becker1cd6e002021-08-10 13:27:10 +01002825
Jerry Yuf017ee42022-01-12 15:49:48 +08002826/* Configure allowed signature algorithms for handshake */
Gilles Peskine449bd832023-01-11 14:50:10 +01002827void mbedtls_ssl_conf_sig_algs(mbedtls_ssl_config *conf,
2828 const uint16_t *sig_algs)
Hanno Becker1cd6e002021-08-10 13:27:10 +01002829{
Jerry Yuf017ee42022-01-12 15:49:48 +08002830#if !defined(MBEDTLS_DEPRECATED_REMOVED)
2831 conf->sig_hashes = NULL;
2832#endif /* !MBEDTLS_DEPRECATED_REMOVED */
2833 conf->sig_algs = sig_algs;
Hanno Becker1cd6e002021-08-10 13:27:10 +01002834}
Ronald Crone68ab4f2022-10-05 12:46:29 +02002835#endif /* MBEDTLS_SSL_HANDSHAKE_WITH_CERT_ENABLED */
Manuel Pégourié-Gonnard36a8b572015-06-17 12:43:26 +02002836
Manuel Pégourié-Gonnardb541da62015-06-17 11:43:30 +02002837#if defined(MBEDTLS_ECP_C)
Brett Warrene0edc842021-08-17 09:53:13 +01002838#if !defined(MBEDTLS_DEPRECATED_REMOVED)
Manuel Pégourié-Gonnard7f38ed02014-02-04 15:52:33 +01002839/*
2840 * Set the allowed elliptic curves
Brett Warrene0edc842021-08-17 09:53:13 +01002841 *
2842 * mbedtls_ssl_setup() takes the provided list
2843 * and translates it to a list of IANA TLS group identifiers,
2844 * stored in ssl->handshake->group_list.
2845 *
Manuel Pégourié-Gonnard7f38ed02014-02-04 15:52:33 +01002846 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002847void mbedtls_ssl_conf_curves(mbedtls_ssl_config *conf,
2848 const mbedtls_ecp_group_id *curve_list)
Manuel Pégourié-Gonnard7f38ed02014-02-04 15:52:33 +01002849{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02002850 conf->curve_list = curve_list;
Brett Warrene0edc842021-08-17 09:53:13 +01002851 conf->group_list = NULL;
Manuel Pégourié-Gonnard7f38ed02014-02-04 15:52:33 +01002852}
Brett Warrene0edc842021-08-17 09:53:13 +01002853#endif /* MBEDTLS_DEPRECATED_REMOVED */
Hanno Becker947194e2017-04-07 13:25:49 +01002854#endif /* MBEDTLS_ECP_C */
Manuel Pégourié-Gonnard7f38ed02014-02-04 15:52:33 +01002855
Brett Warrene0edc842021-08-17 09:53:13 +01002856/*
2857 * Set the allowed groups
2858 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002859void mbedtls_ssl_conf_groups(mbedtls_ssl_config *conf,
2860 const uint16_t *group_list)
Brett Warrene0edc842021-08-17 09:53:13 +01002861{
2862#if defined(MBEDTLS_ECP_C) && !defined(MBEDTLS_DEPRECATED_REMOVED)
2863 conf->curve_list = NULL;
2864#endif
2865 conf->group_list = group_list;
2866}
2867
Manuel Pégourié-Gonnardbc2b7712015-05-06 11:14:19 +01002868#if defined(MBEDTLS_X509_CRT_PARSE_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01002869int mbedtls_ssl_set_hostname(mbedtls_ssl_context *ssl, const char *hostname)
Paul Bakker5121ce52009-01-03 21:22:43 +00002870{
Hanno Becker947194e2017-04-07 13:25:49 +01002871 /* Initialize to suppress unnecessary compiler warning */
2872 size_t hostname_len = 0;
2873
2874 /* Check if new hostname is valid before
2875 * making any change to current one */
Gilles Peskine449bd832023-01-11 14:50:10 +01002876 if (hostname != NULL) {
2877 hostname_len = strlen(hostname);
Hanno Becker947194e2017-04-07 13:25:49 +01002878
Gilles Peskine449bd832023-01-11 14:50:10 +01002879 if (hostname_len > MBEDTLS_SSL_MAX_HOST_NAME_LEN) {
2880 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
2881 }
Hanno Becker947194e2017-04-07 13:25:49 +01002882 }
2883
2884 /* Now it's clear that we will overwrite the old hostname,
2885 * so we can free it safely */
2886
Gilles Peskine449bd832023-01-11 14:50:10 +01002887 if (ssl->hostname != NULL) {
2888 mbedtls_platform_zeroize(ssl->hostname, strlen(ssl->hostname));
2889 mbedtls_free(ssl->hostname);
Hanno Becker947194e2017-04-07 13:25:49 +01002890 }
2891
2892 /* Passing NULL as hostname shall clear the old one */
Manuel Pégourié-Gonnardba26c242015-05-06 10:47:06 +01002893
Gilles Peskine449bd832023-01-11 14:50:10 +01002894 if (hostname == NULL) {
Hanno Becker947194e2017-04-07 13:25:49 +01002895 ssl->hostname = NULL;
Gilles Peskine449bd832023-01-11 14:50:10 +01002896 } else {
2897 ssl->hostname = mbedtls_calloc(1, hostname_len + 1);
2898 if (ssl->hostname == NULL) {
2899 return MBEDTLS_ERR_SSL_ALLOC_FAILED;
2900 }
Paul Bakker75c1a6f2013-08-19 14:25:29 +02002901
Gilles Peskine449bd832023-01-11 14:50:10 +01002902 memcpy(ssl->hostname, hostname, hostname_len);
Paul Bakker75c1a6f2013-08-19 14:25:29 +02002903
Hanno Becker947194e2017-04-07 13:25:49 +01002904 ssl->hostname[hostname_len] = '\0';
2905 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002906
Gilles Peskine449bd832023-01-11 14:50:10 +01002907 return 0;
Paul Bakker5121ce52009-01-03 21:22:43 +00002908}
Hanno Becker1a9a51c2017-04-07 13:02:16 +01002909#endif /* MBEDTLS_X509_CRT_PARSE_C */
Paul Bakker5121ce52009-01-03 21:22:43 +00002910
Manuel Pégourié-Gonnardbc2b7712015-05-06 11:14:19 +01002911#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
Gilles Peskine449bd832023-01-11 14:50:10 +01002912void mbedtls_ssl_conf_sni(mbedtls_ssl_config *conf,
2913 int (*f_sni)(void *, mbedtls_ssl_context *,
2914 const unsigned char *, size_t),
2915 void *p_sni)
Paul Bakker5701cdc2012-09-27 21:49:42 +00002916{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02002917 conf->f_sni = f_sni;
2918 conf->p_sni = p_sni;
Paul Bakker5701cdc2012-09-27 21:49:42 +00002919}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002920#endif /* MBEDTLS_SSL_SERVER_NAME_INDICATION */
Paul Bakker5701cdc2012-09-27 21:49:42 +00002921
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002922#if defined(MBEDTLS_SSL_ALPN)
Gilles Peskine449bd832023-01-11 14:50:10 +01002923int mbedtls_ssl_conf_alpn_protocols(mbedtls_ssl_config *conf, const char **protos)
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02002924{
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +02002925 size_t cur_len, tot_len;
2926 const char **p;
2927
2928 /*
Brian J Murray1903fb32016-11-06 04:45:15 -08002929 * RFC 7301 3.1: "Empty strings MUST NOT be included and byte strings
2930 * MUST NOT be truncated."
2931 * We check lengths now rather than later.
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +02002932 */
2933 tot_len = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +01002934 for (p = protos; *p != NULL; p++) {
2935 cur_len = strlen(*p);
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +02002936 tot_len += cur_len;
2937
Gilles Peskine449bd832023-01-11 14:50:10 +01002938 if ((cur_len == 0) ||
2939 (cur_len > MBEDTLS_SSL_MAX_ALPN_NAME_LEN) ||
2940 (tot_len > MBEDTLS_SSL_MAX_ALPN_LIST_LEN)) {
2941 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
2942 }
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +02002943 }
2944
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02002945 conf->alpn_list = protos;
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +02002946
Gilles Peskine449bd832023-01-11 14:50:10 +01002947 return 0;
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02002948}
2949
Gilles Peskine449bd832023-01-11 14:50:10 +01002950const char *mbedtls_ssl_get_alpn_protocol(const mbedtls_ssl_context *ssl)
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02002951{
Gilles Peskine449bd832023-01-11 14:50:10 +01002952 return ssl->alpn_chosen;
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02002953}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002954#endif /* MBEDTLS_SSL_ALPN */
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02002955
Johan Pascalb62bb512015-12-03 21:56:45 +01002956#if defined(MBEDTLS_SSL_DTLS_SRTP)
Gilles Peskine449bd832023-01-11 14:50:10 +01002957void mbedtls_ssl_conf_srtp_mki_value_supported(mbedtls_ssl_config *conf,
2958 int support_mki_value)
Ron Eldor591f1622018-01-22 12:30:04 +02002959{
2960 conf->dtls_srtp_mki_support = support_mki_value;
2961}
2962
Gilles Peskine449bd832023-01-11 14:50:10 +01002963int mbedtls_ssl_dtls_srtp_set_mki_value(mbedtls_ssl_context *ssl,
2964 unsigned char *mki_value,
2965 uint16_t mki_len)
Ron Eldor591f1622018-01-22 12:30:04 +02002966{
Gilles Peskine449bd832023-01-11 14:50:10 +01002967 if (mki_len > MBEDTLS_TLS_SRTP_MAX_MKI_LENGTH) {
2968 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
Ron Eldora9788042018-12-05 11:04:31 +02002969 }
Ron Eldor591f1622018-01-22 12:30:04 +02002970
Gilles Peskine449bd832023-01-11 14:50:10 +01002971 if (ssl->conf->dtls_srtp_mki_support == MBEDTLS_SSL_DTLS_SRTP_MKI_UNSUPPORTED) {
2972 return MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
Ron Eldora9788042018-12-05 11:04:31 +02002973 }
Ron Eldor591f1622018-01-22 12:30:04 +02002974
Gilles Peskine449bd832023-01-11 14:50:10 +01002975 memcpy(ssl->dtls_srtp_info.mki_value, mki_value, mki_len);
Ron Eldor591f1622018-01-22 12:30:04 +02002976 ssl->dtls_srtp_info.mki_len = mki_len;
Gilles Peskine449bd832023-01-11 14:50:10 +01002977 return 0;
Ron Eldor591f1622018-01-22 12:30:04 +02002978}
2979
Gilles Peskine449bd832023-01-11 14:50:10 +01002980int mbedtls_ssl_conf_dtls_srtp_protection_profiles(mbedtls_ssl_config *conf,
2981 const mbedtls_ssl_srtp_profile *profiles)
Johan Pascalb62bb512015-12-03 21:56:45 +01002982{
Johan Pascal253d0262020-09-22 13:04:45 +02002983 const mbedtls_ssl_srtp_profile *p;
2984 size_t list_size = 0;
Johan Pascalb62bb512015-12-03 21:56:45 +01002985
Johan Pascal253d0262020-09-22 13:04:45 +02002986 /* check the profiles list: all entry must be valid,
2987 * its size cannot be more than the total number of supported profiles, currently 4 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002988 for (p = profiles; *p != MBEDTLS_TLS_SRTP_UNSET &&
2989 list_size <= MBEDTLS_TLS_SRTP_MAX_PROFILE_LIST_LENGTH;
2990 p++) {
2991 if (mbedtls_ssl_check_srtp_profile_value(*p) != MBEDTLS_TLS_SRTP_UNSET) {
Johan Pascal76fdf1d2020-10-22 23:31:00 +02002992 list_size++;
Gilles Peskine449bd832023-01-11 14:50:10 +01002993 } else {
Johan Pascal76fdf1d2020-10-22 23:31:00 +02002994 /* unsupported value, stop parsing and set the size to an error value */
2995 list_size = MBEDTLS_TLS_SRTP_MAX_PROFILE_LIST_LENGTH + 1;
Johan Pascalb62bb512015-12-03 21:56:45 +01002996 }
2997 }
2998
Gilles Peskine449bd832023-01-11 14:50:10 +01002999 if (list_size > MBEDTLS_TLS_SRTP_MAX_PROFILE_LIST_LENGTH) {
3000 conf->dtls_srtp_profile_list = NULL;
3001 conf->dtls_srtp_profile_list_len = 0;
3002 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
Johan Pascal253d0262020-09-22 13:04:45 +02003003 }
3004
Johan Pascal9bc97ca2020-09-21 23:44:45 +02003005 conf->dtls_srtp_profile_list = profiles;
Johan Pascal253d0262020-09-22 13:04:45 +02003006 conf->dtls_srtp_profile_list_len = list_size;
Johan Pascalb62bb512015-12-03 21:56:45 +01003007
Gilles Peskine449bd832023-01-11 14:50:10 +01003008 return 0;
Johan Pascalb62bb512015-12-03 21:56:45 +01003009}
3010
Gilles Peskine449bd832023-01-11 14:50:10 +01003011void mbedtls_ssl_get_dtls_srtp_negotiation_result(const mbedtls_ssl_context *ssl,
3012 mbedtls_dtls_srtp_info *dtls_srtp_info)
Johan Pascalb62bb512015-12-03 21:56:45 +01003013{
Johan Pascal2258a4f2020-10-28 13:53:09 +01003014 dtls_srtp_info->chosen_dtls_srtp_profile = ssl->dtls_srtp_info.chosen_dtls_srtp_profile;
3015 /* do not copy the mki value if there is no chosen profile */
Gilles Peskine449bd832023-01-11 14:50:10 +01003016 if (dtls_srtp_info->chosen_dtls_srtp_profile == MBEDTLS_TLS_SRTP_UNSET) {
Johan Pascal2258a4f2020-10-28 13:53:09 +01003017 dtls_srtp_info->mki_len = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +01003018 } else {
Johan Pascal2258a4f2020-10-28 13:53:09 +01003019 dtls_srtp_info->mki_len = ssl->dtls_srtp_info.mki_len;
Gilles Peskine449bd832023-01-11 14:50:10 +01003020 memcpy(dtls_srtp_info->mki_value, ssl->dtls_srtp_info.mki_value,
3021 ssl->dtls_srtp_info.mki_len);
Johan Pascal2258a4f2020-10-28 13:53:09 +01003022 }
Johan Pascalb62bb512015-12-03 21:56:45 +01003023}
Johan Pascalb62bb512015-12-03 21:56:45 +01003024#endif /* MBEDTLS_SSL_DTLS_SRTP */
3025
Aditya Patwardhan3096f332022-07-26 14:31:46 +05303026#if !defined(MBEDTLS_DEPRECATED_REMOVED)
Gilles Peskine449bd832023-01-11 14:50:10 +01003027void mbedtls_ssl_conf_max_version(mbedtls_ssl_config *conf, int major, int minor)
Paul Bakker490ecc82011-10-06 13:04:09 +00003028{
Glenn Strauss2dfcea22022-03-14 17:26:42 -04003029 conf->max_tls_version = (major << 8) | minor;
Paul Bakker490ecc82011-10-06 13:04:09 +00003030}
3031
Gilles Peskine449bd832023-01-11 14:50:10 +01003032void mbedtls_ssl_conf_min_version(mbedtls_ssl_config *conf, int major, int minor)
Paul Bakker1d29fb52012-09-28 13:28:45 +00003033{
Glenn Strauss2dfcea22022-03-14 17:26:42 -04003034 conf->min_tls_version = (major << 8) | minor;
Paul Bakker1d29fb52012-09-28 13:28:45 +00003035}
Aditya Patwardhan3096f332022-07-26 14:31:46 +05303036#endif /* MBEDTLS_DEPRECATED_REMOVED */
Paul Bakker1d29fb52012-09-28 13:28:45 +00003037
Janos Follath088ce432017-04-10 12:42:31 +01003038#if defined(MBEDTLS_SSL_SRV_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01003039void mbedtls_ssl_conf_cert_req_ca_list(mbedtls_ssl_config *conf,
3040 char cert_req_ca_list)
Janos Follath088ce432017-04-10 12:42:31 +01003041{
3042 conf->cert_req_ca_list = cert_req_ca_list;
3043}
3044#endif
3045
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003046#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
Gilles Peskine449bd832023-01-11 14:50:10 +01003047void mbedtls_ssl_conf_encrypt_then_mac(mbedtls_ssl_config *conf, char etm)
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +01003048{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02003049 conf->encrypt_then_mac = etm;
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +01003050}
3051#endif
3052
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003053#if defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET)
Gilles Peskine449bd832023-01-11 14:50:10 +01003054void mbedtls_ssl_conf_extended_master_secret(mbedtls_ssl_config *conf, char ems)
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +02003055{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02003056 conf->extended_ms = ems;
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +02003057}
3058#endif
3059
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003060#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
Gilles Peskine449bd832023-01-11 14:50:10 +01003061int mbedtls_ssl_conf_max_frag_len(mbedtls_ssl_config *conf, unsigned char mfl_code)
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02003062{
Gilles Peskine449bd832023-01-11 14:50:10 +01003063 if (mfl_code >= MBEDTLS_SSL_MAX_FRAG_LEN_INVALID ||
3064 ssl_mfl_code_to_length(mfl_code) > MBEDTLS_TLS_EXT_ADV_CONTENT_LEN) {
3065 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02003066 }
3067
Manuel Pégourié-Gonnard6bf89d62015-05-05 17:01:57 +01003068 conf->mfl_code = mfl_code;
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02003069
Gilles Peskine449bd832023-01-11 14:50:10 +01003070 return 0;
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02003071}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003072#endif /* MBEDTLS_SSL_MAX_FRAGMENT_LENGTH */
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02003073
Gilles Peskine449bd832023-01-11 14:50:10 +01003074void mbedtls_ssl_conf_legacy_renegotiation(mbedtls_ssl_config *conf, int allow_legacy)
Paul Bakker48916f92012-09-16 19:57:18 +00003075{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02003076 conf->allow_legacy_renegotiation = allow_legacy;
Paul Bakker48916f92012-09-16 19:57:18 +00003077}
3078
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003079#if defined(MBEDTLS_SSL_RENEGOTIATION)
Gilles Peskine449bd832023-01-11 14:50:10 +01003080void mbedtls_ssl_conf_renegotiation(mbedtls_ssl_config *conf, int renegotiation)
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01003081{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02003082 conf->disable_renegotiation = renegotiation;
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01003083}
3084
Gilles Peskine449bd832023-01-11 14:50:10 +01003085void mbedtls_ssl_conf_renegotiation_enforced(mbedtls_ssl_config *conf, int max_records)
Manuel Pégourié-Gonnarda9964db2014-07-03 19:29:16 +02003086{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02003087 conf->renego_max_records = max_records;
Manuel Pégourié-Gonnarda9964db2014-07-03 19:29:16 +02003088}
3089
Gilles Peskine449bd832023-01-11 14:50:10 +01003090void mbedtls_ssl_conf_renegotiation_period(mbedtls_ssl_config *conf,
3091 const unsigned char period[8])
Manuel Pégourié-Gonnard837f0fe2014-11-05 13:58:53 +01003092{
Gilles Peskine449bd832023-01-11 14:50:10 +01003093 memcpy(conf->renego_period, period, 8);
Manuel Pégourié-Gonnard837f0fe2014-11-05 13:58:53 +01003094}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003095#endif /* MBEDTLS_SSL_RENEGOTIATION */
Paul Bakker5121ce52009-01-03 21:22:43 +00003096
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003097#if defined(MBEDTLS_SSL_SESSION_TICKETS)
Manuel Pégourié-Gonnardb596abf2015-05-20 10:45:29 +02003098#if defined(MBEDTLS_SSL_CLI_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01003099void mbedtls_ssl_conf_session_tickets(mbedtls_ssl_config *conf, int use_tickets)
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +02003100{
Manuel Pégourié-Gonnard2b494452015-05-06 10:05:11 +01003101 conf->session_tickets = use_tickets;
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +02003102}
Manuel Pégourié-Gonnardb596abf2015-05-20 10:45:29 +02003103#endif
Paul Bakker606b4ba2013-08-14 16:52:14 +02003104
Manuel Pégourié-Gonnardb596abf2015-05-20 10:45:29 +02003105#if defined(MBEDTLS_SSL_SRV_C)
Jerry Yu1ad7ace2022-08-09 13:28:39 +08003106
Jerry Yud0766ec2022-09-22 10:46:57 +08003107#if defined(MBEDTLS_SSL_PROTO_TLS1_3) && defined(MBEDTLS_SSL_SESSION_TICKETS)
Gilles Peskine449bd832023-01-11 14:50:10 +01003108void mbedtls_ssl_conf_new_session_tickets(mbedtls_ssl_config *conf,
3109 uint16_t num_tickets)
Jerry Yu1ad7ace2022-08-09 13:28:39 +08003110{
Jerry Yud0766ec2022-09-22 10:46:57 +08003111 conf->new_session_tickets_count = num_tickets;
Jerry Yu1ad7ace2022-08-09 13:28:39 +08003112}
3113#endif
3114
Gilles Peskine449bd832023-01-11 14:50:10 +01003115void mbedtls_ssl_conf_session_tickets_cb(mbedtls_ssl_config *conf,
3116 mbedtls_ssl_ticket_write_t *f_ticket_write,
3117 mbedtls_ssl_ticket_parse_t *f_ticket_parse,
3118 void *p_ticket)
Paul Bakker606b4ba2013-08-14 16:52:14 +02003119{
Manuel Pégourié-Gonnardd59675d2015-05-19 15:28:00 +02003120 conf->f_ticket_write = f_ticket_write;
3121 conf->f_ticket_parse = f_ticket_parse;
3122 conf->p_ticket = p_ticket;
Paul Bakker606b4ba2013-08-14 16:52:14 +02003123}
Manuel Pégourié-Gonnardb596abf2015-05-20 10:45:29 +02003124#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003125#endif /* MBEDTLS_SSL_SESSION_TICKETS */
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +02003126
Gilles Peskine449bd832023-01-11 14:50:10 +01003127void mbedtls_ssl_set_export_keys_cb(mbedtls_ssl_context *ssl,
3128 mbedtls_ssl_export_keys_t *f_export_keys,
3129 void *p_export_keys)
Robert Cragie4feb7ae2015-10-02 13:33:37 +01003130{
Hanno Becker7e6c1782021-06-08 09:24:55 +01003131 ssl->f_export_keys = f_export_keys;
3132 ssl->p_export_keys = p_export_keys;
Ron Eldorf5cc10d2019-05-07 18:33:40 +03003133}
Robert Cragie4feb7ae2015-10-02 13:33:37 +01003134
Gilles Peskineb74a1c72018-04-24 13:09:22 +02003135#if defined(MBEDTLS_SSL_ASYNC_PRIVATE)
Gilles Peskine8bf79f62018-01-05 21:11:53 +01003136void mbedtls_ssl_conf_async_private_cb(
3137 mbedtls_ssl_config *conf,
3138 mbedtls_ssl_async_sign_t *f_async_sign,
3139 mbedtls_ssl_async_decrypt_t *f_async_decrypt,
3140 mbedtls_ssl_async_resume_t *f_async_resume,
3141 mbedtls_ssl_async_cancel_t *f_async_cancel,
Gilles Peskine449bd832023-01-11 14:50:10 +01003142 void *async_config_data)
Gilles Peskine8bf79f62018-01-05 21:11:53 +01003143{
3144 conf->f_async_sign_start = f_async_sign;
3145 conf->f_async_decrypt_start = f_async_decrypt;
3146 conf->f_async_resume = f_async_resume;
3147 conf->f_async_cancel = f_async_cancel;
Gilles Peskinedf13d5c2018-04-25 20:39:48 +02003148 conf->p_async_config_data = async_config_data;
3149}
3150
Gilles Peskine449bd832023-01-11 14:50:10 +01003151void *mbedtls_ssl_conf_get_async_config_data(const mbedtls_ssl_config *conf)
Gilles Peskine8f97af72018-04-26 11:46:10 +02003152{
Gilles Peskine449bd832023-01-11 14:50:10 +01003153 return conf->p_async_config_data;
Gilles Peskine8f97af72018-04-26 11:46:10 +02003154}
3155
Gilles Peskine449bd832023-01-11 14:50:10 +01003156void *mbedtls_ssl_get_async_operation_data(const mbedtls_ssl_context *ssl)
Gilles Peskinedf13d5c2018-04-25 20:39:48 +02003157{
Gilles Peskine449bd832023-01-11 14:50:10 +01003158 if (ssl->handshake == NULL) {
3159 return NULL;
3160 } else {
3161 return ssl->handshake->user_async_ctx;
3162 }
Gilles Peskinedf13d5c2018-04-25 20:39:48 +02003163}
3164
Gilles Peskine449bd832023-01-11 14:50:10 +01003165void mbedtls_ssl_set_async_operation_data(mbedtls_ssl_context *ssl,
3166 void *ctx)
Gilles Peskinedf13d5c2018-04-25 20:39:48 +02003167{
Gilles Peskine449bd832023-01-11 14:50:10 +01003168 if (ssl->handshake != NULL) {
Gilles Peskinedf13d5c2018-04-25 20:39:48 +02003169 ssl->handshake->user_async_ctx = ctx;
Gilles Peskine449bd832023-01-11 14:50:10 +01003170 }
Gilles Peskine8bf79f62018-01-05 21:11:53 +01003171}
Gilles Peskineb74a1c72018-04-24 13:09:22 +02003172#endif /* MBEDTLS_SSL_ASYNC_PRIVATE */
Gilles Peskine8bf79f62018-01-05 21:11:53 +01003173
Paul Bakker5121ce52009-01-03 21:22:43 +00003174/*
3175 * SSL get accessors
3176 */
Gilles Peskine449bd832023-01-11 14:50:10 +01003177uint32_t mbedtls_ssl_get_verify_result(const mbedtls_ssl_context *ssl)
Paul Bakker5121ce52009-01-03 21:22:43 +00003178{
Gilles Peskine449bd832023-01-11 14:50:10 +01003179 if (ssl->session != NULL) {
3180 return ssl->session->verify_result;
3181 }
Manuel Pégourié-Gonnarde89163c2015-01-23 14:30:57 +00003182
Gilles Peskine449bd832023-01-11 14:50:10 +01003183 if (ssl->session_negotiate != NULL) {
3184 return ssl->session_negotiate->verify_result;
3185 }
Manuel Pégourié-Gonnarde89163c2015-01-23 14:30:57 +00003186
Gilles Peskine449bd832023-01-11 14:50:10 +01003187 return 0xFFFFFFFF;
Paul Bakker5121ce52009-01-03 21:22:43 +00003188}
3189
Gilles Peskine449bd832023-01-11 14:50:10 +01003190int mbedtls_ssl_get_ciphersuite_id_from_ssl(const mbedtls_ssl_context *ssl)
Glenn Strauss8f526902022-01-13 00:04:49 -05003191{
Gilles Peskine449bd832023-01-11 14:50:10 +01003192 if (ssl == NULL || ssl->session == NULL) {
3193 return 0;
3194 }
Glenn Strauss8f526902022-01-13 00:04:49 -05003195
Gilles Peskine449bd832023-01-11 14:50:10 +01003196 return ssl->session->ciphersuite;
Glenn Strauss8f526902022-01-13 00:04:49 -05003197}
3198
Gilles Peskine449bd832023-01-11 14:50:10 +01003199const char *mbedtls_ssl_get_ciphersuite(const mbedtls_ssl_context *ssl)
Paul Bakker72f62662011-01-16 21:27:44 +00003200{
Gilles Peskine449bd832023-01-11 14:50:10 +01003201 if (ssl == NULL || ssl->session == NULL) {
3202 return NULL;
3203 }
Paul Bakker926c8e42013-03-06 10:23:34 +01003204
Gilles Peskine449bd832023-01-11 14:50:10 +01003205 return mbedtls_ssl_get_ciphersuite_name(ssl->session->ciphersuite);
Paul Bakker72f62662011-01-16 21:27:44 +00003206}
3207
Gilles Peskine449bd832023-01-11 14:50:10 +01003208const char *mbedtls_ssl_get_version(const mbedtls_ssl_context *ssl)
Paul Bakker43ca69c2011-01-15 17:35:19 +00003209{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003210#if defined(MBEDTLS_SSL_PROTO_DTLS)
Gilles Peskine449bd832023-01-11 14:50:10 +01003211 if (ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM) {
3212 switch (ssl->tls_version) {
Glenn Strauss60bfe602022-03-14 19:04:24 -04003213 case MBEDTLS_SSL_VERSION_TLS1_2:
Gilles Peskine449bd832023-01-11 14:50:10 +01003214 return "DTLSv1.2";
Manuel Pégourié-Gonnardb21ca2a2014-02-10 13:43:33 +01003215 default:
Gilles Peskine449bd832023-01-11 14:50:10 +01003216 return "unknown (DTLS)";
Manuel Pégourié-Gonnardb21ca2a2014-02-10 13:43:33 +01003217 }
3218 }
3219#endif
3220
Gilles Peskine449bd832023-01-11 14:50:10 +01003221 switch (ssl->tls_version) {
Glenn Strauss60bfe602022-03-14 19:04:24 -04003222 case MBEDTLS_SSL_VERSION_TLS1_2:
Gilles Peskine449bd832023-01-11 14:50:10 +01003223 return "TLSv1.2";
Glenn Strauss60bfe602022-03-14 19:04:24 -04003224 case MBEDTLS_SSL_VERSION_TLS1_3:
Gilles Peskine449bd832023-01-11 14:50:10 +01003225 return "TLSv1.3";
Paul Bakker43ca69c2011-01-15 17:35:19 +00003226 default:
Gilles Peskine449bd832023-01-11 14:50:10 +01003227 return "unknown";
Paul Bakker43ca69c2011-01-15 17:35:19 +00003228 }
Paul Bakker43ca69c2011-01-15 17:35:19 +00003229}
3230
Manuel Pégourié-Gonnarda2cda6b2015-08-31 18:30:52 +02003231#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
Gilles Peskine449bd832023-01-11 14:50:10 +01003232size_t mbedtls_ssl_get_input_max_frag_len(const mbedtls_ssl_context *ssl)
Andrzej Kurek90c6e842020-04-03 05:25:29 -04003233{
David Horstmann95d516f2021-05-04 18:36:56 +01003234 size_t max_len = MBEDTLS_SSL_IN_CONTENT_LEN;
Andrzej Kurek90c6e842020-04-03 05:25:29 -04003235 size_t read_mfl;
3236
Jerry Yuddda0502022-12-01 19:43:12 +08003237#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
Andrzej Kurek90c6e842020-04-03 05:25:29 -04003238 /* Use the configured MFL for the client if we're past SERVER_HELLO_DONE */
Gilles Peskine449bd832023-01-11 14:50:10 +01003239 if (ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT &&
3240 ssl->state >= MBEDTLS_SSL_SERVER_HELLO_DONE) {
3241 return ssl_mfl_code_to_length(ssl->conf->mfl_code);
Andrzej Kurek90c6e842020-04-03 05:25:29 -04003242 }
Jerry Yuddda0502022-12-01 19:43:12 +08003243#endif
Andrzej Kurek90c6e842020-04-03 05:25:29 -04003244
3245 /* Check if a smaller max length was negotiated */
Gilles Peskine449bd832023-01-11 14:50:10 +01003246 if (ssl->session_out != NULL) {
3247 read_mfl = ssl_mfl_code_to_length(ssl->session_out->mfl_code);
3248 if (read_mfl < max_len) {
Andrzej Kurek90c6e842020-04-03 05:25:29 -04003249 max_len = read_mfl;
3250 }
3251 }
3252
Jerry Yuddda0502022-12-01 19:43:12 +08003253 /* During a handshake, use the value being negotiated */
Gilles Peskine449bd832023-01-11 14:50:10 +01003254 if (ssl->session_negotiate != NULL) {
3255 read_mfl = ssl_mfl_code_to_length(ssl->session_negotiate->mfl_code);
3256 if (read_mfl < max_len) {
Andrzej Kurek90c6e842020-04-03 05:25:29 -04003257 max_len = read_mfl;
3258 }
3259 }
3260
Gilles Peskine449bd832023-01-11 14:50:10 +01003261 return max_len;
Andrzej Kurek90c6e842020-04-03 05:25:29 -04003262}
3263
Gilles Peskine449bd832023-01-11 14:50:10 +01003264size_t mbedtls_ssl_get_output_max_frag_len(const mbedtls_ssl_context *ssl)
Manuel Pégourié-Gonnarda2cda6b2015-08-31 18:30:52 +02003265{
3266 size_t max_len;
3267
3268 /*
3269 * Assume mfl_code is correct since it was checked when set
3270 */
Gilles Peskine449bd832023-01-11 14:50:10 +01003271 max_len = ssl_mfl_code_to_length(ssl->conf->mfl_code);
Manuel Pégourié-Gonnarda2cda6b2015-08-31 18:30:52 +02003272
Manuel Pégourié-Gonnard2cb17e22017-09-19 13:00:47 +02003273 /* Check if a smaller max length was negotiated */
Gilles Peskine449bd832023-01-11 14:50:10 +01003274 if (ssl->session_out != NULL &&
3275 ssl_mfl_code_to_length(ssl->session_out->mfl_code) < max_len) {
3276 max_len = ssl_mfl_code_to_length(ssl->session_out->mfl_code);
Manuel Pégourié-Gonnarda2cda6b2015-08-31 18:30:52 +02003277 }
3278
Manuel Pégourié-Gonnard2cb17e22017-09-19 13:00:47 +02003279 /* During a handshake, use the value being negotiated */
Gilles Peskine449bd832023-01-11 14:50:10 +01003280 if (ssl->session_negotiate != NULL &&
3281 ssl_mfl_code_to_length(ssl->session_negotiate->mfl_code) < max_len) {
3282 max_len = ssl_mfl_code_to_length(ssl->session_negotiate->mfl_code);
Manuel Pégourié-Gonnard2cb17e22017-09-19 13:00:47 +02003283 }
3284
Gilles Peskine449bd832023-01-11 14:50:10 +01003285 return max_len;
Manuel Pégourié-Gonnarda2cda6b2015-08-31 18:30:52 +02003286}
3287#endif /* MBEDTLS_SSL_MAX_FRAGMENT_LENGTH */
3288
Manuel Pégourié-Gonnardb8eec192018-08-20 09:34:02 +02003289#if defined(MBEDTLS_SSL_PROTO_DTLS)
Gilles Peskine449bd832023-01-11 14:50:10 +01003290size_t mbedtls_ssl_get_current_mtu(const mbedtls_ssl_context *ssl)
Manuel Pégourié-Gonnardb8eec192018-08-20 09:34:02 +02003291{
Andrzej Kurekef43ce62018-10-09 08:24:12 -04003292 /* Return unlimited mtu for client hello messages to avoid fragmentation. */
Gilles Peskine449bd832023-01-11 14:50:10 +01003293 if (ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT &&
3294 (ssl->state == MBEDTLS_SSL_CLIENT_HELLO ||
3295 ssl->state == MBEDTLS_SSL_SERVER_HELLO)) {
3296 return 0;
3297 }
Andrzej Kurekef43ce62018-10-09 08:24:12 -04003298
Gilles Peskine449bd832023-01-11 14:50:10 +01003299 if (ssl->handshake == NULL || ssl->handshake->mtu == 0) {
3300 return ssl->mtu;
3301 }
Manuel Pégourié-Gonnardb8eec192018-08-20 09:34:02 +02003302
Gilles Peskine449bd832023-01-11 14:50:10 +01003303 if (ssl->mtu == 0) {
3304 return ssl->handshake->mtu;
3305 }
Manuel Pégourié-Gonnardb8eec192018-08-20 09:34:02 +02003306
Gilles Peskine449bd832023-01-11 14:50:10 +01003307 return ssl->mtu < ssl->handshake->mtu ?
3308 ssl->mtu : ssl->handshake->mtu;
Manuel Pégourié-Gonnardb8eec192018-08-20 09:34:02 +02003309}
3310#endif /* MBEDTLS_SSL_PROTO_DTLS */
3311
Gilles Peskine449bd832023-01-11 14:50:10 +01003312int mbedtls_ssl_get_max_out_record_payload(const mbedtls_ssl_context *ssl)
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02003313{
3314 size_t max_len = MBEDTLS_SSL_OUT_CONTENT_LEN;
3315
Manuel Pégourié-Gonnard000281e2018-08-21 11:20:58 +02003316#if !defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH) && \
3317 !defined(MBEDTLS_SSL_PROTO_DTLS)
3318 (void) ssl;
3319#endif
3320
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02003321#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
Gilles Peskine449bd832023-01-11 14:50:10 +01003322 const size_t mfl = mbedtls_ssl_get_output_max_frag_len(ssl);
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02003323
Gilles Peskine449bd832023-01-11 14:50:10 +01003324 if (max_len > mfl) {
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02003325 max_len = mfl;
Gilles Peskine449bd832023-01-11 14:50:10 +01003326 }
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02003327#endif
3328
3329#if defined(MBEDTLS_SSL_PROTO_DTLS)
Gilles Peskine449bd832023-01-11 14:50:10 +01003330 if (mbedtls_ssl_get_current_mtu(ssl) != 0) {
3331 const size_t mtu = mbedtls_ssl_get_current_mtu(ssl);
3332 const int ret = mbedtls_ssl_get_record_expansion(ssl);
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02003333 const size_t overhead = (size_t) ret;
3334
Gilles Peskine449bd832023-01-11 14:50:10 +01003335 if (ret < 0) {
3336 return ret;
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02003337 }
3338
Gilles Peskine449bd832023-01-11 14:50:10 +01003339 if (mtu <= overhead) {
3340 MBEDTLS_SSL_DEBUG_MSG(1, ("MTU too low for record expansion"));
3341 return MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
3342 }
3343
3344 if (max_len > mtu - overhead) {
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02003345 max_len = mtu - overhead;
Gilles Peskine449bd832023-01-11 14:50:10 +01003346 }
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02003347 }
Manuel Pégourié-Gonnardb8eec192018-08-20 09:34:02 +02003348#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02003349
Hanno Becker0defedb2018-08-10 12:35:02 +01003350#if !defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH) && \
3351 !defined(MBEDTLS_SSL_PROTO_DTLS)
3352 ((void) ssl);
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02003353#endif
3354
Gilles Peskine449bd832023-01-11 14:50:10 +01003355 return (int) max_len;
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02003356}
3357
Gilles Peskine449bd832023-01-11 14:50:10 +01003358int mbedtls_ssl_get_max_in_record_payload(const mbedtls_ssl_context *ssl)
Hanno Becker2d8e99b2021-04-21 06:19:50 +01003359{
3360 size_t max_len = MBEDTLS_SSL_IN_CONTENT_LEN;
3361
3362#if !defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
3363 (void) ssl;
3364#endif
3365
3366#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
Gilles Peskine449bd832023-01-11 14:50:10 +01003367 const size_t mfl = mbedtls_ssl_get_input_max_frag_len(ssl);
Hanno Becker2d8e99b2021-04-21 06:19:50 +01003368
Gilles Peskine449bd832023-01-11 14:50:10 +01003369 if (max_len > mfl) {
Hanno Becker2d8e99b2021-04-21 06:19:50 +01003370 max_len = mfl;
Gilles Peskine449bd832023-01-11 14:50:10 +01003371 }
Hanno Becker2d8e99b2021-04-21 06:19:50 +01003372#endif
3373
Gilles Peskine449bd832023-01-11 14:50:10 +01003374 return (int) max_len;
Hanno Becker2d8e99b2021-04-21 06:19:50 +01003375}
3376
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003377#if defined(MBEDTLS_X509_CRT_PARSE_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01003378const mbedtls_x509_crt *mbedtls_ssl_get_peer_cert(const mbedtls_ssl_context *ssl)
Paul Bakkerb0550d92012-10-30 07:51:03 +00003379{
Gilles Peskine449bd832023-01-11 14:50:10 +01003380 if (ssl == NULL || ssl->session == NULL) {
3381 return NULL;
3382 }
Paul Bakkerb0550d92012-10-30 07:51:03 +00003383
Hanno Beckere6824572019-02-07 13:18:46 +00003384#if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
Gilles Peskine449bd832023-01-11 14:50:10 +01003385 return ssl->session->peer_cert;
Hanno Beckere6824572019-02-07 13:18:46 +00003386#else
Gilles Peskine449bd832023-01-11 14:50:10 +01003387 return NULL;
Hanno Beckere6824572019-02-07 13:18:46 +00003388#endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
Paul Bakkerb0550d92012-10-30 07:51:03 +00003389}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003390#endif /* MBEDTLS_X509_CRT_PARSE_C */
Paul Bakkerb0550d92012-10-30 07:51:03 +00003391
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003392#if defined(MBEDTLS_SSL_CLI_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01003393int mbedtls_ssl_get_session(const mbedtls_ssl_context *ssl,
3394 mbedtls_ssl_session *dst)
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02003395{
Hanno Beckere810bbc2021-05-14 16:01:05 +01003396 int ret;
3397
Gilles Peskine449bd832023-01-11 14:50:10 +01003398 if (ssl == NULL ||
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02003399 dst == NULL ||
3400 ssl->session == NULL ||
Gilles Peskine449bd832023-01-11 14:50:10 +01003401 ssl->conf->endpoint != MBEDTLS_SSL_IS_CLIENT) {
3402 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02003403 }
3404
Hanno Beckere810bbc2021-05-14 16:01:05 +01003405 /* Since Mbed TLS 3.0, mbedtls_ssl_get_session() is no longer
3406 * idempotent: Each session can only be exported once.
3407 *
3408 * (This is in preparation for TLS 1.3 support where we will
3409 * need the ability to export multiple sessions (aka tickets),
3410 * which will be achieved by calling mbedtls_ssl_get_session()
3411 * multiple times until it fails.)
3412 *
3413 * Check whether we have already exported the current session,
3414 * and fail if so.
3415 */
Gilles Peskine449bd832023-01-11 14:50:10 +01003416 if (ssl->session->exported == 1) {
3417 return MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
3418 }
Hanno Beckere810bbc2021-05-14 16:01:05 +01003419
Gilles Peskine449bd832023-01-11 14:50:10 +01003420 ret = mbedtls_ssl_session_copy(dst, ssl->session);
3421 if (ret != 0) {
3422 return ret;
3423 }
Hanno Beckere810bbc2021-05-14 16:01:05 +01003424
3425 /* Remember that we've exported the session. */
3426 ssl->session->exported = 1;
Gilles Peskine449bd832023-01-11 14:50:10 +01003427 return 0;
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02003428}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003429#endif /* MBEDTLS_SSL_CLI_C */
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02003430
Paul Bakker5121ce52009-01-03 21:22:43 +00003431/*
Hanno Beckera835da52019-05-16 12:39:07 +01003432 * Define ticket header determining Mbed TLS version
3433 * and structure of the ticket.
3434 */
3435
Hanno Becker94ef3b32019-05-16 12:50:45 +01003436/*
Hanno Becker50b59662019-05-28 14:30:45 +01003437 * Define bitflag determining compile-time settings influencing
3438 * structure of serialized SSL sessions.
Hanno Becker94ef3b32019-05-16 12:50:45 +01003439 */
3440
Hanno Becker50b59662019-05-28 14:30:45 +01003441#if defined(MBEDTLS_HAVE_TIME)
Hanno Becker3e088662019-05-29 11:10:18 +01003442#define SSL_SERIALIZED_SESSION_CONFIG_TIME 1
Hanno Becker50b59662019-05-28 14:30:45 +01003443#else
Hanno Becker3e088662019-05-29 11:10:18 +01003444#define SSL_SERIALIZED_SESSION_CONFIG_TIME 0
Hanno Becker94ef3b32019-05-16 12:50:45 +01003445#endif /* MBEDTLS_HAVE_TIME */
3446
3447#if defined(MBEDTLS_X509_CRT_PARSE_C)
Hanno Becker3e088662019-05-29 11:10:18 +01003448#define SSL_SERIALIZED_SESSION_CONFIG_CRT 1
Hanno Becker94ef3b32019-05-16 12:50:45 +01003449#else
Hanno Becker3e088662019-05-29 11:10:18 +01003450#define SSL_SERIALIZED_SESSION_CONFIG_CRT 0
Hanno Becker94ef3b32019-05-16 12:50:45 +01003451#endif /* MBEDTLS_X509_CRT_PARSE_C */
3452
3453#if defined(MBEDTLS_SSL_CLI_C) && defined(MBEDTLS_SSL_SESSION_TICKETS)
Hanno Becker3e088662019-05-29 11:10:18 +01003454#define SSL_SERIALIZED_SESSION_CONFIG_CLIENT_TICKET 1
Hanno Becker94ef3b32019-05-16 12:50:45 +01003455#else
Hanno Becker3e088662019-05-29 11:10:18 +01003456#define SSL_SERIALIZED_SESSION_CONFIG_CLIENT_TICKET 0
Hanno Becker94ef3b32019-05-16 12:50:45 +01003457#endif /* MBEDTLS_SSL_CLI_C && MBEDTLS_SSL_SESSION_TICKETS */
3458
3459#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
Hanno Becker3e088662019-05-29 11:10:18 +01003460#define SSL_SERIALIZED_SESSION_CONFIG_MFL 1
Hanno Becker94ef3b32019-05-16 12:50:45 +01003461#else
Hanno Becker3e088662019-05-29 11:10:18 +01003462#define SSL_SERIALIZED_SESSION_CONFIG_MFL 0
Hanno Becker94ef3b32019-05-16 12:50:45 +01003463#endif /* MBEDTLS_SSL_MAX_FRAGMENT_LENGTH */
3464
Hanno Becker94ef3b32019-05-16 12:50:45 +01003465#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
Hanno Becker3e088662019-05-29 11:10:18 +01003466#define SSL_SERIALIZED_SESSION_CONFIG_ETM 1
Hanno Becker94ef3b32019-05-16 12:50:45 +01003467#else
Hanno Becker3e088662019-05-29 11:10:18 +01003468#define SSL_SERIALIZED_SESSION_CONFIG_ETM 0
Hanno Becker94ef3b32019-05-16 12:50:45 +01003469#endif /* MBEDTLS_SSL_ENCRYPT_THEN_MAC */
3470
Hanno Becker94ef3b32019-05-16 12:50:45 +01003471#if defined(MBEDTLS_SSL_SESSION_TICKETS)
3472#define SSL_SERIALIZED_SESSION_CONFIG_TICKET 1
3473#else
3474#define SSL_SERIALIZED_SESSION_CONFIG_TICKET 0
3475#endif /* MBEDTLS_SSL_SESSION_TICKETS */
3476
Hanno Becker3e088662019-05-29 11:10:18 +01003477#define SSL_SERIALIZED_SESSION_CONFIG_TIME_BIT 0
3478#define SSL_SERIALIZED_SESSION_CONFIG_CRT_BIT 1
3479#define SSL_SERIALIZED_SESSION_CONFIG_CLIENT_TICKET_BIT 2
3480#define SSL_SERIALIZED_SESSION_CONFIG_MFL_BIT 3
Hanno Becker37bdbe62021-08-01 05:38:58 +01003481#define SSL_SERIALIZED_SESSION_CONFIG_ETM_BIT 4
3482#define SSL_SERIALIZED_SESSION_CONFIG_TICKET_BIT 5
Hanno Becker3e088662019-05-29 11:10:18 +01003483
Hanno Becker50b59662019-05-28 14:30:45 +01003484#define SSL_SERIALIZED_SESSION_CONFIG_BITFLAG \
Gilles Peskine449bd832023-01-11 14:50:10 +01003485 ((uint16_t) ( \
3486 (SSL_SERIALIZED_SESSION_CONFIG_TIME << SSL_SERIALIZED_SESSION_CONFIG_TIME_BIT) | \
3487 (SSL_SERIALIZED_SESSION_CONFIG_CRT << SSL_SERIALIZED_SESSION_CONFIG_CRT_BIT) | \
3488 (SSL_SERIALIZED_SESSION_CONFIG_CLIENT_TICKET << \
3489 SSL_SERIALIZED_SESSION_CONFIG_CLIENT_TICKET_BIT) | \
3490 (SSL_SERIALIZED_SESSION_CONFIG_MFL << SSL_SERIALIZED_SESSION_CONFIG_MFL_BIT) | \
3491 (SSL_SERIALIZED_SESSION_CONFIG_ETM << SSL_SERIALIZED_SESSION_CONFIG_ETM_BIT) | \
3492 (SSL_SERIALIZED_SESSION_CONFIG_TICKET << SSL_SERIALIZED_SESSION_CONFIG_TICKET_BIT)))
Hanno Becker94ef3b32019-05-16 12:50:45 +01003493
Hanno Beckerf8787072019-05-16 12:41:07 +01003494static unsigned char ssl_serialized_session_header[] = {
Hanno Becker94ef3b32019-05-16 12:50:45 +01003495 MBEDTLS_VERSION_MAJOR,
3496 MBEDTLS_VERSION_MINOR,
3497 MBEDTLS_VERSION_PATCH,
Gilles Peskine449bd832023-01-11 14:50:10 +01003498 MBEDTLS_BYTE_1(SSL_SERIALIZED_SESSION_CONFIG_BITFLAG),
3499 MBEDTLS_BYTE_0(SSL_SERIALIZED_SESSION_CONFIG_BITFLAG),
Hanno Beckerf8787072019-05-16 12:41:07 +01003500};
Hanno Beckera835da52019-05-16 12:39:07 +01003501
3502/*
Manuel Pégourié-Gonnarda3e7c652019-05-16 10:08:35 +02003503 * Serialize a session in the following format:
Manuel Pégourié-Gonnard35eb8022019-05-16 11:11:08 +02003504 * (in the presentation language of TLS, RFC 8446 section 3)
Manuel Pégourié-Gonnarda3e7c652019-05-16 10:08:35 +02003505 *
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01003506 * struct {
Hanno Beckerdc28b6c2019-05-29 11:08:00 +01003507 *
Hanno Beckerdce50972021-08-01 05:39:23 +01003508 * opaque mbedtls_version[3]; // library version: major, minor, patch
3509 * opaque session_format[2]; // library-version specific 16-bit field
3510 * // determining the format of the remaining
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01003511 * // serialized data.
Hanno Beckerdc28b6c2019-05-29 11:08:00 +01003512 *
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01003513 * Note: When updating the format, remember to keep
3514 * these version+format bytes.
Manuel Pégourié-Gonnarda3e7c652019-05-16 10:08:35 +02003515 *
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01003516 * // In this version, `session_format` determines
3517 * // the setting of those compile-time
3518 * // configuration options which influence
3519 * // the structure of mbedtls_ssl_session.
3520 *
Glenn Straussda7851c2022-03-14 13:29:48 -04003521 * uint8_t minor_ver; // Protocol minor version. Possible values:
Jerry Yu0c2a7382022-12-06 13:27:25 +08003522 * // - TLS 1.2 (0x0303)
3523 * // - TLS 1.3 (0x0304)
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01003524 *
Glenn Straussda7851c2022-03-14 13:29:48 -04003525 * select (serialized_session.tls_version) {
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01003526 *
Glenn Straussda7851c2022-03-14 13:29:48 -04003527 * case MBEDTLS_SSL_VERSION_TLS1_2:
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01003528 * serialized_session_tls12 data;
Jerry Yu0c2a7382022-12-06 13:27:25 +08003529 * case MBEDTLS_SSL_VERSION_TLS1_3:
Jerry Yuddda0502022-12-01 19:43:12 +08003530 * serialized_session_tls13 data;
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01003531 *
3532 * };
3533 *
3534 * } serialized_session;
3535 *
Manuel Pégourié-Gonnarda3e7c652019-05-16 10:08:35 +02003536 */
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01003537
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02003538MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01003539static int ssl_session_save(const mbedtls_ssl_session *session,
3540 unsigned char omit_header,
3541 unsigned char *buf,
3542 size_t buf_len,
3543 size_t *olen)
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01003544{
3545 unsigned char *p = buf;
3546 size_t used = 0;
Jerry Yu251a12e2022-07-13 15:15:48 +08003547 size_t remaining_len;
Jerry Yue36fdd62022-08-17 21:31:36 +08003548#if defined(MBEDTLS_SSL_PROTO_TLS1_3)
3549 size_t out_len;
3550 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
3551#endif
Gilles Peskine449bd832023-01-11 14:50:10 +01003552 if (session == NULL) {
3553 return MBEDTLS_ERR_SSL_INTERNAL_ERROR;
3554 }
Jerry Yu438ddd82022-07-07 06:55:50 +00003555
Gilles Peskine449bd832023-01-11 14:50:10 +01003556 if (!omit_header) {
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01003557 /*
3558 * Add Mbed TLS version identifier
3559 */
Gilles Peskine449bd832023-01-11 14:50:10 +01003560 used += sizeof(ssl_serialized_session_header);
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01003561
Gilles Peskine449bd832023-01-11 14:50:10 +01003562 if (used <= buf_len) {
3563 memcpy(p, ssl_serialized_session_header,
3564 sizeof(ssl_serialized_session_header));
3565 p += sizeof(ssl_serialized_session_header);
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01003566 }
3567 }
3568
3569 /*
3570 * TLS version identifier
3571 */
3572 used += 1;
Gilles Peskine449bd832023-01-11 14:50:10 +01003573 if (used <= buf_len) {
3574 *p++ = MBEDTLS_BYTE_0(session->tls_version);
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01003575 }
3576
3577 /* Forward to version-specific serialization routine. */
Jerry Yufca4d572022-07-21 10:37:48 +08003578 remaining_len = (buf_len >= used) ? buf_len - used : 0;
Gilles Peskine449bd832023-01-11 14:50:10 +01003579 switch (session->tls_version) {
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01003580#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
Gilles Peskine449bd832023-01-11 14:50:10 +01003581 case MBEDTLS_SSL_VERSION_TLS1_2:
3582 used += ssl_tls12_session_save(session, p, remaining_len);
3583 break;
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01003584#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
3585
Jerry Yu251a12e2022-07-13 15:15:48 +08003586#if defined(MBEDTLS_SSL_PROTO_TLS1_3)
Gilles Peskine449bd832023-01-11 14:50:10 +01003587 case MBEDTLS_SSL_VERSION_TLS1_3:
3588 ret = ssl_tls13_session_save(session, p, remaining_len, &out_len);
3589 if (ret != 0 && ret != MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL) {
3590 return ret;
3591 }
3592 used += out_len;
3593 break;
Jerry Yu251a12e2022-07-13 15:15:48 +08003594#endif /* MBEDTLS_SSL_PROTO_TLS1_3 */
3595
Gilles Peskine449bd832023-01-11 14:50:10 +01003596 default:
3597 return MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01003598 }
3599
3600 *olen = used;
Gilles Peskine449bd832023-01-11 14:50:10 +01003601 if (used > buf_len) {
3602 return MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL;
3603 }
Manuel Pégourié-Gonnarda3e7c652019-05-16 10:08:35 +02003604
Gilles Peskine449bd832023-01-11 14:50:10 +01003605 return 0;
Manuel Pégourié-Gonnarda3e7c652019-05-16 10:08:35 +02003606}
3607
3608/*
Manuel Pégourié-Gonnard45ac1f02019-07-23 16:52:45 +02003609 * Public wrapper for ssl_session_save()
3610 */
Gilles Peskine449bd832023-01-11 14:50:10 +01003611int mbedtls_ssl_session_save(const mbedtls_ssl_session *session,
3612 unsigned char *buf,
3613 size_t buf_len,
3614 size_t *olen)
Manuel Pégourié-Gonnard45ac1f02019-07-23 16:52:45 +02003615{
Gilles Peskine449bd832023-01-11 14:50:10 +01003616 return ssl_session_save(session, 0, buf, buf_len, olen);
Manuel Pégourié-Gonnard45ac1f02019-07-23 16:52:45 +02003617}
Manuel Pégourié-Gonnarda3e7c652019-05-16 10:08:35 +02003618
Jerry Yuf1b23ca2022-02-18 11:48:47 +08003619/*
3620 * Deserialize session, see mbedtls_ssl_session_save() for format.
3621 *
3622 * This internal version is wrapped by a public function that cleans up in
3623 * case of error, and has an extra option omit_header.
3624 */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02003625MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01003626static int ssl_session_load(mbedtls_ssl_session *session,
3627 unsigned char omit_header,
3628 const unsigned char *buf,
3629 size_t len)
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01003630{
3631 const unsigned char *p = buf;
3632 const unsigned char * const end = buf + len;
Jerry Yu438ddd82022-07-07 06:55:50 +00003633 size_t remaining_len;
3634
3635
Gilles Peskine449bd832023-01-11 14:50:10 +01003636 if (session == NULL) {
3637 return MBEDTLS_ERR_SSL_INTERNAL_ERROR;
3638 }
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01003639
Gilles Peskine449bd832023-01-11 14:50:10 +01003640 if (!omit_header) {
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01003641 /*
3642 * Check Mbed TLS version identifier
3643 */
3644
Gilles Peskine449bd832023-01-11 14:50:10 +01003645 if ((size_t) (end - p) < sizeof(ssl_serialized_session_header)) {
3646 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01003647 }
Gilles Peskine449bd832023-01-11 14:50:10 +01003648
3649 if (memcmp(p, ssl_serialized_session_header,
3650 sizeof(ssl_serialized_session_header)) != 0) {
3651 return MBEDTLS_ERR_SSL_VERSION_MISMATCH;
3652 }
3653 p += sizeof(ssl_serialized_session_header);
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01003654 }
3655
3656 /*
3657 * TLS version identifier
3658 */
Gilles Peskine449bd832023-01-11 14:50:10 +01003659 if (1 > (size_t) (end - p)) {
3660 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
3661 }
Glenn Straussda7851c2022-03-14 13:29:48 -04003662 session->tls_version = 0x0300 | *p++;
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01003663
3664 /* Dispatch according to TLS version. */
Gilles Peskine449bd832023-01-11 14:50:10 +01003665 remaining_len = (end - p);
3666 switch (session->tls_version) {
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01003667#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
Gilles Peskine449bd832023-01-11 14:50:10 +01003668 case MBEDTLS_SSL_VERSION_TLS1_2:
3669 return ssl_tls12_session_load(session, p, remaining_len);
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01003670#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
3671
Jerry Yu438ddd82022-07-07 06:55:50 +00003672#if defined(MBEDTLS_SSL_PROTO_TLS1_3)
Gilles Peskine449bd832023-01-11 14:50:10 +01003673 case MBEDTLS_SSL_VERSION_TLS1_3:
3674 return ssl_tls13_session_load(session, p, remaining_len);
Jerry Yu438ddd82022-07-07 06:55:50 +00003675#endif /* MBEDTLS_SSL_PROTO_TLS1_3 */
3676
Gilles Peskine449bd832023-01-11 14:50:10 +01003677 default:
3678 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01003679 }
3680}
3681
Manuel Pégourié-Gonnarda3e7c652019-05-16 10:08:35 +02003682/*
Manuel Pégourié-Gonnardb9dfc9f2019-07-12 10:50:19 +02003683 * Deserialize session: public wrapper for error cleaning
Manuel Pégourié-Gonnarda3d831b2019-05-23 12:28:45 +02003684 */
Gilles Peskine449bd832023-01-11 14:50:10 +01003685int mbedtls_ssl_session_load(mbedtls_ssl_session *session,
3686 const unsigned char *buf,
3687 size_t len)
Manuel Pégourié-Gonnarda3d831b2019-05-23 12:28:45 +02003688{
Gilles Peskine449bd832023-01-11 14:50:10 +01003689 int ret = ssl_session_load(session, 0, buf, len);
Manuel Pégourié-Gonnarda3d831b2019-05-23 12:28:45 +02003690
Gilles Peskine449bd832023-01-11 14:50:10 +01003691 if (ret != 0) {
3692 mbedtls_ssl_session_free(session);
3693 }
Manuel Pégourié-Gonnarda3d831b2019-05-23 12:28:45 +02003694
Gilles Peskine449bd832023-01-11 14:50:10 +01003695 return ret;
Manuel Pégourié-Gonnarda3d831b2019-05-23 12:28:45 +02003696}
3697
3698/*
Paul Bakker1961b702013-01-25 14:49:24 +01003699 * Perform a single step of the SSL handshake
Paul Bakker5121ce52009-01-03 21:22:43 +00003700 */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02003701MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01003702static int ssl_prepare_handshake_step(mbedtls_ssl_context *ssl)
Hanno Becker41934dd2021-08-07 19:13:43 +01003703{
3704 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
3705
Ronald Cron66dbf912022-02-02 15:33:46 +01003706 /*
3707 * We may have not been able to send to the peer all the handshake data
Ronald Cron3f20b772022-03-08 16:00:02 +01003708 * that were written into the output buffer by the previous handshake step,
3709 * if the write to the network callback returned with the
Ronald Cron66dbf912022-02-02 15:33:46 +01003710 * #MBEDTLS_ERR_SSL_WANT_WRITE error code.
3711 * We proceed to the next handshake step only when all data from the
3712 * previous one have been sent to the peer, thus we make sure that this is
3713 * the case here by calling `mbedtls_ssl_flush_output()`. The function may
3714 * return with the #MBEDTLS_ERR_SSL_WANT_WRITE error code in which case
3715 * we have to wait before to go ahead.
3716 * In the case of TLS 1.3, handshake step handlers do not send data to the
3717 * peer. Data are only sent here and through
3718 * `mbedtls_ssl_handle_pending_alert` in case an error that triggered an
Andrzej Kurek5c65c572022-04-13 14:28:52 -04003719 * alert occurred.
Ronald Cron66dbf912022-02-02 15:33:46 +01003720 */
Gilles Peskine449bd832023-01-11 14:50:10 +01003721 if ((ret = mbedtls_ssl_flush_output(ssl)) != 0) {
3722 return ret;
3723 }
Hanno Becker41934dd2021-08-07 19:13:43 +01003724
3725#if defined(MBEDTLS_SSL_PROTO_DTLS)
Gilles Peskine449bd832023-01-11 14:50:10 +01003726 if (ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
3727 ssl->handshake->retransmit_state == MBEDTLS_SSL_RETRANS_SENDING) {
3728 if ((ret = mbedtls_ssl_flight_transmit(ssl)) != 0) {
3729 return ret;
3730 }
Hanno Becker41934dd2021-08-07 19:13:43 +01003731 }
3732#endif /* MBEDTLS_SSL_PROTO_DTLS */
3733
Gilles Peskine449bd832023-01-11 14:50:10 +01003734 return ret;
Hanno Becker41934dd2021-08-07 19:13:43 +01003735}
3736
Gilles Peskine449bd832023-01-11 14:50:10 +01003737int mbedtls_ssl_handshake_step(mbedtls_ssl_context *ssl)
Paul Bakker5121ce52009-01-03 21:22:43 +00003738{
Hanno Becker41934dd2021-08-07 19:13:43 +01003739 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker5121ce52009-01-03 21:22:43 +00003740
Gilles Peskine449bd832023-01-11 14:50:10 +01003741 if (ssl == NULL ||
Hanno Becker41934dd2021-08-07 19:13:43 +01003742 ssl->conf == NULL ||
3743 ssl->handshake == NULL ||
Gilles Peskine449bd832023-01-11 14:50:10 +01003744 ssl->state == MBEDTLS_SSL_HANDSHAKE_OVER) {
3745 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
Hanno Becker41934dd2021-08-07 19:13:43 +01003746 }
3747
Gilles Peskine449bd832023-01-11 14:50:10 +01003748 ret = ssl_prepare_handshake_step(ssl);
3749 if (ret != 0) {
3750 return ret;
3751 }
Manuel Pégourié-Gonnardf81ee2e2015-09-01 17:43:40 +02003752
Gilles Peskine449bd832023-01-11 14:50:10 +01003753 ret = mbedtls_ssl_handle_pending_alert(ssl);
3754 if (ret != 0) {
Jerry Yue7047812021-09-13 19:26:39 +08003755 goto cleanup;
Gilles Peskine449bd832023-01-11 14:50:10 +01003756 }
Jerry Yue7047812021-09-13 19:26:39 +08003757
Tom Cosgrove2fdc7b32022-09-21 12:33:17 +01003758 /* If ssl->conf->endpoint is not one of MBEDTLS_SSL_IS_CLIENT or
3759 * MBEDTLS_SSL_IS_SERVER, this is the return code we give */
3760 ret = MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
3761
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003762#if defined(MBEDTLS_SSL_CLI_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01003763 if (ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT) {
3764 MBEDTLS_SSL_DEBUG_MSG(2, ("client state: %s",
3765 mbedtls_ssl_states_str(ssl->state)));
Jerry Yub9930e72021-08-06 17:11:51 +08003766
Gilles Peskine449bd832023-01-11 14:50:10 +01003767 switch (ssl->state) {
Ronald Cron9f0fba32022-02-10 16:45:15 +01003768 case MBEDTLS_SSL_HELLO_REQUEST:
3769 ssl->state = MBEDTLS_SSL_CLIENT_HELLO;
Tom Cosgrove87d9c6c2022-09-22 09:27:56 +01003770 ret = 0;
Ronald Cron9f0fba32022-02-10 16:45:15 +01003771 break;
Jerry Yub9930e72021-08-06 17:11:51 +08003772
Ronald Cron9f0fba32022-02-10 16:45:15 +01003773 case MBEDTLS_SSL_CLIENT_HELLO:
Gilles Peskine449bd832023-01-11 14:50:10 +01003774 ret = mbedtls_ssl_write_client_hello(ssl);
Ronald Cron9f0fba32022-02-10 16:45:15 +01003775 break;
3776
3777 default:
3778#if defined(MBEDTLS_SSL_PROTO_TLS1_2) && defined(MBEDTLS_SSL_PROTO_TLS1_3)
Gilles Peskine449bd832023-01-11 14:50:10 +01003779 if (ssl->tls_version == MBEDTLS_SSL_VERSION_TLS1_3) {
3780 ret = mbedtls_ssl_tls13_handshake_client_step(ssl);
3781 } else {
3782 ret = mbedtls_ssl_handshake_client_step(ssl);
3783 }
Ronald Cron9f0fba32022-02-10 16:45:15 +01003784#elif defined(MBEDTLS_SSL_PROTO_TLS1_2)
Gilles Peskine449bd832023-01-11 14:50:10 +01003785 ret = mbedtls_ssl_handshake_client_step(ssl);
Ronald Cron9f0fba32022-02-10 16:45:15 +01003786#else
Gilles Peskine449bd832023-01-11 14:50:10 +01003787 ret = mbedtls_ssl_tls13_handshake_client_step(ssl);
Ronald Cron9f0fba32022-02-10 16:45:15 +01003788#endif
3789 }
Jerry Yub9930e72021-08-06 17:11:51 +08003790 }
Paul Bakker5121ce52009-01-03 21:22:43 +00003791#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003792#if defined(MBEDTLS_SSL_SRV_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01003793 if (ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER) {
Ronald Cron6f135e12021-12-08 16:57:54 +01003794#if defined(MBEDTLS_SSL_PROTO_TLS1_3)
Gilles Peskine449bd832023-01-11 14:50:10 +01003795 if (mbedtls_ssl_conf_is_tls13_only(ssl->conf)) {
3796 ret = mbedtls_ssl_tls13_handshake_server_step(ssl);
3797 }
Ronald Cron6f135e12021-12-08 16:57:54 +01003798#endif /* MBEDTLS_SSL_PROTO_TLS1_3 */
Jerry Yub9930e72021-08-06 17:11:51 +08003799
3800#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
Gilles Peskine449bd832023-01-11 14:50:10 +01003801 if (mbedtls_ssl_conf_is_tls12_only(ssl->conf)) {
3802 ret = mbedtls_ssl_handshake_server_step(ssl);
3803 }
Jerry Yub9930e72021-08-06 17:11:51 +08003804#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
3805 }
Paul Bakker5121ce52009-01-03 21:22:43 +00003806#endif
3807
Gilles Peskine449bd832023-01-11 14:50:10 +01003808 if (ret != 0) {
Jerry Yubbd5a3f2021-09-18 20:50:22 +08003809 /* handshake_step return error. And it is same
3810 * with alert_reason.
3811 */
Gilles Peskine449bd832023-01-11 14:50:10 +01003812 if (ssl->send_alert) {
3813 ret = mbedtls_ssl_handle_pending_alert(ssl);
Jerry Yue7047812021-09-13 19:26:39 +08003814 goto cleanup;
3815 }
3816 }
3817
3818cleanup:
Gilles Peskine449bd832023-01-11 14:50:10 +01003819 return ret;
Paul Bakker1961b702013-01-25 14:49:24 +01003820}
3821
3822/*
3823 * Perform the SSL handshake
3824 */
Gilles Peskine449bd832023-01-11 14:50:10 +01003825int mbedtls_ssl_handshake(mbedtls_ssl_context *ssl)
Paul Bakker1961b702013-01-25 14:49:24 +01003826{
3827 int ret = 0;
3828
Hanno Beckera817ea42020-10-20 15:20:23 +01003829 /* Sanity checks */
3830
Gilles Peskine449bd832023-01-11 14:50:10 +01003831 if (ssl == NULL || ssl->conf == NULL) {
3832 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
3833 }
Manuel Pégourié-Gonnardf81ee2e2015-09-01 17:43:40 +02003834
Hanno Beckera817ea42020-10-20 15:20:23 +01003835#if defined(MBEDTLS_SSL_PROTO_DTLS)
Gilles Peskine449bd832023-01-11 14:50:10 +01003836 if (ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
3837 (ssl->f_set_timer == NULL || ssl->f_get_timer == NULL)) {
3838 MBEDTLS_SSL_DEBUG_MSG(1, ("You must use "
3839 "mbedtls_ssl_set_timer_cb() for DTLS"));
3840 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
Hanno Beckera817ea42020-10-20 15:20:23 +01003841 }
3842#endif /* MBEDTLS_SSL_PROTO_DTLS */
3843
Gilles Peskine449bd832023-01-11 14:50:10 +01003844 MBEDTLS_SSL_DEBUG_MSG(2, ("=> handshake"));
Paul Bakker1961b702013-01-25 14:49:24 +01003845
Hanno Beckera817ea42020-10-20 15:20:23 +01003846 /* Main handshake loop */
Gilles Peskine449bd832023-01-11 14:50:10 +01003847 while (ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER) {
3848 ret = mbedtls_ssl_handshake_step(ssl);
Paul Bakker1961b702013-01-25 14:49:24 +01003849
Gilles Peskine449bd832023-01-11 14:50:10 +01003850 if (ret != 0) {
Paul Bakker1961b702013-01-25 14:49:24 +01003851 break;
Gilles Peskine449bd832023-01-11 14:50:10 +01003852 }
Paul Bakker1961b702013-01-25 14:49:24 +01003853 }
3854
Gilles Peskine449bd832023-01-11 14:50:10 +01003855 MBEDTLS_SSL_DEBUG_MSG(2, ("<= handshake"));
Paul Bakker5121ce52009-01-03 21:22:43 +00003856
Gilles Peskine449bd832023-01-11 14:50:10 +01003857 return ret;
Paul Bakker5121ce52009-01-03 21:22:43 +00003858}
3859
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003860#if defined(MBEDTLS_SSL_RENEGOTIATION)
3861#if defined(MBEDTLS_SSL_SRV_C)
Paul Bakker5121ce52009-01-03 21:22:43 +00003862/*
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01003863 * Write HelloRequest to request renegotiation on server
Paul Bakker48916f92012-09-16 19:57:18 +00003864 */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02003865MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01003866static int ssl_write_hello_request(mbedtls_ssl_context *ssl)
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01003867{
Janos Follath865b3eb2019-12-16 11:46:15 +00003868 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01003869
Gilles Peskine449bd832023-01-11 14:50:10 +01003870 MBEDTLS_SSL_DEBUG_MSG(2, ("=> write hello request"));
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01003871
3872 ssl->out_msglen = 4;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003873 ssl->out_msgtype = MBEDTLS_SSL_MSG_HANDSHAKE;
3874 ssl->out_msg[0] = MBEDTLS_SSL_HS_HELLO_REQUEST;
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01003875
Gilles Peskine449bd832023-01-11 14:50:10 +01003876 if ((ret = mbedtls_ssl_write_handshake_msg(ssl)) != 0) {
3877 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ssl_write_handshake_msg", ret);
3878 return ret;
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01003879 }
3880
Gilles Peskine449bd832023-01-11 14:50:10 +01003881 MBEDTLS_SSL_DEBUG_MSG(2, ("<= write hello request"));
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01003882
Gilles Peskine449bd832023-01-11 14:50:10 +01003883 return 0;
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01003884}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003885#endif /* MBEDTLS_SSL_SRV_C */
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01003886
3887/*
3888 * Actually renegotiate current connection, triggered by either:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003889 * - any side: calling mbedtls_ssl_renegotiate(),
3890 * - client: receiving a HelloRequest during mbedtls_ssl_read(),
3891 * - server: receiving any handshake message on server during mbedtls_ssl_read() after
Manuel Pégourié-Gonnard55e4ff22014-08-19 11:16:35 +02003892 * the initial handshake is completed.
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01003893 * If the handshake doesn't complete due to waiting for I/O, it will continue
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003894 * during the next calls to mbedtls_ssl_renegotiate() or mbedtls_ssl_read() respectively.
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01003895 */
Gilles Peskine449bd832023-01-11 14:50:10 +01003896int mbedtls_ssl_start_renegotiation(mbedtls_ssl_context *ssl)
Paul Bakker48916f92012-09-16 19:57:18 +00003897{
Janos Follath865b3eb2019-12-16 11:46:15 +00003898 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker48916f92012-09-16 19:57:18 +00003899
Gilles Peskine449bd832023-01-11 14:50:10 +01003900 MBEDTLS_SSL_DEBUG_MSG(2, ("=> renegotiate"));
Paul Bakker48916f92012-09-16 19:57:18 +00003901
Gilles Peskine449bd832023-01-11 14:50:10 +01003902 if ((ret = ssl_handshake_init(ssl)) != 0) {
3903 return ret;
3904 }
Paul Bakker48916f92012-09-16 19:57:18 +00003905
Manuel Pégourié-Gonnard0557bd52014-08-19 19:18:39 +02003906 /* RFC 6347 4.2.2: "[...] the HelloRequest will have message_seq = 0 and
3907 * the ServerHello will have message_seq = 1" */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003908#if defined(MBEDTLS_SSL_PROTO_DTLS)
Gilles Peskine449bd832023-01-11 14:50:10 +01003909 if (ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
3910 ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_PENDING) {
3911 if (ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER) {
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02003912 ssl->handshake->out_msg_seq = 1;
Gilles Peskine449bd832023-01-11 14:50:10 +01003913 } else {
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02003914 ssl->handshake->in_msg_seq = 1;
Gilles Peskine449bd832023-01-11 14:50:10 +01003915 }
Manuel Pégourié-Gonnard0557bd52014-08-19 19:18:39 +02003916 }
3917#endif
3918
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003919 ssl->state = MBEDTLS_SSL_HELLO_REQUEST;
3920 ssl->renego_status = MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS;
Paul Bakker48916f92012-09-16 19:57:18 +00003921
Gilles Peskine449bd832023-01-11 14:50:10 +01003922 if ((ret = mbedtls_ssl_handshake(ssl)) != 0) {
3923 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ssl_handshake", ret);
3924 return ret;
Paul Bakker48916f92012-09-16 19:57:18 +00003925 }
3926
Gilles Peskine449bd832023-01-11 14:50:10 +01003927 MBEDTLS_SSL_DEBUG_MSG(2, ("<= renegotiate"));
Paul Bakker48916f92012-09-16 19:57:18 +00003928
Gilles Peskine449bd832023-01-11 14:50:10 +01003929 return 0;
Paul Bakker48916f92012-09-16 19:57:18 +00003930}
3931
3932/*
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01003933 * Renegotiate current connection on client,
3934 * or request renegotiation on server
3935 */
Gilles Peskine449bd832023-01-11 14:50:10 +01003936int mbedtls_ssl_renegotiate(mbedtls_ssl_context *ssl)
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01003937{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003938 int ret = MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01003939
Gilles Peskine449bd832023-01-11 14:50:10 +01003940 if (ssl == NULL || ssl->conf == NULL) {
3941 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
3942 }
Manuel Pégourié-Gonnardf81ee2e2015-09-01 17:43:40 +02003943
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003944#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01003945 /* On server, just send the request */
Gilles Peskine449bd832023-01-11 14:50:10 +01003946 if (ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER) {
3947 if (mbedtls_ssl_is_handshake_over(ssl) == 0) {
3948 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
3949 }
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01003950
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003951 ssl->renego_status = MBEDTLS_SSL_RENEGOTIATION_PENDING;
Manuel Pégourié-Gonnardf07f4212014-08-15 19:04:47 +02003952
3953 /* Did we already try/start sending HelloRequest? */
Gilles Peskine449bd832023-01-11 14:50:10 +01003954 if (ssl->out_left != 0) {
3955 return mbedtls_ssl_flush_output(ssl);
3956 }
Manuel Pégourié-Gonnardf07f4212014-08-15 19:04:47 +02003957
Gilles Peskine449bd832023-01-11 14:50:10 +01003958 return ssl_write_hello_request(ssl);
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01003959 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003960#endif /* MBEDTLS_SSL_SRV_C */
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01003961
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003962#if defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01003963 /*
3964 * On client, either start the renegotiation process or,
3965 * if already in progress, continue the handshake
3966 */
Gilles Peskine449bd832023-01-11 14:50:10 +01003967 if (ssl->renego_status != MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS) {
3968 if (mbedtls_ssl_is_handshake_over(ssl) == 0) {
3969 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01003970 }
Gilles Peskine449bd832023-01-11 14:50:10 +01003971
3972 if ((ret = mbedtls_ssl_start_renegotiation(ssl)) != 0) {
3973 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ssl_start_renegotiation", ret);
3974 return ret;
3975 }
3976 } else {
3977 if ((ret = mbedtls_ssl_handshake(ssl)) != 0) {
3978 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ssl_handshake", ret);
3979 return ret;
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01003980 }
3981 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003982#endif /* MBEDTLS_SSL_CLI_C */
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01003983
Gilles Peskine449bd832023-01-11 14:50:10 +01003984 return ret;
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01003985}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003986#endif /* MBEDTLS_SSL_RENEGOTIATION */
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01003987
Gilles Peskine449bd832023-01-11 14:50:10 +01003988void mbedtls_ssl_handshake_free(mbedtls_ssl_context *ssl)
Paul Bakker48916f92012-09-16 19:57:18 +00003989{
Gilles Peskine9b562d52018-04-25 20:32:43 +02003990 mbedtls_ssl_handshake_params *handshake = ssl->handshake;
3991
Gilles Peskine449bd832023-01-11 14:50:10 +01003992 if (handshake == NULL) {
Paul Bakkeraccaffe2014-06-26 13:37:14 +02003993 return;
Gilles Peskine449bd832023-01-11 14:50:10 +01003994 }
Paul Bakkeraccaffe2014-06-26 13:37:14 +02003995
Brett Warrene0edc842021-08-17 09:53:13 +01003996#if defined(MBEDTLS_ECP_C)
3997#if !defined(MBEDTLS_DEPRECATED_REMOVED)
Gilles Peskine449bd832023-01-11 14:50:10 +01003998 if (ssl->handshake->group_list_heap_allocated) {
3999 mbedtls_free((void *) handshake->group_list);
4000 }
Brett Warrene0edc842021-08-17 09:53:13 +01004001 handshake->group_list = NULL;
4002#endif /* MBEDTLS_DEPRECATED_REMOVED */
4003#endif /* MBEDTLS_ECP_C */
4004
Ronald Crone68ab4f2022-10-05 12:46:29 +02004005#if defined(MBEDTLS_SSL_HANDSHAKE_WITH_CERT_ENABLED)
Jerry Yuf017ee42022-01-12 15:49:48 +08004006#if !defined(MBEDTLS_DEPRECATED_REMOVED)
Gilles Peskine449bd832023-01-11 14:50:10 +01004007 if (ssl->handshake->sig_algs_heap_allocated) {
4008 mbedtls_free((void *) handshake->sig_algs);
4009 }
Jerry Yuf017ee42022-01-12 15:49:48 +08004010 handshake->sig_algs = NULL;
4011#endif /* MBEDTLS_DEPRECATED_REMOVED */
Xiaofei Baic234ecf2022-02-08 09:59:23 +00004012#if defined(MBEDTLS_SSL_PROTO_TLS1_3)
Gilles Peskine449bd832023-01-11 14:50:10 +01004013 if (ssl->handshake->certificate_request_context) {
4014 mbedtls_free((void *) handshake->certificate_request_context);
Xiaofei Baic234ecf2022-02-08 09:59:23 +00004015 }
4016#endif /* MBEDTLS_SSL_PROTO_TLS1_3 */
Ronald Crone68ab4f2022-10-05 12:46:29 +02004017#endif /* MBEDTLS_SSL_HANDSHAKE_WITH_CERT_ENABLED */
Jerry Yuf017ee42022-01-12 15:49:48 +08004018
Gilles Peskinedf13d5c2018-04-25 20:39:48 +02004019#if defined(MBEDTLS_SSL_ASYNC_PRIVATE)
Gilles Peskine449bd832023-01-11 14:50:10 +01004020 if (ssl->conf->f_async_cancel != NULL && handshake->async_in_progress != 0) {
4021 ssl->conf->f_async_cancel(ssl);
Gilles Peskinedf13d5c2018-04-25 20:39:48 +02004022 handshake->async_in_progress = 0;
4023 }
4024#endif /* MBEDTLS_SSL_ASYNC_PRIVATE */
4025
Andrzej Kurek25f27152022-08-17 16:09:31 -04004026#if defined(MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Andrzej Kurekeb342242019-01-29 09:14:33 -05004027#if defined(MBEDTLS_USE_PSA_CRYPTO)
Gilles Peskine449bd832023-01-11 14:50:10 +01004028 psa_hash_abort(&handshake->fin_sha256_psa);
Andrzej Kurekeb342242019-01-29 09:14:33 -05004029#else
Gilles Peskine449bd832023-01-11 14:50:10 +01004030 mbedtls_sha256_free(&handshake->fin_sha256);
Manuel Pégourié-Gonnardb9d64e52015-07-06 14:18:56 +02004031#endif
Andrzej Kurekeb342242019-01-29 09:14:33 -05004032#endif
Andrzej Kurek25f27152022-08-17 16:09:31 -04004033#if defined(MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Andrzej Kurekeb342242019-01-29 09:14:33 -05004034#if defined(MBEDTLS_USE_PSA_CRYPTO)
Gilles Peskine449bd832023-01-11 14:50:10 +01004035 psa_hash_abort(&handshake->fin_sha384_psa);
Andrzej Kurekeb342242019-01-29 09:14:33 -05004036#else
Gilles Peskine449bd832023-01-11 14:50:10 +01004037 mbedtls_sha512_free(&handshake->fin_sha384);
Manuel Pégourié-Gonnardb9d64e52015-07-06 14:18:56 +02004038#endif
Andrzej Kurekeb342242019-01-29 09:14:33 -05004039#endif
Manuel Pégourié-Gonnardb9d64e52015-07-06 14:18:56 +02004040
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004041#if defined(MBEDTLS_DHM_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01004042 mbedtls_dhm_free(&handshake->dhm_ctx);
Paul Bakker48916f92012-09-16 19:57:18 +00004043#endif
Neil Armstrongf3f46412022-04-12 14:43:39 +02004044#if !defined(MBEDTLS_USE_PSA_CRYPTO) && defined(MBEDTLS_ECDH_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01004045 mbedtls_ecdh_free(&handshake->ecdh_ctx);
Paul Bakker61d113b2013-07-04 11:51:43 +02004046#endif
Valerio Setti02c25b52022-11-15 14:08:42 +01004047
Manuel Pégourié-Gonnardeef142d2015-09-16 10:05:04 +02004048#if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
Neil Armstrongca7d5062022-05-31 14:43:23 +02004049#if defined(MBEDTLS_USE_PSA_CRYPTO)
Gilles Peskine449bd832023-01-11 14:50:10 +01004050 psa_pake_abort(&handshake->psa_pake_ctx);
Valerio Settieb3f7882022-12-08 18:42:58 +01004051 /*
4052 * Opaque keys are not stored in the handshake's data and it's the user
4053 * responsibility to destroy them. Clear ones, instead, are created by
4054 * the TLS library and should be destroyed at the same level
4055 */
Gilles Peskine449bd832023-01-11 14:50:10 +01004056 if (!mbedtls_svc_key_id_is_null(handshake->psa_pake_password)) {
4057 psa_destroy_key(handshake->psa_pake_password);
Valerio Settieb3f7882022-12-08 18:42:58 +01004058 }
Neil Armstrongca7d5062022-05-31 14:43:23 +02004059 handshake->psa_pake_password = MBEDTLS_SVC_KEY_ID_INIT;
4060#else
Gilles Peskine449bd832023-01-11 14:50:10 +01004061 mbedtls_ecjpake_free(&handshake->ecjpake_ctx);
Neil Armstrongca7d5062022-05-31 14:43:23 +02004062#endif /* MBEDTLS_USE_PSA_CRYPTO */
Manuel Pégourié-Gonnard77c06462015-09-17 13:59:49 +02004063#if defined(MBEDTLS_SSL_CLI_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01004064 mbedtls_free(handshake->ecjpake_cache);
Manuel Pégourié-Gonnard77c06462015-09-17 13:59:49 +02004065 handshake->ecjpake_cache = NULL;
4066 handshake->ecjpake_cache_len = 0;
4067#endif
Manuel Pégourié-Gonnard76cfd3f2015-09-15 12:10:54 +02004068#endif
Paul Bakker61d113b2013-07-04 11:51:43 +02004069
Janos Follath4ae5c292016-02-10 11:27:43 +00004070#if defined(MBEDTLS_ECDH_C) || defined(MBEDTLS_ECDSA_C) || \
4071 defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
Paul Bakker9af723c2014-05-01 13:03:14 +02004072 /* explicit void pointer cast for buggy MS compiler */
Gilles Peskine449bd832023-01-11 14:50:10 +01004073 mbedtls_free((void *) handshake->curves_tls_id);
Manuel Pégourié-Gonnardd09453c2013-09-23 19:11:32 +02004074#endif
4075
Ronald Cron73fe8df2022-10-05 14:31:43 +02004076#if defined(MBEDTLS_SSL_HANDSHAKE_WITH_PSK_ENABLED)
Neil Armstrong501c9322022-05-03 09:35:09 +02004077#if defined(MBEDTLS_USE_PSA_CRYPTO)
Gilles Peskine449bd832023-01-11 14:50:10 +01004078 if (!mbedtls_svc_key_id_is_null(ssl->handshake->psk_opaque)) {
Neil Armstrong501c9322022-05-03 09:35:09 +02004079 /* The maintenance of the external PSK key slot is the
4080 * user's responsibility. */
Gilles Peskine449bd832023-01-11 14:50:10 +01004081 if (ssl->handshake->psk_opaque_is_internal) {
4082 psa_destroy_key(ssl->handshake->psk_opaque);
Neil Armstrong501c9322022-05-03 09:35:09 +02004083 ssl->handshake->psk_opaque_is_internal = 0;
4084 }
4085 ssl->handshake->psk_opaque = MBEDTLS_SVC_KEY_ID_INIT;
4086 }
Neil Armstronge952a302022-05-03 10:22:14 +02004087#else
Gilles Peskine449bd832023-01-11 14:50:10 +01004088 if (handshake->psk != NULL) {
4089 mbedtls_platform_zeroize(handshake->psk, handshake->psk_len);
4090 mbedtls_free(handshake->psk);
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01004091 }
Neil Armstronge952a302022-05-03 10:22:14 +02004092#endif /* MBEDTLS_USE_PSA_CRYPTO */
Ronald Cron73fe8df2022-10-05 14:31:43 +02004093#endif /* MBEDTLS_SSL_HANDSHAKE_WITH_PSK_ENABLED */
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01004094
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004095#if defined(MBEDTLS_X509_CRT_PARSE_C) && \
4096 defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
Manuel Pégourié-Gonnard83724542013-09-24 22:30:56 +02004097 /*
4098 * Free only the linked list wrapper, not the keys themselves
4099 * since the belong to the SNI callback
4100 */
Gilles Peskine449bd832023-01-11 14:50:10 +01004101 ssl_key_cert_free(handshake->sni_key_cert);
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004102#endif /* MBEDTLS_X509_CRT_PARSE_C && MBEDTLS_SSL_SERVER_NAME_INDICATION */
Manuel Pégourié-Gonnard705fcca2013-09-23 20:04:20 +02004103
Gilles Peskineeccd8882020-03-10 12:19:08 +01004104#if defined(MBEDTLS_SSL_ECP_RESTARTABLE_ENABLED)
Gilles Peskine449bd832023-01-11 14:50:10 +01004105 mbedtls_x509_crt_restart_free(&handshake->ecrs_ctx);
4106 if (handshake->ecrs_peer_cert != NULL) {
4107 mbedtls_x509_crt_free(handshake->ecrs_peer_cert);
4108 mbedtls_free(handshake->ecrs_peer_cert);
Hanno Becker3dad3112019-02-05 17:19:52 +00004109 }
Manuel Pégourié-Gonnard862cde52017-05-17 11:56:15 +02004110#endif
4111
Hanno Becker75173122019-02-06 16:18:31 +00004112#if defined(MBEDTLS_X509_CRT_PARSE_C) && \
4113 !defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
Gilles Peskine449bd832023-01-11 14:50:10 +01004114 mbedtls_pk_free(&handshake->peer_pubkey);
Hanno Becker75173122019-02-06 16:18:31 +00004115#endif /* MBEDTLS_X509_CRT_PARSE_C && !MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
4116
XiaokangQian9b93c0d2022-02-09 06:02:25 +00004117#if defined(MBEDTLS_SSL_CLI_C) && \
Gilles Peskine449bd832023-01-11 14:50:10 +01004118 (defined(MBEDTLS_SSL_PROTO_DTLS) || defined(MBEDTLS_SSL_PROTO_TLS1_3))
4119 mbedtls_free(handshake->cookie);
XiaokangQian9b93c0d2022-02-09 06:02:25 +00004120#endif /* MBEDTLS_SSL_CLI_C &&
4121 ( MBEDTLS_SSL_PROTO_DTLS || MBEDTLS_SSL_PROTO_TLS1_3 ) */
XiaokangQian8499b6c2022-01-27 09:00:11 +00004122
4123#if defined(MBEDTLS_SSL_PROTO_DTLS)
Gilles Peskine449bd832023-01-11 14:50:10 +01004124 mbedtls_ssl_flight_free(handshake->flight);
4125 mbedtls_ssl_buffering_free(ssl);
XiaokangQian8499b6c2022-01-27 09:00:11 +00004126#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnard74848812014-07-11 02:43:49 +02004127
Ronald Cronf12b81d2022-03-15 10:42:41 +01004128#if defined(MBEDTLS_ECDH_C) && \
Gilles Peskine449bd832023-01-11 14:50:10 +01004129 (defined(MBEDTLS_USE_PSA_CRYPTO) || defined(MBEDTLS_SSL_PROTO_TLS1_3))
4130 if (handshake->ecdh_psa_privkey_is_external == 0) {
4131 psa_destroy_key(handshake->ecdh_psa_privkey);
4132 }
Hanno Becker4a63ed42019-01-08 11:39:35 +00004133#endif /* MBEDTLS_ECDH_C && MBEDTLS_USE_PSA_CRYPTO */
4134
Ronald Cron6f135e12021-12-08 16:57:54 +01004135#if defined(MBEDTLS_SSL_PROTO_TLS1_3)
Gilles Peskine449bd832023-01-11 14:50:10 +01004136 mbedtls_ssl_transform_free(handshake->transform_handshake);
4137 mbedtls_free(handshake->transform_handshake);
Jerry Yu3d9b5902022-11-04 14:07:25 +08004138#if defined(MBEDTLS_SSL_EARLY_DATA)
Gilles Peskine449bd832023-01-11 14:50:10 +01004139 mbedtls_ssl_transform_free(handshake->transform_earlydata);
4140 mbedtls_free(handshake->transform_earlydata);
Jerry Yu3d9b5902022-11-04 14:07:25 +08004141#endif
Ronald Cron6f135e12021-12-08 16:57:54 +01004142#endif /* MBEDTLS_SSL_PROTO_TLS1_3 */
Jerry Yuba9c7272021-10-30 11:54:10 +08004143
Andrzej Kurek0afa2a12020-03-03 10:39:58 -05004144
4145#if defined(MBEDTLS_SSL_VARIABLE_BUFFER_LENGTH)
4146 /* If the buffers are too big - reallocate. Because of the way Mbed TLS
4147 * processes datagrams and the fact that a datagram is allowed to have
4148 * several records in it, it is possible that the I/O buffers are not
4149 * empty at this stage */
Gilles Peskine449bd832023-01-11 14:50:10 +01004150 handle_buffer_resizing(ssl, 1, mbedtls_ssl_get_input_buflen(ssl),
4151 mbedtls_ssl_get_output_buflen(ssl));
Andrzej Kurek0afa2a12020-03-03 10:39:58 -05004152#endif
Hanno Becker3aa186f2021-08-10 09:24:19 +01004153
Jerry Yuba9c7272021-10-30 11:54:10 +08004154 /* mbedtls_platform_zeroize MUST be last one in this function */
Gilles Peskine449bd832023-01-11 14:50:10 +01004155 mbedtls_platform_zeroize(handshake,
4156 sizeof(mbedtls_ssl_handshake_params));
Paul Bakker48916f92012-09-16 19:57:18 +00004157}
4158
Gilles Peskine449bd832023-01-11 14:50:10 +01004159void mbedtls_ssl_session_free(mbedtls_ssl_session *session)
Paul Bakker48916f92012-09-16 19:57:18 +00004160{
Gilles Peskine449bd832023-01-11 14:50:10 +01004161 if (session == NULL) {
Paul Bakkeraccaffe2014-06-26 13:37:14 +02004162 return;
Gilles Peskine449bd832023-01-11 14:50:10 +01004163 }
Paul Bakkeraccaffe2014-06-26 13:37:14 +02004164
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004165#if defined(MBEDTLS_X509_CRT_PARSE_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01004166 ssl_clear_peer_cert(session);
Paul Bakkered27a042013-04-18 22:46:23 +02004167#endif
Paul Bakker0a597072012-09-25 21:55:46 +00004168
Manuel Pégourié-Gonnardb596abf2015-05-20 10:45:29 +02004169#if defined(MBEDTLS_SSL_SESSION_TICKETS) && defined(MBEDTLS_SSL_CLI_C)
Xiaokang Qianbc663a02022-10-09 11:14:39 +00004170#if defined(MBEDTLS_SSL_PROTO_TLS1_3) && \
4171 defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
Gilles Peskine449bd832023-01-11 14:50:10 +01004172 mbedtls_free(session->hostname);
Xiaokang Qian281fd1b2022-09-20 11:35:41 +00004173#endif
Gilles Peskine449bd832023-01-11 14:50:10 +01004174 mbedtls_free(session->ticket);
Paul Bakkera503a632013-08-14 13:48:06 +02004175#endif
Manuel Pégourié-Gonnard75d44012013-08-02 14:44:04 +02004176
Gilles Peskine449bd832023-01-11 14:50:10 +01004177 mbedtls_platform_zeroize(session, sizeof(mbedtls_ssl_session));
Paul Bakker48916f92012-09-16 19:57:18 +00004178}
4179
Manuel Pégourié-Gonnard5c0e3772019-07-23 16:13:17 +02004180#if defined(MBEDTLS_SSL_CONTEXT_SERIALIZATION)
Manuel Pégourié-Gonnard4e9370b2019-07-23 16:31:16 +02004181
4182#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
4183#define SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_CONNECTION_ID 1u
4184#else
4185#define SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_CONNECTION_ID 0u
4186#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
4187
Manuel Pégourié-Gonnard4e9370b2019-07-23 16:31:16 +02004188#define SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_BADMAC_LIMIT 1u
Manuel Pégourié-Gonnard4e9370b2019-07-23 16:31:16 +02004189
4190#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
4191#define SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_ANTI_REPLAY 1u
4192#else
4193#define SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_ANTI_REPLAY 0u
4194#endif /* MBEDTLS_SSL_DTLS_ANTI_REPLAY */
4195
4196#if defined(MBEDTLS_SSL_ALPN)
4197#define SSL_SERIALIZED_CONTEXT_CONFIG_ALPN 1u
4198#else
4199#define SSL_SERIALIZED_CONTEXT_CONFIG_ALPN 0u
4200#endif /* MBEDTLS_SSL_ALPN */
4201
4202#define SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_CONNECTION_ID_BIT 0
4203#define SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_BADMAC_LIMIT_BIT 1
4204#define SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_ANTI_REPLAY_BIT 2
4205#define SSL_SERIALIZED_CONTEXT_CONFIG_ALPN_BIT 3
4206
4207#define SSL_SERIALIZED_CONTEXT_CONFIG_BITFLAG \
Gilles Peskine449bd832023-01-11 14:50:10 +01004208 ((uint32_t) ( \
4209 (SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_CONNECTION_ID << \
4210 SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_CONNECTION_ID_BIT) | \
4211 (SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_BADMAC_LIMIT << \
4212 SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_BADMAC_LIMIT_BIT) | \
4213 (SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_ANTI_REPLAY << \
4214 SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_ANTI_REPLAY_BIT) | \
4215 (SSL_SERIALIZED_CONTEXT_CONFIG_ALPN << SSL_SERIALIZED_CONTEXT_CONFIG_ALPN_BIT) | \
4216 0u))
Manuel Pégourié-Gonnard4e9370b2019-07-23 16:31:16 +02004217
Manuel Pégourié-Gonnard4c90e852019-07-11 10:58:10 +02004218static unsigned char ssl_serialized_context_header[] = {
4219 MBEDTLS_VERSION_MAJOR,
4220 MBEDTLS_VERSION_MINOR,
4221 MBEDTLS_VERSION_PATCH,
Gilles Peskine449bd832023-01-11 14:50:10 +01004222 MBEDTLS_BYTE_1(SSL_SERIALIZED_SESSION_CONFIG_BITFLAG),
4223 MBEDTLS_BYTE_0(SSL_SERIALIZED_SESSION_CONFIG_BITFLAG),
4224 MBEDTLS_BYTE_2(SSL_SERIALIZED_CONTEXT_CONFIG_BITFLAG),
4225 MBEDTLS_BYTE_1(SSL_SERIALIZED_CONTEXT_CONFIG_BITFLAG),
4226 MBEDTLS_BYTE_0(SSL_SERIALIZED_CONTEXT_CONFIG_BITFLAG),
Manuel Pégourié-Gonnard4c90e852019-07-11 10:58:10 +02004227};
4228
Paul Bakker5121ce52009-01-03 21:22:43 +00004229/*
Manuel Pégourié-Gonnardac87e282019-05-28 13:02:16 +02004230 * Serialize a full SSL context
Manuel Pégourié-Gonnard00400c22019-07-10 14:58:45 +02004231 *
4232 * The format of the serialized data is:
4233 * (in the presentation language of TLS, RFC 8446 section 3)
4234 *
4235 * // header
4236 * opaque mbedtls_version[3]; // major, minor, patch
Manuel Pégourié-Gonnard4c90e852019-07-11 10:58:10 +02004237 * opaque context_format[5]; // version-specific field determining
Manuel Pégourié-Gonnard00400c22019-07-10 14:58:45 +02004238 * // the format of the remaining
Manuel Pégourié-Gonnard4c90e852019-07-11 10:58:10 +02004239 * // serialized data.
Manuel Pégourié-Gonnard4e9370b2019-07-23 16:31:16 +02004240 * Note: When updating the format, remember to keep these
4241 * version+format bytes. (We may make their size part of the API.)
Manuel Pégourié-Gonnard00400c22019-07-10 14:58:45 +02004242 *
4243 * // session sub-structure
4244 * opaque session<1..2^32-1>; // see mbedtls_ssl_session_save()
4245 * // transform sub-structure
4246 * uint8 random[64]; // ServerHello.random+ClientHello.random
4247 * uint8 in_cid<0..2^8-1> // Connection ID: expected incoming value
4248 * uint8 out_cid<0..2^8-1> // Connection ID: outgoing value to use
4249 * // fields from ssl_context
4250 * uint32 badmac_seen; // DTLS: number of records with failing MAC
4251 * uint64 in_window_top; // DTLS: last validated record seq_num
4252 * uint64 in_window; // DTLS: bitmask for replay protection
4253 * uint8 disable_datagram_packing; // DTLS: only one record per datagram
4254 * uint64 cur_out_ctr; // Record layer: outgoing sequence number
4255 * uint16 mtu; // DTLS: path mtu (max outgoing fragment size)
4256 * uint8 alpn_chosen<0..2^8-1> // ALPN: negotiated application protocol
4257 *
4258 * Note that many fields of the ssl_context or sub-structures are not
4259 * serialized, as they fall in one of the following categories:
4260 *
4261 * 1. forced value (eg in_left must be 0)
4262 * 2. pointer to dynamically-allocated memory (eg session, transform)
4263 * 3. value can be re-derived from other data (eg session keys from MS)
4264 * 4. value was temporary (eg content of input buffer)
4265 * 5. value will be provided by the user again (eg I/O callbacks and context)
Manuel Pégourié-Gonnardac87e282019-05-28 13:02:16 +02004266 */
Gilles Peskine449bd832023-01-11 14:50:10 +01004267int mbedtls_ssl_context_save(mbedtls_ssl_context *ssl,
4268 unsigned char *buf,
4269 size_t buf_len,
4270 size_t *olen)
Manuel Pégourié-Gonnardac87e282019-05-28 13:02:16 +02004271{
Manuel Pégourié-Gonnard4c90e852019-07-11 10:58:10 +02004272 unsigned char *p = buf;
4273 size_t used = 0;
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02004274 size_t session_len;
4275 int ret = 0;
Manuel Pégourié-Gonnard4c90e852019-07-11 10:58:10 +02004276
Manuel Pégourié-Gonnard1aaf6692019-07-10 14:14:05 +02004277 /*
Manuel Pégourié-Gonnarde4588692019-07-29 12:28:52 +02004278 * Enforce usage restrictions, see "return BAD_INPUT_DATA" in
4279 * this function's documentation.
4280 *
4281 * These are due to assumptions/limitations in the implementation. Some of
4282 * them are likely to stay (no handshake in progress) some might go away
4283 * (only DTLS) but are currently used to simplify the implementation.
Manuel Pégourié-Gonnard1aaf6692019-07-10 14:14:05 +02004284 */
Manuel Pégourié-Gonnarde4588692019-07-29 12:28:52 +02004285 /* The initial handshake must be over */
Gilles Peskine449bd832023-01-11 14:50:10 +01004286 if (mbedtls_ssl_is_handshake_over(ssl) == 0) {
4287 MBEDTLS_SSL_DEBUG_MSG(1, ("Initial handshake isn't over"));
4288 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
Jarno Lamsa8c51b7c2019-08-21 13:45:05 +03004289 }
Gilles Peskine449bd832023-01-11 14:50:10 +01004290 if (ssl->handshake != NULL) {
4291 MBEDTLS_SSL_DEBUG_MSG(1, ("Handshake isn't completed"));
4292 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
Jarno Lamsa8c51b7c2019-08-21 13:45:05 +03004293 }
Manuel Pégourié-Gonnarde4588692019-07-29 12:28:52 +02004294 /* Double-check that sub-structures are indeed ready */
Gilles Peskine449bd832023-01-11 14:50:10 +01004295 if (ssl->transform == NULL || ssl->session == NULL) {
4296 MBEDTLS_SSL_DEBUG_MSG(1, ("Serialised structures aren't ready"));
4297 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
Jarno Lamsa8c51b7c2019-08-21 13:45:05 +03004298 }
Manuel Pégourié-Gonnarde4588692019-07-29 12:28:52 +02004299 /* There must be no pending incoming or outgoing data */
Gilles Peskine449bd832023-01-11 14:50:10 +01004300 if (mbedtls_ssl_check_pending(ssl) != 0) {
4301 MBEDTLS_SSL_DEBUG_MSG(1, ("There is pending incoming data"));
4302 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
Jarno Lamsa8c51b7c2019-08-21 13:45:05 +03004303 }
Gilles Peskine449bd832023-01-11 14:50:10 +01004304 if (ssl->out_left != 0) {
4305 MBEDTLS_SSL_DEBUG_MSG(1, ("There is pending outgoing data"));
4306 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
Jarno Lamsa8c51b7c2019-08-21 13:45:05 +03004307 }
Dave Rodgman556e8a32022-12-06 16:31:25 +00004308 /* Protocol must be DTLS, not TLS */
Gilles Peskine449bd832023-01-11 14:50:10 +01004309 if (ssl->conf->transport != MBEDTLS_SSL_TRANSPORT_DATAGRAM) {
4310 MBEDTLS_SSL_DEBUG_MSG(1, ("Only DTLS is supported"));
4311 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
Jarno Lamsa8c51b7c2019-08-21 13:45:05 +03004312 }
Manuel Pégourié-Gonnarde4588692019-07-29 12:28:52 +02004313 /* Version must be 1.2 */
Gilles Peskine449bd832023-01-11 14:50:10 +01004314 if (ssl->tls_version != MBEDTLS_SSL_VERSION_TLS1_2) {
4315 MBEDTLS_SSL_DEBUG_MSG(1, ("Only version 1.2 supported"));
4316 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
Jarno Lamsa8c51b7c2019-08-21 13:45:05 +03004317 }
Manuel Pégourié-Gonnarde4588692019-07-29 12:28:52 +02004318 /* We must be using an AEAD ciphersuite */
Gilles Peskine449bd832023-01-11 14:50:10 +01004319 if (mbedtls_ssl_transform_uses_aead(ssl->transform) != 1) {
4320 MBEDTLS_SSL_DEBUG_MSG(1, ("Only AEAD ciphersuites supported"));
4321 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
Jarno Lamsa8c51b7c2019-08-21 13:45:05 +03004322 }
Manuel Pégourié-Gonnarde4588692019-07-29 12:28:52 +02004323 /* Renegotiation must not be enabled */
4324#if defined(MBEDTLS_SSL_RENEGOTIATION)
Gilles Peskine449bd832023-01-11 14:50:10 +01004325 if (ssl->conf->disable_renegotiation != MBEDTLS_SSL_RENEGOTIATION_DISABLED) {
4326 MBEDTLS_SSL_DEBUG_MSG(1, ("Renegotiation must not be enabled"));
4327 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
Jarno Lamsa8c51b7c2019-08-21 13:45:05 +03004328 }
Manuel Pégourié-Gonnarde4588692019-07-29 12:28:52 +02004329#endif
Manuel Pégourié-Gonnardac87e282019-05-28 13:02:16 +02004330
Manuel Pégourié-Gonnard4c90e852019-07-11 10:58:10 +02004331 /*
4332 * Version and format identifier
4333 */
Gilles Peskine449bd832023-01-11 14:50:10 +01004334 used += sizeof(ssl_serialized_context_header);
Manuel Pégourié-Gonnardac87e282019-05-28 13:02:16 +02004335
Gilles Peskine449bd832023-01-11 14:50:10 +01004336 if (used <= buf_len) {
4337 memcpy(p, ssl_serialized_context_header,
4338 sizeof(ssl_serialized_context_header));
4339 p += sizeof(ssl_serialized_context_header);
Manuel Pégourié-Gonnard4c90e852019-07-11 10:58:10 +02004340 }
4341
4342 /*
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02004343 * Session (length + data)
4344 */
Gilles Peskine449bd832023-01-11 14:50:10 +01004345 ret = ssl_session_save(ssl->session, 1, NULL, 0, &session_len);
4346 if (ret != MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL) {
4347 return ret;
4348 }
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02004349
4350 used += 4 + session_len;
Gilles Peskine449bd832023-01-11 14:50:10 +01004351 if (used <= buf_len) {
4352 MBEDTLS_PUT_UINT32_BE(session_len, p, 0);
Joe Subbiani1f6c3ae2021-08-20 11:44:44 +01004353 p += 4;
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02004354
Gilles Peskine449bd832023-01-11 14:50:10 +01004355 ret = ssl_session_save(ssl->session, 1,
4356 p, session_len, &session_len);
4357 if (ret != 0) {
4358 return ret;
4359 }
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02004360
4361 p += session_len;
4362 }
4363
4364 /*
Manuel Pégourié-Gonnardc2a7b892019-07-15 09:04:11 +02004365 * Transform
4366 */
Gilles Peskine449bd832023-01-11 14:50:10 +01004367 used += sizeof(ssl->transform->randbytes);
4368 if (used <= buf_len) {
4369 memcpy(p, ssl->transform->randbytes,
4370 sizeof(ssl->transform->randbytes));
4371 p += sizeof(ssl->transform->randbytes);
Manuel Pégourié-Gonnardc2a7b892019-07-15 09:04:11 +02004372 }
4373
4374#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
4375 used += 2 + ssl->transform->in_cid_len + ssl->transform->out_cid_len;
Gilles Peskine449bd832023-01-11 14:50:10 +01004376 if (used <= buf_len) {
Manuel Pégourié-Gonnardc2a7b892019-07-15 09:04:11 +02004377 *p++ = ssl->transform->in_cid_len;
Gilles Peskine449bd832023-01-11 14:50:10 +01004378 memcpy(p, ssl->transform->in_cid, ssl->transform->in_cid_len);
Manuel Pégourié-Gonnardc2a7b892019-07-15 09:04:11 +02004379 p += ssl->transform->in_cid_len;
4380
4381 *p++ = ssl->transform->out_cid_len;
Gilles Peskine449bd832023-01-11 14:50:10 +01004382 memcpy(p, ssl->transform->out_cid, ssl->transform->out_cid_len);
Manuel Pégourié-Gonnardc2a7b892019-07-15 09:04:11 +02004383 p += ssl->transform->out_cid_len;
4384 }
4385#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
4386
4387 /*
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02004388 * Saved fields from top-level ssl_context structure
4389 */
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02004390 used += 4;
Gilles Peskine449bd832023-01-11 14:50:10 +01004391 if (used <= buf_len) {
4392 MBEDTLS_PUT_UINT32_BE(ssl->badmac_seen, p, 0);
Joe Subbiani1f6c3ae2021-08-20 11:44:44 +01004393 p += 4;
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02004394 }
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02004395
4396#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
4397 used += 16;
Gilles Peskine449bd832023-01-11 14:50:10 +01004398 if (used <= buf_len) {
4399 MBEDTLS_PUT_UINT64_BE(ssl->in_window_top, p, 0);
Joe Subbiani1f6c3ae2021-08-20 11:44:44 +01004400 p += 8;
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02004401
Gilles Peskine449bd832023-01-11 14:50:10 +01004402 MBEDTLS_PUT_UINT64_BE(ssl->in_window, p, 0);
Joe Subbiani1f6c3ae2021-08-20 11:44:44 +01004403 p += 8;
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02004404 }
4405#endif /* MBEDTLS_SSL_DTLS_ANTI_REPLAY */
4406
4407#if defined(MBEDTLS_SSL_PROTO_DTLS)
4408 used += 1;
Gilles Peskine449bd832023-01-11 14:50:10 +01004409 if (used <= buf_len) {
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02004410 *p++ = ssl->disable_datagram_packing;
4411 }
4412#endif /* MBEDTLS_SSL_PROTO_DTLS */
4413
Jerry Yuae0b2e22021-10-08 15:21:19 +08004414 used += MBEDTLS_SSL_SEQUENCE_NUMBER_LEN;
Gilles Peskine449bd832023-01-11 14:50:10 +01004415 if (used <= buf_len) {
4416 memcpy(p, ssl->cur_out_ctr, MBEDTLS_SSL_SEQUENCE_NUMBER_LEN);
Jerry Yuae0b2e22021-10-08 15:21:19 +08004417 p += MBEDTLS_SSL_SEQUENCE_NUMBER_LEN;
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02004418 }
4419
4420#if defined(MBEDTLS_SSL_PROTO_DTLS)
4421 used += 2;
Gilles Peskine449bd832023-01-11 14:50:10 +01004422 if (used <= buf_len) {
4423 MBEDTLS_PUT_UINT16_BE(ssl->mtu, p, 0);
Joe Subbiani1f6c3ae2021-08-20 11:44:44 +01004424 p += 2;
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02004425 }
4426#endif /* MBEDTLS_SSL_PROTO_DTLS */
4427
4428#if defined(MBEDTLS_SSL_ALPN)
4429 {
4430 const uint8_t alpn_len = ssl->alpn_chosen
Gilles Peskine449bd832023-01-11 14:50:10 +01004431 ? (uint8_t) strlen(ssl->alpn_chosen)
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02004432 : 0;
4433
4434 used += 1 + alpn_len;
Gilles Peskine449bd832023-01-11 14:50:10 +01004435 if (used <= buf_len) {
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02004436 *p++ = alpn_len;
4437
Gilles Peskine449bd832023-01-11 14:50:10 +01004438 if (ssl->alpn_chosen != NULL) {
4439 memcpy(p, ssl->alpn_chosen, alpn_len);
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02004440 p += alpn_len;
4441 }
4442 }
4443 }
4444#endif /* MBEDTLS_SSL_ALPN */
4445
4446 /*
Manuel Pégourié-Gonnard4c90e852019-07-11 10:58:10 +02004447 * Done
4448 */
4449 *olen = used;
4450
Gilles Peskine449bd832023-01-11 14:50:10 +01004451 if (used > buf_len) {
4452 return MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL;
4453 }
Manuel Pégourié-Gonnardac87e282019-05-28 13:02:16 +02004454
Gilles Peskine449bd832023-01-11 14:50:10 +01004455 MBEDTLS_SSL_DEBUG_BUF(4, "saved context", buf, used);
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02004456
Gilles Peskine449bd832023-01-11 14:50:10 +01004457 return mbedtls_ssl_session_reset_int(ssl, 0);
Manuel Pégourié-Gonnardac87e282019-05-28 13:02:16 +02004458}
4459
4460/*
Manuel Pégourié-Gonnardb9dfc9f2019-07-12 10:50:19 +02004461 * Deserialize context, see mbedtls_ssl_context_save() for format.
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02004462 *
4463 * This internal version is wrapped by a public function that cleans up in
4464 * case of error.
Manuel Pégourié-Gonnardac87e282019-05-28 13:02:16 +02004465 */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02004466MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01004467static int ssl_context_load(mbedtls_ssl_context *ssl,
4468 const unsigned char *buf,
4469 size_t len)
Manuel Pégourié-Gonnardac87e282019-05-28 13:02:16 +02004470{
Manuel Pégourié-Gonnard4c90e852019-07-11 10:58:10 +02004471 const unsigned char *p = buf;
4472 const unsigned char * const end = buf + len;
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02004473 size_t session_len;
Janos Follath865b3eb2019-12-16 11:46:15 +00004474 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Andrzej Kurek2d59dbc2022-10-13 08:34:38 -04004475#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
Andrzej Kurek894edde2022-09-29 06:31:14 -04004476 tls_prf_fn prf_func = NULL;
Andrzej Kurek2d59dbc2022-10-13 08:34:38 -04004477#endif
Manuel Pégourié-Gonnard4c90e852019-07-11 10:58:10 +02004478
Manuel Pégourié-Gonnard0ff76402019-07-11 09:56:30 +02004479 /*
4480 * The context should have been freshly setup or reset.
4481 * Give the user an error in case of obvious misuse.
Manuel Pégourié-Gonnard4ca930f2019-07-26 16:31:53 +02004482 * (Checking session is useful because it won't be NULL if we're
Manuel Pégourié-Gonnard0ff76402019-07-11 09:56:30 +02004483 * renegotiating, or if the user mistakenly loaded a session first.)
4484 */
Gilles Peskine449bd832023-01-11 14:50:10 +01004485 if (ssl->state != MBEDTLS_SSL_HELLO_REQUEST ||
4486 ssl->session != NULL) {
4487 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
Manuel Pégourié-Gonnard0ff76402019-07-11 09:56:30 +02004488 }
4489
4490 /*
4491 * We can't check that the config matches the initial one, but we can at
4492 * least check it matches the requirements for serializing.
4493 */
Gilles Peskine449bd832023-01-11 14:50:10 +01004494 if (ssl->conf->transport != MBEDTLS_SSL_TRANSPORT_DATAGRAM ||
Glenn Strauss2dfcea22022-03-14 17:26:42 -04004495 ssl->conf->max_tls_version < MBEDTLS_SSL_VERSION_TLS1_2 ||
4496 ssl->conf->min_tls_version > MBEDTLS_SSL_VERSION_TLS1_2 ||
Manuel Pégourié-Gonnard0ff76402019-07-11 09:56:30 +02004497#if defined(MBEDTLS_SSL_RENEGOTIATION)
Manuel Pégourié-Gonnard9a96fd72019-07-23 17:11:24 +02004498 ssl->conf->disable_renegotiation != MBEDTLS_SSL_RENEGOTIATION_DISABLED ||
Manuel Pégourié-Gonnard0ff76402019-07-11 09:56:30 +02004499#endif
Gilles Peskine449bd832023-01-11 14:50:10 +01004500 0) {
4501 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
Manuel Pégourié-Gonnard0ff76402019-07-11 09:56:30 +02004502 }
4503
Gilles Peskine449bd832023-01-11 14:50:10 +01004504 MBEDTLS_SSL_DEBUG_BUF(4, "context to load", buf, len);
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02004505
Manuel Pégourié-Gonnard4c90e852019-07-11 10:58:10 +02004506 /*
4507 * Check version identifier
4508 */
Gilles Peskine449bd832023-01-11 14:50:10 +01004509 if ((size_t) (end - p) < sizeof(ssl_serialized_context_header)) {
4510 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
Manuel Pégourié-Gonnard4c90e852019-07-11 10:58:10 +02004511 }
Gilles Peskine449bd832023-01-11 14:50:10 +01004512
4513 if (memcmp(p, ssl_serialized_context_header,
4514 sizeof(ssl_serialized_context_header)) != 0) {
4515 return MBEDTLS_ERR_SSL_VERSION_MISMATCH;
4516 }
4517 p += sizeof(ssl_serialized_context_header);
Manuel Pégourié-Gonnard4c90e852019-07-11 10:58:10 +02004518
4519 /*
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02004520 * Session
4521 */
Gilles Peskine449bd832023-01-11 14:50:10 +01004522 if ((size_t) (end - p) < 4) {
4523 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
4524 }
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02004525
Gilles Peskine449bd832023-01-11 14:50:10 +01004526 session_len = ((size_t) p[0] << 24) |
4527 ((size_t) p[1] << 16) |
4528 ((size_t) p[2] << 8) |
4529 ((size_t) p[3]);
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02004530 p += 4;
4531
Manuel Pégourié-Gonnard142ba732019-07-23 14:43:30 +02004532 /* This has been allocated by ssl_handshake_init(), called by
Hanno Becker43aefe22020-02-05 10:44:56 +00004533 * by either mbedtls_ssl_session_reset_int() or mbedtls_ssl_setup(). */
Manuel Pégourié-Gonnard142ba732019-07-23 14:43:30 +02004534 ssl->session = ssl->session_negotiate;
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02004535 ssl->session_in = ssl->session;
4536 ssl->session_out = ssl->session;
Manuel Pégourié-Gonnard142ba732019-07-23 14:43:30 +02004537 ssl->session_negotiate = NULL;
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02004538
Gilles Peskine449bd832023-01-11 14:50:10 +01004539 if ((size_t) (end - p) < session_len) {
4540 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
4541 }
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02004542
Gilles Peskine449bd832023-01-11 14:50:10 +01004543 ret = ssl_session_load(ssl->session, 1, p, session_len);
4544 if (ret != 0) {
4545 mbedtls_ssl_session_free(ssl->session);
4546 return ret;
Manuel Pégourié-Gonnard45ac1f02019-07-23 16:52:45 +02004547 }
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02004548
4549 p += session_len;
4550
4551 /*
Manuel Pégourié-Gonnardc2a7b892019-07-15 09:04:11 +02004552 * Transform
4553 */
4554
Manuel Pégourié-Gonnard142ba732019-07-23 14:43:30 +02004555 /* This has been allocated by ssl_handshake_init(), called by
Hanno Becker43aefe22020-02-05 10:44:56 +00004556 * by either mbedtls_ssl_session_reset_int() or mbedtls_ssl_setup(). */
Jerry Yu2e199812022-12-01 18:57:19 +08004557#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
Manuel Pégourié-Gonnard142ba732019-07-23 14:43:30 +02004558 ssl->transform = ssl->transform_negotiate;
Manuel Pégourié-Gonnardc2a7b892019-07-15 09:04:11 +02004559 ssl->transform_in = ssl->transform;
4560 ssl->transform_out = ssl->transform;
Manuel Pégourié-Gonnard142ba732019-07-23 14:43:30 +02004561 ssl->transform_negotiate = NULL;
Jerry Yu2e199812022-12-01 18:57:19 +08004562#endif
Manuel Pégourié-Gonnardc2a7b892019-07-15 09:04:11 +02004563
Andrzej Kurek2d59dbc2022-10-13 08:34:38 -04004564#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
Gilles Peskine449bd832023-01-11 14:50:10 +01004565 prf_func = ssl_tls12prf_from_cs(ssl->session->ciphersuite);
4566 if (prf_func == NULL) {
4567 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
4568 }
Andrzej Kurek894edde2022-09-29 06:31:14 -04004569
Manuel Pégourié-Gonnardc2a7b892019-07-15 09:04:11 +02004570 /* Read random bytes and populate structure */
Gilles Peskine449bd832023-01-11 14:50:10 +01004571 if ((size_t) (end - p) < sizeof(ssl->transform->randbytes)) {
4572 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
4573 }
Andrzej Kurek2d59dbc2022-10-13 08:34:38 -04004574
Gilles Peskine449bd832023-01-11 14:50:10 +01004575 ret = ssl_tls12_populate_transform(ssl->transform,
4576 ssl->session->ciphersuite,
4577 ssl->session->master,
Neil Armstrongf2c82f02022-04-05 11:16:53 +02004578#if defined(MBEDTLS_SSL_SOME_SUITES_USE_CBC_ETM)
Gilles Peskine449bd832023-01-11 14:50:10 +01004579 ssl->session->encrypt_then_mac,
Neil Armstrongf2c82f02022-04-05 11:16:53 +02004580#endif /* MBEDTLS_SSL_SOME_SUITES_USE_CBC_ETM */
Gilles Peskine449bd832023-01-11 14:50:10 +01004581 prf_func,
4582 p, /* currently pointing to randbytes */
4583 MBEDTLS_SSL_VERSION_TLS1_2, /* (D)TLS 1.2 is forced */
4584 ssl->conf->endpoint,
4585 ssl);
4586 if (ret != 0) {
4587 return ret;
4588 }
Jerry Yu840fbb22022-02-17 14:59:29 +08004589#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Gilles Peskine449bd832023-01-11 14:50:10 +01004590 p += sizeof(ssl->transform->randbytes);
Manuel Pégourié-Gonnardc2a7b892019-07-15 09:04:11 +02004591
4592#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
4593 /* Read connection IDs and store them */
Gilles Peskine449bd832023-01-11 14:50:10 +01004594 if ((size_t) (end - p) < 1) {
4595 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
4596 }
Manuel Pégourié-Gonnardc2a7b892019-07-15 09:04:11 +02004597
4598 ssl->transform->in_cid_len = *p++;
4599
Gilles Peskine449bd832023-01-11 14:50:10 +01004600 if ((size_t) (end - p) < ssl->transform->in_cid_len + 1u) {
4601 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
4602 }
Manuel Pégourié-Gonnardc2a7b892019-07-15 09:04:11 +02004603
Gilles Peskine449bd832023-01-11 14:50:10 +01004604 memcpy(ssl->transform->in_cid, p, ssl->transform->in_cid_len);
Manuel Pégourié-Gonnardc2a7b892019-07-15 09:04:11 +02004605 p += ssl->transform->in_cid_len;
4606
4607 ssl->transform->out_cid_len = *p++;
4608
Gilles Peskine449bd832023-01-11 14:50:10 +01004609 if ((size_t) (end - p) < ssl->transform->out_cid_len) {
4610 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
4611 }
Manuel Pégourié-Gonnardc2a7b892019-07-15 09:04:11 +02004612
Gilles Peskine449bd832023-01-11 14:50:10 +01004613 memcpy(ssl->transform->out_cid, p, ssl->transform->out_cid_len);
Manuel Pégourié-Gonnardc2a7b892019-07-15 09:04:11 +02004614 p += ssl->transform->out_cid_len;
4615#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
4616
4617 /*
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02004618 * Saved fields from top-level ssl_context structure
4619 */
Gilles Peskine449bd832023-01-11 14:50:10 +01004620 if ((size_t) (end - p) < 4) {
4621 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
4622 }
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02004623
Gilles Peskine449bd832023-01-11 14:50:10 +01004624 ssl->badmac_seen = ((uint32_t) p[0] << 24) |
4625 ((uint32_t) p[1] << 16) |
4626 ((uint32_t) p[2] << 8) |
4627 ((uint32_t) p[3]);
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02004628 p += 4;
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02004629
4630#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
Gilles Peskine449bd832023-01-11 14:50:10 +01004631 if ((size_t) (end - p) < 16) {
4632 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
4633 }
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02004634
Gilles Peskine449bd832023-01-11 14:50:10 +01004635 ssl->in_window_top = ((uint64_t) p[0] << 56) |
4636 ((uint64_t) p[1] << 48) |
4637 ((uint64_t) p[2] << 40) |
4638 ((uint64_t) p[3] << 32) |
4639 ((uint64_t) p[4] << 24) |
4640 ((uint64_t) p[5] << 16) |
4641 ((uint64_t) p[6] << 8) |
4642 ((uint64_t) p[7]);
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02004643 p += 8;
4644
Gilles Peskine449bd832023-01-11 14:50:10 +01004645 ssl->in_window = ((uint64_t) p[0] << 56) |
4646 ((uint64_t) p[1] << 48) |
4647 ((uint64_t) p[2] << 40) |
4648 ((uint64_t) p[3] << 32) |
4649 ((uint64_t) p[4] << 24) |
4650 ((uint64_t) p[5] << 16) |
4651 ((uint64_t) p[6] << 8) |
4652 ((uint64_t) p[7]);
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02004653 p += 8;
4654#endif /* MBEDTLS_SSL_DTLS_ANTI_REPLAY */
4655
4656#if defined(MBEDTLS_SSL_PROTO_DTLS)
Gilles Peskine449bd832023-01-11 14:50:10 +01004657 if ((size_t) (end - p) < 1) {
4658 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
4659 }
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02004660
4661 ssl->disable_datagram_packing = *p++;
4662#endif /* MBEDTLS_SSL_PROTO_DTLS */
4663
Gilles Peskine449bd832023-01-11 14:50:10 +01004664 if ((size_t) (end - p) < sizeof(ssl->cur_out_ctr)) {
4665 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
4666 }
4667 memcpy(ssl->cur_out_ctr, p, sizeof(ssl->cur_out_ctr));
4668 p += sizeof(ssl->cur_out_ctr);
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02004669
4670#if defined(MBEDTLS_SSL_PROTO_DTLS)
Gilles Peskine449bd832023-01-11 14:50:10 +01004671 if ((size_t) (end - p) < 2) {
4672 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
4673 }
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02004674
Gilles Peskine449bd832023-01-11 14:50:10 +01004675 ssl->mtu = (p[0] << 8) | p[1];
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02004676 p += 2;
4677#endif /* MBEDTLS_SSL_PROTO_DTLS */
4678
4679#if defined(MBEDTLS_SSL_ALPN)
4680 {
4681 uint8_t alpn_len;
4682 const char **cur;
4683
Gilles Peskine449bd832023-01-11 14:50:10 +01004684 if ((size_t) (end - p) < 1) {
4685 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
4686 }
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02004687
4688 alpn_len = *p++;
4689
Gilles Peskine449bd832023-01-11 14:50:10 +01004690 if (alpn_len != 0 && ssl->conf->alpn_list != NULL) {
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02004691 /* alpn_chosen should point to an item in the configured list */
Gilles Peskine449bd832023-01-11 14:50:10 +01004692 for (cur = ssl->conf->alpn_list; *cur != NULL; cur++) {
4693 if (strlen(*cur) == alpn_len &&
4694 memcmp(p, cur, alpn_len) == 0) {
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02004695 ssl->alpn_chosen = *cur;
4696 break;
4697 }
4698 }
4699 }
4700
4701 /* can only happen on conf mismatch */
Gilles Peskine449bd832023-01-11 14:50:10 +01004702 if (alpn_len != 0 && ssl->alpn_chosen == NULL) {
4703 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
4704 }
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02004705
4706 p += alpn_len;
4707 }
4708#endif /* MBEDTLS_SSL_ALPN */
4709
4710 /*
Manuel Pégourié-Gonnard0eb3eac2019-07-15 11:53:51 +02004711 * Forced fields from top-level ssl_context structure
4712 *
4713 * Most of them already set to the correct value by mbedtls_ssl_init() and
4714 * mbedtls_ssl_reset(), so we only need to set the remaining ones.
4715 */
4716 ssl->state = MBEDTLS_SSL_HANDSHAKE_OVER;
Glenn Strauss60bfe602022-03-14 19:04:24 -04004717 ssl->tls_version = MBEDTLS_SSL_VERSION_TLS1_2;
Manuel Pégourié-Gonnard0eb3eac2019-07-15 11:53:51 +02004718
Hanno Becker361b10d2019-08-30 10:42:49 +01004719 /* Adjust pointers for header fields of outgoing records to
4720 * the given transform, accounting for explicit IV and CID. */
Gilles Peskine449bd832023-01-11 14:50:10 +01004721 mbedtls_ssl_update_out_pointers(ssl, ssl->transform);
Hanno Becker361b10d2019-08-30 10:42:49 +01004722
Manuel Pégourié-Gonnard0eb3eac2019-07-15 11:53:51 +02004723#if defined(MBEDTLS_SSL_PROTO_DTLS)
4724 ssl->in_epoch = 1;
4725#endif
4726
4727 /* mbedtls_ssl_reset() leaves the handshake sub-structure allocated,
4728 * which we don't want - otherwise we'd end up freeing the wrong transform
Hanno Beckerce5f5fd2020-02-05 10:47:44 +00004729 * by calling mbedtls_ssl_handshake_wrapup_free_hs_transform()
4730 * inappropriately. */
Gilles Peskine449bd832023-01-11 14:50:10 +01004731 if (ssl->handshake != NULL) {
4732 mbedtls_ssl_handshake_free(ssl);
4733 mbedtls_free(ssl->handshake);
Manuel Pégourié-Gonnard0eb3eac2019-07-15 11:53:51 +02004734 ssl->handshake = NULL;
4735 }
4736
4737 /*
Manuel Pégourié-Gonnard4c90e852019-07-11 10:58:10 +02004738 * Done - should have consumed entire buffer
4739 */
Gilles Peskine449bd832023-01-11 14:50:10 +01004740 if (p != end) {
4741 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
4742 }
Manuel Pégourié-Gonnardac87e282019-05-28 13:02:16 +02004743
Gilles Peskine449bd832023-01-11 14:50:10 +01004744 return 0;
Manuel Pégourié-Gonnardac87e282019-05-28 13:02:16 +02004745}
4746
4747/*
Manuel Pégourié-Gonnardb9dfc9f2019-07-12 10:50:19 +02004748 * Deserialize context: public wrapper for error cleaning
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02004749 */
Gilles Peskine449bd832023-01-11 14:50:10 +01004750int mbedtls_ssl_context_load(mbedtls_ssl_context *context,
4751 const unsigned char *buf,
4752 size_t len)
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02004753{
Gilles Peskine449bd832023-01-11 14:50:10 +01004754 int ret = ssl_context_load(context, buf, len);
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02004755
Gilles Peskine449bd832023-01-11 14:50:10 +01004756 if (ret != 0) {
4757 mbedtls_ssl_free(context);
4758 }
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02004759
Gilles Peskine449bd832023-01-11 14:50:10 +01004760 return ret;
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02004761}
Manuel Pégourié-Gonnard5c0e3772019-07-23 16:13:17 +02004762#endif /* MBEDTLS_SSL_CONTEXT_SERIALIZATION */
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02004763
4764/*
Paul Bakker5121ce52009-01-03 21:22:43 +00004765 * Free an SSL context
4766 */
Gilles Peskine449bd832023-01-11 14:50:10 +01004767void mbedtls_ssl_free(mbedtls_ssl_context *ssl)
Paul Bakker5121ce52009-01-03 21:22:43 +00004768{
Gilles Peskine449bd832023-01-11 14:50:10 +01004769 if (ssl == NULL) {
Paul Bakkeraccaffe2014-06-26 13:37:14 +02004770 return;
Gilles Peskine449bd832023-01-11 14:50:10 +01004771 }
Paul Bakkeraccaffe2014-06-26 13:37:14 +02004772
Gilles Peskine449bd832023-01-11 14:50:10 +01004773 MBEDTLS_SSL_DEBUG_MSG(2, ("=> free"));
Paul Bakker5121ce52009-01-03 21:22:43 +00004774
Gilles Peskine449bd832023-01-11 14:50:10 +01004775 if (ssl->out_buf != NULL) {
sander-visserb8aa2072020-05-06 22:05:13 +02004776#if defined(MBEDTLS_SSL_VARIABLE_BUFFER_LENGTH)
4777 size_t out_buf_len = ssl->out_buf_len;
4778#else
4779 size_t out_buf_len = MBEDTLS_SSL_OUT_BUFFER_LEN;
4780#endif
4781
Gilles Peskine449bd832023-01-11 14:50:10 +01004782 mbedtls_platform_zeroize(ssl->out_buf, out_buf_len);
4783 mbedtls_free(ssl->out_buf);
Andrzej Kurek0afa2a12020-03-03 10:39:58 -05004784 ssl->out_buf = NULL;
Paul Bakker5121ce52009-01-03 21:22:43 +00004785 }
4786
Gilles Peskine449bd832023-01-11 14:50:10 +01004787 if (ssl->in_buf != NULL) {
sander-visserb8aa2072020-05-06 22:05:13 +02004788#if defined(MBEDTLS_SSL_VARIABLE_BUFFER_LENGTH)
4789 size_t in_buf_len = ssl->in_buf_len;
4790#else
4791 size_t in_buf_len = MBEDTLS_SSL_IN_BUFFER_LEN;
4792#endif
4793
Gilles Peskine449bd832023-01-11 14:50:10 +01004794 mbedtls_platform_zeroize(ssl->in_buf, in_buf_len);
4795 mbedtls_free(ssl->in_buf);
Andrzej Kurek0afa2a12020-03-03 10:39:58 -05004796 ssl->in_buf = NULL;
Paul Bakker5121ce52009-01-03 21:22:43 +00004797 }
4798
Gilles Peskine449bd832023-01-11 14:50:10 +01004799 if (ssl->transform) {
4800 mbedtls_ssl_transform_free(ssl->transform);
4801 mbedtls_free(ssl->transform);
Paul Bakker48916f92012-09-16 19:57:18 +00004802 }
4803
Gilles Peskine449bd832023-01-11 14:50:10 +01004804 if (ssl->handshake) {
4805 mbedtls_ssl_handshake_free(ssl);
4806 mbedtls_free(ssl->handshake);
Jerry Yu2e199812022-12-01 18:57:19 +08004807
4808#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
Gilles Peskine449bd832023-01-11 14:50:10 +01004809 mbedtls_ssl_transform_free(ssl->transform_negotiate);
4810 mbedtls_free(ssl->transform_negotiate);
Jerry Yu2e199812022-12-01 18:57:19 +08004811#endif
4812
Gilles Peskine449bd832023-01-11 14:50:10 +01004813 mbedtls_ssl_session_free(ssl->session_negotiate);
4814 mbedtls_free(ssl->session_negotiate);
Paul Bakker48916f92012-09-16 19:57:18 +00004815 }
4816
Ronald Cron6f135e12021-12-08 16:57:54 +01004817#if defined(MBEDTLS_SSL_PROTO_TLS1_3)
Gilles Peskine449bd832023-01-11 14:50:10 +01004818 mbedtls_ssl_transform_free(ssl->transform_application);
4819 mbedtls_free(ssl->transform_application);
Ronald Cron6f135e12021-12-08 16:57:54 +01004820#endif /* MBEDTLS_SSL_PROTO_TLS1_3 */
Hanno Becker3aa186f2021-08-10 09:24:19 +01004821
Gilles Peskine449bd832023-01-11 14:50:10 +01004822 if (ssl->session) {
4823 mbedtls_ssl_session_free(ssl->session);
4824 mbedtls_free(ssl->session);
Paul Bakkerc0463502013-02-14 11:19:38 +01004825 }
4826
Manuel Pégourié-Gonnard55fab2d2015-05-11 16:15:19 +02004827#if defined(MBEDTLS_X509_CRT_PARSE_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01004828 if (ssl->hostname != NULL) {
4829 mbedtls_platform_zeroize(ssl->hostname, strlen(ssl->hostname));
4830 mbedtls_free(ssl->hostname);
Paul Bakker5121ce52009-01-03 21:22:43 +00004831 }
Paul Bakker0be444a2013-08-27 21:55:01 +02004832#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00004833
Manuel Pégourié-Gonnarde057d3b2015-05-20 10:59:43 +02004834#if defined(MBEDTLS_SSL_DTLS_HELLO_VERIFY) && defined(MBEDTLS_SSL_SRV_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01004835 mbedtls_free(ssl->cli_id);
Manuel Pégourié-Gonnard43c02182014-07-22 17:32:01 +02004836#endif
4837
Gilles Peskine449bd832023-01-11 14:50:10 +01004838 MBEDTLS_SSL_DEBUG_MSG(2, ("<= free"));
Paul Bakker2da561c2009-02-05 18:00:28 +00004839
Paul Bakker86f04f42013-02-14 11:20:09 +01004840 /* Actually clear after last debug message */
Gilles Peskine449bd832023-01-11 14:50:10 +01004841 mbedtls_platform_zeroize(ssl, sizeof(mbedtls_ssl_context));
Paul Bakker5121ce52009-01-03 21:22:43 +00004842}
4843
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02004844/*
Shaun Case8b0ecbc2021-12-20 21:14:10 -08004845 * Initialize mbedtls_ssl_config
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02004846 */
Gilles Peskine449bd832023-01-11 14:50:10 +01004847void mbedtls_ssl_config_init(mbedtls_ssl_config *conf)
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02004848{
Gilles Peskine449bd832023-01-11 14:50:10 +01004849 memset(conf, 0, sizeof(mbedtls_ssl_config));
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02004850}
4851
Gilles Peskineae270bf2021-06-02 00:05:29 +02004852/* The selection should be the same as mbedtls_x509_crt_profile_default in
4853 * x509_crt.c, plus Montgomery curves for ECDHE. Here, the order matters:
Gilles Peskineb1940a72021-06-02 15:18:12 +02004854 * curves with a lower resource usage come first.
Gilles Peskineae270bf2021-06-02 00:05:29 +02004855 * See the documentation of mbedtls_ssl_conf_curves() for what we promise
Gilles Peskineb1940a72021-06-02 15:18:12 +02004856 * about this list.
4857 */
Brett Warrene0edc842021-08-17 09:53:13 +01004858static uint16_t ssl_preset_default_groups[] = {
Gilles Peskineae270bf2021-06-02 00:05:29 +02004859#if defined(MBEDTLS_ECP_DP_CURVE25519_ENABLED)
Brett Warrene0edc842021-08-17 09:53:13 +01004860 MBEDTLS_SSL_IANA_TLS_GROUP_X25519,
Gilles Peskineae270bf2021-06-02 00:05:29 +02004861#endif
4862#if defined(MBEDTLS_ECP_DP_SECP256R1_ENABLED)
Brett Warrene0edc842021-08-17 09:53:13 +01004863 MBEDTLS_SSL_IANA_TLS_GROUP_SECP256R1,
Gilles Peskineae270bf2021-06-02 00:05:29 +02004864#endif
Gilles Peskineb1940a72021-06-02 15:18:12 +02004865#if defined(MBEDTLS_ECP_DP_SECP384R1_ENABLED)
Brett Warrene0edc842021-08-17 09:53:13 +01004866 MBEDTLS_SSL_IANA_TLS_GROUP_SECP384R1,
Gilles Peskineb1940a72021-06-02 15:18:12 +02004867#endif
4868#if defined(MBEDTLS_ECP_DP_CURVE448_ENABLED)
Brett Warrene0edc842021-08-17 09:53:13 +01004869 MBEDTLS_SSL_IANA_TLS_GROUP_X448,
Gilles Peskineb1940a72021-06-02 15:18:12 +02004870#endif
4871#if defined(MBEDTLS_ECP_DP_SECP521R1_ENABLED)
Brett Warrene0edc842021-08-17 09:53:13 +01004872 MBEDTLS_SSL_IANA_TLS_GROUP_SECP521R1,
Gilles Peskineb1940a72021-06-02 15:18:12 +02004873#endif
Gilles Peskineae270bf2021-06-02 00:05:29 +02004874#if defined(MBEDTLS_ECP_DP_BP256R1_ENABLED)
Brett Warrene0edc842021-08-17 09:53:13 +01004875 MBEDTLS_SSL_IANA_TLS_GROUP_BP256R1,
Gilles Peskineae270bf2021-06-02 00:05:29 +02004876#endif
Gilles Peskineb1940a72021-06-02 15:18:12 +02004877#if defined(MBEDTLS_ECP_DP_BP384R1_ENABLED)
Brett Warrene0edc842021-08-17 09:53:13 +01004878 MBEDTLS_SSL_IANA_TLS_GROUP_BP384R1,
Gilles Peskineb1940a72021-06-02 15:18:12 +02004879#endif
4880#if defined(MBEDTLS_ECP_DP_BP512R1_ENABLED)
Brett Warrene0edc842021-08-17 09:53:13 +01004881 MBEDTLS_SSL_IANA_TLS_GROUP_BP512R1,
Gilles Peskineb1940a72021-06-02 15:18:12 +02004882#endif
Brett Warrene0edc842021-08-17 09:53:13 +01004883 MBEDTLS_SSL_IANA_TLS_GROUP_NONE
Gilles Peskineae270bf2021-06-02 00:05:29 +02004884};
Gilles Peskineae270bf2021-06-02 00:05:29 +02004885
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02004886static int ssl_preset_suiteb_ciphersuites[] = {
4887 MBEDTLS_TLS_ECDHE_ECDSA_WITH_AES_128_GCM_SHA256,
4888 MBEDTLS_TLS_ECDHE_ECDSA_WITH_AES_256_GCM_SHA384,
4889 0
4890};
4891
Ronald Crone68ab4f2022-10-05 12:46:29 +02004892#if defined(MBEDTLS_SSL_HANDSHAKE_WITH_CERT_ENABLED)
Hanno Becker9c6aa7b2021-08-10 13:50:43 +01004893
Jerry Yu909df7b2022-01-22 11:56:27 +08004894/* NOTICE:
Jerry Yu0b994b82022-01-25 17:22:12 +08004895 * For ssl_preset_*_sig_algs and ssl_tls12_preset_*_sig_algs, the following
Jerry Yu370e1462022-01-25 10:36:53 +08004896 * rules SHOULD be upheld.
4897 * - No duplicate entries.
4898 * - But if there is a good reason, do not change the order of the algorithms.
Jerry Yu09a99fc2022-07-28 14:22:17 +08004899 * - ssl_tls12_preset* is for TLS 1.2 use only.
Jerry Yu370e1462022-01-25 10:36:53 +08004900 * - ssl_preset_* is for TLS 1.3 only or hybrid TLS 1.3/1.2 handshakes.
Jerry Yu1a8b4812022-01-20 17:56:50 +08004901 */
Hanno Becker9c6aa7b2021-08-10 13:50:43 +01004902static uint16_t ssl_preset_default_sig_algs[] = {
Jerry Yu1a8b4812022-01-20 17:56:50 +08004903
Andrzej Kurek25f27152022-08-17 16:09:31 -04004904#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 +08004905 defined(MBEDTLS_ECP_DP_SECP256R1_ENABLED)
4906 MBEDTLS_TLS1_3_SIG_ECDSA_SECP256R1_SHA256,
Andrzej Kurekcccb0442022-08-19 03:42:11 -04004907#endif /* MBEDTLS_ECDSA_C && MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA &&
Jerry Yued5e9f42022-01-26 11:21:34 +08004908 MBEDTLS_ECP_DP_SECP256R1_ENABLED */
Jerry Yu909df7b2022-01-22 11:56:27 +08004909
Andrzej Kurek25f27152022-08-17 16:09:31 -04004910#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 +08004911 defined(MBEDTLS_ECP_DP_SECP384R1_ENABLED)
4912 MBEDTLS_TLS1_3_SIG_ECDSA_SECP384R1_SHA384,
Andrzej Kurekcccb0442022-08-19 03:42:11 -04004913#endif /* MBEDTLS_ECDSA_C && MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA&&
Jerry Yu909df7b2022-01-22 11:56:27 +08004914 MBEDTLS_ECP_DP_SECP384R1_ENABLED */
4915
Andrzej Kurek25f27152022-08-17 16:09:31 -04004916#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 +08004917 defined(MBEDTLS_ECP_DP_SECP521R1_ENABLED)
4918 MBEDTLS_TLS1_3_SIG_ECDSA_SECP521R1_SHA512,
Andrzej Kurekcccb0442022-08-19 03:42:11 -04004919#endif /* MBEDTLS_ECDSA_C && MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA&&
Jerry Yued5e9f42022-01-26 11:21:34 +08004920 MBEDTLS_ECP_DP_SECP521R1_ENABLED */
Jerry Yu909df7b2022-01-22 11:56:27 +08004921
Gilles Peskine449bd832023-01-11 14:50:10 +01004922#if defined(MBEDTLS_X509_RSASSA_PSS_SUPPORT) && \
4923 defined(MBEDTLS_HAS_ALG_SHA_512_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Jerry Yu9bb3ee42022-06-23 10:16:33 +08004924 MBEDTLS_TLS1_3_SIG_RSA_PSS_RSAE_SHA512,
Gilles Peskine449bd832023-01-11 14:50:10 +01004925#endif \
4926 /* 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 +08004927
Gilles Peskine449bd832023-01-11 14:50:10 +01004928#if defined(MBEDTLS_X509_RSASSA_PSS_SUPPORT) && \
4929 defined(MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Jerry Yu9bb3ee42022-06-23 10:16:33 +08004930 MBEDTLS_TLS1_3_SIG_RSA_PSS_RSAE_SHA384,
Gilles Peskine449bd832023-01-11 14:50:10 +01004931#endif \
4932 /* 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 +08004933
Gilles Peskine449bd832023-01-11 14:50:10 +01004934#if defined(MBEDTLS_X509_RSASSA_PSS_SUPPORT) && \
4935 defined(MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Jerry Yu9bb3ee42022-06-23 10:16:33 +08004936 MBEDTLS_TLS1_3_SIG_RSA_PSS_RSAE_SHA256,
Gilles Peskine449bd832023-01-11 14:50:10 +01004937#endif \
4938 /* 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 +08004939
Andrzej Kurek25f27152022-08-17 16:09:31 -04004940#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 +08004941 MBEDTLS_TLS1_3_SIG_RSA_PKCS1_SHA512,
4942#endif /* MBEDTLS_RSA_C && MBEDTLS_SHA512_C */
4943
Andrzej Kurek25f27152022-08-17 16:09:31 -04004944#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 +08004945 MBEDTLS_TLS1_3_SIG_RSA_PKCS1_SHA384,
4946#endif /* MBEDTLS_RSA_C && MBEDTLS_SHA384_C */
4947
Andrzej Kurek25f27152022-08-17 16:09:31 -04004948#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 +08004949 MBEDTLS_TLS1_3_SIG_RSA_PKCS1_SHA256,
4950#endif /* MBEDTLS_RSA_C && MBEDTLS_SHA256_C */
4951
Gabor Mezei15b95a62022-05-09 16:37:58 +02004952 MBEDTLS_TLS_SIG_NONE
Jerry Yu909df7b2022-01-22 11:56:27 +08004953};
4954
4955/* NOTICE: see above */
4956#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
4957static uint16_t ssl_tls12_preset_default_sig_algs[] = {
Andrzej Kurek25f27152022-08-17 16:09:31 -04004958#if defined(MBEDTLS_HAS_ALG_SHA_512_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Gabor Mezeic1051b62022-05-10 13:13:58 +02004959#if defined(MBEDTLS_ECDSA_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01004960 MBEDTLS_SSL_TLS12_SIG_AND_HASH_ALG(MBEDTLS_SSL_SIG_ECDSA, MBEDTLS_SSL_HASH_SHA512),
Jerry Yu713013f2022-01-17 18:16:35 +08004961#endif
Jerry Yu09a99fc2022-07-28 14:22:17 +08004962#if defined(MBEDTLS_X509_RSASSA_PSS_SUPPORT)
4963 MBEDTLS_TLS1_3_SIG_RSA_PSS_RSAE_SHA512,
4964#endif /* MBEDTLS_X509_RSASSA_PSS_SUPPORT */
Gabor Mezeic1051b62022-05-10 13:13:58 +02004965#if defined(MBEDTLS_RSA_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01004966 MBEDTLS_SSL_TLS12_SIG_AND_HASH_ALG(MBEDTLS_SSL_SIG_RSA, MBEDTLS_SSL_HASH_SHA512),
Gabor Mezeic1051b62022-05-10 13:13:58 +02004967#endif
Andrzej Kurekcccb0442022-08-19 03:42:11 -04004968#endif /* MBEDTLS_HAS_ALG_SHA_512_VIA_MD_OR_PSA_BASED_ON_USE_PSA*/
Andrzej Kurek25f27152022-08-17 16:09:31 -04004969#if defined(MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Gabor Mezeic1051b62022-05-10 13:13:58 +02004970#if defined(MBEDTLS_ECDSA_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01004971 MBEDTLS_SSL_TLS12_SIG_AND_HASH_ALG(MBEDTLS_SSL_SIG_ECDSA, MBEDTLS_SSL_HASH_SHA384),
Jerry Yu11f0a9c2022-01-12 18:43:08 +08004972#endif
Jerry Yu09a99fc2022-07-28 14:22:17 +08004973#if defined(MBEDTLS_X509_RSASSA_PSS_SUPPORT)
4974 MBEDTLS_TLS1_3_SIG_RSA_PSS_RSAE_SHA384,
4975#endif /* MBEDTLS_X509_RSASSA_PSS_SUPPORT */
Gabor Mezeic1051b62022-05-10 13:13:58 +02004976#if defined(MBEDTLS_RSA_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01004977 MBEDTLS_SSL_TLS12_SIG_AND_HASH_ALG(MBEDTLS_SSL_SIG_RSA, MBEDTLS_SSL_HASH_SHA384),
Gabor Mezeic1051b62022-05-10 13:13:58 +02004978#endif
Andrzej Kurekcccb0442022-08-19 03:42:11 -04004979#endif /* MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA*/
Andrzej Kurek25f27152022-08-17 16:09:31 -04004980#if defined(MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Gabor Mezeic1051b62022-05-10 13:13:58 +02004981#if defined(MBEDTLS_ECDSA_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01004982 MBEDTLS_SSL_TLS12_SIG_AND_HASH_ALG(MBEDTLS_SSL_SIG_ECDSA, MBEDTLS_SSL_HASH_SHA256),
Jerry Yu11f0a9c2022-01-12 18:43:08 +08004983#endif
Jerry Yu09a99fc2022-07-28 14:22:17 +08004984#if defined(MBEDTLS_X509_RSASSA_PSS_SUPPORT)
4985 MBEDTLS_TLS1_3_SIG_RSA_PSS_RSAE_SHA256,
4986#endif /* MBEDTLS_X509_RSASSA_PSS_SUPPORT */
Gabor Mezeic1051b62022-05-10 13:13:58 +02004987#if defined(MBEDTLS_RSA_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01004988 MBEDTLS_SSL_TLS12_SIG_AND_HASH_ALG(MBEDTLS_SSL_SIG_RSA, MBEDTLS_SSL_HASH_SHA256),
Gabor Mezeic1051b62022-05-10 13:13:58 +02004989#endif
Andrzej Kurekcccb0442022-08-19 03:42:11 -04004990#endif /* MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA*/
Gabor Mezei15b95a62022-05-09 16:37:58 +02004991 MBEDTLS_TLS_SIG_NONE
Jerry Yu909df7b2022-01-22 11:56:27 +08004992};
4993#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
4994/* NOTICE: see above */
4995static uint16_t ssl_preset_suiteb_sig_algs[] = {
4996
Andrzej Kurek25f27152022-08-17 16:09:31 -04004997#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 +08004998 defined(MBEDTLS_ECP_DP_SECP256R1_ENABLED)
4999 MBEDTLS_TLS1_3_SIG_ECDSA_SECP256R1_SHA256,
Andrzej Kurekcccb0442022-08-19 03:42:11 -04005000#endif /* MBEDTLS_ECDSA_C && MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA&&
Jerry Yu909df7b2022-01-22 11:56:27 +08005001 MBEDTLS_ECP_DP_SECP256R1_ENABLED */
5002
Andrzej Kurek25f27152022-08-17 16:09:31 -04005003#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 +08005004 defined(MBEDTLS_ECP_DP_SECP384R1_ENABLED)
5005 MBEDTLS_TLS1_3_SIG_ECDSA_SECP384R1_SHA384,
Andrzej Kurekcccb0442022-08-19 03:42:11 -04005006#endif /* MBEDTLS_ECDSA_C && MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA&&
Jerry Yu53037892022-01-25 11:02:06 +08005007 MBEDTLS_ECP_DP_SECP384R1_ENABLED */
Jerry Yu909df7b2022-01-22 11:56:27 +08005008
Gilles Peskine449bd832023-01-11 14:50:10 +01005009#if defined(MBEDTLS_X509_RSASSA_PSS_SUPPORT) && \
5010 defined(MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Jerry Yu909df7b2022-01-22 11:56:27 +08005011 MBEDTLS_TLS1_3_SIG_RSA_PSS_RSAE_SHA256,
Gilles Peskine449bd832023-01-11 14:50:10 +01005012#endif \
5013 /* 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 +08005014
Andrzej Kurek25f27152022-08-17 16:09:31 -04005015#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 +08005016 MBEDTLS_TLS1_3_SIG_RSA_PKCS1_SHA256,
Andrzej Kurekcccb0442022-08-19 03:42:11 -04005017#endif /* MBEDTLS_RSA_C && MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA*/
Jerry Yu53037892022-01-25 11:02:06 +08005018
Gabor Mezei15b95a62022-05-09 16:37:58 +02005019 MBEDTLS_TLS_SIG_NONE
Jerry Yu713013f2022-01-17 18:16:35 +08005020};
Jerry Yu6106fdc2022-01-12 16:36:14 +08005021
Jerry Yu909df7b2022-01-22 11:56:27 +08005022/* NOTICE: see above */
5023#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
5024static uint16_t ssl_tls12_preset_suiteb_sig_algs[] = {
Andrzej Kurek25f27152022-08-17 16:09:31 -04005025#if defined(MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Gabor Mezeic1051b62022-05-10 13:13:58 +02005026#if defined(MBEDTLS_ECDSA_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01005027 MBEDTLS_SSL_TLS12_SIG_AND_HASH_ALG(MBEDTLS_SSL_SIG_ECDSA, MBEDTLS_SSL_HASH_SHA256),
Jerry Yu713013f2022-01-17 18:16:35 +08005028#endif
Gabor Mezeic1051b62022-05-10 13:13:58 +02005029#if defined(MBEDTLS_RSA_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01005030 MBEDTLS_SSL_TLS12_SIG_AND_HASH_ALG(MBEDTLS_SSL_SIG_RSA, MBEDTLS_SSL_HASH_SHA256),
Gabor Mezeic1051b62022-05-10 13:13:58 +02005031#endif
Andrzej Kurekcccb0442022-08-19 03:42:11 -04005032#endif /* MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA*/
Andrzej Kurek25f27152022-08-17 16:09:31 -04005033#if defined(MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Gabor Mezeic1051b62022-05-10 13:13:58 +02005034#if defined(MBEDTLS_ECDSA_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01005035 MBEDTLS_SSL_TLS12_SIG_AND_HASH_ALG(MBEDTLS_SSL_SIG_ECDSA, MBEDTLS_SSL_HASH_SHA384),
Jerry Yu18c833e2022-01-25 10:55:47 +08005036#endif
Gabor Mezeic1051b62022-05-10 13:13:58 +02005037#if defined(MBEDTLS_RSA_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01005038 MBEDTLS_SSL_TLS12_SIG_AND_HASH_ALG(MBEDTLS_SSL_SIG_RSA, MBEDTLS_SSL_HASH_SHA384),
Gabor Mezeic1051b62022-05-10 13:13:58 +02005039#endif
Andrzej Kurekcccb0442022-08-19 03:42:11 -04005040#endif /* MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA*/
Gabor Mezei15b95a62022-05-09 16:37:58 +02005041 MBEDTLS_TLS_SIG_NONE
Hanno Becker9c6aa7b2021-08-10 13:50:43 +01005042};
Jerry Yu909df7b2022-01-22 11:56:27 +08005043#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
5044
Ronald Crone68ab4f2022-10-05 12:46:29 +02005045#endif /* MBEDTLS_SSL_HANDSHAKE_WITH_CERT_ENABLED */
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02005046
Brett Warrene0edc842021-08-17 09:53:13 +01005047static uint16_t ssl_preset_suiteb_groups[] = {
Jaeden Amerod4311042019-06-03 08:27:16 +01005048#if defined(MBEDTLS_ECP_DP_SECP256R1_ENABLED)
Brett Warrene0edc842021-08-17 09:53:13 +01005049 MBEDTLS_SSL_IANA_TLS_GROUP_SECP256R1,
Jaeden Amerod4311042019-06-03 08:27:16 +01005050#endif
5051#if defined(MBEDTLS_ECP_DP_SECP384R1_ENABLED)
Brett Warrene0edc842021-08-17 09:53:13 +01005052 MBEDTLS_SSL_IANA_TLS_GROUP_SECP384R1,
Jaeden Amerod4311042019-06-03 08:27:16 +01005053#endif
Brett Warrene0edc842021-08-17 09:53:13 +01005054 MBEDTLS_SSL_IANA_TLS_GROUP_NONE
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02005055};
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02005056
Ronald Crone68ab4f2022-10-05 12:46:29 +02005057#if defined(MBEDTLS_DEBUG_C) && defined(MBEDTLS_SSL_HANDSHAKE_WITH_CERT_ENABLED)
Jerry Yu909df7b2022-01-22 11:56:27 +08005058/* Function for checking `ssl_preset_*_sig_algs` and `ssl_tls12_preset_*_sig_algs`
Jerry Yu370e1462022-01-25 10:36:53 +08005059 * to make sure there are no duplicated signature algorithm entries. */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02005060MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01005061static int ssl_check_no_sig_alg_duplication(uint16_t *sig_algs)
Jerry Yu1a8b4812022-01-20 17:56:50 +08005062{
5063 size_t i, j;
5064 int ret = 0;
Jerry Yu909df7b2022-01-22 11:56:27 +08005065
Gilles Peskine449bd832023-01-11 14:50:10 +01005066 for (i = 0; sig_algs[i] != MBEDTLS_TLS_SIG_NONE; i++) {
5067 for (j = 0; j < i; j++) {
5068 if (sig_algs[i] != sig_algs[j]) {
Jerry Yuf377d642022-01-25 10:43:59 +08005069 continue;
Gilles Peskine449bd832023-01-11 14:50:10 +01005070 }
5071 mbedtls_printf(" entry(%04x,%" MBEDTLS_PRINTF_SIZET
5072 ") is duplicated at %" MBEDTLS_PRINTF_SIZET "\n",
5073 sig_algs[i], j, i);
Jerry Yuf377d642022-01-25 10:43:59 +08005074 ret = -1;
Jerry Yu1a8b4812022-01-20 17:56:50 +08005075 }
5076 }
Gilles Peskine449bd832023-01-11 14:50:10 +01005077 return ret;
Jerry Yu1a8b4812022-01-20 17:56:50 +08005078}
5079
Ronald Crone68ab4f2022-10-05 12:46:29 +02005080#endif /* MBEDTLS_DEBUG_C && MBEDTLS_SSL_HANDSHAKE_WITH_CERT_ENABLED */
Jerry Yu1a8b4812022-01-20 17:56:50 +08005081
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02005082/*
Tillmann Karras588ad502015-09-25 04:27:22 +02005083 * Load default in mbedtls_ssl_config
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02005084 */
Gilles Peskine449bd832023-01-11 14:50:10 +01005085int mbedtls_ssl_config_defaults(mbedtls_ssl_config *conf,
5086 int endpoint, int transport, int preset)
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02005087{
Manuel Pégourié-Gonnard8b431fb2015-05-11 12:54:52 +02005088#if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_SSL_SRV_C)
Janos Follath865b3eb2019-12-16 11:46:15 +00005089 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnard8b431fb2015-05-11 12:54:52 +02005090#endif
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02005091
Ronald Crone68ab4f2022-10-05 12:46:29 +02005092#if defined(MBEDTLS_DEBUG_C) && defined(MBEDTLS_SSL_HANDSHAKE_WITH_CERT_ENABLED)
Gilles Peskine449bd832023-01-11 14:50:10 +01005093 if (ssl_check_no_sig_alg_duplication(ssl_preset_suiteb_sig_algs)) {
5094 mbedtls_printf("ssl_preset_suiteb_sig_algs has duplicated entries\n");
5095 return MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Jerry Yu1a8b4812022-01-20 17:56:50 +08005096 }
5097
Gilles Peskine449bd832023-01-11 14:50:10 +01005098 if (ssl_check_no_sig_alg_duplication(ssl_preset_default_sig_algs)) {
5099 mbedtls_printf("ssl_preset_default_sig_algs has duplicated entries\n");
5100 return MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Jerry Yu1a8b4812022-01-20 17:56:50 +08005101 }
Jerry Yu909df7b2022-01-22 11:56:27 +08005102
5103#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
Gilles Peskine449bd832023-01-11 14:50:10 +01005104 if (ssl_check_no_sig_alg_duplication(ssl_tls12_preset_suiteb_sig_algs)) {
5105 mbedtls_printf("ssl_tls12_preset_suiteb_sig_algs has duplicated entries\n");
5106 return MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Jerry Yu909df7b2022-01-22 11:56:27 +08005107 }
5108
Gilles Peskine449bd832023-01-11 14:50:10 +01005109 if (ssl_check_no_sig_alg_duplication(ssl_tls12_preset_default_sig_algs)) {
5110 mbedtls_printf("ssl_tls12_preset_default_sig_algs has duplicated entries\n");
5111 return MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Jerry Yu909df7b2022-01-22 11:56:27 +08005112 }
5113#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Ronald Crone68ab4f2022-10-05 12:46:29 +02005114#endif /* MBEDTLS_DEBUG_C && MBEDTLS_SSL_HANDSHAKE_WITH_CERT_ENABLED */
Jerry Yu1a8b4812022-01-20 17:56:50 +08005115
Manuel Pégourié-Gonnard0de074f2015-05-14 12:58:01 +02005116 /* Use the functions here so that they are covered in tests,
5117 * but otherwise access member directly for efficiency */
Gilles Peskine449bd832023-01-11 14:50:10 +01005118 mbedtls_ssl_conf_endpoint(conf, endpoint);
5119 mbedtls_ssl_conf_transport(conf, transport);
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02005120
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02005121 /*
5122 * Things that are common to all presets
5123 */
Manuel Pégourié-Gonnard419d5ae2015-05-04 19:32:36 +02005124#if defined(MBEDTLS_SSL_CLI_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01005125 if (endpoint == MBEDTLS_SSL_IS_CLIENT) {
Manuel Pégourié-Gonnard419d5ae2015-05-04 19:32:36 +02005126 conf->authmode = MBEDTLS_SSL_VERIFY_REQUIRED;
5127#if defined(MBEDTLS_SSL_SESSION_TICKETS)
5128 conf->session_tickets = MBEDTLS_SSL_SESSION_TICKETS_ENABLED;
5129#endif
5130 }
5131#endif
5132
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02005133#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
5134 conf->encrypt_then_mac = MBEDTLS_SSL_ETM_ENABLED;
5135#endif
5136
5137#if defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET)
5138 conf->extended_ms = MBEDTLS_SSL_EXTENDED_MS_ENABLED;
5139#endif
5140
Manuel Pégourié-Gonnarde057d3b2015-05-20 10:59:43 +02005141#if defined(MBEDTLS_SSL_DTLS_HELLO_VERIFY) && defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02005142 conf->f_cookie_write = ssl_cookie_write_dummy;
5143 conf->f_cookie_check = ssl_cookie_check_dummy;
5144#endif
5145
5146#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
5147 conf->anti_replay = MBEDTLS_SSL_ANTI_REPLAY_ENABLED;
5148#endif
5149
Janos Follath088ce432017-04-10 12:42:31 +01005150#if defined(MBEDTLS_SSL_SRV_C)
5151 conf->cert_req_ca_list = MBEDTLS_SSL_CERT_REQ_CA_LIST_ENABLED;
TRodziewicz3946f792021-06-14 12:11:18 +02005152 conf->respect_cli_pref = MBEDTLS_SSL_SRV_CIPHERSUITE_ORDER_SERVER;
Janos Follath088ce432017-04-10 12:42:31 +01005153#endif
5154
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02005155#if defined(MBEDTLS_SSL_PROTO_DTLS)
5156 conf->hs_timeout_min = MBEDTLS_SSL_DTLS_TIMEOUT_DFL_MIN;
5157 conf->hs_timeout_max = MBEDTLS_SSL_DTLS_TIMEOUT_DFL_MAX;
5158#endif
5159
5160#if defined(MBEDTLS_SSL_RENEGOTIATION)
5161 conf->renego_max_records = MBEDTLS_SSL_RENEGO_MAX_RECORDS_DEFAULT;
Gilles Peskine449bd832023-01-11 14:50:10 +01005162 memset(conf->renego_period, 0x00, 2);
5163 memset(conf->renego_period + 2, 0xFF, 6);
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02005164#endif
5165
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02005166#if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_SSL_SRV_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01005167 if (endpoint == MBEDTLS_SSL_IS_SERVER) {
Hanno Beckere2defad2021-07-24 05:59:17 +01005168 const unsigned char dhm_p[] =
5169 MBEDTLS_DHM_RFC3526_MODP_2048_P_BIN;
5170 const unsigned char dhm_g[] =
5171 MBEDTLS_DHM_RFC3526_MODP_2048_G_BIN;
Hanno Becker00d0a682017-10-04 13:14:29 +01005172
Gilles Peskine449bd832023-01-11 14:50:10 +01005173 if ((ret = mbedtls_ssl_conf_dh_param_bin(conf,
5174 dhm_p, sizeof(dhm_p),
5175 dhm_g, sizeof(dhm_g))) != 0) {
5176 return ret;
Hanno Beckere2defad2021-07-24 05:59:17 +01005177 }
5178 }
Manuel Pégourié-Gonnardbd990d62015-06-11 14:49:42 +02005179#endif
5180
Ronald Cron6f135e12021-12-08 16:57:54 +01005181#if defined(MBEDTLS_SSL_PROTO_TLS1_3)
Jerry Yu6ee56aa2022-12-06 17:47:22 +08005182
5183#if defined(MBEDTLS_SSL_EARLY_DATA)
Gilles Peskine449bd832023-01-11 14:50:10 +01005184 mbedtls_ssl_tls13_conf_early_data(conf, MBEDTLS_SSL_EARLY_DATA_DISABLED);
Jerry Yu6ee56aa2022-12-06 17:47:22 +08005185#if defined(MBEDTLS_SSL_SRV_C)
5186 mbedtls_ssl_tls13_conf_max_early_data_size(
Gilles Peskine449bd832023-01-11 14:50:10 +01005187 conf, MBEDTLS_SSL_MAX_EARLY_DATA_SIZE);
Jerry Yu6ee56aa2022-12-06 17:47:22 +08005188#endif
5189#endif /* MBEDTLS_SSL_EARLY_DATA */
5190
Jerry Yud0766ec2022-09-22 10:46:57 +08005191#if defined(MBEDTLS_SSL_SRV_C) && defined(MBEDTLS_SSL_SESSION_TICKETS)
Jerry Yu1ad7ace2022-08-09 13:28:39 +08005192 mbedtls_ssl_conf_new_session_tickets(
Gilles Peskine449bd832023-01-11 14:50:10 +01005193 conf, MBEDTLS_SSL_TLS1_3_DEFAULT_NEW_SESSION_TICKETS);
Jerry Yu1ad7ace2022-08-09 13:28:39 +08005194#endif
Hanno Becker71f1ed62021-07-24 06:01:47 +01005195 /*
5196 * Allow all TLS 1.3 key exchange modes by default.
5197 */
Xiaofei Bai746f9482021-11-12 08:53:56 +00005198 conf->tls13_kex_modes = MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_ALL;
Ronald Cron6f135e12021-12-08 16:57:54 +01005199#endif /* MBEDTLS_SSL_PROTO_TLS1_3 */
Hanno Becker71f1ed62021-07-24 06:01:47 +01005200
Gilles Peskine449bd832023-01-11 14:50:10 +01005201 if (transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM) {
Glenn Strauss2dfcea22022-03-14 17:26:42 -04005202#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
XiaokangQian4d3a6042022-04-21 13:46:17 +00005203 conf->min_tls_version = MBEDTLS_SSL_VERSION_TLS1_2;
XiaokangQian060d8672022-04-21 09:24:56 +00005204 conf->max_tls_version = MBEDTLS_SSL_VERSION_TLS1_2;
XiaokangQian4d3a6042022-04-21 13:46:17 +00005205#else
Gilles Peskine449bd832023-01-11 14:50:10 +01005206 return MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
XiaokangQian060d8672022-04-21 09:24:56 +00005207#endif
Gilles Peskine449bd832023-01-11 14:50:10 +01005208 } else {
XiaokangQian4d3a6042022-04-21 13:46:17 +00005209#if defined(MBEDTLS_SSL_PROTO_TLS1_2) && defined(MBEDTLS_SSL_PROTO_TLS1_3)
Gilles Peskine449bd832023-01-11 14:50:10 +01005210 if (endpoint == MBEDTLS_SSL_IS_CLIENT) {
XiaokangQian4d3a6042022-04-21 13:46:17 +00005211 conf->min_tls_version = MBEDTLS_SSL_VERSION_TLS1_2;
5212 conf->max_tls_version = MBEDTLS_SSL_VERSION_TLS1_3;
Gilles Peskine449bd832023-01-11 14:50:10 +01005213 } else {
5214 /* Hybrid TLS 1.2 / 1.3 is not supported on server side yet */
XiaokangQian4d3a6042022-04-21 13:46:17 +00005215 conf->min_tls_version = MBEDTLS_SSL_VERSION_TLS1_2;
5216 conf->max_tls_version = MBEDTLS_SSL_VERSION_TLS1_2;
5217 }
5218#elif defined(MBEDTLS_SSL_PROTO_TLS1_3)
5219 conf->min_tls_version = MBEDTLS_SSL_VERSION_TLS1_3;
5220 conf->max_tls_version = MBEDTLS_SSL_VERSION_TLS1_3;
5221#elif defined(MBEDTLS_SSL_PROTO_TLS1_2)
5222 conf->min_tls_version = MBEDTLS_SSL_VERSION_TLS1_2;
5223 conf->max_tls_version = MBEDTLS_SSL_VERSION_TLS1_2;
5224#else
Gilles Peskine449bd832023-01-11 14:50:10 +01005225 return MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
XiaokangQian4d3a6042022-04-21 13:46:17 +00005226#endif
5227 }
Glenn Strauss2dfcea22022-03-14 17:26:42 -04005228
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02005229 /*
5230 * Preset-specific defaults
5231 */
Gilles Peskine449bd832023-01-11 14:50:10 +01005232 switch (preset) {
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02005233 /*
5234 * NSA Suite B
5235 */
5236 case MBEDTLS_SSL_PRESET_SUITEB:
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02005237
Hanno Beckerd60b6c62021-04-29 12:04:11 +01005238 conf->ciphersuite_list = ssl_preset_suiteb_ciphersuites;
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02005239
5240#if defined(MBEDTLS_X509_CRT_PARSE_C)
5241 conf->cert_profile = &mbedtls_x509_crt_profile_suiteb;
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02005242#endif
5243
Ronald Crone68ab4f2022-10-05 12:46:29 +02005244#if defined(MBEDTLS_SSL_HANDSHAKE_WITH_CERT_ENABLED)
Jerry Yu909df7b2022-01-22 11:56:27 +08005245#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
Gilles Peskine449bd832023-01-11 14:50:10 +01005246 if (mbedtls_ssl_conf_is_tls12_only(conf)) {
Jerry Yu909df7b2022-01-22 11:56:27 +08005247 conf->sig_algs = ssl_tls12_preset_suiteb_sig_algs;
Gilles Peskine449bd832023-01-11 14:50:10 +01005248 } else
Jerry Yu909df7b2022-01-22 11:56:27 +08005249#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Gilles Peskine449bd832023-01-11 14:50:10 +01005250 conf->sig_algs = ssl_preset_suiteb_sig_algs;
Ronald Crone68ab4f2022-10-05 12:46:29 +02005251#endif /* MBEDTLS_SSL_HANDSHAKE_WITH_CERT_ENABLED */
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02005252
Brett Warrene0edc842021-08-17 09:53:13 +01005253#if defined(MBEDTLS_ECP_C) && !defined(MBEDTLS_DEPRECATED_REMOVED)
5254 conf->curve_list = NULL;
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02005255#endif
Brett Warrene0edc842021-08-17 09:53:13 +01005256 conf->group_list = ssl_preset_suiteb_groups;
Manuel Pégourié-Gonnardc98204e2015-08-11 04:21:01 +02005257 break;
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02005258
5259 /*
5260 * Default
5261 */
5262 default:
Ronald Cronf6606552022-03-15 11:23:25 +01005263
Hanno Beckerd60b6c62021-04-29 12:04:11 +01005264 conf->ciphersuite_list = mbedtls_ssl_list_ciphersuites();
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02005265
5266#if defined(MBEDTLS_X509_CRT_PARSE_C)
5267 conf->cert_profile = &mbedtls_x509_crt_profile_default;
5268#endif
5269
Ronald Crone68ab4f2022-10-05 12:46:29 +02005270#if defined(MBEDTLS_SSL_HANDSHAKE_WITH_CERT_ENABLED)
Jerry Yu909df7b2022-01-22 11:56:27 +08005271#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
Gilles Peskine449bd832023-01-11 14:50:10 +01005272 if (mbedtls_ssl_conf_is_tls12_only(conf)) {
Jerry Yu909df7b2022-01-22 11:56:27 +08005273 conf->sig_algs = ssl_tls12_preset_default_sig_algs;
Gilles Peskine449bd832023-01-11 14:50:10 +01005274 } else
Jerry Yu909df7b2022-01-22 11:56:27 +08005275#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Gilles Peskine449bd832023-01-11 14:50:10 +01005276 conf->sig_algs = ssl_preset_default_sig_algs;
Ronald Crone68ab4f2022-10-05 12:46:29 +02005277#endif /* MBEDTLS_SSL_HANDSHAKE_WITH_CERT_ENABLED */
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02005278
Brett Warrene0edc842021-08-17 09:53:13 +01005279#if defined(MBEDTLS_ECP_C) && !defined(MBEDTLS_DEPRECATED_REMOVED)
5280 conf->curve_list = NULL;
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02005281#endif
Brett Warrene0edc842021-08-17 09:53:13 +01005282 conf->group_list = ssl_preset_default_groups;
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02005283
5284#if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_SSL_CLI_C)
5285 conf->dhm_min_bitlen = 1024;
5286#endif
5287 }
5288
Gilles Peskine449bd832023-01-11 14:50:10 +01005289 return 0;
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02005290}
5291
5292/*
5293 * Free mbedtls_ssl_config
5294 */
Gilles Peskine449bd832023-01-11 14:50:10 +01005295void mbedtls_ssl_config_free(mbedtls_ssl_config *conf)
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02005296{
5297#if defined(MBEDTLS_DHM_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01005298 mbedtls_mpi_free(&conf->dhm_P);
5299 mbedtls_mpi_free(&conf->dhm_G);
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02005300#endif
5301
Ronald Cron73fe8df2022-10-05 14:31:43 +02005302#if defined(MBEDTLS_SSL_HANDSHAKE_WITH_PSK_ENABLED)
Neil Armstrong501c9322022-05-03 09:35:09 +02005303#if defined(MBEDTLS_USE_PSA_CRYPTO)
Gilles Peskine449bd832023-01-11 14:50:10 +01005304 if (!mbedtls_svc_key_id_is_null(conf->psk_opaque)) {
Neil Armstrong501c9322022-05-03 09:35:09 +02005305 conf->psk_opaque = MBEDTLS_SVC_KEY_ID_INIT;
5306 }
Neil Armstrong8ecd6682022-05-05 11:40:35 +02005307#endif /* MBEDTLS_USE_PSA_CRYPTO */
Gilles Peskine449bd832023-01-11 14:50:10 +01005308 if (conf->psk != NULL) {
5309 mbedtls_platform_zeroize(conf->psk, conf->psk_len);
5310 mbedtls_free(conf->psk);
Azim Khan27e8a122018-03-21 14:24:11 +00005311 conf->psk = NULL;
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02005312 conf->psk_len = 0;
junyeonLEE316b1622017-12-20 16:29:30 +09005313 }
5314
Gilles Peskine449bd832023-01-11 14:50:10 +01005315 if (conf->psk_identity != NULL) {
5316 mbedtls_platform_zeroize(conf->psk_identity, conf->psk_identity_len);
5317 mbedtls_free(conf->psk_identity);
Azim Khan27e8a122018-03-21 14:24:11 +00005318 conf->psk_identity = NULL;
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02005319 conf->psk_identity_len = 0;
5320 }
Ronald Cron73fe8df2022-10-05 14:31:43 +02005321#endif /* MBEDTLS_SSL_HANDSHAKE_WITH_PSK_ENABLED */
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02005322
5323#if defined(MBEDTLS_X509_CRT_PARSE_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01005324 ssl_key_cert_free(conf->key_cert);
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02005325#endif
5326
Gilles Peskine449bd832023-01-11 14:50:10 +01005327 mbedtls_platform_zeroize(conf, sizeof(mbedtls_ssl_config));
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02005328}
5329
Manuel Pégourié-Gonnard5674a972015-10-19 15:14:03 +02005330#if defined(MBEDTLS_PK_C) && \
Gilles Peskine449bd832023-01-11 14:50:10 +01005331 (defined(MBEDTLS_RSA_C) || defined(MBEDTLS_ECDSA_C))
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02005332/*
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005333 * Convert between MBEDTLS_PK_XXX and SSL_SIG_XXX
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02005334 */
Gilles Peskine449bd832023-01-11 14:50:10 +01005335unsigned char mbedtls_ssl_sig_from_pk(mbedtls_pk_context *pk)
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02005336{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005337#if defined(MBEDTLS_RSA_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01005338 if (mbedtls_pk_can_do(pk, MBEDTLS_PK_RSA)) {
5339 return MBEDTLS_SSL_SIG_RSA;
5340 }
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02005341#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005342#if defined(MBEDTLS_ECDSA_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01005343 if (mbedtls_pk_can_do(pk, MBEDTLS_PK_ECDSA)) {
5344 return MBEDTLS_SSL_SIG_ECDSA;
5345 }
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02005346#endif
Gilles Peskine449bd832023-01-11 14:50:10 +01005347 return MBEDTLS_SSL_SIG_ANON;
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02005348}
5349
Gilles Peskine449bd832023-01-11 14:50:10 +01005350unsigned char mbedtls_ssl_sig_from_pk_alg(mbedtls_pk_type_t type)
Hanno Becker7e5437a2017-04-28 17:15:26 +01005351{
Gilles Peskine449bd832023-01-11 14:50:10 +01005352 switch (type) {
Hanno Becker7e5437a2017-04-28 17:15:26 +01005353 case MBEDTLS_PK_RSA:
Gilles Peskine449bd832023-01-11 14:50:10 +01005354 return MBEDTLS_SSL_SIG_RSA;
Hanno Becker7e5437a2017-04-28 17:15:26 +01005355 case MBEDTLS_PK_ECDSA:
5356 case MBEDTLS_PK_ECKEY:
Gilles Peskine449bd832023-01-11 14:50:10 +01005357 return MBEDTLS_SSL_SIG_ECDSA;
Hanno Becker7e5437a2017-04-28 17:15:26 +01005358 default:
Gilles Peskine449bd832023-01-11 14:50:10 +01005359 return MBEDTLS_SSL_SIG_ANON;
Hanno Becker7e5437a2017-04-28 17:15:26 +01005360 }
5361}
5362
Gilles Peskine449bd832023-01-11 14:50:10 +01005363mbedtls_pk_type_t mbedtls_ssl_pk_alg_from_sig(unsigned char sig)
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02005364{
Gilles Peskine449bd832023-01-11 14:50:10 +01005365 switch (sig) {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005366#if defined(MBEDTLS_RSA_C)
5367 case MBEDTLS_SSL_SIG_RSA:
Gilles Peskine449bd832023-01-11 14:50:10 +01005368 return MBEDTLS_PK_RSA;
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02005369#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005370#if defined(MBEDTLS_ECDSA_C)
5371 case MBEDTLS_SSL_SIG_ECDSA:
Gilles Peskine449bd832023-01-11 14:50:10 +01005372 return MBEDTLS_PK_ECDSA;
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02005373#endif
5374 default:
Gilles Peskine449bd832023-01-11 14:50:10 +01005375 return MBEDTLS_PK_NONE;
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02005376 }
5377}
Manuel Pégourié-Gonnard5674a972015-10-19 15:14:03 +02005378#endif /* MBEDTLS_PK_C && ( MBEDTLS_RSA_C || MBEDTLS_ECDSA_C ) */
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02005379
Manuel Pégourié-Gonnard1a483832013-09-20 12:29:15 +02005380/*
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +02005381 * Convert from MBEDTLS_SSL_HASH_XXX to MBEDTLS_MD_XXX
Manuel Pégourié-Gonnard1a483832013-09-20 12:29:15 +02005382 */
Gilles Peskine449bd832023-01-11 14:50:10 +01005383mbedtls_md_type_t mbedtls_ssl_md_alg_from_hash(unsigned char hash)
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02005384{
Gilles Peskine449bd832023-01-11 14:50:10 +01005385 switch (hash) {
Andrzej Kurek25f27152022-08-17 16:09:31 -04005386#if defined(MBEDTLS_HAS_ALG_MD5_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005387 case MBEDTLS_SSL_HASH_MD5:
Gilles Peskine449bd832023-01-11 14:50:10 +01005388 return MBEDTLS_MD_MD5;
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02005389#endif
Andrzej Kurek25f27152022-08-17 16:09:31 -04005390#if defined(MBEDTLS_HAS_ALG_SHA_1_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005391 case MBEDTLS_SSL_HASH_SHA1:
Gilles Peskine449bd832023-01-11 14:50:10 +01005392 return MBEDTLS_MD_SHA1;
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02005393#endif
Andrzej Kurek25f27152022-08-17 16:09:31 -04005394#if defined(MBEDTLS_HAS_ALG_SHA_224_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005395 case MBEDTLS_SSL_HASH_SHA224:
Gilles Peskine449bd832023-01-11 14:50:10 +01005396 return MBEDTLS_MD_SHA224;
Mateusz Starzyke3c48b42021-04-19 16:46:28 +02005397#endif
Andrzej Kurek25f27152022-08-17 16:09:31 -04005398#if defined(MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005399 case MBEDTLS_SSL_HASH_SHA256:
Gilles Peskine449bd832023-01-11 14:50:10 +01005400 return MBEDTLS_MD_SHA256;
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02005401#endif
Andrzej Kurek25f27152022-08-17 16:09:31 -04005402#if defined(MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005403 case MBEDTLS_SSL_HASH_SHA384:
Gilles Peskine449bd832023-01-11 14:50:10 +01005404 return MBEDTLS_MD_SHA384;
Mateusz Starzyk3352a532021-04-06 14:28:22 +02005405#endif
Andrzej Kurek25f27152022-08-17 16:09:31 -04005406#if defined(MBEDTLS_HAS_ALG_SHA_512_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005407 case MBEDTLS_SSL_HASH_SHA512:
Gilles Peskine449bd832023-01-11 14:50:10 +01005408 return MBEDTLS_MD_SHA512;
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02005409#endif
5410 default:
Gilles Peskine449bd832023-01-11 14:50:10 +01005411 return MBEDTLS_MD_NONE;
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02005412 }
5413}
5414
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +02005415/*
5416 * Convert from MBEDTLS_MD_XXX to MBEDTLS_SSL_HASH_XXX
5417 */
Gilles Peskine449bd832023-01-11 14:50:10 +01005418unsigned char mbedtls_ssl_hash_from_md_alg(int md)
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +02005419{
Gilles Peskine449bd832023-01-11 14:50:10 +01005420 switch (md) {
Andrzej Kurek25f27152022-08-17 16:09:31 -04005421#if defined(MBEDTLS_HAS_ALG_MD5_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +02005422 case MBEDTLS_MD_MD5:
Gilles Peskine449bd832023-01-11 14:50:10 +01005423 return MBEDTLS_SSL_HASH_MD5;
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +02005424#endif
Andrzej Kurek25f27152022-08-17 16:09:31 -04005425#if defined(MBEDTLS_HAS_ALG_SHA_1_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +02005426 case MBEDTLS_MD_SHA1:
Gilles Peskine449bd832023-01-11 14:50:10 +01005427 return MBEDTLS_SSL_HASH_SHA1;
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +02005428#endif
Andrzej Kurek25f27152022-08-17 16:09:31 -04005429#if defined(MBEDTLS_HAS_ALG_SHA_224_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +02005430 case MBEDTLS_MD_SHA224:
Gilles Peskine449bd832023-01-11 14:50:10 +01005431 return MBEDTLS_SSL_HASH_SHA224;
Mateusz Starzyke3c48b42021-04-19 16:46:28 +02005432#endif
Andrzej Kurek25f27152022-08-17 16:09:31 -04005433#if defined(MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +02005434 case MBEDTLS_MD_SHA256:
Gilles Peskine449bd832023-01-11 14:50:10 +01005435 return MBEDTLS_SSL_HASH_SHA256;
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +02005436#endif
Andrzej Kurek25f27152022-08-17 16:09:31 -04005437#if defined(MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +02005438 case MBEDTLS_MD_SHA384:
Gilles Peskine449bd832023-01-11 14:50:10 +01005439 return MBEDTLS_SSL_HASH_SHA384;
Mateusz Starzyk3352a532021-04-06 14:28:22 +02005440#endif
Andrzej Kurek25f27152022-08-17 16:09:31 -04005441#if defined(MBEDTLS_HAS_ALG_SHA_512_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +02005442 case MBEDTLS_MD_SHA512:
Gilles Peskine449bd832023-01-11 14:50:10 +01005443 return MBEDTLS_SSL_HASH_SHA512;
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +02005444#endif
5445 default:
Gilles Peskine449bd832023-01-11 14:50:10 +01005446 return MBEDTLS_SSL_HASH_NONE;
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +02005447 }
5448}
5449
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01005450/*
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +02005451 * Check if a curve proposed by the peer is in our list.
Manuel Pégourié-Gonnard9d412d82015-06-17 12:10:46 +02005452 * Return 0 if we're willing to use it, -1 otherwise.
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01005453 */
Gilles Peskine449bd832023-01-11 14:50:10 +01005454int mbedtls_ssl_check_curve_tls_id(const mbedtls_ssl_context *ssl, uint16_t tls_id)
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01005455{
Gilles Peskine449bd832023-01-11 14:50:10 +01005456 const uint16_t *group_list = mbedtls_ssl_get_groups(ssl);
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01005457
Gilles Peskine449bd832023-01-11 14:50:10 +01005458 if (group_list == NULL) {
5459 return -1;
Brett Warrene0edc842021-08-17 09:53:13 +01005460 }
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01005461
Gilles Peskine449bd832023-01-11 14:50:10 +01005462 for (; *group_list != 0; group_list++) {
5463 if (*group_list == tls_id) {
5464 return 0;
5465 }
5466 }
5467
5468 return -1;
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01005469}
Manuel Pégourié-Gonnard0d63b842022-01-18 13:10:56 +01005470
5471#if defined(MBEDTLS_ECP_C)
5472/*
5473 * Same as mbedtls_ssl_check_curve_tls_id() but with a mbedtls_ecp_group_id.
5474 */
Gilles Peskine449bd832023-01-11 14:50:10 +01005475int mbedtls_ssl_check_curve(const mbedtls_ssl_context *ssl, mbedtls_ecp_group_id grp_id)
Manuel Pégourié-Gonnard0d63b842022-01-18 13:10:56 +01005476{
Gilles Peskine449bd832023-01-11 14:50:10 +01005477 uint16_t tls_id = mbedtls_ssl_get_tls_id_from_ecp_group_id(grp_id);
Leonid Rozenboim19e59732022-08-08 16:52:38 -07005478
Gilles Peskine449bd832023-01-11 14:50:10 +01005479 if (tls_id == 0) {
Leonid Rozenboim19e59732022-08-08 16:52:38 -07005480 return -1;
Gilles Peskine449bd832023-01-11 14:50:10 +01005481 }
Leonid Rozenboim19e59732022-08-08 16:52:38 -07005482
Gilles Peskine449bd832023-01-11 14:50:10 +01005483 return mbedtls_ssl_check_curve_tls_id(ssl, tls_id);
Manuel Pégourié-Gonnard0d63b842022-01-18 13:10:56 +01005484}
Manuel Pégourié-Gonnardb541da62015-06-17 11:43:30 +02005485#endif /* MBEDTLS_ECP_C */
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02005486
Gilles Peskine449bd832023-01-11 14:50:10 +01005487#if defined(MBEDTLS_DEBUG_C)
Valerio Setti67419f02023-01-04 16:12:42 +01005488#define EC_NAME(_name_) _name_
5489#else
5490#define EC_NAME(_name_) NULL
5491#endif
5492
Valerio Setti18c9fed2022-12-30 17:44:24 +01005493static const struct {
5494 uint16_t tls_id;
5495 mbedtls_ecp_group_id ecp_group_id;
5496 psa_ecc_family_t psa_family;
5497 uint16_t bits;
Gilles Peskine449bd832023-01-11 14:50:10 +01005498 const char *name;
Valerio Setti18c9fed2022-12-30 17:44:24 +01005499} tls_id_match_table[] =
5500{
5501#if defined(MBEDTLS_ECP_DP_SECP521R1_ENABLED) || defined(PSA_WANT_ECC_SECP_R1_521)
Gilles Peskine449bd832023-01-11 14:50:10 +01005502 { 25, MBEDTLS_ECP_DP_SECP521R1, PSA_ECC_FAMILY_SECP_R1, 521, EC_NAME("secp521r1") },
Valerio Setti18c9fed2022-12-30 17:44:24 +01005503#endif
5504#if defined(MBEDTLS_ECP_DP_BP512R1_ENABLED) || defined(PSA_WANT_ECC_BRAINPOOL_P_R1_512)
Gilles Peskine449bd832023-01-11 14:50:10 +01005505 { 28, MBEDTLS_ECP_DP_BP512R1, PSA_ECC_FAMILY_BRAINPOOL_P_R1, 512, EC_NAME("brainpoolP512r1") },
Valerio Setti18c9fed2022-12-30 17:44:24 +01005506#endif
Valerio Setti67419f02023-01-04 16:12:42 +01005507#if defined(MBEDTLS_ECP_DP_SECP384R1_ENABLED) || defined(PSA_WANT_ECC_SECP_R1_384)
Gilles Peskine449bd832023-01-11 14:50:10 +01005508 { 24, MBEDTLS_ECP_DP_SECP384R1, PSA_ECC_FAMILY_SECP_R1, 384, EC_NAME("secp384r1") },
Valerio Setti18c9fed2022-12-30 17:44:24 +01005509#endif
Valerio Setti67419f02023-01-04 16:12:42 +01005510#if defined(MBEDTLS_ECP_DP_BP384R1_ENABLED) || defined(PSA_WANT_ECC_BRAINPOOL_P_R1_384)
Gilles Peskine449bd832023-01-11 14:50:10 +01005511 { 27, MBEDTLS_ECP_DP_BP384R1, PSA_ECC_FAMILY_BRAINPOOL_P_R1, 384, EC_NAME("brainpoolP384r1") },
Valerio Setti18c9fed2022-12-30 17:44:24 +01005512#endif
Valerio Setti67419f02023-01-04 16:12:42 +01005513#if defined(MBEDTLS_ECP_DP_SECP256R1_ENABLED) || defined(PSA_WANT_ECC_SECP_R1_256)
Gilles Peskine449bd832023-01-11 14:50:10 +01005514 { 23, MBEDTLS_ECP_DP_SECP256R1, PSA_ECC_FAMILY_SECP_R1, 256, EC_NAME("secp256r1") },
Valerio Setti18c9fed2022-12-30 17:44:24 +01005515#endif
5516#if defined(MBEDTLS_ECP_DP_SECP256K1_ENABLED) || defined(PSA_WANT_ECC_SECP_K1_256)
Gilles Peskine449bd832023-01-11 14:50:10 +01005517 { 22, MBEDTLS_ECP_DP_SECP256K1, PSA_ECC_FAMILY_SECP_K1, 256, EC_NAME("secp256k1") },
Valerio Setti18c9fed2022-12-30 17:44:24 +01005518#endif
Valerio Setti67419f02023-01-04 16:12:42 +01005519#if defined(MBEDTLS_ECP_DP_BP256R1_ENABLED) || defined(PSA_WANT_ECC_BRAINPOOL_P_R1_256)
Gilles Peskine449bd832023-01-11 14:50:10 +01005520 { 26, MBEDTLS_ECP_DP_BP256R1, PSA_ECC_FAMILY_BRAINPOOL_P_R1, 256, EC_NAME("brainpoolP256r1") },
Valerio Setti18c9fed2022-12-30 17:44:24 +01005521#endif
5522#if defined(MBEDTLS_ECP_DP_SECP224R1_ENABLED) || defined(PSA_WANT_ECC_SECP_R1_224)
Gilles Peskine449bd832023-01-11 14:50:10 +01005523 { 21, MBEDTLS_ECP_DP_SECP224R1, PSA_ECC_FAMILY_SECP_R1, 224, EC_NAME("secp224r1") },
Valerio Setti18c9fed2022-12-30 17:44:24 +01005524#endif
5525#if defined(MBEDTLS_ECP_DP_SECP224K1_ENABLED) || defined(PSA_WANT_ECC_SECP_K1_224)
Gilles Peskine449bd832023-01-11 14:50:10 +01005526 { 20, MBEDTLS_ECP_DP_SECP224K1, PSA_ECC_FAMILY_SECP_K1, 224, EC_NAME("secp224k1") },
Valerio Setti18c9fed2022-12-30 17:44:24 +01005527#endif
5528#if defined(MBEDTLS_ECP_DP_SECP192R1_ENABLED) || defined(PSA_WANT_ECC_SECP_R1_192)
Gilles Peskine449bd832023-01-11 14:50:10 +01005529 { 19, MBEDTLS_ECP_DP_SECP192R1, PSA_ECC_FAMILY_SECP_R1, 192, EC_NAME("secp192r1") },
Valerio Setti18c9fed2022-12-30 17:44:24 +01005530#endif
5531#if defined(MBEDTLS_ECP_DP_SECP192K1_ENABLED) || defined(PSA_WANT_ECC_SECP_K1_192)
Gilles Peskine449bd832023-01-11 14:50:10 +01005532 { 18, MBEDTLS_ECP_DP_SECP192K1, PSA_ECC_FAMILY_SECP_K1, 192, EC_NAME("secp192k1") },
Valerio Setti18c9fed2022-12-30 17:44:24 +01005533#endif
5534#if defined(MBEDTLS_ECP_DP_CURVE25519_ENABLED) || defined(PSA_WANT_ECC_MONTGOMERY_255)
Gilles Peskine449bd832023-01-11 14:50:10 +01005535 { 29, MBEDTLS_ECP_DP_CURVE25519, PSA_ECC_FAMILY_MONTGOMERY, 255, EC_NAME("x25519") },
Valerio Setti18c9fed2022-12-30 17:44:24 +01005536#endif
5537#if defined(MBEDTLS_ECP_DP_CURVE448_ENABLED) || defined(PSA_WANT_ECC_MONTGOMERY_448)
Gilles Peskine449bd832023-01-11 14:50:10 +01005538 { 30, MBEDTLS_ECP_DP_CURVE448, PSA_ECC_FAMILY_MONTGOMERY, 448, EC_NAME("x448") },
Valerio Setti18c9fed2022-12-30 17:44:24 +01005539#endif
5540 { 0, MBEDTLS_ECP_DP_NONE, 0, 0, NULL },
5541};
5542
Gilles Peskine449bd832023-01-11 14:50:10 +01005543int mbedtls_ssl_get_psa_curve_info_from_tls_id(uint16_t tls_id,
5544 psa_ecc_family_t *family,
5545 size_t *bits)
Valerio Setti18c9fed2022-12-30 17:44:24 +01005546{
Gilles Peskine449bd832023-01-11 14:50:10 +01005547 for (int i = 0; tls_id_match_table[i].tls_id != 0; i++) {
5548 if (tls_id_match_table[i].tls_id == tls_id) {
5549 if (family != NULL) {
Valerio Setti18c9fed2022-12-30 17:44:24 +01005550 *family = tls_id_match_table[i].psa_family;
Gilles Peskine449bd832023-01-11 14:50:10 +01005551 }
5552 if (bits != NULL) {
Valerio Setti18c9fed2022-12-30 17:44:24 +01005553 *bits = tls_id_match_table[i].bits;
Gilles Peskine449bd832023-01-11 14:50:10 +01005554 }
Valerio Setti18c9fed2022-12-30 17:44:24 +01005555 return PSA_SUCCESS;
5556 }
5557 }
5558
5559 return PSA_ERROR_NOT_SUPPORTED;
5560}
5561
Gilles Peskine449bd832023-01-11 14:50:10 +01005562mbedtls_ecp_group_id mbedtls_ssl_get_ecp_group_id_from_tls_id(uint16_t tls_id)
Valerio Setti18c9fed2022-12-30 17:44:24 +01005563{
Gilles Peskine449bd832023-01-11 14:50:10 +01005564 for (int i = 0; tls_id_match_table[i].tls_id != 0; i++) {
5565 if (tls_id_match_table[i].tls_id == tls_id) {
Valerio Setti18c9fed2022-12-30 17:44:24 +01005566 return tls_id_match_table[i].ecp_group_id;
Gilles Peskine449bd832023-01-11 14:50:10 +01005567 }
Valerio Setti18c9fed2022-12-30 17:44:24 +01005568 }
5569
5570 return MBEDTLS_ECP_DP_NONE;
5571}
5572
Gilles Peskine449bd832023-01-11 14:50:10 +01005573uint16_t mbedtls_ssl_get_tls_id_from_ecp_group_id(mbedtls_ecp_group_id grp_id)
Valerio Setti18c9fed2022-12-30 17:44:24 +01005574{
Gilles Peskine449bd832023-01-11 14:50:10 +01005575 for (int i = 0; tls_id_match_table[i].ecp_group_id != MBEDTLS_ECP_DP_NONE;
5576 i++) {
5577 if (tls_id_match_table[i].ecp_group_id == grp_id) {
Valerio Setti18c9fed2022-12-30 17:44:24 +01005578 return tls_id_match_table[i].tls_id;
Gilles Peskine449bd832023-01-11 14:50:10 +01005579 }
Valerio Setti18c9fed2022-12-30 17:44:24 +01005580 }
5581
5582 return 0;
5583}
5584
Valerio Setti67419f02023-01-04 16:12:42 +01005585#if defined(MBEDTLS_DEBUG_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01005586const char *mbedtls_ssl_get_curve_name_from_tls_id(uint16_t tls_id)
Valerio Setti18c9fed2022-12-30 17:44:24 +01005587{
Gilles Peskine449bd832023-01-11 14:50:10 +01005588 for (int i = 0; tls_id_match_table[i].tls_id != 0; i++) {
5589 if (tls_id_match_table[i].tls_id == tls_id) {
Valerio Setti18c9fed2022-12-30 17:44:24 +01005590 return tls_id_match_table[i].name;
Gilles Peskine449bd832023-01-11 14:50:10 +01005591 }
Valerio Setti18c9fed2022-12-30 17:44:24 +01005592 }
5593
5594 return NULL;
5595}
Valerio Setti67419f02023-01-04 16:12:42 +01005596#endif
Valerio Setti18c9fed2022-12-30 17:44:24 +01005597
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005598#if defined(MBEDTLS_X509_CRT_PARSE_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01005599int mbedtls_ssl_check_cert_usage(const mbedtls_x509_crt *cert,
5600 const mbedtls_ssl_ciphersuite_t *ciphersuite,
5601 int cert_endpoint,
5602 uint32_t *flags)
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02005603{
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +01005604 int ret = 0;
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02005605 int usage = 0;
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +02005606 const char *ext_oid;
5607 size_t ext_len;
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +02005608
Gilles Peskine449bd832023-01-11 14:50:10 +01005609 if (cert_endpoint == MBEDTLS_SSL_IS_SERVER) {
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02005610 /* Server part of the key exchange */
Gilles Peskine449bd832023-01-11 14:50:10 +01005611 switch (ciphersuite->key_exchange) {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005612 case MBEDTLS_KEY_EXCHANGE_RSA:
5613 case MBEDTLS_KEY_EXCHANGE_RSA_PSK:
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +01005614 usage = MBEDTLS_X509_KU_KEY_ENCIPHERMENT;
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02005615 break;
5616
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005617 case MBEDTLS_KEY_EXCHANGE_DHE_RSA:
5618 case MBEDTLS_KEY_EXCHANGE_ECDHE_RSA:
5619 case MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA:
5620 usage = MBEDTLS_X509_KU_DIGITAL_SIGNATURE;
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02005621 break;
5622
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005623 case MBEDTLS_KEY_EXCHANGE_ECDH_RSA:
5624 case MBEDTLS_KEY_EXCHANGE_ECDH_ECDSA:
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +01005625 usage = MBEDTLS_X509_KU_KEY_AGREEMENT;
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02005626 break;
5627
5628 /* Don't use default: we want warnings when adding new values */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005629 case MBEDTLS_KEY_EXCHANGE_NONE:
5630 case MBEDTLS_KEY_EXCHANGE_PSK:
5631 case MBEDTLS_KEY_EXCHANGE_DHE_PSK:
5632 case MBEDTLS_KEY_EXCHANGE_ECDHE_PSK:
Manuel Pégourié-Gonnard557535d2015-09-15 17:53:32 +02005633 case MBEDTLS_KEY_EXCHANGE_ECJPAKE:
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02005634 usage = 0;
5635 }
Gilles Peskine449bd832023-01-11 14:50:10 +01005636 } else {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005637 /* Client auth: we only implement rsa_sign and mbedtls_ecdsa_sign for now */
5638 usage = MBEDTLS_X509_KU_DIGITAL_SIGNATURE;
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02005639 }
5640
Gilles Peskine449bd832023-01-11 14:50:10 +01005641 if (mbedtls_x509_crt_check_key_usage(cert, usage) != 0) {
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +01005642 *flags |= MBEDTLS_X509_BADCERT_KEY_USAGE;
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +01005643 ret = -1;
5644 }
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02005645
Gilles Peskine449bd832023-01-11 14:50:10 +01005646 if (cert_endpoint == MBEDTLS_SSL_IS_SERVER) {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005647 ext_oid = MBEDTLS_OID_SERVER_AUTH;
Gilles Peskine449bd832023-01-11 14:50:10 +01005648 ext_len = MBEDTLS_OID_SIZE(MBEDTLS_OID_SERVER_AUTH);
5649 } else {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005650 ext_oid = MBEDTLS_OID_CLIENT_AUTH;
Gilles Peskine449bd832023-01-11 14:50:10 +01005651 ext_len = MBEDTLS_OID_SIZE(MBEDTLS_OID_CLIENT_AUTH);
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +02005652 }
5653
Gilles Peskine449bd832023-01-11 14:50:10 +01005654 if (mbedtls_x509_crt_check_extended_key_usage(cert, ext_oid, ext_len) != 0) {
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +01005655 *flags |= MBEDTLS_X509_BADCERT_EXT_KEY_USAGE;
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +01005656 ret = -1;
5657 }
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +02005658
Gilles Peskine449bd832023-01-11 14:50:10 +01005659 return ret;
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02005660}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005661#endif /* MBEDTLS_X509_CRT_PARSE_C */
Manuel Pégourié-Gonnard3a306b92014-04-29 15:11:17 +02005662
Jerry Yu148165c2021-09-24 23:20:59 +08005663#if defined(MBEDTLS_USE_PSA_CRYPTO)
Gilles Peskine449bd832023-01-11 14:50:10 +01005664int mbedtls_ssl_get_handshake_transcript(mbedtls_ssl_context *ssl,
5665 const mbedtls_md_type_t md,
5666 unsigned char *dst,
5667 size_t dst_len,
5668 size_t *olen)
Jerry Yu148165c2021-09-24 23:20:59 +08005669{
Ronald Cronf6893e12022-01-07 22:09:01 +01005670 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
5671 psa_hash_operation_t *hash_operation_to_clone;
5672 psa_hash_operation_t hash_operation = psa_hash_operation_init();
5673
Jerry Yu148165c2021-09-24 23:20:59 +08005674 *olen = 0;
Ronald Cronf6893e12022-01-07 22:09:01 +01005675
Gilles Peskine449bd832023-01-11 14:50:10 +01005676 switch (md) {
Andrzej Kurek25f27152022-08-17 16:09:31 -04005677#if defined(MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Gilles Peskine449bd832023-01-11 14:50:10 +01005678 case MBEDTLS_MD_SHA384:
5679 hash_operation_to_clone = &ssl->handshake->fin_sha384_psa;
5680 break;
Ronald Cronf6893e12022-01-07 22:09:01 +01005681#endif
5682
Andrzej Kurek25f27152022-08-17 16:09:31 -04005683#if defined(MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Gilles Peskine449bd832023-01-11 14:50:10 +01005684 case MBEDTLS_MD_SHA256:
5685 hash_operation_to_clone = &ssl->handshake->fin_sha256_psa;
5686 break;
Ronald Cronf6893e12022-01-07 22:09:01 +01005687#endif
5688
Gilles Peskine449bd832023-01-11 14:50:10 +01005689 default:
5690 goto exit;
5691 }
5692
5693 status = psa_hash_clone(hash_operation_to_clone, &hash_operation);
5694 if (status != PSA_SUCCESS) {
Ronald Cronf6893e12022-01-07 22:09:01 +01005695 goto exit;
5696 }
5697
Gilles Peskine449bd832023-01-11 14:50:10 +01005698 status = psa_hash_finish(&hash_operation, dst, dst_len, olen);
5699 if (status != PSA_SUCCESS) {
Ronald Cronf6893e12022-01-07 22:09:01 +01005700 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01005701 }
Ronald Cronf6893e12022-01-07 22:09:01 +01005702
5703exit:
Andrzej Kurekeabeb302022-10-17 07:52:51 -04005704#if !defined(MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA) && \
5705 !defined(MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
5706 (void) ssl;
5707#endif
Gilles Peskine449bd832023-01-11 14:50:10 +01005708 return psa_ssl_status_to_mbedtls(status);
Jerry Yu148165c2021-09-24 23:20:59 +08005709}
5710#else /* MBEDTLS_USE_PSA_CRYPTO */
5711
Andrzej Kurek25f27152022-08-17 16:09:31 -04005712#if defined(MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02005713MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01005714static int ssl_get_handshake_transcript_sha384(mbedtls_ssl_context *ssl,
5715 unsigned char *dst,
5716 size_t dst_len,
5717 size_t *olen)
Jerry Yu24c0ec32021-09-09 14:21:07 +08005718{
Jerry Yu24c0ec32021-09-09 14:21:07 +08005719 int ret;
5720 mbedtls_sha512_context sha512;
5721
Gilles Peskine449bd832023-01-11 14:50:10 +01005722 if (dst_len < 48) {
5723 return MBEDTLS_ERR_SSL_INTERNAL_ERROR;
5724 }
Jerry Yu24c0ec32021-09-09 14:21:07 +08005725
Gilles Peskine449bd832023-01-11 14:50:10 +01005726 mbedtls_sha512_init(&sha512);
5727 mbedtls_sha512_clone(&sha512, &ssl->handshake->fin_sha384);
Jerry Yu24c0ec32021-09-09 14:21:07 +08005728
Gilles Peskine449bd832023-01-11 14:50:10 +01005729 if ((ret = mbedtls_sha512_finish(&sha512, dst)) != 0) {
5730 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_sha512_finish", ret);
Jerry Yu24c0ec32021-09-09 14:21:07 +08005731 goto exit;
5732 }
5733
5734 *olen = 48;
5735
5736exit:
5737
Gilles Peskine449bd832023-01-11 14:50:10 +01005738 mbedtls_sha512_free(&sha512);
5739 return ret;
Jerry Yu24c0ec32021-09-09 14:21:07 +08005740}
Andrzej Kurek25f27152022-08-17 16:09:31 -04005741#endif /* MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA */
Jerry Yu24c0ec32021-09-09 14:21:07 +08005742
Andrzej Kurek25f27152022-08-17 16:09:31 -04005743#if defined(MBEDTLS_HAS_ALG_SHA_256_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_sha256(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_sha256_context sha256;
5752
Gilles Peskine449bd832023-01-11 14:50:10 +01005753 if (dst_len < 32) {
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_sha256_init(&sha256);
5758 mbedtls_sha256_clone(&sha256, &ssl->handshake->fin_sha256);
Jerry Yuc5aef882021-12-23 20:15:02 +08005759
Gilles Peskine449bd832023-01-11 14:50:10 +01005760 if ((ret = mbedtls_sha256_finish(&sha256, dst)) != 0) {
5761 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_sha256_finish", ret);
Jerry Yu24c0ec32021-09-09 14:21:07 +08005762 goto exit;
5763 }
5764
5765 *olen = 32;
5766
5767exit:
5768
Gilles Peskine449bd832023-01-11 14:50:10 +01005769 mbedtls_sha256_free(&sha256);
5770 return ret;
Jerry Yu24c0ec32021-09-09 14:21:07 +08005771}
Andrzej Kurek25f27152022-08-17 16:09:31 -04005772#endif /* MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA */
Jerry Yu24c0ec32021-09-09 14:21:07 +08005773
Gilles Peskine449bd832023-01-11 14:50:10 +01005774int mbedtls_ssl_get_handshake_transcript(mbedtls_ssl_context *ssl,
5775 const mbedtls_md_type_t md,
5776 unsigned char *dst,
5777 size_t dst_len,
5778 size_t *olen)
Jerry Yu24c0ec32021-09-09 14:21:07 +08005779{
Gilles Peskine449bd832023-01-11 14:50:10 +01005780 switch (md) {
Jerry Yuc1ddeef2021-10-08 15:14:45 +08005781
Andrzej Kurek25f27152022-08-17 16:09:31 -04005782#if defined(MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Gilles Peskine449bd832023-01-11 14:50:10 +01005783 case MBEDTLS_MD_SHA384:
5784 return ssl_get_handshake_transcript_sha384(ssl, dst, dst_len, olen);
Andrzej Kurekcccb0442022-08-19 03:42:11 -04005785#endif /* MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA*/
Jerry Yuc1ddeef2021-10-08 15:14:45 +08005786
Andrzej Kurek25f27152022-08-17 16:09:31 -04005787#if defined(MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Gilles Peskine449bd832023-01-11 14:50:10 +01005788 case MBEDTLS_MD_SHA256:
5789 return ssl_get_handshake_transcript_sha256(ssl, dst, dst_len, olen);
Andrzej Kurekcccb0442022-08-19 03:42:11 -04005790#endif /* MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA*/
Jerry Yuc1ddeef2021-10-08 15:14:45 +08005791
Gilles Peskine449bd832023-01-11 14:50:10 +01005792 default:
Andrzej Kurek409248a2022-10-24 10:33:21 -04005793#if !defined(MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA) && \
Gilles Peskine449bd832023-01-11 14:50:10 +01005794 !defined(MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
5795 (void) ssl;
5796 (void) dst;
5797 (void) dst_len;
5798 (void) olen;
Andrzej Kurek409248a2022-10-24 10:33:21 -04005799#endif
Gilles Peskine449bd832023-01-11 14:50:10 +01005800 break;
Jerry Yuc1ddeef2021-10-08 15:14:45 +08005801 }
Gilles Peskine449bd832023-01-11 14:50:10 +01005802 return MBEDTLS_ERR_SSL_INTERNAL_ERROR;
Jerry Yu24c0ec32021-09-09 14:21:07 +08005803}
XiaokangQian647719a2021-12-07 09:16:29 +00005804
Jerry Yu148165c2021-09-24 23:20:59 +08005805#endif /* !MBEDTLS_USE_PSA_CRYPTO */
Jerry Yu24c0ec32021-09-09 14:21:07 +08005806
Ronald Crone68ab4f2022-10-05 12:46:29 +02005807#if defined(MBEDTLS_SSL_HANDSHAKE_WITH_CERT_ENABLED)
Gabor Mezei078e8032022-04-27 21:17:56 +02005808/* mbedtls_ssl_parse_sig_alg_ext()
5809 *
5810 * The `extension_data` field of signature algorithm contains a `SignatureSchemeList`
5811 * value (TLS 1.3 RFC8446):
5812 * enum {
5813 * ....
5814 * ecdsa_secp256r1_sha256( 0x0403 ),
5815 * ecdsa_secp384r1_sha384( 0x0503 ),
5816 * ecdsa_secp521r1_sha512( 0x0603 ),
5817 * ....
5818 * } SignatureScheme;
5819 *
5820 * struct {
5821 * SignatureScheme supported_signature_algorithms<2..2^16-2>;
5822 * } SignatureSchemeList;
5823 *
5824 * The `extension_data` field of signature algorithm contains a `SignatureAndHashAlgorithm`
5825 * value (TLS 1.2 RFC5246):
5826 * enum {
5827 * none(0), md5(1), sha1(2), sha224(3), sha256(4), sha384(5),
5828 * sha512(6), (255)
5829 * } HashAlgorithm;
5830 *
5831 * enum { anonymous(0), rsa(1), dsa(2), ecdsa(3), (255) }
5832 * SignatureAlgorithm;
5833 *
5834 * struct {
5835 * HashAlgorithm hash;
5836 * SignatureAlgorithm signature;
5837 * } SignatureAndHashAlgorithm;
5838 *
5839 * SignatureAndHashAlgorithm
5840 * supported_signature_algorithms<2..2^16-2>;
5841 *
5842 * The TLS 1.3 signature algorithm extension was defined to be a compatible
5843 * generalization of the TLS 1.2 signature algorithm extension.
5844 * `SignatureAndHashAlgorithm` field of TLS 1.2 can be represented by
5845 * `SignatureScheme` field of TLS 1.3
5846 *
5847 */
Gilles Peskine449bd832023-01-11 14:50:10 +01005848int mbedtls_ssl_parse_sig_alg_ext(mbedtls_ssl_context *ssl,
5849 const unsigned char *buf,
5850 const unsigned char *end)
Gabor Mezei078e8032022-04-27 21:17:56 +02005851{
5852 const unsigned char *p = buf;
5853 size_t supported_sig_algs_len = 0;
5854 const unsigned char *supported_sig_algs_end;
5855 uint16_t sig_alg;
5856 uint32_t common_idx = 0;
5857
Gilles Peskine449bd832023-01-11 14:50:10 +01005858 MBEDTLS_SSL_CHK_BUF_READ_PTR(p, end, 2);
5859 supported_sig_algs_len = MBEDTLS_GET_UINT16_BE(p, 0);
Gabor Mezei078e8032022-04-27 21:17:56 +02005860 p += 2;
5861
Gilles Peskine449bd832023-01-11 14:50:10 +01005862 memset(ssl->handshake->received_sig_algs, 0,
5863 sizeof(ssl->handshake->received_sig_algs));
Gabor Mezei078e8032022-04-27 21:17:56 +02005864
Gilles Peskine449bd832023-01-11 14:50:10 +01005865 MBEDTLS_SSL_CHK_BUF_READ_PTR(p, end, supported_sig_algs_len);
Gabor Mezei078e8032022-04-27 21:17:56 +02005866 supported_sig_algs_end = p + supported_sig_algs_len;
Gilles Peskine449bd832023-01-11 14:50:10 +01005867 while (p < supported_sig_algs_end) {
5868 MBEDTLS_SSL_CHK_BUF_READ_PTR(p, supported_sig_algs_end, 2);
5869 sig_alg = MBEDTLS_GET_UINT16_BE(p, 0);
Gabor Mezei078e8032022-04-27 21:17:56 +02005870 p += 2;
Gilles Peskine449bd832023-01-11 14:50:10 +01005871 MBEDTLS_SSL_DEBUG_MSG(4, ("received signature algorithm: 0x%x %s",
5872 sig_alg,
5873 mbedtls_ssl_sig_alg_to_str(sig_alg)));
Jerry Yu2fe6c632022-06-29 10:02:38 +08005874#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
Gilles Peskine449bd832023-01-11 14:50:10 +01005875 if (ssl->tls_version == MBEDTLS_SSL_VERSION_TLS1_2 &&
5876 (!(mbedtls_ssl_sig_alg_is_supported(ssl, sig_alg) &&
5877 mbedtls_ssl_sig_alg_is_offered(ssl, sig_alg)))) {
Gabor Mezei078e8032022-04-27 21:17:56 +02005878 continue;
Jerry Yu2fe6c632022-06-29 10:02:38 +08005879 }
5880#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Gabor Mezei078e8032022-04-27 21:17:56 +02005881
Gilles Peskine449bd832023-01-11 14:50:10 +01005882 MBEDTLS_SSL_DEBUG_MSG(4, ("valid signature algorithm: %s",
5883 mbedtls_ssl_sig_alg_to_str(sig_alg)));
Gabor Mezei078e8032022-04-27 21:17:56 +02005884
Gilles Peskine449bd832023-01-11 14:50:10 +01005885 if (common_idx + 1 < MBEDTLS_RECEIVED_SIG_ALGS_SIZE) {
Gabor Mezei078e8032022-04-27 21:17:56 +02005886 ssl->handshake->received_sig_algs[common_idx] = sig_alg;
5887 common_idx += 1;
5888 }
5889 }
5890 /* Check that we consumed all the message. */
Gilles Peskine449bd832023-01-11 14:50:10 +01005891 if (p != end) {
5892 MBEDTLS_SSL_DEBUG_MSG(1,
5893 ("Signature algorithms extension length misaligned"));
5894 MBEDTLS_SSL_PEND_FATAL_ALERT(MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR,
5895 MBEDTLS_ERR_SSL_DECODE_ERROR);
5896 return MBEDTLS_ERR_SSL_DECODE_ERROR;
Gabor Mezei078e8032022-04-27 21:17:56 +02005897 }
5898
Gilles Peskine449bd832023-01-11 14:50:10 +01005899 if (common_idx == 0) {
5900 MBEDTLS_SSL_DEBUG_MSG(3, ("no signature algorithm in common"));
5901 MBEDTLS_SSL_PEND_FATAL_ALERT(MBEDTLS_SSL_ALERT_MSG_HANDSHAKE_FAILURE,
5902 MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE);
5903 return MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE;
Gabor Mezei078e8032022-04-27 21:17:56 +02005904 }
5905
Gabor Mezei15b95a62022-05-09 16:37:58 +02005906 ssl->handshake->received_sig_algs[common_idx] = MBEDTLS_TLS_SIG_NONE;
Gilles Peskine449bd832023-01-11 14:50:10 +01005907 return 0;
Gabor Mezei078e8032022-04-27 21:17:56 +02005908}
5909
Ronald Crone68ab4f2022-10-05 12:46:29 +02005910#endif /* MBEDTLS_SSL_HANDSHAKE_WITH_CERT_ENABLED */
Gabor Mezei078e8032022-04-27 21:17:56 +02005911
Jerry Yudc7bd172022-02-17 13:44:15 +08005912#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
5913
5914#if defined(MBEDTLS_USE_PSA_CRYPTO)
5915
Gilles Peskine449bd832023-01-11 14:50:10 +01005916static psa_status_t setup_psa_key_derivation(psa_key_derivation_operation_t *derivation,
5917 mbedtls_svc_key_id_t key,
5918 psa_algorithm_t alg,
5919 const unsigned char *raw_psk, size_t raw_psk_length,
5920 const unsigned char *seed, size_t seed_length,
5921 const unsigned char *label, size_t label_length,
5922 const unsigned char *other_secret,
5923 size_t other_secret_length,
5924 size_t capacity)
Jerry Yudc7bd172022-02-17 13:44:15 +08005925{
5926 psa_status_t status;
5927
Gilles Peskine449bd832023-01-11 14:50:10 +01005928 status = psa_key_derivation_setup(derivation, alg);
5929 if (status != PSA_SUCCESS) {
5930 return status;
5931 }
Jerry Yudc7bd172022-02-17 13:44:15 +08005932
Gilles Peskine449bd832023-01-11 14:50:10 +01005933 if (PSA_ALG_IS_TLS12_PRF(alg) || PSA_ALG_IS_TLS12_PSK_TO_MS(alg)) {
5934 status = psa_key_derivation_input_bytes(derivation,
5935 PSA_KEY_DERIVATION_INPUT_SEED,
5936 seed, seed_length);
5937 if (status != PSA_SUCCESS) {
5938 return status;
Przemek Stekiel1f027032022-04-05 17:12:11 +02005939 }
5940
Gilles Peskine449bd832023-01-11 14:50:10 +01005941 if (other_secret != NULL) {
5942 status = psa_key_derivation_input_bytes(derivation,
5943 PSA_KEY_DERIVATION_INPUT_OTHER_SECRET,
5944 other_secret, other_secret_length);
5945 if (status != PSA_SUCCESS) {
5946 return status;
5947 }
5948 }
5949
5950 if (mbedtls_svc_key_id_is_null(key)) {
Jerry Yudc7bd172022-02-17 13:44:15 +08005951 status = psa_key_derivation_input_bytes(
5952 derivation, PSA_KEY_DERIVATION_INPUT_SECRET,
Gilles Peskine449bd832023-01-11 14:50:10 +01005953 raw_psk, raw_psk_length);
5954 } else {
Jerry Yudc7bd172022-02-17 13:44:15 +08005955 status = psa_key_derivation_input_key(
Gilles Peskine449bd832023-01-11 14:50:10 +01005956 derivation, PSA_KEY_DERIVATION_INPUT_SECRET, key);
Jerry Yudc7bd172022-02-17 13:44:15 +08005957 }
Gilles Peskine449bd832023-01-11 14:50:10 +01005958 if (status != PSA_SUCCESS) {
5959 return status;
5960 }
Jerry Yudc7bd172022-02-17 13:44:15 +08005961
Gilles Peskine449bd832023-01-11 14:50:10 +01005962 status = psa_key_derivation_input_bytes(derivation,
5963 PSA_KEY_DERIVATION_INPUT_LABEL,
5964 label, label_length);
5965 if (status != PSA_SUCCESS) {
5966 return status;
5967 }
5968 } else {
5969 return PSA_ERROR_NOT_SUPPORTED;
Jerry Yudc7bd172022-02-17 13:44:15 +08005970 }
5971
Gilles Peskine449bd832023-01-11 14:50:10 +01005972 status = psa_key_derivation_set_capacity(derivation, capacity);
5973 if (status != PSA_SUCCESS) {
5974 return status;
5975 }
Jerry Yudc7bd172022-02-17 13:44:15 +08005976
Gilles Peskine449bd832023-01-11 14:50:10 +01005977 return PSA_SUCCESS;
Jerry Yudc7bd172022-02-17 13:44:15 +08005978}
5979
Andrzej Kurek57d10632022-10-24 10:32:01 -04005980#if defined(PSA_WANT_ALG_SHA_384) || \
5981 defined(PSA_WANT_ALG_SHA_256)
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02005982MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01005983static int tls_prf_generic(mbedtls_md_type_t md_type,
5984 const unsigned char *secret, size_t slen,
5985 const char *label,
5986 const unsigned char *random, size_t rlen,
5987 unsigned char *dstbuf, size_t dlen)
Jerry Yudc7bd172022-02-17 13:44:15 +08005988{
5989 psa_status_t status;
5990 psa_algorithm_t alg;
5991 mbedtls_svc_key_id_t master_key = MBEDTLS_SVC_KEY_ID_INIT;
5992 psa_key_derivation_operation_t derivation =
5993 PSA_KEY_DERIVATION_OPERATION_INIT;
5994
Gilles Peskine449bd832023-01-11 14:50:10 +01005995 if (md_type == MBEDTLS_MD_SHA384) {
Jerry Yudc7bd172022-02-17 13:44:15 +08005996 alg = PSA_ALG_TLS12_PRF(PSA_ALG_SHA_384);
Gilles Peskine449bd832023-01-11 14:50:10 +01005997 } else {
Jerry Yudc7bd172022-02-17 13:44:15 +08005998 alg = PSA_ALG_TLS12_PRF(PSA_ALG_SHA_256);
Gilles Peskine449bd832023-01-11 14:50:10 +01005999 }
Jerry Yudc7bd172022-02-17 13:44:15 +08006000
6001 /* Normally a "secret" should be long enough to be impossible to
6002 * find by brute force, and in particular should not be empty. But
6003 * this PRF is also used to derive an IV, in particular in EAP-TLS,
6004 * and for this use case it makes sense to have a 0-length "secret".
6005 * Since the key API doesn't allow importing a key of length 0,
6006 * keep master_key=0, which setup_psa_key_derivation() understands
6007 * to mean a 0-length "secret" input. */
Gilles Peskine449bd832023-01-11 14:50:10 +01006008 if (slen != 0) {
Jerry Yudc7bd172022-02-17 13:44:15 +08006009 psa_key_attributes_t key_attributes = psa_key_attributes_init();
Gilles Peskine449bd832023-01-11 14:50:10 +01006010 psa_set_key_usage_flags(&key_attributes, PSA_KEY_USAGE_DERIVE);
6011 psa_set_key_algorithm(&key_attributes, alg);
6012 psa_set_key_type(&key_attributes, PSA_KEY_TYPE_DERIVE);
Jerry Yudc7bd172022-02-17 13:44:15 +08006013
Gilles Peskine449bd832023-01-11 14:50:10 +01006014 status = psa_import_key(&key_attributes, secret, slen, &master_key);
6015 if (status != PSA_SUCCESS) {
6016 return MBEDTLS_ERR_SSL_HW_ACCEL_FAILED;
6017 }
Jerry Yudc7bd172022-02-17 13:44:15 +08006018 }
6019
Gilles Peskine449bd832023-01-11 14:50:10 +01006020 status = setup_psa_key_derivation(&derivation,
6021 master_key, alg,
6022 NULL, 0,
6023 random, rlen,
6024 (unsigned char const *) label,
6025 (size_t) strlen(label),
6026 NULL, 0,
6027 dlen);
6028 if (status != PSA_SUCCESS) {
6029 psa_key_derivation_abort(&derivation);
6030 psa_destroy_key(master_key);
6031 return MBEDTLS_ERR_SSL_HW_ACCEL_FAILED;
Jerry Yudc7bd172022-02-17 13:44:15 +08006032 }
6033
Gilles Peskine449bd832023-01-11 14:50:10 +01006034 status = psa_key_derivation_output_bytes(&derivation, dstbuf, dlen);
6035 if (status != PSA_SUCCESS) {
6036 psa_key_derivation_abort(&derivation);
6037 psa_destroy_key(master_key);
6038 return MBEDTLS_ERR_SSL_HW_ACCEL_FAILED;
Jerry Yudc7bd172022-02-17 13:44:15 +08006039 }
6040
Gilles Peskine449bd832023-01-11 14:50:10 +01006041 status = psa_key_derivation_abort(&derivation);
6042 if (status != PSA_SUCCESS) {
6043 psa_destroy_key(master_key);
6044 return MBEDTLS_ERR_SSL_HW_ACCEL_FAILED;
Jerry Yudc7bd172022-02-17 13:44:15 +08006045 }
6046
Gilles Peskine449bd832023-01-11 14:50:10 +01006047 if (!mbedtls_svc_key_id_is_null(master_key)) {
6048 status = psa_destroy_key(master_key);
6049 }
6050 if (status != PSA_SUCCESS) {
6051 return MBEDTLS_ERR_SSL_HW_ACCEL_FAILED;
6052 }
Jerry Yudc7bd172022-02-17 13:44:15 +08006053
Gilles Peskine449bd832023-01-11 14:50:10 +01006054 return 0;
Jerry Yudc7bd172022-02-17 13:44:15 +08006055}
Andrzej Kurek57d10632022-10-24 10:32:01 -04006056#endif /* PSA_WANT_ALG_SHA_256 || PSA_WANT_ALG_SHA_384 */
Jerry Yudc7bd172022-02-17 13:44:15 +08006057#else /* MBEDTLS_USE_PSA_CRYPTO */
6058
Andrzej Kurek57d10632022-10-24 10:32:01 -04006059#if defined(MBEDTLS_MD_C) && \
Gilles Peskine449bd832023-01-11 14:50:10 +01006060 (defined(MBEDTLS_SHA256_C) || \
6061 defined(MBEDTLS_SHA384_C))
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02006062MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01006063static int tls_prf_generic(mbedtls_md_type_t md_type,
6064 const unsigned char *secret, size_t slen,
6065 const char *label,
6066 const unsigned char *random, size_t rlen,
6067 unsigned char *dstbuf, size_t dlen)
Jerry Yudc7bd172022-02-17 13:44:15 +08006068{
6069 size_t nb;
6070 size_t i, j, k, md_len;
6071 unsigned char *tmp;
6072 size_t tmp_len = 0;
6073 unsigned char h_i[MBEDTLS_MD_MAX_SIZE];
6074 const mbedtls_md_info_t *md_info;
6075 mbedtls_md_context_t md_ctx;
6076 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
6077
Gilles Peskine449bd832023-01-11 14:50:10 +01006078 mbedtls_md_init(&md_ctx);
Jerry Yudc7bd172022-02-17 13:44:15 +08006079
Gilles Peskine449bd832023-01-11 14:50:10 +01006080 if ((md_info = mbedtls_md_info_from_type(md_type)) == NULL) {
6081 return MBEDTLS_ERR_SSL_INTERNAL_ERROR;
6082 }
Jerry Yudc7bd172022-02-17 13:44:15 +08006083
Gilles Peskine449bd832023-01-11 14:50:10 +01006084 md_len = mbedtls_md_get_size(md_info);
Jerry Yudc7bd172022-02-17 13:44:15 +08006085
Gilles Peskine449bd832023-01-11 14:50:10 +01006086 tmp_len = md_len + strlen(label) + rlen;
6087 tmp = mbedtls_calloc(1, tmp_len);
6088 if (tmp == NULL) {
Jerry Yudc7bd172022-02-17 13:44:15 +08006089 ret = MBEDTLS_ERR_SSL_ALLOC_FAILED;
6090 goto exit;
6091 }
6092
Gilles Peskine449bd832023-01-11 14:50:10 +01006093 nb = strlen(label);
6094 memcpy(tmp + md_len, label, nb);
6095 memcpy(tmp + md_len + nb, random, rlen);
Jerry Yudc7bd172022-02-17 13:44:15 +08006096 nb += rlen;
6097
6098 /*
6099 * Compute P_<hash>(secret, label + random)[0..dlen]
6100 */
Gilles Peskine449bd832023-01-11 14:50:10 +01006101 if ((ret = mbedtls_md_setup(&md_ctx, md_info, 1)) != 0) {
Jerry Yudc7bd172022-02-17 13:44:15 +08006102 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01006103 }
Jerry Yudc7bd172022-02-17 13:44:15 +08006104
Gilles Peskine449bd832023-01-11 14:50:10 +01006105 ret = mbedtls_md_hmac_starts(&md_ctx, secret, slen);
6106 if (ret != 0) {
Jerry Yudc7bd172022-02-17 13:44:15 +08006107 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01006108 }
6109 ret = mbedtls_md_hmac_update(&md_ctx, tmp + md_len, nb);
6110 if (ret != 0) {
Jerry Yudc7bd172022-02-17 13:44:15 +08006111 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01006112 }
6113 ret = mbedtls_md_hmac_finish(&md_ctx, tmp);
6114 if (ret != 0) {
Jerry Yudc7bd172022-02-17 13:44:15 +08006115 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01006116 }
Jerry Yudc7bd172022-02-17 13:44:15 +08006117
Gilles Peskine449bd832023-01-11 14:50:10 +01006118 for (i = 0; i < dlen; i += md_len) {
6119 ret = mbedtls_md_hmac_reset(&md_ctx);
6120 if (ret != 0) {
Jerry Yudc7bd172022-02-17 13:44:15 +08006121 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01006122 }
6123 ret = mbedtls_md_hmac_update(&md_ctx, tmp, md_len + nb);
6124 if (ret != 0) {
Jerry Yudc7bd172022-02-17 13:44:15 +08006125 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01006126 }
6127 ret = mbedtls_md_hmac_finish(&md_ctx, h_i);
6128 if (ret != 0) {
Jerry Yudc7bd172022-02-17 13:44:15 +08006129 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01006130 }
Jerry Yudc7bd172022-02-17 13:44:15 +08006131
Gilles Peskine449bd832023-01-11 14:50:10 +01006132 ret = mbedtls_md_hmac_reset(&md_ctx);
6133 if (ret != 0) {
Jerry Yudc7bd172022-02-17 13:44:15 +08006134 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01006135 }
6136 ret = mbedtls_md_hmac_update(&md_ctx, tmp, md_len);
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_finish(&md_ctx, tmp);
6141 if (ret != 0) {
Jerry Yudc7bd172022-02-17 13:44:15 +08006142 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01006143 }
Jerry Yudc7bd172022-02-17 13:44:15 +08006144
Gilles Peskine449bd832023-01-11 14:50:10 +01006145 k = (i + md_len > dlen) ? dlen % md_len : md_len;
Jerry Yudc7bd172022-02-17 13:44:15 +08006146
Gilles Peskine449bd832023-01-11 14:50:10 +01006147 for (j = 0; j < k; j++) {
Jerry Yudc7bd172022-02-17 13:44:15 +08006148 dstbuf[i + j] = h_i[j];
Gilles Peskine449bd832023-01-11 14:50:10 +01006149 }
Jerry Yudc7bd172022-02-17 13:44:15 +08006150 }
6151
6152exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01006153 mbedtls_md_free(&md_ctx);
Jerry Yudc7bd172022-02-17 13:44:15 +08006154
Gilles Peskine449bd832023-01-11 14:50:10 +01006155 if (tmp != NULL) {
6156 mbedtls_platform_zeroize(tmp, tmp_len);
6157 }
Dave Rodgman29b9b2b2022-11-01 16:08:14 +00006158
Gilles Peskine449bd832023-01-11 14:50:10 +01006159 mbedtls_platform_zeroize(h_i, sizeof(h_i));
Jerry Yudc7bd172022-02-17 13:44:15 +08006160
Gilles Peskine449bd832023-01-11 14:50:10 +01006161 mbedtls_free(tmp);
Jerry Yudc7bd172022-02-17 13:44:15 +08006162
Gilles Peskine449bd832023-01-11 14:50:10 +01006163 return ret;
Jerry Yudc7bd172022-02-17 13:44:15 +08006164}
Andrzej Kurek57d10632022-10-24 10:32:01 -04006165#endif /* MBEDTLS_MD_C && ( MBEDTLS_SHA256_C || MBEDTLS_SHA384_C ) */
Jerry Yudc7bd172022-02-17 13:44:15 +08006166#endif /* MBEDTLS_USE_PSA_CRYPTO */
6167
Andrzej Kurek25f27152022-08-17 16:09:31 -04006168#if defined(MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02006169MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01006170static int tls_prf_sha256(const unsigned char *secret, size_t slen,
6171 const char *label,
6172 const unsigned char *random, size_t rlen,
6173 unsigned char *dstbuf, size_t dlen)
Jerry Yudc7bd172022-02-17 13:44:15 +08006174{
Gilles Peskine449bd832023-01-11 14:50:10 +01006175 return tls_prf_generic(MBEDTLS_MD_SHA256, secret, slen,
6176 label, random, rlen, dstbuf, dlen);
Jerry Yudc7bd172022-02-17 13:44:15 +08006177}
Andrzej Kurekcccb0442022-08-19 03:42:11 -04006178#endif /* MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA*/
Jerry Yudc7bd172022-02-17 13:44:15 +08006179
Andrzej Kurek25f27152022-08-17 16:09:31 -04006180#if defined(MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02006181MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01006182static int tls_prf_sha384(const unsigned char *secret, size_t slen,
6183 const char *label,
6184 const unsigned char *random, size_t rlen,
6185 unsigned char *dstbuf, size_t dlen)
Jerry Yudc7bd172022-02-17 13:44:15 +08006186{
Gilles Peskine449bd832023-01-11 14:50:10 +01006187 return tls_prf_generic(MBEDTLS_MD_SHA384, secret, slen,
6188 label, random, rlen, dstbuf, dlen);
Jerry Yudc7bd172022-02-17 13:44:15 +08006189}
Andrzej Kurekcccb0442022-08-19 03:42:11 -04006190#endif /* MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA*/
Jerry Yudc7bd172022-02-17 13:44:15 +08006191
Jerry Yuf009d862022-02-17 14:01:37 +08006192/*
6193 * Set appropriate PRF function and other SSL / TLS1.2 functions
6194 *
6195 * Inputs:
Jerry Yuf009d862022-02-17 14:01:37 +08006196 * - hash associated with the ciphersuite (only used by TLS 1.2)
6197 *
6198 * Outputs:
6199 * - the tls_prf, calc_verify and calc_finished members of handshake structure
6200 */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02006201MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01006202static int ssl_set_handshake_prfs(mbedtls_ssl_handshake_params *handshake,
6203 mbedtls_md_type_t hash)
Jerry Yuf009d862022-02-17 14:01:37 +08006204{
Andrzej Kurek25f27152022-08-17 16:09:31 -04006205#if defined(MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Gilles Peskine449bd832023-01-11 14:50:10 +01006206 if (hash == MBEDTLS_MD_SHA384) {
Jerry Yuf009d862022-02-17 14:01:37 +08006207 handshake->tls_prf = tls_prf_sha384;
6208 handshake->calc_verify = ssl_calc_verify_tls_sha384;
6209 handshake->calc_finished = ssl_calc_finished_tls_sha384;
Gilles Peskine449bd832023-01-11 14:50:10 +01006210 } else
Jerry Yuf009d862022-02-17 14:01:37 +08006211#endif
Andrzej Kurek25f27152022-08-17 16:09:31 -04006212#if defined(MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Jerry Yuf009d862022-02-17 14:01:37 +08006213 {
Ronald Cron81591aa2022-03-07 09:05:51 +01006214 (void) hash;
Jerry Yuf009d862022-02-17 14:01:37 +08006215 handshake->tls_prf = tls_prf_sha256;
6216 handshake->calc_verify = ssl_calc_verify_tls_sha256;
6217 handshake->calc_finished = ssl_calc_finished_tls_sha256;
6218 }
Ronald Cron81591aa2022-03-07 09:05:51 +01006219#else
Jerry Yuf009d862022-02-17 14:01:37 +08006220 {
Jerry Yuf009d862022-02-17 14:01:37 +08006221 (void) handshake;
Ronald Cron81591aa2022-03-07 09:05:51 +01006222 (void) hash;
Gilles Peskine449bd832023-01-11 14:50:10 +01006223 return MBEDTLS_ERR_SSL_INTERNAL_ERROR;
Jerry Yuf009d862022-02-17 14:01:37 +08006224 }
Ronald Cron81591aa2022-03-07 09:05:51 +01006225#endif
Jerry Yuf009d862022-02-17 14:01:37 +08006226
Gilles Peskine449bd832023-01-11 14:50:10 +01006227 return 0;
Jerry Yuf009d862022-02-17 14:01:37 +08006228}
Jerry Yud6ab2352022-02-17 14:03:43 +08006229
Jerry Yu2a7b5ac2022-02-17 14:07:00 +08006230/*
6231 * Compute master secret if needed
6232 *
6233 * Parameters:
6234 * [in/out] handshake
6235 * [in] resume, premaster, extended_ms, calc_verify, tls_prf
6236 * (PSA-PSK) ciphersuite_info, psk_opaque
6237 * [out] premaster (cleared)
6238 * [out] master
6239 * [in] ssl: optionally used for debugging, EMS and PSA-PSK
6240 * debug: conf->f_dbg, conf->p_dbg
6241 * EMS: passed to calc_verify (debug + session_negotiate)
Ronald Crona25cf582022-03-07 11:10:36 +01006242 * PSA-PSA: conf
Jerry Yu2a7b5ac2022-02-17 14:07:00 +08006243 */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02006244MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01006245static int ssl_compute_master(mbedtls_ssl_handshake_params *handshake,
6246 unsigned char *master,
6247 const mbedtls_ssl_context *ssl)
Jerry Yu2a7b5ac2022-02-17 14:07:00 +08006248{
6249 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
6250
6251 /* cf. RFC 5246, Section 8.1:
6252 * "The master secret is always exactly 48 bytes in length." */
6253 size_t const master_secret_len = 48;
6254
6255#if defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET)
6256 unsigned char session_hash[48];
6257#endif /* MBEDTLS_SSL_EXTENDED_MASTER_SECRET */
6258
6259 /* The label for the KDF used for key expansion.
6260 * This is either "master secret" or "extended master secret"
6261 * depending on whether the Extended Master Secret extension
6262 * is used. */
6263 char const *lbl = "master secret";
6264
Przemek Stekielae4ed302022-04-05 17:15:55 +02006265 /* The seed for the KDF used for key expansion.
Jerry Yu2a7b5ac2022-02-17 14:07:00 +08006266 * - If the Extended Master Secret extension is not used,
6267 * this is ClientHello.Random + ServerHello.Random
6268 * (see Sect. 8.1 in RFC 5246).
6269 * - If the Extended Master Secret extension is used,
6270 * this is the transcript of the handshake so far.
6271 * (see Sect. 4 in RFC 7627). */
Przemek Stekielae4ed302022-04-05 17:15:55 +02006272 unsigned char const *seed = handshake->randbytes;
6273 size_t seed_len = 64;
Jerry Yu2a7b5ac2022-02-17 14:07:00 +08006274
6275#if !defined(MBEDTLS_DEBUG_C) && \
6276 !defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET) && \
6277 !(defined(MBEDTLS_USE_PSA_CRYPTO) && \
Gilles Peskine449bd832023-01-11 14:50:10 +01006278 defined(MBEDTLS_KEY_EXCHANGE_PSK_ENABLED))
Jerry Yu2a7b5ac2022-02-17 14:07:00 +08006279 ssl = NULL; /* make sure we don't use it except for those cases */
6280 (void) ssl;
6281#endif
6282
Gilles Peskine449bd832023-01-11 14:50:10 +01006283 if (handshake->resume != 0) {
6284 MBEDTLS_SSL_DEBUG_MSG(3, ("no premaster (session resumed)"));
6285 return 0;
Jerry Yu2a7b5ac2022-02-17 14:07:00 +08006286 }
6287
6288#if defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET)
Gilles Peskine449bd832023-01-11 14:50:10 +01006289 if (handshake->extended_ms == MBEDTLS_SSL_EXTENDED_MS_ENABLED) {
Jerry Yu2a7b5ac2022-02-17 14:07:00 +08006290 lbl = "extended master secret";
Przemek Stekielae4ed302022-04-05 17:15:55 +02006291 seed = session_hash;
Gilles Peskine449bd832023-01-11 14:50:10 +01006292 handshake->calc_verify(ssl, session_hash, &seed_len);
Jerry Yu2a7b5ac2022-02-17 14:07:00 +08006293
Gilles Peskine449bd832023-01-11 14:50:10 +01006294 MBEDTLS_SSL_DEBUG_BUF(3, "session hash for extended master secret",
6295 session_hash, seed_len);
Jerry Yu2a7b5ac2022-02-17 14:07:00 +08006296 }
Przemek Stekiel169bf0b2022-04-29 07:53:29 +02006297#endif /* MBEDTLS_SSL_EXTENDED_MASTER_SECRET */
Jerry Yu2a7b5ac2022-02-17 14:07:00 +08006298
Przemek Stekiel99114f32022-04-22 11:20:09 +02006299#if defined(MBEDTLS_USE_PSA_CRYPTO) && \
Przemek Stekiel8a4b7fd2022-04-28 09:22:22 +02006300 defined(MBEDTLS_KEY_EXCHANGE_SOME_PSK_ENABLED)
Gilles Peskine449bd832023-01-11 14:50:10 +01006301 if (mbedtls_ssl_ciphersuite_uses_psk(handshake->ciphersuite_info) == 1) {
Jerry Yu2a7b5ac2022-02-17 14:07:00 +08006302 /* Perform PSK-to-MS expansion in a single step. */
6303 psa_status_t status;
6304 psa_algorithm_t alg;
6305 mbedtls_svc_key_id_t psk;
6306 psa_key_derivation_operation_t derivation =
6307 PSA_KEY_DERIVATION_OPERATION_INIT;
6308 mbedtls_md_type_t hash_alg = handshake->ciphersuite_info->mac;
6309
Gilles Peskine449bd832023-01-11 14:50:10 +01006310 MBEDTLS_SSL_DEBUG_MSG(2, ("perform PSA-based PSK-to-MS expansion"));
Jerry Yu2a7b5ac2022-02-17 14:07:00 +08006311
Gilles Peskine449bd832023-01-11 14:50:10 +01006312 psk = mbedtls_ssl_get_opaque_psk(ssl);
Jerry Yu2a7b5ac2022-02-17 14:07:00 +08006313
Gilles Peskine449bd832023-01-11 14:50:10 +01006314 if (hash_alg == MBEDTLS_MD_SHA384) {
Jerry Yu2a7b5ac2022-02-17 14:07:00 +08006315 alg = PSA_ALG_TLS12_PSK_TO_MS(PSA_ALG_SHA_384);
Gilles Peskine449bd832023-01-11 14:50:10 +01006316 } else {
Jerry Yu2a7b5ac2022-02-17 14:07:00 +08006317 alg = PSA_ALG_TLS12_PSK_TO_MS(PSA_ALG_SHA_256);
Gilles Peskine449bd832023-01-11 14:50:10 +01006318 }
Jerry Yu2a7b5ac2022-02-17 14:07:00 +08006319
Przemek Stekiel51a1f362022-04-13 08:57:06 +02006320 size_t other_secret_len = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +01006321 unsigned char *other_secret = NULL;
Przemek Stekielc2033402022-04-05 17:19:41 +02006322
Gilles Peskine449bd832023-01-11 14:50:10 +01006323 switch (handshake->ciphersuite_info->key_exchange) {
Przemek Stekiel19b80f82022-04-14 08:29:31 +02006324 /* Provide other secret.
Przemek Stekiel51a1f362022-04-13 08:57:06 +02006325 * Other secret is stored in premaster, where first 2 bytes hold the
Przemek Stekiel19b80f82022-04-14 08:29:31 +02006326 * length of the other key.
Przemek Stekielc2033402022-04-05 17:19:41 +02006327 */
Przemek Stekiel19b80f82022-04-14 08:29:31 +02006328 case MBEDTLS_KEY_EXCHANGE_RSA_PSK:
Przemek Stekiel8abcee92022-04-28 09:16:28 +02006329 /* For RSA-PSK other key length is always 48 bytes. */
Przemek Stekiel19b80f82022-04-14 08:29:31 +02006330 other_secret_len = 48;
6331 other_secret = handshake->premaster + 2;
6332 break;
6333 case MBEDTLS_KEY_EXCHANGE_ECDHE_PSK:
Przemek Stekielb293aaa2022-04-19 12:22:38 +02006334 case MBEDTLS_KEY_EXCHANGE_DHE_PSK:
Przemek Stekiel19b80f82022-04-14 08:29:31 +02006335 other_secret_len = MBEDTLS_GET_UINT16_BE(handshake->premaster, 0);
6336 other_secret = handshake->premaster + 2;
6337 break;
6338 default:
6339 break;
Przemek Stekielc2033402022-04-05 17:19:41 +02006340 }
6341
Gilles Peskine449bd832023-01-11 14:50:10 +01006342 status = setup_psa_key_derivation(&derivation, psk, alg,
6343 ssl->conf->psk, ssl->conf->psk_len,
6344 seed, seed_len,
6345 (unsigned char const *) lbl,
6346 (size_t) strlen(lbl),
6347 other_secret, other_secret_len,
6348 master_secret_len);
6349 if (status != PSA_SUCCESS) {
6350 psa_key_derivation_abort(&derivation);
6351 return MBEDTLS_ERR_SSL_HW_ACCEL_FAILED;
Jerry Yu2a7b5ac2022-02-17 14:07:00 +08006352 }
6353
Gilles Peskine449bd832023-01-11 14:50:10 +01006354 status = psa_key_derivation_output_bytes(&derivation,
6355 master,
6356 master_secret_len);
6357 if (status != PSA_SUCCESS) {
6358 psa_key_derivation_abort(&derivation);
6359 return MBEDTLS_ERR_SSL_HW_ACCEL_FAILED;
Jerry Yu2a7b5ac2022-02-17 14:07:00 +08006360 }
6361
Gilles Peskine449bd832023-01-11 14:50:10 +01006362 status = psa_key_derivation_abort(&derivation);
6363 if (status != PSA_SUCCESS) {
6364 return MBEDTLS_ERR_SSL_HW_ACCEL_FAILED;
6365 }
6366 } else
Jerry Yu2a7b5ac2022-02-17 14:07:00 +08006367#endif
6368 {
Neil Armstrongca7d5062022-05-31 14:43:23 +02006369#if defined(MBEDTLS_USE_PSA_CRYPTO) && \
Gilles Peskine449bd832023-01-11 14:50:10 +01006370 defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
6371 if (handshake->ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECJPAKE) {
Neil Armstrongca7d5062022-05-31 14:43:23 +02006372 psa_status_t status;
6373 psa_algorithm_t alg = PSA_ALG_TLS12_ECJPAKE_TO_PMS;
6374 psa_key_derivation_operation_t derivation =
6375 PSA_KEY_DERIVATION_OPERATION_INIT;
6376
Gilles Peskine449bd832023-01-11 14:50:10 +01006377 MBEDTLS_SSL_DEBUG_MSG(2, ("perform PSA-based PMS KDF for ECJPAKE"));
Neil Armstrongca7d5062022-05-31 14:43:23 +02006378
6379 handshake->pmslen = PSA_TLS12_ECJPAKE_TO_PMS_DATA_SIZE;
6380
Gilles Peskine449bd832023-01-11 14:50:10 +01006381 status = psa_key_derivation_setup(&derivation, alg);
6382 if (status != PSA_SUCCESS) {
6383 return MBEDTLS_ERR_SSL_HW_ACCEL_FAILED;
Neil Armstrongca7d5062022-05-31 14:43:23 +02006384 }
6385
Gilles Peskine449bd832023-01-11 14:50:10 +01006386 status = psa_key_derivation_set_capacity(&derivation,
6387 PSA_TLS12_ECJPAKE_TO_PMS_DATA_SIZE);
6388 if (status != PSA_SUCCESS) {
6389 psa_key_derivation_abort(&derivation);
6390 return MBEDTLS_ERR_SSL_HW_ACCEL_FAILED;
Neil Armstrongca7d5062022-05-31 14:43:23 +02006391 }
6392
Gilles Peskine449bd832023-01-11 14:50:10 +01006393 status = psa_pake_get_implicit_key(&handshake->psa_pake_ctx,
6394 &derivation);
6395 if (status != PSA_SUCCESS) {
6396 psa_key_derivation_abort(&derivation);
6397 return MBEDTLS_ERR_SSL_HW_ACCEL_FAILED;
Neil Armstrongca7d5062022-05-31 14:43:23 +02006398 }
6399
Gilles Peskine449bd832023-01-11 14:50:10 +01006400 status = psa_key_derivation_output_bytes(&derivation,
6401 handshake->premaster,
6402 handshake->pmslen);
6403 if (status != PSA_SUCCESS) {
6404 psa_key_derivation_abort(&derivation);
6405 return MBEDTLS_ERR_SSL_HW_ACCEL_FAILED;
6406 }
6407
6408 status = psa_key_derivation_abort(&derivation);
6409 if (status != PSA_SUCCESS) {
6410 return MBEDTLS_ERR_SSL_HW_ACCEL_FAILED;
Neil Armstrongca7d5062022-05-31 14:43:23 +02006411 }
6412 }
6413#endif
Gilles Peskine449bd832023-01-11 14:50:10 +01006414 ret = handshake->tls_prf(handshake->premaster, handshake->pmslen,
6415 lbl, seed, seed_len,
6416 master,
6417 master_secret_len);
6418 if (ret != 0) {
6419 MBEDTLS_SSL_DEBUG_RET(1, "prf", ret);
6420 return ret;
Jerry Yu2a7b5ac2022-02-17 14:07:00 +08006421 }
6422
Gilles Peskine449bd832023-01-11 14:50:10 +01006423 MBEDTLS_SSL_DEBUG_BUF(3, "premaster secret",
6424 handshake->premaster,
6425 handshake->pmslen);
Jerry Yu2a7b5ac2022-02-17 14:07:00 +08006426
Gilles Peskine449bd832023-01-11 14:50:10 +01006427 mbedtls_platform_zeroize(handshake->premaster,
6428 sizeof(handshake->premaster));
Jerry Yu2a7b5ac2022-02-17 14:07:00 +08006429 }
6430
Gilles Peskine449bd832023-01-11 14:50:10 +01006431 return 0;
Jerry Yu2a7b5ac2022-02-17 14:07:00 +08006432}
6433
Gilles Peskine449bd832023-01-11 14:50:10 +01006434int mbedtls_ssl_derive_keys(mbedtls_ssl_context *ssl)
Jerry Yud62f87e2022-02-17 14:09:02 +08006435{
6436 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
6437 const mbedtls_ssl_ciphersuite_t * const ciphersuite_info =
6438 ssl->handshake->ciphersuite_info;
6439
Gilles Peskine449bd832023-01-11 14:50:10 +01006440 MBEDTLS_SSL_DEBUG_MSG(2, ("=> derive keys"));
Jerry Yud62f87e2022-02-17 14:09:02 +08006441
6442 /* Set PRF, calc_verify and calc_finished function pointers */
Gilles Peskine449bd832023-01-11 14:50:10 +01006443 ret = ssl_set_handshake_prfs(ssl->handshake,
6444 ciphersuite_info->mac);
6445 if (ret != 0) {
6446 MBEDTLS_SSL_DEBUG_RET(1, "ssl_set_handshake_prfs", ret);
6447 return ret;
Jerry Yud62f87e2022-02-17 14:09:02 +08006448 }
6449
6450 /* Compute master secret if needed */
Gilles Peskine449bd832023-01-11 14:50:10 +01006451 ret = ssl_compute_master(ssl->handshake,
6452 ssl->session_negotiate->master,
6453 ssl);
6454 if (ret != 0) {
6455 MBEDTLS_SSL_DEBUG_RET(1, "ssl_compute_master", ret);
6456 return ret;
Jerry Yud62f87e2022-02-17 14:09:02 +08006457 }
6458
6459 /* Swap the client and server random values:
6460 * - MS derivation wanted client+server (RFC 5246 8.1)
6461 * - key derivation wants server+client (RFC 5246 6.3) */
6462 {
6463 unsigned char tmp[64];
Gilles Peskine449bd832023-01-11 14:50:10 +01006464 memcpy(tmp, ssl->handshake->randbytes, 64);
6465 memcpy(ssl->handshake->randbytes, tmp + 32, 32);
6466 memcpy(ssl->handshake->randbytes + 32, tmp, 32);
6467 mbedtls_platform_zeroize(tmp, sizeof(tmp));
Jerry Yud62f87e2022-02-17 14:09:02 +08006468 }
6469
6470 /* Populate transform structure */
Gilles Peskine449bd832023-01-11 14:50:10 +01006471 ret = ssl_tls12_populate_transform(ssl->transform_negotiate,
6472 ssl->session_negotiate->ciphersuite,
6473 ssl->session_negotiate->master,
Neil Armstrongf2c82f02022-04-05 11:16:53 +02006474#if defined(MBEDTLS_SSL_SOME_SUITES_USE_CBC_ETM)
Gilles Peskine449bd832023-01-11 14:50:10 +01006475 ssl->session_negotiate->encrypt_then_mac,
Neil Armstrongf2c82f02022-04-05 11:16:53 +02006476#endif /* MBEDTLS_SSL_SOME_SUITES_USE_CBC_ETM */
Gilles Peskine449bd832023-01-11 14:50:10 +01006477 ssl->handshake->tls_prf,
6478 ssl->handshake->randbytes,
6479 ssl->tls_version,
6480 ssl->conf->endpoint,
6481 ssl);
6482 if (ret != 0) {
6483 MBEDTLS_SSL_DEBUG_RET(1, "ssl_tls12_populate_transform", ret);
6484 return ret;
Jerry Yud62f87e2022-02-17 14:09:02 +08006485 }
6486
6487 /* We no longer need Server/ClientHello.random values */
Gilles Peskine449bd832023-01-11 14:50:10 +01006488 mbedtls_platform_zeroize(ssl->handshake->randbytes,
6489 sizeof(ssl->handshake->randbytes));
Jerry Yud62f87e2022-02-17 14:09:02 +08006490
Gilles Peskine449bd832023-01-11 14:50:10 +01006491 MBEDTLS_SSL_DEBUG_MSG(2, ("<= derive keys"));
Jerry Yud62f87e2022-02-17 14:09:02 +08006492
Gilles Peskine449bd832023-01-11 14:50:10 +01006493 return 0;
Jerry Yud62f87e2022-02-17 14:09:02 +08006494}
Jerry Yu8392e0d2022-02-17 14:10:24 +08006495
Gilles Peskine449bd832023-01-11 14:50:10 +01006496int mbedtls_ssl_set_calc_verify_md(mbedtls_ssl_context *ssl, int md)
Ronald Cron4dcbca92022-03-07 10:21:40 +01006497{
Gilles Peskine449bd832023-01-11 14:50:10 +01006498 switch (md) {
Andrzej Kurek25f27152022-08-17 16:09:31 -04006499#if defined(MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Ronald Cron4dcbca92022-03-07 10:21:40 +01006500 case MBEDTLS_SSL_HASH_SHA384:
6501 ssl->handshake->calc_verify = ssl_calc_verify_tls_sha384;
6502 break;
6503#endif
Andrzej Kurek25f27152022-08-17 16:09:31 -04006504#if defined(MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Ronald Cron4dcbca92022-03-07 10:21:40 +01006505 case MBEDTLS_SSL_HASH_SHA256:
6506 ssl->handshake->calc_verify = ssl_calc_verify_tls_sha256;
6507 break;
6508#endif
6509 default:
Gilles Peskine449bd832023-01-11 14:50:10 +01006510 return -1;
Ronald Cron4dcbca92022-03-07 10:21:40 +01006511 }
Andrzej Kurekeabeb302022-10-17 07:52:51 -04006512#if !defined(MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA) && \
6513 !defined(MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
6514 (void) ssl;
6515#endif
Gilles Peskine449bd832023-01-11 14:50:10 +01006516 return 0;
Ronald Cron4dcbca92022-03-07 10:21:40 +01006517}
6518
Andrzej Kurek25f27152022-08-17 16:09:31 -04006519#if defined(MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Manuel Pégourié-Gonnard226aa152023-02-05 09:46:59 +01006520int ssl_calc_verify_tls_sha256(const mbedtls_ssl_context *ssl,
6521 unsigned char *hash,
6522 size_t *hlen)
Jerry Yu8392e0d2022-02-17 14:10:24 +08006523{
6524#if defined(MBEDTLS_USE_PSA_CRYPTO)
6525 size_t hash_size;
6526 psa_status_t status;
6527 psa_hash_operation_t sha256_psa = psa_hash_operation_init();
6528
Gilles Peskine449bd832023-01-11 14:50:10 +01006529 MBEDTLS_SSL_DEBUG_MSG(2, ("=> PSA calc verify sha256"));
6530 status = psa_hash_clone(&ssl->handshake->fin_sha256_psa, &sha256_psa);
6531 if (status != PSA_SUCCESS) {
6532 MBEDTLS_SSL_DEBUG_MSG(2, ("PSA hash clone failed"));
Manuel Pégourié-Gonnard226aa152023-02-05 09:46:59 +01006533 return 0;
Jerry Yu8392e0d2022-02-17 14:10:24 +08006534 }
6535
Gilles Peskine449bd832023-01-11 14:50:10 +01006536 status = psa_hash_finish(&sha256_psa, hash, 32, &hash_size);
6537 if (status != PSA_SUCCESS) {
6538 MBEDTLS_SSL_DEBUG_MSG(2, ("PSA hash finish failed"));
Manuel Pégourié-Gonnard226aa152023-02-05 09:46:59 +01006539 return 0;
Jerry Yu8392e0d2022-02-17 14:10:24 +08006540 }
6541
6542 *hlen = 32;
Gilles Peskine449bd832023-01-11 14:50:10 +01006543 MBEDTLS_SSL_DEBUG_BUF(3, "PSA calculated verify result", hash, *hlen);
6544 MBEDTLS_SSL_DEBUG_MSG(2, ("<= PSA calc verify"));
Jerry Yu8392e0d2022-02-17 14:10:24 +08006545#else
6546 mbedtls_sha256_context sha256;
6547
Gilles Peskine449bd832023-01-11 14:50:10 +01006548 mbedtls_sha256_init(&sha256);
Jerry Yu8392e0d2022-02-17 14:10:24 +08006549
Gilles Peskine449bd832023-01-11 14:50:10 +01006550 MBEDTLS_SSL_DEBUG_MSG(2, ("=> calc verify sha256"));
Jerry Yu8392e0d2022-02-17 14:10:24 +08006551
Gilles Peskine449bd832023-01-11 14:50:10 +01006552 mbedtls_sha256_clone(&sha256, &ssl->handshake->fin_sha256);
6553 mbedtls_sha256_finish(&sha256, hash);
Jerry Yu8392e0d2022-02-17 14:10:24 +08006554
6555 *hlen = 32;
6556
Gilles Peskine449bd832023-01-11 14:50:10 +01006557 MBEDTLS_SSL_DEBUG_BUF(3, "calculated verify result", hash, *hlen);
6558 MBEDTLS_SSL_DEBUG_MSG(2, ("<= calc verify"));
Jerry Yu8392e0d2022-02-17 14:10:24 +08006559
Gilles Peskine449bd832023-01-11 14:50:10 +01006560 mbedtls_sha256_free(&sha256);
Jerry Yu8392e0d2022-02-17 14:10:24 +08006561#endif /* MBEDTLS_USE_PSA_CRYPTO */
Manuel Pégourié-Gonnard226aa152023-02-05 09:46:59 +01006562 return 0;
Jerry Yu8392e0d2022-02-17 14:10:24 +08006563}
Andrzej Kurek25f27152022-08-17 16:09:31 -04006564#endif /* MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA */
Jerry Yu8392e0d2022-02-17 14:10:24 +08006565
Andrzej Kurek25f27152022-08-17 16:09:31 -04006566#if defined(MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Manuel Pégourié-Gonnard226aa152023-02-05 09:46:59 +01006567int ssl_calc_verify_tls_sha384(const mbedtls_ssl_context *ssl,
6568 unsigned char *hash,
6569 size_t *hlen)
Jerry Yuc1cb3842022-02-17 14:13:48 +08006570{
6571#if defined(MBEDTLS_USE_PSA_CRYPTO)
6572 size_t hash_size;
6573 psa_status_t status;
6574 psa_hash_operation_t sha384_psa = psa_hash_operation_init();
6575
Gilles Peskine449bd832023-01-11 14:50:10 +01006576 MBEDTLS_SSL_DEBUG_MSG(2, ("=> PSA calc verify sha384"));
6577 status = psa_hash_clone(&ssl->handshake->fin_sha384_psa, &sha384_psa);
6578 if (status != PSA_SUCCESS) {
6579 MBEDTLS_SSL_DEBUG_MSG(2, ("PSA hash clone failed"));
Manuel Pégourié-Gonnard226aa152023-02-05 09:46:59 +01006580 return 0;
Jerry Yuc1cb3842022-02-17 14:13:48 +08006581 }
6582
Gilles Peskine449bd832023-01-11 14:50:10 +01006583 status = psa_hash_finish(&sha384_psa, hash, 48, &hash_size);
6584 if (status != PSA_SUCCESS) {
6585 MBEDTLS_SSL_DEBUG_MSG(2, ("PSA hash finish failed"));
Manuel Pégourié-Gonnard226aa152023-02-05 09:46:59 +01006586 return 0;
Jerry Yuc1cb3842022-02-17 14:13:48 +08006587 }
6588
6589 *hlen = 48;
Gilles Peskine449bd832023-01-11 14:50:10 +01006590 MBEDTLS_SSL_DEBUG_BUF(3, "PSA calculated verify result", hash, *hlen);
6591 MBEDTLS_SSL_DEBUG_MSG(2, ("<= PSA calc verify"));
Jerry Yuc1cb3842022-02-17 14:13:48 +08006592#else
6593 mbedtls_sha512_context sha512;
6594
Gilles Peskine449bd832023-01-11 14:50:10 +01006595 mbedtls_sha512_init(&sha512);
Jerry Yuc1cb3842022-02-17 14:13:48 +08006596
Gilles Peskine449bd832023-01-11 14:50:10 +01006597 MBEDTLS_SSL_DEBUG_MSG(2, ("=> calc verify sha384"));
Jerry Yuc1cb3842022-02-17 14:13:48 +08006598
Gilles Peskine449bd832023-01-11 14:50:10 +01006599 mbedtls_sha512_clone(&sha512, &ssl->handshake->fin_sha384);
6600 mbedtls_sha512_finish(&sha512, hash);
Jerry Yuc1cb3842022-02-17 14:13:48 +08006601
6602 *hlen = 48;
6603
Gilles Peskine449bd832023-01-11 14:50:10 +01006604 MBEDTLS_SSL_DEBUG_BUF(3, "calculated verify result", hash, *hlen);
6605 MBEDTLS_SSL_DEBUG_MSG(2, ("<= calc verify"));
Jerry Yuc1cb3842022-02-17 14:13:48 +08006606
Gilles Peskine449bd832023-01-11 14:50:10 +01006607 mbedtls_sha512_free(&sha512);
Jerry Yuc1cb3842022-02-17 14:13:48 +08006608#endif /* MBEDTLS_USE_PSA_CRYPTO */
Manuel Pégourié-Gonnard226aa152023-02-05 09:46:59 +01006609 return 0;
Jerry Yuc1cb3842022-02-17 14:13:48 +08006610}
Andrzej Kurek25f27152022-08-17 16:09:31 -04006611#endif /* MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA */
Jerry Yuc1cb3842022-02-17 14:13:48 +08006612
Neil Armstrong80f6f322022-05-03 17:56:38 +02006613#if !defined(MBEDTLS_USE_PSA_CRYPTO) && \
6614 defined(MBEDTLS_KEY_EXCHANGE_SOME_PSK_ENABLED)
Gilles Peskine449bd832023-01-11 14:50:10 +01006615int mbedtls_ssl_psk_derive_premaster(mbedtls_ssl_context *ssl, mbedtls_key_exchange_type_t key_ex)
Jerry Yuce3dca42022-02-17 14:16:37 +08006616{
6617 unsigned char *p = ssl->handshake->premaster;
Gilles Peskine449bd832023-01-11 14:50:10 +01006618 unsigned char *end = p + sizeof(ssl->handshake->premaster);
Jerry Yuce3dca42022-02-17 14:16:37 +08006619 const unsigned char *psk = NULL;
6620 size_t psk_len = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +01006621 int psk_ret = mbedtls_ssl_get_psk(ssl, &psk, &psk_len);
Jerry Yuce3dca42022-02-17 14:16:37 +08006622
Gilles Peskine449bd832023-01-11 14:50:10 +01006623 if (psk_ret == MBEDTLS_ERR_SSL_PRIVATE_KEY_REQUIRED) {
Jerry Yuce3dca42022-02-17 14:16:37 +08006624 /*
6625 * This should never happen because the existence of a PSK is always
Przemek Stekielb293aaa2022-04-19 12:22:38 +02006626 * checked before calling this function.
6627 *
6628 * The exception is opaque DHE-PSK. For DHE-PSK fill premaster with
Przemek Stekiel8abcee92022-04-28 09:16:28 +02006629 * the shared secret without PSK.
Jerry Yuce3dca42022-02-17 14:16:37 +08006630 */
Gilles Peskine449bd832023-01-11 14:50:10 +01006631 if (key_ex != MBEDTLS_KEY_EXCHANGE_DHE_PSK) {
6632 MBEDTLS_SSL_DEBUG_MSG(1, ("should never happen"));
6633 return MBEDTLS_ERR_SSL_INTERNAL_ERROR;
Przemek Stekielb293aaa2022-04-19 12:22:38 +02006634 }
Jerry Yuce3dca42022-02-17 14:16:37 +08006635 }
6636
6637 /*
6638 * PMS = struct {
6639 * opaque other_secret<0..2^16-1>;
6640 * opaque psk<0..2^16-1>;
6641 * };
6642 * with "other_secret" depending on the particular key exchange
6643 */
6644#if defined(MBEDTLS_KEY_EXCHANGE_PSK_ENABLED)
Gilles Peskine449bd832023-01-11 14:50:10 +01006645 if (key_ex == MBEDTLS_KEY_EXCHANGE_PSK) {
6646 if (end - p < 2) {
6647 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
6648 }
Jerry Yuce3dca42022-02-17 14:16:37 +08006649
Gilles Peskine449bd832023-01-11 14:50:10 +01006650 MBEDTLS_PUT_UINT16_BE(psk_len, p, 0);
Jerry Yuce3dca42022-02-17 14:16:37 +08006651 p += 2;
6652
Gilles Peskine449bd832023-01-11 14:50:10 +01006653 if (end < p || (size_t) (end - p) < psk_len) {
6654 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
6655 }
Jerry Yuce3dca42022-02-17 14:16:37 +08006656
Gilles Peskine449bd832023-01-11 14:50:10 +01006657 memset(p, 0, psk_len);
Jerry Yuce3dca42022-02-17 14:16:37 +08006658 p += psk_len;
Gilles Peskine449bd832023-01-11 14:50:10 +01006659 } else
Jerry Yuce3dca42022-02-17 14:16:37 +08006660#endif /* MBEDTLS_KEY_EXCHANGE_PSK_ENABLED */
6661#if defined(MBEDTLS_KEY_EXCHANGE_RSA_PSK_ENABLED)
Gilles Peskine449bd832023-01-11 14:50:10 +01006662 if (key_ex == MBEDTLS_KEY_EXCHANGE_RSA_PSK) {
Jerry Yuce3dca42022-02-17 14:16:37 +08006663 /*
6664 * other_secret already set by the ClientKeyExchange message,
6665 * and is 48 bytes long
6666 */
Gilles Peskine449bd832023-01-11 14:50:10 +01006667 if (end - p < 2) {
6668 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
6669 }
Jerry Yuce3dca42022-02-17 14:16:37 +08006670
6671 *p++ = 0;
6672 *p++ = 48;
6673 p += 48;
Gilles Peskine449bd832023-01-11 14:50:10 +01006674 } else
Jerry Yuce3dca42022-02-17 14:16:37 +08006675#endif /* MBEDTLS_KEY_EXCHANGE_RSA_PSK_ENABLED */
6676#if defined(MBEDTLS_KEY_EXCHANGE_DHE_PSK_ENABLED)
Gilles Peskine449bd832023-01-11 14:50:10 +01006677 if (key_ex == MBEDTLS_KEY_EXCHANGE_DHE_PSK) {
Jerry Yuce3dca42022-02-17 14:16:37 +08006678 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
6679 size_t len;
6680
6681 /* Write length only when we know the actual value */
Gilles Peskine449bd832023-01-11 14:50:10 +01006682 if ((ret = mbedtls_dhm_calc_secret(&ssl->handshake->dhm_ctx,
6683 p + 2, end - (p + 2), &len,
6684 ssl->conf->f_rng, ssl->conf->p_rng)) != 0) {
6685 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_dhm_calc_secret", ret);
6686 return ret;
Jerry Yuce3dca42022-02-17 14:16:37 +08006687 }
Gilles Peskine449bd832023-01-11 14:50:10 +01006688 MBEDTLS_PUT_UINT16_BE(len, p, 0);
Jerry Yuce3dca42022-02-17 14:16:37 +08006689 p += 2 + len;
6690
Gilles Peskine449bd832023-01-11 14:50:10 +01006691 MBEDTLS_SSL_DEBUG_MPI(3, "DHM: K ", &ssl->handshake->dhm_ctx.K);
6692 } else
Jerry Yuce3dca42022-02-17 14:16:37 +08006693#endif /* MBEDTLS_KEY_EXCHANGE_DHE_PSK_ENABLED */
Neil Armstrong80f6f322022-05-03 17:56:38 +02006694#if defined(MBEDTLS_KEY_EXCHANGE_ECDHE_PSK_ENABLED)
Gilles Peskine449bd832023-01-11 14:50:10 +01006695 if (key_ex == MBEDTLS_KEY_EXCHANGE_ECDHE_PSK) {
Jerry Yuce3dca42022-02-17 14:16:37 +08006696 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
6697 size_t zlen;
6698
Gilles Peskine449bd832023-01-11 14:50:10 +01006699 if ((ret = mbedtls_ecdh_calc_secret(&ssl->handshake->ecdh_ctx, &zlen,
6700 p + 2, end - (p + 2),
6701 ssl->conf->f_rng, ssl->conf->p_rng)) != 0) {
6702 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ecdh_calc_secret", ret);
6703 return ret;
Jerry Yuce3dca42022-02-17 14:16:37 +08006704 }
6705
Gilles Peskine449bd832023-01-11 14:50:10 +01006706 MBEDTLS_PUT_UINT16_BE(zlen, p, 0);
Jerry Yuce3dca42022-02-17 14:16:37 +08006707 p += 2 + zlen;
6708
Gilles Peskine449bd832023-01-11 14:50:10 +01006709 MBEDTLS_SSL_DEBUG_ECDH(3, &ssl->handshake->ecdh_ctx,
6710 MBEDTLS_DEBUG_ECDH_Z);
6711 } else
Neil Armstrong80f6f322022-05-03 17:56:38 +02006712#endif /* MBEDTLS_KEY_EXCHANGE_ECDHE_PSK_ENABLED */
Jerry Yuce3dca42022-02-17 14:16:37 +08006713 {
Gilles Peskine449bd832023-01-11 14:50:10 +01006714 MBEDTLS_SSL_DEBUG_MSG(1, ("should never happen"));
6715 return MBEDTLS_ERR_SSL_INTERNAL_ERROR;
Jerry Yuce3dca42022-02-17 14:16:37 +08006716 }
6717
6718 /* opaque psk<0..2^16-1>; */
Gilles Peskine449bd832023-01-11 14:50:10 +01006719 if (end - p < 2) {
6720 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
6721 }
Jerry Yuce3dca42022-02-17 14:16:37 +08006722
Gilles Peskine449bd832023-01-11 14:50:10 +01006723 MBEDTLS_PUT_UINT16_BE(psk_len, p, 0);
Jerry Yuce3dca42022-02-17 14:16:37 +08006724 p += 2;
6725
Gilles Peskine449bd832023-01-11 14:50:10 +01006726 if (end < p || (size_t) (end - p) < psk_len) {
6727 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
6728 }
Jerry Yuce3dca42022-02-17 14:16:37 +08006729
Gilles Peskine449bd832023-01-11 14:50:10 +01006730 memcpy(p, psk, psk_len);
Jerry Yuce3dca42022-02-17 14:16:37 +08006731 p += psk_len;
6732
6733 ssl->handshake->pmslen = p - ssl->handshake->premaster;
6734
Gilles Peskine449bd832023-01-11 14:50:10 +01006735 return 0;
Jerry Yuce3dca42022-02-17 14:16:37 +08006736}
Neil Armstrong80f6f322022-05-03 17:56:38 +02006737#endif /* !MBEDTLS_USE_PSA_CRYPTO && MBEDTLS_KEY_EXCHANGE_SOME_PSK_ENABLED */
Jerry Yuc2c673d2022-02-17 14:20:39 +08006738
6739#if defined(MBEDTLS_SSL_SRV_C) && defined(MBEDTLS_SSL_RENEGOTIATION)
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02006740MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01006741static int ssl_write_hello_request(mbedtls_ssl_context *ssl);
Jerry Yuc2c673d2022-02-17 14:20:39 +08006742
6743#if defined(MBEDTLS_SSL_PROTO_DTLS)
Gilles Peskine449bd832023-01-11 14:50:10 +01006744int mbedtls_ssl_resend_hello_request(mbedtls_ssl_context *ssl)
Jerry Yuc2c673d2022-02-17 14:20:39 +08006745{
6746 /* If renegotiation is not enforced, retransmit until we would reach max
6747 * timeout if we were using the usual handshake doubling scheme */
Gilles Peskine449bd832023-01-11 14:50:10 +01006748 if (ssl->conf->renego_max_records < 0) {
Jerry Yuc2c673d2022-02-17 14:20:39 +08006749 uint32_t ratio = ssl->conf->hs_timeout_max / ssl->conf->hs_timeout_min + 1;
6750 unsigned char doublings = 1;
6751
Gilles Peskine449bd832023-01-11 14:50:10 +01006752 while (ratio != 0) {
Jerry Yuc2c673d2022-02-17 14:20:39 +08006753 ++doublings;
6754 ratio >>= 1;
6755 }
6756
Gilles Peskine449bd832023-01-11 14:50:10 +01006757 if (++ssl->renego_records_seen > doublings) {
6758 MBEDTLS_SSL_DEBUG_MSG(2, ("no longer retransmitting hello request"));
6759 return 0;
Jerry Yuc2c673d2022-02-17 14:20:39 +08006760 }
6761 }
6762
Gilles Peskine449bd832023-01-11 14:50:10 +01006763 return ssl_write_hello_request(ssl);
Jerry Yuc2c673d2022-02-17 14:20:39 +08006764}
6765#endif
6766#endif /* MBEDTLS_SSL_SRV_C && MBEDTLS_SSL_RENEGOTIATION */
Jerry Yud9526692022-02-17 14:23:47 +08006767
Jerry Yud9526692022-02-17 14:23:47 +08006768/*
6769 * Handshake functions
6770 */
6771#if !defined(MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED)
6772/* No certificate support -> dummy functions */
Gilles Peskine449bd832023-01-11 14:50:10 +01006773int mbedtls_ssl_write_certificate(mbedtls_ssl_context *ssl)
Jerry Yud9526692022-02-17 14:23:47 +08006774{
6775 const mbedtls_ssl_ciphersuite_t *ciphersuite_info =
6776 ssl->handshake->ciphersuite_info;
6777
Gilles Peskine449bd832023-01-11 14:50:10 +01006778 MBEDTLS_SSL_DEBUG_MSG(2, ("=> write certificate"));
Jerry Yud9526692022-02-17 14:23:47 +08006779
Gilles Peskine449bd832023-01-11 14:50:10 +01006780 if (!mbedtls_ssl_ciphersuite_uses_srv_cert(ciphersuite_info)) {
6781 MBEDTLS_SSL_DEBUG_MSG(2, ("<= skip write certificate"));
Jerry Yud9526692022-02-17 14:23:47 +08006782 ssl->state++;
Gilles Peskine449bd832023-01-11 14:50:10 +01006783 return 0;
Jerry Yud9526692022-02-17 14:23:47 +08006784 }
6785
Gilles Peskine449bd832023-01-11 14:50:10 +01006786 MBEDTLS_SSL_DEBUG_MSG(1, ("should never happen"));
6787 return MBEDTLS_ERR_SSL_INTERNAL_ERROR;
Jerry Yud9526692022-02-17 14:23:47 +08006788}
6789
Gilles Peskine449bd832023-01-11 14:50:10 +01006790int mbedtls_ssl_parse_certificate(mbedtls_ssl_context *ssl)
Jerry Yud9526692022-02-17 14:23:47 +08006791{
6792 const mbedtls_ssl_ciphersuite_t *ciphersuite_info =
6793 ssl->handshake->ciphersuite_info;
6794
Gilles Peskine449bd832023-01-11 14:50:10 +01006795 MBEDTLS_SSL_DEBUG_MSG(2, ("=> parse certificate"));
Jerry Yud9526692022-02-17 14:23:47 +08006796
Gilles Peskine449bd832023-01-11 14:50:10 +01006797 if (!mbedtls_ssl_ciphersuite_uses_srv_cert(ciphersuite_info)) {
6798 MBEDTLS_SSL_DEBUG_MSG(2, ("<= skip parse certificate"));
Jerry Yud9526692022-02-17 14:23:47 +08006799 ssl->state++;
Gilles Peskine449bd832023-01-11 14:50:10 +01006800 return 0;
Jerry Yud9526692022-02-17 14:23:47 +08006801 }
6802
Gilles Peskine449bd832023-01-11 14:50:10 +01006803 MBEDTLS_SSL_DEBUG_MSG(1, ("should never happen"));
6804 return MBEDTLS_ERR_SSL_INTERNAL_ERROR;
Jerry Yud9526692022-02-17 14:23:47 +08006805}
6806
6807#else /* MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED */
6808/* Some certificate support -> implement write and parse */
6809
Gilles Peskine449bd832023-01-11 14:50:10 +01006810int mbedtls_ssl_write_certificate(mbedtls_ssl_context *ssl)
Jerry Yud9526692022-02-17 14:23:47 +08006811{
6812 int ret = MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
6813 size_t i, n;
6814 const mbedtls_x509_crt *crt;
6815 const mbedtls_ssl_ciphersuite_t *ciphersuite_info =
6816 ssl->handshake->ciphersuite_info;
6817
Gilles Peskine449bd832023-01-11 14:50:10 +01006818 MBEDTLS_SSL_DEBUG_MSG(2, ("=> write certificate"));
Jerry Yud9526692022-02-17 14:23:47 +08006819
Gilles Peskine449bd832023-01-11 14:50:10 +01006820 if (!mbedtls_ssl_ciphersuite_uses_srv_cert(ciphersuite_info)) {
6821 MBEDTLS_SSL_DEBUG_MSG(2, ("<= skip write certificate"));
Jerry Yud9526692022-02-17 14:23:47 +08006822 ssl->state++;
Gilles Peskine449bd832023-01-11 14:50:10 +01006823 return 0;
Jerry Yud9526692022-02-17 14:23:47 +08006824 }
6825
6826#if defined(MBEDTLS_SSL_CLI_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01006827 if (ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT) {
6828 if (ssl->handshake->client_auth == 0) {
6829 MBEDTLS_SSL_DEBUG_MSG(2, ("<= skip write certificate"));
Jerry Yud9526692022-02-17 14:23:47 +08006830 ssl->state++;
Gilles Peskine449bd832023-01-11 14:50:10 +01006831 return 0;
Jerry Yud9526692022-02-17 14:23:47 +08006832 }
6833 }
6834#endif /* MBEDTLS_SSL_CLI_C */
6835#if defined(MBEDTLS_SSL_SRV_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01006836 if (ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER) {
6837 if (mbedtls_ssl_own_cert(ssl) == NULL) {
Jerry Yud9526692022-02-17 14:23:47 +08006838 /* Should never happen because we shouldn't have picked the
6839 * ciphersuite if we don't have a certificate. */
Gilles Peskine449bd832023-01-11 14:50:10 +01006840 return MBEDTLS_ERR_SSL_INTERNAL_ERROR;
Jerry Yud9526692022-02-17 14:23:47 +08006841 }
6842 }
6843#endif
6844
Gilles Peskine449bd832023-01-11 14:50:10 +01006845 MBEDTLS_SSL_DEBUG_CRT(3, "own certificate", mbedtls_ssl_own_cert(ssl));
Jerry Yud9526692022-02-17 14:23:47 +08006846
6847 /*
6848 * 0 . 0 handshake type
6849 * 1 . 3 handshake length
6850 * 4 . 6 length of all certs
6851 * 7 . 9 length of cert. 1
6852 * 10 . n-1 peer certificate
6853 * n . n+2 length of cert. 2
6854 * n+3 . ... upper level cert, etc.
6855 */
6856 i = 7;
Gilles Peskine449bd832023-01-11 14:50:10 +01006857 crt = mbedtls_ssl_own_cert(ssl);
Jerry Yud9526692022-02-17 14:23:47 +08006858
Gilles Peskine449bd832023-01-11 14:50:10 +01006859 while (crt != NULL) {
Jerry Yud9526692022-02-17 14:23:47 +08006860 n = crt->raw.len;
Gilles Peskine449bd832023-01-11 14:50:10 +01006861 if (n > MBEDTLS_SSL_OUT_CONTENT_LEN - 3 - i) {
6862 MBEDTLS_SSL_DEBUG_MSG(1, ("certificate too large, %" MBEDTLS_PRINTF_SIZET
6863 " > %" MBEDTLS_PRINTF_SIZET,
6864 i + 3 + n, (size_t) MBEDTLS_SSL_OUT_CONTENT_LEN));
6865 return MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL;
Jerry Yud9526692022-02-17 14:23:47 +08006866 }
6867
Gilles Peskine449bd832023-01-11 14:50:10 +01006868 ssl->out_msg[i] = MBEDTLS_BYTE_2(n);
6869 ssl->out_msg[i + 1] = MBEDTLS_BYTE_1(n);
6870 ssl->out_msg[i + 2] = MBEDTLS_BYTE_0(n);
Jerry Yud9526692022-02-17 14:23:47 +08006871
Gilles Peskine449bd832023-01-11 14:50:10 +01006872 i += 3; memcpy(ssl->out_msg + i, crt->raw.p, n);
Jerry Yud9526692022-02-17 14:23:47 +08006873 i += n; crt = crt->next;
6874 }
6875
Gilles Peskine449bd832023-01-11 14:50:10 +01006876 ssl->out_msg[4] = MBEDTLS_BYTE_2(i - 7);
6877 ssl->out_msg[5] = MBEDTLS_BYTE_1(i - 7);
6878 ssl->out_msg[6] = MBEDTLS_BYTE_0(i - 7);
Jerry Yud9526692022-02-17 14:23:47 +08006879
6880 ssl->out_msglen = i;
6881 ssl->out_msgtype = MBEDTLS_SSL_MSG_HANDSHAKE;
6882 ssl->out_msg[0] = MBEDTLS_SSL_HS_CERTIFICATE;
6883
6884 ssl->state++;
6885
Gilles Peskine449bd832023-01-11 14:50:10 +01006886 if ((ret = mbedtls_ssl_write_handshake_msg(ssl)) != 0) {
6887 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ssl_write_handshake_msg", ret);
6888 return ret;
Jerry Yud9526692022-02-17 14:23:47 +08006889 }
6890
Gilles Peskine449bd832023-01-11 14:50:10 +01006891 MBEDTLS_SSL_DEBUG_MSG(2, ("<= write certificate"));
Jerry Yud9526692022-02-17 14:23:47 +08006892
Gilles Peskine449bd832023-01-11 14:50:10 +01006893 return ret;
Jerry Yud9526692022-02-17 14:23:47 +08006894}
6895
6896#if defined(MBEDTLS_SSL_RENEGOTIATION) && defined(MBEDTLS_SSL_CLI_C)
6897
6898#if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02006899MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01006900static int ssl_check_peer_crt_unchanged(mbedtls_ssl_context *ssl,
6901 unsigned char *crt_buf,
6902 size_t crt_buf_len)
Jerry Yud9526692022-02-17 14:23:47 +08006903{
6904 mbedtls_x509_crt const * const peer_crt = ssl->session->peer_cert;
6905
Gilles Peskine449bd832023-01-11 14:50:10 +01006906 if (peer_crt == NULL) {
6907 return -1;
6908 }
Jerry Yud9526692022-02-17 14:23:47 +08006909
Gilles Peskine449bd832023-01-11 14:50:10 +01006910 if (peer_crt->raw.len != crt_buf_len) {
6911 return -1;
6912 }
Jerry Yud9526692022-02-17 14:23:47 +08006913
Gilles Peskine449bd832023-01-11 14:50:10 +01006914 return memcmp(peer_crt->raw.p, crt_buf, peer_crt->raw.len);
Jerry Yud9526692022-02-17 14:23:47 +08006915}
6916#else /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02006917MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01006918static int ssl_check_peer_crt_unchanged(mbedtls_ssl_context *ssl,
6919 unsigned char *crt_buf,
6920 size_t crt_buf_len)
Jerry Yud9526692022-02-17 14:23:47 +08006921{
6922 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
6923 unsigned char const * const peer_cert_digest =
6924 ssl->session->peer_cert_digest;
6925 mbedtls_md_type_t const peer_cert_digest_type =
6926 ssl->session->peer_cert_digest_type;
6927 mbedtls_md_info_t const * const digest_info =
Gilles Peskine449bd832023-01-11 14:50:10 +01006928 mbedtls_md_info_from_type(peer_cert_digest_type);
Jerry Yud9526692022-02-17 14:23:47 +08006929 unsigned char tmp_digest[MBEDTLS_SSL_PEER_CERT_DIGEST_MAX_LEN];
6930 size_t digest_len;
6931
Gilles Peskine449bd832023-01-11 14:50:10 +01006932 if (peer_cert_digest == NULL || digest_info == NULL) {
6933 return -1;
6934 }
Jerry Yud9526692022-02-17 14:23:47 +08006935
Gilles Peskine449bd832023-01-11 14:50:10 +01006936 digest_len = mbedtls_md_get_size(digest_info);
6937 if (digest_len > MBEDTLS_SSL_PEER_CERT_DIGEST_MAX_LEN) {
6938 return -1;
6939 }
Jerry Yud9526692022-02-17 14:23:47 +08006940
Gilles Peskine449bd832023-01-11 14:50:10 +01006941 ret = mbedtls_md(digest_info, crt_buf, crt_buf_len, tmp_digest);
6942 if (ret != 0) {
6943 return -1;
6944 }
Jerry Yud9526692022-02-17 14:23:47 +08006945
Gilles Peskine449bd832023-01-11 14:50:10 +01006946 return memcmp(tmp_digest, peer_cert_digest, digest_len);
Jerry Yud9526692022-02-17 14:23:47 +08006947}
6948#endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
6949#endif /* MBEDTLS_SSL_RENEGOTIATION && MBEDTLS_SSL_CLI_C */
6950
6951/*
6952 * Once the certificate message is read, parse it into a cert chain and
6953 * perform basic checks, but leave actual verification to the caller
6954 */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02006955MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01006956static int ssl_parse_certificate_chain(mbedtls_ssl_context *ssl,
6957 mbedtls_x509_crt *chain)
Jerry Yud9526692022-02-17 14:23:47 +08006958{
6959 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
6960#if defined(MBEDTLS_SSL_RENEGOTIATION) && defined(MBEDTLS_SSL_CLI_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01006961 int crt_cnt = 0;
Jerry Yud9526692022-02-17 14:23:47 +08006962#endif
6963 size_t i, n;
6964 uint8_t alert;
6965
Gilles Peskine449bd832023-01-11 14:50:10 +01006966 if (ssl->in_msgtype != MBEDTLS_SSL_MSG_HANDSHAKE) {
6967 MBEDTLS_SSL_DEBUG_MSG(1, ("bad certificate message"));
6968 mbedtls_ssl_send_alert_message(ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6969 MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE);
6970 return MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE;
Jerry Yud9526692022-02-17 14:23:47 +08006971 }
6972
Gilles Peskine449bd832023-01-11 14:50:10 +01006973 if (ssl->in_msg[0] != MBEDTLS_SSL_HS_CERTIFICATE) {
6974 mbedtls_ssl_send_alert_message(ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6975 MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE);
6976 return MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE;
Jerry Yud9526692022-02-17 14:23:47 +08006977 }
6978
Gilles Peskine449bd832023-01-11 14:50:10 +01006979 if (ssl->in_hslen < mbedtls_ssl_hs_hdr_len(ssl) + 3 + 3) {
6980 MBEDTLS_SSL_DEBUG_MSG(1, ("bad certificate message"));
6981 mbedtls_ssl_send_alert_message(ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6982 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR);
6983 return MBEDTLS_ERR_SSL_DECODE_ERROR;
Jerry Yud9526692022-02-17 14:23:47 +08006984 }
6985
Gilles Peskine449bd832023-01-11 14:50:10 +01006986 i = mbedtls_ssl_hs_hdr_len(ssl);
Jerry Yud9526692022-02-17 14:23:47 +08006987
6988 /*
6989 * Same message structure as in mbedtls_ssl_write_certificate()
6990 */
Gilles Peskine449bd832023-01-11 14:50:10 +01006991 n = (ssl->in_msg[i+1] << 8) | ssl->in_msg[i+2];
Jerry Yud9526692022-02-17 14:23:47 +08006992
Gilles Peskine449bd832023-01-11 14:50:10 +01006993 if (ssl->in_msg[i] != 0 ||
6994 ssl->in_hslen != n + 3 + mbedtls_ssl_hs_hdr_len(ssl)) {
6995 MBEDTLS_SSL_DEBUG_MSG(1, ("bad certificate message"));
6996 mbedtls_ssl_send_alert_message(ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6997 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR);
6998 return MBEDTLS_ERR_SSL_DECODE_ERROR;
Jerry Yud9526692022-02-17 14:23:47 +08006999 }
7000
7001 /* Make &ssl->in_msg[i] point to the beginning of the CRT chain. */
7002 i += 3;
7003
7004 /* Iterate through and parse the CRTs in the provided chain. */
Gilles Peskine449bd832023-01-11 14:50:10 +01007005 while (i < ssl->in_hslen) {
Jerry Yud9526692022-02-17 14:23:47 +08007006 /* Check that there's room for the next CRT's length fields. */
Gilles Peskine449bd832023-01-11 14:50:10 +01007007 if (i + 3 > ssl->in_hslen) {
7008 MBEDTLS_SSL_DEBUG_MSG(1, ("bad certificate message"));
7009 mbedtls_ssl_send_alert_message(ssl,
7010 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
7011 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR);
7012 return MBEDTLS_ERR_SSL_DECODE_ERROR;
Jerry Yud9526692022-02-17 14:23:47 +08007013 }
7014 /* In theory, the CRT can be up to 2**24 Bytes, but we don't support
7015 * anything beyond 2**16 ~ 64K. */
Gilles Peskine449bd832023-01-11 14:50:10 +01007016 if (ssl->in_msg[i] != 0) {
7017 MBEDTLS_SSL_DEBUG_MSG(1, ("bad certificate message"));
7018 mbedtls_ssl_send_alert_message(ssl,
7019 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
7020 MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT);
7021 return MBEDTLS_ERR_SSL_BAD_CERTIFICATE;
Jerry Yud9526692022-02-17 14:23:47 +08007022 }
7023
7024 /* Read length of the next CRT in the chain. */
Gilles Peskine449bd832023-01-11 14:50:10 +01007025 n = ((unsigned int) ssl->in_msg[i + 1] << 8)
Jerry Yud9526692022-02-17 14:23:47 +08007026 | (unsigned int) ssl->in_msg[i + 2];
7027 i += 3;
7028
Gilles Peskine449bd832023-01-11 14:50:10 +01007029 if (n < 128 || i + n > ssl->in_hslen) {
7030 MBEDTLS_SSL_DEBUG_MSG(1, ("bad certificate message"));
7031 mbedtls_ssl_send_alert_message(ssl,
7032 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
7033 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR);
7034 return MBEDTLS_ERR_SSL_DECODE_ERROR;
Jerry Yud9526692022-02-17 14:23:47 +08007035 }
7036
7037 /* Check if we're handling the first CRT in the chain. */
7038#if defined(MBEDTLS_SSL_RENEGOTIATION) && defined(MBEDTLS_SSL_CLI_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01007039 if (crt_cnt++ == 0 &&
Jerry Yud9526692022-02-17 14:23:47 +08007040 ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT &&
Gilles Peskine449bd832023-01-11 14:50:10 +01007041 ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS) {
Jerry Yud9526692022-02-17 14:23:47 +08007042 /* During client-side renegotiation, check that the server's
7043 * end-CRTs hasn't changed compared to the initial handshake,
7044 * mitigating the triple handshake attack. On success, reuse
7045 * the original end-CRT instead of parsing it again. */
Gilles Peskine449bd832023-01-11 14:50:10 +01007046 MBEDTLS_SSL_DEBUG_MSG(3, ("Check that peer CRT hasn't changed during renegotiation"));
7047 if (ssl_check_peer_crt_unchanged(ssl,
7048 &ssl->in_msg[i],
7049 n) != 0) {
7050 MBEDTLS_SSL_DEBUG_MSG(1, ("new server cert during renegotiation"));
7051 mbedtls_ssl_send_alert_message(ssl,
7052 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
7053 MBEDTLS_SSL_ALERT_MSG_ACCESS_DENIED);
7054 return MBEDTLS_ERR_SSL_BAD_CERTIFICATE;
Jerry Yud9526692022-02-17 14:23:47 +08007055 }
7056
7057 /* Now we can safely free the original chain. */
Gilles Peskine449bd832023-01-11 14:50:10 +01007058 ssl_clear_peer_cert(ssl->session);
Jerry Yud9526692022-02-17 14:23:47 +08007059 }
7060#endif /* MBEDTLS_SSL_RENEGOTIATION && MBEDTLS_SSL_CLI_C */
7061
7062 /* Parse the next certificate in the chain. */
7063#if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
Gilles Peskine449bd832023-01-11 14:50:10 +01007064 ret = mbedtls_x509_crt_parse_der(chain, ssl->in_msg + i, n);
Jerry Yud9526692022-02-17 14:23:47 +08007065#else
7066 /* If we don't need to store the CRT chain permanently, parse
7067 * it in-place from the input buffer instead of making a copy. */
Gilles Peskine449bd832023-01-11 14:50:10 +01007068 ret = mbedtls_x509_crt_parse_der_nocopy(chain, ssl->in_msg + i, n);
Jerry Yud9526692022-02-17 14:23:47 +08007069#endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
Gilles Peskine449bd832023-01-11 14:50:10 +01007070 switch (ret) {
Jerry Yud9526692022-02-17 14:23:47 +08007071 case 0: /*ok*/
7072 case MBEDTLS_ERR_X509_UNKNOWN_SIG_ALG + MBEDTLS_ERR_OID_NOT_FOUND:
7073 /* Ignore certificate with an unknown algorithm: maybe a
7074 prior certificate was already trusted. */
7075 break;
7076
7077 case MBEDTLS_ERR_X509_ALLOC_FAILED:
7078 alert = MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR;
7079 goto crt_parse_der_failed;
7080
7081 case MBEDTLS_ERR_X509_UNKNOWN_VERSION:
7082 alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT;
7083 goto crt_parse_der_failed;
7084
7085 default:
7086 alert = MBEDTLS_SSL_ALERT_MSG_BAD_CERT;
Gilles Peskine449bd832023-01-11 14:50:10 +01007087crt_parse_der_failed:
7088 mbedtls_ssl_send_alert_message(ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL, alert);
7089 MBEDTLS_SSL_DEBUG_RET(1, " mbedtls_x509_crt_parse_der", ret);
7090 return ret;
Jerry Yud9526692022-02-17 14:23:47 +08007091 }
7092
7093 i += n;
7094 }
7095
Gilles Peskine449bd832023-01-11 14:50:10 +01007096 MBEDTLS_SSL_DEBUG_CRT(3, "peer certificate", chain);
7097 return 0;
Jerry Yud9526692022-02-17 14:23:47 +08007098}
7099
7100#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02007101MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01007102static int ssl_srv_check_client_no_crt_notification(mbedtls_ssl_context *ssl)
Jerry Yud9526692022-02-17 14:23:47 +08007103{
Gilles Peskine449bd832023-01-11 14:50:10 +01007104 if (ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT) {
7105 return -1;
7106 }
Jerry Yud9526692022-02-17 14:23:47 +08007107
Gilles Peskine449bd832023-01-11 14:50:10 +01007108 if (ssl->in_hslen == 3 + mbedtls_ssl_hs_hdr_len(ssl) &&
Jerry Yud9526692022-02-17 14:23:47 +08007109 ssl->in_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE &&
7110 ssl->in_msg[0] == MBEDTLS_SSL_HS_CERTIFICATE &&
Gilles Peskine449bd832023-01-11 14:50:10 +01007111 memcmp(ssl->in_msg + mbedtls_ssl_hs_hdr_len(ssl), "\0\0\0", 3) == 0) {
7112 MBEDTLS_SSL_DEBUG_MSG(1, ("peer has no certificate"));
7113 return 0;
Jerry Yud9526692022-02-17 14:23:47 +08007114 }
Gilles Peskine449bd832023-01-11 14:50:10 +01007115 return -1;
Jerry Yud9526692022-02-17 14:23:47 +08007116}
7117#endif /* MBEDTLS_SSL_SRV_C */
7118
7119/* Check if a certificate message is expected.
7120 * Return either
7121 * - SSL_CERTIFICATE_EXPECTED, or
7122 * - SSL_CERTIFICATE_SKIP
7123 * indicating whether a Certificate message is expected or not.
7124 */
7125#define SSL_CERTIFICATE_EXPECTED 0
7126#define SSL_CERTIFICATE_SKIP 1
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02007127MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01007128static int ssl_parse_certificate_coordinate(mbedtls_ssl_context *ssl,
7129 int authmode)
Jerry Yud9526692022-02-17 14:23:47 +08007130{
7131 const mbedtls_ssl_ciphersuite_t *ciphersuite_info =
7132 ssl->handshake->ciphersuite_info;
7133
Gilles Peskine449bd832023-01-11 14:50:10 +01007134 if (!mbedtls_ssl_ciphersuite_uses_srv_cert(ciphersuite_info)) {
7135 return SSL_CERTIFICATE_SKIP;
7136 }
Jerry Yud9526692022-02-17 14:23:47 +08007137
7138#if defined(MBEDTLS_SSL_SRV_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01007139 if (ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER) {
7140 if (ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_RSA_PSK) {
7141 return SSL_CERTIFICATE_SKIP;
7142 }
Jerry Yud9526692022-02-17 14:23:47 +08007143
Gilles Peskine449bd832023-01-11 14:50:10 +01007144 if (authmode == MBEDTLS_SSL_VERIFY_NONE) {
Jerry Yud9526692022-02-17 14:23:47 +08007145 ssl->session_negotiate->verify_result =
7146 MBEDTLS_X509_BADCERT_SKIP_VERIFY;
Gilles Peskine449bd832023-01-11 14:50:10 +01007147 return SSL_CERTIFICATE_SKIP;
Jerry Yud9526692022-02-17 14:23:47 +08007148 }
7149 }
7150#else
7151 ((void) authmode);
7152#endif /* MBEDTLS_SSL_SRV_C */
7153
Gilles Peskine449bd832023-01-11 14:50:10 +01007154 return SSL_CERTIFICATE_EXPECTED;
Jerry Yud9526692022-02-17 14:23:47 +08007155}
7156
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02007157MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01007158static int ssl_parse_certificate_verify(mbedtls_ssl_context *ssl,
7159 int authmode,
7160 mbedtls_x509_crt *chain,
7161 void *rs_ctx)
Jerry Yud9526692022-02-17 14:23:47 +08007162{
7163 int ret = 0;
7164 const mbedtls_ssl_ciphersuite_t *ciphersuite_info =
7165 ssl->handshake->ciphersuite_info;
7166 int have_ca_chain = 0;
7167
7168 int (*f_vrfy)(void *, mbedtls_x509_crt *, int, uint32_t *);
7169 void *p_vrfy;
7170
Gilles Peskine449bd832023-01-11 14:50:10 +01007171 if (authmode == MBEDTLS_SSL_VERIFY_NONE) {
7172 return 0;
7173 }
Jerry Yud9526692022-02-17 14:23:47 +08007174
Gilles Peskine449bd832023-01-11 14:50:10 +01007175 if (ssl->f_vrfy != NULL) {
7176 MBEDTLS_SSL_DEBUG_MSG(3, ("Use context-specific verification callback"));
Jerry Yud9526692022-02-17 14:23:47 +08007177 f_vrfy = ssl->f_vrfy;
7178 p_vrfy = ssl->p_vrfy;
Gilles Peskine449bd832023-01-11 14:50:10 +01007179 } else {
7180 MBEDTLS_SSL_DEBUG_MSG(3, ("Use configuration-specific verification callback"));
Jerry Yud9526692022-02-17 14:23:47 +08007181 f_vrfy = ssl->conf->f_vrfy;
7182 p_vrfy = ssl->conf->p_vrfy;
7183 }
7184
7185 /*
7186 * Main check: verify certificate
7187 */
7188#if defined(MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK)
Gilles Peskine449bd832023-01-11 14:50:10 +01007189 if (ssl->conf->f_ca_cb != NULL) {
Jerry Yud9526692022-02-17 14:23:47 +08007190 ((void) rs_ctx);
7191 have_ca_chain = 1;
7192
Gilles Peskine449bd832023-01-11 14:50:10 +01007193 MBEDTLS_SSL_DEBUG_MSG(3, ("use CA callback for X.509 CRT verification"));
Jerry Yud9526692022-02-17 14:23:47 +08007194 ret = mbedtls_x509_crt_verify_with_ca_cb(
7195 chain,
7196 ssl->conf->f_ca_cb,
7197 ssl->conf->p_ca_cb,
7198 ssl->conf->cert_profile,
7199 ssl->hostname,
7200 &ssl->session_negotiate->verify_result,
Gilles Peskine449bd832023-01-11 14:50:10 +01007201 f_vrfy, p_vrfy);
7202 } else
Jerry Yud9526692022-02-17 14:23:47 +08007203#endif /* MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK */
7204 {
7205 mbedtls_x509_crt *ca_chain;
7206 mbedtls_x509_crl *ca_crl;
7207
7208#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
Gilles Peskine449bd832023-01-11 14:50:10 +01007209 if (ssl->handshake->sni_ca_chain != NULL) {
Jerry Yud9526692022-02-17 14:23:47 +08007210 ca_chain = ssl->handshake->sni_ca_chain;
7211 ca_crl = ssl->handshake->sni_ca_crl;
Gilles Peskine449bd832023-01-11 14:50:10 +01007212 } else
Jerry Yud9526692022-02-17 14:23:47 +08007213#endif
7214 {
7215 ca_chain = ssl->conf->ca_chain;
7216 ca_crl = ssl->conf->ca_crl;
7217 }
7218
Gilles Peskine449bd832023-01-11 14:50:10 +01007219 if (ca_chain != NULL) {
Jerry Yud9526692022-02-17 14:23:47 +08007220 have_ca_chain = 1;
Gilles Peskine449bd832023-01-11 14:50:10 +01007221 }
Jerry Yud9526692022-02-17 14:23:47 +08007222
7223 ret = mbedtls_x509_crt_verify_restartable(
7224 chain,
7225 ca_chain, ca_crl,
7226 ssl->conf->cert_profile,
7227 ssl->hostname,
7228 &ssl->session_negotiate->verify_result,
Gilles Peskine449bd832023-01-11 14:50:10 +01007229 f_vrfy, p_vrfy, rs_ctx);
Jerry Yud9526692022-02-17 14:23:47 +08007230 }
7231
Gilles Peskine449bd832023-01-11 14:50:10 +01007232 if (ret != 0) {
7233 MBEDTLS_SSL_DEBUG_RET(1, "x509_verify_cert", ret);
Jerry Yud9526692022-02-17 14:23:47 +08007234 }
7235
7236#if defined(MBEDTLS_SSL_ECP_RESTARTABLE_ENABLED)
Gilles Peskine449bd832023-01-11 14:50:10 +01007237 if (ret == MBEDTLS_ERR_ECP_IN_PROGRESS) {
7238 return MBEDTLS_ERR_SSL_CRYPTO_IN_PROGRESS;
7239 }
Jerry Yud9526692022-02-17 14:23:47 +08007240#endif
7241
7242 /*
7243 * Secondary checks: always done, but change 'ret' only if it was 0
7244 */
7245
7246#if defined(MBEDTLS_ECP_C)
7247 {
7248 const mbedtls_pk_context *pk = &chain->pk;
7249
Manuel Pégourié-Gonnard66b0d612022-06-17 10:49:29 +02007250 /* If certificate uses an EC key, make sure the curve is OK.
7251 * This is a public key, so it can't be opaque, so can_do() is a good
7252 * enough check to ensure pk_ec() is safe to use here. */
Gilles Peskine449bd832023-01-11 14:50:10 +01007253 if (mbedtls_pk_can_do(pk, MBEDTLS_PK_ECKEY)) {
Leonid Rozenboim19e59732022-08-08 16:52:38 -07007254 /* and in the unlikely case the above assumption no longer holds
7255 * we are making sure that pk_ec() here does not return a NULL
7256 */
Gilles Peskine449bd832023-01-11 14:50:10 +01007257 const mbedtls_ecp_keypair *ec = mbedtls_pk_ec(*pk);
7258 if (ec == NULL) {
7259 MBEDTLS_SSL_DEBUG_MSG(1, ("mbedtls_pk_ec() returned NULL"));
7260 return MBEDTLS_ERR_SSL_INTERNAL_ERROR;
Leonid Rozenboim19e59732022-08-08 16:52:38 -07007261 }
Jerry Yud9526692022-02-17 14:23:47 +08007262
Gilles Peskine449bd832023-01-11 14:50:10 +01007263 if (mbedtls_ssl_check_curve(ssl, ec->grp.id) != 0) {
Leonid Rozenboim19e59732022-08-08 16:52:38 -07007264 ssl->session_negotiate->verify_result |=
7265 MBEDTLS_X509_BADCERT_BAD_KEY;
7266
Gilles Peskine449bd832023-01-11 14:50:10 +01007267 MBEDTLS_SSL_DEBUG_MSG(1, ("bad certificate (EC key curve)"));
7268 if (ret == 0) {
Leonid Rozenboim19e59732022-08-08 16:52:38 -07007269 ret = MBEDTLS_ERR_SSL_BAD_CERTIFICATE;
Gilles Peskine449bd832023-01-11 14:50:10 +01007270 }
Leonid Rozenboim19e59732022-08-08 16:52:38 -07007271 }
Jerry Yud9526692022-02-17 14:23:47 +08007272 }
7273 }
7274#endif /* MBEDTLS_ECP_C */
7275
Gilles Peskine449bd832023-01-11 14:50:10 +01007276 if (mbedtls_ssl_check_cert_usage(chain,
7277 ciphersuite_info,
7278 !ssl->conf->endpoint,
7279 &ssl->session_negotiate->verify_result) != 0) {
7280 MBEDTLS_SSL_DEBUG_MSG(1, ("bad certificate (usage extensions)"));
7281 if (ret == 0) {
Jerry Yud9526692022-02-17 14:23:47 +08007282 ret = MBEDTLS_ERR_SSL_BAD_CERTIFICATE;
Gilles Peskine449bd832023-01-11 14:50:10 +01007283 }
Jerry Yud9526692022-02-17 14:23:47 +08007284 }
7285
7286 /* mbedtls_x509_crt_verify_with_profile is supposed to report a
7287 * verification failure through MBEDTLS_ERR_X509_CERT_VERIFY_FAILED,
7288 * with details encoded in the verification flags. All other kinds
7289 * of error codes, including those from the user provided f_vrfy
7290 * functions, are treated as fatal and lead to a failure of
7291 * ssl_parse_certificate even if verification was optional. */
Gilles Peskine449bd832023-01-11 14:50:10 +01007292 if (authmode == MBEDTLS_SSL_VERIFY_OPTIONAL &&
7293 (ret == MBEDTLS_ERR_X509_CERT_VERIFY_FAILED ||
7294 ret == MBEDTLS_ERR_SSL_BAD_CERTIFICATE)) {
Jerry Yud9526692022-02-17 14:23:47 +08007295 ret = 0;
7296 }
7297
Gilles Peskine449bd832023-01-11 14:50:10 +01007298 if (have_ca_chain == 0 && authmode == MBEDTLS_SSL_VERIFY_REQUIRED) {
7299 MBEDTLS_SSL_DEBUG_MSG(1, ("got no CA chain"));
Jerry Yud9526692022-02-17 14:23:47 +08007300 ret = MBEDTLS_ERR_SSL_CA_CHAIN_REQUIRED;
7301 }
7302
Gilles Peskine449bd832023-01-11 14:50:10 +01007303 if (ret != 0) {
Jerry Yud9526692022-02-17 14:23:47 +08007304 uint8_t alert;
7305
7306 /* The certificate may have been rejected for several reasons.
7307 Pick one and send the corresponding alert. Which alert to send
7308 may be a subject of debate in some cases. */
Gilles Peskine449bd832023-01-11 14:50:10 +01007309 if (ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_OTHER) {
Jerry Yud9526692022-02-17 14:23:47 +08007310 alert = MBEDTLS_SSL_ALERT_MSG_ACCESS_DENIED;
Gilles Peskine449bd832023-01-11 14:50:10 +01007311 } else if (ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_CN_MISMATCH) {
Jerry Yud9526692022-02-17 14:23:47 +08007312 alert = MBEDTLS_SSL_ALERT_MSG_BAD_CERT;
Gilles Peskine449bd832023-01-11 14:50:10 +01007313 } else if (ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_KEY_USAGE) {
Jerry Yud9526692022-02-17 14:23:47 +08007314 alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT;
Gilles Peskine449bd832023-01-11 14:50:10 +01007315 } else if (ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_EXT_KEY_USAGE) {
Jerry Yud9526692022-02-17 14:23:47 +08007316 alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT;
Gilles Peskine449bd832023-01-11 14:50:10 +01007317 } else if (ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_NS_CERT_TYPE) {
Jerry Yud9526692022-02-17 14:23:47 +08007318 alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT;
Gilles Peskine449bd832023-01-11 14:50:10 +01007319 } else if (ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_BAD_PK) {
Jerry Yud9526692022-02-17 14:23:47 +08007320 alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT;
Gilles Peskine449bd832023-01-11 14:50:10 +01007321 } else if (ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_BAD_KEY) {
Jerry Yud9526692022-02-17 14:23:47 +08007322 alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT;
Gilles Peskine449bd832023-01-11 14:50:10 +01007323 } else if (ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_EXPIRED) {
Jerry Yud9526692022-02-17 14:23:47 +08007324 alert = MBEDTLS_SSL_ALERT_MSG_CERT_EXPIRED;
Gilles Peskine449bd832023-01-11 14:50:10 +01007325 } else if (ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_REVOKED) {
Jerry Yud9526692022-02-17 14:23:47 +08007326 alert = MBEDTLS_SSL_ALERT_MSG_CERT_REVOKED;
Gilles Peskine449bd832023-01-11 14:50:10 +01007327 } else if (ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_NOT_TRUSTED) {
Jerry Yud9526692022-02-17 14:23:47 +08007328 alert = MBEDTLS_SSL_ALERT_MSG_UNKNOWN_CA;
Gilles Peskine449bd832023-01-11 14:50:10 +01007329 } else {
Jerry Yud9526692022-02-17 14:23:47 +08007330 alert = MBEDTLS_SSL_ALERT_MSG_CERT_UNKNOWN;
Gilles Peskine449bd832023-01-11 14:50:10 +01007331 }
7332 mbedtls_ssl_send_alert_message(ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
7333 alert);
Jerry Yud9526692022-02-17 14:23:47 +08007334 }
7335
7336#if defined(MBEDTLS_DEBUG_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01007337 if (ssl->session_negotiate->verify_result != 0) {
7338 MBEDTLS_SSL_DEBUG_MSG(3, ("! Certificate verification flags %08x",
7339 (unsigned int) ssl->session_negotiate->verify_result));
7340 } else {
7341 MBEDTLS_SSL_DEBUG_MSG(3, ("Certificate verification flags clear"));
Jerry Yud9526692022-02-17 14:23:47 +08007342 }
7343#endif /* MBEDTLS_DEBUG_C */
7344
Gilles Peskine449bd832023-01-11 14:50:10 +01007345 return ret;
Jerry Yud9526692022-02-17 14:23:47 +08007346}
7347
7348#if !defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02007349MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01007350static int ssl_remember_peer_crt_digest(mbedtls_ssl_context *ssl,
7351 unsigned char *start, size_t len)
Jerry Yud9526692022-02-17 14:23:47 +08007352{
7353 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
7354 /* Remember digest of the peer's end-CRT. */
7355 ssl->session_negotiate->peer_cert_digest =
Gilles Peskine449bd832023-01-11 14:50:10 +01007356 mbedtls_calloc(1, MBEDTLS_SSL_PEER_CERT_DIGEST_DFL_LEN);
7357 if (ssl->session_negotiate->peer_cert_digest == NULL) {
7358 MBEDTLS_SSL_DEBUG_MSG(1, ("alloc(%d bytes) failed",
7359 MBEDTLS_SSL_PEER_CERT_DIGEST_DFL_LEN));
7360 mbedtls_ssl_send_alert_message(ssl,
7361 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
7362 MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR);
Jerry Yud9526692022-02-17 14:23:47 +08007363
Gilles Peskine449bd832023-01-11 14:50:10 +01007364 return MBEDTLS_ERR_SSL_ALLOC_FAILED;
Jerry Yud9526692022-02-17 14:23:47 +08007365 }
7366
Gilles Peskine449bd832023-01-11 14:50:10 +01007367 ret = mbedtls_md(mbedtls_md_info_from_type(
7368 MBEDTLS_SSL_PEER_CERT_DIGEST_DFL_TYPE),
7369 start, len,
7370 ssl->session_negotiate->peer_cert_digest);
Jerry Yud9526692022-02-17 14:23:47 +08007371
7372 ssl->session_negotiate->peer_cert_digest_type =
7373 MBEDTLS_SSL_PEER_CERT_DIGEST_DFL_TYPE;
7374 ssl->session_negotiate->peer_cert_digest_len =
7375 MBEDTLS_SSL_PEER_CERT_DIGEST_DFL_LEN;
7376
Gilles Peskine449bd832023-01-11 14:50:10 +01007377 return ret;
Jerry Yud9526692022-02-17 14:23:47 +08007378}
7379
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02007380MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01007381static int ssl_remember_peer_pubkey(mbedtls_ssl_context *ssl,
7382 unsigned char *start, size_t len)
Jerry Yud9526692022-02-17 14:23:47 +08007383{
7384 unsigned char *end = start + len;
7385 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
7386
7387 /* Make a copy of the peer's raw public key. */
Gilles Peskine449bd832023-01-11 14:50:10 +01007388 mbedtls_pk_init(&ssl->handshake->peer_pubkey);
7389 ret = mbedtls_pk_parse_subpubkey(&start, end,
7390 &ssl->handshake->peer_pubkey);
7391 if (ret != 0) {
Jerry Yud9526692022-02-17 14:23:47 +08007392 /* We should have parsed the public key before. */
Gilles Peskine449bd832023-01-11 14:50:10 +01007393 return MBEDTLS_ERR_SSL_INTERNAL_ERROR;
Jerry Yud9526692022-02-17 14:23:47 +08007394 }
7395
Gilles Peskine449bd832023-01-11 14:50:10 +01007396 return 0;
Jerry Yud9526692022-02-17 14:23:47 +08007397}
7398#endif /* !MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
7399
Gilles Peskine449bd832023-01-11 14:50:10 +01007400int mbedtls_ssl_parse_certificate(mbedtls_ssl_context *ssl)
Jerry Yud9526692022-02-17 14:23:47 +08007401{
7402 int ret = 0;
7403 int crt_expected;
7404#if defined(MBEDTLS_SSL_SRV_C) && defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
7405 const int authmode = ssl->handshake->sni_authmode != MBEDTLS_SSL_VERIFY_UNSET
7406 ? ssl->handshake->sni_authmode
7407 : ssl->conf->authmode;
7408#else
7409 const int authmode = ssl->conf->authmode;
7410#endif
7411 void *rs_ctx = NULL;
7412 mbedtls_x509_crt *chain = NULL;
7413
Gilles Peskine449bd832023-01-11 14:50:10 +01007414 MBEDTLS_SSL_DEBUG_MSG(2, ("=> parse certificate"));
Jerry Yud9526692022-02-17 14:23:47 +08007415
Gilles Peskine449bd832023-01-11 14:50:10 +01007416 crt_expected = ssl_parse_certificate_coordinate(ssl, authmode);
7417 if (crt_expected == SSL_CERTIFICATE_SKIP) {
7418 MBEDTLS_SSL_DEBUG_MSG(2, ("<= skip parse certificate"));
Jerry Yud9526692022-02-17 14:23:47 +08007419 goto exit;
7420 }
7421
7422#if defined(MBEDTLS_SSL_ECP_RESTARTABLE_ENABLED)
Gilles Peskine449bd832023-01-11 14:50:10 +01007423 if (ssl->handshake->ecrs_enabled &&
7424 ssl->handshake->ecrs_state == ssl_ecrs_crt_verify) {
Jerry Yud9526692022-02-17 14:23:47 +08007425 chain = ssl->handshake->ecrs_peer_cert;
7426 ssl->handshake->ecrs_peer_cert = NULL;
7427 goto crt_verify;
7428 }
7429#endif
7430
Gilles Peskine449bd832023-01-11 14:50:10 +01007431 if ((ret = mbedtls_ssl_read_record(ssl, 1)) != 0) {
Jerry Yud9526692022-02-17 14:23:47 +08007432 /* mbedtls_ssl_read_record may have sent an alert already. We
7433 let it decide whether to alert. */
Gilles Peskine449bd832023-01-11 14:50:10 +01007434 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ssl_read_record", ret);
Jerry Yud9526692022-02-17 14:23:47 +08007435 goto exit;
7436 }
7437
7438#if defined(MBEDTLS_SSL_SRV_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01007439 if (ssl_srv_check_client_no_crt_notification(ssl) == 0) {
Jerry Yud9526692022-02-17 14:23:47 +08007440 ssl->session_negotiate->verify_result = MBEDTLS_X509_BADCERT_MISSING;
7441
Gilles Peskine449bd832023-01-11 14:50:10 +01007442 if (authmode != MBEDTLS_SSL_VERIFY_OPTIONAL) {
Jerry Yud9526692022-02-17 14:23:47 +08007443 ret = MBEDTLS_ERR_SSL_NO_CLIENT_CERTIFICATE;
Gilles Peskine449bd832023-01-11 14:50:10 +01007444 }
Jerry Yud9526692022-02-17 14:23:47 +08007445
7446 goto exit;
7447 }
7448#endif /* MBEDTLS_SSL_SRV_C */
7449
7450 /* Clear existing peer CRT structure in case we tried to
7451 * reuse a session but it failed, and allocate a new one. */
Gilles Peskine449bd832023-01-11 14:50:10 +01007452 ssl_clear_peer_cert(ssl->session_negotiate);
Jerry Yud9526692022-02-17 14:23:47 +08007453
Gilles Peskine449bd832023-01-11 14:50:10 +01007454 chain = mbedtls_calloc(1, sizeof(mbedtls_x509_crt));
7455 if (chain == NULL) {
7456 MBEDTLS_SSL_DEBUG_MSG(1, ("alloc(%" MBEDTLS_PRINTF_SIZET " bytes) failed",
7457 sizeof(mbedtls_x509_crt)));
7458 mbedtls_ssl_send_alert_message(ssl,
7459 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
7460 MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR);
Jerry Yud9526692022-02-17 14:23:47 +08007461
7462 ret = MBEDTLS_ERR_SSL_ALLOC_FAILED;
7463 goto exit;
7464 }
Gilles Peskine449bd832023-01-11 14:50:10 +01007465 mbedtls_x509_crt_init(chain);
Jerry Yud9526692022-02-17 14:23:47 +08007466
Gilles Peskine449bd832023-01-11 14:50:10 +01007467 ret = ssl_parse_certificate_chain(ssl, chain);
7468 if (ret != 0) {
Jerry Yud9526692022-02-17 14:23:47 +08007469 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01007470 }
Jerry Yud9526692022-02-17 14:23:47 +08007471
7472#if defined(MBEDTLS_SSL_ECP_RESTARTABLE_ENABLED)
Gilles Peskine449bd832023-01-11 14:50:10 +01007473 if (ssl->handshake->ecrs_enabled) {
Jerry Yud9526692022-02-17 14:23:47 +08007474 ssl->handshake->ecrs_state = ssl_ecrs_crt_verify;
Gilles Peskine449bd832023-01-11 14:50:10 +01007475 }
Jerry Yud9526692022-02-17 14:23:47 +08007476
7477crt_verify:
Gilles Peskine449bd832023-01-11 14:50:10 +01007478 if (ssl->handshake->ecrs_enabled) {
Jerry Yud9526692022-02-17 14:23:47 +08007479 rs_ctx = &ssl->handshake->ecrs_ctx;
Gilles Peskine449bd832023-01-11 14:50:10 +01007480 }
Jerry Yud9526692022-02-17 14:23:47 +08007481#endif
7482
Gilles Peskine449bd832023-01-11 14:50:10 +01007483 ret = ssl_parse_certificate_verify(ssl, authmode,
7484 chain, rs_ctx);
7485 if (ret != 0) {
Jerry Yud9526692022-02-17 14:23:47 +08007486 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01007487 }
Jerry Yud9526692022-02-17 14:23:47 +08007488
7489#if !defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
7490 {
7491 unsigned char *crt_start, *pk_start;
7492 size_t crt_len, pk_len;
7493
7494 /* We parse the CRT chain without copying, so
7495 * these pointers point into the input buffer,
7496 * and are hence still valid after freeing the
7497 * CRT chain. */
7498
7499 crt_start = chain->raw.p;
7500 crt_len = chain->raw.len;
7501
7502 pk_start = chain->pk_raw.p;
7503 pk_len = chain->pk_raw.len;
7504
7505 /* Free the CRT structures before computing
7506 * digest and copying the peer's public key. */
Gilles Peskine449bd832023-01-11 14:50:10 +01007507 mbedtls_x509_crt_free(chain);
7508 mbedtls_free(chain);
Jerry Yud9526692022-02-17 14:23:47 +08007509 chain = NULL;
7510
Gilles Peskine449bd832023-01-11 14:50:10 +01007511 ret = ssl_remember_peer_crt_digest(ssl, crt_start, crt_len);
7512 if (ret != 0) {
Jerry Yud9526692022-02-17 14:23:47 +08007513 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01007514 }
Jerry Yud9526692022-02-17 14:23:47 +08007515
Gilles Peskine449bd832023-01-11 14:50:10 +01007516 ret = ssl_remember_peer_pubkey(ssl, pk_start, pk_len);
7517 if (ret != 0) {
Jerry Yud9526692022-02-17 14:23:47 +08007518 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01007519 }
Jerry Yud9526692022-02-17 14:23:47 +08007520 }
7521#else /* !MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
7522 /* Pass ownership to session structure. */
7523 ssl->session_negotiate->peer_cert = chain;
7524 chain = NULL;
7525#endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
7526
Gilles Peskine449bd832023-01-11 14:50:10 +01007527 MBEDTLS_SSL_DEBUG_MSG(2, ("<= parse certificate"));
Jerry Yud9526692022-02-17 14:23:47 +08007528
7529exit:
7530
Gilles Peskine449bd832023-01-11 14:50:10 +01007531 if (ret == 0) {
Jerry Yud9526692022-02-17 14:23:47 +08007532 ssl->state++;
Gilles Peskine449bd832023-01-11 14:50:10 +01007533 }
Jerry Yud9526692022-02-17 14:23:47 +08007534
7535#if defined(MBEDTLS_SSL_ECP_RESTARTABLE_ENABLED)
Gilles Peskine449bd832023-01-11 14:50:10 +01007536 if (ret == MBEDTLS_ERR_SSL_CRYPTO_IN_PROGRESS) {
Jerry Yud9526692022-02-17 14:23:47 +08007537 ssl->handshake->ecrs_peer_cert = chain;
7538 chain = NULL;
7539 }
7540#endif
7541
Gilles Peskine449bd832023-01-11 14:50:10 +01007542 if (chain != NULL) {
7543 mbedtls_x509_crt_free(chain);
7544 mbedtls_free(chain);
Jerry Yud9526692022-02-17 14:23:47 +08007545 }
7546
Gilles Peskine449bd832023-01-11 14:50:10 +01007547 return ret;
Jerry Yud9526692022-02-17 14:23:47 +08007548}
7549#endif /* MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED */
7550
Andrzej Kurek25f27152022-08-17 16:09:31 -04007551#if defined(MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Manuel Pégourié-Gonnard226aa152023-02-05 09:46:59 +01007552static int ssl_calc_finished_tls_sha256(
Gilles Peskine449bd832023-01-11 14:50:10 +01007553 mbedtls_ssl_context *ssl, unsigned char *buf, int from)
Jerry Yu615bd6f2022-02-17 14:25:15 +08007554{
7555 int len = 12;
7556 const char *sender;
7557 unsigned char padbuf[32];
7558#if defined(MBEDTLS_USE_PSA_CRYPTO)
7559 size_t hash_size;
7560 psa_hash_operation_t sha256_psa = PSA_HASH_OPERATION_INIT;
7561 psa_status_t status;
7562#else
7563 mbedtls_sha256_context sha256;
7564#endif
7565
7566 mbedtls_ssl_session *session = ssl->session_negotiate;
Gilles Peskine449bd832023-01-11 14:50:10 +01007567 if (!session) {
Jerry Yu615bd6f2022-02-17 14:25:15 +08007568 session = ssl->session;
Gilles Peskine449bd832023-01-11 14:50:10 +01007569 }
Jerry Yu615bd6f2022-02-17 14:25:15 +08007570
Gilles Peskine449bd832023-01-11 14:50:10 +01007571 sender = (from == MBEDTLS_SSL_IS_CLIENT)
Jerry Yu615bd6f2022-02-17 14:25:15 +08007572 ? "client finished"
7573 : "server finished";
7574
7575#if defined(MBEDTLS_USE_PSA_CRYPTO)
7576 sha256_psa = psa_hash_operation_init();
7577
Gilles Peskine449bd832023-01-11 14:50:10 +01007578 MBEDTLS_SSL_DEBUG_MSG(2, ("=> calc PSA finished tls sha256"));
Jerry Yu615bd6f2022-02-17 14:25:15 +08007579
Gilles Peskine449bd832023-01-11 14:50:10 +01007580 status = psa_hash_clone(&ssl->handshake->fin_sha256_psa, &sha256_psa);
7581 if (status != PSA_SUCCESS) {
7582 MBEDTLS_SSL_DEBUG_MSG(2, ("PSA hash clone failed"));
Manuel Pégourié-Gonnard226aa152023-02-05 09:46:59 +01007583 return 0;
Jerry Yu615bd6f2022-02-17 14:25:15 +08007584 }
7585
Gilles Peskine449bd832023-01-11 14:50:10 +01007586 status = psa_hash_finish(&sha256_psa, padbuf, sizeof(padbuf), &hash_size);
7587 if (status != PSA_SUCCESS) {
7588 MBEDTLS_SSL_DEBUG_MSG(2, ("PSA hash finish failed"));
Manuel Pégourié-Gonnard226aa152023-02-05 09:46:59 +01007589 return 0;
Jerry Yu615bd6f2022-02-17 14:25:15 +08007590 }
Gilles Peskine449bd832023-01-11 14:50:10 +01007591 MBEDTLS_SSL_DEBUG_BUF(3, "PSA calculated padbuf", padbuf, 32);
Jerry Yu615bd6f2022-02-17 14:25:15 +08007592#else
7593
Gilles Peskine449bd832023-01-11 14:50:10 +01007594 mbedtls_sha256_init(&sha256);
Jerry Yu615bd6f2022-02-17 14:25:15 +08007595
Gilles Peskine449bd832023-01-11 14:50:10 +01007596 MBEDTLS_SSL_DEBUG_MSG(2, ("=> calc finished tls sha256"));
Jerry Yu615bd6f2022-02-17 14:25:15 +08007597
Gilles Peskine449bd832023-01-11 14:50:10 +01007598 mbedtls_sha256_clone(&sha256, &ssl->handshake->fin_sha256);
Jerry Yu615bd6f2022-02-17 14:25:15 +08007599
7600 /*
7601 * TLSv1.2:
7602 * hash = PRF( master, finished_label,
7603 * Hash( handshake ) )[0.11]
7604 */
7605
7606#if !defined(MBEDTLS_SHA256_ALT)
Gilles Peskine449bd832023-01-11 14:50:10 +01007607 MBEDTLS_SSL_DEBUG_BUF(4, "finished sha2 state", (unsigned char *)
7608 sha256.state, sizeof(sha256.state));
Jerry Yu615bd6f2022-02-17 14:25:15 +08007609#endif
7610
Gilles Peskine449bd832023-01-11 14:50:10 +01007611 mbedtls_sha256_finish(&sha256, padbuf);
7612 mbedtls_sha256_free(&sha256);
Jerry Yu615bd6f2022-02-17 14:25:15 +08007613#endif /* MBEDTLS_USE_PSA_CRYPTO */
7614
Gilles Peskine449bd832023-01-11 14:50:10 +01007615 ssl->handshake->tls_prf(session->master, 48, sender,
7616 padbuf, 32, buf, len);
Jerry Yu615bd6f2022-02-17 14:25:15 +08007617
Gilles Peskine449bd832023-01-11 14:50:10 +01007618 MBEDTLS_SSL_DEBUG_BUF(3, "calc finished result", buf, len);
Jerry Yu615bd6f2022-02-17 14:25:15 +08007619
Gilles Peskine449bd832023-01-11 14:50:10 +01007620 mbedtls_platform_zeroize(padbuf, sizeof(padbuf));
Jerry Yu615bd6f2022-02-17 14:25:15 +08007621
Gilles Peskine449bd832023-01-11 14:50:10 +01007622 MBEDTLS_SSL_DEBUG_MSG(2, ("<= calc finished"));
Manuel Pégourié-Gonnard226aa152023-02-05 09:46:59 +01007623 return 0;
Jerry Yu615bd6f2022-02-17 14:25:15 +08007624}
Andrzej Kurekcccb0442022-08-19 03:42:11 -04007625#endif /* MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA*/
Jerry Yu615bd6f2022-02-17 14:25:15 +08007626
Jerry Yub7ba49e2022-02-17 14:25:53 +08007627
Andrzej Kurek25f27152022-08-17 16:09:31 -04007628#if defined(MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Manuel Pégourié-Gonnard226aa152023-02-05 09:46:59 +01007629static int ssl_calc_finished_tls_sha384(
Gilles Peskine449bd832023-01-11 14:50:10 +01007630 mbedtls_ssl_context *ssl, unsigned char *buf, int from)
Jerry Yub7ba49e2022-02-17 14:25:53 +08007631{
7632 int len = 12;
7633 const char *sender;
7634 unsigned char padbuf[48];
7635#if defined(MBEDTLS_USE_PSA_CRYPTO)
7636 size_t hash_size;
7637 psa_hash_operation_t sha384_psa = PSA_HASH_OPERATION_INIT;
7638 psa_status_t status;
7639#else
7640 mbedtls_sha512_context sha512;
7641#endif
7642
7643 mbedtls_ssl_session *session = ssl->session_negotiate;
Gilles Peskine449bd832023-01-11 14:50:10 +01007644 if (!session) {
Jerry Yub7ba49e2022-02-17 14:25:53 +08007645 session = ssl->session;
Gilles Peskine449bd832023-01-11 14:50:10 +01007646 }
Jerry Yub7ba49e2022-02-17 14:25:53 +08007647
Gilles Peskine449bd832023-01-11 14:50:10 +01007648 sender = (from == MBEDTLS_SSL_IS_CLIENT)
Jerry Yub7ba49e2022-02-17 14:25:53 +08007649 ? "client finished"
7650 : "server finished";
7651
7652#if defined(MBEDTLS_USE_PSA_CRYPTO)
7653 sha384_psa = psa_hash_operation_init();
7654
Gilles Peskine449bd832023-01-11 14:50:10 +01007655 MBEDTLS_SSL_DEBUG_MSG(2, ("=> calc PSA finished tls sha384"));
Jerry Yub7ba49e2022-02-17 14:25:53 +08007656
Gilles Peskine449bd832023-01-11 14:50:10 +01007657 status = psa_hash_clone(&ssl->handshake->fin_sha384_psa, &sha384_psa);
7658 if (status != PSA_SUCCESS) {
7659 MBEDTLS_SSL_DEBUG_MSG(2, ("PSA hash clone failed"));
Manuel Pégourié-Gonnard226aa152023-02-05 09:46:59 +01007660 return 0;
Jerry Yub7ba49e2022-02-17 14:25:53 +08007661 }
7662
Gilles Peskine449bd832023-01-11 14:50:10 +01007663 status = psa_hash_finish(&sha384_psa, padbuf, sizeof(padbuf), &hash_size);
7664 if (status != PSA_SUCCESS) {
7665 MBEDTLS_SSL_DEBUG_MSG(2, ("PSA hash finish failed"));
Manuel Pégourié-Gonnard226aa152023-02-05 09:46:59 +01007666 return 0;
Jerry Yub7ba49e2022-02-17 14:25:53 +08007667 }
Gilles Peskine449bd832023-01-11 14:50:10 +01007668 MBEDTLS_SSL_DEBUG_BUF(3, "PSA calculated padbuf", padbuf, 48);
Jerry Yub7ba49e2022-02-17 14:25:53 +08007669#else
Gilles Peskine449bd832023-01-11 14:50:10 +01007670 mbedtls_sha512_init(&sha512);
Jerry Yub7ba49e2022-02-17 14:25:53 +08007671
Gilles Peskine449bd832023-01-11 14:50:10 +01007672 MBEDTLS_SSL_DEBUG_MSG(2, ("=> calc finished tls sha384"));
Jerry Yub7ba49e2022-02-17 14:25:53 +08007673
Gilles Peskine449bd832023-01-11 14:50:10 +01007674 mbedtls_sha512_clone(&sha512, &ssl->handshake->fin_sha384);
Jerry Yub7ba49e2022-02-17 14:25:53 +08007675
7676 /*
7677 * TLSv1.2:
7678 * hash = PRF( master, finished_label,
7679 * Hash( handshake ) )[0.11]
7680 */
7681
7682#if !defined(MBEDTLS_SHA512_ALT)
Gilles Peskine449bd832023-01-11 14:50:10 +01007683 MBEDTLS_SSL_DEBUG_BUF(4, "finished sha512 state", (unsigned char *)
7684 sha512.state, sizeof(sha512.state));
Jerry Yub7ba49e2022-02-17 14:25:53 +08007685#endif
Gilles Peskine449bd832023-01-11 14:50:10 +01007686 mbedtls_sha512_finish(&sha512, padbuf);
Jerry Yub7ba49e2022-02-17 14:25:53 +08007687
Gilles Peskine449bd832023-01-11 14:50:10 +01007688 mbedtls_sha512_free(&sha512);
Jerry Yub7ba49e2022-02-17 14:25:53 +08007689#endif
7690
Gilles Peskine449bd832023-01-11 14:50:10 +01007691 ssl->handshake->tls_prf(session->master, 48, sender,
7692 padbuf, 48, buf, len);
Jerry Yub7ba49e2022-02-17 14:25:53 +08007693
Gilles Peskine449bd832023-01-11 14:50:10 +01007694 MBEDTLS_SSL_DEBUG_BUF(3, "calc finished result", buf, len);
Jerry Yub7ba49e2022-02-17 14:25:53 +08007695
Gilles Peskine449bd832023-01-11 14:50:10 +01007696 mbedtls_platform_zeroize(padbuf, sizeof(padbuf));
Jerry Yub7ba49e2022-02-17 14:25:53 +08007697
Gilles Peskine449bd832023-01-11 14:50:10 +01007698 MBEDTLS_SSL_DEBUG_MSG(2, ("<= calc finished"));
Manuel Pégourié-Gonnard226aa152023-02-05 09:46:59 +01007699 return 0;
Jerry Yub7ba49e2022-02-17 14:25:53 +08007700}
Andrzej Kurekcccb0442022-08-19 03:42:11 -04007701#endif /* MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA*/
Jerry Yub7ba49e2022-02-17 14:25:53 +08007702
Gilles Peskine449bd832023-01-11 14:50:10 +01007703void mbedtls_ssl_handshake_wrapup_free_hs_transform(mbedtls_ssl_context *ssl)
Jerry Yuaef00152022-02-17 14:27:31 +08007704{
Gilles Peskine449bd832023-01-11 14:50:10 +01007705 MBEDTLS_SSL_DEBUG_MSG(3, ("=> handshake wrapup: final free"));
Jerry Yuaef00152022-02-17 14:27:31 +08007706
7707 /*
7708 * Free our handshake params
7709 */
Gilles Peskine449bd832023-01-11 14:50:10 +01007710 mbedtls_ssl_handshake_free(ssl);
7711 mbedtls_free(ssl->handshake);
Jerry Yuaef00152022-02-17 14:27:31 +08007712 ssl->handshake = NULL;
7713
7714 /*
Shaun Case8b0ecbc2021-12-20 21:14:10 -08007715 * Free the previous transform and switch in the current one
Jerry Yuaef00152022-02-17 14:27:31 +08007716 */
Gilles Peskine449bd832023-01-11 14:50:10 +01007717 if (ssl->transform) {
7718 mbedtls_ssl_transform_free(ssl->transform);
7719 mbedtls_free(ssl->transform);
Jerry Yuaef00152022-02-17 14:27:31 +08007720 }
7721 ssl->transform = ssl->transform_negotiate;
7722 ssl->transform_negotiate = NULL;
7723
Gilles Peskine449bd832023-01-11 14:50:10 +01007724 MBEDTLS_SSL_DEBUG_MSG(3, ("<= handshake wrapup: final free"));
Jerry Yuaef00152022-02-17 14:27:31 +08007725}
7726
Gilles Peskine449bd832023-01-11 14:50:10 +01007727void mbedtls_ssl_handshake_wrapup(mbedtls_ssl_context *ssl)
Jerry Yu2a9fff52022-02-17 14:28:51 +08007728{
7729 int resume = ssl->handshake->resume;
7730
Gilles Peskine449bd832023-01-11 14:50:10 +01007731 MBEDTLS_SSL_DEBUG_MSG(3, ("=> handshake wrapup"));
Jerry Yu2a9fff52022-02-17 14:28:51 +08007732
7733#if defined(MBEDTLS_SSL_RENEGOTIATION)
Gilles Peskine449bd832023-01-11 14:50:10 +01007734 if (ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS) {
Jerry Yu2a9fff52022-02-17 14:28:51 +08007735 ssl->renego_status = MBEDTLS_SSL_RENEGOTIATION_DONE;
7736 ssl->renego_records_seen = 0;
7737 }
7738#endif
7739
7740 /*
7741 * Free the previous session and switch in the current one
7742 */
Gilles Peskine449bd832023-01-11 14:50:10 +01007743 if (ssl->session) {
Jerry Yu2a9fff52022-02-17 14:28:51 +08007744#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
7745 /* RFC 7366 3.1: keep the EtM state */
7746 ssl->session_negotiate->encrypt_then_mac =
Gilles Peskine449bd832023-01-11 14:50:10 +01007747 ssl->session->encrypt_then_mac;
Jerry Yu2a9fff52022-02-17 14:28:51 +08007748#endif
7749
Gilles Peskine449bd832023-01-11 14:50:10 +01007750 mbedtls_ssl_session_free(ssl->session);
7751 mbedtls_free(ssl->session);
Jerry Yu2a9fff52022-02-17 14:28:51 +08007752 }
7753 ssl->session = ssl->session_negotiate;
7754 ssl->session_negotiate = NULL;
7755
7756 /*
7757 * Add cache entry
7758 */
Gilles Peskine449bd832023-01-11 14:50:10 +01007759 if (ssl->conf->f_set_cache != NULL &&
Jerry Yu2a9fff52022-02-17 14:28:51 +08007760 ssl->session->id_len != 0 &&
Gilles Peskine449bd832023-01-11 14:50:10 +01007761 resume == 0) {
7762 if (ssl->conf->f_set_cache(ssl->conf->p_cache,
7763 ssl->session->id,
7764 ssl->session->id_len,
7765 ssl->session) != 0) {
7766 MBEDTLS_SSL_DEBUG_MSG(1, ("cache did not store session"));
7767 }
Jerry Yu2a9fff52022-02-17 14:28:51 +08007768 }
7769
7770#if defined(MBEDTLS_SSL_PROTO_DTLS)
Gilles Peskine449bd832023-01-11 14:50:10 +01007771 if (ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
7772 ssl->handshake->flight != NULL) {
Jerry Yu2a9fff52022-02-17 14:28:51 +08007773 /* Cancel handshake timer */
Gilles Peskine449bd832023-01-11 14:50:10 +01007774 mbedtls_ssl_set_timer(ssl, 0);
Jerry Yu2a9fff52022-02-17 14:28:51 +08007775
7776 /* Keep last flight around in case we need to resend it:
7777 * we need the handshake and transform structures for that */
Gilles Peskine449bd832023-01-11 14:50:10 +01007778 MBEDTLS_SSL_DEBUG_MSG(3, ("skip freeing handshake and transform"));
7779 } else
Jerry Yu2a9fff52022-02-17 14:28:51 +08007780#endif
Gilles Peskine449bd832023-01-11 14:50:10 +01007781 mbedtls_ssl_handshake_wrapup_free_hs_transform(ssl);
Jerry Yu2a9fff52022-02-17 14:28:51 +08007782
Jerry Yu5ed73ff2022-10-27 13:08:42 +08007783 ssl->state = MBEDTLS_SSL_HANDSHAKE_OVER;
Jerry Yu2a9fff52022-02-17 14:28:51 +08007784
Gilles Peskine449bd832023-01-11 14:50:10 +01007785 MBEDTLS_SSL_DEBUG_MSG(3, ("<= handshake wrapup"));
Jerry Yu2a9fff52022-02-17 14:28:51 +08007786}
7787
Gilles Peskine449bd832023-01-11 14:50:10 +01007788int mbedtls_ssl_write_finished(mbedtls_ssl_context *ssl)
Jerry Yu3c8e47b2022-02-17 14:30:01 +08007789{
7790 int ret, hash_len;
7791
Gilles Peskine449bd832023-01-11 14:50:10 +01007792 MBEDTLS_SSL_DEBUG_MSG(2, ("=> write finished"));
Jerry Yu3c8e47b2022-02-17 14:30:01 +08007793
Gilles Peskine449bd832023-01-11 14:50:10 +01007794 mbedtls_ssl_update_out_pointers(ssl, ssl->transform_negotiate);
Jerry Yu3c8e47b2022-02-17 14:30:01 +08007795
Gilles Peskine449bd832023-01-11 14:50:10 +01007796 ssl->handshake->calc_finished(ssl, ssl->out_msg + 4, ssl->conf->endpoint);
Jerry Yu3c8e47b2022-02-17 14:30:01 +08007797
7798 /*
7799 * RFC 5246 7.4.9 (Page 63) says 12 is the default length and ciphersuites
7800 * may define some other value. Currently (early 2016), no defined
7801 * ciphersuite does this (and this is unlikely to change as activity has
7802 * moved to TLS 1.3 now) so we can keep the hardcoded 12 here.
7803 */
7804 hash_len = 12;
7805
7806#if defined(MBEDTLS_SSL_RENEGOTIATION)
7807 ssl->verify_data_len = hash_len;
Gilles Peskine449bd832023-01-11 14:50:10 +01007808 memcpy(ssl->own_verify_data, ssl->out_msg + 4, hash_len);
Jerry Yu3c8e47b2022-02-17 14:30:01 +08007809#endif
7810
7811 ssl->out_msglen = 4 + hash_len;
7812 ssl->out_msgtype = MBEDTLS_SSL_MSG_HANDSHAKE;
7813 ssl->out_msg[0] = MBEDTLS_SSL_HS_FINISHED;
7814
7815 /*
7816 * In case of session resuming, invert the client and server
7817 * ChangeCipherSpec messages order.
7818 */
Gilles Peskine449bd832023-01-11 14:50:10 +01007819 if (ssl->handshake->resume != 0) {
Jerry Yu3c8e47b2022-02-17 14:30:01 +08007820#if defined(MBEDTLS_SSL_CLI_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01007821 if (ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT) {
Jerry Yu3c8e47b2022-02-17 14:30:01 +08007822 ssl->state = MBEDTLS_SSL_HANDSHAKE_WRAPUP;
Gilles Peskine449bd832023-01-11 14:50:10 +01007823 }
Jerry Yu3c8e47b2022-02-17 14:30:01 +08007824#endif
7825#if defined(MBEDTLS_SSL_SRV_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01007826 if (ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER) {
Jerry Yu3c8e47b2022-02-17 14:30:01 +08007827 ssl->state = MBEDTLS_SSL_CLIENT_CHANGE_CIPHER_SPEC;
Gilles Peskine449bd832023-01-11 14:50:10 +01007828 }
Jerry Yu3c8e47b2022-02-17 14:30:01 +08007829#endif
Gilles Peskine449bd832023-01-11 14:50:10 +01007830 } else {
Jerry Yu3c8e47b2022-02-17 14:30:01 +08007831 ssl->state++;
Gilles Peskine449bd832023-01-11 14:50:10 +01007832 }
Jerry Yu3c8e47b2022-02-17 14:30:01 +08007833
7834 /*
7835 * Switch to our negotiated transform and session parameters for outbound
7836 * data.
7837 */
Gilles Peskine449bd832023-01-11 14:50:10 +01007838 MBEDTLS_SSL_DEBUG_MSG(3, ("switching to new transform spec for outbound data"));
Jerry Yu3c8e47b2022-02-17 14:30:01 +08007839
7840#if defined(MBEDTLS_SSL_PROTO_DTLS)
Gilles Peskine449bd832023-01-11 14:50:10 +01007841 if (ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM) {
Jerry Yu3c8e47b2022-02-17 14:30:01 +08007842 unsigned char i;
7843
7844 /* Remember current epoch settings for resending */
7845 ssl->handshake->alt_transform_out = ssl->transform_out;
Gilles Peskine449bd832023-01-11 14:50:10 +01007846 memcpy(ssl->handshake->alt_out_ctr, ssl->cur_out_ctr,
7847 sizeof(ssl->handshake->alt_out_ctr));
Jerry Yu3c8e47b2022-02-17 14:30:01 +08007848
7849 /* Set sequence_number to zero */
Gilles Peskine449bd832023-01-11 14:50:10 +01007850 memset(&ssl->cur_out_ctr[2], 0, sizeof(ssl->cur_out_ctr) - 2);
Jerry Yu3c8e47b2022-02-17 14:30:01 +08007851
7852
7853 /* Increment epoch */
Gilles Peskine449bd832023-01-11 14:50:10 +01007854 for (i = 2; i > 0; i--) {
7855 if (++ssl->cur_out_ctr[i - 1] != 0) {
Jerry Yu3c8e47b2022-02-17 14:30:01 +08007856 break;
Gilles Peskine449bd832023-01-11 14:50:10 +01007857 }
7858 }
Jerry Yu3c8e47b2022-02-17 14:30:01 +08007859
7860 /* The loop goes to its end iff the counter is wrapping */
Gilles Peskine449bd832023-01-11 14:50:10 +01007861 if (i == 0) {
7862 MBEDTLS_SSL_DEBUG_MSG(1, ("DTLS epoch would wrap"));
7863 return MBEDTLS_ERR_SSL_COUNTER_WRAPPING;
Jerry Yu3c8e47b2022-02-17 14:30:01 +08007864 }
Gilles Peskine449bd832023-01-11 14:50:10 +01007865 } else
Jerry Yu3c8e47b2022-02-17 14:30:01 +08007866#endif /* MBEDTLS_SSL_PROTO_DTLS */
Gilles Peskine449bd832023-01-11 14:50:10 +01007867 memset(ssl->cur_out_ctr, 0, sizeof(ssl->cur_out_ctr));
Jerry Yu3c8e47b2022-02-17 14:30:01 +08007868
7869 ssl->transform_out = ssl->transform_negotiate;
7870 ssl->session_out = ssl->session_negotiate;
7871
7872#if defined(MBEDTLS_SSL_PROTO_DTLS)
Gilles Peskine449bd832023-01-11 14:50:10 +01007873 if (ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM) {
7874 mbedtls_ssl_send_flight_completed(ssl);
7875 }
Jerry Yu3c8e47b2022-02-17 14:30:01 +08007876#endif
7877
Gilles Peskine449bd832023-01-11 14:50:10 +01007878 if ((ret = mbedtls_ssl_write_handshake_msg(ssl)) != 0) {
7879 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ssl_write_handshake_msg", ret);
7880 return ret;
Jerry Yu3c8e47b2022-02-17 14:30:01 +08007881 }
7882
7883#if defined(MBEDTLS_SSL_PROTO_DTLS)
Gilles Peskine449bd832023-01-11 14:50:10 +01007884 if (ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
7885 (ret = mbedtls_ssl_flight_transmit(ssl)) != 0) {
7886 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ssl_flight_transmit", ret);
7887 return ret;
Jerry Yu3c8e47b2022-02-17 14:30:01 +08007888 }
7889#endif
7890
Gilles Peskine449bd832023-01-11 14:50:10 +01007891 MBEDTLS_SSL_DEBUG_MSG(2, ("<= write finished"));
Jerry Yu3c8e47b2022-02-17 14:30:01 +08007892
Gilles Peskine449bd832023-01-11 14:50:10 +01007893 return 0;
Jerry Yu3c8e47b2022-02-17 14:30:01 +08007894}
7895
Jerry Yu0b3d7c12022-02-17 14:30:51 +08007896#define SSL_MAX_HASH_LEN 12
7897
Gilles Peskine449bd832023-01-11 14:50:10 +01007898int mbedtls_ssl_parse_finished(mbedtls_ssl_context *ssl)
Jerry Yu0b3d7c12022-02-17 14:30:51 +08007899{
7900 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
7901 unsigned int hash_len = 12;
7902 unsigned char buf[SSL_MAX_HASH_LEN];
7903
Gilles Peskine449bd832023-01-11 14:50:10 +01007904 MBEDTLS_SSL_DEBUG_MSG(2, ("=> parse finished"));
Jerry Yu0b3d7c12022-02-17 14:30:51 +08007905
Gilles Peskine449bd832023-01-11 14:50:10 +01007906 ssl->handshake->calc_finished(ssl, buf, ssl->conf->endpoint ^ 1);
Jerry Yu0b3d7c12022-02-17 14:30:51 +08007907
Gilles Peskine449bd832023-01-11 14:50:10 +01007908 if ((ret = mbedtls_ssl_read_record(ssl, 1)) != 0) {
7909 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ssl_read_record", ret);
Jerry Yu0b3d7c12022-02-17 14:30:51 +08007910 goto exit;
7911 }
7912
Gilles Peskine449bd832023-01-11 14:50:10 +01007913 if (ssl->in_msgtype != MBEDTLS_SSL_MSG_HANDSHAKE) {
7914 MBEDTLS_SSL_DEBUG_MSG(1, ("bad finished message"));
7915 mbedtls_ssl_send_alert_message(ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
7916 MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE);
Jerry Yu0b3d7c12022-02-17 14:30:51 +08007917 ret = MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE;
7918 goto exit;
7919 }
7920
Gilles Peskine449bd832023-01-11 14:50:10 +01007921 if (ssl->in_msg[0] != MBEDTLS_SSL_HS_FINISHED) {
7922 mbedtls_ssl_send_alert_message(ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
7923 MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE);
Jerry Yu0b3d7c12022-02-17 14:30:51 +08007924 ret = MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE;
7925 goto exit;
7926 }
7927
Gilles Peskine449bd832023-01-11 14:50:10 +01007928 if (ssl->in_hslen != mbedtls_ssl_hs_hdr_len(ssl) + hash_len) {
7929 MBEDTLS_SSL_DEBUG_MSG(1, ("bad finished message"));
7930 mbedtls_ssl_send_alert_message(ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
7931 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR);
Jerry Yu0b3d7c12022-02-17 14:30:51 +08007932 ret = MBEDTLS_ERR_SSL_DECODE_ERROR;
7933 goto exit;
7934 }
7935
Gilles Peskine449bd832023-01-11 14:50:10 +01007936 if (mbedtls_ct_memcmp(ssl->in_msg + mbedtls_ssl_hs_hdr_len(ssl),
7937 buf, hash_len) != 0) {
7938 MBEDTLS_SSL_DEBUG_MSG(1, ("bad finished message"));
7939 mbedtls_ssl_send_alert_message(ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
7940 MBEDTLS_SSL_ALERT_MSG_DECRYPT_ERROR);
Jerry Yu0b3d7c12022-02-17 14:30:51 +08007941 ret = MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE;
7942 goto exit;
7943 }
7944
7945#if defined(MBEDTLS_SSL_RENEGOTIATION)
7946 ssl->verify_data_len = hash_len;
Gilles Peskine449bd832023-01-11 14:50:10 +01007947 memcpy(ssl->peer_verify_data, buf, hash_len);
Jerry Yu0b3d7c12022-02-17 14:30:51 +08007948#endif
7949
Gilles Peskine449bd832023-01-11 14:50:10 +01007950 if (ssl->handshake->resume != 0) {
Jerry Yu0b3d7c12022-02-17 14:30:51 +08007951#if defined(MBEDTLS_SSL_CLI_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01007952 if (ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT) {
Jerry Yu0b3d7c12022-02-17 14:30:51 +08007953 ssl->state = MBEDTLS_SSL_CLIENT_CHANGE_CIPHER_SPEC;
Gilles Peskine449bd832023-01-11 14:50:10 +01007954 }
Jerry Yu0b3d7c12022-02-17 14:30:51 +08007955#endif
7956#if defined(MBEDTLS_SSL_SRV_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01007957 if (ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER) {
Jerry Yu0b3d7c12022-02-17 14:30:51 +08007958 ssl->state = MBEDTLS_SSL_HANDSHAKE_WRAPUP;
Gilles Peskine449bd832023-01-11 14:50:10 +01007959 }
Jerry Yu0b3d7c12022-02-17 14:30:51 +08007960#endif
Gilles Peskine449bd832023-01-11 14:50:10 +01007961 } else {
Jerry Yu0b3d7c12022-02-17 14:30:51 +08007962 ssl->state++;
Gilles Peskine449bd832023-01-11 14:50:10 +01007963 }
Jerry Yu0b3d7c12022-02-17 14:30:51 +08007964
7965#if defined(MBEDTLS_SSL_PROTO_DTLS)
Gilles Peskine449bd832023-01-11 14:50:10 +01007966 if (ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM) {
7967 mbedtls_ssl_recv_flight_completed(ssl);
7968 }
Jerry Yu0b3d7c12022-02-17 14:30:51 +08007969#endif
7970
Gilles Peskine449bd832023-01-11 14:50:10 +01007971 MBEDTLS_SSL_DEBUG_MSG(2, ("<= parse finished"));
Jerry Yu0b3d7c12022-02-17 14:30:51 +08007972
7973exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01007974 mbedtls_platform_zeroize(buf, hash_len);
7975 return ret;
Jerry Yu0b3d7c12022-02-17 14:30:51 +08007976}
7977
Jerry Yu392112c2022-02-17 14:34:10 +08007978#if defined(MBEDTLS_SSL_CONTEXT_SERIALIZATION)
7979/*
7980 * Helper to get TLS 1.2 PRF from ciphersuite
7981 * (Duplicates bits of logic from ssl_set_handshake_prfs().)
7982 */
Gilles Peskine449bd832023-01-11 14:50:10 +01007983static tls_prf_fn ssl_tls12prf_from_cs(int ciphersuite_id)
Jerry Yu392112c2022-02-17 14:34:10 +08007984{
Jerry Yu392112c2022-02-17 14:34:10 +08007985 const mbedtls_ssl_ciphersuite_t * const ciphersuite_info =
Gilles Peskine449bd832023-01-11 14:50:10 +01007986 mbedtls_ssl_ciphersuite_from_id(ciphersuite_id);
Andrzej Kurek68327742022-10-03 06:18:18 -04007987#if defined(MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Gilles Peskine449bd832023-01-11 14:50:10 +01007988 if (ciphersuite_info != NULL && ciphersuite_info->mac == MBEDTLS_MD_SHA384) {
7989 return tls_prf_sha384;
7990 } else
Jerry Yu392112c2022-02-17 14:34:10 +08007991#endif
Andrzej Kurek894edde2022-09-29 06:31:14 -04007992#if defined(MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
7993 {
Gilles Peskine449bd832023-01-11 14:50:10 +01007994 if (ciphersuite_info != NULL && ciphersuite_info->mac == MBEDTLS_MD_SHA256) {
7995 return tls_prf_sha256;
7996 }
Andrzej Kurek894edde2022-09-29 06:31:14 -04007997 }
7998#endif
7999#if !defined(MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA) && \
8000 !defined(MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
8001 (void) ciphersuite_info;
8002#endif
Andrzej Kurekeabeb302022-10-17 07:52:51 -04008003
Gilles Peskine449bd832023-01-11 14:50:10 +01008004 return NULL;
Jerry Yu392112c2022-02-17 14:34:10 +08008005}
8006#endif /* MBEDTLS_SSL_CONTEXT_SERIALIZATION */
Jerry Yue93ffcd2022-02-17 14:37:06 +08008007
Gilles Peskine449bd832023-01-11 14:50:10 +01008008static mbedtls_tls_prf_types tls_prf_get_type(mbedtls_ssl_tls_prf_cb *tls_prf)
Jerry Yue93ffcd2022-02-17 14:37:06 +08008009{
8010 ((void) tls_prf);
Andrzej Kurek25f27152022-08-17 16:09:31 -04008011#if defined(MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Gilles Peskine449bd832023-01-11 14:50:10 +01008012 if (tls_prf == tls_prf_sha384) {
8013 return MBEDTLS_SSL_TLS_PRF_SHA384;
8014 } else
Jerry Yue93ffcd2022-02-17 14:37:06 +08008015#endif
Andrzej Kurek25f27152022-08-17 16:09:31 -04008016#if defined(MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Gilles Peskine449bd832023-01-11 14:50:10 +01008017 if (tls_prf == tls_prf_sha256) {
8018 return MBEDTLS_SSL_TLS_PRF_SHA256;
8019 } else
Jerry Yue93ffcd2022-02-17 14:37:06 +08008020#endif
Gilles Peskine449bd832023-01-11 14:50:10 +01008021 return MBEDTLS_SSL_TLS_PRF_NONE;
Jerry Yue93ffcd2022-02-17 14:37:06 +08008022}
8023
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008024/*
8025 * Populate a transform structure with session keys and all the other
8026 * necessary information.
8027 *
8028 * Parameters:
8029 * - [in/out]: transform: structure to populate
8030 * [in] must be just initialised with mbedtls_ssl_transform_init()
8031 * [out] fully populated, ready for use by mbedtls_ssl_{en,de}crypt_buf()
8032 * - [in] ciphersuite
8033 * - [in] master
8034 * - [in] encrypt_then_mac
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008035 * - [in] tls_prf: pointer to PRF to use for key derivation
8036 * - [in] randbytes: buffer holding ServerHello.random + ClientHello.random
Glenn Strauss07c64162022-03-14 12:34:51 -04008037 * - [in] tls_version: TLS version
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008038 * - [in] endpoint: client or server
8039 * - [in] ssl: used for:
8040 * - ssl->conf->{f,p}_export_keys
8041 * [in] optionally used for:
8042 * - MBEDTLS_DEBUG_C: ssl->conf->{f,p}_dbg
8043 */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02008044MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01008045static int ssl_tls12_populate_transform(mbedtls_ssl_transform *transform,
8046 int ciphersuite,
8047 const unsigned char master[48],
Neil Armstrongf2c82f02022-04-05 11:16:53 +02008048#if defined(MBEDTLS_SSL_SOME_SUITES_USE_CBC_ETM)
Gilles Peskine449bd832023-01-11 14:50:10 +01008049 int encrypt_then_mac,
Neil Armstrongf2c82f02022-04-05 11:16:53 +02008050#endif /* MBEDTLS_SSL_SOME_SUITES_USE_CBC_ETM */
Gilles Peskine449bd832023-01-11 14:50:10 +01008051 ssl_tls_prf_t tls_prf,
8052 const unsigned char randbytes[64],
8053 mbedtls_ssl_protocol_version tls_version,
8054 unsigned endpoint,
8055 const mbedtls_ssl_context *ssl)
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008056{
8057 int ret = 0;
8058 unsigned char keyblk[256];
8059 unsigned char *key1;
8060 unsigned char *key2;
8061 unsigned char *mac_enc;
8062 unsigned char *mac_dec;
8063 size_t mac_key_len = 0;
8064 size_t iv_copy_len;
8065 size_t keylen;
8066 const mbedtls_ssl_ciphersuite_t *ciphersuite_info;
Neil Armstrong7fea33e2022-04-01 15:40:25 +02008067 mbedtls_ssl_mode_t ssl_mode;
Neil Armstronge4512952022-03-08 09:08:22 +01008068#if !defined(MBEDTLS_USE_PSA_CRYPTO)
Neil Armstronga0eeb7f2022-04-01 17:36:10 +02008069 const mbedtls_cipher_info_t *cipher_info;
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008070 const mbedtls_md_info_t *md_info;
Neil Armstronge4512952022-03-08 09:08:22 +01008071#endif /* !MBEDTLS_USE_PSA_CRYPTO */
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008072
8073#if defined(MBEDTLS_USE_PSA_CRYPTO)
8074 psa_key_type_t key_type;
8075 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
8076 psa_algorithm_t alg;
Neil Armstronge4512952022-03-08 09:08:22 +01008077 psa_algorithm_t mac_alg = 0;
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008078 size_t key_bits;
8079 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
8080#endif
8081
8082#if !defined(MBEDTLS_DEBUG_C) && \
8083 !defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Gilles Peskine449bd832023-01-11 14:50:10 +01008084 if (ssl->f_export_keys == NULL) {
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008085 ssl = NULL; /* make sure we don't use it except for these cases */
8086 (void) ssl;
8087 }
8088#endif
8089
8090 /*
8091 * Some data just needs copying into the structure
8092 */
Neil Armstrongf2c82f02022-04-05 11:16:53 +02008093#if defined(MBEDTLS_SSL_SOME_SUITES_USE_CBC_ETM)
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008094 transform->encrypt_then_mac = encrypt_then_mac;
Neil Armstrongf2c82f02022-04-05 11:16:53 +02008095#endif /* MBEDTLS_SSL_SOME_SUITES_USE_CBC_ETM */
Glenn Strauss07c64162022-03-14 12:34:51 -04008096 transform->tls_version = tls_version;
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008097
8098#if defined(MBEDTLS_SSL_CONTEXT_SERIALIZATION)
Gilles Peskine449bd832023-01-11 14:50:10 +01008099 memcpy(transform->randbytes, randbytes, sizeof(transform->randbytes));
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008100#endif
8101
8102#if defined(MBEDTLS_SSL_PROTO_TLS1_3)
Gilles Peskine449bd832023-01-11 14:50:10 +01008103 if (tls_version == MBEDTLS_SSL_VERSION_TLS1_3) {
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008104 /* At the moment, we keep TLS <= 1.2 and TLS 1.3 transform
8105 * generation separate. This should never happen. */
Gilles Peskine449bd832023-01-11 14:50:10 +01008106 return MBEDTLS_ERR_SSL_INTERNAL_ERROR;
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008107 }
8108#endif /* MBEDTLS_SSL_PROTO_TLS1_3 */
8109
8110 /*
8111 * Get various info structures
8112 */
Gilles Peskine449bd832023-01-11 14:50:10 +01008113 ciphersuite_info = mbedtls_ssl_ciphersuite_from_id(ciphersuite);
8114 if (ciphersuite_info == NULL) {
8115 MBEDTLS_SSL_DEBUG_MSG(1, ("ciphersuite info for %d not found",
8116 ciphersuite));
8117 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008118 }
8119
Neil Armstrongab555e02022-04-04 11:07:59 +02008120 ssl_mode = mbedtls_ssl_get_mode_from_ciphersuite(
Neil Armstrongf2c82f02022-04-05 11:16:53 +02008121#if defined(MBEDTLS_SSL_SOME_SUITES_USE_CBC_ETM)
Gilles Peskine449bd832023-01-11 14:50:10 +01008122 encrypt_then_mac,
Neil Armstrongf2c82f02022-04-05 11:16:53 +02008123#endif /* MBEDTLS_SSL_SOME_SUITES_USE_CBC_ETM */
Gilles Peskine449bd832023-01-11 14:50:10 +01008124 ciphersuite_info);
Neil Armstrong7fea33e2022-04-01 15:40:25 +02008125
Gilles Peskine449bd832023-01-11 14:50:10 +01008126 if (ssl_mode == MBEDTLS_SSL_MODE_AEAD) {
Neil Armstronga0eeb7f2022-04-01 17:36:10 +02008127 transform->taglen =
8128 ciphersuite_info->flags & MBEDTLS_CIPHERSUITE_SHORT_TAG ? 8 : 16;
Gilles Peskine449bd832023-01-11 14:50:10 +01008129 }
Neil Armstronga0eeb7f2022-04-01 17:36:10 +02008130
8131#if defined(MBEDTLS_USE_PSA_CRYPTO)
Gilles Peskine449bd832023-01-11 14:50:10 +01008132 if ((status = mbedtls_ssl_cipher_to_psa(ciphersuite_info->cipher,
8133 transform->taglen,
8134 &alg,
8135 &key_type,
8136 &key_bits)) != PSA_SUCCESS) {
8137 ret = psa_ssl_status_to_mbedtls(status);
8138 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ssl_cipher_to_psa", ret);
Neil Armstronga0eeb7f2022-04-01 17:36:10 +02008139 goto end;
8140 }
8141#else
Gilles Peskine449bd832023-01-11 14:50:10 +01008142 cipher_info = mbedtls_cipher_info_from_type(ciphersuite_info->cipher);
8143 if (cipher_info == NULL) {
8144 MBEDTLS_SSL_DEBUG_MSG(1, ("cipher info for %u not found",
8145 ciphersuite_info->cipher));
8146 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008147 }
Neil Armstronga0eeb7f2022-04-01 17:36:10 +02008148#endif /* MBEDTLS_USE_PSA_CRYPTO */
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008149
Neil Armstronge4512952022-03-08 09:08:22 +01008150#if defined(MBEDTLS_USE_PSA_CRYPTO)
Gilles Peskine449bd832023-01-11 14:50:10 +01008151 mac_alg = mbedtls_hash_info_psa_from_md(ciphersuite_info->mac);
8152 if (mac_alg == 0) {
8153 MBEDTLS_SSL_DEBUG_MSG(1, ("mbedtls_hash_info_psa_from_md for %u not found",
8154 (unsigned) ciphersuite_info->mac));
8155 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
Neil Armstronge4512952022-03-08 09:08:22 +01008156 }
8157#else
Gilles Peskine449bd832023-01-11 14:50:10 +01008158 md_info = mbedtls_md_info_from_type(ciphersuite_info->mac);
8159 if (md_info == NULL) {
8160 MBEDTLS_SSL_DEBUG_MSG(1, ("mbedtls_md info for %u not found",
8161 (unsigned) ciphersuite_info->mac));
8162 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008163 }
Neil Armstronge4512952022-03-08 09:08:22 +01008164#endif /* MBEDTLS_USE_PSA_CRYPTO */
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008165
8166#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
8167 /* Copy own and peer's CID if the use of the CID
8168 * extension has been negotiated. */
Gilles Peskine449bd832023-01-11 14:50:10 +01008169 if (ssl->handshake->cid_in_use == MBEDTLS_SSL_CID_ENABLED) {
8170 MBEDTLS_SSL_DEBUG_MSG(3, ("Copy CIDs into SSL transform"));
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008171
8172 transform->in_cid_len = ssl->own_cid_len;
Gilles Peskine449bd832023-01-11 14:50:10 +01008173 memcpy(transform->in_cid, ssl->own_cid, ssl->own_cid_len);
8174 MBEDTLS_SSL_DEBUG_BUF(3, "Incoming CID", transform->in_cid,
8175 transform->in_cid_len);
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008176
8177 transform->out_cid_len = ssl->handshake->peer_cid_len;
Gilles Peskine449bd832023-01-11 14:50:10 +01008178 memcpy(transform->out_cid, ssl->handshake->peer_cid,
8179 ssl->handshake->peer_cid_len);
8180 MBEDTLS_SSL_DEBUG_BUF(3, "Outgoing CID", transform->out_cid,
8181 transform->out_cid_len);
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008182 }
8183#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
8184
8185 /*
8186 * Compute key block using the PRF
8187 */
Gilles Peskine449bd832023-01-11 14:50:10 +01008188 ret = tls_prf(master, 48, "key expansion", randbytes, 64, keyblk, 256);
8189 if (ret != 0) {
8190 MBEDTLS_SSL_DEBUG_RET(1, "prf", ret);
8191 return ret;
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008192 }
8193
Gilles Peskine449bd832023-01-11 14:50:10 +01008194 MBEDTLS_SSL_DEBUG_MSG(3, ("ciphersuite = %s",
8195 mbedtls_ssl_get_ciphersuite_name(ciphersuite)));
8196 MBEDTLS_SSL_DEBUG_BUF(3, "master secret", master, 48);
8197 MBEDTLS_SSL_DEBUG_BUF(4, "random bytes", randbytes, 64);
8198 MBEDTLS_SSL_DEBUG_BUF(4, "key block", keyblk, 256);
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008199
8200 /*
8201 * Determine the appropriate key, IV and MAC length.
8202 */
8203
Neil Armstronga0eeb7f2022-04-01 17:36:10 +02008204#if defined(MBEDTLS_USE_PSA_CRYPTO)
8205 keylen = PSA_BITS_TO_BYTES(key_bits);
8206#else
Gilles Peskine449bd832023-01-11 14:50:10 +01008207 keylen = mbedtls_cipher_info_get_key_bitlen(cipher_info) / 8;
Neil Armstronga0eeb7f2022-04-01 17:36:10 +02008208#endif
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008209
8210#if defined(MBEDTLS_GCM_C) || \
8211 defined(MBEDTLS_CCM_C) || \
8212 defined(MBEDTLS_CHACHAPOLY_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01008213 if (ssl_mode == MBEDTLS_SSL_MODE_AEAD) {
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008214 size_t explicit_ivlen;
8215
8216 transform->maclen = 0;
8217 mac_key_len = 0;
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008218
8219 /* All modes haves 96-bit IVs, but the length of the static parts vary
8220 * with mode and version:
8221 * - For GCM and CCM in TLS 1.2, there's a static IV of 4 Bytes
8222 * (to be concatenated with a dynamically chosen IV of 8 Bytes)
8223 * - For ChaChaPoly in TLS 1.2, and all modes in TLS 1.3, there's
8224 * a static IV of 12 Bytes (to be XOR'ed with the 8 Byte record
8225 * sequence number).
8226 */
8227 transform->ivlen = 12;
David Horstmann3b2276a2022-10-06 14:49:08 +01008228
8229 int is_chachapoly = 0;
Neil Armstronga0eeb7f2022-04-01 17:36:10 +02008230#if defined(MBEDTLS_USE_PSA_CRYPTO)
Gilles Peskine449bd832023-01-11 14:50:10 +01008231 is_chachapoly = (key_type == PSA_KEY_TYPE_CHACHA20);
Neil Armstronga0eeb7f2022-04-01 17:36:10 +02008232#else
Gilles Peskine449bd832023-01-11 14:50:10 +01008233 is_chachapoly = (mbedtls_cipher_info_get_mode(cipher_info)
8234 == MBEDTLS_MODE_CHACHAPOLY);
Neil Armstronga0eeb7f2022-04-01 17:36:10 +02008235#endif /* MBEDTLS_USE_PSA_CRYPTO */
David Horstmann3b2276a2022-10-06 14:49:08 +01008236
Gilles Peskine449bd832023-01-11 14:50:10 +01008237 if (is_chachapoly) {
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008238 transform->fixed_ivlen = 12;
Gilles Peskine449bd832023-01-11 14:50:10 +01008239 } else {
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008240 transform->fixed_ivlen = 4;
Gilles Peskine449bd832023-01-11 14:50:10 +01008241 }
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008242
8243 /* Minimum length of encrypted record */
8244 explicit_ivlen = transform->ivlen - transform->fixed_ivlen;
8245 transform->minlen = explicit_ivlen + transform->taglen;
Gilles Peskine449bd832023-01-11 14:50:10 +01008246 } else
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008247#endif /* MBEDTLS_GCM_C || MBEDTLS_CCM_C || MBEDTLS_CHACHAPOLY_C */
8248#if defined(MBEDTLS_SSL_SOME_SUITES_USE_MAC)
Gilles Peskine449bd832023-01-11 14:50:10 +01008249 if (ssl_mode == MBEDTLS_SSL_MODE_STREAM ||
Neil Armstrong7fea33e2022-04-01 15:40:25 +02008250 ssl_mode == MBEDTLS_SSL_MODE_CBC ||
Gilles Peskine449bd832023-01-11 14:50:10 +01008251 ssl_mode == MBEDTLS_SSL_MODE_CBC_ETM) {
Neil Armstronge4512952022-03-08 09:08:22 +01008252#if defined(MBEDTLS_USE_PSA_CRYPTO)
Gilles Peskine449bd832023-01-11 14:50:10 +01008253 size_t block_size = PSA_BLOCK_CIPHER_BLOCK_LENGTH(key_type);
Neil Armstronga0eeb7f2022-04-01 17:36:10 +02008254#else
8255 size_t block_size = cipher_info->block_size;
8256#endif /* MBEDTLS_USE_PSA_CRYPTO */
8257
8258#if defined(MBEDTLS_USE_PSA_CRYPTO)
Neil Armstronge4512952022-03-08 09:08:22 +01008259 /* Get MAC length */
8260 mac_key_len = PSA_HASH_LENGTH(mac_alg);
8261#else
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008262 /* Initialize HMAC contexts */
Gilles Peskine449bd832023-01-11 14:50:10 +01008263 if ((ret = mbedtls_md_setup(&transform->md_ctx_enc, md_info, 1)) != 0 ||
8264 (ret = mbedtls_md_setup(&transform->md_ctx_dec, md_info, 1)) != 0) {
8265 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_md_setup", ret);
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008266 goto end;
8267 }
8268
8269 /* Get MAC length */
Gilles Peskine449bd832023-01-11 14:50:10 +01008270 mac_key_len = mbedtls_md_get_size(md_info);
Neil Armstronge4512952022-03-08 09:08:22 +01008271#endif /* MBEDTLS_USE_PSA_CRYPTO */
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008272 transform->maclen = mac_key_len;
8273
8274 /* IV length */
Neil Armstronga0eeb7f2022-04-01 17:36:10 +02008275#if defined(MBEDTLS_USE_PSA_CRYPTO)
Gilles Peskine449bd832023-01-11 14:50:10 +01008276 transform->ivlen = PSA_CIPHER_IV_LENGTH(key_type, alg);
Neil Armstronga0eeb7f2022-04-01 17:36:10 +02008277#else
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008278 transform->ivlen = cipher_info->iv_size;
Neil Armstronga0eeb7f2022-04-01 17:36:10 +02008279#endif /* MBEDTLS_USE_PSA_CRYPTO */
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008280
8281 /* Minimum length */
Gilles Peskine449bd832023-01-11 14:50:10 +01008282 if (ssl_mode == MBEDTLS_SSL_MODE_STREAM) {
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008283 transform->minlen = transform->maclen;
Gilles Peskine449bd832023-01-11 14:50:10 +01008284 } else {
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008285 /*
8286 * GenericBlockCipher:
8287 * 1. if EtM is in use: one block plus MAC
8288 * otherwise: * first multiple of blocklen greater than maclen
8289 * 2. IV
8290 */
8291#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
Gilles Peskine449bd832023-01-11 14:50:10 +01008292 if (ssl_mode == MBEDTLS_SSL_MODE_CBC_ETM) {
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008293 transform->minlen = transform->maclen
Gilles Peskine449bd832023-01-11 14:50:10 +01008294 + block_size;
8295 } else
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008296#endif
8297 {
8298 transform->minlen = transform->maclen
Gilles Peskine449bd832023-01-11 14:50:10 +01008299 + block_size
8300 - transform->maclen % block_size;
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008301 }
8302
Gilles Peskine449bd832023-01-11 14:50:10 +01008303 if (tls_version == MBEDTLS_SSL_VERSION_TLS1_2) {
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008304 transform->minlen += transform->ivlen;
Gilles Peskine449bd832023-01-11 14:50:10 +01008305 } else {
8306 MBEDTLS_SSL_DEBUG_MSG(1, ("should never happen"));
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008307 ret = MBEDTLS_ERR_SSL_INTERNAL_ERROR;
8308 goto end;
8309 }
8310 }
Gilles Peskine449bd832023-01-11 14:50:10 +01008311 } else
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008312#endif /* MBEDTLS_SSL_SOME_SUITES_USE_MAC */
8313 {
Gilles Peskine449bd832023-01-11 14:50:10 +01008314 MBEDTLS_SSL_DEBUG_MSG(1, ("should never happen"));
8315 return MBEDTLS_ERR_SSL_INTERNAL_ERROR;
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008316 }
8317
Gilles Peskine449bd832023-01-11 14:50:10 +01008318 MBEDTLS_SSL_DEBUG_MSG(3, ("keylen: %u, minlen: %u, ivlen: %u, maclen: %u",
8319 (unsigned) keylen,
8320 (unsigned) transform->minlen,
8321 (unsigned) transform->ivlen,
8322 (unsigned) transform->maclen));
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008323
8324 /*
8325 * Finally setup the cipher contexts, IVs and MAC secrets.
8326 */
8327#if defined(MBEDTLS_SSL_CLI_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01008328 if (endpoint == MBEDTLS_SSL_IS_CLIENT) {
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008329 key1 = keyblk + mac_key_len * 2;
8330 key2 = keyblk + mac_key_len * 2 + keylen;
8331
8332 mac_enc = keyblk;
8333 mac_dec = keyblk + mac_key_len;
8334
Gilles Peskine449bd832023-01-11 14:50:10 +01008335 iv_copy_len = (transform->fixed_ivlen) ?
8336 transform->fixed_ivlen : transform->ivlen;
8337 memcpy(transform->iv_enc, key2 + keylen, iv_copy_len);
8338 memcpy(transform->iv_dec, key2 + keylen + iv_copy_len,
8339 iv_copy_len);
8340 } else
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008341#endif /* MBEDTLS_SSL_CLI_C */
8342#if defined(MBEDTLS_SSL_SRV_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01008343 if (endpoint == MBEDTLS_SSL_IS_SERVER) {
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008344 key1 = keyblk + mac_key_len * 2 + keylen;
8345 key2 = keyblk + mac_key_len * 2;
8346
8347 mac_enc = keyblk + mac_key_len;
8348 mac_dec = keyblk;
8349
Gilles Peskine449bd832023-01-11 14:50:10 +01008350 iv_copy_len = (transform->fixed_ivlen) ?
8351 transform->fixed_ivlen : transform->ivlen;
8352 memcpy(transform->iv_dec, key1 + keylen, iv_copy_len);
8353 memcpy(transform->iv_enc, key1 + keylen + iv_copy_len,
8354 iv_copy_len);
8355 } else
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008356#endif /* MBEDTLS_SSL_SRV_C */
8357 {
Gilles Peskine449bd832023-01-11 14:50:10 +01008358 MBEDTLS_SSL_DEBUG_MSG(1, ("should never happen"));
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008359 ret = MBEDTLS_ERR_SSL_INTERNAL_ERROR;
8360 goto end;
8361 }
8362
Gilles Peskine449bd832023-01-11 14:50:10 +01008363 if (ssl != NULL && ssl->f_export_keys != NULL) {
8364 ssl->f_export_keys(ssl->p_export_keys,
8365 MBEDTLS_SSL_KEY_EXPORT_TLS12_MASTER_SECRET,
8366 master, 48,
8367 randbytes + 32,
8368 randbytes,
8369 tls_prf_get_type(tls_prf));
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008370 }
8371
8372#if defined(MBEDTLS_USE_PSA_CRYPTO)
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008373 transform->psa_alg = alg;
8374
Gilles Peskine449bd832023-01-11 14:50:10 +01008375 if (alg != MBEDTLS_SSL_NULL_CIPHER) {
8376 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_ENCRYPT);
8377 psa_set_key_algorithm(&attributes, alg);
8378 psa_set_key_type(&attributes, key_type);
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008379
Gilles Peskine449bd832023-01-11 14:50:10 +01008380 if ((status = psa_import_key(&attributes,
8381 key1,
8382 PSA_BITS_TO_BYTES(key_bits),
8383 &transform->psa_key_enc)) != PSA_SUCCESS) {
8384 MBEDTLS_SSL_DEBUG_RET(3, "psa_import_key", (int) status);
8385 ret = psa_ssl_status_to_mbedtls(status);
8386 MBEDTLS_SSL_DEBUG_RET(1, "psa_import_key", ret);
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008387 goto end;
8388 }
8389
Gilles Peskine449bd832023-01-11 14:50:10 +01008390 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_DECRYPT);
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008391
Gilles Peskine449bd832023-01-11 14:50:10 +01008392 if ((status = psa_import_key(&attributes,
8393 key2,
8394 PSA_BITS_TO_BYTES(key_bits),
8395 &transform->psa_key_dec)) != PSA_SUCCESS) {
8396 ret = psa_ssl_status_to_mbedtls(status);
8397 MBEDTLS_SSL_DEBUG_RET(1, "psa_import_key", ret);
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008398 goto end;
8399 }
8400 }
8401#else
Gilles Peskine449bd832023-01-11 14:50:10 +01008402 if ((ret = mbedtls_cipher_setup(&transform->cipher_ctx_enc,
8403 cipher_info)) != 0) {
8404 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_cipher_setup", ret);
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008405 goto end;
8406 }
8407
Gilles Peskine449bd832023-01-11 14:50:10 +01008408 if ((ret = mbedtls_cipher_setup(&transform->cipher_ctx_dec,
8409 cipher_info)) != 0) {
8410 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_cipher_setup", ret);
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008411 goto end;
8412 }
8413
Gilles Peskine449bd832023-01-11 14:50:10 +01008414 if ((ret = mbedtls_cipher_setkey(&transform->cipher_ctx_enc, key1,
8415 (int) mbedtls_cipher_info_get_key_bitlen(cipher_info),
8416 MBEDTLS_ENCRYPT)) != 0) {
8417 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_cipher_setkey", ret);
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008418 goto end;
8419 }
8420
Gilles Peskine449bd832023-01-11 14:50:10 +01008421 if ((ret = mbedtls_cipher_setkey(&transform->cipher_ctx_dec, key2,
8422 (int) mbedtls_cipher_info_get_key_bitlen(cipher_info),
8423 MBEDTLS_DECRYPT)) != 0) {
8424 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_cipher_setkey", ret);
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008425 goto end;
8426 }
8427
8428#if defined(MBEDTLS_CIPHER_MODE_CBC)
Gilles Peskine449bd832023-01-11 14:50:10 +01008429 if (mbedtls_cipher_info_get_mode(cipher_info) == MBEDTLS_MODE_CBC) {
8430 if ((ret = mbedtls_cipher_set_padding_mode(&transform->cipher_ctx_enc,
8431 MBEDTLS_PADDING_NONE)) != 0) {
8432 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_cipher_set_padding_mode", ret);
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008433 goto end;
8434 }
8435
Gilles Peskine449bd832023-01-11 14:50:10 +01008436 if ((ret = mbedtls_cipher_set_padding_mode(&transform->cipher_ctx_dec,
8437 MBEDTLS_PADDING_NONE)) != 0) {
8438 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_cipher_set_padding_mode", ret);
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008439 goto end;
8440 }
8441 }
8442#endif /* MBEDTLS_CIPHER_MODE_CBC */
8443#endif /* MBEDTLS_USE_PSA_CRYPTO */
8444
Neil Armstrong29c0c042022-03-17 17:47:28 +01008445#if defined(MBEDTLS_SSL_SOME_SUITES_USE_MAC)
8446 /* For HMAC-based ciphersuites, initialize the HMAC transforms.
8447 For AEAD-based ciphersuites, there is nothing to do here. */
Gilles Peskine449bd832023-01-11 14:50:10 +01008448 if (mac_key_len != 0) {
Neil Armstrong29c0c042022-03-17 17:47:28 +01008449#if defined(MBEDTLS_USE_PSA_CRYPTO)
Gilles Peskine449bd832023-01-11 14:50:10 +01008450 transform->psa_mac_alg = PSA_ALG_HMAC(mac_alg);
Neil Armstrong29c0c042022-03-17 17:47:28 +01008451
Gilles Peskine449bd832023-01-11 14:50:10 +01008452 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_SIGN_MESSAGE);
8453 psa_set_key_algorithm(&attributes, PSA_ALG_HMAC(mac_alg));
8454 psa_set_key_type(&attributes, PSA_KEY_TYPE_HMAC);
Neil Armstrong29c0c042022-03-17 17:47:28 +01008455
Gilles Peskine449bd832023-01-11 14:50:10 +01008456 if ((status = psa_import_key(&attributes,
8457 mac_enc, mac_key_len,
8458 &transform->psa_mac_enc)) != PSA_SUCCESS) {
8459 ret = psa_ssl_status_to_mbedtls(status);
8460 MBEDTLS_SSL_DEBUG_RET(1, "psa_import_mac_key", ret);
Neil Armstrong29c0c042022-03-17 17:47:28 +01008461 goto end;
8462 }
8463
Gilles Peskine449bd832023-01-11 14:50:10 +01008464 if ((transform->psa_alg == MBEDTLS_SSL_NULL_CIPHER) ||
8465 ((transform->psa_alg == PSA_ALG_CBC_NO_PADDING)
Andrzej Kurek8c95ac42022-08-17 16:17:00 -04008466#if defined(MBEDTLS_SSL_SOME_SUITES_USE_CBC_ETM)
Gilles Peskine449bd832023-01-11 14:50:10 +01008467 && (transform->encrypt_then_mac == MBEDTLS_SSL_ETM_DISABLED)
Andrzej Kurek8c95ac42022-08-17 16:17:00 -04008468#endif
Gilles Peskine449bd832023-01-11 14:50:10 +01008469 )) {
Neil Armstrong29c0c042022-03-17 17:47:28 +01008470 /* mbedtls_ct_hmac() requires the key to be exportable */
Gilles Peskine449bd832023-01-11 14:50:10 +01008471 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_EXPORT |
8472 PSA_KEY_USAGE_VERIFY_HASH);
8473 } else {
8474 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_VERIFY_HASH);
8475 }
Neil Armstrong29c0c042022-03-17 17:47:28 +01008476
Gilles Peskine449bd832023-01-11 14:50:10 +01008477 if ((status = psa_import_key(&attributes,
8478 mac_dec, mac_key_len,
8479 &transform->psa_mac_dec)) != PSA_SUCCESS) {
8480 ret = psa_ssl_status_to_mbedtls(status);
8481 MBEDTLS_SSL_DEBUG_RET(1, "psa_import_mac_key", ret);
Neil Armstrong29c0c042022-03-17 17:47:28 +01008482 goto end;
8483 }
8484#else
Gilles Peskine449bd832023-01-11 14:50:10 +01008485 ret = mbedtls_md_hmac_starts(&transform->md_ctx_enc, mac_enc, mac_key_len);
8486 if (ret != 0) {
Neil Armstrong29c0c042022-03-17 17:47:28 +01008487 goto end;
Gilles Peskine449bd832023-01-11 14:50:10 +01008488 }
8489 ret = mbedtls_md_hmac_starts(&transform->md_ctx_dec, mac_dec, mac_key_len);
8490 if (ret != 0) {
Neil Armstrong29c0c042022-03-17 17:47:28 +01008491 goto end;
Gilles Peskine449bd832023-01-11 14:50:10 +01008492 }
Neil Armstrong29c0c042022-03-17 17:47:28 +01008493#endif /* MBEDTLS_USE_PSA_CRYPTO */
8494 }
8495#endif /* MBEDTLS_SSL_SOME_SUITES_USE_MAC */
8496
8497 ((void) mac_dec);
8498 ((void) mac_enc);
8499
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008500end:
Gilles Peskine449bd832023-01-11 14:50:10 +01008501 mbedtls_platform_zeroize(keyblk, sizeof(keyblk));
8502 return ret;
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008503}
8504
Valerio Settia08b1a42022-11-17 15:10:02 +01008505#if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED) && \
8506 defined(MBEDTLS_USE_PSA_CRYPTO)
Valerio Setti6b3dab02022-11-17 17:14:54 +01008507int mbedtls_psa_ecjpake_read_round(
Gilles Peskine449bd832023-01-11 14:50:10 +01008508 psa_pake_operation_t *pake_ctx,
8509 const unsigned char *buf,
8510 size_t len, mbedtls_ecjpake_rounds_t round)
Valerio Settia08b1a42022-11-17 15:10:02 +01008511{
8512 psa_status_t status;
8513 size_t input_offset = 0;
Valerio Setti819de862022-11-17 18:05:19 +01008514 /*
Valerio Setti6b3dab02022-11-17 17:14:54 +01008515 * At round one repeat the KEY_SHARE, ZK_PUBLIC & ZF_PROOF twice
8516 * At round two perform a single cycle
8517 */
Gilles Peskine449bd832023-01-11 14:50:10 +01008518 unsigned int remaining_steps = (round == MBEDTLS_ECJPAKE_ROUND_ONE) ? 2 : 1;
Valerio Settia08b1a42022-11-17 15:10:02 +01008519
Gilles Peskine449bd832023-01-11 14:50:10 +01008520 for (; remaining_steps > 0; remaining_steps--) {
8521 for (psa_pake_step_t step = PSA_PAKE_STEP_KEY_SHARE;
Valerio Settia08b1a42022-11-17 15:10:02 +01008522 step <= PSA_PAKE_STEP_ZK_PROOF;
Gilles Peskine449bd832023-01-11 14:50:10 +01008523 ++step) {
Valerio Settia08b1a42022-11-17 15:10:02 +01008524 /* Length is stored at the first byte */
8525 size_t length = buf[input_offset];
8526 input_offset += 1;
8527
Gilles Peskine449bd832023-01-11 14:50:10 +01008528 if (input_offset + length > len) {
Valerio Settia08b1a42022-11-17 15:10:02 +01008529 return MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE;
8530 }
8531
Gilles Peskine449bd832023-01-11 14:50:10 +01008532 status = psa_pake_input(pake_ctx, step,
8533 buf + input_offset, length);
8534 if (status != PSA_SUCCESS) {
8535 return psa_ssl_status_to_mbedtls(status);
Valerio Settia08b1a42022-11-17 15:10:02 +01008536 }
8537
8538 input_offset += length;
8539 }
8540 }
8541
Gilles Peskine449bd832023-01-11 14:50:10 +01008542 if (input_offset != len) {
Valerio Setti61ea17d2022-11-18 12:11:00 +01008543 return MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE;
Gilles Peskine449bd832023-01-11 14:50:10 +01008544 }
Valerio Setti30ebe112022-11-17 16:23:34 +01008545
Gilles Peskine449bd832023-01-11 14:50:10 +01008546 return 0;
Valerio Settia08b1a42022-11-17 15:10:02 +01008547}
8548
Valerio Setti6b3dab02022-11-17 17:14:54 +01008549int mbedtls_psa_ecjpake_write_round(
Gilles Peskine449bd832023-01-11 14:50:10 +01008550 psa_pake_operation_t *pake_ctx,
8551 unsigned char *buf,
8552 size_t len, size_t *olen,
8553 mbedtls_ecjpake_rounds_t round)
Valerio Settia08b1a42022-11-17 15:10:02 +01008554{
8555 psa_status_t status;
8556 size_t output_offset = 0;
8557 size_t output_len;
Valerio Setti819de862022-11-17 18:05:19 +01008558 /*
Valerio Setti6b3dab02022-11-17 17:14:54 +01008559 * At round one repeat the KEY_SHARE, ZK_PUBLIC & ZF_PROOF twice
8560 * At round two perform a single cycle
8561 */
Gilles Peskine449bd832023-01-11 14:50:10 +01008562 unsigned int remaining_steps = (round == MBEDTLS_ECJPAKE_ROUND_ONE) ? 2 : 1;
Valerio Settia08b1a42022-11-17 15:10:02 +01008563
Gilles Peskine449bd832023-01-11 14:50:10 +01008564 for (; remaining_steps > 0; remaining_steps--) {
8565 for (psa_pake_step_t step = PSA_PAKE_STEP_KEY_SHARE;
8566 step <= PSA_PAKE_STEP_ZK_PROOF;
8567 ++step) {
Valerio Setti79f6b6b2022-11-21 14:17:03 +01008568 /*
Valerio Settid4a9b1a2022-11-22 11:11:10 +01008569 * For each step, prepend 1 byte with the length of the data as
8570 * given by psa_pake_output().
Valerio Setti79f6b6b2022-11-21 14:17:03 +01008571 */
Gilles Peskine449bd832023-01-11 14:50:10 +01008572 status = psa_pake_output(pake_ctx, step,
8573 buf + output_offset + 1,
8574 len - output_offset - 1,
8575 &output_len);
8576 if (status != PSA_SUCCESS) {
8577 return psa_ssl_status_to_mbedtls(status);
Valerio Settia08b1a42022-11-17 15:10:02 +01008578 }
8579
Valerio Setti99d88c12022-11-22 16:03:43 +01008580 *(buf + output_offset) = (uint8_t) output_len;
Valerio Setti79f6b6b2022-11-21 14:17:03 +01008581
8582 output_offset += output_len + 1;
Valerio Settia08b1a42022-11-17 15:10:02 +01008583 }
8584 }
8585
8586 *olen = output_offset;
8587
Gilles Peskine449bd832023-01-11 14:50:10 +01008588 return 0;
Valerio Settia08b1a42022-11-17 15:10:02 +01008589}
Valerio Settia08b1a42022-11-17 15:10:02 +01008590#endif //MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED && MBEDTLS_USE_PSA_CRYPTO
8591
Jerry Yuee40f9d2022-02-17 14:55:16 +08008592#if defined(MBEDTLS_USE_PSA_CRYPTO)
Gilles Peskine449bd832023-01-11 14:50:10 +01008593int mbedtls_ssl_get_key_exchange_md_tls1_2(mbedtls_ssl_context *ssl,
8594 unsigned char *hash, size_t *hashlen,
8595 unsigned char *data, size_t data_len,
8596 mbedtls_md_type_t md_alg)
Jerry Yuee40f9d2022-02-17 14:55:16 +08008597{
8598 psa_status_t status;
8599 psa_hash_operation_t hash_operation = PSA_HASH_OPERATION_INIT;
Gilles Peskine449bd832023-01-11 14:50:10 +01008600 psa_algorithm_t hash_alg = mbedtls_hash_info_psa_from_md(md_alg);
Jerry Yuee40f9d2022-02-17 14:55:16 +08008601
Gilles Peskine449bd832023-01-11 14:50:10 +01008602 MBEDTLS_SSL_DEBUG_MSG(3, ("Perform PSA-based computation of digest of ServerKeyExchange"));
Jerry Yuee40f9d2022-02-17 14:55:16 +08008603
Gilles Peskine449bd832023-01-11 14:50:10 +01008604 if ((status = psa_hash_setup(&hash_operation,
8605 hash_alg)) != PSA_SUCCESS) {
8606 MBEDTLS_SSL_DEBUG_RET(1, "psa_hash_setup", status);
Jerry Yuee40f9d2022-02-17 14:55:16 +08008607 goto exit;
8608 }
8609
Gilles Peskine449bd832023-01-11 14:50:10 +01008610 if ((status = psa_hash_update(&hash_operation, ssl->handshake->randbytes,
8611 64)) != PSA_SUCCESS) {
8612 MBEDTLS_SSL_DEBUG_RET(1, "psa_hash_update", status);
Jerry Yuee40f9d2022-02-17 14:55:16 +08008613 goto exit;
8614 }
8615
Gilles Peskine449bd832023-01-11 14:50:10 +01008616 if ((status = psa_hash_update(&hash_operation,
8617 data, data_len)) != PSA_SUCCESS) {
8618 MBEDTLS_SSL_DEBUG_RET(1, "psa_hash_update", status);
Jerry Yuee40f9d2022-02-17 14:55:16 +08008619 goto exit;
8620 }
8621
Gilles Peskine449bd832023-01-11 14:50:10 +01008622 if ((status = psa_hash_finish(&hash_operation, hash, PSA_HASH_MAX_SIZE,
8623 hashlen)) != PSA_SUCCESS) {
8624 MBEDTLS_SSL_DEBUG_RET(1, "psa_hash_finish", status);
8625 goto exit;
Jerry Yuee40f9d2022-02-17 14:55:16 +08008626 }
8627
8628exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01008629 if (status != PSA_SUCCESS) {
8630 mbedtls_ssl_send_alert_message(ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
8631 MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR);
8632 switch (status) {
Jerry Yuee40f9d2022-02-17 14:55:16 +08008633 case PSA_ERROR_NOT_SUPPORTED:
Gilles Peskine449bd832023-01-11 14:50:10 +01008634 return MBEDTLS_ERR_MD_FEATURE_UNAVAILABLE;
Jerry Yuee40f9d2022-02-17 14:55:16 +08008635 case PSA_ERROR_BAD_STATE: /* Intentional fallthrough */
8636 case PSA_ERROR_BUFFER_TOO_SMALL:
Gilles Peskine449bd832023-01-11 14:50:10 +01008637 return MBEDTLS_ERR_MD_BAD_INPUT_DATA;
Jerry Yuee40f9d2022-02-17 14:55:16 +08008638 case PSA_ERROR_INSUFFICIENT_MEMORY:
Gilles Peskine449bd832023-01-11 14:50:10 +01008639 return MBEDTLS_ERR_MD_ALLOC_FAILED;
Jerry Yuee40f9d2022-02-17 14:55:16 +08008640 default:
Gilles Peskine449bd832023-01-11 14:50:10 +01008641 return MBEDTLS_ERR_PLATFORM_HW_ACCEL_FAILED;
Jerry Yuee40f9d2022-02-17 14:55:16 +08008642 }
8643 }
Gilles Peskine449bd832023-01-11 14:50:10 +01008644 return 0;
Jerry Yuee40f9d2022-02-17 14:55:16 +08008645}
8646
8647#else
8648
Gilles Peskine449bd832023-01-11 14:50:10 +01008649int mbedtls_ssl_get_key_exchange_md_tls1_2(mbedtls_ssl_context *ssl,
8650 unsigned char *hash, size_t *hashlen,
8651 unsigned char *data, size_t data_len,
8652 mbedtls_md_type_t md_alg)
Jerry Yuee40f9d2022-02-17 14:55:16 +08008653{
8654 int ret = 0;
8655 mbedtls_md_context_t ctx;
Gilles Peskine449bd832023-01-11 14:50:10 +01008656 const mbedtls_md_info_t *md_info = mbedtls_md_info_from_type(md_alg);
8657 *hashlen = mbedtls_md_get_size(md_info);
Jerry Yuee40f9d2022-02-17 14:55:16 +08008658
Gilles Peskine449bd832023-01-11 14:50:10 +01008659 MBEDTLS_SSL_DEBUG_MSG(3, ("Perform mbedtls-based computation of digest of ServerKeyExchange"));
Jerry Yuee40f9d2022-02-17 14:55:16 +08008660
Gilles Peskine449bd832023-01-11 14:50:10 +01008661 mbedtls_md_init(&ctx);
Jerry Yuee40f9d2022-02-17 14:55:16 +08008662
8663 /*
8664 * digitally-signed struct {
8665 * opaque client_random[32];
8666 * opaque server_random[32];
8667 * ServerDHParams params;
8668 * };
8669 */
Gilles Peskine449bd832023-01-11 14:50:10 +01008670 if ((ret = mbedtls_md_setup(&ctx, md_info, 0)) != 0) {
8671 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_md_setup", ret);
Jerry Yuee40f9d2022-02-17 14:55:16 +08008672 goto exit;
8673 }
Gilles Peskine449bd832023-01-11 14:50:10 +01008674 if ((ret = mbedtls_md_starts(&ctx)) != 0) {
8675 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_md_starts", ret);
Jerry Yuee40f9d2022-02-17 14:55:16 +08008676 goto exit;
8677 }
Gilles Peskine449bd832023-01-11 14:50:10 +01008678 if ((ret = mbedtls_md_update(&ctx, ssl->handshake->randbytes, 64)) != 0) {
8679 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_md_update", ret);
Jerry Yuee40f9d2022-02-17 14:55:16 +08008680 goto exit;
8681 }
Gilles Peskine449bd832023-01-11 14:50:10 +01008682 if ((ret = mbedtls_md_update(&ctx, data, data_len)) != 0) {
8683 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_md_update", ret);
Jerry Yuee40f9d2022-02-17 14:55:16 +08008684 goto exit;
8685 }
Gilles Peskine449bd832023-01-11 14:50:10 +01008686 if ((ret = mbedtls_md_finish(&ctx, hash)) != 0) {
8687 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_md_finish", ret);
Jerry Yuee40f9d2022-02-17 14:55:16 +08008688 goto exit;
8689 }
8690
8691exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01008692 mbedtls_md_free(&ctx);
Jerry Yuee40f9d2022-02-17 14:55:16 +08008693
Gilles Peskine449bd832023-01-11 14:50:10 +01008694 if (ret != 0) {
8695 mbedtls_ssl_send_alert_message(ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
8696 MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR);
8697 }
Jerry Yuee40f9d2022-02-17 14:55:16 +08008698
Gilles Peskine449bd832023-01-11 14:50:10 +01008699 return ret;
Jerry Yuee40f9d2022-02-17 14:55:16 +08008700}
8701#endif /* MBEDTLS_USE_PSA_CRYPTO */
8702
Jerry Yud9d91da2022-02-17 14:57:06 +08008703#if defined(MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED)
8704
Gabor Mezeia3d016c2022-05-10 12:44:09 +02008705/* Find the preferred hash for a given signature algorithm. */
8706unsigned int mbedtls_ssl_tls12_get_preferred_hash_for_sig_alg(
Gilles Peskine449bd832023-01-11 14:50:10 +01008707 mbedtls_ssl_context *ssl,
8708 unsigned int sig_alg)
Jerry Yud9d91da2022-02-17 14:57:06 +08008709{
Gabor Mezei078e8032022-04-27 21:17:56 +02008710 unsigned int i;
Gabor Mezeia3d016c2022-05-10 12:44:09 +02008711 uint16_t *received_sig_algs = ssl->handshake->received_sig_algs;
Gabor Mezei078e8032022-04-27 21:17:56 +02008712
Gilles Peskine449bd832023-01-11 14:50:10 +01008713 if (sig_alg == MBEDTLS_SSL_SIG_ANON) {
8714 return MBEDTLS_SSL_HASH_NONE;
8715 }
Gabor Mezei078e8032022-04-27 21:17:56 +02008716
Gilles Peskine449bd832023-01-11 14:50:10 +01008717 for (i = 0; received_sig_algs[i] != MBEDTLS_TLS_SIG_NONE; i++) {
Neil Armstrong9f1176a2022-06-24 18:19:19 +02008718 unsigned int hash_alg_received =
Gilles Peskine449bd832023-01-11 14:50:10 +01008719 MBEDTLS_SSL_TLS12_HASH_ALG_FROM_SIG_AND_HASH_ALG(
8720 received_sig_algs[i]);
Neil Armstrong9f1176a2022-06-24 18:19:19 +02008721 unsigned int sig_alg_received =
Gilles Peskine449bd832023-01-11 14:50:10 +01008722 MBEDTLS_SSL_TLS12_SIG_ALG_FROM_SIG_AND_HASH_ALG(
8723 received_sig_algs[i]);
Neil Armstrong9f1176a2022-06-24 18:19:19 +02008724
Gilles Peskine449bd832023-01-11 14:50:10 +01008725 if (sig_alg == sig_alg_received) {
Neil Armstrong9f1176a2022-06-24 18:19:19 +02008726#if defined(MBEDTLS_USE_PSA_CRYPTO)
Gilles Peskine449bd832023-01-11 14:50:10 +01008727 if (ssl->handshake->key_cert && ssl->handshake->key_cert->key) {
Neil Armstrong96eceb82022-06-30 18:05:05 +02008728 psa_algorithm_t psa_hash_alg =
Gilles Peskine449bd832023-01-11 14:50:10 +01008729 mbedtls_hash_info_psa_from_md(hash_alg_received);
Neil Armstrong9f1176a2022-06-24 18:19:19 +02008730
Gilles Peskine449bd832023-01-11 14:50:10 +01008731 if (sig_alg_received == MBEDTLS_SSL_SIG_ECDSA &&
8732 !mbedtls_pk_can_do_ext(ssl->handshake->key_cert->key,
8733 PSA_ALG_ECDSA(psa_hash_alg),
8734 PSA_KEY_USAGE_SIGN_HASH)) {
Neil Armstrong9f1176a2022-06-24 18:19:19 +02008735 continue;
Gilles Peskine449bd832023-01-11 14:50:10 +01008736 }
Neil Armstrong9f1176a2022-06-24 18:19:19 +02008737
Gilles Peskine449bd832023-01-11 14:50:10 +01008738 if (sig_alg_received == MBEDTLS_SSL_SIG_RSA &&
8739 !mbedtls_pk_can_do_ext(ssl->handshake->key_cert->key,
8740 PSA_ALG_RSA_PKCS1V15_SIGN(
8741 psa_hash_alg),
8742 PSA_KEY_USAGE_SIGN_HASH)) {
Neil Armstrong9f1176a2022-06-24 18:19:19 +02008743 continue;
Gilles Peskine449bd832023-01-11 14:50:10 +01008744 }
Neil Armstrong9f1176a2022-06-24 18:19:19 +02008745 }
Neil Armstrong9f1176a2022-06-24 18:19:19 +02008746#endif /* MBEDTLS_USE_PSA_CRYPTO */
Neil Armstrong96eceb82022-06-30 18:05:05 +02008747
Gilles Peskine449bd832023-01-11 14:50:10 +01008748 return hash_alg_received;
Neil Armstrong9f1176a2022-06-24 18:19:19 +02008749 }
Jerry Yud9d91da2022-02-17 14:57:06 +08008750 }
Jerry Yud9d91da2022-02-17 14:57:06 +08008751
Gilles Peskine449bd832023-01-11 14:50:10 +01008752 return MBEDTLS_SSL_HASH_NONE;
Jerry Yud9d91da2022-02-17 14:57:06 +08008753}
8754
8755#endif /* MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED */
8756
Jerry Yu4f9e3ef2022-02-17 14:58:27 +08008757/* Serialization of TLS 1.2 sessions:
8758 *
8759 * struct {
8760 * uint64 start_time;
8761 * uint8 ciphersuite[2]; // defined by the standard
Jerry Yu4f9e3ef2022-02-17 14:58:27 +08008762 * uint8 session_id_len; // at most 32
8763 * opaque session_id[32];
8764 * opaque master[48]; // fixed length in the standard
8765 * uint32 verify_result;
8766 * opaque peer_cert<0..2^24-1>; // length 0 means no peer cert
8767 * opaque ticket<0..2^24-1>; // length 0 means no ticket
8768 * uint32 ticket_lifetime;
8769 * uint8 mfl_code; // up to 255 according to standard
8770 * uint8 encrypt_then_mac; // 0 or 1
8771 * } serialized_session_tls12;
8772 *
8773 */
Gilles Peskine449bd832023-01-11 14:50:10 +01008774static size_t ssl_tls12_session_save(const mbedtls_ssl_session *session,
8775 unsigned char *buf,
8776 size_t buf_len)
Jerry Yu4f9e3ef2022-02-17 14:58:27 +08008777{
8778 unsigned char *p = buf;
8779 size_t used = 0;
8780
8781#if defined(MBEDTLS_HAVE_TIME)
8782 uint64_t start;
8783#endif
8784#if defined(MBEDTLS_X509_CRT_PARSE_C)
8785#if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
8786 size_t cert_len;
8787#endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
8788#endif /* MBEDTLS_X509_CRT_PARSE_C */
8789
8790 /*
8791 * Time
8792 */
8793#if defined(MBEDTLS_HAVE_TIME)
8794 used += 8;
8795
Gilles Peskine449bd832023-01-11 14:50:10 +01008796 if (used <= buf_len) {
Jerry Yu4f9e3ef2022-02-17 14:58:27 +08008797 start = (uint64_t) session->start;
8798
Gilles Peskine449bd832023-01-11 14:50:10 +01008799 MBEDTLS_PUT_UINT64_BE(start, p, 0);
Jerry Yu4f9e3ef2022-02-17 14:58:27 +08008800 p += 8;
8801 }
8802#endif /* MBEDTLS_HAVE_TIME */
8803
8804 /*
8805 * Basic mandatory fields
8806 */
8807 used += 2 /* ciphersuite */
Gilles Peskine449bd832023-01-11 14:50:10 +01008808 + 1 /* id_len */
8809 + sizeof(session->id)
8810 + sizeof(session->master)
8811 + 4; /* verify_result */
Jerry Yu4f9e3ef2022-02-17 14:58:27 +08008812
Gilles Peskine449bd832023-01-11 14:50:10 +01008813 if (used <= buf_len) {
8814 MBEDTLS_PUT_UINT16_BE(session->ciphersuite, p, 0);
Jerry Yu4f9e3ef2022-02-17 14:58:27 +08008815 p += 2;
8816
Gilles Peskine449bd832023-01-11 14:50:10 +01008817 *p++ = MBEDTLS_BYTE_0(session->id_len);
8818 memcpy(p, session->id, 32);
Jerry Yu4f9e3ef2022-02-17 14:58:27 +08008819 p += 32;
8820
Gilles Peskine449bd832023-01-11 14:50:10 +01008821 memcpy(p, session->master, 48);
Jerry Yu4f9e3ef2022-02-17 14:58:27 +08008822 p += 48;
8823
Gilles Peskine449bd832023-01-11 14:50:10 +01008824 MBEDTLS_PUT_UINT32_BE(session->verify_result, p, 0);
Jerry Yu4f9e3ef2022-02-17 14:58:27 +08008825 p += 4;
8826 }
8827
8828 /*
8829 * Peer's end-entity certificate
8830 */
8831#if defined(MBEDTLS_X509_CRT_PARSE_C)
8832#if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
Gilles Peskine449bd832023-01-11 14:50:10 +01008833 if (session->peer_cert == NULL) {
Jerry Yu4f9e3ef2022-02-17 14:58:27 +08008834 cert_len = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +01008835 } else {
Jerry Yu4f9e3ef2022-02-17 14:58:27 +08008836 cert_len = session->peer_cert->raw.len;
Gilles Peskine449bd832023-01-11 14:50:10 +01008837 }
Jerry Yu4f9e3ef2022-02-17 14:58:27 +08008838
8839 used += 3 + cert_len;
8840
Gilles Peskine449bd832023-01-11 14:50:10 +01008841 if (used <= buf_len) {
8842 *p++ = MBEDTLS_BYTE_2(cert_len);
8843 *p++ = MBEDTLS_BYTE_1(cert_len);
8844 *p++ = MBEDTLS_BYTE_0(cert_len);
Jerry Yu4f9e3ef2022-02-17 14:58:27 +08008845
Gilles Peskine449bd832023-01-11 14:50:10 +01008846 if (session->peer_cert != NULL) {
8847 memcpy(p, session->peer_cert->raw.p, cert_len);
Jerry Yu4f9e3ef2022-02-17 14:58:27 +08008848 p += cert_len;
8849 }
8850 }
8851#else /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
Gilles Peskine449bd832023-01-11 14:50:10 +01008852 if (session->peer_cert_digest != NULL) {
Jerry Yu4f9e3ef2022-02-17 14:58:27 +08008853 used += 1 /* type */ + 1 /* length */ + session->peer_cert_digest_len;
Gilles Peskine449bd832023-01-11 14:50:10 +01008854 if (used <= buf_len) {
Jerry Yu4f9e3ef2022-02-17 14:58:27 +08008855 *p++ = (unsigned char) session->peer_cert_digest_type;
8856 *p++ = (unsigned char) session->peer_cert_digest_len;
Gilles Peskine449bd832023-01-11 14:50:10 +01008857 memcpy(p, session->peer_cert_digest,
8858 session->peer_cert_digest_len);
Jerry Yu4f9e3ef2022-02-17 14:58:27 +08008859 p += session->peer_cert_digest_len;
8860 }
Gilles Peskine449bd832023-01-11 14:50:10 +01008861 } else {
Jerry Yu4f9e3ef2022-02-17 14:58:27 +08008862 used += 2;
Gilles Peskine449bd832023-01-11 14:50:10 +01008863 if (used <= buf_len) {
Jerry Yu4f9e3ef2022-02-17 14:58:27 +08008864 *p++ = (unsigned char) MBEDTLS_MD_NONE;
8865 *p++ = 0;
8866 }
8867 }
8868#endif /* !MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
8869#endif /* MBEDTLS_X509_CRT_PARSE_C */
8870
8871 /*
8872 * Session ticket if any, plus associated data
8873 */
8874#if defined(MBEDTLS_SSL_SESSION_TICKETS) && defined(MBEDTLS_SSL_CLI_C)
8875 used += 3 + session->ticket_len + 4; /* len + ticket + lifetime */
8876
Gilles Peskine449bd832023-01-11 14:50:10 +01008877 if (used <= buf_len) {
8878 *p++ = MBEDTLS_BYTE_2(session->ticket_len);
8879 *p++ = MBEDTLS_BYTE_1(session->ticket_len);
8880 *p++ = MBEDTLS_BYTE_0(session->ticket_len);
Jerry Yu4f9e3ef2022-02-17 14:58:27 +08008881
Gilles Peskine449bd832023-01-11 14:50:10 +01008882 if (session->ticket != NULL) {
8883 memcpy(p, session->ticket, session->ticket_len);
Jerry Yu4f9e3ef2022-02-17 14:58:27 +08008884 p += session->ticket_len;
8885 }
8886
Gilles Peskine449bd832023-01-11 14:50:10 +01008887 MBEDTLS_PUT_UINT32_BE(session->ticket_lifetime, p, 0);
Jerry Yu4f9e3ef2022-02-17 14:58:27 +08008888 p += 4;
8889 }
8890#endif /* MBEDTLS_SSL_SESSION_TICKETS && MBEDTLS_SSL_CLI_C */
8891
8892 /*
8893 * Misc extension-related info
8894 */
8895#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
8896 used += 1;
8897
Gilles Peskine449bd832023-01-11 14:50:10 +01008898 if (used <= buf_len) {
Jerry Yu4f9e3ef2022-02-17 14:58:27 +08008899 *p++ = session->mfl_code;
Gilles Peskine449bd832023-01-11 14:50:10 +01008900 }
Jerry Yu4f9e3ef2022-02-17 14:58:27 +08008901#endif
8902
8903#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
8904 used += 1;
8905
Gilles Peskine449bd832023-01-11 14:50:10 +01008906 if (used <= buf_len) {
8907 *p++ = MBEDTLS_BYTE_0(session->encrypt_then_mac);
8908 }
Jerry Yu4f9e3ef2022-02-17 14:58:27 +08008909#endif
8910
Gilles Peskine449bd832023-01-11 14:50:10 +01008911 return used;
Jerry Yu4f9e3ef2022-02-17 14:58:27 +08008912}
8913
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02008914MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01008915static int ssl_tls12_session_load(mbedtls_ssl_session *session,
8916 const unsigned char *buf,
8917 size_t len)
Jerry Yu4f9e3ef2022-02-17 14:58:27 +08008918{
8919#if defined(MBEDTLS_HAVE_TIME)
8920 uint64_t start;
8921#endif
8922#if defined(MBEDTLS_X509_CRT_PARSE_C)
8923#if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
8924 size_t cert_len;
8925#endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
8926#endif /* MBEDTLS_X509_CRT_PARSE_C */
8927
8928 const unsigned char *p = buf;
8929 const unsigned char * const end = buf + len;
8930
8931 /*
8932 * Time
8933 */
8934#if defined(MBEDTLS_HAVE_TIME)
Gilles Peskine449bd832023-01-11 14:50:10 +01008935 if (8 > (size_t) (end - p)) {
8936 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
8937 }
Jerry Yu4f9e3ef2022-02-17 14:58:27 +08008938
Gilles Peskine449bd832023-01-11 14:50:10 +01008939 start = ((uint64_t) p[0] << 56) |
8940 ((uint64_t) p[1] << 48) |
8941 ((uint64_t) p[2] << 40) |
8942 ((uint64_t) p[3] << 32) |
8943 ((uint64_t) p[4] << 24) |
8944 ((uint64_t) p[5] << 16) |
8945 ((uint64_t) p[6] << 8) |
8946 ((uint64_t) p[7]);
Jerry Yu4f9e3ef2022-02-17 14:58:27 +08008947 p += 8;
8948
8949 session->start = (time_t) start;
8950#endif /* MBEDTLS_HAVE_TIME */
8951
8952 /*
8953 * Basic mandatory fields
8954 */
Gilles Peskine449bd832023-01-11 14:50:10 +01008955 if (2 + 1 + 32 + 48 + 4 > (size_t) (end - p)) {
8956 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
8957 }
Jerry Yu4f9e3ef2022-02-17 14:58:27 +08008958
Gilles Peskine449bd832023-01-11 14:50:10 +01008959 session->ciphersuite = (p[0] << 8) | p[1];
Jerry Yu4f9e3ef2022-02-17 14:58:27 +08008960 p += 2;
8961
Jerry Yu4f9e3ef2022-02-17 14:58:27 +08008962 session->id_len = *p++;
Gilles Peskine449bd832023-01-11 14:50:10 +01008963 memcpy(session->id, p, 32);
Jerry Yu4f9e3ef2022-02-17 14:58:27 +08008964 p += 32;
8965
Gilles Peskine449bd832023-01-11 14:50:10 +01008966 memcpy(session->master, p, 48);
Jerry Yu4f9e3ef2022-02-17 14:58:27 +08008967 p += 48;
8968
Gilles Peskine449bd832023-01-11 14:50:10 +01008969 session->verify_result = ((uint32_t) p[0] << 24) |
8970 ((uint32_t) p[1] << 16) |
8971 ((uint32_t) p[2] << 8) |
8972 ((uint32_t) p[3]);
Jerry Yu4f9e3ef2022-02-17 14:58:27 +08008973 p += 4;
8974
8975 /* Immediately clear invalid pointer values that have been read, in case
8976 * we exit early before we replaced them with valid ones. */
8977#if defined(MBEDTLS_X509_CRT_PARSE_C)
8978#if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
8979 session->peer_cert = NULL;
8980#else
8981 session->peer_cert_digest = NULL;
8982#endif /* !MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
8983#endif /* MBEDTLS_X509_CRT_PARSE_C */
8984#if defined(MBEDTLS_SSL_SESSION_TICKETS) && defined(MBEDTLS_SSL_CLI_C)
8985 session->ticket = NULL;
8986#endif /* MBEDTLS_SSL_SESSION_TICKETS && MBEDTLS_SSL_CLI_C */
8987
8988 /*
8989 * Peer certificate
8990 */
8991#if defined(MBEDTLS_X509_CRT_PARSE_C)
8992#if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
8993 /* Deserialize CRT from the end of the ticket. */
Gilles Peskine449bd832023-01-11 14:50:10 +01008994 if (3 > (size_t) (end - p)) {
8995 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
8996 }
Jerry Yu4f9e3ef2022-02-17 14:58:27 +08008997
Gilles Peskine449bd832023-01-11 14:50:10 +01008998 cert_len = (p[0] << 16) | (p[1] << 8) | p[2];
Jerry Yu4f9e3ef2022-02-17 14:58:27 +08008999 p += 3;
9000
Gilles Peskine449bd832023-01-11 14:50:10 +01009001 if (cert_len != 0) {
Jerry Yu4f9e3ef2022-02-17 14:58:27 +08009002 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
9003
Gilles Peskine449bd832023-01-11 14:50:10 +01009004 if (cert_len > (size_t) (end - p)) {
9005 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
9006 }
Jerry Yu4f9e3ef2022-02-17 14:58:27 +08009007
Gilles Peskine449bd832023-01-11 14:50:10 +01009008 session->peer_cert = mbedtls_calloc(1, sizeof(mbedtls_x509_crt));
Jerry Yu4f9e3ef2022-02-17 14:58:27 +08009009
Gilles Peskine449bd832023-01-11 14:50:10 +01009010 if (session->peer_cert == NULL) {
9011 return MBEDTLS_ERR_SSL_ALLOC_FAILED;
9012 }
Jerry Yu4f9e3ef2022-02-17 14:58:27 +08009013
Gilles Peskine449bd832023-01-11 14:50:10 +01009014 mbedtls_x509_crt_init(session->peer_cert);
Jerry Yu4f9e3ef2022-02-17 14:58:27 +08009015
Gilles Peskine449bd832023-01-11 14:50:10 +01009016 if ((ret = mbedtls_x509_crt_parse_der(session->peer_cert,
9017 p, cert_len)) != 0) {
9018 mbedtls_x509_crt_free(session->peer_cert);
9019 mbedtls_free(session->peer_cert);
Jerry Yu4f9e3ef2022-02-17 14:58:27 +08009020 session->peer_cert = NULL;
Gilles Peskine449bd832023-01-11 14:50:10 +01009021 return ret;
Jerry Yu4f9e3ef2022-02-17 14:58:27 +08009022 }
9023
9024 p += cert_len;
9025 }
9026#else /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
9027 /* Deserialize CRT digest from the end of the ticket. */
Gilles Peskine449bd832023-01-11 14:50:10 +01009028 if (2 > (size_t) (end - p)) {
9029 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
9030 }
Jerry Yu4f9e3ef2022-02-17 14:58:27 +08009031
9032 session->peer_cert_digest_type = (mbedtls_md_type_t) *p++;
9033 session->peer_cert_digest_len = (size_t) *p++;
9034
Gilles Peskine449bd832023-01-11 14:50:10 +01009035 if (session->peer_cert_digest_len != 0) {
Jerry Yu4f9e3ef2022-02-17 14:58:27 +08009036 const mbedtls_md_info_t *md_info =
Gilles Peskine449bd832023-01-11 14:50:10 +01009037 mbedtls_md_info_from_type(session->peer_cert_digest_type);
9038 if (md_info == NULL) {
9039 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
9040 }
9041 if (session->peer_cert_digest_len != mbedtls_md_get_size(md_info)) {
9042 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
9043 }
Jerry Yu4f9e3ef2022-02-17 14:58:27 +08009044
Gilles Peskine449bd832023-01-11 14:50:10 +01009045 if (session->peer_cert_digest_len > (size_t) (end - p)) {
9046 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
9047 }
Jerry Yu4f9e3ef2022-02-17 14:58:27 +08009048
9049 session->peer_cert_digest =
Gilles Peskine449bd832023-01-11 14:50:10 +01009050 mbedtls_calloc(1, session->peer_cert_digest_len);
9051 if (session->peer_cert_digest == NULL) {
9052 return MBEDTLS_ERR_SSL_ALLOC_FAILED;
9053 }
Jerry Yu4f9e3ef2022-02-17 14:58:27 +08009054
Gilles Peskine449bd832023-01-11 14:50:10 +01009055 memcpy(session->peer_cert_digest, p,
9056 session->peer_cert_digest_len);
Jerry Yu4f9e3ef2022-02-17 14:58:27 +08009057 p += session->peer_cert_digest_len;
9058 }
9059#endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
9060#endif /* MBEDTLS_X509_CRT_PARSE_C */
9061
9062 /*
9063 * Session ticket and associated data
9064 */
9065#if defined(MBEDTLS_SSL_SESSION_TICKETS) && defined(MBEDTLS_SSL_CLI_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01009066 if (3 > (size_t) (end - p)) {
9067 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
9068 }
Jerry Yu4f9e3ef2022-02-17 14:58:27 +08009069
Gilles Peskine449bd832023-01-11 14:50:10 +01009070 session->ticket_len = (p[0] << 16) | (p[1] << 8) | p[2];
Jerry Yu4f9e3ef2022-02-17 14:58:27 +08009071 p += 3;
9072
Gilles Peskine449bd832023-01-11 14:50:10 +01009073 if (session->ticket_len != 0) {
9074 if (session->ticket_len > (size_t) (end - p)) {
9075 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
9076 }
Jerry Yu4f9e3ef2022-02-17 14:58:27 +08009077
Gilles Peskine449bd832023-01-11 14:50:10 +01009078 session->ticket = mbedtls_calloc(1, session->ticket_len);
9079 if (session->ticket == NULL) {
9080 return MBEDTLS_ERR_SSL_ALLOC_FAILED;
9081 }
Jerry Yu4f9e3ef2022-02-17 14:58:27 +08009082
Gilles Peskine449bd832023-01-11 14:50:10 +01009083 memcpy(session->ticket, p, session->ticket_len);
Jerry Yu4f9e3ef2022-02-17 14:58:27 +08009084 p += session->ticket_len;
9085 }
9086
Gilles Peskine449bd832023-01-11 14:50:10 +01009087 if (4 > (size_t) (end - p)) {
9088 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
9089 }
Jerry Yu4f9e3ef2022-02-17 14:58:27 +08009090
Gilles Peskine449bd832023-01-11 14:50:10 +01009091 session->ticket_lifetime = ((uint32_t) p[0] << 24) |
9092 ((uint32_t) p[1] << 16) |
9093 ((uint32_t) p[2] << 8) |
9094 ((uint32_t) p[3]);
Jerry Yu4f9e3ef2022-02-17 14:58:27 +08009095 p += 4;
9096#endif /* MBEDTLS_SSL_SESSION_TICKETS && MBEDTLS_SSL_CLI_C */
9097
9098 /*
9099 * Misc extension-related info
9100 */
9101#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
Gilles Peskine449bd832023-01-11 14:50:10 +01009102 if (1 > (size_t) (end - p)) {
9103 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
9104 }
Jerry Yu4f9e3ef2022-02-17 14:58:27 +08009105
9106 session->mfl_code = *p++;
9107#endif
9108
9109#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
Gilles Peskine449bd832023-01-11 14:50:10 +01009110 if (1 > (size_t) (end - p)) {
9111 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
9112 }
Jerry Yu4f9e3ef2022-02-17 14:58:27 +08009113
9114 session->encrypt_then_mac = *p++;
9115#endif
9116
9117 /* Done, should have consumed entire buffer */
Gilles Peskine449bd832023-01-11 14:50:10 +01009118 if (p != end) {
9119 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
9120 }
Jerry Yu4f9e3ef2022-02-17 14:58:27 +08009121
Gilles Peskine449bd832023-01-11 14:50:10 +01009122 return 0;
Jerry Yu4f9e3ef2022-02-17 14:58:27 +08009123}
Jerry Yudc7bd172022-02-17 13:44:15 +08009124#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
9125
XiaokangQian75d40ef2022-04-20 11:05:24 +00009126int mbedtls_ssl_validate_ciphersuite(
9127 const mbedtls_ssl_context *ssl,
9128 const mbedtls_ssl_ciphersuite_t *suite_info,
9129 mbedtls_ssl_protocol_version min_tls_version,
Gilles Peskine449bd832023-01-11 14:50:10 +01009130 mbedtls_ssl_protocol_version max_tls_version)
XiaokangQian75d40ef2022-04-20 11:05:24 +00009131{
9132 (void) ssl;
9133
Gilles Peskine449bd832023-01-11 14:50:10 +01009134 if (suite_info == NULL) {
9135 return -1;
9136 }
XiaokangQian75d40ef2022-04-20 11:05:24 +00009137
Gilles Peskine449bd832023-01-11 14:50:10 +01009138 if ((suite_info->min_tls_version > max_tls_version) ||
9139 (suite_info->max_tls_version < min_tls_version)) {
9140 return -1;
XiaokangQian75d40ef2022-04-20 11:05:24 +00009141 }
9142
XiaokangQian060d8672022-04-21 09:24:56 +00009143#if defined(MBEDTLS_SSL_PROTO_TLS1_2) && defined(MBEDTLS_SSL_CLI_C)
XiaokangQian75d40ef2022-04-20 11:05:24 +00009144#if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
Neil Armstrongca7d5062022-05-31 14:43:23 +02009145#if defined(MBEDTLS_USE_PSA_CRYPTO)
Gilles Peskine449bd832023-01-11 14:50:10 +01009146 if (suite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECJPAKE &&
9147 ssl->handshake->psa_pake_ctx_is_ok != 1)
Neil Armstrongca7d5062022-05-31 14:43:23 +02009148#else
Gilles Peskine449bd832023-01-11 14:50:10 +01009149 if (suite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECJPAKE &&
9150 mbedtls_ecjpake_check(&ssl->handshake->ecjpake_ctx) != 0)
Neil Armstrongca7d5062022-05-31 14:43:23 +02009151#endif /* MBEDTLS_USE_PSA_CRYPTO */
XiaokangQian75d40ef2022-04-20 11:05:24 +00009152 {
Gilles Peskine449bd832023-01-11 14:50:10 +01009153 return -1;
XiaokangQian75d40ef2022-04-20 11:05:24 +00009154 }
9155#endif
9156
9157 /* Don't suggest PSK-based ciphersuite if no PSK is available. */
9158#if defined(MBEDTLS_KEY_EXCHANGE_SOME_PSK_ENABLED)
Gilles Peskine449bd832023-01-11 14:50:10 +01009159 if (mbedtls_ssl_ciphersuite_uses_psk(suite_info) &&
9160 mbedtls_ssl_conf_has_static_psk(ssl->conf) == 0) {
9161 return -1;
XiaokangQian75d40ef2022-04-20 11:05:24 +00009162 }
9163#endif /* MBEDTLS_KEY_EXCHANGE_SOME_PSK_ENABLED */
9164#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
9165
Gilles Peskine449bd832023-01-11 14:50:10 +01009166 return 0;
XiaokangQian75d40ef2022-04-20 11:05:24 +00009167}
9168
Ronald Crone68ab4f2022-10-05 12:46:29 +02009169#if defined(MBEDTLS_SSL_HANDSHAKE_WITH_CERT_ENABLED)
XiaokangQianeaf36512022-04-24 09:07:44 +00009170/*
9171 * Function for writing a signature algorithm extension.
9172 *
9173 * The `extension_data` field of signature algorithm contains a `SignatureSchemeList`
9174 * value (TLS 1.3 RFC8446):
9175 * enum {
9176 * ....
9177 * ecdsa_secp256r1_sha256( 0x0403 ),
9178 * ecdsa_secp384r1_sha384( 0x0503 ),
9179 * ecdsa_secp521r1_sha512( 0x0603 ),
9180 * ....
9181 * } SignatureScheme;
9182 *
9183 * struct {
9184 * SignatureScheme supported_signature_algorithms<2..2^16-2>;
9185 * } SignatureSchemeList;
9186 *
9187 * The `extension_data` field of signature algorithm contains a `SignatureAndHashAlgorithm`
9188 * value (TLS 1.2 RFC5246):
9189 * enum {
9190 * none(0), md5(1), sha1(2), sha224(3), sha256(4), sha384(5),
9191 * sha512(6), (255)
9192 * } HashAlgorithm;
9193 *
9194 * enum { anonymous(0), rsa(1), dsa(2), ecdsa(3), (255) }
9195 * SignatureAlgorithm;
9196 *
9197 * struct {
9198 * HashAlgorithm hash;
9199 * SignatureAlgorithm signature;
9200 * } SignatureAndHashAlgorithm;
9201 *
9202 * SignatureAndHashAlgorithm
9203 * supported_signature_algorithms<2..2^16-2>;
9204 *
9205 * The TLS 1.3 signature algorithm extension was defined to be a compatible
9206 * generalization of the TLS 1.2 signature algorithm extension.
9207 * `SignatureAndHashAlgorithm` field of TLS 1.2 can be represented by
9208 * `SignatureScheme` field of TLS 1.3
9209 *
9210 */
Gilles Peskine449bd832023-01-11 14:50:10 +01009211int mbedtls_ssl_write_sig_alg_ext(mbedtls_ssl_context *ssl, unsigned char *buf,
9212 const unsigned char *end, size_t *out_len)
XiaokangQianeaf36512022-04-24 09:07:44 +00009213{
9214 unsigned char *p = buf;
9215 unsigned char *supported_sig_alg; /* Start of supported_signature_algorithms */
9216 size_t supported_sig_alg_len = 0; /* Length of supported_signature_algorithms */
9217
9218 *out_len = 0;
9219
Gilles Peskine449bd832023-01-11 14:50:10 +01009220 MBEDTLS_SSL_DEBUG_MSG(3, ("adding signature_algorithms extension"));
XiaokangQianeaf36512022-04-24 09:07:44 +00009221
9222 /* Check if we have space for header and length field:
9223 * - extension_type (2 bytes)
9224 * - extension_data_length (2 bytes)
9225 * - supported_signature_algorithms_length (2 bytes)
9226 */
Gilles Peskine449bd832023-01-11 14:50:10 +01009227 MBEDTLS_SSL_CHK_BUF_PTR(p, end, 6);
XiaokangQianeaf36512022-04-24 09:07:44 +00009228 p += 6;
9229
9230 /*
9231 * Write supported_signature_algorithms
9232 */
9233 supported_sig_alg = p;
Gilles Peskine449bd832023-01-11 14:50:10 +01009234 const uint16_t *sig_alg = mbedtls_ssl_get_sig_algs(ssl);
9235 if (sig_alg == NULL) {
9236 return MBEDTLS_ERR_SSL_BAD_CONFIG;
9237 }
XiaokangQianeaf36512022-04-24 09:07:44 +00009238
Gilles Peskine449bd832023-01-11 14:50:10 +01009239 for (; *sig_alg != MBEDTLS_TLS1_3_SIG_NONE; sig_alg++) {
9240 MBEDTLS_SSL_DEBUG_MSG(3, ("got signature scheme [%x] %s",
9241 *sig_alg,
9242 mbedtls_ssl_sig_alg_to_str(*sig_alg)));
9243 if (!mbedtls_ssl_sig_alg_is_supported(ssl, *sig_alg)) {
XiaokangQianeaf36512022-04-24 09:07:44 +00009244 continue;
Gilles Peskine449bd832023-01-11 14:50:10 +01009245 }
9246 MBEDTLS_SSL_CHK_BUF_PTR(p, end, 2);
9247 MBEDTLS_PUT_UINT16_BE(*sig_alg, p, 0);
XiaokangQianeaf36512022-04-24 09:07:44 +00009248 p += 2;
Gilles Peskine449bd832023-01-11 14:50:10 +01009249 MBEDTLS_SSL_DEBUG_MSG(3, ("sent signature scheme [%x] %s",
9250 *sig_alg,
9251 mbedtls_ssl_sig_alg_to_str(*sig_alg)));
XiaokangQianeaf36512022-04-24 09:07:44 +00009252 }
9253
9254 /* Length of supported_signature_algorithms */
9255 supported_sig_alg_len = p - supported_sig_alg;
Gilles Peskine449bd832023-01-11 14:50:10 +01009256 if (supported_sig_alg_len == 0) {
9257 MBEDTLS_SSL_DEBUG_MSG(1, ("No signature algorithms defined."));
9258 return MBEDTLS_ERR_SSL_INTERNAL_ERROR;
XiaokangQianeaf36512022-04-24 09:07:44 +00009259 }
9260
Gilles Peskine449bd832023-01-11 14:50:10 +01009261 MBEDTLS_PUT_UINT16_BE(MBEDTLS_TLS_EXT_SIG_ALG, buf, 0);
9262 MBEDTLS_PUT_UINT16_BE(supported_sig_alg_len + 2, buf, 2);
9263 MBEDTLS_PUT_UINT16_BE(supported_sig_alg_len, buf, 4);
XiaokangQianeaf36512022-04-24 09:07:44 +00009264
XiaokangQianeaf36512022-04-24 09:07:44 +00009265 *out_len = p - buf;
9266
9267#if defined(MBEDTLS_SSL_PROTO_TLS1_3)
Gilles Peskine449bd832023-01-11 14:50:10 +01009268 mbedtls_ssl_tls13_set_hs_sent_ext_mask(ssl, MBEDTLS_TLS_EXT_SIG_ALG);
XiaokangQianeaf36512022-04-24 09:07:44 +00009269#endif /* MBEDTLS_SSL_PROTO_TLS1_3 */
Jerry Yu0c354a22022-08-29 15:25:36 +08009270
Gilles Peskine449bd832023-01-11 14:50:10 +01009271 return 0;
XiaokangQianeaf36512022-04-24 09:07:44 +00009272}
Ronald Crone68ab4f2022-10-05 12:46:29 +02009273#endif /* MBEDTLS_SSL_HANDSHAKE_WITH_CERT_ENABLED */
XiaokangQianeaf36512022-04-24 09:07:44 +00009274
XiaokangQian40a35232022-05-07 09:02:40 +00009275#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
XiaokangQian9b2b7712022-05-17 02:57:00 +00009276/*
9277 * mbedtls_ssl_parse_server_name_ext
9278 *
9279 * Structure of server_name extension:
9280 *
9281 * enum {
9282 * host_name(0), (255)
9283 * } NameType;
9284 * opaque HostName<1..2^16-1>;
9285 *
9286 * struct {
9287 * NameType name_type;
9288 * select (name_type) {
9289 * case host_name: HostName;
9290 * } name;
9291 * } ServerName;
9292 * struct {
9293 * ServerName server_name_list<1..2^16-1>
9294 * } ServerNameList;
9295 */
Ronald Cronce7d76e2022-07-08 18:56:49 +02009296MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01009297int mbedtls_ssl_parse_server_name_ext(mbedtls_ssl_context *ssl,
9298 const unsigned char *buf,
9299 const unsigned char *end)
XiaokangQian40a35232022-05-07 09:02:40 +00009300{
9301 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
9302 const unsigned char *p = buf;
XiaokangQian9b2b7712022-05-17 02:57:00 +00009303 size_t server_name_list_len, hostname_len;
9304 const unsigned char *server_name_list_end;
XiaokangQian40a35232022-05-07 09:02:40 +00009305
Gilles Peskine449bd832023-01-11 14:50:10 +01009306 MBEDTLS_SSL_DEBUG_MSG(3, ("parse ServerName extension"));
XiaokangQian40a35232022-05-07 09:02:40 +00009307
Gilles Peskine449bd832023-01-11 14:50:10 +01009308 MBEDTLS_SSL_CHK_BUF_READ_PTR(p, end, 2);
9309 server_name_list_len = MBEDTLS_GET_UINT16_BE(p, 0);
XiaokangQian40a35232022-05-07 09:02:40 +00009310 p += 2;
9311
Gilles Peskine449bd832023-01-11 14:50:10 +01009312 MBEDTLS_SSL_CHK_BUF_READ_PTR(p, end, server_name_list_len);
XiaokangQian9b2b7712022-05-17 02:57:00 +00009313 server_name_list_end = p + server_name_list_len;
Gilles Peskine449bd832023-01-11 14:50:10 +01009314 while (p < server_name_list_end) {
9315 MBEDTLS_SSL_CHK_BUF_READ_PTR(p, server_name_list_end, 3);
9316 hostname_len = MBEDTLS_GET_UINT16_BE(p, 1);
9317 MBEDTLS_SSL_CHK_BUF_READ_PTR(p, server_name_list_end,
9318 hostname_len + 3);
XiaokangQian40a35232022-05-07 09:02:40 +00009319
Gilles Peskine449bd832023-01-11 14:50:10 +01009320 if (p[0] == MBEDTLS_TLS_EXT_SERVERNAME_HOSTNAME) {
XiaokangQian75fe8c72022-06-15 09:42:45 +00009321 /* sni_name is intended to be used only during the parsing of the
9322 * ClientHello message (it is reset to NULL before the end of
9323 * the message parsing). Thus it is ok to just point to the
9324 * reception buffer and not make a copy of it.
9325 */
XiaokangQianf2a94202022-05-20 06:44:24 +00009326 ssl->handshake->sni_name = p + 3;
9327 ssl->handshake->sni_name_len = hostname_len;
Gilles Peskine449bd832023-01-11 14:50:10 +01009328 if (ssl->conf->f_sni == NULL) {
9329 return 0;
XiaokangQian40a35232022-05-07 09:02:40 +00009330 }
Gilles Peskine449bd832023-01-11 14:50:10 +01009331 ret = ssl->conf->f_sni(ssl->conf->p_sni,
9332 ssl, p + 3, hostname_len);
9333 if (ret != 0) {
9334 MBEDTLS_SSL_DEBUG_RET(1, "ssl_sni_wrapper", ret);
9335 MBEDTLS_SSL_PEND_FATAL_ALERT(MBEDTLS_SSL_ALERT_MSG_UNRECOGNIZED_NAME,
9336 MBEDTLS_ERR_SSL_UNRECOGNIZED_NAME);
9337 return MBEDTLS_ERR_SSL_UNRECOGNIZED_NAME;
9338 }
9339 return 0;
XiaokangQian40a35232022-05-07 09:02:40 +00009340 }
9341
9342 p += hostname_len + 3;
9343 }
9344
Gilles Peskine449bd832023-01-11 14:50:10 +01009345 return 0;
XiaokangQian40a35232022-05-07 09:02:40 +00009346}
9347#endif /* MBEDTLS_SSL_SERVER_NAME_INDICATION */
9348
XiaokangQianacb39922022-06-17 10:18:48 +00009349#if defined(MBEDTLS_SSL_ALPN)
Ronald Cronce7d76e2022-07-08 18:56:49 +02009350MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01009351int mbedtls_ssl_parse_alpn_ext(mbedtls_ssl_context *ssl,
9352 const unsigned char *buf,
9353 const unsigned char *end)
XiaokangQianacb39922022-06-17 10:18:48 +00009354{
9355 const unsigned char *p = buf;
XiaokangQianc7403452022-06-23 03:24:12 +00009356 size_t protocol_name_list_len;
XiaokangQian95d5f542022-06-24 02:29:26 +00009357 const unsigned char *protocol_name_list;
9358 const unsigned char *protocol_name_list_end;
XiaokangQianc7403452022-06-23 03:24:12 +00009359 size_t protocol_name_len;
XiaokangQianacb39922022-06-17 10:18:48 +00009360
9361 /* If ALPN not configured, just ignore the extension */
Gilles Peskine449bd832023-01-11 14:50:10 +01009362 if (ssl->conf->alpn_list == NULL) {
9363 return 0;
9364 }
XiaokangQianacb39922022-06-17 10:18:48 +00009365
9366 /*
XiaokangQianc7403452022-06-23 03:24:12 +00009367 * RFC7301, section 3.1
9368 * opaque ProtocolName<1..2^8-1>;
XiaokangQianacb39922022-06-17 10:18:48 +00009369 *
XiaokangQianc7403452022-06-23 03:24:12 +00009370 * struct {
9371 * ProtocolName protocol_name_list<2..2^16-1>
9372 * } ProtocolNameList;
XiaokangQianacb39922022-06-17 10:18:48 +00009373 */
9374
XiaokangQianc7403452022-06-23 03:24:12 +00009375 /*
XiaokangQian0b776e22022-06-24 09:04:59 +00009376 * protocol_name_list_len 2 bytes
9377 * protocol_name_len 1 bytes
9378 * protocol_name >=1 byte
XiaokangQianc7403452022-06-23 03:24:12 +00009379 */
Gilles Peskine449bd832023-01-11 14:50:10 +01009380 MBEDTLS_SSL_CHK_BUF_READ_PTR(p, end, 4);
XiaokangQianacb39922022-06-17 10:18:48 +00009381
Gilles Peskine449bd832023-01-11 14:50:10 +01009382 protocol_name_list_len = MBEDTLS_GET_UINT16_BE(p, 0);
XiaokangQianacb39922022-06-17 10:18:48 +00009383 p += 2;
Gilles Peskine449bd832023-01-11 14:50:10 +01009384 MBEDTLS_SSL_CHK_BUF_READ_PTR(p, end, protocol_name_list_len);
XiaokangQian95d5f542022-06-24 02:29:26 +00009385 protocol_name_list = p;
9386 protocol_name_list_end = p + protocol_name_list_len;
XiaokangQianacb39922022-06-17 10:18:48 +00009387
9388 /* Validate peer's list (lengths) */
Gilles Peskine449bd832023-01-11 14:50:10 +01009389 while (p < protocol_name_list_end) {
XiaokangQian95d5f542022-06-24 02:29:26 +00009390 protocol_name_len = *p++;
Gilles Peskine449bd832023-01-11 14:50:10 +01009391 MBEDTLS_SSL_CHK_BUF_READ_PTR(p, protocol_name_list_end,
9392 protocol_name_len);
9393 if (protocol_name_len == 0) {
XiaokangQian95d5f542022-06-24 02:29:26 +00009394 MBEDTLS_SSL_PEND_FATAL_ALERT(
9395 MBEDTLS_SSL_ALERT_MSG_ILLEGAL_PARAMETER,
Gilles Peskine449bd832023-01-11 14:50:10 +01009396 MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER);
9397 return MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER;
XiaokangQian95d5f542022-06-24 02:29:26 +00009398 }
9399
9400 p += protocol_name_len;
XiaokangQianacb39922022-06-17 10:18:48 +00009401 }
9402
9403 /* Use our order of preference */
Gilles Peskine449bd832023-01-11 14:50:10 +01009404 for (const char **alpn = ssl->conf->alpn_list; *alpn != NULL; alpn++) {
9405 size_t const alpn_len = strlen(*alpn);
XiaokangQian95d5f542022-06-24 02:29:26 +00009406 p = protocol_name_list;
Gilles Peskine449bd832023-01-11 14:50:10 +01009407 while (p < protocol_name_list_end) {
XiaokangQian95d5f542022-06-24 02:29:26 +00009408 protocol_name_len = *p++;
Gilles Peskine449bd832023-01-11 14:50:10 +01009409 if (protocol_name_len == alpn_len &&
9410 memcmp(p, *alpn, alpn_len) == 0) {
XiaokangQianacb39922022-06-17 10:18:48 +00009411 ssl->alpn_chosen = *alpn;
Gilles Peskine449bd832023-01-11 14:50:10 +01009412 return 0;
XiaokangQianacb39922022-06-17 10:18:48 +00009413 }
XiaokangQian95d5f542022-06-24 02:29:26 +00009414
9415 p += protocol_name_len;
XiaokangQianacb39922022-06-17 10:18:48 +00009416 }
9417 }
9418
XiaokangQian95d5f542022-06-24 02:29:26 +00009419 /* If we get here, no match was found */
XiaokangQianacb39922022-06-17 10:18:48 +00009420 MBEDTLS_SSL_PEND_FATAL_ALERT(
Gilles Peskine449bd832023-01-11 14:50:10 +01009421 MBEDTLS_SSL_ALERT_MSG_NO_APPLICATION_PROTOCOL,
9422 MBEDTLS_ERR_SSL_NO_APPLICATION_PROTOCOL);
9423 return MBEDTLS_ERR_SSL_NO_APPLICATION_PROTOCOL;
XiaokangQianacb39922022-06-17 10:18:48 +00009424}
9425
Gilles Peskine449bd832023-01-11 14:50:10 +01009426int mbedtls_ssl_write_alpn_ext(mbedtls_ssl_context *ssl,
9427 unsigned char *buf,
9428 unsigned char *end,
9429 size_t *out_len)
XiaokangQianacb39922022-06-17 10:18:48 +00009430{
9431 unsigned char *p = buf;
XiaokangQian95d5f542022-06-24 02:29:26 +00009432 size_t protocol_name_len;
XiaokangQianc7403452022-06-23 03:24:12 +00009433 *out_len = 0;
XiaokangQianacb39922022-06-17 10:18:48 +00009434
Gilles Peskine449bd832023-01-11 14:50:10 +01009435 if (ssl->alpn_chosen == NULL) {
9436 return 0;
XiaokangQianacb39922022-06-17 10:18:48 +00009437 }
9438
Gilles Peskine449bd832023-01-11 14:50:10 +01009439 protocol_name_len = strlen(ssl->alpn_chosen);
9440 MBEDTLS_SSL_CHK_BUF_PTR(p, end, 7 + protocol_name_len);
XiaokangQianacb39922022-06-17 10:18:48 +00009441
Gilles Peskine449bd832023-01-11 14:50:10 +01009442 MBEDTLS_SSL_DEBUG_MSG(3, ("server side, adding alpn extension"));
XiaokangQianacb39922022-06-17 10:18:48 +00009443 /*
9444 * 0 . 1 ext identifier
9445 * 2 . 3 ext length
9446 * 4 . 5 protocol list length
9447 * 6 . 6 protocol name length
9448 * 7 . 7+n protocol name
9449 */
Gilles Peskine449bd832023-01-11 14:50:10 +01009450 MBEDTLS_PUT_UINT16_BE(MBEDTLS_TLS_EXT_ALPN, p, 0);
XiaokangQianacb39922022-06-17 10:18:48 +00009451
XiaokangQian95d5f542022-06-24 02:29:26 +00009452 *out_len = 7 + protocol_name_len;
XiaokangQianacb39922022-06-17 10:18:48 +00009453
Gilles Peskine449bd832023-01-11 14:50:10 +01009454 MBEDTLS_PUT_UINT16_BE(protocol_name_len + 3, p, 2);
9455 MBEDTLS_PUT_UINT16_BE(protocol_name_len + 1, p, 4);
XiaokangQian0b776e22022-06-24 09:04:59 +00009456 /* Note: the length of the chosen protocol has been checked to be less
9457 * than 255 bytes in `mbedtls_ssl_conf_alpn_protocols`.
9458 */
Gilles Peskine449bd832023-01-11 14:50:10 +01009459 p[6] = MBEDTLS_BYTE_0(protocol_name_len);
XiaokangQianacb39922022-06-17 10:18:48 +00009460
Gilles Peskine449bd832023-01-11 14:50:10 +01009461 memcpy(p + 7, ssl->alpn_chosen, protocol_name_len);
Jerry Yub95dd362022-11-08 21:19:34 +08009462
9463#if defined(MBEDTLS_SSL_PROTO_TLS1_3)
Gilles Peskine449bd832023-01-11 14:50:10 +01009464 mbedtls_ssl_tls13_set_hs_sent_ext_mask(ssl, MBEDTLS_TLS_EXT_ALPN);
Jerry Yub95dd362022-11-08 21:19:34 +08009465#endif
9466
Gilles Peskine449bd832023-01-11 14:50:10 +01009467 return 0;
XiaokangQianacb39922022-06-17 10:18:48 +00009468}
9469#endif /* MBEDTLS_SSL_ALPN */
9470
Xiaokang Qiana3b451f2022-10-11 06:20:56 +00009471#if defined(MBEDTLS_SSL_PROTO_TLS1_3) && \
Xiaokang Qian03409292022-10-12 02:49:52 +00009472 defined(MBEDTLS_SSL_SESSION_TICKETS) && \
Xiaokang Qianed0620c2022-10-12 06:58:13 +00009473 defined(MBEDTLS_SSL_SERVER_NAME_INDICATION) && \
Xiaokang Qianed3afcd2022-10-12 08:31:11 +00009474 defined(MBEDTLS_SSL_CLI_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01009475int mbedtls_ssl_session_set_hostname(mbedtls_ssl_session *session,
9476 const char *hostname)
Xiaokang Qiana3b451f2022-10-11 06:20:56 +00009477{
9478 /* Initialize to suppress unnecessary compiler warning */
9479 size_t hostname_len = 0;
9480
9481 /* Check if new hostname is valid before
9482 * making any change to current one */
Gilles Peskine449bd832023-01-11 14:50:10 +01009483 if (hostname != NULL) {
9484 hostname_len = strlen(hostname);
Xiaokang Qiana3b451f2022-10-11 06:20:56 +00009485
Gilles Peskine449bd832023-01-11 14:50:10 +01009486 if (hostname_len > MBEDTLS_SSL_MAX_HOST_NAME_LEN) {
9487 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
9488 }
Xiaokang Qiana3b451f2022-10-11 06:20:56 +00009489 }
9490
9491 /* Now it's clear that we will overwrite the old hostname,
9492 * so we can free it safely */
Gilles Peskine449bd832023-01-11 14:50:10 +01009493 if (session->hostname != NULL) {
9494 mbedtls_platform_zeroize(session->hostname,
9495 strlen(session->hostname));
9496 mbedtls_free(session->hostname);
Xiaokang Qiana3b451f2022-10-11 06:20:56 +00009497 }
9498
9499 /* Passing NULL as hostname shall clear the old one */
Gilles Peskine449bd832023-01-11 14:50:10 +01009500 if (hostname == NULL) {
Xiaokang Qiana3b451f2022-10-11 06:20:56 +00009501 session->hostname = NULL;
Gilles Peskine449bd832023-01-11 14:50:10 +01009502 } else {
9503 session->hostname = mbedtls_calloc(1, hostname_len + 1);
9504 if (session->hostname == NULL) {
9505 return MBEDTLS_ERR_SSL_ALLOC_FAILED;
9506 }
Xiaokang Qiana3b451f2022-10-11 06:20:56 +00009507
Gilles Peskine449bd832023-01-11 14:50:10 +01009508 memcpy(session->hostname, hostname, hostname_len);
Xiaokang Qiana3b451f2022-10-11 06:20:56 +00009509 }
9510
Gilles Peskine449bd832023-01-11 14:50:10 +01009511 return 0;
Xiaokang Qiana3b451f2022-10-11 06:20:56 +00009512}
Xiaokang Qian03409292022-10-12 02:49:52 +00009513#endif /* MBEDTLS_SSL_PROTO_TLS1_3 &&
9514 MBEDTLS_SSL_SESSION_TICKETS &&
Xiaokang Qianed0620c2022-10-12 06:58:13 +00009515 MBEDTLS_SSL_SERVER_NAME_INDICATION &&
Xiaokang Qianed3afcd2022-10-12 08:31:11 +00009516 MBEDTLS_SSL_CLI_C */
Xiaokang Qiana3b451f2022-10-11 06:20:56 +00009517
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009518#endif /* MBEDTLS_SSL_TLS_C */