blob: 7798d78cb0160b40f547f09c707f6bcd7c9d4f33 [file] [log] [blame]
Paul Bakker5121ce52009-01-03 21:22:43 +00001/*
Mateusz Starzyk06b07fb2021-02-18 13:55:21 +01002 * TLS shared functions
Paul Bakker5121ce52009-01-03 21:22:43 +00003 *
Bence Szépkúti1e148272020-08-07 13:07:28 +02004 * Copyright The Mbed TLS Contributors
Manuel Pégourié-Gonnard37ff1402015-09-04 14:21:07 +02005 * SPDX-License-Identifier: Apache-2.0
6 *
7 * Licensed under the Apache License, Version 2.0 (the "License"); you may
8 * not use this file except in compliance with the License.
9 * You may obtain a copy of the License at
10 *
11 * http://www.apache.org/licenses/LICENSE-2.0
12 *
13 * Unless required by applicable law or agreed to in writing, software
14 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
15 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 * See the License for the specific language governing permissions and
17 * limitations under the License.
Paul Bakker5121ce52009-01-03 21:22:43 +000018 */
19/*
Paul Bakker5121ce52009-01-03 21:22:43 +000020 * http://www.ietf.org/rfc/rfc2246.txt
21 * http://www.ietf.org/rfc/rfc4346.txt
22 */
23
Gilles Peskinedb09ef62020-06-03 01:43:33 +020024#include "common.h"
Paul Bakker5121ce52009-01-03 21:22:43 +000025
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020026#if defined(MBEDTLS_SSL_TLS_C)
Paul Bakker5121ce52009-01-03 21:22:43 +000027
Jerry Yub476a442022-01-21 18:14:45 +080028#include <assert.h>
29
SimonBd5800b72016-04-26 07:43:27 +010030#include "mbedtls/platform.h"
SimonBd5800b72016-04-26 07:43:27 +010031
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000032#include "mbedtls/ssl.h"
Ronald Cron9f0fba32022-02-10 16:45:15 +010033#include "ssl_client.h"
Ronald Cron27c85e72022-03-08 11:37:55 +010034#include "ssl_debug_helpers.h"
Chris Jones84a773f2021-03-05 18:38:47 +000035#include "ssl_misc.h"
Andrzej Kurek25f27152022-08-17 16:09:31 -040036
Janos Follath73c616b2019-12-18 15:07:04 +000037#include "mbedtls/debug.h"
38#include "mbedtls/error.h"
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -050039#include "mbedtls/platform_util.h"
Hanno Beckera835da52019-05-16 12:39:07 +010040#include "mbedtls/version.h"
Gabor Mezei765862c2021-10-19 12:22:25 +020041#include "mbedtls/constant_time.h"
Paul Bakker0be444a2013-08-27 21:55:01 +020042
Rich Evans00ab4702015-02-06 13:43:58 +000043#include <string.h>
44
Andrzej Kurekd6db9be2019-01-10 05:27:10 -050045#if defined(MBEDTLS_USE_PSA_CRYPTO)
46#include "mbedtls/psa_util.h"
47#include "psa/crypto.h"
48#endif
Manuel Pégourié-Gonnard07018f92022-09-15 11:29:35 +020049#include "mbedtls/legacy_or_psa.h"
Andrzej Kurekd6db9be2019-01-10 05:27:10 -050050
Janos Follath23bdca02016-10-07 14:47:14 +010051#if defined(MBEDTLS_X509_CRT_PARSE_C)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000052#include "mbedtls/oid.h"
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +020053#endif
54
Ronald Cronad8c17b2022-06-10 17:18:09 +020055#if defined(MBEDTLS_TEST_HOOKS)
56static mbedtls_ssl_chk_buf_ptr_args chk_buf_ptr_fail_args;
57
58void mbedtls_ssl_set_chk_buf_ptr_fail_args(
Gilles Peskine449bd832023-01-11 14:50:10 +010059 const uint8_t *cur, const uint8_t *end, size_t need)
Ronald Cronad8c17b2022-06-10 17:18:09 +020060{
61 chk_buf_ptr_fail_args.cur = cur;
62 chk_buf_ptr_fail_args.end = end;
63 chk_buf_ptr_fail_args.need = need;
64}
65
Gilles Peskine449bd832023-01-11 14:50:10 +010066void mbedtls_ssl_reset_chk_buf_ptr_fail_args(void)
Ronald Cronad8c17b2022-06-10 17:18:09 +020067{
Gilles Peskine449bd832023-01-11 14:50:10 +010068 memset(&chk_buf_ptr_fail_args, 0, sizeof(chk_buf_ptr_fail_args));
Ronald Cronad8c17b2022-06-10 17:18:09 +020069}
70
Gilles Peskine449bd832023-01-11 14:50:10 +010071int mbedtls_ssl_cmp_chk_buf_ptr_fail_args(mbedtls_ssl_chk_buf_ptr_args *args)
Ronald Cronad8c17b2022-06-10 17:18:09 +020072{
Gilles Peskine449bd832023-01-11 14:50:10 +010073 return (chk_buf_ptr_fail_args.cur != args->cur) ||
74 (chk_buf_ptr_fail_args.end != args->end) ||
75 (chk_buf_ptr_fail_args.need != args->need);
Ronald Cronad8c17b2022-06-10 17:18:09 +020076}
77#endif /* MBEDTLS_TEST_HOOKS */
78
Manuel Pégourié-Gonnard286a1362015-05-13 16:22:05 +020079#if defined(MBEDTLS_SSL_PROTO_DTLS)
Hanno Becker2b1e3542018-08-06 11:19:13 +010080
Hanno Beckera0e20d02019-05-15 14:03:01 +010081#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Beckerf8542cf2019-04-09 15:22:03 +010082/* Top-level Connection ID API */
83
Gilles Peskine449bd832023-01-11 14:50:10 +010084int mbedtls_ssl_conf_cid(mbedtls_ssl_config *conf,
85 size_t len,
86 int ignore_other_cid)
Hanno Beckerad4a1372019-05-03 13:06:44 +010087{
Gilles Peskine449bd832023-01-11 14:50:10 +010088 if (len > MBEDTLS_SSL_CID_IN_LEN_MAX) {
89 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
90 }
Hanno Beckerad4a1372019-05-03 13:06:44 +010091
Gilles Peskine449bd832023-01-11 14:50:10 +010092 if (ignore_other_cid != MBEDTLS_SSL_UNEXPECTED_CID_FAIL &&
93 ignore_other_cid != MBEDTLS_SSL_UNEXPECTED_CID_IGNORE) {
94 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
Hanno Becker611ac772019-05-14 11:45:26 +010095 }
96
97 conf->ignore_unexpected_cid = ignore_other_cid;
Hanno Beckerad4a1372019-05-03 13:06:44 +010098 conf->cid_len = len;
Gilles Peskine449bd832023-01-11 14:50:10 +010099 return 0;
Hanno Beckerad4a1372019-05-03 13:06:44 +0100100}
101
Gilles Peskine449bd832023-01-11 14:50:10 +0100102int mbedtls_ssl_set_cid(mbedtls_ssl_context *ssl,
103 int enable,
104 unsigned char const *own_cid,
105 size_t own_cid_len)
Hanno Beckerf8542cf2019-04-09 15:22:03 +0100106{
Gilles Peskine449bd832023-01-11 14:50:10 +0100107 if (ssl->conf->transport != MBEDTLS_SSL_TRANSPORT_DATAGRAM) {
108 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
109 }
Hanno Becker76a79ab2019-05-03 14:38:32 +0100110
Hanno Beckerca092242019-04-25 16:01:49 +0100111 ssl->negotiate_cid = enable;
Gilles Peskine449bd832023-01-11 14:50:10 +0100112 if (enable == MBEDTLS_SSL_CID_DISABLED) {
113 MBEDTLS_SSL_DEBUG_MSG(3, ("Disable use of CID extension."));
114 return 0;
Hanno Beckerca092242019-04-25 16:01:49 +0100115 }
Gilles Peskine449bd832023-01-11 14:50:10 +0100116 MBEDTLS_SSL_DEBUG_MSG(3, ("Enable use of CID extension."));
117 MBEDTLS_SSL_DEBUG_BUF(3, "Own CID", own_cid, own_cid_len);
Hanno Beckerca092242019-04-25 16:01:49 +0100118
Gilles Peskine449bd832023-01-11 14:50:10 +0100119 if (own_cid_len != ssl->conf->cid_len) {
120 MBEDTLS_SSL_DEBUG_MSG(3, ("CID length %u does not match CID length %u in config",
121 (unsigned) own_cid_len,
122 (unsigned) ssl->conf->cid_len));
123 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
Hanno Beckerca092242019-04-25 16:01:49 +0100124 }
125
Gilles Peskine449bd832023-01-11 14:50:10 +0100126 memcpy(ssl->own_cid, own_cid, own_cid_len);
Hanno Beckerb7ee0cf2019-04-30 14:07:31 +0100127 /* Truncation is not an issue here because
128 * MBEDTLS_SSL_CID_IN_LEN_MAX at most 255. */
129 ssl->own_cid_len = (uint8_t) own_cid_len;
Hanno Beckerca092242019-04-25 16:01:49 +0100130
Gilles Peskine449bd832023-01-11 14:50:10 +0100131 return 0;
Hanno Beckerf8542cf2019-04-09 15:22:03 +0100132}
133
Gilles Peskine449bd832023-01-11 14:50:10 +0100134int mbedtls_ssl_get_own_cid(mbedtls_ssl_context *ssl,
135 int *enabled,
136 unsigned char own_cid[MBEDTLS_SSL_CID_OUT_LEN_MAX],
137 size_t *own_cid_len)
Paul Elliott0113cf12022-03-11 20:26:47 +0000138{
139 *enabled = MBEDTLS_SSL_CID_DISABLED;
140
Gilles Peskine449bd832023-01-11 14:50:10 +0100141 if (ssl->conf->transport != MBEDTLS_SSL_TRANSPORT_DATAGRAM) {
142 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
143 }
Paul Elliott0113cf12022-03-11 20:26:47 +0000144
145 /* We report MBEDTLS_SSL_CID_DISABLED in case the CID length is
146 * zero as this is indistinguishable from not requesting to use
147 * the CID extension. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100148 if (ssl->own_cid_len == 0 || ssl->negotiate_cid == MBEDTLS_SSL_CID_DISABLED) {
149 return 0;
150 }
Paul Elliott0113cf12022-03-11 20:26:47 +0000151
Gilles Peskine449bd832023-01-11 14:50:10 +0100152 if (own_cid_len != NULL) {
Paul Elliott0113cf12022-03-11 20:26:47 +0000153 *own_cid_len = ssl->own_cid_len;
Gilles Peskine449bd832023-01-11 14:50:10 +0100154 if (own_cid != NULL) {
155 memcpy(own_cid, ssl->own_cid, ssl->own_cid_len);
156 }
Paul Elliott0113cf12022-03-11 20:26:47 +0000157 }
158
159 *enabled = MBEDTLS_SSL_CID_ENABLED;
160
Gilles Peskine449bd832023-01-11 14:50:10 +0100161 return 0;
Paul Elliott0113cf12022-03-11 20:26:47 +0000162}
163
Gilles Peskine449bd832023-01-11 14:50:10 +0100164int mbedtls_ssl_get_peer_cid(mbedtls_ssl_context *ssl,
165 int *enabled,
166 unsigned char peer_cid[MBEDTLS_SSL_CID_OUT_LEN_MAX],
167 size_t *peer_cid_len)
Hanno Beckerf8542cf2019-04-09 15:22:03 +0100168{
Hanno Beckerf8542cf2019-04-09 15:22:03 +0100169 *enabled = MBEDTLS_SSL_CID_DISABLED;
Hanno Beckerb1f89cd2019-04-26 17:08:02 +0100170
Gilles Peskine449bd832023-01-11 14:50:10 +0100171 if (ssl->conf->transport != MBEDTLS_SSL_TRANSPORT_DATAGRAM ||
172 mbedtls_ssl_is_handshake_over(ssl) == 0) {
173 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
Hanno Becker76a79ab2019-05-03 14:38:32 +0100174 }
Hanno Beckerb1f89cd2019-04-26 17:08:02 +0100175
Hanno Beckerc5f24222019-05-03 12:54:52 +0100176 /* We report MBEDTLS_SSL_CID_DISABLED in case the CID extensions
177 * were used, but client and server requested the empty CID.
178 * This is indistinguishable from not using the CID extension
179 * in the first place. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100180 if (ssl->transform_in->in_cid_len == 0 &&
181 ssl->transform_in->out_cid_len == 0) {
182 return 0;
Hanno Beckerb1f89cd2019-04-26 17:08:02 +0100183 }
184
Gilles Peskine449bd832023-01-11 14:50:10 +0100185 if (peer_cid_len != NULL) {
Hanno Becker615ef172019-05-22 16:50:35 +0100186 *peer_cid_len = ssl->transform_in->out_cid_len;
Gilles Peskine449bd832023-01-11 14:50:10 +0100187 if (peer_cid != NULL) {
188 memcpy(peer_cid, ssl->transform_in->out_cid,
189 ssl->transform_in->out_cid_len);
Hanno Becker615ef172019-05-22 16:50:35 +0100190 }
191 }
Hanno Beckerb1f89cd2019-04-26 17:08:02 +0100192
193 *enabled = MBEDTLS_SSL_CID_ENABLED;
194
Gilles Peskine449bd832023-01-11 14:50:10 +0100195 return 0;
Hanno Beckerf8542cf2019-04-09 15:22:03 +0100196}
Hanno Beckera0e20d02019-05-15 14:03:01 +0100197#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Beckerf8542cf2019-04-09 15:22:03 +0100198
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200199#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +0200200
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200201#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
Manuel Pégourié-Gonnard581e6b62013-07-18 12:32:27 +0200202/*
203 * Convert max_fragment_length codes to length.
204 * RFC 6066 says:
205 * enum{
206 * 2^9(1), 2^10(2), 2^11(3), 2^12(4), (255)
207 * } MaxFragmentLength;
208 * and we add 0 -> extension unused
209 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100210static unsigned int ssl_mfl_code_to_length(int mfl)
Manuel Pégourié-Gonnard581e6b62013-07-18 12:32:27 +0200211{
Gilles Peskine449bd832023-01-11 14:50:10 +0100212 switch (mfl) {
213 case MBEDTLS_SSL_MAX_FRAG_LEN_NONE:
214 return MBEDTLS_TLS_EXT_ADV_CONTENT_LEN;
215 case MBEDTLS_SSL_MAX_FRAG_LEN_512:
216 return 512;
217 case MBEDTLS_SSL_MAX_FRAG_LEN_1024:
218 return 1024;
219 case MBEDTLS_SSL_MAX_FRAG_LEN_2048:
220 return 2048;
221 case MBEDTLS_SSL_MAX_FRAG_LEN_4096:
222 return 4096;
223 default:
224 return MBEDTLS_TLS_EXT_ADV_CONTENT_LEN;
Angus Grattond8213d02016-05-25 20:56:48 +1000225 }
226}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200227#endif /* MBEDTLS_SSL_MAX_FRAGMENT_LENGTH */
Manuel Pégourié-Gonnard581e6b62013-07-18 12:32:27 +0200228
Gilles Peskine449bd832023-01-11 14:50:10 +0100229int mbedtls_ssl_session_copy(mbedtls_ssl_session *dst,
230 const mbedtls_ssl_session *src)
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200231{
Gilles Peskine449bd832023-01-11 14:50:10 +0100232 mbedtls_ssl_session_free(dst);
233 memcpy(dst, src, sizeof(mbedtls_ssl_session));
吴敬辉0b716112021-11-29 10:46:35 +0800234#if defined(MBEDTLS_SSL_SESSION_TICKETS) && defined(MBEDTLS_SSL_CLI_C)
235 dst->ticket = NULL;
Xiaokang Qian87306442022-10-12 09:47:38 +0000236#if defined(MBEDTLS_SSL_PROTO_TLS1_3) && \
237 defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
Xiaokang Qian126bf8e2022-10-13 02:22:40 +0000238 dst->hostname = NULL;
吴敬辉0b716112021-11-29 10:46:35 +0800239#endif
Xiaokang Qian87306442022-10-12 09:47:38 +0000240#endif /* MBEDTLS_SSL_SESSION_TICKETS && MBEDTLS_SSL_CLI_C */
吴敬辉0b716112021-11-29 10:46:35 +0800241
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200242#if defined(MBEDTLS_X509_CRT_PARSE_C)
Hanno Becker6d1986e2019-02-07 12:27:42 +0000243
244#if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
Gilles Peskine449bd832023-01-11 14:50:10 +0100245 if (src->peer_cert != NULL) {
Janos Follath865b3eb2019-12-16 11:46:15 +0000246 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker2292d1f2013-09-15 17:06:49 +0200247
Gilles Peskine449bd832023-01-11 14:50:10 +0100248 dst->peer_cert = mbedtls_calloc(1, sizeof(mbedtls_x509_crt));
249 if (dst->peer_cert == NULL) {
250 return MBEDTLS_ERR_SSL_ALLOC_FAILED;
251 }
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200252
Gilles Peskine449bd832023-01-11 14:50:10 +0100253 mbedtls_x509_crt_init(dst->peer_cert);
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200254
Gilles Peskine449bd832023-01-11 14:50:10 +0100255 if ((ret = mbedtls_x509_crt_parse_der(dst->peer_cert, src->peer_cert->raw.p,
256 src->peer_cert->raw.len)) != 0) {
257 mbedtls_free(dst->peer_cert);
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200258 dst->peer_cert = NULL;
Gilles Peskine449bd832023-01-11 14:50:10 +0100259 return ret;
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200260 }
261 }
Hanno Becker6d1986e2019-02-07 12:27:42 +0000262#else /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
Gilles Peskine449bd832023-01-11 14:50:10 +0100263 if (src->peer_cert_digest != NULL) {
Hanno Becker9198ad12019-02-05 17:00:50 +0000264 dst->peer_cert_digest =
Gilles Peskine449bd832023-01-11 14:50:10 +0100265 mbedtls_calloc(1, src->peer_cert_digest_len);
266 if (dst->peer_cert_digest == NULL) {
267 return MBEDTLS_ERR_SSL_ALLOC_FAILED;
268 }
Hanno Becker9198ad12019-02-05 17:00:50 +0000269
Gilles Peskine449bd832023-01-11 14:50:10 +0100270 memcpy(dst->peer_cert_digest, src->peer_cert_digest,
271 src->peer_cert_digest_len);
Hanno Becker9198ad12019-02-05 17:00:50 +0000272 dst->peer_cert_digest_type = src->peer_cert_digest_type;
Hanno Beckeraccc5992019-02-25 10:06:59 +0000273 dst->peer_cert_digest_len = src->peer_cert_digest_len;
Hanno Becker9198ad12019-02-05 17:00:50 +0000274 }
275#endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
276
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200277#endif /* MBEDTLS_X509_CRT_PARSE_C */
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200278
Manuel Pégourié-Gonnardb596abf2015-05-20 10:45:29 +0200279#if defined(MBEDTLS_SSL_SESSION_TICKETS) && defined(MBEDTLS_SSL_CLI_C)
Gilles Peskine449bd832023-01-11 14:50:10 +0100280 if (src->ticket != NULL) {
281 dst->ticket = mbedtls_calloc(1, src->ticket_len);
282 if (dst->ticket == NULL) {
283 return MBEDTLS_ERR_SSL_ALLOC_FAILED;
284 }
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200285
Gilles Peskine449bd832023-01-11 14:50:10 +0100286 memcpy(dst->ticket, src->ticket, src->ticket_len);
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200287 }
Xiaokang Qian126bf8e2022-10-13 02:22:40 +0000288
289#if defined(MBEDTLS_SSL_PROTO_TLS1_3) && \
290 defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
Gilles Peskine449bd832023-01-11 14:50:10 +0100291 if (src->endpoint == MBEDTLS_SSL_IS_CLIENT) {
Xiaokang Qian126bf8e2022-10-13 02:22:40 +0000292 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Gilles Peskine449bd832023-01-11 14:50:10 +0100293 ret = mbedtls_ssl_session_set_hostname(dst, src->hostname);
294 if (ret != 0) {
295 return ret;
296 }
Xiaokang Qian126bf8e2022-10-13 02:22:40 +0000297 }
298#endif /* MBEDTLS_SSL_PROTO_TLS1_3 &&
299 MBEDTLS_SSL_SERVER_NAME_INDICATION */
Manuel Pégourié-Gonnardb596abf2015-05-20 10:45:29 +0200300#endif /* MBEDTLS_SSL_SESSION_TICKETS && MBEDTLS_SSL_CLI_C */
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200301
Gilles Peskine449bd832023-01-11 14:50:10 +0100302 return 0;
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200303}
304
Andrzej Kurek0afa2a12020-03-03 10:39:58 -0500305#if defined(MBEDTLS_SSL_VARIABLE_BUFFER_LENGTH)
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +0200306MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +0100307static int resize_buffer(unsigned char **buffer, size_t len_new, size_t *len_old)
Andrzej Kurek0afa2a12020-03-03 10:39:58 -0500308{
Gilles Peskine449bd832023-01-11 14:50:10 +0100309 unsigned char *resized_buffer = mbedtls_calloc(1, len_new);
310 if (resized_buffer == NULL) {
Andrzej Kurek0afa2a12020-03-03 10:39:58 -0500311 return -1;
Gilles Peskine449bd832023-01-11 14:50:10 +0100312 }
Andrzej Kurek0afa2a12020-03-03 10:39:58 -0500313
314 /* We want to copy len_new bytes when downsizing the buffer, and
315 * len_old bytes when upsizing, so we choose the smaller of two sizes,
316 * to fit one buffer into another. Size checks, ensuring that no data is
317 * lost, are done outside of this function. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100318 memcpy(resized_buffer, *buffer,
319 (len_new < *len_old) ? len_new : *len_old);
320 mbedtls_platform_zeroize(*buffer, *len_old);
321 mbedtls_free(*buffer);
Andrzej Kurek0afa2a12020-03-03 10:39:58 -0500322
323 *buffer = resized_buffer;
324 *len_old = len_new;
325
326 return 0;
327}
Andrzej Kurek4a063792020-10-21 15:08:44 +0200328
Gilles Peskine449bd832023-01-11 14:50:10 +0100329static void handle_buffer_resizing(mbedtls_ssl_context *ssl, int downsizing,
330 size_t in_buf_new_len,
331 size_t out_buf_new_len)
Andrzej Kurek4a063792020-10-21 15:08:44 +0200332{
333 int modified = 0;
334 size_t written_in = 0, iv_offset_in = 0, len_offset_in = 0;
335 size_t written_out = 0, iv_offset_out = 0, len_offset_out = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +0100336 if (ssl->in_buf != NULL) {
Andrzej Kurek4a063792020-10-21 15:08:44 +0200337 written_in = ssl->in_msg - ssl->in_buf;
338 iv_offset_in = ssl->in_iv - ssl->in_buf;
339 len_offset_in = ssl->in_len - ssl->in_buf;
Gilles Peskine449bd832023-01-11 14:50:10 +0100340 if (downsizing ?
Andrzej Kurek4a063792020-10-21 15:08:44 +0200341 ssl->in_buf_len > in_buf_new_len && ssl->in_left < in_buf_new_len :
Gilles Peskine449bd832023-01-11 14:50:10 +0100342 ssl->in_buf_len < in_buf_new_len) {
343 if (resize_buffer(&ssl->in_buf, in_buf_new_len, &ssl->in_buf_len) != 0) {
344 MBEDTLS_SSL_DEBUG_MSG(1, ("input buffer resizing failed - out of memory"));
345 } else {
346 MBEDTLS_SSL_DEBUG_MSG(2, ("Reallocating in_buf to %" MBEDTLS_PRINTF_SIZET,
347 in_buf_new_len));
Andrzej Kurek4a063792020-10-21 15:08:44 +0200348 modified = 1;
349 }
350 }
351 }
352
Gilles Peskine449bd832023-01-11 14:50:10 +0100353 if (ssl->out_buf != NULL) {
Andrzej Kurek4a063792020-10-21 15:08:44 +0200354 written_out = ssl->out_msg - ssl->out_buf;
355 iv_offset_out = ssl->out_iv - ssl->out_buf;
356 len_offset_out = ssl->out_len - ssl->out_buf;
Gilles Peskine449bd832023-01-11 14:50:10 +0100357 if (downsizing ?
Andrzej Kurek4a063792020-10-21 15:08:44 +0200358 ssl->out_buf_len > out_buf_new_len && ssl->out_left < out_buf_new_len :
Gilles Peskine449bd832023-01-11 14:50:10 +0100359 ssl->out_buf_len < out_buf_new_len) {
360 if (resize_buffer(&ssl->out_buf, out_buf_new_len, &ssl->out_buf_len) != 0) {
361 MBEDTLS_SSL_DEBUG_MSG(1, ("output buffer resizing failed - out of memory"));
362 } else {
363 MBEDTLS_SSL_DEBUG_MSG(2, ("Reallocating out_buf to %" MBEDTLS_PRINTF_SIZET,
364 out_buf_new_len));
Andrzej Kurek4a063792020-10-21 15:08:44 +0200365 modified = 1;
366 }
367 }
368 }
Gilles Peskine449bd832023-01-11 14:50:10 +0100369 if (modified) {
Andrzej Kurek4a063792020-10-21 15:08:44 +0200370 /* Update pointers here to avoid doing it twice. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100371 mbedtls_ssl_reset_in_out_pointers(ssl);
Andrzej Kurek4a063792020-10-21 15:08:44 +0200372 /* Fields below might not be properly updated with record
373 * splitting or with CID, so they are manually updated here. */
374 ssl->out_msg = ssl->out_buf + written_out;
375 ssl->out_len = ssl->out_buf + len_offset_out;
376 ssl->out_iv = ssl->out_buf + iv_offset_out;
377
378 ssl->in_msg = ssl->in_buf + written_in;
379 ssl->in_len = ssl->in_buf + len_offset_in;
380 ssl->in_iv = ssl->in_buf + iv_offset_in;
381 }
382}
Andrzej Kurek0afa2a12020-03-03 10:39:58 -0500383#endif /* MBEDTLS_SSL_VARIABLE_BUFFER_LENGTH */
384
Jerry Yudb8c48a2022-01-27 14:54:54 +0800385#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
Jerry Yued14c932022-02-17 13:40:45 +0800386
387#if defined(MBEDTLS_SSL_CONTEXT_SERIALIZATION)
Gilles Peskine449bd832023-01-11 14:50:10 +0100388typedef int (*tls_prf_fn)(const unsigned char *secret, size_t slen,
389 const char *label,
390 const unsigned char *random, size_t rlen,
391 unsigned char *dstbuf, size_t dlen);
Jerry Yued14c932022-02-17 13:40:45 +0800392
Gilles Peskine449bd832023-01-11 14:50:10 +0100393static tls_prf_fn ssl_tls12prf_from_cs(int ciphersuite_id);
Jerry Yued14c932022-02-17 13:40:45 +0800394
395#endif /* MBEDTLS_SSL_CONTEXT_SERIALIZATION */
396
397/* Type for the TLS PRF */
398typedef int ssl_tls_prf_t(const unsigned char *, size_t, const char *,
399 const unsigned char *, size_t,
400 unsigned char *, size_t);
401
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +0200402MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +0100403static int ssl_tls12_populate_transform(mbedtls_ssl_transform *transform,
404 int ciphersuite,
405 const unsigned char master[48],
Neil Armstrongf2c82f02022-04-05 11:16:53 +0200406#if defined(MBEDTLS_SSL_SOME_SUITES_USE_CBC_ETM)
Gilles Peskine449bd832023-01-11 14:50:10 +0100407 int encrypt_then_mac,
Neil Armstrongf2c82f02022-04-05 11:16:53 +0200408#endif /* MBEDTLS_SSL_SOME_SUITES_USE_CBC_ETM */
Gilles Peskine449bd832023-01-11 14:50:10 +0100409 ssl_tls_prf_t tls_prf,
410 const unsigned char randbytes[64],
411 mbedtls_ssl_protocol_version tls_version,
412 unsigned endpoint,
413 const mbedtls_ssl_context *ssl);
Jerry Yued14c932022-02-17 13:40:45 +0800414
Andrzej Kurek25f27152022-08-17 16:09:31 -0400415#if defined(MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +0200416MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +0100417static int tls_prf_sha256(const unsigned char *secret, size_t slen,
Ron Eldor51d3ab52019-05-12 14:54:30 +0300418 const char *label,
419 const unsigned char *random, size_t rlen,
Gilles Peskine449bd832023-01-11 14:50:10 +0100420 unsigned char *dstbuf, size_t dlen);
Manuel Pégourié-Gonnard226aa152023-02-05 09:46:59 +0100421static int ssl_calc_verify_tls_sha256(const mbedtls_ssl_context *, unsigned char *, size_t *);
422static int ssl_calc_finished_tls_sha256(mbedtls_ssl_context *, unsigned char *, int);
Gilles Peskine449bd832023-01-11 14:50:10 +0100423
424#endif /* MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA*/
425
426#if defined(MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
427MBEDTLS_CHECK_RETURN_CRITICAL
428static int tls_prf_sha384(const unsigned char *secret, size_t slen,
429 const char *label,
430 const unsigned char *random, size_t rlen,
431 unsigned char *dstbuf, size_t dlen);
432
Manuel Pégourié-Gonnard226aa152023-02-05 09:46:59 +0100433static int ssl_calc_verify_tls_sha384(const mbedtls_ssl_context *, unsigned char *, size_t *);
434static int ssl_calc_finished_tls_sha384(mbedtls_ssl_context *, unsigned char *, int);
Gilles Peskine449bd832023-01-11 14:50:10 +0100435#endif /* MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA*/
436
437static size_t ssl_tls12_session_save(const mbedtls_ssl_session *session,
438 unsigned char *buf,
439 size_t buf_len);
440
441MBEDTLS_CHECK_RETURN_CRITICAL
442static int ssl_tls12_session_load(mbedtls_ssl_session *session,
443 const unsigned char *buf,
444 size_t len);
445#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
446
Manuel Pégourié-Gonnard226aa152023-02-05 09:46:59 +0100447static int ssl_update_checksum_start(mbedtls_ssl_context *, const unsigned char *, size_t);
Gilles Peskine449bd832023-01-11 14:50:10 +0100448
449#if defined(MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Manuel Pégourié-Gonnard226aa152023-02-05 09:46:59 +0100450static int ssl_update_checksum_sha256(mbedtls_ssl_context *, const unsigned char *, size_t);
Gilles Peskine449bd832023-01-11 14:50:10 +0100451#endif /* MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA*/
452
453#if defined(MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Manuel Pégourié-Gonnard226aa152023-02-05 09:46:59 +0100454static int ssl_update_checksum_sha384(mbedtls_ssl_context *, const unsigned char *, size_t);
Gilles Peskine449bd832023-01-11 14:50:10 +0100455#endif /* MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA*/
456
457int mbedtls_ssl_tls_prf(const mbedtls_tls_prf_types prf,
458 const unsigned char *secret, size_t slen,
459 const char *label,
460 const unsigned char *random, size_t rlen,
461 unsigned char *dstbuf, size_t dlen)
Ron Eldor51d3ab52019-05-12 14:54:30 +0300462{
463 mbedtls_ssl_tls_prf_cb *tls_prf = NULL;
464
Gilles Peskine449bd832023-01-11 14:50:10 +0100465 switch (prf) {
Ron Eldord2f25f72019-05-15 14:54:22 +0300466#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
Andrzej Kurek25f27152022-08-17 16:09:31 -0400467#if defined(MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Ron Eldor51d3ab52019-05-12 14:54:30 +0300468 case MBEDTLS_SSL_TLS_PRF_SHA384:
469 tls_prf = tls_prf_sha384;
Gilles Peskine449bd832023-01-11 14:50:10 +0100470 break;
Andrzej Kurekcccb0442022-08-19 03:42:11 -0400471#endif /* MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA*/
Andrzej Kurek25f27152022-08-17 16:09:31 -0400472#if defined(MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Ron Eldor51d3ab52019-05-12 14:54:30 +0300473 case MBEDTLS_SSL_TLS_PRF_SHA256:
474 tls_prf = tls_prf_sha256;
Gilles Peskine449bd832023-01-11 14:50:10 +0100475 break;
Andrzej Kurekcccb0442022-08-19 03:42:11 -0400476#endif /* MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA*/
Ron Eldord2f25f72019-05-15 14:54:22 +0300477#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100478 default:
479 return MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
Ron Eldor51d3ab52019-05-12 14:54:30 +0300480 }
481
Gilles Peskine449bd832023-01-11 14:50:10 +0100482 return tls_prf(secret, slen, label, random, rlen, dstbuf, dlen);
Ron Eldor51d3ab52019-05-12 14:54:30 +0300483}
484
Jerry Yuc73c6182022-02-08 20:29:25 +0800485#if defined(MBEDTLS_X509_CRT_PARSE_C)
Gilles Peskine449bd832023-01-11 14:50:10 +0100486static void ssl_clear_peer_cert(mbedtls_ssl_session *session)
Jerry Yuc73c6182022-02-08 20:29:25 +0800487{
488#if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
Gilles Peskine449bd832023-01-11 14:50:10 +0100489 if (session->peer_cert != NULL) {
490 mbedtls_x509_crt_free(session->peer_cert);
491 mbedtls_free(session->peer_cert);
Jerry Yuc73c6182022-02-08 20:29:25 +0800492 session->peer_cert = NULL;
493 }
494#else /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
Gilles Peskine449bd832023-01-11 14:50:10 +0100495 if (session->peer_cert_digest != NULL) {
Jerry Yuc73c6182022-02-08 20:29:25 +0800496 /* Zeroization is not necessary. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100497 mbedtls_free(session->peer_cert_digest);
Jerry Yuc73c6182022-02-08 20:29:25 +0800498 session->peer_cert_digest = NULL;
499 session->peer_cert_digest_type = MBEDTLS_MD_NONE;
500 session->peer_cert_digest_len = 0;
501 }
502#endif /* !MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
503}
504#endif /* MBEDTLS_X509_CRT_PARSE_C */
505
Gilles Peskine449bd832023-01-11 14:50:10 +0100506uint32_t mbedtls_ssl_get_extension_id(unsigned int extension_type)
Jerry Yu7a485c12022-10-31 13:08:18 +0800507{
Gilles Peskine449bd832023-01-11 14:50:10 +0100508 switch (extension_type) {
Jerry Yu7a485c12022-10-31 13:08:18 +0800509 case MBEDTLS_TLS_EXT_SERVERNAME:
Gilles Peskine449bd832023-01-11 14:50:10 +0100510 return MBEDTLS_SSL_EXT_ID_SERVERNAME;
Jerry Yu7a485c12022-10-31 13:08:18 +0800511
512 case MBEDTLS_TLS_EXT_MAX_FRAGMENT_LENGTH:
Gilles Peskine449bd832023-01-11 14:50:10 +0100513 return MBEDTLS_SSL_EXT_ID_MAX_FRAGMENT_LENGTH;
Jerry Yu7a485c12022-10-31 13:08:18 +0800514
515 case MBEDTLS_TLS_EXT_STATUS_REQUEST:
Gilles Peskine449bd832023-01-11 14:50:10 +0100516 return MBEDTLS_SSL_EXT_ID_STATUS_REQUEST;
Jerry Yu7a485c12022-10-31 13:08:18 +0800517
518 case MBEDTLS_TLS_EXT_SUPPORTED_GROUPS:
Gilles Peskine449bd832023-01-11 14:50:10 +0100519 return MBEDTLS_SSL_EXT_ID_SUPPORTED_GROUPS;
Jerry Yu7a485c12022-10-31 13:08:18 +0800520
521 case MBEDTLS_TLS_EXT_SIG_ALG:
Gilles Peskine449bd832023-01-11 14:50:10 +0100522 return MBEDTLS_SSL_EXT_ID_SIG_ALG;
Jerry Yu7a485c12022-10-31 13:08:18 +0800523
524 case MBEDTLS_TLS_EXT_USE_SRTP:
Gilles Peskine449bd832023-01-11 14:50:10 +0100525 return MBEDTLS_SSL_EXT_ID_USE_SRTP;
Jerry Yu7a485c12022-10-31 13:08:18 +0800526
527 case MBEDTLS_TLS_EXT_HEARTBEAT:
Gilles Peskine449bd832023-01-11 14:50:10 +0100528 return MBEDTLS_SSL_EXT_ID_HEARTBEAT;
Jerry Yu7a485c12022-10-31 13:08:18 +0800529
530 case MBEDTLS_TLS_EXT_ALPN:
Gilles Peskine449bd832023-01-11 14:50:10 +0100531 return MBEDTLS_SSL_EXT_ID_ALPN;
Jerry Yu7a485c12022-10-31 13:08:18 +0800532
533 case MBEDTLS_TLS_EXT_SCT:
Gilles Peskine449bd832023-01-11 14:50:10 +0100534 return MBEDTLS_SSL_EXT_ID_SCT;
Jerry Yu7a485c12022-10-31 13:08:18 +0800535
536 case MBEDTLS_TLS_EXT_CLI_CERT_TYPE:
Gilles Peskine449bd832023-01-11 14:50:10 +0100537 return MBEDTLS_SSL_EXT_ID_CLI_CERT_TYPE;
Jerry Yu7a485c12022-10-31 13:08:18 +0800538
539 case MBEDTLS_TLS_EXT_SERV_CERT_TYPE:
Gilles Peskine449bd832023-01-11 14:50:10 +0100540 return MBEDTLS_SSL_EXT_ID_SERV_CERT_TYPE;
Jerry Yu7a485c12022-10-31 13:08:18 +0800541
542 case MBEDTLS_TLS_EXT_PADDING:
Gilles Peskine449bd832023-01-11 14:50:10 +0100543 return MBEDTLS_SSL_EXT_ID_PADDING;
Jerry Yu7a485c12022-10-31 13:08:18 +0800544
545 case MBEDTLS_TLS_EXT_PRE_SHARED_KEY:
Gilles Peskine449bd832023-01-11 14:50:10 +0100546 return MBEDTLS_SSL_EXT_ID_PRE_SHARED_KEY;
Jerry Yu7a485c12022-10-31 13:08:18 +0800547
548 case MBEDTLS_TLS_EXT_EARLY_DATA:
Gilles Peskine449bd832023-01-11 14:50:10 +0100549 return MBEDTLS_SSL_EXT_ID_EARLY_DATA;
Jerry Yu7a485c12022-10-31 13:08:18 +0800550
551 case MBEDTLS_TLS_EXT_SUPPORTED_VERSIONS:
Gilles Peskine449bd832023-01-11 14:50:10 +0100552 return MBEDTLS_SSL_EXT_ID_SUPPORTED_VERSIONS;
Jerry Yu7a485c12022-10-31 13:08:18 +0800553
554 case MBEDTLS_TLS_EXT_COOKIE:
Gilles Peskine449bd832023-01-11 14:50:10 +0100555 return MBEDTLS_SSL_EXT_ID_COOKIE;
Jerry Yu7a485c12022-10-31 13:08:18 +0800556
557 case MBEDTLS_TLS_EXT_PSK_KEY_EXCHANGE_MODES:
Gilles Peskine449bd832023-01-11 14:50:10 +0100558 return MBEDTLS_SSL_EXT_ID_PSK_KEY_EXCHANGE_MODES;
Jerry Yu7a485c12022-10-31 13:08:18 +0800559
560 case MBEDTLS_TLS_EXT_CERT_AUTH:
Gilles Peskine449bd832023-01-11 14:50:10 +0100561 return MBEDTLS_SSL_EXT_ID_CERT_AUTH;
Jerry Yu7a485c12022-10-31 13:08:18 +0800562
563 case MBEDTLS_TLS_EXT_OID_FILTERS:
Gilles Peskine449bd832023-01-11 14:50:10 +0100564 return MBEDTLS_SSL_EXT_ID_OID_FILTERS;
Jerry Yu7a485c12022-10-31 13:08:18 +0800565
566 case MBEDTLS_TLS_EXT_POST_HANDSHAKE_AUTH:
Gilles Peskine449bd832023-01-11 14:50:10 +0100567 return MBEDTLS_SSL_EXT_ID_POST_HANDSHAKE_AUTH;
Jerry Yu7a485c12022-10-31 13:08:18 +0800568
569 case MBEDTLS_TLS_EXT_SIG_ALG_CERT:
Gilles Peskine449bd832023-01-11 14:50:10 +0100570 return MBEDTLS_SSL_EXT_ID_SIG_ALG_CERT;
Jerry Yu7a485c12022-10-31 13:08:18 +0800571
572 case MBEDTLS_TLS_EXT_KEY_SHARE:
Gilles Peskine449bd832023-01-11 14:50:10 +0100573 return MBEDTLS_SSL_EXT_ID_KEY_SHARE;
Jerry Yu7a485c12022-10-31 13:08:18 +0800574
575 case MBEDTLS_TLS_EXT_TRUNCATED_HMAC:
Gilles Peskine449bd832023-01-11 14:50:10 +0100576 return MBEDTLS_SSL_EXT_ID_TRUNCATED_HMAC;
Jerry Yu7a485c12022-10-31 13:08:18 +0800577
578 case MBEDTLS_TLS_EXT_SUPPORTED_POINT_FORMATS:
Gilles Peskine449bd832023-01-11 14:50:10 +0100579 return MBEDTLS_SSL_EXT_ID_SUPPORTED_POINT_FORMATS;
Jerry Yu7a485c12022-10-31 13:08:18 +0800580
581 case MBEDTLS_TLS_EXT_ENCRYPT_THEN_MAC:
Gilles Peskine449bd832023-01-11 14:50:10 +0100582 return MBEDTLS_SSL_EXT_ID_ENCRYPT_THEN_MAC;
Jerry Yu7a485c12022-10-31 13:08:18 +0800583
584 case MBEDTLS_TLS_EXT_EXTENDED_MASTER_SECRET:
Gilles Peskine449bd832023-01-11 14:50:10 +0100585 return MBEDTLS_SSL_EXT_ID_EXTENDED_MASTER_SECRET;
Jerry Yu7a485c12022-10-31 13:08:18 +0800586
587 case MBEDTLS_TLS_EXT_SESSION_TICKET:
Gilles Peskine449bd832023-01-11 14:50:10 +0100588 return MBEDTLS_SSL_EXT_ID_SESSION_TICKET;
Jerry Yu7a485c12022-10-31 13:08:18 +0800589
590 }
591
Gilles Peskine449bd832023-01-11 14:50:10 +0100592 return MBEDTLS_SSL_EXT_ID_UNRECOGNIZED;
Jerry Yu7a485c12022-10-31 13:08:18 +0800593}
594
Gilles Peskine449bd832023-01-11 14:50:10 +0100595uint32_t mbedtls_ssl_get_extension_mask(unsigned int extension_type)
Jerry Yu7a485c12022-10-31 13:08:18 +0800596{
Gilles Peskine449bd832023-01-11 14:50:10 +0100597 return 1 << mbedtls_ssl_get_extension_id(extension_type);
Jerry Yu7a485c12022-10-31 13:08:18 +0800598}
599
Jerry Yud25cab02022-10-31 12:48:30 +0800600#if defined(MBEDTLS_DEBUG_C)
601static const char *extension_name_table[] = {
Jerry Yuea52ed92022-11-08 21:01:17 +0800602 [MBEDTLS_SSL_EXT_ID_UNRECOGNIZED] = "unrecognized",
Jerry Yud25cab02022-10-31 12:48:30 +0800603 [MBEDTLS_SSL_EXT_ID_SERVERNAME] = "server_name",
604 [MBEDTLS_SSL_EXT_ID_MAX_FRAGMENT_LENGTH] = "max_fragment_length",
605 [MBEDTLS_SSL_EXT_ID_STATUS_REQUEST] = "status_request",
606 [MBEDTLS_SSL_EXT_ID_SUPPORTED_GROUPS] = "supported_groups",
607 [MBEDTLS_SSL_EXT_ID_SIG_ALG] = "signature_algorithms",
608 [MBEDTLS_SSL_EXT_ID_USE_SRTP] = "use_srtp",
609 [MBEDTLS_SSL_EXT_ID_HEARTBEAT] = "heartbeat",
610 [MBEDTLS_SSL_EXT_ID_ALPN] = "application_layer_protocol_negotiation",
611 [MBEDTLS_SSL_EXT_ID_SCT] = "signed_certificate_timestamp",
612 [MBEDTLS_SSL_EXT_ID_CLI_CERT_TYPE] = "client_certificate_type",
613 [MBEDTLS_SSL_EXT_ID_SERV_CERT_TYPE] = "server_certificate_type",
614 [MBEDTLS_SSL_EXT_ID_PADDING] = "padding",
615 [MBEDTLS_SSL_EXT_ID_PRE_SHARED_KEY] = "pre_shared_key",
616 [MBEDTLS_SSL_EXT_ID_EARLY_DATA] = "early_data",
617 [MBEDTLS_SSL_EXT_ID_SUPPORTED_VERSIONS] = "supported_versions",
618 [MBEDTLS_SSL_EXT_ID_COOKIE] = "cookie",
619 [MBEDTLS_SSL_EXT_ID_PSK_KEY_EXCHANGE_MODES] = "psk_key_exchange_modes",
620 [MBEDTLS_SSL_EXT_ID_CERT_AUTH] = "certificate_authorities",
621 [MBEDTLS_SSL_EXT_ID_OID_FILTERS] = "oid_filters",
622 [MBEDTLS_SSL_EXT_ID_POST_HANDSHAKE_AUTH] = "post_handshake_auth",
623 [MBEDTLS_SSL_EXT_ID_SIG_ALG_CERT] = "signature_algorithms_cert",
624 [MBEDTLS_SSL_EXT_ID_KEY_SHARE] = "key_share",
625 [MBEDTLS_SSL_EXT_ID_TRUNCATED_HMAC] = "truncated_hmac",
626 [MBEDTLS_SSL_EXT_ID_SUPPORTED_POINT_FORMATS] = "supported_point_formats",
627 [MBEDTLS_SSL_EXT_ID_ENCRYPT_THEN_MAC] = "encrypt_then_mac",
628 [MBEDTLS_SSL_EXT_ID_EXTENDED_MASTER_SECRET] = "extended_master_secret",
629 [MBEDTLS_SSL_EXT_ID_SESSION_TICKET] = "session_ticket"
630};
631
Gilles Peskine449bd832023-01-11 14:50:10 +0100632static unsigned int extension_type_table[] = {
Jerry Yud25cab02022-10-31 12:48:30 +0800633 [MBEDTLS_SSL_EXT_ID_UNRECOGNIZED] = 0xff,
634 [MBEDTLS_SSL_EXT_ID_SERVERNAME] = MBEDTLS_TLS_EXT_SERVERNAME,
635 [MBEDTLS_SSL_EXT_ID_MAX_FRAGMENT_LENGTH] = MBEDTLS_TLS_EXT_MAX_FRAGMENT_LENGTH,
636 [MBEDTLS_SSL_EXT_ID_STATUS_REQUEST] = MBEDTLS_TLS_EXT_STATUS_REQUEST,
637 [MBEDTLS_SSL_EXT_ID_SUPPORTED_GROUPS] = MBEDTLS_TLS_EXT_SUPPORTED_GROUPS,
638 [MBEDTLS_SSL_EXT_ID_SIG_ALG] = MBEDTLS_TLS_EXT_SIG_ALG,
639 [MBEDTLS_SSL_EXT_ID_USE_SRTP] = MBEDTLS_TLS_EXT_USE_SRTP,
640 [MBEDTLS_SSL_EXT_ID_HEARTBEAT] = MBEDTLS_TLS_EXT_HEARTBEAT,
641 [MBEDTLS_SSL_EXT_ID_ALPN] = MBEDTLS_TLS_EXT_ALPN,
642 [MBEDTLS_SSL_EXT_ID_SCT] = MBEDTLS_TLS_EXT_SCT,
643 [MBEDTLS_SSL_EXT_ID_CLI_CERT_TYPE] = MBEDTLS_TLS_EXT_CLI_CERT_TYPE,
644 [MBEDTLS_SSL_EXT_ID_SERV_CERT_TYPE] = MBEDTLS_TLS_EXT_SERV_CERT_TYPE,
645 [MBEDTLS_SSL_EXT_ID_PADDING] = MBEDTLS_TLS_EXT_PADDING,
646 [MBEDTLS_SSL_EXT_ID_PRE_SHARED_KEY] = MBEDTLS_TLS_EXT_PRE_SHARED_KEY,
647 [MBEDTLS_SSL_EXT_ID_EARLY_DATA] = MBEDTLS_TLS_EXT_EARLY_DATA,
648 [MBEDTLS_SSL_EXT_ID_SUPPORTED_VERSIONS] = MBEDTLS_TLS_EXT_SUPPORTED_VERSIONS,
649 [MBEDTLS_SSL_EXT_ID_COOKIE] = MBEDTLS_TLS_EXT_COOKIE,
650 [MBEDTLS_SSL_EXT_ID_PSK_KEY_EXCHANGE_MODES] = MBEDTLS_TLS_EXT_PSK_KEY_EXCHANGE_MODES,
651 [MBEDTLS_SSL_EXT_ID_CERT_AUTH] = MBEDTLS_TLS_EXT_CERT_AUTH,
652 [MBEDTLS_SSL_EXT_ID_OID_FILTERS] = MBEDTLS_TLS_EXT_OID_FILTERS,
653 [MBEDTLS_SSL_EXT_ID_POST_HANDSHAKE_AUTH] = MBEDTLS_TLS_EXT_POST_HANDSHAKE_AUTH,
654 [MBEDTLS_SSL_EXT_ID_SIG_ALG_CERT] = MBEDTLS_TLS_EXT_SIG_ALG_CERT,
655 [MBEDTLS_SSL_EXT_ID_KEY_SHARE] = MBEDTLS_TLS_EXT_KEY_SHARE,
656 [MBEDTLS_SSL_EXT_ID_TRUNCATED_HMAC] = MBEDTLS_TLS_EXT_TRUNCATED_HMAC,
657 [MBEDTLS_SSL_EXT_ID_SUPPORTED_POINT_FORMATS] = MBEDTLS_TLS_EXT_SUPPORTED_POINT_FORMATS,
658 [MBEDTLS_SSL_EXT_ID_ENCRYPT_THEN_MAC] = MBEDTLS_TLS_EXT_ENCRYPT_THEN_MAC,
659 [MBEDTLS_SSL_EXT_ID_EXTENDED_MASTER_SECRET] = MBEDTLS_TLS_EXT_EXTENDED_MASTER_SECRET,
660 [MBEDTLS_SSL_EXT_ID_SESSION_TICKET] = MBEDTLS_TLS_EXT_SESSION_TICKET
661};
662
Gilles Peskine449bd832023-01-11 14:50:10 +0100663const char *mbedtls_ssl_get_extension_name(unsigned int extension_type)
Jerry Yud25cab02022-10-31 12:48:30 +0800664{
Gilles Peskine449bd832023-01-11 14:50:10 +0100665 return extension_name_table[
666 mbedtls_ssl_get_extension_id(extension_type)];
Jerry Yud25cab02022-10-31 12:48:30 +0800667}
668
Gilles Peskine449bd832023-01-11 14:50:10 +0100669static const char *ssl_tls13_get_hs_msg_name(int hs_msg_type)
Jerry Yud25cab02022-10-31 12:48:30 +0800670{
Gilles Peskine449bd832023-01-11 14:50:10 +0100671 switch (hs_msg_type) {
Jerry Yud25cab02022-10-31 12:48:30 +0800672 case MBEDTLS_SSL_HS_CLIENT_HELLO:
Gilles Peskine449bd832023-01-11 14:50:10 +0100673 return "ClientHello";
Jerry Yud25cab02022-10-31 12:48:30 +0800674 case MBEDTLS_SSL_HS_SERVER_HELLO:
Gilles Peskine449bd832023-01-11 14:50:10 +0100675 return "ServerHello";
Jerry Yud25cab02022-10-31 12:48:30 +0800676 case MBEDTLS_SSL_TLS1_3_HS_HELLO_RETRY_REQUEST:
Gilles Peskine449bd832023-01-11 14:50:10 +0100677 return "HelloRetryRequest";
Jerry Yud25cab02022-10-31 12:48:30 +0800678 case MBEDTLS_SSL_HS_NEW_SESSION_TICKET:
Gilles Peskine449bd832023-01-11 14:50:10 +0100679 return "NewSessionTicket";
Jerry Yud25cab02022-10-31 12:48:30 +0800680 case MBEDTLS_SSL_HS_ENCRYPTED_EXTENSIONS:
Gilles Peskine449bd832023-01-11 14:50:10 +0100681 return "EncryptedExtensions";
Jerry Yud25cab02022-10-31 12:48:30 +0800682 case MBEDTLS_SSL_HS_CERTIFICATE:
Gilles Peskine449bd832023-01-11 14:50:10 +0100683 return "Certificate";
Jerry Yud25cab02022-10-31 12:48:30 +0800684 case MBEDTLS_SSL_HS_CERTIFICATE_REQUEST:
Gilles Peskine449bd832023-01-11 14:50:10 +0100685 return "CertificateRequest";
Jerry Yud25cab02022-10-31 12:48:30 +0800686 }
Gilles Peskine449bd832023-01-11 14:50:10 +0100687 return "Unknown";
Jerry Yud25cab02022-10-31 12:48:30 +0800688}
689
Gilles Peskine449bd832023-01-11 14:50:10 +0100690void mbedtls_ssl_print_extension(const mbedtls_ssl_context *ssl,
691 int level, const char *file, int line,
692 int hs_msg_type, unsigned int extension_type,
693 const char *extra_msg0, const char *extra_msg1)
Jerry Yud25cab02022-10-31 12:48:30 +0800694{
695 const char *extra_msg;
Gilles Peskine449bd832023-01-11 14:50:10 +0100696 if (extra_msg0 && extra_msg1) {
Jerry Yud25cab02022-10-31 12:48:30 +0800697 mbedtls_debug_print_msg(
698 ssl, level, file, line,
699 "%s: %s(%u) extension %s %s.",
Gilles Peskine449bd832023-01-11 14:50:10 +0100700 ssl_tls13_get_hs_msg_name(hs_msg_type),
701 mbedtls_ssl_get_extension_name(extension_type),
Jerry Yud25cab02022-10-31 12:48:30 +0800702 extension_type,
Gilles Peskine449bd832023-01-11 14:50:10 +0100703 extra_msg0, extra_msg1);
Jerry Yud25cab02022-10-31 12:48:30 +0800704 return;
705 }
706
707 extra_msg = extra_msg0 ? extra_msg0 : extra_msg1;
Gilles Peskine449bd832023-01-11 14:50:10 +0100708 if (extra_msg) {
Jerry Yud25cab02022-10-31 12:48:30 +0800709 mbedtls_debug_print_msg(
710 ssl, level, file, line,
Gilles Peskine449bd832023-01-11 14:50:10 +0100711 "%s: %s(%u) extension %s.", ssl_tls13_get_hs_msg_name(hs_msg_type),
712 mbedtls_ssl_get_extension_name(extension_type), extension_type,
713 extra_msg);
Jerry Yud25cab02022-10-31 12:48:30 +0800714 return;
715 }
716
717 mbedtls_debug_print_msg(
718 ssl, level, file, line,
Gilles Peskine449bd832023-01-11 14:50:10 +0100719 "%s: %s(%u) extension.", ssl_tls13_get_hs_msg_name(hs_msg_type),
720 mbedtls_ssl_get_extension_name(extension_type), extension_type);
Jerry Yud25cab02022-10-31 12:48:30 +0800721}
722
Gilles Peskine449bd832023-01-11 14:50:10 +0100723void mbedtls_ssl_print_extensions(const mbedtls_ssl_context *ssl,
724 int level, const char *file, int line,
725 int hs_msg_type, uint32_t extensions_mask,
726 const char *extra)
Jerry Yud25cab02022-10-31 12:48:30 +0800727{
728
Gilles Peskine449bd832023-01-11 14:50:10 +0100729 for (unsigned i = 0;
730 i < sizeof(extension_name_table) / sizeof(extension_name_table[0]);
731 i++) {
Jerry Yu79aa7212022-11-08 21:30:21 +0800732 mbedtls_ssl_print_extension(
Jerry Yuea52ed92022-11-08 21:01:17 +0800733 ssl, level, file, line, hs_msg_type, extension_type_table[i],
Gilles Peskine449bd832023-01-11 14:50:10 +0100734 extensions_mask & (1 << i) ? "exists" : "does not exist", extra);
Jerry Yud25cab02022-10-31 12:48:30 +0800735 }
736}
737
Pengyu Lvee455c02023-01-12 14:37:24 +0800738#if defined(MBEDTLS_SSL_PROTO_TLS1_3) && defined(MBEDTLS_SSL_SESSION_TICKETS)
739#define ARRAY_LENGTH(a) (sizeof(a) / sizeof(*(a)))
740
741static const char *ticket_flag_name_table[] =
742{
743 [0] = "ALLOW_PSK_RESUMPTION",
744 [2] = "ALLOW_PSK_EPHEMERAL_RESUMPTION",
745 [3] = "ALLOW_EARLY_DATA",
746};
747
Pengyu Lv4938a562023-01-16 11:28:49 +0800748void mbedtls_ssl_print_ticket_flags(const mbedtls_ssl_context *ssl,
749 int level, const char *file, int line,
750 unsigned int flags)
Pengyu Lvee455c02023-01-12 14:37:24 +0800751{
752 size_t i;
753
754 mbedtls_debug_print_msg(ssl, level, file, line,
Pengyu Lv4938a562023-01-16 11:28:49 +0800755 "print ticket_flags (0x%02x)", flags);
756
757 flags = flags & MBEDTLS_SSL_TLS1_3_TICKET_FLAGS_MASK;
Pengyu Lvee455c02023-01-12 14:37:24 +0800758
759 for (i = 0; i < ARRAY_LENGTH(ticket_flag_name_table); i++) {
Pengyu Lv4938a562023-01-16 11:28:49 +0800760 if ((flags & (1 << i))) {
Pengyu Lvee455c02023-01-12 14:37:24 +0800761 mbedtls_debug_print_msg(ssl, level, file, line, "- %s is set.",
762 ticket_flag_name_table[i]);
763 }
764 }
765}
766#endif /* MBEDTLS_SSL_PROTO_TLS1_3 && MBEDTLS_SSL_SESSION_TICKETS */
767
Jerry Yud25cab02022-10-31 12:48:30 +0800768#endif /* MBEDTLS_DEBUG_C */
769
Gilles Peskine449bd832023-01-11 14:50:10 +0100770void mbedtls_ssl_optimize_checksum(mbedtls_ssl_context *ssl,
771 const mbedtls_ssl_ciphersuite_t *ciphersuite_info)
Jerry Yuc73c6182022-02-08 20:29:25 +0800772{
773 ((void) ciphersuite_info);
774
Andrzej Kurek25f27152022-08-17 16:09:31 -0400775#if defined(MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Gilles Peskine449bd832023-01-11 14:50:10 +0100776 if (ciphersuite_info->mac == MBEDTLS_MD_SHA384) {
Jerry Yuc73c6182022-02-08 20:29:25 +0800777 ssl->handshake->update_checksum = ssl_update_checksum_sha384;
Gilles Peskine449bd832023-01-11 14:50:10 +0100778 } else
Jerry Yuc73c6182022-02-08 20:29:25 +0800779#endif
Andrzej Kurek25f27152022-08-17 16:09:31 -0400780#if defined(MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Gilles Peskine449bd832023-01-11 14:50:10 +0100781 if (ciphersuite_info->mac != MBEDTLS_MD_SHA384) {
Jerry Yuc73c6182022-02-08 20:29:25 +0800782 ssl->handshake->update_checksum = ssl_update_checksum_sha256;
Gilles Peskine449bd832023-01-11 14:50:10 +0100783 } else
Jerry Yuc73c6182022-02-08 20:29:25 +0800784#endif
785 {
Gilles Peskine449bd832023-01-11 14:50:10 +0100786 MBEDTLS_SSL_DEBUG_MSG(1, ("should never happen"));
Jerry Yuc73c6182022-02-08 20:29:25 +0800787 return;
788 }
789}
790
Manuel Pégourié-Gonnardb8b07aa2023-02-06 00:34:21 +0100791int mbedtls_ssl_add_hs_hdr_to_checksum(mbedtls_ssl_context *ssl,
Manuel Pégourié-Gonnard43cc1272023-02-06 11:48:19 +0100792 unsigned hs_type,
793 size_t total_hs_len)
Ronald Cron8f6d39a2022-03-10 18:56:50 +0100794{
795 unsigned char hs_hdr[4];
796
797 /* Build HS header for checksum update. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100798 hs_hdr[0] = MBEDTLS_BYTE_0(hs_type);
799 hs_hdr[1] = MBEDTLS_BYTE_2(total_hs_len);
800 hs_hdr[2] = MBEDTLS_BYTE_1(total_hs_len);
801 hs_hdr[3] = MBEDTLS_BYTE_0(total_hs_len);
Ronald Cron8f6d39a2022-03-10 18:56:50 +0100802
Manuel Pégourié-Gonnardb8b07aa2023-02-06 00:34:21 +0100803 return ssl->handshake->update_checksum(ssl, hs_hdr, sizeof(hs_hdr));
Ronald Cron8f6d39a2022-03-10 18:56:50 +0100804}
805
Manuel Pégourié-Gonnardb8b07aa2023-02-06 00:34:21 +0100806int mbedtls_ssl_add_hs_msg_to_checksum(mbedtls_ssl_context *ssl,
Manuel Pégourié-Gonnard43cc1272023-02-06 11:48:19 +0100807 unsigned hs_type,
808 unsigned char const *msg,
809 size_t msg_len)
Ronald Cron8f6d39a2022-03-10 18:56:50 +0100810{
Manuel Pégourié-Gonnardb8b07aa2023-02-06 00:34:21 +0100811 int ret;
812 ret = mbedtls_ssl_add_hs_hdr_to_checksum(ssl, hs_type, msg_len);
Manuel Pégourié-Gonnard43cc1272023-02-06 11:48:19 +0100813 if (ret != 0) {
Manuel Pégourié-Gonnardb8b07aa2023-02-06 00:34:21 +0100814 return ret;
Manuel Pégourié-Gonnard43cc1272023-02-06 11:48:19 +0100815 }
Manuel Pégourié-Gonnardb8b07aa2023-02-06 00:34:21 +0100816 return ssl->handshake->update_checksum(ssl, msg, msg_len);
Ronald Cron8f6d39a2022-03-10 18:56:50 +0100817}
818
Manuel Pégourié-Gonnard226aa152023-02-05 09:46:59 +0100819int mbedtls_ssl_reset_checksum(mbedtls_ssl_context *ssl)
Jerry Yuc73c6182022-02-08 20:29:25 +0800820{
Manuel Pégourié-Gonnard626aaed2023-02-06 22:03:06 +0100821#if defined(MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA) || \
822 defined(MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Manuel Pégourié-Gonnardb72ff492023-02-06 09:54:49 +0100823#if defined(MBEDTLS_USE_PSA_CRYPTO)
824 psa_status_t status;
825#else
826 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
827#endif
Manuel Pégourié-Gonnard626aaed2023-02-06 22:03:06 +0100828#else /* SHA-256 or SHA-384 */
Jerry Yuc73c6182022-02-08 20:29:25 +0800829 ((void) ssl);
Manuel Pégourié-Gonnard626aaed2023-02-06 22:03:06 +0100830#endif /* SHA-256 or SHA-384 */
Andrzej Kurek25f27152022-08-17 16:09:31 -0400831#if defined(MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Jerry Yuc73c6182022-02-08 20:29:25 +0800832#if defined(MBEDTLS_USE_PSA_CRYPTO)
Manuel Pégourié-Gonnardb72ff492023-02-06 09:54:49 +0100833 status = psa_hash_abort(&ssl->handshake->fin_sha256_psa);
834 if (status != PSA_SUCCESS) {
835 return mbedtls_md_error_from_psa(status);
836 }
837 status = psa_hash_setup(&ssl->handshake->fin_sha256_psa, PSA_ALG_SHA_256);
838 if (status != PSA_SUCCESS) {
839 return mbedtls_md_error_from_psa(status);
840 }
Jerry Yuc73c6182022-02-08 20:29:25 +0800841#else
Manuel Pégourié-Gonnardf057ecf2023-02-24 13:19:17 +0100842 ret = mbedtls_md_setup(&ssl->handshake->fin_sha256,
843 mbedtls_md_info_from_type(MBEDTLS_MD_SHA256),
844 0);
845 if (ret != 0) {
846 return ret;
847 }
848 ret = mbedtls_md_starts(&ssl->handshake->fin_sha256);
Manuel Pégourié-Gonnardb72ff492023-02-06 09:54:49 +0100849 if (ret != 0) {
850 return ret;
851 }
Jerry Yuc73c6182022-02-08 20:29:25 +0800852#endif
853#endif
Andrzej Kurek25f27152022-08-17 16:09:31 -0400854#if defined(MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Jerry Yuc73c6182022-02-08 20:29:25 +0800855#if defined(MBEDTLS_USE_PSA_CRYPTO)
Manuel Pégourié-Gonnardb72ff492023-02-06 09:54:49 +0100856 status = psa_hash_abort(&ssl->handshake->fin_sha384_psa);
857 if (status != PSA_SUCCESS) {
858 return mbedtls_md_error_from_psa(status);
859 }
860 status = psa_hash_setup(&ssl->handshake->fin_sha384_psa, PSA_ALG_SHA_384);
861 if (status != PSA_SUCCESS) {
862 return mbedtls_md_error_from_psa(status);
863 }
Jerry Yuc73c6182022-02-08 20:29:25 +0800864#else
Manuel Pégourié-Gonnardf057ecf2023-02-24 13:19:17 +0100865 ret = mbedtls_md_setup(&ssl->handshake->fin_sha384,
866 mbedtls_md_info_from_type(MBEDTLS_MD_SHA384), 0);
867 if (ret != 0) {
868 return ret;
869 }
870 ret = mbedtls_md_starts(&ssl->handshake->fin_sha384);
Manuel Pégourié-Gonnardb72ff492023-02-06 09:54:49 +0100871 if (ret != 0) {
872 return ret;
873 }
Jerry Yuc73c6182022-02-08 20:29:25 +0800874#endif
875#endif
Manuel Pégourié-Gonnard226aa152023-02-05 09:46:59 +0100876 return 0;
Jerry Yuc73c6182022-02-08 20:29:25 +0800877}
878
Manuel Pégourié-Gonnard226aa152023-02-05 09:46:59 +0100879static int ssl_update_checksum_start(mbedtls_ssl_context *ssl,
Manuel Pégourié-Gonnard43cc1272023-02-06 11:48:19 +0100880 const unsigned char *buf, size_t len)
Jerry Yuc73c6182022-02-08 20:29:25 +0800881{
Manuel Pégourié-Gonnard626aaed2023-02-06 22:03:06 +0100882#if defined(MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA) || \
883 defined(MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Manuel Pégourié-Gonnarddf949012023-02-06 10:00:52 +0100884#if defined(MBEDTLS_USE_PSA_CRYPTO)
885 psa_status_t status;
886#else
887 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
888#endif
Manuel Pégourié-Gonnard626aaed2023-02-06 22:03:06 +0100889#else /* SHA-256 or SHA-384 */
890 ((void) ssl);
891 (void) buf;
892 (void) len;
893#endif /* SHA-256 or SHA-384 */
Andrzej Kurek25f27152022-08-17 16:09:31 -0400894#if defined(MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Jerry Yuc73c6182022-02-08 20:29:25 +0800895#if defined(MBEDTLS_USE_PSA_CRYPTO)
Manuel Pégourié-Gonnarddf949012023-02-06 10:00:52 +0100896 status = psa_hash_update(&ssl->handshake->fin_sha256_psa, buf, len);
897 if (status != PSA_SUCCESS) {
898 return mbedtls_md_error_from_psa(status);
899 }
Jerry Yuc73c6182022-02-08 20:29:25 +0800900#else
Manuel Pégourié-Gonnardf057ecf2023-02-24 13:19:17 +0100901 ret = mbedtls_md_update(&ssl->handshake->fin_sha256, buf, len);
Manuel Pégourié-Gonnarddf949012023-02-06 10:00:52 +0100902 if (ret != 0) {
903 return ret;
904 }
Jerry Yuc73c6182022-02-08 20:29:25 +0800905#endif
906#endif
Andrzej Kurek25f27152022-08-17 16:09:31 -0400907#if defined(MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Jerry Yuc73c6182022-02-08 20:29:25 +0800908#if defined(MBEDTLS_USE_PSA_CRYPTO)
Manuel Pégourié-Gonnarddf949012023-02-06 10:00:52 +0100909 status = psa_hash_update(&ssl->handshake->fin_sha384_psa, buf, len);
910 if (status != PSA_SUCCESS) {
911 return mbedtls_md_error_from_psa(status);
912 }
Jerry Yuc73c6182022-02-08 20:29:25 +0800913#else
Manuel Pégourié-Gonnardf057ecf2023-02-24 13:19:17 +0100914 ret = mbedtls_md_update(&ssl->handshake->fin_sha384, buf, len);
Manuel Pégourié-Gonnarddf949012023-02-06 10:00:52 +0100915 if (ret != 0) {
916 return ret;
917 }
Jerry Yuc73c6182022-02-08 20:29:25 +0800918#endif
919#endif
Manuel Pégourié-Gonnard226aa152023-02-05 09:46:59 +0100920 return 0;
Jerry Yuc73c6182022-02-08 20:29:25 +0800921}
922
Andrzej Kurek25f27152022-08-17 16:09:31 -0400923#if defined(MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Manuel Pégourié-Gonnard226aa152023-02-05 09:46:59 +0100924static int ssl_update_checksum_sha256(mbedtls_ssl_context *ssl,
Manuel Pégourié-Gonnard43cc1272023-02-06 11:48:19 +0100925 const unsigned char *buf, size_t len)
Jerry Yuc73c6182022-02-08 20:29:25 +0800926{
927#if defined(MBEDTLS_USE_PSA_CRYPTO)
Manuel Pégourié-Gonnarddf949012023-02-06 10:00:52 +0100928 return mbedtls_md_error_from_psa(psa_hash_update(
Manuel Pégourié-Gonnard43cc1272023-02-06 11:48:19 +0100929 &ssl->handshake->fin_sha256_psa, buf, len));
Jerry Yuc73c6182022-02-08 20:29:25 +0800930#else
Manuel Pégourié-Gonnardf057ecf2023-02-24 13:19:17 +0100931 return mbedtls_md_update(&ssl->handshake->fin_sha256, buf, len);
Jerry Yuc73c6182022-02-08 20:29:25 +0800932#endif
933}
934#endif
935
Andrzej Kurek25f27152022-08-17 16:09:31 -0400936#if defined(MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Manuel Pégourié-Gonnard226aa152023-02-05 09:46:59 +0100937static int ssl_update_checksum_sha384(mbedtls_ssl_context *ssl,
Manuel Pégourié-Gonnard43cc1272023-02-06 11:48:19 +0100938 const unsigned char *buf, size_t len)
Jerry Yuc73c6182022-02-08 20:29:25 +0800939{
940#if defined(MBEDTLS_USE_PSA_CRYPTO)
Manuel Pégourié-Gonnarddf949012023-02-06 10:00:52 +0100941 return mbedtls_md_error_from_psa(psa_hash_update(
Manuel Pégourié-Gonnard43cc1272023-02-06 11:48:19 +0100942 &ssl->handshake->fin_sha384_psa, buf, len));
Jerry Yuc73c6182022-02-08 20:29:25 +0800943#else
Manuel Pégourié-Gonnardf057ecf2023-02-24 13:19:17 +0100944 return mbedtls_md_update(&ssl->handshake->fin_sha384, buf, len);
Jerry Yuc73c6182022-02-08 20:29:25 +0800945#endif
946}
947#endif
948
Gilles Peskine449bd832023-01-11 14:50:10 +0100949static void ssl_handshake_params_init(mbedtls_ssl_handshake_params *handshake)
Paul Bakkeraccaffe2014-06-26 13:37:14 +0200950{
Gilles Peskine449bd832023-01-11 14:50:10 +0100951 memset(handshake, 0, sizeof(mbedtls_ssl_handshake_params));
Paul Bakkeraccaffe2014-06-26 13:37:14 +0200952
Andrzej Kurek25f27152022-08-17 16:09:31 -0400953#if defined(MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Andrzej Kurekeb342242019-01-29 09:14:33 -0500954#if defined(MBEDTLS_USE_PSA_CRYPTO)
955 handshake->fin_sha256_psa = psa_hash_operation_init();
Andrzej Kurekeb342242019-01-29 09:14:33 -0500956#else
Manuel Pégourié-Gonnardf057ecf2023-02-24 13:19:17 +0100957 mbedtls_md_init(&handshake->fin_sha256);
Paul Bakkeraccaffe2014-06-26 13:37:14 +0200958#endif
Andrzej Kurekeb342242019-01-29 09:14:33 -0500959#endif
Andrzej Kurek25f27152022-08-17 16:09:31 -0400960#if defined(MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Andrzej Kurekeb342242019-01-29 09:14:33 -0500961#if defined(MBEDTLS_USE_PSA_CRYPTO)
Andrzej Kurek972fba52019-01-30 03:29:12 -0500962 handshake->fin_sha384_psa = psa_hash_operation_init();
Andrzej Kurekeb342242019-01-29 09:14:33 -0500963#else
Manuel Pégourié-Gonnardf057ecf2023-02-24 13:19:17 +0100964 mbedtls_md_init(&handshake->fin_sha384);
Paul Bakkeraccaffe2014-06-26 13:37:14 +0200965#endif
Andrzej Kurekeb342242019-01-29 09:14:33 -0500966#endif
Paul Bakkeraccaffe2014-06-26 13:37:14 +0200967
968 handshake->update_checksum = ssl_update_checksum_start;
Hanno Becker7e5437a2017-04-28 17:15:26 +0100969
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200970#if defined(MBEDTLS_DHM_C)
Gilles Peskine449bd832023-01-11 14:50:10 +0100971 mbedtls_dhm_init(&handshake->dhm_ctx);
Paul Bakkeraccaffe2014-06-26 13:37:14 +0200972#endif
Neil Armstrongf3f46412022-04-12 14:43:39 +0200973#if !defined(MBEDTLS_USE_PSA_CRYPTO) && defined(MBEDTLS_ECDH_C)
Gilles Peskine449bd832023-01-11 14:50:10 +0100974 mbedtls_ecdh_init(&handshake->ecdh_ctx);
Paul Bakkeraccaffe2014-06-26 13:37:14 +0200975#endif
Manuel Pégourié-Gonnardeef142d2015-09-16 10:05:04 +0200976#if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
Neil Armstrongca7d5062022-05-31 14:43:23 +0200977#if defined(MBEDTLS_USE_PSA_CRYPTO)
978 handshake->psa_pake_ctx = psa_pake_operation_init();
979 handshake->psa_pake_password = MBEDTLS_SVC_KEY_ID_INIT;
980#else
Gilles Peskine449bd832023-01-11 14:50:10 +0100981 mbedtls_ecjpake_init(&handshake->ecjpake_ctx);
Neil Armstrongca7d5062022-05-31 14:43:23 +0200982#endif /* MBEDTLS_USE_PSA_CRYPTO */
Manuel Pégourié-Gonnard77c06462015-09-17 13:59:49 +0200983#if defined(MBEDTLS_SSL_CLI_C)
984 handshake->ecjpake_cache = NULL;
985 handshake->ecjpake_cache_len = 0;
986#endif
Manuel Pégourié-Gonnard76cfd3f2015-09-15 12:10:54 +0200987#endif
Manuel Pégourié-Gonnardcdc26ae2015-06-19 12:16:31 +0200988
Gilles Peskineeccd8882020-03-10 12:19:08 +0100989#if defined(MBEDTLS_SSL_ECP_RESTARTABLE_ENABLED)
Gilles Peskine449bd832023-01-11 14:50:10 +0100990 mbedtls_x509_crt_restart_init(&handshake->ecrs_ctx);
Manuel Pégourié-Gonnard862cde52017-05-17 11:56:15 +0200991#endif
992
Manuel Pégourié-Gonnardcdc26ae2015-06-19 12:16:31 +0200993#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
994 handshake->sni_authmode = MBEDTLS_SSL_VERIFY_UNSET;
995#endif
Hanno Becker75173122019-02-06 16:18:31 +0000996
997#if defined(MBEDTLS_X509_CRT_PARSE_C) && \
998 !defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
Gilles Peskine449bd832023-01-11 14:50:10 +0100999 mbedtls_pk_init(&handshake->peer_pubkey);
Hanno Becker75173122019-02-06 16:18:31 +00001000#endif
Paul Bakkeraccaffe2014-06-26 13:37:14 +02001001}
1002
Gilles Peskine449bd832023-01-11 14:50:10 +01001003void mbedtls_ssl_transform_init(mbedtls_ssl_transform *transform)
Paul Bakkeraccaffe2014-06-26 13:37:14 +02001004{
Gilles Peskine449bd832023-01-11 14:50:10 +01001005 memset(transform, 0, sizeof(mbedtls_ssl_transform));
Paul Bakker84bbeb52014-07-01 14:53:22 +02001006
Przemyslaw Stekiel8f80fb92022-01-11 08:28:13 +01001007#if defined(MBEDTLS_USE_PSA_CRYPTO)
1008 transform->psa_key_enc = MBEDTLS_SVC_KEY_ID_INIT;
1009 transform->psa_key_dec = MBEDTLS_SVC_KEY_ID_INIT;
Przemyslaw Stekiel6be9cf52022-01-19 16:00:22 +01001010#else
Gilles Peskine449bd832023-01-11 14:50:10 +01001011 mbedtls_cipher_init(&transform->cipher_ctx_enc);
1012 mbedtls_cipher_init(&transform->cipher_ctx_dec);
Przemyslaw Stekiel8f80fb92022-01-11 08:28:13 +01001013#endif
1014
Hanno Beckerfd86ca82020-11-30 08:54:23 +00001015#if defined(MBEDTLS_SSL_SOME_SUITES_USE_MAC)
Neil Armstrong39b8e7d2022-02-23 09:24:45 +01001016#if defined(MBEDTLS_USE_PSA_CRYPTO)
1017 transform->psa_mac_enc = MBEDTLS_SVC_KEY_ID_INIT;
1018 transform->psa_mac_dec = MBEDTLS_SVC_KEY_ID_INIT;
Neil Armstrongcf8841a2022-02-24 11:17:45 +01001019#else
Gilles Peskine449bd832023-01-11 14:50:10 +01001020 mbedtls_md_init(&transform->md_ctx_enc);
1021 mbedtls_md_init(&transform->md_ctx_dec);
Hanno Beckerd56ed242018-01-03 15:32:51 +00001022#endif
Neil Armstrongcf8841a2022-02-24 11:17:45 +01001023#endif
Paul Bakkeraccaffe2014-06-26 13:37:14 +02001024}
1025
Gilles Peskine449bd832023-01-11 14:50:10 +01001026void mbedtls_ssl_session_init(mbedtls_ssl_session *session)
Paul Bakkeraccaffe2014-06-26 13:37:14 +02001027{
Gilles Peskine449bd832023-01-11 14:50:10 +01001028 memset(session, 0, sizeof(mbedtls_ssl_session));
Paul Bakkeraccaffe2014-06-26 13:37:14 +02001029}
1030
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02001031MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01001032static int ssl_handshake_init(mbedtls_ssl_context *ssl)
Paul Bakker48916f92012-09-16 19:57:18 +00001033{
Manuel Pégourié-Gonnardb8b07aa2023-02-06 00:34:21 +01001034 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
1035
Paul Bakkeraccaffe2014-06-26 13:37:14 +02001036 /* Clear old handshake information if present */
Jerry Yu2e199812022-12-01 18:57:19 +08001037#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
Gilles Peskine449bd832023-01-11 14:50:10 +01001038 if (ssl->transform_negotiate) {
1039 mbedtls_ssl_transform_free(ssl->transform_negotiate);
1040 }
Jerry Yu2e199812022-12-01 18:57:19 +08001041#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001042 if (ssl->session_negotiate) {
1043 mbedtls_ssl_session_free(ssl->session_negotiate);
1044 }
1045 if (ssl->handshake) {
1046 mbedtls_ssl_handshake_free(ssl);
1047 }
Paul Bakkeraccaffe2014-06-26 13:37:14 +02001048
Jerry Yu2e199812022-12-01 18:57:19 +08001049#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
Paul Bakkeraccaffe2014-06-26 13:37:14 +02001050 /*
1051 * Either the pointers are now NULL or cleared properly and can be freed.
1052 * Now allocate missing structures.
1053 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001054 if (ssl->transform_negotiate == NULL) {
1055 ssl->transform_negotiate = mbedtls_calloc(1, sizeof(mbedtls_ssl_transform));
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02001056 }
Jerry Yu2e199812022-12-01 18:57:19 +08001057#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakker48916f92012-09-16 19:57:18 +00001058
Gilles Peskine449bd832023-01-11 14:50:10 +01001059 if (ssl->session_negotiate == NULL) {
1060 ssl->session_negotiate = mbedtls_calloc(1, sizeof(mbedtls_ssl_session));
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02001061 }
Paul Bakker48916f92012-09-16 19:57:18 +00001062
Gilles Peskine449bd832023-01-11 14:50:10 +01001063 if (ssl->handshake == NULL) {
1064 ssl->handshake = mbedtls_calloc(1, sizeof(mbedtls_ssl_handshake_params));
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02001065 }
Andrzej Kurek0afa2a12020-03-03 10:39:58 -05001066#if defined(MBEDTLS_SSL_VARIABLE_BUFFER_LENGTH)
1067 /* If the buffers are too small - reallocate */
Andrzej Kurek8ea68722020-04-03 06:40:47 -04001068
Gilles Peskine449bd832023-01-11 14:50:10 +01001069 handle_buffer_resizing(ssl, 0, MBEDTLS_SSL_IN_BUFFER_LEN,
1070 MBEDTLS_SSL_OUT_BUFFER_LEN);
Andrzej Kurek0afa2a12020-03-03 10:39:58 -05001071#endif
Paul Bakker48916f92012-09-16 19:57:18 +00001072
Paul Bakkeraccaffe2014-06-26 13:37:14 +02001073 /* All pointers should exist and can be directly freed without issue */
Gilles Peskine449bd832023-01-11 14:50:10 +01001074 if (ssl->handshake == NULL ||
Jerry Yu2e199812022-12-01 18:57:19 +08001075#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
Paul Bakker48916f92012-09-16 19:57:18 +00001076 ssl->transform_negotiate == NULL ||
Jerry Yu2e199812022-12-01 18:57:19 +08001077#endif
Gilles Peskine449bd832023-01-11 14:50:10 +01001078 ssl->session_negotiate == NULL) {
1079 MBEDTLS_SSL_DEBUG_MSG(1, ("alloc() of ssl sub-contexts failed"));
Paul Bakkeraccaffe2014-06-26 13:37:14 +02001080
Gilles Peskine449bd832023-01-11 14:50:10 +01001081 mbedtls_free(ssl->handshake);
Paul Bakkeraccaffe2014-06-26 13:37:14 +02001082 ssl->handshake = NULL;
Jerry Yu2e199812022-12-01 18:57:19 +08001083
1084#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
Gilles Peskine449bd832023-01-11 14:50:10 +01001085 mbedtls_free(ssl->transform_negotiate);
Paul Bakkeraccaffe2014-06-26 13:37:14 +02001086 ssl->transform_negotiate = NULL;
Jerry Yu2e199812022-12-01 18:57:19 +08001087#endif
1088
Gilles Peskine449bd832023-01-11 14:50:10 +01001089 mbedtls_free(ssl->session_negotiate);
Paul Bakkeraccaffe2014-06-26 13:37:14 +02001090 ssl->session_negotiate = NULL;
1091
Gilles Peskine449bd832023-01-11 14:50:10 +01001092 return MBEDTLS_ERR_SSL_ALLOC_FAILED;
Paul Bakker48916f92012-09-16 19:57:18 +00001093 }
1094
Paul Bakkeraccaffe2014-06-26 13:37:14 +02001095 /* Initialize structures */
Gilles Peskine449bd832023-01-11 14:50:10 +01001096 mbedtls_ssl_session_init(ssl->session_negotiate);
1097 ssl_handshake_params_init(ssl->handshake);
Paul Bakker968afaa2014-07-09 11:09:24 +02001098
Jerry Yu2e199812022-12-01 18:57:19 +08001099#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
Gilles Peskine449bd832023-01-11 14:50:10 +01001100 mbedtls_ssl_transform_init(ssl->transform_negotiate);
Jerry Yu2e199812022-12-01 18:57:19 +08001101#endif
1102
Manuel Pégourié-Gonnard537f2312023-02-05 10:17:45 +01001103 /* Setup handshake checksums */
Manuel Pégourié-Gonnardb8b07aa2023-02-06 00:34:21 +01001104 ret = mbedtls_ssl_reset_checksum(ssl);
1105 if (ret != 0) {
1106 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ssl_reset_checksum", ret);
1107 return ret;
1108 }
Manuel Pégourié-Gonnard537f2312023-02-05 10:17:45 +01001109
Jerry Yud0766ec2022-09-22 10:46:57 +08001110#if defined(MBEDTLS_SSL_PROTO_TLS1_3) && \
1111 defined(MBEDTLS_SSL_SRV_C) && \
1112 defined(MBEDTLS_SSL_SESSION_TICKETS)
1113 ssl->handshake->new_session_tickets_count =
Gilles Peskine449bd832023-01-11 14:50:10 +01001114 ssl->conf->new_session_tickets_count;
Jerry Yud0766ec2022-09-22 10:46:57 +08001115#endif
1116
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001117#if defined(MBEDTLS_SSL_PROTO_DTLS)
Gilles Peskine449bd832023-01-11 14:50:10 +01001118 if (ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM) {
Manuel Pégourié-Gonnard06939ce2015-05-11 11:25:46 +02001119 ssl->handshake->alt_transform_out = ssl->transform_out;
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02001120
Gilles Peskine449bd832023-01-11 14:50:10 +01001121 if (ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT) {
Manuel Pégourié-Gonnard06939ce2015-05-11 11:25:46 +02001122 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_PREPARING;
Gilles Peskine449bd832023-01-11 14:50:10 +01001123 } else {
Manuel Pégourié-Gonnard06939ce2015-05-11 11:25:46 +02001124 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_WAITING;
Gilles Peskine449bd832023-01-11 14:50:10 +01001125 }
Manuel Pégourié-Gonnard286a1362015-05-13 16:22:05 +02001126
Gilles Peskine449bd832023-01-11 14:50:10 +01001127 mbedtls_ssl_set_timer(ssl, 0);
Manuel Pégourié-Gonnard06939ce2015-05-11 11:25:46 +02001128 }
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02001129#endif
1130
Brett Warrene0edc842021-08-17 09:53:13 +01001131/*
1132 * curve_list is translated to IANA TLS group identifiers here because
1133 * mbedtls_ssl_conf_curves returns void and so can't return
1134 * any error codes.
1135 */
1136#if defined(MBEDTLS_ECP_C)
1137#if !defined(MBEDTLS_DEPRECATED_REMOVED)
1138 /* Heap allocate and translate curve_list from internal to IANA group ids */
Gilles Peskine449bd832023-01-11 14:50:10 +01001139 if (ssl->conf->curve_list != NULL) {
Brett Warrene0edc842021-08-17 09:53:13 +01001140 size_t length;
1141 const mbedtls_ecp_group_id *curve_list = ssl->conf->curve_list;
1142
Gilles Peskine449bd832023-01-11 14:50:10 +01001143 for (length = 0; (curve_list[length] != MBEDTLS_ECP_DP_NONE) &&
1144 (length < MBEDTLS_ECP_DP_MAX); length++) {
1145 }
Brett Warrene0edc842021-08-17 09:53:13 +01001146
1147 /* Leave room for zero termination */
Gilles Peskine449bd832023-01-11 14:50:10 +01001148 uint16_t *group_list = mbedtls_calloc(length + 1, sizeof(uint16_t));
1149 if (group_list == NULL) {
1150 return MBEDTLS_ERR_SSL_ALLOC_FAILED;
1151 }
Brett Warrene0edc842021-08-17 09:53:13 +01001152
Gilles Peskine449bd832023-01-11 14:50:10 +01001153 for (size_t i = 0; i < length; i++) {
Valerio Setti18c9fed2022-12-30 17:44:24 +01001154 uint16_t tls_id = mbedtls_ssl_get_tls_id_from_ecp_group_id(
Gilles Peskine449bd832023-01-11 14:50:10 +01001155 curve_list[i]);
1156 if (tls_id == 0) {
1157 mbedtls_free(group_list);
1158 return MBEDTLS_ERR_SSL_BAD_CONFIG;
Brett Warrene0edc842021-08-17 09:53:13 +01001159 }
Valerio Setti18c9fed2022-12-30 17:44:24 +01001160 group_list[i] = tls_id;
Brett Warrene0edc842021-08-17 09:53:13 +01001161 }
1162
1163 group_list[length] = 0;
1164
1165 ssl->handshake->group_list = group_list;
1166 ssl->handshake->group_list_heap_allocated = 1;
Gilles Peskine449bd832023-01-11 14:50:10 +01001167 } else {
Brett Warrene0edc842021-08-17 09:53:13 +01001168 ssl->handshake->group_list = ssl->conf->group_list;
1169 ssl->handshake->group_list_heap_allocated = 0;
1170 }
1171#endif /* MBEDTLS_DEPRECATED_REMOVED */
1172#endif /* MBEDTLS_ECP_C */
1173
Ronald Crone68ab4f2022-10-05 12:46:29 +02001174#if defined(MBEDTLS_SSL_HANDSHAKE_WITH_CERT_ENABLED)
Jerry Yuf017ee42022-01-12 15:49:48 +08001175#if !defined(MBEDTLS_DEPRECATED_REMOVED)
Jerry Yua69269a2022-01-17 21:06:01 +08001176#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
Jerry Yu713013f2022-01-17 18:16:35 +08001177 /* Heap allocate and translate sig_hashes from internal hash identifiers to
1178 signature algorithms IANA identifiers. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001179 if (mbedtls_ssl_conf_is_tls12_only(ssl->conf) &&
1180 ssl->conf->sig_hashes != NULL) {
Jerry Yuf017ee42022-01-12 15:49:48 +08001181 const int *md;
1182 const int *sig_hashes = ssl->conf->sig_hashes;
Jerry Yub476a442022-01-21 18:14:45 +08001183 size_t sig_algs_len = 0;
Jerry Yuf017ee42022-01-12 15:49:48 +08001184 uint16_t *p;
1185
Jerry Yub476a442022-01-21 18:14:45 +08001186#if defined(static_assert)
Gilles Peskine449bd832023-01-11 14:50:10 +01001187 static_assert(MBEDTLS_SSL_MAX_SIG_ALG_LIST_LEN
1188 <= (SIZE_MAX - (2 * sizeof(uint16_t))),
1189 "MBEDTLS_SSL_MAX_SIG_ALG_LIST_LEN too big");
Jerry Yua68dca22022-01-20 16:28:27 +08001190#endif
1191
Gilles Peskine449bd832023-01-11 14:50:10 +01001192 for (md = sig_hashes; *md != MBEDTLS_MD_NONE; md++) {
1193 if (mbedtls_ssl_hash_from_md_alg(*md) == MBEDTLS_SSL_HASH_NONE) {
Jerry Yuf017ee42022-01-12 15:49:48 +08001194 continue;
Gilles Peskine449bd832023-01-11 14:50:10 +01001195 }
Jerry Yub476a442022-01-21 18:14:45 +08001196#if defined(MBEDTLS_ECDSA_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01001197 sig_algs_len += sizeof(uint16_t);
Jerry Yub476a442022-01-21 18:14:45 +08001198#endif
Jerry Yua68dca22022-01-20 16:28:27 +08001199
Jerry Yub476a442022-01-21 18:14:45 +08001200#if defined(MBEDTLS_RSA_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01001201 sig_algs_len += sizeof(uint16_t);
Jerry Yub476a442022-01-21 18:14:45 +08001202#endif
Gilles Peskine449bd832023-01-11 14:50:10 +01001203 if (sig_algs_len > MBEDTLS_SSL_MAX_SIG_ALG_LIST_LEN) {
1204 return MBEDTLS_ERR_SSL_BAD_CONFIG;
1205 }
Jerry Yuf017ee42022-01-12 15:49:48 +08001206 }
1207
Gilles Peskine449bd832023-01-11 14:50:10 +01001208 if (sig_algs_len < MBEDTLS_SSL_MIN_SIG_ALG_LIST_LEN) {
1209 return MBEDTLS_ERR_SSL_BAD_CONFIG;
1210 }
Jerry Yuf017ee42022-01-12 15:49:48 +08001211
Gilles Peskine449bd832023-01-11 14:50:10 +01001212 ssl->handshake->sig_algs = mbedtls_calloc(1, sig_algs_len +
1213 sizeof(uint16_t));
1214 if (ssl->handshake->sig_algs == NULL) {
1215 return MBEDTLS_ERR_SSL_ALLOC_FAILED;
1216 }
Jerry Yuf017ee42022-01-12 15:49:48 +08001217
Gilles Peskine449bd832023-01-11 14:50:10 +01001218 p = (uint16_t *) ssl->handshake->sig_algs;
1219 for (md = sig_hashes; *md != MBEDTLS_MD_NONE; md++) {
1220 unsigned char hash = mbedtls_ssl_hash_from_md_alg(*md);
1221 if (hash == MBEDTLS_SSL_HASH_NONE) {
Jerry Yuf017ee42022-01-12 15:49:48 +08001222 continue;
Gilles Peskine449bd832023-01-11 14:50:10 +01001223 }
Jerry Yu6106fdc2022-01-12 16:36:14 +08001224#if defined(MBEDTLS_ECDSA_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01001225 *p = ((hash << 8) | MBEDTLS_SSL_SIG_ECDSA);
Jerry Yuf017ee42022-01-12 15:49:48 +08001226 p++;
Jerry Yu6106fdc2022-01-12 16:36:14 +08001227#endif
1228#if defined(MBEDTLS_RSA_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01001229 *p = ((hash << 8) | MBEDTLS_SSL_SIG_RSA);
Jerry Yuf017ee42022-01-12 15:49:48 +08001230 p++;
Jerry Yu6106fdc2022-01-12 16:36:14 +08001231#endif
Jerry Yuf017ee42022-01-12 15:49:48 +08001232 }
Gabor Mezei15b95a62022-05-09 16:37:58 +02001233 *p = MBEDTLS_TLS_SIG_NONE;
Jerry Yuf017ee42022-01-12 15:49:48 +08001234 ssl->handshake->sig_algs_heap_allocated = 1;
Gilles Peskine449bd832023-01-11 14:50:10 +01001235 } else
Jerry Yua69269a2022-01-17 21:06:01 +08001236#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Jerry Yuf017ee42022-01-12 15:49:48 +08001237 {
Jerry Yuf017ee42022-01-12 15:49:48 +08001238 ssl->handshake->sig_algs_heap_allocated = 0;
1239 }
Jerry Yucc539102022-06-27 16:27:35 +08001240#endif /* !MBEDTLS_DEPRECATED_REMOVED */
Ronald Crone68ab4f2022-10-05 12:46:29 +02001241#endif /* MBEDTLS_SSL_HANDSHAKE_WITH_CERT_ENABLED */
Gilles Peskine449bd832023-01-11 14:50:10 +01001242 return 0;
Paul Bakker48916f92012-09-16 19:57:18 +00001243}
1244
Manuel Pégourié-Gonnarde057d3b2015-05-20 10:59:43 +02001245#if defined(MBEDTLS_SSL_DTLS_HELLO_VERIFY) && defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard7d38d212014-07-23 17:52:09 +02001246/* Dummy cookie callbacks for defaults */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02001247MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01001248static int ssl_cookie_write_dummy(void *ctx,
1249 unsigned char **p, unsigned char *end,
1250 const unsigned char *cli_id, size_t cli_id_len)
Manuel Pégourié-Gonnard7d38d212014-07-23 17:52:09 +02001251{
1252 ((void) ctx);
1253 ((void) p);
1254 ((void) end);
1255 ((void) cli_id);
1256 ((void) cli_id_len);
1257
Gilles Peskine449bd832023-01-11 14:50:10 +01001258 return MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
Manuel Pégourié-Gonnard7d38d212014-07-23 17:52:09 +02001259}
1260
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02001261MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01001262static int ssl_cookie_check_dummy(void *ctx,
1263 const unsigned char *cookie, size_t cookie_len,
1264 const unsigned char *cli_id, size_t cli_id_len)
Manuel Pégourié-Gonnard7d38d212014-07-23 17:52:09 +02001265{
1266 ((void) ctx);
1267 ((void) cookie);
1268 ((void) cookie_len);
1269 ((void) cli_id);
1270 ((void) cli_id_len);
1271
Gilles Peskine449bd832023-01-11 14:50:10 +01001272 return MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
Manuel Pégourié-Gonnard7d38d212014-07-23 17:52:09 +02001273}
Manuel Pégourié-Gonnarde057d3b2015-05-20 10:59:43 +02001274#endif /* MBEDTLS_SSL_DTLS_HELLO_VERIFY && MBEDTLS_SSL_SRV_C */
Manuel Pégourié-Gonnard7d38d212014-07-23 17:52:09 +02001275
Paul Bakker5121ce52009-01-03 21:22:43 +00001276/*
1277 * Initialize an SSL context
1278 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001279void mbedtls_ssl_init(mbedtls_ssl_context *ssl)
Manuel Pégourié-Gonnard41d479e2015-04-29 00:48:22 +02001280{
Gilles Peskine449bd832023-01-11 14:50:10 +01001281 memset(ssl, 0, sizeof(mbedtls_ssl_context));
Manuel Pégourié-Gonnard41d479e2015-04-29 00:48:22 +02001282}
1283
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02001284MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01001285static int ssl_conf_version_check(const mbedtls_ssl_context *ssl)
Jerry Yu60835a82021-08-04 10:13:52 +08001286{
Ronald Cron086ee0b2022-03-15 15:18:51 +01001287 const mbedtls_ssl_config *conf = ssl->conf;
1288
Ronald Cron6f135e12021-12-08 16:57:54 +01001289#if defined(MBEDTLS_SSL_PROTO_TLS1_3)
Gilles Peskine449bd832023-01-11 14:50:10 +01001290 if (mbedtls_ssl_conf_is_tls13_only(conf)) {
1291 if (conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM) {
1292 MBEDTLS_SSL_DEBUG_MSG(1, ("DTLS 1.3 is not yet supported."));
1293 return MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
XiaokangQianed582dd2022-04-13 08:21:05 +00001294 }
1295
Gilles Peskine449bd832023-01-11 14:50:10 +01001296 MBEDTLS_SSL_DEBUG_MSG(4, ("The SSL configuration is tls13 only."));
1297 return 0;
Jerry Yu60835a82021-08-04 10:13:52 +08001298 }
1299#endif
1300
1301#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
Gilles Peskine449bd832023-01-11 14:50:10 +01001302 if (mbedtls_ssl_conf_is_tls12_only(conf)) {
1303 MBEDTLS_SSL_DEBUG_MSG(4, ("The SSL configuration is tls12 only."));
1304 return 0;
Jerry Yu60835a82021-08-04 10:13:52 +08001305 }
1306#endif
1307
Ronald Cron6f135e12021-12-08 16:57:54 +01001308#if defined(MBEDTLS_SSL_PROTO_TLS1_2) && defined(MBEDTLS_SSL_PROTO_TLS1_3)
Gilles Peskine449bd832023-01-11 14:50:10 +01001309 if (mbedtls_ssl_conf_is_hybrid_tls12_tls13(conf)) {
1310 if (ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM) {
1311 MBEDTLS_SSL_DEBUG_MSG(1, ("DTLS not yet supported in Hybrid TLS 1.3 + TLS 1.2"));
1312 return MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
XiaokangQianed582dd2022-04-13 08:21:05 +00001313 }
1314
Gilles Peskine449bd832023-01-11 14:50:10 +01001315 if (conf->endpoint == MBEDTLS_SSL_IS_SERVER) {
1316 MBEDTLS_SSL_DEBUG_MSG(1, ("TLS 1.3 server is not supported yet."));
1317 return MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
XiaokangQian8f9dfe42022-04-15 02:52:39 +00001318 }
1319
1320
Gilles Peskine449bd832023-01-11 14:50:10 +01001321 MBEDTLS_SSL_DEBUG_MSG(4, ("The SSL configuration is TLS 1.3 or TLS 1.2."));
1322 return 0;
Jerry Yu60835a82021-08-04 10:13:52 +08001323 }
1324#endif
1325
Gilles Peskine449bd832023-01-11 14:50:10 +01001326 MBEDTLS_SSL_DEBUG_MSG(1, ("The SSL configuration is invalid."));
1327 return MBEDTLS_ERR_SSL_BAD_CONFIG;
Jerry Yu60835a82021-08-04 10:13:52 +08001328}
1329
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02001330MBEDTLS_CHECK_RETURN_CRITICAL
Jerry Yu60835a82021-08-04 10:13:52 +08001331static int ssl_conf_check(const mbedtls_ssl_context *ssl)
1332{
1333 int ret;
Gilles Peskine449bd832023-01-11 14:50:10 +01001334 ret = ssl_conf_version_check(ssl);
1335 if (ret != 0) {
1336 return ret;
1337 }
Jerry Yu60835a82021-08-04 10:13:52 +08001338
Jerry Yudef7ae42022-10-30 14:13:19 +08001339#if defined(MBEDTLS_SSL_PROTO_TLS1_3)
1340 /* RFC 8446 section 4.4.3
1341 *
1342 * If the verification fails, the receiver MUST terminate the handshake with
1343 * a "decrypt_error" alert.
1344 *
1345 * If the client is configured as TLS 1.3 only with optional verify, return
1346 * bad config.
1347 *
1348 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001349 if (mbedtls_ssl_conf_tls13_ephemeral_enabled(
1350 (mbedtls_ssl_context *) ssl) &&
Jerry Yudef7ae42022-10-30 14:13:19 +08001351 ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT &&
1352 ssl->conf->max_tls_version == MBEDTLS_SSL_VERSION_TLS1_3 &&
1353 ssl->conf->min_tls_version == MBEDTLS_SSL_VERSION_TLS1_3 &&
Gilles Peskine449bd832023-01-11 14:50:10 +01001354 ssl->conf->authmode == MBEDTLS_SSL_VERIFY_OPTIONAL) {
Jerry Yudef7ae42022-10-30 14:13:19 +08001355 MBEDTLS_SSL_DEBUG_MSG(
Gilles Peskine449bd832023-01-11 14:50:10 +01001356 1, ("Optional verify auth mode "
1357 "is not available for TLS 1.3 client"));
1358 return MBEDTLS_ERR_SSL_BAD_CONFIG;
Jerry Yudef7ae42022-10-30 14:13:19 +08001359 }
1360#endif /* MBEDTLS_SSL_PROTO_TLS1_3 */
1361
Jerry Yu60835a82021-08-04 10:13:52 +08001362 /* Space for further checks */
1363
Gilles Peskine449bd832023-01-11 14:50:10 +01001364 return 0;
Jerry Yu60835a82021-08-04 10:13:52 +08001365}
1366
Manuel Pégourié-Gonnard41d479e2015-04-29 00:48:22 +02001367/*
1368 * Setup an SSL context
1369 */
Hanno Becker2a43f6f2018-08-10 11:12:52 +01001370
Gilles Peskine449bd832023-01-11 14:50:10 +01001371int mbedtls_ssl_setup(mbedtls_ssl_context *ssl,
1372 const mbedtls_ssl_config *conf)
Paul Bakker5121ce52009-01-03 21:22:43 +00001373{
Janos Follath865b3eb2019-12-16 11:46:15 +00001374 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Darryl Greenb33cc762019-11-28 14:29:44 +00001375 size_t in_buf_len = MBEDTLS_SSL_IN_BUFFER_LEN;
1376 size_t out_buf_len = MBEDTLS_SSL_OUT_BUFFER_LEN;
Paul Bakker5121ce52009-01-03 21:22:43 +00001377
Manuel Pégourié-Gonnarddef0bbe2015-05-04 14:56:36 +02001378 ssl->conf = conf;
Paul Bakker62f2dee2012-09-28 07:31:51 +00001379
Gilles Peskine449bd832023-01-11 14:50:10 +01001380 if ((ret = ssl_conf_check(ssl)) != 0) {
1381 return ret;
1382 }
Jerry Yu60835a82021-08-04 10:13:52 +08001383
Paul Bakker62f2dee2012-09-28 07:31:51 +00001384 /*
Manuel Pégourié-Gonnard06193482014-02-14 08:39:32 +01001385 * Prepare base structures
Paul Bakker62f2dee2012-09-28 07:31:51 +00001386 */
k-stachowiakc9a5f022018-07-24 13:53:31 +02001387
1388 /* Set to NULL in case of an error condition */
1389 ssl->out_buf = NULL;
k-stachowiaka47911c2018-07-04 17:41:58 +02001390
Darryl Greenb33cc762019-11-28 14:29:44 +00001391#if defined(MBEDTLS_SSL_VARIABLE_BUFFER_LENGTH)
1392 ssl->in_buf_len = in_buf_len;
1393#endif
Gilles Peskine449bd832023-01-11 14:50:10 +01001394 ssl->in_buf = mbedtls_calloc(1, in_buf_len);
1395 if (ssl->in_buf == NULL) {
1396 MBEDTLS_SSL_DEBUG_MSG(1, ("alloc(%" MBEDTLS_PRINTF_SIZET " bytes) failed", in_buf_len));
k-stachowiak9f7798e2018-07-31 16:52:32 +02001397 ret = MBEDTLS_ERR_SSL_ALLOC_FAILED;
k-stachowiaka47911c2018-07-04 17:41:58 +02001398 goto error;
Angus Grattond8213d02016-05-25 20:56:48 +10001399 }
1400
Darryl Greenb33cc762019-11-28 14:29:44 +00001401#if defined(MBEDTLS_SSL_VARIABLE_BUFFER_LENGTH)
1402 ssl->out_buf_len = out_buf_len;
1403#endif
Gilles Peskine449bd832023-01-11 14:50:10 +01001404 ssl->out_buf = mbedtls_calloc(1, out_buf_len);
1405 if (ssl->out_buf == NULL) {
1406 MBEDTLS_SSL_DEBUG_MSG(1, ("alloc(%" MBEDTLS_PRINTF_SIZET " bytes) failed", out_buf_len));
k-stachowiak9f7798e2018-07-31 16:52:32 +02001407 ret = MBEDTLS_ERR_SSL_ALLOC_FAILED;
k-stachowiaka47911c2018-07-04 17:41:58 +02001408 goto error;
Paul Bakker5121ce52009-01-03 21:22:43 +00001409 }
1410
Gilles Peskine449bd832023-01-11 14:50:10 +01001411 mbedtls_ssl_reset_in_out_pointers(ssl);
Manuel Pégourié-Gonnard419d5ae2015-05-04 19:32:36 +02001412
Johan Pascalb62bb512015-12-03 21:56:45 +01001413#if defined(MBEDTLS_SSL_DTLS_SRTP)
Gilles Peskine449bd832023-01-11 14:50:10 +01001414 memset(&ssl->dtls_srtp_info, 0, sizeof(ssl->dtls_srtp_info));
Johan Pascalb62bb512015-12-03 21:56:45 +01001415#endif
1416
Gilles Peskine449bd832023-01-11 14:50:10 +01001417 if ((ret = ssl_handshake_init(ssl)) != 0) {
k-stachowiaka47911c2018-07-04 17:41:58 +02001418 goto error;
Gilles Peskine449bd832023-01-11 14:50:10 +01001419 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001420
Gilles Peskine449bd832023-01-11 14:50:10 +01001421 return 0;
k-stachowiaka47911c2018-07-04 17:41:58 +02001422
1423error:
Gilles Peskine449bd832023-01-11 14:50:10 +01001424 mbedtls_free(ssl->in_buf);
1425 mbedtls_free(ssl->out_buf);
k-stachowiaka47911c2018-07-04 17:41:58 +02001426
1427 ssl->conf = NULL;
1428
Darryl Greenb33cc762019-11-28 14:29:44 +00001429#if defined(MBEDTLS_SSL_VARIABLE_BUFFER_LENGTH)
1430 ssl->in_buf_len = 0;
1431 ssl->out_buf_len = 0;
1432#endif
k-stachowiaka47911c2018-07-04 17:41:58 +02001433 ssl->in_buf = NULL;
1434 ssl->out_buf = NULL;
1435
1436 ssl->in_hdr = NULL;
1437 ssl->in_ctr = NULL;
1438 ssl->in_len = NULL;
1439 ssl->in_iv = NULL;
1440 ssl->in_msg = NULL;
1441
1442 ssl->out_hdr = NULL;
1443 ssl->out_ctr = NULL;
1444 ssl->out_len = NULL;
1445 ssl->out_iv = NULL;
1446 ssl->out_msg = NULL;
1447
Gilles Peskine449bd832023-01-11 14:50:10 +01001448 return ret;
Paul Bakker5121ce52009-01-03 21:22:43 +00001449}
1450
1451/*
Paul Bakker7eb013f2011-10-06 12:37:39 +00001452 * Reset an initialized and used SSL context for re-use while retaining
1453 * all application-set variables, function pointers and data.
Manuel Pégourié-Gonnard3f09b6d2015-09-08 11:58:14 +02001454 *
1455 * If partial is non-zero, keep data in the input buffer and client ID.
1456 * (Use when a DTLS client reconnects from the same port.)
Paul Bakker7eb013f2011-10-06 12:37:39 +00001457 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001458void mbedtls_ssl_session_reset_msg_layer(mbedtls_ssl_context *ssl,
1459 int partial)
Paul Bakker7eb013f2011-10-06 12:37:39 +00001460{
Darryl Greenb33cc762019-11-28 14:29:44 +00001461#if defined(MBEDTLS_SSL_VARIABLE_BUFFER_LENGTH)
1462 size_t in_buf_len = ssl->in_buf_len;
1463 size_t out_buf_len = ssl->out_buf_len;
1464#else
1465 size_t in_buf_len = MBEDTLS_SSL_IN_BUFFER_LEN;
1466 size_t out_buf_len = MBEDTLS_SSL_OUT_BUFFER_LEN;
1467#endif
Paul Bakker48916f92012-09-16 19:57:18 +00001468
Hanno Beckerb0302c42021-08-03 09:39:42 +01001469#if !defined(MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE) || !defined(MBEDTLS_SSL_SRV_C)
1470 partial = 0;
Hanno Becker7e772132018-08-10 12:38:21 +01001471#endif
1472
Manuel Pégourié-Gonnard286a1362015-05-13 16:22:05 +02001473 /* Cancel any possibly running timer */
Gilles Peskine449bd832023-01-11 14:50:10 +01001474 mbedtls_ssl_set_timer(ssl, 0);
Manuel Pégourié-Gonnard286a1362015-05-13 16:22:05 +02001475
Gilles Peskine449bd832023-01-11 14:50:10 +01001476 mbedtls_ssl_reset_in_out_pointers(ssl);
Hanno Beckerb0302c42021-08-03 09:39:42 +01001477
1478 /* Reset incoming message parsing */
1479 ssl->in_offt = NULL;
1480 ssl->nb_zero = 0;
1481 ssl->in_msgtype = 0;
1482 ssl->in_msglen = 0;
1483 ssl->in_hslen = 0;
1484 ssl->keep_current_message = 0;
1485 ssl->transform_in = NULL;
1486
1487#if defined(MBEDTLS_SSL_PROTO_DTLS)
1488 ssl->next_record_offset = 0;
1489 ssl->in_epoch = 0;
1490#endif
1491
1492 /* Keep current datagram if partial == 1 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001493 if (partial == 0) {
Hanno Beckerb0302c42021-08-03 09:39:42 +01001494 ssl->in_left = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +01001495 memset(ssl->in_buf, 0, in_buf_len);
Hanno Beckerb0302c42021-08-03 09:39:42 +01001496 }
1497
Ronald Cronad8c17b2022-06-10 17:18:09 +02001498 ssl->send_alert = 0;
1499
Hanno Beckerb0302c42021-08-03 09:39:42 +01001500 /* Reset outgoing message writing */
1501 ssl->out_msgtype = 0;
1502 ssl->out_msglen = 0;
1503 ssl->out_left = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +01001504 memset(ssl->out_buf, 0, out_buf_len);
1505 memset(ssl->cur_out_ctr, 0, sizeof(ssl->cur_out_ctr));
Hanno Beckerb0302c42021-08-03 09:39:42 +01001506 ssl->transform_out = NULL;
1507
1508#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
Gilles Peskine449bd832023-01-11 14:50:10 +01001509 mbedtls_ssl_dtls_replay_reset(ssl);
Hanno Beckerb0302c42021-08-03 09:39:42 +01001510#endif
1511
XiaokangQian2b01dc32022-01-21 02:53:13 +00001512#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
Gilles Peskine449bd832023-01-11 14:50:10 +01001513 if (ssl->transform) {
1514 mbedtls_ssl_transform_free(ssl->transform);
1515 mbedtls_free(ssl->transform);
Hanno Beckerb0302c42021-08-03 09:39:42 +01001516 ssl->transform = NULL;
1517 }
XiaokangQian2b01dc32022-01-21 02:53:13 +00001518#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
1519
XiaokangQian2b01dc32022-01-21 02:53:13 +00001520#if defined(MBEDTLS_SSL_PROTO_TLS1_3)
Gilles Peskine449bd832023-01-11 14:50:10 +01001521 mbedtls_ssl_transform_free(ssl->transform_application);
1522 mbedtls_free(ssl->transform_application);
XiaokangQian2b01dc32022-01-21 02:53:13 +00001523 ssl->transform_application = NULL;
1524
Gilles Peskine449bd832023-01-11 14:50:10 +01001525 if (ssl->handshake != NULL) {
Jerry Yu3d9b5902022-11-04 14:07:25 +08001526#if defined(MBEDTLS_SSL_EARLY_DATA)
Gilles Peskine449bd832023-01-11 14:50:10 +01001527 mbedtls_ssl_transform_free(ssl->handshake->transform_earlydata);
1528 mbedtls_free(ssl->handshake->transform_earlydata);
XiaokangQian2b01dc32022-01-21 02:53:13 +00001529 ssl->handshake->transform_earlydata = NULL;
Jerry Yu3d9b5902022-11-04 14:07:25 +08001530#endif
XiaokangQian2b01dc32022-01-21 02:53:13 +00001531
Gilles Peskine449bd832023-01-11 14:50:10 +01001532 mbedtls_ssl_transform_free(ssl->handshake->transform_handshake);
1533 mbedtls_free(ssl->handshake->transform_handshake);
XiaokangQian2b01dc32022-01-21 02:53:13 +00001534 ssl->handshake->transform_handshake = NULL;
1535 }
1536
XiaokangQian2b01dc32022-01-21 02:53:13 +00001537#endif /* MBEDTLS_SSL_PROTO_TLS1_3 */
Hanno Beckerb0302c42021-08-03 09:39:42 +01001538}
1539
Gilles Peskine449bd832023-01-11 14:50:10 +01001540int mbedtls_ssl_session_reset_int(mbedtls_ssl_context *ssl, int partial)
Hanno Beckerb0302c42021-08-03 09:39:42 +01001541{
1542 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
1543
1544 ssl->state = MBEDTLS_SSL_HELLO_REQUEST;
1545
Gilles Peskine449bd832023-01-11 14:50:10 +01001546 mbedtls_ssl_session_reset_msg_layer(ssl, partial);
Hanno Beckerb0302c42021-08-03 09:39:42 +01001547
1548 /* Reset renegotiation state */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001549#if defined(MBEDTLS_SSL_RENEGOTIATION)
1550 ssl->renego_status = MBEDTLS_SSL_INITIAL_HANDSHAKE;
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01001551 ssl->renego_records_seen = 0;
Paul Bakker48916f92012-09-16 19:57:18 +00001552
1553 ssl->verify_data_len = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +01001554 memset(ssl->own_verify_data, 0, MBEDTLS_SSL_VERIFY_DATA_MAX_LEN);
1555 memset(ssl->peer_verify_data, 0, MBEDTLS_SSL_VERIFY_DATA_MAX_LEN);
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01001556#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001557 ssl->secure_renegotiation = MBEDTLS_SSL_LEGACY_RENEGOTIATION;
Paul Bakker48916f92012-09-16 19:57:18 +00001558
Hanno Beckerb0302c42021-08-03 09:39:42 +01001559 ssl->session_in = NULL;
Hanno Becker78640902018-08-13 16:35:15 +01001560 ssl->session_out = NULL;
Gilles Peskine449bd832023-01-11 14:50:10 +01001561 if (ssl->session) {
1562 mbedtls_ssl_session_free(ssl->session);
1563 mbedtls_free(ssl->session);
Paul Bakkerc0463502013-02-14 11:19:38 +01001564 ssl->session = NULL;
1565 }
1566
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001567#if defined(MBEDTLS_SSL_ALPN)
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02001568 ssl->alpn_chosen = NULL;
1569#endif
1570
Manuel Pégourié-Gonnarde057d3b2015-05-20 10:59:43 +02001571#if defined(MBEDTLS_SSL_DTLS_HELLO_VERIFY) && defined(MBEDTLS_SSL_SRV_C)
David Horstmann3b2276a2022-10-06 14:49:08 +01001572 int free_cli_id = 1;
Hanno Becker4ccbf062018-08-10 11:20:38 +01001573#if defined(MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE)
Gilles Peskine449bd832023-01-11 14:50:10 +01001574 free_cli_id = (partial == 0);
Hanno Becker4ccbf062018-08-10 11:20:38 +01001575#endif
Gilles Peskine449bd832023-01-11 14:50:10 +01001576 if (free_cli_id) {
1577 mbedtls_free(ssl->cli_id);
Manuel Pégourié-Gonnard3f09b6d2015-09-08 11:58:14 +02001578 ssl->cli_id = NULL;
1579 ssl->cli_id_len = 0;
1580 }
Manuel Pégourié-Gonnard43c02182014-07-22 17:32:01 +02001581#endif
1582
Gilles Peskine449bd832023-01-11 14:50:10 +01001583 if ((ret = ssl_handshake_init(ssl)) != 0) {
1584 return ret;
1585 }
Paul Bakker2770fbd2012-07-03 13:30:23 +00001586
Gilles Peskine449bd832023-01-11 14:50:10 +01001587 return 0;
Paul Bakker7eb013f2011-10-06 12:37:39 +00001588}
1589
Manuel Pégourié-Gonnard779e4292013-08-03 13:50:48 +02001590/*
Manuel Pégourié-Gonnard3f09b6d2015-09-08 11:58:14 +02001591 * Reset an initialized and used SSL context for re-use while retaining
1592 * all application-set variables, function pointers and data.
1593 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001594int mbedtls_ssl_session_reset(mbedtls_ssl_context *ssl)
Manuel Pégourié-Gonnard3f09b6d2015-09-08 11:58:14 +02001595{
Gilles Peskine449bd832023-01-11 14:50:10 +01001596 return mbedtls_ssl_session_reset_int(ssl, 0);
Manuel Pégourié-Gonnard3f09b6d2015-09-08 11:58:14 +02001597}
1598
1599/*
Paul Bakker5121ce52009-01-03 21:22:43 +00001600 * SSL set accessors
1601 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001602void mbedtls_ssl_conf_endpoint(mbedtls_ssl_config *conf, int endpoint)
Paul Bakker5121ce52009-01-03 21:22:43 +00001603{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02001604 conf->endpoint = endpoint;
Paul Bakker5121ce52009-01-03 21:22:43 +00001605}
1606
Gilles Peskine449bd832023-01-11 14:50:10 +01001607void mbedtls_ssl_conf_transport(mbedtls_ssl_config *conf, int transport)
Manuel Pégourié-Gonnard0b1ff292014-02-06 13:04:16 +01001608{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02001609 conf->transport = transport;
Manuel Pégourié-Gonnard0b1ff292014-02-06 13:04:16 +01001610}
1611
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001612#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
Gilles Peskine449bd832023-01-11 14:50:10 +01001613void mbedtls_ssl_conf_dtls_anti_replay(mbedtls_ssl_config *conf, char mode)
Manuel Pégourié-Gonnard27393132014-09-24 14:41:11 +02001614{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02001615 conf->anti_replay = mode;
Manuel Pégourié-Gonnard27393132014-09-24 14:41:11 +02001616}
1617#endif
1618
Gilles Peskine449bd832023-01-11 14:50:10 +01001619void mbedtls_ssl_conf_dtls_badmac_limit(mbedtls_ssl_config *conf, unsigned limit)
Manuel Pégourié-Gonnardb0643d12014-10-14 18:30:36 +02001620{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02001621 conf->badmac_limit = limit;
Manuel Pégourié-Gonnardb0643d12014-10-14 18:30:36 +02001622}
Manuel Pégourié-Gonnardb0643d12014-10-14 18:30:36 +02001623
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001624#if defined(MBEDTLS_SSL_PROTO_DTLS)
Hanno Becker04da1892018-08-14 13:22:10 +01001625
Gilles Peskine449bd832023-01-11 14:50:10 +01001626void mbedtls_ssl_set_datagram_packing(mbedtls_ssl_context *ssl,
1627 unsigned allow_packing)
Hanno Becker04da1892018-08-14 13:22:10 +01001628{
1629 ssl->disable_datagram_packing = !allow_packing;
1630}
1631
Gilles Peskine449bd832023-01-11 14:50:10 +01001632void mbedtls_ssl_conf_handshake_timeout(mbedtls_ssl_config *conf,
1633 uint32_t min, uint32_t max)
Manuel Pégourié-Gonnard905dd242014-10-01 12:03:55 +02001634{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02001635 conf->hs_timeout_min = min;
1636 conf->hs_timeout_max = max;
Manuel Pégourié-Gonnard905dd242014-10-01 12:03:55 +02001637}
1638#endif
1639
Gilles Peskine449bd832023-01-11 14:50:10 +01001640void mbedtls_ssl_conf_authmode(mbedtls_ssl_config *conf, int authmode)
Paul Bakker5121ce52009-01-03 21:22:43 +00001641{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02001642 conf->authmode = authmode;
Paul Bakker5121ce52009-01-03 21:22:43 +00001643}
1644
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001645#if defined(MBEDTLS_X509_CRT_PARSE_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01001646void mbedtls_ssl_conf_verify(mbedtls_ssl_config *conf,
1647 int (*f_vrfy)(void *, mbedtls_x509_crt *, int, uint32_t *),
1648 void *p_vrfy)
Paul Bakkerb63b0af2011-01-13 17:54:59 +00001649{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02001650 conf->f_vrfy = f_vrfy;
1651 conf->p_vrfy = p_vrfy;
Paul Bakkerb63b0af2011-01-13 17:54:59 +00001652}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001653#endif /* MBEDTLS_X509_CRT_PARSE_C */
Paul Bakkerb63b0af2011-01-13 17:54:59 +00001654
Gilles Peskine449bd832023-01-11 14:50:10 +01001655void mbedtls_ssl_conf_rng(mbedtls_ssl_config *conf,
1656 int (*f_rng)(void *, unsigned char *, size_t),
1657 void *p_rng)
Paul Bakker5121ce52009-01-03 21:22:43 +00001658{
Manuel Pégourié-Gonnard750e4d72015-05-07 12:35:38 +01001659 conf->f_rng = f_rng;
1660 conf->p_rng = p_rng;
Paul Bakker5121ce52009-01-03 21:22:43 +00001661}
1662
Gilles Peskine449bd832023-01-11 14:50:10 +01001663void mbedtls_ssl_conf_dbg(mbedtls_ssl_config *conf,
1664 void (*f_dbg)(void *, int, const char *, int, const char *),
1665 void *p_dbg)
Paul Bakker5121ce52009-01-03 21:22:43 +00001666{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02001667 conf->f_dbg = f_dbg;
1668 conf->p_dbg = p_dbg;
Paul Bakker5121ce52009-01-03 21:22:43 +00001669}
1670
Gilles Peskine449bd832023-01-11 14:50:10 +01001671void mbedtls_ssl_set_bio(mbedtls_ssl_context *ssl,
1672 void *p_bio,
1673 mbedtls_ssl_send_t *f_send,
1674 mbedtls_ssl_recv_t *f_recv,
1675 mbedtls_ssl_recv_timeout_t *f_recv_timeout)
Manuel Pégourié-Gonnard8fa6dfd2014-09-17 10:47:43 +02001676{
1677 ssl->p_bio = p_bio;
1678 ssl->f_send = f_send;
1679 ssl->f_recv = f_recv;
1680 ssl->f_recv_timeout = f_recv_timeout;
Manuel Pégourié-Gonnard97fd52c2015-05-06 15:38:52 +01001681}
1682
Manuel Pégourié-Gonnard6e7aaca2018-08-20 10:37:23 +02001683#if defined(MBEDTLS_SSL_PROTO_DTLS)
Gilles Peskine449bd832023-01-11 14:50:10 +01001684void mbedtls_ssl_set_mtu(mbedtls_ssl_context *ssl, uint16_t mtu)
Manuel Pégourié-Gonnard6e7aaca2018-08-20 10:37:23 +02001685{
1686 ssl->mtu = mtu;
1687}
1688#endif
1689
Gilles Peskine449bd832023-01-11 14:50:10 +01001690void mbedtls_ssl_conf_read_timeout(mbedtls_ssl_config *conf, uint32_t timeout)
Manuel Pégourié-Gonnard97fd52c2015-05-06 15:38:52 +01001691{
1692 conf->read_timeout = timeout;
Manuel Pégourié-Gonnard8fa6dfd2014-09-17 10:47:43 +02001693}
1694
Gilles Peskine449bd832023-01-11 14:50:10 +01001695void mbedtls_ssl_set_timer_cb(mbedtls_ssl_context *ssl,
1696 void *p_timer,
1697 mbedtls_ssl_set_timer_t *f_set_timer,
1698 mbedtls_ssl_get_timer_t *f_get_timer)
Manuel Pégourié-Gonnard2e012912015-05-12 20:55:41 +02001699{
1700 ssl->p_timer = p_timer;
1701 ssl->f_set_timer = f_set_timer;
1702 ssl->f_get_timer = f_get_timer;
Manuel Pégourié-Gonnard286a1362015-05-13 16:22:05 +02001703
1704 /* Make sure we start with no timer running */
Gilles Peskine449bd832023-01-11 14:50:10 +01001705 mbedtls_ssl_set_timer(ssl, 0);
Manuel Pégourié-Gonnard2e012912015-05-12 20:55:41 +02001706}
1707
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001708#if defined(MBEDTLS_SSL_SRV_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01001709void mbedtls_ssl_conf_session_cache(mbedtls_ssl_config *conf,
1710 void *p_cache,
1711 mbedtls_ssl_cache_get_t *f_get_cache,
1712 mbedtls_ssl_cache_set_t *f_set_cache)
Paul Bakker5121ce52009-01-03 21:22:43 +00001713{
Manuel Pégourié-Gonnard5cb33082015-05-06 18:06:26 +01001714 conf->p_cache = p_cache;
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02001715 conf->f_get_cache = f_get_cache;
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02001716 conf->f_set_cache = f_set_cache;
Paul Bakker5121ce52009-01-03 21:22:43 +00001717}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001718#endif /* MBEDTLS_SSL_SRV_C */
Paul Bakker5121ce52009-01-03 21:22:43 +00001719
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001720#if defined(MBEDTLS_SSL_CLI_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01001721int mbedtls_ssl_set_session(mbedtls_ssl_context *ssl, const mbedtls_ssl_session *session)
Paul Bakker5121ce52009-01-03 21:22:43 +00001722{
Janos Follath865b3eb2019-12-16 11:46:15 +00001723 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +02001724
Gilles Peskine449bd832023-01-11 14:50:10 +01001725 if (ssl == NULL ||
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +02001726 session == NULL ||
1727 ssl->session_negotiate == NULL ||
Gilles Peskine449bd832023-01-11 14:50:10 +01001728 ssl->conf->endpoint != MBEDTLS_SSL_IS_CLIENT) {
1729 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +02001730 }
1731
Gilles Peskine449bd832023-01-11 14:50:10 +01001732 if (ssl->handshake->resume == 1) {
1733 return MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
1734 }
Hanno Beckere810bbc2021-05-14 16:01:05 +01001735
Jerry Yu21092062022-10-10 21:21:31 +08001736#if defined(MBEDTLS_SSL_PROTO_TLS1_3)
Gilles Peskine449bd832023-01-11 14:50:10 +01001737 if (session->tls_version == MBEDTLS_SSL_VERSION_TLS1_3) {
Jerry Yu21092062022-10-10 21:21:31 +08001738 const mbedtls_ssl_ciphersuite_t *ciphersuite_info =
Gilles Peskine449bd832023-01-11 14:50:10 +01001739 mbedtls_ssl_ciphersuite_from_id(session->ciphersuite);
Jerry Yu21092062022-10-10 21:21:31 +08001740
Gilles Peskine449bd832023-01-11 14:50:10 +01001741 if (mbedtls_ssl_validate_ciphersuite(
Jerry Yu21092062022-10-10 21:21:31 +08001742 ssl, ciphersuite_info, MBEDTLS_SSL_VERSION_TLS1_3,
Gilles Peskine449bd832023-01-11 14:50:10 +01001743 MBEDTLS_SSL_VERSION_TLS1_3) != 0) {
1744 MBEDTLS_SSL_DEBUG_MSG(4, ("%d is not a valid TLS 1.3 ciphersuite.",
1745 session->ciphersuite));
1746 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
Jerry Yu21092062022-10-10 21:21:31 +08001747 }
Jerry Yu40afab62022-10-08 10:42:13 +08001748 }
Jerry Yu21092062022-10-10 21:21:31 +08001749#endif /* MBEDTLS_SSL_PROTO_TLS1_3 */
Jerry Yu40afab62022-10-08 10:42:13 +08001750
Gilles Peskine449bd832023-01-11 14:50:10 +01001751 if ((ret = mbedtls_ssl_session_copy(ssl->session_negotiate,
1752 session)) != 0) {
1753 return ret;
1754 }
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +02001755
Paul Bakker0a597072012-09-25 21:55:46 +00001756 ssl->handshake->resume = 1;
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +02001757
Gilles Peskine449bd832023-01-11 14:50:10 +01001758 return 0;
Paul Bakker5121ce52009-01-03 21:22:43 +00001759}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001760#endif /* MBEDTLS_SSL_CLI_C */
Paul Bakker5121ce52009-01-03 21:22:43 +00001761
Gilles Peskine449bd832023-01-11 14:50:10 +01001762void mbedtls_ssl_conf_ciphersuites(mbedtls_ssl_config *conf,
1763 const int *ciphersuites)
Mateusz Starzyk06b07fb2021-02-18 13:55:21 +01001764{
Hanno Beckerd60b6c62021-04-29 12:04:11 +01001765 conf->ciphersuite_list = ciphersuites;
Paul Bakker5121ce52009-01-03 21:22:43 +00001766}
1767
Ronald Cron6f135e12021-12-08 16:57:54 +01001768#if defined(MBEDTLS_SSL_PROTO_TLS1_3)
Gilles Peskine449bd832023-01-11 14:50:10 +01001769void mbedtls_ssl_conf_tls13_key_exchange_modes(mbedtls_ssl_config *conf,
1770 const int kex_modes)
Hanno Becker71f1ed62021-07-24 06:01:47 +01001771{
Xiaofei Bai746f9482021-11-12 08:53:56 +00001772 conf->tls13_kex_modes = kex_modes & MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_ALL;
Hanno Becker71f1ed62021-07-24 06:01:47 +01001773}
Xiaokang Qian72de95d2022-10-25 02:54:33 +00001774
1775#if defined(MBEDTLS_SSL_EARLY_DATA)
Gilles Peskine449bd832023-01-11 14:50:10 +01001776void mbedtls_ssl_tls13_conf_early_data(mbedtls_ssl_config *conf,
1777 int early_data_enabled)
Xiaokang Qian72de95d2022-10-25 02:54:33 +00001778{
1779 conf->early_data_enabled = early_data_enabled;
1780}
Jerry Yucc4e0072022-11-22 17:22:22 +08001781
1782#if defined(MBEDTLS_SSL_SRV_C)
1783void mbedtls_ssl_tls13_conf_max_early_data_size(
Gilles Peskine449bd832023-01-11 14:50:10 +01001784 mbedtls_ssl_config *conf, uint32_t max_early_data_size)
Jerry Yucc4e0072022-11-22 17:22:22 +08001785{
Jerry Yu39da9852022-12-06 16:58:36 +08001786 conf->max_early_data_size = max_early_data_size;
Jerry Yucc4e0072022-11-22 17:22:22 +08001787}
1788#endif /* MBEDTLS_SSL_SRV_C */
1789
Xiaokang Qian72de95d2022-10-25 02:54:33 +00001790#endif /* MBEDTLS_SSL_EARLY_DATA */
Ronald Cron6f135e12021-12-08 16:57:54 +01001791#endif /* MBEDTLS_SSL_PROTO_TLS1_3 */
Hanno Becker71f1ed62021-07-24 06:01:47 +01001792
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001793#if defined(MBEDTLS_X509_CRT_PARSE_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01001794void mbedtls_ssl_conf_cert_profile(mbedtls_ssl_config *conf,
1795 const mbedtls_x509_crt_profile *profile)
Manuel Pégourié-Gonnard6e3ee3a2015-06-17 10:58:20 +02001796{
1797 conf->cert_profile = profile;
1798}
1799
Gilles Peskine449bd832023-01-11 14:50:10 +01001800static void ssl_key_cert_free(mbedtls_ssl_key_cert *key_cert)
Glenn Strauss36872db2022-01-22 05:06:31 -05001801{
1802 mbedtls_ssl_key_cert *cur = key_cert, *next;
1803
Gilles Peskine449bd832023-01-11 14:50:10 +01001804 while (cur != NULL) {
Glenn Strauss36872db2022-01-22 05:06:31 -05001805 next = cur->next;
Gilles Peskine449bd832023-01-11 14:50:10 +01001806 mbedtls_free(cur);
Glenn Strauss36872db2022-01-22 05:06:31 -05001807 cur = next;
1808 }
1809}
1810
Manuel Pégourié-Gonnard8f618a82015-05-10 21:13:36 +02001811/* Append a new keycert entry to a (possibly empty) list */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02001812MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01001813static int ssl_append_key_cert(mbedtls_ssl_key_cert **head,
1814 mbedtls_x509_crt *cert,
1815 mbedtls_pk_context *key)
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02001816{
niisato8ee24222018-06-25 19:05:48 +09001817 mbedtls_ssl_key_cert *new_cert;
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02001818
Gilles Peskine449bd832023-01-11 14:50:10 +01001819 if (cert == NULL) {
Glenn Strauss36872db2022-01-22 05:06:31 -05001820 /* Free list if cert is null */
Gilles Peskine449bd832023-01-11 14:50:10 +01001821 ssl_key_cert_free(*head);
Glenn Strauss36872db2022-01-22 05:06:31 -05001822 *head = NULL;
Gilles Peskine449bd832023-01-11 14:50:10 +01001823 return 0;
Glenn Strauss36872db2022-01-22 05:06:31 -05001824 }
1825
Gilles Peskine449bd832023-01-11 14:50:10 +01001826 new_cert = mbedtls_calloc(1, sizeof(mbedtls_ssl_key_cert));
1827 if (new_cert == NULL) {
1828 return MBEDTLS_ERR_SSL_ALLOC_FAILED;
1829 }
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02001830
niisato8ee24222018-06-25 19:05:48 +09001831 new_cert->cert = cert;
1832 new_cert->key = key;
1833 new_cert->next = NULL;
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02001834
Glenn Strauss36872db2022-01-22 05:06:31 -05001835 /* Update head if the list was null, else add to the end */
Gilles Peskine449bd832023-01-11 14:50:10 +01001836 if (*head == NULL) {
niisato8ee24222018-06-25 19:05:48 +09001837 *head = new_cert;
Gilles Peskine449bd832023-01-11 14:50:10 +01001838 } else {
Manuel Pégourié-Gonnard8f618a82015-05-10 21:13:36 +02001839 mbedtls_ssl_key_cert *cur = *head;
Gilles Peskine449bd832023-01-11 14:50:10 +01001840 while (cur->next != NULL) {
Manuel Pégourié-Gonnard8f618a82015-05-10 21:13:36 +02001841 cur = cur->next;
Gilles Peskine449bd832023-01-11 14:50:10 +01001842 }
niisato8ee24222018-06-25 19:05:48 +09001843 cur->next = new_cert;
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02001844 }
1845
Gilles Peskine449bd832023-01-11 14:50:10 +01001846 return 0;
Manuel Pégourié-Gonnard8f618a82015-05-10 21:13:36 +02001847}
1848
Gilles Peskine449bd832023-01-11 14:50:10 +01001849int mbedtls_ssl_conf_own_cert(mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnard8f618a82015-05-10 21:13:36 +02001850 mbedtls_x509_crt *own_cert,
Gilles Peskine449bd832023-01-11 14:50:10 +01001851 mbedtls_pk_context *pk_key)
Manuel Pégourié-Gonnard8f618a82015-05-10 21:13:36 +02001852{
Gilles Peskine449bd832023-01-11 14:50:10 +01001853 return ssl_append_key_cert(&conf->key_cert, own_cert, pk_key);
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02001854}
1855
Gilles Peskine449bd832023-01-11 14:50:10 +01001856void mbedtls_ssl_conf_ca_chain(mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnardbc2b7712015-05-06 11:14:19 +01001857 mbedtls_x509_crt *ca_chain,
Gilles Peskine449bd832023-01-11 14:50:10 +01001858 mbedtls_x509_crl *ca_crl)
Paul Bakker5121ce52009-01-03 21:22:43 +00001859{
Manuel Pégourié-Gonnardbc2b7712015-05-06 11:14:19 +01001860 conf->ca_chain = ca_chain;
1861 conf->ca_crl = ca_crl;
Hanno Becker5adaad92019-03-27 16:54:37 +00001862
1863#if defined(MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK)
1864 /* mbedtls_ssl_conf_ca_chain() and mbedtls_ssl_conf_ca_cb()
1865 * cannot be used together. */
1866 conf->f_ca_cb = NULL;
1867 conf->p_ca_cb = NULL;
1868#endif /* MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK */
Paul Bakker5121ce52009-01-03 21:22:43 +00001869}
Hanno Becker5adaad92019-03-27 16:54:37 +00001870
1871#if defined(MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK)
Gilles Peskine449bd832023-01-11 14:50:10 +01001872void mbedtls_ssl_conf_ca_cb(mbedtls_ssl_config *conf,
1873 mbedtls_x509_crt_ca_cb_t f_ca_cb,
1874 void *p_ca_cb)
Hanno Becker5adaad92019-03-27 16:54:37 +00001875{
1876 conf->f_ca_cb = f_ca_cb;
1877 conf->p_ca_cb = p_ca_cb;
1878
1879 /* mbedtls_ssl_conf_ca_chain() and mbedtls_ssl_conf_ca_cb()
1880 * cannot be used together. */
1881 conf->ca_chain = NULL;
1882 conf->ca_crl = NULL;
1883}
1884#endif /* MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001885#endif /* MBEDTLS_X509_CRT_PARSE_C */
Paul Bakkereb2c6582012-09-27 19:15:01 +00001886
Manuel Pégourié-Gonnard1af6c852015-05-10 23:10:37 +02001887#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
Gilles Peskine449bd832023-01-11 14:50:10 +01001888const unsigned char *mbedtls_ssl_get_hs_sni(mbedtls_ssl_context *ssl,
1889 size_t *name_len)
Glenn Strauss69894072022-01-24 12:58:00 -05001890{
1891 *name_len = ssl->handshake->sni_name_len;
Gilles Peskine449bd832023-01-11 14:50:10 +01001892 return ssl->handshake->sni_name;
Glenn Strauss69894072022-01-24 12:58:00 -05001893}
1894
Gilles Peskine449bd832023-01-11 14:50:10 +01001895int mbedtls_ssl_set_hs_own_cert(mbedtls_ssl_context *ssl,
1896 mbedtls_x509_crt *own_cert,
1897 mbedtls_pk_context *pk_key)
Manuel Pégourié-Gonnard1af6c852015-05-10 23:10:37 +02001898{
Gilles Peskine449bd832023-01-11 14:50:10 +01001899 return ssl_append_key_cert(&ssl->handshake->sni_key_cert,
1900 own_cert, pk_key);
Manuel Pégourié-Gonnard1af6c852015-05-10 23:10:37 +02001901}
Manuel Pégourié-Gonnard22bfa4b2015-05-11 08:46:37 +02001902
Gilles Peskine449bd832023-01-11 14:50:10 +01001903void mbedtls_ssl_set_hs_ca_chain(mbedtls_ssl_context *ssl,
1904 mbedtls_x509_crt *ca_chain,
1905 mbedtls_x509_crl *ca_crl)
Manuel Pégourié-Gonnard22bfa4b2015-05-11 08:46:37 +02001906{
1907 ssl->handshake->sni_ca_chain = ca_chain;
1908 ssl->handshake->sni_ca_crl = ca_crl;
1909}
Manuel Pégourié-Gonnardcdc26ae2015-06-19 12:16:31 +02001910
Glenn Strauss999ef702022-03-11 01:37:23 -05001911#if defined(MBEDTLS_KEY_EXCHANGE_CERT_REQ_ALLOWED_ENABLED)
Gilles Peskine449bd832023-01-11 14:50:10 +01001912void mbedtls_ssl_set_hs_dn_hints(mbedtls_ssl_context *ssl,
1913 const mbedtls_x509_crt *crt)
Glenn Strauss999ef702022-03-11 01:37:23 -05001914{
1915 ssl->handshake->dn_hints = crt;
1916}
1917#endif /* MBEDTLS_KEY_EXCHANGE_CERT_REQ_ALLOWED_ENABLED */
1918
Gilles Peskine449bd832023-01-11 14:50:10 +01001919void mbedtls_ssl_set_hs_authmode(mbedtls_ssl_context *ssl,
1920 int authmode)
Manuel Pégourié-Gonnardcdc26ae2015-06-19 12:16:31 +02001921{
1922 ssl->handshake->sni_authmode = authmode;
1923}
Manuel Pégourié-Gonnard1af6c852015-05-10 23:10:37 +02001924#endif /* MBEDTLS_SSL_SERVER_NAME_INDICATION */
1925
Hanno Becker8927c832019-04-03 12:52:50 +01001926#if defined(MBEDTLS_X509_CRT_PARSE_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01001927void mbedtls_ssl_set_verify(mbedtls_ssl_context *ssl,
1928 int (*f_vrfy)(void *, mbedtls_x509_crt *, int, uint32_t *),
1929 void *p_vrfy)
Hanno Becker8927c832019-04-03 12:52:50 +01001930{
1931 ssl->f_vrfy = f_vrfy;
1932 ssl->p_vrfy = p_vrfy;
1933}
1934#endif
1935
Manuel Pégourié-Gonnardeef142d2015-09-16 10:05:04 +02001936#if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
Manuel Pégourié-Gonnard7002f4a2015-09-15 12:43:43 +02001937
Valerio Setti016f6822022-12-09 14:17:50 +01001938#if defined(MBEDTLS_USE_PSA_CRYPTO)
1939static psa_status_t mbedtls_ssl_set_hs_ecjpake_password_common(
Gilles Peskine449bd832023-01-11 14:50:10 +01001940 mbedtls_ssl_context *ssl,
1941 mbedtls_svc_key_id_t pwd)
Valerio Setti016f6822022-12-09 14:17:50 +01001942{
1943 psa_status_t status;
1944 psa_pake_role_t psa_role;
1945 psa_pake_cipher_suite_t cipher_suite = psa_pake_cipher_suite_init();
1946
Gilles Peskine449bd832023-01-11 14:50:10 +01001947 psa_pake_cs_set_algorithm(&cipher_suite, PSA_ALG_JPAKE);
1948 psa_pake_cs_set_primitive(&cipher_suite,
1949 PSA_PAKE_PRIMITIVE(PSA_PAKE_PRIMITIVE_TYPE_ECC,
1950 PSA_ECC_FAMILY_SECP_R1,
1951 256));
1952 psa_pake_cs_set_hash(&cipher_suite, PSA_ALG_SHA_256);
Valerio Setti016f6822022-12-09 14:17:50 +01001953
Gilles Peskine449bd832023-01-11 14:50:10 +01001954 status = psa_pake_setup(&ssl->handshake->psa_pake_ctx, &cipher_suite);
1955 if (status != PSA_SUCCESS) {
Valerio Setti016f6822022-12-09 14:17:50 +01001956 return status;
Gilles Peskine449bd832023-01-11 14:50:10 +01001957 }
Manuel Pégourié-Gonnard7002f4a2015-09-15 12:43:43 +02001958
Gilles Peskine449bd832023-01-11 14:50:10 +01001959 if (ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER) {
Neil Armstrongca7d5062022-05-31 14:43:23 +02001960 psa_role = PSA_PAKE_ROLE_SERVER;
Gilles Peskine449bd832023-01-11 14:50:10 +01001961 } else {
Neil Armstrongca7d5062022-05-31 14:43:23 +02001962 psa_role = PSA_PAKE_ROLE_CLIENT;
Gilles Peskine449bd832023-01-11 14:50:10 +01001963 }
Neil Armstrongca7d5062022-05-31 14:43:23 +02001964
Gilles Peskine449bd832023-01-11 14:50:10 +01001965 status = psa_pake_set_role(&ssl->handshake->psa_pake_ctx, psa_role);
1966 if (status != PSA_SUCCESS) {
Valerio Setti016f6822022-12-09 14:17:50 +01001967 return status;
Gilles Peskine449bd832023-01-11 14:50:10 +01001968 }
Valerio Setti016f6822022-12-09 14:17:50 +01001969
Gilles Peskine449bd832023-01-11 14:50:10 +01001970 status = psa_pake_set_password_key(&ssl->handshake->psa_pake_ctx, pwd);
1971 if (status != PSA_SUCCESS) {
Valerio Setti016f6822022-12-09 14:17:50 +01001972 return status;
Gilles Peskine449bd832023-01-11 14:50:10 +01001973 }
Valerio Setti016f6822022-12-09 14:17:50 +01001974
1975 ssl->handshake->psa_pake_ctx_is_ok = 1;
1976
Gilles Peskine449bd832023-01-11 14:50:10 +01001977 return PSA_SUCCESS;
Valerio Setti016f6822022-12-09 14:17:50 +01001978}
1979
Gilles Peskine449bd832023-01-11 14:50:10 +01001980int mbedtls_ssl_set_hs_ecjpake_password(mbedtls_ssl_context *ssl,
1981 const unsigned char *pw,
1982 size_t pw_len)
Valerio Setti016f6822022-12-09 14:17:50 +01001983{
1984 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
1985 psa_status_t status;
1986
Gilles Peskine449bd832023-01-11 14:50:10 +01001987 if (ssl->handshake == NULL || ssl->conf == NULL) {
1988 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
Neil Armstrongca7d5062022-05-31 14:43:23 +02001989 }
1990
Gilles Peskine449bd832023-01-11 14:50:10 +01001991 /* Empty password is not valid */
1992 if ((pw == NULL) || (pw_len == 0)) {
1993 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
1994 }
1995
1996 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_DERIVE);
1997 psa_set_key_algorithm(&attributes, PSA_ALG_JPAKE);
1998 psa_set_key_type(&attributes, PSA_KEY_TYPE_PASSWORD);
1999
2000 status = psa_import_key(&attributes, pw, pw_len,
2001 &ssl->handshake->psa_pake_password);
2002 if (status != PSA_SUCCESS) {
2003 return MBEDTLS_ERR_SSL_HW_ACCEL_FAILED;
2004 }
2005
2006 status = mbedtls_ssl_set_hs_ecjpake_password_common(ssl,
2007 ssl->handshake->psa_pake_password);
2008 if (status != PSA_SUCCESS) {
2009 psa_destroy_key(ssl->handshake->psa_pake_password);
2010 psa_pake_abort(&ssl->handshake->psa_pake_ctx);
2011 return MBEDTLS_ERR_SSL_HW_ACCEL_FAILED;
2012 }
2013
2014 return 0;
Valerio Setti02c25b52022-11-15 14:08:42 +01002015}
Valerio Settia9a97dc2022-11-28 18:26:16 +01002016
Gilles Peskine449bd832023-01-11 14:50:10 +01002017int mbedtls_ssl_set_hs_ecjpake_password_opaque(mbedtls_ssl_context *ssl,
2018 mbedtls_svc_key_id_t pwd)
Valerio Settia9a97dc2022-11-28 18:26:16 +01002019{
Valerio Settia9a97dc2022-11-28 18:26:16 +01002020 psa_status_t status;
2021
Gilles Peskine449bd832023-01-11 14:50:10 +01002022 if (ssl->handshake == NULL || ssl->conf == NULL) {
2023 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
Neil Armstrongca7d5062022-05-31 14:43:23 +02002024 }
2025
Gilles Peskine449bd832023-01-11 14:50:10 +01002026 if (mbedtls_svc_key_id_is_null(pwd)) {
2027 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
2028 }
2029
2030 status = mbedtls_ssl_set_hs_ecjpake_password_common(ssl, pwd);
2031 if (status != PSA_SUCCESS) {
2032 psa_pake_abort(&ssl->handshake->psa_pake_ctx);
2033 return MBEDTLS_ERR_SSL_HW_ACCEL_FAILED;
2034 }
2035
2036 return 0;
Valerio Setti02c25b52022-11-15 14:08:42 +01002037}
2038#else /* MBEDTLS_USE_PSA_CRYPTO */
Gilles Peskine449bd832023-01-11 14:50:10 +01002039int mbedtls_ssl_set_hs_ecjpake_password(mbedtls_ssl_context *ssl,
2040 const unsigned char *pw,
2041 size_t pw_len)
Manuel Pégourié-Gonnard7002f4a2015-09-15 12:43:43 +02002042{
2043 mbedtls_ecjpake_role role;
2044
Gilles Peskine449bd832023-01-11 14:50:10 +01002045 if (ssl->handshake == NULL || ssl->conf == NULL) {
2046 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
2047 }
Manuel Pégourié-Gonnard7002f4a2015-09-15 12:43:43 +02002048
Valerio Settic689ed82022-12-07 14:40:38 +01002049 /* Empty password is not valid */
Gilles Peskine449bd832023-01-11 14:50:10 +01002050 if ((pw == NULL) || (pw_len == 0)) {
2051 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
2052 }
Valerio Settic689ed82022-12-07 14:40:38 +01002053
Gilles Peskine449bd832023-01-11 14:50:10 +01002054 if (ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER) {
Manuel Pégourié-Gonnard7002f4a2015-09-15 12:43:43 +02002055 role = MBEDTLS_ECJPAKE_SERVER;
Gilles Peskine449bd832023-01-11 14:50:10 +01002056 } else {
Manuel Pégourié-Gonnard7002f4a2015-09-15 12:43:43 +02002057 role = MBEDTLS_ECJPAKE_CLIENT;
Gilles Peskine449bd832023-01-11 14:50:10 +01002058 }
Manuel Pégourié-Gonnard7002f4a2015-09-15 12:43:43 +02002059
Gilles Peskine449bd832023-01-11 14:50:10 +01002060 return mbedtls_ecjpake_setup(&ssl->handshake->ecjpake_ctx,
2061 role,
2062 MBEDTLS_MD_SHA256,
2063 MBEDTLS_ECP_DP_SECP256R1,
2064 pw, pw_len);
Manuel Pégourié-Gonnard7002f4a2015-09-15 12:43:43 +02002065}
Valerio Setti02c25b52022-11-15 14:08:42 +01002066#endif /* MBEDTLS_USE_PSA_CRYPTO */
Manuel Pégourié-Gonnardeef142d2015-09-16 10:05:04 +02002067#endif /* MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED */
Manuel Pégourié-Gonnard7002f4a2015-09-15 12:43:43 +02002068
Ronald Cron73fe8df2022-10-05 14:31:43 +02002069#if defined(MBEDTLS_SSL_HANDSHAKE_WITH_PSK_ENABLED)
Gilles Peskine449bd832023-01-11 14:50:10 +01002070int mbedtls_ssl_conf_has_static_psk(mbedtls_ssl_config const *conf)
Hanno Becker2ed3dce2021-04-19 21:59:14 +01002071{
Gilles Peskine449bd832023-01-11 14:50:10 +01002072 if (conf->psk_identity == NULL ||
2073 conf->psk_identity_len == 0) {
2074 return 0;
Ronald Crond29e13e2022-10-19 10:33:48 +02002075 }
2076
Hanno Becker2ed3dce2021-04-19 21:59:14 +01002077#if defined(MBEDTLS_USE_PSA_CRYPTO)
Gilles Peskine449bd832023-01-11 14:50:10 +01002078 if (!mbedtls_svc_key_id_is_null(conf->psk_opaque)) {
2079 return 1;
2080 }
Hanno Becker2ed3dce2021-04-19 21:59:14 +01002081#endif /* MBEDTLS_USE_PSA_CRYPTO */
Ronald Crond29e13e2022-10-19 10:33:48 +02002082
Gilles Peskine449bd832023-01-11 14:50:10 +01002083 if (conf->psk != NULL && conf->psk_len != 0) {
2084 return 1;
2085 }
Hanno Becker2ed3dce2021-04-19 21:59:14 +01002086
Gilles Peskine449bd832023-01-11 14:50:10 +01002087 return 0;
Hanno Becker2ed3dce2021-04-19 21:59:14 +01002088}
2089
Gilles Peskine449bd832023-01-11 14:50:10 +01002090static void ssl_conf_remove_psk(mbedtls_ssl_config *conf)
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01002091{
2092 /* Remove reference to existing PSK, if any. */
2093#if defined(MBEDTLS_USE_PSA_CRYPTO)
Gilles Peskine449bd832023-01-11 14:50:10 +01002094 if (!mbedtls_svc_key_id_is_null(conf->psk_opaque)) {
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01002095 /* The maintenance of the PSK key slot is the
2096 * user's responsibility. */
Ronald Croncf56a0a2020-08-04 09:51:30 +02002097 conf->psk_opaque = MBEDTLS_SVC_KEY_ID_INIT;
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01002098 }
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01002099#endif /* MBEDTLS_USE_PSA_CRYPTO */
Gilles Peskine449bd832023-01-11 14:50:10 +01002100 if (conf->psk != NULL) {
2101 mbedtls_platform_zeroize(conf->psk, conf->psk_len);
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01002102
Gilles Peskine449bd832023-01-11 14:50:10 +01002103 mbedtls_free(conf->psk);
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01002104 conf->psk = NULL;
2105 conf->psk_len = 0;
2106 }
2107
2108 /* Remove reference to PSK identity, if any. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002109 if (conf->psk_identity != NULL) {
2110 mbedtls_free(conf->psk_identity);
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01002111 conf->psk_identity = NULL;
2112 conf->psk_identity_len = 0;
2113 }
2114}
2115
Hanno Becker7390c712018-11-15 13:33:04 +00002116/* This function assumes that PSK identity in the SSL config is unset.
2117 * It checks that the provided identity is well-formed and attempts
2118 * to make a copy of it in the SSL config.
2119 * On failure, the PSK identity in the config remains unset. */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02002120MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01002121static int ssl_conf_set_psk_identity(mbedtls_ssl_config *conf,
2122 unsigned char const *psk_identity,
2123 size_t psk_identity_len)
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02002124{
Manuel Pégourié-Gonnardc6b5d832015-08-27 16:37:35 +02002125 /* Identity len will be encoded on two bytes */
Gilles Peskine449bd832023-01-11 14:50:10 +01002126 if (psk_identity == NULL ||
Ronald Cron2a87e9b2022-10-19 10:55:26 +02002127 psk_identity_len == 0 ||
Gilles Peskine449bd832023-01-11 14:50:10 +01002128 (psk_identity_len >> 16) != 0 ||
2129 psk_identity_len > MBEDTLS_SSL_OUT_CONTENT_LEN) {
2130 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
Manuel Pégourié-Gonnardc6b5d832015-08-27 16:37:35 +02002131 }
2132
Gilles Peskine449bd832023-01-11 14:50:10 +01002133 conf->psk_identity = mbedtls_calloc(1, psk_identity_len);
2134 if (conf->psk_identity == NULL) {
2135 return MBEDTLS_ERR_SSL_ALLOC_FAILED;
2136 }
Paul Bakker6db455e2013-09-18 17:29:31 +02002137
Manuel Pégourié-Gonnard120fdbd2015-05-07 17:07:50 +01002138 conf->psk_identity_len = psk_identity_len;
Gilles Peskine449bd832023-01-11 14:50:10 +01002139 memcpy(conf->psk_identity, psk_identity, conf->psk_identity_len);
Paul Bakker5ad403f2013-09-18 21:21:30 +02002140
Gilles Peskine449bd832023-01-11 14:50:10 +01002141 return 0;
Paul Bakker6db455e2013-09-18 17:29:31 +02002142}
2143
Gilles Peskine449bd832023-01-11 14:50:10 +01002144int mbedtls_ssl_conf_psk(mbedtls_ssl_config *conf,
2145 const unsigned char *psk, size_t psk_len,
2146 const unsigned char *psk_identity, size_t psk_identity_len)
Hanno Becker7390c712018-11-15 13:33:04 +00002147{
Janos Follath865b3eb2019-12-16 11:46:15 +00002148 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Hanno Becker2ed3dce2021-04-19 21:59:14 +01002149
2150 /* We currently only support one PSK, raw or opaque. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002151 if (mbedtls_ssl_conf_has_static_psk(conf)) {
2152 return MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
2153 }
Hanno Becker7390c712018-11-15 13:33:04 +00002154
2155 /* Check and set raw PSK */
Gilles Peskine449bd832023-01-11 14:50:10 +01002156 if (psk == NULL) {
2157 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
2158 }
2159 if (psk_len == 0) {
2160 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
2161 }
2162 if (psk_len > MBEDTLS_PSK_MAX_LEN) {
2163 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
2164 }
Piotr Nowicki9926eaf2019-11-20 14:54:36 +01002165
Gilles Peskine449bd832023-01-11 14:50:10 +01002166 if ((conf->psk = mbedtls_calloc(1, psk_len)) == NULL) {
2167 return MBEDTLS_ERR_SSL_ALLOC_FAILED;
2168 }
Hanno Becker7390c712018-11-15 13:33:04 +00002169 conf->psk_len = psk_len;
Gilles Peskine449bd832023-01-11 14:50:10 +01002170 memcpy(conf->psk, psk, conf->psk_len);
Hanno Becker7390c712018-11-15 13:33:04 +00002171
2172 /* Check and set PSK Identity */
Gilles Peskine449bd832023-01-11 14:50:10 +01002173 ret = ssl_conf_set_psk_identity(conf, psk_identity, psk_identity_len);
2174 if (ret != 0) {
2175 ssl_conf_remove_psk(conf);
2176 }
Hanno Becker7390c712018-11-15 13:33:04 +00002177
Gilles Peskine449bd832023-01-11 14:50:10 +01002178 return ret;
Hanno Becker7390c712018-11-15 13:33:04 +00002179}
2180
Gilles Peskine449bd832023-01-11 14:50:10 +01002181static void ssl_remove_psk(mbedtls_ssl_context *ssl)
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01002182{
2183#if defined(MBEDTLS_USE_PSA_CRYPTO)
Gilles Peskine449bd832023-01-11 14:50:10 +01002184 if (!mbedtls_svc_key_id_is_null(ssl->handshake->psk_opaque)) {
Neil Armstrong501c9322022-05-03 09:35:09 +02002185 /* The maintenance of the external PSK key slot is the
2186 * user's responsibility. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002187 if (ssl->handshake->psk_opaque_is_internal) {
2188 psa_destroy_key(ssl->handshake->psk_opaque);
Neil Armstrong501c9322022-05-03 09:35:09 +02002189 ssl->handshake->psk_opaque_is_internal = 0;
2190 }
Ronald Croncf56a0a2020-08-04 09:51:30 +02002191 ssl->handshake->psk_opaque = MBEDTLS_SVC_KEY_ID_INIT;
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01002192 }
Neil Armstronge952a302022-05-03 10:22:14 +02002193#else
Gilles Peskine449bd832023-01-11 14:50:10 +01002194 if (ssl->handshake->psk != NULL) {
2195 mbedtls_platform_zeroize(ssl->handshake->psk,
2196 ssl->handshake->psk_len);
2197 mbedtls_free(ssl->handshake->psk);
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01002198 ssl->handshake->psk_len = 0;
2199 }
Neil Armstronge952a302022-05-03 10:22:14 +02002200#endif /* MBEDTLS_USE_PSA_CRYPTO */
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01002201}
2202
Gilles Peskine449bd832023-01-11 14:50:10 +01002203int mbedtls_ssl_set_hs_psk(mbedtls_ssl_context *ssl,
2204 const unsigned char *psk, size_t psk_len)
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01002205{
Neil Armstrong501c9322022-05-03 09:35:09 +02002206#if defined(MBEDTLS_USE_PSA_CRYPTO)
2207 psa_key_attributes_t key_attributes = psa_key_attributes_init();
Jerry Yu5d01c052022-08-17 10:18:10 +08002208 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Jerry Yu24b8c812022-08-20 19:06:56 +08002209 psa_algorithm_t alg = PSA_ALG_NONE;
Jerry Yu5d01c052022-08-17 10:18:10 +08002210 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Neil Armstrong501c9322022-05-03 09:35:09 +02002211#endif /* MBEDTLS_USE_PSA_CRYPTO */
2212
Gilles Peskine449bd832023-01-11 14:50:10 +01002213 if (psk == NULL || ssl->handshake == NULL) {
2214 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
2215 }
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01002216
Gilles Peskine449bd832023-01-11 14:50:10 +01002217 if (psk_len > MBEDTLS_PSK_MAX_LEN) {
2218 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
2219 }
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01002220
Gilles Peskine449bd832023-01-11 14:50:10 +01002221 ssl_remove_psk(ssl);
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01002222
Neil Armstrong501c9322022-05-03 09:35:09 +02002223#if defined(MBEDTLS_USE_PSA_CRYPTO)
Jerry Yuccc68a42022-07-26 16:39:20 +08002224#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
Gilles Peskine449bd832023-01-11 14:50:10 +01002225 if (ssl->tls_version == MBEDTLS_SSL_VERSION_TLS1_2) {
2226 if (ssl->handshake->ciphersuite_info->mac == MBEDTLS_MD_SHA384) {
2227 alg = PSA_ALG_TLS12_PSK_TO_MS(PSA_ALG_SHA_384);
2228 } else {
2229 alg = PSA_ALG_TLS12_PSK_TO_MS(PSA_ALG_SHA_256);
2230 }
2231 psa_set_key_usage_flags(&key_attributes, PSA_KEY_USAGE_DERIVE);
Jerry Yuccc68a42022-07-26 16:39:20 +08002232 }
2233#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Neil Armstrong501c9322022-05-03 09:35:09 +02002234
Jerry Yu568ec252022-07-22 21:27:34 +08002235#if defined(MBEDTLS_SSL_PROTO_TLS1_3)
Gilles Peskine449bd832023-01-11 14:50:10 +01002236 if (ssl->tls_version == MBEDTLS_SSL_VERSION_TLS1_3) {
2237 alg = PSA_ALG_HKDF_EXTRACT(PSA_ALG_ANY_HASH);
2238 psa_set_key_usage_flags(&key_attributes,
2239 PSA_KEY_USAGE_DERIVE | PSA_KEY_USAGE_EXPORT);
Jerry Yuccc68a42022-07-26 16:39:20 +08002240 }
2241#endif /* MBEDTLS_SSL_PROTO_TLS1_3 */
2242
Gilles Peskine449bd832023-01-11 14:50:10 +01002243 psa_set_key_algorithm(&key_attributes, alg);
2244 psa_set_key_type(&key_attributes, PSA_KEY_TYPE_DERIVE);
Neil Armstrong501c9322022-05-03 09:35:09 +02002245
Gilles Peskine449bd832023-01-11 14:50:10 +01002246 status = psa_import_key(&key_attributes, psk, psk_len, &key);
2247 if (status != PSA_SUCCESS) {
2248 return MBEDTLS_ERR_SSL_HW_ACCEL_FAILED;
2249 }
Neil Armstrong501c9322022-05-03 09:35:09 +02002250
2251 /* Allow calling psa_destroy_key() on psk remove */
2252 ssl->handshake->psk_opaque_is_internal = 1;
Gilles Peskine449bd832023-01-11 14:50:10 +01002253 return mbedtls_ssl_set_hs_psk_opaque(ssl, key);
Neil Armstrong501c9322022-05-03 09:35:09 +02002254#else
Gilles Peskine449bd832023-01-11 14:50:10 +01002255 if ((ssl->handshake->psk = mbedtls_calloc(1, psk_len)) == NULL) {
2256 return MBEDTLS_ERR_SSL_ALLOC_FAILED;
2257 }
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01002258
2259 ssl->handshake->psk_len = psk_len;
Gilles Peskine449bd832023-01-11 14:50:10 +01002260 memcpy(ssl->handshake->psk, psk, ssl->handshake->psk_len);
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01002261
Gilles Peskine449bd832023-01-11 14:50:10 +01002262 return 0;
Neil Armstrong501c9322022-05-03 09:35:09 +02002263#endif /* MBEDTLS_USE_PSA_CRYPTO */
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01002264}
2265
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01002266#if defined(MBEDTLS_USE_PSA_CRYPTO)
Gilles Peskine449bd832023-01-11 14:50:10 +01002267int mbedtls_ssl_conf_psk_opaque(mbedtls_ssl_config *conf,
2268 mbedtls_svc_key_id_t psk,
2269 const unsigned char *psk_identity,
2270 size_t psk_identity_len)
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01002271{
Janos Follath865b3eb2019-12-16 11:46:15 +00002272 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Hanno Becker2ed3dce2021-04-19 21:59:14 +01002273
2274 /* We currently only support one PSK, raw or opaque. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002275 if (mbedtls_ssl_conf_has_static_psk(conf)) {
2276 return MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
2277 }
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01002278
Hanno Becker7390c712018-11-15 13:33:04 +00002279 /* Check and set opaque PSK */
Gilles Peskine449bd832023-01-11 14:50:10 +01002280 if (mbedtls_svc_key_id_is_null(psk)) {
2281 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
2282 }
Ronald Croncf56a0a2020-08-04 09:51:30 +02002283 conf->psk_opaque = psk;
Hanno Becker7390c712018-11-15 13:33:04 +00002284
2285 /* Check and set PSK Identity */
Gilles Peskine449bd832023-01-11 14:50:10 +01002286 ret = ssl_conf_set_psk_identity(conf, psk_identity,
2287 psk_identity_len);
2288 if (ret != 0) {
2289 ssl_conf_remove_psk(conf);
2290 }
Hanno Becker7390c712018-11-15 13:33:04 +00002291
Gilles Peskine449bd832023-01-11 14:50:10 +01002292 return ret;
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01002293}
2294
Gilles Peskine449bd832023-01-11 14:50:10 +01002295int mbedtls_ssl_set_hs_psk_opaque(mbedtls_ssl_context *ssl,
2296 mbedtls_svc_key_id_t psk)
Przemyslaw Stekiel6928a512022-02-03 13:50:35 +01002297{
Gilles Peskine449bd832023-01-11 14:50:10 +01002298 if ((mbedtls_svc_key_id_is_null(psk)) ||
2299 (ssl->handshake == NULL)) {
2300 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
2301 }
Przemyslaw Stekiel6928a512022-02-03 13:50:35 +01002302
Gilles Peskine449bd832023-01-11 14:50:10 +01002303 ssl_remove_psk(ssl);
Przemyslaw Stekiel6928a512022-02-03 13:50:35 +01002304 ssl->handshake->psk_opaque = psk;
Gilles Peskine449bd832023-01-11 14:50:10 +01002305 return 0;
Przemyslaw Stekiel6928a512022-02-03 13:50:35 +01002306}
2307#endif /* MBEDTLS_USE_PSA_CRYPTO */
2308
Jerry Yu8897c072022-08-12 13:56:53 +08002309#if defined(MBEDTLS_SSL_SRV_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01002310void mbedtls_ssl_conf_psk_cb(mbedtls_ssl_config *conf,
2311 int (*f_psk)(void *, mbedtls_ssl_context *, const unsigned char *,
2312 size_t),
2313 void *p_psk)
Przemyslaw Stekiel6928a512022-02-03 13:50:35 +01002314{
2315 conf->f_psk = f_psk;
2316 conf->p_psk = p_psk;
2317}
Jerry Yu8897c072022-08-12 13:56:53 +08002318#endif /* MBEDTLS_SSL_SRV_C */
2319
Ronald Cron73fe8df2022-10-05 14:31:43 +02002320#endif /* MBEDTLS_SSL_HANDSHAKE_WITH_PSK_ENABLED */
Przemyslaw Stekiel6928a512022-02-03 13:50:35 +01002321
2322#if defined(MBEDTLS_USE_PSA_CRYPTO)
Gilles Peskine301711e2022-04-26 16:57:05 +02002323static mbedtls_ssl_mode_t mbedtls_ssl_get_base_mode(
Gilles Peskine449bd832023-01-11 14:50:10 +01002324 psa_algorithm_t alg)
Neil Armstrong8a0f3e82022-03-30 10:57:37 +02002325{
Neil Armstrong8a0f3e82022-03-30 10:57:37 +02002326#if defined(MBEDTLS_SSL_SOME_SUITES_USE_MAC)
Gilles Peskine449bd832023-01-11 14:50:10 +01002327 if (alg == PSA_ALG_CBC_NO_PADDING) {
2328 return MBEDTLS_SSL_MODE_CBC;
2329 }
Neil Armstrong8a0f3e82022-03-30 10:57:37 +02002330#endif /* MBEDTLS_SSL_SOME_SUITES_USE_MAC */
Gilles Peskine449bd832023-01-11 14:50:10 +01002331 if (PSA_ALG_IS_AEAD(alg)) {
2332 return MBEDTLS_SSL_MODE_AEAD;
2333 }
2334 return MBEDTLS_SSL_MODE_STREAM;
Gilles Peskine301711e2022-04-26 16:57:05 +02002335}
2336
2337#else /* MBEDTLS_USE_PSA_CRYPTO */
2338
2339static mbedtls_ssl_mode_t mbedtls_ssl_get_base_mode(
Gilles Peskine449bd832023-01-11 14:50:10 +01002340 mbedtls_cipher_mode_t mode)
Gilles Peskine301711e2022-04-26 16:57:05 +02002341{
2342#if defined(MBEDTLS_SSL_SOME_SUITES_USE_MAC)
Gilles Peskine449bd832023-01-11 14:50:10 +01002343 if (mode == MBEDTLS_MODE_CBC) {
2344 return MBEDTLS_SSL_MODE_CBC;
2345 }
Gilles Peskine301711e2022-04-26 16:57:05 +02002346#endif /* MBEDTLS_SSL_SOME_SUITES_USE_MAC */
2347
Neil Armstrong8a0f3e82022-03-30 10:57:37 +02002348#if defined(MBEDTLS_GCM_C) || \
2349 defined(MBEDTLS_CCM_C) || \
2350 defined(MBEDTLS_CHACHAPOLY_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01002351 if (mode == MBEDTLS_MODE_GCM ||
Neil Armstrong8a0f3e82022-03-30 10:57:37 +02002352 mode == MBEDTLS_MODE_CCM ||
Gilles Peskine449bd832023-01-11 14:50:10 +01002353 mode == MBEDTLS_MODE_CHACHAPOLY) {
2354 return MBEDTLS_SSL_MODE_AEAD;
2355 }
Neil Armstrong8a0f3e82022-03-30 10:57:37 +02002356#endif /* MBEDTLS_GCM_C || MBEDTLS_CCM_C || MBEDTLS_CHACHAPOLY_C */
Neil Armstrong8a0f3e82022-03-30 10:57:37 +02002357
Gilles Peskine449bd832023-01-11 14:50:10 +01002358 return MBEDTLS_SSL_MODE_STREAM;
Neil Armstrong8a0f3e82022-03-30 10:57:37 +02002359}
Gilles Peskine301711e2022-04-26 16:57:05 +02002360#endif /* MBEDTLS_USE_PSA_CRYPTO */
Neil Armstrong8a0f3e82022-03-30 10:57:37 +02002361
Gilles Peskinee108d982022-04-26 16:50:40 +02002362static mbedtls_ssl_mode_t mbedtls_ssl_get_actual_mode(
2363 mbedtls_ssl_mode_t base_mode,
Gilles Peskine449bd832023-01-11 14:50:10 +01002364 int encrypt_then_mac)
Gilles Peskinee108d982022-04-26 16:50:40 +02002365{
2366#if defined(MBEDTLS_SSL_SOME_SUITES_USE_CBC_ETM)
Gilles Peskine449bd832023-01-11 14:50:10 +01002367 if (encrypt_then_mac == MBEDTLS_SSL_ETM_ENABLED &&
2368 base_mode == MBEDTLS_SSL_MODE_CBC) {
2369 return MBEDTLS_SSL_MODE_CBC_ETM;
Gilles Peskinee108d982022-04-26 16:50:40 +02002370 }
2371#else
2372 (void) encrypt_then_mac;
2373#endif
Gilles Peskine449bd832023-01-11 14:50:10 +01002374 return base_mode;
Gilles Peskinee108d982022-04-26 16:50:40 +02002375}
2376
Neil Armstrongab555e02022-04-04 11:07:59 +02002377mbedtls_ssl_mode_t mbedtls_ssl_get_mode_from_transform(
Gilles Peskine449bd832023-01-11 14:50:10 +01002378 const mbedtls_ssl_transform *transform)
Neil Armstrong4bf4c862022-04-01 10:35:48 +02002379{
Gilles Peskinee108d982022-04-26 16:50:40 +02002380 mbedtls_ssl_mode_t base_mode = mbedtls_ssl_get_base_mode(
Neil Armstrong4bf4c862022-04-01 10:35:48 +02002381#if defined(MBEDTLS_USE_PSA_CRYPTO)
Gilles Peskine449bd832023-01-11 14:50:10 +01002382 transform->psa_alg
Neil Armstrong4bf4c862022-04-01 10:35:48 +02002383#else
Gilles Peskine449bd832023-01-11 14:50:10 +01002384 mbedtls_cipher_get_cipher_mode(&transform->cipher_ctx_enc)
Gilles Peskinee108d982022-04-26 16:50:40 +02002385#endif
2386 );
Neil Armstrong4bf4c862022-04-01 10:35:48 +02002387
Gilles Peskinee108d982022-04-26 16:50:40 +02002388 int encrypt_then_mac = 0;
Neil Armstrongf2c82f02022-04-05 11:16:53 +02002389#if defined(MBEDTLS_SSL_SOME_SUITES_USE_CBC_ETM)
Gilles Peskinee108d982022-04-26 16:50:40 +02002390 encrypt_then_mac = transform->encrypt_then_mac;
2391#endif
Gilles Peskine449bd832023-01-11 14:50:10 +01002392 return mbedtls_ssl_get_actual_mode(base_mode, encrypt_then_mac);
Neil Armstrong4bf4c862022-04-01 10:35:48 +02002393}
2394
Neil Armstrongab555e02022-04-04 11:07:59 +02002395mbedtls_ssl_mode_t mbedtls_ssl_get_mode_from_ciphersuite(
Neil Armstrongf2c82f02022-04-05 11:16:53 +02002396#if defined(MBEDTLS_SSL_SOME_SUITES_USE_CBC_ETM)
Gilles Peskine449bd832023-01-11 14:50:10 +01002397 int encrypt_then_mac,
Neil Armstrongf2c82f02022-04-05 11:16:53 +02002398#endif /* MBEDTLS_SSL_SOME_SUITES_USE_CBC_ETM */
Gilles Peskine449bd832023-01-11 14:50:10 +01002399 const mbedtls_ssl_ciphersuite_t *suite)
Neil Armstrong4bf4c862022-04-01 10:35:48 +02002400{
Gilles Peskinee108d982022-04-26 16:50:40 +02002401 mbedtls_ssl_mode_t base_mode = MBEDTLS_SSL_MODE_STREAM;
2402
Neil Armstrong4bf4c862022-04-01 10:35:48 +02002403#if defined(MBEDTLS_USE_PSA_CRYPTO)
2404 psa_status_t status;
2405 psa_algorithm_t alg;
2406 psa_key_type_t type;
2407 size_t size;
Gilles Peskine449bd832023-01-11 14:50:10 +01002408 status = mbedtls_ssl_cipher_to_psa(suite->cipher, 0, &alg, &type, &size);
2409 if (status == PSA_SUCCESS) {
2410 base_mode = mbedtls_ssl_get_base_mode(alg);
2411 }
Neil Armstrong4bf4c862022-04-01 10:35:48 +02002412#else
2413 const mbedtls_cipher_info_t *cipher =
Gilles Peskine449bd832023-01-11 14:50:10 +01002414 mbedtls_cipher_info_from_type(suite->cipher);
2415 if (cipher != NULL) {
Gilles Peskinee108d982022-04-26 16:50:40 +02002416 base_mode =
2417 mbedtls_ssl_get_base_mode(
Gilles Peskine449bd832023-01-11 14:50:10 +01002418 mbedtls_cipher_info_get_mode(cipher));
Gilles Peskinee108d982022-04-26 16:50:40 +02002419 }
Neil Armstrong4bf4c862022-04-01 10:35:48 +02002420#endif /* MBEDTLS_USE_PSA_CRYPTO */
2421
Gilles Peskinee108d982022-04-26 16:50:40 +02002422#if !defined(MBEDTLS_SSL_SOME_SUITES_USE_CBC_ETM)
2423 int encrypt_then_mac = 0;
2424#endif
Gilles Peskine449bd832023-01-11 14:50:10 +01002425 return mbedtls_ssl_get_actual_mode(base_mode, encrypt_then_mac);
Neil Armstrong4bf4c862022-04-01 10:35:48 +02002426}
2427
Neil Armstrong8395d7a2022-05-18 11:44:56 +02002428#if defined(MBEDTLS_USE_PSA_CRYPTO) || defined(MBEDTLS_SSL_PROTO_TLS1_3)
Jerry Yu251a12e2022-07-13 15:15:48 +08002429
2430#if defined(MBEDTLS_SSL_PROTO_TLS1_3)
Jerry Yu438ddd82022-07-07 06:55:50 +00002431/* Serialization of TLS 1.3 sessions:
2432 *
2433 * struct {
Xiaokang Qian126bf8e2022-10-13 02:22:40 +00002434 * opaque hostname<0..2^16-1>;
Jerry Yu438ddd82022-07-07 06:55:50 +00002435 * uint64 ticket_received;
2436 * uint32 ticket_lifetime;
Jerry Yu34191072022-08-18 10:32:09 +08002437 * opaque ticket<1..2^16-1>;
Jerry Yu438ddd82022-07-07 06:55:50 +00002438 * } ClientOnlyData;
2439 *
2440 * struct {
2441 * uint8 endpoint;
2442 * uint8 ciphersuite[2];
2443 * uint32 ticket_age_add;
2444 * uint8 ticket_flags;
2445 * opaque resumption_key<0..255>;
2446 * select ( endpoint ) {
2447 * case client: ClientOnlyData;
2448 * case server: uint64 start_time;
2449 * };
2450 * } serialized_session_tls13;
2451 *
2452 */
2453#if defined(MBEDTLS_SSL_SESSION_TICKETS)
Jerry Yue36fdd62022-08-17 21:31:36 +08002454MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01002455static int ssl_tls13_session_save(const mbedtls_ssl_session *session,
2456 unsigned char *buf,
2457 size_t buf_len,
2458 size_t *olen)
Jerry Yu438ddd82022-07-07 06:55:50 +00002459{
2460 unsigned char *p = buf;
Xiaokang Qiana3b451f2022-10-11 06:20:56 +00002461#if defined(MBEDTLS_SSL_CLI_C) && \
2462 defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
Gilles Peskine449bd832023-01-11 14:50:10 +01002463 size_t hostname_len = (session->hostname == NULL) ?
2464 0 : strlen(session->hostname) + 1;
Xiaokang Qiana3b451f2022-10-11 06:20:56 +00002465#endif
Jerry Yubc7c1a42022-07-21 22:57:37 +08002466 size_t needed = 1 /* endpoint */
2467 + 2 /* ciphersuite */
2468 + 4 /* ticket_age_add */
Jerry Yu34191072022-08-18 10:32:09 +08002469 + 1 /* ticket_flags */
2470 + 1; /* resumption_key length */
Jerry Yue36fdd62022-08-17 21:31:36 +08002471 *olen = 0;
Jerry Yu34191072022-08-18 10:32:09 +08002472
Gilles Peskine449bd832023-01-11 14:50:10 +01002473 if (session->resumption_key_len > MBEDTLS_SSL_TLS1_3_TICKET_RESUMPTION_KEY_LEN) {
2474 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
2475 }
Jerry Yu34191072022-08-18 10:32:09 +08002476 needed += session->resumption_key_len; /* resumption_key */
2477
Jerry Yu438ddd82022-07-07 06:55:50 +00002478#if defined(MBEDTLS_HAVE_TIME)
2479 needed += 8; /* start_time or ticket_received */
2480#endif
2481
2482#if defined(MBEDTLS_SSL_CLI_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01002483 if (session->endpoint == MBEDTLS_SSL_IS_CLIENT) {
Xiaokang Qian126bf8e2022-10-13 02:22:40 +00002484#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
Xiaokang Qianbc663a02022-10-09 11:14:39 +00002485 needed += 2 /* hostname_len */
Gilles Peskine449bd832023-01-11 14:50:10 +01002486 + hostname_len; /* hostname */
Xiaokang Qian281fd1b2022-09-20 11:35:41 +00002487#endif
2488
Jerry Yu438ddd82022-07-07 06:55:50 +00002489 needed += 4 /* ticket_lifetime */
Jerry Yue36fdd62022-08-17 21:31:36 +08002490 + 2; /* ticket_len */
Jerry Yu34191072022-08-18 10:32:09 +08002491
2492 /* Check size_t overflow */
Gilles Peskine449bd832023-01-11 14:50:10 +01002493 if (session->ticket_len > SIZE_MAX - needed) {
2494 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
2495 }
Jerry Yu34191072022-08-18 10:32:09 +08002496
Jerry Yue28d9742022-08-18 15:44:03 +08002497 needed += session->ticket_len; /* ticket */
Jerry Yu438ddd82022-07-07 06:55:50 +00002498 }
2499#endif /* MBEDTLS_SSL_CLI_C */
2500
Jerry Yue36fdd62022-08-17 21:31:36 +08002501 *olen = needed;
Gilles Peskine449bd832023-01-11 14:50:10 +01002502 if (needed > buf_len) {
2503 return MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL;
2504 }
Jerry Yu438ddd82022-07-07 06:55:50 +00002505
2506 p[0] = session->endpoint;
Gilles Peskine449bd832023-01-11 14:50:10 +01002507 MBEDTLS_PUT_UINT16_BE(session->ciphersuite, p, 1);
2508 MBEDTLS_PUT_UINT32_BE(session->ticket_age_add, p, 3);
Jerry Yu438ddd82022-07-07 06:55:50 +00002509 p[7] = session->ticket_flags;
2510
2511 /* save resumption_key */
Jerry Yubc7c1a42022-07-21 22:57:37 +08002512 p[8] = session->resumption_key_len;
Jerry Yu438ddd82022-07-07 06:55:50 +00002513 p += 9;
Gilles Peskine449bd832023-01-11 14:50:10 +01002514 memcpy(p, session->resumption_key, session->resumption_key_len);
Jerry Yubc7c1a42022-07-21 22:57:37 +08002515 p += session->resumption_key_len;
Jerry Yu438ddd82022-07-07 06:55:50 +00002516
2517#if defined(MBEDTLS_HAVE_TIME) && defined(MBEDTLS_SSL_SRV_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01002518 if (session->endpoint == MBEDTLS_SSL_IS_SERVER) {
2519 MBEDTLS_PUT_UINT64_BE((uint64_t) session->start, p, 0);
Jerry Yu438ddd82022-07-07 06:55:50 +00002520 p += 8;
2521 }
2522#endif /* MBEDTLS_HAVE_TIME */
2523
2524#if defined(MBEDTLS_SSL_CLI_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01002525 if (session->endpoint == MBEDTLS_SSL_IS_CLIENT) {
Xiaokang Qianed0620c2022-10-12 06:58:13 +00002526#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
Gilles Peskine449bd832023-01-11 14:50:10 +01002527 MBEDTLS_PUT_UINT16_BE(hostname_len, p, 0);
Xiaokang Qianed0620c2022-10-12 06:58:13 +00002528 p += 2;
Gilles Peskine449bd832023-01-11 14:50:10 +01002529 if (hostname_len > 0) {
Xiaokang Qianed0620c2022-10-12 06:58:13 +00002530 /* save host name */
Gilles Peskine449bd832023-01-11 14:50:10 +01002531 memcpy(p, session->hostname, hostname_len);
Xiaokang Qianed0620c2022-10-12 06:58:13 +00002532 p += hostname_len;
2533 }
2534#endif /* MBEDTLS_SSL_SERVER_NAME_INDICATION */
2535
Jerry Yu438ddd82022-07-07 06:55:50 +00002536#if defined(MBEDTLS_HAVE_TIME)
Gilles Peskine449bd832023-01-11 14:50:10 +01002537 MBEDTLS_PUT_UINT64_BE((uint64_t) session->ticket_received, p, 0);
Jerry Yu438ddd82022-07-07 06:55:50 +00002538 p += 8;
2539#endif
Gilles Peskine449bd832023-01-11 14:50:10 +01002540 MBEDTLS_PUT_UINT32_BE(session->ticket_lifetime, p, 0);
Jerry Yu438ddd82022-07-07 06:55:50 +00002541 p += 4;
2542
Gilles Peskine449bd832023-01-11 14:50:10 +01002543 MBEDTLS_PUT_UINT16_BE(session->ticket_len, p, 0);
Jerry Yu438ddd82022-07-07 06:55:50 +00002544 p += 2;
Jerry Yu34191072022-08-18 10:32:09 +08002545
Gilles Peskine449bd832023-01-11 14:50:10 +01002546 if (session->ticket != NULL && session->ticket_len > 0) {
2547 memcpy(p, session->ticket, session->ticket_len);
Jerry Yu438ddd82022-07-07 06:55:50 +00002548 p += session->ticket_len;
2549 }
2550 }
2551#endif /* MBEDTLS_SSL_CLI_C */
Gilles Peskine449bd832023-01-11 14:50:10 +01002552 return 0;
Jerry Yu438ddd82022-07-07 06:55:50 +00002553}
2554
2555MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01002556static int ssl_tls13_session_load(mbedtls_ssl_session *session,
2557 const unsigned char *buf,
2558 size_t len)
Jerry Yu438ddd82022-07-07 06:55:50 +00002559{
2560 const unsigned char *p = buf;
2561 const unsigned char *end = buf + len;
2562
Gilles Peskine449bd832023-01-11 14:50:10 +01002563 if (end - p < 9) {
2564 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
2565 }
Jerry Yu438ddd82022-07-07 06:55:50 +00002566 session->endpoint = p[0];
Gilles Peskine449bd832023-01-11 14:50:10 +01002567 session->ciphersuite = MBEDTLS_GET_UINT16_BE(p, 1);
2568 session->ticket_age_add = MBEDTLS_GET_UINT32_BE(p, 3);
Jerry Yu438ddd82022-07-07 06:55:50 +00002569 session->ticket_flags = p[7];
2570
2571 /* load resumption_key */
Jerry Yubc7c1a42022-07-21 22:57:37 +08002572 session->resumption_key_len = p[8];
Jerry Yu438ddd82022-07-07 06:55:50 +00002573 p += 9;
2574
Gilles Peskine449bd832023-01-11 14:50:10 +01002575 if (end - p < session->resumption_key_len) {
2576 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
2577 }
Jerry Yu438ddd82022-07-07 06:55:50 +00002578
Gilles Peskine449bd832023-01-11 14:50:10 +01002579 if (sizeof(session->resumption_key) < session->resumption_key_len) {
2580 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
2581 }
2582 memcpy(session->resumption_key, p, session->resumption_key_len);
Jerry Yubc7c1a42022-07-21 22:57:37 +08002583 p += session->resumption_key_len;
Jerry Yu438ddd82022-07-07 06:55:50 +00002584
2585#if defined(MBEDTLS_HAVE_TIME) && defined(MBEDTLS_SSL_SRV_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01002586 if (session->endpoint == MBEDTLS_SSL_IS_SERVER) {
2587 if (end - p < 8) {
2588 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
2589 }
2590 session->start = MBEDTLS_GET_UINT64_BE(p, 0);
Jerry Yu438ddd82022-07-07 06:55:50 +00002591 p += 8;
2592 }
2593#endif /* MBEDTLS_HAVE_TIME */
2594
2595#if defined(MBEDTLS_SSL_CLI_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01002596 if (session->endpoint == MBEDTLS_SSL_IS_CLIENT) {
Xiaokang Qianed0620c2022-10-12 06:58:13 +00002597#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION) && \
Gilles Peskine449bd832023-01-11 14:50:10 +01002598 defined(MBEDTLS_SSL_SESSION_TICKETS)
Xiaokang Qianed0620c2022-10-12 06:58:13 +00002599 size_t hostname_len;
2600 /* load host name */
Gilles Peskine449bd832023-01-11 14:50:10 +01002601 if (end - p < 2) {
2602 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
2603 }
2604 hostname_len = MBEDTLS_GET_UINT16_BE(p, 0);
Xiaokang Qianed0620c2022-10-12 06:58:13 +00002605 p += 2;
2606
Gilles Peskine449bd832023-01-11 14:50:10 +01002607 if (end - p < (long int) hostname_len) {
2608 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
2609 }
2610 if (hostname_len > 0) {
2611 session->hostname = mbedtls_calloc(1, hostname_len);
2612 if (session->hostname == NULL) {
2613 return MBEDTLS_ERR_SSL_ALLOC_FAILED;
2614 }
2615 memcpy(session->hostname, p, hostname_len);
Xiaokang Qianed0620c2022-10-12 06:58:13 +00002616 p += hostname_len;
2617 }
2618#endif /* MBEDTLS_SSL_SERVER_NAME_INDICATION &&
2619 MBEDTLS_SSL_SESSION_TICKETS */
2620
Jerry Yu438ddd82022-07-07 06:55:50 +00002621#if defined(MBEDTLS_HAVE_TIME)
Gilles Peskine449bd832023-01-11 14:50:10 +01002622 if (end - p < 8) {
2623 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
2624 }
2625 session->ticket_received = MBEDTLS_GET_UINT64_BE(p, 0);
Jerry Yu438ddd82022-07-07 06:55:50 +00002626 p += 8;
2627#endif
Gilles Peskine449bd832023-01-11 14:50:10 +01002628 if (end - p < 4) {
2629 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
2630 }
2631 session->ticket_lifetime = MBEDTLS_GET_UINT32_BE(p, 0);
Jerry Yu438ddd82022-07-07 06:55:50 +00002632 p += 4;
2633
Gilles Peskine449bd832023-01-11 14:50:10 +01002634 if (end - p < 2) {
2635 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
2636 }
2637 session->ticket_len = MBEDTLS_GET_UINT16_BE(p, 0);
Jerry Yu438ddd82022-07-07 06:55:50 +00002638 p += 2;
2639
Gilles Peskine449bd832023-01-11 14:50:10 +01002640 if (end - p < (long int) session->ticket_len) {
2641 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
2642 }
2643 if (session->ticket_len > 0) {
2644 session->ticket = mbedtls_calloc(1, session->ticket_len);
2645 if (session->ticket == NULL) {
2646 return MBEDTLS_ERR_SSL_ALLOC_FAILED;
2647 }
2648 memcpy(session->ticket, p, session->ticket_len);
Jerry Yu438ddd82022-07-07 06:55:50 +00002649 p += session->ticket_len;
2650 }
2651 }
2652#endif /* MBEDTLS_SSL_CLI_C */
2653
Gilles Peskine449bd832023-01-11 14:50:10 +01002654 return 0;
Jerry Yu438ddd82022-07-07 06:55:50 +00002655
2656}
2657#else /* MBEDTLS_SSL_SESSION_TICKETS */
Jerry Yue36fdd62022-08-17 21:31:36 +08002658MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01002659static int ssl_tls13_session_save(const mbedtls_ssl_session *session,
2660 unsigned char *buf,
2661 size_t buf_len,
2662 size_t *olen)
Jerry Yu251a12e2022-07-13 15:15:48 +08002663{
2664 ((void) session);
2665 ((void) buf);
2666 ((void) buf_len);
Jerry Yue36fdd62022-08-17 21:31:36 +08002667 *olen = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +01002668 return MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
Jerry Yu251a12e2022-07-13 15:15:48 +08002669}
Jerry Yu438ddd82022-07-07 06:55:50 +00002670
Gilles Peskine449bd832023-01-11 14:50:10 +01002671static int ssl_tls13_session_load(const mbedtls_ssl_session *session,
2672 unsigned char *buf,
2673 size_t buf_len)
Jerry Yu438ddd82022-07-07 06:55:50 +00002674{
2675 ((void) session);
2676 ((void) buf);
2677 ((void) buf_len);
Gilles Peskine449bd832023-01-11 14:50:10 +01002678 return MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
Jerry Yu438ddd82022-07-07 06:55:50 +00002679}
2680#endif /* !MBEDTLS_SSL_SESSION_TICKETS */
Jerry Yu251a12e2022-07-13 15:15:48 +08002681#endif /* MBEDTLS_SSL_PROTO_TLS1_3 */
2682
Gilles Peskine449bd832023-01-11 14:50:10 +01002683psa_status_t mbedtls_ssl_cipher_to_psa(mbedtls_cipher_type_t mbedtls_cipher_type,
2684 size_t taglen,
2685 psa_algorithm_t *alg,
2686 psa_key_type_t *key_type,
2687 size_t *key_size)
Przemyslaw Stekiel430f3372022-01-10 11:55:46 +01002688{
Gilles Peskine449bd832023-01-11 14:50:10 +01002689 switch (mbedtls_cipher_type) {
Przemyslaw Stekiel430f3372022-01-10 11:55:46 +01002690 case MBEDTLS_CIPHER_AES_128_CBC:
2691 *alg = PSA_ALG_CBC_NO_PADDING;
2692 *key_type = PSA_KEY_TYPE_AES;
2693 *key_size = 128;
2694 break;
2695 case MBEDTLS_CIPHER_AES_128_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_AES;
2698 *key_size = 128;
2699 break;
2700 case MBEDTLS_CIPHER_AES_128_GCM:
2701 *alg = PSA_ALG_GCM;
2702 *key_type = PSA_KEY_TYPE_AES;
2703 *key_size = 128;
2704 break;
2705 case MBEDTLS_CIPHER_AES_192_CCM:
Gilles Peskine449bd832023-01-11 14:50:10 +01002706 *alg = taglen ? PSA_ALG_AEAD_WITH_SHORTENED_TAG(PSA_ALG_CCM, taglen) : PSA_ALG_CCM;
Przemyslaw Stekiel430f3372022-01-10 11:55:46 +01002707 *key_type = PSA_KEY_TYPE_AES;
2708 *key_size = 192;
2709 break;
2710 case MBEDTLS_CIPHER_AES_192_GCM:
2711 *alg = PSA_ALG_GCM;
2712 *key_type = PSA_KEY_TYPE_AES;
2713 *key_size = 192;
2714 break;
2715 case MBEDTLS_CIPHER_AES_256_CBC:
2716 *alg = PSA_ALG_CBC_NO_PADDING;
2717 *key_type = PSA_KEY_TYPE_AES;
2718 *key_size = 256;
2719 break;
2720 case MBEDTLS_CIPHER_AES_256_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_AES;
2723 *key_size = 256;
2724 break;
2725 case MBEDTLS_CIPHER_AES_256_GCM:
2726 *alg = PSA_ALG_GCM;
2727 *key_type = PSA_KEY_TYPE_AES;
2728 *key_size = 256;
2729 break;
2730 case MBEDTLS_CIPHER_ARIA_128_CBC:
2731 *alg = PSA_ALG_CBC_NO_PADDING;
2732 *key_type = PSA_KEY_TYPE_ARIA;
2733 *key_size = 128;
2734 break;
2735 case MBEDTLS_CIPHER_ARIA_128_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_ARIA;
2738 *key_size = 128;
2739 break;
2740 case MBEDTLS_CIPHER_ARIA_128_GCM:
2741 *alg = PSA_ALG_GCM;
2742 *key_type = PSA_KEY_TYPE_ARIA;
2743 *key_size = 128;
2744 break;
2745 case MBEDTLS_CIPHER_ARIA_192_CCM:
Gilles Peskine449bd832023-01-11 14:50:10 +01002746 *alg = taglen ? PSA_ALG_AEAD_WITH_SHORTENED_TAG(PSA_ALG_CCM, taglen) : PSA_ALG_CCM;
Przemyslaw Stekiel430f3372022-01-10 11:55:46 +01002747 *key_type = PSA_KEY_TYPE_ARIA;
2748 *key_size = 192;
2749 break;
2750 case MBEDTLS_CIPHER_ARIA_192_GCM:
2751 *alg = PSA_ALG_GCM;
2752 *key_type = PSA_KEY_TYPE_ARIA;
2753 *key_size = 192;
2754 break;
2755 case MBEDTLS_CIPHER_ARIA_256_CBC:
2756 *alg = PSA_ALG_CBC_NO_PADDING;
2757 *key_type = PSA_KEY_TYPE_ARIA;
2758 *key_size = 256;
2759 break;
2760 case MBEDTLS_CIPHER_ARIA_256_CCM:
Gilles Peskine449bd832023-01-11 14:50:10 +01002761 *alg = taglen ? PSA_ALG_AEAD_WITH_SHORTENED_TAG(PSA_ALG_CCM, taglen) : PSA_ALG_CCM;
Przemyslaw Stekiel430f3372022-01-10 11:55:46 +01002762 *key_type = PSA_KEY_TYPE_ARIA;
2763 *key_size = 256;
2764 break;
2765 case MBEDTLS_CIPHER_ARIA_256_GCM:
2766 *alg = PSA_ALG_GCM;
2767 *key_type = PSA_KEY_TYPE_ARIA;
2768 *key_size = 256;
2769 break;
2770 case MBEDTLS_CIPHER_CAMELLIA_128_CBC:
2771 *alg = PSA_ALG_CBC_NO_PADDING;
2772 *key_type = PSA_KEY_TYPE_CAMELLIA;
2773 *key_size = 128;
2774 break;
2775 case MBEDTLS_CIPHER_CAMELLIA_128_CCM:
Gilles Peskine449bd832023-01-11 14:50:10 +01002776 *alg = taglen ? PSA_ALG_AEAD_WITH_SHORTENED_TAG(PSA_ALG_CCM, taglen) : PSA_ALG_CCM;
Przemyslaw Stekiel430f3372022-01-10 11:55:46 +01002777 *key_type = PSA_KEY_TYPE_CAMELLIA;
2778 *key_size = 128;
2779 break;
2780 case MBEDTLS_CIPHER_CAMELLIA_128_GCM:
2781 *alg = PSA_ALG_GCM;
2782 *key_type = PSA_KEY_TYPE_CAMELLIA;
2783 *key_size = 128;
2784 break;
2785 case MBEDTLS_CIPHER_CAMELLIA_192_CCM:
Gilles Peskine449bd832023-01-11 14:50:10 +01002786 *alg = taglen ? PSA_ALG_AEAD_WITH_SHORTENED_TAG(PSA_ALG_CCM, taglen) : PSA_ALG_CCM;
Przemyslaw Stekiel430f3372022-01-10 11:55:46 +01002787 *key_type = PSA_KEY_TYPE_CAMELLIA;
2788 *key_size = 192;
2789 break;
2790 case MBEDTLS_CIPHER_CAMELLIA_192_GCM:
2791 *alg = PSA_ALG_GCM;
2792 *key_type = PSA_KEY_TYPE_CAMELLIA;
2793 *key_size = 192;
2794 break;
2795 case MBEDTLS_CIPHER_CAMELLIA_256_CBC:
2796 *alg = PSA_ALG_CBC_NO_PADDING;
2797 *key_type = PSA_KEY_TYPE_CAMELLIA;
2798 *key_size = 256;
2799 break;
2800 case MBEDTLS_CIPHER_CAMELLIA_256_CCM:
Gilles Peskine449bd832023-01-11 14:50:10 +01002801 *alg = taglen ? PSA_ALG_AEAD_WITH_SHORTENED_TAG(PSA_ALG_CCM, taglen) : PSA_ALG_CCM;
Przemyslaw Stekiel430f3372022-01-10 11:55:46 +01002802 *key_type = PSA_KEY_TYPE_CAMELLIA;
2803 *key_size = 256;
2804 break;
2805 case MBEDTLS_CIPHER_CAMELLIA_256_GCM:
2806 *alg = PSA_ALG_GCM;
2807 *key_type = PSA_KEY_TYPE_CAMELLIA;
2808 *key_size = 256;
2809 break;
2810 case MBEDTLS_CIPHER_CHACHA20_POLY1305:
2811 *alg = PSA_ALG_CHACHA20_POLY1305;
2812 *key_type = PSA_KEY_TYPE_CHACHA20;
2813 *key_size = 256;
2814 break;
2815 case MBEDTLS_CIPHER_NULL:
2816 *alg = MBEDTLS_SSL_NULL_CIPHER;
2817 *key_type = 0;
2818 *key_size = 0;
2819 break;
2820 default:
2821 return PSA_ERROR_NOT_SUPPORTED;
2822 }
2823
2824 return PSA_SUCCESS;
2825}
Neil Armstrong8395d7a2022-05-18 11:44:56 +02002826#endif /* MBEDTLS_USE_PSA_CRYPTO || MBEDTLS_SSL_PROTO_TLS1_3 */
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01002827
Manuel Pégourié-Gonnardcf141ca2015-05-20 10:35:51 +02002828#if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_SSL_SRV_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01002829int mbedtls_ssl_conf_dh_param_bin(mbedtls_ssl_config *conf,
2830 const unsigned char *dhm_P, size_t P_len,
2831 const unsigned char *dhm_G, size_t G_len)
Hanno Beckera90658f2017-10-04 15:29:08 +01002832{
Janos Follath865b3eb2019-12-16 11:46:15 +00002833 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Hanno Beckera90658f2017-10-04 15:29:08 +01002834
Gilles Peskine449bd832023-01-11 14:50:10 +01002835 mbedtls_mpi_free(&conf->dhm_P);
2836 mbedtls_mpi_free(&conf->dhm_G);
Glenn Strausscee11292021-12-20 01:43:17 -05002837
Gilles Peskine449bd832023-01-11 14:50:10 +01002838 if ((ret = mbedtls_mpi_read_binary(&conf->dhm_P, dhm_P, P_len)) != 0 ||
2839 (ret = mbedtls_mpi_read_binary(&conf->dhm_G, dhm_G, G_len)) != 0) {
2840 mbedtls_mpi_free(&conf->dhm_P);
2841 mbedtls_mpi_free(&conf->dhm_G);
2842 return ret;
Hanno Beckera90658f2017-10-04 15:29:08 +01002843 }
2844
Gilles Peskine449bd832023-01-11 14:50:10 +01002845 return 0;
Hanno Beckera90658f2017-10-04 15:29:08 +01002846}
Paul Bakker5121ce52009-01-03 21:22:43 +00002847
Gilles Peskine449bd832023-01-11 14:50:10 +01002848int mbedtls_ssl_conf_dh_param_ctx(mbedtls_ssl_config *conf, mbedtls_dhm_context *dhm_ctx)
Paul Bakker1b57b062011-01-06 15:48:19 +00002849{
Janos Follath865b3eb2019-12-16 11:46:15 +00002850 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker1b57b062011-01-06 15:48:19 +00002851
Gilles Peskine449bd832023-01-11 14:50:10 +01002852 mbedtls_mpi_free(&conf->dhm_P);
2853 mbedtls_mpi_free(&conf->dhm_G);
Glenn Strausscee11292021-12-20 01:43:17 -05002854
Gilles Peskine449bd832023-01-11 14:50:10 +01002855 if ((ret = mbedtls_dhm_get_value(dhm_ctx, MBEDTLS_DHM_PARAM_P,
2856 &conf->dhm_P)) != 0 ||
2857 (ret = mbedtls_dhm_get_value(dhm_ctx, MBEDTLS_DHM_PARAM_G,
2858 &conf->dhm_G)) != 0) {
2859 mbedtls_mpi_free(&conf->dhm_P);
2860 mbedtls_mpi_free(&conf->dhm_G);
2861 return ret;
Manuel Pégourié-Gonnard1028b742015-05-06 17:33:07 +01002862 }
Paul Bakker1b57b062011-01-06 15:48:19 +00002863
Gilles Peskine449bd832023-01-11 14:50:10 +01002864 return 0;
Paul Bakker1b57b062011-01-06 15:48:19 +00002865}
Manuel Pégourié-Gonnardcf141ca2015-05-20 10:35:51 +02002866#endif /* MBEDTLS_DHM_C && MBEDTLS_SSL_SRV_C */
Paul Bakker1b57b062011-01-06 15:48:19 +00002867
Manuel Pégourié-Gonnardbd990d62015-06-11 14:49:42 +02002868#if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_SSL_CLI_C)
2869/*
2870 * Set the minimum length for Diffie-Hellman parameters
2871 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002872void mbedtls_ssl_conf_dhm_min_bitlen(mbedtls_ssl_config *conf,
2873 unsigned int bitlen)
Manuel Pégourié-Gonnardbd990d62015-06-11 14:49:42 +02002874{
2875 conf->dhm_min_bitlen = bitlen;
2876}
2877#endif /* MBEDTLS_DHM_C && MBEDTLS_SSL_CLI_C */
2878
Ronald Crone68ab4f2022-10-05 12:46:29 +02002879#if defined(MBEDTLS_SSL_HANDSHAKE_WITH_CERT_ENABLED)
Jerry Yu7ddc38c2022-01-19 11:08:05 +08002880#if !defined(MBEDTLS_DEPRECATED_REMOVED) && defined(MBEDTLS_SSL_PROTO_TLS1_2)
Manuel Pégourié-Gonnard36a8b572015-06-17 12:43:26 +02002881/*
2882 * Set allowed/preferred hashes for handshake signatures
2883 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002884void mbedtls_ssl_conf_sig_hashes(mbedtls_ssl_config *conf,
2885 const int *hashes)
Manuel Pégourié-Gonnard36a8b572015-06-17 12:43:26 +02002886{
2887 conf->sig_hashes = hashes;
2888}
Jerry Yu7ddc38c2022-01-19 11:08:05 +08002889#endif /* !MBEDTLS_DEPRECATED_REMOVED && MBEDTLS_SSL_PROTO_TLS1_2 */
Hanno Becker1cd6e002021-08-10 13:27:10 +01002890
Jerry Yuf017ee42022-01-12 15:49:48 +08002891/* Configure allowed signature algorithms for handshake */
Gilles Peskine449bd832023-01-11 14:50:10 +01002892void mbedtls_ssl_conf_sig_algs(mbedtls_ssl_config *conf,
2893 const uint16_t *sig_algs)
Hanno Becker1cd6e002021-08-10 13:27:10 +01002894{
Jerry Yuf017ee42022-01-12 15:49:48 +08002895#if !defined(MBEDTLS_DEPRECATED_REMOVED)
2896 conf->sig_hashes = NULL;
2897#endif /* !MBEDTLS_DEPRECATED_REMOVED */
2898 conf->sig_algs = sig_algs;
Hanno Becker1cd6e002021-08-10 13:27:10 +01002899}
Ronald Crone68ab4f2022-10-05 12:46:29 +02002900#endif /* MBEDTLS_SSL_HANDSHAKE_WITH_CERT_ENABLED */
Manuel Pégourié-Gonnard36a8b572015-06-17 12:43:26 +02002901
Manuel Pégourié-Gonnardb541da62015-06-17 11:43:30 +02002902#if defined(MBEDTLS_ECP_C)
Brett Warrene0edc842021-08-17 09:53:13 +01002903#if !defined(MBEDTLS_DEPRECATED_REMOVED)
Manuel Pégourié-Gonnard7f38ed02014-02-04 15:52:33 +01002904/*
2905 * Set the allowed elliptic curves
Brett Warrene0edc842021-08-17 09:53:13 +01002906 *
2907 * mbedtls_ssl_setup() takes the provided list
2908 * and translates it to a list of IANA TLS group identifiers,
2909 * stored in ssl->handshake->group_list.
2910 *
Manuel Pégourié-Gonnard7f38ed02014-02-04 15:52:33 +01002911 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002912void mbedtls_ssl_conf_curves(mbedtls_ssl_config *conf,
2913 const mbedtls_ecp_group_id *curve_list)
Manuel Pégourié-Gonnard7f38ed02014-02-04 15:52:33 +01002914{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02002915 conf->curve_list = curve_list;
Brett Warrene0edc842021-08-17 09:53:13 +01002916 conf->group_list = NULL;
Manuel Pégourié-Gonnard7f38ed02014-02-04 15:52:33 +01002917}
Brett Warrene0edc842021-08-17 09:53:13 +01002918#endif /* MBEDTLS_DEPRECATED_REMOVED */
Hanno Becker947194e2017-04-07 13:25:49 +01002919#endif /* MBEDTLS_ECP_C */
Manuel Pégourié-Gonnard7f38ed02014-02-04 15:52:33 +01002920
Brett Warrene0edc842021-08-17 09:53:13 +01002921/*
2922 * Set the allowed groups
2923 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002924void mbedtls_ssl_conf_groups(mbedtls_ssl_config *conf,
2925 const uint16_t *group_list)
Brett Warrene0edc842021-08-17 09:53:13 +01002926{
2927#if defined(MBEDTLS_ECP_C) && !defined(MBEDTLS_DEPRECATED_REMOVED)
2928 conf->curve_list = NULL;
2929#endif
2930 conf->group_list = group_list;
2931}
2932
Manuel Pégourié-Gonnardbc2b7712015-05-06 11:14:19 +01002933#if defined(MBEDTLS_X509_CRT_PARSE_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01002934int mbedtls_ssl_set_hostname(mbedtls_ssl_context *ssl, const char *hostname)
Paul Bakker5121ce52009-01-03 21:22:43 +00002935{
Hanno Becker947194e2017-04-07 13:25:49 +01002936 /* Initialize to suppress unnecessary compiler warning */
2937 size_t hostname_len = 0;
2938
2939 /* Check if new hostname is valid before
2940 * making any change to current one */
Gilles Peskine449bd832023-01-11 14:50:10 +01002941 if (hostname != NULL) {
2942 hostname_len = strlen(hostname);
Hanno Becker947194e2017-04-07 13:25:49 +01002943
Gilles Peskine449bd832023-01-11 14:50:10 +01002944 if (hostname_len > MBEDTLS_SSL_MAX_HOST_NAME_LEN) {
2945 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
2946 }
Hanno Becker947194e2017-04-07 13:25:49 +01002947 }
2948
2949 /* Now it's clear that we will overwrite the old hostname,
2950 * so we can free it safely */
2951
Gilles Peskine449bd832023-01-11 14:50:10 +01002952 if (ssl->hostname != NULL) {
2953 mbedtls_platform_zeroize(ssl->hostname, strlen(ssl->hostname));
2954 mbedtls_free(ssl->hostname);
Hanno Becker947194e2017-04-07 13:25:49 +01002955 }
2956
2957 /* Passing NULL as hostname shall clear the old one */
Manuel Pégourié-Gonnardba26c242015-05-06 10:47:06 +01002958
Gilles Peskine449bd832023-01-11 14:50:10 +01002959 if (hostname == NULL) {
Hanno Becker947194e2017-04-07 13:25:49 +01002960 ssl->hostname = NULL;
Gilles Peskine449bd832023-01-11 14:50:10 +01002961 } else {
2962 ssl->hostname = mbedtls_calloc(1, hostname_len + 1);
2963 if (ssl->hostname == NULL) {
2964 return MBEDTLS_ERR_SSL_ALLOC_FAILED;
2965 }
Paul Bakker75c1a6f2013-08-19 14:25:29 +02002966
Gilles Peskine449bd832023-01-11 14:50:10 +01002967 memcpy(ssl->hostname, hostname, hostname_len);
Paul Bakker75c1a6f2013-08-19 14:25:29 +02002968
Hanno Becker947194e2017-04-07 13:25:49 +01002969 ssl->hostname[hostname_len] = '\0';
2970 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002971
Gilles Peskine449bd832023-01-11 14:50:10 +01002972 return 0;
Paul Bakker5121ce52009-01-03 21:22:43 +00002973}
Hanno Becker1a9a51c2017-04-07 13:02:16 +01002974#endif /* MBEDTLS_X509_CRT_PARSE_C */
Paul Bakker5121ce52009-01-03 21:22:43 +00002975
Manuel Pégourié-Gonnardbc2b7712015-05-06 11:14:19 +01002976#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
Gilles Peskine449bd832023-01-11 14:50:10 +01002977void mbedtls_ssl_conf_sni(mbedtls_ssl_config *conf,
2978 int (*f_sni)(void *, mbedtls_ssl_context *,
2979 const unsigned char *, size_t),
2980 void *p_sni)
Paul Bakker5701cdc2012-09-27 21:49:42 +00002981{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02002982 conf->f_sni = f_sni;
2983 conf->p_sni = p_sni;
Paul Bakker5701cdc2012-09-27 21:49:42 +00002984}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002985#endif /* MBEDTLS_SSL_SERVER_NAME_INDICATION */
Paul Bakker5701cdc2012-09-27 21:49:42 +00002986
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002987#if defined(MBEDTLS_SSL_ALPN)
Gilles Peskine449bd832023-01-11 14:50:10 +01002988int mbedtls_ssl_conf_alpn_protocols(mbedtls_ssl_config *conf, const char **protos)
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02002989{
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +02002990 size_t cur_len, tot_len;
2991 const char **p;
2992
2993 /*
Brian J Murray1903fb32016-11-06 04:45:15 -08002994 * RFC 7301 3.1: "Empty strings MUST NOT be included and byte strings
2995 * MUST NOT be truncated."
2996 * We check lengths now rather than later.
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +02002997 */
2998 tot_len = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +01002999 for (p = protos; *p != NULL; p++) {
3000 cur_len = strlen(*p);
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +02003001 tot_len += cur_len;
3002
Gilles Peskine449bd832023-01-11 14:50:10 +01003003 if ((cur_len == 0) ||
3004 (cur_len > MBEDTLS_SSL_MAX_ALPN_NAME_LEN) ||
3005 (tot_len > MBEDTLS_SSL_MAX_ALPN_LIST_LEN)) {
3006 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
3007 }
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +02003008 }
3009
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02003010 conf->alpn_list = protos;
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +02003011
Gilles Peskine449bd832023-01-11 14:50:10 +01003012 return 0;
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02003013}
3014
Gilles Peskine449bd832023-01-11 14:50:10 +01003015const char *mbedtls_ssl_get_alpn_protocol(const mbedtls_ssl_context *ssl)
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02003016{
Gilles Peskine449bd832023-01-11 14:50:10 +01003017 return ssl->alpn_chosen;
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02003018}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003019#endif /* MBEDTLS_SSL_ALPN */
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02003020
Johan Pascalb62bb512015-12-03 21:56:45 +01003021#if defined(MBEDTLS_SSL_DTLS_SRTP)
Gilles Peskine449bd832023-01-11 14:50:10 +01003022void mbedtls_ssl_conf_srtp_mki_value_supported(mbedtls_ssl_config *conf,
3023 int support_mki_value)
Ron Eldor591f1622018-01-22 12:30:04 +02003024{
3025 conf->dtls_srtp_mki_support = support_mki_value;
3026}
3027
Gilles Peskine449bd832023-01-11 14:50:10 +01003028int mbedtls_ssl_dtls_srtp_set_mki_value(mbedtls_ssl_context *ssl,
3029 unsigned char *mki_value,
3030 uint16_t mki_len)
Ron Eldor591f1622018-01-22 12:30:04 +02003031{
Gilles Peskine449bd832023-01-11 14:50:10 +01003032 if (mki_len > MBEDTLS_TLS_SRTP_MAX_MKI_LENGTH) {
3033 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
Ron Eldora9788042018-12-05 11:04:31 +02003034 }
Ron Eldor591f1622018-01-22 12:30:04 +02003035
Gilles Peskine449bd832023-01-11 14:50:10 +01003036 if (ssl->conf->dtls_srtp_mki_support == MBEDTLS_SSL_DTLS_SRTP_MKI_UNSUPPORTED) {
3037 return MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
Ron Eldora9788042018-12-05 11:04:31 +02003038 }
Ron Eldor591f1622018-01-22 12:30:04 +02003039
Gilles Peskine449bd832023-01-11 14:50:10 +01003040 memcpy(ssl->dtls_srtp_info.mki_value, mki_value, mki_len);
Ron Eldor591f1622018-01-22 12:30:04 +02003041 ssl->dtls_srtp_info.mki_len = mki_len;
Gilles Peskine449bd832023-01-11 14:50:10 +01003042 return 0;
Ron Eldor591f1622018-01-22 12:30:04 +02003043}
3044
Gilles Peskine449bd832023-01-11 14:50:10 +01003045int mbedtls_ssl_conf_dtls_srtp_protection_profiles(mbedtls_ssl_config *conf,
3046 const mbedtls_ssl_srtp_profile *profiles)
Johan Pascalb62bb512015-12-03 21:56:45 +01003047{
Johan Pascal253d0262020-09-22 13:04:45 +02003048 const mbedtls_ssl_srtp_profile *p;
3049 size_t list_size = 0;
Johan Pascalb62bb512015-12-03 21:56:45 +01003050
Johan Pascal253d0262020-09-22 13:04:45 +02003051 /* check the profiles list: all entry must be valid,
3052 * its size cannot be more than the total number of supported profiles, currently 4 */
Gilles Peskine449bd832023-01-11 14:50:10 +01003053 for (p = profiles; *p != MBEDTLS_TLS_SRTP_UNSET &&
3054 list_size <= MBEDTLS_TLS_SRTP_MAX_PROFILE_LIST_LENGTH;
3055 p++) {
3056 if (mbedtls_ssl_check_srtp_profile_value(*p) != MBEDTLS_TLS_SRTP_UNSET) {
Johan Pascal76fdf1d2020-10-22 23:31:00 +02003057 list_size++;
Gilles Peskine449bd832023-01-11 14:50:10 +01003058 } else {
Johan Pascal76fdf1d2020-10-22 23:31:00 +02003059 /* unsupported value, stop parsing and set the size to an error value */
3060 list_size = MBEDTLS_TLS_SRTP_MAX_PROFILE_LIST_LENGTH + 1;
Johan Pascalb62bb512015-12-03 21:56:45 +01003061 }
3062 }
3063
Gilles Peskine449bd832023-01-11 14:50:10 +01003064 if (list_size > MBEDTLS_TLS_SRTP_MAX_PROFILE_LIST_LENGTH) {
3065 conf->dtls_srtp_profile_list = NULL;
3066 conf->dtls_srtp_profile_list_len = 0;
3067 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
Johan Pascal253d0262020-09-22 13:04:45 +02003068 }
3069
Johan Pascal9bc97ca2020-09-21 23:44:45 +02003070 conf->dtls_srtp_profile_list = profiles;
Johan Pascal253d0262020-09-22 13:04:45 +02003071 conf->dtls_srtp_profile_list_len = list_size;
Johan Pascalb62bb512015-12-03 21:56:45 +01003072
Gilles Peskine449bd832023-01-11 14:50:10 +01003073 return 0;
Johan Pascalb62bb512015-12-03 21:56:45 +01003074}
3075
Gilles Peskine449bd832023-01-11 14:50:10 +01003076void mbedtls_ssl_get_dtls_srtp_negotiation_result(const mbedtls_ssl_context *ssl,
3077 mbedtls_dtls_srtp_info *dtls_srtp_info)
Johan Pascalb62bb512015-12-03 21:56:45 +01003078{
Johan Pascal2258a4f2020-10-28 13:53:09 +01003079 dtls_srtp_info->chosen_dtls_srtp_profile = ssl->dtls_srtp_info.chosen_dtls_srtp_profile;
3080 /* do not copy the mki value if there is no chosen profile */
Gilles Peskine449bd832023-01-11 14:50:10 +01003081 if (dtls_srtp_info->chosen_dtls_srtp_profile == MBEDTLS_TLS_SRTP_UNSET) {
Johan Pascal2258a4f2020-10-28 13:53:09 +01003082 dtls_srtp_info->mki_len = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +01003083 } else {
Johan Pascal2258a4f2020-10-28 13:53:09 +01003084 dtls_srtp_info->mki_len = ssl->dtls_srtp_info.mki_len;
Gilles Peskine449bd832023-01-11 14:50:10 +01003085 memcpy(dtls_srtp_info->mki_value, ssl->dtls_srtp_info.mki_value,
3086 ssl->dtls_srtp_info.mki_len);
Johan Pascal2258a4f2020-10-28 13:53:09 +01003087 }
Johan Pascalb62bb512015-12-03 21:56:45 +01003088}
Johan Pascalb62bb512015-12-03 21:56:45 +01003089#endif /* MBEDTLS_SSL_DTLS_SRTP */
3090
Aditya Patwardhan3096f332022-07-26 14:31:46 +05303091#if !defined(MBEDTLS_DEPRECATED_REMOVED)
Gilles Peskine449bd832023-01-11 14:50:10 +01003092void mbedtls_ssl_conf_max_version(mbedtls_ssl_config *conf, int major, int minor)
Paul Bakker490ecc82011-10-06 13:04:09 +00003093{
Glenn Strauss2dfcea22022-03-14 17:26:42 -04003094 conf->max_tls_version = (major << 8) | minor;
Paul Bakker490ecc82011-10-06 13:04:09 +00003095}
3096
Gilles Peskine449bd832023-01-11 14:50:10 +01003097void mbedtls_ssl_conf_min_version(mbedtls_ssl_config *conf, int major, int minor)
Paul Bakker1d29fb52012-09-28 13:28:45 +00003098{
Glenn Strauss2dfcea22022-03-14 17:26:42 -04003099 conf->min_tls_version = (major << 8) | minor;
Paul Bakker1d29fb52012-09-28 13:28:45 +00003100}
Aditya Patwardhan3096f332022-07-26 14:31:46 +05303101#endif /* MBEDTLS_DEPRECATED_REMOVED */
Paul Bakker1d29fb52012-09-28 13:28:45 +00003102
Janos Follath088ce432017-04-10 12:42:31 +01003103#if defined(MBEDTLS_SSL_SRV_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01003104void mbedtls_ssl_conf_cert_req_ca_list(mbedtls_ssl_config *conf,
3105 char cert_req_ca_list)
Janos Follath088ce432017-04-10 12:42:31 +01003106{
3107 conf->cert_req_ca_list = cert_req_ca_list;
3108}
3109#endif
3110
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003111#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
Gilles Peskine449bd832023-01-11 14:50:10 +01003112void mbedtls_ssl_conf_encrypt_then_mac(mbedtls_ssl_config *conf, char etm)
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +01003113{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02003114 conf->encrypt_then_mac = etm;
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +01003115}
3116#endif
3117
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003118#if defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET)
Gilles Peskine449bd832023-01-11 14:50:10 +01003119void mbedtls_ssl_conf_extended_master_secret(mbedtls_ssl_config *conf, char ems)
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +02003120{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02003121 conf->extended_ms = ems;
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +02003122}
3123#endif
3124
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003125#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
Gilles Peskine449bd832023-01-11 14:50:10 +01003126int mbedtls_ssl_conf_max_frag_len(mbedtls_ssl_config *conf, unsigned char mfl_code)
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02003127{
Gilles Peskine449bd832023-01-11 14:50:10 +01003128 if (mfl_code >= MBEDTLS_SSL_MAX_FRAG_LEN_INVALID ||
3129 ssl_mfl_code_to_length(mfl_code) > MBEDTLS_TLS_EXT_ADV_CONTENT_LEN) {
3130 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02003131 }
3132
Manuel Pégourié-Gonnard6bf89d62015-05-05 17:01:57 +01003133 conf->mfl_code = mfl_code;
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02003134
Gilles Peskine449bd832023-01-11 14:50:10 +01003135 return 0;
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02003136}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003137#endif /* MBEDTLS_SSL_MAX_FRAGMENT_LENGTH */
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02003138
Gilles Peskine449bd832023-01-11 14:50:10 +01003139void mbedtls_ssl_conf_legacy_renegotiation(mbedtls_ssl_config *conf, int allow_legacy)
Paul Bakker48916f92012-09-16 19:57:18 +00003140{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02003141 conf->allow_legacy_renegotiation = allow_legacy;
Paul Bakker48916f92012-09-16 19:57:18 +00003142}
3143
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003144#if defined(MBEDTLS_SSL_RENEGOTIATION)
Gilles Peskine449bd832023-01-11 14:50:10 +01003145void mbedtls_ssl_conf_renegotiation(mbedtls_ssl_config *conf, int renegotiation)
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01003146{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02003147 conf->disable_renegotiation = renegotiation;
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01003148}
3149
Gilles Peskine449bd832023-01-11 14:50:10 +01003150void mbedtls_ssl_conf_renegotiation_enforced(mbedtls_ssl_config *conf, int max_records)
Manuel Pégourié-Gonnarda9964db2014-07-03 19:29:16 +02003151{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02003152 conf->renego_max_records = max_records;
Manuel Pégourié-Gonnarda9964db2014-07-03 19:29:16 +02003153}
3154
Gilles Peskine449bd832023-01-11 14:50:10 +01003155void mbedtls_ssl_conf_renegotiation_period(mbedtls_ssl_config *conf,
3156 const unsigned char period[8])
Manuel Pégourié-Gonnard837f0fe2014-11-05 13:58:53 +01003157{
Gilles Peskine449bd832023-01-11 14:50:10 +01003158 memcpy(conf->renego_period, period, 8);
Manuel Pégourié-Gonnard837f0fe2014-11-05 13:58:53 +01003159}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003160#endif /* MBEDTLS_SSL_RENEGOTIATION */
Paul Bakker5121ce52009-01-03 21:22:43 +00003161
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003162#if defined(MBEDTLS_SSL_SESSION_TICKETS)
Manuel Pégourié-Gonnardb596abf2015-05-20 10:45:29 +02003163#if defined(MBEDTLS_SSL_CLI_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01003164void mbedtls_ssl_conf_session_tickets(mbedtls_ssl_config *conf, int use_tickets)
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +02003165{
Manuel Pégourié-Gonnard2b494452015-05-06 10:05:11 +01003166 conf->session_tickets = use_tickets;
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +02003167}
Manuel Pégourié-Gonnardb596abf2015-05-20 10:45:29 +02003168#endif
Paul Bakker606b4ba2013-08-14 16:52:14 +02003169
Manuel Pégourié-Gonnardb596abf2015-05-20 10:45:29 +02003170#if defined(MBEDTLS_SSL_SRV_C)
Jerry Yu1ad7ace2022-08-09 13:28:39 +08003171
Jerry Yud0766ec2022-09-22 10:46:57 +08003172#if defined(MBEDTLS_SSL_PROTO_TLS1_3) && defined(MBEDTLS_SSL_SESSION_TICKETS)
Gilles Peskine449bd832023-01-11 14:50:10 +01003173void mbedtls_ssl_conf_new_session_tickets(mbedtls_ssl_config *conf,
3174 uint16_t num_tickets)
Jerry Yu1ad7ace2022-08-09 13:28:39 +08003175{
Jerry Yud0766ec2022-09-22 10:46:57 +08003176 conf->new_session_tickets_count = num_tickets;
Jerry Yu1ad7ace2022-08-09 13:28:39 +08003177}
3178#endif
3179
Gilles Peskine449bd832023-01-11 14:50:10 +01003180void mbedtls_ssl_conf_session_tickets_cb(mbedtls_ssl_config *conf,
3181 mbedtls_ssl_ticket_write_t *f_ticket_write,
3182 mbedtls_ssl_ticket_parse_t *f_ticket_parse,
3183 void *p_ticket)
Paul Bakker606b4ba2013-08-14 16:52:14 +02003184{
Manuel Pégourié-Gonnardd59675d2015-05-19 15:28:00 +02003185 conf->f_ticket_write = f_ticket_write;
3186 conf->f_ticket_parse = f_ticket_parse;
3187 conf->p_ticket = p_ticket;
Paul Bakker606b4ba2013-08-14 16:52:14 +02003188}
Manuel Pégourié-Gonnardb596abf2015-05-20 10:45:29 +02003189#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003190#endif /* MBEDTLS_SSL_SESSION_TICKETS */
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +02003191
Gilles Peskine449bd832023-01-11 14:50:10 +01003192void mbedtls_ssl_set_export_keys_cb(mbedtls_ssl_context *ssl,
3193 mbedtls_ssl_export_keys_t *f_export_keys,
3194 void *p_export_keys)
Robert Cragie4feb7ae2015-10-02 13:33:37 +01003195{
Hanno Becker7e6c1782021-06-08 09:24:55 +01003196 ssl->f_export_keys = f_export_keys;
3197 ssl->p_export_keys = p_export_keys;
Ron Eldorf5cc10d2019-05-07 18:33:40 +03003198}
Robert Cragie4feb7ae2015-10-02 13:33:37 +01003199
Gilles Peskineb74a1c72018-04-24 13:09:22 +02003200#if defined(MBEDTLS_SSL_ASYNC_PRIVATE)
Gilles Peskine8bf79f62018-01-05 21:11:53 +01003201void mbedtls_ssl_conf_async_private_cb(
3202 mbedtls_ssl_config *conf,
3203 mbedtls_ssl_async_sign_t *f_async_sign,
3204 mbedtls_ssl_async_decrypt_t *f_async_decrypt,
3205 mbedtls_ssl_async_resume_t *f_async_resume,
3206 mbedtls_ssl_async_cancel_t *f_async_cancel,
Gilles Peskine449bd832023-01-11 14:50:10 +01003207 void *async_config_data)
Gilles Peskine8bf79f62018-01-05 21:11:53 +01003208{
3209 conf->f_async_sign_start = f_async_sign;
3210 conf->f_async_decrypt_start = f_async_decrypt;
3211 conf->f_async_resume = f_async_resume;
3212 conf->f_async_cancel = f_async_cancel;
Gilles Peskinedf13d5c2018-04-25 20:39:48 +02003213 conf->p_async_config_data = async_config_data;
3214}
3215
Gilles Peskine449bd832023-01-11 14:50:10 +01003216void *mbedtls_ssl_conf_get_async_config_data(const mbedtls_ssl_config *conf)
Gilles Peskine8f97af72018-04-26 11:46:10 +02003217{
Gilles Peskine449bd832023-01-11 14:50:10 +01003218 return conf->p_async_config_data;
Gilles Peskine8f97af72018-04-26 11:46:10 +02003219}
3220
Gilles Peskine449bd832023-01-11 14:50:10 +01003221void *mbedtls_ssl_get_async_operation_data(const mbedtls_ssl_context *ssl)
Gilles Peskinedf13d5c2018-04-25 20:39:48 +02003222{
Gilles Peskine449bd832023-01-11 14:50:10 +01003223 if (ssl->handshake == NULL) {
3224 return NULL;
3225 } else {
3226 return ssl->handshake->user_async_ctx;
3227 }
Gilles Peskinedf13d5c2018-04-25 20:39:48 +02003228}
3229
Gilles Peskine449bd832023-01-11 14:50:10 +01003230void mbedtls_ssl_set_async_operation_data(mbedtls_ssl_context *ssl,
3231 void *ctx)
Gilles Peskinedf13d5c2018-04-25 20:39:48 +02003232{
Gilles Peskine449bd832023-01-11 14:50:10 +01003233 if (ssl->handshake != NULL) {
Gilles Peskinedf13d5c2018-04-25 20:39:48 +02003234 ssl->handshake->user_async_ctx = ctx;
Gilles Peskine449bd832023-01-11 14:50:10 +01003235 }
Gilles Peskine8bf79f62018-01-05 21:11:53 +01003236}
Gilles Peskineb74a1c72018-04-24 13:09:22 +02003237#endif /* MBEDTLS_SSL_ASYNC_PRIVATE */
Gilles Peskine8bf79f62018-01-05 21:11:53 +01003238
Paul Bakker5121ce52009-01-03 21:22:43 +00003239/*
3240 * SSL get accessors
3241 */
Gilles Peskine449bd832023-01-11 14:50:10 +01003242uint32_t mbedtls_ssl_get_verify_result(const mbedtls_ssl_context *ssl)
Paul Bakker5121ce52009-01-03 21:22:43 +00003243{
Gilles Peskine449bd832023-01-11 14:50:10 +01003244 if (ssl->session != NULL) {
3245 return ssl->session->verify_result;
3246 }
Manuel Pégourié-Gonnarde89163c2015-01-23 14:30:57 +00003247
Gilles Peskine449bd832023-01-11 14:50:10 +01003248 if (ssl->session_negotiate != NULL) {
3249 return ssl->session_negotiate->verify_result;
3250 }
Manuel Pégourié-Gonnarde89163c2015-01-23 14:30:57 +00003251
Gilles Peskine449bd832023-01-11 14:50:10 +01003252 return 0xFFFFFFFF;
Paul Bakker5121ce52009-01-03 21:22:43 +00003253}
3254
Gilles Peskine449bd832023-01-11 14:50:10 +01003255int mbedtls_ssl_get_ciphersuite_id_from_ssl(const mbedtls_ssl_context *ssl)
Glenn Strauss8f526902022-01-13 00:04:49 -05003256{
Gilles Peskine449bd832023-01-11 14:50:10 +01003257 if (ssl == NULL || ssl->session == NULL) {
3258 return 0;
3259 }
Glenn Strauss8f526902022-01-13 00:04:49 -05003260
Gilles Peskine449bd832023-01-11 14:50:10 +01003261 return ssl->session->ciphersuite;
Glenn Strauss8f526902022-01-13 00:04:49 -05003262}
3263
Gilles Peskine449bd832023-01-11 14:50:10 +01003264const char *mbedtls_ssl_get_ciphersuite(const mbedtls_ssl_context *ssl)
Paul Bakker72f62662011-01-16 21:27:44 +00003265{
Gilles Peskine449bd832023-01-11 14:50:10 +01003266 if (ssl == NULL || ssl->session == NULL) {
3267 return NULL;
3268 }
Paul Bakker926c8e42013-03-06 10:23:34 +01003269
Gilles Peskine449bd832023-01-11 14:50:10 +01003270 return mbedtls_ssl_get_ciphersuite_name(ssl->session->ciphersuite);
Paul Bakker72f62662011-01-16 21:27:44 +00003271}
3272
Gilles Peskine449bd832023-01-11 14:50:10 +01003273const char *mbedtls_ssl_get_version(const mbedtls_ssl_context *ssl)
Paul Bakker43ca69c2011-01-15 17:35:19 +00003274{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003275#if defined(MBEDTLS_SSL_PROTO_DTLS)
Gilles Peskine449bd832023-01-11 14:50:10 +01003276 if (ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM) {
3277 switch (ssl->tls_version) {
Glenn Strauss60bfe602022-03-14 19:04:24 -04003278 case MBEDTLS_SSL_VERSION_TLS1_2:
Gilles Peskine449bd832023-01-11 14:50:10 +01003279 return "DTLSv1.2";
Manuel Pégourié-Gonnardb21ca2a2014-02-10 13:43:33 +01003280 default:
Gilles Peskine449bd832023-01-11 14:50:10 +01003281 return "unknown (DTLS)";
Manuel Pégourié-Gonnardb21ca2a2014-02-10 13:43:33 +01003282 }
3283 }
3284#endif
3285
Gilles Peskine449bd832023-01-11 14:50:10 +01003286 switch (ssl->tls_version) {
Glenn Strauss60bfe602022-03-14 19:04:24 -04003287 case MBEDTLS_SSL_VERSION_TLS1_2:
Gilles Peskine449bd832023-01-11 14:50:10 +01003288 return "TLSv1.2";
Glenn Strauss60bfe602022-03-14 19:04:24 -04003289 case MBEDTLS_SSL_VERSION_TLS1_3:
Gilles Peskine449bd832023-01-11 14:50:10 +01003290 return "TLSv1.3";
Paul Bakker43ca69c2011-01-15 17:35:19 +00003291 default:
Gilles Peskine449bd832023-01-11 14:50:10 +01003292 return "unknown";
Paul Bakker43ca69c2011-01-15 17:35:19 +00003293 }
Paul Bakker43ca69c2011-01-15 17:35:19 +00003294}
3295
Manuel Pégourié-Gonnarda2cda6b2015-08-31 18:30:52 +02003296#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
Gilles Peskine449bd832023-01-11 14:50:10 +01003297size_t mbedtls_ssl_get_input_max_frag_len(const mbedtls_ssl_context *ssl)
Andrzej Kurek90c6e842020-04-03 05:25:29 -04003298{
David Horstmann95d516f2021-05-04 18:36:56 +01003299 size_t max_len = MBEDTLS_SSL_IN_CONTENT_LEN;
Andrzej Kurek90c6e842020-04-03 05:25:29 -04003300 size_t read_mfl;
3301
Jerry Yuddda0502022-12-01 19:43:12 +08003302#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
Andrzej Kurek90c6e842020-04-03 05:25:29 -04003303 /* Use the configured MFL for the client if we're past SERVER_HELLO_DONE */
Gilles Peskine449bd832023-01-11 14:50:10 +01003304 if (ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT &&
3305 ssl->state >= MBEDTLS_SSL_SERVER_HELLO_DONE) {
3306 return ssl_mfl_code_to_length(ssl->conf->mfl_code);
Andrzej Kurek90c6e842020-04-03 05:25:29 -04003307 }
Jerry Yuddda0502022-12-01 19:43:12 +08003308#endif
Andrzej Kurek90c6e842020-04-03 05:25:29 -04003309
3310 /* Check if a smaller max length was negotiated */
Gilles Peskine449bd832023-01-11 14:50:10 +01003311 if (ssl->session_out != NULL) {
3312 read_mfl = ssl_mfl_code_to_length(ssl->session_out->mfl_code);
3313 if (read_mfl < max_len) {
Andrzej Kurek90c6e842020-04-03 05:25:29 -04003314 max_len = read_mfl;
3315 }
3316 }
3317
Jerry Yuddda0502022-12-01 19:43:12 +08003318 /* During a handshake, use the value being negotiated */
Gilles Peskine449bd832023-01-11 14:50:10 +01003319 if (ssl->session_negotiate != NULL) {
3320 read_mfl = ssl_mfl_code_to_length(ssl->session_negotiate->mfl_code);
3321 if (read_mfl < max_len) {
Andrzej Kurek90c6e842020-04-03 05:25:29 -04003322 max_len = read_mfl;
3323 }
3324 }
3325
Gilles Peskine449bd832023-01-11 14:50:10 +01003326 return max_len;
Andrzej Kurek90c6e842020-04-03 05:25:29 -04003327}
3328
Gilles Peskine449bd832023-01-11 14:50:10 +01003329size_t mbedtls_ssl_get_output_max_frag_len(const mbedtls_ssl_context *ssl)
Manuel Pégourié-Gonnarda2cda6b2015-08-31 18:30:52 +02003330{
3331 size_t max_len;
3332
3333 /*
3334 * Assume mfl_code is correct since it was checked when set
3335 */
Gilles Peskine449bd832023-01-11 14:50:10 +01003336 max_len = ssl_mfl_code_to_length(ssl->conf->mfl_code);
Manuel Pégourié-Gonnarda2cda6b2015-08-31 18:30:52 +02003337
Manuel Pégourié-Gonnard2cb17e22017-09-19 13:00:47 +02003338 /* Check if a smaller max length was negotiated */
Gilles Peskine449bd832023-01-11 14:50:10 +01003339 if (ssl->session_out != NULL &&
3340 ssl_mfl_code_to_length(ssl->session_out->mfl_code) < max_len) {
3341 max_len = ssl_mfl_code_to_length(ssl->session_out->mfl_code);
Manuel Pégourié-Gonnarda2cda6b2015-08-31 18:30:52 +02003342 }
3343
Manuel Pégourié-Gonnard2cb17e22017-09-19 13:00:47 +02003344 /* During a handshake, use the value being negotiated */
Gilles Peskine449bd832023-01-11 14:50:10 +01003345 if (ssl->session_negotiate != NULL &&
3346 ssl_mfl_code_to_length(ssl->session_negotiate->mfl_code) < max_len) {
3347 max_len = ssl_mfl_code_to_length(ssl->session_negotiate->mfl_code);
Manuel Pégourié-Gonnard2cb17e22017-09-19 13:00:47 +02003348 }
3349
Gilles Peskine449bd832023-01-11 14:50:10 +01003350 return max_len;
Manuel Pégourié-Gonnarda2cda6b2015-08-31 18:30:52 +02003351}
3352#endif /* MBEDTLS_SSL_MAX_FRAGMENT_LENGTH */
3353
Manuel Pégourié-Gonnardb8eec192018-08-20 09:34:02 +02003354#if defined(MBEDTLS_SSL_PROTO_DTLS)
Gilles Peskine449bd832023-01-11 14:50:10 +01003355size_t mbedtls_ssl_get_current_mtu(const mbedtls_ssl_context *ssl)
Manuel Pégourié-Gonnardb8eec192018-08-20 09:34:02 +02003356{
Andrzej Kurekef43ce62018-10-09 08:24:12 -04003357 /* Return unlimited mtu for client hello messages to avoid fragmentation. */
Gilles Peskine449bd832023-01-11 14:50:10 +01003358 if (ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT &&
3359 (ssl->state == MBEDTLS_SSL_CLIENT_HELLO ||
3360 ssl->state == MBEDTLS_SSL_SERVER_HELLO)) {
3361 return 0;
3362 }
Andrzej Kurekef43ce62018-10-09 08:24:12 -04003363
Gilles Peskine449bd832023-01-11 14:50:10 +01003364 if (ssl->handshake == NULL || ssl->handshake->mtu == 0) {
3365 return ssl->mtu;
3366 }
Manuel Pégourié-Gonnardb8eec192018-08-20 09:34:02 +02003367
Gilles Peskine449bd832023-01-11 14:50:10 +01003368 if (ssl->mtu == 0) {
3369 return ssl->handshake->mtu;
3370 }
Manuel Pégourié-Gonnardb8eec192018-08-20 09:34:02 +02003371
Gilles Peskine449bd832023-01-11 14:50:10 +01003372 return ssl->mtu < ssl->handshake->mtu ?
3373 ssl->mtu : ssl->handshake->mtu;
Manuel Pégourié-Gonnardb8eec192018-08-20 09:34:02 +02003374}
3375#endif /* MBEDTLS_SSL_PROTO_DTLS */
3376
Gilles Peskine449bd832023-01-11 14:50:10 +01003377int mbedtls_ssl_get_max_out_record_payload(const mbedtls_ssl_context *ssl)
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02003378{
3379 size_t max_len = MBEDTLS_SSL_OUT_CONTENT_LEN;
3380
Manuel Pégourié-Gonnard000281e2018-08-21 11:20:58 +02003381#if !defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH) && \
3382 !defined(MBEDTLS_SSL_PROTO_DTLS)
3383 (void) ssl;
3384#endif
3385
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02003386#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
Gilles Peskine449bd832023-01-11 14:50:10 +01003387 const size_t mfl = mbedtls_ssl_get_output_max_frag_len(ssl);
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02003388
Gilles Peskine449bd832023-01-11 14:50:10 +01003389 if (max_len > mfl) {
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02003390 max_len = mfl;
Gilles Peskine449bd832023-01-11 14:50:10 +01003391 }
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02003392#endif
3393
3394#if defined(MBEDTLS_SSL_PROTO_DTLS)
Gilles Peskine449bd832023-01-11 14:50:10 +01003395 if (mbedtls_ssl_get_current_mtu(ssl) != 0) {
3396 const size_t mtu = mbedtls_ssl_get_current_mtu(ssl);
3397 const int ret = mbedtls_ssl_get_record_expansion(ssl);
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02003398 const size_t overhead = (size_t) ret;
3399
Gilles Peskine449bd832023-01-11 14:50:10 +01003400 if (ret < 0) {
3401 return ret;
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02003402 }
3403
Gilles Peskine449bd832023-01-11 14:50:10 +01003404 if (mtu <= overhead) {
3405 MBEDTLS_SSL_DEBUG_MSG(1, ("MTU too low for record expansion"));
3406 return MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
3407 }
3408
3409 if (max_len > mtu - overhead) {
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02003410 max_len = mtu - overhead;
Gilles Peskine449bd832023-01-11 14:50:10 +01003411 }
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02003412 }
Manuel Pégourié-Gonnardb8eec192018-08-20 09:34:02 +02003413#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02003414
Hanno Becker0defedb2018-08-10 12:35:02 +01003415#if !defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH) && \
3416 !defined(MBEDTLS_SSL_PROTO_DTLS)
3417 ((void) ssl);
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02003418#endif
3419
Gilles Peskine449bd832023-01-11 14:50:10 +01003420 return (int) max_len;
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02003421}
3422
Gilles Peskine449bd832023-01-11 14:50:10 +01003423int mbedtls_ssl_get_max_in_record_payload(const mbedtls_ssl_context *ssl)
Hanno Becker2d8e99b2021-04-21 06:19:50 +01003424{
3425 size_t max_len = MBEDTLS_SSL_IN_CONTENT_LEN;
3426
3427#if !defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
3428 (void) ssl;
3429#endif
3430
3431#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
Gilles Peskine449bd832023-01-11 14:50:10 +01003432 const size_t mfl = mbedtls_ssl_get_input_max_frag_len(ssl);
Hanno Becker2d8e99b2021-04-21 06:19:50 +01003433
Gilles Peskine449bd832023-01-11 14:50:10 +01003434 if (max_len > mfl) {
Hanno Becker2d8e99b2021-04-21 06:19:50 +01003435 max_len = mfl;
Gilles Peskine449bd832023-01-11 14:50:10 +01003436 }
Hanno Becker2d8e99b2021-04-21 06:19:50 +01003437#endif
3438
Gilles Peskine449bd832023-01-11 14:50:10 +01003439 return (int) max_len;
Hanno Becker2d8e99b2021-04-21 06:19:50 +01003440}
3441
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003442#if defined(MBEDTLS_X509_CRT_PARSE_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01003443const mbedtls_x509_crt *mbedtls_ssl_get_peer_cert(const mbedtls_ssl_context *ssl)
Paul Bakkerb0550d92012-10-30 07:51:03 +00003444{
Gilles Peskine449bd832023-01-11 14:50:10 +01003445 if (ssl == NULL || ssl->session == NULL) {
3446 return NULL;
3447 }
Paul Bakkerb0550d92012-10-30 07:51:03 +00003448
Hanno Beckere6824572019-02-07 13:18:46 +00003449#if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
Gilles Peskine449bd832023-01-11 14:50:10 +01003450 return ssl->session->peer_cert;
Hanno Beckere6824572019-02-07 13:18:46 +00003451#else
Gilles Peskine449bd832023-01-11 14:50:10 +01003452 return NULL;
Hanno Beckere6824572019-02-07 13:18:46 +00003453#endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
Paul Bakkerb0550d92012-10-30 07:51:03 +00003454}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003455#endif /* MBEDTLS_X509_CRT_PARSE_C */
Paul Bakkerb0550d92012-10-30 07:51:03 +00003456
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003457#if defined(MBEDTLS_SSL_CLI_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01003458int mbedtls_ssl_get_session(const mbedtls_ssl_context *ssl,
3459 mbedtls_ssl_session *dst)
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02003460{
Hanno Beckere810bbc2021-05-14 16:01:05 +01003461 int ret;
3462
Gilles Peskine449bd832023-01-11 14:50:10 +01003463 if (ssl == NULL ||
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02003464 dst == NULL ||
3465 ssl->session == NULL ||
Gilles Peskine449bd832023-01-11 14:50:10 +01003466 ssl->conf->endpoint != MBEDTLS_SSL_IS_CLIENT) {
3467 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02003468 }
3469
Hanno Beckere810bbc2021-05-14 16:01:05 +01003470 /* Since Mbed TLS 3.0, mbedtls_ssl_get_session() is no longer
3471 * idempotent: Each session can only be exported once.
3472 *
3473 * (This is in preparation for TLS 1.3 support where we will
3474 * need the ability to export multiple sessions (aka tickets),
3475 * which will be achieved by calling mbedtls_ssl_get_session()
3476 * multiple times until it fails.)
3477 *
3478 * Check whether we have already exported the current session,
3479 * and fail if so.
3480 */
Gilles Peskine449bd832023-01-11 14:50:10 +01003481 if (ssl->session->exported == 1) {
3482 return MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
3483 }
Hanno Beckere810bbc2021-05-14 16:01:05 +01003484
Gilles Peskine449bd832023-01-11 14:50:10 +01003485 ret = mbedtls_ssl_session_copy(dst, ssl->session);
3486 if (ret != 0) {
3487 return ret;
3488 }
Hanno Beckere810bbc2021-05-14 16:01:05 +01003489
3490 /* Remember that we've exported the session. */
3491 ssl->session->exported = 1;
Gilles Peskine449bd832023-01-11 14:50:10 +01003492 return 0;
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02003493}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003494#endif /* MBEDTLS_SSL_CLI_C */
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02003495
Paul Bakker5121ce52009-01-03 21:22:43 +00003496/*
Hanno Beckera835da52019-05-16 12:39:07 +01003497 * Define ticket header determining Mbed TLS version
3498 * and structure of the ticket.
3499 */
3500
Hanno Becker94ef3b32019-05-16 12:50:45 +01003501/*
Hanno Becker50b59662019-05-28 14:30:45 +01003502 * Define bitflag determining compile-time settings influencing
3503 * structure of serialized SSL sessions.
Hanno Becker94ef3b32019-05-16 12:50:45 +01003504 */
3505
Hanno Becker50b59662019-05-28 14:30:45 +01003506#if defined(MBEDTLS_HAVE_TIME)
Hanno Becker3e088662019-05-29 11:10:18 +01003507#define SSL_SERIALIZED_SESSION_CONFIG_TIME 1
Hanno Becker50b59662019-05-28 14:30:45 +01003508#else
Hanno Becker3e088662019-05-29 11:10:18 +01003509#define SSL_SERIALIZED_SESSION_CONFIG_TIME 0
Hanno Becker94ef3b32019-05-16 12:50:45 +01003510#endif /* MBEDTLS_HAVE_TIME */
3511
3512#if defined(MBEDTLS_X509_CRT_PARSE_C)
Hanno Becker3e088662019-05-29 11:10:18 +01003513#define SSL_SERIALIZED_SESSION_CONFIG_CRT 1
Hanno Becker94ef3b32019-05-16 12:50:45 +01003514#else
Hanno Becker3e088662019-05-29 11:10:18 +01003515#define SSL_SERIALIZED_SESSION_CONFIG_CRT 0
Hanno Becker94ef3b32019-05-16 12:50:45 +01003516#endif /* MBEDTLS_X509_CRT_PARSE_C */
3517
3518#if defined(MBEDTLS_SSL_CLI_C) && defined(MBEDTLS_SSL_SESSION_TICKETS)
Hanno Becker3e088662019-05-29 11:10:18 +01003519#define SSL_SERIALIZED_SESSION_CONFIG_CLIENT_TICKET 1
Hanno Becker94ef3b32019-05-16 12:50:45 +01003520#else
Hanno Becker3e088662019-05-29 11:10:18 +01003521#define SSL_SERIALIZED_SESSION_CONFIG_CLIENT_TICKET 0
Hanno Becker94ef3b32019-05-16 12:50:45 +01003522#endif /* MBEDTLS_SSL_CLI_C && MBEDTLS_SSL_SESSION_TICKETS */
3523
3524#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
Hanno Becker3e088662019-05-29 11:10:18 +01003525#define SSL_SERIALIZED_SESSION_CONFIG_MFL 1
Hanno Becker94ef3b32019-05-16 12:50:45 +01003526#else
Hanno Becker3e088662019-05-29 11:10:18 +01003527#define SSL_SERIALIZED_SESSION_CONFIG_MFL 0
Hanno Becker94ef3b32019-05-16 12:50:45 +01003528#endif /* MBEDTLS_SSL_MAX_FRAGMENT_LENGTH */
3529
Hanno Becker94ef3b32019-05-16 12:50:45 +01003530#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
Hanno Becker3e088662019-05-29 11:10:18 +01003531#define SSL_SERIALIZED_SESSION_CONFIG_ETM 1
Hanno Becker94ef3b32019-05-16 12:50:45 +01003532#else
Hanno Becker3e088662019-05-29 11:10:18 +01003533#define SSL_SERIALIZED_SESSION_CONFIG_ETM 0
Hanno Becker94ef3b32019-05-16 12:50:45 +01003534#endif /* MBEDTLS_SSL_ENCRYPT_THEN_MAC */
3535
Hanno Becker94ef3b32019-05-16 12:50:45 +01003536#if defined(MBEDTLS_SSL_SESSION_TICKETS)
3537#define SSL_SERIALIZED_SESSION_CONFIG_TICKET 1
3538#else
3539#define SSL_SERIALIZED_SESSION_CONFIG_TICKET 0
3540#endif /* MBEDTLS_SSL_SESSION_TICKETS */
3541
Hanno Becker3e088662019-05-29 11:10:18 +01003542#define SSL_SERIALIZED_SESSION_CONFIG_TIME_BIT 0
3543#define SSL_SERIALIZED_SESSION_CONFIG_CRT_BIT 1
3544#define SSL_SERIALIZED_SESSION_CONFIG_CLIENT_TICKET_BIT 2
3545#define SSL_SERIALIZED_SESSION_CONFIG_MFL_BIT 3
Hanno Becker37bdbe62021-08-01 05:38:58 +01003546#define SSL_SERIALIZED_SESSION_CONFIG_ETM_BIT 4
3547#define SSL_SERIALIZED_SESSION_CONFIG_TICKET_BIT 5
Hanno Becker3e088662019-05-29 11:10:18 +01003548
Hanno Becker50b59662019-05-28 14:30:45 +01003549#define SSL_SERIALIZED_SESSION_CONFIG_BITFLAG \
Gilles Peskine449bd832023-01-11 14:50:10 +01003550 ((uint16_t) ( \
3551 (SSL_SERIALIZED_SESSION_CONFIG_TIME << SSL_SERIALIZED_SESSION_CONFIG_TIME_BIT) | \
3552 (SSL_SERIALIZED_SESSION_CONFIG_CRT << SSL_SERIALIZED_SESSION_CONFIG_CRT_BIT) | \
3553 (SSL_SERIALIZED_SESSION_CONFIG_CLIENT_TICKET << \
3554 SSL_SERIALIZED_SESSION_CONFIG_CLIENT_TICKET_BIT) | \
3555 (SSL_SERIALIZED_SESSION_CONFIG_MFL << SSL_SERIALIZED_SESSION_CONFIG_MFL_BIT) | \
3556 (SSL_SERIALIZED_SESSION_CONFIG_ETM << SSL_SERIALIZED_SESSION_CONFIG_ETM_BIT) | \
3557 (SSL_SERIALIZED_SESSION_CONFIG_TICKET << SSL_SERIALIZED_SESSION_CONFIG_TICKET_BIT)))
Hanno Becker94ef3b32019-05-16 12:50:45 +01003558
Hanno Beckerf8787072019-05-16 12:41:07 +01003559static unsigned char ssl_serialized_session_header[] = {
Hanno Becker94ef3b32019-05-16 12:50:45 +01003560 MBEDTLS_VERSION_MAJOR,
3561 MBEDTLS_VERSION_MINOR,
3562 MBEDTLS_VERSION_PATCH,
Gilles Peskine449bd832023-01-11 14:50:10 +01003563 MBEDTLS_BYTE_1(SSL_SERIALIZED_SESSION_CONFIG_BITFLAG),
3564 MBEDTLS_BYTE_0(SSL_SERIALIZED_SESSION_CONFIG_BITFLAG),
Hanno Beckerf8787072019-05-16 12:41:07 +01003565};
Hanno Beckera835da52019-05-16 12:39:07 +01003566
3567/*
Manuel Pégourié-Gonnarda3e7c652019-05-16 10:08:35 +02003568 * Serialize a session in the following format:
Manuel Pégourié-Gonnard35eb8022019-05-16 11:11:08 +02003569 * (in the presentation language of TLS, RFC 8446 section 3)
Manuel Pégourié-Gonnarda3e7c652019-05-16 10:08:35 +02003570 *
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01003571 * struct {
Hanno Beckerdc28b6c2019-05-29 11:08:00 +01003572 *
Hanno Beckerdce50972021-08-01 05:39:23 +01003573 * opaque mbedtls_version[3]; // library version: major, minor, patch
3574 * opaque session_format[2]; // library-version specific 16-bit field
3575 * // determining the format of the remaining
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01003576 * // serialized data.
Hanno Beckerdc28b6c2019-05-29 11:08:00 +01003577 *
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01003578 * Note: When updating the format, remember to keep
3579 * these version+format bytes.
Manuel Pégourié-Gonnarda3e7c652019-05-16 10:08:35 +02003580 *
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01003581 * // In this version, `session_format` determines
3582 * // the setting of those compile-time
3583 * // configuration options which influence
3584 * // the structure of mbedtls_ssl_session.
3585 *
Glenn Straussda7851c2022-03-14 13:29:48 -04003586 * uint8_t minor_ver; // Protocol minor version. Possible values:
Jerry Yu0c2a7382022-12-06 13:27:25 +08003587 * // - TLS 1.2 (0x0303)
3588 * // - TLS 1.3 (0x0304)
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01003589 *
Glenn Straussda7851c2022-03-14 13:29:48 -04003590 * select (serialized_session.tls_version) {
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01003591 *
Glenn Straussda7851c2022-03-14 13:29:48 -04003592 * case MBEDTLS_SSL_VERSION_TLS1_2:
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01003593 * serialized_session_tls12 data;
Jerry Yu0c2a7382022-12-06 13:27:25 +08003594 * case MBEDTLS_SSL_VERSION_TLS1_3:
Jerry Yuddda0502022-12-01 19:43:12 +08003595 * serialized_session_tls13 data;
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01003596 *
3597 * };
3598 *
3599 * } serialized_session;
3600 *
Manuel Pégourié-Gonnarda3e7c652019-05-16 10:08:35 +02003601 */
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01003602
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02003603MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01003604static int ssl_session_save(const mbedtls_ssl_session *session,
3605 unsigned char omit_header,
3606 unsigned char *buf,
3607 size_t buf_len,
3608 size_t *olen)
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01003609{
3610 unsigned char *p = buf;
3611 size_t used = 0;
Jerry Yu251a12e2022-07-13 15:15:48 +08003612 size_t remaining_len;
Jerry Yue36fdd62022-08-17 21:31:36 +08003613#if defined(MBEDTLS_SSL_PROTO_TLS1_3)
3614 size_t out_len;
3615 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
3616#endif
Gilles Peskine449bd832023-01-11 14:50:10 +01003617 if (session == NULL) {
3618 return MBEDTLS_ERR_SSL_INTERNAL_ERROR;
3619 }
Jerry Yu438ddd82022-07-07 06:55:50 +00003620
Gilles Peskine449bd832023-01-11 14:50:10 +01003621 if (!omit_header) {
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01003622 /*
3623 * Add Mbed TLS version identifier
3624 */
Gilles Peskine449bd832023-01-11 14:50:10 +01003625 used += sizeof(ssl_serialized_session_header);
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01003626
Gilles Peskine449bd832023-01-11 14:50:10 +01003627 if (used <= buf_len) {
3628 memcpy(p, ssl_serialized_session_header,
3629 sizeof(ssl_serialized_session_header));
3630 p += sizeof(ssl_serialized_session_header);
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01003631 }
3632 }
3633
3634 /*
3635 * TLS version identifier
3636 */
3637 used += 1;
Gilles Peskine449bd832023-01-11 14:50:10 +01003638 if (used <= buf_len) {
3639 *p++ = MBEDTLS_BYTE_0(session->tls_version);
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01003640 }
3641
3642 /* Forward to version-specific serialization routine. */
Jerry Yufca4d572022-07-21 10:37:48 +08003643 remaining_len = (buf_len >= used) ? buf_len - used : 0;
Gilles Peskine449bd832023-01-11 14:50:10 +01003644 switch (session->tls_version) {
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01003645#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
Gilles Peskine449bd832023-01-11 14:50:10 +01003646 case MBEDTLS_SSL_VERSION_TLS1_2:
3647 used += ssl_tls12_session_save(session, p, remaining_len);
3648 break;
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01003649#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
3650
Jerry Yu251a12e2022-07-13 15:15:48 +08003651#if defined(MBEDTLS_SSL_PROTO_TLS1_3)
Gilles Peskine449bd832023-01-11 14:50:10 +01003652 case MBEDTLS_SSL_VERSION_TLS1_3:
3653 ret = ssl_tls13_session_save(session, p, remaining_len, &out_len);
3654 if (ret != 0 && ret != MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL) {
3655 return ret;
3656 }
3657 used += out_len;
3658 break;
Jerry Yu251a12e2022-07-13 15:15:48 +08003659#endif /* MBEDTLS_SSL_PROTO_TLS1_3 */
3660
Gilles Peskine449bd832023-01-11 14:50:10 +01003661 default:
3662 return MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01003663 }
3664
3665 *olen = used;
Gilles Peskine449bd832023-01-11 14:50:10 +01003666 if (used > buf_len) {
3667 return MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL;
3668 }
Manuel Pégourié-Gonnarda3e7c652019-05-16 10:08:35 +02003669
Gilles Peskine449bd832023-01-11 14:50:10 +01003670 return 0;
Manuel Pégourié-Gonnarda3e7c652019-05-16 10:08:35 +02003671}
3672
3673/*
Manuel Pégourié-Gonnard45ac1f02019-07-23 16:52:45 +02003674 * Public wrapper for ssl_session_save()
3675 */
Gilles Peskine449bd832023-01-11 14:50:10 +01003676int mbedtls_ssl_session_save(const mbedtls_ssl_session *session,
3677 unsigned char *buf,
3678 size_t buf_len,
3679 size_t *olen)
Manuel Pégourié-Gonnard45ac1f02019-07-23 16:52:45 +02003680{
Gilles Peskine449bd832023-01-11 14:50:10 +01003681 return ssl_session_save(session, 0, buf, buf_len, olen);
Manuel Pégourié-Gonnard45ac1f02019-07-23 16:52:45 +02003682}
Manuel Pégourié-Gonnarda3e7c652019-05-16 10:08:35 +02003683
Jerry Yuf1b23ca2022-02-18 11:48:47 +08003684/*
3685 * Deserialize session, see mbedtls_ssl_session_save() for format.
3686 *
3687 * This internal version is wrapped by a public function that cleans up in
3688 * case of error, and has an extra option omit_header.
3689 */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02003690MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01003691static int ssl_session_load(mbedtls_ssl_session *session,
3692 unsigned char omit_header,
3693 const unsigned char *buf,
3694 size_t len)
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01003695{
3696 const unsigned char *p = buf;
3697 const unsigned char * const end = buf + len;
Jerry Yu438ddd82022-07-07 06:55:50 +00003698 size_t remaining_len;
3699
3700
Gilles Peskine449bd832023-01-11 14:50:10 +01003701 if (session == NULL) {
3702 return MBEDTLS_ERR_SSL_INTERNAL_ERROR;
3703 }
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01003704
Gilles Peskine449bd832023-01-11 14:50:10 +01003705 if (!omit_header) {
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01003706 /*
3707 * Check Mbed TLS version identifier
3708 */
3709
Gilles Peskine449bd832023-01-11 14:50:10 +01003710 if ((size_t) (end - p) < sizeof(ssl_serialized_session_header)) {
3711 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01003712 }
Gilles Peskine449bd832023-01-11 14:50:10 +01003713
3714 if (memcmp(p, ssl_serialized_session_header,
3715 sizeof(ssl_serialized_session_header)) != 0) {
3716 return MBEDTLS_ERR_SSL_VERSION_MISMATCH;
3717 }
3718 p += sizeof(ssl_serialized_session_header);
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01003719 }
3720
3721 /*
3722 * TLS version identifier
3723 */
Gilles Peskine449bd832023-01-11 14:50:10 +01003724 if (1 > (size_t) (end - p)) {
3725 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
3726 }
Glenn Straussda7851c2022-03-14 13:29:48 -04003727 session->tls_version = 0x0300 | *p++;
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01003728
3729 /* Dispatch according to TLS version. */
Gilles Peskine449bd832023-01-11 14:50:10 +01003730 remaining_len = (end - p);
3731 switch (session->tls_version) {
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01003732#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
Gilles Peskine449bd832023-01-11 14:50:10 +01003733 case MBEDTLS_SSL_VERSION_TLS1_2:
3734 return ssl_tls12_session_load(session, p, remaining_len);
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01003735#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
3736
Jerry Yu438ddd82022-07-07 06:55:50 +00003737#if defined(MBEDTLS_SSL_PROTO_TLS1_3)
Gilles Peskine449bd832023-01-11 14:50:10 +01003738 case MBEDTLS_SSL_VERSION_TLS1_3:
3739 return ssl_tls13_session_load(session, p, remaining_len);
Jerry Yu438ddd82022-07-07 06:55:50 +00003740#endif /* MBEDTLS_SSL_PROTO_TLS1_3 */
3741
Gilles Peskine449bd832023-01-11 14:50:10 +01003742 default:
3743 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01003744 }
3745}
3746
Manuel Pégourié-Gonnarda3e7c652019-05-16 10:08:35 +02003747/*
Manuel Pégourié-Gonnardb9dfc9f2019-07-12 10:50:19 +02003748 * Deserialize session: public wrapper for error cleaning
Manuel Pégourié-Gonnarda3d831b2019-05-23 12:28:45 +02003749 */
Gilles Peskine449bd832023-01-11 14:50:10 +01003750int mbedtls_ssl_session_load(mbedtls_ssl_session *session,
3751 const unsigned char *buf,
3752 size_t len)
Manuel Pégourié-Gonnarda3d831b2019-05-23 12:28:45 +02003753{
Gilles Peskine449bd832023-01-11 14:50:10 +01003754 int ret = ssl_session_load(session, 0, buf, len);
Manuel Pégourié-Gonnarda3d831b2019-05-23 12:28:45 +02003755
Gilles Peskine449bd832023-01-11 14:50:10 +01003756 if (ret != 0) {
3757 mbedtls_ssl_session_free(session);
3758 }
Manuel Pégourié-Gonnarda3d831b2019-05-23 12:28:45 +02003759
Gilles Peskine449bd832023-01-11 14:50:10 +01003760 return ret;
Manuel Pégourié-Gonnarda3d831b2019-05-23 12:28:45 +02003761}
3762
3763/*
Paul Bakker1961b702013-01-25 14:49:24 +01003764 * Perform a single step of the SSL handshake
Paul Bakker5121ce52009-01-03 21:22:43 +00003765 */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02003766MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01003767static int ssl_prepare_handshake_step(mbedtls_ssl_context *ssl)
Hanno Becker41934dd2021-08-07 19:13:43 +01003768{
3769 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
3770
Ronald Cron66dbf912022-02-02 15:33:46 +01003771 /*
3772 * We may have not been able to send to the peer all the handshake data
Ronald Cron3f20b772022-03-08 16:00:02 +01003773 * that were written into the output buffer by the previous handshake step,
3774 * if the write to the network callback returned with the
Ronald Cron66dbf912022-02-02 15:33:46 +01003775 * #MBEDTLS_ERR_SSL_WANT_WRITE error code.
3776 * We proceed to the next handshake step only when all data from the
3777 * previous one have been sent to the peer, thus we make sure that this is
3778 * the case here by calling `mbedtls_ssl_flush_output()`. The function may
3779 * return with the #MBEDTLS_ERR_SSL_WANT_WRITE error code in which case
3780 * we have to wait before to go ahead.
3781 * In the case of TLS 1.3, handshake step handlers do not send data to the
3782 * peer. Data are only sent here and through
3783 * `mbedtls_ssl_handle_pending_alert` in case an error that triggered an
Andrzej Kurek5c65c572022-04-13 14:28:52 -04003784 * alert occurred.
Ronald Cron66dbf912022-02-02 15:33:46 +01003785 */
Gilles Peskine449bd832023-01-11 14:50:10 +01003786 if ((ret = mbedtls_ssl_flush_output(ssl)) != 0) {
3787 return ret;
3788 }
Hanno Becker41934dd2021-08-07 19:13:43 +01003789
3790#if defined(MBEDTLS_SSL_PROTO_DTLS)
Gilles Peskine449bd832023-01-11 14:50:10 +01003791 if (ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
3792 ssl->handshake->retransmit_state == MBEDTLS_SSL_RETRANS_SENDING) {
3793 if ((ret = mbedtls_ssl_flight_transmit(ssl)) != 0) {
3794 return ret;
3795 }
Hanno Becker41934dd2021-08-07 19:13:43 +01003796 }
3797#endif /* MBEDTLS_SSL_PROTO_DTLS */
3798
Gilles Peskine449bd832023-01-11 14:50:10 +01003799 return ret;
Hanno Becker41934dd2021-08-07 19:13:43 +01003800}
3801
Gilles Peskine449bd832023-01-11 14:50:10 +01003802int mbedtls_ssl_handshake_step(mbedtls_ssl_context *ssl)
Paul Bakker5121ce52009-01-03 21:22:43 +00003803{
Hanno Becker41934dd2021-08-07 19:13:43 +01003804 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker5121ce52009-01-03 21:22:43 +00003805
Gilles Peskine449bd832023-01-11 14:50:10 +01003806 if (ssl == NULL ||
Hanno Becker41934dd2021-08-07 19:13:43 +01003807 ssl->conf == NULL ||
3808 ssl->handshake == NULL ||
Gilles Peskine449bd832023-01-11 14:50:10 +01003809 ssl->state == MBEDTLS_SSL_HANDSHAKE_OVER) {
3810 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
Hanno Becker41934dd2021-08-07 19:13:43 +01003811 }
3812
Gilles Peskine449bd832023-01-11 14:50:10 +01003813 ret = ssl_prepare_handshake_step(ssl);
3814 if (ret != 0) {
3815 return ret;
3816 }
Manuel Pégourié-Gonnardf81ee2e2015-09-01 17:43:40 +02003817
Gilles Peskine449bd832023-01-11 14:50:10 +01003818 ret = mbedtls_ssl_handle_pending_alert(ssl);
3819 if (ret != 0) {
Jerry Yue7047812021-09-13 19:26:39 +08003820 goto cleanup;
Gilles Peskine449bd832023-01-11 14:50:10 +01003821 }
Jerry Yue7047812021-09-13 19:26:39 +08003822
Tom Cosgrove2fdc7b32022-09-21 12:33:17 +01003823 /* If ssl->conf->endpoint is not one of MBEDTLS_SSL_IS_CLIENT or
3824 * MBEDTLS_SSL_IS_SERVER, this is the return code we give */
3825 ret = MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
3826
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003827#if defined(MBEDTLS_SSL_CLI_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01003828 if (ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT) {
3829 MBEDTLS_SSL_DEBUG_MSG(2, ("client state: %s",
3830 mbedtls_ssl_states_str(ssl->state)));
Jerry Yub9930e72021-08-06 17:11:51 +08003831
Gilles Peskine449bd832023-01-11 14:50:10 +01003832 switch (ssl->state) {
Ronald Cron9f0fba32022-02-10 16:45:15 +01003833 case MBEDTLS_SSL_HELLO_REQUEST:
3834 ssl->state = MBEDTLS_SSL_CLIENT_HELLO;
Tom Cosgrove87d9c6c2022-09-22 09:27:56 +01003835 ret = 0;
Ronald Cron9f0fba32022-02-10 16:45:15 +01003836 break;
Jerry Yub9930e72021-08-06 17:11:51 +08003837
Ronald Cron9f0fba32022-02-10 16:45:15 +01003838 case MBEDTLS_SSL_CLIENT_HELLO:
Gilles Peskine449bd832023-01-11 14:50:10 +01003839 ret = mbedtls_ssl_write_client_hello(ssl);
Ronald Cron9f0fba32022-02-10 16:45:15 +01003840 break;
3841
3842 default:
3843#if defined(MBEDTLS_SSL_PROTO_TLS1_2) && defined(MBEDTLS_SSL_PROTO_TLS1_3)
Gilles Peskine449bd832023-01-11 14:50:10 +01003844 if (ssl->tls_version == MBEDTLS_SSL_VERSION_TLS1_3) {
3845 ret = mbedtls_ssl_tls13_handshake_client_step(ssl);
3846 } else {
3847 ret = mbedtls_ssl_handshake_client_step(ssl);
3848 }
Ronald Cron9f0fba32022-02-10 16:45:15 +01003849#elif defined(MBEDTLS_SSL_PROTO_TLS1_2)
Gilles Peskine449bd832023-01-11 14:50:10 +01003850 ret = mbedtls_ssl_handshake_client_step(ssl);
Ronald Cron9f0fba32022-02-10 16:45:15 +01003851#else
Gilles Peskine449bd832023-01-11 14:50:10 +01003852 ret = mbedtls_ssl_tls13_handshake_client_step(ssl);
Ronald Cron9f0fba32022-02-10 16:45:15 +01003853#endif
3854 }
Jerry Yub9930e72021-08-06 17:11:51 +08003855 }
Paul Bakker5121ce52009-01-03 21:22:43 +00003856#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003857#if defined(MBEDTLS_SSL_SRV_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01003858 if (ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER) {
Ronald Cron6f135e12021-12-08 16:57:54 +01003859#if defined(MBEDTLS_SSL_PROTO_TLS1_3)
Gilles Peskine449bd832023-01-11 14:50:10 +01003860 if (mbedtls_ssl_conf_is_tls13_only(ssl->conf)) {
3861 ret = mbedtls_ssl_tls13_handshake_server_step(ssl);
3862 }
Ronald Cron6f135e12021-12-08 16:57:54 +01003863#endif /* MBEDTLS_SSL_PROTO_TLS1_3 */
Jerry Yub9930e72021-08-06 17:11:51 +08003864
3865#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
Gilles Peskine449bd832023-01-11 14:50:10 +01003866 if (mbedtls_ssl_conf_is_tls12_only(ssl->conf)) {
3867 ret = mbedtls_ssl_handshake_server_step(ssl);
3868 }
Jerry Yub9930e72021-08-06 17:11:51 +08003869#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
3870 }
Paul Bakker5121ce52009-01-03 21:22:43 +00003871#endif
3872
Gilles Peskine449bd832023-01-11 14:50:10 +01003873 if (ret != 0) {
Jerry Yubbd5a3f2021-09-18 20:50:22 +08003874 /* handshake_step return error. And it is same
3875 * with alert_reason.
3876 */
Gilles Peskine449bd832023-01-11 14:50:10 +01003877 if (ssl->send_alert) {
3878 ret = mbedtls_ssl_handle_pending_alert(ssl);
Jerry Yue7047812021-09-13 19:26:39 +08003879 goto cleanup;
3880 }
3881 }
3882
3883cleanup:
Gilles Peskine449bd832023-01-11 14:50:10 +01003884 return ret;
Paul Bakker1961b702013-01-25 14:49:24 +01003885}
3886
3887/*
3888 * Perform the SSL handshake
3889 */
Gilles Peskine449bd832023-01-11 14:50:10 +01003890int mbedtls_ssl_handshake(mbedtls_ssl_context *ssl)
Paul Bakker1961b702013-01-25 14:49:24 +01003891{
3892 int ret = 0;
3893
Hanno Beckera817ea42020-10-20 15:20:23 +01003894 /* Sanity checks */
3895
Gilles Peskine449bd832023-01-11 14:50:10 +01003896 if (ssl == NULL || ssl->conf == NULL) {
3897 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
3898 }
Manuel Pégourié-Gonnardf81ee2e2015-09-01 17:43:40 +02003899
Hanno Beckera817ea42020-10-20 15:20:23 +01003900#if defined(MBEDTLS_SSL_PROTO_DTLS)
Gilles Peskine449bd832023-01-11 14:50:10 +01003901 if (ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
3902 (ssl->f_set_timer == NULL || ssl->f_get_timer == NULL)) {
3903 MBEDTLS_SSL_DEBUG_MSG(1, ("You must use "
3904 "mbedtls_ssl_set_timer_cb() for DTLS"));
3905 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
Hanno Beckera817ea42020-10-20 15:20:23 +01003906 }
3907#endif /* MBEDTLS_SSL_PROTO_DTLS */
3908
Gilles Peskine449bd832023-01-11 14:50:10 +01003909 MBEDTLS_SSL_DEBUG_MSG(2, ("=> handshake"));
Paul Bakker1961b702013-01-25 14:49:24 +01003910
Hanno Beckera817ea42020-10-20 15:20:23 +01003911 /* Main handshake loop */
Gilles Peskine449bd832023-01-11 14:50:10 +01003912 while (ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER) {
3913 ret = mbedtls_ssl_handshake_step(ssl);
Paul Bakker1961b702013-01-25 14:49:24 +01003914
Gilles Peskine449bd832023-01-11 14:50:10 +01003915 if (ret != 0) {
Paul Bakker1961b702013-01-25 14:49:24 +01003916 break;
Gilles Peskine449bd832023-01-11 14:50:10 +01003917 }
Paul Bakker1961b702013-01-25 14:49:24 +01003918 }
3919
Gilles Peskine449bd832023-01-11 14:50:10 +01003920 MBEDTLS_SSL_DEBUG_MSG(2, ("<= handshake"));
Paul Bakker5121ce52009-01-03 21:22:43 +00003921
Gilles Peskine449bd832023-01-11 14:50:10 +01003922 return ret;
Paul Bakker5121ce52009-01-03 21:22:43 +00003923}
3924
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003925#if defined(MBEDTLS_SSL_RENEGOTIATION)
3926#if defined(MBEDTLS_SSL_SRV_C)
Paul Bakker5121ce52009-01-03 21:22:43 +00003927/*
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01003928 * Write HelloRequest to request renegotiation on server
Paul Bakker48916f92012-09-16 19:57:18 +00003929 */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02003930MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01003931static int ssl_write_hello_request(mbedtls_ssl_context *ssl)
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01003932{
Janos Follath865b3eb2019-12-16 11:46:15 +00003933 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01003934
Gilles Peskine449bd832023-01-11 14:50:10 +01003935 MBEDTLS_SSL_DEBUG_MSG(2, ("=> write hello request"));
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01003936
3937 ssl->out_msglen = 4;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003938 ssl->out_msgtype = MBEDTLS_SSL_MSG_HANDSHAKE;
3939 ssl->out_msg[0] = MBEDTLS_SSL_HS_HELLO_REQUEST;
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01003940
Gilles Peskine449bd832023-01-11 14:50:10 +01003941 if ((ret = mbedtls_ssl_write_handshake_msg(ssl)) != 0) {
3942 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ssl_write_handshake_msg", ret);
3943 return ret;
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01003944 }
3945
Gilles Peskine449bd832023-01-11 14:50:10 +01003946 MBEDTLS_SSL_DEBUG_MSG(2, ("<= write hello request"));
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01003947
Gilles Peskine449bd832023-01-11 14:50:10 +01003948 return 0;
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01003949}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003950#endif /* MBEDTLS_SSL_SRV_C */
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01003951
3952/*
3953 * Actually renegotiate current connection, triggered by either:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003954 * - any side: calling mbedtls_ssl_renegotiate(),
3955 * - client: receiving a HelloRequest during mbedtls_ssl_read(),
3956 * - server: receiving any handshake message on server during mbedtls_ssl_read() after
Manuel Pégourié-Gonnard55e4ff22014-08-19 11:16:35 +02003957 * the initial handshake is completed.
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01003958 * If the handshake doesn't complete due to waiting for I/O, it will continue
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003959 * during the next calls to mbedtls_ssl_renegotiate() or mbedtls_ssl_read() respectively.
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01003960 */
Gilles Peskine449bd832023-01-11 14:50:10 +01003961int mbedtls_ssl_start_renegotiation(mbedtls_ssl_context *ssl)
Paul Bakker48916f92012-09-16 19:57:18 +00003962{
Janos Follath865b3eb2019-12-16 11:46:15 +00003963 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker48916f92012-09-16 19:57:18 +00003964
Gilles Peskine449bd832023-01-11 14:50:10 +01003965 MBEDTLS_SSL_DEBUG_MSG(2, ("=> renegotiate"));
Paul Bakker48916f92012-09-16 19:57:18 +00003966
Gilles Peskine449bd832023-01-11 14:50:10 +01003967 if ((ret = ssl_handshake_init(ssl)) != 0) {
3968 return ret;
3969 }
Paul Bakker48916f92012-09-16 19:57:18 +00003970
Manuel Pégourié-Gonnard0557bd52014-08-19 19:18:39 +02003971 /* RFC 6347 4.2.2: "[...] the HelloRequest will have message_seq = 0 and
3972 * the ServerHello will have message_seq = 1" */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003973#if defined(MBEDTLS_SSL_PROTO_DTLS)
Gilles Peskine449bd832023-01-11 14:50:10 +01003974 if (ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
3975 ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_PENDING) {
3976 if (ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER) {
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02003977 ssl->handshake->out_msg_seq = 1;
Gilles Peskine449bd832023-01-11 14:50:10 +01003978 } else {
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02003979 ssl->handshake->in_msg_seq = 1;
Gilles Peskine449bd832023-01-11 14:50:10 +01003980 }
Manuel Pégourié-Gonnard0557bd52014-08-19 19:18:39 +02003981 }
3982#endif
3983
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003984 ssl->state = MBEDTLS_SSL_HELLO_REQUEST;
3985 ssl->renego_status = MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS;
Paul Bakker48916f92012-09-16 19:57:18 +00003986
Gilles Peskine449bd832023-01-11 14:50:10 +01003987 if ((ret = mbedtls_ssl_handshake(ssl)) != 0) {
3988 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ssl_handshake", ret);
3989 return ret;
Paul Bakker48916f92012-09-16 19:57:18 +00003990 }
3991
Gilles Peskine449bd832023-01-11 14:50:10 +01003992 MBEDTLS_SSL_DEBUG_MSG(2, ("<= renegotiate"));
Paul Bakker48916f92012-09-16 19:57:18 +00003993
Gilles Peskine449bd832023-01-11 14:50:10 +01003994 return 0;
Paul Bakker48916f92012-09-16 19:57:18 +00003995}
3996
3997/*
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01003998 * Renegotiate current connection on client,
3999 * or request renegotiation on server
4000 */
Gilles Peskine449bd832023-01-11 14:50:10 +01004001int mbedtls_ssl_renegotiate(mbedtls_ssl_context *ssl)
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01004002{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004003 int ret = MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01004004
Gilles Peskine449bd832023-01-11 14:50:10 +01004005 if (ssl == NULL || ssl->conf == NULL) {
4006 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
4007 }
Manuel Pégourié-Gonnardf81ee2e2015-09-01 17:43:40 +02004008
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004009#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01004010 /* On server, just send the request */
Gilles Peskine449bd832023-01-11 14:50:10 +01004011 if (ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER) {
4012 if (mbedtls_ssl_is_handshake_over(ssl) == 0) {
4013 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
4014 }
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01004015
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004016 ssl->renego_status = MBEDTLS_SSL_RENEGOTIATION_PENDING;
Manuel Pégourié-Gonnardf07f4212014-08-15 19:04:47 +02004017
4018 /* Did we already try/start sending HelloRequest? */
Gilles Peskine449bd832023-01-11 14:50:10 +01004019 if (ssl->out_left != 0) {
4020 return mbedtls_ssl_flush_output(ssl);
4021 }
Manuel Pégourié-Gonnardf07f4212014-08-15 19:04:47 +02004022
Gilles Peskine449bd832023-01-11 14:50:10 +01004023 return ssl_write_hello_request(ssl);
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01004024 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004025#endif /* MBEDTLS_SSL_SRV_C */
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01004026
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004027#if defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01004028 /*
4029 * On client, either start the renegotiation process or,
4030 * if already in progress, continue the handshake
4031 */
Gilles Peskine449bd832023-01-11 14:50:10 +01004032 if (ssl->renego_status != MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS) {
4033 if (mbedtls_ssl_is_handshake_over(ssl) == 0) {
4034 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01004035 }
Gilles Peskine449bd832023-01-11 14:50:10 +01004036
4037 if ((ret = mbedtls_ssl_start_renegotiation(ssl)) != 0) {
4038 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ssl_start_renegotiation", ret);
4039 return ret;
4040 }
4041 } else {
4042 if ((ret = mbedtls_ssl_handshake(ssl)) != 0) {
4043 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ssl_handshake", ret);
4044 return ret;
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01004045 }
4046 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004047#endif /* MBEDTLS_SSL_CLI_C */
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01004048
Gilles Peskine449bd832023-01-11 14:50:10 +01004049 return ret;
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01004050}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004051#endif /* MBEDTLS_SSL_RENEGOTIATION */
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01004052
Gilles Peskine449bd832023-01-11 14:50:10 +01004053void mbedtls_ssl_handshake_free(mbedtls_ssl_context *ssl)
Paul Bakker48916f92012-09-16 19:57:18 +00004054{
Gilles Peskine9b562d52018-04-25 20:32:43 +02004055 mbedtls_ssl_handshake_params *handshake = ssl->handshake;
4056
Gilles Peskine449bd832023-01-11 14:50:10 +01004057 if (handshake == NULL) {
Paul Bakkeraccaffe2014-06-26 13:37:14 +02004058 return;
Gilles Peskine449bd832023-01-11 14:50:10 +01004059 }
Paul Bakkeraccaffe2014-06-26 13:37:14 +02004060
Brett Warrene0edc842021-08-17 09:53:13 +01004061#if defined(MBEDTLS_ECP_C)
4062#if !defined(MBEDTLS_DEPRECATED_REMOVED)
Gilles Peskine449bd832023-01-11 14:50:10 +01004063 if (ssl->handshake->group_list_heap_allocated) {
4064 mbedtls_free((void *) handshake->group_list);
4065 }
Brett Warrene0edc842021-08-17 09:53:13 +01004066 handshake->group_list = NULL;
4067#endif /* MBEDTLS_DEPRECATED_REMOVED */
4068#endif /* MBEDTLS_ECP_C */
4069
Ronald Crone68ab4f2022-10-05 12:46:29 +02004070#if defined(MBEDTLS_SSL_HANDSHAKE_WITH_CERT_ENABLED)
Jerry Yuf017ee42022-01-12 15:49:48 +08004071#if !defined(MBEDTLS_DEPRECATED_REMOVED)
Gilles Peskine449bd832023-01-11 14:50:10 +01004072 if (ssl->handshake->sig_algs_heap_allocated) {
4073 mbedtls_free((void *) handshake->sig_algs);
4074 }
Jerry Yuf017ee42022-01-12 15:49:48 +08004075 handshake->sig_algs = NULL;
4076#endif /* MBEDTLS_DEPRECATED_REMOVED */
Xiaofei Baic234ecf2022-02-08 09:59:23 +00004077#if defined(MBEDTLS_SSL_PROTO_TLS1_3)
Gilles Peskine449bd832023-01-11 14:50:10 +01004078 if (ssl->handshake->certificate_request_context) {
4079 mbedtls_free((void *) handshake->certificate_request_context);
Xiaofei Baic234ecf2022-02-08 09:59:23 +00004080 }
4081#endif /* MBEDTLS_SSL_PROTO_TLS1_3 */
Ronald Crone68ab4f2022-10-05 12:46:29 +02004082#endif /* MBEDTLS_SSL_HANDSHAKE_WITH_CERT_ENABLED */
Jerry Yuf017ee42022-01-12 15:49:48 +08004083
Gilles Peskinedf13d5c2018-04-25 20:39:48 +02004084#if defined(MBEDTLS_SSL_ASYNC_PRIVATE)
Gilles Peskine449bd832023-01-11 14:50:10 +01004085 if (ssl->conf->f_async_cancel != NULL && handshake->async_in_progress != 0) {
4086 ssl->conf->f_async_cancel(ssl);
Gilles Peskinedf13d5c2018-04-25 20:39:48 +02004087 handshake->async_in_progress = 0;
4088 }
4089#endif /* MBEDTLS_SSL_ASYNC_PRIVATE */
4090
Andrzej Kurek25f27152022-08-17 16:09:31 -04004091#if defined(MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Andrzej Kurekeb342242019-01-29 09:14:33 -05004092#if defined(MBEDTLS_USE_PSA_CRYPTO)
Gilles Peskine449bd832023-01-11 14:50:10 +01004093 psa_hash_abort(&handshake->fin_sha256_psa);
Andrzej Kurekeb342242019-01-29 09:14:33 -05004094#else
Manuel Pégourié-Gonnardf057ecf2023-02-24 13:19:17 +01004095 mbedtls_md_free(&handshake->fin_sha256);
Manuel Pégourié-Gonnardb9d64e52015-07-06 14:18:56 +02004096#endif
Andrzej Kurekeb342242019-01-29 09:14:33 -05004097#endif
Andrzej Kurek25f27152022-08-17 16:09:31 -04004098#if defined(MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Andrzej Kurekeb342242019-01-29 09:14:33 -05004099#if defined(MBEDTLS_USE_PSA_CRYPTO)
Gilles Peskine449bd832023-01-11 14:50:10 +01004100 psa_hash_abort(&handshake->fin_sha384_psa);
Andrzej Kurekeb342242019-01-29 09:14:33 -05004101#else
Manuel Pégourié-Gonnardf057ecf2023-02-24 13:19:17 +01004102 mbedtls_md_free(&handshake->fin_sha384);
Manuel Pégourié-Gonnardb9d64e52015-07-06 14:18:56 +02004103#endif
Andrzej Kurekeb342242019-01-29 09:14:33 -05004104#endif
Manuel Pégourié-Gonnardb9d64e52015-07-06 14:18:56 +02004105
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004106#if defined(MBEDTLS_DHM_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01004107 mbedtls_dhm_free(&handshake->dhm_ctx);
Paul Bakker48916f92012-09-16 19:57:18 +00004108#endif
Neil Armstrongf3f46412022-04-12 14:43:39 +02004109#if !defined(MBEDTLS_USE_PSA_CRYPTO) && defined(MBEDTLS_ECDH_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01004110 mbedtls_ecdh_free(&handshake->ecdh_ctx);
Paul Bakker61d113b2013-07-04 11:51:43 +02004111#endif
Valerio Setti02c25b52022-11-15 14:08:42 +01004112
Manuel Pégourié-Gonnardeef142d2015-09-16 10:05:04 +02004113#if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
Neil Armstrongca7d5062022-05-31 14:43:23 +02004114#if defined(MBEDTLS_USE_PSA_CRYPTO)
Gilles Peskine449bd832023-01-11 14:50:10 +01004115 psa_pake_abort(&handshake->psa_pake_ctx);
Valerio Settieb3f7882022-12-08 18:42:58 +01004116 /*
4117 * Opaque keys are not stored in the handshake's data and it's the user
4118 * responsibility to destroy them. Clear ones, instead, are created by
4119 * the TLS library and should be destroyed at the same level
4120 */
Gilles Peskine449bd832023-01-11 14:50:10 +01004121 if (!mbedtls_svc_key_id_is_null(handshake->psa_pake_password)) {
4122 psa_destroy_key(handshake->psa_pake_password);
Valerio Settieb3f7882022-12-08 18:42:58 +01004123 }
Neil Armstrongca7d5062022-05-31 14:43:23 +02004124 handshake->psa_pake_password = MBEDTLS_SVC_KEY_ID_INIT;
4125#else
Gilles Peskine449bd832023-01-11 14:50:10 +01004126 mbedtls_ecjpake_free(&handshake->ecjpake_ctx);
Neil Armstrongca7d5062022-05-31 14:43:23 +02004127#endif /* MBEDTLS_USE_PSA_CRYPTO */
Manuel Pégourié-Gonnard77c06462015-09-17 13:59:49 +02004128#if defined(MBEDTLS_SSL_CLI_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01004129 mbedtls_free(handshake->ecjpake_cache);
Manuel Pégourié-Gonnard77c06462015-09-17 13:59:49 +02004130 handshake->ecjpake_cache = NULL;
4131 handshake->ecjpake_cache_len = 0;
4132#endif
Manuel Pégourié-Gonnard76cfd3f2015-09-15 12:10:54 +02004133#endif
Paul Bakker61d113b2013-07-04 11:51:43 +02004134
Janos Follath4ae5c292016-02-10 11:27:43 +00004135#if defined(MBEDTLS_ECDH_C) || defined(MBEDTLS_ECDSA_C) || \
4136 defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
Paul Bakker9af723c2014-05-01 13:03:14 +02004137 /* explicit void pointer cast for buggy MS compiler */
Gilles Peskine449bd832023-01-11 14:50:10 +01004138 mbedtls_free((void *) handshake->curves_tls_id);
Manuel Pégourié-Gonnardd09453c2013-09-23 19:11:32 +02004139#endif
4140
Ronald Cron73fe8df2022-10-05 14:31:43 +02004141#if defined(MBEDTLS_SSL_HANDSHAKE_WITH_PSK_ENABLED)
Neil Armstrong501c9322022-05-03 09:35:09 +02004142#if defined(MBEDTLS_USE_PSA_CRYPTO)
Gilles Peskine449bd832023-01-11 14:50:10 +01004143 if (!mbedtls_svc_key_id_is_null(ssl->handshake->psk_opaque)) {
Neil Armstrong501c9322022-05-03 09:35:09 +02004144 /* The maintenance of the external PSK key slot is the
4145 * user's responsibility. */
Gilles Peskine449bd832023-01-11 14:50:10 +01004146 if (ssl->handshake->psk_opaque_is_internal) {
4147 psa_destroy_key(ssl->handshake->psk_opaque);
Neil Armstrong501c9322022-05-03 09:35:09 +02004148 ssl->handshake->psk_opaque_is_internal = 0;
4149 }
4150 ssl->handshake->psk_opaque = MBEDTLS_SVC_KEY_ID_INIT;
4151 }
Neil Armstronge952a302022-05-03 10:22:14 +02004152#else
Gilles Peskine449bd832023-01-11 14:50:10 +01004153 if (handshake->psk != NULL) {
4154 mbedtls_platform_zeroize(handshake->psk, handshake->psk_len);
4155 mbedtls_free(handshake->psk);
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01004156 }
Neil Armstronge952a302022-05-03 10:22:14 +02004157#endif /* MBEDTLS_USE_PSA_CRYPTO */
Ronald Cron73fe8df2022-10-05 14:31:43 +02004158#endif /* MBEDTLS_SSL_HANDSHAKE_WITH_PSK_ENABLED */
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01004159
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004160#if defined(MBEDTLS_X509_CRT_PARSE_C) && \
4161 defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
Manuel Pégourié-Gonnard83724542013-09-24 22:30:56 +02004162 /*
4163 * Free only the linked list wrapper, not the keys themselves
4164 * since the belong to the SNI callback
4165 */
Gilles Peskine449bd832023-01-11 14:50:10 +01004166 ssl_key_cert_free(handshake->sni_key_cert);
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004167#endif /* MBEDTLS_X509_CRT_PARSE_C && MBEDTLS_SSL_SERVER_NAME_INDICATION */
Manuel Pégourié-Gonnard705fcca2013-09-23 20:04:20 +02004168
Gilles Peskineeccd8882020-03-10 12:19:08 +01004169#if defined(MBEDTLS_SSL_ECP_RESTARTABLE_ENABLED)
Gilles Peskine449bd832023-01-11 14:50:10 +01004170 mbedtls_x509_crt_restart_free(&handshake->ecrs_ctx);
4171 if (handshake->ecrs_peer_cert != NULL) {
4172 mbedtls_x509_crt_free(handshake->ecrs_peer_cert);
4173 mbedtls_free(handshake->ecrs_peer_cert);
Hanno Becker3dad3112019-02-05 17:19:52 +00004174 }
Manuel Pégourié-Gonnard862cde52017-05-17 11:56:15 +02004175#endif
4176
Hanno Becker75173122019-02-06 16:18:31 +00004177#if defined(MBEDTLS_X509_CRT_PARSE_C) && \
4178 !defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
Gilles Peskine449bd832023-01-11 14:50:10 +01004179 mbedtls_pk_free(&handshake->peer_pubkey);
Hanno Becker75173122019-02-06 16:18:31 +00004180#endif /* MBEDTLS_X509_CRT_PARSE_C && !MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
4181
XiaokangQian9b93c0d2022-02-09 06:02:25 +00004182#if defined(MBEDTLS_SSL_CLI_C) && \
Gilles Peskine449bd832023-01-11 14:50:10 +01004183 (defined(MBEDTLS_SSL_PROTO_DTLS) || defined(MBEDTLS_SSL_PROTO_TLS1_3))
4184 mbedtls_free(handshake->cookie);
XiaokangQian9b93c0d2022-02-09 06:02:25 +00004185#endif /* MBEDTLS_SSL_CLI_C &&
4186 ( MBEDTLS_SSL_PROTO_DTLS || MBEDTLS_SSL_PROTO_TLS1_3 ) */
XiaokangQian8499b6c2022-01-27 09:00:11 +00004187
4188#if defined(MBEDTLS_SSL_PROTO_DTLS)
Gilles Peskine449bd832023-01-11 14:50:10 +01004189 mbedtls_ssl_flight_free(handshake->flight);
4190 mbedtls_ssl_buffering_free(ssl);
XiaokangQian8499b6c2022-01-27 09:00:11 +00004191#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnard74848812014-07-11 02:43:49 +02004192
Ronald Cronf12b81d2022-03-15 10:42:41 +01004193#if defined(MBEDTLS_ECDH_C) && \
Gilles Peskine449bd832023-01-11 14:50:10 +01004194 (defined(MBEDTLS_USE_PSA_CRYPTO) || defined(MBEDTLS_SSL_PROTO_TLS1_3))
4195 if (handshake->ecdh_psa_privkey_is_external == 0) {
4196 psa_destroy_key(handshake->ecdh_psa_privkey);
4197 }
Hanno Becker4a63ed42019-01-08 11:39:35 +00004198#endif /* MBEDTLS_ECDH_C && MBEDTLS_USE_PSA_CRYPTO */
4199
Ronald Cron6f135e12021-12-08 16:57:54 +01004200#if defined(MBEDTLS_SSL_PROTO_TLS1_3)
Gilles Peskine449bd832023-01-11 14:50:10 +01004201 mbedtls_ssl_transform_free(handshake->transform_handshake);
4202 mbedtls_free(handshake->transform_handshake);
Jerry Yu3d9b5902022-11-04 14:07:25 +08004203#if defined(MBEDTLS_SSL_EARLY_DATA)
Gilles Peskine449bd832023-01-11 14:50:10 +01004204 mbedtls_ssl_transform_free(handshake->transform_earlydata);
4205 mbedtls_free(handshake->transform_earlydata);
Jerry Yu3d9b5902022-11-04 14:07:25 +08004206#endif
Ronald Cron6f135e12021-12-08 16:57:54 +01004207#endif /* MBEDTLS_SSL_PROTO_TLS1_3 */
Jerry Yuba9c7272021-10-30 11:54:10 +08004208
Andrzej Kurek0afa2a12020-03-03 10:39:58 -05004209
4210#if defined(MBEDTLS_SSL_VARIABLE_BUFFER_LENGTH)
4211 /* If the buffers are too big - reallocate. Because of the way Mbed TLS
4212 * processes datagrams and the fact that a datagram is allowed to have
4213 * several records in it, it is possible that the I/O buffers are not
4214 * empty at this stage */
Gilles Peskine449bd832023-01-11 14:50:10 +01004215 handle_buffer_resizing(ssl, 1, mbedtls_ssl_get_input_buflen(ssl),
4216 mbedtls_ssl_get_output_buflen(ssl));
Andrzej Kurek0afa2a12020-03-03 10:39:58 -05004217#endif
Hanno Becker3aa186f2021-08-10 09:24:19 +01004218
Jerry Yuba9c7272021-10-30 11:54:10 +08004219 /* mbedtls_platform_zeroize MUST be last one in this function */
Gilles Peskine449bd832023-01-11 14:50:10 +01004220 mbedtls_platform_zeroize(handshake,
4221 sizeof(mbedtls_ssl_handshake_params));
Paul Bakker48916f92012-09-16 19:57:18 +00004222}
4223
Gilles Peskine449bd832023-01-11 14:50:10 +01004224void mbedtls_ssl_session_free(mbedtls_ssl_session *session)
Paul Bakker48916f92012-09-16 19:57:18 +00004225{
Gilles Peskine449bd832023-01-11 14:50:10 +01004226 if (session == NULL) {
Paul Bakkeraccaffe2014-06-26 13:37:14 +02004227 return;
Gilles Peskine449bd832023-01-11 14:50:10 +01004228 }
Paul Bakkeraccaffe2014-06-26 13:37:14 +02004229
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004230#if defined(MBEDTLS_X509_CRT_PARSE_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01004231 ssl_clear_peer_cert(session);
Paul Bakkered27a042013-04-18 22:46:23 +02004232#endif
Paul Bakker0a597072012-09-25 21:55:46 +00004233
Manuel Pégourié-Gonnardb596abf2015-05-20 10:45:29 +02004234#if defined(MBEDTLS_SSL_SESSION_TICKETS) && defined(MBEDTLS_SSL_CLI_C)
Xiaokang Qianbc663a02022-10-09 11:14:39 +00004235#if defined(MBEDTLS_SSL_PROTO_TLS1_3) && \
4236 defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
Gilles Peskine449bd832023-01-11 14:50:10 +01004237 mbedtls_free(session->hostname);
Xiaokang Qian281fd1b2022-09-20 11:35:41 +00004238#endif
Gilles Peskine449bd832023-01-11 14:50:10 +01004239 mbedtls_free(session->ticket);
Paul Bakkera503a632013-08-14 13:48:06 +02004240#endif
Manuel Pégourié-Gonnard75d44012013-08-02 14:44:04 +02004241
Gilles Peskine449bd832023-01-11 14:50:10 +01004242 mbedtls_platform_zeroize(session, sizeof(mbedtls_ssl_session));
Paul Bakker48916f92012-09-16 19:57:18 +00004243}
4244
Manuel Pégourié-Gonnard5c0e3772019-07-23 16:13:17 +02004245#if defined(MBEDTLS_SSL_CONTEXT_SERIALIZATION)
Manuel Pégourié-Gonnard4e9370b2019-07-23 16:31:16 +02004246
4247#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
4248#define SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_CONNECTION_ID 1u
4249#else
4250#define SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_CONNECTION_ID 0u
4251#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
4252
Manuel Pégourié-Gonnard4e9370b2019-07-23 16:31:16 +02004253#define SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_BADMAC_LIMIT 1u
Manuel Pégourié-Gonnard4e9370b2019-07-23 16:31:16 +02004254
4255#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
4256#define SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_ANTI_REPLAY 1u
4257#else
4258#define SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_ANTI_REPLAY 0u
4259#endif /* MBEDTLS_SSL_DTLS_ANTI_REPLAY */
4260
4261#if defined(MBEDTLS_SSL_ALPN)
4262#define SSL_SERIALIZED_CONTEXT_CONFIG_ALPN 1u
4263#else
4264#define SSL_SERIALIZED_CONTEXT_CONFIG_ALPN 0u
4265#endif /* MBEDTLS_SSL_ALPN */
4266
4267#define SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_CONNECTION_ID_BIT 0
4268#define SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_BADMAC_LIMIT_BIT 1
4269#define SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_ANTI_REPLAY_BIT 2
4270#define SSL_SERIALIZED_CONTEXT_CONFIG_ALPN_BIT 3
4271
4272#define SSL_SERIALIZED_CONTEXT_CONFIG_BITFLAG \
Gilles Peskine449bd832023-01-11 14:50:10 +01004273 ((uint32_t) ( \
4274 (SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_CONNECTION_ID << \
4275 SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_CONNECTION_ID_BIT) | \
4276 (SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_BADMAC_LIMIT << \
4277 SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_BADMAC_LIMIT_BIT) | \
4278 (SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_ANTI_REPLAY << \
4279 SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_ANTI_REPLAY_BIT) | \
4280 (SSL_SERIALIZED_CONTEXT_CONFIG_ALPN << SSL_SERIALIZED_CONTEXT_CONFIG_ALPN_BIT) | \
4281 0u))
Manuel Pégourié-Gonnard4e9370b2019-07-23 16:31:16 +02004282
Manuel Pégourié-Gonnard4c90e852019-07-11 10:58:10 +02004283static unsigned char ssl_serialized_context_header[] = {
4284 MBEDTLS_VERSION_MAJOR,
4285 MBEDTLS_VERSION_MINOR,
4286 MBEDTLS_VERSION_PATCH,
Gilles Peskine449bd832023-01-11 14:50:10 +01004287 MBEDTLS_BYTE_1(SSL_SERIALIZED_SESSION_CONFIG_BITFLAG),
4288 MBEDTLS_BYTE_0(SSL_SERIALIZED_SESSION_CONFIG_BITFLAG),
4289 MBEDTLS_BYTE_2(SSL_SERIALIZED_CONTEXT_CONFIG_BITFLAG),
4290 MBEDTLS_BYTE_1(SSL_SERIALIZED_CONTEXT_CONFIG_BITFLAG),
4291 MBEDTLS_BYTE_0(SSL_SERIALIZED_CONTEXT_CONFIG_BITFLAG),
Manuel Pégourié-Gonnard4c90e852019-07-11 10:58:10 +02004292};
4293
Paul Bakker5121ce52009-01-03 21:22:43 +00004294/*
Manuel Pégourié-Gonnardac87e282019-05-28 13:02:16 +02004295 * Serialize a full SSL context
Manuel Pégourié-Gonnard00400c22019-07-10 14:58:45 +02004296 *
4297 * The format of the serialized data is:
4298 * (in the presentation language of TLS, RFC 8446 section 3)
4299 *
4300 * // header
4301 * opaque mbedtls_version[3]; // major, minor, patch
Manuel Pégourié-Gonnard4c90e852019-07-11 10:58:10 +02004302 * opaque context_format[5]; // version-specific field determining
Manuel Pégourié-Gonnard00400c22019-07-10 14:58:45 +02004303 * // the format of the remaining
Manuel Pégourié-Gonnard4c90e852019-07-11 10:58:10 +02004304 * // serialized data.
Manuel Pégourié-Gonnard4e9370b2019-07-23 16:31:16 +02004305 * Note: When updating the format, remember to keep these
4306 * version+format bytes. (We may make their size part of the API.)
Manuel Pégourié-Gonnard00400c22019-07-10 14:58:45 +02004307 *
4308 * // session sub-structure
4309 * opaque session<1..2^32-1>; // see mbedtls_ssl_session_save()
4310 * // transform sub-structure
4311 * uint8 random[64]; // ServerHello.random+ClientHello.random
4312 * uint8 in_cid<0..2^8-1> // Connection ID: expected incoming value
4313 * uint8 out_cid<0..2^8-1> // Connection ID: outgoing value to use
4314 * // fields from ssl_context
4315 * uint32 badmac_seen; // DTLS: number of records with failing MAC
4316 * uint64 in_window_top; // DTLS: last validated record seq_num
4317 * uint64 in_window; // DTLS: bitmask for replay protection
4318 * uint8 disable_datagram_packing; // DTLS: only one record per datagram
4319 * uint64 cur_out_ctr; // Record layer: outgoing sequence number
4320 * uint16 mtu; // DTLS: path mtu (max outgoing fragment size)
4321 * uint8 alpn_chosen<0..2^8-1> // ALPN: negotiated application protocol
4322 *
4323 * Note that many fields of the ssl_context or sub-structures are not
4324 * serialized, as they fall in one of the following categories:
4325 *
4326 * 1. forced value (eg in_left must be 0)
4327 * 2. pointer to dynamically-allocated memory (eg session, transform)
4328 * 3. value can be re-derived from other data (eg session keys from MS)
4329 * 4. value was temporary (eg content of input buffer)
4330 * 5. value will be provided by the user again (eg I/O callbacks and context)
Manuel Pégourié-Gonnardac87e282019-05-28 13:02:16 +02004331 */
Gilles Peskine449bd832023-01-11 14:50:10 +01004332int mbedtls_ssl_context_save(mbedtls_ssl_context *ssl,
4333 unsigned char *buf,
4334 size_t buf_len,
4335 size_t *olen)
Manuel Pégourié-Gonnardac87e282019-05-28 13:02:16 +02004336{
Manuel Pégourié-Gonnard4c90e852019-07-11 10:58:10 +02004337 unsigned char *p = buf;
4338 size_t used = 0;
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02004339 size_t session_len;
4340 int ret = 0;
Manuel Pégourié-Gonnard4c90e852019-07-11 10:58:10 +02004341
Manuel Pégourié-Gonnard1aaf6692019-07-10 14:14:05 +02004342 /*
Manuel Pégourié-Gonnarde4588692019-07-29 12:28:52 +02004343 * Enforce usage restrictions, see "return BAD_INPUT_DATA" in
4344 * this function's documentation.
4345 *
4346 * These are due to assumptions/limitations in the implementation. Some of
4347 * them are likely to stay (no handshake in progress) some might go away
4348 * (only DTLS) but are currently used to simplify the implementation.
Manuel Pégourié-Gonnard1aaf6692019-07-10 14:14:05 +02004349 */
Manuel Pégourié-Gonnarde4588692019-07-29 12:28:52 +02004350 /* The initial handshake must be over */
Gilles Peskine449bd832023-01-11 14:50:10 +01004351 if (mbedtls_ssl_is_handshake_over(ssl) == 0) {
4352 MBEDTLS_SSL_DEBUG_MSG(1, ("Initial handshake isn't over"));
4353 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
Jarno Lamsa8c51b7c2019-08-21 13:45:05 +03004354 }
Gilles Peskine449bd832023-01-11 14:50:10 +01004355 if (ssl->handshake != NULL) {
4356 MBEDTLS_SSL_DEBUG_MSG(1, ("Handshake isn't completed"));
4357 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
Jarno Lamsa8c51b7c2019-08-21 13:45:05 +03004358 }
Manuel Pégourié-Gonnarde4588692019-07-29 12:28:52 +02004359 /* Double-check that sub-structures are indeed ready */
Gilles Peskine449bd832023-01-11 14:50:10 +01004360 if (ssl->transform == NULL || ssl->session == NULL) {
4361 MBEDTLS_SSL_DEBUG_MSG(1, ("Serialised structures aren't ready"));
4362 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
Jarno Lamsa8c51b7c2019-08-21 13:45:05 +03004363 }
Manuel Pégourié-Gonnarde4588692019-07-29 12:28:52 +02004364 /* There must be no pending incoming or outgoing data */
Gilles Peskine449bd832023-01-11 14:50:10 +01004365 if (mbedtls_ssl_check_pending(ssl) != 0) {
4366 MBEDTLS_SSL_DEBUG_MSG(1, ("There is pending incoming data"));
4367 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
Jarno Lamsa8c51b7c2019-08-21 13:45:05 +03004368 }
Gilles Peskine449bd832023-01-11 14:50:10 +01004369 if (ssl->out_left != 0) {
4370 MBEDTLS_SSL_DEBUG_MSG(1, ("There is pending outgoing data"));
4371 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
Jarno Lamsa8c51b7c2019-08-21 13:45:05 +03004372 }
Dave Rodgman556e8a32022-12-06 16:31:25 +00004373 /* Protocol must be DTLS, not TLS */
Gilles Peskine449bd832023-01-11 14:50:10 +01004374 if (ssl->conf->transport != MBEDTLS_SSL_TRANSPORT_DATAGRAM) {
4375 MBEDTLS_SSL_DEBUG_MSG(1, ("Only DTLS is supported"));
4376 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
Jarno Lamsa8c51b7c2019-08-21 13:45:05 +03004377 }
Manuel Pégourié-Gonnarde4588692019-07-29 12:28:52 +02004378 /* Version must be 1.2 */
Gilles Peskine449bd832023-01-11 14:50:10 +01004379 if (ssl->tls_version != MBEDTLS_SSL_VERSION_TLS1_2) {
4380 MBEDTLS_SSL_DEBUG_MSG(1, ("Only version 1.2 supported"));
4381 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
Jarno Lamsa8c51b7c2019-08-21 13:45:05 +03004382 }
Manuel Pégourié-Gonnarde4588692019-07-29 12:28:52 +02004383 /* We must be using an AEAD ciphersuite */
Gilles Peskine449bd832023-01-11 14:50:10 +01004384 if (mbedtls_ssl_transform_uses_aead(ssl->transform) != 1) {
4385 MBEDTLS_SSL_DEBUG_MSG(1, ("Only AEAD ciphersuites supported"));
4386 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
Jarno Lamsa8c51b7c2019-08-21 13:45:05 +03004387 }
Manuel Pégourié-Gonnarde4588692019-07-29 12:28:52 +02004388 /* Renegotiation must not be enabled */
4389#if defined(MBEDTLS_SSL_RENEGOTIATION)
Gilles Peskine449bd832023-01-11 14:50:10 +01004390 if (ssl->conf->disable_renegotiation != MBEDTLS_SSL_RENEGOTIATION_DISABLED) {
4391 MBEDTLS_SSL_DEBUG_MSG(1, ("Renegotiation must not be enabled"));
4392 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
Jarno Lamsa8c51b7c2019-08-21 13:45:05 +03004393 }
Manuel Pégourié-Gonnarde4588692019-07-29 12:28:52 +02004394#endif
Manuel Pégourié-Gonnardac87e282019-05-28 13:02:16 +02004395
Manuel Pégourié-Gonnard4c90e852019-07-11 10:58:10 +02004396 /*
4397 * Version and format identifier
4398 */
Gilles Peskine449bd832023-01-11 14:50:10 +01004399 used += sizeof(ssl_serialized_context_header);
Manuel Pégourié-Gonnardac87e282019-05-28 13:02:16 +02004400
Gilles Peskine449bd832023-01-11 14:50:10 +01004401 if (used <= buf_len) {
4402 memcpy(p, ssl_serialized_context_header,
4403 sizeof(ssl_serialized_context_header));
4404 p += sizeof(ssl_serialized_context_header);
Manuel Pégourié-Gonnard4c90e852019-07-11 10:58:10 +02004405 }
4406
4407 /*
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02004408 * Session (length + data)
4409 */
Gilles Peskine449bd832023-01-11 14:50:10 +01004410 ret = ssl_session_save(ssl->session, 1, NULL, 0, &session_len);
4411 if (ret != MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL) {
4412 return ret;
4413 }
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02004414
4415 used += 4 + session_len;
Gilles Peskine449bd832023-01-11 14:50:10 +01004416 if (used <= buf_len) {
4417 MBEDTLS_PUT_UINT32_BE(session_len, p, 0);
Joe Subbiani1f6c3ae2021-08-20 11:44:44 +01004418 p += 4;
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02004419
Gilles Peskine449bd832023-01-11 14:50:10 +01004420 ret = ssl_session_save(ssl->session, 1,
4421 p, session_len, &session_len);
4422 if (ret != 0) {
4423 return ret;
4424 }
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02004425
4426 p += session_len;
4427 }
4428
4429 /*
Manuel Pégourié-Gonnardc2a7b892019-07-15 09:04:11 +02004430 * Transform
4431 */
Gilles Peskine449bd832023-01-11 14:50:10 +01004432 used += sizeof(ssl->transform->randbytes);
4433 if (used <= buf_len) {
4434 memcpy(p, ssl->transform->randbytes,
4435 sizeof(ssl->transform->randbytes));
4436 p += sizeof(ssl->transform->randbytes);
Manuel Pégourié-Gonnardc2a7b892019-07-15 09:04:11 +02004437 }
4438
4439#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
4440 used += 2 + ssl->transform->in_cid_len + ssl->transform->out_cid_len;
Gilles Peskine449bd832023-01-11 14:50:10 +01004441 if (used <= buf_len) {
Manuel Pégourié-Gonnardc2a7b892019-07-15 09:04:11 +02004442 *p++ = ssl->transform->in_cid_len;
Gilles Peskine449bd832023-01-11 14:50:10 +01004443 memcpy(p, ssl->transform->in_cid, ssl->transform->in_cid_len);
Manuel Pégourié-Gonnardc2a7b892019-07-15 09:04:11 +02004444 p += ssl->transform->in_cid_len;
4445
4446 *p++ = ssl->transform->out_cid_len;
Gilles Peskine449bd832023-01-11 14:50:10 +01004447 memcpy(p, ssl->transform->out_cid, ssl->transform->out_cid_len);
Manuel Pégourié-Gonnardc2a7b892019-07-15 09:04:11 +02004448 p += ssl->transform->out_cid_len;
4449 }
4450#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
4451
4452 /*
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02004453 * Saved fields from top-level ssl_context structure
4454 */
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02004455 used += 4;
Gilles Peskine449bd832023-01-11 14:50:10 +01004456 if (used <= buf_len) {
4457 MBEDTLS_PUT_UINT32_BE(ssl->badmac_seen, p, 0);
Joe Subbiani1f6c3ae2021-08-20 11:44:44 +01004458 p += 4;
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02004459 }
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02004460
4461#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
4462 used += 16;
Gilles Peskine449bd832023-01-11 14:50:10 +01004463 if (used <= buf_len) {
4464 MBEDTLS_PUT_UINT64_BE(ssl->in_window_top, p, 0);
Joe Subbiani1f6c3ae2021-08-20 11:44:44 +01004465 p += 8;
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02004466
Gilles Peskine449bd832023-01-11 14:50:10 +01004467 MBEDTLS_PUT_UINT64_BE(ssl->in_window, p, 0);
Joe Subbiani1f6c3ae2021-08-20 11:44:44 +01004468 p += 8;
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02004469 }
4470#endif /* MBEDTLS_SSL_DTLS_ANTI_REPLAY */
4471
4472#if defined(MBEDTLS_SSL_PROTO_DTLS)
4473 used += 1;
Gilles Peskine449bd832023-01-11 14:50:10 +01004474 if (used <= buf_len) {
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02004475 *p++ = ssl->disable_datagram_packing;
4476 }
4477#endif /* MBEDTLS_SSL_PROTO_DTLS */
4478
Jerry Yuae0b2e22021-10-08 15:21:19 +08004479 used += MBEDTLS_SSL_SEQUENCE_NUMBER_LEN;
Gilles Peskine449bd832023-01-11 14:50:10 +01004480 if (used <= buf_len) {
4481 memcpy(p, ssl->cur_out_ctr, MBEDTLS_SSL_SEQUENCE_NUMBER_LEN);
Jerry Yuae0b2e22021-10-08 15:21:19 +08004482 p += MBEDTLS_SSL_SEQUENCE_NUMBER_LEN;
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02004483 }
4484
4485#if defined(MBEDTLS_SSL_PROTO_DTLS)
4486 used += 2;
Gilles Peskine449bd832023-01-11 14:50:10 +01004487 if (used <= buf_len) {
4488 MBEDTLS_PUT_UINT16_BE(ssl->mtu, p, 0);
Joe Subbiani1f6c3ae2021-08-20 11:44:44 +01004489 p += 2;
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02004490 }
4491#endif /* MBEDTLS_SSL_PROTO_DTLS */
4492
4493#if defined(MBEDTLS_SSL_ALPN)
4494 {
4495 const uint8_t alpn_len = ssl->alpn_chosen
Gilles Peskine449bd832023-01-11 14:50:10 +01004496 ? (uint8_t) strlen(ssl->alpn_chosen)
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02004497 : 0;
4498
4499 used += 1 + alpn_len;
Gilles Peskine449bd832023-01-11 14:50:10 +01004500 if (used <= buf_len) {
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02004501 *p++ = alpn_len;
4502
Gilles Peskine449bd832023-01-11 14:50:10 +01004503 if (ssl->alpn_chosen != NULL) {
4504 memcpy(p, ssl->alpn_chosen, alpn_len);
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02004505 p += alpn_len;
4506 }
4507 }
4508 }
4509#endif /* MBEDTLS_SSL_ALPN */
4510
4511 /*
Manuel Pégourié-Gonnard4c90e852019-07-11 10:58:10 +02004512 * Done
4513 */
4514 *olen = used;
4515
Gilles Peskine449bd832023-01-11 14:50:10 +01004516 if (used > buf_len) {
4517 return MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL;
4518 }
Manuel Pégourié-Gonnardac87e282019-05-28 13:02:16 +02004519
Gilles Peskine449bd832023-01-11 14:50:10 +01004520 MBEDTLS_SSL_DEBUG_BUF(4, "saved context", buf, used);
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02004521
Gilles Peskine449bd832023-01-11 14:50:10 +01004522 return mbedtls_ssl_session_reset_int(ssl, 0);
Manuel Pégourié-Gonnardac87e282019-05-28 13:02:16 +02004523}
4524
4525/*
Manuel Pégourié-Gonnardb9dfc9f2019-07-12 10:50:19 +02004526 * Deserialize context, see mbedtls_ssl_context_save() for format.
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02004527 *
4528 * This internal version is wrapped by a public function that cleans up in
4529 * case of error.
Manuel Pégourié-Gonnardac87e282019-05-28 13:02:16 +02004530 */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02004531MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01004532static int ssl_context_load(mbedtls_ssl_context *ssl,
4533 const unsigned char *buf,
4534 size_t len)
Manuel Pégourié-Gonnardac87e282019-05-28 13:02:16 +02004535{
Manuel Pégourié-Gonnard4c90e852019-07-11 10:58:10 +02004536 const unsigned char *p = buf;
4537 const unsigned char * const end = buf + len;
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02004538 size_t session_len;
Janos Follath865b3eb2019-12-16 11:46:15 +00004539 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Andrzej Kurek2d59dbc2022-10-13 08:34:38 -04004540#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
Andrzej Kurek894edde2022-09-29 06:31:14 -04004541 tls_prf_fn prf_func = NULL;
Andrzej Kurek2d59dbc2022-10-13 08:34:38 -04004542#endif
Manuel Pégourié-Gonnard4c90e852019-07-11 10:58:10 +02004543
Manuel Pégourié-Gonnard0ff76402019-07-11 09:56:30 +02004544 /*
4545 * The context should have been freshly setup or reset.
4546 * Give the user an error in case of obvious misuse.
Manuel Pégourié-Gonnard4ca930f2019-07-26 16:31:53 +02004547 * (Checking session is useful because it won't be NULL if we're
Manuel Pégourié-Gonnard0ff76402019-07-11 09:56:30 +02004548 * renegotiating, or if the user mistakenly loaded a session first.)
4549 */
Gilles Peskine449bd832023-01-11 14:50:10 +01004550 if (ssl->state != MBEDTLS_SSL_HELLO_REQUEST ||
4551 ssl->session != NULL) {
4552 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
Manuel Pégourié-Gonnard0ff76402019-07-11 09:56:30 +02004553 }
4554
4555 /*
4556 * We can't check that the config matches the initial one, but we can at
4557 * least check it matches the requirements for serializing.
4558 */
Gilles Peskine449bd832023-01-11 14:50:10 +01004559 if (ssl->conf->transport != MBEDTLS_SSL_TRANSPORT_DATAGRAM ||
Glenn Strauss2dfcea22022-03-14 17:26:42 -04004560 ssl->conf->max_tls_version < MBEDTLS_SSL_VERSION_TLS1_2 ||
4561 ssl->conf->min_tls_version > MBEDTLS_SSL_VERSION_TLS1_2 ||
Manuel Pégourié-Gonnard0ff76402019-07-11 09:56:30 +02004562#if defined(MBEDTLS_SSL_RENEGOTIATION)
Manuel Pégourié-Gonnard9a96fd72019-07-23 17:11:24 +02004563 ssl->conf->disable_renegotiation != MBEDTLS_SSL_RENEGOTIATION_DISABLED ||
Manuel Pégourié-Gonnard0ff76402019-07-11 09:56:30 +02004564#endif
Gilles Peskine449bd832023-01-11 14:50:10 +01004565 0) {
4566 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
Manuel Pégourié-Gonnard0ff76402019-07-11 09:56:30 +02004567 }
4568
Gilles Peskine449bd832023-01-11 14:50:10 +01004569 MBEDTLS_SSL_DEBUG_BUF(4, "context to load", buf, len);
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02004570
Manuel Pégourié-Gonnard4c90e852019-07-11 10:58:10 +02004571 /*
4572 * Check version identifier
4573 */
Gilles Peskine449bd832023-01-11 14:50:10 +01004574 if ((size_t) (end - p) < sizeof(ssl_serialized_context_header)) {
4575 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
Manuel Pégourié-Gonnard4c90e852019-07-11 10:58:10 +02004576 }
Gilles Peskine449bd832023-01-11 14:50:10 +01004577
4578 if (memcmp(p, ssl_serialized_context_header,
4579 sizeof(ssl_serialized_context_header)) != 0) {
4580 return MBEDTLS_ERR_SSL_VERSION_MISMATCH;
4581 }
4582 p += sizeof(ssl_serialized_context_header);
Manuel Pégourié-Gonnard4c90e852019-07-11 10:58:10 +02004583
4584 /*
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02004585 * Session
4586 */
Gilles Peskine449bd832023-01-11 14:50:10 +01004587 if ((size_t) (end - p) < 4) {
4588 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
4589 }
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02004590
Gilles Peskine449bd832023-01-11 14:50:10 +01004591 session_len = ((size_t) p[0] << 24) |
4592 ((size_t) p[1] << 16) |
4593 ((size_t) p[2] << 8) |
4594 ((size_t) p[3]);
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02004595 p += 4;
4596
Manuel Pégourié-Gonnard142ba732019-07-23 14:43:30 +02004597 /* This has been allocated by ssl_handshake_init(), called by
Hanno Becker43aefe22020-02-05 10:44:56 +00004598 * by either mbedtls_ssl_session_reset_int() or mbedtls_ssl_setup(). */
Manuel Pégourié-Gonnard142ba732019-07-23 14:43:30 +02004599 ssl->session = ssl->session_negotiate;
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02004600 ssl->session_in = ssl->session;
4601 ssl->session_out = ssl->session;
Manuel Pégourié-Gonnard142ba732019-07-23 14:43:30 +02004602 ssl->session_negotiate = NULL;
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02004603
Gilles Peskine449bd832023-01-11 14:50:10 +01004604 if ((size_t) (end - p) < session_len) {
4605 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
4606 }
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02004607
Gilles Peskine449bd832023-01-11 14:50:10 +01004608 ret = ssl_session_load(ssl->session, 1, p, session_len);
4609 if (ret != 0) {
4610 mbedtls_ssl_session_free(ssl->session);
4611 return ret;
Manuel Pégourié-Gonnard45ac1f02019-07-23 16:52:45 +02004612 }
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02004613
4614 p += session_len;
4615
4616 /*
Manuel Pégourié-Gonnardc2a7b892019-07-15 09:04:11 +02004617 * Transform
4618 */
4619
Manuel Pégourié-Gonnard142ba732019-07-23 14:43:30 +02004620 /* This has been allocated by ssl_handshake_init(), called by
Hanno Becker43aefe22020-02-05 10:44:56 +00004621 * by either mbedtls_ssl_session_reset_int() or mbedtls_ssl_setup(). */
Jerry Yu2e199812022-12-01 18:57:19 +08004622#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
Manuel Pégourié-Gonnard142ba732019-07-23 14:43:30 +02004623 ssl->transform = ssl->transform_negotiate;
Manuel Pégourié-Gonnardc2a7b892019-07-15 09:04:11 +02004624 ssl->transform_in = ssl->transform;
4625 ssl->transform_out = ssl->transform;
Manuel Pégourié-Gonnard142ba732019-07-23 14:43:30 +02004626 ssl->transform_negotiate = NULL;
Jerry Yu2e199812022-12-01 18:57:19 +08004627#endif
Manuel Pégourié-Gonnardc2a7b892019-07-15 09:04:11 +02004628
Andrzej Kurek2d59dbc2022-10-13 08:34:38 -04004629#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
Gilles Peskine449bd832023-01-11 14:50:10 +01004630 prf_func = ssl_tls12prf_from_cs(ssl->session->ciphersuite);
4631 if (prf_func == NULL) {
4632 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
4633 }
Andrzej Kurek894edde2022-09-29 06:31:14 -04004634
Manuel Pégourié-Gonnardc2a7b892019-07-15 09:04:11 +02004635 /* Read random bytes and populate structure */
Gilles Peskine449bd832023-01-11 14:50:10 +01004636 if ((size_t) (end - p) < sizeof(ssl->transform->randbytes)) {
4637 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
4638 }
Andrzej Kurek2d59dbc2022-10-13 08:34:38 -04004639
Gilles Peskine449bd832023-01-11 14:50:10 +01004640 ret = ssl_tls12_populate_transform(ssl->transform,
4641 ssl->session->ciphersuite,
4642 ssl->session->master,
Neil Armstrongf2c82f02022-04-05 11:16:53 +02004643#if defined(MBEDTLS_SSL_SOME_SUITES_USE_CBC_ETM)
Gilles Peskine449bd832023-01-11 14:50:10 +01004644 ssl->session->encrypt_then_mac,
Neil Armstrongf2c82f02022-04-05 11:16:53 +02004645#endif /* MBEDTLS_SSL_SOME_SUITES_USE_CBC_ETM */
Gilles Peskine449bd832023-01-11 14:50:10 +01004646 prf_func,
4647 p, /* currently pointing to randbytes */
4648 MBEDTLS_SSL_VERSION_TLS1_2, /* (D)TLS 1.2 is forced */
4649 ssl->conf->endpoint,
4650 ssl);
4651 if (ret != 0) {
4652 return ret;
4653 }
Jerry Yu840fbb22022-02-17 14:59:29 +08004654#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Gilles Peskine449bd832023-01-11 14:50:10 +01004655 p += sizeof(ssl->transform->randbytes);
Manuel Pégourié-Gonnardc2a7b892019-07-15 09:04:11 +02004656
4657#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
4658 /* Read connection IDs and store them */
Gilles Peskine449bd832023-01-11 14:50:10 +01004659 if ((size_t) (end - p) < 1) {
4660 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
4661 }
Manuel Pégourié-Gonnardc2a7b892019-07-15 09:04:11 +02004662
4663 ssl->transform->in_cid_len = *p++;
4664
Gilles Peskine449bd832023-01-11 14:50:10 +01004665 if ((size_t) (end - p) < ssl->transform->in_cid_len + 1u) {
4666 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
4667 }
Manuel Pégourié-Gonnardc2a7b892019-07-15 09:04:11 +02004668
Gilles Peskine449bd832023-01-11 14:50:10 +01004669 memcpy(ssl->transform->in_cid, p, ssl->transform->in_cid_len);
Manuel Pégourié-Gonnardc2a7b892019-07-15 09:04:11 +02004670 p += ssl->transform->in_cid_len;
4671
4672 ssl->transform->out_cid_len = *p++;
4673
Gilles Peskine449bd832023-01-11 14:50:10 +01004674 if ((size_t) (end - p) < ssl->transform->out_cid_len) {
4675 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
4676 }
Manuel Pégourié-Gonnardc2a7b892019-07-15 09:04:11 +02004677
Gilles Peskine449bd832023-01-11 14:50:10 +01004678 memcpy(ssl->transform->out_cid, p, ssl->transform->out_cid_len);
Manuel Pégourié-Gonnardc2a7b892019-07-15 09:04:11 +02004679 p += ssl->transform->out_cid_len;
4680#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
4681
4682 /*
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02004683 * Saved fields from top-level ssl_context structure
4684 */
Gilles Peskine449bd832023-01-11 14:50:10 +01004685 if ((size_t) (end - p) < 4) {
4686 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
4687 }
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02004688
Gilles Peskine449bd832023-01-11 14:50:10 +01004689 ssl->badmac_seen = ((uint32_t) p[0] << 24) |
4690 ((uint32_t) p[1] << 16) |
4691 ((uint32_t) p[2] << 8) |
4692 ((uint32_t) p[3]);
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02004693 p += 4;
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02004694
4695#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
Gilles Peskine449bd832023-01-11 14:50:10 +01004696 if ((size_t) (end - p) < 16) {
4697 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
4698 }
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02004699
Gilles Peskine449bd832023-01-11 14:50:10 +01004700 ssl->in_window_top = ((uint64_t) p[0] << 56) |
4701 ((uint64_t) p[1] << 48) |
4702 ((uint64_t) p[2] << 40) |
4703 ((uint64_t) p[3] << 32) |
4704 ((uint64_t) p[4] << 24) |
4705 ((uint64_t) p[5] << 16) |
4706 ((uint64_t) p[6] << 8) |
4707 ((uint64_t) p[7]);
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02004708 p += 8;
4709
Gilles Peskine449bd832023-01-11 14:50:10 +01004710 ssl->in_window = ((uint64_t) p[0] << 56) |
4711 ((uint64_t) p[1] << 48) |
4712 ((uint64_t) p[2] << 40) |
4713 ((uint64_t) p[3] << 32) |
4714 ((uint64_t) p[4] << 24) |
4715 ((uint64_t) p[5] << 16) |
4716 ((uint64_t) p[6] << 8) |
4717 ((uint64_t) p[7]);
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02004718 p += 8;
4719#endif /* MBEDTLS_SSL_DTLS_ANTI_REPLAY */
4720
4721#if defined(MBEDTLS_SSL_PROTO_DTLS)
Gilles Peskine449bd832023-01-11 14:50:10 +01004722 if ((size_t) (end - p) < 1) {
4723 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
4724 }
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02004725
4726 ssl->disable_datagram_packing = *p++;
4727#endif /* MBEDTLS_SSL_PROTO_DTLS */
4728
Gilles Peskine449bd832023-01-11 14:50:10 +01004729 if ((size_t) (end - p) < sizeof(ssl->cur_out_ctr)) {
4730 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
4731 }
4732 memcpy(ssl->cur_out_ctr, p, sizeof(ssl->cur_out_ctr));
4733 p += sizeof(ssl->cur_out_ctr);
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02004734
4735#if defined(MBEDTLS_SSL_PROTO_DTLS)
Gilles Peskine449bd832023-01-11 14:50:10 +01004736 if ((size_t) (end - p) < 2) {
4737 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
4738 }
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02004739
Gilles Peskine449bd832023-01-11 14:50:10 +01004740 ssl->mtu = (p[0] << 8) | p[1];
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02004741 p += 2;
4742#endif /* MBEDTLS_SSL_PROTO_DTLS */
4743
4744#if defined(MBEDTLS_SSL_ALPN)
4745 {
4746 uint8_t alpn_len;
4747 const char **cur;
4748
Gilles Peskine449bd832023-01-11 14:50:10 +01004749 if ((size_t) (end - p) < 1) {
4750 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
4751 }
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02004752
4753 alpn_len = *p++;
4754
Gilles Peskine449bd832023-01-11 14:50:10 +01004755 if (alpn_len != 0 && ssl->conf->alpn_list != NULL) {
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02004756 /* alpn_chosen should point to an item in the configured list */
Gilles Peskine449bd832023-01-11 14:50:10 +01004757 for (cur = ssl->conf->alpn_list; *cur != NULL; cur++) {
4758 if (strlen(*cur) == alpn_len &&
4759 memcmp(p, cur, alpn_len) == 0) {
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02004760 ssl->alpn_chosen = *cur;
4761 break;
4762 }
4763 }
4764 }
4765
4766 /* can only happen on conf mismatch */
Gilles Peskine449bd832023-01-11 14:50:10 +01004767 if (alpn_len != 0 && ssl->alpn_chosen == NULL) {
4768 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
4769 }
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02004770
4771 p += alpn_len;
4772 }
4773#endif /* MBEDTLS_SSL_ALPN */
4774
4775 /*
Manuel Pégourié-Gonnard0eb3eac2019-07-15 11:53:51 +02004776 * Forced fields from top-level ssl_context structure
4777 *
4778 * Most of them already set to the correct value by mbedtls_ssl_init() and
4779 * mbedtls_ssl_reset(), so we only need to set the remaining ones.
4780 */
4781 ssl->state = MBEDTLS_SSL_HANDSHAKE_OVER;
Glenn Strauss60bfe602022-03-14 19:04:24 -04004782 ssl->tls_version = MBEDTLS_SSL_VERSION_TLS1_2;
Manuel Pégourié-Gonnard0eb3eac2019-07-15 11:53:51 +02004783
Hanno Becker361b10d2019-08-30 10:42:49 +01004784 /* Adjust pointers for header fields of outgoing records to
4785 * the given transform, accounting for explicit IV and CID. */
Gilles Peskine449bd832023-01-11 14:50:10 +01004786 mbedtls_ssl_update_out_pointers(ssl, ssl->transform);
Hanno Becker361b10d2019-08-30 10:42:49 +01004787
Manuel Pégourié-Gonnard0eb3eac2019-07-15 11:53:51 +02004788#if defined(MBEDTLS_SSL_PROTO_DTLS)
4789 ssl->in_epoch = 1;
4790#endif
4791
4792 /* mbedtls_ssl_reset() leaves the handshake sub-structure allocated,
4793 * which we don't want - otherwise we'd end up freeing the wrong transform
Hanno Beckerce5f5fd2020-02-05 10:47:44 +00004794 * by calling mbedtls_ssl_handshake_wrapup_free_hs_transform()
4795 * inappropriately. */
Gilles Peskine449bd832023-01-11 14:50:10 +01004796 if (ssl->handshake != NULL) {
4797 mbedtls_ssl_handshake_free(ssl);
4798 mbedtls_free(ssl->handshake);
Manuel Pégourié-Gonnard0eb3eac2019-07-15 11:53:51 +02004799 ssl->handshake = NULL;
4800 }
4801
4802 /*
Manuel Pégourié-Gonnard4c90e852019-07-11 10:58:10 +02004803 * Done - should have consumed entire buffer
4804 */
Gilles Peskine449bd832023-01-11 14:50:10 +01004805 if (p != end) {
4806 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
4807 }
Manuel Pégourié-Gonnardac87e282019-05-28 13:02:16 +02004808
Gilles Peskine449bd832023-01-11 14:50:10 +01004809 return 0;
Manuel Pégourié-Gonnardac87e282019-05-28 13:02:16 +02004810}
4811
4812/*
Manuel Pégourié-Gonnardb9dfc9f2019-07-12 10:50:19 +02004813 * Deserialize context: public wrapper for error cleaning
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02004814 */
Gilles Peskine449bd832023-01-11 14:50:10 +01004815int mbedtls_ssl_context_load(mbedtls_ssl_context *context,
4816 const unsigned char *buf,
4817 size_t len)
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02004818{
Gilles Peskine449bd832023-01-11 14:50:10 +01004819 int ret = ssl_context_load(context, buf, len);
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02004820
Gilles Peskine449bd832023-01-11 14:50:10 +01004821 if (ret != 0) {
4822 mbedtls_ssl_free(context);
4823 }
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02004824
Gilles Peskine449bd832023-01-11 14:50:10 +01004825 return ret;
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02004826}
Manuel Pégourié-Gonnard5c0e3772019-07-23 16:13:17 +02004827#endif /* MBEDTLS_SSL_CONTEXT_SERIALIZATION */
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02004828
4829/*
Paul Bakker5121ce52009-01-03 21:22:43 +00004830 * Free an SSL context
4831 */
Gilles Peskine449bd832023-01-11 14:50:10 +01004832void mbedtls_ssl_free(mbedtls_ssl_context *ssl)
Paul Bakker5121ce52009-01-03 21:22:43 +00004833{
Gilles Peskine449bd832023-01-11 14:50:10 +01004834 if (ssl == NULL) {
Paul Bakkeraccaffe2014-06-26 13:37:14 +02004835 return;
Gilles Peskine449bd832023-01-11 14:50:10 +01004836 }
Paul Bakkeraccaffe2014-06-26 13:37:14 +02004837
Gilles Peskine449bd832023-01-11 14:50:10 +01004838 MBEDTLS_SSL_DEBUG_MSG(2, ("=> free"));
Paul Bakker5121ce52009-01-03 21:22:43 +00004839
Gilles Peskine449bd832023-01-11 14:50:10 +01004840 if (ssl->out_buf != NULL) {
sander-visserb8aa2072020-05-06 22:05:13 +02004841#if defined(MBEDTLS_SSL_VARIABLE_BUFFER_LENGTH)
4842 size_t out_buf_len = ssl->out_buf_len;
4843#else
4844 size_t out_buf_len = MBEDTLS_SSL_OUT_BUFFER_LEN;
4845#endif
4846
Gilles Peskine449bd832023-01-11 14:50:10 +01004847 mbedtls_platform_zeroize(ssl->out_buf, out_buf_len);
4848 mbedtls_free(ssl->out_buf);
Andrzej Kurek0afa2a12020-03-03 10:39:58 -05004849 ssl->out_buf = NULL;
Paul Bakker5121ce52009-01-03 21:22:43 +00004850 }
4851
Gilles Peskine449bd832023-01-11 14:50:10 +01004852 if (ssl->in_buf != NULL) {
sander-visserb8aa2072020-05-06 22:05:13 +02004853#if defined(MBEDTLS_SSL_VARIABLE_BUFFER_LENGTH)
4854 size_t in_buf_len = ssl->in_buf_len;
4855#else
4856 size_t in_buf_len = MBEDTLS_SSL_IN_BUFFER_LEN;
4857#endif
4858
Gilles Peskine449bd832023-01-11 14:50:10 +01004859 mbedtls_platform_zeroize(ssl->in_buf, in_buf_len);
4860 mbedtls_free(ssl->in_buf);
Andrzej Kurek0afa2a12020-03-03 10:39:58 -05004861 ssl->in_buf = NULL;
Paul Bakker5121ce52009-01-03 21:22:43 +00004862 }
4863
Gilles Peskine449bd832023-01-11 14:50:10 +01004864 if (ssl->transform) {
4865 mbedtls_ssl_transform_free(ssl->transform);
4866 mbedtls_free(ssl->transform);
Paul Bakker48916f92012-09-16 19:57:18 +00004867 }
4868
Gilles Peskine449bd832023-01-11 14:50:10 +01004869 if (ssl->handshake) {
4870 mbedtls_ssl_handshake_free(ssl);
4871 mbedtls_free(ssl->handshake);
Jerry Yu2e199812022-12-01 18:57:19 +08004872
4873#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
Gilles Peskine449bd832023-01-11 14:50:10 +01004874 mbedtls_ssl_transform_free(ssl->transform_negotiate);
4875 mbedtls_free(ssl->transform_negotiate);
Jerry Yu2e199812022-12-01 18:57:19 +08004876#endif
4877
Gilles Peskine449bd832023-01-11 14:50:10 +01004878 mbedtls_ssl_session_free(ssl->session_negotiate);
4879 mbedtls_free(ssl->session_negotiate);
Paul Bakker48916f92012-09-16 19:57:18 +00004880 }
4881
Ronald Cron6f135e12021-12-08 16:57:54 +01004882#if defined(MBEDTLS_SSL_PROTO_TLS1_3)
Gilles Peskine449bd832023-01-11 14:50:10 +01004883 mbedtls_ssl_transform_free(ssl->transform_application);
4884 mbedtls_free(ssl->transform_application);
Ronald Cron6f135e12021-12-08 16:57:54 +01004885#endif /* MBEDTLS_SSL_PROTO_TLS1_3 */
Hanno Becker3aa186f2021-08-10 09:24:19 +01004886
Gilles Peskine449bd832023-01-11 14:50:10 +01004887 if (ssl->session) {
4888 mbedtls_ssl_session_free(ssl->session);
4889 mbedtls_free(ssl->session);
Paul Bakkerc0463502013-02-14 11:19:38 +01004890 }
4891
Manuel Pégourié-Gonnard55fab2d2015-05-11 16:15:19 +02004892#if defined(MBEDTLS_X509_CRT_PARSE_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01004893 if (ssl->hostname != NULL) {
4894 mbedtls_platform_zeroize(ssl->hostname, strlen(ssl->hostname));
4895 mbedtls_free(ssl->hostname);
Paul Bakker5121ce52009-01-03 21:22:43 +00004896 }
Paul Bakker0be444a2013-08-27 21:55:01 +02004897#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00004898
Manuel Pégourié-Gonnarde057d3b2015-05-20 10:59:43 +02004899#if defined(MBEDTLS_SSL_DTLS_HELLO_VERIFY) && defined(MBEDTLS_SSL_SRV_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01004900 mbedtls_free(ssl->cli_id);
Manuel Pégourié-Gonnard43c02182014-07-22 17:32:01 +02004901#endif
4902
Gilles Peskine449bd832023-01-11 14:50:10 +01004903 MBEDTLS_SSL_DEBUG_MSG(2, ("<= free"));
Paul Bakker2da561c2009-02-05 18:00:28 +00004904
Paul Bakker86f04f42013-02-14 11:20:09 +01004905 /* Actually clear after last debug message */
Gilles Peskine449bd832023-01-11 14:50:10 +01004906 mbedtls_platform_zeroize(ssl, sizeof(mbedtls_ssl_context));
Paul Bakker5121ce52009-01-03 21:22:43 +00004907}
4908
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02004909/*
Shaun Case8b0ecbc2021-12-20 21:14:10 -08004910 * Initialize mbedtls_ssl_config
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02004911 */
Gilles Peskine449bd832023-01-11 14:50:10 +01004912void mbedtls_ssl_config_init(mbedtls_ssl_config *conf)
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02004913{
Gilles Peskine449bd832023-01-11 14:50:10 +01004914 memset(conf, 0, sizeof(mbedtls_ssl_config));
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02004915}
4916
Gilles Peskineae270bf2021-06-02 00:05:29 +02004917/* The selection should be the same as mbedtls_x509_crt_profile_default in
4918 * x509_crt.c, plus Montgomery curves for ECDHE. Here, the order matters:
Gilles Peskineb1940a72021-06-02 15:18:12 +02004919 * curves with a lower resource usage come first.
Gilles Peskineae270bf2021-06-02 00:05:29 +02004920 * See the documentation of mbedtls_ssl_conf_curves() for what we promise
Gilles Peskineb1940a72021-06-02 15:18:12 +02004921 * about this list.
4922 */
Brett Warrene0edc842021-08-17 09:53:13 +01004923static uint16_t ssl_preset_default_groups[] = {
Gilles Peskineae270bf2021-06-02 00:05:29 +02004924#if defined(MBEDTLS_ECP_DP_CURVE25519_ENABLED)
Brett Warrene0edc842021-08-17 09:53:13 +01004925 MBEDTLS_SSL_IANA_TLS_GROUP_X25519,
Gilles Peskineae270bf2021-06-02 00:05:29 +02004926#endif
4927#if defined(MBEDTLS_ECP_DP_SECP256R1_ENABLED)
Brett Warrene0edc842021-08-17 09:53:13 +01004928 MBEDTLS_SSL_IANA_TLS_GROUP_SECP256R1,
Gilles Peskineae270bf2021-06-02 00:05:29 +02004929#endif
Gilles Peskineb1940a72021-06-02 15:18:12 +02004930#if defined(MBEDTLS_ECP_DP_SECP384R1_ENABLED)
Brett Warrene0edc842021-08-17 09:53:13 +01004931 MBEDTLS_SSL_IANA_TLS_GROUP_SECP384R1,
Gilles Peskineb1940a72021-06-02 15:18:12 +02004932#endif
4933#if defined(MBEDTLS_ECP_DP_CURVE448_ENABLED)
Brett Warrene0edc842021-08-17 09:53:13 +01004934 MBEDTLS_SSL_IANA_TLS_GROUP_X448,
Gilles Peskineb1940a72021-06-02 15:18:12 +02004935#endif
4936#if defined(MBEDTLS_ECP_DP_SECP521R1_ENABLED)
Brett Warrene0edc842021-08-17 09:53:13 +01004937 MBEDTLS_SSL_IANA_TLS_GROUP_SECP521R1,
Gilles Peskineb1940a72021-06-02 15:18:12 +02004938#endif
Gilles Peskineae270bf2021-06-02 00:05:29 +02004939#if defined(MBEDTLS_ECP_DP_BP256R1_ENABLED)
Brett Warrene0edc842021-08-17 09:53:13 +01004940 MBEDTLS_SSL_IANA_TLS_GROUP_BP256R1,
Gilles Peskineae270bf2021-06-02 00:05:29 +02004941#endif
Gilles Peskineb1940a72021-06-02 15:18:12 +02004942#if defined(MBEDTLS_ECP_DP_BP384R1_ENABLED)
Brett Warrene0edc842021-08-17 09:53:13 +01004943 MBEDTLS_SSL_IANA_TLS_GROUP_BP384R1,
Gilles Peskineb1940a72021-06-02 15:18:12 +02004944#endif
4945#if defined(MBEDTLS_ECP_DP_BP512R1_ENABLED)
Brett Warrene0edc842021-08-17 09:53:13 +01004946 MBEDTLS_SSL_IANA_TLS_GROUP_BP512R1,
Gilles Peskineb1940a72021-06-02 15:18:12 +02004947#endif
Brett Warrene0edc842021-08-17 09:53:13 +01004948 MBEDTLS_SSL_IANA_TLS_GROUP_NONE
Gilles Peskineae270bf2021-06-02 00:05:29 +02004949};
Gilles Peskineae270bf2021-06-02 00:05:29 +02004950
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02004951static int ssl_preset_suiteb_ciphersuites[] = {
4952 MBEDTLS_TLS_ECDHE_ECDSA_WITH_AES_128_GCM_SHA256,
4953 MBEDTLS_TLS_ECDHE_ECDSA_WITH_AES_256_GCM_SHA384,
4954 0
4955};
4956
Ronald Crone68ab4f2022-10-05 12:46:29 +02004957#if defined(MBEDTLS_SSL_HANDSHAKE_WITH_CERT_ENABLED)
Hanno Becker9c6aa7b2021-08-10 13:50:43 +01004958
Jerry Yu909df7b2022-01-22 11:56:27 +08004959/* NOTICE:
Jerry Yu0b994b82022-01-25 17:22:12 +08004960 * For ssl_preset_*_sig_algs and ssl_tls12_preset_*_sig_algs, the following
Jerry Yu370e1462022-01-25 10:36:53 +08004961 * rules SHOULD be upheld.
4962 * - No duplicate entries.
4963 * - But if there is a good reason, do not change the order of the algorithms.
Jerry Yu09a99fc2022-07-28 14:22:17 +08004964 * - ssl_tls12_preset* is for TLS 1.2 use only.
Jerry Yu370e1462022-01-25 10:36:53 +08004965 * - ssl_preset_* is for TLS 1.3 only or hybrid TLS 1.3/1.2 handshakes.
Jerry Yu1a8b4812022-01-20 17:56:50 +08004966 */
Hanno Becker9c6aa7b2021-08-10 13:50:43 +01004967static uint16_t ssl_preset_default_sig_algs[] = {
Jerry Yu1a8b4812022-01-20 17:56:50 +08004968
Andrzej Kurek25f27152022-08-17 16:09:31 -04004969#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 +08004970 defined(MBEDTLS_ECP_DP_SECP256R1_ENABLED)
4971 MBEDTLS_TLS1_3_SIG_ECDSA_SECP256R1_SHA256,
Andrzej Kurekcccb0442022-08-19 03:42:11 -04004972#endif /* MBEDTLS_ECDSA_C && MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA &&
Jerry Yued5e9f42022-01-26 11:21:34 +08004973 MBEDTLS_ECP_DP_SECP256R1_ENABLED */
Jerry Yu909df7b2022-01-22 11:56:27 +08004974
Andrzej Kurek25f27152022-08-17 16:09:31 -04004975#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 +08004976 defined(MBEDTLS_ECP_DP_SECP384R1_ENABLED)
4977 MBEDTLS_TLS1_3_SIG_ECDSA_SECP384R1_SHA384,
Andrzej Kurekcccb0442022-08-19 03:42:11 -04004978#endif /* MBEDTLS_ECDSA_C && MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA&&
Jerry Yu909df7b2022-01-22 11:56:27 +08004979 MBEDTLS_ECP_DP_SECP384R1_ENABLED */
4980
Andrzej Kurek25f27152022-08-17 16:09:31 -04004981#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 +08004982 defined(MBEDTLS_ECP_DP_SECP521R1_ENABLED)
4983 MBEDTLS_TLS1_3_SIG_ECDSA_SECP521R1_SHA512,
Andrzej Kurekcccb0442022-08-19 03:42:11 -04004984#endif /* MBEDTLS_ECDSA_C && MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA&&
Jerry Yued5e9f42022-01-26 11:21:34 +08004985 MBEDTLS_ECP_DP_SECP521R1_ENABLED */
Jerry Yu909df7b2022-01-22 11:56:27 +08004986
Gilles Peskine449bd832023-01-11 14:50:10 +01004987#if defined(MBEDTLS_X509_RSASSA_PSS_SUPPORT) && \
4988 defined(MBEDTLS_HAS_ALG_SHA_512_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Jerry Yu9bb3ee42022-06-23 10:16:33 +08004989 MBEDTLS_TLS1_3_SIG_RSA_PSS_RSAE_SHA512,
Gilles Peskine449bd832023-01-11 14:50:10 +01004990#endif \
4991 /* 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 +08004992
Gilles Peskine449bd832023-01-11 14:50:10 +01004993#if defined(MBEDTLS_X509_RSASSA_PSS_SUPPORT) && \
4994 defined(MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Jerry Yu9bb3ee42022-06-23 10:16:33 +08004995 MBEDTLS_TLS1_3_SIG_RSA_PSS_RSAE_SHA384,
Gilles Peskine449bd832023-01-11 14:50:10 +01004996#endif \
4997 /* 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 +08004998
Gilles Peskine449bd832023-01-11 14:50:10 +01004999#if defined(MBEDTLS_X509_RSASSA_PSS_SUPPORT) && \
5000 defined(MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Jerry Yu9bb3ee42022-06-23 10:16:33 +08005001 MBEDTLS_TLS1_3_SIG_RSA_PSS_RSAE_SHA256,
Gilles Peskine449bd832023-01-11 14:50:10 +01005002#endif \
5003 /* 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 +08005004
Andrzej Kurek25f27152022-08-17 16:09:31 -04005005#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 +08005006 MBEDTLS_TLS1_3_SIG_RSA_PKCS1_SHA512,
5007#endif /* MBEDTLS_RSA_C && MBEDTLS_SHA512_C */
5008
Andrzej Kurek25f27152022-08-17 16:09:31 -04005009#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 +08005010 MBEDTLS_TLS1_3_SIG_RSA_PKCS1_SHA384,
5011#endif /* MBEDTLS_RSA_C && MBEDTLS_SHA384_C */
5012
Andrzej Kurek25f27152022-08-17 16:09:31 -04005013#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 +08005014 MBEDTLS_TLS1_3_SIG_RSA_PKCS1_SHA256,
5015#endif /* MBEDTLS_RSA_C && MBEDTLS_SHA256_C */
5016
Gabor Mezei15b95a62022-05-09 16:37:58 +02005017 MBEDTLS_TLS_SIG_NONE
Jerry Yu909df7b2022-01-22 11:56:27 +08005018};
5019
5020/* NOTICE: see above */
5021#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
5022static uint16_t ssl_tls12_preset_default_sig_algs[] = {
Andrzej Kurek25f27152022-08-17 16:09:31 -04005023#if defined(MBEDTLS_HAS_ALG_SHA_512_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Gabor Mezeic1051b62022-05-10 13:13:58 +02005024#if defined(MBEDTLS_ECDSA_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01005025 MBEDTLS_SSL_TLS12_SIG_AND_HASH_ALG(MBEDTLS_SSL_SIG_ECDSA, MBEDTLS_SSL_HASH_SHA512),
Jerry Yu713013f2022-01-17 18:16:35 +08005026#endif
Jerry Yu09a99fc2022-07-28 14:22:17 +08005027#if defined(MBEDTLS_X509_RSASSA_PSS_SUPPORT)
5028 MBEDTLS_TLS1_3_SIG_RSA_PSS_RSAE_SHA512,
5029#endif /* MBEDTLS_X509_RSASSA_PSS_SUPPORT */
Gabor Mezeic1051b62022-05-10 13:13:58 +02005030#if defined(MBEDTLS_RSA_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01005031 MBEDTLS_SSL_TLS12_SIG_AND_HASH_ALG(MBEDTLS_SSL_SIG_RSA, MBEDTLS_SSL_HASH_SHA512),
Gabor Mezeic1051b62022-05-10 13:13:58 +02005032#endif
Andrzej Kurekcccb0442022-08-19 03:42:11 -04005033#endif /* MBEDTLS_HAS_ALG_SHA_512_VIA_MD_OR_PSA_BASED_ON_USE_PSA*/
Andrzej Kurek25f27152022-08-17 16:09:31 -04005034#if defined(MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Gabor Mezeic1051b62022-05-10 13:13:58 +02005035#if defined(MBEDTLS_ECDSA_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01005036 MBEDTLS_SSL_TLS12_SIG_AND_HASH_ALG(MBEDTLS_SSL_SIG_ECDSA, MBEDTLS_SSL_HASH_SHA384),
Jerry Yu11f0a9c2022-01-12 18:43:08 +08005037#endif
Jerry Yu09a99fc2022-07-28 14:22:17 +08005038#if defined(MBEDTLS_X509_RSASSA_PSS_SUPPORT)
5039 MBEDTLS_TLS1_3_SIG_RSA_PSS_RSAE_SHA384,
5040#endif /* MBEDTLS_X509_RSASSA_PSS_SUPPORT */
Gabor Mezeic1051b62022-05-10 13:13:58 +02005041#if defined(MBEDTLS_RSA_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01005042 MBEDTLS_SSL_TLS12_SIG_AND_HASH_ALG(MBEDTLS_SSL_SIG_RSA, MBEDTLS_SSL_HASH_SHA384),
Gabor Mezeic1051b62022-05-10 13:13:58 +02005043#endif
Andrzej Kurekcccb0442022-08-19 03:42:11 -04005044#endif /* MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA*/
Andrzej Kurek25f27152022-08-17 16:09:31 -04005045#if defined(MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Gabor Mezeic1051b62022-05-10 13:13:58 +02005046#if defined(MBEDTLS_ECDSA_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01005047 MBEDTLS_SSL_TLS12_SIG_AND_HASH_ALG(MBEDTLS_SSL_SIG_ECDSA, MBEDTLS_SSL_HASH_SHA256),
Jerry Yu11f0a9c2022-01-12 18:43:08 +08005048#endif
Jerry Yu09a99fc2022-07-28 14:22:17 +08005049#if defined(MBEDTLS_X509_RSASSA_PSS_SUPPORT)
5050 MBEDTLS_TLS1_3_SIG_RSA_PSS_RSAE_SHA256,
5051#endif /* MBEDTLS_X509_RSASSA_PSS_SUPPORT */
Gabor Mezeic1051b62022-05-10 13:13:58 +02005052#if defined(MBEDTLS_RSA_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01005053 MBEDTLS_SSL_TLS12_SIG_AND_HASH_ALG(MBEDTLS_SSL_SIG_RSA, MBEDTLS_SSL_HASH_SHA256),
Gabor Mezeic1051b62022-05-10 13:13:58 +02005054#endif
Andrzej Kurekcccb0442022-08-19 03:42:11 -04005055#endif /* MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA*/
Gabor Mezei15b95a62022-05-09 16:37:58 +02005056 MBEDTLS_TLS_SIG_NONE
Jerry Yu909df7b2022-01-22 11:56:27 +08005057};
5058#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
5059/* NOTICE: see above */
5060static uint16_t ssl_preset_suiteb_sig_algs[] = {
5061
Andrzej Kurek25f27152022-08-17 16:09:31 -04005062#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 +08005063 defined(MBEDTLS_ECP_DP_SECP256R1_ENABLED)
5064 MBEDTLS_TLS1_3_SIG_ECDSA_SECP256R1_SHA256,
Andrzej Kurekcccb0442022-08-19 03:42:11 -04005065#endif /* MBEDTLS_ECDSA_C && MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA&&
Jerry Yu909df7b2022-01-22 11:56:27 +08005066 MBEDTLS_ECP_DP_SECP256R1_ENABLED */
5067
Andrzej Kurek25f27152022-08-17 16:09:31 -04005068#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 +08005069 defined(MBEDTLS_ECP_DP_SECP384R1_ENABLED)
5070 MBEDTLS_TLS1_3_SIG_ECDSA_SECP384R1_SHA384,
Andrzej Kurekcccb0442022-08-19 03:42:11 -04005071#endif /* MBEDTLS_ECDSA_C && MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA&&
Jerry Yu53037892022-01-25 11:02:06 +08005072 MBEDTLS_ECP_DP_SECP384R1_ENABLED */
Jerry Yu909df7b2022-01-22 11:56:27 +08005073
Gilles Peskine449bd832023-01-11 14:50:10 +01005074#if defined(MBEDTLS_X509_RSASSA_PSS_SUPPORT) && \
5075 defined(MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Jerry Yu909df7b2022-01-22 11:56:27 +08005076 MBEDTLS_TLS1_3_SIG_RSA_PSS_RSAE_SHA256,
Gilles Peskine449bd832023-01-11 14:50:10 +01005077#endif \
5078 /* 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 +08005079
Andrzej Kurek25f27152022-08-17 16:09:31 -04005080#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 +08005081 MBEDTLS_TLS1_3_SIG_RSA_PKCS1_SHA256,
Andrzej Kurekcccb0442022-08-19 03:42:11 -04005082#endif /* MBEDTLS_RSA_C && MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA*/
Jerry Yu53037892022-01-25 11:02:06 +08005083
Gabor Mezei15b95a62022-05-09 16:37:58 +02005084 MBEDTLS_TLS_SIG_NONE
Jerry Yu713013f2022-01-17 18:16:35 +08005085};
Jerry Yu6106fdc2022-01-12 16:36:14 +08005086
Jerry Yu909df7b2022-01-22 11:56:27 +08005087/* NOTICE: see above */
5088#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
5089static uint16_t ssl_tls12_preset_suiteb_sig_algs[] = {
Andrzej Kurek25f27152022-08-17 16:09:31 -04005090#if defined(MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Gabor Mezeic1051b62022-05-10 13:13:58 +02005091#if defined(MBEDTLS_ECDSA_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01005092 MBEDTLS_SSL_TLS12_SIG_AND_HASH_ALG(MBEDTLS_SSL_SIG_ECDSA, MBEDTLS_SSL_HASH_SHA256),
Jerry Yu713013f2022-01-17 18:16:35 +08005093#endif
Gabor Mezeic1051b62022-05-10 13:13:58 +02005094#if defined(MBEDTLS_RSA_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01005095 MBEDTLS_SSL_TLS12_SIG_AND_HASH_ALG(MBEDTLS_SSL_SIG_RSA, MBEDTLS_SSL_HASH_SHA256),
Gabor Mezeic1051b62022-05-10 13:13:58 +02005096#endif
Andrzej Kurekcccb0442022-08-19 03:42:11 -04005097#endif /* MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA*/
Andrzej Kurek25f27152022-08-17 16:09:31 -04005098#if defined(MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Gabor Mezeic1051b62022-05-10 13:13:58 +02005099#if defined(MBEDTLS_ECDSA_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01005100 MBEDTLS_SSL_TLS12_SIG_AND_HASH_ALG(MBEDTLS_SSL_SIG_ECDSA, MBEDTLS_SSL_HASH_SHA384),
Jerry Yu18c833e2022-01-25 10:55:47 +08005101#endif
Gabor Mezeic1051b62022-05-10 13:13:58 +02005102#if defined(MBEDTLS_RSA_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01005103 MBEDTLS_SSL_TLS12_SIG_AND_HASH_ALG(MBEDTLS_SSL_SIG_RSA, MBEDTLS_SSL_HASH_SHA384),
Gabor Mezeic1051b62022-05-10 13:13:58 +02005104#endif
Andrzej Kurekcccb0442022-08-19 03:42:11 -04005105#endif /* MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA*/
Gabor Mezei15b95a62022-05-09 16:37:58 +02005106 MBEDTLS_TLS_SIG_NONE
Hanno Becker9c6aa7b2021-08-10 13:50:43 +01005107};
Jerry Yu909df7b2022-01-22 11:56:27 +08005108#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
5109
Ronald Crone68ab4f2022-10-05 12:46:29 +02005110#endif /* MBEDTLS_SSL_HANDSHAKE_WITH_CERT_ENABLED */
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02005111
Brett Warrene0edc842021-08-17 09:53:13 +01005112static uint16_t ssl_preset_suiteb_groups[] = {
Jaeden Amerod4311042019-06-03 08:27:16 +01005113#if defined(MBEDTLS_ECP_DP_SECP256R1_ENABLED)
Brett Warrene0edc842021-08-17 09:53:13 +01005114 MBEDTLS_SSL_IANA_TLS_GROUP_SECP256R1,
Jaeden Amerod4311042019-06-03 08:27:16 +01005115#endif
5116#if defined(MBEDTLS_ECP_DP_SECP384R1_ENABLED)
Brett Warrene0edc842021-08-17 09:53:13 +01005117 MBEDTLS_SSL_IANA_TLS_GROUP_SECP384R1,
Jaeden Amerod4311042019-06-03 08:27:16 +01005118#endif
Brett Warrene0edc842021-08-17 09:53:13 +01005119 MBEDTLS_SSL_IANA_TLS_GROUP_NONE
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02005120};
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02005121
Ronald Crone68ab4f2022-10-05 12:46:29 +02005122#if defined(MBEDTLS_DEBUG_C) && defined(MBEDTLS_SSL_HANDSHAKE_WITH_CERT_ENABLED)
Jerry Yu909df7b2022-01-22 11:56:27 +08005123/* Function for checking `ssl_preset_*_sig_algs` and `ssl_tls12_preset_*_sig_algs`
Jerry Yu370e1462022-01-25 10:36:53 +08005124 * to make sure there are no duplicated signature algorithm entries. */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02005125MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01005126static int ssl_check_no_sig_alg_duplication(uint16_t *sig_algs)
Jerry Yu1a8b4812022-01-20 17:56:50 +08005127{
5128 size_t i, j;
5129 int ret = 0;
Jerry Yu909df7b2022-01-22 11:56:27 +08005130
Gilles Peskine449bd832023-01-11 14:50:10 +01005131 for (i = 0; sig_algs[i] != MBEDTLS_TLS_SIG_NONE; i++) {
5132 for (j = 0; j < i; j++) {
5133 if (sig_algs[i] != sig_algs[j]) {
Jerry Yuf377d642022-01-25 10:43:59 +08005134 continue;
Gilles Peskine449bd832023-01-11 14:50:10 +01005135 }
5136 mbedtls_printf(" entry(%04x,%" MBEDTLS_PRINTF_SIZET
5137 ") is duplicated at %" MBEDTLS_PRINTF_SIZET "\n",
5138 sig_algs[i], j, i);
Jerry Yuf377d642022-01-25 10:43:59 +08005139 ret = -1;
Jerry Yu1a8b4812022-01-20 17:56:50 +08005140 }
5141 }
Gilles Peskine449bd832023-01-11 14:50:10 +01005142 return ret;
Jerry Yu1a8b4812022-01-20 17:56:50 +08005143}
5144
Ronald Crone68ab4f2022-10-05 12:46:29 +02005145#endif /* MBEDTLS_DEBUG_C && MBEDTLS_SSL_HANDSHAKE_WITH_CERT_ENABLED */
Jerry Yu1a8b4812022-01-20 17:56:50 +08005146
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02005147/*
Tillmann Karras588ad502015-09-25 04:27:22 +02005148 * Load default in mbedtls_ssl_config
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02005149 */
Gilles Peskine449bd832023-01-11 14:50:10 +01005150int mbedtls_ssl_config_defaults(mbedtls_ssl_config *conf,
5151 int endpoint, int transport, int preset)
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02005152{
Manuel Pégourié-Gonnard8b431fb2015-05-11 12:54:52 +02005153#if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_SSL_SRV_C)
Janos Follath865b3eb2019-12-16 11:46:15 +00005154 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnard8b431fb2015-05-11 12:54:52 +02005155#endif
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02005156
Ronald Crone68ab4f2022-10-05 12:46:29 +02005157#if defined(MBEDTLS_DEBUG_C) && defined(MBEDTLS_SSL_HANDSHAKE_WITH_CERT_ENABLED)
Gilles Peskine449bd832023-01-11 14:50:10 +01005158 if (ssl_check_no_sig_alg_duplication(ssl_preset_suiteb_sig_algs)) {
5159 mbedtls_printf("ssl_preset_suiteb_sig_algs has duplicated entries\n");
5160 return MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Jerry Yu1a8b4812022-01-20 17:56:50 +08005161 }
5162
Gilles Peskine449bd832023-01-11 14:50:10 +01005163 if (ssl_check_no_sig_alg_duplication(ssl_preset_default_sig_algs)) {
5164 mbedtls_printf("ssl_preset_default_sig_algs has duplicated entries\n");
5165 return MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Jerry Yu1a8b4812022-01-20 17:56:50 +08005166 }
Jerry Yu909df7b2022-01-22 11:56:27 +08005167
5168#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
Gilles Peskine449bd832023-01-11 14:50:10 +01005169 if (ssl_check_no_sig_alg_duplication(ssl_tls12_preset_suiteb_sig_algs)) {
5170 mbedtls_printf("ssl_tls12_preset_suiteb_sig_algs has duplicated entries\n");
5171 return MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Jerry Yu909df7b2022-01-22 11:56:27 +08005172 }
5173
Gilles Peskine449bd832023-01-11 14:50:10 +01005174 if (ssl_check_no_sig_alg_duplication(ssl_tls12_preset_default_sig_algs)) {
5175 mbedtls_printf("ssl_tls12_preset_default_sig_algs has duplicated entries\n");
5176 return MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Jerry Yu909df7b2022-01-22 11:56:27 +08005177 }
5178#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Ronald Crone68ab4f2022-10-05 12:46:29 +02005179#endif /* MBEDTLS_DEBUG_C && MBEDTLS_SSL_HANDSHAKE_WITH_CERT_ENABLED */
Jerry Yu1a8b4812022-01-20 17:56:50 +08005180
Manuel Pégourié-Gonnard0de074f2015-05-14 12:58:01 +02005181 /* Use the functions here so that they are covered in tests,
5182 * but otherwise access member directly for efficiency */
Gilles Peskine449bd832023-01-11 14:50:10 +01005183 mbedtls_ssl_conf_endpoint(conf, endpoint);
5184 mbedtls_ssl_conf_transport(conf, transport);
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02005185
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02005186 /*
5187 * Things that are common to all presets
5188 */
Manuel Pégourié-Gonnard419d5ae2015-05-04 19:32:36 +02005189#if defined(MBEDTLS_SSL_CLI_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01005190 if (endpoint == MBEDTLS_SSL_IS_CLIENT) {
Manuel Pégourié-Gonnard419d5ae2015-05-04 19:32:36 +02005191 conf->authmode = MBEDTLS_SSL_VERIFY_REQUIRED;
5192#if defined(MBEDTLS_SSL_SESSION_TICKETS)
5193 conf->session_tickets = MBEDTLS_SSL_SESSION_TICKETS_ENABLED;
5194#endif
5195 }
5196#endif
5197
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02005198#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
5199 conf->encrypt_then_mac = MBEDTLS_SSL_ETM_ENABLED;
5200#endif
5201
5202#if defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET)
5203 conf->extended_ms = MBEDTLS_SSL_EXTENDED_MS_ENABLED;
5204#endif
5205
Manuel Pégourié-Gonnarde057d3b2015-05-20 10:59:43 +02005206#if defined(MBEDTLS_SSL_DTLS_HELLO_VERIFY) && defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02005207 conf->f_cookie_write = ssl_cookie_write_dummy;
5208 conf->f_cookie_check = ssl_cookie_check_dummy;
5209#endif
5210
5211#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
5212 conf->anti_replay = MBEDTLS_SSL_ANTI_REPLAY_ENABLED;
5213#endif
5214
Janos Follath088ce432017-04-10 12:42:31 +01005215#if defined(MBEDTLS_SSL_SRV_C)
5216 conf->cert_req_ca_list = MBEDTLS_SSL_CERT_REQ_CA_LIST_ENABLED;
TRodziewicz3946f792021-06-14 12:11:18 +02005217 conf->respect_cli_pref = MBEDTLS_SSL_SRV_CIPHERSUITE_ORDER_SERVER;
Janos Follath088ce432017-04-10 12:42:31 +01005218#endif
5219
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02005220#if defined(MBEDTLS_SSL_PROTO_DTLS)
5221 conf->hs_timeout_min = MBEDTLS_SSL_DTLS_TIMEOUT_DFL_MIN;
5222 conf->hs_timeout_max = MBEDTLS_SSL_DTLS_TIMEOUT_DFL_MAX;
5223#endif
5224
5225#if defined(MBEDTLS_SSL_RENEGOTIATION)
5226 conf->renego_max_records = MBEDTLS_SSL_RENEGO_MAX_RECORDS_DEFAULT;
Gilles Peskine449bd832023-01-11 14:50:10 +01005227 memset(conf->renego_period, 0x00, 2);
5228 memset(conf->renego_period + 2, 0xFF, 6);
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02005229#endif
5230
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02005231#if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_SSL_SRV_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01005232 if (endpoint == MBEDTLS_SSL_IS_SERVER) {
Hanno Beckere2defad2021-07-24 05:59:17 +01005233 const unsigned char dhm_p[] =
5234 MBEDTLS_DHM_RFC3526_MODP_2048_P_BIN;
5235 const unsigned char dhm_g[] =
5236 MBEDTLS_DHM_RFC3526_MODP_2048_G_BIN;
Hanno Becker00d0a682017-10-04 13:14:29 +01005237
Gilles Peskine449bd832023-01-11 14:50:10 +01005238 if ((ret = mbedtls_ssl_conf_dh_param_bin(conf,
5239 dhm_p, sizeof(dhm_p),
5240 dhm_g, sizeof(dhm_g))) != 0) {
5241 return ret;
Hanno Beckere2defad2021-07-24 05:59:17 +01005242 }
5243 }
Manuel Pégourié-Gonnardbd990d62015-06-11 14:49:42 +02005244#endif
5245
Ronald Cron6f135e12021-12-08 16:57:54 +01005246#if defined(MBEDTLS_SSL_PROTO_TLS1_3)
Jerry Yu6ee56aa2022-12-06 17:47:22 +08005247
5248#if defined(MBEDTLS_SSL_EARLY_DATA)
Gilles Peskine449bd832023-01-11 14:50:10 +01005249 mbedtls_ssl_tls13_conf_early_data(conf, MBEDTLS_SSL_EARLY_DATA_DISABLED);
Jerry Yu6ee56aa2022-12-06 17:47:22 +08005250#if defined(MBEDTLS_SSL_SRV_C)
5251 mbedtls_ssl_tls13_conf_max_early_data_size(
Gilles Peskine449bd832023-01-11 14:50:10 +01005252 conf, MBEDTLS_SSL_MAX_EARLY_DATA_SIZE);
Jerry Yu6ee56aa2022-12-06 17:47:22 +08005253#endif
5254#endif /* MBEDTLS_SSL_EARLY_DATA */
5255
Jerry Yud0766ec2022-09-22 10:46:57 +08005256#if defined(MBEDTLS_SSL_SRV_C) && defined(MBEDTLS_SSL_SESSION_TICKETS)
Jerry Yu1ad7ace2022-08-09 13:28:39 +08005257 mbedtls_ssl_conf_new_session_tickets(
Gilles Peskine449bd832023-01-11 14:50:10 +01005258 conf, MBEDTLS_SSL_TLS1_3_DEFAULT_NEW_SESSION_TICKETS);
Jerry Yu1ad7ace2022-08-09 13:28:39 +08005259#endif
Hanno Becker71f1ed62021-07-24 06:01:47 +01005260 /*
5261 * Allow all TLS 1.3 key exchange modes by default.
5262 */
Xiaofei Bai746f9482021-11-12 08:53:56 +00005263 conf->tls13_kex_modes = MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_ALL;
Ronald Cron6f135e12021-12-08 16:57:54 +01005264#endif /* MBEDTLS_SSL_PROTO_TLS1_3 */
Hanno Becker71f1ed62021-07-24 06:01:47 +01005265
Gilles Peskine449bd832023-01-11 14:50:10 +01005266 if (transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM) {
Glenn Strauss2dfcea22022-03-14 17:26:42 -04005267#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
XiaokangQian4d3a6042022-04-21 13:46:17 +00005268 conf->min_tls_version = MBEDTLS_SSL_VERSION_TLS1_2;
XiaokangQian060d8672022-04-21 09:24:56 +00005269 conf->max_tls_version = MBEDTLS_SSL_VERSION_TLS1_2;
XiaokangQian4d3a6042022-04-21 13:46:17 +00005270#else
Gilles Peskine449bd832023-01-11 14:50:10 +01005271 return MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
XiaokangQian060d8672022-04-21 09:24:56 +00005272#endif
Gilles Peskine449bd832023-01-11 14:50:10 +01005273 } else {
XiaokangQian4d3a6042022-04-21 13:46:17 +00005274#if defined(MBEDTLS_SSL_PROTO_TLS1_2) && defined(MBEDTLS_SSL_PROTO_TLS1_3)
Gilles Peskine449bd832023-01-11 14:50:10 +01005275 if (endpoint == MBEDTLS_SSL_IS_CLIENT) {
XiaokangQian4d3a6042022-04-21 13:46:17 +00005276 conf->min_tls_version = MBEDTLS_SSL_VERSION_TLS1_2;
5277 conf->max_tls_version = MBEDTLS_SSL_VERSION_TLS1_3;
Gilles Peskine449bd832023-01-11 14:50:10 +01005278 } else {
5279 /* Hybrid TLS 1.2 / 1.3 is not supported on server side yet */
XiaokangQian4d3a6042022-04-21 13:46:17 +00005280 conf->min_tls_version = MBEDTLS_SSL_VERSION_TLS1_2;
5281 conf->max_tls_version = MBEDTLS_SSL_VERSION_TLS1_2;
5282 }
5283#elif defined(MBEDTLS_SSL_PROTO_TLS1_3)
5284 conf->min_tls_version = MBEDTLS_SSL_VERSION_TLS1_3;
5285 conf->max_tls_version = MBEDTLS_SSL_VERSION_TLS1_3;
5286#elif defined(MBEDTLS_SSL_PROTO_TLS1_2)
5287 conf->min_tls_version = MBEDTLS_SSL_VERSION_TLS1_2;
5288 conf->max_tls_version = MBEDTLS_SSL_VERSION_TLS1_2;
5289#else
Gilles Peskine449bd832023-01-11 14:50:10 +01005290 return MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
XiaokangQian4d3a6042022-04-21 13:46:17 +00005291#endif
5292 }
Glenn Strauss2dfcea22022-03-14 17:26:42 -04005293
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02005294 /*
5295 * Preset-specific defaults
5296 */
Gilles Peskine449bd832023-01-11 14:50:10 +01005297 switch (preset) {
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02005298 /*
5299 * NSA Suite B
5300 */
5301 case MBEDTLS_SSL_PRESET_SUITEB:
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02005302
Hanno Beckerd60b6c62021-04-29 12:04:11 +01005303 conf->ciphersuite_list = ssl_preset_suiteb_ciphersuites;
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02005304
5305#if defined(MBEDTLS_X509_CRT_PARSE_C)
5306 conf->cert_profile = &mbedtls_x509_crt_profile_suiteb;
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02005307#endif
5308
Ronald Crone68ab4f2022-10-05 12:46:29 +02005309#if defined(MBEDTLS_SSL_HANDSHAKE_WITH_CERT_ENABLED)
Jerry Yu909df7b2022-01-22 11:56:27 +08005310#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
Gilles Peskine449bd832023-01-11 14:50:10 +01005311 if (mbedtls_ssl_conf_is_tls12_only(conf)) {
Jerry Yu909df7b2022-01-22 11:56:27 +08005312 conf->sig_algs = ssl_tls12_preset_suiteb_sig_algs;
Gilles Peskine449bd832023-01-11 14:50:10 +01005313 } else
Jerry Yu909df7b2022-01-22 11:56:27 +08005314#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Gilles Peskine449bd832023-01-11 14:50:10 +01005315 conf->sig_algs = ssl_preset_suiteb_sig_algs;
Ronald Crone68ab4f2022-10-05 12:46:29 +02005316#endif /* MBEDTLS_SSL_HANDSHAKE_WITH_CERT_ENABLED */
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02005317
Brett Warrene0edc842021-08-17 09:53:13 +01005318#if defined(MBEDTLS_ECP_C) && !defined(MBEDTLS_DEPRECATED_REMOVED)
5319 conf->curve_list = NULL;
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02005320#endif
Brett Warrene0edc842021-08-17 09:53:13 +01005321 conf->group_list = ssl_preset_suiteb_groups;
Manuel Pégourié-Gonnardc98204e2015-08-11 04:21:01 +02005322 break;
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02005323
5324 /*
5325 * Default
5326 */
5327 default:
Ronald Cronf6606552022-03-15 11:23:25 +01005328
Hanno Beckerd60b6c62021-04-29 12:04:11 +01005329 conf->ciphersuite_list = mbedtls_ssl_list_ciphersuites();
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02005330
5331#if defined(MBEDTLS_X509_CRT_PARSE_C)
5332 conf->cert_profile = &mbedtls_x509_crt_profile_default;
5333#endif
5334
Ronald Crone68ab4f2022-10-05 12:46:29 +02005335#if defined(MBEDTLS_SSL_HANDSHAKE_WITH_CERT_ENABLED)
Jerry Yu909df7b2022-01-22 11:56:27 +08005336#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
Gilles Peskine449bd832023-01-11 14:50:10 +01005337 if (mbedtls_ssl_conf_is_tls12_only(conf)) {
Jerry Yu909df7b2022-01-22 11:56:27 +08005338 conf->sig_algs = ssl_tls12_preset_default_sig_algs;
Gilles Peskine449bd832023-01-11 14:50:10 +01005339 } else
Jerry Yu909df7b2022-01-22 11:56:27 +08005340#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Gilles Peskine449bd832023-01-11 14:50:10 +01005341 conf->sig_algs = ssl_preset_default_sig_algs;
Ronald Crone68ab4f2022-10-05 12:46:29 +02005342#endif /* MBEDTLS_SSL_HANDSHAKE_WITH_CERT_ENABLED */
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02005343
Brett Warrene0edc842021-08-17 09:53:13 +01005344#if defined(MBEDTLS_ECP_C) && !defined(MBEDTLS_DEPRECATED_REMOVED)
5345 conf->curve_list = NULL;
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02005346#endif
Brett Warrene0edc842021-08-17 09:53:13 +01005347 conf->group_list = ssl_preset_default_groups;
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02005348
5349#if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_SSL_CLI_C)
5350 conf->dhm_min_bitlen = 1024;
5351#endif
5352 }
5353
Gilles Peskine449bd832023-01-11 14:50:10 +01005354 return 0;
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02005355}
5356
5357/*
5358 * Free mbedtls_ssl_config
5359 */
Gilles Peskine449bd832023-01-11 14:50:10 +01005360void mbedtls_ssl_config_free(mbedtls_ssl_config *conf)
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02005361{
5362#if defined(MBEDTLS_DHM_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01005363 mbedtls_mpi_free(&conf->dhm_P);
5364 mbedtls_mpi_free(&conf->dhm_G);
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02005365#endif
5366
Ronald Cron73fe8df2022-10-05 14:31:43 +02005367#if defined(MBEDTLS_SSL_HANDSHAKE_WITH_PSK_ENABLED)
Neil Armstrong501c9322022-05-03 09:35:09 +02005368#if defined(MBEDTLS_USE_PSA_CRYPTO)
Gilles Peskine449bd832023-01-11 14:50:10 +01005369 if (!mbedtls_svc_key_id_is_null(conf->psk_opaque)) {
Neil Armstrong501c9322022-05-03 09:35:09 +02005370 conf->psk_opaque = MBEDTLS_SVC_KEY_ID_INIT;
5371 }
Neil Armstrong8ecd6682022-05-05 11:40:35 +02005372#endif /* MBEDTLS_USE_PSA_CRYPTO */
Gilles Peskine449bd832023-01-11 14:50:10 +01005373 if (conf->psk != NULL) {
5374 mbedtls_platform_zeroize(conf->psk, conf->psk_len);
5375 mbedtls_free(conf->psk);
Azim Khan27e8a122018-03-21 14:24:11 +00005376 conf->psk = NULL;
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02005377 conf->psk_len = 0;
junyeonLEE316b1622017-12-20 16:29:30 +09005378 }
5379
Gilles Peskine449bd832023-01-11 14:50:10 +01005380 if (conf->psk_identity != NULL) {
5381 mbedtls_platform_zeroize(conf->psk_identity, conf->psk_identity_len);
5382 mbedtls_free(conf->psk_identity);
Azim Khan27e8a122018-03-21 14:24:11 +00005383 conf->psk_identity = NULL;
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02005384 conf->psk_identity_len = 0;
5385 }
Ronald Cron73fe8df2022-10-05 14:31:43 +02005386#endif /* MBEDTLS_SSL_HANDSHAKE_WITH_PSK_ENABLED */
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02005387
5388#if defined(MBEDTLS_X509_CRT_PARSE_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01005389 ssl_key_cert_free(conf->key_cert);
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02005390#endif
5391
Gilles Peskine449bd832023-01-11 14:50:10 +01005392 mbedtls_platform_zeroize(conf, sizeof(mbedtls_ssl_config));
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02005393}
5394
Manuel Pégourié-Gonnard5674a972015-10-19 15:14:03 +02005395#if defined(MBEDTLS_PK_C) && \
Gilles Peskine449bd832023-01-11 14:50:10 +01005396 (defined(MBEDTLS_RSA_C) || defined(MBEDTLS_ECDSA_C))
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02005397/*
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005398 * Convert between MBEDTLS_PK_XXX and SSL_SIG_XXX
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02005399 */
Gilles Peskine449bd832023-01-11 14:50:10 +01005400unsigned char mbedtls_ssl_sig_from_pk(mbedtls_pk_context *pk)
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02005401{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005402#if defined(MBEDTLS_RSA_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01005403 if (mbedtls_pk_can_do(pk, MBEDTLS_PK_RSA)) {
5404 return MBEDTLS_SSL_SIG_RSA;
5405 }
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02005406#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005407#if defined(MBEDTLS_ECDSA_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01005408 if (mbedtls_pk_can_do(pk, MBEDTLS_PK_ECDSA)) {
5409 return MBEDTLS_SSL_SIG_ECDSA;
5410 }
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02005411#endif
Gilles Peskine449bd832023-01-11 14:50:10 +01005412 return MBEDTLS_SSL_SIG_ANON;
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02005413}
5414
Gilles Peskine449bd832023-01-11 14:50:10 +01005415unsigned char mbedtls_ssl_sig_from_pk_alg(mbedtls_pk_type_t type)
Hanno Becker7e5437a2017-04-28 17:15:26 +01005416{
Gilles Peskine449bd832023-01-11 14:50:10 +01005417 switch (type) {
Hanno Becker7e5437a2017-04-28 17:15:26 +01005418 case MBEDTLS_PK_RSA:
Gilles Peskine449bd832023-01-11 14:50:10 +01005419 return MBEDTLS_SSL_SIG_RSA;
Hanno Becker7e5437a2017-04-28 17:15:26 +01005420 case MBEDTLS_PK_ECDSA:
5421 case MBEDTLS_PK_ECKEY:
Gilles Peskine449bd832023-01-11 14:50:10 +01005422 return MBEDTLS_SSL_SIG_ECDSA;
Hanno Becker7e5437a2017-04-28 17:15:26 +01005423 default:
Gilles Peskine449bd832023-01-11 14:50:10 +01005424 return MBEDTLS_SSL_SIG_ANON;
Hanno Becker7e5437a2017-04-28 17:15:26 +01005425 }
5426}
5427
Gilles Peskine449bd832023-01-11 14:50:10 +01005428mbedtls_pk_type_t mbedtls_ssl_pk_alg_from_sig(unsigned char sig)
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02005429{
Gilles Peskine449bd832023-01-11 14:50:10 +01005430 switch (sig) {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005431#if defined(MBEDTLS_RSA_C)
5432 case MBEDTLS_SSL_SIG_RSA:
Gilles Peskine449bd832023-01-11 14:50:10 +01005433 return MBEDTLS_PK_RSA;
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02005434#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005435#if defined(MBEDTLS_ECDSA_C)
5436 case MBEDTLS_SSL_SIG_ECDSA:
Gilles Peskine449bd832023-01-11 14:50:10 +01005437 return MBEDTLS_PK_ECDSA;
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02005438#endif
5439 default:
Gilles Peskine449bd832023-01-11 14:50:10 +01005440 return MBEDTLS_PK_NONE;
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02005441 }
5442}
Manuel Pégourié-Gonnard5674a972015-10-19 15:14:03 +02005443#endif /* MBEDTLS_PK_C && ( MBEDTLS_RSA_C || MBEDTLS_ECDSA_C ) */
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02005444
Manuel Pégourié-Gonnard1a483832013-09-20 12:29:15 +02005445/*
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +02005446 * Convert from MBEDTLS_SSL_HASH_XXX to MBEDTLS_MD_XXX
Manuel Pégourié-Gonnard1a483832013-09-20 12:29:15 +02005447 */
Gilles Peskine449bd832023-01-11 14:50:10 +01005448mbedtls_md_type_t mbedtls_ssl_md_alg_from_hash(unsigned char hash)
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02005449{
Gilles Peskine449bd832023-01-11 14:50:10 +01005450 switch (hash) {
Andrzej Kurek25f27152022-08-17 16:09:31 -04005451#if defined(MBEDTLS_HAS_ALG_MD5_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005452 case MBEDTLS_SSL_HASH_MD5:
Gilles Peskine449bd832023-01-11 14:50:10 +01005453 return MBEDTLS_MD_MD5;
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02005454#endif
Andrzej Kurek25f27152022-08-17 16:09:31 -04005455#if defined(MBEDTLS_HAS_ALG_SHA_1_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005456 case MBEDTLS_SSL_HASH_SHA1:
Gilles Peskine449bd832023-01-11 14:50:10 +01005457 return MBEDTLS_MD_SHA1;
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02005458#endif
Andrzej Kurek25f27152022-08-17 16:09:31 -04005459#if defined(MBEDTLS_HAS_ALG_SHA_224_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005460 case MBEDTLS_SSL_HASH_SHA224:
Gilles Peskine449bd832023-01-11 14:50:10 +01005461 return MBEDTLS_MD_SHA224;
Mateusz Starzyke3c48b42021-04-19 16:46:28 +02005462#endif
Andrzej Kurek25f27152022-08-17 16:09:31 -04005463#if defined(MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005464 case MBEDTLS_SSL_HASH_SHA256:
Gilles Peskine449bd832023-01-11 14:50:10 +01005465 return MBEDTLS_MD_SHA256;
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02005466#endif
Andrzej Kurek25f27152022-08-17 16:09:31 -04005467#if defined(MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005468 case MBEDTLS_SSL_HASH_SHA384:
Gilles Peskine449bd832023-01-11 14:50:10 +01005469 return MBEDTLS_MD_SHA384;
Mateusz Starzyk3352a532021-04-06 14:28:22 +02005470#endif
Andrzej Kurek25f27152022-08-17 16:09:31 -04005471#if defined(MBEDTLS_HAS_ALG_SHA_512_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005472 case MBEDTLS_SSL_HASH_SHA512:
Gilles Peskine449bd832023-01-11 14:50:10 +01005473 return MBEDTLS_MD_SHA512;
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02005474#endif
5475 default:
Gilles Peskine449bd832023-01-11 14:50:10 +01005476 return MBEDTLS_MD_NONE;
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02005477 }
5478}
5479
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +02005480/*
5481 * Convert from MBEDTLS_MD_XXX to MBEDTLS_SSL_HASH_XXX
5482 */
Gilles Peskine449bd832023-01-11 14:50:10 +01005483unsigned char mbedtls_ssl_hash_from_md_alg(int md)
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +02005484{
Gilles Peskine449bd832023-01-11 14:50:10 +01005485 switch (md) {
Andrzej Kurek25f27152022-08-17 16:09:31 -04005486#if defined(MBEDTLS_HAS_ALG_MD5_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +02005487 case MBEDTLS_MD_MD5:
Gilles Peskine449bd832023-01-11 14:50:10 +01005488 return MBEDTLS_SSL_HASH_MD5;
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +02005489#endif
Andrzej Kurek25f27152022-08-17 16:09:31 -04005490#if defined(MBEDTLS_HAS_ALG_SHA_1_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +02005491 case MBEDTLS_MD_SHA1:
Gilles Peskine449bd832023-01-11 14:50:10 +01005492 return MBEDTLS_SSL_HASH_SHA1;
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +02005493#endif
Andrzej Kurek25f27152022-08-17 16:09:31 -04005494#if defined(MBEDTLS_HAS_ALG_SHA_224_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +02005495 case MBEDTLS_MD_SHA224:
Gilles Peskine449bd832023-01-11 14:50:10 +01005496 return MBEDTLS_SSL_HASH_SHA224;
Mateusz Starzyke3c48b42021-04-19 16:46:28 +02005497#endif
Andrzej Kurek25f27152022-08-17 16:09:31 -04005498#if defined(MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +02005499 case MBEDTLS_MD_SHA256:
Gilles Peskine449bd832023-01-11 14:50:10 +01005500 return MBEDTLS_SSL_HASH_SHA256;
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +02005501#endif
Andrzej Kurek25f27152022-08-17 16:09:31 -04005502#if defined(MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +02005503 case MBEDTLS_MD_SHA384:
Gilles Peskine449bd832023-01-11 14:50:10 +01005504 return MBEDTLS_SSL_HASH_SHA384;
Mateusz Starzyk3352a532021-04-06 14:28:22 +02005505#endif
Andrzej Kurek25f27152022-08-17 16:09:31 -04005506#if defined(MBEDTLS_HAS_ALG_SHA_512_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +02005507 case MBEDTLS_MD_SHA512:
Gilles Peskine449bd832023-01-11 14:50:10 +01005508 return MBEDTLS_SSL_HASH_SHA512;
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +02005509#endif
5510 default:
Gilles Peskine449bd832023-01-11 14:50:10 +01005511 return MBEDTLS_SSL_HASH_NONE;
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +02005512 }
5513}
5514
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01005515/*
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +02005516 * Check if a curve proposed by the peer is in our list.
Manuel Pégourié-Gonnard9d412d82015-06-17 12:10:46 +02005517 * Return 0 if we're willing to use it, -1 otherwise.
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01005518 */
Gilles Peskine449bd832023-01-11 14:50:10 +01005519int mbedtls_ssl_check_curve_tls_id(const mbedtls_ssl_context *ssl, uint16_t tls_id)
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01005520{
Gilles Peskine449bd832023-01-11 14:50:10 +01005521 const uint16_t *group_list = mbedtls_ssl_get_groups(ssl);
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01005522
Gilles Peskine449bd832023-01-11 14:50:10 +01005523 if (group_list == NULL) {
5524 return -1;
Brett Warrene0edc842021-08-17 09:53:13 +01005525 }
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01005526
Gilles Peskine449bd832023-01-11 14:50:10 +01005527 for (; *group_list != 0; group_list++) {
5528 if (*group_list == tls_id) {
5529 return 0;
5530 }
5531 }
5532
5533 return -1;
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01005534}
Manuel Pégourié-Gonnard0d63b842022-01-18 13:10:56 +01005535
5536#if defined(MBEDTLS_ECP_C)
5537/*
5538 * Same as mbedtls_ssl_check_curve_tls_id() but with a mbedtls_ecp_group_id.
5539 */
Gilles Peskine449bd832023-01-11 14:50:10 +01005540int mbedtls_ssl_check_curve(const mbedtls_ssl_context *ssl, mbedtls_ecp_group_id grp_id)
Manuel Pégourié-Gonnard0d63b842022-01-18 13:10:56 +01005541{
Gilles Peskine449bd832023-01-11 14:50:10 +01005542 uint16_t tls_id = mbedtls_ssl_get_tls_id_from_ecp_group_id(grp_id);
Leonid Rozenboim19e59732022-08-08 16:52:38 -07005543
Gilles Peskine449bd832023-01-11 14:50:10 +01005544 if (tls_id == 0) {
Leonid Rozenboim19e59732022-08-08 16:52:38 -07005545 return -1;
Gilles Peskine449bd832023-01-11 14:50:10 +01005546 }
Leonid Rozenboim19e59732022-08-08 16:52:38 -07005547
Gilles Peskine449bd832023-01-11 14:50:10 +01005548 return mbedtls_ssl_check_curve_tls_id(ssl, tls_id);
Manuel Pégourié-Gonnard0d63b842022-01-18 13:10:56 +01005549}
Manuel Pégourié-Gonnardb541da62015-06-17 11:43:30 +02005550#endif /* MBEDTLS_ECP_C */
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02005551
Gilles Peskine449bd832023-01-11 14:50:10 +01005552#if defined(MBEDTLS_DEBUG_C)
Valerio Setti67419f02023-01-04 16:12:42 +01005553#define EC_NAME(_name_) _name_
5554#else
5555#define EC_NAME(_name_) NULL
5556#endif
5557
Valerio Setti18c9fed2022-12-30 17:44:24 +01005558static const struct {
5559 uint16_t tls_id;
5560 mbedtls_ecp_group_id ecp_group_id;
5561 psa_ecc_family_t psa_family;
5562 uint16_t bits;
Gilles Peskine449bd832023-01-11 14:50:10 +01005563 const char *name;
Valerio Setti18c9fed2022-12-30 17:44:24 +01005564} tls_id_match_table[] =
5565{
5566#if defined(MBEDTLS_ECP_DP_SECP521R1_ENABLED) || defined(PSA_WANT_ECC_SECP_R1_521)
Gilles Peskine449bd832023-01-11 14:50:10 +01005567 { 25, MBEDTLS_ECP_DP_SECP521R1, PSA_ECC_FAMILY_SECP_R1, 521, EC_NAME("secp521r1") },
Valerio Setti18c9fed2022-12-30 17:44:24 +01005568#endif
5569#if defined(MBEDTLS_ECP_DP_BP512R1_ENABLED) || defined(PSA_WANT_ECC_BRAINPOOL_P_R1_512)
Gilles Peskine449bd832023-01-11 14:50:10 +01005570 { 28, MBEDTLS_ECP_DP_BP512R1, PSA_ECC_FAMILY_BRAINPOOL_P_R1, 512, EC_NAME("brainpoolP512r1") },
Valerio Setti18c9fed2022-12-30 17:44:24 +01005571#endif
Valerio Setti67419f02023-01-04 16:12:42 +01005572#if defined(MBEDTLS_ECP_DP_SECP384R1_ENABLED) || defined(PSA_WANT_ECC_SECP_R1_384)
Gilles Peskine449bd832023-01-11 14:50:10 +01005573 { 24, MBEDTLS_ECP_DP_SECP384R1, PSA_ECC_FAMILY_SECP_R1, 384, EC_NAME("secp384r1") },
Valerio Setti18c9fed2022-12-30 17:44:24 +01005574#endif
Valerio Setti67419f02023-01-04 16:12:42 +01005575#if defined(MBEDTLS_ECP_DP_BP384R1_ENABLED) || defined(PSA_WANT_ECC_BRAINPOOL_P_R1_384)
Gilles Peskine449bd832023-01-11 14:50:10 +01005576 { 27, MBEDTLS_ECP_DP_BP384R1, PSA_ECC_FAMILY_BRAINPOOL_P_R1, 384, EC_NAME("brainpoolP384r1") },
Valerio Setti18c9fed2022-12-30 17:44:24 +01005577#endif
Valerio Setti67419f02023-01-04 16:12:42 +01005578#if defined(MBEDTLS_ECP_DP_SECP256R1_ENABLED) || defined(PSA_WANT_ECC_SECP_R1_256)
Gilles Peskine449bd832023-01-11 14:50:10 +01005579 { 23, MBEDTLS_ECP_DP_SECP256R1, PSA_ECC_FAMILY_SECP_R1, 256, EC_NAME("secp256r1") },
Valerio Setti18c9fed2022-12-30 17:44:24 +01005580#endif
5581#if defined(MBEDTLS_ECP_DP_SECP256K1_ENABLED) || defined(PSA_WANT_ECC_SECP_K1_256)
Gilles Peskine449bd832023-01-11 14:50:10 +01005582 { 22, MBEDTLS_ECP_DP_SECP256K1, PSA_ECC_FAMILY_SECP_K1, 256, EC_NAME("secp256k1") },
Valerio Setti18c9fed2022-12-30 17:44:24 +01005583#endif
Valerio Setti67419f02023-01-04 16:12:42 +01005584#if defined(MBEDTLS_ECP_DP_BP256R1_ENABLED) || defined(PSA_WANT_ECC_BRAINPOOL_P_R1_256)
Gilles Peskine449bd832023-01-11 14:50:10 +01005585 { 26, MBEDTLS_ECP_DP_BP256R1, PSA_ECC_FAMILY_BRAINPOOL_P_R1, 256, EC_NAME("brainpoolP256r1") },
Valerio Setti18c9fed2022-12-30 17:44:24 +01005586#endif
5587#if defined(MBEDTLS_ECP_DP_SECP224R1_ENABLED) || defined(PSA_WANT_ECC_SECP_R1_224)
Gilles Peskine449bd832023-01-11 14:50:10 +01005588 { 21, MBEDTLS_ECP_DP_SECP224R1, PSA_ECC_FAMILY_SECP_R1, 224, EC_NAME("secp224r1") },
Valerio Setti18c9fed2022-12-30 17:44:24 +01005589#endif
5590#if defined(MBEDTLS_ECP_DP_SECP224K1_ENABLED) || defined(PSA_WANT_ECC_SECP_K1_224)
Gilles Peskine449bd832023-01-11 14:50:10 +01005591 { 20, MBEDTLS_ECP_DP_SECP224K1, PSA_ECC_FAMILY_SECP_K1, 224, EC_NAME("secp224k1") },
Valerio Setti18c9fed2022-12-30 17:44:24 +01005592#endif
5593#if defined(MBEDTLS_ECP_DP_SECP192R1_ENABLED) || defined(PSA_WANT_ECC_SECP_R1_192)
Gilles Peskine449bd832023-01-11 14:50:10 +01005594 { 19, MBEDTLS_ECP_DP_SECP192R1, PSA_ECC_FAMILY_SECP_R1, 192, EC_NAME("secp192r1") },
Valerio Setti18c9fed2022-12-30 17:44:24 +01005595#endif
5596#if defined(MBEDTLS_ECP_DP_SECP192K1_ENABLED) || defined(PSA_WANT_ECC_SECP_K1_192)
Gilles Peskine449bd832023-01-11 14:50:10 +01005597 { 18, MBEDTLS_ECP_DP_SECP192K1, PSA_ECC_FAMILY_SECP_K1, 192, EC_NAME("secp192k1") },
Valerio Setti18c9fed2022-12-30 17:44:24 +01005598#endif
5599#if defined(MBEDTLS_ECP_DP_CURVE25519_ENABLED) || defined(PSA_WANT_ECC_MONTGOMERY_255)
Gilles Peskine449bd832023-01-11 14:50:10 +01005600 { 29, MBEDTLS_ECP_DP_CURVE25519, PSA_ECC_FAMILY_MONTGOMERY, 255, EC_NAME("x25519") },
Valerio Setti18c9fed2022-12-30 17:44:24 +01005601#endif
5602#if defined(MBEDTLS_ECP_DP_CURVE448_ENABLED) || defined(PSA_WANT_ECC_MONTGOMERY_448)
Gilles Peskine449bd832023-01-11 14:50:10 +01005603 { 30, MBEDTLS_ECP_DP_CURVE448, PSA_ECC_FAMILY_MONTGOMERY, 448, EC_NAME("x448") },
Valerio Setti18c9fed2022-12-30 17:44:24 +01005604#endif
5605 { 0, MBEDTLS_ECP_DP_NONE, 0, 0, NULL },
5606};
5607
Gilles Peskine449bd832023-01-11 14:50:10 +01005608int mbedtls_ssl_get_psa_curve_info_from_tls_id(uint16_t tls_id,
5609 psa_ecc_family_t *family,
5610 size_t *bits)
Valerio Setti18c9fed2022-12-30 17:44:24 +01005611{
Gilles Peskine449bd832023-01-11 14:50:10 +01005612 for (int i = 0; tls_id_match_table[i].tls_id != 0; i++) {
5613 if (tls_id_match_table[i].tls_id == tls_id) {
5614 if (family != NULL) {
Valerio Setti18c9fed2022-12-30 17:44:24 +01005615 *family = tls_id_match_table[i].psa_family;
Gilles Peskine449bd832023-01-11 14:50:10 +01005616 }
5617 if (bits != NULL) {
Valerio Setti18c9fed2022-12-30 17:44:24 +01005618 *bits = tls_id_match_table[i].bits;
Gilles Peskine449bd832023-01-11 14:50:10 +01005619 }
Valerio Setti18c9fed2022-12-30 17:44:24 +01005620 return PSA_SUCCESS;
5621 }
5622 }
5623
5624 return PSA_ERROR_NOT_SUPPORTED;
5625}
5626
Gilles Peskine449bd832023-01-11 14:50:10 +01005627mbedtls_ecp_group_id mbedtls_ssl_get_ecp_group_id_from_tls_id(uint16_t tls_id)
Valerio Setti18c9fed2022-12-30 17:44:24 +01005628{
Gilles Peskine449bd832023-01-11 14:50:10 +01005629 for (int i = 0; tls_id_match_table[i].tls_id != 0; i++) {
5630 if (tls_id_match_table[i].tls_id == tls_id) {
Valerio Setti18c9fed2022-12-30 17:44:24 +01005631 return tls_id_match_table[i].ecp_group_id;
Gilles Peskine449bd832023-01-11 14:50:10 +01005632 }
Valerio Setti18c9fed2022-12-30 17:44:24 +01005633 }
5634
5635 return MBEDTLS_ECP_DP_NONE;
5636}
5637
Gilles Peskine449bd832023-01-11 14:50:10 +01005638uint16_t mbedtls_ssl_get_tls_id_from_ecp_group_id(mbedtls_ecp_group_id grp_id)
Valerio Setti18c9fed2022-12-30 17:44:24 +01005639{
Gilles Peskine449bd832023-01-11 14:50:10 +01005640 for (int i = 0; tls_id_match_table[i].ecp_group_id != MBEDTLS_ECP_DP_NONE;
5641 i++) {
5642 if (tls_id_match_table[i].ecp_group_id == grp_id) {
Valerio Setti18c9fed2022-12-30 17:44:24 +01005643 return tls_id_match_table[i].tls_id;
Gilles Peskine449bd832023-01-11 14:50:10 +01005644 }
Valerio Setti18c9fed2022-12-30 17:44:24 +01005645 }
5646
5647 return 0;
5648}
5649
Valerio Setti67419f02023-01-04 16:12:42 +01005650#if defined(MBEDTLS_DEBUG_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01005651const char *mbedtls_ssl_get_curve_name_from_tls_id(uint16_t tls_id)
Valerio Setti18c9fed2022-12-30 17:44:24 +01005652{
Gilles Peskine449bd832023-01-11 14:50:10 +01005653 for (int i = 0; tls_id_match_table[i].tls_id != 0; i++) {
5654 if (tls_id_match_table[i].tls_id == tls_id) {
Valerio Setti18c9fed2022-12-30 17:44:24 +01005655 return tls_id_match_table[i].name;
Gilles Peskine449bd832023-01-11 14:50:10 +01005656 }
Valerio Setti18c9fed2022-12-30 17:44:24 +01005657 }
5658
5659 return NULL;
5660}
Valerio Setti67419f02023-01-04 16:12:42 +01005661#endif
Valerio Setti18c9fed2022-12-30 17:44:24 +01005662
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005663#if defined(MBEDTLS_X509_CRT_PARSE_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01005664int mbedtls_ssl_check_cert_usage(const mbedtls_x509_crt *cert,
5665 const mbedtls_ssl_ciphersuite_t *ciphersuite,
5666 int cert_endpoint,
5667 uint32_t *flags)
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02005668{
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +01005669 int ret = 0;
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02005670 int usage = 0;
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +02005671 const char *ext_oid;
5672 size_t ext_len;
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +02005673
Gilles Peskine449bd832023-01-11 14:50:10 +01005674 if (cert_endpoint == MBEDTLS_SSL_IS_SERVER) {
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02005675 /* Server part of the key exchange */
Gilles Peskine449bd832023-01-11 14:50:10 +01005676 switch (ciphersuite->key_exchange) {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005677 case MBEDTLS_KEY_EXCHANGE_RSA:
5678 case MBEDTLS_KEY_EXCHANGE_RSA_PSK:
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +01005679 usage = MBEDTLS_X509_KU_KEY_ENCIPHERMENT;
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02005680 break;
5681
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005682 case MBEDTLS_KEY_EXCHANGE_DHE_RSA:
5683 case MBEDTLS_KEY_EXCHANGE_ECDHE_RSA:
5684 case MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA:
5685 usage = MBEDTLS_X509_KU_DIGITAL_SIGNATURE;
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02005686 break;
5687
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005688 case MBEDTLS_KEY_EXCHANGE_ECDH_RSA:
5689 case MBEDTLS_KEY_EXCHANGE_ECDH_ECDSA:
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +01005690 usage = MBEDTLS_X509_KU_KEY_AGREEMENT;
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02005691 break;
5692
5693 /* Don't use default: we want warnings when adding new values */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005694 case MBEDTLS_KEY_EXCHANGE_NONE:
5695 case MBEDTLS_KEY_EXCHANGE_PSK:
5696 case MBEDTLS_KEY_EXCHANGE_DHE_PSK:
5697 case MBEDTLS_KEY_EXCHANGE_ECDHE_PSK:
Manuel Pégourié-Gonnard557535d2015-09-15 17:53:32 +02005698 case MBEDTLS_KEY_EXCHANGE_ECJPAKE:
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02005699 usage = 0;
5700 }
Gilles Peskine449bd832023-01-11 14:50:10 +01005701 } else {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005702 /* Client auth: we only implement rsa_sign and mbedtls_ecdsa_sign for now */
5703 usage = MBEDTLS_X509_KU_DIGITAL_SIGNATURE;
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02005704 }
5705
Gilles Peskine449bd832023-01-11 14:50:10 +01005706 if (mbedtls_x509_crt_check_key_usage(cert, usage) != 0) {
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +01005707 *flags |= MBEDTLS_X509_BADCERT_KEY_USAGE;
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +01005708 ret = -1;
5709 }
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02005710
Gilles Peskine449bd832023-01-11 14:50:10 +01005711 if (cert_endpoint == MBEDTLS_SSL_IS_SERVER) {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005712 ext_oid = MBEDTLS_OID_SERVER_AUTH;
Gilles Peskine449bd832023-01-11 14:50:10 +01005713 ext_len = MBEDTLS_OID_SIZE(MBEDTLS_OID_SERVER_AUTH);
5714 } else {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005715 ext_oid = MBEDTLS_OID_CLIENT_AUTH;
Gilles Peskine449bd832023-01-11 14:50:10 +01005716 ext_len = MBEDTLS_OID_SIZE(MBEDTLS_OID_CLIENT_AUTH);
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +02005717 }
5718
Gilles Peskine449bd832023-01-11 14:50:10 +01005719 if (mbedtls_x509_crt_check_extended_key_usage(cert, ext_oid, ext_len) != 0) {
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +01005720 *flags |= MBEDTLS_X509_BADCERT_EXT_KEY_USAGE;
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +01005721 ret = -1;
5722 }
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +02005723
Gilles Peskine449bd832023-01-11 14:50:10 +01005724 return ret;
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02005725}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005726#endif /* MBEDTLS_X509_CRT_PARSE_C */
Manuel Pégourié-Gonnard3a306b92014-04-29 15:11:17 +02005727
Jerry Yu148165c2021-09-24 23:20:59 +08005728#if defined(MBEDTLS_USE_PSA_CRYPTO)
Gilles Peskine449bd832023-01-11 14:50:10 +01005729int mbedtls_ssl_get_handshake_transcript(mbedtls_ssl_context *ssl,
5730 const mbedtls_md_type_t md,
5731 unsigned char *dst,
5732 size_t dst_len,
5733 size_t *olen)
Jerry Yu148165c2021-09-24 23:20:59 +08005734{
Ronald Cronf6893e12022-01-07 22:09:01 +01005735 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
5736 psa_hash_operation_t *hash_operation_to_clone;
5737 psa_hash_operation_t hash_operation = psa_hash_operation_init();
5738
Jerry Yu148165c2021-09-24 23:20:59 +08005739 *olen = 0;
Ronald Cronf6893e12022-01-07 22:09:01 +01005740
Gilles Peskine449bd832023-01-11 14:50:10 +01005741 switch (md) {
Andrzej Kurek25f27152022-08-17 16:09:31 -04005742#if defined(MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Gilles Peskine449bd832023-01-11 14:50:10 +01005743 case MBEDTLS_MD_SHA384:
5744 hash_operation_to_clone = &ssl->handshake->fin_sha384_psa;
5745 break;
Ronald Cronf6893e12022-01-07 22:09:01 +01005746#endif
5747
Andrzej Kurek25f27152022-08-17 16:09:31 -04005748#if defined(MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Gilles Peskine449bd832023-01-11 14:50:10 +01005749 case MBEDTLS_MD_SHA256:
5750 hash_operation_to_clone = &ssl->handshake->fin_sha256_psa;
5751 break;
Ronald Cronf6893e12022-01-07 22:09:01 +01005752#endif
5753
Gilles Peskine449bd832023-01-11 14:50:10 +01005754 default:
5755 goto exit;
5756 }
5757
5758 status = psa_hash_clone(hash_operation_to_clone, &hash_operation);
5759 if (status != PSA_SUCCESS) {
Ronald Cronf6893e12022-01-07 22:09:01 +01005760 goto exit;
5761 }
5762
Gilles Peskine449bd832023-01-11 14:50:10 +01005763 status = psa_hash_finish(&hash_operation, dst, dst_len, olen);
5764 if (status != PSA_SUCCESS) {
Ronald Cronf6893e12022-01-07 22:09:01 +01005765 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01005766 }
Ronald Cronf6893e12022-01-07 22:09:01 +01005767
5768exit:
Andrzej Kurekeabeb302022-10-17 07:52:51 -04005769#if !defined(MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA) && \
5770 !defined(MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
5771 (void) ssl;
5772#endif
Gilles Peskine449bd832023-01-11 14:50:10 +01005773 return psa_ssl_status_to_mbedtls(status);
Jerry Yu148165c2021-09-24 23:20:59 +08005774}
5775#else /* MBEDTLS_USE_PSA_CRYPTO */
5776
Andrzej Kurek25f27152022-08-17 16:09:31 -04005777#if defined(MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02005778MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01005779static int ssl_get_handshake_transcript_sha384(mbedtls_ssl_context *ssl,
5780 unsigned char *dst,
5781 size_t dst_len,
5782 size_t *olen)
Jerry Yu24c0ec32021-09-09 14:21:07 +08005783{
Jerry Yu24c0ec32021-09-09 14:21:07 +08005784 int ret;
Manuel Pégourié-Gonnardf057ecf2023-02-24 13:19:17 +01005785 mbedtls_md_context_t sha512;
Jerry Yu24c0ec32021-09-09 14:21:07 +08005786
Gilles Peskine449bd832023-01-11 14:50:10 +01005787 if (dst_len < 48) {
5788 return MBEDTLS_ERR_SSL_INTERNAL_ERROR;
5789 }
Jerry Yu24c0ec32021-09-09 14:21:07 +08005790
Manuel Pégourié-Gonnardf057ecf2023-02-24 13:19:17 +01005791 mbedtls_md_init(&sha512);
5792 ret = mbedtls_md_setup(&sha512, mbedtls_md_info_from_type(MBEDTLS_MD_SHA384), 0);
5793 if (ret != 0) {
5794 goto exit;
5795 }
5796 ret = mbedtls_md_clone(&sha512, &ssl->handshake->fin_sha384);
5797 if (ret != 0) {
5798 goto exit;
5799 }
Jerry Yu24c0ec32021-09-09 14:21:07 +08005800
Manuel Pégourié-Gonnardf057ecf2023-02-24 13:19:17 +01005801 if ((ret = mbedtls_md_finish(&sha512, dst)) != 0) {
5802 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_md_finish", ret);
Jerry Yu24c0ec32021-09-09 14:21:07 +08005803 goto exit;
5804 }
5805
5806 *olen = 48;
5807
5808exit:
5809
Manuel Pégourié-Gonnardf057ecf2023-02-24 13:19:17 +01005810 mbedtls_md_free(&sha512);
Gilles Peskine449bd832023-01-11 14:50:10 +01005811 return ret;
Jerry Yu24c0ec32021-09-09 14:21:07 +08005812}
Andrzej Kurek25f27152022-08-17 16:09:31 -04005813#endif /* MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA */
Jerry Yu24c0ec32021-09-09 14:21:07 +08005814
Andrzej Kurek25f27152022-08-17 16:09:31 -04005815#if defined(MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02005816MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01005817static int ssl_get_handshake_transcript_sha256(mbedtls_ssl_context *ssl,
5818 unsigned char *dst,
5819 size_t dst_len,
5820 size_t *olen)
Jerry Yu24c0ec32021-09-09 14:21:07 +08005821{
Jerry Yu24c0ec32021-09-09 14:21:07 +08005822 int ret;
Manuel Pégourié-Gonnardf057ecf2023-02-24 13:19:17 +01005823 mbedtls_md_context_t sha256;
Jerry Yu24c0ec32021-09-09 14:21:07 +08005824
Gilles Peskine449bd832023-01-11 14:50:10 +01005825 if (dst_len < 32) {
5826 return MBEDTLS_ERR_SSL_INTERNAL_ERROR;
5827 }
Jerry Yu24c0ec32021-09-09 14:21:07 +08005828
Manuel Pégourié-Gonnardf057ecf2023-02-24 13:19:17 +01005829 mbedtls_md_init(&sha256);
5830 ret = mbedtls_md_setup(&sha256, mbedtls_md_info_from_type(MBEDTLS_MD_SHA256), 0);
5831 if (ret != 0) {
5832 goto exit;
5833 }
5834 ret = mbedtls_md_clone(&sha256, &ssl->handshake->fin_sha256);
5835 if (ret != 0) {
5836 goto exit;
5837 }
Jerry Yuc5aef882021-12-23 20:15:02 +08005838
Manuel Pégourié-Gonnardf057ecf2023-02-24 13:19:17 +01005839 if ((ret = mbedtls_md_finish(&sha256, dst)) != 0) {
5840 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_md_finish", ret);
Jerry Yu24c0ec32021-09-09 14:21:07 +08005841 goto exit;
5842 }
5843
5844 *olen = 32;
5845
5846exit:
5847
Manuel Pégourié-Gonnardf057ecf2023-02-24 13:19:17 +01005848 mbedtls_md_free(&sha256);
Gilles Peskine449bd832023-01-11 14:50:10 +01005849 return ret;
Jerry Yu24c0ec32021-09-09 14:21:07 +08005850}
Andrzej Kurek25f27152022-08-17 16:09:31 -04005851#endif /* MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA */
Jerry Yu24c0ec32021-09-09 14:21:07 +08005852
Gilles Peskine449bd832023-01-11 14:50:10 +01005853int mbedtls_ssl_get_handshake_transcript(mbedtls_ssl_context *ssl,
5854 const mbedtls_md_type_t md,
5855 unsigned char *dst,
5856 size_t dst_len,
5857 size_t *olen)
Jerry Yu24c0ec32021-09-09 14:21:07 +08005858{
Gilles Peskine449bd832023-01-11 14:50:10 +01005859 switch (md) {
Jerry Yuc1ddeef2021-10-08 15:14:45 +08005860
Andrzej Kurek25f27152022-08-17 16:09:31 -04005861#if defined(MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Gilles Peskine449bd832023-01-11 14:50:10 +01005862 case MBEDTLS_MD_SHA384:
5863 return ssl_get_handshake_transcript_sha384(ssl, dst, dst_len, olen);
Andrzej Kurekcccb0442022-08-19 03:42:11 -04005864#endif /* MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA*/
Jerry Yuc1ddeef2021-10-08 15:14:45 +08005865
Andrzej Kurek25f27152022-08-17 16:09:31 -04005866#if defined(MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Gilles Peskine449bd832023-01-11 14:50:10 +01005867 case MBEDTLS_MD_SHA256:
5868 return ssl_get_handshake_transcript_sha256(ssl, dst, dst_len, olen);
Andrzej Kurekcccb0442022-08-19 03:42:11 -04005869#endif /* MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA*/
Jerry Yuc1ddeef2021-10-08 15:14:45 +08005870
Gilles Peskine449bd832023-01-11 14:50:10 +01005871 default:
Andrzej Kurek409248a2022-10-24 10:33:21 -04005872#if !defined(MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA) && \
Gilles Peskine449bd832023-01-11 14:50:10 +01005873 !defined(MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
5874 (void) ssl;
5875 (void) dst;
5876 (void) dst_len;
5877 (void) olen;
Andrzej Kurek409248a2022-10-24 10:33:21 -04005878#endif
Gilles Peskine449bd832023-01-11 14:50:10 +01005879 break;
Jerry Yuc1ddeef2021-10-08 15:14:45 +08005880 }
Gilles Peskine449bd832023-01-11 14:50:10 +01005881 return MBEDTLS_ERR_SSL_INTERNAL_ERROR;
Jerry Yu24c0ec32021-09-09 14:21:07 +08005882}
XiaokangQian647719a2021-12-07 09:16:29 +00005883
Jerry Yu148165c2021-09-24 23:20:59 +08005884#endif /* !MBEDTLS_USE_PSA_CRYPTO */
Jerry Yu24c0ec32021-09-09 14:21:07 +08005885
Ronald Crone68ab4f2022-10-05 12:46:29 +02005886#if defined(MBEDTLS_SSL_HANDSHAKE_WITH_CERT_ENABLED)
Gabor Mezei078e8032022-04-27 21:17:56 +02005887/* mbedtls_ssl_parse_sig_alg_ext()
5888 *
5889 * The `extension_data` field of signature algorithm contains a `SignatureSchemeList`
5890 * value (TLS 1.3 RFC8446):
5891 * enum {
5892 * ....
5893 * ecdsa_secp256r1_sha256( 0x0403 ),
5894 * ecdsa_secp384r1_sha384( 0x0503 ),
5895 * ecdsa_secp521r1_sha512( 0x0603 ),
5896 * ....
5897 * } SignatureScheme;
5898 *
5899 * struct {
5900 * SignatureScheme supported_signature_algorithms<2..2^16-2>;
5901 * } SignatureSchemeList;
5902 *
5903 * The `extension_data` field of signature algorithm contains a `SignatureAndHashAlgorithm`
5904 * value (TLS 1.2 RFC5246):
5905 * enum {
5906 * none(0), md5(1), sha1(2), sha224(3), sha256(4), sha384(5),
5907 * sha512(6), (255)
5908 * } HashAlgorithm;
5909 *
5910 * enum { anonymous(0), rsa(1), dsa(2), ecdsa(3), (255) }
5911 * SignatureAlgorithm;
5912 *
5913 * struct {
5914 * HashAlgorithm hash;
5915 * SignatureAlgorithm signature;
5916 * } SignatureAndHashAlgorithm;
5917 *
5918 * SignatureAndHashAlgorithm
5919 * supported_signature_algorithms<2..2^16-2>;
5920 *
5921 * The TLS 1.3 signature algorithm extension was defined to be a compatible
5922 * generalization of the TLS 1.2 signature algorithm extension.
5923 * `SignatureAndHashAlgorithm` field of TLS 1.2 can be represented by
5924 * `SignatureScheme` field of TLS 1.3
5925 *
5926 */
Gilles Peskine449bd832023-01-11 14:50:10 +01005927int mbedtls_ssl_parse_sig_alg_ext(mbedtls_ssl_context *ssl,
5928 const unsigned char *buf,
5929 const unsigned char *end)
Gabor Mezei078e8032022-04-27 21:17:56 +02005930{
5931 const unsigned char *p = buf;
5932 size_t supported_sig_algs_len = 0;
5933 const unsigned char *supported_sig_algs_end;
5934 uint16_t sig_alg;
5935 uint32_t common_idx = 0;
5936
Gilles Peskine449bd832023-01-11 14:50:10 +01005937 MBEDTLS_SSL_CHK_BUF_READ_PTR(p, end, 2);
5938 supported_sig_algs_len = MBEDTLS_GET_UINT16_BE(p, 0);
Gabor Mezei078e8032022-04-27 21:17:56 +02005939 p += 2;
5940
Gilles Peskine449bd832023-01-11 14:50:10 +01005941 memset(ssl->handshake->received_sig_algs, 0,
5942 sizeof(ssl->handshake->received_sig_algs));
Gabor Mezei078e8032022-04-27 21:17:56 +02005943
Gilles Peskine449bd832023-01-11 14:50:10 +01005944 MBEDTLS_SSL_CHK_BUF_READ_PTR(p, end, supported_sig_algs_len);
Gabor Mezei078e8032022-04-27 21:17:56 +02005945 supported_sig_algs_end = p + supported_sig_algs_len;
Gilles Peskine449bd832023-01-11 14:50:10 +01005946 while (p < supported_sig_algs_end) {
5947 MBEDTLS_SSL_CHK_BUF_READ_PTR(p, supported_sig_algs_end, 2);
5948 sig_alg = MBEDTLS_GET_UINT16_BE(p, 0);
Gabor Mezei078e8032022-04-27 21:17:56 +02005949 p += 2;
Gilles Peskine449bd832023-01-11 14:50:10 +01005950 MBEDTLS_SSL_DEBUG_MSG(4, ("received signature algorithm: 0x%x %s",
5951 sig_alg,
5952 mbedtls_ssl_sig_alg_to_str(sig_alg)));
Jerry Yu2fe6c632022-06-29 10:02:38 +08005953#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
Gilles Peskine449bd832023-01-11 14:50:10 +01005954 if (ssl->tls_version == MBEDTLS_SSL_VERSION_TLS1_2 &&
5955 (!(mbedtls_ssl_sig_alg_is_supported(ssl, sig_alg) &&
5956 mbedtls_ssl_sig_alg_is_offered(ssl, sig_alg)))) {
Gabor Mezei078e8032022-04-27 21:17:56 +02005957 continue;
Jerry Yu2fe6c632022-06-29 10:02:38 +08005958 }
5959#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Gabor Mezei078e8032022-04-27 21:17:56 +02005960
Gilles Peskine449bd832023-01-11 14:50:10 +01005961 MBEDTLS_SSL_DEBUG_MSG(4, ("valid signature algorithm: %s",
5962 mbedtls_ssl_sig_alg_to_str(sig_alg)));
Gabor Mezei078e8032022-04-27 21:17:56 +02005963
Gilles Peskine449bd832023-01-11 14:50:10 +01005964 if (common_idx + 1 < MBEDTLS_RECEIVED_SIG_ALGS_SIZE) {
Gabor Mezei078e8032022-04-27 21:17:56 +02005965 ssl->handshake->received_sig_algs[common_idx] = sig_alg;
5966 common_idx += 1;
5967 }
5968 }
5969 /* Check that we consumed all the message. */
Gilles Peskine449bd832023-01-11 14:50:10 +01005970 if (p != end) {
5971 MBEDTLS_SSL_DEBUG_MSG(1,
5972 ("Signature algorithms extension length misaligned"));
5973 MBEDTLS_SSL_PEND_FATAL_ALERT(MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR,
5974 MBEDTLS_ERR_SSL_DECODE_ERROR);
5975 return MBEDTLS_ERR_SSL_DECODE_ERROR;
Gabor Mezei078e8032022-04-27 21:17:56 +02005976 }
5977
Gilles Peskine449bd832023-01-11 14:50:10 +01005978 if (common_idx == 0) {
5979 MBEDTLS_SSL_DEBUG_MSG(3, ("no signature algorithm in common"));
5980 MBEDTLS_SSL_PEND_FATAL_ALERT(MBEDTLS_SSL_ALERT_MSG_HANDSHAKE_FAILURE,
5981 MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE);
5982 return MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE;
Gabor Mezei078e8032022-04-27 21:17:56 +02005983 }
5984
Gabor Mezei15b95a62022-05-09 16:37:58 +02005985 ssl->handshake->received_sig_algs[common_idx] = MBEDTLS_TLS_SIG_NONE;
Gilles Peskine449bd832023-01-11 14:50:10 +01005986 return 0;
Gabor Mezei078e8032022-04-27 21:17:56 +02005987}
5988
Ronald Crone68ab4f2022-10-05 12:46:29 +02005989#endif /* MBEDTLS_SSL_HANDSHAKE_WITH_CERT_ENABLED */
Gabor Mezei078e8032022-04-27 21:17:56 +02005990
Jerry Yudc7bd172022-02-17 13:44:15 +08005991#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
5992
5993#if defined(MBEDTLS_USE_PSA_CRYPTO)
5994
Gilles Peskine449bd832023-01-11 14:50:10 +01005995static psa_status_t setup_psa_key_derivation(psa_key_derivation_operation_t *derivation,
5996 mbedtls_svc_key_id_t key,
5997 psa_algorithm_t alg,
5998 const unsigned char *raw_psk, size_t raw_psk_length,
5999 const unsigned char *seed, size_t seed_length,
6000 const unsigned char *label, size_t label_length,
6001 const unsigned char *other_secret,
6002 size_t other_secret_length,
6003 size_t capacity)
Jerry Yudc7bd172022-02-17 13:44:15 +08006004{
6005 psa_status_t status;
6006
Gilles Peskine449bd832023-01-11 14:50:10 +01006007 status = psa_key_derivation_setup(derivation, alg);
6008 if (status != PSA_SUCCESS) {
6009 return status;
6010 }
Jerry Yudc7bd172022-02-17 13:44:15 +08006011
Gilles Peskine449bd832023-01-11 14:50:10 +01006012 if (PSA_ALG_IS_TLS12_PRF(alg) || PSA_ALG_IS_TLS12_PSK_TO_MS(alg)) {
6013 status = psa_key_derivation_input_bytes(derivation,
6014 PSA_KEY_DERIVATION_INPUT_SEED,
6015 seed, seed_length);
6016 if (status != PSA_SUCCESS) {
6017 return status;
Przemek Stekiel1f027032022-04-05 17:12:11 +02006018 }
6019
Gilles Peskine449bd832023-01-11 14:50:10 +01006020 if (other_secret != NULL) {
6021 status = psa_key_derivation_input_bytes(derivation,
6022 PSA_KEY_DERIVATION_INPUT_OTHER_SECRET,
6023 other_secret, other_secret_length);
6024 if (status != PSA_SUCCESS) {
6025 return status;
6026 }
6027 }
6028
6029 if (mbedtls_svc_key_id_is_null(key)) {
Jerry Yudc7bd172022-02-17 13:44:15 +08006030 status = psa_key_derivation_input_bytes(
6031 derivation, PSA_KEY_DERIVATION_INPUT_SECRET,
Gilles Peskine449bd832023-01-11 14:50:10 +01006032 raw_psk, raw_psk_length);
6033 } else {
Jerry Yudc7bd172022-02-17 13:44:15 +08006034 status = psa_key_derivation_input_key(
Gilles Peskine449bd832023-01-11 14:50:10 +01006035 derivation, PSA_KEY_DERIVATION_INPUT_SECRET, key);
Jerry Yudc7bd172022-02-17 13:44:15 +08006036 }
Gilles Peskine449bd832023-01-11 14:50:10 +01006037 if (status != PSA_SUCCESS) {
6038 return status;
6039 }
Jerry Yudc7bd172022-02-17 13:44:15 +08006040
Gilles Peskine449bd832023-01-11 14:50:10 +01006041 status = psa_key_derivation_input_bytes(derivation,
6042 PSA_KEY_DERIVATION_INPUT_LABEL,
6043 label, label_length);
6044 if (status != PSA_SUCCESS) {
6045 return status;
6046 }
6047 } else {
6048 return PSA_ERROR_NOT_SUPPORTED;
Jerry Yudc7bd172022-02-17 13:44:15 +08006049 }
6050
Gilles Peskine449bd832023-01-11 14:50:10 +01006051 status = psa_key_derivation_set_capacity(derivation, capacity);
6052 if (status != PSA_SUCCESS) {
6053 return status;
6054 }
Jerry Yudc7bd172022-02-17 13:44:15 +08006055
Gilles Peskine449bd832023-01-11 14:50:10 +01006056 return PSA_SUCCESS;
Jerry Yudc7bd172022-02-17 13:44:15 +08006057}
6058
Andrzej Kurek57d10632022-10-24 10:32:01 -04006059#if defined(PSA_WANT_ALG_SHA_384) || \
6060 defined(PSA_WANT_ALG_SHA_256)
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02006061MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01006062static int tls_prf_generic(mbedtls_md_type_t md_type,
6063 const unsigned char *secret, size_t slen,
6064 const char *label,
6065 const unsigned char *random, size_t rlen,
6066 unsigned char *dstbuf, size_t dlen)
Jerry Yudc7bd172022-02-17 13:44:15 +08006067{
6068 psa_status_t status;
6069 psa_algorithm_t alg;
6070 mbedtls_svc_key_id_t master_key = MBEDTLS_SVC_KEY_ID_INIT;
6071 psa_key_derivation_operation_t derivation =
6072 PSA_KEY_DERIVATION_OPERATION_INIT;
6073
Gilles Peskine449bd832023-01-11 14:50:10 +01006074 if (md_type == MBEDTLS_MD_SHA384) {
Jerry Yudc7bd172022-02-17 13:44:15 +08006075 alg = PSA_ALG_TLS12_PRF(PSA_ALG_SHA_384);
Gilles Peskine449bd832023-01-11 14:50:10 +01006076 } else {
Jerry Yudc7bd172022-02-17 13:44:15 +08006077 alg = PSA_ALG_TLS12_PRF(PSA_ALG_SHA_256);
Gilles Peskine449bd832023-01-11 14:50:10 +01006078 }
Jerry Yudc7bd172022-02-17 13:44:15 +08006079
6080 /* Normally a "secret" should be long enough to be impossible to
6081 * find by brute force, and in particular should not be empty. But
6082 * this PRF is also used to derive an IV, in particular in EAP-TLS,
6083 * and for this use case it makes sense to have a 0-length "secret".
6084 * Since the key API doesn't allow importing a key of length 0,
6085 * keep master_key=0, which setup_psa_key_derivation() understands
6086 * to mean a 0-length "secret" input. */
Gilles Peskine449bd832023-01-11 14:50:10 +01006087 if (slen != 0) {
Jerry Yudc7bd172022-02-17 13:44:15 +08006088 psa_key_attributes_t key_attributes = psa_key_attributes_init();
Gilles Peskine449bd832023-01-11 14:50:10 +01006089 psa_set_key_usage_flags(&key_attributes, PSA_KEY_USAGE_DERIVE);
6090 psa_set_key_algorithm(&key_attributes, alg);
6091 psa_set_key_type(&key_attributes, PSA_KEY_TYPE_DERIVE);
Jerry Yudc7bd172022-02-17 13:44:15 +08006092
Gilles Peskine449bd832023-01-11 14:50:10 +01006093 status = psa_import_key(&key_attributes, secret, slen, &master_key);
6094 if (status != PSA_SUCCESS) {
6095 return MBEDTLS_ERR_SSL_HW_ACCEL_FAILED;
6096 }
Jerry Yudc7bd172022-02-17 13:44:15 +08006097 }
6098
Gilles Peskine449bd832023-01-11 14:50:10 +01006099 status = setup_psa_key_derivation(&derivation,
6100 master_key, alg,
6101 NULL, 0,
6102 random, rlen,
6103 (unsigned char const *) label,
6104 (size_t) strlen(label),
6105 NULL, 0,
6106 dlen);
6107 if (status != PSA_SUCCESS) {
6108 psa_key_derivation_abort(&derivation);
6109 psa_destroy_key(master_key);
6110 return MBEDTLS_ERR_SSL_HW_ACCEL_FAILED;
Jerry Yudc7bd172022-02-17 13:44:15 +08006111 }
6112
Gilles Peskine449bd832023-01-11 14:50:10 +01006113 status = psa_key_derivation_output_bytes(&derivation, dstbuf, dlen);
6114 if (status != PSA_SUCCESS) {
6115 psa_key_derivation_abort(&derivation);
6116 psa_destroy_key(master_key);
6117 return MBEDTLS_ERR_SSL_HW_ACCEL_FAILED;
Jerry Yudc7bd172022-02-17 13:44:15 +08006118 }
6119
Gilles Peskine449bd832023-01-11 14:50:10 +01006120 status = psa_key_derivation_abort(&derivation);
6121 if (status != PSA_SUCCESS) {
6122 psa_destroy_key(master_key);
6123 return MBEDTLS_ERR_SSL_HW_ACCEL_FAILED;
Jerry Yudc7bd172022-02-17 13:44:15 +08006124 }
6125
Gilles Peskine449bd832023-01-11 14:50:10 +01006126 if (!mbedtls_svc_key_id_is_null(master_key)) {
6127 status = psa_destroy_key(master_key);
6128 }
6129 if (status != PSA_SUCCESS) {
6130 return MBEDTLS_ERR_SSL_HW_ACCEL_FAILED;
6131 }
Jerry Yudc7bd172022-02-17 13:44:15 +08006132
Gilles Peskine449bd832023-01-11 14:50:10 +01006133 return 0;
Jerry Yudc7bd172022-02-17 13:44:15 +08006134}
Andrzej Kurek57d10632022-10-24 10:32:01 -04006135#endif /* PSA_WANT_ALG_SHA_256 || PSA_WANT_ALG_SHA_384 */
Jerry Yudc7bd172022-02-17 13:44:15 +08006136#else /* MBEDTLS_USE_PSA_CRYPTO */
6137
Andrzej Kurek57d10632022-10-24 10:32:01 -04006138#if defined(MBEDTLS_MD_C) && \
Gilles Peskine449bd832023-01-11 14:50:10 +01006139 (defined(MBEDTLS_SHA256_C) || \
6140 defined(MBEDTLS_SHA384_C))
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02006141MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01006142static int tls_prf_generic(mbedtls_md_type_t md_type,
6143 const unsigned char *secret, size_t slen,
6144 const char *label,
6145 const unsigned char *random, size_t rlen,
6146 unsigned char *dstbuf, size_t dlen)
Jerry Yudc7bd172022-02-17 13:44:15 +08006147{
6148 size_t nb;
6149 size_t i, j, k, md_len;
6150 unsigned char *tmp;
6151 size_t tmp_len = 0;
6152 unsigned char h_i[MBEDTLS_MD_MAX_SIZE];
6153 const mbedtls_md_info_t *md_info;
6154 mbedtls_md_context_t md_ctx;
6155 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
6156
Gilles Peskine449bd832023-01-11 14:50:10 +01006157 mbedtls_md_init(&md_ctx);
Jerry Yudc7bd172022-02-17 13:44:15 +08006158
Gilles Peskine449bd832023-01-11 14:50:10 +01006159 if ((md_info = mbedtls_md_info_from_type(md_type)) == NULL) {
6160 return MBEDTLS_ERR_SSL_INTERNAL_ERROR;
6161 }
Jerry Yudc7bd172022-02-17 13:44:15 +08006162
Gilles Peskine449bd832023-01-11 14:50:10 +01006163 md_len = mbedtls_md_get_size(md_info);
Jerry Yudc7bd172022-02-17 13:44:15 +08006164
Gilles Peskine449bd832023-01-11 14:50:10 +01006165 tmp_len = md_len + strlen(label) + rlen;
6166 tmp = mbedtls_calloc(1, tmp_len);
6167 if (tmp == NULL) {
Jerry Yudc7bd172022-02-17 13:44:15 +08006168 ret = MBEDTLS_ERR_SSL_ALLOC_FAILED;
6169 goto exit;
6170 }
6171
Gilles Peskine449bd832023-01-11 14:50:10 +01006172 nb = strlen(label);
6173 memcpy(tmp + md_len, label, nb);
6174 memcpy(tmp + md_len + nb, random, rlen);
Jerry Yudc7bd172022-02-17 13:44:15 +08006175 nb += rlen;
6176
6177 /*
6178 * Compute P_<hash>(secret, label + random)[0..dlen]
6179 */
Gilles Peskine449bd832023-01-11 14:50:10 +01006180 if ((ret = mbedtls_md_setup(&md_ctx, md_info, 1)) != 0) {
Jerry Yudc7bd172022-02-17 13:44:15 +08006181 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01006182 }
Jerry Yudc7bd172022-02-17 13:44:15 +08006183
Gilles Peskine449bd832023-01-11 14:50:10 +01006184 ret = mbedtls_md_hmac_starts(&md_ctx, secret, slen);
6185 if (ret != 0) {
Jerry Yudc7bd172022-02-17 13:44:15 +08006186 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01006187 }
6188 ret = mbedtls_md_hmac_update(&md_ctx, tmp + md_len, nb);
6189 if (ret != 0) {
Jerry Yudc7bd172022-02-17 13:44:15 +08006190 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01006191 }
6192 ret = mbedtls_md_hmac_finish(&md_ctx, tmp);
6193 if (ret != 0) {
Jerry Yudc7bd172022-02-17 13:44:15 +08006194 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01006195 }
Jerry Yudc7bd172022-02-17 13:44:15 +08006196
Gilles Peskine449bd832023-01-11 14:50:10 +01006197 for (i = 0; i < dlen; i += md_len) {
6198 ret = mbedtls_md_hmac_reset(&md_ctx);
6199 if (ret != 0) {
Jerry Yudc7bd172022-02-17 13:44:15 +08006200 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01006201 }
6202 ret = mbedtls_md_hmac_update(&md_ctx, tmp, md_len + nb);
6203 if (ret != 0) {
Jerry Yudc7bd172022-02-17 13:44:15 +08006204 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01006205 }
6206 ret = mbedtls_md_hmac_finish(&md_ctx, h_i);
6207 if (ret != 0) {
Jerry Yudc7bd172022-02-17 13:44:15 +08006208 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01006209 }
Jerry Yudc7bd172022-02-17 13:44:15 +08006210
Gilles Peskine449bd832023-01-11 14:50:10 +01006211 ret = mbedtls_md_hmac_reset(&md_ctx);
6212 if (ret != 0) {
Jerry Yudc7bd172022-02-17 13:44:15 +08006213 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01006214 }
6215 ret = mbedtls_md_hmac_update(&md_ctx, tmp, md_len);
6216 if (ret != 0) {
Jerry Yudc7bd172022-02-17 13:44:15 +08006217 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01006218 }
6219 ret = mbedtls_md_hmac_finish(&md_ctx, tmp);
6220 if (ret != 0) {
Jerry Yudc7bd172022-02-17 13:44:15 +08006221 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01006222 }
Jerry Yudc7bd172022-02-17 13:44:15 +08006223
Gilles Peskine449bd832023-01-11 14:50:10 +01006224 k = (i + md_len > dlen) ? dlen % md_len : md_len;
Jerry Yudc7bd172022-02-17 13:44:15 +08006225
Gilles Peskine449bd832023-01-11 14:50:10 +01006226 for (j = 0; j < k; j++) {
Jerry Yudc7bd172022-02-17 13:44:15 +08006227 dstbuf[i + j] = h_i[j];
Gilles Peskine449bd832023-01-11 14:50:10 +01006228 }
Jerry Yudc7bd172022-02-17 13:44:15 +08006229 }
6230
6231exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01006232 mbedtls_md_free(&md_ctx);
Jerry Yudc7bd172022-02-17 13:44:15 +08006233
Gilles Peskine449bd832023-01-11 14:50:10 +01006234 if (tmp != NULL) {
6235 mbedtls_platform_zeroize(tmp, tmp_len);
6236 }
Dave Rodgman29b9b2b2022-11-01 16:08:14 +00006237
Gilles Peskine449bd832023-01-11 14:50:10 +01006238 mbedtls_platform_zeroize(h_i, sizeof(h_i));
Jerry Yudc7bd172022-02-17 13:44:15 +08006239
Gilles Peskine449bd832023-01-11 14:50:10 +01006240 mbedtls_free(tmp);
Jerry Yudc7bd172022-02-17 13:44:15 +08006241
Gilles Peskine449bd832023-01-11 14:50:10 +01006242 return ret;
Jerry Yudc7bd172022-02-17 13:44:15 +08006243}
Andrzej Kurek57d10632022-10-24 10:32:01 -04006244#endif /* MBEDTLS_MD_C && ( MBEDTLS_SHA256_C || MBEDTLS_SHA384_C ) */
Jerry Yudc7bd172022-02-17 13:44:15 +08006245#endif /* MBEDTLS_USE_PSA_CRYPTO */
6246
Andrzej Kurek25f27152022-08-17 16:09:31 -04006247#if defined(MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02006248MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01006249static int tls_prf_sha256(const unsigned char *secret, size_t slen,
6250 const char *label,
6251 const unsigned char *random, size_t rlen,
6252 unsigned char *dstbuf, size_t dlen)
Jerry Yudc7bd172022-02-17 13:44:15 +08006253{
Gilles Peskine449bd832023-01-11 14:50:10 +01006254 return tls_prf_generic(MBEDTLS_MD_SHA256, secret, slen,
6255 label, random, rlen, dstbuf, dlen);
Jerry Yudc7bd172022-02-17 13:44:15 +08006256}
Andrzej Kurekcccb0442022-08-19 03:42:11 -04006257#endif /* MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA*/
Jerry Yudc7bd172022-02-17 13:44:15 +08006258
Andrzej Kurek25f27152022-08-17 16:09:31 -04006259#if defined(MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02006260MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01006261static int tls_prf_sha384(const unsigned char *secret, size_t slen,
6262 const char *label,
6263 const unsigned char *random, size_t rlen,
6264 unsigned char *dstbuf, size_t dlen)
Jerry Yudc7bd172022-02-17 13:44:15 +08006265{
Gilles Peskine449bd832023-01-11 14:50:10 +01006266 return tls_prf_generic(MBEDTLS_MD_SHA384, secret, slen,
6267 label, random, rlen, dstbuf, dlen);
Jerry Yudc7bd172022-02-17 13:44:15 +08006268}
Andrzej Kurekcccb0442022-08-19 03:42:11 -04006269#endif /* MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA*/
Jerry Yudc7bd172022-02-17 13:44:15 +08006270
Jerry Yuf009d862022-02-17 14:01:37 +08006271/*
6272 * Set appropriate PRF function and other SSL / TLS1.2 functions
6273 *
6274 * Inputs:
Jerry Yuf009d862022-02-17 14:01:37 +08006275 * - hash associated with the ciphersuite (only used by TLS 1.2)
6276 *
6277 * Outputs:
6278 * - the tls_prf, calc_verify and calc_finished members of handshake structure
6279 */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02006280MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01006281static int ssl_set_handshake_prfs(mbedtls_ssl_handshake_params *handshake,
6282 mbedtls_md_type_t hash)
Jerry Yuf009d862022-02-17 14:01:37 +08006283{
Andrzej Kurek25f27152022-08-17 16:09:31 -04006284#if defined(MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Gilles Peskine449bd832023-01-11 14:50:10 +01006285 if (hash == MBEDTLS_MD_SHA384) {
Jerry Yuf009d862022-02-17 14:01:37 +08006286 handshake->tls_prf = tls_prf_sha384;
6287 handshake->calc_verify = ssl_calc_verify_tls_sha384;
6288 handshake->calc_finished = ssl_calc_finished_tls_sha384;
Gilles Peskine449bd832023-01-11 14:50:10 +01006289 } else
Jerry Yuf009d862022-02-17 14:01:37 +08006290#endif
Andrzej Kurek25f27152022-08-17 16:09:31 -04006291#if defined(MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Jerry Yuf009d862022-02-17 14:01:37 +08006292 {
Ronald Cron81591aa2022-03-07 09:05:51 +01006293 (void) hash;
Jerry Yuf009d862022-02-17 14:01:37 +08006294 handshake->tls_prf = tls_prf_sha256;
6295 handshake->calc_verify = ssl_calc_verify_tls_sha256;
6296 handshake->calc_finished = ssl_calc_finished_tls_sha256;
6297 }
Ronald Cron81591aa2022-03-07 09:05:51 +01006298#else
Jerry Yuf009d862022-02-17 14:01:37 +08006299 {
Jerry Yuf009d862022-02-17 14:01:37 +08006300 (void) handshake;
Ronald Cron81591aa2022-03-07 09:05:51 +01006301 (void) hash;
Gilles Peskine449bd832023-01-11 14:50:10 +01006302 return MBEDTLS_ERR_SSL_INTERNAL_ERROR;
Jerry Yuf009d862022-02-17 14:01:37 +08006303 }
Ronald Cron81591aa2022-03-07 09:05:51 +01006304#endif
Jerry Yuf009d862022-02-17 14:01:37 +08006305
Gilles Peskine449bd832023-01-11 14:50:10 +01006306 return 0;
Jerry Yuf009d862022-02-17 14:01:37 +08006307}
Jerry Yud6ab2352022-02-17 14:03:43 +08006308
Jerry Yu2a7b5ac2022-02-17 14:07:00 +08006309/*
6310 * Compute master secret if needed
6311 *
6312 * Parameters:
6313 * [in/out] handshake
6314 * [in] resume, premaster, extended_ms, calc_verify, tls_prf
6315 * (PSA-PSK) ciphersuite_info, psk_opaque
6316 * [out] premaster (cleared)
6317 * [out] master
6318 * [in] ssl: optionally used for debugging, EMS and PSA-PSK
6319 * debug: conf->f_dbg, conf->p_dbg
6320 * EMS: passed to calc_verify (debug + session_negotiate)
Ronald Crona25cf582022-03-07 11:10:36 +01006321 * PSA-PSA: conf
Jerry Yu2a7b5ac2022-02-17 14:07:00 +08006322 */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02006323MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01006324static int ssl_compute_master(mbedtls_ssl_handshake_params *handshake,
6325 unsigned char *master,
6326 const mbedtls_ssl_context *ssl)
Jerry Yu2a7b5ac2022-02-17 14:07:00 +08006327{
6328 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
6329
6330 /* cf. RFC 5246, Section 8.1:
6331 * "The master secret is always exactly 48 bytes in length." */
6332 size_t const master_secret_len = 48;
6333
6334#if defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET)
6335 unsigned char session_hash[48];
6336#endif /* MBEDTLS_SSL_EXTENDED_MASTER_SECRET */
6337
6338 /* The label for the KDF used for key expansion.
6339 * This is either "master secret" or "extended master secret"
6340 * depending on whether the Extended Master Secret extension
6341 * is used. */
6342 char const *lbl = "master secret";
6343
Przemek Stekielae4ed302022-04-05 17:15:55 +02006344 /* The seed for the KDF used for key expansion.
Jerry Yu2a7b5ac2022-02-17 14:07:00 +08006345 * - If the Extended Master Secret extension is not used,
6346 * this is ClientHello.Random + ServerHello.Random
6347 * (see Sect. 8.1 in RFC 5246).
6348 * - If the Extended Master Secret extension is used,
6349 * this is the transcript of the handshake so far.
6350 * (see Sect. 4 in RFC 7627). */
Przemek Stekielae4ed302022-04-05 17:15:55 +02006351 unsigned char const *seed = handshake->randbytes;
6352 size_t seed_len = 64;
Jerry Yu2a7b5ac2022-02-17 14:07:00 +08006353
6354#if !defined(MBEDTLS_DEBUG_C) && \
6355 !defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET) && \
6356 !(defined(MBEDTLS_USE_PSA_CRYPTO) && \
Gilles Peskine449bd832023-01-11 14:50:10 +01006357 defined(MBEDTLS_KEY_EXCHANGE_PSK_ENABLED))
Jerry Yu2a7b5ac2022-02-17 14:07:00 +08006358 ssl = NULL; /* make sure we don't use it except for those cases */
6359 (void) ssl;
6360#endif
6361
Gilles Peskine449bd832023-01-11 14:50:10 +01006362 if (handshake->resume != 0) {
6363 MBEDTLS_SSL_DEBUG_MSG(3, ("no premaster (session resumed)"));
6364 return 0;
Jerry Yu2a7b5ac2022-02-17 14:07:00 +08006365 }
6366
6367#if defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET)
Gilles Peskine449bd832023-01-11 14:50:10 +01006368 if (handshake->extended_ms == MBEDTLS_SSL_EXTENDED_MS_ENABLED) {
Jerry Yu2a7b5ac2022-02-17 14:07:00 +08006369 lbl = "extended master secret";
Przemek Stekielae4ed302022-04-05 17:15:55 +02006370 seed = session_hash;
Manuel Pégourié-Gonnardb8b07aa2023-02-06 00:34:21 +01006371 ret = handshake->calc_verify(ssl, session_hash, &seed_len);
6372 if (ret != 0) {
6373 MBEDTLS_SSL_DEBUG_RET(1, "calc_verify", ret);
6374 }
Jerry Yu2a7b5ac2022-02-17 14:07:00 +08006375
Gilles Peskine449bd832023-01-11 14:50:10 +01006376 MBEDTLS_SSL_DEBUG_BUF(3, "session hash for extended master secret",
6377 session_hash, seed_len);
Jerry Yu2a7b5ac2022-02-17 14:07:00 +08006378 }
Przemek Stekiel169bf0b2022-04-29 07:53:29 +02006379#endif /* MBEDTLS_SSL_EXTENDED_MASTER_SECRET */
Jerry Yu2a7b5ac2022-02-17 14:07:00 +08006380
Przemek Stekiel99114f32022-04-22 11:20:09 +02006381#if defined(MBEDTLS_USE_PSA_CRYPTO) && \
Przemek Stekiel8a4b7fd2022-04-28 09:22:22 +02006382 defined(MBEDTLS_KEY_EXCHANGE_SOME_PSK_ENABLED)
Gilles Peskine449bd832023-01-11 14:50:10 +01006383 if (mbedtls_ssl_ciphersuite_uses_psk(handshake->ciphersuite_info) == 1) {
Jerry Yu2a7b5ac2022-02-17 14:07:00 +08006384 /* Perform PSK-to-MS expansion in a single step. */
6385 psa_status_t status;
6386 psa_algorithm_t alg;
6387 mbedtls_svc_key_id_t psk;
6388 psa_key_derivation_operation_t derivation =
6389 PSA_KEY_DERIVATION_OPERATION_INIT;
6390 mbedtls_md_type_t hash_alg = handshake->ciphersuite_info->mac;
6391
Gilles Peskine449bd832023-01-11 14:50:10 +01006392 MBEDTLS_SSL_DEBUG_MSG(2, ("perform PSA-based PSK-to-MS expansion"));
Jerry Yu2a7b5ac2022-02-17 14:07:00 +08006393
Gilles Peskine449bd832023-01-11 14:50:10 +01006394 psk = mbedtls_ssl_get_opaque_psk(ssl);
Jerry Yu2a7b5ac2022-02-17 14:07:00 +08006395
Gilles Peskine449bd832023-01-11 14:50:10 +01006396 if (hash_alg == MBEDTLS_MD_SHA384) {
Jerry Yu2a7b5ac2022-02-17 14:07:00 +08006397 alg = PSA_ALG_TLS12_PSK_TO_MS(PSA_ALG_SHA_384);
Gilles Peskine449bd832023-01-11 14:50:10 +01006398 } else {
Jerry Yu2a7b5ac2022-02-17 14:07:00 +08006399 alg = PSA_ALG_TLS12_PSK_TO_MS(PSA_ALG_SHA_256);
Gilles Peskine449bd832023-01-11 14:50:10 +01006400 }
Jerry Yu2a7b5ac2022-02-17 14:07:00 +08006401
Przemek Stekiel51a1f362022-04-13 08:57:06 +02006402 size_t other_secret_len = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +01006403 unsigned char *other_secret = NULL;
Przemek Stekielc2033402022-04-05 17:19:41 +02006404
Gilles Peskine449bd832023-01-11 14:50:10 +01006405 switch (handshake->ciphersuite_info->key_exchange) {
Przemek Stekiel19b80f82022-04-14 08:29:31 +02006406 /* Provide other secret.
Przemek Stekiel51a1f362022-04-13 08:57:06 +02006407 * Other secret is stored in premaster, where first 2 bytes hold the
Przemek Stekiel19b80f82022-04-14 08:29:31 +02006408 * length of the other key.
Przemek Stekielc2033402022-04-05 17:19:41 +02006409 */
Przemek Stekiel19b80f82022-04-14 08:29:31 +02006410 case MBEDTLS_KEY_EXCHANGE_RSA_PSK:
Przemek Stekiel8abcee92022-04-28 09:16:28 +02006411 /* For RSA-PSK other key length is always 48 bytes. */
Przemek Stekiel19b80f82022-04-14 08:29:31 +02006412 other_secret_len = 48;
6413 other_secret = handshake->premaster + 2;
6414 break;
6415 case MBEDTLS_KEY_EXCHANGE_ECDHE_PSK:
Przemek Stekielb293aaa2022-04-19 12:22:38 +02006416 case MBEDTLS_KEY_EXCHANGE_DHE_PSK:
Przemek Stekiel19b80f82022-04-14 08:29:31 +02006417 other_secret_len = MBEDTLS_GET_UINT16_BE(handshake->premaster, 0);
6418 other_secret = handshake->premaster + 2;
6419 break;
6420 default:
6421 break;
Przemek Stekielc2033402022-04-05 17:19:41 +02006422 }
6423
Gilles Peskine449bd832023-01-11 14:50:10 +01006424 status = setup_psa_key_derivation(&derivation, psk, alg,
6425 ssl->conf->psk, ssl->conf->psk_len,
6426 seed, seed_len,
6427 (unsigned char const *) lbl,
6428 (size_t) strlen(lbl),
6429 other_secret, other_secret_len,
6430 master_secret_len);
6431 if (status != PSA_SUCCESS) {
6432 psa_key_derivation_abort(&derivation);
6433 return MBEDTLS_ERR_SSL_HW_ACCEL_FAILED;
Jerry Yu2a7b5ac2022-02-17 14:07:00 +08006434 }
6435
Gilles Peskine449bd832023-01-11 14:50:10 +01006436 status = psa_key_derivation_output_bytes(&derivation,
6437 master,
6438 master_secret_len);
6439 if (status != PSA_SUCCESS) {
6440 psa_key_derivation_abort(&derivation);
6441 return MBEDTLS_ERR_SSL_HW_ACCEL_FAILED;
Jerry Yu2a7b5ac2022-02-17 14:07:00 +08006442 }
6443
Gilles Peskine449bd832023-01-11 14:50:10 +01006444 status = psa_key_derivation_abort(&derivation);
6445 if (status != PSA_SUCCESS) {
6446 return MBEDTLS_ERR_SSL_HW_ACCEL_FAILED;
6447 }
6448 } else
Jerry Yu2a7b5ac2022-02-17 14:07:00 +08006449#endif
6450 {
Neil Armstrongca7d5062022-05-31 14:43:23 +02006451#if defined(MBEDTLS_USE_PSA_CRYPTO) && \
Gilles Peskine449bd832023-01-11 14:50:10 +01006452 defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
6453 if (handshake->ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECJPAKE) {
Neil Armstrongca7d5062022-05-31 14:43:23 +02006454 psa_status_t status;
6455 psa_algorithm_t alg = PSA_ALG_TLS12_ECJPAKE_TO_PMS;
6456 psa_key_derivation_operation_t derivation =
6457 PSA_KEY_DERIVATION_OPERATION_INIT;
6458
Gilles Peskine449bd832023-01-11 14:50:10 +01006459 MBEDTLS_SSL_DEBUG_MSG(2, ("perform PSA-based PMS KDF for ECJPAKE"));
Neil Armstrongca7d5062022-05-31 14:43:23 +02006460
6461 handshake->pmslen = PSA_TLS12_ECJPAKE_TO_PMS_DATA_SIZE;
6462
Gilles Peskine449bd832023-01-11 14:50:10 +01006463 status = psa_key_derivation_setup(&derivation, alg);
6464 if (status != PSA_SUCCESS) {
6465 return MBEDTLS_ERR_SSL_HW_ACCEL_FAILED;
Neil Armstrongca7d5062022-05-31 14:43:23 +02006466 }
6467
Gilles Peskine449bd832023-01-11 14:50:10 +01006468 status = psa_key_derivation_set_capacity(&derivation,
6469 PSA_TLS12_ECJPAKE_TO_PMS_DATA_SIZE);
6470 if (status != PSA_SUCCESS) {
6471 psa_key_derivation_abort(&derivation);
6472 return MBEDTLS_ERR_SSL_HW_ACCEL_FAILED;
Neil Armstrongca7d5062022-05-31 14:43:23 +02006473 }
6474
Gilles Peskine449bd832023-01-11 14:50:10 +01006475 status = psa_pake_get_implicit_key(&handshake->psa_pake_ctx,
6476 &derivation);
6477 if (status != PSA_SUCCESS) {
6478 psa_key_derivation_abort(&derivation);
6479 return MBEDTLS_ERR_SSL_HW_ACCEL_FAILED;
Neil Armstrongca7d5062022-05-31 14:43:23 +02006480 }
6481
Gilles Peskine449bd832023-01-11 14:50:10 +01006482 status = psa_key_derivation_output_bytes(&derivation,
6483 handshake->premaster,
6484 handshake->pmslen);
6485 if (status != PSA_SUCCESS) {
6486 psa_key_derivation_abort(&derivation);
6487 return MBEDTLS_ERR_SSL_HW_ACCEL_FAILED;
6488 }
6489
6490 status = psa_key_derivation_abort(&derivation);
6491 if (status != PSA_SUCCESS) {
6492 return MBEDTLS_ERR_SSL_HW_ACCEL_FAILED;
Neil Armstrongca7d5062022-05-31 14:43:23 +02006493 }
6494 }
6495#endif
Gilles Peskine449bd832023-01-11 14:50:10 +01006496 ret = handshake->tls_prf(handshake->premaster, handshake->pmslen,
6497 lbl, seed, seed_len,
6498 master,
6499 master_secret_len);
6500 if (ret != 0) {
6501 MBEDTLS_SSL_DEBUG_RET(1, "prf", ret);
6502 return ret;
Jerry Yu2a7b5ac2022-02-17 14:07:00 +08006503 }
6504
Gilles Peskine449bd832023-01-11 14:50:10 +01006505 MBEDTLS_SSL_DEBUG_BUF(3, "premaster secret",
6506 handshake->premaster,
6507 handshake->pmslen);
Jerry Yu2a7b5ac2022-02-17 14:07:00 +08006508
Gilles Peskine449bd832023-01-11 14:50:10 +01006509 mbedtls_platform_zeroize(handshake->premaster,
6510 sizeof(handshake->premaster));
Jerry Yu2a7b5ac2022-02-17 14:07:00 +08006511 }
6512
Gilles Peskine449bd832023-01-11 14:50:10 +01006513 return 0;
Jerry Yu2a7b5ac2022-02-17 14:07:00 +08006514}
6515
Gilles Peskine449bd832023-01-11 14:50:10 +01006516int mbedtls_ssl_derive_keys(mbedtls_ssl_context *ssl)
Jerry Yud62f87e2022-02-17 14:09:02 +08006517{
6518 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
6519 const mbedtls_ssl_ciphersuite_t * const ciphersuite_info =
6520 ssl->handshake->ciphersuite_info;
6521
Gilles Peskine449bd832023-01-11 14:50:10 +01006522 MBEDTLS_SSL_DEBUG_MSG(2, ("=> derive keys"));
Jerry Yud62f87e2022-02-17 14:09:02 +08006523
6524 /* Set PRF, calc_verify and calc_finished function pointers */
Gilles Peskine449bd832023-01-11 14:50:10 +01006525 ret = ssl_set_handshake_prfs(ssl->handshake,
6526 ciphersuite_info->mac);
6527 if (ret != 0) {
6528 MBEDTLS_SSL_DEBUG_RET(1, "ssl_set_handshake_prfs", ret);
6529 return ret;
Jerry Yud62f87e2022-02-17 14:09:02 +08006530 }
6531
6532 /* Compute master secret if needed */
Gilles Peskine449bd832023-01-11 14:50:10 +01006533 ret = ssl_compute_master(ssl->handshake,
6534 ssl->session_negotiate->master,
6535 ssl);
6536 if (ret != 0) {
6537 MBEDTLS_SSL_DEBUG_RET(1, "ssl_compute_master", ret);
6538 return ret;
Jerry Yud62f87e2022-02-17 14:09:02 +08006539 }
6540
6541 /* Swap the client and server random values:
6542 * - MS derivation wanted client+server (RFC 5246 8.1)
6543 * - key derivation wants server+client (RFC 5246 6.3) */
6544 {
6545 unsigned char tmp[64];
Gilles Peskine449bd832023-01-11 14:50:10 +01006546 memcpy(tmp, ssl->handshake->randbytes, 64);
6547 memcpy(ssl->handshake->randbytes, tmp + 32, 32);
6548 memcpy(ssl->handshake->randbytes + 32, tmp, 32);
6549 mbedtls_platform_zeroize(tmp, sizeof(tmp));
Jerry Yud62f87e2022-02-17 14:09:02 +08006550 }
6551
6552 /* Populate transform structure */
Gilles Peskine449bd832023-01-11 14:50:10 +01006553 ret = ssl_tls12_populate_transform(ssl->transform_negotiate,
6554 ssl->session_negotiate->ciphersuite,
6555 ssl->session_negotiate->master,
Neil Armstrongf2c82f02022-04-05 11:16:53 +02006556#if defined(MBEDTLS_SSL_SOME_SUITES_USE_CBC_ETM)
Gilles Peskine449bd832023-01-11 14:50:10 +01006557 ssl->session_negotiate->encrypt_then_mac,
Neil Armstrongf2c82f02022-04-05 11:16:53 +02006558#endif /* MBEDTLS_SSL_SOME_SUITES_USE_CBC_ETM */
Gilles Peskine449bd832023-01-11 14:50:10 +01006559 ssl->handshake->tls_prf,
6560 ssl->handshake->randbytes,
6561 ssl->tls_version,
6562 ssl->conf->endpoint,
6563 ssl);
6564 if (ret != 0) {
6565 MBEDTLS_SSL_DEBUG_RET(1, "ssl_tls12_populate_transform", ret);
6566 return ret;
Jerry Yud62f87e2022-02-17 14:09:02 +08006567 }
6568
6569 /* We no longer need Server/ClientHello.random values */
Gilles Peskine449bd832023-01-11 14:50:10 +01006570 mbedtls_platform_zeroize(ssl->handshake->randbytes,
6571 sizeof(ssl->handshake->randbytes));
Jerry Yud62f87e2022-02-17 14:09:02 +08006572
Gilles Peskine449bd832023-01-11 14:50:10 +01006573 MBEDTLS_SSL_DEBUG_MSG(2, ("<= derive keys"));
Jerry Yud62f87e2022-02-17 14:09:02 +08006574
Gilles Peskine449bd832023-01-11 14:50:10 +01006575 return 0;
Jerry Yud62f87e2022-02-17 14:09:02 +08006576}
Jerry Yu8392e0d2022-02-17 14:10:24 +08006577
Gilles Peskine449bd832023-01-11 14:50:10 +01006578int mbedtls_ssl_set_calc_verify_md(mbedtls_ssl_context *ssl, int md)
Ronald Cron4dcbca92022-03-07 10:21:40 +01006579{
Gilles Peskine449bd832023-01-11 14:50:10 +01006580 switch (md) {
Andrzej Kurek25f27152022-08-17 16:09:31 -04006581#if defined(MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Ronald Cron4dcbca92022-03-07 10:21:40 +01006582 case MBEDTLS_SSL_HASH_SHA384:
6583 ssl->handshake->calc_verify = ssl_calc_verify_tls_sha384;
6584 break;
6585#endif
Andrzej Kurek25f27152022-08-17 16:09:31 -04006586#if defined(MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Ronald Cron4dcbca92022-03-07 10:21:40 +01006587 case MBEDTLS_SSL_HASH_SHA256:
6588 ssl->handshake->calc_verify = ssl_calc_verify_tls_sha256;
6589 break;
6590#endif
6591 default:
Gilles Peskine449bd832023-01-11 14:50:10 +01006592 return -1;
Ronald Cron4dcbca92022-03-07 10:21:40 +01006593 }
Andrzej Kurekeabeb302022-10-17 07:52:51 -04006594#if !defined(MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA) && \
6595 !defined(MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
6596 (void) ssl;
6597#endif
Gilles Peskine449bd832023-01-11 14:50:10 +01006598 return 0;
Ronald Cron4dcbca92022-03-07 10:21:40 +01006599}
6600
Andrzej Kurek25f27152022-08-17 16:09:31 -04006601#if defined(MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Manuel Pégourié-Gonnard226aa152023-02-05 09:46:59 +01006602int ssl_calc_verify_tls_sha256(const mbedtls_ssl_context *ssl,
6603 unsigned char *hash,
6604 size_t *hlen)
Jerry Yu8392e0d2022-02-17 14:10:24 +08006605{
6606#if defined(MBEDTLS_USE_PSA_CRYPTO)
6607 size_t hash_size;
6608 psa_status_t status;
6609 psa_hash_operation_t sha256_psa = psa_hash_operation_init();
6610
Gilles Peskine449bd832023-01-11 14:50:10 +01006611 MBEDTLS_SSL_DEBUG_MSG(2, ("=> PSA calc verify sha256"));
6612 status = psa_hash_clone(&ssl->handshake->fin_sha256_psa, &sha256_psa);
6613 if (status != PSA_SUCCESS) {
Manuel Pégourié-Gonnardb9b564e2023-02-06 10:06:04 +01006614 goto exit;
Jerry Yu8392e0d2022-02-17 14:10:24 +08006615 }
6616
Gilles Peskine449bd832023-01-11 14:50:10 +01006617 status = psa_hash_finish(&sha256_psa, hash, 32, &hash_size);
6618 if (status != PSA_SUCCESS) {
Manuel Pégourié-Gonnardb9b564e2023-02-06 10:06:04 +01006619 goto exit;
Jerry Yu8392e0d2022-02-17 14:10:24 +08006620 }
6621
6622 *hlen = 32;
Gilles Peskine449bd832023-01-11 14:50:10 +01006623 MBEDTLS_SSL_DEBUG_BUF(3, "PSA calculated verify result", hash, *hlen);
6624 MBEDTLS_SSL_DEBUG_MSG(2, ("<= PSA calc verify"));
Manuel Pégourié-Gonnardb9b564e2023-02-06 10:06:04 +01006625
6626exit:
6627 psa_hash_abort(&sha256_psa);
6628 return mbedtls_md_error_from_psa(status);
Jerry Yu8392e0d2022-02-17 14:10:24 +08006629#else
Manuel Pégourié-Gonnardb9b564e2023-02-06 10:06:04 +01006630 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnardf057ecf2023-02-24 13:19:17 +01006631 mbedtls_md_context_t sha256;
Jerry Yu8392e0d2022-02-17 14:10:24 +08006632
Manuel Pégourié-Gonnardf057ecf2023-02-24 13:19:17 +01006633 mbedtls_md_init(&sha256);
Jerry Yu8392e0d2022-02-17 14:10:24 +08006634
Gilles Peskine449bd832023-01-11 14:50:10 +01006635 MBEDTLS_SSL_DEBUG_MSG(2, ("=> calc verify sha256"));
Jerry Yu8392e0d2022-02-17 14:10:24 +08006636
Manuel Pégourié-Gonnardf057ecf2023-02-24 13:19:17 +01006637 ret = mbedtls_md_setup(&sha256, mbedtls_md_info_from_type(MBEDTLS_MD_SHA256), 0);
6638 if (ret != 0) {
6639 goto exit;
6640 }
6641 ret = mbedtls_md_clone(&sha256, &ssl->handshake->fin_sha256);
6642 if (ret != 0) {
6643 goto exit;
6644 }
Manuel Pégourié-Gonnardb9b564e2023-02-06 10:06:04 +01006645
Manuel Pégourié-Gonnardf057ecf2023-02-24 13:19:17 +01006646 ret = mbedtls_md_finish(&sha256, hash);
Manuel Pégourié-Gonnardb9b564e2023-02-06 10:06:04 +01006647 if (ret != 0) {
6648 goto exit;
6649 }
Jerry Yu8392e0d2022-02-17 14:10:24 +08006650
6651 *hlen = 32;
6652
Gilles Peskine449bd832023-01-11 14:50:10 +01006653 MBEDTLS_SSL_DEBUG_BUF(3, "calculated verify result", hash, *hlen);
6654 MBEDTLS_SSL_DEBUG_MSG(2, ("<= calc verify"));
Jerry Yu8392e0d2022-02-17 14:10:24 +08006655
Manuel Pégourié-Gonnardb9b564e2023-02-06 10:06:04 +01006656exit:
Manuel Pégourié-Gonnardf057ecf2023-02-24 13:19:17 +01006657 mbedtls_md_free(&sha256);
Manuel Pégourié-Gonnard8e176f72023-02-09 10:33:54 +01006658 return ret;
Jerry Yu8392e0d2022-02-17 14:10:24 +08006659#endif /* MBEDTLS_USE_PSA_CRYPTO */
Jerry Yu8392e0d2022-02-17 14:10:24 +08006660}
Andrzej Kurek25f27152022-08-17 16:09:31 -04006661#endif /* MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA */
Jerry Yu8392e0d2022-02-17 14:10:24 +08006662
Andrzej Kurek25f27152022-08-17 16:09:31 -04006663#if defined(MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Manuel Pégourié-Gonnard226aa152023-02-05 09:46:59 +01006664int ssl_calc_verify_tls_sha384(const mbedtls_ssl_context *ssl,
6665 unsigned char *hash,
6666 size_t *hlen)
Jerry Yuc1cb3842022-02-17 14:13:48 +08006667{
6668#if defined(MBEDTLS_USE_PSA_CRYPTO)
6669 size_t hash_size;
6670 psa_status_t status;
6671 psa_hash_operation_t sha384_psa = psa_hash_operation_init();
6672
Gilles Peskine449bd832023-01-11 14:50:10 +01006673 MBEDTLS_SSL_DEBUG_MSG(2, ("=> PSA calc verify sha384"));
6674 status = psa_hash_clone(&ssl->handshake->fin_sha384_psa, &sha384_psa);
6675 if (status != PSA_SUCCESS) {
Manuel Pégourié-Gonnardb9b564e2023-02-06 10:06:04 +01006676 goto exit;
Jerry Yuc1cb3842022-02-17 14:13:48 +08006677 }
6678
Gilles Peskine449bd832023-01-11 14:50:10 +01006679 status = psa_hash_finish(&sha384_psa, hash, 48, &hash_size);
6680 if (status != PSA_SUCCESS) {
Manuel Pégourié-Gonnardb9b564e2023-02-06 10:06:04 +01006681 goto exit;
Jerry Yuc1cb3842022-02-17 14:13:48 +08006682 }
6683
6684 *hlen = 48;
Gilles Peskine449bd832023-01-11 14:50:10 +01006685 MBEDTLS_SSL_DEBUG_BUF(3, "PSA calculated verify result", hash, *hlen);
6686 MBEDTLS_SSL_DEBUG_MSG(2, ("<= PSA calc verify"));
Manuel Pégourié-Gonnardb9b564e2023-02-06 10:06:04 +01006687
6688exit:
6689 psa_hash_abort(&sha384_psa);
6690 return mbedtls_md_error_from_psa(status);
Jerry Yuc1cb3842022-02-17 14:13:48 +08006691#else
Manuel Pégourié-Gonnardb9b564e2023-02-06 10:06:04 +01006692 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnardf057ecf2023-02-24 13:19:17 +01006693 mbedtls_md_context_t sha512;
Jerry Yuc1cb3842022-02-17 14:13:48 +08006694
Manuel Pégourié-Gonnardf057ecf2023-02-24 13:19:17 +01006695 mbedtls_md_init(&sha512);
Jerry Yuc1cb3842022-02-17 14:13:48 +08006696
Gilles Peskine449bd832023-01-11 14:50:10 +01006697 MBEDTLS_SSL_DEBUG_MSG(2, ("=> calc verify sha384"));
Jerry Yuc1cb3842022-02-17 14:13:48 +08006698
Manuel Pégourié-Gonnardf057ecf2023-02-24 13:19:17 +01006699 ret = mbedtls_md_setup(&sha512, mbedtls_md_info_from_type(MBEDTLS_MD_SHA384), 0);
6700 if (ret != 0) {
6701 goto exit;
6702 }
6703 ret = mbedtls_md_clone(&sha512, &ssl->handshake->fin_sha384);
6704 if (ret != 0) {
6705 goto exit;
6706 }
Manuel Pégourié-Gonnardb9b564e2023-02-06 10:06:04 +01006707
Manuel Pégourié-Gonnardf057ecf2023-02-24 13:19:17 +01006708 ret = mbedtls_md_finish(&sha512, hash);
Manuel Pégourié-Gonnardb9b564e2023-02-06 10:06:04 +01006709 if (ret != 0) {
6710 goto exit;
6711 }
Jerry Yuc1cb3842022-02-17 14:13:48 +08006712
6713 *hlen = 48;
6714
Gilles Peskine449bd832023-01-11 14:50:10 +01006715 MBEDTLS_SSL_DEBUG_BUF(3, "calculated verify result", hash, *hlen);
6716 MBEDTLS_SSL_DEBUG_MSG(2, ("<= calc verify"));
Jerry Yuc1cb3842022-02-17 14:13:48 +08006717
Manuel Pégourié-Gonnardb9b564e2023-02-06 10:06:04 +01006718exit:
Manuel Pégourié-Gonnardf057ecf2023-02-24 13:19:17 +01006719 mbedtls_md_free(&sha512);
Manuel Pégourié-Gonnardb9b564e2023-02-06 10:06:04 +01006720 return ret;
Jerry Yuc1cb3842022-02-17 14:13:48 +08006721#endif /* MBEDTLS_USE_PSA_CRYPTO */
Jerry Yuc1cb3842022-02-17 14:13:48 +08006722}
Andrzej Kurek25f27152022-08-17 16:09:31 -04006723#endif /* MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA */
Jerry Yuc1cb3842022-02-17 14:13:48 +08006724
Neil Armstrong80f6f322022-05-03 17:56:38 +02006725#if !defined(MBEDTLS_USE_PSA_CRYPTO) && \
6726 defined(MBEDTLS_KEY_EXCHANGE_SOME_PSK_ENABLED)
Gilles Peskine449bd832023-01-11 14:50:10 +01006727int mbedtls_ssl_psk_derive_premaster(mbedtls_ssl_context *ssl, mbedtls_key_exchange_type_t key_ex)
Jerry Yuce3dca42022-02-17 14:16:37 +08006728{
6729 unsigned char *p = ssl->handshake->premaster;
Gilles Peskine449bd832023-01-11 14:50:10 +01006730 unsigned char *end = p + sizeof(ssl->handshake->premaster);
Jerry Yuce3dca42022-02-17 14:16:37 +08006731 const unsigned char *psk = NULL;
6732 size_t psk_len = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +01006733 int psk_ret = mbedtls_ssl_get_psk(ssl, &psk, &psk_len);
Jerry Yuce3dca42022-02-17 14:16:37 +08006734
Gilles Peskine449bd832023-01-11 14:50:10 +01006735 if (psk_ret == MBEDTLS_ERR_SSL_PRIVATE_KEY_REQUIRED) {
Jerry Yuce3dca42022-02-17 14:16:37 +08006736 /*
6737 * This should never happen because the existence of a PSK is always
Przemek Stekielb293aaa2022-04-19 12:22:38 +02006738 * checked before calling this function.
6739 *
6740 * The exception is opaque DHE-PSK. For DHE-PSK fill premaster with
Przemek Stekiel8abcee92022-04-28 09:16:28 +02006741 * the shared secret without PSK.
Jerry Yuce3dca42022-02-17 14:16:37 +08006742 */
Gilles Peskine449bd832023-01-11 14:50:10 +01006743 if (key_ex != MBEDTLS_KEY_EXCHANGE_DHE_PSK) {
6744 MBEDTLS_SSL_DEBUG_MSG(1, ("should never happen"));
6745 return MBEDTLS_ERR_SSL_INTERNAL_ERROR;
Przemek Stekielb293aaa2022-04-19 12:22:38 +02006746 }
Jerry Yuce3dca42022-02-17 14:16:37 +08006747 }
6748
6749 /*
6750 * PMS = struct {
6751 * opaque other_secret<0..2^16-1>;
6752 * opaque psk<0..2^16-1>;
6753 * };
6754 * with "other_secret" depending on the particular key exchange
6755 */
6756#if defined(MBEDTLS_KEY_EXCHANGE_PSK_ENABLED)
Gilles Peskine449bd832023-01-11 14:50:10 +01006757 if (key_ex == MBEDTLS_KEY_EXCHANGE_PSK) {
6758 if (end - p < 2) {
6759 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
6760 }
Jerry Yuce3dca42022-02-17 14:16:37 +08006761
Gilles Peskine449bd832023-01-11 14:50:10 +01006762 MBEDTLS_PUT_UINT16_BE(psk_len, p, 0);
Jerry Yuce3dca42022-02-17 14:16:37 +08006763 p += 2;
6764
Gilles Peskine449bd832023-01-11 14:50:10 +01006765 if (end < p || (size_t) (end - p) < psk_len) {
6766 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
6767 }
Jerry Yuce3dca42022-02-17 14:16:37 +08006768
Gilles Peskine449bd832023-01-11 14:50:10 +01006769 memset(p, 0, psk_len);
Jerry Yuce3dca42022-02-17 14:16:37 +08006770 p += psk_len;
Gilles Peskine449bd832023-01-11 14:50:10 +01006771 } else
Jerry Yuce3dca42022-02-17 14:16:37 +08006772#endif /* MBEDTLS_KEY_EXCHANGE_PSK_ENABLED */
6773#if defined(MBEDTLS_KEY_EXCHANGE_RSA_PSK_ENABLED)
Gilles Peskine449bd832023-01-11 14:50:10 +01006774 if (key_ex == MBEDTLS_KEY_EXCHANGE_RSA_PSK) {
Jerry Yuce3dca42022-02-17 14:16:37 +08006775 /*
6776 * other_secret already set by the ClientKeyExchange message,
6777 * and is 48 bytes long
6778 */
Gilles Peskine449bd832023-01-11 14:50:10 +01006779 if (end - p < 2) {
6780 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
6781 }
Jerry Yuce3dca42022-02-17 14:16:37 +08006782
6783 *p++ = 0;
6784 *p++ = 48;
6785 p += 48;
Gilles Peskine449bd832023-01-11 14:50:10 +01006786 } else
Jerry Yuce3dca42022-02-17 14:16:37 +08006787#endif /* MBEDTLS_KEY_EXCHANGE_RSA_PSK_ENABLED */
6788#if defined(MBEDTLS_KEY_EXCHANGE_DHE_PSK_ENABLED)
Gilles Peskine449bd832023-01-11 14:50:10 +01006789 if (key_ex == MBEDTLS_KEY_EXCHANGE_DHE_PSK) {
Jerry Yuce3dca42022-02-17 14:16:37 +08006790 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
6791 size_t len;
6792
6793 /* Write length only when we know the actual value */
Gilles Peskine449bd832023-01-11 14:50:10 +01006794 if ((ret = mbedtls_dhm_calc_secret(&ssl->handshake->dhm_ctx,
6795 p + 2, end - (p + 2), &len,
6796 ssl->conf->f_rng, ssl->conf->p_rng)) != 0) {
6797 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_dhm_calc_secret", ret);
6798 return ret;
Jerry Yuce3dca42022-02-17 14:16:37 +08006799 }
Gilles Peskine449bd832023-01-11 14:50:10 +01006800 MBEDTLS_PUT_UINT16_BE(len, p, 0);
Jerry Yuce3dca42022-02-17 14:16:37 +08006801 p += 2 + len;
6802
Gilles Peskine449bd832023-01-11 14:50:10 +01006803 MBEDTLS_SSL_DEBUG_MPI(3, "DHM: K ", &ssl->handshake->dhm_ctx.K);
6804 } else
Jerry Yuce3dca42022-02-17 14:16:37 +08006805#endif /* MBEDTLS_KEY_EXCHANGE_DHE_PSK_ENABLED */
Neil Armstrong80f6f322022-05-03 17:56:38 +02006806#if defined(MBEDTLS_KEY_EXCHANGE_ECDHE_PSK_ENABLED)
Gilles Peskine449bd832023-01-11 14:50:10 +01006807 if (key_ex == MBEDTLS_KEY_EXCHANGE_ECDHE_PSK) {
Jerry Yuce3dca42022-02-17 14:16:37 +08006808 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
6809 size_t zlen;
6810
Gilles Peskine449bd832023-01-11 14:50:10 +01006811 if ((ret = mbedtls_ecdh_calc_secret(&ssl->handshake->ecdh_ctx, &zlen,
6812 p + 2, end - (p + 2),
6813 ssl->conf->f_rng, ssl->conf->p_rng)) != 0) {
6814 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ecdh_calc_secret", ret);
6815 return ret;
Jerry Yuce3dca42022-02-17 14:16:37 +08006816 }
6817
Gilles Peskine449bd832023-01-11 14:50:10 +01006818 MBEDTLS_PUT_UINT16_BE(zlen, p, 0);
Jerry Yuce3dca42022-02-17 14:16:37 +08006819 p += 2 + zlen;
6820
Gilles Peskine449bd832023-01-11 14:50:10 +01006821 MBEDTLS_SSL_DEBUG_ECDH(3, &ssl->handshake->ecdh_ctx,
6822 MBEDTLS_DEBUG_ECDH_Z);
6823 } else
Neil Armstrong80f6f322022-05-03 17:56:38 +02006824#endif /* MBEDTLS_KEY_EXCHANGE_ECDHE_PSK_ENABLED */
Jerry Yuce3dca42022-02-17 14:16:37 +08006825 {
Gilles Peskine449bd832023-01-11 14:50:10 +01006826 MBEDTLS_SSL_DEBUG_MSG(1, ("should never happen"));
6827 return MBEDTLS_ERR_SSL_INTERNAL_ERROR;
Jerry Yuce3dca42022-02-17 14:16:37 +08006828 }
6829
6830 /* opaque psk<0..2^16-1>; */
Gilles Peskine449bd832023-01-11 14:50:10 +01006831 if (end - p < 2) {
6832 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
6833 }
Jerry Yuce3dca42022-02-17 14:16:37 +08006834
Gilles Peskine449bd832023-01-11 14:50:10 +01006835 MBEDTLS_PUT_UINT16_BE(psk_len, p, 0);
Jerry Yuce3dca42022-02-17 14:16:37 +08006836 p += 2;
6837
Gilles Peskine449bd832023-01-11 14:50:10 +01006838 if (end < p || (size_t) (end - p) < psk_len) {
6839 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
6840 }
Jerry Yuce3dca42022-02-17 14:16:37 +08006841
Gilles Peskine449bd832023-01-11 14:50:10 +01006842 memcpy(p, psk, psk_len);
Jerry Yuce3dca42022-02-17 14:16:37 +08006843 p += psk_len;
6844
6845 ssl->handshake->pmslen = p - ssl->handshake->premaster;
6846
Gilles Peskine449bd832023-01-11 14:50:10 +01006847 return 0;
Jerry Yuce3dca42022-02-17 14:16:37 +08006848}
Neil Armstrong80f6f322022-05-03 17:56:38 +02006849#endif /* !MBEDTLS_USE_PSA_CRYPTO && MBEDTLS_KEY_EXCHANGE_SOME_PSK_ENABLED */
Jerry Yuc2c673d2022-02-17 14:20:39 +08006850
6851#if defined(MBEDTLS_SSL_SRV_C) && defined(MBEDTLS_SSL_RENEGOTIATION)
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02006852MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01006853static int ssl_write_hello_request(mbedtls_ssl_context *ssl);
Jerry Yuc2c673d2022-02-17 14:20:39 +08006854
6855#if defined(MBEDTLS_SSL_PROTO_DTLS)
Gilles Peskine449bd832023-01-11 14:50:10 +01006856int mbedtls_ssl_resend_hello_request(mbedtls_ssl_context *ssl)
Jerry Yuc2c673d2022-02-17 14:20:39 +08006857{
6858 /* If renegotiation is not enforced, retransmit until we would reach max
6859 * timeout if we were using the usual handshake doubling scheme */
Gilles Peskine449bd832023-01-11 14:50:10 +01006860 if (ssl->conf->renego_max_records < 0) {
Jerry Yuc2c673d2022-02-17 14:20:39 +08006861 uint32_t ratio = ssl->conf->hs_timeout_max / ssl->conf->hs_timeout_min + 1;
6862 unsigned char doublings = 1;
6863
Gilles Peskine449bd832023-01-11 14:50:10 +01006864 while (ratio != 0) {
Jerry Yuc2c673d2022-02-17 14:20:39 +08006865 ++doublings;
6866 ratio >>= 1;
6867 }
6868
Gilles Peskine449bd832023-01-11 14:50:10 +01006869 if (++ssl->renego_records_seen > doublings) {
6870 MBEDTLS_SSL_DEBUG_MSG(2, ("no longer retransmitting hello request"));
6871 return 0;
Jerry Yuc2c673d2022-02-17 14:20:39 +08006872 }
6873 }
6874
Gilles Peskine449bd832023-01-11 14:50:10 +01006875 return ssl_write_hello_request(ssl);
Jerry Yuc2c673d2022-02-17 14:20:39 +08006876}
6877#endif
6878#endif /* MBEDTLS_SSL_SRV_C && MBEDTLS_SSL_RENEGOTIATION */
Jerry Yud9526692022-02-17 14:23:47 +08006879
Jerry Yud9526692022-02-17 14:23:47 +08006880/*
6881 * Handshake functions
6882 */
6883#if !defined(MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED)
6884/* No certificate support -> dummy functions */
Gilles Peskine449bd832023-01-11 14:50:10 +01006885int mbedtls_ssl_write_certificate(mbedtls_ssl_context *ssl)
Jerry Yud9526692022-02-17 14:23:47 +08006886{
6887 const mbedtls_ssl_ciphersuite_t *ciphersuite_info =
6888 ssl->handshake->ciphersuite_info;
6889
Gilles Peskine449bd832023-01-11 14:50:10 +01006890 MBEDTLS_SSL_DEBUG_MSG(2, ("=> write certificate"));
Jerry Yud9526692022-02-17 14:23:47 +08006891
Gilles Peskine449bd832023-01-11 14:50:10 +01006892 if (!mbedtls_ssl_ciphersuite_uses_srv_cert(ciphersuite_info)) {
6893 MBEDTLS_SSL_DEBUG_MSG(2, ("<= skip write certificate"));
Jerry Yud9526692022-02-17 14:23:47 +08006894 ssl->state++;
Gilles Peskine449bd832023-01-11 14:50:10 +01006895 return 0;
Jerry Yud9526692022-02-17 14:23:47 +08006896 }
6897
Gilles Peskine449bd832023-01-11 14:50:10 +01006898 MBEDTLS_SSL_DEBUG_MSG(1, ("should never happen"));
6899 return MBEDTLS_ERR_SSL_INTERNAL_ERROR;
Jerry Yud9526692022-02-17 14:23:47 +08006900}
6901
Gilles Peskine449bd832023-01-11 14:50:10 +01006902int mbedtls_ssl_parse_certificate(mbedtls_ssl_context *ssl)
Jerry Yud9526692022-02-17 14:23:47 +08006903{
6904 const mbedtls_ssl_ciphersuite_t *ciphersuite_info =
6905 ssl->handshake->ciphersuite_info;
6906
Gilles Peskine449bd832023-01-11 14:50:10 +01006907 MBEDTLS_SSL_DEBUG_MSG(2, ("=> parse certificate"));
Jerry Yud9526692022-02-17 14:23:47 +08006908
Gilles Peskine449bd832023-01-11 14:50:10 +01006909 if (!mbedtls_ssl_ciphersuite_uses_srv_cert(ciphersuite_info)) {
6910 MBEDTLS_SSL_DEBUG_MSG(2, ("<= skip parse certificate"));
Jerry Yud9526692022-02-17 14:23:47 +08006911 ssl->state++;
Gilles Peskine449bd832023-01-11 14:50:10 +01006912 return 0;
Jerry Yud9526692022-02-17 14:23:47 +08006913 }
6914
Gilles Peskine449bd832023-01-11 14:50:10 +01006915 MBEDTLS_SSL_DEBUG_MSG(1, ("should never happen"));
6916 return MBEDTLS_ERR_SSL_INTERNAL_ERROR;
Jerry Yud9526692022-02-17 14:23:47 +08006917}
6918
6919#else /* MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED */
6920/* Some certificate support -> implement write and parse */
6921
Gilles Peskine449bd832023-01-11 14:50:10 +01006922int mbedtls_ssl_write_certificate(mbedtls_ssl_context *ssl)
Jerry Yud9526692022-02-17 14:23:47 +08006923{
6924 int ret = MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
6925 size_t i, n;
6926 const mbedtls_x509_crt *crt;
6927 const mbedtls_ssl_ciphersuite_t *ciphersuite_info =
6928 ssl->handshake->ciphersuite_info;
6929
Gilles Peskine449bd832023-01-11 14:50:10 +01006930 MBEDTLS_SSL_DEBUG_MSG(2, ("=> write certificate"));
Jerry Yud9526692022-02-17 14:23:47 +08006931
Gilles Peskine449bd832023-01-11 14:50:10 +01006932 if (!mbedtls_ssl_ciphersuite_uses_srv_cert(ciphersuite_info)) {
6933 MBEDTLS_SSL_DEBUG_MSG(2, ("<= skip write certificate"));
Jerry Yud9526692022-02-17 14:23:47 +08006934 ssl->state++;
Gilles Peskine449bd832023-01-11 14:50:10 +01006935 return 0;
Jerry Yud9526692022-02-17 14:23:47 +08006936 }
6937
6938#if defined(MBEDTLS_SSL_CLI_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01006939 if (ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT) {
6940 if (ssl->handshake->client_auth == 0) {
6941 MBEDTLS_SSL_DEBUG_MSG(2, ("<= skip write certificate"));
Jerry Yud9526692022-02-17 14:23:47 +08006942 ssl->state++;
Gilles Peskine449bd832023-01-11 14:50:10 +01006943 return 0;
Jerry Yud9526692022-02-17 14:23:47 +08006944 }
6945 }
6946#endif /* MBEDTLS_SSL_CLI_C */
6947#if defined(MBEDTLS_SSL_SRV_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01006948 if (ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER) {
6949 if (mbedtls_ssl_own_cert(ssl) == NULL) {
Jerry Yud9526692022-02-17 14:23:47 +08006950 /* Should never happen because we shouldn't have picked the
6951 * ciphersuite if we don't have a certificate. */
Gilles Peskine449bd832023-01-11 14:50:10 +01006952 return MBEDTLS_ERR_SSL_INTERNAL_ERROR;
Jerry Yud9526692022-02-17 14:23:47 +08006953 }
6954 }
6955#endif
6956
Gilles Peskine449bd832023-01-11 14:50:10 +01006957 MBEDTLS_SSL_DEBUG_CRT(3, "own certificate", mbedtls_ssl_own_cert(ssl));
Jerry Yud9526692022-02-17 14:23:47 +08006958
6959 /*
6960 * 0 . 0 handshake type
6961 * 1 . 3 handshake length
6962 * 4 . 6 length of all certs
6963 * 7 . 9 length of cert. 1
6964 * 10 . n-1 peer certificate
6965 * n . n+2 length of cert. 2
6966 * n+3 . ... upper level cert, etc.
6967 */
6968 i = 7;
Gilles Peskine449bd832023-01-11 14:50:10 +01006969 crt = mbedtls_ssl_own_cert(ssl);
Jerry Yud9526692022-02-17 14:23:47 +08006970
Gilles Peskine449bd832023-01-11 14:50:10 +01006971 while (crt != NULL) {
Jerry Yud9526692022-02-17 14:23:47 +08006972 n = crt->raw.len;
Gilles Peskine449bd832023-01-11 14:50:10 +01006973 if (n > MBEDTLS_SSL_OUT_CONTENT_LEN - 3 - i) {
6974 MBEDTLS_SSL_DEBUG_MSG(1, ("certificate too large, %" MBEDTLS_PRINTF_SIZET
6975 " > %" MBEDTLS_PRINTF_SIZET,
6976 i + 3 + n, (size_t) MBEDTLS_SSL_OUT_CONTENT_LEN));
6977 return MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL;
Jerry Yud9526692022-02-17 14:23:47 +08006978 }
6979
Gilles Peskine449bd832023-01-11 14:50:10 +01006980 ssl->out_msg[i] = MBEDTLS_BYTE_2(n);
6981 ssl->out_msg[i + 1] = MBEDTLS_BYTE_1(n);
6982 ssl->out_msg[i + 2] = MBEDTLS_BYTE_0(n);
Jerry Yud9526692022-02-17 14:23:47 +08006983
Gilles Peskine449bd832023-01-11 14:50:10 +01006984 i += 3; memcpy(ssl->out_msg + i, crt->raw.p, n);
Jerry Yud9526692022-02-17 14:23:47 +08006985 i += n; crt = crt->next;
6986 }
6987
Gilles Peskine449bd832023-01-11 14:50:10 +01006988 ssl->out_msg[4] = MBEDTLS_BYTE_2(i - 7);
6989 ssl->out_msg[5] = MBEDTLS_BYTE_1(i - 7);
6990 ssl->out_msg[6] = MBEDTLS_BYTE_0(i - 7);
Jerry Yud9526692022-02-17 14:23:47 +08006991
6992 ssl->out_msglen = i;
6993 ssl->out_msgtype = MBEDTLS_SSL_MSG_HANDSHAKE;
6994 ssl->out_msg[0] = MBEDTLS_SSL_HS_CERTIFICATE;
6995
6996 ssl->state++;
6997
Gilles Peskine449bd832023-01-11 14:50:10 +01006998 if ((ret = mbedtls_ssl_write_handshake_msg(ssl)) != 0) {
6999 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ssl_write_handshake_msg", ret);
7000 return ret;
Jerry Yud9526692022-02-17 14:23:47 +08007001 }
7002
Gilles Peskine449bd832023-01-11 14:50:10 +01007003 MBEDTLS_SSL_DEBUG_MSG(2, ("<= write certificate"));
Jerry Yud9526692022-02-17 14:23:47 +08007004
Gilles Peskine449bd832023-01-11 14:50:10 +01007005 return ret;
Jerry Yud9526692022-02-17 14:23:47 +08007006}
7007
7008#if defined(MBEDTLS_SSL_RENEGOTIATION) && defined(MBEDTLS_SSL_CLI_C)
7009
7010#if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02007011MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01007012static int ssl_check_peer_crt_unchanged(mbedtls_ssl_context *ssl,
7013 unsigned char *crt_buf,
7014 size_t crt_buf_len)
Jerry Yud9526692022-02-17 14:23:47 +08007015{
7016 mbedtls_x509_crt const * const peer_crt = ssl->session->peer_cert;
7017
Gilles Peskine449bd832023-01-11 14:50:10 +01007018 if (peer_crt == NULL) {
7019 return -1;
7020 }
Jerry Yud9526692022-02-17 14:23:47 +08007021
Gilles Peskine449bd832023-01-11 14:50:10 +01007022 if (peer_crt->raw.len != crt_buf_len) {
7023 return -1;
7024 }
Jerry Yud9526692022-02-17 14:23:47 +08007025
Gilles Peskine449bd832023-01-11 14:50:10 +01007026 return memcmp(peer_crt->raw.p, crt_buf, peer_crt->raw.len);
Jerry Yud9526692022-02-17 14:23:47 +08007027}
7028#else /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02007029MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01007030static int ssl_check_peer_crt_unchanged(mbedtls_ssl_context *ssl,
7031 unsigned char *crt_buf,
7032 size_t crt_buf_len)
Jerry Yud9526692022-02-17 14:23:47 +08007033{
7034 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
7035 unsigned char const * const peer_cert_digest =
7036 ssl->session->peer_cert_digest;
7037 mbedtls_md_type_t const peer_cert_digest_type =
7038 ssl->session->peer_cert_digest_type;
7039 mbedtls_md_info_t const * const digest_info =
Gilles Peskine449bd832023-01-11 14:50:10 +01007040 mbedtls_md_info_from_type(peer_cert_digest_type);
Jerry Yud9526692022-02-17 14:23:47 +08007041 unsigned char tmp_digest[MBEDTLS_SSL_PEER_CERT_DIGEST_MAX_LEN];
7042 size_t digest_len;
7043
Gilles Peskine449bd832023-01-11 14:50:10 +01007044 if (peer_cert_digest == NULL || digest_info == NULL) {
7045 return -1;
7046 }
Jerry Yud9526692022-02-17 14:23:47 +08007047
Gilles Peskine449bd832023-01-11 14:50:10 +01007048 digest_len = mbedtls_md_get_size(digest_info);
7049 if (digest_len > MBEDTLS_SSL_PEER_CERT_DIGEST_MAX_LEN) {
7050 return -1;
7051 }
Jerry Yud9526692022-02-17 14:23:47 +08007052
Gilles Peskine449bd832023-01-11 14:50:10 +01007053 ret = mbedtls_md(digest_info, crt_buf, crt_buf_len, tmp_digest);
7054 if (ret != 0) {
7055 return -1;
7056 }
Jerry Yud9526692022-02-17 14:23:47 +08007057
Gilles Peskine449bd832023-01-11 14:50:10 +01007058 return memcmp(tmp_digest, peer_cert_digest, digest_len);
Jerry Yud9526692022-02-17 14:23:47 +08007059}
7060#endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
7061#endif /* MBEDTLS_SSL_RENEGOTIATION && MBEDTLS_SSL_CLI_C */
7062
7063/*
7064 * Once the certificate message is read, parse it into a cert chain and
7065 * perform basic checks, but leave actual verification to the caller
7066 */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02007067MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01007068static int ssl_parse_certificate_chain(mbedtls_ssl_context *ssl,
7069 mbedtls_x509_crt *chain)
Jerry Yud9526692022-02-17 14:23:47 +08007070{
7071 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
7072#if defined(MBEDTLS_SSL_RENEGOTIATION) && defined(MBEDTLS_SSL_CLI_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01007073 int crt_cnt = 0;
Jerry Yud9526692022-02-17 14:23:47 +08007074#endif
7075 size_t i, n;
7076 uint8_t alert;
7077
Gilles Peskine449bd832023-01-11 14:50:10 +01007078 if (ssl->in_msgtype != MBEDTLS_SSL_MSG_HANDSHAKE) {
7079 MBEDTLS_SSL_DEBUG_MSG(1, ("bad certificate message"));
7080 mbedtls_ssl_send_alert_message(ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
7081 MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE);
7082 return MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE;
Jerry Yud9526692022-02-17 14:23:47 +08007083 }
7084
Gilles Peskine449bd832023-01-11 14:50:10 +01007085 if (ssl->in_msg[0] != MBEDTLS_SSL_HS_CERTIFICATE) {
7086 mbedtls_ssl_send_alert_message(ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
7087 MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE);
7088 return MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE;
Jerry Yud9526692022-02-17 14:23:47 +08007089 }
7090
Gilles Peskine449bd832023-01-11 14:50:10 +01007091 if (ssl->in_hslen < mbedtls_ssl_hs_hdr_len(ssl) + 3 + 3) {
7092 MBEDTLS_SSL_DEBUG_MSG(1, ("bad certificate message"));
7093 mbedtls_ssl_send_alert_message(ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
7094 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR);
7095 return MBEDTLS_ERR_SSL_DECODE_ERROR;
Jerry Yud9526692022-02-17 14:23:47 +08007096 }
7097
Gilles Peskine449bd832023-01-11 14:50:10 +01007098 i = mbedtls_ssl_hs_hdr_len(ssl);
Jerry Yud9526692022-02-17 14:23:47 +08007099
7100 /*
7101 * Same message structure as in mbedtls_ssl_write_certificate()
7102 */
Gilles Peskine449bd832023-01-11 14:50:10 +01007103 n = (ssl->in_msg[i+1] << 8) | ssl->in_msg[i+2];
Jerry Yud9526692022-02-17 14:23:47 +08007104
Gilles Peskine449bd832023-01-11 14:50:10 +01007105 if (ssl->in_msg[i] != 0 ||
7106 ssl->in_hslen != n + 3 + mbedtls_ssl_hs_hdr_len(ssl)) {
7107 MBEDTLS_SSL_DEBUG_MSG(1, ("bad certificate message"));
7108 mbedtls_ssl_send_alert_message(ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
7109 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR);
7110 return MBEDTLS_ERR_SSL_DECODE_ERROR;
Jerry Yud9526692022-02-17 14:23:47 +08007111 }
7112
7113 /* Make &ssl->in_msg[i] point to the beginning of the CRT chain. */
7114 i += 3;
7115
7116 /* Iterate through and parse the CRTs in the provided chain. */
Gilles Peskine449bd832023-01-11 14:50:10 +01007117 while (i < ssl->in_hslen) {
Jerry Yud9526692022-02-17 14:23:47 +08007118 /* Check that there's room for the next CRT's length fields. */
Gilles Peskine449bd832023-01-11 14:50:10 +01007119 if (i + 3 > ssl->in_hslen) {
7120 MBEDTLS_SSL_DEBUG_MSG(1, ("bad certificate message"));
7121 mbedtls_ssl_send_alert_message(ssl,
7122 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
7123 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR);
7124 return MBEDTLS_ERR_SSL_DECODE_ERROR;
Jerry Yud9526692022-02-17 14:23:47 +08007125 }
7126 /* In theory, the CRT can be up to 2**24 Bytes, but we don't support
7127 * anything beyond 2**16 ~ 64K. */
Gilles Peskine449bd832023-01-11 14:50:10 +01007128 if (ssl->in_msg[i] != 0) {
7129 MBEDTLS_SSL_DEBUG_MSG(1, ("bad certificate message"));
7130 mbedtls_ssl_send_alert_message(ssl,
7131 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
7132 MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT);
7133 return MBEDTLS_ERR_SSL_BAD_CERTIFICATE;
Jerry Yud9526692022-02-17 14:23:47 +08007134 }
7135
7136 /* Read length of the next CRT in the chain. */
Gilles Peskine449bd832023-01-11 14:50:10 +01007137 n = ((unsigned int) ssl->in_msg[i + 1] << 8)
Jerry Yud9526692022-02-17 14:23:47 +08007138 | (unsigned int) ssl->in_msg[i + 2];
7139 i += 3;
7140
Gilles Peskine449bd832023-01-11 14:50:10 +01007141 if (n < 128 || i + n > ssl->in_hslen) {
7142 MBEDTLS_SSL_DEBUG_MSG(1, ("bad certificate message"));
7143 mbedtls_ssl_send_alert_message(ssl,
7144 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
7145 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR);
7146 return MBEDTLS_ERR_SSL_DECODE_ERROR;
Jerry Yud9526692022-02-17 14:23:47 +08007147 }
7148
7149 /* Check if we're handling the first CRT in the chain. */
7150#if defined(MBEDTLS_SSL_RENEGOTIATION) && defined(MBEDTLS_SSL_CLI_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01007151 if (crt_cnt++ == 0 &&
Jerry Yud9526692022-02-17 14:23:47 +08007152 ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT &&
Gilles Peskine449bd832023-01-11 14:50:10 +01007153 ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS) {
Jerry Yud9526692022-02-17 14:23:47 +08007154 /* During client-side renegotiation, check that the server's
7155 * end-CRTs hasn't changed compared to the initial handshake,
7156 * mitigating the triple handshake attack. On success, reuse
7157 * the original end-CRT instead of parsing it again. */
Gilles Peskine449bd832023-01-11 14:50:10 +01007158 MBEDTLS_SSL_DEBUG_MSG(3, ("Check that peer CRT hasn't changed during renegotiation"));
7159 if (ssl_check_peer_crt_unchanged(ssl,
7160 &ssl->in_msg[i],
7161 n) != 0) {
7162 MBEDTLS_SSL_DEBUG_MSG(1, ("new server cert during renegotiation"));
7163 mbedtls_ssl_send_alert_message(ssl,
7164 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
7165 MBEDTLS_SSL_ALERT_MSG_ACCESS_DENIED);
7166 return MBEDTLS_ERR_SSL_BAD_CERTIFICATE;
Jerry Yud9526692022-02-17 14:23:47 +08007167 }
7168
7169 /* Now we can safely free the original chain. */
Gilles Peskine449bd832023-01-11 14:50:10 +01007170 ssl_clear_peer_cert(ssl->session);
Jerry Yud9526692022-02-17 14:23:47 +08007171 }
7172#endif /* MBEDTLS_SSL_RENEGOTIATION && MBEDTLS_SSL_CLI_C */
7173
7174 /* Parse the next certificate in the chain. */
7175#if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
Gilles Peskine449bd832023-01-11 14:50:10 +01007176 ret = mbedtls_x509_crt_parse_der(chain, ssl->in_msg + i, n);
Jerry Yud9526692022-02-17 14:23:47 +08007177#else
7178 /* If we don't need to store the CRT chain permanently, parse
7179 * it in-place from the input buffer instead of making a copy. */
Gilles Peskine449bd832023-01-11 14:50:10 +01007180 ret = mbedtls_x509_crt_parse_der_nocopy(chain, ssl->in_msg + i, n);
Jerry Yud9526692022-02-17 14:23:47 +08007181#endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
Gilles Peskine449bd832023-01-11 14:50:10 +01007182 switch (ret) {
Jerry Yud9526692022-02-17 14:23:47 +08007183 case 0: /*ok*/
7184 case MBEDTLS_ERR_X509_UNKNOWN_SIG_ALG + MBEDTLS_ERR_OID_NOT_FOUND:
7185 /* Ignore certificate with an unknown algorithm: maybe a
7186 prior certificate was already trusted. */
7187 break;
7188
7189 case MBEDTLS_ERR_X509_ALLOC_FAILED:
7190 alert = MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR;
7191 goto crt_parse_der_failed;
7192
7193 case MBEDTLS_ERR_X509_UNKNOWN_VERSION:
7194 alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT;
7195 goto crt_parse_der_failed;
7196
7197 default:
7198 alert = MBEDTLS_SSL_ALERT_MSG_BAD_CERT;
Gilles Peskine449bd832023-01-11 14:50:10 +01007199crt_parse_der_failed:
7200 mbedtls_ssl_send_alert_message(ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL, alert);
7201 MBEDTLS_SSL_DEBUG_RET(1, " mbedtls_x509_crt_parse_der", ret);
7202 return ret;
Jerry Yud9526692022-02-17 14:23:47 +08007203 }
7204
7205 i += n;
7206 }
7207
Gilles Peskine449bd832023-01-11 14:50:10 +01007208 MBEDTLS_SSL_DEBUG_CRT(3, "peer certificate", chain);
7209 return 0;
Jerry Yud9526692022-02-17 14:23:47 +08007210}
7211
7212#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02007213MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01007214static int ssl_srv_check_client_no_crt_notification(mbedtls_ssl_context *ssl)
Jerry Yud9526692022-02-17 14:23:47 +08007215{
Gilles Peskine449bd832023-01-11 14:50:10 +01007216 if (ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT) {
7217 return -1;
7218 }
Jerry Yud9526692022-02-17 14:23:47 +08007219
Gilles Peskine449bd832023-01-11 14:50:10 +01007220 if (ssl->in_hslen == 3 + mbedtls_ssl_hs_hdr_len(ssl) &&
Jerry Yud9526692022-02-17 14:23:47 +08007221 ssl->in_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE &&
7222 ssl->in_msg[0] == MBEDTLS_SSL_HS_CERTIFICATE &&
Gilles Peskine449bd832023-01-11 14:50:10 +01007223 memcmp(ssl->in_msg + mbedtls_ssl_hs_hdr_len(ssl), "\0\0\0", 3) == 0) {
7224 MBEDTLS_SSL_DEBUG_MSG(1, ("peer has no certificate"));
7225 return 0;
Jerry Yud9526692022-02-17 14:23:47 +08007226 }
Gilles Peskine449bd832023-01-11 14:50:10 +01007227 return -1;
Jerry Yud9526692022-02-17 14:23:47 +08007228}
7229#endif /* MBEDTLS_SSL_SRV_C */
7230
7231/* Check if a certificate message is expected.
7232 * Return either
7233 * - SSL_CERTIFICATE_EXPECTED, or
7234 * - SSL_CERTIFICATE_SKIP
7235 * indicating whether a Certificate message is expected or not.
7236 */
7237#define SSL_CERTIFICATE_EXPECTED 0
7238#define SSL_CERTIFICATE_SKIP 1
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02007239MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01007240static int ssl_parse_certificate_coordinate(mbedtls_ssl_context *ssl,
7241 int authmode)
Jerry Yud9526692022-02-17 14:23:47 +08007242{
7243 const mbedtls_ssl_ciphersuite_t *ciphersuite_info =
7244 ssl->handshake->ciphersuite_info;
7245
Gilles Peskine449bd832023-01-11 14:50:10 +01007246 if (!mbedtls_ssl_ciphersuite_uses_srv_cert(ciphersuite_info)) {
7247 return SSL_CERTIFICATE_SKIP;
7248 }
Jerry Yud9526692022-02-17 14:23:47 +08007249
7250#if defined(MBEDTLS_SSL_SRV_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01007251 if (ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER) {
7252 if (ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_RSA_PSK) {
7253 return SSL_CERTIFICATE_SKIP;
7254 }
Jerry Yud9526692022-02-17 14:23:47 +08007255
Gilles Peskine449bd832023-01-11 14:50:10 +01007256 if (authmode == MBEDTLS_SSL_VERIFY_NONE) {
Jerry Yud9526692022-02-17 14:23:47 +08007257 ssl->session_negotiate->verify_result =
7258 MBEDTLS_X509_BADCERT_SKIP_VERIFY;
Gilles Peskine449bd832023-01-11 14:50:10 +01007259 return SSL_CERTIFICATE_SKIP;
Jerry Yud9526692022-02-17 14:23:47 +08007260 }
7261 }
7262#else
7263 ((void) authmode);
7264#endif /* MBEDTLS_SSL_SRV_C */
7265
Gilles Peskine449bd832023-01-11 14:50:10 +01007266 return SSL_CERTIFICATE_EXPECTED;
Jerry Yud9526692022-02-17 14:23:47 +08007267}
7268
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02007269MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01007270static int ssl_parse_certificate_verify(mbedtls_ssl_context *ssl,
7271 int authmode,
7272 mbedtls_x509_crt *chain,
7273 void *rs_ctx)
Jerry Yud9526692022-02-17 14:23:47 +08007274{
7275 int ret = 0;
7276 const mbedtls_ssl_ciphersuite_t *ciphersuite_info =
7277 ssl->handshake->ciphersuite_info;
7278 int have_ca_chain = 0;
7279
7280 int (*f_vrfy)(void *, mbedtls_x509_crt *, int, uint32_t *);
7281 void *p_vrfy;
7282
Gilles Peskine449bd832023-01-11 14:50:10 +01007283 if (authmode == MBEDTLS_SSL_VERIFY_NONE) {
7284 return 0;
7285 }
Jerry Yud9526692022-02-17 14:23:47 +08007286
Gilles Peskine449bd832023-01-11 14:50:10 +01007287 if (ssl->f_vrfy != NULL) {
7288 MBEDTLS_SSL_DEBUG_MSG(3, ("Use context-specific verification callback"));
Jerry Yud9526692022-02-17 14:23:47 +08007289 f_vrfy = ssl->f_vrfy;
7290 p_vrfy = ssl->p_vrfy;
Gilles Peskine449bd832023-01-11 14:50:10 +01007291 } else {
7292 MBEDTLS_SSL_DEBUG_MSG(3, ("Use configuration-specific verification callback"));
Jerry Yud9526692022-02-17 14:23:47 +08007293 f_vrfy = ssl->conf->f_vrfy;
7294 p_vrfy = ssl->conf->p_vrfy;
7295 }
7296
7297 /*
7298 * Main check: verify certificate
7299 */
7300#if defined(MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK)
Gilles Peskine449bd832023-01-11 14:50:10 +01007301 if (ssl->conf->f_ca_cb != NULL) {
Jerry Yud9526692022-02-17 14:23:47 +08007302 ((void) rs_ctx);
7303 have_ca_chain = 1;
7304
Gilles Peskine449bd832023-01-11 14:50:10 +01007305 MBEDTLS_SSL_DEBUG_MSG(3, ("use CA callback for X.509 CRT verification"));
Jerry Yud9526692022-02-17 14:23:47 +08007306 ret = mbedtls_x509_crt_verify_with_ca_cb(
7307 chain,
7308 ssl->conf->f_ca_cb,
7309 ssl->conf->p_ca_cb,
7310 ssl->conf->cert_profile,
7311 ssl->hostname,
7312 &ssl->session_negotiate->verify_result,
Gilles Peskine449bd832023-01-11 14:50:10 +01007313 f_vrfy, p_vrfy);
7314 } else
Jerry Yud9526692022-02-17 14:23:47 +08007315#endif /* MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK */
7316 {
7317 mbedtls_x509_crt *ca_chain;
7318 mbedtls_x509_crl *ca_crl;
7319
7320#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
Gilles Peskine449bd832023-01-11 14:50:10 +01007321 if (ssl->handshake->sni_ca_chain != NULL) {
Jerry Yud9526692022-02-17 14:23:47 +08007322 ca_chain = ssl->handshake->sni_ca_chain;
7323 ca_crl = ssl->handshake->sni_ca_crl;
Gilles Peskine449bd832023-01-11 14:50:10 +01007324 } else
Jerry Yud9526692022-02-17 14:23:47 +08007325#endif
7326 {
7327 ca_chain = ssl->conf->ca_chain;
7328 ca_crl = ssl->conf->ca_crl;
7329 }
7330
Gilles Peskine449bd832023-01-11 14:50:10 +01007331 if (ca_chain != NULL) {
Jerry Yud9526692022-02-17 14:23:47 +08007332 have_ca_chain = 1;
Gilles Peskine449bd832023-01-11 14:50:10 +01007333 }
Jerry Yud9526692022-02-17 14:23:47 +08007334
7335 ret = mbedtls_x509_crt_verify_restartable(
7336 chain,
7337 ca_chain, ca_crl,
7338 ssl->conf->cert_profile,
7339 ssl->hostname,
7340 &ssl->session_negotiate->verify_result,
Gilles Peskine449bd832023-01-11 14:50:10 +01007341 f_vrfy, p_vrfy, rs_ctx);
Jerry Yud9526692022-02-17 14:23:47 +08007342 }
7343
Gilles Peskine449bd832023-01-11 14:50:10 +01007344 if (ret != 0) {
7345 MBEDTLS_SSL_DEBUG_RET(1, "x509_verify_cert", ret);
Jerry Yud9526692022-02-17 14:23:47 +08007346 }
7347
7348#if defined(MBEDTLS_SSL_ECP_RESTARTABLE_ENABLED)
Gilles Peskine449bd832023-01-11 14:50:10 +01007349 if (ret == MBEDTLS_ERR_ECP_IN_PROGRESS) {
7350 return MBEDTLS_ERR_SSL_CRYPTO_IN_PROGRESS;
7351 }
Jerry Yud9526692022-02-17 14:23:47 +08007352#endif
7353
7354 /*
7355 * Secondary checks: always done, but change 'ret' only if it was 0
7356 */
7357
7358#if defined(MBEDTLS_ECP_C)
7359 {
7360 const mbedtls_pk_context *pk = &chain->pk;
7361
Manuel Pégourié-Gonnard66b0d612022-06-17 10:49:29 +02007362 /* If certificate uses an EC key, make sure the curve is OK.
7363 * This is a public key, so it can't be opaque, so can_do() is a good
7364 * enough check to ensure pk_ec() is safe to use here. */
Gilles Peskine449bd832023-01-11 14:50:10 +01007365 if (mbedtls_pk_can_do(pk, MBEDTLS_PK_ECKEY)) {
Leonid Rozenboim19e59732022-08-08 16:52:38 -07007366 /* and in the unlikely case the above assumption no longer holds
7367 * we are making sure that pk_ec() here does not return a NULL
7368 */
Gilles Peskine449bd832023-01-11 14:50:10 +01007369 const mbedtls_ecp_keypair *ec = mbedtls_pk_ec(*pk);
7370 if (ec == NULL) {
7371 MBEDTLS_SSL_DEBUG_MSG(1, ("mbedtls_pk_ec() returned NULL"));
7372 return MBEDTLS_ERR_SSL_INTERNAL_ERROR;
Leonid Rozenboim19e59732022-08-08 16:52:38 -07007373 }
Jerry Yud9526692022-02-17 14:23:47 +08007374
Gilles Peskine449bd832023-01-11 14:50:10 +01007375 if (mbedtls_ssl_check_curve(ssl, ec->grp.id) != 0) {
Leonid Rozenboim19e59732022-08-08 16:52:38 -07007376 ssl->session_negotiate->verify_result |=
7377 MBEDTLS_X509_BADCERT_BAD_KEY;
7378
Gilles Peskine449bd832023-01-11 14:50:10 +01007379 MBEDTLS_SSL_DEBUG_MSG(1, ("bad certificate (EC key curve)"));
7380 if (ret == 0) {
Leonid Rozenboim19e59732022-08-08 16:52:38 -07007381 ret = MBEDTLS_ERR_SSL_BAD_CERTIFICATE;
Gilles Peskine449bd832023-01-11 14:50:10 +01007382 }
Leonid Rozenboim19e59732022-08-08 16:52:38 -07007383 }
Jerry Yud9526692022-02-17 14:23:47 +08007384 }
7385 }
7386#endif /* MBEDTLS_ECP_C */
7387
Gilles Peskine449bd832023-01-11 14:50:10 +01007388 if (mbedtls_ssl_check_cert_usage(chain,
7389 ciphersuite_info,
7390 !ssl->conf->endpoint,
7391 &ssl->session_negotiate->verify_result) != 0) {
7392 MBEDTLS_SSL_DEBUG_MSG(1, ("bad certificate (usage extensions)"));
7393 if (ret == 0) {
Jerry Yud9526692022-02-17 14:23:47 +08007394 ret = MBEDTLS_ERR_SSL_BAD_CERTIFICATE;
Gilles Peskine449bd832023-01-11 14:50:10 +01007395 }
Jerry Yud9526692022-02-17 14:23:47 +08007396 }
7397
7398 /* mbedtls_x509_crt_verify_with_profile is supposed to report a
7399 * verification failure through MBEDTLS_ERR_X509_CERT_VERIFY_FAILED,
7400 * with details encoded in the verification flags. All other kinds
7401 * of error codes, including those from the user provided f_vrfy
7402 * functions, are treated as fatal and lead to a failure of
7403 * ssl_parse_certificate even if verification was optional. */
Gilles Peskine449bd832023-01-11 14:50:10 +01007404 if (authmode == MBEDTLS_SSL_VERIFY_OPTIONAL &&
7405 (ret == MBEDTLS_ERR_X509_CERT_VERIFY_FAILED ||
7406 ret == MBEDTLS_ERR_SSL_BAD_CERTIFICATE)) {
Jerry Yud9526692022-02-17 14:23:47 +08007407 ret = 0;
7408 }
7409
Gilles Peskine449bd832023-01-11 14:50:10 +01007410 if (have_ca_chain == 0 && authmode == MBEDTLS_SSL_VERIFY_REQUIRED) {
7411 MBEDTLS_SSL_DEBUG_MSG(1, ("got no CA chain"));
Jerry Yud9526692022-02-17 14:23:47 +08007412 ret = MBEDTLS_ERR_SSL_CA_CHAIN_REQUIRED;
7413 }
7414
Gilles Peskine449bd832023-01-11 14:50:10 +01007415 if (ret != 0) {
Jerry Yud9526692022-02-17 14:23:47 +08007416 uint8_t alert;
7417
7418 /* The certificate may have been rejected for several reasons.
7419 Pick one and send the corresponding alert. Which alert to send
7420 may be a subject of debate in some cases. */
Gilles Peskine449bd832023-01-11 14:50:10 +01007421 if (ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_OTHER) {
Jerry Yud9526692022-02-17 14:23:47 +08007422 alert = MBEDTLS_SSL_ALERT_MSG_ACCESS_DENIED;
Gilles Peskine449bd832023-01-11 14:50:10 +01007423 } else if (ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_CN_MISMATCH) {
Jerry Yud9526692022-02-17 14:23:47 +08007424 alert = MBEDTLS_SSL_ALERT_MSG_BAD_CERT;
Gilles Peskine449bd832023-01-11 14:50:10 +01007425 } else if (ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_KEY_USAGE) {
Jerry Yud9526692022-02-17 14:23:47 +08007426 alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT;
Gilles Peskine449bd832023-01-11 14:50:10 +01007427 } else if (ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_EXT_KEY_USAGE) {
Jerry Yud9526692022-02-17 14:23:47 +08007428 alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT;
Gilles Peskine449bd832023-01-11 14:50:10 +01007429 } else if (ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_NS_CERT_TYPE) {
Jerry Yud9526692022-02-17 14:23:47 +08007430 alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT;
Gilles Peskine449bd832023-01-11 14:50:10 +01007431 } else if (ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_BAD_PK) {
Jerry Yud9526692022-02-17 14:23:47 +08007432 alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT;
Gilles Peskine449bd832023-01-11 14:50:10 +01007433 } else if (ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_BAD_KEY) {
Jerry Yud9526692022-02-17 14:23:47 +08007434 alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT;
Gilles Peskine449bd832023-01-11 14:50:10 +01007435 } else if (ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_EXPIRED) {
Jerry Yud9526692022-02-17 14:23:47 +08007436 alert = MBEDTLS_SSL_ALERT_MSG_CERT_EXPIRED;
Gilles Peskine449bd832023-01-11 14:50:10 +01007437 } else if (ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_REVOKED) {
Jerry Yud9526692022-02-17 14:23:47 +08007438 alert = MBEDTLS_SSL_ALERT_MSG_CERT_REVOKED;
Gilles Peskine449bd832023-01-11 14:50:10 +01007439 } else if (ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_NOT_TRUSTED) {
Jerry Yud9526692022-02-17 14:23:47 +08007440 alert = MBEDTLS_SSL_ALERT_MSG_UNKNOWN_CA;
Gilles Peskine449bd832023-01-11 14:50:10 +01007441 } else {
Jerry Yud9526692022-02-17 14:23:47 +08007442 alert = MBEDTLS_SSL_ALERT_MSG_CERT_UNKNOWN;
Gilles Peskine449bd832023-01-11 14:50:10 +01007443 }
7444 mbedtls_ssl_send_alert_message(ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
7445 alert);
Jerry Yud9526692022-02-17 14:23:47 +08007446 }
7447
7448#if defined(MBEDTLS_DEBUG_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01007449 if (ssl->session_negotiate->verify_result != 0) {
7450 MBEDTLS_SSL_DEBUG_MSG(3, ("! Certificate verification flags %08x",
7451 (unsigned int) ssl->session_negotiate->verify_result));
7452 } else {
7453 MBEDTLS_SSL_DEBUG_MSG(3, ("Certificate verification flags clear"));
Jerry Yud9526692022-02-17 14:23:47 +08007454 }
7455#endif /* MBEDTLS_DEBUG_C */
7456
Gilles Peskine449bd832023-01-11 14:50:10 +01007457 return ret;
Jerry Yud9526692022-02-17 14:23:47 +08007458}
7459
7460#if !defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02007461MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01007462static int ssl_remember_peer_crt_digest(mbedtls_ssl_context *ssl,
7463 unsigned char *start, size_t len)
Jerry Yud9526692022-02-17 14:23:47 +08007464{
7465 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
7466 /* Remember digest of the peer's end-CRT. */
7467 ssl->session_negotiate->peer_cert_digest =
Gilles Peskine449bd832023-01-11 14:50:10 +01007468 mbedtls_calloc(1, MBEDTLS_SSL_PEER_CERT_DIGEST_DFL_LEN);
7469 if (ssl->session_negotiate->peer_cert_digest == NULL) {
7470 MBEDTLS_SSL_DEBUG_MSG(1, ("alloc(%d bytes) failed",
7471 MBEDTLS_SSL_PEER_CERT_DIGEST_DFL_LEN));
7472 mbedtls_ssl_send_alert_message(ssl,
7473 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
7474 MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR);
Jerry Yud9526692022-02-17 14:23:47 +08007475
Gilles Peskine449bd832023-01-11 14:50:10 +01007476 return MBEDTLS_ERR_SSL_ALLOC_FAILED;
Jerry Yud9526692022-02-17 14:23:47 +08007477 }
7478
Gilles Peskine449bd832023-01-11 14:50:10 +01007479 ret = mbedtls_md(mbedtls_md_info_from_type(
7480 MBEDTLS_SSL_PEER_CERT_DIGEST_DFL_TYPE),
7481 start, len,
7482 ssl->session_negotiate->peer_cert_digest);
Jerry Yud9526692022-02-17 14:23:47 +08007483
7484 ssl->session_negotiate->peer_cert_digest_type =
7485 MBEDTLS_SSL_PEER_CERT_DIGEST_DFL_TYPE;
7486 ssl->session_negotiate->peer_cert_digest_len =
7487 MBEDTLS_SSL_PEER_CERT_DIGEST_DFL_LEN;
7488
Gilles Peskine449bd832023-01-11 14:50:10 +01007489 return ret;
Jerry Yud9526692022-02-17 14:23:47 +08007490}
7491
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02007492MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01007493static int ssl_remember_peer_pubkey(mbedtls_ssl_context *ssl,
7494 unsigned char *start, size_t len)
Jerry Yud9526692022-02-17 14:23:47 +08007495{
7496 unsigned char *end = start + len;
7497 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
7498
7499 /* Make a copy of the peer's raw public key. */
Gilles Peskine449bd832023-01-11 14:50:10 +01007500 mbedtls_pk_init(&ssl->handshake->peer_pubkey);
7501 ret = mbedtls_pk_parse_subpubkey(&start, end,
7502 &ssl->handshake->peer_pubkey);
7503 if (ret != 0) {
Jerry Yud9526692022-02-17 14:23:47 +08007504 /* We should have parsed the public key before. */
Gilles Peskine449bd832023-01-11 14:50:10 +01007505 return MBEDTLS_ERR_SSL_INTERNAL_ERROR;
Jerry Yud9526692022-02-17 14:23:47 +08007506 }
7507
Gilles Peskine449bd832023-01-11 14:50:10 +01007508 return 0;
Jerry Yud9526692022-02-17 14:23:47 +08007509}
7510#endif /* !MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
7511
Gilles Peskine449bd832023-01-11 14:50:10 +01007512int mbedtls_ssl_parse_certificate(mbedtls_ssl_context *ssl)
Jerry Yud9526692022-02-17 14:23:47 +08007513{
7514 int ret = 0;
7515 int crt_expected;
7516#if defined(MBEDTLS_SSL_SRV_C) && defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
7517 const int authmode = ssl->handshake->sni_authmode != MBEDTLS_SSL_VERIFY_UNSET
7518 ? ssl->handshake->sni_authmode
7519 : ssl->conf->authmode;
7520#else
7521 const int authmode = ssl->conf->authmode;
7522#endif
7523 void *rs_ctx = NULL;
7524 mbedtls_x509_crt *chain = NULL;
7525
Gilles Peskine449bd832023-01-11 14:50:10 +01007526 MBEDTLS_SSL_DEBUG_MSG(2, ("=> parse certificate"));
Jerry Yud9526692022-02-17 14:23:47 +08007527
Gilles Peskine449bd832023-01-11 14:50:10 +01007528 crt_expected = ssl_parse_certificate_coordinate(ssl, authmode);
7529 if (crt_expected == SSL_CERTIFICATE_SKIP) {
7530 MBEDTLS_SSL_DEBUG_MSG(2, ("<= skip parse certificate"));
Jerry Yud9526692022-02-17 14:23:47 +08007531 goto exit;
7532 }
7533
7534#if defined(MBEDTLS_SSL_ECP_RESTARTABLE_ENABLED)
Gilles Peskine449bd832023-01-11 14:50:10 +01007535 if (ssl->handshake->ecrs_enabled &&
7536 ssl->handshake->ecrs_state == ssl_ecrs_crt_verify) {
Jerry Yud9526692022-02-17 14:23:47 +08007537 chain = ssl->handshake->ecrs_peer_cert;
7538 ssl->handshake->ecrs_peer_cert = NULL;
7539 goto crt_verify;
7540 }
7541#endif
7542
Gilles Peskine449bd832023-01-11 14:50:10 +01007543 if ((ret = mbedtls_ssl_read_record(ssl, 1)) != 0) {
Jerry Yud9526692022-02-17 14:23:47 +08007544 /* mbedtls_ssl_read_record may have sent an alert already. We
7545 let it decide whether to alert. */
Gilles Peskine449bd832023-01-11 14:50:10 +01007546 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ssl_read_record", ret);
Jerry Yud9526692022-02-17 14:23:47 +08007547 goto exit;
7548 }
7549
7550#if defined(MBEDTLS_SSL_SRV_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01007551 if (ssl_srv_check_client_no_crt_notification(ssl) == 0) {
Jerry Yud9526692022-02-17 14:23:47 +08007552 ssl->session_negotiate->verify_result = MBEDTLS_X509_BADCERT_MISSING;
7553
Gilles Peskine449bd832023-01-11 14:50:10 +01007554 if (authmode != MBEDTLS_SSL_VERIFY_OPTIONAL) {
Jerry Yud9526692022-02-17 14:23:47 +08007555 ret = MBEDTLS_ERR_SSL_NO_CLIENT_CERTIFICATE;
Gilles Peskine449bd832023-01-11 14:50:10 +01007556 }
Jerry Yud9526692022-02-17 14:23:47 +08007557
7558 goto exit;
7559 }
7560#endif /* MBEDTLS_SSL_SRV_C */
7561
7562 /* Clear existing peer CRT structure in case we tried to
7563 * reuse a session but it failed, and allocate a new one. */
Gilles Peskine449bd832023-01-11 14:50:10 +01007564 ssl_clear_peer_cert(ssl->session_negotiate);
Jerry Yud9526692022-02-17 14:23:47 +08007565
Gilles Peskine449bd832023-01-11 14:50:10 +01007566 chain = mbedtls_calloc(1, sizeof(mbedtls_x509_crt));
7567 if (chain == NULL) {
7568 MBEDTLS_SSL_DEBUG_MSG(1, ("alloc(%" MBEDTLS_PRINTF_SIZET " bytes) failed",
7569 sizeof(mbedtls_x509_crt)));
7570 mbedtls_ssl_send_alert_message(ssl,
7571 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
7572 MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR);
Jerry Yud9526692022-02-17 14:23:47 +08007573
7574 ret = MBEDTLS_ERR_SSL_ALLOC_FAILED;
7575 goto exit;
7576 }
Gilles Peskine449bd832023-01-11 14:50:10 +01007577 mbedtls_x509_crt_init(chain);
Jerry Yud9526692022-02-17 14:23:47 +08007578
Gilles Peskine449bd832023-01-11 14:50:10 +01007579 ret = ssl_parse_certificate_chain(ssl, chain);
7580 if (ret != 0) {
Jerry Yud9526692022-02-17 14:23:47 +08007581 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01007582 }
Jerry Yud9526692022-02-17 14:23:47 +08007583
7584#if defined(MBEDTLS_SSL_ECP_RESTARTABLE_ENABLED)
Gilles Peskine449bd832023-01-11 14:50:10 +01007585 if (ssl->handshake->ecrs_enabled) {
Jerry Yud9526692022-02-17 14:23:47 +08007586 ssl->handshake->ecrs_state = ssl_ecrs_crt_verify;
Gilles Peskine449bd832023-01-11 14:50:10 +01007587 }
Jerry Yud9526692022-02-17 14:23:47 +08007588
7589crt_verify:
Gilles Peskine449bd832023-01-11 14:50:10 +01007590 if (ssl->handshake->ecrs_enabled) {
Jerry Yud9526692022-02-17 14:23:47 +08007591 rs_ctx = &ssl->handshake->ecrs_ctx;
Gilles Peskine449bd832023-01-11 14:50:10 +01007592 }
Jerry Yud9526692022-02-17 14:23:47 +08007593#endif
7594
Gilles Peskine449bd832023-01-11 14:50:10 +01007595 ret = ssl_parse_certificate_verify(ssl, authmode,
7596 chain, rs_ctx);
7597 if (ret != 0) {
Jerry Yud9526692022-02-17 14:23:47 +08007598 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01007599 }
Jerry Yud9526692022-02-17 14:23:47 +08007600
7601#if !defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
7602 {
7603 unsigned char *crt_start, *pk_start;
7604 size_t crt_len, pk_len;
7605
7606 /* We parse the CRT chain without copying, so
7607 * these pointers point into the input buffer,
7608 * and are hence still valid after freeing the
7609 * CRT chain. */
7610
7611 crt_start = chain->raw.p;
7612 crt_len = chain->raw.len;
7613
7614 pk_start = chain->pk_raw.p;
7615 pk_len = chain->pk_raw.len;
7616
7617 /* Free the CRT structures before computing
7618 * digest and copying the peer's public key. */
Gilles Peskine449bd832023-01-11 14:50:10 +01007619 mbedtls_x509_crt_free(chain);
7620 mbedtls_free(chain);
Jerry Yud9526692022-02-17 14:23:47 +08007621 chain = NULL;
7622
Gilles Peskine449bd832023-01-11 14:50:10 +01007623 ret = ssl_remember_peer_crt_digest(ssl, crt_start, crt_len);
7624 if (ret != 0) {
Jerry Yud9526692022-02-17 14:23:47 +08007625 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01007626 }
Jerry Yud9526692022-02-17 14:23:47 +08007627
Gilles Peskine449bd832023-01-11 14:50:10 +01007628 ret = ssl_remember_peer_pubkey(ssl, pk_start, pk_len);
7629 if (ret != 0) {
Jerry Yud9526692022-02-17 14:23:47 +08007630 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01007631 }
Jerry Yud9526692022-02-17 14:23:47 +08007632 }
7633#else /* !MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
7634 /* Pass ownership to session structure. */
7635 ssl->session_negotiate->peer_cert = chain;
7636 chain = NULL;
7637#endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
7638
Gilles Peskine449bd832023-01-11 14:50:10 +01007639 MBEDTLS_SSL_DEBUG_MSG(2, ("<= parse certificate"));
Jerry Yud9526692022-02-17 14:23:47 +08007640
7641exit:
7642
Gilles Peskine449bd832023-01-11 14:50:10 +01007643 if (ret == 0) {
Jerry Yud9526692022-02-17 14:23:47 +08007644 ssl->state++;
Gilles Peskine449bd832023-01-11 14:50:10 +01007645 }
Jerry Yud9526692022-02-17 14:23:47 +08007646
7647#if defined(MBEDTLS_SSL_ECP_RESTARTABLE_ENABLED)
Gilles Peskine449bd832023-01-11 14:50:10 +01007648 if (ret == MBEDTLS_ERR_SSL_CRYPTO_IN_PROGRESS) {
Jerry Yud9526692022-02-17 14:23:47 +08007649 ssl->handshake->ecrs_peer_cert = chain;
7650 chain = NULL;
7651 }
7652#endif
7653
Gilles Peskine449bd832023-01-11 14:50:10 +01007654 if (chain != NULL) {
7655 mbedtls_x509_crt_free(chain);
7656 mbedtls_free(chain);
Jerry Yud9526692022-02-17 14:23:47 +08007657 }
7658
Gilles Peskine449bd832023-01-11 14:50:10 +01007659 return ret;
Jerry Yud9526692022-02-17 14:23:47 +08007660}
7661#endif /* MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED */
7662
Andrzej Kurek25f27152022-08-17 16:09:31 -04007663#if defined(MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Manuel Pégourié-Gonnard226aa152023-02-05 09:46:59 +01007664static int ssl_calc_finished_tls_sha256(
Gilles Peskine449bd832023-01-11 14:50:10 +01007665 mbedtls_ssl_context *ssl, unsigned char *buf, int from)
Jerry Yu615bd6f2022-02-17 14:25:15 +08007666{
7667 int len = 12;
7668 const char *sender;
7669 unsigned char padbuf[32];
7670#if defined(MBEDTLS_USE_PSA_CRYPTO)
7671 size_t hash_size;
7672 psa_hash_operation_t sha256_psa = PSA_HASH_OPERATION_INIT;
7673 psa_status_t status;
7674#else
Manuel Pégourié-Gonnarde1a4caa2023-02-06 10:14:25 +01007675 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnardf057ecf2023-02-24 13:19:17 +01007676 mbedtls_md_context_t sha256;
Jerry Yu615bd6f2022-02-17 14:25:15 +08007677#endif
7678
7679 mbedtls_ssl_session *session = ssl->session_negotiate;
Gilles Peskine449bd832023-01-11 14:50:10 +01007680 if (!session) {
Jerry Yu615bd6f2022-02-17 14:25:15 +08007681 session = ssl->session;
Gilles Peskine449bd832023-01-11 14:50:10 +01007682 }
Jerry Yu615bd6f2022-02-17 14:25:15 +08007683
Gilles Peskine449bd832023-01-11 14:50:10 +01007684 sender = (from == MBEDTLS_SSL_IS_CLIENT)
Jerry Yu615bd6f2022-02-17 14:25:15 +08007685 ? "client finished"
7686 : "server finished";
7687
7688#if defined(MBEDTLS_USE_PSA_CRYPTO)
7689 sha256_psa = psa_hash_operation_init();
7690
Gilles Peskine449bd832023-01-11 14:50:10 +01007691 MBEDTLS_SSL_DEBUG_MSG(2, ("=> calc PSA finished tls sha256"));
Jerry Yu615bd6f2022-02-17 14:25:15 +08007692
Gilles Peskine449bd832023-01-11 14:50:10 +01007693 status = psa_hash_clone(&ssl->handshake->fin_sha256_psa, &sha256_psa);
7694 if (status != PSA_SUCCESS) {
Manuel Pégourié-Gonnarde1a4caa2023-02-06 10:14:25 +01007695 goto exit;
Jerry Yu615bd6f2022-02-17 14:25:15 +08007696 }
7697
Gilles Peskine449bd832023-01-11 14:50:10 +01007698 status = psa_hash_finish(&sha256_psa, padbuf, sizeof(padbuf), &hash_size);
7699 if (status != PSA_SUCCESS) {
Manuel Pégourié-Gonnarde1a4caa2023-02-06 10:14:25 +01007700 goto exit;
Jerry Yu615bd6f2022-02-17 14:25:15 +08007701 }
Gilles Peskine449bd832023-01-11 14:50:10 +01007702 MBEDTLS_SSL_DEBUG_BUF(3, "PSA calculated padbuf", padbuf, 32);
Jerry Yu615bd6f2022-02-17 14:25:15 +08007703#else
7704
Manuel Pégourié-Gonnardf057ecf2023-02-24 13:19:17 +01007705 mbedtls_md_init(&sha256);
Jerry Yu615bd6f2022-02-17 14:25:15 +08007706
Gilles Peskine449bd832023-01-11 14:50:10 +01007707 MBEDTLS_SSL_DEBUG_MSG(2, ("=> calc finished tls sha256"));
Jerry Yu615bd6f2022-02-17 14:25:15 +08007708
Manuel Pégourié-Gonnardf057ecf2023-02-24 13:19:17 +01007709 ret = mbedtls_md_setup(&sha256, mbedtls_md_info_from_type(MBEDTLS_MD_SHA256), 0);
7710 if (ret != 0) {
7711 goto exit;
7712 }
7713 ret = mbedtls_md_clone(&sha256, &ssl->handshake->fin_sha256);
7714 if (ret != 0) {
7715 goto exit;
7716 }
Jerry Yu615bd6f2022-02-17 14:25:15 +08007717
7718 /*
7719 * TLSv1.2:
7720 * hash = PRF( master, finished_label,
7721 * Hash( handshake ) )[0.11]
7722 */
7723
Manuel Pégourié-Gonnardf057ecf2023-02-24 13:19:17 +01007724 ret = mbedtls_md_finish(&sha256, padbuf);
Manuel Pégourié-Gonnarde1a4caa2023-02-06 10:14:25 +01007725 if (ret != 0) {
7726 goto exit;
7727 }
Jerry Yu615bd6f2022-02-17 14:25:15 +08007728#endif /* MBEDTLS_USE_PSA_CRYPTO */
7729
Manuel Pégourié-Gonnard0ac71c02023-02-24 12:13:55 +01007730 MBEDTLS_SSL_DEBUG_BUF(4, "finished sha256 output", padbuf, 32);
7731
Gilles Peskine449bd832023-01-11 14:50:10 +01007732 ssl->handshake->tls_prf(session->master, 48, sender,
7733 padbuf, 32, buf, len);
Jerry Yu615bd6f2022-02-17 14:25:15 +08007734
Gilles Peskine449bd832023-01-11 14:50:10 +01007735 MBEDTLS_SSL_DEBUG_BUF(3, "calc finished result", buf, len);
Jerry Yu615bd6f2022-02-17 14:25:15 +08007736
Gilles Peskine449bd832023-01-11 14:50:10 +01007737 mbedtls_platform_zeroize(padbuf, sizeof(padbuf));
Jerry Yu615bd6f2022-02-17 14:25:15 +08007738
Gilles Peskine449bd832023-01-11 14:50:10 +01007739 MBEDTLS_SSL_DEBUG_MSG(2, ("<= calc finished"));
Manuel Pégourié-Gonnarde1a4caa2023-02-06 10:14:25 +01007740
7741exit:
7742#if defined(MBEDTLS_USE_PSA_CRYPTO)
7743 psa_hash_abort(&sha256_psa);
7744 return mbedtls_md_error_from_psa(status);
7745#else
Manuel Pégourié-Gonnardf057ecf2023-02-24 13:19:17 +01007746 mbedtls_md_free(&sha256);
Manuel Pégourié-Gonnarde1a4caa2023-02-06 10:14:25 +01007747 return ret;
7748#endif /* MBEDTLS_USE_PSA_CRYPTO */
Jerry Yu615bd6f2022-02-17 14:25:15 +08007749}
Andrzej Kurekcccb0442022-08-19 03:42:11 -04007750#endif /* MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA*/
Jerry Yu615bd6f2022-02-17 14:25:15 +08007751
Jerry Yub7ba49e2022-02-17 14:25:53 +08007752
Andrzej Kurek25f27152022-08-17 16:09:31 -04007753#if defined(MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Manuel Pégourié-Gonnard226aa152023-02-05 09:46:59 +01007754static int ssl_calc_finished_tls_sha384(
Gilles Peskine449bd832023-01-11 14:50:10 +01007755 mbedtls_ssl_context *ssl, unsigned char *buf, int from)
Jerry Yub7ba49e2022-02-17 14:25:53 +08007756{
7757 int len = 12;
7758 const char *sender;
7759 unsigned char padbuf[48];
7760#if defined(MBEDTLS_USE_PSA_CRYPTO)
7761 size_t hash_size;
7762 psa_hash_operation_t sha384_psa = PSA_HASH_OPERATION_INIT;
7763 psa_status_t status;
7764#else
Manuel Pégourié-Gonnarde1a4caa2023-02-06 10:14:25 +01007765 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnardf057ecf2023-02-24 13:19:17 +01007766 mbedtls_md_context_t sha512;
Jerry Yub7ba49e2022-02-17 14:25:53 +08007767#endif
7768
7769 mbedtls_ssl_session *session = ssl->session_negotiate;
Gilles Peskine449bd832023-01-11 14:50:10 +01007770 if (!session) {
Jerry Yub7ba49e2022-02-17 14:25:53 +08007771 session = ssl->session;
Gilles Peskine449bd832023-01-11 14:50:10 +01007772 }
Jerry Yub7ba49e2022-02-17 14:25:53 +08007773
Gilles Peskine449bd832023-01-11 14:50:10 +01007774 sender = (from == MBEDTLS_SSL_IS_CLIENT)
Jerry Yub7ba49e2022-02-17 14:25:53 +08007775 ? "client finished"
7776 : "server finished";
7777
7778#if defined(MBEDTLS_USE_PSA_CRYPTO)
7779 sha384_psa = psa_hash_operation_init();
7780
Gilles Peskine449bd832023-01-11 14:50:10 +01007781 MBEDTLS_SSL_DEBUG_MSG(2, ("=> calc PSA finished tls sha384"));
Jerry Yub7ba49e2022-02-17 14:25:53 +08007782
Gilles Peskine449bd832023-01-11 14:50:10 +01007783 status = psa_hash_clone(&ssl->handshake->fin_sha384_psa, &sha384_psa);
7784 if (status != PSA_SUCCESS) {
Manuel Pégourié-Gonnarde1a4caa2023-02-06 10:14:25 +01007785 goto exit;
Jerry Yub7ba49e2022-02-17 14:25:53 +08007786 }
7787
Gilles Peskine449bd832023-01-11 14:50:10 +01007788 status = psa_hash_finish(&sha384_psa, padbuf, sizeof(padbuf), &hash_size);
7789 if (status != PSA_SUCCESS) {
Manuel Pégourié-Gonnarde1a4caa2023-02-06 10:14:25 +01007790 goto exit;
Jerry Yub7ba49e2022-02-17 14:25:53 +08007791 }
Gilles Peskine449bd832023-01-11 14:50:10 +01007792 MBEDTLS_SSL_DEBUG_BUF(3, "PSA calculated padbuf", padbuf, 48);
Jerry Yub7ba49e2022-02-17 14:25:53 +08007793#else
Manuel Pégourié-Gonnardf057ecf2023-02-24 13:19:17 +01007794 mbedtls_md_init(&sha512);
Jerry Yub7ba49e2022-02-17 14:25:53 +08007795
Gilles Peskine449bd832023-01-11 14:50:10 +01007796 MBEDTLS_SSL_DEBUG_MSG(2, ("=> calc finished tls sha384"));
Jerry Yub7ba49e2022-02-17 14:25:53 +08007797
Manuel Pégourié-Gonnardf057ecf2023-02-24 13:19:17 +01007798 ret = mbedtls_md_setup(&sha512, mbedtls_md_info_from_type(MBEDTLS_MD_SHA384), 0);
7799 if (ret != 0) {
7800 goto exit;
7801 }
7802 ret = mbedtls_md_clone(&sha512, &ssl->handshake->fin_sha384);
7803 if (ret != 0) {
7804 goto exit;
7805 }
Jerry Yub7ba49e2022-02-17 14:25:53 +08007806
7807 /*
7808 * TLSv1.2:
7809 * hash = PRF( master, finished_label,
7810 * Hash( handshake ) )[0.11]
7811 */
7812
Manuel Pégourié-Gonnardf057ecf2023-02-24 13:19:17 +01007813 ret = mbedtls_md_finish(&sha512, padbuf);
Manuel Pégourié-Gonnarde1a4caa2023-02-06 10:14:25 +01007814 if (ret != 0) {
7815 goto exit;
7816 }
Jerry Yub7ba49e2022-02-17 14:25:53 +08007817#endif
7818
Manuel Pégourié-Gonnard0ac71c02023-02-24 12:13:55 +01007819 MBEDTLS_SSL_DEBUG_BUF(4, "finished sha384 output", padbuf, 48);
7820
Gilles Peskine449bd832023-01-11 14:50:10 +01007821 ssl->handshake->tls_prf(session->master, 48, sender,
7822 padbuf, 48, buf, len);
Jerry Yub7ba49e2022-02-17 14:25:53 +08007823
Gilles Peskine449bd832023-01-11 14:50:10 +01007824 MBEDTLS_SSL_DEBUG_BUF(3, "calc finished result", buf, len);
Jerry Yub7ba49e2022-02-17 14:25:53 +08007825
Gilles Peskine449bd832023-01-11 14:50:10 +01007826 mbedtls_platform_zeroize(padbuf, sizeof(padbuf));
Jerry Yub7ba49e2022-02-17 14:25:53 +08007827
Gilles Peskine449bd832023-01-11 14:50:10 +01007828 MBEDTLS_SSL_DEBUG_MSG(2, ("<= calc finished"));
Manuel Pégourié-Gonnarde1a4caa2023-02-06 10:14:25 +01007829
7830exit:
7831#if defined(MBEDTLS_USE_PSA_CRYPTO)
7832 psa_hash_abort(&sha384_psa);
7833 return mbedtls_md_error_from_psa(status);
7834#else
Manuel Pégourié-Gonnardf057ecf2023-02-24 13:19:17 +01007835 mbedtls_md_free(&sha512);
Manuel Pégourié-Gonnarde1a4caa2023-02-06 10:14:25 +01007836 return ret;
7837#endif /* MBEDTLS_USE_PSA_CRYPTO */
Jerry Yub7ba49e2022-02-17 14:25:53 +08007838}
Andrzej Kurekcccb0442022-08-19 03:42:11 -04007839#endif /* MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA*/
Jerry Yub7ba49e2022-02-17 14:25:53 +08007840
Gilles Peskine449bd832023-01-11 14:50:10 +01007841void mbedtls_ssl_handshake_wrapup_free_hs_transform(mbedtls_ssl_context *ssl)
Jerry Yuaef00152022-02-17 14:27:31 +08007842{
Gilles Peskine449bd832023-01-11 14:50:10 +01007843 MBEDTLS_SSL_DEBUG_MSG(3, ("=> handshake wrapup: final free"));
Jerry Yuaef00152022-02-17 14:27:31 +08007844
7845 /*
7846 * Free our handshake params
7847 */
Gilles Peskine449bd832023-01-11 14:50:10 +01007848 mbedtls_ssl_handshake_free(ssl);
7849 mbedtls_free(ssl->handshake);
Jerry Yuaef00152022-02-17 14:27:31 +08007850 ssl->handshake = NULL;
7851
7852 /*
Shaun Case8b0ecbc2021-12-20 21:14:10 -08007853 * Free the previous transform and switch in the current one
Jerry Yuaef00152022-02-17 14:27:31 +08007854 */
Gilles Peskine449bd832023-01-11 14:50:10 +01007855 if (ssl->transform) {
7856 mbedtls_ssl_transform_free(ssl->transform);
7857 mbedtls_free(ssl->transform);
Jerry Yuaef00152022-02-17 14:27:31 +08007858 }
7859 ssl->transform = ssl->transform_negotiate;
7860 ssl->transform_negotiate = NULL;
7861
Gilles Peskine449bd832023-01-11 14:50:10 +01007862 MBEDTLS_SSL_DEBUG_MSG(3, ("<= handshake wrapup: final free"));
Jerry Yuaef00152022-02-17 14:27:31 +08007863}
7864
Gilles Peskine449bd832023-01-11 14:50:10 +01007865void mbedtls_ssl_handshake_wrapup(mbedtls_ssl_context *ssl)
Jerry Yu2a9fff52022-02-17 14:28:51 +08007866{
7867 int resume = ssl->handshake->resume;
7868
Gilles Peskine449bd832023-01-11 14:50:10 +01007869 MBEDTLS_SSL_DEBUG_MSG(3, ("=> handshake wrapup"));
Jerry Yu2a9fff52022-02-17 14:28:51 +08007870
7871#if defined(MBEDTLS_SSL_RENEGOTIATION)
Gilles Peskine449bd832023-01-11 14:50:10 +01007872 if (ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS) {
Jerry Yu2a9fff52022-02-17 14:28:51 +08007873 ssl->renego_status = MBEDTLS_SSL_RENEGOTIATION_DONE;
7874 ssl->renego_records_seen = 0;
7875 }
7876#endif
7877
7878 /*
7879 * Free the previous session and switch in the current one
7880 */
Gilles Peskine449bd832023-01-11 14:50:10 +01007881 if (ssl->session) {
Jerry Yu2a9fff52022-02-17 14:28:51 +08007882#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
7883 /* RFC 7366 3.1: keep the EtM state */
7884 ssl->session_negotiate->encrypt_then_mac =
Gilles Peskine449bd832023-01-11 14:50:10 +01007885 ssl->session->encrypt_then_mac;
Jerry Yu2a9fff52022-02-17 14:28:51 +08007886#endif
7887
Gilles Peskine449bd832023-01-11 14:50:10 +01007888 mbedtls_ssl_session_free(ssl->session);
7889 mbedtls_free(ssl->session);
Jerry Yu2a9fff52022-02-17 14:28:51 +08007890 }
7891 ssl->session = ssl->session_negotiate;
7892 ssl->session_negotiate = NULL;
7893
7894 /*
7895 * Add cache entry
7896 */
Gilles Peskine449bd832023-01-11 14:50:10 +01007897 if (ssl->conf->f_set_cache != NULL &&
Jerry Yu2a9fff52022-02-17 14:28:51 +08007898 ssl->session->id_len != 0 &&
Gilles Peskine449bd832023-01-11 14:50:10 +01007899 resume == 0) {
7900 if (ssl->conf->f_set_cache(ssl->conf->p_cache,
7901 ssl->session->id,
7902 ssl->session->id_len,
7903 ssl->session) != 0) {
7904 MBEDTLS_SSL_DEBUG_MSG(1, ("cache did not store session"));
7905 }
Jerry Yu2a9fff52022-02-17 14:28:51 +08007906 }
7907
7908#if defined(MBEDTLS_SSL_PROTO_DTLS)
Gilles Peskine449bd832023-01-11 14:50:10 +01007909 if (ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
7910 ssl->handshake->flight != NULL) {
Jerry Yu2a9fff52022-02-17 14:28:51 +08007911 /* Cancel handshake timer */
Gilles Peskine449bd832023-01-11 14:50:10 +01007912 mbedtls_ssl_set_timer(ssl, 0);
Jerry Yu2a9fff52022-02-17 14:28:51 +08007913
7914 /* Keep last flight around in case we need to resend it:
7915 * we need the handshake and transform structures for that */
Gilles Peskine449bd832023-01-11 14:50:10 +01007916 MBEDTLS_SSL_DEBUG_MSG(3, ("skip freeing handshake and transform"));
7917 } else
Jerry Yu2a9fff52022-02-17 14:28:51 +08007918#endif
Gilles Peskine449bd832023-01-11 14:50:10 +01007919 mbedtls_ssl_handshake_wrapup_free_hs_transform(ssl);
Jerry Yu2a9fff52022-02-17 14:28:51 +08007920
Jerry Yu5ed73ff2022-10-27 13:08:42 +08007921 ssl->state = MBEDTLS_SSL_HANDSHAKE_OVER;
Jerry Yu2a9fff52022-02-17 14:28:51 +08007922
Gilles Peskine449bd832023-01-11 14:50:10 +01007923 MBEDTLS_SSL_DEBUG_MSG(3, ("<= handshake wrapup"));
Jerry Yu2a9fff52022-02-17 14:28:51 +08007924}
7925
Gilles Peskine449bd832023-01-11 14:50:10 +01007926int mbedtls_ssl_write_finished(mbedtls_ssl_context *ssl)
Jerry Yu3c8e47b2022-02-17 14:30:01 +08007927{
7928 int ret, hash_len;
7929
Gilles Peskine449bd832023-01-11 14:50:10 +01007930 MBEDTLS_SSL_DEBUG_MSG(2, ("=> write finished"));
Jerry Yu3c8e47b2022-02-17 14:30:01 +08007931
Gilles Peskine449bd832023-01-11 14:50:10 +01007932 mbedtls_ssl_update_out_pointers(ssl, ssl->transform_negotiate);
Jerry Yu3c8e47b2022-02-17 14:30:01 +08007933
Manuel Pégourié-Gonnardb8b07aa2023-02-06 00:34:21 +01007934 ret = ssl->handshake->calc_finished(ssl, ssl->out_msg + 4, ssl->conf->endpoint);
7935 if (ret != 0) {
7936 MBEDTLS_SSL_DEBUG_RET(1, "calc_finished", ret);
7937 }
Jerry Yu3c8e47b2022-02-17 14:30:01 +08007938
7939 /*
7940 * RFC 5246 7.4.9 (Page 63) says 12 is the default length and ciphersuites
7941 * may define some other value. Currently (early 2016), no defined
7942 * ciphersuite does this (and this is unlikely to change as activity has
7943 * moved to TLS 1.3 now) so we can keep the hardcoded 12 here.
7944 */
7945 hash_len = 12;
7946
7947#if defined(MBEDTLS_SSL_RENEGOTIATION)
7948 ssl->verify_data_len = hash_len;
Gilles Peskine449bd832023-01-11 14:50:10 +01007949 memcpy(ssl->own_verify_data, ssl->out_msg + 4, hash_len);
Jerry Yu3c8e47b2022-02-17 14:30:01 +08007950#endif
7951
7952 ssl->out_msglen = 4 + hash_len;
7953 ssl->out_msgtype = MBEDTLS_SSL_MSG_HANDSHAKE;
7954 ssl->out_msg[0] = MBEDTLS_SSL_HS_FINISHED;
7955
7956 /*
7957 * In case of session resuming, invert the client and server
7958 * ChangeCipherSpec messages order.
7959 */
Gilles Peskine449bd832023-01-11 14:50:10 +01007960 if (ssl->handshake->resume != 0) {
Jerry Yu3c8e47b2022-02-17 14:30:01 +08007961#if defined(MBEDTLS_SSL_CLI_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01007962 if (ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT) {
Jerry Yu3c8e47b2022-02-17 14:30:01 +08007963 ssl->state = MBEDTLS_SSL_HANDSHAKE_WRAPUP;
Gilles Peskine449bd832023-01-11 14:50:10 +01007964 }
Jerry Yu3c8e47b2022-02-17 14:30:01 +08007965#endif
7966#if defined(MBEDTLS_SSL_SRV_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01007967 if (ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER) {
Jerry Yu3c8e47b2022-02-17 14:30:01 +08007968 ssl->state = MBEDTLS_SSL_CLIENT_CHANGE_CIPHER_SPEC;
Gilles Peskine449bd832023-01-11 14:50:10 +01007969 }
Jerry Yu3c8e47b2022-02-17 14:30:01 +08007970#endif
Gilles Peskine449bd832023-01-11 14:50:10 +01007971 } else {
Jerry Yu3c8e47b2022-02-17 14:30:01 +08007972 ssl->state++;
Gilles Peskine449bd832023-01-11 14:50:10 +01007973 }
Jerry Yu3c8e47b2022-02-17 14:30:01 +08007974
7975 /*
7976 * Switch to our negotiated transform and session parameters for outbound
7977 * data.
7978 */
Gilles Peskine449bd832023-01-11 14:50:10 +01007979 MBEDTLS_SSL_DEBUG_MSG(3, ("switching to new transform spec for outbound data"));
Jerry Yu3c8e47b2022-02-17 14:30:01 +08007980
7981#if defined(MBEDTLS_SSL_PROTO_DTLS)
Gilles Peskine449bd832023-01-11 14:50:10 +01007982 if (ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM) {
Jerry Yu3c8e47b2022-02-17 14:30:01 +08007983 unsigned char i;
7984
7985 /* Remember current epoch settings for resending */
7986 ssl->handshake->alt_transform_out = ssl->transform_out;
Gilles Peskine449bd832023-01-11 14:50:10 +01007987 memcpy(ssl->handshake->alt_out_ctr, ssl->cur_out_ctr,
7988 sizeof(ssl->handshake->alt_out_ctr));
Jerry Yu3c8e47b2022-02-17 14:30:01 +08007989
7990 /* Set sequence_number to zero */
Gilles Peskine449bd832023-01-11 14:50:10 +01007991 memset(&ssl->cur_out_ctr[2], 0, sizeof(ssl->cur_out_ctr) - 2);
Jerry Yu3c8e47b2022-02-17 14:30:01 +08007992
7993
7994 /* Increment epoch */
Gilles Peskine449bd832023-01-11 14:50:10 +01007995 for (i = 2; i > 0; i--) {
7996 if (++ssl->cur_out_ctr[i - 1] != 0) {
Jerry Yu3c8e47b2022-02-17 14:30:01 +08007997 break;
Gilles Peskine449bd832023-01-11 14:50:10 +01007998 }
7999 }
Jerry Yu3c8e47b2022-02-17 14:30:01 +08008000
8001 /* The loop goes to its end iff the counter is wrapping */
Gilles Peskine449bd832023-01-11 14:50:10 +01008002 if (i == 0) {
8003 MBEDTLS_SSL_DEBUG_MSG(1, ("DTLS epoch would wrap"));
8004 return MBEDTLS_ERR_SSL_COUNTER_WRAPPING;
Jerry Yu3c8e47b2022-02-17 14:30:01 +08008005 }
Gilles Peskine449bd832023-01-11 14:50:10 +01008006 } else
Jerry Yu3c8e47b2022-02-17 14:30:01 +08008007#endif /* MBEDTLS_SSL_PROTO_DTLS */
Gilles Peskine449bd832023-01-11 14:50:10 +01008008 memset(ssl->cur_out_ctr, 0, sizeof(ssl->cur_out_ctr));
Jerry Yu3c8e47b2022-02-17 14:30:01 +08008009
8010 ssl->transform_out = ssl->transform_negotiate;
8011 ssl->session_out = ssl->session_negotiate;
8012
8013#if defined(MBEDTLS_SSL_PROTO_DTLS)
Gilles Peskine449bd832023-01-11 14:50:10 +01008014 if (ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM) {
8015 mbedtls_ssl_send_flight_completed(ssl);
8016 }
Jerry Yu3c8e47b2022-02-17 14:30:01 +08008017#endif
8018
Gilles Peskine449bd832023-01-11 14:50:10 +01008019 if ((ret = mbedtls_ssl_write_handshake_msg(ssl)) != 0) {
8020 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ssl_write_handshake_msg", ret);
8021 return ret;
Jerry Yu3c8e47b2022-02-17 14:30:01 +08008022 }
8023
8024#if defined(MBEDTLS_SSL_PROTO_DTLS)
Gilles Peskine449bd832023-01-11 14:50:10 +01008025 if (ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
8026 (ret = mbedtls_ssl_flight_transmit(ssl)) != 0) {
8027 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ssl_flight_transmit", ret);
8028 return ret;
Jerry Yu3c8e47b2022-02-17 14:30:01 +08008029 }
8030#endif
8031
Gilles Peskine449bd832023-01-11 14:50:10 +01008032 MBEDTLS_SSL_DEBUG_MSG(2, ("<= write finished"));
Jerry Yu3c8e47b2022-02-17 14:30:01 +08008033
Gilles Peskine449bd832023-01-11 14:50:10 +01008034 return 0;
Jerry Yu3c8e47b2022-02-17 14:30:01 +08008035}
8036
Jerry Yu0b3d7c12022-02-17 14:30:51 +08008037#define SSL_MAX_HASH_LEN 12
8038
Gilles Peskine449bd832023-01-11 14:50:10 +01008039int mbedtls_ssl_parse_finished(mbedtls_ssl_context *ssl)
Jerry Yu0b3d7c12022-02-17 14:30:51 +08008040{
8041 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
8042 unsigned int hash_len = 12;
8043 unsigned char buf[SSL_MAX_HASH_LEN];
8044
Gilles Peskine449bd832023-01-11 14:50:10 +01008045 MBEDTLS_SSL_DEBUG_MSG(2, ("=> parse finished"));
Jerry Yu0b3d7c12022-02-17 14:30:51 +08008046
Manuel Pégourié-Gonnardb8b07aa2023-02-06 00:34:21 +01008047 ret = ssl->handshake->calc_finished(ssl, buf, ssl->conf->endpoint ^ 1);
8048 if (ret != 0) {
8049 MBEDTLS_SSL_DEBUG_RET(1, "calc_finished", ret);
8050 }
Jerry Yu0b3d7c12022-02-17 14:30:51 +08008051
Gilles Peskine449bd832023-01-11 14:50:10 +01008052 if ((ret = mbedtls_ssl_read_record(ssl, 1)) != 0) {
8053 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ssl_read_record", ret);
Jerry Yu0b3d7c12022-02-17 14:30:51 +08008054 goto exit;
8055 }
8056
Gilles Peskine449bd832023-01-11 14:50:10 +01008057 if (ssl->in_msgtype != MBEDTLS_SSL_MSG_HANDSHAKE) {
8058 MBEDTLS_SSL_DEBUG_MSG(1, ("bad finished message"));
8059 mbedtls_ssl_send_alert_message(ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
8060 MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE);
Jerry Yu0b3d7c12022-02-17 14:30:51 +08008061 ret = MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE;
8062 goto exit;
8063 }
8064
Gilles Peskine449bd832023-01-11 14:50:10 +01008065 if (ssl->in_msg[0] != MBEDTLS_SSL_HS_FINISHED) {
8066 mbedtls_ssl_send_alert_message(ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
8067 MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE);
Jerry Yu0b3d7c12022-02-17 14:30:51 +08008068 ret = MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE;
8069 goto exit;
8070 }
8071
Gilles Peskine449bd832023-01-11 14:50:10 +01008072 if (ssl->in_hslen != mbedtls_ssl_hs_hdr_len(ssl) + hash_len) {
8073 MBEDTLS_SSL_DEBUG_MSG(1, ("bad finished message"));
8074 mbedtls_ssl_send_alert_message(ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
8075 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR);
Jerry Yu0b3d7c12022-02-17 14:30:51 +08008076 ret = MBEDTLS_ERR_SSL_DECODE_ERROR;
8077 goto exit;
8078 }
8079
Gilles Peskine449bd832023-01-11 14:50:10 +01008080 if (mbedtls_ct_memcmp(ssl->in_msg + mbedtls_ssl_hs_hdr_len(ssl),
8081 buf, hash_len) != 0) {
8082 MBEDTLS_SSL_DEBUG_MSG(1, ("bad finished message"));
8083 mbedtls_ssl_send_alert_message(ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
8084 MBEDTLS_SSL_ALERT_MSG_DECRYPT_ERROR);
Jerry Yu0b3d7c12022-02-17 14:30:51 +08008085 ret = MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE;
8086 goto exit;
8087 }
8088
8089#if defined(MBEDTLS_SSL_RENEGOTIATION)
8090 ssl->verify_data_len = hash_len;
Gilles Peskine449bd832023-01-11 14:50:10 +01008091 memcpy(ssl->peer_verify_data, buf, hash_len);
Jerry Yu0b3d7c12022-02-17 14:30:51 +08008092#endif
8093
Gilles Peskine449bd832023-01-11 14:50:10 +01008094 if (ssl->handshake->resume != 0) {
Jerry Yu0b3d7c12022-02-17 14:30:51 +08008095#if defined(MBEDTLS_SSL_CLI_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01008096 if (ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT) {
Jerry Yu0b3d7c12022-02-17 14:30:51 +08008097 ssl->state = MBEDTLS_SSL_CLIENT_CHANGE_CIPHER_SPEC;
Gilles Peskine449bd832023-01-11 14:50:10 +01008098 }
Jerry Yu0b3d7c12022-02-17 14:30:51 +08008099#endif
8100#if defined(MBEDTLS_SSL_SRV_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01008101 if (ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER) {
Jerry Yu0b3d7c12022-02-17 14:30:51 +08008102 ssl->state = MBEDTLS_SSL_HANDSHAKE_WRAPUP;
Gilles Peskine449bd832023-01-11 14:50:10 +01008103 }
Jerry Yu0b3d7c12022-02-17 14:30:51 +08008104#endif
Gilles Peskine449bd832023-01-11 14:50:10 +01008105 } else {
Jerry Yu0b3d7c12022-02-17 14:30:51 +08008106 ssl->state++;
Gilles Peskine449bd832023-01-11 14:50:10 +01008107 }
Jerry Yu0b3d7c12022-02-17 14:30:51 +08008108
8109#if defined(MBEDTLS_SSL_PROTO_DTLS)
Gilles Peskine449bd832023-01-11 14:50:10 +01008110 if (ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM) {
8111 mbedtls_ssl_recv_flight_completed(ssl);
8112 }
Jerry Yu0b3d7c12022-02-17 14:30:51 +08008113#endif
8114
Gilles Peskine449bd832023-01-11 14:50:10 +01008115 MBEDTLS_SSL_DEBUG_MSG(2, ("<= parse finished"));
Jerry Yu0b3d7c12022-02-17 14:30:51 +08008116
8117exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01008118 mbedtls_platform_zeroize(buf, hash_len);
8119 return ret;
Jerry Yu0b3d7c12022-02-17 14:30:51 +08008120}
8121
Jerry Yu392112c2022-02-17 14:34:10 +08008122#if defined(MBEDTLS_SSL_CONTEXT_SERIALIZATION)
8123/*
8124 * Helper to get TLS 1.2 PRF from ciphersuite
8125 * (Duplicates bits of logic from ssl_set_handshake_prfs().)
8126 */
Gilles Peskine449bd832023-01-11 14:50:10 +01008127static tls_prf_fn ssl_tls12prf_from_cs(int ciphersuite_id)
Jerry Yu392112c2022-02-17 14:34:10 +08008128{
Jerry Yu392112c2022-02-17 14:34:10 +08008129 const mbedtls_ssl_ciphersuite_t * const ciphersuite_info =
Gilles Peskine449bd832023-01-11 14:50:10 +01008130 mbedtls_ssl_ciphersuite_from_id(ciphersuite_id);
Andrzej Kurek68327742022-10-03 06:18:18 -04008131#if defined(MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Gilles Peskine449bd832023-01-11 14:50:10 +01008132 if (ciphersuite_info != NULL && ciphersuite_info->mac == MBEDTLS_MD_SHA384) {
8133 return tls_prf_sha384;
8134 } else
Jerry Yu392112c2022-02-17 14:34:10 +08008135#endif
Andrzej Kurek894edde2022-09-29 06:31:14 -04008136#if defined(MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
8137 {
Gilles Peskine449bd832023-01-11 14:50:10 +01008138 if (ciphersuite_info != NULL && ciphersuite_info->mac == MBEDTLS_MD_SHA256) {
8139 return tls_prf_sha256;
8140 }
Andrzej Kurek894edde2022-09-29 06:31:14 -04008141 }
8142#endif
8143#if !defined(MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA) && \
8144 !defined(MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
8145 (void) ciphersuite_info;
8146#endif
Andrzej Kurekeabeb302022-10-17 07:52:51 -04008147
Gilles Peskine449bd832023-01-11 14:50:10 +01008148 return NULL;
Jerry Yu392112c2022-02-17 14:34:10 +08008149}
8150#endif /* MBEDTLS_SSL_CONTEXT_SERIALIZATION */
Jerry Yue93ffcd2022-02-17 14:37:06 +08008151
Gilles Peskine449bd832023-01-11 14:50:10 +01008152static mbedtls_tls_prf_types tls_prf_get_type(mbedtls_ssl_tls_prf_cb *tls_prf)
Jerry Yue93ffcd2022-02-17 14:37:06 +08008153{
8154 ((void) tls_prf);
Andrzej Kurek25f27152022-08-17 16:09:31 -04008155#if defined(MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Gilles Peskine449bd832023-01-11 14:50:10 +01008156 if (tls_prf == tls_prf_sha384) {
8157 return MBEDTLS_SSL_TLS_PRF_SHA384;
8158 } else
Jerry Yue93ffcd2022-02-17 14:37:06 +08008159#endif
Andrzej Kurek25f27152022-08-17 16:09:31 -04008160#if defined(MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Gilles Peskine449bd832023-01-11 14:50:10 +01008161 if (tls_prf == tls_prf_sha256) {
8162 return MBEDTLS_SSL_TLS_PRF_SHA256;
8163 } else
Jerry Yue93ffcd2022-02-17 14:37:06 +08008164#endif
Gilles Peskine449bd832023-01-11 14:50:10 +01008165 return MBEDTLS_SSL_TLS_PRF_NONE;
Jerry Yue93ffcd2022-02-17 14:37:06 +08008166}
8167
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008168/*
8169 * Populate a transform structure with session keys and all the other
8170 * necessary information.
8171 *
8172 * Parameters:
8173 * - [in/out]: transform: structure to populate
8174 * [in] must be just initialised with mbedtls_ssl_transform_init()
8175 * [out] fully populated, ready for use by mbedtls_ssl_{en,de}crypt_buf()
8176 * - [in] ciphersuite
8177 * - [in] master
8178 * - [in] encrypt_then_mac
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008179 * - [in] tls_prf: pointer to PRF to use for key derivation
8180 * - [in] randbytes: buffer holding ServerHello.random + ClientHello.random
Glenn Strauss07c64162022-03-14 12:34:51 -04008181 * - [in] tls_version: TLS version
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008182 * - [in] endpoint: client or server
8183 * - [in] ssl: used for:
8184 * - ssl->conf->{f,p}_export_keys
8185 * [in] optionally used for:
8186 * - MBEDTLS_DEBUG_C: ssl->conf->{f,p}_dbg
8187 */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02008188MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01008189static int ssl_tls12_populate_transform(mbedtls_ssl_transform *transform,
8190 int ciphersuite,
8191 const unsigned char master[48],
Neil Armstrongf2c82f02022-04-05 11:16:53 +02008192#if defined(MBEDTLS_SSL_SOME_SUITES_USE_CBC_ETM)
Gilles Peskine449bd832023-01-11 14:50:10 +01008193 int encrypt_then_mac,
Neil Armstrongf2c82f02022-04-05 11:16:53 +02008194#endif /* MBEDTLS_SSL_SOME_SUITES_USE_CBC_ETM */
Gilles Peskine449bd832023-01-11 14:50:10 +01008195 ssl_tls_prf_t tls_prf,
8196 const unsigned char randbytes[64],
8197 mbedtls_ssl_protocol_version tls_version,
8198 unsigned endpoint,
8199 const mbedtls_ssl_context *ssl)
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008200{
8201 int ret = 0;
8202 unsigned char keyblk[256];
8203 unsigned char *key1;
8204 unsigned char *key2;
8205 unsigned char *mac_enc;
8206 unsigned char *mac_dec;
8207 size_t mac_key_len = 0;
8208 size_t iv_copy_len;
8209 size_t keylen;
8210 const mbedtls_ssl_ciphersuite_t *ciphersuite_info;
Neil Armstrong7fea33e2022-04-01 15:40:25 +02008211 mbedtls_ssl_mode_t ssl_mode;
Neil Armstronge4512952022-03-08 09:08:22 +01008212#if !defined(MBEDTLS_USE_PSA_CRYPTO)
Neil Armstronga0eeb7f2022-04-01 17:36:10 +02008213 const mbedtls_cipher_info_t *cipher_info;
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008214 const mbedtls_md_info_t *md_info;
Neil Armstronge4512952022-03-08 09:08:22 +01008215#endif /* !MBEDTLS_USE_PSA_CRYPTO */
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008216
8217#if defined(MBEDTLS_USE_PSA_CRYPTO)
8218 psa_key_type_t key_type;
8219 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
8220 psa_algorithm_t alg;
Neil Armstronge4512952022-03-08 09:08:22 +01008221 psa_algorithm_t mac_alg = 0;
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008222 size_t key_bits;
8223 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
8224#endif
8225
8226#if !defined(MBEDTLS_DEBUG_C) && \
8227 !defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Gilles Peskine449bd832023-01-11 14:50:10 +01008228 if (ssl->f_export_keys == NULL) {
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008229 ssl = NULL; /* make sure we don't use it except for these cases */
8230 (void) ssl;
8231 }
8232#endif
8233
8234 /*
8235 * Some data just needs copying into the structure
8236 */
Neil Armstrongf2c82f02022-04-05 11:16:53 +02008237#if defined(MBEDTLS_SSL_SOME_SUITES_USE_CBC_ETM)
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008238 transform->encrypt_then_mac = encrypt_then_mac;
Neil Armstrongf2c82f02022-04-05 11:16:53 +02008239#endif /* MBEDTLS_SSL_SOME_SUITES_USE_CBC_ETM */
Glenn Strauss07c64162022-03-14 12:34:51 -04008240 transform->tls_version = tls_version;
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008241
8242#if defined(MBEDTLS_SSL_CONTEXT_SERIALIZATION)
Gilles Peskine449bd832023-01-11 14:50:10 +01008243 memcpy(transform->randbytes, randbytes, sizeof(transform->randbytes));
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008244#endif
8245
8246#if defined(MBEDTLS_SSL_PROTO_TLS1_3)
Gilles Peskine449bd832023-01-11 14:50:10 +01008247 if (tls_version == MBEDTLS_SSL_VERSION_TLS1_3) {
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008248 /* At the moment, we keep TLS <= 1.2 and TLS 1.3 transform
8249 * generation separate. This should never happen. */
Gilles Peskine449bd832023-01-11 14:50:10 +01008250 return MBEDTLS_ERR_SSL_INTERNAL_ERROR;
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008251 }
8252#endif /* MBEDTLS_SSL_PROTO_TLS1_3 */
8253
8254 /*
8255 * Get various info structures
8256 */
Gilles Peskine449bd832023-01-11 14:50:10 +01008257 ciphersuite_info = mbedtls_ssl_ciphersuite_from_id(ciphersuite);
8258 if (ciphersuite_info == NULL) {
8259 MBEDTLS_SSL_DEBUG_MSG(1, ("ciphersuite info for %d not found",
8260 ciphersuite));
8261 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008262 }
8263
Neil Armstrongab555e02022-04-04 11:07:59 +02008264 ssl_mode = mbedtls_ssl_get_mode_from_ciphersuite(
Neil Armstrongf2c82f02022-04-05 11:16:53 +02008265#if defined(MBEDTLS_SSL_SOME_SUITES_USE_CBC_ETM)
Gilles Peskine449bd832023-01-11 14:50:10 +01008266 encrypt_then_mac,
Neil Armstrongf2c82f02022-04-05 11:16:53 +02008267#endif /* MBEDTLS_SSL_SOME_SUITES_USE_CBC_ETM */
Gilles Peskine449bd832023-01-11 14:50:10 +01008268 ciphersuite_info);
Neil Armstrong7fea33e2022-04-01 15:40:25 +02008269
Gilles Peskine449bd832023-01-11 14:50:10 +01008270 if (ssl_mode == MBEDTLS_SSL_MODE_AEAD) {
Neil Armstronga0eeb7f2022-04-01 17:36:10 +02008271 transform->taglen =
8272 ciphersuite_info->flags & MBEDTLS_CIPHERSUITE_SHORT_TAG ? 8 : 16;
Gilles Peskine449bd832023-01-11 14:50:10 +01008273 }
Neil Armstronga0eeb7f2022-04-01 17:36:10 +02008274
8275#if defined(MBEDTLS_USE_PSA_CRYPTO)
Gilles Peskine449bd832023-01-11 14:50:10 +01008276 if ((status = mbedtls_ssl_cipher_to_psa(ciphersuite_info->cipher,
8277 transform->taglen,
8278 &alg,
8279 &key_type,
8280 &key_bits)) != PSA_SUCCESS) {
8281 ret = psa_ssl_status_to_mbedtls(status);
8282 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ssl_cipher_to_psa", ret);
Neil Armstronga0eeb7f2022-04-01 17:36:10 +02008283 goto end;
8284 }
8285#else
Gilles Peskine449bd832023-01-11 14:50:10 +01008286 cipher_info = mbedtls_cipher_info_from_type(ciphersuite_info->cipher);
8287 if (cipher_info == NULL) {
8288 MBEDTLS_SSL_DEBUG_MSG(1, ("cipher info for %u not found",
8289 ciphersuite_info->cipher));
8290 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008291 }
Neil Armstronga0eeb7f2022-04-01 17:36:10 +02008292#endif /* MBEDTLS_USE_PSA_CRYPTO */
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008293
Neil Armstronge4512952022-03-08 09:08:22 +01008294#if defined(MBEDTLS_USE_PSA_CRYPTO)
Gilles Peskine449bd832023-01-11 14:50:10 +01008295 mac_alg = mbedtls_hash_info_psa_from_md(ciphersuite_info->mac);
8296 if (mac_alg == 0) {
8297 MBEDTLS_SSL_DEBUG_MSG(1, ("mbedtls_hash_info_psa_from_md for %u not found",
8298 (unsigned) ciphersuite_info->mac));
8299 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
Neil Armstronge4512952022-03-08 09:08:22 +01008300 }
8301#else
Gilles Peskine449bd832023-01-11 14:50:10 +01008302 md_info = mbedtls_md_info_from_type(ciphersuite_info->mac);
8303 if (md_info == NULL) {
8304 MBEDTLS_SSL_DEBUG_MSG(1, ("mbedtls_md info for %u not found",
8305 (unsigned) ciphersuite_info->mac));
8306 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008307 }
Neil Armstronge4512952022-03-08 09:08:22 +01008308#endif /* MBEDTLS_USE_PSA_CRYPTO */
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008309
8310#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
8311 /* Copy own and peer's CID if the use of the CID
8312 * extension has been negotiated. */
Gilles Peskine449bd832023-01-11 14:50:10 +01008313 if (ssl->handshake->cid_in_use == MBEDTLS_SSL_CID_ENABLED) {
8314 MBEDTLS_SSL_DEBUG_MSG(3, ("Copy CIDs into SSL transform"));
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008315
8316 transform->in_cid_len = ssl->own_cid_len;
Gilles Peskine449bd832023-01-11 14:50:10 +01008317 memcpy(transform->in_cid, ssl->own_cid, ssl->own_cid_len);
8318 MBEDTLS_SSL_DEBUG_BUF(3, "Incoming CID", transform->in_cid,
8319 transform->in_cid_len);
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008320
8321 transform->out_cid_len = ssl->handshake->peer_cid_len;
Gilles Peskine449bd832023-01-11 14:50:10 +01008322 memcpy(transform->out_cid, ssl->handshake->peer_cid,
8323 ssl->handshake->peer_cid_len);
8324 MBEDTLS_SSL_DEBUG_BUF(3, "Outgoing CID", transform->out_cid,
8325 transform->out_cid_len);
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008326 }
8327#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
8328
8329 /*
8330 * Compute key block using the PRF
8331 */
Gilles Peskine449bd832023-01-11 14:50:10 +01008332 ret = tls_prf(master, 48, "key expansion", randbytes, 64, keyblk, 256);
8333 if (ret != 0) {
8334 MBEDTLS_SSL_DEBUG_RET(1, "prf", ret);
8335 return ret;
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008336 }
8337
Gilles Peskine449bd832023-01-11 14:50:10 +01008338 MBEDTLS_SSL_DEBUG_MSG(3, ("ciphersuite = %s",
8339 mbedtls_ssl_get_ciphersuite_name(ciphersuite)));
8340 MBEDTLS_SSL_DEBUG_BUF(3, "master secret", master, 48);
8341 MBEDTLS_SSL_DEBUG_BUF(4, "random bytes", randbytes, 64);
8342 MBEDTLS_SSL_DEBUG_BUF(4, "key block", keyblk, 256);
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008343
8344 /*
8345 * Determine the appropriate key, IV and MAC length.
8346 */
8347
Neil Armstronga0eeb7f2022-04-01 17:36:10 +02008348#if defined(MBEDTLS_USE_PSA_CRYPTO)
8349 keylen = PSA_BITS_TO_BYTES(key_bits);
8350#else
Gilles Peskine449bd832023-01-11 14:50:10 +01008351 keylen = mbedtls_cipher_info_get_key_bitlen(cipher_info) / 8;
Neil Armstronga0eeb7f2022-04-01 17:36:10 +02008352#endif
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008353
8354#if defined(MBEDTLS_GCM_C) || \
8355 defined(MBEDTLS_CCM_C) || \
8356 defined(MBEDTLS_CHACHAPOLY_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01008357 if (ssl_mode == MBEDTLS_SSL_MODE_AEAD) {
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008358 size_t explicit_ivlen;
8359
8360 transform->maclen = 0;
8361 mac_key_len = 0;
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008362
8363 /* All modes haves 96-bit IVs, but the length of the static parts vary
8364 * with mode and version:
8365 * - For GCM and CCM in TLS 1.2, there's a static IV of 4 Bytes
8366 * (to be concatenated with a dynamically chosen IV of 8 Bytes)
8367 * - For ChaChaPoly in TLS 1.2, and all modes in TLS 1.3, there's
8368 * a static IV of 12 Bytes (to be XOR'ed with the 8 Byte record
8369 * sequence number).
8370 */
8371 transform->ivlen = 12;
David Horstmann3b2276a2022-10-06 14:49:08 +01008372
8373 int is_chachapoly = 0;
Neil Armstronga0eeb7f2022-04-01 17:36:10 +02008374#if defined(MBEDTLS_USE_PSA_CRYPTO)
Gilles Peskine449bd832023-01-11 14:50:10 +01008375 is_chachapoly = (key_type == PSA_KEY_TYPE_CHACHA20);
Neil Armstronga0eeb7f2022-04-01 17:36:10 +02008376#else
Gilles Peskine449bd832023-01-11 14:50:10 +01008377 is_chachapoly = (mbedtls_cipher_info_get_mode(cipher_info)
8378 == MBEDTLS_MODE_CHACHAPOLY);
Neil Armstronga0eeb7f2022-04-01 17:36:10 +02008379#endif /* MBEDTLS_USE_PSA_CRYPTO */
David Horstmann3b2276a2022-10-06 14:49:08 +01008380
Gilles Peskine449bd832023-01-11 14:50:10 +01008381 if (is_chachapoly) {
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008382 transform->fixed_ivlen = 12;
Gilles Peskine449bd832023-01-11 14:50:10 +01008383 } else {
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008384 transform->fixed_ivlen = 4;
Gilles Peskine449bd832023-01-11 14:50:10 +01008385 }
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008386
8387 /* Minimum length of encrypted record */
8388 explicit_ivlen = transform->ivlen - transform->fixed_ivlen;
8389 transform->minlen = explicit_ivlen + transform->taglen;
Gilles Peskine449bd832023-01-11 14:50:10 +01008390 } else
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008391#endif /* MBEDTLS_GCM_C || MBEDTLS_CCM_C || MBEDTLS_CHACHAPOLY_C */
8392#if defined(MBEDTLS_SSL_SOME_SUITES_USE_MAC)
Gilles Peskine449bd832023-01-11 14:50:10 +01008393 if (ssl_mode == MBEDTLS_SSL_MODE_STREAM ||
Neil Armstrong7fea33e2022-04-01 15:40:25 +02008394 ssl_mode == MBEDTLS_SSL_MODE_CBC ||
Gilles Peskine449bd832023-01-11 14:50:10 +01008395 ssl_mode == MBEDTLS_SSL_MODE_CBC_ETM) {
Neil Armstronge4512952022-03-08 09:08:22 +01008396#if defined(MBEDTLS_USE_PSA_CRYPTO)
Gilles Peskine449bd832023-01-11 14:50:10 +01008397 size_t block_size = PSA_BLOCK_CIPHER_BLOCK_LENGTH(key_type);
Neil Armstronga0eeb7f2022-04-01 17:36:10 +02008398#else
8399 size_t block_size = cipher_info->block_size;
8400#endif /* MBEDTLS_USE_PSA_CRYPTO */
8401
8402#if defined(MBEDTLS_USE_PSA_CRYPTO)
Neil Armstronge4512952022-03-08 09:08:22 +01008403 /* Get MAC length */
8404 mac_key_len = PSA_HASH_LENGTH(mac_alg);
8405#else
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008406 /* Initialize HMAC contexts */
Gilles Peskine449bd832023-01-11 14:50:10 +01008407 if ((ret = mbedtls_md_setup(&transform->md_ctx_enc, md_info, 1)) != 0 ||
8408 (ret = mbedtls_md_setup(&transform->md_ctx_dec, md_info, 1)) != 0) {
8409 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_md_setup", ret);
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008410 goto end;
8411 }
8412
8413 /* Get MAC length */
Gilles Peskine449bd832023-01-11 14:50:10 +01008414 mac_key_len = mbedtls_md_get_size(md_info);
Neil Armstronge4512952022-03-08 09:08:22 +01008415#endif /* MBEDTLS_USE_PSA_CRYPTO */
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008416 transform->maclen = mac_key_len;
8417
8418 /* IV length */
Neil Armstronga0eeb7f2022-04-01 17:36:10 +02008419#if defined(MBEDTLS_USE_PSA_CRYPTO)
Gilles Peskine449bd832023-01-11 14:50:10 +01008420 transform->ivlen = PSA_CIPHER_IV_LENGTH(key_type, alg);
Neil Armstronga0eeb7f2022-04-01 17:36:10 +02008421#else
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008422 transform->ivlen = cipher_info->iv_size;
Neil Armstronga0eeb7f2022-04-01 17:36:10 +02008423#endif /* MBEDTLS_USE_PSA_CRYPTO */
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008424
8425 /* Minimum length */
Gilles Peskine449bd832023-01-11 14:50:10 +01008426 if (ssl_mode == MBEDTLS_SSL_MODE_STREAM) {
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008427 transform->minlen = transform->maclen;
Gilles Peskine449bd832023-01-11 14:50:10 +01008428 } else {
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008429 /*
8430 * GenericBlockCipher:
8431 * 1. if EtM is in use: one block plus MAC
8432 * otherwise: * first multiple of blocklen greater than maclen
8433 * 2. IV
8434 */
8435#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
Gilles Peskine449bd832023-01-11 14:50:10 +01008436 if (ssl_mode == MBEDTLS_SSL_MODE_CBC_ETM) {
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008437 transform->minlen = transform->maclen
Gilles Peskine449bd832023-01-11 14:50:10 +01008438 + block_size;
8439 } else
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008440#endif
8441 {
8442 transform->minlen = transform->maclen
Gilles Peskine449bd832023-01-11 14:50:10 +01008443 + block_size
8444 - transform->maclen % block_size;
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008445 }
8446
Gilles Peskine449bd832023-01-11 14:50:10 +01008447 if (tls_version == MBEDTLS_SSL_VERSION_TLS1_2) {
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008448 transform->minlen += transform->ivlen;
Gilles Peskine449bd832023-01-11 14:50:10 +01008449 } else {
8450 MBEDTLS_SSL_DEBUG_MSG(1, ("should never happen"));
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008451 ret = MBEDTLS_ERR_SSL_INTERNAL_ERROR;
8452 goto end;
8453 }
8454 }
Gilles Peskine449bd832023-01-11 14:50:10 +01008455 } else
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008456#endif /* MBEDTLS_SSL_SOME_SUITES_USE_MAC */
8457 {
Gilles Peskine449bd832023-01-11 14:50:10 +01008458 MBEDTLS_SSL_DEBUG_MSG(1, ("should never happen"));
8459 return MBEDTLS_ERR_SSL_INTERNAL_ERROR;
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008460 }
8461
Gilles Peskine449bd832023-01-11 14:50:10 +01008462 MBEDTLS_SSL_DEBUG_MSG(3, ("keylen: %u, minlen: %u, ivlen: %u, maclen: %u",
8463 (unsigned) keylen,
8464 (unsigned) transform->minlen,
8465 (unsigned) transform->ivlen,
8466 (unsigned) transform->maclen));
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008467
8468 /*
8469 * Finally setup the cipher contexts, IVs and MAC secrets.
8470 */
8471#if defined(MBEDTLS_SSL_CLI_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01008472 if (endpoint == MBEDTLS_SSL_IS_CLIENT) {
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008473 key1 = keyblk + mac_key_len * 2;
8474 key2 = keyblk + mac_key_len * 2 + keylen;
8475
8476 mac_enc = keyblk;
8477 mac_dec = keyblk + mac_key_len;
8478
Gilles Peskine449bd832023-01-11 14:50:10 +01008479 iv_copy_len = (transform->fixed_ivlen) ?
8480 transform->fixed_ivlen : transform->ivlen;
8481 memcpy(transform->iv_enc, key2 + keylen, iv_copy_len);
8482 memcpy(transform->iv_dec, key2 + keylen + iv_copy_len,
8483 iv_copy_len);
8484 } else
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008485#endif /* MBEDTLS_SSL_CLI_C */
8486#if defined(MBEDTLS_SSL_SRV_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01008487 if (endpoint == MBEDTLS_SSL_IS_SERVER) {
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008488 key1 = keyblk + mac_key_len * 2 + keylen;
8489 key2 = keyblk + mac_key_len * 2;
8490
8491 mac_enc = keyblk + mac_key_len;
8492 mac_dec = keyblk;
8493
Gilles Peskine449bd832023-01-11 14:50:10 +01008494 iv_copy_len = (transform->fixed_ivlen) ?
8495 transform->fixed_ivlen : transform->ivlen;
8496 memcpy(transform->iv_dec, key1 + keylen, iv_copy_len);
8497 memcpy(transform->iv_enc, key1 + keylen + iv_copy_len,
8498 iv_copy_len);
8499 } else
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008500#endif /* MBEDTLS_SSL_SRV_C */
8501 {
Gilles Peskine449bd832023-01-11 14:50:10 +01008502 MBEDTLS_SSL_DEBUG_MSG(1, ("should never happen"));
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008503 ret = MBEDTLS_ERR_SSL_INTERNAL_ERROR;
8504 goto end;
8505 }
8506
Gilles Peskine449bd832023-01-11 14:50:10 +01008507 if (ssl != NULL && ssl->f_export_keys != NULL) {
8508 ssl->f_export_keys(ssl->p_export_keys,
8509 MBEDTLS_SSL_KEY_EXPORT_TLS12_MASTER_SECRET,
8510 master, 48,
8511 randbytes + 32,
8512 randbytes,
8513 tls_prf_get_type(tls_prf));
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008514 }
8515
8516#if defined(MBEDTLS_USE_PSA_CRYPTO)
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008517 transform->psa_alg = alg;
8518
Gilles Peskine449bd832023-01-11 14:50:10 +01008519 if (alg != MBEDTLS_SSL_NULL_CIPHER) {
8520 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_ENCRYPT);
8521 psa_set_key_algorithm(&attributes, alg);
8522 psa_set_key_type(&attributes, key_type);
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008523
Gilles Peskine449bd832023-01-11 14:50:10 +01008524 if ((status = psa_import_key(&attributes,
8525 key1,
8526 PSA_BITS_TO_BYTES(key_bits),
8527 &transform->psa_key_enc)) != PSA_SUCCESS) {
8528 MBEDTLS_SSL_DEBUG_RET(3, "psa_import_key", (int) status);
8529 ret = psa_ssl_status_to_mbedtls(status);
8530 MBEDTLS_SSL_DEBUG_RET(1, "psa_import_key", ret);
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008531 goto end;
8532 }
8533
Gilles Peskine449bd832023-01-11 14:50:10 +01008534 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_DECRYPT);
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008535
Gilles Peskine449bd832023-01-11 14:50:10 +01008536 if ((status = psa_import_key(&attributes,
8537 key2,
8538 PSA_BITS_TO_BYTES(key_bits),
8539 &transform->psa_key_dec)) != PSA_SUCCESS) {
8540 ret = psa_ssl_status_to_mbedtls(status);
8541 MBEDTLS_SSL_DEBUG_RET(1, "psa_import_key", ret);
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008542 goto end;
8543 }
8544 }
8545#else
Gilles Peskine449bd832023-01-11 14:50:10 +01008546 if ((ret = mbedtls_cipher_setup(&transform->cipher_ctx_enc,
8547 cipher_info)) != 0) {
8548 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_cipher_setup", ret);
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008549 goto end;
8550 }
8551
Gilles Peskine449bd832023-01-11 14:50:10 +01008552 if ((ret = mbedtls_cipher_setup(&transform->cipher_ctx_dec,
8553 cipher_info)) != 0) {
8554 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_cipher_setup", ret);
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008555 goto end;
8556 }
8557
Gilles Peskine449bd832023-01-11 14:50:10 +01008558 if ((ret = mbedtls_cipher_setkey(&transform->cipher_ctx_enc, key1,
8559 (int) mbedtls_cipher_info_get_key_bitlen(cipher_info),
8560 MBEDTLS_ENCRYPT)) != 0) {
8561 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_cipher_setkey", ret);
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008562 goto end;
8563 }
8564
Gilles Peskine449bd832023-01-11 14:50:10 +01008565 if ((ret = mbedtls_cipher_setkey(&transform->cipher_ctx_dec, key2,
8566 (int) mbedtls_cipher_info_get_key_bitlen(cipher_info),
8567 MBEDTLS_DECRYPT)) != 0) {
8568 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_cipher_setkey", ret);
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008569 goto end;
8570 }
8571
8572#if defined(MBEDTLS_CIPHER_MODE_CBC)
Gilles Peskine449bd832023-01-11 14:50:10 +01008573 if (mbedtls_cipher_info_get_mode(cipher_info) == MBEDTLS_MODE_CBC) {
8574 if ((ret = mbedtls_cipher_set_padding_mode(&transform->cipher_ctx_enc,
8575 MBEDTLS_PADDING_NONE)) != 0) {
8576 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_cipher_set_padding_mode", ret);
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008577 goto end;
8578 }
8579
Gilles Peskine449bd832023-01-11 14:50:10 +01008580 if ((ret = mbedtls_cipher_set_padding_mode(&transform->cipher_ctx_dec,
8581 MBEDTLS_PADDING_NONE)) != 0) {
8582 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_cipher_set_padding_mode", ret);
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008583 goto end;
8584 }
8585 }
8586#endif /* MBEDTLS_CIPHER_MODE_CBC */
8587#endif /* MBEDTLS_USE_PSA_CRYPTO */
8588
Neil Armstrong29c0c042022-03-17 17:47:28 +01008589#if defined(MBEDTLS_SSL_SOME_SUITES_USE_MAC)
8590 /* For HMAC-based ciphersuites, initialize the HMAC transforms.
8591 For AEAD-based ciphersuites, there is nothing to do here. */
Gilles Peskine449bd832023-01-11 14:50:10 +01008592 if (mac_key_len != 0) {
Neil Armstrong29c0c042022-03-17 17:47:28 +01008593#if defined(MBEDTLS_USE_PSA_CRYPTO)
Gilles Peskine449bd832023-01-11 14:50:10 +01008594 transform->psa_mac_alg = PSA_ALG_HMAC(mac_alg);
Neil Armstrong29c0c042022-03-17 17:47:28 +01008595
Gilles Peskine449bd832023-01-11 14:50:10 +01008596 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_SIGN_MESSAGE);
8597 psa_set_key_algorithm(&attributes, PSA_ALG_HMAC(mac_alg));
8598 psa_set_key_type(&attributes, PSA_KEY_TYPE_HMAC);
Neil Armstrong29c0c042022-03-17 17:47:28 +01008599
Gilles Peskine449bd832023-01-11 14:50:10 +01008600 if ((status = psa_import_key(&attributes,
8601 mac_enc, mac_key_len,
8602 &transform->psa_mac_enc)) != PSA_SUCCESS) {
8603 ret = psa_ssl_status_to_mbedtls(status);
8604 MBEDTLS_SSL_DEBUG_RET(1, "psa_import_mac_key", ret);
Neil Armstrong29c0c042022-03-17 17:47:28 +01008605 goto end;
8606 }
8607
Gilles Peskine449bd832023-01-11 14:50:10 +01008608 if ((transform->psa_alg == MBEDTLS_SSL_NULL_CIPHER) ||
8609 ((transform->psa_alg == PSA_ALG_CBC_NO_PADDING)
Andrzej Kurek8c95ac42022-08-17 16:17:00 -04008610#if defined(MBEDTLS_SSL_SOME_SUITES_USE_CBC_ETM)
Gilles Peskine449bd832023-01-11 14:50:10 +01008611 && (transform->encrypt_then_mac == MBEDTLS_SSL_ETM_DISABLED)
Andrzej Kurek8c95ac42022-08-17 16:17:00 -04008612#endif
Gilles Peskine449bd832023-01-11 14:50:10 +01008613 )) {
Neil Armstrong29c0c042022-03-17 17:47:28 +01008614 /* mbedtls_ct_hmac() requires the key to be exportable */
Gilles Peskine449bd832023-01-11 14:50:10 +01008615 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_EXPORT |
8616 PSA_KEY_USAGE_VERIFY_HASH);
8617 } else {
8618 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_VERIFY_HASH);
8619 }
Neil Armstrong29c0c042022-03-17 17:47:28 +01008620
Gilles Peskine449bd832023-01-11 14:50:10 +01008621 if ((status = psa_import_key(&attributes,
8622 mac_dec, mac_key_len,
8623 &transform->psa_mac_dec)) != PSA_SUCCESS) {
8624 ret = psa_ssl_status_to_mbedtls(status);
8625 MBEDTLS_SSL_DEBUG_RET(1, "psa_import_mac_key", ret);
Neil Armstrong29c0c042022-03-17 17:47:28 +01008626 goto end;
8627 }
8628#else
Gilles Peskine449bd832023-01-11 14:50:10 +01008629 ret = mbedtls_md_hmac_starts(&transform->md_ctx_enc, mac_enc, mac_key_len);
8630 if (ret != 0) {
Neil Armstrong29c0c042022-03-17 17:47:28 +01008631 goto end;
Gilles Peskine449bd832023-01-11 14:50:10 +01008632 }
8633 ret = mbedtls_md_hmac_starts(&transform->md_ctx_dec, mac_dec, mac_key_len);
8634 if (ret != 0) {
Neil Armstrong29c0c042022-03-17 17:47:28 +01008635 goto end;
Gilles Peskine449bd832023-01-11 14:50:10 +01008636 }
Neil Armstrong29c0c042022-03-17 17:47:28 +01008637#endif /* MBEDTLS_USE_PSA_CRYPTO */
8638 }
8639#endif /* MBEDTLS_SSL_SOME_SUITES_USE_MAC */
8640
8641 ((void) mac_dec);
8642 ((void) mac_enc);
8643
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008644end:
Gilles Peskine449bd832023-01-11 14:50:10 +01008645 mbedtls_platform_zeroize(keyblk, sizeof(keyblk));
8646 return ret;
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008647}
8648
Valerio Settia08b1a42022-11-17 15:10:02 +01008649#if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED) && \
8650 defined(MBEDTLS_USE_PSA_CRYPTO)
Valerio Setti6b3dab02022-11-17 17:14:54 +01008651int mbedtls_psa_ecjpake_read_round(
Gilles Peskine449bd832023-01-11 14:50:10 +01008652 psa_pake_operation_t *pake_ctx,
8653 const unsigned char *buf,
8654 size_t len, mbedtls_ecjpake_rounds_t round)
Valerio Settia08b1a42022-11-17 15:10:02 +01008655{
8656 psa_status_t status;
8657 size_t input_offset = 0;
Valerio Setti819de862022-11-17 18:05:19 +01008658 /*
Valerio Setti6b3dab02022-11-17 17:14:54 +01008659 * At round one repeat the KEY_SHARE, ZK_PUBLIC & ZF_PROOF twice
8660 * At round two perform a single cycle
8661 */
Gilles Peskine449bd832023-01-11 14:50:10 +01008662 unsigned int remaining_steps = (round == MBEDTLS_ECJPAKE_ROUND_ONE) ? 2 : 1;
Valerio Settia08b1a42022-11-17 15:10:02 +01008663
Gilles Peskine449bd832023-01-11 14:50:10 +01008664 for (; remaining_steps > 0; remaining_steps--) {
8665 for (psa_pake_step_t step = PSA_PAKE_STEP_KEY_SHARE;
Valerio Settia08b1a42022-11-17 15:10:02 +01008666 step <= PSA_PAKE_STEP_ZK_PROOF;
Gilles Peskine449bd832023-01-11 14:50:10 +01008667 ++step) {
Valerio Settia08b1a42022-11-17 15:10:02 +01008668 /* Length is stored at the first byte */
8669 size_t length = buf[input_offset];
8670 input_offset += 1;
8671
Gilles Peskine449bd832023-01-11 14:50:10 +01008672 if (input_offset + length > len) {
Valerio Settia08b1a42022-11-17 15:10:02 +01008673 return MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE;
8674 }
8675
Gilles Peskine449bd832023-01-11 14:50:10 +01008676 status = psa_pake_input(pake_ctx, step,
8677 buf + input_offset, length);
8678 if (status != PSA_SUCCESS) {
8679 return psa_ssl_status_to_mbedtls(status);
Valerio Settia08b1a42022-11-17 15:10:02 +01008680 }
8681
8682 input_offset += length;
8683 }
8684 }
8685
Gilles Peskine449bd832023-01-11 14:50:10 +01008686 if (input_offset != len) {
Valerio Setti61ea17d2022-11-18 12:11:00 +01008687 return MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE;
Gilles Peskine449bd832023-01-11 14:50:10 +01008688 }
Valerio Setti30ebe112022-11-17 16:23:34 +01008689
Gilles Peskine449bd832023-01-11 14:50:10 +01008690 return 0;
Valerio Settia08b1a42022-11-17 15:10:02 +01008691}
8692
Valerio Setti6b3dab02022-11-17 17:14:54 +01008693int mbedtls_psa_ecjpake_write_round(
Gilles Peskine449bd832023-01-11 14:50:10 +01008694 psa_pake_operation_t *pake_ctx,
8695 unsigned char *buf,
8696 size_t len, size_t *olen,
8697 mbedtls_ecjpake_rounds_t round)
Valerio Settia08b1a42022-11-17 15:10:02 +01008698{
8699 psa_status_t status;
8700 size_t output_offset = 0;
8701 size_t output_len;
Valerio Setti819de862022-11-17 18:05:19 +01008702 /*
Valerio Setti6b3dab02022-11-17 17:14:54 +01008703 * At round one repeat the KEY_SHARE, ZK_PUBLIC & ZF_PROOF twice
8704 * At round two perform a single cycle
8705 */
Gilles Peskine449bd832023-01-11 14:50:10 +01008706 unsigned int remaining_steps = (round == MBEDTLS_ECJPAKE_ROUND_ONE) ? 2 : 1;
Valerio Settia08b1a42022-11-17 15:10:02 +01008707
Gilles Peskine449bd832023-01-11 14:50:10 +01008708 for (; remaining_steps > 0; remaining_steps--) {
8709 for (psa_pake_step_t step = PSA_PAKE_STEP_KEY_SHARE;
8710 step <= PSA_PAKE_STEP_ZK_PROOF;
8711 ++step) {
Valerio Setti79f6b6b2022-11-21 14:17:03 +01008712 /*
Valerio Settid4a9b1a2022-11-22 11:11:10 +01008713 * For each step, prepend 1 byte with the length of the data as
8714 * given by psa_pake_output().
Valerio Setti79f6b6b2022-11-21 14:17:03 +01008715 */
Gilles Peskine449bd832023-01-11 14:50:10 +01008716 status = psa_pake_output(pake_ctx, step,
8717 buf + output_offset + 1,
8718 len - output_offset - 1,
8719 &output_len);
8720 if (status != PSA_SUCCESS) {
8721 return psa_ssl_status_to_mbedtls(status);
Valerio Settia08b1a42022-11-17 15:10:02 +01008722 }
8723
Valerio Setti99d88c12022-11-22 16:03:43 +01008724 *(buf + output_offset) = (uint8_t) output_len;
Valerio Setti79f6b6b2022-11-21 14:17:03 +01008725
8726 output_offset += output_len + 1;
Valerio Settia08b1a42022-11-17 15:10:02 +01008727 }
8728 }
8729
8730 *olen = output_offset;
8731
Gilles Peskine449bd832023-01-11 14:50:10 +01008732 return 0;
Valerio Settia08b1a42022-11-17 15:10:02 +01008733}
Valerio Settia08b1a42022-11-17 15:10:02 +01008734#endif //MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED && MBEDTLS_USE_PSA_CRYPTO
8735
Jerry Yuee40f9d2022-02-17 14:55:16 +08008736#if defined(MBEDTLS_USE_PSA_CRYPTO)
Gilles Peskine449bd832023-01-11 14:50:10 +01008737int mbedtls_ssl_get_key_exchange_md_tls1_2(mbedtls_ssl_context *ssl,
8738 unsigned char *hash, size_t *hashlen,
8739 unsigned char *data, size_t data_len,
8740 mbedtls_md_type_t md_alg)
Jerry Yuee40f9d2022-02-17 14:55:16 +08008741{
8742 psa_status_t status;
8743 psa_hash_operation_t hash_operation = PSA_HASH_OPERATION_INIT;
Gilles Peskine449bd832023-01-11 14:50:10 +01008744 psa_algorithm_t hash_alg = mbedtls_hash_info_psa_from_md(md_alg);
Jerry Yuee40f9d2022-02-17 14:55:16 +08008745
Gilles Peskine449bd832023-01-11 14:50:10 +01008746 MBEDTLS_SSL_DEBUG_MSG(3, ("Perform PSA-based computation of digest of ServerKeyExchange"));
Jerry Yuee40f9d2022-02-17 14:55:16 +08008747
Gilles Peskine449bd832023-01-11 14:50:10 +01008748 if ((status = psa_hash_setup(&hash_operation,
8749 hash_alg)) != PSA_SUCCESS) {
8750 MBEDTLS_SSL_DEBUG_RET(1, "psa_hash_setup", status);
Jerry Yuee40f9d2022-02-17 14:55:16 +08008751 goto exit;
8752 }
8753
Gilles Peskine449bd832023-01-11 14:50:10 +01008754 if ((status = psa_hash_update(&hash_operation, ssl->handshake->randbytes,
8755 64)) != PSA_SUCCESS) {
8756 MBEDTLS_SSL_DEBUG_RET(1, "psa_hash_update", status);
Jerry Yuee40f9d2022-02-17 14:55:16 +08008757 goto exit;
8758 }
8759
Gilles Peskine449bd832023-01-11 14:50:10 +01008760 if ((status = psa_hash_update(&hash_operation,
8761 data, data_len)) != PSA_SUCCESS) {
8762 MBEDTLS_SSL_DEBUG_RET(1, "psa_hash_update", status);
Jerry Yuee40f9d2022-02-17 14:55:16 +08008763 goto exit;
8764 }
8765
Gilles Peskine449bd832023-01-11 14:50:10 +01008766 if ((status = psa_hash_finish(&hash_operation, hash, PSA_HASH_MAX_SIZE,
8767 hashlen)) != PSA_SUCCESS) {
8768 MBEDTLS_SSL_DEBUG_RET(1, "psa_hash_finish", status);
8769 goto exit;
Jerry Yuee40f9d2022-02-17 14:55:16 +08008770 }
8771
8772exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01008773 if (status != PSA_SUCCESS) {
8774 mbedtls_ssl_send_alert_message(ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
8775 MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR);
8776 switch (status) {
Jerry Yuee40f9d2022-02-17 14:55:16 +08008777 case PSA_ERROR_NOT_SUPPORTED:
Gilles Peskine449bd832023-01-11 14:50:10 +01008778 return MBEDTLS_ERR_MD_FEATURE_UNAVAILABLE;
Jerry Yuee40f9d2022-02-17 14:55:16 +08008779 case PSA_ERROR_BAD_STATE: /* Intentional fallthrough */
8780 case PSA_ERROR_BUFFER_TOO_SMALL:
Gilles Peskine449bd832023-01-11 14:50:10 +01008781 return MBEDTLS_ERR_MD_BAD_INPUT_DATA;
Jerry Yuee40f9d2022-02-17 14:55:16 +08008782 case PSA_ERROR_INSUFFICIENT_MEMORY:
Gilles Peskine449bd832023-01-11 14:50:10 +01008783 return MBEDTLS_ERR_MD_ALLOC_FAILED;
Jerry Yuee40f9d2022-02-17 14:55:16 +08008784 default:
Gilles Peskine449bd832023-01-11 14:50:10 +01008785 return MBEDTLS_ERR_PLATFORM_HW_ACCEL_FAILED;
Jerry Yuee40f9d2022-02-17 14:55:16 +08008786 }
8787 }
Gilles Peskine449bd832023-01-11 14:50:10 +01008788 return 0;
Jerry Yuee40f9d2022-02-17 14:55:16 +08008789}
8790
8791#else
8792
Gilles Peskine449bd832023-01-11 14:50:10 +01008793int mbedtls_ssl_get_key_exchange_md_tls1_2(mbedtls_ssl_context *ssl,
8794 unsigned char *hash, size_t *hashlen,
8795 unsigned char *data, size_t data_len,
8796 mbedtls_md_type_t md_alg)
Jerry Yuee40f9d2022-02-17 14:55:16 +08008797{
8798 int ret = 0;
8799 mbedtls_md_context_t ctx;
Gilles Peskine449bd832023-01-11 14:50:10 +01008800 const mbedtls_md_info_t *md_info = mbedtls_md_info_from_type(md_alg);
8801 *hashlen = mbedtls_md_get_size(md_info);
Jerry Yuee40f9d2022-02-17 14:55:16 +08008802
Gilles Peskine449bd832023-01-11 14:50:10 +01008803 MBEDTLS_SSL_DEBUG_MSG(3, ("Perform mbedtls-based computation of digest of ServerKeyExchange"));
Jerry Yuee40f9d2022-02-17 14:55:16 +08008804
Gilles Peskine449bd832023-01-11 14:50:10 +01008805 mbedtls_md_init(&ctx);
Jerry Yuee40f9d2022-02-17 14:55:16 +08008806
8807 /*
8808 * digitally-signed struct {
8809 * opaque client_random[32];
8810 * opaque server_random[32];
8811 * ServerDHParams params;
8812 * };
8813 */
Gilles Peskine449bd832023-01-11 14:50:10 +01008814 if ((ret = mbedtls_md_setup(&ctx, md_info, 0)) != 0) {
8815 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_md_setup", ret);
Jerry Yuee40f9d2022-02-17 14:55:16 +08008816 goto exit;
8817 }
Gilles Peskine449bd832023-01-11 14:50:10 +01008818 if ((ret = mbedtls_md_starts(&ctx)) != 0) {
8819 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_md_starts", ret);
Jerry Yuee40f9d2022-02-17 14:55:16 +08008820 goto exit;
8821 }
Gilles Peskine449bd832023-01-11 14:50:10 +01008822 if ((ret = mbedtls_md_update(&ctx, ssl->handshake->randbytes, 64)) != 0) {
8823 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_md_update", ret);
Jerry Yuee40f9d2022-02-17 14:55:16 +08008824 goto exit;
8825 }
Gilles Peskine449bd832023-01-11 14:50:10 +01008826 if ((ret = mbedtls_md_update(&ctx, data, data_len)) != 0) {
8827 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_md_update", ret);
Jerry Yuee40f9d2022-02-17 14:55:16 +08008828 goto exit;
8829 }
Gilles Peskine449bd832023-01-11 14:50:10 +01008830 if ((ret = mbedtls_md_finish(&ctx, hash)) != 0) {
8831 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_md_finish", ret);
Jerry Yuee40f9d2022-02-17 14:55:16 +08008832 goto exit;
8833 }
8834
8835exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01008836 mbedtls_md_free(&ctx);
Jerry Yuee40f9d2022-02-17 14:55:16 +08008837
Gilles Peskine449bd832023-01-11 14:50:10 +01008838 if (ret != 0) {
8839 mbedtls_ssl_send_alert_message(ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
8840 MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR);
8841 }
Jerry Yuee40f9d2022-02-17 14:55:16 +08008842
Gilles Peskine449bd832023-01-11 14:50:10 +01008843 return ret;
Jerry Yuee40f9d2022-02-17 14:55:16 +08008844}
8845#endif /* MBEDTLS_USE_PSA_CRYPTO */
8846
Jerry Yud9d91da2022-02-17 14:57:06 +08008847#if defined(MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED)
8848
Gabor Mezeia3d016c2022-05-10 12:44:09 +02008849/* Find the preferred hash for a given signature algorithm. */
8850unsigned int mbedtls_ssl_tls12_get_preferred_hash_for_sig_alg(
Gilles Peskine449bd832023-01-11 14:50:10 +01008851 mbedtls_ssl_context *ssl,
8852 unsigned int sig_alg)
Jerry Yud9d91da2022-02-17 14:57:06 +08008853{
Gabor Mezei078e8032022-04-27 21:17:56 +02008854 unsigned int i;
Gabor Mezeia3d016c2022-05-10 12:44:09 +02008855 uint16_t *received_sig_algs = ssl->handshake->received_sig_algs;
Gabor Mezei078e8032022-04-27 21:17:56 +02008856
Gilles Peskine449bd832023-01-11 14:50:10 +01008857 if (sig_alg == MBEDTLS_SSL_SIG_ANON) {
8858 return MBEDTLS_SSL_HASH_NONE;
8859 }
Gabor Mezei078e8032022-04-27 21:17:56 +02008860
Gilles Peskine449bd832023-01-11 14:50:10 +01008861 for (i = 0; received_sig_algs[i] != MBEDTLS_TLS_SIG_NONE; i++) {
Neil Armstrong9f1176a2022-06-24 18:19:19 +02008862 unsigned int hash_alg_received =
Gilles Peskine449bd832023-01-11 14:50:10 +01008863 MBEDTLS_SSL_TLS12_HASH_ALG_FROM_SIG_AND_HASH_ALG(
8864 received_sig_algs[i]);
Neil Armstrong9f1176a2022-06-24 18:19:19 +02008865 unsigned int sig_alg_received =
Gilles Peskine449bd832023-01-11 14:50:10 +01008866 MBEDTLS_SSL_TLS12_SIG_ALG_FROM_SIG_AND_HASH_ALG(
8867 received_sig_algs[i]);
Neil Armstrong9f1176a2022-06-24 18:19:19 +02008868
Gilles Peskine449bd832023-01-11 14:50:10 +01008869 if (sig_alg == sig_alg_received) {
Neil Armstrong9f1176a2022-06-24 18:19:19 +02008870#if defined(MBEDTLS_USE_PSA_CRYPTO)
Gilles Peskine449bd832023-01-11 14:50:10 +01008871 if (ssl->handshake->key_cert && ssl->handshake->key_cert->key) {
Neil Armstrong96eceb82022-06-30 18:05:05 +02008872 psa_algorithm_t psa_hash_alg =
Gilles Peskine449bd832023-01-11 14:50:10 +01008873 mbedtls_hash_info_psa_from_md(hash_alg_received);
Neil Armstrong9f1176a2022-06-24 18:19:19 +02008874
Gilles Peskine449bd832023-01-11 14:50:10 +01008875 if (sig_alg_received == MBEDTLS_SSL_SIG_ECDSA &&
8876 !mbedtls_pk_can_do_ext(ssl->handshake->key_cert->key,
8877 PSA_ALG_ECDSA(psa_hash_alg),
8878 PSA_KEY_USAGE_SIGN_HASH)) {
Neil Armstrong9f1176a2022-06-24 18:19:19 +02008879 continue;
Gilles Peskine449bd832023-01-11 14:50:10 +01008880 }
Neil Armstrong9f1176a2022-06-24 18:19:19 +02008881
Gilles Peskine449bd832023-01-11 14:50:10 +01008882 if (sig_alg_received == MBEDTLS_SSL_SIG_RSA &&
8883 !mbedtls_pk_can_do_ext(ssl->handshake->key_cert->key,
8884 PSA_ALG_RSA_PKCS1V15_SIGN(
8885 psa_hash_alg),
8886 PSA_KEY_USAGE_SIGN_HASH)) {
Neil Armstrong9f1176a2022-06-24 18:19:19 +02008887 continue;
Gilles Peskine449bd832023-01-11 14:50:10 +01008888 }
Neil Armstrong9f1176a2022-06-24 18:19:19 +02008889 }
Neil Armstrong9f1176a2022-06-24 18:19:19 +02008890#endif /* MBEDTLS_USE_PSA_CRYPTO */
Neil Armstrong96eceb82022-06-30 18:05:05 +02008891
Gilles Peskine449bd832023-01-11 14:50:10 +01008892 return hash_alg_received;
Neil Armstrong9f1176a2022-06-24 18:19:19 +02008893 }
Jerry Yud9d91da2022-02-17 14:57:06 +08008894 }
Jerry Yud9d91da2022-02-17 14:57:06 +08008895
Gilles Peskine449bd832023-01-11 14:50:10 +01008896 return MBEDTLS_SSL_HASH_NONE;
Jerry Yud9d91da2022-02-17 14:57:06 +08008897}
8898
8899#endif /* MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED */
8900
Jerry Yu4f9e3ef2022-02-17 14:58:27 +08008901/* Serialization of TLS 1.2 sessions:
8902 *
8903 * struct {
8904 * uint64 start_time;
8905 * uint8 ciphersuite[2]; // defined by the standard
Jerry Yu4f9e3ef2022-02-17 14:58:27 +08008906 * uint8 session_id_len; // at most 32
8907 * opaque session_id[32];
8908 * opaque master[48]; // fixed length in the standard
8909 * uint32 verify_result;
8910 * opaque peer_cert<0..2^24-1>; // length 0 means no peer cert
8911 * opaque ticket<0..2^24-1>; // length 0 means no ticket
8912 * uint32 ticket_lifetime;
8913 * uint8 mfl_code; // up to 255 according to standard
8914 * uint8 encrypt_then_mac; // 0 or 1
8915 * } serialized_session_tls12;
8916 *
8917 */
Gilles Peskine449bd832023-01-11 14:50:10 +01008918static size_t ssl_tls12_session_save(const mbedtls_ssl_session *session,
8919 unsigned char *buf,
8920 size_t buf_len)
Jerry Yu4f9e3ef2022-02-17 14:58:27 +08008921{
8922 unsigned char *p = buf;
8923 size_t used = 0;
8924
8925#if defined(MBEDTLS_HAVE_TIME)
8926 uint64_t start;
8927#endif
8928#if defined(MBEDTLS_X509_CRT_PARSE_C)
8929#if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
8930 size_t cert_len;
8931#endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
8932#endif /* MBEDTLS_X509_CRT_PARSE_C */
8933
8934 /*
8935 * Time
8936 */
8937#if defined(MBEDTLS_HAVE_TIME)
8938 used += 8;
8939
Gilles Peskine449bd832023-01-11 14:50:10 +01008940 if (used <= buf_len) {
Jerry Yu4f9e3ef2022-02-17 14:58:27 +08008941 start = (uint64_t) session->start;
8942
Gilles Peskine449bd832023-01-11 14:50:10 +01008943 MBEDTLS_PUT_UINT64_BE(start, p, 0);
Jerry Yu4f9e3ef2022-02-17 14:58:27 +08008944 p += 8;
8945 }
8946#endif /* MBEDTLS_HAVE_TIME */
8947
8948 /*
8949 * Basic mandatory fields
8950 */
8951 used += 2 /* ciphersuite */
Gilles Peskine449bd832023-01-11 14:50:10 +01008952 + 1 /* id_len */
8953 + sizeof(session->id)
8954 + sizeof(session->master)
8955 + 4; /* verify_result */
Jerry Yu4f9e3ef2022-02-17 14:58:27 +08008956
Gilles Peskine449bd832023-01-11 14:50:10 +01008957 if (used <= buf_len) {
8958 MBEDTLS_PUT_UINT16_BE(session->ciphersuite, p, 0);
Jerry Yu4f9e3ef2022-02-17 14:58:27 +08008959 p += 2;
8960
Gilles Peskine449bd832023-01-11 14:50:10 +01008961 *p++ = MBEDTLS_BYTE_0(session->id_len);
8962 memcpy(p, session->id, 32);
Jerry Yu4f9e3ef2022-02-17 14:58:27 +08008963 p += 32;
8964
Gilles Peskine449bd832023-01-11 14:50:10 +01008965 memcpy(p, session->master, 48);
Jerry Yu4f9e3ef2022-02-17 14:58:27 +08008966 p += 48;
8967
Gilles Peskine449bd832023-01-11 14:50:10 +01008968 MBEDTLS_PUT_UINT32_BE(session->verify_result, p, 0);
Jerry Yu4f9e3ef2022-02-17 14:58:27 +08008969 p += 4;
8970 }
8971
8972 /*
8973 * Peer's end-entity certificate
8974 */
8975#if defined(MBEDTLS_X509_CRT_PARSE_C)
8976#if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
Gilles Peskine449bd832023-01-11 14:50:10 +01008977 if (session->peer_cert == NULL) {
Jerry Yu4f9e3ef2022-02-17 14:58:27 +08008978 cert_len = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +01008979 } else {
Jerry Yu4f9e3ef2022-02-17 14:58:27 +08008980 cert_len = session->peer_cert->raw.len;
Gilles Peskine449bd832023-01-11 14:50:10 +01008981 }
Jerry Yu4f9e3ef2022-02-17 14:58:27 +08008982
8983 used += 3 + cert_len;
8984
Gilles Peskine449bd832023-01-11 14:50:10 +01008985 if (used <= buf_len) {
8986 *p++ = MBEDTLS_BYTE_2(cert_len);
8987 *p++ = MBEDTLS_BYTE_1(cert_len);
8988 *p++ = MBEDTLS_BYTE_0(cert_len);
Jerry Yu4f9e3ef2022-02-17 14:58:27 +08008989
Gilles Peskine449bd832023-01-11 14:50:10 +01008990 if (session->peer_cert != NULL) {
8991 memcpy(p, session->peer_cert->raw.p, cert_len);
Jerry Yu4f9e3ef2022-02-17 14:58:27 +08008992 p += cert_len;
8993 }
8994 }
8995#else /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
Gilles Peskine449bd832023-01-11 14:50:10 +01008996 if (session->peer_cert_digest != NULL) {
Jerry Yu4f9e3ef2022-02-17 14:58:27 +08008997 used += 1 /* type */ + 1 /* length */ + session->peer_cert_digest_len;
Gilles Peskine449bd832023-01-11 14:50:10 +01008998 if (used <= buf_len) {
Jerry Yu4f9e3ef2022-02-17 14:58:27 +08008999 *p++ = (unsigned char) session->peer_cert_digest_type;
9000 *p++ = (unsigned char) session->peer_cert_digest_len;
Gilles Peskine449bd832023-01-11 14:50:10 +01009001 memcpy(p, session->peer_cert_digest,
9002 session->peer_cert_digest_len);
Jerry Yu4f9e3ef2022-02-17 14:58:27 +08009003 p += session->peer_cert_digest_len;
9004 }
Gilles Peskine449bd832023-01-11 14:50:10 +01009005 } else {
Jerry Yu4f9e3ef2022-02-17 14:58:27 +08009006 used += 2;
Gilles Peskine449bd832023-01-11 14:50:10 +01009007 if (used <= buf_len) {
Jerry Yu4f9e3ef2022-02-17 14:58:27 +08009008 *p++ = (unsigned char) MBEDTLS_MD_NONE;
9009 *p++ = 0;
9010 }
9011 }
9012#endif /* !MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
9013#endif /* MBEDTLS_X509_CRT_PARSE_C */
9014
9015 /*
9016 * Session ticket if any, plus associated data
9017 */
9018#if defined(MBEDTLS_SSL_SESSION_TICKETS) && defined(MBEDTLS_SSL_CLI_C)
9019 used += 3 + session->ticket_len + 4; /* len + ticket + lifetime */
9020
Gilles Peskine449bd832023-01-11 14:50:10 +01009021 if (used <= buf_len) {
9022 *p++ = MBEDTLS_BYTE_2(session->ticket_len);
9023 *p++ = MBEDTLS_BYTE_1(session->ticket_len);
9024 *p++ = MBEDTLS_BYTE_0(session->ticket_len);
Jerry Yu4f9e3ef2022-02-17 14:58:27 +08009025
Gilles Peskine449bd832023-01-11 14:50:10 +01009026 if (session->ticket != NULL) {
9027 memcpy(p, session->ticket, session->ticket_len);
Jerry Yu4f9e3ef2022-02-17 14:58:27 +08009028 p += session->ticket_len;
9029 }
9030
Gilles Peskine449bd832023-01-11 14:50:10 +01009031 MBEDTLS_PUT_UINT32_BE(session->ticket_lifetime, p, 0);
Jerry Yu4f9e3ef2022-02-17 14:58:27 +08009032 p += 4;
9033 }
9034#endif /* MBEDTLS_SSL_SESSION_TICKETS && MBEDTLS_SSL_CLI_C */
9035
9036 /*
9037 * Misc extension-related info
9038 */
9039#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
9040 used += 1;
9041
Gilles Peskine449bd832023-01-11 14:50:10 +01009042 if (used <= buf_len) {
Jerry Yu4f9e3ef2022-02-17 14:58:27 +08009043 *p++ = session->mfl_code;
Gilles Peskine449bd832023-01-11 14:50:10 +01009044 }
Jerry Yu4f9e3ef2022-02-17 14:58:27 +08009045#endif
9046
9047#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
9048 used += 1;
9049
Gilles Peskine449bd832023-01-11 14:50:10 +01009050 if (used <= buf_len) {
9051 *p++ = MBEDTLS_BYTE_0(session->encrypt_then_mac);
9052 }
Jerry Yu4f9e3ef2022-02-17 14:58:27 +08009053#endif
9054
Gilles Peskine449bd832023-01-11 14:50:10 +01009055 return used;
Jerry Yu4f9e3ef2022-02-17 14:58:27 +08009056}
9057
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02009058MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01009059static int ssl_tls12_session_load(mbedtls_ssl_session *session,
9060 const unsigned char *buf,
9061 size_t len)
Jerry Yu4f9e3ef2022-02-17 14:58:27 +08009062{
9063#if defined(MBEDTLS_HAVE_TIME)
9064 uint64_t start;
9065#endif
9066#if defined(MBEDTLS_X509_CRT_PARSE_C)
9067#if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
9068 size_t cert_len;
9069#endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
9070#endif /* MBEDTLS_X509_CRT_PARSE_C */
9071
9072 const unsigned char *p = buf;
9073 const unsigned char * const end = buf + len;
9074
9075 /*
9076 * Time
9077 */
9078#if defined(MBEDTLS_HAVE_TIME)
Gilles Peskine449bd832023-01-11 14:50:10 +01009079 if (8 > (size_t) (end - p)) {
9080 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
9081 }
Jerry Yu4f9e3ef2022-02-17 14:58:27 +08009082
Gilles Peskine449bd832023-01-11 14:50:10 +01009083 start = ((uint64_t) p[0] << 56) |
9084 ((uint64_t) p[1] << 48) |
9085 ((uint64_t) p[2] << 40) |
9086 ((uint64_t) p[3] << 32) |
9087 ((uint64_t) p[4] << 24) |
9088 ((uint64_t) p[5] << 16) |
9089 ((uint64_t) p[6] << 8) |
9090 ((uint64_t) p[7]);
Jerry Yu4f9e3ef2022-02-17 14:58:27 +08009091 p += 8;
9092
9093 session->start = (time_t) start;
9094#endif /* MBEDTLS_HAVE_TIME */
9095
9096 /*
9097 * Basic mandatory fields
9098 */
Gilles Peskine449bd832023-01-11 14:50:10 +01009099 if (2 + 1 + 32 + 48 + 4 > (size_t) (end - p)) {
9100 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
9101 }
Jerry Yu4f9e3ef2022-02-17 14:58:27 +08009102
Gilles Peskine449bd832023-01-11 14:50:10 +01009103 session->ciphersuite = (p[0] << 8) | p[1];
Jerry Yu4f9e3ef2022-02-17 14:58:27 +08009104 p += 2;
9105
Jerry Yu4f9e3ef2022-02-17 14:58:27 +08009106 session->id_len = *p++;
Gilles Peskine449bd832023-01-11 14:50:10 +01009107 memcpy(session->id, p, 32);
Jerry Yu4f9e3ef2022-02-17 14:58:27 +08009108 p += 32;
9109
Gilles Peskine449bd832023-01-11 14:50:10 +01009110 memcpy(session->master, p, 48);
Jerry Yu4f9e3ef2022-02-17 14:58:27 +08009111 p += 48;
9112
Gilles Peskine449bd832023-01-11 14:50:10 +01009113 session->verify_result = ((uint32_t) p[0] << 24) |
9114 ((uint32_t) p[1] << 16) |
9115 ((uint32_t) p[2] << 8) |
9116 ((uint32_t) p[3]);
Jerry Yu4f9e3ef2022-02-17 14:58:27 +08009117 p += 4;
9118
9119 /* Immediately clear invalid pointer values that have been read, in case
9120 * we exit early before we replaced them with valid ones. */
9121#if defined(MBEDTLS_X509_CRT_PARSE_C)
9122#if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
9123 session->peer_cert = NULL;
9124#else
9125 session->peer_cert_digest = NULL;
9126#endif /* !MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
9127#endif /* MBEDTLS_X509_CRT_PARSE_C */
9128#if defined(MBEDTLS_SSL_SESSION_TICKETS) && defined(MBEDTLS_SSL_CLI_C)
9129 session->ticket = NULL;
9130#endif /* MBEDTLS_SSL_SESSION_TICKETS && MBEDTLS_SSL_CLI_C */
9131
9132 /*
9133 * Peer certificate
9134 */
9135#if defined(MBEDTLS_X509_CRT_PARSE_C)
9136#if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
9137 /* Deserialize CRT from the end of the ticket. */
Gilles Peskine449bd832023-01-11 14:50:10 +01009138 if (3 > (size_t) (end - p)) {
9139 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
9140 }
Jerry Yu4f9e3ef2022-02-17 14:58:27 +08009141
Gilles Peskine449bd832023-01-11 14:50:10 +01009142 cert_len = (p[0] << 16) | (p[1] << 8) | p[2];
Jerry Yu4f9e3ef2022-02-17 14:58:27 +08009143 p += 3;
9144
Gilles Peskine449bd832023-01-11 14:50:10 +01009145 if (cert_len != 0) {
Jerry Yu4f9e3ef2022-02-17 14:58:27 +08009146 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
9147
Gilles Peskine449bd832023-01-11 14:50:10 +01009148 if (cert_len > (size_t) (end - p)) {
9149 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
9150 }
Jerry Yu4f9e3ef2022-02-17 14:58:27 +08009151
Gilles Peskine449bd832023-01-11 14:50:10 +01009152 session->peer_cert = mbedtls_calloc(1, sizeof(mbedtls_x509_crt));
Jerry Yu4f9e3ef2022-02-17 14:58:27 +08009153
Gilles Peskine449bd832023-01-11 14:50:10 +01009154 if (session->peer_cert == NULL) {
9155 return MBEDTLS_ERR_SSL_ALLOC_FAILED;
9156 }
Jerry Yu4f9e3ef2022-02-17 14:58:27 +08009157
Gilles Peskine449bd832023-01-11 14:50:10 +01009158 mbedtls_x509_crt_init(session->peer_cert);
Jerry Yu4f9e3ef2022-02-17 14:58:27 +08009159
Gilles Peskine449bd832023-01-11 14:50:10 +01009160 if ((ret = mbedtls_x509_crt_parse_der(session->peer_cert,
9161 p, cert_len)) != 0) {
9162 mbedtls_x509_crt_free(session->peer_cert);
9163 mbedtls_free(session->peer_cert);
Jerry Yu4f9e3ef2022-02-17 14:58:27 +08009164 session->peer_cert = NULL;
Gilles Peskine449bd832023-01-11 14:50:10 +01009165 return ret;
Jerry Yu4f9e3ef2022-02-17 14:58:27 +08009166 }
9167
9168 p += cert_len;
9169 }
9170#else /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
9171 /* Deserialize CRT digest from the end of the ticket. */
Gilles Peskine449bd832023-01-11 14:50:10 +01009172 if (2 > (size_t) (end - p)) {
9173 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
9174 }
Jerry Yu4f9e3ef2022-02-17 14:58:27 +08009175
9176 session->peer_cert_digest_type = (mbedtls_md_type_t) *p++;
9177 session->peer_cert_digest_len = (size_t) *p++;
9178
Gilles Peskine449bd832023-01-11 14:50:10 +01009179 if (session->peer_cert_digest_len != 0) {
Jerry Yu4f9e3ef2022-02-17 14:58:27 +08009180 const mbedtls_md_info_t *md_info =
Gilles Peskine449bd832023-01-11 14:50:10 +01009181 mbedtls_md_info_from_type(session->peer_cert_digest_type);
9182 if (md_info == NULL) {
9183 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
9184 }
9185 if (session->peer_cert_digest_len != mbedtls_md_get_size(md_info)) {
9186 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
9187 }
Jerry Yu4f9e3ef2022-02-17 14:58:27 +08009188
Gilles Peskine449bd832023-01-11 14:50:10 +01009189 if (session->peer_cert_digest_len > (size_t) (end - p)) {
9190 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
9191 }
Jerry Yu4f9e3ef2022-02-17 14:58:27 +08009192
9193 session->peer_cert_digest =
Gilles Peskine449bd832023-01-11 14:50:10 +01009194 mbedtls_calloc(1, session->peer_cert_digest_len);
9195 if (session->peer_cert_digest == NULL) {
9196 return MBEDTLS_ERR_SSL_ALLOC_FAILED;
9197 }
Jerry Yu4f9e3ef2022-02-17 14:58:27 +08009198
Gilles Peskine449bd832023-01-11 14:50:10 +01009199 memcpy(session->peer_cert_digest, p,
9200 session->peer_cert_digest_len);
Jerry Yu4f9e3ef2022-02-17 14:58:27 +08009201 p += session->peer_cert_digest_len;
9202 }
9203#endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
9204#endif /* MBEDTLS_X509_CRT_PARSE_C */
9205
9206 /*
9207 * Session ticket and associated data
9208 */
9209#if defined(MBEDTLS_SSL_SESSION_TICKETS) && defined(MBEDTLS_SSL_CLI_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01009210 if (3 > (size_t) (end - p)) {
9211 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
9212 }
Jerry Yu4f9e3ef2022-02-17 14:58:27 +08009213
Gilles Peskine449bd832023-01-11 14:50:10 +01009214 session->ticket_len = (p[0] << 16) | (p[1] << 8) | p[2];
Jerry Yu4f9e3ef2022-02-17 14:58:27 +08009215 p += 3;
9216
Gilles Peskine449bd832023-01-11 14:50:10 +01009217 if (session->ticket_len != 0) {
9218 if (session->ticket_len > (size_t) (end - p)) {
9219 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
9220 }
Jerry Yu4f9e3ef2022-02-17 14:58:27 +08009221
Gilles Peskine449bd832023-01-11 14:50:10 +01009222 session->ticket = mbedtls_calloc(1, session->ticket_len);
9223 if (session->ticket == NULL) {
9224 return MBEDTLS_ERR_SSL_ALLOC_FAILED;
9225 }
Jerry Yu4f9e3ef2022-02-17 14:58:27 +08009226
Gilles Peskine449bd832023-01-11 14:50:10 +01009227 memcpy(session->ticket, p, session->ticket_len);
Jerry Yu4f9e3ef2022-02-17 14:58:27 +08009228 p += session->ticket_len;
9229 }
9230
Gilles Peskine449bd832023-01-11 14:50:10 +01009231 if (4 > (size_t) (end - p)) {
9232 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
9233 }
Jerry Yu4f9e3ef2022-02-17 14:58:27 +08009234
Gilles Peskine449bd832023-01-11 14:50:10 +01009235 session->ticket_lifetime = ((uint32_t) p[0] << 24) |
9236 ((uint32_t) p[1] << 16) |
9237 ((uint32_t) p[2] << 8) |
9238 ((uint32_t) p[3]);
Jerry Yu4f9e3ef2022-02-17 14:58:27 +08009239 p += 4;
9240#endif /* MBEDTLS_SSL_SESSION_TICKETS && MBEDTLS_SSL_CLI_C */
9241
9242 /*
9243 * Misc extension-related info
9244 */
9245#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
Gilles Peskine449bd832023-01-11 14:50:10 +01009246 if (1 > (size_t) (end - p)) {
9247 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
9248 }
Jerry Yu4f9e3ef2022-02-17 14:58:27 +08009249
9250 session->mfl_code = *p++;
9251#endif
9252
9253#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
Gilles Peskine449bd832023-01-11 14:50:10 +01009254 if (1 > (size_t) (end - p)) {
9255 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
9256 }
Jerry Yu4f9e3ef2022-02-17 14:58:27 +08009257
9258 session->encrypt_then_mac = *p++;
9259#endif
9260
9261 /* Done, should have consumed entire buffer */
Gilles Peskine449bd832023-01-11 14:50:10 +01009262 if (p != end) {
9263 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
9264 }
Jerry Yu4f9e3ef2022-02-17 14:58:27 +08009265
Gilles Peskine449bd832023-01-11 14:50:10 +01009266 return 0;
Jerry Yu4f9e3ef2022-02-17 14:58:27 +08009267}
Jerry Yudc7bd172022-02-17 13:44:15 +08009268#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
9269
XiaokangQian75d40ef2022-04-20 11:05:24 +00009270int mbedtls_ssl_validate_ciphersuite(
9271 const mbedtls_ssl_context *ssl,
9272 const mbedtls_ssl_ciphersuite_t *suite_info,
9273 mbedtls_ssl_protocol_version min_tls_version,
Gilles Peskine449bd832023-01-11 14:50:10 +01009274 mbedtls_ssl_protocol_version max_tls_version)
XiaokangQian75d40ef2022-04-20 11:05:24 +00009275{
9276 (void) ssl;
9277
Gilles Peskine449bd832023-01-11 14:50:10 +01009278 if (suite_info == NULL) {
9279 return -1;
9280 }
XiaokangQian75d40ef2022-04-20 11:05:24 +00009281
Gilles Peskine449bd832023-01-11 14:50:10 +01009282 if ((suite_info->min_tls_version > max_tls_version) ||
9283 (suite_info->max_tls_version < min_tls_version)) {
9284 return -1;
XiaokangQian75d40ef2022-04-20 11:05:24 +00009285 }
9286
XiaokangQian060d8672022-04-21 09:24:56 +00009287#if defined(MBEDTLS_SSL_PROTO_TLS1_2) && defined(MBEDTLS_SSL_CLI_C)
XiaokangQian75d40ef2022-04-20 11:05:24 +00009288#if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
Neil Armstrongca7d5062022-05-31 14:43:23 +02009289#if defined(MBEDTLS_USE_PSA_CRYPTO)
Gilles Peskine449bd832023-01-11 14:50:10 +01009290 if (suite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECJPAKE &&
9291 ssl->handshake->psa_pake_ctx_is_ok != 1)
Neil Armstrongca7d5062022-05-31 14:43:23 +02009292#else
Gilles Peskine449bd832023-01-11 14:50:10 +01009293 if (suite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECJPAKE &&
9294 mbedtls_ecjpake_check(&ssl->handshake->ecjpake_ctx) != 0)
Neil Armstrongca7d5062022-05-31 14:43:23 +02009295#endif /* MBEDTLS_USE_PSA_CRYPTO */
XiaokangQian75d40ef2022-04-20 11:05:24 +00009296 {
Gilles Peskine449bd832023-01-11 14:50:10 +01009297 return -1;
XiaokangQian75d40ef2022-04-20 11:05:24 +00009298 }
9299#endif
9300
9301 /* Don't suggest PSK-based ciphersuite if no PSK is available. */
9302#if defined(MBEDTLS_KEY_EXCHANGE_SOME_PSK_ENABLED)
Gilles Peskine449bd832023-01-11 14:50:10 +01009303 if (mbedtls_ssl_ciphersuite_uses_psk(suite_info) &&
9304 mbedtls_ssl_conf_has_static_psk(ssl->conf) == 0) {
9305 return -1;
XiaokangQian75d40ef2022-04-20 11:05:24 +00009306 }
9307#endif /* MBEDTLS_KEY_EXCHANGE_SOME_PSK_ENABLED */
9308#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
9309
Gilles Peskine449bd832023-01-11 14:50:10 +01009310 return 0;
XiaokangQian75d40ef2022-04-20 11:05:24 +00009311}
9312
Ronald Crone68ab4f2022-10-05 12:46:29 +02009313#if defined(MBEDTLS_SSL_HANDSHAKE_WITH_CERT_ENABLED)
XiaokangQianeaf36512022-04-24 09:07:44 +00009314/*
9315 * Function for writing a signature algorithm extension.
9316 *
9317 * The `extension_data` field of signature algorithm contains a `SignatureSchemeList`
9318 * value (TLS 1.3 RFC8446):
9319 * enum {
9320 * ....
9321 * ecdsa_secp256r1_sha256( 0x0403 ),
9322 * ecdsa_secp384r1_sha384( 0x0503 ),
9323 * ecdsa_secp521r1_sha512( 0x0603 ),
9324 * ....
9325 * } SignatureScheme;
9326 *
9327 * struct {
9328 * SignatureScheme supported_signature_algorithms<2..2^16-2>;
9329 * } SignatureSchemeList;
9330 *
9331 * The `extension_data` field of signature algorithm contains a `SignatureAndHashAlgorithm`
9332 * value (TLS 1.2 RFC5246):
9333 * enum {
9334 * none(0), md5(1), sha1(2), sha224(3), sha256(4), sha384(5),
9335 * sha512(6), (255)
9336 * } HashAlgorithm;
9337 *
9338 * enum { anonymous(0), rsa(1), dsa(2), ecdsa(3), (255) }
9339 * SignatureAlgorithm;
9340 *
9341 * struct {
9342 * HashAlgorithm hash;
9343 * SignatureAlgorithm signature;
9344 * } SignatureAndHashAlgorithm;
9345 *
9346 * SignatureAndHashAlgorithm
9347 * supported_signature_algorithms<2..2^16-2>;
9348 *
9349 * The TLS 1.3 signature algorithm extension was defined to be a compatible
9350 * generalization of the TLS 1.2 signature algorithm extension.
9351 * `SignatureAndHashAlgorithm` field of TLS 1.2 can be represented by
9352 * `SignatureScheme` field of TLS 1.3
9353 *
9354 */
Gilles Peskine449bd832023-01-11 14:50:10 +01009355int mbedtls_ssl_write_sig_alg_ext(mbedtls_ssl_context *ssl, unsigned char *buf,
9356 const unsigned char *end, size_t *out_len)
XiaokangQianeaf36512022-04-24 09:07:44 +00009357{
9358 unsigned char *p = buf;
9359 unsigned char *supported_sig_alg; /* Start of supported_signature_algorithms */
9360 size_t supported_sig_alg_len = 0; /* Length of supported_signature_algorithms */
9361
9362 *out_len = 0;
9363
Gilles Peskine449bd832023-01-11 14:50:10 +01009364 MBEDTLS_SSL_DEBUG_MSG(3, ("adding signature_algorithms extension"));
XiaokangQianeaf36512022-04-24 09:07:44 +00009365
9366 /* Check if we have space for header and length field:
9367 * - extension_type (2 bytes)
9368 * - extension_data_length (2 bytes)
9369 * - supported_signature_algorithms_length (2 bytes)
9370 */
Gilles Peskine449bd832023-01-11 14:50:10 +01009371 MBEDTLS_SSL_CHK_BUF_PTR(p, end, 6);
XiaokangQianeaf36512022-04-24 09:07:44 +00009372 p += 6;
9373
9374 /*
9375 * Write supported_signature_algorithms
9376 */
9377 supported_sig_alg = p;
Gilles Peskine449bd832023-01-11 14:50:10 +01009378 const uint16_t *sig_alg = mbedtls_ssl_get_sig_algs(ssl);
9379 if (sig_alg == NULL) {
9380 return MBEDTLS_ERR_SSL_BAD_CONFIG;
9381 }
XiaokangQianeaf36512022-04-24 09:07:44 +00009382
Gilles Peskine449bd832023-01-11 14:50:10 +01009383 for (; *sig_alg != MBEDTLS_TLS1_3_SIG_NONE; sig_alg++) {
9384 MBEDTLS_SSL_DEBUG_MSG(3, ("got signature scheme [%x] %s",
9385 *sig_alg,
9386 mbedtls_ssl_sig_alg_to_str(*sig_alg)));
9387 if (!mbedtls_ssl_sig_alg_is_supported(ssl, *sig_alg)) {
XiaokangQianeaf36512022-04-24 09:07:44 +00009388 continue;
Gilles Peskine449bd832023-01-11 14:50:10 +01009389 }
9390 MBEDTLS_SSL_CHK_BUF_PTR(p, end, 2);
9391 MBEDTLS_PUT_UINT16_BE(*sig_alg, p, 0);
XiaokangQianeaf36512022-04-24 09:07:44 +00009392 p += 2;
Gilles Peskine449bd832023-01-11 14:50:10 +01009393 MBEDTLS_SSL_DEBUG_MSG(3, ("sent signature scheme [%x] %s",
9394 *sig_alg,
9395 mbedtls_ssl_sig_alg_to_str(*sig_alg)));
XiaokangQianeaf36512022-04-24 09:07:44 +00009396 }
9397
9398 /* Length of supported_signature_algorithms */
9399 supported_sig_alg_len = p - supported_sig_alg;
Gilles Peskine449bd832023-01-11 14:50:10 +01009400 if (supported_sig_alg_len == 0) {
9401 MBEDTLS_SSL_DEBUG_MSG(1, ("No signature algorithms defined."));
9402 return MBEDTLS_ERR_SSL_INTERNAL_ERROR;
XiaokangQianeaf36512022-04-24 09:07:44 +00009403 }
9404
Gilles Peskine449bd832023-01-11 14:50:10 +01009405 MBEDTLS_PUT_UINT16_BE(MBEDTLS_TLS_EXT_SIG_ALG, buf, 0);
9406 MBEDTLS_PUT_UINT16_BE(supported_sig_alg_len + 2, buf, 2);
9407 MBEDTLS_PUT_UINT16_BE(supported_sig_alg_len, buf, 4);
XiaokangQianeaf36512022-04-24 09:07:44 +00009408
XiaokangQianeaf36512022-04-24 09:07:44 +00009409 *out_len = p - buf;
9410
9411#if defined(MBEDTLS_SSL_PROTO_TLS1_3)
Gilles Peskine449bd832023-01-11 14:50:10 +01009412 mbedtls_ssl_tls13_set_hs_sent_ext_mask(ssl, MBEDTLS_TLS_EXT_SIG_ALG);
XiaokangQianeaf36512022-04-24 09:07:44 +00009413#endif /* MBEDTLS_SSL_PROTO_TLS1_3 */
Jerry Yu0c354a22022-08-29 15:25:36 +08009414
Gilles Peskine449bd832023-01-11 14:50:10 +01009415 return 0;
XiaokangQianeaf36512022-04-24 09:07:44 +00009416}
Ronald Crone68ab4f2022-10-05 12:46:29 +02009417#endif /* MBEDTLS_SSL_HANDSHAKE_WITH_CERT_ENABLED */
XiaokangQianeaf36512022-04-24 09:07:44 +00009418
XiaokangQian40a35232022-05-07 09:02:40 +00009419#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
XiaokangQian9b2b7712022-05-17 02:57:00 +00009420/*
9421 * mbedtls_ssl_parse_server_name_ext
9422 *
9423 * Structure of server_name extension:
9424 *
9425 * enum {
9426 * host_name(0), (255)
9427 * } NameType;
9428 * opaque HostName<1..2^16-1>;
9429 *
9430 * struct {
9431 * NameType name_type;
9432 * select (name_type) {
9433 * case host_name: HostName;
9434 * } name;
9435 * } ServerName;
9436 * struct {
9437 * ServerName server_name_list<1..2^16-1>
9438 * } ServerNameList;
9439 */
Ronald Cronce7d76e2022-07-08 18:56:49 +02009440MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01009441int mbedtls_ssl_parse_server_name_ext(mbedtls_ssl_context *ssl,
9442 const unsigned char *buf,
9443 const unsigned char *end)
XiaokangQian40a35232022-05-07 09:02:40 +00009444{
9445 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
9446 const unsigned char *p = buf;
XiaokangQian9b2b7712022-05-17 02:57:00 +00009447 size_t server_name_list_len, hostname_len;
9448 const unsigned char *server_name_list_end;
XiaokangQian40a35232022-05-07 09:02:40 +00009449
Gilles Peskine449bd832023-01-11 14:50:10 +01009450 MBEDTLS_SSL_DEBUG_MSG(3, ("parse ServerName extension"));
XiaokangQian40a35232022-05-07 09:02:40 +00009451
Gilles Peskine449bd832023-01-11 14:50:10 +01009452 MBEDTLS_SSL_CHK_BUF_READ_PTR(p, end, 2);
9453 server_name_list_len = MBEDTLS_GET_UINT16_BE(p, 0);
XiaokangQian40a35232022-05-07 09:02:40 +00009454 p += 2;
9455
Gilles Peskine449bd832023-01-11 14:50:10 +01009456 MBEDTLS_SSL_CHK_BUF_READ_PTR(p, end, server_name_list_len);
XiaokangQian9b2b7712022-05-17 02:57:00 +00009457 server_name_list_end = p + server_name_list_len;
Gilles Peskine449bd832023-01-11 14:50:10 +01009458 while (p < server_name_list_end) {
9459 MBEDTLS_SSL_CHK_BUF_READ_PTR(p, server_name_list_end, 3);
9460 hostname_len = MBEDTLS_GET_UINT16_BE(p, 1);
9461 MBEDTLS_SSL_CHK_BUF_READ_PTR(p, server_name_list_end,
9462 hostname_len + 3);
XiaokangQian40a35232022-05-07 09:02:40 +00009463
Gilles Peskine449bd832023-01-11 14:50:10 +01009464 if (p[0] == MBEDTLS_TLS_EXT_SERVERNAME_HOSTNAME) {
XiaokangQian75fe8c72022-06-15 09:42:45 +00009465 /* sni_name is intended to be used only during the parsing of the
9466 * ClientHello message (it is reset to NULL before the end of
9467 * the message parsing). Thus it is ok to just point to the
9468 * reception buffer and not make a copy of it.
9469 */
XiaokangQianf2a94202022-05-20 06:44:24 +00009470 ssl->handshake->sni_name = p + 3;
9471 ssl->handshake->sni_name_len = hostname_len;
Gilles Peskine449bd832023-01-11 14:50:10 +01009472 if (ssl->conf->f_sni == NULL) {
9473 return 0;
XiaokangQian40a35232022-05-07 09:02:40 +00009474 }
Gilles Peskine449bd832023-01-11 14:50:10 +01009475 ret = ssl->conf->f_sni(ssl->conf->p_sni,
9476 ssl, p + 3, hostname_len);
9477 if (ret != 0) {
9478 MBEDTLS_SSL_DEBUG_RET(1, "ssl_sni_wrapper", ret);
9479 MBEDTLS_SSL_PEND_FATAL_ALERT(MBEDTLS_SSL_ALERT_MSG_UNRECOGNIZED_NAME,
9480 MBEDTLS_ERR_SSL_UNRECOGNIZED_NAME);
9481 return MBEDTLS_ERR_SSL_UNRECOGNIZED_NAME;
9482 }
9483 return 0;
XiaokangQian40a35232022-05-07 09:02:40 +00009484 }
9485
9486 p += hostname_len + 3;
9487 }
9488
Gilles Peskine449bd832023-01-11 14:50:10 +01009489 return 0;
XiaokangQian40a35232022-05-07 09:02:40 +00009490}
9491#endif /* MBEDTLS_SSL_SERVER_NAME_INDICATION */
9492
XiaokangQianacb39922022-06-17 10:18:48 +00009493#if defined(MBEDTLS_SSL_ALPN)
Ronald Cronce7d76e2022-07-08 18:56:49 +02009494MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01009495int mbedtls_ssl_parse_alpn_ext(mbedtls_ssl_context *ssl,
9496 const unsigned char *buf,
9497 const unsigned char *end)
XiaokangQianacb39922022-06-17 10:18:48 +00009498{
9499 const unsigned char *p = buf;
XiaokangQianc7403452022-06-23 03:24:12 +00009500 size_t protocol_name_list_len;
XiaokangQian95d5f542022-06-24 02:29:26 +00009501 const unsigned char *protocol_name_list;
9502 const unsigned char *protocol_name_list_end;
XiaokangQianc7403452022-06-23 03:24:12 +00009503 size_t protocol_name_len;
XiaokangQianacb39922022-06-17 10:18:48 +00009504
9505 /* If ALPN not configured, just ignore the extension */
Gilles Peskine449bd832023-01-11 14:50:10 +01009506 if (ssl->conf->alpn_list == NULL) {
9507 return 0;
9508 }
XiaokangQianacb39922022-06-17 10:18:48 +00009509
9510 /*
XiaokangQianc7403452022-06-23 03:24:12 +00009511 * RFC7301, section 3.1
9512 * opaque ProtocolName<1..2^8-1>;
XiaokangQianacb39922022-06-17 10:18:48 +00009513 *
XiaokangQianc7403452022-06-23 03:24:12 +00009514 * struct {
9515 * ProtocolName protocol_name_list<2..2^16-1>
9516 * } ProtocolNameList;
XiaokangQianacb39922022-06-17 10:18:48 +00009517 */
9518
XiaokangQianc7403452022-06-23 03:24:12 +00009519 /*
XiaokangQian0b776e22022-06-24 09:04:59 +00009520 * protocol_name_list_len 2 bytes
9521 * protocol_name_len 1 bytes
9522 * protocol_name >=1 byte
XiaokangQianc7403452022-06-23 03:24:12 +00009523 */
Gilles Peskine449bd832023-01-11 14:50:10 +01009524 MBEDTLS_SSL_CHK_BUF_READ_PTR(p, end, 4);
XiaokangQianacb39922022-06-17 10:18:48 +00009525
Gilles Peskine449bd832023-01-11 14:50:10 +01009526 protocol_name_list_len = MBEDTLS_GET_UINT16_BE(p, 0);
XiaokangQianacb39922022-06-17 10:18:48 +00009527 p += 2;
Gilles Peskine449bd832023-01-11 14:50:10 +01009528 MBEDTLS_SSL_CHK_BUF_READ_PTR(p, end, protocol_name_list_len);
XiaokangQian95d5f542022-06-24 02:29:26 +00009529 protocol_name_list = p;
9530 protocol_name_list_end = p + protocol_name_list_len;
XiaokangQianacb39922022-06-17 10:18:48 +00009531
9532 /* Validate peer's list (lengths) */
Gilles Peskine449bd832023-01-11 14:50:10 +01009533 while (p < protocol_name_list_end) {
XiaokangQian95d5f542022-06-24 02:29:26 +00009534 protocol_name_len = *p++;
Gilles Peskine449bd832023-01-11 14:50:10 +01009535 MBEDTLS_SSL_CHK_BUF_READ_PTR(p, protocol_name_list_end,
9536 protocol_name_len);
9537 if (protocol_name_len == 0) {
XiaokangQian95d5f542022-06-24 02:29:26 +00009538 MBEDTLS_SSL_PEND_FATAL_ALERT(
9539 MBEDTLS_SSL_ALERT_MSG_ILLEGAL_PARAMETER,
Gilles Peskine449bd832023-01-11 14:50:10 +01009540 MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER);
9541 return MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER;
XiaokangQian95d5f542022-06-24 02:29:26 +00009542 }
9543
9544 p += protocol_name_len;
XiaokangQianacb39922022-06-17 10:18:48 +00009545 }
9546
9547 /* Use our order of preference */
Gilles Peskine449bd832023-01-11 14:50:10 +01009548 for (const char **alpn = ssl->conf->alpn_list; *alpn != NULL; alpn++) {
9549 size_t const alpn_len = strlen(*alpn);
XiaokangQian95d5f542022-06-24 02:29:26 +00009550 p = protocol_name_list;
Gilles Peskine449bd832023-01-11 14:50:10 +01009551 while (p < protocol_name_list_end) {
XiaokangQian95d5f542022-06-24 02:29:26 +00009552 protocol_name_len = *p++;
Gilles Peskine449bd832023-01-11 14:50:10 +01009553 if (protocol_name_len == alpn_len &&
9554 memcmp(p, *alpn, alpn_len) == 0) {
XiaokangQianacb39922022-06-17 10:18:48 +00009555 ssl->alpn_chosen = *alpn;
Gilles Peskine449bd832023-01-11 14:50:10 +01009556 return 0;
XiaokangQianacb39922022-06-17 10:18:48 +00009557 }
XiaokangQian95d5f542022-06-24 02:29:26 +00009558
9559 p += protocol_name_len;
XiaokangQianacb39922022-06-17 10:18:48 +00009560 }
9561 }
9562
XiaokangQian95d5f542022-06-24 02:29:26 +00009563 /* If we get here, no match was found */
XiaokangQianacb39922022-06-17 10:18:48 +00009564 MBEDTLS_SSL_PEND_FATAL_ALERT(
Gilles Peskine449bd832023-01-11 14:50:10 +01009565 MBEDTLS_SSL_ALERT_MSG_NO_APPLICATION_PROTOCOL,
9566 MBEDTLS_ERR_SSL_NO_APPLICATION_PROTOCOL);
9567 return MBEDTLS_ERR_SSL_NO_APPLICATION_PROTOCOL;
XiaokangQianacb39922022-06-17 10:18:48 +00009568}
9569
Gilles Peskine449bd832023-01-11 14:50:10 +01009570int mbedtls_ssl_write_alpn_ext(mbedtls_ssl_context *ssl,
9571 unsigned char *buf,
9572 unsigned char *end,
9573 size_t *out_len)
XiaokangQianacb39922022-06-17 10:18:48 +00009574{
9575 unsigned char *p = buf;
XiaokangQian95d5f542022-06-24 02:29:26 +00009576 size_t protocol_name_len;
XiaokangQianc7403452022-06-23 03:24:12 +00009577 *out_len = 0;
XiaokangQianacb39922022-06-17 10:18:48 +00009578
Gilles Peskine449bd832023-01-11 14:50:10 +01009579 if (ssl->alpn_chosen == NULL) {
9580 return 0;
XiaokangQianacb39922022-06-17 10:18:48 +00009581 }
9582
Gilles Peskine449bd832023-01-11 14:50:10 +01009583 protocol_name_len = strlen(ssl->alpn_chosen);
9584 MBEDTLS_SSL_CHK_BUF_PTR(p, end, 7 + protocol_name_len);
XiaokangQianacb39922022-06-17 10:18:48 +00009585
Gilles Peskine449bd832023-01-11 14:50:10 +01009586 MBEDTLS_SSL_DEBUG_MSG(3, ("server side, adding alpn extension"));
XiaokangQianacb39922022-06-17 10:18:48 +00009587 /*
9588 * 0 . 1 ext identifier
9589 * 2 . 3 ext length
9590 * 4 . 5 protocol list length
9591 * 6 . 6 protocol name length
9592 * 7 . 7+n protocol name
9593 */
Gilles Peskine449bd832023-01-11 14:50:10 +01009594 MBEDTLS_PUT_UINT16_BE(MBEDTLS_TLS_EXT_ALPN, p, 0);
XiaokangQianacb39922022-06-17 10:18:48 +00009595
XiaokangQian95d5f542022-06-24 02:29:26 +00009596 *out_len = 7 + protocol_name_len;
XiaokangQianacb39922022-06-17 10:18:48 +00009597
Gilles Peskine449bd832023-01-11 14:50:10 +01009598 MBEDTLS_PUT_UINT16_BE(protocol_name_len + 3, p, 2);
9599 MBEDTLS_PUT_UINT16_BE(protocol_name_len + 1, p, 4);
XiaokangQian0b776e22022-06-24 09:04:59 +00009600 /* Note: the length of the chosen protocol has been checked to be less
9601 * than 255 bytes in `mbedtls_ssl_conf_alpn_protocols`.
9602 */
Gilles Peskine449bd832023-01-11 14:50:10 +01009603 p[6] = MBEDTLS_BYTE_0(protocol_name_len);
XiaokangQianacb39922022-06-17 10:18:48 +00009604
Gilles Peskine449bd832023-01-11 14:50:10 +01009605 memcpy(p + 7, ssl->alpn_chosen, protocol_name_len);
Jerry Yub95dd362022-11-08 21:19:34 +08009606
9607#if defined(MBEDTLS_SSL_PROTO_TLS1_3)
Gilles Peskine449bd832023-01-11 14:50:10 +01009608 mbedtls_ssl_tls13_set_hs_sent_ext_mask(ssl, MBEDTLS_TLS_EXT_ALPN);
Jerry Yub95dd362022-11-08 21:19:34 +08009609#endif
9610
Gilles Peskine449bd832023-01-11 14:50:10 +01009611 return 0;
XiaokangQianacb39922022-06-17 10:18:48 +00009612}
9613#endif /* MBEDTLS_SSL_ALPN */
9614
Xiaokang Qiana3b451f2022-10-11 06:20:56 +00009615#if defined(MBEDTLS_SSL_PROTO_TLS1_3) && \
Xiaokang Qian03409292022-10-12 02:49:52 +00009616 defined(MBEDTLS_SSL_SESSION_TICKETS) && \
Xiaokang Qianed0620c2022-10-12 06:58:13 +00009617 defined(MBEDTLS_SSL_SERVER_NAME_INDICATION) && \
Xiaokang Qianed3afcd2022-10-12 08:31:11 +00009618 defined(MBEDTLS_SSL_CLI_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01009619int mbedtls_ssl_session_set_hostname(mbedtls_ssl_session *session,
9620 const char *hostname)
Xiaokang Qiana3b451f2022-10-11 06:20:56 +00009621{
9622 /* Initialize to suppress unnecessary compiler warning */
9623 size_t hostname_len = 0;
9624
9625 /* Check if new hostname is valid before
9626 * making any change to current one */
Gilles Peskine449bd832023-01-11 14:50:10 +01009627 if (hostname != NULL) {
9628 hostname_len = strlen(hostname);
Xiaokang Qiana3b451f2022-10-11 06:20:56 +00009629
Gilles Peskine449bd832023-01-11 14:50:10 +01009630 if (hostname_len > MBEDTLS_SSL_MAX_HOST_NAME_LEN) {
9631 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
9632 }
Xiaokang Qiana3b451f2022-10-11 06:20:56 +00009633 }
9634
9635 /* Now it's clear that we will overwrite the old hostname,
9636 * so we can free it safely */
Gilles Peskine449bd832023-01-11 14:50:10 +01009637 if (session->hostname != NULL) {
9638 mbedtls_platform_zeroize(session->hostname,
9639 strlen(session->hostname));
9640 mbedtls_free(session->hostname);
Xiaokang Qiana3b451f2022-10-11 06:20:56 +00009641 }
9642
9643 /* Passing NULL as hostname shall clear the old one */
Gilles Peskine449bd832023-01-11 14:50:10 +01009644 if (hostname == NULL) {
Xiaokang Qiana3b451f2022-10-11 06:20:56 +00009645 session->hostname = NULL;
Gilles Peskine449bd832023-01-11 14:50:10 +01009646 } else {
9647 session->hostname = mbedtls_calloc(1, hostname_len + 1);
9648 if (session->hostname == NULL) {
9649 return MBEDTLS_ERR_SSL_ALLOC_FAILED;
9650 }
Xiaokang Qiana3b451f2022-10-11 06:20:56 +00009651
Gilles Peskine449bd832023-01-11 14:50:10 +01009652 memcpy(session->hostname, hostname, hostname_len);
Xiaokang Qiana3b451f2022-10-11 06:20:56 +00009653 }
9654
Gilles Peskine449bd832023-01-11 14:50:10 +01009655 return 0;
Xiaokang Qiana3b451f2022-10-11 06:20:56 +00009656}
Xiaokang Qian03409292022-10-12 02:49:52 +00009657#endif /* MBEDTLS_SSL_PROTO_TLS1_3 &&
9658 MBEDTLS_SSL_SESSION_TICKETS &&
Xiaokang Qianed0620c2022-10-12 06:58:13 +00009659 MBEDTLS_SSL_SERVER_NAME_INDICATION &&
Xiaokang Qianed3afcd2022-10-12 08:31:11 +00009660 MBEDTLS_SSL_CLI_C */
Xiaokang Qiana3b451f2022-10-11 06:20:56 +00009661
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009662#endif /* MBEDTLS_SSL_TLS_C */